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