intrax8: Keep a reference to the context idctdsp
[libav.git] / libavcodec / vc1dec.c
1 /*
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
6 *
7 * This file is part of Libav.
8 *
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.
13 *
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.
18 *
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
22 */
23
24 /**
25 * @file
26 * VC-1 and WMV3 decoder
27 */
28
29 #include "avcodec.h"
30 #include "blockdsp.h"
31 #include "get_bits.h"
32 #include "internal.h"
33 #include "mpeg_er.h"
34 #include "mpegvideo.h"
35 #include "msmpeg4.h"
36 #include "msmpeg4data.h"
37 #include "profiles.h"
38 #include "vc1.h"
39 #include "vc1data.h"
40
41 #if CONFIG_WMV3IMAGE_DECODER || CONFIG_VC1IMAGE_DECODER
42
43 typedef struct SpriteData {
44 /**
45 * Transform coefficients for both sprites in 16.16 fixed point format,
46 * in the order they appear in the bitstream:
47 * x scale
48 * rotation 1 (unused)
49 * x offset
50 * rotation 2 (unused)
51 * y scale
52 * y offset
53 * alpha
54 */
55 int coefs[2][7];
56
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
60 } SpriteData;
61
62 static inline int get_fp_val(GetBitContext* gb)
63 {
64 return (get_bits_long(gb, 30) - (1 << 29)) << 1;
65 }
66
67 static void vc1_sprite_parse_transform(GetBitContext* gb, int c[7])
68 {
69 c[1] = c[3] = 0;
70
71 switch (get_bits(gb, 2)) {
72 case 0:
73 c[0] = 1 << 16;
74 c[2] = get_fp_val(gb);
75 c[4] = 1 << 16;
76 break;
77 case 1:
78 c[0] = c[4] = get_fp_val(gb);
79 c[2] = get_fp_val(gb);
80 break;
81 case 2:
82 c[0] = get_fp_val(gb);
83 c[2] = get_fp_val(gb);
84 c[4] = get_fp_val(gb);
85 break;
86 case 3:
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);
92 break;
93 }
94 c[5] = get_fp_val(gb);
95 if (get_bits1(gb))
96 c[6] = get_fp_val(gb);
97 else
98 c[6] = 1 << 16;
99 }
100
101 static void vc1_parse_sprites(VC1Context *v, GetBitContext* gb, SpriteData* sd)
102 {
103 AVCodecContext *avctx = v->s.avctx;
104 int sprite, i;
105
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");
116 }
117
118 skip_bits(gb, 2);
119 if (sd->effect_type = get_bits_long(gb, 30)) {
120 switch (sd->effect_pcount1 = get_bits(gb, 4)) {
121 case 7:
122 vc1_sprite_parse_transform(gb, sd->effect_params1);
123 break;
124 case 14:
125 vc1_sprite_parse_transform(gb, sd->effect_params1);
126 vc1_sprite_parse_transform(gb, sd->effect_params1 + 7);
127 break;
128 default:
129 for (i = 0; i < sd->effect_pcount1; i++)
130 sd->effect_params1[i] = get_fp_val(gb);
131 }
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");
140 }
141
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");
145 return;
146 } else if (sd->effect_pcount2) {
147 i = -1;
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));
154 }
155 av_log(avctx, AV_LOG_DEBUG, "\n");
156 }
157 }
158 if (sd->effect_flag = get_bits1(gb))
159 av_log(avctx, AV_LOG_DEBUG, "Effect flag set\n");
160
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");
166 }
167
168 static void vc1_draw_sprites(VC1Context *v, SpriteData* sd)
169 {
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;
174 int ysub[2];
175 MpegEncContext *s = &v->s;
176
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);
182
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);
185 }
186 alpha = av_clip_uint16(sd->coefs[1][6]);
187
188 for (plane = 0; plane < (s->avctx->flags & AV_CODEC_FLAG_GRAY ? 1 : 3); plane++) {
189 int width = v->output_width>>!!plane;
190
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;
194
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;
200 int next_line;
201 ysub[sprite] = ycoord & 0xFFFF;
202 if (sprite) {
203 iplane = s->last_picture.f->data[plane];
204 iline = s->last_picture.f->linesize[plane];
205 }
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;
209 if (ysub[sprite])
210 src_h[sprite][1] = iplane + (xoff[sprite] >> 16) + next_line;
211 } else {
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]);
216 } else {
217 v->vc1dsp.sprite_h(v->sr_rows[sprite][0], iplane + yline * iline, xoff[sprite], xadv[sprite], width);
218 sr_cache[sprite][0] = yline;
219 }
220 }
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;
226 }
227 src_h[sprite][0] = v->sr_rows[sprite][0];
228 src_h[sprite][1] = v->sr_rows[sprite][1];
229 }
230 }
231
232 if (!v->two_sprites) {
233 if (ysub[0]) {
234 v->vc1dsp.sprite_v_single(dst, src_h[0][0], src_h[0][1], ysub[0], width);
235 } else {
236 memcpy(dst, src_h[0][0], width);
237 }
238 } else {
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);
248 } else {
249 v->vc1dsp.sprite_v_double_noscale(dst, src_h[0][0], src_h[1][0], alpha, width);
250 }
251 }
252 }
253
254 if (!plane) {
255 for (i = 0; i < 2; i++) {
256 xoff[i] >>= 1;
257 yoff[i] >>= 1;
258 }
259 }
260
261 }
262 }
263
264
265 static int vc1_decode_sprites(VC1Context *v, GetBitContext* gb)
266 {
267 MpegEncContext *s = &v->s;
268 AVCodecContext *avctx = s->avctx;
269 SpriteData sd;
270
271 vc1_parse_sprites(v, gb, &sd);
272
273 if (!s->current_picture.f->data[0]) {
274 av_log(avctx, AV_LOG_ERROR, "Got no sprites\n");
275 return -1;
276 }
277
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");
280 v->two_sprites = 0;
281 }
282
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");
286 return -1;
287 }
288
289 vc1_draw_sprites(v, &sd);
290
291 return 0;
292 }
293
294 static void vc1_sprite_flush(AVCodecContext *avctx)
295 {
296 VC1Context *v = avctx->priv_data;
297 MpegEncContext *s = &v->s;
298 AVFrame *f = s->current_picture.f;
299 int plane, i;
300
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. */
304
305 if (f && f->data[0])
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]);
310 }
311
312 #endif
313
314 av_cold int ff_vc1_decode_init_alloc_tables(VC1Context *v)
315 {
316 MpegEncContext *s = &v->s;
317 int i, ret = AVERROR(ENOMEM);
318 int mb_height = FFALIGN(s->mb_height, 2);
319
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);
327 if (!v->mv_type_mb_plane || !v->direct_mb_plane || !v->forward_mb_plane ||
328 !v->fieldtx_plane || !v->acpred_plane || !v->over_flags_plane)
329 goto error;
330
331 v->n_allocated_blks = s->mb_width + 2;
332 v->block = av_malloc(sizeof(*v->block) * v->n_allocated_blks);
333 v->cbp_base = av_malloc(sizeof(v->cbp_base[0]) * 2 * s->mb_stride);
334 if (!v->block || !v->cbp_base)
335 goto error;
336 v->cbp = v->cbp_base + s->mb_stride;
337 v->ttblk_base = av_malloc(sizeof(v->ttblk_base[0]) * 2 * s->mb_stride);
338 if (!v->ttblk_base)
339 goto error;
340 v->ttblk = v->ttblk_base + s->mb_stride;
341 v->is_intra_base = av_mallocz(sizeof(v->is_intra_base[0]) * 2 * s->mb_stride);
342 if (!v->is_intra_base)
343 goto error;
344 v->is_intra = v->is_intra_base + s->mb_stride;
345 v->luma_mv_base = av_malloc(sizeof(v->luma_mv_base[0]) * 2 * s->mb_stride);
346 if (!v->luma_mv_base)
347 goto error;
348 v->luma_mv = v->luma_mv_base + s->mb_stride;
349
350 /* allocate block type info in that way so it could be used with s->block_index[] */
351 v->mb_type_base = av_malloc(s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2);
352 if (!v->mb_type_base)
353 goto error;
354 v->mb_type[0] = v->mb_type_base + s->b8_stride + 1;
355 v->mb_type[1] = v->mb_type_base + s->b8_stride * (mb_height * 2 + 1) + s->mb_stride + 1;
356 v->mb_type[2] = v->mb_type[1] + s->mb_stride * (mb_height + 1);
357
358 /* allocate memory to store block level MV info */
359 v->blk_mv_type_base = av_mallocz( s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2);
360 if (!v->blk_mv_type_base)
361 goto error;
362 v->blk_mv_type = v->blk_mv_type_base + s->b8_stride + 1;
363 v->mv_f_base = av_mallocz(2 * (s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2));
364 if (!v->mv_f_base)
365 goto error;
366 v->mv_f[0] = v->mv_f_base + s->b8_stride + 1;
367 v->mv_f[1] = v->mv_f[0] + (s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2);
368 v->mv_f_next_base = av_mallocz(2 * (s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2));
369 if (!v->mv_f_next_base)
370 goto error;
371 v->mv_f_next[0] = v->mv_f_next_base + s->b8_stride + 1;
372 v->mv_f_next[1] = v->mv_f_next[0] + (s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2);
373
374 if (s->avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || s->avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
375 for (i = 0; i < 4; i++) {
376 v->sr_rows[i >> 1][i & 1] = av_malloc(v->output_width);
377 if (!v->sr_rows[i >> 1][i & 1])
378 goto error;
379 }
380 }
381
382 ret = ff_intrax8_common_init(&v->x8, &s->idsp, s);
383 if (ret < 0)
384 goto error;
385
386 return 0;
387
388 error:
389 ff_vc1_decode_end(s->avctx);
390 return ret;
391 }
392
393 av_cold void ff_vc1_init_transposed_scantables(VC1Context *v)
394 {
395 int i;
396 for (i = 0; i < 64; i++) {
397 #define transpose(x) ((x >> 3) | ((x & 7) << 3))
398 v->zz_8x8[0][i] = transpose(ff_wmv1_scantable[0][i]);
399 v->zz_8x8[1][i] = transpose(ff_wmv1_scantable[1][i]);
400 v->zz_8x8[2][i] = transpose(ff_wmv1_scantable[2][i]);
401 v->zz_8x8[3][i] = transpose(ff_wmv1_scantable[3][i]);
402 v->zzi_8x8[i] = transpose(ff_vc1_adv_interlaced_8x8_zz[i]);
403 }
404 v->left_blk_sh = 0;
405 v->top_blk_sh = 3;
406 }
407
408 /** Initialize a VC1/WMV3 decoder
409 * @todo TODO: Handle VC-1 IDUs (Transport level?)
410 * @todo TODO: Decypher remaining bits in extra_data
411 */
412 static av_cold int vc1_decode_init(AVCodecContext *avctx)
413 {
414 VC1Context *v = avctx->priv_data;
415 MpegEncContext *s = &v->s;
416 GetBitContext gb;
417
418 /* save the container output size for WMImage */
419 v->output_width = avctx->width;
420 v->output_height = avctx->height;
421
422 if (!avctx->extradata_size || !avctx->extradata)
423 return -1;
424 if (!(avctx->flags & AV_CODEC_FLAG_GRAY))
425 avctx->pix_fmt = ff_get_format(avctx, avctx->codec->pix_fmts);
426 else
427 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
428 v->s.avctx = avctx;
429
430 if (ff_vc1_init_common(v) < 0)
431 return -1;
432 ff_blockdsp_init(&s->bdsp, avctx);
433 ff_h264chroma_init(&v->h264chroma, 8);
434 ff_qpeldsp_init(&s->qdsp);
435
436 if (avctx->codec_id == AV_CODEC_ID_WMV3 || avctx->codec_id == AV_CODEC_ID_WMV3IMAGE) {
437 int count = 0;
438
439 // looks like WMV3 has a sequence header stored in the extradata
440 // advanced sequence header may be before the first frame
441 // the last byte of the extradata is a version number, 1 for the
442 // samples we can decode
443
444 init_get_bits(&gb, avctx->extradata, avctx->extradata_size*8);
445
446 if (ff_vc1_decode_sequence_header(avctx, v, &gb) < 0)
447 return -1;
448
449 count = avctx->extradata_size*8 - get_bits_count(&gb);
450 if (count > 0) {
451 av_log(avctx, AV_LOG_INFO, "Extra data: %i bits left, value: %X\n",
452 count, get_bits_long(&gb, FFMIN(count, 32)));
453 } else if (count < 0) {
454 av_log(avctx, AV_LOG_INFO, "Read %i bits in overflow\n", -count);
455 }
456 } else { // VC1/WVC1/WVP2
457 const uint8_t *start = avctx->extradata;
458 uint8_t *end = avctx->extradata + avctx->extradata_size;
459 const uint8_t *next;
460 int size, buf2_size;
461 uint8_t *buf2 = NULL;
462 int seq_initialized = 0, ep_initialized = 0;
463
464 if (avctx->extradata_size < 16) {
465 av_log(avctx, AV_LOG_ERROR, "Extradata size too small: %i\n", avctx->extradata_size);
466 return -1;
467 }
468
469 buf2 = av_mallocz(avctx->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
470 start = find_next_marker(start, end); // in WVC1 extradata first byte is its size, but can be 0 in mkv
471 next = start;
472 for (; next < end; start = next) {
473 next = find_next_marker(start + 4, end);
474 size = next - start - 4;
475 if (size <= 0)
476 continue;
477 buf2_size = vc1_unescape_buffer(start + 4, size, buf2);
478 init_get_bits(&gb, buf2, buf2_size * 8);
479 switch (AV_RB32(start)) {
480 case VC1_CODE_SEQHDR:
481 if (ff_vc1_decode_sequence_header(avctx, v, &gb) < 0) {
482 av_free(buf2);
483 return -1;
484 }
485 seq_initialized = 1;
486 break;
487 case VC1_CODE_ENTRYPOINT:
488 if (ff_vc1_decode_entry_point(avctx, v, &gb) < 0) {
489 av_free(buf2);
490 return -1;
491 }
492 ep_initialized = 1;
493 break;
494 }
495 }
496 av_free(buf2);
497 if (!seq_initialized || !ep_initialized) {
498 av_log(avctx, AV_LOG_ERROR, "Incomplete extradata\n");
499 return -1;
500 }
501 v->res_sprite = (avctx->codec_id == AV_CODEC_ID_VC1IMAGE);
502 }
503
504 v->sprite_output_frame = av_frame_alloc();
505 if (!v->sprite_output_frame)
506 return AVERROR(ENOMEM);
507
508 avctx->profile = v->profile;
509 if (v->profile == PROFILE_ADVANCED)
510 avctx->level = v->level;
511
512 avctx->has_b_frames = !!avctx->max_b_frames;
513
514 if (v->color_prim == 1 || v->color_prim == 5 || v->color_prim == 6)
515 avctx->color_primaries = v->color_prim;
516 if (v->transfer_char == 1 || v->transfer_char == 7)
517 avctx->color_trc = v->transfer_char;
518 if (v->matrix_coef == 1 || v->matrix_coef == 6 || v->matrix_coef == 7)
519 avctx->colorspace = v->matrix_coef;
520
521 s->mb_width = (avctx->coded_width + 15) >> 4;
522 s->mb_height = (avctx->coded_height + 15) >> 4;
523
524 if (v->profile == PROFILE_ADVANCED || v->res_fasttx) {
525 ff_vc1_init_transposed_scantables(v);
526 } else {
527 memcpy(v->zz_8x8, ff_wmv1_scantable, 4*64);
528 v->left_blk_sh = 3;
529 v->top_blk_sh = 0;
530 }
531
532 if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
533 v->sprite_width = avctx->coded_width;
534 v->sprite_height = avctx->coded_height;
535
536 avctx->coded_width = avctx->width = v->output_width;
537 avctx->coded_height = avctx->height = v->output_height;
538
539 // prevent 16.16 overflows
540 if (v->sprite_width > 1 << 14 ||
541 v->sprite_height > 1 << 14 ||
542 v->output_width > 1 << 14 ||
543 v->output_height > 1 << 14) return -1;
544 }
545 return 0;
546 }
547
548 /** Close a VC1/WMV3 decoder
549 * @warning Initial try at using MpegEncContext stuff
550 */
551 av_cold int ff_vc1_decode_end(AVCodecContext *avctx)
552 {
553 VC1Context *v = avctx->priv_data;
554 int i;
555
556 av_frame_free(&v->sprite_output_frame);
557
558 for (i = 0; i < 4; i++)
559 av_freep(&v->sr_rows[i >> 1][i & 1]);
560 av_freep(&v->hrd_rate);
561 av_freep(&v->hrd_buffer);
562 ff_mpv_common_end(&v->s);
563 av_freep(&v->mv_type_mb_plane);
564 av_freep(&v->direct_mb_plane);
565 av_freep(&v->forward_mb_plane);
566 av_freep(&v->fieldtx_plane);
567 av_freep(&v->acpred_plane);
568 av_freep(&v->over_flags_plane);
569 av_freep(&v->mb_type_base);
570 av_freep(&v->blk_mv_type_base);
571 av_freep(&v->mv_f_base);
572 av_freep(&v->mv_f_next_base);
573 av_freep(&v->block);
574 av_freep(&v->cbp_base);
575 av_freep(&v->ttblk_base);
576 av_freep(&v->is_intra_base); // FIXME use v->mb_type[]
577 av_freep(&v->luma_mv_base);
578 ff_intrax8_common_end(&v->x8);
579 return 0;
580 }
581
582
583 /** Decode a VC1/WMV3 frame
584 * @todo TODO: Handle VC-1 IDUs (Transport level?)
585 */
586 static int vc1_decode_frame(AVCodecContext *avctx, void *data,
587 int *got_frame, AVPacket *avpkt)
588 {
589 const uint8_t *buf = avpkt->data;
590 int buf_size = avpkt->size, n_slices = 0, i, ret;
591 VC1Context *v = avctx->priv_data;
592 MpegEncContext *s = &v->s;
593 AVFrame *pict = data;
594 uint8_t *buf2 = NULL;
595 const uint8_t *buf_start = buf;
596 int mb_height, n_slices1;
597 struct {
598 uint8_t *buf;
599 GetBitContext gb;
600 int mby_start;
601 } *slices = NULL, *tmp;
602
603 /* no supplementary picture */
604 if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == VC1_CODE_ENDOFSEQ)) {
605 /* special case for last picture */
606 if (s->low_delay == 0 && s->next_picture_ptr) {
607 if ((ret = av_frame_ref(pict, s->next_picture_ptr->f)) < 0)
608 return ret;
609 s->next_picture_ptr = NULL;
610
611 *got_frame = 1;
612 }
613
614 return 0;
615 }
616
617 //for advanced profile we may need to parse and unescape data
618 if (avctx->codec_id == AV_CODEC_ID_VC1 || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
619 int buf_size2 = 0;
620 buf2 = av_mallocz(buf_size + AV_INPUT_BUFFER_PADDING_SIZE);
621
622 if (IS_MARKER(AV_RB32(buf))) { /* frame starts with marker and needs to be parsed */
623 const uint8_t *start, *end, *next;
624 int size;
625
626 next = buf;
627 for (start = buf, end = buf + buf_size; next < end; start = next) {
628 next = find_next_marker(start + 4, end);
629 size = next - start - 4;
630 if (size <= 0) continue;
631 switch (AV_RB32(start)) {
632 case VC1_CODE_FRAME:
633 if (avctx->hwaccel)
634 buf_start = start;
635 buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
636 break;
637 case VC1_CODE_FIELD: {
638 int buf_size3;
639 tmp = av_realloc(slices, sizeof(*slices) * (n_slices+1));
640 if (!tmp)
641 goto err;
642 slices = tmp;
643 slices[n_slices].buf = av_mallocz(buf_size + AV_INPUT_BUFFER_PADDING_SIZE);
644 if (!slices[n_slices].buf)
645 goto err;
646 buf_size3 = vc1_unescape_buffer(start + 4, size,
647 slices[n_slices].buf);
648 init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
649 buf_size3 << 3);
650 /* assuming that the field marker is at the exact middle,
651 hope it's correct */
652 slices[n_slices].mby_start = s->mb_height >> 1;
653 n_slices1 = n_slices - 1; // index of the last slice of the first field
654 n_slices++;
655 break;
656 }
657 case VC1_CODE_ENTRYPOINT: /* it should be before frame data */
658 buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
659 init_get_bits(&s->gb, buf2, buf_size2 * 8);
660 ff_vc1_decode_entry_point(avctx, v, &s->gb);
661 break;
662 case VC1_CODE_SLICE: {
663 int buf_size3;
664 tmp = av_realloc(slices, sizeof(*slices) * (n_slices+1));
665 if (!tmp)
666 goto err;
667 slices = tmp;
668 slices[n_slices].buf = av_mallocz(buf_size + AV_INPUT_BUFFER_PADDING_SIZE);
669 if (!slices[n_slices].buf)
670 goto err;
671 buf_size3 = vc1_unescape_buffer(start + 4, size,
672 slices[n_slices].buf);
673 init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
674 buf_size3 << 3);
675 slices[n_slices].mby_start = get_bits(&slices[n_slices].gb, 9);
676 n_slices++;
677 break;
678 }
679 }
680 }
681 } else if (v->interlace && ((buf[0] & 0xC0) == 0xC0)) { /* WVC1 interlaced stores both fields divided by marker */
682 const uint8_t *divider;
683 int buf_size3;
684
685 divider = find_next_marker(buf, buf + buf_size);
686 if ((divider == (buf + buf_size)) || AV_RB32(divider) != VC1_CODE_FIELD) {
687 av_log(avctx, AV_LOG_ERROR, "Error in WVC1 interlaced frame\n");
688 goto err;
689 } else { // found field marker, unescape second field
690 tmp = av_realloc(slices, sizeof(*slices) * (n_slices+1));
691 if (!tmp)
692 goto err;
693 slices = tmp;
694 slices[n_slices].buf = av_mallocz(buf_size + AV_INPUT_BUFFER_PADDING_SIZE);
695 if (!slices[n_slices].buf)
696 goto err;
697 buf_size3 = vc1_unescape_buffer(divider + 4, buf + buf_size - divider - 4, slices[n_slices].buf);
698 init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
699 buf_size3 << 3);
700 slices[n_slices].mby_start = s->mb_height >> 1;
701 n_slices1 = n_slices - 1;
702 n_slices++;
703 }
704 buf_size2 = vc1_unescape_buffer(buf, divider - buf, buf2);
705 } else {
706 buf_size2 = vc1_unescape_buffer(buf, buf_size, buf2);
707 }
708 init_get_bits(&s->gb, buf2, buf_size2*8);
709 } else
710 init_get_bits(&s->gb, buf, buf_size*8);
711
712 if (v->res_sprite) {
713 v->new_sprite = !get_bits1(&s->gb);
714 v->two_sprites = get_bits1(&s->gb);
715 /* res_sprite means a Windows Media Image stream, AV_CODEC_ID_*IMAGE means
716 we're using the sprite compositor. These are intentionally kept separate
717 so you can get the raw sprites by using the wmv3 decoder for WMVP or
718 the vc1 one for WVP2 */
719 if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
720 if (v->new_sprite) {
721 // switch AVCodecContext parameters to those of the sprites
722 avctx->width = avctx->coded_width = v->sprite_width;
723 avctx->height = avctx->coded_height = v->sprite_height;
724 } else {
725 goto image;
726 }
727 }
728 }
729
730 if (s->context_initialized &&
731 (s->width != avctx->coded_width ||
732 s->height != avctx->coded_height)) {
733 ff_vc1_decode_end(avctx);
734 }
735
736 if (!s->context_initialized) {
737 if (ff_msmpeg4_decode_init(avctx) < 0)
738 goto err;
739 if (ff_vc1_decode_init_alloc_tables(v) < 0) {
740 ff_mpv_common_end(s);
741 goto err;
742 }
743
744 s->low_delay = !avctx->has_b_frames || v->res_sprite;
745
746 if (v->profile == PROFILE_ADVANCED) {
747 s->h_edge_pos = avctx->coded_width;
748 s->v_edge_pos = avctx->coded_height;
749 }
750 }
751
752 // do parse frame header
753 v->pic_header_flag = 0;
754 v->first_pic_header_flag = 1;
755 if (v->profile < PROFILE_ADVANCED) {
756 if (ff_vc1_parse_frame_header(v, &s->gb) < 0) {
757 goto err;
758 }
759 } else {
760 if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
761 goto err;
762 }
763 }
764 v->first_pic_header_flag = 0;
765
766 if ((avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE)
767 && s->pict_type != AV_PICTURE_TYPE_I) {
768 av_log(v->s.avctx, AV_LOG_ERROR, "Sprite decoder: expected I-frame\n");
769 goto err;
770 }
771
772 // for skipping the frame
773 s->current_picture.f->pict_type = s->pict_type;
774 s->current_picture.f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
775
776 /* skip B-frames if we don't have reference frames */
777 if (!s->last_picture_ptr && (s->pict_type == AV_PICTURE_TYPE_B || s->droppable)) {
778 goto end;
779 }
780 if ((avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B) ||
781 (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I) ||
782 avctx->skip_frame >= AVDISCARD_ALL) {
783 goto end;
784 }
785
786 if (s->next_p_frame_damaged) {
787 if (s->pict_type == AV_PICTURE_TYPE_B)
788 goto end;
789 else
790 s->next_p_frame_damaged = 0;
791 }
792
793 if (ff_mpv_frame_start(s, avctx) < 0) {
794 goto err;
795 }
796
797 // process pulldown flags
798 s->current_picture_ptr->f->repeat_pict = 0;
799 // Pulldown flags are only valid when 'broadcast' has been set.
800 // So ticks_per_frame will be 2
801 if (v->rff) {
802 // repeat field
803 s->current_picture_ptr->f->repeat_pict = 1;
804 } else if (v->rptfrm) {
805 // repeat frames
806 s->current_picture_ptr->f->repeat_pict = v->rptfrm * 2;
807 }
808
809 s->me.qpel_put = s->qdsp.put_qpel_pixels_tab;
810 s->me.qpel_avg = s->qdsp.avg_qpel_pixels_tab;
811
812 if (avctx->hwaccel) {
813 if (avctx->hwaccel->start_frame(avctx, buf, buf_size) < 0)
814 goto err;
815 if (avctx->hwaccel->decode_slice(avctx, buf_start, (buf + buf_size) - buf_start) < 0)
816 goto err;
817 if (avctx->hwaccel->end_frame(avctx) < 0)
818 goto err;
819 } else {
820 int header_ret = 0;
821
822 ff_mpeg_er_frame_start(s);
823
824 v->bits = buf_size * 8;
825 v->end_mb_x = s->mb_width;
826 if (v->field_mode) {
827 s->current_picture.f->linesize[0] <<= 1;
828 s->current_picture.f->linesize[1] <<= 1;
829 s->current_picture.f->linesize[2] <<= 1;
830 s->linesize <<= 1;
831 s->uvlinesize <<= 1;
832 }
833 mb_height = s->mb_height >> v->field_mode;
834
835 if (!mb_height) {
836 av_log(v->s.avctx, AV_LOG_ERROR, "Invalid mb_height.\n");
837 goto err;
838 }
839
840 for (i = 0; i <= n_slices; i++) {
841 if (i > 0 && slices[i - 1].mby_start >= mb_height) {
842 if (v->field_mode <= 0) {
843 av_log(v->s.avctx, AV_LOG_ERROR, "Slice %d starts beyond "
844 "picture boundary (%d >= %d)\n", i,
845 slices[i - 1].mby_start, mb_height);
846 continue;
847 }
848 v->second_field = 1;
849 v->blocks_off = s->mb_width * s->mb_height << 1;
850 v->mb_off = s->mb_stride * s->mb_height >> 1;
851 } else {
852 v->second_field = 0;
853 v->blocks_off = 0;
854 v->mb_off = 0;
855 }
856 if (i) {
857 v->pic_header_flag = 0;
858 if (v->field_mode && i == n_slices1 + 2) {
859 if ((header_ret = ff_vc1_parse_frame_header_adv(v, &s->gb)) < 0) {
860 av_log(v->s.avctx, AV_LOG_ERROR, "Field header damaged\n");
861 if (avctx->err_recognition & AV_EF_EXPLODE)
862 goto err;
863 continue;
864 }
865 } else if (get_bits1(&s->gb)) {
866 v->pic_header_flag = 1;
867 if ((header_ret = ff_vc1_parse_frame_header_adv(v, &s->gb)) < 0) {
868 av_log(v->s.avctx, AV_LOG_ERROR, "Slice header damaged\n");
869 if (avctx->err_recognition & AV_EF_EXPLODE)
870 goto err;
871 continue;
872 }
873 }
874 }
875 if (header_ret < 0)
876 continue;
877 s->start_mb_y = (i == 0) ? 0 : FFMAX(0, slices[i-1].mby_start % mb_height);
878 if (!v->field_mode || v->second_field)
879 s->end_mb_y = (i == n_slices ) ? mb_height : FFMIN(mb_height, slices[i].mby_start % mb_height);
880 else
881 s->end_mb_y = (i <= n_slices1 + 1) ? mb_height : FFMIN(mb_height, slices[i].mby_start % mb_height);
882 ff_vc1_decode_blocks(v);
883 if (i != n_slices)
884 s->gb = slices[i].gb;
885 }
886 if (v->field_mode) {
887 v->second_field = 0;
888 s->current_picture.f->linesize[0] >>= 1;
889 s->current_picture.f->linesize[1] >>= 1;
890 s->current_picture.f->linesize[2] >>= 1;
891 s->linesize >>= 1;
892 s->uvlinesize >>= 1;
893 if (v->s.pict_type != AV_PICTURE_TYPE_BI && v->s.pict_type != AV_PICTURE_TYPE_B) {
894 FFSWAP(uint8_t *, v->mv_f_next[0], v->mv_f[0]);
895 FFSWAP(uint8_t *, v->mv_f_next[1], v->mv_f[1]);
896 }
897 }
898 ff_dlog(s->avctx, "Consumed %i/%i bits\n",
899 get_bits_count(&s->gb), s->gb.size_in_bits);
900 // if (get_bits_count(&s->gb) > buf_size * 8)
901 // return -1;
902 if (!v->field_mode)
903 ff_er_frame_end(&s->er);
904 }
905
906 ff_mpv_frame_end(s);
907
908 if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
909 image:
910 avctx->width = avctx->coded_width = v->output_width;
911 avctx->height = avctx->coded_height = v->output_height;
912 if (avctx->skip_frame >= AVDISCARD_NONREF)
913 goto end;
914 #if CONFIG_WMV3IMAGE_DECODER || CONFIG_VC1IMAGE_DECODER
915 if (vc1_decode_sprites(v, &s->gb))
916 goto err;
917 #endif
918 if ((ret = av_frame_ref(pict, v->sprite_output_frame)) < 0)
919 goto err;
920 *got_frame = 1;
921 } else {
922 if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
923 if ((ret = av_frame_ref(pict, s->current_picture_ptr->f)) < 0)
924 goto err;
925 ff_print_debug_info(s, s->current_picture_ptr);
926 *got_frame = 1;
927 } else if (s->last_picture_ptr) {
928 if ((ret = av_frame_ref(pict, s->last_picture_ptr->f)) < 0)
929 goto err;
930 ff_print_debug_info(s, s->last_picture_ptr);
931 *got_frame = 1;
932 }
933 }
934
935 end:
936 av_free(buf2);
937 for (i = 0; i < n_slices; i++)
938 av_free(slices[i].buf);
939 av_free(slices);
940 return buf_size;
941
942 err:
943 av_free(buf2);
944 for (i = 0; i < n_slices; i++)
945 av_free(slices[i].buf);
946 av_free(slices);
947 return -1;
948 }
949
950
951 static const enum AVPixelFormat vc1_hwaccel_pixfmt_list_420[] = {
952 #if CONFIG_VC1_DXVA2_HWACCEL
953 AV_PIX_FMT_DXVA2_VLD,
954 #endif
955 #if CONFIG_VC1_D3D11VA_HWACCEL
956 AV_PIX_FMT_D3D11VA_VLD,
957 #endif
958 #if CONFIG_VC1_VAAPI_HWACCEL
959 AV_PIX_FMT_VAAPI,
960 #endif
961 #if CONFIG_VC1_VDPAU_HWACCEL
962 AV_PIX_FMT_VDPAU,
963 #endif
964 AV_PIX_FMT_YUV420P,
965 AV_PIX_FMT_NONE
966 };
967
968 AVCodec ff_vc1_decoder = {
969 .name = "vc1",
970 .long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-1"),
971 .type = AVMEDIA_TYPE_VIDEO,
972 .id = AV_CODEC_ID_VC1,
973 .priv_data_size = sizeof(VC1Context),
974 .init = vc1_decode_init,
975 .close = ff_vc1_decode_end,
976 .decode = vc1_decode_frame,
977 .flush = ff_mpeg_flush,
978 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY,
979 .pix_fmts = vc1_hwaccel_pixfmt_list_420,
980 .profiles = NULL_IF_CONFIG_SMALL(ff_vc1_profiles)
981 };
982
983 #if CONFIG_WMV3_DECODER
984 AVCodec ff_wmv3_decoder = {
985 .name = "wmv3",
986 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9"),
987 .type = AVMEDIA_TYPE_VIDEO,
988 .id = AV_CODEC_ID_WMV3,
989 .priv_data_size = sizeof(VC1Context),
990 .init = vc1_decode_init,
991 .close = ff_vc1_decode_end,
992 .decode = vc1_decode_frame,
993 .flush = ff_mpeg_flush,
994 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY,
995 .pix_fmts = vc1_hwaccel_pixfmt_list_420,
996 .profiles = NULL_IF_CONFIG_SMALL(ff_vc1_profiles)
997 };
998 #endif
999
1000 #if CONFIG_WMV3IMAGE_DECODER
1001 AVCodec ff_wmv3image_decoder = {
1002 .name = "wmv3image",
1003 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 Image"),
1004 .type = AVMEDIA_TYPE_VIDEO,
1005 .id = AV_CODEC_ID_WMV3IMAGE,
1006 .priv_data_size = sizeof(VC1Context),
1007 .init = vc1_decode_init,
1008 .close = ff_vc1_decode_end,
1009 .decode = vc1_decode_frame,
1010 .capabilities = AV_CODEC_CAP_DR1,
1011 .flush = vc1_sprite_flush,
1012 .pix_fmts = (const enum AVPixelFormat[]) {
1013 AV_PIX_FMT_YUV420P,
1014 AV_PIX_FMT_NONE
1015 },
1016 };
1017 #endif
1018
1019 #if CONFIG_VC1IMAGE_DECODER
1020 AVCodec ff_vc1image_decoder = {
1021 .name = "vc1image",
1022 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 Image v2"),
1023 .type = AVMEDIA_TYPE_VIDEO,
1024 .id = AV_CODEC_ID_VC1IMAGE,
1025 .priv_data_size = sizeof(VC1Context),
1026 .init = vc1_decode_init,
1027 .close = ff_vc1_decode_end,
1028 .decode = vc1_decode_frame,
1029 .capabilities = AV_CODEC_CAP_DR1,
1030 .flush = vc1_sprite_flush,
1031 .pix_fmts = (const enum AVPixelFormat[]) {
1032 AV_PIX_FMT_YUV420P,
1033 AV_PIX_FMT_NONE
1034 },
1035 };
1036 #endif