onyx_if.c 182 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 33
#include "vp8/common/swapyv12buffer.h"
#include "vp8/common/threading.h"
John Koleszar's avatar
John Koleszar committed
34
#include "vpx_ports/vpx_timer.h"
Johann's avatar
Johann committed
35
#include "temporal_filter.h"
36 37 38
#if ARCH_ARM
#include "vpx_ports/arm.h"
#endif
Yunqing Wang's avatar
Yunqing Wang committed
39 40 41
#if CONFIG_MULTI_RES_ENCODING
#include "mr_dissim.h"
#endif
42

John Koleszar's avatar
John Koleszar committed
43 44 45 46 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
#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_estimate_entropy_savings(VP8_COMP *cpi);
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
75
extern void vp8_temporal_filter_prepare_c(VP8_COMP *cpi, int distance);
John Koleszar's avatar
John Koleszar committed
76

77
static void set_default_lf_deltas(VP8_COMP *cpi);
John Koleszar's avatar
John Koleszar committed
78 79 80

extern const int vp8_gf_interval_table[101];

81
#if CONFIG_INTERNAL_STATS
John Koleszar's avatar
John Koleszar committed
82 83 84 85 86 87 88
#include "math.h"

extern double vp8_calc_ssim
(
    YV12_BUFFER_CONFIG *source,
    YV12_BUFFER_CONFIG *dest,
    int lumamask,
89 90
    double *weight,
    const vp8_variance_rtcd_vtable_t *rtcd
John Koleszar's avatar
John Koleszar committed
91 92
);

93

John Koleszar's avatar
John Koleszar committed
94 95 96 97 98 99
extern double vp8_calc_ssimg
(
    YV12_BUFFER_CONFIG *source,
    YV12_BUFFER_CONFIG *dest,
    double *ssim_y,
    double *ssim_u,
100 101
    double *ssim_v,
    const vp8_variance_rtcd_vtable_t *rtcd
John Koleszar's avatar
John Koleszar committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
);


#endif


#ifdef OUTPUT_YUV_SRC
FILE *yuv_file;
#endif

#if 0
FILE *framepsnr;
FILE *kf_list;
FILE *keyfile;
#endif

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


#ifdef ENTROPY_STATS
extern int intra_mode_stats[10][10][10];
#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

#ifdef MODE_STATS
extern unsigned __int64 Sectionbits[50];
extern int y_modes[5]  ;
extern int uv_modes[4] ;
extern int b_modes[10]  ;

extern int inter_y_modes[10] ;
extern int inter_uv_modes[4] ;
extern unsigned int inter_b_modes[15];
#endif

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

extern const int vp8_bits_per_mb[2][QINDEX_RANGE];

extern const int qrounding_factors[129];
extern const int qzbin_factors[129];
extern void vp8cx_init_quantizer(VP8_COMP *cpi);
extern const int vp8cx_base_skip_false_prob[128];

157 158 159
// Tables relating active max Q to active min Q
static const int kf_low_motion_minq[QINDEX_RANGE] =
{
Paul Wilkins's avatar
CQ Mode  
Paul Wilkins committed
160 161 162 163 164 165 166 167
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,1,1,1,1,1,1,1,1,2,2,2,2,
    3,3,3,3,3,3,4,4,4,5,5,5,5,5,6,6,
    6,6,7,7,8,8,8,8,9,9,10,10,10,10,11,11,
    11,11,12,12,13,13,13,13,14,14,15,15,15,15,16,16,
    16,16,17,17,18,18,18,18,19,20,20,21,21,22,23,23
168 169 170
};
static const int kf_high_motion_minq[QINDEX_RANGE] =
{
Paul Wilkins's avatar
CQ Mode  
Paul Wilkins committed
171 172 173 174 175 176 177 178
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    1,1,1,1,1,1,1,1,2,2,2,2,3,3,3,3,
    3,3,3,3,4,4,4,4,5,5,5,5,5,5,6,6,
    6,6,7,7,8,8,8,8,9,9,10,10,10,10,11,11,
    11,11,12,12,13,13,13,13,14,14,15,15,15,15,16,16,
    16,16,17,17,18,18,18,18,19,19,20,20,20,20,21,21,
    21,21,22,22,23,23,24,25,25,26,26,27,28,28,29,30
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
};
static const int gf_low_motion_minq[QINDEX_RANGE] =
{
    0,0,0,0,1,1,1,1,1,1,1,1,2,2,2,2,
    3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,
    7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10,
    11,11,12,12,13,13,14,14,15,15,16,16,17,17,18,18,
    19,19,20,20,21,21,22,22,23,23,24,24,25,25,26,26,
    27,27,28,28,29,29,30,30,31,31,32,32,33,33,34,34,
    35,35,36,36,37,37,38,38,39,39,40,40,41,41,42,42,
    43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58
};
static const int gf_mid_motion_minq[QINDEX_RANGE] =
{
    0,0,0,0,1,1,1,1,1,1,2,2,3,3,3,4,
    4,4,5,5,5,6,6,6,7,7,7,8,8,8,9,9,
    9,10,10,10,10,11,11,11,12,12,12,12,13,13,13,14,
    14,14,15,15,16,16,17,17,18,18,19,19,20,20,21,21,
    22,22,23,23,24,24,25,25,26,26,27,27,28,28,29,29,
    30,30,31,31,32,32,33,33,34,34,35,35,36,36,37,37,
    38,39,39,40,40,41,41,42,42,43,43,44,45,46,47,48,
Paul Wilkins's avatar
CQ Mode  
Paul Wilkins committed
200
    49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64
201 202 203 204 205 206 207 208 209 210
};
static const int gf_high_motion_minq[QINDEX_RANGE] =
{
    0,0,0,0,1,1,1,1,1,2,2,2,3,3,3,4,
    4,4,5,5,5,6,6,6,7,7,7,8,8,8,9,9,
    9,10,10,10,11,11,12,12,13,13,14,14,15,15,16,16,
    17,17,18,18,19,19,20,20,21,21,22,22,23,23,24,24,
    25,25,26,26,27,27,28,28,29,29,30,30,31,31,32,32,
    33,33,34,34,35,35,36,36,37,37,38,38,39,39,40,40,
    41,41,42,42,43,44,45,46,47,48,49,50,51,52,53,54,
Paul Wilkins's avatar
CQ Mode  
Paul Wilkins committed
211
    55,56,57,58,59,60,62,64,66,68,70,72,74,76,78,80
212 213 214
};
static const int inter_minq[QINDEX_RANGE] =
{
Paul Wilkins's avatar
Paul Wilkins committed
215 216 217 218 219 220 221 222
    0,0,1,1,2,3,3,4,4,5,6,6,7,8,8,9,
    9,10,11,11,12,13,13,14,15,15,16,17,17,18,19,20,
    20,21,22,22,23,24,24,25,26,27,27,28,29,30,30,31,
    32,33,33,34,35,36,36,37,38,39,39,40,41,42,42,43,
    44,45,46,46,47,48,49,50,50,51,52,53,54,55,55,56,
    57,58,59,60,60,61,62,63,64,65,66,67,67,68,69,70,
    71,72,73,74,75,75,76,77,78,79,80,81,82,83,84,85,
    86,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100
223
};
John Koleszar's avatar
John Koleszar committed
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242

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();

        init_done = 1;
    }
}
#ifdef PACKET_TESTING
extern FILE *vpxlogc;
#endif

243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
static void save_layer_context(VP8_COMP *cpi)
{
    LAYER_CONTEXT *lc = &cpi->layer_context[cpi->current_layer];

    // Save layer dependent coding state
    lc->target_bandwidth                 = cpi->target_bandwidth;
    //lc->target_bandwidth                 = cpi->oxcf.target_bandwidth;
    lc->starting_buffer_level            = cpi->oxcf.starting_buffer_level;
    lc->optimal_buffer_level             = cpi->oxcf.optimal_buffer_level;
    lc->maximum_buffer_size              = cpi->oxcf.maximum_buffer_size;
    lc->buffer_level                     = cpi->buffer_level;
    lc->bits_off_target                  = cpi->bits_off_target;
    lc->total_actual_bits                = cpi->total_actual_bits;
    lc->worst_quality                    = cpi->worst_quality;
    lc->active_worst_quality             = cpi->active_worst_quality;
    lc->best_quality                     = cpi->best_quality;
    lc->active_best_quality              = cpi->active_best_quality;
    lc->ni_av_qi                         = cpi->ni_av_qi;
    lc->ni_tot_qi                        = cpi->ni_tot_qi;
    lc->ni_frames                        = cpi->ni_frames;
    lc->avg_frame_qindex                 = cpi->avg_frame_qindex;
    lc->rate_correction_factor           = cpi->rate_correction_factor;
    lc->key_frame_rate_correction_factor = cpi->key_frame_rate_correction_factor;
    lc->gf_rate_correction_factor        = cpi->gf_rate_correction_factor;
    lc->zbin_over_quant                  = cpi->zbin_over_quant;
    lc->inter_frame_target               = cpi->inter_frame_target;
    lc->total_byte_count                 = cpi->total_byte_count;
    lc->filter_level                     = cpi->common.filter_level;

    lc->last_frame_percent_intra         = cpi->last_frame_percent_intra;

    memcpy (lc->count_mb_ref_frame_usage,
            cpi->count_mb_ref_frame_usage,
            sizeof(cpi->count_mb_ref_frame_usage));
}

static void restore_layer_context(VP8_COMP *cpi, const int layer)
{
    LAYER_CONTEXT *lc = &cpi->layer_context[layer];

    // Restore layer dependent coding state
    cpi->current_layer                    = layer;
    cpi->target_bandwidth                 = lc->target_bandwidth;
    cpi->oxcf.target_bandwidth            = lc->target_bandwidth;
    cpi->oxcf.starting_buffer_level       = lc->starting_buffer_level;
    cpi->oxcf.optimal_buffer_level        = lc->optimal_buffer_level;
    cpi->oxcf.maximum_buffer_size         = lc->maximum_buffer_size;
    cpi->buffer_level                     = lc->buffer_level;
    cpi->bits_off_target                  = lc->bits_off_target;
    cpi->total_actual_bits                = lc->total_actual_bits;
    //cpi->worst_quality                    = lc->worst_quality;
    cpi->active_worst_quality             = lc->active_worst_quality;
    //cpi->best_quality                     = lc->best_quality;
    cpi->active_best_quality              = lc->active_best_quality;
    cpi->ni_av_qi                         = lc->ni_av_qi;
    cpi->ni_tot_qi                        = lc->ni_tot_qi;
    cpi->ni_frames                        = lc->ni_frames;
    cpi->avg_frame_qindex                 = lc->avg_frame_qindex;
    cpi->rate_correction_factor           = lc->rate_correction_factor;
    cpi->key_frame_rate_correction_factor = lc->key_frame_rate_correction_factor;
    cpi->gf_rate_correction_factor        = lc->gf_rate_correction_factor;
    cpi->zbin_over_quant                  = lc->zbin_over_quant;
    cpi->inter_frame_target               = lc->inter_frame_target;
    cpi->total_byte_count                 = lc->total_byte_count;
    cpi->common.filter_level              = lc->filter_level;

    cpi->last_frame_percent_intra         = lc->last_frame_percent_intra;

    memcpy (cpi->count_mb_ref_frame_usage,
            lc->count_mb_ref_frame_usage,
            sizeof(cpi->count_mb_ref_frame_usage));
}

John Koleszar's avatar
John Koleszar committed
316 317 318 319 320 321 322 323 324 325 326 327 328
static void setup_features(VP8_COMP *cpi)
{
    // Set up default state for MB feature flags
    cpi->mb.e_mbd.segmentation_enabled = 0;
    cpi->mb.e_mbd.update_mb_segmentation_map = 0;
    cpi->mb.e_mbd.update_mb_segmentation_data = 0;
    vpx_memset(cpi->mb.e_mbd.mb_segment_tree_probs, 255, sizeof(cpi->mb.e_mbd.mb_segment_tree_probs));
    vpx_memset(cpi->mb.e_mbd.segment_feature_data, 0, sizeof(cpi->mb.e_mbd.segment_feature_data));

    cpi->mb.e_mbd.mode_ref_lf_delta_enabled = 0;
    cpi->mb.e_mbd.mode_ref_lf_delta_update = 0;
    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));
329 330
    vpx_memset(cpi->mb.e_mbd.last_ref_lf_deltas, 0, sizeof(cpi->mb.e_mbd.ref_lf_deltas));
    vpx_memset(cpi->mb.e_mbd.last_mode_lf_deltas, 0, sizeof(cpi->mb.e_mbd.mode_lf_deltas));
John Koleszar's avatar
John Koleszar committed
331

332
    set_default_lf_deltas(cpi);
John Koleszar's avatar
John Koleszar committed
333 334 335 336

}


337
static void dealloc_compressor_data(VP8_COMP *cpi)
John Koleszar's avatar
John Koleszar committed
338
{
339
    vpx_free(cpi->tplist);
340 341
    cpi->tplist = NULL;

342
    // Delete last frame MV storage buffers
343
    vpx_free(cpi->lfmv);
344 345
    cpi->lfmv = 0;

346
    vpx_free(cpi->lf_ref_frame_sign_bias);
347 348
    cpi->lf_ref_frame_sign_bias = 0;

349
    vpx_free(cpi->lf_ref_frame);
350
    cpi->lf_ref_frame = 0;
John Koleszar's avatar
John Koleszar committed
351 352

    // Delete sementation map
353
    vpx_free(cpi->segmentation_map);
John Koleszar's avatar
John Koleszar committed
354 355
    cpi->segmentation_map = 0;

356
    vpx_free(cpi->active_map);
John Koleszar's avatar
John Koleszar committed
357 358 359 360 361 362 363
    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
364
    vp8_yv12_de_alloc_frame_buffer(&cpi->alt_ref_buffer);
John Koleszar's avatar
John Koleszar committed
365
#endif
John Koleszar's avatar
John Koleszar committed
366
    vp8_lookahead_destroy(cpi->lookahead);
John Koleszar's avatar
John Koleszar committed
367 368 369 370

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

371
    // Structure used to monitor GF usage
372
    vpx_free(cpi->gf_active_flags);
373 374
    cpi->gf_active_flags = 0;

375 376 377 378 379 380
    // 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;

381
    vpx_free(cpi->mb.pip);
382 383
    cpi->mb.pip = 0;

384
#if !(CONFIG_REALTIME_ONLY)
385 386
    vpx_free(cpi->twopass.total_stats);
    cpi->twopass.total_stats = 0;
387

Paul Wilkins's avatar
Paul Wilkins committed
388 389 390
    vpx_free(cpi->twopass.total_left_stats);
    cpi->twopass.total_left_stats = 0;

391 392
    vpx_free(cpi->twopass.this_frame_stats);
    cpi->twopass.this_frame_stats = 0;
393
#endif
John Koleszar's avatar
John Koleszar committed
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 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 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
}

static void enable_segmentation(VP8_PTR ptr)
{
    VP8_COMP *cpi = (VP8_COMP *)(ptr);

    // Set the appropriate feature bit
    cpi->mb.e_mbd.segmentation_enabled = 1;
    cpi->mb.e_mbd.update_mb_segmentation_map = 1;
    cpi->mb.e_mbd.update_mb_segmentation_data = 1;
}
static void disable_segmentation(VP8_PTR ptr)
{
    VP8_COMP *cpi = (VP8_COMP *)(ptr);

    // Clear the appropriate feature bit
    cpi->mb.e_mbd.segmentation_enabled = 0;
}

// Valid values for a segment are 0 to 3
// Segmentation map is arrange as [Rows][Columns]
static void set_segmentation_map(VP8_PTR ptr, unsigned char *segmentation_map)
{
    VP8_COMP *cpi = (VP8_COMP *)(ptr);

    // Copy in the new segmentation map
    vpx_memcpy(cpi->segmentation_map, segmentation_map, (cpi->common.mb_rows * cpi->common.mb_cols));

    // Signal that the map should be updated.
    cpi->mb.e_mbd.update_mb_segmentation_map = 1;
    cpi->mb.e_mbd.update_mb_segmentation_data = 1;
}

// The values given for each segment can be either deltas (from the default value chosen for the frame) or absolute values.
//
// Valid range for abs values is (0-127 for MB_LVL_ALT_Q) , (0-63 for SEGMENT_ALT_LF)
// Valid range for delta values are (+/-127 for MB_LVL_ALT_Q) , (+/-63 for SEGMENT_ALT_LF)
//
// abs_delta = SEGMENT_DELTADATA (deltas) abs_delta = SEGMENT_ABSDATA (use the absolute values given).
//
//
static void set_segment_data(VP8_PTR ptr, signed char *feature_data, unsigned char abs_delta)
{
    VP8_COMP *cpi = (VP8_COMP *)(ptr);

    cpi->mb.e_mbd.mb_segement_abs_delta = abs_delta;
    vpx_memcpy(cpi->segment_feature_data, feature_data, sizeof(cpi->segment_feature_data));
}


static void segmentation_test_function(VP8_PTR ptr)
{
    VP8_COMP *cpi = (VP8_COMP *)(ptr);

    unsigned char *seg_map;
    signed char feature_data[MB_LVL_MAX][MAX_MB_SEGMENTS];

    // Create a temporary map for segmentation data.
    CHECK_MEM_ERROR(seg_map, vpx_calloc(cpi->common.mb_rows * cpi->common.mb_cols, 1));

    // MB loop to set local segmentation map
    /*for ( i = 0; i < cpi->common.mb_rows; i++ )
    {
        for ( j = 0; j < cpi->common.mb_cols; j++ )
        {
            //seg_map[(i*cpi->common.mb_cols) + j] = (j % 2) + ((i%2)* 2);
            //if ( j < cpi->common.mb_cols/2 )

            // Segment 1 around the edge else 0
            if ( (i == 0) || (j == 0) || (i == (cpi->common.mb_rows-1)) || (j == (cpi->common.mb_cols-1)) )
                seg_map[(i*cpi->common.mb_cols) + j] = 1;
            //else if ( (i < 2) || (j < 2) || (i > (cpi->common.mb_rows-3)) || (j > (cpi->common.mb_cols-3)) )
            //  seg_map[(i*cpi->common.mb_cols) + j] = 2;
            //else if ( (i < 5) || (j < 5) || (i > (cpi->common.mb_rows-6)) || (j > (cpi->common.mb_cols-6)) )
            //  seg_map[(i*cpi->common.mb_cols) + j] = 3;
            else
                seg_map[(i*cpi->common.mb_cols) + j] = 0;
        }
    }*/

    // Set the segmentation Map
    set_segmentation_map(ptr, seg_map);

    // Activate segmentation.
    enable_segmentation(ptr);

    // Set up the quant segment data
    feature_data[MB_LVL_ALT_Q][0] = 0;
    feature_data[MB_LVL_ALT_Q][1] = 4;
    feature_data[MB_LVL_ALT_Q][2] = 0;
    feature_data[MB_LVL_ALT_Q][3] = 0;
    // Set up the loop segment data
    feature_data[MB_LVL_ALT_LF][0] = 0;
    feature_data[MB_LVL_ALT_LF][1] = 0;
    feature_data[MB_LVL_ALT_LF][2] = 0;
    feature_data[MB_LVL_ALT_LF][3] = 0;

    // Initialise the feature data structure
    // SEGMENT_DELTADATA    0, SEGMENT_ABSDATA      1
    set_segment_data(ptr, &feature_data[0][0], SEGMENT_DELTADATA);

    // Delete sementation map
        vpx_free(seg_map);

    seg_map = 0;

}

// A simple function to cyclically refresh the background at a lower Q
static void cyclic_background_refresh(VP8_COMP *cpi, int Q, int lf_adjustment)
{
    unsigned char *seg_map;
    signed char feature_data[MB_LVL_MAX][MAX_MB_SEGMENTS];
    int i;
    int block_count = cpi->cyclic_refresh_mode_max_mbs_perframe;
    int mbs_in_frame = cpi->common.mb_rows * cpi->common.mb_cols;

    // Create a temporary map for segmentation data.
    CHECK_MEM_ERROR(seg_map, vpx_calloc(cpi->common.mb_rows * cpi->common.mb_cols, 1));

    cpi->cyclic_refresh_q = Q;

    for (i = Q; i > 0; i--)
    {
        if (vp8_bits_per_mb[cpi->common.frame_type][i] >= ((vp8_bits_per_mb[cpi->common.frame_type][Q]*(Q + 128)) / 64))
            //if ( vp8_bits_per_mb[cpi->common.frame_type][i] >= ((vp8_bits_per_mb[cpi->common.frame_type][Q]*((2*Q)+96))/64) )
        {
            break;
        }
    }

    cpi->cyclic_refresh_q = i;

    // Only update for inter frames
    if (cpi->common.frame_type != KEY_FRAME)
    {
        // Cycle through the macro_block rows
        // MB loop to set local segmentation map
        for (i = cpi->cyclic_refresh_mode_index; i < mbs_in_frame; i++)
        {
            // If the MB is as a candidate for clean up then mark it for possible boost/refresh (segment 1)
            // The segment id may get reset to 0 later if the MB gets coded anything other than last frame 0,0
            // as only (last frame 0,0) MBs are eligable for refresh : that is to say Mbs likely to be background blocks.
            if (cpi->cyclic_refresh_map[i] == 0)
            {
                seg_map[i] = 1;
            }
            else
            {
                seg_map[i] = 0;

                // Skip blocks that have been refreshed recently anyway.
                if (cpi->cyclic_refresh_map[i] < 0)
                    //cpi->cyclic_refresh_map[i] = cpi->cyclic_refresh_map[i] / 16;
                    cpi->cyclic_refresh_map[i]++;
            }


            if (block_count > 0)
                block_count--;
            else
                break;

        }

        // If we have gone through the frame reset to the start
        cpi->cyclic_refresh_mode_index = i;

        if (cpi->cyclic_refresh_mode_index >= mbs_in_frame)
            cpi->cyclic_refresh_mode_index = 0;
    }

    // Set the segmentation Map
    set_segmentation_map((VP8_PTR)cpi, seg_map);

    // Activate segmentation.
    enable_segmentation((VP8_PTR)cpi);

    // Set up the quant segment data
    feature_data[MB_LVL_ALT_Q][0] = 0;
    feature_data[MB_LVL_ALT_Q][1] = (cpi->cyclic_refresh_q - Q);
    feature_data[MB_LVL_ALT_Q][2] = 0;
    feature_data[MB_LVL_ALT_Q][3] = 0;

    // Set up the loop segment data
    feature_data[MB_LVL_ALT_LF][0] = 0;
    feature_data[MB_LVL_ALT_LF][1] = lf_adjustment;
    feature_data[MB_LVL_ALT_LF][2] = 0;
    feature_data[MB_LVL_ALT_LF][3] = 0;

    // Initialise the feature data structure
    // SEGMENT_DELTADATA    0, SEGMENT_ABSDATA      1
    set_segment_data((VP8_PTR)cpi, &feature_data[0][0], SEGMENT_DELTADATA);

    // Delete sementation map
589
    vpx_free(seg_map);
John Koleszar's avatar
John Koleszar committed
590 591 592 593 594

    seg_map = 0;

}

595
static void set_default_lf_deltas(VP8_COMP *cpi)
John Koleszar's avatar
John Koleszar committed
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
{
    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;
622
    int last_improved_quant = sf->improved_quant;
623
    int ref_frames;
John Koleszar's avatar
John Koleszar committed
624 625 626 627 628 629 630 631 632 633 634

    // 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;
    }

    cpi->mbs_tested_so_far = 0;

635
    // best quality defaults
John Koleszar's avatar
John Koleszar committed
636 637 638 639 640 641 642 643 644 645
    sf->RD = 1;
    sf->search_method = NSTEP;
    sf->improved_quant = 1;
    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;
646
    sf->use_fastquant_for_pick = 0;
647
    sf->no_skip_block4x4_search = 1;
John Koleszar's avatar
John Koleszar committed
648 649 650

    sf->first_step = 0;
    sf->max_step_search_steps = MAX_MVSEARCH_STEPS;
651
    sf->improved_mv_pred = 1;
John Koleszar's avatar
John Koleszar committed
652 653 654 655 656

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

657 658 659 660 661 662 663 664 665
    /* Count enabled references */
    ref_frames = 1;
    if (cpi->ref_frame_flags & VP8_LAST_FLAG)
        ref_frames++;
    if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
        ref_frames++;
    if (cpi->ref_frame_flags & VP8_ALT_FLAG)
        ref_frames++;

John Koleszar's avatar
John Koleszar committed
666 667 668 669
    switch (Mode)
    {
#if !(CONFIG_REALTIME_ONLY)
    case 0: // best quality mode
670 671 672 673 674 675 676 677 678
        sf->thresh_mult[THR_ZERO1    ] = 0;
        sf->thresh_mult[THR_ZERO2    ] = 0;
        sf->thresh_mult[THR_ZERO3    ] = 0;
        sf->thresh_mult[THR_NEAREST1 ] = 0;
        sf->thresh_mult[THR_NEAREST2 ] = 0;
        sf->thresh_mult[THR_NEAREST3 ] = 0;
        sf->thresh_mult[THR_NEAR1    ] = 0;
        sf->thresh_mult[THR_NEAR2    ] = 0;
        sf->thresh_mult[THR_NEAR3    ] = 0;
John Koleszar's avatar
John Koleszar committed
679 680 681 682 683 684 685 686

        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_TM       ] = 1000;

687 688 689
        sf->thresh_mult[THR_NEW1     ] = 1000;
        sf->thresh_mult[THR_NEW2     ] = 1000;
        sf->thresh_mult[THR_NEW3     ] = 1000;
John Koleszar's avatar
John Koleszar committed
690

691 692 693
        sf->thresh_mult[THR_SPLIT1   ] = 2500;
        sf->thresh_mult[THR_SPLIT2   ] = 5000;
        sf->thresh_mult[THR_SPLIT3   ] = 5000;
John Koleszar's avatar
John Koleszar committed
694 695 696 697 698 699 700


        sf->first_step = 0;
        sf->max_step_search_steps = MAX_MVSEARCH_STEPS;
        break;
    case 1:
    case 3:
701 702
        sf->thresh_mult[THR_NEAREST1 ] = 0;
        sf->thresh_mult[THR_ZERO1    ] = 0;
John Koleszar's avatar
John Koleszar committed
703
        sf->thresh_mult[THR_DC       ] = 0;
704
        sf->thresh_mult[THR_NEAR1    ] = 0;
John Koleszar's avatar
John Koleszar committed
705 706 707 708 709
        sf->thresh_mult[THR_V_PRED   ] = 1000;
        sf->thresh_mult[THR_H_PRED   ] = 1000;
        sf->thresh_mult[THR_B_PRED   ] = 2500;
        sf->thresh_mult[THR_TM       ] = 1000;

710 711
        sf->thresh_mult[THR_NEAREST2 ] = 1000;
        sf->thresh_mult[THR_NEAREST3 ] = 1000;
John Koleszar's avatar
John Koleszar committed
712

713 714 715 716
        sf->thresh_mult[THR_ZERO2    ] = 1000;
        sf->thresh_mult[THR_ZERO3    ] = 1000;
        sf->thresh_mult[THR_NEAR2    ] = 1000;
        sf->thresh_mult[THR_NEAR3    ] = 1000;
John Koleszar's avatar
John Koleszar committed
717

718
#if 1
719 720 721 722 723 724 725 726 727
        sf->thresh_mult[THR_ZERO1    ] = 0;
        sf->thresh_mult[THR_ZERO2    ] = 0;
        sf->thresh_mult[THR_ZERO3    ] = 0;
        sf->thresh_mult[THR_NEAREST1 ] = 0;
        sf->thresh_mult[THR_NEAREST2 ] = 0;
        sf->thresh_mult[THR_NEAREST3 ] = 0;
        sf->thresh_mult[THR_NEAR1    ] = 0;
        sf->thresh_mult[THR_NEAR2    ] = 0;
        sf->thresh_mult[THR_NEAR3    ] = 0;
728 729 730 731 732 733 734 735

//        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_TM       ] = 1000;

736 737 738
        sf->thresh_mult[THR_NEW1     ] = 1000;
        sf->thresh_mult[THR_NEW2     ] = 1000;
        sf->thresh_mult[THR_NEW3     ] = 1000;
739

740 741 742
        sf->thresh_mult[THR_SPLIT1   ] = 1700;
        sf->thresh_mult[THR_SPLIT2   ] = 4500;
        sf->thresh_mult[THR_SPLIT3   ] = 4500;
743
#else
744 745 746
        sf->thresh_mult[THR_NEW1     ] = 1500;
        sf->thresh_mult[THR_NEW2     ] = 1500;
        sf->thresh_mult[THR_NEW3     ] = 1500;
John Koleszar's avatar
John Koleszar committed
747

748 749 750
        sf->thresh_mult[THR_SPLIT1   ] = 5000;
        sf->thresh_mult[THR_SPLIT2   ] = 10000;
        sf->thresh_mult[THR_SPLIT3   ] = 10000;
751
#endif
John Koleszar's avatar
John Koleszar committed
752

753
        if (Speed > 0)
John Koleszar's avatar
John Koleszar committed
754
        {
755 756 757 758
            /* Disable coefficient optimization above speed 0 */
            sf->optimize_coefficients = 0;
            sf->use_fastquant_for_pick = 1;
            sf->no_skip_block4x4_search = 0;
759

760
            sf->first_step = 1;
761

762 763 764
            cpi->mode_check_freq[THR_SPLIT2] = 2;
            cpi->mode_check_freq[THR_SPLIT3] = 2;
            cpi->mode_check_freq[THR_SPLIT1 ] = 0;
John Koleszar's avatar
John Koleszar committed
765 766
        }

767
        if (Speed > 1)
John Koleszar's avatar
John Koleszar committed
768
        {
769 770 771
            cpi->mode_check_freq[THR_SPLIT2] = 4;
            cpi->mode_check_freq[THR_SPLIT3] = 4;
            cpi->mode_check_freq[THR_SPLIT1 ] = 2;
John Koleszar's avatar
John Koleszar committed
772 773 774 775 776 777

            sf->thresh_mult[THR_TM       ] = 1500;
            sf->thresh_mult[THR_V_PRED   ] = 1500;
            sf->thresh_mult[THR_H_PRED   ] = 1500;
            sf->thresh_mult[THR_B_PRED   ] = 5000;

778
            if (ref_frames > 1)
John Koleszar's avatar
John Koleszar committed
779
            {
780 781
                sf->thresh_mult[THR_NEW1     ] = 2000;
                sf->thresh_mult[THR_SPLIT1   ] = 10000;
John Koleszar's avatar
John Koleszar committed
782 783
            }

784
            if (ref_frames > 2)
John Koleszar's avatar
John Koleszar committed
785
            {
786 787 788 789 790
                sf->thresh_mult[THR_NEAREST2 ] = 1500;
                sf->thresh_mult[THR_ZERO2    ] = 1500;
                sf->thresh_mult[THR_NEAR2    ] = 1500;
                sf->thresh_mult[THR_NEW2     ] = 2000;
                sf->thresh_mult[THR_SPLIT2   ] = 20000;
John Koleszar's avatar
John Koleszar committed
791 792
            }

793
            if (ref_frames > 3)
John Koleszar's avatar
John Koleszar committed
794
            {
795 796 797 798 799
                sf->thresh_mult[THR_NEAREST3 ] = 1500;
                sf->thresh_mult[THR_ZERO3    ] = 1500;
                sf->thresh_mult[THR_NEAR3    ] = 1500;
                sf->thresh_mult[THR_NEW3     ] = 2000;
                sf->thresh_mult[THR_SPLIT3   ] = 20000;
John Koleszar's avatar
John Koleszar committed
800 801 802
            }
        }

803
        if (Speed > 2)
John Koleszar's avatar
John Koleszar committed
804
        {
805 806 807
            cpi->mode_check_freq[THR_SPLIT2] = 15;
            cpi->mode_check_freq[THR_SPLIT3] = 15;
            cpi->mode_check_freq[THR_SPLIT1 ] = 7;
John Koleszar's avatar
John Koleszar committed
808 809 810 811 812 813

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

814
            if (ref_frames > 1)
John Koleszar's avatar
John Koleszar committed
815
            {
816 817
                sf->thresh_mult[THR_NEW1     ] = 2000;
                sf->thresh_mult[THR_SPLIT1   ] = 25000;
John Koleszar's avatar
John Koleszar committed
818 819
            }

820
            if (ref_frames > 2)
John Koleszar's avatar
John Koleszar committed
821
            {
822 823 824 825 826
                sf->thresh_mult[THR_NEAREST2 ] = 2000;
                sf->thresh_mult[THR_ZERO2    ] = 2000;
                sf->thresh_mult[THR_NEAR2    ] = 2000;
                sf->thresh_mult[THR_NEW2     ] = 2500;
                sf->thresh_mult[THR_SPLIT2   ] = 50000;
John Koleszar's avatar
John Koleszar committed
827 828
            }

829
            if (ref_frames > 3)
John Koleszar's avatar
John Koleszar committed
830
            {
831 832 833 834 835
                sf->thresh_mult[THR_NEAREST3 ] = 2000;
                sf->thresh_mult[THR_ZERO3    ] = 2000;
                sf->thresh_mult[THR_NEAR3    ] = 2000;
                sf->thresh_mult[THR_NEW3     ] = 2500;
                sf->thresh_mult[THR_SPLIT3   ] = 50000;
John Koleszar's avatar
John Koleszar committed
836 837
            }

838 839 840
            sf->improved_quant = 0;
            sf->improved_dct = 0;

841 842
            // Only do recode loop on key frames, golden frames and
            // alt ref frames
John Koleszar's avatar
John Koleszar committed
843 844 845 846
            sf->recode_loop = 2;

        }

847
        if (Speed > 3)
John Koleszar's avatar
John Koleszar committed
848
        {
849 850 851
            sf->thresh_mult[THR_SPLIT3  ] = INT_MAX;
            sf->thresh_mult[THR_SPLIT2  ] = INT_MAX;
            sf->thresh_mult[THR_SPLIT1   ] = INT_MAX;
John Koleszar's avatar
John Koleszar committed
852 853 854 855

            cpi->mode_check_freq[THR_V_PRED] = 0;
            cpi->mode_check_freq[THR_H_PRED] = 0;
            cpi->mode_check_freq[THR_B_PRED] = 0;
856 857 858 859
            cpi->mode_check_freq[THR_NEAR2] = 0;
            cpi->mode_check_freq[THR_NEW2] = 0;
            cpi->mode_check_freq[THR_NEAR3] = 0;
            cpi->mode_check_freq[THR_NEW3] = 0;
John Koleszar's avatar
John Koleszar committed
860 861 862 863

            sf->auto_filter = 1;
            sf->recode_loop = 0; // recode loop off
            sf->RD = 0;         // Turn rd off
864

John Koleszar's avatar
John Koleszar committed
865 866 867 868 869 870 871 872 873 874
        }

        if (Speed > 4)
        {
            sf->auto_filter = 0;                     // Faster selection of loop filter

            cpi->mode_check_freq[THR_V_PRED] = 2;
            cpi->mode_check_freq[THR_H_PRED] = 2;
            cpi->mode_check_freq[THR_B_PRED] = 2;

875
            if (ref_frames > 2)
John Koleszar's avatar
John Koleszar committed
876
            {
877 878
                cpi->mode_check_freq[THR_NEAR2] = 2;
                cpi->mode_check_freq[THR_NEW2] = 4;
John Koleszar's avatar
John Koleszar committed
879 880
            }

881
            if (ref_frames > 3)
John Koleszar's avatar
John Koleszar committed
882
            {
883 884
                cpi->mode_check_freq[THR_NEAR3] = 2;
                cpi->mode_check_freq[THR_NEW3] = 4;
John Koleszar's avatar
John Koleszar committed
885 886
            }

887
            if (ref_frames > 2)
John Koleszar's avatar
John Koleszar committed
888
            {
889 890 891 892
                sf->thresh_mult[THR_NEAREST2 ] = 2000;
                sf->thresh_mult[THR_ZERO2    ] = 2000;
                sf->thresh_mult[THR_NEAR2    ] = 2000;
                sf->thresh_mult[THR_NEW2     ] = 4000;
John Koleszar's avatar
John Koleszar committed
893 894
            }

895
            if (ref_frames > 3)
John Koleszar's avatar
John Koleszar committed
896
            {
897 898 899 900
                sf->thresh_mult[THR_NEAREST3 ] = 2000;
                sf->thresh_mult[THR_ZERO3    ] = 2000;
                sf->thresh_mult[THR_NEAR3    ] = 2000;
                sf->thresh_mult[THR_NEW3     ] = 4000;
John Koleszar's avatar
John Koleszar committed
901 902 903 904 905 906 907 908 909 910
            }
        }

        break;
#endif
    case 2:
        sf->optimize_coefficients = 0;
        sf->recode_loop = 0;
        sf->auto_filter = 1;
        sf->iterative_sub_pixel = 1;
911 912
        sf->thresh_mult[THR_NEAREST1 ] = 0;
        sf->thresh_mult[THR_ZERO1    ] = 0;
John Koleszar's avatar
John Koleszar committed
913 914
        sf->thresh_mult[THR_DC       ] = 0;
        sf->thresh_mult[THR_TM       ] = 0;
915
        sf->thresh_mult[THR_NEAR1    ] = 0;
John Koleszar's avatar
John Koleszar committed
916 917 918
        sf->thresh_mult[THR_V_PRED   ] = 1000;
        sf->thresh_mult[THR_H_PRED   ] = 1000;
        sf->thresh_mult[THR_B_PRED   ] = 2500;
919 920 921 922 923 924 925 926 927 928 929 930
        sf->thresh_mult[THR_NEAREST2 ] = 1000;
        sf->thresh_mult[THR_ZERO2    ] = 1000;
        sf->thresh_mult[THR_NEAR2    ] = 1000;
        sf->thresh_mult[THR_NEAREST3 ] = 1000;
        sf->thresh_mult[THR_ZERO3    ] = 1000;
        sf->thresh_mult[THR_NEAR3    ] = 1000;
        sf->thresh_mult[THR_NEW1     ] = 2000;
        sf->thresh_mult[THR_NEW2     ] = 2000;
        sf->thresh_mult[THR_NEW3     ] = 2000;
        sf->thresh_mult[THR_SPLIT1   ] = 5000;
        sf->thresh_mult[THR_SPLIT2   ] = 10000;
        sf->thresh_mult[THR_SPLIT3   ] = 10000;
John Koleszar's avatar
John Koleszar committed
931 932 933 934
        sf->search_method = NSTEP;

        if (Speed > 0)
        {
935 936 937
            cpi->mode_check_freq[THR_SPLIT2] = 4;
            cpi->mode_check_freq[THR_SPLIT3] = 4;
            cpi->mode_check_freq[THR_SPLIT1 ] = 2;
John Koleszar's avatar
John Koleszar committed
938 939 940 941 942 943 944

            sf->thresh_mult[THR_DC       ] = 0;
            sf->thresh_mult[THR_TM       ] = 1000;
            sf->thresh_mult[THR_V_PRED   ] = 2000;
            sf->thresh_mult[THR_H_PRED   ] = 2000;
            sf->thresh_mult[THR_B_PRED   ] = 5000;

945
            if (ref_frames > 1)
John Koleszar's avatar
John Koleszar committed
946
            {
947 948 949 950 951
                sf->thresh_mult[THR_NEAREST1 ] = 0;
                sf->thresh_mult[THR_ZERO1    ] = 0;
                sf->thresh_mult[THR_NEAR1    ] = 0;
                sf->thresh_mult[THR_NEW1     ] = 2000;
                sf->thresh_mult[THR_SPLIT1   ] = 10000;
John Koleszar's avatar
John Koleszar committed
952 953
            }

954
            if (ref_frames > 2)
John Koleszar's avatar
John Koleszar committed
955
            {
956 957 958 959 960
                sf->thresh_mult[THR_NEAREST2 ] = 1000;
                sf->thresh_mult[THR_ZERO2    ] = 1000;
                sf->thresh_mult[THR_NEAR2    ] = 1000;
                sf->thresh_mult[THR_NEW2     ] = 2000;
                sf->thresh_mult[THR_SPLIT2   ] = 20000;
John Koleszar's avatar
John Koleszar committed
961 962
            }

963
            if (ref_frames > 3)
John Koleszar's avatar
John Koleszar committed
964
            {
965 966 967 968 969
                sf->thresh_mult[THR_NEAREST3 ] = 1000;
                sf->thresh_mult[THR_ZERO3    ] = 1000;
                sf->thresh_mult[THR_NEAR3    ] = 1000;
                sf->thresh_mult[THR_NEW3     ] = 2000;
                sf->thresh_mult[THR_SPLIT3   ] = 20000;
John Koleszar's avatar
John Koleszar committed
970 971 972 973
            }

            sf->improved_quant = 0;
            sf->improved_dct = 0;
974 975 976 977

            sf->use_fastquant_for_pick = 1;
            sf->no_skip_block4x4_search = 0;
            sf->first_step = 1;
John Koleszar's avatar
John Koleszar committed
978 979 980 981
        }

        if (Speed > 1)
        {
982 983 984
            cpi->mode_check_freq[THR_SPLIT1 ] = 7;
            cpi->mode_check_freq[THR_SPLIT2] = 15;
            cpi->mode_check_freq[THR_SPLIT3] = 15;
John Koleszar's avatar
John Koleszar committed
985 986 987 988 989 990

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

991
            if (ref_frames > 1)
John Koleszar's avatar
John Koleszar committed
992
            {
993 994
                sf->thresh_mult[THR_NEW1     ] = 2000;
                sf->thresh_mult[THR_SPLIT1   ] = 25000;
John Koleszar's avatar
John Koleszar committed
995 996
            }

997
            if (ref_frames > 2)
John Koleszar's avatar
John Koleszar committed
998
            {
999 1000 1001 1002 1003
                sf->thresh_mult[THR_NEAREST2 ] = 2000;
                sf->thresh_mult[THR_ZERO2    ] = 2000;
                sf->thresh_mult[THR_NEAR2    ] = 2000;
                sf->thresh_mult[THR_NEW2     ] = 2500;
                sf->thresh_mult[THR_SPLIT2   ] = 50000;
John Koleszar's avatar
John Koleszar committed
1004 1005
            }

1006
            if (ref_frames > 3)
John Koleszar's avatar
John Koleszar committed
1007
            {
1008 1009 1010 1011 1012
                sf->thresh_mult[THR_NEAREST3 ] = 2000;
                sf->thresh_mult[THR_ZERO3    ] = 2000;
                sf->thresh_mult[THR_NEAR3    ] = 2000;
                sf->thresh_mult[THR_NEW3     ] = 2500;
                sf->thresh_mult[THR_SPLIT3   ] = 50000;
John Koleszar's avatar
John Koleszar committed
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
            }

        }

        if (Speed > 2)
        {
            sf->auto_filter = 0;                     // Faster selection of loop filter

            cpi->mode_check_freq[THR_V_PRED] = 2;
            cpi->mode_check_freq[THR_H_PRED] = 2;
            cpi->mode_check_freq[THR_B_PRED] = 2;

1025
            if (ref_frames > 2)
John Koleszar's avatar
John Koleszar committed
1026
            {
1027 1028
                cpi->mode_check_freq[THR_NEAR2] = 2;
                cpi->mode_check_freq[THR_NEW2] = 4;
John Koleszar's avatar
John Koleszar committed
1029 1030
            }

1031
            if (ref_frames > 3)
John Koleszar's avatar
John Koleszar committed
1032
            {
1033 1034
                cpi->mode_check_freq[THR_NEAR3] = 2;
                cpi->mode_check_freq[THR_NEW3] = 4;
John Koleszar's avatar
John Koleszar committed
1035 1036
            }

1037 1038 1039
            sf->thresh_mult[THR_SPLIT1   ] = INT_MAX;
            sf->thresh_mult[THR_SPLIT2  ] = INT_MAX;
            sf->thresh_mult[THR_SPLIT3  ] = INT_MAX;
John Koleszar's avatar
John Koleszar committed
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054

        }

        if (Speed > 3)
        {
            sf->RD = 0;

            sf->auto_filter = 1;
        }

        if (Speed > 4)
        {
            sf->auto_filter = 0;                     // Faster selection of loop filter

            sf->search_method = HEX;
1055 1056
            //sf->search_method = DIAMOND;

1057
            sf->iterative_sub_pixel = 0;
John Koleszar's avatar
John Koleszar committed
1058 1059 1060 1061 1062 1063 1064

            cpi->mode_check_freq[THR_V_PRED] = 4;
            cpi->mode_check_freq[THR_H_PRED] = 4;
            cpi->mode_check_freq[THR_B_PRED] = 4;

            if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
            {
1065 1066
                cpi->mode_check_freq[THR_NEAR2] = 2;
                cpi->mode_check_freq[THR_NEW2] = 4;
John Koleszar's avatar
John Koleszar committed
1067 1068 1069 1070
            }

            if (cpi->ref_frame_flags & VP8_ALT_FLAG)
            {
1071 1072
                cpi->mode_check_freq[THR_NEAR3] = 2;
                cpi->mode_check_freq[THR_NEW3] = 4;
John Koleszar's avatar
John Koleszar committed
1073 1074 1075 1076 1077 1078 1079
            }

            sf->thresh_mult[THR_TM       ] = 2000;
            sf->thresh_mult[THR_B_PRED   ] = 5000;

            if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
            {
1080 1081 1082 1083
                sf->thresh_mult[THR_NEAREST2 ] = 2000;
                sf->thresh_mult[THR_ZERO2    ] = 2000;
                sf->thresh_mult[THR_NEAR2    ] = 2000;
                sf->thresh_mult[THR_NEW2     ] = 4000;
John Koleszar's avatar
John Koleszar committed
1084 1085 1086 1087
            }

            if (cpi->ref_frame_flags & VP8_ALT_FLAG)
            {
1088 1089 1090 1091
                sf->thresh_mult[THR_NEAREST3 ] = 2000;
                sf->thresh_mult[THR_ZERO3    ] = 2000;
                sf->thresh_mult[THR_NEAR3    ] = 2000;
                sf->thresh_mult[THR_NEW3     ] = 4000;
John Koleszar's avatar
John Koleszar committed
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137
            }
        }

        if (Speed > 5)
        {
            // Disable split MB intra prediction mode
            sf->thresh_mult[THR_B_PRED] = INT_MAX;
        }

        if (Speed > 6)
        {
            unsigned int i, sum = 0;
            unsigned int total_mbs = cm->MBs;
            int thresh;
            int total_skip;

            int min = 2000;

            if (cpi->oxcf.encode_breakout > 2000)
                min = cpi->oxcf.encode_breakout;

            min >>= 7;

            for (i = 0; i < min; i++)
            {
                sum += cpi->error_bins[i];
            }

            total_skip = sum;
            sum = 0;

            // i starts from 2 to make sure thresh started from 2048
            for (; i < 1024; i++)
            {
                sum += cpi->error_bins[i];

                if (10 * sum >= (unsigned int)(cpi->Speed - 6)*(total_mbs - total_skip))
                    break;
            }

            i--;
            thresh = (i << 7);

            if (thresh < 2000)
                thresh = 2000;

1138
            if (ref_frames > 1)
John Koleszar's avatar
John Koleszar committed
1139
            {
1140 1141 1142
                sf->thresh_mult[THR_NEW1 ] = thresh;
                sf->thresh_mult[THR_NEAREST1  ] = thresh >> 1;
                sf->thresh_mult[THR_NEAR1     ] = thresh >> 1;
John Koleszar's avatar
John Koleszar committed
1143 1144
            }

1145
            if (ref_frames > 2)
John Koleszar's avatar
John Koleszar committed
1146
            {
1147 1148 1149
                sf->thresh_mult[THR_NEW2] = thresh << 1;
                sf->thresh_mult[THR_NEAREST2 ] = thresh;
                sf->thresh_mult[THR_NEAR2    ] = thresh;
John Koleszar's avatar
John Koleszar committed
1150 1151
            }

1152
            if (ref_frames > 3)
John Koleszar's avatar
John Koleszar committed
1153
            {
1154 1155 1156
                sf->thresh_mult[THR_NEW3] = thresh << 1;
                sf->thresh_mult[THR_NEAREST3 ] = thresh;
                sf->thresh_mult[THR_NEAR3    ] = thresh;
John Koleszar's avatar
John Koleszar committed
1157 1158 1159 1160 1161 1162 1163
            }

            // Disable other intra prediction modes
            sf->thresh_mult[THR_TM] = INT_MAX;
            sf->thresh_mult[THR_V_PRED] = INT_MAX;
            sf->thresh_mult[THR_H_PRED] = INT_MAX;

1164
            sf->improved_mv_pred = 0;
John Koleszar's avatar
John Koleszar committed
1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180
        }

        if (Speed > 8)
        {
            sf->quarter_pixel_search = 0;
        }

        if (Speed > 9)
        {
            int Tmp = cpi->Speed - 8;

            if (Tmp > 4)
                Tmp = 4;

            if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
            {
1181 1182 1183 1184
                cpi->mode_check_freq[THR_ZERO2] = 1 << (Tmp - 1);
                cpi->mode_check_freq[THR_NEAREST2] = 1 << (Tmp - 1);
                cpi->mode_check_freq[THR_NEAR2] = 1 << Tmp;
                cpi->mode_check_freq[THR_NEW2] = 1 << (Tmp + 1);
John Koleszar's avatar
John Koleszar committed
1185 1186 1187 1188
            }

            if (cpi->ref_frame_flags & VP8_ALT_FLAG)
            {
1189 1190 1191 1192
                cpi->mode_check_freq[THR_ZERO3] = 1 << (Tmp - 1);
                cpi->mode_check_freq[THR_NEAREST3] = 1 << (Tmp - 1);
                cpi->mode_check_freq[THR_NEAR3] = 1 << Tmp;
                cpi->mode_check_freq[THR_NEW3] = 1 << (Tmp + 1);
John Koleszar's avatar
John Koleszar committed
1193 1194
            }

1195
            cpi->mode_check_freq[THR_NEW1 ] = 1 << (Tmp - 1);
John Koleszar's avatar
John Koleszar committed
1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209
        }

        cm->filter_type = NORMAL_LOOPFILTER;

        if (Speed >= 14)
            cm->filter_type = SIMPLE_LOOPFILTER;

        if (Speed >= 15)
        {
            sf->half_pixel_search = 0;        // This has a big hit on quality. Last resort
        }

        vpx_memset(cpi->error_bins, 0, sizeof(cpi->error_bins));

1210 1211
    }; /* switch */

1212 1213 1214 1215 1216 1217 1218 1219 1220
    // Slow quant, dct and trellis not worthwhile for first pass
    // so make sure they are always turned off.
    if ( cpi->pass == 1 )
    {
        sf->improved_quant = 0;
        sf->optimize_coefficients = 0;
        sf->improved_dct = 0;
    }

John Koleszar's avatar
John Koleszar committed
1221 1222
    if (cpi->sf.search_method == NSTEP)
    {
1223
        vp8_init3smotion_compensation(&cpi->mb, cm->yv12_fb[cm->lst_fb_idx].y_stride);
John Koleszar's avatar
John Koleszar committed
1224 1225 1226
    }
    else if (cpi->sf.search_method == DIAMOND)
    {
1227
        vp8_init_dsmotion_compensation(&cpi->mb, cm->yv12_fb[cm->lst_fb_idx].y_stride);
John Koleszar's avatar
John Koleszar committed
1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244
    }

    if (cpi->sf.improved_dct)
    {
        cpi->mb.vp8_short_fdct8x4 = FDCT_INVOKE(&cpi->rtcd.fdct, short8x4);
        cpi->mb.vp8_short_fdct4x4 = FDCT_INVOKE(&cpi->rtcd.fdct, short4x4);
    }
    else
    {
        cpi->mb.vp8_short_fdct8x4   = FDCT_INVOKE(&cpi->rtcd.fdct, fast8x4);
        cpi->mb.vp8_short_fdct4x4   = FDCT_INVOKE(&cpi->rtcd.fdct, fast4x4);
    }

    cpi->mb.short_walsh4x4 = FDCT_INVOKE(&cpi->rtcd.fdct, walsh_short4x4);

    if (cpi->sf.improved_quant)
    {
1245 1246 1247 1248
        cpi->mb.quantize_b      = QUANTIZE_INVOKE(&cpi->rtcd.quantize,
                                                  quantb);
        cpi->mb.quantize_b_pair = QUANTIZE_INVOKE(&cpi->rtcd.quantize,
                                                  quantb_pair);
John Koleszar's avatar
John Koleszar committed
1249 1250 1251
    }
    else
    {
1252 1253 1254 1255
        cpi->mb.quantize_b      = QUANTIZE_INVOKE(&cpi->rtcd.quantize,
                                                  fastquantb);
        cpi->mb.quantize_b_pair = QUANTIZE_INVOKE(&cpi->rtcd.quantize,
                                                  fastquantb_pair);
John Koleszar's avatar
John Koleszar committed
1256
    }
1257 1258
    if (cpi->sf.improved_quant != last_improved_quant)
        vp8cx_init_quantizer(cpi);
John Koleszar's avatar
John Koleszar committed
1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280

#if CONFIG_RUNTIME_CPU_DETECT
    cpi->mb.e_mbd.rtcd = &cpi->common.rtcd;
#endif

    if (cpi->sf.iterative_sub_pixel == 1)
    {
        cpi->find_fractional_mv_step = vp8_find_best_sub_pixel_step_iteratively;
    }
    else if (cpi->sf.quarter_pixel_search)
    {
        cpi->find_fractional_mv_step = vp8_find_best_sub_pixel_step;
    }
    else if (cpi->sf.half_pixel_search)
    {
        cpi->find_fractional_mv_step = vp8_find_best_half_pixel_step;
    }
    else
    {
        cpi->find_fractional_mv_step = vp8_skip_fractional_mv_step;
    }

1281
    if (cpi->sf.optimize_coefficients == 1 && cpi->pass!=1)
Yaowu Xu's avatar
Yaowu Xu committed
1282
        cpi->mb.optimize = 1;
John Koleszar's avatar
John Koleszar committed
1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
    else
        cpi->mb.optimize = 0;

    if (cpi->common.full_pixel)
        cpi->find_fractional_mv_step = vp8_skip_fractional_mv_step;

#ifdef SPEEDSTATS
    frames_at_speed[cpi->Speed]++;
#endif
}
static void alloc_raw_frame_buffers(VP8_COMP *cpi)
{
1295
    int width = (cpi->oxcf.Width + 15) & ~15;
John Koleszar's avatar
John Koleszar committed
1296
    int height = (cpi->oxcf.Height + 15) & ~15;
John Koleszar's avatar
John Koleszar committed
1297

John Koleszar's avatar
John Koleszar committed
1298 1299 1300 1301 1302
    cpi->lookahead = vp8_lookahead_init(cpi->oxcf.Width, cpi->oxcf.Height,
                                        cpi->oxcf.lag_in_frames);
    if(!cpi->lookahead)
        vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
                           "Failed to allocate lag buffers");
John Koleszar's avatar
John Koleszar committed
1303 1304 1305

#if VP8_TEMPORAL_ALT_REF

John Koleszar's avatar
John Koleszar committed
1306
    if (vp8_yv12_alloc_frame_buffer(&cpi->alt_ref_buffer,
1307
                                    width, height, VP8BORDERINPIXELS))
John Koleszar's avatar
John Koleszar committed
1308 1309 1310 1311 1312
        vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
                           "Failed to allocate altref buffer");

#endif
}
1313 1314 1315

static int vp8_alloc_partition_data(VP8_COMP *cpi)
{
1316 1317
        vpx_free(cpi->mb.pip);

1318 1319 1320 1321
    cpi->mb.pip = vpx_calloc((cpi->common.mb_cols + 1) *
                                (cpi->common.mb_rows + 1),
                                sizeof(PARTITION_INFO));
    if(!cpi->mb.pip)
John Koleszar's avatar
John Koleszar committed
1322
        return 1;
1323 1324 1325 1326 1327 1328

    cpi->mb.pi = cpi->mb.pip + cpi->common.mode_info_stride + 1;

    return 0;
}

John Koleszar's avatar
John Koleszar committed