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