30
30
#include "avcodec.h"
31
31
#include "dsputil.h"
32
32
#include "bitstream.h"
33
#include "mpegvideo.h"
36
36
#include "vp56data.h"
37
37
#include "vp6data.h"
40
static int vp6_parse_header(vp56_context_t *s, uint8_t *buf, int buf_size,
40
static void vp6_parse_coeff(VP56Context *s);
41
static void vp6_parse_coeff_huffman(VP56Context *s);
43
static int vp6_parse_header(VP56Context *s, const uint8_t *buf, int buf_size,
43
vp56_range_coder_t *c = &s->c;
46
VP56RangeCoder *c = &s->c;
44
47
int parse_filter_info = 0;
45
48
int coeff_offset = 0;
72
75
/* buf[4] is number of displayed macroblock rows */
73
76
/* buf[5] is number of displayed macroblock cols */
75
if (16*cols != s->avctx->coded_width ||
78
if (!s->macroblocks || /* first frame */
79
16*cols != s->avctx->coded_width ||
76
80
16*rows != s->avctx->coded_height) {
77
81
avcodec_set_dimensions(s->avctx, 16*cols, 16*rows);
78
82
if (s->avctx->extradata_size == 1) {
126
130
s->filter_selection = 16;
130
av_log(s->avctx, AV_LOG_WARNING,
131
"alternative entropy decoding not supported\n");
133
s->use_huffman = vp56_rac_get(c);
135
s->parse_coeff = vp6_parse_coeff;
133
136
if (coeff_offset) {
134
vp56_init_range_decoder(&s->cc, buf+coeff_offset,
135
buf_size-coeff_offset);
138
buf_size -= coeff_offset;
141
if (s->use_huffman) {
142
s->parse_coeff = vp6_parse_coeff_huffman;
143
init_get_bits(&s->gb, buf, buf_size<<3);
145
vp56_init_range_decoder(&s->cc, buf, buf_size);
152
163
s->modelp->coeff_index_to_pos[idx++] = pos;
155
static void vp6_default_models_init(vp56_context_t *s)
166
static void vp6_default_models_init(VP56Context *s)
157
vp56_model_t *model = s->modelp;
168
VP56Model *model = s->modelp;
159
170
model->vector_dct[0] = 0xA2;
160
171
model->vector_dct[1] = 0xA4;
170
181
vp6_coeff_order_table_init(s);
173
static void vp6_parse_vector_models(vp56_context_t *s)
184
static void vp6_parse_vector_models(VP56Context *s)
175
vp56_range_coder_t *c = &s->c;
176
vp56_model_t *model = s->modelp;
186
VP56RangeCoder *c = &s->c;
187
VP56Model *model = s->modelp;
179
190
for (comp=0; comp<2; comp++) {
194
205
model->vector_fdv[comp][node] = vp56_rac_gets_nn(c, 7);
197
static void vp6_parse_coeff_models(vp56_context_t *s)
199
vp56_range_coder_t *c = &s->c;
200
vp56_model_t *model = s->modelp;
208
/* nodes must ascend by count, but with descending symbol order */
209
static int vp6_huff_cmp(const void *va, const void *vb)
211
const Node *a = va, *b = vb;
212
return (a->count - b->count)*16 + (b->sym - a->sym);
215
static void vp6_build_huff_tree(VP56Context *s, uint8_t coeff_model[],
216
const uint8_t *map, unsigned size, VLC *vlc)
218
Node nodes[2*size], *tmp = &nodes[size];
221
/* first compute probabilities from model */
223
for (i=0; i<size-1; i++) {
224
a = tmp[i].count * coeff_model[i] >> 8;
225
b = tmp[i].count * (255 - coeff_model[i]) >> 8;
226
nodes[map[2*i ]].count = a + !a;
227
nodes[map[2*i+1]].count = b + !b;
230
/* then build the huffman tree accodring to probabilities */
231
ff_huff_build_tree(s->avctx, vlc, size, nodes, vp6_huff_cmp,
232
FF_HUFFMAN_FLAG_HNODE_FIRST);
235
static void vp6_parse_coeff_models(VP56Context *s)
237
VP56RangeCoder *c = &s->c;
238
VP56Model *model = s->modelp;
201
239
int def_prob[11];
202
240
int node, cg, ctx, pos;
203
241
int ct; /* code type */
237
275
model->coeff_ract[pt][ct][cg][node] = def_prob[node];
278
if (s->use_huffman) {
279
for (pt=0; pt<2; pt++) {
280
vp6_build_huff_tree(s, model->coeff_dccv[pt],
281
vp6_huff_coeff_map, 12, &s->dccv_vlc[pt]);
282
vp6_build_huff_tree(s, model->coeff_runv[pt],
283
vp6_huff_run_map, 9, &s->runv_vlc[pt]);
284
for (ct=0; ct<3; ct++)
285
for (cg = 0; cg < 6; cg++)
286
vp6_build_huff_tree(s, model->coeff_ract[pt][ct][cg],
287
vp6_huff_coeff_map, 12,
288
&s->ract_vlc[pt][ct][cg]);
290
memset(s->nb_null, 0, sizeof(s->nb_null));
240
292
/* coeff_dcct is a linear combination of coeff_dccv */
241
293
for (pt=0; pt<2; pt++)
242
294
for (ctx=0; ctx<3; ctx++)
243
295
for (node=0; node<5; node++)
244
296
model->coeff_dcct[pt][ctx][node] = av_clip(((model->coeff_dccv[pt][node] * vp6_dccv_lc[ctx][node][0] + 128) >> 8) + vp6_dccv_lc[ctx][node][1], 1, 255);
247
static void vp6_parse_vector_adjustment(vp56_context_t *s, vp56_mv_t *vect)
300
static void vp6_parse_vector_adjustment(VP56Context *s, VP56mv *vect)
249
vp56_range_coder_t *c = &s->c;
250
vp56_model_t *model = s->modelp;
302
VP56RangeCoder *c = &s->c;
303
VP56Model *model = s->modelp;
253
*vect = (vp56_mv_t) {0,0};
306
*vect = (VP56mv) {0,0};
254
307
if (s->vector_candidate_pos < 2)
255
308
*vect = s->vector_candidate[0];
285
static void vp6_parse_coeff(vp56_context_t *s)
287
vp56_range_coder_t *c = s->ccp;
288
vp56_model_t *model = s->modelp;
339
* Read number of consecutive blocks with null DC or AC.
340
* This value is < 74.
342
static unsigned vp6_get_nb_null(VP56Context *s)
344
unsigned val = get_bits(&s->gb, 2);
346
val += get_bits(&s->gb, 2);
348
val = get_bits1(&s->gb) << 2;
349
val = 6+val + get_bits(&s->gb, 2+val);
354
static void vp6_parse_coeff_huffman(VP56Context *s)
356
VP56Model *model = s->modelp;
357
uint8_t *permute = s->scantable.permutated;
359
int coeff, sign, coeff_idx;
361
int pt = 0; /* plane type (0 for Y, 1 for U or V) */
363
for (b=0; b<6; b++) {
364
int ct = 0; /* code type */
366
vlc_coeff = &s->dccv_vlc[pt];
368
for (coeff_idx=0; coeff_idx<64; ) {
370
if (coeff_idx<2 && s->nb_null[coeff_idx][pt]) {
371
s->nb_null[coeff_idx][pt]--;
375
coeff = get_vlc2(&s->gb, vlc_coeff->table, 9, 3);
378
int pt = (coeff_idx >= 6);
379
run += get_vlc2(&s->gb, s->runv_vlc[pt].table, 9, 3);
381
run += get_bits(&s->gb, 6);
383
s->nb_null[0][pt] = vp6_get_nb_null(s);
385
} else if (coeff == 11) { /* end of block */
386
if (coeff_idx == 1) /* first AC coeff ? */
387
s->nb_null[1][pt] = vp6_get_nb_null(s);
390
int coeff2 = vp56_coeff_bias[coeff];
392
coeff2 += get_bits(&s->gb, coeff <= 9 ? coeff - 4 : 11);
393
ct = 1 + (coeff2 > 1);
394
sign = get_bits1(&s->gb);
395
coeff2 = (coeff2 ^ -sign) + sign;
397
coeff2 *= s->dequant_ac;
398
idx = model->coeff_index_to_pos[coeff_idx];
399
s->block_coeff[b][permute[idx]] = coeff2;
403
cg = FFMIN(vp6_coeff_groups[coeff_idx], 3);
404
vlc_coeff = &s->ract_vlc[pt][ct][cg];
409
static void vp6_parse_coeff(VP56Context *s)
411
VP56RangeCoder *c = s->ccp;
412
VP56Model *model = s->modelp;
289
413
uint8_t *permute = s->scantable.permutated;
290
414
uint8_t *model1, *model2, *model3;
291
415
int coeff, sign, coeff_idx;
309
433
if (vp56_rac_get_prob(c, model2[2])) {
310
434
if (vp56_rac_get_prob(c, model2[3])) {
311
435
idx = vp56_rac_get_tree(c, vp56_pc_tree, model1);
312
coeff = vp56_coeff_bias[idx];
436
coeff = vp56_coeff_bias[idx+5];
313
437
for (i=vp56_coeff_bit_length[idx]; i>=0; i--)
314
438
coeff += vp56_rac_get_prob(c, vp56_coeff_parse_table[idx][i]) << i;
402
static void vp6_filter_diag2(vp56_context_t *s, uint8_t *dst, uint8_t *src,
526
static void vp6_filter_diag2(VP56Context *s, uint8_t *dst, uint8_t *src,
403
527
int stride, int h_weight, int v_weight)
405
529
uint8_t *tmp = s->edge_emu_buffer+16;
407
531
s->dsp.put_h264_chroma_pixels_tab[0](dst, tmp, stride, 8, 0, v_weight);
410
static void vp6_filter_diag4(uint8_t *dst, uint8_t *src, int stride,
411
const int16_t *h_weights,const int16_t *v_weights)
419
for (y=0; y<11; y++) {
420
for (x=0; x<8; x++) {
421
t[x] = av_clip_uint8(( src[x-1] * h_weights[0]
422
+ src[x ] * h_weights[1]
423
+ src[x+1] * h_weights[2]
424
+ src[x+2] * h_weights[3] + 64) >> 7);
431
for (y=0; y<8; y++) {
432
for (x=0; x<8; x++) {
433
dst[x] = av_clip_uint8(( t[x-8 ] * v_weights[0]
434
+ t[x ] * v_weights[1]
435
+ t[x+8 ] * v_weights[2]
436
+ t[x+16] * v_weights[3] + 64) >> 7);
443
static void vp6_filter(vp56_context_t *s, uint8_t *dst, uint8_t *src,
534
static void vp6_filter(VP56Context *s, uint8_t *dst, uint8_t *src,
444
535
int offset1, int offset2, int stride,
445
vp56_mv_t mv, int mask, int select, int luma)
536
VP56mv mv, int mask, int select, int luma)
448
539
int x8 = mv.x & mask;
477
568
vp6_filter_hv4(dst, src+offset1, stride, stride,
478
569
vp6_block_copy_filter[select][y8]);
480
vp6_filter_diag4(dst, src+offset1 + ((mv.x^mv.y)>>31), stride,
571
s->dsp.vp6_filter_diag4(dst, src+offset1+((mv.x^mv.y)>>31), stride,
481
572
vp6_block_copy_filter[select][x8],
482
573
vp6_block_copy_filter[select][y8]);
493
static int vp6_decode_init(AVCodecContext *avctx)
584
static av_cold int vp6_decode_init(AVCodecContext *avctx)
495
vp56_context_t *s = avctx->priv_data;
586
VP56Context *s = avctx->priv_data;
497
588
vp56_init(avctx, avctx->codec->id == CODEC_ID_VP6,
498
589
avctx->codec->id == CODEC_ID_VP6A);