bitstream.c 73.8 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 "defaultcoefcounts.h"
27

28
//#if CONFIG_SEGFEATURES
29
30
#include "vp8/common/seg_common.h"

John Koleszar's avatar
John Koleszar committed
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const int vp8cx_base_skip_false_prob[128] =
{
    255, 255, 255, 255, 255, 255, 255, 255,
    255, 255, 255, 255, 255, 255, 255, 255,
    255, 255, 255, 255, 255, 255, 255, 255,
    255, 255, 255, 255, 255, 255, 255, 255,
    255, 255, 255, 255, 255, 255, 255, 255,
    255, 255, 255, 255, 255, 255, 255, 255,
    255, 255, 255, 255, 255, 255, 255, 255,
    251, 248, 244, 240, 236, 232, 229, 225,
    221, 217, 213, 208, 204, 199, 194, 190,
    187, 183, 179, 175, 172, 168, 164, 160,
    157, 153, 149, 145, 142, 138, 134, 130,
    127, 124, 120, 117, 114, 110, 107, 104,
    101, 98,  95,  92,  89,  86,  83, 80,
    77,  74,  71,  68,  65,  62,  59, 56,
    53,  50,  47,  44,  41,  38,  35, 32,
    30,  28,  26,  24,  22,  20,  18, 16,
};

#if defined(SECTIONBITS_OUTPUT)
unsigned __int64 Sectionbits[500];
#endif

#ifdef ENTROPY_STATS
int intra_mode_stats[10][10][10];
57
static unsigned int tree_update_hist [BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [ENTROPY_NODES] [2];
58
59
60
61
#if CONFIG_T8X8
static unsigned int tree_update_hist_8x8 [BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [ENTROPY_NODES] [2];
#endif

John Koleszar's avatar
John Koleszar committed
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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
extern unsigned int active_section;
#endif

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


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
        );
    }
    {
        vp8_prob Pnew   [VP8_UV_MODES-1];
        unsigned int bct [VP8_UV_MODES-1] [2];

        update_mode(
            w, VP8_UV_MODES, vp8_uv_mode_encodings, vp8_uv_mode_tree,
            Pnew, x->fc.uv_mode_prob, bct, (unsigned int *)cpi->uv_mode_count
        );
    }
}

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
151
152
153
154
155
156
#if CONFIG_I8X8
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);
}
#endif
John Koleszar's avatar
John Koleszar committed
157
158
159
160
161
162
163
164
165
166
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
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
    );
}

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;

        if (p->skip_eob_node)
        {
            n--;
            i = 2;
        }

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

216
            shift = vp8_norm[range];
John Koleszar's avatar
John Koleszar committed
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
            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;
                    }

276
                    shift = vp8_norm[range];
John Koleszar's avatar
John Koleszar committed
277
278
279
280
281
282
283
284
285
286
287
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
                    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 pack_tokens_into_partitions_c(VP8_COMP *cpi, unsigned char *cx_data, int num_part, int *size)
{

    int i;
    unsigned char *ptr = cx_data;
    unsigned int shift;
    vp8_writer *w = &cpi->bc2;
    *size = 3 * (num_part - 1);
382
    cpi->partition_sz[0] += *size;
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
401
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
    ptr = cx_data + (*size);

    for (i = 0; i < num_part; i++)
    {
        vp8_start_encode(w, ptr);
        {
            unsigned int split;
            int count = w->count;
            unsigned int range = w->range;
            unsigned int lowvalue = w->lowvalue;
            int mb_row;

            for (mb_row = i; mb_row < cpi->common.mb_rows; mb_row += num_part)
            {
                TOKENEXTRA *p    = cpi->tplist[mb_row].start;
                TOKENEXTRA *stop = cpi->tplist[mb_row].stop;

                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;

                    if (p->skip_eob_node)
                    {
                        n--;
                        i = 2;
                    }

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

432
                        shift = vp8_norm[range];
John Koleszar's avatar
John Koleszar committed
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
                        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;
                                }

492
                                shift = vp8_norm[range];
John Koleszar's avatar
John Koleszar committed
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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
                                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;

        }

        vp8_stop_encode(w);
        *size +=   w->pos;

579
580
581
        /* The first partition size is set earlier */
        cpi->partition_sz[i + 1] = w->pos;

John Koleszar's avatar
John Koleszar committed
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
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
        if (i < (num_part - 1))
        {
            write_partition_size(cx_data, w->pos);
            cx_data += 3;
            ptr += w->pos;
        }
    }
}


static void pack_mb_row_tokens_c(VP8_COMP *cpi, vp8_writer *w)
{

    unsigned int split;
    int count = w->count;
    unsigned int range = w->range;
    unsigned int lowvalue = w->lowvalue;
    unsigned int shift;
    int mb_row;

    for (mb_row = 0; mb_row < cpi->common.mb_rows; mb_row++)
    {
        TOKENEXTRA *p    = cpi->tplist[mb_row].start;
        TOKENEXTRA *stop = cpi->tplist[mb_row].stop;

        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;

            if (p->skip_eob_node)
            {
                n--;
                i = 2;
            }

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

639
                shift = vp8_norm[range];
John Koleszar's avatar
John Koleszar committed
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
                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;
                        }

699
                        shift = vp8_norm[range];
John Koleszar's avatar
John Koleszar committed
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
                        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_mv_ref
(
    vp8_writer *w, MB_PREDICTION_MODE m, const vp8_prob *p
)
{
788
#if CONFIG_DEBUG
John Koleszar's avatar
John Koleszar committed
789
    assert(NEARESTMV <= m  &&  m <= SPLITMV);
790
#endif
791
792
    vp8_write_token(w, vp8_mv_ref_tree, p,
                    vp8_mv_ref_encoding_array - NEARESTMV + m);
John Koleszar's avatar
John Koleszar committed
793
794
795
796
797
798
799
}

static void write_sub_mv_ref
(
    vp8_writer *w, B_PREDICTION_MODE m, const vp8_prob *p
)
{
800
#if CONFIG_DEBUG
John Koleszar's avatar
John Koleszar committed
801
    assert(LEFT4X4 <= m  &&  m <= NEW4X4);
802
#endif
803
804
    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
805
806
807
808
}

static void write_mv
(
Scott LaVarnway's avatar
Scott LaVarnway committed
809
    vp8_writer *w, const MV *mv, const int_mv *ref, const MV_CONTEXT *mvc
John Koleszar's avatar
John Koleszar committed
810
811
812
)
{
    MV e;
Scott LaVarnway's avatar
Scott LaVarnway committed
813
814
    e.row = mv->row - ref->as_mv.row;
    e.col = mv->col - ref->as_mv.col;
John Koleszar's avatar
John Koleszar committed
815
816
817
818

    vp8_encode_motion_vector(w, &e, mvc);
}

Paul Wilkins's avatar
Paul Wilkins committed
819
820
821
822
// 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
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
853
854
{
    // 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
855
856
857
858
859
860
861
862
863
864
// This function encodes the reference frame
static void encode_ref_frame( vp8_writer *const w,
                              MACROBLOCKD *xd,
                              int segment_id,
                              MV_REFERENCE_FRAME rf,
                              int prob_intra_coded,
                              int prob_last_coded,
                              int prob_gf_coded )
{
    int seg_ref_active;
865
//#if CONFIG_SEGFEATURES
Paul Wilkins's avatar
Paul Wilkins committed
866
867
868
869
    seg_ref_active = segfeature_active( xd,
                                        segment_id,
                                        SEG_LVL_REF_FRAME );

Paul Wilkins's avatar
Paul Wilkins committed
870
    // No segment features or segment reference frame feature is disabled
Paul Wilkins's avatar
Paul Wilkins committed
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
    if ( !seg_ref_active )
    {
        if (rf == INTRA_FRAME)
        {
            vp8_write(w, 0, prob_intra_coded);
        }
        else    /* inter coded */
        {
            vp8_write(w, 1, prob_intra_coded);

            if (rf == LAST_FRAME)
            {
                vp8_write(w, 0, prob_last_coded);
            }
            else
            {
                vp8_write(w, 1, prob_last_coded);
                vp8_write(w, (rf == GOLDEN_FRAME) ? 0 : 1, prob_gf_coded);
            }
        }
    }
892
//#if CONFIG_SEGFEATURES
Paul Wilkins's avatar
Paul Wilkins committed
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
    else
    {
        if (rf == INTRA_FRAME)
        {
            // This MB intra coded. If inter also allowed we must code
            // an explicit inter/intra flag.
            if ( check_segref_inter( xd, segment_id ) )
                vp8_write(w, 0, prob_intra_coded);
        }
        else    /* inter coded */
        {
            // If intra also allowed we must code an explicit intra/inter flag.
            if ( check_segref( xd, segment_id, INTRA_FRAME ) )
                vp8_write(w, 1, prob_intra_coded);

            if (rf == LAST_FRAME)
            {
                // If GOLDEN or ALTREF allowed we must code explicit flag.
                if ( check_segref( xd, segment_id, GOLDEN_FRAME ) ||
                     check_segref( xd, segment_id, ALTREF_FRAME ) )
                {
                    vp8_write(w, 0, prob_last_coded);
                }
            }
            else
            {
                // if LAST is allowed we must code  explicit flag
                if ( check_segref( xd, segment_id, LAST_FRAME ) )
                {
                    vp8_write(w, 1, prob_last_coded);
                }

                // if GOLDEN and ALTREF allowed we must code an explicit flag
                if ( check_segref( xd, segment_id, GOLDEN_FRAME ) &&
                     check_segref( xd, segment_id, ALTREF_FRAME ) )
                {
                    vp8_write(w, (rf == GOLDEN_FRAME) ? 0 : 1, prob_gf_coded);
                }
            }
        }
    }
}
John Koleszar's avatar
John Koleszar committed
935
936
937
938
939
940

static void pack_inter_mode_mvs(VP8_COMP *const cpi)
{
    VP8_COMMON *const pc = & cpi->common;
    vp8_writer *const w = & cpi->bc;
    const MV_CONTEXT *mvc = pc->fc.mvc;
941
    MACROBLOCKD *xd = &cpi->mb.e_mbd;
942

943
    int i;
944
    int pred_context;
945
    int index = 0;
946

John Koleszar's avatar
John Koleszar committed
947
948
949
950
    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];

951
952
953
954
955
    MODE_INFO *m = pc->mi;
#if CONFIG_NEWNEAR
    MODE_INFO *prev_m = pc->prev_mi;
#endif

John Koleszar's avatar
John Koleszar committed
956
957
958
959
960
961
    const int mis = pc->mode_info_stride;
    int mb_row = -1;

    int prob_last_coded;
    int prob_gf_coded;
    int prob_skip_false = 0;
962
963
964
#if CONFIG_DUALPRED
    int prob_dual_pred[3];
#endif /* CONFIG_DUALPRED */
John Koleszar's avatar
John Koleszar committed
965

966
967
    cpi->mb.partition_info = cpi->mb.pi;

968
969
970
    // Calculate the probabilities to be used to code the reference frame
    // based on actual useage this frame
//#if CONFIG_SEGFEATURES
971
972
    cpi->prob_intra_coded = (rf_intra + rf_inter)
                            ? rf_intra * 255 / (rf_intra + rf_inter) : 1;
973

974
    if (!cpi->prob_intra_coded)
John Koleszar's avatar
John Koleszar committed
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
        cpi->prob_intra_coded = 1;

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

    if (!prob_last_coded)
        prob_last_coded = 1;

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

    if (!prob_gf_coded)
        prob_gf_coded = 1;


#ifdef ENTROPY_STATS
    active_section = 1;
#endif

    if (pc->mb_no_coeff_skip)
    {
995
996
997
998
999
        // Divide by 0 check. 0 case possible with segment features
        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
1000

1001
1002
            if (prob_skip_false <= 1)
                prob_skip_false = 1;
John Koleszar's avatar
John Koleszar committed
1003

1004
1005
1006
1007
            if (prob_skip_false > 255)
                prob_skip_false = 255;
        }
        else
John Koleszar's avatar
John Koleszar committed
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
            prob_skip_false = 255;

        cpi->prob_skip_false = prob_skip_false;
        vp8_write_literal(w, prob_skip_false, 8);
    }

    vp8_write_literal(w, cpi->prob_intra_coded, 8);
    vp8_write_literal(w, prob_last_coded, 8);
    vp8_write_literal(w, prob_gf_coded, 8);

1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
#if CONFIG_DUALPRED
    if (cpi->common.dual_pred_mode == HYBRID_PREDICTION)
    {
        vp8_write(w, 1, 128);
        vp8_write(w, 1, 128);
        for (i = 0; i < 3; i++) {
        if (cpi->single_pred_count[i] + cpi->dual_pred_count[i])
        {
            prob_dual_pred[i] = cpi->single_pred_count[i] * 256 /
                        (cpi->single_pred_count[i] + cpi->dual_pred_count[i]);
            if (prob_dual_pred[i] < 1)
                prob_dual_pred[i] = 1;
            else if (prob_dual_pred[i] > 255)
                prob_dual_pred[i] = 255;
        }
        else
        {
            prob_dual_pred[i] = 128;
        }
        vp8_write_literal(w, prob_dual_pred[i], 8);
        }
    }
    else if (cpi->common.dual_pred_mode == SINGLE_PREDICTION_ONLY)
    {
        vp8_write(w, 0, 128);
    }
    else /* dual prediction only */
    {
        vp8_write(w, 1, 128);
        vp8_write(w, 0, 128);
    }
#endif /* CONFIG_DUALPRED */

John Koleszar's avatar
John Koleszar committed
1051
1052
1053
    update_mbintra_mode_probs(cpi);

    vp8_write_mvprobs(cpi);
Paul Wilkins's avatar
Paul Wilkins committed
1054

John Koleszar's avatar
John Koleszar committed
1055
1056
1057
1058
1059
1060
1061
1062
1063
    while (++mb_row < pc->mb_rows)
    {
        int mb_col = -1;

        while (++mb_col < pc->mb_cols)
        {
            const MB_MODE_INFO *const mi = & m->mbmi;
            const MV_REFERENCE_FRAME rf = mi->ref_frame;
            const MB_PREDICTION_MODE mode = mi->mode;
1064
            const int segment_id = mi->segment_id;
John Koleszar's avatar
John Koleszar committed
1065
1066
1067
1068
1069
1070
1071

            // 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;
Paul Wilkins's avatar
Paul Wilkins committed
1072
1073
1074

            // Make sure the MacroBlockD mode info pointer is set correctly
            xd->mode_info_context = m;
1075
1076
1077
#if CONFIG_NEWNEAR
            xd->prev_mode_info_context = prev_m;
#endif
Paul Wilkins's avatar
Paul Wilkins committed
1078

John Koleszar's avatar
John Koleszar committed
1079
1080
1081
1082
1083
#ifdef ENTROPY_STATS
            active_section = 9;
#endif

            if (cpi->mb.e_mbd.update_mb_segmentation_map)
1084
            {
1085
                // Is temporal coding of the segment map enabled
1086
                if (xd->temporal_update)
1087
                {
1088
1089
                    // Look at whether neighbours were successfully predicted
                    // to create a context for the seg_id_predicted flag.
1090
                    pred_context = 0;
1091
                    if (mb_col != 0)
1092
                        pred_context += (m-1)->mbmi.seg_id_predicted;
1093
                    if (mb_row != 0)
1094
                        pred_context += (m-pc->mb_cols)->mbmi.seg_id_predicted;
1095

1096
1097
1098
1099
1100
1101
                    // Code the prediction flag for this mb
                    vp8_write( w, m->mbmi.seg_id_predicted,
                               xd->mb_segment_pred_probs[pred_context]);

                    // If the mbs segment id was not predicted code explicitly
                    if (!m->mbmi.seg_id_predicted)
Paul Wilkins's avatar
Paul Wilkins committed
1102
                        write_mb_segid(w, mi, &cpi->mb.e_mbd);
1103
1104
1105
                }
                else
                {
1106
                    // Normal undpredicted coding
Paul Wilkins's avatar
Paul Wilkins committed
1107
                    write_mb_segid(w, mi, &cpi->mb.e_mbd);
1108
                }
1109
1110
                index++;
            }
John Koleszar's avatar
John Koleszar committed
1111

1112
//#if CONFIG_SEGFEATURES
1113
1114
            if ( pc->mb_no_coeff_skip &&
                 ( !segfeature_active( xd, segment_id, SEG_LVL_EOB ) ||
Paul Wilkins's avatar
Paul Wilkins committed
1115
                   ( get_segdata( xd, segment_id, SEG_LVL_EOB ) != 0 ) ) )
1116
1117
1118
            {
                vp8_encode_bool(w, mi->mb_skip_coeff, prob_skip_false);
            }
John Koleszar's avatar
John Koleszar committed
1119

Paul Wilkins's avatar
Paul Wilkins committed
1120
1121
1122
1123
1124
            // Encode the reference frame.
            encode_ref_frame( w, xd, segment_id, rf,
                              cpi->prob_intra_coded,
                              prob_last_coded, prob_gf_coded );

John Koleszar's avatar
John Koleszar committed
1125
1126
            if (rf == INTRA_FRAME)
            {
Paul Wilkins's avatar
Paul Wilkins committed
1127
    #ifdef ENTROPY_STATS
John Koleszar's avatar
John Koleszar committed
1128
                active_section = 6;
Paul Wilkins's avatar
Paul Wilkins committed
1129
1130
    #endif

1131
//#if CONFIG_SEGFEATURES
Paul Wilkins's avatar
Paul Wilkins committed
1132
1133
                if ( !segfeature_active( xd, segment_id, SEG_LVL_MODE ) )
                    write_ymode(w, mode, pc->fc.ymode_prob);
John Koleszar's avatar
John Koleszar committed
1134
1135
1136
1137
1138
1139

                if (mode == B_PRED)
                {
                    int j = 0;

                    do
Scott LaVarnway's avatar
Scott LaVarnway committed
1140
                        write_bmode(w, m->bmi[j].as_mode, pc->fc.bmode_prob);
John Koleszar's avatar
John Koleszar committed
1141
1142
                    while (++j < 16);
                }
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
#if CONFIG_I8X8
                if(mode == I8X8_PRED)
                {
                    write_i8x8_mode(w, m->bmi[0].as_mode, pc->i8x8_mode_prob);
                    write_i8x8_mode(w, m->bmi[2].as_mode, pc->i8x8_mode_prob);
                    write_i8x8_mode(w, m->bmi[8].as_mode, pc->i8x8_mode_prob);
                    write_i8x8_mode(w, m->bmi[10].as_mode, pc->i8x8_mode_prob);
                }
                else
#endif
John Koleszar's avatar
John Koleszar committed
1153
1154
1155

                write_uv_mode(w, mi->uv_mode, pc->fc.uv_mode_prob);
            }
Paul Wilkins's avatar
Paul Wilkins committed
1156
            else
John Koleszar's avatar
John Koleszar committed
1157
            {
Scott LaVarnway's avatar
Scott LaVarnway committed
1158
                int_mv best_mv;
John Koleszar's avatar
John Koleszar committed
1159
1160
1161
                vp8_prob mv_ref_p [VP8_MVREFS-1];

                {
Scott LaVarnway's avatar
Scott LaVarnway committed
1162
                    int_mv n1, n2;
John Koleszar's avatar
John Koleszar committed
1163
1164
                    int ct[4];

1165
1166
1167
1168
1169
                    vp8_find_near_mvs(xd, m,
#if CONFIG_NEWNEAR
                        prev_m,
#endif
                        &n1, &n2, &best_mv, ct, rf, cpi->common.ref_frame_sign_bias);
John Koleszar's avatar
John Koleszar committed
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
                    vp8_mv_ref_probs(mv_ref_p, ct);

#ifdef ENTROPY_STATS
                    accum_mv_refs(mode, ct);
#endif
                }

#ifdef ENTROPY_STATS
                active_section = 3;
#endif

1181
//#if CONFIG_SEGFEATURES
1182
                // Is the segment coding of reference frame enabled
1183
                if ( !segfeature_active( xd, segment_id, SEG_LVL_MODE ) )
John Koleszar's avatar
John Koleszar committed
1184
                {
1185
                    write_mv_ref(w, mode, mv_ref_p);
Paul Wilkins's avatar
Paul Wilkins committed
1186
                }
1187

Paul Wilkins's avatar
Paul Wilkins committed
1188
                {
1189
1190
1191
1192
1193
1194
                    switch (mode)   /* new, split require MVs */
                    {
                    case NEWMV:
    #ifdef ENTROPY_STATS
                        active_section = 5;
    #endif
John Koleszar's avatar
John Koleszar committed
1195

1196
                        write_mv(w, &mi->mv.as_mv, &best_mv, mvc);
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
#if CONFIG_DUALPRED
                        if (cpi->common.dual_pred_mode == HYBRID_PREDICTION)
                        {
                            int t = m[-mis].mbmi.second_ref_frame != INTRA_FRAME;
                            int l = m[-1  ].mbmi.second_ref_frame != INTRA_FRAME;
                            vp8_write(w, mi->second_ref_frame != INTRA_FRAME,
                                      prob_dual_pred[t + l]);
                        }
                        if (mi->second_ref_frame)
                        {
                            const int second_rf = mi->second_ref_frame;
                            int_mv n1, n2;
                            int ct[4];
Paul Wilkins's avatar
Paul Wilkins committed
1210
1211
1212
1213
1214
1215
1216
                            vp8_find_near_mvs(xd, m,
#if CONFIG_NEWNEAR
                                              prev_m,
#endif
                                              &n1, &n2, &best_mv,
                                              ct, second_rf,
                                              cpi->common.ref_frame_sign_bias);
1217
1218
1219
                            write_mv(w, &mi->second_mv.as_mv, &best_mv, mvc);
                        }
#endif /* CONFIG_DUALPRED */
1220
1221
                        break;
                    case SPLITMV:
John Koleszar's avatar
John Koleszar committed
1222
                    {
1223
                        int j = 0;
John Koleszar's avatar
John Koleszar committed
1224

1225
1226
1227
    #ifdef MODE_STATS
                        ++count_mb_seg [mi->partitioning];
    #endif
John Koleszar's avatar
John Koleszar committed
1228

1229
1230
1231
                        write_split(w, mi->partitioning);

                        do
John Koleszar's avatar
John Koleszar committed
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
                            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;
    #if CONFIG_DEBUG
                            while (j != L[++k])
                                if (k >= 16)
                                    assert(0);
    #else
                            while (j != L[++k]);
    #endif
                            leftmv.as_int = left_block_mv(m, k);
                            abovemv.as_int = above_block_mv(m, k, mis);
                            mv_contz = vp8_mv_cont(&leftmv, &abovemv);

                            write_sub_mv_ref(w, blockmode, vp8_sub_mv_ref_prob2 [mv_contz]);

                            if (blockmode == NEW4X4)
                            {
    #ifdef ENTROPY_STATS
                                active_section = 11;
    #endif
                                write_mv(w, &blockmv.as_mv, &best_mv, (const MV_CONTEXT *) mvc);
                            }
John Koleszar's avatar
John Koleszar committed
1262
                        }
1263
                        while (++j < cpi->mb.partition_info->count);
John Koleszar's avatar
John Koleszar committed
1264
1265
                    }
                    break;
1266
                    default:
1267
1268
1269
1270
1271
1272
1273
1274
1275
#if CONFIG_DUALPRED
                        if (cpi->common.dual_pred_mode == HYBRID_PREDICTION)
                        {
                            int t = m[-mis].mbmi.second_ref_frame != INTRA_FRAME;
                            int l = m[-1  ].mbmi.second_ref_frame != INTRA_FRAME;
                            vp8_write(w, mi->second_ref_frame != INTRA_FRAME,
                                      prob_dual_pred[t + l]);
                        }
#endif /* CONFIG_DUALPRED */
1276
1277
                        break;
                    }
John Koleszar's avatar
John Koleszar committed
1278
1279
1280
1281
                }
            }

            ++m;
1282
1283
1284
1285
1286
1287
#if CONFIG_NEWNEAR
            ++prev_m;
            assert((prev_m-cpi->common.prev_mip)==(m-cpi->common.mip));
            assert((prev_m-cpi->common.prev_mi)==(m-cpi->common.mi));
#endif

1288
            cpi->mb.partition_info++;
John Koleszar's avatar
John Koleszar committed
1289
1290
1291
        }

        ++m;  /* skip L prediction border */
1292
1293
1294
#if CONFIG_NEWNEAR
        ++prev_m;
#endif
1295
        cpi->mb.partition_info++;
John Koleszar's avatar
John Koleszar committed
1296
    }
1297
1298
1299
1300
1301
1302
1303
1304
1305

#if CONFIG_DUALPRED
    if (cpi->common.dual_pred_mode == HYBRID_PREDICTION)
    {
        cpi->prob_dualpred[0] = (prob_dual_pred[0] + cpi->prob_dualpred[0] + 1) >> 1;
        cpi->prob_dualpred[1] = (prob_dual_pred[1] + cpi->prob_dualpred[1] + 1) >> 1;
        cpi->prob_dualpred[2] = (prob_dual_pred[2] + cpi->prob_dualpred[2] + 1) >> 1;
    }
#endif /* CONFIG_DUALPRED */
John Koleszar's avatar
John Koleszar committed
1306
1307
1308
1309
1310
1311
1312
1313
1314
}


static void write_kfmodes(VP8_COMP *cpi)
{
    vp8_writer *const bc = & cpi->bc;
    const VP8_COMMON *const c = & cpi->common;
    /* const */
    MODE_INFO *m = c->mi;
Yaowu Xu's avatar
Yaowu Xu committed
1315
    int i;
1316
    int index = 0;
John Koleszar's avatar
John Koleszar committed
1317
1318
1319
    int mb_row = -1;
    int prob_skip_false = 0;

1320
//#if CONFIG_SEGFEATURES
1321
1322
    MACROBLOCKD *xd = &cpi->mb.e_mbd;

John Koleszar's avatar
John Koleszar committed
1323
1324
    if (c->mb_no_coeff_skip)
    {
1325
1326
1327
1328
1329
        // Divide by 0 check. 0 case possible with segment features
        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
1330

1331
1332
            if (prob_skip_false <= 1)
                prob_skip_false = 1;
John Koleszar's avatar
John Koleszar committed
1333

1334
1335
1336
1337
            if (prob_skip_false > 255)
                prob_skip_false = 255;
        }
        else
John Koleszar's avatar
John Koleszar committed
1338
1339
1340
1341
1342
1343
            prob_skip_false = 255;

        cpi->prob_skip_false = prob_skip_false;
        vp8_write_literal(bc, prob_skip_false, 8);
    }

1344
1345
1346
1347
1348
1349
1350
#if CONFIG_QIMODE
    if(!c->kf_ymode_probs_update)
    {
        vp8_write_literal(bc, c->kf_ymode_probs_index, 3);
    }
#endif

John Koleszar's avatar
John Koleszar committed
1351
1352
1353
1354
1355
1356
1357
    while (++mb_row < c->mb_rows)
    {
        int mb_col = -1;

        while (++mb_col < c->mb_cols)
        {
            const int ym = m->mbmi.mode;
1358
1359
            int segment_id = m->mbmi.segment_id;

John Koleszar's avatar
John Koleszar committed
1360
            if (cpi->mb.e_mbd.update_mb_segmentation_map)
1361
            {
1362
                index++;
1363
                write_mb_segid(bc, &m->mbmi, &cpi->mb.e_mbd);
1364
            }
John Koleszar's avatar
John Koleszar committed
1365

1366
//#if CONFIG_SEGFEATURES
1367
1368
            if ( c->mb_no_coeff_skip &&
                 ( !segfeature_active( xd, segment_id, SEG_LVL_EOB ) ||
Paul Wilkins's avatar
Paul Wilkins committed
1369
                   (get_segdata( xd, segment_id, SEG_LVL_EOB ) != 0) ) )
1370
            {
John Koleszar's avatar
John Koleszar committed
1371
                vp8_encode_bool(bc, m->mbmi.mb_skip_coeff, prob_skip_false);
1372
            }
1373
#if CONFIG_QIMODE
1374
            kfwrite_ymode(bc, ym, c->kf_ymode_prob[c->kf_ymode_probs_index]);
1375
#else
John Koleszar's avatar
John Koleszar committed
1376
            kfwrite_ymode(bc, ym, c->kf_ymode_prob);
1377
#endif
John Koleszar's avatar
John Koleszar committed
1378
1379
1380
1381
1382
1383
1384
            if (ym == B_PRED)
            {
                const int mis = c->mode_info_stride;
                int i = 0;

                do
                {
Scott LaVarnway's avatar
Scott LaVarnway committed
1385
1386
1387
                    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;
John Koleszar's avatar
John Koleszar committed
1388
1389
1390
1391
1392
1393
1394
1395
1396

#ifdef ENTROPY_STATS
                    ++intra_mode_stats [A] [L] [bm];
#endif

                    write_bmode(bc, bm, c->kf_bmode_prob [A] [L]);
                }
                while (++i < 16);
            }
Yaowu Xu's avatar
Yaowu Xu committed
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
#if CONFIG_I8X8
            if(ym == I8X8_PRED)
            {
                write_i8x8_mode(bc, m->bmi[0].as_mode, c->i8x8_mode_prob);
                write_i8x8_mode(bc, m->bmi[2].as_mode, c->i8x8_mode_prob);
                write_i8x8_mode(bc, m->bmi[8].as_mode, c->i8x8_mode_prob);
                write_i8x8_mode(bc, m->bmi[10].as_mode, c->i8x8_mode_prob);
                m++;
            }
            else
#endif
1408
1409
1410
#if CONFIG_UVINTRA
            write_uv_mode(bc, (m++)->mbmi.uv_mode, c->kf_uv_mode_prob[ym]);
#else
John Koleszar's avatar
John Koleszar committed
1411
            write_uv_mode(bc, (m++)->mbmi.uv_mode, c->kf_uv_mode_prob);
1412
#endif
John Koleszar's avatar
John Koleszar committed
1413
        }
Yaowu Xu's avatar
Yaowu Xu committed
1414
        //printf("\n");
John Koleszar's avatar
John Koleszar committed
1415
1416
1417
        m++;    // skip L prediction border
    }
}
1418
1419
1420

/* This function is used for debugging probability trees. */
static void print_prob_tree(vp8_prob
1421
     coef_probs[BLOCK_TYPES][COEF_BANDS][PREV_COEF_CONTEXTS][ENTROPY_NODES])
John Koleszar's avatar
John Koleszar committed
1422
{
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
    /* print coef probability tree */
    int i,j,k,l;
    FILE* f = fopen("enc_tree_probs.txt", "a");
    fprintf(f, "{\n");
    for (i = 0; i < BLOCK_TYPES; i++)
    {
        fprintf(f, "  {\n");
        for (j = 0; j < COEF_BANDS; j++)
        {
            fprintf(f, "    {\n");
            for (k = 0; k < PREV_COEF_CONTEXTS; k++)
            {
                fprintf(f, "      {");
1436
                for (l = 0; l < ENTROPY_NODES; l++)
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
                {
                    fprintf(f, "%3u, ",
                            (unsigned int)(coef_probs [i][j][k][l]));
                }
                fprintf(f, " }\n");
            }
            fprintf(f, "    }\n");
        }
        fprintf(f, "  }\n");
    }
    fprintf(f, "}\n");
    fclose(f);
}

static void sum_probs_over_prev_coef_context(
1452
        const unsigned int probs[PREV_COEF_CONTEXTS][MAX_ENTROPY_TOKENS],
1453
1454
1455
        unsigned int* out)
{
    int i, j;
1456
    for (i=0; i < MAX_ENTROPY_TOKENS; ++i)
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
    {
        for (j=0; j < PREV_COEF_CONTEXTS; ++j)
        {
            const int tmp = out[i];
            out[i] += probs[j][i];
            /* check for wrap */
            if (out[i] < tmp)
                out[i] = UINT_MAX;
        }
    }
}

static int prob_update_savings(const unsigned int *ct,
                                   const vp8_prob oldp, const vp8_prob newp,
                                   const vp8_prob upd)
{
    const int old_b = vp8_cost_branch(ct, oldp);
    const int new_b = vp8_cost_branch(ct, newp);
    const int update_b = 8 +
                         ((vp8_cost_one(upd) - vp8_cost_zero(upd)) >> 8);

    return old_b - new_b - update_b;
}

static int independent_coef_context_savings(VP8_COMP *cpi)
John Koleszar's avatar
John Koleszar committed
1482
{
1483
    int savings = 0;
John Koleszar's avatar
John Koleszar committed
1484
    int i = 0;
1485
1486
1487
1488
1489
1490
    do
    {
        int j = 0;
        do
        {
            int k = 0;
1491
1492
            unsigned int prev_coef_count_sum[MAX_ENTROPY_TOKENS] = {0};
            int prev_coef_savings[MAX_ENTROPY_TOKENS] = {0};
1493
1494
1495
1496
1497
1498
            /* Calculate new probabilities given the constraint that
             * they must be equal over the prev coef contexts
             */
            if (cpi->common.frame_type == KEY_FRAME)
            {
                /* Reset to default probabilities at key frames */
1499
                sum_probs_over_prev_coef_context(default_coef_counts[i][j],
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
                                                 prev_coef_count_sum);
            }
            else
            {
                sum_probs_over_prev_coef_context(cpi->coef_counts[i][j],
                                                 prev_coef_count_sum);
            }
            do
            {
                /* at every context */

                /* calc probs and branch cts for this frame only */
1512
1513
                //vp8_prob new_p           [ENTROPY_NODES];
                //unsigned int branch_ct   [ENTROPY_NODES] [2];
1514
1515
1516
1517

                int t = 0;      /* token/prob index */

                vp8_tree_probs_from_distribution(
1518
                    MAX_ENTROPY_TOKENS, vp8_coef_encodings, vp8_coef_tree,
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
                    cpi->frame_coef_probs[i][j][k],
                    cpi->frame_branch_ct [i][j][k],
                    prev_coef_count_sum,
                    256, 1);

                do
                {
                    const unsigned int *ct  = cpi->frame_branch_ct [i][j][k][t];
                    const vp8_prob newp = cpi->frame_coef_probs [i][j][k][t];
                    const vp8_prob oldp = cpi->common.fc.coef_probs [i][j][k][t];
                    const vp8_prob upd = vp8_coef_update_probs [i][j][k][t];
                    const int s = prob_update_savings(ct, oldp, newp, upd);

                    if (cpi->common.frame_type != KEY_FRAME ||
                        (cpi->common.frame_type == KEY_FRAME && newp != oldp))
                        prev_coef_savings[t] += s;
                }
1536
                while (++t < ENTROPY_NODES);
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
            }
            while (++k < PREV_COEF_CONTEXTS);
            k = 0;
            do
            {
                /* We only update probabilities if we can save bits, except
                 * for key frames where we have to update all probabilities
                 * to get the equal probabilities across the prev coef
                 * contexts.
                 */
                if (prev_coef_savings[k] > 0 ||
                    cpi->common.frame_type == KEY_FRAME)
                    savings += prev_coef_savings[k];
            }
1551
            while (++k < ENTROPY_NODES);
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
        }
        while (++j < COEF_BANDS);
    }
    while (++i < BLOCK_TYPES);
    return savings;
}

static int default_coef_context_savings(VP8_COMP *cpi)
{
    int savings = 0;
    int i = 0;
    do
    {
        int j = 0;
        do
        {
            int k = 0;
            do
            {
                /* at every context */

                /* calc probs and branch cts for this frame only */
1574
1575
                //vp8_prob new_p           [ENTROPY_NODES];
                //unsigned int branch_ct   [ENTROPY_NODES] [2];
1576
1577
1578
1579
1580

                int t = 0;      /* token/prob index */


                vp8_tree_probs_from_distribution(
1581
                    MAX_ENTROPY_TOKENS, vp8_coef_encodings, vp8_coef_tree,
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
                    cpi->frame_coef_probs [i][j][k],
                    cpi->frame_branch_ct [i][j][k],
                    cpi->coef_counts [i][j][k],
                    256, 1
                );

                do
                {
                    const unsigned int *ct  = cpi->frame_branch_ct [i][j][k][t];
                    const vp8_prob newp = cpi->frame_coef_probs [i][j][k][t];
                    const vp8_prob oldp = cpi->common.fc.coef_probs [i][j][k][t];
                    const vp8_prob upd = vp8_coef_update_probs [i][j][k][t];
                    const int s = prob_update_savings(ct, oldp, newp, upd);

                    if (s > 0)
                    {
                        savings += s;
                    }
                }
1601
                while (++t < ENTROPY_NODES);
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
            }
            while (++k < PREV_COEF_CONTEXTS);
        }
        while (++j < COEF_BANDS);
    }
    while (++i < BLOCK_TYPES);
    return savings;
}

int vp8_estimate_entropy_savings(VP8_COMP *cpi)
{
John Koleszar's avatar
John Koleszar committed
1613
    int savings = 0;
1614
    int i=0;
John Koleszar's avatar
John Koleszar committed
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625

    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];
    int new_intra, new_last, gf_last, oldtotal, newtotal;
    int ref_frame_cost[MAX_REF_FRAMES];

    vp8_clear_system_state(); //__asm emms;

    if (cpi->common.frame_type != KEY_FRAME)
    {
1626
//#if CONFIG_SEGFEATURES
1627
1628
        new_intra = (rf_intra + rf_inter)
                    ? rf_intra * 255 / (rf_intra + rf_inter) : 1;
1629

1630
        if (!new_intra)
John Koleszar's avatar
John Koleszar committed
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
            new_intra = 1;

        new_last = rf_inter ? (rfct[LAST_FRAME] * 255) / rf_inter : 128;

        gf_last = (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME])
                  ? (rfct[GOLDEN_FRAME] * 255) / (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME]) : 128;

        // new costs
        ref_frame_cost[INTRA_FRAME]   = vp8_cost_zero(new_intra);
        ref_frame_cost[LAST_FRAME]    = vp8_cost_one(new_intra)
                                        + vp8_cost_zero(new_last);
        ref_frame_cost[GOLDEN_FRAME]  = vp8_cost_one(new_intra)
                                        + vp8_cost_one(new_last)
                                        + vp8_cost_zero(gf_last);
        ref_frame_cost[ALTREF_FRAME]  = vp8_cost_one(new_intra)
                                        + vp8_cost_one(new_last)
                                        + vp8_cost_one(gf_last);

        newtotal =
            rfct[INTRA_FRAME] * ref_frame_cost[INTRA_FRAME] +
            rfct[LAST_FRAME] * ref_frame_cost[LAST_FRAME] +
            rfct[GOLDEN_FRAME] * ref_frame_cost[GOLDEN_FRAME] +
            rfct[ALTREF_FRAME] * ref_frame_cost[ALTREF_FRAME];


        // old costs
        ref_frame_cost[INTRA_FRAME]   = vp8_cost_zero(cpi->prob_intra_coded);
        ref_frame_cost[LAST_FRAME]    = vp8_cost_one(cpi->prob_intra_coded)
                                        + vp8_cost_zero(cpi->prob_last_coded);
        ref_frame_cost[GOLDEN_FRAME]  = vp8_cost_one(cpi->prob_intra_coded)
                                        + vp8_cost_one(cpi->prob_last_coded)
                                        + vp8_cost_zero(cpi->prob_gf_coded);
        ref_frame_cost[ALTREF_FRAME]  = vp8_cost_one(cpi->prob_intra_coded)
                                        + vp8_cost_one(cpi->prob_last_coded)
                                        + vp8_cost_one(cpi->prob_gf_coded);

        oldtotal =
            rfct[INTRA_FRAME] * ref_frame_cost[INTRA_FRAME] +
            rfct[LAST_FRAME] * ref_frame_cost[LAST_FRAME] +
            rfct[GOLDEN_FRAME] * ref_frame_cost[GOLDEN_FRAME] +
            rfct[ALTREF_FRAME] * ref_frame_cost[ALTREF_FRAME];

        savings += (oldtotal - newtotal) / 256;
    }


1677
1678
1679
1680
    if (cpi->oxcf.error_resilient_mode & VPX_ERROR_RESILIENT_PARTITIONS)
        savings += independent_coef_context_savings(cpi);
    else
        savings += default_coef_context_savings(cpi);
John Koleszar's avatar
John Koleszar committed
1681
1682


1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
#if CONFIG_T8X8
    i = 0;
    do
    {
        int j = 0;

        do
        {
            int k = 0;

            do
            {
                /* at every context */

                /* calc probs and branch cts for this frame only */
                //vp8_prob new_p           [ENTROPY_NODES];
                //unsigned int branch_ct   [ENTROPY_NODES] [2];

                int t = 0;      /* token/prob index */

                vp8_tree_probs_from_distribution(
                    MAX_ENTROPY_TOKENS, vp8_coef_encodings, vp8_coef_tree,
                    cpi->frame_coef_probs_8x8 [i][j][k], cpi->frame_branch_ct_8x8 [i][j][k], cpi->coef_counts_8x8 [i][j][k],
                    256, 1
                );

                do
                {
                    const unsigned int *ct  = cpi->frame_branch_ct_8x8 [i][j][k][t];
                    const vp8_prob newp = cpi->frame_coef_probs_8x8 [i][j][k][t];

                    const vp8_prob old = cpi->common.fc.coef_probs_8x8 [i][j][k][t];
                    const vp8_prob upd = vp8_coef_update_probs_8x8 [i][j][k][t];

                    const int old_b = vp8_cost_branch(ct, old);
                    const int new_b = vp8_cost_branch(ct, newp);

                    const int update_b = 8 +
                                         ((vp8_cost_one(upd) - vp8_cost_zero(upd)) >> 8);

                    const int s = old_b - new_b - update_b;

                    if (s > 0)
                        savings += s;


                }
                while (++t < MAX_ENTROPY_TOKENS - 1);


            }
            while (++k < PREV_COEF_CONTEXTS);
        }
        while (++j < COEF_BANDS);
    }
    while (++i < BLOCK_TYPES);
#endif


John Koleszar's avatar
John Koleszar committed
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
    return savings;
}

static void update_coef_probs(VP8_COMP *cpi)
{
    int i = 0;
    vp8_writer *const w = & cpi->bc;
    int savings = 0;

    vp8_clear_system_state(); //__asm emms;

    do
    {
        int j