Mercurial > libavcodec.hg
comparison vp6.c @ 5821:51918cb97f6f libavcodec
add support for VP6 with huffman encoded blocks
closes issue 104
author | aurel |
---|---|
date | Sun, 14 Oct 2007 22:30:59 +0000 |
parents | 314be1cfdcb0 |
children | 3413fce9e26a |
comparison
equal
deleted
inserted
replaced
5820:ffac546a3861 | 5821:51918cb97f6f |
---|---|
28 #include <stdlib.h> | 28 #include <stdlib.h> |
29 | 29 |
30 #include "avcodec.h" | 30 #include "avcodec.h" |
31 #include "dsputil.h" | 31 #include "dsputil.h" |
32 #include "bitstream.h" | 32 #include "bitstream.h" |
33 #include "huffman.h" | |
33 #include "mpegvideo.h" | 34 #include "mpegvideo.h" |
34 | 35 |
35 #include "vp56.h" | 36 #include "vp56.h" |
36 #include "vp56data.h" | 37 #include "vp56data.h" |
37 #include "vp6data.h" | 38 #include "vp6data.h" |
38 | 39 |
40 | |
41 static void vp6_parse_coeff(vp56_context_t *s); | |
42 static void vp6_parse_coeff_huffman(vp56_context_t *s); | |
39 | 43 |
40 static int vp6_parse_header(vp56_context_t *s, uint8_t *buf, int buf_size, | 44 static int vp6_parse_header(vp56_context_t *s, uint8_t *buf, int buf_size, |
41 int *golden_frame) | 45 int *golden_frame) |
42 { | 46 { |
43 vp56_range_coder_t *c = &s->c; | 47 vp56_range_coder_t *c = &s->c; |
124 s->filter_selection = vp56_rac_gets(c, 4); | 128 s->filter_selection = vp56_rac_gets(c, 4); |
125 else | 129 else |
126 s->filter_selection = 16; | 130 s->filter_selection = 16; |
127 } | 131 } |
128 | 132 |
129 if (vp56_rac_get(c)) | 133 s->use_huffman = vp56_rac_get(c); |
130 av_log(s->avctx, AV_LOG_WARNING, | 134 |
131 "alternative entropy decoding not supported\n"); | 135 s->parse_coeff = vp6_parse_coeff; |
132 | |
133 if (coeff_offset) { | 136 if (coeff_offset) { |
134 vp56_init_range_decoder(&s->cc, buf+coeff_offset, | 137 buf += coeff_offset; |
135 buf_size-coeff_offset); | 138 buf_size -= coeff_offset; |
136 s->ccp = &s->cc; | 139 if (s->use_huffman) { |
140 s->parse_coeff = vp6_parse_coeff_huffman; | |
141 init_get_bits(&s->gb, buf, buf_size); | |
142 } else { | |
143 vp56_init_range_decoder(&s->cc, buf, buf_size); | |
144 s->ccp = &s->cc; | |
145 } | |
137 } else { | 146 } else { |
138 s->ccp = &s->c; | 147 s->ccp = &s->c; |
139 } | 148 } |
140 | 149 |
141 return res; | 150 return res; |
190 | 199 |
191 for (comp=0; comp<2; comp++) | 200 for (comp=0; comp<2; comp++) |
192 for (node=0; node<8; node++) | 201 for (node=0; node<8; node++) |
193 if (vp56_rac_get_prob(c, vp6_fdv_pct[comp][node])) | 202 if (vp56_rac_get_prob(c, vp6_fdv_pct[comp][node])) |
194 model->vector_fdv[comp][node] = vp56_rac_gets_nn(c, 7); | 203 model->vector_fdv[comp][node] = vp56_rac_gets_nn(c, 7); |
204 } | |
205 | |
206 static int vp6_huff_cmp(const void *va, const void *vb) | |
207 { | |
208 const Node *a = va, *b = vb; | |
209 return a->count >= b->count; | |
210 } | |
211 | |
212 static void vp6_build_huff_tree(vp56_context_t *s, uint8_t coeff_model[], | |
213 const uint8_t *map, unsigned size, VLC *vlc) | |
214 { | |
215 Node nodes[2*size], *tmp = &nodes[size]; | |
216 int a, b, i; | |
217 | |
218 /* first compute probabilities from model */ | |
219 tmp[0].count = 256; | |
220 for (i=0; i<size-1; i++) { | |
221 a = tmp[i].count * coeff_model[i] >> 8; | |
222 b = tmp[i].count * (255 - coeff_model[i]) >> 8; | |
223 nodes[map[2*i ]].count = a + !a; | |
224 nodes[map[2*i+1]].count = b + !b; | |
225 } | |
226 | |
227 /* then build the huffman tree accodring to probabilities */ | |
228 ff_huff_build_tree(s->avctx, vlc, size, nodes, vp6_huff_cmp, 1); | |
195 } | 229 } |
196 | 230 |
197 static void vp6_parse_coeff_models(vp56_context_t *s) | 231 static void vp6_parse_coeff_models(vp56_context_t *s) |
198 { | 232 { |
199 vp56_range_coder_t *c = &s->c; | 233 vp56_range_coder_t *c = &s->c; |
235 model->coeff_ract[pt][ct][cg][node] = def_prob[node]; | 269 model->coeff_ract[pt][ct][cg][node] = def_prob[node]; |
236 } else if (s->framep[VP56_FRAME_CURRENT]->key_frame) { | 270 } else if (s->framep[VP56_FRAME_CURRENT]->key_frame) { |
237 model->coeff_ract[pt][ct][cg][node] = def_prob[node]; | 271 model->coeff_ract[pt][ct][cg][node] = def_prob[node]; |
238 } | 272 } |
239 | 273 |
274 if (s->use_huffman) { | |
275 for (pt=0; pt<2; pt++) { | |
276 vp6_build_huff_tree(s, model->coeff_dccv[pt], | |
277 vp6_huff_coeff_map, 12, &s->dccv_vlc[pt]); | |
278 vp6_build_huff_tree(s, model->coeff_runv[pt], | |
279 vp6_huff_run_map, 9, &s->runv_vlc[pt]); | |
280 for (ct=0; ct<3; ct++) | |
281 for (cg = 0; cg < 6; cg++) | |
282 vp6_build_huff_tree(s, model->coeff_ract[pt][ct][cg], | |
283 vp6_huff_coeff_map, 12, | |
284 &s->ract_vlc[pt][ct][cg]); | |
285 } | |
286 memset(s->nb_null, 0, sizeof(s->nb_null)); | |
287 } else { | |
240 /* coeff_dcct is a linear combination of coeff_dccv */ | 288 /* coeff_dcct is a linear combination of coeff_dccv */ |
241 for (pt=0; pt<2; pt++) | 289 for (pt=0; pt<2; pt++) |
242 for (ctx=0; ctx<3; ctx++) | 290 for (ctx=0; ctx<3; ctx++) |
243 for (node=0; node<5; node++) | 291 for (node=0; node<5; node++) |
244 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); | 292 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); |
293 } | |
245 } | 294 } |
246 | 295 |
247 static void vp6_parse_vector_adjustment(vp56_context_t *s, vp56_mv_t *vect) | 296 static void vp6_parse_vector_adjustment(vp56_context_t *s, vp56_mv_t *vect) |
248 { | 297 { |
249 vp56_range_coder_t *c = &s->c; | 298 vp56_range_coder_t *c = &s->c; |
280 else | 329 else |
281 vect->y += delta; | 330 vect->y += delta; |
282 } | 331 } |
283 } | 332 } |
284 | 333 |
334 /** | |
335 * Read number of consecutive blocks with null DC or AC. | |
336 * This value is < 74. | |
337 */ | |
338 static unsigned vp6_get_nb_null(vp56_context_t *s) | |
339 { | |
340 unsigned val = get_bits(&s->gb, 2); | |
341 if (val == 2) | |
342 val += get_bits(&s->gb, 2); | |
343 else if (val == 3) { | |
344 val = get_bits1(&s->gb) << 2; | |
345 val = 6+val + get_bits(&s->gb, 2+val); | |
346 } | |
347 return val; | |
348 } | |
349 | |
350 static void vp6_parse_coeff_huffman(vp56_context_t *s) | |
351 { | |
352 vp56_model_t *model = s->modelp; | |
353 uint8_t *permute = s->scantable.permutated; | |
354 VLC *vlc_coeff; | |
355 int coeff, sign, coeff_idx; | |
356 int b, cg, idx; | |
357 int pt = 0; /* plane type (0 for Y, 1 for U or V) */ | |
358 | |
359 for (b=0; b<6; b++) { | |
360 int ct = 0; /* code type */ | |
361 if (b > 3) pt = 1; | |
362 vlc_coeff = &s->dccv_vlc[pt]; | |
363 | |
364 for (coeff_idx=0; coeff_idx<64; ) { | |
365 int run = 1; | |
366 if (coeff_idx<2 && s->nb_null[coeff_idx][pt]) { | |
367 s->nb_null[coeff_idx][pt]--; | |
368 if (coeff_idx) | |
369 break; | |
370 } else { | |
371 coeff = get_vlc2(&s->gb, vlc_coeff->table, 9, 3); | |
372 if (coeff == 0) { | |
373 if (coeff_idx) { | |
374 int pt = (coeff_idx >= 6); | |
375 run += get_vlc2(&s->gb, s->runv_vlc[pt].table, 9, 3); | |
376 if (run >= 9) | |
377 run += get_bits(&s->gb, 6); | |
378 } else | |
379 s->nb_null[0][pt] = vp6_get_nb_null(s); | |
380 ct = 0; | |
381 } else if (coeff == 11) { /* end of block */ | |
382 if (coeff_idx == 1) /* first AC coeff ? */ | |
383 s->nb_null[1][pt] = vp6_get_nb_null(s); | |
384 break; | |
385 } else { | |
386 int coeff2 = vp56_coeff_bias[coeff]; | |
387 if (coeff > 4) | |
388 coeff2 += get_bits(&s->gb, coeff <= 9 ? coeff - 4 : 11); | |
389 ct = 1 + (coeff2 > 1); | |
390 sign = get_bits1(&s->gb); | |
391 coeff2 = (coeff2 ^ -sign) + sign; | |
392 if (coeff_idx) | |
393 coeff2 *= s->dequant_ac; | |
394 idx = model->coeff_index_to_pos[coeff_idx]; | |
395 s->block_coeff[b][permute[idx]] = coeff2; | |
396 } | |
397 } | |
398 coeff_idx+=run; | |
399 cg = FFMIN(vp6_coeff_groups[coeff_idx], 3); | |
400 vlc_coeff = &s->ract_vlc[pt][ct][cg]; | |
401 } | |
402 } | |
403 } | |
404 | |
285 static void vp6_parse_coeff(vp56_context_t *s) | 405 static void vp6_parse_coeff(vp56_context_t *s) |
286 { | 406 { |
287 vp56_range_coder_t *c = s->ccp; | 407 vp56_range_coder_t *c = s->ccp; |
288 vp56_model_t *model = s->modelp; | 408 vp56_model_t *model = s->modelp; |
289 uint8_t *permute = s->scantable.permutated; | 409 uint8_t *permute = s->scantable.permutated; |
307 if ((coeff_idx>1 && ct==0) || vp56_rac_get_prob(c, model2[0])) { | 427 if ((coeff_idx>1 && ct==0) || vp56_rac_get_prob(c, model2[0])) { |
308 /* parse a coeff */ | 428 /* parse a coeff */ |
309 if (vp56_rac_get_prob(c, model2[2])) { | 429 if (vp56_rac_get_prob(c, model2[2])) { |
310 if (vp56_rac_get_prob(c, model2[3])) { | 430 if (vp56_rac_get_prob(c, model2[3])) { |
311 idx = vp56_rac_get_tree(c, vp56_pc_tree, model1); | 431 idx = vp56_rac_get_tree(c, vp56_pc_tree, model1); |
312 coeff = vp56_coeff_bias[idx]; | 432 coeff = vp56_coeff_bias[idx+5]; |
313 for (i=vp56_coeff_bit_length[idx]; i>=0; i--) | 433 for (i=vp56_coeff_bit_length[idx]; i>=0; i--) |
314 coeff += vp56_rac_get_prob(c, vp56_coeff_parse_table[idx][i]) << i; | 434 coeff += vp56_rac_get_prob(c, vp56_coeff_parse_table[idx][i]) << i; |
315 } else { | 435 } else { |
316 if (vp56_rac_get_prob(c, model2[4])) | 436 if (vp56_rac_get_prob(c, model2[4])) |
317 coeff = 3 + vp56_rac_get_prob(c, model1[5]); | 437 coeff = 3 + vp56_rac_get_prob(c, model1[5]); |
498 avctx->codec->id == CODEC_ID_VP6A); | 618 avctx->codec->id == CODEC_ID_VP6A); |
499 s->vp56_coord_div = vp6_coord_div; | 619 s->vp56_coord_div = vp6_coord_div; |
500 s->parse_vector_adjustment = vp6_parse_vector_adjustment; | 620 s->parse_vector_adjustment = vp6_parse_vector_adjustment; |
501 s->adjust = vp6_adjust; | 621 s->adjust = vp6_adjust; |
502 s->filter = vp6_filter; | 622 s->filter = vp6_filter; |
503 s->parse_coeff = vp6_parse_coeff; | |
504 s->default_models_init = vp6_default_models_init; | 623 s->default_models_init = vp6_default_models_init; |
505 s->parse_vector_models = vp6_parse_vector_models; | 624 s->parse_vector_models = vp6_parse_vector_models; |
506 s->parse_coeff_models = vp6_parse_coeff_models; | 625 s->parse_coeff_models = vp6_parse_coeff_models; |
507 s->parse_header = vp6_parse_header; | 626 s->parse_header = vp6_parse_header; |
508 | 627 |