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