• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   uint8_t speed;
74   int loopfilter_ctrl;
75 } LAYER_CONTEXT;
76 
77 typedef struct SVC {
78   int spatial_layer_id;
79   int temporal_layer_id;
80   int number_spatial_layers;
81   int number_temporal_layers;
82 
83   int spatial_layer_to_encode;
84 
85   // Workaround for multiple frame contexts
86   enum { ENCODED = 0, ENCODING, NEED_TO_ENCODE } encode_empty_frame_state;
87   struct lookahead_entry empty_frame;
88   int encode_intra_empty_frame;
89 
90   // Store scaled source frames to be used for temporal filter to generate
91   // a alt ref frame.
92   YV12_BUFFER_CONFIG scaled_frames[MAX_LAG_BUFFERS];
93   // Temp buffer used for 2-stage down-sampling, for real-time mode.
94   YV12_BUFFER_CONFIG scaled_temp;
95   int scaled_one_half;
96   int scaled_temp_is_alloc;
97 
98   // Layer context used for rate control in one pass temporal CBR mode or
99   // two pass spatial mode.
100   LAYER_CONTEXT layer_context[VPX_MAX_LAYERS];
101   // Indicates what sort of temporal layering is used.
102   // Currently, this only works for CBR mode.
103   VP9E_TEMPORAL_LAYERING_MODE temporal_layering_mode;
104   // Frame flags and buffer indexes for each spatial layer, set by the
105   // application (external settings).
106   int ext_frame_flags[VPX_MAX_LAYERS];
107   int lst_fb_idx[VPX_MAX_LAYERS];
108   int gld_fb_idx[VPX_MAX_LAYERS];
109   int alt_fb_idx[VPX_MAX_LAYERS];
110   int force_zero_mode_spatial_ref;
111   // Sequence level flag to enable second (long term) temporal reference.
112   int use_gf_temporal_ref;
113   // Frame level flag to enable second (long term) temporal reference.
114   int use_gf_temporal_ref_current_layer;
115   // Allow second reference for at most 2 top highest resolution layers.
116   BUFFER_LONGTERM_REF buffer_gf_temporal_ref[2];
117   int current_superframe;
118   int non_reference_frame;
119   int use_base_mv;
120   int use_partition_reuse;
121   // Used to control the downscaling filter for source scaling, for 1 pass CBR.
122   // downsample_filter_phase: = 0 will do sub-sampling (no weighted average),
123   // = 8 will center the target pixel and get a symmetric averaging filter.
124   // downsample_filter_type: 4 filters may be used: eighttap_regular,
125   // eighttap_smooth, eighttap_sharp, and bilinear.
126   INTERP_FILTER downsample_filter_type[VPX_SS_MAX_LAYERS];
127   int downsample_filter_phase[VPX_SS_MAX_LAYERS];
128 
129   BLOCK_SIZE *prev_partition_svc;
130   int mi_stride[VPX_MAX_LAYERS];
131   int mi_rows[VPX_MAX_LAYERS];
132   int mi_cols[VPX_MAX_LAYERS];
133 
134   int first_layer_denoise;
135 
136   int skip_enhancement_layer;
137 
138   int lower_layer_qindex;
139 
140   int last_layer_dropped[VPX_MAX_LAYERS];
141   int drop_spatial_layer[VPX_MAX_LAYERS];
142   int framedrop_thresh[VPX_MAX_LAYERS];
143   int drop_count[VPX_MAX_LAYERS];
144   int force_drop_constrained_from_above[VPX_MAX_LAYERS];
145   int max_consec_drop;
146   SVC_LAYER_DROP_MODE framedrop_mode;
147 
148   INTER_LAYER_PRED disable_inter_layer_pred;
149 
150   // Flag to indicate scene change and high num of motion blocks at current
151   // superframe, scene detection is currently checked for each superframe prior
152   // to encoding, on the full resolution source.
153   int high_source_sad_superframe;
154   int high_num_blocks_with_motion;
155 
156   // Flags used to get SVC pattern info.
157   int update_buffer_slot[VPX_SS_MAX_LAYERS];
158   uint8_t reference_last[VPX_SS_MAX_LAYERS];
159   uint8_t reference_golden[VPX_SS_MAX_LAYERS];
160   uint8_t reference_altref[VPX_SS_MAX_LAYERS];
161   // TODO(jianj): Remove these last 3, deprecated.
162   uint8_t update_last[VPX_SS_MAX_LAYERS];
163   uint8_t update_golden[VPX_SS_MAX_LAYERS];
164   uint8_t update_altref[VPX_SS_MAX_LAYERS];
165 
166   // Keep track of the frame buffer index updated/refreshed on the base
167   // temporal superframe.
168   int fb_idx_upd_tl0[VPX_SS_MAX_LAYERS];
169 
170   // Keep track of the spatial and temporal layer id of the frame that last
171   // updated the frame buffer index.
172   uint8_t fb_idx_spatial_layer_id[REF_FRAMES];
173   uint8_t fb_idx_temporal_layer_id[REF_FRAMES];
174 
175   int spatial_layer_sync[VPX_SS_MAX_LAYERS];
176   // Quantizer for each spatial layer.
177   int base_qindex[VPX_SS_MAX_LAYERS];
178   uint8_t set_intra_only_frame;
179   uint8_t previous_frame_is_intra_only;
180   uint8_t superframe_has_layer_sync;
181 
182   uint8_t fb_idx_base[REF_FRAMES];
183 
184   int use_set_ref_frame_config;
185 
186   int temporal_layer_id_per_spatial[VPX_SS_MAX_LAYERS];
187 
188   int first_spatial_layer_to_encode;
189 
190   // Parameters for allowing framerate per spatial layer, and buffer
191   // update based on timestamps.
192   int64_t duration[VPX_SS_MAX_LAYERS];
193   int64_t timebase_fac;
194   int64_t time_stamp_superframe;
195   int64_t time_stamp_prev[VPX_SS_MAX_LAYERS];
196 
197   int num_encoded_top_layer;
198 
199   // Every spatial layer on a superframe whose base is key is key too.
200   int simulcast_mode;
201 
202   // Flag to indicate SVC is dynamically switched to a single layer.
203   int single_layer_svc;
204   int resize_set;
205 } SVC;
206 
207 struct VP9_COMP;
208 
209 // Initialize layer context data from init_config().
210 void vp9_init_layer_context(struct VP9_COMP *const cpi);
211 
212 // Update the layer context from a change_config() call.
213 void vp9_update_layer_context_change_config(struct VP9_COMP *const cpi,
214                                             const int target_bandwidth);
215 
216 // Prior to encoding the frame, update framerate-related quantities
217 // for the current temporal layer.
218 void vp9_update_temporal_layer_framerate(struct VP9_COMP *const cpi);
219 
220 // Update framerate-related quantities for the current spatial layer.
221 void vp9_update_spatial_layer_framerate(struct VP9_COMP *const cpi,
222                                         double framerate);
223 
224 // Prior to encoding the frame, set the layer context, for the current layer
225 // to be encoded, to the cpi struct.
226 void vp9_restore_layer_context(struct VP9_COMP *const cpi);
227 
228 // Save the layer context after encoding the frame.
229 void vp9_save_layer_context(struct VP9_COMP *const cpi);
230 
231 // Initialize second pass rc for spatial svc.
232 void vp9_init_second_pass_spatial_svc(struct VP9_COMP *cpi);
233 
234 void get_layer_resolution(const int width_org, const int height_org,
235                           const int num, const int den, int *width_out,
236                           int *height_out);
237 
238 // Increment number of video frames in layer
239 void vp9_inc_frame_in_layer(struct VP9_COMP *const cpi);
240 
241 // Check if current layer is key frame in spatial upper layer
242 int vp9_is_upper_layer_key_frame(const struct VP9_COMP *const cpi);
243 
244 // Get the next source buffer to encode
245 struct lookahead_entry *vp9_svc_lookahead_pop(struct VP9_COMP *const cpi,
246                                               struct lookahead_ctx *ctx,
247                                               int drain);
248 
249 // Start a frame and initialize svc parameters
250 int vp9_svc_start_frame(struct VP9_COMP *const cpi);
251 
252 #if CONFIG_VP9_TEMPORAL_DENOISING
253 int vp9_denoise_svc_non_key(struct VP9_COMP *const cpi);
254 #endif
255 
256 void vp9_copy_flags_ref_update_idx(struct VP9_COMP *const cpi);
257 
258 int vp9_one_pass_svc_start_layer(struct VP9_COMP *const cpi);
259 
260 void vp9_free_svc_cyclic_refresh(struct VP9_COMP *const cpi);
261 
262 void vp9_svc_reset_temporal_layers(struct VP9_COMP *const cpi, int is_key);
263 
264 void vp9_svc_check_reset_layer_rc_flag(struct VP9_COMP *const cpi);
265 
266 void vp9_svc_constrain_inter_layer_pred(struct VP9_COMP *const cpi);
267 
268 void vp9_svc_assert_constraints_pattern(struct VP9_COMP *const cpi);
269 
270 void vp9_svc_check_spatial_layer_sync(struct VP9_COMP *const cpi);
271 
272 void vp9_svc_update_ref_frame_buffer_idx(struct VP9_COMP *const cpi);
273 
274 void vp9_svc_update_ref_frame_key_simulcast(struct VP9_COMP *const cpi);
275 
276 void vp9_svc_update_ref_frame(struct VP9_COMP *const cpi);
277 
278 void vp9_svc_adjust_frame_rate(struct VP9_COMP *const cpi);
279 
280 void vp9_svc_adjust_avg_frame_qindex(struct VP9_COMP *const cpi);
281 #ifdef __cplusplus
282 }  // extern "C"
283 #endif
284 
285 #endif  // VPX_VP9_ENCODER_VP9_SVC_LAYERCONTEXT_H_
286