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