firstpass.c 86.30 KiB
/*
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */
#include "math.h"
#include "limits.h"
#include "block.h"
#include "onyx_int.h"
#include "variance.h"
#include "encodeintra.h"
#include "vp8/common/setupintrarecon.h"
#include "mcomp.h"
#include "firstpass.h"
#include "vpx_scale/vpxscale.h"
#include "encodemb.h"
#include "vp8/common/extend.h"
#include "vp8/common/systemdependent.h"
#include "vpx_scale/yv12extend.h"
#include "vpx_mem/vpx_mem.h"
#include "vp8/common/swapyv12buffer.h"
#include <stdio.h>
#include "rdopt.h"
#include "ratectrl.h"
#include "vp8/common/quant_common.h"
#include "vp8/common/entropymv.h"
#include "encodemv.h"
#define OUTPUT_FPF 0
#if CONFIG_RUNTIME_CPU_DETECT
#define IF_RTCD(x) (x)
#else
#define IF_RTCD(x) NULL
#endif
extern void vp9_build_block_offsets(MACROBLOCK *x);
extern void vp9_setup_block_ptrs(MACROBLOCK *x);
extern void vp9cx_frame_init_quantizer(VP8_COMP *cpi);
extern void vp9_set_mbmode_and_mvs(MACROBLOCK *x, MB_PREDICTION_MODE mb, int_mv *mv);
extern void vp9_alloc_compressor_data(VP8_COMP *cpi);
#define IIFACTOR   12.5
#define IIKFACTOR1 12.5
#define IIKFACTOR2 15.0
#define RMAX       128.0
#define GF_RMAX    96.0
#define ERR_DIVISOR   150.0
#define KF_MB_INTRA_MIN 300
#define GF_MB_INTRA_MIN 200
#define DOUBLE_DIVIDE_CHECK(X) ((X)<0?(X)-.000001:(X)+.000001)
#define POW1 (double)cpi->oxcf.two_pass_vbrbias/100.0
#define POW2 (double)cpi->oxcf.two_pass_vbrbias/100.0
static void find_next_key_frame(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame);
static int select_cq_level(int qindex) {
  int ret_val = QINDEX_RANGE - 1;
  int i;
  double target_q = (vp9_convert_qindex_to_q(qindex) * 0.5847) + 1.0;
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
for (i = 0; i < QINDEX_RANGE; i++) { if (target_q <= vp9_convert_qindex_to_q(i)) { ret_val = i; break; } } return ret_val; } // Resets the first pass file to the given position using a relative seek from the current position static void reset_fpf_position(VP8_COMP *cpi, FIRSTPASS_STATS *Position) { cpi->twopass.stats_in = Position; } static int lookup_next_frame_stats(VP8_COMP *cpi, FIRSTPASS_STATS *next_frame) { if (cpi->twopass.stats_in >= cpi->twopass.stats_in_end) return EOF; *next_frame = *cpi->twopass.stats_in; return 1; } // Read frame stats at an offset from the current position static int read_frame_stats(VP8_COMP *cpi, FIRSTPASS_STATS *frame_stats, int offset) { FIRSTPASS_STATS *fps_ptr = cpi->twopass.stats_in; // Check legality of offset if (offset >= 0) { if (&fps_ptr[offset] >= cpi->twopass.stats_in_end) return EOF; } else if (offset < 0) { if (&fps_ptr[offset] < cpi->twopass.stats_in_start) return EOF; } *frame_stats = fps_ptr[offset]; return 1; } static int input_stats(VP8_COMP *cpi, FIRSTPASS_STATS *fps) { if (cpi->twopass.stats_in >= cpi->twopass.stats_in_end) return EOF; *fps = *cpi->twopass.stats_in; cpi->twopass.stats_in = (void *)((char *)cpi->twopass.stats_in + sizeof(FIRSTPASS_STATS)); return 1; } static void output_stats(const VP8_COMP *cpi, struct vpx_codec_pkt_list *pktlist, FIRSTPASS_STATS *stats) { struct vpx_codec_cx_pkt pkt; pkt.kind = VPX_CODEC_STATS_PKT; pkt.data.twopass_stats.buf = stats; pkt.data.twopass_stats.sz = sizeof(FIRSTPASS_STATS); vpx_codec_pkt_list_add(pktlist, &pkt); // TEMP debug code #if OUTPUT_FPF { FILE *fpfile; fpfile = fopen("firstpass.stt", "a"); fprintf(fpfile, "%12.0f %12.0f %12.0f %12.0f %12.0f %12.4f %12.4f"
141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
"%12.4f %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f" "%12.0f %12.0f %12.4f %12.0f %12.0f %12.4f\n", stats->frame, stats->intra_error, stats->coded_error, stats->sr_coded_error, stats->ssim_weighted_pred_err, stats->pcnt_inter, stats->pcnt_motion, stats->pcnt_second_ref, stats->pcnt_neutral, stats->MVr, stats->mvr_abs, stats->MVc, stats->mvc_abs, stats->MVrv, stats->MVcv, stats->mv_in_out_count, stats->new_mv_count, stats->count, stats->duration); fclose(fpfile); } #endif } static void zero_stats(FIRSTPASS_STATS *section) { section->frame = 0.0; section->intra_error = 0.0; section->coded_error = 0.0; section->sr_coded_error = 0.0; section->ssim_weighted_pred_err = 0.0; section->pcnt_inter = 0.0; section->pcnt_motion = 0.0; section->pcnt_second_ref = 0.0; section->pcnt_neutral = 0.0; section->MVr = 0.0; section->mvr_abs = 0.0; section->MVc = 0.0; section->mvc_abs = 0.0; section->MVrv = 0.0; section->MVcv = 0.0; section->mv_in_out_count = 0.0; section->new_mv_count = 0.0; section->count = 0.0; section->duration = 1.0; } static void accumulate_stats(FIRSTPASS_STATS *section, FIRSTPASS_STATS *frame) { section->frame += frame->frame; section->intra_error += frame->intra_error; section->coded_error += frame->coded_error; section->sr_coded_error += frame->sr_coded_error; section->ssim_weighted_pred_err += frame->ssim_weighted_pred_err; section->pcnt_inter += frame->pcnt_inter; section->pcnt_motion += frame->pcnt_motion; section->pcnt_second_ref += frame->pcnt_second_ref; section->pcnt_neutral += frame->pcnt_neutral; section->MVr += frame->MVr; section->mvr_abs += frame->mvr_abs; section->MVc += frame->MVc; section->mvc_abs += frame->mvc_abs; section->MVrv += frame->MVrv; section->MVcv += frame->MVcv; section->mv_in_out_count += frame->mv_in_out_count; section->new_mv_count += frame->new_mv_count; section->count += frame->count; section->duration += frame->duration; }
211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
static void subtract_stats(FIRSTPASS_STATS *section, FIRSTPASS_STATS *frame) { section->frame -= frame->frame; section->intra_error -= frame->intra_error; section->coded_error -= frame->coded_error; section->sr_coded_error -= frame->sr_coded_error; section->ssim_weighted_pred_err -= frame->ssim_weighted_pred_err; section->pcnt_inter -= frame->pcnt_inter; section->pcnt_motion -= frame->pcnt_motion; section->pcnt_second_ref -= frame->pcnt_second_ref; section->pcnt_neutral -= frame->pcnt_neutral; section->MVr -= frame->MVr; section->mvr_abs -= frame->mvr_abs; section->MVc -= frame->MVc; section->mvc_abs -= frame->mvc_abs; section->MVrv -= frame->MVrv; section->MVcv -= frame->MVcv; section->mv_in_out_count -= frame->mv_in_out_count; section->new_mv_count -= frame->new_mv_count; section->count -= frame->count; section->duration -= frame->duration; } static void avg_stats(FIRSTPASS_STATS *section) { if (section->count < 1.0) return; section->intra_error /= section->count; section->coded_error /= section->count; section->sr_coded_error /= section->count; section->ssim_weighted_pred_err /= section->count; section->pcnt_inter /= section->count; section->pcnt_second_ref /= section->count; section->pcnt_neutral /= section->count; section->pcnt_motion /= section->count; section->MVr /= section->count; section->mvr_abs /= section->count; section->MVc /= section->count; section->mvc_abs /= section->count; section->MVrv /= section->count; section->MVcv /= section->count; section->mv_in_out_count /= section->count; section->duration /= section->count; } // Calculate a modified Error used in distributing bits between easier and harder frames static double calculate_modified_err(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame) { double av_err = (cpi->twopass.total_stats->ssim_weighted_pred_err / cpi->twopass.total_stats->count); double this_err = this_frame->ssim_weighted_pred_err; double modified_err; if (this_err > av_err) modified_err = av_err * pow((this_err / DOUBLE_DIVIDE_CHECK(av_err)), POW1); else modified_err = av_err * pow((this_err / DOUBLE_DIVIDE_CHECK(av_err)), POW2); return modified_err; } static const double weight_table[256] = { 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.031250, 0.062500, 0.093750, 0.125000, 0.156250, 0.187500, 0.218750, 0.250000, 0.281250, 0.312500, 0.343750, 0.375000, 0.406250, 0.437500, 0.468750, 0.500000, 0.531250, 0.562500, 0.593750, 0.625000, 0.656250, 0.687500, 0.718750, 0.750000, 0.781250, 0.812500, 0.843750, 0.875000, 0.906250, 0.937500, 0.968750, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000 }; static double simple_weight(YV12_BUFFER_CONFIG *source) { int i, j; unsigned char *src = source->y_buffer; double sum_weights = 0.0; // Loop throught the Y plane raw examining levels and creating a weight for the image i = source->y_height; do { j = source->y_width; do { sum_weights += weight_table[ *src]; src++; } while (--j); src -= source->y_width; src += source->y_stride; } while (--i); sum_weights /= (source->y_height * source->y_width); return sum_weights; } // This function returns the current per frame maximum bitrate target static int frame_max_bits(VP8_COMP *cpi) { // Max allocation for a single frame based on the max section guidelines passed in and how many bits are left int max_bits; // For VBR base this on the bits and frames left plus the two_pass_vbrmax_section rate passed in by the user 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)); // Trap case where we are out of bits if (max_bits < 0) max_bits = 0; return max_bits; } void vp9_init_first_pass(VP8_COMP *cpi) { zero_stats(cpi->twopass.total_stats); } void vp9_end_first_pass(VP8_COMP *cpi) { output_stats(cpi, cpi->output_pkt_list, cpi->twopass.total_stats); }
351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
static void zz_motion_search(VP8_COMP *cpi, MACROBLOCK *x, YV12_BUFFER_CONFIG *recon_buffer, int *best_motion_err, int recon_yoffset) { MACROBLOCKD *const xd = &x->e_mbd; BLOCK *b = &x->block[0]; BLOCKD *d = &x->e_mbd.block[0]; unsigned char *src_ptr = (*(b->base_src) + b->src); int src_stride = b->src_stride; unsigned char *ref_ptr; int ref_stride = d->pre_stride; // Set up pointers for this macro block recon buffer xd->pre.y_buffer = recon_buffer->y_buffer + recon_yoffset; ref_ptr = (unsigned char *)(*(d->base_pre) + d->pre); vp9_mse16x16(src_ptr, src_stride, ref_ptr, ref_stride, (unsigned int *)(best_motion_err)); } static void first_pass_motion_search(VP8_COMP *cpi, MACROBLOCK *x, int_mv *ref_mv, MV *best_mv, YV12_BUFFER_CONFIG *recon_buffer, int *best_motion_err, int recon_yoffset) { MACROBLOCKD *const xd = &x->e_mbd; BLOCK *b = &x->block[0]; BLOCKD *d = &x->e_mbd.block[0]; int num00; int_mv tmp_mv; int_mv ref_mv_full; int tmp_err; int step_param = 3; int further_steps = (MAX_MVSEARCH_STEPS - 1) - step_param; int n; vp8_variance_fn_ptr_t v_fn_ptr = cpi->fn_ptr[BLOCK_16X16]; int new_mv_mode_penalty = 256; // override the default variance function to use MSE v_fn_ptr.vf = vp9_mse16x16; // Set up pointers for this macro block recon buffer xd->pre.y_buffer = recon_buffer->y_buffer + recon_yoffset; // Initial step/diamond search centred on best mv tmp_mv.as_int = 0; ref_mv_full.as_mv.col = ref_mv->as_mv.col >> 3; ref_mv_full.as_mv.row = ref_mv->as_mv.row >> 3; tmp_err = cpi->diamond_search_sad(x, b, d, &ref_mv_full, &tmp_mv, step_param, x->sadperbit16, &num00, &v_fn_ptr, XMVCOST, ref_mv); if (tmp_err < INT_MAX - new_mv_mode_penalty) tmp_err += new_mv_mode_penalty; if (tmp_err < *best_motion_err) { *best_motion_err = tmp_err; best_mv->row = tmp_mv.as_mv.row; best_mv->col = tmp_mv.as_mv.col; } // Further step/diamond searches as necessary n = num00; num00 = 0; while (n < further_steps) { n++; if (num00) num00--;
421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
else { tmp_err = cpi->diamond_search_sad(x, b, d, &ref_mv_full, &tmp_mv, step_param + n, x->sadperbit16, &num00, &v_fn_ptr, XMVCOST, ref_mv); if (tmp_err < INT_MAX - new_mv_mode_penalty) tmp_err += new_mv_mode_penalty; if (tmp_err < *best_motion_err) { *best_motion_err = tmp_err; best_mv->row = tmp_mv.as_mv.row; best_mv->col = tmp_mv.as_mv.col; } } } } void vp9_first_pass(VP8_COMP *cpi) { int mb_row, mb_col; MACROBLOCK *const x = &cpi->mb; VP8_COMMON *const cm = &cpi->common; MACROBLOCKD *const xd = &x->e_mbd; int recon_yoffset, recon_uvoffset; YV12_BUFFER_CONFIG *lst_yv12 = &cm->yv12_fb[cm->lst_fb_idx]; YV12_BUFFER_CONFIG *new_yv12 = &cm->yv12_fb[cm->new_fb_idx]; YV12_BUFFER_CONFIG *gld_yv12 = &cm->yv12_fb[cm->gld_fb_idx]; int recon_y_stride = lst_yv12->y_stride; int recon_uv_stride = lst_yv12->uv_stride; int64_t intra_error = 0; int64_t coded_error = 0; int64_t sr_coded_error = 0; int sum_mvr = 0, sum_mvc = 0; int sum_mvr_abs = 0, sum_mvc_abs = 0; int sum_mvrs = 0, sum_mvcs = 0; int mvcount = 0; int intercount = 0; int second_ref_count = 0; int intrapenalty = 256; int neutral_count = 0; int new_mv_count = 0; int sum_in_vectors = 0; uint32_t lastmv_as_int = 0; int_mv zero_ref_mv; zero_ref_mv.as_int = 0; vp8_clear_system_state(); // __asm emms; x->src = * cpi->Source; xd->pre = *lst_yv12; xd->dst = *new_yv12; x->partition_info = x->pi; xd->mode_info_context = cm->mi; vp9_build_block_offsets(x); vp8_setup_block_dptrs(&x->e_mbd); vp9_setup_block_ptrs(x); // set up frame new frame for intra coded blocks vp8_setup_intra_recon(new_yv12); vp9cx_frame_init_quantizer(cpi); // Initialise the MV cost table to the defaults
491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
// if( cm->current_video_frame == 0) // if ( 0 ) { int flag[2] = {1, 1}; vp8_init_mv_probs(cm); vp9_initialize_rd_consts(cpi, cm->base_qindex + cm->y1dc_delta_q); } // for each macroblock row in image for (mb_row = 0; mb_row < cm->mb_rows; mb_row++) { int_mv best_ref_mv; best_ref_mv.as_int = 0; // reset above block coeffs xd->up_available = (mb_row != 0); recon_yoffset = (mb_row * recon_y_stride * 16); recon_uvoffset = (mb_row * recon_uv_stride * 8); // Set up limit values for motion vectors to prevent them extending outside the UMV borders x->mv_row_min = -((mb_row * 16) + (VP8BORDERINPIXELS - 16)); x->mv_row_max = ((cm->mb_rows - 1 - mb_row) * 16) + (VP8BORDERINPIXELS - 16); // for each macroblock col in image for (mb_col = 0; mb_col < cm->mb_cols; mb_col++) { int this_error; int gf_motion_error = INT_MAX; int use_dc_pred = (mb_col || mb_row) && (!mb_col || !mb_row); xd->dst.y_buffer = new_yv12->y_buffer + recon_yoffset; xd->dst.u_buffer = new_yv12->u_buffer + recon_uvoffset; xd->dst.v_buffer = new_yv12->v_buffer + recon_uvoffset; xd->left_available = (mb_col != 0); // Copy current mb to a buffer vp8_copy_mem16x16(x->src.y_buffer, x->src.y_stride, x->thismb, 16); // do intra 16x16 prediction this_error = vp9_encode_intra(cpi, x, use_dc_pred); // "intrapenalty" below deals with situations where the intra and inter error scores are very low (eg a plain black frame) // We do not have special cases in first pass for 0,0 and nearest etc so all inter modes carry an overhead cost estimate fot the mv. // When the error score is very low this causes us to pick all or lots of INTRA modes and throw lots of key frames. // This penalty adds a cost matching that of a 0,0 mv to the intra case. this_error += intrapenalty; // Cumulative intra error total intra_error += (int64_t)this_error; // Set up limit values for motion vectors to prevent them extending outside the UMV borders x->mv_col_min = -((mb_col * 16) + (VP8BORDERINPIXELS - 16)); x->mv_col_max = ((cm->mb_cols - 1 - mb_col) * 16) + (VP8BORDERINPIXELS - 16); // Other than for the first frame do a motion search if (cm->current_video_frame > 0) { int tmp_err; int motion_error = INT_MAX; int_mv mv, tmp_mv; // Simple 0,0 motion with no mv overhead zz_motion_search(cpi, x, lst_yv12, &motion_error, recon_yoffset); mv.as_int = tmp_mv.as_int = 0; // Test last reference frame using the previous best mv as the // starting point (best reference) for the search first_pass_motion_search(cpi, x, &best_ref_mv, &mv.as_mv, lst_yv12, &motion_error, recon_yoffset);
561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
// If the current best reference mv is not centred on 0,0 then do a 0,0 based search as well if (best_ref_mv.as_int) { tmp_err = INT_MAX; first_pass_motion_search(cpi, x, &zero_ref_mv, &tmp_mv.as_mv, lst_yv12, &tmp_err, recon_yoffset); if (tmp_err < motion_error) { motion_error = tmp_err; mv.as_int = tmp_mv.as_int; } } // Experimental search in an older reference frame if (cm->current_video_frame > 1) { // Simple 0,0 motion with no mv overhead zz_motion_search(cpi, x, gld_yv12, &gf_motion_error, recon_yoffset); first_pass_motion_search(cpi, x, &zero_ref_mv, &tmp_mv.as_mv, gld_yv12, &gf_motion_error, recon_yoffset); if ((gf_motion_error < motion_error) && (gf_motion_error < this_error)) { second_ref_count++; } // Reset to last frame as reference buffer xd->pre.y_buffer = lst_yv12->y_buffer + recon_yoffset; xd->pre.u_buffer = lst_yv12->u_buffer + recon_uvoffset; xd->pre.v_buffer = lst_yv12->v_buffer + recon_uvoffset; // In accumulating a score for the older reference frame // take the best of the motion predicted score and // the intra coded error (just as will be done for) // accumulation of "coded_error" for the last frame. if (gf_motion_error < this_error) sr_coded_error += gf_motion_error; else sr_coded_error += this_error; } else sr_coded_error += motion_error; /* Intra assumed best */ best_ref_mv.as_int = 0; if (motion_error <= this_error) { // Keep a count of cases where the inter and intra were // very close and very low. This helps with scene cut // detection for example in cropped clips with black bars // at the sides or top and bottom. if ((((this_error - intrapenalty) * 9) <= (motion_error * 10)) && (this_error < (2 * intrapenalty))) { neutral_count++; } mv.as_mv.row <<= 3; mv.as_mv.col <<= 3; this_error = motion_error; vp9_set_mbmode_and_mvs(x, NEWMV, &mv); xd->mode_info_context->mbmi.txfm_size = TX_4X4; vp9_encode_inter16x16y(IF_RTCD(&cpi->rtcd), x); sum_mvr += mv.as_mv.row; sum_mvr_abs += abs(mv.as_mv.row); sum_mvc += mv.as_mv.col; sum_mvc_abs += abs(mv.as_mv.col); sum_mvrs += mv.as_mv.row * mv.as_mv.row; sum_mvcs += mv.as_mv.col * mv.as_mv.col; intercount++;
631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
best_ref_mv.as_int = mv.as_int; // Was the vector non-zero if (mv.as_int) { mvcount++; // Was it different from the last non zero vector if (mv.as_int != lastmv_as_int) new_mv_count++; lastmv_as_int = mv.as_int; // Does the Row vector point inwards or outwards if (mb_row < cm->mb_rows / 2) { if (mv.as_mv.row > 0) sum_in_vectors--; else if (mv.as_mv.row < 0) sum_in_vectors++; } else if (mb_row > cm->mb_rows / 2) { if (mv.as_mv.row > 0) sum_in_vectors++; else if (mv.as_mv.row < 0) sum_in_vectors--; } // Does the Row vector point inwards or outwards if (mb_col < cm->mb_cols / 2) { if (mv.as_mv.col > 0) sum_in_vectors--; else if (mv.as_mv.col < 0) sum_in_vectors++; } else if (mb_col > cm->mb_cols / 2) { if (mv.as_mv.col > 0) sum_in_vectors++; else if (mv.as_mv.col < 0) sum_in_vectors--; } } } } else sr_coded_error += (int64_t)this_error; coded_error += (int64_t)this_error; // adjust to the next column of macroblocks x->src.y_buffer += 16; x->src.u_buffer += 8; x->src.v_buffer += 8; recon_yoffset += 16; recon_uvoffset += 8; } // adjust to the next row of mbs x->src.y_buffer += 16 * x->src.y_stride - 16 * cm->mb_cols; x->src.u_buffer += 8 * x->src.uv_stride - 8 * cm->mb_cols; x->src.v_buffer += 8 * x->src.uv_stride - 8 * cm->mb_cols; // extend the recon for intra prediction vp8_extend_mb_row(new_yv12, xd->dst.y_buffer + 16, xd->dst.u_buffer + 8, xd->dst.v_buffer + 8); vp8_clear_system_state(); // __asm emms; } vp8_clear_system_state(); // __asm emms; { double weight = 0.0; FIRSTPASS_STATS fps; fps.frame = cm->current_video_frame;
701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
fps.intra_error = intra_error >> 8; fps.coded_error = coded_error >> 8; fps.sr_coded_error = sr_coded_error >> 8; weight = simple_weight(cpi->Source); if (weight < 0.1) weight = 0.1; fps.ssim_weighted_pred_err = fps.coded_error * weight; fps.pcnt_inter = 0.0; fps.pcnt_motion = 0.0; fps.MVr = 0.0; fps.mvr_abs = 0.0; fps.MVc = 0.0; fps.mvc_abs = 0.0; fps.MVrv = 0.0; fps.MVcv = 0.0; fps.mv_in_out_count = 0.0; fps.new_mv_count = 0.0; fps.count = 1.0; fps.pcnt_inter = 1.0 * (double)intercount / cm->MBs; fps.pcnt_second_ref = 1.0 * (double)second_ref_count / cm->MBs; fps.pcnt_neutral = 1.0 * (double)neutral_count / cm->MBs; if (mvcount > 0) { fps.MVr = (double)sum_mvr / (double)mvcount; fps.mvr_abs = (double)sum_mvr_abs / (double)mvcount; fps.MVc = (double)sum_mvc / (double)mvcount; fps.mvc_abs = (double)sum_mvc_abs / (double)mvcount; fps.MVrv = ((double)sum_mvrs - (fps.MVr * fps.MVr / (double)mvcount)) / (double)mvcount; fps.MVcv = ((double)sum_mvcs - (fps.MVc * fps.MVc / (double)mvcount)) / (double)mvcount; fps.mv_in_out_count = (double)sum_in_vectors / (double)(mvcount * 2); fps.new_mv_count = new_mv_count; fps.pcnt_motion = 1.0 * (double)mvcount / cpi->common.MBs; } // TODO: handle the case when duration is set to 0, or something less // than the full time between subsequent cpi->source_time_stamp s . fps.duration = cpi->source->ts_end - cpi->source->ts_start; // don't want to do output stats with a stack variable! memcpy(cpi->twopass.this_frame_stats, &fps, sizeof(FIRSTPASS_STATS)); output_stats(cpi, cpi->output_pkt_list, cpi->twopass.this_frame_stats); accumulate_stats(cpi->twopass.total_stats, &fps); } // Copy the previous Last Frame back into gf and and arf buffers if // the prediction is good enough... but also dont allow it to lag too far if ((cpi->twopass.sr_update_lag > 3) || ((cm->current_video_frame > 0) && (cpi->twopass.this_frame_stats->pcnt_inter > 0.20) && ((cpi->twopass.this_frame_stats->intra_error / cpi->twopass.this_frame_stats->coded_error) > 2.0))) { vp8_yv12_copy_frame_ptr(lst_yv12, gld_yv12); cpi->twopass.sr_update_lag = 1; } else cpi->twopass.sr_update_lag++; // swap frame pointers so last frame refers to the frame we just compressed vp8_swap_yv12_buffer(lst_yv12, new_yv12); vp8_yv12_extend_frame_borders(lst_yv12); // Special case for the first frame. Copy into the GF buffer as a second reference.
771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
if (cm->current_video_frame == 0) { vp8_yv12_copy_frame_ptr(lst_yv12, gld_yv12); } // use this to see what the first pass reconstruction looks like if (0) { char filename[512]; FILE *recon_file; sprintf(filename, "enc%04d.yuv", (int) cm->current_video_frame); if (cm->current_video_frame == 0) recon_file = fopen(filename, "wb"); else recon_file = fopen(filename, "ab"); if (fwrite(lst_yv12->buffer_alloc, lst_yv12->frame_size, 1, recon_file)); fclose(recon_file); } cm->current_video_frame++; } // Estimate a cost per mb attributable to overheads such as the coding of // modes and motion vectors. // Currently simplistic in its assumptions for testing. // static double bitcost(double prob) { return -(log(prob) / log(2.0)); } static long long estimate_modemvcost(VP8_COMP *cpi, FIRSTPASS_STATS *fpstats) { int mv_cost; int mode_cost; double av_pct_inter = fpstats->pcnt_inter / fpstats->count; double av_pct_motion = fpstats->pcnt_motion / fpstats->count; double av_intra = (1.0 - av_pct_inter); double zz_cost; double motion_cost; double intra_cost; zz_cost = bitcost(av_pct_inter - av_pct_motion); motion_cost = bitcost(av_pct_motion); intra_cost = bitcost(av_intra); // Estimate of extra bits per mv overhead for mbs // << 9 is the normalization to the (bits * 512) used in vp9_bits_per_mb mv_cost = ((int)(fpstats->new_mv_count / fpstats->count) * 8) << 9; // Crude estimate of overhead cost from modes // << 9 is the normalization to (bits * 512) used in vp9_bits_per_mb mode_cost = (int)((((av_pct_inter - av_pct_motion) * zz_cost) + (av_pct_motion * motion_cost) + (av_intra * intra_cost)) * cpi->common.MBs) << 9; // return mv_cost + mode_cost; // TODO PGW Fix overhead costs for extended Q range return 0; } static double calc_correction_factor(double err_per_mb, double err_divisor, double pt_low,
841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
double pt_high, int Q) { double power_term; double error_term = err_per_mb / err_divisor; double correction_factor; // Adjustment based on actual quantizer to power term. power_term = (vp9_convert_qindex_to_q(Q) * 0.01) + pt_low; power_term = (power_term > pt_high) ? pt_high : power_term; // Adjustments to error term // TBD // Calculate correction factor correction_factor = pow(error_term, power_term); // Clip range correction_factor = (correction_factor < 0.05) ? 0.05 : (correction_factor > 2.0) ? 2.0 : correction_factor; return correction_factor; } // Given a current maxQ value sets a range for future values. // PGW TODO.. // This code removes direct dependency on QIndex to determin the range // (now uses the actual quantizer) but has not been tuned. static void adjust_maxq_qrange(VP8_COMP *cpi) { int i; double q; // Set the max corresponding to cpi->avg_q * 2.0 q = cpi->avg_q * 2.0; cpi->twopass.maxq_max_limit = cpi->worst_quality; for (i = cpi->best_quality; i <= cpi->worst_quality; i++) { cpi->twopass.maxq_max_limit = i; if (vp9_convert_qindex_to_q(i) >= q) break; } // Set the min corresponding to cpi->avg_q * 0.5 q = cpi->avg_q * 0.5; cpi->twopass.maxq_min_limit = cpi->best_quality; for (i = cpi->worst_quality; i >= cpi->best_quality; i--) { cpi->twopass.maxq_min_limit = i; if (vp9_convert_qindex_to_q(i) <= q) break; } } static int estimate_max_q(VP8_COMP *cpi, FIRSTPASS_STATS *fpstats, int section_target_bandwitdh, int overhead_bits) { int Q; int num_mbs = cpi->common.MBs; int target_norm_bits_per_mb; double section_err = (fpstats->coded_error / fpstats->count); double sr_err_diff; double sr_correction; double err_per_mb = section_err / num_mbs; double err_correction_factor; double speed_correction = 1.0; int overhead_bits_per_mb; if (section_target_bandwitdh <= 0) return cpi->twopass.maxq_max_limit; // Highest value allowed
911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
target_norm_bits_per_mb = (section_target_bandwitdh < (1 << 20)) ? (512 * section_target_bandwitdh) / num_mbs : 512 * (section_target_bandwitdh / num_mbs); // Look at the drop in prediction quality between the last frame // and the GF buffer (which contained an older frame). sr_err_diff = (fpstats->sr_coded_error - fpstats->coded_error) / (fpstats->count * cpi->common.MBs); sr_correction = (sr_err_diff / 32.0); sr_correction = pow(sr_correction, 0.25); if (sr_correction < 0.75) sr_correction = 0.75; else if (sr_correction > 1.25) sr_correction = 1.25; // Calculate a corrective factor based on a rolling ratio of bits spent // vs target bits if ((cpi->rolling_target_bits > 0) && (cpi->active_worst_quality < cpi->worst_quality)) { double rolling_ratio; rolling_ratio = (double)cpi->rolling_actual_bits / (double)cpi->rolling_target_bits; if (rolling_ratio < 0.95) cpi->twopass.est_max_qcorrection_factor -= 0.005; else if (rolling_ratio > 1.05) cpi->twopass.est_max_qcorrection_factor += 0.005; cpi->twopass.est_max_qcorrection_factor = (cpi->twopass.est_max_qcorrection_factor < 0.1) ? 0.1 : (cpi->twopass.est_max_qcorrection_factor > 10.0) ? 10.0 : cpi->twopass.est_max_qcorrection_factor; } // Corrections for higher compression speed settings // (reduced compression expected) if (cpi->compressor_speed == 1) { if (cpi->oxcf.cpu_used <= 5) speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04); else speed_correction = 1.25; } // Estimate of overhead bits per mb // Correction to overhead bits for min allowed Q. // PGW TODO.. This code is broken for the extended Q range // for now overhead set to 0. overhead_bits_per_mb = overhead_bits / num_mbs; overhead_bits_per_mb *= pow(0.98, (double)cpi->twopass.maxq_min_limit); // Try and pick a max Q that will be high enough to encode the // content at the given rate. for (Q = cpi->twopass.maxq_min_limit; Q < cpi->twopass.maxq_max_limit; Q++) { int bits_per_mb_at_this_q; err_correction_factor = calc_correction_factor(err_per_mb, ERR_DIVISOR, 0.4, 0.90, Q) * sr_correction * speed_correction * cpi->twopass.est_max_qcorrection_factor; if (err_correction_factor < 0.05) err_correction_factor = 0.05; else if (err_correction_factor > 5.0) err_correction_factor = 5.0; bits_per_mb_at_this_q =
981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
vp9_bits_per_mb(INTER_FRAME, Q) + overhead_bits_per_mb; bits_per_mb_at_this_q = (int)(.5 + err_correction_factor * (double)bits_per_mb_at_this_q); // Mode and motion overhead // As Q rises in real encode loop rd code will force overhead down // We make a crude adjustment for this here as *.98 per Q step. // PGW TODO.. This code is broken for the extended Q range // for now overhead set to 0. // overhead_bits_per_mb = (int)((double)overhead_bits_per_mb * 0.98); if (bits_per_mb_at_this_q <= target_norm_bits_per_mb) break; } // Restriction on active max q for constrained quality mode. if ((cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) && (Q < cpi->cq_target_quality)) { Q = cpi->cq_target_quality; } // Adjust maxq_min_limit and maxq_max_limit limits based on // averaga q observed in clip for non kf/gf/arf frames // Give average a chance to settle though. // PGW TODO.. This code is broken for the extended Q range if ((cpi->ni_frames > ((unsigned int)cpi->twopass.total_stats->count >> 8)) && (cpi->ni_frames > 150)) { adjust_maxq_qrange(cpi); } return Q; } // For cq mode estimate a cq level that matches the observed // complexity and data rate. static int estimate_cq(VP8_COMP *cpi, FIRSTPASS_STATS *fpstats, int section_target_bandwitdh, int overhead_bits) { int Q; int num_mbs = cpi->common.MBs; int target_norm_bits_per_mb; double section_err = (fpstats->coded_error / fpstats->count); double err_per_mb = section_err / num_mbs; double err_correction_factor; double sr_err_diff; double sr_correction; double speed_correction = 1.0; double clip_iiratio; double clip_iifactor; int overhead_bits_per_mb; target_norm_bits_per_mb = (section_target_bandwitdh < (1 << 20)) ? (512 * section_target_bandwitdh) / num_mbs : 512 * (section_target_bandwitdh / num_mbs); // Estimate of overhead bits per mb overhead_bits_per_mb = overhead_bits / num_mbs; // Corrections for higher compression speed settings // (reduced compression expected) if (cpi->compressor_speed == 1) { if (cpi->oxcf.cpu_used <= 5) speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04); else speed_correction = 1.25;
1051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120
} // Look at the drop in prediction quality between the last frame // and the GF buffer (which contained an older frame). sr_err_diff = (fpstats->sr_coded_error - fpstats->coded_error) / (fpstats->count * cpi->common.MBs); sr_correction = (sr_err_diff / 32.0); sr_correction = pow(sr_correction, 0.25); if (sr_correction < 0.75) sr_correction = 0.75; else if (sr_correction > 1.25) sr_correction = 1.25; // II ratio correction factor for clip as a whole clip_iiratio = cpi->twopass.total_stats->intra_error / DOUBLE_DIVIDE_CHECK(cpi->twopass.total_stats->coded_error); clip_iifactor = 1.0 - ((clip_iiratio - 10.0) * 0.025); if (clip_iifactor < 0.80) clip_iifactor = 0.80; // Try and pick a Q that can encode the content at the given rate. for (Q = 0; Q < MAXQ; Q++) { int bits_per_mb_at_this_q; // Error per MB based correction factor err_correction_factor = calc_correction_factor(err_per_mb, 100.0, 0.4, 0.90, Q) * sr_correction * speed_correction * clip_iifactor; if (err_correction_factor < 0.05) err_correction_factor = 0.05; else if (err_correction_factor > 5.0) err_correction_factor = 5.0; bits_per_mb_at_this_q = vp9_bits_per_mb(INTER_FRAME, Q) + overhead_bits_per_mb; bits_per_mb_at_this_q = (int)(.5 + err_correction_factor * (double)bits_per_mb_at_this_q); // Mode and motion overhead // As Q rises in real encode loop rd code will force overhead down // We make a crude adjustment for this here as *.98 per Q step. // PGW TODO.. This code is broken for the extended Q range // for now overhead set to 0. overhead_bits_per_mb = (int)((double)overhead_bits_per_mb * 0.98); if (bits_per_mb_at_this_q <= target_norm_bits_per_mb) break; } // Clip value to range "best allowed to (worst allowed - 1)" Q = select_cq_level(Q); if (Q >= cpi->worst_quality) Q = cpi->worst_quality - 1; if (Q < cpi->best_quality) Q = cpi->best_quality; return Q; } extern void vp9_new_frame_rate(VP8_COMP *cpi, double framerate); void vp9_init_second_pass(VP8_COMP *cpi) { FIRSTPASS_STATS this_frame; FIRSTPASS_STATS *start_pos; double lower_bounds_min_rate = FRAME_OVERHEAD_BITS * cpi->oxcf.frame_rate;
1121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190
double two_pass_min_rate = (double)(cpi->oxcf.target_bandwidth * cpi->oxcf.two_pass_vbrmin_section / 100); if (two_pass_min_rate < lower_bounds_min_rate) two_pass_min_rate = lower_bounds_min_rate; zero_stats(cpi->twopass.total_stats); zero_stats(cpi->twopass.total_left_stats); if (!cpi->twopass.stats_in_end) return; *cpi->twopass.total_stats = *cpi->twopass.stats_in_end; *cpi->twopass.total_left_stats = *cpi->twopass.total_stats; // each frame can have a different duration, as the frame rate in the source // isn't guaranteed to be constant. The frame rate prior to the first frame // encoded in the second pass is a guess. However the sum duration is not. // Its calculated based on the actual durations of all frames from the first // pass. vp9_new_frame_rate(cpi, 10000000.0 * cpi->twopass.total_stats->count / cpi->twopass.total_stats->duration); cpi->output_frame_rate = cpi->oxcf.frame_rate; cpi->twopass.bits_left = (int64_t)(cpi->twopass.total_stats->duration * cpi->oxcf.target_bandwidth / 10000000.0); cpi->twopass.bits_left -= (int64_t)(cpi->twopass.total_stats->duration * two_pass_min_rate / 10000000.0); // Calculate a minimum intra value to be used in determining the IIratio // scores used in the second pass. We have this minimum to make sure // that clips that are static but "low complexity" in the intra domain // are still boosted appropriately for KF/GF/ARF cpi->twopass.kf_intra_err_min = KF_MB_INTRA_MIN * cpi->common.MBs; cpi->twopass.gf_intra_err_min = GF_MB_INTRA_MIN * cpi->common.MBs; // This variable monitors how far behind the second ref update is lagging cpi->twopass.sr_update_lag = 1; // Scan the first pass file and calculate an average Intra / Inter error score ratio for the sequence { double sum_iiratio = 0.0; double IIRatio; start_pos = cpi->twopass.stats_in; // Note starting "file" position while (input_stats(cpi, &this_frame) != EOF) { IIRatio = this_frame.intra_error / DOUBLE_DIVIDE_CHECK(this_frame.coded_error); IIRatio = (IIRatio < 1.0) ? 1.0 : (IIRatio > 20.0) ? 20.0 : IIRatio; sum_iiratio += IIRatio; } cpi->twopass.avg_iiratio = sum_iiratio / DOUBLE_DIVIDE_CHECK((double)cpi->twopass.total_stats->count); // Reset file position reset_fpf_position(cpi, start_pos); } // Scan the first pass file and calculate a modified total error based upon the bias/power function // used to allocate bits { start_pos = cpi->twopass.stats_in; // Note starting "file" position cpi->twopass.modified_error_total = 0.0; cpi->twopass.modified_error_used = 0.0; while (input_stats(cpi, &this_frame) != EOF) { cpi->twopass.modified_error_total += calculate_modified_err(cpi, &this_frame); } cpi->twopass.modified_error_left = cpi->twopass.modified_error_total; reset_fpf_position(cpi, start_pos); // Reset file position