]> git.sesse.net Git - ffmpeg/blob - libavcodec/vc1dec.c
avcodec/vc1: Don't pretend ff_vc1_init_common() can fail
[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 FFmpeg.
8  *
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.
13  *
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.
18  *
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
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 "hwconfig.h"
33 #include "internal.h"
34 #include "mpeg_er.h"
35 #include "mpegvideo.h"
36 #include "msmpeg4.h"
37 #include "msmpeg4data.h"
38 #include "profiles.h"
39 #include "vc1.h"
40 #include "vc1data.h"
41 #include "libavutil/avassert.h"
42
43
44 #if CONFIG_WMV3IMAGE_DECODER || CONFIG_VC1IMAGE_DECODER
45
46 typedef struct SpriteData {
47     /**
48      * Transform coefficients for both sprites in 16.16 fixed point format,
49      * in the order they appear in the bitstream:
50      *  x scale
51      *  rotation 1 (unused)
52      *  x offset
53      *  rotation 2 (unused)
54      *  y scale
55      *  y offset
56      *  alpha
57      */
58     int coefs[2][7];
59
60     int effect_type, effect_flag;
61     int effect_pcount1, effect_pcount2;   ///< amount of effect parameters stored in effect_params
62     int effect_params1[15], effect_params2[10]; ///< effect parameters in 16.16 fixed point format
63 } SpriteData;
64
65 static inline int get_fp_val(GetBitContext* gb)
66 {
67     return (get_bits_long(gb, 30) - (1 << 29)) << 1;
68 }
69
70 static void vc1_sprite_parse_transform(GetBitContext* gb, int c[7])
71 {
72     c[1] = c[3] = 0;
73
74     switch (get_bits(gb, 2)) {
75     case 0:
76         c[0] = 1 << 16;
77         c[2] = get_fp_val(gb);
78         c[4] = 1 << 16;
79         break;
80     case 1:
81         c[0] = c[4] = get_fp_val(gb);
82         c[2] = get_fp_val(gb);
83         break;
84     case 2:
85         c[0] = get_fp_val(gb);
86         c[2] = get_fp_val(gb);
87         c[4] = get_fp_val(gb);
88         break;
89     case 3:
90         c[0] = get_fp_val(gb);
91         c[1] = get_fp_val(gb);
92         c[2] = get_fp_val(gb);
93         c[3] = get_fp_val(gb);
94         c[4] = get_fp_val(gb);
95         break;
96     }
97     c[5] = get_fp_val(gb);
98     if (get_bits1(gb))
99         c[6] = get_fp_val(gb);
100     else
101         c[6] = 1 << 16;
102 }
103
104 static int vc1_parse_sprites(VC1Context *v, GetBitContext* gb, SpriteData* sd)
105 {
106     AVCodecContext *avctx = v->s.avctx;
107     int sprite, i;
108
109     for (sprite = 0; sprite <= v->two_sprites; sprite++) {
110         vc1_sprite_parse_transform(gb, sd->coefs[sprite]);
111         if (sd->coefs[sprite][1] || sd->coefs[sprite][3])
112             avpriv_request_sample(avctx, "Non-zero rotation coefficients");
113         av_log(avctx, AV_LOG_DEBUG, sprite ? "S2:" : "S1:");
114         for (i = 0; i < 7; i++)
115             av_log(avctx, AV_LOG_DEBUG, " %d.%.3d",
116                    sd->coefs[sprite][i] / (1<<16),
117                    (abs(sd->coefs[sprite][i]) & 0xFFFF) * 1000 / (1 << 16));
118         av_log(avctx, AV_LOG_DEBUG, "\n");
119     }
120
121     skip_bits(gb, 2);
122     if (sd->effect_type = get_bits_long(gb, 30)) {
123         switch (sd->effect_pcount1 = get_bits(gb, 4)) {
124         case 7:
125             vc1_sprite_parse_transform(gb, sd->effect_params1);
126             break;
127         case 14:
128             vc1_sprite_parse_transform(gb, sd->effect_params1);
129             vc1_sprite_parse_transform(gb, sd->effect_params1 + 7);
130             break;
131         default:
132             for (i = 0; i < sd->effect_pcount1; i++)
133                 sd->effect_params1[i] = get_fp_val(gb);
134         }
135         if (sd->effect_type != 13 || sd->effect_params1[0] != sd->coefs[0][6]) {
136             // effect 13 is simple alpha blending and matches the opacity above
137             av_log(avctx, AV_LOG_DEBUG, "Effect: %d; params: ", sd->effect_type);
138             for (i = 0; i < sd->effect_pcount1; i++)
139                 av_log(avctx, AV_LOG_DEBUG, " %d.%.2d",
140                        sd->effect_params1[i] / (1 << 16),
141                        (abs(sd->effect_params1[i]) & 0xFFFF) * 1000 / (1 << 16));
142             av_log(avctx, AV_LOG_DEBUG, "\n");
143         }
144
145         sd->effect_pcount2 = get_bits(gb, 16);
146         if (sd->effect_pcount2 > 10) {
147             av_log(avctx, AV_LOG_ERROR, "Too many effect parameters\n");
148             return AVERROR_INVALIDDATA;
149         } else if (sd->effect_pcount2) {
150             i = -1;
151             av_log(avctx, AV_LOG_DEBUG, "Effect params 2: ");
152             while (++i < sd->effect_pcount2) {
153                 sd->effect_params2[i] = get_fp_val(gb);
154                 av_log(avctx, AV_LOG_DEBUG, " %d.%.2d",
155                        sd->effect_params2[i] / (1 << 16),
156                        (abs(sd->effect_params2[i]) & 0xFFFF) * 1000 / (1 << 16));
157             }
158             av_log(avctx, AV_LOG_DEBUG, "\n");
159         }
160     }
161     if (sd->effect_flag = get_bits1(gb))
162         av_log(avctx, AV_LOG_DEBUG, "Effect flag set\n");
163
164     if (get_bits_count(gb) >= gb->size_in_bits +
165        (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE ? 64 : 0)) {
166         av_log(avctx, AV_LOG_ERROR, "Buffer overrun\n");
167         return AVERROR_INVALIDDATA;
168     }
169     if (get_bits_count(gb) < gb->size_in_bits - 8)
170         av_log(avctx, AV_LOG_WARNING, "Buffer not fully read\n");
171
172     return 0;
173 }
174
175 static void vc1_draw_sprites(VC1Context *v, SpriteData* sd)
176 {
177     int i, plane, row, sprite;
178     int sr_cache[2][2] = { { -1, -1 }, { -1, -1 } };
179     uint8_t* src_h[2][2];
180     int xoff[2], xadv[2], yoff[2], yadv[2], alpha;
181     int ysub[2];
182     MpegEncContext *s = &v->s;
183
184     for (i = 0; i <= v->two_sprites; i++) {
185         xoff[i] = av_clip(sd->coefs[i][2], 0, v->sprite_width-1 << 16);
186         xadv[i] = sd->coefs[i][0];
187         if (xadv[i] != 1<<16 || (v->sprite_width << 16) - (v->output_width << 16) - xoff[i])
188             xadv[i] = av_clip(xadv[i], 0, ((v->sprite_width<<16) - xoff[i] - 1) / v->output_width);
189
190         yoff[i] = av_clip(sd->coefs[i][5], 0, v->sprite_height-1 << 16);
191         yadv[i] = av_clip(sd->coefs[i][4], 0, ((v->sprite_height << 16) - yoff[i]) / v->output_height);
192     }
193     alpha = av_clip_uint16(sd->coefs[1][6]);
194
195     for (plane = 0; plane < (CONFIG_GRAY && s->avctx->flags & AV_CODEC_FLAG_GRAY ? 1 : 3); plane++) {
196         int width = v->output_width>>!!plane;
197
198         for (row = 0; row < v->output_height>>!!plane; row++) {
199             uint8_t *dst = v->sprite_output_frame->data[plane] +
200                            v->sprite_output_frame->linesize[plane] * row;
201
202             for (sprite = 0; sprite <= v->two_sprites; sprite++) {
203                 uint8_t *iplane = s->current_picture.f->data[plane];
204                 int      iline  = s->current_picture.f->linesize[plane];
205                 int      ycoord = yoff[sprite] + yadv[sprite] * row;
206                 int      yline  = ycoord >> 16;
207                 int      next_line;
208                 ysub[sprite] = ycoord & 0xFFFF;
209                 if (sprite) {
210                     iplane = s->last_picture.f->data[plane];
211                     iline  = s->last_picture.f->linesize[plane];
212                 }
213                 next_line = FFMIN(yline + 1, (v->sprite_height >> !!plane) - 1) * iline;
214                 if (!(xoff[sprite] & 0xFFFF) && xadv[sprite] == 1 << 16) {
215                         src_h[sprite][0] = iplane + (xoff[sprite] >> 16) +  yline      * iline;
216                     if (ysub[sprite])
217                         src_h[sprite][1] = iplane + (xoff[sprite] >> 16) + next_line;
218                 } else {
219                     if (sr_cache[sprite][0] != yline) {
220                         if (sr_cache[sprite][1] == yline) {
221                             FFSWAP(uint8_t*, v->sr_rows[sprite][0], v->sr_rows[sprite][1]);
222                             FFSWAP(int,        sr_cache[sprite][0],   sr_cache[sprite][1]);
223                         } else {
224                             v->vc1dsp.sprite_h(v->sr_rows[sprite][0], iplane + yline * iline, xoff[sprite], xadv[sprite], width);
225                             sr_cache[sprite][0] = yline;
226                         }
227                     }
228                     if (ysub[sprite] && sr_cache[sprite][1] != yline + 1) {
229                         v->vc1dsp.sprite_h(v->sr_rows[sprite][1],
230                                            iplane + next_line, xoff[sprite],
231                                            xadv[sprite], width);
232                         sr_cache[sprite][1] = yline + 1;
233                     }
234                     src_h[sprite][0] = v->sr_rows[sprite][0];
235                     src_h[sprite][1] = v->sr_rows[sprite][1];
236                 }
237             }
238
239             if (!v->two_sprites) {
240                 if (ysub[0]) {
241                     v->vc1dsp.sprite_v_single(dst, src_h[0][0], src_h[0][1], ysub[0], width);
242                 } else {
243                     memcpy(dst, src_h[0][0], width);
244                 }
245             } else {
246                 if (ysub[0] && ysub[1]) {
247                     v->vc1dsp.sprite_v_double_twoscale(dst, src_h[0][0], src_h[0][1], ysub[0],
248                                                        src_h[1][0], src_h[1][1], ysub[1], alpha, width);
249                 } else if (ysub[0]) {
250                     v->vc1dsp.sprite_v_double_onescale(dst, src_h[0][0], src_h[0][1], ysub[0],
251                                                        src_h[1][0], alpha, width);
252                 } else if (ysub[1]) {
253                     v->vc1dsp.sprite_v_double_onescale(dst, src_h[1][0], src_h[1][1], ysub[1],
254                                                        src_h[0][0], (1<<16)-1-alpha, width);
255                 } else {
256                     v->vc1dsp.sprite_v_double_noscale(dst, src_h[0][0], src_h[1][0], alpha, width);
257                 }
258             }
259         }
260
261         if (!plane) {
262             for (i = 0; i <= v->two_sprites; i++) {
263                 xoff[i] >>= 1;
264                 yoff[i] >>= 1;
265             }
266         }
267
268     }
269 }
270
271
272 static int vc1_decode_sprites(VC1Context *v, GetBitContext* gb)
273 {
274     int ret;
275     MpegEncContext *s     = &v->s;
276     AVCodecContext *avctx = s->avctx;
277     SpriteData sd;
278
279     memset(&sd, 0, sizeof(sd));
280
281     ret = vc1_parse_sprites(v, gb, &sd);
282     if (ret < 0)
283         return ret;
284
285     if (!s->current_picture.f || !s->current_picture.f->data[0]) {
286         av_log(avctx, AV_LOG_ERROR, "Got no sprites\n");
287         return AVERROR_UNKNOWN;
288     }
289
290     if (v->two_sprites && (!s->last_picture_ptr || !s->last_picture.f->data[0])) {
291         av_log(avctx, AV_LOG_WARNING, "Need two sprites, only got one\n");
292         v->two_sprites = 0;
293     }
294
295     av_frame_unref(v->sprite_output_frame);
296     if ((ret = ff_get_buffer(avctx, v->sprite_output_frame, 0)) < 0)
297         return ret;
298
299     vc1_draw_sprites(v, &sd);
300
301     return 0;
302 }
303
304 static void vc1_sprite_flush(AVCodecContext *avctx)
305 {
306     VC1Context *v     = avctx->priv_data;
307     MpegEncContext *s = &v->s;
308     AVFrame *f = s->current_picture.f;
309     int plane, i;
310
311     /* Windows Media Image codecs have a convergence interval of two keyframes.
312        Since we can't enforce it, clear to black the missing sprite. This is
313        wrong but it looks better than doing nothing. */
314
315     if (f && f->data[0])
316         for (plane = 0; plane < (CONFIG_GRAY && s->avctx->flags & AV_CODEC_FLAG_GRAY ? 1 : 3); plane++)
317             for (i = 0; i < v->sprite_height>>!!plane; i++)
318                 memset(f->data[plane] + i * f->linesize[plane],
319                        plane ? 128 : 0, f->linesize[plane]);
320 }
321
322 #endif
323
324 av_cold int ff_vc1_decode_init_alloc_tables(VC1Context *v)
325 {
326     MpegEncContext *s = &v->s;
327     int i, ret = AVERROR(ENOMEM);
328     int mb_height = FFALIGN(s->mb_height, 2);
329
330     /* Allocate mb bitplanes */
331     v->mv_type_mb_plane = av_malloc (s->mb_stride * mb_height);
332     v->direct_mb_plane  = av_malloc (s->mb_stride * mb_height);
333     v->forward_mb_plane = av_malloc (s->mb_stride * mb_height);
334     v->fieldtx_plane    = av_mallocz(s->mb_stride * mb_height);
335     v->acpred_plane     = av_malloc (s->mb_stride * mb_height);
336     v->over_flags_plane = av_malloc (s->mb_stride * mb_height);
337     if (!v->mv_type_mb_plane || !v->direct_mb_plane || !v->forward_mb_plane ||
338         !v->fieldtx_plane || !v->acpred_plane || !v->over_flags_plane)
339         goto error;
340
341     v->n_allocated_blks = s->mb_width + 2;
342     v->block            = av_malloc(sizeof(*v->block) * v->n_allocated_blks);
343     v->cbp_base         = av_malloc(sizeof(v->cbp_base[0]) * 3 * s->mb_stride);
344     if (!v->block || !v->cbp_base)
345         goto error;
346     v->cbp              = v->cbp_base + 2 * s->mb_stride;
347     v->ttblk_base       = av_malloc(sizeof(v->ttblk_base[0]) * 3 * s->mb_stride);
348     if (!v->ttblk_base)
349         goto error;
350     v->ttblk            = v->ttblk_base + 2 * s->mb_stride;
351     v->is_intra_base    = av_mallocz(sizeof(v->is_intra_base[0]) * 3 * s->mb_stride);
352     if (!v->is_intra_base)
353         goto error;
354     v->is_intra         = v->is_intra_base + 2 * s->mb_stride;
355     v->luma_mv_base     = av_mallocz(sizeof(v->luma_mv_base[0]) * 3 * s->mb_stride);
356     if (!v->luma_mv_base)
357         goto error;
358     v->luma_mv          = v->luma_mv_base + 2 * s->mb_stride;
359
360     /* allocate block type info in that way so it could be used with s->block_index[] */
361     v->mb_type_base = av_malloc(s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2);
362     if (!v->mb_type_base)
363         goto error;
364     v->mb_type[0]   = v->mb_type_base + s->b8_stride + 1;
365     v->mb_type[1]   = v->mb_type_base + s->b8_stride * (mb_height * 2 + 1) + s->mb_stride + 1;
366     v->mb_type[2]   = v->mb_type[1] + s->mb_stride * (mb_height + 1);
367
368     /* allocate memory to store block level MV info */
369     v->blk_mv_type_base = av_mallocz(     s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2);
370     if (!v->blk_mv_type_base)
371         goto error;
372     v->blk_mv_type      = v->blk_mv_type_base + s->b8_stride + 1;
373     v->mv_f_base        = av_mallocz(2 * (s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2));
374     if (!v->mv_f_base)
375         goto error;
376     v->mv_f[0]          = v->mv_f_base + s->b8_stride + 1;
377     v->mv_f[1]          = v->mv_f[0] + (s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2);
378     v->mv_f_next_base   = av_mallocz(2 * (s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2));
379     if (!v->mv_f_next_base)
380         goto error;
381     v->mv_f_next[0]     = v->mv_f_next_base + s->b8_stride + 1;
382     v->mv_f_next[1]     = v->mv_f_next[0] + (s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2);
383
384     if (s->avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || s->avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
385         for (i = 0; i < 4; i++)
386             if (!(v->sr_rows[i >> 1][i & 1] = av_malloc(v->output_width)))
387                 goto error;
388     }
389
390     ret = ff_intrax8_common_init(s->avctx, &v->x8, &s->idsp,
391                                  s->block, s->block_last_index,
392                                  s->mb_width, s->mb_height);
393     if (ret < 0)
394         goto error;
395
396     return 0;
397
398 error:
399     ff_vc1_decode_end(s->avctx);
400     return ret;
401 }
402
403 av_cold void ff_vc1_init_transposed_scantables(VC1Context *v)
404 {
405     int i;
406     for (i = 0; i < 64; i++) {
407 #define transpose(x) (((x) >> 3) | (((x) & 7) << 3))
408         v->zz_8x8[0][i] = transpose(ff_wmv1_scantable[0][i]);
409         v->zz_8x8[1][i] = transpose(ff_wmv1_scantable[1][i]);
410         v->zz_8x8[2][i] = transpose(ff_wmv1_scantable[2][i]);
411         v->zz_8x8[3][i] = transpose(ff_wmv1_scantable[3][i]);
412         v->zzi_8x8[i]   = transpose(ff_vc1_adv_interlaced_8x8_zz[i]);
413     }
414     v->left_blk_sh = 0;
415     v->top_blk_sh  = 3;
416 }
417
418 /** Initialize a VC1/WMV3 decoder
419  * @todo TODO: Handle VC-1 IDUs (Transport level?)
420  * @todo TODO: Decipher remaining bits in extra_data
421  */
422 static av_cold int vc1_decode_init(AVCodecContext *avctx)
423 {
424     VC1Context *v = avctx->priv_data;
425     MpegEncContext *s = &v->s;
426     GetBitContext gb;
427     int ret;
428
429     /* save the container output size for WMImage */
430     v->output_width  = avctx->width;
431     v->output_height = avctx->height;
432
433     if (!avctx->extradata_size || !avctx->extradata)
434         return AVERROR_INVALIDDATA;
435     v->s.avctx = avctx;
436
437     ff_vc1_init_common(v);
438
439     if (avctx->codec_id == AV_CODEC_ID_WMV3 || avctx->codec_id == AV_CODEC_ID_WMV3IMAGE) {
440         int count = 0;
441
442         // looks like WMV3 has a sequence header stored in the extradata
443         // advanced sequence header may be before the first frame
444         // the last byte of the extradata is a version number, 1 for the
445         // samples we can decode
446
447         init_get_bits(&gb, avctx->extradata, avctx->extradata_size*8);
448
449         if ((ret = ff_vc1_decode_sequence_header(avctx, v, &gb)) < 0)
450           return ret;
451
452         if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE && !v->res_sprite) {
453             avpriv_request_sample(avctx, "Non sprite WMV3IMAGE");
454             return AVERROR_PATCHWELCOME;
455         }
456
457         count = avctx->extradata_size*8 - get_bits_count(&gb);
458         if (count > 0) {
459             av_log(avctx, AV_LOG_INFO, "Extra data: %i bits left, value: %X\n",
460                    count, get_bits_long(&gb, FFMIN(count, 32)));
461         } else if (count < 0) {
462             av_log(avctx, AV_LOG_INFO, "Read %i bits in overflow\n", -count);
463         }
464     } else { // VC1/WVC1/WVP2
465         const uint8_t *start = avctx->extradata;
466         uint8_t *end = avctx->extradata + avctx->extradata_size;
467         const uint8_t *next;
468         int size, buf2_size;
469         uint8_t *buf2 = NULL;
470         int seq_initialized = 0, ep_initialized = 0;
471
472         if (avctx->extradata_size < 16) {
473             av_log(avctx, AV_LOG_ERROR, "Extradata size too small: %i\n", avctx->extradata_size);
474             return AVERROR_INVALIDDATA;
475         }
476
477         buf2  = av_mallocz(avctx->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
478         if (!buf2)
479             return AVERROR(ENOMEM);
480
481         start = find_next_marker(start, end); // in WVC1 extradata first byte is its size, but can be 0 in mkv
482         next  = start;
483         for (; next < end; start = next) {
484             next = find_next_marker(start + 4, end);
485             size = next - start - 4;
486             if (size <= 0)
487                 continue;
488             buf2_size = vc1_unescape_buffer(start + 4, size, buf2);
489             init_get_bits(&gb, buf2, buf2_size * 8);
490             switch (AV_RB32(start)) {
491             case VC1_CODE_SEQHDR:
492                 if ((ret = ff_vc1_decode_sequence_header(avctx, v, &gb)) < 0) {
493                     av_free(buf2);
494                     return ret;
495                 }
496                 seq_initialized = 1;
497                 break;
498             case VC1_CODE_ENTRYPOINT:
499                 if ((ret = ff_vc1_decode_entry_point(avctx, v, &gb)) < 0) {
500                     av_free(buf2);
501                     return ret;
502                 }
503                 ep_initialized = 1;
504                 break;
505             }
506         }
507         av_free(buf2);
508         if (!seq_initialized || !ep_initialized) {
509             av_log(avctx, AV_LOG_ERROR, "Incomplete extradata\n");
510             return AVERROR_INVALIDDATA;
511         }
512         v->res_sprite = (avctx->codec_id == AV_CODEC_ID_VC1IMAGE);
513     }
514
515     avctx->profile = v->profile;
516     if (v->profile == PROFILE_ADVANCED)
517         avctx->level = v->level;
518
519     if (!CONFIG_GRAY || !(avctx->flags & AV_CODEC_FLAG_GRAY))
520         avctx->pix_fmt = ff_get_format(avctx, avctx->codec->pix_fmts);
521     else {
522         avctx->pix_fmt = AV_PIX_FMT_GRAY8;
523         if (avctx->color_range == AVCOL_RANGE_UNSPECIFIED)
524             avctx->color_range = AVCOL_RANGE_MPEG;
525     }
526
527     // ensure static VLC tables are initialized
528     if ((ret = ff_msmpeg4_decode_init(avctx)) < 0)
529         return ret;
530     if ((ret = ff_vc1_decode_init_alloc_tables(v)) < 0)
531         return ret;
532     // Hack to ensure the above functions will be called
533     // again once we know all necessary settings.
534     // That this is necessary might indicate a bug.
535     ff_vc1_decode_end(avctx);
536
537     ff_blockdsp_init(&s->bdsp, avctx);
538     ff_h264chroma_init(&v->h264chroma, 8);
539     ff_qpeldsp_init(&s->qdsp);
540
541     avctx->has_b_frames = !!avctx->max_b_frames;
542
543     if (v->color_prim == 1 || v->color_prim == 5 || v->color_prim == 6)
544         avctx->color_primaries = v->color_prim;
545     if (v->transfer_char == 1 || v->transfer_char == 7)
546         avctx->color_trc = v->transfer_char;
547     if (v->matrix_coef == 1 || v->matrix_coef == 6 || v->matrix_coef == 7)
548         avctx->colorspace = v->matrix_coef;
549
550     s->mb_width  = (avctx->coded_width  + 15) >> 4;
551     s->mb_height = (avctx->coded_height + 15) >> 4;
552
553     if (v->profile == PROFILE_ADVANCED || v->res_fasttx) {
554         ff_vc1_init_transposed_scantables(v);
555     } else {
556         memcpy(v->zz_8x8, ff_wmv1_scantable, 4*64);
557         v->left_blk_sh = 3;
558         v->top_blk_sh  = 0;
559     }
560
561     if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
562         v->sprite_width  = avctx->coded_width;
563         v->sprite_height = avctx->coded_height;
564
565         avctx->coded_width  = avctx->width  = v->output_width;
566         avctx->coded_height = avctx->height = v->output_height;
567
568         // prevent 16.16 overflows
569         if (v->sprite_width  > 1 << 14 ||
570             v->sprite_height > 1 << 14 ||
571             v->output_width  > 1 << 14 ||
572             v->output_height > 1 << 14) {
573             return AVERROR_INVALIDDATA;
574         }
575
576         if ((v->sprite_width&1) || (v->sprite_height&1)) {
577             avpriv_request_sample(avctx, "odd sprites support");
578             return AVERROR_PATCHWELCOME;
579         }
580     }
581     return 0;
582 }
583
584 /** Close a VC1/WMV3 decoder
585  * @warning Initial try at using MpegEncContext stuff
586  */
587 av_cold int ff_vc1_decode_end(AVCodecContext *avctx)
588 {
589     VC1Context *v = avctx->priv_data;
590     int i;
591
592     av_frame_free(&v->sprite_output_frame);
593
594     for (i = 0; i < 4; i++)
595         av_freep(&v->sr_rows[i >> 1][i & 1]);
596     av_freep(&v->hrd_rate);
597     av_freep(&v->hrd_buffer);
598     ff_mpv_common_end(&v->s);
599     av_freep(&v->mv_type_mb_plane);
600     av_freep(&v->direct_mb_plane);
601     av_freep(&v->forward_mb_plane);
602     av_freep(&v->fieldtx_plane);
603     av_freep(&v->acpred_plane);
604     av_freep(&v->over_flags_plane);
605     av_freep(&v->mb_type_base);
606     av_freep(&v->blk_mv_type_base);
607     av_freep(&v->mv_f_base);
608     av_freep(&v->mv_f_next_base);
609     av_freep(&v->block);
610     av_freep(&v->cbp_base);
611     av_freep(&v->ttblk_base);
612     av_freep(&v->is_intra_base); // FIXME use v->mb_type[]
613     av_freep(&v->luma_mv_base);
614     ff_intrax8_common_end(&v->x8);
615     return 0;
616 }
617
618
619 /** Decode a VC1/WMV3 frame
620  * @todo TODO: Handle VC-1 IDUs (Transport level?)
621  */
622 static int vc1_decode_frame(AVCodecContext *avctx, void *data,
623                             int *got_frame, AVPacket *avpkt)
624 {
625     const uint8_t *buf = avpkt->data;
626     int buf_size = avpkt->size, n_slices = 0, i, ret;
627     VC1Context *v = avctx->priv_data;
628     MpegEncContext *s = &v->s;
629     AVFrame *pict = data;
630     uint8_t *buf2 = NULL;
631     const uint8_t *buf_start = buf, *buf_start_second_field = NULL;
632     int mb_height, n_slices1=-1;
633     struct {
634         uint8_t *buf;
635         GetBitContext gb;
636         int mby_start;
637         const uint8_t *rawbuf;
638         int raw_size;
639     } *slices = NULL, *tmp;
640
641     v->second_field = 0;
642
643     if(s->avctx->flags & AV_CODEC_FLAG_LOW_DELAY)
644         s->low_delay = 1;
645
646     /* no supplementary picture */
647     if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == VC1_CODE_ENDOFSEQ)) {
648         /* special case for last picture */
649         if (s->low_delay == 0 && s->next_picture_ptr) {
650             if ((ret = av_frame_ref(pict, s->next_picture_ptr->f)) < 0)
651                 return ret;
652             s->next_picture_ptr = NULL;
653
654             *got_frame = 1;
655         }
656
657         return buf_size;
658     }
659
660     //for advanced profile we may need to parse and unescape data
661     if (avctx->codec_id == AV_CODEC_ID_VC1 || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
662         int buf_size2 = 0;
663         buf2 = av_mallocz(buf_size + AV_INPUT_BUFFER_PADDING_SIZE);
664         if (!buf2)
665             return AVERROR(ENOMEM);
666
667         if (IS_MARKER(AV_RB32(buf))) { /* frame starts with marker and needs to be parsed */
668             const uint8_t *start, *end, *next;
669             int size;
670
671             next = buf;
672             for (start = buf, end = buf + buf_size; next < end; start = next) {
673                 next = find_next_marker(start + 4, end);
674                 size = next - start - 4;
675                 if (size <= 0) continue;
676                 switch (AV_RB32(start)) {
677                 case VC1_CODE_FRAME:
678                     if (avctx->hwaccel)
679                         buf_start = start;
680                     buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
681                     break;
682                 case VC1_CODE_FIELD: {
683                     int buf_size3;
684                     if (avctx->hwaccel)
685                         buf_start_second_field = start;
686                     tmp = av_realloc_array(slices, sizeof(*slices), n_slices+1);
687                     if (!tmp) {
688                         ret = AVERROR(ENOMEM);
689                         goto err;
690                     }
691                     slices = tmp;
692                     slices[n_slices].buf = av_mallocz(size + AV_INPUT_BUFFER_PADDING_SIZE);
693                     if (!slices[n_slices].buf) {
694                         ret = AVERROR(ENOMEM);
695                         goto err;
696                     }
697                     buf_size3 = vc1_unescape_buffer(start + 4, size,
698                                                     slices[n_slices].buf);
699                     init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
700                                   buf_size3 << 3);
701                     slices[n_slices].mby_start = avctx->coded_height + 31 >> 5;
702                     slices[n_slices].rawbuf = start;
703                     slices[n_slices].raw_size = size + 4;
704                     n_slices1 = n_slices - 1; // index of the last slice of the first field
705                     n_slices++;
706                     break;
707                 }
708                 case VC1_CODE_ENTRYPOINT: /* it should be before frame data */
709                     buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
710                     init_get_bits(&s->gb, buf2, buf_size2 * 8);
711                     ff_vc1_decode_entry_point(avctx, v, &s->gb);
712                     break;
713                 case VC1_CODE_SLICE: {
714                     int buf_size3;
715                     tmp = av_realloc_array(slices, sizeof(*slices), n_slices+1);
716                     if (!tmp) {
717                         ret = AVERROR(ENOMEM);
718                         goto err;
719                     }
720                     slices = tmp;
721                     slices[n_slices].buf = av_mallocz(size + AV_INPUT_BUFFER_PADDING_SIZE);
722                     if (!slices[n_slices].buf) {
723                         ret = AVERROR(ENOMEM);
724                         goto err;
725                     }
726                     buf_size3 = vc1_unescape_buffer(start + 4, size,
727                                                     slices[n_slices].buf);
728                     init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
729                                   buf_size3 << 3);
730                     slices[n_slices].mby_start = get_bits(&slices[n_slices].gb, 9);
731                     slices[n_slices].rawbuf = start;
732                     slices[n_slices].raw_size = size + 4;
733                     n_slices++;
734                     break;
735                 }
736                 }
737             }
738         } else if (v->interlace && ((buf[0] & 0xC0) == 0xC0)) { /* WVC1 interlaced stores both fields divided by marker */
739             const uint8_t *divider;
740             int buf_size3;
741
742             divider = find_next_marker(buf, buf + buf_size);
743             if ((divider == (buf + buf_size)) || AV_RB32(divider) != VC1_CODE_FIELD) {
744                 av_log(avctx, AV_LOG_ERROR, "Error in WVC1 interlaced frame\n");
745                 ret = AVERROR_INVALIDDATA;
746                 goto err;
747             } else { // found field marker, unescape second field
748                 if (avctx->hwaccel)
749                     buf_start_second_field = divider;
750                 tmp = av_realloc_array(slices, sizeof(*slices), n_slices+1);
751                 if (!tmp) {
752                     ret = AVERROR(ENOMEM);
753                     goto err;
754                 }
755                 slices = tmp;
756                 slices[n_slices].buf = av_mallocz(buf_size + AV_INPUT_BUFFER_PADDING_SIZE);
757                 if (!slices[n_slices].buf) {
758                     ret = AVERROR(ENOMEM);
759                     goto err;
760                 }
761                 buf_size3 = vc1_unescape_buffer(divider + 4, buf + buf_size - divider - 4, slices[n_slices].buf);
762                 init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
763                               buf_size3 << 3);
764                 slices[n_slices].mby_start = s->mb_height + 1 >> 1;
765                 slices[n_slices].rawbuf = divider;
766                 slices[n_slices].raw_size = buf + buf_size - divider;
767                 n_slices1 = n_slices - 1;
768                 n_slices++;
769             }
770             buf_size2 = vc1_unescape_buffer(buf, divider - buf, buf2);
771         } else {
772             buf_size2 = vc1_unescape_buffer(buf, buf_size, buf2);
773         }
774         init_get_bits(&s->gb, buf2, buf_size2*8);
775     } else
776         init_get_bits(&s->gb, buf, buf_size*8);
777
778     if (v->res_sprite) {
779         v->new_sprite  = !get_bits1(&s->gb);
780         v->two_sprites =  get_bits1(&s->gb);
781         /* res_sprite means a Windows Media Image stream, AV_CODEC_ID_*IMAGE means
782            we're using the sprite compositor. These are intentionally kept separate
783            so you can get the raw sprites by using the wmv3 decoder for WMVP or
784            the vc1 one for WVP2 */
785         if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
786             if (v->new_sprite) {
787                 // switch AVCodecContext parameters to those of the sprites
788                 avctx->width  = avctx->coded_width  = v->sprite_width;
789                 avctx->height = avctx->coded_height = v->sprite_height;
790             } else {
791                 goto image;
792             }
793         }
794     }
795
796     if (s->context_initialized &&
797         (s->width  != avctx->coded_width ||
798          s->height != avctx->coded_height)) {
799         ff_vc1_decode_end(avctx);
800     }
801
802     if (!s->context_initialized) {
803         if ((ret = ff_msmpeg4_decode_init(avctx)) < 0)
804             goto err;
805         if ((ret = ff_vc1_decode_init_alloc_tables(v)) < 0) {
806             ff_mpv_common_end(s);
807             goto err;
808         }
809
810         s->low_delay = !avctx->has_b_frames || v->res_sprite;
811
812         if (v->profile == PROFILE_ADVANCED) {
813             if(avctx->coded_width<=1 || avctx->coded_height<=1) {
814                 ret = AVERROR_INVALIDDATA;
815                 goto err;
816             }
817             s->h_edge_pos = avctx->coded_width;
818             s->v_edge_pos = avctx->coded_height;
819         }
820     }
821
822     // do parse frame header
823     v->pic_header_flag = 0;
824     v->first_pic_header_flag = 1;
825     if (v->profile < PROFILE_ADVANCED) {
826         if ((ret = ff_vc1_parse_frame_header(v, &s->gb)) < 0) {
827             goto err;
828         }
829     } else {
830         if ((ret = ff_vc1_parse_frame_header_adv(v, &s->gb)) < 0) {
831             goto err;
832         }
833     }
834     v->first_pic_header_flag = 0;
835
836     if (avctx->debug & FF_DEBUG_PICT_INFO)
837         av_log(v->s.avctx, AV_LOG_DEBUG, "pict_type: %c\n", av_get_picture_type_char(s->pict_type));
838
839     if ((avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE)
840         && s->pict_type != AV_PICTURE_TYPE_I) {
841         av_log(v->s.avctx, AV_LOG_ERROR, "Sprite decoder: expected I-frame\n");
842         ret = AVERROR_INVALIDDATA;
843         goto err;
844     }
845     if ((avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE)
846         && v->field_mode) {
847         av_log(v->s.avctx, AV_LOG_ERROR, "Sprite decoder: expected Frames not Fields\n");
848         ret = AVERROR_INVALIDDATA;
849         goto err;
850     }
851     if ((s->mb_height >> v->field_mode) == 0) {
852         av_log(v->s.avctx, AV_LOG_ERROR, "image too short\n");
853         ret = AVERROR_INVALIDDATA;
854         goto err;
855     }
856
857     // for skipping the frame
858     s->current_picture.f->pict_type = s->pict_type;
859     s->current_picture.f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
860
861     /* skip B-frames if we don't have reference frames */
862     if (!s->last_picture_ptr && (s->pict_type == AV_PICTURE_TYPE_B || s->droppable)) {
863         av_log(v->s.avctx, AV_LOG_DEBUG, "Skipping B frame without reference frames\n");
864         goto end;
865     }
866     if ((avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B) ||
867         (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I) ||
868          avctx->skip_frame >= AVDISCARD_ALL) {
869         goto end;
870     }
871
872     if (s->next_p_frame_damaged) {
873         if (s->pict_type == AV_PICTURE_TYPE_B)
874             goto end;
875         else
876             s->next_p_frame_damaged = 0;
877     }
878
879     if ((ret = ff_mpv_frame_start(s, avctx)) < 0) {
880         goto err;
881     }
882
883     v->s.current_picture_ptr->field_picture = v->field_mode;
884     v->s.current_picture_ptr->f->interlaced_frame = (v->fcm != PROGRESSIVE);
885     v->s.current_picture_ptr->f->top_field_first  = v->tff;
886
887     // process pulldown flags
888     s->current_picture_ptr->f->repeat_pict = 0;
889     // Pulldown flags are only valid when 'broadcast' has been set.
890     // So ticks_per_frame will be 2
891     if (v->rff) {
892         // repeat field
893         s->current_picture_ptr->f->repeat_pict = 1;
894     } else if (v->rptfrm) {
895         // repeat frames
896         s->current_picture_ptr->f->repeat_pict = v->rptfrm * 2;
897     }
898
899     s->me.qpel_put = s->qdsp.put_qpel_pixels_tab;
900     s->me.qpel_avg = s->qdsp.avg_qpel_pixels_tab;
901
902     if (avctx->hwaccel) {
903         s->mb_y = 0;
904         if (v->field_mode && buf_start_second_field) {
905             // decode first field
906             s->picture_structure = PICT_BOTTOM_FIELD - v->tff;
907             if ((ret = avctx->hwaccel->start_frame(avctx, buf_start, buf_start_second_field - buf_start)) < 0)
908                 goto err;
909
910             if (n_slices1 == -1) {
911                 // no slices, decode the field as-is
912                 if ((ret = avctx->hwaccel->decode_slice(avctx, buf_start, buf_start_second_field - buf_start)) < 0)
913                     goto err;
914             } else {
915                 if ((ret = avctx->hwaccel->decode_slice(avctx, buf_start, slices[0].rawbuf - buf_start)) < 0)
916                     goto err;
917
918                 for (i = 0 ; i < n_slices1 + 1; i++) {
919                     s->gb = slices[i].gb;
920                     s->mb_y = slices[i].mby_start;
921
922                     v->pic_header_flag = get_bits1(&s->gb);
923                     if (v->pic_header_flag) {
924                         if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
925                             av_log(v->s.avctx, AV_LOG_ERROR, "Slice header damaged\n");
926                             ret = AVERROR_INVALIDDATA;
927                             if (avctx->err_recognition & AV_EF_EXPLODE)
928                                 goto err;
929                             continue;
930                         }
931                     }
932
933                     if ((ret = avctx->hwaccel->decode_slice(avctx, slices[i].rawbuf, slices[i].raw_size)) < 0)
934                         goto err;
935                 }
936             }
937
938             if ((ret = avctx->hwaccel->end_frame(avctx)) < 0)
939                 goto err;
940
941             // decode second field
942             s->gb = slices[n_slices1 + 1].gb;
943             s->mb_y = slices[n_slices1 + 1].mby_start;
944             s->picture_structure = PICT_TOP_FIELD + v->tff;
945             v->second_field = 1;
946             v->pic_header_flag = 0;
947             if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
948                 av_log(avctx, AV_LOG_ERROR, "parsing header for second field failed");
949                 ret = AVERROR_INVALIDDATA;
950                 goto err;
951             }
952             v->s.current_picture_ptr->f->pict_type = v->s.pict_type;
953
954             if ((ret = avctx->hwaccel->start_frame(avctx, buf_start_second_field, (buf + buf_size) - buf_start_second_field)) < 0)
955                 goto err;
956
957             if (n_slices - n_slices1 == 2) {
958                 // no slices, decode the field as-is
959                 if ((ret = avctx->hwaccel->decode_slice(avctx, buf_start_second_field, (buf + buf_size) - buf_start_second_field)) < 0)
960                     goto err;
961             } else {
962                 if ((ret = avctx->hwaccel->decode_slice(avctx, buf_start_second_field, slices[n_slices1 + 2].rawbuf - buf_start_second_field)) < 0)
963                     goto err;
964
965                 for (i = n_slices1 + 2; i < n_slices; i++) {
966                     s->gb = slices[i].gb;
967                     s->mb_y = slices[i].mby_start;
968
969                     v->pic_header_flag = get_bits1(&s->gb);
970                     if (v->pic_header_flag) {
971                         if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
972                             av_log(v->s.avctx, AV_LOG_ERROR, "Slice header damaged\n");
973                             ret = AVERROR_INVALIDDATA;
974                             if (avctx->err_recognition & AV_EF_EXPLODE)
975                                 goto err;
976                             continue;
977                         }
978                     }
979
980                     if ((ret = avctx->hwaccel->decode_slice(avctx, slices[i].rawbuf, slices[i].raw_size)) < 0)
981                         goto err;
982                 }
983             }
984
985             if ((ret = avctx->hwaccel->end_frame(avctx)) < 0)
986                 goto err;
987         } else {
988             s->picture_structure = PICT_FRAME;
989             if ((ret = avctx->hwaccel->start_frame(avctx, buf_start, (buf + buf_size) - buf_start)) < 0)
990                 goto err;
991
992             if (n_slices == 0) {
993                 // no slices, decode the frame as-is
994                 if ((ret = avctx->hwaccel->decode_slice(avctx, buf_start, (buf + buf_size) - buf_start)) < 0)
995                     goto err;
996             } else {
997                 // decode the frame part as the first slice
998                 if ((ret = avctx->hwaccel->decode_slice(avctx, buf_start, slices[0].rawbuf - buf_start)) < 0)
999                     goto err;
1000
1001                 // and process the slices as additional slices afterwards
1002                 for (i = 0 ; i < n_slices; i++) {
1003                     s->gb = slices[i].gb;
1004                     s->mb_y = slices[i].mby_start;
1005
1006                     v->pic_header_flag = get_bits1(&s->gb);
1007                     if (v->pic_header_flag) {
1008                         if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
1009                             av_log(v->s.avctx, AV_LOG_ERROR, "Slice header damaged\n");
1010                             ret = AVERROR_INVALIDDATA;
1011                             if (avctx->err_recognition & AV_EF_EXPLODE)
1012                                 goto err;
1013                             continue;
1014                         }
1015                     }
1016
1017                     if ((ret = avctx->hwaccel->decode_slice(avctx, slices[i].rawbuf, slices[i].raw_size)) < 0)
1018                         goto err;
1019                 }
1020             }
1021             if ((ret = avctx->hwaccel->end_frame(avctx)) < 0)
1022                 goto err;
1023         }
1024     } else {
1025         int header_ret = 0;
1026
1027         ff_mpeg_er_frame_start(s);
1028
1029         v->end_mb_x = s->mb_width;
1030         if (v->field_mode) {
1031             s->current_picture.f->linesize[0] <<= 1;
1032             s->current_picture.f->linesize[1] <<= 1;
1033             s->current_picture.f->linesize[2] <<= 1;
1034             s->linesize                      <<= 1;
1035             s->uvlinesize                    <<= 1;
1036         }
1037         mb_height = s->mb_height >> v->field_mode;
1038
1039         av_assert0 (mb_height > 0);
1040
1041         for (i = 0; i <= n_slices; i++) {
1042             if (i > 0 &&  slices[i - 1].mby_start >= mb_height) {
1043                 if (v->field_mode <= 0) {
1044                     av_log(v->s.avctx, AV_LOG_ERROR, "Slice %d starts beyond "
1045                            "picture boundary (%d >= %d)\n", i,
1046                            slices[i - 1].mby_start, mb_height);
1047                     continue;
1048                 }
1049                 v->second_field = 1;
1050                 av_assert0((s->mb_height & 1) == 0);
1051                 v->blocks_off   = s->b8_stride * (s->mb_height&~1);
1052                 v->mb_off       = s->mb_stride * s->mb_height >> 1;
1053             } else {
1054                 v->second_field = 0;
1055                 v->blocks_off   = 0;
1056                 v->mb_off       = 0;
1057             }
1058             if (i) {
1059                 v->pic_header_flag = 0;
1060                 if (v->field_mode && i == n_slices1 + 2) {
1061                     if ((header_ret = ff_vc1_parse_frame_header_adv(v, &s->gb)) < 0) {
1062                         av_log(v->s.avctx, AV_LOG_ERROR, "Field header damaged\n");
1063                         ret = AVERROR_INVALIDDATA;
1064                         if (avctx->err_recognition & AV_EF_EXPLODE)
1065                             goto err;
1066                         continue;
1067                     }
1068                 } else if (get_bits1(&s->gb)) {
1069                     v->pic_header_flag = 1;
1070                     if ((header_ret = ff_vc1_parse_frame_header_adv(v, &s->gb)) < 0) {
1071                         av_log(v->s.avctx, AV_LOG_ERROR, "Slice header damaged\n");
1072                         ret = AVERROR_INVALIDDATA;
1073                         if (avctx->err_recognition & AV_EF_EXPLODE)
1074                             goto err;
1075                         continue;
1076                     }
1077                 }
1078             }
1079             if (header_ret < 0)
1080                 continue;
1081             s->start_mb_y = (i == 0) ? 0 : FFMAX(0, slices[i-1].mby_start % mb_height);
1082             if (!v->field_mode || v->second_field)
1083                 s->end_mb_y = (i == n_slices     ) ? mb_height : FFMIN(mb_height, slices[i].mby_start % mb_height);
1084             else {
1085                 if (i >= n_slices) {
1086                     av_log(v->s.avctx, AV_LOG_ERROR, "first field slice count too large\n");
1087                     continue;
1088                 }
1089                 s->end_mb_y = (i == n_slices1 + 1) ? mb_height : FFMIN(mb_height, slices[i].mby_start % mb_height);
1090             }
1091             if (s->end_mb_y <= s->start_mb_y) {
1092                 av_log(v->s.avctx, AV_LOG_ERROR, "end mb y %d %d invalid\n", s->end_mb_y, s->start_mb_y);
1093                 continue;
1094             }
1095             if (((s->pict_type == AV_PICTURE_TYPE_P && !v->p_frame_skipped) ||
1096                  (s->pict_type == AV_PICTURE_TYPE_B && !v->bi_type)) &&
1097                 !v->cbpcy_vlc) {
1098                 av_log(v->s.avctx, AV_LOG_ERROR, "missing cbpcy_vlc\n");
1099                 continue;
1100             }
1101             ff_vc1_decode_blocks(v);
1102             if (i != n_slices) {
1103                 s->gb = slices[i].gb;
1104             }
1105         }
1106         if (v->field_mode) {
1107             v->second_field = 0;
1108             s->current_picture.f->linesize[0] >>= 1;
1109             s->current_picture.f->linesize[1] >>= 1;
1110             s->current_picture.f->linesize[2] >>= 1;
1111             s->linesize                      >>= 1;
1112             s->uvlinesize                    >>= 1;
1113             if (v->s.pict_type != AV_PICTURE_TYPE_BI && v->s.pict_type != AV_PICTURE_TYPE_B) {
1114                 FFSWAP(uint8_t *, v->mv_f_next[0], v->mv_f[0]);
1115                 FFSWAP(uint8_t *, v->mv_f_next[1], v->mv_f[1]);
1116             }
1117         }
1118         ff_dlog(s->avctx, "Consumed %i/%i bits\n",
1119                 get_bits_count(&s->gb), s->gb.size_in_bits);
1120 //  if (get_bits_count(&s->gb) > buf_size * 8)
1121 //      return -1;
1122         if(s->er.error_occurred && s->pict_type == AV_PICTURE_TYPE_B) {
1123             ret = AVERROR_INVALIDDATA;
1124             goto err;
1125         }
1126         if (!v->field_mode)
1127             ff_er_frame_end(&s->er);
1128     }
1129
1130     ff_mpv_frame_end(s);
1131
1132     if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
1133 image:
1134         avctx->width  = avctx->coded_width  = v->output_width;
1135         avctx->height = avctx->coded_height = v->output_height;
1136         if (avctx->skip_frame >= AVDISCARD_NONREF)
1137             goto end;
1138         if (!v->sprite_output_frame &&
1139             !(v->sprite_output_frame = av_frame_alloc())) {
1140             ret = AVERROR(ENOMEM);
1141             goto err;
1142         }
1143 #if CONFIG_WMV3IMAGE_DECODER || CONFIG_VC1IMAGE_DECODER
1144         if ((ret = vc1_decode_sprites(v, &s->gb)) < 0)
1145             goto err;
1146 #endif
1147         if ((ret = av_frame_ref(pict, v->sprite_output_frame)) < 0)
1148             goto err;
1149         *got_frame = 1;
1150     } else {
1151         if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
1152             if ((ret = av_frame_ref(pict, s->current_picture_ptr->f)) < 0)
1153                 goto err;
1154             ff_print_debug_info(s, s->current_picture_ptr, pict);
1155             *got_frame = 1;
1156         } else if (s->last_picture_ptr) {
1157             if ((ret = av_frame_ref(pict, s->last_picture_ptr->f)) < 0)
1158                 goto err;
1159             ff_print_debug_info(s, s->last_picture_ptr, pict);
1160             *got_frame = 1;
1161         }
1162     }
1163
1164 end:
1165     av_free(buf2);
1166     for (i = 0; i < n_slices; i++)
1167         av_free(slices[i].buf);
1168     av_free(slices);
1169     return buf_size;
1170
1171 err:
1172     av_free(buf2);
1173     for (i = 0; i < n_slices; i++)
1174         av_free(slices[i].buf);
1175     av_free(slices);
1176     return ret;
1177 }
1178
1179
1180 static const enum AVPixelFormat vc1_hwaccel_pixfmt_list_420[] = {
1181 #if CONFIG_VC1_DXVA2_HWACCEL
1182     AV_PIX_FMT_DXVA2_VLD,
1183 #endif
1184 #if CONFIG_VC1_D3D11VA_HWACCEL
1185     AV_PIX_FMT_D3D11VA_VLD,
1186     AV_PIX_FMT_D3D11,
1187 #endif
1188 #if CONFIG_VC1_NVDEC_HWACCEL
1189     AV_PIX_FMT_CUDA,
1190 #endif
1191 #if CONFIG_VC1_VAAPI_HWACCEL
1192     AV_PIX_FMT_VAAPI,
1193 #endif
1194 #if CONFIG_VC1_VDPAU_HWACCEL
1195     AV_PIX_FMT_VDPAU,
1196 #endif
1197     AV_PIX_FMT_YUV420P,
1198     AV_PIX_FMT_NONE
1199 };
1200
1201 AVCodec ff_vc1_decoder = {
1202     .name           = "vc1",
1203     .long_name      = NULL_IF_CONFIG_SMALL("SMPTE VC-1"),
1204     .type           = AVMEDIA_TYPE_VIDEO,
1205     .id             = AV_CODEC_ID_VC1,
1206     .priv_data_size = sizeof(VC1Context),
1207     .init           = vc1_decode_init,
1208     .close          = ff_vc1_decode_end,
1209     .decode         = vc1_decode_frame,
1210     .flush          = ff_mpeg_flush,
1211     .capabilities   = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY,
1212     .pix_fmts       = vc1_hwaccel_pixfmt_list_420,
1213     .hw_configs     = (const AVCodecHWConfigInternal *const []) {
1214 #if CONFIG_VC1_DXVA2_HWACCEL
1215                         HWACCEL_DXVA2(vc1),
1216 #endif
1217 #if CONFIG_VC1_D3D11VA_HWACCEL
1218                         HWACCEL_D3D11VA(vc1),
1219 #endif
1220 #if CONFIG_VC1_D3D11VA2_HWACCEL
1221                         HWACCEL_D3D11VA2(vc1),
1222 #endif
1223 #if CONFIG_VC1_NVDEC_HWACCEL
1224                         HWACCEL_NVDEC(vc1),
1225 #endif
1226 #if CONFIG_VC1_VAAPI_HWACCEL
1227                         HWACCEL_VAAPI(vc1),
1228 #endif
1229 #if CONFIG_VC1_VDPAU_HWACCEL
1230                         HWACCEL_VDPAU(vc1),
1231 #endif
1232                         NULL
1233                     },
1234     .profiles       = NULL_IF_CONFIG_SMALL(ff_vc1_profiles)
1235 };
1236
1237 #if CONFIG_WMV3_DECODER
1238 AVCodec ff_wmv3_decoder = {
1239     .name           = "wmv3",
1240     .long_name      = NULL_IF_CONFIG_SMALL("Windows Media Video 9"),
1241     .type           = AVMEDIA_TYPE_VIDEO,
1242     .id             = AV_CODEC_ID_WMV3,
1243     .priv_data_size = sizeof(VC1Context),
1244     .init           = vc1_decode_init,
1245     .close          = ff_vc1_decode_end,
1246     .decode         = vc1_decode_frame,
1247     .flush          = ff_mpeg_flush,
1248     .capabilities   = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY,
1249     .pix_fmts       = vc1_hwaccel_pixfmt_list_420,
1250     .hw_configs     = (const AVCodecHWConfigInternal *const []) {
1251 #if CONFIG_WMV3_DXVA2_HWACCEL
1252                         HWACCEL_DXVA2(wmv3),
1253 #endif
1254 #if CONFIG_WMV3_D3D11VA_HWACCEL
1255                         HWACCEL_D3D11VA(wmv3),
1256 #endif
1257 #if CONFIG_WMV3_D3D11VA2_HWACCEL
1258                         HWACCEL_D3D11VA2(wmv3),
1259 #endif
1260 #if CONFIG_WMV3_NVDEC_HWACCEL
1261                         HWACCEL_NVDEC(wmv3),
1262 #endif
1263 #if CONFIG_WMV3_VAAPI_HWACCEL
1264                         HWACCEL_VAAPI(wmv3),
1265 #endif
1266 #if CONFIG_WMV3_VDPAU_HWACCEL
1267                         HWACCEL_VDPAU(wmv3),
1268 #endif
1269                         NULL
1270                     },
1271     .profiles       = NULL_IF_CONFIG_SMALL(ff_vc1_profiles)
1272 };
1273 #endif
1274
1275 #if CONFIG_WMV3IMAGE_DECODER
1276 AVCodec ff_wmv3image_decoder = {
1277     .name           = "wmv3image",
1278     .long_name      = NULL_IF_CONFIG_SMALL("Windows Media Video 9 Image"),
1279     .type           = AVMEDIA_TYPE_VIDEO,
1280     .id             = AV_CODEC_ID_WMV3IMAGE,
1281     .priv_data_size = sizeof(VC1Context),
1282     .init           = vc1_decode_init,
1283     .close          = ff_vc1_decode_end,
1284     .decode         = vc1_decode_frame,
1285     .capabilities   = AV_CODEC_CAP_DR1,
1286     .flush          = vc1_sprite_flush,
1287     .pix_fmts       = (const enum AVPixelFormat[]) {
1288         AV_PIX_FMT_YUV420P,
1289         AV_PIX_FMT_NONE
1290     },
1291 };
1292 #endif
1293
1294 #if CONFIG_VC1IMAGE_DECODER
1295 AVCodec ff_vc1image_decoder = {
1296     .name           = "vc1image",
1297     .long_name      = NULL_IF_CONFIG_SMALL("Windows Media Video 9 Image v2"),
1298     .type           = AVMEDIA_TYPE_VIDEO,
1299     .id             = AV_CODEC_ID_VC1IMAGE,
1300     .priv_data_size = sizeof(VC1Context),
1301     .init           = vc1_decode_init,
1302     .close          = ff_vc1_decode_end,
1303     .decode         = vc1_decode_frame,
1304     .capabilities   = AV_CODEC_CAP_DR1,
1305     .flush          = vc1_sprite_flush,
1306     .pix_fmts       = (const enum AVPixelFormat[]) {
1307         AV_PIX_FMT_YUV420P,
1308         AV_PIX_FMT_NONE
1309     },
1310 };
1311 #endif