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