3 * VP6 compatible video decoder
5 * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org>
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.
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.
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
27 #include "bitstream.h"
28 #include "mpegvideo.h"
35 static int vp6_parse_header(vp56_context_t *s, uint8_t *buf, int buf_size,
38 vp56_range_coder_t *c = &s->c;
39 int parse_filter_info;
46 s->frames[VP56_FRAME_CURRENT].key_frame = !(buf[0] & 0x80);
47 vp56_init_dequant(s, (buf[0] >> 1) & 0x3F);
49 if (s->frames[VP56_FRAME_CURRENT].key_frame) {
50 if ((buf[1] & 0xFE) != 0x46) /* would be 0x36 for VP61 */
53 av_log(s->avctx, AV_LOG_ERROR, "interlacing not supported\n");
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 */
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);
68 vp56_init_range_decoder(c, buf+6, buf_size-6);
71 parse_filter_info = 1;
73 vp56_init_range_decoder(c, buf+1, buf_size-1);
75 *golden_frame = vp56_rac_get(c);
76 s->deblock_filtering = vp56_rac_get(c);
77 if (s->deblock_filtering)
79 parse_filter_info = vp56_rac_get(c);
82 if (parse_filter_info) {
83 if (vp56_rac_get(c)) {
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)) {
92 s->filter_selection = vp56_rac_gets(c, 4);
99 static void vp6_coeff_order_table_init(vp56_context_t *s)
103 s->coeff_index_to_pos[0] = 0;
105 for (pos=1; pos<64; pos++)
106 if (s->coeff_reorder[pos] == i)
107 s->coeff_index_to_pos[idx++] = pos;
110 static void vp6_default_models_init(vp56_context_t *s)
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;
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));
123 vp6_coeff_order_table_init(s);
126 static void vp6_parse_vector_models(vp56_context_t *s)
128 vp56_range_coder_t *c = &s->c;
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);
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);
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);
149 static void vp6_parse_coeff_models(vp56_context_t *s)
151 vp56_range_coder_t *c = &s->c;
153 int node, cg, ctx, pos;
154 int ct; /* code type */
155 int pt; /* plane type (0 for Y, 1 for U or V) */
157 memset(def_prob, 0x80, sizeof(def_prob));
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];
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);
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);
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];
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);
198 static void vp6_parse_vector_adjustment(vp56_context_t *s, vp56_mv_t *vect)
200 vp56_range_coder_t *c = &s->c;
203 *vect = (vp56_mv_t) {0,0};
204 if (s->vector_candidate_pos < 2)
205 *vect = s->vector_candidate[0];
207 for (comp=0; comp<2; comp++) {
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;
217 delta |= vp56_rac_get_prob(c, s->vector_model_fdv[comp][3])<<3;
221 delta = vp56_rac_get_tree(c, vp56_pva_tree,
222 s->vector_model_pdv[comp]);
225 if (delta && vp56_rac_get_prob(c, s->vector_model_sig[comp]))
235 static void vp6_parse_coeff(vp56_context_t *s)
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) */
244 for (b=0; b<6; b++) {
245 int ct = 1; /* code type */
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];
255 for (coeff_idx=0; coeff_idx<64; ) {
256 if ((coeff_idx>1 && ct==0) || vp56_rac_get_prob(c, model2[0])) {
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;
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;
270 if (vp56_rac_get_prob(c, model2[4]))
271 coeff = 3 + vp56_rac_get_prob(c, model[5]);
280 sign = vp56_rac_get(c);
281 coeff = (coeff ^ -sign) + sign;
283 coeff *= s->dequant_ac;
284 idx = s->coeff_index_to_pos[coeff_idx];
285 s->block_coeff[b][permute[idx]] = coeff;
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;
294 if (!vp56_rac_get_prob(c, model2[1]))
297 model3 = s->coeff_model_runv[coeff_idx >= 6];
298 run = vp56_rac_get_tree(c, vp6_pcr_tree, model3);
300 for (run=9, i=0; i<6; i++)
301 run += vp56_rac_get_prob(c, model3[i+8]) << i;
305 cg = vp6_coeff_groups[coeff_idx+=run];
306 model = model2 = s->coeff_model_ract[pt][ct][cg];
311 static int vp6_adjust(int v, int t)
313 int V = v, s = v >> 31;
316 if (V-t-1 >= (unsigned)(t-1))
324 static int vp6_block_variance(uint8_t *src, int stride)
326 int sum = 0, square_sum = 0;
329 for (y=0; y<8; y+=2) {
330 for (x=0; x<8; x+=2) {
332 square_sum += src[x]*src[x];
336 return (16*square_sum - sum*sum) / (16*16);
339 static void vp6_filter_hv2(vp56_context_t *s, uint8_t *dst, uint8_t *src,
340 int stride, int delta, int16_t weight)
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);
347 static void vp6_filter_hv4(uint8_t *dst, uint8_t *src, int stride,
348 int delta, const int16_t *weights)
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);
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)
367 uint8_t *tmp = s->edge_emu_buffer+16;
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;
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);
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)
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);
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);
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)
420 int x8 = mv.x & mask;
421 int y8 = mv.y & mask;
426 filter4 = s->filter_mode;
428 if (s->max_vector_length &&
429 (ABS(mv.x) > s->max_vector_length ||
430 ABS(mv.y) > s->max_vector_length)) {
432 } else if (!s->sample_variance_threshold
433 || (vp6_block_variance(src+offset1, stride)
434 < s->sample_variance_threshold)) {
440 if ((y8 && (offset2-offset1)*s->flip<0) || (!y8 && offset1 > offset2)) {
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]);
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);
473 static int vp6_decode_init(AVCodecContext *avctx)
475 vp56_context_t *s = avctx->priv_data;
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;
491 AVCodec vp6_decoder = {
495 sizeof(vp56_context_t),
502 /* flash version, not flipped upside-down */
503 AVCodec vp6f_decoder = {
507 sizeof(vp56_context_t),