• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2019, Alliance for Open Media. 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 AOM_AV1_ENCODER_SVC_LAYERCONTEXT_H_
12 #define AOM_AV1_ENCODER_SVC_LAYERCONTEXT_H_
13 
14 #include "av1/encoder/aq_cyclicrefresh.h"
15 #include "av1/encoder/encoder.h"
16 #include "av1/encoder/ratectrl.h"
17 
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21 
22 /*!
23  * \brief The stucture of quantities related to each spatial and temporal layer.
24  * \ingroup SVC
25  */
26 typedef struct {
27   /*!\cond */
28   RATE_CONTROL rc;
29   PRIMARY_RATE_CONTROL p_rc;
30   int framerate_factor;
31   int64_t layer_target_bitrate;
32   int scaling_factor_num;
33   int scaling_factor_den;
34   int64_t target_bandwidth;
35   int64_t spatial_layer_target_bandwidth;
36   double framerate;
37   int avg_frame_size;
38   int max_q;
39   int min_q;
40   int frames_from_key_frame;
41   /*!\endcond */
42 
43   /*!
44    * Cyclic refresh parameters (aq-mode=3), that need to be updated per-frame.
45    */
46   int sb_index;
47   /*!
48    * Segmentation map
49    */
50   int8_t *map;
51   /*!
52    * Segmentation map for last coded quantization paramters.
53    */
54   uint8_t *last_coded_q_map;
55 
56   /*!
57    * Number of blocks on segment 1
58    */
59   int actual_num_seg1_blocks;
60 
61   /*!
62    * Number of blocks on segment 2
63    */
64   int actual_num_seg2_blocks;
65   /*!
66    * Counter used to detect scene change.
67    */
68   int counter_encode_maxq_scene_change;
69 
70   /*!
71    * Speed settings for each layer.
72    */
73   uint8_t speed;
74   /*!
75    * GF group index.
76    */
77   unsigned char group_index;
78   /*!
79    * If current layer is key frame.
80    */
81   int is_key_frame;
82   /*!
83    * Maximum motion magnitude of previous encoded layer.
84    */
85   int max_mv_magnitude;
86 } LAYER_CONTEXT;
87 
88 /*!
89  * \brief The stucture of SVC.
90  * \ingroup SVC
91  */
92 typedef struct SVC {
93   /*!\cond */
94   int spatial_layer_id;
95   int temporal_layer_id;
96   int number_spatial_layers;
97   int number_temporal_layers;
98   int set_ref_frame_config;
99   int non_reference_frame;
100   int use_flexible_mode;
101   int ksvc_fixed_mode;
102   int ref_frame_comp[3];
103   /*!\endcond */
104 
105   /*!
106    * LAST_FRAME (0), LAST2_FRAME(1), LAST3_FRAME(2), GOLDEN_FRAME(3),
107    * BWDREF_FRAME(4), ALTREF2_FRAME(5), ALTREF_FRAME(6).
108    */
109   int reference[INTER_REFS_PER_FRAME];
110   /*!\cond */
111   int ref_idx[INTER_REFS_PER_FRAME];
112   int refresh[REF_FRAMES];
113   double base_framerate;
114   unsigned int current_superframe;
115   unsigned int buffer_time_index[REF_FRAMES];
116   unsigned char buffer_spatial_layer[REF_FRAMES];
117   int skip_mvsearch_last;
118   int skip_mvsearch_gf;
119   int spatial_layer_fb[REF_FRAMES];
120   int temporal_layer_fb[REF_FRAMES];
121   int num_encoded_top_layer;
122   int first_layer_denoise;
123   /*!\endcond */
124 
125   /*!
126    * Layer context used for rate control in CBR mode.
127    */
128   LAYER_CONTEXT layer_context[AOM_MAX_LAYERS];
129 
130   /*!
131    * EIGHTTAP_SMOOTH or BILINEAR
132    */
133   InterpFilter downsample_filter_type[AOM_MAX_SS_LAYERS];
134 
135   /*!
136    * Downsample_filter_phase: = 0 will do sub-sampling (no weighted average),
137    * = 8 will center the target pixel and get a symmetric averaging filter.
138    */
139   int downsample_filter_phase[AOM_MAX_SS_LAYERS];
140 
141   /*!
142    * Force zero-mv in mode search for the spatial/inter-layer reference.
143    */
144   int force_zero_mode_spatial_ref;
145 } SVC;
146 
147 struct AV1_COMP;
148 
149 /*!\brief Initialize layer context data from init_config().
150  *
151  * \ingroup SVC
152  * \callgraph
153  * \callergraph
154  *
155  * \param[in]       cpi  Top level encoder structure
156  *
157  * \return  Nothing returned. Set cpi->svc.
158  */
159 void av1_init_layer_context(struct AV1_COMP *const cpi);
160 
161 /*!\brief Update the layer context from a change_config() call.
162  *
163  * \ingroup SVC
164  * \callgraph
165  * \callergraph
166  *
167  * \param[in]       cpi  Top level encoder structure
168  * \param[in]       target_bandwidth  Total target bandwidth
169  *
170  * \return  Nothing returned. Buffer level for each layer is set.
171  */
172 void av1_update_layer_context_change_config(struct AV1_COMP *const cpi,
173                                             const int64_t target_bandwidth);
174 
175 /*!\brief Prior to encoding the frame, update framerate-related quantities
176           for the current temporal layer.
177  *
178  * \ingroup SVC
179  * \callgraph
180  * \callergraph
181  *
182  * \param[in]       cpi  Top level encoder structure
183  *
184  * \return  Nothing returned. Frame related quantities for current temporal
185  layer are updated.
186  */
187 void av1_update_temporal_layer_framerate(struct AV1_COMP *const cpi);
188 
189 /*!\brief Prior to encoding the frame, set the layer context, for the current
190  layer to be encoded, to the cpi struct.
191  *
192  * \ingroup SVC
193  * \callgraph
194  * \callergraph
195  *
196  * \param[in]       cpi  Top level encoder structure
197  *
198  * \return  Nothing returned. Layer context for current layer is set.
199  */
200 void av1_restore_layer_context(struct AV1_COMP *const cpi);
201 
202 /*!\brief Save the layer context after encoding the frame.
203  *
204  * \ingroup SVC
205  * \callgraph
206  * \callergraph
207  *
208  * \param[in]       cpi  Top level encoder structure
209  *
210  * \return  Nothing returned.
211  */
212 void av1_save_layer_context(struct AV1_COMP *const cpi);
213 
214 /*!\brief Free the memory used for cyclic refresh in layer context.
215  *
216  * \ingroup SVC
217  * \callgraph
218  * \callergraph
219  *
220  * \param[in]       cpi  Top level encoder structure
221  *
222  * \return  Nothing returned.
223  */
224 void av1_free_svc_cyclic_refresh(struct AV1_COMP *const cpi);
225 
226 /*!\brief Reset on key frame: reset counters, references and buffer updates.
227  *
228  * \ingroup SVC
229  * \callgraph
230  * \callergraph
231  *
232  * \param[in]       cpi  Top level encoder structure
233  * \param[in]       is_key  Whether current layer is key frame
234  *
235  * \return  Nothing returned.
236  */
237 void av1_svc_reset_temporal_layers(struct AV1_COMP *const cpi, int is_key);
238 
239 /*!\brief Before encoding, set resolutions and allocate compressor data.
240  *
241  * \ingroup SVC
242  * \callgraph
243  * \callergraph
244  *
245  * \param[in]       cpi  Top level encoder structure
246  *
247  * \return  Nothing returned.
248  */
249 void av1_one_pass_cbr_svc_start_layer(struct AV1_COMP *const cpi);
250 
251 /*!\brief Get primary reference frame for current layer
252  *
253  * \ingroup SVC
254  * \callgraph
255  * \callergraph
256  *
257  * \param[in]       cpi  Top level encoder structure
258  *
259  * \return  The primary reference frame for current layer.
260  */
261 int av1_svc_primary_ref_frame(const struct AV1_COMP *const cpi);
262 
263 /*!\brief Get resolution for current layer.
264  *
265  * \ingroup SVC
266  * \param[in]       width_org    Original width, unscaled
267  * \param[in]       height_org   Original height, unscaled
268  * \param[in]       num          Numerator for the scale ratio
269  * \param[in]       den          Denominator for the scale ratio
270  * \param[in]       width_out    Output width, scaled for current layer
271  * \param[in]       height_out   Output height, scaled for current layer
272  *
273  * \return Nothing is returned. Instead the scaled width and height are set.
274  */
275 void av1_get_layer_resolution(const int width_org, const int height_org,
276                               const int num, const int den, int *width_out,
277                               int *height_out);
278 
279 void av1_set_svc_fixed_mode(struct AV1_COMP *const cpi);
280 
281 void av1_svc_check_reset_layer_rc_flag(struct AV1_COMP *const cpi);
282 
283 #ifdef __cplusplus
284 }  // extern "C"
285 #endif
286 
287 #endif  // AOM_AV1_ENCODER_SVC_LAYERCONTEXT_H_
288