blob: 7da3d71adc1982d8243fe9d74fcc2219722c59cd [file] [log] [blame]
John Koleszar0ea50ce2010-05-18 11:58:33 -04001/*
John Koleszarc2140b82010-09-09 08:16:39 -04002 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
John Koleszar0ea50ce2010-05-18 11:58:33 -04003 *
John Koleszar94c52e42010-06-18 12:39:21 -04004 * Use of this source code is governed by a BSD-style license
John Koleszar09202d82010-06-04 16:19:40 -04005 * 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
John Koleszar94c52e42010-06-18 12:39:21 -04007 * in the file PATENTS. All contributing project authors may
John Koleszar09202d82010-06-04 16:19:40 -04008 * be found in the AUTHORS file in the root of the source tree.
John Koleszar0ea50ce2010-05-18 11:58:33 -04009 */
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"
John Koleszar02321de2011-02-10 14:41:38 -050019#include "vp8/common/common.h"
John Koleszar0ea50ce2010-05-18 11:58:33 -040020#include "ratectrl.h"
John Koleszar02321de2011-02-10 14:41:38 -050021#include "vp8/common/entropymode.h"
John Koleszar0ea50ce2010-05-18 11:58:33 -040022#include "vpx_mem/vpx_mem.h"
John Koleszar02321de2011-02-10 14:41:38 -050023#include "vp8/common/systemdependent.h"
John Koleszar0ea50ce2010-05-18 11:58:33 -040024#include "encodemv.h"
James Zern820302a2015-08-27 14:46:08 -070025#include "vpx_dsp/vpx_dsp_common.h"
John Koleszar0ea50ce2010-05-18 11:58:33 -040026
27
28#define MIN_BPB_FACTOR 0.01
29#define MAX_BPB_FACTOR 50
30
31extern const MB_PREDICTION_MODE vp8_mode_order[MAX_MODES];
John Koleszar0ea50ce2010-05-18 11:58:33 -040032
33
34
35#ifdef MODE_STATS
36extern int y_modes[5];
37extern int uv_modes[4];
38extern int b_modes[10];
39
40extern int inter_y_modes[10];
41extern int inter_uv_modes[4];
42extern int inter_b_modes[10];
43#endif
44
John Koleszar0164a1c2012-05-21 14:30:56 -070045/* Bits Per MB at different Q (Multiplied by 512) */
John Koleszar0ea50ce2010-05-18 11:58:33 -040046#define BPER_MB_NORMBITS 9
47
John Koleszar0164a1c2012-05-21 14:30:56 -070048/* 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 */
John Koleszar0ea50ce2010-05-18 11:58:33 -040052const int vp8_bits_per_mb[2][QINDEX_RANGE] =
53{
John Koleszar0164a1c2012-05-21 14:30:56 -070054 /* Intra case 450000/Qintra */
John Koleszar0ea50ce2010-05-18 11:58:33 -040055 {
Paul Wilkinsad6150f2010-11-22 13:17:35 +000056 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,
John Koleszar0ea50ce2010-05-18 11:58:33 -040072 },
John Koleszar0164a1c2012-05-21 14:30:56 -070073 /* Inter case 285000/Qinter */
John Koleszar0ea50ce2010-05-18 11:58:33 -040074 {
Paul Wilkinsad6150f2010-11-22 13:17:35 +000075 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,
John Koleszar0ea50ce2010-05-18 11:58:33 -040091 }
92};
93
John Koleszar429dc672011-03-17 17:07:59 -040094static const int kf_boost_qadjustment[QINDEX_RANGE] =
John Koleszar0ea50ce2010-05-18 11:58:33 -040095{
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
John Koleszar0164a1c2012-05-21 14:30:56 -0700114/* #define GFQ_ADJUSTMENT (Q+100) */
John Koleszar0ea50ce2010-05-18 11:58:33 -0400115#define GFQ_ADJUSTMENT vp8_gf_boost_qadjustment[Q]
116const 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/*
137const 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
John Koleszar429dc672011-03-17 17:07:59 -0400158static const int kf_gf_boost_qlimits[QINDEX_RANGE] =
John Koleszar0ea50ce2010-05-18 11:58:33 -0400159{
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
John Koleszar429dc672011-03-17 17:07:59 -0400178static const int gf_adjust_table[101] =
John Koleszar0ea50ce2010-05-18 11:58:33 -0400179{
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
John Koleszar429dc672011-03-17 17:07:59 -0400193static const int gf_intra_usage_adjustment[20] =
John Koleszar0ea50ce2010-05-18 11:58:33 -0400194{
195 125, 120, 115, 110, 105, 100, 95, 85, 80, 75,
196 70, 65, 60, 55, 50, 50, 50, 50, 50, 50,
197};
198
John Koleszar429dc672011-03-17 17:07:59 -0400199static const int gf_interval_table[101] =
John Koleszar0ea50ce2010-05-18 11:58:33 -0400200{
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
214static const unsigned int prior_key_frame_weight[KEY_FRAME_CONTEXT] = { 1, 2, 3, 4, 5 };
215
216
217void vp8_save_coding_context(VP8_COMP *cpi)
218{
219 CODING_CONTEXT *const cc = & cpi->coding_context;
220
John Koleszar0164a1c2012-05-21 14:30:56 -0700221 /* 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 */
John Koleszar0ea50ce2010-05-18 11:58:33 -0400226
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;
James Zern5f30a0c2013-07-18 14:09:21 -0700230 cc->frames_since_golden = cpi->frames_since_golden;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400231
232 vp8_copy(cc->mvc, cpi->common.fc.mvc);
Attila Nagyb41c17d2012-04-17 10:40:56 +0300233 vp8_copy(cc->mvcosts, cpi->rd_costs.mvcosts);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400234
John Koleszar0ea50ce2010-05-18 11:58:33 -0400235 vp8_copy(cc->ymode_prob, cpi->common.fc.ymode_prob);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400236 vp8_copy(cc->uv_mode_prob, cpi->common.fc.uv_mode_prob);
237
Scott LaVarnway95390b22012-11-05 12:25:18 -0800238 vp8_copy(cc->ymode_count, cpi->mb.ymode_count);
239 vp8_copy(cc->uv_mode_count, cpi->mb.uv_mode_count);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400240
241
John Koleszar0164a1c2012-05-21 14:30:56 -0700242 /* Stats */
John Koleszar0ea50ce2010-05-18 11:58:33 -0400243#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
256void vp8_restore_coding_context(VP8_COMP *cpi)
257{
258 CODING_CONTEXT *const cc = & cpi->coding_context;
259
John Koleszar0164a1c2012-05-21 14:30:56 -0700260 /* Restore key state variables to the snapshot state stored in the
261 * previous call to vp8_save_coding_context.
262 */
John Koleszar0ea50ce2010-05-18 11:58:33 -0400263
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;
James Zern5f30a0c2013-07-18 14:09:21 -0700267 cpi->frames_since_golden = cc->frames_since_golden;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400268
269 vp8_copy(cpi->common.fc.mvc, cc->mvc);
270
Attila Nagyb41c17d2012-04-17 10:40:56 +0300271 vp8_copy(cpi->rd_costs.mvcosts, cc->mvcosts);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400272
John Koleszar0ea50ce2010-05-18 11:58:33 -0400273 vp8_copy(cpi->common.fc.ymode_prob, cc->ymode_prob);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400274 vp8_copy(cpi->common.fc.uv_mode_prob, cc->uv_mode_prob);
275
Scott LaVarnway95390b22012-11-05 12:25:18 -0800276 vp8_copy(cpi->mb.ymode_count, cc->ymode_count);
277 vp8_copy(cpi->mb.uv_mode_count, cc->uv_mode_count);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400278
John Koleszar0164a1c2012-05-21 14:30:56 -0700279 /* Stats */
John Koleszar0ea50ce2010-05-18 11:58:33 -0400280#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
294void vp8_setup_key_frame(VP8_COMP *cpi)
295{
John Koleszar0164a1c2012-05-21 14:30:56 -0700296 /* Setup for Key frame: */
John Koleszar0ea50ce2010-05-18 11:58:33 -0400297
298 vp8_default_coef_probs(& cpi->common);
Jim Bankoski6de67cd2011-12-09 16:56:18 -0800299
James Zernf274c212015-04-23 20:42:19 -0700300 memcpy(cpi->common.fc.mvc, vp8_default_mv_context, sizeof(vp8_default_mv_context));
John Koleszar0ea50ce2010-05-18 11:58:33 -0400301 {
302 int flag[2] = {1, 1};
Yunqing Wang3d681582011-04-01 16:41:58 -0400303 vp8_build_component_cost_table(cpi->mb.mvcost, (const MV_CONTEXT *) cpi->common.fc.mvc, flag);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400304 }
305
John Koleszar0164a1c2012-05-21 14:30:56 -0700306 /* 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 */
James Zernf274c212015-04-23 20:42:19 -0700309 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));
Jim Bankoski6de67cd2011-12-09 16:56:18 -0800312
John Koleszar0ea50ce2010-05-18 11:58:33 -0400313 cpi->common.filter_level = cpi->common.base_qindex * 3 / 8 ;
314
John Koleszar0164a1c2012-05-21 14:30:56 -0700315 /* Provisional interval before next GF */
John Koleszar0ea50ce2010-05-18 11:58:33 -0400316 if (cpi->auto_gold)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400317 cpi->frames_till_gf_update_due = cpi->baseline_gf_interval;
318 else
Yunqing Wang0ca0c4f2012-06-12 11:57:26 -0400319 cpi->frames_till_gf_update_due = DEFAULT_GF_INTERVAL;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400320
John Koleszarf56918b2011-12-21 14:21:29 -0800321 cpi->common.refresh_golden_frame = 1;
322 cpi->common.refresh_alt_ref_frame = 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400323}
324
John Koleszar81d22062011-04-25 15:02:54 -0400325
John Koleszardb5057c2011-04-26 16:45:30 -0400326static 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}
John Koleszar81d22062011-04-25 15:02:54 -0400340
341
John Koleszardb5057c2011-04-26 16:45:30 -0400342static void calc_iframe_target_size(VP8_COMP *cpi)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400343{
John Koleszar0164a1c2012-05-21 14:30:56 -0700344 /* boost defaults to half second */
John Koleszar0ea50ce2010-05-18 11:58:33 -0400345 int kf_boost;
Yaowu Xud71ba032012-08-17 10:05:35 -0700346 uint64_t target;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400347
John Koleszar0164a1c2012-05-21 14:30:56 -0700348 /* Clear down mmx registers to allow floating point in what follows */
349 vp8_clear_system_state();
John Koleszar0ea50ce2010-05-18 11:58:33 -0400350
351 if (cpi->oxcf.fixed_q >= 0)
352 {
John Koleszardb5057c2011-04-26 16:45:30 -0400353 int Q = cpi->oxcf.key_q;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400354
John Koleszardb5057c2011-04-26 16:45:30 -0400355 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)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400359 {
John Koleszar0164a1c2012-05-21 14:30:56 -0700360 /* New Two pass RC */
John Koleszardb5057c2011-04-26 16:45:30 -0400361 target = cpi->per_frame_bandwidth;
362 }
John Koleszar0164a1c2012-05-21 14:30:56 -0700363 /* First Frame is a special case */
John Koleszardb5057c2011-04-26 16:45:30 -0400364 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;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400374 }
375 else
376 {
John Koleszar0164a1c2012-05-21 14:30:56 -0700377 /* if this keyframe was forced, use a more recent Q estimate */
John Koleszardb5057c2011-04-26 16:45:30 -0400378 int Q = (cpi->common.frame_flags & FRAMEFLAGS_KEY)
379 ? cpi->avg_frame_qindex : cpi->ni_av_qi;
380
Marco Paniconief4a9672012-07-27 17:24:29 -0700381 int initial_boost = 32; /* |3.0 * per_frame_bandwidth| */
John Koleszar0164a1c2012-05-21 14:30:56 -0700382 /* Boost depends somewhat on frame rate: only used for 1 layer case. */
Marco Paniconi507ee872012-02-22 14:39:17 -0800383 if (cpi->oxcf.number_of_layers == 1) {
James Zern820302a2015-08-27 14:46:08 -0700384 kf_boost = VPXMAX(initial_boost,
385 (int)(2 * cpi->output_framerate - 16));
Marco Paniconi507ee872012-02-22 14:39:17 -0800386 }
387 else {
Marco Paniconief4a9672012-07-27 17:24:29 -0700388 /* Initial factor: set target size to: |3.0 * per_frame_bandwidth|. */
Marco Paniconi30140912012-03-12 16:23:08 -0700389 kf_boost = initial_boost;
Marco Paniconi507ee872012-02-22 14:39:17 -0800390 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400391
John Koleszar0164a1c2012-05-21 14:30:56 -0700392 /* adjustment up based on q: this factor ranges from ~1.2 to 2.2. */
John Koleszardb5057c2011-04-26 16:45:30 -0400393 kf_boost = kf_boost * kf_boost_qadjustment[Q] / 100;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400394
John Koleszar0164a1c2012-05-21 14:30:56 -0700395 /* frame separation adjustment ( down) */
James Zern9581eb62013-07-12 17:12:46 -0700396 if (cpi->frames_since_key < cpi->output_framerate / 2)
John Koleszardb5057c2011-04-26 16:45:30 -0400397 kf_boost = (int)(kf_boost
James Zern9581eb62013-07-12 17:12:46 -0700398 * cpi->frames_since_key / (cpi->output_framerate / 2));
John Koleszar0ea50ce2010-05-18 11:58:33 -0400399
John Koleszar0164a1c2012-05-21 14:30:56 -0700400 /* Minimal target size is |2* per_frame_bandwidth|. */
John Koleszar0ea50ce2010-05-18 11:58:33 -0400401 if (kf_boost < 16)
402 kf_boost = 16;
403
John Koleszardb5057c2011-04-26 16:45:30 -0400404 target = ((16 + kf_boost) * cpi->per_frame_bandwidth) >> 4;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400405 }
406
407
John Koleszardb5057c2011-04-26 16:45:30 -0400408 if (cpi->oxcf.rc_max_intra_bitrate_pct)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400409 {
John Koleszardb5057c2011-04-26 16:45:30 -0400410 unsigned int max_rate = cpi->per_frame_bandwidth
411 * cpi->oxcf.rc_max_intra_bitrate_pct / 100;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400412
John Koleszardb5057c2011-04-26 16:45:30 -0400413 if (target > max_rate)
414 target = max_rate;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400415 }
416
Yaowu Xud71ba032012-08-17 10:05:35 -0700417 cpi->this_frame_target = (int)target;
John Koleszardb5057c2011-04-26 16:45:30 -0400418
John Koleszar0164a1c2012-05-21 14:30:56 -0700419 /* 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 */
John Koleszarabc99582011-05-06 11:48:50 -0400422 if (cpi->pass != 2)
423 cpi->active_worst_quality = cpi->worst_quality;
John Koleszardb5057c2011-04-26 16:45:30 -0400424
425#if 0
John Koleszar0ea50ce2010-05-18 11:58:33 -0400426 {
427 FILE *f;
428
429 f = fopen("kf_boost.stt", "a");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400430 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 }
John Koleszardb5057c2011-04-26 16:45:30 -0400435#endif
John Koleszar0ea50ce2010-05-18 11:58:33 -0400436}
437
John Koleszardb5057c2011-04-26 16:45:30 -0400438
John Koleszar0164a1c2012-05-21 14:30:56 -0700439/* Do the best we can to define the parameters for the next GF based on what
440 * information we have available.
441 */
John Koleszar0ea50ce2010-05-18 11:58:33 -0400442static 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
John Koleszar0164a1c2012-05-21 14:30:56 -0700447 int gf_frame_useage = 0; /* Golden frame useage since last GF */
John Koleszar0ea50ce2010-05-18 11:58:33 -0400448 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
Scott LaVarnway99f46d62010-08-11 11:02:31 -0400453 int pct_gf_active = (100 * cpi->gf_active_count) / (cpi->common.mb_rows * cpi->common.mb_cols);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400454
John Koleszar0ea50ce2010-05-18 11:58:33 -0400455 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
John Koleszar0164a1c2012-05-21 14:30:56 -0700461 /* Not two pass */
John Koleszar0ea50ce2010-05-18 11:58:33 -0400462 if (cpi->pass != 2)
463 {
John Koleszar0164a1c2012-05-21 14:30:56 -0700464 /* Single Pass lagged mode: TBD */
John Koleszarf56918b2011-12-21 14:21:29 -0800465 if (0)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400466 {
467 }
468
John Koleszar0164a1c2012-05-21 14:30:56 -0700469 /* Single Pass compression: Has to use current and historical data */
John Koleszar0ea50ce2010-05-18 11:58:33 -0400470 else
471 {
472#if 0
John Koleszar0164a1c2012-05-21 14:30:56 -0700473 /* Experimental code */
John Koleszar0ea50ce2010-05-18 11:58:33 -0400474 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
John Koleszar0164a1c2012-05-21 14:30:56 -0700477 /* ************** Experimental code - incomplete */
John Koleszar0ea50ce2010-05-18 11:58:33 -0400478 /*
John Koleszar0ea50ce2010-05-18 11:58:33 -0400479 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 /*************************************************************/
John Koleszar0164a1c2012-05-21 14:30:56 -0700521 /* OLD code */
John Koleszar0ea50ce2010-05-18 11:58:33 -0400522
John Koleszar0164a1c2012-05-21 14:30:56 -0700523 /* Adjust boost based upon ambient Q */
John Koleszar0ea50ce2010-05-18 11:58:33 -0400524 Boost = GFQ_ADJUSTMENT;
525
John Koleszar0164a1c2012-05-21 14:30:56 -0700526 /* Adjust based upon most recently measure intra useage */
John Koleszar429dc672011-03-17 17:07:59 -0400527 Boost = Boost * gf_intra_usage_adjustment[(cpi->this_frame_percent_intra < 15) ? cpi->this_frame_percent_intra : 14] / 100;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400528
John Koleszar0164a1c2012-05-21 14:30:56 -0700529 /* Adjust gf boost based upon GF usage since last GF */
John Koleszar429dc672011-03-17 17:07:59 -0400530 Boost = Boost * gf_adjust_table[gf_frame_useage] / 100;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400531#endif
532 }
533
John Koleszar0164a1c2012-05-21 14:30:56 -0700534 /* golden frame boost without recode loop often goes awry. be
535 * safe by keeping numbers down.
536 */
John Koleszar0ea50ce2010-05-18 11:58:33 -0400537 if (!cpi->sf.recode_loop)
538 {
539 if (cpi->compressor_speed == 2)
540 Boost = Boost / 2;
541 }
542
John Koleszar0164a1c2012-05-21 14:30:56 -0700543 /* Apply an upper limit based on Q for 1 pass encodes */
John Koleszar429dc672011-03-17 17:07:59 -0400544 if (Boost > kf_gf_boost_qlimits[Q] && (cpi->pass == 0))
545 Boost = kf_gf_boost_qlimits[Q];
John Koleszar0ea50ce2010-05-18 11:58:33 -0400546
John Koleszar0164a1c2012-05-21 14:30:56 -0700547 /* Apply lower limits to boost. */
John Koleszar0ea50ce2010-05-18 11:58:33 -0400548 else if (Boost < 110)
549 Boost = 110;
550
John Koleszar0164a1c2012-05-21 14:30:56 -0700551 /* Note the boost used */
John Koleszar0ea50ce2010-05-18 11:58:33 -0400552 cpi->last_boost = Boost;
553
554 }
555
John Koleszar0164a1c2012-05-21 14:30:56 -0700556 /* Estimate next interval
557 * This is updated once the real frame size/boost is known.
558 */
John Koleszar0ea50ce2010-05-18 11:58:33 -0400559 if (cpi->oxcf.fixed_q == -1)
560 {
John Koleszar0164a1c2012-05-21 14:30:56 -0700561 if (cpi->pass == 2) /* 2 Pass */
John Koleszar0ea50ce2010-05-18 11:58:33 -0400562 {
563 cpi->frames_till_gf_update_due = cpi->baseline_gf_interval;
564 }
John Koleszar0164a1c2012-05-21 14:30:56 -0700565 else /* 1 Pass */
John Koleszar0ea50ce2010-05-18 11:58:33 -0400566 {
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
John Koleszar429dc672011-03-17 17:07:59 -0400581 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];
John Koleszar0ea50ce2010-05-18 11:58:33 -0400583
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
John Koleszar0164a1c2012-05-21 14:30:56 -0700591 /* ARF on or off */
John Koleszar0ea50ce2010-05-18 11:58:33 -0400592 if (cpi->pass != 2)
593 {
John Koleszar0164a1c2012-05-21 14:30:56 -0700594 /* For now Alt ref is not allowed except in 2 pass modes. */
John Koleszarf56918b2011-12-21 14:21:29 -0800595 cpi->source_alt_ref_pending = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400596
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)) ) )
John Koleszarf56918b2011-12-21 14:21:29 -0800600 cpi->source_alt_ref_pending = 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400601 else
John Koleszarf56918b2011-12-21 14:21:29 -0800602 cpi->source_alt_ref_pending = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400603 }*/
604 }
605}
John Koleszar0ea50ce2010-05-18 11:58:33 -0400606
607
John Koleszar81d22062011-04-25 15:02:54 -0400608static void calc_pframe_target_size(VP8_COMP *cpi)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400609{
John Koleszardb67dcb2011-06-23 11:47:09 -0400610 int min_frame_target;
Adrian Grange217591f2011-10-06 15:49:11 -0700611 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;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400616
John Koleszare9613172011-08-12 14:51:36 -0400617 min_frame_target = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400618
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;
John Koleszar212f6182011-05-03 11:17:05 -0400625 }
John Koleszare9613172011-08-12 14:51:36 -0400626 else if (min_frame_target < cpi->per_frame_bandwidth / 4)
627 min_frame_target = cpi->per_frame_bandwidth / 4;
628
John Koleszar0ea50ce2010-05-18 11:58:33 -0400629
John Koleszar0164a1c2012-05-21 14:30:56 -0700630 /* Special alt reference frame case */
Adrian Grange217591f2011-10-06 15:49:11 -0700631 if((cpi->common.refresh_alt_ref_frame) && (cpi->oxcf.number_of_layers == 1))
John Koleszar0ea50ce2010-05-18 11:58:33 -0400632 {
633 if (cpi->pass == 2)
634 {
John Koleszar0164a1c2012-05-21 14:30:56 -0700635 /* Per frame bit target for the alt ref frame */
636 cpi->per_frame_bandwidth = cpi->twopass.gf_bits;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400637 cpi->this_frame_target = cpi->per_frame_bandwidth;
638 }
639
640 /* One Pass ??? TBD */
John Koleszar0ea50ce2010-05-18 11:58:33 -0400641 }
642
John Koleszar0164a1c2012-05-21 14:30:56 -0700643 /* Normal frames (gf,and inter) */
John Koleszar0ea50ce2010-05-18 11:58:33 -0400644 else
645 {
John Koleszar0164a1c2012-05-21 14:30:56 -0700646 /* 2 pass */
John Koleszar0ea50ce2010-05-18 11:58:33 -0400647 if (cpi->pass == 2)
648 {
649 cpi->this_frame_target = cpi->per_frame_bandwidth;
650 }
John Koleszar0164a1c2012-05-21 14:30:56 -0700651 /* 1 pass */
John Koleszar0ea50ce2010-05-18 11:58:33 -0400652 else
653 {
Scott LaVarnway96e41cb2013-03-14 09:49:38 -0700654 int Adjustment;
John Koleszar0164a1c2012-05-21 14:30:56 -0700655 /* 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 */
John Koleszar0ea50ce2010-05-18 11:58:33 -0400659 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
John Koleszar0164a1c2012-05-21 14:30:56 -0700668 /* 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 */
John Koleszar0ea50ce2010-05-18 11:58:33 -0400672 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
John Koleszar0164a1c2012-05-21 14:30:56 -0700680 /* If appropriate make an adjustment to recover bits spent on a
681 * recent GF
682 */
John Koleszar0ea50ce2010-05-18 11:58:33 -0400683 if ((cpi->gf_overspend_bits > 0) && (cpi->this_frame_target > min_frame_target))
684 {
Scott LaVarnway96e41cb2013-03-14 09:49:38 -0700685 Adjustment = (cpi->non_gf_bitrate_adjustment <= cpi->gf_overspend_bits) ? cpi->non_gf_bitrate_adjustment : cpi->gf_overspend_bits;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400686
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
John Koleszar0164a1c2012-05-21 14:30:56 -0700694 /* Apply small + and - boosts for non gf frames */
John Koleszar0ea50ce2010-05-18 11:58:33 -0400695 if ((cpi->last_boost > 150) && (cpi->frames_till_gf_update_due > 0) &&
696 (cpi->current_gf_interval >= (MIN_GF_INTERVAL << 1)))
697 {
John Koleszar0164a1c2012-05-21 14:30:56 -0700698 /* % Adjustment limited to the range 1% to 10% */
John Koleszar0ea50ce2010-05-18 11:58:33 -0400699 Adjustment = (cpi->last_boost - 100) >> 5;
700
701 if (Adjustment < 1)
702 Adjustment = 1;
703 else if (Adjustment > 10)
704 Adjustment = 10;
705
John Koleszar0164a1c2012-05-21 14:30:56 -0700706 /* Convert to bits */
John Koleszar0ea50ce2010-05-18 11:58:33 -0400707 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
James Zern5f30a0c2013-07-18 14:09:21 -0700712 if (cpi->frames_since_golden == (cpi->current_gf_interval >> 1))
Marcoaf898b52014-11-10 13:07:05 -0800713 {
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 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400720 else
721 cpi->this_frame_target -= Adjustment;
722 }
723 }
724 }
725
John Koleszar0164a1c2012-05-21 14:30:56 -0700726 /* 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 */
John Koleszar0ea50ce2010-05-18 11:58:33 -0400734 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)
John Koleszar0164a1c2012-05-21 14:30:56 -0700738 /* Note the baseline target data rate for this inter frame. */
John Koleszar0ea50ce2010-05-18 11:58:33 -0400739 cpi->inter_frame_target = cpi->this_frame_target;
740
John Koleszar0164a1c2012-05-21 14:30:56 -0700741 /* One Pass specific code */
John Koleszar0ea50ce2010-05-18 11:58:33 -0400742 if (cpi->pass == 0)
743 {
John Koleszar0164a1c2012-05-21 14:30:56 -0700744 /* Adapt target frame size with respect to any buffering constraints: */
John Koleszar0ea50ce2010-05-18 11:58:33 -0400745 if (cpi->buffered_mode)
746 {
Yaowu Xud71ba032012-08-17 10:05:35 -0700747 int one_percent_bits = (int)
748 (1 + cpi->oxcf.optimal_buffer_level / 100);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400749
Paul Wilkins2ae91fb2011-03-10 16:11:39 +0000750 if ((cpi->buffer_level < cpi->oxcf.optimal_buffer_level) ||
751 (cpi->bits_off_target < cpi->oxcf.optimal_buffer_level))
John Koleszar0ea50ce2010-05-18 11:58:33 -0400752 {
753 int percent_low = 0;
754
John Koleszar0164a1c2012-05-21 14:30:56 -0700755 /* 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 */
Paul Wilkins2ae91fb2011-03-10 16:11:39 +0000762 if ((cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) &&
763 (cpi->buffer_level < cpi->oxcf.optimal_buffer_level))
John Koleszar0ea50ce2010-05-18 11:58:33 -0400764 {
Yaowu Xud71ba032012-08-17 10:05:35 -0700765 percent_low = (int)
766 ((cpi->oxcf.optimal_buffer_level - cpi->buffer_level) /
767 one_percent_bits);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400768 }
John Koleszar0164a1c2012-05-21 14:30:56 -0700769 /* Are we overshooting the long term clip data rate... */
John Koleszar0ea50ce2010-05-18 11:58:33 -0400770 else if (cpi->bits_off_target < 0)
771 {
John Koleszar0164a1c2012-05-21 14:30:56 -0700772 /* Adjust per frame data target downwards to compensate. */
Paul Wilkins2ae91fb2011-03-10 16:11:39 +0000773 percent_low = (int)(100 * -cpi->bits_off_target /
774 (cpi->total_byte_count * 8));
John Koleszar0ea50ce2010-05-18 11:58:33 -0400775 }
776
John Koleszarc99f9d72011-04-11 11:29:23 -0400777 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
John Koleszar0164a1c2012-05-21 14:30:56 -0700782 /* lower the target bandwidth for this frame. */
Adrian Grange217591f2011-10-06 15:49:11 -0700783 cpi->this_frame_target -=
784 (cpi->this_frame_target * percent_low) / 200;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400785
John Koleszar0164a1c2012-05-21 14:30:56 -0700786 /* Are we using allowing control of active_worst_allowed_q
787 * according to buffer level.
788 */
Adrian Grange217591f2011-10-06 15:49:11 -0700789 if (cpi->auto_worst_q && cpi->ni_frames > 150)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400790 {
Yaowu Xud71ba032012-08-17 10:05:35 -0700791 int64_t critical_buffer_level;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400792
John Koleszar0164a1c2012-05-21 14:30:56 -0700793 /* 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 */
John Koleszar0ea50ce2010-05-18 11:58:33 -0400799 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
800 {
John Koleszar0164a1c2012-05-21 14:30:56 -0700801 /* Take the smaller of cpi->buffer_level and
802 * cpi->bits_off_target
803 */
Paul Wilkins2ae91fb2011-03-10 16:11:39 +0000804 critical_buffer_level =
805 (cpi->buffer_level < cpi->bits_off_target)
806 ? cpi->buffer_level : cpi->bits_off_target;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400807 }
John Koleszar0164a1c2012-05-21 14:30:56 -0700808 /* For local file playback short term buffering constraints
809 * are less of an issue
810 */
John Koleszar0ea50ce2010-05-18 11:58:33 -0400811 else
812 {
John Koleszar0164a1c2012-05-21 14:30:56 -0700813 /* Consider only how we are doing for the clip as a
814 * whole
815 */
John Koleszar0ea50ce2010-05-18 11:58:33 -0400816 critical_buffer_level = cpi->bits_off_target;
817 }
818
John Koleszar0164a1c2012-05-21 14:30:56 -0700819 /* Set the active worst quality based upon the selected
820 * buffer fullness number.
821 */
John Koleszar0ea50ce2010-05-18 11:58:33 -0400822 if (critical_buffer_level < cpi->oxcf.optimal_buffer_level)
823 {
Paul Wilkins2ae91fb2011-03-10 16:11:39 +0000824 if ( critical_buffer_level >
825 (cpi->oxcf.optimal_buffer_level >> 2) )
John Koleszar0ea50ce2010-05-18 11:58:33 -0400826 {
James Zernb45065d2011-07-25 18:44:59 -0700827 int64_t qadjustment_range =
Paul Wilkins2ae91fb2011-03-10 16:11:39 +0000828 cpi->worst_quality - cpi->ni_av_qi;
James Zernb45065d2011-07-25 18:44:59 -0700829 int64_t above_base =
Paul Wilkins2ae91fb2011-03-10 16:11:39 +0000830 (critical_buffer_level -
831 (cpi->oxcf.optimal_buffer_level >> 2));
John Koleszar0ea50ce2010-05-18 11:58:33 -0400832
John Koleszar0164a1c2012-05-21 14:30:56 -0700833 /* 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 */
Paul Wilkins2ae91fb2011-03-10 16:11:39 +0000840 cpi->active_worst_quality =
841 cpi->worst_quality -
Yaowu Xud71ba032012-08-17 10:05:35 -0700842 (int)((qadjustment_range * above_base) /
Paul Wilkins2ae91fb2011-03-10 16:11:39 +0000843 (cpi->oxcf.optimal_buffer_level*3>>2));
John Koleszar0ea50ce2010-05-18 11:58:33 -0400844 }
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 {
John Koleszarc99f9d72011-04-11 11:29:23 -0400862 int percent_high = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400863
John Koleszarc99f9d72011-04-11 11:29:23 -0400864 if ((cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
865 && (cpi->buffer_level > cpi->oxcf.optimal_buffer_level))
John Koleszar0ea50ce2010-05-18 11:58:33 -0400866 {
Yaowu Xud71ba032012-08-17 10:05:35 -0700867 percent_high = (int)((cpi->buffer_level
John Koleszarc99f9d72011-04-11 11:29:23 -0400868 - cpi->oxcf.optimal_buffer_level)
Yaowu Xud71ba032012-08-17 10:05:35 -0700869 / one_percent_bits);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400870 }
John Koleszarc99f9d72011-04-11 11:29:23 -0400871 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 *
Adrian Grange217591f2011-10-06 15:49:11 -0700883 percent_high) / 200;
John Koleszarc99f9d72011-04-11 11:29:23 -0400884
John Koleszar0164a1c2012-05-21 14:30:56 -0700885 /* Are we allowing control of active_worst_allowed_q according
886 * to buffer level.
887 */
Adrian Grange217591f2011-10-06 15:49:11 -0700888 if (cpi->auto_worst_q && cpi->ni_frames > 150)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400889 {
John Koleszar0164a1c2012-05-21 14:30:56 -0700890 /* When using the relaxed buffer model stick to the
891 * user specified value
892 */
John Koleszar0ea50ce2010-05-18 11:58:33 -0400893 cpi->active_worst_quality = cpi->ni_av_qi;
894 }
895 else
896 {
897 cpi->active_worst_quality = cpi->worst_quality;
898 }
899 }
900
John Koleszar0164a1c2012-05-21 14:30:56 -0700901 /* Set active_best_quality to prevent quality rising too high */
John Koleszar0ea50ce2010-05-18 11:58:33 -0400902 cpi->active_best_quality = cpi->best_quality;
903
John Koleszar0164a1c2012-05-21 14:30:56 -0700904 /* Worst quality obviously must not be better than best quality */
John Koleszar0ea50ce2010-05-18 11:58:33 -0400905 if (cpi->active_worst_quality <= cpi->active_best_quality)
906 cpi->active_worst_quality = cpi->active_best_quality + 1;
907
James Berryf8b431c2011-12-06 13:08:44 -0500908 if(cpi->active_worst_quality > 127)
909 cpi->active_worst_quality = 127;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400910 }
John Koleszar0164a1c2012-05-21 14:30:56 -0700911 /* Unbuffered mode (eg. video conferencing) */
John Koleszar0ea50ce2010-05-18 11:58:33 -0400912 else
913 {
John Koleszar0164a1c2012-05-21 14:30:56 -0700914 /* Set the active worst quality */
John Koleszar0ea50ce2010-05-18 11:58:33 -0400915 cpi->active_worst_quality = cpi->worst_quality;
916 }
Paul Wilkins2ae91fb2011-03-10 16:11:39 +0000917
John Koleszar0164a1c2012-05-21 14:30:56 -0700918 /* Special trap for constrained quality mode
919 * "active_worst_quality" may never drop below cq level
920 * for any frame type.
921 */
Paul Wilkins2ae91fb2011-03-10 16:11:39 +0000922 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 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400927 }
928
John Koleszar0164a1c2012-05-21 14:30:56 -0700929 /* 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 */
Yunqing Wang9904c282012-06-07 11:26:47 -0400935 if (cpi->drop_frames_allowed &&
John Koleszar0ea50ce2010-05-18 11:58:33 -0400936 (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) &&
John Koleszar0164a1c2012-05-21 14:30:56 -0700937 ((cpi->common.frame_type != KEY_FRAME)))
John Koleszar0ea50ce2010-05-18 11:58:33 -0400938 {
John Koleszar0164a1c2012-05-21 14:30:56 -0700939 /* Check for a buffer underun-crisis in which case we have to drop
940 * a frame
941 */
John Koleszar0ea50ce2010-05-18 11:58:33 -0400942 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
John Koleszarf56918b2011-12-21 14:21:29 -0800952 cpi->drop_frame = 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400953
John Koleszar0164a1c2012-05-21 14:30:56 -0700954 /* Update the buffer level variable. */
John Koleszar0ea50ce2010-05-18 11:58:33 -0400955 cpi->bits_off_target += cpi->av_per_frame_bandwidth;
Adrian Grangeb615a6d2011-11-07 17:15:28 -0800956 if (cpi->bits_off_target > cpi->oxcf.maximum_buffer_size)
Yaowu Xud71ba032012-08-17 10:05:35 -0700957 cpi->bits_off_target = (int)cpi->oxcf.maximum_buffer_size;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400958 cpi->buffer_level = cpi->bits_off_target;
Marco Paniconi41138e02013-11-01 11:03:03 -0700959
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 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400974 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400975 }
976
John Koleszar0164a1c2012-05-21 14:30:56 -0700977 /* Adjust target frame size for Golden Frames: */
John Koleszar0ea50ce2010-05-18 11:58:33 -0400978 if (cpi->oxcf.error_resilient_mode == 0 &&
979 (cpi->frames_till_gf_update_due == 0) && !cpi->drop_frame)
980 {
John Koleszar0ea50ce2010-05-18 11:58:33 -0400981 int Q = (cpi->oxcf.fixed_q < 0) ? cpi->last_q[INTER_FRAME] : cpi->oxcf.fixed_q;
982
John Koleszar0164a1c2012-05-21 14:30:56 -0700983 int gf_frame_useage = 0; /* Golden frame useage since last GF */
John Koleszar0ea50ce2010-05-18 11:58:33 -0400984 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
Scott LaVarnway99f46d62010-08-11 11:02:31 -0400989 int pct_gf_active = (100 * cpi->gf_active_count) / (cpi->common.mb_rows * cpi->common.mb_cols);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400990
John Koleszar0ea50ce2010-05-18 11:58:33 -0400991 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
John Koleszar0164a1c2012-05-21 14:30:56 -0700997 /* Is a fixed manual GF frequency being used */
Patrik Westin85340712010-11-16 11:06:00 +0100998 if (cpi->auto_gold)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400999 {
John Koleszar0164a1c2012-05-21 14:30:56 -07001000 /* For one pass throw a GF if recent frame intra useage is
1001 * low or the GF useage is high
1002 */
John Koleszar0ea50ce2010-05-18 11:58:33 -04001003 if ((cpi->pass == 0) && (cpi->this_frame_percent_intra < 15 || gf_frame_useage >= 5))
John Koleszarf56918b2011-12-21 14:21:29 -08001004 cpi->common.refresh_golden_frame = 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001005
John Koleszar0164a1c2012-05-21 14:30:56 -07001006 /* Two pass GF descision */
John Koleszar0ea50ce2010-05-18 11:58:33 -04001007 else if (cpi->pass == 2)
John Koleszarf56918b2011-12-21 14:21:29 -08001008 cpi->common.refresh_golden_frame = 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001009 }
1010
1011#if 0
1012
John Koleszar0164a1c2012-05-21 14:30:56 -07001013 /* Debug stats */
John Koleszar0ea50ce2010-05-18 11:58:33 -04001014 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
John Koleszarf56918b2011-12-21 14:21:29 -08001026 if (cpi->common.refresh_golden_frame == 1)
John Koleszar0ea50ce2010-05-18 11:58:33 -04001027 {
John Koleszar0ea50ce2010-05-18 11:58:33 -04001028#if 0
1029
John Koleszar0164a1c2012-05-21 14:30:56 -07001030 if (0)
John Koleszar0ea50ce2010-05-18 11:58:33 -04001031 {
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
John Koleszar0ea50ce2010-05-18 11:58:33 -04001040
1041 if (cpi->auto_adjust_gold_quantizer)
1042 {
1043 calc_gf_params(cpi);
1044 }
1045
John Koleszar0164a1c2012-05-21 14:30:56 -07001046 /* 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 */
John Koleszar0ea50ce2010-05-18 11:58:33 -04001050 if (!cpi->source_alt_ref_active)
1051 {
1052 if (cpi->oxcf.fixed_q < 0)
1053 {
1054 if (cpi->pass == 2)
1055 {
John Koleszar0164a1c2012-05-21 14:30:56 -07001056 /* 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;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001060 }
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
John Koleszar0164a1c2012-05-21 14:30:56 -07001068 /* Normalize Altboost and allocations chunck down to
1069 * prevent overflow
1070 */
John Koleszar0ea50ce2010-05-18 11:58:33 -04001071 while (Boost > 1000)
1072 {
1073 Boost /= 2;
1074 allocation_chunks /= 2;
1075 }
1076
John Koleszar0164a1c2012-05-21 14:30:56 -07001077 /* Avoid loss of precision but avoid overflow */
John Koleszar0ea50ce2010-05-18 11:58:33 -04001078 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
John Koleszardb5057c2011-04-26 16:45:30 -04001085 cpi->this_frame_target =
1086 (estimate_bits_at_q(1, Q, cpi->common.MBs, 1.0)
1087 * cpi->last_boost) / 100;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001088
1089 }
John Koleszar0164a1c2012-05-21 14:30:56 -07001090 /* 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 */
Paul Wilkinsbf180692010-07-19 14:10:07 +01001095 else
John Koleszar0ea50ce2010-05-18 11:58:33 -04001096 {
Paul Wilkinsbf180692010-07-19 14:10:07 +01001097 cpi->this_frame_target = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001098 }
1099
1100 cpi->current_gf_interval = cpi->frames_till_gf_update_due;
1101
1102 }
1103 }
Adrian Grange217591f2011-10-06 15:49:11 -07001104
1105 cpi->per_frame_bandwidth = old_per_frame_bandwidth;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001106}
1107
1108
1109void 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
John Koleszar0164a1c2012-05-21 14:30:56 -07001118 /* Clear down mmx registers to allow floating point in what follows */
1119 vp8_clear_system_state();
John Koleszar0ea50ce2010-05-18 11:58:33 -04001120
1121 if (cpi->common.frame_type == KEY_FRAME)
1122 {
1123 rate_correction_factor = cpi->key_frame_rate_correction_factor;
1124 }
1125 else
1126 {
Marco Paniconi464b1df2012-11-26 14:14:50 -08001127 if (cpi->oxcf.number_of_layers == 1 &&
1128 (cpi->common.refresh_alt_ref_frame ||
1129 cpi->common.refresh_golden_frame))
John Koleszar0ea50ce2010-05-18 11:58:33 -04001130 rate_correction_factor = cpi->gf_rate_correction_factor;
1131 else
1132 rate_correction_factor = cpi->rate_correction_factor;
1133 }
1134
John Koleszar0164a1c2012-05-21 14:30:56 -07001135 /* 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 */
John Koleszar0ea50ce2010-05-18 11:58:33 -04001139 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
John Koleszar0164a1c2012-05-21 14:30:56 -07001141 /* Make some allowance for cpi->zbin_over_quant */
Scott LaVarnway3a19eeb2012-12-10 10:51:42 -08001142 if (cpi->mb.zbin_over_quant > 0)
John Koleszar0ea50ce2010-05-18 11:58:33 -04001143 {
Scott LaVarnway3a19eeb2012-12-10 10:51:42 -08001144 int Z = cpi->mb.zbin_over_quant;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001145 double Factor = 0.99;
John Koleszar0164a1c2012-05-21 14:30:56 -07001146 double factor_adjustment = 0.01 / 256.0;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001147
1148 while (Z > 0)
1149 {
1150 Z --;
Paul Wilkinsc012d632010-05-20 16:49:39 +01001151 projected_size_based_on_q =
1152 (int)(Factor * projected_size_based_on_q);
John Koleszar0ea50ce2010-05-18 11:58:33 -04001153 Factor += factor_adjustment;
1154
1155 if (Factor >= 0.999)
1156 Factor = 0.999;
1157 }
1158 }
1159
John Koleszar0164a1c2012-05-21 14:30:56 -07001160 /* Work out a size correction factor. */
John Koleszar0ea50ce2010-05-18 11:58:33 -04001161 if (projected_size_based_on_q > 0)
1162 correction_factor = (100 * cpi->projected_frame_size) / projected_size_based_on_q;
1163
John Koleszar0164a1c2012-05-21 14:30:56 -07001164 /* More heavily damped adjustment used if we have been oscillating
1165 * either side of target
1166 */
John Koleszar0ea50ce2010-05-18 11:58:33 -04001167 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
John Koleszar0ea50ce2010-05-18 11:58:33 -04001181 if (correction_factor > 102)
1182 {
John Koleszar0164a1c2012-05-21 14:30:56 -07001183 /* We are not already at the worst allowable quality */
John Koleszar0ea50ce2010-05-18 11:58:33 -04001184 correction_factor = (int)(100.5 + ((correction_factor - 100) * adjustment_limit));
1185 rate_correction_factor = ((rate_correction_factor * correction_factor) / 100);
1186
John Koleszar0164a1c2012-05-21 14:30:56 -07001187 /* Keep rate_correction_factor within limits */
John Koleszar0ea50ce2010-05-18 11:58:33 -04001188 if (rate_correction_factor > MAX_BPB_FACTOR)
1189 rate_correction_factor = MAX_BPB_FACTOR;
1190 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04001191 else if (correction_factor < 99)
1192 {
John Koleszar0164a1c2012-05-21 14:30:56 -07001193 /* We are not already at the best allowable quality */
John Koleszar0ea50ce2010-05-18 11:58:33 -04001194 correction_factor = (int)(100.5 - ((100 - correction_factor) * adjustment_limit));
1195 rate_correction_factor = ((rate_correction_factor * correction_factor) / 100);
1196
John Koleszar0164a1c2012-05-21 14:30:56 -07001197 /* Keep rate_correction_factor within limits */
John Koleszar0ea50ce2010-05-18 11:58:33 -04001198 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 {
Marco Paniconi464b1df2012-11-26 14:14:50 -08001206 if (cpi->oxcf.number_of_layers == 1 &&
1207 (cpi->common.refresh_alt_ref_frame ||
1208 cpi->common.refresh_golden_frame))
John Koleszar0ea50ce2010-05-18 11:58:33 -04001209 cpi->gf_rate_correction_factor = rate_correction_factor;
1210 else
1211 cpi->rate_correction_factor = rate_correction_factor;
1212 }
1213}
1214
John Koleszar0ea50ce2010-05-18 11:58:33 -04001215
1216int vp8_regulate_q(VP8_COMP *cpi, int target_bits_per_frame)
1217{
1218 int Q = cpi->active_worst_quality;
1219
Marco976f7f42015-04-28 08:29:48 -07001220 if (cpi->force_maxqp == 1) {
1221 cpi->active_worst_quality = cpi->worst_quality;
1222 return cpi->worst_quality;
1223 }
1224
John Koleszar0164a1c2012-05-21 14:30:56 -07001225 /* Reset Zbin OQ value */
Scott LaVarnway3a19eeb2012-12-10 10:51:42 -08001226 cpi->mb.zbin_over_quant = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001227
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 }
Marco Paniconi464b1df2012-11-26 14:14:50 -08001236 else if (cpi->oxcf.number_of_layers == 1 &&
1237 cpi->common.refresh_alt_ref_frame)
John Koleszar0ea50ce2010-05-18 11:58:33 -04001238 {
1239 Q = cpi->oxcf.alt_q;
1240 }
Marco Paniconi464b1df2012-11-26 14:14:50 -08001241 else if (cpi->oxcf.number_of_layers == 1 &&
1242 cpi->common.refresh_golden_frame)
John Koleszar0ea50ce2010-05-18 11:58:33 -04001243 {
1244 Q = cpi->oxcf.gold_q;
1245 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04001246 }
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
John Koleszar0164a1c2012-05-21 14:30:56 -07001255 /* Select the appropriate correction factor based upon type of frame. */
John Koleszar0ea50ce2010-05-18 11:58:33 -04001256 if (cpi->common.frame_type == KEY_FRAME)
1257 correction_factor = cpi->key_frame_rate_correction_factor;
1258 else
1259 {
Marco Paniconi464b1df2012-11-26 14:14:50 -08001260 if (cpi->oxcf.number_of_layers == 1 &&
1261 (cpi->common.refresh_alt_ref_frame ||
1262 cpi->common.refresh_golden_frame))
John Koleszar0ea50ce2010-05-18 11:58:33 -04001263 correction_factor = cpi->gf_rate_correction_factor;
1264 else
1265 correction_factor = cpi->rate_correction_factor;
1266 }
1267
John Koleszar0164a1c2012-05-21 14:30:56 -07001268 /* Calculate required scaling factor based on target frame size and
1269 * size of frame produced using previous Q
1270 */
John Koleszar0ea50ce2010-05-18 11:58:33 -04001271 if (target_bits_per_frame >= (INT_MAX >> BPER_MB_NORMBITS))
John Koleszar0164a1c2012-05-21 14:30:56 -07001272 /* Case where we would overflow int */
1273 target_bits_per_mb = (target_bits_per_frame / cpi->common.MBs) << BPER_MB_NORMBITS;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001274 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
John Koleszar0164a1c2012-05-21 14:30:56 -07001298 /* 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 */
John Koleszar0ea50ce2010-05-18 11:58:33 -04001302 if (Q >= MAXQ)
1303 {
1304 int zbin_oqmax;
1305
1306 double Factor = 0.99;
John Koleszar0164a1c2012-05-21 14:30:56 -07001307 double factor_adjustment = 0.01 / 256.0;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001308
1309 if (cpi->common.frame_type == KEY_FRAME)
John Koleszar0164a1c2012-05-21 14:30:56 -07001310 zbin_oqmax = 0;
Marco Paniconi464b1df2012-11-26 14:14:50 -08001311 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)))
John Koleszar0ea50ce2010-05-18 11:58:33 -04001315 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
John Koleszar0164a1c2012-05-21 14:30:56 -07001333 /* 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 */
Scott LaVarnway3a19eeb2012-12-10 10:51:42 -08001340 while (cpi->mb.zbin_over_quant < zbin_oqmax)
John Koleszar0ea50ce2010-05-18 11:58:33 -04001341 {
Scott LaVarnway3a19eeb2012-12-10 10:51:42 -08001342 cpi->mb.zbin_over_quant ++;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001343
Scott LaVarnway3a19eeb2012-12-10 10:51:42 -08001344 if (cpi->mb.zbin_over_quant > zbin_oqmax)
1345 cpi->mb.zbin_over_quant = zbin_oqmax;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001346
John Koleszar0164a1c2012-05-21 14:30:56 -07001347 /* Adjust bits_per_mb_at_this_q estimate */
Paul Wilkinsc012d632010-05-20 16:49:39 +01001348 bits_per_mb_at_this_q = (int)(Factor * bits_per_mb_at_this_q);
John Koleszar0ea50ce2010-05-18 11:58:33 -04001349 Factor += factor_adjustment;
1350
1351 if (Factor >= 0.999)
1352 Factor = 0.999;
1353
John Koleszar0164a1c2012-05-21 14:30:56 -07001354 /* Break out if we get down to the target rate */
1355 if (bits_per_mb_at_this_q <= target_bits_per_mb)
John Koleszar0ea50ce2010-05-18 11:58:33 -04001356 break;
1357 }
1358
1359 }
1360 }
1361
1362 return Q;
1363}
1364
John Koleszarad6a8ca2011-04-19 16:08:45 -04001365
1366static int estimate_keyframe_frequency(VP8_COMP *cpi)
John Koleszar0ea50ce2010-05-18 11:58:33 -04001367{
1368 int i;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001369
John Koleszar0164a1c2012-05-21 14:30:56 -07001370 /* Average key frame frequency */
John Koleszarad6a8ca2011-04-19 16:08:45 -04001371 int av_key_frame_frequency = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001372
John Koleszarad6a8ca2011-04-19 16:08:45 -04001373 /* First key frame at start of sequence is a special case. We have no
1374 * frequency data.
1375 */
John Koleszar0ea50ce2010-05-18 11:58:33 -04001376 if (cpi->key_frame_count == 1)
1377 {
John Koleszarad6a8ca2011-04-19 16:08:45 -04001378 /* Assume a default of 1 kf every 2 seconds, or the max kf interval,
1379 * whichever is smaller.
1380 */
James Berry8d5ce812011-04-22 11:54:18 -04001381 int key_freq = cpi->oxcf.key_freq>0 ? cpi->oxcf.key_freq : 1;
James Zern9581eb62013-07-12 17:12:46 -07001382 av_key_frame_frequency = 1 + (int)cpi->output_framerate * 2;
James Berry8d5ce812011-04-22 11:54:18 -04001383
1384 if (cpi->oxcf.auto_key && av_key_frame_frequency > key_freq)
Marco Paniconiec6cf492013-01-31 10:46:39 -08001385 av_key_frame_frequency = key_freq;
John Koleszarad6a8ca2011-04-19 16:08:45 -04001386
1387 cpi->prior_key_frame_distance[KEY_FRAME_CONTEXT - 1]
1388 = av_key_frame_frequency;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001389 }
1390 else
1391 {
John Koleszarad6a8ca2011-04-19 16:08:45 -04001392 unsigned int total_weight = 0;
Paul Wilkinsc239a1b2010-08-20 12:27:26 +01001393 int last_kf_interval =
1394 (cpi->frames_since_key > 0) ? cpi->frames_since_key : 1;
1395
John Koleszarad6a8ca2011-04-19 16:08:45 -04001396 /* reset keyframe context and calculate weighted average of last
1397 * KEY_FRAME_CONTEXT keyframes
1398 */
John Koleszar0ea50ce2010-05-18 11:58:33 -04001399 for (i = 0; i < KEY_FRAME_CONTEXT; i++)
1400 {
1401 if (i < KEY_FRAME_CONTEXT - 1)
John Koleszarad6a8ca2011-04-19 16:08:45 -04001402 cpi->prior_key_frame_distance[i]
1403 = cpi->prior_key_frame_distance[i+1];
John Koleszar0ea50ce2010-05-18 11:58:33 -04001404 else
Paul Wilkinsc239a1b2010-08-20 12:27:26 +01001405 cpi->prior_key_frame_distance[i] = last_kf_interval;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001406
John Koleszarad6a8ca2011-04-19 16:08:45 -04001407 av_key_frame_frequency += prior_key_frame_weight[i]
1408 * cpi->prior_key_frame_distance[i];
1409 total_weight += prior_key_frame_weight[i];
John Koleszar0ea50ce2010-05-18 11:58:33 -04001410 }
1411
John Koleszar0ea50ce2010-05-18 11:58:33 -04001412 av_key_frame_frequency /= total_weight;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001413
1414 }
Marco Paniconiec6cf492013-01-31 10:46:39 -08001415 // 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;
John Koleszarad6a8ca2011-04-19 16:08:45 -04001419 return av_key_frame_frequency;
1420}
1421
1422
1423void vp8_adjust_key_frame_context(VP8_COMP *cpi)
1424{
John Koleszar0164a1c2012-05-21 14:30:56 -07001425 /* Clear down mmx registers to allow floating point in what follows */
John Koleszarad6a8ca2011-04-19 16:08:45 -04001426 vp8_clear_system_state();
John Koleszar0ea50ce2010-05-18 11:58:33 -04001427
John Koleszar0164a1c2012-05-21 14:30:56 -07001428 /* Do we have any key frame overspend to recover? */
1429 /* Two-pass overspend handled elsewhere. */
John Koleszarad6a8ca2011-04-19 16:08:45 -04001430 if ((cpi->pass != 2)
1431 && (cpi->projected_frame_size > cpi->per_frame_bandwidth))
John Koleszar0ea50ce2010-05-18 11:58:33 -04001432 {
John Koleszarad6a8ca2011-04-19 16:08:45 -04001433 int overspend;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001434
John Koleszarad6a8ca2011-04-19 16:08:45 -04001435 /* 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);
Adrian Grange217591f2011-10-06 15:49:11 -07001442
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 }
John Koleszarad6a8ca2011-04-19 16:08:45 -04001450
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);
John Koleszar0ea50ce2010-05-18 11:58:33 -04001454 }
1455
1456 cpi->frames_since_key = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001457 cpi->key_frame_count++;
1458}
1459
John Koleszarfd6da3b2011-04-25 14:30:57 -04001460
John Koleszar0ea50ce2010-05-18 11:58:33 -04001461void vp8_compute_frame_size_bounds(VP8_COMP *cpi, int *frame_under_shoot_limit, int *frame_over_shoot_limit)
1462{
John Koleszar0164a1c2012-05-21 14:30:56 -07001463 /* Set-up bounds on acceptable frame size: */
John Koleszar0ea50ce2010-05-18 11:58:33 -04001464 if (cpi->oxcf.fixed_q >= 0)
1465 {
John Koleszar0164a1c2012-05-21 14:30:56 -07001466 /* Fixed Q scenario: frame size never outranges target
1467 * (there is no target!)
1468 */
John Koleszar0ea50ce2010-05-18 11:58:33 -04001469 *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 {
Adrian Grange217591f2011-10-06 15:49:11 -07001481 if (cpi->oxcf.number_of_layers > 1 ||
1482 cpi->common.refresh_alt_ref_frame ||
1483 cpi->common.refresh_golden_frame)
John Koleszar0ea50ce2010-05-18 11:58:33 -04001484 {
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 {
John Koleszar0164a1c2012-05-21 14:30:56 -07001490 /* For CBR take buffer fullness into account */
John Koleszar0ea50ce2010-05-18 11:58:33 -04001491 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 {
John Koleszar0164a1c2012-05-21 14:30:56 -07001495 /* Buffer is too full so relax overshoot and tighten
1496 * undershoot
1497 */
John Koleszar0ea50ce2010-05-18 11:58:33 -04001498 *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 {
John Koleszar0164a1c2012-05-21 14:30:56 -07001503 /* Buffer is too low so relax undershoot and tighten
1504 * overshoot
1505 */
John Koleszar0ea50ce2010-05-18 11:58:33 -04001506 *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 }
John Koleszar0164a1c2012-05-21 14:30:56 -07001515 /* VBR and CQ mode */
1516 /* Note that tighter restrictions here can help quality
1517 * but hurt encode speed
1518 */
John Koleszar0ea50ce2010-05-18 11:58:33 -04001519 else
1520 {
John Koleszar0164a1c2012-05-21 14:30:56 -07001521 /* Stron overshoot limit for constrained quality */
Paul Wilkinse0846c92011-01-07 18:29:37 +00001522 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 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04001532 }
1533 }
1534 }
Paul Wilkins6d843222012-02-29 12:32:46 -08001535
John Koleszar0164a1c2012-05-21 14:30:56 -07001536 /* 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 */
Paul Wilkins6d843222012-02-29 12:32:46 -08001540 *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
John Koleszar0ea50ce2010-05-18 11:58:33 -04001545 }
1546}
John Koleszar81d22062011-04-25 15:02:54 -04001547
1548
John Koleszar0164a1c2012-05-21 14:30:56 -07001549/* return of 0 means drop frame */
John Koleszar81d22062011-04-25 15:02:54 -04001550int vp8_pick_frame_size(VP8_COMP *cpi)
1551{
1552 VP8_COMMON *cm = &cpi->common;
1553
John Koleszardb5057c2011-04-26 16:45:30 -04001554 if (cm->frame_type == KEY_FRAME)
John Koleszar81d22062011-04-25 15:02:54 -04001555 calc_iframe_target_size(cpi);
John Koleszar81d22062011-04-25 15:02:54 -04001556 else
1557 {
John Koleszar81d22062011-04-25 15:02:54 -04001558 calc_pframe_target_size(cpi);
1559
John Koleszar0164a1c2012-05-21 14:30:56 -07001560 /* Check if we're dropping the frame: */
John Koleszar81d22062011-04-25 15:02:54 -04001561 if (cpi->drop_frame)
1562 {
John Koleszarf56918b2011-12-21 14:21:29 -08001563 cpi->drop_frame = 0;
John Koleszar81d22062011-04-25 15:02:54 -04001564 return 0;
1565 }
1566 }
John Koleszar81d22062011-04-25 15:02:54 -04001567 return 1;
1568}
Marco976f7f42015-04-28 08:29:48 -07001569// 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.
1575int 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);
Marco7ca17432015-05-21 14:07:21 -07001592 int pred_err_mb = (int)(cpi->mb.prediction_error / cpi->common.MBs);
Marco976f7f42015-04-28 08:29:48 -07001593 if (Q < thresh_qp &&
1594 cpi->projected_frame_size > thresh_rate &&
1595 pred_err_mb > thresh_pred_err_mb) {
Yaowu Xu7c514e22015-09-28 15:55:46 -07001596 double new_correction_factor = cpi->rate_correction_factor;
1597 const int target_size = cpi->av_per_frame_bandwidth;
1598 int target_bits_per_mb;
Marco976f7f42015-04-28 08:29:48 -07001599 // 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;
Yaowu Xu7c514e22015-09-28 15:55:46 -07001604 // 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;
Marco976f7f42015-04-28 08:29:48 -07001628 return 1;
1629 } else {
1630 cpi->force_maxqp = 0;
1631 return 0;
1632 }
Marco6f41e292015-05-20 12:20:17 -07001633 cpi->force_maxqp = 0;
Marco976f7f42015-04-28 08:29:48 -07001634 return 0;
1635 }
Marco6f41e292015-05-20 12:20:17 -07001636 cpi->force_maxqp = 0;
Marco976f7f42015-04-28 08:29:48 -07001637 return 0;
1638}