3 * VP6 compatible video decoder
5 * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org>
7 * This file is part of FFmpeg.
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29 #include "bitstream.h"
30 #include "mpegvideo.h"
37 static int vp6_parse_header(vp56_context_t *s, uint8_t *buf, int buf_size,
40 vp56_range_coder_t *c = &s->c;
41 int parse_filter_info;
48 s->frames[VP56_FRAME_CURRENT].key_frame = !(buf[0] & 0x80);
49 vp56_init_dequant(s, (buf[0] >> 1) & 0x3F);
51 if (s->frames[VP56_FRAME_CURRENT].key_frame) {
52 if ((buf[1] & 0xFE) != 0x46) /* would be 0x36 for VP61 */
55 av_log(s->avctx, AV_LOG_ERROR, "interlacing not supported\n");
59 rows = buf[2]; /* number of stored macroblock rows */
60 cols = buf[3]; /* number of stored macroblock cols */
61 /* buf[4] is number of displayed macroblock rows */
62 /* buf[5] is number of displayed macroblock cols */
64 if (16*cols != s->avctx->coded_width ||
65 16*rows != s->avctx->coded_height) {
66 avcodec_set_dimensions(s->avctx, 16*cols, 16*rows);
67 if (s->avctx->extradata_size == 1) {
68 s->avctx->width -= s->avctx->extradata[0] >> 4;
69 s->avctx->height -= s->avctx->extradata[0] & 0x0F;
74 vp56_init_range_decoder(c, buf+6, buf_size-6);
77 parse_filter_info = 1;
79 vp56_init_range_decoder(c, buf+1, buf_size-1);
81 *golden_frame = vp56_rac_get(c);
82 s->deblock_filtering = vp56_rac_get(c);
83 if (s->deblock_filtering)
85 parse_filter_info = vp56_rac_get(c);
88 if (parse_filter_info) {
89 if (vp56_rac_get(c)) {
91 s->sample_variance_threshold = vp56_rac_gets(c, 5);
92 s->max_vector_length = 2 << vp56_rac_gets(c, 3);
93 } else if (vp56_rac_get(c)) {
98 s->filter_selection = vp56_rac_gets(c, 4);
105 static void vp6_coeff_order_table_init(vp56_context_t *s)
109 s->coeff_index_to_pos[0] = 0;
111 for (pos=1; pos<64; pos++)
112 if (s->coeff_reorder[pos] == i)
113 s->coeff_index_to_pos[idx++] = pos;
116 static void vp6_default_models_init(vp56_context_t *s)
118 s->vector_model_dct[0] = 0xA2;
119 s->vector_model_dct[1] = 0xA4;
120 s->vector_model_sig[0] = 0x80;
121 s->vector_model_sig[1] = 0x80;
123 memcpy(s->mb_types_stats, vp56_def_mb_types_stats, sizeof(s->mb_types_stats));
124 memcpy(s->vector_model_fdv, vp6_def_fdv_vector_model, sizeof(s->vector_model_fdv));
125 memcpy(s->vector_model_pdv, vp6_def_pdv_vector_model, sizeof(s->vector_model_pdv));
126 memcpy(s->coeff_model_runv, vp6_def_runv_coeff_model, sizeof(s->coeff_model_runv));
127 memcpy(s->coeff_reorder, vp6_def_coeff_reorder, sizeof(s->coeff_reorder));
129 vp6_coeff_order_table_init(s);
132 static void vp6_parse_vector_models(vp56_context_t *s)
134 vp56_range_coder_t *c = &s->c;
137 for (comp=0; comp<2; comp++) {
138 if (vp56_rac_get_prob(c, vp6_sig_dct_pct[comp][0]))
139 s->vector_model_dct[comp] = vp56_rac_gets_nn(c, 7);
140 if (vp56_rac_get_prob(c, vp6_sig_dct_pct[comp][1]))
141 s->vector_model_sig[comp] = vp56_rac_gets_nn(c, 7);
144 for (comp=0; comp<2; comp++)
145 for (node=0; node<7; node++)
146 if (vp56_rac_get_prob(c, vp6_pdv_pct[comp][node]))
147 s->vector_model_pdv[comp][node] = vp56_rac_gets_nn(c, 7);
149 for (comp=0; comp<2; comp++)
150 for (node=0; node<8; node++)
151 if (vp56_rac_get_prob(c, vp6_fdv_pct[comp][node]))
152 s->vector_model_fdv[comp][node] = vp56_rac_gets_nn(c, 7);
155 static void vp6_parse_coeff_models(vp56_context_t *s)
157 vp56_range_coder_t *c = &s->c;
159 int node, cg, ctx, pos;
160 int ct; /* code type */
161 int pt; /* plane type (0 for Y, 1 for U or V) */
163 memset(def_prob, 0x80, sizeof(def_prob));
165 for (pt=0; pt<2; pt++)
166 for (node=0; node<11; node++)
167 if (vp56_rac_get_prob(c, vp6_dccv_pct[pt][node])) {
168 def_prob[node] = vp56_rac_gets_nn(c, 7);
169 s->coeff_model_dccv[pt][node] = def_prob[node];
170 } else if (s->frames[VP56_FRAME_CURRENT].key_frame) {
171 s->coeff_model_dccv[pt][node] = def_prob[node];
174 if (vp56_rac_get(c)) {
175 for (pos=1; pos<64; pos++)
176 if (vp56_rac_get_prob(c, vp6_coeff_reorder_pct[pos]))
177 s->coeff_reorder[pos] = vp56_rac_gets(c, 4);
178 vp6_coeff_order_table_init(s);
181 for (cg=0; cg<2; cg++)
182 for (node=0; node<14; node++)
183 if (vp56_rac_get_prob(c, vp6_runv_pct[cg][node]))
184 s->coeff_model_runv[cg][node] = vp56_rac_gets_nn(c, 7);
186 for (ct=0; ct<3; ct++)
187 for (pt=0; pt<2; pt++)
188 for (cg=0; cg<6; cg++)
189 for (node=0; node<11; node++)
190 if (vp56_rac_get_prob(c, vp6_ract_pct[ct][pt][cg][node])) {
191 def_prob[node] = vp56_rac_gets_nn(c, 7);
192 s->coeff_model_ract[pt][ct][cg][node] = def_prob[node];
193 } else if (s->frames[VP56_FRAME_CURRENT].key_frame) {
194 s->coeff_model_ract[pt][ct][cg][node] = def_prob[node];
197 /* coeff_model_dcct is a linear combination of coeff_model_dccv */
198 for (pt=0; pt<2; pt++)
199 for (ctx=0; ctx<3; ctx++)
200 for (node=0; node<5; node++)
201 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);
204 static void vp6_parse_vector_adjustment(vp56_context_t *s, vp56_mv_t *vect)
206 vp56_range_coder_t *c = &s->c;
209 *vect = (vp56_mv_t) {0,0};
210 if (s->vector_candidate_pos < 2)
211 *vect = s->vector_candidate[0];
213 for (comp=0; comp<2; comp++) {
216 if (vp56_rac_get_prob(c, s->vector_model_dct[comp])) {
217 static const uint8_t prob_order[] = {0, 1, 2, 7, 6, 5, 4};
218 for (i=0; i<sizeof(prob_order); i++) {
219 int j = prob_order[i];
220 delta |= vp56_rac_get_prob(c, s->vector_model_fdv[comp][j])<<j;
223 delta |= vp56_rac_get_prob(c, s->vector_model_fdv[comp][3])<<3;
227 delta = vp56_rac_get_tree(c, vp56_pva_tree,
228 s->vector_model_pdv[comp]);
231 if (delta && vp56_rac_get_prob(c, s->vector_model_sig[comp]))
241 static void vp6_parse_coeff(vp56_context_t *s)
243 vp56_range_coder_t *c = &s->c;
244 uint8_t *permute = s->scantable.permutated;
245 uint8_t *model, *model2, *model3;
246 int coeff, sign, coeff_idx;
247 int b, i, cg, idx, ctx;
248 int pt = 0; /* plane type (0 for Y, 1 for U or V) */
250 for (b=0; b<6; b++) {
251 int ct = 1; /* code type */
256 ctx = s->left_block[vp56_b6to4[b]].not_null_dc
257 + s->above_blocks[s->above_block_idx[b]].not_null_dc;
258 model = s->coeff_model_dccv[pt];
259 model2 = s->coeff_model_dcct[pt][ctx];
261 for (coeff_idx=0; coeff_idx<64; ) {
262 if ((coeff_idx>1 && ct==0) || vp56_rac_get_prob(c, model2[0])) {
264 if (coeff_idx == 0) {
265 s->left_block[vp56_b6to4[b]].not_null_dc = 1;
266 s->above_blocks[s->above_block_idx[b]].not_null_dc = 1;
269 if (vp56_rac_get_prob(c, model2[2])) {
270 if (vp56_rac_get_prob(c, model2[3])) {
271 idx = vp56_rac_get_tree(c, vp56_pc_tree, model);
272 coeff = vp56_coeff_bias[idx];
273 for (i=vp56_coeff_bit_length[idx]; i>=0; i--)
274 coeff += vp56_rac_get_prob(c, vp56_coeff_parse_table[idx][i]) << i;
276 if (vp56_rac_get_prob(c, model2[4]))
277 coeff = 3 + vp56_rac_get_prob(c, model[5]);
286 sign = vp56_rac_get(c);
287 coeff = (coeff ^ -sign) + sign;
289 coeff *= s->dequant_ac;
290 idx = s->coeff_index_to_pos[coeff_idx];
291 s->block_coeff[b][permute[idx]] = coeff;
296 if (coeff_idx == 0) {
297 s->left_block[vp56_b6to4[b]].not_null_dc = 0;
298 s->above_blocks[s->above_block_idx[b]].not_null_dc = 0;
300 if (!vp56_rac_get_prob(c, model2[1]))
303 model3 = s->coeff_model_runv[coeff_idx >= 6];
304 run = vp56_rac_get_tree(c, vp6_pcr_tree, model3);
306 for (run=9, i=0; i<6; i++)
307 run += vp56_rac_get_prob(c, model3[i+8]) << i;
311 cg = vp6_coeff_groups[coeff_idx+=run];
312 model = model2 = s->coeff_model_ract[pt][ct][cg];
317 static int vp6_adjust(int v, int t)
319 int V = v, s = v >> 31;
322 if (V-t-1 >= (unsigned)(t-1))
330 static int vp6_block_variance(uint8_t *src, int stride)
332 int sum = 0, square_sum = 0;
335 for (y=0; y<8; y+=2) {
336 for (x=0; x<8; x+=2) {
338 square_sum += src[x]*src[x];
342 return (16*square_sum - sum*sum) / (16*16);
345 static void vp6_filter_hv2(vp56_context_t *s, uint8_t *dst, uint8_t *src,
346 int stride, int delta, int16_t weight)
348 s->dsp.put_pixels_tab[1][0](dst, src, stride, 8);
349 s->dsp.biweight_h264_pixels_tab[3](dst, src+delta, stride, 2,
350 8-weight, weight, 0);
353 static void vp6_filter_hv4(uint8_t *dst, uint8_t *src, int stride,
354 int delta, const int16_t *weights)
358 for (y=0; y<8; y++) {
359 for (x=0; x<8; x++) {
360 dst[x] = clip_uint8(( src[x-delta ] * weights[0]
361 + src[x ] * weights[1]
362 + src[x+delta ] * weights[2]
363 + src[x+2*delta] * weights[3] + 64) >> 7);
370 static void vp6_filter_diag2(vp56_context_t *s, uint8_t *dst, uint8_t *src,
371 int stride, int h_weight, int v_weight)
373 uint8_t *tmp = s->edge_emu_buffer+16;
376 s->dsp.put_pixels_tab[1][0](tmp, src, stride, 8);
377 s->dsp.biweight_h264_pixels_tab[3](tmp, src+1, stride, 2,
378 8-h_weight, h_weight, 0);
379 /* we need a 8x9 block to do vertical filter, so compute one more line */
380 for (x=8*stride, xmax=x+8; x<xmax; x++)
381 tmp[x] = (src[x]*(8-h_weight) + src[x+1]*h_weight + 4) >> 3;
383 s->dsp.put_pixels_tab[1][0](dst, tmp, stride, 8);
384 s->dsp.biweight_h264_pixels_tab[3](dst, tmp+stride, stride, 2,
385 8-v_weight, v_weight, 0);
388 static void vp6_filter_diag4(uint8_t *dst, uint8_t *src, int stride,
389 const int16_t *h_weights,const int16_t *v_weights)
397 for (y=0; y<11; y++) {
398 for (x=0; x<8; x++) {
399 t[x] = clip_uint8(( src[x-1] * h_weights[0]
400 + src[x ] * h_weights[1]
401 + src[x+1] * h_weights[2]
402 + src[x+2] * h_weights[3] + 64) >> 7);
409 for (y=0; y<8; y++) {
410 for (x=0; x<8; x++) {
411 dst[x] = clip_uint8(( t[x-8 ] * v_weights[0]
412 + t[x ] * v_weights[1]
413 + t[x+8 ] * v_weights[2]
414 + t[x+16] * v_weights[3] + 64) >> 7);
421 static void vp6_filter(vp56_context_t *s, uint8_t *dst, uint8_t *src,
422 int offset1, int offset2, int stride,
423 vp56_mv_t mv, int mask, int select, int luma)
426 int x8 = mv.x & mask;
427 int y8 = mv.y & mask;
432 filter4 = s->filter_mode;
434 if (s->max_vector_length &&
435 (FFABS(mv.x) > s->max_vector_length ||
436 FFABS(mv.y) > s->max_vector_length)) {
438 } else if (!s->sample_variance_threshold
439 || (vp6_block_variance(src+offset1, stride)
440 < s->sample_variance_threshold)) {
446 if ((y8 && (offset2-offset1)*s->flip<0) || (!y8 && offset1 > offset2)) {
451 if (!y8) { /* left or right combine */
452 vp6_filter_hv4(dst, src+offset1, stride, 1,
453 vp6_block_copy_filter[select][x8]);
454 } else if (!x8) { /* above or below combine */
455 vp6_filter_hv4(dst, src+offset1, stride, stride,
456 vp6_block_copy_filter[select][y8]);
457 } else if ((mv.x^mv.y) >> 31) { /* lower-left or upper-right combine */
458 vp6_filter_diag4(dst, src+offset1-1, stride,
459 vp6_block_copy_filter[select][x8],
460 vp6_block_copy_filter[select][y8]);
461 } else { /* lower-right or upper-left combine */
462 vp6_filter_diag4(dst, src+offset1, stride,
463 vp6_block_copy_filter[select][x8],
464 vp6_block_copy_filter[select][y8]);
467 if (!y8) { /* left or right combine */
468 vp6_filter_hv2(s, dst, src+offset1, stride, 1, x8);
469 } else if (!x8) { /* above or below combine */
470 vp6_filter_hv2(s, dst, src+offset1, stride, stride, y8);
471 } else if ((mv.x^mv.y) >> 31) { /* lower-left or upper-right combine */
472 vp6_filter_diag2(s, dst, src+offset1-1, stride, x8, y8);
473 } else { /* lower-right or upper-left combine */
474 vp6_filter_diag2(s, dst, src+offset1, stride, x8, y8);
479 static int vp6_decode_init(AVCodecContext *avctx)
481 vp56_context_t *s = avctx->priv_data;
483 vp56_init(s, avctx, avctx->codec->id == CODEC_ID_VP6);
484 s->vp56_coord_div = vp6_coord_div;
485 s->parse_vector_adjustment = vp6_parse_vector_adjustment;
486 s->adjust = vp6_adjust;
487 s->filter = vp6_filter;
488 s->parse_coeff = vp6_parse_coeff;
489 s->default_models_init = vp6_default_models_init;
490 s->parse_vector_models = vp6_parse_vector_models;
491 s->parse_coeff_models = vp6_parse_coeff_models;
492 s->parse_header = vp6_parse_header;
497 AVCodec vp6_decoder = {
501 sizeof(vp56_context_t),
508 /* flash version, not flipped upside-down */
509 AVCodec vp6f_decoder = {
513 sizeof(vp56_context_t),