Home | History | Annotate | Download | only in encoder
      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 "common.h"
     20 #include "ratectrl.h"
     21 #include "entropymode.h"
     22 #include "vpx_mem/vpx_mem.h"
     23 #include "systemdependent.h"
     24 #include "encodemv.h"
     25 
     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 extern const MV_REFERENCE_FRAME vp8_ref_frame_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 const int vp8_bits_per_mb[2][QINDEX_RANGE] =
     49 {
     50     // (Updated 19 March 08) Baseline estimate of INTRA-frame Bits Per MB at each Q:
     51     {
     52         674781, 606845, 553905, 524293, 500428, 452540, 435379, 414719,
     53         390970, 371082, 359416, 341807, 336957, 317263, 303724, 298402,
     54         285688, 275237, 268455, 262560, 256038, 248734, 241087, 237615,
     55         229247, 225211, 219112, 213920, 211559, 202714, 198482, 193401,
     56         187866, 183453, 179212, 175965, 171852, 167235, 163972, 160560,
     57         156032, 154349, 151390, 148725, 145708, 142311, 139981, 137700,
     58         134084, 131863, 129746, 128498, 126077, 123461, 121290, 117782,
     59         114883, 112332, 108410, 105685, 103434, 101192,  98587,  95959,
     60         94059,  92017,  89970,  87936,  86142,  84801,  82736,  81106,
     61         79668,  78135,  76641,  75103,  73943,  72693,  71401,  70098,
     62         69165,  67901,  67170,  65987,  64923,  63534,  62378,  61302,
     63         59921,  58941,  57844,  56782,  55960,  54973,  54257,  53454,
     64         52230,  50938,  49962,  49190,  48288,  47270,  46738,  46037,
     65         45020,  44027,  43216,  42287,  41594,  40702,  40081,  39414,
     66         38282,  37627,  36987,  36375,  35808,  35236,  34710,  34162,
     67         33659,  33327,  32751,  32384,  31936,  31461,  30982,  30582,
     68     },
     69 
     70     // (Updated 19 March 08) Baseline estimate of INTER-frame Bits Per MB at each Q:
     71     {
     72         497401, 426316, 372064, 352732, 335763, 283921, 273848, 253321,
     73         233181, 217727, 210030, 196685, 194836, 178396, 167753, 164116,
     74         154119, 146929, 142254, 138488, 133591, 127741, 123166, 120226,
     75         114188, 111756, 107882, 104749, 102522,  96451,  94424,  90905,
     76         87286,  84931,  82111,  80534,  77610,  74700,  73037,  70715,
     77         68006,  67235,  65374,  64009,  62134,  60180,  59105,  57691,
     78         55509,  54512,  53318,  52693,  51194,  49840,  48944,  46980,
     79         45668,  44177,  42348,  40994,  39859,  38889,  37717,  36391,
     80         35482,  34622,  33795,  32756,  32002,  31492,  30573,  29737,
     81         29152,  28514,  27941,  27356,  26859,  26329,  25874,  25364,
     82         24957,  24510,  24290,  23689,  23380,  22845,  22481,  22066,
     83         21587,  21219,  20880,  20452,  20260,  19926,  19661,  19334,
     84         18915,  18391,  18046,  17833,  17441,  17105,  16888,  16729,
     85         16383,  16023,  15706,  15442,  15222,  14938,  14673,  14452,
     86         14005,  13807,  13611,  13447,  13223,  13102,  12963,  12801,
     87         12627,  12534,  12356,  12228,  12056,  11907,  11746,  11643,
     88     }
     89 };
     90 
     91 const int vp8_kf_boost_qadjustment[QINDEX_RANGE] =
     92 {
     93     128, 129, 130, 131, 132, 133, 134, 135,
     94     136, 137, 138, 139, 140, 141, 142, 143,
     95     144, 145, 146, 147, 148, 149, 150, 151,
     96     152, 153, 154, 155, 156, 157, 158, 159,
     97     160, 161, 162, 163, 164, 165, 166, 167,
     98     168, 169, 170, 171, 172, 173, 174, 175,
     99     176, 177, 178, 179, 180, 181, 182, 183,
    100     184, 185, 186, 187, 188, 189, 190, 191,
    101     192, 193, 194, 195, 196, 197, 198, 199,
    102     200, 200, 201, 201, 202, 203, 203, 203,
    103     204, 204, 205, 205, 206, 206, 207, 207,
    104     208, 208, 209, 209, 210, 210, 211, 211,
    105     212, 212, 213, 213, 214, 214, 215, 215,
    106     216, 216, 217, 217, 218, 218, 219, 219,
    107     220, 220, 220, 220, 220, 220, 220, 220,
    108     220, 220, 220, 220, 220, 220, 220, 220,
    109 };
    110 
    111 //#define GFQ_ADJUSTMENT (Q+100)
    112 #define GFQ_ADJUSTMENT vp8_gf_boost_qadjustment[Q]
    113 const int vp8_gf_boost_qadjustment[QINDEX_RANGE] =
    114 {
    115     80, 82, 84, 86, 88, 90, 92, 94,
    116     96, 97, 98, 99, 100, 101, 102, 103,
    117     104, 105, 106, 107, 108, 109, 110, 111,
    118     112, 113, 114, 115, 116, 117, 118, 119,
    119     120, 121, 122, 123, 124, 125, 126, 127,
    120     128, 129, 130, 131, 132, 133, 134, 135,
    121     136, 137, 138, 139, 140, 141, 142, 143,
    122     144, 145, 146, 147, 148, 149, 150, 151,
    123     152, 153, 154, 155, 156, 157, 158, 159,
    124     160, 161, 162, 163, 164, 165, 166, 167,
    125     168, 169, 170, 171, 172, 173, 174, 175,
    126     176, 177, 178, 179, 180, 181, 182, 183,
    127     184, 184, 185, 185, 186, 186, 187, 187,
    128     188, 188, 189, 189, 190, 190, 191, 191,
    129     192, 192, 193, 193, 194, 194, 194, 194,
    130     195, 195, 196, 196, 197, 197, 198, 198
    131 };
    132 
    133 /*
    134 const int vp8_gf_boost_qadjustment[QINDEX_RANGE] =
    135 {
    136     100,101,102,103,104,105,105,106,
    137     106,107,107,108,109,109,110,111,
    138     112,113,114,115,116,117,118,119,
    139     120,121,122,123,124,125,126,127,
    140     128,129,130,131,132,133,134,135,
    141     136,137,138,139,140,141,142,143,
    142     144,145,146,147,148,149,150,151,
    143     152,153,154,155,156,157,158,159,
    144     160,161,162,163,164,165,166,167,
    145     168,169,170,170,171,171,172,172,
    146     173,173,173,174,174,174,175,175,
    147     175,176,176,176,177,177,177,177,
    148     178,178,179,179,180,180,181,181,
    149     182,182,183,183,184,184,185,185,
    150     186,186,187,187,188,188,189,189,
    151     190,190,191,191,192,192,193,193,
    152 };
    153 */
    154 
    155 const int vp8_kf_gf_boost_qlimits[QINDEX_RANGE] =
    156 {
    157     150, 155, 160, 165, 170, 175, 180, 185,
    158     190, 195, 200, 205, 210, 215, 220, 225,
    159     230, 235, 240, 245, 250, 255, 260, 265,
    160     270, 275, 280, 285, 290, 295, 300, 305,
    161     310, 320, 330, 340, 350, 360, 370, 380,
    162     390, 400, 410, 420, 430, 440, 450, 460,
    163     470, 480, 490, 500, 510, 520, 530, 540,
    164     550, 560, 570, 580, 590, 600, 600, 600,
    165     600, 600, 600, 600, 600, 600, 600, 600,
    166     600, 600, 600, 600, 600, 600, 600, 600,
    167     600, 600, 600, 600, 600, 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 };
    174 
    175 // % adjustment to target kf size based on seperation from previous frame
    176 const int vp8_kf_boost_seperationt_adjustment[16] =
    177 {
    178     30,   40,   50,   55,   60,   65,   70,   75,
    179     80,   85,   90,   95,  100,  100,  100,  100,
    180 };
    181 
    182 
    183 const int vp8_gf_adjust_table[101] =
    184 {
    185     100,
    186     115, 130, 145, 160, 175, 190, 200, 210, 220, 230,
    187     240, 260, 270, 280, 290, 300, 310, 320, 330, 340,
    188     350, 360, 370, 380, 390, 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     400, 400, 400, 400, 400, 400, 400, 400, 400, 400,
    192     400, 400, 400, 400, 400, 400, 400, 400, 400, 400,
    193     400, 400, 400, 400, 400, 400, 400, 400, 400, 400,
    194     400, 400, 400, 400, 400, 400, 400, 400, 400, 400,
    195     400, 400, 400, 400, 400, 400, 400, 400, 400, 400,
    196 };
    197 
    198 const int vp8_gf_intra_useage_adjustment[20] =
    199 {
    200     125, 120, 115, 110, 105, 100,  95,  85,  80,  75,
    201     70,  65,  60,  55,  50,  50,  50,  50,  50,  50,
    202 };
    203 
    204 const int vp8_gf_interval_table[101] =
    205 {
    206     7,
    207     7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
    208     7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
    209     7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
    210     8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
    211     8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
    212     9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
    213     9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
    214     10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
    215     10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
    216     11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
    217 };
    218 
    219 static const unsigned int prior_key_frame_weight[KEY_FRAME_CONTEXT] = { 1, 2, 3, 4, 5 };
    220 
    221 
    222 void vp8_save_coding_context(VP8_COMP *cpi)
    223 {
    224     CODING_CONTEXT *const cc = & cpi->coding_context;
    225 
    226     // Stores a snapshot of key state variables which can subsequently be
    227     // restored with a call to vp8_restore_coding_context. These functions are
    228     // intended for use in a re-code loop in vp8_compress_frame where the
    229     // quantizer value is adjusted between loop iterations.
    230 
    231     cc->frames_since_key          = cpi->frames_since_key;
    232     cc->filter_level             = cpi->common.filter_level;
    233     cc->frames_till_gf_update_due   = cpi->frames_till_gf_update_due;
    234     cc->frames_since_golden       = cpi->common.frames_since_golden;
    235 
    236     vp8_copy(cc->mvc,      cpi->common.fc.mvc);
    237     vp8_copy(cc->mvcosts,  cpi->mb.mvcosts);
    238 
    239     vp8_copy(cc->kf_ymode_prob,   cpi->common.kf_ymode_prob);
    240     vp8_copy(cc->ymode_prob,   cpi->common.fc.ymode_prob);
    241     vp8_copy(cc->kf_uv_mode_prob,  cpi->common.kf_uv_mode_prob);
    242     vp8_copy(cc->uv_mode_prob,  cpi->common.fc.uv_mode_prob);
    243 
    244     vp8_copy(cc->ymode_count, cpi->ymode_count);
    245     vp8_copy(cc->uv_mode_count, cpi->uv_mode_count);
    246 
    247 
    248     // Stats
    249 #ifdef MODE_STATS
    250     vp8_copy(cc->y_modes,       y_modes);
    251     vp8_copy(cc->uv_modes,      uv_modes);
    252     vp8_copy(cc->b_modes,       b_modes);
    253     vp8_copy(cc->inter_y_modes,  inter_y_modes);
    254     vp8_copy(cc->inter_uv_modes, inter_uv_modes);
    255     vp8_copy(cc->inter_b_modes,  inter_b_modes);
    256 #endif
    257 
    258     cc->this_frame_percent_intra = cpi->this_frame_percent_intra;
    259 }
    260 
    261 
    262 void vp8_restore_coding_context(VP8_COMP *cpi)
    263 {
    264     CODING_CONTEXT *const cc = & cpi->coding_context;
    265 
    266     // Restore key state variables to the snapshot state stored in the
    267     // previous call to vp8_save_coding_context.
    268 
    269     cpi->frames_since_key         =   cc->frames_since_key;
    270     cpi->common.filter_level     =   cc->filter_level;
    271     cpi->frames_till_gf_update_due  =   cc->frames_till_gf_update_due;
    272     cpi->common.frames_since_golden       =   cc->frames_since_golden;
    273 
    274     vp8_copy(cpi->common.fc.mvc, cc->mvc);
    275 
    276     vp8_copy(cpi->mb.mvcosts, cc->mvcosts);
    277 
    278     vp8_copy(cpi->common.kf_ymode_prob,   cc->kf_ymode_prob);
    279     vp8_copy(cpi->common.fc.ymode_prob,   cc->ymode_prob);
    280     vp8_copy(cpi->common.kf_uv_mode_prob,  cc->kf_uv_mode_prob);
    281     vp8_copy(cpi->common.fc.uv_mode_prob,  cc->uv_mode_prob);
    282 
    283     vp8_copy(cpi->ymode_count, cc->ymode_count);
    284     vp8_copy(cpi->uv_mode_count, cc->uv_mode_count);
    285 
    286     // Stats
    287 #ifdef MODE_STATS
    288     vp8_copy(y_modes, cc->y_modes);
    289     vp8_copy(uv_modes, cc->uv_modes);
    290     vp8_copy(b_modes, cc->b_modes);
    291     vp8_copy(inter_y_modes, cc->inter_y_modes);
    292     vp8_copy(inter_uv_modes, cc->inter_uv_modes);
    293     vp8_copy(inter_b_modes, cc->inter_b_modes);
    294 #endif
    295 
    296 
    297     cpi->this_frame_percent_intra = cc->this_frame_percent_intra;
    298 }
    299 
    300 
    301 void vp8_setup_key_frame(VP8_COMP *cpi)
    302 {
    303     // Setup for Key frame:
    304 
    305     vp8_default_coef_probs(& cpi->common);
    306     vp8_kf_default_bmode_probs(cpi->common.kf_bmode_prob);
    307 
    308     vpx_memcpy(cpi->common.fc.mvc, vp8_default_mv_context, sizeof(vp8_default_mv_context));
    309     {
    310         int flag[2] = {1, 1};
    311         vp8_build_component_cost_table(cpi->mb.mvcost, cpi->mb.mvsadcost, (const MV_CONTEXT *) cpi->common.fc.mvc, flag);
    312     }
    313 
    314     vpx_memset(cpi->common.fc.pre_mvc, 0, sizeof(cpi->common.fc.pre_mvc));  //initialize pre_mvc to all zero.
    315 
    316     //cpi->common.filter_level = 0;      // Reset every key frame.
    317     cpi->common.filter_level = cpi->common.base_qindex * 3 / 8 ;
    318 
    319     // Provisional interval before next GF
    320     if (cpi->auto_gold)
    321         //cpi->frames_till_gf_update_due = DEFAULT_GF_INTERVAL;
    322         cpi->frames_till_gf_update_due = cpi->baseline_gf_interval;
    323     else
    324         cpi->frames_till_gf_update_due = cpi->goldfreq;
    325 
    326     cpi->common.refresh_golden_frame = TRUE;
    327 }
    328 
    329 void vp8_calc_auto_iframe_target_size(VP8_COMP *cpi)
    330 {
    331     // boost defaults to half second
    332     int kf_boost;
    333 
    334     // Clear down mmx registers to allow floating point in what follows
    335     vp8_clear_system_state();  //__asm emms;
    336 
    337     if (cpi->oxcf.fixed_q >= 0)
    338     {
    339         vp8_calc_iframe_target_size(cpi);
    340         return;
    341     }
    342 
    343     if (cpi->pass == 2)
    344     {
    345         cpi->this_frame_target = cpi->per_frame_bandwidth;      // New Two pass RC
    346     }
    347     else
    348     {
    349         // Boost depends somewhat on frame rate
    350         kf_boost = (int)(2 * cpi->output_frame_rate - 16);
    351 
    352         // adjustment up based on q
    353         kf_boost = kf_boost * vp8_kf_boost_qadjustment[cpi->ni_av_qi] / 100;
    354 
    355         // frame separation adjustment ( down)
    356         if (cpi->frames_since_key  < cpi->output_frame_rate / 2)
    357             kf_boost = (int)(kf_boost * cpi->frames_since_key / (cpi->output_frame_rate / 2));
    358 
    359         if (kf_boost < 16)
    360             kf_boost = 16;
    361 
    362         // Reset the active worst quality to the baseline value for key frames.
    363         cpi->active_worst_quality = cpi->worst_quality;
    364 
    365         cpi->this_frame_target = ((16 + kf_boost)  * cpi->per_frame_bandwidth) >> 4;
    366     }
    367 
    368 
    369     // Should the next frame be an altref frame
    370     if (cpi->pass != 2)
    371     {
    372         // For now Alt ref is not allowed except in 2 pass modes.
    373         cpi->source_alt_ref_pending = FALSE;
    374 
    375         /*if ( cpi->oxcf.fixed_q == -1)
    376         {
    377             if ( cpi->oxcf.play_alternate && ( (cpi->last_boost/2) > (100+(AF_THRESH*cpi->frames_till_gf_update_due)) ) )
    378                 cpi->source_alt_ref_pending = TRUE;
    379             else
    380                 cpi->source_alt_ref_pending = FALSE;
    381         }*/
    382     }
    383 
    384     if (0)
    385     {
    386         FILE *f;
    387 
    388         f = fopen("kf_boost.stt", "a");
    389         //fprintf(f, " %8d %10d %10d %10d %10d %10d %10d\n",
    390         //  cpi->common.current_video_frame,  cpi->target_bandwidth, cpi->frames_to_key, kf_boost_qadjustment[cpi->ni_av_qi], cpi->kf_boost, (cpi->this_frame_target *100 / cpi->per_frame_bandwidth), cpi->this_frame_target );
    391 
    392         fprintf(f, " %8u %10d %10d %10d\n",
    393                 cpi->common.current_video_frame,  cpi->gfu_boost, cpi->baseline_gf_interval, cpi->source_alt_ref_pending);
    394 
    395         fclose(f);
    396     }
    397 }
    398 
    399 //  Do the best we can to define the parameteres for the next GF based on what information we have available.
    400 static void calc_gf_params(VP8_COMP *cpi)
    401 {
    402     int Q = (cpi->oxcf.fixed_q < 0) ? cpi->last_q[INTER_FRAME] : cpi->oxcf.fixed_q;
    403     int Boost = 0;
    404 
    405     int gf_frame_useage = 0;      // Golden frame useage since last GF
    406     int tot_mbs = cpi->recent_ref_frame_usage[INTRA_FRAME]  +
    407                   cpi->recent_ref_frame_usage[LAST_FRAME]   +
    408                   cpi->recent_ref_frame_usage[GOLDEN_FRAME] +
    409                   cpi->recent_ref_frame_usage[ALTREF_FRAME];
    410 
    411     int pct_gf_active = (100 * cpi->gf_active_count) / (cpi->common.mb_rows * cpi->common.mb_cols);
    412 
    413     // Reset the last boost indicator
    414     //cpi->last_boost = 100;
    415 
    416     if (tot_mbs)
    417         gf_frame_useage = (cpi->recent_ref_frame_usage[GOLDEN_FRAME] + cpi->recent_ref_frame_usage[ALTREF_FRAME]) * 100 / tot_mbs;
    418 
    419     if (pct_gf_active > gf_frame_useage)
    420         gf_frame_useage = pct_gf_active;
    421 
    422     // Not two pass
    423     if (cpi->pass != 2)
    424     {
    425         // Single Pass lagged mode: TBD
    426         if (FALSE)
    427         {
    428         }
    429 
    430         // Single Pass compression: Has to use current and historical data
    431         else
    432         {
    433 #if 0
    434             // Experimental code
    435             int index = cpi->one_pass_frame_index;
    436             int frames_to_scan = (cpi->max_gf_interval <= MAX_LAG_BUFFERS) ? cpi->max_gf_interval : MAX_LAG_BUFFERS;
    437 
    438             /*
    439             // *************** Experimental code - incomplete
    440             double decay_val = 1.0;
    441             double IIAccumulator = 0.0;
    442             double last_iiaccumulator = 0.0;
    443             double IIRatio;
    444 
    445             cpi->one_pass_frame_index = cpi->common.current_video_frame%MAX_LAG_BUFFERS;
    446 
    447             for ( i = 0; i < (frames_to_scan - 1); i++ )
    448             {
    449                 if ( index < 0 )
    450                     index = MAX_LAG_BUFFERS;
    451                 index --;
    452 
    453                 if ( cpi->one_pass_frame_stats[index].frame_coded_error > 0.0 )
    454                 {
    455                     IIRatio = cpi->one_pass_frame_stats[index].frame_intra_error / cpi->one_pass_frame_stats[index].frame_coded_error;
    456 
    457                     if ( IIRatio > 30.0 )
    458                         IIRatio = 30.0;
    459                 }
    460                 else
    461                     IIRatio = 30.0;
    462 
    463                 IIAccumulator += IIRatio * decay_val;
    464 
    465                 decay_val = decay_val * cpi->one_pass_frame_stats[index].frame_pcnt_inter;
    466 
    467                 if (    (i > MIN_GF_INTERVAL) &&
    468                         ((IIAccumulator - last_iiaccumulator) < 2.0) )
    469                 {
    470                     break;
    471                 }
    472                 last_iiaccumulator = IIAccumulator;
    473             }
    474 
    475             Boost = IIAccumulator*100.0/16.0;
    476             cpi->baseline_gf_interval = i;
    477 
    478             */
    479 #else
    480 
    481             /*************************************************************/
    482             // OLD code
    483 
    484             // Adjust boost based upon ambient Q
    485             Boost = GFQ_ADJUSTMENT;
    486 
    487             // Adjust based upon most recently measure intra useage
    488             Boost = Boost * vp8_gf_intra_useage_adjustment[(cpi->this_frame_percent_intra < 15) ? cpi->this_frame_percent_intra : 14] / 100;
    489 
    490             // Adjust gf boost based upon GF usage since last GF
    491             Boost = Boost * vp8_gf_adjust_table[gf_frame_useage] / 100;
    492 #endif
    493         }
    494 
    495         // golden frame boost without recode loop often goes awry.  be safe by keeping numbers down.
    496         if (!cpi->sf.recode_loop)
    497         {
    498             if (cpi->compressor_speed == 2)
    499                 Boost = Boost / 2;
    500         }
    501 
    502         // Apply an upper limit based on Q for 1 pass encodes
    503         if (Boost > vp8_kf_gf_boost_qlimits[Q] && (cpi->pass == 0))
    504             Boost = vp8_kf_gf_boost_qlimits[Q];
    505 
    506         // Apply lower limits to boost.
    507         else if (Boost < 110)
    508             Boost = 110;
    509 
    510         // Note the boost used
    511         cpi->last_boost = Boost;
    512 
    513     }
    514 
    515     // Estimate next interval
    516     // This is updated once the real frame size/boost is known.
    517     if (cpi->oxcf.fixed_q == -1)
    518     {
    519         if (cpi->pass == 2)         // 2 Pass
    520         {
    521             cpi->frames_till_gf_update_due = cpi->baseline_gf_interval;
    522         }
    523         else                            // 1 Pass
    524         {
    525             cpi->frames_till_gf_update_due = cpi->baseline_gf_interval;
    526 
    527             if (cpi->last_boost > 750)
    528                 cpi->frames_till_gf_update_due++;
    529 
    530             if (cpi->last_boost > 1000)
    531                 cpi->frames_till_gf_update_due++;
    532 
    533             if (cpi->last_boost > 1250)
    534                 cpi->frames_till_gf_update_due++;
    535 
    536             if (cpi->last_boost >= 1500)
    537                 cpi->frames_till_gf_update_due ++;
    538 
    539             if (vp8_gf_interval_table[gf_frame_useage] > cpi->frames_till_gf_update_due)
    540                 cpi->frames_till_gf_update_due = vp8_gf_interval_table[gf_frame_useage];
    541 
    542             if (cpi->frames_till_gf_update_due > cpi->max_gf_interval)
    543                 cpi->frames_till_gf_update_due = cpi->max_gf_interval;
    544         }
    545     }
    546     else
    547         cpi->frames_till_gf_update_due = cpi->baseline_gf_interval;
    548 
    549     // ARF on or off
    550     if (cpi->pass != 2)
    551     {
    552         // For now Alt ref is not allowed except in 2 pass modes.
    553         cpi->source_alt_ref_pending = FALSE;
    554 
    555         /*if ( cpi->oxcf.fixed_q == -1)
    556         {
    557             if ( cpi->oxcf.play_alternate && (cpi->last_boost > (100 + (AF_THRESH*cpi->frames_till_gf_update_due)) ) )
    558                 cpi->source_alt_ref_pending = TRUE;
    559             else
    560                 cpi->source_alt_ref_pending = FALSE;
    561         }*/
    562     }
    563 }
    564 /* This is equvialent to estimate_bits_at_q without the rate_correction_factor. */
    565 static int baseline_bits_at_q(int frame_kind, int Q, int MBs)
    566 {
    567     int Bpm = vp8_bits_per_mb[frame_kind][Q];
    568 
    569     /* Attempt to retain reasonable accuracy without overflow. The cutoff is
    570      * chosen such that the maximum product of Bpm and MBs fits 31 bits. The
    571      * largest Bpm takes 20 bits.
    572      */
    573     if (MBs > (1 << 11))
    574         return (Bpm >> BPER_MB_NORMBITS) * MBs;
    575     else
    576         return (Bpm * MBs) >> BPER_MB_NORMBITS;
    577 }
    578 
    579 void vp8_calc_iframe_target_size(VP8_COMP *cpi)
    580 {
    581     int Q;
    582     int Boost = 100;
    583 
    584     Q = (cpi->oxcf.fixed_q >= 0) ? cpi->oxcf.fixed_q : cpi->avg_frame_qindex;
    585 
    586     if (cpi->auto_adjust_key_quantizer == 1)
    587     {
    588         // If (auto_adjust_key_quantizer==1) then a lower Q is selected for key-frames.
    589         // The enhanced Q is calculated so as to boost the key frame size by a factor
    590         // specified in kf_boost_qadjustment. Also, can adjust based on distance
    591         // between key frames.
    592 
    593         // Adjust boost based upon ambient Q
    594         Boost = vp8_kf_boost_qadjustment[Q];
    595 
    596         // Make the Key frame boost less if the seperation from the previous key frame is small
    597         if (cpi->frames_since_key < 16)
    598             Boost = Boost * vp8_kf_boost_seperationt_adjustment[cpi->frames_since_key] / 100;
    599         else
    600             Boost = Boost * vp8_kf_boost_seperationt_adjustment[15] / 100;
    601 
    602         // Apply limits on boost
    603         if (Boost > vp8_kf_gf_boost_qlimits[Q])
    604             Boost = vp8_kf_gf_boost_qlimits[Q];
    605         else if (Boost < 120)
    606             Boost = 120;
    607     }
    608 
    609     // Keep a record of the boost that was used
    610     cpi->last_boost = Boost;
    611 
    612     // Should the next frame be an altref frame
    613     if (cpi->pass != 2)
    614     {
    615         // For now Alt ref is not allowed except in 2 pass modes.
    616         cpi->source_alt_ref_pending = FALSE;
    617 
    618         /*if ( cpi->oxcf.fixed_q == -1)
    619         {
    620             if ( cpi->oxcf.play_alternate && ( (cpi->last_boost/2) > (100+(AF_THRESH*cpi->frames_till_gf_update_due)) ) )
    621                 cpi->source_alt_ref_pending = TRUE;
    622             else
    623                 cpi->source_alt_ref_pending = FALSE;
    624         }*/
    625     }
    626 
    627     if (cpi->oxcf.fixed_q >= 0)
    628     {
    629         cpi->this_frame_target = (baseline_bits_at_q(0, Q, cpi->common.MBs) * Boost) / 100;
    630     }
    631     else
    632     {
    633 
    634         int bits_per_mb_at_this_q ;
    635 
    636         if (cpi->oxcf.error_resilient_mode == 1)
    637         {
    638             cpi->this_frame_target = 2 * cpi->av_per_frame_bandwidth;
    639             return;
    640         }
    641 
    642         // Rate targetted scenario:
    643         // Be careful of 32-bit OVERFLOW if restructuring the caluclation of cpi->this_frame_target
    644         bits_per_mb_at_this_q = (int)(.5 +
    645                                       cpi->key_frame_rate_correction_factor * vp8_bits_per_mb[0][Q]);
    646 
    647         cpi->this_frame_target = (((bits_per_mb_at_this_q * cpi->common.MBs) >> BPER_MB_NORMBITS) * Boost) / 100;
    648 
    649         // Reset the active worst quality to the baseline value for key frames.
    650         if (cpi->pass < 2)
    651             cpi->active_worst_quality = cpi->worst_quality;
    652     }
    653 }
    654 
    655 
    656 
    657 void vp8_calc_pframe_target_size(VP8_COMP *cpi)
    658 {
    659     int min_frame_target;
    660     int Adjustment;
    661 
    662     // Set the min frame bandwidth.
    663     //min_frame_target = estimate_min_frame_size( cpi );
    664     min_frame_target = 0;
    665 
    666     if (cpi->pass == 2)
    667     {
    668         min_frame_target = cpi->min_frame_bandwidth;
    669 
    670         if (min_frame_target < (cpi->av_per_frame_bandwidth >> 5))
    671             min_frame_target = cpi->av_per_frame_bandwidth >> 5;
    672     }
    673     else if (min_frame_target < cpi->per_frame_bandwidth / 4)
    674         min_frame_target = cpi->per_frame_bandwidth / 4;
    675 
    676 
    677     // Special alt reference frame case
    678     if (cpi->common.refresh_alt_ref_frame)
    679     {
    680         if (cpi->pass == 2)
    681         {
    682             cpi->per_frame_bandwidth = cpi->gf_bits;                       // Per frame bit target for the alt ref frame
    683             cpi->this_frame_target = cpi->per_frame_bandwidth;
    684         }
    685 
    686         /* One Pass ??? TBD */
    687         /*else
    688         {
    689             int frames_in_section;
    690             int allocation_chunks;
    691             int Q = (cpi->oxcf.fixed_q < 0) ? cpi->last_q[INTER_FRAME] : cpi->oxcf.fixed_q;
    692             int alt_boost;
    693             int max_arf_rate;
    694 
    695             alt_boost = (cpi->gfu_boost * 3 * GFQ_ADJUSTMENT) / (2 * 100);
    696             alt_boost += (cpi->frames_till_gf_update_due * 50);
    697 
    698             // If alt ref is not currently active then we have a pottential double hit with GF and ARF so reduce the boost a bit.
    699             // A similar thing is done on GFs that preceed a arf update.
    700             if ( !cpi->source_alt_ref_active )
    701                 alt_boost = alt_boost * 3 / 4;
    702 
    703             frames_in_section = cpi->frames_till_gf_update_due+1;                                   // Standard frames + GF
    704             allocation_chunks = (frames_in_section * 100) + alt_boost;
    705 
    706             // Normalize Altboost and allocations chunck down to prevent overflow
    707             while ( alt_boost > 1000 )
    708             {
    709                 alt_boost /= 2;
    710                 allocation_chunks /= 2;
    711             }
    712 
    713             else
    714             {
    715                 int bits_in_section;
    716 
    717                 if ( cpi->kf_overspend_bits > 0 )
    718                 {
    719                     Adjustment = (cpi->kf_bitrate_adjustment <= cpi->kf_overspend_bits) ? cpi->kf_bitrate_adjustment : cpi->kf_overspend_bits;
    720 
    721                     if ( Adjustment > (cpi->per_frame_bandwidth - min_frame_target) )
    722                         Adjustment = (cpi->per_frame_bandwidth - min_frame_target);
    723 
    724                     cpi->kf_overspend_bits -= Adjustment;
    725 
    726                     // Calculate an inter frame bandwidth target for the next few frames designed to recover
    727                     // any extra bits spent on the key frame.
    728                     cpi->inter_frame_target = cpi->per_frame_bandwidth - Adjustment;
    729                     if ( cpi->inter_frame_target < min_frame_target )
    730                         cpi->inter_frame_target = min_frame_target;
    731                 }
    732                 else
    733                     cpi->inter_frame_target = cpi->per_frame_bandwidth;
    734 
    735                 bits_in_section = cpi->inter_frame_target * frames_in_section;
    736 
    737                 // Avoid loss of precision but avoid overflow
    738                 if ( (bits_in_section>>7) > allocation_chunks )
    739                     cpi->this_frame_target = alt_boost * (bits_in_section / allocation_chunks);
    740                 else
    741                     cpi->this_frame_target = (alt_boost * bits_in_section) / allocation_chunks;
    742             }
    743         }
    744         */
    745     }
    746 
    747     // Normal frames (gf,and inter)
    748     else
    749     {
    750         // 2 pass
    751         if (cpi->pass == 2)
    752         {
    753             cpi->this_frame_target = cpi->per_frame_bandwidth;
    754         }
    755         // 1 pass
    756         else
    757         {
    758             // Make rate adjustment to recover bits spent in key frame
    759             // Test to see if the key frame inter data rate correction should still be in force
    760             if (cpi->kf_overspend_bits > 0)
    761             {
    762                 Adjustment = (cpi->kf_bitrate_adjustment <= cpi->kf_overspend_bits) ? cpi->kf_bitrate_adjustment : cpi->kf_overspend_bits;
    763 
    764                 if (Adjustment > (cpi->per_frame_bandwidth - min_frame_target))
    765                     Adjustment = (cpi->per_frame_bandwidth - min_frame_target);
    766 
    767                 cpi->kf_overspend_bits -= Adjustment;
    768 
    769                 // Calculate an inter frame bandwidth target for the next few frames designed to recover
    770                 // any extra bits spent on the key frame.
    771                 cpi->this_frame_target = cpi->per_frame_bandwidth - Adjustment;
    772 
    773                 if (cpi->this_frame_target < min_frame_target)
    774                     cpi->this_frame_target = min_frame_target;
    775             }
    776             else
    777                 cpi->this_frame_target = cpi->per_frame_bandwidth;
    778 
    779             // If appropriate make an adjustment to recover bits spent on a recent GF
    780             if ((cpi->gf_overspend_bits > 0) && (cpi->this_frame_target > min_frame_target))
    781             {
    782                 int Adjustment = (cpi->non_gf_bitrate_adjustment <= cpi->gf_overspend_bits) ? cpi->non_gf_bitrate_adjustment : cpi->gf_overspend_bits;
    783 
    784                 if (Adjustment > (cpi->this_frame_target - min_frame_target))
    785                     Adjustment = (cpi->this_frame_target - min_frame_target);
    786 
    787                 cpi->gf_overspend_bits -= Adjustment;
    788                 cpi->this_frame_target -= Adjustment;
    789             }
    790 
    791             // Apply small + and - boosts for non gf frames
    792             if ((cpi->last_boost > 150) && (cpi->frames_till_gf_update_due > 0) &&
    793                 (cpi->current_gf_interval >= (MIN_GF_INTERVAL << 1)))
    794             {
    795                 // % Adjustment limited to the range 1% to 10%
    796                 Adjustment = (cpi->last_boost - 100) >> 5;
    797 
    798                 if (Adjustment < 1)
    799                     Adjustment = 1;
    800                 else if (Adjustment > 10)
    801                     Adjustment = 10;
    802 
    803                 // Convert to bits
    804                 Adjustment = (cpi->this_frame_target * Adjustment) / 100;
    805 
    806                 if (Adjustment > (cpi->this_frame_target - min_frame_target))
    807                     Adjustment = (cpi->this_frame_target - min_frame_target);
    808 
    809                 if (cpi->common.frames_since_golden == (cpi->current_gf_interval >> 1))
    810                     cpi->this_frame_target += ((cpi->current_gf_interval - 1) * Adjustment);
    811                 else
    812                     cpi->this_frame_target -= Adjustment;
    813             }
    814         }
    815     }
    816 
    817     // Set a reduced data rate target for our initial Q calculation.
    818     // This should help to save bits during earier sections.
    819     if ((cpi->oxcf.under_shoot_pct > 0) && (cpi->oxcf.under_shoot_pct <= 100))
    820         cpi->this_frame_target = (cpi->this_frame_target * cpi->oxcf.under_shoot_pct) / 100;
    821 
    822     // Sanity check that the total sum of adjustments is not above the maximum allowed
    823     // That is that having allowed for KF and GF penalties we have not pushed the
    824     // current interframe target to low. If the adjustment we apply here is not capable of recovering
    825     // all the extra bits we have spent in the KF or GF then the remainder will have to be recovered over
    826     // a longer time span via other buffer / rate control mechanisms.
    827     if (cpi->this_frame_target < min_frame_target)
    828         cpi->this_frame_target = min_frame_target;
    829 
    830     if (!cpi->common.refresh_alt_ref_frame)
    831         // Note the baseline target data rate for this inter frame.
    832         cpi->inter_frame_target = cpi->this_frame_target;
    833 
    834     // One Pass specific code
    835     if (cpi->pass == 0)
    836     {
    837         // Adapt target frame size with respect to any buffering constraints:
    838         if (cpi->buffered_mode)
    839         {
    840             int one_percent_bits = 1 + cpi->oxcf.optimal_buffer_level / 100;
    841 
    842             if ((cpi->buffer_level < cpi->oxcf.optimal_buffer_level) || (cpi->bits_off_target < cpi->oxcf.optimal_buffer_level))
    843             {
    844                 int percent_low = 0;
    845 
    846                 // Decide whether or not we need to adjust the frame data rate target.
    847                 //
    848                 // If we are are below the optimal buffer fullness level and adherence
    849                 // to buffering contraints is important to the end useage then adjust
    850                 // the per frame target.
    851                 if ((cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) && (cpi->buffer_level < cpi->oxcf.optimal_buffer_level))
    852                 {
    853                     percent_low = (cpi->oxcf.optimal_buffer_level - cpi->buffer_level) / one_percent_bits;
    854 
    855                     if (percent_low > 100)
    856                         percent_low = 100;
    857                     else if (percent_low < 0)
    858                         percent_low = 0;
    859                 }
    860                 // Are we overshooting the long term clip data rate...
    861                 else if (cpi->bits_off_target < 0)
    862                 {
    863                     // Adjust per frame data target downwards to compensate.
    864                     percent_low = (int)(100 * -cpi->bits_off_target / (cpi->total_byte_count * 8));
    865 
    866                     if (percent_low > 100)
    867                         percent_low = 100;
    868                     else if (percent_low < 0)
    869                         percent_low = 0;
    870                 }
    871 
    872                 // lower the target bandwidth for this frame.
    873                 cpi->this_frame_target = (cpi->this_frame_target * (100 - (percent_low / 2))) / 100;
    874 
    875                 // Are we using allowing control of active_worst_allowed_q according to buffer level.
    876                 if (cpi->auto_worst_q)
    877                 {
    878                     int critical_buffer_level;
    879 
    880                     // For streaming applications the most important factor is cpi->buffer_level as this takes
    881                     // into account the specified short term buffering constraints. However, hitting the long
    882                     // term clip data rate target is also important.
    883                     if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
    884                     {
    885                         // Take the smaller of cpi->buffer_level and cpi->bits_off_target
    886                         critical_buffer_level = (cpi->buffer_level < cpi->bits_off_target) ? cpi->buffer_level : cpi->bits_off_target;
    887                     }
    888                     // For local file playback short term buffering contraints are less of an issue
    889                     else
    890                     {
    891                         // Consider only how we are doing for the clip as a whole
    892                         critical_buffer_level = cpi->bits_off_target;
    893                     }
    894 
    895                     // Set the active worst quality based upon the selected buffer fullness number.
    896                     if (critical_buffer_level < cpi->oxcf.optimal_buffer_level)
    897                     {
    898                         if (critical_buffer_level > (cpi->oxcf.optimal_buffer_level / 4))
    899                         {
    900                             int qadjustment_range = cpi->worst_quality - cpi->ni_av_qi;
    901                             int above_base = (critical_buffer_level - (cpi->oxcf.optimal_buffer_level / 4));
    902 
    903                             // Step active worst quality down from cpi->ni_av_qi when (critical_buffer_level == cpi->optimal_buffer_level)
    904                             // to cpi->oxcf.worst_allowed_q when (critical_buffer_level == cpi->optimal_buffer_level/4)
    905                             cpi->active_worst_quality = cpi->worst_quality - ((qadjustment_range * above_base) / (cpi->oxcf.optimal_buffer_level * 3 / 4));
    906                         }
    907                         else
    908                         {
    909                             cpi->active_worst_quality = cpi->worst_quality;
    910                         }
    911                     }
    912                     else
    913                     {
    914                         cpi->active_worst_quality = cpi->ni_av_qi;
    915                     }
    916                 }
    917                 else
    918                 {
    919                     cpi->active_worst_quality = cpi->worst_quality;
    920                 }
    921             }
    922             else
    923             {
    924                 int percent_high;
    925 
    926                 if (cpi->bits_off_target > cpi->oxcf.optimal_buffer_level)
    927                 {
    928                     percent_high = (int)(100 * (cpi->bits_off_target - cpi->oxcf.optimal_buffer_level) / (cpi->total_byte_count * 8));
    929 
    930                     if (percent_high > 100)
    931                         percent_high = 100;
    932                     else if (percent_high < 0)
    933                         percent_high = 0;
    934 
    935                     cpi->this_frame_target = (cpi->this_frame_target * (100 + (percent_high / 2))) / 100;
    936 
    937                 }
    938 
    939                 // Are we allowing control of active_worst_allowed_q according to bufferl level.
    940                 if (cpi->auto_worst_q)
    941                 {
    942                     // When using the relaxed buffer model stick to the user specified value
    943                     cpi->active_worst_quality = cpi->ni_av_qi;
    944                 }
    945                 else
    946                 {
    947                     cpi->active_worst_quality = cpi->worst_quality;
    948                 }
    949             }
    950 
    951             // Set active_best_quality to prevent quality rising too high
    952             cpi->active_best_quality = cpi->best_quality;
    953 
    954             // Worst quality obviously must not be better than best quality
    955             if (cpi->active_worst_quality <= cpi->active_best_quality)
    956                 cpi->active_worst_quality = cpi->active_best_quality + 1;
    957 
    958         }
    959         // Unbuffered mode (eg. video conferencing)
    960         else
    961         {
    962             // Set the active worst quality
    963             cpi->active_worst_quality = cpi->worst_quality;
    964         }
    965     }
    966 
    967     // Test to see if we have to drop a frame
    968     // The auto-drop frame code is only used in buffered mode.
    969     // In unbufferd mode (eg vide conferencing) the descision to
    970     // code or drop a frame is made outside the codec in response to real
    971     // world comms or buffer considerations.
    972     if (cpi->drop_frames_allowed && cpi->buffered_mode &&
    973         (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) &&
    974         ((cpi->common.frame_type != KEY_FRAME))) //|| !cpi->oxcf.allow_spatial_resampling) )
    975     {
    976         // Check for a buffer underun-crisis in which case we have to drop a frame
    977         if ((cpi->buffer_level < 0))
    978         {
    979 #if 0
    980             FILE *f = fopen("dec.stt", "a");
    981             fprintf(f, "%10d %10d %10d %10d ***** BUFFER EMPTY\n",
    982                     (int) cpi->common.current_video_frame,
    983                     cpi->decimation_factor, cpi->common.horiz_scale,
    984                     (cpi->buffer_level * 100) / cpi->oxcf.optimal_buffer_level);
    985             fclose(f);
    986 #endif
    987             //vpx_log("Decoder: Drop frame due to bandwidth: %d \n",cpi->buffer_level, cpi->av_per_frame_bandwidth);
    988 
    989             cpi->drop_frame = TRUE;
    990         }
    991 
    992 #if 0
    993         // Check for other drop frame crtieria (Note 2 pass cbr uses decimation on whole KF sections)
    994         else if ((cpi->buffer_level < cpi->oxcf.drop_frames_water_mark * cpi->oxcf.optimal_buffer_level / 100) &&
    995                  (cpi->drop_count < cpi->max_drop_count) && (cpi->pass == 0))
    996         {
    997             cpi->drop_frame = TRUE;
    998         }
    999 
   1000 #endif
   1001 
   1002         if (cpi->drop_frame)
   1003         {
   1004             // Update the buffer level variable.
   1005             cpi->bits_off_target += cpi->av_per_frame_bandwidth;
   1006             cpi->buffer_level = cpi->bits_off_target;
   1007         }
   1008         else
   1009             cpi->drop_count = 0;
   1010     }
   1011 
   1012     // Adjust target frame size for Golden Frames:
   1013     if (cpi->oxcf.error_resilient_mode == 0 &&
   1014         (cpi->frames_till_gf_update_due == 0) && !cpi->drop_frame)
   1015     {
   1016         //int Boost = 0;
   1017         int Q = (cpi->oxcf.fixed_q < 0) ? cpi->last_q[INTER_FRAME] : cpi->oxcf.fixed_q;
   1018 
   1019         int gf_frame_useage = 0;      // Golden frame useage since last GF
   1020         int tot_mbs = cpi->recent_ref_frame_usage[INTRA_FRAME]  +
   1021                       cpi->recent_ref_frame_usage[LAST_FRAME]   +
   1022                       cpi->recent_ref_frame_usage[GOLDEN_FRAME] +
   1023                       cpi->recent_ref_frame_usage[ALTREF_FRAME];
   1024 
   1025         int pct_gf_active = (100 * cpi->gf_active_count) / (cpi->common.mb_rows * cpi->common.mb_cols);
   1026 
   1027         // Reset the last boost indicator
   1028         //cpi->last_boost = 100;
   1029 
   1030         if (tot_mbs)
   1031             gf_frame_useage = (cpi->recent_ref_frame_usage[GOLDEN_FRAME] + cpi->recent_ref_frame_usage[ALTREF_FRAME]) * 100 / tot_mbs;
   1032 
   1033         if (pct_gf_active > gf_frame_useage)
   1034             gf_frame_useage = pct_gf_active;
   1035 
   1036         // Is a fixed manual GF frequency being used
   1037         if (!cpi->auto_gold)
   1038             cpi->common.refresh_golden_frame = TRUE;
   1039         else
   1040         {
   1041             // For one pass throw a GF if recent frame intra useage is low or the GF useage is high
   1042             if ((cpi->pass == 0) && (cpi->this_frame_percent_intra < 15 || gf_frame_useage >= 5))
   1043                 cpi->common.refresh_golden_frame = TRUE;
   1044 
   1045             // Two pass GF descision
   1046             else if (cpi->pass == 2)
   1047                 cpi->common.refresh_golden_frame = TRUE;
   1048         }
   1049 
   1050 #if 0
   1051 
   1052         // Debug stats
   1053         if (0)
   1054         {
   1055             FILE *f;
   1056 
   1057             f = fopen("gf_useaget.stt", "a");
   1058             fprintf(f, " %8ld %10ld %10ld %10ld %10ld\n",
   1059                     cpi->common.current_video_frame,  cpi->gfu_boost, GFQ_ADJUSTMENT, cpi->gfu_boost, gf_frame_useage);
   1060             fclose(f);
   1061         }
   1062 
   1063 #endif
   1064 
   1065         if (cpi->common.refresh_golden_frame == TRUE)
   1066         {
   1067 #if 0
   1068 
   1069             if (0)   // p_gw
   1070             {
   1071                 FILE *f;
   1072 
   1073                 f = fopen("GFexit.stt", "a");
   1074                 fprintf(f, "%8ld GF coded\n", cpi->common.current_video_frame);
   1075                 fclose(f);
   1076             }
   1077 
   1078 #endif
   1079             cpi->initial_gf_use = 0;
   1080 
   1081             if (cpi->auto_adjust_gold_quantizer)
   1082             {
   1083                 calc_gf_params(cpi);
   1084             }
   1085 
   1086             // If we are using alternate ref instead of gf then do not apply the boost
   1087             // It will instead be applied to the altref update
   1088             // Jims modified boost
   1089             if (!cpi->source_alt_ref_active)
   1090             {
   1091                 if (cpi->oxcf.fixed_q < 0)
   1092                 {
   1093                     if (cpi->pass == 2)
   1094                     {
   1095                         cpi->this_frame_target = cpi->per_frame_bandwidth;          // The spend on the GF is defined in the two pass code for two pass encodes
   1096                     }
   1097                     else
   1098                     {
   1099                         int Boost = cpi->last_boost;
   1100                         int frames_in_section = cpi->frames_till_gf_update_due + 1;
   1101                         int allocation_chunks = (frames_in_section * 100) + (Boost - 100);
   1102                         int bits_in_section = cpi->inter_frame_target * frames_in_section;
   1103 
   1104                         // Normalize Altboost and allocations chunck down to prevent overflow
   1105                         while (Boost > 1000)
   1106                         {
   1107                             Boost /= 2;
   1108                             allocation_chunks /= 2;
   1109                         }
   1110 
   1111                         // Avoid loss of precision but avoid overflow
   1112                         if ((bits_in_section >> 7) > allocation_chunks)
   1113                             cpi->this_frame_target = Boost * (bits_in_section / allocation_chunks);
   1114                         else
   1115                             cpi->this_frame_target = (Boost * bits_in_section) / allocation_chunks;
   1116                     }
   1117                 }
   1118                 else
   1119                     cpi->this_frame_target = (baseline_bits_at_q(1, Q, cpi->common.MBs) * cpi->last_boost) / 100;
   1120 
   1121             }
   1122             // If there is an active ARF at this location use the minimum
   1123             // bits on this frame even if it is a contructed arf.
   1124             // The active maximum quantizer insures that an appropriate
   1125             // number of bits will be spent if needed for contstructed ARFs.
   1126             else
   1127             {
   1128                 cpi->this_frame_target = 0;
   1129             }
   1130 
   1131             cpi->current_gf_interval = cpi->frames_till_gf_update_due;
   1132 
   1133         }
   1134     }
   1135 }
   1136 
   1137 
   1138 void vp8_update_rate_correction_factors(VP8_COMP *cpi, int damp_var)
   1139 {
   1140     int    Q = cpi->common.base_qindex;
   1141     int    correction_factor = 100;
   1142     double rate_correction_factor;
   1143     double adjustment_limit;
   1144 
   1145     int    projected_size_based_on_q = 0;
   1146 
   1147     // Clear down mmx registers to allow floating point in what follows
   1148     vp8_clear_system_state();  //__asm emms;
   1149 
   1150     if (cpi->common.frame_type == KEY_FRAME)
   1151     {
   1152         rate_correction_factor = cpi->key_frame_rate_correction_factor;
   1153     }
   1154     else
   1155     {
   1156         if (cpi->common.refresh_alt_ref_frame || cpi->common.refresh_golden_frame)
   1157             rate_correction_factor = cpi->gf_rate_correction_factor;
   1158         else
   1159             rate_correction_factor = cpi->rate_correction_factor;
   1160     }
   1161 
   1162     // Work out how big we would have expected the frame to be at this Q given the current correction factor.
   1163     // Stay in double to avoid int overflow when values are large
   1164     //projected_size_based_on_q = ((int)(.5 + rate_correction_factor * vp8_bits_per_mb[cpi->common.frame_type][Q]) * cpi->common.MBs) >> BPER_MB_NORMBITS;
   1165     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));
   1166 
   1167     // Make some allowance for cpi->zbin_over_quant
   1168     if (cpi->zbin_over_quant > 0)
   1169     {
   1170         int Z = cpi->zbin_over_quant;
   1171         double Factor = 0.99;
   1172         double factor_adjustment = 0.01 / 256.0; //(double)ZBIN_OQ_MAX;
   1173 
   1174         while (Z > 0)
   1175         {
   1176             Z --;
   1177             projected_size_based_on_q =
   1178                 (int)(Factor * projected_size_based_on_q);
   1179             Factor += factor_adjustment;
   1180 
   1181             if (Factor  >= 0.999)
   1182                 Factor = 0.999;
   1183         }
   1184     }
   1185 
   1186     // Work out a size correction factor.
   1187     //if ( cpi->this_frame_target > 0 )
   1188     //  correction_factor = (100 * cpi->projected_frame_size) / cpi->this_frame_target;
   1189     if (projected_size_based_on_q > 0)
   1190         correction_factor = (100 * cpi->projected_frame_size) / projected_size_based_on_q;
   1191 
   1192     // More heavily damped adjustment used if we have been oscillating either side of target
   1193     switch (damp_var)
   1194     {
   1195     case 0:
   1196         adjustment_limit = 0.75;
   1197         break;
   1198     case 1:
   1199         adjustment_limit = 0.375;
   1200         break;
   1201     case 2:
   1202     default:
   1203         adjustment_limit = 0.25;
   1204         break;
   1205     }
   1206 
   1207     //if ( (correction_factor > 102) && (Q < cpi->active_worst_quality) )
   1208     if (correction_factor > 102)
   1209     {
   1210         // We are not already at the worst allowable quality
   1211         correction_factor = (int)(100.5 + ((correction_factor - 100) * adjustment_limit));
   1212         rate_correction_factor = ((rate_correction_factor * correction_factor) / 100);
   1213 
   1214         // Keep rate_correction_factor within limits
   1215         if (rate_correction_factor > MAX_BPB_FACTOR)
   1216             rate_correction_factor = MAX_BPB_FACTOR;
   1217     }
   1218     //else if ( (correction_factor < 99) && (Q > cpi->active_best_quality) )
   1219     else if (correction_factor < 99)
   1220     {
   1221         // We are not already at the best allowable quality
   1222         correction_factor = (int)(100.5 - ((100 - correction_factor) * adjustment_limit));
   1223         rate_correction_factor = ((rate_correction_factor * correction_factor) / 100);
   1224 
   1225         // Keep rate_correction_factor within limits
   1226         if (rate_correction_factor < MIN_BPB_FACTOR)
   1227             rate_correction_factor = MIN_BPB_FACTOR;
   1228     }
   1229 
   1230     if (cpi->common.frame_type == KEY_FRAME)
   1231         cpi->key_frame_rate_correction_factor = rate_correction_factor;
   1232     else
   1233     {
   1234         if (cpi->common.refresh_alt_ref_frame || cpi->common.refresh_golden_frame)
   1235             cpi->gf_rate_correction_factor = rate_correction_factor;
   1236         else
   1237             cpi->rate_correction_factor = rate_correction_factor;
   1238     }
   1239 }
   1240 
   1241 static int estimate_bits_at_q(VP8_COMP *cpi, int Q)
   1242 {
   1243     int Bpm = (int)(.5 + cpi->rate_correction_factor * vp8_bits_per_mb[INTER_FRAME][Q]);
   1244 
   1245     /* Attempt to retain reasonable accuracy without overflow. The cutoff is
   1246      * chosen such that the maximum product of Bpm and MBs fits 31 bits. The
   1247      * largest Bpm takes 20 bits.
   1248      */
   1249     if (cpi->common.MBs > (1 << 11))
   1250         return (Bpm >> BPER_MB_NORMBITS) * cpi->common.MBs;
   1251     else
   1252         return (Bpm * cpi->common.MBs) >> BPER_MB_NORMBITS;
   1253 
   1254 }
   1255 
   1256 
   1257 int vp8_regulate_q(VP8_COMP *cpi, int target_bits_per_frame)
   1258 {
   1259     int Q = cpi->active_worst_quality;
   1260 
   1261     // Reset Zbin OQ value
   1262     cpi->zbin_over_quant = 0;
   1263 
   1264     if (cpi->oxcf.fixed_q >= 0)
   1265     {
   1266         Q = cpi->oxcf.fixed_q;
   1267 
   1268         if (cpi->common.frame_type == KEY_FRAME)
   1269         {
   1270             Q = cpi->oxcf.key_q;
   1271         }
   1272         else if (cpi->common.refresh_alt_ref_frame)
   1273         {
   1274             Q = cpi->oxcf.alt_q;
   1275         }
   1276         else if (cpi->common.refresh_golden_frame)
   1277         {
   1278             Q = cpi->oxcf.gold_q;
   1279         }
   1280 
   1281     }
   1282     else
   1283     {
   1284         int i;
   1285         int last_error = INT_MAX;
   1286         int target_bits_per_mb;
   1287         int bits_per_mb_at_this_q;
   1288         double correction_factor;
   1289 
   1290         // Select the appropriate correction factor based upon type of frame.
   1291         if (cpi->common.frame_type == KEY_FRAME)
   1292             correction_factor = cpi->key_frame_rate_correction_factor;
   1293         else
   1294         {
   1295             if (cpi->common.refresh_alt_ref_frame || cpi->common.refresh_golden_frame)
   1296                 correction_factor = cpi->gf_rate_correction_factor;
   1297             else
   1298                 correction_factor = cpi->rate_correction_factor;
   1299         }
   1300 
   1301         // Calculate required scaling factor based on target frame size and size of frame produced using previous Q
   1302         if (target_bits_per_frame >= (INT_MAX >> BPER_MB_NORMBITS))
   1303             target_bits_per_mb = (target_bits_per_frame / cpi->common.MBs) << BPER_MB_NORMBITS;       // Case where we would overflow int
   1304         else
   1305             target_bits_per_mb = (target_bits_per_frame << BPER_MB_NORMBITS) / cpi->common.MBs;
   1306 
   1307         i = cpi->active_best_quality;
   1308 
   1309         do
   1310         {
   1311             bits_per_mb_at_this_q = (int)(.5 + correction_factor * vp8_bits_per_mb[cpi->common.frame_type][i]);
   1312 
   1313             if (bits_per_mb_at_this_q <= target_bits_per_mb)
   1314             {
   1315                 if ((target_bits_per_mb - bits_per_mb_at_this_q) <= last_error)
   1316                     Q = i;
   1317                 else
   1318                     Q = i - 1;
   1319 
   1320                 break;
   1321             }
   1322             else
   1323                 last_error = bits_per_mb_at_this_q - target_bits_per_mb;
   1324         }
   1325         while (++i <= cpi->active_worst_quality);
   1326 
   1327 
   1328         // If we are at MAXQ then enable Q over-run which seeks to claw back additional bits through things like
   1329         // the RD multiplier and zero bin size.
   1330         if (Q >= MAXQ)
   1331         {
   1332             int zbin_oqmax;
   1333 
   1334             double Factor = 0.99;
   1335             double factor_adjustment = 0.01 / 256.0; //(double)ZBIN_OQ_MAX;
   1336 
   1337             if (cpi->common.frame_type == KEY_FRAME)
   1338                 zbin_oqmax = 0; //ZBIN_OQ_MAX/16
   1339             else if (cpi->common.refresh_alt_ref_frame || (cpi->common.refresh_golden_frame && !cpi->source_alt_ref_active))
   1340                 zbin_oqmax = 16;
   1341             else
   1342                 zbin_oqmax = ZBIN_OQ_MAX;
   1343 
   1344             /*{
   1345                 double Factor = (double)target_bits_per_mb/(double)bits_per_mb_at_this_q;
   1346                 double Oq;
   1347 
   1348                 Factor = Factor/1.2683;
   1349 
   1350                 Oq = pow( Factor, (1.0/-0.165) );
   1351 
   1352                 if ( Oq > zbin_oqmax )
   1353                     Oq = zbin_oqmax;
   1354 
   1355                 cpi->zbin_over_quant = (int)Oq;
   1356             }*/
   1357 
   1358             // Each incrment in the zbin is assumed to have a fixed effect on bitrate. This is not of course true.
   1359             // The effect will be highly clip dependent and may well have sudden steps.
   1360             // The idea here is to acheive higher effective quantizers than the normal maximum by expanding the zero
   1361             // bin and hence decreasing the number of low magnitude non zero coefficients.
   1362             while (cpi->zbin_over_quant < zbin_oqmax)
   1363             {
   1364                 cpi->zbin_over_quant ++;
   1365 
   1366                 if (cpi->zbin_over_quant > zbin_oqmax)
   1367                     cpi->zbin_over_quant = zbin_oqmax;
   1368 
   1369                 // Adjust bits_per_mb_at_this_q estimate
   1370                 bits_per_mb_at_this_q = (int)(Factor * bits_per_mb_at_this_q);
   1371                 Factor += factor_adjustment;
   1372 
   1373                 if (Factor  >= 0.999)
   1374                     Factor = 0.999;
   1375 
   1376                 if (bits_per_mb_at_this_q <= target_bits_per_mb)    // Break out if we get down to the target rate
   1377                     break;
   1378             }
   1379 
   1380         }
   1381     }
   1382 
   1383     return Q;
   1384 }
   1385 
   1386 static int estimate_min_frame_size(VP8_COMP *cpi)
   1387 {
   1388     double correction_factor;
   1389     int bits_per_mb_at_max_q;
   1390 
   1391     // This funtion returns a default value for the first few frames untill the correction factor has had time to adapt.
   1392     if (cpi->common.current_video_frame < 10)
   1393     {
   1394         if (cpi->pass == 2)
   1395             return (cpi->min_frame_bandwidth);
   1396         else
   1397             return cpi->per_frame_bandwidth / 3;
   1398     }
   1399 
   1400     /*  // Select the appropriate correction factor based upon type of frame.
   1401         if ( cpi->common.frame_type == KEY_FRAME )
   1402             correction_factor = cpi->key_frame_rate_correction_factor;
   1403         else
   1404         {
   1405             if ( cpi->common.refresh_alt_ref_frame || cpi->common.refresh_golden_frame )
   1406                 correction_factor = cpi->gf_rate_correction_factor;
   1407             else
   1408                 correction_factor = cpi->rate_correction_factor;
   1409         }*/
   1410 
   1411     // We estimate at half the value we get from vp8_bits_per_mb
   1412     correction_factor = cpi->rate_correction_factor / 2.0;
   1413 
   1414     bits_per_mb_at_max_q = (int)(.5 + correction_factor * vp8_bits_per_mb[cpi->common.frame_type][MAXQ]);
   1415 
   1416     return (bits_per_mb_at_max_q * cpi->common.MBs) >> BPER_MB_NORMBITS;
   1417 }
   1418 
   1419 void vp8_adjust_key_frame_context(VP8_COMP *cpi)
   1420 {
   1421     int i;
   1422     int av_key_frames_per_second;
   1423 
   1424     // Average key frame frequency and size
   1425     unsigned int total_weight = 0;
   1426     unsigned int av_key_frame_frequency = 0;
   1427     unsigned int av_key_frame_bits = 0;
   1428 
   1429     unsigned int output_frame_rate = (unsigned int)(100 * cpi->output_frame_rate);
   1430     unsigned int target_bandwidth = (unsigned int)(100 * cpi->target_bandwidth);
   1431 
   1432     // Clear down mmx registers to allow floating point in what follows
   1433     vp8_clear_system_state();  //__asm emms;
   1434 
   1435     // Update the count of total key frame bits
   1436     cpi->tot_key_frame_bits += cpi->projected_frame_size;
   1437 
   1438     // First key frame at start of sequence is a special case. We have no frequency data.
   1439     if (cpi->key_frame_count == 1)
   1440     {
   1441         av_key_frame_frequency = (int)cpi->output_frame_rate * 2;            // Assume a default of 1 kf every 2 seconds
   1442         av_key_frame_bits = cpi->projected_frame_size;
   1443         av_key_frames_per_second  = output_frame_rate / av_key_frame_frequency;  // Note output_frame_rate not cpi->output_frame_rate
   1444     }
   1445     else
   1446     {
   1447         int last_kf_interval =
   1448                 (cpi->frames_since_key > 0) ? cpi->frames_since_key : 1;
   1449 
   1450         // reset keyframe context and calculate weighted average of last KEY_FRAME_CONTEXT keyframes
   1451         for (i = 0; i < KEY_FRAME_CONTEXT; i++)
   1452         {
   1453             if (i < KEY_FRAME_CONTEXT - 1)
   1454             {
   1455                 cpi->prior_key_frame_size[i]     = cpi->prior_key_frame_size[i+1];
   1456                 cpi->prior_key_frame_distance[i] = cpi->prior_key_frame_distance[i+1];
   1457             }
   1458             else
   1459             {
   1460                 cpi->prior_key_frame_size[i]     = cpi->projected_frame_size;
   1461                 cpi->prior_key_frame_distance[i] = last_kf_interval;
   1462             }
   1463 
   1464             av_key_frame_bits      += prior_key_frame_weight[i] * cpi->prior_key_frame_size[i];
   1465             av_key_frame_frequency += prior_key_frame_weight[i] * cpi->prior_key_frame_distance[i];
   1466             total_weight         += prior_key_frame_weight[i];
   1467         }
   1468 
   1469         av_key_frame_bits       /= total_weight;
   1470         av_key_frame_frequency  /= total_weight;
   1471         av_key_frames_per_second  = output_frame_rate / av_key_frame_frequency;
   1472 
   1473     }
   1474 
   1475     // Do we have any key frame overspend to recover?
   1476     if ((cpi->pass != 2) && (cpi->projected_frame_size > cpi->per_frame_bandwidth))
   1477     {
   1478         // Update the count of key frame overspend to be recovered in subsequent frames
   1479         // A portion of the KF overspend is treated as gf overspend (and hence recovered more quickly)
   1480         // as the kf is also a gf. Otherwise the few frames following each kf tend to get more bits
   1481         // allocated than those following other gfs.
   1482         cpi->kf_overspend_bits += (cpi->projected_frame_size - cpi->per_frame_bandwidth) * 7 / 8;
   1483         cpi->gf_overspend_bits += (cpi->projected_frame_size - cpi->per_frame_bandwidth) * 1 / 8;
   1484 
   1485         // Work out how much to try and recover per frame.
   1486         // For one pass we estimate the number of frames to spread it over based upon past history.
   1487         // For two pass we know how many frames there will be till the next kf.
   1488         if (cpi->pass == 2)
   1489         {
   1490             if (cpi->frames_to_key > 16)
   1491                 cpi->kf_bitrate_adjustment = cpi->kf_overspend_bits / (int)cpi->frames_to_key;
   1492             else
   1493                 cpi->kf_bitrate_adjustment = cpi->kf_overspend_bits / 16;
   1494         }
   1495         else
   1496             cpi->kf_bitrate_adjustment = cpi->kf_overspend_bits / (int)av_key_frame_frequency;
   1497     }
   1498 
   1499     cpi->frames_since_key = 0;
   1500     cpi->last_key_frame_size = cpi->projected_frame_size;
   1501     cpi->key_frame_count++;
   1502 }
   1503 
   1504 void vp8_compute_frame_size_bounds(VP8_COMP *cpi, int *frame_under_shoot_limit, int *frame_over_shoot_limit)
   1505 {
   1506     // Set-up bounds on acceptable frame size:
   1507     if (cpi->oxcf.fixed_q >= 0)
   1508     {
   1509         // Fixed Q scenario: frame size never outranges target (there is no target!)
   1510         *frame_under_shoot_limit = 0;
   1511         *frame_over_shoot_limit  = INT_MAX;
   1512     }
   1513     else
   1514     {
   1515         if (cpi->common.frame_type == KEY_FRAME)
   1516         {
   1517             *frame_over_shoot_limit  = cpi->this_frame_target * 9 / 8;
   1518             *frame_under_shoot_limit = cpi->this_frame_target * 7 / 8;
   1519         }
   1520         else
   1521         {
   1522             if (cpi->common.refresh_alt_ref_frame || cpi->common.refresh_golden_frame)
   1523             {
   1524                 *frame_over_shoot_limit  = cpi->this_frame_target * 9 / 8;
   1525                 *frame_under_shoot_limit = cpi->this_frame_target * 7 / 8;
   1526             }
   1527             else
   1528             {
   1529                 // For CBR take buffer fullness into account
   1530                 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
   1531                 {
   1532                     if (cpi->buffer_level >= ((cpi->oxcf.optimal_buffer_level + cpi->oxcf.maximum_buffer_size) >> 1))
   1533                     {
   1534                         // Buffer is too full so relax overshoot and tighten undershoot
   1535                         *frame_over_shoot_limit  = cpi->this_frame_target * 12 / 8;
   1536                         *frame_under_shoot_limit = cpi->this_frame_target * 6 / 8;
   1537                     }
   1538                     else if (cpi->buffer_level <= (cpi->oxcf.optimal_buffer_level >> 1))
   1539                     {
   1540                         // Buffer is too low so relax undershoot and tighten overshoot
   1541                         *frame_over_shoot_limit  = cpi->this_frame_target * 10 / 8;
   1542                         *frame_under_shoot_limit = cpi->this_frame_target * 4 / 8;
   1543                     }
   1544                     else
   1545                     {
   1546                         *frame_over_shoot_limit  = cpi->this_frame_target * 11 / 8;
   1547                         *frame_under_shoot_limit = cpi->this_frame_target * 5 / 8;
   1548                     }
   1549                 }
   1550                 // VBR
   1551                 // Note that tighter restrictions here can help quality but hurt encode speed
   1552                 else
   1553                 {
   1554                     *frame_over_shoot_limit  = cpi->this_frame_target * 11 / 8;
   1555                     *frame_under_shoot_limit = cpi->this_frame_target * 5 / 8;
   1556                 }
   1557             }
   1558         }
   1559     }
   1560 }
   1561