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 "vp9/common/vp9_onyxc_int.h"
     13 #include "vp9/common/vp9_entropymv.h"
     14 
     15 #define MV_COUNT_SAT 20
     16 #define MV_MAX_UPDATE_FACTOR 128
     17 
     18 /* Integer pel reference mv threshold for use of high-precision 1/8 mv */
     19 #define COMPANDED_MVREF_THRESH 8
     20 
     21 const vp9_tree_index vp9_mv_joint_tree[TREE_SIZE(MV_JOINTS)] = {
     22   -MV_JOINT_ZERO, 2,
     23   -MV_JOINT_HNZVZ, 4,
     24   -MV_JOINT_HZVNZ, -MV_JOINT_HNZVNZ
     25 };
     26 struct vp9_token vp9_mv_joint_encodings[MV_JOINTS];
     27 
     28 const vp9_tree_index vp9_mv_class_tree[TREE_SIZE(MV_CLASSES)] = {
     29   -MV_CLASS_0, 2,
     30   -MV_CLASS_1, 4,
     31   6, 8,
     32   -MV_CLASS_2, -MV_CLASS_3,
     33   10, 12,
     34   -MV_CLASS_4, -MV_CLASS_5,
     35   -MV_CLASS_6, 14,
     36   16, 18,
     37   -MV_CLASS_7, -MV_CLASS_8,
     38   -MV_CLASS_9, -MV_CLASS_10,
     39 };
     40 struct vp9_token vp9_mv_class_encodings[MV_CLASSES];
     41 
     42 const vp9_tree_index vp9_mv_class0_tree[TREE_SIZE(CLASS0_SIZE)] = {
     43   -0, -1,
     44 };
     45 struct vp9_token vp9_mv_class0_encodings[CLASS0_SIZE];
     46 
     47 const vp9_tree_index vp9_mv_fp_tree[TREE_SIZE(4)] = {
     48   -0, 2,
     49   -1, 4,
     50   -2, -3
     51 };
     52 struct vp9_token vp9_mv_fp_encodings[4];
     53 
     54 static const nmv_context default_nmv_context = {
     55   {32, 64, 96},
     56   { // NOLINT
     57     { /* vert component */ // NOLINT
     58       128,                                                  /* sign */
     59       {224, 144, 192, 168, 192, 176, 192, 198, 198, 245},   /* class */
     60       {216},                                                /* class0 */
     61       {136, 140, 148, 160, 176, 192, 224, 234, 234, 240},   /* bits */
     62       {{128, 128, 64}, {96, 112, 64}},                      /* class0_fp */
     63       {64, 96, 64},                                         /* fp */
     64       160,                                                  /* class0_hp bit */
     65       128,                                                  /* hp */
     66     },
     67     { /* hor component */ // NOLINT
     68       128,                                                  /* sign */
     69       {216, 128, 176, 160, 176, 176, 192, 198, 198, 208},   /* class */
     70       {208},                                                /* class0 */
     71       {136, 140, 148, 160, 176, 192, 224, 234, 234, 240},   /* bits */
     72       {{128, 128, 64}, {96, 112, 64}},                      /* class0_fp */
     73       {64, 96, 64},                                         /* fp */
     74       160,                                                  /* class0_hp bit */
     75       128,                                                  /* hp */
     76     }
     77   },
     78 };
     79 
     80 #define mv_class_base(c) ((c) ? (CLASS0_SIZE << (c + 2)) : 0)
     81 
     82 static const uint8_t log_in_base_2[] = {
     83   0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
     84   4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
     85   5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6,
     86   6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
     87   6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
     88   6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
     89   7, 7, 7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9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
    117   9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
    118   9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
    119   9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
    120   9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
    121   9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
    122   9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
    123   9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
    124   9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
    125   9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10
    126 };
    127 
    128 MV_CLASS_TYPE vp9_get_mv_class(int z, int *offset) {
    129   MV_CLASS_TYPE c = MV_CLASS_0;
    130   if (z >= CLASS0_SIZE * 4096)
    131     c = MV_CLASS_10;
    132   else
    133     c = log_in_base_2[z >> 3];
    134 
    135   if (offset)
    136     *offset = z - mv_class_base(c);
    137   return c;
    138 }
    139 
    140 int vp9_use_mv_hp(const MV *ref) {
    141   return (abs(ref->row) >> 3) < COMPANDED_MVREF_THRESH &&
    142          (abs(ref->col) >> 3) < COMPANDED_MVREF_THRESH;
    143 }
    144 
    145 int vp9_get_mv_mag(MV_CLASS_TYPE c, int offset) {
    146   return mv_class_base(c) + offset;
    147 }
    148 
    149 static void inc_mv_component(int v, nmv_component_counts *comp_counts,
    150                              int incr, int usehp) {
    151   int s, z, c, o, d, e, f;
    152   assert(v != 0);            /* should not be zero */
    153   s = v < 0;
    154   comp_counts->sign[s] += incr;
    155   z = (s ? -v : v) - 1;       /* magnitude - 1 */
    156 
    157   c = vp9_get_mv_class(z, &o);
    158   comp_counts->classes[c] += incr;
    159 
    160   d = (o >> 3);               /* int mv data */
    161   f = (o >> 1) & 3;           /* fractional pel mv data */
    162   e = (o & 1);                /* high precision mv data */
    163 
    164   if (c == MV_CLASS_0) {
    165     comp_counts->class0[d] += incr;
    166     comp_counts->class0_fp[d][f] += incr;
    167     comp_counts->class0_hp[e] += usehp * incr;
    168   } else {
    169     int i;
    170     int b = c + CLASS0_BITS - 1;  // number of bits
    171     for (i = 0; i < b; ++i)
    172       comp_counts->bits[i][((d >> i) & 1)] += incr;
    173     comp_counts->fp[f] += incr;
    174     comp_counts->hp[e] += usehp * incr;
    175   }
    176 }
    177 
    178 void vp9_inc_mv(const MV *mv, nmv_context_counts *counts) {
    179   if (counts != NULL) {
    180     const MV_JOINT_TYPE j = vp9_get_mv_joint(mv);
    181     ++counts->joints[j];
    182 
    183     if (mv_joint_vertical(j)) {
    184       inc_mv_component(mv->row, &counts->comps[0], 1, 1);
    185     }
    186 
    187     if (mv_joint_horizontal(j)) {
    188       inc_mv_component(mv->col, &counts->comps[1], 1, 1);
    189     }
    190   }
    191 }
    192 
    193 static vp9_prob adapt_prob(vp9_prob prep, const unsigned int ct[2]) {
    194   return merge_probs(prep, ct, MV_COUNT_SAT, MV_MAX_UPDATE_FACTOR);
    195 }
    196 
    197 static void adapt_probs(const vp9_tree_index *tree, const vp9_prob *pre_probs,
    198                         const unsigned int *counts, vp9_prob *probs) {
    199   tree_merge_probs(tree, pre_probs, counts, MV_COUNT_SAT, MV_MAX_UPDATE_FACTOR,
    200                    probs);
    201 }
    202 
    203 void vp9_adapt_mv_probs(VP9_COMMON *cm, int allow_hp) {
    204   int i, j;
    205 
    206   nmv_context *fc = &cm->fc.nmvc;
    207   const nmv_context *pre_fc = &cm->frame_contexts[cm->frame_context_idx].nmvc;
    208   const nmv_context_counts *counts = &cm->counts.mv;
    209 
    210   adapt_probs(vp9_mv_joint_tree, pre_fc->joints, counts->joints, fc->joints);
    211 
    212   for (i = 0; i < 2; ++i) {
    213     nmv_component *comp = &fc->comps[i];
    214     const nmv_component *pre_comp = &pre_fc->comps[i];
    215     const nmv_component_counts *c = &counts->comps[i];
    216 
    217     comp->sign = adapt_prob(pre_comp->sign, c->sign);
    218     adapt_probs(vp9_mv_class_tree, pre_comp->classes, c->classes,
    219                 comp->classes);
    220     adapt_probs(vp9_mv_class0_tree, pre_comp->class0, c->class0, comp->class0);
    221 
    222     for (j = 0; j < MV_OFFSET_BITS; ++j)
    223       comp->bits[j] = adapt_prob(pre_comp->bits[j], c->bits[j]);
    224 
    225     for (j = 0; j < CLASS0_SIZE; ++j)
    226       adapt_probs(vp9_mv_fp_tree, pre_comp->class0_fp[j], c->class0_fp[j],
    227                   comp->class0_fp[j]);
    228 
    229     adapt_probs(vp9_mv_fp_tree, pre_comp->fp, c->fp, comp->fp);
    230 
    231     if (allow_hp) {
    232       comp->class0_hp = adapt_prob(pre_comp->class0_hp, c->class0_hp);
    233       comp->hp = adapt_prob(pre_comp->hp, c->hp);
    234     }
    235   }
    236 }
    237 
    238 void vp9_entropy_mv_init() {
    239   vp9_tokens_from_tree(vp9_mv_joint_encodings, vp9_mv_joint_tree);
    240   vp9_tokens_from_tree(vp9_mv_class_encodings, vp9_mv_class_tree);
    241   vp9_tokens_from_tree(vp9_mv_class0_encodings, vp9_mv_class0_tree);
    242   vp9_tokens_from_tree(vp9_mv_fp_encodings, vp9_mv_fp_tree);
    243 }
    244 
    245 void vp9_init_mv_probs(VP9_COMMON *cm) {
    246   cm->fc.nmvc = default_nmv_context;
    247 }
    248