]> granicus.if.org Git - libvpx/blob - vp9/encoder/vp9_svc_layercontext.c
Merge "[svc] Make size of empty frame to be 16x16 all the time"
[libvpx] / vp9 / encoder / vp9_svc_layercontext.c
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 #include <math.h>
12
13 #include "vp9/encoder/vp9_encoder.h"
14 #include "vp9/encoder/vp9_svc_layercontext.h"
15 #include "vp9/encoder/vp9_extend.h"
16
17 #define SMALL_FRAME_FB_IDX 7
18 #define SMALL_FRAME_WIDTH  16
19 #define SMALL_FRAME_HEIGHT 16
20
21 void vp9_init_layer_context(VP9_COMP *const cpi) {
22   SVC *const svc = &cpi->svc;
23   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
24   int layer;
25   int layer_end;
26   int alt_ref_idx = svc->number_spatial_layers;
27
28   svc->spatial_layer_id = 0;
29   svc->temporal_layer_id = 0;
30
31   if (svc->number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) {
32     layer_end = svc->number_temporal_layers;
33   } else {
34     layer_end = svc->number_spatial_layers;
35
36     if (cpi->oxcf.error_resilient_mode == 0 && cpi->oxcf.pass == 2) {
37       if (vp9_realloc_frame_buffer(&cpi->svc.empty_frame.img,
38                                    SMALL_FRAME_WIDTH, SMALL_FRAME_HEIGHT,
39                                    cpi->common.subsampling_x,
40                                    cpi->common.subsampling_y,
41 #if CONFIG_VP9_HIGHBITDEPTH
42                                  cpi->common.use_highbitdepth,
43 #endif
44                                  VP9_ENC_BORDER_IN_PIXELS,
45                                  cpi->common.byte_alignment,
46                                  NULL, NULL, NULL))
47         vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
48                            "Failed to allocate empty frame for multiple frame "
49                            "contexts");
50
51       memset(cpi->svc.empty_frame.img.buffer_alloc, 0x80,
52              cpi->svc.empty_frame.img.buffer_alloc_sz);
53     }
54   }
55
56   for (layer = 0; layer < layer_end; ++layer) {
57     LAYER_CONTEXT *const lc = &svc->layer_context[layer];
58     RATE_CONTROL *const lrc = &lc->rc;
59     int i;
60     lc->current_video_frame_in_layer = 0;
61     lc->layer_size = 0;
62     lc->frames_from_key_frame = 0;
63     lc->last_frame_type = FRAME_TYPES;
64     lrc->ni_av_qi = oxcf->worst_allowed_q;
65     lrc->total_actual_bits = 0;
66     lrc->total_target_vs_actual = 0;
67     lrc->ni_tot_qi = 0;
68     lrc->tot_q = 0.0;
69     lrc->avg_q = 0.0;
70     lrc->ni_frames = 0;
71     lrc->decimation_count = 0;
72     lrc->decimation_factor = 0;
73
74     for (i = 0; i < RATE_FACTOR_LEVELS; ++i) {
75       lrc->rate_correction_factors[i] = 1.0;
76     }
77
78     if (svc->number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) {
79       lc->target_bandwidth = oxcf->ts_target_bitrate[layer];
80       lrc->last_q[INTER_FRAME] = oxcf->worst_allowed_q;
81       lrc->avg_frame_qindex[INTER_FRAME] = oxcf->worst_allowed_q;
82       lrc->avg_frame_qindex[KEY_FRAME] = oxcf->worst_allowed_q;
83     } else {
84       lc->target_bandwidth = oxcf->ss_target_bitrate[layer];
85       lrc->last_q[KEY_FRAME] = oxcf->best_allowed_q;
86       lrc->last_q[INTER_FRAME] = oxcf->best_allowed_q;
87       lrc->avg_frame_qindex[KEY_FRAME] = (oxcf->worst_allowed_q +
88                                           oxcf->best_allowed_q) / 2;
89       lrc->avg_frame_qindex[INTER_FRAME] = (oxcf->worst_allowed_q +
90                                             oxcf->best_allowed_q) / 2;
91       if (oxcf->ss_enable_auto_arf[layer])
92         lc->alt_ref_idx = alt_ref_idx++;
93       else
94         lc->alt_ref_idx = INVALID_IDX;
95       lc->gold_ref_idx = INVALID_IDX;
96     }
97
98     lrc->buffer_level = oxcf->starting_buffer_level_ms *
99                             lc->target_bandwidth / 1000;
100     lrc->bits_off_target = lrc->buffer_level;
101   }
102
103   // Still have extra buffer for base layer golden frame
104   if (!(svc->number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR)
105       && alt_ref_idx < REF_FRAMES)
106     svc->layer_context[0].gold_ref_idx = alt_ref_idx;
107 }
108
109 // Update the layer context from a change_config() call.
110 void vp9_update_layer_context_change_config(VP9_COMP *const cpi,
111                                             const int target_bandwidth) {
112   SVC *const svc = &cpi->svc;
113   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
114   const RATE_CONTROL *const rc = &cpi->rc;
115   int layer;
116   int layer_end;
117   float bitrate_alloc = 1.0;
118
119   if (svc->number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) {
120     layer_end = svc->number_temporal_layers;
121   } else {
122     layer_end = svc->number_spatial_layers;
123   }
124
125   for (layer = 0; layer < layer_end; ++layer) {
126     LAYER_CONTEXT *const lc = &svc->layer_context[layer];
127     RATE_CONTROL *const lrc = &lc->rc;
128
129     if (svc->number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) {
130       lc->target_bandwidth = oxcf->ts_target_bitrate[layer];
131     } else {
132       lc->target_bandwidth = oxcf->ss_target_bitrate[layer];
133     }
134     bitrate_alloc = (float)lc->target_bandwidth / target_bandwidth;
135     // Update buffer-related quantities.
136     lrc->starting_buffer_level =
137         (int64_t)(rc->starting_buffer_level * bitrate_alloc);
138     lrc->optimal_buffer_level =
139         (int64_t)(rc->optimal_buffer_level * bitrate_alloc);
140     lrc->maximum_buffer_size =
141         (int64_t)(rc->maximum_buffer_size * bitrate_alloc);
142     lrc->bits_off_target = MIN(lrc->bits_off_target, lrc->maximum_buffer_size);
143     lrc->buffer_level = MIN(lrc->buffer_level, lrc->maximum_buffer_size);
144     // Update framerate-related quantities.
145     if (svc->number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) {
146       lc->framerate = cpi->framerate / oxcf->ts_rate_decimator[layer];
147     } else {
148       lc->framerate = cpi->framerate;
149     }
150     lrc->avg_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate);
151     lrc->max_frame_bandwidth = rc->max_frame_bandwidth;
152     // Update qp-related quantities.
153     lrc->worst_quality = rc->worst_quality;
154     lrc->best_quality = rc->best_quality;
155   }
156 }
157
158 static LAYER_CONTEXT *get_layer_context(VP9_COMP *const cpi) {
159   return (cpi->svc.number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) ?
160          &cpi->svc.layer_context[cpi->svc.temporal_layer_id] :
161          &cpi->svc.layer_context[cpi->svc.spatial_layer_id];
162 }
163
164 void vp9_update_temporal_layer_framerate(VP9_COMP *const cpi) {
165   SVC *const svc = &cpi->svc;
166   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
167   LAYER_CONTEXT *const lc = get_layer_context(cpi);
168   RATE_CONTROL *const lrc = &lc->rc;
169   const int layer = svc->temporal_layer_id;
170
171   lc->framerate = cpi->framerate / oxcf->ts_rate_decimator[layer];
172   lrc->avg_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate);
173   lrc->max_frame_bandwidth = cpi->rc.max_frame_bandwidth;
174   // Update the average layer frame size (non-cumulative per-frame-bw).
175   if (layer == 0) {
176     lc->avg_frame_size = lrc->avg_frame_bandwidth;
177   } else {
178     const double prev_layer_framerate =
179         cpi->framerate / oxcf->ts_rate_decimator[layer - 1];
180     const int prev_layer_target_bandwidth = oxcf->ts_target_bitrate[layer - 1];
181     lc->avg_frame_size =
182         (int)((lc->target_bandwidth - prev_layer_target_bandwidth) /
183               (lc->framerate - prev_layer_framerate));
184   }
185 }
186
187 void vp9_update_spatial_layer_framerate(VP9_COMP *const cpi, double framerate) {
188   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
189   LAYER_CONTEXT *const lc = get_layer_context(cpi);
190   RATE_CONTROL *const lrc = &lc->rc;
191
192   lc->framerate = framerate;
193   lrc->avg_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate);
194   lrc->min_frame_bandwidth = (int)(lrc->avg_frame_bandwidth *
195                                    oxcf->two_pass_vbrmin_section / 100);
196   lrc->max_frame_bandwidth = (int)(((int64_t)lrc->avg_frame_bandwidth *
197                                    oxcf->two_pass_vbrmax_section) / 100);
198   vp9_rc_set_gf_interval_range(cpi, lrc);
199 }
200
201 void vp9_restore_layer_context(VP9_COMP *const cpi) {
202   LAYER_CONTEXT *const lc = get_layer_context(cpi);
203   const int old_frame_since_key = cpi->rc.frames_since_key;
204   const int old_frame_to_key = cpi->rc.frames_to_key;
205
206   cpi->rc = lc->rc;
207   cpi->twopass = lc->twopass;
208   cpi->oxcf.target_bandwidth = lc->target_bandwidth;
209   cpi->alt_ref_source = lc->alt_ref_source;
210   // Reset the frames_since_key and frames_to_key counters to their values
211   // before the layer restore. Keep these defined for the stream (not layer).
212   if (cpi->svc.number_temporal_layers > 1) {
213     cpi->rc.frames_since_key = old_frame_since_key;
214     cpi->rc.frames_to_key = old_frame_to_key;
215   }
216 }
217
218 void vp9_save_layer_context(VP9_COMP *const cpi) {
219   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
220   LAYER_CONTEXT *const lc = get_layer_context(cpi);
221
222   lc->rc = cpi->rc;
223   lc->twopass = cpi->twopass;
224   lc->target_bandwidth = (int)oxcf->target_bandwidth;
225   lc->alt_ref_source = cpi->alt_ref_source;
226 }
227
228 void vp9_init_second_pass_spatial_svc(VP9_COMP *cpi) {
229   SVC *const svc = &cpi->svc;
230   int i;
231
232   for (i = 0; i < svc->number_spatial_layers; ++i) {
233     TWO_PASS *const twopass = &svc->layer_context[i].twopass;
234
235     svc->spatial_layer_id = i;
236     vp9_init_second_pass(cpi);
237
238     twopass->total_stats.spatial_layer_id = i;
239     twopass->total_left_stats.spatial_layer_id = i;
240   }
241   svc->spatial_layer_id = 0;
242 }
243
244 void vp9_inc_frame_in_layer(VP9_COMP *const cpi) {
245   LAYER_CONTEXT *const lc =
246       (cpi->svc.number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) ?
247       &cpi->svc.layer_context[cpi->svc.temporal_layer_id] :
248       &cpi->svc.layer_context[cpi->svc.spatial_layer_id];
249   ++lc->current_video_frame_in_layer;
250   ++lc->frames_from_key_frame;
251 }
252
253 int vp9_is_upper_layer_key_frame(const VP9_COMP *const cpi) {
254   return is_two_pass_svc(cpi) &&
255          cpi->svc.spatial_layer_id > 0 &&
256          cpi->svc.layer_context[cpi->svc.spatial_layer_id].is_key_frame;
257 }
258
259 #if CONFIG_SPATIAL_SVC
260 static void get_layer_resolution(const int width_org, const int height_org,
261                                  const int num, const int den,
262                                  int *width_out, int *height_out) {
263   int w, h;
264
265   if (width_out == NULL || height_out == NULL || den == 0)
266     return;
267
268   w = width_org * num / den;
269   h = height_org * num / den;
270
271   // make height and width even to make chrome player happy
272   w += w % 2;
273   h += h % 2;
274
275   *width_out = w;
276   *height_out = h;
277 }
278
279 int vp9_svc_start_frame(VP9_COMP *const cpi) {
280   int width = 0, height = 0;
281   LAYER_CONTEXT *lc;
282   struct lookahead_entry *buf;
283   int count = 1 << (cpi->svc.number_temporal_layers - 1);
284
285   cpi->svc.spatial_layer_id = cpi->svc.spatial_layer_to_encode;
286   lc = &cpi->svc.layer_context[cpi->svc.spatial_layer_id];
287
288   cpi->svc.temporal_layer_id = 0;
289   while ((lc->current_video_frame_in_layer % count) != 0) {
290     ++cpi->svc.temporal_layer_id;
291     count >>= 1;
292   }
293
294   cpi->ref_frame_flags = VP9_ALT_FLAG | VP9_GOLD_FLAG | VP9_LAST_FLAG;
295
296   cpi->lst_fb_idx = cpi->svc.spatial_layer_id;
297
298   if (cpi->svc.spatial_layer_id == 0)
299     cpi->gld_fb_idx = (lc->gold_ref_idx >= 0) ?
300                       lc->gold_ref_idx : cpi->lst_fb_idx;
301   else
302     cpi->gld_fb_idx = cpi->svc.spatial_layer_id - 1;
303
304   if (lc->current_video_frame_in_layer == 0) {
305     if (cpi->svc.spatial_layer_id >= 2) {
306       cpi->alt_fb_idx = cpi->svc.spatial_layer_id - 2;
307     } else {
308       cpi->alt_fb_idx = cpi->lst_fb_idx;
309       cpi->ref_frame_flags &= (~VP9_LAST_FLAG & ~VP9_ALT_FLAG);
310     }
311   } else {
312     if (cpi->oxcf.ss_enable_auto_arf[cpi->svc.spatial_layer_id]) {
313       cpi->alt_fb_idx = lc->alt_ref_idx;
314       if (!lc->has_alt_frame)
315         cpi->ref_frame_flags &= (~VP9_ALT_FLAG);
316     } else {
317       // Find a proper alt_fb_idx for layers that don't have alt ref frame
318       if (cpi->svc.spatial_layer_id == 0) {
319         cpi->alt_fb_idx = cpi->lst_fb_idx;
320       } else {
321         LAYER_CONTEXT *lc_lower =
322             &cpi->svc.layer_context[cpi->svc.spatial_layer_id - 1];
323
324         if (cpi->oxcf.ss_enable_auto_arf[cpi->svc.spatial_layer_id - 1] &&
325             lc_lower->alt_ref_source != NULL)
326           cpi->alt_fb_idx = lc_lower->alt_ref_idx;
327         else if (cpi->svc.spatial_layer_id >= 2)
328           cpi->alt_fb_idx = cpi->svc.spatial_layer_id - 2;
329         else
330           cpi->alt_fb_idx = cpi->lst_fb_idx;
331       }
332     }
333   }
334
335   get_layer_resolution(cpi->oxcf.width, cpi->oxcf.height,
336                        lc->scaling_factor_num, lc->scaling_factor_den,
337                        &width, &height);
338
339   // Workaround for multiple frame contexts. In some frames we can't use prev_mi
340   // since its previous frame could be changed during decoding time. The idea is
341   // we put a empty invisible frame in front of them, then we will not use
342   // prev_mi when encoding these frames.
343
344   buf = vp9_lookahead_peek(cpi->lookahead, 0);
345   if (cpi->oxcf.error_resilient_mode == 0 && cpi->oxcf.pass == 2 &&
346       cpi->svc.encode_empty_frame_state == NEED_TO_ENCODE &&
347       lc->rc.frames_to_key != 0 &&
348       !(buf != NULL && (buf->flags & VPX_EFLAG_FORCE_KF))) {
349     if ((cpi->svc.number_temporal_layers > 1 &&
350          cpi->svc.temporal_layer_id < cpi->svc.number_temporal_layers - 1) ||
351         (cpi->svc.number_spatial_layers > 1 &&
352          cpi->svc.spatial_layer_id == 0)) {
353       struct lookahead_entry *buf = vp9_lookahead_peek(cpi->lookahead, 0);
354
355       if (buf != NULL) {
356         cpi->svc.empty_frame.ts_start = buf->ts_start;
357         cpi->svc.empty_frame.ts_end = buf->ts_end;
358         cpi->svc.encode_empty_frame_state = ENCODING;
359         cpi->common.show_frame = 0;
360         cpi->ref_frame_flags = 0;
361         cpi->common.frame_type = INTER_FRAME;
362         cpi->lst_fb_idx =
363             cpi->gld_fb_idx = cpi->alt_fb_idx = SMALL_FRAME_FB_IDX;
364
365         if (cpi->svc.encode_intra_empty_frame != 0)
366           cpi->common.intra_only = 1;
367
368         width = SMALL_FRAME_WIDTH;
369         height = SMALL_FRAME_HEIGHT;
370       }
371     }
372   }
373
374   cpi->oxcf.worst_allowed_q = vp9_quantizer_to_qindex(lc->max_q);
375   cpi->oxcf.best_allowed_q = vp9_quantizer_to_qindex(lc->min_q);
376
377   vp9_change_config(cpi, &cpi->oxcf);
378
379   if (vp9_set_size_literal(cpi, width, height) != 0)
380     return VPX_CODEC_INVALID_PARAM;
381
382   vp9_set_high_precision_mv(cpi, 1);
383
384   cpi->alt_ref_source = get_layer_context(cpi)->alt_ref_source;
385
386   return 0;
387 }
388
389 struct lookahead_entry *vp9_svc_lookahead_pop(VP9_COMP *const cpi,
390                                               struct lookahead_ctx *ctx,
391                                               int drain) {
392   struct lookahead_entry *buf = NULL;
393
394   if (ctx->sz && (drain || ctx->sz == ctx->max_sz - MAX_PRE_FRAMES)) {
395     buf = vp9_lookahead_peek(ctx, 0);
396     if (buf != NULL) {
397       // Only remove the buffer when pop the highest layer.
398       if (cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1) {
399         vp9_lookahead_pop(ctx, drain);
400       }
401     }
402   }
403
404   return buf;
405 }
406 #endif