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

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

Issue 341293003: libvpx: Pull from upstream (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/deps/third_party/libvpx/
Patch Set: Created 6 years, 6 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_rdopt.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 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
152 int current_temporal_layer = svc->temporal_layer_id; 152 int current_temporal_layer = svc->temporal_layer_id;
153 for (temporal_layer = current_temporal_layer + 1; 153 for (temporal_layer = current_temporal_layer + 1;
154 temporal_layer < svc->number_temporal_layers; ++temporal_layer) { 154 temporal_layer < svc->number_temporal_layers; ++temporal_layer) {
155 LAYER_CONTEXT *lc = &svc->layer_context[temporal_layer]; 155 LAYER_CONTEXT *lc = &svc->layer_context[temporal_layer];
156 RATE_CONTROL *lrc = &lc->rc; 156 RATE_CONTROL *lrc = &lc->rc;
157 int bits_off_for_this_layer = (int)(lc->target_bandwidth / lc->framerate - 157 int bits_off_for_this_layer = (int)(lc->target_bandwidth / lc->framerate -
158 encoded_frame_size); 158 encoded_frame_size);
159 lrc->bits_off_target += bits_off_for_this_layer; 159 lrc->bits_off_target += bits_off_for_this_layer;
160 160
161 // Clip buffer level to maximum buffer size for the layer. 161 // Clip buffer level to maximum buffer size for the layer.
162 lrc->bits_off_target = MIN(lrc->bits_off_target, lc->maximum_buffer_size); 162 lrc->bits_off_target = MIN(lrc->bits_off_target, lrc->maximum_buffer_size);
163 lrc->buffer_level = lrc->bits_off_target; 163 lrc->buffer_level = lrc->bits_off_target;
164 } 164 }
165 } 165 }
166 166
167 // Update the buffer level: leaky bucket model. 167 // Update the buffer level: leaky bucket model.
168 static void update_buffer_level(VP9_COMP *cpi, int encoded_frame_size) { 168 static void update_buffer_level(VP9_COMP *cpi, int encoded_frame_size) {
169 const VP9_COMMON *const cm = &cpi->common; 169 const VP9_COMMON *const cm = &cpi->common;
170 const VP9EncoderConfig *oxcf = &cpi->oxcf;
171 RATE_CONTROL *const rc = &cpi->rc; 170 RATE_CONTROL *const rc = &cpi->rc;
172 171
173 // Non-viewable frames are a special case and are treated as pure overhead. 172 // Non-viewable frames are a special case and are treated as pure overhead.
174 if (!cm->show_frame) { 173 if (!cm->show_frame) {
175 rc->bits_off_target -= encoded_frame_size; 174 rc->bits_off_target -= encoded_frame_size;
176 } else { 175 } else {
177 rc->bits_off_target += rc->avg_frame_bandwidth - encoded_frame_size; 176 rc->bits_off_target += rc->avg_frame_bandwidth - encoded_frame_size;
178 } 177 }
179 178
180 // Clip the buffer level to the maximum specified buffer size. 179 // Clip the buffer level to the maximum specified buffer size.
181 rc->bits_off_target = MIN(rc->bits_off_target, oxcf->maximum_buffer_size); 180 rc->bits_off_target = MIN(rc->bits_off_target, rc->maximum_buffer_size);
182 rc->buffer_level = rc->bits_off_target; 181 rc->buffer_level = rc->bits_off_target;
183 182
184 if (cpi->use_svc && cpi->oxcf.rc_mode == RC_MODE_CBR) { 183 if (cpi->use_svc && cpi->oxcf.rc_mode == VPX_CBR) {
185 update_layer_buffer_level(&cpi->svc, encoded_frame_size); 184 update_layer_buffer_level(&cpi->svc, encoded_frame_size);
186 } 185 }
187 } 186 }
188 187
189 void vp9_rc_init(const VP9EncoderConfig *oxcf, int pass, RATE_CONTROL *rc) { 188 void vp9_rc_init(const VP9EncoderConfig *oxcf, int pass, RATE_CONTROL *rc) {
190 if (pass == 0 && oxcf->rc_mode == RC_MODE_CBR) { 189 if (pass == 0 && oxcf->rc_mode == VPX_CBR) {
191 rc->avg_frame_qindex[0] = oxcf->worst_allowed_q; 190 rc->avg_frame_qindex[KEY_FRAME] = oxcf->worst_allowed_q;
192 rc->avg_frame_qindex[1] = oxcf->worst_allowed_q; 191 rc->avg_frame_qindex[INTER_FRAME] = oxcf->worst_allowed_q;
193 rc->avg_frame_qindex[2] = oxcf->worst_allowed_q;
194 } else { 192 } else {
195 rc->avg_frame_qindex[0] = (oxcf->worst_allowed_q + 193 rc->avg_frame_qindex[KEY_FRAME] = (oxcf->worst_allowed_q +
196 oxcf->best_allowed_q) / 2; 194 oxcf->best_allowed_q) / 2;
197 rc->avg_frame_qindex[1] = (oxcf->worst_allowed_q + 195 rc->avg_frame_qindex[INTER_FRAME] = (oxcf->worst_allowed_q +
198 oxcf->best_allowed_q) / 2; 196 oxcf->best_allowed_q) / 2;
199 rc->avg_frame_qindex[2] = (oxcf->worst_allowed_q +
200 oxcf->best_allowed_q) / 2;
201 } 197 }
202 198
203 rc->last_q[0] = oxcf->best_allowed_q; 199 rc->last_q[KEY_FRAME] = oxcf->best_allowed_q;
204 rc->last_q[1] = oxcf->best_allowed_q; 200 rc->last_q[INTER_FRAME] = oxcf->best_allowed_q;
205 rc->last_q[2] = oxcf->best_allowed_q;
206 201
207 rc->buffer_level = oxcf->starting_buffer_level; 202 rc->buffer_level = rc->starting_buffer_level;
208 rc->bits_off_target = oxcf->starting_buffer_level; 203 rc->bits_off_target = rc->starting_buffer_level;
209 204
210 rc->rolling_target_bits = rc->avg_frame_bandwidth; 205 rc->rolling_target_bits = rc->avg_frame_bandwidth;
211 rc->rolling_actual_bits = rc->avg_frame_bandwidth; 206 rc->rolling_actual_bits = rc->avg_frame_bandwidth;
212 rc->long_rolling_target_bits = rc->avg_frame_bandwidth; 207 rc->long_rolling_target_bits = rc->avg_frame_bandwidth;
213 rc->long_rolling_actual_bits = rc->avg_frame_bandwidth; 208 rc->long_rolling_actual_bits = rc->avg_frame_bandwidth;
214 209
215 rc->total_actual_bits = 0; 210 rc->total_actual_bits = 0;
211 rc->total_target_bits = 0;
216 rc->total_target_vs_actual = 0; 212 rc->total_target_vs_actual = 0;
217 213
218 rc->baseline_gf_interval = DEFAULT_GF_INTERVAL; 214 rc->baseline_gf_interval = DEFAULT_GF_INTERVAL;
219 rc->frames_since_key = 8; // Sensible default for first frame. 215 rc->frames_since_key = 8; // Sensible default for first frame.
220 rc->this_key_frame_forced = 0; 216 rc->this_key_frame_forced = 0;
221 rc->next_key_frame_forced = 0; 217 rc->next_key_frame_forced = 0;
222 rc->source_alt_ref_pending = 0; 218 rc->source_alt_ref_pending = 0;
223 rc->source_alt_ref_active = 0; 219 rc->source_alt_ref_active = 0;
224 220
225 rc->frames_till_gf_update_due = 0; 221 rc->frames_till_gf_update_due = 0;
(...skipping 17 matching lines...) Expand all
243 if (!oxcf->drop_frames_water_mark) { 239 if (!oxcf->drop_frames_water_mark) {
244 return 0; 240 return 0;
245 } else { 241 } else {
246 if (rc->buffer_level < 0) { 242 if (rc->buffer_level < 0) {
247 // Always drop if buffer is below 0. 243 // Always drop if buffer is below 0.
248 return 1; 244 return 1;
249 } else { 245 } else {
250 // If buffer is below drop_mark, for now just drop every other frame 246 // If buffer is below drop_mark, for now just drop every other frame
251 // (starting with the next frame) until it increases back over drop_mark. 247 // (starting with the next frame) until it increases back over drop_mark.
252 int drop_mark = (int)(oxcf->drop_frames_water_mark * 248 int drop_mark = (int)(oxcf->drop_frames_water_mark *
253 oxcf->optimal_buffer_level / 100); 249 rc->optimal_buffer_level / 100);
254 if ((rc->buffer_level > drop_mark) && 250 if ((rc->buffer_level > drop_mark) &&
255 (rc->decimation_factor > 0)) { 251 (rc->decimation_factor > 0)) {
256 --rc->decimation_factor; 252 --rc->decimation_factor;
257 } else if (rc->buffer_level <= drop_mark && 253 } else if (rc->buffer_level <= drop_mark &&
258 rc->decimation_factor == 0) { 254 rc->decimation_factor == 0) {
259 rc->decimation_factor = 1; 255 rc->decimation_factor = 1;
260 } 256 }
261 if (rc->decimation_factor > 0) { 257 if (rc->decimation_factor > 0) {
262 if (rc->decimation_count > 0) { 258 if (rc->decimation_count > 0) {
263 --rc->decimation_count; 259 --rc->decimation_count;
264 return 1; 260 return 1;
265 } else { 261 } else {
266 rc->decimation_count = rc->decimation_factor; 262 rc->decimation_count = rc->decimation_factor;
267 return 0; 263 return 0;
268 } 264 }
269 } else { 265 } else {
270 rc->decimation_count = 0; 266 rc->decimation_count = 0;
271 return 0; 267 return 0;
272 } 268 }
273 } 269 }
274 } 270 }
275 } 271 }
276 272
277 static double get_rate_correction_factor(const VP9_COMP *cpi) { 273 static double get_rate_correction_factor(const VP9_COMP *cpi) {
278 if (cpi->common.frame_type == KEY_FRAME) { 274 if (cpi->common.frame_type == KEY_FRAME) {
279 return cpi->rc.key_frame_rate_correction_factor; 275 return cpi->rc.key_frame_rate_correction_factor;
280 } else { 276 } else {
281 if ((cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) && 277 if ((cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) &&
282 !cpi->rc.is_src_frame_alt_ref && 278 !cpi->rc.is_src_frame_alt_ref &&
283 !(cpi->use_svc && cpi->oxcf.rc_mode == RC_MODE_CBR)) 279 !(cpi->use_svc && cpi->oxcf.rc_mode == VPX_CBR))
284 return cpi->rc.gf_rate_correction_factor; 280 return cpi->rc.gf_rate_correction_factor;
285 else 281 else
286 return cpi->rc.rate_correction_factor; 282 return cpi->rc.rate_correction_factor;
287 } 283 }
288 } 284 }
289 285
290 static void set_rate_correction_factor(VP9_COMP *cpi, double factor) { 286 static void set_rate_correction_factor(VP9_COMP *cpi, double factor) {
291 if (cpi->common.frame_type == KEY_FRAME) { 287 if (cpi->common.frame_type == KEY_FRAME) {
292 cpi->rc.key_frame_rate_correction_factor = factor; 288 cpi->rc.key_frame_rate_correction_factor = factor;
293 } else { 289 } else {
294 if ((cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) && 290 if ((cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) &&
295 !cpi->rc.is_src_frame_alt_ref && 291 !cpi->rc.is_src_frame_alt_ref &&
296 !(cpi->use_svc && cpi->oxcf.rc_mode == RC_MODE_CBR)) 292 !(cpi->use_svc && cpi->oxcf.rc_mode == VPX_CBR))
297 cpi->rc.gf_rate_correction_factor = factor; 293 cpi->rc.gf_rate_correction_factor = factor;
298 else 294 else
299 cpi->rc.rate_correction_factor = factor; 295 cpi->rc.rate_correction_factor = factor;
300 } 296 }
301 } 297 }
302 298
303 void vp9_rc_update_rate_correction_factors(VP9_COMP *cpi, int damp_var) { 299 void vp9_rc_update_rate_correction_factors(VP9_COMP *cpi, int damp_var) {
304 const VP9_COMMON *const cm = &cpi->common; 300 const VP9_COMMON *const cm = &cpi->common;
305 int correction_factor = 100; 301 int correction_factor = 100;
306 double rate_correction_factor = get_rate_correction_factor(cpi); 302 double rate_correction_factor = get_rate_correction_factor(cpi);
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
425 } else { 421 } else {
426 if (!rc->is_src_frame_alt_ref && 422 if (!rc->is_src_frame_alt_ref &&
427 (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) { 423 (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
428 active_worst_quality = curr_frame == 1 ? rc->last_q[KEY_FRAME] * 5 / 4 424 active_worst_quality = curr_frame == 1 ? rc->last_q[KEY_FRAME] * 5 / 4
429 : rc->last_q[INTER_FRAME]; 425 : rc->last_q[INTER_FRAME];
430 } else { 426 } else {
431 active_worst_quality = curr_frame == 1 ? rc->last_q[KEY_FRAME] * 2 427 active_worst_quality = curr_frame == 1 ? rc->last_q[KEY_FRAME] * 2
432 : rc->last_q[INTER_FRAME] * 2; 428 : rc->last_q[INTER_FRAME] * 2;
433 } 429 }
434 } 430 }
435
436 return MIN(active_worst_quality, rc->worst_quality); 431 return MIN(active_worst_quality, rc->worst_quality);
437 } 432 }
438 433
439 // Adjust active_worst_quality level based on buffer level. 434 // Adjust active_worst_quality level based on buffer level.
440 static int calc_active_worst_quality_one_pass_cbr(const VP9_COMP *cpi) { 435 static int calc_active_worst_quality_one_pass_cbr(const VP9_COMP *cpi) {
441 // Adjust active_worst_quality: If buffer is above the optimal/target level, 436 // Adjust active_worst_quality: If buffer is above the optimal/target level,
442 // bring active_worst_quality down depending on fullness of buffer. 437 // bring active_worst_quality down depending on fullness of buffer.
443 // If buffer is below the optimal level, let the active_worst_quality go from 438 // If buffer is below the optimal level, let the active_worst_quality go from
444 // ambient Q (at buffer = optimal level) to worst_quality level 439 // ambient Q (at buffer = optimal level) to worst_quality level
445 // (at buffer = critical level). 440 // (at buffer = critical level).
446 const VP9_COMMON *const cm = &cpi->common; 441 const VP9_COMMON *const cm = &cpi->common;
447 const VP9EncoderConfig *oxcf = &cpi->oxcf;
448 const RATE_CONTROL *rc = &cpi->rc; 442 const RATE_CONTROL *rc = &cpi->rc;
449 // Buffer level below which we push active_worst to worst_quality. 443 // Buffer level below which we push active_worst to worst_quality.
450 int64_t critical_level = oxcf->optimal_buffer_level >> 2; 444 int64_t critical_level = rc->optimal_buffer_level >> 2;
451 int64_t buff_lvl_step = 0; 445 int64_t buff_lvl_step = 0;
452 int adjustment = 0; 446 int adjustment = 0;
453 int active_worst_quality; 447 int active_worst_quality;
454 if (cm->frame_type == KEY_FRAME) 448 if (cm->frame_type == KEY_FRAME)
455 return rc->worst_quality; 449 return rc->worst_quality;
456 if (cm->current_video_frame > 1) 450 if (cm->current_video_frame > 1)
457 active_worst_quality = MIN(rc->worst_quality, 451 active_worst_quality = MIN(rc->worst_quality,
458 rc->avg_frame_qindex[INTER_FRAME] * 5 / 4); 452 rc->avg_frame_qindex[INTER_FRAME] * 5 / 4);
459 else 453 else
460 active_worst_quality = MIN(rc->worst_quality, 454 active_worst_quality = MIN(rc->worst_quality,
461 rc->avg_frame_qindex[KEY_FRAME] * 3 / 2); 455 rc->avg_frame_qindex[KEY_FRAME] * 3 / 2);
462 if (rc->buffer_level > oxcf->optimal_buffer_level) { 456 if (rc->buffer_level > rc->optimal_buffer_level) {
463 // Adjust down. 457 // Adjust down.
464 // Maximum limit for down adjustment, ~30%. 458 // Maximum limit for down adjustment, ~30%.
465 int max_adjustment_down = active_worst_quality / 3; 459 int max_adjustment_down = active_worst_quality / 3;
466 if (max_adjustment_down) { 460 if (max_adjustment_down) {
467 buff_lvl_step = ((oxcf->maximum_buffer_size - 461 buff_lvl_step = ((rc->maximum_buffer_size -
468 oxcf->optimal_buffer_level) / max_adjustment_down); 462 rc->optimal_buffer_level) / max_adjustment_down);
469 if (buff_lvl_step) 463 if (buff_lvl_step)
470 adjustment = (int)((rc->buffer_level - oxcf->optimal_buffer_level) / 464 adjustment = (int)((rc->buffer_level - rc->optimal_buffer_level) /
471 buff_lvl_step); 465 buff_lvl_step);
472 active_worst_quality -= adjustment; 466 active_worst_quality -= adjustment;
473 } 467 }
474 } else if (rc->buffer_level > critical_level) { 468 } else if (rc->buffer_level > critical_level) {
475 // Adjust up from ambient Q. 469 // Adjust up from ambient Q.
476 if (critical_level) { 470 if (critical_level) {
477 buff_lvl_step = (oxcf->optimal_buffer_level - critical_level); 471 buff_lvl_step = (rc->optimal_buffer_level - critical_level);
478 if (buff_lvl_step) { 472 if (buff_lvl_step) {
479 adjustment = 473 adjustment =
480 (int)((rc->worst_quality - rc->avg_frame_qindex[INTER_FRAME]) * 474 (int)((rc->worst_quality - rc->avg_frame_qindex[INTER_FRAME]) *
481 (oxcf->optimal_buffer_level - rc->buffer_level) / 475 (rc->optimal_buffer_level - rc->buffer_level) /
482 buff_lvl_step); 476 buff_lvl_step);
483 } 477 }
484 active_worst_quality = rc->avg_frame_qindex[INTER_FRAME] + adjustment; 478 active_worst_quality = rc->avg_frame_qindex[INTER_FRAME] + adjustment;
485 } 479 }
486 } else { 480 } else {
487 // Set to worst_quality if buffer is below critical level. 481 // Set to worst_quality if buffer is below critical level.
488 active_worst_quality = rc->worst_quality; 482 active_worst_quality = rc->worst_quality;
489 } 483 }
490 return active_worst_quality; 484 return active_worst_quality;
491 } 485 }
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
600 } 594 }
601 } 595 }
602 assert(*top_index <= rc->worst_quality && 596 assert(*top_index <= rc->worst_quality &&
603 *top_index >= rc->best_quality); 597 *top_index >= rc->best_quality);
604 assert(*bottom_index <= rc->worst_quality && 598 assert(*bottom_index <= rc->worst_quality &&
605 *bottom_index >= rc->best_quality); 599 *bottom_index >= rc->best_quality);
606 assert(q <= rc->worst_quality && q >= rc->best_quality); 600 assert(q <= rc->worst_quality && q >= rc->best_quality);
607 return q; 601 return q;
608 } 602 }
609 603
604 static int get_active_cq_level(const RATE_CONTROL *rc,
605 const VP9EncoderConfig *const oxcf) {
606 static const double cq_adjust_threshold = 0.5;
607 int active_cq_level = oxcf->cq_level;
608 if (oxcf->rc_mode == VPX_CQ &&
609 rc->total_target_bits > 0) {
610 const double x = (double)rc->total_actual_bits / rc->total_target_bits;
611 if (x < cq_adjust_threshold) {
612 active_cq_level = (int)(active_cq_level * x / cq_adjust_threshold);
613 }
614 }
615 return active_cq_level;
616 }
617
610 static int rc_pick_q_and_bounds_one_pass_vbr(const VP9_COMP *cpi, 618 static int rc_pick_q_and_bounds_one_pass_vbr(const VP9_COMP *cpi,
611 int *bottom_index, 619 int *bottom_index,
612 int *top_index) { 620 int *top_index) {
613 const VP9_COMMON *const cm = &cpi->common; 621 const VP9_COMMON *const cm = &cpi->common;
614 const RATE_CONTROL *const rc = &cpi->rc; 622 const RATE_CONTROL *const rc = &cpi->rc;
615 const VP9EncoderConfig *const oxcf = &cpi->oxcf; 623 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
616 const int cq_level = oxcf->cq_level; 624 const int cq_level = get_active_cq_level(rc, oxcf);
617 int active_best_quality; 625 int active_best_quality;
618 int active_worst_quality = calc_active_worst_quality_one_pass_vbr(cpi); 626 int active_worst_quality = calc_active_worst_quality_one_pass_vbr(cpi);
619 int q; 627 int q;
620 628
621 if (frame_is_intra_only(cm)) { 629 if (frame_is_intra_only(cm)) {
622 active_best_quality = rc->best_quality; 630 active_best_quality = rc->best_quality;
623 #if !CONFIG_MULTIPLE_ARF 631 #if !CONFIG_MULTIPLE_ARF
624 // Handle the special case for key frames forced when we have75 reached 632 // Handle the special case for key frames forced when we have75 reached
625 // the maximum key frame interval. Here force the Q to a range 633 // the maximum key frame interval. Here force the Q to a range
626 // based on the ambient Q to reduce the risk of popping. 634 // based on the ambient Q to reduce the risk of popping.
627 if (rc->this_key_frame_forced) { 635 if (rc->this_key_frame_forced) {
628 int qindex = rc->last_boosted_qindex; 636 int qindex = rc->last_boosted_qindex;
629 double last_boosted_q = vp9_convert_qindex_to_q(qindex); 637 double last_boosted_q = vp9_convert_qindex_to_q(qindex);
630 int delta_qindex = vp9_compute_qdelta(rc, last_boosted_q, 638 int delta_qindex = vp9_compute_qdelta(rc, last_boosted_q,
631 last_boosted_q * 0.75); 639 last_boosted_q * 0.75);
632 active_best_quality = MAX(qindex + delta_qindex, rc->best_quality); 640 active_best_quality = MAX(qindex + delta_qindex, rc->best_quality);
633 } else if (cm->current_video_frame > 0) { 641 } else {
634 // not first frame of one pass and kf_boost is set 642 // not first frame of one pass and kf_boost is set
635 double q_adj_factor = 1.0; 643 double q_adj_factor = 1.0;
636 double q_val; 644 double q_val;
637 645
638 active_best_quality = get_active_quality(rc->avg_frame_qindex[KEY_FRAME], 646 active_best_quality = get_active_quality(rc->avg_frame_qindex[KEY_FRAME],
639 rc->kf_boost, 647 rc->kf_boost,
640 kf_low, kf_high, 648 kf_low, kf_high,
641 kf_low_motion_minq, 649 kf_low_motion_minq,
642 kf_high_motion_minq); 650 kf_high_motion_minq);
643 651
(...skipping 20 matching lines...) Expand all
664 // Use the lower of active_worst_quality and recent 672 // Use the lower of active_worst_quality and recent
665 // average Q as basis for GF/ARF best Q limit unless last frame was 673 // average Q as basis for GF/ARF best Q limit unless last frame was
666 // a key frame. 674 // a key frame.
667 if (rc->frames_since_key > 1 && 675 if (rc->frames_since_key > 1 &&
668 rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) { 676 rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) {
669 q = rc->avg_frame_qindex[INTER_FRAME]; 677 q = rc->avg_frame_qindex[INTER_FRAME];
670 } else { 678 } else {
671 q = rc->avg_frame_qindex[KEY_FRAME]; 679 q = rc->avg_frame_qindex[KEY_FRAME];
672 } 680 }
673 // For constrained quality dont allow Q less than the cq level 681 // For constrained quality dont allow Q less than the cq level
674 if (oxcf->rc_mode == RC_MODE_CONSTRAINED_QUALITY) { 682 if (oxcf->rc_mode == VPX_CQ) {
675 if (q < cq_level) 683 if (q < cq_level)
676 q = cq_level; 684 q = cq_level;
677 685
678 active_best_quality = get_active_quality(q, rc->gfu_boost, 686 active_best_quality = get_active_quality(q, rc->gfu_boost,
679 gf_low, gf_high, 687 gf_low, gf_high,
680 arfgf_low_motion_minq, 688 arfgf_low_motion_minq,
681 arfgf_high_motion_minq); 689 arfgf_high_motion_minq);
682 690
683 // Constrained quality use slightly lower active best. 691 // Constrained quality use slightly lower active best.
684 active_best_quality = active_best_quality * 15 / 16; 692 active_best_quality = active_best_quality * 15 / 16;
685 693
686 } else if (oxcf->rc_mode == RC_MODE_CONSTANT_QUALITY) { 694 } else if (oxcf->rc_mode == VPX_Q) {
687 if (!cpi->refresh_alt_ref_frame) { 695 if (!cpi->refresh_alt_ref_frame) {
688 active_best_quality = cq_level; 696 active_best_quality = cq_level;
689 } else { 697 } else {
690 active_best_quality = get_active_quality( 698 active_best_quality = get_active_quality(
691 q, rc->gfu_boost, gf_low, gf_high, 699 q, rc->gfu_boost, gf_low, gf_high,
692 arfgf_low_motion_minq, arfgf_high_motion_minq); 700 arfgf_low_motion_minq, arfgf_high_motion_minq);
693 } 701 }
694 } else { 702 } else {
695 active_best_quality = get_active_quality( 703 active_best_quality = get_active_quality(
696 q, rc->gfu_boost, gf_low, gf_high, 704 q, rc->gfu_boost, gf_low, gf_high,
697 arfgf_low_motion_minq, arfgf_high_motion_minq); 705 arfgf_low_motion_minq, arfgf_high_motion_minq);
698 } 706 }
699 } else { 707 } else {
700 if (oxcf->rc_mode == RC_MODE_CONSTANT_QUALITY) { 708 if (oxcf->rc_mode == VPX_Q) {
701 active_best_quality = cq_level; 709 active_best_quality = cq_level;
702 } else { 710 } else {
703 // Use the lower of active_worst_quality and recent/average Q. 711 // Use the lower of active_worst_quality and recent/average Q.
704 if (cm->current_video_frame > 1) 712 if (cm->current_video_frame > 1)
705 active_best_quality = inter_minq[rc->avg_frame_qindex[INTER_FRAME]]; 713 active_best_quality = inter_minq[rc->avg_frame_qindex[INTER_FRAME]];
706 else 714 else
707 active_best_quality = inter_minq[rc->avg_frame_qindex[KEY_FRAME]]; 715 active_best_quality = inter_minq[rc->avg_frame_qindex[KEY_FRAME]];
708 // For the constrained quality mode we don't want 716 // For the constrained quality mode we don't want
709 // q to fall below the cq level. 717 // q to fall below the cq level.
710 if ((oxcf->rc_mode == RC_MODE_CONSTRAINED_QUALITY) && 718 if ((oxcf->rc_mode == VPX_CQ) &&
711 (active_best_quality < cq_level)) { 719 (active_best_quality < cq_level)) {
712 active_best_quality = cq_level; 720 active_best_quality = cq_level;
713 } 721 }
714 } 722 }
715 } 723 }
716 724
717 // Clip the active best and worst quality values to limits 725 // Clip the active best and worst quality values to limits
718 active_best_quality = clamp(active_best_quality, 726 active_best_quality = clamp(active_best_quality,
719 rc->best_quality, rc->worst_quality); 727 rc->best_quality, rc->worst_quality);
720 active_worst_quality = clamp(active_worst_quality, 728 active_worst_quality = clamp(active_worst_quality,
(...skipping 16 matching lines...) Expand all
737 } else if (!rc->is_src_frame_alt_ref && 745 } else if (!rc->is_src_frame_alt_ref &&
738 (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) { 746 (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
739 qdelta = vp9_compute_qdelta_by_rate(&cpi->rc, cm->frame_type, 747 qdelta = vp9_compute_qdelta_by_rate(&cpi->rc, cm->frame_type,
740 active_worst_quality, 1.75); 748 active_worst_quality, 1.75);
741 } 749 }
742 *top_index = active_worst_quality + qdelta; 750 *top_index = active_worst_quality + qdelta;
743 *top_index = (*top_index > *bottom_index) ? *top_index : *bottom_index; 751 *top_index = (*top_index > *bottom_index) ? *top_index : *bottom_index;
744 } 752 }
745 #endif 753 #endif
746 754
747 if (oxcf->rc_mode == RC_MODE_CONSTANT_QUALITY) { 755 if (oxcf->rc_mode == VPX_Q) {
748 q = active_best_quality; 756 q = active_best_quality;
749 // Special case code to try and match quality with forced key frames 757 // Special case code to try and match quality with forced key frames
750 } else if ((cm->frame_type == KEY_FRAME) && rc->this_key_frame_forced) { 758 } else if ((cm->frame_type == KEY_FRAME) && rc->this_key_frame_forced) {
751 q = rc->last_boosted_qindex; 759 q = rc->last_boosted_qindex;
752 } else { 760 } else {
753 q = vp9_rc_regulate_q(cpi, rc->this_frame_target, 761 q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
754 active_best_quality, active_worst_quality); 762 active_best_quality, active_worst_quality);
755 if (q > *top_index) { 763 if (q > *top_index) {
756 // Special case when we are targeting the max allowed rate 764 // Special case when we are targeting the max allowed rate
757 if (rc->this_frame_target >= rc->max_frame_bandwidth) 765 if (rc->this_frame_target >= rc->max_frame_bandwidth)
758 *top_index = q; 766 *top_index = q;
759 else 767 else
760 q = *top_index; 768 q = *top_index;
761 } 769 }
762 } 770 }
763 #if CONFIG_MULTIPLE_ARF 771 #if CONFIG_MULTIPLE_ARF
764 // Force the quantizer determined by the coding order pattern. 772 // Force the quantizer determined by the coding order pattern.
765 if (cpi->multi_arf_enabled && (cm->frame_type != KEY_FRAME) && 773 if (cpi->multi_arf_enabled && (cm->frame_type != KEY_FRAME) &&
766 cpi->oxcf.rc_mode != RC_MODE_CONSTANT_QUALITY) { 774 cpi->oxcf.rc_mode != VPX_Q) {
767 double new_q; 775 double new_q;
768 double current_q = vp9_convert_qindex_to_q(active_worst_quality); 776 double current_q = vp9_convert_qindex_to_q(active_worst_quality);
769 int level = cpi->this_frame_weight; 777 int level = cpi->this_frame_weight;
770 assert(level >= 0); 778 assert(level >= 0);
771 new_q = current_q * (1.0 - (0.2 * (cpi->max_arf_level - level))); 779 new_q = current_q * (1.0 - (0.2 * (cpi->max_arf_level - level)));
772 q = active_worst_quality + 780 q = active_worst_quality +
773 vp9_compute_qdelta(rc, current_q, new_q); 781 vp9_compute_qdelta(rc, current_q, new_q);
774 782
775 *bottom_index = q; 783 *bottom_index = q;
776 *top_index = q; 784 *top_index = q;
777 printf("frame:%d q:%d\n", cm->current_video_frame, q); 785 printf("frame:%d q:%d\n", cm->current_video_frame, q);
778 } 786 }
779 #endif 787 #endif
780 assert(*top_index <= rc->worst_quality && 788 assert(*top_index <= rc->worst_quality &&
781 *top_index >= rc->best_quality); 789 *top_index >= rc->best_quality);
782 assert(*bottom_index <= rc->worst_quality && 790 assert(*bottom_index <= rc->worst_quality &&
783 *bottom_index >= rc->best_quality); 791 *bottom_index >= rc->best_quality);
784 assert(q <= rc->worst_quality && q >= rc->best_quality); 792 assert(q <= rc->worst_quality && q >= rc->best_quality);
785 return q; 793 return q;
786 } 794 }
787 795
788 static int rc_pick_q_and_bounds_two_pass(const VP9_COMP *cpi, 796 static int rc_pick_q_and_bounds_two_pass(const VP9_COMP *cpi,
789 int *bottom_index, 797 int *bottom_index,
790 int *top_index) { 798 int *top_index) {
791 const VP9_COMMON *const cm = &cpi->common; 799 const VP9_COMMON *const cm = &cpi->common;
792 const RATE_CONTROL *const rc = &cpi->rc; 800 const RATE_CONTROL *const rc = &cpi->rc;
793 const VP9EncoderConfig *const oxcf = &cpi->oxcf; 801 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
794 const int cq_level = oxcf->cq_level; 802 const int cq_level = get_active_cq_level(rc, oxcf);
795 int active_best_quality; 803 int active_best_quality;
796 int active_worst_quality = cpi->twopass.active_worst_quality; 804 int active_worst_quality = cpi->twopass.active_worst_quality;
797 int q; 805 int q;
798 806
799 if (frame_is_intra_only(cm) || vp9_is_upper_layer_key_frame(cpi)) { 807 if (frame_is_intra_only(cm) || vp9_is_upper_layer_key_frame(cpi)) {
800 #if !CONFIG_MULTIPLE_ARF 808 #if !CONFIG_MULTIPLE_ARF
801 // Handle the special case for key frames forced when we have75 reached 809 // Handle the special case for key frames forced when we have75 reached
802 // the maximum key frame interval. Here force the Q to a range 810 // the maximum key frame interval. Here force the Q to a range
803 // based on the ambient Q to reduce the risk of popping. 811 // based on the ambient Q to reduce the risk of popping.
804 if (rc->this_key_frame_forced) { 812 if (rc->this_key_frame_forced) {
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
844 // Use the lower of active_worst_quality and recent 852 // Use the lower of active_worst_quality and recent
845 // average Q as basis for GF/ARF best Q limit unless last frame was 853 // average Q as basis for GF/ARF best Q limit unless last frame was
846 // a key frame. 854 // a key frame.
847 if (rc->frames_since_key > 1 && 855 if (rc->frames_since_key > 1 &&
848 rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) { 856 rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) {
849 q = rc->avg_frame_qindex[INTER_FRAME]; 857 q = rc->avg_frame_qindex[INTER_FRAME];
850 } else { 858 } else {
851 q = active_worst_quality; 859 q = active_worst_quality;
852 } 860 }
853 // For constrained quality dont allow Q less than the cq level 861 // For constrained quality dont allow Q less than the cq level
854 if (oxcf->rc_mode == RC_MODE_CONSTRAINED_QUALITY) { 862 if (oxcf->rc_mode == VPX_CQ) {
855 if (q < cq_level) 863 if (q < cq_level)
856 q = cq_level; 864 q = cq_level;
857 865
858 active_best_quality = get_active_quality(q, rc->gfu_boost, 866 active_best_quality = get_active_quality(q, rc->gfu_boost,
859 gf_low, gf_high, 867 gf_low, gf_high,
860 arfgf_low_motion_minq, 868 arfgf_low_motion_minq,
861 arfgf_high_motion_minq); 869 arfgf_high_motion_minq);
862 870
863 // Constrained quality use slightly lower active best. 871 // Constrained quality use slightly lower active best.
864 active_best_quality = active_best_quality * 15 / 16; 872 active_best_quality = active_best_quality * 15 / 16;
865 873
866 } else if (oxcf->rc_mode == RC_MODE_CONSTANT_QUALITY) { 874 } else if (oxcf->rc_mode == VPX_Q) {
867 if (!cpi->refresh_alt_ref_frame) { 875 if (!cpi->refresh_alt_ref_frame) {
868 active_best_quality = cq_level; 876 active_best_quality = cq_level;
869 } else { 877 } else {
870 active_best_quality = get_active_quality( 878 active_best_quality = get_active_quality(
871 q, rc->gfu_boost, gf_low, gf_high, 879 q, rc->gfu_boost, gf_low, gf_high,
872 arfgf_low_motion_minq, arfgf_high_motion_minq); 880 arfgf_low_motion_minq, arfgf_high_motion_minq);
873 } 881 }
874 } else { 882 } else {
875 active_best_quality = get_active_quality( 883 active_best_quality = get_active_quality(
876 q, rc->gfu_boost, gf_low, gf_high, 884 q, rc->gfu_boost, gf_low, gf_high,
877 arfgf_low_motion_minq, arfgf_high_motion_minq); 885 arfgf_low_motion_minq, arfgf_high_motion_minq);
878 } 886 }
879 } else { 887 } else {
880 if (oxcf->rc_mode == RC_MODE_CONSTANT_QUALITY) { 888 if (oxcf->rc_mode == VPX_Q) {
881 active_best_quality = cq_level; 889 active_best_quality = cq_level;
882 } else { 890 } else {
883 active_best_quality = inter_minq[active_worst_quality]; 891 active_best_quality = inter_minq[active_worst_quality];
884 892
885 // For the constrained quality mode we don't want 893 // For the constrained quality mode we don't want
886 // q to fall below the cq level. 894 // q to fall below the cq level.
887 if ((oxcf->rc_mode == RC_MODE_CONSTRAINED_QUALITY) && 895 if ((oxcf->rc_mode == VPX_CQ) &&
888 (active_best_quality < cq_level)) { 896 (active_best_quality < cq_level)) {
889 active_best_quality = cq_level; 897 active_best_quality = cq_level;
890 } 898 }
891 } 899 }
892 } 900 }
893 901
894 // Clip the active best and worst quality values to limits. 902 // Clip the active best and worst quality values to limits.
895 active_best_quality = clamp(active_best_quality, 903 active_best_quality = clamp(active_best_quality,
896 rc->best_quality, rc->worst_quality); 904 rc->best_quality, rc->worst_quality);
897 active_worst_quality = clamp(active_worst_quality, 905 active_worst_quality = clamp(active_worst_quality,
898 active_best_quality, rc->worst_quality); 906 active_best_quality, rc->worst_quality);
899 907
900 *top_index = active_worst_quality; 908 *top_index = active_worst_quality;
901 *bottom_index = active_best_quality; 909 *bottom_index = active_best_quality;
902 910
903 #if LIMIT_QRANGE_FOR_ALTREF_AND_KEY 911 #if LIMIT_QRANGE_FOR_ALTREF_AND_KEY
904 { 912 {
905 int qdelta = 0; 913 int qdelta = 0;
906 vp9_clear_system_state(); 914 vp9_clear_system_state();
907 915
908 // Limit Q range for the adaptive loop. 916 // Limit Q range for the adaptive loop.
909 if ((cm->frame_type == KEY_FRAME || vp9_is_upper_layer_key_frame(cpi)) && 917 if ((cm->frame_type == KEY_FRAME || vp9_is_upper_layer_key_frame(cpi)) &&
910 !rc->this_key_frame_forced) { 918 !rc->this_key_frame_forced) {
911 qdelta = vp9_compute_qdelta_by_rate(&cpi->rc, cm->frame_type, 919 qdelta = vp9_compute_qdelta_by_rate(&cpi->rc, cm->frame_type,
912 active_worst_quality, 2.0); 920 active_worst_quality, 2.0);
913 } else if (!rc->is_src_frame_alt_ref && 921 } else if (!rc->is_src_frame_alt_ref &&
914 (oxcf->rc_mode != RC_MODE_CBR) && 922 (oxcf->rc_mode != VPX_CBR) &&
915 (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) { 923 (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
916 qdelta = vp9_compute_qdelta_by_rate(&cpi->rc, cm->frame_type, 924 qdelta = vp9_compute_qdelta_by_rate(&cpi->rc, cm->frame_type,
917 active_worst_quality, 1.75); 925 active_worst_quality, 1.75);
918 } 926 }
919 *top_index = active_worst_quality + qdelta; 927 *top_index = active_worst_quality + qdelta;
920 *top_index = (*top_index > *bottom_index) ? *top_index : *bottom_index; 928 *top_index = (*top_index > *bottom_index) ? *top_index : *bottom_index;
921 } 929 }
922 #endif 930 #endif
923 931
924 if (oxcf->rc_mode == RC_MODE_CONSTANT_QUALITY) { 932 if (oxcf->rc_mode == VPX_Q) {
925 q = active_best_quality; 933 q = active_best_quality;
926 // Special case code to try and match quality with forced key frames. 934 // Special case code to try and match quality with forced key frames.
927 } else if ((cm->frame_type == KEY_FRAME) && rc->this_key_frame_forced) { 935 } else if ((cm->frame_type == KEY_FRAME) && rc->this_key_frame_forced) {
928 q = rc->last_boosted_qindex; 936 q = rc->last_boosted_qindex;
929 } else { 937 } else {
930 q = vp9_rc_regulate_q(cpi, rc->this_frame_target, 938 q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
931 active_best_quality, active_worst_quality); 939 active_best_quality, active_worst_quality);
932 if (q > *top_index) { 940 if (q > *top_index) {
933 // Special case when we are targeting the max allowed rate. 941 // Special case when we are targeting the max allowed rate.
934 if (rc->this_frame_target >= rc->max_frame_bandwidth) 942 if (rc->this_frame_target >= rc->max_frame_bandwidth)
935 *top_index = q; 943 *top_index = q;
936 else 944 else
937 q = *top_index; 945 q = *top_index;
938 } 946 }
939 } 947 }
940 #if CONFIG_MULTIPLE_ARF 948 #if CONFIG_MULTIPLE_ARF
941 // Force the quantizer determined by the coding order pattern. 949 // Force the quantizer determined by the coding order pattern.
942 if (cpi->multi_arf_enabled && (cm->frame_type != KEY_FRAME) && 950 if (cpi->multi_arf_enabled && (cm->frame_type != KEY_FRAME) &&
943 cpi->oxcf.rc_mode != RC_MODE_CONSTANT_QUALITY) { 951 cpi->oxcf.rc_mode != VPX_Q) {
944 double new_q; 952 double new_q;
945 double current_q = vp9_convert_qindex_to_q(active_worst_quality); 953 double current_q = vp9_convert_qindex_to_q(active_worst_quality);
946 int level = cpi->this_frame_weight; 954 int level = cpi->this_frame_weight;
947 assert(level >= 0); 955 assert(level >= 0);
948 new_q = current_q * (1.0 - (0.2 * (cpi->max_arf_level - level))); 956 new_q = current_q * (1.0 - (0.2 * (cpi->max_arf_level - level)));
949 q = active_worst_quality + 957 q = active_worst_quality +
950 vp9_compute_qdelta(rc, current_q, new_q); 958 vp9_compute_qdelta(rc, current_q, new_q);
951 959
952 *bottom_index = q; 960 *bottom_index = q;
953 *top_index = q; 961 *top_index = q;
954 printf("frame:%d q:%d\n", cm->current_video_frame, q); 962 printf("frame:%d q:%d\n", cm->current_video_frame, q);
955 } 963 }
956 #endif 964 #endif
957 assert(*top_index <= rc->worst_quality && 965 assert(*top_index <= rc->worst_quality &&
958 *top_index >= rc->best_quality); 966 *top_index >= rc->best_quality);
959 assert(*bottom_index <= rc->worst_quality && 967 assert(*bottom_index <= rc->worst_quality &&
960 *bottom_index >= rc->best_quality); 968 *bottom_index >= rc->best_quality);
961 assert(q <= rc->worst_quality && q >= rc->best_quality); 969 assert(q <= rc->worst_quality && q >= rc->best_quality);
962 return q; 970 return q;
963 } 971 }
964 972
965 int vp9_rc_pick_q_and_bounds(const VP9_COMP *cpi, 973 int vp9_rc_pick_q_and_bounds(const VP9_COMP *cpi,
966 int *bottom_index, int *top_index) { 974 int *bottom_index, int *top_index) {
967 int q; 975 int q;
968 if (cpi->pass == 0) { 976 if (cpi->pass == 0) {
969 if (cpi->oxcf.rc_mode == RC_MODE_CBR) 977 if (cpi->oxcf.rc_mode == VPX_CBR)
970 q = rc_pick_q_and_bounds_one_pass_cbr(cpi, bottom_index, top_index); 978 q = rc_pick_q_and_bounds_one_pass_cbr(cpi, bottom_index, top_index);
971 else 979 else
972 q = rc_pick_q_and_bounds_one_pass_vbr(cpi, bottom_index, top_index); 980 q = rc_pick_q_and_bounds_one_pass_vbr(cpi, bottom_index, top_index);
973 } else { 981 } else {
974 q = rc_pick_q_and_bounds_two_pass(cpi, bottom_index, top_index); 982 q = rc_pick_q_and_bounds_two_pass(cpi, bottom_index, top_index);
975 } 983 }
976
977 if (cpi->sf.use_nonrd_pick_mode) { 984 if (cpi->sf.use_nonrd_pick_mode) {
978 if (cpi->sf.force_frame_boost == 1) 985 if (cpi->sf.force_frame_boost == 1)
979 q -= cpi->sf.max_delta_qindex; 986 q -= cpi->sf.max_delta_qindex;
980 987
981 if (q < *bottom_index) 988 if (q < *bottom_index)
982 *bottom_index = q; 989 *bottom_index = q;
983 else if (q > *top_index) 990 else if (q > *top_index)
984 *top_index = q; 991 *top_index = q;
985 } 992 }
986 return q; 993 return q;
987 } 994 }
988 995
989 void vp9_rc_compute_frame_size_bounds(const VP9_COMP *cpi, 996 void vp9_rc_compute_frame_size_bounds(const VP9_COMP *cpi,
990 int frame_target, 997 int frame_target,
991 int *frame_under_shoot_limit, 998 int *frame_under_shoot_limit,
992 int *frame_over_shoot_limit) { 999 int *frame_over_shoot_limit) {
993 if (cpi->oxcf.rc_mode == RC_MODE_CONSTANT_QUALITY) { 1000 if (cpi->oxcf.rc_mode == VPX_Q) {
994 *frame_under_shoot_limit = 0; 1001 *frame_under_shoot_limit = 0;
995 *frame_over_shoot_limit = INT_MAX; 1002 *frame_over_shoot_limit = INT_MAX;
996 } else { 1003 } else {
997 // For very small rate targets where the fractional adjustment 1004 // For very small rate targets where the fractional adjustment
998 // may be tiny make sure there is at least a minimum range. 1005 // may be tiny make sure there is at least a minimum range.
999 const int tolerance = (cpi->sf.recode_tolerance * frame_target) / 100; 1006 const int tolerance = (cpi->sf.recode_tolerance * frame_target) / 100;
1000 *frame_under_shoot_limit = MAX(frame_target - tolerance - 200, 0); 1007 *frame_under_shoot_limit = MAX(frame_target - tolerance - 200, 0);
1001 *frame_over_shoot_limit = MIN(frame_target + tolerance + 200, 1008 *frame_over_shoot_limit = MIN(frame_target + tolerance + 200,
1002 cpi->rc.max_frame_bandwidth); 1009 cpi->rc.max_frame_bandwidth);
1003 } 1010 }
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
1058 const VP9EncoderConfig *const oxcf = &cpi->oxcf; 1065 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
1059 RATE_CONTROL *const rc = &cpi->rc; 1066 RATE_CONTROL *const rc = &cpi->rc;
1060 const int qindex = cm->base_qindex; 1067 const int qindex = cm->base_qindex;
1061 1068
1062 // Update rate control heuristics 1069 // Update rate control heuristics
1063 rc->projected_frame_size = (int)(bytes_used << 3); 1070 rc->projected_frame_size = (int)(bytes_used << 3);
1064 1071
1065 // Post encode loop adjustment of Q prediction. 1072 // Post encode loop adjustment of Q prediction.
1066 vp9_rc_update_rate_correction_factors( 1073 vp9_rc_update_rate_correction_factors(
1067 cpi, (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF || 1074 cpi, (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF ||
1068 oxcf->rc_mode == RC_MODE_CBR) ? 2 : 0); 1075 oxcf->rc_mode == VPX_CBR) ? 2 : 0);
1069 1076
1070 // Keep a record of last Q and ambient average Q. 1077 // Keep a record of last Q and ambient average Q.
1071 if (cm->frame_type == KEY_FRAME) { 1078 if (cm->frame_type == KEY_FRAME) {
1072 rc->last_q[KEY_FRAME] = qindex; 1079 rc->last_q[KEY_FRAME] = qindex;
1073 rc->avg_frame_qindex[KEY_FRAME] = 1080 rc->avg_frame_qindex[KEY_FRAME] =
1074 ROUND_POWER_OF_TWO(3 * rc->avg_frame_qindex[KEY_FRAME] + qindex, 2); 1081 ROUND_POWER_OF_TWO(3 * rc->avg_frame_qindex[KEY_FRAME] + qindex, 2);
1075 } else if (!rc->is_src_frame_alt_ref &&
1076 (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame) &&
1077 !(cpi->use_svc && oxcf->rc_mode == RC_MODE_CBR)) {
1078 rc->last_q[2] = qindex;
1079 rc->avg_frame_qindex[2] =
1080 ROUND_POWER_OF_TWO(3 * rc->avg_frame_qindex[2] + qindex, 2);
1081 } else { 1082 } else {
1082 rc->last_q[INTER_FRAME] = qindex; 1083 if (rc->is_src_frame_alt_ref ||
1083 rc->avg_frame_qindex[INTER_FRAME] = 1084 !(cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame) ||
1085 (cpi->use_svc && oxcf->rc_mode == VPX_CBR)) {
1086 rc->last_q[INTER_FRAME] = qindex;
1087 rc->avg_frame_qindex[INTER_FRAME] =
1084 ROUND_POWER_OF_TWO(3 * rc->avg_frame_qindex[INTER_FRAME] + qindex, 2); 1088 ROUND_POWER_OF_TWO(3 * rc->avg_frame_qindex[INTER_FRAME] + qindex, 2);
1085 rc->ni_frames++; 1089 rc->ni_frames++;
1086 rc->tot_q += vp9_convert_qindex_to_q(qindex); 1090 rc->tot_q += vp9_convert_qindex_to_q(qindex);
1087 rc->avg_q = rc->tot_q / rc->ni_frames; 1091 rc->avg_q = rc->tot_q / rc->ni_frames;
1088 // Calculate the average Q for normal inter frames (not key or GFU frames). 1092 // Calculate the average Q for normal inter frames (not key or GFU
1089 rc->ni_tot_qi += qindex; 1093 // frames).
1090 rc->ni_av_qi = rc->ni_tot_qi / rc->ni_frames; 1094 rc->ni_tot_qi += qindex;
1095 rc->ni_av_qi = rc->ni_tot_qi / rc->ni_frames;
1096 }
1091 } 1097 }
1092 1098
1093 // Keep record of last boosted (KF/KF/ARF) Q value. 1099 // Keep record of last boosted (KF/KF/ARF) Q value.
1094 // If the current frame is coded at a lower Q then we also update it. 1100 // If the current frame is coded at a lower Q then we also update it.
1095 // If all mbs in this group are skipped only update if the Q value is 1101 // If all mbs in this group are skipped only update if the Q value is
1096 // better than that already stored. 1102 // better than that already stored.
1097 // This is used to help set quality in forced key frames to reduce popping 1103 // This is used to help set quality in forced key frames to reduce popping
1098 if ((qindex < rc->last_boosted_qindex) || 1104 if ((qindex < rc->last_boosted_qindex) ||
1099 ((cpi->static_mb_pct < 100) && 1105 ((cpi->static_mb_pct < 100) &&
1100 ((cm->frame_type == KEY_FRAME) || cpi->refresh_alt_ref_frame || 1106 ((cm->frame_type == KEY_FRAME) || cpi->refresh_alt_ref_frame ||
(...skipping 15 matching lines...) Expand all
1116 rc->long_rolling_actual_bits = ROUND_POWER_OF_TWO( 1122 rc->long_rolling_actual_bits = ROUND_POWER_OF_TWO(
1117 rc->long_rolling_actual_bits * 31 + rc->projected_frame_size, 5); 1123 rc->long_rolling_actual_bits * 31 + rc->projected_frame_size, 5);
1118 } 1124 }
1119 1125
1120 // Actual bits spent 1126 // Actual bits spent
1121 rc->total_actual_bits += rc->projected_frame_size; 1127 rc->total_actual_bits += rc->projected_frame_size;
1122 rc->total_target_bits += cm->show_frame ? rc->avg_frame_bandwidth : 0; 1128 rc->total_target_bits += cm->show_frame ? rc->avg_frame_bandwidth : 0;
1123 1129
1124 rc->total_target_vs_actual = rc->total_actual_bits - rc->total_target_bits; 1130 rc->total_target_vs_actual = rc->total_actual_bits - rc->total_target_bits;
1125 1131
1126 if (oxcf->play_alternate && cpi->refresh_alt_ref_frame && 1132 if (is_altref_enabled(oxcf) && cpi->refresh_alt_ref_frame &&
1127 (cm->frame_type != KEY_FRAME)) 1133 (cm->frame_type != KEY_FRAME))
1128 // Update the alternate reference frame stats as appropriate. 1134 // Update the alternate reference frame stats as appropriate.
1129 update_alt_ref_frame_stats(cpi); 1135 update_alt_ref_frame_stats(cpi);
1130 else 1136 else
1131 // Update the Golden frame stats as appropriate. 1137 // Update the Golden frame stats as appropriate.
1132 update_golden_frame_stats(cpi); 1138 update_golden_frame_stats(cpi);
1133 1139
1134 if (cm->frame_type == KEY_FRAME) 1140 if (cm->frame_type == KEY_FRAME)
1135 rc->frames_since_key = 0; 1141 rc->frames_since_key = 0;
1136 if (cm->show_frame) { 1142 if (cm->show_frame) {
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
1207 target = calc_iframe_target_size_one_pass_vbr(cpi); 1213 target = calc_iframe_target_size_one_pass_vbr(cpi);
1208 else 1214 else
1209 target = calc_pframe_target_size_one_pass_vbr(cpi); 1215 target = calc_pframe_target_size_one_pass_vbr(cpi);
1210 vp9_rc_set_frame_target(cpi, target); 1216 vp9_rc_set_frame_target(cpi, target);
1211 } 1217 }
1212 1218
1213 static int calc_pframe_target_size_one_pass_cbr(const VP9_COMP *cpi) { 1219 static int calc_pframe_target_size_one_pass_cbr(const VP9_COMP *cpi) {
1214 const VP9EncoderConfig *oxcf = &cpi->oxcf; 1220 const VP9EncoderConfig *oxcf = &cpi->oxcf;
1215 const RATE_CONTROL *rc = &cpi->rc; 1221 const RATE_CONTROL *rc = &cpi->rc;
1216 const SVC *const svc = &cpi->svc; 1222 const SVC *const svc = &cpi->svc;
1217 const int64_t diff = oxcf->optimal_buffer_level - rc->buffer_level; 1223 const int64_t diff = rc->optimal_buffer_level - rc->buffer_level;
1218 const int64_t one_pct_bits = 1 + oxcf->optimal_buffer_level / 100; 1224 const int64_t one_pct_bits = 1 + rc->optimal_buffer_level / 100;
1219 int min_frame_target = MAX(rc->avg_frame_bandwidth >> 4, FRAME_OVERHEAD_BITS); 1225 int min_frame_target = MAX(rc->avg_frame_bandwidth >> 4, FRAME_OVERHEAD_BITS);
1220 int target = rc->avg_frame_bandwidth; 1226 int target = rc->avg_frame_bandwidth;
1221 if (svc->number_temporal_layers > 1 && 1227 if (svc->number_temporal_layers > 1 &&
1222 oxcf->rc_mode == RC_MODE_CBR) { 1228 oxcf->rc_mode == VPX_CBR) {
1223 // Note that for layers, avg_frame_bandwidth is the cumulative 1229 // Note that for layers, avg_frame_bandwidth is the cumulative
1224 // per-frame-bandwidth. For the target size of this frame, use the 1230 // per-frame-bandwidth. For the target size of this frame, use the
1225 // layer average frame size (i.e., non-cumulative per-frame-bw). 1231 // layer average frame size (i.e., non-cumulative per-frame-bw).
1226 int current_temporal_layer = svc->temporal_layer_id; 1232 int current_temporal_layer = svc->temporal_layer_id;
1227 const LAYER_CONTEXT *lc = &svc->layer_context[current_temporal_layer]; 1233 const LAYER_CONTEXT *lc = &svc->layer_context[current_temporal_layer];
1228 target = lc->avg_frame_size; 1234 target = lc->avg_frame_size;
1229 min_frame_target = MAX(lc->avg_frame_size >> 4, FRAME_OVERHEAD_BITS); 1235 min_frame_target = MAX(lc->avg_frame_size >> 4, FRAME_OVERHEAD_BITS);
1230 } 1236 }
1231 if (diff > 0) { 1237 if (diff > 0) {
1232 // Lower the target bandwidth for this frame. 1238 // Lower the target bandwidth for this frame.
1233 const int pct_low = (int)MIN(diff / one_pct_bits, oxcf->under_shoot_pct); 1239 const int pct_low = (int)MIN(diff / one_pct_bits, oxcf->under_shoot_pct);
1234 target -= (target * pct_low) / 200; 1240 target -= (target * pct_low) / 200;
1235 } else if (diff < 0) { 1241 } else if (diff < 0) {
1236 // Increase the target bandwidth for this frame. 1242 // Increase the target bandwidth for this frame.
1237 const int pct_high = (int)MIN(-diff / one_pct_bits, oxcf->over_shoot_pct); 1243 const int pct_high = (int)MIN(-diff / one_pct_bits, oxcf->over_shoot_pct);
1238 target += (target * pct_high) / 200; 1244 target += (target * pct_high) / 200;
1239 } 1245 }
1240 return MAX(min_frame_target, target); 1246 return MAX(min_frame_target, target);
1241 } 1247 }
1242 1248
1243 static int calc_iframe_target_size_one_pass_cbr(const VP9_COMP *cpi) { 1249 static int calc_iframe_target_size_one_pass_cbr(const VP9_COMP *cpi) {
1244 const RATE_CONTROL *rc = &cpi->rc; 1250 const RATE_CONTROL *rc = &cpi->rc;
1245 const VP9EncoderConfig *oxcf = &cpi->oxcf; 1251 const VP9EncoderConfig *oxcf = &cpi->oxcf;
1246 const SVC *const svc = &cpi->svc; 1252 const SVC *const svc = &cpi->svc;
1247 int target; 1253 int target;
1248 if (cpi->common.current_video_frame == 0) { 1254 if (cpi->common.current_video_frame == 0) {
1249 target = ((cpi->oxcf.starting_buffer_level / 2) > INT_MAX) 1255 target = ((rc->starting_buffer_level / 2) > INT_MAX)
1250 ? INT_MAX : (int)(cpi->oxcf.starting_buffer_level / 2); 1256 ? INT_MAX : (int)(rc->starting_buffer_level / 2);
1251 } else { 1257 } else {
1252 int kf_boost = 32; 1258 int kf_boost = 32;
1253 double framerate = oxcf->framerate; 1259 double framerate = oxcf->framerate;
1254 if (svc->number_temporal_layers > 1 && 1260 if (svc->number_temporal_layers > 1 &&
1255 oxcf->rc_mode == RC_MODE_CBR) { 1261 oxcf->rc_mode == VPX_CBR) {
1256 // Use the layer framerate for temporal layers CBR mode. 1262 // Use the layer framerate for temporal layers CBR mode.
1257 const LAYER_CONTEXT *lc = &svc->layer_context[svc->temporal_layer_id]; 1263 const LAYER_CONTEXT *lc = &svc->layer_context[svc->temporal_layer_id];
1258 framerate = lc->framerate; 1264 framerate = lc->framerate;
1259 } 1265 }
1260 kf_boost = MAX(kf_boost, (int)(2 * framerate - 16)); 1266 kf_boost = MAX(kf_boost, (int)(2 * framerate - 16));
1261 if (rc->frames_since_key < framerate / 2) { 1267 if (rc->frames_since_key < framerate / 2) {
1262 kf_boost = (int)(kf_boost * rc->frames_since_key / 1268 kf_boost = (int)(kf_boost * rc->frames_since_key /
1263 (framerate / 2)); 1269 (framerate / 2));
1264 } 1270 }
1265 target = ((16 + kf_boost) * rc->avg_frame_bandwidth) >> 4; 1271 target = ((16 + kf_boost) * rc->avg_frame_bandwidth) >> 4;
1266 } 1272 }
1267 return vp9_rc_clamp_iframe_target_size(cpi, target); 1273 return vp9_rc_clamp_iframe_target_size(cpi, target);
1268 } 1274 }
1269 1275
1270 void vp9_rc_get_svc_params(VP9_COMP *cpi) { 1276 void vp9_rc_get_svc_params(VP9_COMP *cpi) {
1271 VP9_COMMON *const cm = &cpi->common; 1277 VP9_COMMON *const cm = &cpi->common;
1272 RATE_CONTROL *const rc = &cpi->rc; 1278 RATE_CONTROL *const rc = &cpi->rc;
1273 int target = rc->avg_frame_bandwidth; 1279 int target = rc->avg_frame_bandwidth;
1274 if ((cm->current_video_frame == 0) || 1280 if ((cm->current_video_frame == 0) ||
1275 (cpi->frame_flags & FRAMEFLAGS_KEY) || 1281 (cpi->frame_flags & FRAMEFLAGS_KEY) ||
1276 (cpi->oxcf.auto_key && (rc->frames_since_key % 1282 (cpi->oxcf.auto_key && (rc->frames_since_key %
1277 cpi->oxcf.key_freq == 0))) { 1283 cpi->oxcf.key_freq == 0))) {
1278 cm->frame_type = KEY_FRAME; 1284 cm->frame_type = KEY_FRAME;
1279 rc->source_alt_ref_active = 0; 1285 rc->source_alt_ref_active = 0;
1280 1286
1281 if (cpi->use_svc && cpi->svc.number_temporal_layers == 1) { 1287 if (cpi->use_svc && cpi->svc.number_temporal_layers == 1) {
1282 cpi->svc.layer_context[cpi->svc.spatial_layer_id].is_key_frame = 1; 1288 cpi->svc.layer_context[cpi->svc.spatial_layer_id].is_key_frame = 1;
1283 } 1289 }
1284 1290
1285 if (cpi->pass == 0 && cpi->oxcf.rc_mode == RC_MODE_CBR) { 1291 if (cpi->pass == 0 && cpi->oxcf.rc_mode == VPX_CBR) {
1286 target = calc_iframe_target_size_one_pass_cbr(cpi); 1292 target = calc_iframe_target_size_one_pass_cbr(cpi);
1287 } 1293 }
1288 } else { 1294 } else {
1289 cm->frame_type = INTER_FRAME; 1295 cm->frame_type = INTER_FRAME;
1290 1296
1291 if (cpi->use_svc && cpi->svc.number_temporal_layers == 1) { 1297 if (cpi->use_svc && cpi->svc.number_temporal_layers == 1) {
1292 LAYER_CONTEXT *lc = &cpi->svc.layer_context[cpi->svc.spatial_layer_id]; 1298 LAYER_CONTEXT *lc = &cpi->svc.layer_context[cpi->svc.spatial_layer_id];
1293 if (cpi->svc.spatial_layer_id == 0) { 1299 if (cpi->svc.spatial_layer_id == 0) {
1294 lc->is_key_frame = 0; 1300 lc->is_key_frame = 0;
1295 } else { 1301 } else {
1296 lc->is_key_frame = cpi->svc.layer_context[0].is_key_frame; 1302 lc->is_key_frame = cpi->svc.layer_context[0].is_key_frame;
1297 } 1303 }
1298 } 1304 }
1299 1305
1300 if (cpi->pass == 0 && cpi->oxcf.rc_mode == RC_MODE_CBR) { 1306 if (cpi->pass == 0 && cpi->oxcf.rc_mode == VPX_CBR) {
1301 target = calc_pframe_target_size_one_pass_cbr(cpi); 1307 target = calc_pframe_target_size_one_pass_cbr(cpi);
1302 } 1308 }
1303 } 1309 }
1304 vp9_rc_set_frame_target(cpi, target); 1310 vp9_rc_set_frame_target(cpi, target);
1305 rc->frames_till_gf_update_due = INT_MAX; 1311 rc->frames_till_gf_update_due = INT_MAX;
1306 rc->baseline_gf_interval = INT_MAX; 1312 rc->baseline_gf_interval = INT_MAX;
1307 } 1313 }
1308 1314
1309 void vp9_rc_get_one_pass_cbr_params(VP9_COMP *cpi) { 1315 void vp9_rc_get_one_pass_cbr_params(VP9_COMP *cpi) {
1310 VP9_COMMON *const cm = &cpi->common; 1316 VP9_COMMON *const cm = &cpi->common;
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
1368 // Convert the q target to an index 1374 // Convert the q target to an index
1369 for (i = rc->best_quality; i < rc->worst_quality; ++i) { 1375 for (i = rc->best_quality; i < rc->worst_quality; ++i) {
1370 target_index = i; 1376 target_index = i;
1371 if (vp9_rc_bits_per_mb(frame_type, i, 1.0) <= target_bits_per_mb ) 1377 if (vp9_rc_bits_per_mb(frame_type, i, 1.0) <= target_bits_per_mb )
1372 break; 1378 break;
1373 } 1379 }
1374 1380
1375 return target_index - qindex; 1381 return target_index - qindex;
1376 } 1382 }
1377 1383
1384 void vp9_rc_set_gf_max_interval(const VP9EncoderConfig *const oxcf,
1385 RATE_CONTROL *const rc) {
1386 // Set Maximum gf/arf interval
1387 rc->max_gf_interval = 16;
1388
1389 // Extended interval for genuinely static scenes
1390 rc->static_scene_max_gf_interval = oxcf->key_freq >> 1;
1391
1392 if (is_altref_enabled(oxcf)) {
1393 if (rc->static_scene_max_gf_interval > oxcf->lag_in_frames - 1)
1394 rc->static_scene_max_gf_interval = oxcf->lag_in_frames - 1;
1395 }
1396
1397 if (rc->max_gf_interval > rc->static_scene_max_gf_interval)
1398 rc->max_gf_interval = rc->static_scene_max_gf_interval;
1399 }
1400
1378 void vp9_rc_update_framerate(VP9_COMP *cpi) { 1401 void vp9_rc_update_framerate(VP9_COMP *cpi) {
1379 const VP9_COMMON *const cm = &cpi->common; 1402 const VP9_COMMON *const cm = &cpi->common;
1380 const VP9EncoderConfig *const oxcf = &cpi->oxcf; 1403 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
1381 RATE_CONTROL *const rc = &cpi->rc; 1404 RATE_CONTROL *const rc = &cpi->rc;
1382 int vbr_max_bits; 1405 int vbr_max_bits;
1383 1406
1384 rc->avg_frame_bandwidth = (int)(oxcf->target_bandwidth / oxcf->framerate); 1407 rc->avg_frame_bandwidth = (int)(oxcf->target_bandwidth / oxcf->framerate);
1385 rc->min_frame_bandwidth = (int)(rc->avg_frame_bandwidth * 1408 rc->min_frame_bandwidth = (int)(rc->avg_frame_bandwidth *
1386 oxcf->two_pass_vbrmin_section / 100); 1409 oxcf->two_pass_vbrmin_section / 100);
1387 1410
1388 rc->min_frame_bandwidth = MAX(rc->min_frame_bandwidth, FRAME_OVERHEAD_BITS); 1411 rc->min_frame_bandwidth = MAX(rc->min_frame_bandwidth, FRAME_OVERHEAD_BITS);
1389 1412
1390 // A maximum bitrate for a frame is defined. 1413 // A maximum bitrate for a frame is defined.
1391 // The baseline for this aligns with HW implementations that 1414 // The baseline for this aligns with HW implementations that
1392 // can support decode of 1080P content up to a bitrate of MAX_MB_RATE bits 1415 // can support decode of 1080P content up to a bitrate of MAX_MB_RATE bits
1393 // per 16x16 MB (averaged over a frame). However this limit is extended if 1416 // per 16x16 MB (averaged over a frame). However this limit is extended if
1394 // a very high rate is given on the command line or the the rate cannnot 1417 // a very high rate is given on the command line or the the rate cannnot
1395 // be acheived because of a user specificed max q (e.g. when the user 1418 // be acheived because of a user specificed max q (e.g. when the user
1396 // specifies lossless encode. 1419 // specifies lossless encode.
1397 vbr_max_bits = (int)(((int64_t)rc->avg_frame_bandwidth * 1420 vbr_max_bits = (int)(((int64_t)rc->avg_frame_bandwidth *
1398 oxcf->two_pass_vbrmax_section) / 100); 1421 oxcf->two_pass_vbrmax_section) / 100);
1399 rc->max_frame_bandwidth = MAX(MAX((cm->MBs * MAX_MB_RATE), MAXRATE_1080P), 1422 rc->max_frame_bandwidth = MAX(MAX((cm->MBs * MAX_MB_RATE), MAXRATE_1080P),
1400 vbr_max_bits); 1423 vbr_max_bits);
1401 1424
1402 // Set Maximum gf/arf interval 1425 vp9_rc_set_gf_max_interval(oxcf, rc);
1403 rc->max_gf_interval = 16;
1404
1405 // Extended interval for genuinely static scenes
1406 rc->static_scene_max_gf_interval = cpi->oxcf.key_freq >> 1;
1407
1408 // Special conditions when alt ref frame enabled in lagged compress mode
1409 if (oxcf->play_alternate && oxcf->lag_in_frames) {
1410 if (rc->max_gf_interval > oxcf->lag_in_frames - 1)
1411 rc->max_gf_interval = oxcf->lag_in_frames - 1;
1412
1413 if (rc->static_scene_max_gf_interval > oxcf->lag_in_frames - 1)
1414 rc->static_scene_max_gf_interval = oxcf->lag_in_frames - 1;
1415 }
1416
1417 if (rc->max_gf_interval > rc->static_scene_max_gf_interval)
1418 rc->max_gf_interval = rc->static_scene_max_gf_interval;
1419 } 1426 }
OLDNEW
« no previous file with comments | « source/libvpx/vp9/encoder/vp9_ratectrl.h ('k') | source/libvpx/vp9/encoder/vp9_rdopt.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698