Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright (c) 2010 The WebM project authors. All Rights Reserved. |
michael@0 | 3 | * |
michael@0 | 4 | * Use of this source code is governed by a BSD-style license |
michael@0 | 5 | * that can be found in the LICENSE file in the root of the source |
michael@0 | 6 | * tree. An additional intellectual property rights grant can be found |
michael@0 | 7 | * in the file PATENTS. All contributing project authors may |
michael@0 | 8 | * be found in the AUTHORS file in the root of the source tree. |
michael@0 | 9 | */ |
michael@0 | 10 | |
michael@0 | 11 | |
michael@0 | 12 | #include <stdlib.h> |
michael@0 | 13 | #include <stdio.h> |
michael@0 | 14 | #include <string.h> |
michael@0 | 15 | #include <limits.h> |
michael@0 | 16 | #include <assert.h> |
michael@0 | 17 | #include <math.h> |
michael@0 | 18 | |
michael@0 | 19 | #include "vp9/common/vp9_alloccommon.h" |
michael@0 | 20 | #include "vp9/common/vp9_common.h" |
michael@0 | 21 | #include "vp9/encoder/vp9_ratectrl.h" |
michael@0 | 22 | #include "vp9/common/vp9_entropymode.h" |
michael@0 | 23 | #include "vpx_mem/vpx_mem.h" |
michael@0 | 24 | #include "vp9/common/vp9_systemdependent.h" |
michael@0 | 25 | #include "vp9/encoder/vp9_encodemv.h" |
michael@0 | 26 | #include "vp9/common/vp9_quant_common.h" |
michael@0 | 27 | #include "vp9/common/vp9_seg_common.h" |
michael@0 | 28 | |
michael@0 | 29 | #define MIN_BPB_FACTOR 0.005 |
michael@0 | 30 | #define MAX_BPB_FACTOR 50 |
michael@0 | 31 | |
michael@0 | 32 | // Bits Per MB at different Q (Multiplied by 512) |
michael@0 | 33 | #define BPER_MB_NORMBITS 9 |
michael@0 | 34 | |
michael@0 | 35 | static const unsigned int prior_key_frame_weight[KEY_FRAME_CONTEXT] = |
michael@0 | 36 | { 1, 2, 3, 4, 5 }; |
michael@0 | 37 | |
michael@0 | 38 | // These functions use formulaic calculations to make playing with the |
michael@0 | 39 | // quantizer tables easier. If necessary they can be replaced by lookup |
michael@0 | 40 | // tables if and when things settle down in the experimental bitstream |
michael@0 | 41 | double vp9_convert_qindex_to_q(int qindex) { |
michael@0 | 42 | // Convert the index to a real Q value (scaled down to match old Q values) |
michael@0 | 43 | return vp9_ac_quant(qindex, 0) / 4.0; |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | int vp9_gfboost_qadjust(int qindex) { |
michael@0 | 47 | const double q = vp9_convert_qindex_to_q(qindex); |
michael@0 | 48 | return (int)((0.00000828 * q * q * q) + |
michael@0 | 49 | (-0.0055 * q * q) + |
michael@0 | 50 | (1.32 * q) + 79.3); |
michael@0 | 51 | } |
michael@0 | 52 | |
michael@0 | 53 | static int kfboost_qadjust(int qindex) { |
michael@0 | 54 | const double q = vp9_convert_qindex_to_q(qindex); |
michael@0 | 55 | return (int)((0.00000973 * q * q * q) + |
michael@0 | 56 | (-0.00613 * q * q) + |
michael@0 | 57 | (1.316 * q) + 121.2); |
michael@0 | 58 | } |
michael@0 | 59 | |
michael@0 | 60 | int vp9_bits_per_mb(FRAME_TYPE frame_type, int qindex, |
michael@0 | 61 | double correction_factor) { |
michael@0 | 62 | const double q = vp9_convert_qindex_to_q(qindex); |
michael@0 | 63 | int enumerator = frame_type == KEY_FRAME ? 3300000 : 2250000; |
michael@0 | 64 | |
michael@0 | 65 | // q based adjustment to baseline enumerator |
michael@0 | 66 | enumerator += (int)(enumerator * q) >> 12; |
michael@0 | 67 | return (int)(0.5 + (enumerator * correction_factor / q)); |
michael@0 | 68 | } |
michael@0 | 69 | |
michael@0 | 70 | void vp9_save_coding_context(VP9_COMP *cpi) { |
michael@0 | 71 | CODING_CONTEXT *const cc = &cpi->coding_context; |
michael@0 | 72 | VP9_COMMON *cm = &cpi->common; |
michael@0 | 73 | |
michael@0 | 74 | // Stores a snapshot of key state variables which can subsequently be |
michael@0 | 75 | // restored with a call to vp9_restore_coding_context. These functions are |
michael@0 | 76 | // intended for use in a re-code loop in vp9_compress_frame where the |
michael@0 | 77 | // quantizer value is adjusted between loop iterations. |
michael@0 | 78 | vp9_copy(cc->nmvjointcost, cpi->mb.nmvjointcost); |
michael@0 | 79 | vp9_copy(cc->nmvcosts, cpi->mb.nmvcosts); |
michael@0 | 80 | vp9_copy(cc->nmvcosts_hp, cpi->mb.nmvcosts_hp); |
michael@0 | 81 | |
michael@0 | 82 | vp9_copy(cc->segment_pred_probs, cm->seg.pred_probs); |
michael@0 | 83 | |
michael@0 | 84 | vpx_memcpy(cpi->coding_context.last_frame_seg_map_copy, |
michael@0 | 85 | cm->last_frame_seg_map, (cm->mi_rows * cm->mi_cols)); |
michael@0 | 86 | |
michael@0 | 87 | vp9_copy(cc->last_ref_lf_deltas, cm->lf.last_ref_deltas); |
michael@0 | 88 | vp9_copy(cc->last_mode_lf_deltas, cm->lf.last_mode_deltas); |
michael@0 | 89 | |
michael@0 | 90 | cc->fc = cm->fc; |
michael@0 | 91 | } |
michael@0 | 92 | |
michael@0 | 93 | void vp9_restore_coding_context(VP9_COMP *cpi) { |
michael@0 | 94 | CODING_CONTEXT *const cc = &cpi->coding_context; |
michael@0 | 95 | VP9_COMMON *cm = &cpi->common; |
michael@0 | 96 | |
michael@0 | 97 | // Restore key state variables to the snapshot state stored in the |
michael@0 | 98 | // previous call to vp9_save_coding_context. |
michael@0 | 99 | vp9_copy(cpi->mb.nmvjointcost, cc->nmvjointcost); |
michael@0 | 100 | vp9_copy(cpi->mb.nmvcosts, cc->nmvcosts); |
michael@0 | 101 | vp9_copy(cpi->mb.nmvcosts_hp, cc->nmvcosts_hp); |
michael@0 | 102 | |
michael@0 | 103 | vp9_copy(cm->seg.pred_probs, cc->segment_pred_probs); |
michael@0 | 104 | |
michael@0 | 105 | vpx_memcpy(cm->last_frame_seg_map, |
michael@0 | 106 | cpi->coding_context.last_frame_seg_map_copy, |
michael@0 | 107 | (cm->mi_rows * cm->mi_cols)); |
michael@0 | 108 | |
michael@0 | 109 | vp9_copy(cm->lf.last_ref_deltas, cc->last_ref_lf_deltas); |
michael@0 | 110 | vp9_copy(cm->lf.last_mode_deltas, cc->last_mode_lf_deltas); |
michael@0 | 111 | |
michael@0 | 112 | cm->fc = cc->fc; |
michael@0 | 113 | } |
michael@0 | 114 | |
michael@0 | 115 | void vp9_setup_key_frame(VP9_COMP *cpi) { |
michael@0 | 116 | VP9_COMMON *cm = &cpi->common; |
michael@0 | 117 | |
michael@0 | 118 | vp9_setup_past_independence(cm); |
michael@0 | 119 | |
michael@0 | 120 | // interval before next GF |
michael@0 | 121 | cpi->frames_till_gf_update_due = cpi->baseline_gf_interval; |
michael@0 | 122 | /* All buffers are implicitly updated on key frames. */ |
michael@0 | 123 | cpi->refresh_golden_frame = 1; |
michael@0 | 124 | cpi->refresh_alt_ref_frame = 1; |
michael@0 | 125 | } |
michael@0 | 126 | |
michael@0 | 127 | void vp9_setup_inter_frame(VP9_COMP *cpi) { |
michael@0 | 128 | VP9_COMMON *cm = &cpi->common; |
michael@0 | 129 | if (cm->error_resilient_mode || cm->intra_only) |
michael@0 | 130 | vp9_setup_past_independence(cm); |
michael@0 | 131 | |
michael@0 | 132 | assert(cm->frame_context_idx < NUM_FRAME_CONTEXTS); |
michael@0 | 133 | cm->fc = cm->frame_contexts[cm->frame_context_idx]; |
michael@0 | 134 | } |
michael@0 | 135 | |
michael@0 | 136 | static int estimate_bits_at_q(int frame_kind, int q, int mbs, |
michael@0 | 137 | double correction_factor) { |
michael@0 | 138 | const int bpm = (int)(vp9_bits_per_mb(frame_kind, q, correction_factor)); |
michael@0 | 139 | |
michael@0 | 140 | // Attempt to retain reasonable accuracy without overflow. The cutoff is |
michael@0 | 141 | // chosen such that the maximum product of Bpm and MBs fits 31 bits. The |
michael@0 | 142 | // largest Bpm takes 20 bits. |
michael@0 | 143 | return (mbs > (1 << 11)) ? (bpm >> BPER_MB_NORMBITS) * mbs |
michael@0 | 144 | : (bpm * mbs) >> BPER_MB_NORMBITS; |
michael@0 | 145 | } |
michael@0 | 146 | |
michael@0 | 147 | |
michael@0 | 148 | static void calc_iframe_target_size(VP9_COMP *cpi) { |
michael@0 | 149 | // boost defaults to half second |
michael@0 | 150 | int target; |
michael@0 | 151 | |
michael@0 | 152 | // Clear down mmx registers to allow floating point in what follows |
michael@0 | 153 | vp9_clear_system_state(); // __asm emms; |
michael@0 | 154 | |
michael@0 | 155 | // New Two pass RC |
michael@0 | 156 | target = cpi->per_frame_bandwidth; |
michael@0 | 157 | |
michael@0 | 158 | if (cpi->oxcf.rc_max_intra_bitrate_pct) { |
michael@0 | 159 | int max_rate = cpi->per_frame_bandwidth |
michael@0 | 160 | * cpi->oxcf.rc_max_intra_bitrate_pct / 100; |
michael@0 | 161 | |
michael@0 | 162 | if (target > max_rate) |
michael@0 | 163 | target = max_rate; |
michael@0 | 164 | } |
michael@0 | 165 | |
michael@0 | 166 | cpi->this_frame_target = target; |
michael@0 | 167 | } |
michael@0 | 168 | |
michael@0 | 169 | |
michael@0 | 170 | // Do the best we can to define the parameters for the next GF based |
michael@0 | 171 | // on what information we have available. |
michael@0 | 172 | // |
michael@0 | 173 | // In this experimental code only two pass is supported |
michael@0 | 174 | // so we just use the interval determined in the two pass code. |
michael@0 | 175 | static void calc_gf_params(VP9_COMP *cpi) { |
michael@0 | 176 | // Set the gf interval |
michael@0 | 177 | cpi->frames_till_gf_update_due = cpi->baseline_gf_interval; |
michael@0 | 178 | } |
michael@0 | 179 | |
michael@0 | 180 | |
michael@0 | 181 | static void calc_pframe_target_size(VP9_COMP *cpi) { |
michael@0 | 182 | const int min_frame_target = MAX(cpi->min_frame_bandwidth, |
michael@0 | 183 | cpi->av_per_frame_bandwidth >> 5); |
michael@0 | 184 | if (cpi->refresh_alt_ref_frame) { |
michael@0 | 185 | // Special alt reference frame case |
michael@0 | 186 | // Per frame bit target for the alt ref frame |
michael@0 | 187 | cpi->per_frame_bandwidth = cpi->twopass.gf_bits; |
michael@0 | 188 | cpi->this_frame_target = cpi->per_frame_bandwidth; |
michael@0 | 189 | } else { |
michael@0 | 190 | // Normal frames (gf,and inter) |
michael@0 | 191 | cpi->this_frame_target = cpi->per_frame_bandwidth; |
michael@0 | 192 | } |
michael@0 | 193 | |
michael@0 | 194 | // Check that the total sum of adjustments is not above the maximum allowed. |
michael@0 | 195 | // That is, having allowed for the KF and GF penalties, we have not pushed |
michael@0 | 196 | // the current inter-frame target too low. If the adjustment we apply here is |
michael@0 | 197 | // not capable of recovering all the extra bits we have spent in the KF or GF, |
michael@0 | 198 | // then the remainder will have to be recovered over a longer time span via |
michael@0 | 199 | // other buffer / rate control mechanisms. |
michael@0 | 200 | if (cpi->this_frame_target < min_frame_target) |
michael@0 | 201 | cpi->this_frame_target = min_frame_target; |
michael@0 | 202 | |
michael@0 | 203 | if (!cpi->refresh_alt_ref_frame) |
michael@0 | 204 | // Note the baseline target data rate for this inter frame. |
michael@0 | 205 | cpi->inter_frame_target = cpi->this_frame_target; |
michael@0 | 206 | |
michael@0 | 207 | // Adjust target frame size for Golden Frames: |
michael@0 | 208 | if (cpi->frames_till_gf_update_due == 0) { |
michael@0 | 209 | const int q = (cpi->oxcf.fixed_q < 0) ? cpi->last_q[INTER_FRAME] |
michael@0 | 210 | : cpi->oxcf.fixed_q; |
michael@0 | 211 | |
michael@0 | 212 | cpi->refresh_golden_frame = 1; |
michael@0 | 213 | |
michael@0 | 214 | calc_gf_params(cpi); |
michael@0 | 215 | |
michael@0 | 216 | // If we are using alternate ref instead of gf then do not apply the boost |
michael@0 | 217 | // It will instead be applied to the altref update |
michael@0 | 218 | // Jims modified boost |
michael@0 | 219 | if (!cpi->source_alt_ref_active) { |
michael@0 | 220 | if (cpi->oxcf.fixed_q < 0) { |
michael@0 | 221 | // The spend on the GF is defined in the two pass code |
michael@0 | 222 | // for two pass encodes |
michael@0 | 223 | cpi->this_frame_target = cpi->per_frame_bandwidth; |
michael@0 | 224 | } else { |
michael@0 | 225 | cpi->this_frame_target = |
michael@0 | 226 | (estimate_bits_at_q(1, q, cpi->common.MBs, 1.0) |
michael@0 | 227 | * cpi->last_boost) / 100; |
michael@0 | 228 | } |
michael@0 | 229 | } else { |
michael@0 | 230 | // If there is an active ARF at this location use the minimum |
michael@0 | 231 | // bits on this frame even if it is a constructed arf. |
michael@0 | 232 | // The active maximum quantizer insures that an appropriate |
michael@0 | 233 | // number of bits will be spent if needed for constructed ARFs. |
michael@0 | 234 | cpi->this_frame_target = 0; |
michael@0 | 235 | } |
michael@0 | 236 | } |
michael@0 | 237 | } |
michael@0 | 238 | |
michael@0 | 239 | |
michael@0 | 240 | void vp9_update_rate_correction_factors(VP9_COMP *cpi, int damp_var) { |
michael@0 | 241 | const int q = cpi->common.base_qindex; |
michael@0 | 242 | int correction_factor = 100; |
michael@0 | 243 | double rate_correction_factor; |
michael@0 | 244 | double adjustment_limit; |
michael@0 | 245 | |
michael@0 | 246 | int projected_size_based_on_q = 0; |
michael@0 | 247 | |
michael@0 | 248 | // Clear down mmx registers to allow floating point in what follows |
michael@0 | 249 | vp9_clear_system_state(); // __asm emms; |
michael@0 | 250 | |
michael@0 | 251 | if (cpi->common.frame_type == KEY_FRAME) { |
michael@0 | 252 | rate_correction_factor = cpi->key_frame_rate_correction_factor; |
michael@0 | 253 | } else { |
michael@0 | 254 | if (cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) |
michael@0 | 255 | rate_correction_factor = cpi->gf_rate_correction_factor; |
michael@0 | 256 | else |
michael@0 | 257 | rate_correction_factor = cpi->rate_correction_factor; |
michael@0 | 258 | } |
michael@0 | 259 | |
michael@0 | 260 | // Work out how big we would have expected the frame to be at this Q given |
michael@0 | 261 | // the current correction factor. |
michael@0 | 262 | // Stay in double to avoid int overflow when values are large |
michael@0 | 263 | projected_size_based_on_q = estimate_bits_at_q(cpi->common.frame_type, q, |
michael@0 | 264 | cpi->common.MBs, |
michael@0 | 265 | rate_correction_factor); |
michael@0 | 266 | |
michael@0 | 267 | // Work out a size correction factor. |
michael@0 | 268 | if (projected_size_based_on_q > 0) |
michael@0 | 269 | correction_factor = |
michael@0 | 270 | (100 * cpi->projected_frame_size) / projected_size_based_on_q; |
michael@0 | 271 | |
michael@0 | 272 | // More heavily damped adjustment used if we have been oscillating either side |
michael@0 | 273 | // of target. |
michael@0 | 274 | switch (damp_var) { |
michael@0 | 275 | case 0: |
michael@0 | 276 | adjustment_limit = 0.75; |
michael@0 | 277 | break; |
michael@0 | 278 | case 1: |
michael@0 | 279 | adjustment_limit = 0.375; |
michael@0 | 280 | break; |
michael@0 | 281 | case 2: |
michael@0 | 282 | default: |
michael@0 | 283 | adjustment_limit = 0.25; |
michael@0 | 284 | break; |
michael@0 | 285 | } |
michael@0 | 286 | |
michael@0 | 287 | // if ( (correction_factor > 102) && (Q < cpi->active_worst_quality) ) |
michael@0 | 288 | if (correction_factor > 102) { |
michael@0 | 289 | // We are not already at the worst allowable quality |
michael@0 | 290 | correction_factor = |
michael@0 | 291 | (int)(100 + ((correction_factor - 100) * adjustment_limit)); |
michael@0 | 292 | rate_correction_factor = |
michael@0 | 293 | ((rate_correction_factor * correction_factor) / 100); |
michael@0 | 294 | |
michael@0 | 295 | // Keep rate_correction_factor within limits |
michael@0 | 296 | if (rate_correction_factor > MAX_BPB_FACTOR) |
michael@0 | 297 | rate_correction_factor = MAX_BPB_FACTOR; |
michael@0 | 298 | } else if (correction_factor < 99) { |
michael@0 | 299 | // We are not already at the best allowable quality |
michael@0 | 300 | correction_factor = |
michael@0 | 301 | (int)(100 - ((100 - correction_factor) * adjustment_limit)); |
michael@0 | 302 | rate_correction_factor = |
michael@0 | 303 | ((rate_correction_factor * correction_factor) / 100); |
michael@0 | 304 | |
michael@0 | 305 | // Keep rate_correction_factor within limits |
michael@0 | 306 | if (rate_correction_factor < MIN_BPB_FACTOR) |
michael@0 | 307 | rate_correction_factor = MIN_BPB_FACTOR; |
michael@0 | 308 | } |
michael@0 | 309 | |
michael@0 | 310 | if (cpi->common.frame_type == KEY_FRAME) { |
michael@0 | 311 | cpi->key_frame_rate_correction_factor = rate_correction_factor; |
michael@0 | 312 | } else { |
michael@0 | 313 | if (cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) |
michael@0 | 314 | cpi->gf_rate_correction_factor = rate_correction_factor; |
michael@0 | 315 | else |
michael@0 | 316 | cpi->rate_correction_factor = rate_correction_factor; |
michael@0 | 317 | } |
michael@0 | 318 | } |
michael@0 | 319 | |
michael@0 | 320 | |
michael@0 | 321 | int vp9_regulate_q(VP9_COMP *cpi, int target_bits_per_frame) { |
michael@0 | 322 | int q = cpi->active_worst_quality; |
michael@0 | 323 | |
michael@0 | 324 | int i; |
michael@0 | 325 | int last_error = INT_MAX; |
michael@0 | 326 | int target_bits_per_mb; |
michael@0 | 327 | int bits_per_mb_at_this_q; |
michael@0 | 328 | double correction_factor; |
michael@0 | 329 | |
michael@0 | 330 | // Select the appropriate correction factor based upon type of frame. |
michael@0 | 331 | if (cpi->common.frame_type == KEY_FRAME) { |
michael@0 | 332 | correction_factor = cpi->key_frame_rate_correction_factor; |
michael@0 | 333 | } else { |
michael@0 | 334 | if (cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) |
michael@0 | 335 | correction_factor = cpi->gf_rate_correction_factor; |
michael@0 | 336 | else |
michael@0 | 337 | correction_factor = cpi->rate_correction_factor; |
michael@0 | 338 | } |
michael@0 | 339 | |
michael@0 | 340 | // Calculate required scaling factor based on target frame size and size of |
michael@0 | 341 | // frame produced using previous Q. |
michael@0 | 342 | if (target_bits_per_frame >= (INT_MAX >> BPER_MB_NORMBITS)) |
michael@0 | 343 | target_bits_per_mb = |
michael@0 | 344 | (target_bits_per_frame / cpi->common.MBs) |
michael@0 | 345 | << BPER_MB_NORMBITS; // Case where we would overflow int |
michael@0 | 346 | else |
michael@0 | 347 | target_bits_per_mb = |
michael@0 | 348 | (target_bits_per_frame << BPER_MB_NORMBITS) / cpi->common.MBs; |
michael@0 | 349 | |
michael@0 | 350 | i = cpi->active_best_quality; |
michael@0 | 351 | |
michael@0 | 352 | do { |
michael@0 | 353 | bits_per_mb_at_this_q = (int)vp9_bits_per_mb(cpi->common.frame_type, i, |
michael@0 | 354 | correction_factor); |
michael@0 | 355 | |
michael@0 | 356 | if (bits_per_mb_at_this_q <= target_bits_per_mb) { |
michael@0 | 357 | if ((target_bits_per_mb - bits_per_mb_at_this_q) <= last_error) |
michael@0 | 358 | q = i; |
michael@0 | 359 | else |
michael@0 | 360 | q = i - 1; |
michael@0 | 361 | |
michael@0 | 362 | break; |
michael@0 | 363 | } else { |
michael@0 | 364 | last_error = bits_per_mb_at_this_q - target_bits_per_mb; |
michael@0 | 365 | } |
michael@0 | 366 | } while (++i <= cpi->active_worst_quality); |
michael@0 | 367 | |
michael@0 | 368 | return q; |
michael@0 | 369 | } |
michael@0 | 370 | |
michael@0 | 371 | |
michael@0 | 372 | static int estimate_keyframe_frequency(VP9_COMP *cpi) { |
michael@0 | 373 | int i; |
michael@0 | 374 | |
michael@0 | 375 | // Average key frame frequency |
michael@0 | 376 | int av_key_frame_frequency = 0; |
michael@0 | 377 | |
michael@0 | 378 | /* First key frame at start of sequence is a special case. We have no |
michael@0 | 379 | * frequency data. |
michael@0 | 380 | */ |
michael@0 | 381 | if (cpi->key_frame_count == 1) { |
michael@0 | 382 | /* Assume a default of 1 kf every 2 seconds, or the max kf interval, |
michael@0 | 383 | * whichever is smaller. |
michael@0 | 384 | */ |
michael@0 | 385 | int key_freq = cpi->oxcf.key_freq > 0 ? cpi->oxcf.key_freq : 1; |
michael@0 | 386 | av_key_frame_frequency = (int)cpi->output_framerate * 2; |
michael@0 | 387 | |
michael@0 | 388 | if (cpi->oxcf.auto_key && av_key_frame_frequency > key_freq) |
michael@0 | 389 | av_key_frame_frequency = cpi->oxcf.key_freq; |
michael@0 | 390 | |
michael@0 | 391 | cpi->prior_key_frame_distance[KEY_FRAME_CONTEXT - 1] |
michael@0 | 392 | = av_key_frame_frequency; |
michael@0 | 393 | } else { |
michael@0 | 394 | unsigned int total_weight = 0; |
michael@0 | 395 | int last_kf_interval = |
michael@0 | 396 | (cpi->frames_since_key > 0) ? cpi->frames_since_key : 1; |
michael@0 | 397 | |
michael@0 | 398 | /* reset keyframe context and calculate weighted average of last |
michael@0 | 399 | * KEY_FRAME_CONTEXT keyframes |
michael@0 | 400 | */ |
michael@0 | 401 | for (i = 0; i < KEY_FRAME_CONTEXT; i++) { |
michael@0 | 402 | if (i < KEY_FRAME_CONTEXT - 1) |
michael@0 | 403 | cpi->prior_key_frame_distance[i] |
michael@0 | 404 | = cpi->prior_key_frame_distance[i + 1]; |
michael@0 | 405 | else |
michael@0 | 406 | cpi->prior_key_frame_distance[i] = last_kf_interval; |
michael@0 | 407 | |
michael@0 | 408 | av_key_frame_frequency += prior_key_frame_weight[i] |
michael@0 | 409 | * cpi->prior_key_frame_distance[i]; |
michael@0 | 410 | total_weight += prior_key_frame_weight[i]; |
michael@0 | 411 | } |
michael@0 | 412 | |
michael@0 | 413 | av_key_frame_frequency /= total_weight; |
michael@0 | 414 | } |
michael@0 | 415 | return av_key_frame_frequency; |
michael@0 | 416 | } |
michael@0 | 417 | |
michael@0 | 418 | |
michael@0 | 419 | void vp9_adjust_key_frame_context(VP9_COMP *cpi) { |
michael@0 | 420 | // Clear down mmx registers to allow floating point in what follows |
michael@0 | 421 | vp9_clear_system_state(); |
michael@0 | 422 | |
michael@0 | 423 | cpi->frames_since_key = 0; |
michael@0 | 424 | cpi->key_frame_count++; |
michael@0 | 425 | } |
michael@0 | 426 | |
michael@0 | 427 | |
michael@0 | 428 | void vp9_compute_frame_size_bounds(VP9_COMP *cpi, int *frame_under_shoot_limit, |
michael@0 | 429 | int *frame_over_shoot_limit) { |
michael@0 | 430 | // Set-up bounds on acceptable frame size: |
michael@0 | 431 | if (cpi->oxcf.fixed_q >= 0) { |
michael@0 | 432 | // Fixed Q scenario: frame size never outranges target (there is no target!) |
michael@0 | 433 | *frame_under_shoot_limit = 0; |
michael@0 | 434 | *frame_over_shoot_limit = INT_MAX; |
michael@0 | 435 | } else { |
michael@0 | 436 | if (cpi->common.frame_type == KEY_FRAME) { |
michael@0 | 437 | *frame_over_shoot_limit = cpi->this_frame_target * 9 / 8; |
michael@0 | 438 | *frame_under_shoot_limit = cpi->this_frame_target * 7 / 8; |
michael@0 | 439 | } else { |
michael@0 | 440 | if (cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) { |
michael@0 | 441 | *frame_over_shoot_limit = cpi->this_frame_target * 9 / 8; |
michael@0 | 442 | *frame_under_shoot_limit = cpi->this_frame_target * 7 / 8; |
michael@0 | 443 | } else { |
michael@0 | 444 | // Stron overshoot limit for constrained quality |
michael@0 | 445 | if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) { |
michael@0 | 446 | *frame_over_shoot_limit = cpi->this_frame_target * 11 / 8; |
michael@0 | 447 | *frame_under_shoot_limit = cpi->this_frame_target * 2 / 8; |
michael@0 | 448 | } else { |
michael@0 | 449 | *frame_over_shoot_limit = cpi->this_frame_target * 11 / 8; |
michael@0 | 450 | *frame_under_shoot_limit = cpi->this_frame_target * 5 / 8; |
michael@0 | 451 | } |
michael@0 | 452 | } |
michael@0 | 453 | } |
michael@0 | 454 | |
michael@0 | 455 | // For very small rate targets where the fractional adjustment |
michael@0 | 456 | // (eg * 7/8) may be tiny make sure there is at least a minimum |
michael@0 | 457 | // range. |
michael@0 | 458 | *frame_over_shoot_limit += 200; |
michael@0 | 459 | *frame_under_shoot_limit -= 200; |
michael@0 | 460 | if (*frame_under_shoot_limit < 0) |
michael@0 | 461 | *frame_under_shoot_limit = 0; |
michael@0 | 462 | } |
michael@0 | 463 | } |
michael@0 | 464 | |
michael@0 | 465 | |
michael@0 | 466 | // return of 0 means drop frame |
michael@0 | 467 | int vp9_pick_frame_size(VP9_COMP *cpi) { |
michael@0 | 468 | VP9_COMMON *cm = &cpi->common; |
michael@0 | 469 | |
michael@0 | 470 | if (cm->frame_type == KEY_FRAME) |
michael@0 | 471 | calc_iframe_target_size(cpi); |
michael@0 | 472 | else |
michael@0 | 473 | calc_pframe_target_size(cpi); |
michael@0 | 474 | |
michael@0 | 475 | return 1; |
michael@0 | 476 | } |