blob: 2a74ff4ae3a1313a47b23733850627e4054cf08e [file] [log] [blame]
John Koleszar0ea50ce2010-05-18 11:58:33 -04001/*
John Koleszarc2140b82010-09-09 08:16:39 -04002 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
John Koleszar0ea50ce2010-05-18 11:58:33 -04003 *
John Koleszar94c52e42010-06-18 12:39:21 -04004 * Use of this source code is governed by a BSD-style license
John Koleszar09202d82010-06-04 16:19:40 -04005 * 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
John Koleszar94c52e42010-06-18 12:39:21 -04007 * in the file PATENTS. All contributing project authors may
John Koleszar09202d82010-06-04 16:19:40 -04008 * be found in the AUTHORS file in the root of the source tree.
John Koleszar0ea50ce2010-05-18 11:58:33 -04009 */
10
11
John Koleszar02321de2011-02-10 14:41:38 -050012#include "vp8/common/common.h"
John Koleszar0ea50ce2010-05-18 11:58:33 -040013#include "encodemv.h"
John Koleszar02321de2011-02-10 14:41:38 -050014#include "vp8/common/entropymode.h"
15#include "vp8/common/systemdependent.h"
John Koleszar0ea50ce2010-05-18 11:58:33 -040016
17#include <math.h>
18
Ronald S. Bultje65d22822013-03-18 15:28:51 -070019#ifdef VP8_ENTROPY_STATS
John Koleszar0ea50ce2010-05-18 11:58:33 -040020extern unsigned int active_section;
21#endif
22
23static void encode_mvcomponent(
24 vp8_writer *const w,
25 const int v,
26 const struct mv_context *mvc
27)
28{
29 const vp8_prob *p = mvc->prob;
30 const int x = v < 0 ? -v : v;
31
John Koleszar0164a1c2012-05-21 14:30:56 -070032 if (x < mvnum_short) /* Small */
John Koleszar0ea50ce2010-05-18 11:58:33 -040033 {
34 vp8_write(w, 0, p [mvpis_short]);
35 vp8_treed_write(w, vp8_small_mvtree, p + MVPshort, x, 3);
36
37 if (!x)
John Koleszar0164a1c2012-05-21 14:30:56 -070038 return; /* no sign bit */
John Koleszar0ea50ce2010-05-18 11:58:33 -040039 }
John Koleszar0164a1c2012-05-21 14:30:56 -070040 else /* Large */
John Koleszar0ea50ce2010-05-18 11:58:33 -040041 {
42 int i = 0;
43
44 vp8_write(w, 1, p [mvpis_short]);
45
46 do
47 vp8_write(w, (x >> i) & 1, p [MVPbits + i]);
48
49 while (++i < 3);
50
51 i = mvlong_width - 1; /* Skip bit 3, which is sometimes implicit */
52
53 do
54 vp8_write(w, (x >> i) & 1, p [MVPbits + i]);
55
56 while (--i > 3);
57
58 if (x & 0xFFF0)
59 vp8_write(w, (x >> 3) & 1, p [MVPbits + 3]);
60 }
61
62 vp8_write(w, v < 0, p [MVPsign]);
63}
64#if 0
65static int max_mv_r = 0;
66static int max_mv_c = 0;
67#endif
68void vp8_encode_motion_vector(vp8_writer *w, const MV *mv, const MV_CONTEXT *mvc)
69{
70
71#if 0
72 {
73 if (abs(mv->row >> 1) > max_mv_r)
74 {
75 FILE *f = fopen("maxmv.stt", "a");
76 max_mv_r = abs(mv->row >> 1);
77 fprintf(f, "New Mv Row Max %6d\n", (mv->row >> 1));
78
79 if ((abs(mv->row) / 2) != max_mv_r)
80 fprintf(f, "MV Row conversion error %6d\n", abs(mv->row) / 2);
81
82 fclose(f);
83 }
84
85 if (abs(mv->col >> 1) > max_mv_c)
86 {
87 FILE *f = fopen("maxmv.stt", "a");
88 fprintf(f, "New Mv Col Max %6d\n", (mv->col >> 1));
89 max_mv_c = abs(mv->col >> 1);
90 fclose(f);
91 }
92 }
93#endif
94
95 encode_mvcomponent(w, mv->row >> 1, &mvc[0]);
96 encode_mvcomponent(w, mv->col >> 1, &mvc[1]);
97}
98
99
100static unsigned int cost_mvcomponent(const int v, const struct mv_context *mvc)
101{
102 const vp8_prob *p = mvc->prob;
John Koleszar0164a1c2012-05-21 14:30:56 -0700103 const int x = v;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400104 unsigned int cost;
105
106 if (x < mvnum_short)
107 {
108 cost = vp8_cost_zero(p [mvpis_short])
109 + vp8_treed_cost(vp8_small_mvtree, p + MVPshort, x, 3);
110
111 if (!x)
112 return cost;
113 }
114 else
115 {
116 int i = 0;
117 cost = vp8_cost_one(p [mvpis_short]);
118
119 do
120 cost += vp8_cost_bit(p [MVPbits + i], (x >> i) & 1);
121
122 while (++i < 3);
123
124 i = mvlong_width - 1; /* Skip bit 3, which is sometimes implicit */
125
126 do
127 cost += vp8_cost_bit(p [MVPbits + i], (x >> i) & 1);
128
129 while (--i > 3);
130
John Koleszar9954d052011-02-09 12:50:17 -0500131 if (x & 0xFFF0)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400132 cost += vp8_cost_bit(p [MVPbits + 3], (x >> 3) & 1);
133 }
134
John Koleszar0164a1c2012-05-21 14:30:56 -0700135 return cost; /* + vp8_cost_bit( p [MVPsign], v < 0); */
John Koleszar0ea50ce2010-05-18 11:58:33 -0400136}
John Koleszar0ea50ce2010-05-18 11:58:33 -0400137
Yunqing Wang3d681582011-04-01 16:41:58 -0400138void vp8_build_component_cost_table(int *mvcost[2], const MV_CONTEXT *mvc, int mvc_flag[2])
John Koleszar0ea50ce2010-05-18 11:58:33 -0400139{
John Koleszar0164a1c2012-05-21 14:30:56 -0700140 int i = 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400141 unsigned int cost0 = 0;
142 unsigned int cost1 = 0;
143
144 vp8_clear_system_state();
John Koleszar0ea50ce2010-05-18 11:58:33 -0400145
146 i = 1;
147
148 if (mvc_flag[0])
149 {
150 mvcost [0] [0] = cost_mvcomponent(0, &mvc[0]);
151
152 do
153 {
John Koleszar0ea50ce2010-05-18 11:58:33 -0400154 cost0 = cost_mvcomponent(i, &mvc[0]);
155
156 mvcost [0] [i] = cost0 + vp8_cost_zero(mvc[0].prob[MVPsign]);
157 mvcost [0] [-i] = cost0 + vp8_cost_one(mvc[0].prob[MVPsign]);
158 }
159 while (++i <= mv_max);
160 }
161
162 i = 1;
163
164 if (mvc_flag[1])
165 {
166 mvcost [1] [0] = cost_mvcomponent(0, &mvc[1]);
167
168 do
169 {
John Koleszar0ea50ce2010-05-18 11:58:33 -0400170 cost1 = cost_mvcomponent(i, &mvc[1]);
171
172 mvcost [1] [i] = cost1 + vp8_cost_zero(mvc[1].prob[MVPsign]);
173 mvcost [1] [-i] = cost1 + vp8_cost_one(mvc[1].prob[MVPsign]);
174 }
175 while (++i <= mv_max);
176 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400177}
178
179
John Koleszar0164a1c2012-05-21 14:30:56 -0700180/* Motion vector probability table update depends on benefit.
181 * Small correction allows for the fact that an update to an MV probability
182 * may have benefit in subsequent frames as well as the current one.
183 */
John Koleszar0ea50ce2010-05-18 11:58:33 -0400184#define MV_PROB_UPDATE_CORRECTION -1
185
186
James Berry451ab0c2012-03-21 14:11:10 -0400187static void calc_prob(vp8_prob *p, const unsigned int ct[2])
John Koleszar0ea50ce2010-05-18 11:58:33 -0400188{
189 const unsigned int tot = ct[0] + ct[1];
190
191 if (tot)
192 {
193 const vp8_prob x = ((ct[0] * 255) / tot) & -2;
194 *p = x ? x : 1;
195 }
196}
197
198static void update(
199 vp8_writer *const w,
200 const unsigned int ct[2],
201 vp8_prob *const cur_p,
202 const vp8_prob new_p,
203 const vp8_prob update_p,
204 int *updated
205)
206{
207 const int cur_b = vp8_cost_branch(ct, *cur_p);
208 const int new_b = vp8_cost_branch(ct, new_p);
209 const int cost = 7 + MV_PROB_UPDATE_CORRECTION + ((vp8_cost_one(update_p) - vp8_cost_zero(update_p) + 128) >> 8);
210
211 if (cur_b - new_b > cost)
212 {
213 *cur_p = new_p;
214 vp8_write(w, 1, update_p);
215 vp8_write_literal(w, new_p >> 1, 7);
216 *updated = 1;
217
218 }
219 else
220 vp8_write(w, 0, update_p);
221}
222
223static void write_component_probs(
224 vp8_writer *const w,
225 struct mv_context *cur_mvc,
226 const struct mv_context *default_mvc_,
John Koleszar94c52e42010-06-18 12:39:21 -0400227 const struct mv_context *update_mvc,
John Koleszar0ea50ce2010-05-18 11:58:33 -0400228 const unsigned int events [MVvals],
229 unsigned int rc,
230 int *updated
231)
232{
233 vp8_prob *Pcur = cur_mvc->prob;
234 const vp8_prob *default_mvc = default_mvc_->prob;
235 const vp8_prob *Pupdate = update_mvc->prob;
236 unsigned int is_short_ct[2], sign_ct[2];
237
238 unsigned int bit_ct [mvlong_width] [2];
239
240 unsigned int short_ct [mvnum_short];
241 unsigned int short_bct [mvnum_short-1] [2];
242
243 vp8_prob Pnew [MVPcount];
244
245 (void) rc;
246 vp8_copy_array(Pnew, default_mvc, MVPcount);
247
248 vp8_zero(is_short_ct)
249 vp8_zero(sign_ct)
250 vp8_zero(bit_ct)
251 vp8_zero(short_ct)
252 vp8_zero(short_bct)
253
254
John Koleszar0164a1c2012-05-21 14:30:56 -0700255 /* j=0 */
John Koleszar0ea50ce2010-05-18 11:58:33 -0400256 {
John Koleszar0ea50ce2010-05-18 11:58:33 -0400257 const int c = events [mv_max];
258
John Koleszar0164a1c2012-05-21 14:30:56 -0700259 is_short_ct [0] += c; /* Short vector */
260 short_ct [0] += c; /* Magnitude distribution */
John Koleszar0ea50ce2010-05-18 11:58:33 -0400261 }
262
John Koleszar0164a1c2012-05-21 14:30:56 -0700263 /* j: 1 ~ mv_max (1023) */
John Koleszar0ea50ce2010-05-18 11:58:33 -0400264 {
265 int j = 1;
266
267 do
268 {
John Koleszar0164a1c2012-05-21 14:30:56 -0700269 const int c1 = events [mv_max + j]; /* positive */
270 const int c2 = events [mv_max - j]; /* negative */
John Koleszar0ea50ce2010-05-18 11:58:33 -0400271 const int c = c1 + c2;
272 int a = j;
273
274 sign_ct [0] += c1;
275 sign_ct [1] += c2;
276
277 if (a < mvnum_short)
278 {
John Koleszar0164a1c2012-05-21 14:30:56 -0700279 is_short_ct [0] += c; /* Short vector */
280 short_ct [a] += c; /* Magnitude distribution */
John Koleszar0ea50ce2010-05-18 11:58:33 -0400281 }
282 else
283 {
284 int k = mvlong_width - 1;
John Koleszar0164a1c2012-05-21 14:30:56 -0700285 is_short_ct [1] += c; /* Long vector */
John Koleszar0ea50ce2010-05-18 11:58:33 -0400286
287 /* bit 3 not always encoded. */
288 do
289 bit_ct [k] [(a >> k) & 1] += c;
290
291 while (--k >= 0);
292 }
293 }
294 while (++j <= mv_max);
295 }
296
John Koleszar0ea50ce2010-05-18 11:58:33 -0400297 calc_prob(Pnew + mvpis_short, is_short_ct);
298
299 calc_prob(Pnew + MVPsign, sign_ct);
300
301 {
302 vp8_prob p [mvnum_short - 1]; /* actually only need branch ct */
303 int j = 0;
304
305 vp8_tree_probs_from_distribution(
306 8, vp8_small_mvencodings, vp8_small_mvtree,
307 p, short_bct, short_ct,
308 256, 1
309 );
310
311 do
312 calc_prob(Pnew + MVPshort + j, short_bct[j]);
313
314 while (++j < mvnum_short - 1);
315 }
316
317 {
318 int j = 0;
319
320 do
321 calc_prob(Pnew + MVPbits + j, bit_ct[j]);
322
323 while (++j < mvlong_width);
324 }
325
326 update(w, is_short_ct, Pcur + mvpis_short, Pnew[mvpis_short], *Pupdate++, updated);
327
328 update(w, sign_ct, Pcur + MVPsign, Pnew[MVPsign], *Pupdate++, updated);
329
330 {
331 const vp8_prob *const new_p = Pnew + MVPshort;
332 vp8_prob *const cur_p = Pcur + MVPshort;
333
334 int j = 0;
335
336 do
337
338 update(w, short_bct[j], cur_p + j, new_p[j], *Pupdate++, updated);
339
340 while (++j < mvnum_short - 1);
341 }
342
343 {
344 const vp8_prob *const new_p = Pnew + MVPbits;
345 vp8_prob *const cur_p = Pcur + MVPbits;
346
347 int j = 0;
348
349 do
350
351 update(w, bit_ct[j], cur_p + j, new_p[j], *Pupdate++, updated);
352
353 while (++j < mvlong_width);
354 }
355}
356
357void vp8_write_mvprobs(VP8_COMP *cpi)
358{
Attila Nagy97259b42011-11-10 13:07:37 +0200359 vp8_writer *const w = cpi->bc;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400360 MV_CONTEXT *mvc = cpi->common.fc.mvc;
361 int flags[2] = {0, 0};
Ronald S. Bultje65d22822013-03-18 15:28:51 -0700362#ifdef VP8_ENTROPY_STATS
John Koleszar0ea50ce2010-05-18 11:58:33 -0400363 active_section = 4;
364#endif
365 write_component_probs(
Scott LaVarnway01824d12012-11-05 12:41:24 -0800366 w, &mvc[0], &vp8_default_mv_context[0], &vp8_mv_update_probs[0],
367 cpi->mb.MVcount[0], 0, &flags[0]
John Koleszar0ea50ce2010-05-18 11:58:33 -0400368 );
369 write_component_probs(
Scott LaVarnway01824d12012-11-05 12:41:24 -0800370 w, &mvc[1], &vp8_default_mv_context[1], &vp8_mv_update_probs[1],
371 cpi->mb.MVcount[1], 1, &flags[1]
John Koleszar0ea50ce2010-05-18 11:58:33 -0400372 );
373
374 if (flags[0] || flags[1])
Yunqing Wang3d681582011-04-01 16:41:58 -0400375 vp8_build_component_cost_table(cpi->mb.mvcost, (const MV_CONTEXT *) cpi->common.fc.mvc, flags);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400376
Ronald S. Bultje65d22822013-03-18 15:28:51 -0700377#ifdef VP8_ENTROPY_STATS
John Koleszar0ea50ce2010-05-18 11:58:33 -0400378 active_section = 5;
379#endif
380}