xref: /aosp_15_r20/external/libaom/av1/encoder/svc_layercontext.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 #ifndef AOM_AV1_ENCODER_SVC_LAYERCONTEXT_H_
13 #define AOM_AV1_ENCODER_SVC_LAYERCONTEXT_H_
14 
15 #include "aom_scale/yv12config.h"
16 #include "av1/encoder/aq_cyclicrefresh.h"
17 #include "av1/encoder/encoder.h"
18 #include "av1/encoder/ratectrl.h"
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 
24 /*!
25  * \brief The stucture of quantities related to each spatial and temporal layer.
26  * \ingroup SVC
27  */
28 typedef struct {
29   /*!\cond */
30   RATE_CONTROL rc;
31   PRIMARY_RATE_CONTROL p_rc;
32   int framerate_factor;
33   int64_t layer_target_bitrate;  // In bits per second.
34   int scaling_factor_num;
35   int scaling_factor_den;
36   int64_t target_bandwidth;
37   int64_t spatial_layer_target_bandwidth;
38   double framerate;
39   int avg_frame_size;
40   int max_q;
41   int min_q;
42   int frames_from_key_frame;
43   /*!\endcond */
44 
45   /*!
46    * Cyclic refresh parameters (aq-mode=3), that need to be updated per-frame.
47    */
48   int sb_index;
49   /*!
50    * Segmentation map
51    */
52   int8_t *map;
53   /*!
54    * Number of blocks on segment 1
55    */
56   int actual_num_seg1_blocks;
57 
58   /*!
59    * Number of blocks on segment 2
60    */
61   int actual_num_seg2_blocks;
62   /*!
63    * Counter used to detect scene change.
64    */
65   int counter_encode_maxq_scene_change;
66 
67   /*!
68    * Speed settings for each layer.
69    */
70   uint8_t speed;
71   /*!
72    * GF group index.
73    */
74   unsigned char group_index;
75   /*!
76    * If current layer is key frame.
77    */
78   int is_key_frame;
79   /*!
80    * Maximum motion magnitude of previous encoded layer.
81    */
82   int max_mv_magnitude;
83 } LAYER_CONTEXT;
84 
85 /*!
86  * \brief The stucture of SVC.
87  * \ingroup SVC
88  */
89 typedef struct SVC {
90   /*!\cond */
91   int spatial_layer_id;
92   int temporal_layer_id;
93   int number_spatial_layers;
94   int number_temporal_layers;
95   int prev_number_spatial_layers;
96   int use_flexible_mode;
97   int ksvc_fixed_mode;
98   /*!\endcond */
99 
100   /*!\cond */
101   double base_framerate;
102   unsigned int current_superframe;
103   int skip_mvsearch_last;
104   int skip_mvsearch_gf;
105   int skip_mvsearch_altref;
106   int spatial_layer_fb[REF_FRAMES];
107   int temporal_layer_fb[REF_FRAMES];
108   int num_encoded_top_layer;
109   int first_layer_denoise;
110   YV12_BUFFER_CONFIG source_last_TL0;
111   int mi_cols_full_resoln;
112   int mi_rows_full_resoln;
113   /*!\endcond */
114 
115   /*!
116    * Layer context used for rate control in CBR mode.
117    * An array. The index for spatial layer `sl` and temporal layer `tl` is
118    * sl * number_temporal_layers + tl.
119    */
120   LAYER_CONTEXT *layer_context;
121 
122   /*!
123    * Number of layers allocated for layer_context. If nonzero, must be greater
124    * than or equal to number_spatial_layers * number_temporal_layers.
125    */
126   int num_allocated_layers;
127 
128   /*!
129    * EIGHTTAP_SMOOTH or BILINEAR
130    */
131   InterpFilter downsample_filter_type[AOM_MAX_SS_LAYERS];
132 
133   /*!
134    * Downsample_filter_phase: = 0 will do sub-sampling (no weighted average),
135    * = 8 will center the target pixel and get a symmetric averaging filter.
136    */
137   int downsample_filter_phase[AOM_MAX_SS_LAYERS];
138 
139   /*!
140    * Force zero-mv in mode search for the spatial/inter-layer reference.
141    */
142   int force_zero_mode_spatial_ref;
143 
144   /*!
145    * Flag to indicate that current spatial layer has a lower quality layer
146    * (at the same timestamp) that can be used as a reference.
147    * Lower quality layer refers to the same resolution but encoded at
148    * different/lower bitrate.
149    */
150   int has_lower_quality_layer;
151 
152   /*!
153    * Flag to indicate the frame drop mode for SVC: one of the two settings:
154    * AOM_LAYER_DROP (default) or AOM_FULL_SUPERFRAME_DROP.
155    */
156   AOM_SVC_FRAME_DROP_MODE framedrop_mode;
157 
158   /*!
159    * Flag to indicate if frame was dropped for a given spatial_layer_id on
160    * previous superframe.
161    */
162   bool last_layer_dropped[AOM_MAX_SS_LAYERS];
163 
164   /*!
165    * Flag to indicate if a previous spatial was dropped for the same superframe.
166    */
167   bool drop_spatial_layer[AOM_MAX_SS_LAYERS];
168 } SVC;
169 
170 struct AV1_COMP;
171 struct EncodeFrameInput;
172 
173 /*!\brief Initialize layer context data from init_config().
174  *
175  * \ingroup SVC
176  * \callgraph
177  * \callergraph
178  *
179  * \param[in]       cpi  Top level encoder structure
180  *
181  * \remark  Nothing returned. Set cpi->svc.
182  */
183 void av1_init_layer_context(struct AV1_COMP *const cpi);
184 
185 /*!\brief Allocate layer context data.
186  *
187  * \ingroup SVC
188  * \callgraph
189  * \callergraph
190  *
191  * \param[in]       cpi  Top level encoder structure
192  * \param[in]       num_layers  Number of layers to be allocated
193  *
194  * \remark  Allocates memory for cpi->svc.layer_context.
195  * \return  True on success, false on allocation failure.
196  */
197 bool av1_alloc_layer_context(struct AV1_COMP *cpi, int num_layers);
198 
199 /*!\brief Update the layer context from a change_config() call.
200  *
201  * \ingroup SVC
202  * \callgraph
203  * \callergraph
204  *
205  * \param[in]       cpi  Top level encoder structure
206  * \param[in]       target_bandwidth  Total target bandwidth
207  *
208  * \remark  Nothing returned. Buffer level for each layer is set.
209  */
210 void av1_update_layer_context_change_config(struct AV1_COMP *const cpi,
211                                             const int64_t target_bandwidth);
212 
213 /*!\brief Prior to encoding the frame, update framerate-related quantities
214           for the current temporal layer.
215  *
216  * \ingroup SVC
217  * \callgraph
218  * \callergraph
219  *
220  * \param[in]       cpi  Top level encoder structure
221  *
222  * \remark  Nothing returned. Frame related quantities for current temporal
223  layer are updated.
224  */
225 void av1_update_temporal_layer_framerate(struct AV1_COMP *const cpi);
226 
227 /*!\brief Prior to check if reference is lower spatial layer at the same
228  *        timestamp/superframe.
229  *
230  * \ingroup SVC
231  * \callgraph
232  * \callergraph
233  *
234  * \param[in]       cpi  Top level encoder structure
235  * \param[in]       ref_frame Reference frame
236  *
237  * \return  True if the ref_frame if lower spatial layer, otherwise false.
238  */
239 bool av1_check_ref_is_low_spatial_res_super_frame(struct AV1_COMP *const cpi,
240                                                   int ref_frame);
241 
242 /*!\brief Prior to encoding the frame, set the layer context, for the current
243  layer to be encoded, to the cpi struct.
244  *
245  * \ingroup SVC
246  * \callgraph
247  * \callergraph
248  *
249  * \param[in]       cpi  Top level encoder structure
250  *
251  * \remark  Nothing returned. Layer context for current layer is set.
252  */
253 void av1_restore_layer_context(struct AV1_COMP *const cpi);
254 
255 /*!\brief Save the layer context after encoding the frame.
256  *
257  * \ingroup SVC
258  * \callgraph
259  * \callergraph
260  *
261  * \param[in]       cpi  Top level encoder structure
262  */
263 void av1_save_layer_context(struct AV1_COMP *const cpi);
264 
265 /*!\brief Free the memory used for cyclic refresh in layer context.
266  *
267  * \ingroup SVC
268  * \callgraph
269  * \callergraph
270  *
271  * \param[in]       cpi  Top level encoder structure
272  */
273 void av1_free_svc_cyclic_refresh(struct AV1_COMP *const cpi);
274 
275 /*!\brief Reset on key frame: reset counters, references and buffer updates.
276  *
277  * \ingroup SVC
278  * \callgraph
279  * \callergraph
280  *
281  * \param[in]       cpi  Top level encoder structure
282  * \param[in]       is_key  Whether current layer is key frame
283  */
284 void av1_svc_reset_temporal_layers(struct AV1_COMP *const cpi, int is_key);
285 
286 /*!\brief Before encoding, set resolutions and allocate compressor data.
287  *
288  * \ingroup SVC
289  * \callgraph
290  * \callergraph
291  *
292  * \param[in]       cpi  Top level encoder structure
293  */
294 void av1_one_pass_cbr_svc_start_layer(struct AV1_COMP *const cpi);
295 
296 /*!\brief Get primary reference frame for current layer
297  *
298  * \ingroup SVC
299  * \callgraph
300  * \callergraph
301  *
302  * \param[in]       cpi  Top level encoder structure
303  *
304  * \return  The primary reference frame for current layer.
305  */
306 int av1_svc_primary_ref_frame(const struct AV1_COMP *const cpi);
307 
308 /*!\brief Get resolution for current layer.
309  *
310  * \ingroup SVC
311  * \param[in]       width_org    Original width, unscaled
312  * \param[in]       height_org   Original height, unscaled
313  * \param[in]       num          Numerator for the scale ratio
314  * \param[in]       den          Denominator for the scale ratio
315  * \param[in]       width_out    Output width, scaled for current layer
316  * \param[in]       height_out   Output height, scaled for current layer
317  *
318  * \remark Nothing is returned. Instead the scaled width and height are set.
319  */
320 void av1_get_layer_resolution(const int width_org, const int height_org,
321                               const int num, const int den, int *width_out,
322                               int *height_out);
323 
324 void av1_set_svc_fixed_mode(struct AV1_COMP *const cpi);
325 
326 void av1_svc_check_reset_layer_rc_flag(struct AV1_COMP *const cpi);
327 
328 void av1_svc_set_last_source(struct AV1_COMP *const cpi,
329                              struct EncodeFrameInput *frame_input,
330                              YV12_BUFFER_CONFIG *prev_source);
331 
332 void av1_svc_update_buffer_slot_refreshed(struct AV1_COMP *const cpi);
333 
334 int av1_svc_get_min_ref_dist(const struct AV1_COMP *cpi);
335 
336 void av1_svc_set_reference_was_previous(struct AV1_COMP *cpi);
337 #ifdef __cplusplus
338 }  // extern "C"
339 #endif
340 
341 #endif  // AOM_AV1_ENCODER_SVC_LAYERCONTEXT_H_
342