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 FFmpeg.
9 * FFmpeg 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 * FFmpeg 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 FFmpeg; 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
29 #include "config_components.h"
33 #include "codec_internal.h"
36 #include "hwaccel_internal.h"
39 #include "mpegutils.h"
40 #include "mpegvideo.h"
41 #include "mpegvideodec.h"
42 #include "msmpeg4_vc1_data.h"
44 #include "simple_idct.h"
47 #include "vc1_vlc_data.h"
48 #include "libavutil/attributes.h"
49 #include "libavutil/avassert.h"
50 #include "libavutil/imgutils.h"
51 #include "libavutil/mem.h"
52 #include "libavutil/thread.h"
55 static const enum AVPixelFormat vc1_hwaccel_pixfmt_list_420
[] = {
56 #if CONFIG_VC1_DXVA2_HWACCEL
59 #if CONFIG_VC1_D3D11VA_HWACCEL
60 AV_PIX_FMT_D3D11VA_VLD
,
63 #if CONFIG_VC1_D3D12VA_HWACCEL
66 #if CONFIG_VC1_NVDEC_HWACCEL
69 #if CONFIG_VC1_VAAPI_HWACCEL
72 #if CONFIG_VC1_VDPAU_HWACCEL
79 #if CONFIG_WMV3IMAGE_DECODER || CONFIG_VC1IMAGE_DECODER
81 typedef struct SpriteData
{
83 * Transform coefficients for both sprites in 16.16 fixed point format,
84 * in the order they appear in the bitstream:
95 int effect_type
, effect_flag
;
96 int effect_pcount1
, effect_pcount2
; ///< amount of effect parameters stored in effect_params
97 int effect_params1
[15], effect_params2
[10]; ///< effect parameters in 16.16 fixed point format
100 static inline int get_fp_val(GetBitContext
* gb
)
102 return (get_bits_long(gb
, 30) - (1 << 29)) << 1;
105 static void vc1_sprite_parse_transform(GetBitContext
* gb
, int c
[7])
109 switch (get_bits(gb
, 2)) {
112 c
[2] = get_fp_val(gb
);
116 c
[0] = c
[4] = get_fp_val(gb
);
117 c
[2] = get_fp_val(gb
);
120 c
[0] = get_fp_val(gb
);
121 c
[2] = get_fp_val(gb
);
122 c
[4] = get_fp_val(gb
);
125 c
[0] = get_fp_val(gb
);
126 c
[1] = get_fp_val(gb
);
127 c
[2] = get_fp_val(gb
);
128 c
[3] = get_fp_val(gb
);
129 c
[4] = get_fp_val(gb
);
132 c
[5] = get_fp_val(gb
);
134 c
[6] = get_fp_val(gb
);
139 static int vc1_parse_sprites(VC1Context
*v
, GetBitContext
* gb
, SpriteData
* sd
)
141 AVCodecContext
*avctx
= v
->s
.avctx
;
144 for (sprite
= 0; sprite
<= v
->two_sprites
; sprite
++) {
145 vc1_sprite_parse_transform(gb
, sd
->coefs
[sprite
]);
146 if (sd
->coefs
[sprite
][1] || sd
->coefs
[sprite
][3])
147 avpriv_request_sample(avctx
, "Non-zero rotation coefficients");
148 av_log(avctx
, AV_LOG_DEBUG
, sprite
? "S2:" : "S1:");
149 for (i
= 0; i
< 7; i
++)
150 av_log(avctx
, AV_LOG_DEBUG
, " %d.%.3d",
151 sd
->coefs
[sprite
][i
] / (1<<16),
152 (abs(sd
->coefs
[sprite
][i
]) & 0xFFFF) * 1000 / (1 << 16));
153 av_log(avctx
, AV_LOG_DEBUG
, "\n");
157 if (sd
->effect_type
= get_bits_long(gb
, 30)) {
158 switch (sd
->effect_pcount1
= get_bits(gb
, 4)) {
160 vc1_sprite_parse_transform(gb
, sd
->effect_params1
);
163 vc1_sprite_parse_transform(gb
, sd
->effect_params1
);
164 vc1_sprite_parse_transform(gb
, sd
->effect_params1
+ 7);
167 for (i
= 0; i
< sd
->effect_pcount1
; i
++)
168 sd
->effect_params1
[i
] = get_fp_val(gb
);
170 if (sd
->effect_type
!= 13 || sd
->effect_params1
[0] != sd
->coefs
[0][6]) {
171 // effect 13 is simple alpha blending and matches the opacity above
172 av_log(avctx
, AV_LOG_DEBUG
, "Effect: %d; params: ", sd
->effect_type
);
173 for (i
= 0; i
< sd
->effect_pcount1
; i
++)
174 av_log(avctx
, AV_LOG_DEBUG
, " %d.%.2d",
175 sd
->effect_params1
[i
] / (1 << 16),
176 (abs(sd
->effect_params1
[i
]) & 0xFFFF) * 1000 / (1 << 16));
177 av_log(avctx
, AV_LOG_DEBUG
, "\n");
180 sd
->effect_pcount2
= get_bits(gb
, 16);
181 if (sd
->effect_pcount2
> 10) {
182 av_log(avctx
, AV_LOG_ERROR
, "Too many effect parameters\n");
183 return AVERROR_INVALIDDATA
;
184 } else if (sd
->effect_pcount2
) {
186 av_log(avctx
, AV_LOG_DEBUG
, "Effect params 2: ");
187 while (++i
< sd
->effect_pcount2
) {
188 sd
->effect_params2
[i
] = get_fp_val(gb
);
189 av_log(avctx
, AV_LOG_DEBUG
, " %d.%.2d",
190 sd
->effect_params2
[i
] / (1 << 16),
191 (abs(sd
->effect_params2
[i
]) & 0xFFFF) * 1000 / (1 << 16));
193 av_log(avctx
, AV_LOG_DEBUG
, "\n");
196 if (sd
->effect_flag
= get_bits1(gb
))
197 av_log(avctx
, AV_LOG_DEBUG
, "Effect flag set\n");
199 if (get_bits_count(gb
) >= gb
->size_in_bits
+
200 (avctx
->codec_id
== AV_CODEC_ID_WMV3IMAGE
? 64 : 0)) {
201 av_log(avctx
, AV_LOG_ERROR
, "Buffer overrun\n");
202 return AVERROR_INVALIDDATA
;
204 if (get_bits_count(gb
) < gb
->size_in_bits
- 8)
205 av_log(avctx
, AV_LOG_WARNING
, "Buffer not fully read\n");
210 static void vc1_draw_sprites(VC1Context
*v
, SpriteData
* sd
)
212 int i
, plane
, row
, sprite
;
213 int sr_cache
[2][2] = { { -1, -1 }, { -1, -1 } };
214 const uint8_t *src_h
[2][2];
215 int xoff
[2], xadv
[2], yoff
[2], yadv
[2], alpha
;
217 MpegEncContext
*s
= &v
->s
;
219 for (i
= 0; i
<= v
->two_sprites
; i
++) {
220 xoff
[i
] = av_clip(sd
->coefs
[i
][2], 0, v
->sprite_width
-1 << 16);
221 xadv
[i
] = sd
->coefs
[i
][0];
222 if (xadv
[i
] != 1<<16 || (v
->sprite_width
<< 16) - (v
->output_width
<< 16) - xoff
[i
])
223 xadv
[i
] = av_clip(xadv
[i
], 0, ((v
->sprite_width
<<16) - xoff
[i
] - 1) / v
->output_width
);
225 yoff
[i
] = av_clip(sd
->coefs
[i
][5], 0, v
->sprite_height
-1 << 16);
226 yadv
[i
] = av_clip(sd
->coefs
[i
][4], 0, ((v
->sprite_height
<< 16) - yoff
[i
]) / v
->output_height
);
228 alpha
= av_clip_uint16(sd
->coefs
[1][6]);
230 for (plane
= 0; plane
< (CONFIG_GRAY
&& s
->avctx
->flags
& AV_CODEC_FLAG_GRAY
? 1 : 3); plane
++) {
231 int width
= v
->output_width
>>!!plane
;
233 for (row
= 0; row
< v
->output_height
>>!!plane
; row
++) {
234 uint8_t *dst
= v
->sprite_output_frame
->data
[plane
] +
235 v
->sprite_output_frame
->linesize
[plane
] * row
;
237 for (sprite
= 0; sprite
<= v
->two_sprites
; sprite
++) {
238 const uint8_t *iplane
= s
->cur_pic
.data
[plane
];
239 int iline
= s
->cur_pic
.linesize
[plane
];
240 int ycoord
= yoff
[sprite
] + yadv
[sprite
] * row
;
241 int yline
= ycoord
>> 16;
243 ysub
[sprite
] = ycoord
& 0xFFFF;
245 iplane
= s
->last_pic
.data
[plane
];
246 iline
= s
->last_pic
.linesize
[plane
];
248 next_line
= FFMIN(yline
+ 1, (v
->sprite_height
>> !!plane
) - 1) * iline
;
249 if (!(xoff
[sprite
] & 0xFFFF) && xadv
[sprite
] == 1 << 16) {
250 src_h
[sprite
][0] = iplane
+ (xoff
[sprite
] >> 16) + yline
* iline
;
252 src_h
[sprite
][1] = iplane
+ (xoff
[sprite
] >> 16) + next_line
;
254 if (sr_cache
[sprite
][0] != yline
) {
255 if (sr_cache
[sprite
][1] == yline
) {
256 FFSWAP(uint8_t*, v
->sr_rows
[sprite
][0], v
->sr_rows
[sprite
][1]);
257 FFSWAP(int, sr_cache
[sprite
][0], sr_cache
[sprite
][1]);
259 v
->vc1dsp
.sprite_h(v
->sr_rows
[sprite
][0], iplane
+ yline
* iline
, xoff
[sprite
], xadv
[sprite
], width
);
260 sr_cache
[sprite
][0] = yline
;
263 if (ysub
[sprite
] && sr_cache
[sprite
][1] != yline
+ 1) {
264 v
->vc1dsp
.sprite_h(v
->sr_rows
[sprite
][1],
265 iplane
+ next_line
, xoff
[sprite
],
266 xadv
[sprite
], width
);
267 sr_cache
[sprite
][1] = yline
+ 1;
269 src_h
[sprite
][0] = v
->sr_rows
[sprite
][0];
270 src_h
[sprite
][1] = v
->sr_rows
[sprite
][1];
274 if (!v
->two_sprites
) {
276 v
->vc1dsp
.sprite_v_single(dst
, src_h
[0][0], src_h
[0][1], ysub
[0], width
);
278 memcpy(dst
, src_h
[0][0], width
);
281 if (ysub
[0] && ysub
[1]) {
282 v
->vc1dsp
.sprite_v_double_twoscale(dst
, src_h
[0][0], src_h
[0][1], ysub
[0],
283 src_h
[1][0], src_h
[1][1], ysub
[1], alpha
, width
);
284 } else if (ysub
[0]) {
285 v
->vc1dsp
.sprite_v_double_onescale(dst
, src_h
[0][0], src_h
[0][1], ysub
[0],
286 src_h
[1][0], alpha
, width
);
287 } else if (ysub
[1]) {
288 v
->vc1dsp
.sprite_v_double_onescale(dst
, src_h
[1][0], src_h
[1][1], ysub
[1],
289 src_h
[0][0], (1<<16)-1-alpha
, width
);
291 v
->vc1dsp
.sprite_v_double_noscale(dst
, src_h
[0][0], src_h
[1][0], alpha
, width
);
297 for (i
= 0; i
<= v
->two_sprites
; i
++) {
307 static int vc1_decode_sprites(VC1Context
*v
, GetBitContext
* gb
)
310 MpegEncContext
*s
= &v
->s
;
311 AVCodecContext
*avctx
= s
->avctx
;
314 memset(&sd
, 0, sizeof(sd
));
316 ret
= vc1_parse_sprites(v
, gb
, &sd
);
320 if (!s
->cur_pic
.data
[0]) {
321 av_log(avctx
, AV_LOG_ERROR
, "Got no sprites\n");
322 return AVERROR_UNKNOWN
;
325 if (v
->two_sprites
&& (!s
->last_pic
.ptr
|| !s
->last_pic
.data
[0])) {
326 av_log(avctx
, AV_LOG_WARNING
, "Need two sprites, only got one\n");
330 av_frame_unref(v
->sprite_output_frame
);
331 if ((ret
= ff_get_buffer(avctx
, v
->sprite_output_frame
, 0)) < 0)
334 vc1_draw_sprites(v
, &sd
);
339 static void vc1_sprite_flush(AVCodecContext
*avctx
)
341 VC1Context
*v
= avctx
->priv_data
;
342 MpegEncContext
*s
= &v
->s
;
343 MPVWorkPicture
*f
= &s
->cur_pic
;
346 /* Windows Media Image codecs have a convergence interval of two keyframes.
347 Since we can't enforce it, clear to black the missing sprite. This is
348 wrong but it looks better than doing nothing. */
351 for (plane
= 0; plane
< (CONFIG_GRAY
&& s
->avctx
->flags
& AV_CODEC_FLAG_GRAY
? 1 : 3); plane
++)
352 for (i
= 0; i
< v
->sprite_height
>>!!plane
; i
++)
353 memset(f
->data
[plane
] + i
* f
->linesize
[plane
],
354 plane
? 128 : 0, f
->linesize
[plane
]);
359 static av_cold
int vc1_decode_init_alloc_tables(VC1Context
*v
)
361 MpegEncContext
*s
= &v
->s
;
363 int mb_height
= FFALIGN(s
->mb_height
, 2);
365 /* Allocate mb bitplanes */
366 v
->mv_type_mb_plane
= av_malloc (s
->mb_stride
* mb_height
);
367 v
->direct_mb_plane
= av_malloc (s
->mb_stride
* mb_height
);
368 v
->forward_mb_plane
= av_malloc (s
->mb_stride
* mb_height
);
369 v
->fieldtx_plane
= av_mallocz(s
->mb_stride
* mb_height
);
370 v
->acpred_plane
= av_malloc (s
->mb_stride
* mb_height
);
371 v
->over_flags_plane
= av_malloc (s
->mb_stride
* mb_height
);
372 if (!v
->mv_type_mb_plane
|| !v
->direct_mb_plane
|| !v
->forward_mb_plane
||
373 !v
->fieldtx_plane
|| !v
->acpred_plane
|| !v
->over_flags_plane
)
374 return AVERROR(ENOMEM
);
376 v
->n_allocated_blks
= s
->mb_width
+ 2;
377 v
->block
= av_malloc(sizeof(*v
->block
) * v
->n_allocated_blks
);
378 v
->cbp_base
= av_malloc(sizeof(v
->cbp_base
[0]) * 3 * s
->mb_stride
);
379 if (!v
->block
|| !v
->cbp_base
)
380 return AVERROR(ENOMEM
);
381 v
->cbp
= v
->cbp_base
+ 2 * s
->mb_stride
;
382 v
->ttblk_base
= av_mallocz(sizeof(v
->ttblk_base
[0]) * 3 * s
->mb_stride
);
384 return AVERROR(ENOMEM
);
385 v
->ttblk
= v
->ttblk_base
+ 2 * s
->mb_stride
;
386 v
->is_intra_base
= av_mallocz(sizeof(v
->is_intra_base
[0]) * 3 * s
->mb_stride
);
387 if (!v
->is_intra_base
)
388 return AVERROR(ENOMEM
);
389 v
->is_intra
= v
->is_intra_base
+ 2 * s
->mb_stride
;
390 v
->luma_mv_base
= av_mallocz(sizeof(v
->luma_mv_base
[0]) * 3 * s
->mb_stride
);
391 if (!v
->luma_mv_base
)
392 return AVERROR(ENOMEM
);
393 v
->luma_mv
= v
->luma_mv_base
+ 2 * s
->mb_stride
;
395 /* allocate block type info in that way so it could be used with s->block_index[] */
396 v
->mb_type_base
= av_mallocz(s
->b8_stride
* (mb_height
* 2 + 1) + s
->mb_stride
* (mb_height
+ 1) * 2);
397 if (!v
->mb_type_base
)
398 return AVERROR(ENOMEM
);
399 v
->mb_type
[0] = v
->mb_type_base
+ s
->b8_stride
+ 1;
400 v
->mb_type
[1] = v
->mb_type_base
+ s
->b8_stride
* (mb_height
* 2 + 1) + s
->mb_stride
+ 1;
401 v
->mb_type
[2] = v
->mb_type
[1] + s
->mb_stride
* (mb_height
+ 1);
403 /* allocate memory to store block level MV info */
404 v
->blk_mv_type_base
= av_mallocz( s
->b8_stride
* (mb_height
* 2 + 1) + s
->mb_stride
* (mb_height
+ 1) * 2);
405 if (!v
->blk_mv_type_base
)
406 return AVERROR(ENOMEM
);
407 v
->blk_mv_type
= v
->blk_mv_type_base
+ s
->b8_stride
+ 1;
408 v
->mv_f_base
= av_mallocz(2 * (s
->b8_stride
* (mb_height
* 2 + 1) + s
->mb_stride
* (mb_height
+ 1) * 2));
410 return AVERROR(ENOMEM
);
411 v
->mv_f
[0] = v
->mv_f_base
+ s
->b8_stride
+ 1;
412 v
->mv_f
[1] = v
->mv_f
[0] + (s
->b8_stride
* (mb_height
* 2 + 1) + s
->mb_stride
* (mb_height
+ 1) * 2);
413 v
->mv_f_next_base
= av_mallocz(2 * (s
->b8_stride
* (mb_height
* 2 + 1) + s
->mb_stride
* (mb_height
+ 1) * 2));
414 if (!v
->mv_f_next_base
)
415 return AVERROR(ENOMEM
);
416 v
->mv_f_next
[0] = v
->mv_f_next_base
+ s
->b8_stride
+ 1;
417 v
->mv_f_next
[1] = v
->mv_f_next
[0] + (s
->b8_stride
* (mb_height
* 2 + 1) + s
->mb_stride
* (mb_height
+ 1) * 2);
419 if (s
->avctx
->codec_id
== AV_CODEC_ID_WMV3IMAGE
|| s
->avctx
->codec_id
== AV_CODEC_ID_VC1IMAGE
) {
420 for (i
= 0; i
< 4; i
++)
421 if (!(v
->sr_rows
[i
>> 1][i
& 1] = av_malloc(v
->output_width
)))
422 return AVERROR(ENOMEM
);
425 ret
= ff_intrax8_common_init(s
->avctx
, &v
->x8
,
426 s
->block
, s
->block_last_index
,
427 s
->mb_width
, s
->mb_height
);
434 static enum AVPixelFormat
vc1_get_format(AVCodecContext
*avctx
)
436 if (avctx
->codec_id
== AV_CODEC_ID_MSS2
)
437 return AV_PIX_FMT_YUV420P
;
439 if (CONFIG_GRAY
&& (avctx
->flags
& AV_CODEC_FLAG_GRAY
)) {
440 if (avctx
->color_range
== AVCOL_RANGE_UNSPECIFIED
)
441 avctx
->color_range
= AVCOL_RANGE_MPEG
;
442 return AV_PIX_FMT_GRAY8
;
445 if (avctx
->codec_id
== AV_CODEC_ID_VC1IMAGE
||
446 avctx
->codec_id
== AV_CODEC_ID_WMV3IMAGE
)
447 return AV_PIX_FMT_YUV420P
;
449 return ff_get_format(avctx
, vc1_hwaccel_pixfmt_list_420
);
452 static void vc1_decode_reset(AVCodecContext
*avctx
);
454 av_cold
int ff_vc1_decode_init(AVCodecContext
*avctx
)
456 VC1Context
*const v
= avctx
->priv_data
;
457 MpegEncContext
*const s
= &v
->s
;
460 ret
= av_image_check_size(avctx
->width
, avctx
->height
, 0, avctx
);
464 ret
= ff_mpv_decode_init(s
, avctx
);
468 avctx
->pix_fmt
= vc1_get_format(avctx
);
470 ret
= ff_mpv_common_init(s
);
474 s
->y_dc_scale_table
= ff_wmv3_dc_scale_table
;
475 s
->c_dc_scale_table
= ff_wmv3_dc_scale_table
;
477 ff_init_scantable(s
->idsp
.idct_permutation
, &s
->inter_scantable
,
478 ff_wmv1_scantable
[0]);
479 ff_init_scantable(s
->idsp
.idct_permutation
, &s
->intra_scantable
,
480 ff_wmv1_scantable
[1]);
482 ret
= vc1_decode_init_alloc_tables(v
);
484 vc1_decode_reset(avctx
);
490 av_cold
void ff_vc1_init_transposed_scantables(VC1Context
*v
)
493 for (i
= 0; i
< 64; i
++) {
494 #define transpose(x) (((x) >> 3) | (((x) & 7) << 3))
495 v
->zz_8x8
[0][i
] = transpose(ff_wmv1_scantable
[0][i
]);
496 v
->zz_8x8
[1][i
] = transpose(ff_wmv1_scantable
[1][i
]);
497 v
->zz_8x8
[2][i
] = transpose(ff_wmv1_scantable
[2][i
]);
498 v
->zz_8x8
[3][i
] = transpose(ff_wmv1_scantable
[3][i
]);
499 v
->zzi_8x8
[i
] = transpose(ff_vc1_adv_interlaced_8x8_zz
[i
]);
505 static av_cold
void vc1_init_static(void)
507 static VLCElem vlc_table
[32372];
508 VLCInitState state
= VLC_INIT_STATE(vlc_table
);
510 VLC_INIT_STATIC_TABLE(ff_vc1_norm2_vlc
, VC1_NORM2_VLC_BITS
, 4,
511 vc1_norm2_bits
, 1, 1,
512 vc1_norm2_codes
, 1, 1, 0);
513 VLC_INIT_STATIC_TABLE(ff_vc1_norm6_vlc
, VC1_NORM6_VLC_BITS
, 64,
514 vc1_norm6_bits
, 1, 1,
515 vc1_norm6_codes
, 2, 2, 0);
516 VLC_INIT_STATIC_TABLE(ff_vc1_imode_vlc
, VC1_IMODE_VLC_BITS
, 7,
517 vc1_imode_bits
, 1, 1,
518 vc1_imode_codes
, 1, 1, 0);
519 for (int i
= 0; i
< 3; i
++) {
521 ff_vlc_init_tables(&state
, VC1_TTMB_VLC_BITS
, 16,
522 vc1_ttmb_bits
[i
], 1, 1,
523 vc1_ttmb_codes
[i
], 2, 2, 0);
524 ff_vc1_ttblk_vlc
[i
] =
525 ff_vlc_init_tables(&state
, VC1_TTBLK_VLC_BITS
, 8,
526 vc1_ttblk_bits
[i
], 1, 1,
527 vc1_ttblk_codes
[i
], 1, 1, 0);
528 ff_vc1_subblkpat_vlc
[i
] =
529 ff_vlc_init_tables(&state
, VC1_SUBBLKPAT_VLC_BITS
, 15,
530 vc1_subblkpat_bits
[i
], 1, 1,
531 vc1_subblkpat_codes
[i
], 1, 1, 0);
533 for (int i
= 0; i
< 4; i
++) {
534 ff_vc1_4mv_block_pattern_vlc
[i
] =
535 ff_vlc_init_tables(&state
, VC1_4MV_BLOCK_PATTERN_VLC_BITS
, 16,
536 vc1_4mv_block_pattern_bits
[i
], 1, 1,
537 vc1_4mv_block_pattern_codes
[i
], 1, 1, 0);
538 ff_vc1_cbpcy_p_vlc
[i
] =
539 ff_vlc_init_tables(&state
, VC1_CBPCY_P_VLC_BITS
, 64,
540 vc1_cbpcy_p_bits
[i
], 1, 1,
541 vc1_cbpcy_p_codes
[i
], 2, 2, 0);
542 ff_vc1_mv_diff_vlc
[i
] =
543 ff_vlc_init_tables(&state
, VC1_MV_DIFF_VLC_BITS
, 73,
544 vc1_mv_diff_bits
[i
], 1, 1,
545 vc1_mv_diff_codes
[i
], 2, 2, 0);
546 /* initialize 4MV MBMODE VLC tables for interlaced frame P picture */
547 ff_vc1_intfr_4mv_mbmode_vlc
[i
] =
548 ff_vlc_init_tables(&state
, VC1_INTFR_4MV_MBMODE_VLC_BITS
, 15,
549 vc1_intfr_4mv_mbmode_bits
[i
], 1, 1,
550 vc1_intfr_4mv_mbmode_codes
[i
], 2, 2, 0);
551 /* initialize NON-4MV MBMODE VLC tables for the same */
552 ff_vc1_intfr_non4mv_mbmode_vlc
[i
] =
553 ff_vlc_init_tables(&state
, VC1_INTFR_NON4MV_MBMODE_VLC_BITS
, 9,
554 vc1_intfr_non4mv_mbmode_bits
[i
], 1, 1,
555 vc1_intfr_non4mv_mbmode_codes
[i
], 1, 1, 0);
556 /* initialize interlaced MVDATA tables (1-Ref) */
557 ff_vc1_1ref_mvdata_vlc
[i
] =
558 ff_vlc_init_tables(&state
, VC1_1REF_MVDATA_VLC_BITS
, 72,
559 vc1_1ref_mvdata_bits
[i
], 1, 1,
560 vc1_1ref_mvdata_codes
[i
], 4, 4, 0);
561 /* Initialize 2MV Block pattern VLC tables */
562 ff_vc1_2mv_block_pattern_vlc
[i
] =
563 ff_vlc_init_tables(&state
, VC1_2MV_BLOCK_PATTERN_VLC_BITS
, 4,
564 vc1_2mv_block_pattern_bits
[i
], 1, 1,
565 vc1_2mv_block_pattern_codes
[i
], 1, 1, 0);
567 for (int i
= 0; i
< 8; i
++) {
568 ff_vc1_ac_coeff_table
[i
] =
569 ff_vlc_init_tables(&state
, AC_VLC_BITS
, ff_vc1_ac_sizes
[i
],
570 &vc1_ac_tables
[i
][0][1], 8, 4,
571 &vc1_ac_tables
[i
][0][0], 8, 4, 0);
572 /* initialize interlaced MVDATA tables (2-Ref) */
573 ff_vc1_2ref_mvdata_vlc
[i
] =
574 ff_vlc_init_tables(&state
, VC1_2REF_MVDATA_VLC_BITS
, 126,
575 vc1_2ref_mvdata_bits
[i
], 1, 1,
576 vc1_2ref_mvdata_codes
[i
], 4, 4, 0);
577 /* Initialize interlaced CBPCY VLC tables (Table 124 - Table 131) */
578 ff_vc1_icbpcy_vlc
[i
] =
579 ff_vlc_init_tables(&state
, VC1_ICBPCY_VLC_BITS
, 63,
580 vc1_icbpcy_p_bits
[i
], 1, 1,
581 vc1_icbpcy_p_codes
[i
], 2, 2, 0);
582 /* Initialize interlaced field picture MBMODE VLC tables */
583 ff_vc1_if_mmv_mbmode_vlc
[i
] =
584 ff_vlc_init_tables(&state
, VC1_IF_MMV_MBMODE_VLC_BITS
, 8,
585 vc1_if_mmv_mbmode_bits
[i
], 1, 1,
586 vc1_if_mmv_mbmode_codes
[i
], 1, 1, 0);
587 ff_vc1_if_1mv_mbmode_vlc
[i
] =
588 ff_vlc_init_tables(&state
, VC1_IF_1MV_MBMODE_VLC_BITS
, 6,
589 vc1_if_1mv_mbmode_bits
[i
], 1, 1,
590 vc1_if_1mv_mbmode_codes
[i
], 1, 1, 0);
592 ff_msmp4_vc1_vlcs_init_once();
596 * Init VC-1 specific tables and VC1Context members
597 * @param v The VC1Context to initialize
600 av_cold
void ff_vc1_init_common(VC1Context
*v
)
602 static AVOnce init_static_once
= AV_ONCE_INIT
;
603 MpegEncContext
*const s
= &v
->s
;
607 v
->mvrange
= 0; /* 7.1.1.18, p80 */
609 s
->avctx
->chroma_sample_location
= AVCHROMA_LOC_LEFT
;
610 s
->out_format
= FMT_H263
;
613 s
->msmpeg4_version
= MSMP4_VC1
;
615 ff_vc1dsp_init(&v
->vc1dsp
);
617 /* For error resilience */
618 ff_qpeldsp_init(&s
->qdsp
);
621 ff_thread_once(&init_static_once
, vc1_init_static
);
624 /** Initialize a VC1/WMV3 decoder
625 * @todo TODO: Handle VC-1 IDUs (Transport level?)
626 * @todo TODO: Decipher remaining bits in extra_data
628 static av_cold
int vc1_decode_init(AVCodecContext
*avctx
)
630 VC1Context
*v
= avctx
->priv_data
;
631 MpegEncContext
*s
= &v
->s
;
635 /* save the container output size for WMImage */
636 v
->output_width
= avctx
->width
;
637 v
->output_height
= avctx
->height
;
639 if (!avctx
->extradata_size
|| !avctx
->extradata
)
640 return AVERROR_INVALIDDATA
;
643 ff_vc1_init_common(v
);
645 if (avctx
->codec_id
== AV_CODEC_ID_WMV3
|| avctx
->codec_id
== AV_CODEC_ID_WMV3IMAGE
) {
648 // looks like WMV3 has a sequence header stored in the extradata
649 // advanced sequence header may be before the first frame
650 // the last byte of the extradata is a version number, 1 for the
651 // samples we can decode
653 ret
= init_get_bits8(&gb
, avctx
->extradata
, avctx
->extradata_size
);
657 if ((ret
= ff_vc1_decode_sequence_header(avctx
, v
, &gb
)) < 0)
660 if (avctx
->codec_id
== AV_CODEC_ID_WMV3IMAGE
&& !v
->res_sprite
) {
661 avpriv_request_sample(avctx
, "Non sprite WMV3IMAGE");
662 return AVERROR_PATCHWELCOME
;
665 count
= avctx
->extradata_size
*8 - get_bits_count(&gb
);
667 av_log(avctx
, AV_LOG_INFO
, "Extra data: %i bits left, value: %X\n",
668 count
, get_bits_long(&gb
, FFMIN(count
, 32)));
669 } else if (count
< 0) {
670 av_log(avctx
, AV_LOG_INFO
, "Read %i bits in overflow\n", -count
);
672 } else { // VC1/WVC1/WVP2
673 const uint8_t *start
= avctx
->extradata
;
674 const uint8_t *end
= avctx
->extradata
+ avctx
->extradata_size
;
677 uint8_t *buf2
= NULL
;
678 int seq_initialized
= 0, ep_initialized
= 0;
680 if (avctx
->extradata_size
< 16) {
681 av_log(avctx
, AV_LOG_ERROR
, "Extradata size too small: %i\n", avctx
->extradata_size
);
682 return AVERROR_INVALIDDATA
;
685 buf2
= av_mallocz(avctx
->extradata_size
+ AV_INPUT_BUFFER_PADDING_SIZE
);
687 return AVERROR(ENOMEM
);
689 start
= find_next_marker(start
, end
); // in WVC1 extradata first byte is its size, but can be 0 in mkv
691 for (; next
< end
; start
= next
) {
692 next
= find_next_marker(start
+ 4, end
);
693 size
= next
- start
- 4;
696 buf2_size
= v
->vc1dsp
.vc1_unescape_buffer(start
+ 4, size
, buf2
);
697 init_get_bits(&gb
, buf2
, buf2_size
* 8);
698 switch (AV_RB32(start
)) {
699 case VC1_CODE_SEQHDR
:
700 if ((ret
= ff_vc1_decode_sequence_header(avctx
, v
, &gb
)) < 0) {
706 case VC1_CODE_ENTRYPOINT
:
707 if ((ret
= ff_vc1_decode_entry_point(avctx
, v
, &gb
)) < 0) {
716 if (!seq_initialized
|| !ep_initialized
) {
717 av_log(avctx
, AV_LOG_ERROR
, "Incomplete extradata\n");
718 return AVERROR_INVALIDDATA
;
720 v
->res_sprite
= (avctx
->codec_id
== AV_CODEC_ID_VC1IMAGE
);
723 avctx
->profile
= v
->profile
;
724 if (v
->profile
== PROFILE_ADVANCED
)
725 avctx
->level
= v
->level
;
727 ff_blockdsp_init(&s
->bdsp
);
728 ff_h264chroma_init(&v
->h264chroma
, 8);
730 avctx
->has_b_frames
= !!avctx
->max_b_frames
;
732 if (v
->color_prim
== 1 || v
->color_prim
== 5 || v
->color_prim
== 6)
733 avctx
->color_primaries
= v
->color_prim
;
734 if (v
->transfer_char
== 1 || v
->transfer_char
== 7)
735 avctx
->color_trc
= v
->transfer_char
;
736 if (v
->matrix_coef
== 1 || v
->matrix_coef
== 6 || v
->matrix_coef
== 7)
737 avctx
->colorspace
= v
->matrix_coef
;
739 s
->mb_width
= (avctx
->coded_width
+ 15) >> 4;
740 s
->mb_height
= (avctx
->coded_height
+ 15) >> 4;
742 if (v
->profile
== PROFILE_ADVANCED
|| v
->res_fasttx
) {
743 ff_vc1_init_transposed_scantables(v
);
745 memcpy(v
->zz_8x8
, ff_wmv1_scantable
, 4*64);
748 v
->vc1dsp
.vc1_inv_trans_8x8
= ff_simple_idct_int16_8bit
;
749 v
->vc1dsp
.vc1_inv_trans_8x4
= ff_simple_idct84_add
;
750 v
->vc1dsp
.vc1_inv_trans_4x8
= ff_simple_idct48_add
;
751 v
->vc1dsp
.vc1_inv_trans_4x4
= ff_simple_idct44_add
;
752 v
->vc1dsp
.vc1_inv_trans_8x8_dc
= ff_simple_idct_add_int16_8bit
;
753 v
->vc1dsp
.vc1_inv_trans_8x4_dc
= ff_simple_idct84_add
;
754 v
->vc1dsp
.vc1_inv_trans_4x8_dc
= ff_simple_idct48_add
;
755 v
->vc1dsp
.vc1_inv_trans_4x4_dc
= ff_simple_idct44_add
;
758 if (avctx
->codec_id
== AV_CODEC_ID_WMV3IMAGE
|| avctx
->codec_id
== AV_CODEC_ID_VC1IMAGE
) {
759 v
->sprite_width
= avctx
->coded_width
;
760 v
->sprite_height
= avctx
->coded_height
;
762 avctx
->coded_width
= avctx
->width
= v
->output_width
;
763 avctx
->coded_height
= avctx
->height
= v
->output_height
;
765 // prevent 16.16 overflows
766 if (v
->sprite_width
> 1 << 14 ||
767 v
->sprite_height
> 1 << 14 ||
768 v
->output_width
> 1 << 14 ||
769 v
->output_height
> 1 << 14) {
770 return AVERROR_INVALIDDATA
;
773 if ((v
->sprite_width
&1) || (v
->sprite_height
&1)) {
774 avpriv_request_sample(avctx
, "odd sprites support");
775 return AVERROR_PATCHWELCOME
;
781 static av_cold
void vc1_decode_reset(AVCodecContext
*avctx
)
783 VC1Context
*v
= avctx
->priv_data
;
786 av_frame_free(&v
->sprite_output_frame
);
788 for (i
= 0; i
< 4; i
++)
789 av_freep(&v
->sr_rows
[i
>> 1][i
& 1]);
790 ff_mpv_common_end(&v
->s
);
791 memset(v
->s
.block_index
, 0, sizeof(v
->s
.block_index
));
792 av_freep(&v
->mv_type_mb_plane
);
793 av_freep(&v
->direct_mb_plane
);
794 av_freep(&v
->forward_mb_plane
);
795 av_freep(&v
->fieldtx_plane
);
796 av_freep(&v
->acpred_plane
);
797 av_freep(&v
->over_flags_plane
);
798 av_freep(&v
->mb_type_base
);
799 av_freep(&v
->blk_mv_type_base
);
800 av_freep(&v
->mv_f_base
);
801 av_freep(&v
->mv_f_next_base
);
803 av_freep(&v
->cbp_base
);
804 av_freep(&v
->ttblk_base
);
805 av_freep(&v
->is_intra_base
); // FIXME use v->mb_type[]
806 av_freep(&v
->luma_mv_base
);
807 ff_intrax8_common_end(&v
->x8
);
811 * Close a MSS2/VC1/WMV3 decoder
813 av_cold
int ff_vc1_decode_end(AVCodecContext
*avctx
)
815 vc1_decode_reset(avctx
);
816 return ff_mpv_decode_close(avctx
);
819 /** Decode a VC1/WMV3 frame
820 * @todo TODO: Handle VC-1 IDUs (Transport level?)
822 static int vc1_decode_frame(AVCodecContext
*avctx
, AVFrame
*pict
,
823 int *got_frame
, AVPacket
*avpkt
)
825 const uint8_t *buf
= avpkt
->data
;
826 int buf_size
= avpkt
->size
, n_slices
= 0, i
, ret
;
827 VC1Context
*v
= avctx
->priv_data
;
828 MpegEncContext
*s
= &v
->s
;
829 uint8_t *buf2
= NULL
;
830 const uint8_t *buf_start
= buf
, *buf_start_second_field
= NULL
;
831 int mb_height
, n_slices1
=-1;
836 const uint8_t *rawbuf
;
838 } *slices
= NULL
, *tmp
;
839 unsigned slices_allocated
= 0;
843 if(s
->avctx
->flags
& AV_CODEC_FLAG_LOW_DELAY
)
846 /* no supplementary picture */
847 if (buf_size
== 0 || (buf_size
== 4 && AV_RB32(buf
) == VC1_CODE_ENDOFSEQ
)) {
848 /* special case for last picture */
849 if (s
->low_delay
== 0 && s
->next_pic
.ptr
) {
850 if ((ret
= av_frame_ref(pict
, s
->next_pic
.ptr
->f
)) < 0)
852 ff_mpv_unref_picture(&s
->next_pic
);
860 //for advanced profile we may need to parse and unescape data
861 if (avctx
->codec_id
== AV_CODEC_ID_VC1
|| avctx
->codec_id
== AV_CODEC_ID_VC1IMAGE
) {
863 size_t next_allocated
= 0;
864 buf2
= av_mallocz(buf_size
+ AV_INPUT_BUFFER_PADDING_SIZE
);
866 return AVERROR(ENOMEM
);
868 if (IS_MARKER(AV_RB32(buf
))) { /* frame starts with marker and needs to be parsed */
869 const uint8_t *start
, *end
, *next
;
873 for (start
= buf
, end
= buf
+ buf_size
; next
< end
; start
= next
) {
874 next
= find_next_marker(start
+ 4, end
);
875 size
= next
- start
- 4;
876 if (size
<= 0) continue;
877 switch (AV_RB32(start
)) {
880 buf_size2
= v
->vc1dsp
.vc1_unescape_buffer(start
+ 4, size
, buf2
);
882 case VC1_CODE_FIELD
: {
884 buf_start_second_field
= start
;
885 av_size_mult(sizeof(*slices
), n_slices
+1, &next_allocated
);
886 tmp
= next_allocated
? av_fast_realloc(slices
, &slices_allocated
, next_allocated
) : NULL
;
888 ret
= AVERROR(ENOMEM
);
892 slices
[n_slices
].buf
= av_mallocz(size
+ AV_INPUT_BUFFER_PADDING_SIZE
);
893 if (!slices
[n_slices
].buf
) {
894 ret
= AVERROR(ENOMEM
);
897 buf_size3
= v
->vc1dsp
.vc1_unescape_buffer(start
+ 4, size
,
898 slices
[n_slices
].buf
);
899 init_get_bits(&slices
[n_slices
].gb
, slices
[n_slices
].buf
,
901 slices
[n_slices
].mby_start
= avctx
->coded_height
+ 31 >> 5;
902 slices
[n_slices
].rawbuf
= start
;
903 slices
[n_slices
].raw_size
= size
+ 4;
904 n_slices1
= n_slices
- 1; // index of the last slice of the first field
908 case VC1_CODE_ENTRYPOINT
: /* it should be before frame data */
909 buf_size2
= v
->vc1dsp
.vc1_unescape_buffer(start
+ 4, size
, buf2
);
910 init_get_bits(&s
->gb
, buf2
, buf_size2
* 8);
911 ff_vc1_decode_entry_point(avctx
, v
, &s
->gb
);
913 case VC1_CODE_SLICE
: {
915 av_size_mult(sizeof(*slices
), n_slices
+1, &next_allocated
);
916 tmp
= next_allocated
? av_fast_realloc(slices
, &slices_allocated
, next_allocated
) : NULL
;
918 ret
= AVERROR(ENOMEM
);
922 slices
[n_slices
].buf
= av_mallocz(size
+ AV_INPUT_BUFFER_PADDING_SIZE
);
923 if (!slices
[n_slices
].buf
) {
924 ret
= AVERROR(ENOMEM
);
927 buf_size3
= v
->vc1dsp
.vc1_unescape_buffer(start
+ 4, size
,
928 slices
[n_slices
].buf
);
929 init_get_bits(&slices
[n_slices
].gb
, slices
[n_slices
].buf
,
931 slices
[n_slices
].mby_start
= get_bits(&slices
[n_slices
].gb
, 9);
932 slices
[n_slices
].rawbuf
= start
;
933 slices
[n_slices
].raw_size
= size
+ 4;
939 } else if (v
->interlace
&& ((buf
[0] & 0xC0) == 0xC0)) { /* WVC1 interlaced stores both fields divided by marker */
940 const uint8_t *divider
;
943 divider
= find_next_marker(buf
, buf
+ buf_size
);
944 if ((divider
== (buf
+ buf_size
)) || AV_RB32(divider
) != VC1_CODE_FIELD
) {
945 av_log(avctx
, AV_LOG_ERROR
, "Error in WVC1 interlaced frame\n");
946 ret
= AVERROR_INVALIDDATA
;
948 } else { // found field marker, unescape second field
949 buf_start_second_field
= divider
;
950 av_size_mult(sizeof(*slices
), n_slices
+1, &next_allocated
);
951 tmp
= next_allocated
? av_fast_realloc(slices
, &slices_allocated
, next_allocated
) : NULL
;
953 ret
= AVERROR(ENOMEM
);
957 slices
[n_slices
].buf
= av_mallocz(buf_size
+ AV_INPUT_BUFFER_PADDING_SIZE
);
958 if (!slices
[n_slices
].buf
) {
959 ret
= AVERROR(ENOMEM
);
962 buf_size3
= v
->vc1dsp
.vc1_unescape_buffer(divider
+ 4, buf
+ buf_size
- divider
- 4, slices
[n_slices
].buf
);
963 init_get_bits(&slices
[n_slices
].gb
, slices
[n_slices
].buf
,
965 slices
[n_slices
].mby_start
= s
->mb_height
+ 1 >> 1;
966 slices
[n_slices
].rawbuf
= divider
;
967 slices
[n_slices
].raw_size
= buf
+ buf_size
- divider
;
968 n_slices1
= n_slices
- 1;
971 buf_size2
= v
->vc1dsp
.vc1_unescape_buffer(buf
, divider
- buf
, buf2
);
973 buf_size2
= v
->vc1dsp
.vc1_unescape_buffer(buf
, buf_size
, buf2
);
975 init_get_bits(&s
->gb
, buf2
, buf_size2
*8);
977 ret
= init_get_bits8(&s
->gb
, buf
, buf_size
);
983 v
->new_sprite
= !get_bits1(&s
->gb
);
984 v
->two_sprites
= get_bits1(&s
->gb
);
985 /* res_sprite means a Windows Media Image stream, AV_CODEC_ID_*IMAGE means
986 we're using the sprite compositor. These are intentionally kept separate
987 so you can get the raw sprites by using the wmv3 decoder for WMVP or
988 the vc1 one for WVP2 */
989 if (avctx
->codec_id
== AV_CODEC_ID_WMV3IMAGE
|| avctx
->codec_id
== AV_CODEC_ID_VC1IMAGE
) {
991 // switch AVCodecContext parameters to those of the sprites
992 avctx
->width
= avctx
->coded_width
= v
->sprite_width
;
993 avctx
->height
= avctx
->coded_height
= v
->sprite_height
;
1000 if (s
->context_initialized
&&
1001 (s
->width
!= avctx
->coded_width
||
1002 s
->height
!= avctx
->coded_height
)) {
1003 vc1_decode_reset(avctx
);
1006 if (!s
->context_initialized
) {
1007 ret
= ff_vc1_decode_init(avctx
);
1011 s
->low_delay
= !avctx
->has_b_frames
|| v
->res_sprite
;
1013 if (v
->profile
== PROFILE_ADVANCED
) {
1014 if(avctx
->coded_width
<=1 || avctx
->coded_height
<=1) {
1015 ret
= AVERROR_INVALIDDATA
;
1018 s
->h_edge_pos
= avctx
->coded_width
;
1019 s
->v_edge_pos
= avctx
->coded_height
;
1023 // do parse frame header
1024 v
->pic_header_flag
= 0;
1025 v
->first_pic_header_flag
= 1;
1026 if (v
->profile
< PROFILE_ADVANCED
) {
1027 if ((ret
= ff_vc1_parse_frame_header(v
, &s
->gb
)) < 0) {
1031 if ((ret
= ff_vc1_parse_frame_header_adv(v
, &s
->gb
)) < 0) {
1035 v
->first_pic_header_flag
= 0;
1037 if (avctx
->debug
& FF_DEBUG_PICT_INFO
)
1038 av_log(v
->s
.avctx
, AV_LOG_DEBUG
, "pict_type: %c\n", av_get_picture_type_char(s
->pict_type
));
1040 if ((avctx
->codec_id
== AV_CODEC_ID_WMV3IMAGE
|| avctx
->codec_id
== AV_CODEC_ID_VC1IMAGE
)
1041 && s
->pict_type
!= AV_PICTURE_TYPE_I
) {
1042 av_log(v
->s
.avctx
, AV_LOG_ERROR
, "Sprite decoder: expected I-frame\n");
1043 ret
= AVERROR_INVALIDDATA
;
1046 if ((avctx
->codec_id
== AV_CODEC_ID_WMV3IMAGE
|| avctx
->codec_id
== AV_CODEC_ID_VC1IMAGE
)
1048 av_log(v
->s
.avctx
, AV_LOG_ERROR
, "Sprite decoder: expected Frames not Fields\n");
1049 ret
= AVERROR_INVALIDDATA
;
1052 if ((s
->mb_height
>> v
->field_mode
) == 0) {
1053 av_log(v
->s
.avctx
, AV_LOG_ERROR
, "image too short\n");
1054 ret
= AVERROR_INVALIDDATA
;
1058 /* skip B-frames if we don't have reference frames */
1059 if (!s
->last_pic
.ptr
&& s
->pict_type
== AV_PICTURE_TYPE_B
) {
1060 av_log(v
->s
.avctx
, AV_LOG_DEBUG
, "Skipping B frame without reference frames\n");
1063 if ((avctx
->skip_frame
>= AVDISCARD_NONREF
&& s
->pict_type
== AV_PICTURE_TYPE_B
) ||
1064 (avctx
->skip_frame
>= AVDISCARD_NONKEY
&& s
->pict_type
!= AV_PICTURE_TYPE_I
) ||
1065 avctx
->skip_frame
>= AVDISCARD_ALL
) {
1069 if ((ret
= ff_mpv_frame_start(s
, avctx
)) < 0) {
1073 v
->s
.cur_pic
.ptr
->field_picture
= v
->field_mode
;
1074 v
->s
.cur_pic
.ptr
->f
->flags
|= AV_FRAME_FLAG_INTERLACED
* (v
->fcm
!= PROGRESSIVE
);
1075 v
->s
.cur_pic
.ptr
->f
->flags
|= AV_FRAME_FLAG_TOP_FIELD_FIRST
* !!v
->tff
;
1076 v
->last_interlaced
= v
->s
.last_pic
.ptr
? v
->s
.last_pic
.ptr
->f
->flags
& AV_FRAME_FLAG_INTERLACED
: 0;
1077 v
->next_interlaced
= v
->s
.next_pic
.ptr
? v
->s
.next_pic
.ptr
->f
->flags
& AV_FRAME_FLAG_INTERLACED
: 0;
1079 // process pulldown flags
1080 s
->cur_pic
.ptr
->f
->repeat_pict
= 0;
1081 // Pulldown flags are only valid when 'broadcast' has been set.
1084 s
->cur_pic
.ptr
->f
->repeat_pict
= 1;
1085 } else if (v
->rptfrm
) {
1087 s
->cur_pic
.ptr
->f
->repeat_pict
= v
->rptfrm
* 2;
1090 if (avctx
->hwaccel
) {
1091 const FFHWAccel
*hwaccel
= ffhwaccel(avctx
->hwaccel
);
1093 if (v
->field_mode
&& buf_start_second_field
) {
1094 // decode first field
1095 s
->picture_structure
= PICT_BOTTOM_FIELD
- v
->tff
;
1096 ret
= hwaccel
->start_frame(avctx
, buf_start
,
1097 buf_start_second_field
- buf_start
);
1101 if (n_slices1
== -1) {
1102 // no slices, decode the field as-is
1103 ret
= hwaccel
->decode_slice(avctx
, buf_start
,
1104 buf_start_second_field
- buf_start
);
1108 ret
= hwaccel
->decode_slice(avctx
, buf_start
,
1109 slices
[0].rawbuf
- buf_start
);
1113 for (i
= 0 ; i
< n_slices1
+ 1; i
++) {
1114 s
->gb
= slices
[i
].gb
;
1115 s
->mb_y
= slices
[i
].mby_start
;
1117 v
->pic_header_flag
= get_bits1(&s
->gb
);
1118 if (v
->pic_header_flag
) {
1119 if (ff_vc1_parse_frame_header_adv(v
, &s
->gb
) < 0) {
1120 av_log(v
->s
.avctx
, AV_LOG_ERROR
, "Slice header damaged\n");
1121 ret
= AVERROR_INVALIDDATA
;
1122 if (avctx
->err_recognition
& AV_EF_EXPLODE
)
1128 ret
= hwaccel
->decode_slice(avctx
, slices
[i
].rawbuf
,
1129 slices
[i
].raw_size
);
1135 if ((ret
= hwaccel
->end_frame(avctx
)) < 0)
1138 // decode second field
1139 s
->gb
= slices
[n_slices1
+ 1].gb
;
1140 s
->mb_y
= slices
[n_slices1
+ 1].mby_start
;
1141 s
->picture_structure
= PICT_TOP_FIELD
+ v
->tff
;
1142 v
->second_field
= 1;
1143 v
->pic_header_flag
= 0;
1144 if (ff_vc1_parse_frame_header_adv(v
, &s
->gb
) < 0) {
1145 av_log(avctx
, AV_LOG_ERROR
, "parsing header for second field failed");
1146 ret
= AVERROR_INVALIDDATA
;
1149 v
->s
.cur_pic
.ptr
->f
->pict_type
= v
->s
.pict_type
;
1151 ret
= hwaccel
->start_frame(avctx
, buf_start_second_field
,
1152 (buf
+ buf_size
) - buf_start_second_field
);
1156 if (n_slices
- n_slices1
== 2) {
1157 // no slices, decode the field as-is
1158 ret
= hwaccel
->decode_slice(avctx
, buf_start_second_field
,
1159 (buf
+ buf_size
) - buf_start_second_field
);
1163 ret
= hwaccel
->decode_slice(avctx
, buf_start_second_field
,
1164 slices
[n_slices1
+ 2].rawbuf
- buf_start_second_field
);
1168 for (i
= n_slices1
+ 2; i
< n_slices
; i
++) {
1169 s
->gb
= slices
[i
].gb
;
1170 s
->mb_y
= slices
[i
].mby_start
;
1172 v
->pic_header_flag
= get_bits1(&s
->gb
);
1173 if (v
->pic_header_flag
) {
1174 if (ff_vc1_parse_frame_header_adv(v
, &s
->gb
) < 0) {
1175 av_log(v
->s
.avctx
, AV_LOG_ERROR
, "Slice header damaged\n");
1176 ret
= AVERROR_INVALIDDATA
;
1177 if (avctx
->err_recognition
& AV_EF_EXPLODE
)
1183 ret
= hwaccel
->decode_slice(avctx
, slices
[i
].rawbuf
,
1184 slices
[i
].raw_size
);
1190 if ((ret
= hwaccel
->end_frame(avctx
)) < 0)
1193 s
->picture_structure
= PICT_FRAME
;
1194 ret
= hwaccel
->start_frame(avctx
, buf_start
,
1195 (buf
+ buf_size
) - buf_start
);
1199 if (n_slices
== 0) {
1200 // no slices, decode the frame as-is
1201 ret
= hwaccel
->decode_slice(avctx
, buf_start
,
1202 (buf
+ buf_size
) - buf_start
);
1206 // decode the frame part as the first slice
1207 ret
= hwaccel
->decode_slice(avctx
, buf_start
,
1208 slices
[0].rawbuf
- buf_start
);
1212 // and process the slices as additional slices afterwards
1213 for (i
= 0 ; i
< n_slices
; i
++) {
1214 s
->gb
= slices
[i
].gb
;
1215 s
->mb_y
= slices
[i
].mby_start
;
1217 v
->pic_header_flag
= get_bits1(&s
->gb
);
1218 if (v
->pic_header_flag
) {
1219 if (ff_vc1_parse_frame_header_adv(v
, &s
->gb
) < 0) {
1220 av_log(v
->s
.avctx
, AV_LOG_ERROR
, "Slice header damaged\n");
1221 ret
= AVERROR_INVALIDDATA
;
1222 if (avctx
->err_recognition
& AV_EF_EXPLODE
)
1228 ret
= hwaccel
->decode_slice(avctx
, slices
[i
].rawbuf
,
1229 slices
[i
].raw_size
);
1234 if ((ret
= hwaccel
->end_frame(avctx
)) < 0)
1240 ff_mpeg_er_frame_start(s
);
1242 v
->end_mb_x
= s
->mb_width
;
1243 if (v
->field_mode
) {
1244 s
->cur_pic
.linesize
[0] <<= 1;
1245 s
->cur_pic
.linesize
[1] <<= 1;
1246 s
->cur_pic
.linesize
[2] <<= 1;
1248 s
->uvlinesize
<<= 1;
1250 mb_height
= s
->mb_height
>> v
->field_mode
;
1252 av_assert0 (mb_height
> 0);
1254 for (i
= 0; i
<= n_slices
; i
++) {
1255 if (i
> 0 && slices
[i
- 1].mby_start
>= mb_height
) {
1256 if (v
->field_mode
<= 0) {
1257 av_log(v
->s
.avctx
, AV_LOG_ERROR
, "Slice %d starts beyond "
1258 "picture boundary (%d >= %d)\n", i
,
1259 slices
[i
- 1].mby_start
, mb_height
);
1262 v
->second_field
= 1;
1263 av_assert0((s
->mb_height
& 1) == 0);
1264 v
->blocks_off
= s
->b8_stride
* (s
->mb_height
&~1);
1265 v
->mb_off
= s
->mb_stride
* s
->mb_height
>> 1;
1267 v
->second_field
= 0;
1272 v
->pic_header_flag
= 0;
1273 if (v
->field_mode
&& i
== n_slices1
+ 2) {
1274 if ((header_ret
= ff_vc1_parse_frame_header_adv(v
, &s
->gb
)) < 0) {
1275 av_log(v
->s
.avctx
, AV_LOG_ERROR
, "Field header damaged\n");
1276 ret
= AVERROR_INVALIDDATA
;
1277 if (avctx
->err_recognition
& AV_EF_EXPLODE
)
1281 } else if (get_bits1(&s
->gb
)) {
1282 v
->pic_header_flag
= 1;
1283 if ((header_ret
= ff_vc1_parse_frame_header_adv(v
, &s
->gb
)) < 0) {
1284 av_log(v
->s
.avctx
, AV_LOG_ERROR
, "Slice header damaged\n");
1285 ret
= AVERROR_INVALIDDATA
;
1286 if (avctx
->err_recognition
& AV_EF_EXPLODE
)
1294 s
->start_mb_y
= (i
== 0) ? 0 : FFMAX(0, slices
[i
-1].mby_start
% mb_height
);
1295 if (!v
->field_mode
|| v
->second_field
)
1296 s
->end_mb_y
= (i
== n_slices
) ? mb_height
: FFMIN(mb_height
, slices
[i
].mby_start
% mb_height
);
1298 if (i
>= n_slices
) {
1299 av_log(v
->s
.avctx
, AV_LOG_ERROR
, "first field slice count too large\n");
1302 s
->end_mb_y
= (i
== n_slices1
+ 1) ? mb_height
: FFMIN(mb_height
, slices
[i
].mby_start
% mb_height
);
1304 if (s
->end_mb_y
<= s
->start_mb_y
) {
1305 av_log(v
->s
.avctx
, AV_LOG_ERROR
, "end mb y %d %d invalid\n", s
->end_mb_y
, s
->start_mb_y
);
1308 if (((s
->pict_type
== AV_PICTURE_TYPE_P
&& !v
->p_frame_skipped
) ||
1309 (s
->pict_type
== AV_PICTURE_TYPE_B
&& !v
->bi_type
)) &&
1311 av_log(v
->s
.avctx
, AV_LOG_ERROR
, "missing cbpcy_vlc\n");
1314 ff_vc1_decode_blocks(v
);
1315 if (i
!= n_slices
) {
1316 s
->gb
= slices
[i
].gb
;
1319 if (v
->field_mode
) {
1320 v
->second_field
= 0;
1321 s
->cur_pic
.linesize
[0] >>= 1;
1322 s
->cur_pic
.linesize
[1] >>= 1;
1323 s
->cur_pic
.linesize
[2] >>= 1;
1325 s
->uvlinesize
>>= 1;
1326 if (v
->s
.pict_type
!= AV_PICTURE_TYPE_BI
&& v
->s
.pict_type
!= AV_PICTURE_TYPE_B
) {
1327 FFSWAP(uint8_t *, v
->mv_f_next
[0], v
->mv_f
[0]);
1328 FFSWAP(uint8_t *, v
->mv_f_next
[1], v
->mv_f
[1]);
1331 ff_dlog(s
->avctx
, "Consumed %i/%i bits\n",
1332 get_bits_count(&s
->gb
), s
->gb
.size_in_bits
);
1333 // if (get_bits_count(&s->gb) > buf_size * 8)
1335 if(s
->er
.error_occurred
&& s
->pict_type
== AV_PICTURE_TYPE_B
) {
1336 ret
= AVERROR_INVALIDDATA
;
1340 && avctx
->codec_id
!= AV_CODEC_ID_WMV3IMAGE
1341 && avctx
->codec_id
!= AV_CODEC_ID_VC1IMAGE
)
1342 ff_er_frame_end(&s
->er
, NULL
);
1345 ff_mpv_frame_end(s
);
1347 if (avctx
->codec_id
== AV_CODEC_ID_WMV3IMAGE
|| avctx
->codec_id
== AV_CODEC_ID_VC1IMAGE
) {
1349 avctx
->width
= avctx
->coded_width
= v
->output_width
;
1350 avctx
->height
= avctx
->coded_height
= v
->output_height
;
1351 if (avctx
->skip_frame
>= AVDISCARD_NONREF
)
1353 if (!v
->sprite_output_frame
&&
1354 !(v
->sprite_output_frame
= av_frame_alloc())) {
1355 ret
= AVERROR(ENOMEM
);
1358 #if CONFIG_WMV3IMAGE_DECODER || CONFIG_VC1IMAGE_DECODER
1359 if ((ret
= vc1_decode_sprites(v
, &s
->gb
)) < 0)
1362 if ((ret
= av_frame_ref(pict
, v
->sprite_output_frame
)) < 0)
1366 if (s
->pict_type
== AV_PICTURE_TYPE_B
|| s
->low_delay
) {
1367 if ((ret
= av_frame_ref(pict
, s
->cur_pic
.ptr
->f
)) < 0)
1370 ff_print_debug_info(s
, s
->cur_pic
.ptr
, pict
);
1372 } else if (s
->last_pic
.ptr
) {
1373 if ((ret
= av_frame_ref(pict
, s
->last_pic
.ptr
->f
)) < 0)
1376 ff_print_debug_info(s
, s
->last_pic
.ptr
, pict
);
1383 for (i
= 0; i
< n_slices
; i
++)
1384 av_free(slices
[i
].buf
);
1390 for (i
= 0; i
< n_slices
; i
++)
1391 av_free(slices
[i
].buf
);
1397 const FFCodec ff_vc1_decoder
= {
1399 CODEC_LONG_NAME("SMPTE VC-1"),
1400 .p
.type
= AVMEDIA_TYPE_VIDEO
,
1401 .p
.id
= AV_CODEC_ID_VC1
,
1402 .priv_data_size
= sizeof(VC1Context
),
1403 .init
= vc1_decode_init
,
1404 .close
= ff_vc1_decode_end
,
1405 FF_CODEC_DECODE_CB(vc1_decode_frame
),
1406 .flush
= ff_mpeg_flush
,
1407 .p
.capabilities
= AV_CODEC_CAP_DR1
| AV_CODEC_CAP_DELAY
,
1408 .hw_configs
= (const AVCodecHWConfigInternal
*const []) {
1409 #if CONFIG_VC1_DXVA2_HWACCEL
1412 #if CONFIG_VC1_D3D11VA_HWACCEL
1413 HWACCEL_D3D11VA(vc1
),
1415 #if CONFIG_VC1_D3D11VA2_HWACCEL
1416 HWACCEL_D3D11VA2(vc1
),
1418 #if CONFIG_VC1_D3D12VA_HWACCEL
1419 HWACCEL_D3D12VA(vc1
),
1421 #if CONFIG_VC1_NVDEC_HWACCEL
1424 #if CONFIG_VC1_VAAPI_HWACCEL
1427 #if CONFIG_VC1_VDPAU_HWACCEL
1432 .p
.profiles
= NULL_IF_CONFIG_SMALL(ff_vc1_profiles
)
1435 #if CONFIG_WMV3_DECODER
1436 const FFCodec ff_wmv3_decoder
= {
1438 CODEC_LONG_NAME("Windows Media Video 9"),
1439 .p
.type
= AVMEDIA_TYPE_VIDEO
,
1440 .p
.id
= AV_CODEC_ID_WMV3
,
1441 .priv_data_size
= sizeof(VC1Context
),
1442 .init
= vc1_decode_init
,
1443 .close
= ff_vc1_decode_end
,
1444 FF_CODEC_DECODE_CB(vc1_decode_frame
),
1445 .flush
= ff_mpeg_flush
,
1446 .p
.capabilities
= AV_CODEC_CAP_DR1
| AV_CODEC_CAP_DELAY
,
1447 .hw_configs
= (const AVCodecHWConfigInternal
*const []) {
1448 #if CONFIG_WMV3_DXVA2_HWACCEL
1449 HWACCEL_DXVA2(wmv3
),
1451 #if CONFIG_WMV3_D3D11VA_HWACCEL
1452 HWACCEL_D3D11VA(wmv3
),
1454 #if CONFIG_WMV3_D3D11VA2_HWACCEL
1455 HWACCEL_D3D11VA2(wmv3
),
1457 #if CONFIG_WMV3_D3D12VA_HWACCEL
1458 HWACCEL_D3D12VA(wmv3
),
1460 #if CONFIG_WMV3_NVDEC_HWACCEL
1461 HWACCEL_NVDEC(wmv3
),
1463 #if CONFIG_WMV3_VAAPI_HWACCEL
1464 HWACCEL_VAAPI(wmv3
),
1466 #if CONFIG_WMV3_VDPAU_HWACCEL
1467 HWACCEL_VDPAU(wmv3
),
1471 .p
.profiles
= NULL_IF_CONFIG_SMALL(ff_vc1_profiles
)
1475 #if CONFIG_WMV3IMAGE_DECODER
1476 const FFCodec ff_wmv3image_decoder
= {
1477 .p
.name
= "wmv3image",
1478 CODEC_LONG_NAME("Windows Media Video 9 Image"),
1479 .p
.type
= AVMEDIA_TYPE_VIDEO
,
1480 .p
.id
= AV_CODEC_ID_WMV3IMAGE
,
1481 .priv_data_size
= sizeof(VC1Context
),
1482 .init
= vc1_decode_init
,
1483 .close
= ff_vc1_decode_end
,
1484 FF_CODEC_DECODE_CB(vc1_decode_frame
),
1485 .p
.capabilities
= AV_CODEC_CAP_DR1
,
1486 .flush
= vc1_sprite_flush
,
1490 #if CONFIG_VC1IMAGE_DECODER
1491 const FFCodec ff_vc1image_decoder
= {
1492 .p
.name
= "vc1image",
1493 CODEC_LONG_NAME("Windows Media Video 9 Image v2"),
1494 .p
.type
= AVMEDIA_TYPE_VIDEO
,
1495 .p
.id
= AV_CODEC_ID_VC1IMAGE
,
1496 .priv_data_size
= sizeof(VC1Context
),
1497 .init
= vc1_decode_init
,
1498 .close
= ff_vc1_decode_end
,
1499 FF_CODEC_DECODE_CB(vc1_decode_frame
),
1500 .p
.capabilities
= AV_CODEC_CAP_DR1
,
1501 .flush
= vc1_sprite_flush
,