xref: /aosp_15_r20/external/libaom/av1/encoder/encode_strategy.h (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1 /*
2  * Copyright (c) 2019, Alliance for Open Media. All rights reserved.
3  *
4  * This source code is subject to the terms of the BSD 2 Clause License and
5  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6  * was not distributed with this source code in the LICENSE file, you can
7  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8  * Media Patent License 1.0 was not distributed with this source code in the
9  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10  */
11 
12 /*!\file
13  * \brief Declares frame encoding functions.
14  */
15 #ifndef AOM_AV1_ENCODER_ENCODE_STRATEGY_H_
16 #define AOM_AV1_ENCODER_ENCODE_STRATEGY_H_
17 
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21 
22 #include <stdint.h>
23 
24 #include "aom/aom_encoder.h"
25 
26 #include "av1/encoder/encoder.h"
27 #include "av1/encoder/firstpass.h"
28 
29 /*!\brief Implement high-level encode strategy
30  *
31  * \ingroup high_level_algo
32  * \callgraph
33  * \callergraph
34  * This function will implement high-level encode strategy, choosing frame type,
35  * frame placement, etc. It populates an EncodeFrameParams struct with the
36  * results of these decisions and then encodes the frame. The caller should use
37  * the output parameters *time_stamp and *time_end only when this function
38  * returns AOM_CODEC_OK.
39  *
40  * \param[in]    cpi         Top-level encoder structure
41  * \param[out]   size        Bitstream size
42  * \param[out]   dest        Bitstream output buffer
43  * \param[in]    dest_size   Bitstream output buffer size
44  * \param[in]    frame_flags Flags to decide how to encoding the frame
45  * \param[out]   time_stamp  Time stamp of the frame
46  * \param[out]   time_end    Time end
47  * \param[in]    timestamp_ratio Time base
48  * \param[in]    pop_lookahead Decide to pop the source frame from queue
49  * \param[in]    flush       Decide to encode one frame or the rest of frames
50  *
51  * \return Returns a value to indicate if the encoding is done successfully.
52  * \retval #AOM_CODEC_OK
53  * \retval -1
54  * \retval #AOM_CODEC_ERROR
55  */
56 int av1_encode_strategy(AV1_COMP *const cpi, size_t *const size,
57                         uint8_t *const dest, size_t dest_size,
58                         unsigned int *frame_flags, int64_t *const time_stamp,
59                         int64_t *const time_end,
60                         const aom_rational64_t *const timestamp_ratio,
61                         int *const pop_lookahead, int flush);
62 
63 /*!\cond */
64 // Set individual buffer update flags based on frame reference type.
65 // force_refresh_all is used when we have a KEY_FRAME or S_FRAME.  It forces all
66 // refresh_*_frame flags to be set, because we refresh all buffers in this case.
67 void av1_configure_buffer_updates(AV1_COMP *const cpi,
68                                   RefreshFrameInfo *const refresh_frame,
69                                   const FRAME_UPDATE_TYPE type,
70                                   const REFBUF_STATE refbuf_state,
71                                   int force_refresh_all);
72 
73 int av1_get_refresh_frame_flags(
74     const AV1_COMP *const cpi, const EncodeFrameParams *const frame_params,
75     FRAME_UPDATE_TYPE frame_update_type, int gf_index, int cur_disp_order,
76     RefFrameMapPair ref_frame_map_pairs[REF_FRAMES]);
77 
78 int av1_get_refresh_ref_frame_map(int refresh_frame_flags);
79 
80 /*!\brief Obtain indices of reference frames in ref_frame_map
81  *
82  * \callgraph
83  * \callergraph
84  *
85  * \param[out]   remapped_ref_idx  An array for storing indices of reference
86  *                                 frames. The index is used to retrieve a
87  *                                 reference frame buffer from ref_frame_map
88  *                                 in AV1Common.
89  */
90 void av1_get_ref_frames(RefFrameMapPair ref_frame_map_pairs[REF_FRAMES],
91                         int cur_frame_disp, const AV1_COMP *cpi, int gf_index,
92                         int is_parallel_encode,
93                         int remapped_ref_idx[REF_FRAMES]);
94 
95 int is_forced_keyframe_pending(struct lookahead_ctx *lookahead,
96                                const int up_to_index,
97                                const COMPRESSOR_STAGE compressor_stage);
98 
is_frame_droppable(const RTC_REF * const rtc_ref,const ExtRefreshFrameFlagsInfo * const ext_refresh_frame_flags)99 static inline int is_frame_droppable(
100     const RTC_REF *const rtc_ref,
101     const ExtRefreshFrameFlagsInfo *const ext_refresh_frame_flags) {
102   // Droppable frame is only used by external refresh flags. VoD setting won't
103   // trigger its use case.
104   if (rtc_ref->set_ref_frame_config)
105     return rtc_ref->non_reference_frame;
106   else if (ext_refresh_frame_flags->update_pending)
107     return !(ext_refresh_frame_flags->alt_ref_frame ||
108              ext_refresh_frame_flags->alt2_ref_frame ||
109              ext_refresh_frame_flags->bwd_ref_frame ||
110              ext_refresh_frame_flags->golden_frame ||
111              ext_refresh_frame_flags->last_frame);
112   else
113     return 0;
114 }
115 
get_current_frame_ref_type(const AV1_COMP * const cpi)116 static inline int get_current_frame_ref_type(const AV1_COMP *const cpi) {
117   // We choose the reference "type" of this frame from the flags which indicate
118   // which reference frames will be refreshed by it. More than one of these
119   // flags may be set, so the order here implies an order of precedence. This is
120   // just used to choose the primary_ref_frame (as the most recent reference
121   // buffer of the same reference-type as the current frame).
122 
123   switch (cpi->ppi->gf_group.layer_depth[cpi->gf_frame_index]) {
124     case 0: return 0;
125     case 1: return 1;
126     case MAX_ARF_LAYERS:
127     case MAX_ARF_LAYERS + 1: return 4;
128     default: return 7;
129   }
130 }
131 
132 int av1_calc_refresh_idx_for_intnl_arf(
133     AV1_COMP *cpi, RefFrameMapPair ref_frame_map_pairs[REF_FRAMES],
134     int gf_index);
135 /*!\endcond */
136 #ifdef __cplusplus
137 }  // extern "C"
138 #endif
139 
140 #endif  // AOM_AV1_ENCODER_ENCODE_STRATEGY_H_
141