2 * VC-1 and WMV3 decoder
3 * Copyright (c) 2011 Mashiat Sarker Shakkhar
4 * Copyright (c) 2006-2007 Konstantin Shishkov
5 * Partly based on vc9.c (c) 2005 Anonymous, Alex Beregszaszi, Michael Niedermayer
7 * This file is part of Libav.
9 * Libav 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.
14 * Libav 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.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with Libav; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 * VC-1 and WMV3 decoder
35 #include "mpegvideo.h"
37 #include "msmpeg4data.h"
42 #if CONFIG_WMV3IMAGE_DECODER || CONFIG_VC1IMAGE_DECODER
44 typedef struct SpriteData {
46 * Transform coefficients for both sprites in 16.16 fixed point format,
47 * in the order they appear in the bitstream:
58 int effect_type, effect_flag;
59 int effect_pcount1, effect_pcount2; ///< amount of effect parameters stored in effect_params
60 int effect_params1[15], effect_params2[10]; ///< effect parameters in 16.16 fixed point format
63 static inline int get_fp_val(GetBitContext* gb)
65 return (get_bits_long(gb, 30) - (1 << 29)) << 1;
68 static void vc1_sprite_parse_transform(GetBitContext* gb, int c[7])
72 switch (get_bits(gb, 2)) {
75 c[2] = get_fp_val(gb);
79 c[0] = c[4] = get_fp_val(gb);
80 c[2] = get_fp_val(gb);
83 c[0] = get_fp_val(gb);
84 c[2] = get_fp_val(gb);
85 c[4] = get_fp_val(gb);
88 c[0] = get_fp_val(gb);
89 c[1] = get_fp_val(gb);
90 c[2] = get_fp_val(gb);
91 c[3] = get_fp_val(gb);
92 c[4] = get_fp_val(gb);
95 c[5] = get_fp_val(gb);
97 c[6] = get_fp_val(gb);
102 static void vc1_parse_sprites(VC1Context *v, GetBitContext* gb, SpriteData* sd)
104 AVCodecContext *avctx = v->s.avctx;
107 for (sprite = 0; sprite <= v->two_sprites; sprite++) {
108 vc1_sprite_parse_transform(gb, sd->coefs[sprite]);
109 if (sd->coefs[sprite][1] || sd->coefs[sprite][3])
110 avpriv_request_sample(avctx, "Non-zero rotation coefficients");
111 av_log(avctx, AV_LOG_DEBUG, sprite ? "S2:" : "S1:");
112 for (i = 0; i < 7; i++)
113 av_log(avctx, AV_LOG_DEBUG, " %d.%.3d",
114 sd->coefs[sprite][i] / (1<<16),
115 (abs(sd->coefs[sprite][i]) & 0xFFFF) * 1000 / (1 << 16));
116 av_log(avctx, AV_LOG_DEBUG, "\n");
120 if (sd->effect_type = get_bits_long(gb, 30)) {
121 switch (sd->effect_pcount1 = get_bits(gb, 4)) {
123 vc1_sprite_parse_transform(gb, sd->effect_params1);
126 vc1_sprite_parse_transform(gb, sd->effect_params1);
127 vc1_sprite_parse_transform(gb, sd->effect_params1 + 7);
130 for (i = 0; i < sd->effect_pcount1; i++)
131 sd->effect_params1[i] = get_fp_val(gb);
133 if (sd->effect_type != 13 || sd->effect_params1[0] != sd->coefs[0][6]) {
134 // effect 13 is simple alpha blending and matches the opacity above
135 av_log(avctx, AV_LOG_DEBUG, "Effect: %d; params: ", sd->effect_type);
136 for (i = 0; i < sd->effect_pcount1; i++)
137 av_log(avctx, AV_LOG_DEBUG, " %d.%.2d",
138 sd->effect_params1[i] / (1 << 16),
139 (abs(sd->effect_params1[i]) & 0xFFFF) * 1000 / (1 << 16));
140 av_log(avctx, AV_LOG_DEBUG, "\n");
143 sd->effect_pcount2 = get_bits(gb, 16);
144 if (sd->effect_pcount2 > 10) {
145 av_log(avctx, AV_LOG_ERROR, "Too many effect parameters\n");
147 } else if (sd->effect_pcount2) {
149 av_log(avctx, AV_LOG_DEBUG, "Effect params 2: ");
150 while (++i < sd->effect_pcount2) {
151 sd->effect_params2[i] = get_fp_val(gb);
152 av_log(avctx, AV_LOG_DEBUG, " %d.%.2d",
153 sd->effect_params2[i] / (1 << 16),
154 (abs(sd->effect_params2[i]) & 0xFFFF) * 1000 / (1 << 16));
156 av_log(avctx, AV_LOG_DEBUG, "\n");
159 if (sd->effect_flag = get_bits1(gb))
160 av_log(avctx, AV_LOG_DEBUG, "Effect flag set\n");
162 if (get_bits_count(gb) >= gb->size_in_bits +
163 (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE ? 64 : 0))
164 av_log(avctx, AV_LOG_ERROR, "Buffer overrun\n");
165 if (get_bits_count(gb) < gb->size_in_bits - 8)
166 av_log(avctx, AV_LOG_WARNING, "Buffer not fully read\n");
169 static void vc1_draw_sprites(VC1Context *v, SpriteData* sd)
171 int i, plane, row, sprite;
172 int sr_cache[2][2] = { { -1, -1 }, { -1, -1 } };
173 uint8_t* src_h[2][2];
174 int xoff[2], xadv[2], yoff[2], yadv[2], alpha;
176 MpegEncContext *s = &v->s;
178 for (i = 0; i < 2; i++) {
179 xoff[i] = av_clip(sd->coefs[i][2], 0, v->sprite_width-1 << 16);
180 xadv[i] = sd->coefs[i][0];
181 if (xadv[i] != 1<<16 || (v->sprite_width << 16) - (v->output_width << 16) - xoff[i])
182 xadv[i] = av_clip(xadv[i], 0, ((v->sprite_width<<16) - xoff[i] - 1) / v->output_width);
184 yoff[i] = av_clip(sd->coefs[i][5], 0, v->sprite_height-1 << 16);
185 yadv[i] = av_clip(sd->coefs[i][4], 0, ((v->sprite_height << 16) - yoff[i]) / v->output_height);
187 alpha = av_clip_uint16(sd->coefs[1][6]);
189 for (plane = 0; plane < (s->avctx->flags & AV_CODEC_FLAG_GRAY ? 1 : 3); plane++) {
190 int width = v->output_width>>!!plane;
192 for (row = 0; row < v->output_height>>!!plane; row++) {
193 uint8_t *dst = v->sprite_output_frame->data[plane] +
194 v->sprite_output_frame->linesize[plane] * row;
196 for (sprite = 0; sprite <= v->two_sprites; sprite++) {
197 uint8_t *iplane = s->current_picture.f->data[plane];
198 int iline = s->current_picture.f->linesize[plane];
199 int ycoord = yoff[sprite] + yadv[sprite] * row;
200 int yline = ycoord >> 16;
202 ysub[sprite] = ycoord & 0xFFFF;
204 iplane = s->last_picture.f->data[plane];
205 iline = s->last_picture.f->linesize[plane];
207 next_line = FFMIN(yline + 1, (v->sprite_height >> !!plane) - 1) * iline;
208 if (!(xoff[sprite] & 0xFFFF) && xadv[sprite] == 1 << 16) {
209 src_h[sprite][0] = iplane + (xoff[sprite] >> 16) + yline * iline;
211 src_h[sprite][1] = iplane + (xoff[sprite] >> 16) + next_line;
213 if (sr_cache[sprite][0] != yline) {
214 if (sr_cache[sprite][1] == yline) {
215 FFSWAP(uint8_t*, v->sr_rows[sprite][0], v->sr_rows[sprite][1]);
216 FFSWAP(int, sr_cache[sprite][0], sr_cache[sprite][1]);
218 v->vc1dsp.sprite_h(v->sr_rows[sprite][0], iplane + yline * iline, xoff[sprite], xadv[sprite], width);
219 sr_cache[sprite][0] = yline;
222 if (ysub[sprite] && sr_cache[sprite][1] != yline + 1) {
223 v->vc1dsp.sprite_h(v->sr_rows[sprite][1],
224 iplane + next_line, xoff[sprite],
225 xadv[sprite], width);
226 sr_cache[sprite][1] = yline + 1;
228 src_h[sprite][0] = v->sr_rows[sprite][0];
229 src_h[sprite][1] = v->sr_rows[sprite][1];
233 if (!v->two_sprites) {
235 v->vc1dsp.sprite_v_single(dst, src_h[0][0], src_h[0][1], ysub[0], width);
237 memcpy(dst, src_h[0][0], width);
240 if (ysub[0] && ysub[1]) {
241 v->vc1dsp.sprite_v_double_twoscale(dst, src_h[0][0], src_h[0][1], ysub[0],
242 src_h[1][0], src_h[1][1], ysub[1], alpha, width);
243 } else if (ysub[0]) {
244 v->vc1dsp.sprite_v_double_onescale(dst, src_h[0][0], src_h[0][1], ysub[0],
245 src_h[1][0], alpha, width);
246 } else if (ysub[1]) {
247 v->vc1dsp.sprite_v_double_onescale(dst, src_h[1][0], src_h[1][1], ysub[1],
248 src_h[0][0], (1<<16)-1-alpha, width);
250 v->vc1dsp.sprite_v_double_noscale(dst, src_h[0][0], src_h[1][0], alpha, width);
256 for (i = 0; i < 2; i++) {
266 static int vc1_decode_sprites(VC1Context *v, GetBitContext* gb)
268 MpegEncContext *s = &v->s;
269 AVCodecContext *avctx = s->avctx;
272 vc1_parse_sprites(v, gb, &sd);
274 if (!s->current_picture.f || !s->current_picture.f->data[0]) {
275 av_log(avctx, AV_LOG_ERROR, "Got no sprites\n");
279 if (v->two_sprites && (!s->last_picture_ptr || !s->last_picture.f->data[0])) {
280 av_log(avctx, AV_LOG_WARNING, "Need two sprites, only got one\n");
284 av_frame_unref(v->sprite_output_frame);
285 if (ff_get_buffer(avctx, v->sprite_output_frame, 0) < 0) {
286 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
290 vc1_draw_sprites(v, &sd);
295 static void vc1_sprite_flush(AVCodecContext *avctx)
297 VC1Context *v = avctx->priv_data;
298 MpegEncContext *s = &v->s;
299 AVFrame *f = s->current_picture.f;
302 /* Windows Media Image codecs have a convergence interval of two keyframes.
303 Since we can't enforce it, clear to black the missing sprite. This is
304 wrong but it looks better than doing nothing. */
307 for (plane = 0; plane < (s->avctx->flags & AV_CODEC_FLAG_GRAY ? 1 : 3); plane++)
308 for (i = 0; i < v->sprite_height>>!!plane; i++)
309 memset(f->data[plane] + i * f->linesize[plane],
310 plane ? 128 : 0, f->linesize[plane]);
315 av_cold int ff_vc1_decode_init_alloc_tables(VC1Context *v)
317 MpegEncContext *s = &v->s;
318 int i, ret = AVERROR(ENOMEM);
319 int mb_height = FFALIGN(s->mb_height, 2);
321 /* Allocate mb bitplanes */
322 v->mv_type_mb_plane = av_malloc (s->mb_stride * mb_height);
323 v->direct_mb_plane = av_malloc (s->mb_stride * mb_height);
324 v->forward_mb_plane = av_malloc (s->mb_stride * mb_height);
325 v->fieldtx_plane = av_mallocz(s->mb_stride * mb_height);
326 v->acpred_plane = av_malloc (s->mb_stride * mb_height);
327 v->over_flags_plane = av_malloc (s->mb_stride * mb_height);
328 if (!v->mv_type_mb_plane || !v->direct_mb_plane || !v->forward_mb_plane ||
329 !v->fieldtx_plane || !v->acpred_plane || !v->over_flags_plane)
332 v->n_allocated_blks = s->mb_width + 2;
333 v->block = av_malloc(sizeof(*v->block) * v->n_allocated_blks);
334 v->cbp_base = av_malloc(sizeof(v->cbp_base[0]) * 2 * s->mb_stride);
335 if (!v->block || !v->cbp_base)
337 v->cbp = v->cbp_base + s->mb_stride;
338 v->ttblk_base = av_malloc(sizeof(v->ttblk_base[0]) * 2 * s->mb_stride);
341 v->ttblk = v->ttblk_base + s->mb_stride;
342 v->is_intra_base = av_mallocz(sizeof(v->is_intra_base[0]) * 2 * s->mb_stride);
343 if (!v->is_intra_base)
345 v->is_intra = v->is_intra_base + s->mb_stride;
346 v->luma_mv_base = av_malloc(sizeof(v->luma_mv_base[0]) * 2 * s->mb_stride);
347 if (!v->luma_mv_base)
349 v->luma_mv = v->luma_mv_base + s->mb_stride;
351 /* allocate block type info in that way so it could be used with s->block_index[] */
352 v->mb_type_base = av_malloc(s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2);
353 if (!v->mb_type_base)
355 v->mb_type[0] = v->mb_type_base + s->b8_stride + 1;
356 v->mb_type[1] = v->mb_type_base + s->b8_stride * (mb_height * 2 + 1) + s->mb_stride + 1;
357 v->mb_type[2] = v->mb_type[1] + s->mb_stride * (mb_height + 1);
359 /* allocate memory to store block level MV info */
360 v->blk_mv_type_base = av_mallocz( s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2);
361 if (!v->blk_mv_type_base)
363 v->blk_mv_type = v->blk_mv_type_base + s->b8_stride + 1;
364 v->mv_f_base = av_mallocz(2 * (s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2));
367 v->mv_f[0] = v->mv_f_base + s->b8_stride + 1;
368 v->mv_f[1] = v->mv_f[0] + (s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2);
369 v->mv_f_next_base = av_mallocz(2 * (s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2));
370 if (!v->mv_f_next_base)
372 v->mv_f_next[0] = v->mv_f_next_base + s->b8_stride + 1;
373 v->mv_f_next[1] = v->mv_f_next[0] + (s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2);
375 if (s->avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || s->avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
376 for (i = 0; i < 4; i++) {
377 v->sr_rows[i >> 1][i & 1] = av_malloc(v->output_width);
378 if (!v->sr_rows[i >> 1][i & 1])
383 ret = ff_intrax8_common_init(s->avctx, &v->x8, &s->idsp,
384 s->block, s->block_last_index,
385 s->mb_width, s->mb_height);
392 ff_vc1_decode_end(s->avctx);
396 av_cold void ff_vc1_init_transposed_scantables(VC1Context *v)
399 for (i = 0; i < 64; i++) {
400 #define transpose(x) ((x >> 3) | ((x & 7) << 3))
401 v->zz_8x8[0][i] = transpose(ff_wmv1_scantable[0][i]);
402 v->zz_8x8[1][i] = transpose(ff_wmv1_scantable[1][i]);
403 v->zz_8x8[2][i] = transpose(ff_wmv1_scantable[2][i]);
404 v->zz_8x8[3][i] = transpose(ff_wmv1_scantable[3][i]);
405 v->zzi_8x8[i] = transpose(ff_vc1_adv_interlaced_8x8_zz[i]);
411 /** Initialize a VC1/WMV3 decoder
412 * @todo TODO: Handle VC-1 IDUs (Transport level?)
413 * @todo TODO: Decipher remaining bits in extra_data
415 static av_cold int vc1_decode_init(AVCodecContext *avctx)
417 VC1Context *v = avctx->priv_data;
418 MpegEncContext *s = &v->s;
421 /* save the container output size for WMImage */
422 v->output_width = avctx->width;
423 v->output_height = avctx->height;
425 if (!avctx->extradata_size || !avctx->extradata)
427 if (!(avctx->flags & AV_CODEC_FLAG_GRAY))
428 avctx->pix_fmt = ff_get_format(avctx, avctx->codec->pix_fmts);
430 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
433 if (ff_vc1_init_common(v) < 0)
435 ff_blockdsp_init(&s->bdsp);
436 ff_h264chroma_init(&v->h264chroma, 8);
437 ff_qpeldsp_init(&s->qdsp);
439 if (avctx->codec_id == AV_CODEC_ID_WMV3 || avctx->codec_id == AV_CODEC_ID_WMV3IMAGE) {
442 // looks like WMV3 has a sequence header stored in the extradata
443 // advanced sequence header may be before the first frame
444 // the last byte of the extradata is a version number, 1 for the
445 // samples we can decode
447 init_get_bits(&gb, avctx->extradata, avctx->extradata_size*8);
449 if (ff_vc1_decode_sequence_header(avctx, v, &gb) < 0)
452 count = avctx->extradata_size*8 - get_bits_count(&gb);
454 av_log(avctx, AV_LOG_INFO, "Extra data: %i bits left, value: %X\n",
455 count, get_bits_long(&gb, FFMIN(count, 32)));
456 } else if (count < 0) {
457 av_log(avctx, AV_LOG_INFO, "Read %i bits in overflow\n", -count);
459 } else { // VC1/WVC1/WVP2
460 const uint8_t *start = avctx->extradata;
461 uint8_t *end = avctx->extradata + avctx->extradata_size;
464 uint8_t *buf2 = NULL;
465 int seq_initialized = 0, ep_initialized = 0;
467 if (avctx->extradata_size < 16) {
468 av_log(avctx, AV_LOG_ERROR, "Extradata size too small: %i\n", avctx->extradata_size);
472 buf2 = av_mallocz(avctx->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
473 start = find_next_marker(start, end); // in WVC1 extradata first byte is its size, but can be 0 in mkv
475 for (; next < end; start = next) {
476 next = find_next_marker(start + 4, end);
477 size = next - start - 4;
480 buf2_size = vc1_unescape_buffer(start + 4, size, buf2);
481 init_get_bits(&gb, buf2, buf2_size * 8);
482 switch (AV_RB32(start)) {
483 case VC1_CODE_SEQHDR:
484 if (ff_vc1_decode_sequence_header(avctx, v, &gb) < 0) {
490 case VC1_CODE_ENTRYPOINT:
491 if (ff_vc1_decode_entry_point(avctx, v, &gb) < 0) {
500 if (!seq_initialized || !ep_initialized) {
501 av_log(avctx, AV_LOG_ERROR, "Incomplete extradata\n");
504 v->res_sprite = (avctx->codec_id == AV_CODEC_ID_VC1IMAGE);
507 v->sprite_output_frame = av_frame_alloc();
508 if (!v->sprite_output_frame)
509 return AVERROR(ENOMEM);
511 avctx->profile = v->profile;
512 if (v->profile == PROFILE_ADVANCED)
513 avctx->level = v->level;
515 avctx->has_b_frames = !!avctx->max_b_frames;
517 if (v->color_prim == 1 || v->color_prim == 5 || v->color_prim == 6)
518 avctx->color_primaries = v->color_prim;
519 if (v->transfer_char == 1 || v->transfer_char == 7)
520 avctx->color_trc = v->transfer_char;
521 if (v->matrix_coef == 1 || v->matrix_coef == 6 || v->matrix_coef == 7)
522 avctx->colorspace = v->matrix_coef;
524 s->mb_width = (avctx->coded_width + 15) >> 4;
525 s->mb_height = (avctx->coded_height + 15) >> 4;
527 if (v->profile == PROFILE_ADVANCED || v->res_fasttx) {
528 ff_vc1_init_transposed_scantables(v);
530 memcpy(v->zz_8x8, ff_wmv1_scantable, 4*64);
535 if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
536 v->sprite_width = avctx->coded_width;
537 v->sprite_height = avctx->coded_height;
539 avctx->coded_width = avctx->width = v->output_width;
540 avctx->coded_height = avctx->height = v->output_height;
542 // prevent 16.16 overflows
543 if (v->sprite_width > 1 << 14 ||
544 v->sprite_height > 1 << 14 ||
545 v->output_width > 1 << 14 ||
546 v->output_height > 1 << 14) return -1;
551 /** Close a VC1/WMV3 decoder
552 * @warning Initial try at using MpegEncContext stuff
554 av_cold int ff_vc1_decode_end(AVCodecContext *avctx)
556 VC1Context *v = avctx->priv_data;
559 av_frame_free(&v->sprite_output_frame);
561 for (i = 0; i < 4; i++)
562 av_freep(&v->sr_rows[i >> 1][i & 1]);
563 av_freep(&v->hrd_rate);
564 av_freep(&v->hrd_buffer);
565 ff_mpv_common_end(&v->s);
566 av_freep(&v->mv_type_mb_plane);
567 av_freep(&v->direct_mb_plane);
568 av_freep(&v->forward_mb_plane);
569 av_freep(&v->fieldtx_plane);
570 av_freep(&v->acpred_plane);
571 av_freep(&v->over_flags_plane);
572 av_freep(&v->mb_type_base);
573 av_freep(&v->blk_mv_type_base);
574 av_freep(&v->mv_f_base);
575 av_freep(&v->mv_f_next_base);
577 av_freep(&v->cbp_base);
578 av_freep(&v->ttblk_base);
579 av_freep(&v->is_intra_base); // FIXME use v->mb_type[]
580 av_freep(&v->luma_mv_base);
581 ff_intrax8_common_end(&v->x8);
586 /** Decode a VC1/WMV3 frame
587 * @todo TODO: Handle VC-1 IDUs (Transport level?)
589 static int vc1_decode_frame(AVCodecContext *avctx, void *data,
590 int *got_frame, AVPacket *avpkt)
592 const uint8_t *buf = avpkt->data;
593 int buf_size = avpkt->size, n_slices = 0, i, ret;
594 VC1Context *v = avctx->priv_data;
595 MpegEncContext *s = &v->s;
596 AVFrame *pict = data;
597 uint8_t *buf2 = NULL;
598 const uint8_t *buf_start = buf;
599 int mb_height, n_slices1;
604 } *slices = NULL, *tmp;
606 /* no supplementary picture */
607 if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == VC1_CODE_ENDOFSEQ)) {
608 /* special case for last picture */
609 if (s->low_delay == 0 && s->next_picture_ptr) {
610 if ((ret = av_frame_ref(pict, s->next_picture_ptr->f)) < 0)
612 s->next_picture_ptr = NULL;
620 //for advanced profile we may need to parse and unescape data
621 if (avctx->codec_id == AV_CODEC_ID_VC1 || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
623 buf2 = av_mallocz(buf_size + AV_INPUT_BUFFER_PADDING_SIZE);
625 if (IS_MARKER(AV_RB32(buf))) { /* frame starts with marker and needs to be parsed */
626 const uint8_t *start, *end, *next;
630 for (start = buf, end = buf + buf_size; next < end; start = next) {
631 next = find_next_marker(start + 4, end);
632 size = next - start - 4;
633 if (size <= 0) continue;
634 switch (AV_RB32(start)) {
638 buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
640 case VC1_CODE_FIELD: {
642 tmp = av_realloc(slices, sizeof(*slices) * (n_slices+1));
646 slices[n_slices].buf = av_mallocz(buf_size + AV_INPUT_BUFFER_PADDING_SIZE);
647 if (!slices[n_slices].buf)
649 buf_size3 = vc1_unescape_buffer(start + 4, size,
650 slices[n_slices].buf);
651 init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
653 /* assuming that the field marker is at the exact middle,
655 slices[n_slices].mby_start = s->mb_height + 1 >> 1;
656 n_slices1 = n_slices - 1; // index of the last slice of the first field
660 case VC1_CODE_ENTRYPOINT: /* it should be before frame data */
661 buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
662 init_get_bits(&s->gb, buf2, buf_size2 * 8);
663 ff_vc1_decode_entry_point(avctx, v, &s->gb);
665 case VC1_CODE_SLICE: {
667 tmp = av_realloc(slices, sizeof(*slices) * (n_slices+1));
671 slices[n_slices].buf = av_mallocz(buf_size + AV_INPUT_BUFFER_PADDING_SIZE);
672 if (!slices[n_slices].buf)
674 buf_size3 = vc1_unescape_buffer(start + 4, size,
675 slices[n_slices].buf);
676 init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
678 slices[n_slices].mby_start = get_bits(&slices[n_slices].gb, 9);
684 } else if (v->interlace && ((buf[0] & 0xC0) == 0xC0)) { /* WVC1 interlaced stores both fields divided by marker */
685 const uint8_t *divider;
688 divider = find_next_marker(buf, buf + buf_size);
689 if ((divider == (buf + buf_size)) || AV_RB32(divider) != VC1_CODE_FIELD) {
690 av_log(avctx, AV_LOG_ERROR, "Error in WVC1 interlaced frame\n");
692 } else { // found field marker, unescape second field
693 tmp = av_realloc(slices, sizeof(*slices) * (n_slices+1));
697 slices[n_slices].buf = av_mallocz(buf_size + AV_INPUT_BUFFER_PADDING_SIZE);
698 if (!slices[n_slices].buf)
700 buf_size3 = vc1_unescape_buffer(divider + 4, buf + buf_size - divider - 4, slices[n_slices].buf);
701 init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
703 slices[n_slices].mby_start = s->mb_height + 1 >> 1;
704 n_slices1 = n_slices - 1;
707 buf_size2 = vc1_unescape_buffer(buf, divider - buf, buf2);
709 buf_size2 = vc1_unescape_buffer(buf, buf_size, buf2);
711 init_get_bits(&s->gb, buf2, buf_size2*8);
713 init_get_bits(&s->gb, buf, buf_size*8);
716 v->new_sprite = !get_bits1(&s->gb);
717 v->two_sprites = get_bits1(&s->gb);
718 /* res_sprite means a Windows Media Image stream, AV_CODEC_ID_*IMAGE means
719 we're using the sprite compositor. These are intentionally kept separate
720 so you can get the raw sprites by using the wmv3 decoder for WMVP or
721 the vc1 one for WVP2 */
722 if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
724 // switch AVCodecContext parameters to those of the sprites
725 avctx->width = avctx->coded_width = v->sprite_width;
726 avctx->height = avctx->coded_height = v->sprite_height;
733 if (s->context_initialized &&
734 (s->width != avctx->coded_width ||
735 s->height != avctx->coded_height)) {
736 ff_vc1_decode_end(avctx);
739 if (!s->context_initialized) {
740 if (ff_msmpeg4_decode_init(avctx) < 0)
742 if (ff_vc1_decode_init_alloc_tables(v) < 0) {
743 ff_mpv_common_end(s);
747 s->low_delay = !avctx->has_b_frames || v->res_sprite;
749 if (v->profile == PROFILE_ADVANCED) {
750 s->h_edge_pos = avctx->coded_width;
751 s->v_edge_pos = avctx->coded_height;
755 // do parse frame header
756 v->pic_header_flag = 0;
757 v->first_pic_header_flag = 1;
758 if (v->profile < PROFILE_ADVANCED) {
759 if (ff_vc1_parse_frame_header(v, &s->gb) < 0) {
763 if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
767 v->first_pic_header_flag = 0;
769 if ((avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE)
770 && s->pict_type != AV_PICTURE_TYPE_I) {
771 av_log(v->s.avctx, AV_LOG_ERROR, "Sprite decoder: expected I-frame\n");
775 // for skipping the frame
776 s->current_picture.f->pict_type = s->pict_type;
777 s->current_picture.f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
779 /* skip B-frames if we don't have reference frames */
780 if (!s->last_picture_ptr && (s->pict_type == AV_PICTURE_TYPE_B || s->droppable)) {
783 if ((avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B) ||
784 (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I) ||
785 avctx->skip_frame >= AVDISCARD_ALL) {
789 if (s->next_p_frame_damaged) {
790 if (s->pict_type == AV_PICTURE_TYPE_B)
793 s->next_p_frame_damaged = 0;
796 if (ff_mpv_frame_start(s, avctx) < 0) {
800 // process pulldown flags
801 s->current_picture_ptr->f->repeat_pict = 0;
802 // Pulldown flags are only valid when 'broadcast' has been set.
803 // So ticks_per_frame will be 2
806 s->current_picture_ptr->f->repeat_pict = 1;
807 } else if (v->rptfrm) {
809 s->current_picture_ptr->f->repeat_pict = v->rptfrm * 2;
812 s->me.qpel_put = s->qdsp.put_qpel_pixels_tab;
813 s->me.qpel_avg = s->qdsp.avg_qpel_pixels_tab;
815 if (avctx->hwaccel) {
816 if (avctx->hwaccel->start_frame(avctx, buf, buf_size) < 0)
818 if (avctx->hwaccel->decode_slice(avctx, buf_start, (buf + buf_size) - buf_start) < 0)
820 if (avctx->hwaccel->end_frame(avctx) < 0)
825 ff_mpeg_er_frame_start(s);
827 v->bits = buf_size * 8;
828 v->end_mb_x = s->mb_width;
830 s->current_picture.f->linesize[0] <<= 1;
831 s->current_picture.f->linesize[1] <<= 1;
832 s->current_picture.f->linesize[2] <<= 1;
836 mb_height = s->mb_height >> v->field_mode;
839 av_log(v->s.avctx, AV_LOG_ERROR, "Invalid mb_height.\n");
843 for (i = 0; i <= n_slices; i++) {
844 if (i > 0 && slices[i - 1].mby_start >= mb_height) {
845 if (v->field_mode <= 0) {
846 av_log(v->s.avctx, AV_LOG_ERROR, "Slice %d starts beyond "
847 "picture boundary (%d >= %d)\n", i,
848 slices[i - 1].mby_start, mb_height);
852 v->blocks_off = s->mb_width * s->mb_height << 1;
853 v->mb_off = s->mb_stride * s->mb_height >> 1;
860 v->pic_header_flag = 0;
861 if (v->field_mode && i == n_slices1 + 2) {
862 if ((header_ret = ff_vc1_parse_frame_header_adv(v, &s->gb)) < 0) {
863 av_log(v->s.avctx, AV_LOG_ERROR, "Field header damaged\n");
864 if (avctx->err_recognition & AV_EF_EXPLODE)
868 } else if (get_bits1(&s->gb)) {
869 v->pic_header_flag = 1;
870 if ((header_ret = ff_vc1_parse_frame_header_adv(v, &s->gb)) < 0) {
871 av_log(v->s.avctx, AV_LOG_ERROR, "Slice header damaged\n");
872 if (avctx->err_recognition & AV_EF_EXPLODE)
880 s->start_mb_y = (i == 0) ? 0 : FFMAX(0, slices[i-1].mby_start % mb_height);
881 if (!v->field_mode || v->second_field)
882 s->end_mb_y = (i == n_slices ) ? mb_height : FFMIN(mb_height, slices[i].mby_start % mb_height);
884 s->end_mb_y = (i <= n_slices1 + 1) ? mb_height : FFMIN(mb_height, slices[i].mby_start % mb_height);
886 if (s->end_mb_y <= s->start_mb_y) {
887 av_log(v->s.avctx, AV_LOG_ERROR, "Invalid slice size\n");
891 ff_vc1_decode_blocks(v);
893 s->gb = slices[i].gb;
897 s->current_picture.f->linesize[0] >>= 1;
898 s->current_picture.f->linesize[1] >>= 1;
899 s->current_picture.f->linesize[2] >>= 1;
902 if (v->s.pict_type != AV_PICTURE_TYPE_BI && v->s.pict_type != AV_PICTURE_TYPE_B) {
903 FFSWAP(uint8_t *, v->mv_f_next[0], v->mv_f[0]);
904 FFSWAP(uint8_t *, v->mv_f_next[1], v->mv_f[1]);
907 ff_dlog(s->avctx, "Consumed %i/%i bits\n",
908 get_bits_count(&s->gb), s->gb.size_in_bits);
909 // if (get_bits_count(&s->gb) > buf_size * 8)
912 ff_er_frame_end(&s->er);
917 if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
919 avctx->width = avctx->coded_width = v->output_width;
920 avctx->height = avctx->coded_height = v->output_height;
921 if (avctx->skip_frame >= AVDISCARD_NONREF)
923 #if CONFIG_WMV3IMAGE_DECODER || CONFIG_VC1IMAGE_DECODER
924 if (vc1_decode_sprites(v, &s->gb))
927 if ((ret = av_frame_ref(pict, v->sprite_output_frame)) < 0)
931 if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
932 if ((ret = av_frame_ref(pict, s->current_picture_ptr->f)) < 0)
934 ff_print_debug_info(s, s->current_picture_ptr);
936 } else if (s->last_picture_ptr) {
937 if ((ret = av_frame_ref(pict, s->last_picture_ptr->f)) < 0)
939 ff_print_debug_info(s, s->last_picture_ptr);
946 for (i = 0; i < n_slices; i++)
947 av_free(slices[i].buf);
953 for (i = 0; i < n_slices; i++)
954 av_free(slices[i].buf);
960 static const enum AVPixelFormat vc1_hwaccel_pixfmt_list_420[] = {
961 #if CONFIG_VC1_DXVA2_HWACCEL
962 AV_PIX_FMT_DXVA2_VLD,
964 #if CONFIG_VC1_D3D11VA_HWACCEL
965 AV_PIX_FMT_D3D11VA_VLD,
968 #if CONFIG_VC1_VAAPI_HWACCEL
971 #if CONFIG_VC1_VDPAU_HWACCEL
978 AVCodec ff_vc1_decoder = {
980 .long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-1"),
981 .type = AVMEDIA_TYPE_VIDEO,
982 .id = AV_CODEC_ID_VC1,
983 .priv_data_size = sizeof(VC1Context),
984 .init = vc1_decode_init,
985 .close = ff_vc1_decode_end,
986 .decode = vc1_decode_frame,
987 .flush = ff_mpeg_flush,
988 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY,
989 .pix_fmts = vc1_hwaccel_pixfmt_list_420,
990 .hw_configs = (const AVCodecHWConfigInternal*[]) {
991 #if CONFIG_VC1_DXVA2_HWACCEL
994 #if CONFIG_VC1_D3D11VA_HWACCEL
995 HWACCEL_D3D11VA(vc1),
997 #if CONFIG_VC1_D3D11VA2_HWACCEL
998 HWACCEL_D3D11VA2(vc1),
1000 #if CONFIG_VC1_VAAPI_HWACCEL
1003 #if CONFIG_VC1_VDPAU_HWACCEL
1008 .profiles = NULL_IF_CONFIG_SMALL(ff_vc1_profiles)
1011 #if CONFIG_WMV3_DECODER
1012 AVCodec ff_wmv3_decoder = {
1014 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9"),
1015 .type = AVMEDIA_TYPE_VIDEO,
1016 .id = AV_CODEC_ID_WMV3,
1017 .priv_data_size = sizeof(VC1Context),
1018 .init = vc1_decode_init,
1019 .close = ff_vc1_decode_end,
1020 .decode = vc1_decode_frame,
1021 .flush = ff_mpeg_flush,
1022 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY,
1023 .pix_fmts = vc1_hwaccel_pixfmt_list_420,
1024 .hw_configs = (const AVCodecHWConfigInternal*[]) {
1025 #if CONFIG_WMV3_DXVA2_HWACCEL
1026 HWACCEL_DXVA2(wmv3),
1028 #if CONFIG_WMV3_D3D11VA_HWACCEL
1029 HWACCEL_D3D11VA(wmv3),
1031 #if CONFIG_WMV3_D3D11VA2_HWACCEL
1032 HWACCEL_D3D11VA2(wmv3),
1034 #if CONFIG_WMV3_VAAPI_HWACCEL
1035 HWACCEL_VAAPI(wmv3),
1037 #if CONFIG_WMV3_VDPAU_HWACCEL
1038 HWACCEL_VDPAU(wmv3),
1042 .profiles = NULL_IF_CONFIG_SMALL(ff_vc1_profiles)
1046 #if CONFIG_WMV3IMAGE_DECODER
1047 AVCodec ff_wmv3image_decoder = {
1048 .name = "wmv3image",
1049 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 Image"),
1050 .type = AVMEDIA_TYPE_VIDEO,
1051 .id = AV_CODEC_ID_WMV3IMAGE,
1052 .priv_data_size = sizeof(VC1Context),
1053 .init = vc1_decode_init,
1054 .close = ff_vc1_decode_end,
1055 .decode = vc1_decode_frame,
1056 .capabilities = AV_CODEC_CAP_DR1,
1057 .flush = vc1_sprite_flush,
1058 .pix_fmts = (const enum AVPixelFormat[]) {
1065 #if CONFIG_VC1IMAGE_DECODER
1066 AVCodec ff_vc1image_decoder = {
1068 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 Image v2"),
1069 .type = AVMEDIA_TYPE_VIDEO,
1070 .id = AV_CODEC_ID_VC1IMAGE,
1071 .priv_data_size = sizeof(VC1Context),
1072 .init = vc1_decode_init,
1073 .close = ff_vc1_decode_end,
1074 .decode = vc1_decode_frame,
1075 .capabilities = AV_CODEC_CAP_DR1,
1076 .flush = vc1_sprite_flush,
1077 .pix_fmts = (const enum AVPixelFormat[]) {