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