Home | History | Annotate | Download | only in common
      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 "entropymode.h"
     13 #include "entropy.h"
     14 #include "vpx_mem/vpx_mem.h"
     15 
     16 static const unsigned int kf_y_mode_cts[VP8_YMODES] = { 1607, 915, 812, 811, 5455};
     17 static const unsigned int y_mode_cts  [VP8_YMODES] = { 8080, 1908, 1582, 1007, 5874};
     18 
     19 static const unsigned int uv_mode_cts  [VP8_UV_MODES] = { 59483, 13605, 16492, 4230};
     20 static const unsigned int kf_uv_mode_cts[VP8_UV_MODES] = { 5319, 1904, 1703, 674};
     21 
     22 static const unsigned int bmode_cts[VP8_BINTRAMODES] =
     23 {
     24     43891, 17694, 10036, 3920, 3363, 2546, 5119, 3221, 2471, 1723
     25 };
     26 
     27 typedef enum
     28 {
     29     SUBMVREF_NORMAL,
     30     SUBMVREF_LEFT_ZED,
     31     SUBMVREF_ABOVE_ZED,
     32     SUBMVREF_LEFT_ABOVE_SAME,
     33     SUBMVREF_LEFT_ABOVE_ZED
     34 } sumvfref_t;
     35 
     36 int vp8_mv_cont(const MV *l, const MV *a)
     37 {
     38     int lez = (l->row == 0 && l->col == 0);
     39     int aez = (a->row == 0 && a->col == 0);
     40     int lea = (l->row == a->row && l->col == a->col);
     41 
     42     if (lea && lez)
     43         return SUBMVREF_LEFT_ABOVE_ZED;
     44 
     45     if (lea)
     46         return SUBMVREF_LEFT_ABOVE_SAME;
     47 
     48     if (aez)
     49         return SUBMVREF_ABOVE_ZED;
     50 
     51     if (lez)
     52         return SUBMVREF_LEFT_ZED;
     53 
     54     return SUBMVREF_NORMAL;
     55 }
     56 
     57 static const vp8_prob sub_mv_ref_prob [VP8_SUBMVREFS-1] = { 180, 162, 25};
     58 
     59 const vp8_prob vp8_sub_mv_ref_prob2 [SUBMVREF_COUNT][VP8_SUBMVREFS-1] =
     60 {
     61     { 147, 136, 18 },
     62     { 106, 145, 1  },
     63     { 179, 121, 1  },
     64     { 223, 1  , 34 },
     65     { 208, 1  , 1  }
     66 };
     67 
     68 
     69 
     70 vp8_mbsplit vp8_mbsplits [VP8_NUMMBSPLITS] =
     71 {
     72     {
     73         0,  0,  0,  0,
     74         0,  0,  0,  0,
     75         1,  1,  1,  1,
     76         1,  1,  1,  1,
     77     },
     78     {
     79         0,  0,  1,  1,
     80         0,  0,  1,  1,
     81         0,  0,  1,  1,
     82         0,  0,  1,  1,
     83     },
     84     {
     85         0,  0,  1,  1,
     86         0,  0,  1,  1,
     87         2,  2,  3,  3,
     88         2,  2,  3,  3,
     89     },
     90     {
     91         0,  1,  2,  3,
     92         4,  5,  6,  7,
     93         8,  9,  10, 11,
     94         12, 13, 14, 15,
     95     },
     96 };
     97 
     98 const int vp8_mbsplit_count [VP8_NUMMBSPLITS] = { 2, 2, 4, 16};
     99 
    100 const vp8_prob vp8_mbsplit_probs [VP8_NUMMBSPLITS-1] = { 110, 111, 150};
    101 
    102 
    103 /* Array indices are identical to previously-existing INTRAMODECONTEXTNODES. */
    104 
    105 const vp8_tree_index vp8_bmode_tree[18] =     /* INTRAMODECONTEXTNODE value */
    106 {
    107     -B_DC_PRED, 2,                             /* 0 = DC_NODE */
    108     -B_TM_PRED, 4,                            /* 1 = TM_NODE */
    109     -B_VE_PRED, 6,                           /* 2 = VE_NODE */
    110     8, 12,                                  /* 3 = COM_NODE */
    111     -B_HE_PRED, 10,                        /* 4 = HE_NODE */
    112     -B_RD_PRED, -B_VR_PRED,               /* 5 = RD_NODE */
    113     -B_LD_PRED, 14,                        /* 6 = LD_NODE */
    114     -B_VL_PRED, 16,                      /* 7 = VL_NODE */
    115     -B_HD_PRED, -B_HU_PRED             /* 8 = HD_NODE */
    116 };
    117 
    118 /* Again, these trees use the same probability indices as their
    119    explicitly-programmed predecessors. */
    120 
    121 const vp8_tree_index vp8_ymode_tree[8] =
    122 {
    123     -DC_PRED, 2,
    124     4, 6,
    125     -V_PRED, -H_PRED,
    126     -TM_PRED, -B_PRED
    127 };
    128 
    129 const vp8_tree_index vp8_kf_ymode_tree[8] =
    130 {
    131     -B_PRED, 2,
    132     4, 6,
    133     -DC_PRED, -V_PRED,
    134     -H_PRED, -TM_PRED
    135 };
    136 
    137 const vp8_tree_index vp8_uv_mode_tree[6] =
    138 {
    139     -DC_PRED, 2,
    140     -V_PRED, 4,
    141     -H_PRED, -TM_PRED
    142 };
    143 
    144 const vp8_tree_index vp8_mbsplit_tree[6] =
    145 {
    146     -3, 2,
    147     -2, 4,
    148     -0, -1
    149 };
    150 
    151 const vp8_tree_index vp8_mv_ref_tree[8] =
    152 {
    153     -ZEROMV, 2,
    154     -NEARESTMV, 4,
    155     -NEARMV, 6,
    156     -NEWMV, -SPLITMV
    157 };
    158 
    159 const vp8_tree_index vp8_sub_mv_ref_tree[6] =
    160 {
    161     -LEFT4X4, 2,
    162     -ABOVE4X4, 4,
    163     -ZERO4X4, -NEW4X4
    164 };
    165 
    166 
    167 struct vp8_token_struct vp8_bmode_encodings   [VP8_BINTRAMODES];
    168 struct vp8_token_struct vp8_ymode_encodings   [VP8_YMODES];
    169 struct vp8_token_struct vp8_kf_ymode_encodings [VP8_YMODES];
    170 struct vp8_token_struct vp8_uv_mode_encodings  [VP8_UV_MODES];
    171 struct vp8_token_struct vp8_mbsplit_encodings [VP8_NUMMBSPLITS];
    172 
    173 struct vp8_token_struct vp8_mv_ref_encoding_array    [VP8_MVREFS];
    174 struct vp8_token_struct vp8_sub_mv_ref_encoding_array [VP8_SUBMVREFS];
    175 
    176 
    177 const vp8_tree_index vp8_small_mvtree [14] =
    178 {
    179     2, 8,
    180     4, 6,
    181     -0, -1,
    182     -2, -3,
    183     10, 12,
    184     -4, -5,
    185     -6, -7
    186 };
    187 
    188 struct vp8_token_struct vp8_small_mvencodings [8];
    189 
    190 void vp8_init_mbmode_probs(VP8_COMMON *x)
    191 {
    192     unsigned int bct [VP8_YMODES] [2];      /* num Ymodes > num UV modes */
    193 
    194     vp8_tree_probs_from_distribution(
    195         VP8_YMODES, vp8_ymode_encodings, vp8_ymode_tree,
    196         x->fc.ymode_prob, bct, y_mode_cts,
    197         256, 1
    198     );
    199     vp8_tree_probs_from_distribution(
    200         VP8_YMODES, vp8_kf_ymode_encodings, vp8_kf_ymode_tree,
    201         x->kf_ymode_prob, bct, kf_y_mode_cts,
    202         256, 1
    203     );
    204     vp8_tree_probs_from_distribution(
    205         VP8_UV_MODES, vp8_uv_mode_encodings, vp8_uv_mode_tree,
    206         x->fc.uv_mode_prob, bct, uv_mode_cts,
    207         256, 1
    208     );
    209     vp8_tree_probs_from_distribution(
    210         VP8_UV_MODES, vp8_uv_mode_encodings, vp8_uv_mode_tree,
    211         x->kf_uv_mode_prob, bct, kf_uv_mode_cts,
    212         256, 1
    213     );
    214     vpx_memcpy(x->fc.sub_mv_ref_prob, sub_mv_ref_prob, sizeof(sub_mv_ref_prob));
    215 }
    216 
    217 
    218 static void intra_bmode_probs_from_distribution(
    219     vp8_prob p [VP8_BINTRAMODES-1],
    220     unsigned int branch_ct [VP8_BINTRAMODES-1] [2],
    221     const unsigned int events [VP8_BINTRAMODES]
    222 )
    223 {
    224     vp8_tree_probs_from_distribution(
    225         VP8_BINTRAMODES, vp8_bmode_encodings, vp8_bmode_tree,
    226         p, branch_ct, events,
    227         256, 1
    228     );
    229 }
    230 
    231 void vp8_default_bmode_probs(vp8_prob p [VP8_BINTRAMODES-1])
    232 {
    233     unsigned int branch_ct [VP8_BINTRAMODES-1] [2];
    234     intra_bmode_probs_from_distribution(p, branch_ct, bmode_cts);
    235 }
    236 
    237 void vp8_kf_default_bmode_probs(vp8_prob p [VP8_BINTRAMODES] [VP8_BINTRAMODES] [VP8_BINTRAMODES-1])
    238 {
    239     unsigned int branch_ct [VP8_BINTRAMODES-1] [2];
    240 
    241     int i = 0;
    242 
    243     do
    244     {
    245         int j = 0;
    246 
    247         do
    248         {
    249             intra_bmode_probs_from_distribution(
    250                 p[i][j], branch_ct, vp8_kf_default_bmode_counts[i][j]);
    251 
    252         }
    253         while (++j < VP8_BINTRAMODES);
    254     }
    255     while (++i < VP8_BINTRAMODES);
    256 }
    257 
    258 
    259 void vp8_entropy_mode_init()
    260 {
    261     vp8_tokens_from_tree(vp8_bmode_encodings,   vp8_bmode_tree);
    262     vp8_tokens_from_tree(vp8_ymode_encodings,   vp8_ymode_tree);
    263     vp8_tokens_from_tree(vp8_kf_ymode_encodings, vp8_kf_ymode_tree);
    264     vp8_tokens_from_tree(vp8_uv_mode_encodings,  vp8_uv_mode_tree);
    265     vp8_tokens_from_tree(vp8_mbsplit_encodings, vp8_mbsplit_tree);
    266 
    267     vp8_tokens_from_tree_offset(vp8_mv_ref_encoding_array,
    268                                 vp8_mv_ref_tree, NEARESTMV);
    269     vp8_tokens_from_tree_offset(vp8_sub_mv_ref_encoding_array,
    270                                 vp8_sub_mv_ref_tree, LEFT4X4);
    271 
    272     vp8_tokens_from_tree(vp8_small_mvencodings, vp8_small_mvtree);
    273 }
    274