onyx_if.c 198 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"
13
#include "./vpx_scale_rtcd.h"
Johann's avatar
Johann committed
14
#include "./vp8_rtcd.h"
15
#include "vp8/common/onyxc_int.h"
16
#include "vp8/common/blockd.h"
John Koleszar's avatar
John Koleszar committed
17
#include "onyx_int.h"
18
#include "vp8/common/systemdependent.h"
John Koleszar's avatar
John Koleszar committed
19
#include "quantize.h"
20
#include "vp8/common/alloccommon.h"
John Koleszar's avatar
John Koleszar committed
21 22
#include "mcomp.h"
#include "firstpass.h"
23
#include "vpx/internal/vpx_psnr.h"
Johann's avatar
Johann committed
24
#include "vpx_scale/vpx_scale.h"
25
#include "vp8/common/extend.h"
John Koleszar's avatar
John Koleszar committed
26
#include "ratectrl.h"
27
#include "vp8/common/quant_common.h"
28
#include "segmentation.h"
29
#if CONFIG_POSTPROC
30
#include "vp8/common/postproc.h"
31
#endif
John Koleszar's avatar
John Koleszar committed
32
#include "vpx_mem/vpx_mem.h"
33 34
#include "vp8/common/swapyv12buffer.h"
#include "vp8/common/threading.h"
John Koleszar's avatar
John Koleszar committed
35
#include "vpx_ports/vpx_timer.h"
36 37 38
#if ARCH_ARM
#include "vpx_ports/arm.h"
#endif
39 40 41
#if CONFIG_MULTI_RES_ENCODING
#include "mr_dissim.h"
#endif
42
#include "encodeframe.h"
43

John Koleszar's avatar
John Koleszar committed
44 45 46 47
#include <math.h>
#include <stdio.h>
#include <limits.h>

48 49 50 51 52
#if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
extern int vp8_update_coef_context(VP8_COMP *cpi);
extern void vp8_update_coef_probs(VP8_COMP *cpi);
#endif

John Koleszar's avatar
John Koleszar committed
53 54 55 56 57 58 59 60
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_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();
61
extern int vp8cx_create_encoder_threads(VP8_COMP *cpi);
John Koleszar's avatar
John Koleszar committed
62 63 64
extern void vp8cx_remove_encoder_threads(VP8_COMP *cpi);

int vp8_estimate_entropy_savings(VP8_COMP *cpi);
65

66
int vp8_calc_ss_err(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *dest);
John Koleszar's avatar
John Koleszar committed
67

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

70
static void set_default_lf_deltas(VP8_COMP *cpi);
John Koleszar's avatar
John Koleszar committed
71 72 73

extern const int vp8_gf_interval_table[101];

74
#if CONFIG_INTERNAL_STATS
John Koleszar's avatar
John Koleszar committed
75 76 77 78 79 80 81
#include "math.h"

extern double vp8_calc_ssim
(
    YV12_BUFFER_CONFIG *source,
    YV12_BUFFER_CONFIG *dest,
    int lumamask,
82
    double *weight
John Koleszar's avatar
John Koleszar committed
83 84
);

85

John Koleszar's avatar
John Koleszar committed
86 87 88 89 90 91
extern double vp8_calc_ssimg
(
    YV12_BUFFER_CONFIG *source,
    YV12_BUFFER_CONFIG *dest,
    double *ssim_y,
    double *ssim_u,
92
    double *ssim_v
John Koleszar's avatar
John Koleszar committed
93 94 95 96 97 98 99 100 101
);


#endif


#ifdef OUTPUT_YUV_SRC
FILE *yuv_file;
#endif
102 103 104
#ifdef OUTPUT_YUV_DENOISED
FILE *yuv_denoised_file;
#endif
John Koleszar's avatar
John Koleszar committed
105 106 107 108 109 110 111 112 113 114 115 116 117

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

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


118
#ifdef VP8_ENTROPY_STATS
John Koleszar's avatar
John Koleszar committed
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
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 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];

148
/* Tables relating active max Q to active min Q */
149
static const unsigned char kf_low_motion_minq[QINDEX_RANGE] =
150
{
Paul Wilkins's avatar
Paul Wilkins committed
151 152 153 154 155 156 157 158
    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
159
};
160
static const unsigned char kf_high_motion_minq[QINDEX_RANGE] =
161
{
Paul Wilkins's avatar
Paul Wilkins committed
162 163 164 165 166 167 168 169
    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
170
};
171
static const unsigned char gf_low_motion_minq[QINDEX_RANGE] =
172 173 174 175 176 177 178 179 180 181
{
    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
};
182
static const unsigned char gf_mid_motion_minq[QINDEX_RANGE] =
183 184 185 186 187 188 189 190
{
    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
Paul Wilkins committed
191
    49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64
192
};
193
static const unsigned char gf_high_motion_minq[QINDEX_RANGE] =
194 195 196 197 198 199 200 201
{
    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
Paul Wilkins committed
202
    55,56,57,58,59,60,62,64,66,68,70,72,74,76,78,80
203
};
204
static const unsigned char inter_minq[QINDEX_RANGE] =
205
{
206 207 208 209 210 211 212 213
    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
214
};
John Koleszar's avatar
John Koleszar committed
215 216 217 218 219

#ifdef PACKET_TESTING
extern FILE *vpxlogc;
#endif

220 221 222 223
static void save_layer_context(VP8_COMP *cpi)
{
    LAYER_CONTEXT *lc = &cpi->layer_context[cpi->current_layer];

224
    /* Save layer dependent coding state */
225 226 227 228
    lc->target_bandwidth                 = cpi->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;
229 230 231
    lc->starting_buffer_level_in_ms      = cpi->oxcf.starting_buffer_level_in_ms;
    lc->optimal_buffer_level_in_ms       = cpi->oxcf.optimal_buffer_level_in_ms;
    lc->maximum_buffer_size_in_ms        = cpi->oxcf.maximum_buffer_size_in_ms;
232 233 234 235 236 237 238 239 240 241 242 243 244 245
    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;
246
    lc->zbin_over_quant                  = cpi->mb.zbin_over_quant;
247 248 249 250 251 252 253
    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,
254 255
            cpi->mb.count_mb_ref_frame_usage,
            sizeof(cpi->mb.count_mb_ref_frame_usage));
256 257 258 259 260 261
}

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

262
    /* Restore layer dependent coding state */
263 264 265 266 267 268
    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;
269 270 271
    cpi->oxcf.starting_buffer_level_in_ms = lc->starting_buffer_level_in_ms;
    cpi->oxcf.optimal_buffer_level_in_ms  = lc->optimal_buffer_level_in_ms;
    cpi->oxcf.maximum_buffer_size_in_ms   = lc->maximum_buffer_size_in_ms;
272 273 274 275 276 277 278 279 280 281 282 283
    cpi->buffer_level                     = lc->buffer_level;
    cpi->bits_off_target                  = lc->bits_off_target;
    cpi->total_actual_bits                = lc->total_actual_bits;
    cpi->active_worst_quality             = lc->active_worst_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;
284
    cpi->mb.zbin_over_quant                  = lc->zbin_over_quant;
285 286 287 288 289 290
    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;

291
    memcpy (cpi->mb.count_mb_ref_frame_usage,
292
            lc->count_mb_ref_frame_usage,
293
            sizeof(cpi->mb.count_mb_ref_frame_usage));
294 295
}

296 297 298 299 300 301 302 303 304 305 306 307
static int rescale(int val, int num, int denom)
{
    int64_t llnum = num;
    int64_t llden = denom;
    int64_t llval = val;

    return (int)(llval * llnum / llden);
}

static void init_temporal_layer_context(VP8_COMP *cpi,
                                        VP8_CONFIG *oxcf,
                                        const int layer,
308
                                        double prev_layer_framerate)
309 310 311
{
    LAYER_CONTEXT *lc = &cpi->layer_context[layer];

312
    lc->framerate = cpi->output_framerate / cpi->oxcf.rate_decimator[layer];
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
    lc->target_bandwidth = cpi->oxcf.target_bitrate[layer] * 1000;

    lc->starting_buffer_level_in_ms = oxcf->starting_buffer_level;
    lc->optimal_buffer_level_in_ms  = oxcf->optimal_buffer_level;
    lc->maximum_buffer_size_in_ms   = oxcf->maximum_buffer_size;

    lc->starting_buffer_level =
        rescale((int)(oxcf->starting_buffer_level),
                lc->target_bandwidth, 1000);

    if (oxcf->optimal_buffer_level == 0)
      lc->optimal_buffer_level = lc->target_bandwidth / 8;
    else
      lc->optimal_buffer_level =
          rescale((int)(oxcf->optimal_buffer_level),
                  lc->target_bandwidth, 1000);

    if (oxcf->maximum_buffer_size == 0)
      lc->maximum_buffer_size = lc->target_bandwidth / 8;
    else
      lc->maximum_buffer_size =
          rescale((int)(oxcf->maximum_buffer_size),
                  lc->target_bandwidth, 1000);

    /* Work out the average size of a frame within this layer */
    if (layer > 0)
      lc->avg_frame_size_for_layer =
          (int)((cpi->oxcf.target_bitrate[layer] -
                cpi->oxcf.target_bitrate[layer-1]) * 1000 /
342
                (lc->framerate - prev_layer_framerate));
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368

     lc->active_worst_quality         = cpi->oxcf.worst_allowed_q;
     lc->active_best_quality          = cpi->oxcf.best_allowed_q;
     lc->avg_frame_qindex             = cpi->oxcf.worst_allowed_q;

     lc->buffer_level                 = lc->starting_buffer_level;
     lc->bits_off_target              = lc->starting_buffer_level;

     lc->total_actual_bits                 = 0;
     lc->ni_av_qi                          = 0;
     lc->ni_tot_qi                         = 0;
     lc->ni_frames                         = 0;
     lc->rate_correction_factor            = 1.0;
     lc->key_frame_rate_correction_factor  = 1.0;
     lc->gf_rate_correction_factor         = 1.0;
     lc->inter_frame_target                = 0;
}

// Upon a run-time change in temporal layers, reset the layer context parameters
// for any "new" layers. For "existing" layers, let them inherit the parameters
// from the previous layer state (at the same layer #). In future we may want
// to better map the previous layer state(s) to the "new" ones.
static void reset_temporal_layer_change(VP8_COMP *cpi,
                                        VP8_CONFIG *oxcf,
                                        const int prev_num_layers)
{
369
    int i;
370
    double prev_layer_framerate = 0;
371 372 373 374 375 376 377 378 379 380 381 382 383
    const int curr_num_layers = cpi->oxcf.number_of_layers;
    // If the previous state was 1 layer, get current layer context from cpi.
    // We need this to set the layer context for the new layers below.
    if (prev_num_layers == 1)
    {
        cpi->current_layer = 0;
        save_layer_context(cpi);
    }
    for (i = 0; i < curr_num_layers; i++)
    {
        LAYER_CONTEXT *lc = &cpi->layer_context[i];
        if (i >= prev_num_layers)
        {
384
           init_temporal_layer_context(cpi, oxcf, i, prev_layer_framerate);
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
        }
        // The initial buffer levels are set based on their starting levels.
        // We could set the buffer levels based on the previous state (normalized
        // properly by the layer bandwidths) but we would need to keep track of
        // the previous set of layer bandwidths (i.e., target_bitrate[i])
        // before the layer change. For now, reset to the starting levels.
        lc->buffer_level = cpi->oxcf.starting_buffer_level_in_ms *
                           cpi->oxcf.target_bitrate[i];
        lc->bits_off_target = lc->buffer_level;
        // TDOD(marpan): Should we set the rate_correction_factor and
        // active_worst/best_quality to values derived from the previous layer
        // state (to smooth-out quality dips/rate fluctuation at transition)?

        // We need to treat the 1 layer case separately: oxcf.target_bitrate[i]
        // is not set for 1 layer, and the restore_layer_context/save_context()
        // are not called in the encoding loop, so we need to call it here to
        // pass the layer context state to |cpi|.
        if (curr_num_layers == 1)
        {
            lc->target_bandwidth = cpi->oxcf.target_bandwidth;
            lc->buffer_level = cpi->oxcf.starting_buffer_level_in_ms *
                               lc->target_bandwidth  / 1000;
            lc->bits_off_target = lc->buffer_level;
            restore_layer_context(cpi, 0);
        }
410 411
        prev_layer_framerate = cpi->output_framerate /
                               cpi->oxcf.rate_decimator[i];
412 413 414
    }
}

John Koleszar's avatar
John Koleszar committed
415 416
static void setup_features(VP8_COMP *cpi)
{
417 418 419 420 421 422 423 424 425 426 427
    // If segmentation enabled set the update flags
    if ( cpi->mb.e_mbd.segmentation_enabled )
    {
        cpi->mb.e_mbd.update_mb_segmentation_map = 1;
        cpi->mb.e_mbd.update_mb_segmentation_data = 1;
    }
    else
    {
        cpi->mb.e_mbd.update_mb_segmentation_map = 0;
        cpi->mb.e_mbd.update_mb_segmentation_data = 0;
    }
John Koleszar's avatar
John Koleszar committed
428 429 430 431 432

    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));
433 434
    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
435

436
    set_default_lf_deltas(cpi);
John Koleszar's avatar
John Koleszar committed
437 438 439 440

}


441 442 443
static void dealloc_raw_frame_buffers(VP8_COMP *cpi);


444
static void dealloc_compressor_data(VP8_COMP *cpi)
John Koleszar's avatar
John Koleszar committed
445
{
446
    vpx_free(cpi->tplist);
447 448
    cpi->tplist = NULL;

449
    /* Delete last frame MV storage buffers */
450
    vpx_free(cpi->lfmv);
451 452
    cpi->lfmv = 0;

453
    vpx_free(cpi->lf_ref_frame_sign_bias);
454 455
    cpi->lf_ref_frame_sign_bias = 0;

456
    vpx_free(cpi->lf_ref_frame);
457
    cpi->lf_ref_frame = 0;
John Koleszar's avatar
John Koleszar committed
458

459
    /* Delete sementation map */
460
    vpx_free(cpi->segmentation_map);
John Koleszar's avatar
John Koleszar committed
461 462
    cpi->segmentation_map = 0;

463
    vpx_free(cpi->active_map);
John Koleszar's avatar
John Koleszar committed
464 465 466 467
    cpi->active_map = 0;

    vp8_de_alloc_frame_buffers(&cpi->common);

468
    vp8_yv12_de_alloc_frame_buffer(&cpi->pick_lf_lvl_frame);
John Koleszar's avatar
John Koleszar committed
469
    vp8_yv12_de_alloc_frame_buffer(&cpi->scaled_source);
470
    dealloc_raw_frame_buffers(cpi);
John Koleszar's avatar
John Koleszar committed
471 472 473 474

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

475
    /* Structure used to monitor GF usage */
476
    vpx_free(cpi->gf_active_flags);
477 478
    cpi->gf_active_flags = 0;

479
    /* Activity mask based per mb zbin adjustments */
480 481 482
    vpx_free(cpi->mb_activity_map);
    cpi->mb_activity_map = 0;

483
    vpx_free(cpi->mb.pip);
484
    cpi->mb.pip = 0;
485 486 487 488 489

#if CONFIG_MULTITHREAD
    vpx_free(cpi->mt_current_mb_col);
    cpi->mt_current_mb_col = NULL;
#endif
John Koleszar's avatar
John Koleszar committed
490 491
}

492
static void enable_segmentation(VP8_COMP *cpi)
John Koleszar's avatar
John Koleszar committed
493
{
494
    /* Set the appropriate feature bit */
John Koleszar's avatar
John Koleszar committed
495 496 497 498
    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;
}
499
static void disable_segmentation(VP8_COMP *cpi)
John Koleszar's avatar
John Koleszar committed
500
{
501
    /* Clear the appropriate feature bit */
John Koleszar's avatar
John Koleszar committed
502 503 504
    cpi->mb.e_mbd.segmentation_enabled = 0;
}

505 506 507
/* Valid values for a segment are 0 to 3
 * Segmentation map is arrange as [Rows][Columns]
 */
508
static void set_segmentation_map(VP8_COMP *cpi, unsigned char *segmentation_map)
John Koleszar's avatar
John Koleszar committed
509
{
510
    /* Copy in the new segmentation map */
James Zern's avatar
James Zern committed
511
    memcpy(cpi->segmentation_map, segmentation_map, (cpi->common.mb_rows * cpi->common.mb_cols));
John Koleszar's avatar
John Koleszar committed
512

513
    /* Signal that the map should be updated. */
John Koleszar's avatar
John Koleszar committed
514 515 516 517
    cpi->mb.e_mbd.update_mb_segmentation_map = 1;
    cpi->mb.e_mbd.update_mb_segmentation_data = 1;
}

518 519 520 521 522 523 524 525 526 527 528 529
/* 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).
 *
 */
530
static void set_segment_data(VP8_COMP *cpi, signed char *feature_data, unsigned char abs_delta)
John Koleszar's avatar
John Koleszar committed
531 532
{
    cpi->mb.e_mbd.mb_segement_abs_delta = abs_delta;
James Zern's avatar
James Zern committed
533
    memcpy(cpi->segment_feature_data, feature_data, sizeof(cpi->segment_feature_data));
John Koleszar's avatar
John Koleszar committed
534 535 536
}


537
static void segmentation_test_function(VP8_COMP *cpi)
John Koleszar's avatar
John Koleszar committed
538 539 540 541
{
    unsigned char *seg_map;
    signed char feature_data[MB_LVL_MAX][MAX_MB_SEGMENTS];

542
    // Create a temporary map for segmentation data.
John Koleszar's avatar
John Koleszar committed
543 544
    CHECK_MEM_ERROR(seg_map, vpx_calloc(cpi->common.mb_rows * cpi->common.mb_cols, 1));

545
    // Set the segmentation Map
546
    set_segmentation_map(cpi, seg_map);
John Koleszar's avatar
John Koleszar committed
547

548
    // Activate segmentation.
549
    enable_segmentation(cpi);
John Koleszar's avatar
John Koleszar committed
550

551
    // Set up the quant segment data
John Koleszar's avatar
John Koleszar committed
552 553 554 555
    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;
556
    // Set up the loop segment data
John Koleszar's avatar
John Koleszar committed
557 558 559 560 561
    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;

562 563
    // Initialise the feature data structure
    // SEGMENT_DELTADATA    0, SEGMENT_ABSDATA      1
564
    set_segment_data(cpi, &feature_data[0][0], SEGMENT_DELTADATA);
John Koleszar's avatar
John Koleszar committed
565

566
    // Delete sementation map
567
    vpx_free(seg_map);
John Koleszar's avatar
John Koleszar committed
568 569 570 571

    seg_map = 0;
}

572
/* A simple function to cyclically refresh the background at a lower Q */
John Koleszar's avatar
John Koleszar committed
573 574
static void cyclic_background_refresh(VP8_COMP *cpi, int Q, int lf_adjustment)
{
575
    unsigned char *seg_map = cpi->segmentation_map;
John Koleszar's avatar
John Koleszar committed
576 577 578 579 580
    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;

581
    cpi->cyclic_refresh_q = Q / 2;
John Koleszar's avatar
John Koleszar committed
582

Marco's avatar
Marco committed
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
    if (cpi->oxcf.screen_content_mode) {
      // Modify quality ramp-up based on Q. Above some Q level, increase the
      // number of blocks to be refreshed, and reduce it below the thredhold.
      // Turn-off under certain conditions (i.e., away from key frame, and if
      // we are at good quality (low Q) and most of the blocks were skipped-encoded
      // in previous frame.
      if (Q >= 100) {
        cpi->cyclic_refresh_mode_max_mbs_perframe =
            (cpi->common.mb_rows * cpi->common.mb_cols) / 10;
      } else if (cpi->frames_since_key > 250 &&
                 Q < 20 &&
                 cpi->mb.skip_true_count > (int)(0.95 * mbs_in_frame)) {
        cpi->cyclic_refresh_mode_max_mbs_perframe = 0;
      } else {
        cpi->cyclic_refresh_mode_max_mbs_perframe =
            (cpi->common.mb_rows * cpi->common.mb_cols) / 20;
      }
      block_count = cpi->cyclic_refresh_mode_max_mbs_perframe;
    }

603 604 605 606
    // Set every macroblock to be eligible for update.
    // For key frame this will reset seg map to 0.
    vpx_memset(cpi->segmentation_map, 0, mbs_in_frame);

Marco's avatar
Marco committed
607
    if (cpi->common.frame_type != KEY_FRAME && block_count > 0)
John Koleszar's avatar
John Koleszar committed
608
    {
609 610
        /* Cycle through the macro_block rows */
        /* MB loop to set local segmentation map */
611
        i = cpi->cyclic_refresh_mode_index;
612
        assert(i < mbs_in_frame);
613
        do
John Koleszar's avatar
John Koleszar committed
614
        {
615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
          /* 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;
              block_count --;
          }
          else if (cpi->cyclic_refresh_map[i] < 0)
              cpi->cyclic_refresh_map[i]++;

          i++;
          if (i == mbs_in_frame)
              i = 0;
John Koleszar's avatar
John Koleszar committed
632 633

        }
634
        while(block_count && i != cpi->cyclic_refresh_mode_index);
John Koleszar's avatar
John Koleszar committed
635 636

        cpi->cyclic_refresh_mode_index = i;
637 638

#if CONFIG_TEMPORAL_DENOISING
639 640
        if (cpi->oxcf.noise_sensitivity > 0) {
          if (cpi->denoiser.denoiser_mode == kDenoiserOnYUVAggressive &&
Marco's avatar
Marco committed
641 642 643
              Q < (int)cpi->denoiser.denoise_pars.qp_thresh &&
              (cpi->frames_since_key >
               2 * cpi->denoiser.denoise_pars.consec_zerolast)) {
644
            // Under aggressive denoising, use segmentation to turn off loop
Marco's avatar
Marco committed
645
            // filter below some qp thresh. The filter is reduced for all
646 647 648 649 650
            // blocks that have been encoded as ZEROMV LAST x frames in a row,
            // where x is set by cpi->denoiser.denoise_pars.consec_zerolast.
            // This is to avoid "dot" artifacts that can occur from repeated
            // loop filtering on noisy input source.
            cpi->cyclic_refresh_q = Q;
Marco's avatar
Marco committed
651 652
            // lf_adjustment = -MAX_LOOP_FILTER;
            lf_adjustment = -40;
653 654 655 656
            for (i = 0; i < mbs_in_frame; ++i) {
              seg_map[i] = (cpi->consec_zero_last[i] >
                            cpi->denoiser.denoise_pars.consec_zerolast) ? 1 : 0;
            }
657 658 659
          }
        }
#endif
John Koleszar's avatar
John Koleszar committed
660 661
    }

662
    /* Activate segmentation. */
663 664
    cpi->mb.e_mbd.update_mb_segmentation_map = 1;
    cpi->mb.e_mbd.update_mb_segmentation_data = 1;
665
    enable_segmentation(cpi);
John Koleszar's avatar
John Koleszar committed
666

667
    /* Set up the quant segment data */
John Koleszar's avatar
John Koleszar committed
668 669 670 671 672
    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;

673
    /* Set up the loop segment data */
John Koleszar's avatar
John Koleszar committed
674 675 676 677 678
    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;

679
    /* Initialise the feature data structure */
680
    set_segment_data(cpi, &feature_data[0][0], SEGMENT_DELTADATA);
John Koleszar's avatar
John Koleszar committed
681 682 683

}

684
static void set_default_lf_deltas(VP8_COMP *cpi)
John Koleszar's avatar
John Koleszar committed
685 686 687 688 689 690 691
{
    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));

692
    /* Test of ref frame deltas */
John Koleszar's avatar
John Koleszar committed
693 694 695 696 697
    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;

698
    cpi->mb.e_mbd.mode_lf_deltas[0] = 4;               /* BPRED */
699 700 701 702 703 704

    if(cpi->oxcf.Mode == MODE_REALTIME)
      cpi->mb.e_mbd.mode_lf_deltas[1] = -12;              /* Zero */
    else
      cpi->mb.e_mbd.mode_lf_deltas[1] = -2;              /* Zero */

705 706
    cpi->mb.e_mbd.mode_lf_deltas[2] = 2;               /* New mv */
    cpi->mb.e_mbd.mode_lf_deltas[3] = 4;               /* Split mv */
John Koleszar's avatar
John Koleszar committed
707 708
}

709 710 711 712 713 714
/* Convenience macros for mapping speed and mode into a continuous
 * range
 */
#define GOOD(x) (x+1)
#define RT(x) (x+7)

715
static int speed_map(int speed, const int *map)
716 717 718 719 720 721 722 723 724 725
{
    int res;

    do
    {
        res = *map++;
    } while(speed >= *map++);
    return res;
}

726
static const int thresh_mult_map_znn[] = {
727 728 729 730
    /* map common to zero, nearest, and near */
    0, GOOD(2), 1500, GOOD(3), 2000, RT(0), 1000, RT(2), 2000, INT_MAX
};

731
static const int thresh_mult_map_vhpred[] = {
732 733 734 735
    1000, GOOD(2), 1500, GOOD(3), 2000, RT(0), 1000, RT(1), 2000,
    RT(7), INT_MAX, INT_MAX
};

736
static const int thresh_mult_map_bpred[] = {
737 738 739 740
    2000, GOOD(0), 2500, GOOD(2), 5000, GOOD(3), 7500, RT(0), 2500, RT(1), 5000,
    RT(6), INT_MAX, INT_MAX
};

741
static const int thresh_mult_map_tm[] = {
742 743 744 745
    1000, GOOD(2), 1500, GOOD(3), 2000, RT(0), 0, RT(1), 1000, RT(2), 2000,
    RT(7), INT_MAX, INT_MAX
};

746
static const int thresh_mult_map_new1[] = {
747 748 749
    1000, GOOD(2), 2000, RT(0), 2000, INT_MAX
};

750
static const int thresh_mult_map_new2[] = {
751 752 753 754
    1000, GOOD(2), 2000, GOOD(3), 2500, GOOD(5), 4000, RT(0), 2000, RT(2), 2500,
    RT(5), 4000, INT_MAX
};

755
static const int thresh_mult_map_split1[] = {
756 757 758 759
    2500, GOOD(0), 1700, GOOD(2), 10000, GOOD(3), 25000, GOOD(4), INT_MAX,
    RT(0), 5000, RT(1), 10000, RT(2), 25000, RT(3), INT_MAX, INT_MAX
};

760
static const int thresh_mult_map_split2[] = {
761 762 763 764
    5000, GOOD(0), 4500, GOOD(2), 20000, GOOD(3), 50000, GOOD(4), INT_MAX,
    RT(0), 10000, RT(1), 20000, RT(2), 50000, RT(3), INT_MAX, INT_MAX
};

765
static const int mode_check_freq_map_zn2[] = {
766 767 768 769
    /* {zero,nearest}{2,3} */
    0, RT(10), 1<<1, RT(11), 1<<2, RT(12), 1<<3, INT_MAX
};

770
static const int mode_check_freq_map_vhbpred[] = {
771 772 773
    0, GOOD(5), 2, RT(0), 0, RT(3), 2, RT(5), 4, INT_MAX
};

774
static const int mode_check_freq_map_near2[] = {
775 776 777 778
    0, GOOD(5), 2, RT(0), 0, RT(3), 2, RT(10), 1<<2, RT(11), 1<<3, RT(12), 1<<4,
    INT_MAX
};

779
static const int mode_check_freq_map_new1[] = {
780 781 782
    0, RT(10), 1<<1, RT(11), 1<<2, RT(12), 1<<3, INT_MAX
};

783
static const int mode_check_freq_map_new2[] = {
784 785 786 787
    0, GOOD(5), 4, RT(0), 0, RT(3), 4, RT(10), 1<<3, RT(11), 1<<4, RT(12), 1<<5,
    INT_MAX
};

788
static const int mode_check_freq_map_split1[] = {
789 790 791
    0, GOOD(2), 2, GOOD(3), 7, RT(1), 2, RT(2), 7, INT_MAX
};

792
static const int mode_check_freq_map_split2[] = {
793 794 795
    0, GOOD(1), 2, GOOD(2), 4, GOOD(3), 15, RT(1), 4, RT(2), 15, INT_MAX
};

John Koleszar's avatar
John Koleszar committed
796 797 798 799 800 801 802
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;
803
    int last_improved_quant = sf->improved_quant;
804
    int ref_frames;
John Koleszar's avatar
John Koleszar committed
805

806
    /* Initialise default mode frequency sampling variables */
John Koleszar's avatar
John Koleszar committed
807 808 809 810 811
    for (i = 0; i < MAX_MODES; i ++)
    {
        cpi->mode_check_freq[i] = 0;
    }

812
    cpi->mb.mbs_tested_so_far = 0;
Marco's avatar
Marco committed
813
    cpi->mb.mbs_zero_last_dot_suppress = 0;
John Koleszar's avatar
John Koleszar committed
814

815
    /* best quality defaults */
John Koleszar's avatar
John Koleszar committed
816 817 818 819 820 821 822 823 824 825
    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;
826
    sf->use_fastquant_for_pick = 0;
827
    sf->no_skip_block4x4_search = 1;
John Koleszar's avatar
John Koleszar committed
828 829 830

    sf->first_step = 0;
    sf->max_step_search_steps = MAX_MVSEARCH_STEPS;
831
    sf->improved_mv_pred = 1;
John Koleszar's avatar
John Koleszar committed
832

833
    /* default thresholds to 0 */
John Koleszar's avatar
John Koleszar committed
834 835 836
    for (i = 0; i < MAX_MODES; i++)
        sf->thresh_mult[i] = 0;

837 838
    /* Count enabled references */
    ref_frames = 1;
Johann's avatar
Johann committed
839
    if (cpi->ref_frame_flags & VP8_LAST_FRAME)
840
        ref_frames++;
Johann's avatar
Johann committed
841
    if (cpi->ref_frame_flags & VP8_GOLD_FRAME)
842
        ref_frames++;
Johann's avatar
Johann committed
843
    if (cpi->ref_frame_flags & VP8_ALTR_FRAME)
844 845
        ref_frames++;

846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880
    /* Convert speed to continuous range, with clamping */
    if (Mode == 0)
        Speed = 0;
    else if (Mode == 2)
        Speed = RT(Speed);
    else
    {
        if (Speed > 5)
            Speed = 5;
        Speed = GOOD(Speed);
    }

    sf->thresh_mult[THR_ZERO1] =
    sf->thresh_mult[THR_NEAREST1] =
    sf->thresh_mult[THR_NEAR1] =
    sf->thresh_mult[THR_DC] = 0; /* always */

    sf->thresh_mult[THR_ZERO2] =
    sf->thresh_mult[THR_ZERO3] =
    sf->thresh_mult[THR_NEAREST2] =
    sf->thresh_mult[THR_NEAREST3] =
    sf->thresh_mult[THR_NEAR2]  =
    sf->thresh_mult[THR_NEAR3]  = speed_map(Speed, thresh_mult_map_znn);

    sf->thresh_mult[THR_V_PRED] =
    sf->thresh_mult[THR_H_PRED] = speed_map(Speed, thresh_mult_map_vhpred);
    sf->thresh_mult[THR_B_PRED] = speed_map(Speed, thresh_mult_map_bpred);
    sf->thresh_mult[THR_TM]     = speed_map(Speed, thresh_mult_map_tm);
    sf->thresh_mult[THR_NEW1]   = speed_map(Speed, thresh_mult_map_new1);
    sf->thresh_mult[THR_NEW2]   =
    sf->thresh_mult[THR_NEW3]   = speed_map(Speed, thresh_mult_map_new2);
    sf->thresh_mult[THR_SPLIT1] = speed_map(Speed, thresh_mult_map_split1);
    sf->thresh_mult[THR_SPLIT2] =
    sf->thresh_mult[THR_SPLIT3] = speed_map(Speed, thresh_mult_map_split2);

Marco's avatar
Marco committed
881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899
    // Special case for temporal layers.
    // Reduce the thresholds for zero/nearest/near for GOLDEN, if GOLDEN is
    // used as second reference. We don't modify thresholds for ALTREF case
    // since ALTREF is usually used as long-term reference in temporal layers.
    if ((cpi->Speed <= 6) &&
        (cpi->oxcf.number_of_layers > 1) &&
        (cpi->ref_frame_flags & VP8_LAST_FRAME) &&
        (cpi->ref_frame_flags & VP8_GOLD_FRAME)) {
      if (cpi->closest_reference_frame == GOLDEN_FRAME) {
        sf->thresh_mult[THR_ZERO2] =  sf->thresh_mult[THR_ZERO2] >> 3;
        sf->thresh_mult[THR_NEAREST2] = sf->thresh_mult[THR_NEAREST2] >> 3;
        sf->thresh_mult[THR_NEAR2]  = sf->thresh_mult[THR_NEAR2] >> 3;
      } else {
        sf->thresh_mult[THR_ZERO2] =  sf->thresh_mult[THR_ZERO2] >> 1;
        sf->thresh_mult[THR_NEAREST2] = sf->thresh_mult[THR_NEAREST2] >> 1;
        sf->thresh_mult[THR_NEAR2]  = sf->thresh_mult[THR_NEAR2] >> 1;
      }
    }

900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929
    cpi->mode_check_freq[THR_ZERO1] =
    cpi->mode_check_freq[THR_NEAREST1] =
    cpi->mode_check_freq[THR_NEAR1] =
    cpi->mode_check_freq[THR_TM]     =
    cpi->mode_check_freq[THR_DC] = 0; /* always */

    cpi->mode_check_freq[THR_ZERO2] =
    cpi->mode_check_freq[THR_ZERO3] =
    cpi->mode_check_freq[THR_NEAREST2] =
    cpi->mode_check_freq[THR_NEAREST3] = speed_map(Speed,
                                                   mode_check_freq_map_zn2);

    cpi->mode_check_freq[THR_NEAR2]  =
    cpi->mode_check_freq[THR_NEAR3]  = speed_map(Speed,
                                                 mode_check_freq_map_near2);

    cpi->mode_check_freq[THR_V_PRED] =
    cpi->mode_check_freq[THR_H_PRED] =
    cpi->mode_check_freq[THR_B_PRED] = speed_map(Speed,
                                                 mode_check_freq_map_vhbpred);
    cpi->mode_check_freq[THR_NEW1]   = speed_map(Speed,
                                                 mode_check_freq_map_new1);
    cpi->mode_check_freq[THR_NEW2]   =
    cpi->mode_check_freq[THR_NEW3]   = speed_map(Speed,
                                                 mode_check_freq_map_new2);
    cpi->mode_check_freq[THR_SPLIT1] = speed_map(Speed,
                                                 mode_check_freq_map_split1);
    cpi->mode_check_freq[THR_SPLIT2] =
    cpi->mode_check_freq[THR_SPLIT3] = speed_map(Speed,
                                                 mode_check_freq_map_split2);
930
    Speed = cpi->Speed;
John Koleszar's avatar
John Koleszar committed
931 932 933
    switch (Mode)
    {
#if !(CONFIG_REALTIME_ONLY)
934
    case 0: /* best quality mode */
John Koleszar's avatar
John Koleszar committed
935 936 937 938 939
        sf->first_step = 0;
        sf->max_step_search_steps = MAX_MVSEARCH_STEPS;
        break;
    case 1:
    case 3:
940
        if (Speed > 0)
John Koleszar's avatar
John Koleszar committed
941
        {
942 943 944 945
            /* Disable coefficient optimization above speed 0 */
            sf->optimize_coefficients = 0;
            sf->use_fastquant_for_pick = 1;
            sf->no_skip_block4x4_search = 0;
946

947
            sf->first_step = 1;
John Koleszar's avatar
John Koleszar committed
948 949
        }

950
        if (Speed > 2)
John Koleszar's avatar
John Koleszar committed
951
        {
952 953 954
            sf->improved_quant = 0;
            sf->improved_dct = 0;

955 956 957
            /* Only do recode loop on key frames, golden frames and
             * alt ref frames
             */
John Koleszar's avatar
John Koleszar committed
958 959 960 961
            sf->recode_loop = 2;

        }

962
        if (Speed > 3)
John Koleszar's avatar
John Koleszar committed
963 964
        {
            sf->auto_filter = 1;
965 966
            sf->recode_loop = 0; /* recode loop off */
            sf->RD = 0;         /* Turn rd off */
967

John Koleszar's avatar
John Koleszar committed
968 969 970 971
        }

        if (Speed > 4)
        {
972
            sf->auto_filter = 0;  /* Faster selection of loop filter */
John Koleszar's avatar
John Koleszar committed
973 974 975 976 977 978 979 980 981 982 983 984 985 986 987
        }

        break;
#endif
    case 2:
        sf->optimize_coefficients = 0;
        sf->recode_loop = 0;
        sf->auto_filter = 1;
        sf->iterative_sub_pixel = 1;
        sf->search_method = NSTEP;

        if (Speed > 0)
        {
            sf->improved_quant = 0;
            sf->improved_dct = 0;
988 989 990 991

            sf->use_fastquant_for_pick = 1;
            sf->no_skip_block4x4_search = 0;
            sf->first_step = 1;
John Koleszar's avatar
John Koleszar committed
992 993 994
        }

        if (Speed > 2)
995
            sf->auto_filter = 0;  /* Faster selection of loop filter */
John Koleszar's avatar
John Koleszar committed
996 997 998 999 1000 1001 1002 1003 1004

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

        if (Speed > 4)
        {
1005
            sf->auto_filter = 0;  /* Faster selection of loop filter */
John Koleszar's avatar
John Koleszar committed
1006
            sf->search_method = HEX;
1007
            sf->iterative_sub_pixel = 0;
John Koleszar's avatar
John Koleszar committed
1008 1009 1010 1011
        }

        if (Speed > 6)
        {
1012
            unsigned int sum = 0;
John Koleszar's avatar
John Koleszar committed
1013
            unsigned int total_mbs = cm->MBs;
1014
            int thresh;
1015
            unsigned int total_skip;
John Koleszar's avatar
John Koleszar committed
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025

            int min = 2000;

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

            min >>= 7;

            for (i = 0; i < min; i++)
            {
1026
                sum += cpi->mb.error_bins[i];
John Koleszar's avatar
John Koleszar committed
1027 1028 1029 1030 1031
            }

            total_skip = sum;
            sum = 0;

1032
            /* i starts from 2 to make sure thresh started from 2048 */
John Koleszar's avatar
John Koleszar committed
1033 1034
            for (; i < 1024; i++)
            {
1035
                sum += cpi->mb.error_bins[i];
John Koleszar's avatar
John Koleszar committed
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046

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

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

            if (thresh < 2000)
                thresh = 2000;

1047
            if (ref_frames > 1)
John Koleszar's avatar
John Koleszar committed
1048
            {
1049 1050 1051
                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
1052 1053
            }

1054
            if (ref_frames > 2)
John Koleszar's avatar
John Koleszar committed
1055
            {
1056 1057 1058
                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
1059 1060
            }

1061
            if (ref_frames > 3)
John Koleszar's avatar
John Koleszar committed
1062
            {
1063 1064 1065
                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
1066 1067
            }

1068
            sf->improved_mv_pred = 0;
John Koleszar's avatar
John Koleszar committed
1069 1070 1071 1072 1073
        }

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

1074 1075 1076
        if(cm->version == 0)
        {
            cm->filter_type = NORMAL_LOOPFILTER;
John Koleszar's avatar
John Koleszar committed
1077

1078 1079 1080 1081 1082
            if (Speed >= 14)
                cm->filter_type = SIMPLE_LOOPFILTER;
        }
        else
        {
John Koleszar's avatar
John Koleszar committed
1083
            cm->filter_type = SIMPLE_LOOPFILTER;
1084
        }
John Koleszar's avatar
John Koleszar committed
1085

1086
        /* This has a big hit on quality. Last resort */
John Koleszar's avatar
John Koleszar committed
1087
        if (Speed >= 15)
1088
            sf->half_pixel_search = 0;
John Koleszar's avatar
John Koleszar committed
1089

1090
        vpx_memset(cpi->mb.error_bins, 0, sizeof(cpi->mb.error_bins));
John Koleszar's avatar
John Koleszar committed
1091

1092 1093
    }; /* switch */

1094 1095 1096
    /* Slow quant, dct and trellis not worthwhile for first pass
     * so make sure they are always turned off.
     */
1097 1098 1099 1100 1101 1102 1103
    if ( cpi->pass == 1 )
    {
        sf->improved_quant = 0;
        sf->optimize_coefficients = 0;
        sf->improved_dct = 0;
    }

John Koleszar's avatar
John Koleszar committed
1104 1105
    if (cpi->sf.search_method == NSTEP)
    {
1106
        vp8_init3smotion_compensation(&cpi->mb, cm->yv12_fb[cm->lst_fb_idx].y_stride);
John Koleszar's avatar
John Koleszar committed
1107 1108 1109
    }
    else if (cpi->sf.search_method == DIAMOND)
    {
1110
        vp8_init_dsmotion_compensation(&cpi->mb, cm->yv12_fb[cm->lst_fb_idx].y_stride);
John Koleszar's avatar
John Koleszar committed
1111 1112 1113 1114
    }

    if (cpi->sf.improved_dct)
    {
John Koleszar's avatar
John Koleszar committed
1115 1116
        cpi->mb.short_fdct8x4 = vp8_short_fdct8x4;
        cpi->mb.short_fdct4x4 = vp8_short_fdct4x4;
John Koleszar's avatar
John Koleszar committed
1117 1118 1119
    }
    else
    {
John Koleszar's avatar
John Koleszar committed
1120 1121 1122
        /* No fast FDCT defined for any platform at this time. */
        cpi->mb.short_fdct8x4 = vp8_short_fdct8x4;
        cpi->mb.short_fdct4x4 = vp8_short_fdct4x4;
John Koleszar's avatar
John Koleszar committed
1123 1124
    }

John Koleszar's avatar
John Koleszar committed
1125
    cpi->mb.short_walsh4x4 = vp8_short_walsh4x4;
John Koleszar's avatar
John Koleszar committed
1126 1127 1128

    if (cpi->sf.improved_quant)
    {
1129
        cpi->mb.quantize_b      = vp8_regular_quantize_b;
John Koleszar's avatar
John Koleszar committed
1130 1131 1132
    }
    else
    {
1133
        cpi->mb.quantize_b      = vp8_fast_quantize_b;
John Koleszar's avatar
John Koleszar committed
1134
    }
1135 1136
    if (cpi->sf.improved_quant != last_improved_quant)
        vp8cx_init_quantizer(cpi);
John Koleszar's avatar
John Koleszar committed
1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154

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

1155
    if (cpi->sf.optimize_coefficients == 1 && cpi->pass!=1)
1156
        cpi->mb.optimize = 1;
John Koleszar's avatar
John Koleszar committed
1157 1158 1159 1160 1161 1162 1163 1164 1165 1166
    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
}
1167 1168 1169
#undef GOOD
#undef RT

John Koleszar's avatar
John Koleszar committed
1170 1171
static void alloc_raw_frame_buffers(VP8_COMP *cpi)
{
Attila Nagy's avatar
Attila Nagy committed
1172
#if VP8_TEMPORAL_ALT_REF
1173
    int width = (cpi->oxcf.Width + 15) & ~15;
1174
    int height = (cpi->oxcf.Height + 15) & ~15;
Attila Nagy's avatar
Attila Nagy committed
1175
#endif
John Koleszar's avatar
John Koleszar committed
1176

1177 1178 1179 1180 1181
    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
1182 1183 1184

#if VP8_TEMPORAL_ALT_REF

1185
    if (vp8_yv12_alloc_frame_buffer(&cpi->alt_ref_buffer,
1186
                                    width, height, VP8BORDERINPIXELS))
John Koleszar's avatar
John Koleszar committed
1187 1188 1189 1190 1191
        vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
                           "Failed to allocate altref buffer");

#endif
}
1192

1193 1194 1195 1196 1197 1198 1199 1200 1201 1202

static void dealloc_raw_frame_buffers(VP8_COMP *cpi)
{
#if VP8_TEMPORAL_ALT_REF
    vp8_yv12_de_alloc_frame_buffer(&cpi->alt_ref_buffer);
#endif
    vp8_lookahead_destroy(cpi->lookahead);
}


1203 1204
static int vp8_alloc_partition_data(VP8_COMP *cpi)
{
1205 1206
        vpx_free(cpi->mb.pip);

1207 1208 1209 1210
    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
1211
        return 1;
1212 1213 1214 1215 1216 1217

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

    return 0;
}

John Koleszar's avatar
John Koleszar committed
1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228
void vp8_alloc_compressor_data(VP8_COMP *cpi)
{
    VP8_COMMON *cm = & cpi->common;

    int width = cm->Width;
    int height = cm->Height;

    if (vp8_alloc_frame_buffers(cm, width, height))
        vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
                           "Failed to allocate frame buffers");

1229 1230 1231 1232 1233
    if (vp8_alloc_partition_data(cpi))
        vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
                           "Failed to allocate partition data");


John Koleszar's avatar
John Koleszar committed
1234 1235 1236 1237 1238 1239 1240
    if ((width & 0xf) != 0)
        width += 16 - (width & 0xf);

    if ((height & 0xf) != 0)
        height += 16 - (height & 0xf);


1241
    if (vp8_yv12_alloc_frame_buffer(&cpi->pick_lf_lvl_frame,
John Koleszar's avatar
John Koleszar committed
1242 1243 1244 1245
                                    width, height, VP8BORDERINPIXELS))
        vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
                           "Failed to allocate last frame buffer");

1246 1247
    if (vp8_yv12_alloc_frame_buffer(&cpi->scaled_source,
                                    width, height, VP8BORDERINPIXELS))
John Koleszar's avatar
John Koleszar committed
1248 1249
        vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
                           "Failed to allocate scaled source buffer");
1250 1251 1252

    vpx_free(cpi->tok);

John Koleszar's avatar
John Koleszar committed
1253
    {
1254 1255 1256
#if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
        unsigned int tokens = 8 * 24 * 16; /* one MB for each thread */
#else
John Koleszar's avatar
John Koleszar committed
1257
        unsigned int tokens = cm->mb_rows * cm->mb_cols * 24 * 16;
1258
#endif
John Koleszar's avatar
John Koleszar committed
1259 1260 1261
        CHECK_MEM_ERROR(cpi->tok, vpx_calloc(tokens, sizeof(*cpi->tok)));
    }

1262
    /* Data used for real time vc mode to see if gf needs refreshing */
1263
    cpi->zeromv_count = 0;
1264 1265


1266
    /* Structures used to monitor GF usage */
1267 1268
    vpx_free(cpi->gf_active_flags);
    CHECK_MEM_ERROR(cpi->gf_active_flags,
1269 1270
                    vpx_calloc(sizeof(*cpi->gf_active_flags),
                    cm->mb_rows * cm->mb_cols));
1271
    cpi->gf_active_count = cm->mb_rows * cm->mb_cols;
1272

1273 1274
    vpx_free(cpi->mb_activity_map);
    CHECK_MEM_ERROR(cpi->mb_activity_map,
1275
                    vpx_calloc(sizeof(*cpi->mb_activity_map),
1276
                    cm->mb_rows * cm->mb_cols));
1277

1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295
    /* allocate memory for storing last frame's MVs for MV prediction. */
    vpx_free(cpi->lfmv);
    CHECK_MEM_ERROR(cpi->lfmv, vpx_calloc((cm->mb_rows+2) * (cm->mb_cols+2),
                    sizeof(*cpi->lfmv)));
    vpx_free(cpi->lf_ref_frame_sign_bias);
    CHECK_MEM_ERROR(cpi->lf_ref_frame_sign_bias,
                    vpx_calloc((cm->mb_rows+2) * (cm->mb_cols+2),
                    sizeof(*cpi->lf_ref_frame_sign_bias)));
    vpx_free(cpi->lf_ref_frame);
    CHECK_MEM_ERROR(cpi->lf_ref_frame,
                    vpx_calloc((cm->mb_rows+2) * (cm->mb_cols+2),
                    sizeof(*cpi->lf_ref_frame)));

    /* Create the encoder segmentation map and set all entries to 0 */
    vpx_free(cpi->segmentation_map);
    CHECK_MEM_ERROR(cpi->segmentation_map,
                    vpx_calloc(cm->mb_rows * cm->mb_cols,
                    sizeof(*cpi->segmentation_map)));
1296
    cpi->cyclic_refresh_mode_index = 0;
1297 1298 1299 1300 1301 1302
    vpx_free(cpi->active_map);
    CHECK_MEM_ERROR(cpi->active_map,
                    vpx_calloc(cm->mb_rows * cm->mb_cols,
                    sizeof(*cpi->active_map)));
    vpx_memset(cpi->active_map , 1, (cm->mb_rows * cm->mb_cols));

Attila Nagy's avatar
Attila Nagy committed
1303 1304 1305 1306 1307 1308 1309 1310 1311
#if CONFIG_MULTITHREAD
    if (width < 640)
        cpi->mt_sync_range = 1;
    else if (width <= 1280)
        cpi->mt_sync_range = 4;
    else if (width <= 2560)
        cpi->mt_sync_range = 8;
    else
        cpi->mt_sync_range = 16;
1312 1313 1314 1315 1316

    if (cpi->oxcf.multi_threaded > 1)
    {
        vpx_free(cpi->mt_current_mb_col);
        CHECK_MEM_ERROR(cpi->mt_current_mb_col,
1317
                    vpx_malloc(sizeof(*cpi->mt_current_mb_col) * cm->mb_rows));
1318 1319
    }

Attila Nagy's avatar
Attila Nagy committed
1320
#endif
1321

1322
    vpx_free(cpi->tplist);
1323
    CHECK_MEM_ERROR(cpi->tplist, vpx_malloc(sizeof(TOKENLIST) * cm->mb_rows));
1324 1325 1326 1327 1328 1329

#if CONFIG_TEMPORAL_DENOISING
    if (cpi->oxcf.noise_sensitivity > 0) {
      vp8_denoiser_free(&cpi->denoiser);
      vp8_denoiser_allocate(&cpi->denoiser, width, height,
                            cm->mb_rows, cm->mb_cols,
1330
                            cpi->oxcf.noise_sensitivity);
1331 1332
    }
#endif
John Koleszar's avatar
John Koleszar committed
1333 1334 1335
}


1336
/* Quant MOD */
John Koleszar's avatar
John Koleszar committed
1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357
static const int q_trans[] =
{
    0,   1,  2,  3,  4,  5,  7,  8,
    9,  10, 12, 13, 15, 17, 18, 19,
    20,  21, 23, 24, 25, 26, 27, 28,
    29,  30, 31, 33, 35, 37, 39, 41,
    43,  45, 47, 49, 51, 53, 55, 57,
    59,  61, 64, 67, 70, 73, 76, 79,
    82,  85, 88, 91, 94, 97, 100, 103,
    106, 109, 112, 115, 118, 121, 124, 127,
};

int vp8_reverse_trans(int x)
{
    int i;

    for (i = 0; i < 64; i++)
        if (q_trans[i] >= x)
            return i