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