1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/libvpx/vp9/encoder/vp9_ratectrl.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,476 @@ 1.4 +/* 1.5 + * Copyright (c) 2010 The WebM project authors. All Rights Reserved. 1.6 + * 1.7 + * Use of this source code is governed by a BSD-style license 1.8 + * that can be found in the LICENSE file in the root of the source 1.9 + * tree. An additional intellectual property rights grant can be found 1.10 + * in the file PATENTS. All contributing project authors may 1.11 + * be found in the AUTHORS file in the root of the source tree. 1.12 + */ 1.13 + 1.14 + 1.15 +#include <stdlib.h> 1.16 +#include <stdio.h> 1.17 +#include <string.h> 1.18 +#include <limits.h> 1.19 +#include <assert.h> 1.20 +#include <math.h> 1.21 + 1.22 +#include "vp9/common/vp9_alloccommon.h" 1.23 +#include "vp9/common/vp9_common.h" 1.24 +#include "vp9/encoder/vp9_ratectrl.h" 1.25 +#include "vp9/common/vp9_entropymode.h" 1.26 +#include "vpx_mem/vpx_mem.h" 1.27 +#include "vp9/common/vp9_systemdependent.h" 1.28 +#include "vp9/encoder/vp9_encodemv.h" 1.29 +#include "vp9/common/vp9_quant_common.h" 1.30 +#include "vp9/common/vp9_seg_common.h" 1.31 + 1.32 +#define MIN_BPB_FACTOR 0.005 1.33 +#define MAX_BPB_FACTOR 50 1.34 + 1.35 +// Bits Per MB at different Q (Multiplied by 512) 1.36 +#define BPER_MB_NORMBITS 9 1.37 + 1.38 +static const unsigned int prior_key_frame_weight[KEY_FRAME_CONTEXT] = 1.39 + { 1, 2, 3, 4, 5 }; 1.40 + 1.41 +// These functions use formulaic calculations to make playing with the 1.42 +// quantizer tables easier. If necessary they can be replaced by lookup 1.43 +// tables if and when things settle down in the experimental bitstream 1.44 +double vp9_convert_qindex_to_q(int qindex) { 1.45 + // Convert the index to a real Q value (scaled down to match old Q values) 1.46 + return vp9_ac_quant(qindex, 0) / 4.0; 1.47 +} 1.48 + 1.49 +int vp9_gfboost_qadjust(int qindex) { 1.50 + const double q = vp9_convert_qindex_to_q(qindex); 1.51 + return (int)((0.00000828 * q * q * q) + 1.52 + (-0.0055 * q * q) + 1.53 + (1.32 * q) + 79.3); 1.54 +} 1.55 + 1.56 +static int kfboost_qadjust(int qindex) { 1.57 + const double q = vp9_convert_qindex_to_q(qindex); 1.58 + return (int)((0.00000973 * q * q * q) + 1.59 + (-0.00613 * q * q) + 1.60 + (1.316 * q) + 121.2); 1.61 +} 1.62 + 1.63 +int vp9_bits_per_mb(FRAME_TYPE frame_type, int qindex, 1.64 + double correction_factor) { 1.65 + const double q = vp9_convert_qindex_to_q(qindex); 1.66 + int enumerator = frame_type == KEY_FRAME ? 3300000 : 2250000; 1.67 + 1.68 + // q based adjustment to baseline enumerator 1.69 + enumerator += (int)(enumerator * q) >> 12; 1.70 + return (int)(0.5 + (enumerator * correction_factor / q)); 1.71 +} 1.72 + 1.73 +void vp9_save_coding_context(VP9_COMP *cpi) { 1.74 + CODING_CONTEXT *const cc = &cpi->coding_context; 1.75 + VP9_COMMON *cm = &cpi->common; 1.76 + 1.77 + // Stores a snapshot of key state variables which can subsequently be 1.78 + // restored with a call to vp9_restore_coding_context. These functions are 1.79 + // intended for use in a re-code loop in vp9_compress_frame where the 1.80 + // quantizer value is adjusted between loop iterations. 1.81 + vp9_copy(cc->nmvjointcost, cpi->mb.nmvjointcost); 1.82 + vp9_copy(cc->nmvcosts, cpi->mb.nmvcosts); 1.83 + vp9_copy(cc->nmvcosts_hp, cpi->mb.nmvcosts_hp); 1.84 + 1.85 + vp9_copy(cc->segment_pred_probs, cm->seg.pred_probs); 1.86 + 1.87 + vpx_memcpy(cpi->coding_context.last_frame_seg_map_copy, 1.88 + cm->last_frame_seg_map, (cm->mi_rows * cm->mi_cols)); 1.89 + 1.90 + vp9_copy(cc->last_ref_lf_deltas, cm->lf.last_ref_deltas); 1.91 + vp9_copy(cc->last_mode_lf_deltas, cm->lf.last_mode_deltas); 1.92 + 1.93 + cc->fc = cm->fc; 1.94 +} 1.95 + 1.96 +void vp9_restore_coding_context(VP9_COMP *cpi) { 1.97 + CODING_CONTEXT *const cc = &cpi->coding_context; 1.98 + VP9_COMMON *cm = &cpi->common; 1.99 + 1.100 + // Restore key state variables to the snapshot state stored in the 1.101 + // previous call to vp9_save_coding_context. 1.102 + vp9_copy(cpi->mb.nmvjointcost, cc->nmvjointcost); 1.103 + vp9_copy(cpi->mb.nmvcosts, cc->nmvcosts); 1.104 + vp9_copy(cpi->mb.nmvcosts_hp, cc->nmvcosts_hp); 1.105 + 1.106 + vp9_copy(cm->seg.pred_probs, cc->segment_pred_probs); 1.107 + 1.108 + vpx_memcpy(cm->last_frame_seg_map, 1.109 + cpi->coding_context.last_frame_seg_map_copy, 1.110 + (cm->mi_rows * cm->mi_cols)); 1.111 + 1.112 + vp9_copy(cm->lf.last_ref_deltas, cc->last_ref_lf_deltas); 1.113 + vp9_copy(cm->lf.last_mode_deltas, cc->last_mode_lf_deltas); 1.114 + 1.115 + cm->fc = cc->fc; 1.116 +} 1.117 + 1.118 +void vp9_setup_key_frame(VP9_COMP *cpi) { 1.119 + VP9_COMMON *cm = &cpi->common; 1.120 + 1.121 + vp9_setup_past_independence(cm); 1.122 + 1.123 + // interval before next GF 1.124 + cpi->frames_till_gf_update_due = cpi->baseline_gf_interval; 1.125 + /* All buffers are implicitly updated on key frames. */ 1.126 + cpi->refresh_golden_frame = 1; 1.127 + cpi->refresh_alt_ref_frame = 1; 1.128 +} 1.129 + 1.130 +void vp9_setup_inter_frame(VP9_COMP *cpi) { 1.131 + VP9_COMMON *cm = &cpi->common; 1.132 + if (cm->error_resilient_mode || cm->intra_only) 1.133 + vp9_setup_past_independence(cm); 1.134 + 1.135 + assert(cm->frame_context_idx < NUM_FRAME_CONTEXTS); 1.136 + cm->fc = cm->frame_contexts[cm->frame_context_idx]; 1.137 +} 1.138 + 1.139 +static int estimate_bits_at_q(int frame_kind, int q, int mbs, 1.140 + double correction_factor) { 1.141 + const int bpm = (int)(vp9_bits_per_mb(frame_kind, q, correction_factor)); 1.142 + 1.143 + // Attempt to retain reasonable accuracy without overflow. The cutoff is 1.144 + // chosen such that the maximum product of Bpm and MBs fits 31 bits. The 1.145 + // largest Bpm takes 20 bits. 1.146 + return (mbs > (1 << 11)) ? (bpm >> BPER_MB_NORMBITS) * mbs 1.147 + : (bpm * mbs) >> BPER_MB_NORMBITS; 1.148 +} 1.149 + 1.150 + 1.151 +static void calc_iframe_target_size(VP9_COMP *cpi) { 1.152 + // boost defaults to half second 1.153 + int target; 1.154 + 1.155 + // Clear down mmx registers to allow floating point in what follows 1.156 + vp9_clear_system_state(); // __asm emms; 1.157 + 1.158 + // New Two pass RC 1.159 + target = cpi->per_frame_bandwidth; 1.160 + 1.161 + if (cpi->oxcf.rc_max_intra_bitrate_pct) { 1.162 + int max_rate = cpi->per_frame_bandwidth 1.163 + * cpi->oxcf.rc_max_intra_bitrate_pct / 100; 1.164 + 1.165 + if (target > max_rate) 1.166 + target = max_rate; 1.167 + } 1.168 + 1.169 + cpi->this_frame_target = target; 1.170 +} 1.171 + 1.172 + 1.173 +// Do the best we can to define the parameters for the next GF based 1.174 +// on what information we have available. 1.175 +// 1.176 +// In this experimental code only two pass is supported 1.177 +// so we just use the interval determined in the two pass code. 1.178 +static void calc_gf_params(VP9_COMP *cpi) { 1.179 + // Set the gf interval 1.180 + cpi->frames_till_gf_update_due = cpi->baseline_gf_interval; 1.181 +} 1.182 + 1.183 + 1.184 +static void calc_pframe_target_size(VP9_COMP *cpi) { 1.185 + const int min_frame_target = MAX(cpi->min_frame_bandwidth, 1.186 + cpi->av_per_frame_bandwidth >> 5); 1.187 + if (cpi->refresh_alt_ref_frame) { 1.188 + // Special alt reference frame case 1.189 + // Per frame bit target for the alt ref frame 1.190 + cpi->per_frame_bandwidth = cpi->twopass.gf_bits; 1.191 + cpi->this_frame_target = cpi->per_frame_bandwidth; 1.192 + } else { 1.193 + // Normal frames (gf,and inter) 1.194 + cpi->this_frame_target = cpi->per_frame_bandwidth; 1.195 + } 1.196 + 1.197 + // Check that the total sum of adjustments is not above the maximum allowed. 1.198 + // That is, having allowed for the KF and GF penalties, we have not pushed 1.199 + // the current inter-frame target too low. If the adjustment we apply here is 1.200 + // not capable of recovering all the extra bits we have spent in the KF or GF, 1.201 + // then the remainder will have to be recovered over a longer time span via 1.202 + // other buffer / rate control mechanisms. 1.203 + if (cpi->this_frame_target < min_frame_target) 1.204 + cpi->this_frame_target = min_frame_target; 1.205 + 1.206 + if (!cpi->refresh_alt_ref_frame) 1.207 + // Note the baseline target data rate for this inter frame. 1.208 + cpi->inter_frame_target = cpi->this_frame_target; 1.209 + 1.210 + // Adjust target frame size for Golden Frames: 1.211 + if (cpi->frames_till_gf_update_due == 0) { 1.212 + const int q = (cpi->oxcf.fixed_q < 0) ? cpi->last_q[INTER_FRAME] 1.213 + : cpi->oxcf.fixed_q; 1.214 + 1.215 + cpi->refresh_golden_frame = 1; 1.216 + 1.217 + calc_gf_params(cpi); 1.218 + 1.219 + // If we are using alternate ref instead of gf then do not apply the boost 1.220 + // It will instead be applied to the altref update 1.221 + // Jims modified boost 1.222 + if (!cpi->source_alt_ref_active) { 1.223 + if (cpi->oxcf.fixed_q < 0) { 1.224 + // The spend on the GF is defined in the two pass code 1.225 + // for two pass encodes 1.226 + cpi->this_frame_target = cpi->per_frame_bandwidth; 1.227 + } else { 1.228 + cpi->this_frame_target = 1.229 + (estimate_bits_at_q(1, q, cpi->common.MBs, 1.0) 1.230 + * cpi->last_boost) / 100; 1.231 + } 1.232 + } else { 1.233 + // If there is an active ARF at this location use the minimum 1.234 + // bits on this frame even if it is a constructed arf. 1.235 + // The active maximum quantizer insures that an appropriate 1.236 + // number of bits will be spent if needed for constructed ARFs. 1.237 + cpi->this_frame_target = 0; 1.238 + } 1.239 + } 1.240 +} 1.241 + 1.242 + 1.243 +void vp9_update_rate_correction_factors(VP9_COMP *cpi, int damp_var) { 1.244 + const int q = cpi->common.base_qindex; 1.245 + int correction_factor = 100; 1.246 + double rate_correction_factor; 1.247 + double adjustment_limit; 1.248 + 1.249 + int projected_size_based_on_q = 0; 1.250 + 1.251 + // Clear down mmx registers to allow floating point in what follows 1.252 + vp9_clear_system_state(); // __asm emms; 1.253 + 1.254 + if (cpi->common.frame_type == KEY_FRAME) { 1.255 + rate_correction_factor = cpi->key_frame_rate_correction_factor; 1.256 + } else { 1.257 + if (cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) 1.258 + rate_correction_factor = cpi->gf_rate_correction_factor; 1.259 + else 1.260 + rate_correction_factor = cpi->rate_correction_factor; 1.261 + } 1.262 + 1.263 + // Work out how big we would have expected the frame to be at this Q given 1.264 + // the current correction factor. 1.265 + // Stay in double to avoid int overflow when values are large 1.266 + projected_size_based_on_q = estimate_bits_at_q(cpi->common.frame_type, q, 1.267 + cpi->common.MBs, 1.268 + rate_correction_factor); 1.269 + 1.270 + // Work out a size correction factor. 1.271 + if (projected_size_based_on_q > 0) 1.272 + correction_factor = 1.273 + (100 * cpi->projected_frame_size) / projected_size_based_on_q; 1.274 + 1.275 + // More heavily damped adjustment used if we have been oscillating either side 1.276 + // of target. 1.277 + switch (damp_var) { 1.278 + case 0: 1.279 + adjustment_limit = 0.75; 1.280 + break; 1.281 + case 1: 1.282 + adjustment_limit = 0.375; 1.283 + break; 1.284 + case 2: 1.285 + default: 1.286 + adjustment_limit = 0.25; 1.287 + break; 1.288 + } 1.289 + 1.290 + // if ( (correction_factor > 102) && (Q < cpi->active_worst_quality) ) 1.291 + if (correction_factor > 102) { 1.292 + // We are not already at the worst allowable quality 1.293 + correction_factor = 1.294 + (int)(100 + ((correction_factor - 100) * adjustment_limit)); 1.295 + rate_correction_factor = 1.296 + ((rate_correction_factor * correction_factor) / 100); 1.297 + 1.298 + // Keep rate_correction_factor within limits 1.299 + if (rate_correction_factor > MAX_BPB_FACTOR) 1.300 + rate_correction_factor = MAX_BPB_FACTOR; 1.301 + } else if (correction_factor < 99) { 1.302 + // We are not already at the best allowable quality 1.303 + correction_factor = 1.304 + (int)(100 - ((100 - correction_factor) * adjustment_limit)); 1.305 + rate_correction_factor = 1.306 + ((rate_correction_factor * correction_factor) / 100); 1.307 + 1.308 + // Keep rate_correction_factor within limits 1.309 + if (rate_correction_factor < MIN_BPB_FACTOR) 1.310 + rate_correction_factor = MIN_BPB_FACTOR; 1.311 + } 1.312 + 1.313 + if (cpi->common.frame_type == KEY_FRAME) { 1.314 + cpi->key_frame_rate_correction_factor = rate_correction_factor; 1.315 + } else { 1.316 + if (cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) 1.317 + cpi->gf_rate_correction_factor = rate_correction_factor; 1.318 + else 1.319 + cpi->rate_correction_factor = rate_correction_factor; 1.320 + } 1.321 +} 1.322 + 1.323 + 1.324 +int vp9_regulate_q(VP9_COMP *cpi, int target_bits_per_frame) { 1.325 + int q = cpi->active_worst_quality; 1.326 + 1.327 + int i; 1.328 + int last_error = INT_MAX; 1.329 + int target_bits_per_mb; 1.330 + int bits_per_mb_at_this_q; 1.331 + double correction_factor; 1.332 + 1.333 + // Select the appropriate correction factor based upon type of frame. 1.334 + if (cpi->common.frame_type == KEY_FRAME) { 1.335 + correction_factor = cpi->key_frame_rate_correction_factor; 1.336 + } else { 1.337 + if (cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) 1.338 + correction_factor = cpi->gf_rate_correction_factor; 1.339 + else 1.340 + correction_factor = cpi->rate_correction_factor; 1.341 + } 1.342 + 1.343 + // Calculate required scaling factor based on target frame size and size of 1.344 + // frame produced using previous Q. 1.345 + if (target_bits_per_frame >= (INT_MAX >> BPER_MB_NORMBITS)) 1.346 + target_bits_per_mb = 1.347 + (target_bits_per_frame / cpi->common.MBs) 1.348 + << BPER_MB_NORMBITS; // Case where we would overflow int 1.349 + else 1.350 + target_bits_per_mb = 1.351 + (target_bits_per_frame << BPER_MB_NORMBITS) / cpi->common.MBs; 1.352 + 1.353 + i = cpi->active_best_quality; 1.354 + 1.355 + do { 1.356 + bits_per_mb_at_this_q = (int)vp9_bits_per_mb(cpi->common.frame_type, i, 1.357 + correction_factor); 1.358 + 1.359 + if (bits_per_mb_at_this_q <= target_bits_per_mb) { 1.360 + if ((target_bits_per_mb - bits_per_mb_at_this_q) <= last_error) 1.361 + q = i; 1.362 + else 1.363 + q = i - 1; 1.364 + 1.365 + break; 1.366 + } else { 1.367 + last_error = bits_per_mb_at_this_q - target_bits_per_mb; 1.368 + } 1.369 + } while (++i <= cpi->active_worst_quality); 1.370 + 1.371 + return q; 1.372 +} 1.373 + 1.374 + 1.375 +static int estimate_keyframe_frequency(VP9_COMP *cpi) { 1.376 + int i; 1.377 + 1.378 + // Average key frame frequency 1.379 + int av_key_frame_frequency = 0; 1.380 + 1.381 + /* First key frame at start of sequence is a special case. We have no 1.382 + * frequency data. 1.383 + */ 1.384 + if (cpi->key_frame_count == 1) { 1.385 + /* Assume a default of 1 kf every 2 seconds, or the max kf interval, 1.386 + * whichever is smaller. 1.387 + */ 1.388 + int key_freq = cpi->oxcf.key_freq > 0 ? cpi->oxcf.key_freq : 1; 1.389 + av_key_frame_frequency = (int)cpi->output_framerate * 2; 1.390 + 1.391 + if (cpi->oxcf.auto_key && av_key_frame_frequency > key_freq) 1.392 + av_key_frame_frequency = cpi->oxcf.key_freq; 1.393 + 1.394 + cpi->prior_key_frame_distance[KEY_FRAME_CONTEXT - 1] 1.395 + = av_key_frame_frequency; 1.396 + } else { 1.397 + unsigned int total_weight = 0; 1.398 + int last_kf_interval = 1.399 + (cpi->frames_since_key > 0) ? cpi->frames_since_key : 1; 1.400 + 1.401 + /* reset keyframe context and calculate weighted average of last 1.402 + * KEY_FRAME_CONTEXT keyframes 1.403 + */ 1.404 + for (i = 0; i < KEY_FRAME_CONTEXT; i++) { 1.405 + if (i < KEY_FRAME_CONTEXT - 1) 1.406 + cpi->prior_key_frame_distance[i] 1.407 + = cpi->prior_key_frame_distance[i + 1]; 1.408 + else 1.409 + cpi->prior_key_frame_distance[i] = last_kf_interval; 1.410 + 1.411 + av_key_frame_frequency += prior_key_frame_weight[i] 1.412 + * cpi->prior_key_frame_distance[i]; 1.413 + total_weight += prior_key_frame_weight[i]; 1.414 + } 1.415 + 1.416 + av_key_frame_frequency /= total_weight; 1.417 + } 1.418 + return av_key_frame_frequency; 1.419 +} 1.420 + 1.421 + 1.422 +void vp9_adjust_key_frame_context(VP9_COMP *cpi) { 1.423 + // Clear down mmx registers to allow floating point in what follows 1.424 + vp9_clear_system_state(); 1.425 + 1.426 + cpi->frames_since_key = 0; 1.427 + cpi->key_frame_count++; 1.428 +} 1.429 + 1.430 + 1.431 +void vp9_compute_frame_size_bounds(VP9_COMP *cpi, int *frame_under_shoot_limit, 1.432 + int *frame_over_shoot_limit) { 1.433 + // Set-up bounds on acceptable frame size: 1.434 + if (cpi->oxcf.fixed_q >= 0) { 1.435 + // Fixed Q scenario: frame size never outranges target (there is no target!) 1.436 + *frame_under_shoot_limit = 0; 1.437 + *frame_over_shoot_limit = INT_MAX; 1.438 + } else { 1.439 + if (cpi->common.frame_type == KEY_FRAME) { 1.440 + *frame_over_shoot_limit = cpi->this_frame_target * 9 / 8; 1.441 + *frame_under_shoot_limit = cpi->this_frame_target * 7 / 8; 1.442 + } else { 1.443 + if (cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) { 1.444 + *frame_over_shoot_limit = cpi->this_frame_target * 9 / 8; 1.445 + *frame_under_shoot_limit = cpi->this_frame_target * 7 / 8; 1.446 + } else { 1.447 + // Stron overshoot limit for constrained quality 1.448 + if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) { 1.449 + *frame_over_shoot_limit = cpi->this_frame_target * 11 / 8; 1.450 + *frame_under_shoot_limit = cpi->this_frame_target * 2 / 8; 1.451 + } else { 1.452 + *frame_over_shoot_limit = cpi->this_frame_target * 11 / 8; 1.453 + *frame_under_shoot_limit = cpi->this_frame_target * 5 / 8; 1.454 + } 1.455 + } 1.456 + } 1.457 + 1.458 + // For very small rate targets where the fractional adjustment 1.459 + // (eg * 7/8) may be tiny make sure there is at least a minimum 1.460 + // range. 1.461 + *frame_over_shoot_limit += 200; 1.462 + *frame_under_shoot_limit -= 200; 1.463 + if (*frame_under_shoot_limit < 0) 1.464 + *frame_under_shoot_limit = 0; 1.465 + } 1.466 +} 1.467 + 1.468 + 1.469 +// return of 0 means drop frame 1.470 +int vp9_pick_frame_size(VP9_COMP *cpi) { 1.471 + VP9_COMMON *cm = &cpi->common; 1.472 + 1.473 + if (cm->frame_type == KEY_FRAME) 1.474 + calc_iframe_target_size(cpi); 1.475 + else 1.476 + calc_pframe_target_size(cpi); 1.477 + 1.478 + return 1; 1.479 +}