rdopt.c 124 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
13
14
15
 */


#include <stdio.h>
#include <math.h>
#include <limits.h>
#include <assert.h>
John Koleszar's avatar
John Koleszar committed
16
#include "vp8/common/pragmas.h"
John Koleszar's avatar
John Koleszar committed
17
18
19
20
21
22

#include "tokenize.h"
#include "treewriter.h"
#include "onyx_int.h"
#include "modecosts.h"
#include "encodeintra.h"
John Koleszar's avatar
John Koleszar committed
23
24
25
26
27
#include "vp8/common/entropymode.h"
#include "vp8/common/reconinter.h"
#include "vp8/common/reconintra.h"
#include "vp8/common/reconintra4x4.h"
#include "vp8/common/findnearmv.h"
Christian Duvivier's avatar
Christian Duvivier committed
28
#include "vp8/common/quant_common.h"
John Koleszar's avatar
John Koleszar committed
29
30
#include "encodemb.h"
#include "quantize.h"
John Koleszar's avatar
John Koleszar committed
31
32
#include "vp8/common/idct.h"
#include "vp8/common/g_common.h"
John Koleszar's avatar
John Koleszar committed
33
34
#include "variance.h"
#include "mcomp.h"
Yunqing Wang's avatar
Yunqing Wang committed
35
#include "rdopt.h"
Paul Wilkins's avatar
Paul Wilkins committed
36
#include "ratectrl.h"
John Koleszar's avatar
John Koleszar committed
37
38
#include "vpx_mem/vpx_mem.h"
#include "dct.h"
John Koleszar's avatar
John Koleszar committed
39
#include "vp8/common/systemdependent.h"
John Koleszar's avatar
John Koleszar committed
40

41
#include "vp8/common/seg_common.h"
42
#include "vp8/common/pred_common.h"
43

John Koleszar's avatar
John Koleszar committed
44
45
46
47
48
49
#if CONFIG_RUNTIME_CPU_DETECT
#define IF_RTCD(x)  (x)
#else
#define IF_RTCD(x)  NULL
#endif

Scott LaVarnway's avatar
Scott LaVarnway committed
50
51
52
extern void vp8cx_mb_init_quantizer(VP8_COMP *cpi, MACROBLOCK *x);
extern void vp8_update_zbin_extra(VP8_COMP *cpi, MACROBLOCK *x);

53
54
55
56
57
58
#if CONFIG_HIGH_PRECISION_MV
#define XMVCOST (x->e_mbd.allow_high_precision_mv?x->mvcost_hp:x->mvcost)
#else
#define XMVCOST (x->mvcost)
#endif

John Koleszar's avatar
John Koleszar committed
59
60
#define MAXF(a,b)            (((a) > (b)) ? (a) : (b))

61
static const int auto_speed_thresh[17] =
John Koleszar's avatar
John Koleszar committed
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
{
    1000,
    200,
    150,
    130,
    150,
    125,
    120,
    115,
    115,
    115,
    115,
    115,
    115,
    115,
    115,
    115,
    105
};

const MB_PREDICTION_MODE vp8_mode_order[MAX_MODES] =
{
    ZEROMV,
    DC_PRED,

    NEARESTMV,
    NEARMV,

    ZEROMV,
    NEARESTMV,

    ZEROMV,
    NEARESTMV,

    NEARMV,
    NEARMV,

    V_PRED,
    H_PRED,
    TM_PRED,

    NEWMV,
    NEWMV,
    NEWMV,

    SPLITMV,
    SPLITMV,
    SPLITMV,

    B_PRED,
112
    I8X8_PRED,
113

114
    /* compound prediction modes */
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
    ZEROMV,
    NEARESTMV,
    NEARMV,

    ZEROMV,
    NEARESTMV,
    NEARMV,

    ZEROMV,
    NEARESTMV,
    NEARMV,

    NEWMV,
    NEWMV,
    NEWMV,
John Koleszar's avatar
John Koleszar committed
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
};

const MV_REFERENCE_FRAME vp8_ref_frame_order[MAX_MODES] =
{
    LAST_FRAME,
    INTRA_FRAME,

    LAST_FRAME,
    LAST_FRAME,

    GOLDEN_FRAME,
    GOLDEN_FRAME,

    ALTREF_FRAME,
    ALTREF_FRAME,

    GOLDEN_FRAME,
    ALTREF_FRAME,

    INTRA_FRAME,
    INTRA_FRAME,
    INTRA_FRAME,

    LAST_FRAME,
    GOLDEN_FRAME,
    ALTREF_FRAME,

    LAST_FRAME,
    GOLDEN_FRAME,
    ALTREF_FRAME,

    INTRA_FRAME,
162
    INTRA_FRAME,
163

164
    /* compound prediction modes */
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
    LAST_FRAME,
    LAST_FRAME,
    LAST_FRAME,

    ALTREF_FRAME,
    ALTREF_FRAME,
    ALTREF_FRAME,

    GOLDEN_FRAME,
    GOLDEN_FRAME,
    GOLDEN_FRAME,

    LAST_FRAME,
    ALTREF_FRAME,
    GOLDEN_FRAME,
};

const MV_REFERENCE_FRAME vp8_second_ref_frame_order[MAX_MODES] =
{
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0,

188
    /* compound prediction modes */
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
    GOLDEN_FRAME,
    GOLDEN_FRAME,
    GOLDEN_FRAME,

    LAST_FRAME,
    LAST_FRAME,
    LAST_FRAME,

    ALTREF_FRAME,
    ALTREF_FRAME,
    ALTREF_FRAME,

    GOLDEN_FRAME,
    LAST_FRAME,
    ALTREF_FRAME,
John Koleszar's avatar
John Koleszar committed
204
205
206
};

static void fill_token_costs(
207
208
    unsigned int c      [BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [MAX_ENTROPY_TOKENS],
    const vp8_prob p    [BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [ENTROPY_NODES]
John Koleszar's avatar
John Koleszar committed
209
210
211
212
213
214
215
216
)
{
    int i, j, k;


    for (i = 0; i < BLOCK_TYPES; i++)
        for (j = 0; j < COEF_BANDS; j++)
            for (k = 0; k < PREV_COEF_CONTEXTS; k++)
217
218
219
220
221
222
223
224
            {

                if(k == 0 && ((j > 0 && i > 0) || (j > 1 && i == 0)))
                    vp8_cost_tokens_skip((int *)(c [i][j][k]), p [i][j][k], vp8_coef_tree);
                else

                    vp8_cost_tokens((int *)(c [i][j][k]), p [i][j][k], vp8_coef_tree);
            }
John Koleszar's avatar
John Koleszar committed
225
226
227

}

Paul Wilkins's avatar
Paul Wilkins committed
228
static int rd_iifactor [ 32 ] =  {    4,   4,   3,   2,   1,   0,   0,   0,
John Koleszar's avatar
John Koleszar committed
229
230
231
232
233
                                      0,   0,   0,   0,   0,   0,   0,   0,
                                      0,   0,   0,   0,   0,   0,   0,   0,
                                      0,   0,   0,   0,   0,   0,   0,   0,
                                 };

234
// 3* dc_qlookup[Q]*dc_qlookup[Q];
235

236
/* values are now correlated to quantizer */
Paul Wilkins's avatar
Paul Wilkins committed
237
238
239
240
static int sad_per_bit16lut[QINDEX_RANGE];
static int sad_per_bit4lut[QINDEX_RANGE];

void vp8_init_me_luts()
John Koleszar's avatar
John Koleszar committed
241
{
Paul Wilkins's avatar
Paul Wilkins committed
242
243
244
245
246
247
248
249
250
251
252
253
    int i;

    // Initialize the sad lut tables using a formulaic calculation for now
    // This is to make it easier to resolve the impact of experimental changes
    // to the quantizer tables.
    for ( i = 0; i < QINDEX_RANGE; i++ )
    {
        sad_per_bit16lut[i] =
            (int)((0.0418*vp8_convert_qindex_to_q(i)) + 2.4107);
        sad_per_bit4lut[i] = (int)((0.063*vp8_convert_qindex_to_q(i)) + 2.742);
    }
}
John Koleszar's avatar
John Koleszar committed
254

255
256
257
258
259
int compute_rd_mult( int qindex )
{
    int q;

    q = vp8_dc_quant(qindex,0);
260
    return (11 * q * q) >> 6;
261
262
}

John Koleszar's avatar
John Koleszar committed
263
264
void vp8cx_initialize_me_consts(VP8_COMP *cpi, int QIndex)
{
Yaowu Xu's avatar
Yaowu Xu committed
265
266
    cpi->mb.sadperbit16 =  sad_per_bit16lut[QIndex];
    cpi->mb.sadperbit4  =  sad_per_bit4lut[QIndex];
John Koleszar's avatar
John Koleszar committed
267
268
}

269
270

void vp8_initialize_rd_consts(VP8_COMP *cpi, int QIndex)
John Koleszar's avatar
John Koleszar committed
271
272
273
274
275
276
{
    int q;
    int i;

    vp8_clear_system_state();  //__asm emms;

277
278
279
280
    // Further tests required to see if optimum is different
    // for key frames, golden frames and arf frames.
    // if (cpi->common.refresh_golden_frame ||
    //     cpi->common.refresh_alt_ref_frame)
Paul Wilkins's avatar
Paul Wilkins committed
281
    QIndex=(QIndex<0)? 0 : ((QIndex>MAXQ)?MAXQ : QIndex);
282
283

    cpi->RDMULT = compute_rd_mult(QIndex);
John Koleszar's avatar
John Koleszar committed
284

285
286
287
288
289
290
291
292
    // Extend rate multiplier along side quantizer zbin increases
    if (cpi->zbin_over_quant  > 0)
    {
        double oq_factor;

        // Experimental code using the same basic equation as used for Q above
        // The units of cpi->zbin_over_quant are 1/128 of Q bin size
        oq_factor = 1.0 + ((double)0.0015625 * cpi->zbin_over_quant);
293
        cpi->RDMULT = (int)((double)cpi->RDMULT * oq_factor * oq_factor);
294
    }
John Koleszar's avatar
John Koleszar committed
295

Paul Wilkins's avatar
Paul Wilkins committed
296
    if (cpi->pass == 2 && (cpi->common.frame_type != KEY_FRAME))
John Koleszar's avatar
John Koleszar committed
297
    {
298
        if (cpi->twopass.next_iiratio > 31)
Paul Wilkins's avatar
Paul Wilkins committed
299
            cpi->RDMULT += (cpi->RDMULT * rd_iifactor[31]) >> 4;
John Koleszar's avatar
John Koleszar committed
300
        else
301
302
            cpi->RDMULT +=
                (cpi->RDMULT * rd_iifactor[cpi->twopass.next_iiratio]) >> 4;
John Koleszar's avatar
John Koleszar committed
303
304
    }

305
306
    if (cpi->RDMULT < 7)
        cpi->RDMULT = 7;
307

308
    cpi->mb.errorperbit = (cpi->RDMULT / 110);
309
310
    cpi->mb.errorperbit += (cpi->mb.errorperbit==0);

John Koleszar's avatar
John Koleszar committed
311
312
    vp8_set_speed_features(cpi);

313
314
    q = (int)pow(vp8_dc_quant(QIndex,0)>>2, 1.25);
    q = q << 2;
315
    cpi->RDMULT = cpi->RDMULT << 4;
Paul Wilkins's avatar
Paul Wilkins committed
316

317
318
    if (q < 8)
        q = 8;
319

John Koleszar's avatar
John Koleszar committed
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
    if (cpi->RDMULT > 1000)
    {
        cpi->RDDIV = 1;
        cpi->RDMULT /= 100;

        for (i = 0; i < MAX_MODES; i++)
        {
            if (cpi->sf.thresh_mult[i] < INT_MAX)
            {
                cpi->rd_threshes[i] = cpi->sf.thresh_mult[i] * q / 100;
            }
            else
            {
                cpi->rd_threshes[i] = INT_MAX;
            }

            cpi->rd_baseline_thresh[i] = cpi->rd_threshes[i];
        }
    }
    else
    {
        cpi->RDDIV = 100;

        for (i = 0; i < MAX_MODES; i++)
        {
            if (cpi->sf.thresh_mult[i] < (INT_MAX / q))
            {
                cpi->rd_threshes[i] = cpi->sf.thresh_mult[i] * q;
            }
            else
            {
                cpi->rd_threshes[i] = INT_MAX;
            }

            cpi->rd_baseline_thresh[i] = cpi->rd_threshes[i];
        }
    }

    fill_token_costs(
        cpi->mb.token_costs,
        (const vp8_prob( *)[8][3][11]) cpi->common.fc.coef_probs
    );

363
364
365
366
    fill_token_costs(
        cpi->mb.token_costs_8x8,
        (const vp8_prob( *)[8][3][11]) cpi->common.fc.coef_probs_8x8
    );
367
368
369
370
#if CONFIG_QIMODE
    //rough estimate for costing
    cpi->common.kf_ymode_probs_index = cpi->common.base_qindex>>4;
#endif
John Koleszar's avatar
John Koleszar committed
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
    vp8_init_mode_costs(cpi);

}

void vp8_auto_select_speed(VP8_COMP *cpi)
{
    int milliseconds_for_compress = (int)(1000000 / cpi->oxcf.frame_rate);

    milliseconds_for_compress = milliseconds_for_compress * (16 - cpi->oxcf.cpu_used) / 16;

#if 0

    if (0)
    {
        FILE *f;

        f = fopen("speed.stt", "a");
        fprintf(f, " %8ld %10ld %10ld %10ld\n",
                cpi->common.current_video_frame, cpi->Speed, milliseconds_for_compress, cpi->avg_pick_mode_time);
        fclose(f);
    }

#endif

    /*
    // this is done during parameter valid check
Johann's avatar
Johann committed
397
398
399
400
    if( cpi->oxcf.cpu_used > 16)
        cpi->oxcf.cpu_used = 16;
    if( cpi->oxcf.cpu_used < -16)
        cpi->oxcf.cpu_used = -16;
John Koleszar's avatar
John Koleszar committed
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
    */

    if (cpi->avg_pick_mode_time < milliseconds_for_compress && (cpi->avg_encode_time - cpi->avg_pick_mode_time) < milliseconds_for_compress)
    {
        if (cpi->avg_pick_mode_time == 0)
        {
            cpi->Speed = 4;
        }
        else
        {
            if (milliseconds_for_compress * 100 < cpi->avg_encode_time * 95)
            {
                cpi->Speed          += 2;
                cpi->avg_pick_mode_time = 0;
                cpi->avg_encode_time = 0;

                if (cpi->Speed > 16)
                {
                    cpi->Speed = 16;
                }
            }

423
            if (milliseconds_for_compress * 100 > cpi->avg_encode_time * auto_speed_thresh[cpi->Speed])
John Koleszar's avatar
John Koleszar committed
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
            {
                cpi->Speed          -= 1;
                cpi->avg_pick_mode_time = 0;
                cpi->avg_encode_time = 0;

                // In real-time mode, cpi->speed is in [4, 16].
                if (cpi->Speed < 4)        //if ( cpi->Speed < 0 )
                {
                    cpi->Speed = 4;        //cpi->Speed = 0;
                }
            }
        }
    }
    else
    {
        cpi->Speed += 4;

        if (cpi->Speed > 16)
            cpi->Speed = 16;


        cpi->avg_pick_mode_time = 0;
        cpi->avg_encode_time = 0;
    }
}

int vp8_block_error_c(short *coeff, short *dqcoeff)
{
    int i;
    int error = 0;

    for (i = 0; i < 16; i++)
    {
        int this_diff = coeff[i] - dqcoeff[i];
        error += this_diff * this_diff;
    }

    return error;
}

int vp8_mbblock_error_c(MACROBLOCK *mb, int dc)
{
    BLOCK  *be;
    BLOCKD *bd;
    int i, j;
    int berror, error = 0;

    for (i = 0; i < 16; i++)
    {
        be = &mb->block[i];
        bd = &mb->e_mbd.block[i];

        berror = 0;

        for (j = dc; j < 16; j++)
        {
            int this_diff = be->coeff[j] - bd->dqcoeff[j];
            berror += this_diff * this_diff;
        }

        error += berror;
    }

    return error;
}

int vp8_mbuverror_c(MACROBLOCK *mb)
{

    BLOCK  *be;
    BLOCKD *bd;


    int i;
    int error = 0;

    for (i = 16; i < 24; i++)
    {
        be = &mb->block[i];
        bd = &mb->e_mbd.block[i];

        error += vp8_block_error_c(be->coeff, bd->dqcoeff);
    }

    return error;
}

int VP8_UVSSE(MACROBLOCK *x, const vp8_variance_rtcd_vtable_t *rtcd)
{
    unsigned char *uptr, *vptr;
    unsigned char *upred_ptr = (*(x->block[16].base_src) + x->block[16].src);
    unsigned char *vpred_ptr = (*(x->block[20].base_src) + x->block[20].src);
    int uv_stride = x->block[16].src_stride;

    unsigned int sse1 = 0;
    unsigned int sse2 = 0;
520
521
    int mv_row = x->e_mbd.mode_info_context->mbmi.mv.as_mv.row;
    int mv_col = x->e_mbd.mode_info_context->mbmi.mv.as_mv.col;
John Koleszar's avatar
John Koleszar committed
522
523
524
    int offset;
    int pre_stride = x->e_mbd.block[16].pre_stride;

525
526
527
528
529
530
531
532
533
534
535
536
    if (mv_row < 0)
        mv_row -= 1;
    else
        mv_row += 1;

    if (mv_col < 0)
        mv_col -= 1;
    else
        mv_col += 1;

    mv_row /= 2;
    mv_col /= 2;
John Koleszar's avatar
John Koleszar committed
537
538
539
540
541
542
543

    offset = (mv_row >> 3) * pre_stride + (mv_col >> 3);
    uptr = x->e_mbd.pre.u_buffer + offset;
    vptr = x->e_mbd.pre.v_buffer + offset;

    if ((mv_row | mv_col) & 7)
    {
544
545
546
547
548
549
#if CONFIG_SIXTEENTH_SUBPEL_UV
        VARIANCE_INVOKE(rtcd, subpixvar8x8)(uptr, pre_stride,
            (mv_col & 7)<<1, (mv_row & 7)<<1, upred_ptr, uv_stride, &sse2);
        VARIANCE_INVOKE(rtcd, subpixvar8x8)(vptr, pre_stride,
            (mv_col & 7)<<1, (mv_row & 7)<<1, vpred_ptr, uv_stride, &sse1);
#else
550
551
552
553
        VARIANCE_INVOKE(rtcd, subpixvar8x8)(uptr, pre_stride,
            mv_col & 7, mv_row & 7, upred_ptr, uv_stride, &sse2);
        VARIANCE_INVOKE(rtcd, subpixvar8x8)(vptr, pre_stride,
            mv_col & 7, mv_row & 7, vpred_ptr, uv_stride, &sse1);
554
#endif
John Koleszar's avatar
John Koleszar committed
555
556
557
558
        sse2 += sse1;
    }
    else
    {
559
560
561
562
        VARIANCE_INVOKE(rtcd, var8x8)(uptr, pre_stride,
            upred_ptr, uv_stride, &sse2);
        VARIANCE_INVOKE(rtcd, var8x8)(vptr, pre_stride,
            vpred_ptr, uv_stride, &sse1);
John Koleszar's avatar
John Koleszar committed
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
        sse2 += sse1;
    }
    return sse2;

}

static int cost_coeffs(MACROBLOCK *mb, BLOCKD *b, int type, ENTROPY_CONTEXT *a, ENTROPY_CONTEXT *l)
{
    int c = !type;              /* start at coef 0, unless Y with Y2 */
    int eob = b->eob;
    int pt ;    /* surrounding block/prev coef predictor */
    int cost = 0;
    short *qcoeff_ptr = b->qcoeff;

    VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);

# define QC( I)  ( qcoeff_ptr [vp8_default_zig_zag1d[I]] )

    for (; c < eob; c++)
    {
        int v = QC(c);
        int t = vp8_dct_value_tokens_ptr[v].Token;
        cost += mb->token_costs [type] [vp8_coef_bands[c]] [pt] [t];
        cost += vp8_dct_value_cost_ptr[v];
        pt = vp8_prev_token_class[t];
    }

# undef QC

    if (c < 16)
        cost += mb->token_costs [type] [vp8_coef_bands[c]] [pt] [DCT_EOB_TOKEN];

    pt = (c != !type); // is eob first coefficient;
    *a = *l = pt;

    return cost;
}

Scott LaVarnway's avatar
Scott LaVarnway committed
601
static int vp8_rdcost_mby(MACROBLOCK *mb)
John Koleszar's avatar
John Koleszar committed
602
603
604
605
{
    int cost = 0;
    int b;
    MACROBLOCKD *x = &mb->e_mbd;
606
607
608
609
610
611
    ENTROPY_CONTEXT_PLANES t_above, t_left;
    ENTROPY_CONTEXT *ta;
    ENTROPY_CONTEXT *tl;

    vpx_memcpy(&t_above, mb->e_mbd.above_context, sizeof(ENTROPY_CONTEXT_PLANES));
    vpx_memcpy(&t_left, mb->e_mbd.left_context, sizeof(ENTROPY_CONTEXT_PLANES));
John Koleszar's avatar
John Koleszar committed
612

613
614
    ta = (ENTROPY_CONTEXT *)&t_above;
    tl = (ENTROPY_CONTEXT *)&t_left;
John Koleszar's avatar
John Koleszar committed
615
616

    for (b = 0; b < 16; b++)
Scott LaVarnway's avatar
Scott LaVarnway committed
617
        cost += cost_coeffs(mb, x->block + b, PLANE_TYPE_Y_NO_DC,
618
                    ta + vp8_block2above[b], tl + vp8_block2left[b]);
John Koleszar's avatar
John Koleszar committed
619

Scott LaVarnway's avatar
Scott LaVarnway committed
620
    cost += cost_coeffs(mb, x->block + 24, PLANE_TYPE_Y2,
Scott LaVarnway's avatar
Scott LaVarnway committed
621
                ta + vp8_block2above[24], tl + vp8_block2left[24]);
John Koleszar's avatar
John Koleszar committed
622
623
624
625

    return cost;
}

626
627
628
629
630
631
632
633
634
635
636
637
638
static void macro_block_yrd( MACROBLOCK *mb,
                             int *Rate,
                             int *Distortion,
                             const vp8_encodemb_rtcd_vtable_t *rtcd)
{
    int b;
    MACROBLOCKD *const x = &mb->e_mbd;
    BLOCK   *const mb_y2 = mb->block + 24;
    BLOCKD *const x_y2  = x->block + 24;
    short *Y2DCPtr = mb_y2->src_diff;
    BLOCK *beptr;
    int d;

639
640
    ENCODEMB_INVOKE(rtcd, submby)( mb->src_diff, *(mb->block[0].base_src),
                                   mb->e_mbd.predictor, mb->block[0].src_stride );
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663

    // Fdct and building the 2nd order block
    for (beptr = mb->block; beptr < mb->block + 16; beptr += 2)
    {
        mb->vp8_short_fdct8x4(beptr->src_diff, beptr->coeff, 32);
        *Y2DCPtr++ = beptr->coeff[0];
        *Y2DCPtr++ = beptr->coeff[16];
    }

    // 2nd order fdct
    mb->short_walsh4x4(mb_y2->src_diff, mb_y2->coeff, 8);

    // Quantization
    for (b = 0; b < 16; b++)
    {
        mb->quantize_b(&mb->block[b], &mb->e_mbd.block[b]);
    }

    // DC predication and Quantization of 2nd Order block
    mb->quantize_b(mb_y2, x_y2);

    // Distortion
    d = ENCODEMB_INVOKE(rtcd, mberr)(mb, 1) << 2;
664
665

    d += ENCODEMB_INVOKE(rtcd, berr)(mb_y2->coeff, x_y2->dqcoeff)<<2;
666
667
668
669
670

    *Distortion = (d >> 4);
    // rate
    *Rate = vp8_rdcost_mby(mb);
}
John Koleszar's avatar
John Koleszar committed
671

672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709

static int cost_coeffs_2x2(MACROBLOCK *mb,
                           BLOCKD *b, int type,
                           ENTROPY_CONTEXT *a, ENTROPY_CONTEXT *l)
{
    int c = !type;              /* start at coef 0, unless Y with Y2 */
    int eob = b->eob;
    int pt ;    /* surrounding block/prev coef predictor */
    int cost = 0;
    short *qcoeff_ptr = b->qcoeff;

    VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);
    assert(eob<=4);

# define QC2X2( I)  ( qcoeff_ptr [vp8_default_zig_zag1d[I]] )

    for (; c < eob; c++)
    {
        int v = QC2X2(c);
        int t = vp8_dct_value_tokens_ptr[v].Token;
        cost += mb->token_costs_8x8[type] [vp8_coef_bands[c]] [pt] [t];
        cost += vp8_dct_value_cost_ptr[v];
        pt = vp8_prev_token_class[t];
    }

# undef QC2X2
    if (c < 4)
        cost += mb->token_costs_8x8 [type][vp8_coef_bands[c]]
                                    [pt] [DCT_EOB_TOKEN];

    pt = (c != !type); // is eob first coefficient;
    *a = *l = pt;
    return cost;
}


static int cost_coeffs_8x8(MACROBLOCK *mb,
                           BLOCKD *b, int type,
710
                           ENTROPY_CONTEXT *a, ENTROPY_CONTEXT *l)
711
712
713
714
715
716
717
{
    int c = !type;              /* start at coef 0, unless Y with Y2 */
    int eob = b->eob;
    int pt ;    /* surrounding block/prev coef predictor */
    int cost = 0;
    short *qcoeff_ptr = b->qcoeff;

718
    VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756

# define QC8X8( I)  ( qcoeff_ptr [vp8_default_zig_zag1d_8x8[I]] )

    for (; c < eob; c++)
    {
        int v = QC8X8(c);
        int t = vp8_dct_value_tokens_ptr[v].Token;
        cost += mb->token_costs_8x8[type] [vp8_coef_bands_8x8[c]] [pt] [t];
        cost += vp8_dct_value_cost_ptr[v];
        pt = vp8_prev_token_class[t];
    }

# undef QC8X8
    if (c < 64)
        cost += mb->token_costs_8x8 [type][vp8_coef_bands_8x8[c]]
                                    [pt] [DCT_EOB_TOKEN];

    pt = (c != !type); // is eob first coefficient;
    *a = *l = pt;
    return cost;
}
static int vp8_rdcost_mby_8x8(MACROBLOCK *mb)
{
    int cost = 0;
    int b;
    MACROBLOCKD *x = &mb->e_mbd;
    ENTROPY_CONTEXT_PLANES t_above, t_left;
    ENTROPY_CONTEXT *ta;
    ENTROPY_CONTEXT *tl;

    vpx_memcpy(&t_above, mb->e_mbd.above_context, sizeof(ENTROPY_CONTEXT_PLANES));
    vpx_memcpy(&t_left, mb->e_mbd.left_context, sizeof(ENTROPY_CONTEXT_PLANES));

    ta = (ENTROPY_CONTEXT *)&t_above;
    tl = (ENTROPY_CONTEXT *)&t_left;

    for (b = 0; b < 16; b+=4)
        cost += cost_coeffs_8x8(mb, x->block + b, PLANE_TYPE_Y_NO_DC,
757
                    ta + vp8_block2above_8x8[b], tl + vp8_block2left_8x8[b]);
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792

    cost += cost_coeffs_2x2(mb, x->block + 24, PLANE_TYPE_Y2,
                ta + vp8_block2above[24], tl + vp8_block2left[24]);
    return cost;
}

static void macro_block_yrd_8x8( MACROBLOCK *mb,
                             int *Rate,
                             int *Distortion,
                             const VP8_ENCODER_RTCD *rtcd)
{
    MACROBLOCKD *const x = &mb->e_mbd;
    BLOCK   *const mb_y2 = mb->block + 24;
    BLOCKD *const x_y2  = x->block + 24;
    short *Y2DCPtr = mb_y2->src_diff;
    int d;

    ENCODEMB_INVOKE(&rtcd->encodemb, submby)
        ( mb->src_diff, *(mb->block[0].base_src),
        mb->e_mbd.predictor, mb->block[0].src_stride );

    vp8_transform_mby_8x8(mb);
    vp8_quantize_mby_8x8(mb);

    /* remove 1st order dc to properly combine 1st/2nd order distortion */
    mb->coeff[0] = 0;
    mb->coeff[64] = 0;
    mb->coeff[128] = 0;
    mb->coeff[192] = 0;
    mb->e_mbd.dqcoeff[0] = 0;
    mb->e_mbd.dqcoeff[64] = 0;
    mb->e_mbd.dqcoeff[128] = 0;
    mb->e_mbd.dqcoeff[192] = 0;
    d = ENCODEMB_INVOKE(&rtcd->encodemb, mberr)(mb, 0) << 2;

793
    d += ENCODEMB_INVOKE(&rtcd->encodemb, berr)(mb_y2->coeff, x_y2->dqcoeff)<<2;
794
795
796
797
798

    *Distortion = (d >> 4);
    // rate
    *Rate = vp8_rdcost_mby_8x8(mb);
}
799

800
static void copy_predictor(unsigned char *dst, const unsigned char *predictor)
801
{
802
803
804
805
806
807
    const unsigned int *p = (const unsigned int *)predictor;
    unsigned int *d = (unsigned int *)dst;
    d[0] = p[0];
    d[4] = p[4];
    d[8] = p[8];
    d[12] = p[12];
808
}
Yaowu Xu's avatar
Yaowu Xu committed
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831

static void copy_predictor_8x8(unsigned char *dst, const unsigned char *predictor)
{
    const unsigned int *p = (const unsigned int *)predictor;
    unsigned int *d = (unsigned int *)dst;
    d[0] = p[0];
    d[1] = p[1];
    d[4] = p[4];
    d[5] = p[5];
    d[8] = p[8];
    d[9] = p[9];
    d[12] = p[12];
    d[13] = p[13];
    d[16] = p[16];
    d[17] = p[17];
    d[20] = p[20];
    d[21] = p[21];
    d[24] = p[24];
    d[25] = p[25];
    d[28] = p[28];
    d[29] = p[29];
}

832
static int rd_pick_intra4x4block(
John Koleszar's avatar
John Koleszar committed
833
834
835
836
837
    VP8_COMP *cpi,
    MACROBLOCK *x,
    BLOCK *be,
    BLOCKD *b,
    B_PREDICTION_MODE *best_mode,
838
839
840
#if CONFIG_COMP_INTRA_PRED
    B_PREDICTION_MODE *best_second_mode,
#endif
841
    unsigned int *bmode_costs,
John Koleszar's avatar
John Koleszar committed
842
843
844
845
846
    ENTROPY_CONTEXT *a,
    ENTROPY_CONTEXT *l,

    int *bestrate,
    int *bestratey,
847
848
    int *bestdistortion,
    int allow_comp)
John Koleszar's avatar
John Koleszar committed
849
850
{
    B_PREDICTION_MODE mode;
851
852
853
#if CONFIG_COMP_INTRA_PRED
    B_PREDICTION_MODE mode2;
#endif
854
    int best_rd = INT_MAX;
John Koleszar's avatar
John Koleszar committed
855
856
857
858
859
    int rate = 0;
    int distortion;

    ENTROPY_CONTEXT ta = *a, tempa = *a;
    ENTROPY_CONTEXT tl = *l, templ = *l;
860
861
862
863
864
865
866
    /*
     * The predictor buffer is a 2d buffer with a stride of 16.  Create
     * a temp buffer that meets the stride requirements, but we are only
     * interested in the left 4x4 block
     * */
    DECLARE_ALIGNED_ARRAY(16, unsigned char,  best_predictor, 16*4);
    DECLARE_ALIGNED_ARRAY(16, short, best_dqcoeff, 16);
John Koleszar's avatar
John Koleszar committed
867
868
869

    for (mode = B_DC_PRED; mode <= B_HU_PRED; mode++)
    {
870
#if CONFIG_COMP_INTRA_PRED
871
        for (mode2 = (allow_comp ? 0 : (B_DC_PRED - 1)); mode2 != (allow_comp ? (mode + 1) : 0); mode2++)
872
873
        {
#endif
John Koleszar's avatar
John Koleszar committed
874
875
876
        int this_rd;
        int ratey;

Adrian Grange's avatar
Adrian Grange committed
877
878
879
880
881
882
        // TODO Temporarily ignore modes that need the above-right data. SB
        // encoding means this data is not available for the bottom right MB
        // Do we need to do this for mode2 also?
        if (mode==B_LD_PRED || mode==B_VL_PRED)
            continue;

883
884
        rate = bmode_costs[mode];

885
886
887
888
#if CONFIG_COMP_INTRA_PRED
            if (mode2 == (B_PREDICTION_MODE) (B_DC_PRED - 1))
            {
#endif
889
890
        RECON_INVOKE(&cpi->rtcd.common->recon, intra4x4_predict)
                     (b, mode, b->predictor);
891
892
893
894
895
896
897
898
899
#if CONFIG_COMP_INTRA_PRED
            }
            else
            {
                RECON_INVOKE(&cpi->rtcd.common->recon, comp_intra4x4_predict)
                    (b, mode, mode2, b->predictor);
                rate += bmode_costs[mode2];
            }
#endif
900
901
902
        ENCODEMB_INVOKE(IF_RTCD(&cpi->rtcd.encodemb), subb)(be, b, 16);
        x->vp8_short_fdct4x4(be->src_diff, be->coeff, 32);
        x->quantize_b(be, b);
John Koleszar's avatar
John Koleszar committed
903
904
905
906

        tempa = ta;
        templ = tl;

Scott LaVarnway's avatar
Scott LaVarnway committed
907
        ratey = cost_coeffs(x, b, PLANE_TYPE_Y_WITH_DC, &tempa, &templ);
John Koleszar's avatar
John Koleszar committed
908
909
910
911
912
913
914
915
916
917
918
919
        rate += ratey;
        distortion = ENCODEMB_INVOKE(IF_RTCD(&cpi->rtcd.encodemb), berr)(be->coeff, b->dqcoeff) >> 2;

        this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);

        if (this_rd < best_rd)
        {
            *bestrate = rate;
            *bestratey = ratey;
            *bestdistortion = distortion;
            best_rd = this_rd;
            *best_mode = mode;
920
921
922
#if CONFIG_COMP_INTRA_PRED
            *best_second_mode = mode2;
#endif
John Koleszar's avatar
John Koleszar committed
923
924
            *a = tempa;
            *l = templ;
925
926
            copy_predictor(best_predictor, b->predictor);
            vpx_memcpy(best_dqcoeff, b->dqcoeff, 32);
927
928
929
#if CONFIG_COMP_INTRA_PRED
        }
#endif
John Koleszar's avatar
John Koleszar committed
930
931
        }
    }
932
933
934
935
    b->bmi.as_mode.first = (B_PREDICTION_MODE)(*best_mode);
#if CONFIG_COMP_INTRA_PRED
    b->bmi.as_mode.second = (B_PREDICTION_MODE)(*best_second_mode);
#endif
936

937
938
    IDCT_INVOKE(IF_RTCD(&cpi->rtcd.common->idct), idct16)(best_dqcoeff, b->diff, 32);
    RECON_INVOKE(IF_RTCD(&cpi->rtcd.common->recon), recon)(best_predictor, b->diff, *(b->base_dst) + b->dst, b->dst_stride);
John Koleszar's avatar
John Koleszar committed
939

940
    return best_rd;
John Koleszar's avatar
John Koleszar committed
941
942
}

943
static int rd_pick_intra4x4mby_modes(VP8_COMP *cpi, MACROBLOCK *mb, int *Rate,
944
                                     int *rate_y, int *Distortion, int best_rd,
Adrian Grange's avatar
Adrian Grange committed
945
                                     int allow_comp, int update_contexts)
John Koleszar's avatar
John Koleszar committed
946
947
{
    int i;
Adrian Grange's avatar
Adrian Grange committed
948
    MACROBLOCKD *const xd = &mb->e_mbd;
John Koleszar's avatar
John Koleszar committed
949
950
951
    int cost = mb->mbmode_cost [xd->frame_type] [B_PRED];
    int distortion = 0;
    int tot_rate_y = 0;
952
    int64_t total_rd = 0;
953
954
955
    ENTROPY_CONTEXT_PLANES t_above, t_left;
    ENTROPY_CONTEXT *ta;
    ENTROPY_CONTEXT *tl;
956
    unsigned int *bmode_costs;
957

Adrian Grange's avatar
Adrian Grange committed
958
959
960
961
962
963
964
965
966
967
968
    if (update_contexts)
    {
        ta = (ENTROPY_CONTEXT *)mb->e_mbd.above_context;
        tl = (ENTROPY_CONTEXT *)mb->e_mbd.left_context;
    }
    else
    {
        vpx_memcpy(&t_above, mb->e_mbd.above_context,
                   sizeof(ENTROPY_CONTEXT_PLANES));
        vpx_memcpy(&t_left, mb->e_mbd.left_context,
                   sizeof(ENTROPY_CONTEXT_PLANES));
969

Adrian Grange's avatar
Adrian Grange committed
970
971
972
        ta = (ENTROPY_CONTEXT *)&t_above;
        tl = (ENTROPY_CONTEXT *)&t_left;
    }
John Koleszar's avatar
John Koleszar committed
973

Adrian Grange's avatar
Adrian Grange committed
974
#if 0
John Koleszar's avatar
John Koleszar committed
975
    vp8_intra_prediction_down_copy(xd);
Adrian Grange's avatar
Adrian Grange committed
976
#endif
John Koleszar's avatar
John Koleszar committed
977

978
979
    bmode_costs = mb->inter_bmode_costs;

John Koleszar's avatar
John Koleszar committed
980
981
982
983
984
    for (i = 0; i < 16; i++)
    {
        MODE_INFO *const mic = xd->mode_info_context;
        const int mis = xd->mode_info_stride;
        B_PREDICTION_MODE UNINITIALIZED_IS_SAFE(best_mode);
985
986
987
#if CONFIG_COMP_INTRA_PRED
        B_PREDICTION_MODE UNINITIALIZED_IS_SAFE(best_second_mode);
#endif
John Koleszar's avatar
John Koleszar committed
988
989
        int UNINITIALIZED_IS_SAFE(r), UNINITIALIZED_IS_SAFE(ry), UNINITIALIZED_IS_SAFE(d);

990
991
        if (mb->e_mbd.frame_type == KEY_FRAME)
        {
Scott LaVarnway's avatar
Scott LaVarnway committed
992
993
            const B_PREDICTION_MODE A = above_block_mode(mic, i, mis);
            const B_PREDICTION_MODE L = left_block_mode(mic, i);
994
995
996
997

            bmode_costs  = mb->bmode_costs[A][L];
        }

998
        total_rd += rd_pick_intra4x4block(
999
1000
1001
1002
1003
            cpi, mb, mb->block + i, xd->block + i, &best_mode,
#if CONFIG_COMP_INTRA_PRED
            &best_second_mode,
#endif
            bmode_costs, ta + vp8_block2above[i],
1004
            tl + vp8_block2left[i], &r, &ry, &d, allow_comp);
John Koleszar's avatar
John Koleszar committed
1005
1006
1007
1008

        cost += r;
        distortion += d;
        tot_rate_y += ry;
Scott LaVarnway's avatar
Scott LaVarnway committed
1009

1010
1011
1012
1013
        mic->bmi[i].as_mode.first = best_mode;
#if CONFIG_COMP_INTRA_PRED
        mic->bmi[i].as_mode.second = best_second_mode;
#endif
1014

1015
        if(total_rd >= (int64_t)best_rd)
1016
            break;
John Koleszar's avatar
John Koleszar committed
1017
1018
    }

1019
    if(total_rd >= (int64_t)best_rd)
1020
        return INT_MAX;
1021

1022
    *Rate = cost + vp8_cost_bit(128, allow_comp);
John Koleszar's avatar
John Koleszar committed
1023
1024
1025
1026
1027
    *rate_y += tot_rate_y;
    *Distortion = distortion;

    return RDCOST(mb->rdmult, mb->rddiv, cost, distortion);
}
1028
1029
1030
1031
1032
1033
1034


static int rd_pick_intra16x16mby_mode(VP8_COMP *cpi,
                                      MACROBLOCK *x,
                                      int *Rate,
                                      int *rate_y,
                                      int *Distortion)
John Koleszar's avatar
John Koleszar committed
1035
1036
1037
{
    MB_PREDICTION_MODE mode;
    MB_PREDICTION_MODE UNINITIALIZED_IS_SAFE(mode_selected);
1038
1039
1040
1041
#if CONFIG_COMP_INTRA_PRED
    MB_PREDICTION_MODE mode2;
    MB_PREDICTION_MODE UNINITIALIZED_IS_SAFE(mode2_selected);
#endif
John Koleszar's avatar
John Koleszar committed
1042
    int rate, ratey;
1043
    int distortion;
John Koleszar's avatar
John Koleszar committed
1044
    int best_rd = INT_MAX;
1045
    int this_rd;
John Koleszar's avatar
John Koleszar committed
1046
1047
1048
1049

    //Y Search for 16x16 intra prediction mode
    for (mode = DC_PRED; mode <= TM_PRED; mode++)
    {
1050
        x->e_mbd.mode_info_context->mbmi.mode = mode;
1051
1052
1053
1054
1055
1056
1057
#if CONFIG_COMP_INTRA_PRED
        for (mode2 = DC_PRED - 1; mode2 != TM_PRED + 1; mode2++)
        {
            x->e_mbd.mode_info_context->mbmi.second_mode = mode2;
            if (mode2 == (MB_PREDICTION_MODE) (DC_PRED - 1))
            {
#endif
1058
1059
        RECON_INVOKE(&cpi->common.rtcd.recon, build_intra_predictors_mby)
            (&x->e_mbd);
1060
1061
1062
1063
1064
1065
1066
1067
#if CONFIG_COMP_INTRA_PRED
            }
            else
            {
                continue; // i.e. disable for now
                RECON_INVOKE(&cpi->common.rtcd.recon, build_comp_intra_predictors_mby)(&x->e_mbd);
            }
#endif
John Koleszar's avatar
John Koleszar committed
1068

1069
        macro_block_yrd(x, &ratey, &distortion, IF_RTCD(&cpi->rtcd.encodemb));
1070
1071
            // FIXME add compoundmode cost
            // FIXME add rate for mode2
1072
1073
        rate = ratey + x->mbmode_cost[x->e_mbd.frame_type]
                                     [x->e_mbd.mode_info_context->mbmi.mode];
John Koleszar's avatar
John Koleszar committed
1074
1075
1076
1077
1078
1079

        this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);

        if (this_rd < best_rd)
        {
            mode_selected = mode;
1080
1081
1082
#if CONFIG_COMP_INTRA_PRED
            mode2_selected = mode2;
#endif
John Koleszar's avatar
John Koleszar committed
1083
1084
1085
            best_rd = this_rd;
            *Rate = rate;
            *rate_y = ratey;
1086
            *Distortion = distortion;
John Koleszar's avatar
John Koleszar committed
1087
        }
1088
1089
1090
#if CONFIG_COMP_INTRA_PRED
        }
#endif
John Koleszar's avatar
John Koleszar committed
1091
1092
    }

1093
    x->e_mbd.mode_info_context->mbmi.mode = mode_selected;
1094
1095
1096
#if CONFIG_COMP_INTRA_PRED
    x->e_mbd.mode_info_context->mbmi.second_mode = mode2_selected;
#endif
John Koleszar's avatar
John Koleszar committed
1097
1098
    return best_rd;
}
Yaowu Xu's avatar
Yaowu Xu committed
1099
1100
1101
1102
1103
static int rd_pick_intra8x8block(
    VP8_COMP *cpi,
    MACROBLOCK *x,
    int ib,
    B_PREDICTION_MODE *best_mode,
1104
1105
1106
#if CONFIG_COMP_INTRA_PRED
    B_PREDICTION_MODE *best_second_mode,
#endif
Yaowu Xu's avatar
Yaowu Xu committed
1107
1108
1109
1110
1111
1112
1113
1114
    unsigned int *mode_costs,
    ENTROPY_CONTEXT *a,
    ENTROPY_CONTEXT *l,
    int *bestrate,
    int *bestratey,
    int *bestdistortion)
{
    MB_PREDICTION_MODE mode;
1115
1116
1117
#if CONFIG_COMP_INTRA_PRED
    MB_PREDICTION_MODE mode2;
#endif
Yaowu Xu's avatar
Yaowu Xu committed
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
    MACROBLOCKD *xd = &x->e_mbd;
    int best_rd = INT_MAX;
    int rate = 0;
    int distortion;
    BLOCK  *be=x->block + ib;
    BLOCKD *b=x->e_mbd.block + ib;
    ENTROPY_CONTEXT ta0, ta1, besta0, besta1;
    ENTROPY_CONTEXT tl0, tl1, bestl0, bestl1;


    /*
     * The predictor buffer is a 2d buffer with a stride of 16.  Create
     * a temp buffer that meets the stride requirements, but we are only
     * interested in the left 8x8 block
     * */

    DECLARE_ALIGNED_ARRAY(16, unsigned char,  best_predictor, 16*8);
    DECLARE_ALIGNED_ARRAY(16, short, best_dqcoeff, 16*4);

    for (mode = DC_PRED; mode <= TM_PRED; mode++)
    {
1139
1140
1141
1142
#if CONFIG_COMP_INTRA_PRED
        for (mode2 = DC_PRED - 1; mode2 != TM_PRED + 1; mode2++)
        {
#endif
Yaowu Xu's avatar
Yaowu Xu committed
1143
1144
1145
        int this_rd;
        int rate_t;

1146
        // FIXME rate for compound mode and second intrapred mode
Yaowu Xu's avatar
Yaowu Xu committed
1147
1148
        rate = mode_costs[mode];

1149
1150
1151
1152
#if CONFIG_COMP_INTRA_PRED
            if (mode2 == (MB_PREDICTION_MODE) (DC_PRED - 1))
            {
#endif
Yaowu Xu's avatar
Yaowu Xu committed
1153
1154
        RECON_INVOKE(&cpi->rtcd.common->recon, intra8x8_predict)
                     (b, mode, b->predictor);
1155
1156
1157
1158
1159
1160
1161
1162
1163
#if CONFIG_COMP_INTRA_PRED
            }
            else
            {
                continue; // i.e. disable for now
                RECON_INVOKE(&cpi->rtcd.common->recon, comp_intra8x8_predict)
                    (b, mode, mode2, b->predictor);
            }
#endif
Yaowu Xu's avatar
Yaowu Xu committed
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208

        vp8_subtract_4b_c(be, b, 16);

        x->vp8_short_fdct8x4(be->src_diff, be->coeff, 32);
        x->vp8_short_fdct8x4(be->src_diff + 64, be->coeff + 64, 32);

        x->quantize_b_pair(x->block+ib, x->block+ib+1,
                            xd->block+ib, xd->block+ib+1);
        x->quantize_b_pair(x->block+ib+4, x->block+ib+5,
                            xd->block+ib+4, xd->block+ib+5);

        distortion = ENCODEMB_INVOKE(IF_RTCD(&cpi->rtcd.encodemb), berr)
            ((x->block+ib)->coeff,(xd->block+ib)->dqcoeff)>>2;
        distortion += ENCODEMB_INVOKE(IF_RTCD(&cpi->rtcd.encodemb), berr)
            ((x->block+ib+1)->coeff,(xd->block+ib+1)->dqcoeff)>>2;
        distortion += ENCODEMB_INVOKE(IF_RTCD(&cpi->rtcd.encodemb), berr)
            ((x->block+ib+4)->coeff,(xd->block+ib+4)->dqcoeff)>>2;
        distortion += ENCODEMB_INVOKE(IF_RTCD(&cpi->rtcd.encodemb), berr)
            ((x->block+ib+5)->coeff,(xd->block+ib+5)->dqcoeff)>>2;

        ta0 = *(a + vp8_block2above[ib]);
        ta1 = *(a + vp8_block2above[ib+1]);
        tl0 = *(l + vp8_block2above[ib]);
        tl1 = *(l + vp8_block2above[ib+4]);
        rate_t = cost_coeffs(x, xd->block+ib, PLANE_TYPE_Y_WITH_DC,
            &ta0, &tl0);
        rate_t += cost_coeffs(x, xd->block+ib+1, PLANE_TYPE_Y_WITH_DC,
            &ta1, &tl0);
        rate_t += cost_coeffs(x, xd->block+ib+4, PLANE_TYPE_Y_WITH_DC,
            &ta0, &tl1);
        rate_t += cost_coeffs(x, xd->block+ib+5, PLANE_TYPE_Y_WITH_DC,
            &ta1, &tl1);
        rate += rate_t;
        this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);
        if (this_rd < best_rd)
        {
            *bestrate = rate;
            *bestratey = rate_t;
            *bestdistortion = distortion;
            besta0 = ta0;
            besta1 = ta1;
            bestl0 = tl0;
            bestl1 = tl1;
            best_rd = this_rd;
            *best_mode = mode;
1209
1210
1211
#if CONFIG_COMP_INTRA_PRED
            *best_second_mode = mode2;
#endif
Yaowu Xu's avatar
Yaowu Xu committed
1212
1213
1214
            copy_predictor_8x8(best_predictor, b->predictor);
            vpx_memcpy(best_dqcoeff, b->dqcoeff, 64);
            vpx_memcpy(best_dqcoeff+32, b->dqcoeff+64, 64);
1215
1216
1217
#if CONFIG_COMP_INTRA_PRED
        }
#endif
Yaowu Xu's avatar
Yaowu Xu committed
1218
1219
        }
    }
1220
1221
1222
1223
    b->bmi.as_mode.first = (*best_mode);
#if CONFIG_COMP_INTRA_PRED
    b->bmi.as_mode.second = (*best_second_mode);
#endif
Yaowu Xu's avatar
Yaowu Xu committed
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
    vp8_encode_intra8x8 (IF_RTCD(&cpi->rtcd), x, ib);
    *(a + vp8_block2above[ib])   = besta0;
    *(a + vp8_block2above[ib+1]) = besta1;
    *(l + vp8_block2above[ib])   = bestl0;
    *(l + vp8_block2above[ib+4]) = bestl1;
    return best_rd;
}

const int vp8_i8x8_block[4]={0, 2, 8, 10};
int rd_pick_intra8x8mby_modes(VP8_COMP *cpi,
                                      MACROBLOCK *mb,
                                      int *Rate,
                                      int *rate_y,
                                      int *Distortion,
                                      int best_rd)
{
    MACROBLOCKD *const xd = &mb->e_mbd;
    int i,ib;
    int cost = mb->mbmode_cost [xd->frame_type] [I8X8_PRED];
    int distortion = 0;
    int tot_rate_y = 0;
    long long total_rd = 0;
    ENTROPY_CONTEXT_PLANES t_above, t_left;
    ENTROPY_CONTEXT *ta;
    ENTROPY_CONTEXT *tl;
    unsigned int *i8x8mode_costs;

    vpx_memcpy(&t_above, mb->e_mbd.above_context, sizeof(ENTROPY_CONTEXT_PLANES));
    vpx_memcpy(&t_left, mb->e_mbd.left_context, sizeof(ENTROPY_CONTEXT_PLANES));

    ta = (ENTROPY_CONTEXT *)&t_above;
    tl = (ENTROPY_CONTEXT *)&t_left;

    i8x8mode_costs  = mb->i8x8_mode_costs;

    for (i = 0; i < 4; i++)
    {
        MODE_INFO *const mic = xd->mode_info_context;
        B_PREDICTION_MODE UNINITIALIZED_IS_SAFE(best_mode);
1263
1264
1265
#if CONFIG_COMP_INTRA_PRED
        B_PREDICTION_MODE UNINITIALIZED_IS_SAFE(best_second_mode);
#endif
Yaowu Xu's avatar
Yaowu Xu committed
1266
1267
1268
1269
        int UNINITIALIZED_IS_SAFE(r), UNINITIALIZED_IS_SAFE(ry), UNINITIALIZED_IS_SAFE(d);

        ib = vp8_i8x8_block[i];
        total_rd += rd_pick_intra8x8block(
1270
1271
1272
1273
1274
            cpi, mb, ib, &best_mode,
#if CONFIG_COMP_INTRA_PRED
            &best_second_mode,
#endif
            i8x8mode_costs, ta, tl, &r, &ry, &d);
Yaowu Xu's avatar
Yaowu Xu committed
1275
1276
1277
        cost += r;
        distortion += d;
        tot_rate_y += ry;
1278
1279
1280
1281
        mic->bmi[ib].as_mode.first = best_mode;
#if CONFIG_COMP_INTRA_PRED
        mic->bmi[ib].as_mode.second = best_second_mode;
#endif
Yaowu Xu's avatar
Yaowu Xu committed
1282
1283
1284
1285
1286
1287
    }
    *Rate = cost;
    *rate_y += tot_rate_y;
    *Distortion = distortion;
    return RDCOST(mb->rdmult, mb->rddiv, cost, distortion);
}
John Koleszar's avatar
John Koleszar committed
1288
1289
1290
1291
1292
1293

static int rd_cost_mbuv(MACROBLOCK *mb)
{
    int b;
    int cost = 0;
    MACROBLOCKD *x = &mb->e_mbd;
1294
1295
1296
1297
1298
1299
    ENTROPY_CONTEXT_PLANES t_above, t_left;
    ENTROPY_CONTEXT *ta;
    ENTROPY_CONTEXT *tl;

    vpx_memcpy(&t_above, mb->e_mbd.above_context, sizeof(ENTROPY_CONTEXT_PLANES));
    vpx_memcpy(&t_left, mb->e_mbd.left_context, sizeof(ENTROPY_CONTEXT_PLANES));
John Koleszar's avatar
John Koleszar committed
1300

1301
1302
    ta = (ENTROPY_CONTEXT *)&t_above;
    tl = (ENTROPY_CONTEXT *)&t_left;
John Koleszar's avatar
John Koleszar committed
1303

Scott LaVarnway's avatar
Scott LaVarnway committed
1304
1305
    for (b = 16; b < 24; b++)
        cost += cost_coeffs(mb, x->block + b, PLANE_TYPE_UV,
1306
                    ta + vp8_block2above[b], tl + vp8_block2left[b]);
John Koleszar's avatar
John Koleszar committed
1307
1308
1309
1310
1311

    return cost;
}


1312
1313
static int rd_inter16x16_uv(VP8_COMP *cpi, MACROBLOCK *x, int *rate,
                            int *distortion, int fullpixel)
John Koleszar's avatar
John Koleszar committed
1314
{
1315
1316
    ENCODEMB_INVOKE(IF_RTCD(&cpi->rtcd.encodemb), submbuv)(x->src_diff,
        x->src.u_buffer, x->src.v_buffer, x->e_mbd.predictor, x->src.uv_stride);
John Koleszar's avatar
John Koleszar committed
1317

1318
1319
1320
1321
1322
1323
1324
1325
    vp8_transform_mbuv(x);
    vp8_quantize_mbuv(x);

    *rate       = rd_cost_mbuv(x);
    *distortion = ENCODEMB_INVOKE(&cpi->rtcd.encodemb, mbuverr)(x) / 4;

    return RDCOST(x->rdmult, x->rddiv, *rate, *distortion);
}
Yaowu Xu's avatar
Yaowu Xu committed
1326

1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
static int rd_cost_mbuv_8x8(MACROBLOCK *mb)
{
    int b;
    int cost = 0;
    MACROBLOCKD *x = &mb->e_mbd;
    ENTROPY_CONTEXT_PLANES t_above, t_left;
    ENTROPY_CONTEXT *ta;
    ENTROPY_CONTEXT *tl;

    vpx_memcpy(&t_above, mb->e_mbd.above_context, sizeof(ENTROPY_CONTEXT_PLANES));
    vpx_memcpy(&t_left, mb->e_mbd.left_context, sizeof(ENTROPY_CONTEXT_PLANES));

    ta = (ENTROPY_CONTEXT *)&t_above;
    tl = (ENTROPY_CONTEXT *)&t_left;

    for (b = 16; b < 24; b+=4)
        cost += cost_coeffs_8x8(mb, x->block + b, PLANE_TYPE_UV,
1344
1345
                                ta + vp8_block2above_8x8[b],
                                tl + vp8_block2left_8x8[b]);
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365

    return cost;
}


static int rd_inter16x16_uv_8x8(VP8_COMP *cpi, MACROBLOCK *x, int *rate,
                            int *distortion, int fullpixel)
{
    ENCODEMB_INVOKE(IF_RTCD(&cpi->rtcd.encodemb), submbuv)(x->src_diff,
        x->src.u_buffer, x->src.v_buffer, x->e_mbd.predictor, x->src.uv_stride);

    vp8_transform_mbuv_8x8(x);

    vp8_quantize_mbuv_8x8(x);

    *rate       = rd_cost_mbuv_8x8(x);
    *distortion = ENCODEMB_INVOKE(&cpi->rtcd.encodemb, mbuverr)(x) / 4;

    return RDCOST(x->rdmult, x->rddiv, *rate, *distortion);
}
Yaowu Xu's avatar
Yaowu Xu committed
1366

1367
1368
1369
1370
1371
1372
1373
1374
1375
1376

static int rd_inter4x4_uv(VP8_COMP *cpi, MACROBLOCK *x, int *rate,
                          int *distortion, int fullpixel)
{
    vp8_build_inter4x4_predictors_mbuv(&x->e_mbd);
    ENCODEMB_INVOKE(IF_RTCD(&cpi->rtcd.encodemb), submbuv)(x->src_diff,
        x->src.u_buffer, x->src.v_buffer, x->e_mbd.predictor, x->src.uv_stride);

    vp8_transform_mbuv(x);
    vp8_quantize_mbuv(x);
John Koleszar's avatar
John Koleszar committed
1377
1378
1379
1380

    *rate       = rd_cost_mbuv(x);
    *distortion = ENCODEMB_INVOKE(&cpi->rtcd.encodemb, mbuverr)(x) / 4;

1381
    return RDCOST(x->rdmult, x->rddiv, *rate, *distortion);
John Koleszar's avatar
John Koleszar committed
1382
1383
}

1384
1385
1386
1387
1388
static void rd_pick_intra_mbuv_mode(VP8_COMP *cpi,
                                    MACROBLOCK *x,
                                    int *rate,
                                    int *rate_tokenonly,
                                    int *distortion)
John Koleszar's avatar
John Koleszar committed
1389
1390
1391
{
    MB_PREDICTION_MODE mode;
    MB_PREDICTION_MODE UNINITIALIZED_IS_SAFE(mode_selected);
1392
1393
1394
1395
#if CONFIG_COMP_INTRA_PRED
    MB_PREDICTION_MODE mode2;
    MB_PREDICTION_MODE UNINITIALIZED_IS_SAFE(mode2_selected);
#endif
John Koleszar's avatar
John Koleszar committed
1396
1397
1398
1399
1400
1401
    int best_rd = INT_MAX;
    int UNINITIALIZED_IS_SAFE(d), UNINITIALIZED_IS_SAFE(r);
    int rate_to;

    for (mode = DC_PRED; mode <= TM_PRED; mode++)
    {
1402
1403
1404
1405
#if CONFIG_COMP_INTRA_PRED
        for (mode2 = DC_PRED - 1; mode2 != TM_PRED + 1; mode2++)
        {
#endif
John Koleszar's avatar
John Koleszar committed
1406
1407
1408
1409
        int rate;
        int distortion;
        int this_rd;

1410
        x->e_mbd.mode_info_context->mbmi.uv_mode = mode;
1411
1412
1413
1414
1415
#if CONFIG_COMP_INTRA_PRED
            x->e_mbd.mode_info_context->mbmi.second_uv_mode = mode2;
            if (mode2 == (MB_PREDICTION_MODE) (DC_PRED - 1))
            {
#endif
1416
1417
        RECON_INVOKE(&cpi->rtcd.common->recon, build_intra_predictors_mbuv)
                     (&x->e_mbd);
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
#if CONFIG_COMP_INTRA_PRED
            }
            else
            {
                continue;
                RECON_INVOKE(&cpi->rtcd.common->recon, build_comp_intra_predictors_mbuv)
                    (&x->e_mbd);
            }
#endif

1428
1429
1430
1431
1432
        ENCODEMB_INVOKE(IF_RTCD(&cpi->rtcd.encodemb), submbuv)(x->src_diff,
                      x->src.u_buffer, x->src.v_buffer, x->e_mbd.predictor,
                      x->src.uv_stride);
        vp8_transform_mbuv(x);
        vp8_quantize_mbuv(x);
John Koleszar's avatar
John Koleszar committed
1433
1434

        rate_to = rd_cost_mbuv(x);
1435
        rate = rate_to + x->intra_uv_mode_cost[x->e_mbd.frame_type][x->e_mbd.mode_info_context->mbmi.uv_mode];
John Koleszar's avatar
John Koleszar committed
1436

1437
        distortion = ENCODEMB_INVOKE(&cpi->rtcd.encodemb, mbuverr)(x) / 4;
John Koleszar's avatar
John Koleszar committed
1438

1439
        this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);
John Koleszar's avatar
John Koleszar committed
1440
1441
1442
1443
1444
1445
1446
1447

        if (this_rd < best_rd)
        {
            best_rd = this_rd;
            d = distortion;
            r = rate;
            *rate_tokenonly = rate_to;
            mode_selected = mode;
1448
1449
1450
1451
#if CONFIG_COMP_INTRA_PRED
            mode2_selected = mode2;
        }
#endif
John Koleszar's avatar
John Koleszar committed
1452
1453
1454
1455
1456
1457
        }
    }

    *rate = r;
    *distortion = d;

1458
    x->e_mbd.mode_info_context->mbmi.uv_mode = mode_selected;
1459
1460
1461
#if CONFIG_COMP_INTRA_PRED
    x->e_mbd.mode_info_context->mbmi.second_uv_mode = mode2_selected;
#endif
John Koleszar's avatar
John Koleszar committed
1462
1463
}

1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
static void rd_pick_intra_mbuv_mode_8x8(VP8_COMP *cpi,
                                        MACROBLOCK *x,
                                        int *rate,
                                        int *rate_tokenonly,
                                        int *distortion)
{
    MB_PREDICTION_MODE mode;
    MB_PREDICTION_MODE UNINITIALIZED_IS_SAFE(mode_selected);
    int best_rd = INT_MAX;
    int UNINITIALIZED_IS_SAFE(d), UNINITIALIZED_IS_SAFE(r);
    int rate_to;

    for (mode = DC_PRED; mode <= TM_PRED; mode++)
    {
        int rate;
        int distortion;
        int this_rd;

        x->e_mbd.mode_info_context->mbmi.uv_mode = mode;
        RECON_INVOKE(&cpi->rtcd.common->recon, build_intra_predictors_mbuv)
                     (&x->e_mbd);
        ENCODEMB_INVOKE(IF_RTCD(&cpi->rtcd.encodemb), submbuv)(x->src_diff,
                      x->src.u_buffer, x->src.v_buffer, x->e_mbd.predictor,
                      x->src.uv_stride);
        vp8_transform_mbuv_8x8(x);

        vp8_quantize_mbuv_8x8(x);

        rate_to = rd_cost_mbuv_8x8(x);
        rate = rate_to + x->intra_uv_mode_cost[x->e_mbd.frame_type]
                        [x->e_mbd.mode_info_context->mbmi.uv_mode];

        distortion = ENCODEMB_INVOKE(&cpi->rtcd.encodemb, mbuverr)(x) / 4;
        this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);

        if (this_rd < best_rd)
        {
            best_rd = this_rd;
            d = distortion;
            r = rate;
            *rate_tokenonly = rate_to;
            mode_selected = mode;
        }
    }
    *rate = r;
    *distortion = d;
    x->e_mbd.mode_info_context->mbmi.uv_mode = mode_selected;
}

1513
int vp8_cost_mv_ref(VP8_COMP *cpi,
Yaowu Xu's avatar
Yaowu Xu committed
1514
1515
                    MB_PREDICTION_MODE m,
                    const int near_mv_ref_ct[4])
John Koleszar's avatar
John Koleszar committed
1516
{
Paul Wilkins's avatar
Paul Wilkins committed
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
    MACROBLOCKD *xd = &cpi->mb.e_mbd;
    int segment_id = xd->mode_info_context->mbmi.segment_id;

    // If the mode coding is done entirely at the segment level
    // we should not account for it at the per mb level in rd code.
    // Note that if the segment level coding is expanded from single mode
    // to multiple mode masks as per reference frame coding we will need
    // to do something different here.
    if ( !segfeature_active( xd, segment_id, SEG_LVL_MODE) )
    {
        VP8_COMMON *pc = &cpi->common;
1528

Paul Wilkins's avatar
Paul Wilkins committed
1529
1530
1531
1532
1533
1534
1535
1536
        vp8_prob p [VP8_MVREFS-1];
        assert(NEARESTMV <= m  &&  m <= SPLITMV);
        vp8_mv_ref_probs(pc, p, near_mv_ref_ct);
        return vp8_cost_token(vp8_mv_ref_tree, p<