]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - libavcodec/vp6.c
use the adjustment value present in FLV to crop VP6 video
[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 #include <stdlib.h>
25 #include <inttypes.h>
26
27 #include "avcodec.h"
28 #include "dsputil.h"
29 #include "bitstream.h"
30 #include "mpegvideo.h"
31
32 #include "vp56.h"
33 #include "vp56data.h"
34 #include "vp6data.h"
35
36
37 static int vp6_parse_header(vp56_context_t *s, uint8_t *buf, int buf_size,
38                             int *golden_frame)
39 {
40     vp56_range_coder_t *c = &s->c;
41     int parse_filter_info;
42     int rows, cols;
43     int res = 1;
44
45     if (buf[0] & 1)
46         return 0;
47
48     s->frames[VP56_FRAME_CURRENT].key_frame = !(buf[0] & 0x80);
49     vp56_init_dequant(s, (buf[0] >> 1) & 0x3F);
50
51     if (s->frames[VP56_FRAME_CURRENT].key_frame) {
52         if ((buf[1] & 0xFE) != 0x46)  /* would be 0x36 for VP61 */
53             return 0;
54         if (buf[1] & 1) {
55             av_log(s->avctx, AV_LOG_ERROR, "interlacing not supported\n");
56             return 0;
57         }
58
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 */
63
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;
70             }
71             res = 2;
72         }
73
74         vp56_init_range_decoder(c, buf+6, buf_size-6);
75         vp56_rac_gets(c, 2);
76
77         parse_filter_info = 1;
78     } else {
79         vp56_init_range_decoder(c, buf+1, buf_size-1);
80
81         *golden_frame = vp56_rac_get(c);
82         s->deblock_filtering = vp56_rac_get(c);
83         if (s->deblock_filtering)
84             vp56_rac_get(c);
85         parse_filter_info = vp56_rac_get(c);
86     }
87
88     if (parse_filter_info) {
89         if (vp56_rac_get(c)) {
90             s->filter_mode = 2;
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)) {
94             s->filter_mode = 1;
95         } else {
96             s->filter_mode = 0;
97         }
98         s->filter_selection = vp56_rac_gets(c, 4);
99     }
100
101     vp56_rac_get(c);
102     return res;
103 }
104
105 static void vp6_coeff_order_table_init(vp56_context_t *s)
106 {
107     int i, pos, idx = 1;
108
109     s->coeff_index_to_pos[0] = 0;
110     for (i=0; i<16; i++)
111         for (pos=1; pos<64; pos++)
112             if (s->coeff_reorder[pos] == i)
113                 s->coeff_index_to_pos[idx++] = pos;
114 }
115
116 static void vp6_default_models_init(vp56_context_t *s)
117 {
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;
122
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));
128
129     vp6_coeff_order_table_init(s);
130 }
131
132 static void vp6_parse_vector_models(vp56_context_t *s)
133 {
134     vp56_range_coder_t *c = &s->c;
135     int comp, node;
136
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);
142     }
143
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);
148
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);
153 }
154
155 static void vp6_parse_coeff_models(vp56_context_t *s)
156 {
157     vp56_range_coder_t *c = &s->c;
158     int def_prob[11];
159     int node, cg, ctx, pos;
160     int ct;    /* code type */
161     int pt;    /* plane type (0 for Y, 1 for U or V) */
162
163     memset(def_prob, 0x80, sizeof(def_prob));
164
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];
172             }
173
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);
179     }
180
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);
185
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];
195                     }
196
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);
202 }
203
204 static void vp6_parse_vector_adjustment(vp56_context_t *s, vp56_mv_t *vect)
205 {
206     vp56_range_coder_t *c = &s->c;
207     int comp;
208
209     *vect = (vp56_mv_t) {0,0};
210     if (s->vector_candidate_pos < 2)
211         *vect = s->vector_candidate[0];
212
213     for (comp=0; comp<2; comp++) {
214         int i, delta = 0;
215
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;
221             }
222             if (delta & 0xF0)
223                 delta |= vp56_rac_get_prob(c, s->vector_model_fdv[comp][3])<<3;
224             else
225                 delta |= 8;
226         } else {
227             delta = vp56_rac_get_tree(c, vp56_pva_tree,
228                                       s->vector_model_pdv[comp]);
229         }
230
231         if (delta && vp56_rac_get_prob(c, s->vector_model_sig[comp]))
232             delta = -delta;
233
234         if (!comp)
235             vect->x += delta;
236         else
237             vect->y += delta;
238     }
239 }
240
241 static void vp6_parse_coeff(vp56_context_t *s)
242 {
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) */
249
250     for (b=0; b<6; b++) {
251         int ct = 1;    /* code type */
252         int run = 1;
253
254         if (b > 3) pt = 1;
255
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];
260
261         for (coeff_idx=0; coeff_idx<64; ) {
262             if ((coeff_idx>1 && ct==0) || vp56_rac_get_prob(c, model2[0])) {
263                 /* parse a coeff */
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;
267                 }
268
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;
275                     } else {
276                         if (vp56_rac_get_prob(c, model2[4]))
277                             coeff = 3 + vp56_rac_get_prob(c, model[5]);
278                         else
279                             coeff = 2;
280                     }
281                     ct = 2;
282                 } else {
283                     ct = 1;
284                     coeff = 1;
285                 }
286                 sign = vp56_rac_get(c);
287                 coeff = (coeff ^ -sign) + sign;
288                 if (coeff_idx)
289                     coeff *= s->dequant_ac;
290                 idx = s->coeff_index_to_pos[coeff_idx];
291                 s->block_coeff[b][permute[idx]] = coeff;
292                 run = 1;
293             } else {
294                 /* parse a run */
295                 ct = 0;
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;
299                 } else {
300                     if (!vp56_rac_get_prob(c, model2[1]))
301                         break;
302
303                     model3 = s->coeff_model_runv[coeff_idx >= 6];
304                     run = vp56_rac_get_tree(c, vp6_pcr_tree, model3);
305                     if (!run)
306                         for (run=9, i=0; i<6; i++)
307                             run += vp56_rac_get_prob(c, model3[i+8]) << i;
308                 }
309             }
310
311             cg = vp6_coeff_groups[coeff_idx+=run];
312             model = model2 = s->coeff_model_ract[pt][ct][cg];
313         }
314     }
315 }
316
317 static int vp6_adjust(int v, int t)
318 {
319     int V = v, s = v >> 31;
320     V ^= s;
321     V -= s;
322     if (V-t-1 >= (unsigned)(t-1))
323         return v;
324     V = 2*t - V;
325     V += s;
326     V ^= s;
327     return V;
328 }
329
330 static int vp6_block_variance(uint8_t *src, int stride)
331 {
332     int sum = 0, square_sum = 0;
333     int y, x;
334
335     for (y=0; y<8; y+=2) {
336         for (x=0; x<8; x+=2) {
337             sum += src[x];
338             square_sum += src[x]*src[x];
339         }
340         src += 2*stride;
341     }
342     return (16*square_sum - sum*sum) / (16*16);
343 }
344
345 static void vp6_filter_hv2(vp56_context_t *s, uint8_t *dst, uint8_t *src,
346                            int stride, int delta, int16_t weight)
347 {
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);
351 }
352
353 static void vp6_filter_hv4(uint8_t *dst, uint8_t *src, int stride,
354                            int delta, const int16_t *weights)
355 {
356     int x, y;
357
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);
364         }
365         src += stride;
366         dst += stride;
367     }
368 }
369
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)
372 {
373     uint8_t *tmp = s->edge_emu_buffer+16;
374     int x, xmax;
375
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;
382
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);
386 }
387
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)
390 {
391     int x, y;
392     int tmp[8*11];
393     int *t = tmp;
394
395     src -= stride;
396
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);
403         }
404         src += stride;
405         t += 8;
406     }
407
408     t = tmp + 8;
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);
415         }
416         dst += stride;
417         t += 8;
418     }
419 }
420
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)
424 {
425     int filter4 = 0;
426     int x8 = mv.x & mask;
427     int y8 = mv.y & mask;
428
429     if (luma) {
430         x8 *= 2;
431         y8 *= 2;
432         filter4 = s->filter_mode;
433         if (filter4 == 2) {
434             if (s->max_vector_length &&
435                 (FFABS(mv.x) > s->max_vector_length ||
436                  FFABS(mv.y) > s->max_vector_length)) {
437                 filter4 = 0;
438             } else if (!s->sample_variance_threshold
439                        || (vp6_block_variance(src+offset1, stride)
440                            < s->sample_variance_threshold)) {
441                 filter4 = 0;
442             }
443         }
444     }
445
446     if ((y8 && (offset2-offset1)*s->flip<0) || (!y8 && offset1 > offset2)) {
447         offset1 = offset2;
448     }
449
450     if (filter4) {
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]);
465         }
466     } else {
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);
475         }
476     }
477 }
478
479 static int vp6_decode_init(AVCodecContext *avctx)
480 {
481     vp56_context_t *s = avctx->priv_data;
482
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;
493
494     return 0;
495 }
496
497 AVCodec vp6_decoder = {
498     "vp6",
499     CODEC_TYPE_VIDEO,
500     CODEC_ID_VP6,
501     sizeof(vp56_context_t),
502     vp6_decode_init,
503     NULL,
504     vp56_free,
505     vp56_decode_frame,
506 };
507
508 /* flash version, not flipped upside-down */
509 AVCodec vp6f_decoder = {
510     "vp6f",
511     CODEC_TYPE_VIDEO,
512     CODEC_ID_VP6F,
513     sizeof(vp56_context_t),
514     vp6_decode_init,
515     NULL,
516     vp56_free,
517     vp56_decode_frame,
518 };