encodemb.c 41.2 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
 */


#include "vpx_ports/config.h"
#include "encodemb.h"
John Koleszar's avatar
John Koleszar committed
14
#include "vp8/common/reconinter.h"
John Koleszar's avatar
John Koleszar committed
15
#include "quantize.h"
16
#include "tokenize.h"
John Koleszar's avatar
John Koleszar committed
17
18
19
#include "vp8/common/invtrans.h"
#include "vp8/common/recon.h"
#include "vp8/common/reconintra.h"
John Koleszar's avatar
John Koleszar committed
20
21
#include "dct.h"
#include "vpx_mem/vpx_mem.h"
Yunqing Wang's avatar
Yunqing Wang committed
22
#include "rdopt.h"
23
#include "vp8/common/systemdependent.h"
John Koleszar's avatar
John Koleszar committed
24
25
26
27
28
29

#if CONFIG_RUNTIME_CPU_DETECT
#define IF_RTCD(x) (x)
#else
#define IF_RTCD(x) NULL
#endif
30
31
32
33
34

#ifdef ENC_DEBUG
extern int enc_debug;
#endif

John Koleszar's avatar
John Koleszar committed
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
void vp8_subtract_b_c(BLOCK *be, BLOCKD *bd, int pitch)
{
    unsigned char *src_ptr = (*(be->base_src) + be->src);
    short *diff_ptr = be->src_diff;
    unsigned char *pred_ptr = bd->predictor;
    int src_stride = be->src_stride;

    int r, c;

    for (r = 0; r < 4; r++)
    {
        for (c = 0; c < 4; c++)
        {
            diff_ptr[c] = src_ptr[c] - pred_ptr[c];
        }

        diff_ptr += pitch;
        pred_ptr += pitch;
        src_ptr  += src_stride;
    }
}

Yaowu Xu's avatar
Yaowu Xu committed
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
void vp8_subtract_4b_c(BLOCK *be, BLOCKD *bd, int pitch)
{
    unsigned char *src_ptr = (*(be->base_src) + be->src);
    short *diff_ptr = be->src_diff;
    unsigned char *pred_ptr = bd->predictor;
    int src_stride = be->src_stride;
    int r, c;
    for (r = 0; r < 8; r++)
    {
        for (c = 0; c < 8; c++)
        {
            diff_ptr[c] = src_ptr[c] - pred_ptr[c];
        }
        diff_ptr += pitch;
        pred_ptr += pitch;
        src_ptr  += src_stride;
    }
}

John Koleszar's avatar
John Koleszar committed
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
void vp8_subtract_mbuv_c(short *diff, unsigned char *usrc, unsigned char *vsrc, unsigned char *pred, int stride)
{
    short *udiff = diff + 256;
    short *vdiff = diff + 320;
    unsigned char *upred = pred + 256;
    unsigned char *vpred = pred + 320;

    int r, c;

    for (r = 0; r < 8; r++)
    {
        for (c = 0; c < 8; c++)
        {
            udiff[c] = usrc[c] - upred[c];
        }

        udiff += 8;
        upred += 8;
        usrc  += stride;
    }

    for (r = 0; r < 8; r++)
    {
        for (c = 0; c < 8; c++)
        {
            vdiff[c] = vsrc[c] - vpred[c];
        }

        vdiff += 8;
        vpred += 8;
        vsrc  += stride;
    }
}

void vp8_subtract_mby_c(short *diff, unsigned char *src, unsigned char *pred, int stride)
{
    int r, c;

    for (r = 0; r < 16; r++)
    {
        for (c = 0; c < 16; c++)
        {
            diff[c] = src[c] - pred[c];
        }

        diff += 16;
        pred += 16;
        src  += stride;
    }
}

static void vp8_subtract_mb(const VP8_ENCODER_RTCD *rtcd, MACROBLOCK *x)
{
129
130
131
    BLOCK *b = &x->block[0];

    ENCODEMB_INVOKE(&rtcd->encodemb, submby)(x->src_diff, *(b->base_src), x->e_mbd.predictor, b->src_stride);
John Koleszar's avatar
John Koleszar committed
132
133
134
    ENCODEMB_INVOKE(&rtcd->encodemb, submbuv)(x->src_diff, x->src.u_buffer, x->src.v_buffer, x->e_mbd.predictor, x->src.uv_stride);
}

135
static void build_dcblock(MACROBLOCK *x)
John Koleszar's avatar
John Koleszar committed
136
137
138
139
140
141
142
143
144
{
    short *src_diff_ptr = &x->src_diff[384];
    int i;

    for (i = 0; i < 16; i++)
    {
        src_diff_ptr[i] = x->coeff[i * 16];
    }
}
Yaowu Xu's avatar
Yaowu Xu committed
145
#if CONFIG_T8X8
146
147
148
149
150
151
152
153
154
155
156
157
158
void vp8_build_dcblock_8x8(MACROBLOCK *x)
{
    short *src_diff_ptr = &x->src_diff[384];
    int i;
    for (i = 0; i < 16; i++)
    {
        src_diff_ptr[i] = 0;
    }
    src_diff_ptr[0] = x->coeff[0 * 16];
    src_diff_ptr[1] = x->coeff[4 * 16];
    src_diff_ptr[4] = x->coeff[8 * 16];
    src_diff_ptr[8] = x->coeff[12 * 16];
}
Yaowu Xu's avatar
Yaowu Xu committed
159
#endif
John Koleszar's avatar
John Koleszar committed
160
161
162
163
164
165
void vp8_transform_mbuv(MACROBLOCK *x)
{
    int i;

    for (i = 16; i < 24; i += 2)
    {
166
        x->vp8_short_fdct8x4(&x->block[i].src_diff[0],
Yaowu Xu's avatar
Yaowu Xu committed
167
            &x->block[i].coeff[0], 16);
John Koleszar's avatar
John Koleszar committed
168
169
170
    }
}

Yaowu Xu's avatar
Yaowu Xu committed
171

John Koleszar's avatar
John Koleszar committed
172
173
174
175
176
177
void vp8_transform_intra_mby(MACROBLOCK *x)
{
    int i;

    for (i = 0; i < 16; i += 2)
    {
Yaowu Xu's avatar
Yaowu Xu committed
178
179
        x->vp8_short_fdct8x4(&x->block[i].src_diff[0],
            &x->block[i].coeff[0], 32);
John Koleszar's avatar
John Koleszar committed
180
181
182
    }

    // build dc block from 16 y dc values
183
    build_dcblock(x);
John Koleszar's avatar
John Koleszar committed
184
185

    // do 2nd order transform on the dc block
Yaowu Xu's avatar
Yaowu Xu committed
186
187
    x->short_walsh4x4(&x->block[24].src_diff[0],
        &x->block[24].coeff[0], 8);
John Koleszar's avatar
John Koleszar committed
188
189
190
191

}


192
static void transform_mb(MACROBLOCK *x)
John Koleszar's avatar
John Koleszar committed
193
194
195
196
197
{
    int i;

    for (i = 0; i < 16; i += 2)
    {
Yaowu Xu's avatar
Yaowu Xu committed
198
199
        x->vp8_short_fdct8x4(&x->block[i].src_diff[0],
            &x->block[i].coeff[0], 32);
John Koleszar's avatar
John Koleszar committed
200
201
202
    }

    // build dc block from 16 y dc values
203
    if (x->e_mbd.mode_info_context->mbmi.mode != SPLITMV)
204
        build_dcblock(x);
John Koleszar's avatar
John Koleszar committed
205
206
207

    for (i = 16; i < 24; i += 2)
    {
Yaowu Xu's avatar
Yaowu Xu committed
208
209
        x->vp8_short_fdct8x4(&x->block[i].src_diff[0],
            &x->block[i].coeff[0], 16);
John Koleszar's avatar
John Koleszar committed
210
211
212
    }

    // do 2nd order transform on the dc block
213
    if (x->e_mbd.mode_info_context->mbmi.mode != SPLITMV)
Yaowu Xu's avatar
Yaowu Xu committed
214
215
        x->short_walsh4x4(&x->block[24].src_diff[0],
        &x->block[24].coeff[0], 8);
John Koleszar's avatar
John Koleszar committed
216
217
218

}

219
220

static void transform_mby(MACROBLOCK *x)
John Koleszar's avatar
John Koleszar committed
221
222
223
224
225
{
    int i;

    for (i = 0; i < 16; i += 2)
    {
Yaowu Xu's avatar
Yaowu Xu committed
226
227
        x->vp8_short_fdct8x4(&x->block[i].src_diff[0],
            &x->block[i].coeff[0], 32);
John Koleszar's avatar
John Koleszar committed
228
229
230
    }

    // build dc block from 16 y dc values
231
    if (x->e_mbd.mode_info_context->mbmi.mode != SPLITMV)
John Koleszar's avatar
John Koleszar committed
232
    {
233
        build_dcblock(x);
Yaowu Xu's avatar
Yaowu Xu committed
234
235
        x->short_walsh4x4(&x->block[24].src_diff[0],
            &x->block[24].coeff[0], 8);
John Koleszar's avatar
John Koleszar committed
236
237
238
    }
}

239
#if CONFIG_T8X8
John Koleszar's avatar
John Koleszar committed
240

241
242
243
void vp8_transform_mbuv_8x8(MACROBLOCK *x)
{
    int i;
John Koleszar's avatar
John Koleszar committed
244

245
246
    vp8_clear_system_state();

247
248
249
250
251
252
253
254
255
256
257
    for (i = 16; i < 24; i += 4)
    {
        x->vp8_short_fdct8x8(&x->block[i].src_diff[0],
            &x->block[i].coeff[0], 16);
    }
}


void vp8_transform_intra_mby_8x8(MACROBLOCK *x)//changed
{
    int i;
258

259
260
    vp8_clear_system_state();

261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
    for (i = 0; i < 9; i += 8)
    {
        x->vp8_short_fdct8x8(&x->block[i].src_diff[0],
            &x->block[i].coeff[0], 32);
    }
    for (i = 2; i < 11; i += 8)
    {
        x->vp8_short_fdct8x8(&x->block[i].src_diff[0],
            &x->block[i+2].coeff[0], 32);
    }
    // build dc block from 16 y dc values
    vp8_build_dcblock_8x8(x);
    //vp8_build_dcblock(x);

    // do 2nd order transform on the dc block
    x->short_fhaar2x2(&x->block[24].src_diff[0],
        &x->block[24].coeff[0], 8);

}


void vp8_transform_mb_8x8(MACROBLOCK *x)
{
    int i;

286
287
    vp8_clear_system_state();

288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
    for (i = 0; i < 9; i += 8)
    {
        x->vp8_short_fdct8x8(&x->block[i].src_diff[0],
            &x->block[i].coeff[0], 32);
    }
    for (i = 2; i < 11; i += 8)
    {
        x->vp8_short_fdct8x8(&x->block[i].src_diff[0],
            &x->block[i+2].coeff[0], 32);
    }
    // build dc block from 16 y dc values
    if (x->e_mbd.mode_info_context->mbmi.mode != B_PRED &&x->e_mbd.mode_info_context->mbmi.mode != SPLITMV)
        vp8_build_dcblock_8x8(x);
      //vp8_build_dcblock(x);

    for (i = 16; i < 24; i += 4)
    {
        x->vp8_short_fdct8x8(&x->block[i].src_diff[0],
            &x->block[i].coeff[0], 16);
    }

    // do 2nd order transform on the dc block
    if (x->e_mbd.mode_info_context->mbmi.mode != B_PRED &&x->e_mbd.mode_info_context->mbmi.mode != SPLITMV)
        x->short_fhaar2x2(&x->block[24].src_diff[0],
        &x->block[24].coeff[0], 8);
}

void vp8_transform_mby_8x8(MACROBLOCK *x)
{
    int i;

319
320
    vp8_clear_system_state();

321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
    for (i = 0; i < 9; i += 8)
    {
        x->vp8_short_fdct8x8(&x->block[i].src_diff[0],
            &x->block[i].coeff[0], 32);
    }
    for (i = 2; i < 11; i += 8)
    {
        x->vp8_short_fdct8x8(&x->block[i].src_diff[0],
            &x->block[i+2].coeff[0], 32);
    }
    // build dc block from 16 y dc values
    if (x->e_mbd.mode_info_context->mbmi.mode != SPLITMV)
    {
        //vp8_build_dcblock(x);
        vp8_build_dcblock_8x8(x);
        x->short_fhaar2x2(&x->block[24].src_diff[0],
            &x->block[24].coeff[0], 8);
    }
}

#endif

#define RDTRUNC(RM,DM,R,D) ( (128+(R)*(RM)) & 0xFF )
#define RDTRUNC_8x8(RM,DM,R,D) ( (128+(R)*(RM)) & 0xFF )
345
346
347
348
349
350
351
352
353
354
typedef struct vp8_token_state vp8_token_state;

struct vp8_token_state{
  int           rate;
  int           error;
  signed char   next;
  signed char   token;
  short         qc;
};

355
// TODO: experiments to find optimal multiple numbers
356
357
#define Y1_RD_MULT 4
#define UV_RD_MULT 2
358
#if !CONFIG_EXTEND_QRANGE
359
#define Y2_RD_MULT 16
360
361
362
#else
#define Y2_RD_MULT 4
#endif
363
364
365
366
367
368
369
370
371

static const int plane_rd_mult[4]=
{
    Y1_RD_MULT,
    Y2_RD_MULT,
    UV_RD_MULT,
    Y1_RD_MULT
};

372
373
374
static void optimize_b(MACROBLOCK *mb, int ib, int type,
                       ENTROPY_CONTEXT *a, ENTROPY_CONTEXT *l,
                       const VP8_ENCODER_RTCD *rtcd)
John Koleszar's avatar
John Koleszar committed
375
{
376
377
378
379
380
381
382
383
384
385
386
387
    BLOCK *b;
    BLOCKD *d;
    vp8_token_state tokens[17][2];
    unsigned best_mask[2];
    const short *dequant_ptr;
    const short *coeff_ptr;
    short *qcoeff_ptr;
    short *dqcoeff_ptr;
    int eob;
    int i0;
    int rc;
    int x;
Johann's avatar
Johann committed
388
    int sz = 0;
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
    int next;
    int rdmult;
    int rddiv;
    int final_eob;
    int rd_cost0;
    int rd_cost1;
    int rate0;
    int rate1;
    int error0;
    int error1;
    int t0;
    int t1;
    int best;
    int band;
    int pt;
404
405
    int i;
    int err_mult = plane_rd_mult[type];
406

407
408
    b = &mb->block[ib];
    d = &mb->e_mbd.block[ib];
409
410
411
412
413
414
415

    /* Enable this to test the effect of RDO as a replacement for the dynamic
     *  zero bin instead of an augmentation of it.
     */
#if 0
    vp8_strict_quantize_b(b, d);
#endif
John Koleszar's avatar
John Koleszar committed
416

417
418
    dequant_ptr = d->dequant;
    coeff_ptr = b->coeff;
419
420
421
422
423
424
    qcoeff_ptr = d->qcoeff;
    dqcoeff_ptr = d->dqcoeff;
    i0 = !type;
    eob = d->eob;

    /* Now set up a Viterbi trellis to evaluate alternative roundings. */
425
    rdmult = mb->rdmult * err_mult;
426
427
428
    if(mb->e_mbd.mode_info_context->mbmi.ref_frame==INTRA_FRAME)
        rdmult = (rdmult * 9)>>4;

429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
    rddiv = mb->rddiv;
    best_mask[0] = best_mask[1] = 0;
    /* Initialize the sentinel node of the trellis. */
    tokens[eob][0].rate = 0;
    tokens[eob][0].error = 0;
    tokens[eob][0].next = 16;
    tokens[eob][0].token = DCT_EOB_TOKEN;
    tokens[eob][0].qc = 0;
    *(tokens[eob] + 1) = *(tokens[eob] + 0);
    next = eob;
    for (i = eob; i-- > i0;)
    {
        int base_bits;
        int d2;
        int dx;

        rc = vp8_default_zig_zag1d[i];
        x = qcoeff_ptr[rc];
        /* Only add a trellis state for non-zero coefficients. */
        if (x)
John Koleszar's avatar
John Koleszar committed
449
        {
450
451
452
453
454
455
456
457
458
            int shortcut=0;
            error0 = tokens[next][0].error;
            error1 = tokens[next][1].error;
            /* Evaluate the first possibility for this state. */
            rate0 = tokens[next][0].rate;
            rate1 = tokens[next][1].rate;
            t0 = (vp8_dct_value_tokens_ptr + x)->Token;
            /* Consider both possible successor states. */
            if (next < 16)
John Koleszar's avatar
John Koleszar committed
459
            {
460
461
462
463
464
465
                band = vp8_coef_bands[i + 1];
                pt = vp8_prev_token_class[t0];
                rate0 +=
                    mb->token_costs[type][band][pt][tokens[next][0].token];
                rate1 +=
                    mb->token_costs[type][band][pt][tokens[next][1].token];
John Koleszar's avatar
John Koleszar committed
466
            }
467
468
469
            rd_cost0 = RDCOST(rdmult, rddiv, rate0, error0);
            rd_cost1 = RDCOST(rdmult, rddiv, rate1, error1);
            if (rd_cost0 == rd_cost1)
John Koleszar's avatar
John Koleszar committed
470
            {
471
472
                rd_cost0 = RDTRUNC(rdmult, rddiv, rate0, error0);
                rd_cost1 = RDTRUNC(rdmult, rddiv, rate1, error1);
John Koleszar's avatar
John Koleszar committed
473
            }
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
            /* And pick the best. */
            best = rd_cost1 < rd_cost0;
            base_bits = *(vp8_dct_value_cost_ptr + x);
            dx = dqcoeff_ptr[rc] - coeff_ptr[rc];
            d2 = dx*dx;
            tokens[i][0].rate = base_bits + (best ? rate1 : rate0);
            tokens[i][0].error = d2 + (best ? error1 : error0);
            tokens[i][0].next = next;
            tokens[i][0].token = t0;
            tokens[i][0].qc = x;
            best_mask[0] |= best << i;
            /* Evaluate the second possibility for this state. */
            rate0 = tokens[next][0].rate;
            rate1 = tokens[next][1].rate;

            if((abs(x)*dequant_ptr[rc]>abs(coeff_ptr[rc])) &&
               (abs(x)*dequant_ptr[rc]<abs(coeff_ptr[rc])+dequant_ptr[rc]))
                shortcut = 1;
            else
                shortcut = 0;

            if(shortcut)
John Koleszar's avatar
John Koleszar committed
496
            {
497
498
499
                sz = -(x < 0);
                x -= 2*sz + 1;
            }
John Koleszar's avatar
John Koleszar committed
500

501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
            /* Consider both possible successor states. */
            if (!x)
            {
                /* If we reduced this coefficient to zero, check to see if
                 *  we need to move the EOB back here.
                 */
                t0 = tokens[next][0].token == DCT_EOB_TOKEN ?
                    DCT_EOB_TOKEN : ZERO_TOKEN;
                t1 = tokens[next][1].token == DCT_EOB_TOKEN ?
                    DCT_EOB_TOKEN : ZERO_TOKEN;
            }
            else
            {
                t0=t1 = (vp8_dct_value_tokens_ptr + x)->Token;
            }
            if (next < 16)
            {
                band = vp8_coef_bands[i + 1];
                if(t0!=DCT_EOB_TOKEN)
John Koleszar's avatar
John Koleszar committed
520
                {
521
522
523
                    pt = vp8_prev_token_class[t0];
                    rate0 += mb->token_costs[type][band][pt][
                        tokens[next][0].token];
John Koleszar's avatar
John Koleszar committed
524
                }
525
                if(t1!=DCT_EOB_TOKEN)
John Koleszar's avatar
John Koleszar committed
526
                {
527
528
529
                    pt = vp8_prev_token_class[t1];
                    rate1 += mb->token_costs[type][band][pt][
                        tokens[next][1].token];
John Koleszar's avatar
John Koleszar committed
530
531
532
                }
            }

533
534
535
            rd_cost0 = RDCOST(rdmult, rddiv, rate0, error0);
            rd_cost1 = RDCOST(rdmult, rddiv, rate1, error1);
            if (rd_cost0 == rd_cost1)
John Koleszar's avatar
John Koleszar committed
536
            {
537
538
                rd_cost0 = RDTRUNC(rdmult, rddiv, rate0, error0);
                rd_cost1 = RDTRUNC(rdmult, rddiv, rate1, error1);
John Koleszar's avatar
John Koleszar committed
539
            }
540
541
542
            /* And pick the best. */
            best = rd_cost1 < rd_cost0;
            base_bits = *(vp8_dct_value_cost_ptr + x);
John Koleszar's avatar
John Koleszar committed
543

544
            if(shortcut)
John Koleszar's avatar
John Koleszar committed
545
            {
546
547
                dx -= (dequant_ptr[rc] + sz) ^ sz;
                d2 = dx*dx;
John Koleszar's avatar
John Koleszar committed
548
            }
549
550
551
552
553
554
555
556
            tokens[i][1].rate = base_bits + (best ? rate1 : rate0);
            tokens[i][1].error = d2 + (best ? error1 : error0);
            tokens[i][1].next = next;
            tokens[i][1].token =best?t1:t0;
            tokens[i][1].qc = x;
            best_mask[1] |= best << i;
            /* Finally, make this the new head of the trellis. */
            next = i;
John Koleszar's avatar
John Koleszar committed
557
        }
558
559
560
        /* There's no choice to make for a zero coefficient, so we don't
         *  add a new trellis node, but we do need to update the costs.
         */
John Koleszar's avatar
John Koleszar committed
561
562
        else
        {
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
            band = vp8_coef_bands[i + 1];
            t0 = tokens[next][0].token;
            t1 = tokens[next][1].token;
            /* Update the cost of each path if we're past the EOB token. */
            if (t0 != DCT_EOB_TOKEN)
            {
                tokens[next][0].rate += mb->token_costs[type][band][0][t0];
                tokens[next][0].token = ZERO_TOKEN;
            }
            if (t1 != DCT_EOB_TOKEN)
            {
                tokens[next][1].rate += mb->token_costs[type][band][0][t1];
                tokens[next][1].token = ZERO_TOKEN;
            }
            /* Don't update next, because we didn't add a new node. */
John Koleszar's avatar
John Koleszar committed
578
579
580
        }
    }

581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
    /* Now pick the best path through the whole trellis. */
    band = vp8_coef_bands[i + 1];
    VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);
    rate0 = tokens[next][0].rate;
    rate1 = tokens[next][1].rate;
    error0 = tokens[next][0].error;
    error1 = tokens[next][1].error;
    t0 = tokens[next][0].token;
    t1 = tokens[next][1].token;
    rate0 += mb->token_costs[type][band][pt][t0];
    rate1 += mb->token_costs[type][band][pt][t1];
    rd_cost0 = RDCOST(rdmult, rddiv, rate0, error0);
    rd_cost1 = RDCOST(rdmult, rddiv, rate1, error1);
    if (rd_cost0 == rd_cost1)
    {
        rd_cost0 = RDTRUNC(rdmult, rddiv, rate0, error0);
        rd_cost1 = RDTRUNC(rdmult, rddiv, rate1, error1);
    }
    best = rd_cost1 < rd_cost0;
    final_eob = i0 - 1;
    for (i = next; i < eob; i = next)
    {
        x = tokens[i][best].qc;
        if (x)
            final_eob = i;
        rc = vp8_default_zig_zag1d[i];
        qcoeff_ptr[rc] = x;
        dqcoeff_ptr[rc] = x * dequant_ptr[rc];
        next = tokens[i][best].next;
        best = (best_mask[best] >> i) & 1;
    }
    final_eob++;

    d->eob = final_eob;
    *a = *l = (d->eob != !type);
John Koleszar's avatar
John Koleszar committed
616
617
}

618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
#if CONFIG_EXTEND_QRANGE
    /**************************************************************************
    our inverse hadamard transform effectively is weighted sum of all 16 inputs
    with weight either 1 or -1. It has a last stage scaling of (sum+1)>>2. And
    dc only idct is (dc+16)>>5. So if all the sums are between -65 and 63 the
    output after inverse wht and idct will be all zero. A sum of absolute value
    smaller than 65 guarantees all 16 different (+1/-1) weighted sums in wht
    fall between -65 and +65.
    **************************************************************************/
#define SUM_2ND_COEFF_THRESH 65
#else
    /**************************************************************************
    our inverse hadamard transform effectively is weighted sum of all 16 inputs
    with weight either 1 or -1. It has a last stage scaling of (sum+3)>>3. And
    dc only idct is (dc+4)>>3. So if all the sums are between -35 and 29, the
    output after inverse wht and idct will be all zero. A sum of absolute value
    smaller than 35 guarantees all 16 different (+1/-1) weighted sums in wht
    fall between -35 and +35.
    **************************************************************************/
#define SUM_2ND_COEFF_THRESH 35

#endif

641
642
static void check_reset_2nd_coeffs(MACROBLOCKD *x, int type,
                                   ENTROPY_CONTEXT *a, ENTROPY_CONTEXT *l)
643
644
645
646
{
    int sum=0;
    int i;
    BLOCKD *bd = &x->block[24];
647
648
    if(bd->dequant[0]>=SUM_2ND_COEFF_THRESH
        && bd->dequant[1]>=SUM_2ND_COEFF_THRESH)
649
        return;
650
651
652
653
654

    for(i=0;i<bd->eob;i++)
    {
        int coef = bd->dqcoeff[vp8_default_zig_zag1d[i]];
        sum+= (coef>=0)?coef:-coef;
655
        if(sum>=SUM_2ND_COEFF_THRESH)
656
            return;
657
    }
658

659
    if(sum < SUM_2ND_COEFF_THRESH)
660
    {
661
662
663
664
665
666
        for(i=0;i<bd->eob;i++)
        {
            int rc = vp8_default_zig_zag1d[i];
            bd->qcoeff[rc]=0;
            bd->dqcoeff[rc]=0;
        }
667
        bd->eob = 0;
668
        *a = *l = (bd->eob != !type);
669
670
    }
}
671
#define SUM_2ND_COEFF_THRESH_8X8 32
672
673
static void check_reset_8x8_2nd_coeffs(MACROBLOCKD *x, int type,
                                   ENTROPY_CONTEXT *a, ENTROPY_CONTEXT *l)
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
{
    int sum=0;
    int i;
    BLOCKD *bd = &x->block[24];
    int coef;

    coef = bd->dqcoeff[0];
    sum+= (coef>=0)?coef:-coef;
    coef = bd->dqcoeff[1];
    sum+= (coef>=0)?coef:-coef;
    coef = bd->dqcoeff[4];
    sum+= (coef>=0)?coef:-coef;
    coef = bd->dqcoeff[8];
    sum+= (coef>=0)?coef:-coef;

689
    if(sum < SUM_2ND_COEFF_THRESH_8X8)
690
691
692
693
694
695
696
697
698
699
    {
        bd->qcoeff[0] = 0;
        bd->dqcoeff[0] = 0;
        bd->qcoeff[1] = 0;
        bd->dqcoeff[1] = 0;
        bd->qcoeff[4] = 0;
        bd->dqcoeff[4] = 0;
        bd->qcoeff[8] = 0;
        bd->dqcoeff[8] = 0;
        bd->eob = 0;
700
        *a = *l = (bd->eob != !type);
701
702
703
704
705
    }
}



706
static void optimize_mb(MACROBLOCK *x, const VP8_ENCODER_RTCD *rtcd)
John Koleszar's avatar
John Koleszar committed
707
708
{
    int b;
709
710
    int type;
    int has_2nd_order;
711
712
713
714
715
716
717
718
719
    ENTROPY_CONTEXT_PLANES t_above, t_left;
    ENTROPY_CONTEXT *ta;
    ENTROPY_CONTEXT *tl;

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

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

721
    has_2nd_order = (x->e_mbd.mode_info_context->mbmi.mode != B_PRED
722
        &&x->e_mbd.mode_info_context->mbmi.mode != I8X8_PRED
723
        && x->e_mbd.mode_info_context->mbmi.mode != SPLITMV);
Scott LaVarnway's avatar
Scott LaVarnway committed
724
    type = has_2nd_order ? PLANE_TYPE_Y_NO_DC : PLANE_TYPE_Y_WITH_DC;
John Koleszar's avatar
John Koleszar committed
725
726
727

    for (b = 0; b < 16; b++)
    {
728
        optimize_b(x, b, type,
729
            ta + vp8_block2above[b], tl + vp8_block2left[b], rtcd);
John Koleszar's avatar
John Koleszar committed
730
731
    }

Scott LaVarnway's avatar
Scott LaVarnway committed
732
    for (b = 16; b < 24; b++)
John Koleszar's avatar
John Koleszar committed
733
    {
734
        optimize_b(x, b, PLANE_TYPE_UV,
735
            ta + vp8_block2above[b], tl + vp8_block2left[b], rtcd);
John Koleszar's avatar
John Koleszar committed
736
737
    }

738
739
    if (has_2nd_order)
    {
740
        b=24;
741
        optimize_b(x, b, PLANE_TYPE_Y2,
742
            ta + vp8_block2above[b], tl + vp8_block2left[b], rtcd);
743
744
        check_reset_2nd_coeffs(&x->e_mbd, PLANE_TYPE_Y2,
            ta + vp8_block2above[b], tl + vp8_block2left[b]);
John Koleszar's avatar
John Koleszar committed
745
746
747
748
749
750
751
    }
}


void vp8_optimize_mby(MACROBLOCK *x, const VP8_ENCODER_RTCD *rtcd)
{
    int b;
752
753
    int type;
    int has_2nd_order;
John Koleszar's avatar
John Koleszar committed
754

755
756
757
758
759
    ENTROPY_CONTEXT_PLANES t_above, t_left;
    ENTROPY_CONTEXT *ta;
    ENTROPY_CONTEXT *tl;

    if (!x->e_mbd.above_context)
John Koleszar's avatar
John Koleszar committed
760
761
        return;

762
    if (!x->e_mbd.left_context)
John Koleszar's avatar
John Koleszar committed
763
        return;
764
765
766
767
768
769
770

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

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

771
    has_2nd_order = (x->e_mbd.mode_info_context->mbmi.mode != B_PRED
772
        &&x->e_mbd.mode_info_context->mbmi.mode != I8X8_PRED
773
        && x->e_mbd.mode_info_context->mbmi.mode != SPLITMV);
Scott LaVarnway's avatar
Scott LaVarnway committed
774
    type = has_2nd_order ? PLANE_TYPE_Y_NO_DC : PLANE_TYPE_Y_WITH_DC;
John Koleszar's avatar
John Koleszar committed
775
776
777

    for (b = 0; b < 16; b++)
    {
778
        optimize_b(x, b, type,
779
        ta + vp8_block2above[b], tl + vp8_block2left[b], rtcd);
John Koleszar's avatar
John Koleszar committed
780
781
    }

782

783
784
    if (has_2nd_order)
    {
785
        b=24;
786
        optimize_b(x, b, PLANE_TYPE_Y2,
787
            ta + vp8_block2above[b], tl + vp8_block2left[b], rtcd);
788
789
        check_reset_2nd_coeffs(&x->e_mbd, PLANE_TYPE_Y2,
            ta + vp8_block2above[b], tl + vp8_block2left[b]);
790
    }
John Koleszar's avatar
John Koleszar committed
791
792
793
794
795
}

void vp8_optimize_mbuv(MACROBLOCK *x, const VP8_ENCODER_RTCD *rtcd)
{
    int b;
796
797
798
    ENTROPY_CONTEXT_PLANES t_above, t_left;
    ENTROPY_CONTEXT *ta;
    ENTROPY_CONTEXT *tl;
John Koleszar's avatar
John Koleszar committed
799

800
    if (!x->e_mbd.above_context)
John Koleszar's avatar
John Koleszar committed
801
802
        return;

803
    if (!x->e_mbd.left_context)
John Koleszar's avatar
John Koleszar committed
804
805
        return;

806
807
    vpx_memcpy(&t_above, x->e_mbd.above_context, sizeof(ENTROPY_CONTEXT_PLANES));
    vpx_memcpy(&t_left, x->e_mbd.left_context, sizeof(ENTROPY_CONTEXT_PLANES));
John Koleszar's avatar
John Koleszar committed
808

809
810
    ta = (ENTROPY_CONTEXT *)&t_above;
    tl = (ENTROPY_CONTEXT *)&t_left;
John Koleszar's avatar
John Koleszar committed
811

Scott LaVarnway's avatar
Scott LaVarnway committed
812
    for (b = 16; b < 24; b++)
John Koleszar's avatar
John Koleszar committed
813
    {
814
        optimize_b(x, b, PLANE_TYPE_UV,
815
            ta + vp8_block2above[b], tl + vp8_block2left[b], rtcd);
John Koleszar's avatar
John Koleszar committed
816
817
818
    }
}

819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
#if CONFIG_T8X8
void optimize_b_8x8(MACROBLOCK *mb, int i, int type,
                    ENTROPY_CONTEXT *a, ENTROPY_CONTEXT *l,
                    ENTROPY_CONTEXT *a1, ENTROPY_CONTEXT *l1,
                    const VP8_ENCODER_RTCD *rtcd)
{
    BLOCK *b;
    BLOCKD *d;
    vp8_token_state tokens[65][2];
    unsigned best_mask[2];
    const short *dequant_ptr;
    const short *coeff_ptr;
    short *qcoeff_ptr;
    short *dqcoeff_ptr;
    int eob;
    int i0;
    int rc;
    int x;
    int sz = 0;
    int next;
    int rdmult;
    int rddiv;
    int final_eob;
    int rd_cost0;
    int rd_cost1;
    int rate0;
    int rate1;
    int error0;
    int error1;
    int t0;
    int t1;
    int best;
    int band;
    int pt;
853
    int err_mult = plane_rd_mult[type];
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872

    b = &mb->block[i];
    d = &mb->e_mbd.block[i];

    /* Enable this to test the effect of RDO as a replacement for the dynamic
     *  zero bin instead of an augmentation of it.
     */
#if 0
    vp8_strict_quantize_b(b, d);
#endif

    dequant_ptr = d->dequant;
    coeff_ptr = b->coeff;
    qcoeff_ptr = d->qcoeff;
    dqcoeff_ptr = d->dqcoeff;
    i0 = !type;
    eob = d->eob;

    /* Now set up a Viterbi trellis to evaluate alternative roundings. */
873
874
875
    rdmult = mb->rdmult * err_mult;
    if(mb->e_mbd.mode_info_context->mbmi.ref_frame==INTRA_FRAME)
        rdmult = (rdmult * 9)>>4;
876
877
878
879
880
881
882
883
884
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
    rddiv = mb->rddiv;
    best_mask[0] = best_mask[1] = 0;
    /* Initialize the sentinel node of the trellis. */
    tokens[eob][0].rate = 0;
    tokens[eob][0].error = 0;
    tokens[eob][0].next = 64;
    tokens[eob][0].token = DCT_EOB_TOKEN;
    tokens[eob][0].qc = 0;
    *(tokens[eob] + 1) = *(tokens[eob] + 0);
    next = eob;
    for (i = eob; i-- > i0;)
    {
        int base_bits;
        int d2;
        int dx;

        rc = vp8_default_zig_zag1d_8x8[i];
        x = qcoeff_ptr[rc];
        /* Only add a trellis state for non-zero coefficients. */
        if (x)
        {
            int shortcut=0;
            error0 = tokens[next][0].error;
            error1 = tokens[next][1].error;
            /* Evaluate the first possibility for this state. */
            rate0 = tokens[next][0].rate;
            rate1 = tokens[next][1].rate;
            t0 = (vp8_dct_value_tokens_ptr + x)->Token;
            /* Consider both possible successor states. */
            if (next < 64)
            {
                band = vp8_coef_bands_8x8[i + 1];
                pt = vp8_prev_token_class[t0];
                rate0 +=
910
                    mb->token_costs_8x8[type][band][pt][tokens[next][0].token];
911
                rate1 +=
912
                    mb->token_costs_8x8[type][band][pt][tokens[next][1].token];
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
            }
            rd_cost0 = RDCOST_8x8(rdmult, rddiv, rate0, error0);
            rd_cost1 = RDCOST_8x8(rdmult, rddiv, rate1, error1);
            if (rd_cost0 == rd_cost1)
            {
                rd_cost0 = RDTRUNC_8x8(rdmult, rddiv, rate0, error0);
                rd_cost1 = RDTRUNC_8x8(rdmult, rddiv, rate1, error1);
            }
            /* And pick the best. */
            best = rd_cost1 < rd_cost0;
            base_bits = *(vp8_dct_value_cost_ptr + x);
            dx = dqcoeff_ptr[rc] - coeff_ptr[rc];
            d2 = dx*dx;
            tokens[i][0].rate = base_bits + (best ? rate1 : rate0);
            tokens[i][0].error = d2 + (best ? error1 : error0);
            tokens[i][0].next = next;
            tokens[i][0].token = t0;
            tokens[i][0].qc = x;
            best_mask[0] |= best << i;
            /* Evaluate the second possibility for this state. */
            rate0 = tokens[next][0].rate;
            rate1 = tokens[next][1].rate;

            if((abs(x)*dequant_ptr[rc!=0]>abs(coeff_ptr[rc])) &&
               (abs(x)*dequant_ptr[rc!=0]<abs(coeff_ptr[rc])+dequant_ptr[rc!=0]))
                shortcut = 1;
            else
                shortcut = 0;

            if(shortcut)
            {
                sz = -(x < 0);
                x -= 2*sz + 1;
            }

            /* Consider both possible successor states. */
            if (!x)
            {
                /* If we reduced this coefficient to zero, check to see if
                 *  we need to move the EOB back here.
                 */
                t0 = tokens[next][0].token == DCT_EOB_TOKEN ?
                    DCT_EOB_TOKEN : ZERO_TOKEN;
                t1 = tokens[next][1].token == DCT_EOB_TOKEN ?
                    DCT_EOB_TOKEN : ZERO_TOKEN;
            }
            else
            {
                t0=t1 = (vp8_dct_value_tokens_ptr + x)->Token;
            }
            if (next < 64)
            {
                band = vp8_coef_bands_8x8[i + 1];
                if(t0!=DCT_EOB_TOKEN)
                {
                    pt = vp8_prev_token_class[t0];
969
                    rate0 += mb->token_costs_8x8[type][band][pt][
970
971
972
973
974
                        tokens[next][0].token];
                }
                if(t1!=DCT_EOB_TOKEN)
                {
                    pt = vp8_prev_token_class[t1];
975
                    rate1 += mb->token_costs_8x8[type][band][pt][
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
                        tokens[next][1].token];
                }
            }

            rd_cost0 = RDCOST_8x8(rdmult, rddiv, rate0, error0);
            rd_cost1 = RDCOST_8x8(rdmult, rddiv, rate1, error1);
            if (rd_cost0 == rd_cost1)
            {
                rd_cost0 = RDTRUNC_8x8(rdmult, rddiv, rate0, error0);
                rd_cost1 = RDTRUNC_8x8(rdmult, rddiv, rate1, error1);
            }
            /* And pick the best. */
            best = rd_cost1 < rd_cost0;
            base_bits = *(vp8_dct_value_cost_ptr + x);

            if(shortcut)
            {
                dx -= (dequant_ptr[rc!=0] + sz) ^ sz;
                d2 = dx*dx;
            }
            tokens[i][1].rate = base_bits + (best ? rate1 : rate0);
            tokens[i][1].error = d2 + (best ? error1 : error0);
            tokens[i][1].next = next;
            tokens[i][1].token =best?t1:t0;
            tokens[i][1].qc = x;
            best_mask[1] |= best << i;
            /* Finally, make this the new head of the trellis. */
            next = i;
        }
        /* There's no choice to make for a zero coefficient, so we don't
         *  add a new trellis node, but we do need to update the costs.
         */
        else
        {
            band = vp8_coef_bands_8x8[i + 1];
            t0 = tokens[next][0].token;
            t1 = tokens[next][1].token;
            /* Update the cost of each path if we're past the EOB token. */
            if (t0 != DCT_EOB_TOKEN)
            {
1016
                tokens[next][0].rate += mb->token_costs_8x8[type][band][0][t0];
1017
1018
1019
1020
                tokens[next][0].token = ZERO_TOKEN;
            }
            if (t1 != DCT_EOB_TOKEN)
            {
1021
                tokens[next][1].rate += mb->token_costs_8x8[type][band][0][t1];
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
                tokens[next][1].token = ZERO_TOKEN;
            }
            /* Don't update next, because we didn't add a new node. */
        }
    }

    /* Now pick the best path through the whole trellis. */
    band = vp8_coef_bands_8x8[i + 1];
    VP8_COMBINEENTROPYCONTEXTS_8x8(pt, *a, *l, *a1, *l1);
    rate0 = tokens[next][0].rate;
    rate1 = tokens[next][1].rate;
    error0 = tokens[next][0].error;
    error1 = tokens[next][1].error;
    t0 = tokens[next][0].token;
    t1 = tokens[next][1].token;
1037
1038
    rate0 += mb->token_costs_8x8[type][band][pt][t0];
    rate1 += mb->token_costs_8x8[type][band][pt][t1];
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
    rd_cost0 = RDCOST_8x8(rdmult, rddiv, rate0, error0);
    rd_cost1 = RDCOST_8x8(rdmult, rddiv, rate1, error1);
    if (rd_cost0 == rd_cost1)
    {
        rd_cost0 = RDTRUNC_8x8(rdmult, rddiv, rate0, error0);
        rd_cost1 = RDTRUNC_8x8(rdmult, rddiv, rate1, error1);
    }
    best = rd_cost1 < rd_cost0;
    final_eob = i0 - 1;
    for (i = next; i < eob; i = next)
    {
        x = tokens[i][best].qc;
        if (x)
            final_eob = i;
        rc = vp8_default_zig_zag1d_8x8[i];
        qcoeff_ptr[rc] = x;
1055
#if !CONFIG_EXTEND_QRANGE
1056
        dqcoeff_ptr[rc] = x * dequant_ptr[rc!=0];
1057
1058
1059
1060
#else
        dqcoeff_ptr[rc] = (x * dequant_ptr[rc!=0]+2)>>2;
#endif

1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
        next = tokens[i][best].next;
        best = (best_mask[best] >> i) & 1;
    }
    final_eob++;

    d->eob = final_eob;
    *a = *l = (d->eob != !type);

}

void optimize_mb_8x8(MACROBLOCK *x, const VP8_ENCODER_RTCD *rtcd)
{
    int b;
    int type;
    int has_2nd_order;
    ENTROPY_CONTEXT_PLANES t_above, t_left;
    ENTROPY_CONTEXT *ta;
    ENTROPY_CONTEXT *tl;

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

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

    has_2nd_order = (x->e_mbd.mode_info_context->mbmi.mode != B_PRED
1087
        &&x->e_mbd.mode_info_context->mbmi.mode != I8X8_PRED
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
        && x->e_mbd.mode_info_context->mbmi.mode != SPLITMV);
    type = has_2nd_order ? 0 : 3;

    for (b = 0; b < 16; b+=4)
    {
        optimize_b_8x8(x, b, type,
            ta + vp8_block2above[b], tl + vp8_block2left[b],
            ta + vp8_block2above[b+1], tl + vp8_block2left[b+4],
            rtcd);

        if(b==0)
        {
          *(ta + vp8_block2above[1]) = *(ta + vp8_block2above[4]) = *(ta + vp8_block2above[5]) = *(ta + vp8_block2above[b]);
          *(tl + vp8_block2left[1]) = *(tl + vp8_block2left[4]) = *(tl + vp8_block2left[5]) = *(tl + vp8_block2left[b]);
        }
        else if(b==4)
        {
          *(ta + vp8_block2above[2]) = *(ta + vp8_block2above[3]) = *(ta + vp8_block2above[6]) = *(ta + vp8_block2above[7]) = *(ta + vp8_block2above[b]);
          *(tl + vp8_block2left[2]) = *(tl + vp8_block2left[3]) = *(tl + vp8_block2left[6]) = *(tl + vp8_block2left[7]) = *(tl + vp8_block2left[b]);
          *(ta + vp8_block2above[4]) = *(ta + vp8_block2above[1]);
          *(tl + vp8_block2left[4]) = *(tl + vp8_block2left[1]);
        }
        else if(b==8)
        {
          *(ta + vp8_block2above[9]) = *(ta + vp8_block2above[12]) = *(ta + vp8_block2above[13]) = *(ta + vp8_block2above[b]);
          *(tl + vp8_block2left[9]) = *(tl + vp8_block2left[12]) = *(tl + vp8_block2left[13]) = *(tl + vp8_block2left[b]);

        }
        else if(b==12)
        {
          *(ta + vp8_block2above[10]) = *(ta + vp8_block2above[11]) = *(ta + vp8_block2above[14]) = *(ta + vp8_block2above[15]) = *(ta + vp8_block2above[b]);
          *(tl + vp8_block2left[10]) = *(tl + vp8_block2left[11]) = *(tl + vp8_block2left[14]) = *(tl + vp8_block2left[15]) = *(tl + vp8_block2left[b]);
          *(ta + vp8_block2above[12]) = *(ta + vp8_block2above[8]);
          *(tl + vp8_block2left[12]) = *(tl + vp8_block2left[8]);

        }



    }

    for (b = 16; b < 20; b+=4)
    {
        optimize_b_8x8(x, b, PLANE_TYPE_UV, //vp8_block2type[b],
            ta + vp8_block2above[b], tl + vp8_block2left[b],
            ta + vp8_block2above[b+1], tl + vp8_block2left[b+2],
            rtcd);
        *(ta + vp8_block2above[b+1]) = *(ta + vp8_block2above[b+2]) = *(ta + vp8_block2above[b+3]) =
            *(ta + vp8_block2above[b]);
        *(tl + vp8_block2left[b+1]) = *(tl + vp8_block2left[b+2]) = *(tl + vp8_block2left[b+3]) =
            *(tl + vp8_block2left[b]);

    }

    for (b = 20; b < 24; b+=4)
    {
        optimize_b_8x8(x, b, PLANE_TYPE_UV, //vp8_block2type[b],
            ta + vp8_block2above[b], tl + vp8_block2left[b],
            ta + vp8_block2above[b+1], tl + vp8_block2left[b+2],
            rtcd);
        *(ta + vp8_block2above[b+1]) = *(ta + vp8_block2above[b+2]) = *(ta + vp8_block2above[b+3]) =
            *(ta + vp8_block2above[b]);
        *(tl + vp8_block2left[b+1]) = *(tl + vp8_block2left[b+2]) = *(tl + vp8_block2left[b+3]) =
            *(tl + vp8_block2left[b]);

    }
1154

1155
    //8x8 always have 2nd roder haar block
1156
1157
1158
    check_reset_8x8_2nd_coeffs(&x->e_mbd, PLANE_TYPE_Y2,
            ta + vp8_block2above[24], tl + vp8_block2left[24]);

1159

1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
}

void vp8_optimize_mby_8x8(MACROBLOCK *x, const VP8_ENCODER_RTCD *rtcd)
{
    int b;
    int type;
    int has_2nd_order;

    ENTROPY_CONTEXT_PLANES t_above, t_left;
    ENTROPY_CONTEXT *ta;
    ENTROPY_CONTEXT *tl;

1172

1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
    if (!x->e_mbd.above_context)
        return;

    if (!x->e_mbd.left_context)
        return;

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

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

    has_2nd_order = (x->e_mbd.mode_info_context->mbmi.mode != B_PRED
1186
        && x->e_mbd.mode_info_context->mbmi.mode != I8X8_PRED
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
        && x->e_mbd.mode_info_context->mbmi.mode != SPLITMV);
    type = has_2nd_order ? 0 : 3;

    for (b = 0; b < 16; b+=4)
    {
        optimize_b_8x8(x, b, type,
        ta + vp8_block2above[b], tl + vp8_block2left[b],
        ta + vp8_block2above[b+1], tl + vp8_block2left[b+4],
        rtcd);
        if(b==0)
        {
          *(ta + vp8_block2above[1]) = *(ta + vp8_block2above[4]) = *(ta + vp8_block2above[5]) = *(ta + vp8_block2above[b]);
          *(tl + vp8_block2left[1]) = *(tl + vp8_block2left[4]) = *(tl + vp8_block2left[5]) = *(tl + vp8_block2left[b]);
        }
        else if(b==4)
        {
          *(ta + vp8_block2above[2]) = *(ta + vp8_block2above[3]) = *(ta + vp8_block2above[6]) = *(ta + vp8_block2above[7]) = *(ta + vp8_block2above[b]);
          *(tl + vp8_block2left[2]) = *(tl + vp8_block2left[3]) = *(tl + vp8_block2left[6]) = *(tl + vp8_block2left[7]) = *(tl + vp8_block2left[b]);
          *(ta + vp8_block2above[4]) = *(ta + vp8_block2above[1]);
          *(tl + vp8_block2left[4]) = *(tl + vp8_block2left[1]);
        }
        else if(b==8)
        {
          *(ta + vp8_block2above[9]) = *(ta + vp8_block2above[12]) = *(ta + vp8_block2above[13]) = *(ta + vp8_block2above[b]);
          *(tl + vp8_block2left[9]) = *(tl + vp8_block2left[12]) = *(tl + vp8_block2left[13]) = *(tl + vp8_block2left[b]);

        }
        else if(b==12)
        {
          *(ta + vp8_block2above[10]) = *(ta + vp8_block2above[11]) = *(ta + vp8_block2above[14]) = *(ta + vp8_block2above[15]) = *(ta + vp8_block2above[b]);
          *(tl + vp8_block2left[10]) = *(tl + vp8_block2left[11]) = *(tl + vp8_block2left[14]) = *(tl + vp8_block2left[15]) = *(tl + vp8_block2left[b]);
          *(ta + vp8_block2above[12]) = *(ta + vp8_block2above[8]);
          *(tl + vp8_block2left[12]) = *(tl + vp8_block2left[8]);

        }


    }
1225
    //8x8 always have 2nd roder haar block
1226
1227
    check_reset_8x8_2nd_coeffs(&x->e_mbd, PLANE_TYPE_Y2,
            ta + vp8_block2above[24], tl + vp8_block2left[24]);
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
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
}

void vp8_optimize_mbuv_8x8(MACROBLOCK *x, const VP8_ENCODER_RTCD *rtcd)
{
    int b;
    ENTROPY_CONTEXT_PLANES t_above, t_left;
    ENTROPY_CONTEXT *ta;
    ENTROPY_CONTEXT *tl;

    if (!x->e_mbd.above_context)
        return;

    if (!x->e_mbd.left_context)
        return;

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

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

    for (b = 16; b < 20; b+=4)
    {
        optimize_b_8x8(x, b, PLANE_TYPE_UV, //vp8_block2type[b],
            ta + vp8_block2above[b], tl + vp8_block2left[b],
            ta + vp8_block2above[b+1], tl + vp8_block2left[b+2],
            rtcd);
        *(ta + vp8_block2above[b+1]) = *(ta + vp8_block2above[b+2]) = *(ta + vp8_block2above[b+3]) =
            *(ta + vp8_block2above[b]);
        *(tl + vp8_block2left[b+1]) = *(tl + vp8_block2left[b+2]) = *(tl + vp8_block2left[b+3]) =
            *(tl + vp8_block2left[b]);

    }

    for (b = 20; b < 24; b+=4)
    {
        optimize_b_8x8(x, b, PLANE_TYPE_UV, //vp8_block2type[b],
            ta + vp8_block2above[b], tl + vp8_block2left[b],
            ta + vp8_block2above[b+1], tl + vp8_block2left[b+2],
            rtcd);
        *(ta + vp8_block2above[b+1]) = *(ta + vp8_block2above[b+2]) = *(ta + vp8_block2above[b+3]) =
            *(ta + vp8_block2above[b]);
        *(tl + vp8_block2left[b+1]) = *(tl + vp8_block2left[b+2]) = *(tl + vp8_block2left[b+3]) =
            *(tl + vp8_block2left[b]);

    }

}
#endif

John Koleszar's avatar
John Koleszar committed
1279
1280
void vp8_encode_inter16x16(const VP8_ENCODER_RTCD *rtcd, MACROBLOCK *x)
{
Paul Wilkins's avatar
Paul Wilkins committed
1281
1282
1283
#if CONFIG_T8X8
    int tx_type = get_seg_tx_type(&x->e_mbd,
                                  x->e_mbd.mode_info_context->mbmi.segment_id);
1284
    x->e_mbd.mode_info_context->mbmi.txfm_size = tx_type;
Paul Wilkins's avatar
Paul Wilkins committed
1285
1286
#endif

John Koleszar's avatar
John Koleszar committed
1287
1288
1289
1290
    vp8_build_inter_predictors_mb(&x->e_mbd);

    vp8_subtract_mb(rtcd, x);

1291
#if  CONFIG_T8X8
Paul Wilkins's avatar
Paul Wilkins committed
1292
    if( tx_type == TX_8X8 )
1293
1294
1295
1296
         vp8_transform_mb_8x8(x);
    else
#endif
         transform_mb(x);
John Koleszar's avatar
John Koleszar committed
1297

1298
#if  CONFIG_T8X8
Paul Wilkins's avatar
Paul Wilkins committed
1299
    if( tx_type == TX_8X8 )
1300
1301
1302
1303
        vp8_quantize_mb_8x8(x);
    else
#endif
        vp8_quantize_mb(x);
John Koleszar's avatar
John Koleszar committed
1304

1305
    if (x->optimize)
1306
1307
    {
#if CONFIG_T8X8
Paul Wilkins's avatar
Paul Wilkins committed
1308
      if( tx_type == TX_8X8 )
1309
1310
1311
        optimize_mb_8x8(x, rtcd);
      else
#endif
1312
        optimize_mb(x, rtcd);
1313
    }
John Koleszar's avatar
John Koleszar committed
1314

1315
#if CONFIG_T8X8
Paul Wilkins's avatar
Paul Wilkins committed
1316
    if( tx_type == TX_8X8 )
1317
1318
1319
1320
        vp8_inverse_transform_mb_8x8(IF_RTCD(&rtcd->common->idct), &x->e_mbd);
    else
#endif
        vp8_inverse_transform_mb(IF_RTCD(&rtcd->common->idct), &x->e_mbd);
Paul Wilkins's avatar
Paul Wilkins committed
1321
1322
1323
1324

#if CONFIG_T8X8
    if( tx_type == TX_8X8 )
    {
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
#ifdef ENC_DEBUG
        if (enc_debug)
        {
          int i;
          printf("qcoeff:\n");
          printf("%d %d:\n", x->e_mbd.mb_to_left_edge, x->e_mbd.mb_to_top_edge);
          for (i =0; i<400; i++) {
            printf("%3d ", x->e_mbd.qcoeff[i]);
            if (i%16 == 15) printf("\n");
          }
          printf("dqcoeff:\n");
          for (i =0; i<400; i++) {
            printf("%3d ", x->e_mbd.dqcoeff[i]);
            if (i%16 == 15) printf("\n");
          }
          printf("diff:\n");
          for (i =0; i<400; i++) {
            printf("%3d ", x->e_mbd.diff[i]);
            if (i%16 == 15) printf("\n");
          }
          printf("predictor:\n");
          for (i =0; i<400; i++) {
            printf("%3d ", x->e_mbd.predictor[i]);
            if (i%16 == 15) printf("\n");
          }
          printf("\n");
        }
#endif
    }
Paul Wilkins's avatar
Paul Wilkins committed
1354
#endif
John Koleszar's avatar
John Koleszar committed
1355

1356
1357
    RECON_INVOKE(&rtcd->common->recon, recon_mb)
        (IF_RTCD(&rtcd->common->recon), &x->e_mbd);
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
#ifdef ENC_DEBUG
    if (enc_debug) {
      int i, j, k;
      printf("Final Reconstruction\n");
      for (i =0; i<16; i+=4) {
        BLOCKD *b = &x->e_mbd.block[i];
        unsigned char *d = *(b->base_dst) + b->dst;
        for (k=0; k<4; k++) {
          for (j=0; j<16; j++)
            printf("%3d ", d[j]);
          printf("\n");
          d+=b->dst_stride;
        }
      }
    }
#endif
John Koleszar's avatar
John Koleszar committed
1374
1375
1376
}


1377
/* this function is used by first pass only */
John Koleszar's avatar
John Koleszar committed
1378
1379
void vp8_encode_inter16x16y(const VP8_ENCODER_RTCD *rtcd, MACROBLOCK *x)
{
Paul Wilkins's avatar
Paul Wilkins committed
1380
1381
1382
1383
1384
#if CONFIG_T8X8
    int tx_type = get_seg_tx_type(&x->e_mbd,
                                  x->e_mbd.mode_info_context->mbmi.segment_id);
#endif

1385
1386
    BLOCK *b = &x->block[0];

1387
    vp8_build_inter16x16_predictors_mby(&x->e_mbd);
John Koleszar's avatar
John Koleszar committed
1388

1389
    ENCODEMB_INVOKE(&rtcd->encodemb, submby)(x->src_diff, *(b->base_src), x->e_mbd.predictor, b->src_stride);
John Koleszar's avatar
John Koleszar committed
1390

1391
#if CONFIG_T8X8
Paul Wilkins's avatar
Paul Wilkins committed
1392
    if( tx_type == TX_8X8 )
1393
1394
1395
1396
          vp8_transform_mby_8x8(x);
    else
#endif
          transform_mby(x);
John Koleszar's avatar
John Koleszar committed
1397
1398

    vp8_quantize_mby(x);
1399
#if CONFIG_T8X8
Paul Wilkins's avatar
Paul Wilkins committed
1400
    if( tx_type == TX_8X8 )
1401
1402
1403
1404
          vp8_inverse_transform_mby_8x8(IF_RTCD(&rtcd->common->idct), &x->e_mbd);
    else
#endif
          vp8_inverse_transform_mby(IF_RTCD(&rtcd->common->idct), &x->e_mbd);
John Koleszar's avatar
John Koleszar committed
1405

1406
1407
    RECON_INVOKE(&rtcd->common->recon, recon_mby)
        (IF_RTCD(&rtcd->common->recon), &x->e_mbd);
John Koleszar's avatar
John Koleszar committed
1408
}