Home | History | Annotate | Download | only in decoder
      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 "treereader.h"
     13 #include "entropymv.h"
     14 #include "entropymode.h"
     15 #include "onyxd_int.h"
     16 #include "findnearmv.h"
     17 
     18 #if CONFIG_DEBUG
     19 #include <assert.h>
     20 #endif
     21 static int vp8_read_bmode(vp8_reader *bc, const vp8_prob *p)
     22 {
     23     const int i = vp8_treed_read(bc, vp8_bmode_tree, p);
     24 
     25     return i;
     26 }
     27 
     28 
     29 static int vp8_read_ymode(vp8_reader *bc, const vp8_prob *p)
     30 {
     31     const int i = vp8_treed_read(bc, vp8_ymode_tree, p);
     32 
     33     return i;
     34 }
     35 
     36 static int vp8_kfread_ymode(vp8_reader *bc, const vp8_prob *p)
     37 {
     38     const int i = vp8_treed_read(bc, vp8_kf_ymode_tree, p);
     39 
     40     return i;
     41 }
     42 
     43 
     44 
     45 static int vp8_read_uv_mode(vp8_reader *bc, const vp8_prob *p)
     46 {
     47     const int i = vp8_treed_read(bc, vp8_uv_mode_tree, p);
     48 
     49     return i;
     50 }
     51 
     52 static void vp8_read_mb_features(vp8_reader *r, MB_MODE_INFO *mi, MACROBLOCKD *x)
     53 {
     54     // Is segmentation enabled
     55     if (x->segmentation_enabled && x->update_mb_segmentation_map)
     56     {
     57         // If so then read the segment id.
     58         if (vp8_read(r, x->mb_segment_tree_probs[0]))
     59             mi->segment_id = (unsigned char)(2 + vp8_read(r, x->mb_segment_tree_probs[2]));
     60         else
     61             mi->segment_id = (unsigned char)(vp8_read(r, x->mb_segment_tree_probs[1]));
     62     }
     63 }
     64 
     65 static void vp8_kfread_modes(VP8D_COMP *pbi, MODE_INFO *m, int mb_row, int mb_col)
     66 {
     67     vp8_reader *const bc = & pbi->bc;
     68     const int mis = pbi->common.mode_info_stride;
     69 
     70         {
     71             MB_PREDICTION_MODE y_mode;
     72 
     73             // Read the Macroblock segmentation map if it is being updated explicitly this frame (reset to 0 above by default)
     74             // By default on a key frame reset all MBs to segment 0
     75             m->mbmi.segment_id = 0;
     76 
     77             if (pbi->mb.update_mb_segmentation_map)
     78                 vp8_read_mb_features(bc, &m->mbmi, &pbi->mb);
     79 
     80             // Read the macroblock coeff skip flag if this feature is in use, else default to 0
     81             if (pbi->common.mb_no_coeff_skip)
     82                 m->mbmi.mb_skip_coeff = vp8_read(bc, pbi->prob_skip_false);
     83             else
     84                 m->mbmi.mb_skip_coeff = 0;
     85 
     86             y_mode = (MB_PREDICTION_MODE) vp8_kfread_ymode(bc, pbi->common.kf_ymode_prob);
     87 
     88             m->mbmi.ref_frame = INTRA_FRAME;
     89 
     90             if ((m->mbmi.mode = y_mode) == B_PRED)
     91             {
     92                 int i = 0;
     93 
     94                 do
     95                 {
     96                     const B_PREDICTION_MODE A = vp8_above_bmi(m, i, mis)->mode;
     97                     const B_PREDICTION_MODE L = vp8_left_bmi(m, i)->mode;
     98 
     99                     m->bmi[i].mode = (B_PREDICTION_MODE) vp8_read_bmode(bc, pbi->common.kf_bmode_prob [A] [L]);
    100                 }
    101                 while (++i < 16);
    102             }
    103             else
    104             {
    105                 int BMode;
    106                 int i = 0;
    107 
    108                 switch (y_mode)
    109                 {
    110                 case DC_PRED:
    111                     BMode = B_DC_PRED;
    112                     break;
    113                 case V_PRED:
    114                     BMode = B_VE_PRED;
    115                     break;
    116                 case H_PRED:
    117                     BMode = B_HE_PRED;
    118                     break;
    119                 case TM_PRED:
    120                     BMode = B_TM_PRED;
    121                     break;
    122                 default:
    123                     BMode = B_DC_PRED;
    124                     break;
    125                 }
    126 
    127                 do
    128                 {
    129                     m->bmi[i].mode = (B_PREDICTION_MODE)BMode;
    130                 }
    131                 while (++i < 16);
    132             }
    133 
    134             m->mbmi.uv_mode = (MB_PREDICTION_MODE)vp8_read_uv_mode(bc, pbi->common.kf_uv_mode_prob);
    135         }
    136 }
    137 
    138 static int read_mvcomponent(vp8_reader *r, const MV_CONTEXT *mvc)
    139 {
    140     const vp8_prob *const p = (const vp8_prob *) mvc;
    141     int x = 0;
    142 
    143     if (vp8_read(r, p [mvpis_short]))  /* Large */
    144     {
    145         int i = 0;
    146 
    147         do
    148         {
    149             x += vp8_read(r, p [MVPbits + i]) << i;
    150         }
    151         while (++i < 3);
    152 
    153         i = mvlong_width - 1;  /* Skip bit 3, which is sometimes implicit */
    154 
    155         do
    156         {
    157             x += vp8_read(r, p [MVPbits + i]) << i;
    158         }
    159         while (--i > 3);
    160 
    161         if (!(x & 0xFFF0)  ||  vp8_read(r, p [MVPbits + 3]))
    162             x += 8;
    163     }
    164     else   /* small */
    165         x = vp8_treed_read(r, vp8_small_mvtree, p + MVPshort);
    166 
    167     if (x  &&  vp8_read(r, p [MVPsign]))
    168         x = -x;
    169 
    170     return x;
    171 }
    172 
    173 static void read_mv(vp8_reader *r, MV *mv, const MV_CONTEXT *mvc)
    174 {
    175     mv->row = (short)(read_mvcomponent(r,   mvc) << 1);
    176     mv->col = (short)(read_mvcomponent(r, ++mvc) << 1);
    177 }
    178 
    179 
    180 static void read_mvcontexts(vp8_reader *bc, MV_CONTEXT *mvc)
    181 {
    182     int i = 0;
    183 
    184     do
    185     {
    186         const vp8_prob *up = vp8_mv_update_probs[i].prob;
    187         vp8_prob *p = (vp8_prob *)(mvc + i);
    188         vp8_prob *const pstop = p + MVPcount;
    189 
    190         do
    191         {
    192             if (vp8_read(bc, *up++))
    193             {
    194                 const vp8_prob x = (vp8_prob)vp8_read_literal(bc, 7);
    195 
    196                 *p = x ? x << 1 : 1;
    197             }
    198         }
    199         while (++p < pstop);
    200     }
    201     while (++i < 2);
    202 }
    203 
    204 
    205 static MB_PREDICTION_MODE read_mv_ref(vp8_reader *bc, const vp8_prob *p)
    206 {
    207     const int i = vp8_treed_read(bc, vp8_mv_ref_tree, p);
    208 
    209     return (MB_PREDICTION_MODE)i;
    210 }
    211 
    212 static MB_PREDICTION_MODE sub_mv_ref(vp8_reader *bc, const vp8_prob *p)
    213 {
    214     const int i = vp8_treed_read(bc, vp8_sub_mv_ref_tree, p);
    215 
    216     return (MB_PREDICTION_MODE)i;
    217 }
    218 
    219 #ifdef VPX_MODE_COUNT
    220 unsigned int vp8_mv_cont_count[5][4] =
    221 {
    222     { 0, 0, 0, 0 },
    223     { 0, 0, 0, 0 },
    224     { 0, 0, 0, 0 },
    225     { 0, 0, 0, 0 },
    226     { 0, 0, 0, 0 }
    227 };
    228 #endif
    229 
    230 unsigned char vp8_mbsplit_offset[4][16] = {
    231     { 0,  8,  0,  0,  0,  0,  0,  0,  0,  0,   0,  0,  0,  0,  0,  0},
    232     { 0,  2,  0,  0,  0,  0,  0,  0,  0,  0,   0,  0,  0,  0,  0,  0},
    233     { 0,  2,  8, 10,  0,  0,  0,  0,  0,  0,   0,  0,  0,  0,  0,  0},
    234     { 0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 11, 12, 13, 14, 15}
    235 };
    236 
    237 unsigned char vp8_mbsplit_fill_count[4] = {8, 8, 4, 1};
    238 unsigned char vp8_mbsplit_fill_offset[4][16] = {
    239     { 0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 11, 12, 13, 14, 15},
    240     { 0,  1,  4,  5,  8,  9, 12, 13,  2,  3,   6,  7, 10, 11, 14, 15},
    241     { 0,  1,  4,  5,  2,  3,  6,  7,  8,  9,  12, 13, 10, 11, 14, 15},
    242     { 0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 11, 12, 13, 14, 15}
    243 };
    244 
    245 
    246 
    247 
    248 void vp8_mb_mode_mv_init(VP8D_COMP *pbi)
    249 {
    250     vp8_reader *const bc = & pbi->bc;
    251     MV_CONTEXT *const mvc = pbi->common.fc.mvc;
    252 
    253     pbi->prob_skip_false = 0;
    254     if (pbi->common.mb_no_coeff_skip)
    255         pbi->prob_skip_false = (vp8_prob)vp8_read_literal(bc, 8);
    256 
    257     if(pbi->common.frame_type != KEY_FRAME)
    258     {
    259         pbi->prob_intra = (vp8_prob)vp8_read_literal(bc, 8);
    260         pbi->prob_last  = (vp8_prob)vp8_read_literal(bc, 8);
    261         pbi->prob_gf    = (vp8_prob)vp8_read_literal(bc, 8);
    262 
    263         if (vp8_read_bit(bc))
    264         {
    265             int i = 0;
    266 
    267             do
    268             {
    269                 pbi->common.fc.ymode_prob[i] = (vp8_prob) vp8_read_literal(bc, 8);
    270             }
    271             while (++i < 4);
    272         }
    273 
    274         if (vp8_read_bit(bc))
    275         {
    276             int i = 0;
    277 
    278             do
    279             {
    280                 pbi->common.fc.uv_mode_prob[i] = (vp8_prob) vp8_read_literal(bc, 8);
    281             }
    282             while (++i < 3);
    283         }
    284 
    285         read_mvcontexts(bc, mvc);
    286     }
    287 }
    288 
    289 void vp8_read_mb_modes_mv(VP8D_COMP *pbi, MODE_INFO *mi, MB_MODE_INFO *mbmi,
    290                             int mb_row, int mb_col)
    291 {
    292     const MV Zero = { 0, 0};
    293     vp8_reader *const bc = & pbi->bc;
    294     MV_CONTEXT *const mvc = pbi->common.fc.mvc;
    295     const int mis = pbi->common.mode_info_stride;
    296 
    297     MV *const mv = & mbmi->mv.as_mv;
    298     int mb_to_left_edge;
    299     int mb_to_right_edge;
    300     int mb_to_top_edge;
    301     int mb_to_bottom_edge;
    302 
    303     mb_to_top_edge = pbi->mb.mb_to_top_edge;
    304     mb_to_bottom_edge = pbi->mb.mb_to_bottom_edge;
    305     mb_to_top_edge -= LEFT_TOP_MARGIN;
    306     mb_to_bottom_edge += RIGHT_BOTTOM_MARGIN;
    307 
    308     mbmi->need_to_clamp_mvs = 0;
    309     // Distance of Mb to the various image edges.
    310     // These specified to 8th pel as they are always compared to MV values that are in 1/8th pel units
    311     pbi->mb.mb_to_left_edge =
    312     mb_to_left_edge = -((mb_col * 16) << 3);
    313     mb_to_left_edge -= LEFT_TOP_MARGIN;
    314 
    315     pbi->mb.mb_to_right_edge =
    316     mb_to_right_edge = ((pbi->common.mb_cols - 1 - mb_col) * 16) << 3;
    317     mb_to_right_edge += RIGHT_BOTTOM_MARGIN;
    318 
    319     // If required read in new segmentation data for this MB
    320     if (pbi->mb.update_mb_segmentation_map)
    321         vp8_read_mb_features(bc, mbmi, &pbi->mb);
    322 
    323     // Read the macroblock coeff skip flag if this feature is in use, else default to 0
    324     if (pbi->common.mb_no_coeff_skip)
    325         mbmi->mb_skip_coeff = vp8_read(bc, pbi->prob_skip_false);
    326     else
    327         mbmi->mb_skip_coeff = 0;
    328 
    329     if ((mbmi->ref_frame = (MV_REFERENCE_FRAME) vp8_read(bc, pbi->prob_intra)))    /* inter MB */
    330     {
    331         int rct[4];
    332         vp8_prob mv_ref_p [VP8_MVREFS-1];
    333         MV nearest, nearby, best_mv;
    334 
    335         if (vp8_read(bc, pbi->prob_last))
    336         {
    337             mbmi->ref_frame = (MV_REFERENCE_FRAME)((int)mbmi->ref_frame + (int)(1 + vp8_read(bc, pbi->prob_gf)));
    338         }
    339 
    340         vp8_find_near_mvs(&pbi->mb, mi, &nearest, &nearby, &best_mv, rct, mbmi->ref_frame, pbi->common.ref_frame_sign_bias);
    341 
    342         vp8_mv_ref_probs(mv_ref_p, rct);
    343 
    344         mbmi->uv_mode = DC_PRED;
    345         switch (mbmi->mode = read_mv_ref(bc, mv_ref_p))
    346         {
    347         case SPLITMV:
    348         {
    349             const int s = mbmi->partitioning =
    350                       vp8_treed_read(bc, vp8_mbsplit_tree, vp8_mbsplit_probs);
    351             const int num_p = vp8_mbsplit_count [s];
    352             int j = 0;
    353 
    354             do  /* for each subset j */
    355             {
    356                 B_MODE_INFO bmi;
    357                 MV *const mv = & bmi.mv.as_mv;
    358 
    359                 int k;  /* first block in subset j */
    360                 int mv_contz;
    361                 k = vp8_mbsplit_offset[s][j];
    362 
    363                 mv_contz = vp8_mv_cont(&(vp8_left_bmi(mi, k)->mv.as_mv), &(vp8_above_bmi(mi, k, mis)->mv.as_mv));
    364 
    365                 switch (bmi.mode = (B_PREDICTION_MODE) sub_mv_ref(bc, vp8_sub_mv_ref_prob2 [mv_contz])) //pc->fc.sub_mv_ref_prob))
    366                 {
    367                 case NEW4X4:
    368                     read_mv(bc, mv, (const MV_CONTEXT *) mvc);
    369                     mv->row += best_mv.row;
    370                     mv->col += best_mv.col;
    371   #ifdef VPX_MODE_COUNT
    372                     vp8_mv_cont_count[mv_contz][3]++;
    373   #endif
    374                     break;
    375                 case LEFT4X4:
    376                     *mv = vp8_left_bmi(mi, k)->mv.as_mv;
    377   #ifdef VPX_MODE_COUNT
    378                     vp8_mv_cont_count[mv_contz][0]++;
    379   #endif
    380                     break;
    381                 case ABOVE4X4:
    382                     *mv = vp8_above_bmi(mi, k, mis)->mv.as_mv;
    383   #ifdef VPX_MODE_COUNT
    384                     vp8_mv_cont_count[mv_contz][1]++;
    385   #endif
    386                     break;
    387                 case ZERO4X4:
    388                     *mv = Zero;
    389   #ifdef VPX_MODE_COUNT
    390                     vp8_mv_cont_count[mv_contz][2]++;
    391   #endif
    392                     break;
    393                 default:
    394                     break;
    395                 }
    396 
    397                 mbmi->need_to_clamp_mvs = (mv->col < mb_to_left_edge) ? 1 : 0;
    398                 mbmi->need_to_clamp_mvs |= (mv->col > mb_to_right_edge) ? 1 : 0;
    399                 mbmi->need_to_clamp_mvs |= (mv->row < mb_to_top_edge) ? 1 : 0;
    400                 mbmi->need_to_clamp_mvs |= (mv->row > mb_to_bottom_edge) ? 1 : 0;
    401 
    402                 {
    403                     /* Fill (uniform) modes, mvs of jth subset.
    404                      Must do it here because ensuing subsets can
    405                      refer back to us via "left" or "above". */
    406                     unsigned char *fill_offset;
    407                     unsigned int fill_count = vp8_mbsplit_fill_count[s];
    408 
    409                     fill_offset = &vp8_mbsplit_fill_offset[s][(unsigned char)j * vp8_mbsplit_fill_count[s]];
    410 
    411                     do {
    412                         mi->bmi[ *fill_offset] = bmi;
    413                       fill_offset++;
    414 
    415                     }while (--fill_count);
    416                 }
    417 
    418             }
    419             while (++j < num_p);
    420         }
    421 
    422         *mv = mi->bmi[15].mv.as_mv;
    423 
    424         break;  /* done with SPLITMV */
    425 
    426         case NEARMV:
    427             *mv = nearby;
    428             // Clip "next_nearest" so that it does not extend to far out of image
    429             mv->col = (mv->col < mb_to_left_edge) ? mb_to_left_edge : mv->col;
    430             mv->col = (mv->col > mb_to_right_edge) ? mb_to_right_edge : mv->col;
    431             mv->row = (mv->row < mb_to_top_edge) ? mb_to_top_edge : mv->row;
    432             mv->row = (mv->row > mb_to_bottom_edge) ? mb_to_bottom_edge : mv->row;
    433             goto propagate_mv;
    434 
    435         case NEARESTMV:
    436             *mv = nearest;
    437             // Clip "next_nearest" so that it does not extend to far out of image
    438             mv->col = (mv->col < mb_to_left_edge) ? mb_to_left_edge : mv->col;
    439             mv->col = (mv->col > mb_to_right_edge) ? mb_to_right_edge : mv->col;
    440             mv->row = (mv->row < mb_to_top_edge) ? mb_to_top_edge : mv->row;
    441             mv->row = (mv->row > mb_to_bottom_edge) ? mb_to_bottom_edge : mv->row;
    442             goto propagate_mv;
    443 
    444         case ZEROMV:
    445             *mv = Zero;
    446             goto propagate_mv;
    447 
    448         case NEWMV:
    449             read_mv(bc, mv, (const MV_CONTEXT *) mvc);
    450             mv->row += best_mv.row;
    451             mv->col += best_mv.col;
    452 
    453             /* Don't need to check this on NEARMV and NEARESTMV modes
    454              * since those modes clamp the MV. The NEWMV mode does not,
    455              * so signal to the prediction stage whether special
    456              * handling may be required.
    457              */
    458             mbmi->need_to_clamp_mvs = (mv->col < mb_to_left_edge) ? 1 : 0;
    459             mbmi->need_to_clamp_mvs |= (mv->col > mb_to_right_edge) ? 1 : 0;
    460             mbmi->need_to_clamp_mvs |= (mv->row < mb_to_top_edge) ? 1 : 0;
    461             mbmi->need_to_clamp_mvs |= (mv->row > mb_to_bottom_edge) ? 1 : 0;
    462 
    463         propagate_mv:  /* same MV throughout */
    464             {
    465                 //int i=0;
    466                 //do
    467                 //{
    468                 //  mi->bmi[i].mv.as_mv = *mv;
    469                 //}
    470                 //while( ++i < 16);
    471 
    472                 mi->bmi[0].mv.as_mv = *mv;
    473                 mi->bmi[1].mv.as_mv = *mv;
    474                 mi->bmi[2].mv.as_mv = *mv;
    475                 mi->bmi[3].mv.as_mv = *mv;
    476                 mi->bmi[4].mv.as_mv = *mv;
    477                 mi->bmi[5].mv.as_mv = *mv;
    478                 mi->bmi[6].mv.as_mv = *mv;
    479                 mi->bmi[7].mv.as_mv = *mv;
    480                 mi->bmi[8].mv.as_mv = *mv;
    481                 mi->bmi[9].mv.as_mv = *mv;
    482                 mi->bmi[10].mv.as_mv = *mv;
    483                 mi->bmi[11].mv.as_mv = *mv;
    484                 mi->bmi[12].mv.as_mv = *mv;
    485                 mi->bmi[13].mv.as_mv = *mv;
    486                 mi->bmi[14].mv.as_mv = *mv;
    487                 mi->bmi[15].mv.as_mv = *mv;
    488             }
    489             break;
    490         default:;
    491   #if CONFIG_DEBUG
    492             assert(0);
    493   #endif
    494         }
    495     }
    496     else
    497     {
    498         /* MB is intra coded */
    499         int j = 0;
    500         do
    501         {
    502             mi->bmi[j].mv.as_mv = Zero;
    503         }
    504         while (++j < 16);
    505 
    506         if ((mbmi->mode = (MB_PREDICTION_MODE) vp8_read_ymode(bc, pbi->common.fc.ymode_prob)) == B_PRED)
    507         {
    508             j = 0;
    509             do
    510             {
    511                 mi->bmi[j].mode = (B_PREDICTION_MODE)vp8_read_bmode(bc, pbi->common.fc.bmode_prob);
    512             }
    513             while (++j < 16);
    514         }
    515 
    516         mbmi->uv_mode = (MB_PREDICTION_MODE)vp8_read_uv_mode(bc, pbi->common.fc.uv_mode_prob);
    517     }
    518 
    519 }
    520 
    521 void vp8_decode_mode_mvs(VP8D_COMP *pbi)
    522 {
    523     MODE_INFO *mi = pbi->common.mi;
    524     int mb_row = -1;
    525 
    526     vp8_mb_mode_mv_init(pbi);
    527 
    528     while (++mb_row < pbi->common.mb_rows)
    529     {
    530         int mb_col = -1;
    531         int mb_to_top_edge;
    532         int mb_to_bottom_edge;
    533 
    534         pbi->mb.mb_to_top_edge =
    535         mb_to_top_edge = -((mb_row * 16)) << 3;
    536         mb_to_top_edge -= LEFT_TOP_MARGIN;
    537 
    538         pbi->mb.mb_to_bottom_edge =
    539         mb_to_bottom_edge = ((pbi->common.mb_rows - 1 - mb_row) * 16) << 3;
    540         mb_to_bottom_edge += RIGHT_BOTTOM_MARGIN;
    541 
    542         while (++mb_col < pbi->common.mb_cols)
    543         {
    544 //          vp8_read_mb_modes_mv(pbi, xd->mode_info_context, &xd->mode_info_context->mbmi, mb_row, mb_col);
    545             if(pbi->common.frame_type == KEY_FRAME)
    546                 vp8_kfread_modes(pbi, mi, mb_row, mb_col);
    547             else
    548                 vp8_read_mb_modes_mv(pbi, mi, &mi->mbmi, mb_row, mb_col);
    549 
    550             mi++;       // next macroblock
    551         }
    552 
    553         mi++;           // skip left predictor each row
    554     }
    555 }
    556 
    557