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

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

Issue 592203002: libvpx: Pull from upstream (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/deps/third_party/libvpx/
Patch Set: Created 6 years, 3 months 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
« no previous file with comments | « source/libvpx/vp9/encoder/vp9_ratectrl.h ('k') | source/libvpx/vp9/encoder/vp9_rd.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved. 2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
(...skipping 24 matching lines...) Expand all
35 #define DEFAULT_KF_BOOST 2000 35 #define DEFAULT_KF_BOOST 2000
36 #define DEFAULT_GF_BOOST 2000 36 #define DEFAULT_GF_BOOST 2000
37 37
38 #define LIMIT_QRANGE_FOR_ALTREF_AND_KEY 1 38 #define LIMIT_QRANGE_FOR_ALTREF_AND_KEY 1
39 39
40 #define MIN_BPB_FACTOR 0.005 40 #define MIN_BPB_FACTOR 0.005
41 #define MAX_BPB_FACTOR 50 41 #define MAX_BPB_FACTOR 50
42 42
43 #define FRAME_OVERHEAD_BITS 200 43 #define FRAME_OVERHEAD_BITS 200
44 44
45 #if CONFIG_VP9_HIGHBITDEPTH
46 #define ASSIGN_MINQ_TABLE(bit_depth, name) \
47 do { \
48 switch (bit_depth) { \
49 case VPX_BITS_8: \
50 name = name##_8; \
51 break; \
52 case VPX_BITS_10: \
53 name = name##_10; \
54 break; \
55 case VPX_BITS_12: \
56 name = name##_12; \
57 break; \
58 default: \
59 assert(0 && "bit_depth should be VPX_BITS_8, VPX_BITS_10" \
60 " or VPX_BITS_12"); \
61 name = NULL; \
62 } \
63 } while (0)
64 #else
65 #define ASSIGN_MINQ_TABLE(bit_depth, name) \
66 do { \
67 (void) bit_depth; \
68 name = name##_8; \
69 } while (0)
70 #endif
71
45 // Tables relating active max Q to active min Q 72 // Tables relating active max Q to active min Q
46 static int kf_low_motion_minq[QINDEX_RANGE]; 73 static int kf_low_motion_minq_8[QINDEX_RANGE];
47 static int kf_high_motion_minq[QINDEX_RANGE]; 74 static int kf_high_motion_minq_8[QINDEX_RANGE];
48 static int arfgf_low_motion_minq[QINDEX_RANGE]; 75 static int arfgf_low_motion_minq_8[QINDEX_RANGE];
49 static int arfgf_high_motion_minq[QINDEX_RANGE]; 76 static int arfgf_high_motion_minq_8[QINDEX_RANGE];
50 static int inter_minq[QINDEX_RANGE]; 77 static int inter_minq_8[QINDEX_RANGE];
51 static int rtc_minq[QINDEX_RANGE]; 78 static int rtc_minq_8[QINDEX_RANGE];
79
80 #if CONFIG_VP9_HIGHBITDEPTH
81 static int kf_low_motion_minq_10[QINDEX_RANGE];
82 static int kf_high_motion_minq_10[QINDEX_RANGE];
83 static int arfgf_low_motion_minq_10[QINDEX_RANGE];
84 static int arfgf_high_motion_minq_10[QINDEX_RANGE];
85 static int inter_minq_10[QINDEX_RANGE];
86 static int rtc_minq_10[QINDEX_RANGE];
87 static int kf_low_motion_minq_12[QINDEX_RANGE];
88 static int kf_high_motion_minq_12[QINDEX_RANGE];
89 static int arfgf_low_motion_minq_12[QINDEX_RANGE];
90 static int arfgf_high_motion_minq_12[QINDEX_RANGE];
91 static int inter_minq_12[QINDEX_RANGE];
92 static int rtc_minq_12[QINDEX_RANGE];
93 #endif
94
52 static int gf_high = 2000; 95 static int gf_high = 2000;
53 static int gf_low = 400; 96 static int gf_low = 400;
54 static int kf_high = 5000; 97 static int kf_high = 5000;
55 static int kf_low = 400; 98 static int kf_low = 400;
56 99
57 // Functions to compute the active minq lookup table entries based on a 100 // Functions to compute the active minq lookup table entries based on a
58 // formulaic approach to facilitate easier adjustment of the Q tables. 101 // formulaic approach to facilitate easier adjustment of the Q tables.
59 // The formulae were derived from computing a 3rd order polynomial best 102 // The formulae were derived from computing a 3rd order polynomial best
60 // fit to the original data (after plotting real maxq vs minq (not q index)) 103 // fit to the original data (after plotting real maxq vs minq (not q index))
61 static int get_minq_index(double maxq, double x3, double x2, double x1) { 104 static int get_minq_index(double maxq, double x3, double x2, double x1,
105 vpx_bit_depth_t bit_depth) {
62 int i; 106 int i;
63 const double minqtarget = MIN(((x3 * maxq + x2) * maxq + x1) * maxq, 107 const double minqtarget = MIN(((x3 * maxq + x2) * maxq + x1) * maxq,
64 maxq); 108 maxq);
65 109
66 // Special case handling to deal with the step from q2.0 110 // Special case handling to deal with the step from q2.0
67 // down to lossless mode represented by q 1.0. 111 // down to lossless mode represented by q 1.0.
68 if (minqtarget <= 2.0) 112 if (minqtarget <= 2.0)
69 return 0; 113 return 0;
70 114
71 for (i = 0; i < QINDEX_RANGE; i++) 115 for (i = 0; i < QINDEX_RANGE; i++) {
72 if (minqtarget <= vp9_convert_qindex_to_q(i)) 116 if (minqtarget <= vp9_convert_qindex_to_q(i, bit_depth))
73 return i; 117 return i;
118 }
74 119
75 return QINDEX_RANGE - 1; 120 return QINDEX_RANGE - 1;
76 } 121 }
77 122
123 static void init_minq_luts(int *kf_low_m, int *kf_high_m,
124 int *arfgf_low, int *arfgf_high,
125 int *inter, int *rtc, vpx_bit_depth_t bit_depth) {
126 int i;
127 for (i = 0; i < QINDEX_RANGE; i++) {
128 const double maxq = vp9_convert_qindex_to_q(i, bit_depth);
129 kf_low_m[i] = get_minq_index(maxq, 0.000001, -0.0004, 0.150, bit_depth);
130 kf_high_m[i] = get_minq_index(maxq, 0.0000021, -0.00125, 0.55, bit_depth);
131 arfgf_low[i] = get_minq_index(maxq, 0.0000015, -0.0009, 0.30, bit_depth);
132 arfgf_high[i] = get_minq_index(maxq, 0.0000021, -0.00125, 0.55, bit_depth);
133 inter[i] = get_minq_index(maxq, 0.00000271, -0.00113, 0.90, bit_depth);
134 rtc[i] = get_minq_index(maxq, 0.00000271, -0.00113, 0.70, bit_depth);
135 }
136 }
137
78 void vp9_rc_init_minq_luts() { 138 void vp9_rc_init_minq_luts() {
79 int i; 139 init_minq_luts(kf_low_motion_minq_8, kf_high_motion_minq_8,
80 140 arfgf_low_motion_minq_8, arfgf_high_motion_minq_8,
81 for (i = 0; i < QINDEX_RANGE; i++) { 141 inter_minq_8, rtc_minq_8, VPX_BITS_8);
82 const double maxq = vp9_convert_qindex_to_q(i); 142 #if CONFIG_VP9_HIGHBITDEPTH
83 kf_low_motion_minq[i] = get_minq_index(maxq, 0.000001, -0.0004, 0.125); 143 init_minq_luts(kf_low_motion_minq_10, kf_high_motion_minq_10,
84 kf_high_motion_minq[i] = get_minq_index(maxq, 0.000002, -0.0012, 0.50); 144 arfgf_low_motion_minq_10, arfgf_high_motion_minq_10,
85 arfgf_low_motion_minq[i] = get_minq_index(maxq, 0.0000015, -0.0009, 0.30); 145 inter_minq_10, rtc_minq_10, VPX_BITS_10);
86 arfgf_high_motion_minq[i] = get_minq_index(maxq, 0.0000021, -0.00125, 0.50); 146 init_minq_luts(kf_low_motion_minq_12, kf_high_motion_minq_12,
87 inter_minq[i] = get_minq_index(maxq, 0.00000271, -0.00113, 0.90); 147 arfgf_low_motion_minq_12, arfgf_high_motion_minq_12,
88 rtc_minq[i] = get_minq_index(maxq, 0.00000271, -0.00113, 0.70); 148 inter_minq_12, rtc_minq_12, VPX_BITS_12);
89 } 149 #endif
90 } 150 }
91 151
92 // These functions use formulaic calculations to make playing with the 152 // These functions use formulaic calculations to make playing with the
93 // quantizer tables easier. If necessary they can be replaced by lookup 153 // quantizer tables easier. If necessary they can be replaced by lookup
94 // tables if and when things settle down in the experimental bitstream 154 // tables if and when things settle down in the experimental bitstream
95 double vp9_convert_qindex_to_q(int qindex) { 155 double vp9_convert_qindex_to_q(int qindex, vpx_bit_depth_t bit_depth) {
96 // Convert the index to a real Q value (scaled down to match old Q values) 156 // Convert the index to a real Q value (scaled down to match old Q values)
97 return vp9_ac_quant(qindex, 0) / 4.0; 157 #if CONFIG_VP9_HIGHBITDEPTH
158 switch (bit_depth) {
159 case VPX_BITS_8:
160 return vp9_ac_quant(qindex, 0, bit_depth) / 4.0;
161 case VPX_BITS_10:
162 return vp9_ac_quant(qindex, 0, bit_depth) / 16.0;
163 case VPX_BITS_12:
164 return vp9_ac_quant(qindex, 0, bit_depth) / 64.0;
165 default:
166 assert(0 && "bit_depth should be VPX_BITS_8, VPX_BITS_10 or VPX_BITS_12");
167 return -1.0;
168 }
169 #else
170 return vp9_ac_quant(qindex, 0, bit_depth) / 4.0;
171 #endif
98 } 172 }
99 173
100 int vp9_rc_bits_per_mb(FRAME_TYPE frame_type, int qindex, 174 int vp9_rc_bits_per_mb(FRAME_TYPE frame_type, int qindex,
101 double correction_factor) { 175 double correction_factor,
102 const double q = vp9_convert_qindex_to_q(qindex); 176 vpx_bit_depth_t bit_depth) {
103 int enumerator = frame_type == KEY_FRAME ? 3300000 : 2250000; 177 const double q = vp9_convert_qindex_to_q(qindex, bit_depth);
178 int enumerator = frame_type == KEY_FRAME ? 2700000 : 1800000;
104 179
105 // q based adjustment to baseline enumerator 180 // q based adjustment to baseline enumerator
106 enumerator += (int)(enumerator * q) >> 12; 181 enumerator += (int)(enumerator * q) >> 12;
107 return (int)(enumerator * correction_factor / q); 182 return (int)(enumerator * correction_factor / q);
108 } 183 }
109 184
110 static int estimate_bits_at_q(FRAME_TYPE frame_type, int q, int mbs, 185 static int estimate_bits_at_q(FRAME_TYPE frame_type, int q, int mbs,
111 double correction_factor) { 186 double correction_factor,
112 const int bpm = (int)(vp9_rc_bits_per_mb(frame_type, q, correction_factor)); 187 vpx_bit_depth_t bit_depth) {
188 const int bpm = (int)(vp9_rc_bits_per_mb(frame_type, q, correction_factor,
189 bit_depth));
113 return ((uint64_t)bpm * mbs) >> BPER_MB_NORMBITS; 190 return ((uint64_t)bpm * mbs) >> BPER_MB_NORMBITS;
114 } 191 }
115 192
116 int vp9_rc_clamp_pframe_target_size(const VP9_COMP *const cpi, int target) { 193 int vp9_rc_clamp_pframe_target_size(const VP9_COMP *const cpi, int target) {
117 const RATE_CONTROL *rc = &cpi->rc; 194 const RATE_CONTROL *rc = &cpi->rc;
118 const int min_frame_target = MAX(rc->min_frame_bandwidth, 195 const int min_frame_target = MAX(rc->min_frame_bandwidth,
119 rc->avg_frame_bandwidth >> 5); 196 rc->avg_frame_bandwidth >> 5);
120 if (target < min_frame_target) 197 if (target < min_frame_target)
121 target = min_frame_target; 198 target = min_frame_target;
122 if (cpi->refresh_golden_frame && rc->is_src_frame_alt_ref) { 199 if (cpi->refresh_golden_frame && rc->is_src_frame_alt_ref) {
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
220 rc->source_alt_ref_pending = 0; 297 rc->source_alt_ref_pending = 0;
221 rc->source_alt_ref_active = 0; 298 rc->source_alt_ref_active = 0;
222 299
223 rc->frames_till_gf_update_due = 0; 300 rc->frames_till_gf_update_due = 0;
224 301
225 rc->ni_av_qi = oxcf->worst_allowed_q; 302 rc->ni_av_qi = oxcf->worst_allowed_q;
226 rc->ni_tot_qi = 0; 303 rc->ni_tot_qi = 0;
227 rc->ni_frames = 0; 304 rc->ni_frames = 0;
228 305
229 rc->tot_q = 0.0; 306 rc->tot_q = 0.0;
230 rc->avg_q = vp9_convert_qindex_to_q(oxcf->worst_allowed_q); 307 rc->avg_q = vp9_convert_qindex_to_q(oxcf->worst_allowed_q, oxcf->bit_depth);
231 308
232 for (i = 0; i < RATE_FACTOR_LEVELS; ++i) { 309 for (i = 0; i < RATE_FACTOR_LEVELS; ++i) {
233 rc->rate_correction_factors[i] = 1.0; 310 rc->rate_correction_factors[i] = 1.0;
234 } 311 }
235 } 312 }
236 313
237 int vp9_rc_drop_frame(VP9_COMP *cpi) { 314 int vp9_rc_drop_frame(VP9_COMP *cpi) {
238 const VP9EncoderConfig *oxcf = &cpi->oxcf; 315 const VP9EncoderConfig *oxcf = &cpi->oxcf;
239 RATE_CONTROL *const rc = &cpi->rc; 316 RATE_CONTROL *const rc = &cpi->rc;
240 317
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
323 return; 400 return;
324 401
325 // Clear down mmx registers to allow floating point in what follows 402 // Clear down mmx registers to allow floating point in what follows
326 vp9_clear_system_state(); 403 vp9_clear_system_state();
327 404
328 // Work out how big we would have expected the frame to be at this Q given 405 // Work out how big we would have expected the frame to be at this Q given
329 // the current correction factor. 406 // the current correction factor.
330 // Stay in double to avoid int overflow when values are large 407 // Stay in double to avoid int overflow when values are large
331 projected_size_based_on_q = estimate_bits_at_q(cm->frame_type, 408 projected_size_based_on_q = estimate_bits_at_q(cm->frame_type,
332 cm->base_qindex, cm->MBs, 409 cm->base_qindex, cm->MBs,
333 rate_correction_factor); 410 rate_correction_factor,
411 cm->bit_depth);
334 // Work out a size correction factor. 412 // Work out a size correction factor.
335 if (projected_size_based_on_q > 0) 413 if (projected_size_based_on_q > 0)
336 correction_factor = (100 * cpi->rc.projected_frame_size) / 414 correction_factor = (100 * cpi->rc.projected_frame_size) /
337 projected_size_based_on_q; 415 projected_size_based_on_q;
338 416
339 // More heavily damped adjustment used if we have been oscillating either side 417 // More heavily damped adjustment used if we have been oscillating either side
340 // of target. 418 // of target.
341 switch (damp_var) { 419 switch (damp_var) {
342 case 0: 420 case 0:
343 adjustment_limit = 0.75; 421 adjustment_limit = 0.75;
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
385 463
386 // Calculate required scaling factor based on target frame size and size of 464 // Calculate required scaling factor based on target frame size and size of
387 // frame produced using previous Q. 465 // frame produced using previous Q.
388 target_bits_per_mb = 466 target_bits_per_mb =
389 ((uint64_t)target_bits_per_frame << BPER_MB_NORMBITS) / cm->MBs; 467 ((uint64_t)target_bits_per_frame << BPER_MB_NORMBITS) / cm->MBs;
390 468
391 i = active_best_quality; 469 i = active_best_quality;
392 470
393 do { 471 do {
394 const int bits_per_mb_at_this_q = (int)vp9_rc_bits_per_mb(cm->frame_type, i, 472 const int bits_per_mb_at_this_q = (int)vp9_rc_bits_per_mb(cm->frame_type, i,
395 correction_factor); 473 correction_factor,
474 cm->bit_depth);
396 475
397 if (bits_per_mb_at_this_q <= target_bits_per_mb) { 476 if (bits_per_mb_at_this_q <= target_bits_per_mb) {
398 if ((target_bits_per_mb - bits_per_mb_at_this_q) <= last_error) 477 if ((target_bits_per_mb - bits_per_mb_at_this_q) <= last_error)
399 q = i; 478 q = i;
400 else 479 else
401 q = i - 1; 480 q = i - 1;
402 481
403 break; 482 break;
404 } else { 483 } else {
405 last_error = bits_per_mb_at_this_q - target_bits_per_mb; 484 last_error = bits_per_mb_at_this_q - target_bits_per_mb;
(...skipping 11 matching lines...) Expand all
417 return high_motion_minq[q]; 496 return high_motion_minq[q];
418 } else { 497 } else {
419 const int gap = high - low; 498 const int gap = high - low;
420 const int offset = high - gfu_boost; 499 const int offset = high - gfu_boost;
421 const int qdiff = high_motion_minq[q] - low_motion_minq[q]; 500 const int qdiff = high_motion_minq[q] - low_motion_minq[q];
422 const int adjustment = ((offset * qdiff) + (gap >> 1)) / gap; 501 const int adjustment = ((offset * qdiff) + (gap >> 1)) / gap;
423 return low_motion_minq[q] + adjustment; 502 return low_motion_minq[q] + adjustment;
424 } 503 }
425 } 504 }
426 505
427 static int get_kf_active_quality(const RATE_CONTROL *const rc, int q) { 506 static int get_kf_active_quality(const RATE_CONTROL *const rc, int q,
507 vpx_bit_depth_t bit_depth) {
508 int *kf_low_motion_minq;
509 int *kf_high_motion_minq;
510 ASSIGN_MINQ_TABLE(bit_depth, kf_low_motion_minq);
511 ASSIGN_MINQ_TABLE(bit_depth, kf_high_motion_minq);
428 return get_active_quality(q, rc->kf_boost, kf_low, kf_high, 512 return get_active_quality(q, rc->kf_boost, kf_low, kf_high,
429 kf_low_motion_minq, kf_high_motion_minq); 513 kf_low_motion_minq, kf_high_motion_minq);
430 } 514 }
431 515
432 static int get_gf_active_quality(const RATE_CONTROL *const rc, int q) { 516 static int get_gf_active_quality(const RATE_CONTROL *const rc, int q,
517 vpx_bit_depth_t bit_depth) {
518 int *arfgf_low_motion_minq;
519 int *arfgf_high_motion_minq;
520 ASSIGN_MINQ_TABLE(bit_depth, arfgf_low_motion_minq);
521 ASSIGN_MINQ_TABLE(bit_depth, arfgf_high_motion_minq);
433 return get_active_quality(q, rc->gfu_boost, gf_low, gf_high, 522 return get_active_quality(q, rc->gfu_boost, gf_low, gf_high,
434 arfgf_low_motion_minq, arfgf_high_motion_minq); 523 arfgf_low_motion_minq, arfgf_high_motion_minq);
435 } 524 }
436 525
437 static int calc_active_worst_quality_one_pass_vbr(const VP9_COMP *cpi) { 526 static int calc_active_worst_quality_one_pass_vbr(const VP9_COMP *cpi) {
438 const RATE_CONTROL *const rc = &cpi->rc; 527 const RATE_CONTROL *const rc = &cpi->rc;
439 const unsigned int curr_frame = cpi->common.current_video_frame; 528 const unsigned int curr_frame = cpi->common.current_video_frame;
440 int active_worst_quality; 529 int active_worst_quality;
441 530
442 if (cpi->common.frame_type == KEY_FRAME) { 531 if (cpi->common.frame_type == KEY_FRAME) {
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
509 } 598 }
510 599
511 static int rc_pick_q_and_bounds_one_pass_cbr(const VP9_COMP *cpi, 600 static int rc_pick_q_and_bounds_one_pass_cbr(const VP9_COMP *cpi,
512 int *bottom_index, 601 int *bottom_index,
513 int *top_index) { 602 int *top_index) {
514 const VP9_COMMON *const cm = &cpi->common; 603 const VP9_COMMON *const cm = &cpi->common;
515 const RATE_CONTROL *const rc = &cpi->rc; 604 const RATE_CONTROL *const rc = &cpi->rc;
516 int active_best_quality; 605 int active_best_quality;
517 int active_worst_quality = calc_active_worst_quality_one_pass_cbr(cpi); 606 int active_worst_quality = calc_active_worst_quality_one_pass_cbr(cpi);
518 int q; 607 int q;
608 int *rtc_minq;
609 ASSIGN_MINQ_TABLE(cm->bit_depth, rtc_minq);
519 610
520 if (frame_is_intra_only(cm)) { 611 if (frame_is_intra_only(cm)) {
521 active_best_quality = rc->best_quality; 612 active_best_quality = rc->best_quality;
522 // Handle the special case for key frames forced when we have75 reached 613 // Handle the special case for key frames forced when we have reached
523 // the maximum key frame interval. Here force the Q to a range 614 // the maximum key frame interval. Here force the Q to a range
524 // based on the ambient Q to reduce the risk of popping. 615 // based on the ambient Q to reduce the risk of popping.
525 if (rc->this_key_frame_forced) { 616 if (rc->this_key_frame_forced) {
526 int qindex = rc->last_boosted_qindex; 617 int qindex = rc->last_boosted_qindex;
527 double last_boosted_q = vp9_convert_qindex_to_q(qindex); 618 double last_boosted_q = vp9_convert_qindex_to_q(qindex, cm->bit_depth);
528 int delta_qindex = vp9_compute_qdelta(rc, last_boosted_q, 619 int delta_qindex = vp9_compute_qdelta(rc, last_boosted_q,
529 (last_boosted_q * 0.75)); 620 (last_boosted_q * 0.75),
621 cm->bit_depth);
530 active_best_quality = MAX(qindex + delta_qindex, rc->best_quality); 622 active_best_quality = MAX(qindex + delta_qindex, rc->best_quality);
531 } else if (cm->current_video_frame > 0) { 623 } else if (cm->current_video_frame > 0) {
532 // not first frame of one pass and kf_boost is set 624 // not first frame of one pass and kf_boost is set
533 double q_adj_factor = 1.0; 625 double q_adj_factor = 1.0;
534 double q_val; 626 double q_val;
535 627
536 active_best_quality = 628 active_best_quality =
537 get_kf_active_quality(rc, rc->avg_frame_qindex[KEY_FRAME]); 629 get_kf_active_quality(rc, rc->avg_frame_qindex[KEY_FRAME],
630 cm->bit_depth);
538 631
539 // Allow somewhat lower kf minq with small image formats. 632 // Allow somewhat lower kf minq with small image formats.
540 if ((cm->width * cm->height) <= (352 * 288)) { 633 if ((cm->width * cm->height) <= (352 * 288)) {
541 q_adj_factor -= 0.25; 634 q_adj_factor -= 0.25;
542 } 635 }
543 636
544 // Convert the adjustment factor to a qindex delta 637 // Convert the adjustment factor to a qindex delta
545 // on active_best_quality. 638 // on active_best_quality.
546 q_val = vp9_convert_qindex_to_q(active_best_quality); 639 q_val = vp9_convert_qindex_to_q(active_best_quality, cm->bit_depth);
547 active_best_quality += vp9_compute_qdelta(rc, q_val, 640 active_best_quality += vp9_compute_qdelta(rc, q_val,
548 q_val * q_adj_factor); 641 q_val * q_adj_factor,
642 cm->bit_depth);
549 } 643 }
550 } else if (!rc->is_src_frame_alt_ref && 644 } else if (!rc->is_src_frame_alt_ref &&
551 !cpi->use_svc && 645 !cpi->use_svc &&
552 (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) { 646 (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
553 // Use the lower of active_worst_quality and recent 647 // Use the lower of active_worst_quality and recent
554 // average Q as basis for GF/ARF best Q limit unless last frame was 648 // average Q as basis for GF/ARF best Q limit unless last frame was
555 // a key frame. 649 // a key frame.
556 if (rc->frames_since_key > 1 && 650 if (rc->frames_since_key > 1 &&
557 rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) { 651 rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) {
558 q = rc->avg_frame_qindex[INTER_FRAME]; 652 q = rc->avg_frame_qindex[INTER_FRAME];
559 } else { 653 } else {
560 q = active_worst_quality; 654 q = active_worst_quality;
561 } 655 }
562 active_best_quality = get_gf_active_quality(rc, q); 656 active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth);
563 } else { 657 } else {
564 // Use the lower of active_worst_quality and recent/average Q. 658 // Use the lower of active_worst_quality and recent/average Q.
565 if (cm->current_video_frame > 1) { 659 if (cm->current_video_frame > 1) {
566 if (rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) 660 if (rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality)
567 active_best_quality = rtc_minq[rc->avg_frame_qindex[INTER_FRAME]]; 661 active_best_quality = rtc_minq[rc->avg_frame_qindex[INTER_FRAME]];
568 else 662 else
569 active_best_quality = rtc_minq[active_worst_quality]; 663 active_best_quality = rtc_minq[active_worst_quality];
570 } else { 664 } else {
571 if (rc->avg_frame_qindex[KEY_FRAME] < active_worst_quality) 665 if (rc->avg_frame_qindex[KEY_FRAME] < active_worst_quality)
572 active_best_quality = rtc_minq[rc->avg_frame_qindex[KEY_FRAME]]; 666 active_best_quality = rtc_minq[rc->avg_frame_qindex[KEY_FRAME]];
(...skipping 12 matching lines...) Expand all
585 *bottom_index = active_best_quality; 679 *bottom_index = active_best_quality;
586 680
587 #if LIMIT_QRANGE_FOR_ALTREF_AND_KEY 681 #if LIMIT_QRANGE_FOR_ALTREF_AND_KEY
588 // Limit Q range for the adaptive loop. 682 // Limit Q range for the adaptive loop.
589 if (cm->frame_type == KEY_FRAME && 683 if (cm->frame_type == KEY_FRAME &&
590 !rc->this_key_frame_forced && 684 !rc->this_key_frame_forced &&
591 !(cm->current_video_frame == 0)) { 685 !(cm->current_video_frame == 0)) {
592 int qdelta = 0; 686 int qdelta = 0;
593 vp9_clear_system_state(); 687 vp9_clear_system_state();
594 qdelta = vp9_compute_qdelta_by_rate(&cpi->rc, cm->frame_type, 688 qdelta = vp9_compute_qdelta_by_rate(&cpi->rc, cm->frame_type,
595 active_worst_quality, 2.0); 689 active_worst_quality, 2.0,
690 cm->bit_depth);
596 *top_index = active_worst_quality + qdelta; 691 *top_index = active_worst_quality + qdelta;
597 *top_index = (*top_index > *bottom_index) ? *top_index : *bottom_index; 692 *top_index = (*top_index > *bottom_index) ? *top_index : *bottom_index;
598 } 693 }
599 #endif 694 #endif
600 695
601 // Special case code to try and match quality with forced key frames 696 // Special case code to try and match quality with forced key frames
602 if (cm->frame_type == KEY_FRAME && rc->this_key_frame_forced) { 697 if (cm->frame_type == KEY_FRAME && rc->this_key_frame_forced) {
603 q = rc->last_boosted_qindex; 698 q = rc->last_boosted_qindex;
604 } else { 699 } else {
605 q = vp9_rc_regulate_q(cpi, rc->this_frame_target, 700 q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
637 static int rc_pick_q_and_bounds_one_pass_vbr(const VP9_COMP *cpi, 732 static int rc_pick_q_and_bounds_one_pass_vbr(const VP9_COMP *cpi,
638 int *bottom_index, 733 int *bottom_index,
639 int *top_index) { 734 int *top_index) {
640 const VP9_COMMON *const cm = &cpi->common; 735 const VP9_COMMON *const cm = &cpi->common;
641 const RATE_CONTROL *const rc = &cpi->rc; 736 const RATE_CONTROL *const rc = &cpi->rc;
642 const VP9EncoderConfig *const oxcf = &cpi->oxcf; 737 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
643 const int cq_level = get_active_cq_level(rc, oxcf); 738 const int cq_level = get_active_cq_level(rc, oxcf);
644 int active_best_quality; 739 int active_best_quality;
645 int active_worst_quality = calc_active_worst_quality_one_pass_vbr(cpi); 740 int active_worst_quality = calc_active_worst_quality_one_pass_vbr(cpi);
646 int q; 741 int q;
742 int *inter_minq;
743 ASSIGN_MINQ_TABLE(cm->bit_depth, inter_minq);
647 744
648 if (frame_is_intra_only(cm)) { 745 if (frame_is_intra_only(cm)) {
649 746
650 // Handle the special case for key frames forced when we have reached 747 // Handle the special case for key frames forced when we have reached
651 // the maximum key frame interval. Here force the Q to a range 748 // the maximum key frame interval. Here force the Q to a range
652 // based on the ambient Q to reduce the risk of popping. 749 // based on the ambient Q to reduce the risk of popping.
653 if (rc->this_key_frame_forced) { 750 if (rc->this_key_frame_forced) {
654 int qindex = rc->last_boosted_qindex; 751 int qindex = rc->last_boosted_qindex;
655 double last_boosted_q = vp9_convert_qindex_to_q(qindex); 752 double last_boosted_q = vp9_convert_qindex_to_q(qindex, cm->bit_depth);
656 int delta_qindex = vp9_compute_qdelta(rc, last_boosted_q, 753 int delta_qindex = vp9_compute_qdelta(rc, last_boosted_q,
657 last_boosted_q * 0.75); 754 last_boosted_q * 0.75,
755 cm->bit_depth);
658 active_best_quality = MAX(qindex + delta_qindex, rc->best_quality); 756 active_best_quality = MAX(qindex + delta_qindex, rc->best_quality);
659 } else { 757 } else {
660 // not first frame of one pass and kf_boost is set 758 // not first frame of one pass and kf_boost is set
661 double q_adj_factor = 1.0; 759 double q_adj_factor = 1.0;
662 double q_val; 760 double q_val;
663 761
664 active_best_quality = 762 active_best_quality =
665 get_kf_active_quality(rc, rc->avg_frame_qindex[KEY_FRAME]); 763 get_kf_active_quality(rc, rc->avg_frame_qindex[KEY_FRAME],
764 cm->bit_depth);
666 765
667 // Allow somewhat lower kf minq with small image formats. 766 // Allow somewhat lower kf minq with small image formats.
668 if ((cm->width * cm->height) <= (352 * 288)) { 767 if ((cm->width * cm->height) <= (352 * 288)) {
669 q_adj_factor -= 0.25; 768 q_adj_factor -= 0.25;
670 } 769 }
671 770
672 // Convert the adjustment factor to a qindex delta 771 // Convert the adjustment factor to a qindex delta
673 // on active_best_quality. 772 // on active_best_quality.
674 q_val = vp9_convert_qindex_to_q(active_best_quality); 773 q_val = vp9_convert_qindex_to_q(active_best_quality, cm->bit_depth);
675 active_best_quality += vp9_compute_qdelta(rc, q_val, 774 active_best_quality += vp9_compute_qdelta(rc, q_val,
676 q_val * q_adj_factor); 775 q_val * q_adj_factor,
776 cm->bit_depth);
677 } 777 }
678 } else if (!rc->is_src_frame_alt_ref && 778 } else if (!rc->is_src_frame_alt_ref &&
679 (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) { 779 (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
680 // Use the lower of active_worst_quality and recent 780 // Use the lower of active_worst_quality and recent
681 // average Q as basis for GF/ARF best Q limit unless last frame was 781 // average Q as basis for GF/ARF best Q limit unless last frame was
682 // a key frame. 782 // a key frame.
683 if (rc->frames_since_key > 1 && 783 if (rc->frames_since_key > 1 &&
684 rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) { 784 rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) {
685 q = rc->avg_frame_qindex[INTER_FRAME]; 785 q = rc->avg_frame_qindex[INTER_FRAME];
686 } else { 786 } else {
687 q = rc->avg_frame_qindex[KEY_FRAME]; 787 q = rc->avg_frame_qindex[KEY_FRAME];
688 } 788 }
689 // For constrained quality dont allow Q less than the cq level 789 // For constrained quality dont allow Q less than the cq level
690 if (oxcf->rc_mode == VPX_CQ) { 790 if (oxcf->rc_mode == VPX_CQ) {
691 if (q < cq_level) 791 if (q < cq_level)
692 q = cq_level; 792 q = cq_level;
693 793
694 active_best_quality = get_gf_active_quality(rc, q); 794 active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth);
695 795
696 // Constrained quality use slightly lower active best. 796 // Constrained quality use slightly lower active best.
697 active_best_quality = active_best_quality * 15 / 16; 797 active_best_quality = active_best_quality * 15 / 16;
698 798
699 } else if (oxcf->rc_mode == VPX_Q) { 799 } else if (oxcf->rc_mode == VPX_Q) {
700 if (!cpi->refresh_alt_ref_frame) { 800 if (!cpi->refresh_alt_ref_frame) {
701 active_best_quality = cq_level; 801 active_best_quality = cq_level;
702 } else { 802 } else {
703 active_best_quality = get_gf_active_quality(rc, q); 803 active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth);
704 } 804 }
705 } else { 805 } else {
706 active_best_quality = get_gf_active_quality(rc, q); 806 active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth);
707 } 807 }
708 } else { 808 } else {
709 if (oxcf->rc_mode == VPX_Q) { 809 if (oxcf->rc_mode == VPX_Q) {
710 active_best_quality = cq_level; 810 active_best_quality = cq_level;
711 } else { 811 } else {
712 // Use the lower of active_worst_quality and recent/average Q. 812 // Use the lower of active_worst_quality and recent/average Q.
713 if (cm->current_video_frame > 1) 813 if (cm->current_video_frame > 1)
714 active_best_quality = inter_minq[rc->avg_frame_qindex[INTER_FRAME]]; 814 active_best_quality = inter_minq[rc->avg_frame_qindex[INTER_FRAME]];
715 else 815 else
716 active_best_quality = inter_minq[rc->avg_frame_qindex[KEY_FRAME]]; 816 active_best_quality = inter_minq[rc->avg_frame_qindex[KEY_FRAME]];
(...skipping 18 matching lines...) Expand all
735 #if LIMIT_QRANGE_FOR_ALTREF_AND_KEY 835 #if LIMIT_QRANGE_FOR_ALTREF_AND_KEY
736 { 836 {
737 int qdelta = 0; 837 int qdelta = 0;
738 vp9_clear_system_state(); 838 vp9_clear_system_state();
739 839
740 // Limit Q range for the adaptive loop. 840 // Limit Q range for the adaptive loop.
741 if (cm->frame_type == KEY_FRAME && 841 if (cm->frame_type == KEY_FRAME &&
742 !rc->this_key_frame_forced && 842 !rc->this_key_frame_forced &&
743 !(cm->current_video_frame == 0)) { 843 !(cm->current_video_frame == 0)) {
744 qdelta = vp9_compute_qdelta_by_rate(&cpi->rc, cm->frame_type, 844 qdelta = vp9_compute_qdelta_by_rate(&cpi->rc, cm->frame_type,
745 active_worst_quality, 2.0); 845 active_worst_quality, 2.0,
846 cm->bit_depth);
746 } else if (!rc->is_src_frame_alt_ref && 847 } else if (!rc->is_src_frame_alt_ref &&
747 (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) { 848 (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
748 qdelta = vp9_compute_qdelta_by_rate(&cpi->rc, cm->frame_type, 849 qdelta = vp9_compute_qdelta_by_rate(&cpi->rc, cm->frame_type,
749 active_worst_quality, 1.75); 850 active_worst_quality, 1.75,
851 cm->bit_depth);
750 } 852 }
751 *top_index = active_worst_quality + qdelta; 853 *top_index = active_worst_quality + qdelta;
752 *top_index = (*top_index > *bottom_index) ? *top_index : *bottom_index; 854 *top_index = (*top_index > *bottom_index) ? *top_index : *bottom_index;
753 } 855 }
754 #endif 856 #endif
755 857
756 if (oxcf->rc_mode == VPX_Q) { 858 if (oxcf->rc_mode == VPX_Q) {
757 q = active_best_quality; 859 q = active_best_quality;
758 // Special case code to try and match quality with forced key frames 860 // Special case code to try and match quality with forced key frames
759 } else if ((cm->frame_type == KEY_FRAME) && rc->this_key_frame_forced) { 861 } else if ((cm->frame_type == KEY_FRAME) && rc->this_key_frame_forced) {
(...skipping 11 matching lines...) Expand all
771 } 873 }
772 874
773 assert(*top_index <= rc->worst_quality && 875 assert(*top_index <= rc->worst_quality &&
774 *top_index >= rc->best_quality); 876 *top_index >= rc->best_quality);
775 assert(*bottom_index <= rc->worst_quality && 877 assert(*bottom_index <= rc->worst_quality &&
776 *bottom_index >= rc->best_quality); 878 *bottom_index >= rc->best_quality);
777 assert(q <= rc->worst_quality && q >= rc->best_quality); 879 assert(q <= rc->worst_quality && q >= rc->best_quality);
778 return q; 880 return q;
779 } 881 }
780 882
883 #define STATIC_MOTION_THRESH 95
781 static int rc_pick_q_and_bounds_two_pass(const VP9_COMP *cpi, 884 static int rc_pick_q_and_bounds_two_pass(const VP9_COMP *cpi,
782 int *bottom_index, 885 int *bottom_index,
783 int *top_index) { 886 int *top_index) {
784 const VP9_COMMON *const cm = &cpi->common; 887 const VP9_COMMON *const cm = &cpi->common;
785 const RATE_CONTROL *const rc = &cpi->rc; 888 const RATE_CONTROL *const rc = &cpi->rc;
786 const VP9EncoderConfig *const oxcf = &cpi->oxcf; 889 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
787 const int cq_level = get_active_cq_level(rc, oxcf); 890 const int cq_level = get_active_cq_level(rc, oxcf);
788 int active_best_quality; 891 int active_best_quality;
789 int active_worst_quality = cpi->twopass.active_worst_quality; 892 int active_worst_quality = cpi->twopass.active_worst_quality;
790 int q; 893 int q;
894 int *inter_minq;
895 ASSIGN_MINQ_TABLE(cm->bit_depth, inter_minq);
791 896
792 if (frame_is_intra_only(cm) || vp9_is_upper_layer_key_frame(cpi)) { 897 if (frame_is_intra_only(cm) || vp9_is_upper_layer_key_frame(cpi)) {
793 // Handle the special case for key frames forced when we have75 reached 898 // Handle the special case for key frames forced when we have reached
794 // the maximum key frame interval. Here force the Q to a range 899 // the maximum key frame interval. Here force the Q to a range
795 // based on the ambient Q to reduce the risk of popping. 900 // based on the ambient Q to reduce the risk of popping.
796 if (rc->this_key_frame_forced) { 901 if (rc->this_key_frame_forced) {
797 int qindex = rc->last_boosted_qindex; 902 double last_boosted_q;
798 double last_boosted_q = vp9_convert_qindex_to_q(qindex); 903 int delta_qindex;
799 int delta_qindex = vp9_compute_qdelta(rc, last_boosted_q, 904 int qindex;
800 last_boosted_q * 0.75); 905
801 active_best_quality = MAX(qindex + delta_qindex, rc->best_quality); 906 if (cpi->twopass.last_kfgroup_zeromotion_pct >= STATIC_MOTION_THRESH) {
907 qindex = MIN(rc->last_kf_qindex, rc->last_boosted_qindex);
908 active_best_quality = qindex;
909 last_boosted_q = vp9_convert_qindex_to_q(qindex, cm->bit_depth);
910 delta_qindex = vp9_compute_qdelta(rc, last_boosted_q,
911 last_boosted_q * 1.25,
912 cm->bit_depth);
913 active_worst_quality = MIN(qindex + delta_qindex, active_worst_quality);
914
915 } else {
916 qindex = rc->last_boosted_qindex;
917 last_boosted_q = vp9_convert_qindex_to_q(qindex, cm->bit_depth);
918 delta_qindex = vp9_compute_qdelta(rc, last_boosted_q,
919 last_boosted_q * 0.75,
920 cm->bit_depth);
921 active_best_quality = MAX(qindex + delta_qindex, rc->best_quality);
922 }
802 } else { 923 } else {
803 // Not forced keyframe. 924 // Not forced keyframe.
804 double q_adj_factor = 1.0; 925 double q_adj_factor = 1.0;
805 double q_val; 926 double q_val;
806 // Baseline value derived from cpi->active_worst_quality and kf boost. 927 // Baseline value derived from cpi->active_worst_quality and kf boost.
807 active_best_quality = get_kf_active_quality(rc, active_worst_quality); 928 active_best_quality = get_kf_active_quality(rc, active_worst_quality,
929 cm->bit_depth);
808 930
809 // Allow somewhat lower kf minq with small image formats. 931 // Allow somewhat lower kf minq with small image formats.
810 if ((cm->width * cm->height) <= (352 * 288)) { 932 if ((cm->width * cm->height) <= (352 * 288)) {
811 q_adj_factor -= 0.25; 933 q_adj_factor -= 0.25;
812 } 934 }
813 935
814 // Make a further adjustment based on the kf zero motion measure. 936 // Make a further adjustment based on the kf zero motion measure.
815 q_adj_factor += 0.05 - (0.001 * (double)cpi->twopass.kf_zeromotion_pct); 937 q_adj_factor += 0.05 - (0.001 * (double)cpi->twopass.kf_zeromotion_pct);
816 938
817 // Convert the adjustment factor to a qindex delta 939 // Convert the adjustment factor to a qindex delta
818 // on active_best_quality. 940 // on active_best_quality.
819 q_val = vp9_convert_qindex_to_q(active_best_quality); 941 q_val = vp9_convert_qindex_to_q(active_best_quality, cm->bit_depth);
820 active_best_quality += vp9_compute_qdelta(rc, q_val, 942 active_best_quality += vp9_compute_qdelta(rc, q_val,
821 q_val * q_adj_factor); 943 q_val * q_adj_factor,
944 cm->bit_depth);
822 } 945 }
823 } else if (!rc->is_src_frame_alt_ref && 946 } else if (!rc->is_src_frame_alt_ref &&
824 (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) { 947 (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
825 // Use the lower of active_worst_quality and recent 948 // Use the lower of active_worst_quality and recent
826 // average Q as basis for GF/ARF best Q limit unless last frame was 949 // average Q as basis for GF/ARF best Q limit unless last frame was
827 // a key frame. 950 // a key frame.
828 if (rc->frames_since_key > 1 && 951 if (rc->frames_since_key > 1 &&
829 rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) { 952 rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) {
830 q = rc->avg_frame_qindex[INTER_FRAME]; 953 q = rc->avg_frame_qindex[INTER_FRAME];
831 } else { 954 } else {
832 q = active_worst_quality; 955 q = active_worst_quality;
833 } 956 }
834 // For constrained quality dont allow Q less than the cq level 957 // For constrained quality dont allow Q less than the cq level
835 if (oxcf->rc_mode == VPX_CQ) { 958 if (oxcf->rc_mode == VPX_CQ) {
836 if (q < cq_level) 959 if (q < cq_level)
837 q = cq_level; 960 q = cq_level;
838 961
839 active_best_quality = get_gf_active_quality(rc, q); 962 active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth);
840 963
841 // Constrained quality use slightly lower active best. 964 // Constrained quality use slightly lower active best.
842 active_best_quality = active_best_quality * 15 / 16; 965 active_best_quality = active_best_quality * 15 / 16;
843 966
844 } else if (oxcf->rc_mode == VPX_Q) { 967 } else if (oxcf->rc_mode == VPX_Q) {
845 if (!cpi->refresh_alt_ref_frame) { 968 if (!cpi->refresh_alt_ref_frame) {
846 active_best_quality = cq_level; 969 active_best_quality = cq_level;
847 } else { 970 } else {
848 active_best_quality = get_gf_active_quality(rc, q); 971 active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth);
849 } 972 }
850 } else { 973 } else {
851 active_best_quality = get_gf_active_quality(rc, q); 974 active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth);
852 } 975 }
853 } else { 976 } else {
854 if (oxcf->rc_mode == VPX_Q) { 977 if (oxcf->rc_mode == VPX_Q) {
855 active_best_quality = cq_level; 978 active_best_quality = cq_level;
856 } else { 979 } else {
857 active_best_quality = inter_minq[active_worst_quality]; 980 active_best_quality = inter_minq[active_worst_quality];
858 981
859 // For the constrained quality mode we don't want 982 // For the constrained quality mode we don't want
860 // q to fall below the cq level. 983 // q to fall below the cq level.
861 if ((oxcf->rc_mode == VPX_CQ) && 984 if ((oxcf->rc_mode == VPX_CQ) &&
862 (active_best_quality < cq_level)) { 985 (active_best_quality < cq_level)) {
863 active_best_quality = cq_level; 986 active_best_quality = cq_level;
864 } 987 }
865 } 988 }
866 } 989 }
867 990
868 // Clip the active best and worst quality values to limits.
869 active_best_quality = clamp(active_best_quality,
870 rc->best_quality, rc->worst_quality);
871 active_worst_quality = clamp(active_worst_quality,
872 active_best_quality, rc->worst_quality);
873
874 *top_index = active_worst_quality;
875 *bottom_index = active_best_quality;
876
877 #if LIMIT_QRANGE_FOR_ALTREF_AND_KEY 991 #if LIMIT_QRANGE_FOR_ALTREF_AND_KEY
878 vp9_clear_system_state(); 992 vp9_clear_system_state();
879 { 993 // Static forced key frames Q restrictions dealt with elsewhere.
994 if (!((frame_is_intra_only(cm) || vp9_is_upper_layer_key_frame(cpi))) ||
995 !rc->this_key_frame_forced ||
996 (cpi->twopass.last_kfgroup_zeromotion_pct < STATIC_MOTION_THRESH)) {
880 const GF_GROUP *const gf_group = &cpi->twopass.gf_group; 997 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
881 const double rate_factor_deltas[RATE_FACTOR_LEVELS] = { 998 const double rate_factor_deltas[RATE_FACTOR_LEVELS] = {
882 1.00, // INTER_NORMAL 999 1.00, // INTER_NORMAL
883 1.00, // INTER_HIGH 1000 1.00, // INTER_HIGH
884 1.50, // GF_ARF_LOW 1001 1.50, // GF_ARF_LOW
885 1.75, // GF_ARF_STD 1002 1.75, // GF_ARF_STD
886 2.00, // KF_STD 1003 2.00, // KF_STD
887 }; 1004 };
888 const double rate_factor = 1005 const double rate_factor =
889 rate_factor_deltas[gf_group->rf_level[gf_group->index]]; 1006 rate_factor_deltas[gf_group->rf_level[gf_group->index]];
890 int qdelta = vp9_compute_qdelta_by_rate(&cpi->rc, cm->frame_type, 1007 int qdelta = vp9_compute_qdelta_by_rate(&cpi->rc, cm->frame_type,
891 active_worst_quality, rate_factor); 1008 active_worst_quality, rate_factor,
892 *top_index = active_worst_quality + qdelta; 1009 cm->bit_depth);
893 *top_index = (*top_index > *bottom_index) ? *top_index : *bottom_index; 1010 active_worst_quality = active_worst_quality + qdelta;
1011 active_worst_quality = MAX(active_worst_quality, active_best_quality);
894 } 1012 }
895 #endif 1013 #endif
896 1014
1015 // Clip the active best and worst quality values to limits.
1016 active_best_quality = clamp(active_best_quality,
1017 rc->best_quality, rc->worst_quality);
1018 active_worst_quality = clamp(active_worst_quality,
1019 active_best_quality, rc->worst_quality);
1020
897 if (oxcf->rc_mode == VPX_Q) { 1021 if (oxcf->rc_mode == VPX_Q) {
898 q = active_best_quality; 1022 q = active_best_quality;
899 // Special case code to try and match quality with forced key frames. 1023 // Special case code to try and match quality with forced key frames.
900 } else if ((cm->frame_type == KEY_FRAME) && rc->this_key_frame_forced) { 1024 } else if ((frame_is_intra_only(cm) || vp9_is_upper_layer_key_frame(cpi)) &&
901 q = rc->last_boosted_qindex; 1025 rc->this_key_frame_forced) {
1026 // If static since last kf use better of last boosted and last kf q.
1027 if (cpi->twopass.last_kfgroup_zeromotion_pct >= STATIC_MOTION_THRESH) {
1028 q = MIN(rc->last_kf_qindex, rc->last_boosted_qindex);
1029 } else {
1030 q = rc->last_boosted_qindex;
1031 }
902 } else { 1032 } else {
903 q = vp9_rc_regulate_q(cpi, rc->this_frame_target, 1033 q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
904 active_best_quality, active_worst_quality); 1034 active_best_quality, active_worst_quality);
905 if (q > *top_index) { 1035 if (q > active_worst_quality) {
906 // Special case when we are targeting the max allowed rate. 1036 // Special case when we are targeting the max allowed rate.
907 if (rc->this_frame_target >= rc->max_frame_bandwidth) 1037 if (rc->this_frame_target >= rc->max_frame_bandwidth)
908 *top_index = q; 1038 active_worst_quality = q;
909 else 1039 else
910 q = *top_index; 1040 q = active_worst_quality;
911 } 1041 }
912 } 1042 }
1043 clamp(q, active_best_quality, active_worst_quality);
1044
1045 *top_index = active_worst_quality;
1046 *bottom_index = active_best_quality;
913 1047
914 assert(*top_index <= rc->worst_quality && 1048 assert(*top_index <= rc->worst_quality &&
915 *top_index >= rc->best_quality); 1049 *top_index >= rc->best_quality);
916 assert(*bottom_index <= rc->worst_quality && 1050 assert(*bottom_index <= rc->worst_quality &&
917 *bottom_index >= rc->best_quality); 1051 *bottom_index >= rc->best_quality);
918 assert(q <= rc->worst_quality && q >= rc->best_quality); 1052 assert(q <= rc->worst_quality && q >= rc->best_quality);
919 return q; 1053 return q;
920 } 1054 }
921 1055
922 int vp9_rc_pick_q_and_bounds(const VP9_COMP *cpi, 1056 int vp9_rc_pick_q_and_bounds(const VP9_COMP *cpi,
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
1031 rc->avg_frame_qindex[KEY_FRAME] = 1165 rc->avg_frame_qindex[KEY_FRAME] =
1032 ROUND_POWER_OF_TWO(3 * rc->avg_frame_qindex[KEY_FRAME] + qindex, 2); 1166 ROUND_POWER_OF_TWO(3 * rc->avg_frame_qindex[KEY_FRAME] + qindex, 2);
1033 } else { 1167 } else {
1034 if (rc->is_src_frame_alt_ref || 1168 if (rc->is_src_frame_alt_ref ||
1035 !(cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame) || 1169 !(cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame) ||
1036 (cpi->use_svc && oxcf->rc_mode == VPX_CBR)) { 1170 (cpi->use_svc && oxcf->rc_mode == VPX_CBR)) {
1037 rc->last_q[INTER_FRAME] = qindex; 1171 rc->last_q[INTER_FRAME] = qindex;
1038 rc->avg_frame_qindex[INTER_FRAME] = 1172 rc->avg_frame_qindex[INTER_FRAME] =
1039 ROUND_POWER_OF_TWO(3 * rc->avg_frame_qindex[INTER_FRAME] + qindex, 2); 1173 ROUND_POWER_OF_TWO(3 * rc->avg_frame_qindex[INTER_FRAME] + qindex, 2);
1040 rc->ni_frames++; 1174 rc->ni_frames++;
1041 rc->tot_q += vp9_convert_qindex_to_q(qindex); 1175 rc->tot_q += vp9_convert_qindex_to_q(qindex, cm->bit_depth);
1042 rc->avg_q = rc->tot_q / rc->ni_frames; 1176 rc->avg_q = rc->tot_q / rc->ni_frames;
1043 // Calculate the average Q for normal inter frames (not key or GFU 1177 // Calculate the average Q for normal inter frames (not key or GFU
1044 // frames). 1178 // frames).
1045 rc->ni_tot_qi += qindex; 1179 rc->ni_tot_qi += qindex;
1046 rc->ni_av_qi = rc->ni_tot_qi / rc->ni_frames; 1180 rc->ni_av_qi = rc->ni_tot_qi / rc->ni_frames;
1047 } 1181 }
1048 } 1182 }
1049 1183
1050 // Keep record of last boosted (KF/KF/ARF) Q value. 1184 // Keep record of last boosted (KF/KF/ARF) Q value.
1051 // If the current frame is coded at a lower Q then we also update it. 1185 // If the current frame is coded at a lower Q then we also update it.
1052 // If all mbs in this group are skipped only update if the Q value is 1186 // If all mbs in this group are skipped only update if the Q value is
1053 // better than that already stored. 1187 // better than that already stored.
1054 // This is used to help set quality in forced key frames to reduce popping 1188 // This is used to help set quality in forced key frames to reduce popping
1055 if ((qindex < rc->last_boosted_qindex) || 1189 if ((qindex < rc->last_boosted_qindex) ||
1056 ((cpi->static_mb_pct < 100) && 1190 (((cm->frame_type == KEY_FRAME) || cpi->refresh_alt_ref_frame ||
1057 ((cm->frame_type == KEY_FRAME) || cpi->refresh_alt_ref_frame ||
1058 (cpi->refresh_golden_frame && !rc->is_src_frame_alt_ref)))) { 1191 (cpi->refresh_golden_frame && !rc->is_src_frame_alt_ref)))) {
1059 rc->last_boosted_qindex = qindex; 1192 rc->last_boosted_qindex = qindex;
1060 } 1193 }
1194 if (cm->frame_type == KEY_FRAME)
1195 rc->last_kf_qindex = qindex;
1061 1196
1062 update_buffer_level(cpi, rc->projected_frame_size); 1197 update_buffer_level(cpi, rc->projected_frame_size);
1063 1198
1064 // Rolling monitors of whether we are over or underspending used to help 1199 // Rolling monitors of whether we are over or underspending used to help
1065 // regulate min and Max Q in two pass. 1200 // regulate min and Max Q in two pass.
1066 if (cm->frame_type != KEY_FRAME) { 1201 if (cm->frame_type != KEY_FRAME) {
1067 rc->rolling_target_bits = ROUND_POWER_OF_TWO( 1202 rc->rolling_target_bits = ROUND_POWER_OF_TWO(
1068 rc->rolling_target_bits * 3 + rc->this_frame_target, 2); 1203 rc->rolling_target_bits * 3 + rc->this_frame_target, 2);
1069 rc->rolling_actual_bits = ROUND_POWER_OF_TWO( 1204 rc->rolling_actual_bits = ROUND_POWER_OF_TWO(
1070 rc->rolling_actual_bits * 3 + rc->projected_frame_size, 2); 1205 rc->rolling_actual_bits * 3 + rc->projected_frame_size, 2);
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after
1287 } else { 1422 } else {
1288 cm->frame_type = INTER_FRAME; 1423 cm->frame_type = INTER_FRAME;
1289 target = calc_pframe_target_size_one_pass_cbr(cpi); 1424 target = calc_pframe_target_size_one_pass_cbr(cpi);
1290 } 1425 }
1291 vp9_rc_set_frame_target(cpi, target); 1426 vp9_rc_set_frame_target(cpi, target);
1292 // Don't use gf_update by default in CBR mode. 1427 // Don't use gf_update by default in CBR mode.
1293 rc->frames_till_gf_update_due = INT_MAX; 1428 rc->frames_till_gf_update_due = INT_MAX;
1294 rc->baseline_gf_interval = INT_MAX; 1429 rc->baseline_gf_interval = INT_MAX;
1295 } 1430 }
1296 1431
1297 int vp9_compute_qdelta(const RATE_CONTROL *rc, double qstart, double qtarget) { 1432 int vp9_compute_qdelta(const RATE_CONTROL *rc, double qstart, double qtarget,
1433 vpx_bit_depth_t bit_depth) {
1298 int start_index = rc->worst_quality; 1434 int start_index = rc->worst_quality;
1299 int target_index = rc->worst_quality; 1435 int target_index = rc->worst_quality;
1300 int i; 1436 int i;
1301 1437
1302 // Convert the average q value to an index. 1438 // Convert the average q value to an index.
1303 for (i = rc->best_quality; i < rc->worst_quality; ++i) { 1439 for (i = rc->best_quality; i < rc->worst_quality; ++i) {
1304 start_index = i; 1440 start_index = i;
1305 if (vp9_convert_qindex_to_q(i) >= qstart) 1441 if (vp9_convert_qindex_to_q(i, bit_depth) >= qstart)
1306 break; 1442 break;
1307 } 1443 }
1308 1444
1309 // Convert the q target to an index 1445 // Convert the q target to an index
1310 for (i = rc->best_quality; i < rc->worst_quality; ++i) { 1446 for (i = rc->best_quality; i < rc->worst_quality; ++i) {
1311 target_index = i; 1447 target_index = i;
1312 if (vp9_convert_qindex_to_q(i) >= qtarget) 1448 if (vp9_convert_qindex_to_q(i, bit_depth) >= qtarget)
1313 break; 1449 break;
1314 } 1450 }
1315 1451
1316 return target_index - start_index; 1452 return target_index - start_index;
1317 } 1453 }
1318 1454
1319 int vp9_compute_qdelta_by_rate(const RATE_CONTROL *rc, FRAME_TYPE frame_type, 1455 int vp9_compute_qdelta_by_rate(const RATE_CONTROL *rc, FRAME_TYPE frame_type,
1320 int qindex, double rate_target_ratio) { 1456 int qindex, double rate_target_ratio,
1457 vpx_bit_depth_t bit_depth) {
1321 int target_index = rc->worst_quality; 1458 int target_index = rc->worst_quality;
1322 int i; 1459 int i;
1323 1460
1324 // Look up the current projected bits per block for the base index 1461 // Look up the current projected bits per block for the base index
1325 const int base_bits_per_mb = vp9_rc_bits_per_mb(frame_type, qindex, 1.0); 1462 const int base_bits_per_mb = vp9_rc_bits_per_mb(frame_type, qindex, 1.0,
1463 bit_depth);
1326 1464
1327 // Find the target bits per mb based on the base value and given ratio. 1465 // Find the target bits per mb based on the base value and given ratio.
1328 const int target_bits_per_mb = (int)(rate_target_ratio * base_bits_per_mb); 1466 const int target_bits_per_mb = (int)(rate_target_ratio * base_bits_per_mb);
1329 1467
1330 // Convert the q target to an index 1468 // Convert the q target to an index
1331 for (i = rc->best_quality; i < rc->worst_quality; ++i) { 1469 for (i = rc->best_quality; i < rc->worst_quality; ++i) {
1332 target_index = i; 1470 target_index = i;
1333 if (vp9_rc_bits_per_mb(frame_type, i, 1.0) <= target_bits_per_mb ) 1471 if (vp9_rc_bits_per_mb(frame_type, i, 1.0, bit_depth) <= target_bits_per_mb)
1334 break; 1472 break;
1335 } 1473 }
1336 1474
1337 return target_index - qindex; 1475 return target_index - qindex;
1338 } 1476 }
1339 1477
1340 void vp9_rc_set_gf_max_interval(const VP9_COMP *const cpi, 1478 void vp9_rc_set_gf_max_interval(const VP9_COMP *const cpi,
1341 RATE_CONTROL *const rc) { 1479 RATE_CONTROL *const rc) {
1342 const VP9EncoderConfig *const oxcf = &cpi->oxcf; 1480 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
1343 // Set Maximum gf/arf interval 1481 // Set Maximum gf/arf interval
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
1376 // a very high rate is given on the command line or the the rate cannnot 1514 // a very high rate is given on the command line or the the rate cannnot
1377 // be acheived because of a user specificed max q (e.g. when the user 1515 // be acheived because of a user specificed max q (e.g. when the user
1378 // specifies lossless encode. 1516 // specifies lossless encode.
1379 vbr_max_bits = (int)(((int64_t)rc->avg_frame_bandwidth * 1517 vbr_max_bits = (int)(((int64_t)rc->avg_frame_bandwidth *
1380 oxcf->two_pass_vbrmax_section) / 100); 1518 oxcf->two_pass_vbrmax_section) / 100);
1381 rc->max_frame_bandwidth = MAX(MAX((cm->MBs * MAX_MB_RATE), MAXRATE_1080P), 1519 rc->max_frame_bandwidth = MAX(MAX((cm->MBs * MAX_MB_RATE), MAXRATE_1080P),
1382 vbr_max_bits); 1520 vbr_max_bits);
1383 1521
1384 vp9_rc_set_gf_max_interval(cpi, rc); 1522 vp9_rc_set_gf_max_interval(cpi, rc);
1385 } 1523 }
OLDNEW
« no previous file with comments | « source/libvpx/vp9/encoder/vp9_ratectrl.h ('k') | source/libvpx/vp9/encoder/vp9_rd.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698