• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2010 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 <stdlib.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include <limits.h>
15 #include <assert.h>
16 
17 #include "math.h"
18 #include "vp8/common/common.h"
19 #include "ratectrl.h"
20 #include "vp8/common/entropymode.h"
21 #include "vpx_mem/vpx_mem.h"
22 #include "vp8/common/systemdependent.h"
23 #include "encodemv.h"
24 #include "vpx_dsp/vpx_dsp_common.h"
25 #include "vpx_ports/system_state.h"
26 
27 #define MIN_BPB_FACTOR 0.01
28 #define MAX_BPB_FACTOR 50
29 
30 extern const MB_PREDICTION_MODE vp8_mode_order[MAX_MODES];
31 
32 #ifdef MODE_STATS
33 extern int y_modes[5];
34 extern int uv_modes[4];
35 extern int b_modes[10];
36 
37 extern int inter_y_modes[10];
38 extern int inter_uv_modes[4];
39 extern int inter_b_modes[10];
40 #endif
41 
42 /* Bits Per MB at different Q (Multiplied by 512) */
43 #define BPER_MB_NORMBITS 9
44 
45 /* Work in progress recalibration of baseline rate tables based on
46  * the assumption that bits per mb is inversely proportional to the
47  * quantizer value.
48  */
49 const int vp8_bits_per_mb[2][QINDEX_RANGE] = {
50   /* Intra case 450000/Qintra */
51   {
52       1125000, 900000, 750000, 642857, 562500, 500000, 450000, 450000, 409090,
53       375000,  346153, 321428, 300000, 281250, 264705, 264705, 250000, 236842,
54       225000,  225000, 214285, 214285, 204545, 204545, 195652, 195652, 187500,
55       180000,  180000, 173076, 166666, 160714, 155172, 150000, 145161, 140625,
56       136363,  132352, 128571, 125000, 121621, 121621, 118421, 115384, 112500,
57       109756,  107142, 104651, 102272, 100000, 97826,  97826,  95744,  93750,
58       91836,   90000,  88235,  86538,  84905,  83333,  81818,  80357,  78947,
59       77586,   76271,  75000,  73770,  72580,  71428,  70312,  69230,  68181,
60       67164,   66176,  65217,  64285,  63380,  62500,  61643,  60810,  60000,
61       59210,   59210,  58441,  57692,  56962,  56250,  55555,  54878,  54216,
62       53571,   52941,  52325,  51724,  51136,  50561,  49450,  48387,  47368,
63       46875,   45918,  45000,  44554,  44117,  43269,  42452,  41666,  40909,
64       40178,   39473,  38793,  38135,  36885,  36290,  35714,  35156,  34615,
65       34090,   33582,  33088,  32608,  32142,  31468,  31034,  30405,  29801,
66       29220,   28662,
67   },
68   /* Inter case 285000/Qinter */
69   {
70       712500, 570000, 475000, 407142, 356250, 316666, 285000, 259090, 237500,
71       219230, 203571, 190000, 178125, 167647, 158333, 150000, 142500, 135714,
72       129545, 123913, 118750, 114000, 109615, 105555, 101785, 98275,  95000,
73       91935,  89062,  86363,  83823,  81428,  79166,  77027,  75000,  73076,
74       71250,  69512,  67857,  66279,  64772,  63333,  61956,  60638,  59375,
75       58163,  57000,  55882,  54807,  53773,  52777,  51818,  50892,  50000,
76       49137,  47500,  45967,  44531,  43181,  41911,  40714,  39583,  38513,
77       37500,  36538,  35625,  34756,  33928,  33139,  32386,  31666,  30978,
78       30319,  29687,  29081,  28500,  27941,  27403,  26886,  26388,  25909,
79       25446,  25000,  24568,  23949,  23360,  22800,  22265,  21755,  21268,
80       20802,  20357,  19930,  19520,  19127,  18750,  18387,  18037,  17701,
81       17378,  17065,  16764,  16473,  16101,  15745,  15405,  15079,  14766,
82       14467,  14179,  13902,  13636,  13380,  13133,  12895,  12666,  12445,
83       12179,  11924,  11632,  11445,  11220,  11003,  10795,  10594,  10401,
84       10215,  10035,
85   }
86 };
87 
88 static const int kf_boost_qadjustment[QINDEX_RANGE] = {
89   128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142,
90   143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157,
91   158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172,
92   173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187,
93   188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 200, 201,
94   201, 202, 203, 203, 203, 204, 204, 205, 205, 206, 206, 207, 207, 208, 208,
95   209, 209, 210, 210, 211, 211, 212, 212, 213, 213, 214, 214, 215, 215, 216,
96   216, 217, 217, 218, 218, 219, 219, 220, 220, 220, 220, 220, 220, 220, 220,
97   220, 220, 220, 220, 220, 220, 220, 220,
98 };
99 
100 /* #define GFQ_ADJUSTMENT (Q+100) */
101 #define GFQ_ADJUSTMENT vp8_gf_boost_qadjustment[Q]
102 const int vp8_gf_boost_qadjustment[QINDEX_RANGE] = {
103   80,  82,  84,  86,  88,  90,  92,  94,  96,  97,  98,  99,  100, 101, 102,
104   103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117,
105   118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132,
106   133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147,
107   148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162,
108   163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177,
109   178, 179, 180, 181, 182, 183, 184, 184, 185, 185, 186, 186, 187, 187, 188,
110   188, 189, 189, 190, 190, 191, 191, 192, 192, 193, 193, 194, 194, 194, 194,
111   195, 195, 196, 196, 197, 197, 198, 198
112 };
113 
114 /*
115 const int vp8_gf_boost_qadjustment[QINDEX_RANGE] =
116 {
117     100,101,102,103,104,105,105,106,
118     106,107,107,108,109,109,110,111,
119     112,113,114,115,116,117,118,119,
120     120,121,122,123,124,125,126,127,
121     128,129,130,131,132,133,134,135,
122     136,137,138,139,140,141,142,143,
123     144,145,146,147,148,149,150,151,
124     152,153,154,155,156,157,158,159,
125     160,161,162,163,164,165,166,167,
126     168,169,170,170,171,171,172,172,
127     173,173,173,174,174,174,175,175,
128     175,176,176,176,177,177,177,177,
129     178,178,179,179,180,180,181,181,
130     182,182,183,183,184,184,185,185,
131     186,186,187,187,188,188,189,189,
132     190,190,191,191,192,192,193,193,
133 };
134 */
135 
136 static const int kf_gf_boost_qlimits[QINDEX_RANGE] = {
137   150, 155, 160, 165, 170, 175, 180, 185, 190, 195, 200, 205, 210, 215, 220,
138   225, 230, 235, 240, 245, 250, 255, 260, 265, 270, 275, 280, 285, 290, 295,
139   300, 305, 310, 320, 330, 340, 350, 360, 370, 380, 390, 400, 410, 420, 430,
140   440, 450, 460, 470, 480, 490, 500, 510, 520, 530, 540, 550, 560, 570, 580,
141   590, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600,
142   600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600,
143   600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600,
144   600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600,
145   600, 600, 600, 600, 600, 600, 600, 600,
146 };
147 
148 static const int gf_adjust_table[101] = {
149   100, 115, 130, 145, 160, 175, 190, 200, 210, 220, 230, 240, 260, 270, 280,
150   290, 300, 310, 320, 330, 340, 350, 360, 370, 380, 390, 400, 400, 400, 400,
151   400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400,
152   400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400,
153   400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400,
154   400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400,
155   400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400,
156 };
157 
158 static const int gf_intra_usage_adjustment[20] = {
159   125, 120, 115, 110, 105, 100, 95, 85, 80, 75,
160   70,  65,  60,  55,  50,  50,  50, 50, 50, 50,
161 };
162 
163 static const int gf_interval_table[101] = {
164   7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,
165   7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  8,  8,  8,
166   8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,
167   9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,
168   9,  9,  9,  10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
169   10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
170 };
171 
172 static const unsigned int prior_key_frame_weight[KEY_FRAME_CONTEXT] = { 1, 2, 3,
173                                                                         4, 5 };
174 
vp8_save_coding_context(VP8_COMP * cpi)175 void vp8_save_coding_context(VP8_COMP *cpi) {
176   CODING_CONTEXT *const cc = &cpi->coding_context;
177 
178   /* Stores a snapshot of key state variables which can subsequently be
179    * restored with a call to vp8_restore_coding_context. These functions are
180    * intended for use in a re-code loop in vp8_compress_frame where the
181    * quantizer value is adjusted between loop iterations.
182    */
183 
184   cc->frames_since_key = cpi->frames_since_key;
185   cc->filter_level = cpi->common.filter_level;
186   cc->frames_till_gf_update_due = cpi->frames_till_gf_update_due;
187   cc->frames_since_golden = cpi->frames_since_golden;
188 
189   vp8_copy(cc->mvc, cpi->common.fc.mvc);
190   vp8_copy(cc->mvcosts, cpi->rd_costs.mvcosts);
191 
192   vp8_copy(cc->ymode_prob, cpi->common.fc.ymode_prob);
193   vp8_copy(cc->uv_mode_prob, cpi->common.fc.uv_mode_prob);
194 
195   vp8_copy(cc->ymode_count, cpi->mb.ymode_count);
196   vp8_copy(cc->uv_mode_count, cpi->mb.uv_mode_count);
197 
198 /* Stats */
199 #ifdef MODE_STATS
200   vp8_copy(cc->y_modes, y_modes);
201   vp8_copy(cc->uv_modes, uv_modes);
202   vp8_copy(cc->b_modes, b_modes);
203   vp8_copy(cc->inter_y_modes, inter_y_modes);
204   vp8_copy(cc->inter_uv_modes, inter_uv_modes);
205   vp8_copy(cc->inter_b_modes, inter_b_modes);
206 #endif
207 
208   cc->this_frame_percent_intra = cpi->this_frame_percent_intra;
209 }
210 
vp8_restore_coding_context(VP8_COMP * cpi)211 void vp8_restore_coding_context(VP8_COMP *cpi) {
212   CODING_CONTEXT *const cc = &cpi->coding_context;
213 
214   /* Restore key state variables to the snapshot state stored in the
215    * previous call to vp8_save_coding_context.
216    */
217 
218   cpi->frames_since_key = cc->frames_since_key;
219   cpi->common.filter_level = cc->filter_level;
220   cpi->frames_till_gf_update_due = cc->frames_till_gf_update_due;
221   cpi->frames_since_golden = cc->frames_since_golden;
222 
223   vp8_copy(cpi->common.fc.mvc, cc->mvc);
224 
225   vp8_copy(cpi->rd_costs.mvcosts, cc->mvcosts);
226 
227   vp8_copy(cpi->common.fc.ymode_prob, cc->ymode_prob);
228   vp8_copy(cpi->common.fc.uv_mode_prob, cc->uv_mode_prob);
229 
230   vp8_copy(cpi->mb.ymode_count, cc->ymode_count);
231   vp8_copy(cpi->mb.uv_mode_count, cc->uv_mode_count);
232 
233 /* Stats */
234 #ifdef MODE_STATS
235   vp8_copy(y_modes, cc->y_modes);
236   vp8_copy(uv_modes, cc->uv_modes);
237   vp8_copy(b_modes, cc->b_modes);
238   vp8_copy(inter_y_modes, cc->inter_y_modes);
239   vp8_copy(inter_uv_modes, cc->inter_uv_modes);
240   vp8_copy(inter_b_modes, cc->inter_b_modes);
241 #endif
242 
243   cpi->this_frame_percent_intra = cc->this_frame_percent_intra;
244 }
245 
vp8_setup_key_frame(VP8_COMP * cpi)246 void vp8_setup_key_frame(VP8_COMP *cpi) {
247   /* Setup for Key frame: */
248 
249   vp8_default_coef_probs(&cpi->common);
250 
251   memcpy(cpi->common.fc.mvc, vp8_default_mv_context,
252          sizeof(vp8_default_mv_context));
253   {
254     int flag[2] = { 1, 1 };
255     vp8_build_component_cost_table(
256         cpi->mb.mvcost, (const MV_CONTEXT *)cpi->common.fc.mvc, flag);
257   }
258 
259   /* Make sure we initialize separate contexts for altref,gold, and normal.
260    * TODO shouldn't need 3 different copies of structure to do this!
261    */
262   memcpy(&cpi->lfc_a, &cpi->common.fc, sizeof(cpi->common.fc));
263   memcpy(&cpi->lfc_g, &cpi->common.fc, sizeof(cpi->common.fc));
264   memcpy(&cpi->lfc_n, &cpi->common.fc, sizeof(cpi->common.fc));
265 
266   cpi->common.filter_level = cpi->common.base_qindex * 3 / 8;
267 
268   /* Provisional interval before next GF */
269   if (cpi->auto_gold) {
270     cpi->frames_till_gf_update_due = cpi->baseline_gf_interval;
271   } else {
272     cpi->frames_till_gf_update_due = DEFAULT_GF_INTERVAL;
273   }
274 
275   cpi->common.refresh_golden_frame = 1;
276   cpi->common.refresh_alt_ref_frame = 1;
277 }
278 
estimate_bits_at_q(int frame_kind,int Q,int MBs,double correction_factor)279 static int estimate_bits_at_q(int frame_kind, int Q, int MBs,
280                               double correction_factor) {
281   int Bpm = (int)(.5 + correction_factor * vp8_bits_per_mb[frame_kind][Q]);
282 
283   /* Attempt to retain reasonable accuracy without overflow. The cutoff is
284    * chosen such that the maximum product of Bpm and MBs fits 31 bits. The
285    * largest Bpm takes 20 bits.
286    */
287   if (MBs > (1 << 11)) {
288     return (Bpm >> BPER_MB_NORMBITS) * MBs;
289   } else {
290     return (Bpm * MBs) >> BPER_MB_NORMBITS;
291   }
292 }
293 
calc_iframe_target_size(VP8_COMP * cpi)294 static void calc_iframe_target_size(VP8_COMP *cpi) {
295   /* boost defaults to half second */
296   int kf_boost;
297   uint64_t target;
298 
299   /* Clear down mmx registers to allow floating point in what follows */
300   vpx_clear_system_state();
301 
302   if (cpi->oxcf.fixed_q >= 0) {
303     int Q = cpi->oxcf.key_q;
304 
305     target = estimate_bits_at_q(INTRA_FRAME, Q, cpi->common.MBs,
306                                 cpi->key_frame_rate_correction_factor);
307   } else if (cpi->pass == 2) {
308     /* New Two pass RC */
309     target = cpi->per_frame_bandwidth;
310   }
311   /* First Frame is a special case */
312   else if (cpi->common.current_video_frame == 0) {
313     /* 1 Pass there is no information on which to base size so use
314      * bandwidth per second * fraction of the initial buffer
315      * level
316      */
317     target = (uint64_t)cpi->oxcf.starting_buffer_level / 2;
318 
319     if (target > cpi->oxcf.target_bandwidth * 3 / 2) {
320       target = cpi->oxcf.target_bandwidth * 3 / 2;
321     }
322   } else {
323     /* if this keyframe was forced, use a more recent Q estimate */
324     int Q = (cpi->common.frame_flags & FRAMEFLAGS_KEY) ? cpi->avg_frame_qindex
325                                                        : cpi->ni_av_qi;
326 
327     int initial_boost = 32; /* |3.0 * per_frame_bandwidth| */
328     /* Boost depends somewhat on frame rate: only used for 1 layer case. */
329     if (cpi->oxcf.number_of_layers == 1) {
330       kf_boost =
331           VPXMAX(initial_boost, (int)round(2 * cpi->output_framerate - 16));
332     } else {
333       /* Initial factor: set target size to: |3.0 * per_frame_bandwidth|. */
334       kf_boost = initial_boost;
335     }
336 
337     /* adjustment up based on q: this factor ranges from ~1.2 to 2.2. */
338     kf_boost = kf_boost * kf_boost_qadjustment[Q] / 100;
339 
340     /* frame separation adjustment ( down) */
341     if (cpi->frames_since_key < cpi->output_framerate / 2) {
342       kf_boost =
343           (int)(kf_boost * cpi->frames_since_key / (cpi->output_framerate / 2));
344     }
345 
346     /* Minimal target size is |2* per_frame_bandwidth|. */
347     if (kf_boost < 16) kf_boost = 16;
348 
349     target = ((16 + kf_boost) * cpi->per_frame_bandwidth) >> 4;
350   }
351 
352   if (cpi->oxcf.rc_max_intra_bitrate_pct) {
353     unsigned int max_rate;
354     // This product may overflow unsigned int
355     uint64_t product = cpi->per_frame_bandwidth;
356     product *= cpi->oxcf.rc_max_intra_bitrate_pct;
357     product /= 100;
358     max_rate = (unsigned int)VPXMIN(INT_MAX, product);
359 
360     if (target > max_rate) target = max_rate;
361   }
362 
363   cpi->this_frame_target = (int)target;
364 
365   /* TODO: if we separate rate targeting from Q targeting, move this.
366    * Reset the active worst quality to the baseline value for key frames.
367    */
368   if (cpi->pass != 2) cpi->active_worst_quality = cpi->worst_quality;
369 
370 #if 0
371     {
372         FILE *f;
373 
374         f = fopen("kf_boost.stt", "a");
375         fprintf(f, " %8u %10d %10d %10d\n",
376                 cpi->common.current_video_frame,  cpi->gfu_boost, cpi->baseline_gf_interval, cpi->source_alt_ref_pending);
377 
378         fclose(f);
379     }
380 #endif
381 }
382 
383 /* Do the best we can to define the parameters for the next GF based on what
384  * information we have available.
385  */
calc_gf_params(VP8_COMP * cpi)386 static void calc_gf_params(VP8_COMP *cpi) {
387   int Q =
388       (cpi->oxcf.fixed_q < 0) ? cpi->last_q[INTER_FRAME] : cpi->oxcf.fixed_q;
389   int Boost = 0;
390 
391   int gf_frame_useage = 0; /* Golden frame useage since last GF */
392   int tot_mbs = cpi->recent_ref_frame_usage[INTRA_FRAME] +
393                 cpi->recent_ref_frame_usage[LAST_FRAME] +
394                 cpi->recent_ref_frame_usage[GOLDEN_FRAME] +
395                 cpi->recent_ref_frame_usage[ALTREF_FRAME];
396 
397   int pct_gf_active = (100 * cpi->gf_active_count) /
398                       (cpi->common.mb_rows * cpi->common.mb_cols);
399 
400   if (tot_mbs) {
401     gf_frame_useage = (cpi->recent_ref_frame_usage[GOLDEN_FRAME] +
402                        cpi->recent_ref_frame_usage[ALTREF_FRAME]) *
403                       100 / tot_mbs;
404   }
405 
406   if (pct_gf_active > gf_frame_useage) gf_frame_useage = pct_gf_active;
407 
408   /* Not two pass */
409   if (cpi->pass != 2) {
410     /* Single Pass lagged mode: TBD */
411     if (0) {
412     }
413 
414     /* Single Pass compression: Has to use current and historical data */
415     else {
416 #if 0
417             /* Experimental code */
418             int index = cpi->one_pass_frame_index;
419             int frames_to_scan = (cpi->max_gf_interval <= MAX_LAG_BUFFERS) ? cpi->max_gf_interval : MAX_LAG_BUFFERS;
420 
421             /* ************** Experimental code - incomplete */
422             /*
423             double decay_val = 1.0;
424             double IIAccumulator = 0.0;
425             double last_iiaccumulator = 0.0;
426             double IIRatio;
427 
428             cpi->one_pass_frame_index = cpi->common.current_video_frame%MAX_LAG_BUFFERS;
429 
430             for ( i = 0; i < (frames_to_scan - 1); i++ )
431             {
432                 if ( index < 0 )
433                     index = MAX_LAG_BUFFERS;
434                 index --;
435 
436                 if ( cpi->one_pass_frame_stats[index].frame_coded_error > 0.0 )
437                 {
438                     IIRatio = cpi->one_pass_frame_stats[index].frame_intra_error / cpi->one_pass_frame_stats[index].frame_coded_error;
439 
440                     if ( IIRatio > 30.0 )
441                         IIRatio = 30.0;
442                 }
443                 else
444                     IIRatio = 30.0;
445 
446                 IIAccumulator += IIRatio * decay_val;
447 
448                 decay_val = decay_val * cpi->one_pass_frame_stats[index].frame_pcnt_inter;
449 
450                 if (    (i > MIN_GF_INTERVAL) &&
451                         ((IIAccumulator - last_iiaccumulator) < 2.0) )
452                 {
453                     break;
454                 }
455                 last_iiaccumulator = IIAccumulator;
456             }
457 
458             Boost = IIAccumulator*100.0/16.0;
459             cpi->baseline_gf_interval = i;
460 
461             */
462 #else
463 
464       /*************************************************************/
465       /* OLD code */
466 
467       /* Adjust boost based upon ambient Q */
468       Boost = GFQ_ADJUSTMENT;
469 
470       /* Adjust based upon most recently measure intra useage */
471       Boost = Boost *
472               gf_intra_usage_adjustment[(cpi->this_frame_percent_intra < 15)
473                                             ? cpi->this_frame_percent_intra
474                                             : 14] /
475               100;
476 
477       /* Adjust gf boost based upon GF usage since last GF */
478       Boost = Boost * gf_adjust_table[gf_frame_useage] / 100;
479 #endif
480     }
481 
482     /* golden frame boost without recode loop often goes awry.  be
483      * safe by keeping numbers down.
484      */
485     if (!cpi->sf.recode_loop) {
486       if (cpi->compressor_speed == 2) Boost = Boost / 2;
487     }
488 
489     /* Apply an upper limit based on Q for 1 pass encodes */
490     if (Boost > kf_gf_boost_qlimits[Q] && (cpi->pass == 0)) {
491       Boost = kf_gf_boost_qlimits[Q];
492 
493       /* Apply lower limits to boost. */
494     } else if (Boost < 110) {
495       Boost = 110;
496     }
497 
498     /* Note the boost used */
499     cpi->last_boost = Boost;
500   }
501 
502   /* Estimate next interval
503    * This is updated once the real frame size/boost is known.
504    */
505   if (cpi->oxcf.fixed_q == -1) {
506     if (cpi->pass == 2) { /* 2 Pass */
507       cpi->frames_till_gf_update_due = cpi->baseline_gf_interval;
508     } else { /* 1 Pass */
509       cpi->frames_till_gf_update_due = cpi->baseline_gf_interval;
510 
511       if (cpi->last_boost > 750) cpi->frames_till_gf_update_due++;
512 
513       if (cpi->last_boost > 1000) cpi->frames_till_gf_update_due++;
514 
515       if (cpi->last_boost > 1250) cpi->frames_till_gf_update_due++;
516 
517       if (cpi->last_boost >= 1500) cpi->frames_till_gf_update_due++;
518 
519       if (gf_interval_table[gf_frame_useage] > cpi->frames_till_gf_update_due) {
520         cpi->frames_till_gf_update_due = gf_interval_table[gf_frame_useage];
521       }
522 
523       if (cpi->frames_till_gf_update_due > cpi->max_gf_interval) {
524         cpi->frames_till_gf_update_due = cpi->max_gf_interval;
525       }
526     }
527   } else {
528     cpi->frames_till_gf_update_due = cpi->baseline_gf_interval;
529   }
530 
531   /* ARF on or off */
532   if (cpi->pass != 2) {
533     /* For now Alt ref is not allowed except in 2 pass modes. */
534     cpi->source_alt_ref_pending = 0;
535 
536     /*if ( cpi->oxcf.fixed_q == -1)
537     {
538         if ( cpi->oxcf.play_alternate && (cpi->last_boost > (100 +
539     (AF_THRESH*cpi->frames_till_gf_update_due)) ) )
540             cpi->source_alt_ref_pending = 1;
541         else
542             cpi->source_alt_ref_pending = 0;
543     }*/
544   }
545 }
546 
calc_pframe_target_size(VP8_COMP * cpi)547 static void calc_pframe_target_size(VP8_COMP *cpi) {
548   int min_frame_target;
549   int old_per_frame_bandwidth = cpi->per_frame_bandwidth;
550 
551   if (cpi->current_layer > 0) {
552     cpi->per_frame_bandwidth =
553         cpi->layer_context[cpi->current_layer].avg_frame_size_for_layer;
554   }
555 
556   min_frame_target = 0;
557 
558   if (cpi->pass == 2) {
559     min_frame_target = cpi->min_frame_bandwidth;
560 
561     if (min_frame_target < (cpi->av_per_frame_bandwidth >> 5)) {
562       min_frame_target = cpi->av_per_frame_bandwidth >> 5;
563     }
564   } else if (min_frame_target < cpi->per_frame_bandwidth / 4) {
565     min_frame_target = cpi->per_frame_bandwidth / 4;
566   }
567 
568   /* Special alt reference frame case */
569   if ((cpi->common.refresh_alt_ref_frame) &&
570       (cpi->oxcf.number_of_layers == 1)) {
571     if (cpi->pass == 2) {
572       /* Per frame bit target for the alt ref frame */
573       cpi->per_frame_bandwidth = cpi->twopass.gf_bits;
574       cpi->this_frame_target = cpi->per_frame_bandwidth;
575     }
576 
577     /* One Pass ??? TBD */
578   }
579 
580   /* Normal frames (gf,and inter) */
581   else {
582     /* 2 pass */
583     if (cpi->pass == 2) {
584       cpi->this_frame_target = cpi->per_frame_bandwidth;
585     }
586     /* 1 pass */
587     else {
588       int Adjustment;
589       /* Make rate adjustment to recover bits spent in key frame
590        * Test to see if the key frame inter data rate correction
591        * should still be in force
592        */
593       if (cpi->kf_overspend_bits > 0) {
594         Adjustment = (cpi->kf_bitrate_adjustment <= cpi->kf_overspend_bits)
595                          ? cpi->kf_bitrate_adjustment
596                          : cpi->kf_overspend_bits;
597 
598         if (Adjustment > (cpi->per_frame_bandwidth - min_frame_target)) {
599           Adjustment = (cpi->per_frame_bandwidth - min_frame_target);
600         }
601 
602         cpi->kf_overspend_bits -= Adjustment;
603 
604         /* Calculate an inter frame bandwidth target for the next
605          * few frames designed to recover any extra bits spent on
606          * the key frame.
607          */
608         cpi->this_frame_target = cpi->per_frame_bandwidth - Adjustment;
609 
610         if (cpi->this_frame_target < min_frame_target) {
611           cpi->this_frame_target = min_frame_target;
612         }
613       } else {
614         cpi->this_frame_target = cpi->per_frame_bandwidth;
615       }
616 
617       /* If appropriate make an adjustment to recover bits spent on a
618        * recent GF
619        */
620       if ((cpi->gf_overspend_bits > 0) &&
621           (cpi->this_frame_target > min_frame_target)) {
622         Adjustment = (cpi->non_gf_bitrate_adjustment <= cpi->gf_overspend_bits)
623                          ? cpi->non_gf_bitrate_adjustment
624                          : cpi->gf_overspend_bits;
625 
626         if (Adjustment > (cpi->this_frame_target - min_frame_target)) {
627           Adjustment = (cpi->this_frame_target - min_frame_target);
628         }
629 
630         cpi->gf_overspend_bits -= Adjustment;
631         cpi->this_frame_target -= Adjustment;
632       }
633 
634       /* Apply small + and - boosts for non gf frames */
635       if ((cpi->last_boost > 150) && (cpi->frames_till_gf_update_due > 0) &&
636           (cpi->current_gf_interval >= (MIN_GF_INTERVAL << 1))) {
637         /* % Adjustment limited to the range 1% to 10% */
638         Adjustment = (cpi->last_boost - 100) >> 5;
639 
640         if (Adjustment < 1) {
641           Adjustment = 1;
642         } else if (Adjustment > 10) {
643           Adjustment = 10;
644         }
645 
646         /* Convert to bits */
647         Adjustment = (cpi->this_frame_target * Adjustment) / 100;
648 
649         if (Adjustment > (cpi->this_frame_target - min_frame_target)) {
650           Adjustment = (cpi->this_frame_target - min_frame_target);
651         }
652 
653         if (cpi->frames_since_golden == (cpi->current_gf_interval >> 1)) {
654           Adjustment = (cpi->current_gf_interval - 1) * Adjustment;
655           // Limit adjustment to 10% of current target.
656           if (Adjustment > (10 * cpi->this_frame_target) / 100) {
657             Adjustment = (10 * cpi->this_frame_target) / 100;
658           }
659           cpi->this_frame_target += Adjustment;
660         } else {
661           cpi->this_frame_target -= Adjustment;
662         }
663       }
664     }
665   }
666 
667   /* Sanity check that the total sum of adjustments is not above the
668    * maximum allowed That is that having allowed for KF and GF penalties
669    * we have not pushed the current interframe target to low. If the
670    * adjustment we apply here is not capable of recovering all the extra
671    * bits we have spent in the KF or GF then the remainder will have to
672    * be recovered over a longer time span via other buffer / rate control
673    * mechanisms.
674    */
675   if (cpi->this_frame_target < min_frame_target) {
676     cpi->this_frame_target = min_frame_target;
677   }
678 
679   if (!cpi->common.refresh_alt_ref_frame) {
680     /* Note the baseline target data rate for this inter frame. */
681     cpi->inter_frame_target = cpi->this_frame_target;
682   }
683 
684   /* One Pass specific code */
685   if (cpi->pass == 0) {
686     /* Adapt target frame size with respect to any buffering constraints: */
687     if (cpi->buffered_mode) {
688       int one_percent_bits = (int)(1 + cpi->oxcf.optimal_buffer_level / 100);
689 
690       if ((cpi->buffer_level < cpi->oxcf.optimal_buffer_level) ||
691           (cpi->bits_off_target < cpi->oxcf.optimal_buffer_level)) {
692         int percent_low = 0;
693 
694         /* Decide whether or not we need to adjust the frame data
695          * rate target.
696          *
697          * If we are are below the optimal buffer fullness level
698          * and adherence to buffering constraints is important to
699          * the end usage then adjust the per frame target.
700          */
701         if ((cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) &&
702             (cpi->buffer_level < cpi->oxcf.optimal_buffer_level)) {
703           percent_low =
704               (int)((cpi->oxcf.optimal_buffer_level - cpi->buffer_level) /
705                     one_percent_bits);
706         }
707         /* Are we overshooting the long term clip data rate... */
708         else if (cpi->bits_off_target < 0) {
709           /* Adjust per frame data target downwards to compensate. */
710           percent_low =
711               (int)(100 * -cpi->bits_off_target / (cpi->total_byte_count * 8));
712         }
713 
714         if (percent_low > cpi->oxcf.under_shoot_pct) {
715           percent_low = cpi->oxcf.under_shoot_pct;
716         } else if (percent_low < 0) {
717           percent_low = 0;
718         }
719 
720         /* lower the target bandwidth for this frame. */
721         cpi->this_frame_target -= (cpi->this_frame_target * percent_low) / 200;
722 
723         /* Are we using allowing control of active_worst_allowed_q
724          * according to buffer level.
725          */
726         if (cpi->auto_worst_q && cpi->ni_frames > 150) {
727           int64_t critical_buffer_level;
728 
729           /* For streaming applications the most important factor is
730            * cpi->buffer_level as this takes into account the
731            * specified short term buffering constraints. However,
732            * hitting the long term clip data rate target is also
733            * important.
734            */
735           if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
736             /* Take the smaller of cpi->buffer_level and
737              * cpi->bits_off_target
738              */
739             critical_buffer_level = (cpi->buffer_level < cpi->bits_off_target)
740                                         ? cpi->buffer_level
741                                         : cpi->bits_off_target;
742           }
743           /* For local file playback short term buffering constraints
744            * are less of an issue
745            */
746           else {
747             /* Consider only how we are doing for the clip as a
748              * whole
749              */
750             critical_buffer_level = cpi->bits_off_target;
751           }
752 
753           /* Set the active worst quality based upon the selected
754            * buffer fullness number.
755            */
756           if (critical_buffer_level < cpi->oxcf.optimal_buffer_level) {
757             if (critical_buffer_level > (cpi->oxcf.optimal_buffer_level >> 2)) {
758               int64_t qadjustment_range = cpi->worst_quality - cpi->ni_av_qi;
759               int64_t above_base = (critical_buffer_level -
760                                     (cpi->oxcf.optimal_buffer_level >> 2));
761 
762               /* Step active worst quality down from
763                * cpi->ni_av_qi when (critical_buffer_level ==
764                * cpi->optimal_buffer_level) to
765                * cpi->worst_quality when
766                * (critical_buffer_level ==
767                *     cpi->optimal_buffer_level >> 2)
768                */
769               cpi->active_worst_quality =
770                   cpi->worst_quality -
771                   (int)((qadjustment_range * above_base) /
772                         (cpi->oxcf.optimal_buffer_level * 3 >> 2));
773             } else {
774               cpi->active_worst_quality = cpi->worst_quality;
775             }
776           } else {
777             cpi->active_worst_quality = cpi->ni_av_qi;
778           }
779         } else {
780           cpi->active_worst_quality = cpi->worst_quality;
781         }
782       } else {
783         int percent_high = 0;
784         int64_t target = cpi->this_frame_target;
785 
786         if ((cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) &&
787             (cpi->buffer_level > cpi->oxcf.optimal_buffer_level)) {
788           percent_high =
789               (int)((cpi->buffer_level - cpi->oxcf.optimal_buffer_level) /
790                     one_percent_bits);
791         } else if (cpi->bits_off_target > cpi->oxcf.optimal_buffer_level) {
792           percent_high =
793               (int)((100 * cpi->bits_off_target) / (cpi->total_byte_count * 8));
794         }
795 
796         if (percent_high > cpi->oxcf.over_shoot_pct) {
797           percent_high = cpi->oxcf.over_shoot_pct;
798         } else if (percent_high < 0) {
799           percent_high = 0;
800         }
801 
802         target += (target * percent_high) / 200;
803         target = VPXMIN(target, INT_MAX);
804         cpi->this_frame_target = (int)target;
805 
806         /* Are we allowing control of active_worst_allowed_q according
807          * to buffer level.
808          */
809         if (cpi->auto_worst_q && cpi->ni_frames > 150) {
810           /* When using the relaxed buffer model stick to the
811            * user specified value
812            */
813           cpi->active_worst_quality = cpi->ni_av_qi;
814         } else {
815           cpi->active_worst_quality = cpi->worst_quality;
816         }
817       }
818 
819       /* Set active_best_quality to prevent quality rising too high */
820       cpi->active_best_quality = cpi->best_quality;
821 
822       /* Worst quality obviously must not be better than best quality */
823       if (cpi->active_worst_quality <= cpi->active_best_quality) {
824         cpi->active_worst_quality = cpi->active_best_quality + 1;
825       }
826 
827       if (cpi->active_worst_quality > 127) cpi->active_worst_quality = 127;
828     }
829     /* Unbuffered mode (eg. video conferencing) */
830     else {
831       /* Set the active worst quality */
832       cpi->active_worst_quality = cpi->worst_quality;
833     }
834 
835     /* Special trap for constrained quality mode
836      * "active_worst_quality" may never drop below cq level
837      * for any frame type.
838      */
839     if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY &&
840         cpi->active_worst_quality < cpi->cq_target_quality) {
841       cpi->active_worst_quality = cpi->cq_target_quality;
842     }
843   }
844 
845   /* Test to see if we have to drop a frame
846    * The auto-drop frame code is only used in buffered mode.
847    * In unbufferd mode (eg vide conferencing) the descision to
848    * code or drop a frame is made outside the codec in response to real
849    * world comms or buffer considerations.
850    */
851   if (cpi->drop_frames_allowed &&
852       (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) &&
853       ((cpi->common.frame_type != KEY_FRAME))) {
854     /* Check for a buffer underun-crisis in which case we have to drop
855      * a frame
856      */
857     if ((cpi->buffer_level < 0)) {
858 #if 0
859             FILE *f = fopen("dec.stt", "a");
860             fprintf(f, "%10d %10d %10d %10d ***** BUFFER EMPTY\n",
861                     (int) cpi->common.current_video_frame,
862                     cpi->decimation_factor, cpi->common.horiz_scale,
863                     (cpi->buffer_level * 100) / cpi->oxcf.optimal_buffer_level);
864             fclose(f);
865 #endif
866       cpi->drop_frame = 1;
867 
868       /* Update the buffer level variable. */
869       cpi->bits_off_target += cpi->av_per_frame_bandwidth;
870       if (cpi->bits_off_target > cpi->oxcf.maximum_buffer_size) {
871         cpi->bits_off_target = (int)cpi->oxcf.maximum_buffer_size;
872       }
873       cpi->buffer_level = cpi->bits_off_target;
874 
875       if (cpi->oxcf.number_of_layers > 1) {
876         unsigned int i;
877 
878         // Propagate bits saved by dropping the frame to higher layers.
879         for (i = cpi->current_layer + 1; i < cpi->oxcf.number_of_layers; ++i) {
880           LAYER_CONTEXT *lc = &cpi->layer_context[i];
881           lc->bits_off_target += (int)(lc->target_bandwidth / lc->framerate);
882           if (lc->bits_off_target > lc->maximum_buffer_size) {
883             lc->bits_off_target = lc->maximum_buffer_size;
884           }
885           lc->buffer_level = lc->bits_off_target;
886         }
887       }
888     }
889   }
890 
891   /* Adjust target frame size for Golden Frames: */
892   if (cpi->oxcf.error_resilient_mode == 0 &&
893       (cpi->frames_till_gf_update_due == 0) && !cpi->drop_frame) {
894     if (!cpi->gf_update_onepass_cbr) {
895       int Q = (cpi->oxcf.fixed_q < 0) ? cpi->last_q[INTER_FRAME]
896                                       : cpi->oxcf.fixed_q;
897 
898       int gf_frame_useage = 0; /* Golden frame useage since last GF */
899       int tot_mbs = cpi->recent_ref_frame_usage[INTRA_FRAME] +
900                     cpi->recent_ref_frame_usage[LAST_FRAME] +
901                     cpi->recent_ref_frame_usage[GOLDEN_FRAME] +
902                     cpi->recent_ref_frame_usage[ALTREF_FRAME];
903 
904       int pct_gf_active = (100 * cpi->gf_active_count) /
905                           (cpi->common.mb_rows * cpi->common.mb_cols);
906 
907       if (tot_mbs) {
908         gf_frame_useage = (cpi->recent_ref_frame_usage[GOLDEN_FRAME] +
909                            cpi->recent_ref_frame_usage[ALTREF_FRAME]) *
910                           100 / tot_mbs;
911       }
912 
913       if (pct_gf_active > gf_frame_useage) gf_frame_useage = pct_gf_active;
914 
915       /* Is a fixed manual GF frequency being used */
916       if (cpi->auto_gold) {
917         /* For one pass throw a GF if recent frame intra useage is
918          * low or the GF useage is high
919          */
920         if ((cpi->pass == 0) &&
921             (cpi->this_frame_percent_intra < 15 || gf_frame_useage >= 5)) {
922           cpi->common.refresh_golden_frame = 1;
923 
924           /* Two pass GF descision */
925         } else if (cpi->pass == 2) {
926           cpi->common.refresh_golden_frame = 1;
927         }
928       }
929 
930 #if 0
931 
932           /* Debug stats */
933           if (0) {
934               FILE *f;
935 
936               f = fopen("gf_useaget.stt", "a");
937               fprintf(f, " %8ld %10ld %10ld %10ld %10ld\n",
938                       cpi->common.current_video_frame,  cpi->gfu_boost,
939                       GFQ_ADJUSTMENT, cpi->gfu_boost, gf_frame_useage);
940               fclose(f);
941           }
942 
943 #endif
944 
945       if (cpi->common.refresh_golden_frame == 1) {
946 #if 0
947 
948             if (0) {
949                 FILE *f;
950 
951                 f = fopen("GFexit.stt", "a");
952                 fprintf(f, "%8ld GF coded\n", cpi->common.current_video_frame);
953                 fclose(f);
954             }
955 
956 #endif
957 
958         if (cpi->auto_adjust_gold_quantizer) {
959           calc_gf_params(cpi);
960         }
961 
962         /* If we are using alternate ref instead of gf then do not apply the
963          * boost It will instead be applied to the altref update Jims
964          * modified boost
965          */
966         if (!cpi->source_alt_ref_active) {
967           if (cpi->oxcf.fixed_q < 0) {
968             if (cpi->pass == 2) {
969               /* The spend on the GF is defined in the two pass
970                * code for two pass encodes
971                */
972               cpi->this_frame_target = cpi->per_frame_bandwidth;
973             } else {
974               int Boost = cpi->last_boost;
975               int frames_in_section = cpi->frames_till_gf_update_due + 1;
976               int allocation_chunks = (frames_in_section * 100) + (Boost - 100);
977               int bits_in_section = cpi->inter_frame_target * frames_in_section;
978 
979               /* Normalize Altboost and allocations chunck down to
980                * prevent overflow
981                */
982               while (Boost > 1000) {
983                 Boost /= 2;
984                 allocation_chunks /= 2;
985               }
986 
987               /* Avoid loss of precision but avoid overflow */
988               if ((bits_in_section >> 7) > allocation_chunks) {
989                 cpi->this_frame_target =
990                     Boost * (bits_in_section / allocation_chunks);
991               } else {
992                 cpi->this_frame_target =
993                     (Boost * bits_in_section) / allocation_chunks;
994               }
995             }
996           } else {
997             cpi->this_frame_target =
998                 (estimate_bits_at_q(1, Q, cpi->common.MBs, 1.0) *
999                  cpi->last_boost) /
1000                 100;
1001           }
1002         } else {
1003           /* If there is an active ARF at this location use the minimum
1004            * bits on this frame even if it is a contructed arf.
1005            * The active maximum quantizer insures that an appropriate
1006            * number of bits will be spent if needed for contstructed ARFs.
1007            */
1008           cpi->this_frame_target = 0;
1009         }
1010 
1011         cpi->current_gf_interval = cpi->frames_till_gf_update_due;
1012       }
1013     } else {
1014       // Special case for 1 pass CBR: fixed gf period.
1015       // TODO(marpan): Adjust this boost/interval logic.
1016       // If gf_cbr_boost_pct is small (below threshold) set the flag
1017       // gf_noboost_onepass_cbr = 1, which forces the gf to use the same
1018       // rate correction factor as last.
1019       cpi->gf_noboost_onepass_cbr = (cpi->oxcf.gf_cbr_boost_pct <= 100);
1020       cpi->baseline_gf_interval = cpi->gf_interval_onepass_cbr;
1021       // Skip this update if the zero_mvcount is low.
1022       if (cpi->zeromv_count > (cpi->common.MBs >> 1)) {
1023         cpi->common.refresh_golden_frame = 1;
1024         cpi->this_frame_target =
1025             (cpi->this_frame_target * (100 + cpi->oxcf.gf_cbr_boost_pct)) / 100;
1026       }
1027       cpi->frames_till_gf_update_due = cpi->baseline_gf_interval;
1028       cpi->current_gf_interval = cpi->frames_till_gf_update_due;
1029     }
1030   }
1031 
1032   cpi->per_frame_bandwidth = old_per_frame_bandwidth;
1033 }
1034 
vp8_update_rate_correction_factors(VP8_COMP * cpi,int damp_var)1035 void vp8_update_rate_correction_factors(VP8_COMP *cpi, int damp_var) {
1036   int Q = cpi->common.base_qindex;
1037   int correction_factor = 100;
1038   double rate_correction_factor;
1039   double adjustment_limit;
1040 
1041   int projected_size_based_on_q = 0;
1042 
1043   /* Clear down mmx registers to allow floating point in what follows */
1044   vpx_clear_system_state();
1045 
1046   if (cpi->common.frame_type == KEY_FRAME) {
1047     rate_correction_factor = cpi->key_frame_rate_correction_factor;
1048   } else {
1049     if (cpi->oxcf.number_of_layers == 1 && !cpi->gf_noboost_onepass_cbr &&
1050         (cpi->common.refresh_alt_ref_frame ||
1051          cpi->common.refresh_golden_frame)) {
1052       rate_correction_factor = cpi->gf_rate_correction_factor;
1053     } else {
1054       rate_correction_factor = cpi->rate_correction_factor;
1055     }
1056   }
1057 
1058   /* Work out how big we would have expected the frame to be at this Q
1059    * given the current correction factor. Stay in double to avoid int
1060    * overflow when values are large
1061    */
1062   projected_size_based_on_q =
1063       (int)(((.5 + rate_correction_factor *
1064                        vp8_bits_per_mb[cpi->common.frame_type][Q]) *
1065              cpi->common.MBs) /
1066             (1 << BPER_MB_NORMBITS));
1067 
1068   /* Make some allowance for cpi->zbin_over_quant */
1069   if (cpi->mb.zbin_over_quant > 0) {
1070     int Z = cpi->mb.zbin_over_quant;
1071     double Factor = 0.99;
1072     double factor_adjustment = 0.01 / 256.0;
1073 
1074     while (Z > 0) {
1075       Z--;
1076       projected_size_based_on_q = (int)(Factor * projected_size_based_on_q);
1077       Factor += factor_adjustment;
1078 
1079       if (Factor >= 0.999) Factor = 0.999;
1080     }
1081   }
1082 
1083   /* Work out a size correction factor. */
1084   if (projected_size_based_on_q > 0) {
1085     correction_factor = (int)((100 * (int64_t)cpi->projected_frame_size) /
1086                               projected_size_based_on_q);
1087   }
1088 
1089   /* More heavily damped adjustment used if we have been oscillating
1090    * either side of target
1091    */
1092   switch (damp_var) {
1093     case 0: adjustment_limit = 0.75; break;
1094     case 1: adjustment_limit = 0.375; break;
1095     case 2:
1096     default: adjustment_limit = 0.25; break;
1097   }
1098 
1099   if (correction_factor > 102) {
1100     /* We are not already at the worst allowable quality */
1101     correction_factor =
1102         (int)(100.5 + ((correction_factor - 100) * adjustment_limit));
1103     rate_correction_factor =
1104         ((rate_correction_factor * correction_factor) / 100);
1105 
1106     /* Keep rate_correction_factor within limits */
1107     if (rate_correction_factor > MAX_BPB_FACTOR) {
1108       rate_correction_factor = MAX_BPB_FACTOR;
1109     }
1110   } else if (correction_factor < 99) {
1111     /* We are not already at the best allowable quality */
1112     correction_factor =
1113         (int)(100.5 - ((100 - correction_factor) * adjustment_limit));
1114     rate_correction_factor =
1115         ((rate_correction_factor * correction_factor) / 100);
1116 
1117     /* Keep rate_correction_factor within limits */
1118     if (rate_correction_factor < MIN_BPB_FACTOR) {
1119       rate_correction_factor = MIN_BPB_FACTOR;
1120     }
1121   }
1122 
1123   if (cpi->common.frame_type == KEY_FRAME) {
1124     cpi->key_frame_rate_correction_factor = rate_correction_factor;
1125   } else {
1126     if (cpi->oxcf.number_of_layers == 1 && !cpi->gf_noboost_onepass_cbr &&
1127         (cpi->common.refresh_alt_ref_frame ||
1128          cpi->common.refresh_golden_frame)) {
1129       cpi->gf_rate_correction_factor = rate_correction_factor;
1130     } else {
1131       cpi->rate_correction_factor = rate_correction_factor;
1132     }
1133   }
1134 }
1135 
limit_q_cbr_inter(int last_q,int current_q)1136 static int limit_q_cbr_inter(int last_q, int current_q) {
1137   int limit_down = 12;
1138   if (last_q - current_q > limit_down)
1139     return (last_q - limit_down);
1140   else
1141     return current_q;
1142 }
1143 
vp8_regulate_q(VP8_COMP * cpi,int target_bits_per_frame)1144 int vp8_regulate_q(VP8_COMP *cpi, int target_bits_per_frame) {
1145   int Q = cpi->active_worst_quality;
1146 
1147   if (cpi->force_maxqp == 1) {
1148     cpi->active_worst_quality = cpi->worst_quality;
1149     return cpi->worst_quality;
1150   }
1151   /* Reset Zbin OQ value */
1152   cpi->mb.zbin_over_quant = 0;
1153 
1154   if (cpi->oxcf.fixed_q >= 0) {
1155     Q = cpi->oxcf.fixed_q;
1156 
1157     if (cpi->common.frame_type == KEY_FRAME) {
1158       Q = cpi->oxcf.key_q;
1159     } else if (cpi->oxcf.number_of_layers == 1 &&
1160                cpi->common.refresh_alt_ref_frame &&
1161                !cpi->gf_noboost_onepass_cbr) {
1162       Q = cpi->oxcf.alt_q;
1163     } else if (cpi->oxcf.number_of_layers == 1 &&
1164                cpi->common.refresh_golden_frame &&
1165                !cpi->gf_noboost_onepass_cbr) {
1166       Q = cpi->oxcf.gold_q;
1167     }
1168   } else {
1169     int i;
1170     int last_error = INT_MAX;
1171     int target_bits_per_mb;
1172     int bits_per_mb_at_this_q;
1173     double correction_factor;
1174 
1175     /* Select the appropriate correction factor based upon type of frame. */
1176     if (cpi->common.frame_type == KEY_FRAME) {
1177       correction_factor = cpi->key_frame_rate_correction_factor;
1178     } else {
1179       if (cpi->oxcf.number_of_layers == 1 && !cpi->gf_noboost_onepass_cbr &&
1180           (cpi->common.refresh_alt_ref_frame ||
1181            cpi->common.refresh_golden_frame)) {
1182         correction_factor = cpi->gf_rate_correction_factor;
1183       } else {
1184         correction_factor = cpi->rate_correction_factor;
1185       }
1186     }
1187 
1188     /* Calculate required scaling factor based on target frame size and
1189      * size of frame produced using previous Q
1190      */
1191     if (target_bits_per_frame >= (INT_MAX >> BPER_MB_NORMBITS)) {
1192       /* Case where we would overflow int */
1193       target_bits_per_mb = (target_bits_per_frame / cpi->common.MBs)
1194                            << BPER_MB_NORMBITS;
1195     } else {
1196       target_bits_per_mb =
1197           (target_bits_per_frame << BPER_MB_NORMBITS) / cpi->common.MBs;
1198     }
1199 
1200     i = cpi->active_best_quality;
1201 
1202     do {
1203       bits_per_mb_at_this_q =
1204           (int)(.5 +
1205                 correction_factor * vp8_bits_per_mb[cpi->common.frame_type][i]);
1206 
1207       if (bits_per_mb_at_this_q <= target_bits_per_mb) {
1208         if ((target_bits_per_mb - bits_per_mb_at_this_q) <= last_error) {
1209           Q = i;
1210         } else {
1211           Q = i - 1;
1212         }
1213 
1214         break;
1215       } else {
1216         last_error = bits_per_mb_at_this_q - target_bits_per_mb;
1217       }
1218     } while (++i <= cpi->active_worst_quality);
1219 
1220     /* If we are at MAXQ then enable Q over-run which seeks to claw
1221      * back additional bits through things like the RD multiplier
1222      * and zero bin size.
1223      */
1224     if (Q >= MAXQ) {
1225       int zbin_oqmax;
1226 
1227       double Factor = 0.99;
1228       double factor_adjustment = 0.01 / 256.0;
1229 
1230       if (cpi->common.frame_type == KEY_FRAME) {
1231         zbin_oqmax = 0;
1232       } else if (cpi->oxcf.number_of_layers == 1 &&
1233                  !cpi->gf_noboost_onepass_cbr &&
1234                  (cpi->common.refresh_alt_ref_frame ||
1235                   (cpi->common.refresh_golden_frame &&
1236                    !cpi->source_alt_ref_active))) {
1237         zbin_oqmax = 16;
1238       } else {
1239         zbin_oqmax = ZBIN_OQ_MAX;
1240       }
1241 
1242       /*{
1243           double Factor =
1244       (double)target_bits_per_mb/(double)bits_per_mb_at_this_q;
1245           double Oq;
1246 
1247           Factor = Factor/1.2683;
1248 
1249           Oq = pow( Factor, (1.0/-0.165) );
1250 
1251           if ( Oq > zbin_oqmax )
1252               Oq = zbin_oqmax;
1253 
1254           cpi->zbin_over_quant = (int)Oq;
1255       }*/
1256 
1257       /* Each incrment in the zbin is assumed to have a fixed effect
1258        * on bitrate. This is not of course true. The effect will be
1259        * highly clip dependent and may well have sudden steps. The
1260        * idea here is to acheive higher effective quantizers than the
1261        * normal maximum by expanding the zero bin and hence
1262        * decreasing the number of low magnitude non zero coefficients.
1263        */
1264       while (cpi->mb.zbin_over_quant < zbin_oqmax) {
1265         cpi->mb.zbin_over_quant++;
1266 
1267         if (cpi->mb.zbin_over_quant > zbin_oqmax) {
1268           cpi->mb.zbin_over_quant = zbin_oqmax;
1269         }
1270 
1271         /* Adjust bits_per_mb_at_this_q estimate */
1272         bits_per_mb_at_this_q = (int)(Factor * bits_per_mb_at_this_q);
1273         Factor += factor_adjustment;
1274 
1275         if (Factor >= 0.999) Factor = 0.999;
1276 
1277         /* Break out if we get down to the target rate */
1278         if (bits_per_mb_at_this_q <= target_bits_per_mb) break;
1279       }
1280     }
1281   }
1282 
1283   // Limit decrease in Q for 1 pass CBR screen content mode.
1284   if (cpi->common.frame_type != KEY_FRAME && cpi->pass == 0 &&
1285       cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER &&
1286       cpi->oxcf.screen_content_mode)
1287     Q = limit_q_cbr_inter(cpi->last_q[1], Q);
1288 
1289   return Q;
1290 }
1291 
estimate_keyframe_frequency(VP8_COMP * cpi)1292 static int estimate_keyframe_frequency(VP8_COMP *cpi) {
1293   int i;
1294 
1295   /* Average key frame frequency */
1296   int av_key_frame_frequency = 0;
1297 
1298   /* First key frame at start of sequence is a special case. We have no
1299    * frequency data.
1300    */
1301   if (cpi->key_frame_count == 1) {
1302     /* Assume a default of 1 kf every 2 seconds, or the max kf interval,
1303      * whichever is smaller.
1304      */
1305     int key_freq = cpi->oxcf.key_freq > 0 ? cpi->oxcf.key_freq : 1;
1306     av_key_frame_frequency = 1 + (int)cpi->output_framerate * 2;
1307 
1308     if (cpi->oxcf.auto_key && av_key_frame_frequency > key_freq) {
1309       av_key_frame_frequency = key_freq;
1310     }
1311 
1312     cpi->prior_key_frame_distance[KEY_FRAME_CONTEXT - 1] =
1313         av_key_frame_frequency;
1314   } else {
1315     unsigned int total_weight = 0;
1316     int last_kf_interval =
1317         (cpi->frames_since_key > 0) ? cpi->frames_since_key : 1;
1318 
1319     /* reset keyframe context and calculate weighted average of last
1320      * KEY_FRAME_CONTEXT keyframes
1321      */
1322     for (i = 0; i < KEY_FRAME_CONTEXT; ++i) {
1323       if (i < KEY_FRAME_CONTEXT - 1) {
1324         cpi->prior_key_frame_distance[i] = cpi->prior_key_frame_distance[i + 1];
1325       } else {
1326         cpi->prior_key_frame_distance[i] = last_kf_interval;
1327       }
1328 
1329       av_key_frame_frequency +=
1330           prior_key_frame_weight[i] * cpi->prior_key_frame_distance[i];
1331       total_weight += prior_key_frame_weight[i];
1332     }
1333 
1334     av_key_frame_frequency /= total_weight;
1335   }
1336   // TODO (marpan): Given the checks above, |av_key_frame_frequency|
1337   // should always be above 0. But for now we keep the sanity check in.
1338   if (av_key_frame_frequency == 0) av_key_frame_frequency = 1;
1339   return av_key_frame_frequency;
1340 }
1341 
vp8_adjust_key_frame_context(VP8_COMP * cpi)1342 void vp8_adjust_key_frame_context(VP8_COMP *cpi) {
1343   /* Clear down mmx registers to allow floating point in what follows */
1344   vpx_clear_system_state();
1345 
1346   /* Do we have any key frame overspend to recover? */
1347   /* Two-pass overspend handled elsewhere. */
1348   if ((cpi->pass != 2) &&
1349       (cpi->projected_frame_size > cpi->per_frame_bandwidth)) {
1350     int overspend;
1351 
1352     /* Update the count of key frame overspend to be recovered in
1353      * subsequent frames. A portion of the KF overspend is treated as gf
1354      * overspend (and hence recovered more quickly) as the kf is also a
1355      * gf. Otherwise the few frames following each kf tend to get more
1356      * bits allocated than those following other gfs.
1357      */
1358     overspend = (cpi->projected_frame_size - cpi->per_frame_bandwidth);
1359 
1360     if (cpi->oxcf.number_of_layers > 1) {
1361       cpi->kf_overspend_bits += overspend;
1362     } else {
1363       cpi->kf_overspend_bits += overspend * 7 / 8;
1364       cpi->gf_overspend_bits += overspend * 1 / 8;
1365     }
1366 
1367     /* Work out how much to try and recover per frame. */
1368     cpi->kf_bitrate_adjustment =
1369         cpi->kf_overspend_bits / estimate_keyframe_frequency(cpi);
1370   }
1371 
1372   cpi->frames_since_key = 0;
1373   cpi->key_frame_count++;
1374 }
1375 
vp8_compute_frame_size_bounds(VP8_COMP * cpi,int * frame_under_shoot_limit,int * frame_over_shoot_limit)1376 void vp8_compute_frame_size_bounds(VP8_COMP *cpi, int *frame_under_shoot_limit,
1377                                    int *frame_over_shoot_limit) {
1378   /* Set-up bounds on acceptable frame size: */
1379   if (cpi->oxcf.fixed_q >= 0) {
1380     /* Fixed Q scenario: frame size never outranges target
1381      * (there is no target!)
1382      */
1383     *frame_under_shoot_limit = 0;
1384     *frame_over_shoot_limit = INT_MAX;
1385   } else {
1386     const int64_t this_frame_target = cpi->this_frame_target;
1387     int64_t over_shoot_limit, under_shoot_limit;
1388 
1389     if (cpi->common.frame_type == KEY_FRAME) {
1390       over_shoot_limit = this_frame_target * 9 / 8;
1391       under_shoot_limit = this_frame_target * 7 / 8;
1392     } else {
1393       if (cpi->oxcf.number_of_layers > 1 || cpi->common.refresh_alt_ref_frame ||
1394           cpi->common.refresh_golden_frame) {
1395         over_shoot_limit = this_frame_target * 9 / 8;
1396         under_shoot_limit = this_frame_target * 7 / 8;
1397       } else {
1398         /* For CBR take buffer fullness into account */
1399         if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
1400           if (cpi->buffer_level >= ((cpi->oxcf.optimal_buffer_level +
1401                                      cpi->oxcf.maximum_buffer_size) >>
1402                                     1)) {
1403             /* Buffer is too full so relax overshoot and tighten
1404              * undershoot
1405              */
1406             over_shoot_limit = this_frame_target * 12 / 8;
1407             under_shoot_limit = this_frame_target * 6 / 8;
1408           } else if (cpi->buffer_level <=
1409                      (cpi->oxcf.optimal_buffer_level >> 1)) {
1410             /* Buffer is too low so relax undershoot and tighten
1411              * overshoot
1412              */
1413             over_shoot_limit = this_frame_target * 10 / 8;
1414             under_shoot_limit = this_frame_target * 4 / 8;
1415           } else {
1416             over_shoot_limit = this_frame_target * 11 / 8;
1417             under_shoot_limit = this_frame_target * 5 / 8;
1418           }
1419         }
1420         /* VBR and CQ mode */
1421         /* Note that tighter restrictions here can help quality
1422          * but hurt encode speed
1423          */
1424         else {
1425           /* Stron overshoot limit for constrained quality */
1426           if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) {
1427             over_shoot_limit = this_frame_target * 11 / 8;
1428             under_shoot_limit = this_frame_target * 2 / 8;
1429           } else {
1430             over_shoot_limit = this_frame_target * 11 / 8;
1431             under_shoot_limit = this_frame_target * 5 / 8;
1432           }
1433         }
1434       }
1435     }
1436 
1437     /* For very small rate targets where the fractional adjustment
1438      * (eg * 7/8) may be tiny make sure there is at least a minimum
1439      * range.
1440      */
1441     over_shoot_limit += 200;
1442     under_shoot_limit -= 200;
1443     if (under_shoot_limit < 0) under_shoot_limit = 0;
1444     if (under_shoot_limit > INT_MAX) under_shoot_limit = INT_MAX;
1445     if (over_shoot_limit > INT_MAX) over_shoot_limit = INT_MAX;
1446     *frame_under_shoot_limit = (int)under_shoot_limit;
1447     *frame_over_shoot_limit = (int)over_shoot_limit;
1448   }
1449 }
1450 
1451 /* return of 0 means drop frame */
vp8_pick_frame_size(VP8_COMP * cpi)1452 int vp8_pick_frame_size(VP8_COMP *cpi) {
1453   VP8_COMMON *cm = &cpi->common;
1454 
1455   if (cm->frame_type == KEY_FRAME) {
1456     calc_iframe_target_size(cpi);
1457   } else {
1458     calc_pframe_target_size(cpi);
1459 
1460     /* Check if we're dropping the frame: */
1461     if (cpi->drop_frame) {
1462       cpi->drop_frame = 0;
1463       return 0;
1464     }
1465   }
1466   return 1;
1467 }
1468 // If this just encoded frame (mcomp/transform/quant, but before loopfilter and
1469 // pack_bitstream) has large overshoot, and was not being encoded close to the
1470 // max QP, then drop this frame and force next frame to be encoded at max QP.
1471 // Allow this for screen_content_mode = 2, or if drop frames is allowed.
1472 // TODO(marpan): Should do this exit condition during the encode_frame
1473 // (i.e., halfway during the encoding of the frame) to save cycles.
vp8_drop_encodedframe_overshoot(VP8_COMP * cpi,int Q)1474 int vp8_drop_encodedframe_overshoot(VP8_COMP *cpi, int Q) {
1475   int force_drop_overshoot = 0;
1476 #if CONFIG_MULTI_RES_ENCODING
1477   // Only check for dropping due to overshoot on the lowest stream.
1478   // If the lowest stream of the multi-res encoding was dropped due to
1479   // overshoot, then force dropping on all upper layer streams
1480   // (mr_encoder_id > 0).
1481   LOWER_RES_FRAME_INFO *low_res_frame_info =
1482       (LOWER_RES_FRAME_INFO *)cpi->oxcf.mr_low_res_mode_info;
1483   if (cpi->oxcf.mr_total_resolutions > 1 && cpi->oxcf.mr_encoder_id > 0) {
1484     force_drop_overshoot = low_res_frame_info->is_frame_dropped_overshoot_maxqp;
1485     if (!force_drop_overshoot) {
1486       cpi->force_maxqp = 0;
1487       cpi->frames_since_last_drop_overshoot++;
1488       return 0;
1489     }
1490   }
1491 #endif
1492   if (cpi->common.frame_type != KEY_FRAME &&
1493       (cpi->oxcf.screen_content_mode == 2 ||
1494        (cpi->drop_frames_allowed &&
1495         (force_drop_overshoot ||
1496          (cpi->rate_correction_factor < (8.0f * MIN_BPB_FACTOR) &&
1497           cpi->frames_since_last_drop_overshoot > (int)cpi->framerate))))) {
1498     // Note: the "projected_frame_size" from encode_frame() only gives estimate
1499     // of mode/motion vector rate (in non-rd mode): so below we only require
1500     // that projected_frame_size is somewhat greater than per-frame-bandwidth,
1501     // but add additional condition with high threshold on prediction residual.
1502 
1503     // QP threshold: only allow dropping if we are not close to qp_max.
1504     int thresh_qp = 3 * cpi->worst_quality >> 2;
1505     // Rate threshold, in bytes.
1506     int thresh_rate = 2 * (cpi->av_per_frame_bandwidth >> 3);
1507     // Threshold for the average (over all macroblocks) of the pixel-sum
1508     // residual error over 16x16 block.
1509     int thresh_pred_err_mb = (200 << 4);
1510     int pred_err_mb = (int)(cpi->mb.prediction_error / cpi->common.MBs);
1511     // Reduce/ignore thresh_rate if pred_err_mb much larger than its threshold,
1512     // give more weight to pred_err metric for overshoot detection.
1513     if (cpi->drop_frames_allowed && pred_err_mb > (thresh_pred_err_mb << 4))
1514       thresh_rate = thresh_rate >> 3;
1515     if ((Q < thresh_qp && cpi->projected_frame_size > thresh_rate &&
1516          pred_err_mb > thresh_pred_err_mb &&
1517          pred_err_mb > 2 * cpi->last_pred_err_mb) ||
1518         force_drop_overshoot) {
1519       unsigned int i;
1520       double new_correction_factor;
1521       int target_bits_per_mb;
1522       const int target_size = cpi->av_per_frame_bandwidth;
1523       // Flag to indicate we will force next frame to be encoded at max QP.
1524       cpi->force_maxqp = 1;
1525       // Reset the buffer levels.
1526       cpi->buffer_level = cpi->oxcf.optimal_buffer_level;
1527       cpi->bits_off_target = cpi->oxcf.optimal_buffer_level;
1528       // Compute a new rate correction factor, corresponding to the current
1529       // target frame size and max_QP, and adjust the rate correction factor
1530       // upwards, if needed.
1531       // This is to prevent a bad state where the re-encoded frame at max_QP
1532       // undershoots significantly, and then we end up dropping every other
1533       // frame because the QP/rate_correction_factor may have been too low
1534       // before the drop and then takes too long to come up.
1535       if (target_size >= (INT_MAX >> BPER_MB_NORMBITS)) {
1536         target_bits_per_mb = (target_size / cpi->common.MBs)
1537                              << BPER_MB_NORMBITS;
1538       } else {
1539         target_bits_per_mb =
1540             (target_size << BPER_MB_NORMBITS) / cpi->common.MBs;
1541       }
1542       // Rate correction factor based on target_size_per_mb and max_QP.
1543       new_correction_factor =
1544           (double)target_bits_per_mb /
1545           (double)vp8_bits_per_mb[INTER_FRAME][cpi->worst_quality];
1546       if (new_correction_factor > cpi->rate_correction_factor) {
1547         cpi->rate_correction_factor =
1548             VPXMIN(2.0 * cpi->rate_correction_factor, new_correction_factor);
1549       }
1550       if (cpi->rate_correction_factor > MAX_BPB_FACTOR) {
1551         cpi->rate_correction_factor = MAX_BPB_FACTOR;
1552       }
1553       // Drop this frame: update frame counters.
1554       cpi->common.current_video_frame++;
1555       cpi->frames_since_key++;
1556       cpi->temporal_pattern_counter++;
1557       cpi->frames_since_last_drop_overshoot = 0;
1558       if (cpi->oxcf.number_of_layers > 1) {
1559         // Set max_qp and rate correction for all temporal layers if overshoot
1560         // is detected.
1561         for (i = 0; i < cpi->oxcf.number_of_layers; ++i) {
1562           LAYER_CONTEXT *lc = &cpi->layer_context[i];
1563           lc->force_maxqp = 1;
1564           lc->frames_since_last_drop_overshoot = 0;
1565           lc->rate_correction_factor = cpi->rate_correction_factor;
1566         }
1567       }
1568 #if CONFIG_MULTI_RES_ENCODING
1569       if (cpi->oxcf.mr_total_resolutions > 1)
1570         low_res_frame_info->is_frame_dropped_overshoot_maxqp = 1;
1571 #endif
1572       return 1;
1573     }
1574     cpi->force_maxqp = 0;
1575     cpi->frames_since_last_drop_overshoot++;
1576 #if CONFIG_MULTI_RES_ENCODING
1577     if (cpi->oxcf.mr_total_resolutions > 1)
1578       low_res_frame_info->is_frame_dropped_overshoot_maxqp = 0;
1579 #endif
1580     return 0;
1581   }
1582   cpi->force_maxqp = 0;
1583   cpi->frames_since_last_drop_overshoot++;
1584 #if CONFIG_MULTI_RES_ENCODING
1585   if (cpi->oxcf.mr_total_resolutions > 1)
1586     low_res_frame_info->is_frame_dropped_overshoot_maxqp = 0;
1587 #endif
1588   return 0;
1589 }
1590