Home | History | Annotate | Download | only in encoder
      1 /*
      2  *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
      3  *
      4  *  Use of this source code is governed by a BSD-style license
      5  *  that can be found in the LICENSE file in the root of the source
      6  *  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.
      9  */
     10 
     11 
     12 #include "header.h"
     13 #include "encodemv.h"
     14 #include "entropymode.h"
     15 #include "findnearmv.h"
     16 #include "mcomp.h"
     17 #include "systemdependent.h"
     18 #include <assert.h>
     19 #include <stdio.h>
     20 #include "pragmas.h"
     21 #include "vpx_mem/vpx_mem.h"
     22 #include "bitstream.h"
     23 
     24 const int vp8cx_base_skip_false_prob[128] =
     25 {
     26     255, 255, 255, 255, 255, 255, 255, 255,
     27     255, 255, 255, 255, 255, 255, 255, 255,
     28     255, 255, 255, 255, 255, 255, 255, 255,
     29     255, 255, 255, 255, 255, 255, 255, 255,
     30     255, 255, 255, 255, 255, 255, 255, 255,
     31     255, 255, 255, 255, 255, 255, 255, 255,
     32     255, 255, 255, 255, 255, 255, 255, 255,
     33     251, 248, 244, 240, 236, 232, 229, 225,
     34     221, 217, 213, 208, 204, 199, 194, 190,
     35     187, 183, 179, 175, 172, 168, 164, 160,
     36     157, 153, 149, 145, 142, 138, 134, 130,
     37     127, 124, 120, 117, 114, 110, 107, 104,
     38     101, 98,  95,  92,  89,  86,  83, 80,
     39     77,  74,  71,  68,  65,  62,  59, 56,
     40     53,  50,  47,  44,  41,  38,  35, 32,
     41     30,  28,  26,  24,  22,  20,  18, 16,
     42 };
     43 #ifdef VP8REF
     44 #define __int64 long long
     45 #endif
     46 
     47 #if defined(SECTIONBITS_OUTPUT)
     48 unsigned __int64 Sectionbits[500];
     49 #endif
     50 
     51 #ifdef ENTROPY_STATS
     52 int intra_mode_stats[10][10][10];
     53 static unsigned int tree_update_hist [BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [vp8_coef_tokens-1] [2];
     54 extern unsigned int active_section;
     55 #endif
     56 
     57 #ifdef MODE_STATS
     58 int count_mb_seg[4] = { 0, 0, 0, 0 };
     59 #endif
     60 
     61 #if CONFIG_BIG_ENDIAN
     62 # define make_endian_16(a)  \
     63     (((unsigned int)(a & 0xff)) << 8) | (((unsigned int)(a & 0xff00)) >> 8)
     64 # define make_endian_32(a)                              \
     65     (((unsigned int)(a & 0xff)) << 24)    | (((unsigned int)(a & 0xff00)) << 8) |   \
     66     (((unsigned int)(a & 0xff0000)) >> 8) | (((unsigned int)(a & 0xff000000)) >> 24)
     67 #else
     68 # define make_endian_16(a)  a
     69 # define make_endian_32(a)  a
     70 #endif
     71 
     72 static void update_mode(
     73     vp8_writer *const w,
     74     int n,
     75     vp8_token tok               [/* n */],
     76     vp8_tree tree,
     77     vp8_prob Pnew               [/* n-1 */],
     78     vp8_prob Pcur               [/* n-1 */],
     79     unsigned int bct            [/* n-1 */] [2],
     80     const unsigned int num_events[/* n */]
     81 )
     82 {
     83     unsigned int new_b = 0, old_b = 0;
     84     int i = 0;
     85 
     86     vp8_tree_probs_from_distribution(
     87         n--, tok, tree,
     88         Pnew, bct, num_events,
     89         256, 1
     90     );
     91 
     92     do
     93     {
     94         new_b += vp8_cost_branch(bct[i], Pnew[i]);
     95         old_b += vp8_cost_branch(bct[i], Pcur[i]);
     96     }
     97     while (++i < n);
     98 
     99     if (new_b + (n << 8) < old_b)
    100     {
    101         int i = 0;
    102 
    103         vp8_write_bit(w, 1);
    104 
    105         do
    106         {
    107             const vp8_prob p = Pnew[i];
    108 
    109             vp8_write_literal(w, Pcur[i] = p ? p : 1, 8);
    110         }
    111         while (++i < n);
    112     }
    113     else
    114         vp8_write_bit(w, 0);
    115 }
    116 
    117 static void update_mbintra_mode_probs(VP8_COMP *cpi)
    118 {
    119     VP8_COMMON *const x = & cpi->common;
    120 
    121     vp8_writer *const w = & cpi->bc;
    122 
    123     {
    124         vp8_prob Pnew   [VP8_YMODES-1];
    125         unsigned int bct [VP8_YMODES-1] [2];
    126 
    127         update_mode(
    128             w, VP8_YMODES, vp8_ymode_encodings, vp8_ymode_tree,
    129             Pnew, x->fc.ymode_prob, bct, (unsigned int *)cpi->ymode_count
    130         );
    131     }
    132     {
    133         vp8_prob Pnew   [VP8_UV_MODES-1];
    134         unsigned int bct [VP8_UV_MODES-1] [2];
    135 
    136         update_mode(
    137             w, VP8_UV_MODES, vp8_uv_mode_encodings, vp8_uv_mode_tree,
    138             Pnew, x->fc.uv_mode_prob, bct, (unsigned int *)cpi->uv_mode_count
    139         );
    140     }
    141 }
    142 
    143 static void write_ymode(vp8_writer *bc, int m, const vp8_prob *p)
    144 {
    145     vp8_write_token(bc, vp8_ymode_tree, p, vp8_ymode_encodings + m);
    146 }
    147 
    148 static void kfwrite_ymode(vp8_writer *bc, int m, const vp8_prob *p)
    149 {
    150     vp8_write_token(bc, vp8_kf_ymode_tree, p, vp8_kf_ymode_encodings + m);
    151 }
    152 
    153 static void write_uv_mode(vp8_writer *bc, int m, const vp8_prob *p)
    154 {
    155     vp8_write_token(bc, vp8_uv_mode_tree, p, vp8_uv_mode_encodings + m);
    156 }
    157 
    158 
    159 static void write_bmode(vp8_writer *bc, int m, const vp8_prob *p)
    160 {
    161     vp8_write_token(bc, vp8_bmode_tree, p, vp8_bmode_encodings + m);
    162 }
    163 
    164 static void write_split(vp8_writer *bc, int x)
    165 {
    166     vp8_write_token(
    167         bc, vp8_mbsplit_tree, vp8_mbsplit_probs, vp8_mbsplit_encodings + x
    168     );
    169 }
    170 
    171 static const unsigned int norm[256] =
    172 {
    173     0, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
    174     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
    175     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
    176     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
    177     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    178     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    179     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    180     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
    181 };
    182 
    183 static void pack_tokens_c(vp8_writer *w, const TOKENEXTRA *p, int xcount)
    184 {
    185     const TOKENEXTRA *const stop = p + xcount;
    186     unsigned int split;
    187     unsigned int shift;
    188     int count = w->count;
    189     unsigned int range = w->range;
    190     unsigned int lowvalue = w->lowvalue;
    191 
    192     while (p < stop)
    193     {
    194         const int t = p->Token;
    195         vp8_token *const a = vp8_coef_encodings + t;
    196         const vp8_extra_bit_struct *const b = vp8_extra_bits + t;
    197         int i = 0;
    198         const unsigned char *pp = p->context_tree;
    199         int v = a->value;
    200         int n = a->Len;
    201 
    202         if (p->skip_eob_node)
    203         {
    204             n--;
    205             i = 2;
    206         }
    207 
    208         do
    209         {
    210             const int bb = (v >> --n) & 1;
    211             split = 1 + (((range - 1) * pp[i>>1]) >> 8);
    212             i = vp8_coef_tree[i+bb];
    213 
    214             if (bb)
    215             {
    216                 lowvalue += split;
    217                 range = range - split;
    218             }
    219             else
    220             {
    221                 range = split;
    222             }
    223 
    224             shift = norm[range];
    225             range <<= shift;
    226             count += shift;
    227 
    228             if (count >= 0)
    229             {
    230                 int offset = shift - count;
    231 
    232                 if ((lowvalue << (offset - 1)) & 0x80000000)
    233                 {
    234                     int x = w->pos - 1;
    235 
    236                     while (x >= 0 && w->buffer[x] == 0xff)
    237                     {
    238                         w->buffer[x] = (unsigned char)0;
    239                         x--;
    240                     }
    241 
    242                     w->buffer[x] += 1;
    243                 }
    244 
    245                 w->buffer[w->pos++] = (lowvalue >> (24 - offset));
    246                 lowvalue <<= offset;
    247                 shift = count;
    248                 lowvalue &= 0xffffff;
    249                 count -= 8 ;
    250             }
    251 
    252             lowvalue <<= shift;
    253         }
    254         while (n);
    255 
    256 
    257         if (b->base_val)
    258         {
    259             const int e = p->Extra, L = b->Len;
    260 
    261             if (L)
    262             {
    263                 const unsigned char *pp = b->prob;
    264                 int v = e >> 1;
    265                 int n = L;              /* number of bits in v, assumed nonzero */
    266                 int i = 0;
    267 
    268                 do
    269                 {
    270                     const int bb = (v >> --n) & 1;
    271                     split = 1 + (((range - 1) * pp[i>>1]) >> 8);
    272                     i = b->tree[i+bb];
    273 
    274                     if (bb)
    275                     {
    276                         lowvalue += split;
    277                         range = range - split;
    278                     }
    279                     else
    280                     {
    281                         range = split;
    282                     }
    283 
    284                     shift = norm[range];
    285                     range <<= shift;
    286                     count += shift;
    287 
    288                     if (count >= 0)
    289                     {
    290                         int offset = shift - count;
    291 
    292                         if ((lowvalue << (offset - 1)) & 0x80000000)
    293                         {
    294                             int x = w->pos - 1;
    295 
    296                             while (x >= 0 && w->buffer[x] == 0xff)
    297                             {
    298                                 w->buffer[x] = (unsigned char)0;
    299                                 x--;
    300                             }
    301 
    302                             w->buffer[x] += 1;
    303                         }
    304 
    305                         w->buffer[w->pos++] = (lowvalue >> (24 - offset));
    306                         lowvalue <<= offset;
    307                         shift = count;
    308                         lowvalue &= 0xffffff;
    309                         count -= 8 ;
    310                     }
    311 
    312                     lowvalue <<= shift;
    313                 }
    314                 while (n);
    315             }
    316 
    317 
    318             {
    319 
    320                 split = (range + 1) >> 1;
    321 
    322                 if (e & 1)
    323                 {
    324                     lowvalue += split;
    325                     range = range - split;
    326                 }
    327                 else
    328                 {
    329                     range = split;
    330                 }
    331 
    332                 range <<= 1;
    333 
    334                 if ((lowvalue & 0x80000000))
    335                 {
    336                     int x = w->pos - 1;
    337 
    338                     while (x >= 0 && w->buffer[x] == 0xff)
    339                     {
    340                         w->buffer[x] = (unsigned char)0;
    341                         x--;
    342                     }
    343 
    344                     w->buffer[x] += 1;
    345 
    346                 }
    347 
    348                 lowvalue  <<= 1;
    349 
    350                 if (!++count)
    351                 {
    352                     count = -8;
    353                     w->buffer[w->pos++] = (lowvalue >> 24);
    354                     lowvalue &= 0xffffff;
    355                 }
    356             }
    357 
    358         }
    359 
    360         ++p;
    361     }
    362 
    363     w->count = count;
    364     w->lowvalue = lowvalue;
    365     w->range = range;
    366 
    367 }
    368 
    369 static void write_partition_size(unsigned char *cx_data, int size)
    370 {
    371     signed char csize;
    372 
    373     csize = size & 0xff;
    374     *cx_data = csize;
    375     csize = (size >> 8) & 0xff;
    376     *(cx_data + 1) = csize;
    377     csize = (size >> 16) & 0xff;
    378     *(cx_data + 2) = csize;
    379 
    380 }
    381 
    382 static void pack_tokens_into_partitions_c(VP8_COMP *cpi, unsigned char *cx_data, int num_part, int *size)
    383 {
    384 
    385     int i;
    386     unsigned char *ptr = cx_data;
    387     unsigned int shift;
    388     vp8_writer *w = &cpi->bc2;
    389     *size = 3 * (num_part - 1);
    390     ptr = cx_data + (*size);
    391 
    392     for (i = 0; i < num_part; i++)
    393     {
    394         vp8_start_encode(w, ptr);
    395         {
    396             unsigned int split;
    397             int count = w->count;
    398             unsigned int range = w->range;
    399             unsigned int lowvalue = w->lowvalue;
    400             int mb_row;
    401 
    402             for (mb_row = i; mb_row < cpi->common.mb_rows; mb_row += num_part)
    403             {
    404                 TOKENEXTRA *p    = cpi->tplist[mb_row].start;
    405                 TOKENEXTRA *stop = cpi->tplist[mb_row].stop;
    406 
    407                 while (p < stop)
    408                 {
    409                     const int t = p->Token;
    410                     vp8_token *const a = vp8_coef_encodings + t;
    411                     const vp8_extra_bit_struct *const b = vp8_extra_bits + t;
    412                     int i = 0;
    413                     const unsigned char *pp = p->context_tree;
    414                     int v = a->value;
    415                     int n = a->Len;
    416 
    417                     if (p->skip_eob_node)
    418                     {
    419                         n--;
    420                         i = 2;
    421                     }
    422 
    423                     do
    424                     {
    425                         const int bb = (v >> --n) & 1;
    426                         split = 1 + (((range - 1) * pp[i>>1]) >> 8);
    427                         i = vp8_coef_tree[i+bb];
    428 
    429                         if (bb)
    430                         {
    431                             lowvalue += split;
    432                             range = range - split;
    433                         }
    434                         else
    435                         {
    436                             range = split;
    437                         }
    438 
    439                         shift = norm[range];
    440                         range <<= shift;
    441                         count += shift;
    442 
    443                         if (count >= 0)
    444                         {
    445                             int offset = shift - count;
    446 
    447                             if ((lowvalue << (offset - 1)) & 0x80000000)
    448                             {
    449                                 int x = w->pos - 1;
    450 
    451                                 while (x >= 0 && w->buffer[x] == 0xff)
    452                                 {
    453                                     w->buffer[x] = (unsigned char)0;
    454                                     x--;
    455                                 }
    456 
    457                                 w->buffer[x] += 1;
    458                             }
    459 
    460                             w->buffer[w->pos++] = (lowvalue >> (24 - offset));
    461                             lowvalue <<= offset;
    462                             shift = count;
    463                             lowvalue &= 0xffffff;
    464                             count -= 8 ;
    465                         }
    466 
    467                         lowvalue <<= shift;
    468                     }
    469                     while (n);
    470 
    471 
    472                     if (b->base_val)
    473                     {
    474                         const int e = p->Extra, L = b->Len;
    475 
    476                         if (L)
    477                         {
    478                             const unsigned char *pp = b->prob;
    479                             int v = e >> 1;
    480                             int n = L;              /* number of bits in v, assumed nonzero */
    481                             int i = 0;
    482 
    483                             do
    484                             {
    485                                 const int bb = (v >> --n) & 1;
    486                                 split = 1 + (((range - 1) * pp[i>>1]) >> 8);
    487                                 i = b->tree[i+bb];
    488 
    489                                 if (bb)
    490                                 {
    491                                     lowvalue += split;
    492                                     range = range - split;
    493                                 }
    494                                 else
    495                                 {
    496                                     range = split;
    497                                 }
    498 
    499                                 shift = norm[range];
    500                                 range <<= shift;
    501                                 count += shift;
    502 
    503                                 if (count >= 0)
    504                                 {
    505                                     int offset = shift - count;
    506 
    507                                     if ((lowvalue << (offset - 1)) & 0x80000000)
    508                                     {
    509                                         int x = w->pos - 1;
    510 
    511                                         while (x >= 0 && w->buffer[x] == 0xff)
    512                                         {
    513                                             w->buffer[x] = (unsigned char)0;
    514                                             x--;
    515                                         }
    516 
    517                                         w->buffer[x] += 1;
    518                                     }
    519 
    520                                     w->buffer[w->pos++] = (lowvalue >> (24 - offset));
    521                                     lowvalue <<= offset;
    522                                     shift = count;
    523                                     lowvalue &= 0xffffff;
    524                                     count -= 8 ;
    525                                 }
    526 
    527                                 lowvalue <<= shift;
    528                             }
    529                             while (n);
    530                         }
    531 
    532                         {
    533                             split = (range + 1) >> 1;
    534 
    535                             if (e & 1)
    536                             {
    537                                 lowvalue += split;
    538                                 range = range - split;
    539                             }
    540                             else
    541                             {
    542                                 range = split;
    543                             }
    544 
    545                             range <<= 1;
    546 
    547                             if ((lowvalue & 0x80000000))
    548                             {
    549                                 int x = w->pos - 1;
    550 
    551                                 while (x >= 0 && w->buffer[x] == 0xff)
    552                                 {
    553                                     w->buffer[x] = (unsigned char)0;
    554                                     x--;
    555                                 }
    556 
    557                                 w->buffer[x] += 1;
    558 
    559                             }
    560 
    561                             lowvalue  <<= 1;
    562 
    563                             if (!++count)
    564                             {
    565                                 count = -8;
    566                                 w->buffer[w->pos++] = (lowvalue >> 24);
    567                                 lowvalue &= 0xffffff;
    568                             }
    569                         }
    570 
    571                     }
    572 
    573                     ++p;
    574                 }
    575             }
    576 
    577             w->count    = count;
    578             w->lowvalue = lowvalue;
    579             w->range    = range;
    580 
    581         }
    582 
    583         vp8_stop_encode(w);
    584         *size +=   w->pos;
    585 
    586         if (i < (num_part - 1))
    587         {
    588             write_partition_size(cx_data, w->pos);
    589             cx_data += 3;
    590             ptr += w->pos;
    591         }
    592     }
    593 }
    594 
    595 
    596 static void pack_mb_row_tokens_c(VP8_COMP *cpi, vp8_writer *w)
    597 {
    598 
    599     unsigned int split;
    600     int count = w->count;
    601     unsigned int range = w->range;
    602     unsigned int lowvalue = w->lowvalue;
    603     unsigned int shift;
    604     int mb_row;
    605 
    606     for (mb_row = 0; mb_row < cpi->common.mb_rows; mb_row++)
    607     {
    608         TOKENEXTRA *p    = cpi->tplist[mb_row].start;
    609         TOKENEXTRA *stop = cpi->tplist[mb_row].stop;
    610 
    611         while (p < stop)
    612         {
    613             const int t = p->Token;
    614             vp8_token *const a = vp8_coef_encodings + t;
    615             const vp8_extra_bit_struct *const b = vp8_extra_bits + t;
    616             int i = 0;
    617             const unsigned char *pp = p->context_tree;
    618             int v = a->value;
    619             int n = a->Len;
    620 
    621             if (p->skip_eob_node)
    622             {
    623                 n--;
    624                 i = 2;
    625             }
    626 
    627             do
    628             {
    629                 const int bb = (v >> --n) & 1;
    630                 split = 1 + (((range - 1) * pp[i>>1]) >> 8);
    631                 i = vp8_coef_tree[i+bb];
    632 
    633                 if (bb)
    634                 {
    635                     lowvalue += split;
    636                     range = range - split;
    637                 }
    638                 else
    639                 {
    640                     range = split;
    641                 }
    642 
    643                 shift = norm[range];
    644                 range <<= shift;
    645                 count += shift;
    646 
    647                 if (count >= 0)
    648                 {
    649                     int offset = shift - count;
    650 
    651                     if ((lowvalue << (offset - 1)) & 0x80000000)
    652                     {
    653                         int x = w->pos - 1;
    654 
    655                         while (x >= 0 && w->buffer[x] == 0xff)
    656                         {
    657                             w->buffer[x] = (unsigned char)0;
    658                             x--;
    659                         }
    660 
    661                         w->buffer[x] += 1;
    662                     }
    663 
    664                     w->buffer[w->pos++] = (lowvalue >> (24 - offset));
    665                     lowvalue <<= offset;
    666                     shift = count;
    667                     lowvalue &= 0xffffff;
    668                     count -= 8 ;
    669                 }
    670 
    671                 lowvalue <<= shift;
    672             }
    673             while (n);
    674 
    675 
    676             if (b->base_val)
    677             {
    678                 const int e = p->Extra, L = b->Len;
    679 
    680                 if (L)
    681                 {
    682                     const unsigned char *pp = b->prob;
    683                     int v = e >> 1;
    684                     int n = L;              /* number of bits in v, assumed nonzero */
    685                     int i = 0;
    686 
    687                     do
    688                     {
    689                         const int bb = (v >> --n) & 1;
    690                         split = 1 + (((range - 1) * pp[i>>1]) >> 8);
    691                         i = b->tree[i+bb];
    692 
    693                         if (bb)
    694                         {
    695                             lowvalue += split;
    696                             range = range - split;
    697                         }
    698                         else
    699                         {
    700                             range = split;
    701                         }
    702 
    703                         shift = norm[range];
    704                         range <<= shift;
    705                         count += shift;
    706 
    707                         if (count >= 0)
    708                         {
    709                             int offset = shift - count;
    710 
    711                             if ((lowvalue << (offset - 1)) & 0x80000000)
    712                             {
    713                                 int x = w->pos - 1;
    714 
    715                                 while (x >= 0 && w->buffer[x] == 0xff)
    716                                 {
    717                                     w->buffer[x] = (unsigned char)0;
    718                                     x--;
    719                                 }
    720 
    721                                 w->buffer[x] += 1;
    722                             }
    723 
    724                             w->buffer[w->pos++] = (lowvalue >> (24 - offset));
    725                             lowvalue <<= offset;
    726                             shift = count;
    727                             lowvalue &= 0xffffff;
    728                             count -= 8 ;
    729                         }
    730 
    731                         lowvalue <<= shift;
    732                     }
    733                     while (n);
    734                 }
    735 
    736                 {
    737                     split = (range + 1) >> 1;
    738 
    739                     if (e & 1)
    740                     {
    741                         lowvalue += split;
    742                         range = range - split;
    743                     }
    744                     else
    745                     {
    746                         range = split;
    747                     }
    748 
    749                     range <<= 1;
    750 
    751                     if ((lowvalue & 0x80000000))
    752                     {
    753                         int x = w->pos - 1;
    754 
    755                         while (x >= 0 && w->buffer[x] == 0xff)
    756                         {
    757                             w->buffer[x] = (unsigned char)0;
    758                             x--;
    759                         }
    760 
    761                         w->buffer[x] += 1;
    762 
    763                     }
    764 
    765                     lowvalue  <<= 1;
    766 
    767                     if (!++count)
    768                     {
    769                         count = -8;
    770                         w->buffer[w->pos++] = (lowvalue >> 24);
    771                         lowvalue &= 0xffffff;
    772                     }
    773                 }
    774 
    775             }
    776 
    777             ++p;
    778         }
    779     }
    780 
    781     w->count = count;
    782     w->lowvalue = lowvalue;
    783     w->range = range;
    784 
    785 }
    786 
    787 static void write_mv_ref
    788 (
    789     vp8_writer *w, MB_PREDICTION_MODE m, const vp8_prob *p
    790 )
    791 {
    792 
    793     assert(NEARESTMV <= m  &&  m <= SPLITMV);
    794 
    795     vp8_write_token(w, vp8_mv_ref_tree, p,
    796                     vp8_mv_ref_encoding_array - NEARESTMV + m);
    797 }
    798 
    799 static void write_sub_mv_ref
    800 (
    801     vp8_writer *w, B_PREDICTION_MODE m, const vp8_prob *p
    802 )
    803 {
    804     assert(LEFT4X4 <= m  &&  m <= NEW4X4);
    805 
    806     vp8_write_token(w, vp8_sub_mv_ref_tree, p,
    807                     vp8_sub_mv_ref_encoding_array - LEFT4X4 + m);
    808 }
    809 
    810 static void write_mv
    811 (
    812     vp8_writer *w, const MV *mv, const MV *ref, const MV_CONTEXT *mvc
    813 )
    814 {
    815     MV e;
    816     e.row = mv->row - ref->row;
    817     e.col = mv->col - ref->col;
    818 
    819     vp8_encode_motion_vector(w, &e, mvc);
    820 }
    821 
    822 static void write_mb_features(vp8_writer *w, const MB_MODE_INFO *mi, const MACROBLOCKD *x)
    823 {
    824     // Encode the MB segment id.
    825     if (x->segmentation_enabled && x->update_mb_segmentation_map)
    826     {
    827         switch (mi->segment_id)
    828         {
    829         case 0:
    830             vp8_write(w, 0, x->mb_segment_tree_probs[0]);
    831             vp8_write(w, 0, x->mb_segment_tree_probs[1]);
    832             break;
    833         case 1:
    834             vp8_write(w, 0, x->mb_segment_tree_probs[0]);
    835             vp8_write(w, 1, x->mb_segment_tree_probs[1]);
    836             break;
    837         case 2:
    838             vp8_write(w, 1, x->mb_segment_tree_probs[0]);
    839             vp8_write(w, 0, x->mb_segment_tree_probs[2]);
    840             break;
    841         case 3:
    842             vp8_write(w, 1, x->mb_segment_tree_probs[0]);
    843             vp8_write(w, 1, x->mb_segment_tree_probs[2]);
    844             break;
    845 
    846             // TRAP.. This should not happen
    847         default:
    848             vp8_write(w, 0, x->mb_segment_tree_probs[0]);
    849             vp8_write(w, 0, x->mb_segment_tree_probs[1]);
    850             break;
    851         }
    852     }
    853 }
    854 
    855 
    856 static void pack_inter_mode_mvs(VP8_COMP *const cpi)
    857 {
    858     VP8_COMMON *const pc = & cpi->common;
    859     vp8_writer *const w = & cpi->bc;
    860     const MV_CONTEXT *mvc = pc->fc.mvc;
    861 
    862     const int *const rfct = cpi->count_mb_ref_frame_usage;
    863     const int rf_intra = rfct[INTRA_FRAME];
    864     const int rf_inter = rfct[LAST_FRAME] + rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME];
    865 
    866     MODE_INFO *m = pc->mi, *ms;
    867     const int mis = pc->mode_info_stride;
    868     int mb_row = -1;
    869 
    870     int prob_last_coded;
    871     int prob_gf_coded;
    872     int prob_skip_false = 0;
    873     ms = pc->mi - 1;
    874 
    875     cpi->mb.partition_info = cpi->mb.pi;
    876 
    877     // Calculate the probabilities to be used to code the reference frame based on actual useage this frame
    878     if (!(cpi->prob_intra_coded = rf_intra * 255 / (rf_intra + rf_inter)))
    879         cpi->prob_intra_coded = 1;
    880 
    881     prob_last_coded = rf_inter ? (rfct[LAST_FRAME] * 255) / rf_inter : 128;
    882 
    883     if (!prob_last_coded)
    884         prob_last_coded = 1;
    885 
    886     prob_gf_coded = (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME])
    887                     ? (rfct[GOLDEN_FRAME] * 255) / (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME]) : 128;
    888 
    889     if (!prob_gf_coded)
    890         prob_gf_coded = 1;
    891 
    892 
    893 #ifdef ENTROPY_STATS
    894     active_section = 1;
    895 #endif
    896 
    897     if (pc->mb_no_coeff_skip)
    898     {
    899         prob_skip_false = cpi->skip_false_count * 256 / (cpi->skip_false_count + cpi->skip_true_count);
    900 
    901         if (prob_skip_false <= 1)
    902             prob_skip_false = 1;
    903 
    904         if (prob_skip_false > 255)
    905             prob_skip_false = 255;
    906 
    907         cpi->prob_skip_false = prob_skip_false;
    908         vp8_write_literal(w, prob_skip_false, 8);
    909     }
    910 
    911     vp8_write_literal(w, cpi->prob_intra_coded, 8);
    912     vp8_write_literal(w, prob_last_coded, 8);
    913     vp8_write_literal(w, prob_gf_coded, 8);
    914 
    915     update_mbintra_mode_probs(cpi);
    916 
    917     vp8_write_mvprobs(cpi);
    918 
    919     while (++mb_row < pc->mb_rows)
    920     {
    921         int mb_col = -1;
    922 
    923         while (++mb_col < pc->mb_cols)
    924         {
    925             const MB_MODE_INFO *const mi = & m->mbmi;
    926             const MV_REFERENCE_FRAME rf = mi->ref_frame;
    927             const MB_PREDICTION_MODE mode = mi->mode;
    928 
    929             MACROBLOCKD *xd = &cpi->mb.e_mbd;
    930 
    931             // Distance of Mb to the various image edges.
    932             // These specified to 8th pel as they are always compared to MV values that are in 1/8th pel units
    933             xd->mb_to_left_edge = -((mb_col * 16) << 3);
    934             xd->mb_to_right_edge = ((pc->mb_cols - 1 - mb_col) * 16) << 3;
    935             xd->mb_to_top_edge = -((mb_row * 16)) << 3;
    936             xd->mb_to_bottom_edge = ((pc->mb_rows - 1 - mb_row) * 16) << 3;
    937 
    938 #ifdef ENTROPY_STATS
    939             active_section = 9;
    940 #endif
    941 
    942             if (cpi->mb.e_mbd.update_mb_segmentation_map)
    943                 write_mb_features(w, mi, &cpi->mb.e_mbd);
    944 
    945             if (pc->mb_no_coeff_skip)
    946                 vp8_encode_bool(w, m->mbmi.mb_skip_coeff, prob_skip_false);
    947 
    948             if (rf == INTRA_FRAME)
    949             {
    950                 vp8_write(w, 0, cpi->prob_intra_coded);
    951 #ifdef ENTROPY_STATS
    952                 active_section = 6;
    953 #endif
    954                 write_ymode(w, mode, pc->fc.ymode_prob);
    955 
    956                 if (mode == B_PRED)
    957                 {
    958                     int j = 0;
    959 
    960                     do
    961                         write_bmode(w, m->bmi[j].mode, pc->fc.bmode_prob);
    962 
    963                     while (++j < 16);
    964                 }
    965 
    966                 write_uv_mode(w, mi->uv_mode, pc->fc.uv_mode_prob);
    967             }
    968             else    /* inter coded */
    969             {
    970                 MV best_mv;
    971                 vp8_prob mv_ref_p [VP8_MVREFS-1];
    972 
    973                 vp8_write(w, 1, cpi->prob_intra_coded);
    974 
    975                 if (rf == LAST_FRAME)
    976                     vp8_write(w, 0, prob_last_coded);
    977                 else
    978                 {
    979                     vp8_write(w, 1, prob_last_coded);
    980                     vp8_write(w, (rf == GOLDEN_FRAME) ? 0 : 1, prob_gf_coded);
    981                 }
    982 
    983                 {
    984                     MV n1, n2;
    985                     int ct[4];
    986 
    987                     vp8_find_near_mvs(xd, m, &n1, &n2, &best_mv, ct, rf, cpi->common.ref_frame_sign_bias);
    988                     vp8_mv_ref_probs(mv_ref_p, ct);
    989 
    990 #ifdef ENTROPY_STATS
    991                     accum_mv_refs(mode, ct);
    992 #endif
    993 
    994                 }
    995 
    996 #ifdef ENTROPY_STATS
    997                 active_section = 3;
    998 #endif
    999 
   1000                 write_mv_ref(w, mode, mv_ref_p);
   1001 
   1002                 switch (mode)   /* new, split require MVs */
   1003                 {
   1004                 case NEWMV:
   1005 
   1006 #ifdef ENTROPY_STATS
   1007                     active_section = 5;
   1008 #endif
   1009 
   1010                     write_mv(w, &mi->mv.as_mv, &best_mv, mvc);
   1011                     break;
   1012 
   1013                 case SPLITMV:
   1014                 {
   1015                     int j = 0;
   1016 
   1017 #ifdef MODE_STATS
   1018                     ++count_mb_seg [mi->partitioning];
   1019 #endif
   1020 
   1021                     write_split(w, mi->partitioning);
   1022 
   1023                     do
   1024                     {
   1025                         const B_MODE_INFO *const b = cpi->mb.partition_info->bmi + j;
   1026                         const int *const  L = vp8_mbsplits [mi->partitioning];
   1027                         int k = -1;  /* first block in subset j */
   1028                         int mv_contz;
   1029 
   1030                         while (j != L[++k])
   1031                             if (k >= 16)
   1032                                 assert(0);
   1033 
   1034                         mv_contz = vp8_mv_cont
   1035                                    (&(vp8_left_bmi(m, k)->mv.as_mv),
   1036                                     &(vp8_above_bmi(m, k, mis)->mv.as_mv));
   1037                         write_sub_mv_ref(w, b->mode, vp8_sub_mv_ref_prob2 [mv_contz]); //pc->fc.sub_mv_ref_prob);
   1038 
   1039                         if (b->mode == NEW4X4)
   1040                         {
   1041 #ifdef ENTROPY_STATS
   1042                             active_section = 11;
   1043 #endif
   1044                             write_mv(w, &b->mv.as_mv, &best_mv, (const MV_CONTEXT *) mvc);
   1045                         }
   1046                     }
   1047                     while (++j < cpi->mb.partition_info->count);
   1048                 }
   1049                 break;
   1050                 default:
   1051                     break;
   1052                 }
   1053             }
   1054 
   1055             ++m;
   1056             cpi->mb.partition_info++;
   1057         }
   1058 
   1059         ++m;  /* skip L prediction border */
   1060         cpi->mb.partition_info++;
   1061     }
   1062 }
   1063 
   1064 
   1065 static void write_kfmodes(VP8_COMP *cpi)
   1066 {
   1067     vp8_writer *const bc = & cpi->bc;
   1068     const VP8_COMMON *const c = & cpi->common;
   1069     /* const */
   1070     MODE_INFO *m = c->mi;
   1071 
   1072     int mb_row = -1;
   1073     int prob_skip_false = 0;
   1074 
   1075     if (c->mb_no_coeff_skip)
   1076     {
   1077         prob_skip_false = cpi->skip_false_count * 256 / (cpi->skip_false_count + cpi->skip_true_count);
   1078 
   1079         if (prob_skip_false <= 1)
   1080             prob_skip_false = 1;
   1081 
   1082         if (prob_skip_false >= 255)
   1083             prob_skip_false = 255;
   1084 
   1085         cpi->prob_skip_false = prob_skip_false;
   1086         vp8_write_literal(bc, prob_skip_false, 8);
   1087     }
   1088 
   1089     while (++mb_row < c->mb_rows)
   1090     {
   1091         int mb_col = -1;
   1092 
   1093         while (++mb_col < c->mb_cols)
   1094         {
   1095             const int ym = m->mbmi.mode;
   1096 
   1097             if (cpi->mb.e_mbd.update_mb_segmentation_map)
   1098                 write_mb_features(bc, &m->mbmi, &cpi->mb.e_mbd);
   1099 
   1100             if (c->mb_no_coeff_skip)
   1101                 vp8_encode_bool(bc, m->mbmi.mb_skip_coeff, prob_skip_false);
   1102 
   1103             kfwrite_ymode(bc, ym, c->kf_ymode_prob);
   1104 
   1105             if (ym == B_PRED)
   1106             {
   1107                 const int mis = c->mode_info_stride;
   1108                 int i = 0;
   1109 
   1110                 do
   1111                 {
   1112                     const B_PREDICTION_MODE A = vp8_above_bmi(m, i, mis)->mode;
   1113                     const B_PREDICTION_MODE L = vp8_left_bmi(m, i)->mode;
   1114                     const int bm = m->bmi[i].mode;
   1115 
   1116 #ifdef ENTROPY_STATS
   1117                     ++intra_mode_stats [A] [L] [bm];
   1118 #endif
   1119 
   1120                     write_bmode(bc, bm, c->kf_bmode_prob [A] [L]);
   1121                 }
   1122                 while (++i < 16);
   1123             }
   1124 
   1125             write_uv_mode(bc, (m++)->mbmi.uv_mode, c->kf_uv_mode_prob);
   1126         }
   1127 
   1128         m++;    // skip L prediction border
   1129     }
   1130 }
   1131 int vp8_estimate_entropy_savings(VP8_COMP *cpi)
   1132 {
   1133     int i = 0;
   1134     int savings = 0;
   1135 
   1136     const int *const rfct = cpi->count_mb_ref_frame_usage;
   1137     const int rf_intra = rfct[INTRA_FRAME];
   1138     const int rf_inter = rfct[LAST_FRAME] + rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME];
   1139     int new_intra, new_last, gf_last, oldtotal, newtotal;
   1140     int ref_frame_cost[MAX_REF_FRAMES];
   1141 
   1142     vp8_clear_system_state(); //__asm emms;
   1143 
   1144     if (cpi->common.frame_type != KEY_FRAME)
   1145     {
   1146         if (!(new_intra = rf_intra * 255 / (rf_intra + rf_inter)))
   1147             new_intra = 1;
   1148 
   1149         new_last = rf_inter ? (rfct[LAST_FRAME] * 255) / rf_inter : 128;
   1150 
   1151         gf_last = (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME])
   1152                   ? (rfct[GOLDEN_FRAME] * 255) / (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME]) : 128;
   1153 
   1154         // new costs
   1155         ref_frame_cost[INTRA_FRAME]   = vp8_cost_zero(new_intra);
   1156         ref_frame_cost[LAST_FRAME]    = vp8_cost_one(new_intra)
   1157                                         + vp8_cost_zero(new_last);
   1158         ref_frame_cost[GOLDEN_FRAME]  = vp8_cost_one(new_intra)
   1159                                         + vp8_cost_one(new_last)
   1160                                         + vp8_cost_zero(gf_last);
   1161         ref_frame_cost[ALTREF_FRAME]  = vp8_cost_one(new_intra)
   1162                                         + vp8_cost_one(new_last)
   1163                                         + vp8_cost_one(gf_last);
   1164 
   1165         newtotal =
   1166             rfct[INTRA_FRAME] * ref_frame_cost[INTRA_FRAME] +
   1167             rfct[LAST_FRAME] * ref_frame_cost[LAST_FRAME] +
   1168             rfct[GOLDEN_FRAME] * ref_frame_cost[GOLDEN_FRAME] +
   1169             rfct[ALTREF_FRAME] * ref_frame_cost[ALTREF_FRAME];
   1170 
   1171 
   1172         // old costs
   1173         ref_frame_cost[INTRA_FRAME]   = vp8_cost_zero(cpi->prob_intra_coded);
   1174         ref_frame_cost[LAST_FRAME]    = vp8_cost_one(cpi->prob_intra_coded)
   1175                                         + vp8_cost_zero(cpi->prob_last_coded);
   1176         ref_frame_cost[GOLDEN_FRAME]  = vp8_cost_one(cpi->prob_intra_coded)
   1177                                         + vp8_cost_one(cpi->prob_last_coded)
   1178                                         + vp8_cost_zero(cpi->prob_gf_coded);
   1179         ref_frame_cost[ALTREF_FRAME]  = vp8_cost_one(cpi->prob_intra_coded)
   1180                                         + vp8_cost_one(cpi->prob_last_coded)
   1181                                         + vp8_cost_one(cpi->prob_gf_coded);
   1182 
   1183         oldtotal =
   1184             rfct[INTRA_FRAME] * ref_frame_cost[INTRA_FRAME] +
   1185             rfct[LAST_FRAME] * ref_frame_cost[LAST_FRAME] +
   1186             rfct[GOLDEN_FRAME] * ref_frame_cost[GOLDEN_FRAME] +
   1187             rfct[ALTREF_FRAME] * ref_frame_cost[ALTREF_FRAME];
   1188 
   1189         savings += (oldtotal - newtotal) / 256;
   1190     }
   1191 
   1192 
   1193     do
   1194     {
   1195         int j = 0;
   1196 
   1197         do
   1198         {
   1199             int k = 0;
   1200 
   1201             do
   1202             {
   1203                 /* at every context */
   1204 
   1205                 /* calc probs and branch cts for this frame only */
   1206                 //vp8_prob new_p           [vp8_coef_tokens-1];
   1207                 //unsigned int branch_ct   [vp8_coef_tokens-1] [2];
   1208 
   1209                 int t = 0;      /* token/prob index */
   1210 
   1211                 vp8_tree_probs_from_distribution(
   1212                     vp8_coef_tokens, vp8_coef_encodings, vp8_coef_tree,
   1213                     cpi->frame_coef_probs [i][j][k], cpi->frame_branch_ct [i][j][k], cpi->coef_counts [i][j][k],
   1214                     256, 1
   1215                 );
   1216 
   1217                 do
   1218                 {
   1219                     const unsigned int *ct  = cpi->frame_branch_ct [i][j][k][t];
   1220                     const vp8_prob newp = cpi->frame_coef_probs [i][j][k][t];
   1221 
   1222                     const vp8_prob old = cpi->common.fc.coef_probs [i][j][k][t];
   1223                     const vp8_prob upd = vp8_coef_update_probs [i][j][k][t];
   1224 
   1225                     const int old_b = vp8_cost_branch(ct, old);
   1226                     const int new_b = vp8_cost_branch(ct, newp);
   1227 
   1228                     const int update_b = 8 +
   1229                                          ((vp8_cost_one(upd) - vp8_cost_zero(upd)) >> 8);
   1230 
   1231                     const int s = old_b - new_b - update_b;
   1232 
   1233                     if (s > 0)
   1234                         savings += s;
   1235 
   1236 
   1237                 }
   1238                 while (++t < vp8_coef_tokens - 1);
   1239 
   1240 
   1241             }
   1242             while (++k < PREV_COEF_CONTEXTS);
   1243         }
   1244         while (++j < COEF_BANDS);
   1245     }
   1246     while (++i < BLOCK_TYPES);
   1247 
   1248     return savings;
   1249 }
   1250 
   1251 static void update_coef_probs(VP8_COMP *cpi)
   1252 {
   1253     int i = 0;
   1254     vp8_writer *const w = & cpi->bc;
   1255     int savings = 0;
   1256 
   1257     vp8_clear_system_state(); //__asm emms;
   1258 
   1259 
   1260     do
   1261     {
   1262         int j = 0;
   1263 
   1264         do
   1265         {
   1266             int k = 0;
   1267 
   1268             do
   1269             {
   1270                 //note: use result from vp8_estimate_entropy_savings, so no need to call vp8_tree_probs_from_distribution here.
   1271                 /* at every context */
   1272 
   1273                 /* calc probs and branch cts for this frame only */
   1274                 //vp8_prob new_p           [vp8_coef_tokens-1];
   1275                 //unsigned int branch_ct   [vp8_coef_tokens-1] [2];
   1276 
   1277                 int t = 0;      /* token/prob index */
   1278 
   1279                 //vp8_tree_probs_from_distribution(
   1280                 //    vp8_coef_tokens, vp8_coef_encodings, vp8_coef_tree,
   1281                 //    new_p, branch_ct, (unsigned int *)cpi->coef_counts [i][j][k],
   1282                 //    256, 1
   1283                 //    );
   1284 
   1285                 do
   1286                 {
   1287                     const unsigned int *ct  = cpi->frame_branch_ct [i][j][k][t];
   1288                     const vp8_prob newp = cpi->frame_coef_probs [i][j][k][t];
   1289 
   1290                     vp8_prob *Pold = cpi->common.fc.coef_probs [i][j][k] + t;
   1291                     const vp8_prob old = *Pold;
   1292                     const vp8_prob upd = vp8_coef_update_probs [i][j][k][t];
   1293 
   1294                     const int old_b = vp8_cost_branch(ct, old);
   1295                     const int new_b = vp8_cost_branch(ct, newp);
   1296 
   1297                     const int update_b = 8 +
   1298                                          ((vp8_cost_one(upd) - vp8_cost_zero(upd)) >> 8);
   1299 
   1300                     const int s = old_b - new_b - update_b;
   1301                     const int u = s > 0 ? 1 : 0;
   1302 
   1303                     vp8_write(w, u, upd);
   1304 
   1305 
   1306 #ifdef ENTROPY_STATS
   1307                     ++ tree_update_hist [i][j][k][t] [u];
   1308 #endif
   1309 
   1310                     if (u)
   1311                     {
   1312                         /* send/use new probability */
   1313 
   1314                         *Pold = newp;
   1315                         vp8_write_literal(w, newp, 8);
   1316 
   1317                         savings += s;
   1318 
   1319                     }
   1320 
   1321                 }
   1322                 while (++t < vp8_coef_tokens - 1);
   1323 
   1324                 /* Accum token counts for generation of default statistics */
   1325 #ifdef ENTROPY_STATS
   1326                 t = 0;
   1327 
   1328                 do
   1329                 {
   1330                     context_counters [i][j][k][t] += cpi->coef_counts [i][j][k][t];
   1331                 }
   1332                 while (++t < vp8_coef_tokens);
   1333 
   1334 #endif
   1335 
   1336             }
   1337             while (++k < PREV_COEF_CONTEXTS);
   1338         }
   1339         while (++j < COEF_BANDS);
   1340     }
   1341     while (++i < BLOCK_TYPES);
   1342 
   1343 }
   1344 #ifdef PACKET_TESTING
   1345 FILE *vpxlogc = 0;
   1346 #endif
   1347 
   1348 static void put_delta_q(vp8_writer *bc, int delta_q)
   1349 {
   1350     if (delta_q != 0)
   1351     {
   1352         vp8_write_bit(bc, 1);
   1353         vp8_write_literal(bc, abs(delta_q), 4);
   1354 
   1355         if (delta_q < 0)
   1356             vp8_write_bit(bc, 1);
   1357         else
   1358             vp8_write_bit(bc, 0);
   1359     }
   1360     else
   1361         vp8_write_bit(bc, 0);
   1362 }
   1363 
   1364 void vp8_pack_bitstream(VP8_COMP *cpi, unsigned char *dest, unsigned long *size)
   1365 {
   1366     int i, j;
   1367     VP8_HEADER oh;
   1368     VP8_COMMON *const pc = & cpi->common;
   1369     vp8_writer *const bc = & cpi->bc;
   1370     MACROBLOCKD *const xd = & cpi->mb.e_mbd;
   1371     int extra_bytes_packed = 0;
   1372 
   1373     unsigned char *cx_data = dest;
   1374     const int *mb_feature_data_bits;
   1375 
   1376     oh.show_frame = (int) pc->show_frame;
   1377     oh.type = (int)pc->frame_type;
   1378     oh.version = pc->version;
   1379 
   1380     mb_feature_data_bits = vp8_mb_feature_data_bits;
   1381     cx_data += 3;
   1382 
   1383 #if defined(SECTIONBITS_OUTPUT)
   1384     Sectionbits[active_section = 1] += sizeof(VP8_HEADER) * 8 * 256;
   1385 #endif
   1386 
   1387     //vp8_kf_default_bmode_probs() is called in vp8_setup_key_frame() once for each
   1388     //K frame before encode frame. pc->kf_bmode_prob doesn't get changed anywhere
   1389     //else. No need to call it again here. --yw
   1390     //vp8_kf_default_bmode_probs( pc->kf_bmode_prob);
   1391 
   1392     // every keyframe send startcode, width, height, scale factor, clamp and color type
   1393     if (oh.type == KEY_FRAME)
   1394     {
   1395         // Start / synch code
   1396         cx_data[0] = 0x9D;
   1397         cx_data[1] = 0x01;
   1398         cx_data[2] = 0x2a;
   1399 
   1400         *((unsigned short *)(cx_data + 3)) = make_endian_16((pc->horiz_scale << 14) | pc->Width);
   1401         *((unsigned short *)(cx_data + 5)) = make_endian_16((pc->vert_scale << 14) | pc->Height);
   1402 
   1403         extra_bytes_packed = 7;
   1404         cx_data += extra_bytes_packed ;
   1405 
   1406         vp8_start_encode(bc, cx_data);
   1407 
   1408         // signal clr type
   1409         vp8_write_bit(bc, pc->clr_type);
   1410         vp8_write_bit(bc, pc->clamp_type);
   1411 
   1412     }
   1413     else
   1414         vp8_start_encode(bc, cx_data);
   1415 
   1416 
   1417     // Signal whether or not Segmentation is enabled
   1418     vp8_write_bit(bc, (xd->segmentation_enabled) ? 1 : 0);
   1419 
   1420     // Indicate which features are enabled
   1421     if (xd->segmentation_enabled)
   1422     {
   1423         // Signal whether or not the segmentation map is being updated.
   1424         vp8_write_bit(bc, (xd->update_mb_segmentation_map) ? 1 : 0);
   1425         vp8_write_bit(bc, (xd->update_mb_segmentation_data) ? 1 : 0);
   1426 
   1427         if (xd->update_mb_segmentation_data)
   1428         {
   1429             signed char Data;
   1430 
   1431             vp8_write_bit(bc, (xd->mb_segement_abs_delta) ? 1 : 0);
   1432 
   1433             // For each segmentation feature (Quant and loop filter level)
   1434             for (i = 0; i < MB_LVL_MAX; i++)
   1435             {
   1436                 // For each of the segments
   1437                 for (j = 0; j < MAX_MB_SEGMENTS; j++)
   1438                 {
   1439                     Data = xd->segment_feature_data[i][j];
   1440 
   1441                     // Frame level data
   1442                     if (Data)
   1443                     {
   1444                         vp8_write_bit(bc, 1);
   1445 
   1446                         if (Data < 0)
   1447                         {
   1448                             Data = - Data;
   1449                             vp8_write_literal(bc, Data, mb_feature_data_bits[i]);
   1450                             vp8_write_bit(bc, 1);
   1451                         }
   1452                         else
   1453                         {
   1454                             vp8_write_literal(bc, Data, mb_feature_data_bits[i]);
   1455                             vp8_write_bit(bc, 0);
   1456                         }
   1457                     }
   1458                     else
   1459                         vp8_write_bit(bc, 0);
   1460                 }
   1461             }
   1462         }
   1463 
   1464         if (xd->update_mb_segmentation_map)
   1465         {
   1466             // Write the probs used to decode the segment id for each macro block.
   1467             for (i = 0; i < MB_FEATURE_TREE_PROBS; i++)
   1468             {
   1469                 int Data = xd->mb_segment_tree_probs[i];
   1470 
   1471                 if (Data != 255)
   1472                 {
   1473                     vp8_write_bit(bc, 1);
   1474                     vp8_write_literal(bc, Data, 8);
   1475                 }
   1476                 else
   1477                     vp8_write_bit(bc, 0);
   1478             }
   1479         }
   1480     }
   1481 
   1482     // Code to determine whether or not to update the scan order.
   1483     vp8_write_bit(bc, pc->filter_type);
   1484     vp8_write_literal(bc, pc->filter_level, 6);
   1485     vp8_write_literal(bc, pc->sharpness_level, 3);
   1486 
   1487     // Write out loop filter deltas applied at the MB level based on mode or ref frame (if they are enabled).
   1488     vp8_write_bit(bc, (xd->mode_ref_lf_delta_enabled) ? 1 : 0);
   1489 
   1490     if (xd->mode_ref_lf_delta_enabled)
   1491     {
   1492         // Do the deltas need to be updated
   1493         int send_update = xd->mode_ref_lf_delta_update
   1494                           || cpi->oxcf.error_resilient_mode;
   1495 
   1496         vp8_write_bit(bc, send_update);
   1497         if (send_update)
   1498         {
   1499             int Data;
   1500 
   1501             // Send update
   1502             for (i = 0; i < MAX_REF_LF_DELTAS; i++)
   1503             {
   1504                 Data = xd->ref_lf_deltas[i];
   1505 
   1506                 // Frame level data
   1507                 if (xd->ref_lf_deltas[i] != xd->last_ref_lf_deltas[i]
   1508                     || cpi->oxcf.error_resilient_mode)
   1509                 {
   1510                     xd->last_ref_lf_deltas[i] = xd->ref_lf_deltas[i];
   1511                     vp8_write_bit(bc, 1);
   1512 
   1513                     if (Data > 0)
   1514                     {
   1515                         vp8_write_literal(bc, (Data & 0x3F), 6);
   1516                         vp8_write_bit(bc, 0);    // sign
   1517                     }
   1518                     else
   1519                     {
   1520                         Data = -Data;
   1521                         vp8_write_literal(bc, (Data & 0x3F), 6);
   1522                         vp8_write_bit(bc, 1);    // sign
   1523                     }
   1524                 }
   1525                 else
   1526                     vp8_write_bit(bc, 0);
   1527             }
   1528 
   1529             // Send update
   1530             for (i = 0; i < MAX_MODE_LF_DELTAS; i++)
   1531             {
   1532                 Data = xd->mode_lf_deltas[i];
   1533 
   1534                 if (xd->mode_lf_deltas[i] != xd->last_mode_lf_deltas[i]
   1535                     || cpi->oxcf.error_resilient_mode)
   1536                 {
   1537                     xd->last_mode_lf_deltas[i] = xd->mode_lf_deltas[i];
   1538                     vp8_write_bit(bc, 1);
   1539 
   1540                     if (Data > 0)
   1541                     {
   1542                         vp8_write_literal(bc, (Data & 0x3F), 6);
   1543                         vp8_write_bit(bc, 0);    // sign
   1544                     }
   1545                     else
   1546                     {
   1547                         Data = -Data;
   1548                         vp8_write_literal(bc, (Data & 0x3F), 6);
   1549                         vp8_write_bit(bc, 1);    // sign
   1550                     }
   1551                 }
   1552                 else
   1553                     vp8_write_bit(bc, 0);
   1554             }
   1555         }
   1556     }
   1557 
   1558     //signal here is multi token partition is enabled
   1559     vp8_write_literal(bc, pc->multi_token_partition, 2);
   1560 
   1561     // Frame Qbaseline quantizer index
   1562     vp8_write_literal(bc, pc->base_qindex, 7);
   1563 
   1564     // Transmit Dc, Second order and Uv quantizer delta information
   1565     put_delta_q(bc, pc->y1dc_delta_q);
   1566     put_delta_q(bc, pc->y2dc_delta_q);
   1567     put_delta_q(bc, pc->y2ac_delta_q);
   1568     put_delta_q(bc, pc->uvdc_delta_q);
   1569     put_delta_q(bc, pc->uvac_delta_q);
   1570 
   1571     // When there is a key frame all reference buffers are updated using the new key frame
   1572     if (pc->frame_type != KEY_FRAME)
   1573     {
   1574         // Should the GF or ARF be updated using the transmitted frame or buffer
   1575         vp8_write_bit(bc, pc->refresh_golden_frame);
   1576         vp8_write_bit(bc, pc->refresh_alt_ref_frame);
   1577 
   1578         // If not being updated from current frame should either GF or ARF be updated from another buffer
   1579         if (!pc->refresh_golden_frame)
   1580             vp8_write_literal(bc, pc->copy_buffer_to_gf, 2);
   1581 
   1582         if (!pc->refresh_alt_ref_frame)
   1583             vp8_write_literal(bc, pc->copy_buffer_to_arf, 2);
   1584 
   1585         // Indicate reference frame sign bias for Golden and ARF frames (always 0 for last frame buffer)
   1586         vp8_write_bit(bc, pc->ref_frame_sign_bias[GOLDEN_FRAME]);
   1587         vp8_write_bit(bc, pc->ref_frame_sign_bias[ALTREF_FRAME]);
   1588     }
   1589 
   1590     vp8_write_bit(bc, pc->refresh_entropy_probs);
   1591 
   1592     if (pc->frame_type != KEY_FRAME)
   1593         vp8_write_bit(bc, pc->refresh_last_frame);
   1594 
   1595 #ifdef ENTROPY_STATS
   1596 
   1597     if (pc->frame_type == INTER_FRAME)
   1598         active_section = 0;
   1599     else
   1600         active_section = 7;
   1601 
   1602 #endif
   1603 
   1604     vp8_clear_system_state();  //__asm emms;
   1605 
   1606     //************************************************
   1607     // save a copy for later refresh
   1608     {
   1609         vpx_memcpy(&cpi->common.lfc, &cpi->common.fc, sizeof(cpi->common.fc));
   1610     }
   1611 
   1612     update_coef_probs(cpi);
   1613 
   1614 #ifdef ENTROPY_STATS
   1615     active_section = 2;
   1616 #endif
   1617 
   1618     // Write out the mb_no_coeff_skip flag
   1619     vp8_write_bit(bc, pc->mb_no_coeff_skip);
   1620 
   1621     if (pc->frame_type == KEY_FRAME)
   1622     {
   1623         write_kfmodes(cpi);
   1624 
   1625 #ifdef ENTROPY_STATS
   1626         active_section = 8;
   1627 #endif
   1628     }
   1629     else
   1630     {
   1631         pack_inter_mode_mvs(cpi);
   1632 
   1633 #ifdef ENTROPY_STATS
   1634         active_section = 1;
   1635 #endif
   1636     }
   1637 
   1638     vp8_stop_encode(bc);
   1639 
   1640 
   1641     if (pc->multi_token_partition != ONE_PARTITION)
   1642     {
   1643         int num_part;
   1644         int asize;
   1645         num_part = 1 << pc->multi_token_partition;
   1646 
   1647         pack_tokens_into_partitions(cpi, cx_data + bc->pos, num_part, &asize);
   1648 
   1649         oh.first_partition_length_in_bytes = cpi->bc.pos;
   1650 
   1651         *size = cpi->bc.pos + VP8_HEADER_SIZE + asize + extra_bytes_packed;
   1652     }
   1653     else
   1654     {
   1655         vp8_start_encode(&cpi->bc2, cx_data + bc->pos);
   1656 
   1657         if (!cpi->b_multi_threaded)
   1658             pack_tokens(&cpi->bc2, cpi->tok, cpi->tok_count);
   1659         else
   1660             pack_mb_row_tokens(cpi, &cpi->bc2);
   1661 
   1662         vp8_stop_encode(&cpi->bc2);
   1663         oh.first_partition_length_in_bytes = cpi->bc.pos ;
   1664         *size = cpi->bc2.pos + cpi->bc.pos + VP8_HEADER_SIZE + extra_bytes_packed;
   1665     }
   1666 
   1667 #if CONFIG_BIG_ENDIAN
   1668     {
   1669         int v = (oh.first_partition_length_in_bytes << 5) |
   1670                 (oh.show_frame << 4) |
   1671                 (oh.version << 1) |
   1672                 oh.type;
   1673 
   1674         v = make_endian_32(v);
   1675         vpx_memcpy(dest, &v, 3);
   1676     }
   1677 #else
   1678     vpx_memcpy(dest, &oh, 3);
   1679 #endif
   1680 }
   1681 
   1682 #ifdef ENTROPY_STATS
   1683 void print_tree_update_probs()
   1684 {
   1685     int i, j, k, l;
   1686     FILE *f = fopen("context.c", "a");
   1687     int Sum;
   1688     fprintf(f, "\n/* Update probabilities for token entropy tree. */\n\n");
   1689     fprintf(f, "const vp8_prob tree_update_probs[BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [vp8_coef_tokens-1] = {\n");
   1690 
   1691     for (i = 0; i < BLOCK_TYPES; i++)
   1692     {
   1693         fprintf(f, "  { \n");
   1694 
   1695         for (j = 0; j < COEF_BANDS; j++)
   1696         {
   1697             fprintf(f, "    {\n");
   1698 
   1699             for (k = 0; k < PREV_COEF_CONTEXTS; k++)
   1700             {
   1701                 fprintf(f, "      {");
   1702 
   1703                 for (l = 0; l < MAX_ENTROPY_TOKENS - 1; l++)
   1704                 {
   1705                     Sum = tree_update_hist[i][j][k][l][0] + tree_update_hist[i][j][k][l][1];
   1706 
   1707                     if (Sum > 0)
   1708                     {
   1709                         if (((tree_update_hist[i][j][k][l][0] * 255) / Sum) > 0)
   1710                             fprintf(f, "%3ld, ", (tree_update_hist[i][j][k][l][0] * 255) / Sum);
   1711                         else
   1712                             fprintf(f, "%3ld, ", 1);
   1713                     }
   1714                     else
   1715                         fprintf(f, "%3ld, ", 128);
   1716                 }
   1717 
   1718                 fprintf(f, "},\n");
   1719             }
   1720 
   1721             fprintf(f, "    },\n");
   1722         }
   1723 
   1724         fprintf(f, "  },\n");
   1725     }
   1726 
   1727     fprintf(f, "};\n");
   1728     fclose(f);
   1729 }
   1730 #endif
   1731