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