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 #include <math.h>
     12 
     13 #include "vp9/common/vp9_common.h"
     14 #include "vp9/common/vp9_entropymode.h"
     15 #include "vp9/common/vp9_systemdependent.h"
     16 #include "vp9/encoder/vp9_encodemv.h"
     17 
     18 
     19 #ifdef ENTROPY_STATS
     20 extern unsigned int active_section;
     21 #endif
     22 
     23 static void encode_mv_component(vp9_writer* w, int comp,
     24                                 const nmv_component* mvcomp, int usehp) {
     25   int offset;
     26   const int sign = comp < 0;
     27   const int mag = sign ? -comp : comp;
     28   const int mv_class = vp9_get_mv_class(mag - 1, &offset);
     29   const int d = offset >> 3;                // int mv data
     30   const int fr = (offset >> 1) & 3;         // fractional mv data
     31   const int hp = offset & 1;                // high precision mv data
     32 
     33   assert(comp != 0);
     34 
     35   // Sign
     36   vp9_write(w, sign, mvcomp->sign);
     37 
     38   // Class
     39   write_token(w, vp9_mv_class_tree, mvcomp->classes,
     40               &vp9_mv_class_encodings[mv_class]);
     41 
     42   // Integer bits
     43   if (mv_class == MV_CLASS_0) {
     44     write_token(w, vp9_mv_class0_tree, mvcomp->class0,
     45                 &vp9_mv_class0_encodings[d]);
     46   } else {
     47     int i;
     48     const int n = mv_class + CLASS0_BITS - 1;  // number of bits
     49     for (i = 0; i < n; ++i)
     50       vp9_write(w, (d >> i) & 1, mvcomp->bits[i]);
     51   }
     52 
     53   // Fractional bits
     54   write_token(w, vp9_mv_fp_tree,
     55               mv_class == MV_CLASS_0 ?  mvcomp->class0_fp[d] : mvcomp->fp,
     56               &vp9_mv_fp_encodings[fr]);
     57 
     58   // High precision bit
     59   if (usehp)
     60     vp9_write(w, hp,
     61               mv_class == MV_CLASS_0 ? mvcomp->class0_hp : mvcomp->hp);
     62 }
     63 
     64 
     65 static void build_nmv_component_cost_table(int *mvcost,
     66                                            const nmv_component* const mvcomp,
     67                                            int usehp) {
     68   int i, v;
     69   int sign_cost[2], class_cost[MV_CLASSES], class0_cost[CLASS0_SIZE];
     70   int bits_cost[MV_OFFSET_BITS][2];
     71   int class0_fp_cost[CLASS0_SIZE][4], fp_cost[4];
     72   int class0_hp_cost[2], hp_cost[2];
     73 
     74   sign_cost[0] = vp9_cost_zero(mvcomp->sign);
     75   sign_cost[1] = vp9_cost_one(mvcomp->sign);
     76   vp9_cost_tokens(class_cost, mvcomp->classes, vp9_mv_class_tree);
     77   vp9_cost_tokens(class0_cost, mvcomp->class0, vp9_mv_class0_tree);
     78   for (i = 0; i < MV_OFFSET_BITS; ++i) {
     79     bits_cost[i][0] = vp9_cost_zero(mvcomp->bits[i]);
     80     bits_cost[i][1] = vp9_cost_one(mvcomp->bits[i]);
     81   }
     82 
     83   for (i = 0; i < CLASS0_SIZE; ++i)
     84     vp9_cost_tokens(class0_fp_cost[i], mvcomp->class0_fp[i], vp9_mv_fp_tree);
     85   vp9_cost_tokens(fp_cost, mvcomp->fp, vp9_mv_fp_tree);
     86 
     87   if (usehp) {
     88     class0_hp_cost[0] = vp9_cost_zero(mvcomp->class0_hp);
     89     class0_hp_cost[1] = vp9_cost_one(mvcomp->class0_hp);
     90     hp_cost[0] = vp9_cost_zero(mvcomp->hp);
     91     hp_cost[1] = vp9_cost_one(mvcomp->hp);
     92   }
     93   mvcost[0] = 0;
     94   for (v = 1; v <= MV_MAX; ++v) {
     95     int z, c, o, d, e, f, cost = 0;
     96     z = v - 1;
     97     c = vp9_get_mv_class(z, &o);
     98     cost += class_cost[c];
     99     d = (o >> 3);               /* int mv data */
    100     f = (o >> 1) & 3;           /* fractional pel mv data */
    101     e = (o & 1);                /* high precision mv data */
    102     if (c == MV_CLASS_0) {
    103       cost += class0_cost[d];
    104     } else {
    105       int i, b;
    106       b = c + CLASS0_BITS - 1;  /* number of bits */
    107       for (i = 0; i < b; ++i)
    108         cost += bits_cost[i][((d >> i) & 1)];
    109     }
    110     if (c == MV_CLASS_0) {
    111       cost += class0_fp_cost[d][f];
    112     } else {
    113       cost += fp_cost[f];
    114     }
    115     if (usehp) {
    116       if (c == MV_CLASS_0) {
    117         cost += class0_hp_cost[e];
    118       } else {
    119         cost += hp_cost[e];
    120       }
    121     }
    122     mvcost[v] = cost + sign_cost[0];
    123     mvcost[-v] = cost + sign_cost[1];
    124   }
    125 }
    126 
    127 static int update_mv(vp9_writer *w, const unsigned int ct[2], vp9_prob *cur_p,
    128                      vp9_prob upd_p) {
    129   const vp9_prob new_p = get_binary_prob(ct[0], ct[1]);
    130   vp9_prob mod_p = new_p | 1;
    131   const int cur_b = cost_branch256(ct, *cur_p);
    132   const int mod_b = cost_branch256(ct, mod_p);
    133   const int cost = 7 * 256 + (vp9_cost_one(upd_p) - vp9_cost_zero(upd_p));
    134   if (cur_b - mod_b > cost) {
    135     *cur_p = mod_p;
    136     vp9_write(w, 1, upd_p);
    137     vp9_write_literal(w, mod_p >> 1, 7);
    138     return 1;
    139   } else {
    140     vp9_write(w, 0, upd_p);
    141     return 0;
    142   }
    143 }
    144 
    145 static void counts_to_nmv_context(
    146     nmv_context_counts *nmv_count,
    147     int usehp,
    148     unsigned int (*branch_ct_joint)[2],
    149     unsigned int (*branch_ct_sign)[2],
    150     unsigned int (*branch_ct_classes)[MV_CLASSES - 1][2],
    151     unsigned int (*branch_ct_class0)[CLASS0_SIZE - 1][2],
    152     unsigned int (*branch_ct_bits)[MV_OFFSET_BITS][2],
    153     unsigned int (*branch_ct_class0_fp)[CLASS0_SIZE][4 - 1][2],
    154     unsigned int (*branch_ct_fp)[4 - 1][2],
    155     unsigned int (*branch_ct_class0_hp)[2],
    156     unsigned int (*branch_ct_hp)[2]) {
    157   int i, j, k;
    158   vp9_tree_probs_from_distribution(vp9_mv_joint_tree, branch_ct_joint,
    159                                    nmv_count->joints);
    160   for (i = 0; i < 2; ++i) {
    161     const uint32_t s0 = nmv_count->comps[i].sign[0];
    162     const uint32_t s1 = nmv_count->comps[i].sign[1];
    163 
    164     branch_ct_sign[i][0] = s0;
    165     branch_ct_sign[i][1] = s1;
    166     vp9_tree_probs_from_distribution(vp9_mv_class_tree,
    167                                     branch_ct_classes[i],
    168                                     nmv_count->comps[i].classes);
    169     vp9_tree_probs_from_distribution(vp9_mv_class0_tree,
    170                                      branch_ct_class0[i],
    171                                      nmv_count->comps[i].class0);
    172     for (j = 0; j < MV_OFFSET_BITS; ++j) {
    173       const uint32_t b0 = nmv_count->comps[i].bits[j][0];
    174       const uint32_t b1 = nmv_count->comps[i].bits[j][1];
    175 
    176       branch_ct_bits[i][j][0] = b0;
    177       branch_ct_bits[i][j][1] = b1;
    178     }
    179   }
    180   for (i = 0; i < 2; ++i) {
    181     for (k = 0; k < CLASS0_SIZE; ++k) {
    182       vp9_tree_probs_from_distribution(vp9_mv_fp_tree,
    183                                        branch_ct_class0_fp[i][k],
    184                                        nmv_count->comps[i].class0_fp[k]);
    185     }
    186     vp9_tree_probs_from_distribution(vp9_mv_fp_tree,
    187                                      branch_ct_fp[i],
    188                                      nmv_count->comps[i].fp);
    189   }
    190   if (usehp) {
    191     for (i = 0; i < 2; ++i) {
    192       const uint32_t c0_hp0 = nmv_count->comps[i].class0_hp[0];
    193       const uint32_t c0_hp1 = nmv_count->comps[i].class0_hp[1];
    194       const uint32_t hp0 = nmv_count->comps[i].hp[0];
    195       const uint32_t hp1 = nmv_count->comps[i].hp[1];
    196 
    197       branch_ct_class0_hp[i][0] = c0_hp0;
    198       branch_ct_class0_hp[i][1] = c0_hp1;
    199 
    200       branch_ct_hp[i][0] = hp0;
    201       branch_ct_hp[i][1] = hp1;
    202     }
    203   }
    204 }
    205 
    206 void vp9_write_nmv_probs(VP9_COMP* const cpi, int usehp, vp9_writer* const bc) {
    207   int i, j;
    208   unsigned int branch_ct_joint[MV_JOINTS - 1][2];
    209   unsigned int branch_ct_sign[2][2];
    210   unsigned int branch_ct_classes[2][MV_CLASSES - 1][2];
    211   unsigned int branch_ct_class0[2][CLASS0_SIZE - 1][2];
    212   unsigned int branch_ct_bits[2][MV_OFFSET_BITS][2];
    213   unsigned int branch_ct_class0_fp[2][CLASS0_SIZE][4 - 1][2];
    214   unsigned int branch_ct_fp[2][4 - 1][2];
    215   unsigned int branch_ct_class0_hp[2][2];
    216   unsigned int branch_ct_hp[2][2];
    217   nmv_context *mvc = &cpi->common.fc.nmvc;
    218 
    219   counts_to_nmv_context(&cpi->NMVcount, usehp,
    220                         branch_ct_joint, branch_ct_sign, branch_ct_classes,
    221                         branch_ct_class0, branch_ct_bits,
    222                         branch_ct_class0_fp, branch_ct_fp,
    223                         branch_ct_class0_hp, branch_ct_hp);
    224 
    225   for (j = 0; j < MV_JOINTS - 1; ++j)
    226     update_mv(bc, branch_ct_joint[j], &mvc->joints[j], NMV_UPDATE_PROB);
    227 
    228   for (i = 0; i < 2; ++i) {
    229     update_mv(bc, branch_ct_sign[i], &mvc->comps[i].sign, NMV_UPDATE_PROB);
    230     for (j = 0; j < MV_CLASSES - 1; ++j)
    231       update_mv(bc, branch_ct_classes[i][j], &mvc->comps[i].classes[j],
    232                 NMV_UPDATE_PROB);
    233 
    234     for (j = 0; j < CLASS0_SIZE - 1; ++j)
    235       update_mv(bc, branch_ct_class0[i][j], &mvc->comps[i].class0[j],
    236                 NMV_UPDATE_PROB);
    237 
    238     for (j = 0; j < MV_OFFSET_BITS; ++j)
    239       update_mv(bc, branch_ct_bits[i][j], &mvc->comps[i].bits[j],
    240                 NMV_UPDATE_PROB);
    241   }
    242 
    243   for (i = 0; i < 2; ++i) {
    244     for (j = 0; j < CLASS0_SIZE; ++j) {
    245       int k;
    246       for (k = 0; k < 3; ++k)
    247         update_mv(bc, branch_ct_class0_fp[i][j][k],
    248                   &mvc->comps[i].class0_fp[j][k], NMV_UPDATE_PROB);
    249     }
    250 
    251     for (j = 0; j < 3; ++j)
    252       update_mv(bc, branch_ct_fp[i][j], &mvc->comps[i].fp[j], NMV_UPDATE_PROB);
    253   }
    254 
    255   if (usehp) {
    256     for (i = 0; i < 2; ++i) {
    257       update_mv(bc, branch_ct_class0_hp[i], &mvc->comps[i].class0_hp,
    258                 NMV_UPDATE_PROB);
    259       update_mv(bc, branch_ct_hp[i], &mvc->comps[i].hp,
    260                 NMV_UPDATE_PROB);
    261     }
    262   }
    263 }
    264 
    265 void vp9_encode_mv(VP9_COMP* cpi, vp9_writer* w,
    266                    const MV* mv, const MV* ref,
    267                    const nmv_context* mvctx, int usehp) {
    268   const MV diff = {mv->row - ref->row,
    269                    mv->col - ref->col};
    270   const MV_JOINT_TYPE j = vp9_get_mv_joint(&diff);
    271   usehp = usehp && vp9_use_mv_hp(ref);
    272 
    273   write_token(w, vp9_mv_joint_tree, mvctx->joints, &vp9_mv_joint_encodings[j]);
    274   if (mv_joint_vertical(j))
    275     encode_mv_component(w, diff.row, &mvctx->comps[0], usehp);
    276 
    277   if (mv_joint_horizontal(j))
    278     encode_mv_component(w, diff.col, &mvctx->comps[1], usehp);
    279 
    280   // If auto_mv_step_size is enabled then keep track of the largest
    281   // motion vector component used.
    282   if (!cpi->dummy_packing && cpi->sf.auto_mv_step_size) {
    283     unsigned int maxv = MAX(abs(mv->row), abs(mv->col)) >> 3;
    284     cpi->max_mv_magnitude = MAX(maxv, cpi->max_mv_magnitude);
    285   }
    286 }
    287 
    288 void vp9_build_nmv_cost_table(int *mvjoint,
    289                               int *mvcost[2],
    290                               const nmv_context* const mvctx,
    291                               int usehp,
    292                               int mvc_flag_v,
    293                               int mvc_flag_h) {
    294   vp9_clear_system_state();
    295   vp9_cost_tokens(mvjoint, mvctx->joints, vp9_mv_joint_tree);
    296   if (mvc_flag_v)
    297     build_nmv_component_cost_table(mvcost[0], &mvctx->comps[0], usehp);
    298   if (mvc_flag_h)
    299     build_nmv_component_cost_table(mvcost[1], &mvctx->comps[1], usehp);
    300 }
    301 
    302 static void inc_mvs(int_mv mv[2], int_mv ref[2], int is_compound,
    303                     nmv_context_counts *counts) {
    304   int i;
    305   for (i = 0; i < 1 + is_compound; ++i) {
    306     const MV diff = { mv[i].as_mv.row - ref[i].as_mv.row,
    307                       mv[i].as_mv.col - ref[i].as_mv.col };
    308     vp9_inc_mv(&diff, counts);
    309   }
    310 }
    311 
    312 void vp9_update_mv_count(VP9_COMP *cpi, MACROBLOCK *x, int_mv best_ref_mv[2]) {
    313   MODE_INFO *mi = x->e_mbd.mi_8x8[0];
    314   MB_MODE_INFO *const mbmi = &mi->mbmi;
    315   const int is_compound = has_second_ref(mbmi);
    316 
    317   if (mbmi->sb_type < BLOCK_8X8) {
    318     const int num_4x4_w = num_4x4_blocks_wide_lookup[mbmi->sb_type];
    319     const int num_4x4_h = num_4x4_blocks_high_lookup[mbmi->sb_type];
    320     int idx, idy;
    321 
    322     for (idy = 0; idy < 2; idy += num_4x4_h) {
    323       for (idx = 0; idx < 2; idx += num_4x4_w) {
    324         const int i = idy * 2 + idx;
    325         if (mi->bmi[i].as_mode == NEWMV)
    326           inc_mvs(mi->bmi[i].as_mv, best_ref_mv, is_compound, &cpi->NMVcount);
    327       }
    328     }
    329   } else if (mbmi->mode == NEWMV) {
    330     inc_mvs(mbmi->mv, best_ref_mv, is_compound, &cpi->NMVcount);
    331   }
    332 }
    333