PageRenderTime 161ms CodeModel.GetById 30ms app.highlight 114ms RepoModel.GetById 1ms app.codeStats 1ms

/media/libvpx/vp8/encoder/onyx_if.c

http://github.com/zpao/v8monkey
C | 5152 lines | 3740 code | 972 blank | 440 comment | 740 complexity | 724943c382813bdcad79d2b2804f0307 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   1/*
   2 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
   3 *
   4 *  Use of this source code is governed by a BSD-style license
   5 *  that can be found in the LICENSE file in the root of the source
   6 *  tree. An additional intellectual property rights grant can be found
   7 *  in the file PATENTS.  All contributing project authors may
   8 *  be found in the AUTHORS file in the root of the source tree.
   9 */
  10
  11
  12#include "vpx_config.h"
  13#include "vp8/common/onyxc_int.h"
  14#include "onyx_int.h"
  15#include "vp8/common/systemdependent.h"
  16#include "quantize.h"
  17#include "vp8/common/alloccommon.h"
  18#include "mcomp.h"
  19#include "firstpass.h"
  20#include "psnr.h"
  21#include "vpx_scale/vpxscale.h"
  22#include "vp8/common/extend.h"
  23#include "ratectrl.h"
  24#include "vp8/common/quant_common.h"
  25#include "segmentation.h"
  26#include "vp8/common/g_common.h"
  27#include "vpx_scale/yv12extend.h"
  28#if CONFIG_POSTPROC
  29#include "vp8/common/postproc.h"
  30#endif
  31#include "vpx_mem/vpx_mem.h"
  32#include "vp8/common/swapyv12buffer.h"
  33#include "vp8/common/threading.h"
  34#include "vpx_ports/vpx_timer.h"
  35#include "temporal_filter.h"
  36#if ARCH_ARM
  37#include "vpx_ports/arm.h"
  38#endif
  39
  40#include <math.h>
  41#include <stdio.h>
  42#include <limits.h>
  43
  44#if CONFIG_RUNTIME_CPU_DETECT
  45#define IF_RTCD(x) (x)
  46#define RTCD(x) &cpi->common.rtcd.x
  47#else
  48#define IF_RTCD(x) NULL
  49#define RTCD(x) NULL
  50#endif
  51
  52extern void vp8cx_pick_filter_level_fast(YV12_BUFFER_CONFIG *sd, VP8_COMP *cpi);
  53extern void vp8cx_set_alt_lf_level(VP8_COMP *cpi, int filt_val);
  54extern void vp8cx_pick_filter_level(YV12_BUFFER_CONFIG *sd, VP8_COMP *cpi);
  55
  56extern void vp8_dmachine_specific_config(VP8_COMP *cpi);
  57extern void vp8_cmachine_specific_config(VP8_COMP *cpi);
  58extern void vp8_deblock_frame(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *post, int filt_lvl, int low_var_thresh, int flag);
  59extern void print_parms(VP8_CONFIG *ocf, char *filenam);
  60extern unsigned int vp8_get_processor_freq();
  61extern void print_tree_update_probs();
  62extern void vp8cx_create_encoder_threads(VP8_COMP *cpi);
  63extern void vp8cx_remove_encoder_threads(VP8_COMP *cpi);
  64#if HAVE_ARMV7
  65extern void vp8_yv12_copy_frame_func_neon(YV12_BUFFER_CONFIG *src_ybc, YV12_BUFFER_CONFIG *dst_ybc);
  66extern void vp8_yv12_copy_src_frame_func_neon(YV12_BUFFER_CONFIG *src_ybc, YV12_BUFFER_CONFIG *dst_ybc);
  67#endif
  68
  69int vp8_estimate_entropy_savings(VP8_COMP *cpi);
  70int vp8_calc_ss_err(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *dest, const vp8_variance_rtcd_vtable_t *rtcd);
  71
  72extern void vp8_temporal_filter_prepare_c(VP8_COMP *cpi, int distance);
  73
  74static void set_default_lf_deltas(VP8_COMP *cpi);
  75
  76extern const int vp8_gf_interval_table[101];
  77
  78#if CONFIG_INTERNAL_STATS
  79#include "math.h"
  80
  81extern double vp8_calc_ssim
  82(
  83    YV12_BUFFER_CONFIG *source,
  84    YV12_BUFFER_CONFIG *dest,
  85    int lumamask,
  86    double *weight,
  87    const vp8_variance_rtcd_vtable_t *rtcd
  88);
  89
  90
  91extern double vp8_calc_ssimg
  92(
  93    YV12_BUFFER_CONFIG *source,
  94    YV12_BUFFER_CONFIG *dest,
  95    double *ssim_y,
  96    double *ssim_u,
  97    double *ssim_v,
  98    const vp8_variance_rtcd_vtable_t *rtcd
  99);
 100
 101
 102#endif
 103
 104
 105#ifdef OUTPUT_YUV_SRC
 106FILE *yuv_file;
 107#endif
 108
 109#if 0
 110FILE *framepsnr;
 111FILE *kf_list;
 112FILE *keyfile;
 113#endif
 114
 115#if 0
 116extern int skip_true_count;
 117extern int skip_false_count;
 118#endif
 119
 120
 121#ifdef ENTROPY_STATS
 122extern int intra_mode_stats[10][10][10];
 123#endif
 124
 125#ifdef SPEEDSTATS
 126unsigned int frames_at_speed[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
 127unsigned int tot_pm = 0;
 128unsigned int cnt_pm = 0;
 129unsigned int tot_ef = 0;
 130unsigned int cnt_ef = 0;
 131#endif
 132
 133#ifdef MODE_STATS
 134extern unsigned __int64 Sectionbits[50];
 135extern int y_modes[5]  ;
 136extern int uv_modes[4] ;
 137extern int b_modes[10]  ;
 138
 139extern int inter_y_modes[10] ;
 140extern int inter_uv_modes[4] ;
 141extern unsigned int inter_b_modes[15];
 142#endif
 143
 144extern void (*vp8_short_fdct4x4)(short *input, short *output, int pitch);
 145extern void (*vp8_short_fdct8x4)(short *input, short *output, int pitch);
 146
 147extern const int vp8_bits_per_mb[2][QINDEX_RANGE];
 148
 149extern const int qrounding_factors[129];
 150extern const int qzbin_factors[129];
 151extern void vp8cx_init_quantizer(VP8_COMP *cpi);
 152extern const int vp8cx_base_skip_false_prob[128];
 153
 154// Tables relating active max Q to active min Q
 155static const int kf_low_motion_minq[QINDEX_RANGE] =
 156{
 157    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
 158    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
 159    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
 160    0,0,0,0,1,1,1,1,1,1,1,1,2,2,2,2,
 161    3,3,3,3,3,3,4,4,4,5,5,5,5,5,6,6,
 162    6,6,7,7,8,8,8,8,9,9,10,10,10,10,11,11,
 163    11,11,12,12,13,13,13,13,14,14,15,15,15,15,16,16,
 164    16,16,17,17,18,18,18,18,19,20,20,21,21,22,23,23
 165};
 166static const int kf_high_motion_minq[QINDEX_RANGE] =
 167{
 168    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
 169    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
 170    1,1,1,1,1,1,1,1,2,2,2,2,3,3,3,3,
 171    3,3,3,3,4,4,4,4,5,5,5,5,5,5,6,6,
 172    6,6,7,7,8,8,8,8,9,9,10,10,10,10,11,11,
 173    11,11,12,12,13,13,13,13,14,14,15,15,15,15,16,16,
 174    16,16,17,17,18,18,18,18,19,19,20,20,20,20,21,21,
 175    21,21,22,22,23,23,24,25,25,26,26,27,28,28,29,30
 176};
 177static const int gf_low_motion_minq[QINDEX_RANGE] =
 178{
 179    0,0,0,0,1,1,1,1,1,1,1,1,2,2,2,2,
 180    3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,
 181    7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10,
 182    11,11,12,12,13,13,14,14,15,15,16,16,17,17,18,18,
 183    19,19,20,20,21,21,22,22,23,23,24,24,25,25,26,26,
 184    27,27,28,28,29,29,30,30,31,31,32,32,33,33,34,34,
 185    35,35,36,36,37,37,38,38,39,39,40,40,41,41,42,42,
 186    43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58
 187};
 188static const int gf_mid_motion_minq[QINDEX_RANGE] =
 189{
 190    0,0,0,0,1,1,1,1,1,1,2,2,3,3,3,4,
 191    4,4,5,5,5,6,6,6,7,7,7,8,8,8,9,9,
 192    9,10,10,10,10,11,11,11,12,12,12,12,13,13,13,14,
 193    14,14,15,15,16,16,17,17,18,18,19,19,20,20,21,21,
 194    22,22,23,23,24,24,25,25,26,26,27,27,28,28,29,29,
 195    30,30,31,31,32,32,33,33,34,34,35,35,36,36,37,37,
 196    38,39,39,40,40,41,41,42,42,43,43,44,45,46,47,48,
 197    49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64
 198};
 199static const int gf_high_motion_minq[QINDEX_RANGE] =
 200{
 201    0,0,0,0,1,1,1,1,1,2,2,2,3,3,3,4,
 202    4,4,5,5,5,6,6,6,7,7,7,8,8,8,9,9,
 203    9,10,10,10,11,11,12,12,13,13,14,14,15,15,16,16,
 204    17,17,18,18,19,19,20,20,21,21,22,22,23,23,24,24,
 205    25,25,26,26,27,27,28,28,29,29,30,30,31,31,32,32,
 206    33,33,34,34,35,35,36,36,37,37,38,38,39,39,40,40,
 207    41,41,42,42,43,44,45,46,47,48,49,50,51,52,53,54,
 208    55,56,57,58,59,60,62,64,66,68,70,72,74,76,78,80
 209};
 210static const int inter_minq[QINDEX_RANGE] =
 211{
 212    0,0,1,1,2,3,3,4,4,5,6,6,7,8,8,9,
 213    9,10,11,11,12,13,13,14,15,15,16,17,17,18,19,20,
 214    20,21,22,22,23,24,24,25,26,27,27,28,29,30,30,31,
 215    32,33,33,34,35,36,36,37,38,39,39,40,41,42,42,43,
 216    44,45,46,46,47,48,49,50,50,51,52,53,54,55,55,56,
 217    57,58,59,60,60,61,62,63,64,65,66,67,67,68,69,70,
 218    71,72,73,74,75,75,76,77,78,79,80,81,82,83,84,85,
 219    86,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100
 220};
 221
 222void vp8_initialize()
 223{
 224    static int init_done = 0;
 225
 226    if (!init_done)
 227    {
 228        vp8_scale_machine_specific_config();
 229        vp8_initialize_common();
 230        //vp8_dmachine_specific_config();
 231        vp8_tokenize_initialize();
 232
 233        init_done = 1;
 234    }
 235}
 236#ifdef PACKET_TESTING
 237extern FILE *vpxlogc;
 238#endif
 239
 240static void setup_features(VP8_COMP *cpi)
 241{
 242    // Set up default state for MB feature flags
 243    cpi->mb.e_mbd.segmentation_enabled = 0;
 244    cpi->mb.e_mbd.update_mb_segmentation_map = 0;
 245    cpi->mb.e_mbd.update_mb_segmentation_data = 0;
 246    vpx_memset(cpi->mb.e_mbd.mb_segment_tree_probs, 255, sizeof(cpi->mb.e_mbd.mb_segment_tree_probs));
 247    vpx_memset(cpi->mb.e_mbd.segment_feature_data, 0, sizeof(cpi->mb.e_mbd.segment_feature_data));
 248
 249    cpi->mb.e_mbd.mode_ref_lf_delta_enabled = 0;
 250    cpi->mb.e_mbd.mode_ref_lf_delta_update = 0;
 251    vpx_memset(cpi->mb.e_mbd.ref_lf_deltas, 0, sizeof(cpi->mb.e_mbd.ref_lf_deltas));
 252    vpx_memset(cpi->mb.e_mbd.mode_lf_deltas, 0, sizeof(cpi->mb.e_mbd.mode_lf_deltas));
 253    vpx_memset(cpi->mb.e_mbd.last_ref_lf_deltas, 0, sizeof(cpi->mb.e_mbd.ref_lf_deltas));
 254    vpx_memset(cpi->mb.e_mbd.last_mode_lf_deltas, 0, sizeof(cpi->mb.e_mbd.mode_lf_deltas));
 255
 256    set_default_lf_deltas(cpi);
 257
 258}
 259
 260
 261static void dealloc_compressor_data(VP8_COMP *cpi)
 262{
 263    vpx_free(cpi->tplist);
 264    cpi->tplist = NULL;
 265
 266    // Delete last frame MV storage buffers
 267    vpx_free(cpi->lfmv);
 268    cpi->lfmv = 0;
 269
 270    vpx_free(cpi->lf_ref_frame_sign_bias);
 271    cpi->lf_ref_frame_sign_bias = 0;
 272
 273    vpx_free(cpi->lf_ref_frame);
 274    cpi->lf_ref_frame = 0;
 275
 276    // Delete sementation map
 277    vpx_free(cpi->segmentation_map);
 278    cpi->segmentation_map = 0;
 279
 280    vpx_free(cpi->active_map);
 281    cpi->active_map = 0;
 282
 283    vp8_de_alloc_frame_buffers(&cpi->common);
 284
 285    vp8_yv12_de_alloc_frame_buffer(&cpi->last_frame_uf);
 286    vp8_yv12_de_alloc_frame_buffer(&cpi->scaled_source);
 287#if VP8_TEMPORAL_ALT_REF
 288    vp8_yv12_de_alloc_frame_buffer(&cpi->alt_ref_buffer);
 289#endif
 290    vp8_lookahead_destroy(cpi->lookahead);
 291
 292    vpx_free(cpi->tok);
 293    cpi->tok = 0;
 294
 295    // Structure used to monitor GF usage
 296    vpx_free(cpi->gf_active_flags);
 297    cpi->gf_active_flags = 0;
 298
 299    // Activity mask based per mb zbin adjustments
 300    vpx_free(cpi->mb_activity_map);
 301    cpi->mb_activity_map = 0;
 302    vpx_free(cpi->mb_norm_activity_map);
 303    cpi->mb_norm_activity_map = 0;
 304
 305    vpx_free(cpi->mb.pip);
 306    cpi->mb.pip = 0;
 307
 308#if !(CONFIG_REALTIME_ONLY)
 309    vpx_free(cpi->twopass.total_stats);
 310    cpi->twopass.total_stats = 0;
 311
 312    vpx_free(cpi->twopass.this_frame_stats);
 313    cpi->twopass.this_frame_stats = 0;
 314#endif
 315}
 316
 317static void enable_segmentation(VP8_PTR ptr)
 318{
 319    VP8_COMP *cpi = (VP8_COMP *)(ptr);
 320
 321    // Set the appropriate feature bit
 322    cpi->mb.e_mbd.segmentation_enabled = 1;
 323    cpi->mb.e_mbd.update_mb_segmentation_map = 1;
 324    cpi->mb.e_mbd.update_mb_segmentation_data = 1;
 325}
 326static void disable_segmentation(VP8_PTR ptr)
 327{
 328    VP8_COMP *cpi = (VP8_COMP *)(ptr);
 329
 330    // Clear the appropriate feature bit
 331    cpi->mb.e_mbd.segmentation_enabled = 0;
 332}
 333
 334// Valid values for a segment are 0 to 3
 335// Segmentation map is arrange as [Rows][Columns]
 336static void set_segmentation_map(VP8_PTR ptr, unsigned char *segmentation_map)
 337{
 338    VP8_COMP *cpi = (VP8_COMP *)(ptr);
 339
 340    // Copy in the new segmentation map
 341    vpx_memcpy(cpi->segmentation_map, segmentation_map, (cpi->common.mb_rows * cpi->common.mb_cols));
 342
 343    // Signal that the map should be updated.
 344    cpi->mb.e_mbd.update_mb_segmentation_map = 1;
 345    cpi->mb.e_mbd.update_mb_segmentation_data = 1;
 346}
 347
 348// The values given for each segment can be either deltas (from the default value chosen for the frame) or absolute values.
 349//
 350// Valid range for abs values is (0-127 for MB_LVL_ALT_Q) , (0-63 for SEGMENT_ALT_LF)
 351// Valid range for delta values are (+/-127 for MB_LVL_ALT_Q) , (+/-63 for SEGMENT_ALT_LF)
 352//
 353// abs_delta = SEGMENT_DELTADATA (deltas) abs_delta = SEGMENT_ABSDATA (use the absolute values given).
 354//
 355//
 356static void set_segment_data(VP8_PTR ptr, signed char *feature_data, unsigned char abs_delta)
 357{
 358    VP8_COMP *cpi = (VP8_COMP *)(ptr);
 359
 360    cpi->mb.e_mbd.mb_segement_abs_delta = abs_delta;
 361    vpx_memcpy(cpi->segment_feature_data, feature_data, sizeof(cpi->segment_feature_data));
 362}
 363
 364
 365static void segmentation_test_function(VP8_PTR ptr)
 366{
 367    VP8_COMP *cpi = (VP8_COMP *)(ptr);
 368
 369    unsigned char *seg_map;
 370    signed char feature_data[MB_LVL_MAX][MAX_MB_SEGMENTS];
 371
 372    // Create a temporary map for segmentation data.
 373    CHECK_MEM_ERROR(seg_map, vpx_calloc(cpi->common.mb_rows * cpi->common.mb_cols, 1));
 374
 375    // MB loop to set local segmentation map
 376    /*for ( i = 0; i < cpi->common.mb_rows; i++ )
 377    {
 378        for ( j = 0; j < cpi->common.mb_cols; j++ )
 379        {
 380            //seg_map[(i*cpi->common.mb_cols) + j] = (j % 2) + ((i%2)* 2);
 381            //if ( j < cpi->common.mb_cols/2 )
 382
 383            // Segment 1 around the edge else 0
 384            if ( (i == 0) || (j == 0) || (i == (cpi->common.mb_rows-1)) || (j == (cpi->common.mb_cols-1)) )
 385                seg_map[(i*cpi->common.mb_cols) + j] = 1;
 386            //else if ( (i < 2) || (j < 2) || (i > (cpi->common.mb_rows-3)) || (j > (cpi->common.mb_cols-3)) )
 387            //  seg_map[(i*cpi->common.mb_cols) + j] = 2;
 388            //else if ( (i < 5) || (j < 5) || (i > (cpi->common.mb_rows-6)) || (j > (cpi->common.mb_cols-6)) )
 389            //  seg_map[(i*cpi->common.mb_cols) + j] = 3;
 390            else
 391                seg_map[(i*cpi->common.mb_cols) + j] = 0;
 392        }
 393    }*/
 394
 395    // Set the segmentation Map
 396    set_segmentation_map(ptr, seg_map);
 397
 398    // Activate segmentation.
 399    enable_segmentation(ptr);
 400
 401    // Set up the quant segment data
 402    feature_data[MB_LVL_ALT_Q][0] = 0;
 403    feature_data[MB_LVL_ALT_Q][1] = 4;
 404    feature_data[MB_LVL_ALT_Q][2] = 0;
 405    feature_data[MB_LVL_ALT_Q][3] = 0;
 406    // Set up the loop segment data
 407    feature_data[MB_LVL_ALT_LF][0] = 0;
 408    feature_data[MB_LVL_ALT_LF][1] = 0;
 409    feature_data[MB_LVL_ALT_LF][2] = 0;
 410    feature_data[MB_LVL_ALT_LF][3] = 0;
 411
 412    // Initialise the feature data structure
 413    // SEGMENT_DELTADATA    0, SEGMENT_ABSDATA      1
 414    set_segment_data(ptr, &feature_data[0][0], SEGMENT_DELTADATA);
 415
 416    // Delete sementation map
 417        vpx_free(seg_map);
 418
 419    seg_map = 0;
 420
 421}
 422
 423// A simple function to cyclically refresh the background at a lower Q
 424static void cyclic_background_refresh(VP8_COMP *cpi, int Q, int lf_adjustment)
 425{
 426    unsigned char *seg_map;
 427    signed char feature_data[MB_LVL_MAX][MAX_MB_SEGMENTS];
 428    int i;
 429    int block_count = cpi->cyclic_refresh_mode_max_mbs_perframe;
 430    int mbs_in_frame = cpi->common.mb_rows * cpi->common.mb_cols;
 431
 432    // Create a temporary map for segmentation data.
 433    CHECK_MEM_ERROR(seg_map, vpx_calloc(cpi->common.mb_rows * cpi->common.mb_cols, 1));
 434
 435    cpi->cyclic_refresh_q = Q;
 436
 437    for (i = Q; i > 0; i--)
 438    {
 439        if (vp8_bits_per_mb[cpi->common.frame_type][i] >= ((vp8_bits_per_mb[cpi->common.frame_type][Q]*(Q + 128)) / 64))
 440            //if ( vp8_bits_per_mb[cpi->common.frame_type][i] >= ((vp8_bits_per_mb[cpi->common.frame_type][Q]*((2*Q)+96))/64) )
 441        {
 442            break;
 443        }
 444    }
 445
 446    cpi->cyclic_refresh_q = i;
 447
 448    // Only update for inter frames
 449    if (cpi->common.frame_type != KEY_FRAME)
 450    {
 451        // Cycle through the macro_block rows
 452        // MB loop to set local segmentation map
 453        for (i = cpi->cyclic_refresh_mode_index; i < mbs_in_frame; i++)
 454        {
 455            // If the MB is as a candidate for clean up then mark it for possible boost/refresh (segment 1)
 456            // The segment id may get reset to 0 later if the MB gets coded anything other than last frame 0,0
 457            // as only (last frame 0,0) MBs are eligable for refresh : that is to say Mbs likely to be background blocks.
 458            if (cpi->cyclic_refresh_map[i] == 0)
 459            {
 460                seg_map[i] = 1;
 461            }
 462            else
 463            {
 464                seg_map[i] = 0;
 465
 466                // Skip blocks that have been refreshed recently anyway.
 467                if (cpi->cyclic_refresh_map[i] < 0)
 468                    //cpi->cyclic_refresh_map[i] = cpi->cyclic_refresh_map[i] / 16;
 469                    cpi->cyclic_refresh_map[i]++;
 470            }
 471
 472
 473            if (block_count > 0)
 474                block_count--;
 475            else
 476                break;
 477
 478        }
 479
 480        // If we have gone through the frame reset to the start
 481        cpi->cyclic_refresh_mode_index = i;
 482
 483        if (cpi->cyclic_refresh_mode_index >= mbs_in_frame)
 484            cpi->cyclic_refresh_mode_index = 0;
 485    }
 486
 487    // Set the segmentation Map
 488    set_segmentation_map((VP8_PTR)cpi, seg_map);
 489
 490    // Activate segmentation.
 491    enable_segmentation((VP8_PTR)cpi);
 492
 493    // Set up the quant segment data
 494    feature_data[MB_LVL_ALT_Q][0] = 0;
 495    feature_data[MB_LVL_ALT_Q][1] = (cpi->cyclic_refresh_q - Q);
 496    feature_data[MB_LVL_ALT_Q][2] = 0;
 497    feature_data[MB_LVL_ALT_Q][3] = 0;
 498
 499    // Set up the loop segment data
 500    feature_data[MB_LVL_ALT_LF][0] = 0;
 501    feature_data[MB_LVL_ALT_LF][1] = lf_adjustment;
 502    feature_data[MB_LVL_ALT_LF][2] = 0;
 503    feature_data[MB_LVL_ALT_LF][3] = 0;
 504
 505    // Initialise the feature data structure
 506    // SEGMENT_DELTADATA    0, SEGMENT_ABSDATA      1
 507    set_segment_data((VP8_PTR)cpi, &feature_data[0][0], SEGMENT_DELTADATA);
 508
 509    // Delete sementation map
 510        vpx_free(seg_map);
 511
 512    seg_map = 0;
 513
 514}
 515
 516static void set_default_lf_deltas(VP8_COMP *cpi)
 517{
 518    cpi->mb.e_mbd.mode_ref_lf_delta_enabled = 1;
 519    cpi->mb.e_mbd.mode_ref_lf_delta_update = 1;
 520
 521    vpx_memset(cpi->mb.e_mbd.ref_lf_deltas, 0, sizeof(cpi->mb.e_mbd.ref_lf_deltas));
 522    vpx_memset(cpi->mb.e_mbd.mode_lf_deltas, 0, sizeof(cpi->mb.e_mbd.mode_lf_deltas));
 523
 524    // Test of ref frame deltas
 525    cpi->mb.e_mbd.ref_lf_deltas[INTRA_FRAME] = 2;
 526    cpi->mb.e_mbd.ref_lf_deltas[LAST_FRAME] = 0;
 527    cpi->mb.e_mbd.ref_lf_deltas[GOLDEN_FRAME] = -2;
 528    cpi->mb.e_mbd.ref_lf_deltas[ALTREF_FRAME] = -2;
 529
 530    cpi->mb.e_mbd.mode_lf_deltas[0] = 4;               // BPRED
 531    cpi->mb.e_mbd.mode_lf_deltas[1] = -2;              // Zero
 532    cpi->mb.e_mbd.mode_lf_deltas[2] = 2;               // New mv
 533    cpi->mb.e_mbd.mode_lf_deltas[3] = 4;               // Split mv
 534}
 535
 536void vp8_set_speed_features(VP8_COMP *cpi)
 537{
 538    SPEED_FEATURES *sf = &cpi->sf;
 539    int Mode = cpi->compressor_speed;
 540    int Speed = cpi->Speed;
 541    int i;
 542    VP8_COMMON *cm = &cpi->common;
 543    int last_improved_quant = sf->improved_quant;
 544
 545    // Initialise default mode frequency sampling variables
 546    for (i = 0; i < MAX_MODES; i ++)
 547    {
 548        cpi->mode_check_freq[i] = 0;
 549        cpi->mode_test_hit_counts[i] = 0;
 550        cpi->mode_chosen_counts[i] = 0;
 551    }
 552
 553    cpi->mbs_tested_so_far = 0;
 554
 555    // best quality defaults
 556    sf->RD = 1;
 557    sf->search_method = NSTEP;
 558    sf->improved_quant = 1;
 559    sf->improved_dct = 1;
 560    sf->auto_filter = 1;
 561    sf->recode_loop = 1;
 562    sf->quarter_pixel_search = 1;
 563    sf->half_pixel_search = 1;
 564    sf->iterative_sub_pixel = 1;
 565    sf->optimize_coefficients = 1;
 566    sf->use_fastquant_for_pick = 0;
 567    sf->no_skip_block4x4_search = 1;
 568
 569    sf->first_step = 0;
 570    sf->max_step_search_steps = MAX_MVSEARCH_STEPS;
 571    sf->improved_mv_pred = 1;
 572
 573    // default thresholds to 0
 574    for (i = 0; i < MAX_MODES; i++)
 575        sf->thresh_mult[i] = 0;
 576
 577    switch (Mode)
 578    {
 579#if !(CONFIG_REALTIME_ONLY)
 580    case 0: // best quality mode
 581        sf->thresh_mult[THR_ZEROMV   ] = 0;
 582        sf->thresh_mult[THR_ZEROG    ] = 0;
 583        sf->thresh_mult[THR_ZEROA    ] = 0;
 584        sf->thresh_mult[THR_NEARESTMV] = 0;
 585        sf->thresh_mult[THR_NEARESTG ] = 0;
 586        sf->thresh_mult[THR_NEARESTA ] = 0;
 587        sf->thresh_mult[THR_NEARMV   ] = 0;
 588        sf->thresh_mult[THR_NEARG    ] = 0;
 589        sf->thresh_mult[THR_NEARA    ] = 0;
 590
 591        sf->thresh_mult[THR_DC       ] = 0;
 592
 593        sf->thresh_mult[THR_V_PRED   ] = 1000;
 594        sf->thresh_mult[THR_H_PRED   ] = 1000;
 595        sf->thresh_mult[THR_B_PRED   ] = 2000;
 596        sf->thresh_mult[THR_TM       ] = 1000;
 597
 598        sf->thresh_mult[THR_NEWMV    ] = 1000;
 599        sf->thresh_mult[THR_NEWG     ] = 1000;
 600        sf->thresh_mult[THR_NEWA     ] = 1000;
 601
 602        sf->thresh_mult[THR_SPLITMV  ] = 2500;
 603        sf->thresh_mult[THR_SPLITG   ] = 5000;
 604        sf->thresh_mult[THR_SPLITA   ] = 5000;
 605
 606
 607        sf->first_step = 0;
 608        sf->max_step_search_steps = MAX_MVSEARCH_STEPS;
 609        break;
 610    case 1:
 611    case 3:
 612        sf->thresh_mult[THR_NEARESTMV] = 0;
 613        sf->thresh_mult[THR_ZEROMV   ] = 0;
 614        sf->thresh_mult[THR_DC       ] = 0;
 615        sf->thresh_mult[THR_NEARMV   ] = 0;
 616        sf->thresh_mult[THR_V_PRED   ] = 1000;
 617        sf->thresh_mult[THR_H_PRED   ] = 1000;
 618        sf->thresh_mult[THR_B_PRED   ] = 2500;
 619        sf->thresh_mult[THR_TM       ] = 1000;
 620
 621        sf->thresh_mult[THR_NEARESTG ] = 1000;
 622        sf->thresh_mult[THR_NEARESTA ] = 1000;
 623
 624        sf->thresh_mult[THR_ZEROG    ] = 1000;
 625        sf->thresh_mult[THR_ZEROA    ] = 1000;
 626        sf->thresh_mult[THR_NEARG    ] = 1000;
 627        sf->thresh_mult[THR_NEARA    ] = 1000;
 628
 629#if 1
 630        sf->thresh_mult[THR_ZEROMV   ] = 0;
 631        sf->thresh_mult[THR_ZEROG    ] = 0;
 632        sf->thresh_mult[THR_ZEROA    ] = 0;
 633        sf->thresh_mult[THR_NEARESTMV] = 0;
 634        sf->thresh_mult[THR_NEARESTG ] = 0;
 635        sf->thresh_mult[THR_NEARESTA ] = 0;
 636        sf->thresh_mult[THR_NEARMV   ] = 0;
 637        sf->thresh_mult[THR_NEARG    ] = 0;
 638        sf->thresh_mult[THR_NEARA    ] = 0;
 639
 640//        sf->thresh_mult[THR_DC       ] = 0;
 641
 642//        sf->thresh_mult[THR_V_PRED   ] = 1000;
 643//        sf->thresh_mult[THR_H_PRED   ] = 1000;
 644//        sf->thresh_mult[THR_B_PRED   ] = 2000;
 645//        sf->thresh_mult[THR_TM       ] = 1000;
 646
 647        sf->thresh_mult[THR_NEWMV    ] = 1000;
 648        sf->thresh_mult[THR_NEWG     ] = 1000;
 649        sf->thresh_mult[THR_NEWA     ] = 1000;
 650
 651        sf->thresh_mult[THR_SPLITMV  ] = 1700;
 652        sf->thresh_mult[THR_SPLITG   ] = 4500;
 653        sf->thresh_mult[THR_SPLITA   ] = 4500;
 654#else
 655        sf->thresh_mult[THR_NEWMV    ] = 1500;
 656        sf->thresh_mult[THR_NEWG     ] = 1500;
 657        sf->thresh_mult[THR_NEWA     ] = 1500;
 658
 659        sf->thresh_mult[THR_SPLITMV  ] = 5000;
 660        sf->thresh_mult[THR_SPLITG   ] = 10000;
 661        sf->thresh_mult[THR_SPLITA   ] = 10000;
 662#endif
 663
 664        if (Speed > 0)
 665        {
 666            /* Disable coefficient optimization above speed 0 */
 667            sf->optimize_coefficients = 0;
 668            sf->use_fastquant_for_pick = 1;
 669            sf->no_skip_block4x4_search = 0;
 670
 671            sf->first_step = 1;
 672
 673            cpi->mode_check_freq[THR_SPLITG] = 2;
 674            cpi->mode_check_freq[THR_SPLITA] = 2;
 675            cpi->mode_check_freq[THR_SPLITMV] = 0;
 676        }
 677
 678        if (Speed > 1)
 679        {
 680            cpi->mode_check_freq[THR_SPLITG] = 4;
 681            cpi->mode_check_freq[THR_SPLITA] = 4;
 682            cpi->mode_check_freq[THR_SPLITMV] = 2;
 683
 684            sf->thresh_mult[THR_TM       ] = 1500;
 685            sf->thresh_mult[THR_V_PRED   ] = 1500;
 686            sf->thresh_mult[THR_H_PRED   ] = 1500;
 687            sf->thresh_mult[THR_B_PRED   ] = 5000;
 688
 689            if (cpi->ref_frame_flags & VP8_LAST_FLAG)
 690            {
 691                sf->thresh_mult[THR_NEWMV    ] = 2000;
 692                sf->thresh_mult[THR_SPLITMV  ] = 10000;
 693            }
 694
 695            if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
 696            {
 697                sf->thresh_mult[THR_NEARESTG ] = 1500;
 698                sf->thresh_mult[THR_ZEROG    ] = 1500;
 699                sf->thresh_mult[THR_NEARG    ] = 1500;
 700                sf->thresh_mult[THR_NEWG     ] = 2000;
 701                sf->thresh_mult[THR_SPLITG   ] = 20000;
 702            }
 703
 704            if (cpi->ref_frame_flags & VP8_ALT_FLAG)
 705            {
 706                sf->thresh_mult[THR_NEARESTA ] = 1500;
 707                sf->thresh_mult[THR_ZEROA    ] = 1500;
 708                sf->thresh_mult[THR_NEARA    ] = 1500;
 709                sf->thresh_mult[THR_NEWA     ] = 2000;
 710                sf->thresh_mult[THR_SPLITA   ] = 20000;
 711            }
 712        }
 713
 714        if (Speed > 2)
 715        {
 716            cpi->mode_check_freq[THR_SPLITG] = 15;
 717            cpi->mode_check_freq[THR_SPLITA] = 15;
 718            cpi->mode_check_freq[THR_SPLITMV] = 7;
 719
 720            sf->thresh_mult[THR_TM       ] = 2000;
 721            sf->thresh_mult[THR_V_PRED   ] = 2000;
 722            sf->thresh_mult[THR_H_PRED   ] = 2000;
 723            sf->thresh_mult[THR_B_PRED   ] = 7500;
 724
 725            if (cpi->ref_frame_flags & VP8_LAST_FLAG)
 726            {
 727                sf->thresh_mult[THR_NEWMV    ] = 2000;
 728                sf->thresh_mult[THR_SPLITMV  ] = 25000;
 729            }
 730
 731            if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
 732            {
 733                sf->thresh_mult[THR_NEARESTG ] = 2000;
 734                sf->thresh_mult[THR_ZEROG    ] = 2000;
 735                sf->thresh_mult[THR_NEARG    ] = 2000;
 736                sf->thresh_mult[THR_NEWG     ] = 2500;
 737                sf->thresh_mult[THR_SPLITG   ] = 50000;
 738            }
 739
 740            if (cpi->ref_frame_flags & VP8_ALT_FLAG)
 741            {
 742                sf->thresh_mult[THR_NEARESTA ] = 2000;
 743                sf->thresh_mult[THR_ZEROA    ] = 2000;
 744                sf->thresh_mult[THR_NEARA    ] = 2000;
 745                sf->thresh_mult[THR_NEWA     ] = 2500;
 746                sf->thresh_mult[THR_SPLITA   ] = 50000;
 747            }
 748
 749            sf->improved_quant = 0;
 750            sf->improved_dct = 0;
 751
 752            // Only do recode loop on key frames, golden frames and
 753            // alt ref frames
 754            sf->recode_loop = 2;
 755
 756        }
 757
 758        if (Speed > 3)
 759        {
 760            sf->thresh_mult[THR_SPLITA  ] = INT_MAX;
 761            sf->thresh_mult[THR_SPLITG  ] = INT_MAX;
 762            sf->thresh_mult[THR_SPLITMV  ] = INT_MAX;
 763
 764            cpi->mode_check_freq[THR_V_PRED] = 0;
 765            cpi->mode_check_freq[THR_H_PRED] = 0;
 766            cpi->mode_check_freq[THR_B_PRED] = 0;
 767            cpi->mode_check_freq[THR_NEARG] = 0;
 768            cpi->mode_check_freq[THR_NEWG] = 0;
 769            cpi->mode_check_freq[THR_NEARA] = 0;
 770            cpi->mode_check_freq[THR_NEWA] = 0;
 771
 772            sf->auto_filter = 1;
 773            sf->recode_loop = 0; // recode loop off
 774            sf->RD = 0;         // Turn rd off
 775
 776        }
 777
 778        if (Speed > 4)
 779        {
 780            sf->auto_filter = 0;                     // Faster selection of loop filter
 781
 782            cpi->mode_check_freq[THR_V_PRED] = 2;
 783            cpi->mode_check_freq[THR_H_PRED] = 2;
 784            cpi->mode_check_freq[THR_B_PRED] = 2;
 785
 786            if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
 787            {
 788                cpi->mode_check_freq[THR_NEARG] = 2;
 789                cpi->mode_check_freq[THR_NEWG] = 4;
 790            }
 791
 792            if (cpi->ref_frame_flags & VP8_ALT_FLAG)
 793            {
 794                cpi->mode_check_freq[THR_NEARA] = 2;
 795                cpi->mode_check_freq[THR_NEWA] = 4;
 796            }
 797
 798            if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
 799            {
 800                sf->thresh_mult[THR_NEARESTG ] = 2000;
 801                sf->thresh_mult[THR_ZEROG    ] = 2000;
 802                sf->thresh_mult[THR_NEARG    ] = 2000;
 803                sf->thresh_mult[THR_NEWG     ] = 4000;
 804            }
 805
 806            if (cpi->ref_frame_flags & VP8_ALT_FLAG)
 807            {
 808                sf->thresh_mult[THR_NEARESTA ] = 2000;
 809                sf->thresh_mult[THR_ZEROA    ] = 2000;
 810                sf->thresh_mult[THR_NEARA    ] = 2000;
 811                sf->thresh_mult[THR_NEWA     ] = 4000;
 812            }
 813        }
 814
 815        break;
 816#endif
 817    case 2:
 818        sf->optimize_coefficients = 0;
 819        sf->recode_loop = 0;
 820        sf->auto_filter = 1;
 821        sf->iterative_sub_pixel = 1;
 822        sf->thresh_mult[THR_NEARESTMV] = 0;
 823        sf->thresh_mult[THR_ZEROMV   ] = 0;
 824        sf->thresh_mult[THR_DC       ] = 0;
 825        sf->thresh_mult[THR_TM       ] = 0;
 826        sf->thresh_mult[THR_NEARMV   ] = 0;
 827        sf->thresh_mult[THR_V_PRED   ] = 1000;
 828        sf->thresh_mult[THR_H_PRED   ] = 1000;
 829        sf->thresh_mult[THR_B_PRED   ] = 2500;
 830        sf->thresh_mult[THR_NEARESTG ] = 1000;
 831        sf->thresh_mult[THR_ZEROG    ] = 1000;
 832        sf->thresh_mult[THR_NEARG    ] = 1000;
 833        sf->thresh_mult[THR_NEARESTA ] = 1000;
 834        sf->thresh_mult[THR_ZEROA    ] = 1000;
 835        sf->thresh_mult[THR_NEARA    ] = 1000;
 836        sf->thresh_mult[THR_NEWMV    ] = 2000;
 837        sf->thresh_mult[THR_NEWG     ] = 2000;
 838        sf->thresh_mult[THR_NEWA     ] = 2000;
 839        sf->thresh_mult[THR_SPLITMV  ] = 5000;
 840        sf->thresh_mult[THR_SPLITG   ] = 10000;
 841        sf->thresh_mult[THR_SPLITA   ] = 10000;
 842        sf->search_method = NSTEP;
 843
 844        if (Speed > 0)
 845        {
 846            cpi->mode_check_freq[THR_SPLITG] = 4;
 847            cpi->mode_check_freq[THR_SPLITA] = 4;
 848            cpi->mode_check_freq[THR_SPLITMV] = 2;
 849
 850            sf->thresh_mult[THR_DC       ] = 0;
 851            sf->thresh_mult[THR_TM       ] = 1000;
 852            sf->thresh_mult[THR_V_PRED   ] = 2000;
 853            sf->thresh_mult[THR_H_PRED   ] = 2000;
 854            sf->thresh_mult[THR_B_PRED   ] = 5000;
 855
 856            if (cpi->ref_frame_flags & VP8_LAST_FLAG)
 857            {
 858                sf->thresh_mult[THR_NEARESTMV] = 0;
 859                sf->thresh_mult[THR_ZEROMV   ] = 0;
 860                sf->thresh_mult[THR_NEARMV   ] = 0;
 861                sf->thresh_mult[THR_NEWMV    ] = 2000;
 862                sf->thresh_mult[THR_SPLITMV  ] = 10000;
 863            }
 864
 865            if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
 866            {
 867                sf->thresh_mult[THR_NEARESTG ] = 1000;
 868                sf->thresh_mult[THR_ZEROG    ] = 1000;
 869                sf->thresh_mult[THR_NEARG    ] = 1000;
 870                sf->thresh_mult[THR_NEWG     ] = 2000;
 871                sf->thresh_mult[THR_SPLITG   ] = 20000;
 872            }
 873
 874            if (cpi->ref_frame_flags & VP8_ALT_FLAG)
 875            {
 876                sf->thresh_mult[THR_NEARESTA ] = 1000;
 877                sf->thresh_mult[THR_ZEROA    ] = 1000;
 878                sf->thresh_mult[THR_NEARA    ] = 1000;
 879                sf->thresh_mult[THR_NEWA     ] = 2000;
 880                sf->thresh_mult[THR_SPLITA   ] = 20000;
 881            }
 882
 883            sf->improved_quant = 0;
 884            sf->improved_dct = 0;
 885
 886            sf->use_fastquant_for_pick = 1;
 887            sf->no_skip_block4x4_search = 0;
 888            sf->first_step = 1;
 889        }
 890
 891        if (Speed > 1)
 892        {
 893            cpi->mode_check_freq[THR_SPLITMV] = 7;
 894            cpi->mode_check_freq[THR_SPLITG] = 15;
 895            cpi->mode_check_freq[THR_SPLITA] = 15;
 896
 897            sf->thresh_mult[THR_TM       ] = 2000;
 898            sf->thresh_mult[THR_V_PRED   ] = 2000;
 899            sf->thresh_mult[THR_H_PRED   ] = 2000;
 900            sf->thresh_mult[THR_B_PRED   ] = 5000;
 901
 902            if (cpi->ref_frame_flags & VP8_LAST_FLAG)
 903            {
 904                sf->thresh_mult[THR_NEWMV    ] = 2000;
 905                sf->thresh_mult[THR_SPLITMV  ] = 25000;
 906            }
 907
 908            if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
 909            {
 910                sf->thresh_mult[THR_NEARESTG ] = 2000;
 911                sf->thresh_mult[THR_ZEROG    ] = 2000;
 912                sf->thresh_mult[THR_NEARG    ] = 2000;
 913                sf->thresh_mult[THR_NEWG     ] = 2500;
 914                sf->thresh_mult[THR_SPLITG   ] = 50000;
 915            }
 916
 917            if (cpi->ref_frame_flags & VP8_ALT_FLAG)
 918            {
 919                sf->thresh_mult[THR_NEARESTA ] = 2000;
 920                sf->thresh_mult[THR_ZEROA    ] = 2000;
 921                sf->thresh_mult[THR_NEARA    ] = 2000;
 922                sf->thresh_mult[THR_NEWA     ] = 2500;
 923                sf->thresh_mult[THR_SPLITA   ] = 50000;
 924            }
 925
 926        }
 927
 928        if (Speed > 2)
 929        {
 930            sf->auto_filter = 0;                     // Faster selection of loop filter
 931
 932            cpi->mode_check_freq[THR_V_PRED] = 2;
 933            cpi->mode_check_freq[THR_H_PRED] = 2;
 934            cpi->mode_check_freq[THR_B_PRED] = 2;
 935
 936            if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
 937            {
 938                cpi->mode_check_freq[THR_NEARG] = 2;
 939                cpi->mode_check_freq[THR_NEWG] = 4;
 940            }
 941
 942            if (cpi->ref_frame_flags & VP8_ALT_FLAG)
 943            {
 944                cpi->mode_check_freq[THR_NEARA] = 2;
 945                cpi->mode_check_freq[THR_NEWA] = 4;
 946            }
 947
 948            sf->thresh_mult[THR_SPLITMV  ] = INT_MAX;
 949            sf->thresh_mult[THR_SPLITG  ] = INT_MAX;
 950            sf->thresh_mult[THR_SPLITA  ] = INT_MAX;
 951
 952        }
 953
 954        if (Speed > 3)
 955        {
 956            sf->RD = 0;
 957
 958            sf->auto_filter = 1;
 959        }
 960
 961        if (Speed > 4)
 962        {
 963            sf->auto_filter = 0;                     // Faster selection of loop filter
 964
 965            sf->search_method = HEX;
 966            //sf->search_method = DIAMOND;
 967
 968            sf->iterative_sub_pixel = 0;
 969
 970            cpi->mode_check_freq[THR_V_PRED] = 4;
 971            cpi->mode_check_freq[THR_H_PRED] = 4;
 972            cpi->mode_check_freq[THR_B_PRED] = 4;
 973
 974            if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
 975            {
 976                cpi->mode_check_freq[THR_NEARG] = 2;
 977                cpi->mode_check_freq[THR_NEWG] = 4;
 978            }
 979
 980            if (cpi->ref_frame_flags & VP8_ALT_FLAG)
 981            {
 982                cpi->mode_check_freq[THR_NEARA] = 2;
 983                cpi->mode_check_freq[THR_NEWA] = 4;
 984            }
 985
 986            sf->thresh_mult[THR_TM       ] = 2000;
 987            sf->thresh_mult[THR_B_PRED   ] = 5000;
 988
 989            if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
 990            {
 991                sf->thresh_mult[THR_NEARESTG ] = 2000;
 992                sf->thresh_mult[THR_ZEROG    ] = 2000;
 993                sf->thresh_mult[THR_NEARG    ] = 2000;
 994                sf->thresh_mult[THR_NEWG     ] = 4000;
 995            }
 996
 997            if (cpi->ref_frame_flags & VP8_ALT_FLAG)
 998            {
 999                sf->thresh_mult[THR_NEARESTA ] = 2000;
1000                sf->thresh_mult[THR_ZEROA    ] = 2000;
1001                sf->thresh_mult[THR_NEARA    ] = 2000;
1002                sf->thresh_mult[THR_NEWA     ] = 4000;
1003            }
1004        }
1005
1006        if (Speed > 5)
1007        {
1008            // Disable split MB intra prediction mode
1009            sf->thresh_mult[THR_B_PRED] = INT_MAX;
1010        }
1011
1012        if (Speed > 6)
1013        {
1014            unsigned int i, sum = 0;
1015            unsigned int total_mbs = cm->MBs;
1016            int thresh;
1017            int total_skip;
1018
1019            int min = 2000;
1020
1021            if (cpi->oxcf.encode_breakout > 2000)
1022                min = cpi->oxcf.encode_breakout;
1023
1024            min >>= 7;
1025
1026            for (i = 0; i < min; i++)
1027            {
1028                sum += cpi->error_bins[i];
1029            }
1030
1031            total_skip = sum;
1032            sum = 0;
1033
1034            // i starts from 2 to make sure thresh started from 2048
1035            for (; i < 1024; i++)
1036            {
1037                sum += cpi->error_bins[i];
1038
1039                if (10 * sum >= (unsigned int)(cpi->Speed - 6)*(total_mbs - total_skip))
1040                    break;
1041            }
1042
1043            i--;
1044            thresh = (i << 7);
1045
1046            if (thresh < 2000)
1047                thresh = 2000;
1048
1049            if (cpi->ref_frame_flags & VP8_LAST_FLAG)
1050            {
1051                sf->thresh_mult[THR_NEWMV] = thresh;
1052                sf->thresh_mult[THR_NEARESTMV ] = thresh >> 1;
1053                sf->thresh_mult[THR_NEARMV    ] = thresh >> 1;
1054            }
1055
1056            if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
1057            {
1058                sf->thresh_mult[THR_NEWG] = thresh << 1;
1059                sf->thresh_mult[THR_NEARESTG ] = thresh;
1060                sf->thresh_mult[THR_NEARG    ] = thresh;
1061            }
1062
1063            if (cpi->ref_frame_flags & VP8_ALT_FLAG)
1064            {
1065                sf->thresh_mult[THR_NEWA] = thresh << 1;
1066                sf->thresh_mult[THR_NEARESTA ] = thresh;
1067                sf->thresh_mult[THR_NEARA    ] = thresh;
1068            }
1069
1070            // Disable other intra prediction modes
1071            sf->thresh_mult[THR_TM] = INT_MAX;
1072            sf->thresh_mult[THR_V_PRED] = INT_MAX;
1073            sf->thresh_mult[THR_H_PRED] = INT_MAX;
1074
1075            sf->improved_mv_pred = 0;
1076        }
1077
1078        if (Speed > 8)
1079        {
1080            sf->quarter_pixel_search = 0;
1081        }
1082
1083        if (Speed > 9)
1084        {
1085            int Tmp = cpi->Speed - 8;
1086
1087            if (Tmp > 4)
1088                Tmp = 4;
1089
1090            if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
1091            {
1092                cpi->mode_check_freq[THR_ZEROG] = 1 << (Tmp - 1);
1093                cpi->mode_check_freq[THR_NEARESTG] = 1 << (Tmp - 1);
1094                cpi->mode_check_freq[THR_NEARG] = 1 << Tmp;
1095                cpi->mode_check_freq[THR_NEWG] = 1 << (Tmp + 1);
1096            }
1097
1098            if (cpi->ref_frame_flags & VP8_ALT_FLAG)
1099            {
1100                cpi->mode_check_freq[THR_ZEROA] = 1 << (Tmp - 1);
1101                cpi->mode_check_freq[THR_NEARESTA] = 1 << (Tmp - 1);
1102                cpi->mode_check_freq[THR_NEARA] = 1 << Tmp;
1103                cpi->mode_check_freq[THR_NEWA] = 1 << (Tmp + 1);
1104            }
1105
1106            cpi->mode_check_freq[THR_NEWMV] = 1 << (Tmp - 1);
1107        }
1108
1109        cm->filter_type = NORMAL_LOOPFILTER;
1110
1111        if (Speed >= 14)
1112            cm->filter_type = SIMPLE_LOOPFILTER;
1113
1114        if (Speed >= 15)
1115        {
1116            sf->half_pixel_search = 0;        // This has a big hit on quality. Last resort
1117        }
1118
1119        vpx_memset(cpi->error_bins, 0, sizeof(cpi->error_bins));
1120
1121    }; /* switch */
1122
1123    /* disable frame modes if flags not set */
1124    if (!(cpi->ref_frame_flags & VP8_LAST_FLAG))
1125    {
1126        sf->thresh_mult[THR_NEWMV    ] = INT_MAX;
1127        sf->thresh_mult[THR_NEARESTMV] = INT_MAX;
1128        sf->thresh_mult[THR_ZEROMV   ] = INT_MAX;
1129        sf->thresh_mult[THR_NEARMV   ] = INT_MAX;
1130        sf->thresh_mult[THR_SPLITMV  ] = INT_MAX;
1131    }
1132
1133    if (!(cpi->ref_frame_flags & VP8_GOLD_FLAG))
1134    {
1135        sf->thresh_mult[THR_NEARESTG ] = INT_MAX;
1136        sf->thresh_mult[THR_ZEROG    ] = INT_MAX;
1137        sf->thresh_mult[THR_NEARG    ] = INT_MAX;
1138        sf->thresh_mult[THR_NEWG     ] = INT_MAX;
1139        sf->thresh_mult[THR_SPLITG   ] = INT_MAX;
1140    }
1141
1142    if (!(cpi->ref_frame_flags & VP8_ALT_FLAG))
1143    {
1144        sf->thresh_mult[THR_NEARESTA ] = INT_MAX;
1145        sf->thresh_mult[THR_ZEROA    ] = INT_MAX;
1146        sf->thresh_mult[THR_NEARA    ] = INT_MAX;
1147        sf->thresh_mult[THR_NEWA     ] = INT_MAX;
1148        sf->thresh_mult[THR_SPLITA   ] = INT_MAX;
1149    }
1150
1151
1152    // Slow quant, dct and trellis not worthwhile for first pass
1153    // so make sure they are always turned off.
1154    if ( cpi->pass == 1 )
1155    {
1156        sf->improved_quant = 0;
1157        sf->optimize_coefficients = 0;
1158        sf->improved_dct = 0;
1159    }
1160
1161    if (cpi->sf.search_method == NSTEP)
1162    {
1163        vp8_init3smotion_compensation(&cpi->mb, cm->yv12_fb[cm->lst_fb_idx].y_stride);
1164    }
1165    else if (cpi->sf.search_method == DIAMOND)
1166    {
1167        vp8_init_dsmotion_compensation(&cpi->mb, cm->yv12_fb[cm->lst_fb_idx].y_stride);
1168    }
1169
1170    if (cpi->sf.improved_dct)
1171    {
1172        cpi->mb.vp8_short_fdct8x4 = FDCT_INVOKE(&cpi->rtcd.fdct, short8x4);
1173        cpi->mb.vp8_short_fdct4x4 = FDCT_INVOKE(&cpi->rtcd.fdct, short4x4);
1174    }
1175    else
1176    {
1177        cpi->mb.vp8_short_fdct8x4   = FDCT_INVOKE(&cpi->rtcd.fdct, fast8x4);
1178        cpi->mb.vp8_short_fdct4x4   = FDCT_INVOKE(&cpi->rtcd.fdct, fast4x4);
1179    }
1180
1181    cpi->mb.short_walsh4x4 = FDCT_INVOKE(&cpi->rtcd.fdct, walsh_short4x4);
1182
1183    if (cpi->sf.improved_quant)
1184    {
1185        cpi->mb.quantize_b      = QUANTIZE_INVOKE(&cpi->rtcd.quantize,
1186                                                  quantb);
1187        cpi->mb.quantize_b_pair = QUANTIZE_INVOKE(&cpi->rtcd.quantize,
1188                                                  quantb_pair);
1189    }
1190    else
1191    {
1192        cpi->mb.quantize_b      = QUANTIZE_INVOKE(&cpi->rtcd.quantize,
1193                                                  fastquantb);
1194        cpi->mb.quantize_b_pair = QUANTIZE_INVOKE(&cpi->rtcd.quantize,
1195                                                  fastquantb_pair);
1196    }
1197    if (cpi->sf.improved_quant != last_improved_quant)
1198        vp8cx_init_quantizer(cpi);
1199
1200#if CONFIG_RUNTIME_CPU_DETECT
1201    cpi->mb.e_mbd.rtcd = &cpi->common.rtcd;
1202#endif
1203
1204    if (cpi->sf.iterative_sub_pixel == 1)
1205    {
1206        cpi->find_fractional_mv_step = vp8_find_best_sub_pixel_step_iteratively;
1207    }
1208    else if (cpi->sf.quarter_pixel_search)
1209    {
1210        cpi->find_fractional_mv_step = vp8_find_best_sub_pixel_step;
1211    }
1212    else if (cpi->sf.half_pixel_search)
1213    {
1214        cpi->find_fractional_mv_step = vp8_find_best_half_pixel_step;
1215    }
1216    else
1217    {
1218        cpi->find_fractional_mv_step = vp8_skip_fractional_mv_step;
1219    }
1220
1221    if (cpi->sf.optimize_coefficients == 1 && cpi->pass!=1)
1222        cpi->mb.optimize = 1;
1223    else
1224        cpi->mb.optimize = 0;
1225
1226    if (cpi->common.full_pixel)
1227        cpi->find_fractional_mv_step = vp8_skip_fractional_mv_step;
1228
1229#ifdef SPEEDSTATS
1230    frames_at_speed[cpi->Speed]++;
1231#endif
1232}
1233static void alloc_raw_frame_buffers(VP8_COMP *cpi)
1234{
1235    int width = (cpi->oxcf.Width + 15) & ~15;
1236    int height = (cpi->oxcf.Height + 15) & ~15;
1237
1238    cpi->lookahead = vp8_lookahead_init(cpi->oxcf.Width, cpi->oxcf.Height,
1239                                        cpi->oxcf.lag_in_frames);
1240    if(!cpi->lookahead)
1241        vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1242                           "Failed to allocate lag buffers");
1243
1244#if VP8_TEMPORAL_ALT_REF
1245
1246    if (vp8_yv12_alloc_frame_buffer(&cpi->alt_ref_buffer,
1247                                    width, height, VP8BORDERINPIXELS))
1248        vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1249                           "Failed to allocate altref buffer");
1250
1251#endif
1252}
1253
1254static int vp8_alloc_partition_data(VP8_COMP *cpi)
1255{
1256        vpx_free(cpi->mb.pip);
1257
1258    cpi->mb.pip = vpx_calloc((cpi->common.mb_cols + 1) *
1259                                (cpi->common.mb_rows + 1),
1260                                sizeof(PARTITION_INFO));
1261    if(!cpi->mb.pip)
1262        return 1;
1263
1264    cpi->mb.pi = cpi->mb.pip + cpi->common.mode_info_stride + 1;
1265
1266    return 0;
1267}
1268
1269void vp8_alloc_compressor_data(VP8_COMP *cpi)
1270{
1271    VP8_COMMON *cm = & cpi->common;
1272
1273    int width = cm->Width;
1274    int height = cm->Height;
1275
1276    if (vp8_alloc_frame_buffers(cm, width, height))
1277        vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1278                           "Failed to allocate frame buffers");
1279
1280    if (vp8_alloc_partition_data(cpi))
1281        vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1282                           "Failed to allocate partition data");
1283
1284
1285    if ((width & 0xf) != 0)
1286        width += 16 - (width & 0xf);
1287
1288    if ((height & 0xf) != 0)
1289        height += 16 - (height & 0xf);
1290
1291
1292    if (vp8_yv12_alloc_frame_buffer(&cpi->last_frame_uf,
1293                                    width, height, VP8BORDERINPIXELS))
1294        vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1295                           "Failed to allocate last frame buffer");
1296
1297    if (vp8_yv12_alloc_frame_buffer(&cpi->scaled_source,
1298                                    width, height, VP8BORDERINPIXELS))
1299        vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1300                           "Failed to allocate scaled source buffer");
1301
1302
1303        vpx_free(cpi->tok);
1304
1305    {
1306        unsigned int tokens = cm->mb_rows * cm->mb_cols * 24 * 16;
1307
1308        CHECK_MEM_ERROR(cpi->tok, vpx_calloc(tokens, sizeof(*cpi->tok)));
1309    }
1310
1311    // Data used for real time vc mode to see if gf needs refreshing
1312    cpi->inter_zz_count = 0;
1313    cpi->gf_bad_count = 0;
1314    cpi->gf_update_recommended = 0;
1315
1316
1317    // Structures used to minitor GF usage
1318    vpx_free(cpi->gf_active_flags);
1319    CHECK_MEM_ERROR(cpi->gf_active_flags,
1320                    vpx_calloc(1, cm->mb_rows * cm->mb_cols));
1321    cpi->gf_active_count = cm->mb_rows * cm->mb_cols;
1322
1323    vpx_free(cpi->mb_activity_map);
1324    CHECK_MEM_ERROR(cpi->mb_activity_map,
1325                    vpx_calloc(sizeof(unsigned int),
1326                    cm->mb_rows * cm->mb_cols));
1327
1328    vpx_free(cpi->mb_norm_activity_map);
1329    CHECK_MEM_ERROR(cpi->mb_norm_activity_map,
1330                    vpx_calloc(sizeof(unsigned int),
1331                    cm->mb_rows * cm->mb_cols));
1332
1333#if !(CONFIG_REALTIME_ONLY)
1334        vpx_free(cpi->twopass.total_stats);
1335
1336    cpi->twopass.total_stats = vpx_calloc(1, sizeof(FIRSTPASS_STATS));
1337
1338        vpx_free(cpi->twopass.this_frame_stats);
1339
1340    cpi->twopass.this_frame_stats = vpx_calloc(1, sizeof(FIRSTPASS_STATS));
1341
1342    if(!cpi->twopass.total_stats || !cpi->twopass.this_frame_stats)
1343        vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1344                           "Failed to allocate firstpass stats");
1345#endif
1346
1347#if CONFIG_MULTITHREAD
1348    if (width < 640)
1349        cpi->mt_sync_range = 1;
1350    else if (width <= 1280)
1351        cpi->mt_sync_range = 4;
1352    else if (width <= 2560)
1353        cpi->mt_sync_range = 8;
1354    else
1355        cpi->mt_sync_range = 16;
1356#endif
1357
1358        vpx_free(cpi->tplist);
1359
1360    CHECK_MEM_ERROR(cpi->tplist, vpx_malloc(sizeof(TOKENLIST) * cpi->common.mb_rows));
1361}
1362
1363
1364// Quant MOD
1365static const int q_trans[] =
1366{
1367    0,   1,  2,  3,  4,  5,  7,  8,
1368    9,  10, 12, 13, 15, 17, 18, 19,
1369    20,  21, 23, 24, 25, 26, 27, 28,
1370    29,  30, 31, 33, 35, 37, 39, 41,
1371    43,  45, 47, 49, 51, 53, 55, 57,
1372    59,  61, 64, 67, 70, 73, 76, 79,
1373    82,  85, 88, 91, 94, 97, 100, 103,
1374    106, 109, 112, 115, 118, 121, 124, 127,
1375};
1376
1377int vp8_reverse_trans(int x)
1378{
1379    int i;
1380
1381    for (i = 0; i < 64; i++)
1382        if (q_trans[i] >= x)
1383            return i;
1384
1385    return 63;
1386};
1387void vp8_new_frame_rate(VP8_COMP *cpi, double framerate)
1388{
1389    if(framerate < .1)
1390        framerate = 30;
1391
1392    cpi->oxcf.frame_rate             = framerate;
1393    cpi->output_frame_rate            = cpi->oxcf.frame_rate;
1394    cpi->per_frame_bandwidth          = (int)(cpi->oxcf.target_bandwidth / cpi->output_frame_rate);
1395    cpi->av_per_frame_bandwidth        = (int)(cpi->oxcf.target_bandwidth / cpi->output_frame_rate);
1396    cpi->min_frame_bandwidth          = (int)(cpi->av_per_frame_bandwidth * cpi->oxcf.two_pass_vbrmin_section / 100);
1397
1398    // Set Maximum gf/arf interval
1399    cpi->max_gf_interval = ((int)(cpi->output_frame_rate / 2.0) + 2);
1400
1401    if(cpi->max_gf_interval < 12)
1402        cpi->max_gf_interval = 12;
1403
1404    // Extended interval for genuinely static scenes
1405    cpi->twopass.static_scene_max_gf_interval = cpi->key_frame_frequency >> 1;
1406
1407     // Special conditions when altr ref frame enabled in lagged compress mode
1408    if (cpi->oxcf.play_alternate && cpi->oxcf.lag_in_frames)
1409    {
1410        if (cpi->max_gf_interval > cpi->oxcf.lag_in_frames - 1)
1411            cpi->max_gf_interval = cpi->oxcf.lag_in_frames - 1;
1412
1413        if (cpi->twopass.static_scene_max_gf_interval > cpi->oxcf.lag_in_frames - 1)
1414            cpi->twopass.static_scene_max_gf_interval = cpi->oxcf.lag_in_frames - 1;
1415    }
1416
1417    if ( cpi->max_gf_interval > cpi->twopass.static_scene_max_gf_interval )
1418        cpi->max_gf_interval = cpi->twopass.static_scene_max_gf_interval;
1419}
1420
1421
1422static int
1423rescale(int val, int num, int denom)
1424{
1425    int64_t llnum = num;
1426    int64_t llden = denom;
1427    int64_t llval = val;
1428
1429    return llval * llnum / llden;
1430}
1431
1432
1433static void init_config(VP8_PTR ptr, VP8_CONFIG *oxcf)
1434{
1435    VP8_COMP *cpi = (VP8_COMP *)(ptr);
1436    VP8_COMMON *cm = &cpi->common;
1437
1438    cpi->oxcf = *oxcf;
1439
1440    cpi->auto_gold = 1;
1441    cpi->auto_adjust_gold_quantizer = 1;
1442    cpi->goldfreq = 7;
1443
1444    cm->version = oxcf->Version;
1445    vp8_setup_version(cm);
1446
1447    // change includes all joint functionality
1448    vp8_change_config(ptr, oxcf);
1449
1450    // Initialize active best and worst q and average q values.
1451    cpi->active_worst_quality         = cpi->oxcf.worst_allowed_q;
1452    cpi->active_best_quality          = cpi->oxcf.best_allowed_q;
1453    cpi->avg_frame_qindex             = cpi->oxcf.worst_allowed_q;
1454
1455    // Initialise the starting buffer levels
1456    cpi->buffer_level                 = cpi->oxcf.starting_buffer_level;
1457    cpi->bits_off_target              = cpi->oxcf.starting_buffer_level;
1458
1459    cpi->rolling_target_bits          = cpi->av_per_frame_bandwidth;
1460    cpi->rolling_actual_bits          = cpi->av_per_frame_bandwidth;
1461    cpi->long_rolling_target_bits     = cpi->av_per_frame_bandwidth;
1462    cpi->long_rolling_actual_bits     = cpi->av_per_frame_bandwidth;
1463
1464    cpi->total_actual_bits            = 0;
1465    cpi->total_target_vs_actual       = 0;
1466
1467#if VP8_TEMPORAL_ALT_REF
1468    {
1469        int i;
1470
1471        cpi->fixed_divide[0] = 0;
1472
1473        for (i = 1; i < 512; i++)
1474            cpi->fixed_divide[i] = 0x80000 / i;
1475    }
1476#endif
1477}
1478
1479
1480void vp8_change_config(VP8_PTR ptr, VP8_CONFIG *oxcf)
1481{
1482    VP8_COMP *cpi = (VP8_COMP *)(ptr);
1483    VP8_COMMON *cm = &cpi->common;
1484
1485    if (!cpi)
1486        return;
1487
1488    if (!oxcf)
1489        return;
1490
1491    if (cm->version != oxcf->Version)
1492    {
1493        cm->version = oxcf->Version;
1494        vp8_setup_version(cm);
1495    }
1496
1497    cpi->oxcf = *oxcf;
1498
1499    switch (cpi->oxcf.Mode)
1500    {
1501
1502    case MODE_REALTIME:
1503        cpi->pass = 0;
1504        cpi->compressor_speed = 2;
1505
1506        if (cpi->oxcf.cpu_used < -16)
1507        {
1508            cpi->oxcf.cpu_used = -16;
1509        }
1510
1511        if (cpi->oxcf.cpu_used > 16)
1512            cpi->oxcf.cpu_used = 16;
1513
1514        break;
1515
1516    case MODE_GOODQUALITY:
1517        cpi->pass = 0;
1518        cpi->compressor_speed = 1;
1519
1520        if (cpi->oxcf.cpu_used < -5)
1521        {
1522            cpi->oxcf.cpu_used = -5;
1523        }
1524
1525        if (cpi->oxcf.cpu_used > 5)
1526            cpi->oxcf.cpu_used = 5;
1527
1528        break;
1529
1530    case MODE_BESTQUALITY:
1531        cpi->pass = 0;
1532        cpi->compressor_speed = 0;
1533        break;
1534
1535    case MODE_FIRSTPASS:
1536        cpi->pass = 1;
1537        cpi->compressor_speed = 1;
1538        break;
1539    case MODE_SECONDPASS:
1540        cpi->pass = 2;
1541        cpi->compressor_speed = 1;
1542
1543        if (cpi->oxcf.cpu_used < -5)
1544        {
1545            cpi->oxcf.cpu_used = -5;
1546        }
1547
1548        if (cpi->oxcf.cpu_used > 5)
1549            cpi->oxcf.cpu_used = 5;
1550
1551        break;
1552    case MODE_SECONDPASS_BEST:
1553        cpi->pass = 2;
1554        cpi->compressor_speed = 0;
1555        break;
1556    }
1557
1558    if (cpi->pass == 0)
1559        cpi->auto_worst_q = 1;
1560
1561    cpi->oxcf.worst_allowed_q = q_trans[oxcf->worst_allowed_q];
1562    cpi->oxcf.best_allowed_q = q_trans[oxcf->best_allowed_q];
1563    cpi->oxcf.cq_level = q_trans[cpi->oxcf.cq_level];
1564
1565    if (oxcf->fixed_q >= 0)
1566    {
1567        if (oxcf->worst_allowed_q < 0)
1568            cpi->oxcf.fixed_q = q_trans[0];
1569        else
1570            cpi->oxcf.fixed_q = q_trans[oxcf->worst_allowed_q];
1571
1572        if (oxcf->alt_q < 0)
1573            cpi->oxcf.alt_q = q_trans[0];
1574        else
1575            cpi->oxcf.alt_q = q_trans[oxcf->alt_q];
1576
1577        if (oxcf->key_q < 0)
1578            cpi->oxcf.key_q = q_trans[0];
1579        else
1580            cpi->oxcf.key_q = q_trans[oxcf->key_q];
1581
1582        if (oxcf->gold_q < 0)
1583            cpi->oxcf.gold_q = q_trans[0];
1584        else
1585            cpi->oxcf.gold_q = q_trans[oxcf->gold_q];
1586
1587    }
1588
1589    cpi->baseline_gf_interval =
1590        cpi->oxcf.alt_freq ? cpi->oxcf.alt_freq : DEFAULT_GF_INTERVAL;
1591
1592    cpi->ref_frame_flags = VP8_ALT_FLAG | VP8_GOLD_FLAG | VP8_LAST_FLAG;
1593
1594    //cpi->use_golden_frame_only = 0;
1595    //cpi->use_last_frame_only = 

Large files files are truncated, but you can click here to view the full file