bitstream.c 86.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
 */


John Koleszar's avatar
John Koleszar committed
12
#include "vp8/common/header.h"
John Koleszar's avatar
John Koleszar committed
13
#include "encodemv.h"
John Koleszar's avatar
John Koleszar committed
14
15
#include "vp8/common/entropymode.h"
#include "vp8/common/findnearmv.h"
John Koleszar's avatar
John Koleszar committed
16
#include "mcomp.h"
John Koleszar's avatar
John Koleszar committed
17
#include "vp8/common/systemdependent.h"
John Koleszar's avatar
John Koleszar committed
18
19
#include <assert.h>
#include <stdio.h>
20
#include <limits.h>
John Koleszar's avatar
John Koleszar committed
21
#include "vp8/common/pragmas.h"
22
#include "vpx/vpx_encoder.h"
John Koleszar's avatar
John Koleszar committed
23
24
#include "vpx_mem/vpx_mem.h"
#include "bitstream.h"
25

26
#include "vp8/common/seg_common.h"
27
#include "vp8/common/pred_common.h"
28
#include "vp8/common/entropy.h"
29

John Koleszar's avatar
John Koleszar committed
30
31
32
33
34
#if defined(SECTIONBITS_OUTPUT)
unsigned __int64 Sectionbits[500];
#endif

#ifdef ENTROPY_STATS
35
36
37
int intra_mode_stats[VP8_BINTRAMODES]
                    [VP8_BINTRAMODES]
                    [VP8_BINTRAMODES];
38
39
40
static unsigned int tree_update_hist [BLOCK_TYPES]
                                     [COEF_BANDS]
                                     [PREV_COEF_CONTEXTS]
41
                                     [ENTROPY_NODES] [2]={0};
42
43
44
45
static unsigned int tree_update_hist_8x8 [BLOCK_TYPES_8X8]
                                         [COEF_BANDS]
                                         [PREV_COEF_CONTEXTS]
                                         [ENTROPY_NODES] [2]={0};
46

John Koleszar's avatar
John Koleszar committed
47
48
49
50
51
52
53
extern unsigned int active_section;
#endif

#ifdef MODE_STATS
int count_mb_seg[4] = { 0, 0, 0, 0 };
#endif

54
#define vp8_cost_upd  ((int)(vp8_cost_one(upd) - vp8_cost_zero(upd)) >> 8)
55
56
57
58
59
60
61
62
63
64
65
66
67
#define vp8_cost_upd256  ((int)(vp8_cost_one(upd) - vp8_cost_zero(upd)))

#if CONFIG_NEWUPDATE
#define SEARCH_NEWP
static int update_bits[255];

static void compute_update_table()
{
    int i;
    for (i=0; i<255; i++)
        update_bits[i] = vp8_count_term_subexp(i, SUBEXP_PARAM, 255);
}

68
69
70
71
72
73
74
75
static int split_index(int i, int n, int modulus)
{
    int max1 = (n-1 - modulus/2)/modulus + 1;
    if (i%modulus == modulus/2) i = i/modulus;
    else i = max1 + i - (i + modulus-modulus/2)/modulus;
    return i;
}

76
77
78
static int remap_prob(int v, int m)
{
    const int n = 256;
79
80
    const int modulus = MODULUS_PARAM;
    const int max1 = (n-2-modulus/2+modulus-1)/modulus;
81
82
83
84
85
    int i;
    if ((m<<1)<=n)
        i = recenter_nonneg(v, m) - 1;
    else
        i = recenter_nonneg(n-1-v, n-1-m) - 1;
86
87

    i = split_index(i, n-1, modulus);
88
89
    return i;
}
90
91
92
93
94
95
96
97
98
99
100
101
102

static void write_prob_diff_update(vp8_writer *const w,
                                   vp8_prob newp, vp8_prob oldp)
{
    int delp = remap_prob(newp, oldp);
    vp8_encode_term_subexp(w, delp, SUBEXP_PARAM, 255);
}

static int prob_diff_update_cost(vp8_prob newp, vp8_prob oldp)
{
    int delp = remap_prob(newp, oldp);
    return update_bits[delp]*256;
}
103
#endif
104

John Koleszar's avatar
John Koleszar committed
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
static void update_mode(
    vp8_writer *const w,
    int n,
    vp8_token tok               [/* n */],
    vp8_tree tree,
    vp8_prob Pnew               [/* n-1 */],
    vp8_prob Pcur               [/* n-1 */],
    unsigned int bct            [/* n-1 */] [2],
    const unsigned int num_events[/* n */]
)
{
    unsigned int new_b = 0, old_b = 0;
    int i = 0;

    vp8_tree_probs_from_distribution(
        n--, tok, tree,
        Pnew, bct, num_events,
        256, 1
    );

    do
    {
        new_b += vp8_cost_branch(bct[i], Pnew[i]);
        old_b += vp8_cost_branch(bct[i], Pcur[i]);
    }
    while (++i < n);

    if (new_b + (n << 8) < old_b)
    {
        int i = 0;

        vp8_write_bit(w, 1);

        do
        {
            const vp8_prob p = Pnew[i];

            vp8_write_literal(w, Pcur[i] = p ? p : 1, 8);
        }
        while (++i < n);
    }
    else
        vp8_write_bit(w, 0);
}

static void update_mbintra_mode_probs(VP8_COMP *cpi)
{
    VP8_COMMON *const x = & cpi->common;

    vp8_writer *const w = & cpi->bc;

    {
        vp8_prob Pnew   [VP8_YMODES-1];
        unsigned int bct [VP8_YMODES-1] [2];

        update_mode(
            w, VP8_YMODES, vp8_ymode_encodings, vp8_ymode_tree,
            Pnew, x->fc.ymode_prob, bct, (unsigned int *)cpi->ymode_count
        );
    }
}

Paul Wilkins's avatar
Paul Wilkins committed
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
void update_skip_probs(VP8_COMP *cpi)
{
#if CONFIG_NEWENTROPY
    VP8_COMMON *const pc = & cpi->common;
    int prob_skip_false[3] = {0, 0, 0};
    int k;

    for (k=0;k<MBSKIP_CONTEXTS;++k)
    {
        if ( (cpi->skip_false_count[k] + cpi->skip_true_count[k]) )
        {
            prob_skip_false[k] =
                cpi->skip_false_count[k] * 256 /
                (cpi->skip_false_count[k] + cpi->skip_true_count[k]);

            if (prob_skip_false[k] <= 1)
                prob_skip_false[k] = 1;

            if (prob_skip_false[k] > 255)
                prob_skip_false[k] = 255;
        }
        else
            prob_skip_false[k] = 128;

        pc->mbskip_pred_probs[k] = prob_skip_false[k];
    }

#else
    int prob_skip_false = 0;

    if ( (cpi->skip_false_count + cpi->skip_true_count) )
    {
        prob_skip_false = cpi->skip_false_count * 256 /
                          (cpi->skip_false_count + cpi->skip_true_count);

        if (prob_skip_false <= 1)
            prob_skip_false = 1;

        if (prob_skip_false > 255)
            prob_skip_false = 255;
    }
    else
        prob_skip_false = 128;

    cpi->prob_skip_false = prob_skip_false;

#endif
}

216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// This function updates the reference frame prediction stats
static void update_refpred_stats( VP8_COMP *cpi )
{
    VP8_COMMON *const cm = & cpi->common;
    int i;
    int tot_count;
    vp8_prob new_pred_probs[PREDICTION_PROBS];
    int old_cost, new_cost;

    // Set the prediction probability structures to defaults
    if ( cm->frame_type == KEY_FRAME )
    {
        // Set the prediction probabilities to defaults
        cm->ref_pred_probs[0] = 120;
        cm->ref_pred_probs[1] = 80;
        cm->ref_pred_probs[2] = 40;

        vpx_memset(cpi->ref_pred_probs_update, 0,
                   sizeof(cpi->ref_pred_probs_update) );
    }
    else
    {
        // From the prediction counts set the probabilities for each context
        for ( i = 0; i < PREDICTION_PROBS; i++ )
        {
241
242
            tot_count = cpi->ref_pred_count[i][0] + cpi->ref_pred_count[i][1];
            if ( tot_count )
243
            {
244
245
246
247
248
                new_pred_probs[i] =
                    ( cpi->ref_pred_count[i][0] * 255 + (tot_count >> 1)) / tot_count;

                // Clamp to minimum allowed value
                new_pred_probs[i] += !new_pred_probs[i];
249
250
251
252
253
254
255
            }
            else
                new_pred_probs[i] = 128;

            // Decide whether or not to update the reference frame probs.
            // Returned costs are in 1/256 bit units.
            old_cost =
256
257
                (cpi->ref_pred_count[i][0] * vp8_cost_zero(cm->ref_pred_probs[i])) +
                (cpi->ref_pred_count[i][1] * vp8_cost_one(cm->ref_pred_probs[i]));
258
259

            new_cost =
260
261
                (cpi->ref_pred_count[i][0] * vp8_cost_zero(new_pred_probs[i])) +
                (cpi->ref_pred_count[i][1] * vp8_cost_one(new_pred_probs[i]));
262
263
264
265
266
267
268
269
270
271
272
273
274
275

            // Cost saving must be >= 8 bits (2048 in these units)
            if ( (old_cost - new_cost) >= 2048 )
            {
                cpi->ref_pred_probs_update[i] = 1;
                cm->ref_pred_probs[i] = new_pred_probs[i];
            }
            else
                cpi->ref_pred_probs_update[i] = 0;

        }
    }
}

John Koleszar's avatar
John Koleszar committed
276
277
278
279
280
281
282
283
284
285
static void write_ymode(vp8_writer *bc, int m, const vp8_prob *p)
{
    vp8_write_token(bc, vp8_ymode_tree, p, vp8_ymode_encodings + m);
}

static void kfwrite_ymode(vp8_writer *bc, int m, const vp8_prob *p)
{
    vp8_write_token(bc, vp8_kf_ymode_tree, p, vp8_kf_ymode_encodings + m);
}

Yaowu Xu's avatar
Yaowu Xu committed
286
287
288
289
static void write_i8x8_mode(vp8_writer *bc, int m, const vp8_prob *p)
{
    vp8_write_token(bc,vp8_i8x8_mode_tree, p, vp8_i8x8_mode_encodings + m);
}
Yaowu Xu's avatar
Yaowu Xu committed
290

John Koleszar's avatar
John Koleszar committed
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
static void write_uv_mode(vp8_writer *bc, int m, const vp8_prob *p)
{
    vp8_write_token(bc, vp8_uv_mode_tree, p, vp8_uv_mode_encodings + m);
}


static void write_bmode(vp8_writer *bc, int m, const vp8_prob *p)
{
    vp8_write_token(bc, vp8_bmode_tree, p, vp8_bmode_encodings + m);
}

static void write_split(vp8_writer *bc, int x)
{
    vp8_write_token(
        bc, vp8_mbsplit_tree, vp8_mbsplit_probs, vp8_mbsplit_encodings + x
    );
}

309
310
311
312
static int prob_update_savings(const unsigned int *ct,
                               const vp8_prob oldp, const vp8_prob newp,
                               const vp8_prob upd)
{
313
314
315
316
317
318
319
    const int old_b = vp8_cost_branch256(ct, oldp);
    const int new_b = vp8_cost_branch256(ct, newp);
    const int update_b = 2048 + vp8_cost_upd256;
    return (old_b - new_b - update_b);
}

#if CONFIG_NEWUPDATE
320
321
322
323
324
325
326
327
328
329
330
331
static int prob_diff_update_savings(const unsigned int *ct,
                               const vp8_prob oldp, const vp8_prob newp,
                               const vp8_prob upd)
{
    const int old_b = vp8_cost_branch256(ct, oldp);
    const int new_b = vp8_cost_branch256(ct, newp);
    const int update_b = (newp == oldp ? 0 :
        prob_diff_update_cost(newp, oldp) + vp8_cost_upd256);
    return (old_b - new_b - update_b);
}

static int prob_diff_update_savings_search(const unsigned int *ct,
332
333
334
335
                                      const vp8_prob oldp, vp8_prob *bestp,
                                      const vp8_prob upd)
{
    const int old_b = vp8_cost_branch256(ct, oldp);
336
    int new_b, update_b, savings, bestsavings, step;
337
338
339
340
    vp8_prob newp, bestnewp;

    bestsavings = 0;
    bestnewp = oldp;
341

342
343
344
345
    step = (*bestp > oldp ? -1 : 1);
    for (newp = *bestp; newp != oldp; newp+=step)
    {
        new_b = vp8_cost_branch256(ct, newp);
346
        update_b = prob_diff_update_cost(newp, oldp) + vp8_cost_upd256;
347
348
349
350
351
352
353
354
355
        savings = old_b - new_b - update_b;
        if (savings > bestsavings)
        {
            bestsavings = savings;
            bestnewp = newp;
        }
    }
    *bestp = bestnewp;
    return bestsavings;
356
}
357
#endif
358

John Koleszar's avatar
John Koleszar committed
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
static void pack_tokens_c(vp8_writer *w, const TOKENEXTRA *p, int xcount)
{
    const TOKENEXTRA *const stop = p + xcount;
    unsigned int split;
    unsigned int shift;
    int count = w->count;
    unsigned int range = w->range;
    unsigned int lowvalue = w->lowvalue;

    while (p < stop)
    {
        const int t = p->Token;
        vp8_token *const a = vp8_coef_encodings + t;
        const vp8_extra_bit_struct *const b = vp8_extra_bits + t;
        int i = 0;
        const unsigned char *pp = p->context_tree;
        int v = a->value;
        int n = a->Len;

378
        /* skip one or two nodes */
John Koleszar's avatar
John Koleszar committed
379
380
        if (p->skip_eob_node)
        {
381
382
            n-=p->skip_eob_node;
            i = 2*p->skip_eob_node;
John Koleszar's avatar
John Koleszar committed
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
        }

        do
        {
            const int bb = (v >> --n) & 1;
            split = 1 + (((range - 1) * pp[i>>1]) >> 8);
            i = vp8_coef_tree[i+bb];

            if (bb)
            {
                lowvalue += split;
                range = range - split;
            }
            else
            {
                range = split;
            }

401
            shift = vp8_norm[range];
John Koleszar's avatar
John Koleszar committed
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
            range <<= shift;
            count += shift;

            if (count >= 0)
            {
                int offset = shift - count;

                if ((lowvalue << (offset - 1)) & 0x80000000)
                {
                    int x = w->pos - 1;

                    while (x >= 0 && w->buffer[x] == 0xff)
                    {
                        w->buffer[x] = (unsigned char)0;
                        x--;
                    }

                    w->buffer[x] += 1;
                }

                w->buffer[w->pos++] = (lowvalue >> (24 - offset));
                lowvalue <<= offset;
                shift = count;
                lowvalue &= 0xffffff;
                count -= 8 ;
            }

            lowvalue <<= shift;
        }
        while (n);


        if (b->base_val)
        {
            const int e = p->Extra, L = b->Len;

            if (L)
            {
                const unsigned char *pp = b->prob;
                int v = e >> 1;
                int n = L;              /* number of bits in v, assumed nonzero */
                int i = 0;

                do
                {
                    const int bb = (v >> --n) & 1;
                    split = 1 + (((range - 1) * pp[i>>1]) >> 8);
                    i = b->tree[i+bb];

                    if (bb)
                    {
                        lowvalue += split;
                        range = range - split;
                    }
                    else
                    {
                        range = split;
                    }

461
                    shift = vp8_norm[range];
John Koleszar's avatar
John Koleszar committed
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
                    range <<= shift;
                    count += shift;

                    if (count >= 0)
                    {
                        int offset = shift - count;

                        if ((lowvalue << (offset - 1)) & 0x80000000)
                        {
                            int x = w->pos - 1;

                            while (x >= 0 && w->buffer[x] == 0xff)
                            {
                                w->buffer[x] = (unsigned char)0;
                                x--;
                            }

                            w->buffer[x] += 1;
                        }

                        w->buffer[w->pos++] = (lowvalue >> (24 - offset));
                        lowvalue <<= offset;
                        shift = count;
                        lowvalue &= 0xffffff;
                        count -= 8 ;
                    }

                    lowvalue <<= shift;
                }
                while (n);
            }


            {

                split = (range + 1) >> 1;

                if (e & 1)
                {
                    lowvalue += split;
                    range = range - split;
                }
                else
                {
                    range = split;
                }

                range <<= 1;

                if ((lowvalue & 0x80000000))
                {
                    int x = w->pos - 1;

                    while (x >= 0 && w->buffer[x] == 0xff)
                    {
                        w->buffer[x] = (unsigned char)0;
                        x--;
                    }

                    w->buffer[x] += 1;

                }

                lowvalue  <<= 1;

                if (!++count)
                {
                    count = -8;
                    w->buffer[w->pos++] = (lowvalue >> 24);
                    lowvalue &= 0xffffff;
                }
            }

        }

        ++p;
    }

    w->count = count;
    w->lowvalue = lowvalue;
    w->range = range;

}

static void write_partition_size(unsigned char *cx_data, int size)
{
    signed char csize;

    csize = size & 0xff;
    *cx_data = csize;
    csize = (size >> 8) & 0xff;
    *(cx_data + 1) = csize;
    csize = (size >> 16) & 0xff;
    *(cx_data + 2) = csize;

}

static void write_mv_ref
(
    vp8_writer *w, MB_PREDICTION_MODE m, const vp8_prob *p
)
{
564
#if CONFIG_DEBUG
John Koleszar's avatar
John Koleszar committed
565
    assert(NEARESTMV <= m  &&  m <= SPLITMV);
566
#endif
567
568
    vp8_write_token(w, vp8_mv_ref_tree, p,
                    vp8_mv_ref_encoding_array - NEARESTMV + m);
John Koleszar's avatar
John Koleszar committed
569
570
571
572
573
574
575
}

static void write_sub_mv_ref
(
    vp8_writer *w, B_PREDICTION_MODE m, const vp8_prob *p
)
{
576
#if CONFIG_DEBUG
John Koleszar's avatar
John Koleszar committed
577
    assert(LEFT4X4 <= m  &&  m <= NEW4X4);
578
#endif
579
580
    vp8_write_token(w, vp8_sub_mv_ref_tree, p,
                    vp8_sub_mv_ref_encoding_array - LEFT4X4 + m);
John Koleszar's avatar
John Koleszar committed
581
582
583
584
}

static void write_mv
(
Scott LaVarnway's avatar
Scott LaVarnway committed
585
    vp8_writer *w, const MV *mv, const int_mv *ref, const MV_CONTEXT *mvc
John Koleszar's avatar
John Koleszar committed
586
587
588
)
{
    MV e;
Scott LaVarnway's avatar
Scott LaVarnway committed
589
590
    e.row = mv->row - ref->as_mv.row;
    e.col = mv->col - ref->as_mv.col;
John Koleszar's avatar
John Koleszar committed
591
592
593
594

    vp8_encode_motion_vector(w, &e, mvc);
}

595
596
597
598
599
600
601
602
603
604
605
606
607
608
#if CONFIG_HIGH_PRECISION_MV
static void write_mv_hp
(
    vp8_writer *w, const MV *mv, const int_mv *ref, const MV_CONTEXT_HP *mvc
)
{
    MV e;
    e.row = mv->row - ref->as_mv.row;
    e.col = mv->col - ref->as_mv.col;

    vp8_encode_motion_vector_hp(w, &e, mvc);
}
#endif

Paul Wilkins's avatar
Paul Wilkins committed
609
610
611
612
// This function writes the current macro block's segnment id to the bitstream
// It should only be called if a segment map update is indicated.
static void write_mb_segid(vp8_writer *w,
                           const MB_MODE_INFO *mi, const MACROBLOCKD *x)
John Koleszar's avatar
John Koleszar committed
613
614
615
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
641
642
643
644
{
    // Encode the MB segment id.
    if (x->segmentation_enabled && x->update_mb_segmentation_map)
    {
        switch (mi->segment_id)
        {
        case 0:
            vp8_write(w, 0, x->mb_segment_tree_probs[0]);
            vp8_write(w, 0, x->mb_segment_tree_probs[1]);
            break;
        case 1:
            vp8_write(w, 0, x->mb_segment_tree_probs[0]);
            vp8_write(w, 1, x->mb_segment_tree_probs[1]);
            break;
        case 2:
            vp8_write(w, 1, x->mb_segment_tree_probs[0]);
            vp8_write(w, 0, x->mb_segment_tree_probs[2]);
            break;
        case 3:
            vp8_write(w, 1, x->mb_segment_tree_probs[0]);
            vp8_write(w, 1, x->mb_segment_tree_probs[2]);
            break;

            // TRAP.. This should not happen
        default:
            vp8_write(w, 0, x->mb_segment_tree_probs[0]);
            vp8_write(w, 0, x->mb_segment_tree_probs[1]);
            break;
        }
    }
}

Paul Wilkins's avatar
Paul Wilkins committed
645
646
// This function encodes the reference frame
static void encode_ref_frame( vp8_writer *const w,
647
                              VP8_COMMON *const cm,
Paul Wilkins's avatar
Paul Wilkins committed
648
649
                              MACROBLOCKD *xd,
                              int segment_id,
650
                              MV_REFERENCE_FRAME rf )
Paul Wilkins's avatar
Paul Wilkins committed
651
652
{
    int seg_ref_active;
Paul Wilkins's avatar
Paul Wilkins committed
653
    int seg_ref_count = 0;
Paul Wilkins's avatar
Paul Wilkins committed
654
655
656
657
    seg_ref_active = segfeature_active( xd,
                                        segment_id,
                                        SEG_LVL_REF_FRAME );

Paul Wilkins's avatar
Paul Wilkins committed
658
659
660
661
662
663
664
665
    if ( seg_ref_active )
    {
        seg_ref_count = check_segref( xd, segment_id, INTRA_FRAME ) +
                        check_segref( xd, segment_id, LAST_FRAME ) +
                        check_segref( xd, segment_id, GOLDEN_FRAME ) +
                        check_segref( xd, segment_id, ALTREF_FRAME );
    }

666
    // If segment level coding of this signal is disabled...
Paul Wilkins's avatar
Paul Wilkins committed
667
668
    // or the segment allows multiple reference frame options
    if ( !seg_ref_active || (seg_ref_count > 1) )
Paul Wilkins's avatar
Paul Wilkins committed
669
    {
670
671
672
        // Values used in prediction model coding
        unsigned char prediction_flag;
        vp8_prob pred_prob;
673
        MV_REFERENCE_FRAME pred_rf;
674
675
676
677

        // Get the context probability the prediction flag
        pred_prob = get_pred_prob( cm, xd, PRED_REF );

678
679
680
681
682
683
684
685
        // Get the predicted value.
        pred_rf = get_pred_ref( cm, xd );

        // Did the chosen reference frame match its predicted value.
        prediction_flag =
            ( xd->mode_info_context->mbmi.ref_frame == pred_rf );

        set_pred_flag( xd, PRED_REF, prediction_flag );
686
687
688
689
690
        vp8_write( w, prediction_flag, pred_prob );

        // If not predicted correctly then code value explicitly
        if ( !prediction_flag )
        {
Paul Wilkins's avatar
Paul Wilkins committed
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
            vp8_prob mod_refprobs[PREDICTION_PROBS];

            vpx_memcpy( mod_refprobs,
                        cm->mod_refprobs[pred_rf], sizeof(mod_refprobs) );

            // If segment coding enabled blank out options that cant occur by
            // setting the branch probability to 0.
            if ( seg_ref_active )
            {
                mod_refprobs[INTRA_FRAME] *=
                    check_segref( xd, segment_id, INTRA_FRAME );
                mod_refprobs[LAST_FRAME] *=
                    check_segref( xd, segment_id, LAST_FRAME );
                mod_refprobs[GOLDEN_FRAME] *=
                    ( check_segref( xd, segment_id, GOLDEN_FRAME ) *
                      check_segref( xd, segment_id, ALTREF_FRAME ) );
            }
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730

            if ( mod_refprobs[0] )
            {
                vp8_write(w, (rf != INTRA_FRAME), mod_refprobs[0] );
            }

            // Inter coded
            if (rf != INTRA_FRAME)
            {
                if ( mod_refprobs[1] )
                {
                    vp8_write(w, (rf != LAST_FRAME), mod_refprobs[1] );
                }

                if (rf != LAST_FRAME)
                {
                    if ( mod_refprobs[2] )
                    {
                        vp8_write(w, (rf != GOLDEN_FRAME), mod_refprobs[2] );
                    }
                }
            }
        }
Paul Wilkins's avatar
Paul Wilkins committed
731
    }
Paul Wilkins's avatar
Paul Wilkins committed
732
733
734

    // if using the prediction mdoel we have nothing further to do because
    // the reference frame is fully coded by the segment
Paul Wilkins's avatar
Paul Wilkins committed
735
}
John Koleszar's avatar
John Koleszar committed
736

737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
// Update the probabilities used to encode reference frame data
static void update_ref_probs( VP8_COMP *const cpi )
{
    VP8_COMMON *const cm = & cpi->common;

    const int *const rfct = cpi->count_mb_ref_frame_usage;
    const int rf_intra = rfct[INTRA_FRAME];
    const int rf_inter = rfct[LAST_FRAME] +
                         rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME];

    cm->prob_intra_coded = (rf_intra + rf_inter)
                            ? rf_intra * 255 / (rf_intra + rf_inter) : 1;

    if (!cm->prob_intra_coded)
        cm->prob_intra_coded = 1;

    cm->prob_last_coded = rf_inter ? (rfct[LAST_FRAME] * 255) / rf_inter : 128;

    if (!cm->prob_last_coded)
        cm->prob_last_coded = 1;

    cm->prob_gf_coded = (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME])
                        ? (rfct[GOLDEN_FRAME] * 255) /
                          (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME]) : 128;

    if (!cm->prob_gf_coded)
       cm->prob_gf_coded = 1;

    // Compute a modified set of probabilities to use when prediction of the
    // reference frame fails
    compute_mod_refprobs( cm );
}

John Koleszar's avatar
John Koleszar committed
770
771
static void pack_inter_mode_mvs(VP8_COMP *const cpi)
{
Adrian Grange's avatar
Adrian Grange committed
772
    int i;
John Koleszar's avatar
John Koleszar committed
773
774
775
    VP8_COMMON *const pc = & cpi->common;
    vp8_writer *const w = & cpi->bc;
    const MV_CONTEXT *mvc = pc->fc.mvc;
776
777
778
#if CONFIG_HIGH_PRECISION_MV
    const MV_CONTEXT_HP *mvc_hp = pc->fc.mvc_hp;
#endif
779
    MACROBLOCKD *xd = &cpi->mb.e_mbd;
Adrian Grange's avatar
Adrian Grange committed
780
781
    MODE_INFO *m;
    MODE_INFO *prev_m;
782

John Koleszar's avatar
John Koleszar committed
783
    const int mis = pc->mode_info_stride;
Adrian Grange's avatar
Adrian Grange committed
784
785
    int mb_row, mb_col;
    int row, col;
John Koleszar's avatar
John Koleszar committed
786

787
788
789
790
    // Values used in prediction model coding
    vp8_prob pred_prob;
    unsigned char prediction_flag;

Adrian Grange's avatar
Adrian Grange committed
791
792
793
    int row_delta[4] = { 0, +1,  0, -1};
    int col_delta[4] = {+1, -1, +1, +1};

794
795
    cpi->mb.partition_info = cpi->mb.pi;

796
797
    // Update the probabilities used to encode reference frame data
    update_ref_probs( cpi );
John Koleszar's avatar
John Koleszar committed
798
799
800
801
802
803
804

#ifdef ENTROPY_STATS
    active_section = 1;
#endif

    if (pc->mb_no_coeff_skip)
    {
805
806
807
#if CONFIG_NEWENTROPY
        int k;

Paul Wilkins's avatar
Paul Wilkins committed
808
809
        update_skip_probs( cpi );
        for (k=0;k<MBSKIP_CONTEXTS;++k)
810
            vp8_write_literal(w, pc->mbskip_pred_probs[k], 8);
811
#else
Paul Wilkins's avatar
Paul Wilkins committed
812
813
        update_skip_probs( cpi );
        vp8_write_literal(w, cpi->prob_skip_false, 8);
814
#endif
John Koleszar's avatar
John Koleszar committed
815
816
    }

817
818
819
    vp8_write_literal(w, pc->prob_intra_coded, 8);
    vp8_write_literal(w, pc->prob_last_coded, 8);
    vp8_write_literal(w, pc->prob_gf_coded, 8);
John Koleszar's avatar
John Koleszar committed
820

821
    if (cpi->common.comp_pred_mode == HYBRID_PREDICTION)
822
823
824
    {
        vp8_write(w, 1, 128);
        vp8_write(w, 1, 128);
825
        for (i = 0; i < COMP_PRED_CONTEXTS; i++)
826
        {
827
            if (cpi->single_pred_count[i] + cpi->comp_pred_count[i])
828
            {
829
830
831
832
                pc->prob_comppred[i] = cpi->single_pred_count[i] * 255 /
                    (cpi->single_pred_count[i] + cpi->comp_pred_count[i]);
                if (pc->prob_comppred[i] < 1)
                    pc->prob_comppred[i] = 1;
833
834
835
            }
            else
            {
836
                pc->prob_comppred[i] = 128;
837
            }
838
            vp8_write_literal(w, pc->prob_comppred[i], 8);
839
840
        }
    }
841
    else if (cpi->common.comp_pred_mode == SINGLE_PREDICTION_ONLY)
842
843
844
    {
        vp8_write(w, 0, 128);
    }
845
    else /* compound prediction only */
846
847
848
849
850
    {
        vp8_write(w, 1, 128);
        vp8_write(w, 0, 128);
    }

John Koleszar's avatar
John Koleszar committed
851
852
    update_mbintra_mode_probs(cpi);

853
854
855
856
857
#if CONFIG_HIGH_PRECISION_MV
    if (xd->allow_high_precision_mv)
        vp8_write_mvprobs_hp(cpi);
    else
#endif
John Koleszar's avatar
John Koleszar committed
858
    vp8_write_mvprobs(cpi);
Paul Wilkins's avatar
Paul Wilkins committed
859

Adrian Grange's avatar
Adrian Grange committed
860
861
    mb_row = 0;
    for (row=0; row < pc->mb_rows; row += 2)
John Koleszar's avatar
John Koleszar committed
862
    {
Adrian Grange's avatar
Adrian Grange committed
863
864
        m = pc->mi + row * mis;
        prev_m = pc->prev_mi + row * mis;
John Koleszar's avatar
John Koleszar committed
865

Adrian Grange's avatar
Adrian Grange committed
866
867
        mb_col = 0;
        for (col=0; col < pc->mb_cols; col += 2)
John Koleszar's avatar
John Koleszar committed
868
        {
Adrian Grange's avatar
Adrian Grange committed
869
870
871
872
873
874
875
876
877
878
            int i;

            // Process the 4 MBs in the order:
            // top-left, top-right, bottom-left, bottom-right
            for (i=0; i<4; i++)
            {
                const MB_MODE_INFO *const mi = & m->mbmi;
                const MV_REFERENCE_FRAME rf = mi->ref_frame;
                const MB_PREDICTION_MODE mode = mi->mode;
                const int segment_id = mi->segment_id;
John Koleszar's avatar
John Koleszar committed
879

Adrian Grange's avatar
Adrian Grange committed
880
881
882
                int dy = row_delta[i];
                int dx = col_delta[i];
                int offset_extended = dy * mis + dx;
Paul Wilkins's avatar
Paul Wilkins committed
883

Adrian Grange's avatar
Adrian Grange committed
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
                if ((mb_row >= pc->mb_rows) || (mb_col >= pc->mb_cols))
                {
                    // MB lies outside frame, move on
                    mb_row += dy;
                    mb_col += dx;
                    m += offset_extended;
                    prev_m += offset_extended;
                    cpi->mb.partition_info += offset_extended;
                    continue;
                }

                // Distance of Mb to the various image edges.
                // These specified to 8th pel as they are always compared to MV
                // values that are in 1/8th pel units
                xd->mb_to_left_edge = -((mb_col * 16) << 3);
                xd->mb_to_right_edge = ((pc->mb_cols - 1 - mb_col) * 16) << 3;
                xd->mb_to_top_edge = -((mb_row * 16)) << 3;
                xd->mb_to_bottom_edge = ((pc->mb_rows - 1 - mb_row) * 16) << 3;
902

Adrian Grange's avatar
Adrian Grange committed
903
904
905
                // Make sure the MacroBlockD mode info pointer is set correctly
                xd->mode_info_context = m;
                xd->prev_mode_info_context = prev_m;
Paul Wilkins's avatar
Paul Wilkins committed
906

John Koleszar's avatar
John Koleszar committed
907
#ifdef ENTROPY_STATS
Adrian Grange's avatar
Adrian Grange committed
908
                active_section = 9;
John Koleszar's avatar
John Koleszar committed
909
910
#endif

Adrian Grange's avatar
Adrian Grange committed
911
                if (cpi->mb.e_mbd.update_mb_segmentation_map)
912
                {
Adrian Grange's avatar
Adrian Grange committed
913
914
915
916
917
                    // Is temporal coding of the segment map enabled
                    if (pc->temporal_update)
                    {
                        prediction_flag = get_pred_flag( xd, PRED_SEG_ID );
                        pred_prob = get_pred_prob( pc, xd, PRED_SEG_ID);
918

Adrian Grange's avatar
Adrian Grange committed
919
920
                        // Code the segment id prediction flag for this mb
                        vp8_write( w, prediction_flag, pred_prob );
921

Adrian Grange's avatar
Adrian Grange committed
922
923
924
925
926
927
928
                        // If the mb segment id wasn't predicted code explicitly
                        if (!prediction_flag)
                            write_mb_segid(w, mi, &cpi->mb.e_mbd);
                    }
                    else
                    {
                        // Normal unpredicted coding
Paul Wilkins's avatar
Paul Wilkins committed
929
                        write_mb_segid(w, mi, &cpi->mb.e_mbd);
Adrian Grange's avatar
Adrian Grange committed
930
                    }
931
                }
John Koleszar's avatar
John Koleszar committed
932

Adrian Grange's avatar
Adrian Grange committed
933
934
935
936
                if ( pc->mb_no_coeff_skip &&
                     ( !segfeature_active( xd, segment_id, SEG_LVL_EOB ) ||
                       ( get_segdata( xd, segment_id, SEG_LVL_EOB ) != 0 ) ) )
                {
937
#if CONFIG_NEWENTROPY
Adrian Grange's avatar
Adrian Grange committed
938
939
                    vp8_encode_bool(w, mi->mb_skip_coeff,
                                    get_pred_prob(pc, xd, PRED_MBSKIP));
940
#else
Paul Wilkins's avatar
Paul Wilkins committed
941
                vp8_encode_bool(w, mi->mb_skip_coeff, cpi->prob_skip_false);
942
#endif
Adrian Grange's avatar
Adrian Grange committed
943
                }
John Koleszar's avatar
John Koleszar committed
944

Adrian Grange's avatar
Adrian Grange committed
945
946
                // Encode the reference frame.
                encode_ref_frame( w, pc, xd, segment_id, rf );
Paul Wilkins's avatar
Paul Wilkins committed
947

Adrian Grange's avatar
Adrian Grange committed
948
949
                if (rf == INTRA_FRAME)
                {
950
#ifdef ENTROPY_STATS
Adrian Grange's avatar
Adrian Grange committed
951
                    active_section = 6;
952
#endif
Paul Wilkins's avatar
Paul Wilkins committed
953

Adrian Grange's avatar
Adrian Grange committed
954
955
                    if ( !segfeature_active( xd, segment_id, SEG_LVL_MODE ) )
                        write_ymode(w, mode, pc->fc.ymode_prob);
John Koleszar's avatar
John Koleszar committed
956

Adrian Grange's avatar
Adrian Grange committed
957
958
959
                    if (mode == B_PRED)
                    {
                        int j = 0;
960
#if CONFIG_COMP_INTRA_PRED
Adrian Grange's avatar
Adrian Grange committed
961
962
963
964
                        int uses_second =
                                m->bmi[0].as_mode.second !=
                                        (B_PREDICTION_MODE) (B_DC_PRED - 1);
                        vp8_write(w, uses_second, 128);
965
#endif
Adrian Grange's avatar
Adrian Grange committed
966
                        do {
967
#if CONFIG_COMP_INTRA_PRED
Adrian Grange's avatar
Adrian Grange committed
968
                            B_PREDICTION_MODE mode2 = m->bmi[j].as_mode.second;
969
#endif
Adrian Grange's avatar
Adrian Grange committed
970
971
                            write_bmode(w, m->bmi[j].as_mode.first,
                                        pc->fc.bmode_prob);
972
#if CONFIG_COMP_INTRA_PRED
Adrian Grange's avatar
Adrian Grange committed
973
974
975
976
                            if (uses_second)
                            {
                                write_bmode(w, mode2, pc->fc.bmode_prob);
                            }
977
#endif
Adrian Grange's avatar
Adrian Grange committed
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
                        } while (++j < 16);
                    }
                    if(mode == I8X8_PRED)
                    {
                        write_i8x8_mode(w, m->bmi[0].as_mode.first,
                                        pc->i8x8_mode_prob);
                        write_i8x8_mode(w, m->bmi[2].as_mode.first,
                                        pc->i8x8_mode_prob);
                        write_i8x8_mode(w, m->bmi[8].as_mode.first,
                                        pc->i8x8_mode_prob);
                        write_i8x8_mode(w, m->bmi[10].as_mode.first,
                                        pc->i8x8_mode_prob);
                    }
                    else
                    {
                        write_uv_mode(w, mi->uv_mode,
                                      pc->fc.uv_mode_prob[mode]);
995
#ifdef MODE_STATS
Adrian Grange's avatar
Adrian Grange committed
996
997
                        if(mode!=B_PRED)
                            ++cpi->y_uv_mode_count[mode][mi->uv_mode];
998
#endif
Adrian Grange's avatar
Adrian Grange committed
999
                    }
1000
                }
Adrian Grange's avatar
Adrian Grange committed
1001
1002
                else
                {
1003
                    int_mv best_mv, best_second_mv;
Adrian Grange's avatar
Adrian Grange committed
1004
                    int ct[4];
Yaowu Xu's avatar
Yaowu Xu committed
1005

Adrian Grange's avatar
Adrian Grange committed
1006
                    vp8_prob mv_ref_p [VP8_MVREFS-1];
John Koleszar's avatar
John Koleszar committed
1007

Adrian Grange's avatar
Adrian Grange committed
1008
1009
                    {
                        int_mv n1, n2;
John Koleszar's avatar
John Koleszar committed
1010

Adrian Grange's avatar
Adrian Grange committed
1011
1012
1013
                        vp8_find_near_mvs(xd, m, prev_m, &n1, &n2, &best_mv, ct,
                                          rf, cpi->common.ref_frame_sign_bias);
                        vp8_mv_ref_probs(&cpi->common, mv_ref_p, ct);
Yaowu Xu's avatar
Yaowu Xu committed
1014

John Koleszar's avatar
John Koleszar committed
1015
#ifdef ENTROPY_STATS
Adrian Grange's avatar
Adrian Grange committed
1016
                        accum_mv_refs(mode, ct);
John Koleszar's avatar
John Koleszar committed
1017
#endif
Adrian Grange's avatar
Adrian Grange committed
1018
                    }
John Koleszar's avatar
John Koleszar committed
1019
1020

#ifdef ENTROPY_STATS
Adrian Grange's avatar
Adrian Grange committed
1021
                    active_section = 3;
John Koleszar's avatar
John Koleszar committed
1022
1023
#endif

Adrian Grange's avatar
Adrian Grange committed
1024
1025
1026
1027
1028
1029
                    // Is the segment coding of mode enabled
                    if ( !segfeature_active( xd, segment_id, SEG_LVL_MODE ) )
                    {
                        write_mv_ref(w, mode, mv_ref_p);
                        vp8_accum_mv_refs(&cpi->common, mode, ct);
                    }
1030

1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
                    if (mi->second_ref_frame &&
                        (mode == NEWMV || mode == SPLITMV))
                    {
                        int_mv n1, n2;

                        vp8_find_near_mvs(xd, m,
                                          prev_m,
                                          &n1, &n2, &best_second_mv, ct,
                                          mi->second_ref_frame, cpi->common.ref_frame_sign_bias);
                    }

                    // does the feature use compound prediction or not
                    // (if not specified at the frame/segment level)
                    if (cpi->common.comp_pred_mode == HYBRID_PREDICTION)
                    {
                        vp8_write(w, mi->second_ref_frame != INTRA_FRAME,
                                  get_pred_prob( pc, xd, PRED_COMP ) );
                    }

1050
                    {
Adrian Grange's avatar
Adrian Grange committed
1051
1052
1053
                        switch (mode)   /* new, split require MVs */
                        {
                        case NEWMV:
1054
#ifdef ENTROPY_STATS
Adrian Grange's avatar
Adrian Grange committed
1055
                            active_section = 5;
1056
#endif
John Koleszar's avatar
John Koleszar committed
1057

1058
#if CONFIG_HIGH_PRECISION_MV
Adrian Grange's avatar
Adrian Grange committed
1059
1060
1061
                            if (xd->allow_high_precision_mv)
                                write_mv_hp(w, &mi->mv.as_mv, &best_mv, mvc_hp);
                            else
1062
#endif
Adrian Grange's avatar
Adrian Grange committed
1063
                            write_mv(w, &mi->mv.as_mv, &best_mv, mvc);
1064

Adrian Grange's avatar
Adrian Grange committed
1065
1066
                            if (mi->second_ref_frame)
                            {
1067
#if CONFIG_HIGH_PRECISION_MV
Adrian Grange's avatar
Adrian Grange committed
1068
1069
                                if (xd->allow_high_precision_mv)
                                    write_mv_hp(w, &mi->second_mv.as_mv,
1070
                                                &best_second_mv, mvc_hp);
Adrian Grange's avatar
Adrian Grange committed
1071
                                else
1072
#endif
1073
1074
                                write_mv(w, &mi->second_mv.as_mv,
                                         &best_second_mv, mvc);
Adrian Grange's avatar
Adrian Grange committed
1075
1076
1077
1078
1079
                            }
                            break;
                        case SPLITMV:
                        {
                            int j = 0;
John Koleszar's avatar
John Koleszar committed
1080

1081
#ifdef MODE_STATS
Adrian Grange's avatar
Adrian Grange committed
1082
                            ++count_mb_seg [mi->partitioning];
1083
#endif
John Koleszar's avatar
John Koleszar committed
1084

Adrian Grange's avatar
Adrian Grange committed
1085
                            write_split(w, mi->partitioning);
1086

Adrian Grange's avatar
Adrian Grange committed
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
                            do
                            {
                                B_PREDICTION_MODE blockmode;
                                int_mv blockmv;
                                const int *const  L =
                                        vp8_mbsplits [mi->partitioning];
                                int k = -1;  /* first block in subset j */
                                int mv_contz;
                                int_mv leftmv, abovemv;

                                blockmode = cpi->mb.partition_info->bmi[j].mode;
                                blockmv = cpi->mb.partition_info->bmi[j].mv;
1099
#if CONFIG_DEBUG
Adrian Grange's avatar
Adrian Grange committed
1100
1101
1102
                                while (j != L[++k])
                                    if (k >= 16)
                                        assert(0);
1103
#else
Adrian Grange's avatar
Adrian Grange committed
1104
                                while (j != L[++k]);
1105
#endif
Adrian Grange's avatar
Adrian Grange committed
1106
1107
1108
                                leftmv.as_int = left_block_mv(m, k);
                                abovemv.as_int = above_block_mv(m, k, mis);
                                mv_contz = vp8_mv_cont(&leftmv, &abovemv);
1109

Adrian Grange's avatar
Adrian Grange committed
1110
1111
                                write_sub_mv_ref(w, blockmode,
                                               vp8_sub_mv_ref_prob2 [mv_contz]);
1112

Adrian Grange's avatar
Adrian Grange committed
1113
1114
                                if (blockmode == NEW4X4)
                                {
1115
#ifdef ENTROPY_STATS
Adrian Grange's avatar
Adrian Grange committed
1116
                                    active_section = 11;
1117
1118
#endif
#if CONFIG_HIGH_PRECISION_MV
Adrian Grange's avatar
Adrian Grange committed
1119
1120
1121
1122
                                    if (xd->allow_high_precision_mv)
                                        write_mv_hp(w, &blockmv.as_mv, &best_mv,
                                                (const MV_CONTEXT_HP *) mvc_hp);
                                    else
1123
#endif
Adrian Grange's avatar
Adrian Grange committed
1124
1125
                                    write_mv(w, &blockmv.as_mv, &best_mv,
                                             (const MV_CONTEXT *) mvc);
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137

                                    if (mi->second_ref_frame)
                                    {
#if CONFIG_HIGH_PRECISION_MV
                                        if (xd->allow_high_precision_mv)
                                            write_mv_hp(w, &cpi->mb.partition_info->bmi[j].second_mv.as_mv,
                                                        &best_second_mv, (const MV_CONTEXT_HP *) mvc_hp);
                                        else
#endif
                                            write_mv(w, &cpi->mb.partition_info->bmi[j].second_mv.as_mv,
                                                     &best_second_mv, (const MV_CONTEXT *) mvc);
                                    }
Adrian Grange's avatar
Adrian Grange committed
1138
                                }
1139
                            }
Adrian Grange's avatar
Adrian Grange committed
1140
                            while (++j < cpi->mb.partition_info->count);
1141
                        }
1142
                        break;
Adrian Grange's avatar
Adrian Grange committed
1143
1144
1145
                        default:
                            break;
                        }
1146
                    }
John Koleszar's avatar
John Koleszar committed
1147
1148
                }

Adrian Grange's avatar
Adrian Grange committed
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
                // Next MB
                mb_row += dy;
                mb_col += dx;
                m += offset_extended;
                prev_m += offset_extended;
                cpi->mb.partition_info += offset_extended;
#if CONFIG_DEBUG
                assert((prev_m-cpi->common.prev_mip)==(m-cpi->common.mip));
                assert((prev_m-cpi->common.prev_mi)==(m-cpi->common.mi));
#endif
            }
John Koleszar's avatar
John Koleszar committed
1160
1161
        }

Adrian Grange's avatar
Adrian Grange committed
1162
1163
1164
1165
1166
        // Next SB
        mb_row += 2;
        m += mis + (1 - (pc->mb_cols & 0x1));
        prev_m += mis + (1 - (pc->mb_cols & 0x1));
        cpi->mb.partition_info += mis + (1 - (pc->mb_cols & 0x1));
John Koleszar's avatar
John Koleszar committed
1167
1168
    }
}
1169

John Koleszar's avatar
John Koleszar committed
1170
1171
1172
static void write_kfmodes(VP8_COMP *cpi)
{
    vp8_writer *const bc = & cpi->bc;
1173
1174
    VP8_COMMON *const c = & cpi->common;
    const int mis = c->mode_info_stride;
1175
    MACROBLOCKD *xd = &cpi->mb.e_mbd;
Adrian Grange's avatar
Adrian Grange committed
1176
1177
1178
1179
    MODE_INFO *m;
    int i;
    int row, col;
    int mb_row, mb_col;
1180
1181
1182
#if CONFIG_NEWENTROPY
    int prob_skip_false[3] = {0, 0, 0};
#else
John Koleszar's avatar
John Koleszar committed
1183
    int prob_skip_false = 0;
1184
#endif
Adrian Grange's avatar
Adrian Grange committed
1185
1186
    int row_delta[4] = { 0, +1,  0, -1};
    int col_delta[4] = {+1, -1, +1, +1};
John Koleszar's avatar
John Koleszar committed
1187

1188
    //printf("write_kfmodes\n");
John Koleszar's avatar
John Koleszar committed
1189
1190
    if (c->mb_no_coeff_skip)
    {
1191
        // Divide by 0 check. 0 case possible with segment features
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
#if CONFIG_NEWENTROPY
        int k;
        for (k=0;k<MBSKIP_CONTEXTS;++k)
        {
            if ( (cpi->skip_false_count[k] + cpi->skip_true_count[k]) )
            {
                prob_skip_false[k] = cpi->skip_false_count[k] * 256 /
                                  (cpi->skip_false_count[k] + cpi->skip_true_count[k]);

                if (prob_skip_false[k] <= 1)
                    prob_skip_false[k] = 1;

                if (prob_skip_false[k] > 255)
                    prob_skip_false[k] = 255;
            }
            else
                prob_skip_false[k] = 255;

            c->mbskip_pred_probs[k] = prob_skip_false[k];
            vp8_write_literal(bc, prob_skip_false[k], 8);
        }
#else
1214
1215
1216
1217
        if ( (cpi->skip_false_count + cpi->skip_true_count) )
        {
            prob_skip_false = cpi->skip_false_count * 256 /
                              (cpi->skip_false_count + cpi->skip_true_count);
John Koleszar's avatar
John Koleszar committed
1218

1219
1220
            if (prob_skip_false <= 1)
                prob_skip_false = 1;
John Koleszar's avatar
John Koleszar committed
1221

1222
1223
1224
1225
            if (prob_skip_false > 255)
                prob_skip_false = 255;
        }
        else
John Koleszar's avatar
John Koleszar committed
1226
1227
1228
1229
            prob_skip_false = 255;

        cpi->prob_skip_false = prob_skip_false;
        vp8_write_literal(bc, prob_skip_false, 8);
1230
#endif
John Koleszar's avatar
John Koleszar committed
1231
1232
    }

1233
1234
1235
1236
1237
    if(!c->kf_ymode_probs_update)
    {
        vp8_write_literal(bc, c->kf_ymode_probs_index, 3);
    }

Adrian Grange's avatar
Adrian Grange committed
1238
1239
    mb_row = 0;
    for (row=0; row < c->mb_rows; row += 2)
John Koleszar's avatar
John Koleszar committed
1240
    {
Adrian Grange's avatar
Adrian Grange committed
1241
        m = c->mi + row * mis;
John Koleszar's avatar
John Koleszar committed
1242

Adrian Grange's avatar
Adrian Grange committed
1243
1244
        mb_col = 0;
        for (col=0; col < c->mb_cols; col += 2)
John Koleszar's avatar
John Koleszar committed
1245
        {
Adrian Grange's avatar
Adrian Grange committed
1246
1247
1248
            // Process the 4 MBs in the order:
            // top-left, top-right, bottom-left, bottom-right
            for (i=0; i<4; i++)
1249
            {
Adrian Grange's avatar
Adrian Grange committed
1250
1251
1252
1253
1254
                int ym;
                int segment_id;
                int dy = row_delta[i];
                int dx = col_delta[i];
                int offset_extended = dy * mis + dx;
John Koleszar's avatar
John Koleszar committed
1255

Adrian Grange's avatar
Adrian Grange committed
1256
                if ((mb_row >= c->mb_rows) || (mb_col >= c->mb_cols))
John Koleszar's avatar
John Koleszar committed
1257
                {
Adrian Grange's avatar
Adrian Grange committed
1258
1259
1260
1261
1262
1263
                    // MB lies outside frame, move on
                    mb_row += dy;
                    mb_col += dx;
                    m += offset_extended;
                    continue;
                }
John Koleszar's avatar
John Koleszar committed
1264

1265
1266
1267
                // Make sure the MacroBlockD mode info pointer is set correctly
                xd->mode_info_context = m;

Adrian Grange's avatar
Adrian Grange committed
1268
1269
                ym = m->mbmi.mode;
                segment_id = m->mbmi.segment_id;
John Koleszar's avatar
John Koleszar committed
1270

Adrian Grange's avatar
Adrian Grange committed
1271
1272
1273
1274
1275
1276
1277
1278
1279
                if (cpi->mb.e_mbd.update_mb_segmentation_map)
                {
                    write_mb_segid(bc, &m->mbmi, &cpi->mb.e_mbd);
                }

                if ( c->mb_no_coeff_skip &&
                     ( !segfeature_active( xd, segment_id, SEG_LVL_EOB ) ||
                       (get_segdata( xd, segment_id, SEG_LVL_EOB ) != 0) ) )
                {
1280
#if CONFIG_NEWENTROPY
Adrian Grange's avatar
Adrian Grange committed
1281
1282
                    vp8_encode_bool(bc, m->mbmi.mb_skip_coeff,
                                    get_pred_prob(c, xd, PRED_MBSKIP));
1283
#else
Adrian Grange's avatar
Adrian Grange committed
1284
                    vp8_encode_bool(bc, m->mbmi.mb_skip_coeff, prob_skip_false);
1285
#endif
Adrian Grange's avatar
Adrian Grange committed
1286
1287
1288
                }
                kfwrite_ymode(bc, ym,
                              c->kf_ymode_prob[c->kf_ymode_probs_index]);
1289

Adrian Grange's avatar
Adrian Grange committed
1290
1291
1292
1293
                if (ym == B_PRED)
                {
                    const int mis = c->mode_info_stride;
                    int i = 0;
1294
#if CONFIG_COMP_INTRA_PRED
Adrian Grange's avatar
Adrian Grange committed
1295
1296
1297
1298
                    int uses_second =
                            m->bmi[0].as_mode.second !=
                                    (B_PREDICTION_MODE) (B_DC_PRED - 1);
                    vp8_write(bc, uses_second, 128);
1299
#endif
Adrian Grange's avatar
Adrian Grange committed
1300
                    do
1301
                    {
Adrian Grange's avatar
Adrian Grange committed
1302
1303
1304
                        const B_PREDICTION_MODE A = above_block_mode(m, i, mis);
                        const B_PREDICTION_MODE L = left_block_mode(m, i);
                        const int bm = m->bmi[i].as_mode.first;
1305
#if CONFIG_COMP_INTRA_PRED
Adrian Grange's avatar
Adrian Grange committed
1306
                        const int bm2 = m->bmi[i].as_mode.second;
1307
#endif
Adrian Grange's avatar
Adrian Grange committed
1308

1309
#ifdef ENTROPY_STATS
Adrian Grange's avatar
Adrian Grange committed
1310
                        ++intra_mode_stats [A] [L] [bm];
1311
#endif
Adrian Grange's avatar
Adrian Grange committed
1312
1313

                        write_bmode(bc, bm, c->kf_bmode_prob [A] [L]);
1314
                        //printf("    mode: %d\n", bm);
1315
#if CONFIG_COMP_INTRA_PRED
Adrian Grange's avatar
Adrian Grange committed
1316
1317
1318
1319
                        if (uses_second)
                        {
                            write_bmode(bc, bm2, c->kf_bmode_prob [A] [L]);
                        }
1320
#endif
1321
                    }
Adrian Grange's avatar
Adrian Grange committed
1322
                    while (++i < 16);
John Koleszar's avatar
John Koleszar committed
1323
                }
Adrian Grange's avatar
Adrian Grange committed
1324
1325
1326
1327
                if(ym == I8X8_PRED)
                {
                    write_i8x8_mode(bc, m->bmi[0].as_mode.first,
                                    c->i8x8_mode_prob);
1328
                    //printf("    mode: %d\n", m->bmi[0].as_mode.first); fflush(stdout);
Adrian Grange's avatar
Adrian Grange committed
1329
1330
                    write_i8x8_mode(bc, m->bmi[2].as_mode.first,
                                    c->i8x8_mode_prob);
1331
                    //printf("    mode: %d\n", m->bmi[2].as_mode.first); fflush(stdout);
Adrian Grange's avatar
Adrian Grange committed
1332
1333
                    write_i8x8_mode(bc, m->bmi[8].as_mode.first,
                                    c->i8x8_mode_prob);
1334
                    //printf("    mode: %d\n", m->bmi[8].as_mode.first); fflush(stdout);
Adrian Grange's avatar
Adrian Grange committed
1335
1336
                    write_i8x8_mode(bc, m->bmi[10].as_mode.first,
                                    c->i8x8_mode_prob);
1337
                    //printf("    mode: %d\n", m->bmi[10].as_mode.first); fflush(stdout);
Adrian Grange's avatar
Adrian Grange committed
1338
1339
1340
                }
                else
                    write_uv_mode(bc, m->mbmi.uv_mode, c->kf_uv_mode_prob[ym]);
Adrian Grange's avatar