xref: /aosp_15_r20/external/libvpx/vp9/encoder/vp9_svc_layercontext.h (revision fb1b10ab9aebc7c7068eedab379b749d7e3900be)
1 /*
2  *  Copyright (c) 2014 The WebM project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #ifndef VPX_VP9_ENCODER_VP9_SVC_LAYERCONTEXT_H_
12 #define VPX_VP9_ENCODER_VP9_SVC_LAYERCONTEXT_H_
13 
14 #include "vpx/vpx_encoder.h"
15 
16 #include "vp9/encoder/vp9_ratectrl.h"
17 
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21 
22 typedef enum {
23   // Inter-layer prediction is on on all frames.
24   INTER_LAYER_PRED_ON,
25   // Inter-layer prediction is off on all frames.
26   INTER_LAYER_PRED_OFF,
27   // Inter-layer prediction is off on non-key frames and non-sync frames.
28   INTER_LAYER_PRED_OFF_NONKEY,
29   // Inter-layer prediction is on on all frames, but constrained such
30   // that any layer S (> 0) can only predict from previous spatial
31   // layer S-1, from the same superframe.
32   INTER_LAYER_PRED_ON_CONSTRAINED
33 } INTER_LAYER_PRED;
34 
35 typedef struct BUFFER_LONGTERM_REF {
36   int idx;
37   int is_used;
38 } BUFFER_LONGTERM_REF;
39 
40 typedef struct {
41   RATE_CONTROL rc;
42   int target_bandwidth;
43   int spatial_layer_target_bandwidth;  // Target for the spatial layer.
44   double framerate;
45   int avg_frame_size;
46   int max_q;
47   int min_q;
48   int scaling_factor_num;
49   int scaling_factor_den;
50   // Scaling factors used for internal resize scaling for single layer SVC.
51   int scaling_factor_num_resize;
52   int scaling_factor_den_resize;
53   TWO_PASS twopass;
54   vpx_fixed_buf_t rc_twopass_stats_in;
55   unsigned int current_video_frame_in_layer;
56   int is_key_frame;
57   int frames_from_key_frame;
58   FRAME_TYPE last_frame_type;
59   struct lookahead_entry *alt_ref_source;
60   int alt_ref_idx;
61   int gold_ref_idx;
62   int has_alt_frame;
63   size_t layer_size;
64   // Cyclic refresh parameters (aq-mode=3), that need to be updated per-frame.
65   // TODO(jianj/marpan): Is it better to use the full cyclic refresh struct.
66   int sb_index;
67   signed char *map;
68   uint8_t *last_coded_q_map;
69   uint8_t *consec_zero_mv;
70   int actual_num_seg1_blocks;
71   int actual_num_seg2_blocks;
72   int counter_encode_maxq_scene_change;
73   int qindex_delta[3];
74   uint8_t speed;
75   int loopfilter_ctrl;
76   int frame_qp;
77   int MBs;
78 } LAYER_CONTEXT;
79 
80 typedef struct SVC {
81   int spatial_layer_id;
82   int temporal_layer_id;
83   int number_spatial_layers;
84   int number_temporal_layers;
85 
86   int spatial_layer_to_encode;
87 
88   // Workaround for multiple frame contexts
89   enum { ENCODED = 0, ENCODING, NEED_TO_ENCODE } encode_empty_frame_state;
90   struct lookahead_entry empty_frame;
91   int encode_intra_empty_frame;
92 
93   // Store scaled source frames to be used for temporal filter to generate
94   // a alt ref frame.
95   YV12_BUFFER_CONFIG scaled_frames[MAX_LAG_BUFFERS];
96   // Temp buffer used for 2-stage down-sampling, for real-time mode.
97   YV12_BUFFER_CONFIG scaled_temp;
98   int scaled_one_half;
99   int scaled_temp_is_alloc;
100 
101   // Layer context used for rate control in one pass temporal CBR mode or
102   // two pass spatial mode.
103   LAYER_CONTEXT layer_context[VPX_MAX_LAYERS];
104   // Indicates what sort of temporal layering is used.
105   // Currently, this only works for CBR mode.
106   VP9E_TEMPORAL_LAYERING_MODE temporal_layering_mode;
107   // Frame flags and buffer indexes for each spatial layer, set by the
108   // application (external settings).
109   int ext_frame_flags[VPX_MAX_LAYERS];
110   int lst_fb_idx[VPX_MAX_LAYERS];
111   int gld_fb_idx[VPX_MAX_LAYERS];
112   int alt_fb_idx[VPX_MAX_LAYERS];
113   int force_zero_mode_spatial_ref;
114   // Sequence level flag to enable second (long term) temporal reference.
115   int use_gf_temporal_ref;
116   // Frame level flag to enable second (long term) temporal reference.
117   int use_gf_temporal_ref_current_layer;
118   // Allow second reference for at most 2 top highest resolution layers.
119   BUFFER_LONGTERM_REF buffer_gf_temporal_ref[2];
120   int current_superframe;
121   int non_reference_frame;
122   int use_base_mv;
123   int use_partition_reuse;
124   // Used to control the downscaling filter for source scaling, for 1 pass CBR.
125   // downsample_filter_phase: = 0 will do sub-sampling (no weighted average),
126   // = 8 will center the target pixel and get a symmetric averaging filter.
127   // downsample_filter_type: 4 filters may be used: eighttap_regular,
128   // eighttap_smooth, eighttap_sharp, and bilinear.
129   INTERP_FILTER downsample_filter_type[VPX_SS_MAX_LAYERS];
130   int downsample_filter_phase[VPX_SS_MAX_LAYERS];
131 
132   BLOCK_SIZE *prev_partition_svc;
133   int mi_stride[VPX_MAX_LAYERS];
134   int mi_rows[VPX_MAX_LAYERS];
135   int mi_cols[VPX_MAX_LAYERS];
136 
137   int first_layer_denoise;
138 
139   int skip_enhancement_layer;
140 
141   int lower_layer_qindex;
142 
143   int last_layer_dropped[VPX_MAX_LAYERS];
144   int drop_spatial_layer[VPX_MAX_LAYERS];
145   int framedrop_thresh[VPX_MAX_LAYERS];
146   int drop_count[VPX_MAX_LAYERS];
147   int force_drop_constrained_from_above[VPX_MAX_LAYERS];
148   int max_consec_drop;
149   SVC_LAYER_DROP_MODE framedrop_mode;
150 
151   INTER_LAYER_PRED disable_inter_layer_pred;
152 
153   // Flag to indicate scene change and high num of motion blocks at current
154   // superframe, scene detection is currently checked for each superframe prior
155   // to encoding, on the full resolution source.
156   int high_source_sad_superframe;
157   int high_num_blocks_with_motion;
158 
159   // Flags used to get SVC pattern info.
160   int update_buffer_slot[VPX_SS_MAX_LAYERS];
161   uint8_t reference_last[VPX_SS_MAX_LAYERS];
162   uint8_t reference_golden[VPX_SS_MAX_LAYERS];
163   uint8_t reference_altref[VPX_SS_MAX_LAYERS];
164   // TODO(jianj): Remove these last 3, deprecated.
165   uint8_t update_last[VPX_SS_MAX_LAYERS];
166   uint8_t update_golden[VPX_SS_MAX_LAYERS];
167   uint8_t update_altref[VPX_SS_MAX_LAYERS];
168 
169   // Keep track of the frame buffer index updated/refreshed on the base
170   // temporal superframe.
171   int fb_idx_upd_tl0[VPX_SS_MAX_LAYERS];
172 
173   // Keep track of the spatial and temporal layer id of the frame that last
174   // updated the frame buffer index.
175   uint8_t fb_idx_spatial_layer_id[REF_FRAMES];
176   uint8_t fb_idx_temporal_layer_id[REF_FRAMES];
177 
178   int spatial_layer_sync[VPX_SS_MAX_LAYERS];
179   // Quantizer for each spatial layer.
180   int base_qindex[VPX_SS_MAX_LAYERS];
181   uint8_t set_intra_only_frame;
182   uint8_t previous_frame_is_intra_only;
183   uint8_t superframe_has_layer_sync;
184 
185   uint8_t fb_idx_base[REF_FRAMES];
186 
187   int use_set_ref_frame_config;
188 
189   int temporal_layer_id_per_spatial[VPX_SS_MAX_LAYERS];
190 
191   int first_spatial_layer_to_encode;
192 
193   // Parameters for allowing framerate per spatial layer, and buffer
194   // update based on timestamps.
195   int64_t duration[VPX_SS_MAX_LAYERS];
196   int64_t timebase_fac;
197   int64_t time_stamp_superframe;
198   int64_t time_stamp_prev[VPX_SS_MAX_LAYERS];
199 
200   int num_encoded_top_layer;
201 
202   // Every spatial layer on a superframe whose base is key is key too.
203   int simulcast_mode;
204 
205   // Flag to indicate SVC is dynamically switched to a single layer.
206   int single_layer_svc;
207   int resize_set;
208 } SVC;
209 
210 struct VP9_COMP;
211 
212 // Initialize layer context data from init_config().
213 void vp9_init_layer_context(struct VP9_COMP *const cpi);
214 
215 // Update the layer context from a change_config() call.
216 void vp9_update_layer_context_change_config(struct VP9_COMP *const cpi,
217                                             const int target_bandwidth);
218 
219 // Prior to encoding the frame, update framerate-related quantities
220 // for the current temporal layer.
221 void vp9_update_temporal_layer_framerate(struct VP9_COMP *const cpi);
222 
223 // Update framerate-related quantities for the current spatial layer.
224 void vp9_update_spatial_layer_framerate(struct VP9_COMP *const cpi,
225                                         double framerate);
226 
227 // Prior to encoding the frame, set the layer context, for the current layer
228 // to be encoded, to the cpi struct.
229 void vp9_restore_layer_context(struct VP9_COMP *const cpi);
230 
231 // Save the layer context after encoding the frame.
232 void vp9_save_layer_context(struct VP9_COMP *const cpi);
233 
234 // Initialize second pass rc for spatial svc.
235 void vp9_init_second_pass_spatial_svc(struct VP9_COMP *cpi);
236 
237 void get_layer_resolution(const int width_org, const int height_org,
238                           const int num, const int den, int *width_out,
239                           int *height_out);
240 
241 // Increment number of video frames in layer
242 void vp9_inc_frame_in_layer(struct VP9_COMP *const cpi);
243 
244 // Check if current layer is key frame in spatial upper layer
245 int vp9_is_upper_layer_key_frame(const struct VP9_COMP *const cpi);
246 
247 // Get the next source buffer to encode
248 struct lookahead_entry *vp9_svc_lookahead_pop(struct VP9_COMP *const cpi,
249                                               struct lookahead_ctx *ctx,
250                                               int drain);
251 
252 // Start a frame and initialize svc parameters
253 int vp9_svc_start_frame(struct VP9_COMP *const cpi);
254 
255 #if CONFIG_VP9_TEMPORAL_DENOISING
256 int vp9_denoise_svc_non_key(struct VP9_COMP *const cpi);
257 #endif
258 
259 void vp9_copy_flags_ref_update_idx(struct VP9_COMP *const cpi);
260 
261 int vp9_one_pass_svc_start_layer(struct VP9_COMP *const cpi);
262 
263 void vp9_free_svc_cyclic_refresh(struct VP9_COMP *const cpi);
264 
265 void vp9_svc_reset_temporal_layers(struct VP9_COMP *const cpi, int is_key);
266 
267 void vp9_svc_check_reset_layer_rc_flag(struct VP9_COMP *const cpi);
268 
269 void vp9_svc_constrain_inter_layer_pred(struct VP9_COMP *const cpi);
270 
271 void vp9_svc_assert_constraints_pattern(struct VP9_COMP *const cpi);
272 
273 void vp9_svc_check_spatial_layer_sync(struct VP9_COMP *const cpi);
274 
275 void vp9_svc_update_ref_frame_buffer_idx(struct VP9_COMP *const cpi);
276 
277 void vp9_svc_update_ref_frame_key_simulcast(struct VP9_COMP *const cpi);
278 
279 void vp9_svc_update_ref_frame(struct VP9_COMP *const cpi);
280 
281 void vp9_svc_adjust_frame_rate(struct VP9_COMP *const cpi);
282 
283 void vp9_svc_adjust_avg_frame_qindex(struct VP9_COMP *const cpi);
284 
285 int vp9_svc_check_skip_enhancement_layer(struct VP9_COMP *const cpi);
286 #ifdef __cplusplus
287 }  // extern "C"
288 #endif
289 
290 #endif  // VPX_VP9_ENCODER_VP9_SVC_LAYERCONTEXT_H_
291