onyx_if.c 150 KB
Newer Older
John Koleszar's avatar
John Koleszar committed
1
/*
2
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
John Koleszar's avatar
John Koleszar committed
3
 *
4
 *  Use of this source code is governed by a BSD-style license
5
6
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
7
 *  in the file PATENTS.  All contributing project authors may
8
 *  be found in the AUTHORS file in the root of the source tree.
John Koleszar's avatar
John Koleszar committed
9
10
11
 */


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

#include "vp8/common/seg_common.h"
#include "mbgraph.h"
38
#include "vp8/common/pred_common.h"
39

40
41
42
#if ARCH_ARM
#include "vpx_ports/arm.h"
#endif
43

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

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

extern const int vp8_gf_interval_table[101];

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

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

94

John Koleszar's avatar
John Koleszar committed
95
96
97
98
99
100
extern double vp8_calc_ssimg
(
    YV12_BUFFER_CONFIG *source,
    YV12_BUFFER_CONFIG *dest,
    double *ssim_y,
    double *ssim_u,
101
102
    double *ssim_v,
    const vp8_variance_rtcd_vtable_t *rtcd
John Koleszar's avatar
John Koleszar committed
103
104
105
106
107
);


#endif

Yaowu Xu's avatar
Yaowu Xu committed
108
//#define OUTPUT_YUV_REC
John Koleszar's avatar
John Koleszar committed
109
110
111
112

#ifdef OUTPUT_YUV_SRC
FILE *yuv_file;
#endif
113
114
115
#ifdef OUTPUT_YUV_REC
FILE *yuv_rec_file;
#endif
John Koleszar's avatar
John Koleszar committed
116
117
118

#if 0
FILE *framepsnr;
Yaowu Xu's avatar
Yaowu Xu committed
119
FILE *kf_list;
John Koleszar's avatar
John Koleszar committed
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
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

141
142
143
#if defined(SECTIONBITS_OUTPUT)
extern unsigned __int64 Sectionbits[500];
#endif
John Koleszar's avatar
John Koleszar committed
144
#ifdef MODE_STATS
Yaowu Xu's avatar
Yaowu Xu committed
145
146
147
148
extern INT64 Sectionbits[500];
extern int y_modes[VP8_YMODES]  ;
extern int i8x8_modes[VP8_I8X8_MODES];
extern int uv_modes[VP8_UV_MODES] ;
149
extern int uv_modes_y[VP8_YMODES][VP8_UV_MODES];
150
151
152
153
extern int b_modes[B_MODE_COUNT];
extern int inter_y_modes[MB_MODE_COUNT] ;
extern int inter_uv_modes[VP8_UV_MODES] ;
extern unsigned int inter_b_modes[B_MODE_COUNT];
John Koleszar's avatar
John Koleszar committed
154
155
156
157
158
159
#endif

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

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

int vp8cx_base_skip_false_prob[QINDEX_RANGE];
162

163
// Tables relating active max Q to active min Q
164
165
166
167
168
169
170
171
172
173
174
175
176
static int kf_low_motion_minq[QINDEX_RANGE];
static int kf_high_motion_minq[QINDEX_RANGE];
static int gf_low_motion_minq[QINDEX_RANGE];
static int gf_mid_motion_minq[QINDEX_RANGE];
static int gf_high_motion_minq[QINDEX_RANGE];
static int inter_minq[QINDEX_RANGE];

// Functions to compute the active minq lookup table entries based on a
// formulaic approach to facilitate easier adjustment of the Q tables.
// The formulae were derived from computing a 3rd order polynomial best
// fit to the original data (after plotting real maxq vs minq (not q index))
int calculate_minq_index( double maxq,
                          double x3, double x2, double x, double c )
177
{
178
179
180
181
182
183
184
185
186
187
    int i;
    double minqtarget;
    double thisq;

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

    if ( minqtarget > maxq )
188
        minqtarget = maxq;
189
190
191
192
193
194
195

    for ( i = 0; i < QINDEX_RANGE; i++ )
    {
        thisq = vp8_convert_qindex_to_q(i);
        if ( minqtarget <= vp8_convert_qindex_to_q(i) )
            return i;
    }
196
197
    if ( i == QINDEX_RANGE )
        return QINDEX_RANGE-1;
198
199
}
void init_minq_luts()
200
{
201
202
203
204
205
206
207
    int i;
    double maxq;

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

Paul Wilkins's avatar
Paul Wilkins committed
208

209
        kf_low_motion_minq[i] = calculate_minq_index( maxq,
Paul Wilkins's avatar
Paul Wilkins committed
210
211
212
                                                      0.0000003,
                                                      -0.000015,
                                                      0.074,
213
                                                      0.0 );
Paul Wilkins's avatar
Paul Wilkins committed
214

215
        kf_high_motion_minq[i] = calculate_minq_index( maxq,
Paul Wilkins's avatar
Paul Wilkins committed
216
217
218
                                                       0.00000034,
                                                       -0.000125,
                                                       0.13,
219
220
                                                       0.0 );
        gf_low_motion_minq[i] = calculate_minq_index( maxq,
Paul Wilkins's avatar
Paul Wilkins committed
221
222
223
                                                      0.0000016,
                                                      -0.00078,
                                                      0.315,
224
225
                                                      0.0 );
        gf_mid_motion_minq[i] = calculate_minq_index( maxq,
Paul Wilkins's avatar
Paul Wilkins committed
226
227
228
                                                      0.00000415,
                                                      -0.0017,
                                                      0.425,
229
230
                                                      0.0 );
        gf_high_motion_minq[i] = calculate_minq_index( maxq,
Paul Wilkins's avatar
Paul Wilkins committed
231
232
233
                                                       0.00000725,
                                                       -0.00235,
                                                       0.47,
234
235
236
237
238
239
240
241
242
243
                                                       0.0  );
        inter_minq[i] = calculate_minq_index( maxq,
                                              0.00000271,
                                              -0.00113,
                                              0.697,
                                              0.0  );

    }
}

Paul Wilkins's avatar
Paul Wilkins committed
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
void init_base_skip_probs()
{
    int i;
    double q;
    int skip_prob;

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

        // Exponential decay caluclation of baseline skip prob with clamping
        // Based on crude best fit of old table.
        skip_prob = (int)( 564.25 * pow( 2.71828, (-0.012*q) ) );
        if ( skip_prob < 1 )
            skip_prob = 1;
        else if ( skip_prob > 255 )
            skip_prob = 255;

        vp8cx_base_skip_false_prob[i] = skip_prob;
    }
}

John Koleszar's avatar
John Koleszar committed
266
267
268
269
270
271
272
273
274
275
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();
276
        vp8_init_quant_tables();
Paul Wilkins's avatar
Paul Wilkins committed
277
        vp8_init_me_luts();
278
        init_minq_luts();
Paul Wilkins's avatar
Paul Wilkins committed
279
        init_base_skip_probs();
John Koleszar's avatar
John Koleszar committed
280
281
282
283
284
285
286
287
288
        init_done = 1;
    }
}
#ifdef PACKET_TESTING
extern FILE *vpxlogc;
#endif

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

John Koleszar's avatar
John Koleszar committed
291
    // Set up default state for MB feature flags
292
293
294

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

Paul Wilkins's avatar
Paul Wilkins committed
295
296
297
298
    xd->update_mb_segmentation_map = 0;
    xd->update_mb_segmentation_data = 0;
    vpx_memset(xd->mb_segment_tree_probs, 255, sizeof(xd->mb_segment_tree_probs));

Paul Wilkins's avatar
Paul Wilkins committed
299
    clearall_segfeatures( xd );
Paul Wilkins's avatar
Paul Wilkins committed
300
301
302
303
304
305
306

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

308
    set_default_lf_deltas(cpi);
John Koleszar's avatar
John Koleszar committed
309
310
311
312

}


313
static void dealloc_compressor_data(VP8_COMP *cpi)
John Koleszar's avatar
John Koleszar committed
314
{
315
    vpx_free(cpi->tplist);
316
317
    cpi->tplist = NULL;

318
    // Delete last frame MV storage buffers
319
    vpx_free(cpi->lfmv);
320
321
    cpi->lfmv = 0;

322
    vpx_free(cpi->lf_ref_frame_sign_bias);
323
324
    cpi->lf_ref_frame_sign_bias = 0;

325
    vpx_free(cpi->lf_ref_frame);
326
    cpi->lf_ref_frame = 0;
John Koleszar's avatar
John Koleszar committed
327
328

    // Delete sementation map
329
    vpx_free(cpi->segmentation_map);
John Koleszar's avatar
John Koleszar committed
330
    cpi->segmentation_map = 0;
331
332
    vpx_free(cpi->common.last_frame_seg_map);
    cpi->common.last_frame_seg_map = 0;
John Koleszar's avatar
John Koleszar committed
333

334
    vpx_free(cpi->active_map);
John Koleszar's avatar
John Koleszar committed
335
336
337
338
339
340
341
    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
342
    vp8_yv12_de_alloc_frame_buffer(&cpi->alt_ref_buffer);
John Koleszar's avatar
John Koleszar committed
343
#endif
John Koleszar's avatar
John Koleszar committed
344
    vp8_lookahead_destroy(cpi->lookahead);
John Koleszar's avatar
John Koleszar committed
345
346
347
348

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

349
    // Structure used to monitor GF usage
350
    vpx_free(cpi->gf_active_flags);
351
352
    cpi->gf_active_flags = 0;

353
354
355
356
357
358
    // 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;

359
    vpx_free(cpi->mb.pip);
360
361
    cpi->mb.pip = 0;

362
363
    vpx_free(cpi->twopass.total_stats);
    cpi->twopass.total_stats = 0;
364

365
366
367
    vpx_free(cpi->twopass.total_left_stats);
    cpi->twopass.total_left_stats = 0;

368
369
    vpx_free(cpi->twopass.this_frame_stats);
    cpi->twopass.this_frame_stats = 0;
John Koleszar's avatar
John Koleszar committed
370
371
}

372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
// Computes a q delta (in "q index" terms) to get from a starting q value
// to a target value
// target q value
static int compute_qdelta( VP8_COMP *cpi, double qstart, double qtarget )
{
    int i;
    int start_index = cpi->worst_quality;
    int target_index = cpi->worst_quality;
    int retval = 0;

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

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

    return target_index - start_index;
}

401
402
403
static void init_seg_features(VP8_COMP *cpi)
{
    VP8_COMMON *cm = &cpi->common;
404
    MACROBLOCKD *xd = &cpi->mb.e_mbd;
405

Paul Wilkins's avatar
Paul Wilkins committed
406
    int high_q = (int)(cpi->avg_q > 48.0);
407
    int qi_delta;
408

Paul Wilkins's avatar
Paul Wilkins committed
409
410
    // Disable and clear down for KF
    if ( cm->frame_type == KEY_FRAME  )
411
412
413
    {
        // Clear down the global segmentation map
        vpx_memset( cpi->segmentation_map, 0, (cm->mb_rows * cm->mb_cols));
414
415
        xd->update_mb_segmentation_map = 0;
        xd->update_mb_segmentation_data = 0;
416
        cpi->static_mb_pct = 0;
417

418
419
        // Disable segmentation
        vp8_disable_segmentation((VP8_PTR)cpi);
Paul Wilkins's avatar
Paul Wilkins committed
420
421
422

        // Clear down the segment features.
        clearall_segfeatures(xd);
423
    }
424

Paul Wilkins's avatar
Paul Wilkins committed
425
426
    // If this is an alt ref frame
    else if ( cm->refresh_alt_ref_frame )
427
    {
Paul Wilkins's avatar
Paul Wilkins committed
428
429
430
431
        // Clear down the global segmentation map
        vpx_memset( cpi->segmentation_map, 0, (cm->mb_rows * cm->mb_cols));
        xd->update_mb_segmentation_map = 0;
        xd->update_mb_segmentation_data = 0;
432
        cpi->static_mb_pct = 0;
Paul Wilkins's avatar
Paul Wilkins committed
433
434
435
436
437
438
439
440
441
442
443
444

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

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

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

449
450
            qi_delta = compute_qdelta( cpi, cpi->avg_q, (cpi->avg_q * 0.875) );
            set_segdata( xd, 1, SEG_LVL_ALT_Q, (qi_delta - 2) );
Paul Wilkins's avatar
Paul Wilkins committed
451
            set_segdata( xd, 1, SEG_LVL_ALT_LF, -2 );
Paul Wilkins's avatar
Paul Wilkins committed
452

453
454
455
456
457
            enable_segfeature(xd, 1, SEG_LVL_ALT_Q);
            enable_segfeature(xd, 1, SEG_LVL_ALT_LF);

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

459
        }
460
    }
Paul Wilkins's avatar
Paul Wilkins committed
461
    // All other frames if segmentation has been enabled
462
    else if ( xd->segmentation_enabled )
463
    {
464
/*
465
466
467
468
469
470
471
472
473
474
475
476
        int i;

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

Paul Wilkins's avatar
Paul Wilkins committed
479
480
        // First normal frame in a valid gf or alt ref group
        if ( cpi->common.frames_since_golden == 0 )
481
        {
Paul Wilkins's avatar
Paul Wilkins committed
482
483
            // Set up segment features for normal frames in an af group
            if ( cpi->source_alt_ref_active )
484
            {
Paul Wilkins's avatar
Paul Wilkins committed
485
                xd->update_mb_segmentation_map = 0;
486
                xd->update_mb_segmentation_data = 1;
Paul Wilkins's avatar
Paul Wilkins committed
487
488
                xd->mb_segement_abs_delta = SEGMENT_DELTADATA;

489
490
491
492
                qi_delta = compute_qdelta( cpi, cpi->avg_q,
                                           (cpi->avg_q * 1.125) );
                set_segdata( xd, 1, SEG_LVL_ALT_Q, (qi_delta + 2) );
                set_segdata( xd, 1, SEG_LVL_ALT_Q, 0 );
Paul Wilkins's avatar
Paul Wilkins committed
493
                enable_segfeature(xd, 1, SEG_LVL_ALT_Q);
Paul Wilkins's avatar
Paul Wilkins committed
494
495

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

498
499
                // Segment coding disabled for compred testing
                if ( high_q || (cpi->static_mb_pct == 100) )
Paul Wilkins's avatar
Paul Wilkins committed
500
                {
Paul Wilkins's avatar
Paul Wilkins committed
501
                    //set_segref(xd, 1, LAST_FRAME);
Paul Wilkins's avatar
Paul Wilkins committed
502
                    set_segref(xd, 1, ALTREF_FRAME);
Paul Wilkins's avatar
Paul Wilkins committed
503
                    enable_segfeature(xd, 1, SEG_LVL_REF_FRAME);
Paul Wilkins's avatar
Paul Wilkins committed
504
505

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

508
509
510
                    // EOB segment coding not fixed for 8x8 yet
                    set_segdata( xd, 1, SEG_LVL_EOB, 0 );
                    enable_segfeature(xd, 1, SEG_LVL_EOB);
Paul Wilkins's avatar
Paul Wilkins committed
511
                }
512
            }
Paul Wilkins's avatar
Paul Wilkins committed
513
514
            // Disable segmentation and clear down features if alt ref
            // is not active for this group
515
516
            else
            {
Paul Wilkins's avatar
Paul Wilkins committed
517
518
                vp8_disable_segmentation((VP8_PTR)cpi);

519
520
                vpx_memset( cpi->segmentation_map, 0,
                            (cm->mb_rows * cm->mb_cols));
Paul Wilkins's avatar
Paul Wilkins committed
521
522
523
524
525

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

                clearall_segfeatures(xd);
526
527
            }
        }
Paul Wilkins's avatar
Paul Wilkins committed
528
529
530

        // Special case where we are coding over the top of a previous
        // alt ref frame
531
        // Segment coding disabled for compred testing
Paul Wilkins's avatar
Paul Wilkins committed
532
533
534
535
536
        else if ( cpi->is_src_frame_alt_ref )
        {
            // Enable mode and ref frame features for segment 0 as well
            enable_segfeature(xd, 0, SEG_LVL_REF_FRAME);
            enable_segfeature(xd, 0, SEG_LVL_MODE);
537
538
            enable_segfeature(xd, 1, SEG_LVL_REF_FRAME);
            enable_segfeature(xd, 1, SEG_LVL_MODE);
Paul Wilkins's avatar
Paul Wilkins committed
539

Paul Wilkins's avatar
Paul Wilkins committed
540
541
542
543
544
            // All mbs should use ALTREF_FRAME, ZEROMV exclusively
            clear_segref(xd, 0);
            set_segref(xd, 0, ALTREF_FRAME);
            clear_segref(xd, 1);
            set_segref(xd, 1, ALTREF_FRAME);
Paul Wilkins's avatar
Paul Wilkins committed
545
546
            set_segdata( xd, 0, SEG_LVL_MODE, ZEROMV );
            set_segdata( xd, 1, SEG_LVL_MODE, ZEROMV );
Paul Wilkins's avatar
Paul Wilkins committed
547

548
549
550
            // Skip all MBs if high Q
            if ( high_q )
            {
551
552
553
554
                enable_segfeature(xd, 0, SEG_LVL_EOB);
                set_segdata( xd, 0, SEG_LVL_EOB, 0 );
                enable_segfeature(xd, 1, SEG_LVL_EOB);
                set_segdata( xd, 1, SEG_LVL_EOB, 0 );
555
            }
Paul Wilkins's avatar
Paul Wilkins committed
556
557
558
559
            // Enable data udpate
            xd->update_mb_segmentation_data = 1;
        }
        // All other frames.
560
561
        else
        {
Paul Wilkins's avatar
Paul Wilkins committed
562
563
            // No updeates.. leave things as they are.
            xd->update_mb_segmentation_map = 0;
564
            xd->update_mb_segmentation_data = 0;
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
        }
    }
}

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

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

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

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

    fclose(statsfile);
}

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

Paul Wilkins's avatar
Paul Wilkins committed
626
627
628
629
    // Only modes 0 and 1 supported for now in experimental code basae
    if ( Mode > 1 )
        Mode = 1;

John Koleszar's avatar
John Koleszar committed
630
631
632
633
634
635
636
637
638
639
    // 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;

640
    // best quality defaults
John Koleszar's avatar
John Koleszar committed
641
642
643
644
645
646
647
648
649
650
    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;
651
    sf->use_fastquant_for_pick = 0;
652
    sf->no_skip_block4x4_search = 1;
John Koleszar's avatar
John Koleszar committed
653
654
655

    sf->first_step = 0;
    sf->max_step_search_steps = MAX_MVSEARCH_STEPS;
656
    sf->improved_mv_pred = 1;
John Koleszar's avatar
John Koleszar committed
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679

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

    switch (Mode)
    {
    case 0: // best quality mode
        sf->thresh_mult[THR_ZEROMV   ] = 0;
        sf->thresh_mult[THR_ZEROG    ] = 0;
        sf->thresh_mult[THR_ZEROA    ] = 0;
        sf->thresh_mult[THR_NEARESTMV] = 0;
        sf->thresh_mult[THR_NEARESTG ] = 0;
        sf->thresh_mult[THR_NEARESTA ] = 0;
        sf->thresh_mult[THR_NEARMV   ] = 0;
        sf->thresh_mult[THR_NEARG    ] = 0;
        sf->thresh_mult[THR_NEARA    ] = 0;

        sf->thresh_mult[THR_DC       ] = 0;

        sf->thresh_mult[THR_V_PRED   ] = 1000;
        sf->thresh_mult[THR_H_PRED   ] = 1000;
        sf->thresh_mult[THR_B_PRED   ] = 2000;
680
        sf->thresh_mult[THR_I8X8_PRED] = 2000;
John Koleszar's avatar
John Koleszar committed
681
682
683
684
685
686
687
688
689
690
        sf->thresh_mult[THR_TM       ] = 1000;

        sf->thresh_mult[THR_NEWMV    ] = 1000;
        sf->thresh_mult[THR_NEWG     ] = 1000;
        sf->thresh_mult[THR_NEWA     ] = 1000;

        sf->thresh_mult[THR_SPLITMV  ] = 2500;
        sf->thresh_mult[THR_SPLITG   ] = 5000;
        sf->thresh_mult[THR_SPLITA   ] = 5000;

691
692
693
694
695
696
697
698
699
700
701
702
703
        sf->thresh_mult[THR_DUAL_ZEROLG   ] = 0;
        sf->thresh_mult[THR_DUAL_NEARESTLG] = 0;
        sf->thresh_mult[THR_DUAL_NEARLG   ] = 0;
        sf->thresh_mult[THR_DUAL_ZEROLA   ] = 0;
        sf->thresh_mult[THR_DUAL_NEARESTLA] = 0;
        sf->thresh_mult[THR_DUAL_NEARLA   ] = 0;
        sf->thresh_mult[THR_DUAL_ZEROGA   ] = 0;
        sf->thresh_mult[THR_DUAL_NEARESTGA] = 0;
        sf->thresh_mult[THR_DUAL_NEARGA   ] = 0;

        sf->thresh_mult[THR_DUAL_NEWLG    ] = 1000;
        sf->thresh_mult[THR_DUAL_NEWLA    ] = 1000;
        sf->thresh_mult[THR_DUAL_NEWGA    ] = 1000;
John Koleszar's avatar
John Koleszar committed
704
705
706
707
708
709
710
711
712
713
714
715

        sf->first_step = 0;
        sf->max_step_search_steps = MAX_MVSEARCH_STEPS;
        break;
    case 1:
        sf->thresh_mult[THR_NEARESTMV] = 0;
        sf->thresh_mult[THR_ZEROMV   ] = 0;
        sf->thresh_mult[THR_DC       ] = 0;
        sf->thresh_mult[THR_NEARMV   ] = 0;
        sf->thresh_mult[THR_V_PRED   ] = 1000;
        sf->thresh_mult[THR_H_PRED   ] = 1000;
        sf->thresh_mult[THR_B_PRED   ] = 2500;
716
        sf->thresh_mult[THR_I8X8_PRED] = 2500;
John Koleszar's avatar
John Koleszar committed
717
718
719
720
721
722
723
724
725
726
        sf->thresh_mult[THR_TM       ] = 1000;

        sf->thresh_mult[THR_NEARESTG ] = 1000;
        sf->thresh_mult[THR_NEARESTA ] = 1000;

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

727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
        sf->thresh_mult[THR_ZEROMV   ] = 0;
        sf->thresh_mult[THR_ZEROG    ] = 0;
        sf->thresh_mult[THR_ZEROA    ] = 0;
        sf->thresh_mult[THR_NEARESTMV] = 0;
        sf->thresh_mult[THR_NEARESTG ] = 0;
        sf->thresh_mult[THR_NEARESTA ] = 0;
        sf->thresh_mult[THR_NEARMV   ] = 0;
        sf->thresh_mult[THR_NEARG    ] = 0;
        sf->thresh_mult[THR_NEARA    ] = 0;

        sf->thresh_mult[THR_NEWMV    ] = 1000;
        sf->thresh_mult[THR_NEWG     ] = 1000;
        sf->thresh_mult[THR_NEWA     ] = 1000;

        sf->thresh_mult[THR_SPLITMV  ] = 1700;
        sf->thresh_mult[THR_SPLITG   ] = 4500;
        sf->thresh_mult[THR_SPLITA   ] = 4500;
744
745
746
747
748
749
750
751
752
753
754
755
756
757

        sf->thresh_mult[THR_DUAL_ZEROLG   ] = 0;
        sf->thresh_mult[THR_DUAL_NEARESTLG] = 0;
        sf->thresh_mult[THR_DUAL_NEARLG   ] = 0;
        sf->thresh_mult[THR_DUAL_ZEROLA   ] = 0;
        sf->thresh_mult[THR_DUAL_NEARESTLA] = 0;
        sf->thresh_mult[THR_DUAL_NEARLA   ] = 0;
        sf->thresh_mult[THR_DUAL_ZEROGA   ] = 0;
        sf->thresh_mult[THR_DUAL_NEARESTGA] = 0;
        sf->thresh_mult[THR_DUAL_NEARGA   ] = 0;

        sf->thresh_mult[THR_DUAL_NEWLG    ] = 1000;
        sf->thresh_mult[THR_DUAL_NEWLA    ] = 1000;
        sf->thresh_mult[THR_DUAL_NEWGA    ] = 1000;
John Koleszar's avatar
John Koleszar committed
758

759
        if (Speed > 0)
John Koleszar's avatar
John Koleszar committed
760
        {
761
762
763
764
            /* Disable coefficient optimization above speed 0 */
            sf->optimize_coefficients = 0;
            sf->use_fastquant_for_pick = 1;
            sf->no_skip_block4x4_search = 0;
765

766
            sf->first_step = 1;
767

768
769
770
            cpi->mode_check_freq[THR_SPLITG] = 2;
            cpi->mode_check_freq[THR_SPLITA] = 2;
            cpi->mode_check_freq[THR_SPLITMV] = 0;
John Koleszar's avatar
John Koleszar committed
771
772
        }

773
        if (Speed > 1)
John Koleszar's avatar
John Koleszar committed
774
775
776
        {
            cpi->mode_check_freq[THR_SPLITG] = 4;
            cpi->mode_check_freq[THR_SPLITA] = 4;
777
            cpi->mode_check_freq[THR_SPLITMV] = 2;
John Koleszar's avatar
John Koleszar committed
778
779
780
781
782

            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;
783
            sf->thresh_mult[THR_I8X8_PRED] = 5000;
Yaowu Xu's avatar
Yaowu Xu committed
784

John Koleszar's avatar
John Koleszar committed
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
            if (cpi->ref_frame_flags & VP8_LAST_FLAG)
            {
                sf->thresh_mult[THR_NEWMV    ] = 2000;
                sf->thresh_mult[THR_SPLITMV  ] = 10000;
            }

            if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
            {
                sf->thresh_mult[THR_NEARESTG ] = 1500;
                sf->thresh_mult[THR_ZEROG    ] = 1500;
                sf->thresh_mult[THR_NEARG    ] = 1500;
                sf->thresh_mult[THR_NEWG     ] = 2000;
                sf->thresh_mult[THR_SPLITG   ] = 20000;
            }

            if (cpi->ref_frame_flags & VP8_ALT_FLAG)
            {
                sf->thresh_mult[THR_NEARESTA ] = 1500;
                sf->thresh_mult[THR_ZEROA    ] = 1500;
                sf->thresh_mult[THR_NEARA    ] = 1500;
                sf->thresh_mult[THR_NEWA     ] = 2000;
                sf->thresh_mult[THR_SPLITA   ] = 20000;
            }
808
809
810
811
812
813
814
815
816
817
818
819
820
821

            sf->thresh_mult[THR_DUAL_ZEROLG   ] = 1500;
            sf->thresh_mult[THR_DUAL_NEARESTLG] = 1500;
            sf->thresh_mult[THR_DUAL_NEARLG   ] = 1500;
            sf->thresh_mult[THR_DUAL_ZEROLA   ] = 1500;
            sf->thresh_mult[THR_DUAL_NEARESTLA] = 1500;
            sf->thresh_mult[THR_DUAL_NEARLA   ] = 1500;
            sf->thresh_mult[THR_DUAL_ZEROGA   ] = 1500;
            sf->thresh_mult[THR_DUAL_NEARESTGA] = 1500;
            sf->thresh_mult[THR_DUAL_NEARGA   ] = 1500;

            sf->thresh_mult[THR_DUAL_NEWLG    ] = 2000;
            sf->thresh_mult[THR_DUAL_NEWLA    ] = 2000;
            sf->thresh_mult[THR_DUAL_NEWGA    ] = 2000;
John Koleszar's avatar
John Koleszar committed
822
823
        }

824
        if (Speed > 2)
John Koleszar's avatar
John Koleszar committed
825
826
827
828
829
830
831
832
833
        {
            cpi->mode_check_freq[THR_SPLITG] = 15;
            cpi->mode_check_freq[THR_SPLITA] = 15;
            cpi->mode_check_freq[THR_SPLITMV] = 7;

            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;
834
            sf->thresh_mult[THR_I8X8_PRED] = 7500;
John Koleszar's avatar
John Koleszar committed
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859

            if (cpi->ref_frame_flags & VP8_LAST_FLAG)
            {
                sf->thresh_mult[THR_NEWMV    ] = 2000;
                sf->thresh_mult[THR_SPLITMV  ] = 25000;
            }

            if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
            {
                sf->thresh_mult[THR_NEARESTG ] = 2000;
                sf->thresh_mult[THR_ZEROG    ] = 2000;
                sf->thresh_mult[THR_NEARG    ] = 2000;
                sf->thresh_mult[THR_NEWG     ] = 2500;
                sf->thresh_mult[THR_SPLITG   ] = 50000;
            }

            if (cpi->ref_frame_flags & VP8_ALT_FLAG)
            {
                sf->thresh_mult[THR_NEARESTA ] = 2000;
                sf->thresh_mult[THR_ZEROA    ] = 2000;
                sf->thresh_mult[THR_NEARA    ] = 2000;
                sf->thresh_mult[THR_NEWA     ] = 2500;
                sf->thresh_mult[THR_SPLITA   ] = 50000;
            }

860
861
862
863
864
865
866
867
868
869
870
871
872
873
            sf->thresh_mult[THR_DUAL_ZEROLG   ] = 2000;
            sf->thresh_mult[THR_DUAL_NEARESTLG] = 2000;
            sf->thresh_mult[THR_DUAL_NEARLG   ] = 2000;
            sf->thresh_mult[THR_DUAL_ZEROLA   ] = 2000;
            sf->thresh_mult[THR_DUAL_NEARESTLA] = 2000;
            sf->thresh_mult[THR_DUAL_NEARLA   ] = 2000;
            sf->thresh_mult[THR_DUAL_ZEROGA   ] = 2000;
            sf->thresh_mult[THR_DUAL_NEARESTGA] = 2000;
            sf->thresh_mult[THR_DUAL_NEARGA   ] = 2000;

            sf->thresh_mult[THR_DUAL_NEWLG    ] = 2500;
            sf->thresh_mult[THR_DUAL_NEWLA    ] = 2500;
            sf->thresh_mult[THR_DUAL_NEWGA    ] = 2500;

874
875
876
            sf->improved_quant = 0;
            sf->improved_dct = 0;

877
878
            // Only do recode loop on key frames, golden frames and
            // alt ref frames
John Koleszar's avatar
John Koleszar committed
879
880
881
882
883
884
            sf->recode_loop = 2;

        }

        break;

885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
    }; /* switch */

    /* disable frame modes if flags not set */
    if (!(cpi->ref_frame_flags & VP8_LAST_FLAG))
    {
        sf->thresh_mult[THR_NEWMV    ] = INT_MAX;
        sf->thresh_mult[THR_NEARESTMV] = INT_MAX;
        sf->thresh_mult[THR_ZEROMV   ] = INT_MAX;
        sf->thresh_mult[THR_NEARMV   ] = INT_MAX;
        sf->thresh_mult[THR_SPLITMV  ] = INT_MAX;
    }

    if (!(cpi->ref_frame_flags & VP8_GOLD_FLAG))
    {
        sf->thresh_mult[THR_NEARESTG ] = INT_MAX;
        sf->thresh_mult[THR_ZEROG    ] = INT_MAX;
        sf->thresh_mult[THR_NEARG    ] = INT_MAX;
        sf->thresh_mult[THR_NEWG     ] = INT_MAX;
        sf->thresh_mult[THR_SPLITG   ] = INT_MAX;
    }

    if (!(cpi->ref_frame_flags & VP8_ALT_FLAG))
    {
        sf->thresh_mult[THR_NEARESTA ] = INT_MAX;
        sf->thresh_mult[THR_ZEROA    ] = INT_MAX;
        sf->thresh_mult[THR_NEARA    ] = INT_MAX;
        sf->thresh_mult[THR_NEWA     ] = INT_MAX;
        sf->thresh_mult[THR_SPLITA   ] = INT_MAX;
    }

915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
    if ((cpi->ref_frame_flags & (VP8_LAST_FLAG | VP8_GOLD_FLAG)) != (VP8_LAST_FLAG | VP8_GOLD_FLAG))
    {
        sf->thresh_mult[THR_DUAL_ZEROLG   ] = INT_MAX;
        sf->thresh_mult[THR_DUAL_NEARESTLG] = INT_MAX;
        sf->thresh_mult[THR_DUAL_NEARLG   ] = INT_MAX;
        sf->thresh_mult[THR_DUAL_NEWLG    ] = INT_MAX;
    }

    if ((cpi->ref_frame_flags & (VP8_LAST_FLAG | VP8_ALT_FLAG)) != (VP8_LAST_FLAG | VP8_ALT_FLAG))
    {
        sf->thresh_mult[THR_DUAL_ZEROLA   ] = INT_MAX;
        sf->thresh_mult[THR_DUAL_NEARESTLA] = INT_MAX;
        sf->thresh_mult[THR_DUAL_NEARLA   ] = INT_MAX;
        sf->thresh_mult[THR_DUAL_NEWLA    ] = INT_MAX;
    }

    if ((cpi->ref_frame_flags & (VP8_GOLD_FLAG | VP8_ALT_FLAG)) != (VP8_GOLD_FLAG | VP8_ALT_FLAG))
    {
        sf->thresh_mult[THR_DUAL_ZEROGA   ] = INT_MAX;
        sf->thresh_mult[THR_DUAL_NEARESTGA] = INT_MAX;
        sf->thresh_mult[THR_DUAL_NEARGA   ] = INT_MAX;
        sf->thresh_mult[THR_DUAL_NEWGA    ] = INT_MAX;
    }
John Koleszar's avatar
John Koleszar committed
938

939
940
941
942
943
944
945
946
947
    // 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
948
949
    if (cpi->sf.search_method == NSTEP)
    {
950
        vp8_init3smotion_compensation(&cpi->mb, cm->yv12_fb[cm->lst_fb_idx].y_stride);
John Koleszar's avatar
John Koleszar committed
951
952
953
    }
    else if (cpi->sf.search_method == DIAMOND)
    {
954
        vp8_init_dsmotion_compensation(&cpi->mb, cm->yv12_fb[cm->lst_fb_idx].y_stride);
John Koleszar's avatar
John Koleszar committed
955
956
957
958
    }

    if (cpi->sf.improved_dct)
    {
959
960
961
#if CONFIG_T8X8
        cpi->mb.vp8_short_fdct8x8 = FDCT_INVOKE(&cpi->rtcd.fdct, short8x8);
#endif
John Koleszar's avatar
John Koleszar committed
962
963
964
965
966
        cpi->mb.vp8_short_fdct8x4 = FDCT_INVOKE(&cpi->rtcd.fdct, short8x4);
        cpi->mb.vp8_short_fdct4x4 = FDCT_INVOKE(&cpi->rtcd.fdct, short4x4);
    }
    else
    {
967
968
969
#if CONFIG_T8X8
        cpi->mb.vp8_short_fdct8x8 = FDCT_INVOKE(&cpi->rtcd.fdct, short8x8);
#endif
John Koleszar's avatar
John Koleszar committed
970
971
972
973
974
        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);
975
976
977
#if CONFIG_T8X8
    cpi->mb.short_fhaar2x2 = FDCT_INVOKE(&cpi->rtcd.fdct, haar_short2x2);
#endif
John Koleszar's avatar
John Koleszar committed
978
979
980

    if (cpi->sf.improved_quant)
    {
981
982
983
984
        cpi->mb.quantize_b      = QUANTIZE_INVOKE(&cpi->rtcd.quantize,
                                                  quantb);
        cpi->mb.quantize_b_pair = QUANTIZE_INVOKE(&cpi->rtcd.quantize,
                                                  quantb_pair);
985
986
987
988
#if CONFIG_T8X8
        cpi->mb.quantize_b_8x8  = QUANTIZE_INVOKE(&cpi->rtcd.quantize, quantb_8x8);
        cpi->mb.quantize_b_2x2  = QUANTIZE_INVOKE(&cpi->rtcd.quantize, quantb_2x2);
#endif
John Koleszar's avatar
John Koleszar committed
989
990
991
    }
    else
    {
992
993
994
995
        cpi->mb.quantize_b      = QUANTIZE_INVOKE(&cpi->rtcd.quantize,
                                                  fastquantb);
        cpi->mb.quantize_b_pair = QUANTIZE_INVOKE(&cpi->rtcd.quantize,
                                                  fastquantb_pair);
996
997
998
999
#if CONFIG_T8X8
        cpi->mb.quantize_b_8x8  = QUANTIZE_INVOKE(&cpi->rtcd.quantize, fastquantb_8x8);
        cpi->mb.quantize_b_2x2  = QUANTIZE_INVOKE(&cpi->rtcd.quantize, fastquantb_2x2);
#endif
John Koleszar's avatar
John Koleszar committed
1000
    }
1001
1002
    if (cpi->sf.improved_quant != last_improved_quant)
        vp8cx_init_quantizer(cpi);
John Koleszar's avatar
John Koleszar committed
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020

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

1021
    if (cpi->sf.optimize_coefficients == 1 && cpi->pass!=1)
Yaowu Xu's avatar
Yaowu Xu committed
1022
        cpi->mb.optimize = 1;
John Koleszar's avatar
John Koleszar committed
1023
1024
1025
1026
1027
1028
1029
1030
1031
    else
        cpi->mb.optimize = 0;

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

John Koleszar's avatar
John Koleszar committed
1035
1036
1037
1038
1039
    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
1040
1041
1042

#if VP8_TEMPORAL_ALT_REF

John Koleszar's avatar
John Koleszar committed
1043
    if (vp8_yv12_alloc_frame_buffer(&cpi->alt_ref_buffer,
1044
                                    width, height, VP8BORDERINPIXELS))
John Koleszar's avatar
John Koleszar committed
1045
1046
1047
1048
1049
        vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
                           "Failed to allocate altref buffer");

#endif
}
1050
1051
1052

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

1055
1056
1057
1058
    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
1059
        return 1;
1060
1061
1062
1063
1064
1065

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

    return 0;
}

John Koleszar's avatar
John Koleszar committed
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
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");

1077
1078
1079
1080
1081
    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
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
    if ((width & 0xf) != 0)
        width += 16 - (width & 0xf);

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


    if (vp8_yv12_alloc_frame_buffer(&cpi->last_frame_uf,
                                    width, height, VP8BORDERINPIXELS))
        vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
                           "Failed to allocate last frame buffer");

1094
1095
    if (vp8_yv12_alloc_frame_buffer(&cpi->scaled_source,
                                    width, height, VP8BORDERINPIXELS))
John Koleszar's avatar
John Koleszar committed
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
        vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
                           "Failed to allocate scaled source buffer");


        vpx_free(cpi->tok);

    {
        unsigned int tokens = cm->mb_rows * cm->mb_cols * 24 * 16;

        CHECK_MEM_ERROR(cpi->tok, vpx_calloc(tokens, sizeof(*cpi->tok)));
    }

    // Data used for real time vc mode to see if gf needs refreshing
    cpi->inter_zz_count = 0;
    cpi->gf_bad_count = 0;
    cpi->gf_update_recommended = 0;
1112
1113
1114


    // Structures used to minitor GF usage
1115
1116
1117
1118
    vpx_free(cpi->gf_active_flags);
    CHECK_MEM_ERROR(cpi->gf_active_flags,
                    vpx_calloc(1, cm->mb_rows * cm->mb_cols));
    cpi->gf_active_count = cm->mb_rows * cm->mb_cols;
1119

1120
1121
1122
1123
    vpx_free(cpi->mb_activity_map);
    CHECK_MEM_ERROR(cpi->mb_activity_map,
                    vpx_calloc(sizeof(unsigned int),
                    cm->mb_rows * cm->mb_cols));
1124

1125
1126
1127
1128
    vpx_free(cpi->mb_norm_activity_map);
    CHECK_MEM_ERROR(cpi->mb_norm_activity_map,
                    vpx_calloc(sizeof(unsigned int),
                    cm->mb_rows * cm->mb_cols));
1129

1130
        vpx_free(cpi->twopass.total_stats);
1131

1132
    cpi->twopass.total_stats = vpx_calloc(1, sizeof(FIRSTPASS_STATS));
1133

1134
1135
1136
    vpx_free(cpi->twopass.total_left_stats);
    cpi->twopass.total_left_stats = vpx_calloc(1, sizeof(FIRSTPASS_STATS));

1137
        vpx_free(cpi->twopass.this_frame_stats);
1138

1139
    cpi->twopass.this_frame_stats = vpx_calloc(1, sizeof(FIRSTPASS_STATS));
1140

1141
1142
1143
    if( !cpi->twopass.total_stats ||
        !cpi->twopass.total_left_stats ||
        !cpi->twopass.this_frame_stats)
1144
1145
        vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
                           "Failed to allocate firstpass stats");
Attila Nagy's avatar
Attila Nagy committed
1146

James Berry's avatar
James Berry committed
1147
1148
        vpx_free(cpi->tplist);

1149
    CHECK_MEM_ERROR(cpi->tplist, vpx_malloc(sizeof(TOKENLIST) * cpi->common.mb_rows));
John Koleszar's avatar
John Koleszar committed
1150
1151
1152
}


Paul Wilkins's avatar
Paul Wilkins committed
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
// TODO perhaps change number of steps expose to outside world when setting
// max and min limits. Also this will likely want refining for the extended Q
// range.
//
// Table that converts 0-63 Q range values passed in outside to the Qindex
// range used internally.
static const int q_trans[] =
{
     0,    4,   8,  12,  16,  20,  24,  28,
    32,   36,  40,  44,  48,  52,  56,  60,
    64,   68,  72,  76,  80,  84,  88,  92,
    96,  100, 104, 108, 112, 116, 120, 124,
    128, 132, 136, 140, 144, 148, 152, 156,
    160, 164, 168, 172, 176, 180, 184, 188,
    192, 196, 200, 204, 208, 212, 216, 220,
    224, 228, 232, 236, 240, 244, 249, 255,
};
John Koleszar's avatar
John Koleszar committed
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182

int vp8_reverse_trans(int x)
{
    int i;

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

    return 63;
};
void vp8_new_frame_rate(VP8_COMP *cpi, double framerate)
{
1183
1184
1185
    if(framerate < .1)
        framerate = 30;

John Koleszar's avatar
John Koleszar committed
1186
1187
1188
1189
1190
1191
    cpi->oxcf.frame_rate             = framerate;
    cpi->output_frame_rate            = cpi->oxcf.frame_rate;
    cpi->per_frame_bandwidth          = (int)(cpi->oxcf.target_bandwidth / cpi->output_frame_rate);
    cpi->av_per_frame_bandwidth        = (int)(cpi->oxcf.target_bandwidth / cpi->output_frame_rate);
    cpi->min_frame_bandwidth          = (int)(cpi->av_per_frame_bandwidth * cpi->oxcf.two_pass_vbrmin_section / 100);

1192
    // Set Maximum gf/arf interval
1193
    cpi->max_gf_interval = ((int)(cpi->output_frame_rate / 2.0) + 2);
John Koleszar's avatar
John Koleszar committed
1194

1195
    if(cpi->max_gf_interval < 12)
John Koleszar's avatar
John Koleszar committed
1196
1197
        cpi->max_gf_interval = 12;

1198
    // Extended interval for genuinely static scenes
1199
    cpi->twopass.static_scene_max_gf_interval = cpi->key_frame_frequency >> 1;
John Koleszar's avatar
John Koleszar committed
1200

1201
     // Special conditions when altr ref frame enabled in lagged compress mode
1202
    if (cpi->oxcf.play_alternate && cpi->oxcf.lag_in_frames)
John Koleszar's avatar
John Koleszar committed
1203
1204
1205
    {
        if (cpi->max_gf_interval > cpi->oxcf.lag_in_frames - 1)
            cpi->max_gf_interval = cpi->oxcf.lag_in_frames - 1;
1206

1207
1208
        if (cpi->twopass.static_scene_max_gf_interval > cpi->oxcf.lag_in_frames - 1)
            cpi->twopass.static_scene_max_gf_interval = cpi->oxcf.lag_in_frames - 1;
John Koleszar's avatar
John Koleszar committed
1209
    }
1210

1211
1212
    if ( cpi->max_gf_interval > cpi->twopass.static_scene_max_gf_interval )
        cpi->max_gf_interval = cpi->twopass.static_scene_max_gf_interval;
John Koleszar's avatar
John Koleszar committed
1213
1214
}

1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226

static int
rescale(int val, int num, int denom)
{
    int64_t llnum = num;
    int64_t llden = denom;
    int64_t llval = val;

    return llval * llnum / llden;
}


1227
static void init_config(VP8_PTR ptr, VP8_CONFIG *oxcf)
John Koleszar's avatar
John Koleszar committed
1228
1229
1230
1231
{
    VP8_COMP *cpi = (VP8_COMP *)(ptr);
    VP8_COMMON *cm = &cpi->common;

Paul Wilkins's avatar
Paul Wilkins committed
1232
    cpi->oxcf = *oxcf;
John Koleszar's avatar
John Koleszar committed
1233
1234
1235
1236
1237
1238

    cpi->goldfreq = 7;

    cm->version = oxcf->Version;
    vp8_setup_version(cm);

Paul Wilkins's avatar
Paul Wilkins committed
1239
1240
    // change includes all joint functionality
    vp8_change_config(ptr, oxcf);
John Koleszar's avatar
John Koleszar committed
1241

Paul Wilkins's avatar
Paul Wilkins committed
1242
1243
1244
1245
    // Initialize active best and worst q and average q values.
    cpi->active_worst_quality         = cpi->oxcf.worst_allowed_q;
    cpi->active_best_quality          = cpi->oxcf.best_allowed_q;
    cpi->avg_frame_qindex             = cpi->oxcf.worst_allowed_q;
John Koleszar's avatar
John Koleszar committed
1246

Paul Wilkins's avatar
Paul Wilkins committed
1247
    // Initialise the starting buffer levels
1248
    cpi->buffer_level                 = cpi->oxcf.starting_buffer_level;
John Koleszar's avatar
John Koleszar committed
1249
1250
    cpi->bits_off_target              = cpi->oxcf.starting_buffer_level;

1251
1252
    cpi->rolling_target_bits          = cpi->av_per_frame_bandwidth;
    cpi->rolling_actual_bits          = cpi->av_per_frame_bandwidth;
1253
1254
    cpi->long_rolling_target_bits     = cpi->av_per_frame_bandwidth;
    cpi->long_rolling_actual_bits     = cpi->av_per_frame_bandwidth;
John Koleszar's avatar
John Koleszar committed
1255
1256

    cpi->total_actual_bits            = 0;
1257
    cpi->total_target_vs_actual       = 0;
John Koleszar's avatar
John Koleszar committed
1258

1259
1260
    cpi->static_mb_pct = 0;

John Koleszar's avatar
John Koleszar committed
1261
1262
1263
1264
1265
1266
#if VP8_TEMPORAL_ALT_REF
    {
        int i;

        cpi->fixed_divide[0] = 0;

1267
1268
        for (i = 1; i < 512; i++)
            cpi->fixed_divide[i] = 0x80000 / i;
John Koleszar's avatar
John Koleszar committed
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
    }
#endif
}


void vp8_change_config(VP8_PTR ptr, VP8_CONFIG *oxcf)
{
    VP8_COMP *cpi = (VP8_COMP *)(ptr);
    VP8_COMMON *cm = &cpi->common;

    if (!cpi)
        return;

    if (!oxcf)
        return;

    if (cm->version != oxcf->Version)
    {
        cm->version = oxcf->Version;
        vp8_setup_version(cm);
    }

    cpi->oxcf = *oxcf;

    switch (cpi->oxcf.Mode)
    {
Paul Wilkins's avatar
Paul Wilkins committed
1295
    // Real time and one pass deprecated in test code base
John Koleszar's avatar
John Koleszar committed
1296
1297
1298
1299
    case MODE_FIRSTPASS:
        cpi->pass = 1;
        cpi->compressor_speed = 1;
        break;
Paul Wilkins's avatar
Paul Wilkins committed
1300

John Koleszar's avatar
John Koleszar committed
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
    case MODE_SECONDPASS:
        cpi->pass = 2;
        cpi->compressor_speed = 1;

        if (cpi->oxcf.cpu_used < -5)
        {
            cpi->oxcf.cpu_used = -5;
        }

        if (cpi->oxcf.cpu_used > 5)
            cpi->oxcf.cpu_used = 5;

        break;
Paul Wilkins's avatar
Paul Wilkins committed
1314

John Koleszar's avatar
John Koleszar committed
1315
1316
1317
1318
1319
1320
1321
1322
    case MODE_SECONDPASS_BEST:
        cpi->pass = 2;
        cpi->compressor_speed = 0;
        break;
    }

    cpi->oxcf.worst_allowed_q = q_trans[oxcf->worst_allowed_q];
    cpi->oxcf.best_allowed_q = q_trans[oxcf->best_allowed_q];
1323
    cpi->oxcf.cq_level = q_trans[cpi->oxcf.cq_level];
John Koleszar's avatar
John Koleszar committed
1324

1325
    cpi->baseline_gf_interval = DEFAULT_GF_INTERVAL;
John Koleszar's avatar
John Koleszar committed
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335

    cpi->ref_frame_flags = VP8_ALT_FLAG | VP8_GOLD_FLAG | VP8_LAST_FLAG;

    //cpi->use_golden_frame_only = 0;
    //cpi->use_last_frame_only = 0;
    cm->refresh_golden_frame = 0;
    cm->refresh_last_frame = 1;
    cm->refresh_entropy_probs = 1;

    if (cpi->oxcf.token_partitions >= 0 && cpi->oxcf.token_partitions <= 3)