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