comparison vp6.c @ 3695:6795c9e5f983 libavcodec

VP5 and VP6 video decoder
author aurel
date Sat, 09 Sep 2006 17:19:37 +0000
parents
children 596e62da8f29
comparison
equal deleted inserted replaced
3694:8765ee4eaa45 3695:6795c9e5f983
1 /**
2 * @file vp6.c
3 * VP6 compatible video decoder
4 *
5 * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <stdlib.h>
23 #include <inttypes.h>
24
25 #include "avcodec.h"
26 #include "dsputil.h"
27 #include "bitstream.h"
28 #include "mpegvideo.h"
29
30 #include "vp56.h"
31 #include "vp56data.h"
32 #include "vp6data.h"
33
34
35 static int vp6_parse_header(vp56_context_t *s, uint8_t *buf, int buf_size,
36 int *golden_frame)
37 {
38 vp56_range_coder_t *c = &s->c;
39 int parse_filter_info;
40 int rows, cols;
41 int res = 1;
42
43 if (buf[0] & 1)
44 return 0;
45
46 s->frames[VP56_FRAME_CURRENT].key_frame = !(buf[0] & 0x80);
47 vp56_init_dequant(s, (buf[0] >> 1) & 0x3F);
48
49 if (s->frames[VP56_FRAME_CURRENT].key_frame) {
50 if ((buf[1] & 0xFE) != 0x46) /* would be 0x36 for VP61 */
51 return 0;
52 if (buf[1] & 1) {
53 av_log(s->avctx, AV_LOG_ERROR, "interlacing not supported\n");
54 return 0;
55 }
56
57 rows = buf[2]; /* number of stored macroblock rows */
58 cols = buf[3]; /* number of stored macroblock cols */
59 /* buf[4] is number of displayed macroblock rows */
60 /* buf[5] is number of displayed macroblock cols */
61
62 if (16*cols != s->avctx->coded_width ||
63 16*rows != s->avctx->coded_height) {
64 avcodec_set_dimensions(s->avctx, 16*cols, 16*rows);
65 res = 2;
66 }
67
68 vp56_init_range_decoder(c, buf+6, buf_size-6);
69 vp56_rac_gets(c, 2);
70
71 parse_filter_info = 1;
72 } else {
73 vp56_init_range_decoder(c, buf+1, buf_size-1);
74
75 *golden_frame = vp56_rac_get(c);
76 s->deblock_filtering = vp56_rac_get(c);
77 if (s->deblock_filtering)
78 vp56_rac_get(c);
79 parse_filter_info = vp56_rac_get(c);
80 }
81
82 if (parse_filter_info) {
83 if (vp56_rac_get(c)) {
84 s->filter_mode = 2;
85 s->sample_variance_threshold = vp56_rac_gets(c, 5);
86 s->max_vector_length = 2 << vp56_rac_gets(c, 3);
87 } else if (vp56_rac_get(c)) {
88 s->filter_mode = 1;
89 } else {
90 s->filter_mode = 0;
91 }
92 s->filter_selection = vp56_rac_gets(c, 4);
93 }
94
95 vp56_rac_get(c);
96 return res;
97 }
98
99 static void vp6_coeff_order_table_init(vp56_context_t *s)
100 {
101 int i, pos, idx = 1;
102
103 s->coeff_index_to_pos[0] = 0;
104 for (i=0; i<16; i++)
105 for (pos=1; pos<64; pos++)
106 if (s->coeff_reorder[pos] == i)
107 s->coeff_index_to_pos[idx++] = pos;
108 }
109
110 static void vp6_default_models_init(vp56_context_t *s)
111 {
112 s->vector_model_dct[0] = 0xA2;
113 s->vector_model_dct[1] = 0xA4;
114 s->vector_model_sig[0] = 0x80;
115 s->vector_model_sig[1] = 0x80;
116
117 memcpy(s->mb_types_stats, vp56_def_mb_types_stats, sizeof(s->mb_types_stats));
118 memcpy(s->vector_model_fdv, vp6_def_fdv_vector_model, sizeof(s->vector_model_fdv));
119 memcpy(s->vector_model_pdv, vp6_def_pdv_vector_model, sizeof(s->vector_model_pdv));
120 memcpy(s->coeff_model_runv, vp6_def_runv_coeff_model, sizeof(s->coeff_model_runv));
121 memcpy(s->coeff_reorder, vp6_def_coeff_reorder, sizeof(s->coeff_reorder));
122
123 vp6_coeff_order_table_init(s);
124 }
125
126 static void vp6_parse_vector_models(vp56_context_t *s)
127 {
128 vp56_range_coder_t *c = &s->c;
129 int comp, node;
130
131 for (comp=0; comp<2; comp++) {
132 if (vp56_rac_get_prob(c, vp6_sig_dct_pct[comp][0]))
133 s->vector_model_dct[comp] = vp56_rac_gets_nn(c, 7);
134 if (vp56_rac_get_prob(c, vp6_sig_dct_pct[comp][1]))
135 s->vector_model_sig[comp] = vp56_rac_gets_nn(c, 7);
136 }
137
138 for (comp=0; comp<2; comp++)
139 for (node=0; node<7; node++)
140 if (vp56_rac_get_prob(c, vp6_pdv_pct[comp][node]))
141 s->vector_model_pdv[comp][node] = vp56_rac_gets_nn(c, 7);
142
143 for (comp=0; comp<2; comp++)
144 for (node=0; node<8; node++)
145 if (vp56_rac_get_prob(c, vp6_fdv_pct[comp][node]))
146 s->vector_model_fdv[comp][node] = vp56_rac_gets_nn(c, 7);
147 }
148
149 static void vp6_parse_coeff_models(vp56_context_t *s)
150 {
151 vp56_range_coder_t *c = &s->c;
152 int def_prob[11];
153 int node, cg, ctx, pos;
154 int ct; /* code type */
155 int pt; /* plane type (0 for Y, 1 for U or V) */
156
157 memset(def_prob, 0x80, sizeof(def_prob));
158
159 for (pt=0; pt<2; pt++)
160 for (node=0; node<11; node++)
161 if (vp56_rac_get_prob(c, vp6_dccv_pct[pt][node])) {
162 def_prob[node] = vp56_rac_gets_nn(c, 7);
163 s->coeff_model_dccv[pt][node] = def_prob[node];
164 } else if (s->frames[VP56_FRAME_CURRENT].key_frame) {
165 s->coeff_model_dccv[pt][node] = def_prob[node];
166 }
167
168 if (vp56_rac_get(c)) {
169 for (pos=1; pos<64; pos++)
170 if (vp56_rac_get_prob(c, vp6_coeff_reorder_pct[pos]))
171 s->coeff_reorder[pos] = vp56_rac_gets(c, 4);
172 vp6_coeff_order_table_init(s);
173 }
174
175 for (cg=0; cg<2; cg++)
176 for (node=0; node<14; node++)
177 if (vp56_rac_get_prob(c, vp6_runv_pct[cg][node]))
178 s->coeff_model_runv[cg][node] = vp56_rac_gets_nn(c, 7);
179
180 for (ct=0; ct<3; ct++)
181 for (pt=0; pt<2; pt++)
182 for (cg=0; cg<6; cg++)
183 for (node=0; node<11; node++)
184 if (vp56_rac_get_prob(c, vp6_ract_pct[ct][pt][cg][node])) {
185 def_prob[node] = vp56_rac_gets_nn(c, 7);
186 s->coeff_model_ract[pt][ct][cg][node] = def_prob[node];
187 } else if (s->frames[VP56_FRAME_CURRENT].key_frame) {
188 s->coeff_model_ract[pt][ct][cg][node] = def_prob[node];
189 }
190
191 /* coeff_model_dcct is a linear combination of coeff_model_dccv */
192 for (pt=0; pt<2; pt++)
193 for (ctx=0; ctx<3; ctx++)
194 for (node=0; node<5; node++)
195 s->coeff_model_dcct[pt][ctx][node] = clip(((s->coeff_model_dccv[pt][node] * vp6_dccv_lc[ctx][node][0] + 128) >> 8) + vp6_dccv_lc[ctx][node][1], 1, 255);
196 }
197
198 static void vp6_parse_vector_adjustment(vp56_context_t *s, vp56_mv_t *vector)
199 {
200 vp56_range_coder_t *c = &s->c;
201 int comp;
202
203 *vector = (vp56_mv_t) {0,0};
204 if (s->vector_candidate_pos < 2)
205 *vector = s->vector_candidate[0];
206
207 for (comp=0; comp<2; comp++) {
208 int i, delta = 0;
209
210 if (vp56_rac_get_prob(c, s->vector_model_dct[comp])) {
211 static const uint8_t prob_order[] = {0, 1, 2, 7, 6, 5, 4};
212 for (i=0; i<sizeof(prob_order); i++) {
213 int j = prob_order[i];
214 delta |= vp56_rac_get_prob(c, s->vector_model_fdv[comp][j])<<j;
215 }
216 if (delta & 0xF0)
217 delta |= vp56_rac_get_prob(c, s->vector_model_fdv[comp][3])<<3;
218 else
219 delta |= 8;
220 } else {
221 delta = vp56_rac_get_tree(c, vp56_pva_tree,
222 s->vector_model_pdv[comp]);
223 }
224
225 if (delta && vp56_rac_get_prob(c, s->vector_model_sig[comp]))
226 delta = -delta;
227
228 if (!comp)
229 vector->x += delta;
230 else
231 vector->y += delta;
232 }
233 }
234
235 static void vp6_parse_coeff(vp56_context_t *s)
236 {
237 vp56_range_coder_t *c = &s->c;
238 uint8_t *permute = s->scantable.permutated;
239 uint8_t *model, *model2, *model3;
240 int coeff, sign, coeff_idx;
241 int b, i, cg, idx, ctx;
242 int pt = 0; /* plane type (0 for Y, 1 for U or V) */
243
244 for (b=0; b<6; b++) {
245 int ct = 1; /* code type */
246 int run = 1;
247
248 if (b > 3) pt = 1;
249
250 ctx = s->left_block[vp56_b6to4[b]].not_null_dc
251 + s->above_blocks[s->above_block_idx[b]].not_null_dc;
252 model = s->coeff_model_dccv[pt];
253 model2 = s->coeff_model_dcct[pt][ctx];
254
255 for (coeff_idx=0; coeff_idx<64; ) {
256 if ((coeff_idx>1 && ct==0) || vp56_rac_get_prob(c, model2[0])) {
257 /* parse a coeff */
258 if (coeff_idx == 0) {
259 s->left_block[vp56_b6to4[b]].not_null_dc = 1;
260 s->above_blocks[s->above_block_idx[b]].not_null_dc = 1;
261 }
262
263 if (vp56_rac_get_prob(c, model2[2])) {
264 if (vp56_rac_get_prob(c, model2[3])) {
265 idx = vp56_rac_get_tree(c, vp56_pc_tree, model);
266 coeff = vp56_coeff_bias[idx];
267 for (i=vp56_coeff_bit_length[idx]; i>=0; i--)
268 coeff += vp56_rac_get_prob(c, vp56_coeff_parse_table[idx][i]) << i;
269 } else {
270 if (vp56_rac_get_prob(c, model2[4]))
271 coeff = 3 + vp56_rac_get_prob(c, model[5]);
272 else
273 coeff = 2;
274 }
275 ct = 2;
276 } else {
277 ct = 1;
278 coeff = 1;
279 }
280 sign = vp56_rac_get(c);
281 coeff = (coeff ^ -sign) + sign;
282 if (coeff_idx)
283 coeff *= s->dequant_ac;
284 idx = s->coeff_index_to_pos[coeff_idx];
285 s->block_coeff[b][permute[idx]] = coeff;
286 run = 1;
287 } else {
288 /* parse a run */
289 ct = 0;
290 if (coeff_idx == 0) {
291 s->left_block[vp56_b6to4[b]].not_null_dc = 0;
292 s->above_blocks[s->above_block_idx[b]].not_null_dc = 0;
293 } else {
294 if (!vp56_rac_get_prob(c, model2[1]))
295 break;
296
297 model3 = s->coeff_model_runv[coeff_idx >= 6];
298 run = vp56_rac_get_tree(c, vp6_pcr_tree, model3);
299 if (!run)
300 for (run=9, i=0; i<6; i++)
301 run += vp56_rac_get_prob(c, model3[i+8]) << i;
302 }
303 }
304
305 cg = vp6_coeff_groups[coeff_idx+=run];
306 model = model2 = s->coeff_model_ract[pt][ct][cg];
307 }
308 }
309 }
310
311 static int vp6_adjust(int v, int t)
312 {
313 int V = v, s = v >> 31;
314 V ^= s;
315 V -= s;
316 if (V-t-1 >= (unsigned)(t-1))
317 return v;
318 V = 2*t - V;
319 V += s;
320 V ^= s;
321 return V;
322 }
323
324 static int vp6_block_variance(uint8_t *src, int stride)
325 {
326 int sum = 0, square_sum = 0;
327 int y, x;
328
329 for (y=0; y<8; y+=2) {
330 for (x=0; x<8; x+=2) {
331 sum += src[x];
332 square_sum += src[x]*src[x];
333 }
334 src += 2*stride;
335 }
336 return (16*square_sum - sum*sum) / (16*16);
337 }
338
339 static void vp6_filter_hv2(vp56_context_t *s, uint8_t *dst, uint8_t *src,
340 int stride, int delta, int16_t weight)
341 {
342 s->dsp.put_pixels_tab[1][0](dst, src, stride, 8);
343 s->dsp.biweight_h264_pixels_tab[3](dst, src+delta, stride, 2,
344 8-weight, weight, 0);
345 }
346
347 static void vp6_filter_hv4(uint8_t *dst, uint8_t *src, int stride,
348 int delta, const int16_t *weights)
349 {
350 int x, y;
351
352 for (y=0; y<8; y++) {
353 for (x=0; x<8; x++) {
354 dst[x] = clip_uint8(( src[x-delta ] * weights[0]
355 + src[x ] * weights[1]
356 + src[x+delta ] * weights[2]
357 + src[x+2*delta] * weights[3] + 64) >> 7);
358 }
359 src += stride;
360 dst += stride;
361 }
362 }
363
364 static void vp6_filter_diag2(vp56_context_t *s, uint8_t *dst, uint8_t *src,
365 int stride, int h_weight, int v_weight)
366 {
367 uint8_t *tmp = s->edge_emu_buffer+16;
368 int x, xmax;
369
370 s->dsp.put_pixels_tab[1][0](tmp, src, stride, 8);
371 s->dsp.biweight_h264_pixels_tab[3](tmp, src+1, stride, 2,
372 8-h_weight, h_weight, 0);
373 /* we need a 8x9 block to do vertical filter, so compute one more line */
374 for (x=8*stride, xmax=x+8; x<xmax; x++)
375 tmp[x] = (src[x]*(8-h_weight) + src[x+1]*h_weight + 4) >> 3;
376
377 s->dsp.put_pixels_tab[1][0](dst, tmp, stride, 8);
378 s->dsp.biweight_h264_pixels_tab[3](dst, tmp+stride, stride, 2,
379 8-v_weight, v_weight, 0);
380 }
381
382 static void vp6_filter_diag4(uint8_t *dst, uint8_t *src, int stride,
383 const int16_t *h_weights,const int16_t *v_weights)
384 {
385 int x, y;
386 int tmp[8*11];
387 int *t = tmp;
388
389 src -= stride;
390
391 for (y=0; y<11; y++) {
392 for (x=0; x<8; x++) {
393 t[x] = clip_uint8(( src[x-1] * h_weights[0]
394 + src[x ] * h_weights[1]
395 + src[x+1] * h_weights[2]
396 + src[x+2] * h_weights[3] + 64) >> 7);
397 }
398 src += stride;
399 t += 8;
400 }
401
402 t = tmp + 8;
403 for (y=0; y<8; y++) {
404 for (x=0; x<8; x++) {
405 dst[x] = clip_uint8(( t[x-8 ] * v_weights[0]
406 + t[x ] * v_weights[1]
407 + t[x+8 ] * v_weights[2]
408 + t[x+16] * v_weights[3] + 64) >> 7);
409 }
410 dst += stride;
411 t += 8;
412 }
413 }
414
415 static void vp6_filter(vp56_context_t *s, uint8_t *dst, uint8_t *src,
416 int offset1, int offset2, int stride,
417 vp56_mv_t mv, int mask, int select, int luma)
418 {
419 int filter4 = 0;
420 int x8 = mv.x & mask;
421 int y8 = mv.y & mask;
422
423 if (luma) {
424 x8 *= 2;
425 y8 *= 2;
426 filter4 = s->filter_mode;
427 if (filter4 == 2) {
428 if (s->max_vector_length &&
429 (ABS(mv.x) > s->max_vector_length ||
430 ABS(mv.y) > s->max_vector_length)) {
431 filter4 = 0;
432 } else if (!s->sample_variance_threshold
433 || (vp6_block_variance(src+offset1, stride)
434 < s->sample_variance_threshold)) {
435 filter4 = 0;
436 }
437 }
438 }
439
440 if ((y8 && (offset2-offset1)*s->flip<0) || (!y8 && offset1 > offset2)) {
441 offset1 = offset2;
442 }
443
444 if (filter4) {
445 if (!y8) { /* left or right combine */
446 vp6_filter_hv4(dst, src+offset1, stride, 1,
447 vp6_block_copy_filter[select][x8]);
448 } else if (!x8) { /* above or below combine */
449 vp6_filter_hv4(dst, src+offset1, stride, stride,
450 vp6_block_copy_filter[select][y8]);
451 } else if ((mv.x^mv.y) >> 31) { /* lower-left or upper-right combine */
452 vp6_filter_diag4(dst, src+offset1-1, stride,
453 vp6_block_copy_filter[select][x8],
454 vp6_block_copy_filter[select][y8]);
455 } else { /* lower-right or upper-left combine */
456 vp6_filter_diag4(dst, src+offset1, stride,
457 vp6_block_copy_filter[select][x8],
458 vp6_block_copy_filter[select][y8]);
459 }
460 } else {
461 if (!y8) { /* left or right combine */
462 vp6_filter_hv2(s, dst, src+offset1, stride, 1, x8);
463 } else if (!x8) { /* above or below combine */
464 vp6_filter_hv2(s, dst, src+offset1, stride, stride, y8);
465 } else if ((mv.x^mv.y) >> 31) { /* lower-left or upper-right combine */
466 vp6_filter_diag2(s, dst, src+offset1-1, stride, x8, y8);
467 } else { /* lower-right or upper-left combine */
468 vp6_filter_diag2(s, dst, src+offset1, stride, x8, y8);
469 }
470 }
471 }
472
473 static int vp6_decode_init(AVCodecContext *avctx)
474 {
475 vp56_context_t *s = avctx->priv_data;
476
477 vp56_init(s, avctx, avctx->codec->id == CODEC_ID_VP6);
478 s->vp56_coord_div = vp6_coord_div;
479 s->parse_vector_adjustment = vp6_parse_vector_adjustment;
480 s->adjust = vp6_adjust;
481 s->filter = vp6_filter;
482 s->parse_coeff = vp6_parse_coeff;
483 s->default_models_init = vp6_default_models_init;
484 s->parse_vector_models = vp6_parse_vector_models;
485 s->parse_coeff_models = vp6_parse_coeff_models;
486 s->parse_header = vp6_parse_header;
487
488 return 0;
489 }
490
491 AVCodec vp6_decoder = {
492 "vp6",
493 CODEC_TYPE_VIDEO,
494 CODEC_ID_VP6,
495 sizeof(vp56_context_t),
496 vp6_decode_init,
497 NULL,
498 vp56_free,
499 vp56_decode_frame,
500 };
501
502 /* flash version, not flipped upside-down */
503 AVCodec vp6f_decoder = {
504 "vp6f",
505 CODEC_TYPE_VIDEO,
506 CODEC_ID_VP6F,
507 sizeof(vp56_context_t),
508 vp6_decode_init,
509 NULL,
510 vp56_free,
511 vp56_decode_frame,
512 };