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"
35 #include "msmpeg4data.h"
39 #if CONFIG_WMV3IMAGE_DECODER || CONFIG_VC1IMAGE_DECODER
43 * Transform coefficients for both sprites in 16.16 fixed point format,
44 * in the order they appear in the bitstream:
55 int effect_type, effect_flag;
56 int effect_pcount1, effect_pcount2; ///< amount of effect parameters stored in effect_params
57 int effect_params1[15], effect_params2[10]; ///< effect parameters in 16.16 fixed point format
60 static inline int get_fp_val(GetBitContext* gb)
62 return (get_bits_long(gb, 30) - (1 << 29)) << 1;
65 static void vc1_sprite_parse_transform(GetBitContext* gb, int c[7])
69 switch (get_bits(gb, 2)) {
72 c[2] = get_fp_val(gb);
76 c[0] = c[4] = get_fp_val(gb);
77 c[2] = get_fp_val(gb);
80 c[0] = get_fp_val(gb);
81 c[2] = get_fp_val(gb);
82 c[4] = get_fp_val(gb);
85 c[0] = get_fp_val(gb);
86 c[1] = get_fp_val(gb);
87 c[2] = get_fp_val(gb);
88 c[3] = get_fp_val(gb);
89 c[4] = get_fp_val(gb);
92 c[5] = get_fp_val(gb);
94 c[6] = get_fp_val(gb);
99 static void vc1_parse_sprites(VC1Context *v, GetBitContext* gb, SpriteData* sd)
101 AVCodecContext *avctx = v->s.avctx;
104 for (sprite = 0; sprite <= v->two_sprites; sprite++) {
105 vc1_sprite_parse_transform(gb, sd->coefs[sprite]);
106 if (sd->coefs[sprite][1] || sd->coefs[sprite][3])
107 avpriv_request_sample(avctx, "Non-zero rotation coefficients");
108 av_log(avctx, AV_LOG_DEBUG, sprite ? "S2:" : "S1:");
109 for (i = 0; i < 7; i++)
110 av_log(avctx, AV_LOG_DEBUG, " %d.%.3d",
111 sd->coefs[sprite][i] / (1<<16),
112 (abs(sd->coefs[sprite][i]) & 0xFFFF) * 1000 / (1 << 16));
113 av_log(avctx, AV_LOG_DEBUG, "\n");
117 if (sd->effect_type = get_bits_long(gb, 30)) {
118 switch (sd->effect_pcount1 = get_bits(gb, 4)) {
120 vc1_sprite_parse_transform(gb, sd->effect_params1);
123 vc1_sprite_parse_transform(gb, sd->effect_params1);
124 vc1_sprite_parse_transform(gb, sd->effect_params1 + 7);
127 for (i = 0; i < sd->effect_pcount1; i++)
128 sd->effect_params1[i] = get_fp_val(gb);
130 if (sd->effect_type != 13 || sd->effect_params1[0] != sd->coefs[0][6]) {
131 // effect 13 is simple alpha blending and matches the opacity above
132 av_log(avctx, AV_LOG_DEBUG, "Effect: %d; params: ", sd->effect_type);
133 for (i = 0; i < sd->effect_pcount1; i++)
134 av_log(avctx, AV_LOG_DEBUG, " %d.%.2d",
135 sd->effect_params1[i] / (1 << 16),
136 (abs(sd->effect_params1[i]) & 0xFFFF) * 1000 / (1 << 16));
137 av_log(avctx, AV_LOG_DEBUG, "\n");
140 sd->effect_pcount2 = get_bits(gb, 16);
141 if (sd->effect_pcount2 > 10) {
142 av_log(avctx, AV_LOG_ERROR, "Too many effect parameters\n");
144 } else if (sd->effect_pcount2) {
146 av_log(avctx, AV_LOG_DEBUG, "Effect params 2: ");
147 while (++i < sd->effect_pcount2) {
148 sd->effect_params2[i] = get_fp_val(gb);
149 av_log(avctx, AV_LOG_DEBUG, " %d.%.2d",
150 sd->effect_params2[i] / (1 << 16),
151 (abs(sd->effect_params2[i]) & 0xFFFF) * 1000 / (1 << 16));
153 av_log(avctx, AV_LOG_DEBUG, "\n");
156 if (sd->effect_flag = get_bits1(gb))
157 av_log(avctx, AV_LOG_DEBUG, "Effect flag set\n");
159 if (get_bits_count(gb) >= gb->size_in_bits +
160 (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE ? 64 : 0))
161 av_log(avctx, AV_LOG_ERROR, "Buffer overrun\n");
162 if (get_bits_count(gb) < gb->size_in_bits - 8)
163 av_log(avctx, AV_LOG_WARNING, "Buffer not fully read\n");
166 static void vc1_draw_sprites(VC1Context *v, SpriteData* sd)
168 int i, plane, row, sprite;
169 int sr_cache[2][2] = { { -1, -1 }, { -1, -1 } };
170 uint8_t* src_h[2][2];
171 int xoff[2], xadv[2], yoff[2], yadv[2], alpha;
173 MpegEncContext *s = &v->s;
175 for (i = 0; i < 2; i++) {
176 xoff[i] = av_clip(sd->coefs[i][2], 0, v->sprite_width-1 << 16);
177 xadv[i] = sd->coefs[i][0];
178 if (xadv[i] != 1<<16 || (v->sprite_width << 16) - (v->output_width << 16) - xoff[i])
179 xadv[i] = av_clip(xadv[i], 0, ((v->sprite_width<<16) - xoff[i] - 1) / v->output_width);
181 yoff[i] = av_clip(sd->coefs[i][5], 0, v->sprite_height-1 << 16);
182 yadv[i] = av_clip(sd->coefs[i][4], 0, ((v->sprite_height << 16) - yoff[i]) / v->output_height);
184 alpha = av_clip(sd->coefs[1][6], 0, (1<<16) - 1);
186 for (plane = 0; plane < (s->flags&CODEC_FLAG_GRAY ? 1 : 3); plane++) {
187 int width = v->output_width>>!!plane;
189 for (row = 0; row < v->output_height>>!!plane; row++) {
190 uint8_t *dst = v->sprite_output_frame->data[plane] +
191 v->sprite_output_frame->linesize[plane] * row;
193 for (sprite = 0; sprite <= v->two_sprites; sprite++) {
194 uint8_t *iplane = s->current_picture.f->data[plane];
195 int iline = s->current_picture.f->linesize[plane];
196 int ycoord = yoff[sprite] + yadv[sprite] * row;
197 int yline = ycoord >> 16;
199 ysub[sprite] = ycoord & 0xFFFF;
201 iplane = s->last_picture.f->data[plane];
202 iline = s->last_picture.f->linesize[plane];
204 next_line = FFMIN(yline + 1, (v->sprite_height >> !!plane) - 1) * iline;
205 if (!(xoff[sprite] & 0xFFFF) && xadv[sprite] == 1 << 16) {
206 src_h[sprite][0] = iplane + (xoff[sprite] >> 16) + yline * iline;
208 src_h[sprite][1] = iplane + (xoff[sprite] >> 16) + next_line;
210 if (sr_cache[sprite][0] != yline) {
211 if (sr_cache[sprite][1] == yline) {
212 FFSWAP(uint8_t*, v->sr_rows[sprite][0], v->sr_rows[sprite][1]);
213 FFSWAP(int, sr_cache[sprite][0], sr_cache[sprite][1]);
215 v->vc1dsp.sprite_h(v->sr_rows[sprite][0], iplane + yline * iline, xoff[sprite], xadv[sprite], width);
216 sr_cache[sprite][0] = yline;
219 if (ysub[sprite] && sr_cache[sprite][1] != yline + 1) {
220 v->vc1dsp.sprite_h(v->sr_rows[sprite][1],
221 iplane + next_line, xoff[sprite],
222 xadv[sprite], width);
223 sr_cache[sprite][1] = yline + 1;
225 src_h[sprite][0] = v->sr_rows[sprite][0];
226 src_h[sprite][1] = v->sr_rows[sprite][1];
230 if (!v->two_sprites) {
232 v->vc1dsp.sprite_v_single(dst, src_h[0][0], src_h[0][1], ysub[0], width);
234 memcpy(dst, src_h[0][0], width);
237 if (ysub[0] && ysub[1]) {
238 v->vc1dsp.sprite_v_double_twoscale(dst, src_h[0][0], src_h[0][1], ysub[0],
239 src_h[1][0], src_h[1][1], ysub[1], alpha, width);
240 } else if (ysub[0]) {
241 v->vc1dsp.sprite_v_double_onescale(dst, src_h[0][0], src_h[0][1], ysub[0],
242 src_h[1][0], alpha, width);
243 } else if (ysub[1]) {
244 v->vc1dsp.sprite_v_double_onescale(dst, src_h[1][0], src_h[1][1], ysub[1],
245 src_h[0][0], (1<<16)-1-alpha, width);
247 v->vc1dsp.sprite_v_double_noscale(dst, src_h[0][0], src_h[1][0], alpha, width);
253 for (i = 0; i < 2; i++) {
263 static int vc1_decode_sprites(VC1Context *v, GetBitContext* gb)
265 MpegEncContext *s = &v->s;
266 AVCodecContext *avctx = s->avctx;
269 vc1_parse_sprites(v, gb, &sd);
271 if (!s->current_picture.f->data[0]) {
272 av_log(avctx, AV_LOG_ERROR, "Got no sprites\n");
276 if (v->two_sprites && (!s->last_picture_ptr || !s->last_picture.f->data[0])) {
277 av_log(avctx, AV_LOG_WARNING, "Need two sprites, only got one\n");
281 av_frame_unref(v->sprite_output_frame);
282 if (ff_get_buffer(avctx, v->sprite_output_frame, 0) < 0) {
283 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
287 vc1_draw_sprites(v, &sd);
292 static void vc1_sprite_flush(AVCodecContext *avctx)
294 VC1Context *v = avctx->priv_data;
295 MpegEncContext *s = &v->s;
296 AVFrame *f = s->current_picture.f;
299 /* Windows Media Image codecs have a convergence interval of two keyframes.
300 Since we can't enforce it, clear to black the missing sprite. This is
301 wrong but it looks better than doing nothing. */
304 for (plane = 0; plane < (s->flags&CODEC_FLAG_GRAY ? 1 : 3); plane++)
305 for (i = 0; i < v->sprite_height>>!!plane; i++)
306 memset(f->data[plane] + i * f->linesize[plane],
307 plane ? 128 : 0, f->linesize[plane]);
312 av_cold int ff_vc1_decode_init_alloc_tables(VC1Context *v)
314 MpegEncContext *s = &v->s;
316 int mb_height = FFALIGN(s->mb_height, 2);
318 /* Allocate mb bitplanes */
319 v->mv_type_mb_plane = av_malloc (s->mb_stride * mb_height);
320 v->direct_mb_plane = av_malloc (s->mb_stride * mb_height);
321 v->forward_mb_plane = av_malloc (s->mb_stride * mb_height);
322 v->fieldtx_plane = av_mallocz(s->mb_stride * mb_height);
323 v->acpred_plane = av_malloc (s->mb_stride * mb_height);
324 v->over_flags_plane = av_malloc (s->mb_stride * mb_height);
326 v->n_allocated_blks = s->mb_width + 2;
327 v->block = av_malloc(sizeof(*v->block) * v->n_allocated_blks);
328 v->cbp_base = av_malloc(sizeof(v->cbp_base[0]) * 2 * s->mb_stride);
329 v->cbp = v->cbp_base + s->mb_stride;
330 v->ttblk_base = av_malloc(sizeof(v->ttblk_base[0]) * 2 * s->mb_stride);
331 v->ttblk = v->ttblk_base + s->mb_stride;
332 v->is_intra_base = av_mallocz(sizeof(v->is_intra_base[0]) * 2 * s->mb_stride);
333 v->is_intra = v->is_intra_base + s->mb_stride;
334 v->luma_mv_base = av_malloc(sizeof(v->luma_mv_base[0]) * 2 * s->mb_stride);
335 v->luma_mv = v->luma_mv_base + s->mb_stride;
337 /* allocate block type info in that way so it could be used with s->block_index[] */
338 v->mb_type_base = av_malloc(s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2);
339 v->mb_type[0] = v->mb_type_base + s->b8_stride + 1;
340 v->mb_type[1] = v->mb_type_base + s->b8_stride * (mb_height * 2 + 1) + s->mb_stride + 1;
341 v->mb_type[2] = v->mb_type[1] + s->mb_stride * (mb_height + 1);
343 /* allocate memory to store block level MV info */
344 v->blk_mv_type_base = av_mallocz( s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2);
345 v->blk_mv_type = v->blk_mv_type_base + s->b8_stride + 1;
346 v->mv_f_base = av_mallocz(2 * (s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2));
347 v->mv_f[0] = v->mv_f_base + s->b8_stride + 1;
348 v->mv_f[1] = v->mv_f[0] + (s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2);
349 v->mv_f_next_base = av_mallocz(2 * (s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2));
350 v->mv_f_next[0] = v->mv_f_next_base + s->b8_stride + 1;
351 v->mv_f_next[1] = v->mv_f_next[0] + (s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2);
353 /* Init coded blocks info */
354 if (v->profile == PROFILE_ADVANCED) {
355 // if (alloc_bitplane(&v->over_flags_plane, s->mb_width, s->mb_height) < 0)
357 // if (alloc_bitplane(&v->ac_pred_plane, s->mb_width, s->mb_height) < 0)
361 ff_intrax8_common_init(&v->x8,s);
363 if (s->avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || s->avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
364 for (i = 0; i < 4; i++)
365 if (!(v->sr_rows[i >> 1][i & 1] = av_malloc(v->output_width))) return -1;
368 if (!v->mv_type_mb_plane || !v->direct_mb_plane || !v->acpred_plane || !v->over_flags_plane ||
369 !v->block || !v->cbp_base || !v->ttblk_base || !v->is_intra_base || !v->luma_mv_base ||
371 av_freep(&v->mv_type_mb_plane);
372 av_freep(&v->direct_mb_plane);
373 av_freep(&v->acpred_plane);
374 av_freep(&v->over_flags_plane);
376 av_freep(&v->cbp_base);
377 av_freep(&v->ttblk_base);
378 av_freep(&v->is_intra_base);
379 av_freep(&v->luma_mv_base);
380 av_freep(&v->mb_type_base);
381 return AVERROR(ENOMEM);
387 av_cold void ff_vc1_init_transposed_scantables(VC1Context *v)
390 for (i = 0; i < 64; i++) {
391 #define transpose(x) ((x >> 3) | ((x & 7) << 3))
392 v->zz_8x8[0][i] = transpose(ff_wmv1_scantable[0][i]);
393 v->zz_8x8[1][i] = transpose(ff_wmv1_scantable[1][i]);
394 v->zz_8x8[2][i] = transpose(ff_wmv1_scantable[2][i]);
395 v->zz_8x8[3][i] = transpose(ff_wmv1_scantable[3][i]);
396 v->zzi_8x8[i] = transpose(ff_vc1_adv_interlaced_8x8_zz[i]);
402 /** Initialize a VC1/WMV3 decoder
403 * @todo TODO: Handle VC-1 IDUs (Transport level?)
404 * @todo TODO: Decypher remaining bits in extra_data
406 static av_cold int vc1_decode_init(AVCodecContext *avctx)
408 VC1Context *v = avctx->priv_data;
409 MpegEncContext *s = &v->s;
412 /* save the container output size for WMImage */
413 v->output_width = avctx->width;
414 v->output_height = avctx->height;
416 if (!avctx->extradata_size || !avctx->extradata)
418 if (!(avctx->flags & CODEC_FLAG_GRAY))
419 avctx->pix_fmt = ff_get_format(avctx, avctx->codec->pix_fmts);
421 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
424 if (ff_vc1_init_common(v) < 0)
426 ff_blockdsp_init(&s->bdsp, avctx);
427 ff_h264chroma_init(&v->h264chroma, 8);
428 ff_qpeldsp_init(&s->qdsp);
430 if (avctx->codec_id == AV_CODEC_ID_WMV3 || avctx->codec_id == AV_CODEC_ID_WMV3IMAGE) {
433 // looks like WMV3 has a sequence header stored in the extradata
434 // advanced sequence header may be before the first frame
435 // the last byte of the extradata is a version number, 1 for the
436 // samples we can decode
438 init_get_bits(&gb, avctx->extradata, avctx->extradata_size*8);
440 if (ff_vc1_decode_sequence_header(avctx, v, &gb) < 0)
443 count = avctx->extradata_size*8 - get_bits_count(&gb);
445 av_log(avctx, AV_LOG_INFO, "Extra data: %i bits left, value: %X\n",
446 count, get_bits(&gb, count));
447 } else if (count < 0) {
448 av_log(avctx, AV_LOG_INFO, "Read %i bits in overflow\n", -count);
450 } else { // VC1/WVC1/WVP2
451 const uint8_t *start = avctx->extradata;
452 uint8_t *end = avctx->extradata + avctx->extradata_size;
455 uint8_t *buf2 = NULL;
456 int seq_initialized = 0, ep_initialized = 0;
458 if (avctx->extradata_size < 16) {
459 av_log(avctx, AV_LOG_ERROR, "Extradata size too small: %i\n", avctx->extradata_size);
463 buf2 = av_mallocz(avctx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
464 start = find_next_marker(start, end); // in WVC1 extradata first byte is its size, but can be 0 in mkv
466 for (; next < end; start = next) {
467 next = find_next_marker(start + 4, end);
468 size = next - start - 4;
471 buf2_size = vc1_unescape_buffer(start + 4, size, buf2);
472 init_get_bits(&gb, buf2, buf2_size * 8);
473 switch (AV_RB32(start)) {
474 case VC1_CODE_SEQHDR:
475 if (ff_vc1_decode_sequence_header(avctx, v, &gb) < 0) {
481 case VC1_CODE_ENTRYPOINT:
482 if (ff_vc1_decode_entry_point(avctx, v, &gb) < 0) {
491 if (!seq_initialized || !ep_initialized) {
492 av_log(avctx, AV_LOG_ERROR, "Incomplete extradata\n");
495 v->res_sprite = (avctx->codec_id == AV_CODEC_ID_VC1IMAGE);
498 v->sprite_output_frame = av_frame_alloc();
499 if (!v->sprite_output_frame)
500 return AVERROR(ENOMEM);
502 avctx->profile = v->profile;
503 if (v->profile == PROFILE_ADVANCED)
504 avctx->level = v->level;
506 avctx->has_b_frames = !!avctx->max_b_frames;
508 if (v->color_prim == 1 || v->color_prim == 5 || v->color_prim == 6)
509 avctx->color_primaries = v->color_prim;
510 if (v->transfer_char == 1 || v->transfer_char == 7)
511 avctx->color_trc = v->transfer_char;
512 if (v->matrix_coef == 1 || v->matrix_coef == 6 || v->matrix_coef == 7)
513 avctx->colorspace = v->matrix_coef;
515 s->mb_width = (avctx->coded_width + 15) >> 4;
516 s->mb_height = (avctx->coded_height + 15) >> 4;
518 if (v->profile == PROFILE_ADVANCED || v->res_fasttx) {
519 ff_vc1_init_transposed_scantables(v);
521 memcpy(v->zz_8x8, ff_wmv1_scantable, 4*64);
526 if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
527 v->sprite_width = avctx->coded_width;
528 v->sprite_height = avctx->coded_height;
530 avctx->coded_width = avctx->width = v->output_width;
531 avctx->coded_height = avctx->height = v->output_height;
533 // prevent 16.16 overflows
534 if (v->sprite_width > 1 << 14 ||
535 v->sprite_height > 1 << 14 ||
536 v->output_width > 1 << 14 ||
537 v->output_height > 1 << 14) return -1;
542 /** Close a VC1/WMV3 decoder
543 * @warning Initial try at using MpegEncContext stuff
545 av_cold int ff_vc1_decode_end(AVCodecContext *avctx)
547 VC1Context *v = avctx->priv_data;
550 av_frame_free(&v->sprite_output_frame);
552 for (i = 0; i < 4; i++)
553 av_freep(&v->sr_rows[i >> 1][i & 1]);
554 av_freep(&v->hrd_rate);
555 av_freep(&v->hrd_buffer);
556 ff_mpv_common_end(&v->s);
557 av_freep(&v->mv_type_mb_plane);
558 av_freep(&v->direct_mb_plane);
559 av_freep(&v->forward_mb_plane);
560 av_freep(&v->fieldtx_plane);
561 av_freep(&v->acpred_plane);
562 av_freep(&v->over_flags_plane);
563 av_freep(&v->mb_type_base);
564 av_freep(&v->blk_mv_type_base);
565 av_freep(&v->mv_f_base);
566 av_freep(&v->mv_f_next_base);
568 av_freep(&v->cbp_base);
569 av_freep(&v->ttblk_base);
570 av_freep(&v->is_intra_base); // FIXME use v->mb_type[]
571 av_freep(&v->luma_mv_base);
572 ff_intrax8_common_end(&v->x8);
577 /** Decode a VC1/WMV3 frame
578 * @todo TODO: Handle VC-1 IDUs (Transport level?)
580 static int vc1_decode_frame(AVCodecContext *avctx, void *data,
581 int *got_frame, AVPacket *avpkt)
583 const uint8_t *buf = avpkt->data;
584 int buf_size = avpkt->size, n_slices = 0, i, ret;
585 VC1Context *v = avctx->priv_data;
586 MpegEncContext *s = &v->s;
587 AVFrame *pict = data;
588 uint8_t *buf2 = NULL;
589 const uint8_t *buf_start = buf;
590 int mb_height, n_slices1;
595 } *slices = NULL, *tmp;
597 /* no supplementary picture */
598 if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == VC1_CODE_ENDOFSEQ)) {
599 /* special case for last picture */
600 if (s->low_delay == 0 && s->next_picture_ptr) {
601 if ((ret = av_frame_ref(pict, s->next_picture_ptr->f)) < 0)
603 s->next_picture_ptr = NULL;
611 //for advanced profile we may need to parse and unescape data
612 if (avctx->codec_id == AV_CODEC_ID_VC1 || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
614 buf2 = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
616 if (IS_MARKER(AV_RB32(buf))) { /* frame starts with marker and needs to be parsed */
617 const uint8_t *start, *end, *next;
621 for (start = buf, end = buf + buf_size; next < end; start = next) {
622 next = find_next_marker(start + 4, end);
623 size = next - start - 4;
624 if (size <= 0) continue;
625 switch (AV_RB32(start)) {
629 buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
631 case VC1_CODE_FIELD: {
633 tmp = av_realloc(slices, sizeof(*slices) * (n_slices+1));
637 slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
638 if (!slices[n_slices].buf)
640 buf_size3 = vc1_unescape_buffer(start + 4, size,
641 slices[n_slices].buf);
642 init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
644 /* assuming that the field marker is at the exact middle,
646 slices[n_slices].mby_start = s->mb_height >> 1;
647 n_slices1 = n_slices - 1; // index of the last slice of the first field
651 case VC1_CODE_ENTRYPOINT: /* it should be before frame data */
652 buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
653 init_get_bits(&s->gb, buf2, buf_size2 * 8);
654 ff_vc1_decode_entry_point(avctx, v, &s->gb);
656 case VC1_CODE_SLICE: {
658 tmp = av_realloc(slices, sizeof(*slices) * (n_slices+1));
662 slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
663 if (!slices[n_slices].buf)
665 buf_size3 = vc1_unescape_buffer(start + 4, size,
666 slices[n_slices].buf);
667 init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
669 slices[n_slices].mby_start = get_bits(&slices[n_slices].gb, 9);
675 } else if (v->interlace && ((buf[0] & 0xC0) == 0xC0)) { /* WVC1 interlaced stores both fields divided by marker */
676 const uint8_t *divider;
679 divider = find_next_marker(buf, buf + buf_size);
680 if ((divider == (buf + buf_size)) || AV_RB32(divider) != VC1_CODE_FIELD) {
681 av_log(avctx, AV_LOG_ERROR, "Error in WVC1 interlaced frame\n");
683 } else { // found field marker, unescape second field
684 tmp = av_realloc(slices, sizeof(*slices) * (n_slices+1));
688 slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
689 if (!slices[n_slices].buf)
691 buf_size3 = vc1_unescape_buffer(divider + 4, buf + buf_size - divider - 4, slices[n_slices].buf);
692 init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
694 slices[n_slices].mby_start = s->mb_height >> 1;
695 n_slices1 = n_slices - 1;
698 buf_size2 = vc1_unescape_buffer(buf, divider - buf, buf2);
700 buf_size2 = vc1_unescape_buffer(buf, buf_size, buf2);
702 init_get_bits(&s->gb, buf2, buf_size2*8);
704 init_get_bits(&s->gb, buf, buf_size*8);
707 v->new_sprite = !get_bits1(&s->gb);
708 v->two_sprites = get_bits1(&s->gb);
709 /* res_sprite means a Windows Media Image stream, AV_CODEC_ID_*IMAGE means
710 we're using the sprite compositor. These are intentionally kept separate
711 so you can get the raw sprites by using the wmv3 decoder for WMVP or
712 the vc1 one for WVP2 */
713 if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
715 // switch AVCodecContext parameters to those of the sprites
716 avctx->width = avctx->coded_width = v->sprite_width;
717 avctx->height = avctx->coded_height = v->sprite_height;
724 if (s->context_initialized &&
725 (s->width != avctx->coded_width ||
726 s->height != avctx->coded_height)) {
727 ff_vc1_decode_end(avctx);
730 if (!s->context_initialized) {
731 if (ff_msmpeg4_decode_init(avctx) < 0)
733 if (ff_vc1_decode_init_alloc_tables(v) < 0) {
734 ff_mpv_common_end(s);
738 s->low_delay = !avctx->has_b_frames || v->res_sprite;
740 if (v->profile == PROFILE_ADVANCED) {
741 s->h_edge_pos = avctx->coded_width;
742 s->v_edge_pos = avctx->coded_height;
746 // do parse frame header
747 v->pic_header_flag = 0;
748 v->first_pic_header_flag = 1;
749 if (v->profile < PROFILE_ADVANCED) {
750 if (ff_vc1_parse_frame_header(v, &s->gb) < 0) {
754 if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
758 v->first_pic_header_flag = 0;
760 if ((avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE)
761 && s->pict_type != AV_PICTURE_TYPE_I) {
762 av_log(v->s.avctx, AV_LOG_ERROR, "Sprite decoder: expected I-frame\n");
766 // for skipping the frame
767 s->current_picture.f->pict_type = s->pict_type;
768 s->current_picture.f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
770 /* skip B-frames if we don't have reference frames */
771 if (!s->last_picture_ptr && (s->pict_type == AV_PICTURE_TYPE_B || s->droppable)) {
774 if ((avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B) ||
775 (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I) ||
776 avctx->skip_frame >= AVDISCARD_ALL) {
780 if (s->next_p_frame_damaged) {
781 if (s->pict_type == AV_PICTURE_TYPE_B)
784 s->next_p_frame_damaged = 0;
787 if (ff_mpv_frame_start(s, avctx) < 0) {
791 // process pulldown flags
792 s->current_picture_ptr->f->repeat_pict = 0;
793 // Pulldown flags are only valid when 'broadcast' has been set.
794 // So ticks_per_frame will be 2
797 s->current_picture_ptr->f->repeat_pict = 1;
798 } else if (v->rptfrm) {
800 s->current_picture_ptr->f->repeat_pict = v->rptfrm * 2;
803 s->me.qpel_put = s->qdsp.put_qpel_pixels_tab;
804 s->me.qpel_avg = s->qdsp.avg_qpel_pixels_tab;
806 if (avctx->hwaccel) {
807 if (avctx->hwaccel->start_frame(avctx, buf, buf_size) < 0)
809 if (avctx->hwaccel->decode_slice(avctx, buf_start, (buf + buf_size) - buf_start) < 0)
811 if (avctx->hwaccel->end_frame(avctx) < 0)
816 ff_mpeg_er_frame_start(s);
818 v->bits = buf_size * 8;
819 v->end_mb_x = s->mb_width;
821 s->current_picture.f->linesize[0] <<= 1;
822 s->current_picture.f->linesize[1] <<= 1;
823 s->current_picture.f->linesize[2] <<= 1;
827 mb_height = s->mb_height >> v->field_mode;
830 av_log(v->s.avctx, AV_LOG_ERROR, "Invalid mb_height.\n");
834 for (i = 0; i <= n_slices; i++) {
835 if (i > 0 && slices[i - 1].mby_start >= mb_height) {
836 if (v->field_mode <= 0) {
837 av_log(v->s.avctx, AV_LOG_ERROR, "Slice %d starts beyond "
838 "picture boundary (%d >= %d)\n", i,
839 slices[i - 1].mby_start, mb_height);
843 v->blocks_off = s->mb_width * s->mb_height << 1;
844 v->mb_off = s->mb_stride * s->mb_height >> 1;
851 v->pic_header_flag = 0;
852 if (v->field_mode && i == n_slices1 + 2) {
853 if ((header_ret = ff_vc1_parse_frame_header_adv(v, &s->gb)) < 0) {
854 av_log(v->s.avctx, AV_LOG_ERROR, "Field header damaged\n");
855 if (avctx->err_recognition & AV_EF_EXPLODE)
859 } else if (get_bits1(&s->gb)) {
860 v->pic_header_flag = 1;
861 if ((header_ret = ff_vc1_parse_frame_header_adv(v, &s->gb)) < 0) {
862 av_log(v->s.avctx, AV_LOG_ERROR, "Slice header damaged\n");
863 if (avctx->err_recognition & AV_EF_EXPLODE)
871 s->start_mb_y = (i == 0) ? 0 : FFMAX(0, slices[i-1].mby_start % mb_height);
872 if (!v->field_mode || v->second_field)
873 s->end_mb_y = (i == n_slices ) ? mb_height : FFMIN(mb_height, slices[i].mby_start % mb_height);
875 s->end_mb_y = (i <= n_slices1 + 1) ? mb_height : FFMIN(mb_height, slices[i].mby_start % mb_height);
876 ff_vc1_decode_blocks(v);
878 s->gb = slices[i].gb;
882 s->current_picture.f->linesize[0] >>= 1;
883 s->current_picture.f->linesize[1] >>= 1;
884 s->current_picture.f->linesize[2] >>= 1;
887 if (v->s.pict_type != AV_PICTURE_TYPE_BI && v->s.pict_type != AV_PICTURE_TYPE_B) {
888 FFSWAP(uint8_t *, v->mv_f_next[0], v->mv_f[0]);
889 FFSWAP(uint8_t *, v->mv_f_next[1], v->mv_f[1]);
892 av_dlog(s->avctx, "Consumed %i/%i bits\n",
893 get_bits_count(&s->gb), s->gb.size_in_bits);
894 // if (get_bits_count(&s->gb) > buf_size * 8)
897 ff_er_frame_end(&s->er);
902 if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
904 avctx->width = avctx->coded_width = v->output_width;
905 avctx->height = avctx->coded_height = v->output_height;
906 if (avctx->skip_frame >= AVDISCARD_NONREF)
908 #if CONFIG_WMV3IMAGE_DECODER || CONFIG_VC1IMAGE_DECODER
909 if (vc1_decode_sprites(v, &s->gb))
912 if ((ret = av_frame_ref(pict, v->sprite_output_frame)) < 0)
916 if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
917 if ((ret = av_frame_ref(pict, s->current_picture_ptr->f)) < 0)
919 ff_print_debug_info(s, s->current_picture_ptr);
921 } else if (s->last_picture_ptr) {
922 if ((ret = av_frame_ref(pict, s->last_picture_ptr->f)) < 0)
924 ff_print_debug_info(s, s->last_picture_ptr);
931 for (i = 0; i < n_slices; i++)
932 av_free(slices[i].buf);
938 for (i = 0; i < n_slices; i++)
939 av_free(slices[i].buf);
945 static const AVProfile profiles[] = {
946 { FF_PROFILE_VC1_SIMPLE, "Simple" },
947 { FF_PROFILE_VC1_MAIN, "Main" },
948 { FF_PROFILE_VC1_COMPLEX, "Complex" },
949 { FF_PROFILE_VC1_ADVANCED, "Advanced" },
950 { FF_PROFILE_UNKNOWN },
953 static const enum AVPixelFormat vc1_hwaccel_pixfmt_list_420[] = {
954 #if CONFIG_VC1_DXVA2_HWACCEL
955 AV_PIX_FMT_DXVA2_VLD,
957 #if CONFIG_VC1_VAAPI_HWACCEL
958 AV_PIX_FMT_VAAPI_VLD,
960 #if CONFIG_VC1_VDPAU_HWACCEL
967 AVCodec ff_vc1_decoder = {
969 .long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-1"),
970 .type = AVMEDIA_TYPE_VIDEO,
971 .id = AV_CODEC_ID_VC1,
972 .priv_data_size = sizeof(VC1Context),
973 .init = vc1_decode_init,
974 .close = ff_vc1_decode_end,
975 .decode = vc1_decode_frame,
976 .flush = ff_mpeg_flush,
977 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY,
978 .pix_fmts = vc1_hwaccel_pixfmt_list_420,
979 .profiles = NULL_IF_CONFIG_SMALL(profiles)
982 #if CONFIG_WMV3_DECODER
983 AVCodec ff_wmv3_decoder = {
985 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9"),
986 .type = AVMEDIA_TYPE_VIDEO,
987 .id = AV_CODEC_ID_WMV3,
988 .priv_data_size = sizeof(VC1Context),
989 .init = vc1_decode_init,
990 .close = ff_vc1_decode_end,
991 .decode = vc1_decode_frame,
992 .flush = ff_mpeg_flush,
993 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY,
994 .pix_fmts = vc1_hwaccel_pixfmt_list_420,
995 .profiles = NULL_IF_CONFIG_SMALL(profiles)
999 #if CONFIG_WMV3IMAGE_DECODER
1000 AVCodec ff_wmv3image_decoder = {
1001 .name = "wmv3image",
1002 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 Image"),
1003 .type = AVMEDIA_TYPE_VIDEO,
1004 .id = AV_CODEC_ID_WMV3IMAGE,
1005 .priv_data_size = sizeof(VC1Context),
1006 .init = vc1_decode_init,
1007 .close = ff_vc1_decode_end,
1008 .decode = vc1_decode_frame,
1009 .capabilities = CODEC_CAP_DR1,
1010 .flush = vc1_sprite_flush,
1011 .pix_fmts = (const enum AVPixelFormat[]) {
1018 #if CONFIG_VC1IMAGE_DECODER
1019 AVCodec ff_vc1image_decoder = {
1021 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 Image v2"),
1022 .type = AVMEDIA_TYPE_VIDEO,
1023 .id = AV_CODEC_ID_VC1IMAGE,
1024 .priv_data_size = sizeof(VC1Context),
1025 .init = vc1_decode_init,
1026 .close = ff_vc1_decode_end,
1027 .decode = vc1_decode_frame,
1028 .capabilities = CODEC_CAP_DR1,
1029 .flush = vc1_sprite_flush,
1030 .pix_fmts = (const enum AVPixelFormat[]) {