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
34 #include "mpegvideo.h"
36 #include "msmpeg4data.h"
41 #if CONFIG_WMV3IMAGE_DECODER || CONFIG_VC1IMAGE_DECODER
43 typedef struct SpriteData {
45 * Transform coefficients for both sprites in 16.16 fixed point format,
46 * in the order they appear in the bitstream:
57 int effect_type, effect_flag;
58 int effect_pcount1, effect_pcount2; ///< amount of effect parameters stored in effect_params
59 int effect_params1[15], effect_params2[10]; ///< effect parameters in 16.16 fixed point format
62 static inline int get_fp_val(GetBitContext* gb)
64 return (get_bits_long(gb, 30) - (1 << 29)) << 1;
67 static void vc1_sprite_parse_transform(GetBitContext* gb, int c[7])
71 switch (get_bits(gb, 2)) {
74 c[2] = get_fp_val(gb);
78 c[0] = c[4] = get_fp_val(gb);
79 c[2] = get_fp_val(gb);
82 c[0] = get_fp_val(gb);
83 c[2] = get_fp_val(gb);
84 c[4] = get_fp_val(gb);
87 c[0] = get_fp_val(gb);
88 c[1] = get_fp_val(gb);
89 c[2] = get_fp_val(gb);
90 c[3] = get_fp_val(gb);
91 c[4] = get_fp_val(gb);
94 c[5] = get_fp_val(gb);
96 c[6] = get_fp_val(gb);
101 static void vc1_parse_sprites(VC1Context *v, GetBitContext* gb, SpriteData* sd)
103 AVCodecContext *avctx = v->s.avctx;
106 for (sprite = 0; sprite <= v->two_sprites; sprite++) {
107 vc1_sprite_parse_transform(gb, sd->coefs[sprite]);
108 if (sd->coefs[sprite][1] || sd->coefs[sprite][3])
109 avpriv_request_sample(avctx, "Non-zero rotation coefficients");
110 av_log(avctx, AV_LOG_DEBUG, sprite ? "S2:" : "S1:");
111 for (i = 0; i < 7; i++)
112 av_log(avctx, AV_LOG_DEBUG, " %d.%.3d",
113 sd->coefs[sprite][i] / (1<<16),
114 (abs(sd->coefs[sprite][i]) & 0xFFFF) * 1000 / (1 << 16));
115 av_log(avctx, AV_LOG_DEBUG, "\n");
119 if (sd->effect_type = get_bits_long(gb, 30)) {
120 switch (sd->effect_pcount1 = get_bits(gb, 4)) {
122 vc1_sprite_parse_transform(gb, sd->effect_params1);
125 vc1_sprite_parse_transform(gb, sd->effect_params1);
126 vc1_sprite_parse_transform(gb, sd->effect_params1 + 7);
129 for (i = 0; i < sd->effect_pcount1; i++)
130 sd->effect_params1[i] = get_fp_val(gb);
132 if (sd->effect_type != 13 || sd->effect_params1[0] != sd->coefs[0][6]) {
133 // effect 13 is simple alpha blending and matches the opacity above
134 av_log(avctx, AV_LOG_DEBUG, "Effect: %d; params: ", sd->effect_type);
135 for (i = 0; i < sd->effect_pcount1; i++)
136 av_log(avctx, AV_LOG_DEBUG, " %d.%.2d",
137 sd->effect_params1[i] / (1 << 16),
138 (abs(sd->effect_params1[i]) & 0xFFFF) * 1000 / (1 << 16));
139 av_log(avctx, AV_LOG_DEBUG, "\n");
142 sd->effect_pcount2 = get_bits(gb, 16);
143 if (sd->effect_pcount2 > 10) {
144 av_log(avctx, AV_LOG_ERROR, "Too many effect parameters\n");
146 } else if (sd->effect_pcount2) {
148 av_log(avctx, AV_LOG_DEBUG, "Effect params 2: ");
149 while (++i < sd->effect_pcount2) {
150 sd->effect_params2[i] = get_fp_val(gb);
151 av_log(avctx, AV_LOG_DEBUG, " %d.%.2d",
152 sd->effect_params2[i] / (1 << 16),
153 (abs(sd->effect_params2[i]) & 0xFFFF) * 1000 / (1 << 16));
155 av_log(avctx, AV_LOG_DEBUG, "\n");
158 if (sd->effect_flag = get_bits1(gb))
159 av_log(avctx, AV_LOG_DEBUG, "Effect flag set\n");
161 if (get_bits_count(gb) >= gb->size_in_bits +
162 (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE ? 64 : 0))
163 av_log(avctx, AV_LOG_ERROR, "Buffer overrun\n");
164 if (get_bits_count(gb) < gb->size_in_bits - 8)
165 av_log(avctx, AV_LOG_WARNING, "Buffer not fully read\n");
168 static void vc1_draw_sprites(VC1Context *v, SpriteData* sd)
170 int i, plane, row, sprite;
171 int sr_cache[2][2] = { { -1, -1 }, { -1, -1 } };
172 uint8_t* src_h[2][2];
173 int xoff[2], xadv[2], yoff[2], yadv[2], alpha;
175 MpegEncContext *s = &v->s;
177 for (i = 0; i < 2; i++) {
178 xoff[i] = av_clip(sd->coefs[i][2], 0, v->sprite_width-1 << 16);
179 xadv[i] = sd->coefs[i][0];
180 if (xadv[i] != 1<<16 || (v->sprite_width << 16) - (v->output_width << 16) - xoff[i])
181 xadv[i] = av_clip(xadv[i], 0, ((v->sprite_width<<16) - xoff[i] - 1) / v->output_width);
183 yoff[i] = av_clip(sd->coefs[i][5], 0, v->sprite_height-1 << 16);
184 yadv[i] = av_clip(sd->coefs[i][4], 0, ((v->sprite_height << 16) - yoff[i]) / v->output_height);
186 alpha = av_clip_uint16(sd->coefs[1][6]);
188 for (plane = 0; plane < (s->avctx->flags & AV_CODEC_FLAG_GRAY ? 1 : 3); plane++) {
189 int width = v->output_width>>!!plane;
191 for (row = 0; row < v->output_height>>!!plane; row++) {
192 uint8_t *dst = v->sprite_output_frame->data[plane] +
193 v->sprite_output_frame->linesize[plane] * row;
195 for (sprite = 0; sprite <= v->two_sprites; sprite++) {
196 uint8_t *iplane = s->current_picture.f->data[plane];
197 int iline = s->current_picture.f->linesize[plane];
198 int ycoord = yoff[sprite] + yadv[sprite] * row;
199 int yline = ycoord >> 16;
201 ysub[sprite] = ycoord & 0xFFFF;
203 iplane = s->last_picture.f->data[plane];
204 iline = s->last_picture.f->linesize[plane];
206 next_line = FFMIN(yline + 1, (v->sprite_height >> !!plane) - 1) * iline;
207 if (!(xoff[sprite] & 0xFFFF) && xadv[sprite] == 1 << 16) {
208 src_h[sprite][0] = iplane + (xoff[sprite] >> 16) + yline * iline;
210 src_h[sprite][1] = iplane + (xoff[sprite] >> 16) + next_line;
212 if (sr_cache[sprite][0] != yline) {
213 if (sr_cache[sprite][1] == yline) {
214 FFSWAP(uint8_t*, v->sr_rows[sprite][0], v->sr_rows[sprite][1]);
215 FFSWAP(int, sr_cache[sprite][0], sr_cache[sprite][1]);
217 v->vc1dsp.sprite_h(v->sr_rows[sprite][0], iplane + yline * iline, xoff[sprite], xadv[sprite], width);
218 sr_cache[sprite][0] = yline;
221 if (ysub[sprite] && sr_cache[sprite][1] != yline + 1) {
222 v->vc1dsp.sprite_h(v->sr_rows[sprite][1],
223 iplane + next_line, xoff[sprite],
224 xadv[sprite], width);
225 sr_cache[sprite][1] = yline + 1;
227 src_h[sprite][0] = v->sr_rows[sprite][0];
228 src_h[sprite][1] = v->sr_rows[sprite][1];
232 if (!v->two_sprites) {
234 v->vc1dsp.sprite_v_single(dst, src_h[0][0], src_h[0][1], ysub[0], width);
236 memcpy(dst, src_h[0][0], width);
239 if (ysub[0] && ysub[1]) {
240 v->vc1dsp.sprite_v_double_twoscale(dst, src_h[0][0], src_h[0][1], ysub[0],
241 src_h[1][0], src_h[1][1], ysub[1], alpha, width);
242 } else if (ysub[0]) {
243 v->vc1dsp.sprite_v_double_onescale(dst, src_h[0][0], src_h[0][1], ysub[0],
244 src_h[1][0], alpha, width);
245 } else if (ysub[1]) {
246 v->vc1dsp.sprite_v_double_onescale(dst, src_h[1][0], src_h[1][1], ysub[1],
247 src_h[0][0], (1<<16)-1-alpha, width);
249 v->vc1dsp.sprite_v_double_noscale(dst, src_h[0][0], src_h[1][0], alpha, width);
255 for (i = 0; i < 2; i++) {
265 static int vc1_decode_sprites(VC1Context *v, GetBitContext* gb)
267 MpegEncContext *s = &v->s;
268 AVCodecContext *avctx = s->avctx;
271 vc1_parse_sprites(v, gb, &sd);
273 if (!s->current_picture.f->data[0]) {
274 av_log(avctx, AV_LOG_ERROR, "Got no sprites\n");
278 if (v->two_sprites && (!s->last_picture_ptr || !s->last_picture.f->data[0])) {
279 av_log(avctx, AV_LOG_WARNING, "Need two sprites, only got one\n");
283 av_frame_unref(v->sprite_output_frame);
284 if (ff_get_buffer(avctx, v->sprite_output_frame, 0) < 0) {
285 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
289 vc1_draw_sprites(v, &sd);
294 static void vc1_sprite_flush(AVCodecContext *avctx)
296 VC1Context *v = avctx->priv_data;
297 MpegEncContext *s = &v->s;
298 AVFrame *f = s->current_picture.f;
301 /* Windows Media Image codecs have a convergence interval of two keyframes.
302 Since we can't enforce it, clear to black the missing sprite. This is
303 wrong but it looks better than doing nothing. */
306 for (plane = 0; plane < (s->avctx->flags & AV_CODEC_FLAG_GRAY ? 1 : 3); plane++)
307 for (i = 0; i < v->sprite_height>>!!plane; i++)
308 memset(f->data[plane] + i * f->linesize[plane],
309 plane ? 128 : 0, f->linesize[plane]);
314 av_cold int ff_vc1_decode_init_alloc_tables(VC1Context *v)
316 MpegEncContext *s = &v->s;
318 int mb_height = FFALIGN(s->mb_height, 2);
320 /* Allocate mb bitplanes */
321 v->mv_type_mb_plane = av_malloc (s->mb_stride * mb_height);
322 v->direct_mb_plane = av_malloc (s->mb_stride * mb_height);
323 v->forward_mb_plane = av_malloc (s->mb_stride * mb_height);
324 v->fieldtx_plane = av_mallocz(s->mb_stride * mb_height);
325 v->acpred_plane = av_malloc (s->mb_stride * mb_height);
326 v->over_flags_plane = av_malloc (s->mb_stride * mb_height);
328 v->n_allocated_blks = s->mb_width + 2;
329 v->block = av_malloc(sizeof(*v->block) * v->n_allocated_blks);
330 v->cbp_base = av_malloc(sizeof(v->cbp_base[0]) * 2 * s->mb_stride);
331 v->cbp = v->cbp_base + s->mb_stride;
332 v->ttblk_base = av_malloc(sizeof(v->ttblk_base[0]) * 2 * s->mb_stride);
333 v->ttblk = v->ttblk_base + s->mb_stride;
334 v->is_intra_base = av_mallocz(sizeof(v->is_intra_base[0]) * 2 * s->mb_stride);
335 v->is_intra = v->is_intra_base + s->mb_stride;
336 v->luma_mv_base = av_malloc(sizeof(v->luma_mv_base[0]) * 2 * s->mb_stride);
337 v->luma_mv = v->luma_mv_base + s->mb_stride;
339 /* allocate block type info in that way so it could be used with s->block_index[] */
340 v->mb_type_base = av_malloc(s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2);
341 v->mb_type[0] = v->mb_type_base + s->b8_stride + 1;
342 v->mb_type[1] = v->mb_type_base + s->b8_stride * (mb_height * 2 + 1) + s->mb_stride + 1;
343 v->mb_type[2] = v->mb_type[1] + s->mb_stride * (mb_height + 1);
345 /* allocate memory to store block level MV info */
346 v->blk_mv_type_base = av_mallocz( s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2);
347 v->blk_mv_type = v->blk_mv_type_base + s->b8_stride + 1;
348 v->mv_f_base = av_mallocz(2 * (s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2));
349 v->mv_f[0] = v->mv_f_base + s->b8_stride + 1;
350 v->mv_f[1] = v->mv_f[0] + (s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2);
351 v->mv_f_next_base = av_mallocz(2 * (s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2));
352 v->mv_f_next[0] = v->mv_f_next_base + s->b8_stride + 1;
353 v->mv_f_next[1] = v->mv_f_next[0] + (s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2);
355 ff_intrax8_common_init(&v->x8,s);
357 if (s->avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || s->avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
358 for (i = 0; i < 4; i++)
359 if (!(v->sr_rows[i >> 1][i & 1] = av_malloc(v->output_width))) return -1;
362 if (!v->mv_type_mb_plane || !v->direct_mb_plane || !v->acpred_plane || !v->over_flags_plane ||
363 !v->block || !v->cbp_base || !v->ttblk_base || !v->is_intra_base || !v->luma_mv_base ||
365 av_freep(&v->mv_type_mb_plane);
366 av_freep(&v->direct_mb_plane);
367 av_freep(&v->acpred_plane);
368 av_freep(&v->over_flags_plane);
370 av_freep(&v->cbp_base);
371 av_freep(&v->ttblk_base);
372 av_freep(&v->is_intra_base);
373 av_freep(&v->luma_mv_base);
374 av_freep(&v->mb_type_base);
375 return AVERROR(ENOMEM);
381 av_cold void ff_vc1_init_transposed_scantables(VC1Context *v)
384 for (i = 0; i < 64; i++) {
385 #define transpose(x) ((x >> 3) | ((x & 7) << 3))
386 v->zz_8x8[0][i] = transpose(ff_wmv1_scantable[0][i]);
387 v->zz_8x8[1][i] = transpose(ff_wmv1_scantable[1][i]);
388 v->zz_8x8[2][i] = transpose(ff_wmv1_scantable[2][i]);
389 v->zz_8x8[3][i] = transpose(ff_wmv1_scantable[3][i]);
390 v->zzi_8x8[i] = transpose(ff_vc1_adv_interlaced_8x8_zz[i]);
396 /** Initialize a VC1/WMV3 decoder
397 * @todo TODO: Handle VC-1 IDUs (Transport level?)
398 * @todo TODO: Decypher remaining bits in extra_data
400 static av_cold int vc1_decode_init(AVCodecContext *avctx)
402 VC1Context *v = avctx->priv_data;
403 MpegEncContext *s = &v->s;
406 /* save the container output size for WMImage */
407 v->output_width = avctx->width;
408 v->output_height = avctx->height;
410 if (!avctx->extradata_size || !avctx->extradata)
412 if (!(avctx->flags & AV_CODEC_FLAG_GRAY))
413 avctx->pix_fmt = ff_get_format(avctx, avctx->codec->pix_fmts);
415 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
418 if (ff_vc1_init_common(v) < 0)
420 ff_blockdsp_init(&s->bdsp, avctx);
421 ff_h264chroma_init(&v->h264chroma, 8);
422 ff_qpeldsp_init(&s->qdsp);
424 if (avctx->codec_id == AV_CODEC_ID_WMV3 || avctx->codec_id == AV_CODEC_ID_WMV3IMAGE) {
427 // looks like WMV3 has a sequence header stored in the extradata
428 // advanced sequence header may be before the first frame
429 // the last byte of the extradata is a version number, 1 for the
430 // samples we can decode
432 init_get_bits(&gb, avctx->extradata, avctx->extradata_size*8);
434 if (ff_vc1_decode_sequence_header(avctx, v, &gb) < 0)
437 count = avctx->extradata_size*8 - get_bits_count(&gb);
439 av_log(avctx, AV_LOG_INFO, "Extra data: %i bits left, value: %X\n",
440 count, get_bits_long(&gb, FFMIN(count, 32)));
441 } else if (count < 0) {
442 av_log(avctx, AV_LOG_INFO, "Read %i bits in overflow\n", -count);
444 } else { // VC1/WVC1/WVP2
445 const uint8_t *start = avctx->extradata;
446 uint8_t *end = avctx->extradata + avctx->extradata_size;
449 uint8_t *buf2 = NULL;
450 int seq_initialized = 0, ep_initialized = 0;
452 if (avctx->extradata_size < 16) {
453 av_log(avctx, AV_LOG_ERROR, "Extradata size too small: %i\n", avctx->extradata_size);
457 buf2 = av_mallocz(avctx->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
458 start = find_next_marker(start, end); // in WVC1 extradata first byte is its size, but can be 0 in mkv
460 for (; next < end; start = next) {
461 next = find_next_marker(start + 4, end);
462 size = next - start - 4;
465 buf2_size = vc1_unescape_buffer(start + 4, size, buf2);
466 init_get_bits(&gb, buf2, buf2_size * 8);
467 switch (AV_RB32(start)) {
468 case VC1_CODE_SEQHDR:
469 if (ff_vc1_decode_sequence_header(avctx, v, &gb) < 0) {
475 case VC1_CODE_ENTRYPOINT:
476 if (ff_vc1_decode_entry_point(avctx, v, &gb) < 0) {
485 if (!seq_initialized || !ep_initialized) {
486 av_log(avctx, AV_LOG_ERROR, "Incomplete extradata\n");
489 v->res_sprite = (avctx->codec_id == AV_CODEC_ID_VC1IMAGE);
492 v->sprite_output_frame = av_frame_alloc();
493 if (!v->sprite_output_frame)
494 return AVERROR(ENOMEM);
496 avctx->profile = v->profile;
497 if (v->profile == PROFILE_ADVANCED)
498 avctx->level = v->level;
500 avctx->has_b_frames = !!avctx->max_b_frames;
502 if (v->color_prim == 1 || v->color_prim == 5 || v->color_prim == 6)
503 avctx->color_primaries = v->color_prim;
504 if (v->transfer_char == 1 || v->transfer_char == 7)
505 avctx->color_trc = v->transfer_char;
506 if (v->matrix_coef == 1 || v->matrix_coef == 6 || v->matrix_coef == 7)
507 avctx->colorspace = v->matrix_coef;
509 s->mb_width = (avctx->coded_width + 15) >> 4;
510 s->mb_height = (avctx->coded_height + 15) >> 4;
512 if (v->profile == PROFILE_ADVANCED || v->res_fasttx) {
513 ff_vc1_init_transposed_scantables(v);
515 memcpy(v->zz_8x8, ff_wmv1_scantable, 4*64);
520 if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
521 v->sprite_width = avctx->coded_width;
522 v->sprite_height = avctx->coded_height;
524 avctx->coded_width = avctx->width = v->output_width;
525 avctx->coded_height = avctx->height = v->output_height;
527 // prevent 16.16 overflows
528 if (v->sprite_width > 1 << 14 ||
529 v->sprite_height > 1 << 14 ||
530 v->output_width > 1 << 14 ||
531 v->output_height > 1 << 14) return -1;
536 /** Close a VC1/WMV3 decoder
537 * @warning Initial try at using MpegEncContext stuff
539 av_cold int ff_vc1_decode_end(AVCodecContext *avctx)
541 VC1Context *v = avctx->priv_data;
544 av_frame_free(&v->sprite_output_frame);
546 for (i = 0; i < 4; i++)
547 av_freep(&v->sr_rows[i >> 1][i & 1]);
548 av_freep(&v->hrd_rate);
549 av_freep(&v->hrd_buffer);
550 ff_mpv_common_end(&v->s);
551 av_freep(&v->mv_type_mb_plane);
552 av_freep(&v->direct_mb_plane);
553 av_freep(&v->forward_mb_plane);
554 av_freep(&v->fieldtx_plane);
555 av_freep(&v->acpred_plane);
556 av_freep(&v->over_flags_plane);
557 av_freep(&v->mb_type_base);
558 av_freep(&v->blk_mv_type_base);
559 av_freep(&v->mv_f_base);
560 av_freep(&v->mv_f_next_base);
562 av_freep(&v->cbp_base);
563 av_freep(&v->ttblk_base);
564 av_freep(&v->is_intra_base); // FIXME use v->mb_type[]
565 av_freep(&v->luma_mv_base);
566 ff_intrax8_common_end(&v->x8);
571 /** Decode a VC1/WMV3 frame
572 * @todo TODO: Handle VC-1 IDUs (Transport level?)
574 static int vc1_decode_frame(AVCodecContext *avctx, void *data,
575 int *got_frame, AVPacket *avpkt)
577 const uint8_t *buf = avpkt->data;
578 int buf_size = avpkt->size, n_slices = 0, i, ret;
579 VC1Context *v = avctx->priv_data;
580 MpegEncContext *s = &v->s;
581 AVFrame *pict = data;
582 uint8_t *buf2 = NULL;
583 const uint8_t *buf_start = buf;
584 int mb_height, n_slices1;
589 } *slices = NULL, *tmp;
591 /* no supplementary picture */
592 if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == VC1_CODE_ENDOFSEQ)) {
593 /* special case for last picture */
594 if (s->low_delay == 0 && s->next_picture_ptr) {
595 if ((ret = av_frame_ref(pict, s->next_picture_ptr->f)) < 0)
597 s->next_picture_ptr = NULL;
605 //for advanced profile we may need to parse and unescape data
606 if (avctx->codec_id == AV_CODEC_ID_VC1 || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
608 buf2 = av_mallocz(buf_size + AV_INPUT_BUFFER_PADDING_SIZE);
610 if (IS_MARKER(AV_RB32(buf))) { /* frame starts with marker and needs to be parsed */
611 const uint8_t *start, *end, *next;
615 for (start = buf, end = buf + buf_size; next < end; start = next) {
616 next = find_next_marker(start + 4, end);
617 size = next - start - 4;
618 if (size <= 0) continue;
619 switch (AV_RB32(start)) {
623 buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
625 case VC1_CODE_FIELD: {
627 tmp = av_realloc(slices, sizeof(*slices) * (n_slices+1));
631 slices[n_slices].buf = av_mallocz(buf_size + AV_INPUT_BUFFER_PADDING_SIZE);
632 if (!slices[n_slices].buf)
634 buf_size3 = vc1_unescape_buffer(start + 4, size,
635 slices[n_slices].buf);
636 init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
638 /* assuming that the field marker is at the exact middle,
640 slices[n_slices].mby_start = s->mb_height >> 1;
641 n_slices1 = n_slices - 1; // index of the last slice of the first field
645 case VC1_CODE_ENTRYPOINT: /* it should be before frame data */
646 buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
647 init_get_bits(&s->gb, buf2, buf_size2 * 8);
648 ff_vc1_decode_entry_point(avctx, v, &s->gb);
650 case VC1_CODE_SLICE: {
652 tmp = av_realloc(slices, sizeof(*slices) * (n_slices+1));
656 slices[n_slices].buf = av_mallocz(buf_size + AV_INPUT_BUFFER_PADDING_SIZE);
657 if (!slices[n_slices].buf)
659 buf_size3 = vc1_unescape_buffer(start + 4, size,
660 slices[n_slices].buf);
661 init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
663 slices[n_slices].mby_start = get_bits(&slices[n_slices].gb, 9);
669 } else if (v->interlace && ((buf[0] & 0xC0) == 0xC0)) { /* WVC1 interlaced stores both fields divided by marker */
670 const uint8_t *divider;
673 divider = find_next_marker(buf, buf + buf_size);
674 if ((divider == (buf + buf_size)) || AV_RB32(divider) != VC1_CODE_FIELD) {
675 av_log(avctx, AV_LOG_ERROR, "Error in WVC1 interlaced frame\n");
677 } else { // found field marker, unescape second field
678 tmp = av_realloc(slices, sizeof(*slices) * (n_slices+1));
682 slices[n_slices].buf = av_mallocz(buf_size + AV_INPUT_BUFFER_PADDING_SIZE);
683 if (!slices[n_slices].buf)
685 buf_size3 = vc1_unescape_buffer(divider + 4, buf + buf_size - divider - 4, slices[n_slices].buf);
686 init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
688 slices[n_slices].mby_start = s->mb_height >> 1;
689 n_slices1 = n_slices - 1;
692 buf_size2 = vc1_unescape_buffer(buf, divider - buf, buf2);
694 buf_size2 = vc1_unescape_buffer(buf, buf_size, buf2);
696 init_get_bits(&s->gb, buf2, buf_size2*8);
698 init_get_bits(&s->gb, buf, buf_size*8);
701 v->new_sprite = !get_bits1(&s->gb);
702 v->two_sprites = get_bits1(&s->gb);
703 /* res_sprite means a Windows Media Image stream, AV_CODEC_ID_*IMAGE means
704 we're using the sprite compositor. These are intentionally kept separate
705 so you can get the raw sprites by using the wmv3 decoder for WMVP or
706 the vc1 one for WVP2 */
707 if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
709 // switch AVCodecContext parameters to those of the sprites
710 avctx->width = avctx->coded_width = v->sprite_width;
711 avctx->height = avctx->coded_height = v->sprite_height;
718 if (s->context_initialized &&
719 (s->width != avctx->coded_width ||
720 s->height != avctx->coded_height)) {
721 ff_vc1_decode_end(avctx);
724 if (!s->context_initialized) {
725 if (ff_msmpeg4_decode_init(avctx) < 0)
727 if (ff_vc1_decode_init_alloc_tables(v) < 0) {
728 ff_mpv_common_end(s);
732 s->low_delay = !avctx->has_b_frames || v->res_sprite;
734 if (v->profile == PROFILE_ADVANCED) {
735 s->h_edge_pos = avctx->coded_width;
736 s->v_edge_pos = avctx->coded_height;
740 // do parse frame header
741 v->pic_header_flag = 0;
742 v->first_pic_header_flag = 1;
743 if (v->profile < PROFILE_ADVANCED) {
744 if (ff_vc1_parse_frame_header(v, &s->gb) < 0) {
748 if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
752 v->first_pic_header_flag = 0;
754 if ((avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE)
755 && s->pict_type != AV_PICTURE_TYPE_I) {
756 av_log(v->s.avctx, AV_LOG_ERROR, "Sprite decoder: expected I-frame\n");
760 // for skipping the frame
761 s->current_picture.f->pict_type = s->pict_type;
762 s->current_picture.f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
764 /* skip B-frames if we don't have reference frames */
765 if (!s->last_picture_ptr && (s->pict_type == AV_PICTURE_TYPE_B || s->droppable)) {
768 if ((avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B) ||
769 (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I) ||
770 avctx->skip_frame >= AVDISCARD_ALL) {
774 if (s->next_p_frame_damaged) {
775 if (s->pict_type == AV_PICTURE_TYPE_B)
778 s->next_p_frame_damaged = 0;
781 if (ff_mpv_frame_start(s, avctx) < 0) {
785 // process pulldown flags
786 s->current_picture_ptr->f->repeat_pict = 0;
787 // Pulldown flags are only valid when 'broadcast' has been set.
788 // So ticks_per_frame will be 2
791 s->current_picture_ptr->f->repeat_pict = 1;
792 } else if (v->rptfrm) {
794 s->current_picture_ptr->f->repeat_pict = v->rptfrm * 2;
797 s->me.qpel_put = s->qdsp.put_qpel_pixels_tab;
798 s->me.qpel_avg = s->qdsp.avg_qpel_pixels_tab;
800 if (avctx->hwaccel) {
801 if (avctx->hwaccel->start_frame(avctx, buf, buf_size) < 0)
803 if (avctx->hwaccel->decode_slice(avctx, buf_start, (buf + buf_size) - buf_start) < 0)
805 if (avctx->hwaccel->end_frame(avctx) < 0)
810 ff_mpeg_er_frame_start(s);
812 v->bits = buf_size * 8;
813 v->end_mb_x = s->mb_width;
815 s->current_picture.f->linesize[0] <<= 1;
816 s->current_picture.f->linesize[1] <<= 1;
817 s->current_picture.f->linesize[2] <<= 1;
821 mb_height = s->mb_height >> v->field_mode;
824 av_log(v->s.avctx, AV_LOG_ERROR, "Invalid mb_height.\n");
828 for (i = 0; i <= n_slices; i++) {
829 if (i > 0 && slices[i - 1].mby_start >= mb_height) {
830 if (v->field_mode <= 0) {
831 av_log(v->s.avctx, AV_LOG_ERROR, "Slice %d starts beyond "
832 "picture boundary (%d >= %d)\n", i,
833 slices[i - 1].mby_start, mb_height);
837 v->blocks_off = s->mb_width * s->mb_height << 1;
838 v->mb_off = s->mb_stride * s->mb_height >> 1;
845 v->pic_header_flag = 0;
846 if (v->field_mode && i == n_slices1 + 2) {
847 if ((header_ret = ff_vc1_parse_frame_header_adv(v, &s->gb)) < 0) {
848 av_log(v->s.avctx, AV_LOG_ERROR, "Field header damaged\n");
849 if (avctx->err_recognition & AV_EF_EXPLODE)
853 } else if (get_bits1(&s->gb)) {
854 v->pic_header_flag = 1;
855 if ((header_ret = ff_vc1_parse_frame_header_adv(v, &s->gb)) < 0) {
856 av_log(v->s.avctx, AV_LOG_ERROR, "Slice header damaged\n");
857 if (avctx->err_recognition & AV_EF_EXPLODE)
865 s->start_mb_y = (i == 0) ? 0 : FFMAX(0, slices[i-1].mby_start % mb_height);
866 if (!v->field_mode || v->second_field)
867 s->end_mb_y = (i == n_slices ) ? mb_height : FFMIN(mb_height, slices[i].mby_start % mb_height);
869 s->end_mb_y = (i <= n_slices1 + 1) ? mb_height : FFMIN(mb_height, slices[i].mby_start % mb_height);
870 ff_vc1_decode_blocks(v);
872 s->gb = slices[i].gb;
876 s->current_picture.f->linesize[0] >>= 1;
877 s->current_picture.f->linesize[1] >>= 1;
878 s->current_picture.f->linesize[2] >>= 1;
881 if (v->s.pict_type != AV_PICTURE_TYPE_BI && v->s.pict_type != AV_PICTURE_TYPE_B) {
882 FFSWAP(uint8_t *, v->mv_f_next[0], v->mv_f[0]);
883 FFSWAP(uint8_t *, v->mv_f_next[1], v->mv_f[1]);
886 ff_dlog(s->avctx, "Consumed %i/%i bits\n",
887 get_bits_count(&s->gb), s->gb.size_in_bits);
888 // if (get_bits_count(&s->gb) > buf_size * 8)
891 ff_er_frame_end(&s->er);
896 if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
898 avctx->width = avctx->coded_width = v->output_width;
899 avctx->height = avctx->coded_height = v->output_height;
900 if (avctx->skip_frame >= AVDISCARD_NONREF)
902 #if CONFIG_WMV3IMAGE_DECODER || CONFIG_VC1IMAGE_DECODER
903 if (vc1_decode_sprites(v, &s->gb))
906 if ((ret = av_frame_ref(pict, v->sprite_output_frame)) < 0)
910 if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
911 if ((ret = av_frame_ref(pict, s->current_picture_ptr->f)) < 0)
913 ff_print_debug_info(s, s->current_picture_ptr);
915 } else if (s->last_picture_ptr) {
916 if ((ret = av_frame_ref(pict, s->last_picture_ptr->f)) < 0)
918 ff_print_debug_info(s, s->last_picture_ptr);
925 for (i = 0; i < n_slices; i++)
926 av_free(slices[i].buf);
932 for (i = 0; i < n_slices; i++)
933 av_free(slices[i].buf);
939 static const enum AVPixelFormat vc1_hwaccel_pixfmt_list_420[] = {
940 #if CONFIG_VC1_DXVA2_HWACCEL
941 AV_PIX_FMT_DXVA2_VLD,
943 #if CONFIG_VC1_D3D11VA_HWACCEL
944 AV_PIX_FMT_D3D11VA_VLD,
946 #if CONFIG_VC1_VAAPI_HWACCEL
947 AV_PIX_FMT_VAAPI_VLD,
949 #if CONFIG_VC1_VDPAU_HWACCEL
956 AVCodec ff_vc1_decoder = {
958 .long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-1"),
959 .type = AVMEDIA_TYPE_VIDEO,
960 .id = AV_CODEC_ID_VC1,
961 .priv_data_size = sizeof(VC1Context),
962 .init = vc1_decode_init,
963 .close = ff_vc1_decode_end,
964 .decode = vc1_decode_frame,
965 .flush = ff_mpeg_flush,
966 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY,
967 .pix_fmts = vc1_hwaccel_pixfmt_list_420,
968 .profiles = NULL_IF_CONFIG_SMALL(ff_vc1_profiles)
971 #if CONFIG_WMV3_DECODER
972 AVCodec ff_wmv3_decoder = {
974 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9"),
975 .type = AVMEDIA_TYPE_VIDEO,
976 .id = AV_CODEC_ID_WMV3,
977 .priv_data_size = sizeof(VC1Context),
978 .init = vc1_decode_init,
979 .close = ff_vc1_decode_end,
980 .decode = vc1_decode_frame,
981 .flush = ff_mpeg_flush,
982 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY,
983 .pix_fmts = vc1_hwaccel_pixfmt_list_420,
984 .profiles = NULL_IF_CONFIG_SMALL(ff_vc1_profiles)
988 #if CONFIG_WMV3IMAGE_DECODER
989 AVCodec ff_wmv3image_decoder = {
991 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 Image"),
992 .type = AVMEDIA_TYPE_VIDEO,
993 .id = AV_CODEC_ID_WMV3IMAGE,
994 .priv_data_size = sizeof(VC1Context),
995 .init = vc1_decode_init,
996 .close = ff_vc1_decode_end,
997 .decode = vc1_decode_frame,
998 .capabilities = AV_CODEC_CAP_DR1,
999 .flush = vc1_sprite_flush,
1000 .pix_fmts = (const enum AVPixelFormat[]) {
1007 #if CONFIG_VC1IMAGE_DECODER
1008 AVCodec ff_vc1image_decoder = {
1010 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 Image v2"),
1011 .type = AVMEDIA_TYPE_VIDEO,
1012 .id = AV_CODEC_ID_VC1IMAGE,
1013 .priv_data_size = sizeof(VC1Context),
1014 .init = vc1_decode_init,
1015 .close = ff_vc1_decode_end,
1016 .decode = vc1_decode_frame,
1017 .capabilities = AV_CODEC_CAP_DR1,
1018 .flush = vc1_sprite_flush,
1019 .pix_fmts = (const enum AVPixelFormat[]) {