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 "vp8/common/common.h"
     12 #include "encodemv.h"
     13 #include "vp8/common/entropymode.h"
     14 #include "vp8/common/systemdependent.h"
     15 #include "vpx_ports/system_state.h"
     16 
     17 #include <math.h>
     18 
     19 #ifdef VP8_ENTROPY_STATS
     20 extern unsigned int active_section;
     21 #endif
     22 
     23 static void encode_mvcomponent(vp8_writer *const w, const int v,
     24                                const struct mv_context *mvc) {
     25   const vp8_prob *p = mvc->prob;
     26   const int x = v < 0 ? -v : v;
     27 
     28   if (x < mvnum_short) { /* Small */
     29     vp8_write(w, 0, p[mvpis_short]);
     30     vp8_treed_write(w, vp8_small_mvtree, p + MVPshort, x, 3);
     31 
     32     if (!x) return; /* no sign bit */
     33   } else {          /* Large */
     34     int i = 0;
     35 
     36     vp8_write(w, 1, p[mvpis_short]);
     37 
     38     do
     39       vp8_write(w, (x >> i) & 1, p[MVPbits + i]);
     40 
     41     while (++i < 3);
     42 
     43     i = mvlong_width - 1; /* Skip bit 3, which is sometimes implicit */
     44 
     45     do
     46       vp8_write(w, (x >> i) & 1, p[MVPbits + i]);
     47 
     48     while (--i > 3);
     49 
     50     if (x & 0xFFF0) vp8_write(w, (x >> 3) & 1, p[MVPbits + 3]);
     51   }
     52 
     53   vp8_write(w, v < 0, p[MVPsign]);
     54 }
     55 #if 0
     56 static int max_mv_r = 0;
     57 static int max_mv_c = 0;
     58 #endif
     59 void vp8_encode_motion_vector(vp8_writer *w, const MV *mv,
     60                               const MV_CONTEXT *mvc) {
     61 #if 0
     62     {
     63         if (abs(mv->row >> 1) > max_mv_r)
     64         {
     65             FILE *f = fopen("maxmv.stt", "a");
     66             max_mv_r = abs(mv->row >> 1);
     67             fprintf(f, "New Mv Row Max %6d\n", (mv->row >> 1));
     68 
     69             if ((abs(mv->row) / 2) != max_mv_r)
     70                 fprintf(f, "MV Row conversion error %6d\n", abs(mv->row) / 2);
     71 
     72             fclose(f);
     73         }
     74 
     75         if (abs(mv->col >> 1) > max_mv_c)
     76         {
     77             FILE *f = fopen("maxmv.stt", "a");
     78             fprintf(f, "New Mv Col Max %6d\n", (mv->col >> 1));
     79             max_mv_c = abs(mv->col >> 1);
     80             fclose(f);
     81         }
     82     }
     83 #endif
     84 
     85   encode_mvcomponent(w, mv->row >> 1, &mvc[0]);
     86   encode_mvcomponent(w, mv->col >> 1, &mvc[1]);
     87 }
     88 
     89 static unsigned int cost_mvcomponent(const int v,
     90                                      const struct mv_context *mvc) {
     91   const vp8_prob *p = mvc->prob;
     92   const int x = v;
     93   unsigned int cost;
     94 
     95   if (x < mvnum_short) {
     96     cost = vp8_cost_zero(p[mvpis_short]) +
     97            vp8_treed_cost(vp8_small_mvtree, p + MVPshort, x, 3);
     98 
     99     if (!x) return cost;
    100   } else {
    101     int i = 0;
    102     cost = vp8_cost_one(p[mvpis_short]);
    103 
    104     do {
    105       cost += vp8_cost_bit(p[MVPbits + i], (x >> i) & 1);
    106 
    107     } while (++i < 3);
    108 
    109     i = mvlong_width - 1; /* Skip bit 3, which is sometimes implicit */
    110 
    111     do {
    112       cost += vp8_cost_bit(p[MVPbits + i], (x >> i) & 1);
    113 
    114     } while (--i > 3);
    115 
    116     if (x & 0xFFF0) cost += vp8_cost_bit(p[MVPbits + 3], (x >> 3) & 1);
    117   }
    118 
    119   return cost; /* + vp8_cost_bit( p [MVPsign], v < 0); */
    120 }
    121 
    122 void vp8_build_component_cost_table(int *mvcost[2], const MV_CONTEXT *mvc,
    123                                     int mvc_flag[2]) {
    124   int i = 1;
    125   unsigned int cost0 = 0;
    126   unsigned int cost1 = 0;
    127 
    128   vpx_clear_system_state();
    129 
    130   i = 1;
    131 
    132   if (mvc_flag[0]) {
    133     mvcost[0][0] = cost_mvcomponent(0, &mvc[0]);
    134 
    135     do {
    136       cost0 = cost_mvcomponent(i, &mvc[0]);
    137 
    138       mvcost[0][i] = cost0 + vp8_cost_zero(mvc[0].prob[MVPsign]);
    139       mvcost[0][-i] = cost0 + vp8_cost_one(mvc[0].prob[MVPsign]);
    140     } while (++i <= mv_max);
    141   }
    142 
    143   i = 1;
    144 
    145   if (mvc_flag[1]) {
    146     mvcost[1][0] = cost_mvcomponent(0, &mvc[1]);
    147 
    148     do {
    149       cost1 = cost_mvcomponent(i, &mvc[1]);
    150 
    151       mvcost[1][i] = cost1 + vp8_cost_zero(mvc[1].prob[MVPsign]);
    152       mvcost[1][-i] = cost1 + vp8_cost_one(mvc[1].prob[MVPsign]);
    153     } while (++i <= mv_max);
    154   }
    155 }
    156 
    157 /* Motion vector probability table update depends on benefit.
    158  * Small correction allows for the fact that an update to an MV probability
    159  * may have benefit in subsequent frames as well as the current one.
    160  */
    161 #define MV_PROB_UPDATE_CORRECTION -1
    162 
    163 static void calc_prob(vp8_prob *p, const unsigned int ct[2]) {
    164   const unsigned int tot = ct[0] + ct[1];
    165 
    166   if (tot) {
    167     const vp8_prob x = ((ct[0] * 255) / tot) & -2;
    168     *p = x ? x : 1;
    169   }
    170 }
    171 
    172 static void update(vp8_writer *const w, const unsigned int ct[2],
    173                    vp8_prob *const cur_p, const vp8_prob new_p,
    174                    const vp8_prob update_p, int *updated) {
    175   const int cur_b = vp8_cost_branch(ct, *cur_p);
    176   const int new_b = vp8_cost_branch(ct, new_p);
    177   const int cost =
    178       7 + MV_PROB_UPDATE_CORRECTION +
    179       ((vp8_cost_one(update_p) - vp8_cost_zero(update_p) + 128) >> 8);
    180 
    181   if (cur_b - new_b > cost) {
    182     *cur_p = new_p;
    183     vp8_write(w, 1, update_p);
    184     vp8_write_literal(w, new_p >> 1, 7);
    185     *updated = 1;
    186 
    187   } else
    188     vp8_write(w, 0, update_p);
    189 }
    190 
    191 static void write_component_probs(vp8_writer *const w,
    192                                   struct mv_context *cur_mvc,
    193                                   const struct mv_context *default_mvc_,
    194                                   const struct mv_context *update_mvc,
    195                                   const unsigned int events[MVvals],
    196                                   unsigned int rc, int *updated) {
    197   vp8_prob *Pcur = cur_mvc->prob;
    198   const vp8_prob *default_mvc = default_mvc_->prob;
    199   const vp8_prob *Pupdate = update_mvc->prob;
    200   unsigned int is_short_ct[2], sign_ct[2];
    201 
    202   unsigned int bit_ct[mvlong_width][2];
    203 
    204   unsigned int short_ct[mvnum_short];
    205   unsigned int short_bct[mvnum_short - 1][2];
    206 
    207   vp8_prob Pnew[MVPcount];
    208 
    209   (void)rc;
    210   vp8_copy_array(Pnew, default_mvc, MVPcount);
    211 
    212   vp8_zero(is_short_ct) vp8_zero(sign_ct) vp8_zero(bit_ct) vp8_zero(short_ct)
    213       vp8_zero(short_bct)
    214 
    215   /* j=0 */
    216   {
    217     const int c = events[mv_max];
    218 
    219     is_short_ct[0] += c; /* Short vector */
    220     short_ct[0] += c;    /* Magnitude distribution */
    221   }
    222 
    223   /* j: 1 ~ mv_max (1023) */
    224   {
    225     int j = 1;
    226 
    227     do {
    228       const int c1 = events[mv_max + j]; /* positive */
    229       const int c2 = events[mv_max - j]; /* negative */
    230       const int c = c1 + c2;
    231       int a = j;
    232 
    233       sign_ct[0] += c1;
    234       sign_ct[1] += c2;
    235 
    236       if (a < mvnum_short) {
    237         is_short_ct[0] += c; /* Short vector */
    238         short_ct[a] += c;    /* Magnitude distribution */
    239       } else {
    240         int k = mvlong_width - 1;
    241         is_short_ct[1] += c; /* Long vector */
    242 
    243         /*  bit 3 not always encoded. */
    244         do {
    245           bit_ct[k][(a >> k) & 1] += c;
    246 
    247         } while (--k >= 0);
    248       }
    249     } while (++j <= mv_max);
    250   }
    251 
    252   calc_prob(Pnew + mvpis_short, is_short_ct);
    253 
    254   calc_prob(Pnew + MVPsign, sign_ct);
    255 
    256   {
    257     vp8_prob p[mvnum_short - 1]; /* actually only need branch ct */
    258     int j = 0;
    259 
    260     vp8_tree_probs_from_distribution(8, vp8_small_mvencodings, vp8_small_mvtree,
    261                                      p, short_bct, short_ct, 256, 1);
    262 
    263     do {
    264       calc_prob(Pnew + MVPshort + j, short_bct[j]);
    265 
    266     } while (++j < mvnum_short - 1);
    267   }
    268 
    269   {
    270     int j = 0;
    271 
    272     do {
    273       calc_prob(Pnew + MVPbits + j, bit_ct[j]);
    274 
    275     } while (++j < mvlong_width);
    276   }
    277 
    278   update(w, is_short_ct, Pcur + mvpis_short, Pnew[mvpis_short], *Pupdate++,
    279          updated);
    280 
    281   update(w, sign_ct, Pcur + MVPsign, Pnew[MVPsign], *Pupdate++, updated);
    282 
    283   {
    284     const vp8_prob *const new_p = Pnew + MVPshort;
    285     vp8_prob *const cur_p = Pcur + MVPshort;
    286 
    287     int j = 0;
    288 
    289     do {
    290       update(w, short_bct[j], cur_p + j, new_p[j], *Pupdate++, updated);
    291 
    292     } while (++j < mvnum_short - 1);
    293   }
    294 
    295   {
    296     const vp8_prob *const new_p = Pnew + MVPbits;
    297     vp8_prob *const cur_p = Pcur + MVPbits;
    298 
    299     int j = 0;
    300 
    301     do {
    302       update(w, bit_ct[j], cur_p + j, new_p[j], *Pupdate++, updated);
    303 
    304     } while (++j < mvlong_width);
    305   }
    306 }
    307 
    308 void vp8_write_mvprobs(VP8_COMP *cpi) {
    309   vp8_writer *const w = cpi->bc;
    310   MV_CONTEXT *mvc = cpi->common.fc.mvc;
    311   int flags[2] = { 0, 0 };
    312 #ifdef VP8_ENTROPY_STATS
    313   active_section = 4;
    314 #endif
    315   write_component_probs(w, &mvc[0], &vp8_default_mv_context[0],
    316                         &vp8_mv_update_probs[0], cpi->mb.MVcount[0], 0,
    317                         &flags[0]);
    318   write_component_probs(w, &mvc[1], &vp8_default_mv_context[1],
    319                         &vp8_mv_update_probs[1], cpi->mb.MVcount[1], 1,
    320                         &flags[1]);
    321 
    322   if (flags[0] || flags[1]) {
    323     vp8_build_component_cost_table(
    324         cpi->mb.mvcost, (const MV_CONTEXT *)cpi->common.fc.mvc, flags);
    325   }
    326 
    327 #ifdef VP8_ENTROPY_STATS
    328   active_section = 5;
    329 #endif
    330 }
    331