11b362b15af34006e6a11974088a46d42b903418eJohann/* 21b362b15af34006e6a11974088a46d42b903418eJohann * Copyright (c) 2010 The WebM project authors. All Rights Reserved. 31b362b15af34006e6a11974088a46d42b903418eJohann * 41b362b15af34006e6a11974088a46d42b903418eJohann * Use of this source code is governed by a BSD-style license 51b362b15af34006e6a11974088a46d42b903418eJohann * that can be found in the LICENSE file in the root of the source 61b362b15af34006e6a11974088a46d42b903418eJohann * tree. An additional intellectual property rights grant can be found 71b362b15af34006e6a11974088a46d42b903418eJohann * in the file PATENTS. All contributing project authors may 81b362b15af34006e6a11974088a46d42b903418eJohann * be found in the AUTHORS file in the root of the source tree. 91b362b15af34006e6a11974088a46d42b903418eJohann */ 101b362b15af34006e6a11974088a46d42b903418eJohann 111b362b15af34006e6a11974088a46d42b903418eJohann#include <math.h> 121b362b15af34006e6a11974088a46d42b903418eJohann#include <limits.h> 131b362b15af34006e6a11974088a46d42b903418eJohann#include <stdio.h> 141b362b15af34006e6a11974088a46d42b903418eJohann 15da49e34c1fb5e99681f4ad99c21d9cfd83eddb96Vignesh Venkatasubramanian#include "./vpx_dsp_rtcd.h" 16ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang#include "./vpx_scale_rtcd.h" 171b362b15af34006e6a11974088a46d42b903418eJohann#include "block.h" 181b362b15af34006e6a11974088a46d42b903418eJohann#include "onyx_int.h" 19da49e34c1fb5e99681f4ad99c21d9cfd83eddb96Vignesh Venkatasubramanian#include "vpx_dsp/variance.h" 201b362b15af34006e6a11974088a46d42b903418eJohann#include "encodeintra.h" 211b362b15af34006e6a11974088a46d42b903418eJohann#include "vp8/common/setupintrarecon.h" 221b362b15af34006e6a11974088a46d42b903418eJohann#include "vp8/common/systemdependent.h" 231b362b15af34006e6a11974088a46d42b903418eJohann#include "mcomp.h" 241b362b15af34006e6a11974088a46d42b903418eJohann#include "firstpass.h" 25ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang#include "vpx_scale/vpx_scale.h" 261b362b15af34006e6a11974088a46d42b903418eJohann#include "encodemb.h" 271b362b15af34006e6a11974088a46d42b903418eJohann#include "vp8/common/extend.h" 281b362b15af34006e6a11974088a46d42b903418eJohann#include "vpx_mem/vpx_mem.h" 291b362b15af34006e6a11974088a46d42b903418eJohann#include "vp8/common/swapyv12buffer.h" 301b362b15af34006e6a11974088a46d42b903418eJohann#include "rdopt.h" 311b362b15af34006e6a11974088a46d42b903418eJohann#include "vp8/common/quant_common.h" 321b362b15af34006e6a11974088a46d42b903418eJohann#include "encodemv.h" 331b362b15af34006e6a11974088a46d42b903418eJohann#include "encodeframe.h" 341b362b15af34006e6a11974088a46d42b903418eJohann 351b362b15af34006e6a11974088a46d42b903418eJohann/* #define OUTPUT_FPF 1 */ 361b362b15af34006e6a11974088a46d42b903418eJohann 371b362b15af34006e6a11974088a46d42b903418eJohannextern void vp8cx_frame_init_quantizer(VP8_COMP *cpi); 381b362b15af34006e6a11974088a46d42b903418eJohann 391b362b15af34006e6a11974088a46d42b903418eJohann#define GFQ_ADJUSTMENT vp8_gf_boost_qadjustment[Q] 401b362b15af34006e6a11974088a46d42b903418eJohannextern int vp8_kf_boost_qadjustment[QINDEX_RANGE]; 411b362b15af34006e6a11974088a46d42b903418eJohann 421b362b15af34006e6a11974088a46d42b903418eJohannextern const int vp8_gf_boost_qadjustment[QINDEX_RANGE]; 431b362b15af34006e6a11974088a46d42b903418eJohann 441b362b15af34006e6a11974088a46d42b903418eJohann#define IIFACTOR 1.5 451b362b15af34006e6a11974088a46d42b903418eJohann#define IIKFACTOR1 1.40 461b362b15af34006e6a11974088a46d42b903418eJohann#define IIKFACTOR2 1.5 471b362b15af34006e6a11974088a46d42b903418eJohann#define RMAX 14.0 481b362b15af34006e6a11974088a46d42b903418eJohann#define GF_RMAX 48.0 491b362b15af34006e6a11974088a46d42b903418eJohann 501b362b15af34006e6a11974088a46d42b903418eJohann#define KF_MB_INTRA_MIN 300 511b362b15af34006e6a11974088a46d42b903418eJohann#define GF_MB_INTRA_MIN 200 521b362b15af34006e6a11974088a46d42b903418eJohann 531b362b15af34006e6a11974088a46d42b903418eJohann#define DOUBLE_DIVIDE_CHECK(X) ((X)<0?(X)-.000001:(X)+.000001) 541b362b15af34006e6a11974088a46d42b903418eJohann 551b362b15af34006e6a11974088a46d42b903418eJohann#define POW1 (double)cpi->oxcf.two_pass_vbrbias/100.0 561b362b15af34006e6a11974088a46d42b903418eJohann#define POW2 (double)cpi->oxcf.two_pass_vbrbias/100.0 571b362b15af34006e6a11974088a46d42b903418eJohann 581b362b15af34006e6a11974088a46d42b903418eJohann#define NEW_BOOST 1 591b362b15af34006e6a11974088a46d42b903418eJohann 601b362b15af34006e6a11974088a46d42b903418eJohannstatic int vscale_lookup[7] = {0, 1, 1, 2, 2, 3, 3}; 611b362b15af34006e6a11974088a46d42b903418eJohannstatic int hscale_lookup[7] = {0, 0, 1, 1, 2, 2, 3}; 621b362b15af34006e6a11974088a46d42b903418eJohann 631b362b15af34006e6a11974088a46d42b903418eJohann 641b362b15af34006e6a11974088a46d42b903418eJohannstatic const int cq_level[QINDEX_RANGE] = 651b362b15af34006e6a11974088a46d42b903418eJohann{ 661b362b15af34006e6a11974088a46d42b903418eJohann 0,0,1,1,2,3,3,4,4,5,6,6,7,8,8,9, 671b362b15af34006e6a11974088a46d42b903418eJohann 9,10,11,11,12,13,13,14,15,15,16,17,17,18,19,20, 681b362b15af34006e6a11974088a46d42b903418eJohann 20,21,22,22,23,24,24,25,26,27,27,28,29,30,30,31, 691b362b15af34006e6a11974088a46d42b903418eJohann 32,33,33,34,35,36,36,37,38,39,39,40,41,42,42,43, 701b362b15af34006e6a11974088a46d42b903418eJohann 44,45,46,46,47,48,49,50,50,51,52,53,54,55,55,56, 711b362b15af34006e6a11974088a46d42b903418eJohann 57,58,59,60,60,61,62,63,64,65,66,67,67,68,69,70, 721b362b15af34006e6a11974088a46d42b903418eJohann 71,72,73,74,75,75,76,77,78,79,80,81,82,83,84,85, 731b362b15af34006e6a11974088a46d42b903418eJohann 86,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100 741b362b15af34006e6a11974088a46d42b903418eJohann}; 751b362b15af34006e6a11974088a46d42b903418eJohann 761b362b15af34006e6a11974088a46d42b903418eJohannstatic void find_next_key_frame(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame); 771b362b15af34006e6a11974088a46d42b903418eJohann 781b362b15af34006e6a11974088a46d42b903418eJohann/* Resets the first pass file to the given position using a relative seek 791b362b15af34006e6a11974088a46d42b903418eJohann * from the current position 801b362b15af34006e6a11974088a46d42b903418eJohann */ 811b362b15af34006e6a11974088a46d42b903418eJohannstatic void reset_fpf_position(VP8_COMP *cpi, FIRSTPASS_STATS *Position) 821b362b15af34006e6a11974088a46d42b903418eJohann{ 831b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.stats_in = Position; 841b362b15af34006e6a11974088a46d42b903418eJohann} 851b362b15af34006e6a11974088a46d42b903418eJohann 861b362b15af34006e6a11974088a46d42b903418eJohannstatic int lookup_next_frame_stats(VP8_COMP *cpi, FIRSTPASS_STATS *next_frame) 871b362b15af34006e6a11974088a46d42b903418eJohann{ 881b362b15af34006e6a11974088a46d42b903418eJohann if (cpi->twopass.stats_in >= cpi->twopass.stats_in_end) 891b362b15af34006e6a11974088a46d42b903418eJohann return EOF; 901b362b15af34006e6a11974088a46d42b903418eJohann 911b362b15af34006e6a11974088a46d42b903418eJohann *next_frame = *cpi->twopass.stats_in; 921b362b15af34006e6a11974088a46d42b903418eJohann return 1; 931b362b15af34006e6a11974088a46d42b903418eJohann} 941b362b15af34006e6a11974088a46d42b903418eJohann 951b362b15af34006e6a11974088a46d42b903418eJohann/* Read frame stats at an offset from the current position */ 961b362b15af34006e6a11974088a46d42b903418eJohannstatic int read_frame_stats( VP8_COMP *cpi, 971b362b15af34006e6a11974088a46d42b903418eJohann FIRSTPASS_STATS *frame_stats, 981b362b15af34006e6a11974088a46d42b903418eJohann int offset ) 991b362b15af34006e6a11974088a46d42b903418eJohann{ 1001b362b15af34006e6a11974088a46d42b903418eJohann FIRSTPASS_STATS * fps_ptr = cpi->twopass.stats_in; 1011b362b15af34006e6a11974088a46d42b903418eJohann 1021b362b15af34006e6a11974088a46d42b903418eJohann /* Check legality of offset */ 1031b362b15af34006e6a11974088a46d42b903418eJohann if ( offset >= 0 ) 1041b362b15af34006e6a11974088a46d42b903418eJohann { 1051b362b15af34006e6a11974088a46d42b903418eJohann if ( &fps_ptr[offset] >= cpi->twopass.stats_in_end ) 1061b362b15af34006e6a11974088a46d42b903418eJohann return EOF; 1071b362b15af34006e6a11974088a46d42b903418eJohann } 1081b362b15af34006e6a11974088a46d42b903418eJohann else if ( offset < 0 ) 1091b362b15af34006e6a11974088a46d42b903418eJohann { 1101b362b15af34006e6a11974088a46d42b903418eJohann if ( &fps_ptr[offset] < cpi->twopass.stats_in_start ) 1111b362b15af34006e6a11974088a46d42b903418eJohann return EOF; 1121b362b15af34006e6a11974088a46d42b903418eJohann } 1131b362b15af34006e6a11974088a46d42b903418eJohann 1141b362b15af34006e6a11974088a46d42b903418eJohann *frame_stats = fps_ptr[offset]; 1151b362b15af34006e6a11974088a46d42b903418eJohann return 1; 1161b362b15af34006e6a11974088a46d42b903418eJohann} 1171b362b15af34006e6a11974088a46d42b903418eJohann 1181b362b15af34006e6a11974088a46d42b903418eJohannstatic int input_stats(VP8_COMP *cpi, FIRSTPASS_STATS *fps) 1191b362b15af34006e6a11974088a46d42b903418eJohann{ 1201b362b15af34006e6a11974088a46d42b903418eJohann if (cpi->twopass.stats_in >= cpi->twopass.stats_in_end) 1211b362b15af34006e6a11974088a46d42b903418eJohann return EOF; 1221b362b15af34006e6a11974088a46d42b903418eJohann 1231b362b15af34006e6a11974088a46d42b903418eJohann *fps = *cpi->twopass.stats_in; 1241b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.stats_in = 1251b362b15af34006e6a11974088a46d42b903418eJohann (void*)((char *)cpi->twopass.stats_in + sizeof(FIRSTPASS_STATS)); 1261b362b15af34006e6a11974088a46d42b903418eJohann return 1; 1271b362b15af34006e6a11974088a46d42b903418eJohann} 1281b362b15af34006e6a11974088a46d42b903418eJohann 1291b362b15af34006e6a11974088a46d42b903418eJohannstatic void output_stats(const VP8_COMP *cpi, 1301b362b15af34006e6a11974088a46d42b903418eJohann struct vpx_codec_pkt_list *pktlist, 1311b362b15af34006e6a11974088a46d42b903418eJohann FIRSTPASS_STATS *stats) 1321b362b15af34006e6a11974088a46d42b903418eJohann{ 1331b362b15af34006e6a11974088a46d42b903418eJohann struct vpx_codec_cx_pkt pkt; 134da49e34c1fb5e99681f4ad99c21d9cfd83eddb96Vignesh Venkatasubramanian (void)cpi; 1351b362b15af34006e6a11974088a46d42b903418eJohann pkt.kind = VPX_CODEC_STATS_PKT; 1361b362b15af34006e6a11974088a46d42b903418eJohann pkt.data.twopass_stats.buf = stats; 1371b362b15af34006e6a11974088a46d42b903418eJohann pkt.data.twopass_stats.sz = sizeof(FIRSTPASS_STATS); 1381b362b15af34006e6a11974088a46d42b903418eJohann vpx_codec_pkt_list_add(pktlist, &pkt); 1391b362b15af34006e6a11974088a46d42b903418eJohann 1401b362b15af34006e6a11974088a46d42b903418eJohann/* TEMP debug code */ 1411b362b15af34006e6a11974088a46d42b903418eJohann#if OUTPUT_FPF 1421b362b15af34006e6a11974088a46d42b903418eJohann 1431b362b15af34006e6a11974088a46d42b903418eJohann { 1441b362b15af34006e6a11974088a46d42b903418eJohann FILE *fpfile; 1451b362b15af34006e6a11974088a46d42b903418eJohann fpfile = fopen("firstpass.stt", "a"); 1461b362b15af34006e6a11974088a46d42b903418eJohann 1471b362b15af34006e6a11974088a46d42b903418eJohann fprintf(fpfile, "%12.0f %12.0f %12.0f %12.4f %12.4f %12.4f %12.4f" 1481b362b15af34006e6a11974088a46d42b903418eJohann " %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f" 1491b362b15af34006e6a11974088a46d42b903418eJohann " %12.0f %12.0f %12.4f\n", 1501b362b15af34006e6a11974088a46d42b903418eJohann stats->frame, 1511b362b15af34006e6a11974088a46d42b903418eJohann stats->intra_error, 1521b362b15af34006e6a11974088a46d42b903418eJohann stats->coded_error, 1531b362b15af34006e6a11974088a46d42b903418eJohann stats->ssim_weighted_pred_err, 1541b362b15af34006e6a11974088a46d42b903418eJohann stats->pcnt_inter, 1551b362b15af34006e6a11974088a46d42b903418eJohann stats->pcnt_motion, 1561b362b15af34006e6a11974088a46d42b903418eJohann stats->pcnt_second_ref, 1571b362b15af34006e6a11974088a46d42b903418eJohann stats->pcnt_neutral, 1581b362b15af34006e6a11974088a46d42b903418eJohann stats->MVr, 1591b362b15af34006e6a11974088a46d42b903418eJohann stats->mvr_abs, 1601b362b15af34006e6a11974088a46d42b903418eJohann stats->MVc, 1611b362b15af34006e6a11974088a46d42b903418eJohann stats->mvc_abs, 1621b362b15af34006e6a11974088a46d42b903418eJohann stats->MVrv, 1631b362b15af34006e6a11974088a46d42b903418eJohann stats->MVcv, 1641b362b15af34006e6a11974088a46d42b903418eJohann stats->mv_in_out_count, 1651b362b15af34006e6a11974088a46d42b903418eJohann stats->new_mv_count, 1661b362b15af34006e6a11974088a46d42b903418eJohann stats->count, 1671b362b15af34006e6a11974088a46d42b903418eJohann stats->duration); 1681b362b15af34006e6a11974088a46d42b903418eJohann fclose(fpfile); 1691b362b15af34006e6a11974088a46d42b903418eJohann } 1701b362b15af34006e6a11974088a46d42b903418eJohann#endif 1711b362b15af34006e6a11974088a46d42b903418eJohann} 1721b362b15af34006e6a11974088a46d42b903418eJohann 1731b362b15af34006e6a11974088a46d42b903418eJohannstatic void zero_stats(FIRSTPASS_STATS *section) 1741b362b15af34006e6a11974088a46d42b903418eJohann{ 1751b362b15af34006e6a11974088a46d42b903418eJohann section->frame = 0.0; 1761b362b15af34006e6a11974088a46d42b903418eJohann section->intra_error = 0.0; 1771b362b15af34006e6a11974088a46d42b903418eJohann section->coded_error = 0.0; 1781b362b15af34006e6a11974088a46d42b903418eJohann section->ssim_weighted_pred_err = 0.0; 1791b362b15af34006e6a11974088a46d42b903418eJohann section->pcnt_inter = 0.0; 1801b362b15af34006e6a11974088a46d42b903418eJohann section->pcnt_motion = 0.0; 1811b362b15af34006e6a11974088a46d42b903418eJohann section->pcnt_second_ref = 0.0; 1821b362b15af34006e6a11974088a46d42b903418eJohann section->pcnt_neutral = 0.0; 1831b362b15af34006e6a11974088a46d42b903418eJohann section->MVr = 0.0; 1841b362b15af34006e6a11974088a46d42b903418eJohann section->mvr_abs = 0.0; 1851b362b15af34006e6a11974088a46d42b903418eJohann section->MVc = 0.0; 1861b362b15af34006e6a11974088a46d42b903418eJohann section->mvc_abs = 0.0; 1871b362b15af34006e6a11974088a46d42b903418eJohann section->MVrv = 0.0; 1881b362b15af34006e6a11974088a46d42b903418eJohann section->MVcv = 0.0; 1891b362b15af34006e6a11974088a46d42b903418eJohann section->mv_in_out_count = 0.0; 1901b362b15af34006e6a11974088a46d42b903418eJohann section->new_mv_count = 0.0; 1911b362b15af34006e6a11974088a46d42b903418eJohann section->count = 0.0; 1921b362b15af34006e6a11974088a46d42b903418eJohann section->duration = 1.0; 1931b362b15af34006e6a11974088a46d42b903418eJohann} 1941b362b15af34006e6a11974088a46d42b903418eJohann 1951b362b15af34006e6a11974088a46d42b903418eJohannstatic void accumulate_stats(FIRSTPASS_STATS *section, FIRSTPASS_STATS *frame) 1961b362b15af34006e6a11974088a46d42b903418eJohann{ 1971b362b15af34006e6a11974088a46d42b903418eJohann section->frame += frame->frame; 1981b362b15af34006e6a11974088a46d42b903418eJohann section->intra_error += frame->intra_error; 1991b362b15af34006e6a11974088a46d42b903418eJohann section->coded_error += frame->coded_error; 2001b362b15af34006e6a11974088a46d42b903418eJohann section->ssim_weighted_pred_err += frame->ssim_weighted_pred_err; 2011b362b15af34006e6a11974088a46d42b903418eJohann section->pcnt_inter += frame->pcnt_inter; 2021b362b15af34006e6a11974088a46d42b903418eJohann section->pcnt_motion += frame->pcnt_motion; 2031b362b15af34006e6a11974088a46d42b903418eJohann section->pcnt_second_ref += frame->pcnt_second_ref; 2041b362b15af34006e6a11974088a46d42b903418eJohann section->pcnt_neutral += frame->pcnt_neutral; 2051b362b15af34006e6a11974088a46d42b903418eJohann section->MVr += frame->MVr; 2061b362b15af34006e6a11974088a46d42b903418eJohann section->mvr_abs += frame->mvr_abs; 2071b362b15af34006e6a11974088a46d42b903418eJohann section->MVc += frame->MVc; 2081b362b15af34006e6a11974088a46d42b903418eJohann section->mvc_abs += frame->mvc_abs; 2091b362b15af34006e6a11974088a46d42b903418eJohann section->MVrv += frame->MVrv; 2101b362b15af34006e6a11974088a46d42b903418eJohann section->MVcv += frame->MVcv; 2111b362b15af34006e6a11974088a46d42b903418eJohann section->mv_in_out_count += frame->mv_in_out_count; 2121b362b15af34006e6a11974088a46d42b903418eJohann section->new_mv_count += frame->new_mv_count; 2131b362b15af34006e6a11974088a46d42b903418eJohann section->count += frame->count; 2141b362b15af34006e6a11974088a46d42b903418eJohann section->duration += frame->duration; 2151b362b15af34006e6a11974088a46d42b903418eJohann} 2161b362b15af34006e6a11974088a46d42b903418eJohann 2171b362b15af34006e6a11974088a46d42b903418eJohannstatic void subtract_stats(FIRSTPASS_STATS *section, FIRSTPASS_STATS *frame) 2181b362b15af34006e6a11974088a46d42b903418eJohann{ 2191b362b15af34006e6a11974088a46d42b903418eJohann section->frame -= frame->frame; 2201b362b15af34006e6a11974088a46d42b903418eJohann section->intra_error -= frame->intra_error; 2211b362b15af34006e6a11974088a46d42b903418eJohann section->coded_error -= frame->coded_error; 2221b362b15af34006e6a11974088a46d42b903418eJohann section->ssim_weighted_pred_err -= frame->ssim_weighted_pred_err; 2231b362b15af34006e6a11974088a46d42b903418eJohann section->pcnt_inter -= frame->pcnt_inter; 2241b362b15af34006e6a11974088a46d42b903418eJohann section->pcnt_motion -= frame->pcnt_motion; 2251b362b15af34006e6a11974088a46d42b903418eJohann section->pcnt_second_ref -= frame->pcnt_second_ref; 2261b362b15af34006e6a11974088a46d42b903418eJohann section->pcnt_neutral -= frame->pcnt_neutral; 2271b362b15af34006e6a11974088a46d42b903418eJohann section->MVr -= frame->MVr; 2281b362b15af34006e6a11974088a46d42b903418eJohann section->mvr_abs -= frame->mvr_abs; 2291b362b15af34006e6a11974088a46d42b903418eJohann section->MVc -= frame->MVc; 2301b362b15af34006e6a11974088a46d42b903418eJohann section->mvc_abs -= frame->mvc_abs; 2311b362b15af34006e6a11974088a46d42b903418eJohann section->MVrv -= frame->MVrv; 2321b362b15af34006e6a11974088a46d42b903418eJohann section->MVcv -= frame->MVcv; 2331b362b15af34006e6a11974088a46d42b903418eJohann section->mv_in_out_count -= frame->mv_in_out_count; 2341b362b15af34006e6a11974088a46d42b903418eJohann section->new_mv_count -= frame->new_mv_count; 2351b362b15af34006e6a11974088a46d42b903418eJohann section->count -= frame->count; 2361b362b15af34006e6a11974088a46d42b903418eJohann section->duration -= frame->duration; 2371b362b15af34006e6a11974088a46d42b903418eJohann} 2381b362b15af34006e6a11974088a46d42b903418eJohann 2391b362b15af34006e6a11974088a46d42b903418eJohannstatic void avg_stats(FIRSTPASS_STATS *section) 2401b362b15af34006e6a11974088a46d42b903418eJohann{ 2411b362b15af34006e6a11974088a46d42b903418eJohann if (section->count < 1.0) 2421b362b15af34006e6a11974088a46d42b903418eJohann return; 2431b362b15af34006e6a11974088a46d42b903418eJohann 2441b362b15af34006e6a11974088a46d42b903418eJohann section->intra_error /= section->count; 2451b362b15af34006e6a11974088a46d42b903418eJohann section->coded_error /= section->count; 2461b362b15af34006e6a11974088a46d42b903418eJohann section->ssim_weighted_pred_err /= section->count; 2471b362b15af34006e6a11974088a46d42b903418eJohann section->pcnt_inter /= section->count; 2481b362b15af34006e6a11974088a46d42b903418eJohann section->pcnt_second_ref /= section->count; 2491b362b15af34006e6a11974088a46d42b903418eJohann section->pcnt_neutral /= section->count; 2501b362b15af34006e6a11974088a46d42b903418eJohann section->pcnt_motion /= section->count; 2511b362b15af34006e6a11974088a46d42b903418eJohann section->MVr /= section->count; 2521b362b15af34006e6a11974088a46d42b903418eJohann section->mvr_abs /= section->count; 2531b362b15af34006e6a11974088a46d42b903418eJohann section->MVc /= section->count; 2541b362b15af34006e6a11974088a46d42b903418eJohann section->mvc_abs /= section->count; 2551b362b15af34006e6a11974088a46d42b903418eJohann section->MVrv /= section->count; 2561b362b15af34006e6a11974088a46d42b903418eJohann section->MVcv /= section->count; 2571b362b15af34006e6a11974088a46d42b903418eJohann section->mv_in_out_count /= section->count; 2581b362b15af34006e6a11974088a46d42b903418eJohann section->duration /= section->count; 2591b362b15af34006e6a11974088a46d42b903418eJohann} 2601b362b15af34006e6a11974088a46d42b903418eJohann 2611b362b15af34006e6a11974088a46d42b903418eJohann/* Calculate a modified Error used in distributing bits between easier 2621b362b15af34006e6a11974088a46d42b903418eJohann * and harder frames 2631b362b15af34006e6a11974088a46d42b903418eJohann */ 2641b362b15af34006e6a11974088a46d42b903418eJohannstatic double calculate_modified_err(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame) 2651b362b15af34006e6a11974088a46d42b903418eJohann{ 2661b362b15af34006e6a11974088a46d42b903418eJohann double av_err = ( cpi->twopass.total_stats.ssim_weighted_pred_err / 2671b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.total_stats.count ); 2681b362b15af34006e6a11974088a46d42b903418eJohann double this_err = this_frame->ssim_weighted_pred_err; 2691b362b15af34006e6a11974088a46d42b903418eJohann double modified_err; 2701b362b15af34006e6a11974088a46d42b903418eJohann 2711b362b15af34006e6a11974088a46d42b903418eJohann if (this_err > av_err) 2721b362b15af34006e6a11974088a46d42b903418eJohann modified_err = av_err * pow((this_err / DOUBLE_DIVIDE_CHECK(av_err)), POW1); 2731b362b15af34006e6a11974088a46d42b903418eJohann else 2741b362b15af34006e6a11974088a46d42b903418eJohann modified_err = av_err * pow((this_err / DOUBLE_DIVIDE_CHECK(av_err)), POW2); 2751b362b15af34006e6a11974088a46d42b903418eJohann 2761b362b15af34006e6a11974088a46d42b903418eJohann return modified_err; 2771b362b15af34006e6a11974088a46d42b903418eJohann} 2781b362b15af34006e6a11974088a46d42b903418eJohann 2791b362b15af34006e6a11974088a46d42b903418eJohannstatic const double weight_table[256] = { 2801b362b15af34006e6a11974088a46d42b903418eJohann0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 2811b362b15af34006e6a11974088a46d42b903418eJohann0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 2821b362b15af34006e6a11974088a46d42b903418eJohann0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 2831b362b15af34006e6a11974088a46d42b903418eJohann0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 2841b362b15af34006e6a11974088a46d42b903418eJohann0.020000, 0.031250, 0.062500, 0.093750, 0.125000, 0.156250, 0.187500, 0.218750, 2851b362b15af34006e6a11974088a46d42b903418eJohann0.250000, 0.281250, 0.312500, 0.343750, 0.375000, 0.406250, 0.437500, 0.468750, 2861b362b15af34006e6a11974088a46d42b903418eJohann0.500000, 0.531250, 0.562500, 0.593750, 0.625000, 0.656250, 0.687500, 0.718750, 2871b362b15af34006e6a11974088a46d42b903418eJohann0.750000, 0.781250, 0.812500, 0.843750, 0.875000, 0.906250, 0.937500, 0.968750, 2881b362b15af34006e6a11974088a46d42b903418eJohann1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 2891b362b15af34006e6a11974088a46d42b903418eJohann1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 2901b362b15af34006e6a11974088a46d42b903418eJohann1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 2911b362b15af34006e6a11974088a46d42b903418eJohann1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 2921b362b15af34006e6a11974088a46d42b903418eJohann1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 2931b362b15af34006e6a11974088a46d42b903418eJohann1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 2941b362b15af34006e6a11974088a46d42b903418eJohann1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 2951b362b15af34006e6a11974088a46d42b903418eJohann1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 2961b362b15af34006e6a11974088a46d42b903418eJohann1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 2971b362b15af34006e6a11974088a46d42b903418eJohann1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 2981b362b15af34006e6a11974088a46d42b903418eJohann1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 2991b362b15af34006e6a11974088a46d42b903418eJohann1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 3001b362b15af34006e6a11974088a46d42b903418eJohann1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 3011b362b15af34006e6a11974088a46d42b903418eJohann1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 3021b362b15af34006e6a11974088a46d42b903418eJohann1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 3031b362b15af34006e6a11974088a46d42b903418eJohann1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 3041b362b15af34006e6a11974088a46d42b903418eJohann1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 3051b362b15af34006e6a11974088a46d42b903418eJohann1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 3061b362b15af34006e6a11974088a46d42b903418eJohann1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 3071b362b15af34006e6a11974088a46d42b903418eJohann1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 3081b362b15af34006e6a11974088a46d42b903418eJohann1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 3091b362b15af34006e6a11974088a46d42b903418eJohann1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 3101b362b15af34006e6a11974088a46d42b903418eJohann1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 3111b362b15af34006e6a11974088a46d42b903418eJohann1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000 3121b362b15af34006e6a11974088a46d42b903418eJohann}; 3131b362b15af34006e6a11974088a46d42b903418eJohann 3141b362b15af34006e6a11974088a46d42b903418eJohannstatic double simple_weight(YV12_BUFFER_CONFIG *source) 3151b362b15af34006e6a11974088a46d42b903418eJohann{ 3161b362b15af34006e6a11974088a46d42b903418eJohann int i, j; 3171b362b15af34006e6a11974088a46d42b903418eJohann 3181b362b15af34006e6a11974088a46d42b903418eJohann unsigned char *src = source->y_buffer; 3191b362b15af34006e6a11974088a46d42b903418eJohann double sum_weights = 0.0; 3201b362b15af34006e6a11974088a46d42b903418eJohann 3211b362b15af34006e6a11974088a46d42b903418eJohann /* Loop throught the Y plane raw examining levels and creating a weight 3221b362b15af34006e6a11974088a46d42b903418eJohann * for the image 3231b362b15af34006e6a11974088a46d42b903418eJohann */ 3241b362b15af34006e6a11974088a46d42b903418eJohann i = source->y_height; 3251b362b15af34006e6a11974088a46d42b903418eJohann do 3261b362b15af34006e6a11974088a46d42b903418eJohann { 3271b362b15af34006e6a11974088a46d42b903418eJohann j = source->y_width; 3281b362b15af34006e6a11974088a46d42b903418eJohann do 3291b362b15af34006e6a11974088a46d42b903418eJohann { 3301b362b15af34006e6a11974088a46d42b903418eJohann sum_weights += weight_table[ *src]; 3311b362b15af34006e6a11974088a46d42b903418eJohann src++; 3321b362b15af34006e6a11974088a46d42b903418eJohann }while(--j); 3331b362b15af34006e6a11974088a46d42b903418eJohann src -= source->y_width; 3341b362b15af34006e6a11974088a46d42b903418eJohann src += source->y_stride; 3351b362b15af34006e6a11974088a46d42b903418eJohann }while(--i); 3361b362b15af34006e6a11974088a46d42b903418eJohann 3371b362b15af34006e6a11974088a46d42b903418eJohann sum_weights /= (source->y_height * source->y_width); 3381b362b15af34006e6a11974088a46d42b903418eJohann 3391b362b15af34006e6a11974088a46d42b903418eJohann return sum_weights; 3401b362b15af34006e6a11974088a46d42b903418eJohann} 3411b362b15af34006e6a11974088a46d42b903418eJohann 3421b362b15af34006e6a11974088a46d42b903418eJohann 3431b362b15af34006e6a11974088a46d42b903418eJohann/* This function returns the current per frame maximum bitrate target */ 3441b362b15af34006e6a11974088a46d42b903418eJohannstatic int frame_max_bits(VP8_COMP *cpi) 3451b362b15af34006e6a11974088a46d42b903418eJohann{ 3461b362b15af34006e6a11974088a46d42b903418eJohann /* Max allocation for a single frame based on the max section guidelines 3471b362b15af34006e6a11974088a46d42b903418eJohann * passed in and how many bits are left 3481b362b15af34006e6a11974088a46d42b903418eJohann */ 3491b362b15af34006e6a11974088a46d42b903418eJohann int max_bits; 3501b362b15af34006e6a11974088a46d42b903418eJohann 3511b362b15af34006e6a11974088a46d42b903418eJohann /* For CBR we need to also consider buffer fullness. 3521b362b15af34006e6a11974088a46d42b903418eJohann * If we are running below the optimal level then we need to gradually 3531b362b15af34006e6a11974088a46d42b903418eJohann * tighten up on max_bits. 3541b362b15af34006e6a11974088a46d42b903418eJohann */ 3551b362b15af34006e6a11974088a46d42b903418eJohann if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) 3561b362b15af34006e6a11974088a46d42b903418eJohann { 3571b362b15af34006e6a11974088a46d42b903418eJohann double buffer_fullness_ratio = (double)cpi->buffer_level / DOUBLE_DIVIDE_CHECK((double)cpi->oxcf.optimal_buffer_level); 3581b362b15af34006e6a11974088a46d42b903418eJohann 3591b362b15af34006e6a11974088a46d42b903418eJohann /* For CBR base this on the target average bits per frame plus the 3601b362b15af34006e6a11974088a46d42b903418eJohann * maximum sedction rate passed in by the user 3611b362b15af34006e6a11974088a46d42b903418eJohann */ 3621b362b15af34006e6a11974088a46d42b903418eJohann max_bits = (int)(cpi->av_per_frame_bandwidth * ((double)cpi->oxcf.two_pass_vbrmax_section / 100.0)); 3631b362b15af34006e6a11974088a46d42b903418eJohann 3641b362b15af34006e6a11974088a46d42b903418eJohann /* If our buffer is below the optimum level */ 3651b362b15af34006e6a11974088a46d42b903418eJohann if (buffer_fullness_ratio < 1.0) 3661b362b15af34006e6a11974088a46d42b903418eJohann { 3671b362b15af34006e6a11974088a46d42b903418eJohann /* The lower of max_bits / 4 or cpi->av_per_frame_bandwidth / 4. */ 3681b362b15af34006e6a11974088a46d42b903418eJohann int min_max_bits = ((cpi->av_per_frame_bandwidth >> 2) < (max_bits >> 2)) ? cpi->av_per_frame_bandwidth >> 2 : max_bits >> 2; 3691b362b15af34006e6a11974088a46d42b903418eJohann 3701b362b15af34006e6a11974088a46d42b903418eJohann max_bits = (int)(max_bits * buffer_fullness_ratio); 3711b362b15af34006e6a11974088a46d42b903418eJohann 3721b362b15af34006e6a11974088a46d42b903418eJohann /* Lowest value we will set ... which should allow the buffer to 3731b362b15af34006e6a11974088a46d42b903418eJohann * refill. 3741b362b15af34006e6a11974088a46d42b903418eJohann */ 3751b362b15af34006e6a11974088a46d42b903418eJohann if (max_bits < min_max_bits) 3761b362b15af34006e6a11974088a46d42b903418eJohann max_bits = min_max_bits; 3771b362b15af34006e6a11974088a46d42b903418eJohann } 3781b362b15af34006e6a11974088a46d42b903418eJohann } 3791b362b15af34006e6a11974088a46d42b903418eJohann /* VBR */ 3801b362b15af34006e6a11974088a46d42b903418eJohann else 3811b362b15af34006e6a11974088a46d42b903418eJohann { 3821b362b15af34006e6a11974088a46d42b903418eJohann /* For VBR base this on the bits and frames left plus the 3831b362b15af34006e6a11974088a46d42b903418eJohann * two_pass_vbrmax_section rate passed in by the user 3841b362b15af34006e6a11974088a46d42b903418eJohann */ 3851b362b15af34006e6a11974088a46d42b903418eJohann max_bits = (int)(((double)cpi->twopass.bits_left / (cpi->twopass.total_stats.count - (double)cpi->common.current_video_frame)) * ((double)cpi->oxcf.two_pass_vbrmax_section / 100.0)); 3861b362b15af34006e6a11974088a46d42b903418eJohann } 3871b362b15af34006e6a11974088a46d42b903418eJohann 3881b362b15af34006e6a11974088a46d42b903418eJohann /* Trap case where we are out of bits */ 3891b362b15af34006e6a11974088a46d42b903418eJohann if (max_bits < 0) 3901b362b15af34006e6a11974088a46d42b903418eJohann max_bits = 0; 3911b362b15af34006e6a11974088a46d42b903418eJohann 3921b362b15af34006e6a11974088a46d42b903418eJohann return max_bits; 3931b362b15af34006e6a11974088a46d42b903418eJohann} 3941b362b15af34006e6a11974088a46d42b903418eJohann 3951b362b15af34006e6a11974088a46d42b903418eJohannvoid vp8_init_first_pass(VP8_COMP *cpi) 3961b362b15af34006e6a11974088a46d42b903418eJohann{ 3971b362b15af34006e6a11974088a46d42b903418eJohann zero_stats(&cpi->twopass.total_stats); 3981b362b15af34006e6a11974088a46d42b903418eJohann} 3991b362b15af34006e6a11974088a46d42b903418eJohann 4001b362b15af34006e6a11974088a46d42b903418eJohannvoid vp8_end_first_pass(VP8_COMP *cpi) 4011b362b15af34006e6a11974088a46d42b903418eJohann{ 4021b362b15af34006e6a11974088a46d42b903418eJohann output_stats(cpi, cpi->output_pkt_list, &cpi->twopass.total_stats); 4031b362b15af34006e6a11974088a46d42b903418eJohann} 4041b362b15af34006e6a11974088a46d42b903418eJohann 4051b362b15af34006e6a11974088a46d42b903418eJohannstatic void zz_motion_search( VP8_COMP *cpi, MACROBLOCK * x, 4061b362b15af34006e6a11974088a46d42b903418eJohann YV12_BUFFER_CONFIG * raw_buffer, 4071b362b15af34006e6a11974088a46d42b903418eJohann int * raw_motion_err, 4081b362b15af34006e6a11974088a46d42b903418eJohann YV12_BUFFER_CONFIG * recon_buffer, 4091b362b15af34006e6a11974088a46d42b903418eJohann int * best_motion_err, int recon_yoffset) 4101b362b15af34006e6a11974088a46d42b903418eJohann{ 4111b362b15af34006e6a11974088a46d42b903418eJohann MACROBLOCKD * const xd = & x->e_mbd; 4121b362b15af34006e6a11974088a46d42b903418eJohann BLOCK *b = &x->block[0]; 4131b362b15af34006e6a11974088a46d42b903418eJohann BLOCKD *d = &x->e_mbd.block[0]; 4141b362b15af34006e6a11974088a46d42b903418eJohann 4151b362b15af34006e6a11974088a46d42b903418eJohann unsigned char *src_ptr = (*(b->base_src) + b->src); 4161b362b15af34006e6a11974088a46d42b903418eJohann int src_stride = b->src_stride; 4171b362b15af34006e6a11974088a46d42b903418eJohann unsigned char *raw_ptr; 4181b362b15af34006e6a11974088a46d42b903418eJohann int raw_stride = raw_buffer->y_stride; 4191b362b15af34006e6a11974088a46d42b903418eJohann unsigned char *ref_ptr; 4201b362b15af34006e6a11974088a46d42b903418eJohann int ref_stride = x->e_mbd.pre.y_stride; 421da49e34c1fb5e99681f4ad99c21d9cfd83eddb96Vignesh Venkatasubramanian (void)cpi; 4221b362b15af34006e6a11974088a46d42b903418eJohann 4231b362b15af34006e6a11974088a46d42b903418eJohann /* Set up pointers for this macro block raw buffer */ 4241b362b15af34006e6a11974088a46d42b903418eJohann raw_ptr = (unsigned char *)(raw_buffer->y_buffer + recon_yoffset 4251b362b15af34006e6a11974088a46d42b903418eJohann + d->offset); 426da49e34c1fb5e99681f4ad99c21d9cfd83eddb96Vignesh Venkatasubramanian vpx_mse16x16(src_ptr, src_stride, raw_ptr, raw_stride, 427da49e34c1fb5e99681f4ad99c21d9cfd83eddb96Vignesh Venkatasubramanian (unsigned int *)(raw_motion_err)); 4281b362b15af34006e6a11974088a46d42b903418eJohann 4291b362b15af34006e6a11974088a46d42b903418eJohann /* Set up pointers for this macro block recon buffer */ 4301b362b15af34006e6a11974088a46d42b903418eJohann xd->pre.y_buffer = recon_buffer->y_buffer + recon_yoffset; 4311b362b15af34006e6a11974088a46d42b903418eJohann ref_ptr = (unsigned char *)(xd->pre.y_buffer + d->offset ); 432da49e34c1fb5e99681f4ad99c21d9cfd83eddb96Vignesh Venkatasubramanian vpx_mse16x16(src_ptr, src_stride, ref_ptr, ref_stride, 433da49e34c1fb5e99681f4ad99c21d9cfd83eddb96Vignesh Venkatasubramanian (unsigned int *)(best_motion_err)); 4341b362b15af34006e6a11974088a46d42b903418eJohann} 4351b362b15af34006e6a11974088a46d42b903418eJohann 4361b362b15af34006e6a11974088a46d42b903418eJohannstatic void first_pass_motion_search(VP8_COMP *cpi, MACROBLOCK *x, 4371b362b15af34006e6a11974088a46d42b903418eJohann int_mv *ref_mv, MV *best_mv, 4381b362b15af34006e6a11974088a46d42b903418eJohann YV12_BUFFER_CONFIG *recon_buffer, 4391b362b15af34006e6a11974088a46d42b903418eJohann int *best_motion_err, int recon_yoffset ) 4401b362b15af34006e6a11974088a46d42b903418eJohann{ 4411b362b15af34006e6a11974088a46d42b903418eJohann MACROBLOCKD *const xd = & x->e_mbd; 4421b362b15af34006e6a11974088a46d42b903418eJohann BLOCK *b = &x->block[0]; 4431b362b15af34006e6a11974088a46d42b903418eJohann BLOCKD *d = &x->e_mbd.block[0]; 4441b362b15af34006e6a11974088a46d42b903418eJohann int num00; 4451b362b15af34006e6a11974088a46d42b903418eJohann 4461b362b15af34006e6a11974088a46d42b903418eJohann int_mv tmp_mv; 4471b362b15af34006e6a11974088a46d42b903418eJohann int_mv ref_mv_full; 4481b362b15af34006e6a11974088a46d42b903418eJohann 4491b362b15af34006e6a11974088a46d42b903418eJohann int tmp_err; 4501b362b15af34006e6a11974088a46d42b903418eJohann int step_param = 3; /* Dont search over full range for first pass */ 4511b362b15af34006e6a11974088a46d42b903418eJohann int further_steps = (MAX_MVSEARCH_STEPS - 1) - step_param; 4521b362b15af34006e6a11974088a46d42b903418eJohann int n; 4531b362b15af34006e6a11974088a46d42b903418eJohann vp8_variance_fn_ptr_t v_fn_ptr = cpi->fn_ptr[BLOCK_16X16]; 4541b362b15af34006e6a11974088a46d42b903418eJohann int new_mv_mode_penalty = 256; 4551b362b15af34006e6a11974088a46d42b903418eJohann 4561b362b15af34006e6a11974088a46d42b903418eJohann /* override the default variance function to use MSE */ 457da49e34c1fb5e99681f4ad99c21d9cfd83eddb96Vignesh Venkatasubramanian v_fn_ptr.vf = vpx_mse16x16; 4581b362b15af34006e6a11974088a46d42b903418eJohann 4591b362b15af34006e6a11974088a46d42b903418eJohann /* Set up pointers for this macro block recon buffer */ 4601b362b15af34006e6a11974088a46d42b903418eJohann xd->pre.y_buffer = recon_buffer->y_buffer + recon_yoffset; 4611b362b15af34006e6a11974088a46d42b903418eJohann 4621b362b15af34006e6a11974088a46d42b903418eJohann /* Initial step/diamond search centred on best mv */ 4631b362b15af34006e6a11974088a46d42b903418eJohann tmp_mv.as_int = 0; 4641b362b15af34006e6a11974088a46d42b903418eJohann ref_mv_full.as_mv.col = ref_mv->as_mv.col>>3; 4651b362b15af34006e6a11974088a46d42b903418eJohann ref_mv_full.as_mv.row = ref_mv->as_mv.row>>3; 4661b362b15af34006e6a11974088a46d42b903418eJohann tmp_err = cpi->diamond_search_sad(x, b, d, &ref_mv_full, &tmp_mv, step_param, 4671b362b15af34006e6a11974088a46d42b903418eJohann x->sadperbit16, &num00, &v_fn_ptr, 4681b362b15af34006e6a11974088a46d42b903418eJohann x->mvcost, ref_mv); 4691b362b15af34006e6a11974088a46d42b903418eJohann if ( tmp_err < INT_MAX-new_mv_mode_penalty ) 4701b362b15af34006e6a11974088a46d42b903418eJohann tmp_err += new_mv_mode_penalty; 4711b362b15af34006e6a11974088a46d42b903418eJohann 4721b362b15af34006e6a11974088a46d42b903418eJohann if (tmp_err < *best_motion_err) 4731b362b15af34006e6a11974088a46d42b903418eJohann { 4741b362b15af34006e6a11974088a46d42b903418eJohann *best_motion_err = tmp_err; 4751b362b15af34006e6a11974088a46d42b903418eJohann best_mv->row = tmp_mv.as_mv.row; 4761b362b15af34006e6a11974088a46d42b903418eJohann best_mv->col = tmp_mv.as_mv.col; 4771b362b15af34006e6a11974088a46d42b903418eJohann } 4781b362b15af34006e6a11974088a46d42b903418eJohann 4791b362b15af34006e6a11974088a46d42b903418eJohann /* Further step/diamond searches as necessary */ 4801b362b15af34006e6a11974088a46d42b903418eJohann n = num00; 4811b362b15af34006e6a11974088a46d42b903418eJohann num00 = 0; 4821b362b15af34006e6a11974088a46d42b903418eJohann 4831b362b15af34006e6a11974088a46d42b903418eJohann while (n < further_steps) 4841b362b15af34006e6a11974088a46d42b903418eJohann { 4851b362b15af34006e6a11974088a46d42b903418eJohann n++; 4861b362b15af34006e6a11974088a46d42b903418eJohann 4871b362b15af34006e6a11974088a46d42b903418eJohann if (num00) 4881b362b15af34006e6a11974088a46d42b903418eJohann num00--; 4891b362b15af34006e6a11974088a46d42b903418eJohann else 4901b362b15af34006e6a11974088a46d42b903418eJohann { 4911b362b15af34006e6a11974088a46d42b903418eJohann tmp_err = cpi->diamond_search_sad(x, b, d, &ref_mv_full, &tmp_mv, 4921b362b15af34006e6a11974088a46d42b903418eJohann step_param + n, x->sadperbit16, 4931b362b15af34006e6a11974088a46d42b903418eJohann &num00, &v_fn_ptr, x->mvcost, 4941b362b15af34006e6a11974088a46d42b903418eJohann ref_mv); 4951b362b15af34006e6a11974088a46d42b903418eJohann if ( tmp_err < INT_MAX-new_mv_mode_penalty ) 4961b362b15af34006e6a11974088a46d42b903418eJohann tmp_err += new_mv_mode_penalty; 4971b362b15af34006e6a11974088a46d42b903418eJohann 4981b362b15af34006e6a11974088a46d42b903418eJohann if (tmp_err < *best_motion_err) 4991b362b15af34006e6a11974088a46d42b903418eJohann { 5001b362b15af34006e6a11974088a46d42b903418eJohann *best_motion_err = tmp_err; 5011b362b15af34006e6a11974088a46d42b903418eJohann best_mv->row = tmp_mv.as_mv.row; 5021b362b15af34006e6a11974088a46d42b903418eJohann best_mv->col = tmp_mv.as_mv.col; 5031b362b15af34006e6a11974088a46d42b903418eJohann } 5041b362b15af34006e6a11974088a46d42b903418eJohann } 5051b362b15af34006e6a11974088a46d42b903418eJohann } 5061b362b15af34006e6a11974088a46d42b903418eJohann} 5071b362b15af34006e6a11974088a46d42b903418eJohann 5081b362b15af34006e6a11974088a46d42b903418eJohannvoid vp8_first_pass(VP8_COMP *cpi) 5091b362b15af34006e6a11974088a46d42b903418eJohann{ 5101b362b15af34006e6a11974088a46d42b903418eJohann int mb_row, mb_col; 5111b362b15af34006e6a11974088a46d42b903418eJohann MACROBLOCK *const x = & cpi->mb; 5121b362b15af34006e6a11974088a46d42b903418eJohann VP8_COMMON *const cm = & cpi->common; 5131b362b15af34006e6a11974088a46d42b903418eJohann MACROBLOCKD *const xd = & x->e_mbd; 5141b362b15af34006e6a11974088a46d42b903418eJohann 5151b362b15af34006e6a11974088a46d42b903418eJohann int recon_yoffset, recon_uvoffset; 5161b362b15af34006e6a11974088a46d42b903418eJohann YV12_BUFFER_CONFIG *lst_yv12 = &cm->yv12_fb[cm->lst_fb_idx]; 5171b362b15af34006e6a11974088a46d42b903418eJohann YV12_BUFFER_CONFIG *new_yv12 = &cm->yv12_fb[cm->new_fb_idx]; 5181b362b15af34006e6a11974088a46d42b903418eJohann YV12_BUFFER_CONFIG *gld_yv12 = &cm->yv12_fb[cm->gld_fb_idx]; 5191b362b15af34006e6a11974088a46d42b903418eJohann int recon_y_stride = lst_yv12->y_stride; 5201b362b15af34006e6a11974088a46d42b903418eJohann int recon_uv_stride = lst_yv12->uv_stride; 5211b362b15af34006e6a11974088a46d42b903418eJohann int64_t intra_error = 0; 5221b362b15af34006e6a11974088a46d42b903418eJohann int64_t coded_error = 0; 5231b362b15af34006e6a11974088a46d42b903418eJohann 5241b362b15af34006e6a11974088a46d42b903418eJohann int sum_mvr = 0, sum_mvc = 0; 5251b362b15af34006e6a11974088a46d42b903418eJohann int sum_mvr_abs = 0, sum_mvc_abs = 0; 5261b362b15af34006e6a11974088a46d42b903418eJohann int sum_mvrs = 0, sum_mvcs = 0; 5271b362b15af34006e6a11974088a46d42b903418eJohann int mvcount = 0; 5281b362b15af34006e6a11974088a46d42b903418eJohann int intercount = 0; 5291b362b15af34006e6a11974088a46d42b903418eJohann int second_ref_count = 0; 5301b362b15af34006e6a11974088a46d42b903418eJohann int intrapenalty = 256; 5311b362b15af34006e6a11974088a46d42b903418eJohann int neutral_count = 0; 5321b362b15af34006e6a11974088a46d42b903418eJohann int new_mv_count = 0; 5331b362b15af34006e6a11974088a46d42b903418eJohann int sum_in_vectors = 0; 5341b362b15af34006e6a11974088a46d42b903418eJohann uint32_t lastmv_as_int = 0; 5351b362b15af34006e6a11974088a46d42b903418eJohann 5361b362b15af34006e6a11974088a46d42b903418eJohann int_mv zero_ref_mv; 5371b362b15af34006e6a11974088a46d42b903418eJohann 5381b362b15af34006e6a11974088a46d42b903418eJohann zero_ref_mv.as_int = 0; 5391b362b15af34006e6a11974088a46d42b903418eJohann 5401b362b15af34006e6a11974088a46d42b903418eJohann vp8_clear_system_state(); 5411b362b15af34006e6a11974088a46d42b903418eJohann 5421b362b15af34006e6a11974088a46d42b903418eJohann x->src = * cpi->Source; 5431b362b15af34006e6a11974088a46d42b903418eJohann xd->pre = *lst_yv12; 5441b362b15af34006e6a11974088a46d42b903418eJohann xd->dst = *new_yv12; 5451b362b15af34006e6a11974088a46d42b903418eJohann 5461b362b15af34006e6a11974088a46d42b903418eJohann x->partition_info = x->pi; 5471b362b15af34006e6a11974088a46d42b903418eJohann 5481b362b15af34006e6a11974088a46d42b903418eJohann xd->mode_info_context = cm->mi; 5491b362b15af34006e6a11974088a46d42b903418eJohann 5501b362b15af34006e6a11974088a46d42b903418eJohann if(!cm->use_bilinear_mc_filter) 5511b362b15af34006e6a11974088a46d42b903418eJohann { 5521b362b15af34006e6a11974088a46d42b903418eJohann xd->subpixel_predict = vp8_sixtap_predict4x4; 5531b362b15af34006e6a11974088a46d42b903418eJohann xd->subpixel_predict8x4 = vp8_sixtap_predict8x4; 5541b362b15af34006e6a11974088a46d42b903418eJohann xd->subpixel_predict8x8 = vp8_sixtap_predict8x8; 5551b362b15af34006e6a11974088a46d42b903418eJohann xd->subpixel_predict16x16 = vp8_sixtap_predict16x16; 5561b362b15af34006e6a11974088a46d42b903418eJohann } 5571b362b15af34006e6a11974088a46d42b903418eJohann else 5581b362b15af34006e6a11974088a46d42b903418eJohann { 5591b362b15af34006e6a11974088a46d42b903418eJohann xd->subpixel_predict = vp8_bilinear_predict4x4; 5601b362b15af34006e6a11974088a46d42b903418eJohann xd->subpixel_predict8x4 = vp8_bilinear_predict8x4; 5611b362b15af34006e6a11974088a46d42b903418eJohann xd->subpixel_predict8x8 = vp8_bilinear_predict8x8; 5621b362b15af34006e6a11974088a46d42b903418eJohann xd->subpixel_predict16x16 = vp8_bilinear_predict16x16; 5631b362b15af34006e6a11974088a46d42b903418eJohann } 5641b362b15af34006e6a11974088a46d42b903418eJohann 5651b362b15af34006e6a11974088a46d42b903418eJohann vp8_build_block_offsets(x); 5661b362b15af34006e6a11974088a46d42b903418eJohann 5671b362b15af34006e6a11974088a46d42b903418eJohann /* set up frame new frame for intra coded blocks */ 5681b362b15af34006e6a11974088a46d42b903418eJohann vp8_setup_intra_recon(new_yv12); 5691b362b15af34006e6a11974088a46d42b903418eJohann vp8cx_frame_init_quantizer(cpi); 5701b362b15af34006e6a11974088a46d42b903418eJohann 5711b362b15af34006e6a11974088a46d42b903418eJohann /* Initialise the MV cost table to the defaults */ 5721b362b15af34006e6a11974088a46d42b903418eJohann { 5731b362b15af34006e6a11974088a46d42b903418eJohann int flag[2] = {1, 1}; 574ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang vp8_initialize_rd_consts(cpi, x, vp8_dc_quant(cm->base_qindex, cm->y1dc_delta_q)); 575da49e34c1fb5e99681f4ad99c21d9cfd83eddb96Vignesh Venkatasubramanian memcpy(cm->fc.mvc, vp8_default_mv_context, sizeof(vp8_default_mv_context)); 5761b362b15af34006e6a11974088a46d42b903418eJohann vp8_build_component_cost_table(cpi->mb.mvcost, (const MV_CONTEXT *) cm->fc.mvc, flag); 5771b362b15af34006e6a11974088a46d42b903418eJohann } 5781b362b15af34006e6a11974088a46d42b903418eJohann 5791b362b15af34006e6a11974088a46d42b903418eJohann /* for each macroblock row in image */ 5801b362b15af34006e6a11974088a46d42b903418eJohann for (mb_row = 0; mb_row < cm->mb_rows; mb_row++) 5811b362b15af34006e6a11974088a46d42b903418eJohann { 5821b362b15af34006e6a11974088a46d42b903418eJohann int_mv best_ref_mv; 5831b362b15af34006e6a11974088a46d42b903418eJohann 5841b362b15af34006e6a11974088a46d42b903418eJohann best_ref_mv.as_int = 0; 5851b362b15af34006e6a11974088a46d42b903418eJohann 5861b362b15af34006e6a11974088a46d42b903418eJohann /* reset above block coeffs */ 5871b362b15af34006e6a11974088a46d42b903418eJohann xd->up_available = (mb_row != 0); 5881b362b15af34006e6a11974088a46d42b903418eJohann recon_yoffset = (mb_row * recon_y_stride * 16); 5891b362b15af34006e6a11974088a46d42b903418eJohann recon_uvoffset = (mb_row * recon_uv_stride * 8); 5901b362b15af34006e6a11974088a46d42b903418eJohann 5911b362b15af34006e6a11974088a46d42b903418eJohann /* Set up limit values for motion vectors to prevent them extending 5921b362b15af34006e6a11974088a46d42b903418eJohann * outside the UMV borders 5931b362b15af34006e6a11974088a46d42b903418eJohann */ 5941b362b15af34006e6a11974088a46d42b903418eJohann x->mv_row_min = -((mb_row * 16) + (VP8BORDERINPIXELS - 16)); 5951b362b15af34006e6a11974088a46d42b903418eJohann x->mv_row_max = ((cm->mb_rows - 1 - mb_row) * 16) + (VP8BORDERINPIXELS - 16); 5961b362b15af34006e6a11974088a46d42b903418eJohann 5971b362b15af34006e6a11974088a46d42b903418eJohann 5981b362b15af34006e6a11974088a46d42b903418eJohann /* for each macroblock col in image */ 5991b362b15af34006e6a11974088a46d42b903418eJohann for (mb_col = 0; mb_col < cm->mb_cols; mb_col++) 6001b362b15af34006e6a11974088a46d42b903418eJohann { 6011b362b15af34006e6a11974088a46d42b903418eJohann int this_error; 6021b362b15af34006e6a11974088a46d42b903418eJohann int gf_motion_error = INT_MAX; 6031b362b15af34006e6a11974088a46d42b903418eJohann int use_dc_pred = (mb_col || mb_row) && (!mb_col || !mb_row); 6041b362b15af34006e6a11974088a46d42b903418eJohann 6051b362b15af34006e6a11974088a46d42b903418eJohann xd->dst.y_buffer = new_yv12->y_buffer + recon_yoffset; 6061b362b15af34006e6a11974088a46d42b903418eJohann xd->dst.u_buffer = new_yv12->u_buffer + recon_uvoffset; 6071b362b15af34006e6a11974088a46d42b903418eJohann xd->dst.v_buffer = new_yv12->v_buffer + recon_uvoffset; 6081b362b15af34006e6a11974088a46d42b903418eJohann xd->left_available = (mb_col != 0); 6091b362b15af34006e6a11974088a46d42b903418eJohann 6101b362b15af34006e6a11974088a46d42b903418eJohann /* Copy current mb to a buffer */ 6111b362b15af34006e6a11974088a46d42b903418eJohann vp8_copy_mem16x16(x->src.y_buffer, x->src.y_stride, x->thismb, 16); 6121b362b15af34006e6a11974088a46d42b903418eJohann 6131b362b15af34006e6a11974088a46d42b903418eJohann /* do intra 16x16 prediction */ 6141b362b15af34006e6a11974088a46d42b903418eJohann this_error = vp8_encode_intra(cpi, x, use_dc_pred); 6151b362b15af34006e6a11974088a46d42b903418eJohann 6161b362b15af34006e6a11974088a46d42b903418eJohann /* "intrapenalty" below deals with situations where the intra 6171b362b15af34006e6a11974088a46d42b903418eJohann * and inter error scores are very low (eg a plain black frame) 6181b362b15af34006e6a11974088a46d42b903418eJohann * We do not have special cases in first pass for 0,0 and 6191b362b15af34006e6a11974088a46d42b903418eJohann * nearest etc so all inter modes carry an overhead cost 6201b362b15af34006e6a11974088a46d42b903418eJohann * estimate fot the mv. When the error score is very low this 6211b362b15af34006e6a11974088a46d42b903418eJohann * causes us to pick all or lots of INTRA modes and throw lots 6221b362b15af34006e6a11974088a46d42b903418eJohann * of key frames. This penalty adds a cost matching that of a 6231b362b15af34006e6a11974088a46d42b903418eJohann * 0,0 mv to the intra case. 6241b362b15af34006e6a11974088a46d42b903418eJohann */ 6251b362b15af34006e6a11974088a46d42b903418eJohann this_error += intrapenalty; 6261b362b15af34006e6a11974088a46d42b903418eJohann 6271b362b15af34006e6a11974088a46d42b903418eJohann /* Cumulative intra error total */ 6281b362b15af34006e6a11974088a46d42b903418eJohann intra_error += (int64_t)this_error; 6291b362b15af34006e6a11974088a46d42b903418eJohann 6301b362b15af34006e6a11974088a46d42b903418eJohann /* Set up limit values for motion vectors to prevent them 6311b362b15af34006e6a11974088a46d42b903418eJohann * extending outside the UMV borders 6321b362b15af34006e6a11974088a46d42b903418eJohann */ 6331b362b15af34006e6a11974088a46d42b903418eJohann x->mv_col_min = -((mb_col * 16) + (VP8BORDERINPIXELS - 16)); 6341b362b15af34006e6a11974088a46d42b903418eJohann x->mv_col_max = ((cm->mb_cols - 1 - mb_col) * 16) + (VP8BORDERINPIXELS - 16); 6351b362b15af34006e6a11974088a46d42b903418eJohann 6361b362b15af34006e6a11974088a46d42b903418eJohann /* Other than for the first frame do a motion search */ 6371b362b15af34006e6a11974088a46d42b903418eJohann if (cm->current_video_frame > 0) 6381b362b15af34006e6a11974088a46d42b903418eJohann { 6391b362b15af34006e6a11974088a46d42b903418eJohann BLOCKD *d = &x->e_mbd.block[0]; 6401b362b15af34006e6a11974088a46d42b903418eJohann MV tmp_mv = {0, 0}; 6411b362b15af34006e6a11974088a46d42b903418eJohann int tmp_err; 6421b362b15af34006e6a11974088a46d42b903418eJohann int motion_error = INT_MAX; 6431b362b15af34006e6a11974088a46d42b903418eJohann int raw_motion_error = INT_MAX; 6441b362b15af34006e6a11974088a46d42b903418eJohann 6451b362b15af34006e6a11974088a46d42b903418eJohann /* Simple 0,0 motion with no mv overhead */ 6461b362b15af34006e6a11974088a46d42b903418eJohann zz_motion_search( cpi, x, cpi->last_frame_unscaled_source, 6471b362b15af34006e6a11974088a46d42b903418eJohann &raw_motion_error, lst_yv12, &motion_error, 6481b362b15af34006e6a11974088a46d42b903418eJohann recon_yoffset ); 6491b362b15af34006e6a11974088a46d42b903418eJohann d->bmi.mv.as_mv.row = 0; 6501b362b15af34006e6a11974088a46d42b903418eJohann d->bmi.mv.as_mv.col = 0; 6511b362b15af34006e6a11974088a46d42b903418eJohann 6521b362b15af34006e6a11974088a46d42b903418eJohann if (raw_motion_error < cpi->oxcf.encode_breakout) 6531b362b15af34006e6a11974088a46d42b903418eJohann goto skip_motion_search; 6541b362b15af34006e6a11974088a46d42b903418eJohann 6551b362b15af34006e6a11974088a46d42b903418eJohann /* Test last reference frame using the previous best mv as the 6561b362b15af34006e6a11974088a46d42b903418eJohann * starting point (best reference) for the search 6571b362b15af34006e6a11974088a46d42b903418eJohann */ 6581b362b15af34006e6a11974088a46d42b903418eJohann first_pass_motion_search(cpi, x, &best_ref_mv, 6591b362b15af34006e6a11974088a46d42b903418eJohann &d->bmi.mv.as_mv, lst_yv12, 6601b362b15af34006e6a11974088a46d42b903418eJohann &motion_error, recon_yoffset); 6611b362b15af34006e6a11974088a46d42b903418eJohann 6621b362b15af34006e6a11974088a46d42b903418eJohann /* If the current best reference mv is not centred on 0,0 6631b362b15af34006e6a11974088a46d42b903418eJohann * then do a 0,0 based search as well 6641b362b15af34006e6a11974088a46d42b903418eJohann */ 6651b362b15af34006e6a11974088a46d42b903418eJohann if (best_ref_mv.as_int) 6661b362b15af34006e6a11974088a46d42b903418eJohann { 6671b362b15af34006e6a11974088a46d42b903418eJohann tmp_err = INT_MAX; 6681b362b15af34006e6a11974088a46d42b903418eJohann first_pass_motion_search(cpi, x, &zero_ref_mv, &tmp_mv, 6691b362b15af34006e6a11974088a46d42b903418eJohann lst_yv12, &tmp_err, recon_yoffset); 6701b362b15af34006e6a11974088a46d42b903418eJohann 6711b362b15af34006e6a11974088a46d42b903418eJohann if ( tmp_err < motion_error ) 6721b362b15af34006e6a11974088a46d42b903418eJohann { 6731b362b15af34006e6a11974088a46d42b903418eJohann motion_error = tmp_err; 6741b362b15af34006e6a11974088a46d42b903418eJohann d->bmi.mv.as_mv.row = tmp_mv.row; 6751b362b15af34006e6a11974088a46d42b903418eJohann d->bmi.mv.as_mv.col = tmp_mv.col; 6761b362b15af34006e6a11974088a46d42b903418eJohann } 6771b362b15af34006e6a11974088a46d42b903418eJohann } 6781b362b15af34006e6a11974088a46d42b903418eJohann 6791b362b15af34006e6a11974088a46d42b903418eJohann /* Experimental search in a second reference frame ((0,0) 6801b362b15af34006e6a11974088a46d42b903418eJohann * based only) 6811b362b15af34006e6a11974088a46d42b903418eJohann */ 6821b362b15af34006e6a11974088a46d42b903418eJohann if (cm->current_video_frame > 1) 6831b362b15af34006e6a11974088a46d42b903418eJohann { 6841b362b15af34006e6a11974088a46d42b903418eJohann first_pass_motion_search(cpi, x, &zero_ref_mv, &tmp_mv, gld_yv12, &gf_motion_error, recon_yoffset); 6851b362b15af34006e6a11974088a46d42b903418eJohann 6861b362b15af34006e6a11974088a46d42b903418eJohann if ((gf_motion_error < motion_error) && (gf_motion_error < this_error)) 6871b362b15af34006e6a11974088a46d42b903418eJohann { 6881b362b15af34006e6a11974088a46d42b903418eJohann second_ref_count++; 6891b362b15af34006e6a11974088a46d42b903418eJohann } 6901b362b15af34006e6a11974088a46d42b903418eJohann 6911b362b15af34006e6a11974088a46d42b903418eJohann /* Reset to last frame as reference buffer */ 6921b362b15af34006e6a11974088a46d42b903418eJohann xd->pre.y_buffer = lst_yv12->y_buffer + recon_yoffset; 6931b362b15af34006e6a11974088a46d42b903418eJohann xd->pre.u_buffer = lst_yv12->u_buffer + recon_uvoffset; 6941b362b15af34006e6a11974088a46d42b903418eJohann xd->pre.v_buffer = lst_yv12->v_buffer + recon_uvoffset; 6951b362b15af34006e6a11974088a46d42b903418eJohann } 6961b362b15af34006e6a11974088a46d42b903418eJohann 6971b362b15af34006e6a11974088a46d42b903418eJohannskip_motion_search: 6981b362b15af34006e6a11974088a46d42b903418eJohann /* Intra assumed best */ 6991b362b15af34006e6a11974088a46d42b903418eJohann best_ref_mv.as_int = 0; 7001b362b15af34006e6a11974088a46d42b903418eJohann 7011b362b15af34006e6a11974088a46d42b903418eJohann if (motion_error <= this_error) 7021b362b15af34006e6a11974088a46d42b903418eJohann { 7031b362b15af34006e6a11974088a46d42b903418eJohann /* Keep a count of cases where the inter and intra were 7041b362b15af34006e6a11974088a46d42b903418eJohann * very close and very low. This helps with scene cut 7051b362b15af34006e6a11974088a46d42b903418eJohann * detection for example in cropped clips with black bars 7061b362b15af34006e6a11974088a46d42b903418eJohann * at the sides or top and bottom. 7071b362b15af34006e6a11974088a46d42b903418eJohann */ 7081b362b15af34006e6a11974088a46d42b903418eJohann if( (((this_error-intrapenalty) * 9) <= 7091b362b15af34006e6a11974088a46d42b903418eJohann (motion_error*10)) && 7101b362b15af34006e6a11974088a46d42b903418eJohann (this_error < (2*intrapenalty)) ) 7111b362b15af34006e6a11974088a46d42b903418eJohann { 7121b362b15af34006e6a11974088a46d42b903418eJohann neutral_count++; 7131b362b15af34006e6a11974088a46d42b903418eJohann } 7141b362b15af34006e6a11974088a46d42b903418eJohann 7155ae7ac49f08a179e4f054d99fcfc9dce78d26e58hkuang d->bmi.mv.as_mv.row *= 8; 7165ae7ac49f08a179e4f054d99fcfc9dce78d26e58hkuang d->bmi.mv.as_mv.col *= 8; 7171b362b15af34006e6a11974088a46d42b903418eJohann this_error = motion_error; 7181b362b15af34006e6a11974088a46d42b903418eJohann vp8_set_mbmode_and_mvs(x, NEWMV, &d->bmi.mv); 7191b362b15af34006e6a11974088a46d42b903418eJohann vp8_encode_inter16x16y(x); 7201b362b15af34006e6a11974088a46d42b903418eJohann sum_mvr += d->bmi.mv.as_mv.row; 7211b362b15af34006e6a11974088a46d42b903418eJohann sum_mvr_abs += abs(d->bmi.mv.as_mv.row); 7221b362b15af34006e6a11974088a46d42b903418eJohann sum_mvc += d->bmi.mv.as_mv.col; 7231b362b15af34006e6a11974088a46d42b903418eJohann sum_mvc_abs += abs(d->bmi.mv.as_mv.col); 7241b362b15af34006e6a11974088a46d42b903418eJohann sum_mvrs += d->bmi.mv.as_mv.row * d->bmi.mv.as_mv.row; 7251b362b15af34006e6a11974088a46d42b903418eJohann sum_mvcs += d->bmi.mv.as_mv.col * d->bmi.mv.as_mv.col; 7261b362b15af34006e6a11974088a46d42b903418eJohann intercount++; 7271b362b15af34006e6a11974088a46d42b903418eJohann 7281b362b15af34006e6a11974088a46d42b903418eJohann best_ref_mv.as_int = d->bmi.mv.as_int; 7291b362b15af34006e6a11974088a46d42b903418eJohann 7301b362b15af34006e6a11974088a46d42b903418eJohann /* Was the vector non-zero */ 7311b362b15af34006e6a11974088a46d42b903418eJohann if (d->bmi.mv.as_int) 7321b362b15af34006e6a11974088a46d42b903418eJohann { 7331b362b15af34006e6a11974088a46d42b903418eJohann mvcount++; 7341b362b15af34006e6a11974088a46d42b903418eJohann 7351b362b15af34006e6a11974088a46d42b903418eJohann /* Was it different from the last non zero vector */ 7361b362b15af34006e6a11974088a46d42b903418eJohann if ( d->bmi.mv.as_int != lastmv_as_int ) 7371b362b15af34006e6a11974088a46d42b903418eJohann new_mv_count++; 7381b362b15af34006e6a11974088a46d42b903418eJohann lastmv_as_int = d->bmi.mv.as_int; 7391b362b15af34006e6a11974088a46d42b903418eJohann 7401b362b15af34006e6a11974088a46d42b903418eJohann /* Does the Row vector point inwards or outwards */ 7411b362b15af34006e6a11974088a46d42b903418eJohann if (mb_row < cm->mb_rows / 2) 7421b362b15af34006e6a11974088a46d42b903418eJohann { 7431b362b15af34006e6a11974088a46d42b903418eJohann if (d->bmi.mv.as_mv.row > 0) 7441b362b15af34006e6a11974088a46d42b903418eJohann sum_in_vectors--; 7451b362b15af34006e6a11974088a46d42b903418eJohann else if (d->bmi.mv.as_mv.row < 0) 7461b362b15af34006e6a11974088a46d42b903418eJohann sum_in_vectors++; 7471b362b15af34006e6a11974088a46d42b903418eJohann } 7481b362b15af34006e6a11974088a46d42b903418eJohann else if (mb_row > cm->mb_rows / 2) 7491b362b15af34006e6a11974088a46d42b903418eJohann { 7501b362b15af34006e6a11974088a46d42b903418eJohann if (d->bmi.mv.as_mv.row > 0) 7511b362b15af34006e6a11974088a46d42b903418eJohann sum_in_vectors++; 7521b362b15af34006e6a11974088a46d42b903418eJohann else if (d->bmi.mv.as_mv.row < 0) 7531b362b15af34006e6a11974088a46d42b903418eJohann sum_in_vectors--; 7541b362b15af34006e6a11974088a46d42b903418eJohann } 7551b362b15af34006e6a11974088a46d42b903418eJohann 7561b362b15af34006e6a11974088a46d42b903418eJohann /* Does the Row vector point inwards or outwards */ 7571b362b15af34006e6a11974088a46d42b903418eJohann if (mb_col < cm->mb_cols / 2) 7581b362b15af34006e6a11974088a46d42b903418eJohann { 7591b362b15af34006e6a11974088a46d42b903418eJohann if (d->bmi.mv.as_mv.col > 0) 7601b362b15af34006e6a11974088a46d42b903418eJohann sum_in_vectors--; 7611b362b15af34006e6a11974088a46d42b903418eJohann else if (d->bmi.mv.as_mv.col < 0) 7621b362b15af34006e6a11974088a46d42b903418eJohann sum_in_vectors++; 7631b362b15af34006e6a11974088a46d42b903418eJohann } 7641b362b15af34006e6a11974088a46d42b903418eJohann else if (mb_col > cm->mb_cols / 2) 7651b362b15af34006e6a11974088a46d42b903418eJohann { 7661b362b15af34006e6a11974088a46d42b903418eJohann if (d->bmi.mv.as_mv.col > 0) 7671b362b15af34006e6a11974088a46d42b903418eJohann sum_in_vectors++; 7681b362b15af34006e6a11974088a46d42b903418eJohann else if (d->bmi.mv.as_mv.col < 0) 7691b362b15af34006e6a11974088a46d42b903418eJohann sum_in_vectors--; 7701b362b15af34006e6a11974088a46d42b903418eJohann } 7711b362b15af34006e6a11974088a46d42b903418eJohann } 7721b362b15af34006e6a11974088a46d42b903418eJohann } 7731b362b15af34006e6a11974088a46d42b903418eJohann } 7741b362b15af34006e6a11974088a46d42b903418eJohann 7751b362b15af34006e6a11974088a46d42b903418eJohann coded_error += (int64_t)this_error; 7761b362b15af34006e6a11974088a46d42b903418eJohann 7771b362b15af34006e6a11974088a46d42b903418eJohann /* adjust to the next column of macroblocks */ 7781b362b15af34006e6a11974088a46d42b903418eJohann x->src.y_buffer += 16; 7791b362b15af34006e6a11974088a46d42b903418eJohann x->src.u_buffer += 8; 7801b362b15af34006e6a11974088a46d42b903418eJohann x->src.v_buffer += 8; 7811b362b15af34006e6a11974088a46d42b903418eJohann 7821b362b15af34006e6a11974088a46d42b903418eJohann recon_yoffset += 16; 7831b362b15af34006e6a11974088a46d42b903418eJohann recon_uvoffset += 8; 7841b362b15af34006e6a11974088a46d42b903418eJohann } 7851b362b15af34006e6a11974088a46d42b903418eJohann 7861b362b15af34006e6a11974088a46d42b903418eJohann /* adjust to the next row of mbs */ 7871b362b15af34006e6a11974088a46d42b903418eJohann x->src.y_buffer += 16 * x->src.y_stride - 16 * cm->mb_cols; 7881b362b15af34006e6a11974088a46d42b903418eJohann x->src.u_buffer += 8 * x->src.uv_stride - 8 * cm->mb_cols; 7891b362b15af34006e6a11974088a46d42b903418eJohann x->src.v_buffer += 8 * x->src.uv_stride - 8 * cm->mb_cols; 7901b362b15af34006e6a11974088a46d42b903418eJohann 7911b362b15af34006e6a11974088a46d42b903418eJohann /* extend the recon for intra prediction */ 7921b362b15af34006e6a11974088a46d42b903418eJohann vp8_extend_mb_row(new_yv12, xd->dst.y_buffer + 16, xd->dst.u_buffer + 8, xd->dst.v_buffer + 8); 7931b362b15af34006e6a11974088a46d42b903418eJohann vp8_clear_system_state(); 7941b362b15af34006e6a11974088a46d42b903418eJohann } 7951b362b15af34006e6a11974088a46d42b903418eJohann 7961b362b15af34006e6a11974088a46d42b903418eJohann vp8_clear_system_state(); 7971b362b15af34006e6a11974088a46d42b903418eJohann { 7981b362b15af34006e6a11974088a46d42b903418eJohann double weight = 0.0; 7991b362b15af34006e6a11974088a46d42b903418eJohann 8001b362b15af34006e6a11974088a46d42b903418eJohann FIRSTPASS_STATS fps; 8011b362b15af34006e6a11974088a46d42b903418eJohann 8021b362b15af34006e6a11974088a46d42b903418eJohann fps.frame = cm->current_video_frame ; 8031b362b15af34006e6a11974088a46d42b903418eJohann fps.intra_error = (double)(intra_error >> 8); 8041b362b15af34006e6a11974088a46d42b903418eJohann fps.coded_error = (double)(coded_error >> 8); 8051b362b15af34006e6a11974088a46d42b903418eJohann weight = simple_weight(cpi->Source); 8061b362b15af34006e6a11974088a46d42b903418eJohann 8071b362b15af34006e6a11974088a46d42b903418eJohann 8081b362b15af34006e6a11974088a46d42b903418eJohann if (weight < 0.1) 8091b362b15af34006e6a11974088a46d42b903418eJohann weight = 0.1; 8101b362b15af34006e6a11974088a46d42b903418eJohann 8111b362b15af34006e6a11974088a46d42b903418eJohann fps.ssim_weighted_pred_err = fps.coded_error * weight; 8121b362b15af34006e6a11974088a46d42b903418eJohann 8131b362b15af34006e6a11974088a46d42b903418eJohann fps.pcnt_inter = 0.0; 8141b362b15af34006e6a11974088a46d42b903418eJohann fps.pcnt_motion = 0.0; 8151b362b15af34006e6a11974088a46d42b903418eJohann fps.MVr = 0.0; 8161b362b15af34006e6a11974088a46d42b903418eJohann fps.mvr_abs = 0.0; 8171b362b15af34006e6a11974088a46d42b903418eJohann fps.MVc = 0.0; 8181b362b15af34006e6a11974088a46d42b903418eJohann fps.mvc_abs = 0.0; 8191b362b15af34006e6a11974088a46d42b903418eJohann fps.MVrv = 0.0; 8201b362b15af34006e6a11974088a46d42b903418eJohann fps.MVcv = 0.0; 8211b362b15af34006e6a11974088a46d42b903418eJohann fps.mv_in_out_count = 0.0; 8221b362b15af34006e6a11974088a46d42b903418eJohann fps.new_mv_count = 0.0; 8231b362b15af34006e6a11974088a46d42b903418eJohann fps.count = 1.0; 8241b362b15af34006e6a11974088a46d42b903418eJohann 8251b362b15af34006e6a11974088a46d42b903418eJohann fps.pcnt_inter = 1.0 * (double)intercount / cm->MBs; 8261b362b15af34006e6a11974088a46d42b903418eJohann fps.pcnt_second_ref = 1.0 * (double)second_ref_count / cm->MBs; 8271b362b15af34006e6a11974088a46d42b903418eJohann fps.pcnt_neutral = 1.0 * (double)neutral_count / cm->MBs; 8281b362b15af34006e6a11974088a46d42b903418eJohann 8291b362b15af34006e6a11974088a46d42b903418eJohann if (mvcount > 0) 8301b362b15af34006e6a11974088a46d42b903418eJohann { 8311b362b15af34006e6a11974088a46d42b903418eJohann fps.MVr = (double)sum_mvr / (double)mvcount; 8321b362b15af34006e6a11974088a46d42b903418eJohann fps.mvr_abs = (double)sum_mvr_abs / (double)mvcount; 8331b362b15af34006e6a11974088a46d42b903418eJohann fps.MVc = (double)sum_mvc / (double)mvcount; 8341b362b15af34006e6a11974088a46d42b903418eJohann fps.mvc_abs = (double)sum_mvc_abs / (double)mvcount; 8351b362b15af34006e6a11974088a46d42b903418eJohann fps.MVrv = ((double)sum_mvrs - (fps.MVr * fps.MVr / (double)mvcount)) / (double)mvcount; 8361b362b15af34006e6a11974088a46d42b903418eJohann fps.MVcv = ((double)sum_mvcs - (fps.MVc * fps.MVc / (double)mvcount)) / (double)mvcount; 8371b362b15af34006e6a11974088a46d42b903418eJohann fps.mv_in_out_count = (double)sum_in_vectors / (double)(mvcount * 2); 8381b362b15af34006e6a11974088a46d42b903418eJohann fps.new_mv_count = new_mv_count; 8391b362b15af34006e6a11974088a46d42b903418eJohann 8401b362b15af34006e6a11974088a46d42b903418eJohann fps.pcnt_motion = 1.0 * (double)mvcount / cpi->common.MBs; 8411b362b15af34006e6a11974088a46d42b903418eJohann } 8421b362b15af34006e6a11974088a46d42b903418eJohann 8431b362b15af34006e6a11974088a46d42b903418eJohann /* TODO: handle the case when duration is set to 0, or something less 8441b362b15af34006e6a11974088a46d42b903418eJohann * than the full time between subsequent cpi->source_time_stamps 8451b362b15af34006e6a11974088a46d42b903418eJohann */ 8461b362b15af34006e6a11974088a46d42b903418eJohann fps.duration = (double)(cpi->source->ts_end 8471b362b15af34006e6a11974088a46d42b903418eJohann - cpi->source->ts_start); 8481b362b15af34006e6a11974088a46d42b903418eJohann 8491b362b15af34006e6a11974088a46d42b903418eJohann /* don't want to do output stats with a stack variable! */ 8501b362b15af34006e6a11974088a46d42b903418eJohann memcpy(&cpi->twopass.this_frame_stats, 8511b362b15af34006e6a11974088a46d42b903418eJohann &fps, 8521b362b15af34006e6a11974088a46d42b903418eJohann sizeof(FIRSTPASS_STATS)); 8531b362b15af34006e6a11974088a46d42b903418eJohann output_stats(cpi, cpi->output_pkt_list, &cpi->twopass.this_frame_stats); 8541b362b15af34006e6a11974088a46d42b903418eJohann accumulate_stats(&cpi->twopass.total_stats, &fps); 8551b362b15af34006e6a11974088a46d42b903418eJohann } 8561b362b15af34006e6a11974088a46d42b903418eJohann 8571b362b15af34006e6a11974088a46d42b903418eJohann /* Copy the previous Last Frame into the GF buffer if specific 8581b362b15af34006e6a11974088a46d42b903418eJohann * conditions for doing so are met 8591b362b15af34006e6a11974088a46d42b903418eJohann */ 8601b362b15af34006e6a11974088a46d42b903418eJohann if ((cm->current_video_frame > 0) && 8611b362b15af34006e6a11974088a46d42b903418eJohann (cpi->twopass.this_frame_stats.pcnt_inter > 0.20) && 862ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang ((cpi->twopass.this_frame_stats.intra_error / 863ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang DOUBLE_DIVIDE_CHECK(cpi->twopass.this_frame_stats.coded_error)) > 864ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang 2.0)) 8651b362b15af34006e6a11974088a46d42b903418eJohann { 8661b362b15af34006e6a11974088a46d42b903418eJohann vp8_yv12_copy_frame(lst_yv12, gld_yv12); 8671b362b15af34006e6a11974088a46d42b903418eJohann } 8681b362b15af34006e6a11974088a46d42b903418eJohann 8691b362b15af34006e6a11974088a46d42b903418eJohann /* swap frame pointers so last frame refers to the frame we just 8701b362b15af34006e6a11974088a46d42b903418eJohann * compressed 8711b362b15af34006e6a11974088a46d42b903418eJohann */ 8721b362b15af34006e6a11974088a46d42b903418eJohann vp8_swap_yv12_buffer(lst_yv12, new_yv12); 8731b362b15af34006e6a11974088a46d42b903418eJohann vp8_yv12_extend_frame_borders(lst_yv12); 8741b362b15af34006e6a11974088a46d42b903418eJohann 8751b362b15af34006e6a11974088a46d42b903418eJohann /* Special case for the first frame. Copy into the GF buffer as a 8761b362b15af34006e6a11974088a46d42b903418eJohann * second reference. 8771b362b15af34006e6a11974088a46d42b903418eJohann */ 8781b362b15af34006e6a11974088a46d42b903418eJohann if (cm->current_video_frame == 0) 8791b362b15af34006e6a11974088a46d42b903418eJohann { 8801b362b15af34006e6a11974088a46d42b903418eJohann vp8_yv12_copy_frame(lst_yv12, gld_yv12); 8811b362b15af34006e6a11974088a46d42b903418eJohann } 8821b362b15af34006e6a11974088a46d42b903418eJohann 8831b362b15af34006e6a11974088a46d42b903418eJohann 8841b362b15af34006e6a11974088a46d42b903418eJohann /* use this to see what the first pass reconstruction looks like */ 8851b362b15af34006e6a11974088a46d42b903418eJohann if (0) 8861b362b15af34006e6a11974088a46d42b903418eJohann { 8871b362b15af34006e6a11974088a46d42b903418eJohann char filename[512]; 8881b362b15af34006e6a11974088a46d42b903418eJohann FILE *recon_file; 8891b362b15af34006e6a11974088a46d42b903418eJohann sprintf(filename, "enc%04d.yuv", (int) cm->current_video_frame); 8901b362b15af34006e6a11974088a46d42b903418eJohann 8911b362b15af34006e6a11974088a46d42b903418eJohann if (cm->current_video_frame == 0) 8921b362b15af34006e6a11974088a46d42b903418eJohann recon_file = fopen(filename, "wb"); 8931b362b15af34006e6a11974088a46d42b903418eJohann else 8941b362b15af34006e6a11974088a46d42b903418eJohann recon_file = fopen(filename, "ab"); 8951b362b15af34006e6a11974088a46d42b903418eJohann 8961b362b15af34006e6a11974088a46d42b903418eJohann (void) fwrite(lst_yv12->buffer_alloc, lst_yv12->frame_size, 1, 8971b362b15af34006e6a11974088a46d42b903418eJohann recon_file); 8981b362b15af34006e6a11974088a46d42b903418eJohann fclose(recon_file); 8991b362b15af34006e6a11974088a46d42b903418eJohann } 9001b362b15af34006e6a11974088a46d42b903418eJohann 9011b362b15af34006e6a11974088a46d42b903418eJohann cm->current_video_frame++; 9021b362b15af34006e6a11974088a46d42b903418eJohann 9031b362b15af34006e6a11974088a46d42b903418eJohann} 9041b362b15af34006e6a11974088a46d42b903418eJohannextern const int vp8_bits_per_mb[2][QINDEX_RANGE]; 9051b362b15af34006e6a11974088a46d42b903418eJohann 9061b362b15af34006e6a11974088a46d42b903418eJohann/* Estimate a cost per mb attributable to overheads such as the coding of 9071b362b15af34006e6a11974088a46d42b903418eJohann * modes and motion vectors. 9081b362b15af34006e6a11974088a46d42b903418eJohann * Currently simplistic in its assumptions for testing. 9091b362b15af34006e6a11974088a46d42b903418eJohann */ 9101b362b15af34006e6a11974088a46d42b903418eJohann 9111b362b15af34006e6a11974088a46d42b903418eJohannstatic double bitcost( double prob ) 9121b362b15af34006e6a11974088a46d42b903418eJohann{ 9135ae7ac49f08a179e4f054d99fcfc9dce78d26e58hkuang if (prob > 0.000122) 9145ae7ac49f08a179e4f054d99fcfc9dce78d26e58hkuang return -log(prob) / log(2.0); 9155ae7ac49f08a179e4f054d99fcfc9dce78d26e58hkuang else 9165ae7ac49f08a179e4f054d99fcfc9dce78d26e58hkuang return 13.0; 9171b362b15af34006e6a11974088a46d42b903418eJohann} 9181b362b15af34006e6a11974088a46d42b903418eJohannstatic int64_t estimate_modemvcost(VP8_COMP *cpi, 9191b362b15af34006e6a11974088a46d42b903418eJohann FIRSTPASS_STATS * fpstats) 9201b362b15af34006e6a11974088a46d42b903418eJohann{ 9211b362b15af34006e6a11974088a46d42b903418eJohann int mv_cost; 9225ae7ac49f08a179e4f054d99fcfc9dce78d26e58hkuang int64_t mode_cost; 9231b362b15af34006e6a11974088a46d42b903418eJohann 9241b362b15af34006e6a11974088a46d42b903418eJohann double av_pct_inter = fpstats->pcnt_inter / fpstats->count; 9251b362b15af34006e6a11974088a46d42b903418eJohann double av_pct_motion = fpstats->pcnt_motion / fpstats->count; 9261b362b15af34006e6a11974088a46d42b903418eJohann double av_intra = (1.0 - av_pct_inter); 9271b362b15af34006e6a11974088a46d42b903418eJohann 9281b362b15af34006e6a11974088a46d42b903418eJohann double zz_cost; 9291b362b15af34006e6a11974088a46d42b903418eJohann double motion_cost; 9301b362b15af34006e6a11974088a46d42b903418eJohann double intra_cost; 9311b362b15af34006e6a11974088a46d42b903418eJohann 9321b362b15af34006e6a11974088a46d42b903418eJohann zz_cost = bitcost(av_pct_inter - av_pct_motion); 9331b362b15af34006e6a11974088a46d42b903418eJohann motion_cost = bitcost(av_pct_motion); 9341b362b15af34006e6a11974088a46d42b903418eJohann intra_cost = bitcost(av_intra); 9351b362b15af34006e6a11974088a46d42b903418eJohann 9361b362b15af34006e6a11974088a46d42b903418eJohann /* Estimate of extra bits per mv overhead for mbs 9371b362b15af34006e6a11974088a46d42b903418eJohann * << 9 is the normalization to the (bits * 512) used in vp8_bits_per_mb 9381b362b15af34006e6a11974088a46d42b903418eJohann */ 9391b362b15af34006e6a11974088a46d42b903418eJohann mv_cost = ((int)(fpstats->new_mv_count / fpstats->count) * 8) << 9; 9401b362b15af34006e6a11974088a46d42b903418eJohann 9411b362b15af34006e6a11974088a46d42b903418eJohann /* Crude estimate of overhead cost from modes 9421b362b15af34006e6a11974088a46d42b903418eJohann * << 9 is the normalization to (bits * 512) used in vp8_bits_per_mb 9431b362b15af34006e6a11974088a46d42b903418eJohann */ 944b08e2e23eec181e9951df33cd704ac294c5407b6Vignesh Venkatasubramanian mode_cost = (int64_t)((((av_pct_inter - av_pct_motion) * zz_cost) + 945b08e2e23eec181e9951df33cd704ac294c5407b6Vignesh Venkatasubramanian (av_pct_motion * motion_cost) + 946b08e2e23eec181e9951df33cd704ac294c5407b6Vignesh Venkatasubramanian (av_intra * intra_cost)) * cpi->common.MBs) * 512; 9471b362b15af34006e6a11974088a46d42b903418eJohann 9481b362b15af34006e6a11974088a46d42b903418eJohann return mv_cost + mode_cost; 9491b362b15af34006e6a11974088a46d42b903418eJohann} 9501b362b15af34006e6a11974088a46d42b903418eJohann 9511b362b15af34006e6a11974088a46d42b903418eJohannstatic double calc_correction_factor( double err_per_mb, 9521b362b15af34006e6a11974088a46d42b903418eJohann double err_devisor, 9531b362b15af34006e6a11974088a46d42b903418eJohann double pt_low, 9541b362b15af34006e6a11974088a46d42b903418eJohann double pt_high, 9551b362b15af34006e6a11974088a46d42b903418eJohann int Q ) 9561b362b15af34006e6a11974088a46d42b903418eJohann{ 9571b362b15af34006e6a11974088a46d42b903418eJohann double power_term; 9581b362b15af34006e6a11974088a46d42b903418eJohann double error_term = err_per_mb / err_devisor; 9591b362b15af34006e6a11974088a46d42b903418eJohann double correction_factor; 9601b362b15af34006e6a11974088a46d42b903418eJohann 9611b362b15af34006e6a11974088a46d42b903418eJohann /* Adjustment based on Q to power term. */ 9621b362b15af34006e6a11974088a46d42b903418eJohann power_term = pt_low + (Q * 0.01); 9631b362b15af34006e6a11974088a46d42b903418eJohann power_term = (power_term > pt_high) ? pt_high : power_term; 9641b362b15af34006e6a11974088a46d42b903418eJohann 9651b362b15af34006e6a11974088a46d42b903418eJohann /* Adjustments to error term */ 9661b362b15af34006e6a11974088a46d42b903418eJohann /* TBD */ 9671b362b15af34006e6a11974088a46d42b903418eJohann 9681b362b15af34006e6a11974088a46d42b903418eJohann /* Calculate correction factor */ 9691b362b15af34006e6a11974088a46d42b903418eJohann correction_factor = pow(error_term, power_term); 9701b362b15af34006e6a11974088a46d42b903418eJohann 9711b362b15af34006e6a11974088a46d42b903418eJohann /* Clip range */ 9721b362b15af34006e6a11974088a46d42b903418eJohann correction_factor = 9731b362b15af34006e6a11974088a46d42b903418eJohann (correction_factor < 0.05) 9741b362b15af34006e6a11974088a46d42b903418eJohann ? 0.05 : (correction_factor > 5.0) ? 5.0 : correction_factor; 9751b362b15af34006e6a11974088a46d42b903418eJohann 9761b362b15af34006e6a11974088a46d42b903418eJohann return correction_factor; 9771b362b15af34006e6a11974088a46d42b903418eJohann} 9781b362b15af34006e6a11974088a46d42b903418eJohann 9791b362b15af34006e6a11974088a46d42b903418eJohannstatic int estimate_max_q(VP8_COMP *cpi, 9801b362b15af34006e6a11974088a46d42b903418eJohann FIRSTPASS_STATS * fpstats, 9811b362b15af34006e6a11974088a46d42b903418eJohann int section_target_bandwitdh, 9821b362b15af34006e6a11974088a46d42b903418eJohann int overhead_bits ) 9831b362b15af34006e6a11974088a46d42b903418eJohann{ 9841b362b15af34006e6a11974088a46d42b903418eJohann int Q; 9851b362b15af34006e6a11974088a46d42b903418eJohann int num_mbs = cpi->common.MBs; 9861b362b15af34006e6a11974088a46d42b903418eJohann int target_norm_bits_per_mb; 9871b362b15af34006e6a11974088a46d42b903418eJohann 9881b362b15af34006e6a11974088a46d42b903418eJohann double section_err = (fpstats->coded_error / fpstats->count); 9891b362b15af34006e6a11974088a46d42b903418eJohann double err_per_mb = section_err / num_mbs; 9901b362b15af34006e6a11974088a46d42b903418eJohann double err_correction_factor; 9911b362b15af34006e6a11974088a46d42b903418eJohann double speed_correction = 1.0; 9921b362b15af34006e6a11974088a46d42b903418eJohann int overhead_bits_per_mb; 9931b362b15af34006e6a11974088a46d42b903418eJohann 9941b362b15af34006e6a11974088a46d42b903418eJohann if (section_target_bandwitdh <= 0) 9951b362b15af34006e6a11974088a46d42b903418eJohann return cpi->twopass.maxq_max_limit; /* Highest value allowed */ 9961b362b15af34006e6a11974088a46d42b903418eJohann 9971b362b15af34006e6a11974088a46d42b903418eJohann target_norm_bits_per_mb = 9981b362b15af34006e6a11974088a46d42b903418eJohann (section_target_bandwitdh < (1 << 20)) 9991b362b15af34006e6a11974088a46d42b903418eJohann ? (512 * section_target_bandwitdh) / num_mbs 10001b362b15af34006e6a11974088a46d42b903418eJohann : 512 * (section_target_bandwitdh / num_mbs); 10011b362b15af34006e6a11974088a46d42b903418eJohann 10021b362b15af34006e6a11974088a46d42b903418eJohann /* Calculate a corrective factor based on a rolling ratio of bits spent 10031b362b15af34006e6a11974088a46d42b903418eJohann * vs target bits 10041b362b15af34006e6a11974088a46d42b903418eJohann */ 10051b362b15af34006e6a11974088a46d42b903418eJohann if ((cpi->rolling_target_bits > 0) && 10061b362b15af34006e6a11974088a46d42b903418eJohann (cpi->active_worst_quality < cpi->worst_quality)) 10071b362b15af34006e6a11974088a46d42b903418eJohann { 10081b362b15af34006e6a11974088a46d42b903418eJohann double rolling_ratio; 10091b362b15af34006e6a11974088a46d42b903418eJohann 10101b362b15af34006e6a11974088a46d42b903418eJohann rolling_ratio = (double)cpi->rolling_actual_bits / 10111b362b15af34006e6a11974088a46d42b903418eJohann (double)cpi->rolling_target_bits; 10121b362b15af34006e6a11974088a46d42b903418eJohann 10131b362b15af34006e6a11974088a46d42b903418eJohann if (rolling_ratio < 0.95) 10141b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.est_max_qcorrection_factor -= 0.005; 10151b362b15af34006e6a11974088a46d42b903418eJohann else if (rolling_ratio > 1.05) 10161b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.est_max_qcorrection_factor += 0.005; 10171b362b15af34006e6a11974088a46d42b903418eJohann 10181b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.est_max_qcorrection_factor = 10191b362b15af34006e6a11974088a46d42b903418eJohann (cpi->twopass.est_max_qcorrection_factor < 0.1) 10201b362b15af34006e6a11974088a46d42b903418eJohann ? 0.1 10211b362b15af34006e6a11974088a46d42b903418eJohann : (cpi->twopass.est_max_qcorrection_factor > 10.0) 10221b362b15af34006e6a11974088a46d42b903418eJohann ? 10.0 : cpi->twopass.est_max_qcorrection_factor; 10231b362b15af34006e6a11974088a46d42b903418eJohann } 10241b362b15af34006e6a11974088a46d42b903418eJohann 10251b362b15af34006e6a11974088a46d42b903418eJohann /* Corrections for higher compression speed settings 10261b362b15af34006e6a11974088a46d42b903418eJohann * (reduced compression expected) 10271b362b15af34006e6a11974088a46d42b903418eJohann */ 10281b362b15af34006e6a11974088a46d42b903418eJohann if ((cpi->compressor_speed == 3) || (cpi->compressor_speed == 1)) 10291b362b15af34006e6a11974088a46d42b903418eJohann { 10301b362b15af34006e6a11974088a46d42b903418eJohann if (cpi->oxcf.cpu_used <= 5) 10311b362b15af34006e6a11974088a46d42b903418eJohann speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04); 10321b362b15af34006e6a11974088a46d42b903418eJohann else 10331b362b15af34006e6a11974088a46d42b903418eJohann speed_correction = 1.25; 10341b362b15af34006e6a11974088a46d42b903418eJohann } 10351b362b15af34006e6a11974088a46d42b903418eJohann 10361b362b15af34006e6a11974088a46d42b903418eJohann /* Estimate of overhead bits per mb */ 10371b362b15af34006e6a11974088a46d42b903418eJohann /* Correction to overhead bits for min allowed Q. */ 10381b362b15af34006e6a11974088a46d42b903418eJohann overhead_bits_per_mb = overhead_bits / num_mbs; 10391b362b15af34006e6a11974088a46d42b903418eJohann overhead_bits_per_mb = (int)(overhead_bits_per_mb * 10401b362b15af34006e6a11974088a46d42b903418eJohann pow( 0.98, (double)cpi->twopass.maxq_min_limit )); 10411b362b15af34006e6a11974088a46d42b903418eJohann 10421b362b15af34006e6a11974088a46d42b903418eJohann /* Try and pick a max Q that will be high enough to encode the 10431b362b15af34006e6a11974088a46d42b903418eJohann * content at the given rate. 10441b362b15af34006e6a11974088a46d42b903418eJohann */ 10451b362b15af34006e6a11974088a46d42b903418eJohann for (Q = cpi->twopass.maxq_min_limit; Q < cpi->twopass.maxq_max_limit; Q++) 10461b362b15af34006e6a11974088a46d42b903418eJohann { 10471b362b15af34006e6a11974088a46d42b903418eJohann int bits_per_mb_at_this_q; 10481b362b15af34006e6a11974088a46d42b903418eJohann 10491b362b15af34006e6a11974088a46d42b903418eJohann /* Error per MB based correction factor */ 10501b362b15af34006e6a11974088a46d42b903418eJohann err_correction_factor = 10511b362b15af34006e6a11974088a46d42b903418eJohann calc_correction_factor(err_per_mb, 150.0, 0.40, 0.90, Q); 10521b362b15af34006e6a11974088a46d42b903418eJohann 10531b362b15af34006e6a11974088a46d42b903418eJohann bits_per_mb_at_this_q = 10541b362b15af34006e6a11974088a46d42b903418eJohann vp8_bits_per_mb[INTER_FRAME][Q] + overhead_bits_per_mb; 10551b362b15af34006e6a11974088a46d42b903418eJohann 10561b362b15af34006e6a11974088a46d42b903418eJohann bits_per_mb_at_this_q = (int)(.5 + err_correction_factor 10571b362b15af34006e6a11974088a46d42b903418eJohann * speed_correction * cpi->twopass.est_max_qcorrection_factor 10581b362b15af34006e6a11974088a46d42b903418eJohann * cpi->twopass.section_max_qfactor 10591b362b15af34006e6a11974088a46d42b903418eJohann * (double)bits_per_mb_at_this_q); 10601b362b15af34006e6a11974088a46d42b903418eJohann 10611b362b15af34006e6a11974088a46d42b903418eJohann /* Mode and motion overhead */ 10621b362b15af34006e6a11974088a46d42b903418eJohann /* As Q rises in real encode loop rd code will force overhead down 10631b362b15af34006e6a11974088a46d42b903418eJohann * We make a crude adjustment for this here as *.98 per Q step. 10641b362b15af34006e6a11974088a46d42b903418eJohann */ 10651b362b15af34006e6a11974088a46d42b903418eJohann overhead_bits_per_mb = (int)((double)overhead_bits_per_mb * 0.98); 10661b362b15af34006e6a11974088a46d42b903418eJohann 10671b362b15af34006e6a11974088a46d42b903418eJohann if (bits_per_mb_at_this_q <= target_norm_bits_per_mb) 10681b362b15af34006e6a11974088a46d42b903418eJohann break; 10691b362b15af34006e6a11974088a46d42b903418eJohann } 10701b362b15af34006e6a11974088a46d42b903418eJohann 10711b362b15af34006e6a11974088a46d42b903418eJohann /* Restriction on active max q for constrained quality mode. */ 10721b362b15af34006e6a11974088a46d42b903418eJohann if ( (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) && 10731b362b15af34006e6a11974088a46d42b903418eJohann (Q < cpi->cq_target_quality) ) 10741b362b15af34006e6a11974088a46d42b903418eJohann { 10751b362b15af34006e6a11974088a46d42b903418eJohann Q = cpi->cq_target_quality; 10761b362b15af34006e6a11974088a46d42b903418eJohann } 10771b362b15af34006e6a11974088a46d42b903418eJohann 10781b362b15af34006e6a11974088a46d42b903418eJohann /* Adjust maxq_min_limit and maxq_max_limit limits based on 10791b362b15af34006e6a11974088a46d42b903418eJohann * average q observed in clip for non kf/gf.arf frames 10801b362b15af34006e6a11974088a46d42b903418eJohann * Give average a chance to settle though. 10811b362b15af34006e6a11974088a46d42b903418eJohann */ 10821b362b15af34006e6a11974088a46d42b903418eJohann if ( (cpi->ni_frames > 10831b362b15af34006e6a11974088a46d42b903418eJohann ((int)cpi->twopass.total_stats.count >> 8)) && 10841b362b15af34006e6a11974088a46d42b903418eJohann (cpi->ni_frames > 150) ) 10851b362b15af34006e6a11974088a46d42b903418eJohann { 10861b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.maxq_max_limit = ((cpi->ni_av_qi + 32) < cpi->worst_quality) 10871b362b15af34006e6a11974088a46d42b903418eJohann ? (cpi->ni_av_qi + 32) : cpi->worst_quality; 10881b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.maxq_min_limit = ((cpi->ni_av_qi - 32) > cpi->best_quality) 10891b362b15af34006e6a11974088a46d42b903418eJohann ? (cpi->ni_av_qi - 32) : cpi->best_quality; 10901b362b15af34006e6a11974088a46d42b903418eJohann } 10911b362b15af34006e6a11974088a46d42b903418eJohann 10921b362b15af34006e6a11974088a46d42b903418eJohann return Q; 10931b362b15af34006e6a11974088a46d42b903418eJohann} 10941b362b15af34006e6a11974088a46d42b903418eJohann 10951b362b15af34006e6a11974088a46d42b903418eJohann/* For cq mode estimate a cq level that matches the observed 10961b362b15af34006e6a11974088a46d42b903418eJohann * complexity and data rate. 10971b362b15af34006e6a11974088a46d42b903418eJohann */ 10981b362b15af34006e6a11974088a46d42b903418eJohannstatic int estimate_cq( VP8_COMP *cpi, 10991b362b15af34006e6a11974088a46d42b903418eJohann FIRSTPASS_STATS * fpstats, 11001b362b15af34006e6a11974088a46d42b903418eJohann int section_target_bandwitdh, 11011b362b15af34006e6a11974088a46d42b903418eJohann int overhead_bits ) 11021b362b15af34006e6a11974088a46d42b903418eJohann{ 11031b362b15af34006e6a11974088a46d42b903418eJohann int Q; 11041b362b15af34006e6a11974088a46d42b903418eJohann int num_mbs = cpi->common.MBs; 11051b362b15af34006e6a11974088a46d42b903418eJohann int target_norm_bits_per_mb; 11061b362b15af34006e6a11974088a46d42b903418eJohann 11071b362b15af34006e6a11974088a46d42b903418eJohann double section_err = (fpstats->coded_error / fpstats->count); 11081b362b15af34006e6a11974088a46d42b903418eJohann double err_per_mb = section_err / num_mbs; 11091b362b15af34006e6a11974088a46d42b903418eJohann double err_correction_factor; 11101b362b15af34006e6a11974088a46d42b903418eJohann double speed_correction = 1.0; 11111b362b15af34006e6a11974088a46d42b903418eJohann double clip_iiratio; 11121b362b15af34006e6a11974088a46d42b903418eJohann double clip_iifactor; 11131b362b15af34006e6a11974088a46d42b903418eJohann int overhead_bits_per_mb; 11141b362b15af34006e6a11974088a46d42b903418eJohann 11151b362b15af34006e6a11974088a46d42b903418eJohann if (0) 11161b362b15af34006e6a11974088a46d42b903418eJohann { 11171b362b15af34006e6a11974088a46d42b903418eJohann FILE *f = fopen("epmp.stt", "a"); 11181b362b15af34006e6a11974088a46d42b903418eJohann fprintf(f, "%10.2f\n", err_per_mb ); 11191b362b15af34006e6a11974088a46d42b903418eJohann fclose(f); 11201b362b15af34006e6a11974088a46d42b903418eJohann } 11211b362b15af34006e6a11974088a46d42b903418eJohann 11221b362b15af34006e6a11974088a46d42b903418eJohann target_norm_bits_per_mb = (section_target_bandwitdh < (1 << 20)) 11231b362b15af34006e6a11974088a46d42b903418eJohann ? (512 * section_target_bandwitdh) / num_mbs 11241b362b15af34006e6a11974088a46d42b903418eJohann : 512 * (section_target_bandwitdh / num_mbs); 11251b362b15af34006e6a11974088a46d42b903418eJohann 11261b362b15af34006e6a11974088a46d42b903418eJohann /* Estimate of overhead bits per mb */ 11271b362b15af34006e6a11974088a46d42b903418eJohann overhead_bits_per_mb = overhead_bits / num_mbs; 11281b362b15af34006e6a11974088a46d42b903418eJohann 11291b362b15af34006e6a11974088a46d42b903418eJohann /* Corrections for higher compression speed settings 11301b362b15af34006e6a11974088a46d42b903418eJohann * (reduced compression expected) 11311b362b15af34006e6a11974088a46d42b903418eJohann */ 11321b362b15af34006e6a11974088a46d42b903418eJohann if ((cpi->compressor_speed == 3) || (cpi->compressor_speed == 1)) 11331b362b15af34006e6a11974088a46d42b903418eJohann { 11341b362b15af34006e6a11974088a46d42b903418eJohann if (cpi->oxcf.cpu_used <= 5) 11351b362b15af34006e6a11974088a46d42b903418eJohann speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04); 11361b362b15af34006e6a11974088a46d42b903418eJohann else 11371b362b15af34006e6a11974088a46d42b903418eJohann speed_correction = 1.25; 11381b362b15af34006e6a11974088a46d42b903418eJohann } 11391b362b15af34006e6a11974088a46d42b903418eJohann 11401b362b15af34006e6a11974088a46d42b903418eJohann /* II ratio correction factor for clip as a whole */ 11411b362b15af34006e6a11974088a46d42b903418eJohann clip_iiratio = cpi->twopass.total_stats.intra_error / 11421b362b15af34006e6a11974088a46d42b903418eJohann DOUBLE_DIVIDE_CHECK(cpi->twopass.total_stats.coded_error); 11431b362b15af34006e6a11974088a46d42b903418eJohann clip_iifactor = 1.0 - ((clip_iiratio - 10.0) * 0.025); 11441b362b15af34006e6a11974088a46d42b903418eJohann if (clip_iifactor < 0.80) 11451b362b15af34006e6a11974088a46d42b903418eJohann clip_iifactor = 0.80; 11461b362b15af34006e6a11974088a46d42b903418eJohann 11471b362b15af34006e6a11974088a46d42b903418eJohann /* Try and pick a Q that can encode the content at the given rate. */ 11481b362b15af34006e6a11974088a46d42b903418eJohann for (Q = 0; Q < MAXQ; Q++) 11491b362b15af34006e6a11974088a46d42b903418eJohann { 11501b362b15af34006e6a11974088a46d42b903418eJohann int bits_per_mb_at_this_q; 11511b362b15af34006e6a11974088a46d42b903418eJohann 11521b362b15af34006e6a11974088a46d42b903418eJohann /* Error per MB based correction factor */ 11531b362b15af34006e6a11974088a46d42b903418eJohann err_correction_factor = 11541b362b15af34006e6a11974088a46d42b903418eJohann calc_correction_factor(err_per_mb, 100.0, 0.40, 0.90, Q); 11551b362b15af34006e6a11974088a46d42b903418eJohann 11561b362b15af34006e6a11974088a46d42b903418eJohann bits_per_mb_at_this_q = 11571b362b15af34006e6a11974088a46d42b903418eJohann vp8_bits_per_mb[INTER_FRAME][Q] + overhead_bits_per_mb; 11581b362b15af34006e6a11974088a46d42b903418eJohann 11591b362b15af34006e6a11974088a46d42b903418eJohann bits_per_mb_at_this_q = 11601b362b15af34006e6a11974088a46d42b903418eJohann (int)( .5 + err_correction_factor * 11611b362b15af34006e6a11974088a46d42b903418eJohann speed_correction * 11621b362b15af34006e6a11974088a46d42b903418eJohann clip_iifactor * 11631b362b15af34006e6a11974088a46d42b903418eJohann (double)bits_per_mb_at_this_q); 11641b362b15af34006e6a11974088a46d42b903418eJohann 11651b362b15af34006e6a11974088a46d42b903418eJohann /* Mode and motion overhead */ 11661b362b15af34006e6a11974088a46d42b903418eJohann /* As Q rises in real encode loop rd code will force overhead down 11671b362b15af34006e6a11974088a46d42b903418eJohann * We make a crude adjustment for this here as *.98 per Q step. 11681b362b15af34006e6a11974088a46d42b903418eJohann */ 11691b362b15af34006e6a11974088a46d42b903418eJohann overhead_bits_per_mb = (int)((double)overhead_bits_per_mb * 0.98); 11701b362b15af34006e6a11974088a46d42b903418eJohann 11711b362b15af34006e6a11974088a46d42b903418eJohann if (bits_per_mb_at_this_q <= target_norm_bits_per_mb) 11721b362b15af34006e6a11974088a46d42b903418eJohann break; 11731b362b15af34006e6a11974088a46d42b903418eJohann } 11741b362b15af34006e6a11974088a46d42b903418eJohann 11751b362b15af34006e6a11974088a46d42b903418eJohann /* Clip value to range "best allowed to (worst allowed - 1)" */ 11761b362b15af34006e6a11974088a46d42b903418eJohann Q = cq_level[Q]; 11771b362b15af34006e6a11974088a46d42b903418eJohann if ( Q >= cpi->worst_quality ) 11781b362b15af34006e6a11974088a46d42b903418eJohann Q = cpi->worst_quality - 1; 11791b362b15af34006e6a11974088a46d42b903418eJohann if ( Q < cpi->best_quality ) 11801b362b15af34006e6a11974088a46d42b903418eJohann Q = cpi->best_quality; 11811b362b15af34006e6a11974088a46d42b903418eJohann 11821b362b15af34006e6a11974088a46d42b903418eJohann return Q; 11831b362b15af34006e6a11974088a46d42b903418eJohann} 11841b362b15af34006e6a11974088a46d42b903418eJohann 11851b362b15af34006e6a11974088a46d42b903418eJohannstatic int estimate_q(VP8_COMP *cpi, double section_err, int section_target_bandwitdh) 11861b362b15af34006e6a11974088a46d42b903418eJohann{ 11871b362b15af34006e6a11974088a46d42b903418eJohann int Q; 11881b362b15af34006e6a11974088a46d42b903418eJohann int num_mbs = cpi->common.MBs; 11891b362b15af34006e6a11974088a46d42b903418eJohann int target_norm_bits_per_mb; 11901b362b15af34006e6a11974088a46d42b903418eJohann 11911b362b15af34006e6a11974088a46d42b903418eJohann double err_per_mb = section_err / num_mbs; 11921b362b15af34006e6a11974088a46d42b903418eJohann double err_correction_factor; 11931b362b15af34006e6a11974088a46d42b903418eJohann double speed_correction = 1.0; 11941b362b15af34006e6a11974088a46d42b903418eJohann 11951b362b15af34006e6a11974088a46d42b903418eJohann target_norm_bits_per_mb = (section_target_bandwitdh < (1 << 20)) ? (512 * section_target_bandwitdh) / num_mbs : 512 * (section_target_bandwitdh / num_mbs); 11961b362b15af34006e6a11974088a46d42b903418eJohann 11971b362b15af34006e6a11974088a46d42b903418eJohann /* Corrections for higher compression speed settings 11981b362b15af34006e6a11974088a46d42b903418eJohann * (reduced compression expected) 11991b362b15af34006e6a11974088a46d42b903418eJohann */ 12001b362b15af34006e6a11974088a46d42b903418eJohann if ((cpi->compressor_speed == 3) || (cpi->compressor_speed == 1)) 12011b362b15af34006e6a11974088a46d42b903418eJohann { 12021b362b15af34006e6a11974088a46d42b903418eJohann if (cpi->oxcf.cpu_used <= 5) 12031b362b15af34006e6a11974088a46d42b903418eJohann speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04); 12041b362b15af34006e6a11974088a46d42b903418eJohann else 12051b362b15af34006e6a11974088a46d42b903418eJohann speed_correction = 1.25; 12061b362b15af34006e6a11974088a46d42b903418eJohann } 12071b362b15af34006e6a11974088a46d42b903418eJohann 12081b362b15af34006e6a11974088a46d42b903418eJohann /* Try and pick a Q that can encode the content at the given rate. */ 12091b362b15af34006e6a11974088a46d42b903418eJohann for (Q = 0; Q < MAXQ; Q++) 12101b362b15af34006e6a11974088a46d42b903418eJohann { 12111b362b15af34006e6a11974088a46d42b903418eJohann int bits_per_mb_at_this_q; 12121b362b15af34006e6a11974088a46d42b903418eJohann 12131b362b15af34006e6a11974088a46d42b903418eJohann /* Error per MB based correction factor */ 12141b362b15af34006e6a11974088a46d42b903418eJohann err_correction_factor = 12151b362b15af34006e6a11974088a46d42b903418eJohann calc_correction_factor(err_per_mb, 150.0, 0.40, 0.90, Q); 12161b362b15af34006e6a11974088a46d42b903418eJohann 12171b362b15af34006e6a11974088a46d42b903418eJohann bits_per_mb_at_this_q = 12181b362b15af34006e6a11974088a46d42b903418eJohann (int)( .5 + ( err_correction_factor * 12191b362b15af34006e6a11974088a46d42b903418eJohann speed_correction * 12201b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.est_max_qcorrection_factor * 12211b362b15af34006e6a11974088a46d42b903418eJohann (double)vp8_bits_per_mb[INTER_FRAME][Q] / 1.0 ) ); 12221b362b15af34006e6a11974088a46d42b903418eJohann 12231b362b15af34006e6a11974088a46d42b903418eJohann if (bits_per_mb_at_this_q <= target_norm_bits_per_mb) 12241b362b15af34006e6a11974088a46d42b903418eJohann break; 12251b362b15af34006e6a11974088a46d42b903418eJohann } 12261b362b15af34006e6a11974088a46d42b903418eJohann 12271b362b15af34006e6a11974088a46d42b903418eJohann return Q; 12281b362b15af34006e6a11974088a46d42b903418eJohann} 12291b362b15af34006e6a11974088a46d42b903418eJohann 12301b362b15af34006e6a11974088a46d42b903418eJohann/* Estimate a worst case Q for a KF group */ 12311b362b15af34006e6a11974088a46d42b903418eJohannstatic int estimate_kf_group_q(VP8_COMP *cpi, double section_err, int section_target_bandwitdh, double group_iiratio) 12321b362b15af34006e6a11974088a46d42b903418eJohann{ 12331b362b15af34006e6a11974088a46d42b903418eJohann int Q; 12341b362b15af34006e6a11974088a46d42b903418eJohann int num_mbs = cpi->common.MBs; 12351b362b15af34006e6a11974088a46d42b903418eJohann int target_norm_bits_per_mb = (512 * section_target_bandwitdh) / num_mbs; 12361b362b15af34006e6a11974088a46d42b903418eJohann int bits_per_mb_at_this_q; 12371b362b15af34006e6a11974088a46d42b903418eJohann 12381b362b15af34006e6a11974088a46d42b903418eJohann double err_per_mb = section_err / num_mbs; 12391b362b15af34006e6a11974088a46d42b903418eJohann double err_correction_factor; 12401b362b15af34006e6a11974088a46d42b903418eJohann double speed_correction = 1.0; 12411b362b15af34006e6a11974088a46d42b903418eJohann double current_spend_ratio = 1.0; 12421b362b15af34006e6a11974088a46d42b903418eJohann 12431b362b15af34006e6a11974088a46d42b903418eJohann double pow_highq = (POW1 < 0.6) ? POW1 + 0.3 : 0.90; 12441b362b15af34006e6a11974088a46d42b903418eJohann double pow_lowq = (POW1 < 0.7) ? POW1 + 0.1 : 0.80; 12451b362b15af34006e6a11974088a46d42b903418eJohann 12461b362b15af34006e6a11974088a46d42b903418eJohann double iiratio_correction_factor = 1.0; 12471b362b15af34006e6a11974088a46d42b903418eJohann 12481b362b15af34006e6a11974088a46d42b903418eJohann double combined_correction_factor; 12491b362b15af34006e6a11974088a46d42b903418eJohann 12501b362b15af34006e6a11974088a46d42b903418eJohann /* Trap special case where the target is <= 0 */ 12511b362b15af34006e6a11974088a46d42b903418eJohann if (target_norm_bits_per_mb <= 0) 12521b362b15af34006e6a11974088a46d42b903418eJohann return MAXQ * 2; 12531b362b15af34006e6a11974088a46d42b903418eJohann 12541b362b15af34006e6a11974088a46d42b903418eJohann /* Calculate a corrective factor based on a rolling ratio of bits spent 12551b362b15af34006e6a11974088a46d42b903418eJohann * vs target bits 12561b362b15af34006e6a11974088a46d42b903418eJohann * This is clamped to the range 0.1 to 10.0 12571b362b15af34006e6a11974088a46d42b903418eJohann */ 12581b362b15af34006e6a11974088a46d42b903418eJohann if (cpi->long_rolling_target_bits <= 0) 12591b362b15af34006e6a11974088a46d42b903418eJohann current_spend_ratio = 10.0; 12601b362b15af34006e6a11974088a46d42b903418eJohann else 12611b362b15af34006e6a11974088a46d42b903418eJohann { 12621b362b15af34006e6a11974088a46d42b903418eJohann current_spend_ratio = (double)cpi->long_rolling_actual_bits / (double)cpi->long_rolling_target_bits; 12631b362b15af34006e6a11974088a46d42b903418eJohann current_spend_ratio = (current_spend_ratio > 10.0) ? 10.0 : (current_spend_ratio < 0.1) ? 0.1 : current_spend_ratio; 12641b362b15af34006e6a11974088a46d42b903418eJohann } 12651b362b15af34006e6a11974088a46d42b903418eJohann 12661b362b15af34006e6a11974088a46d42b903418eJohann /* Calculate a correction factor based on the quality of prediction in 12671b362b15af34006e6a11974088a46d42b903418eJohann * the sequence as indicated by intra_inter error score ratio (IIRatio) 12681b362b15af34006e6a11974088a46d42b903418eJohann * The idea here is to favour subsampling in the hardest sections vs 12691b362b15af34006e6a11974088a46d42b903418eJohann * the easyest. 12701b362b15af34006e6a11974088a46d42b903418eJohann */ 12711b362b15af34006e6a11974088a46d42b903418eJohann iiratio_correction_factor = 1.0 - ((group_iiratio - 6.0) * 0.1); 12721b362b15af34006e6a11974088a46d42b903418eJohann 12731b362b15af34006e6a11974088a46d42b903418eJohann if (iiratio_correction_factor < 0.5) 12741b362b15af34006e6a11974088a46d42b903418eJohann iiratio_correction_factor = 0.5; 12751b362b15af34006e6a11974088a46d42b903418eJohann 12761b362b15af34006e6a11974088a46d42b903418eJohann /* Corrections for higher compression speed settings 12771b362b15af34006e6a11974088a46d42b903418eJohann * (reduced compression expected) 12781b362b15af34006e6a11974088a46d42b903418eJohann */ 12791b362b15af34006e6a11974088a46d42b903418eJohann if ((cpi->compressor_speed == 3) || (cpi->compressor_speed == 1)) 12801b362b15af34006e6a11974088a46d42b903418eJohann { 12811b362b15af34006e6a11974088a46d42b903418eJohann if (cpi->oxcf.cpu_used <= 5) 12821b362b15af34006e6a11974088a46d42b903418eJohann speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04); 12831b362b15af34006e6a11974088a46d42b903418eJohann else 12841b362b15af34006e6a11974088a46d42b903418eJohann speed_correction = 1.25; 12851b362b15af34006e6a11974088a46d42b903418eJohann } 12861b362b15af34006e6a11974088a46d42b903418eJohann 12871b362b15af34006e6a11974088a46d42b903418eJohann /* Combine the various factors calculated above */ 12881b362b15af34006e6a11974088a46d42b903418eJohann combined_correction_factor = speed_correction * iiratio_correction_factor * current_spend_ratio; 12891b362b15af34006e6a11974088a46d42b903418eJohann 12901b362b15af34006e6a11974088a46d42b903418eJohann /* Try and pick a Q that should be high enough to encode the content at 12911b362b15af34006e6a11974088a46d42b903418eJohann * the given rate. 12921b362b15af34006e6a11974088a46d42b903418eJohann */ 12931b362b15af34006e6a11974088a46d42b903418eJohann for (Q = 0; Q < MAXQ; Q++) 12941b362b15af34006e6a11974088a46d42b903418eJohann { 12951b362b15af34006e6a11974088a46d42b903418eJohann /* Error per MB based correction factor */ 12961b362b15af34006e6a11974088a46d42b903418eJohann err_correction_factor = 12971b362b15af34006e6a11974088a46d42b903418eJohann calc_correction_factor(err_per_mb, 150.0, pow_lowq, pow_highq, Q); 12981b362b15af34006e6a11974088a46d42b903418eJohann 12991b362b15af34006e6a11974088a46d42b903418eJohann bits_per_mb_at_this_q = 13001b362b15af34006e6a11974088a46d42b903418eJohann (int)(.5 + ( err_correction_factor * 13011b362b15af34006e6a11974088a46d42b903418eJohann combined_correction_factor * 13021b362b15af34006e6a11974088a46d42b903418eJohann (double)vp8_bits_per_mb[INTER_FRAME][Q]) ); 13031b362b15af34006e6a11974088a46d42b903418eJohann 13041b362b15af34006e6a11974088a46d42b903418eJohann if (bits_per_mb_at_this_q <= target_norm_bits_per_mb) 13051b362b15af34006e6a11974088a46d42b903418eJohann break; 13061b362b15af34006e6a11974088a46d42b903418eJohann } 13071b362b15af34006e6a11974088a46d42b903418eJohann 13081b362b15af34006e6a11974088a46d42b903418eJohann /* If we could not hit the target even at Max Q then estimate what Q 13091b362b15af34006e6a11974088a46d42b903418eJohann * would have been required 13101b362b15af34006e6a11974088a46d42b903418eJohann */ 13111b362b15af34006e6a11974088a46d42b903418eJohann while ((bits_per_mb_at_this_q > target_norm_bits_per_mb) && (Q < (MAXQ * 2))) 13121b362b15af34006e6a11974088a46d42b903418eJohann { 13131b362b15af34006e6a11974088a46d42b903418eJohann 13141b362b15af34006e6a11974088a46d42b903418eJohann bits_per_mb_at_this_q = (int)(0.96 * bits_per_mb_at_this_q); 13151b362b15af34006e6a11974088a46d42b903418eJohann Q++; 13161b362b15af34006e6a11974088a46d42b903418eJohann } 13171b362b15af34006e6a11974088a46d42b903418eJohann 13181b362b15af34006e6a11974088a46d42b903418eJohann if (0) 13191b362b15af34006e6a11974088a46d42b903418eJohann { 13201b362b15af34006e6a11974088a46d42b903418eJohann FILE *f = fopen("estkf_q.stt", "a"); 13211b362b15af34006e6a11974088a46d42b903418eJohann fprintf(f, "%8d %8d %8d %8.2f %8.3f %8.2f %8.3f %8.3f %8.3f %8d\n", cpi->common.current_video_frame, bits_per_mb_at_this_q, 13221b362b15af34006e6a11974088a46d42b903418eJohann target_norm_bits_per_mb, err_per_mb, err_correction_factor, 13231b362b15af34006e6a11974088a46d42b903418eJohann current_spend_ratio, group_iiratio, iiratio_correction_factor, 13241b362b15af34006e6a11974088a46d42b903418eJohann (double)cpi->buffer_level / (double)cpi->oxcf.optimal_buffer_level, Q); 13251b362b15af34006e6a11974088a46d42b903418eJohann fclose(f); 13261b362b15af34006e6a11974088a46d42b903418eJohann } 13271b362b15af34006e6a11974088a46d42b903418eJohann 13281b362b15af34006e6a11974088a46d42b903418eJohann return Q; 13291b362b15af34006e6a11974088a46d42b903418eJohann} 13301b362b15af34006e6a11974088a46d42b903418eJohann 13311b362b15af34006e6a11974088a46d42b903418eJohannvoid vp8_init_second_pass(VP8_COMP *cpi) 13321b362b15af34006e6a11974088a46d42b903418eJohann{ 13331b362b15af34006e6a11974088a46d42b903418eJohann FIRSTPASS_STATS this_frame; 13341b362b15af34006e6a11974088a46d42b903418eJohann FIRSTPASS_STATS *start_pos; 13351b362b15af34006e6a11974088a46d42b903418eJohann 13361b362b15af34006e6a11974088a46d42b903418eJohann double two_pass_min_rate = (double)(cpi->oxcf.target_bandwidth * cpi->oxcf.two_pass_vbrmin_section / 100); 13371b362b15af34006e6a11974088a46d42b903418eJohann 13381b362b15af34006e6a11974088a46d42b903418eJohann zero_stats(&cpi->twopass.total_stats); 13391b362b15af34006e6a11974088a46d42b903418eJohann zero_stats(&cpi->twopass.total_left_stats); 13401b362b15af34006e6a11974088a46d42b903418eJohann 13411b362b15af34006e6a11974088a46d42b903418eJohann if (!cpi->twopass.stats_in_end) 13421b362b15af34006e6a11974088a46d42b903418eJohann return; 13431b362b15af34006e6a11974088a46d42b903418eJohann 13441b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.total_stats = *cpi->twopass.stats_in_end; 13451b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.total_left_stats = cpi->twopass.total_stats; 13461b362b15af34006e6a11974088a46d42b903418eJohann 13471b362b15af34006e6a11974088a46d42b903418eJohann /* each frame can have a different duration, as the frame rate in the 13481b362b15af34006e6a11974088a46d42b903418eJohann * source isn't guaranteed to be constant. The frame rate prior to 13491b362b15af34006e6a11974088a46d42b903418eJohann * the first frame encoded in the second pass is a guess. However the 13501b362b15af34006e6a11974088a46d42b903418eJohann * sum duration is not. Its calculated based on the actual durations of 13511b362b15af34006e6a11974088a46d42b903418eJohann * all frames from the first pass. 13521b362b15af34006e6a11974088a46d42b903418eJohann */ 135391037db265ecdd914a26e056cf69207b4f50924ehkuang vp8_new_framerate(cpi, 10000000.0 * cpi->twopass.total_stats.count / cpi->twopass.total_stats.duration); 13541b362b15af34006e6a11974088a46d42b903418eJohann 135591037db265ecdd914a26e056cf69207b4f50924ehkuang cpi->output_framerate = cpi->framerate; 13561b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.bits_left = (int64_t)(cpi->twopass.total_stats.duration * cpi->oxcf.target_bandwidth / 10000000.0) ; 13571b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.bits_left -= (int64_t)(cpi->twopass.total_stats.duration * two_pass_min_rate / 10000000.0); 13581b362b15af34006e6a11974088a46d42b903418eJohann 13591b362b15af34006e6a11974088a46d42b903418eJohann /* Calculate a minimum intra value to be used in determining the IIratio 13601b362b15af34006e6a11974088a46d42b903418eJohann * scores used in the second pass. We have this minimum to make sure 13611b362b15af34006e6a11974088a46d42b903418eJohann * that clips that are static but "low complexity" in the intra domain 13621b362b15af34006e6a11974088a46d42b903418eJohann * are still boosted appropriately for KF/GF/ARF 13631b362b15af34006e6a11974088a46d42b903418eJohann */ 13641b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.kf_intra_err_min = KF_MB_INTRA_MIN * cpi->common.MBs; 13651b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.gf_intra_err_min = GF_MB_INTRA_MIN * cpi->common.MBs; 13661b362b15af34006e6a11974088a46d42b903418eJohann 13671b362b15af34006e6a11974088a46d42b903418eJohann /* Scan the first pass file and calculate an average Intra / Inter error 13681b362b15af34006e6a11974088a46d42b903418eJohann * score ratio for the sequence 13691b362b15af34006e6a11974088a46d42b903418eJohann */ 13701b362b15af34006e6a11974088a46d42b903418eJohann { 13711b362b15af34006e6a11974088a46d42b903418eJohann double sum_iiratio = 0.0; 13721b362b15af34006e6a11974088a46d42b903418eJohann double IIRatio; 13731b362b15af34006e6a11974088a46d42b903418eJohann 13741b362b15af34006e6a11974088a46d42b903418eJohann start_pos = cpi->twopass.stats_in; /* Note starting "file" position */ 13751b362b15af34006e6a11974088a46d42b903418eJohann 13761b362b15af34006e6a11974088a46d42b903418eJohann while (input_stats(cpi, &this_frame) != EOF) 13771b362b15af34006e6a11974088a46d42b903418eJohann { 13781b362b15af34006e6a11974088a46d42b903418eJohann IIRatio = this_frame.intra_error / DOUBLE_DIVIDE_CHECK(this_frame.coded_error); 13791b362b15af34006e6a11974088a46d42b903418eJohann IIRatio = (IIRatio < 1.0) ? 1.0 : (IIRatio > 20.0) ? 20.0 : IIRatio; 13801b362b15af34006e6a11974088a46d42b903418eJohann sum_iiratio += IIRatio; 13811b362b15af34006e6a11974088a46d42b903418eJohann } 13821b362b15af34006e6a11974088a46d42b903418eJohann 13831b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.avg_iiratio = sum_iiratio / DOUBLE_DIVIDE_CHECK((double)cpi->twopass.total_stats.count); 13841b362b15af34006e6a11974088a46d42b903418eJohann 13851b362b15af34006e6a11974088a46d42b903418eJohann /* Reset file position */ 13861b362b15af34006e6a11974088a46d42b903418eJohann reset_fpf_position(cpi, start_pos); 13871b362b15af34006e6a11974088a46d42b903418eJohann } 13881b362b15af34006e6a11974088a46d42b903418eJohann 13891b362b15af34006e6a11974088a46d42b903418eJohann /* Scan the first pass file and calculate a modified total error based 13901b362b15af34006e6a11974088a46d42b903418eJohann * upon the bias/power function used to allocate bits 13911b362b15af34006e6a11974088a46d42b903418eJohann */ 13921b362b15af34006e6a11974088a46d42b903418eJohann { 13931b362b15af34006e6a11974088a46d42b903418eJohann start_pos = cpi->twopass.stats_in; /* Note starting "file" position */ 13941b362b15af34006e6a11974088a46d42b903418eJohann 13951b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.modified_error_total = 0.0; 13961b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.modified_error_used = 0.0; 13971b362b15af34006e6a11974088a46d42b903418eJohann 13981b362b15af34006e6a11974088a46d42b903418eJohann while (input_stats(cpi, &this_frame) != EOF) 13991b362b15af34006e6a11974088a46d42b903418eJohann { 14001b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.modified_error_total += calculate_modified_err(cpi, &this_frame); 14011b362b15af34006e6a11974088a46d42b903418eJohann } 14021b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.modified_error_left = cpi->twopass.modified_error_total; 14031b362b15af34006e6a11974088a46d42b903418eJohann 14041b362b15af34006e6a11974088a46d42b903418eJohann reset_fpf_position(cpi, start_pos); /* Reset file position */ 14051b362b15af34006e6a11974088a46d42b903418eJohann 14061b362b15af34006e6a11974088a46d42b903418eJohann } 14071b362b15af34006e6a11974088a46d42b903418eJohann} 14081b362b15af34006e6a11974088a46d42b903418eJohann 14091b362b15af34006e6a11974088a46d42b903418eJohannvoid vp8_end_second_pass(VP8_COMP *cpi) 14101b362b15af34006e6a11974088a46d42b903418eJohann{ 1411da49e34c1fb5e99681f4ad99c21d9cfd83eddb96Vignesh Venkatasubramanian (void)cpi; 14121b362b15af34006e6a11974088a46d42b903418eJohann} 14131b362b15af34006e6a11974088a46d42b903418eJohann 14141b362b15af34006e6a11974088a46d42b903418eJohann/* This function gives and estimate of how badly we believe the prediction 14151b362b15af34006e6a11974088a46d42b903418eJohann * quality is decaying from frame to frame. 14161b362b15af34006e6a11974088a46d42b903418eJohann */ 14171b362b15af34006e6a11974088a46d42b903418eJohannstatic double get_prediction_decay_rate(VP8_COMP *cpi, FIRSTPASS_STATS *next_frame) 14181b362b15af34006e6a11974088a46d42b903418eJohann{ 14191b362b15af34006e6a11974088a46d42b903418eJohann double prediction_decay_rate; 14201b362b15af34006e6a11974088a46d42b903418eJohann double motion_decay; 14211b362b15af34006e6a11974088a46d42b903418eJohann double motion_pct = next_frame->pcnt_motion; 1422da49e34c1fb5e99681f4ad99c21d9cfd83eddb96Vignesh Venkatasubramanian (void)cpi; 14231b362b15af34006e6a11974088a46d42b903418eJohann 14241b362b15af34006e6a11974088a46d42b903418eJohann /* Initial basis is the % mbs inter coded */ 14251b362b15af34006e6a11974088a46d42b903418eJohann prediction_decay_rate = next_frame->pcnt_inter; 14261b362b15af34006e6a11974088a46d42b903418eJohann 14271b362b15af34006e6a11974088a46d42b903418eJohann /* High % motion -> somewhat higher decay rate */ 14281b362b15af34006e6a11974088a46d42b903418eJohann motion_decay = (1.0 - (motion_pct / 20.0)); 14291b362b15af34006e6a11974088a46d42b903418eJohann if (motion_decay < prediction_decay_rate) 14301b362b15af34006e6a11974088a46d42b903418eJohann prediction_decay_rate = motion_decay; 14311b362b15af34006e6a11974088a46d42b903418eJohann 14321b362b15af34006e6a11974088a46d42b903418eJohann /* Adjustment to decay rate based on speed of motion */ 14331b362b15af34006e6a11974088a46d42b903418eJohann { 14341b362b15af34006e6a11974088a46d42b903418eJohann double this_mv_rabs; 14351b362b15af34006e6a11974088a46d42b903418eJohann double this_mv_cabs; 14361b362b15af34006e6a11974088a46d42b903418eJohann double distance_factor; 14371b362b15af34006e6a11974088a46d42b903418eJohann 14381b362b15af34006e6a11974088a46d42b903418eJohann this_mv_rabs = fabs(next_frame->mvr_abs * motion_pct); 14391b362b15af34006e6a11974088a46d42b903418eJohann this_mv_cabs = fabs(next_frame->mvc_abs * motion_pct); 14401b362b15af34006e6a11974088a46d42b903418eJohann 14411b362b15af34006e6a11974088a46d42b903418eJohann distance_factor = sqrt((this_mv_rabs * this_mv_rabs) + 14421b362b15af34006e6a11974088a46d42b903418eJohann (this_mv_cabs * this_mv_cabs)) / 250.0; 14431b362b15af34006e6a11974088a46d42b903418eJohann distance_factor = ((distance_factor > 1.0) 14441b362b15af34006e6a11974088a46d42b903418eJohann ? 0.0 : (1.0 - distance_factor)); 14451b362b15af34006e6a11974088a46d42b903418eJohann if (distance_factor < prediction_decay_rate) 14461b362b15af34006e6a11974088a46d42b903418eJohann prediction_decay_rate = distance_factor; 14471b362b15af34006e6a11974088a46d42b903418eJohann } 14481b362b15af34006e6a11974088a46d42b903418eJohann 14491b362b15af34006e6a11974088a46d42b903418eJohann return prediction_decay_rate; 14501b362b15af34006e6a11974088a46d42b903418eJohann} 14511b362b15af34006e6a11974088a46d42b903418eJohann 14521b362b15af34006e6a11974088a46d42b903418eJohann/* Function to test for a condition where a complex transition is followed 14531b362b15af34006e6a11974088a46d42b903418eJohann * by a static section. For example in slide shows where there is a fade 14541b362b15af34006e6a11974088a46d42b903418eJohann * between slides. This is to help with more optimal kf and gf positioning. 14551b362b15af34006e6a11974088a46d42b903418eJohann */ 14561b362b15af34006e6a11974088a46d42b903418eJohannstatic int detect_transition_to_still( 14571b362b15af34006e6a11974088a46d42b903418eJohann VP8_COMP *cpi, 14581b362b15af34006e6a11974088a46d42b903418eJohann int frame_interval, 14591b362b15af34006e6a11974088a46d42b903418eJohann int still_interval, 14601b362b15af34006e6a11974088a46d42b903418eJohann double loop_decay_rate, 14611b362b15af34006e6a11974088a46d42b903418eJohann double decay_accumulator ) 14621b362b15af34006e6a11974088a46d42b903418eJohann{ 14631b362b15af34006e6a11974088a46d42b903418eJohann int trans_to_still = 0; 14641b362b15af34006e6a11974088a46d42b903418eJohann 14651b362b15af34006e6a11974088a46d42b903418eJohann /* Break clause to detect very still sections after motion 14661b362b15af34006e6a11974088a46d42b903418eJohann * For example a static image after a fade or other transition 14671b362b15af34006e6a11974088a46d42b903418eJohann * instead of a clean scene cut. 14681b362b15af34006e6a11974088a46d42b903418eJohann */ 14691b362b15af34006e6a11974088a46d42b903418eJohann if ( (frame_interval > MIN_GF_INTERVAL) && 14701b362b15af34006e6a11974088a46d42b903418eJohann (loop_decay_rate >= 0.999) && 14711b362b15af34006e6a11974088a46d42b903418eJohann (decay_accumulator < 0.9) ) 14721b362b15af34006e6a11974088a46d42b903418eJohann { 14731b362b15af34006e6a11974088a46d42b903418eJohann int j; 14741b362b15af34006e6a11974088a46d42b903418eJohann FIRSTPASS_STATS * position = cpi->twopass.stats_in; 14751b362b15af34006e6a11974088a46d42b903418eJohann FIRSTPASS_STATS tmp_next_frame; 14761b362b15af34006e6a11974088a46d42b903418eJohann double decay_rate; 14771b362b15af34006e6a11974088a46d42b903418eJohann 14781b362b15af34006e6a11974088a46d42b903418eJohann /* Look ahead a few frames to see if static condition persists... */ 14791b362b15af34006e6a11974088a46d42b903418eJohann for ( j = 0; j < still_interval; j++ ) 14801b362b15af34006e6a11974088a46d42b903418eJohann { 14811b362b15af34006e6a11974088a46d42b903418eJohann if (EOF == input_stats(cpi, &tmp_next_frame)) 14821b362b15af34006e6a11974088a46d42b903418eJohann break; 14831b362b15af34006e6a11974088a46d42b903418eJohann 14841b362b15af34006e6a11974088a46d42b903418eJohann decay_rate = get_prediction_decay_rate(cpi, &tmp_next_frame); 14851b362b15af34006e6a11974088a46d42b903418eJohann if ( decay_rate < 0.999 ) 14861b362b15af34006e6a11974088a46d42b903418eJohann break; 14871b362b15af34006e6a11974088a46d42b903418eJohann } 14881b362b15af34006e6a11974088a46d42b903418eJohann /* Reset file position */ 14891b362b15af34006e6a11974088a46d42b903418eJohann reset_fpf_position(cpi, position); 14901b362b15af34006e6a11974088a46d42b903418eJohann 14911b362b15af34006e6a11974088a46d42b903418eJohann /* Only if it does do we signal a transition to still */ 14921b362b15af34006e6a11974088a46d42b903418eJohann if ( j == still_interval ) 14931b362b15af34006e6a11974088a46d42b903418eJohann trans_to_still = 1; 14941b362b15af34006e6a11974088a46d42b903418eJohann } 14951b362b15af34006e6a11974088a46d42b903418eJohann 14961b362b15af34006e6a11974088a46d42b903418eJohann return trans_to_still; 14971b362b15af34006e6a11974088a46d42b903418eJohann} 14981b362b15af34006e6a11974088a46d42b903418eJohann 14991b362b15af34006e6a11974088a46d42b903418eJohann/* This function detects a flash through the high relative pcnt_second_ref 15001b362b15af34006e6a11974088a46d42b903418eJohann * score in the frame following a flash frame. The offset passed in should 15011b362b15af34006e6a11974088a46d42b903418eJohann * reflect this 15021b362b15af34006e6a11974088a46d42b903418eJohann */ 15031b362b15af34006e6a11974088a46d42b903418eJohannstatic int detect_flash( VP8_COMP *cpi, int offset ) 15041b362b15af34006e6a11974088a46d42b903418eJohann{ 15051b362b15af34006e6a11974088a46d42b903418eJohann FIRSTPASS_STATS next_frame; 15061b362b15af34006e6a11974088a46d42b903418eJohann 15071b362b15af34006e6a11974088a46d42b903418eJohann int flash_detected = 0; 15081b362b15af34006e6a11974088a46d42b903418eJohann 15091b362b15af34006e6a11974088a46d42b903418eJohann /* Read the frame data. */ 15101b362b15af34006e6a11974088a46d42b903418eJohann /* The return is 0 (no flash detected) if not a valid frame */ 15111b362b15af34006e6a11974088a46d42b903418eJohann if ( read_frame_stats(cpi, &next_frame, offset) != EOF ) 15121b362b15af34006e6a11974088a46d42b903418eJohann { 15131b362b15af34006e6a11974088a46d42b903418eJohann /* What we are looking for here is a situation where there is a 15141b362b15af34006e6a11974088a46d42b903418eJohann * brief break in prediction (such as a flash) but subsequent frames 15151b362b15af34006e6a11974088a46d42b903418eJohann * are reasonably well predicted by an earlier (pre flash) frame. 15161b362b15af34006e6a11974088a46d42b903418eJohann * The recovery after a flash is indicated by a high pcnt_second_ref 15171b362b15af34006e6a11974088a46d42b903418eJohann * comapred to pcnt_inter. 15181b362b15af34006e6a11974088a46d42b903418eJohann */ 15191b362b15af34006e6a11974088a46d42b903418eJohann if ( (next_frame.pcnt_second_ref > next_frame.pcnt_inter) && 15201b362b15af34006e6a11974088a46d42b903418eJohann (next_frame.pcnt_second_ref >= 0.5 ) ) 15211b362b15af34006e6a11974088a46d42b903418eJohann { 15221b362b15af34006e6a11974088a46d42b903418eJohann flash_detected = 1; 15231b362b15af34006e6a11974088a46d42b903418eJohann 15241b362b15af34006e6a11974088a46d42b903418eJohann /*if (1) 15251b362b15af34006e6a11974088a46d42b903418eJohann { 15261b362b15af34006e6a11974088a46d42b903418eJohann FILE *f = fopen("flash.stt", "a"); 15271b362b15af34006e6a11974088a46d42b903418eJohann fprintf(f, "%8.0f %6.2f %6.2f\n", 15281b362b15af34006e6a11974088a46d42b903418eJohann next_frame.frame, 15291b362b15af34006e6a11974088a46d42b903418eJohann next_frame.pcnt_inter, 15301b362b15af34006e6a11974088a46d42b903418eJohann next_frame.pcnt_second_ref); 15311b362b15af34006e6a11974088a46d42b903418eJohann fclose(f); 15321b362b15af34006e6a11974088a46d42b903418eJohann }*/ 15331b362b15af34006e6a11974088a46d42b903418eJohann } 15341b362b15af34006e6a11974088a46d42b903418eJohann } 15351b362b15af34006e6a11974088a46d42b903418eJohann 15361b362b15af34006e6a11974088a46d42b903418eJohann return flash_detected; 15371b362b15af34006e6a11974088a46d42b903418eJohann} 15381b362b15af34006e6a11974088a46d42b903418eJohann 15391b362b15af34006e6a11974088a46d42b903418eJohann/* Update the motion related elements to the GF arf boost calculation */ 15401b362b15af34006e6a11974088a46d42b903418eJohannstatic void accumulate_frame_motion_stats( 15411b362b15af34006e6a11974088a46d42b903418eJohann VP8_COMP *cpi, 15421b362b15af34006e6a11974088a46d42b903418eJohann FIRSTPASS_STATS * this_frame, 15431b362b15af34006e6a11974088a46d42b903418eJohann double * this_frame_mv_in_out, 15441b362b15af34006e6a11974088a46d42b903418eJohann double * mv_in_out_accumulator, 15451b362b15af34006e6a11974088a46d42b903418eJohann double * abs_mv_in_out_accumulator, 15461b362b15af34006e6a11974088a46d42b903418eJohann double * mv_ratio_accumulator ) 15471b362b15af34006e6a11974088a46d42b903418eJohann{ 15481b362b15af34006e6a11974088a46d42b903418eJohann double this_frame_mvr_ratio; 15491b362b15af34006e6a11974088a46d42b903418eJohann double this_frame_mvc_ratio; 15501b362b15af34006e6a11974088a46d42b903418eJohann double motion_pct; 1551da49e34c1fb5e99681f4ad99c21d9cfd83eddb96Vignesh Venkatasubramanian (void)cpi; 15521b362b15af34006e6a11974088a46d42b903418eJohann 15531b362b15af34006e6a11974088a46d42b903418eJohann /* Accumulate motion stats. */ 15541b362b15af34006e6a11974088a46d42b903418eJohann motion_pct = this_frame->pcnt_motion; 15551b362b15af34006e6a11974088a46d42b903418eJohann 15561b362b15af34006e6a11974088a46d42b903418eJohann /* Accumulate Motion In/Out of frame stats */ 15571b362b15af34006e6a11974088a46d42b903418eJohann *this_frame_mv_in_out = this_frame->mv_in_out_count * motion_pct; 15581b362b15af34006e6a11974088a46d42b903418eJohann *mv_in_out_accumulator += this_frame->mv_in_out_count * motion_pct; 15591b362b15af34006e6a11974088a46d42b903418eJohann *abs_mv_in_out_accumulator += 15601b362b15af34006e6a11974088a46d42b903418eJohann fabs(this_frame->mv_in_out_count * motion_pct); 15611b362b15af34006e6a11974088a46d42b903418eJohann 15621b362b15af34006e6a11974088a46d42b903418eJohann /* Accumulate a measure of how uniform (or conversely how random) 15631b362b15af34006e6a11974088a46d42b903418eJohann * the motion field is. (A ratio of absmv / mv) 15641b362b15af34006e6a11974088a46d42b903418eJohann */ 15651b362b15af34006e6a11974088a46d42b903418eJohann if (motion_pct > 0.05) 15661b362b15af34006e6a11974088a46d42b903418eJohann { 15671b362b15af34006e6a11974088a46d42b903418eJohann this_frame_mvr_ratio = fabs(this_frame->mvr_abs) / 15681b362b15af34006e6a11974088a46d42b903418eJohann DOUBLE_DIVIDE_CHECK(fabs(this_frame->MVr)); 15691b362b15af34006e6a11974088a46d42b903418eJohann 15701b362b15af34006e6a11974088a46d42b903418eJohann this_frame_mvc_ratio = fabs(this_frame->mvc_abs) / 15711b362b15af34006e6a11974088a46d42b903418eJohann DOUBLE_DIVIDE_CHECK(fabs(this_frame->MVc)); 15721b362b15af34006e6a11974088a46d42b903418eJohann 15731b362b15af34006e6a11974088a46d42b903418eJohann *mv_ratio_accumulator += 15741b362b15af34006e6a11974088a46d42b903418eJohann (this_frame_mvr_ratio < this_frame->mvr_abs) 15751b362b15af34006e6a11974088a46d42b903418eJohann ? (this_frame_mvr_ratio * motion_pct) 15761b362b15af34006e6a11974088a46d42b903418eJohann : this_frame->mvr_abs * motion_pct; 15771b362b15af34006e6a11974088a46d42b903418eJohann 15781b362b15af34006e6a11974088a46d42b903418eJohann *mv_ratio_accumulator += 15791b362b15af34006e6a11974088a46d42b903418eJohann (this_frame_mvc_ratio < this_frame->mvc_abs) 15801b362b15af34006e6a11974088a46d42b903418eJohann ? (this_frame_mvc_ratio * motion_pct) 15811b362b15af34006e6a11974088a46d42b903418eJohann : this_frame->mvc_abs * motion_pct; 15821b362b15af34006e6a11974088a46d42b903418eJohann 15831b362b15af34006e6a11974088a46d42b903418eJohann } 15841b362b15af34006e6a11974088a46d42b903418eJohann} 15851b362b15af34006e6a11974088a46d42b903418eJohann 15861b362b15af34006e6a11974088a46d42b903418eJohann/* Calculate a baseline boost number for the current frame. */ 15871b362b15af34006e6a11974088a46d42b903418eJohannstatic double calc_frame_boost( 15881b362b15af34006e6a11974088a46d42b903418eJohann VP8_COMP *cpi, 15891b362b15af34006e6a11974088a46d42b903418eJohann FIRSTPASS_STATS * this_frame, 15901b362b15af34006e6a11974088a46d42b903418eJohann double this_frame_mv_in_out ) 15911b362b15af34006e6a11974088a46d42b903418eJohann{ 15921b362b15af34006e6a11974088a46d42b903418eJohann double frame_boost; 15931b362b15af34006e6a11974088a46d42b903418eJohann 15941b362b15af34006e6a11974088a46d42b903418eJohann /* Underlying boost factor is based on inter intra error ratio */ 15951b362b15af34006e6a11974088a46d42b903418eJohann if (this_frame->intra_error > cpi->twopass.gf_intra_err_min) 15961b362b15af34006e6a11974088a46d42b903418eJohann frame_boost = (IIFACTOR * this_frame->intra_error / 15971b362b15af34006e6a11974088a46d42b903418eJohann DOUBLE_DIVIDE_CHECK(this_frame->coded_error)); 15981b362b15af34006e6a11974088a46d42b903418eJohann else 15991b362b15af34006e6a11974088a46d42b903418eJohann frame_boost = (IIFACTOR * cpi->twopass.gf_intra_err_min / 16001b362b15af34006e6a11974088a46d42b903418eJohann DOUBLE_DIVIDE_CHECK(this_frame->coded_error)); 16011b362b15af34006e6a11974088a46d42b903418eJohann 16021b362b15af34006e6a11974088a46d42b903418eJohann /* Increase boost for frames where new data coming into frame 16031b362b15af34006e6a11974088a46d42b903418eJohann * (eg zoom out). Slightly reduce boost if there is a net balance 16041b362b15af34006e6a11974088a46d42b903418eJohann * of motion out of the frame (zoom in). 16051b362b15af34006e6a11974088a46d42b903418eJohann * The range for this_frame_mv_in_out is -1.0 to +1.0 16061b362b15af34006e6a11974088a46d42b903418eJohann */ 16071b362b15af34006e6a11974088a46d42b903418eJohann if (this_frame_mv_in_out > 0.0) 16081b362b15af34006e6a11974088a46d42b903418eJohann frame_boost += frame_boost * (this_frame_mv_in_out * 2.0); 16091b362b15af34006e6a11974088a46d42b903418eJohann /* In extreme case boost is halved */ 16101b362b15af34006e6a11974088a46d42b903418eJohann else 16111b362b15af34006e6a11974088a46d42b903418eJohann frame_boost += frame_boost * (this_frame_mv_in_out / 2.0); 16121b362b15af34006e6a11974088a46d42b903418eJohann 16131b362b15af34006e6a11974088a46d42b903418eJohann /* Clip to maximum */ 16141b362b15af34006e6a11974088a46d42b903418eJohann if (frame_boost > GF_RMAX) 16151b362b15af34006e6a11974088a46d42b903418eJohann frame_boost = GF_RMAX; 16161b362b15af34006e6a11974088a46d42b903418eJohann 16171b362b15af34006e6a11974088a46d42b903418eJohann return frame_boost; 16181b362b15af34006e6a11974088a46d42b903418eJohann} 16191b362b15af34006e6a11974088a46d42b903418eJohann 16201b362b15af34006e6a11974088a46d42b903418eJohann#if NEW_BOOST 16211b362b15af34006e6a11974088a46d42b903418eJohannstatic int calc_arf_boost( 16221b362b15af34006e6a11974088a46d42b903418eJohann VP8_COMP *cpi, 16231b362b15af34006e6a11974088a46d42b903418eJohann int offset, 16241b362b15af34006e6a11974088a46d42b903418eJohann int f_frames, 16251b362b15af34006e6a11974088a46d42b903418eJohann int b_frames, 16261b362b15af34006e6a11974088a46d42b903418eJohann int *f_boost, 16271b362b15af34006e6a11974088a46d42b903418eJohann int *b_boost ) 16281b362b15af34006e6a11974088a46d42b903418eJohann{ 16291b362b15af34006e6a11974088a46d42b903418eJohann FIRSTPASS_STATS this_frame; 16301b362b15af34006e6a11974088a46d42b903418eJohann 16311b362b15af34006e6a11974088a46d42b903418eJohann int i; 16321b362b15af34006e6a11974088a46d42b903418eJohann double boost_score = 0.0; 16331b362b15af34006e6a11974088a46d42b903418eJohann double mv_ratio_accumulator = 0.0; 16341b362b15af34006e6a11974088a46d42b903418eJohann double decay_accumulator = 1.0; 16351b362b15af34006e6a11974088a46d42b903418eJohann double this_frame_mv_in_out = 0.0; 16361b362b15af34006e6a11974088a46d42b903418eJohann double mv_in_out_accumulator = 0.0; 16371b362b15af34006e6a11974088a46d42b903418eJohann double abs_mv_in_out_accumulator = 0.0; 16381b362b15af34006e6a11974088a46d42b903418eJohann double r; 16391b362b15af34006e6a11974088a46d42b903418eJohann int flash_detected = 0; 16401b362b15af34006e6a11974088a46d42b903418eJohann 16411b362b15af34006e6a11974088a46d42b903418eJohann /* Search forward from the proposed arf/next gf position */ 16421b362b15af34006e6a11974088a46d42b903418eJohann for ( i = 0; i < f_frames; i++ ) 16431b362b15af34006e6a11974088a46d42b903418eJohann { 16441b362b15af34006e6a11974088a46d42b903418eJohann if ( read_frame_stats(cpi, &this_frame, (i+offset)) == EOF ) 16451b362b15af34006e6a11974088a46d42b903418eJohann break; 16461b362b15af34006e6a11974088a46d42b903418eJohann 16471b362b15af34006e6a11974088a46d42b903418eJohann /* Update the motion related elements to the boost calculation */ 16481b362b15af34006e6a11974088a46d42b903418eJohann accumulate_frame_motion_stats( cpi, &this_frame, 16491b362b15af34006e6a11974088a46d42b903418eJohann &this_frame_mv_in_out, &mv_in_out_accumulator, 16501b362b15af34006e6a11974088a46d42b903418eJohann &abs_mv_in_out_accumulator, &mv_ratio_accumulator ); 16511b362b15af34006e6a11974088a46d42b903418eJohann 16521b362b15af34006e6a11974088a46d42b903418eJohann /* Calculate the baseline boost number for this frame */ 16531b362b15af34006e6a11974088a46d42b903418eJohann r = calc_frame_boost( cpi, &this_frame, this_frame_mv_in_out ); 16541b362b15af34006e6a11974088a46d42b903418eJohann 16551b362b15af34006e6a11974088a46d42b903418eJohann /* We want to discount the the flash frame itself and the recovery 16561b362b15af34006e6a11974088a46d42b903418eJohann * frame that follows as both will have poor scores. 16571b362b15af34006e6a11974088a46d42b903418eJohann */ 16581b362b15af34006e6a11974088a46d42b903418eJohann flash_detected = detect_flash(cpi, (i+offset)) || 16591b362b15af34006e6a11974088a46d42b903418eJohann detect_flash(cpi, (i+offset+1)); 16601b362b15af34006e6a11974088a46d42b903418eJohann 16611b362b15af34006e6a11974088a46d42b903418eJohann /* Cumulative effect of prediction quality decay */ 16621b362b15af34006e6a11974088a46d42b903418eJohann if ( !flash_detected ) 16631b362b15af34006e6a11974088a46d42b903418eJohann { 16641b362b15af34006e6a11974088a46d42b903418eJohann decay_accumulator = 16651b362b15af34006e6a11974088a46d42b903418eJohann decay_accumulator * 16661b362b15af34006e6a11974088a46d42b903418eJohann get_prediction_decay_rate(cpi, &this_frame); 16671b362b15af34006e6a11974088a46d42b903418eJohann decay_accumulator = 16681b362b15af34006e6a11974088a46d42b903418eJohann decay_accumulator < 0.1 ? 0.1 : decay_accumulator; 16691b362b15af34006e6a11974088a46d42b903418eJohann } 16701b362b15af34006e6a11974088a46d42b903418eJohann boost_score += (decay_accumulator * r); 16711b362b15af34006e6a11974088a46d42b903418eJohann 16721b362b15af34006e6a11974088a46d42b903418eJohann /* Break out conditions. */ 16731b362b15af34006e6a11974088a46d42b903418eJohann if ( (!flash_detected) && 16741b362b15af34006e6a11974088a46d42b903418eJohann ((mv_ratio_accumulator > 100.0) || 16751b362b15af34006e6a11974088a46d42b903418eJohann (abs_mv_in_out_accumulator > 3.0) || 16761b362b15af34006e6a11974088a46d42b903418eJohann (mv_in_out_accumulator < -2.0) ) ) 16771b362b15af34006e6a11974088a46d42b903418eJohann { 16781b362b15af34006e6a11974088a46d42b903418eJohann break; 16791b362b15af34006e6a11974088a46d42b903418eJohann } 16801b362b15af34006e6a11974088a46d42b903418eJohann } 16811b362b15af34006e6a11974088a46d42b903418eJohann 16821b362b15af34006e6a11974088a46d42b903418eJohann *f_boost = (int)(boost_score * 100.0) >> 4; 16831b362b15af34006e6a11974088a46d42b903418eJohann 16841b362b15af34006e6a11974088a46d42b903418eJohann /* Reset for backward looking loop */ 16851b362b15af34006e6a11974088a46d42b903418eJohann boost_score = 0.0; 16861b362b15af34006e6a11974088a46d42b903418eJohann mv_ratio_accumulator = 0.0; 16871b362b15af34006e6a11974088a46d42b903418eJohann decay_accumulator = 1.0; 16881b362b15af34006e6a11974088a46d42b903418eJohann this_frame_mv_in_out = 0.0; 16891b362b15af34006e6a11974088a46d42b903418eJohann mv_in_out_accumulator = 0.0; 16901b362b15af34006e6a11974088a46d42b903418eJohann abs_mv_in_out_accumulator = 0.0; 16911b362b15af34006e6a11974088a46d42b903418eJohann 16921b362b15af34006e6a11974088a46d42b903418eJohann /* Search forward from the proposed arf/next gf position */ 16931b362b15af34006e6a11974088a46d42b903418eJohann for ( i = -1; i >= -b_frames; i-- ) 16941b362b15af34006e6a11974088a46d42b903418eJohann { 16951b362b15af34006e6a11974088a46d42b903418eJohann if ( read_frame_stats(cpi, &this_frame, (i+offset)) == EOF ) 16961b362b15af34006e6a11974088a46d42b903418eJohann break; 16971b362b15af34006e6a11974088a46d42b903418eJohann 16981b362b15af34006e6a11974088a46d42b903418eJohann /* Update the motion related elements to the boost calculation */ 16991b362b15af34006e6a11974088a46d42b903418eJohann accumulate_frame_motion_stats( cpi, &this_frame, 17001b362b15af34006e6a11974088a46d42b903418eJohann &this_frame_mv_in_out, &mv_in_out_accumulator, 17011b362b15af34006e6a11974088a46d42b903418eJohann &abs_mv_in_out_accumulator, &mv_ratio_accumulator ); 17021b362b15af34006e6a11974088a46d42b903418eJohann 17031b362b15af34006e6a11974088a46d42b903418eJohann /* Calculate the baseline boost number for this frame */ 17041b362b15af34006e6a11974088a46d42b903418eJohann r = calc_frame_boost( cpi, &this_frame, this_frame_mv_in_out ); 17051b362b15af34006e6a11974088a46d42b903418eJohann 17061b362b15af34006e6a11974088a46d42b903418eJohann /* We want to discount the the flash frame itself and the recovery 17071b362b15af34006e6a11974088a46d42b903418eJohann * frame that follows as both will have poor scores. 17081b362b15af34006e6a11974088a46d42b903418eJohann */ 17091b362b15af34006e6a11974088a46d42b903418eJohann flash_detected = detect_flash(cpi, (i+offset)) || 17101b362b15af34006e6a11974088a46d42b903418eJohann detect_flash(cpi, (i+offset+1)); 17111b362b15af34006e6a11974088a46d42b903418eJohann 17121b362b15af34006e6a11974088a46d42b903418eJohann /* Cumulative effect of prediction quality decay */ 17131b362b15af34006e6a11974088a46d42b903418eJohann if ( !flash_detected ) 17141b362b15af34006e6a11974088a46d42b903418eJohann { 17151b362b15af34006e6a11974088a46d42b903418eJohann decay_accumulator = 17161b362b15af34006e6a11974088a46d42b903418eJohann decay_accumulator * 17171b362b15af34006e6a11974088a46d42b903418eJohann get_prediction_decay_rate(cpi, &this_frame); 17181b362b15af34006e6a11974088a46d42b903418eJohann decay_accumulator = 17191b362b15af34006e6a11974088a46d42b903418eJohann decay_accumulator < 0.1 ? 0.1 : decay_accumulator; 17201b362b15af34006e6a11974088a46d42b903418eJohann } 17211b362b15af34006e6a11974088a46d42b903418eJohann 17221b362b15af34006e6a11974088a46d42b903418eJohann boost_score += (decay_accumulator * r); 17231b362b15af34006e6a11974088a46d42b903418eJohann 17241b362b15af34006e6a11974088a46d42b903418eJohann /* Break out conditions. */ 17251b362b15af34006e6a11974088a46d42b903418eJohann if ( (!flash_detected) && 17261b362b15af34006e6a11974088a46d42b903418eJohann ((mv_ratio_accumulator > 100.0) || 17271b362b15af34006e6a11974088a46d42b903418eJohann (abs_mv_in_out_accumulator > 3.0) || 17281b362b15af34006e6a11974088a46d42b903418eJohann (mv_in_out_accumulator < -2.0) ) ) 17291b362b15af34006e6a11974088a46d42b903418eJohann { 17301b362b15af34006e6a11974088a46d42b903418eJohann break; 17311b362b15af34006e6a11974088a46d42b903418eJohann } 17321b362b15af34006e6a11974088a46d42b903418eJohann } 17331b362b15af34006e6a11974088a46d42b903418eJohann *b_boost = (int)(boost_score * 100.0) >> 4; 17341b362b15af34006e6a11974088a46d42b903418eJohann 17351b362b15af34006e6a11974088a46d42b903418eJohann return (*f_boost + *b_boost); 17361b362b15af34006e6a11974088a46d42b903418eJohann} 17371b362b15af34006e6a11974088a46d42b903418eJohann#endif 17381b362b15af34006e6a11974088a46d42b903418eJohann 17391b362b15af34006e6a11974088a46d42b903418eJohann/* Analyse and define a gf/arf group . */ 17401b362b15af34006e6a11974088a46d42b903418eJohannstatic void define_gf_group(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame) 17411b362b15af34006e6a11974088a46d42b903418eJohann{ 17421b362b15af34006e6a11974088a46d42b903418eJohann FIRSTPASS_STATS next_frame; 17431b362b15af34006e6a11974088a46d42b903418eJohann FIRSTPASS_STATS *start_pos; 17441b362b15af34006e6a11974088a46d42b903418eJohann int i; 17451b362b15af34006e6a11974088a46d42b903418eJohann double r; 17461b362b15af34006e6a11974088a46d42b903418eJohann double boost_score = 0.0; 17471b362b15af34006e6a11974088a46d42b903418eJohann double old_boost_score = 0.0; 17481b362b15af34006e6a11974088a46d42b903418eJohann double gf_group_err = 0.0; 17491b362b15af34006e6a11974088a46d42b903418eJohann double gf_first_frame_err = 0.0; 17501b362b15af34006e6a11974088a46d42b903418eJohann double mod_frame_err = 0.0; 17511b362b15af34006e6a11974088a46d42b903418eJohann 17521b362b15af34006e6a11974088a46d42b903418eJohann double mv_ratio_accumulator = 0.0; 17531b362b15af34006e6a11974088a46d42b903418eJohann double decay_accumulator = 1.0; 17541b362b15af34006e6a11974088a46d42b903418eJohann 17551b362b15af34006e6a11974088a46d42b903418eJohann double loop_decay_rate = 1.00; /* Starting decay rate */ 17561b362b15af34006e6a11974088a46d42b903418eJohann 17571b362b15af34006e6a11974088a46d42b903418eJohann double this_frame_mv_in_out = 0.0; 17581b362b15af34006e6a11974088a46d42b903418eJohann double mv_in_out_accumulator = 0.0; 17591b362b15af34006e6a11974088a46d42b903418eJohann double abs_mv_in_out_accumulator = 0.0; 17601b362b15af34006e6a11974088a46d42b903418eJohann double mod_err_per_mb_accumulator = 0.0; 17611b362b15af34006e6a11974088a46d42b903418eJohann 17621b362b15af34006e6a11974088a46d42b903418eJohann int max_bits = frame_max_bits(cpi); /* Max for a single frame */ 17631b362b15af34006e6a11974088a46d42b903418eJohann 17641b362b15af34006e6a11974088a46d42b903418eJohann unsigned int allow_alt_ref = 17651b362b15af34006e6a11974088a46d42b903418eJohann cpi->oxcf.play_alternate && cpi->oxcf.lag_in_frames; 17661b362b15af34006e6a11974088a46d42b903418eJohann 17671b362b15af34006e6a11974088a46d42b903418eJohann int alt_boost = 0; 17681b362b15af34006e6a11974088a46d42b903418eJohann int f_boost = 0; 17691b362b15af34006e6a11974088a46d42b903418eJohann int b_boost = 0; 17701b362b15af34006e6a11974088a46d42b903418eJohann int flash_detected; 17711b362b15af34006e6a11974088a46d42b903418eJohann 17721b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.gf_group_bits = 0; 17731b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.gf_decay_rate = 0; 17741b362b15af34006e6a11974088a46d42b903418eJohann 17751b362b15af34006e6a11974088a46d42b903418eJohann vp8_clear_system_state(); 17761b362b15af34006e6a11974088a46d42b903418eJohann 17771b362b15af34006e6a11974088a46d42b903418eJohann start_pos = cpi->twopass.stats_in; 17781b362b15af34006e6a11974088a46d42b903418eJohann 1779da49e34c1fb5e99681f4ad99c21d9cfd83eddb96Vignesh Venkatasubramanian memset(&next_frame, 0, sizeof(next_frame)); /* assure clean */ 17801b362b15af34006e6a11974088a46d42b903418eJohann 17811b362b15af34006e6a11974088a46d42b903418eJohann /* Load stats for the current frame. */ 17821b362b15af34006e6a11974088a46d42b903418eJohann mod_frame_err = calculate_modified_err(cpi, this_frame); 17831b362b15af34006e6a11974088a46d42b903418eJohann 17841b362b15af34006e6a11974088a46d42b903418eJohann /* Note the error of the frame at the start of the group (this will be 17851b362b15af34006e6a11974088a46d42b903418eJohann * the GF frame error if we code a normal gf 17861b362b15af34006e6a11974088a46d42b903418eJohann */ 17871b362b15af34006e6a11974088a46d42b903418eJohann gf_first_frame_err = mod_frame_err; 17881b362b15af34006e6a11974088a46d42b903418eJohann 17891b362b15af34006e6a11974088a46d42b903418eJohann /* Special treatment if the current frame is a key frame (which is also 17901b362b15af34006e6a11974088a46d42b903418eJohann * a gf). If it is then its error score (and hence bit allocation) need 17911b362b15af34006e6a11974088a46d42b903418eJohann * to be subtracted out from the calculation for the GF group 17921b362b15af34006e6a11974088a46d42b903418eJohann */ 17931b362b15af34006e6a11974088a46d42b903418eJohann if (cpi->common.frame_type == KEY_FRAME) 17941b362b15af34006e6a11974088a46d42b903418eJohann gf_group_err -= gf_first_frame_err; 17951b362b15af34006e6a11974088a46d42b903418eJohann 17961b362b15af34006e6a11974088a46d42b903418eJohann /* Scan forward to try and work out how many frames the next gf group 17971b362b15af34006e6a11974088a46d42b903418eJohann * should contain and what level of boost is appropriate for the GF 17981b362b15af34006e6a11974088a46d42b903418eJohann * or ARF that will be coded with the group 17991b362b15af34006e6a11974088a46d42b903418eJohann */ 18001b362b15af34006e6a11974088a46d42b903418eJohann i = 0; 18011b362b15af34006e6a11974088a46d42b903418eJohann 18021b362b15af34006e6a11974088a46d42b903418eJohann while (((i < cpi->twopass.static_scene_max_gf_interval) || 18031b362b15af34006e6a11974088a46d42b903418eJohann ((cpi->twopass.frames_to_key - i) < MIN_GF_INTERVAL)) && 18041b362b15af34006e6a11974088a46d42b903418eJohann (i < cpi->twopass.frames_to_key)) 18051b362b15af34006e6a11974088a46d42b903418eJohann { 18061b362b15af34006e6a11974088a46d42b903418eJohann i++; 18071b362b15af34006e6a11974088a46d42b903418eJohann 18081b362b15af34006e6a11974088a46d42b903418eJohann /* Accumulate error score of frames in this gf group */ 18091b362b15af34006e6a11974088a46d42b903418eJohann mod_frame_err = calculate_modified_err(cpi, this_frame); 18101b362b15af34006e6a11974088a46d42b903418eJohann 18111b362b15af34006e6a11974088a46d42b903418eJohann gf_group_err += mod_frame_err; 18121b362b15af34006e6a11974088a46d42b903418eJohann 18131b362b15af34006e6a11974088a46d42b903418eJohann mod_err_per_mb_accumulator += 18141b362b15af34006e6a11974088a46d42b903418eJohann mod_frame_err / DOUBLE_DIVIDE_CHECK((double)cpi->common.MBs); 18151b362b15af34006e6a11974088a46d42b903418eJohann 18161b362b15af34006e6a11974088a46d42b903418eJohann if (EOF == input_stats(cpi, &next_frame)) 18171b362b15af34006e6a11974088a46d42b903418eJohann break; 18181b362b15af34006e6a11974088a46d42b903418eJohann 18191b362b15af34006e6a11974088a46d42b903418eJohann /* Test for the case where there is a brief flash but the prediction 18201b362b15af34006e6a11974088a46d42b903418eJohann * quality back to an earlier frame is then restored. 18211b362b15af34006e6a11974088a46d42b903418eJohann */ 18221b362b15af34006e6a11974088a46d42b903418eJohann flash_detected = detect_flash(cpi, 0); 18231b362b15af34006e6a11974088a46d42b903418eJohann 18241b362b15af34006e6a11974088a46d42b903418eJohann /* Update the motion related elements to the boost calculation */ 18251b362b15af34006e6a11974088a46d42b903418eJohann accumulate_frame_motion_stats( cpi, &next_frame, 18261b362b15af34006e6a11974088a46d42b903418eJohann &this_frame_mv_in_out, &mv_in_out_accumulator, 18271b362b15af34006e6a11974088a46d42b903418eJohann &abs_mv_in_out_accumulator, &mv_ratio_accumulator ); 18281b362b15af34006e6a11974088a46d42b903418eJohann 18291b362b15af34006e6a11974088a46d42b903418eJohann /* Calculate a baseline boost number for this frame */ 18301b362b15af34006e6a11974088a46d42b903418eJohann r = calc_frame_boost( cpi, &next_frame, this_frame_mv_in_out ); 18311b362b15af34006e6a11974088a46d42b903418eJohann 18321b362b15af34006e6a11974088a46d42b903418eJohann /* Cumulative effect of prediction quality decay */ 18331b362b15af34006e6a11974088a46d42b903418eJohann if ( !flash_detected ) 18341b362b15af34006e6a11974088a46d42b903418eJohann { 18351b362b15af34006e6a11974088a46d42b903418eJohann loop_decay_rate = get_prediction_decay_rate(cpi, &next_frame); 18361b362b15af34006e6a11974088a46d42b903418eJohann decay_accumulator = decay_accumulator * loop_decay_rate; 18371b362b15af34006e6a11974088a46d42b903418eJohann decay_accumulator = 18381b362b15af34006e6a11974088a46d42b903418eJohann decay_accumulator < 0.1 ? 0.1 : decay_accumulator; 18391b362b15af34006e6a11974088a46d42b903418eJohann } 18401b362b15af34006e6a11974088a46d42b903418eJohann boost_score += (decay_accumulator * r); 18411b362b15af34006e6a11974088a46d42b903418eJohann 18421b362b15af34006e6a11974088a46d42b903418eJohann /* Break clause to detect very still sections after motion 18431b362b15af34006e6a11974088a46d42b903418eJohann * For example a staic image after a fade or other transition. 18441b362b15af34006e6a11974088a46d42b903418eJohann */ 18451b362b15af34006e6a11974088a46d42b903418eJohann if ( detect_transition_to_still( cpi, i, 5, 18461b362b15af34006e6a11974088a46d42b903418eJohann loop_decay_rate, 18471b362b15af34006e6a11974088a46d42b903418eJohann decay_accumulator ) ) 18481b362b15af34006e6a11974088a46d42b903418eJohann { 18491b362b15af34006e6a11974088a46d42b903418eJohann allow_alt_ref = 0; 18501b362b15af34006e6a11974088a46d42b903418eJohann boost_score = old_boost_score; 18511b362b15af34006e6a11974088a46d42b903418eJohann break; 18521b362b15af34006e6a11974088a46d42b903418eJohann } 18531b362b15af34006e6a11974088a46d42b903418eJohann 18541b362b15af34006e6a11974088a46d42b903418eJohann /* Break out conditions. */ 18551b362b15af34006e6a11974088a46d42b903418eJohann if ( 18561b362b15af34006e6a11974088a46d42b903418eJohann /* Break at cpi->max_gf_interval unless almost totally static */ 18571b362b15af34006e6a11974088a46d42b903418eJohann (i >= cpi->max_gf_interval && (decay_accumulator < 0.995)) || 18581b362b15af34006e6a11974088a46d42b903418eJohann ( 18591b362b15af34006e6a11974088a46d42b903418eJohann /* Dont break out with a very short interval */ 18601b362b15af34006e6a11974088a46d42b903418eJohann (i > MIN_GF_INTERVAL) && 18611b362b15af34006e6a11974088a46d42b903418eJohann /* Dont break out very close to a key frame */ 18621b362b15af34006e6a11974088a46d42b903418eJohann ((cpi->twopass.frames_to_key - i) >= MIN_GF_INTERVAL) && 18631b362b15af34006e6a11974088a46d42b903418eJohann ((boost_score > 20.0) || (next_frame.pcnt_inter < 0.75)) && 18641b362b15af34006e6a11974088a46d42b903418eJohann (!flash_detected) && 18651b362b15af34006e6a11974088a46d42b903418eJohann ((mv_ratio_accumulator > 100.0) || 18661b362b15af34006e6a11974088a46d42b903418eJohann (abs_mv_in_out_accumulator > 3.0) || 18671b362b15af34006e6a11974088a46d42b903418eJohann (mv_in_out_accumulator < -2.0) || 18681b362b15af34006e6a11974088a46d42b903418eJohann ((boost_score - old_boost_score) < 2.0)) 18691b362b15af34006e6a11974088a46d42b903418eJohann ) ) 18701b362b15af34006e6a11974088a46d42b903418eJohann { 18711b362b15af34006e6a11974088a46d42b903418eJohann boost_score = old_boost_score; 18721b362b15af34006e6a11974088a46d42b903418eJohann break; 18731b362b15af34006e6a11974088a46d42b903418eJohann } 18741b362b15af34006e6a11974088a46d42b903418eJohann 1875da49e34c1fb5e99681f4ad99c21d9cfd83eddb96Vignesh Venkatasubramanian memcpy(this_frame, &next_frame, sizeof(*this_frame)); 18761b362b15af34006e6a11974088a46d42b903418eJohann 18771b362b15af34006e6a11974088a46d42b903418eJohann old_boost_score = boost_score; 18781b362b15af34006e6a11974088a46d42b903418eJohann } 18791b362b15af34006e6a11974088a46d42b903418eJohann 18801b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.gf_decay_rate = 18811b362b15af34006e6a11974088a46d42b903418eJohann (i > 0) ? (int)(100.0 * (1.0 - decay_accumulator)) / i : 0; 18821b362b15af34006e6a11974088a46d42b903418eJohann 18831b362b15af34006e6a11974088a46d42b903418eJohann /* When using CBR apply additional buffer related upper limits */ 18841b362b15af34006e6a11974088a46d42b903418eJohann if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) 18851b362b15af34006e6a11974088a46d42b903418eJohann { 18861b362b15af34006e6a11974088a46d42b903418eJohann double max_boost; 18871b362b15af34006e6a11974088a46d42b903418eJohann 18881b362b15af34006e6a11974088a46d42b903418eJohann /* For cbr apply buffer related limits */ 18891b362b15af34006e6a11974088a46d42b903418eJohann if (cpi->drop_frames_allowed) 18901b362b15af34006e6a11974088a46d42b903418eJohann { 18911b362b15af34006e6a11974088a46d42b903418eJohann int64_t df_buffer_level = cpi->oxcf.drop_frames_water_mark * 18921b362b15af34006e6a11974088a46d42b903418eJohann (cpi->oxcf.optimal_buffer_level / 100); 18931b362b15af34006e6a11974088a46d42b903418eJohann 18941b362b15af34006e6a11974088a46d42b903418eJohann if (cpi->buffer_level > df_buffer_level) 18951b362b15af34006e6a11974088a46d42b903418eJohann max_boost = ((double)((cpi->buffer_level - df_buffer_level) * 2 / 3) * 16.0) / DOUBLE_DIVIDE_CHECK((double)cpi->av_per_frame_bandwidth); 18961b362b15af34006e6a11974088a46d42b903418eJohann else 18971b362b15af34006e6a11974088a46d42b903418eJohann max_boost = 0.0; 18981b362b15af34006e6a11974088a46d42b903418eJohann } 18991b362b15af34006e6a11974088a46d42b903418eJohann else if (cpi->buffer_level > 0) 19001b362b15af34006e6a11974088a46d42b903418eJohann { 19011b362b15af34006e6a11974088a46d42b903418eJohann max_boost = ((double)(cpi->buffer_level * 2 / 3) * 16.0) / DOUBLE_DIVIDE_CHECK((double)cpi->av_per_frame_bandwidth); 19021b362b15af34006e6a11974088a46d42b903418eJohann } 19031b362b15af34006e6a11974088a46d42b903418eJohann else 19041b362b15af34006e6a11974088a46d42b903418eJohann { 19051b362b15af34006e6a11974088a46d42b903418eJohann max_boost = 0.0; 19061b362b15af34006e6a11974088a46d42b903418eJohann } 19071b362b15af34006e6a11974088a46d42b903418eJohann 19081b362b15af34006e6a11974088a46d42b903418eJohann if (boost_score > max_boost) 19091b362b15af34006e6a11974088a46d42b903418eJohann boost_score = max_boost; 19101b362b15af34006e6a11974088a46d42b903418eJohann } 19111b362b15af34006e6a11974088a46d42b903418eJohann 19121b362b15af34006e6a11974088a46d42b903418eJohann /* Dont allow conventional gf too near the next kf */ 19131b362b15af34006e6a11974088a46d42b903418eJohann if ((cpi->twopass.frames_to_key - i) < MIN_GF_INTERVAL) 19141b362b15af34006e6a11974088a46d42b903418eJohann { 19151b362b15af34006e6a11974088a46d42b903418eJohann while (i < cpi->twopass.frames_to_key) 19161b362b15af34006e6a11974088a46d42b903418eJohann { 19171b362b15af34006e6a11974088a46d42b903418eJohann i++; 19181b362b15af34006e6a11974088a46d42b903418eJohann 19191b362b15af34006e6a11974088a46d42b903418eJohann if (EOF == input_stats(cpi, this_frame)) 19201b362b15af34006e6a11974088a46d42b903418eJohann break; 19211b362b15af34006e6a11974088a46d42b903418eJohann 19221b362b15af34006e6a11974088a46d42b903418eJohann if (i < cpi->twopass.frames_to_key) 19231b362b15af34006e6a11974088a46d42b903418eJohann { 19241b362b15af34006e6a11974088a46d42b903418eJohann mod_frame_err = calculate_modified_err(cpi, this_frame); 19251b362b15af34006e6a11974088a46d42b903418eJohann gf_group_err += mod_frame_err; 19261b362b15af34006e6a11974088a46d42b903418eJohann } 19271b362b15af34006e6a11974088a46d42b903418eJohann } 19281b362b15af34006e6a11974088a46d42b903418eJohann } 19291b362b15af34006e6a11974088a46d42b903418eJohann 19301b362b15af34006e6a11974088a46d42b903418eJohann cpi->gfu_boost = (int)(boost_score * 100.0) >> 4; 19311b362b15af34006e6a11974088a46d42b903418eJohann 19321b362b15af34006e6a11974088a46d42b903418eJohann#if NEW_BOOST 19331b362b15af34006e6a11974088a46d42b903418eJohann /* Alterrnative boost calculation for alt ref */ 19341b362b15af34006e6a11974088a46d42b903418eJohann alt_boost = calc_arf_boost( cpi, 0, (i-1), (i-1), &f_boost, &b_boost ); 19351b362b15af34006e6a11974088a46d42b903418eJohann#endif 19361b362b15af34006e6a11974088a46d42b903418eJohann 19371b362b15af34006e6a11974088a46d42b903418eJohann /* Should we use the alternate refernce frame */ 19381b362b15af34006e6a11974088a46d42b903418eJohann if (allow_alt_ref && 19391b362b15af34006e6a11974088a46d42b903418eJohann (i >= MIN_GF_INTERVAL) && 19401b362b15af34006e6a11974088a46d42b903418eJohann /* dont use ARF very near next kf */ 19411b362b15af34006e6a11974088a46d42b903418eJohann (i <= (cpi->twopass.frames_to_key - MIN_GF_INTERVAL)) && 19421b362b15af34006e6a11974088a46d42b903418eJohann#if NEW_BOOST 19431b362b15af34006e6a11974088a46d42b903418eJohann ((next_frame.pcnt_inter > 0.75) || 19441b362b15af34006e6a11974088a46d42b903418eJohann (next_frame.pcnt_second_ref > 0.5)) && 19451b362b15af34006e6a11974088a46d42b903418eJohann ((mv_in_out_accumulator / (double)i > -0.2) || 19461b362b15af34006e6a11974088a46d42b903418eJohann (mv_in_out_accumulator > -2.0)) && 19471b362b15af34006e6a11974088a46d42b903418eJohann (b_boost > 100) && 19481b362b15af34006e6a11974088a46d42b903418eJohann (f_boost > 100) ) 19491b362b15af34006e6a11974088a46d42b903418eJohann#else 19501b362b15af34006e6a11974088a46d42b903418eJohann (next_frame.pcnt_inter > 0.75) && 19511b362b15af34006e6a11974088a46d42b903418eJohann ((mv_in_out_accumulator / (double)i > -0.2) || 19521b362b15af34006e6a11974088a46d42b903418eJohann (mv_in_out_accumulator > -2.0)) && 19531b362b15af34006e6a11974088a46d42b903418eJohann (cpi->gfu_boost > 100) && 19541b362b15af34006e6a11974088a46d42b903418eJohann (cpi->twopass.gf_decay_rate <= 19551b362b15af34006e6a11974088a46d42b903418eJohann (ARF_DECAY_THRESH + (cpi->gfu_boost / 200))) ) 19561b362b15af34006e6a11974088a46d42b903418eJohann#endif 19571b362b15af34006e6a11974088a46d42b903418eJohann { 19581b362b15af34006e6a11974088a46d42b903418eJohann int Boost; 19591b362b15af34006e6a11974088a46d42b903418eJohann int allocation_chunks; 19601b362b15af34006e6a11974088a46d42b903418eJohann int Q = (cpi->oxcf.fixed_q < 0) 19611b362b15af34006e6a11974088a46d42b903418eJohann ? cpi->last_q[INTER_FRAME] : cpi->oxcf.fixed_q; 19621b362b15af34006e6a11974088a46d42b903418eJohann int tmp_q; 19631b362b15af34006e6a11974088a46d42b903418eJohann int arf_frame_bits = 0; 19641b362b15af34006e6a11974088a46d42b903418eJohann int group_bits; 19651b362b15af34006e6a11974088a46d42b903418eJohann 19661b362b15af34006e6a11974088a46d42b903418eJohann#if NEW_BOOST 19671b362b15af34006e6a11974088a46d42b903418eJohann cpi->gfu_boost = alt_boost; 19681b362b15af34006e6a11974088a46d42b903418eJohann#endif 19691b362b15af34006e6a11974088a46d42b903418eJohann 19701b362b15af34006e6a11974088a46d42b903418eJohann /* Estimate the bits to be allocated to the group as a whole */ 19711b362b15af34006e6a11974088a46d42b903418eJohann if ((cpi->twopass.kf_group_bits > 0) && 19721b362b15af34006e6a11974088a46d42b903418eJohann (cpi->twopass.kf_group_error_left > 0)) 19731b362b15af34006e6a11974088a46d42b903418eJohann { 19741b362b15af34006e6a11974088a46d42b903418eJohann group_bits = (int)((double)cpi->twopass.kf_group_bits * 19751b362b15af34006e6a11974088a46d42b903418eJohann (gf_group_err / (double)cpi->twopass.kf_group_error_left)); 19761b362b15af34006e6a11974088a46d42b903418eJohann } 19771b362b15af34006e6a11974088a46d42b903418eJohann else 19781b362b15af34006e6a11974088a46d42b903418eJohann group_bits = 0; 19791b362b15af34006e6a11974088a46d42b903418eJohann 19801b362b15af34006e6a11974088a46d42b903418eJohann /* Boost for arf frame */ 19811b362b15af34006e6a11974088a46d42b903418eJohann#if NEW_BOOST 19821b362b15af34006e6a11974088a46d42b903418eJohann Boost = (alt_boost * GFQ_ADJUSTMENT) / 100; 19831b362b15af34006e6a11974088a46d42b903418eJohann#else 19841b362b15af34006e6a11974088a46d42b903418eJohann Boost = (cpi->gfu_boost * 3 * GFQ_ADJUSTMENT) / (2 * 100); 19851b362b15af34006e6a11974088a46d42b903418eJohann#endif 19861b362b15af34006e6a11974088a46d42b903418eJohann Boost += (i * 50); 19871b362b15af34006e6a11974088a46d42b903418eJohann 19881b362b15af34006e6a11974088a46d42b903418eJohann /* Set max and minimum boost and hence minimum allocation */ 19891b362b15af34006e6a11974088a46d42b903418eJohann if (Boost > ((cpi->baseline_gf_interval + 1) * 200)) 19901b362b15af34006e6a11974088a46d42b903418eJohann Boost = ((cpi->baseline_gf_interval + 1) * 200); 19911b362b15af34006e6a11974088a46d42b903418eJohann else if (Boost < 125) 19921b362b15af34006e6a11974088a46d42b903418eJohann Boost = 125; 19931b362b15af34006e6a11974088a46d42b903418eJohann 19941b362b15af34006e6a11974088a46d42b903418eJohann allocation_chunks = (i * 100) + Boost; 19951b362b15af34006e6a11974088a46d42b903418eJohann 19961b362b15af34006e6a11974088a46d42b903418eJohann /* Normalize Altboost and allocations chunck down to prevent overflow */ 19971b362b15af34006e6a11974088a46d42b903418eJohann while (Boost > 1000) 19981b362b15af34006e6a11974088a46d42b903418eJohann { 19991b362b15af34006e6a11974088a46d42b903418eJohann Boost /= 2; 20001b362b15af34006e6a11974088a46d42b903418eJohann allocation_chunks /= 2; 20011b362b15af34006e6a11974088a46d42b903418eJohann } 20021b362b15af34006e6a11974088a46d42b903418eJohann 20031b362b15af34006e6a11974088a46d42b903418eJohann /* Calculate the number of bits to be spent on the arf based on the 20041b362b15af34006e6a11974088a46d42b903418eJohann * boost number 20051b362b15af34006e6a11974088a46d42b903418eJohann */ 20061b362b15af34006e6a11974088a46d42b903418eJohann arf_frame_bits = (int)((double)Boost * (group_bits / 20071b362b15af34006e6a11974088a46d42b903418eJohann (double)allocation_chunks)); 20081b362b15af34006e6a11974088a46d42b903418eJohann 20091b362b15af34006e6a11974088a46d42b903418eJohann /* Estimate if there are enough bits available to make worthwhile use 20101b362b15af34006e6a11974088a46d42b903418eJohann * of an arf. 20111b362b15af34006e6a11974088a46d42b903418eJohann */ 20121b362b15af34006e6a11974088a46d42b903418eJohann tmp_q = estimate_q(cpi, mod_frame_err, (int)arf_frame_bits); 20131b362b15af34006e6a11974088a46d42b903418eJohann 20141b362b15af34006e6a11974088a46d42b903418eJohann /* Only use an arf if it is likely we will be able to code 20151b362b15af34006e6a11974088a46d42b903418eJohann * it at a lower Q than the surrounding frames. 20161b362b15af34006e6a11974088a46d42b903418eJohann */ 20171b362b15af34006e6a11974088a46d42b903418eJohann if (tmp_q < cpi->worst_quality) 20181b362b15af34006e6a11974088a46d42b903418eJohann { 20191b362b15af34006e6a11974088a46d42b903418eJohann int half_gf_int; 20201b362b15af34006e6a11974088a46d42b903418eJohann int frames_after_arf; 20211b362b15af34006e6a11974088a46d42b903418eJohann int frames_bwd = cpi->oxcf.arnr_max_frames - 1; 20221b362b15af34006e6a11974088a46d42b903418eJohann int frames_fwd = cpi->oxcf.arnr_max_frames - 1; 20231b362b15af34006e6a11974088a46d42b903418eJohann 20241b362b15af34006e6a11974088a46d42b903418eJohann cpi->source_alt_ref_pending = 1; 20251b362b15af34006e6a11974088a46d42b903418eJohann 20261b362b15af34006e6a11974088a46d42b903418eJohann /* 20271b362b15af34006e6a11974088a46d42b903418eJohann * For alt ref frames the error score for the end frame of the 20281b362b15af34006e6a11974088a46d42b903418eJohann * group (the alt ref frame) should not contribute to the group 20291b362b15af34006e6a11974088a46d42b903418eJohann * total and hence the number of bit allocated to the group. 20301b362b15af34006e6a11974088a46d42b903418eJohann * Rather it forms part of the next group (it is the GF at the 20311b362b15af34006e6a11974088a46d42b903418eJohann * start of the next group) 20321b362b15af34006e6a11974088a46d42b903418eJohann * gf_group_err -= mod_frame_err; 20331b362b15af34006e6a11974088a46d42b903418eJohann * 20341b362b15af34006e6a11974088a46d42b903418eJohann * For alt ref frames alt ref frame is technically part of the 20351b362b15af34006e6a11974088a46d42b903418eJohann * GF frame for the next group but we always base the error 20361b362b15af34006e6a11974088a46d42b903418eJohann * calculation and bit allocation on the current group of frames. 20371b362b15af34006e6a11974088a46d42b903418eJohann * 20381b362b15af34006e6a11974088a46d42b903418eJohann * Set the interval till the next gf or arf. 20391b362b15af34006e6a11974088a46d42b903418eJohann * For ARFs this is the number of frames to be coded before the 20401b362b15af34006e6a11974088a46d42b903418eJohann * future frame that is coded as an ARF. 20411b362b15af34006e6a11974088a46d42b903418eJohann * The future frame itself is part of the next group 20421b362b15af34006e6a11974088a46d42b903418eJohann */ 20431b362b15af34006e6a11974088a46d42b903418eJohann cpi->baseline_gf_interval = i; 20441b362b15af34006e6a11974088a46d42b903418eJohann 20451b362b15af34006e6a11974088a46d42b903418eJohann /* 20461b362b15af34006e6a11974088a46d42b903418eJohann * Define the arnr filter width for this group of frames: 20471b362b15af34006e6a11974088a46d42b903418eJohann * We only filter frames that lie within a distance of half 20481b362b15af34006e6a11974088a46d42b903418eJohann * the GF interval from the ARF frame. We also have to trap 20491b362b15af34006e6a11974088a46d42b903418eJohann * cases where the filter extends beyond the end of clip. 20501b362b15af34006e6a11974088a46d42b903418eJohann * Note: this_frame->frame has been updated in the loop 20511b362b15af34006e6a11974088a46d42b903418eJohann * so it now points at the ARF frame. 20521b362b15af34006e6a11974088a46d42b903418eJohann */ 20531b362b15af34006e6a11974088a46d42b903418eJohann half_gf_int = cpi->baseline_gf_interval >> 1; 20541b362b15af34006e6a11974088a46d42b903418eJohann frames_after_arf = (int)(cpi->twopass.total_stats.count - 20551b362b15af34006e6a11974088a46d42b903418eJohann this_frame->frame - 1); 20561b362b15af34006e6a11974088a46d42b903418eJohann 20571b362b15af34006e6a11974088a46d42b903418eJohann switch (cpi->oxcf.arnr_type) 20581b362b15af34006e6a11974088a46d42b903418eJohann { 20591b362b15af34006e6a11974088a46d42b903418eJohann case 1: /* Backward filter */ 20601b362b15af34006e6a11974088a46d42b903418eJohann frames_fwd = 0; 20611b362b15af34006e6a11974088a46d42b903418eJohann if (frames_bwd > half_gf_int) 20621b362b15af34006e6a11974088a46d42b903418eJohann frames_bwd = half_gf_int; 20631b362b15af34006e6a11974088a46d42b903418eJohann break; 20641b362b15af34006e6a11974088a46d42b903418eJohann 20651b362b15af34006e6a11974088a46d42b903418eJohann case 2: /* Forward filter */ 20661b362b15af34006e6a11974088a46d42b903418eJohann if (frames_fwd > half_gf_int) 20671b362b15af34006e6a11974088a46d42b903418eJohann frames_fwd = half_gf_int; 20681b362b15af34006e6a11974088a46d42b903418eJohann if (frames_fwd > frames_after_arf) 20691b362b15af34006e6a11974088a46d42b903418eJohann frames_fwd = frames_after_arf; 20701b362b15af34006e6a11974088a46d42b903418eJohann frames_bwd = 0; 20711b362b15af34006e6a11974088a46d42b903418eJohann break; 20721b362b15af34006e6a11974088a46d42b903418eJohann 20731b362b15af34006e6a11974088a46d42b903418eJohann case 3: /* Centered filter */ 20741b362b15af34006e6a11974088a46d42b903418eJohann default: 20751b362b15af34006e6a11974088a46d42b903418eJohann frames_fwd >>= 1; 20761b362b15af34006e6a11974088a46d42b903418eJohann if (frames_fwd > frames_after_arf) 20771b362b15af34006e6a11974088a46d42b903418eJohann frames_fwd = frames_after_arf; 20781b362b15af34006e6a11974088a46d42b903418eJohann if (frames_fwd > half_gf_int) 20791b362b15af34006e6a11974088a46d42b903418eJohann frames_fwd = half_gf_int; 20801b362b15af34006e6a11974088a46d42b903418eJohann 20811b362b15af34006e6a11974088a46d42b903418eJohann frames_bwd = frames_fwd; 20821b362b15af34006e6a11974088a46d42b903418eJohann 20831b362b15af34006e6a11974088a46d42b903418eJohann /* For even length filter there is one more frame backward 20841b362b15af34006e6a11974088a46d42b903418eJohann * than forward: e.g. len=6 ==> bbbAff, len=7 ==> bbbAfff. 20851b362b15af34006e6a11974088a46d42b903418eJohann */ 20861b362b15af34006e6a11974088a46d42b903418eJohann if (frames_bwd < half_gf_int) 20871b362b15af34006e6a11974088a46d42b903418eJohann frames_bwd += (cpi->oxcf.arnr_max_frames+1) & 0x1; 20881b362b15af34006e6a11974088a46d42b903418eJohann break; 20891b362b15af34006e6a11974088a46d42b903418eJohann } 20901b362b15af34006e6a11974088a46d42b903418eJohann 20911b362b15af34006e6a11974088a46d42b903418eJohann cpi->active_arnr_frames = frames_bwd + 1 + frames_fwd; 20921b362b15af34006e6a11974088a46d42b903418eJohann } 20931b362b15af34006e6a11974088a46d42b903418eJohann else 20941b362b15af34006e6a11974088a46d42b903418eJohann { 20951b362b15af34006e6a11974088a46d42b903418eJohann cpi->source_alt_ref_pending = 0; 20961b362b15af34006e6a11974088a46d42b903418eJohann cpi->baseline_gf_interval = i; 20971b362b15af34006e6a11974088a46d42b903418eJohann } 20981b362b15af34006e6a11974088a46d42b903418eJohann } 20991b362b15af34006e6a11974088a46d42b903418eJohann else 21001b362b15af34006e6a11974088a46d42b903418eJohann { 21011b362b15af34006e6a11974088a46d42b903418eJohann cpi->source_alt_ref_pending = 0; 21021b362b15af34006e6a11974088a46d42b903418eJohann cpi->baseline_gf_interval = i; 21031b362b15af34006e6a11974088a46d42b903418eJohann } 21041b362b15af34006e6a11974088a46d42b903418eJohann 21051b362b15af34006e6a11974088a46d42b903418eJohann /* 21061b362b15af34006e6a11974088a46d42b903418eJohann * Now decide how many bits should be allocated to the GF group as a 21071b362b15af34006e6a11974088a46d42b903418eJohann * proportion of those remaining in the kf group. 21081b362b15af34006e6a11974088a46d42b903418eJohann * The final key frame group in the clip is treated as a special case 21091b362b15af34006e6a11974088a46d42b903418eJohann * where cpi->twopass.kf_group_bits is tied to cpi->twopass.bits_left. 21101b362b15af34006e6a11974088a46d42b903418eJohann * This is also important for short clips where there may only be one 21111b362b15af34006e6a11974088a46d42b903418eJohann * key frame. 21121b362b15af34006e6a11974088a46d42b903418eJohann */ 21131b362b15af34006e6a11974088a46d42b903418eJohann if (cpi->twopass.frames_to_key >= (int)(cpi->twopass.total_stats.count - 21141b362b15af34006e6a11974088a46d42b903418eJohann cpi->common.current_video_frame)) 21151b362b15af34006e6a11974088a46d42b903418eJohann { 21161b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.kf_group_bits = 21171b362b15af34006e6a11974088a46d42b903418eJohann (cpi->twopass.bits_left > 0) ? cpi->twopass.bits_left : 0; 21181b362b15af34006e6a11974088a46d42b903418eJohann } 21191b362b15af34006e6a11974088a46d42b903418eJohann 21201b362b15af34006e6a11974088a46d42b903418eJohann /* Calculate the bits to be allocated to the group as a whole */ 21211b362b15af34006e6a11974088a46d42b903418eJohann if ((cpi->twopass.kf_group_bits > 0) && 21221b362b15af34006e6a11974088a46d42b903418eJohann (cpi->twopass.kf_group_error_left > 0)) 21231b362b15af34006e6a11974088a46d42b903418eJohann { 21241b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.gf_group_bits = 2125ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang (int64_t)(cpi->twopass.kf_group_bits * 2126ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang (gf_group_err / cpi->twopass.kf_group_error_left)); 21271b362b15af34006e6a11974088a46d42b903418eJohann } 21281b362b15af34006e6a11974088a46d42b903418eJohann else 21291b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.gf_group_bits = 0; 21301b362b15af34006e6a11974088a46d42b903418eJohann 2131ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang cpi->twopass.gf_group_bits = 21321b362b15af34006e6a11974088a46d42b903418eJohann (cpi->twopass.gf_group_bits < 0) 21331b362b15af34006e6a11974088a46d42b903418eJohann ? 0 21341b362b15af34006e6a11974088a46d42b903418eJohann : (cpi->twopass.gf_group_bits > cpi->twopass.kf_group_bits) 2135ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang ? cpi->twopass.kf_group_bits : cpi->twopass.gf_group_bits; 21361b362b15af34006e6a11974088a46d42b903418eJohann 21371b362b15af34006e6a11974088a46d42b903418eJohann /* Clip cpi->twopass.gf_group_bits based on user supplied data rate 21381b362b15af34006e6a11974088a46d42b903418eJohann * variability limit (cpi->oxcf.two_pass_vbrmax_section) 21391b362b15af34006e6a11974088a46d42b903418eJohann */ 2140ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang if (cpi->twopass.gf_group_bits > 2141ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang (int64_t)max_bits * cpi->baseline_gf_interval) 2142ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang cpi->twopass.gf_group_bits = 2143ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang (int64_t)max_bits * cpi->baseline_gf_interval; 21441b362b15af34006e6a11974088a46d42b903418eJohann 21451b362b15af34006e6a11974088a46d42b903418eJohann /* Reset the file position */ 21461b362b15af34006e6a11974088a46d42b903418eJohann reset_fpf_position(cpi, start_pos); 21471b362b15af34006e6a11974088a46d42b903418eJohann 21481b362b15af34006e6a11974088a46d42b903418eJohann /* Update the record of error used so far (only done once per gf group) */ 21491b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.modified_error_used += gf_group_err; 21501b362b15af34006e6a11974088a46d42b903418eJohann 21511b362b15af34006e6a11974088a46d42b903418eJohann /* Assign bits to the arf or gf. */ 21521b362b15af34006e6a11974088a46d42b903418eJohann for (i = 0; i <= (cpi->source_alt_ref_pending && cpi->common.frame_type != KEY_FRAME); i++) { 21531b362b15af34006e6a11974088a46d42b903418eJohann int Boost; 21541b362b15af34006e6a11974088a46d42b903418eJohann int allocation_chunks; 21551b362b15af34006e6a11974088a46d42b903418eJohann int Q = (cpi->oxcf.fixed_q < 0) ? cpi->last_q[INTER_FRAME] : cpi->oxcf.fixed_q; 21561b362b15af34006e6a11974088a46d42b903418eJohann int gf_bits; 21571b362b15af34006e6a11974088a46d42b903418eJohann 21581b362b15af34006e6a11974088a46d42b903418eJohann /* For ARF frames */ 21591b362b15af34006e6a11974088a46d42b903418eJohann if (cpi->source_alt_ref_pending && i == 0) 21601b362b15af34006e6a11974088a46d42b903418eJohann { 21611b362b15af34006e6a11974088a46d42b903418eJohann#if NEW_BOOST 21621b362b15af34006e6a11974088a46d42b903418eJohann Boost = (alt_boost * GFQ_ADJUSTMENT) / 100; 21631b362b15af34006e6a11974088a46d42b903418eJohann#else 21641b362b15af34006e6a11974088a46d42b903418eJohann Boost = (cpi->gfu_boost * 3 * GFQ_ADJUSTMENT) / (2 * 100); 21651b362b15af34006e6a11974088a46d42b903418eJohann#endif 21661b362b15af34006e6a11974088a46d42b903418eJohann Boost += (cpi->baseline_gf_interval * 50); 21671b362b15af34006e6a11974088a46d42b903418eJohann 21681b362b15af34006e6a11974088a46d42b903418eJohann /* Set max and minimum boost and hence minimum allocation */ 21691b362b15af34006e6a11974088a46d42b903418eJohann if (Boost > ((cpi->baseline_gf_interval + 1) * 200)) 21701b362b15af34006e6a11974088a46d42b903418eJohann Boost = ((cpi->baseline_gf_interval + 1) * 200); 21711b362b15af34006e6a11974088a46d42b903418eJohann else if (Boost < 125) 21721b362b15af34006e6a11974088a46d42b903418eJohann Boost = 125; 21731b362b15af34006e6a11974088a46d42b903418eJohann 21741b362b15af34006e6a11974088a46d42b903418eJohann allocation_chunks = 21751b362b15af34006e6a11974088a46d42b903418eJohann ((cpi->baseline_gf_interval + 1) * 100) + Boost; 21761b362b15af34006e6a11974088a46d42b903418eJohann } 21771b362b15af34006e6a11974088a46d42b903418eJohann /* Else for standard golden frames */ 21781b362b15af34006e6a11974088a46d42b903418eJohann else 21791b362b15af34006e6a11974088a46d42b903418eJohann { 21801b362b15af34006e6a11974088a46d42b903418eJohann /* boost based on inter / intra ratio of subsequent frames */ 21811b362b15af34006e6a11974088a46d42b903418eJohann Boost = (cpi->gfu_boost * GFQ_ADJUSTMENT) / 100; 21821b362b15af34006e6a11974088a46d42b903418eJohann 21831b362b15af34006e6a11974088a46d42b903418eJohann /* Set max and minimum boost and hence minimum allocation */ 21841b362b15af34006e6a11974088a46d42b903418eJohann if (Boost > (cpi->baseline_gf_interval * 150)) 21851b362b15af34006e6a11974088a46d42b903418eJohann Boost = (cpi->baseline_gf_interval * 150); 21861b362b15af34006e6a11974088a46d42b903418eJohann else if (Boost < 125) 21871b362b15af34006e6a11974088a46d42b903418eJohann Boost = 125; 21881b362b15af34006e6a11974088a46d42b903418eJohann 21891b362b15af34006e6a11974088a46d42b903418eJohann allocation_chunks = 21901b362b15af34006e6a11974088a46d42b903418eJohann (cpi->baseline_gf_interval * 100) + (Boost - 100); 21911b362b15af34006e6a11974088a46d42b903418eJohann } 21921b362b15af34006e6a11974088a46d42b903418eJohann 21931b362b15af34006e6a11974088a46d42b903418eJohann /* Normalize Altboost and allocations chunck down to prevent overflow */ 21941b362b15af34006e6a11974088a46d42b903418eJohann while (Boost > 1000) 21951b362b15af34006e6a11974088a46d42b903418eJohann { 21961b362b15af34006e6a11974088a46d42b903418eJohann Boost /= 2; 21971b362b15af34006e6a11974088a46d42b903418eJohann allocation_chunks /= 2; 21981b362b15af34006e6a11974088a46d42b903418eJohann } 21991b362b15af34006e6a11974088a46d42b903418eJohann 22001b362b15af34006e6a11974088a46d42b903418eJohann /* Calculate the number of bits to be spent on the gf or arf based on 22011b362b15af34006e6a11974088a46d42b903418eJohann * the boost number 22021b362b15af34006e6a11974088a46d42b903418eJohann */ 22031b362b15af34006e6a11974088a46d42b903418eJohann gf_bits = (int)((double)Boost * 22041b362b15af34006e6a11974088a46d42b903418eJohann (cpi->twopass.gf_group_bits / 22051b362b15af34006e6a11974088a46d42b903418eJohann (double)allocation_chunks)); 22061b362b15af34006e6a11974088a46d42b903418eJohann 22071b362b15af34006e6a11974088a46d42b903418eJohann /* If the frame that is to be boosted is simpler than the average for 22081b362b15af34006e6a11974088a46d42b903418eJohann * the gf/arf group then use an alternative calculation 22091b362b15af34006e6a11974088a46d42b903418eJohann * based on the error score of the frame itself 22101b362b15af34006e6a11974088a46d42b903418eJohann */ 22111b362b15af34006e6a11974088a46d42b903418eJohann if (mod_frame_err < gf_group_err / (double)cpi->baseline_gf_interval) 22121b362b15af34006e6a11974088a46d42b903418eJohann { 22131b362b15af34006e6a11974088a46d42b903418eJohann double alt_gf_grp_bits; 22141b362b15af34006e6a11974088a46d42b903418eJohann int alt_gf_bits; 22151b362b15af34006e6a11974088a46d42b903418eJohann 22161b362b15af34006e6a11974088a46d42b903418eJohann alt_gf_grp_bits = 22171b362b15af34006e6a11974088a46d42b903418eJohann (double)cpi->twopass.kf_group_bits * 22181b362b15af34006e6a11974088a46d42b903418eJohann (mod_frame_err * (double)cpi->baseline_gf_interval) / 22191b362b15af34006e6a11974088a46d42b903418eJohann DOUBLE_DIVIDE_CHECK((double)cpi->twopass.kf_group_error_left); 22201b362b15af34006e6a11974088a46d42b903418eJohann 22211b362b15af34006e6a11974088a46d42b903418eJohann alt_gf_bits = (int)((double)Boost * (alt_gf_grp_bits / 22221b362b15af34006e6a11974088a46d42b903418eJohann (double)allocation_chunks)); 22231b362b15af34006e6a11974088a46d42b903418eJohann 22241b362b15af34006e6a11974088a46d42b903418eJohann if (gf_bits > alt_gf_bits) 22251b362b15af34006e6a11974088a46d42b903418eJohann { 22261b362b15af34006e6a11974088a46d42b903418eJohann gf_bits = alt_gf_bits; 22271b362b15af34006e6a11974088a46d42b903418eJohann } 22281b362b15af34006e6a11974088a46d42b903418eJohann } 22291b362b15af34006e6a11974088a46d42b903418eJohann /* Else if it is harder than other frames in the group make sure it at 22301b362b15af34006e6a11974088a46d42b903418eJohann * least receives an allocation in keeping with its relative error 22311b362b15af34006e6a11974088a46d42b903418eJohann * score, otherwise it may be worse off than an "un-boosted" frame 22321b362b15af34006e6a11974088a46d42b903418eJohann */ 22331b362b15af34006e6a11974088a46d42b903418eJohann else 22341b362b15af34006e6a11974088a46d42b903418eJohann { 22351b362b15af34006e6a11974088a46d42b903418eJohann int alt_gf_bits = 22361b362b15af34006e6a11974088a46d42b903418eJohann (int)((double)cpi->twopass.kf_group_bits * 22371b362b15af34006e6a11974088a46d42b903418eJohann mod_frame_err / 22381b362b15af34006e6a11974088a46d42b903418eJohann DOUBLE_DIVIDE_CHECK((double)cpi->twopass.kf_group_error_left)); 22391b362b15af34006e6a11974088a46d42b903418eJohann 22401b362b15af34006e6a11974088a46d42b903418eJohann if (alt_gf_bits > gf_bits) 22411b362b15af34006e6a11974088a46d42b903418eJohann { 22421b362b15af34006e6a11974088a46d42b903418eJohann gf_bits = alt_gf_bits; 22431b362b15af34006e6a11974088a46d42b903418eJohann } 22441b362b15af34006e6a11974088a46d42b903418eJohann } 22451b362b15af34006e6a11974088a46d42b903418eJohann 22461b362b15af34006e6a11974088a46d42b903418eJohann /* Apply an additional limit for CBR */ 22471b362b15af34006e6a11974088a46d42b903418eJohann if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) 22481b362b15af34006e6a11974088a46d42b903418eJohann { 22491b362b15af34006e6a11974088a46d42b903418eJohann if (cpi->twopass.gf_bits > (int)(cpi->buffer_level >> 1)) 22501b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.gf_bits = (int)(cpi->buffer_level >> 1); 22511b362b15af34006e6a11974088a46d42b903418eJohann } 22521b362b15af34006e6a11974088a46d42b903418eJohann 22531b362b15af34006e6a11974088a46d42b903418eJohann /* Dont allow a negative value for gf_bits */ 22541b362b15af34006e6a11974088a46d42b903418eJohann if (gf_bits < 0) 22551b362b15af34006e6a11974088a46d42b903418eJohann gf_bits = 0; 22561b362b15af34006e6a11974088a46d42b903418eJohann 22571b362b15af34006e6a11974088a46d42b903418eJohann /* Add in minimum for a frame */ 22581b362b15af34006e6a11974088a46d42b903418eJohann gf_bits += cpi->min_frame_bandwidth; 22591b362b15af34006e6a11974088a46d42b903418eJohann 22601b362b15af34006e6a11974088a46d42b903418eJohann if (i == 0) 22611b362b15af34006e6a11974088a46d42b903418eJohann { 22621b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.gf_bits = gf_bits; 22631b362b15af34006e6a11974088a46d42b903418eJohann } 22641b362b15af34006e6a11974088a46d42b903418eJohann if (i == 1 || (!cpi->source_alt_ref_pending && (cpi->common.frame_type != KEY_FRAME))) 22651b362b15af34006e6a11974088a46d42b903418eJohann { 22661b362b15af34006e6a11974088a46d42b903418eJohann /* Per frame bit target for this frame */ 22671b362b15af34006e6a11974088a46d42b903418eJohann cpi->per_frame_bandwidth = gf_bits; 22681b362b15af34006e6a11974088a46d42b903418eJohann } 22691b362b15af34006e6a11974088a46d42b903418eJohann } 22701b362b15af34006e6a11974088a46d42b903418eJohann 22711b362b15af34006e6a11974088a46d42b903418eJohann { 22721b362b15af34006e6a11974088a46d42b903418eJohann /* Adjust KF group bits and error remainin */ 22731b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.kf_group_error_left -= (int64_t)gf_group_err; 22741b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.kf_group_bits -= cpi->twopass.gf_group_bits; 22751b362b15af34006e6a11974088a46d42b903418eJohann 22761b362b15af34006e6a11974088a46d42b903418eJohann if (cpi->twopass.kf_group_bits < 0) 22771b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.kf_group_bits = 0; 22781b362b15af34006e6a11974088a46d42b903418eJohann 22791b362b15af34006e6a11974088a46d42b903418eJohann /* Note the error score left in the remaining frames of the group. 22801b362b15af34006e6a11974088a46d42b903418eJohann * For normal GFs we want to remove the error score for the first 22811b362b15af34006e6a11974088a46d42b903418eJohann * frame of the group (except in Key frame case where this has 22821b362b15af34006e6a11974088a46d42b903418eJohann * already happened) 22831b362b15af34006e6a11974088a46d42b903418eJohann */ 22841b362b15af34006e6a11974088a46d42b903418eJohann if (!cpi->source_alt_ref_pending && cpi->common.frame_type != KEY_FRAME) 22851b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.gf_group_error_left = (int)(gf_group_err - 22861b362b15af34006e6a11974088a46d42b903418eJohann gf_first_frame_err); 22871b362b15af34006e6a11974088a46d42b903418eJohann else 22881b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.gf_group_error_left = (int) gf_group_err; 22891b362b15af34006e6a11974088a46d42b903418eJohann 22901b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.gf_group_bits -= cpi->twopass.gf_bits - cpi->min_frame_bandwidth; 22911b362b15af34006e6a11974088a46d42b903418eJohann 22921b362b15af34006e6a11974088a46d42b903418eJohann if (cpi->twopass.gf_group_bits < 0) 22931b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.gf_group_bits = 0; 22941b362b15af34006e6a11974088a46d42b903418eJohann 22951b362b15af34006e6a11974088a46d42b903418eJohann /* This condition could fail if there are two kfs very close together 22961b362b15af34006e6a11974088a46d42b903418eJohann * despite (MIN_GF_INTERVAL) and would cause a devide by 0 in the 22971b362b15af34006e6a11974088a46d42b903418eJohann * calculation of cpi->twopass.alt_extra_bits. 22981b362b15af34006e6a11974088a46d42b903418eJohann */ 22991b362b15af34006e6a11974088a46d42b903418eJohann if ( cpi->baseline_gf_interval >= 3 ) 23001b362b15af34006e6a11974088a46d42b903418eJohann { 23011b362b15af34006e6a11974088a46d42b903418eJohann#if NEW_BOOST 23021b362b15af34006e6a11974088a46d42b903418eJohann int boost = (cpi->source_alt_ref_pending) 23031b362b15af34006e6a11974088a46d42b903418eJohann ? b_boost : cpi->gfu_boost; 23041b362b15af34006e6a11974088a46d42b903418eJohann#else 23051b362b15af34006e6a11974088a46d42b903418eJohann int boost = cpi->gfu_boost; 23061b362b15af34006e6a11974088a46d42b903418eJohann#endif 23071b362b15af34006e6a11974088a46d42b903418eJohann if ( boost >= 150 ) 23081b362b15af34006e6a11974088a46d42b903418eJohann { 23091b362b15af34006e6a11974088a46d42b903418eJohann int pct_extra; 23101b362b15af34006e6a11974088a46d42b903418eJohann 23111b362b15af34006e6a11974088a46d42b903418eJohann pct_extra = (boost - 100) / 50; 23121b362b15af34006e6a11974088a46d42b903418eJohann pct_extra = (pct_extra > 20) ? 20 : pct_extra; 23131b362b15af34006e6a11974088a46d42b903418eJohann 23141b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.alt_extra_bits = 2315b08e2e23eec181e9951df33cd704ac294c5407b6Vignesh Venkatasubramanian (int)(cpi->twopass.gf_group_bits * pct_extra) / 100; 23161b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.gf_group_bits -= cpi->twopass.alt_extra_bits; 23171b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.alt_extra_bits /= 23181b362b15af34006e6a11974088a46d42b903418eJohann ((cpi->baseline_gf_interval-1)>>1); 23191b362b15af34006e6a11974088a46d42b903418eJohann } 23201b362b15af34006e6a11974088a46d42b903418eJohann else 23211b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.alt_extra_bits = 0; 23221b362b15af34006e6a11974088a46d42b903418eJohann } 23231b362b15af34006e6a11974088a46d42b903418eJohann else 23241b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.alt_extra_bits = 0; 23251b362b15af34006e6a11974088a46d42b903418eJohann } 23261b362b15af34006e6a11974088a46d42b903418eJohann 23271b362b15af34006e6a11974088a46d42b903418eJohann /* Adjustments based on a measure of complexity of the section */ 23281b362b15af34006e6a11974088a46d42b903418eJohann if (cpi->common.frame_type != KEY_FRAME) 23291b362b15af34006e6a11974088a46d42b903418eJohann { 23301b362b15af34006e6a11974088a46d42b903418eJohann FIRSTPASS_STATS sectionstats; 23311b362b15af34006e6a11974088a46d42b903418eJohann double Ratio; 23321b362b15af34006e6a11974088a46d42b903418eJohann 23331b362b15af34006e6a11974088a46d42b903418eJohann zero_stats(§ionstats); 23341b362b15af34006e6a11974088a46d42b903418eJohann reset_fpf_position(cpi, start_pos); 23351b362b15af34006e6a11974088a46d42b903418eJohann 23361b362b15af34006e6a11974088a46d42b903418eJohann for (i = 0 ; i < cpi->baseline_gf_interval ; i++) 23371b362b15af34006e6a11974088a46d42b903418eJohann { 23381b362b15af34006e6a11974088a46d42b903418eJohann input_stats(cpi, &next_frame); 23391b362b15af34006e6a11974088a46d42b903418eJohann accumulate_stats(§ionstats, &next_frame); 23401b362b15af34006e6a11974088a46d42b903418eJohann } 23411b362b15af34006e6a11974088a46d42b903418eJohann 23421b362b15af34006e6a11974088a46d42b903418eJohann avg_stats(§ionstats); 23431b362b15af34006e6a11974088a46d42b903418eJohann 23441b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.section_intra_rating = (unsigned int) 23451b362b15af34006e6a11974088a46d42b903418eJohann (sectionstats.intra_error / 23461b362b15af34006e6a11974088a46d42b903418eJohann DOUBLE_DIVIDE_CHECK(sectionstats.coded_error)); 23471b362b15af34006e6a11974088a46d42b903418eJohann 23481b362b15af34006e6a11974088a46d42b903418eJohann Ratio = sectionstats.intra_error / DOUBLE_DIVIDE_CHECK(sectionstats.coded_error); 23491b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.section_max_qfactor = 1.0 - ((Ratio - 10.0) * 0.025); 23501b362b15af34006e6a11974088a46d42b903418eJohann 23511b362b15af34006e6a11974088a46d42b903418eJohann if (cpi->twopass.section_max_qfactor < 0.80) 23521b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.section_max_qfactor = 0.80; 23531b362b15af34006e6a11974088a46d42b903418eJohann 23541b362b15af34006e6a11974088a46d42b903418eJohann reset_fpf_position(cpi, start_pos); 23551b362b15af34006e6a11974088a46d42b903418eJohann } 23561b362b15af34006e6a11974088a46d42b903418eJohann} 23571b362b15af34006e6a11974088a46d42b903418eJohann 23581b362b15af34006e6a11974088a46d42b903418eJohann/* Allocate bits to a normal frame that is neither a gf an arf or a key frame. */ 23591b362b15af34006e6a11974088a46d42b903418eJohannstatic void assign_std_frame_bits(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame) 23601b362b15af34006e6a11974088a46d42b903418eJohann{ 23611b362b15af34006e6a11974088a46d42b903418eJohann int target_frame_size; 23621b362b15af34006e6a11974088a46d42b903418eJohann 23631b362b15af34006e6a11974088a46d42b903418eJohann double modified_err; 23641b362b15af34006e6a11974088a46d42b903418eJohann double err_fraction; 23651b362b15af34006e6a11974088a46d42b903418eJohann 23661b362b15af34006e6a11974088a46d42b903418eJohann int max_bits = frame_max_bits(cpi); /* Max for a single frame */ 23671b362b15af34006e6a11974088a46d42b903418eJohann 23681b362b15af34006e6a11974088a46d42b903418eJohann /* Calculate modified prediction error used in bit allocation */ 23691b362b15af34006e6a11974088a46d42b903418eJohann modified_err = calculate_modified_err(cpi, this_frame); 23701b362b15af34006e6a11974088a46d42b903418eJohann 23711b362b15af34006e6a11974088a46d42b903418eJohann /* What portion of the remaining GF group error is used by this frame */ 23721b362b15af34006e6a11974088a46d42b903418eJohann if (cpi->twopass.gf_group_error_left > 0) 23731b362b15af34006e6a11974088a46d42b903418eJohann err_fraction = modified_err / cpi->twopass.gf_group_error_left; 23741b362b15af34006e6a11974088a46d42b903418eJohann else 23751b362b15af34006e6a11974088a46d42b903418eJohann err_fraction = 0.0; 23761b362b15af34006e6a11974088a46d42b903418eJohann 23771b362b15af34006e6a11974088a46d42b903418eJohann /* How many of those bits available for allocation should we give it? */ 23781b362b15af34006e6a11974088a46d42b903418eJohann target_frame_size = (int)((double)cpi->twopass.gf_group_bits * err_fraction); 23791b362b15af34006e6a11974088a46d42b903418eJohann 23801b362b15af34006e6a11974088a46d42b903418eJohann /* Clip to target size to 0 - max_bits (or cpi->twopass.gf_group_bits) 23811b362b15af34006e6a11974088a46d42b903418eJohann * at the top end. 23821b362b15af34006e6a11974088a46d42b903418eJohann */ 23831b362b15af34006e6a11974088a46d42b903418eJohann if (target_frame_size < 0) 23841b362b15af34006e6a11974088a46d42b903418eJohann target_frame_size = 0; 23851b362b15af34006e6a11974088a46d42b903418eJohann else 23861b362b15af34006e6a11974088a46d42b903418eJohann { 23871b362b15af34006e6a11974088a46d42b903418eJohann if (target_frame_size > max_bits) 23881b362b15af34006e6a11974088a46d42b903418eJohann target_frame_size = max_bits; 23891b362b15af34006e6a11974088a46d42b903418eJohann 23901b362b15af34006e6a11974088a46d42b903418eJohann if (target_frame_size > cpi->twopass.gf_group_bits) 2391b08e2e23eec181e9951df33cd704ac294c5407b6Vignesh Venkatasubramanian target_frame_size = (int)cpi->twopass.gf_group_bits; 23921b362b15af34006e6a11974088a46d42b903418eJohann } 23931b362b15af34006e6a11974088a46d42b903418eJohann 23941b362b15af34006e6a11974088a46d42b903418eJohann /* Adjust error and bits remaining */ 23951b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.gf_group_error_left -= (int)modified_err; 23961b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.gf_group_bits -= target_frame_size; 23971b362b15af34006e6a11974088a46d42b903418eJohann 23981b362b15af34006e6a11974088a46d42b903418eJohann if (cpi->twopass.gf_group_bits < 0) 23991b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.gf_group_bits = 0; 24001b362b15af34006e6a11974088a46d42b903418eJohann 24011b362b15af34006e6a11974088a46d42b903418eJohann /* Add in the minimum number of bits that is set aside for every frame. */ 24021b362b15af34006e6a11974088a46d42b903418eJohann target_frame_size += cpi->min_frame_bandwidth; 24031b362b15af34006e6a11974088a46d42b903418eJohann 24041b362b15af34006e6a11974088a46d42b903418eJohann /* Every other frame gets a few extra bits */ 240591037db265ecdd914a26e056cf69207b4f50924ehkuang if ( (cpi->frames_since_golden & 0x01) && 24061b362b15af34006e6a11974088a46d42b903418eJohann (cpi->frames_till_gf_update_due > 0) ) 24071b362b15af34006e6a11974088a46d42b903418eJohann { 24081b362b15af34006e6a11974088a46d42b903418eJohann target_frame_size += cpi->twopass.alt_extra_bits; 24091b362b15af34006e6a11974088a46d42b903418eJohann } 24101b362b15af34006e6a11974088a46d42b903418eJohann 24111b362b15af34006e6a11974088a46d42b903418eJohann /* Per frame bit target for this frame */ 24121b362b15af34006e6a11974088a46d42b903418eJohann cpi->per_frame_bandwidth = target_frame_size; 24131b362b15af34006e6a11974088a46d42b903418eJohann} 24141b362b15af34006e6a11974088a46d42b903418eJohann 24151b362b15af34006e6a11974088a46d42b903418eJohannvoid vp8_second_pass(VP8_COMP *cpi) 24161b362b15af34006e6a11974088a46d42b903418eJohann{ 24171b362b15af34006e6a11974088a46d42b903418eJohann int tmp_q; 24181b362b15af34006e6a11974088a46d42b903418eJohann int frames_left = (int)(cpi->twopass.total_stats.count - cpi->common.current_video_frame); 24191b362b15af34006e6a11974088a46d42b903418eJohann 24201b362b15af34006e6a11974088a46d42b903418eJohann FIRSTPASS_STATS this_frame = {0}; 24211b362b15af34006e6a11974088a46d42b903418eJohann FIRSTPASS_STATS this_frame_copy; 24221b362b15af34006e6a11974088a46d42b903418eJohann 24231b362b15af34006e6a11974088a46d42b903418eJohann double this_frame_intra_error; 24241b362b15af34006e6a11974088a46d42b903418eJohann double this_frame_coded_error; 24251b362b15af34006e6a11974088a46d42b903418eJohann 24261b362b15af34006e6a11974088a46d42b903418eJohann int overhead_bits; 24271b362b15af34006e6a11974088a46d42b903418eJohann 24281b362b15af34006e6a11974088a46d42b903418eJohann if (!cpi->twopass.stats_in) 24291b362b15af34006e6a11974088a46d42b903418eJohann { 24301b362b15af34006e6a11974088a46d42b903418eJohann return ; 24311b362b15af34006e6a11974088a46d42b903418eJohann } 24321b362b15af34006e6a11974088a46d42b903418eJohann 24331b362b15af34006e6a11974088a46d42b903418eJohann vp8_clear_system_state(); 24341b362b15af34006e6a11974088a46d42b903418eJohann 24351b362b15af34006e6a11974088a46d42b903418eJohann if (EOF == input_stats(cpi, &this_frame)) 24361b362b15af34006e6a11974088a46d42b903418eJohann return; 24371b362b15af34006e6a11974088a46d42b903418eJohann 24381b362b15af34006e6a11974088a46d42b903418eJohann this_frame_intra_error = this_frame.intra_error; 24391b362b15af34006e6a11974088a46d42b903418eJohann this_frame_coded_error = this_frame.coded_error; 24401b362b15af34006e6a11974088a46d42b903418eJohann 24411b362b15af34006e6a11974088a46d42b903418eJohann /* keyframe and section processing ! */ 24421b362b15af34006e6a11974088a46d42b903418eJohann if (cpi->twopass.frames_to_key == 0) 24431b362b15af34006e6a11974088a46d42b903418eJohann { 24441b362b15af34006e6a11974088a46d42b903418eJohann /* Define next KF group and assign bits to it */ 2445da49e34c1fb5e99681f4ad99c21d9cfd83eddb96Vignesh Venkatasubramanian memcpy(&this_frame_copy, &this_frame, sizeof(this_frame)); 24461b362b15af34006e6a11974088a46d42b903418eJohann find_next_key_frame(cpi, &this_frame_copy); 24471b362b15af34006e6a11974088a46d42b903418eJohann 24481b362b15af34006e6a11974088a46d42b903418eJohann /* Special case: Error error_resilient_mode mode does not make much 2449b08e2e23eec181e9951df33cd704ac294c5407b6Vignesh Venkatasubramanian * sense for two pass but with its current meaning this code is 24501b362b15af34006e6a11974088a46d42b903418eJohann * designed to stop outlandish behaviour if someone does set it when 24511b362b15af34006e6a11974088a46d42b903418eJohann * using two pass. It effectively disables GF groups. This is 2452b08e2e23eec181e9951df33cd704ac294c5407b6Vignesh Venkatasubramanian * temporary code until we decide what should really happen in this 24531b362b15af34006e6a11974088a46d42b903418eJohann * case. 24541b362b15af34006e6a11974088a46d42b903418eJohann */ 24551b362b15af34006e6a11974088a46d42b903418eJohann if (cpi->oxcf.error_resilient_mode) 24561b362b15af34006e6a11974088a46d42b903418eJohann { 2457ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang cpi->twopass.gf_group_bits = cpi->twopass.kf_group_bits; 24581b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.gf_group_error_left = 24591b362b15af34006e6a11974088a46d42b903418eJohann (int)cpi->twopass.kf_group_error_left; 24601b362b15af34006e6a11974088a46d42b903418eJohann cpi->baseline_gf_interval = cpi->twopass.frames_to_key; 24611b362b15af34006e6a11974088a46d42b903418eJohann cpi->frames_till_gf_update_due = cpi->baseline_gf_interval; 24621b362b15af34006e6a11974088a46d42b903418eJohann cpi->source_alt_ref_pending = 0; 24631b362b15af34006e6a11974088a46d42b903418eJohann } 24641b362b15af34006e6a11974088a46d42b903418eJohann 24651b362b15af34006e6a11974088a46d42b903418eJohann } 24661b362b15af34006e6a11974088a46d42b903418eJohann 24671b362b15af34006e6a11974088a46d42b903418eJohann /* Is this a GF / ARF (Note that a KF is always also a GF) */ 24681b362b15af34006e6a11974088a46d42b903418eJohann if (cpi->frames_till_gf_update_due == 0) 24691b362b15af34006e6a11974088a46d42b903418eJohann { 24701b362b15af34006e6a11974088a46d42b903418eJohann /* Define next gf group and assign bits to it */ 2471da49e34c1fb5e99681f4ad99c21d9cfd83eddb96Vignesh Venkatasubramanian memcpy(&this_frame_copy, &this_frame, sizeof(this_frame)); 24721b362b15af34006e6a11974088a46d42b903418eJohann define_gf_group(cpi, &this_frame_copy); 24731b362b15af34006e6a11974088a46d42b903418eJohann 24741b362b15af34006e6a11974088a46d42b903418eJohann /* If we are going to code an altref frame at the end of the group 24751b362b15af34006e6a11974088a46d42b903418eJohann * and the current frame is not a key frame.... If the previous 24761b362b15af34006e6a11974088a46d42b903418eJohann * group used an arf this frame has already benefited from that arf 24771b362b15af34006e6a11974088a46d42b903418eJohann * boost and it should not be given extra bits If the previous 24781b362b15af34006e6a11974088a46d42b903418eJohann * group was NOT coded using arf we may want to apply some boost to 24791b362b15af34006e6a11974088a46d42b903418eJohann * this GF as well 24801b362b15af34006e6a11974088a46d42b903418eJohann */ 24811b362b15af34006e6a11974088a46d42b903418eJohann if (cpi->source_alt_ref_pending && (cpi->common.frame_type != KEY_FRAME)) 24821b362b15af34006e6a11974088a46d42b903418eJohann { 24831b362b15af34006e6a11974088a46d42b903418eJohann /* Assign a standard frames worth of bits from those allocated 24841b362b15af34006e6a11974088a46d42b903418eJohann * to the GF group 24851b362b15af34006e6a11974088a46d42b903418eJohann */ 24861b362b15af34006e6a11974088a46d42b903418eJohann int bak = cpi->per_frame_bandwidth; 2487da49e34c1fb5e99681f4ad99c21d9cfd83eddb96Vignesh Venkatasubramanian memcpy(&this_frame_copy, &this_frame, sizeof(this_frame)); 24881b362b15af34006e6a11974088a46d42b903418eJohann assign_std_frame_bits(cpi, &this_frame_copy); 24891b362b15af34006e6a11974088a46d42b903418eJohann cpi->per_frame_bandwidth = bak; 24901b362b15af34006e6a11974088a46d42b903418eJohann } 24911b362b15af34006e6a11974088a46d42b903418eJohann } 24921b362b15af34006e6a11974088a46d42b903418eJohann 24931b362b15af34006e6a11974088a46d42b903418eJohann /* Otherwise this is an ordinary frame */ 24941b362b15af34006e6a11974088a46d42b903418eJohann else 24951b362b15af34006e6a11974088a46d42b903418eJohann { 24961b362b15af34006e6a11974088a46d42b903418eJohann /* Special case: Error error_resilient_mode mode does not make much 24971b362b15af34006e6a11974088a46d42b903418eJohann * sense for two pass but with its current meaning but this code is 24981b362b15af34006e6a11974088a46d42b903418eJohann * designed to stop outlandish behaviour if someone does set it 24991b362b15af34006e6a11974088a46d42b903418eJohann * when using two pass. It effectively disables GF groups. This is 25001b362b15af34006e6a11974088a46d42b903418eJohann * temporary code till we decide what should really happen in this 25011b362b15af34006e6a11974088a46d42b903418eJohann * case. 25021b362b15af34006e6a11974088a46d42b903418eJohann */ 25031b362b15af34006e6a11974088a46d42b903418eJohann if (cpi->oxcf.error_resilient_mode) 25041b362b15af34006e6a11974088a46d42b903418eJohann { 25051b362b15af34006e6a11974088a46d42b903418eJohann cpi->frames_till_gf_update_due = cpi->twopass.frames_to_key; 25061b362b15af34006e6a11974088a46d42b903418eJohann 25071b362b15af34006e6a11974088a46d42b903418eJohann if (cpi->common.frame_type != KEY_FRAME) 25081b362b15af34006e6a11974088a46d42b903418eJohann { 25091b362b15af34006e6a11974088a46d42b903418eJohann /* Assign bits from those allocated to the GF group */ 2510da49e34c1fb5e99681f4ad99c21d9cfd83eddb96Vignesh Venkatasubramanian memcpy(&this_frame_copy, &this_frame, sizeof(this_frame)); 25111b362b15af34006e6a11974088a46d42b903418eJohann assign_std_frame_bits(cpi, &this_frame_copy); 25121b362b15af34006e6a11974088a46d42b903418eJohann } 25131b362b15af34006e6a11974088a46d42b903418eJohann } 25141b362b15af34006e6a11974088a46d42b903418eJohann else 25151b362b15af34006e6a11974088a46d42b903418eJohann { 25161b362b15af34006e6a11974088a46d42b903418eJohann /* Assign bits from those allocated to the GF group */ 2517da49e34c1fb5e99681f4ad99c21d9cfd83eddb96Vignesh Venkatasubramanian memcpy(&this_frame_copy, &this_frame, sizeof(this_frame)); 25181b362b15af34006e6a11974088a46d42b903418eJohann assign_std_frame_bits(cpi, &this_frame_copy); 25191b362b15af34006e6a11974088a46d42b903418eJohann } 25201b362b15af34006e6a11974088a46d42b903418eJohann } 25211b362b15af34006e6a11974088a46d42b903418eJohann 25221b362b15af34006e6a11974088a46d42b903418eJohann /* Keep a globally available copy of this and the next frame's iiratio. */ 25231b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.this_iiratio = (unsigned int)(this_frame_intra_error / 25241b362b15af34006e6a11974088a46d42b903418eJohann DOUBLE_DIVIDE_CHECK(this_frame_coded_error)); 25251b362b15af34006e6a11974088a46d42b903418eJohann { 25261b362b15af34006e6a11974088a46d42b903418eJohann FIRSTPASS_STATS next_frame; 25271b362b15af34006e6a11974088a46d42b903418eJohann if ( lookup_next_frame_stats(cpi, &next_frame) != EOF ) 25281b362b15af34006e6a11974088a46d42b903418eJohann { 25291b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.next_iiratio = (unsigned int)(next_frame.intra_error / 25301b362b15af34006e6a11974088a46d42b903418eJohann DOUBLE_DIVIDE_CHECK(next_frame.coded_error)); 25311b362b15af34006e6a11974088a46d42b903418eJohann } 25321b362b15af34006e6a11974088a46d42b903418eJohann } 25331b362b15af34006e6a11974088a46d42b903418eJohann 25341b362b15af34006e6a11974088a46d42b903418eJohann /* Set nominal per second bandwidth for this frame */ 25351b362b15af34006e6a11974088a46d42b903418eJohann cpi->target_bandwidth = (int) 253691037db265ecdd914a26e056cf69207b4f50924ehkuang (cpi->per_frame_bandwidth * cpi->output_framerate); 25371b362b15af34006e6a11974088a46d42b903418eJohann if (cpi->target_bandwidth < 0) 25381b362b15af34006e6a11974088a46d42b903418eJohann cpi->target_bandwidth = 0; 25391b362b15af34006e6a11974088a46d42b903418eJohann 25401b362b15af34006e6a11974088a46d42b903418eJohann 25411b362b15af34006e6a11974088a46d42b903418eJohann /* Account for mv, mode and other overheads. */ 25421b362b15af34006e6a11974088a46d42b903418eJohann overhead_bits = (int)estimate_modemvcost( 25431b362b15af34006e6a11974088a46d42b903418eJohann cpi, &cpi->twopass.total_left_stats ); 25441b362b15af34006e6a11974088a46d42b903418eJohann 25451b362b15af34006e6a11974088a46d42b903418eJohann /* Special case code for first frame. */ 25461b362b15af34006e6a11974088a46d42b903418eJohann if (cpi->common.current_video_frame == 0) 25471b362b15af34006e6a11974088a46d42b903418eJohann { 25481b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.est_max_qcorrection_factor = 1.0; 25491b362b15af34006e6a11974088a46d42b903418eJohann 25501b362b15af34006e6a11974088a46d42b903418eJohann /* Set a cq_level in constrained quality mode. */ 25511b362b15af34006e6a11974088a46d42b903418eJohann if ( cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY ) 25521b362b15af34006e6a11974088a46d42b903418eJohann { 25531b362b15af34006e6a11974088a46d42b903418eJohann int est_cq; 25541b362b15af34006e6a11974088a46d42b903418eJohann 25551b362b15af34006e6a11974088a46d42b903418eJohann est_cq = 25561b362b15af34006e6a11974088a46d42b903418eJohann estimate_cq( cpi, 25571b362b15af34006e6a11974088a46d42b903418eJohann &cpi->twopass.total_left_stats, 25581b362b15af34006e6a11974088a46d42b903418eJohann (int)(cpi->twopass.bits_left / frames_left), 25591b362b15af34006e6a11974088a46d42b903418eJohann overhead_bits ); 25601b362b15af34006e6a11974088a46d42b903418eJohann 25611b362b15af34006e6a11974088a46d42b903418eJohann cpi->cq_target_quality = cpi->oxcf.cq_level; 25621b362b15af34006e6a11974088a46d42b903418eJohann if ( est_cq > cpi->cq_target_quality ) 25631b362b15af34006e6a11974088a46d42b903418eJohann cpi->cq_target_quality = est_cq; 25641b362b15af34006e6a11974088a46d42b903418eJohann } 25651b362b15af34006e6a11974088a46d42b903418eJohann 25661b362b15af34006e6a11974088a46d42b903418eJohann /* guess at maxq needed in 2nd pass */ 25671b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.maxq_max_limit = cpi->worst_quality; 25681b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.maxq_min_limit = cpi->best_quality; 25691b362b15af34006e6a11974088a46d42b903418eJohann 25701b362b15af34006e6a11974088a46d42b903418eJohann tmp_q = estimate_max_q( 25711b362b15af34006e6a11974088a46d42b903418eJohann cpi, 25721b362b15af34006e6a11974088a46d42b903418eJohann &cpi->twopass.total_left_stats, 25731b362b15af34006e6a11974088a46d42b903418eJohann (int)(cpi->twopass.bits_left / frames_left), 25741b362b15af34006e6a11974088a46d42b903418eJohann overhead_bits ); 25751b362b15af34006e6a11974088a46d42b903418eJohann 25761b362b15af34006e6a11974088a46d42b903418eJohann /* Limit the maxq value returned subsequently. 25771b362b15af34006e6a11974088a46d42b903418eJohann * This increases the risk of overspend or underspend if the initial 25781b362b15af34006e6a11974088a46d42b903418eJohann * estimate for the clip is bad, but helps prevent excessive 25791b362b15af34006e6a11974088a46d42b903418eJohann * variation in Q, especially near the end of a clip 25801b362b15af34006e6a11974088a46d42b903418eJohann * where for example a small overspend may cause Q to crash 25811b362b15af34006e6a11974088a46d42b903418eJohann */ 25821b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.maxq_max_limit = ((tmp_q + 32) < cpi->worst_quality) 25831b362b15af34006e6a11974088a46d42b903418eJohann ? (tmp_q + 32) : cpi->worst_quality; 25841b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.maxq_min_limit = ((tmp_q - 32) > cpi->best_quality) 25851b362b15af34006e6a11974088a46d42b903418eJohann ? (tmp_q - 32) : cpi->best_quality; 25861b362b15af34006e6a11974088a46d42b903418eJohann 25871b362b15af34006e6a11974088a46d42b903418eJohann cpi->active_worst_quality = tmp_q; 25881b362b15af34006e6a11974088a46d42b903418eJohann cpi->ni_av_qi = tmp_q; 25891b362b15af34006e6a11974088a46d42b903418eJohann } 25901b362b15af34006e6a11974088a46d42b903418eJohann 25911b362b15af34006e6a11974088a46d42b903418eJohann /* The last few frames of a clip almost always have to few or too many 25921b362b15af34006e6a11974088a46d42b903418eJohann * bits and for the sake of over exact rate control we dont want to make 25931b362b15af34006e6a11974088a46d42b903418eJohann * radical adjustments to the allowed quantizer range just to use up a 25941b362b15af34006e6a11974088a46d42b903418eJohann * few surplus bits or get beneath the target rate. 25951b362b15af34006e6a11974088a46d42b903418eJohann */ 25961b362b15af34006e6a11974088a46d42b903418eJohann else if ( (cpi->common.current_video_frame < 25971b362b15af34006e6a11974088a46d42b903418eJohann (((unsigned int)cpi->twopass.total_stats.count * 255)>>8)) && 25981b362b15af34006e6a11974088a46d42b903418eJohann ((cpi->common.current_video_frame + cpi->baseline_gf_interval) < 25991b362b15af34006e6a11974088a46d42b903418eJohann (unsigned int)cpi->twopass.total_stats.count) ) 26001b362b15af34006e6a11974088a46d42b903418eJohann { 26011b362b15af34006e6a11974088a46d42b903418eJohann if (frames_left < 1) 26021b362b15af34006e6a11974088a46d42b903418eJohann frames_left = 1; 26031b362b15af34006e6a11974088a46d42b903418eJohann 26041b362b15af34006e6a11974088a46d42b903418eJohann tmp_q = estimate_max_q( 26051b362b15af34006e6a11974088a46d42b903418eJohann cpi, 26061b362b15af34006e6a11974088a46d42b903418eJohann &cpi->twopass.total_left_stats, 26071b362b15af34006e6a11974088a46d42b903418eJohann (int)(cpi->twopass.bits_left / frames_left), 26081b362b15af34006e6a11974088a46d42b903418eJohann overhead_bits ); 26091b362b15af34006e6a11974088a46d42b903418eJohann 26101b362b15af34006e6a11974088a46d42b903418eJohann /* Move active_worst_quality but in a damped way */ 26111b362b15af34006e6a11974088a46d42b903418eJohann if (tmp_q > cpi->active_worst_quality) 26121b362b15af34006e6a11974088a46d42b903418eJohann cpi->active_worst_quality ++; 26131b362b15af34006e6a11974088a46d42b903418eJohann else if (tmp_q < cpi->active_worst_quality) 26141b362b15af34006e6a11974088a46d42b903418eJohann cpi->active_worst_quality --; 26151b362b15af34006e6a11974088a46d42b903418eJohann 26161b362b15af34006e6a11974088a46d42b903418eJohann cpi->active_worst_quality = 26171b362b15af34006e6a11974088a46d42b903418eJohann ((cpi->active_worst_quality * 3) + tmp_q + 2) / 4; 26181b362b15af34006e6a11974088a46d42b903418eJohann } 26191b362b15af34006e6a11974088a46d42b903418eJohann 26201b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.frames_to_key --; 26211b362b15af34006e6a11974088a46d42b903418eJohann 26221b362b15af34006e6a11974088a46d42b903418eJohann /* Update the total stats remaining sturcture */ 26231b362b15af34006e6a11974088a46d42b903418eJohann subtract_stats(&cpi->twopass.total_left_stats, &this_frame ); 26241b362b15af34006e6a11974088a46d42b903418eJohann} 26251b362b15af34006e6a11974088a46d42b903418eJohann 26261b362b15af34006e6a11974088a46d42b903418eJohann 26271b362b15af34006e6a11974088a46d42b903418eJohannstatic int test_candidate_kf(VP8_COMP *cpi, FIRSTPASS_STATS *last_frame, FIRSTPASS_STATS *this_frame, FIRSTPASS_STATS *next_frame) 26281b362b15af34006e6a11974088a46d42b903418eJohann{ 26291b362b15af34006e6a11974088a46d42b903418eJohann int is_viable_kf = 0; 26301b362b15af34006e6a11974088a46d42b903418eJohann 26311b362b15af34006e6a11974088a46d42b903418eJohann /* Does the frame satisfy the primary criteria of a key frame 26321b362b15af34006e6a11974088a46d42b903418eJohann * If so, then examine how well it predicts subsequent frames 26331b362b15af34006e6a11974088a46d42b903418eJohann */ 26341b362b15af34006e6a11974088a46d42b903418eJohann if ((this_frame->pcnt_second_ref < 0.10) && 26351b362b15af34006e6a11974088a46d42b903418eJohann (next_frame->pcnt_second_ref < 0.10) && 26361b362b15af34006e6a11974088a46d42b903418eJohann ((this_frame->pcnt_inter < 0.05) || 26371b362b15af34006e6a11974088a46d42b903418eJohann ( 26381b362b15af34006e6a11974088a46d42b903418eJohann ((this_frame->pcnt_inter - this_frame->pcnt_neutral) < .25) && 26391b362b15af34006e6a11974088a46d42b903418eJohann ((this_frame->intra_error / DOUBLE_DIVIDE_CHECK(this_frame->coded_error)) < 2.5) && 26401b362b15af34006e6a11974088a46d42b903418eJohann ((fabs(last_frame->coded_error - this_frame->coded_error) / DOUBLE_DIVIDE_CHECK(this_frame->coded_error) > .40) || 26411b362b15af34006e6a11974088a46d42b903418eJohann (fabs(last_frame->intra_error - this_frame->intra_error) / DOUBLE_DIVIDE_CHECK(this_frame->intra_error) > .40) || 26421b362b15af34006e6a11974088a46d42b903418eJohann ((next_frame->intra_error / DOUBLE_DIVIDE_CHECK(next_frame->coded_error)) > 3.5) 26431b362b15af34006e6a11974088a46d42b903418eJohann ) 26441b362b15af34006e6a11974088a46d42b903418eJohann ) 26451b362b15af34006e6a11974088a46d42b903418eJohann ) 26461b362b15af34006e6a11974088a46d42b903418eJohann ) 26471b362b15af34006e6a11974088a46d42b903418eJohann { 26481b362b15af34006e6a11974088a46d42b903418eJohann int i; 26491b362b15af34006e6a11974088a46d42b903418eJohann FIRSTPASS_STATS *start_pos; 26501b362b15af34006e6a11974088a46d42b903418eJohann 26511b362b15af34006e6a11974088a46d42b903418eJohann FIRSTPASS_STATS local_next_frame; 26521b362b15af34006e6a11974088a46d42b903418eJohann 26531b362b15af34006e6a11974088a46d42b903418eJohann double boost_score = 0.0; 26541b362b15af34006e6a11974088a46d42b903418eJohann double old_boost_score = 0.0; 26551b362b15af34006e6a11974088a46d42b903418eJohann double decay_accumulator = 1.0; 26561b362b15af34006e6a11974088a46d42b903418eJohann double next_iiratio; 26571b362b15af34006e6a11974088a46d42b903418eJohann 2658da49e34c1fb5e99681f4ad99c21d9cfd83eddb96Vignesh Venkatasubramanian memcpy(&local_next_frame, next_frame, sizeof(*next_frame)); 26591b362b15af34006e6a11974088a46d42b903418eJohann 26601b362b15af34006e6a11974088a46d42b903418eJohann /* Note the starting file position so we can reset to it */ 26611b362b15af34006e6a11974088a46d42b903418eJohann start_pos = cpi->twopass.stats_in; 26621b362b15af34006e6a11974088a46d42b903418eJohann 26631b362b15af34006e6a11974088a46d42b903418eJohann /* Examine how well the key frame predicts subsequent frames */ 26641b362b15af34006e6a11974088a46d42b903418eJohann for (i = 0 ; i < 16; i++) 26651b362b15af34006e6a11974088a46d42b903418eJohann { 26661b362b15af34006e6a11974088a46d42b903418eJohann next_iiratio = (IIKFACTOR1 * local_next_frame.intra_error / DOUBLE_DIVIDE_CHECK(local_next_frame.coded_error)) ; 26671b362b15af34006e6a11974088a46d42b903418eJohann 26681b362b15af34006e6a11974088a46d42b903418eJohann if (next_iiratio > RMAX) 26691b362b15af34006e6a11974088a46d42b903418eJohann next_iiratio = RMAX; 26701b362b15af34006e6a11974088a46d42b903418eJohann 26711b362b15af34006e6a11974088a46d42b903418eJohann /* Cumulative effect of decay in prediction quality */ 26721b362b15af34006e6a11974088a46d42b903418eJohann if (local_next_frame.pcnt_inter > 0.85) 26731b362b15af34006e6a11974088a46d42b903418eJohann decay_accumulator = decay_accumulator * local_next_frame.pcnt_inter; 26741b362b15af34006e6a11974088a46d42b903418eJohann else 26751b362b15af34006e6a11974088a46d42b903418eJohann decay_accumulator = decay_accumulator * ((0.85 + local_next_frame.pcnt_inter) / 2.0); 26761b362b15af34006e6a11974088a46d42b903418eJohann 26771b362b15af34006e6a11974088a46d42b903418eJohann /* Keep a running total */ 26781b362b15af34006e6a11974088a46d42b903418eJohann boost_score += (decay_accumulator * next_iiratio); 26791b362b15af34006e6a11974088a46d42b903418eJohann 26801b362b15af34006e6a11974088a46d42b903418eJohann /* Test various breakout clauses */ 26811b362b15af34006e6a11974088a46d42b903418eJohann if ((local_next_frame.pcnt_inter < 0.05) || 26821b362b15af34006e6a11974088a46d42b903418eJohann (next_iiratio < 1.5) || 26831b362b15af34006e6a11974088a46d42b903418eJohann (((local_next_frame.pcnt_inter - 26841b362b15af34006e6a11974088a46d42b903418eJohann local_next_frame.pcnt_neutral) < 0.20) && 26851b362b15af34006e6a11974088a46d42b903418eJohann (next_iiratio < 3.0)) || 26861b362b15af34006e6a11974088a46d42b903418eJohann ((boost_score - old_boost_score) < 0.5) || 26871b362b15af34006e6a11974088a46d42b903418eJohann (local_next_frame.intra_error < 200) 26881b362b15af34006e6a11974088a46d42b903418eJohann ) 26891b362b15af34006e6a11974088a46d42b903418eJohann { 26901b362b15af34006e6a11974088a46d42b903418eJohann break; 26911b362b15af34006e6a11974088a46d42b903418eJohann } 26921b362b15af34006e6a11974088a46d42b903418eJohann 26931b362b15af34006e6a11974088a46d42b903418eJohann old_boost_score = boost_score; 26941b362b15af34006e6a11974088a46d42b903418eJohann 26951b362b15af34006e6a11974088a46d42b903418eJohann /* Get the next frame details */ 26961b362b15af34006e6a11974088a46d42b903418eJohann if (EOF == input_stats(cpi, &local_next_frame)) 26971b362b15af34006e6a11974088a46d42b903418eJohann break; 26981b362b15af34006e6a11974088a46d42b903418eJohann } 26991b362b15af34006e6a11974088a46d42b903418eJohann 27001b362b15af34006e6a11974088a46d42b903418eJohann /* If there is tolerable prediction for at least the next 3 frames 27011b362b15af34006e6a11974088a46d42b903418eJohann * then break out else discard this pottential key frame and move on 27021b362b15af34006e6a11974088a46d42b903418eJohann */ 27031b362b15af34006e6a11974088a46d42b903418eJohann if (boost_score > 5.0 && (i > 3)) 27041b362b15af34006e6a11974088a46d42b903418eJohann is_viable_kf = 1; 27051b362b15af34006e6a11974088a46d42b903418eJohann else 27061b362b15af34006e6a11974088a46d42b903418eJohann { 27071b362b15af34006e6a11974088a46d42b903418eJohann /* Reset the file position */ 27081b362b15af34006e6a11974088a46d42b903418eJohann reset_fpf_position(cpi, start_pos); 27091b362b15af34006e6a11974088a46d42b903418eJohann 27101b362b15af34006e6a11974088a46d42b903418eJohann is_viable_kf = 0; 27111b362b15af34006e6a11974088a46d42b903418eJohann } 27121b362b15af34006e6a11974088a46d42b903418eJohann } 27131b362b15af34006e6a11974088a46d42b903418eJohann 27141b362b15af34006e6a11974088a46d42b903418eJohann return is_viable_kf; 27151b362b15af34006e6a11974088a46d42b903418eJohann} 27161b362b15af34006e6a11974088a46d42b903418eJohannstatic void find_next_key_frame(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame) 27171b362b15af34006e6a11974088a46d42b903418eJohann{ 27181b362b15af34006e6a11974088a46d42b903418eJohann int i,j; 27191b362b15af34006e6a11974088a46d42b903418eJohann FIRSTPASS_STATS last_frame; 27201b362b15af34006e6a11974088a46d42b903418eJohann FIRSTPASS_STATS first_frame; 27211b362b15af34006e6a11974088a46d42b903418eJohann FIRSTPASS_STATS next_frame; 27221b362b15af34006e6a11974088a46d42b903418eJohann FIRSTPASS_STATS *start_position; 27231b362b15af34006e6a11974088a46d42b903418eJohann 27241b362b15af34006e6a11974088a46d42b903418eJohann double decay_accumulator = 1.0; 27251b362b15af34006e6a11974088a46d42b903418eJohann double boost_score = 0; 27261b362b15af34006e6a11974088a46d42b903418eJohann double old_boost_score = 0.0; 27271b362b15af34006e6a11974088a46d42b903418eJohann double loop_decay_rate; 27281b362b15af34006e6a11974088a46d42b903418eJohann 27291b362b15af34006e6a11974088a46d42b903418eJohann double kf_mod_err = 0.0; 27301b362b15af34006e6a11974088a46d42b903418eJohann double kf_group_err = 0.0; 27311b362b15af34006e6a11974088a46d42b903418eJohann double kf_group_intra_err = 0.0; 27321b362b15af34006e6a11974088a46d42b903418eJohann double kf_group_coded_err = 0.0; 27331b362b15af34006e6a11974088a46d42b903418eJohann double recent_loop_decay[8] = {1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0}; 27341b362b15af34006e6a11974088a46d42b903418eJohann 2735da49e34c1fb5e99681f4ad99c21d9cfd83eddb96Vignesh Venkatasubramanian memset(&next_frame, 0, sizeof(next_frame)); 27361b362b15af34006e6a11974088a46d42b903418eJohann 27371b362b15af34006e6a11974088a46d42b903418eJohann vp8_clear_system_state(); 27381b362b15af34006e6a11974088a46d42b903418eJohann start_position = cpi->twopass.stats_in; 27391b362b15af34006e6a11974088a46d42b903418eJohann 27401b362b15af34006e6a11974088a46d42b903418eJohann cpi->common.frame_type = KEY_FRAME; 27411b362b15af34006e6a11974088a46d42b903418eJohann 27421b362b15af34006e6a11974088a46d42b903418eJohann /* is this a forced key frame by interval */ 27431b362b15af34006e6a11974088a46d42b903418eJohann cpi->this_key_frame_forced = cpi->next_key_frame_forced; 27441b362b15af34006e6a11974088a46d42b903418eJohann 27451b362b15af34006e6a11974088a46d42b903418eJohann /* Clear the alt ref active flag as this can never be active on a key 27461b362b15af34006e6a11974088a46d42b903418eJohann * frame 27471b362b15af34006e6a11974088a46d42b903418eJohann */ 27481b362b15af34006e6a11974088a46d42b903418eJohann cpi->source_alt_ref_active = 0; 27491b362b15af34006e6a11974088a46d42b903418eJohann 27501b362b15af34006e6a11974088a46d42b903418eJohann /* Kf is always a gf so clear frames till next gf counter */ 27511b362b15af34006e6a11974088a46d42b903418eJohann cpi->frames_till_gf_update_due = 0; 27521b362b15af34006e6a11974088a46d42b903418eJohann 27531b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.frames_to_key = 1; 27541b362b15af34006e6a11974088a46d42b903418eJohann 27551b362b15af34006e6a11974088a46d42b903418eJohann /* Take a copy of the initial frame details */ 2756da49e34c1fb5e99681f4ad99c21d9cfd83eddb96Vignesh Venkatasubramanian memcpy(&first_frame, this_frame, sizeof(*this_frame)); 27571b362b15af34006e6a11974088a46d42b903418eJohann 27581b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.kf_group_bits = 0; 27591b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.kf_group_error_left = 0; 27601b362b15af34006e6a11974088a46d42b903418eJohann 27611b362b15af34006e6a11974088a46d42b903418eJohann kf_mod_err = calculate_modified_err(cpi, this_frame); 27621b362b15af34006e6a11974088a46d42b903418eJohann 27631b362b15af34006e6a11974088a46d42b903418eJohann /* find the next keyframe */ 27641b362b15af34006e6a11974088a46d42b903418eJohann i = 0; 27651b362b15af34006e6a11974088a46d42b903418eJohann while (cpi->twopass.stats_in < cpi->twopass.stats_in_end) 27661b362b15af34006e6a11974088a46d42b903418eJohann { 27671b362b15af34006e6a11974088a46d42b903418eJohann /* Accumulate kf group error */ 27681b362b15af34006e6a11974088a46d42b903418eJohann kf_group_err += calculate_modified_err(cpi, this_frame); 27691b362b15af34006e6a11974088a46d42b903418eJohann 27701b362b15af34006e6a11974088a46d42b903418eJohann /* These figures keep intra and coded error counts for all frames 27711b362b15af34006e6a11974088a46d42b903418eJohann * including key frames in the group. The effect of the key frame 27721b362b15af34006e6a11974088a46d42b903418eJohann * itself can be subtracted out using the first_frame data 27731b362b15af34006e6a11974088a46d42b903418eJohann * collected above 27741b362b15af34006e6a11974088a46d42b903418eJohann */ 27751b362b15af34006e6a11974088a46d42b903418eJohann kf_group_intra_err += this_frame->intra_error; 27761b362b15af34006e6a11974088a46d42b903418eJohann kf_group_coded_err += this_frame->coded_error; 27771b362b15af34006e6a11974088a46d42b903418eJohann 2778b08e2e23eec181e9951df33cd704ac294c5407b6Vignesh Venkatasubramanian /* Load the next frame's stats. */ 2779da49e34c1fb5e99681f4ad99c21d9cfd83eddb96Vignesh Venkatasubramanian memcpy(&last_frame, this_frame, sizeof(*this_frame)); 27801b362b15af34006e6a11974088a46d42b903418eJohann input_stats(cpi, this_frame); 27811b362b15af34006e6a11974088a46d42b903418eJohann 27821b362b15af34006e6a11974088a46d42b903418eJohann /* Provided that we are not at the end of the file... */ 27831b362b15af34006e6a11974088a46d42b903418eJohann if (cpi->oxcf.auto_key 27841b362b15af34006e6a11974088a46d42b903418eJohann && lookup_next_frame_stats(cpi, &next_frame) != EOF) 27851b362b15af34006e6a11974088a46d42b903418eJohann { 27861b362b15af34006e6a11974088a46d42b903418eJohann /* Normal scene cut check */ 27871b362b15af34006e6a11974088a46d42b903418eJohann if ( ( i >= MIN_GF_INTERVAL ) && 27881b362b15af34006e6a11974088a46d42b903418eJohann test_candidate_kf(cpi, &last_frame, this_frame, &next_frame) ) 27891b362b15af34006e6a11974088a46d42b903418eJohann { 27901b362b15af34006e6a11974088a46d42b903418eJohann break; 27911b362b15af34006e6a11974088a46d42b903418eJohann } 27921b362b15af34006e6a11974088a46d42b903418eJohann 27931b362b15af34006e6a11974088a46d42b903418eJohann /* How fast is prediction quality decaying */ 27941b362b15af34006e6a11974088a46d42b903418eJohann loop_decay_rate = get_prediction_decay_rate(cpi, &next_frame); 27951b362b15af34006e6a11974088a46d42b903418eJohann 27961b362b15af34006e6a11974088a46d42b903418eJohann /* We want to know something about the recent past... rather than 27971b362b15af34006e6a11974088a46d42b903418eJohann * as used elsewhere where we are concened with decay in prediction 27981b362b15af34006e6a11974088a46d42b903418eJohann * quality since the last GF or KF. 27991b362b15af34006e6a11974088a46d42b903418eJohann */ 28001b362b15af34006e6a11974088a46d42b903418eJohann recent_loop_decay[i%8] = loop_decay_rate; 28011b362b15af34006e6a11974088a46d42b903418eJohann decay_accumulator = 1.0; 28021b362b15af34006e6a11974088a46d42b903418eJohann for (j = 0; j < 8; j++) 28031b362b15af34006e6a11974088a46d42b903418eJohann { 28041b362b15af34006e6a11974088a46d42b903418eJohann decay_accumulator = decay_accumulator * recent_loop_decay[j]; 28051b362b15af34006e6a11974088a46d42b903418eJohann } 28061b362b15af34006e6a11974088a46d42b903418eJohann 28071b362b15af34006e6a11974088a46d42b903418eJohann /* Special check for transition or high motion followed by a 28081b362b15af34006e6a11974088a46d42b903418eJohann * static scene. 28091b362b15af34006e6a11974088a46d42b903418eJohann */ 28101b362b15af34006e6a11974088a46d42b903418eJohann if ( detect_transition_to_still( cpi, i, 28111b362b15af34006e6a11974088a46d42b903418eJohann (cpi->key_frame_frequency-i), 28121b362b15af34006e6a11974088a46d42b903418eJohann loop_decay_rate, 28131b362b15af34006e6a11974088a46d42b903418eJohann decay_accumulator ) ) 28141b362b15af34006e6a11974088a46d42b903418eJohann { 28151b362b15af34006e6a11974088a46d42b903418eJohann break; 28161b362b15af34006e6a11974088a46d42b903418eJohann } 28171b362b15af34006e6a11974088a46d42b903418eJohann 28181b362b15af34006e6a11974088a46d42b903418eJohann 28191b362b15af34006e6a11974088a46d42b903418eJohann /* Step on to the next frame */ 28201b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.frames_to_key ++; 28211b362b15af34006e6a11974088a46d42b903418eJohann 28221b362b15af34006e6a11974088a46d42b903418eJohann /* If we don't have a real key frame within the next two 28231b362b15af34006e6a11974088a46d42b903418eJohann * forcekeyframeevery intervals then break out of the loop. 28241b362b15af34006e6a11974088a46d42b903418eJohann */ 28251b362b15af34006e6a11974088a46d42b903418eJohann if (cpi->twopass.frames_to_key >= 2 *(int)cpi->key_frame_frequency) 28261b362b15af34006e6a11974088a46d42b903418eJohann break; 28271b362b15af34006e6a11974088a46d42b903418eJohann } else 28281b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.frames_to_key ++; 28291b362b15af34006e6a11974088a46d42b903418eJohann 28301b362b15af34006e6a11974088a46d42b903418eJohann i++; 28311b362b15af34006e6a11974088a46d42b903418eJohann } 28321b362b15af34006e6a11974088a46d42b903418eJohann 28331b362b15af34006e6a11974088a46d42b903418eJohann /* If there is a max kf interval set by the user we must obey it. 28341b362b15af34006e6a11974088a46d42b903418eJohann * We already breakout of the loop above at 2x max. 28351b362b15af34006e6a11974088a46d42b903418eJohann * This code centers the extra kf if the actual natural 28361b362b15af34006e6a11974088a46d42b903418eJohann * interval is between 1x and 2x 28371b362b15af34006e6a11974088a46d42b903418eJohann */ 28381b362b15af34006e6a11974088a46d42b903418eJohann if (cpi->oxcf.auto_key 28391b362b15af34006e6a11974088a46d42b903418eJohann && cpi->twopass.frames_to_key > (int)cpi->key_frame_frequency ) 28401b362b15af34006e6a11974088a46d42b903418eJohann { 28411b362b15af34006e6a11974088a46d42b903418eJohann FIRSTPASS_STATS *current_pos = cpi->twopass.stats_in; 28421b362b15af34006e6a11974088a46d42b903418eJohann FIRSTPASS_STATS tmp_frame; 28431b362b15af34006e6a11974088a46d42b903418eJohann 28441b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.frames_to_key /= 2; 28451b362b15af34006e6a11974088a46d42b903418eJohann 28461b362b15af34006e6a11974088a46d42b903418eJohann /* Copy first frame details */ 2847da49e34c1fb5e99681f4ad99c21d9cfd83eddb96Vignesh Venkatasubramanian memcpy(&tmp_frame, &first_frame, sizeof(first_frame)); 28481b362b15af34006e6a11974088a46d42b903418eJohann 28491b362b15af34006e6a11974088a46d42b903418eJohann /* Reset to the start of the group */ 28501b362b15af34006e6a11974088a46d42b903418eJohann reset_fpf_position(cpi, start_position); 28511b362b15af34006e6a11974088a46d42b903418eJohann 28521b362b15af34006e6a11974088a46d42b903418eJohann kf_group_err = 0; 28531b362b15af34006e6a11974088a46d42b903418eJohann kf_group_intra_err = 0; 28541b362b15af34006e6a11974088a46d42b903418eJohann kf_group_coded_err = 0; 28551b362b15af34006e6a11974088a46d42b903418eJohann 28561b362b15af34006e6a11974088a46d42b903418eJohann /* Rescan to get the correct error data for the forced kf group */ 28571b362b15af34006e6a11974088a46d42b903418eJohann for( i = 0; i < cpi->twopass.frames_to_key; i++ ) 28581b362b15af34006e6a11974088a46d42b903418eJohann { 28591b362b15af34006e6a11974088a46d42b903418eJohann /* Accumulate kf group errors */ 28601b362b15af34006e6a11974088a46d42b903418eJohann kf_group_err += calculate_modified_err(cpi, &tmp_frame); 28611b362b15af34006e6a11974088a46d42b903418eJohann kf_group_intra_err += tmp_frame.intra_error; 28621b362b15af34006e6a11974088a46d42b903418eJohann kf_group_coded_err += tmp_frame.coded_error; 28631b362b15af34006e6a11974088a46d42b903418eJohann 28641b362b15af34006e6a11974088a46d42b903418eJohann /* Load a the next frame's stats */ 28651b362b15af34006e6a11974088a46d42b903418eJohann input_stats(cpi, &tmp_frame); 28661b362b15af34006e6a11974088a46d42b903418eJohann } 28671b362b15af34006e6a11974088a46d42b903418eJohann 28681b362b15af34006e6a11974088a46d42b903418eJohann /* Reset to the start of the group */ 28691b362b15af34006e6a11974088a46d42b903418eJohann reset_fpf_position(cpi, current_pos); 28701b362b15af34006e6a11974088a46d42b903418eJohann 28711b362b15af34006e6a11974088a46d42b903418eJohann cpi->next_key_frame_forced = 1; 28721b362b15af34006e6a11974088a46d42b903418eJohann } 28731b362b15af34006e6a11974088a46d42b903418eJohann else 28741b362b15af34006e6a11974088a46d42b903418eJohann cpi->next_key_frame_forced = 0; 28751b362b15af34006e6a11974088a46d42b903418eJohann 28761b362b15af34006e6a11974088a46d42b903418eJohann /* Special case for the last frame of the file */ 28771b362b15af34006e6a11974088a46d42b903418eJohann if (cpi->twopass.stats_in >= cpi->twopass.stats_in_end) 28781b362b15af34006e6a11974088a46d42b903418eJohann { 28791b362b15af34006e6a11974088a46d42b903418eJohann /* Accumulate kf group error */ 28801b362b15af34006e6a11974088a46d42b903418eJohann kf_group_err += calculate_modified_err(cpi, this_frame); 28811b362b15af34006e6a11974088a46d42b903418eJohann 28821b362b15af34006e6a11974088a46d42b903418eJohann /* These figures keep intra and coded error counts for all frames 28831b362b15af34006e6a11974088a46d42b903418eJohann * including key frames in the group. The effect of the key frame 28841b362b15af34006e6a11974088a46d42b903418eJohann * itself can be subtracted out using the first_frame data 28851b362b15af34006e6a11974088a46d42b903418eJohann * collected above 28861b362b15af34006e6a11974088a46d42b903418eJohann */ 28871b362b15af34006e6a11974088a46d42b903418eJohann kf_group_intra_err += this_frame->intra_error; 28881b362b15af34006e6a11974088a46d42b903418eJohann kf_group_coded_err += this_frame->coded_error; 28891b362b15af34006e6a11974088a46d42b903418eJohann } 28901b362b15af34006e6a11974088a46d42b903418eJohann 28911b362b15af34006e6a11974088a46d42b903418eJohann /* Calculate the number of bits that should be assigned to the kf group. */ 28921b362b15af34006e6a11974088a46d42b903418eJohann if ((cpi->twopass.bits_left > 0) && (cpi->twopass.modified_error_left > 0.0)) 28931b362b15af34006e6a11974088a46d42b903418eJohann { 28941b362b15af34006e6a11974088a46d42b903418eJohann /* Max for a single normal frame (not key frame) */ 28951b362b15af34006e6a11974088a46d42b903418eJohann int max_bits = frame_max_bits(cpi); 28961b362b15af34006e6a11974088a46d42b903418eJohann 28971b362b15af34006e6a11974088a46d42b903418eJohann /* Maximum bits for the kf group */ 28981b362b15af34006e6a11974088a46d42b903418eJohann int64_t max_grp_bits; 28991b362b15af34006e6a11974088a46d42b903418eJohann 29001b362b15af34006e6a11974088a46d42b903418eJohann /* Default allocation based on bits left and relative 29011b362b15af34006e6a11974088a46d42b903418eJohann * complexity of the section 29021b362b15af34006e6a11974088a46d42b903418eJohann */ 29031b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.kf_group_bits = (int64_t)( cpi->twopass.bits_left * 29041b362b15af34006e6a11974088a46d42b903418eJohann ( kf_group_err / 29051b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.modified_error_left )); 29061b362b15af34006e6a11974088a46d42b903418eJohann 29071b362b15af34006e6a11974088a46d42b903418eJohann /* Clip based on maximum per frame rate defined by the user. */ 29081b362b15af34006e6a11974088a46d42b903418eJohann max_grp_bits = (int64_t)max_bits * (int64_t)cpi->twopass.frames_to_key; 29091b362b15af34006e6a11974088a46d42b903418eJohann if (cpi->twopass.kf_group_bits > max_grp_bits) 29101b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.kf_group_bits = max_grp_bits; 29111b362b15af34006e6a11974088a46d42b903418eJohann 29121b362b15af34006e6a11974088a46d42b903418eJohann /* Additional special case for CBR if buffer is getting full. */ 29131b362b15af34006e6a11974088a46d42b903418eJohann if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) 29141b362b15af34006e6a11974088a46d42b903418eJohann { 29151b362b15af34006e6a11974088a46d42b903418eJohann int64_t opt_buffer_lvl = cpi->oxcf.optimal_buffer_level; 29161b362b15af34006e6a11974088a46d42b903418eJohann int64_t buffer_lvl = cpi->buffer_level; 29171b362b15af34006e6a11974088a46d42b903418eJohann 29181b362b15af34006e6a11974088a46d42b903418eJohann /* If the buffer is near or above the optimal and this kf group is 29191b362b15af34006e6a11974088a46d42b903418eJohann * not being allocated much then increase the allocation a bit. 29201b362b15af34006e6a11974088a46d42b903418eJohann */ 29211b362b15af34006e6a11974088a46d42b903418eJohann if (buffer_lvl >= opt_buffer_lvl) 29221b362b15af34006e6a11974088a46d42b903418eJohann { 29231b362b15af34006e6a11974088a46d42b903418eJohann int64_t high_water_mark = (opt_buffer_lvl + 29241b362b15af34006e6a11974088a46d42b903418eJohann cpi->oxcf.maximum_buffer_size) >> 1; 29251b362b15af34006e6a11974088a46d42b903418eJohann 29261b362b15af34006e6a11974088a46d42b903418eJohann int64_t av_group_bits; 29271b362b15af34006e6a11974088a46d42b903418eJohann 29281b362b15af34006e6a11974088a46d42b903418eJohann /* Av bits per frame * number of frames */ 29291b362b15af34006e6a11974088a46d42b903418eJohann av_group_bits = (int64_t)cpi->av_per_frame_bandwidth * 29301b362b15af34006e6a11974088a46d42b903418eJohann (int64_t)cpi->twopass.frames_to_key; 29311b362b15af34006e6a11974088a46d42b903418eJohann 29321b362b15af34006e6a11974088a46d42b903418eJohann /* We are at or above the maximum. */ 29331b362b15af34006e6a11974088a46d42b903418eJohann if (cpi->buffer_level >= high_water_mark) 29341b362b15af34006e6a11974088a46d42b903418eJohann { 29351b362b15af34006e6a11974088a46d42b903418eJohann int64_t min_group_bits; 29361b362b15af34006e6a11974088a46d42b903418eJohann 29371b362b15af34006e6a11974088a46d42b903418eJohann min_group_bits = av_group_bits + 29381b362b15af34006e6a11974088a46d42b903418eJohann (int64_t)(buffer_lvl - 29391b362b15af34006e6a11974088a46d42b903418eJohann high_water_mark); 29401b362b15af34006e6a11974088a46d42b903418eJohann 29411b362b15af34006e6a11974088a46d42b903418eJohann if (cpi->twopass.kf_group_bits < min_group_bits) 29421b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.kf_group_bits = min_group_bits; 29431b362b15af34006e6a11974088a46d42b903418eJohann } 29441b362b15af34006e6a11974088a46d42b903418eJohann /* We are above optimal but below the maximum */ 29451b362b15af34006e6a11974088a46d42b903418eJohann else if (cpi->twopass.kf_group_bits < av_group_bits) 29461b362b15af34006e6a11974088a46d42b903418eJohann { 29471b362b15af34006e6a11974088a46d42b903418eJohann int64_t bits_below_av = av_group_bits - 29481b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.kf_group_bits; 29491b362b15af34006e6a11974088a46d42b903418eJohann 29501b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.kf_group_bits += 29511b362b15af34006e6a11974088a46d42b903418eJohann (int64_t)((double)bits_below_av * 29521b362b15af34006e6a11974088a46d42b903418eJohann (double)(buffer_lvl - opt_buffer_lvl) / 29531b362b15af34006e6a11974088a46d42b903418eJohann (double)(high_water_mark - opt_buffer_lvl)); 29541b362b15af34006e6a11974088a46d42b903418eJohann } 29551b362b15af34006e6a11974088a46d42b903418eJohann } 29561b362b15af34006e6a11974088a46d42b903418eJohann } 29571b362b15af34006e6a11974088a46d42b903418eJohann } 29581b362b15af34006e6a11974088a46d42b903418eJohann else 29591b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.kf_group_bits = 0; 29601b362b15af34006e6a11974088a46d42b903418eJohann 29611b362b15af34006e6a11974088a46d42b903418eJohann /* Reset the first pass file position */ 29621b362b15af34006e6a11974088a46d42b903418eJohann reset_fpf_position(cpi, start_position); 29631b362b15af34006e6a11974088a46d42b903418eJohann 29641b362b15af34006e6a11974088a46d42b903418eJohann /* determine how big to make this keyframe based on how well the 29651b362b15af34006e6a11974088a46d42b903418eJohann * subsequent frames use inter blocks 29661b362b15af34006e6a11974088a46d42b903418eJohann */ 29671b362b15af34006e6a11974088a46d42b903418eJohann decay_accumulator = 1.0; 29681b362b15af34006e6a11974088a46d42b903418eJohann boost_score = 0.0; 29691b362b15af34006e6a11974088a46d42b903418eJohann 29701b362b15af34006e6a11974088a46d42b903418eJohann for (i = 0 ; i < cpi->twopass.frames_to_key ; i++) 29711b362b15af34006e6a11974088a46d42b903418eJohann { 29721b362b15af34006e6a11974088a46d42b903418eJohann double r; 29731b362b15af34006e6a11974088a46d42b903418eJohann 29741b362b15af34006e6a11974088a46d42b903418eJohann if (EOF == input_stats(cpi, &next_frame)) 29751b362b15af34006e6a11974088a46d42b903418eJohann break; 29761b362b15af34006e6a11974088a46d42b903418eJohann 29771b362b15af34006e6a11974088a46d42b903418eJohann if (next_frame.intra_error > cpi->twopass.kf_intra_err_min) 29781b362b15af34006e6a11974088a46d42b903418eJohann r = (IIKFACTOR2 * next_frame.intra_error / 29791b362b15af34006e6a11974088a46d42b903418eJohann DOUBLE_DIVIDE_CHECK(next_frame.coded_error)); 29801b362b15af34006e6a11974088a46d42b903418eJohann else 29811b362b15af34006e6a11974088a46d42b903418eJohann r = (IIKFACTOR2 * cpi->twopass.kf_intra_err_min / 29821b362b15af34006e6a11974088a46d42b903418eJohann DOUBLE_DIVIDE_CHECK(next_frame.coded_error)); 29831b362b15af34006e6a11974088a46d42b903418eJohann 29841b362b15af34006e6a11974088a46d42b903418eJohann if (r > RMAX) 29851b362b15af34006e6a11974088a46d42b903418eJohann r = RMAX; 29861b362b15af34006e6a11974088a46d42b903418eJohann 29871b362b15af34006e6a11974088a46d42b903418eJohann /* How fast is prediction quality decaying */ 29881b362b15af34006e6a11974088a46d42b903418eJohann loop_decay_rate = get_prediction_decay_rate(cpi, &next_frame); 29891b362b15af34006e6a11974088a46d42b903418eJohann 29901b362b15af34006e6a11974088a46d42b903418eJohann decay_accumulator = decay_accumulator * loop_decay_rate; 29911b362b15af34006e6a11974088a46d42b903418eJohann decay_accumulator = decay_accumulator < 0.1 ? 0.1 : decay_accumulator; 29921b362b15af34006e6a11974088a46d42b903418eJohann 29931b362b15af34006e6a11974088a46d42b903418eJohann boost_score += (decay_accumulator * r); 29941b362b15af34006e6a11974088a46d42b903418eJohann 29951b362b15af34006e6a11974088a46d42b903418eJohann if ((i > MIN_GF_INTERVAL) && 29961b362b15af34006e6a11974088a46d42b903418eJohann ((boost_score - old_boost_score) < 1.0)) 29971b362b15af34006e6a11974088a46d42b903418eJohann { 29981b362b15af34006e6a11974088a46d42b903418eJohann break; 29991b362b15af34006e6a11974088a46d42b903418eJohann } 30001b362b15af34006e6a11974088a46d42b903418eJohann 30011b362b15af34006e6a11974088a46d42b903418eJohann old_boost_score = boost_score; 30021b362b15af34006e6a11974088a46d42b903418eJohann } 30031b362b15af34006e6a11974088a46d42b903418eJohann 30041b362b15af34006e6a11974088a46d42b903418eJohann if (1) 30051b362b15af34006e6a11974088a46d42b903418eJohann { 30061b362b15af34006e6a11974088a46d42b903418eJohann FIRSTPASS_STATS sectionstats; 30071b362b15af34006e6a11974088a46d42b903418eJohann double Ratio; 30081b362b15af34006e6a11974088a46d42b903418eJohann 30091b362b15af34006e6a11974088a46d42b903418eJohann zero_stats(§ionstats); 30101b362b15af34006e6a11974088a46d42b903418eJohann reset_fpf_position(cpi, start_position); 30111b362b15af34006e6a11974088a46d42b903418eJohann 30121b362b15af34006e6a11974088a46d42b903418eJohann for (i = 0 ; i < cpi->twopass.frames_to_key ; i++) 30131b362b15af34006e6a11974088a46d42b903418eJohann { 30141b362b15af34006e6a11974088a46d42b903418eJohann input_stats(cpi, &next_frame); 30151b362b15af34006e6a11974088a46d42b903418eJohann accumulate_stats(§ionstats, &next_frame); 30161b362b15af34006e6a11974088a46d42b903418eJohann } 30171b362b15af34006e6a11974088a46d42b903418eJohann 30181b362b15af34006e6a11974088a46d42b903418eJohann avg_stats(§ionstats); 30191b362b15af34006e6a11974088a46d42b903418eJohann 30201b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.section_intra_rating = (unsigned int) 30211b362b15af34006e6a11974088a46d42b903418eJohann (sectionstats.intra_error 30221b362b15af34006e6a11974088a46d42b903418eJohann / DOUBLE_DIVIDE_CHECK(sectionstats.coded_error)); 30231b362b15af34006e6a11974088a46d42b903418eJohann 30241b362b15af34006e6a11974088a46d42b903418eJohann Ratio = sectionstats.intra_error / DOUBLE_DIVIDE_CHECK(sectionstats.coded_error); 30251b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.section_max_qfactor = 1.0 - ((Ratio - 10.0) * 0.025); 30261b362b15af34006e6a11974088a46d42b903418eJohann 30271b362b15af34006e6a11974088a46d42b903418eJohann if (cpi->twopass.section_max_qfactor < 0.80) 30281b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.section_max_qfactor = 0.80; 30291b362b15af34006e6a11974088a46d42b903418eJohann } 30301b362b15af34006e6a11974088a46d42b903418eJohann 30311b362b15af34006e6a11974088a46d42b903418eJohann /* When using CBR apply additional buffer fullness related upper limits */ 30321b362b15af34006e6a11974088a46d42b903418eJohann if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) 30331b362b15af34006e6a11974088a46d42b903418eJohann { 30341b362b15af34006e6a11974088a46d42b903418eJohann double max_boost; 30351b362b15af34006e6a11974088a46d42b903418eJohann 30361b362b15af34006e6a11974088a46d42b903418eJohann if (cpi->drop_frames_allowed) 30371b362b15af34006e6a11974088a46d42b903418eJohann { 30381b362b15af34006e6a11974088a46d42b903418eJohann int df_buffer_level = (int)(cpi->oxcf.drop_frames_water_mark 30391b362b15af34006e6a11974088a46d42b903418eJohann * (cpi->oxcf.optimal_buffer_level / 100)); 30401b362b15af34006e6a11974088a46d42b903418eJohann 30411b362b15af34006e6a11974088a46d42b903418eJohann if (cpi->buffer_level > df_buffer_level) 30421b362b15af34006e6a11974088a46d42b903418eJohann max_boost = ((double)((cpi->buffer_level - df_buffer_level) * 2 / 3) * 16.0) / DOUBLE_DIVIDE_CHECK((double)cpi->av_per_frame_bandwidth); 30431b362b15af34006e6a11974088a46d42b903418eJohann else 30441b362b15af34006e6a11974088a46d42b903418eJohann max_boost = 0.0; 30451b362b15af34006e6a11974088a46d42b903418eJohann } 30461b362b15af34006e6a11974088a46d42b903418eJohann else if (cpi->buffer_level > 0) 30471b362b15af34006e6a11974088a46d42b903418eJohann { 30481b362b15af34006e6a11974088a46d42b903418eJohann max_boost = ((double)(cpi->buffer_level * 2 / 3) * 16.0) / DOUBLE_DIVIDE_CHECK((double)cpi->av_per_frame_bandwidth); 30491b362b15af34006e6a11974088a46d42b903418eJohann } 30501b362b15af34006e6a11974088a46d42b903418eJohann else 30511b362b15af34006e6a11974088a46d42b903418eJohann { 30521b362b15af34006e6a11974088a46d42b903418eJohann max_boost = 0.0; 30531b362b15af34006e6a11974088a46d42b903418eJohann } 30541b362b15af34006e6a11974088a46d42b903418eJohann 30551b362b15af34006e6a11974088a46d42b903418eJohann if (boost_score > max_boost) 30561b362b15af34006e6a11974088a46d42b903418eJohann boost_score = max_boost; 30571b362b15af34006e6a11974088a46d42b903418eJohann } 30581b362b15af34006e6a11974088a46d42b903418eJohann 30591b362b15af34006e6a11974088a46d42b903418eJohann /* Reset the first pass file position */ 30601b362b15af34006e6a11974088a46d42b903418eJohann reset_fpf_position(cpi, start_position); 30611b362b15af34006e6a11974088a46d42b903418eJohann 30621b362b15af34006e6a11974088a46d42b903418eJohann /* Work out how many bits to allocate for the key frame itself */ 30631b362b15af34006e6a11974088a46d42b903418eJohann if (1) 30641b362b15af34006e6a11974088a46d42b903418eJohann { 30651b362b15af34006e6a11974088a46d42b903418eJohann int kf_boost = (int)boost_score; 30661b362b15af34006e6a11974088a46d42b903418eJohann int allocation_chunks; 30671b362b15af34006e6a11974088a46d42b903418eJohann int Counter = cpi->twopass.frames_to_key; 30681b362b15af34006e6a11974088a46d42b903418eJohann int alt_kf_bits; 30691b362b15af34006e6a11974088a46d42b903418eJohann YV12_BUFFER_CONFIG *lst_yv12 = &cpi->common.yv12_fb[cpi->common.lst_fb_idx]; 30701b362b15af34006e6a11974088a46d42b903418eJohann /* Min boost based on kf interval */ 30711b362b15af34006e6a11974088a46d42b903418eJohann#if 0 30721b362b15af34006e6a11974088a46d42b903418eJohann 30731b362b15af34006e6a11974088a46d42b903418eJohann while ((kf_boost < 48) && (Counter > 0)) 30741b362b15af34006e6a11974088a46d42b903418eJohann { 30751b362b15af34006e6a11974088a46d42b903418eJohann Counter -= 2; 30761b362b15af34006e6a11974088a46d42b903418eJohann kf_boost ++; 30771b362b15af34006e6a11974088a46d42b903418eJohann } 30781b362b15af34006e6a11974088a46d42b903418eJohann 30791b362b15af34006e6a11974088a46d42b903418eJohann#endif 30801b362b15af34006e6a11974088a46d42b903418eJohann 30811b362b15af34006e6a11974088a46d42b903418eJohann if (kf_boost < 48) 30821b362b15af34006e6a11974088a46d42b903418eJohann { 30831b362b15af34006e6a11974088a46d42b903418eJohann kf_boost += ((Counter + 1) >> 1); 30841b362b15af34006e6a11974088a46d42b903418eJohann 30851b362b15af34006e6a11974088a46d42b903418eJohann if (kf_boost > 48) kf_boost = 48; 30861b362b15af34006e6a11974088a46d42b903418eJohann } 30871b362b15af34006e6a11974088a46d42b903418eJohann 30881b362b15af34006e6a11974088a46d42b903418eJohann /* bigger frame sizes need larger kf boosts, smaller frames smaller 30891b362b15af34006e6a11974088a46d42b903418eJohann * boosts... 30901b362b15af34006e6a11974088a46d42b903418eJohann */ 30911b362b15af34006e6a11974088a46d42b903418eJohann if ((lst_yv12->y_width * lst_yv12->y_height) > (320 * 240)) 30921b362b15af34006e6a11974088a46d42b903418eJohann kf_boost += 2 * (lst_yv12->y_width * lst_yv12->y_height) / (320 * 240); 30931b362b15af34006e6a11974088a46d42b903418eJohann else if ((lst_yv12->y_width * lst_yv12->y_height) < (320 * 240)) 30941b362b15af34006e6a11974088a46d42b903418eJohann kf_boost -= 4 * (320 * 240) / (lst_yv12->y_width * lst_yv12->y_height); 30951b362b15af34006e6a11974088a46d42b903418eJohann 30961b362b15af34006e6a11974088a46d42b903418eJohann /* Min KF boost */ 30971b362b15af34006e6a11974088a46d42b903418eJohann kf_boost = (int)((double)kf_boost * 100.0) >> 4; /* Scale 16 to 100 */ 30981b362b15af34006e6a11974088a46d42b903418eJohann if (kf_boost < 250) 30991b362b15af34006e6a11974088a46d42b903418eJohann kf_boost = 250; 31001b362b15af34006e6a11974088a46d42b903418eJohann 31011b362b15af34006e6a11974088a46d42b903418eJohann /* 31021b362b15af34006e6a11974088a46d42b903418eJohann * We do three calculations for kf size. 31031b362b15af34006e6a11974088a46d42b903418eJohann * The first is based on the error score for the whole kf group. 31041b362b15af34006e6a11974088a46d42b903418eJohann * The second (optionaly) on the key frames own error if this is 31051b362b15af34006e6a11974088a46d42b903418eJohann * smaller than the average for the group. 31061b362b15af34006e6a11974088a46d42b903418eJohann * The final one insures that the frame receives at least the 31071b362b15af34006e6a11974088a46d42b903418eJohann * allocation it would have received based on its own error score vs 31081b362b15af34006e6a11974088a46d42b903418eJohann * the error score remaining 31091b362b15af34006e6a11974088a46d42b903418eJohann * Special case if the sequence appears almost totaly static 31101b362b15af34006e6a11974088a46d42b903418eJohann * as measured by the decay accumulator. In this case we want to 31111b362b15af34006e6a11974088a46d42b903418eJohann * spend almost all of the bits on the key frame. 31121b362b15af34006e6a11974088a46d42b903418eJohann * cpi->twopass.frames_to_key-1 because key frame itself is taken 31131b362b15af34006e6a11974088a46d42b903418eJohann * care of by kf_boost. 31141b362b15af34006e6a11974088a46d42b903418eJohann */ 31151b362b15af34006e6a11974088a46d42b903418eJohann if ( decay_accumulator >= 0.99 ) 31161b362b15af34006e6a11974088a46d42b903418eJohann { 31171b362b15af34006e6a11974088a46d42b903418eJohann allocation_chunks = 31181b362b15af34006e6a11974088a46d42b903418eJohann ((cpi->twopass.frames_to_key - 1) * 10) + kf_boost; 31191b362b15af34006e6a11974088a46d42b903418eJohann } 31201b362b15af34006e6a11974088a46d42b903418eJohann else 31211b362b15af34006e6a11974088a46d42b903418eJohann { 31221b362b15af34006e6a11974088a46d42b903418eJohann allocation_chunks = 31231b362b15af34006e6a11974088a46d42b903418eJohann ((cpi->twopass.frames_to_key - 1) * 100) + kf_boost; 31241b362b15af34006e6a11974088a46d42b903418eJohann } 31251b362b15af34006e6a11974088a46d42b903418eJohann 31261b362b15af34006e6a11974088a46d42b903418eJohann /* Normalize Altboost and allocations chunck down to prevent overflow */ 31271b362b15af34006e6a11974088a46d42b903418eJohann while (kf_boost > 1000) 31281b362b15af34006e6a11974088a46d42b903418eJohann { 31291b362b15af34006e6a11974088a46d42b903418eJohann kf_boost /= 2; 31301b362b15af34006e6a11974088a46d42b903418eJohann allocation_chunks /= 2; 31311b362b15af34006e6a11974088a46d42b903418eJohann } 31321b362b15af34006e6a11974088a46d42b903418eJohann 31331b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.kf_group_bits = (cpi->twopass.kf_group_bits < 0) ? 0 : cpi->twopass.kf_group_bits; 31341b362b15af34006e6a11974088a46d42b903418eJohann 31351b362b15af34006e6a11974088a46d42b903418eJohann /* Calculate the number of bits to be spent on the key frame */ 31361b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.kf_bits = (int)((double)kf_boost * ((double)cpi->twopass.kf_group_bits / (double)allocation_chunks)); 31371b362b15af34006e6a11974088a46d42b903418eJohann 31381b362b15af34006e6a11974088a46d42b903418eJohann /* Apply an additional limit for CBR */ 31391b362b15af34006e6a11974088a46d42b903418eJohann if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) 31401b362b15af34006e6a11974088a46d42b903418eJohann { 31411b362b15af34006e6a11974088a46d42b903418eJohann if (cpi->twopass.kf_bits > (int)((3 * cpi->buffer_level) >> 2)) 31421b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.kf_bits = (int)((3 * cpi->buffer_level) >> 2); 31431b362b15af34006e6a11974088a46d42b903418eJohann } 31441b362b15af34006e6a11974088a46d42b903418eJohann 31451b362b15af34006e6a11974088a46d42b903418eJohann /* If the key frame is actually easier than the average for the 31461b362b15af34006e6a11974088a46d42b903418eJohann * kf group (which does sometimes happen... eg a blank intro frame) 31471b362b15af34006e6a11974088a46d42b903418eJohann * Then use an alternate calculation based on the kf error score 31481b362b15af34006e6a11974088a46d42b903418eJohann * which should give a smaller key frame. 31491b362b15af34006e6a11974088a46d42b903418eJohann */ 31501b362b15af34006e6a11974088a46d42b903418eJohann if (kf_mod_err < kf_group_err / cpi->twopass.frames_to_key) 31511b362b15af34006e6a11974088a46d42b903418eJohann { 31521b362b15af34006e6a11974088a46d42b903418eJohann double alt_kf_grp_bits = 31531b362b15af34006e6a11974088a46d42b903418eJohann ((double)cpi->twopass.bits_left * 31541b362b15af34006e6a11974088a46d42b903418eJohann (kf_mod_err * (double)cpi->twopass.frames_to_key) / 31551b362b15af34006e6a11974088a46d42b903418eJohann DOUBLE_DIVIDE_CHECK(cpi->twopass.modified_error_left)); 31561b362b15af34006e6a11974088a46d42b903418eJohann 31571b362b15af34006e6a11974088a46d42b903418eJohann alt_kf_bits = (int)((double)kf_boost * 31581b362b15af34006e6a11974088a46d42b903418eJohann (alt_kf_grp_bits / (double)allocation_chunks)); 31591b362b15af34006e6a11974088a46d42b903418eJohann 31601b362b15af34006e6a11974088a46d42b903418eJohann if (cpi->twopass.kf_bits > alt_kf_bits) 31611b362b15af34006e6a11974088a46d42b903418eJohann { 31621b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.kf_bits = alt_kf_bits; 31631b362b15af34006e6a11974088a46d42b903418eJohann } 31641b362b15af34006e6a11974088a46d42b903418eJohann } 31651b362b15af34006e6a11974088a46d42b903418eJohann /* Else if it is much harder than other frames in the group make sure 31661b362b15af34006e6a11974088a46d42b903418eJohann * it at least receives an allocation in keeping with its relative 31671b362b15af34006e6a11974088a46d42b903418eJohann * error score 31681b362b15af34006e6a11974088a46d42b903418eJohann */ 31691b362b15af34006e6a11974088a46d42b903418eJohann else 31701b362b15af34006e6a11974088a46d42b903418eJohann { 31711b362b15af34006e6a11974088a46d42b903418eJohann alt_kf_bits = 31721b362b15af34006e6a11974088a46d42b903418eJohann (int)((double)cpi->twopass.bits_left * 31731b362b15af34006e6a11974088a46d42b903418eJohann (kf_mod_err / 31741b362b15af34006e6a11974088a46d42b903418eJohann DOUBLE_DIVIDE_CHECK(cpi->twopass.modified_error_left))); 31751b362b15af34006e6a11974088a46d42b903418eJohann 31761b362b15af34006e6a11974088a46d42b903418eJohann if (alt_kf_bits > cpi->twopass.kf_bits) 31771b362b15af34006e6a11974088a46d42b903418eJohann { 31781b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.kf_bits = alt_kf_bits; 31791b362b15af34006e6a11974088a46d42b903418eJohann } 31801b362b15af34006e6a11974088a46d42b903418eJohann } 31811b362b15af34006e6a11974088a46d42b903418eJohann 31821b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.kf_group_bits -= cpi->twopass.kf_bits; 31831b362b15af34006e6a11974088a46d42b903418eJohann /* Add in the minimum frame allowance */ 31841b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.kf_bits += cpi->min_frame_bandwidth; 31851b362b15af34006e6a11974088a46d42b903418eJohann 31861b362b15af34006e6a11974088a46d42b903418eJohann /* Peer frame bit target for this frame */ 31871b362b15af34006e6a11974088a46d42b903418eJohann cpi->per_frame_bandwidth = cpi->twopass.kf_bits; 31881b362b15af34006e6a11974088a46d42b903418eJohann 31891b362b15af34006e6a11974088a46d42b903418eJohann /* Convert to a per second bitrate */ 31901b362b15af34006e6a11974088a46d42b903418eJohann cpi->target_bandwidth = (int)(cpi->twopass.kf_bits * 319191037db265ecdd914a26e056cf69207b4f50924ehkuang cpi->output_framerate); 31921b362b15af34006e6a11974088a46d42b903418eJohann } 31931b362b15af34006e6a11974088a46d42b903418eJohann 31941b362b15af34006e6a11974088a46d42b903418eJohann /* Note the total error score of the kf group minus the key frame itself */ 31951b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.kf_group_error_left = (int)(kf_group_err - kf_mod_err); 31961b362b15af34006e6a11974088a46d42b903418eJohann 31971b362b15af34006e6a11974088a46d42b903418eJohann /* Adjust the count of total modified error left. The count of bits left 31981b362b15af34006e6a11974088a46d42b903418eJohann * is adjusted elsewhere based on real coded frame sizes 31991b362b15af34006e6a11974088a46d42b903418eJohann */ 32001b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.modified_error_left -= kf_group_err; 32011b362b15af34006e6a11974088a46d42b903418eJohann 32021b362b15af34006e6a11974088a46d42b903418eJohann if (cpi->oxcf.allow_spatial_resampling) 32031b362b15af34006e6a11974088a46d42b903418eJohann { 32041b362b15af34006e6a11974088a46d42b903418eJohann int resample_trigger = 0; 32051b362b15af34006e6a11974088a46d42b903418eJohann int last_kf_resampled = 0; 32061b362b15af34006e6a11974088a46d42b903418eJohann int kf_q; 32071b362b15af34006e6a11974088a46d42b903418eJohann int scale_val = 0; 32081b362b15af34006e6a11974088a46d42b903418eJohann int hr, hs, vr, vs; 32091b362b15af34006e6a11974088a46d42b903418eJohann int new_width = cpi->oxcf.Width; 32101b362b15af34006e6a11974088a46d42b903418eJohann int new_height = cpi->oxcf.Height; 32111b362b15af34006e6a11974088a46d42b903418eJohann 3212da49e34c1fb5e99681f4ad99c21d9cfd83eddb96Vignesh Venkatasubramanian int projected_buffer_level; 32131b362b15af34006e6a11974088a46d42b903418eJohann int tmp_q; 32141b362b15af34006e6a11974088a46d42b903418eJohann 32151b362b15af34006e6a11974088a46d42b903418eJohann double projected_bits_perframe; 32161b362b15af34006e6a11974088a46d42b903418eJohann double group_iiratio = (kf_group_intra_err - first_frame.intra_error) / (kf_group_coded_err - first_frame.coded_error); 32171b362b15af34006e6a11974088a46d42b903418eJohann double err_per_frame = kf_group_err / cpi->twopass.frames_to_key; 32181b362b15af34006e6a11974088a46d42b903418eJohann double bits_per_frame; 32191b362b15af34006e6a11974088a46d42b903418eJohann double av_bits_per_frame; 32201b362b15af34006e6a11974088a46d42b903418eJohann double effective_size_ratio; 32211b362b15af34006e6a11974088a46d42b903418eJohann 32221b362b15af34006e6a11974088a46d42b903418eJohann if ((cpi->common.Width != cpi->oxcf.Width) || (cpi->common.Height != cpi->oxcf.Height)) 32231b362b15af34006e6a11974088a46d42b903418eJohann last_kf_resampled = 1; 32241b362b15af34006e6a11974088a46d42b903418eJohann 32251b362b15af34006e6a11974088a46d42b903418eJohann /* Set back to unscaled by defaults */ 32261b362b15af34006e6a11974088a46d42b903418eJohann cpi->common.horiz_scale = NORMAL; 32271b362b15af34006e6a11974088a46d42b903418eJohann cpi->common.vert_scale = NORMAL; 32281b362b15af34006e6a11974088a46d42b903418eJohann 32291b362b15af34006e6a11974088a46d42b903418eJohann /* Calculate Average bits per frame. */ 323091037db265ecdd914a26e056cf69207b4f50924ehkuang av_bits_per_frame = cpi->oxcf.target_bandwidth / DOUBLE_DIVIDE_CHECK((double)cpi->framerate); 32311b362b15af34006e6a11974088a46d42b903418eJohann 32321b362b15af34006e6a11974088a46d42b903418eJohann /* CBR... Use the clip average as the target for deciding resample */ 32331b362b15af34006e6a11974088a46d42b903418eJohann if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) 32341b362b15af34006e6a11974088a46d42b903418eJohann { 32351b362b15af34006e6a11974088a46d42b903418eJohann bits_per_frame = av_bits_per_frame; 32361b362b15af34006e6a11974088a46d42b903418eJohann } 32371b362b15af34006e6a11974088a46d42b903418eJohann 32381b362b15af34006e6a11974088a46d42b903418eJohann /* In VBR we want to avoid downsampling in easy section unless we 32391b362b15af34006e6a11974088a46d42b903418eJohann * are under extreme pressure So use the larger of target bitrate 32401b362b15af34006e6a11974088a46d42b903418eJohann * for this section or average bitrate for sequence 32411b362b15af34006e6a11974088a46d42b903418eJohann */ 32421b362b15af34006e6a11974088a46d42b903418eJohann else 32431b362b15af34006e6a11974088a46d42b903418eJohann { 32441b362b15af34006e6a11974088a46d42b903418eJohann /* This accounts for how hard the section is... */ 32451b362b15af34006e6a11974088a46d42b903418eJohann bits_per_frame = (double) 32461b362b15af34006e6a11974088a46d42b903418eJohann (cpi->twopass.kf_group_bits / cpi->twopass.frames_to_key); 32471b362b15af34006e6a11974088a46d42b903418eJohann 32481b362b15af34006e6a11974088a46d42b903418eJohann /* Dont turn to resampling in easy sections just because they 32491b362b15af34006e6a11974088a46d42b903418eJohann * have been assigned a small number of bits 32501b362b15af34006e6a11974088a46d42b903418eJohann */ 32511b362b15af34006e6a11974088a46d42b903418eJohann if (bits_per_frame < av_bits_per_frame) 32521b362b15af34006e6a11974088a46d42b903418eJohann bits_per_frame = av_bits_per_frame; 32531b362b15af34006e6a11974088a46d42b903418eJohann } 32541b362b15af34006e6a11974088a46d42b903418eJohann 32551b362b15af34006e6a11974088a46d42b903418eJohann /* bits_per_frame should comply with our minimum */ 32561b362b15af34006e6a11974088a46d42b903418eJohann if (bits_per_frame < (cpi->oxcf.target_bandwidth * cpi->oxcf.two_pass_vbrmin_section / 100)) 32571b362b15af34006e6a11974088a46d42b903418eJohann bits_per_frame = (cpi->oxcf.target_bandwidth * cpi->oxcf.two_pass_vbrmin_section / 100); 32581b362b15af34006e6a11974088a46d42b903418eJohann 32591b362b15af34006e6a11974088a46d42b903418eJohann /* Work out if spatial resampling is necessary */ 32601b362b15af34006e6a11974088a46d42b903418eJohann kf_q = estimate_kf_group_q(cpi, err_per_frame, 32611b362b15af34006e6a11974088a46d42b903418eJohann (int)bits_per_frame, group_iiratio); 32621b362b15af34006e6a11974088a46d42b903418eJohann 32631b362b15af34006e6a11974088a46d42b903418eJohann /* If we project a required Q higher than the maximum allowed Q then 32641b362b15af34006e6a11974088a46d42b903418eJohann * make a guess at the actual size of frames in this section 32651b362b15af34006e6a11974088a46d42b903418eJohann */ 32661b362b15af34006e6a11974088a46d42b903418eJohann projected_bits_perframe = bits_per_frame; 32671b362b15af34006e6a11974088a46d42b903418eJohann tmp_q = kf_q; 32681b362b15af34006e6a11974088a46d42b903418eJohann 32691b362b15af34006e6a11974088a46d42b903418eJohann while (tmp_q > cpi->worst_quality) 32701b362b15af34006e6a11974088a46d42b903418eJohann { 32711b362b15af34006e6a11974088a46d42b903418eJohann projected_bits_perframe *= 1.04; 32721b362b15af34006e6a11974088a46d42b903418eJohann tmp_q--; 32731b362b15af34006e6a11974088a46d42b903418eJohann } 32741b362b15af34006e6a11974088a46d42b903418eJohann 32751b362b15af34006e6a11974088a46d42b903418eJohann /* Guess at buffer level at the end of the section */ 32761b362b15af34006e6a11974088a46d42b903418eJohann projected_buffer_level = (int) 32771b362b15af34006e6a11974088a46d42b903418eJohann (cpi->buffer_level - (int) 32781b362b15af34006e6a11974088a46d42b903418eJohann ((projected_bits_perframe - av_bits_per_frame) * 32791b362b15af34006e6a11974088a46d42b903418eJohann cpi->twopass.frames_to_key)); 32801b362b15af34006e6a11974088a46d42b903418eJohann 32811b362b15af34006e6a11974088a46d42b903418eJohann if (0) 32821b362b15af34006e6a11974088a46d42b903418eJohann { 32831b362b15af34006e6a11974088a46d42b903418eJohann FILE *f = fopen("Subsamle.stt", "a"); 32841b362b15af34006e6a11974088a46d42b903418eJohann fprintf(f, " %8d %8d %8d %8d %12.0f %8d %8d %8d\n", cpi->common.current_video_frame, kf_q, cpi->common.horiz_scale, cpi->common.vert_scale, kf_group_err / cpi->twopass.frames_to_key, (int)(cpi->twopass.kf_group_bits / cpi->twopass.frames_to_key), new_height, new_width); 32851b362b15af34006e6a11974088a46d42b903418eJohann fclose(f); 32861b362b15af34006e6a11974088a46d42b903418eJohann } 32871b362b15af34006e6a11974088a46d42b903418eJohann 32881b362b15af34006e6a11974088a46d42b903418eJohann /* The trigger for spatial resampling depends on the various 32891b362b15af34006e6a11974088a46d42b903418eJohann * parameters such as whether we are streaming (CBR) or VBR. 32901b362b15af34006e6a11974088a46d42b903418eJohann */ 32911b362b15af34006e6a11974088a46d42b903418eJohann if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) 32921b362b15af34006e6a11974088a46d42b903418eJohann { 32931b362b15af34006e6a11974088a46d42b903418eJohann /* Trigger resample if we are projected to fall below down 32941b362b15af34006e6a11974088a46d42b903418eJohann * sample level or resampled last time and are projected to 32951b362b15af34006e6a11974088a46d42b903418eJohann * remain below the up sample level 32961b362b15af34006e6a11974088a46d42b903418eJohann */ 32971b362b15af34006e6a11974088a46d42b903418eJohann if ((projected_buffer_level < (cpi->oxcf.resample_down_water_mark * cpi->oxcf.optimal_buffer_level / 100)) || 32981b362b15af34006e6a11974088a46d42b903418eJohann (last_kf_resampled && (projected_buffer_level < (cpi->oxcf.resample_up_water_mark * cpi->oxcf.optimal_buffer_level / 100)))) 32991b362b15af34006e6a11974088a46d42b903418eJohann resample_trigger = 1; 33001b362b15af34006e6a11974088a46d42b903418eJohann else 33011b362b15af34006e6a11974088a46d42b903418eJohann resample_trigger = 0; 33021b362b15af34006e6a11974088a46d42b903418eJohann } 33031b362b15af34006e6a11974088a46d42b903418eJohann else 33041b362b15af34006e6a11974088a46d42b903418eJohann { 330591037db265ecdd914a26e056cf69207b4f50924ehkuang int64_t clip_bits = (int64_t)(cpi->twopass.total_stats.count * cpi->oxcf.target_bandwidth / DOUBLE_DIVIDE_CHECK((double)cpi->framerate)); 33061b362b15af34006e6a11974088a46d42b903418eJohann int64_t over_spend = cpi->oxcf.starting_buffer_level - cpi->buffer_level; 33071b362b15af34006e6a11974088a46d42b903418eJohann 33081b362b15af34006e6a11974088a46d42b903418eJohann /* If triggered last time the threshold for triggering again is 33091b362b15af34006e6a11974088a46d42b903418eJohann * reduced: 33101b362b15af34006e6a11974088a46d42b903418eJohann * 33111b362b15af34006e6a11974088a46d42b903418eJohann * Projected Q higher than allowed and Overspend > 5% of total 33121b362b15af34006e6a11974088a46d42b903418eJohann * bits 33131b362b15af34006e6a11974088a46d42b903418eJohann */ 33141b362b15af34006e6a11974088a46d42b903418eJohann if ((last_kf_resampled && (kf_q > cpi->worst_quality)) || 33151b362b15af34006e6a11974088a46d42b903418eJohann ((kf_q > cpi->worst_quality) && 33161b362b15af34006e6a11974088a46d42b903418eJohann (over_spend > clip_bits / 20))) 33171b362b15af34006e6a11974088a46d42b903418eJohann resample_trigger = 1; 33181b362b15af34006e6a11974088a46d42b903418eJohann else 33191b362b15af34006e6a11974088a46d42b903418eJohann resample_trigger = 0; 33201b362b15af34006e6a11974088a46d42b903418eJohann 33211b362b15af34006e6a11974088a46d42b903418eJohann } 33221b362b15af34006e6a11974088a46d42b903418eJohann 33231b362b15af34006e6a11974088a46d42b903418eJohann if (resample_trigger) 33241b362b15af34006e6a11974088a46d42b903418eJohann { 33251b362b15af34006e6a11974088a46d42b903418eJohann while ((kf_q >= cpi->worst_quality) && (scale_val < 6)) 33261b362b15af34006e6a11974088a46d42b903418eJohann { 33271b362b15af34006e6a11974088a46d42b903418eJohann scale_val ++; 33281b362b15af34006e6a11974088a46d42b903418eJohann 33291b362b15af34006e6a11974088a46d42b903418eJohann cpi->common.vert_scale = vscale_lookup[scale_val]; 33301b362b15af34006e6a11974088a46d42b903418eJohann cpi->common.horiz_scale = hscale_lookup[scale_val]; 33311b362b15af34006e6a11974088a46d42b903418eJohann 33321b362b15af34006e6a11974088a46d42b903418eJohann Scale2Ratio(cpi->common.horiz_scale, &hr, &hs); 33331b362b15af34006e6a11974088a46d42b903418eJohann Scale2Ratio(cpi->common.vert_scale, &vr, &vs); 33341b362b15af34006e6a11974088a46d42b903418eJohann 33351b362b15af34006e6a11974088a46d42b903418eJohann new_width = ((hs - 1) + (cpi->oxcf.Width * hr)) / hs; 33361b362b15af34006e6a11974088a46d42b903418eJohann new_height = ((vs - 1) + (cpi->oxcf.Height * vr)) / vs; 33371b362b15af34006e6a11974088a46d42b903418eJohann 33381b362b15af34006e6a11974088a46d42b903418eJohann /* Reducing the area to 1/4 does not reduce the complexity 33391b362b15af34006e6a11974088a46d42b903418eJohann * (err_per_frame) to 1/4... effective_sizeratio attempts 33401b362b15af34006e6a11974088a46d42b903418eJohann * to provide a crude correction for this 33411b362b15af34006e6a11974088a46d42b903418eJohann */ 33421b362b15af34006e6a11974088a46d42b903418eJohann effective_size_ratio = (double)(new_width * new_height) / (double)(cpi->oxcf.Width * cpi->oxcf.Height); 33431b362b15af34006e6a11974088a46d42b903418eJohann effective_size_ratio = (1.0 + (3.0 * effective_size_ratio)) / 4.0; 33441b362b15af34006e6a11974088a46d42b903418eJohann 33451b362b15af34006e6a11974088a46d42b903418eJohann /* Now try again and see what Q we get with the smaller 33461b362b15af34006e6a11974088a46d42b903418eJohann * image size 33471b362b15af34006e6a11974088a46d42b903418eJohann */ 33481b362b15af34006e6a11974088a46d42b903418eJohann kf_q = estimate_kf_group_q(cpi, 33491b362b15af34006e6a11974088a46d42b903418eJohann err_per_frame * effective_size_ratio, 33501b362b15af34006e6a11974088a46d42b903418eJohann (int)bits_per_frame, group_iiratio); 33511b362b15af34006e6a11974088a46d42b903418eJohann 33521b362b15af34006e6a11974088a46d42b903418eJohann if (0) 33531b362b15af34006e6a11974088a46d42b903418eJohann { 33541b362b15af34006e6a11974088a46d42b903418eJohann FILE *f = fopen("Subsamle.stt", "a"); 33551b362b15af34006e6a11974088a46d42b903418eJohann fprintf(f, "******** %8d %8d %8d %12.0f %8d %8d %8d\n", kf_q, cpi->common.horiz_scale, cpi->common.vert_scale, kf_group_err / cpi->twopass.frames_to_key, (int)(cpi->twopass.kf_group_bits / cpi->twopass.frames_to_key), new_height, new_width); 33561b362b15af34006e6a11974088a46d42b903418eJohann fclose(f); 33571b362b15af34006e6a11974088a46d42b903418eJohann } 33581b362b15af34006e6a11974088a46d42b903418eJohann } 33591b362b15af34006e6a11974088a46d42b903418eJohann } 33601b362b15af34006e6a11974088a46d42b903418eJohann 33611b362b15af34006e6a11974088a46d42b903418eJohann if ((cpi->common.Width != new_width) || (cpi->common.Height != new_height)) 33621b362b15af34006e6a11974088a46d42b903418eJohann { 33631b362b15af34006e6a11974088a46d42b903418eJohann cpi->common.Width = new_width; 33641b362b15af34006e6a11974088a46d42b903418eJohann cpi->common.Height = new_height; 33651b362b15af34006e6a11974088a46d42b903418eJohann vp8_alloc_compressor_data(cpi); 33661b362b15af34006e6a11974088a46d42b903418eJohann } 33671b362b15af34006e6a11974088a46d42b903418eJohann } 33681b362b15af34006e6a11974088a46d42b903418eJohann} 3369