/src/libvpx/vp8/encoder/ratectrl.c
Line | Count | Source (jump to first uncovered line) |
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 | 34.6k | #define MIN_BPB_FACTOR 0.01 |
28 | 42.8k | #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 | 265k | #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 | 5.39k | #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 | 70.0k | void vp8_save_coding_context(VP8_COMP *cpi) { |
176 | 70.0k | 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 | 70.0k | cc->frames_since_key = cpi->frames_since_key; |
185 | 70.0k | cc->filter_level = cpi->common.filter_level; |
186 | 70.0k | cc->frames_till_gf_update_due = cpi->frames_till_gf_update_due; |
187 | 70.0k | cc->frames_since_golden = cpi->frames_since_golden; |
188 | | |
189 | 70.0k | vp8_copy(cc->mvc, cpi->common.fc.mvc); |
190 | 70.0k | vp8_copy(cc->mvcosts, cpi->rd_costs.mvcosts); |
191 | | |
192 | 70.0k | vp8_copy(cc->ymode_prob, cpi->common.fc.ymode_prob); |
193 | 70.0k | vp8_copy(cc->uv_mode_prob, cpi->common.fc.uv_mode_prob); |
194 | | |
195 | 70.0k | vp8_copy(cc->ymode_count, cpi->mb.ymode_count); |
196 | 70.0k | 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 | 70.0k | cc->this_frame_percent_intra = cpi->this_frame_percent_intra; |
209 | 70.0k | } |
210 | | |
211 | 18.0k | void vp8_restore_coding_context(VP8_COMP *cpi) { |
212 | 18.0k | 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 | 18.0k | cpi->frames_since_key = cc->frames_since_key; |
219 | 18.0k | cpi->common.filter_level = cc->filter_level; |
220 | 18.0k | cpi->frames_till_gf_update_due = cc->frames_till_gf_update_due; |
221 | 18.0k | cpi->frames_since_golden = cc->frames_since_golden; |
222 | | |
223 | 18.0k | vp8_copy(cpi->common.fc.mvc, cc->mvc); |
224 | | |
225 | 18.0k | vp8_copy(cpi->rd_costs.mvcosts, cc->mvcosts); |
226 | | |
227 | 18.0k | vp8_copy(cpi->common.fc.ymode_prob, cc->ymode_prob); |
228 | 18.0k | vp8_copy(cpi->common.fc.uv_mode_prob, cc->uv_mode_prob); |
229 | | |
230 | 18.0k | vp8_copy(cpi->mb.ymode_count, cc->ymode_count); |
231 | 18.0k | 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 | 18.0k | cpi->this_frame_percent_intra = cc->this_frame_percent_intra; |
244 | 18.0k | } |
245 | | |
246 | 18.6k | void vp8_setup_key_frame(VP8_COMP *cpi) { |
247 | | /* Setup for Key frame: */ |
248 | | |
249 | 18.6k | vp8_default_coef_probs(&cpi->common); |
250 | | |
251 | 18.6k | memcpy(cpi->common.fc.mvc, vp8_default_mv_context, |
252 | 18.6k | sizeof(vp8_default_mv_context)); |
253 | 18.6k | { |
254 | 18.6k | int flag[2] = { 1, 1 }; |
255 | 18.6k | vp8_build_component_cost_table( |
256 | 18.6k | cpi->mb.mvcost, (const MV_CONTEXT *)cpi->common.fc.mvc, flag); |
257 | 18.6k | } |
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 | 18.6k | memcpy(&cpi->lfc_a, &cpi->common.fc, sizeof(cpi->common.fc)); |
263 | 18.6k | memcpy(&cpi->lfc_g, &cpi->common.fc, sizeof(cpi->common.fc)); |
264 | 18.6k | memcpy(&cpi->lfc_n, &cpi->common.fc, sizeof(cpi->common.fc)); |
265 | | |
266 | 18.6k | cpi->common.filter_level = cpi->common.base_qindex * 3 / 8; |
267 | | |
268 | | /* Provisional interval before next GF */ |
269 | 18.6k | if (cpi->auto_gold) { |
270 | 18.6k | cpi->frames_till_gf_update_due = cpi->baseline_gf_interval; |
271 | 18.6k | } else { |
272 | 0 | cpi->frames_till_gf_update_due = DEFAULT_GF_INTERVAL; |
273 | 0 | } |
274 | | |
275 | 18.6k | cpi->common.refresh_golden_frame = 1; |
276 | 18.6k | cpi->common.refresh_alt_ref_frame = 1; |
277 | 18.6k | } |
278 | | |
279 | | static int estimate_bits_at_q(int frame_kind, int Q, int MBs, |
280 | 0 | 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 | 0 | } else { |
290 | 0 | return (Bpm * MBs) >> BPER_MB_NORMBITS; |
291 | 0 | } |
292 | 0 | } |
293 | | |
294 | 12.1k | static void calc_iframe_target_size(VP8_COMP *cpi) { |
295 | | /* boost defaults to half second */ |
296 | 12.1k | int kf_boost; |
297 | 12.1k | uint64_t target; |
298 | | |
299 | | /* Clear down mmx registers to allow floating point in what follows */ |
300 | 12.1k | vpx_clear_system_state(); |
301 | | |
302 | 12.1k | 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 | 0 | cpi->key_frame_rate_correction_factor); |
307 | 12.1k | } else if (cpi->pass == 2) { |
308 | | /* New Two pass RC */ |
309 | 0 | target = cpi->per_frame_bandwidth; |
310 | 0 | } |
311 | | /* First Frame is a special case */ |
312 | 12.1k | 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 | 4.69k | target = (uint64_t)cpi->oxcf.starting_buffer_level / 2; |
318 | | |
319 | 4.69k | if (target > cpi->oxcf.target_bandwidth * 3 / 2) { |
320 | 4.53k | target = cpi->oxcf.target_bandwidth * 3 / 2; |
321 | 4.53k | } |
322 | 7.49k | } else { |
323 | | /* if this keyframe was forced, use a more recent Q estimate */ |
324 | 7.49k | int Q = (cpi->common.frame_flags & FRAMEFLAGS_KEY) ? cpi->avg_frame_qindex |
325 | 7.49k | : cpi->ni_av_qi; |
326 | | |
327 | 7.49k | int initial_boost = 32; /* |3.0 * per_frame_bandwidth| */ |
328 | | /* Boost depends somewhat on frame rate: only used for 1 layer case. */ |
329 | 7.49k | if (cpi->oxcf.number_of_layers == 1) { |
330 | 7.49k | kf_boost = |
331 | 7.49k | VPXMAX(initial_boost, (int)round(2 * cpi->output_framerate - 16)); |
332 | | // cpi->output_framerate may be as large as 10M. Keep kf_boost small |
333 | | // enough to allow for integer math when multiplying by values in |
334 | | // kf_boost_qadjustment[]. |
335 | 7.49k | const int kMaxKfBoost = 2000; |
336 | 7.49k | if (kf_boost > kMaxKfBoost) kf_boost = kMaxKfBoost; |
337 | 7.49k | } else { |
338 | | /* Initial factor: set target size to: |3.0 * per_frame_bandwidth|. */ |
339 | 0 | kf_boost = initial_boost; |
340 | 0 | } |
341 | | |
342 | | /* adjustment up based on q: this factor ranges from ~1.2 to 2.2. */ |
343 | 7.49k | kf_boost = kf_boost * kf_boost_qadjustment[Q] / 100; |
344 | | |
345 | | /* frame separation adjustment ( down) */ |
346 | 7.49k | if (cpi->frames_since_key < cpi->output_framerate / 2) { |
347 | 5.65k | kf_boost = |
348 | 5.65k | (int)(kf_boost * cpi->frames_since_key / (cpi->output_framerate / 2)); |
349 | 5.65k | } |
350 | | |
351 | | /* Minimal target size is |2* per_frame_bandwidth|. */ |
352 | 7.49k | if (kf_boost < 16) kf_boost = 16; |
353 | | |
354 | 7.49k | target = ((uint64_t)(16 + kf_boost) * cpi->per_frame_bandwidth) >> 4; |
355 | 7.49k | target = VPXMIN(INT_MAX, target); |
356 | 7.49k | } |
357 | | |
358 | 12.1k | if (cpi->oxcf.rc_max_intra_bitrate_pct) { |
359 | 0 | unsigned int max_rate; |
360 | | // This product may overflow unsigned int |
361 | 0 | uint64_t product = cpi->per_frame_bandwidth; |
362 | 0 | product *= cpi->oxcf.rc_max_intra_bitrate_pct; |
363 | 0 | product /= 100; |
364 | 0 | max_rate = (unsigned int)VPXMIN(INT_MAX, product); |
365 | |
|
366 | 0 | if (target > max_rate) target = max_rate; |
367 | 0 | } |
368 | | |
369 | 12.1k | cpi->this_frame_target = (int)target; |
370 | | |
371 | | /* TODO: if we separate rate targeting from Q targeting, move this. |
372 | | * Reset the active worst quality to the baseline value for key frames. |
373 | | */ |
374 | 12.1k | if (cpi->pass != 2) cpi->active_worst_quality = cpi->worst_quality; |
375 | | |
376 | | #if 0 |
377 | | { |
378 | | FILE *f; |
379 | | |
380 | | f = fopen("kf_boost.stt", "a"); |
381 | | fprintf(f, " %8u %10d %10d %10d\n", |
382 | | cpi->common.current_video_frame, cpi->gfu_boost, cpi->baseline_gf_interval, cpi->source_alt_ref_pending); |
383 | | |
384 | | fclose(f); |
385 | | } |
386 | | #endif |
387 | 12.1k | } |
388 | | |
389 | | /* Do the best we can to define the parameters for the next GF based on what |
390 | | * information we have available. |
391 | | */ |
392 | 5.39k | static void calc_gf_params(VP8_COMP *cpi) { |
393 | 5.39k | int Q = |
394 | 5.39k | (cpi->oxcf.fixed_q < 0) ? cpi->last_q[INTER_FRAME] : cpi->oxcf.fixed_q; |
395 | 5.39k | int Boost = 0; |
396 | | |
397 | 5.39k | int gf_frame_usage = 0; /* Golden frame usage since last GF */ |
398 | 5.39k | int tot_mbs = cpi->recent_ref_frame_usage[INTRA_FRAME] + |
399 | 5.39k | cpi->recent_ref_frame_usage[LAST_FRAME] + |
400 | 5.39k | cpi->recent_ref_frame_usage[GOLDEN_FRAME] + |
401 | 5.39k | cpi->recent_ref_frame_usage[ALTREF_FRAME]; |
402 | | |
403 | 5.39k | int pct_gf_active = (100 * cpi->gf_active_count) / |
404 | 5.39k | (cpi->common.mb_rows * cpi->common.mb_cols); |
405 | | |
406 | 5.39k | if (tot_mbs) { |
407 | 5.39k | gf_frame_usage = (cpi->recent_ref_frame_usage[GOLDEN_FRAME] + |
408 | 5.39k | cpi->recent_ref_frame_usage[ALTREF_FRAME]) * |
409 | 5.39k | 100 / tot_mbs; |
410 | 5.39k | } |
411 | | |
412 | 5.39k | if (pct_gf_active > gf_frame_usage) gf_frame_usage = pct_gf_active; |
413 | | |
414 | | /* Not two pass */ |
415 | 5.39k | if (cpi->pass != 2) { |
416 | | /* Single Pass lagged mode: TBD */ |
417 | 5.39k | if (0) { |
418 | 0 | } |
419 | | |
420 | | /* Single Pass compression: Has to use current and historical data */ |
421 | 5.39k | else { |
422 | | #if 0 |
423 | | /* Experimental code */ |
424 | | int index = cpi->one_pass_frame_index; |
425 | | int frames_to_scan = (cpi->max_gf_interval <= MAX_LAG_BUFFERS) ? cpi->max_gf_interval : MAX_LAG_BUFFERS; |
426 | | |
427 | | /* ************** Experimental code - incomplete */ |
428 | | /* |
429 | | double decay_val = 1.0; |
430 | | double IIAccumulator = 0.0; |
431 | | double last_iiaccumulator = 0.0; |
432 | | double IIRatio; |
433 | | |
434 | | cpi->one_pass_frame_index = cpi->common.current_video_frame%MAX_LAG_BUFFERS; |
435 | | |
436 | | for ( i = 0; i < (frames_to_scan - 1); i++ ) |
437 | | { |
438 | | if ( index < 0 ) |
439 | | index = MAX_LAG_BUFFERS; |
440 | | index --; |
441 | | |
442 | | if ( cpi->one_pass_frame_stats[index].frame_coded_error > 0.0 ) |
443 | | { |
444 | | IIRatio = cpi->one_pass_frame_stats[index].frame_intra_error / cpi->one_pass_frame_stats[index].frame_coded_error; |
445 | | |
446 | | if ( IIRatio > 30.0 ) |
447 | | IIRatio = 30.0; |
448 | | } |
449 | | else |
450 | | IIRatio = 30.0; |
451 | | |
452 | | IIAccumulator += IIRatio * decay_val; |
453 | | |
454 | | decay_val = decay_val * cpi->one_pass_frame_stats[index].frame_pcnt_inter; |
455 | | |
456 | | if ( (i > MIN_GF_INTERVAL) && |
457 | | ((IIAccumulator - last_iiaccumulator) < 2.0) ) |
458 | | { |
459 | | break; |
460 | | } |
461 | | last_iiaccumulator = IIAccumulator; |
462 | | } |
463 | | |
464 | | Boost = IIAccumulator*100.0/16.0; |
465 | | cpi->baseline_gf_interval = i; |
466 | | |
467 | | */ |
468 | | #else |
469 | | |
470 | | /*************************************************************/ |
471 | | /* OLD code */ |
472 | | |
473 | | /* Adjust boost based upon ambient Q */ |
474 | 5.39k | Boost = GFQ_ADJUSTMENT; |
475 | | |
476 | | /* Adjust based upon most recently measure intra usage */ |
477 | 5.39k | Boost = Boost * |
478 | 5.39k | gf_intra_usage_adjustment[(cpi->this_frame_percent_intra < 15) |
479 | 5.39k | ? cpi->this_frame_percent_intra |
480 | 5.39k | : 14] / |
481 | 5.39k | 100; |
482 | | |
483 | | /* Adjust gf boost based upon GF usage since last GF */ |
484 | 5.39k | Boost = Boost * gf_adjust_table[gf_frame_usage] / 100; |
485 | 5.39k | #endif |
486 | 5.39k | } |
487 | | |
488 | | /* golden frame boost without recode loop often goes awry. be |
489 | | * safe by keeping numbers down. |
490 | | */ |
491 | 5.39k | if (!cpi->sf.recode_loop) { |
492 | 2.94k | if (cpi->compressor_speed == 2) Boost = Boost / 2; |
493 | 2.94k | } |
494 | | |
495 | | /* Apply an upper limit based on Q for 1 pass encodes */ |
496 | 5.39k | if (Boost > kf_gf_boost_qlimits[Q] && (cpi->pass == 0)) { |
497 | 1.99k | Boost = kf_gf_boost_qlimits[Q]; |
498 | | |
499 | | /* Apply lower limits to boost. */ |
500 | 3.39k | } else if (Boost < 110) { |
501 | 1.17k | Boost = 110; |
502 | 1.17k | } |
503 | | |
504 | | /* Note the boost used */ |
505 | 5.39k | cpi->last_boost = Boost; |
506 | 5.39k | } |
507 | | |
508 | | /* Estimate next interval |
509 | | * This is updated once the real frame size/boost is known. |
510 | | */ |
511 | 5.39k | if (cpi->oxcf.fixed_q == -1) { |
512 | 5.39k | if (cpi->pass == 2) { /* 2 Pass */ |
513 | 0 | cpi->frames_till_gf_update_due = cpi->baseline_gf_interval; |
514 | 5.39k | } else { /* 1 Pass */ |
515 | 5.39k | cpi->frames_till_gf_update_due = cpi->baseline_gf_interval; |
516 | | |
517 | 5.39k | if (cpi->last_boost > 750) cpi->frames_till_gf_update_due++; |
518 | | |
519 | 5.39k | if (cpi->last_boost > 1000) cpi->frames_till_gf_update_due++; |
520 | | |
521 | 5.39k | if (cpi->last_boost > 1250) cpi->frames_till_gf_update_due++; |
522 | | |
523 | 5.39k | if (cpi->last_boost >= 1500) cpi->frames_till_gf_update_due++; |
524 | | |
525 | 5.39k | if (gf_interval_table[gf_frame_usage] > cpi->frames_till_gf_update_due) { |
526 | 2.50k | cpi->frames_till_gf_update_due = gf_interval_table[gf_frame_usage]; |
527 | 2.50k | } |
528 | | |
529 | 5.39k | if (cpi->frames_till_gf_update_due > cpi->max_gf_interval) { |
530 | 34 | cpi->frames_till_gf_update_due = cpi->max_gf_interval; |
531 | 34 | } |
532 | 5.39k | } |
533 | 5.39k | } else { |
534 | 0 | cpi->frames_till_gf_update_due = cpi->baseline_gf_interval; |
535 | 0 | } |
536 | | |
537 | | /* ARF on or off */ |
538 | 5.39k | if (cpi->pass != 2) { |
539 | | /* For now Alt ref is not allowed except in 2 pass modes. */ |
540 | 5.39k | cpi->source_alt_ref_pending = 0; |
541 | | |
542 | | /*if ( cpi->oxcf.fixed_q == -1) |
543 | | { |
544 | | if ( cpi->oxcf.play_alternate && (cpi->last_boost > (100 + |
545 | | (AF_THRESH*cpi->frames_till_gf_update_due)) ) ) |
546 | | cpi->source_alt_ref_pending = 1; |
547 | | else |
548 | | cpi->source_alt_ref_pending = 0; |
549 | | }*/ |
550 | 5.39k | } |
551 | 5.39k | } |
552 | | |
553 | 60.9k | static void calc_pframe_target_size(VP8_COMP *cpi) { |
554 | 60.9k | int min_frame_target; |
555 | 60.9k | int old_per_frame_bandwidth = cpi->per_frame_bandwidth; |
556 | | |
557 | 60.9k | if (cpi->current_layer > 0) { |
558 | 0 | cpi->per_frame_bandwidth = |
559 | 0 | cpi->layer_context[cpi->current_layer].avg_frame_size_for_layer; |
560 | 0 | } |
561 | | |
562 | 60.9k | min_frame_target = 0; |
563 | | |
564 | 60.9k | if (cpi->pass == 2) { |
565 | 0 | min_frame_target = cpi->min_frame_bandwidth; |
566 | |
|
567 | 0 | if (min_frame_target < (cpi->av_per_frame_bandwidth >> 5)) { |
568 | 0 | min_frame_target = cpi->av_per_frame_bandwidth >> 5; |
569 | 0 | } |
570 | 60.9k | } else if (min_frame_target < cpi->per_frame_bandwidth / 4) { |
571 | 56.2k | min_frame_target = cpi->per_frame_bandwidth / 4; |
572 | 56.2k | } |
573 | | |
574 | | /* Special alt reference frame case */ |
575 | 60.9k | if ((cpi->common.refresh_alt_ref_frame) && |
576 | 60.9k | (cpi->oxcf.number_of_layers == 1)) { |
577 | 0 | if (cpi->pass == 2) { |
578 | | /* Per frame bit target for the alt ref frame */ |
579 | 0 | cpi->per_frame_bandwidth = cpi->twopass.gf_bits; |
580 | 0 | cpi->this_frame_target = cpi->per_frame_bandwidth; |
581 | 0 | } |
582 | | |
583 | | /* One Pass ??? TBD */ |
584 | 0 | } |
585 | | |
586 | | /* Normal frames (gf,and inter) */ |
587 | 60.9k | else { |
588 | | /* 2 pass */ |
589 | 60.9k | if (cpi->pass == 2) { |
590 | 0 | cpi->this_frame_target = cpi->per_frame_bandwidth; |
591 | 0 | } |
592 | | /* 1 pass */ |
593 | 60.9k | else { |
594 | 60.9k | int Adjustment; |
595 | | /* Make rate adjustment to recover bits spent in key frame |
596 | | * Test to see if the key frame inter data rate correction |
597 | | * should still be in force |
598 | | */ |
599 | 60.9k | if (cpi->kf_overspend_bits > 0) { |
600 | 32.3k | Adjustment = (cpi->kf_bitrate_adjustment <= cpi->kf_overspend_bits) |
601 | 32.3k | ? cpi->kf_bitrate_adjustment |
602 | 32.3k | : cpi->kf_overspend_bits; |
603 | | |
604 | 32.3k | if (Adjustment > (cpi->per_frame_bandwidth - min_frame_target)) { |
605 | 7.97k | Adjustment = (cpi->per_frame_bandwidth - min_frame_target); |
606 | 7.97k | } |
607 | | |
608 | 32.3k | cpi->kf_overspend_bits -= Adjustment; |
609 | | |
610 | | /* Calculate an inter frame bandwidth target for the next |
611 | | * few frames designed to recover any extra bits spent on |
612 | | * the key frame. |
613 | | */ |
614 | 32.3k | cpi->this_frame_target = cpi->per_frame_bandwidth - Adjustment; |
615 | | |
616 | 32.3k | if (cpi->this_frame_target < min_frame_target) { |
617 | 0 | cpi->this_frame_target = min_frame_target; |
618 | 0 | } |
619 | 32.3k | } else { |
620 | 28.5k | cpi->this_frame_target = cpi->per_frame_bandwidth; |
621 | 28.5k | } |
622 | | |
623 | | /* If appropriate make an adjustment to recover bits spent on a |
624 | | * recent GF |
625 | | */ |
626 | 60.9k | if ((cpi->gf_overspend_bits > 0) && |
627 | 60.9k | (cpi->this_frame_target > min_frame_target)) { |
628 | 24.9k | Adjustment = (cpi->non_gf_bitrate_adjustment <= cpi->gf_overspend_bits) |
629 | 24.9k | ? cpi->non_gf_bitrate_adjustment |
630 | 24.9k | : cpi->gf_overspend_bits; |
631 | | |
632 | 24.9k | if (Adjustment > (cpi->this_frame_target - min_frame_target)) { |
633 | 2.90k | Adjustment = (cpi->this_frame_target - min_frame_target); |
634 | 2.90k | } |
635 | | |
636 | 24.9k | cpi->gf_overspend_bits -= Adjustment; |
637 | 24.9k | cpi->this_frame_target -= Adjustment; |
638 | 24.9k | } |
639 | | |
640 | | /* Apply small + and - boosts for non gf frames */ |
641 | 60.9k | if ((cpi->last_boost > 150) && (cpi->frames_till_gf_update_due > 0) && |
642 | 60.9k | (cpi->current_gf_interval >= (MIN_GF_INTERVAL << 1))) { |
643 | | /* % Adjustment limited to the range 1% to 10% */ |
644 | 16.3k | Adjustment = (cpi->last_boost - 100) >> 5; |
645 | | |
646 | 16.3k | if (Adjustment < 1) { |
647 | 0 | Adjustment = 1; |
648 | 16.3k | } else if (Adjustment > 10) { |
649 | 5.09k | Adjustment = 10; |
650 | 5.09k | } |
651 | | |
652 | | /* Convert to bits */ |
653 | 16.3k | Adjustment = (cpi->this_frame_target * Adjustment) / 100; |
654 | | |
655 | 16.3k | if (Adjustment > (cpi->this_frame_target - min_frame_target)) { |
656 | 708 | Adjustment = (cpi->this_frame_target - min_frame_target); |
657 | 708 | } |
658 | | |
659 | 16.3k | if (cpi->frames_since_golden == (cpi->current_gf_interval >> 1)) { |
660 | 1.82k | Adjustment = (cpi->current_gf_interval - 1) * Adjustment; |
661 | | // Limit adjustment to 10% of current target. |
662 | 1.82k | if (Adjustment > (10 * cpi->this_frame_target) / 100) { |
663 | 1.47k | Adjustment = (10 * cpi->this_frame_target) / 100; |
664 | 1.47k | } |
665 | 1.82k | cpi->this_frame_target += Adjustment; |
666 | 14.5k | } else { |
667 | 14.5k | cpi->this_frame_target -= Adjustment; |
668 | 14.5k | } |
669 | 16.3k | } |
670 | 60.9k | } |
671 | 60.9k | } |
672 | | |
673 | | /* Sanity check that the total sum of adjustments is not above the |
674 | | * maximum allowed That is that having allowed for KF and GF penalties |
675 | | * we have not pushed the current interframe target to low. If the |
676 | | * adjustment we apply here is not capable of recovering all the extra |
677 | | * bits we have spent in the KF or GF then the remainder will have to |
678 | | * be recovered over a longer time span via other buffer / rate control |
679 | | * mechanisms. |
680 | | */ |
681 | 60.9k | if (cpi->this_frame_target < min_frame_target) { |
682 | 0 | cpi->this_frame_target = min_frame_target; |
683 | 0 | } |
684 | | |
685 | 60.9k | if (!cpi->common.refresh_alt_ref_frame) { |
686 | | /* Note the baseline target data rate for this inter frame. */ |
687 | 60.9k | cpi->inter_frame_target = cpi->this_frame_target; |
688 | 60.9k | } |
689 | | |
690 | | /* One Pass specific code */ |
691 | 60.9k | if (cpi->pass == 0) { |
692 | | /* Adapt target frame size with respect to any buffering constraints: */ |
693 | 60.9k | if (cpi->buffered_mode) { |
694 | 56.3k | int one_percent_bits = (int)(1 + cpi->oxcf.optimal_buffer_level / 100); |
695 | | |
696 | 56.3k | if ((cpi->buffer_level < cpi->oxcf.optimal_buffer_level) || |
697 | 56.3k | (cpi->bits_off_target < cpi->oxcf.optimal_buffer_level)) { |
698 | 34.4k | int percent_low = 0; |
699 | | |
700 | | /* Decide whether or not we need to adjust the frame data |
701 | | * rate target. |
702 | | * |
703 | | * If we are are below the optimal buffer fullness level |
704 | | * and adherence to buffering constraints is important to |
705 | | * the end usage then adjust the per frame target. |
706 | | */ |
707 | 34.4k | if ((cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) && |
708 | 34.4k | (cpi->buffer_level < cpi->oxcf.optimal_buffer_level)) { |
709 | 0 | percent_low = |
710 | 0 | (int)((cpi->oxcf.optimal_buffer_level - cpi->buffer_level) / |
711 | 0 | one_percent_bits); |
712 | 0 | } |
713 | | /* Are we overshooting the long term clip data rate... */ |
714 | 34.4k | else if (cpi->bits_off_target < 0) { |
715 | | /* Adjust per frame data target downwards to compensate. */ |
716 | 2.77k | percent_low = |
717 | 2.77k | (int)(100 * -cpi->bits_off_target / (cpi->total_byte_count * 8)); |
718 | 2.77k | } |
719 | | |
720 | 34.4k | if (percent_low > cpi->oxcf.under_shoot_pct) { |
721 | 0 | percent_low = cpi->oxcf.under_shoot_pct; |
722 | 34.4k | } else if (percent_low < 0) { |
723 | 0 | percent_low = 0; |
724 | 0 | } |
725 | | |
726 | | /* lower the target bandwidth for this frame. */ |
727 | 34.4k | cpi->this_frame_target -= |
728 | 34.4k | (int)(((int64_t)cpi->this_frame_target * percent_low) / 200); |
729 | | |
730 | | /* Are we using allowing control of active_worst_allowed_q |
731 | | * according to buffer level. |
732 | | */ |
733 | 34.4k | if (cpi->auto_worst_q && cpi->ni_frames > 150) { |
734 | 1.17k | int64_t critical_buffer_level; |
735 | | |
736 | | /* For streaming applications the most important factor is |
737 | | * cpi->buffer_level as this takes into account the |
738 | | * specified short term buffering constraints. However, |
739 | | * hitting the long term clip data rate target is also |
740 | | * important. |
741 | | */ |
742 | 1.17k | if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) { |
743 | | /* Take the smaller of cpi->buffer_level and |
744 | | * cpi->bits_off_target |
745 | | */ |
746 | 0 | critical_buffer_level = (cpi->buffer_level < cpi->bits_off_target) |
747 | 0 | ? cpi->buffer_level |
748 | 0 | : cpi->bits_off_target; |
749 | 0 | } |
750 | | /* For local file playback short term buffering constraints |
751 | | * are less of an issue |
752 | | */ |
753 | 1.17k | else { |
754 | | /* Consider only how we are doing for the clip as a |
755 | | * whole |
756 | | */ |
757 | 1.17k | critical_buffer_level = cpi->bits_off_target; |
758 | 1.17k | } |
759 | | |
760 | | /* Set the active worst quality based upon the selected |
761 | | * buffer fullness number. |
762 | | */ |
763 | 1.17k | if (critical_buffer_level < cpi->oxcf.optimal_buffer_level) { |
764 | 1.17k | if (critical_buffer_level > (cpi->oxcf.optimal_buffer_level >> 2)) { |
765 | 721 | int64_t qadjustment_range = cpi->worst_quality - cpi->ni_av_qi; |
766 | 721 | int64_t above_base = (critical_buffer_level - |
767 | 721 | (cpi->oxcf.optimal_buffer_level >> 2)); |
768 | | |
769 | | /* Step active worst quality down from |
770 | | * cpi->ni_av_qi when (critical_buffer_level == |
771 | | * cpi->optimal_buffer_level) to |
772 | | * cpi->worst_quality when |
773 | | * (critical_buffer_level == |
774 | | * cpi->optimal_buffer_level >> 2) |
775 | | */ |
776 | 721 | cpi->active_worst_quality = |
777 | 721 | cpi->worst_quality - |
778 | 721 | (int)((qadjustment_range * above_base) / |
779 | 721 | (cpi->oxcf.optimal_buffer_level * 3 >> 2)); |
780 | 721 | } else { |
781 | 458 | cpi->active_worst_quality = cpi->worst_quality; |
782 | 458 | } |
783 | 1.17k | } else { |
784 | 0 | cpi->active_worst_quality = cpi->ni_av_qi; |
785 | 0 | } |
786 | 33.3k | } else { |
787 | 33.3k | cpi->active_worst_quality = cpi->worst_quality; |
788 | 33.3k | } |
789 | 34.4k | } else { |
790 | 21.8k | int percent_high = 0; |
791 | 21.8k | int64_t target = cpi->this_frame_target; |
792 | | |
793 | 21.8k | if ((cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) && |
794 | 21.8k | (cpi->buffer_level > cpi->oxcf.optimal_buffer_level)) { |
795 | 0 | percent_high = |
796 | 0 | (int)((cpi->buffer_level - cpi->oxcf.optimal_buffer_level) / |
797 | 0 | one_percent_bits); |
798 | 21.8k | } else if (cpi->bits_off_target > cpi->oxcf.optimal_buffer_level) { |
799 | 21.3k | if (cpi->total_byte_count > 0) { |
800 | 21.3k | percent_high = (int)((100 * cpi->bits_off_target) / |
801 | 21.3k | (cpi->total_byte_count * 8)); |
802 | 21.3k | } else { |
803 | 0 | percent_high = cpi->oxcf.over_shoot_pct; |
804 | 0 | } |
805 | 21.3k | } |
806 | | |
807 | 21.8k | if (percent_high > cpi->oxcf.over_shoot_pct) { |
808 | 20.8k | percent_high = cpi->oxcf.over_shoot_pct; |
809 | 20.8k | } else if (percent_high < 0) { |
810 | 0 | percent_high = 0; |
811 | 0 | } |
812 | | |
813 | 21.8k | target += (target * percent_high) / 200; |
814 | 21.8k | target = VPXMIN(target, INT_MAX); |
815 | 21.8k | cpi->this_frame_target = (int)target; |
816 | | |
817 | | /* Are we allowing control of active_worst_allowed_q according |
818 | | * to buffer level. |
819 | | */ |
820 | 21.8k | if (cpi->auto_worst_q && cpi->ni_frames > 150) { |
821 | | /* When using the relaxed buffer model stick to the |
822 | | * user specified value |
823 | | */ |
824 | 718 | cpi->active_worst_quality = cpi->ni_av_qi; |
825 | 21.1k | } else { |
826 | 21.1k | cpi->active_worst_quality = cpi->worst_quality; |
827 | 21.1k | } |
828 | 21.8k | } |
829 | | |
830 | | /* Set active_best_quality to prevent quality rising too high */ |
831 | 56.3k | cpi->active_best_quality = cpi->best_quality; |
832 | | |
833 | | /* Worst quality obviously must not be better than best quality */ |
834 | 56.3k | if (cpi->active_worst_quality <= cpi->active_best_quality) { |
835 | 353 | cpi->active_worst_quality = cpi->active_best_quality + 1; |
836 | 353 | } |
837 | | |
838 | 56.3k | if (cpi->active_worst_quality > 127) cpi->active_worst_quality = 127; |
839 | 56.3k | } |
840 | | /* Unbuffered mode (eg. video conferencing) */ |
841 | 4.58k | else { |
842 | | /* Set the active worst quality */ |
843 | 4.58k | cpi->active_worst_quality = cpi->worst_quality; |
844 | 4.58k | } |
845 | | |
846 | | /* Special trap for constrained quality mode |
847 | | * "active_worst_quality" may never drop below cq level |
848 | | * for any frame type. |
849 | | */ |
850 | 60.9k | if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY && |
851 | 60.9k | cpi->active_worst_quality < cpi->cq_target_quality) { |
852 | 0 | cpi->active_worst_quality = cpi->cq_target_quality; |
853 | 0 | } |
854 | 60.9k | } |
855 | | |
856 | | /* Test to see if we have to drop a frame |
857 | | * The auto-drop frame code is only used in buffered mode. |
858 | | * In unbufferd mode (eg vide conferencing) the descision to |
859 | | * code or drop a frame is made outside the codec in response to real |
860 | | * world comms or buffer considerations. |
861 | | */ |
862 | 60.9k | if (cpi->drop_frames_allowed && |
863 | 60.9k | (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) && |
864 | 60.9k | ((cpi->common.frame_type != KEY_FRAME))) { |
865 | | /* Check for a buffer underun-crisis in which case we have to drop |
866 | | * a frame |
867 | | */ |
868 | 0 | if ((cpi->buffer_level < 0)) { |
869 | | #if 0 |
870 | | FILE *f = fopen("dec.stt", "a"); |
871 | | fprintf(f, "%10d %10d %10d %10d ***** BUFFER EMPTY\n", |
872 | | (int) cpi->common.current_video_frame, |
873 | | cpi->decimation_factor, cpi->common.horiz_scale, |
874 | | (cpi->buffer_level * 100) / cpi->oxcf.optimal_buffer_level); |
875 | | fclose(f); |
876 | | #endif |
877 | 0 | cpi->drop_frame = 1; |
878 | | |
879 | | /* Update the buffer level variable. */ |
880 | 0 | cpi->bits_off_target += cpi->av_per_frame_bandwidth; |
881 | 0 | if (cpi->bits_off_target > cpi->oxcf.maximum_buffer_size) { |
882 | 0 | cpi->bits_off_target = (int)cpi->oxcf.maximum_buffer_size; |
883 | 0 | } |
884 | 0 | cpi->buffer_level = cpi->bits_off_target; |
885 | |
|
886 | 0 | if (cpi->oxcf.number_of_layers > 1) { |
887 | 0 | unsigned int i; |
888 | | |
889 | | // Propagate bits saved by dropping the frame to higher layers. |
890 | 0 | for (i = cpi->current_layer + 1; i < cpi->oxcf.number_of_layers; ++i) { |
891 | 0 | LAYER_CONTEXT *lc = &cpi->layer_context[i]; |
892 | 0 | lc->bits_off_target += (int)(lc->target_bandwidth / lc->framerate); |
893 | 0 | if (lc->bits_off_target > lc->maximum_buffer_size) { |
894 | 0 | lc->bits_off_target = lc->maximum_buffer_size; |
895 | 0 | } |
896 | 0 | lc->buffer_level = lc->bits_off_target; |
897 | 0 | } |
898 | 0 | } |
899 | 0 | } |
900 | 0 | } |
901 | | |
902 | | /* Adjust target frame size for Golden Frames: */ |
903 | 60.9k | if (cpi->oxcf.error_resilient_mode == 0 && |
904 | 60.9k | (cpi->frames_till_gf_update_due == 0) && !cpi->drop_frame) { |
905 | 6.47k | if (!cpi->gf_update_onepass_cbr) { |
906 | 6.47k | int Q = (cpi->oxcf.fixed_q < 0) ? cpi->last_q[INTER_FRAME] |
907 | 6.47k | : cpi->oxcf.fixed_q; |
908 | | |
909 | 6.47k | int gf_frame_usage = 0; /* Golden frame usage since last GF */ |
910 | 6.47k | int tot_mbs = cpi->recent_ref_frame_usage[INTRA_FRAME] + |
911 | 6.47k | cpi->recent_ref_frame_usage[LAST_FRAME] + |
912 | 6.47k | cpi->recent_ref_frame_usage[GOLDEN_FRAME] + |
913 | 6.47k | cpi->recent_ref_frame_usage[ALTREF_FRAME]; |
914 | | |
915 | 6.47k | int pct_gf_active = (100 * cpi->gf_active_count) / |
916 | 6.47k | (cpi->common.mb_rows * cpi->common.mb_cols); |
917 | | |
918 | 6.47k | if (tot_mbs) { |
919 | 6.47k | gf_frame_usage = (cpi->recent_ref_frame_usage[GOLDEN_FRAME] + |
920 | 6.47k | cpi->recent_ref_frame_usage[ALTREF_FRAME]) * |
921 | 6.47k | 100 / tot_mbs; |
922 | 6.47k | } |
923 | | |
924 | 6.47k | if (pct_gf_active > gf_frame_usage) gf_frame_usage = pct_gf_active; |
925 | | |
926 | | /* Is a fixed manual GF frequency being used */ |
927 | 6.47k | if (cpi->auto_gold) { |
928 | | /* For one pass throw a GF if recent frame intra usage is |
929 | | * low or the GF usage is high |
930 | | */ |
931 | 6.47k | if ((cpi->pass == 0) && |
932 | 6.47k | (cpi->this_frame_percent_intra < 15 || gf_frame_usage >= 5)) { |
933 | 5.39k | cpi->common.refresh_golden_frame = 1; |
934 | | |
935 | | /* Two pass GF descision */ |
936 | 5.39k | } else if (cpi->pass == 2) { |
937 | 0 | cpi->common.refresh_golden_frame = 1; |
938 | 0 | } |
939 | 6.47k | } |
940 | | |
941 | | #if 0 |
942 | | |
943 | | /* Debug stats */ |
944 | | if (0) { |
945 | | FILE *f; |
946 | | |
947 | | f = fopen("gf_usaget.stt", "a"); |
948 | | fprintf(f, " %8ld %10ld %10ld %10ld %10ld\n", |
949 | | cpi->common.current_video_frame, cpi->gfu_boost, |
950 | | GFQ_ADJUSTMENT, cpi->gfu_boost, gf_frame_usage); |
951 | | fclose(f); |
952 | | } |
953 | | |
954 | | #endif |
955 | | |
956 | 6.47k | if (cpi->common.refresh_golden_frame == 1) { |
957 | | #if 0 |
958 | | |
959 | | if (0) { |
960 | | FILE *f; |
961 | | |
962 | | f = fopen("GFexit.stt", "a"); |
963 | | fprintf(f, "%8ld GF coded\n", cpi->common.current_video_frame); |
964 | | fclose(f); |
965 | | } |
966 | | |
967 | | #endif |
968 | | |
969 | 5.39k | if (cpi->auto_adjust_gold_quantizer) { |
970 | 5.39k | calc_gf_params(cpi); |
971 | 5.39k | } |
972 | | |
973 | | /* If we are using alternate ref instead of gf then do not apply the |
974 | | * boost It will instead be applied to the altref update Jims |
975 | | * modified boost |
976 | | */ |
977 | 5.39k | if (!cpi->source_alt_ref_active) { |
978 | 5.39k | if (cpi->oxcf.fixed_q < 0) { |
979 | 5.39k | if (cpi->pass == 2) { |
980 | | /* The spend on the GF is defined in the two pass |
981 | | * code for two pass encodes |
982 | | */ |
983 | 0 | cpi->this_frame_target = cpi->per_frame_bandwidth; |
984 | 5.39k | } else { |
985 | 5.39k | int Boost = cpi->last_boost; |
986 | 5.39k | int frames_in_section = cpi->frames_till_gf_update_due + 1; |
987 | 5.39k | int allocation_chunks = (frames_in_section * 100) + (Boost - 100); |
988 | 5.39k | int bits_in_section = cpi->inter_frame_target * frames_in_section; |
989 | | |
990 | | /* Normalize Altboost and allocations chunck down to |
991 | | * prevent overflow |
992 | | */ |
993 | 5.39k | while (Boost > 1000) { |
994 | 0 | Boost /= 2; |
995 | 0 | allocation_chunks /= 2; |
996 | 0 | } |
997 | | |
998 | | /* Avoid loss of precision but avoid overflow */ |
999 | 5.39k | if ((bits_in_section >> 7) > allocation_chunks) { |
1000 | 1.36k | cpi->this_frame_target = |
1001 | 1.36k | Boost * (bits_in_section / allocation_chunks); |
1002 | 4.02k | } else { |
1003 | 4.02k | cpi->this_frame_target = |
1004 | 4.02k | (Boost * bits_in_section) / allocation_chunks; |
1005 | 4.02k | } |
1006 | 5.39k | } |
1007 | 5.39k | } else { |
1008 | 0 | cpi->this_frame_target = |
1009 | 0 | (estimate_bits_at_q(1, Q, cpi->common.MBs, 1.0) * |
1010 | 0 | cpi->last_boost) / |
1011 | 0 | 100; |
1012 | 0 | } |
1013 | 5.39k | } else { |
1014 | | /* If there is an active ARF at this location use the minimum |
1015 | | * bits on this frame even if it is a contructed arf. |
1016 | | * The active maximum quantizer insures that an appropriate |
1017 | | * number of bits will be spent if needed for contstructed ARFs. |
1018 | | */ |
1019 | 0 | cpi->this_frame_target = 0; |
1020 | 0 | } |
1021 | | |
1022 | 5.39k | cpi->current_gf_interval = cpi->frames_till_gf_update_due; |
1023 | 5.39k | } |
1024 | 6.47k | } else { |
1025 | | // Special case for 1 pass CBR: fixed gf period. |
1026 | | // TODO(marpan): Adjust this boost/interval logic. |
1027 | | // If gf_cbr_boost_pct is small (below threshold) set the flag |
1028 | | // gf_noboost_onepass_cbr = 1, which forces the gf to use the same |
1029 | | // rate correction factor as last. |
1030 | 0 | cpi->gf_noboost_onepass_cbr = (cpi->oxcf.gf_cbr_boost_pct <= 100); |
1031 | 0 | cpi->baseline_gf_interval = cpi->gf_interval_onepass_cbr; |
1032 | | // Skip this update if the zero_mvcount is low. |
1033 | 0 | if (cpi->zeromv_count > (cpi->common.MBs >> 1)) { |
1034 | 0 | cpi->common.refresh_golden_frame = 1; |
1035 | 0 | cpi->this_frame_target = |
1036 | 0 | (cpi->this_frame_target * (100 + cpi->oxcf.gf_cbr_boost_pct)) / 100; |
1037 | 0 | } |
1038 | 0 | cpi->frames_till_gf_update_due = cpi->baseline_gf_interval; |
1039 | 0 | cpi->current_gf_interval = cpi->frames_till_gf_update_due; |
1040 | 0 | } |
1041 | 6.47k | } |
1042 | | |
1043 | 60.9k | cpi->per_frame_bandwidth = old_per_frame_bandwidth; |
1044 | 60.9k | } |
1045 | | |
1046 | 86.1k | void vp8_update_rate_correction_factors(VP8_COMP *cpi, int damp_var) { |
1047 | 86.1k | int Q = cpi->common.base_qindex; |
1048 | 86.1k | int correction_factor = 100; |
1049 | 86.1k | double rate_correction_factor; |
1050 | 86.1k | double adjustment_limit; |
1051 | | |
1052 | 86.1k | int projected_size_based_on_q = 0; |
1053 | | |
1054 | | /* Clear down mmx registers to allow floating point in what follows */ |
1055 | 86.1k | vpx_clear_system_state(); |
1056 | | |
1057 | 86.1k | if (cpi->common.frame_type == KEY_FRAME) { |
1058 | 18.9k | rate_correction_factor = cpi->key_frame_rate_correction_factor; |
1059 | 67.1k | } else { |
1060 | 67.1k | if (cpi->oxcf.number_of_layers == 1 && !cpi->gf_noboost_onepass_cbr && |
1061 | 67.1k | (cpi->common.refresh_alt_ref_frame || |
1062 | 67.1k | cpi->common.refresh_golden_frame)) { |
1063 | 6.83k | rate_correction_factor = cpi->gf_rate_correction_factor; |
1064 | 60.3k | } else { |
1065 | 60.3k | rate_correction_factor = cpi->rate_correction_factor; |
1066 | 60.3k | } |
1067 | 67.1k | } |
1068 | | |
1069 | | /* Work out how big we would have expected the frame to be at this Q |
1070 | | * given the current correction factor. Stay in double to avoid int |
1071 | | * overflow when values are large |
1072 | | */ |
1073 | 86.1k | projected_size_based_on_q = |
1074 | 86.1k | (int)(((.5 + rate_correction_factor * |
1075 | 86.1k | vp8_bits_per_mb[cpi->common.frame_type][Q]) * |
1076 | 86.1k | cpi->common.MBs) / |
1077 | 86.1k | (1 << BPER_MB_NORMBITS)); |
1078 | | |
1079 | | /* Make some allowance for cpi->zbin_over_quant */ |
1080 | 86.1k | if (cpi->mb.zbin_over_quant > 0) { |
1081 | 21.1k | int Z = cpi->mb.zbin_over_quant; |
1082 | 21.1k | double Factor = 0.99; |
1083 | 21.1k | double factor_adjustment = 0.01 / 256.0; |
1084 | | |
1085 | 2.79M | while (Z > 0) { |
1086 | 2.77M | Z--; |
1087 | 2.77M | projected_size_based_on_q = (int)(Factor * projected_size_based_on_q); |
1088 | 2.77M | Factor += factor_adjustment; |
1089 | | |
1090 | 2.77M | if (Factor >= 0.999) Factor = 0.999; |
1091 | 2.77M | } |
1092 | 21.1k | } |
1093 | | |
1094 | | /* Work out a size correction factor. */ |
1095 | 86.1k | if (projected_size_based_on_q > 0) { |
1096 | 80.5k | correction_factor = (int)((100 * (int64_t)cpi->projected_frame_size) / |
1097 | 80.5k | projected_size_based_on_q); |
1098 | 80.5k | } |
1099 | | |
1100 | | /* More heavily damped adjustment used if we have been oscillating |
1101 | | * either side of target |
1102 | | */ |
1103 | 86.1k | switch (damp_var) { |
1104 | 15.1k | case 0: adjustment_limit = 0.75; break; |
1105 | 1.22k | case 1: adjustment_limit = 0.375; break; |
1106 | 69.7k | case 2: |
1107 | 69.7k | default: adjustment_limit = 0.25; break; |
1108 | 86.1k | } |
1109 | | |
1110 | 86.1k | if (correction_factor > 102) { |
1111 | | /* We are not already at the worst allowable quality */ |
1112 | 41.5k | correction_factor = |
1113 | 41.5k | (int)(100.5 + ((correction_factor - 100) * adjustment_limit)); |
1114 | 41.5k | rate_correction_factor = |
1115 | 41.5k | ((rate_correction_factor * correction_factor) / 100); |
1116 | | |
1117 | | /* Keep rate_correction_factor within limits */ |
1118 | 41.5k | if (rate_correction_factor > MAX_BPB_FACTOR) { |
1119 | 1.24k | rate_correction_factor = MAX_BPB_FACTOR; |
1120 | 1.24k | } |
1121 | 44.6k | } else if (correction_factor < 99) { |
1122 | | /* We are not already at the best allowable quality */ |
1123 | 34.4k | correction_factor = |
1124 | 34.4k | (int)(100.5 - ((100 - correction_factor) * adjustment_limit)); |
1125 | 34.4k | rate_correction_factor = |
1126 | 34.4k | ((rate_correction_factor * correction_factor) / 100); |
1127 | | |
1128 | | /* Keep rate_correction_factor within limits */ |
1129 | 34.4k | if (rate_correction_factor < MIN_BPB_FACTOR) { |
1130 | 258 | rate_correction_factor = MIN_BPB_FACTOR; |
1131 | 258 | } |
1132 | 34.4k | } |
1133 | | |
1134 | 86.1k | if (cpi->common.frame_type == KEY_FRAME) { |
1135 | 18.9k | cpi->key_frame_rate_correction_factor = rate_correction_factor; |
1136 | 67.1k | } else { |
1137 | 67.1k | if (cpi->oxcf.number_of_layers == 1 && !cpi->gf_noboost_onepass_cbr && |
1138 | 67.1k | (cpi->common.refresh_alt_ref_frame || |
1139 | 67.1k | cpi->common.refresh_golden_frame)) { |
1140 | 6.83k | cpi->gf_rate_correction_factor = rate_correction_factor; |
1141 | 60.3k | } else { |
1142 | 60.3k | cpi->rate_correction_factor = rate_correction_factor; |
1143 | 60.3k | } |
1144 | 67.1k | } |
1145 | 86.1k | } |
1146 | | |
1147 | 0 | static int limit_q_cbr_inter(int last_q, int current_q) { |
1148 | 0 | int limit_down = 12; |
1149 | 0 | if (last_q - current_q > limit_down) |
1150 | 0 | return (last_q - limit_down); |
1151 | 0 | else |
1152 | 0 | return current_q; |
1153 | 0 | } |
1154 | | |
1155 | 88.4k | int vp8_regulate_q(VP8_COMP *cpi, int target_bits_per_frame) { |
1156 | 88.4k | int Q = cpi->active_worst_quality; |
1157 | | |
1158 | 88.4k | if (cpi->force_maxqp == 1) { |
1159 | 0 | cpi->active_worst_quality = cpi->worst_quality; |
1160 | 0 | return cpi->worst_quality; |
1161 | 0 | } |
1162 | | /* Reset Zbin OQ value */ |
1163 | 88.4k | cpi->mb.zbin_over_quant = 0; |
1164 | | |
1165 | 88.4k | if (cpi->oxcf.fixed_q >= 0) { |
1166 | 0 | Q = cpi->oxcf.fixed_q; |
1167 | |
|
1168 | 0 | if (cpi->common.frame_type == KEY_FRAME) { |
1169 | 0 | Q = cpi->oxcf.key_q; |
1170 | 0 | } else if (cpi->oxcf.number_of_layers == 1 && |
1171 | 0 | cpi->common.refresh_alt_ref_frame && |
1172 | 0 | !cpi->gf_noboost_onepass_cbr) { |
1173 | 0 | Q = cpi->oxcf.alt_q; |
1174 | 0 | } else if (cpi->oxcf.number_of_layers == 1 && |
1175 | 0 | cpi->common.refresh_golden_frame && |
1176 | 0 | !cpi->gf_noboost_onepass_cbr) { |
1177 | 0 | Q = cpi->oxcf.gold_q; |
1178 | 0 | } |
1179 | 88.4k | } else { |
1180 | 88.4k | int i; |
1181 | 88.4k | int last_error = INT_MAX; |
1182 | 88.4k | int target_bits_per_mb; |
1183 | 88.4k | int bits_per_mb_at_this_q; |
1184 | 88.4k | double correction_factor; |
1185 | | |
1186 | | /* Select the appropriate correction factor based upon type of frame. */ |
1187 | 88.4k | if (cpi->common.frame_type == KEY_FRAME) { |
1188 | 18.2k | correction_factor = cpi->key_frame_rate_correction_factor; |
1189 | 70.2k | } else { |
1190 | 70.2k | if (cpi->oxcf.number_of_layers == 1 && !cpi->gf_noboost_onepass_cbr && |
1191 | 70.2k | (cpi->common.refresh_alt_ref_frame || |
1192 | 70.2k | cpi->common.refresh_golden_frame)) { |
1193 | 7.06k | correction_factor = cpi->gf_rate_correction_factor; |
1194 | 63.1k | } else { |
1195 | 63.1k | correction_factor = cpi->rate_correction_factor; |
1196 | 63.1k | } |
1197 | 70.2k | } |
1198 | | |
1199 | | /* Calculate required scaling factor based on target frame size and |
1200 | | * size of frame produced using previous Q |
1201 | | */ |
1202 | 88.4k | if (target_bits_per_frame > (INT_MAX >> BPER_MB_NORMBITS)) { |
1203 | 3.60k | int temp = target_bits_per_frame / cpi->common.MBs; |
1204 | 3.60k | if (temp > (INT_MAX >> BPER_MB_NORMBITS)) { |
1205 | 977 | target_bits_per_mb = INT_MAX; |
1206 | 2.62k | } else { |
1207 | 2.62k | target_bits_per_mb = temp << BPER_MB_NORMBITS; |
1208 | 2.62k | } |
1209 | 84.8k | } else { |
1210 | 84.8k | target_bits_per_mb = |
1211 | 84.8k | (target_bits_per_frame << BPER_MB_NORMBITS) / cpi->common.MBs; |
1212 | 84.8k | } |
1213 | | |
1214 | 88.4k | i = cpi->active_best_quality; |
1215 | | |
1216 | 4.10M | do { |
1217 | 4.10M | bits_per_mb_at_this_q = |
1218 | 4.10M | (int)(.5 + |
1219 | 4.10M | correction_factor * vp8_bits_per_mb[cpi->common.frame_type][i]); |
1220 | | |
1221 | 4.10M | if (bits_per_mb_at_this_q <= target_bits_per_mb) { |
1222 | 64.0k | if ((target_bits_per_mb - bits_per_mb_at_this_q) <= last_error) { |
1223 | 50.8k | Q = i; |
1224 | 50.8k | } else { |
1225 | 13.1k | Q = i - 1; |
1226 | 13.1k | } |
1227 | | |
1228 | 64.0k | break; |
1229 | 4.04M | } else { |
1230 | 4.04M | last_error = bits_per_mb_at_this_q - target_bits_per_mb; |
1231 | 4.04M | } |
1232 | 4.10M | } while (++i <= cpi->active_worst_quality); |
1233 | | |
1234 | | /* If we are at MAXQ then enable Q over-run which seeks to claw |
1235 | | * back additional bits through things like the RD multiplier |
1236 | | * and zero bin size. |
1237 | | */ |
1238 | 88.4k | if (Q >= MAXQ) { |
1239 | 24.0k | int zbin_oqmax; |
1240 | | |
1241 | 24.0k | double Factor = 0.99; |
1242 | 24.0k | double factor_adjustment = 0.01 / 256.0; |
1243 | | |
1244 | 24.0k | if (cpi->common.frame_type == KEY_FRAME) { |
1245 | 1.53k | zbin_oqmax = 0; |
1246 | 22.4k | } else if (cpi->oxcf.number_of_layers == 1 && |
1247 | 22.4k | !cpi->gf_noboost_onepass_cbr && |
1248 | 22.4k | (cpi->common.refresh_alt_ref_frame || |
1249 | 22.4k | (cpi->common.refresh_golden_frame && |
1250 | 22.4k | !cpi->source_alt_ref_active))) { |
1251 | 938 | zbin_oqmax = 16; |
1252 | 21.5k | } else { |
1253 | 21.5k | zbin_oqmax = ZBIN_OQ_MAX; |
1254 | 21.5k | } |
1255 | | |
1256 | | /*{ |
1257 | | double Factor = |
1258 | | (double)target_bits_per_mb/(double)bits_per_mb_at_this_q; |
1259 | | double Oq; |
1260 | | |
1261 | | Factor = Factor/1.2683; |
1262 | | |
1263 | | Oq = pow( Factor, (1.0/-0.165) ); |
1264 | | |
1265 | | if ( Oq > zbin_oqmax ) |
1266 | | Oq = zbin_oqmax; |
1267 | | |
1268 | | cpi->zbin_over_quant = (int)Oq; |
1269 | | }*/ |
1270 | | |
1271 | | /* Each incrment in the zbin is assumed to have a fixed effect |
1272 | | * on bitrate. This is not of course true. The effect will be |
1273 | | * highly clip dependent and may well have sudden steps. The |
1274 | | * idea here is to acheive higher effective quantizers than the |
1275 | | * normal maximum by expanding the zero bin and hence |
1276 | | * decreasing the number of low magnitude non zero coefficients. |
1277 | | */ |
1278 | 2.95M | while (cpi->mb.zbin_over_quant < zbin_oqmax) { |
1279 | 2.94M | cpi->mb.zbin_over_quant++; |
1280 | | |
1281 | 2.94M | if (cpi->mb.zbin_over_quant > zbin_oqmax) { |
1282 | 0 | cpi->mb.zbin_over_quant = zbin_oqmax; |
1283 | 0 | } |
1284 | | |
1285 | | /* Adjust bits_per_mb_at_this_q estimate */ |
1286 | 2.94M | bits_per_mb_at_this_q = (int)(Factor * bits_per_mb_at_this_q); |
1287 | 2.94M | Factor += factor_adjustment; |
1288 | | |
1289 | 2.94M | if (Factor >= 0.999) Factor = 0.999; |
1290 | | |
1291 | | /* Break out if we get down to the target rate */ |
1292 | 2.94M | if (bits_per_mb_at_this_q <= target_bits_per_mb) break; |
1293 | 2.94M | } |
1294 | 24.0k | } |
1295 | 88.4k | } |
1296 | | |
1297 | | // Limit decrease in Q for 1 pass CBR screen content mode. |
1298 | 88.4k | if (cpi->common.frame_type != KEY_FRAME && cpi->pass == 0 && |
1299 | 88.4k | cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER && |
1300 | 88.4k | cpi->oxcf.screen_content_mode) |
1301 | 0 | Q = limit_q_cbr_inter(cpi->last_q[1], Q); |
1302 | | |
1303 | 88.4k | return Q; |
1304 | 88.4k | } |
1305 | | |
1306 | 7.97k | static int estimate_keyframe_frequency(VP8_COMP *cpi) { |
1307 | 7.97k | int i; |
1308 | | |
1309 | | /* Average key frame frequency */ |
1310 | 7.97k | int av_key_frame_frequency = 0; |
1311 | | |
1312 | | /* First key frame at start of sequence is a special case. We have no |
1313 | | * frequency data. |
1314 | | */ |
1315 | 7.97k | if (cpi->key_frame_count == 1) { |
1316 | | /* Assume a default of 1 kf every 2 seconds, or the max kf interval, |
1317 | | * whichever is smaller. |
1318 | | */ |
1319 | 2.10k | int key_freq = cpi->oxcf.key_freq > 0 ? cpi->oxcf.key_freq : 1; |
1320 | 2.10k | av_key_frame_frequency = 1 + (int)cpi->output_framerate * 2; |
1321 | | |
1322 | 2.10k | if (cpi->oxcf.auto_key && av_key_frame_frequency > key_freq) { |
1323 | 176 | av_key_frame_frequency = key_freq; |
1324 | 176 | } |
1325 | | |
1326 | 2.10k | cpi->prior_key_frame_distance[KEY_FRAME_CONTEXT - 1] = |
1327 | 2.10k | av_key_frame_frequency; |
1328 | 5.87k | } else { |
1329 | 5.87k | unsigned int total_weight = 0; |
1330 | 5.87k | int last_kf_interval = |
1331 | 5.87k | (cpi->frames_since_key > 0) ? cpi->frames_since_key : 1; |
1332 | | |
1333 | | /* reset keyframe context and calculate weighted average of last |
1334 | | * KEY_FRAME_CONTEXT keyframes |
1335 | | */ |
1336 | 35.2k | for (i = 0; i < KEY_FRAME_CONTEXT; ++i) { |
1337 | 29.3k | if (i < KEY_FRAME_CONTEXT - 1) { |
1338 | 23.4k | cpi->prior_key_frame_distance[i] = cpi->prior_key_frame_distance[i + 1]; |
1339 | 23.4k | } else { |
1340 | 5.87k | cpi->prior_key_frame_distance[i] = last_kf_interval; |
1341 | 5.87k | } |
1342 | | |
1343 | 29.3k | av_key_frame_frequency += |
1344 | 29.3k | prior_key_frame_weight[i] * cpi->prior_key_frame_distance[i]; |
1345 | 29.3k | total_weight += prior_key_frame_weight[i]; |
1346 | 29.3k | } |
1347 | | |
1348 | 5.87k | av_key_frame_frequency /= total_weight; |
1349 | 5.87k | } |
1350 | | // TODO (marpan): Given the checks above, |av_key_frame_frequency| |
1351 | | // should always be above 0. But for now we keep the sanity check in. |
1352 | 7.97k | if (av_key_frame_frequency == 0) av_key_frame_frequency = 1; |
1353 | 7.97k | return av_key_frame_frequency; |
1354 | 7.97k | } |
1355 | | |
1356 | 12.1k | void vp8_adjust_key_frame_context(VP8_COMP *cpi) { |
1357 | | /* Clear down mmx registers to allow floating point in what follows */ |
1358 | 12.1k | vpx_clear_system_state(); |
1359 | | |
1360 | | /* Do we have any key frame overspend to recover? */ |
1361 | | /* Two-pass overspend handled elsewhere. */ |
1362 | 12.1k | if ((cpi->pass != 2) && |
1363 | 12.1k | (cpi->projected_frame_size > cpi->per_frame_bandwidth)) { |
1364 | 7.97k | int overspend; |
1365 | | |
1366 | | /* Update the count of key frame overspend to be recovered in |
1367 | | * subsequent frames. A portion of the KF overspend is treated as gf |
1368 | | * overspend (and hence recovered more quickly) as the kf is also a |
1369 | | * gf. Otherwise the few frames following each kf tend to get more |
1370 | | * bits allocated than those following other gfs. |
1371 | | */ |
1372 | 7.97k | overspend = (cpi->projected_frame_size - cpi->per_frame_bandwidth); |
1373 | | |
1374 | 7.97k | if (cpi->oxcf.number_of_layers > 1) { |
1375 | 0 | cpi->kf_overspend_bits += overspend; |
1376 | 7.97k | } else { |
1377 | 7.97k | cpi->kf_overspend_bits += overspend * 7 / 8; |
1378 | 7.97k | cpi->gf_overspend_bits += overspend * 1 / 8; |
1379 | 7.97k | } |
1380 | | |
1381 | | /* Work out how much to try and recover per frame. */ |
1382 | 7.97k | cpi->kf_bitrate_adjustment = |
1383 | 7.97k | cpi->kf_overspend_bits / estimate_keyframe_frequency(cpi); |
1384 | 7.97k | } |
1385 | | |
1386 | 12.1k | cpi->frames_since_key = 0; |
1387 | 12.1k | cpi->key_frame_count++; |
1388 | 12.1k | } |
1389 | | |
1390 | | void vp8_compute_frame_size_bounds(VP8_COMP *cpi, int *frame_under_shoot_limit, |
1391 | 73.1k | int *frame_over_shoot_limit) { |
1392 | | /* Set-up bounds on acceptable frame size: */ |
1393 | 73.1k | if (cpi->oxcf.fixed_q >= 0) { |
1394 | | /* Fixed Q scenario: frame size never outranges target |
1395 | | * (there is no target!) |
1396 | | */ |
1397 | 0 | *frame_under_shoot_limit = 0; |
1398 | 0 | *frame_over_shoot_limit = INT_MAX; |
1399 | 73.1k | } else { |
1400 | 73.1k | const int64_t this_frame_target = cpi->this_frame_target; |
1401 | 73.1k | int64_t over_shoot_limit, under_shoot_limit; |
1402 | | |
1403 | 73.1k | if (cpi->common.frame_type == KEY_FRAME) { |
1404 | 12.1k | over_shoot_limit = this_frame_target * 9 / 8; |
1405 | 12.1k | under_shoot_limit = this_frame_target * 7 / 8; |
1406 | 60.9k | } else { |
1407 | 60.9k | if (cpi->oxcf.number_of_layers > 1 || cpi->common.refresh_alt_ref_frame || |
1408 | 60.9k | cpi->common.refresh_golden_frame) { |
1409 | 5.39k | over_shoot_limit = this_frame_target * 9 / 8; |
1410 | 5.39k | under_shoot_limit = this_frame_target * 7 / 8; |
1411 | 55.5k | } else { |
1412 | | /* For CBR take buffer fullness into account */ |
1413 | 55.5k | if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) { |
1414 | 0 | if (cpi->buffer_level >= ((cpi->oxcf.optimal_buffer_level + |
1415 | 0 | cpi->oxcf.maximum_buffer_size) >> |
1416 | 0 | 1)) { |
1417 | | /* Buffer is too full so relax overshoot and tighten |
1418 | | * undershoot |
1419 | | */ |
1420 | 0 | over_shoot_limit = this_frame_target * 12 / 8; |
1421 | 0 | under_shoot_limit = this_frame_target * 6 / 8; |
1422 | 0 | } else if (cpi->buffer_level <= |
1423 | 0 | (cpi->oxcf.optimal_buffer_level >> 1)) { |
1424 | | /* Buffer is too low so relax undershoot and tighten |
1425 | | * overshoot |
1426 | | */ |
1427 | 0 | over_shoot_limit = this_frame_target * 10 / 8; |
1428 | 0 | under_shoot_limit = this_frame_target * 4 / 8; |
1429 | 0 | } else { |
1430 | 0 | over_shoot_limit = this_frame_target * 11 / 8; |
1431 | 0 | under_shoot_limit = this_frame_target * 5 / 8; |
1432 | 0 | } |
1433 | 0 | } |
1434 | | /* VBR and CQ mode */ |
1435 | | /* Note that tighter restrictions here can help quality |
1436 | | * but hurt encode speed |
1437 | | */ |
1438 | 55.5k | else { |
1439 | | /* Stron overshoot limit for constrained quality */ |
1440 | 55.5k | if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) { |
1441 | 7.54k | over_shoot_limit = this_frame_target * 11 / 8; |
1442 | 7.54k | under_shoot_limit = this_frame_target * 2 / 8; |
1443 | 47.9k | } else { |
1444 | 47.9k | over_shoot_limit = this_frame_target * 11 / 8; |
1445 | 47.9k | under_shoot_limit = this_frame_target * 5 / 8; |
1446 | 47.9k | } |
1447 | 55.5k | } |
1448 | 55.5k | } |
1449 | 60.9k | } |
1450 | | |
1451 | | /* For very small rate targets where the fractional adjustment |
1452 | | * (eg * 7/8) may be tiny make sure there is at least a minimum |
1453 | | * range. |
1454 | | */ |
1455 | 73.1k | over_shoot_limit += 200; |
1456 | 73.1k | under_shoot_limit -= 200; |
1457 | 73.1k | if (under_shoot_limit < 0) under_shoot_limit = 0; |
1458 | 73.1k | if (under_shoot_limit > INT_MAX) under_shoot_limit = INT_MAX; |
1459 | 73.1k | if (over_shoot_limit > INT_MAX) over_shoot_limit = INT_MAX; |
1460 | 73.1k | *frame_under_shoot_limit = (int)under_shoot_limit; |
1461 | 73.1k | *frame_over_shoot_limit = (int)over_shoot_limit; |
1462 | 73.1k | } |
1463 | 73.1k | } |
1464 | | |
1465 | | /* return of 0 means drop frame */ |
1466 | 73.1k | int vp8_pick_frame_size(VP8_COMP *cpi) { |
1467 | 73.1k | VP8_COMMON *cm = &cpi->common; |
1468 | | |
1469 | 73.1k | if (cm->frame_type == KEY_FRAME) { |
1470 | 12.1k | calc_iframe_target_size(cpi); |
1471 | 60.9k | } else { |
1472 | 60.9k | calc_pframe_target_size(cpi); |
1473 | | |
1474 | | /* Check if we're dropping the frame: */ |
1475 | 60.9k | if (cpi->drop_frame) { |
1476 | 0 | cpi->drop_frame = 0; |
1477 | 0 | return 0; |
1478 | 0 | } |
1479 | 60.9k | } |
1480 | 73.1k | return 1; |
1481 | 73.1k | } |
1482 | | // If this just encoded frame (mcomp/transform/quant, but before loopfilter and |
1483 | | // pack_bitstream) has large overshoot, and was not being encoded close to the |
1484 | | // max QP, then drop this frame and force next frame to be encoded at max QP. |
1485 | | // Allow this for screen_content_mode = 2, or if drop frames is allowed. |
1486 | | // TODO(marpan): Should do this exit condition during the encode_frame |
1487 | | // (i.e., halfway during the encoding of the frame) to save cycles. |
1488 | 0 | int vp8_drop_encodedframe_overshoot(VP8_COMP *cpi, int Q) { |
1489 | 0 | int force_drop_overshoot = 0; |
1490 | | #if CONFIG_MULTI_RES_ENCODING |
1491 | | // Only check for dropping due to overshoot on the lowest stream. |
1492 | | // If the lowest stream of the multi-res encoding was dropped due to |
1493 | | // overshoot, then force dropping on all upper layer streams |
1494 | | // (mr_encoder_id > 0). |
1495 | | LOWER_RES_FRAME_INFO *low_res_frame_info = |
1496 | | (LOWER_RES_FRAME_INFO *)cpi->oxcf.mr_low_res_mode_info; |
1497 | | if (cpi->oxcf.mr_total_resolutions > 1 && cpi->oxcf.mr_encoder_id > 0) { |
1498 | | force_drop_overshoot = low_res_frame_info->is_frame_dropped_overshoot_maxqp; |
1499 | | if (!force_drop_overshoot) { |
1500 | | cpi->force_maxqp = 0; |
1501 | | cpi->frames_since_last_drop_overshoot++; |
1502 | | return 0; |
1503 | | } |
1504 | | } |
1505 | | #endif |
1506 | 0 | if (cpi->common.frame_type != KEY_FRAME && |
1507 | 0 | (cpi->oxcf.screen_content_mode == 2 || |
1508 | 0 | (cpi->drop_frames_allowed && |
1509 | 0 | (force_drop_overshoot || |
1510 | 0 | (cpi->rate_correction_factor < (8.0f * MIN_BPB_FACTOR) && |
1511 | 0 | cpi->frames_since_last_drop_overshoot > (int)cpi->framerate))))) { |
1512 | | // Note: the "projected_frame_size" from encode_frame() only gives estimate |
1513 | | // of mode/motion vector rate (in non-rd mode): so below we only require |
1514 | | // that projected_frame_size is somewhat greater than per-frame-bandwidth, |
1515 | | // but add additional condition with high threshold on prediction residual. |
1516 | | |
1517 | | // QP threshold: only allow dropping if we are not close to qp_max. |
1518 | 0 | int thresh_qp = 3 * cpi->worst_quality >> 2; |
1519 | | // Rate threshold, in bytes. |
1520 | 0 | int thresh_rate = 2 * (cpi->av_per_frame_bandwidth >> 3); |
1521 | | // Threshold for the average (over all macroblocks) of the pixel-sum |
1522 | | // residual error over 16x16 block. |
1523 | 0 | int thresh_pred_err_mb = (200 << 4); |
1524 | 0 | int pred_err_mb = (int)(cpi->mb.prediction_error / cpi->common.MBs); |
1525 | | // Reduce/ignore thresh_rate if pred_err_mb much larger than its threshold, |
1526 | | // give more weight to pred_err metric for overshoot detection. |
1527 | 0 | if (cpi->drop_frames_allowed && pred_err_mb > (thresh_pred_err_mb << 4)) |
1528 | 0 | thresh_rate = thresh_rate >> 3; |
1529 | 0 | if ((Q < thresh_qp && cpi->projected_frame_size > thresh_rate && |
1530 | 0 | pred_err_mb > thresh_pred_err_mb && |
1531 | 0 | pred_err_mb > 2 * cpi->last_pred_err_mb) || |
1532 | 0 | force_drop_overshoot) { |
1533 | 0 | unsigned int i; |
1534 | 0 | double new_correction_factor; |
1535 | 0 | int target_bits_per_mb; |
1536 | 0 | const int target_size = cpi->av_per_frame_bandwidth; |
1537 | | // Flag to indicate we will force next frame to be encoded at max QP. |
1538 | 0 | cpi->force_maxqp = 1; |
1539 | | // Reset the buffer levels. |
1540 | 0 | cpi->buffer_level = cpi->oxcf.optimal_buffer_level; |
1541 | 0 | cpi->bits_off_target = cpi->oxcf.optimal_buffer_level; |
1542 | | // Compute a new rate correction factor, corresponding to the current |
1543 | | // target frame size and max_QP, and adjust the rate correction factor |
1544 | | // upwards, if needed. |
1545 | | // This is to prevent a bad state where the re-encoded frame at max_QP |
1546 | | // undershoots significantly, and then we end up dropping every other |
1547 | | // frame because the QP/rate_correction_factor may have been too low |
1548 | | // before the drop and then takes too long to come up. |
1549 | 0 | if (target_size > (INT_MAX >> BPER_MB_NORMBITS)) { |
1550 | 0 | int temp = target_size / cpi->common.MBs; |
1551 | 0 | if (temp > (INT_MAX >> BPER_MB_NORMBITS)) { |
1552 | 0 | target_bits_per_mb = INT_MAX; |
1553 | 0 | } else { |
1554 | 0 | target_bits_per_mb = temp << BPER_MB_NORMBITS; |
1555 | 0 | } |
1556 | 0 | } else { |
1557 | 0 | target_bits_per_mb = |
1558 | 0 | (target_size << BPER_MB_NORMBITS) / cpi->common.MBs; |
1559 | 0 | } |
1560 | | // Rate correction factor based on target_size_per_mb and max_QP. |
1561 | 0 | new_correction_factor = |
1562 | 0 | (double)target_bits_per_mb / |
1563 | 0 | (double)vp8_bits_per_mb[INTER_FRAME][cpi->worst_quality]; |
1564 | 0 | if (new_correction_factor > cpi->rate_correction_factor) { |
1565 | 0 | cpi->rate_correction_factor = |
1566 | 0 | VPXMIN(2.0 * cpi->rate_correction_factor, new_correction_factor); |
1567 | 0 | } |
1568 | 0 | if (cpi->rate_correction_factor > MAX_BPB_FACTOR) { |
1569 | 0 | cpi->rate_correction_factor = MAX_BPB_FACTOR; |
1570 | 0 | } |
1571 | | // Drop this frame: update frame counters. |
1572 | 0 | cpi->common.current_video_frame++; |
1573 | 0 | cpi->frames_since_key++; |
1574 | 0 | cpi->temporal_pattern_counter++; |
1575 | 0 | cpi->frames_since_last_drop_overshoot = 0; |
1576 | 0 | if (cpi->oxcf.number_of_layers > 1) { |
1577 | | // Set max_qp and rate correction for all temporal layers if overshoot |
1578 | | // is detected. |
1579 | 0 | for (i = 0; i < cpi->oxcf.number_of_layers; ++i) { |
1580 | 0 | LAYER_CONTEXT *lc = &cpi->layer_context[i]; |
1581 | 0 | lc->force_maxqp = 1; |
1582 | 0 | lc->frames_since_last_drop_overshoot = 0; |
1583 | 0 | lc->rate_correction_factor = cpi->rate_correction_factor; |
1584 | 0 | } |
1585 | 0 | } |
1586 | | #if CONFIG_MULTI_RES_ENCODING |
1587 | | if (cpi->oxcf.mr_total_resolutions > 1) |
1588 | | low_res_frame_info->is_frame_dropped_overshoot_maxqp = 1; |
1589 | | #endif |
1590 | 0 | return 1; |
1591 | 0 | } |
1592 | 0 | cpi->force_maxqp = 0; |
1593 | 0 | cpi->frames_since_last_drop_overshoot++; |
1594 | | #if CONFIG_MULTI_RES_ENCODING |
1595 | | if (cpi->oxcf.mr_total_resolutions > 1) |
1596 | | low_res_frame_info->is_frame_dropped_overshoot_maxqp = 0; |
1597 | | #endif |
1598 | 0 | return 0; |
1599 | 0 | } |
1600 | 0 | cpi->force_maxqp = 0; |
1601 | 0 | cpi->frames_since_last_drop_overshoot++; |
1602 | | #if CONFIG_MULTI_RES_ENCODING |
1603 | | if (cpi->oxcf.mr_total_resolutions > 1) |
1604 | | low_res_frame_info->is_frame_dropped_overshoot_maxqp = 0; |
1605 | | #endif |
1606 | 0 | return 0; |
1607 | 0 | } |