Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(86)

Side by Side Diff: source/libvpx/vp9/encoder/vp9_ratectrl.c

Issue 11555023: libvpx: Add VP9 decoder. (Closed) Base URL: svn://chrome-svn/chrome/trunk/deps/third_party/libvpx/
Patch Set: Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
1 /*
2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <string.h>
15 #include <limits.h>
16 #include <assert.h>
17
18 #include "math.h"
19 #include "vp9/common/vp9_alloccommon.h"
20 #include "vp9/common/vp9_modecont.h"
21 #include "vp9/common/vp9_common.h"
22 #include "vp9/encoder/vp9_ratectrl.h"
23 #include "vp9/common/vp9_entropymode.h"
24 #include "vpx_mem/vpx_mem.h"
25 #include "vp9/common/vp9_systemdependent.h"
26 #include "vp9/encoder/vp9_encodemv.h"
27 #include "vp9/common/vp9_quant_common.h"
28
29 #define MIN_BPB_FACTOR 0.005
30 #define MAX_BPB_FACTOR 50
31
32 #ifdef MODE_STATS
33 extern unsigned int y_modes[VP9_YMODES];
34 extern unsigned int uv_modes[VP9_UV_MODES];
35 extern unsigned int b_modes[B_MODE_COUNT];
36
37 extern unsigned int inter_y_modes[MB_MODE_COUNT];
38 extern unsigned int inter_uv_modes[VP9_UV_MODES];
39 extern unsigned int inter_b_modes[B_MODE_COUNT];
40 #endif
41
42 // Bits Per MB at different Q (Multiplied by 512)
43 #define BPER_MB_NORMBITS 9
44
45 // % adjustment to target kf size based on seperation from previous frame
46 static const int kf_boost_seperation_adjustment[16] = {
47 30, 40, 50, 55, 60, 65, 70, 75,
48 80, 85, 90, 95, 100, 100, 100, 100,
49 };
50
51 static const int gf_adjust_table[101] = {
52 100,
53 115, 130, 145, 160, 175, 190, 200, 210, 220, 230,
54 240, 260, 270, 280, 290, 300, 310, 320, 330, 340,
55 350, 360, 370, 380, 390, 400, 400, 400, 400, 400,
56 400, 400, 400, 400, 400, 400, 400, 400, 400, 400,
57 400, 400, 400, 400, 400, 400, 400, 400, 400, 400,
58 400, 400, 400, 400, 400, 400, 400, 400, 400, 400,
59 400, 400, 400, 400, 400, 400, 400, 400, 400, 400,
60 400, 400, 400, 400, 400, 400, 400, 400, 400, 400,
61 400, 400, 400, 400, 400, 400, 400, 400, 400, 400,
62 400, 400, 400, 400, 400, 400, 400, 400, 400, 400,
63 };
64
65 static const int gf_intra_usage_adjustment[20] = {
66 125, 120, 115, 110, 105, 100, 95, 85, 80, 75,
67 70, 65, 60, 55, 50, 50, 50, 50, 50, 50,
68 };
69
70 static const int gf_interval_table[101] = {
71 7,
72 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
73 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
74 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
75 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
76 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
77 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
78 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
79 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
80 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
81 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
82 };
83
84 static const unsigned int prior_key_frame_weight[KEY_FRAME_CONTEXT] = { 1, 2, 3, 4, 5 };
85
86 // These functions use formulaic calculations to make playing with the
87 // quantizer tables easier. If necessary they can be replaced by lookup
88 // tables if and when things settle down in the experimental bitstream
89 double vp9_convert_qindex_to_q(int qindex) {
90 // Convert the index to a real Q value (scaled down to match old Q values)
91 return (double)vp9_ac_yquant(qindex) / 4.0;
92 }
93
94 int vp9_gfboost_qadjust(int qindex) {
95 int retval;
96 double q;
97
98 q = vp9_convert_qindex_to_q(qindex);
99 retval = (int)((0.00000828 * q * q * q) +
100 (-0.0055 * q * q) +
101 (1.32 * q) + 79.3);
102 return retval;
103 }
104
105 static int kfboost_qadjust(int qindex) {
106 int retval;
107 double q;
108
109 q = vp9_convert_qindex_to_q(qindex);
110 retval = (int)((0.00000973 * q * q * q) +
111 (-0.00613 * q * q) +
112 (1.316 * q) + 121.2);
113 return retval;
114 }
115
116 int vp9_bits_per_mb(FRAME_TYPE frame_type, int qindex) {
117 if (frame_type == KEY_FRAME)
118 return (int)(4500000 / vp9_convert_qindex_to_q(qindex));
119 else
120 return (int)(2850000 / vp9_convert_qindex_to_q(qindex));
121 }
122
123
124 void vp9_save_coding_context(VP9_COMP *cpi) {
125 CODING_CONTEXT *const cc = &cpi->coding_context;
126 VP9_COMMON *cm = &cpi->common;
127 MACROBLOCKD *xd = &cpi->mb.e_mbd;
128
129 // Stores a snapshot of key state variables which can subsequently be
130 // restored with a call to vp9_restore_coding_context. These functions are
131 // intended for use in a re-code loop in vp9_compress_frame where the
132 // quantizer value is adjusted between loop iterations.
133
134 cc->nmvc = cm->fc.nmvc;
135 vp9_copy(cc->nmvjointcost, cpi->mb.nmvjointcost);
136 vp9_copy(cc->nmvcosts, cpi->mb.nmvcosts);
137 vp9_copy(cc->nmvcosts_hp, cpi->mb.nmvcosts_hp);
138
139 vp9_copy(cc->vp9_mode_contexts, cm->fc.vp9_mode_contexts);
140
141 vp9_copy(cc->ymode_prob, cm->fc.ymode_prob);
142 #if CONFIG_SUPERBLOCKS
143 vp9_copy(cc->sb_ymode_prob, cm->fc.sb_ymode_prob);
144 #endif
145 vp9_copy(cc->bmode_prob, cm->fc.bmode_prob);
146 vp9_copy(cc->uv_mode_prob, cm->fc.uv_mode_prob);
147 vp9_copy(cc->i8x8_mode_prob, cm->fc.i8x8_mode_prob);
148 vp9_copy(cc->sub_mv_ref_prob, cm->fc.sub_mv_ref_prob);
149 vp9_copy(cc->mbsplit_prob, cm->fc.mbsplit_prob);
150
151 // Stats
152 #ifdef MODE_STATS
153 vp9_copy(cc->y_modes, y_modes);
154 vp9_copy(cc->uv_modes, uv_modes);
155 vp9_copy(cc->b_modes, b_modes);
156 vp9_copy(cc->inter_y_modes, inter_y_modes);
157 vp9_copy(cc->inter_uv_modes, inter_uv_modes);
158 vp9_copy(cc->inter_b_modes, inter_b_modes);
159 #endif
160
161 vp9_copy(cc->segment_pred_probs, cm->segment_pred_probs);
162 vp9_copy(cc->ref_pred_probs_update, cpi->ref_pred_probs_update);
163 vp9_copy(cc->ref_pred_probs, cm->ref_pred_probs);
164 vp9_copy(cc->prob_comppred, cm->prob_comppred);
165
166 vpx_memcpy(cpi->coding_context.last_frame_seg_map_copy,
167 cm->last_frame_seg_map, (cm->mb_rows * cm->mb_cols));
168
169 vp9_copy(cc->last_ref_lf_deltas, xd->last_ref_lf_deltas);
170 vp9_copy(cc->last_mode_lf_deltas, xd->last_mode_lf_deltas);
171
172 vp9_copy(cc->coef_probs, cm->fc.coef_probs);
173 vp9_copy(cc->hybrid_coef_probs, cm->fc.hybrid_coef_probs);
174 vp9_copy(cc->coef_probs_8x8, cm->fc.coef_probs_8x8);
175 vp9_copy(cc->hybrid_coef_probs_8x8, cm->fc.hybrid_coef_probs_8x8);
176 vp9_copy(cc->coef_probs_16x16, cm->fc.coef_probs_16x16);
177 vp9_copy(cc->hybrid_coef_probs_16x16, cm->fc.hybrid_coef_probs_16x16);
178 vp9_copy(cc->switchable_interp_prob, cm->fc.switchable_interp_prob);
179 #if CONFIG_COMP_INTERINTRA_PRED
180 cc->interintra_prob = cm->fc.interintra_prob;
181 #endif
182 }
183
184 void vp9_restore_coding_context(VP9_COMP *cpi) {
185 CODING_CONTEXT *const cc = &cpi->coding_context;
186 VP9_COMMON *cm = &cpi->common;
187 MACROBLOCKD *xd = &cpi->mb.e_mbd;
188
189 // Restore key state variables to the snapshot state stored in the
190 // previous call to vp9_save_coding_context.
191
192 cm->fc.nmvc = cc->nmvc;
193 vp9_copy(cpi->mb.nmvjointcost, cc->nmvjointcost);
194 vp9_copy(cpi->mb.nmvcosts, cc->nmvcosts);
195 vp9_copy(cpi->mb.nmvcosts_hp, cc->nmvcosts_hp);
196
197 vp9_copy(cm->fc.vp9_mode_contexts, cc->vp9_mode_contexts);
198
199 vp9_copy(cm->fc.ymode_prob, cc->ymode_prob);
200 #if CONFIG_SUPERBLOCKS
201 vp9_copy(cm->fc.sb_ymode_prob, cc->sb_ymode_prob);
202 #endif
203 vp9_copy(cm->fc.bmode_prob, cc->bmode_prob);
204 vp9_copy(cm->fc.i8x8_mode_prob, cc->i8x8_mode_prob);
205 vp9_copy(cm->fc.uv_mode_prob, cc->uv_mode_prob);
206 vp9_copy(cm->fc.sub_mv_ref_prob, cc->sub_mv_ref_prob);
207 vp9_copy(cm->fc.mbsplit_prob, cc->mbsplit_prob);
208
209 // Stats
210 #ifdef MODE_STATS
211 vp9_copy(y_modes, cc->y_modes);
212 vp9_copy(uv_modes, cc->uv_modes);
213 vp9_copy(b_modes, cc->b_modes);
214 vp9_copy(inter_y_modes, cc->inter_y_modes);
215 vp9_copy(inter_uv_modes, cc->inter_uv_modes);
216 vp9_copy(inter_b_modes, cc->inter_b_modes);
217 #endif
218
219 vp9_copy(cm->segment_pred_probs, cc->segment_pred_probs);
220 vp9_copy(cpi->ref_pred_probs_update, cc->ref_pred_probs_update);
221 vp9_copy(cm->ref_pred_probs, cc->ref_pred_probs);
222 vp9_copy(cm->prob_comppred, cc->prob_comppred);
223
224 vpx_memcpy(cm->last_frame_seg_map,
225 cpi->coding_context.last_frame_seg_map_copy,
226 (cm->mb_rows * cm->mb_cols));
227
228 vp9_copy(xd->last_ref_lf_deltas, cc->last_ref_lf_deltas);
229 vp9_copy(xd->last_mode_lf_deltas, cc->last_mode_lf_deltas);
230
231 vp9_copy(cm->fc.coef_probs, cc->coef_probs);
232 vp9_copy(cm->fc.hybrid_coef_probs, cc->hybrid_coef_probs);
233 vp9_copy(cm->fc.coef_probs_8x8, cc->coef_probs_8x8);
234 vp9_copy(cm->fc.hybrid_coef_probs_8x8, cc->hybrid_coef_probs_8x8);
235 vp9_copy(cm->fc.coef_probs_16x16, cc->coef_probs_16x16);
236 vp9_copy(cm->fc.hybrid_coef_probs_16x16, cc->hybrid_coef_probs_16x16);
237 vp9_copy(cm->fc.switchable_interp_prob, cc->switchable_interp_prob);
238 #if CONFIG_COMP_INTERINTRA_PRED
239 cm->fc.interintra_prob = cc->interintra_prob;
240 #endif
241 }
242
243
244 void vp9_setup_key_frame(VP9_COMP *cpi) {
245 VP9_COMMON *cm = &cpi->common;
246 // Setup for Key frame:
247 vp9_default_coef_probs(& cpi->common);
248 vp9_kf_default_bmode_probs(cpi->common.kf_bmode_prob);
249 vp9_init_mbmode_probs(& cpi->common);
250 vp9_default_bmode_probs(cm->fc.bmode_prob);
251
252 vp9_init_mv_probs(& cpi->common);
253
254 // cpi->common.filter_level = 0; // Reset every key frame.
255 cpi->common.filter_level = cpi->common.base_qindex * 3 / 8;
256
257 // interval before next GF
258 cpi->frames_till_gf_update_due = cpi->baseline_gf_interval;
259
260 cpi->common.refresh_golden_frame = TRUE;
261 cpi->common.refresh_alt_ref_frame = TRUE;
262
263 vp9_init_mode_contexts(&cpi->common);
264 vpx_memcpy(&cpi->common.lfc, &cpi->common.fc, sizeof(cpi->common.fc));
265 vpx_memcpy(&cpi->common.lfc_a, &cpi->common.fc, sizeof(cpi->common.fc));
266
267 vpx_memset(cm->prev_mip, 0,
268 (cm->mb_cols + 1) * (cm->mb_rows + 1)* sizeof(MODE_INFO));
269 vpx_memset(cm->mip, 0,
270 (cm->mb_cols + 1) * (cm->mb_rows + 1)* sizeof(MODE_INFO));
271
272 vp9_update_mode_info_border(cm, cm->mip);
273 vp9_update_mode_info_in_image(cm, cm->mi);
274 }
275
276 void vp9_setup_inter_frame(VP9_COMP *cpi) {
277 if (cpi->common.refresh_alt_ref_frame) {
278 vpx_memcpy(&cpi->common.fc,
279 &cpi->common.lfc_a,
280 sizeof(cpi->common.fc));
281 } else {
282 vpx_memcpy(&cpi->common.fc,
283 &cpi->common.lfc,
284 sizeof(cpi->common.fc));
285 }
286 }
287
288
289 static int estimate_bits_at_q(int frame_kind, int Q, int MBs,
290 double correction_factor) {
291 int Bpm = (int)(.5 + correction_factor * vp9_bits_per_mb(frame_kind, Q));
292
293 /* Attempt to retain reasonable accuracy without overflow. The cutoff is
294 * chosen such that the maximum product of Bpm and MBs fits 31 bits. The
295 * largest Bpm takes 20 bits.
296 */
297 if (MBs > (1 << 11))
298 return (Bpm >> BPER_MB_NORMBITS) * MBs;
299 else
300 return (Bpm * MBs) >> BPER_MB_NORMBITS;
301 }
302
303
304 static void calc_iframe_target_size(VP9_COMP *cpi) {
305 // boost defaults to half second
306 int target;
307
308 // Clear down mmx registers to allow floating point in what follows
309 vp9_clear_system_state(); // __asm emms;
310
311 // New Two pass RC
312 target = cpi->per_frame_bandwidth;
313
314 if (cpi->oxcf.rc_max_intra_bitrate_pct) {
315 int max_rate = cpi->per_frame_bandwidth
316 * cpi->oxcf.rc_max_intra_bitrate_pct / 100;
317
318 if (target > max_rate)
319 target = max_rate;
320 }
321
322 cpi->this_frame_target = target;
323
324 }
325
326
327 // Do the best we can to define the parameteres for the next GF based
328 // on what information we have available.
329 //
330 // In this experimental code only two pass is supported
331 // so we just use the interval determined in the two pass code.
332 static void calc_gf_params(VP9_COMP *cpi) {
333 // Set the gf interval
334 cpi->frames_till_gf_update_due = cpi->baseline_gf_interval;
335 }
336
337
338 static void calc_pframe_target_size(VP9_COMP *cpi) {
339 int min_frame_target;
340
341 min_frame_target = 0;
342
343 min_frame_target = cpi->min_frame_bandwidth;
344
345 if (min_frame_target < (cpi->av_per_frame_bandwidth >> 5))
346 min_frame_target = cpi->av_per_frame_bandwidth >> 5;
347
348
349 // Special alt reference frame case
350 if (cpi->common.refresh_alt_ref_frame) {
351 // Per frame bit target for the alt ref frame
352 cpi->per_frame_bandwidth = cpi->twopass.gf_bits;
353 cpi->this_frame_target = cpi->per_frame_bandwidth;
354 }
355
356 // Normal frames (gf,and inter)
357 else {
358 cpi->this_frame_target = cpi->per_frame_bandwidth;
359 }
360
361 // Sanity check that the total sum of adjustments is not above the maximum all owed
362 // That is that having allowed for KF and GF penalties we have not pushed the
363 // current interframe target to low. If the adjustment we apply here is not ca pable of recovering
364 // all the extra bits we have spent in the KF or GF then the remainder will ha ve to be recovered over
365 // a longer time span via other buffer / rate control mechanisms.
366 if (cpi->this_frame_target < min_frame_target)
367 cpi->this_frame_target = min_frame_target;
368
369 if (!cpi->common.refresh_alt_ref_frame)
370 // Note the baseline target data rate for this inter frame.
371 cpi->inter_frame_target = cpi->this_frame_target;
372
373 // Adjust target frame size for Golden Frames:
374 if (cpi->frames_till_gf_update_due == 0) {
375 // int Boost = 0;
376 int Q = (cpi->oxcf.fixed_q < 0) ? cpi->last_q[INTER_FRAME] : cpi->oxcf.fixed _q;
377
378 cpi->common.refresh_golden_frame = TRUE;
379
380 calc_gf_params(cpi);
381
382 // If we are using alternate ref instead of gf then do not apply the boost
383 // It will instead be applied to the altref update
384 // Jims modified boost
385 if (!cpi->source_alt_ref_active) {
386 if (cpi->oxcf.fixed_q < 0) {
387 // The spend on the GF is defined in the two pass code
388 // for two pass encodes
389 cpi->this_frame_target = cpi->per_frame_bandwidth;
390 } else
391 cpi->this_frame_target =
392 (estimate_bits_at_q(1, Q, cpi->common.MBs, 1.0)
393 * cpi->last_boost) / 100;
394
395 }
396 // If there is an active ARF at this location use the minimum
397 // bits on this frame even if it is a contructed arf.
398 // The active maximum quantizer insures that an appropriate
399 // number of bits will be spent if needed for contstructed ARFs.
400 else {
401 cpi->this_frame_target = 0;
402 }
403
404 cpi->current_gf_interval = cpi->frames_till_gf_update_due;
405 }
406 }
407
408
409 void vp9_update_rate_correction_factors(VP9_COMP *cpi, int damp_var) {
410 int Q = cpi->common.base_qindex;
411 int correction_factor = 100;
412 double rate_correction_factor;
413 double adjustment_limit;
414
415 int projected_size_based_on_q = 0;
416
417 // Clear down mmx registers to allow floating point in what follows
418 vp9_clear_system_state(); // __asm emms;
419
420 if (cpi->common.frame_type == KEY_FRAME) {
421 rate_correction_factor = cpi->key_frame_rate_correction_factor;
422 } else {
423 if (cpi->common.refresh_alt_ref_frame || cpi->common.refresh_golden_frame)
424 rate_correction_factor = cpi->gf_rate_correction_factor;
425 else
426 rate_correction_factor = cpi->rate_correction_factor;
427 }
428
429 // Work out how big we would have expected the frame to be at this Q given the current correction factor.
430 // Stay in double to avoid int overflow when values are large
431 projected_size_based_on_q =
432 (int)(((.5 + rate_correction_factor *
433 vp9_bits_per_mb(cpi->common.frame_type, Q)) *
434 cpi->common.MBs) / (1 << BPER_MB_NORMBITS));
435
436 // Make some allowance for cpi->zbin_over_quant
437 if (cpi->zbin_over_quant > 0) {
438 int Z = cpi->zbin_over_quant;
439 double Factor = 0.99;
440 double factor_adjustment = 0.01 / 256.0; // (double)ZBIN_OQ_MAX;
441
442 while (Z > 0) {
443 Z--;
444 projected_size_based_on_q =
445 (int)(Factor * projected_size_based_on_q);
446 Factor += factor_adjustment;
447
448 if (Factor >= 0.999)
449 Factor = 0.999;
450 }
451 }
452
453 // Work out a size correction factor.
454 // if ( cpi->this_frame_target > 0 )
455 // correction_factor = (100 * cpi->projected_frame_size) / cpi->this_frame_ta rget;
456 if (projected_size_based_on_q > 0)
457 correction_factor = (100 * cpi->projected_frame_size) / projected_size_based _on_q;
458
459 // More heavily damped adjustment used if we have been oscillating either side of target
460 switch (damp_var) {
461 case 0:
462 adjustment_limit = 0.75;
463 break;
464 case 1:
465 adjustment_limit = 0.375;
466 break;
467 case 2:
468 default:
469 adjustment_limit = 0.25;
470 break;
471 }
472
473 // if ( (correction_factor > 102) && (Q < cpi->active_worst_quality) )
474 if (correction_factor > 102) {
475 // We are not already at the worst allowable quality
476 correction_factor = (int)(100.5 + ((correction_factor - 100) * adjustment_li mit));
477 rate_correction_factor = ((rate_correction_factor * correction_factor) / 100 );
478
479 // Keep rate_correction_factor within limits
480 if (rate_correction_factor > MAX_BPB_FACTOR)
481 rate_correction_factor = MAX_BPB_FACTOR;
482 }
483 // else if ( (correction_factor < 99) && (Q > cpi->active_best_quality) )
484 else if (correction_factor < 99) {
485 // We are not already at the best allowable quality
486 correction_factor = (int)(100.5 - ((100 - correction_factor) * adjustment_li mit));
487 rate_correction_factor = ((rate_correction_factor * correction_factor) / 100 );
488
489 // Keep rate_correction_factor within limits
490 if (rate_correction_factor < MIN_BPB_FACTOR)
491 rate_correction_factor = MIN_BPB_FACTOR;
492 }
493
494 if (cpi->common.frame_type == KEY_FRAME)
495 cpi->key_frame_rate_correction_factor = rate_correction_factor;
496 else {
497 if (cpi->common.refresh_alt_ref_frame || cpi->common.refresh_golden_frame)
498 cpi->gf_rate_correction_factor = rate_correction_factor;
499 else
500 cpi->rate_correction_factor = rate_correction_factor;
501 }
502 }
503
504
505 int vp9_regulate_q(VP9_COMP *cpi, int target_bits_per_frame) {
506 int Q = cpi->active_worst_quality;
507
508 int i;
509 int last_error = INT_MAX;
510 int target_bits_per_mb;
511 int bits_per_mb_at_this_q;
512 double correction_factor;
513
514 // Reset Zbin OQ value
515 cpi->zbin_over_quant = 0;
516
517 // Select the appropriate correction factor based upon type of frame.
518 if (cpi->common.frame_type == KEY_FRAME)
519 correction_factor = cpi->key_frame_rate_correction_factor;
520 else {
521 if (cpi->common.refresh_alt_ref_frame || cpi->common.refresh_golden_frame)
522 correction_factor = cpi->gf_rate_correction_factor;
523 else
524 correction_factor = cpi->rate_correction_factor;
525 }
526
527 // Calculate required scaling factor based on target frame size and size of fr ame produced using previous Q
528 if (target_bits_per_frame >= (INT_MAX >> BPER_MB_NORMBITS))
529 target_bits_per_mb = (target_bits_per_frame / cpi->common.MBs) << BPER_MB_NO RMBITS; // Case where we would overflow int
530 else
531 target_bits_per_mb = (target_bits_per_frame << BPER_MB_NORMBITS) / cpi->comm on.MBs;
532
533 i = cpi->active_best_quality;
534
535 do {
536 bits_per_mb_at_this_q =
537 (int)(.5 + correction_factor *
538 vp9_bits_per_mb(cpi->common.frame_type, i));
539
540 if (bits_per_mb_at_this_q <= target_bits_per_mb) {
541 if ((target_bits_per_mb - bits_per_mb_at_this_q) <= last_error)
542 Q = i;
543 else
544 Q = i - 1;
545
546 break;
547 } else
548 last_error = bits_per_mb_at_this_q - target_bits_per_mb;
549 } while (++i <= cpi->active_worst_quality);
550
551
552 // If we are at MAXQ then enable Q over-run which seeks to claw back additiona l bits through things like
553 // the RD multiplier and zero bin size.
554 if (Q >= MAXQ) {
555 int zbin_oqmax;
556
557 double Factor = 0.99;
558 double factor_adjustment = 0.01 / 256.0; // (double)ZBIN_OQ_MAX;
559
560 if (cpi->common.frame_type == KEY_FRAME)
561 zbin_oqmax = 0; // ZBIN_OQ_MAX/16
562 else if (cpi->common.refresh_alt_ref_frame || (cpi->common.refresh_golden_fr ame && !cpi->source_alt_ref_active))
563 zbin_oqmax = 16;
564 else
565 zbin_oqmax = ZBIN_OQ_MAX;
566
567 // Each incrment in the zbin is assumed to have a fixed effect on bitrate. T his is not of course true.
568 // The effect will be highly clip dependent and may well have sudden steps.
569 // The idea here is to acheive higher effective quantizers than the normal m aximum by expanding the zero
570 // bin and hence decreasing the number of low magnitude non zero coefficient s.
571 while (cpi->zbin_over_quant < zbin_oqmax) {
572 cpi->zbin_over_quant++;
573
574 if (cpi->zbin_over_quant > zbin_oqmax)
575 cpi->zbin_over_quant = zbin_oqmax;
576
577 // Adjust bits_per_mb_at_this_q estimate
578 bits_per_mb_at_this_q = (int)(Factor * bits_per_mb_at_this_q);
579 Factor += factor_adjustment;
580
581 if (Factor >= 0.999)
582 Factor = 0.999;
583
584 if (bits_per_mb_at_this_q <= target_bits_per_mb) // Break out if we get down to the target rate
585 break;
586 }
587
588 }
589
590 return Q;
591 }
592
593
594 static int estimate_keyframe_frequency(VP9_COMP *cpi) {
595 int i;
596
597 // Average key frame frequency
598 int av_key_frame_frequency = 0;
599
600 /* First key frame at start of sequence is a special case. We have no
601 * frequency data.
602 */
603 if (cpi->key_frame_count == 1) {
604 /* Assume a default of 1 kf every 2 seconds, or the max kf interval,
605 * whichever is smaller.
606 */
607 int key_freq = cpi->oxcf.key_freq > 0 ? cpi->oxcf.key_freq : 1;
608 av_key_frame_frequency = (int)cpi->output_frame_rate * 2;
609
610 if (cpi->oxcf.auto_key && av_key_frame_frequency > key_freq)
611 av_key_frame_frequency = cpi->oxcf.key_freq;
612
613 cpi->prior_key_frame_distance[KEY_FRAME_CONTEXT - 1]
614 = av_key_frame_frequency;
615 } else {
616 unsigned int total_weight = 0;
617 int last_kf_interval =
618 (cpi->frames_since_key > 0) ? cpi->frames_since_key : 1;
619
620 /* reset keyframe context and calculate weighted average of last
621 * KEY_FRAME_CONTEXT keyframes
622 */
623 for (i = 0; i < KEY_FRAME_CONTEXT; i++) {
624 if (i < KEY_FRAME_CONTEXT - 1)
625 cpi->prior_key_frame_distance[i]
626 = cpi->prior_key_frame_distance[i + 1];
627 else
628 cpi->prior_key_frame_distance[i] = last_kf_interval;
629
630 av_key_frame_frequency += prior_key_frame_weight[i]
631 * cpi->prior_key_frame_distance[i];
632 total_weight += prior_key_frame_weight[i];
633 }
634
635 av_key_frame_frequency /= total_weight;
636
637 }
638 return av_key_frame_frequency;
639 }
640
641
642 void vp9_adjust_key_frame_context(VP9_COMP *cpi) {
643 // Clear down mmx registers to allow floating point in what follows
644 vp9_clear_system_state();
645
646 cpi->frames_since_key = 0;
647 cpi->key_frame_count++;
648 }
649
650
651 void vp9_compute_frame_size_bounds(VP9_COMP *cpi, int *frame_under_shoot_limit,
652 int *frame_over_shoot_limit) {
653 // Set-up bounds on acceptable frame size:
654 if (cpi->oxcf.fixed_q >= 0) {
655 // Fixed Q scenario: frame size never outranges target (there is no target!)
656 *frame_under_shoot_limit = 0;
657 *frame_over_shoot_limit = INT_MAX;
658 } else {
659 if (cpi->common.frame_type == KEY_FRAME) {
660 *frame_over_shoot_limit = cpi->this_frame_target * 9 / 8;
661 *frame_under_shoot_limit = cpi->this_frame_target * 7 / 8;
662 } else {
663 if (cpi->common.refresh_alt_ref_frame || cpi->common.refresh_golden_frame) {
664 *frame_over_shoot_limit = cpi->this_frame_target * 9 / 8;
665 *frame_under_shoot_limit = cpi->this_frame_target * 7 / 8;
666 } else {
667 // Stron overshoot limit for constrained quality
668 if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) {
669 *frame_over_shoot_limit = cpi->this_frame_target * 11 / 8;
670 *frame_under_shoot_limit = cpi->this_frame_target * 2 / 8;
671 } else {
672 *frame_over_shoot_limit = cpi->this_frame_target * 11 / 8;
673 *frame_under_shoot_limit = cpi->this_frame_target * 5 / 8;
674 }
675 }
676 }
677
678 // For very small rate targets where the fractional adjustment
679 // (eg * 7/8) may be tiny make sure there is at least a minimum
680 // range.
681 *frame_over_shoot_limit += 200;
682 *frame_under_shoot_limit -= 200;
683 if (*frame_under_shoot_limit < 0)
684 *frame_under_shoot_limit = 0;
685 }
686 }
687
688
689 // return of 0 means drop frame
690 int vp9_pick_frame_size(VP9_COMP *cpi) {
691 VP9_COMMON *cm = &cpi->common;
692
693 if (cm->frame_type == KEY_FRAME)
694 calc_iframe_target_size(cpi);
695 else
696 calc_pframe_target_size(cpi);
697
698 return 1;
699 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698