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   // Cyclic refresh parameters (aq-mode=3), that need to be updated per-frame.
62   // TODO(jianj/marpan): Is it better to use the full cyclic refresh struct.
63   int sb_index;
64   signed char *map;
65   uint8_t *last_coded_q_map;
66   uint8_t *consec_zero_mv;
67   int actual_num_seg1_blocks;
68   int actual_num_seg2_blocks;
69   int counter_encode_maxq_scene_change;
70   uint8_t speed;
71 } LAYER_CONTEXT;
72 
73 typedef struct SVC {
74   int spatial_layer_id;
75   int temporal_layer_id;
76   int number_spatial_layers;
77   int number_temporal_layers;
78 
79   int spatial_layer_to_encode;
80 
81   // Workaround for multiple frame contexts
82   enum { ENCODED = 0, ENCODING, NEED_TO_ENCODE } encode_empty_frame_state;
83   struct lookahead_entry empty_frame;
84   int encode_intra_empty_frame;
85 
86   // Store scaled source frames to be used for temporal filter to generate
87   // a alt ref frame.
88   YV12_BUFFER_CONFIG scaled_frames[MAX_LAG_BUFFERS];
89   // Temp buffer used for 2-stage down-sampling, for real-time mode.
90   YV12_BUFFER_CONFIG scaled_temp;
91   int scaled_one_half;
92   int scaled_temp_is_alloc;
93 
94   // Layer context used for rate control in one pass temporal CBR mode or
95   // two pass spatial mode.
96   LAYER_CONTEXT layer_context[VPX_MAX_LAYERS];
97   // Indicates what sort of temporal layering is used.
98   // Currently, this only works for CBR mode.
99   VP9E_TEMPORAL_LAYERING_MODE temporal_layering_mode;
100   // Frame flags and buffer indexes for each spatial layer, set by the
101   // application (external settings).
102   int ext_frame_flags[VPX_MAX_LAYERS];
103   int lst_fb_idx[VPX_MAX_LAYERS];
104   int gld_fb_idx[VPX_MAX_LAYERS];
105   int alt_fb_idx[VPX_MAX_LAYERS];
106   int force_zero_mode_spatial_ref;
107   // Sequence level flag to enable second (long term) temporal reference.
108   int use_gf_temporal_ref;
109   // Frame level flag to enable second (long term) temporal reference.
110   int use_gf_temporal_ref_current_layer;
111   // Allow second reference for at most 2 top highest resolution layers.
112   BUFFER_LONGTERM_REF buffer_gf_temporal_ref[2];
113   int current_superframe;
114   int non_reference_frame;
115   int use_base_mv;
116   int use_partition_reuse;
117   // Used to control the downscaling filter for source scaling, for 1 pass CBR.
118   // downsample_filter_phase: = 0 will do sub-sampling (no weighted average),
119   // = 8 will center the target pixel and get a symmetric averaging filter.
120   // downsample_filter_type: 4 filters may be used: eighttap_regular,
121   // eighttap_smooth, eighttap_sharp, and bilinear.
122   INTERP_FILTER downsample_filter_type[VPX_SS_MAX_LAYERS];
123   int downsample_filter_phase[VPX_SS_MAX_LAYERS];
124 
125   BLOCK_SIZE *prev_partition_svc;
126   int mi_stride[VPX_MAX_LAYERS];
127   int mi_rows[VPX_MAX_LAYERS];
128   int mi_cols[VPX_MAX_LAYERS];
129 
130   int first_layer_denoise;
131 
132   int skip_enhancement_layer;
133 
134   int lower_layer_qindex;
135 
136   int last_layer_dropped[VPX_MAX_LAYERS];
137   int drop_spatial_layer[VPX_MAX_LAYERS];
138   int framedrop_thresh[VPX_MAX_LAYERS];
139   int drop_count[VPX_MAX_LAYERS];
140   int force_drop_constrained_from_above[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 
193   // Every spatial layer on a superframe whose base is key is key too.
194   int simulcast_mode;
195 } SVC;
196 
197 struct VP9_COMP;
198 
199 // Initialize layer context data from init_config().
200 void vp9_init_layer_context(struct VP9_COMP *const cpi);
201 
202 // Update the layer context from a change_config() call.
203 void vp9_update_layer_context_change_config(struct VP9_COMP *const cpi,
204                                             const int target_bandwidth);
205 
206 // Prior to encoding the frame, update framerate-related quantities
207 // for the current temporal layer.
208 void vp9_update_temporal_layer_framerate(struct VP9_COMP *const cpi);
209 
210 // Update framerate-related quantities for the current spatial layer.
211 void vp9_update_spatial_layer_framerate(struct VP9_COMP *const cpi,
212                                         double framerate);
213 
214 // Prior to encoding the frame, set the layer context, for the current layer
215 // to be encoded, to the cpi struct.
216 void vp9_restore_layer_context(struct VP9_COMP *const cpi);
217 
218 // Save the layer context after encoding the frame.
219 void vp9_save_layer_context(struct VP9_COMP *const cpi);
220 
221 // Initialize second pass rc for spatial svc.
222 void vp9_init_second_pass_spatial_svc(struct VP9_COMP *cpi);
223 
224 void get_layer_resolution(const int width_org, const int height_org,
225                           const int num, const int den, int *width_out,
226                           int *height_out);
227 
228 // Increment number of video frames in layer
229 void vp9_inc_frame_in_layer(struct VP9_COMP *const cpi);
230 
231 // Check if current layer is key frame in spatial upper layer
232 int vp9_is_upper_layer_key_frame(const struct VP9_COMP *const cpi);
233 
234 // Get the next source buffer to encode
235 struct lookahead_entry *vp9_svc_lookahead_pop(struct VP9_COMP *const cpi,
236                                               struct lookahead_ctx *ctx,
237                                               int drain);
238 
239 // Start a frame and initialize svc parameters
240 int vp9_svc_start_frame(struct VP9_COMP *const cpi);
241 
242 #if CONFIG_VP9_TEMPORAL_DENOISING
243 int vp9_denoise_svc_non_key(struct VP9_COMP *const cpi);
244 #endif
245 
246 void vp9_copy_flags_ref_update_idx(struct VP9_COMP *const cpi);
247 
248 int vp9_one_pass_cbr_svc_start_layer(struct VP9_COMP *const cpi);
249 
250 void vp9_free_svc_cyclic_refresh(struct VP9_COMP *const cpi);
251 
252 void vp9_svc_reset_temporal_layers(struct VP9_COMP *const cpi, int is_key);
253 
254 void vp9_svc_check_reset_layer_rc_flag(struct VP9_COMP *const cpi);
255 
256 void vp9_svc_constrain_inter_layer_pred(struct VP9_COMP *const cpi);
257 
258 void vp9_svc_assert_constraints_pattern(struct VP9_COMP *const cpi);
259 
260 void vp9_svc_check_spatial_layer_sync(struct VP9_COMP *const cpi);
261 
262 void vp9_svc_update_ref_frame_buffer_idx(struct VP9_COMP *const cpi);
263 
264 void vp9_svc_update_ref_frame_key_simulcast(struct VP9_COMP *const cpi);
265 
266 void vp9_svc_update_ref_frame(struct VP9_COMP *const cpi);
267 
268 void vp9_svc_adjust_frame_rate(struct VP9_COMP *const cpi);
269 
270 void vp9_svc_adjust_avg_frame_qindex(struct VP9_COMP *const cpi);
271 #ifdef __cplusplus
272 }  // extern "C"
273 #endif
274 
275 #endif  // VPX_VP9_ENCODER_VP9_SVC_LAYERCONTEXT_H_
276