Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(158)

Side by Side Diff: source/libvpx/vp9/common/vp9_entropymv.c

Issue 11555023: libvpx: Add VP9 decoder. (Closed) Base URL: svn://chrome-svn/chrome/trunk/deps/third_party/libvpx/
Patch Set: Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
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
12 #include "vp9/common/vp9_onyxc_int.h"
13 #include "vp9/common/vp9_entropymv.h"
14
15 //#define MV_COUNT_TESTING
16
17 #define MV_COUNT_SAT 16
18 #define MV_MAX_UPDATE_FACTOR 160
19
20 #if CONFIG_NEW_MVREF
21 /* Integer pel reference mv threshold for use of high-precision 1/8 mv */
22 #define COMPANDED_MVREF_THRESH 1000000
23 #else
24 /* Integer pel reference mv threshold for use of high-precision 1/8 mv */
25 #define COMPANDED_MVREF_THRESH 8
26 #endif
27
28 /* Smooth or bias the mv-counts before prob computation */
29 /* #define SMOOTH_MV_COUNTS */
30
31 const vp9_tree_index vp9_mv_joint_tree[2 * MV_JOINTS - 2] = {
32 -MV_JOINT_ZERO, 2,
33 -MV_JOINT_HNZVZ, 4,
34 -MV_JOINT_HZVNZ, -MV_JOINT_HNZVNZ
35 };
36 struct vp9_token_struct vp9_mv_joint_encodings[MV_JOINTS];
37
38 const vp9_tree_index vp9_mv_class_tree[2 * MV_CLASSES - 2] = {
39 -MV_CLASS_0, 2,
40 -MV_CLASS_1, 4,
41 6, 8,
42 -MV_CLASS_2, -MV_CLASS_3,
43 10, 12,
44 -MV_CLASS_4, -MV_CLASS_5,
45 -MV_CLASS_6, -MV_CLASS_7,
46 };
47 struct vp9_token_struct vp9_mv_class_encodings[MV_CLASSES];
48
49 const vp9_tree_index vp9_mv_class0_tree [2 * CLASS0_SIZE - 2] = {
50 -0, -1,
51 };
52 struct vp9_token_struct vp9_mv_class0_encodings[CLASS0_SIZE];
53
54 const vp9_tree_index vp9_mv_fp_tree [2 * 4 - 2] = {
55 -0, 2,
56 -1, 4,
57 -2, -3
58 };
59 struct vp9_token_struct vp9_mv_fp_encodings[4];
60
61 const nmv_context vp9_default_nmv_context = {
62 {32, 64, 96},
63 {
64 { /* vert component */
65 128, /* sign */
66 {224, 144, 192, 168, 192, 176, 192}, /* class */
67 {216}, /* class0 */
68 {136, 140, 148, 160, 176, 192, 224}, /* bits */
69 {{128, 128, 64}, {96, 112, 64}}, /* class0_fp */
70 {64, 96, 64}, /* fp */
71 160, /* class0_hp bit */
72 128, /* hp */
73 },
74 { /* hor component */
75 128, /* sign */
76 {216, 128, 176, 160, 176, 176, 192}, /* class */
77 {208}, /* class0 */
78 {136, 140, 148, 160, 176, 192, 224}, /* bits */
79 {{128, 128, 64}, {96, 112, 64}}, /* class0_fp */
80 {64, 96, 64}, /* fp */
81 160, /* class0_hp bit */
82 128, /* hp */
83 }
84 },
85 };
86
87 MV_JOINT_TYPE vp9_get_mv_joint(MV mv) {
88 if (mv.row == 0 && mv.col == 0) return MV_JOINT_ZERO;
89 else if (mv.row == 0 && mv.col != 0) return MV_JOINT_HNZVZ;
90 else if (mv.row != 0 && mv.col == 0) return MV_JOINT_HZVNZ;
91 else return MV_JOINT_HNZVNZ;
92 }
93
94 #define mv_class_base(c) ((c) ? (CLASS0_SIZE << (c + 2)) : 0)
95
96 MV_CLASS_TYPE vp9_get_mv_class(int z, int *offset) {
97 MV_CLASS_TYPE c;
98 if (z < CLASS0_SIZE * 8) c = MV_CLASS_0;
99 else if (z < CLASS0_SIZE * 16) c = MV_CLASS_1;
100 else if (z < CLASS0_SIZE * 32) c = MV_CLASS_2;
101 else if (z < CLASS0_SIZE * 64) c = MV_CLASS_3;
102 else if (z < CLASS0_SIZE * 128) c = MV_CLASS_4;
103 else if (z < CLASS0_SIZE * 256) c = MV_CLASS_5;
104 else if (z < CLASS0_SIZE * 512) c = MV_CLASS_6;
105 else if (z < CLASS0_SIZE * 1024) c = MV_CLASS_7;
106 else assert(0);
107 if (offset)
108 *offset = z - mv_class_base(c);
109 return c;
110 }
111
112 int vp9_use_nmv_hp(const MV *ref) {
113 if ((abs(ref->row) >> 3) < COMPANDED_MVREF_THRESH &&
114 (abs(ref->col) >> 3) < COMPANDED_MVREF_THRESH)
115 return 1;
116 else
117 return 0;
118 }
119
120 int vp9_get_mv_mag(MV_CLASS_TYPE c, int offset) {
121 return mv_class_base(c) + offset;
122 }
123
124 static void increment_nmv_component_count(int v,
125 nmv_component_counts *mvcomp,
126 int incr,
127 int usehp) {
128 assert (v != 0); /* should not be zero */
129 mvcomp->mvcount[MV_MAX + v] += incr;
130 }
131
132 static void increment_nmv_component(int v,
133 nmv_component_counts *mvcomp,
134 int incr,
135 int usehp) {
136 int s, z, c, o, d, e, f;
137 assert (v != 0); /* should not be zero */
138 s = v < 0;
139 mvcomp->sign[s] += incr;
140 z = (s ? -v : v) - 1; /* magnitude - 1 */
141
142 c = vp9_get_mv_class(z, &o);
143 mvcomp->classes[c] += incr;
144
145 d = (o >> 3); /* int mv data */
146 f = (o >> 1) & 3; /* fractional pel mv data */
147 e = (o & 1); /* high precision mv data */
148 if (c == MV_CLASS_0) {
149 mvcomp->class0[d] += incr;
150 } else {
151 int i, b;
152 b = c + CLASS0_BITS - 1; /* number of bits */
153 for (i = 0; i < b; ++i)
154 mvcomp->bits[i][((d >> i) & 1)] += incr;
155 }
156
157 /* Code the fractional pel bits */
158 if (c == MV_CLASS_0) {
159 mvcomp->class0_fp[d][f] += incr;
160 } else {
161 mvcomp->fp[f] += incr;
162 }
163
164 /* Code the high precision bit */
165 if (usehp) {
166 if (c == MV_CLASS_0) {
167 mvcomp->class0_hp[e] += incr;
168 } else {
169 mvcomp->hp[e] += incr;
170 }
171 }
172 }
173
174 #ifdef SMOOTH_MV_COUNTS
175 static void smooth_counts(nmv_component_counts *mvcomp) {
176 static const int flen = 3; // (filter_length + 1) / 2
177 static const int fval[] = {8, 3, 1};
178 static const int fvalbits = 4;
179 int i;
180 unsigned int smvcount[MV_VALS];
181 vpx_memcpy(smvcount, mvcomp->mvcount, sizeof(smvcount));
182 smvcount[MV_MAX] = (smvcount[MV_MAX - 1] + smvcount[MV_MAX + 1]) >> 1;
183 for (i = flen - 1; i <= MV_VALS - flen; ++i) {
184 int j, s = smvcount[i] * fval[0];
185 for (j = 1; j < flen; ++j)
186 s += (smvcount[i - j] + smvcount[i + j]) * fval[j];
187 mvcomp->mvcount[i] = (s + (1 << (fvalbits - 1))) >> fvalbits;
188 }
189 }
190 #endif
191
192 static void counts_to_context(nmv_component_counts *mvcomp, int usehp) {
193 int v;
194 vpx_memset(mvcomp->sign, 0, sizeof(nmv_component_counts) - sizeof(mvcomp->mvco unt));
195 for (v = 1; v <= MV_MAX; v++) {
196 increment_nmv_component(-v, mvcomp, mvcomp->mvcount[MV_MAX - v], usehp);
197 increment_nmv_component( v, mvcomp, mvcomp->mvcount[MV_MAX + v], usehp);
198 }
199 }
200
201 void vp9_increment_nmv(const MV *mv, const MV *ref, nmv_context_counts *mvctx,
202 int usehp) {
203 MV_JOINT_TYPE j = vp9_get_mv_joint(*mv);
204 mvctx->joints[j]++;
205 usehp = usehp && vp9_use_nmv_hp(ref);
206 if (j == MV_JOINT_HZVNZ || j == MV_JOINT_HNZVNZ) {
207 increment_nmv_component_count(mv->row, &mvctx->comps[0], 1, usehp);
208 }
209 if (j == MV_JOINT_HNZVZ || j == MV_JOINT_HNZVNZ) {
210 increment_nmv_component_count(mv->col, &mvctx->comps[1], 1, usehp);
211 }
212 }
213
214 static void adapt_prob(vp9_prob *dest, vp9_prob prep, vp9_prob newp,
215 unsigned int ct[2]) {
216 int factor;
217 int prob;
218 int count = ct[0] + ct[1];
219 if (count) {
220 count = count > MV_COUNT_SAT ? MV_COUNT_SAT : count;
221 factor = (MV_MAX_UPDATE_FACTOR * count / MV_COUNT_SAT);
222 prob = ((int)prep * (256 - factor) + (int)(newp) * factor + 128) >> 8;
223 prob += !prob;
224 prob = (prob > 255 ? 255 : prob);
225 *dest = prob;
226 }
227 }
228
229 void vp9_counts_process(nmv_context_counts *NMVcount, int usehp) {
230 counts_to_context(&NMVcount->comps[0], usehp);
231 counts_to_context(&NMVcount->comps[1], usehp);
232 }
233
234 void vp9_counts_to_nmv_context(
235 nmv_context_counts *NMVcount,
236 nmv_context *prob,
237 int usehp,
238 unsigned int (*branch_ct_joint)[2],
239 unsigned int (*branch_ct_sign)[2],
240 unsigned int (*branch_ct_classes)[MV_CLASSES - 1][2],
241 unsigned int (*branch_ct_class0)[CLASS0_SIZE - 1][2],
242 unsigned int (*branch_ct_bits)[MV_OFFSET_BITS][2],
243 unsigned int (*branch_ct_class0_fp)[CLASS0_SIZE][4 - 1][2],
244 unsigned int (*branch_ct_fp)[4 - 1][2],
245 unsigned int (*branch_ct_class0_hp)[2],
246 unsigned int (*branch_ct_hp)[2]) {
247 int i, j, k;
248 vp9_counts_process(NMVcount, usehp);
249 vp9_tree_probs_from_distribution(MV_JOINTS,
250 vp9_mv_joint_encodings,
251 vp9_mv_joint_tree,
252 prob->joints,
253 branch_ct_joint,
254 NMVcount->joints,
255 256, 1);
256 for (i = 0; i < 2; ++i) {
257 prob->comps[i].sign =
258 vp9_bin_prob_from_distribution(NMVcount->comps[i].sign);
259 branch_ct_sign[i][0] = NMVcount->comps[i].sign[0];
260 branch_ct_sign[i][1] = NMVcount->comps[i].sign[1];
261 vp9_tree_probs_from_distribution(MV_CLASSES,
262 vp9_mv_class_encodings,
263 vp9_mv_class_tree,
264 prob->comps[i].classes,
265 branch_ct_classes[i],
266 NMVcount->comps[i].classes,
267 256, 1);
268 vp9_tree_probs_from_distribution(CLASS0_SIZE,
269 vp9_mv_class0_encodings,
270 vp9_mv_class0_tree,
271 prob->comps[i].class0,
272 branch_ct_class0[i],
273 NMVcount->comps[i].class0,
274 256, 1);
275 for (j = 0; j < MV_OFFSET_BITS; ++j) {
276 prob->comps[i].bits[j] = vp9_bin_prob_from_distribution(
277 NMVcount->comps[i].bits[j]);
278 branch_ct_bits[i][j][0] = NMVcount->comps[i].bits[j][0];
279 branch_ct_bits[i][j][1] = NMVcount->comps[i].bits[j][1];
280 }
281 }
282 for (i = 0; i < 2; ++i) {
283 for (k = 0; k < CLASS0_SIZE; ++k) {
284 vp9_tree_probs_from_distribution(4,
285 vp9_mv_fp_encodings,
286 vp9_mv_fp_tree,
287 prob->comps[i].class0_fp[k],
288 branch_ct_class0_fp[i][k],
289 NMVcount->comps[i].class0_fp[k],
290 256, 1);
291 }
292 vp9_tree_probs_from_distribution(4,
293 vp9_mv_fp_encodings,
294 vp9_mv_fp_tree,
295 prob->comps[i].fp,
296 branch_ct_fp[i],
297 NMVcount->comps[i].fp,
298 256, 1);
299 }
300 if (usehp) {
301 for (i = 0; i < 2; ++i) {
302 prob->comps[i].class0_hp = vp9_bin_prob_from_distribution(
303 NMVcount->comps[i].class0_hp);
304 branch_ct_class0_hp[i][0] = NMVcount->comps[i].class0_hp[0];
305 branch_ct_class0_hp[i][1] = NMVcount->comps[i].class0_hp[1];
306
307 prob->comps[i].hp =
308 vp9_bin_prob_from_distribution(NMVcount->comps[i].hp);
309 branch_ct_hp[i][0] = NMVcount->comps[i].hp[0];
310 branch_ct_hp[i][1] = NMVcount->comps[i].hp[1];
311 }
312 }
313 }
314
315 void vp9_adapt_nmv_probs(VP9_COMMON *cm, int usehp) {
316 int i, j, k;
317 nmv_context prob;
318 unsigned int branch_ct_joint[MV_JOINTS - 1][2];
319 unsigned int branch_ct_sign[2][2];
320 unsigned int branch_ct_classes[2][MV_CLASSES - 1][2];
321 unsigned int branch_ct_class0[2][CLASS0_SIZE - 1][2];
322 unsigned int branch_ct_bits[2][MV_OFFSET_BITS][2];
323 unsigned int branch_ct_class0_fp[2][CLASS0_SIZE][4 - 1][2];
324 unsigned int branch_ct_fp[2][4 - 1][2];
325 unsigned int branch_ct_class0_hp[2][2];
326 unsigned int branch_ct_hp[2][2];
327 #ifdef MV_COUNT_TESTING
328 printf("joints count: ");
329 for (j = 0; j < MV_JOINTS; ++j) printf("%d ", cm->fc.NMVcount.joints[j]);
330 printf("\n"); fflush(stdout);
331 printf("signs count:\n");
332 for (i = 0; i < 2; ++i)
333 printf("%d/%d ", cm->fc.NMVcount.comps[i].sign[0], cm->fc.NMVcount.comps[i]. sign[1]);
334 printf("\n"); fflush(stdout);
335 printf("classes count:\n");
336 for (i = 0; i < 2; ++i) {
337 for (j = 0; j < MV_CLASSES; ++j)
338 printf("%d ", cm->fc.NMVcount.comps[i].classes[j]);
339 printf("\n"); fflush(stdout);
340 }
341 printf("class0 count:\n");
342 for (i = 0; i < 2; ++i) {
343 for (j = 0; j < CLASS0_SIZE; ++j)
344 printf("%d ", cm->fc.NMVcount.comps[i].class0[j]);
345 printf("\n"); fflush(stdout);
346 }
347 printf("bits count:\n");
348 for (i = 0; i < 2; ++i) {
349 for (j = 0; j < MV_OFFSET_BITS; ++j)
350 printf("%d/%d ", cm->fc.NMVcount.comps[i].bits[j][0],
351 cm->fc.NMVcount.comps[i].bits[j][1]);
352 printf("\n"); fflush(stdout);
353 }
354 printf("class0_fp count:\n");
355 for (i = 0; i < 2; ++i) {
356 for (j = 0; j < CLASS0_SIZE; ++j) {
357 printf("{");
358 for (k = 0; k < 4; ++k)
359 printf("%d ", cm->fc.NMVcount.comps[i].class0_fp[j][k]);
360 printf("}, ");
361 }
362 printf("\n"); fflush(stdout);
363 }
364 printf("fp count:\n");
365 for (i = 0; i < 2; ++i) {
366 for (j = 0; j < 4; ++j)
367 printf("%d ", cm->fc.NMVcount.comps[i].fp[j]);
368 printf("\n"); fflush(stdout);
369 }
370 if (usehp) {
371 printf("class0_hp count:\n");
372 for (i = 0; i < 2; ++i)
373 printf("%d/%d ", cm->fc.NMVcount.comps[i].class0_hp[0],
374 cm->fc.NMVcount.comps[i].class0_hp[1]);
375 printf("\n"); fflush(stdout);
376 printf("hp count:\n");
377 for (i = 0; i < 2; ++i)
378 printf("%d/%d ", cm->fc.NMVcount.comps[i].hp[0],
379 cm->fc.NMVcount.comps[i].hp[1]);
380 printf("\n"); fflush(stdout);
381 }
382 #endif
383 #ifdef SMOOTH_MV_COUNTS
384 smooth_counts(&cm->fc.NMVcount.comps[0]);
385 smooth_counts(&cm->fc.NMVcount.comps[1]);
386 #endif
387 vp9_counts_to_nmv_context(&cm->fc.NMVcount,
388 &prob,
389 usehp,
390 branch_ct_joint,
391 branch_ct_sign,
392 branch_ct_classes,
393 branch_ct_class0,
394 branch_ct_bits,
395 branch_ct_class0_fp,
396 branch_ct_fp,
397 branch_ct_class0_hp,
398 branch_ct_hp);
399
400 for (j = 0; j < MV_JOINTS - 1; ++j) {
401 adapt_prob(&cm->fc.nmvc.joints[j],
402 cm->fc.pre_nmvc.joints[j],
403 prob.joints[j],
404 branch_ct_joint[j]);
405 }
406 for (i = 0; i < 2; ++i) {
407 adapt_prob(&cm->fc.nmvc.comps[i].sign,
408 cm->fc.pre_nmvc.comps[i].sign,
409 prob.comps[i].sign,
410 branch_ct_sign[i]);
411 for (j = 0; j < MV_CLASSES - 1; ++j) {
412 adapt_prob(&cm->fc.nmvc.comps[i].classes[j],
413 cm->fc.pre_nmvc.comps[i].classes[j],
414 prob.comps[i].classes[j],
415 branch_ct_classes[i][j]);
416 }
417 for (j = 0; j < CLASS0_SIZE - 1; ++j) {
418 adapt_prob(&cm->fc.nmvc.comps[i].class0[j],
419 cm->fc.pre_nmvc.comps[i].class0[j],
420 prob.comps[i].class0[j],
421 branch_ct_class0[i][j]);
422 }
423 for (j = 0; j < MV_OFFSET_BITS; ++j) {
424 adapt_prob(&cm->fc.nmvc.comps[i].bits[j],
425 cm->fc.pre_nmvc.comps[i].bits[j],
426 prob.comps[i].bits[j],
427 branch_ct_bits[i][j]);
428 }
429 }
430 for (i = 0; i < 2; ++i) {
431 for (j = 0; j < CLASS0_SIZE; ++j) {
432 for (k = 0; k < 3; ++k) {
433 adapt_prob(&cm->fc.nmvc.comps[i].class0_fp[j][k],
434 cm->fc.pre_nmvc.comps[i].class0_fp[j][k],
435 prob.comps[i].class0_fp[j][k],
436 branch_ct_class0_fp[i][j][k]);
437 }
438 }
439 for (j = 0; j < 3; ++j) {
440 adapt_prob(&cm->fc.nmvc.comps[i].fp[j],
441 cm->fc.pre_nmvc.comps[i].fp[j],
442 prob.comps[i].fp[j],
443 branch_ct_fp[i][j]);
444 }
445 }
446 if (usehp) {
447 for (i = 0; i < 2; ++i) {
448 adapt_prob(&cm->fc.nmvc.comps[i].class0_hp,
449 cm->fc.pre_nmvc.comps[i].class0_hp,
450 prob.comps[i].class0_hp,
451 branch_ct_class0_hp[i]);
452 adapt_prob(&cm->fc.nmvc.comps[i].hp,
453 cm->fc.pre_nmvc.comps[i].hp,
454 prob.comps[i].hp,
455 branch_ct_hp[i]);
456 }
457 }
458 }
459
460 void vp9_entropy_mv_init() {
461 vp9_tokens_from_tree(vp9_mv_joint_encodings, vp9_mv_joint_tree);
462 vp9_tokens_from_tree(vp9_mv_class_encodings, vp9_mv_class_tree);
463 vp9_tokens_from_tree(vp9_mv_class0_encodings, vp9_mv_class0_tree);
464 vp9_tokens_from_tree(vp9_mv_fp_encodings, vp9_mv_fp_tree);
465 }
466
467 void vp9_init_mv_probs(VP9_COMMON *cm) {
468 vpx_memcpy(&cm->fc.nmvc, &vp9_default_nmv_context, sizeof(nmv_context));
469 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698