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 #define INLINE __inline
14 
15 #include <stdint.h>
16 #include <stdlib.h>
17 #include "assert.h"
18 #include "vp9_treewriter.h"
19 #include "vp9_cost.h"
20 #include "vp9_encodemv.h"
21 #include "vpx_dsp_common.h"
22 
23 static struct vp9_token mv_joint_encodings[MV_JOINTS];
24 static struct vp9_token mv_class_encodings[MV_CLASSES];
25 static struct vp9_token mv_fp_encodings[MV_FP_SIZE];
26 
eb_vp9_entropy_mv_init(void)27 void eb_vp9_entropy_mv_init(void) {
28   eb_vp9_tokens_from_tree(mv_joint_encodings, eb_vp9_mv_joint_tree);
29   eb_vp9_tokens_from_tree(mv_class_encodings, eb_vp9_mv_class_tree);
30   eb_vp9_tokens_from_tree(mv_fp_encodings, eb_vp9_mv_fp_tree);
31 }
32 
encode_mv_component(VpxWriter * w,int comp,const nmv_component * mvcomp,int usehp)33 static void encode_mv_component(VpxWriter *w, int comp,
34                                 const nmv_component *mvcomp, int usehp) {
35   int offset;
36   const int sign = comp < 0;
37   const int mag = sign ? -comp : comp;
38   const int mv_class = eb_vp9_get_mv_class(mag - 1, &offset);
39   const int d = offset >> 3;         // int mv data
40   const int fr = (offset >> 1) & 3;  // fractional mv data
41   const int hp = offset & 1;         // high precision mv data
42 
43   assert(comp != 0);
44 
45   // Sign
46   vpx_write(w, sign, mvcomp->sign);
47 
48   // Class
49   vp9_write_token(w, eb_vp9_mv_class_tree, mvcomp->classes,
50                   &mv_class_encodings[mv_class]);
51 
52   // Integer bits
53   if (mv_class == MV_CLASS_0) {
54     vpx_write(w, d, mvcomp->class0[0]);
55   } else {
56     int i;
57     const int n = mv_class + CLASS0_BITS - 1;  // number of bits
58     for (i = 0; i < n; ++i) vpx_write(w, (d >> i) & 1, mvcomp->bits[i]);
59   }
60 
61   // Fractional bits
62   vp9_write_token(w, eb_vp9_mv_fp_tree,
63                   mv_class == MV_CLASS_0 ? mvcomp->class0_fp[d] : mvcomp->fp,
64                   &mv_fp_encodings[fr]);
65 
66   // High precision bit
67   if (usehp)
68     vpx_write(w, hp, mv_class == MV_CLASS_0 ? mvcomp->class0_hp : mvcomp->hp);
69 }
70 
build_nmv_component_cost_table(int * mvcost,const nmv_component * const mvcomp,int usehp)71 static void build_nmv_component_cost_table(int *mvcost,
72                                            const nmv_component *const mvcomp,
73                                            int usehp) {
74   int sign_cost[2], class_cost[MV_CLASSES], class0_cost[CLASS0_SIZE];
75   int bits_cost[MV_OFFSET_BITS][2];
76   int class0_fp_cost[CLASS0_SIZE][MV_FP_SIZE], fp_cost[MV_FP_SIZE];
77   int class0_hp_cost[2], hp_cost[2];
78   int i;
79   int c, o;
80 
81   sign_cost[0] = vp9_cost_zero(mvcomp->sign);
82   assert(mvcomp->sign > 0);
83   sign_cost[1] = vp9_cost_one(mvcomp->sign);
84   eb_vp9_cost_tokens(class_cost, mvcomp->classes, eb_vp9_mv_class_tree);
85   eb_vp9_cost_tokens(class0_cost, mvcomp->class0, eb_vp9_mv_class0_tree);
86   for (i = 0; i < MV_OFFSET_BITS; ++i) {
87     bits_cost[i][0] = vp9_cost_zero(mvcomp->bits[i]);
88     bits_cost[i][1] = vp9_cost_one(mvcomp->bits[i]);
89   }
90 
91   for (i = 0; i < CLASS0_SIZE; ++i)
92     eb_vp9_cost_tokens(class0_fp_cost[i], mvcomp->class0_fp[i], eb_vp9_mv_fp_tree);
93   eb_vp9_cost_tokens(fp_cost, mvcomp->fp, eb_vp9_mv_fp_tree);
94 
95   // Always build the hp costs to avoid an uninitialized warning from gcc
96   class0_hp_cost[0] = vp9_cost_zero(mvcomp->class0_hp);
97   assert(mvcomp->class0_hp > 0);
98   class0_hp_cost[1] = vp9_cost_one(mvcomp->class0_hp);
99   hp_cost[0] = vp9_cost_zero(mvcomp->hp);
100   assert(mvcomp->hp > 0);
101   hp_cost[1] = vp9_cost_one(mvcomp->hp);
102 
103   mvcost[0] = 0;
104   // MV_CLASS_0
105   for (o = 0; o < (CLASS0_SIZE << 3); ++o) {
106     int d, e, f;
107     int cost = class_cost[MV_CLASS_0];
108     int v = o + 1;
109     d = (o >> 3);     /* int mv data */
110     f = (o >> 1) & 3; /* fractional pel mv data */
111     cost += class0_cost[d];
112     cost += class0_fp_cost[d][f];
113     if (usehp) {
114       e = (o & 1); /* high precision mv data */
115       cost += class0_hp_cost[e];
116     }
117     mvcost[v] = cost + sign_cost[0];
118     mvcost[-v] = cost + sign_cost[1];
119   }
120   for (c = MV_CLASS_1; c < MV_CLASSES; ++c) {
121     int d;
122     for (d = 0; d < (1 << c); ++d) {
123       int f;
124       int whole_cost = class_cost[c];
125       int b = c + CLASS0_BITS - 1; /* number of bits */
126       for (i = 0; i < b; ++i) whole_cost += bits_cost[i][((d >> i) & 1)];
127       for (f = 0; f < 4; ++f) {
128         int cost = whole_cost + fp_cost[f];
129         int v = (CLASS0_SIZE << (c + 2)) + d * 8 + f * 2 /* + e */ + 1;
130         if (usehp) {
131           mvcost[v] = cost + hp_cost[0] + sign_cost[0];
132           mvcost[-v] = cost + hp_cost[0] + sign_cost[1];
133           if (v + 1 > MV_MAX) break;
134           mvcost[v + 1] = cost + hp_cost[1] + sign_cost[0];
135           mvcost[-v - 1] = cost + hp_cost[1] + sign_cost[1];
136         } else {
137           mvcost[v] = cost + sign_cost[0];
138           mvcost[-v] = cost + sign_cost[1];
139           if (v + 1 > MV_MAX) break;
140           mvcost[v + 1] = cost + sign_cost[0];
141           mvcost[-v - 1] = cost + sign_cost[1];
142         }
143       }
144     }
145   }
146 }
147 
update_mv(VpxWriter * w,const unsigned int ct[2],vpx_prob * cur_p,vpx_prob upd_p)148 static int update_mv(VpxWriter *w, const unsigned int ct[2], vpx_prob *cur_p,
149                      vpx_prob upd_p) {
150   const vpx_prob new_p = get_binary_prob(ct[0], ct[1]) | 1;
151   assert(upd_p > 0);
152   const int update = cost_branch256(ct, *cur_p) + vp9_cost_zero(upd_p) >
153                      cost_branch256(ct, new_p) + vp9_cost_one(upd_p) +
154                          (7 << VP9_PROB_COST_SHIFT);
155   vpx_write(w, update, upd_p);
156   if (update) {
157     *cur_p = new_p;
158     vpx_write_literal(w, new_p >> 1, 7);
159   }
160   return update;
161 }
162 
write_mv_update(const vpx_tree_index * tree,vpx_prob probs[],const unsigned int counts[],int n,VpxWriter * w)163 static void write_mv_update(const vpx_tree_index *tree,
164                             vpx_prob probs[/*n - 1*/],
165                             const unsigned int counts[/*n - 1*/], int n,
166                             VpxWriter *w) {
167   int i;
168   unsigned int branch_ct[32][2];
169 
170   // Assuming max number of probabilities <= 32
171   assert(n <= 32);
172 
173   eb_vp9_tree_probs_from_distribution(tree, branch_ct, counts);
174   for (i = 0; i < n - 1; ++i)
175     update_mv(w, branch_ct[i], &probs[i], MV_UPDATE_PROB);
176 }
177 
eb_vp9_write_nmv_probs(VP9_COMMON * cm,int usehp,VpxWriter * w,nmv_context_counts * const counts)178 void eb_vp9_write_nmv_probs(VP9_COMMON *cm, int usehp, VpxWriter *w,
179                          nmv_context_counts *const counts) {
180   int i, j;
181   nmv_context *const mvc = &cm->fc->nmvc;
182 
183   write_mv_update(eb_vp9_mv_joint_tree, mvc->joints, counts->joints, MV_JOINTS, w);
184 
185   for (i = 0; i < 2; ++i) {
186     nmv_component *comp = &mvc->comps[i];
187     nmv_component_counts *comp_counts = &counts->comps[i];
188 
189     update_mv(w, comp_counts->sign, &comp->sign, MV_UPDATE_PROB);
190     write_mv_update(eb_vp9_mv_class_tree, comp->classes, comp_counts->classes,
191                     MV_CLASSES, w);
192     write_mv_update(eb_vp9_mv_class0_tree, comp->class0, comp_counts->class0,
193                     CLASS0_SIZE, w);
194     for (j = 0; j < MV_OFFSET_BITS; ++j)
195       update_mv(w, comp_counts->bits[j], &comp->bits[j], MV_UPDATE_PROB);
196   }
197 
198   for (i = 0; i < 2; ++i) {
199     for (j = 0; j < CLASS0_SIZE; ++j)
200       write_mv_update(eb_vp9_mv_fp_tree, mvc->comps[i].class0_fp[j],
201                       counts->comps[i].class0_fp[j], MV_FP_SIZE, w);
202 
203     write_mv_update(eb_vp9_mv_fp_tree, mvc->comps[i].fp, counts->comps[i].fp,
204                     MV_FP_SIZE, w);
205   }
206 
207   if (usehp) {
208     for (i = 0; i < 2; ++i) {
209       update_mv(w, counts->comps[i].class0_hp, &mvc->comps[i].class0_hp,
210                 MV_UPDATE_PROB);
211       update_mv(w, counts->comps[i].hp, &mvc->comps[i].hp, MV_UPDATE_PROB);
212     }
213   }
214 }
215 
eb_vp9_encode_mv(VP9_COMP * cpi,VpxWriter * w,const MV * mv,const MV * ref,const nmv_context * mvctx,int usehp,unsigned int * const max_mv_magnitude)216 void eb_vp9_encode_mv(VP9_COMP *cpi, VpxWriter *w, const MV *mv, const MV *ref,
217                    const nmv_context *mvctx, int usehp,
218                    unsigned int *const max_mv_magnitude) {
219   const MV diff = { mv->row - ref->row, mv->col - ref->col };
220   const MV_JOINT_TYPE j = vp9_get_mv_joint(&diff);
221   usehp = usehp && use_mv_hp(ref);
222 
223   vp9_write_token(w, eb_vp9_mv_joint_tree, mvctx->joints, &mv_joint_encodings[j]);
224   if (mv_joint_vertical(j))
225     encode_mv_component(w, diff.row, &mvctx->comps[0], usehp);
226 
227   if (mv_joint_horizontal(j))
228     encode_mv_component(w, diff.col, &mvctx->comps[1], usehp);
229 
230   // If auto_mv_step_size is enabled then keep track of the largest
231   // motion vector component used.
232   if (cpi->sf.mv.auto_mv_step_size) {
233     const unsigned int maxv = VPXMAX(abs(mv->row), abs(mv->col)) >> 3;
234     *max_mv_magnitude = VPXMAX(maxv, *max_mv_magnitude);
235   }
236 }
237 
eb_vp9_build_nmv_cost_table(int * mvjoint,int * mvcost[2],const nmv_context * ctx,int usehp)238 void eb_vp9_build_nmv_cost_table(int *mvjoint, int *mvcost[2],
239                               const nmv_context *ctx, int usehp) {
240   eb_vp9_cost_tokens(mvjoint, ctx->joints, eb_vp9_mv_joint_tree);
241   build_nmv_component_cost_table(mvcost[0], &ctx->comps[0], usehp);
242   build_nmv_component_cost_table(mvcost[1], &ctx->comps[1], usehp);
243 }
244 
245 #if 0
246 void vp9_update_mv_count(ThreadData *td) {
247   const MACROBLOCKD *xd = &td->mb.e_mbd;
248   const ModeInfo *mi = xd->mi[0];
249   const MbModeInfoExt *mbmi_ext = td->mb.mbmi_ext;
250 
251   if (mi->sb_type < BLOCK_8X8) {
252     const int num_4x4_w = eb_vp9_num_4x4_blocks_wide_lookup[mi->sb_type];
253     const int num_4x4_h = eb_vp9_num_4x4_blocks_high_lookup[mi->sb_type];
254     int idx, idy;
255 
256     for (idy = 0; idy < 2; idy += num_4x4_h) {
257       for (idx = 0; idx < 2; idx += num_4x4_w) {
258         const int i = idy * 2 + idx;
259         if (mi->bmi[i].as_mode == NEWMV)
260           inc_mvs(mi, mbmi_ext, mi->bmi[i].as_mv, &td->counts->mv);
261       }
262     }
263   } else {
264     if (mi->mode == NEWMV) inc_mvs(mi, mbmi_ext, mi->mv, &td->counts->mv);
265   }
266 }
267 #endif
268