1 /*
2 * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org>
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 /**
22 * @file
23 * VP6 compatible video decoder
24 *
25 * The VP6F decoder accepts an optional 1 byte extradata. It is composed of:
26 * - upper 4 bits: difference between encoded width and visible width
27 * - lower 4 bits: difference between encoded height and visible height
28 */
29
30 #include <stdlib.h>
31
36
40
41 #define VP6_MAX_HUFF_SIZE 12
42
45
47 {
49 int parse_filter_info = 0;
50 int coeff_offset = 0;
51 int vrt_shift = 0;
52 int sub_version;
53 int rows, cols;
54 int res = 0;
55 int ret;
56 int separated_coeff = buf[0] & 1;
57
60
62 sub_version = buf[1] >> 3;
63 if (sub_version > 8)
65 s->filter_header = buf[1] & 0x06;
66 if (buf[1] & 1) {
69 }
70 if (separated_coeff || !s->filter_header) {
71 coeff_offset =
AV_RB16(buf+2) - 2;
72 buf += 2;
73 buf_size -= 2;
74 }
75
76 rows = buf[2]; /* number of stored macroblock rows */
77 cols = buf[3]; /* number of stored macroblock cols */
78 /* buf[4] is number of displayed macroblock rows */
79 /* buf[5] is number of displayed macroblock cols */
80 if (!rows || !cols) {
83 }
84
85 if (!s->macroblocks || /* first frame */
86 16*cols != s->avctx->coded_width ||
87 16*rows != s->avctx->coded_height) {
88 if (s->avctx->extradata_size == 0 &&
89 FFALIGN(s->avctx->width, 16) == 16 * cols &&
90 FFALIGN(s->avctx->height, 16) == 16 * rows) {
91 // We assume this is properly signalled container cropping,
92 // in an F4V file. Just set the coded_width/height, don't
93 // touch the cropped ones.
94 s->avctx->coded_width = 16 * cols;
95 s->avctx->coded_height = 16 * rows;
96 } else {
98 if (ret < 0)
99 return ret;
100
101 if (s->avctx->extradata_size == 1) {
102 s->avctx->width -= s->avctx->extradata[0] >> 4;
103 s->avctx->height -= s->avctx->extradata[0] & 0x0F;
104 }
105 }
107 }
108
110 if (ret < 0)
113
114 parse_filter_info = s->filter_header;
115 if (sub_version < 8)
116 vrt_shift = 5;
117 s->sub_version = sub_version;
118 s->golden_frame = 0;
119 } else {
120 if (!s->sub_version || !s->avctx->coded_width || !s->avctx->coded_height)
122
123 if (separated_coeff || !s->filter_header) {
124 coeff_offset =
AV_RB16(buf+1) - 2;
125 buf += 2;
126 buf_size -= 2;
127 }
129 if (ret < 0)
130 return ret;
131
133 if (s->filter_header) {
135 if (s->deblock_filtering)
137 if (s->sub_version > 7)
139 }
140 }
141
142 if (parse_filter_info) {
144 s->filter_mode = 2;
145 s->sample_variance_threshold =
vp56_rac_gets(c, 5) << vrt_shift;
148 s->filter_mode = 1;
149 } else {
150 s->filter_mode = 0;
151 }
152 if (s->sub_version > 7)
154 else
155 s->filter_selection = 16;
156 }
157
159
161 if (coeff_offset) {
162 buf += coeff_offset;
163 buf_size -= coeff_offset;
164 if (buf_size < 0) {
167 }
168 if (s->use_huffman) {
171 } else {
173 if (ret < 0)
175 s->ccp = &s->cc;
176 }
177 } else {
178 s->ccp = &s->c;
179 }
180
181 return res;
185 return ret;
186 }
187
189 {
190 int i, pos, idx = 1;
191
192 s->modelp->coeff_index_to_pos[0] = 0;
193 for (i=0; i<16; i++)
194 for (pos=1; pos<64; pos++)
195 if (s->modelp->coeff_reorder[pos] == i)
196 s->modelp->coeff_index_to_pos[idx++] = pos;
197 }
198
200 {
202
207
213
215 }
216
218 {
222
223 for (comp=0; comp<2; comp++) {
228 }
229
230 for (comp=0; comp<2; comp++)
231 for (node=0; node<7; node++)
234
235 for (comp=0; comp<2; comp++)
236 for (node=0; node<8; node++)
239 }
240
241 /* nodes must ascend by count, but with descending symbol order */
243 {
244 const Node *
a = va, *
b = vb;
245 return (a->
count - b->count)*16 + (b->sym - a->
sym);
246 }
247
250 {
253
254 /* first compute probabilities from model */
256 for (i=0; i<size-1; i++) {
257 a = tmp[i].
count * coeff_model[i] >> 8;
258 b = tmp[i].
count * (255 - coeff_model[i]) >> 8;
259 nodes[map[2*i ]].
count = a + !
a;
260 nodes[map[2*i+1]].
count = b + !
b;
261 }
262
264 /* then build the huffman tree according to probabilities */
268 }
269
271 {
274 int def_prob[11];
275 int node, cg,
ctx, pos;
276 int ct; /* code type */
277 int pt;
/* plane type (0 for Y, 1 for U or V) */
278
279 memset(def_prob, 0x80, sizeof(def_prob));
280
281 for (pt=0; pt<2; pt++)
282 for (node=0; node<11; node++)
288 }
289
291 for (pos=1; pos<64; pos++)
295 }
296
297 for (cg=0; cg<2; cg++)
298 for (node=0; node<14; node++)
301
302 for (ct=0; ct<3; ct++)
303 for (pt=0; pt<2; pt++)
304 for (cg=0; cg<6; cg++)
305 for (node=0; node<11; node++)
311 }
312
313 if (s->use_huffman) {
314 for (pt=0; pt<2; pt++) {
317 return -1;
320 return -1;
321 for (ct=0; ct<3; ct++)
322 for (cg = 0; cg < 6; cg++)
325 &s->ract_vlc[pt][ct][cg]))
326 return -1;
327 }
328 memset(s->nb_null, 0, sizeof(s->nb_null));
329 } else {
330 /* coeff_dcct is a linear combination of coeff_dccv */
331 for (pt=0; pt<2; pt++)
332 for (ctx=0; ctx<3; ctx++)
333 for (node=0; node<5; node++)
335 }
336 return 0;
337 }
338
340 {
344
346 if (s->vector_candidate_pos < 2)
347 *vect = s->vector_candidate[0];
348
349 for (comp=0; comp<2; comp++) {
351
353 static const uint8_t prob_order[] = {0, 1, 2, 7, 6, 5, 4};
354 for (i=0; i<sizeof(prob_order); i++) {
355 int j = prob_order[i];
357 }
358 if (delta & 0xF0)
360 else
361 delta |= 8;
362 } else {
365 }
366
369
370 if (!comp)
372 else
374 }
375 }
376
377 /**
378 * Read number of consecutive blocks with null DC or AC.
379 * This value is < 74.
380 */
382 {
384 if (val == 2)
386 else if (val == 3) {
388 val = 6+val +
get_bits(&s->gb, 2+val);
389 }
391 }
392
394 {
398 int coeff, sign, coeff_idx;
400 int pt = 0;
/* plane type (0 for Y, 1 for U or V) */
401
402 for (b=0; b<6; b++) {
403 int ct = 0; /* code type */
404 if (b > 3) pt = 1;
405 vlc_coeff = &s->dccv_vlc[
pt];
406
407 for (coeff_idx = 0;;) {
409 if (coeff_idx<2 && s->nb_null[coeff_idx][pt]) {
410 s->nb_null[coeff_idx][
pt]--;
411 if (coeff_idx)
412 break;
413 } else {
417 if (coeff == 0) {
418 if (coeff_idx) {
419 int pt = (coeff_idx >= 6);
421 if (run >= 9)
423 } else
425 ct = 0;
426 } else if (coeff == 11) { /* end of block */
427 if (coeff_idx == 1) /* first AC coeff ? */
429 break;
430 } else {
432 if (coeff > 4)
433 coeff2 +=
get_bits(&s->gb, coeff <= 9 ? coeff - 4 : 11);
434 ct = 1 + (coeff2 > 1);
436 coeff2 = (coeff2 ^ -sign) + sign;
437 if (coeff_idx)
438 coeff2 *= s->dequant_ac;
440 s->block_coeff[
b][permute[idx]] = coeff2;
441 }
442 }
444 if (coeff_idx >= 64)
445 break;
447 vlc_coeff = &s->ract_vlc[
pt][ct][cg];
448 }
449 }
450 return 0;
451 }
452
454 {
458 uint8_t *model1, *model2, *model3;
459 int coeff, sign, coeff_idx;
460 int b, i, cg, idx,
ctx;
461 int pt = 0;
/* plane type (0 for Y, 1 for U or V) */
462
466 }
467
468 for (b=0; b<6; b++) {
469 int ct = 1; /* code type */
471
472 if (b > 3) pt = 1;
473
475 + s->above_blocks[s->above_block_idx[
b]].not_null_dc;
478
479 coeff_idx = 0;
480 for (;;) {
482 /* parse a coeff */
489 } else {
492 else
493 coeff = 2;
494 }
495 ct = 2;
496 } else {
497 ct = 1;
498 coeff = 1;
499 }
501 coeff = (coeff ^ -sign) + sign;
502 if (coeff_idx)
503 coeff *= s->dequant_ac;
505 s->block_coeff[
b][permute[idx]] =
coeff;
506 run = 1;
507 } else {
508 /* parse a run */
509 ct = 0;
510 if (coeff_idx > 0) {
512 break;
513
516 if (!run)
517 for (run=9, i=0; i<6; i++)
519 }
520 }
522 if (coeff_idx >= 64)
523 break;
526 }
527
529 s->above_blocks[s->above_block_idx[
b]].not_null_dc = !!s->block_coeff[
b][0];
530 }
531 return 0;
532 }
533
535 {
536 int sum = 0, square_sum = 0;
537 int y, x;
538
539 for (y=0; y<8; y+=2) {
540 for (x=0; x<8; x+=2) {
541 sum += src[x];
542 square_sum += src[x]*src[x];
543 }
545 }
546 return (16*square_sum - sum*sum) >> 8;
547 }
548
550 int delta,
const int16_t *weights)
551 {
552 int x, y;
553
554 for (y=0; y<8; y++) {
555 for (x=0; x<8; x++) {
556 dst[x] = av_clip_uint8(( src[x-delta ] * weights[0]
557 + src[x ] * weights[1]
558 + src[x+delta ] * weights[2]
559 + src[x+2*delta] * weights[3] + 64) >> 7);
560 }
563 }
564 }
565
567 ptrdiff_t
stride,
int h_weight,
int v_weight)
568 {
570 s->h264chroma.put_h264_chroma_pixels_tab[0](
tmp,
src,
stride, 9, h_weight, 0);
571 s->h264chroma.put_h264_chroma_pixels_tab[0](dst,
tmp,
stride, 8, 0, v_weight);
572 }
573
575 int offset1,
int offset2, ptrdiff_t
stride,
577 {
578 int filter4 = 0;
579 int x8 = mv.
x &
mask;
580 int y8 = mv.
y &
mask;
581
582 if (luma) {
583 x8 *= 2;
584 y8 *= 2;
585 filter4 = s->filter_mode;
586 if (filter4 == 2) {
587 if (s->max_vector_length &&
588 (
FFABS(mv.
x) > s->max_vector_length ||
589 FFABS(mv.
y) > s->max_vector_length)) {
590 filter4 = 0;
591 } else if (s->sample_variance_threshold
593 < s->sample_variance_threshold)) {
594 filter4 = 0;
595 }
596 }
597 }
598
599 if ((y8 && (offset2-offset1)*s->flip<0) || (!y8 && offset1 > offset2)) {
600 offset1 = offset2;
601 }
602
603 if (filter4) {
604 if (!y8) { /* left or right combine */
607 } else if (!x8) { /* above or below combine */
610 } else {
611 s->vp56dsp.vp6_filter_diag4(dst, src+offset1+((mv.
x^mv.
y)>>31), stride,
614 }
615 } else {
616 if (!x8 || !y8) {
617 s->h264chroma.put_h264_chroma_pixels_tab[0](dst, src + offset1,
stride, 8, x8, y8);
618 } else {
620 }
621 }
622 }
623
625
627 {
629 int ret;
630
633 return ret;
635
637
638 if (s->has_alpha) {
639 s->alpha_context =
av_mallocz(
sizeof(VP56Context));
641 s->flip == -1, s->has_alpha);
644 }
645
646 return 0;
647 }
648
650 {
651 s->deblock_filtering = 0;
659 }
660
662
664 {
666
669
670 if (s->alpha_context) {
674 }
675
676 return 0;
677 }
678
680 {
682
683 for (pt=0; pt<2; pt++) {
686 for (ct=0; ct<3; ct++)
687 for (cg=0; cg<6; cg++)
689 }
690 }
691
697 .priv_data_size = sizeof(VP56Context),
702 };
703
704 /* flash version, not flipped upside-down */
710 .priv_data_size = sizeof(VP56Context),
715 };
716
717 /* flash version, not flipped upside-down, with alpha channel */
723 .priv_data_size = sizeof(VP56Context),
728 };
av_cold int ff_vp56_free(AVCodecContext *avctx)
const struct AVCodec * codec
const char const char void * val
static void vp6_coeff_order_table_init(VP56Context *s)
uint8_t coeff_ract[2][3][6][11]
static const uint8_t vp6_ract_pct[3][2][6][11]
static av_cold int vp6_decode_init(AVCodecContext *avctx)
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
#define FF_HUFFMAN_FLAG_HNODE_FIRST
static const uint8_t vp6_runv_pct[2][14]
static int vp6_build_huff_tree(VP56Context *s, uint8_t coeff_model[], const uint8_t *map, unsigned size, VLC *vlc)
uint8_t coeff_index_to_pos[64]
VP5 and VP6 compatible video decoder (common features)
static const int16_t vp6_block_copy_filter[17][8][4]
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
static const uint8_t vp6_def_fdv_vector_model[2][8]
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Check that the provided frame dimensions are valid and set them on the codec context.
const uint8_t ff_vp56_coeff_bias[]
uint8_t coeff_dccv[2][11]
int ff_huff_build_tree(AVCodecContext *avctx, VLC *vlc, int nb_codes, int nb_bits, Node *nodes, HuffCmp cmp, int flags)
nodes size must be 2*nb_codes first nb_codes nodes.count must be set
static av_cold int init(AVCodecContext *avctx)
uint8_t mb_types_stats[3][10][2]
static int vp6_parse_coeff(VP56Context *s)
static void vp6_filter_diag2(VP56Context *s, uint8_t *dst, uint8_t *src, ptrdiff_t stride, int h_weight, int v_weight)
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_RB16
static const uint8_t vp6_huff_coeff_map[]
av_cold int ff_vp56_init(AVCodecContext *avctx, int flip, int has_alpha)
static av_cold void vp6_decode_free_context(VP56Context *s)
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
static const uint8_t vp6_def_runv_coeff_model[2][14]
static const uint8_t vp6_def_coeff_reorder[]
static int vp6_parse_coeff_models(VP56Context *s)
av_cold int ff_vp56_init_context(AVCodecContext *avctx, VP56Context *s, int flip, int has_alpha)
#define VP6_MAX_HUFF_SIZE
bitstream reader API header.
static const uint8_t vp6_dccv_pct[2][11]
static av_always_inline int vp56_rac_get_tree(VP56RangeCoder *c, const VP56Tree *tree, const uint8_t *probs)
uint8_t coeff_reorder[64]
static int get_bits_left(GetBitContext *gb)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
static const uint16_t mask[17]
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
static av_always_inline int vp56_rac_get(VP56RangeCoder *c)
const char * name
Name of the codec implementation.
void ff_vp56_init_dequant(VP56Context *s, int quantizer)
static void vp6_filter(VP56Context *s, uint8_t *dst, uint8_t *src, int offset1, int offset2, ptrdiff_t stride, VP56mv mv, int mask, int select, int luma)
const uint8_t ff_vp56_b6to4[]
static int vp6_huff_cmp(const void *va, const void *vb)
static void vp6_parse_vector_models(VP56Context *s)
int ff_vp56_init_range_decoder(VP56RangeCoder *c, const uint8_t *buf, int buf_size)
VP5 and VP6 compatible video decoder (common data)
static av_unused int vp56_rac_gets_nn(VP56RangeCoder *c, int bits)
static av_cold void vp6_decode_init_context(VP56Context *s)
static void vp6_filter_hv4(uint8_t *dst, uint8_t *src, ptrdiff_t stride, int delta, const int16_t *weights)
VP6 compatible video decoder.
static void vp6_default_models_init(VP56Context *s)
static const uint8_t vp6_huff_run_map[]
int ff_vp56_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
const uint8_t ff_vp56_coeff_bit_length[]
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE(*table)[2], int bits, int max_depth)
Parse a vlc code.
static int vp6_block_variance(uint8_t *src, ptrdiff_t stride)
#define vp56_rac_get_prob
static const VP56Tree vp6_pcr_tree[]
uint8_t coeff_runv[2][14]
static void comp(unsigned char *dst, ptrdiff_t dst_stride, unsigned char *src, ptrdiff_t src_stride, int add)
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
#define AV_CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
static const int8_t mv[256][2]
static av_always_inline int vp56_rac_get_prob_branchy(VP56RangeCoder *c, int prob)
void ff_vp6dsp_init(VP56DSPContext *s)
Libavcodec external API header.
static const uint8_t vp6_coeff_reorder_pct[]
const VP56Tree ff_vp56_pc_tree[]
main external API structure.
static const uint8_t vp6_fdv_pct[2][8]
static const uint8_t vp6_coord_div[]
static void permute(uint8_t *dst, const uint8_t *src, const uint8_t permutation[64])
static unsigned vp6_get_nb_null(VP56Context *s)
Read number of consecutive blocks with null DC or AC.
static const uint8_t vp6_sig_dct_pct[2][2]
const uint8_t ff_vp56_coeff_parse_table[6][11]
uint8_t coeff_dcct[2][36][5]
static unsigned int get_bits1(GetBitContext *s)
static int vp6_parse_header(VP56Context *s, const uint8_t *buf, int buf_size)
static av_cold int vp6_decode_free(AVCodecContext *avctx)
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
huffman tree builder and VLC generator
const VDPAUPixFmtMap * map
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
av_cold int ff_vp56_free_context(VP56Context *s)
static const int vp6_dccv_lc[3][5][2]
const VP56Tree ff_vp56_pva_tree[]
GLint GLenum GLboolean GLsizei stride
common internal api header.
static void vp6_parse_vector_adjustment(VP56Context *s, VP56mv *vect)
static const uint8_t vp6_coeff_groups[]
static const uint8_t vp6_def_pdv_vector_model[2][7]
static int vp6_parse_coeff_huffman(VP56Context *s)
VLC_TYPE(* table)[2]
code, bits
static const double coeff[2][5]
const uint8_t ff_vp56_def_mb_types_stats[3][10][2]
void ff_free_vlc(VLC *vlc)
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
static const uint8_t vp6_pdv_pct[2][7]
static int vp56_rac_gets(VP56RangeCoder *c, int bits)