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