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

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

Issue 1302353004: libvpx: Pull from upstream (Closed) Base URL: https://chromium.googlesource.com/chromium/deps/libvpx.git@master
Patch Set: Created 5 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
« no previous file with comments | « source/libvpx/vp9/encoder/vp9_ratectrl.h ('k') | source/libvpx/vp9/encoder/vp9_rd.c » ('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
11 #include <assert.h> 11 #include <assert.h>
12 #include <limits.h> 12 #include <limits.h>
13 #include <math.h> 13 #include <math.h>
14 #include <stdio.h> 14 #include <stdio.h>
15 #include <stdlib.h> 15 #include <stdlib.h>
16 #include <string.h> 16 #include <string.h>
17 17
18 #include "vpx_mem/vpx_mem.h" 18 #include "vpx_mem/vpx_mem.h"
19 #include "vpx_ports/mem.h" 19 #include "vpx_ports/mem.h"
20 #include "vpx_ports/system_state.h"
20 21
21 #include "vp9/common/vp9_alloccommon.h" 22 #include "vp9/common/vp9_alloccommon.h"
22 #include "vp9/encoder/vp9_aq_cyclicrefresh.h" 23 #include "vp9/encoder/vp9_aq_cyclicrefresh.h"
23 #include "vp9/common/vp9_common.h" 24 #include "vp9/common/vp9_common.h"
24 #include "vp9/common/vp9_entropymode.h" 25 #include "vp9/common/vp9_entropymode.h"
25 #include "vp9/common/vp9_quant_common.h" 26 #include "vp9/common/vp9_quant_common.h"
26 #include "vp9/common/vp9_seg_common.h" 27 #include "vp9/common/vp9_seg_common.h"
27 #include "vp9/common/vp9_systemdependent.h"
28 28
29 #include "vp9/encoder/vp9_encodemv.h" 29 #include "vp9/encoder/vp9_encodemv.h"
30 #include "vp9/encoder/vp9_ratectrl.h" 30 #include "vp9/encoder/vp9_ratectrl.h"
31 31
32 // Max rate target for 1080P and below encodes under normal circumstances 32 // Max rate target for 1080P and below encodes under normal circumstances
33 // (1920 * 1080 / (16 * 16)) * MAX_MB_RATE bits per MB 33 // (1920 * 1080 / (16 * 16)) * MAX_MB_RATE bits per MB
34 #define MAX_MB_RATE 250 34 #define MAX_MB_RATE 250
35 #define MAXRATE_1080P 2025000 35 #define MAXRATE_1080P 2025000
36 36
37 #define DEFAULT_KF_BOOST 2000 37 #define DEFAULT_KF_BOOST 2000
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
99 static int kf_high = 5000; 99 static int kf_high = 5000;
100 static int kf_low = 400; 100 static int kf_low = 400;
101 101
102 // Functions to compute the active minq lookup table entries based on a 102 // Functions to compute the active minq lookup table entries based on a
103 // formulaic approach to facilitate easier adjustment of the Q tables. 103 // formulaic approach to facilitate easier adjustment of the Q tables.
104 // The formulae were derived from computing a 3rd order polynomial best 104 // The formulae were derived from computing a 3rd order polynomial best
105 // fit to the original data (after plotting real maxq vs minq (not q index)) 105 // fit to the original data (after plotting real maxq vs minq (not q index))
106 static int get_minq_index(double maxq, double x3, double x2, double x1, 106 static int get_minq_index(double maxq, double x3, double x2, double x1,
107 vpx_bit_depth_t bit_depth) { 107 vpx_bit_depth_t bit_depth) {
108 int i; 108 int i;
109 const double minqtarget = MIN(((x3 * maxq + x2) * maxq + x1) * maxq, 109 const double minqtarget = VPXMIN(((x3 * maxq + x2) * maxq + x1) * maxq,
110 maxq); 110 maxq);
111 111
112 // Special case handling to deal with the step from q2.0 112 // Special case handling to deal with the step from q2.0
113 // down to lossless mode represented by q 1.0. 113 // down to lossless mode represented by q 1.0.
114 if (minqtarget <= 2.0) 114 if (minqtarget <= 2.0)
115 return 0; 115 return 0;
116 116
117 for (i = 0; i < QINDEX_RANGE; i++) { 117 for (i = 0; i < QINDEX_RANGE; i++) {
118 if (minqtarget <= vp9_convert_qindex_to_q(i, bit_depth)) 118 if (minqtarget <= vp9_convert_qindex_to_q(i, bit_depth))
119 return i; 119 return i;
120 } 120 }
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
185 // q based adjustment to baseline enumerator 185 // q based adjustment to baseline enumerator
186 enumerator += (int)(enumerator * q) >> 12; 186 enumerator += (int)(enumerator * q) >> 12;
187 return (int)(enumerator * correction_factor / q); 187 return (int)(enumerator * correction_factor / q);
188 } 188 }
189 189
190 int vp9_estimate_bits_at_q(FRAME_TYPE frame_type, int q, int mbs, 190 int vp9_estimate_bits_at_q(FRAME_TYPE frame_type, int q, int mbs,
191 double correction_factor, 191 double correction_factor,
192 vpx_bit_depth_t bit_depth) { 192 vpx_bit_depth_t bit_depth) {
193 const int bpm = (int)(vp9_rc_bits_per_mb(frame_type, q, correction_factor, 193 const int bpm = (int)(vp9_rc_bits_per_mb(frame_type, q, correction_factor,
194 bit_depth)); 194 bit_depth));
195 return MAX(FRAME_OVERHEAD_BITS, 195 return VPXMAX(FRAME_OVERHEAD_BITS,
196 (int)((uint64_t)bpm * mbs) >> BPER_MB_NORMBITS); 196 (int)((uint64_t)bpm * mbs) >> BPER_MB_NORMBITS);
197 } 197 }
198 198
199 int vp9_rc_clamp_pframe_target_size(const VP9_COMP *const cpi, int target) { 199 int vp9_rc_clamp_pframe_target_size(const VP9_COMP *const cpi, int target) {
200 const RATE_CONTROL *rc = &cpi->rc; 200 const RATE_CONTROL *rc = &cpi->rc;
201 const VP9EncoderConfig *oxcf = &cpi->oxcf; 201 const VP9EncoderConfig *oxcf = &cpi->oxcf;
202 const int min_frame_target = MAX(rc->min_frame_bandwidth, 202 const int min_frame_target = VPXMAX(rc->min_frame_bandwidth,
203 rc->avg_frame_bandwidth >> 5); 203 rc->avg_frame_bandwidth >> 5);
204 if (target < min_frame_target) 204 if (target < min_frame_target)
205 target = min_frame_target; 205 target = min_frame_target;
206 if (cpi->refresh_golden_frame && rc->is_src_frame_alt_ref) { 206 if (cpi->refresh_golden_frame && rc->is_src_frame_alt_ref) {
207 // If there is an active ARF at this location use the minimum 207 // If there is an active ARF at this location use the minimum
208 // bits on this frame even if it is a constructed arf. 208 // bits on this frame even if it is a constructed arf.
209 // The active maximum quantizer insures that an appropriate 209 // The active maximum quantizer insures that an appropriate
210 // number of bits will be spent if needed for constructed ARFs. 210 // number of bits will be spent if needed for constructed ARFs.
211 target = min_frame_target; 211 target = min_frame_target;
212 } 212 }
213 // Clip the frame target to the maximum allowed value. 213 // Clip the frame target to the maximum allowed value.
214 if (target > rc->max_frame_bandwidth) 214 if (target > rc->max_frame_bandwidth)
215 target = rc->max_frame_bandwidth; 215 target = rc->max_frame_bandwidth;
216 if (oxcf->rc_max_inter_bitrate_pct) { 216 if (oxcf->rc_max_inter_bitrate_pct) {
217 const int max_rate = rc->avg_frame_bandwidth * 217 const int max_rate = rc->avg_frame_bandwidth *
218 oxcf->rc_max_inter_bitrate_pct / 100; 218 oxcf->rc_max_inter_bitrate_pct / 100;
219 target = MIN(target, max_rate); 219 target = VPXMIN(target, max_rate);
220 } 220 }
221 return target; 221 return target;
222 } 222 }
223 223
224 int vp9_rc_clamp_iframe_target_size(const VP9_COMP *const cpi, int target) { 224 int vp9_rc_clamp_iframe_target_size(const VP9_COMP *const cpi, int target) {
225 const RATE_CONTROL *rc = &cpi->rc; 225 const RATE_CONTROL *rc = &cpi->rc;
226 const VP9EncoderConfig *oxcf = &cpi->oxcf; 226 const VP9EncoderConfig *oxcf = &cpi->oxcf;
227 if (oxcf->rc_max_intra_bitrate_pct) { 227 if (oxcf->rc_max_intra_bitrate_pct) {
228 const int max_rate = rc->avg_frame_bandwidth * 228 const int max_rate = rc->avg_frame_bandwidth *
229 oxcf->rc_max_intra_bitrate_pct / 100; 229 oxcf->rc_max_intra_bitrate_pct / 100;
230 target = MIN(target, max_rate); 230 target = VPXMIN(target, max_rate);
231 } 231 }
232 if (target > rc->max_frame_bandwidth) 232 if (target > rc->max_frame_bandwidth)
233 target = rc->max_frame_bandwidth; 233 target = rc->max_frame_bandwidth;
234 return target; 234 return target;
235 } 235 }
236 236
237 // Update the buffer level for higher temporal layers, given the encoded current 237 // Update the buffer level for higher temporal layers, given the encoded current
238 // temporal layer. 238 // temporal layer.
239 static void update_layer_buffer_level(SVC *svc, int encoded_frame_size) { 239 static void update_layer_buffer_level(SVC *svc, int encoded_frame_size) {
240 int i = 0; 240 int i = 0;
241 int current_temporal_layer = svc->temporal_layer_id; 241 int current_temporal_layer = svc->temporal_layer_id;
242 for (i = current_temporal_layer + 1; 242 for (i = current_temporal_layer + 1;
243 i < svc->number_temporal_layers; ++i) { 243 i < svc->number_temporal_layers; ++i) {
244 const int layer = LAYER_IDS_TO_IDX(svc->spatial_layer_id, i, 244 const int layer = LAYER_IDS_TO_IDX(svc->spatial_layer_id, i,
245 svc->number_temporal_layers); 245 svc->number_temporal_layers);
246 LAYER_CONTEXT *lc = &svc->layer_context[layer]; 246 LAYER_CONTEXT *lc = &svc->layer_context[layer];
247 RATE_CONTROL *lrc = &lc->rc; 247 RATE_CONTROL *lrc = &lc->rc;
248 int bits_off_for_this_layer = (int)(lc->target_bandwidth / lc->framerate - 248 int bits_off_for_this_layer = (int)(lc->target_bandwidth / lc->framerate -
249 encoded_frame_size); 249 encoded_frame_size);
250 lrc->bits_off_target += bits_off_for_this_layer; 250 lrc->bits_off_target += bits_off_for_this_layer;
251 251
252 // Clip buffer level to maximum buffer size for the layer. 252 // Clip buffer level to maximum buffer size for the layer.
253 lrc->bits_off_target = MIN(lrc->bits_off_target, lrc->maximum_buffer_size); 253 lrc->bits_off_target =
254 VPXMIN(lrc->bits_off_target, lrc->maximum_buffer_size);
254 lrc->buffer_level = lrc->bits_off_target; 255 lrc->buffer_level = lrc->bits_off_target;
255 } 256 }
256 } 257 }
257 258
258 // Update the buffer level: leaky bucket model. 259 // Update the buffer level: leaky bucket model.
259 static void update_buffer_level(VP9_COMP *cpi, int encoded_frame_size) { 260 static void update_buffer_level(VP9_COMP *cpi, int encoded_frame_size) {
260 const VP9_COMMON *const cm = &cpi->common; 261 const VP9_COMMON *const cm = &cpi->common;
261 RATE_CONTROL *const rc = &cpi->rc; 262 RATE_CONTROL *const rc = &cpi->rc;
262 263
263 // Non-viewable frames are a special case and are treated as pure overhead. 264 // Non-viewable frames are a special case and are treated as pure overhead.
264 if (!cm->show_frame) { 265 if (!cm->show_frame) {
265 rc->bits_off_target -= encoded_frame_size; 266 rc->bits_off_target -= encoded_frame_size;
266 } else { 267 } else {
267 rc->bits_off_target += rc->avg_frame_bandwidth - encoded_frame_size; 268 rc->bits_off_target += rc->avg_frame_bandwidth - encoded_frame_size;
268 } 269 }
269 270
270 // Clip the buffer level to the maximum specified buffer size. 271 // Clip the buffer level to the maximum specified buffer size.
271 rc->bits_off_target = MIN(rc->bits_off_target, rc->maximum_buffer_size); 272 rc->bits_off_target = VPXMIN(rc->bits_off_target, rc->maximum_buffer_size);
272 rc->buffer_level = rc->bits_off_target; 273 rc->buffer_level = rc->bits_off_target;
273 274
274 if (is_one_pass_cbr_svc(cpi)) { 275 if (is_one_pass_cbr_svc(cpi)) {
275 update_layer_buffer_level(&cpi->svc, encoded_frame_size); 276 update_layer_buffer_level(&cpi->svc, encoded_frame_size);
276 } 277 }
277 } 278 }
278 279
279 int vp9_rc_get_default_min_gf_interval( 280 int vp9_rc_get_default_min_gf_interval(
280 int width, int height, double framerate) { 281 int width, int height, double framerate) {
281 // Assume we do not need any constraint lower than 4K 20 fps 282 // Assume we do not need any constraint lower than 4K 20 fps
282 static const double factor_safe = 3840 * 2160 * 20.0; 283 static const double factor_safe = 3840 * 2160 * 20.0;
283 const double factor = width * height * framerate; 284 const double factor = width * height * framerate;
284 const int default_interval = 285 const int default_interval =
285 clamp((int)(framerate * 0.125), MIN_GF_INTERVAL, MAX_GF_INTERVAL); 286 clamp((int)(framerate * 0.125), MIN_GF_INTERVAL, MAX_GF_INTERVAL);
286 287
287 if (factor <= factor_safe) 288 if (factor <= factor_safe)
288 return default_interval; 289 return default_interval;
289 else 290 else
290 return MAX(default_interval, 291 return VPXMAX(default_interval,
291 (int)(MIN_GF_INTERVAL * factor / factor_safe + 0.5)); 292 (int)(MIN_GF_INTERVAL * factor / factor_safe + 0.5));
292 // Note this logic makes: 293 // Note this logic makes:
293 // 4K24: 5 294 // 4K24: 5
294 // 4K30: 6 295 // 4K30: 6
295 // 4K60: 12 296 // 4K60: 12
296 } 297 }
297 298
298 int vp9_rc_get_default_max_gf_interval(double framerate, int min_gf_interval) { 299 int vp9_rc_get_default_max_gf_interval(double framerate, int min_gf_interval) {
299 int interval = MIN(MAX_GF_INTERVAL, (int)(framerate * 0.75)); 300 int interval = VPXMIN(MAX_GF_INTERVAL, (int)(framerate * 0.75));
300 interval += (interval & 0x01); // Round to even value 301 interval += (interval & 0x01); // Round to even value
301 return MAX(interval, min_gf_interval); 302 return VPXMAX(interval, min_gf_interval);
302 } 303 }
303 304
304 void vp9_rc_init(const VP9EncoderConfig *oxcf, int pass, RATE_CONTROL *rc) { 305 void vp9_rc_init(const VP9EncoderConfig *oxcf, int pass, RATE_CONTROL *rc) {
305 int i; 306 int i;
306 307
307 if (pass == 0 && oxcf->rc_mode == VPX_CBR) { 308 if (pass == 0 && oxcf->rc_mode == VPX_CBR) {
308 rc->avg_frame_qindex[KEY_FRAME] = oxcf->worst_allowed_q; 309 rc->avg_frame_qindex[KEY_FRAME] = oxcf->worst_allowed_q;
309 rc->avg_frame_qindex[INTER_FRAME] = oxcf->worst_allowed_q; 310 rc->avg_frame_qindex[INTER_FRAME] = oxcf->worst_allowed_q;
310 } else { 311 } else {
311 rc->avg_frame_qindex[KEY_FRAME] = (oxcf->worst_allowed_q + 312 rc->avg_frame_qindex[KEY_FRAME] = (oxcf->worst_allowed_q +
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
448 double rate_correction_factor = get_rate_correction_factor(cpi); 449 double rate_correction_factor = get_rate_correction_factor(cpi);
449 double adjustment_limit; 450 double adjustment_limit;
450 451
451 int projected_size_based_on_q = 0; 452 int projected_size_based_on_q = 0;
452 453
453 // Do not update the rate factors for arf overlay frames. 454 // Do not update the rate factors for arf overlay frames.
454 if (cpi->rc.is_src_frame_alt_ref) 455 if (cpi->rc.is_src_frame_alt_ref)
455 return; 456 return;
456 457
457 // Clear down mmx registers to allow floating point in what follows 458 // Clear down mmx registers to allow floating point in what follows
458 vp9_clear_system_state(); 459 vpx_clear_system_state();
459 460
460 // Work out how big we would have expected the frame to be at this Q given 461 // Work out how big we would have expected the frame to be at this Q given
461 // the current correction factor. 462 // the current correction factor.
462 // Stay in double to avoid int overflow when values are large 463 // Stay in double to avoid int overflow when values are large
463 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cpi->common.seg.enabled) { 464 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cpi->common.seg.enabled) {
464 projected_size_based_on_q = 465 projected_size_based_on_q =
465 vp9_cyclic_refresh_estimate_bits_at_q(cpi, rate_correction_factor); 466 vp9_cyclic_refresh_estimate_bits_at_q(cpi, rate_correction_factor);
466 } else { 467 } else {
467 projected_size_based_on_q = vp9_estimate_bits_at_q(cpi->common.frame_type, 468 projected_size_based_on_q = vp9_estimate_bits_at_q(cpi->common.frame_type,
468 cm->base_qindex, 469 cm->base_qindex,
469 cm->MBs, 470 cm->MBs,
470 rate_correction_factor, 471 rate_correction_factor,
471 cm->bit_depth); 472 cm->bit_depth);
472 } 473 }
473 // Work out a size correction factor. 474 // Work out a size correction factor.
474 if (projected_size_based_on_q > FRAME_OVERHEAD_BITS) 475 if (projected_size_based_on_q > FRAME_OVERHEAD_BITS)
475 correction_factor = (int)((100 * (int64_t)cpi->rc.projected_frame_size) / 476 correction_factor = (int)((100 * (int64_t)cpi->rc.projected_frame_size) /
476 projected_size_based_on_q); 477 projected_size_based_on_q);
477 478
478 // More heavily damped adjustment used if we have been oscillating either side 479 // More heavily damped adjustment used if we have been oscillating either side
479 // of target. 480 // of target.
480 adjustment_limit = 0.25 + 481 adjustment_limit = 0.25 +
481 0.5 * MIN(1, fabs(log10(0.01 * correction_factor))); 482 0.5 * VPXMIN(1, fabs(log10(0.01 * correction_factor)));
482 483
483 cpi->rc.q_2_frame = cpi->rc.q_1_frame; 484 cpi->rc.q_2_frame = cpi->rc.q_1_frame;
484 cpi->rc.q_1_frame = cm->base_qindex; 485 cpi->rc.q_1_frame = cm->base_qindex;
485 cpi->rc.rc_2_frame = cpi->rc.rc_1_frame; 486 cpi->rc.rc_2_frame = cpi->rc.rc_1_frame;
486 if (correction_factor > 110) 487 if (correction_factor > 110)
487 cpi->rc.rc_1_frame = -1; 488 cpi->rc.rc_1_frame = -1;
488 else if (correction_factor < 90) 489 else if (correction_factor < 90)
489 cpi->rc.rc_1_frame = 1; 490 cpi->rc.rc_1_frame = 1;
490 else 491 else
491 cpi->rc.rc_1_frame = 0; 492 cpi->rc.rc_1_frame = 0;
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
551 } else { 552 } else {
552 last_error = bits_per_mb_at_this_q - target_bits_per_mb; 553 last_error = bits_per_mb_at_this_q - target_bits_per_mb;
553 } 554 }
554 } while (++i <= active_worst_quality); 555 } while (++i <= active_worst_quality);
555 556
556 // In CBR mode, this makes sure q is between oscillating Qs to prevent 557 // In CBR mode, this makes sure q is between oscillating Qs to prevent
557 // resonance. 558 // resonance.
558 if (cpi->oxcf.rc_mode == VPX_CBR && 559 if (cpi->oxcf.rc_mode == VPX_CBR &&
559 (cpi->rc.rc_1_frame * cpi->rc.rc_2_frame == -1) && 560 (cpi->rc.rc_1_frame * cpi->rc.rc_2_frame == -1) &&
560 cpi->rc.q_1_frame != cpi->rc.q_2_frame) { 561 cpi->rc.q_1_frame != cpi->rc.q_2_frame) {
561 q = clamp(q, MIN(cpi->rc.q_1_frame, cpi->rc.q_2_frame), 562 q = clamp(q, VPXMIN(cpi->rc.q_1_frame, cpi->rc.q_2_frame),
562 MAX(cpi->rc.q_1_frame, cpi->rc.q_2_frame)); 563 VPXMAX(cpi->rc.q_1_frame, cpi->rc.q_2_frame));
563 } 564 }
564 return q; 565 return q;
565 } 566 }
566 567
567 static int get_active_quality(int q, int gfu_boost, int low, int high, 568 static int get_active_quality(int q, int gfu_boost, int low, int high,
568 int *low_motion_minq, int *high_motion_minq) { 569 int *low_motion_minq, int *high_motion_minq) {
569 if (gfu_boost > high) { 570 if (gfu_boost > high) {
570 return low_motion_minq[q]; 571 return low_motion_minq[q];
571 } else if (gfu_boost < low) { 572 } else if (gfu_boost < low) {
572 return high_motion_minq[q]; 573 return high_motion_minq[q];
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
610 } else { 611 } else {
611 if (!rc->is_src_frame_alt_ref && 612 if (!rc->is_src_frame_alt_ref &&
612 (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) { 613 (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
613 active_worst_quality = curr_frame == 1 ? rc->last_q[KEY_FRAME] * 5 / 4 614 active_worst_quality = curr_frame == 1 ? rc->last_q[KEY_FRAME] * 5 / 4
614 : rc->last_q[INTER_FRAME]; 615 : rc->last_q[INTER_FRAME];
615 } else { 616 } else {
616 active_worst_quality = curr_frame == 1 ? rc->last_q[KEY_FRAME] * 2 617 active_worst_quality = curr_frame == 1 ? rc->last_q[KEY_FRAME] * 2
617 : rc->last_q[INTER_FRAME] * 2; 618 : rc->last_q[INTER_FRAME] * 2;
618 } 619 }
619 } 620 }
620 return MIN(active_worst_quality, rc->worst_quality); 621 return VPXMIN(active_worst_quality, rc->worst_quality);
621 } 622 }
622 623
623 // Adjust active_worst_quality level based on buffer level. 624 // Adjust active_worst_quality level based on buffer level.
624 static int calc_active_worst_quality_one_pass_cbr(const VP9_COMP *cpi) { 625 static int calc_active_worst_quality_one_pass_cbr(const VP9_COMP *cpi) {
625 // Adjust active_worst_quality: If buffer is above the optimal/target level, 626 // Adjust active_worst_quality: If buffer is above the optimal/target level,
626 // bring active_worst_quality down depending on fullness of buffer. 627 // bring active_worst_quality down depending on fullness of buffer.
627 // If buffer is below the optimal level, let the active_worst_quality go from 628 // If buffer is below the optimal level, let the active_worst_quality go from
628 // ambient Q (at buffer = optimal level) to worst_quality level 629 // ambient Q (at buffer = optimal level) to worst_quality level
629 // (at buffer = critical level). 630 // (at buffer = critical level).
630 const VP9_COMMON *const cm = &cpi->common; 631 const VP9_COMMON *const cm = &cpi->common;
631 const RATE_CONTROL *rc = &cpi->rc; 632 const RATE_CONTROL *rc = &cpi->rc;
632 // Buffer level below which we push active_worst to worst_quality. 633 // Buffer level below which we push active_worst to worst_quality.
633 int64_t critical_level = rc->optimal_buffer_level >> 3; 634 int64_t critical_level = rc->optimal_buffer_level >> 3;
634 int64_t buff_lvl_step = 0; 635 int64_t buff_lvl_step = 0;
635 int adjustment = 0; 636 int adjustment = 0;
636 int active_worst_quality; 637 int active_worst_quality;
637 int ambient_qp; 638 int ambient_qp;
639 unsigned int num_frames_weight_key = 5 * cpi->svc.number_temporal_layers;
638 if (cm->frame_type == KEY_FRAME) 640 if (cm->frame_type == KEY_FRAME)
639 return rc->worst_quality; 641 return rc->worst_quality;
640 // For ambient_qp we use minimum of avg_frame_qindex[KEY_FRAME/INTER_FRAME] 642 // For ambient_qp we use minimum of avg_frame_qindex[KEY_FRAME/INTER_FRAME]
641 // for the first few frames following key frame. These are both initialized 643 // for the first few frames following key frame. These are both initialized
642 // to worst_quality and updated with (3/4, 1/4) average in postencode_update. 644 // to worst_quality and updated with (3/4, 1/4) average in postencode_update.
643 // So for first few frames following key, the qp of that key frame is weighted 645 // So for first few frames following key, the qp of that key frame is weighted
644 // into the active_worst_quality setting. 646 // into the active_worst_quality setting.
645 ambient_qp = (cm->current_video_frame < 5) ? 647 ambient_qp = (cm->current_video_frame < num_frames_weight_key) ?
646 MIN(rc->avg_frame_qindex[INTER_FRAME], rc->avg_frame_qindex[KEY_FRAME]) : 648 VPXMIN(rc->avg_frame_qindex[INTER_FRAME],
647 rc->avg_frame_qindex[INTER_FRAME]; 649 rc->avg_frame_qindex[KEY_FRAME]) :
648 active_worst_quality = MIN(rc->worst_quality, 650 rc->avg_frame_qindex[INTER_FRAME];
649 ambient_qp * 5 / 4); 651 active_worst_quality = VPXMIN(rc->worst_quality, ambient_qp * 5 / 4);
650 if (rc->buffer_level > rc->optimal_buffer_level) { 652 if (rc->buffer_level > rc->optimal_buffer_level) {
651 // Adjust down. 653 // Adjust down.
652 // Maximum limit for down adjustment, ~30%. 654 // Maximum limit for down adjustment, ~30%.
653 int max_adjustment_down = active_worst_quality / 3; 655 int max_adjustment_down = active_worst_quality / 3;
654 if (max_adjustment_down) { 656 if (max_adjustment_down) {
655 buff_lvl_step = ((rc->maximum_buffer_size - 657 buff_lvl_step = ((rc->maximum_buffer_size -
656 rc->optimal_buffer_level) / max_adjustment_down); 658 rc->optimal_buffer_level) / max_adjustment_down);
657 if (buff_lvl_step) 659 if (buff_lvl_step)
658 adjustment = (int)((rc->buffer_level - rc->optimal_buffer_level) / 660 adjustment = (int)((rc->buffer_level - rc->optimal_buffer_level) /
659 buff_lvl_step); 661 buff_lvl_step);
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
692 active_best_quality = rc->best_quality; 694 active_best_quality = rc->best_quality;
693 // Handle the special case for key frames forced when we have reached 695 // Handle the special case for key frames forced when we have reached
694 // the maximum key frame interval. Here force the Q to a range 696 // the maximum key frame interval. Here force the Q to a range
695 // based on the ambient Q to reduce the risk of popping. 697 // based on the ambient Q to reduce the risk of popping.
696 if (rc->this_key_frame_forced) { 698 if (rc->this_key_frame_forced) {
697 int qindex = rc->last_boosted_qindex; 699 int qindex = rc->last_boosted_qindex;
698 double last_boosted_q = vp9_convert_qindex_to_q(qindex, cm->bit_depth); 700 double last_boosted_q = vp9_convert_qindex_to_q(qindex, cm->bit_depth);
699 int delta_qindex = vp9_compute_qdelta(rc, last_boosted_q, 701 int delta_qindex = vp9_compute_qdelta(rc, last_boosted_q,
700 (last_boosted_q * 0.75), 702 (last_boosted_q * 0.75),
701 cm->bit_depth); 703 cm->bit_depth);
702 active_best_quality = MAX(qindex + delta_qindex, rc->best_quality); 704 active_best_quality = VPXMAX(qindex + delta_qindex, rc->best_quality);
703 } else if (cm->current_video_frame > 0) { 705 } else if (cm->current_video_frame > 0) {
704 // not first frame of one pass and kf_boost is set 706 // not first frame of one pass and kf_boost is set
705 double q_adj_factor = 1.0; 707 double q_adj_factor = 1.0;
706 double q_val; 708 double q_val;
707 709
708 active_best_quality = 710 active_best_quality =
709 get_kf_active_quality(rc, rc->avg_frame_qindex[KEY_FRAME], 711 get_kf_active_quality(rc, rc->avg_frame_qindex[KEY_FRAME],
710 cm->bit_depth); 712 cm->bit_depth);
711 713
712 // Allow somewhat lower kf minq with small image formats. 714 // Allow somewhat lower kf minq with small image formats.
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
757 759
758 *top_index = active_worst_quality; 760 *top_index = active_worst_quality;
759 *bottom_index = active_best_quality; 761 *bottom_index = active_best_quality;
760 762
761 #if LIMIT_QRANGE_FOR_ALTREF_AND_KEY 763 #if LIMIT_QRANGE_FOR_ALTREF_AND_KEY
762 // Limit Q range for the adaptive loop. 764 // Limit Q range for the adaptive loop.
763 if (cm->frame_type == KEY_FRAME && 765 if (cm->frame_type == KEY_FRAME &&
764 !rc->this_key_frame_forced && 766 !rc->this_key_frame_forced &&
765 !(cm->current_video_frame == 0)) { 767 !(cm->current_video_frame == 0)) {
766 int qdelta = 0; 768 int qdelta = 0;
767 vp9_clear_system_state(); 769 vpx_clear_system_state();
768 qdelta = vp9_compute_qdelta_by_rate(&cpi->rc, cm->frame_type, 770 qdelta = vp9_compute_qdelta_by_rate(&cpi->rc, cm->frame_type,
769 active_worst_quality, 2.0, 771 active_worst_quality, 2.0,
770 cm->bit_depth); 772 cm->bit_depth);
771 *top_index = active_worst_quality + qdelta; 773 *top_index = active_worst_quality + qdelta;
772 *top_index = (*top_index > *bottom_index) ? *top_index : *bottom_index; 774 *top_index = (*top_index > *bottom_index) ? *top_index : *bottom_index;
773 } 775 }
774 #endif 776 #endif
775 777
776 // Special case code to try and match quality with forced key frames 778 // Special case code to try and match quality with forced key frames
777 if (cm->frame_type == KEY_FRAME && rc->this_key_frame_forced) { 779 if (cm->frame_type == KEY_FRAME && rc->this_key_frame_forced) {
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
816 const RATE_CONTROL *const rc = &cpi->rc; 818 const RATE_CONTROL *const rc = &cpi->rc;
817 const VP9EncoderConfig *const oxcf = &cpi->oxcf; 819 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
818 const int cq_level = get_active_cq_level(rc, oxcf); 820 const int cq_level = get_active_cq_level(rc, oxcf);
819 int active_best_quality; 821 int active_best_quality;
820 int active_worst_quality = calc_active_worst_quality_one_pass_vbr(cpi); 822 int active_worst_quality = calc_active_worst_quality_one_pass_vbr(cpi);
821 int q; 823 int q;
822 int *inter_minq; 824 int *inter_minq;
823 ASSIGN_MINQ_TABLE(cm->bit_depth, inter_minq); 825 ASSIGN_MINQ_TABLE(cm->bit_depth, inter_minq);
824 826
825 if (frame_is_intra_only(cm)) { 827 if (frame_is_intra_only(cm)) {
826
827 // Handle the special case for key frames forced when we have reached 828 // Handle the special case for key frames forced when we have reached
828 // the maximum key frame interval. Here force the Q to a range 829 // the maximum key frame interval. Here force the Q to a range
829 // based on the ambient Q to reduce the risk of popping. 830 // based on the ambient Q to reduce the risk of popping.
830 if (rc->this_key_frame_forced) { 831 if (rc->this_key_frame_forced) {
831 int qindex = rc->last_boosted_qindex; 832 int qindex = rc->last_boosted_qindex;
832 double last_boosted_q = vp9_convert_qindex_to_q(qindex, cm->bit_depth); 833 double last_boosted_q = vp9_convert_qindex_to_q(qindex, cm->bit_depth);
833 int delta_qindex = vp9_compute_qdelta(rc, last_boosted_q, 834 int delta_qindex = vp9_compute_qdelta(rc, last_boosted_q,
834 last_boosted_q * 0.75, 835 last_boosted_q * 0.75,
835 cm->bit_depth); 836 cm->bit_depth);
836 active_best_quality = MAX(qindex + delta_qindex, rc->best_quality); 837 active_best_quality = VPXMAX(qindex + delta_qindex, rc->best_quality);
837 } else { 838 } else {
838 // not first frame of one pass and kf_boost is set 839 // not first frame of one pass and kf_boost is set
839 double q_adj_factor = 1.0; 840 double q_adj_factor = 1.0;
840 double q_val; 841 double q_val;
841 842
842 active_best_quality = 843 active_best_quality =
843 get_kf_active_quality(rc, rc->avg_frame_qindex[KEY_FRAME], 844 get_kf_active_quality(rc, rc->avg_frame_qindex[KEY_FRAME],
844 cm->bit_depth); 845 cm->bit_depth);
845 846
846 // Allow somewhat lower kf minq with small image formats. 847 // Allow somewhat lower kf minq with small image formats.
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
908 rc->best_quality, rc->worst_quality); 909 rc->best_quality, rc->worst_quality);
909 active_worst_quality = clamp(active_worst_quality, 910 active_worst_quality = clamp(active_worst_quality,
910 active_best_quality, rc->worst_quality); 911 active_best_quality, rc->worst_quality);
911 912
912 *top_index = active_worst_quality; 913 *top_index = active_worst_quality;
913 *bottom_index = active_best_quality; 914 *bottom_index = active_best_quality;
914 915
915 #if LIMIT_QRANGE_FOR_ALTREF_AND_KEY 916 #if LIMIT_QRANGE_FOR_ALTREF_AND_KEY
916 { 917 {
917 int qdelta = 0; 918 int qdelta = 0;
918 vp9_clear_system_state(); 919 vpx_clear_system_state();
919 920
920 // Limit Q range for the adaptive loop. 921 // Limit Q range for the adaptive loop.
921 if (cm->frame_type == KEY_FRAME && 922 if (cm->frame_type == KEY_FRAME &&
922 !rc->this_key_frame_forced && 923 !rc->this_key_frame_forced &&
923 !(cm->current_video_frame == 0)) { 924 !(cm->current_video_frame == 0)) {
924 qdelta = vp9_compute_qdelta_by_rate(&cpi->rc, cm->frame_type, 925 qdelta = vp9_compute_qdelta_by_rate(&cpi->rc, cm->frame_type,
925 active_worst_quality, 2.0, 926 active_worst_quality, 2.0,
926 cm->bit_depth); 927 cm->bit_depth);
927 } else if (!rc->is_src_frame_alt_ref && 928 } else if (!rc->is_src_frame_alt_ref &&
928 (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) { 929 (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
995 if (frame_is_intra_only(cm) || vp9_is_upper_layer_key_frame(cpi)) { 996 if (frame_is_intra_only(cm) || vp9_is_upper_layer_key_frame(cpi)) {
996 // Handle the special case for key frames forced when we have reached 997 // Handle the special case for key frames forced when we have reached
997 // the maximum key frame interval. Here force the Q to a range 998 // the maximum key frame interval. Here force the Q to a range
998 // based on the ambient Q to reduce the risk of popping. 999 // based on the ambient Q to reduce the risk of popping.
999 if (rc->this_key_frame_forced) { 1000 if (rc->this_key_frame_forced) {
1000 double last_boosted_q; 1001 double last_boosted_q;
1001 int delta_qindex; 1002 int delta_qindex;
1002 int qindex; 1003 int qindex;
1003 1004
1004 if (cpi->twopass.last_kfgroup_zeromotion_pct >= STATIC_MOTION_THRESH) { 1005 if (cpi->twopass.last_kfgroup_zeromotion_pct >= STATIC_MOTION_THRESH) {
1005 qindex = MIN(rc->last_kf_qindex, rc->last_boosted_qindex); 1006 qindex = VPXMIN(rc->last_kf_qindex, rc->last_boosted_qindex);
1006 active_best_quality = qindex; 1007 active_best_quality = qindex;
1007 last_boosted_q = vp9_convert_qindex_to_q(qindex, cm->bit_depth); 1008 last_boosted_q = vp9_convert_qindex_to_q(qindex, cm->bit_depth);
1008 delta_qindex = vp9_compute_qdelta(rc, last_boosted_q, 1009 delta_qindex = vp9_compute_qdelta(rc, last_boosted_q,
1009 last_boosted_q * 1.25, 1010 last_boosted_q * 1.25,
1010 cm->bit_depth); 1011 cm->bit_depth);
1011 active_worst_quality = MIN(qindex + delta_qindex, active_worst_quality); 1012 active_worst_quality =
1012 1013 VPXMIN(qindex + delta_qindex, active_worst_quality);
1013 } else { 1014 } else {
1014 qindex = rc->last_boosted_qindex; 1015 qindex = rc->last_boosted_qindex;
1015 last_boosted_q = vp9_convert_qindex_to_q(qindex, cm->bit_depth); 1016 last_boosted_q = vp9_convert_qindex_to_q(qindex, cm->bit_depth);
1016 delta_qindex = vp9_compute_qdelta(rc, last_boosted_q, 1017 delta_qindex = vp9_compute_qdelta(rc, last_boosted_q,
1017 last_boosted_q * 0.75, 1018 last_boosted_q * 0.75,
1018 cm->bit_depth); 1019 cm->bit_depth);
1019 active_best_quality = MAX(qindex + delta_qindex, rc->best_quality); 1020 active_best_quality = VPXMAX(qindex + delta_qindex, rc->best_quality);
1020 } 1021 }
1021 } else { 1022 } else {
1022 // Not forced keyframe. 1023 // Not forced keyframe.
1023 double q_adj_factor = 1.0; 1024 double q_adj_factor = 1.0;
1024 double q_val; 1025 double q_val;
1025 // Baseline value derived from cpi->active_worst_quality and kf boost. 1026 // Baseline value derived from cpi->active_worst_quality and kf boost.
1026 active_best_quality = get_kf_active_quality(rc, active_worst_quality, 1027 active_best_quality = get_kf_active_quality(rc, active_worst_quality,
1027 cm->bit_depth); 1028 cm->bit_depth);
1028 1029
1029 // Allow somewhat lower kf minq with small image formats. 1030 // Allow somewhat lower kf minq with small image formats.
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
1102 (cpi->twopass.extend_minq + cpi->twopass.extend_minq_fast); 1103 (cpi->twopass.extend_minq + cpi->twopass.extend_minq_fast);
1103 active_worst_quality += (cpi->twopass.extend_maxq / 2); 1104 active_worst_quality += (cpi->twopass.extend_maxq / 2);
1104 } else { 1105 } else {
1105 active_best_quality -= 1106 active_best_quality -=
1106 (cpi->twopass.extend_minq + cpi->twopass.extend_minq_fast) / 2; 1107 (cpi->twopass.extend_minq + cpi->twopass.extend_minq_fast) / 2;
1107 active_worst_quality += cpi->twopass.extend_maxq; 1108 active_worst_quality += cpi->twopass.extend_maxq;
1108 } 1109 }
1109 } 1110 }
1110 1111
1111 #if LIMIT_QRANGE_FOR_ALTREF_AND_KEY 1112 #if LIMIT_QRANGE_FOR_ALTREF_AND_KEY
1112 vp9_clear_system_state(); 1113 vpx_clear_system_state();
1113 // Static forced key frames Q restrictions dealt with elsewhere. 1114 // Static forced key frames Q restrictions dealt with elsewhere.
1114 if (!((frame_is_intra_only(cm) || vp9_is_upper_layer_key_frame(cpi))) || 1115 if (!((frame_is_intra_only(cm) || vp9_is_upper_layer_key_frame(cpi))) ||
1115 !rc->this_key_frame_forced || 1116 !rc->this_key_frame_forced ||
1116 (cpi->twopass.last_kfgroup_zeromotion_pct < STATIC_MOTION_THRESH)) { 1117 (cpi->twopass.last_kfgroup_zeromotion_pct < STATIC_MOTION_THRESH)) {
1117 int qdelta = vp9_frame_type_qdelta(cpi, gf_group->rf_level[gf_group->index], 1118 int qdelta = vp9_frame_type_qdelta(cpi, gf_group->rf_level[gf_group->index],
1118 active_worst_quality); 1119 active_worst_quality);
1119 active_worst_quality = MAX(active_worst_quality + qdelta, 1120 active_worst_quality = VPXMAX(active_worst_quality + qdelta,
1120 active_best_quality); 1121 active_best_quality);
1121 } 1122 }
1122 #endif 1123 #endif
1123 1124
1124 // Modify active_best_quality for downscaled normal frames. 1125 // Modify active_best_quality for downscaled normal frames.
1125 if (rc->frame_size_selector != UNSCALED && !frame_is_kf_gf_arf(cpi)) { 1126 if (rc->frame_size_selector != UNSCALED && !frame_is_kf_gf_arf(cpi)) {
1126 int qdelta = vp9_compute_qdelta_by_rate(rc, cm->frame_type, 1127 int qdelta = vp9_compute_qdelta_by_rate(rc, cm->frame_type,
1127 active_best_quality, 2.0, 1128 active_best_quality, 2.0,
1128 cm->bit_depth); 1129 cm->bit_depth);
1129 active_best_quality = MAX(active_best_quality + qdelta, rc->best_quality); 1130 active_best_quality =
1131 VPXMAX(active_best_quality + qdelta, rc->best_quality);
1130 } 1132 }
1131 1133
1132 active_best_quality = clamp(active_best_quality, 1134 active_best_quality = clamp(active_best_quality,
1133 rc->best_quality, rc->worst_quality); 1135 rc->best_quality, rc->worst_quality);
1134 active_worst_quality = clamp(active_worst_quality, 1136 active_worst_quality = clamp(active_worst_quality,
1135 active_best_quality, rc->worst_quality); 1137 active_best_quality, rc->worst_quality);
1136 1138
1137 if (oxcf->rc_mode == VPX_Q) { 1139 if (oxcf->rc_mode == VPX_Q) {
1138 q = active_best_quality; 1140 q = active_best_quality;
1139 // Special case code to try and match quality with forced key frames. 1141 // Special case code to try and match quality with forced key frames.
1140 } else if ((frame_is_intra_only(cm) || vp9_is_upper_layer_key_frame(cpi)) && 1142 } else if ((frame_is_intra_only(cm) || vp9_is_upper_layer_key_frame(cpi)) &&
1141 rc->this_key_frame_forced) { 1143 rc->this_key_frame_forced) {
1142 // If static since last kf use better of last boosted and last kf q. 1144 // If static since last kf use better of last boosted and last kf q.
1143 if (cpi->twopass.last_kfgroup_zeromotion_pct >= STATIC_MOTION_THRESH) { 1145 if (cpi->twopass.last_kfgroup_zeromotion_pct >= STATIC_MOTION_THRESH) {
1144 q = MIN(rc->last_kf_qindex, rc->last_boosted_qindex); 1146 q = VPXMIN(rc->last_kf_qindex, rc->last_boosted_qindex);
1145 } else { 1147 } else {
1146 q = rc->last_boosted_qindex; 1148 q = rc->last_boosted_qindex;
1147 } 1149 }
1148 } else { 1150 } else {
1149 q = vp9_rc_regulate_q(cpi, rc->this_frame_target, 1151 q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
1150 active_best_quality, active_worst_quality); 1152 active_best_quality, active_worst_quality);
1151 if (q > active_worst_quality) { 1153 if (q > active_worst_quality) {
1152 // Special case when we are targeting the max allowed rate. 1154 // Special case when we are targeting the max allowed rate.
1153 if (rc->this_frame_target >= rc->max_frame_bandwidth) 1155 if (rc->this_frame_target >= rc->max_frame_bandwidth)
1154 active_worst_quality = q; 1156 active_worst_quality = q;
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
1196 int frame_target, 1198 int frame_target,
1197 int *frame_under_shoot_limit, 1199 int *frame_under_shoot_limit,
1198 int *frame_over_shoot_limit) { 1200 int *frame_over_shoot_limit) {
1199 if (cpi->oxcf.rc_mode == VPX_Q) { 1201 if (cpi->oxcf.rc_mode == VPX_Q) {
1200 *frame_under_shoot_limit = 0; 1202 *frame_under_shoot_limit = 0;
1201 *frame_over_shoot_limit = INT_MAX; 1203 *frame_over_shoot_limit = INT_MAX;
1202 } else { 1204 } else {
1203 // For very small rate targets where the fractional adjustment 1205 // For very small rate targets where the fractional adjustment
1204 // may be tiny make sure there is at least a minimum range. 1206 // may be tiny make sure there is at least a minimum range.
1205 const int tolerance = (cpi->sf.recode_tolerance * frame_target) / 100; 1207 const int tolerance = (cpi->sf.recode_tolerance * frame_target) / 100;
1206 *frame_under_shoot_limit = MAX(frame_target - tolerance - 200, 0); 1208 *frame_under_shoot_limit = VPXMAX(frame_target - tolerance - 200, 0);
1207 *frame_over_shoot_limit = MIN(frame_target + tolerance + 200, 1209 *frame_over_shoot_limit = VPXMIN(frame_target + tolerance + 200,
1208 cpi->rc.max_frame_bandwidth); 1210 cpi->rc.max_frame_bandwidth);
1209 } 1211 }
1210 } 1212 }
1211 1213
1212 void vp9_rc_set_frame_target(VP9_COMP *cpi, int target) { 1214 void vp9_rc_set_frame_target(VP9_COMP *cpi, int target) {
1213 const VP9_COMMON *const cm = &cpi->common; 1215 const VP9_COMMON *const cm = &cpi->common;
1214 RATE_CONTROL *const rc = &cpi->rc; 1216 RATE_CONTROL *const rc = &cpi->rc;
1215 1217
1216 rc->this_frame_target = target; 1218 rc->this_frame_target = target;
1217 1219
1218 // Modify frame size target when down-scaling. 1220 // Modify frame size target when down-scaling.
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
1279 rc->projected_frame_size = (int)(bytes_used << 3); 1281 rc->projected_frame_size = (int)(bytes_used << 3);
1280 1282
1281 // Post encode loop adjustment of Q prediction. 1283 // Post encode loop adjustment of Q prediction.
1282 vp9_rc_update_rate_correction_factors(cpi); 1284 vp9_rc_update_rate_correction_factors(cpi);
1283 1285
1284 // Keep a record of last Q and ambient average Q. 1286 // Keep a record of last Q and ambient average Q.
1285 if (cm->frame_type == KEY_FRAME) { 1287 if (cm->frame_type == KEY_FRAME) {
1286 rc->last_q[KEY_FRAME] = qindex; 1288 rc->last_q[KEY_FRAME] = qindex;
1287 rc->avg_frame_qindex[KEY_FRAME] = 1289 rc->avg_frame_qindex[KEY_FRAME] =
1288 ROUND_POWER_OF_TWO(3 * rc->avg_frame_qindex[KEY_FRAME] + qindex, 2); 1290 ROUND_POWER_OF_TWO(3 * rc->avg_frame_qindex[KEY_FRAME] + qindex, 2);
1291 if (cpi->use_svc) {
1292 int i = 0;
1293 SVC *svc = &cpi->svc;
1294 for (i = 0; i < svc->number_temporal_layers; ++i) {
1295 const int layer = LAYER_IDS_TO_IDX(svc->spatial_layer_id, i,
1296 svc->number_temporal_layers);
1297 LAYER_CONTEXT *lc = &svc->layer_context[layer];
1298 RATE_CONTROL *lrc = &lc->rc;
1299 lrc->last_q[KEY_FRAME] = rc->last_q[KEY_FRAME];
1300 lrc->avg_frame_qindex[KEY_FRAME] = rc->avg_frame_qindex[KEY_FRAME];
1301 }
1302 }
1289 } else { 1303 } else {
1290 if (rc->is_src_frame_alt_ref || 1304 if (rc->is_src_frame_alt_ref ||
1291 !(cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame) || 1305 !(cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame) ||
1292 (cpi->use_svc && oxcf->rc_mode == VPX_CBR)) { 1306 (cpi->use_svc && oxcf->rc_mode == VPX_CBR)) {
1293 rc->last_q[INTER_FRAME] = qindex; 1307 rc->last_q[INTER_FRAME] = qindex;
1294 rc->avg_frame_qindex[INTER_FRAME] = 1308 rc->avg_frame_qindex[INTER_FRAME] =
1295 ROUND_POWER_OF_TWO(3 * rc->avg_frame_qindex[INTER_FRAME] + qindex, 2); 1309 ROUND_POWER_OF_TWO(3 * rc->avg_frame_qindex[INTER_FRAME] + qindex, 2);
1296 rc->ni_frames++; 1310 rc->ni_frames++;
1297 rc->tot_q += vp9_convert_qindex_to_q(qindex, cm->bit_depth); 1311 rc->tot_q += vp9_convert_qindex_to_q(qindex, cm->bit_depth);
1298 rc->avg_q = rc->tot_q / rc->ni_frames; 1312 rc->avg_q = rc->tot_q / rc->ni_frames;
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
1332 rc->long_rolling_actual_bits = ROUND_POWER_OF_TWO( 1346 rc->long_rolling_actual_bits = ROUND_POWER_OF_TWO(
1333 rc->long_rolling_actual_bits * 31 + rc->projected_frame_size, 5); 1347 rc->long_rolling_actual_bits * 31 + rc->projected_frame_size, 5);
1334 } 1348 }
1335 1349
1336 // Actual bits spent 1350 // Actual bits spent
1337 rc->total_actual_bits += rc->projected_frame_size; 1351 rc->total_actual_bits += rc->projected_frame_size;
1338 rc->total_target_bits += cm->show_frame ? rc->avg_frame_bandwidth : 0; 1352 rc->total_target_bits += cm->show_frame ? rc->avg_frame_bandwidth : 0;
1339 1353
1340 rc->total_target_vs_actual = rc->total_actual_bits - rc->total_target_bits; 1354 rc->total_target_vs_actual = rc->total_actual_bits - rc->total_target_bits;
1341 1355
1342 if (is_altref_enabled(cpi) && cpi->refresh_alt_ref_frame && 1356 if (!cpi->use_svc) {
1343 (cm->frame_type != KEY_FRAME)) 1357 if (is_altref_enabled(cpi) && cpi->refresh_alt_ref_frame &&
1344 // Update the alternate reference frame stats as appropriate. 1358 (cm->frame_type != KEY_FRAME))
1345 update_alt_ref_frame_stats(cpi); 1359 // Update the alternate reference frame stats as appropriate.
1346 else 1360 update_alt_ref_frame_stats(cpi);
1347 // Update the Golden frame stats as appropriate. 1361 else
1348 update_golden_frame_stats(cpi); 1362 // Update the Golden frame stats as appropriate.
1363 update_golden_frame_stats(cpi);
1364 }
1349 1365
1350 if (cm->frame_type == KEY_FRAME) 1366 if (cm->frame_type == KEY_FRAME)
1351 rc->frames_since_key = 0; 1367 rc->frames_since_key = 0;
1352 if (cm->show_frame) { 1368 if (cm->show_frame) {
1353 rc->frames_since_key++; 1369 rc->frames_since_key++;
1354 rc->frames_to_key--; 1370 rc->frames_to_key--;
1355 } 1371 }
1356 1372
1357 // Trigger the resizing of the next frame if it is scaled. 1373 // Trigger the resizing of the next frame if it is scaled.
1358 if (oxcf->pass != 0) { 1374 if (oxcf->pass != 0) {
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
1437 target = calc_pframe_target_size_one_pass_vbr(cpi); 1453 target = calc_pframe_target_size_one_pass_vbr(cpi);
1438 vp9_rc_set_frame_target(cpi, target); 1454 vp9_rc_set_frame_target(cpi, target);
1439 } 1455 }
1440 1456
1441 static int calc_pframe_target_size_one_pass_cbr(const VP9_COMP *cpi) { 1457 static int calc_pframe_target_size_one_pass_cbr(const VP9_COMP *cpi) {
1442 const VP9EncoderConfig *oxcf = &cpi->oxcf; 1458 const VP9EncoderConfig *oxcf = &cpi->oxcf;
1443 const RATE_CONTROL *rc = &cpi->rc; 1459 const RATE_CONTROL *rc = &cpi->rc;
1444 const SVC *const svc = &cpi->svc; 1460 const SVC *const svc = &cpi->svc;
1445 const int64_t diff = rc->optimal_buffer_level - rc->buffer_level; 1461 const int64_t diff = rc->optimal_buffer_level - rc->buffer_level;
1446 const int64_t one_pct_bits = 1 + rc->optimal_buffer_level / 100; 1462 const int64_t one_pct_bits = 1 + rc->optimal_buffer_level / 100;
1447 int min_frame_target = MAX(rc->avg_frame_bandwidth >> 4, FRAME_OVERHEAD_BITS); 1463 int min_frame_target =
1464 VPXMAX(rc->avg_frame_bandwidth >> 4, FRAME_OVERHEAD_BITS);
1448 int target; 1465 int target;
1449 1466
1450 if (oxcf->gf_cbr_boost_pct) { 1467 if (oxcf->gf_cbr_boost_pct) {
1451 const int af_ratio_pct = oxcf->gf_cbr_boost_pct + 100; 1468 const int af_ratio_pct = oxcf->gf_cbr_boost_pct + 100;
1452 target = cpi->refresh_golden_frame ? 1469 target = cpi->refresh_golden_frame ?
1453 (rc->avg_frame_bandwidth * rc->baseline_gf_interval * af_ratio_pct) / 1470 (rc->avg_frame_bandwidth * rc->baseline_gf_interval * af_ratio_pct) /
1454 (rc->baseline_gf_interval * 100 + af_ratio_pct - 100) : 1471 (rc->baseline_gf_interval * 100 + af_ratio_pct - 100) :
1455 (rc->avg_frame_bandwidth * rc->baseline_gf_interval * 100) / 1472 (rc->avg_frame_bandwidth * rc->baseline_gf_interval * 100) /
1456 (rc->baseline_gf_interval * 100 + af_ratio_pct - 100); 1473 (rc->baseline_gf_interval * 100 + af_ratio_pct - 100);
1457 } else { 1474 } else {
1458 target = rc->avg_frame_bandwidth; 1475 target = rc->avg_frame_bandwidth;
1459 } 1476 }
1460 if (is_one_pass_cbr_svc(cpi)) { 1477 if (is_one_pass_cbr_svc(cpi)) {
1461 // Note that for layers, avg_frame_bandwidth is the cumulative 1478 // Note that for layers, avg_frame_bandwidth is the cumulative
1462 // per-frame-bandwidth. For the target size of this frame, use the 1479 // per-frame-bandwidth. For the target size of this frame, use the
1463 // layer average frame size (i.e., non-cumulative per-frame-bw). 1480 // layer average frame size (i.e., non-cumulative per-frame-bw).
1464 int layer = 1481 int layer =
1465 LAYER_IDS_TO_IDX(svc->spatial_layer_id, 1482 LAYER_IDS_TO_IDX(svc->spatial_layer_id,
1466 svc->temporal_layer_id, svc->number_temporal_layers); 1483 svc->temporal_layer_id, svc->number_temporal_layers);
1467 const LAYER_CONTEXT *lc = &svc->layer_context[layer]; 1484 const LAYER_CONTEXT *lc = &svc->layer_context[layer];
1468 target = lc->avg_frame_size; 1485 target = lc->avg_frame_size;
1469 min_frame_target = MAX(lc->avg_frame_size >> 4, FRAME_OVERHEAD_BITS); 1486 min_frame_target = VPXMAX(lc->avg_frame_size >> 4, FRAME_OVERHEAD_BITS);
1470 } 1487 }
1471 if (diff > 0) { 1488 if (diff > 0) {
1472 // Lower the target bandwidth for this frame. 1489 // Lower the target bandwidth for this frame.
1473 const int pct_low = (int)MIN(diff / one_pct_bits, oxcf->under_shoot_pct); 1490 const int pct_low = (int)VPXMIN(diff / one_pct_bits, oxcf->under_shoot_pct);
1474 target -= (target * pct_low) / 200; 1491 target -= (target * pct_low) / 200;
1475 } else if (diff < 0) { 1492 } else if (diff < 0) {
1476 // Increase the target bandwidth for this frame. 1493 // Increase the target bandwidth for this frame.
1477 const int pct_high = (int)MIN(-diff / one_pct_bits, oxcf->over_shoot_pct); 1494 const int pct_high =
1495 (int)VPXMIN(-diff / one_pct_bits, oxcf->over_shoot_pct);
1478 target += (target * pct_high) / 200; 1496 target += (target * pct_high) / 200;
1479 } 1497 }
1480 if (oxcf->rc_max_inter_bitrate_pct) { 1498 if (oxcf->rc_max_inter_bitrate_pct) {
1481 const int max_rate = rc->avg_frame_bandwidth * 1499 const int max_rate = rc->avg_frame_bandwidth *
1482 oxcf->rc_max_inter_bitrate_pct / 100; 1500 oxcf->rc_max_inter_bitrate_pct / 100;
1483 target = MIN(target, max_rate); 1501 target = VPXMIN(target, max_rate);
1484 } 1502 }
1485 return MAX(min_frame_target, target); 1503 return VPXMAX(min_frame_target, target);
1486 } 1504 }
1487 1505
1488 static int calc_iframe_target_size_one_pass_cbr(const VP9_COMP *cpi) { 1506 static int calc_iframe_target_size_one_pass_cbr(const VP9_COMP *cpi) {
1489 const RATE_CONTROL *rc = &cpi->rc; 1507 const RATE_CONTROL *rc = &cpi->rc;
1490 const VP9EncoderConfig *oxcf = &cpi->oxcf; 1508 const VP9EncoderConfig *oxcf = &cpi->oxcf;
1491 const SVC *const svc = &cpi->svc; 1509 const SVC *const svc = &cpi->svc;
1492 int target; 1510 int target;
1493 if (cpi->common.current_video_frame == 0) { 1511 if (cpi->common.current_video_frame == 0) {
1494 target = ((rc->starting_buffer_level / 2) > INT_MAX) 1512 target = ((rc->starting_buffer_level / 2) > INT_MAX)
1495 ? INT_MAX : (int)(rc->starting_buffer_level / 2); 1513 ? INT_MAX : (int)(rc->starting_buffer_level / 2);
1496 } else { 1514 } else {
1497 int kf_boost = 32; 1515 int kf_boost = 32;
1498 double framerate = cpi->framerate; 1516 double framerate = cpi->framerate;
1499 if (svc->number_temporal_layers > 1 && 1517 if (svc->number_temporal_layers > 1 &&
1500 oxcf->rc_mode == VPX_CBR) { 1518 oxcf->rc_mode == VPX_CBR) {
1501 // Use the layer framerate for temporal layers CBR mode. 1519 // Use the layer framerate for temporal layers CBR mode.
1502 const int layer = LAYER_IDS_TO_IDX(svc->spatial_layer_id, 1520 const int layer = LAYER_IDS_TO_IDX(svc->spatial_layer_id,
1503 svc->temporal_layer_id, svc->number_temporal_layers); 1521 svc->temporal_layer_id, svc->number_temporal_layers);
1504 const LAYER_CONTEXT *lc = &svc->layer_context[layer]; 1522 const LAYER_CONTEXT *lc = &svc->layer_context[layer];
1505 framerate = lc->framerate; 1523 framerate = lc->framerate;
1506 } 1524 }
1507 kf_boost = MAX(kf_boost, (int)(2 * framerate - 16)); 1525 kf_boost = VPXMAX(kf_boost, (int)(2 * framerate - 16));
1508 if (rc->frames_since_key < framerate / 2) { 1526 if (rc->frames_since_key < framerate / 2) {
1509 kf_boost = (int)(kf_boost * rc->frames_since_key / 1527 kf_boost = (int)(kf_boost * rc->frames_since_key /
1510 (framerate / 2)); 1528 (framerate / 2));
1511 } 1529 }
1512 target = ((16 + kf_boost) * rc->avg_frame_bandwidth) >> 4; 1530 target = ((16 + kf_boost) * rc->avg_frame_bandwidth) >> 4;
1513 } 1531 }
1514 return vp9_rc_clamp_iframe_target_size(cpi, target); 1532 return vp9_rc_clamp_iframe_target_size(cpi, target);
1515 } 1533 }
1516 1534
1517 // Reset information needed to set proper reference frames and buffer updates 1535 // Reset information needed to set proper reference frames and buffer updates
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after
1705 1723
1706 if (is_altref_enabled(cpi)) { 1724 if (is_altref_enabled(cpi)) {
1707 if (rc->static_scene_max_gf_interval > oxcf->lag_in_frames - 1) 1725 if (rc->static_scene_max_gf_interval > oxcf->lag_in_frames - 1)
1708 rc->static_scene_max_gf_interval = oxcf->lag_in_frames - 1; 1726 rc->static_scene_max_gf_interval = oxcf->lag_in_frames - 1;
1709 } 1727 }
1710 1728
1711 if (rc->max_gf_interval > rc->static_scene_max_gf_interval) 1729 if (rc->max_gf_interval > rc->static_scene_max_gf_interval)
1712 rc->max_gf_interval = rc->static_scene_max_gf_interval; 1730 rc->max_gf_interval = rc->static_scene_max_gf_interval;
1713 1731
1714 // Clamp min to max 1732 // Clamp min to max
1715 rc->min_gf_interval = MIN(rc->min_gf_interval, rc->max_gf_interval); 1733 rc->min_gf_interval = VPXMIN(rc->min_gf_interval, rc->max_gf_interval);
1716 } 1734 }
1717 1735
1718 void vp9_rc_update_framerate(VP9_COMP *cpi) { 1736 void vp9_rc_update_framerate(VP9_COMP *cpi) {
1719 const VP9_COMMON *const cm = &cpi->common; 1737 const VP9_COMMON *const cm = &cpi->common;
1720 const VP9EncoderConfig *const oxcf = &cpi->oxcf; 1738 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
1721 RATE_CONTROL *const rc = &cpi->rc; 1739 RATE_CONTROL *const rc = &cpi->rc;
1722 int vbr_max_bits; 1740 int vbr_max_bits;
1723 1741
1724 rc->avg_frame_bandwidth = (int)(oxcf->target_bandwidth / cpi->framerate); 1742 rc->avg_frame_bandwidth = (int)(oxcf->target_bandwidth / cpi->framerate);
1725 rc->min_frame_bandwidth = (int)(rc->avg_frame_bandwidth * 1743 rc->min_frame_bandwidth = (int)(rc->avg_frame_bandwidth *
1726 oxcf->two_pass_vbrmin_section / 100); 1744 oxcf->two_pass_vbrmin_section / 100);
1727 1745
1728 rc->min_frame_bandwidth = MAX(rc->min_frame_bandwidth, FRAME_OVERHEAD_BITS); 1746 rc->min_frame_bandwidth =
1747 VPXMAX(rc->min_frame_bandwidth, FRAME_OVERHEAD_BITS);
1729 1748
1730 // A maximum bitrate for a frame is defined. 1749 // A maximum bitrate for a frame is defined.
1731 // The baseline for this aligns with HW implementations that 1750 // The baseline for this aligns with HW implementations that
1732 // can support decode of 1080P content up to a bitrate of MAX_MB_RATE bits 1751 // can support decode of 1080P content up to a bitrate of MAX_MB_RATE bits
1733 // per 16x16 MB (averaged over a frame). However this limit is extended if 1752 // per 16x16 MB (averaged over a frame). However this limit is extended if
1734 // a very high rate is given on the command line or the the rate cannnot 1753 // a very high rate is given on the command line or the the rate cannnot
1735 // be acheived because of a user specificed max q (e.g. when the user 1754 // be acheived because of a user specificed max q (e.g. when the user
1736 // specifies lossless encode. 1755 // specifies lossless encode.
1737 vbr_max_bits = (int)(((int64_t)rc->avg_frame_bandwidth * 1756 vbr_max_bits = (int)(((int64_t)rc->avg_frame_bandwidth *
1738 oxcf->two_pass_vbrmax_section) / 100); 1757 oxcf->two_pass_vbrmax_section) / 100);
1739 rc->max_frame_bandwidth = MAX(MAX((cm->MBs * MAX_MB_RATE), MAXRATE_1080P), 1758 rc->max_frame_bandwidth =
1740 vbr_max_bits); 1759 VPXMAX(VPXMAX((cm->MBs * MAX_MB_RATE), MAXRATE_1080P), vbr_max_bits);
1741 1760
1742 vp9_rc_set_gf_interval_range(cpi, rc); 1761 vp9_rc_set_gf_interval_range(cpi, rc);
1743 } 1762 }
1744 1763
1745 #define VBR_PCT_ADJUSTMENT_LIMIT 50 1764 #define VBR_PCT_ADJUSTMENT_LIMIT 50
1746 // For VBR...adjustment to the frame target based on error from previous frames 1765 // For VBR...adjustment to the frame target based on error from previous frames
1747 static void vbr_rate_correction(VP9_COMP *cpi, int *this_frame_target) { 1766 static void vbr_rate_correction(VP9_COMP *cpi, int *this_frame_target) {
1748 RATE_CONTROL *const rc = &cpi->rc; 1767 RATE_CONTROL *const rc = &cpi->rc;
1749 int64_t vbr_bits_off_target = rc->vbr_bits_off_target; 1768 int64_t vbr_bits_off_target = rc->vbr_bits_off_target;
1750 int max_delta; 1769 int max_delta;
(...skipping 17 matching lines...) Expand all
1768 } else { 1787 } else {
1769 *this_frame_target -= 1788 *this_frame_target -=
1770 (vbr_bits_off_target < -max_delta) ? max_delta 1789 (vbr_bits_off_target < -max_delta) ? max_delta
1771 : (int)-vbr_bits_off_target; 1790 : (int)-vbr_bits_off_target;
1772 } 1791 }
1773 1792
1774 // Fast redistribution of bits arising from massive local undershoot. 1793 // Fast redistribution of bits arising from massive local undershoot.
1775 // Dont do it for kf,arf,gf or overlay frames. 1794 // Dont do it for kf,arf,gf or overlay frames.
1776 if (!frame_is_kf_gf_arf(cpi) && !rc->is_src_frame_alt_ref && 1795 if (!frame_is_kf_gf_arf(cpi) && !rc->is_src_frame_alt_ref &&
1777 rc->vbr_bits_off_target_fast) { 1796 rc->vbr_bits_off_target_fast) {
1778 int one_frame_bits = MAX(rc->avg_frame_bandwidth, *this_frame_target); 1797 int one_frame_bits = VPXMAX(rc->avg_frame_bandwidth, *this_frame_target);
1779 int fast_extra_bits; 1798 int fast_extra_bits;
1780 fast_extra_bits = 1799 fast_extra_bits = (int)VPXMIN(rc->vbr_bits_off_target_fast, one_frame_bits);
1781 (int)MIN(rc->vbr_bits_off_target_fast, one_frame_bits); 1800 fast_extra_bits = (int)VPXMIN(
1782 fast_extra_bits = (int)MIN(fast_extra_bits, 1801 fast_extra_bits,
1783 MAX(one_frame_bits / 8, rc->vbr_bits_off_target_fast / 8)); 1802 VPXMAX(one_frame_bits / 8, rc->vbr_bits_off_target_fast / 8));
1784 *this_frame_target += (int)fast_extra_bits; 1803 *this_frame_target += (int)fast_extra_bits;
1785 rc->vbr_bits_off_target_fast -= fast_extra_bits; 1804 rc->vbr_bits_off_target_fast -= fast_extra_bits;
1786 } 1805 }
1787 } 1806 }
1788 1807
1789 void vp9_set_target_rate(VP9_COMP *cpi) { 1808 void vp9_set_target_rate(VP9_COMP *cpi) {
1790 RATE_CONTROL *const rc = &cpi->rc; 1809 RATE_CONTROL *const rc = &cpi->rc;
1791 int target_rate = rc->base_frame_target; 1810 int target_rate = rc->base_frame_target;
1792 1811
1793 // Correction to rate target based on prior over or under shoot. 1812 // Correction to rate target based on prior over or under shoot.
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
1879 // If resize is back up, check if projected q index is too much above the 1898 // If resize is back up, check if projected q index is too much above the
1880 // current base_qindex, and if so, reduce the rate correction factor 1899 // current base_qindex, and if so, reduce the rate correction factor
1881 // (since prefer to keep q for resized frame at least close to previous q). 1900 // (since prefer to keep q for resized frame at least close to previous q).
1882 if (resize_now == -1 && 1901 if (resize_now == -1 &&
1883 qindex > 130 * cm->base_qindex / 100) { 1902 qindex > 130 * cm->base_qindex / 100) {
1884 rc->rate_correction_factors[INTER_NORMAL] *= 0.9; 1903 rc->rate_correction_factors[INTER_NORMAL] *= 0.9;
1885 } 1904 }
1886 } 1905 }
1887 return resize_now; 1906 return resize_now;
1888 } 1907 }
1908
1909 // Compute average source sad (temporal sad: between current source and
1910 // previous source) over a subset of superblocks. Use this is detect big changes
1911 // in content and allow rate control to react.
1912 // TODO(marpan): Superblock sad is computed again in variance partition for
1913 // non-rd mode (but based on last reconstructed frame). Should try to reuse
1914 // these computations.
1915 void vp9_avg_source_sad(VP9_COMP *cpi) {
1916 VP9_COMMON * const cm = &cpi->common;
1917 RATE_CONTROL *const rc = &cpi->rc;
1918 rc->high_source_sad = 0;
1919 if (cpi->Last_Source != NULL) {
1920 const uint8_t *src_y = cpi->Source->y_buffer;
1921 const int src_ystride = cpi->Source->y_stride;
1922 const uint8_t *last_src_y = cpi->Last_Source->y_buffer;
1923 const int last_src_ystride = cpi->Last_Source->y_stride;
1924 int sbi_row, sbi_col;
1925 const BLOCK_SIZE bsize = BLOCK_64X64;
1926 // Loop over sub-sample of frame, and compute average sad over 64x64 blocks.
1927 uint64_t avg_sad = 0;
1928 int num_samples = 0;
1929 int sb_cols = (cm->mi_cols + MI_BLOCK_SIZE - 1) / MI_BLOCK_SIZE;
1930 int sb_rows = (cm->mi_rows + MI_BLOCK_SIZE - 1) / MI_BLOCK_SIZE;
1931 for (sbi_row = 0; sbi_row < sb_rows; sbi_row ++) {
1932 for (sbi_col = 0; sbi_col < sb_cols; sbi_col ++) {
1933 // Checker-board pattern, ignore boundary.
1934 if ((sbi_row > 0 && sbi_col > 0) &&
1935 (sbi_row < sb_rows - 1 && sbi_col < sb_cols - 1) &&
1936 ((sbi_row % 2 == 0 && sbi_col % 2 == 0) ||
1937 (sbi_row % 2 != 0 && sbi_col % 2 != 0))) {
1938 num_samples++;
1939 avg_sad += cpi->fn_ptr[bsize].sdf(src_y,
1940 src_ystride,
1941 last_src_y,
1942 last_src_ystride);
1943 }
1944 src_y += 64;
1945 last_src_y += 64;
1946 }
1947 src_y += (src_ystride << 6) - (sb_cols << 6);
1948 last_src_y += (last_src_ystride << 6) - (sb_cols << 6);
1949 }
1950 if (num_samples > 0)
1951 avg_sad = avg_sad / num_samples;
1952 // Set high_source_sad flag if we detect very high increase in avg_sad
1953 // between current and the previous frame value(s). Use a minimum threshold
1954 // for cases where there is small change from content that is completely
1955 // static.
1956 if (avg_sad > VPXMAX(4000, (rc->avg_source_sad << 3)) &&
1957 rc->frames_since_key > 1)
1958 rc->high_source_sad = 1;
1959 else
1960 rc->high_source_sad = 0;
1961 rc->avg_source_sad = (rc->avg_source_sad + avg_sad) >> 1;
1962 }
1963 }
1964
1965 // Test if encoded frame will significantly overshoot the target bitrate, and
1966 // if so, set the QP, reset/adjust some rate control parameters, and return 1.
1967 int vp9_encodedframe_overshoot(VP9_COMP *cpi,
1968 int frame_size,
1969 int *q) {
1970 VP9_COMMON * const cm = &cpi->common;
1971 RATE_CONTROL *const rc = &cpi->rc;
1972 int thresh_qp = 3 * (rc->worst_quality >> 2);
1973 int thresh_rate = rc->avg_frame_bandwidth * 10;
1974 if (cm->base_qindex < thresh_qp &&
1975 frame_size > thresh_rate) {
1976 // Force a re-encode, and for now use max-QP.
1977 *q = cpi->rc.worst_quality;
1978 // Adjust avg_frame_qindex and buffer_level, as these parameters will affect
1979 // QP selection for subsequent frames. If they have settled down to a very
1980 // different (low QP) state, then not re-adjusting them may cause next
1981 // frame to select low QP and overshoot again.
1982 // TODO(marpan): Check if rate correction factor should also be adjusted.
1983 cpi->rc.avg_frame_qindex[INTER_FRAME] = *q;
1984 rc->buffer_level = rc->optimal_buffer_level;
1985 rc->bits_off_target = rc->optimal_buffer_level;
1986 return 1;
1987 } else {
1988 return 0;
1989 }
1990 }
OLDNEW
« no previous file with comments | « source/libvpx/vp9/encoder/vp9_ratectrl.h ('k') | source/libvpx/vp9/encoder/vp9_rd.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698