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