onyx_if.c 157 KB
Newer Older
John Koleszar's avatar
John Koleszar committed
1
/*
2
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
John Koleszar's avatar
John Koleszar committed
3
 *
4
 *  Use of this source code is governed by a BSD-style license
5 6
 *  that can be found in the LICENSE file in the root of the source
 *  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.
John Koleszar's avatar
John Koleszar committed
9 10 11
 */


12
#include "vpx_config.h"
John Koleszar's avatar
John Koleszar committed
13
#include "vp8/common/onyxc_int.h"
John Koleszar's avatar
John Koleszar committed
14
#include "onyx_int.h"
John Koleszar's avatar
John Koleszar committed
15
#include "vp8/common/systemdependent.h"
John Koleszar's avatar
John Koleszar committed
16
#include "quantize.h"
John Koleszar's avatar
John Koleszar committed
17
#include "vp8/common/alloccommon.h"
John Koleszar's avatar
John Koleszar committed
18 19 20 21
#include "mcomp.h"
#include "firstpass.h"
#include "psnr.h"
#include "vpx_scale/vpxscale.h"
John Koleszar's avatar
John Koleszar committed
22
#include "vp8/common/extend.h"
John Koleszar's avatar
John Koleszar committed
23
#include "ratectrl.h"
John Koleszar's avatar
John Koleszar committed
24
#include "vp8/common/quant_common.h"
25
#include "segmentation.h"
John Koleszar's avatar
John Koleszar committed
26
#include "vp8/common/g_common.h"
John Koleszar's avatar
John Koleszar committed
27
#include "vpx_scale/yv12extend.h"
28
#if CONFIG_POSTPROC
John Koleszar's avatar
John Koleszar committed
29
#include "vp8/common/postproc.h"
30
#endif
John Koleszar's avatar
John Koleszar committed
31
#include "vpx_mem/vpx_mem.h"
John Koleszar's avatar
John Koleszar committed
32
#include "vp8/common/swapyv12buffer.h"
John Koleszar's avatar
John Koleszar committed
33
#include "vpx_ports/vpx_timer.h"
Johann's avatar
Johann committed
34
#include "temporal_filter.h"
35 36 37

#include "vp8/common/seg_common.h"
#include "mbgraph.h"
38
#include "vp8/common/pred_common.h"
39
#include "vp8/encoder/rdopt.h"
Christian Duvivier's avatar
Christian Duvivier committed
40 41
#include "bitstream.h"
#include "ratectrl.h"
42

43 44 45
#if ARCH_ARM
#include "vpx_ports/arm.h"
#endif
46

John Koleszar's avatar
John Koleszar committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
#include <math.h>
#include <stdio.h>
#include <limits.h>

#if CONFIG_RUNTIME_CPU_DETECT
#define IF_RTCD(x) (x)
#define RTCD(x) &cpi->common.rtcd.x
#else
#define IF_RTCD(x) NULL
#define RTCD(x) NULL
#endif

extern void vp8cx_pick_filter_level_fast(YV12_BUFFER_CONFIG *sd, VP8_COMP *cpi);
extern void vp8cx_set_alt_lf_level(VP8_COMP *cpi, int filt_val);
extern void vp8cx_pick_filter_level(YV12_BUFFER_CONFIG *sd, VP8_COMP *cpi);

extern void vp8_dmachine_specific_config(VP8_COMP *cpi);
extern void vp8_cmachine_specific_config(VP8_COMP *cpi);
extern void vp8_deblock_frame(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *post, int filt_lvl, int low_var_thresh, int flag);
extern void print_parms(VP8_CONFIG *ocf, char *filenam);
extern unsigned int vp8_get_processor_freq();
extern void print_tree_update_probs();
extern void vp8cx_create_encoder_threads(VP8_COMP *cpi);
extern void vp8cx_remove_encoder_threads(VP8_COMP *cpi);
#if HAVE_ARMV7
extern void vp8_yv12_copy_frame_func_neon(YV12_BUFFER_CONFIG *src_ybc, YV12_BUFFER_CONFIG *dst_ybc);
extern void vp8_yv12_copy_src_frame_func_neon(YV12_BUFFER_CONFIG *src_ybc, YV12_BUFFER_CONFIG *dst_ybc);
#endif

int vp8_calc_ss_err(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *dest, const vp8_variance_rtcd_vtable_t *rtcd);

John Koleszar's avatar
John Koleszar committed
78
extern void vp8_temporal_filter_prepare_c(VP8_COMP *cpi, int distance);
John Koleszar's avatar
John Koleszar committed
79

80
static void set_default_lf_deltas(VP8_COMP *cpi);
John Koleszar's avatar
John Koleszar committed
81 82 83

extern const int vp8_gf_interval_table[101];

84
#if CONFIG_ENHANCED_INTERP
85
#define SEARCH_BEST_FILTER 0            /* to search exhaustively for best filter */
86 87 88
#define RESET_FOREACH_FILTER 0          /* whether to reset the encoder state
                                           before trying each new filter */
#endif
89 90 91 92 93 94 95 96
#if CONFIG_HIGH_PRECISION_MV
#define ALTREF_HIGH_PRECISION_MV 1      /* whether to use high precision mv for altref computation */
#define HIGH_PRECISION_MV_QTHRESH 200   /* Q threshold for use of high precision mv */
                                        /* Choose a very high value for now so
                                         * that HIGH_PRECISION is always chosen
                                         */
#endif

97
#if CONFIG_INTERNAL_STATS
John Koleszar's avatar
John Koleszar committed
98 99 100 101 102 103 104
#include "math.h"

extern double vp8_calc_ssim
(
    YV12_BUFFER_CONFIG *source,
    YV12_BUFFER_CONFIG *dest,
    int lumamask,
105 106
    double *weight,
    const vp8_variance_rtcd_vtable_t *rtcd
John Koleszar's avatar
John Koleszar committed
107 108
);

109

John Koleszar's avatar
John Koleszar committed
110 111 112 113 114 115
extern double vp8_calc_ssimg
(
    YV12_BUFFER_CONFIG *source,
    YV12_BUFFER_CONFIG *dest,
    double *ssim_y,
    double *ssim_u,
116 117
    double *ssim_v,
    const vp8_variance_rtcd_vtable_t *rtcd
John Koleszar's avatar
John Koleszar committed
118 119 120 121 122
);


#endif

Yaowu Xu's avatar
Yaowu Xu committed
123
//#define OUTPUT_YUV_REC
John Koleszar's avatar
John Koleszar committed
124 125 126 127

#ifdef OUTPUT_YUV_SRC
FILE *yuv_file;
#endif
128 129 130
#ifdef OUTPUT_YUV_REC
FILE *yuv_rec_file;
#endif
John Koleszar's avatar
John Koleszar committed
131 132 133

#if 0
FILE *framepsnr;
Yaowu Xu's avatar
Yaowu Xu committed
134
FILE *kf_list;
John Koleszar's avatar
John Koleszar committed
135 136 137 138 139 140 141 142 143 144
FILE *keyfile;
#endif

#if 0
extern int skip_true_count;
extern int skip_false_count;
#endif


#ifdef ENTROPY_STATS
145
extern int intra_mode_stats[VP8_BINTRAMODES][VP8_BINTRAMODES][VP8_BINTRAMODES];
John Koleszar's avatar
John Koleszar committed
146 147 148 149 150 151 152 153 154 155
#endif

#ifdef SPEEDSTATS
unsigned int frames_at_speed[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
unsigned int tot_pm = 0;
unsigned int cnt_pm = 0;
unsigned int tot_ef = 0;
unsigned int cnt_ef = 0;
#endif

156 157 158
#if defined(SECTIONBITS_OUTPUT)
extern unsigned __int64 Sectionbits[500];
#endif
John Koleszar's avatar
John Koleszar committed
159
#ifdef MODE_STATS
Yaowu Xu's avatar
Yaowu Xu committed
160
extern INT64 Sectionbits[500];
161 162 163 164 165 166 167
extern unsigned int y_modes[VP8_YMODES]  ;
extern unsigned int i8x8_modes[VP8_I8X8_MODES];
extern unsigned int uv_modes[VP8_UV_MODES] ;
extern unsigned int uv_modes_y[VP8_YMODES][VP8_UV_MODES];
extern unsigned int b_modes[B_MODE_COUNT];
extern unsigned int inter_y_modes[MB_MODE_COUNT] ;
extern unsigned int inter_uv_modes[VP8_UV_MODES] ;
168
extern unsigned int inter_b_modes[B_MODE_COUNT];
John Koleszar's avatar
John Koleszar committed
169 170 171 172 173 174
#endif

extern void (*vp8_short_fdct4x4)(short *input, short *output, int pitch);
extern void (*vp8_short_fdct8x4)(short *input, short *output, int pitch);

extern void vp8cx_init_quantizer(VP8_COMP *cpi);
Paul Wilkins's avatar
Paul Wilkins committed
175

176 177 178
#if CONFIG_NEWENTROPY
int vp8cx_base_skip_false_prob[QINDEX_RANGE][3];
#else
Paul Wilkins's avatar
Paul Wilkins committed
179
int vp8cx_base_skip_false_prob[QINDEX_RANGE];
180
#endif
181

182
// Tables relating active max Q to active min Q
183 184 185 186 187 188 189 190 191 192 193 194
static int kf_low_motion_minq[QINDEX_RANGE];
static int kf_high_motion_minq[QINDEX_RANGE];
static int gf_low_motion_minq[QINDEX_RANGE];
static int gf_high_motion_minq[QINDEX_RANGE];
static int inter_minq[QINDEX_RANGE];

// Functions to compute the active minq lookup table entries based on a
// formulaic approach to facilitate easier adjustment of the Q tables.
// The formulae were derived from computing a 3rd order polynomial best
// fit to the original data (after plotting real maxq vs minq (not q index))
int calculate_minq_index( double maxq,
                          double x3, double x2, double x, double c )
195
{
196 197 198 199 200 201 202 203 204 205
    int i;
    double minqtarget;
    double thisq;

    minqtarget = ( (x3 * maxq * maxq * maxq) +
                   (x2 * maxq * maxq) +
                   (x * maxq) +
                   c );

    if ( minqtarget > maxq )
206
        minqtarget = maxq;
207 208 209 210 211 212 213

    for ( i = 0; i < QINDEX_RANGE; i++ )
    {
        thisq = vp8_convert_qindex_to_q(i);
        if ( minqtarget <= vp8_convert_qindex_to_q(i) )
            return i;
    }
Adrian Grange's avatar
Adrian Grange committed
214
    return QINDEX_RANGE-1;
215 216
}
void init_minq_luts()
217
{
218 219 220 221 222 223 224
    int i;
    double maxq;

    for ( i = 0; i < QINDEX_RANGE; i++ )
    {
        maxq = vp8_convert_qindex_to_q(i);

Paul Wilkins's avatar
Paul Wilkins committed
225

226
        kf_low_motion_minq[i] = calculate_minq_index( maxq,
Paul Wilkins's avatar
Paul Wilkins committed
227 228 229
                                                      0.0000003,
                                                      -0.000015,
                                                      0.074,
230 231
                                                      0.0 );
        kf_high_motion_minq[i] = calculate_minq_index( maxq,
232
                                                       0.0000004,
Paul Wilkins's avatar
Paul Wilkins committed
233
                                                       -0.000125,
234
                                                       0.14,
235 236
                                                       0.0 );
        gf_low_motion_minq[i] = calculate_minq_index( maxq,
237 238 239
                                                      0.0000015,
                                                      -0.0009,
                                                      0.33,
240 241
                                                      0.0 );
        gf_high_motion_minq[i] = calculate_minq_index( maxq,
242 243 244
                                                       0.0000021,
                                                       -0.00125,
                                                       0.45,
245 246 247 248 249 250 251 252 253 254
                                                       0.0  );
        inter_minq[i] = calculate_minq_index( maxq,
                                              0.00000271,
                                              -0.00113,
                                              0.697,
                                              0.0  );

    }
}

Paul Wilkins's avatar
Paul Wilkins committed
255 256 257 258
void init_base_skip_probs()
{
    int i;
    double q;
259
    int skip_prob, t;
Paul Wilkins's avatar
Paul Wilkins committed
260 261 262 263 264 265 266

    for ( i = 0; i < QINDEX_RANGE; i++ )
    {
        q = vp8_convert_qindex_to_q(i);

        // Exponential decay caluclation of baseline skip prob with clamping
        // Based on crude best fit of old table.
267 268 269 270 271 272 273 274 275 276 277
        t = (int)( 564.25 * pow( 2.71828, (-0.012*q) ) );

        skip_prob = t;
        if ( skip_prob < 1 )
            skip_prob = 1;
        else if ( skip_prob > 255 )
            skip_prob = 255;
#if CONFIG_NEWENTROPY
        vp8cx_base_skip_false_prob[i][1] = skip_prob;

        skip_prob = t * 0.75;
Paul Wilkins's avatar
Paul Wilkins committed
278 279 280 281
        if ( skip_prob < 1 )
            skip_prob = 1;
        else if ( skip_prob > 255 )
            skip_prob = 255;
282
        vp8cx_base_skip_false_prob[i][2] = skip_prob;
Paul Wilkins's avatar
Paul Wilkins committed
283

284 285 286 287 288 289 290
        skip_prob = t * 1.25;
        if ( skip_prob < 1 )
            skip_prob = 1;
        else if ( skip_prob > 255 )
            skip_prob = 255;
        vp8cx_base_skip_false_prob[i][0] = skip_prob;
#else
Paul Wilkins's avatar
Paul Wilkins committed
291
        vp8cx_base_skip_false_prob[i] = skip_prob;
292
#endif
Paul Wilkins's avatar
Paul Wilkins committed
293 294
    }
}
Paul Wilkins's avatar
Paul Wilkins committed
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
void update_base_skip_probs(VP8_COMP *cpi)
{
    VP8_COMMON *cm = &cpi->common;

    if (cm->frame_type != KEY_FRAME)
    {
        update_skip_probs(cpi);

        if (cm->refresh_alt_ref_frame)
        {
#if CONFIG_NEWENTROPY
            int k;
            for (k=0; k<MBSKIP_CONTEXTS; ++k)
                cpi->last_skip_false_probs[2][k] = cm->mbskip_pred_probs[k];
#else
            cpi->last_skip_false_probs[2] = cpi->prob_skip_false;
#endif
            cpi->last_skip_probs_q[2] = cm->base_qindex;
        }
        else if (cpi->common.refresh_golden_frame)
        {
#if CONFIG_NEWENTROPY
            int k;
            for (k=0; k<MBSKIP_CONTEXTS; ++k)
                cpi->last_skip_false_probs[1][k] = cm->mbskip_pred_probs[k];
#else
            cpi->last_skip_false_probs[1] = cpi->prob_skip_false;
#endif
            cpi->last_skip_probs_q[1] = cm->base_qindex;
        }
        else
        {
#if CONFIG_NEWENTROPY
            int k;
            for (k=0; k<MBSKIP_CONTEXTS; ++k)
                cpi->last_skip_false_probs[0][k] = cm->mbskip_pred_probs[k];
#else
            cpi->last_skip_false_probs[0] = cpi->prob_skip_false;
#endif
            cpi->last_skip_probs_q[0] = cm->base_qindex;

            // update the baseline table for the current q
#if CONFIG_NEWENTROPY
            for (k=0; k<MBSKIP_CONTEXTS; ++k)
                cpi->base_skip_false_prob[cm->base_qindex][k] =
                    cm->mbskip_pred_probs[k];
#else
            cpi->base_skip_false_prob[cm->base_qindex] = cpi->prob_skip_false;
#endif
        }
    }

}
Paul Wilkins's avatar
Paul Wilkins committed
348

John Koleszar's avatar
John Koleszar committed
349 350 351 352 353 354 355 356 357 358
void vp8_initialize()
{
    static int init_done = 0;

    if (!init_done)
    {
        vp8_scale_machine_specific_config();
        vp8_initialize_common();
        //vp8_dmachine_specific_config();
        vp8_tokenize_initialize();
359
        vp8_init_quant_tables();
Paul Wilkins's avatar
Paul Wilkins committed
360
        vp8_init_me_luts();
361
        init_minq_luts();
Paul Wilkins's avatar
Paul Wilkins committed
362
        init_base_skip_probs();
John Koleszar's avatar
John Koleszar committed
363 364 365 366 367 368 369 370 371
        init_done = 1;
    }
}
#ifdef PACKET_TESTING
extern FILE *vpxlogc;
#endif

static void setup_features(VP8_COMP *cpi)
{
Paul Wilkins's avatar
Paul Wilkins committed
372 373
    MACROBLOCKD *xd = &cpi->mb.e_mbd;

John Koleszar's avatar
John Koleszar committed
374
    // Set up default state for MB feature flags
375 376 377

    xd->segmentation_enabled = 0;   // Default segmentation disabled

Paul Wilkins's avatar
Paul Wilkins committed
378 379 380 381
    xd->update_mb_segmentation_map = 0;
    xd->update_mb_segmentation_data = 0;
    vpx_memset(xd->mb_segment_tree_probs, 255, sizeof(xd->mb_segment_tree_probs));

Paul Wilkins's avatar
Paul Wilkins committed
382
    clearall_segfeatures( xd );
Paul Wilkins's avatar
Paul Wilkins committed
383 384 385 386 387 388 389

    xd->mode_ref_lf_delta_enabled = 0;
    xd->mode_ref_lf_delta_update = 0;
    vpx_memset(xd->ref_lf_deltas, 0, sizeof(xd->ref_lf_deltas));
    vpx_memset(xd->mode_lf_deltas, 0, sizeof(xd->mode_lf_deltas));
    vpx_memset(xd->last_ref_lf_deltas, 0, sizeof(xd->ref_lf_deltas));
    vpx_memset(xd->last_mode_lf_deltas, 0, sizeof(xd->mode_lf_deltas));
John Koleszar's avatar
John Koleszar committed
390

391
    set_default_lf_deltas(cpi);
John Koleszar's avatar
John Koleszar committed
392 393 394 395

}


396
static void dealloc_compressor_data(VP8_COMP *cpi)
John Koleszar's avatar
John Koleszar committed
397
{
398
    vpx_free(cpi->tplist);
399 400
    cpi->tplist = NULL;

401
    // Delete last frame MV storage buffers
402
    vpx_free(cpi->lfmv);
403 404
    cpi->lfmv = 0;

405
    vpx_free(cpi->lf_ref_frame_sign_bias);
406 407
    cpi->lf_ref_frame_sign_bias = 0;

408
    vpx_free(cpi->lf_ref_frame);
409
    cpi->lf_ref_frame = 0;
John Koleszar's avatar
John Koleszar committed
410 411

    // Delete sementation map
412
    vpx_free(cpi->segmentation_map);
John Koleszar's avatar
John Koleszar committed
413
    cpi->segmentation_map = 0;
414 415
    vpx_free(cpi->common.last_frame_seg_map);
    cpi->common.last_frame_seg_map = 0;
416 417
    vpx_free(cpi->coding_context.last_frame_seg_map_copy);
    cpi->coding_context.last_frame_seg_map_copy = 0;
John Koleszar's avatar
John Koleszar committed
418

419
    vpx_free(cpi->active_map);
John Koleszar's avatar
John Koleszar committed
420 421 422 423 424 425 426
    cpi->active_map = 0;

    vp8_de_alloc_frame_buffers(&cpi->common);

    vp8_yv12_de_alloc_frame_buffer(&cpi->last_frame_uf);
    vp8_yv12_de_alloc_frame_buffer(&cpi->scaled_source);
#if VP8_TEMPORAL_ALT_REF
John Koleszar's avatar
John Koleszar committed
427
    vp8_yv12_de_alloc_frame_buffer(&cpi->alt_ref_buffer);
John Koleszar's avatar
John Koleszar committed
428
#endif
John Koleszar's avatar
John Koleszar committed
429
    vp8_lookahead_destroy(cpi->lookahead);
John Koleszar's avatar
John Koleszar committed
430 431 432 433

    vpx_free(cpi->tok);
    cpi->tok = 0;

434
    // Structure used to monitor GF usage
435
    vpx_free(cpi->gf_active_flags);
436 437
    cpi->gf_active_flags = 0;

438 439 440 441 442 443
    // Activity mask based per mb zbin adjustments
    vpx_free(cpi->mb_activity_map);
    cpi->mb_activity_map = 0;
    vpx_free(cpi->mb_norm_activity_map);
    cpi->mb_norm_activity_map = 0;

444
    vpx_free(cpi->mb.pip);
445 446
    cpi->mb.pip = 0;

447 448
    vpx_free(cpi->twopass.total_stats);
    cpi->twopass.total_stats = 0;
449

450 451 452
    vpx_free(cpi->twopass.total_left_stats);
    cpi->twopass.total_left_stats = 0;

453 454
    vpx_free(cpi->twopass.this_frame_stats);
    cpi->twopass.this_frame_stats = 0;
John Koleszar's avatar
John Koleszar committed
455 456
}

457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
// Computes a q delta (in "q index" terms) to get from a starting q value
// to a target value
// target q value
static int compute_qdelta( VP8_COMP *cpi, double qstart, double qtarget )
{
    int i;
    int start_index = cpi->worst_quality;
    int target_index = cpi->worst_quality;

    // Convert the average q value to an index.
    for ( i = cpi->best_quality; i < cpi->worst_quality; i++ )
    {
        start_index = i;
        if ( vp8_convert_qindex_to_q(i) >= qstart )
            break;
    }

    // Convert the q target to an index
    for ( i = cpi->best_quality; i < cpi->worst_quality; i++ )
    {
        target_index = i;
        if ( vp8_convert_qindex_to_q(i) >= qtarget )
            break;
    }

    return target_index - start_index;
}

485 486 487
static void init_seg_features(VP8_COMP *cpi)
{
    VP8_COMMON *cm = &cpi->common;
488
    MACROBLOCKD *xd = &cpi->mb.e_mbd;
489

Paul Wilkins's avatar
Paul Wilkins committed
490
    int high_q = (int)(cpi->avg_q > 48.0);
491
    int qi_delta;
492

Paul Wilkins's avatar
Paul Wilkins committed
493 494
    // Disable and clear down for KF
    if ( cm->frame_type == KEY_FRAME  )
495 496 497
    {
        // Clear down the global segmentation map
        vpx_memset( cpi->segmentation_map, 0, (cm->mb_rows * cm->mb_cols));
498 499
        xd->update_mb_segmentation_map = 0;
        xd->update_mb_segmentation_data = 0;
500
        cpi->static_mb_pct = 0;
501

502 503
        // Disable segmentation
        vp8_disable_segmentation((VP8_PTR)cpi);
Paul Wilkins's avatar
Paul Wilkins committed
504 505 506

        // Clear down the segment features.
        clearall_segfeatures(xd);
507
    }
508

Paul Wilkins's avatar
Paul Wilkins committed
509 510
    // If this is an alt ref frame
    else if ( cm->refresh_alt_ref_frame )
511
    {
Paul Wilkins's avatar
Paul Wilkins committed
512 513 514 515
        // Clear down the global segmentation map
        vpx_memset( cpi->segmentation_map, 0, (cm->mb_rows * cm->mb_cols));
        xd->update_mb_segmentation_map = 0;
        xd->update_mb_segmentation_data = 0;
516
        cpi->static_mb_pct = 0;
Paul Wilkins's avatar
Paul Wilkins committed
517 518 519 520 521 522 523 524 525 526 527 528

        // Disable segmentation and individual segment features by default
        vp8_disable_segmentation((VP8_PTR)cpi);
        clearall_segfeatures(xd);

        // Scan frames from current to arf frame.
        // This function re-enables segmentation if appropriate.
        vp8_update_mbgraph_stats(cpi);

        // If segmentation was enabled set those features needed for the
        // arf itself.
        if ( xd->segmentation_enabled )
529
        {
Paul Wilkins's avatar
Paul Wilkins committed
530 531 532
            xd->update_mb_segmentation_map = 1;
            xd->update_mb_segmentation_data = 1;

533 534
            qi_delta = compute_qdelta( cpi, cpi->avg_q, (cpi->avg_q * 0.875) );
            set_segdata( xd, 1, SEG_LVL_ALT_Q, (qi_delta - 2) );
Paul Wilkins's avatar
Paul Wilkins committed
535
            set_segdata( xd, 1, SEG_LVL_ALT_LF, -2 );
Paul Wilkins's avatar
Paul Wilkins committed
536

537 538 539 540
            enable_segfeature(xd, 1, SEG_LVL_ALT_Q);
            enable_segfeature(xd, 1, SEG_LVL_ALT_LF);

            // Where relevant assume segment data is delta data
Paul Wilkins's avatar
Paul Wilkins committed
541
            xd->mb_segment_abs_delta = SEGMENT_DELTADATA;
Paul Wilkins's avatar
Paul Wilkins committed
542

543
        }
544
    }
Paul Wilkins's avatar
Paul Wilkins committed
545
    // All other frames if segmentation has been enabled
546
    else if ( xd->segmentation_enabled )
547
    {
548
/*
549 550 551 552 553 554 555 556 557 558 559 560
        int i;

        // clears prior frame seg lev refs
        for (i = 0; i < MAX_MB_SEGMENTS; i++)
        {
            // only do it if the force drop the background stuff is off
            if(!segfeature_active(xd, i, SEG_LVL_MODE))
            {
                disable_segfeature(xd,i,SEG_LVL_REF_FRAME);
                set_segdata( xd,i, SEG_LVL_REF_FRAME, 0xffffff);
            }
        }
561
*/
562

Paul Wilkins's avatar
Paul Wilkins committed
563 564
        // First normal frame in a valid gf or alt ref group
        if ( cpi->common.frames_since_golden == 0 )
565
        {
Paul Wilkins's avatar
Paul Wilkins committed
566 567
            // Set up segment features for normal frames in an af group
            if ( cpi->source_alt_ref_active )
568
            {
Paul Wilkins's avatar
Paul Wilkins committed
569
                xd->update_mb_segmentation_map = 0;
570
                xd->update_mb_segmentation_data = 1;
Paul Wilkins's avatar
Paul Wilkins committed
571
                xd->mb_segment_abs_delta = SEGMENT_DELTADATA;
Paul Wilkins's avatar
Paul Wilkins committed
572

573 574 575 576
                qi_delta = compute_qdelta( cpi, cpi->avg_q,
                                           (cpi->avg_q * 1.125) );
                set_segdata( xd, 1, SEG_LVL_ALT_Q, (qi_delta + 2) );
                set_segdata( xd, 1, SEG_LVL_ALT_Q, 0 );
Paul Wilkins's avatar
Paul Wilkins committed
577
                enable_segfeature(xd, 1, SEG_LVL_ALT_Q);
Paul Wilkins's avatar
Paul Wilkins committed
578 579

                set_segdata( xd, 1, SEG_LVL_ALT_LF, -2 );
Paul Wilkins's avatar
Paul Wilkins committed
580 581
                enable_segfeature(xd, 1, SEG_LVL_ALT_LF);

582 583
                // Segment coding disabled for compred testing
                if ( high_q || (cpi->static_mb_pct == 100) )
Paul Wilkins's avatar
Paul Wilkins committed
584
                {
Paul Wilkins's avatar
Paul Wilkins committed
585
                    //set_segref(xd, 1, LAST_FRAME);
Paul Wilkins's avatar
Paul Wilkins committed
586
                    set_segref(xd, 1, ALTREF_FRAME);
Paul Wilkins's avatar
Paul Wilkins committed
587
                    enable_segfeature(xd, 1, SEG_LVL_REF_FRAME);
Paul Wilkins's avatar
Paul Wilkins committed
588 589

                    set_segdata( xd, 1, SEG_LVL_MODE, ZEROMV );
Paul Wilkins's avatar
Paul Wilkins committed
590
                    enable_segfeature(xd, 1, SEG_LVL_MODE);
Paul Wilkins's avatar
Paul Wilkins committed
591

592 593 594
                    // EOB segment coding not fixed for 8x8 yet
                    set_segdata( xd, 1, SEG_LVL_EOB, 0 );
                    enable_segfeature(xd, 1, SEG_LVL_EOB);
Paul Wilkins's avatar
Paul Wilkins committed
595
                }
596
            }
Paul Wilkins's avatar
Paul Wilkins committed
597 598
            // Disable segmentation and clear down features if alt ref
            // is not active for this group
599 600
            else
            {
Paul Wilkins's avatar
Paul Wilkins committed
601 602
                vp8_disable_segmentation((VP8_PTR)cpi);

603 604
                vpx_memset( cpi->segmentation_map, 0,
                            (cm->mb_rows * cm->mb_cols));
Paul Wilkins's avatar
Paul Wilkins committed
605 606 607 608 609

                xd->update_mb_segmentation_map = 0;
                xd->update_mb_segmentation_data = 0;

                clearall_segfeatures(xd);
610 611
            }
        }
Paul Wilkins's avatar
Paul Wilkins committed
612 613 614

        // Special case where we are coding over the top of a previous
        // alt ref frame
615
        // Segment coding disabled for compred testing
Paul Wilkins's avatar
Paul Wilkins committed
616 617 618 619 620
        else if ( cpi->is_src_frame_alt_ref )
        {
            // Enable mode and ref frame features for segment 0 as well
            enable_segfeature(xd, 0, SEG_LVL_REF_FRAME);
            enable_segfeature(xd, 0, SEG_LVL_MODE);
621 622
            enable_segfeature(xd, 1, SEG_LVL_REF_FRAME);
            enable_segfeature(xd, 1, SEG_LVL_MODE);
Paul Wilkins's avatar
Paul Wilkins committed
623

Paul Wilkins's avatar
Paul Wilkins committed
624 625 626 627 628
            // All mbs should use ALTREF_FRAME, ZEROMV exclusively
            clear_segref(xd, 0);
            set_segref(xd, 0, ALTREF_FRAME);
            clear_segref(xd, 1);
            set_segref(xd, 1, ALTREF_FRAME);
Paul Wilkins's avatar
Paul Wilkins committed
629 630
            set_segdata( xd, 0, SEG_LVL_MODE, ZEROMV );
            set_segdata( xd, 1, SEG_LVL_MODE, ZEROMV );
Paul Wilkins's avatar
Paul Wilkins committed
631

632 633 634
            // Skip all MBs if high Q
            if ( high_q )
            {
635 636 637 638
                enable_segfeature(xd, 0, SEG_LVL_EOB);
                set_segdata( xd, 0, SEG_LVL_EOB, 0 );
                enable_segfeature(xd, 1, SEG_LVL_EOB);
                set_segdata( xd, 1, SEG_LVL_EOB, 0 );
639
            }
Paul Wilkins's avatar
Paul Wilkins committed
640 641 642 643
            // Enable data udpate
            xd->update_mb_segmentation_data = 1;
        }
        // All other frames.
644 645
        else
        {
Adrian Grange's avatar
Adrian Grange committed
646
            // No updates.. leave things as they are.
Paul Wilkins's avatar
Paul Wilkins committed
647
            xd->update_mb_segmentation_map = 0;
648
            xd->update_mb_segmentation_data = 0;
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
        }
    }
}

// DEBUG: Print out the segment id of each MB in the current frame.
static void print_seg_map(VP8_COMP *cpi)
{
    VP8_COMMON *cm = & cpi->common;
    int row,col;
    int map_index = 0;
    FILE *statsfile;

    statsfile = fopen("segmap.stt", "a");

    fprintf(statsfile, "%10d\n",
            cm->current_video_frame );

    for ( row = 0; row < cpi->common.mb_rows; row++ )
    {
        for ( col = 0; col < cpi->common.mb_cols; col++ )
        {
            fprintf(statsfile, "%10d",
                    cpi->segmentation_map[map_index]);
            map_index++;
        }
        fprintf(statsfile, "\n");
    }
    fprintf(statsfile, "\n");

    fclose(statsfile);
}

681
static void set_default_lf_deltas(VP8_COMP *cpi)
John Koleszar's avatar
John Koleszar committed
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
{
    cpi->mb.e_mbd.mode_ref_lf_delta_enabled = 1;
    cpi->mb.e_mbd.mode_ref_lf_delta_update = 1;

    vpx_memset(cpi->mb.e_mbd.ref_lf_deltas, 0, sizeof(cpi->mb.e_mbd.ref_lf_deltas));
    vpx_memset(cpi->mb.e_mbd.mode_lf_deltas, 0, sizeof(cpi->mb.e_mbd.mode_lf_deltas));

    // Test of ref frame deltas
    cpi->mb.e_mbd.ref_lf_deltas[INTRA_FRAME] = 2;
    cpi->mb.e_mbd.ref_lf_deltas[LAST_FRAME] = 0;
    cpi->mb.e_mbd.ref_lf_deltas[GOLDEN_FRAME] = -2;
    cpi->mb.e_mbd.ref_lf_deltas[ALTREF_FRAME] = -2;

    cpi->mb.e_mbd.mode_lf_deltas[0] = 4;               // BPRED
    cpi->mb.e_mbd.mode_lf_deltas[1] = -2;              // Zero
    cpi->mb.e_mbd.mode_lf_deltas[2] = 2;               // New mv
    cpi->mb.e_mbd.mode_lf_deltas[3] = 4;               // Split mv
}

void vp8_set_speed_features(VP8_COMP *cpi)
{
    SPEED_FEATURES *sf = &cpi->sf;
    int Mode = cpi->compressor_speed;
    int Speed = cpi->Speed;
    int i;
    VP8_COMMON *cm = &cpi->common;

Paul Wilkins's avatar
Paul Wilkins committed
709 710 711 712
    // Only modes 0 and 1 supported for now in experimental code basae
    if ( Mode > 1 )
        Mode = 1;

John Koleszar's avatar
John Koleszar committed
713 714 715 716 717 718 719 720
    // Initialise default mode frequency sampling variables
    for (i = 0; i < MAX_MODES; i ++)
    {
        cpi->mode_check_freq[i] = 0;
        cpi->mode_test_hit_counts[i] = 0;
        cpi->mode_chosen_counts[i] = 0;
    }

721
    // best quality defaults
John Koleszar's avatar
John Koleszar committed
722 723 724 725 726 727 728 729 730
    sf->RD = 1;
    sf->search_method = NSTEP;
    sf->improved_dct = 1;
    sf->auto_filter = 1;
    sf->recode_loop = 1;
    sf->quarter_pixel_search = 1;
    sf->half_pixel_search = 1;
    sf->iterative_sub_pixel = 1;
    sf->optimize_coefficients = 1;
731
    sf->no_skip_block4x4_search = 1;
John Koleszar's avatar
John Koleszar committed
732 733 734

    sf->first_step = 0;
    sf->max_step_search_steps = MAX_MVSEARCH_STEPS;
735
    sf->improved_mv_pred = 1;
John Koleszar's avatar
John Koleszar committed
736 737 738 739 740 741 742 743

    // default thresholds to 0
    for (i = 0; i < MAX_MODES; i++)
        sf->thresh_mult[i] = 0;

    switch (Mode)
    {
    case 0: // best quality mode
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778
#if CONFIG_PRED_FILTER
        sf->thresh_mult[THR_ZEROMV        ] = 0;
        sf->thresh_mult[THR_ZEROMV_FILT   ] = 0;
        sf->thresh_mult[THR_ZEROG         ] = 0;
        sf->thresh_mult[THR_ZEROG_FILT    ] = 0;
        sf->thresh_mult[THR_ZEROA         ] = 0;
        sf->thresh_mult[THR_ZEROA_FILT    ] = 0;
        sf->thresh_mult[THR_NEARESTMV     ] = 0;
        sf->thresh_mult[THR_NEARESTMV_FILT] = 0;
        sf->thresh_mult[THR_NEARESTG      ] = 0;
        sf->thresh_mult[THR_NEARESTG_FILT ] = 0;
        sf->thresh_mult[THR_NEARESTA      ] = 0;
        sf->thresh_mult[THR_NEARESTA_FILT ] = 0;
        sf->thresh_mult[THR_NEARMV        ] = 0;
        sf->thresh_mult[THR_NEARMV_FILT   ] = 0;
        sf->thresh_mult[THR_NEARG         ] = 0;
        sf->thresh_mult[THR_NEARG_FILT    ] = 0;
        sf->thresh_mult[THR_NEARA         ] = 0;
        sf->thresh_mult[THR_NEARA_FILT    ] = 0;

        sf->thresh_mult[THR_DC       ] = 0;

        sf->thresh_mult[THR_V_PRED   ] = 1000;
        sf->thresh_mult[THR_H_PRED   ] = 1000;
        sf->thresh_mult[THR_B_PRED   ] = 2000;
        sf->thresh_mult[THR_I8X8_PRED] = 2000;
        sf->thresh_mult[THR_TM       ] = 1000;

        sf->thresh_mult[THR_NEWMV    ] = 1000;
        sf->thresh_mult[THR_NEWG     ] = 1000;
        sf->thresh_mult[THR_NEWA     ] = 1000;
        sf->thresh_mult[THR_NEWMV_FILT    ] = 1000;
        sf->thresh_mult[THR_NEWG_FILT     ] = 1000;
        sf->thresh_mult[THR_NEWA_FILT     ] = 1000;
#else
John Koleszar's avatar
John Koleszar committed
779 780 781 782 783 784 785 786 787 788 789 790 791 792
        sf->thresh_mult[THR_ZEROMV   ] = 0;
        sf->thresh_mult[THR_ZEROG    ] = 0;
        sf->thresh_mult[THR_ZEROA    ] = 0;
        sf->thresh_mult[THR_NEARESTMV] = 0;
        sf->thresh_mult[THR_NEARESTG ] = 0;
        sf->thresh_mult[THR_NEARESTA ] = 0;
        sf->thresh_mult[THR_NEARMV   ] = 0;
        sf->thresh_mult[THR_NEARG    ] = 0;
        sf->thresh_mult[THR_NEARA    ] = 0;

        sf->thresh_mult[THR_DC       ] = 0;

        sf->thresh_mult[THR_V_PRED   ] = 1000;
        sf->thresh_mult[THR_H_PRED   ] = 1000;
793 794 795 796 797 798 799 800
#if CONFIG_NEWINTRAMODES
        sf->thresh_mult[THR_D45_PRED ] = 1000;
        sf->thresh_mult[THR_D135_PRED] = 1000;
        sf->thresh_mult[THR_D117_PRED] = 1000;
        sf->thresh_mult[THR_D153_PRED] = 1000;
        sf->thresh_mult[THR_D27_PRED ] = 1000;
        sf->thresh_mult[THR_D63_PRED ] = 1000;
#endif
John Koleszar's avatar
John Koleszar committed
801
        sf->thresh_mult[THR_B_PRED   ] = 2000;
802
        sf->thresh_mult[THR_I8X8_PRED] = 2000;
John Koleszar's avatar
John Koleszar committed
803 804 805 806 807
        sf->thresh_mult[THR_TM       ] = 1000;

        sf->thresh_mult[THR_NEWMV    ] = 1000;
        sf->thresh_mult[THR_NEWG     ] = 1000;
        sf->thresh_mult[THR_NEWA     ] = 1000;
808
#endif
John Koleszar's avatar
John Koleszar committed
809 810 811 812
        sf->thresh_mult[THR_SPLITMV  ] = 2500;
        sf->thresh_mult[THR_SPLITG   ] = 5000;
        sf->thresh_mult[THR_SPLITA   ] = 5000;

813 814 815 816 817 818 819 820 821 822 823 824 825
        sf->thresh_mult[THR_COMP_ZEROLG   ] = 0;
        sf->thresh_mult[THR_COMP_NEARESTLG] = 0;
        sf->thresh_mult[THR_COMP_NEARLG   ] = 0;
        sf->thresh_mult[THR_COMP_ZEROLA   ] = 0;
        sf->thresh_mult[THR_COMP_NEARESTLA] = 0;
        sf->thresh_mult[THR_COMP_NEARLA   ] = 0;
        sf->thresh_mult[THR_COMP_ZEROGA   ] = 0;
        sf->thresh_mult[THR_COMP_NEARESTGA] = 0;
        sf->thresh_mult[THR_COMP_NEARGA   ] = 0;

        sf->thresh_mult[THR_COMP_NEWLG    ] = 1000;
        sf->thresh_mult[THR_COMP_NEWLA    ] = 1000;
        sf->thresh_mult[THR_COMP_NEWGA    ] = 1000;
John Koleszar's avatar
John Koleszar committed
826

827 828 829 830
        sf->thresh_mult[THR_COMP_SPLITLA  ] = 2500;
        sf->thresh_mult[THR_COMP_SPLITGA  ] = 5000;
        sf->thresh_mult[THR_COMP_SPLITLG  ] = 5000;

John Koleszar's avatar
John Koleszar committed
831 832
        sf->first_step = 0;
        sf->max_step_search_steps = MAX_MVSEARCH_STEPS;
833 834 835
#if CONFIG_ENHANCED_INTERP
        sf->search_best_filter = SEARCH_BEST_FILTER;
#endif
John Koleszar's avatar
John Koleszar committed
836 837
        break;
    case 1:
838
#if CONFIG_PRED_FILTER
John Koleszar's avatar
John Koleszar committed
839
        sf->thresh_mult[THR_NEARESTMV] = 0;
840
        sf->thresh_mult[THR_NEARESTMV_FILT] = 0;
John Koleszar's avatar
John Koleszar committed
841
        sf->thresh_mult[THR_ZEROMV   ] = 0;
842
        sf->thresh_mult[THR_ZEROMV_FILT   ] = 0;
John Koleszar's avatar
John Koleszar committed
843 844
        sf->thresh_mult[THR_DC       ] = 0;
        sf->thresh_mult[THR_NEARMV   ] = 0;
845
        sf->thresh_mult[THR_NEARMV_FILT   ] = 0;
John Koleszar's avatar
John Koleszar committed
846 847
        sf->thresh_mult[THR_V_PRED   ] = 1000;
        sf->thresh_mult[THR_H_PRED   ] = 1000;
848 849 850 851 852 853 854 855
#if CONFIG_NEWINTRAMODES
        sf->thresh_mult[THR_D45_PRED ] = 1000;
        sf->thresh_mult[THR_D135_PRED] = 1000;
        sf->thresh_mult[THR_D117_PRED] = 1000;
        sf->thresh_mult[THR_D153_PRED] = 1000;
        sf->thresh_mult[THR_D27_PRED ] = 1000;
        sf->thresh_mult[THR_D63_PRED ] = 1000;
#endif
John Koleszar's avatar
John Koleszar committed
856
        sf->thresh_mult[THR_B_PRED   ] = 2500;
857
        sf->thresh_mult[THR_I8X8_PRED] = 2500;
John Koleszar's avatar
John Koleszar committed
858 859 860
        sf->thresh_mult[THR_TM       ] = 1000;

        sf->thresh_mult[THR_NEARESTG ] = 1000;
861
        sf->thresh_mult[THR_NEARESTG_FILT ] = 1000;
John Koleszar's avatar
John Koleszar committed
862
        sf->thresh_mult[THR_NEARESTA ] = 1000;
863
        sf->thresh_mult[THR_NEARESTA_FILT ] = 1000;
John Koleszar's avatar
John Koleszar committed
864 865 866 867 868

        sf->thresh_mult[THR_ZEROG    ] = 1000;
        sf->thresh_mult[THR_ZEROA    ] = 1000;
        sf->thresh_mult[THR_NEARG    ] = 1000;
        sf->thresh_mult[THR_NEARA    ] = 1000;
869 870 871 872
        sf->thresh_mult[THR_ZEROG_FILT    ] = 1000;
        sf->thresh_mult[THR_ZEROA_FILT    ] = 1000;
        sf->thresh_mult[THR_NEARG_FILT    ] = 1000;
        sf->thresh_mult[THR_NEARA_FILT    ] = 1000;
John Koleszar's avatar
John Koleszar committed
873

874 875 876 877 878 879 880 881 882
        sf->thresh_mult[THR_ZEROMV   ] = 0;
        sf->thresh_mult[THR_ZEROG    ] = 0;
        sf->thresh_mult[THR_ZEROA    ] = 0;
        sf->thresh_mult[THR_NEARESTMV] = 0;
        sf->thresh_mult[THR_NEARESTG ] = 0;
        sf->thresh_mult[THR_NEARESTA ] = 0;
        sf->thresh_mult[THR_NEARMV   ] = 0;
        sf->thresh_mult[THR_NEARG    ] = 0;
        sf->thresh_mult[THR_NEARA    ] = 0;
883 884 885 886 887 888 889 890 891
        sf->thresh_mult[THR_ZEROMV_FILT   ] = 0;
        sf->thresh_mult[THR_ZEROG_FILT    ] = 0;
        sf->thresh_mult[THR_ZEROA_FILT    ] = 0;
        sf->thresh_mult[THR_NEARESTMV_FILT] = 0;
        sf->thresh_mult[THR_NEARESTG_FILT ] = 0;
        sf->thresh_mult[THR_NEARESTA_FILT ] = 0;
        sf->thresh_mult[THR_NEARMV_FILT   ] = 0;
        sf->thresh_mult[THR_NEARG_FILT    ] = 0;
        sf->thresh_mult[THR_NEARA_FILT    ] = 0;
892 893 894 895

        sf->thresh_mult[THR_NEWMV    ] = 1000;
        sf->thresh_mult[THR_NEWG     ] = 1000;
        sf->thresh_mult[THR_NEWA     ] = 1000;
896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916
        sf->thresh_mult[THR_NEWMV_FILT    ] = 1000;
        sf->thresh_mult[THR_NEWG_FILT     ] = 1000;
        sf->thresh_mult[THR_NEWA_FILT     ] = 1000;
#else
        sf->thresh_mult[THR_NEARESTMV] = 0;
        sf->thresh_mult[THR_ZEROMV   ] = 0;
        sf->thresh_mult[THR_DC       ] = 0;
        sf->thresh_mult[THR_NEARMV   ] = 0;
        sf->thresh_mult[THR_V_PRED   ] = 1000;
        sf->thresh_mult[THR_H_PRED   ] = 1000;
#if CONFIG_NEWINTRAMODES
        sf->thresh_mult[THR_D45_PRED ] = 1000;
        sf->thresh_mult[THR_D135_PRED] = 1000;
        sf->thresh_mult[THR_D117_PRED] = 1000;
        sf->thresh_mult[THR_D153_PRED] = 1000;
        sf->thresh_mult[THR_D27_PRED ] = 1000;
        sf->thresh_mult[THR_D63_PRED ] = 1000;
#endif
        sf->thresh_mult[THR_B_PRED   ] = 2500;
        sf->thresh_mult[THR_I8X8_PRED] = 2500;
        sf->thresh_mult[THR_TM       ] = 1000;
917

918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939
        sf->thresh_mult[THR_NEARESTG ] = 1000;
        sf->thresh_mult[THR_NEARESTA ] = 1000;

        sf->thresh_mult[THR_ZEROG    ] = 1000;
        sf->thresh_mult[THR_ZEROA    ] = 1000;
        sf->thresh_mult[THR_NEARG    ] = 1000;
        sf->thresh_mult[THR_NEARA    ] = 1000;

        sf->thresh_mult[THR_ZEROMV   ] = 0;
        sf->thresh_mult[THR_ZEROG    ] = 0;
        sf->thresh_mult[THR_ZEROA    ] = 0;
        sf->thresh_mult[THR_NEARESTMV] = 0;
        sf->thresh_mult[THR_NEARESTG ] = 0;
        sf->thresh_mult[THR_NEARESTA ] = 0;
        sf->thresh_mult[THR_NEARMV   ] = 0;
        sf->thresh_mult[THR_NEARG    ] = 0;
        sf->thresh_mult[THR_NEARA    ] = 0;

        sf->thresh_mult[THR_NEWMV    ] = 1000;
        sf->thresh_mult[THR_NEWG     ] = 1000;
        sf->thresh_mult[THR_NEWA     ] = 1000;
#endif
940 941 942
        sf->thresh_mult[THR_SPLITMV  ] = 1700;
        sf->thresh_mult[THR_SPLITG   ] = 4500;
        sf->thresh_mult[THR_SPLITA   ] = 4500;
943

944 945 946 947 948 949 950 951 952
        sf->thresh_mult[THR_COMP_ZEROLG   ] = 0;
        sf->thresh_mult[THR_COMP_NEARESTLG] = 0;
        sf->thresh_mult[THR_COMP_NEARLG   ] = 0;
        sf->thresh_mult[THR_COMP_ZEROLA   ] = 0;
        sf->thresh_mult[THR_COMP_NEARESTLA] = 0;
        sf->thresh_mult[THR_COMP_NEARLA   ] = 0;
        sf->thresh_mult[THR_COMP_ZEROGA   ] = 0;
        sf->thresh_mult[THR_COMP_NEARESTGA] = 0;
        sf->thresh_mult[THR_COMP_NEARGA   ] = 0;
953

954 955 956
        sf->thresh_mult[THR_COMP_NEWLG    ] = 1000;
        sf->thresh_mult[THR_COMP_NEWLA    ] = 1000;
        sf->thresh_mult[THR_COMP_NEWGA    ] = 1000;
John Koleszar's avatar
John Koleszar committed
957

958 959 960 961
        sf->thresh_mult[THR_COMP_SPLITLA  ] = 1700;
        sf->thresh_mult[THR_COMP_SPLITGA  ] = 4500;
        sf->thresh_mult[THR_COMP_SPLITLG  ] = 4500;

962
        if (Speed > 0)
John Koleszar's avatar
John Koleszar committed
963
        {
964 965 966
            /* Disable coefficient optimization above speed 0 */
            sf->optimize_coefficients = 0;
            sf->no_skip_block4x4_search = 0;
967

968
            sf->first_step = 1;
969

970 971 972
            cpi->mode_check_freq[THR_SPLITG] = 2;
            cpi->mode_check_freq[THR_SPLITA] = 2;
            cpi->mode_check_freq[THR_SPLITMV] = 0;
973 974 975 976

            cpi->mode_check_freq[THR_COMP_SPLITGA] = 2;
            cpi->mode_check_freq[THR_COMP_SPLITLG] = 2;
            cpi->mode_check_freq[THR_COMP_SPLITLA] = 0;
John Koleszar's avatar
John Koleszar committed
977 978
        }

979
        if (Speed > 1)
John Koleszar's avatar
John Koleszar committed
980 981 982
        {
            cpi->mode_check_freq[THR_SPLITG] = 4;
            cpi->mode_check_freq[THR_SPLITA] = 4;
983
            cpi->mode_check_freq[THR_SPLITMV] = 2;
John Koleszar's avatar
John Koleszar committed
984

985 986 987 988
            cpi->mode_check_freq[THR_COMP_SPLITGA] = 4;
            cpi->mode_check_freq[THR_COMP_SPLITLG] = 4;
            cpi->mode_check_freq[THR_COMP_SPLITLA] = 2;

John Koleszar's avatar
John Koleszar committed
989 990 991
            sf->thresh_mult[THR_TM       ] = 1500;
            sf->thresh_mult[THR_V_PRED   ] = 1500;
            sf->thresh_mult[THR_H_PRED   ] = 1500;
992 993 994 995 996 997 998 999
#if CONFIG_NEWINTRAMODES
            sf->thresh_mult[THR_D45_PRED ] = 1500;
            sf->thresh_mult[THR_D135_PRED] = 1500;
            sf->thresh_mult[THR_D117_PRED] = 1500;
            sf->thresh_mult[THR_D153_PRED] = 1500;
            sf->thresh_mult[THR_D27_PRED ] = 1500;
            sf->thresh_mult[THR_D63_PRED ] = 1500;
#endif
John Koleszar's avatar
John Koleszar committed
1000
            sf->thresh_mult[THR_B_PRED   ] = 5000;
1001
            sf->thresh_mult[THR_I8X8_PRED] = 5000;
Yaowu Xu's avatar
Yaowu Xu committed
1002

John Koleszar's avatar
John Koleszar committed
1003 1004 1005
            if (cpi->ref_frame_flags & VP8_LAST_FLAG)
            {
                sf->thresh_mult[THR_NEWMV    ] = 2000;
1006 1007 1008
#if CONFIG_PRED_FILTER
                sf->thresh_mult[THR_NEWMV_FILT    ] = 2000;
#endif
John Koleszar's avatar
John Koleszar committed
1009
                sf->thresh_mult[THR_SPLITMV  ] = 10000;
1010
                sf->thresh_mult[THR_COMP_SPLITLG  ] = 20000;
John Koleszar's avatar
John Koleszar committed
1011 1012 1013 1014 1015 1016 1017 1018
            }

            if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
            {
                sf->thresh_mult[THR_NEARESTG ] = 1500;
                sf->thresh_mult[THR_ZEROG    ] = 1500;
                sf->thresh_mult[THR_NEARG    ] = 1500;
                sf->thresh_mult[THR_NEWG     ] = 2000;
1019 1020 1021 1022 1023 1024
#if CONFIG_PRED_FILTER
                sf->thresh_mult[THR_NEARESTG_FILT ] = 1500;
                sf->thresh_mult[THR_ZEROG_FILT    ] = 1500;
                sf->thresh_mult[THR_NEARG_FILT    ] = 1500;
                sf->thresh_mult[THR_NEWG_FILT     ] = 2000;
#endif
John Koleszar's avatar
John Koleszar committed
1025
                sf->thresh_mult[THR_SPLITG   ] = 20000;
1026
                sf->thresh_mult[THR_COMP_SPLITGA  ] = 20000;
John Koleszar's avatar
John Koleszar committed
1027 1028 1029 1030 1031 1032 1033 1034
            }

            if (cpi->ref_frame_flags & VP8_ALT_FLAG)
            {
                sf->thresh_mult[THR_NEARESTA ] = 1500;
                sf->thresh_mult[THR_ZEROA    ] = 1500;
                sf->thresh_mult[THR_NEARA    ] = 1500;
                sf->thresh_mult[THR_NEWA     ] = 2000;
1035 1036 1037 1038 1039 1040
#if CONFIG_PRED_FILTER
                sf->thresh_mult[THR_NEARESTA_FILT ] = 1500;
                sf->thresh_mult[THR_ZEROA_FILT    ] = 1500;
                sf->thresh_mult[THR_NEARA_FILT    ] = 1500;
                sf->thresh_mult[THR_NEWA_FILT     ] = 2000;
#endif
John Koleszar's avatar
John Koleszar committed
1041
                sf->thresh_mult[THR_SPLITA   ] = 20000;
1042
                sf->thresh_mult[THR_COMP_SPLITLA  ] = 10000;
John Koleszar's avatar
John Koleszar committed
1043
            }
1044

1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
            sf->thresh_mult[THR_COMP_ZEROLG   ] = 1500;
            sf->thresh_mult[THR_COMP_NEARESTLG] = 1500;
            sf->thresh_mult[THR_COMP_NEARLG   ] = 1500;
            sf->thresh_mult[THR_COMP_ZEROLA   ] = 1500;
            sf->thresh_mult[THR_COMP_NEARESTLA] = 1500;
            sf->thresh_mult[THR_COMP_NEARLA   ] = 1500;
            sf->thresh_mult[THR_COMP_ZEROGA   ] = 1500;
            sf->thresh_mult[THR_COMP_NEARESTGA] = 1500;
            sf->thresh_mult[THR_COMP_NEARGA   ] = 1500;

            sf->thresh_mult[THR_COMP_NEWLG    ] = 2000;
            sf->thresh_mult[THR_COMP_NEWLA    ] = 2000;
            sf->thresh_mult[THR_COMP_NEWGA    ] = 2000;
John Koleszar's avatar
John Koleszar committed
1058 1059
        }

1060
        if (Speed > 2)
John Koleszar's avatar
John Koleszar committed
1061 1062 1063 1064 1065
        {
            cpi->mode_check_freq[THR_SPLITG] = 15;
            cpi->mode_check_freq[THR_SPLITA] = 15;
            cpi->mode_check_freq[THR_SPLITMV] = 7;

1066 1067 1068 1069
            cpi->mode_check_freq[THR_COMP_SPLITGA] = 15;
            cpi->mode_check_freq[THR_COMP_SPLITLG] = 15;
            cpi->mode_check_freq[THR_COMP_SPLITLA] = 7;

John Koleszar's avatar
John Koleszar committed
1070 1071 1072
            sf->thresh_mult[THR_TM       ] = 2000;
            sf->thresh_mult[THR_V_PRED   ] = 2000;
            sf->thresh_mult[THR_H_PRED   ] = 2000;
1073 1074 1075 1076 1077 1078 1079 1080
#if CONFIG_NEWINTRAMODES
            sf->thresh_mult[THR_D45_PRED ] = 2000;
            sf->thresh_mult[THR_D135_PRED] = 2000;
            sf->thresh_mult[THR_D117_PRED] = 2000;
            sf->thresh_mult[THR_D153_PRED] = 2000;
            sf->thresh_mult[THR_D27_PRED ] = 2000;
            sf->thresh_mult[THR_D63_PRED ] = 2000;
#endif
John Koleszar's avatar
John Koleszar committed
1081
            sf->thresh_mult[THR_B_PRED   ] = 7500;
1082
            sf->thresh_mult[THR_I8X8_PRED] = 7500;
John Koleszar's avatar
John Koleszar committed
1083 1084 1085 1086

            if (cpi->ref_frame_flags & VP8_LAST_FLAG)
            {
                sf->thresh_mult[THR_NEWMV    ] = 2000;
1087 1088 1089
#if CONFIG_PRED_FILTER
                sf->thresh_mult[THR_NEWMV_FILT    ] = 2000;
#endif
John Koleszar's avatar
John Koleszar committed
1090
                sf->thresh_mult[THR_SPLITMV  ] = 25000;
1091
                sf->thresh_mult[THR_COMP_SPLITLG  ] = 50000;
John Koleszar's avatar
John Koleszar committed
1092 1093 1094 1095 1096 1097 1098 1099
            }

            if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
            {
                sf->thresh_mult[THR_NEARESTG ] = 2000;
                sf->thresh_mult[THR_ZEROG    ] = 2000;
                sf->thresh_mult[THR_NEARG    ] = 2000;
                sf->thresh_mult[THR_NEWG     ] = 2500;
1100 1101 1102 1103 1104 1105
#if CONFIG_PRED_FILTER
                sf->thresh_mult[THR_NEARESTG_FILT ] = 2000;
                sf->thresh_mult[THR_ZEROG_FILT    ] = 2000;
                sf->thresh_mult[THR_NEARG_FILT    ] = 2000;
                sf->thresh_mult[THR_NEWG_FILT     ] = 2500;
#endif
John Koleszar's avatar
John Koleszar committed
1106
                sf->thresh_mult[THR_SPLITG   ] = 50000;
1107
                sf->thresh_mult[THR_COMP_SPLITGA  ] = 50000;
John Koleszar's avatar
John Koleszar committed
1108 1109 1110 1111 1112 1113 1114 1115
            }

            if (cpi->ref_frame_flags & VP8_ALT_FLAG)
            {
                sf->thresh_mult[THR_NEARESTA ] = 2000;
                sf->thresh_mult[THR_ZEROA    ] = 2000;
                sf->thresh_mult[THR_NEARA    ] = 2000;
                sf->thresh_mult[THR_NEWA     ] = 2500;
1116 1117 1118 1119 1120 1121
#if CONFIG_PRED_FILTER
                sf->thresh_mult[THR_NEARESTA_FILT ] = 2000;
                sf->thresh_mult[THR_ZEROA_FILT    ] = 2000;
                sf->thresh_mult[THR_NEARA_FILT    ] = 2000;
                sf->thresh_mult[THR_NEWA_FILT     ] = 2500;
#endif
John Koleszar's avatar
John Koleszar committed
1122
                sf->thresh_mult[THR_SPLITA   ] = 50000;
1123
                sf->thresh_mult[THR_COMP_SPLITLA  ] = 25000;
John Koleszar's avatar
John Koleszar committed
1124 1125
            }

1126 1127 1128 1129 1130 1131 1132 1133 1134
            sf->thresh_mult[THR_COMP_ZEROLG   ] = 2000;
            sf->thresh_mult[THR_COMP_NEARESTLG] = 2000;
            sf->thresh_mult[THR_COMP_NEARLG   ] = 2000;
            sf->thresh_mult[THR_COMP_ZEROLA   ] = 2000;
            sf->thresh_mult[THR_COMP_NEARESTLA] = 2000;
            sf->thresh_mult[THR_COMP_NEARLA   ] = 2000;
            sf->thresh_mult[THR_COMP_ZEROGA   ] = 2000;
            sf->thresh_mult[THR_COMP_NEARESTGA] = 2000;
            sf->thresh_mult[THR_COMP_NEARGA   ] = 2000;
1135

1136 1137 1138
            sf->thresh_mult[THR_COMP_NEWLG    ] = 2500;
            sf->thresh_mult[THR_COMP_NEWLA    ] = 2500;
            sf->thresh_mult[THR_COMP_NEWGA    ] = 2500;
1139

1140 1141
            sf->improved_dct = 0;

1142 1143
            // Only do recode loop on key frames, golden frames and
            // alt ref frames
John Koleszar's avatar
John Koleszar committed
1144 1145 1146 1147 1148 1149
            sf->recode_loop = 2;

        }

        break;

1150 1151 1152 1153 1154 1155 1156 1157 1158
    }; /* switch */

    /* disable frame modes if flags not set */
    if (!(cpi->ref_frame_flags & VP8_LAST_FLAG))
    {
        sf->thresh_mult[THR_NEWMV    ] = INT_MAX;
        sf->thresh_mult[THR_NEARESTMV] = INT_MAX;
        sf->thresh_mult[THR_ZEROMV   ] = INT_MAX;
        sf->thresh_mult[THR_NEARMV   ] = INT_MAX;
1159 1160 1161 1162 1163 1164
#if CONFIG_PRED_FILTER
        sf->thresh_mult[THR_NEWMV_FILT    ] = INT_MAX;
        sf->thresh_mult[THR_NEARESTMV_FILT] = INT_MAX;
        sf->thresh_mult[THR_ZEROMV_FILT   ] = INT_MAX;
        sf->thresh_mult[THR_NEARMV_FILT   ] = INT_MAX;
#endif
1165 1166 1167 1168 1169 1170 1171 1172 1173
        sf->thresh_mult[THR_SPLITMV  ] = INT_MAX;
    }

    if (!(cpi->ref_frame_flags & VP8_GOLD_FLAG))
    {
        sf->thresh_mult[THR_NEARESTG ] = INT_MAX;
        sf->thresh_mult[THR_ZEROG    ] = INT_MAX;
        sf->thresh_mult[THR_NEARG    ] = INT_MAX;
        sf->thresh_mult[THR_NEWG     ] = INT_MAX;
1174 1175 1176 1177 1178 1179
#if CONFIG_PRED_FILTER
        sf->thresh_mult[THR_NEARESTG_FILT ] = INT_MAX;
        sf->thresh_mult[THR_ZEROG_FILT    ] = INT_MAX;
        sf->thresh_mult[THR_NEARG_FILT    ] = INT_MAX;
        sf->thresh_mult[THR_NEWG_FILT     ] = INT_MAX;
#endif
Scott LaVarnway's avatar