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