/src/ffmpeg/libavcodec/vc1dec.c
Line | Count | Source |
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 "config_components.h" |
30 | | |
31 | | #include "avcodec.h" |
32 | | #include "blockdsp.h" |
33 | | #include "codec_internal.h" |
34 | | #include "decode.h" |
35 | | #include "get_bits.h" |
36 | | #include "hwaccel_internal.h" |
37 | | #include "hwconfig.h" |
38 | | #include "mpeg_er.h" |
39 | | #include "mpegutils.h" |
40 | | #include "mpegvideo.h" |
41 | | #include "mpegvideodec.h" |
42 | | #include "msmpeg4_vc1_data.h" |
43 | | #include "profiles.h" |
44 | | #include "simple_idct.h" |
45 | | #include "vc1.h" |
46 | | #include "vc1data.h" |
47 | | #include "vc1_vlc_data.h" |
48 | | #include "libavutil/attributes.h" |
49 | | #include "libavutil/avassert.h" |
50 | | #include "libavutil/imgutils.h" |
51 | | #include "libavutil/mem.h" |
52 | | #include "libavutil/thread.h" |
53 | | |
54 | | |
55 | | static const enum AVPixelFormat vc1_hwaccel_pixfmt_list_420[] = { |
56 | | #if CONFIG_VC1_DXVA2_HWACCEL |
57 | | AV_PIX_FMT_DXVA2_VLD, |
58 | | #endif |
59 | | #if CONFIG_VC1_D3D11VA_HWACCEL |
60 | | AV_PIX_FMT_D3D11VA_VLD, |
61 | | AV_PIX_FMT_D3D11, |
62 | | #endif |
63 | | #if CONFIG_VC1_D3D12VA_HWACCEL |
64 | | AV_PIX_FMT_D3D12, |
65 | | #endif |
66 | | #if CONFIG_VC1_NVDEC_HWACCEL |
67 | | AV_PIX_FMT_CUDA, |
68 | | #endif |
69 | | #if CONFIG_VC1_VAAPI_HWACCEL |
70 | | AV_PIX_FMT_VAAPI, |
71 | | #endif |
72 | | #if CONFIG_VC1_VDPAU_HWACCEL |
73 | | AV_PIX_FMT_VDPAU, |
74 | | #endif |
75 | | AV_PIX_FMT_YUV420P, |
76 | | AV_PIX_FMT_NONE |
77 | | }; |
78 | | |
79 | | #if CONFIG_WMV3IMAGE_DECODER || CONFIG_VC1IMAGE_DECODER |
80 | | |
81 | | typedef struct SpriteData { |
82 | | /** |
83 | | * Transform coefficients for both sprites in 16.16 fixed point format, |
84 | | * in the order they appear in the bitstream: |
85 | | * x scale |
86 | | * rotation 1 (unused) |
87 | | * x offset |
88 | | * rotation 2 (unused) |
89 | | * y scale |
90 | | * y offset |
91 | | * alpha |
92 | | */ |
93 | | int coefs[2][7]; |
94 | | |
95 | | int effect_type, effect_flag; |
96 | | int effect_pcount1, effect_pcount2; ///< amount of effect parameters stored in effect_params |
97 | | int effect_params1[15], effect_params2[10]; ///< effect parameters in 16.16 fixed point format |
98 | | } SpriteData; |
99 | | |
100 | | static inline int get_fp_val(GetBitContext* gb) |
101 | 1.04M | { |
102 | 1.04M | return (get_bits_long(gb, 30) - (1 << 29)) << 1; |
103 | 1.04M | } |
104 | | |
105 | | static void vc1_sprite_parse_transform(GetBitContext* gb, int c[7]) |
106 | 248k | { |
107 | 248k | c[1] = c[3] = 0; |
108 | | |
109 | 248k | switch (get_bits(gb, 2)) { |
110 | 129k | case 0: |
111 | 129k | c[0] = 1 << 16; |
112 | 129k | c[2] = get_fp_val(gb); |
113 | 129k | c[4] = 1 << 16; |
114 | 129k | break; |
115 | 39.4k | case 1: |
116 | 39.4k | c[0] = c[4] = get_fp_val(gb); |
117 | 39.4k | c[2] = get_fp_val(gb); |
118 | 39.4k | break; |
119 | 12.4k | case 2: |
120 | 12.4k | c[0] = get_fp_val(gb); |
121 | 12.4k | c[2] = get_fp_val(gb); |
122 | 12.4k | c[4] = get_fp_val(gb); |
123 | 12.4k | break; |
124 | 66.4k | case 3: |
125 | 66.4k | c[0] = get_fp_val(gb); |
126 | 66.4k | c[1] = get_fp_val(gb); |
127 | 66.4k | c[2] = get_fp_val(gb); |
128 | 66.4k | c[3] = get_fp_val(gb); |
129 | 66.4k | c[4] = get_fp_val(gb); |
130 | 66.4k | break; |
131 | 248k | } |
132 | 248k | c[5] = get_fp_val(gb); |
133 | 248k | if (get_bits1(gb)) |
134 | 24.8k | c[6] = get_fp_val(gb); |
135 | 223k | else |
136 | 223k | c[6] = 1 << 16; |
137 | 248k | } |
138 | | |
139 | | static int vc1_parse_sprites(VC1Context *v, GetBitContext* gb, SpriteData* sd) |
140 | 208k | { |
141 | 208k | AVCodecContext *avctx = v->s.avctx; |
142 | 208k | int sprite, i; |
143 | | |
144 | 450k | for (sprite = 0; sprite <= v->two_sprites; sprite++) { |
145 | 241k | vc1_sprite_parse_transform(gb, sd->coefs[sprite]); |
146 | 241k | if (sd->coefs[sprite][1] || sd->coefs[sprite][3]) |
147 | 63.4k | avpriv_request_sample(avctx, "Non-zero rotation coefficients"); |
148 | 241k | av_log(avctx, AV_LOG_DEBUG, sprite ? "S2:" : "S1:"); |
149 | 1.93M | for (i = 0; i < 7; i++) |
150 | 1.69M | av_log(avctx, AV_LOG_DEBUG, " %d.%.3d", |
151 | 1.69M | sd->coefs[sprite][i] / (1<<16), |
152 | 1.69M | (abs(sd->coefs[sprite][i]) & 0xFFFF) * 1000 / (1 << 16)); |
153 | 241k | av_log(avctx, AV_LOG_DEBUG, "\n"); |
154 | 241k | } |
155 | | |
156 | 208k | skip_bits(gb, 2); |
157 | 208k | if (sd->effect_type = get_bits_long(gb, 30)) { |
158 | 53.5k | switch (sd->effect_pcount1 = get_bits(gb, 4)) { |
159 | 2.29k | case 7: |
160 | 2.29k | vc1_sprite_parse_transform(gb, sd->effect_params1); |
161 | 2.29k | break; |
162 | 2.09k | case 14: |
163 | 2.09k | vc1_sprite_parse_transform(gb, sd->effect_params1); |
164 | 2.09k | vc1_sprite_parse_transform(gb, sd->effect_params1 + 7); |
165 | 2.09k | break; |
166 | 49.1k | default: |
167 | 227k | for (i = 0; i < sd->effect_pcount1; i++) |
168 | 178k | sd->effect_params1[i] = get_fp_val(gb); |
169 | 53.5k | } |
170 | 53.5k | if (sd->effect_type != 13 || sd->effect_params1[0] != sd->coefs[0][6]) { |
171 | | // effect 13 is simple alpha blending and matches the opacity above |
172 | 52.4k | av_log(avctx, AV_LOG_DEBUG, "Effect: %d; params: ", sd->effect_type); |
173 | 269k | for (i = 0; i < sd->effect_pcount1; i++) |
174 | 217k | av_log(avctx, AV_LOG_DEBUG, " %d.%.2d", |
175 | 217k | sd->effect_params1[i] / (1 << 16), |
176 | 217k | (abs(sd->effect_params1[i]) & 0xFFFF) * 1000 / (1 << 16)); |
177 | 52.4k | av_log(avctx, AV_LOG_DEBUG, "\n"); |
178 | 52.4k | } |
179 | | |
180 | 53.5k | sd->effect_pcount2 = get_bits(gb, 16); |
181 | 53.5k | if (sd->effect_pcount2 > 10) { |
182 | 22.4k | av_log(avctx, AV_LOG_ERROR, "Too many effect parameters\n"); |
183 | 22.4k | return AVERROR_INVALIDDATA; |
184 | 31.1k | } else if (sd->effect_pcount2) { |
185 | 6.17k | i = -1; |
186 | 6.17k | av_log(avctx, AV_LOG_DEBUG, "Effect params 2: "); |
187 | 22.4k | while (++i < sd->effect_pcount2) { |
188 | 16.2k | sd->effect_params2[i] = get_fp_val(gb); |
189 | 16.2k | av_log(avctx, AV_LOG_DEBUG, " %d.%.2d", |
190 | 16.2k | sd->effect_params2[i] / (1 << 16), |
191 | 16.2k | (abs(sd->effect_params2[i]) & 0xFFFF) * 1000 / (1 << 16)); |
192 | 16.2k | } |
193 | 6.17k | av_log(avctx, AV_LOG_DEBUG, "\n"); |
194 | 6.17k | } |
195 | 53.5k | } |
196 | 186k | if (sd->effect_flag = get_bits1(gb)) |
197 | 2.02k | av_log(avctx, AV_LOG_DEBUG, "Effect flag set\n"); |
198 | | |
199 | 186k | if (get_bits_count(gb) >= gb->size_in_bits + |
200 | 186k | (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE ? 64 : 0)) { |
201 | 77.5k | av_log(avctx, AV_LOG_ERROR, "Buffer overrun\n"); |
202 | 77.5k | return AVERROR_INVALIDDATA; |
203 | 77.5k | } |
204 | 108k | if (get_bits_count(gb) < gb->size_in_bits - 8) |
205 | 12.0k | av_log(avctx, AV_LOG_WARNING, "Buffer not fully read\n"); |
206 | | |
207 | 108k | return 0; |
208 | 186k | } |
209 | | |
210 | | static void vc1_draw_sprites(VC1Context *v, SpriteData* sd) |
211 | 82.7k | { |
212 | 82.7k | int i, plane, row, sprite; |
213 | 82.7k | int sr_cache[2][2] = { { -1, -1 }, { -1, -1 } }; |
214 | 82.7k | const uint8_t *src_h[2][2]; |
215 | 82.7k | int xoff[2], xadv[2], yoff[2], yadv[2], alpha; |
216 | 82.7k | int ysub[2]; |
217 | 82.7k | MpegEncContext *s = &v->s; |
218 | | |
219 | 171k | for (i = 0; i <= v->two_sprites; i++) { |
220 | 89.1k | xoff[i] = av_clip(sd->coefs[i][2], 0, v->sprite_width-1 << 16); |
221 | 89.1k | xadv[i] = sd->coefs[i][0]; |
222 | 89.1k | if (xadv[i] != 1<<16 || (v->sprite_width << 16) - (v->output_width << 16) - xoff[i]) |
223 | 88.6k | xadv[i] = av_clip(xadv[i], 0, ((v->sprite_width<<16) - xoff[i] - 1) / v->output_width); |
224 | | |
225 | 89.1k | yoff[i] = av_clip(sd->coefs[i][5], 0, v->sprite_height-1 << 16); |
226 | 89.1k | yadv[i] = av_clip(sd->coefs[i][4], 0, ((v->sprite_height << 16) - yoff[i]) / v->output_height); |
227 | 89.1k | } |
228 | 82.7k | alpha = av_clip_uint16(sd->coefs[1][6]); |
229 | | |
230 | 330k | for (plane = 0; plane < (CONFIG_GRAY && s->avctx->flags & AV_CODEC_FLAG_GRAY ? 1 : 3); plane++) { |
231 | 248k | int width = v->output_width>>!!plane; |
232 | | |
233 | 17.0M | for (row = 0; row < v->output_height>>!!plane; row++) { |
234 | 16.8M | uint8_t *dst = v->sprite_output_frame->data[plane] + |
235 | 16.8M | v->sprite_output_frame->linesize[plane] * row; |
236 | | |
237 | 39.8M | for (sprite = 0; sprite <= v->two_sprites; sprite++) { |
238 | 23.0M | const uint8_t *iplane = s->cur_pic.data[plane]; |
239 | 23.0M | int iline = s->cur_pic.linesize[plane]; |
240 | 23.0M | int ycoord = yoff[sprite] + yadv[sprite] * row; |
241 | 23.0M | int yline = ycoord >> 16; |
242 | 23.0M | int next_line; |
243 | 23.0M | ysub[sprite] = ycoord & 0xFFFF; |
244 | 23.0M | if (sprite) { |
245 | 6.16M | iplane = s->last_pic.data[plane]; |
246 | 6.16M | iline = s->last_pic.linesize[plane]; |
247 | 6.16M | } |
248 | 23.0M | next_line = FFMIN(yline + 1, (v->sprite_height >> !!plane) - 1) * iline; |
249 | 23.0M | if (!(xoff[sprite] & 0xFFFF) && xadv[sprite] == 1 << 16) { |
250 | 7.57M | src_h[sprite][0] = iplane + (xoff[sprite] >> 16) + yline * iline; |
251 | 7.57M | if (ysub[sprite]) |
252 | 7.13M | src_h[sprite][1] = iplane + (xoff[sprite] >> 16) + next_line; |
253 | 15.4M | } else { |
254 | 15.4M | if (sr_cache[sprite][0] != yline) { |
255 | 1.33M | if (sr_cache[sprite][1] == yline) { |
256 | 855k | FFSWAP(uint8_t*, v->sr_rows[sprite][0], v->sr_rows[sprite][1]); |
257 | 855k | FFSWAP(int, sr_cache[sprite][0], sr_cache[sprite][1]); |
258 | 855k | } else { |
259 | 479k | v->vc1dsp.sprite_h(v->sr_rows[sprite][0], iplane + yline * iline, xoff[sprite], xadv[sprite], width); |
260 | 479k | sr_cache[sprite][0] = yline; |
261 | 479k | } |
262 | 1.33M | } |
263 | 15.4M | if (ysub[sprite] && sr_cache[sprite][1] != yline + 1) { |
264 | 1.18M | v->vc1dsp.sprite_h(v->sr_rows[sprite][1], |
265 | 1.18M | iplane + next_line, xoff[sprite], |
266 | 1.18M | xadv[sprite], width); |
267 | 1.18M | sr_cache[sprite][1] = yline + 1; |
268 | 1.18M | } |
269 | 15.4M | src_h[sprite][0] = v->sr_rows[sprite][0]; |
270 | 15.4M | src_h[sprite][1] = v->sr_rows[sprite][1]; |
271 | 15.4M | } |
272 | 23.0M | } |
273 | | |
274 | 16.8M | if (!v->two_sprites) { |
275 | 10.6M | if (ysub[0]) { |
276 | 7.96M | v->vc1dsp.sprite_v_single(dst, src_h[0][0], src_h[0][1], ysub[0], width); |
277 | 7.96M | } else { |
278 | 2.70M | memcpy(dst, src_h[0][0], width); |
279 | 2.70M | } |
280 | 10.6M | } else { |
281 | 6.16M | if (ysub[0] && ysub[1]) { |
282 | 3.35M | v->vc1dsp.sprite_v_double_twoscale(dst, src_h[0][0], src_h[0][1], ysub[0], |
283 | 3.35M | src_h[1][0], src_h[1][1], ysub[1], alpha, width); |
284 | 3.35M | } else if (ysub[0]) { |
285 | 887k | v->vc1dsp.sprite_v_double_onescale(dst, src_h[0][0], src_h[0][1], ysub[0], |
286 | 887k | src_h[1][0], alpha, width); |
287 | 1.92M | } else if (ysub[1]) { |
288 | 1.53M | v->vc1dsp.sprite_v_double_onescale(dst, src_h[1][0], src_h[1][1], ysub[1], |
289 | 1.53M | src_h[0][0], (1<<16)-1-alpha, width); |
290 | 1.53M | } else { |
291 | 391k | v->vc1dsp.sprite_v_double_noscale(dst, src_h[0][0], src_h[1][0], alpha, width); |
292 | 391k | } |
293 | 6.16M | } |
294 | 16.8M | } |
295 | | |
296 | 248k | if (!plane) { |
297 | 171k | for (i = 0; i <= v->two_sprites; i++) { |
298 | 89.1k | xoff[i] >>= 1; |
299 | 89.1k | yoff[i] >>= 1; |
300 | 89.1k | } |
301 | 82.7k | } |
302 | | |
303 | 248k | } |
304 | 82.7k | } |
305 | | |
306 | | |
307 | | static int vc1_decode_sprites(VC1Context *v, GetBitContext* gb) |
308 | 208k | { |
309 | 208k | int ret; |
310 | 208k | MpegEncContext *s = &v->s; |
311 | 208k | AVCodecContext *avctx = s->avctx; |
312 | 208k | SpriteData sd; |
313 | | |
314 | 208k | memset(&sd, 0, sizeof(sd)); |
315 | | |
316 | 208k | ret = vc1_parse_sprites(v, gb, &sd); |
317 | 208k | if (ret < 0) |
318 | 100k | return ret; |
319 | | |
320 | 108k | if (!s->cur_pic.data[0]) { |
321 | 10.3k | av_log(avctx, AV_LOG_ERROR, "Got no sprites\n"); |
322 | 10.3k | return AVERROR_UNKNOWN; |
323 | 10.3k | } |
324 | | |
325 | 98.4k | if (v->two_sprites && (!s->last_pic.ptr || !s->last_pic.data[0])) { |
326 | 2.10k | av_log(avctx, AV_LOG_WARNING, "Need two sprites, only got one\n"); |
327 | 2.10k | v->two_sprites = 0; |
328 | 2.10k | } |
329 | | |
330 | 98.4k | av_frame_unref(v->sprite_output_frame); |
331 | 98.4k | if ((ret = ff_get_buffer(avctx, v->sprite_output_frame, 0)) < 0) |
332 | 15.7k | return ret; |
333 | | |
334 | 82.7k | vc1_draw_sprites(v, &sd); |
335 | | |
336 | 82.7k | return 0; |
337 | 98.4k | } |
338 | | |
339 | | static av_cold void vc1_sprite_flush(AVCodecContext *avctx) |
340 | 95.8k | { |
341 | 95.8k | VC1Context *v = avctx->priv_data; |
342 | 95.8k | MpegEncContext *s = &v->s; |
343 | 95.8k | MPVWorkPicture *f = &s->cur_pic; |
344 | 95.8k | int plane, i; |
345 | | |
346 | | /* Windows Media Image codecs have a convergence interval of two keyframes. |
347 | | Since we can't enforce it, clear to black the missing sprite. This is |
348 | | wrong but it looks better than doing nothing. */ |
349 | | |
350 | 95.8k | if (f->data[0]) |
351 | 274k | for (plane = 0; plane < (CONFIG_GRAY && s->avctx->flags & AV_CODEC_FLAG_GRAY ? 1 : 3); plane++) |
352 | 121M | for (i = 0; i < v->sprite_height>>!!plane; i++) |
353 | 121M | memset(f->data[plane] + i * f->linesize[plane], |
354 | 121M | plane ? 128 : 0, f->linesize[plane]); |
355 | 95.8k | } |
356 | | |
357 | | #endif |
358 | | |
359 | | static av_cold int vc1_decode_init_alloc_tables(VC1Context *v) |
360 | 60.9k | { |
361 | 60.9k | MpegEncContext *s = &v->s; |
362 | 60.9k | int i, ret; |
363 | 60.9k | int mb_height = FFALIGN(s->mb_height, 2); |
364 | | |
365 | | /* Allocate mb bitplanes */ |
366 | 60.9k | v->mv_type_mb_plane = av_malloc (s->mb_stride * mb_height); |
367 | 60.9k | v->direct_mb_plane = av_malloc (s->mb_stride * mb_height); |
368 | 60.9k | v->forward_mb_plane = av_malloc (s->mb_stride * mb_height); |
369 | 60.9k | v->fieldtx_plane = av_mallocz(s->mb_stride * mb_height); |
370 | 60.9k | v->acpred_plane = av_malloc (s->mb_stride * mb_height); |
371 | 60.9k | v->over_flags_plane = av_malloc (s->mb_stride * mb_height); |
372 | 60.9k | if (!v->mv_type_mb_plane || !v->direct_mb_plane || !v->forward_mb_plane || |
373 | 60.9k | !v->fieldtx_plane || !v->acpred_plane || !v->over_flags_plane) |
374 | 0 | return AVERROR(ENOMEM); |
375 | | |
376 | 60.9k | v->n_allocated_blks = s->mb_width + 2; |
377 | 60.9k | v->block = av_malloc(sizeof(*v->block) * v->n_allocated_blks); |
378 | 60.9k | v->cbp_base = av_malloc(sizeof(v->cbp_base[0]) * 3 * s->mb_stride); |
379 | 60.9k | if (!v->block || !v->cbp_base) |
380 | 0 | return AVERROR(ENOMEM); |
381 | 60.9k | v->cbp = v->cbp_base + 2 * s->mb_stride; |
382 | 60.9k | v->ttblk_base = av_mallocz(sizeof(v->ttblk_base[0]) * 3 * s->mb_stride); |
383 | 60.9k | if (!v->ttblk_base) |
384 | 0 | return AVERROR(ENOMEM); |
385 | 60.9k | v->ttblk = v->ttblk_base + 2 * s->mb_stride; |
386 | 60.9k | v->is_intra_base = av_mallocz(sizeof(v->is_intra_base[0]) * 3 * s->mb_stride); |
387 | 60.9k | if (!v->is_intra_base) |
388 | 0 | return AVERROR(ENOMEM); |
389 | 60.9k | v->is_intra = v->is_intra_base + 2 * s->mb_stride; |
390 | 60.9k | v->luma_mv_base = av_mallocz(sizeof(v->luma_mv_base[0]) * 3 * s->mb_stride); |
391 | 60.9k | if (!v->luma_mv_base) |
392 | 0 | return AVERROR(ENOMEM); |
393 | 60.9k | v->luma_mv = v->luma_mv_base + 2 * s->mb_stride; |
394 | | |
395 | | /* allocate block type info in that way so it could be used with s->block_index[] */ |
396 | 60.9k | v->mb_type_base = av_mallocz(s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2); |
397 | 60.9k | if (!v->mb_type_base) |
398 | 0 | return AVERROR(ENOMEM); |
399 | 60.9k | v->mb_type = v->mb_type_base + s->b8_stride + 1; |
400 | | |
401 | | /* allocate memory to store block level MV info */ |
402 | 60.9k | v->blk_mv_type_base = av_mallocz( s->b8_stride * (mb_height * 2 + 1)); |
403 | 60.9k | if (!v->blk_mv_type_base) |
404 | 0 | return AVERROR(ENOMEM); |
405 | 60.9k | v->blk_mv_type = v->blk_mv_type_base + s->b8_stride + 1; |
406 | 60.9k | v->mv_f_base = av_mallocz(2 * (s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2)); |
407 | 60.9k | if (!v->mv_f_base) |
408 | 0 | return AVERROR(ENOMEM); |
409 | 60.9k | v->mv_f[0] = v->mv_f_base + s->b8_stride + 1; |
410 | 60.9k | v->mv_f[1] = v->mv_f[0] + (s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2); |
411 | 60.9k | v->mv_f_next_base = av_mallocz(2 * (s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2)); |
412 | 60.9k | if (!v->mv_f_next_base) |
413 | 0 | return AVERROR(ENOMEM); |
414 | 60.9k | v->mv_f_next[0] = v->mv_f_next_base + s->b8_stride + 1; |
415 | 60.9k | v->mv_f_next[1] = v->mv_f_next[0] + (s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2); |
416 | | |
417 | 60.9k | if (s->avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || s->avctx->codec_id == AV_CODEC_ID_VC1IMAGE) { |
418 | 51.8k | for (i = 0; i < 4; i++) |
419 | 41.4k | if (!(v->sr_rows[i >> 1][i & 1] = av_malloc(v->output_width))) |
420 | 0 | return AVERROR(ENOMEM); |
421 | 10.3k | } |
422 | | |
423 | 60.9k | ret = ff_intrax8_common_init(s->avctx, &v->x8, v->blocks[0], |
424 | 60.9k | s->mb_width, s->mb_height); |
425 | 60.9k | if (ret < 0) |
426 | 0 | return ret; |
427 | | |
428 | 60.9k | return 0; |
429 | 60.9k | } |
430 | | |
431 | | static enum AVPixelFormat vc1_get_format(AVCodecContext *avctx) |
432 | 60.9k | { |
433 | 60.9k | if (avctx->codec_id == AV_CODEC_ID_MSS2) |
434 | 2.40k | return AV_PIX_FMT_YUV420P; |
435 | | |
436 | 58.5k | if (CONFIG_GRAY && (avctx->flags & AV_CODEC_FLAG_GRAY)) { |
437 | 0 | if (avctx->color_range == AVCOL_RANGE_UNSPECIFIED) |
438 | 0 | avctx->color_range = AVCOL_RANGE_MPEG; |
439 | 0 | return AV_PIX_FMT_GRAY8; |
440 | 0 | } |
441 | | |
442 | 58.5k | if (avctx->codec_id == AV_CODEC_ID_VC1IMAGE || |
443 | 50.3k | avctx->codec_id == AV_CODEC_ID_WMV3IMAGE) |
444 | 10.3k | return AV_PIX_FMT_YUV420P; |
445 | | |
446 | 48.2k | return ff_get_format(avctx, vc1_hwaccel_pixfmt_list_420); |
447 | 58.5k | } |
448 | | |
449 | | static void vc1_decode_reset(AVCodecContext *avctx); |
450 | | |
451 | | av_cold int ff_vc1_decode_init(AVCodecContext *avctx) |
452 | 78.3k | { |
453 | 78.3k | VC1Context *const v = avctx->priv_data; |
454 | 78.3k | MpegEncContext *const s = &v->s; |
455 | 78.3k | int ret; |
456 | | |
457 | 78.3k | ret = av_image_check_size(avctx->width, avctx->height, 0, avctx); |
458 | 78.3k | if (ret < 0) |
459 | 17.3k | return ret; |
460 | | |
461 | 60.9k | ret = ff_mpv_decode_init(s, avctx); |
462 | 60.9k | if (ret < 0) |
463 | 0 | return ret; |
464 | | |
465 | 60.9k | avctx->pix_fmt = vc1_get_format(avctx); |
466 | | |
467 | 60.9k | ret = ff_mpv_common_init(s); |
468 | 60.9k | if (ret < 0) |
469 | 0 | return ret; |
470 | | |
471 | 60.9k | ret = vc1_decode_init_alloc_tables(v); |
472 | 60.9k | if (ret < 0) { |
473 | 0 | vc1_decode_reset(avctx); |
474 | 0 | return ret; |
475 | 0 | } |
476 | 60.9k | return 0; |
477 | 60.9k | } |
478 | | |
479 | | av_cold void ff_vc1_init_transposed_scantables(VC1Context *v) |
480 | 22.6k | { |
481 | 22.6k | int i; |
482 | 1.47M | for (i = 0; i < 64; i++) { |
483 | 7.24M | #define transpose(x) (((x) >> 3) | (((x) & 7) << 3)) |
484 | 1.44M | v->zz_8x8[0][i] = transpose(ff_wmv1_scantable[0][i]); |
485 | 1.44M | v->zz_8x8[1][i] = transpose(ff_wmv1_scantable[1][i]); |
486 | 1.44M | v->zz_8x8[2][i] = transpose(ff_wmv1_scantable[2][i]); |
487 | 1.44M | v->zz_8x8[3][i] = transpose(ff_wmv1_scantable[3][i]); |
488 | 1.44M | v->zzi_8x8[i] = transpose(ff_vc1_adv_interlaced_8x8_zz[i]); |
489 | 1.44M | } |
490 | 22.6k | v->left_blk_sh = 0; |
491 | 22.6k | v->top_blk_sh = 3; |
492 | 22.6k | } |
493 | | |
494 | | static av_cold void vc1_init_static(void) |
495 | 5 | { |
496 | 5 | static VLCElem vlc_table[32372]; |
497 | 5 | VLCInitState state = VLC_INIT_STATE(vlc_table); |
498 | | |
499 | 5 | VLC_INIT_STATIC_TABLE(ff_vc1_norm2_vlc, VC1_NORM2_VLC_BITS, 4, |
500 | 5 | vc1_norm2_bits, 1, 1, |
501 | 5 | vc1_norm2_codes, 1, 1, 0); |
502 | 5 | VLC_INIT_STATIC_TABLE(ff_vc1_norm6_vlc, VC1_NORM6_VLC_BITS, 64, |
503 | 5 | vc1_norm6_bits, 1, 1, |
504 | 5 | vc1_norm6_codes, 2, 2, 0); |
505 | 5 | VLC_INIT_STATIC_TABLE(ff_vc1_imode_vlc, VC1_IMODE_VLC_BITS, 7, |
506 | 5 | vc1_imode_bits, 1, 1, |
507 | 5 | vc1_imode_codes, 1, 1, 0); |
508 | 20 | for (int i = 0; i < 3; i++) { |
509 | 15 | ff_vc1_ttmb_vlc[i] = |
510 | 15 | ff_vlc_init_tables(&state, VC1_TTMB_VLC_BITS, 16, |
511 | 15 | vc1_ttmb_bits[i], 1, 1, |
512 | 15 | vc1_ttmb_codes[i], 2, 2, 0); |
513 | 15 | ff_vc1_ttblk_vlc[i] = |
514 | 15 | ff_vlc_init_tables(&state, VC1_TTBLK_VLC_BITS, 8, |
515 | 15 | vc1_ttblk_bits[i], 1, 1, |
516 | 15 | vc1_ttblk_codes[i], 1, 1, 0); |
517 | 15 | ff_vc1_subblkpat_vlc[i] = |
518 | 15 | ff_vlc_init_tables(&state, VC1_SUBBLKPAT_VLC_BITS, 15, |
519 | 15 | vc1_subblkpat_bits[i], 1, 1, |
520 | 15 | vc1_subblkpat_codes[i], 1, 1, 0); |
521 | 15 | } |
522 | 25 | for (int i = 0; i < 4; i++) { |
523 | 20 | ff_vc1_4mv_block_pattern_vlc[i] = |
524 | 20 | ff_vlc_init_tables(&state, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 16, |
525 | 20 | vc1_4mv_block_pattern_bits[i], 1, 1, |
526 | 20 | vc1_4mv_block_pattern_codes[i], 1, 1, 0); |
527 | 20 | ff_vc1_cbpcy_p_vlc[i] = |
528 | 20 | ff_vlc_init_tables(&state, VC1_CBPCY_P_VLC_BITS, 64, |
529 | 20 | vc1_cbpcy_p_bits[i], 1, 1, |
530 | 20 | vc1_cbpcy_p_codes[i], 2, 2, 0); |
531 | 20 | ff_vc1_mv_diff_vlc[i] = |
532 | 20 | ff_vlc_init_tables(&state, VC1_MV_DIFF_VLC_BITS, 73, |
533 | 20 | vc1_mv_diff_bits[i], 1, 1, |
534 | 20 | vc1_mv_diff_codes[i], 2, 2, 0); |
535 | | /* initialize 4MV MBMODE VLC tables for interlaced frame P picture */ |
536 | 20 | ff_vc1_intfr_4mv_mbmode_vlc[i] = |
537 | 20 | ff_vlc_init_tables(&state, VC1_INTFR_4MV_MBMODE_VLC_BITS, 15, |
538 | 20 | vc1_intfr_4mv_mbmode_bits[i], 1, 1, |
539 | 20 | vc1_intfr_4mv_mbmode_codes[i], 2, 2, 0); |
540 | | /* initialize NON-4MV MBMODE VLC tables for the same */ |
541 | 20 | ff_vc1_intfr_non4mv_mbmode_vlc[i] = |
542 | 20 | ff_vlc_init_tables(&state, VC1_INTFR_NON4MV_MBMODE_VLC_BITS, 9, |
543 | 20 | vc1_intfr_non4mv_mbmode_bits[i], 1, 1, |
544 | 20 | vc1_intfr_non4mv_mbmode_codes[i], 1, 1, 0); |
545 | | /* initialize interlaced MVDATA tables (1-Ref) */ |
546 | 20 | ff_vc1_1ref_mvdata_vlc[i] = |
547 | 20 | ff_vlc_init_tables(&state, VC1_1REF_MVDATA_VLC_BITS, 72, |
548 | 20 | vc1_1ref_mvdata_bits[i], 1, 1, |
549 | 20 | vc1_1ref_mvdata_codes[i], 4, 4, 0); |
550 | | /* Initialize 2MV Block pattern VLC tables */ |
551 | 20 | ff_vc1_2mv_block_pattern_vlc[i] = |
552 | 20 | ff_vlc_init_tables(&state, VC1_2MV_BLOCK_PATTERN_VLC_BITS, 4, |
553 | 20 | vc1_2mv_block_pattern_bits[i], 1, 1, |
554 | 20 | vc1_2mv_block_pattern_codes[i], 1, 1, 0); |
555 | 20 | } |
556 | 45 | for (int i = 0; i < 8; i++) { |
557 | 40 | ff_vc1_ac_coeff_table[i] = |
558 | 40 | ff_vlc_init_tables(&state, AC_VLC_BITS, ff_vc1_ac_sizes[i], |
559 | 40 | &vc1_ac_tables[i][0][1], 8, 4, |
560 | 40 | &vc1_ac_tables[i][0][0], 8, 4, 0); |
561 | | /* initialize interlaced MVDATA tables (2-Ref) */ |
562 | 40 | ff_vc1_2ref_mvdata_vlc[i] = |
563 | 40 | ff_vlc_init_tables(&state, VC1_2REF_MVDATA_VLC_BITS, 126, |
564 | 40 | vc1_2ref_mvdata_bits[i], 1, 1, |
565 | 40 | vc1_2ref_mvdata_codes[i], 4, 4, 0); |
566 | | /* Initialize interlaced CBPCY VLC tables (Table 124 - Table 131) */ |
567 | 40 | ff_vc1_icbpcy_vlc[i] = |
568 | 40 | ff_vlc_init_tables(&state, VC1_ICBPCY_VLC_BITS, 63, |
569 | 40 | vc1_icbpcy_p_bits[i], 1, 1, |
570 | 40 | vc1_icbpcy_p_codes[i], 2, 2, 0); |
571 | | /* Initialize interlaced field picture MBMODE VLC tables */ |
572 | 40 | ff_vc1_if_mmv_mbmode_vlc[i] = |
573 | 40 | ff_vlc_init_tables(&state, VC1_IF_MMV_MBMODE_VLC_BITS, 8, |
574 | 40 | vc1_if_mmv_mbmode_bits[i], 1, 1, |
575 | 40 | vc1_if_mmv_mbmode_codes[i], 1, 1, 0); |
576 | 40 | ff_vc1_if_1mv_mbmode_vlc[i] = |
577 | 40 | ff_vlc_init_tables(&state, VC1_IF_1MV_MBMODE_VLC_BITS, 6, |
578 | 40 | vc1_if_1mv_mbmode_bits[i], 1, 1, |
579 | 40 | vc1_if_1mv_mbmode_codes[i], 1, 1, 0); |
580 | 40 | } |
581 | 5 | ff_msmp4_vc1_vlcs_init_once(); |
582 | 5 | } |
583 | | |
584 | | /** |
585 | | * Init VC-1 specific tables and VC1Context members |
586 | | * @param v The VC1Context to initialize |
587 | | * @return Status |
588 | | */ |
589 | | av_cold void ff_vc1_init_common(VC1Context *v) |
590 | 32.2k | { |
591 | 32.2k | static AVOnce init_static_once = AV_ONCE_INIT; |
592 | 32.2k | MpegEncContext *const s = &v->s; |
593 | | |
594 | | /* defaults */ |
595 | 32.2k | v->pq = -1; |
596 | 32.2k | v->mvrange = 0; /* 7.1.1.18, p80 */ |
597 | | |
598 | 32.2k | s->avctx->chroma_sample_location = AVCHROMA_LOC_LEFT; |
599 | 32.2k | s->out_format = FMT_H263; |
600 | | |
601 | 32.2k | s->h263_pred = 1; |
602 | 32.2k | s->msmpeg4_version = MSMP4_VC1; |
603 | | |
604 | 32.2k | ff_vc1dsp_init(&v->vc1dsp); |
605 | | |
606 | | /* For error resilience */ |
607 | 32.2k | ff_qpeldsp_init(&s->qdsp); |
608 | | |
609 | | /* VLC tables */ |
610 | 32.2k | ff_thread_once(&init_static_once, vc1_init_static); |
611 | 32.2k | } |
612 | | |
613 | | /** Initialize a VC1/WMV3 decoder |
614 | | * @todo TODO: Handle VC-1 IDUs (Transport level?) |
615 | | * @todo TODO: Decipher remaining bits in extra_data |
616 | | */ |
617 | | static av_cold int vc1_decode_init(AVCodecContext *avctx) |
618 | 30.5k | { |
619 | 30.5k | VC1Context *v = avctx->priv_data; |
620 | 30.5k | MpegEncContext *s = &v->s; |
621 | 30.5k | GetBitContext gb; |
622 | 30.5k | int ret; |
623 | | |
624 | | /* save the container output size for WMImage */ |
625 | 30.5k | v->output_width = avctx->width; |
626 | 30.5k | v->output_height = avctx->height; |
627 | | |
628 | 30.5k | if (!avctx->extradata_size || !avctx->extradata) |
629 | 705 | return AVERROR_INVALIDDATA; |
630 | 29.8k | v->s.avctx = avctx; |
631 | | |
632 | 29.8k | ff_vc1_init_common(v); |
633 | | |
634 | 29.8k | if (avctx->codec_id == AV_CODEC_ID_WMV3 || avctx->codec_id == AV_CODEC_ID_WMV3IMAGE) { |
635 | 9.25k | int count = 0; |
636 | | |
637 | | // looks like WMV3 has a sequence header stored in the extradata |
638 | | // advanced sequence header may be before the first frame |
639 | | // the last byte of the extradata is a version number, 1 for the |
640 | | // samples we can decode |
641 | | |
642 | 9.25k | ret = init_get_bits8(&gb, avctx->extradata, avctx->extradata_size); |
643 | 9.25k | if (ret < 0) |
644 | 0 | return ret; |
645 | | |
646 | 9.25k | if ((ret = ff_vc1_decode_sequence_header(avctx, v, &gb)) < 0) |
647 | 137 | return ret; |
648 | | |
649 | 9.11k | if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE && !v->res_sprite) { |
650 | 303 | avpriv_request_sample(avctx, "Non sprite WMV3IMAGE"); |
651 | 303 | return AVERROR_PATCHWELCOME; |
652 | 303 | } |
653 | | |
654 | 8.81k | count = avctx->extradata_size*8 - get_bits_count(&gb); |
655 | 8.81k | if (count > 0) { |
656 | 4.28k | av_log(avctx, AV_LOG_INFO, "Extra data: %i bits left, value: %X\n", |
657 | 4.28k | count, get_bits_long(&gb, FFMIN(count, 32))); |
658 | 4.52k | } else if (count < 0) { |
659 | 4.40k | av_log(avctx, AV_LOG_INFO, "Read %i bits in overflow\n", -count); |
660 | 4.40k | } |
661 | 20.5k | } else { // VC1/WVC1/WVP2 |
662 | 20.5k | const uint8_t *start = avctx->extradata; |
663 | 20.5k | const uint8_t *end = avctx->extradata + avctx->extradata_size; |
664 | 20.5k | const uint8_t *next; |
665 | 20.5k | int size, buf2_size; |
666 | 20.5k | uint8_t *buf2 = NULL; |
667 | 20.5k | int seq_initialized = 0, ep_initialized = 0; |
668 | | |
669 | 20.5k | if (avctx->extradata_size < 16) { |
670 | 10 | av_log(avctx, AV_LOG_ERROR, "Extradata size too small: %i\n", avctx->extradata_size); |
671 | 10 | return AVERROR_INVALIDDATA; |
672 | 10 | } |
673 | | |
674 | 20.5k | buf2 = av_mallocz(avctx->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE); |
675 | 20.5k | if (!buf2) |
676 | 0 | return AVERROR(ENOMEM); |
677 | | |
678 | 20.5k | start = find_next_marker(start, end); // in WVC1 extradata first byte is its size, but can be 0 in mkv |
679 | 20.5k | next = start; |
680 | 330k | for (; next < end; start = next) { |
681 | 309k | next = find_next_marker(start + 4, end); |
682 | 309k | size = next - start - 4; |
683 | 309k | if (size <= 0) |
684 | 5.47k | continue; |
685 | 304k | buf2_size = v->vc1dsp.vc1_unescape_buffer(start + 4, size, buf2); |
686 | 304k | ret = init_get_bits8(&gb, buf2, buf2_size); |
687 | 304k | if (ret < 0) { |
688 | 0 | av_free(buf2); |
689 | 0 | return ret; |
690 | 0 | } |
691 | 304k | switch (AV_RB32(start)) { |
692 | 81.2k | case VC1_CODE_SEQHDR: |
693 | 81.2k | if ((ret = ff_vc1_decode_sequence_header(avctx, v, &gb)) < 0) { |
694 | 85 | av_free(buf2); |
695 | 85 | return ret; |
696 | 85 | } |
697 | 81.1k | seq_initialized = 1; |
698 | 81.1k | break; |
699 | 31.6k | case VC1_CODE_ENTRYPOINT: |
700 | 31.6k | if ((ret = ff_vc1_decode_entry_point(avctx, v, &gb)) < 0) { |
701 | 11 | av_free(buf2); |
702 | 11 | return ret; |
703 | 11 | } |
704 | 31.6k | ep_initialized = 1; |
705 | 31.6k | break; |
706 | 304k | } |
707 | 304k | } |
708 | 20.4k | av_free(buf2); |
709 | 20.4k | if (!seq_initialized || !ep_initialized) { |
710 | 235 | av_log(avctx, AV_LOG_ERROR, "Incomplete extradata\n"); |
711 | 235 | return AVERROR_INVALIDDATA; |
712 | 235 | } |
713 | 20.2k | v->res_sprite = (avctx->codec_id == AV_CODEC_ID_VC1IMAGE); |
714 | 20.2k | } |
715 | | |
716 | 29.0k | avctx->profile = v->profile; |
717 | 29.0k | if (v->profile == PROFILE_ADVANCED) |
718 | 13.2k | avctx->level = v->level; |
719 | | |
720 | 29.0k | ff_blockdsp_init(&s->bdsp); |
721 | 29.0k | ff_h264chroma_init(&v->h264chroma, 8); |
722 | | |
723 | 29.0k | avctx->has_b_frames = !!avctx->max_b_frames; |
724 | | |
725 | 29.0k | if (v->color_prim == 1 || v->color_prim == 5 || v->color_prim == 6) |
726 | 41 | avctx->color_primaries = v->color_prim; |
727 | 29.0k | if (v->transfer_char == 1 || v->transfer_char == 7) |
728 | 62 | avctx->color_trc = v->transfer_char; |
729 | 29.0k | if (v->matrix_coef == 1 || v->matrix_coef == 6 || v->matrix_coef == 7) |
730 | 61 | avctx->colorspace = v->matrix_coef; |
731 | | |
732 | 29.0k | s->mb_width = (avctx->coded_width + 15) >> 4; |
733 | 29.0k | s->mb_height = (avctx->coded_height + 15) >> 4; |
734 | | |
735 | 29.0k | if (v->profile == PROFILE_ADVANCED || v->res_fasttx) { |
736 | 20.2k | ff_vc1_init_transposed_scantables(v); |
737 | 20.2k | } else { |
738 | 8.81k | memcpy(v->zz_8x8, ff_wmv1_scantable, 4*64); |
739 | 8.81k | v->left_blk_sh = 3; |
740 | 8.81k | v->top_blk_sh = 0; |
741 | 8.81k | v->vc1dsp.vc1_inv_trans_8x8 = ff_simple_idct_int16_8bit; |
742 | 8.81k | v->vc1dsp.vc1_inv_trans_8x4 = ff_simple_idct84_add; |
743 | 8.81k | v->vc1dsp.vc1_inv_trans_4x8 = ff_simple_idct48_add; |
744 | 8.81k | v->vc1dsp.vc1_inv_trans_4x4 = ff_simple_idct44_add; |
745 | 8.81k | v->vc1dsp.vc1_inv_trans_8x8_dc = ff_simple_idct_add_int16_8bit; |
746 | 8.81k | v->vc1dsp.vc1_inv_trans_8x4_dc = ff_simple_idct84_add; |
747 | 8.81k | v->vc1dsp.vc1_inv_trans_4x8_dc = ff_simple_idct48_add; |
748 | 8.81k | v->vc1dsp.vc1_inv_trans_4x4_dc = ff_simple_idct44_add; |
749 | 8.81k | } |
750 | | |
751 | 29.0k | if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) { |
752 | 10.6k | v->sprite_width = avctx->coded_width; |
753 | 10.6k | v->sprite_height = avctx->coded_height; |
754 | | |
755 | 10.6k | avctx->coded_width = avctx->width = v->output_width; |
756 | 10.6k | avctx->coded_height = avctx->height = v->output_height; |
757 | | |
758 | | // prevent 16.16 overflows |
759 | 10.6k | if (v->sprite_width > 1 << 14 || |
760 | 10.6k | v->sprite_height > 1 << 14 || |
761 | 10.6k | v->output_width > 1 << 14 || |
762 | 10.6k | v->output_height > 1 << 14) { |
763 | 19 | return AVERROR_INVALIDDATA; |
764 | 19 | } |
765 | | |
766 | 10.6k | if ((v->sprite_width&1) || (v->sprite_height&1)) { |
767 | 41 | avpriv_request_sample(avctx, "odd sprites support"); |
768 | 41 | return AVERROR_PATCHWELCOME; |
769 | 41 | } |
770 | 10.6k | } |
771 | 28.9k | return 0; |
772 | 29.0k | } |
773 | | |
774 | | static av_cold void vc1_decode_reset(AVCodecContext *avctx) |
775 | 61.9k | { |
776 | 61.9k | VC1Context *v = avctx->priv_data; |
777 | 61.9k | int i; |
778 | | |
779 | 61.9k | av_frame_free(&v->sprite_output_frame); |
780 | | |
781 | 309k | for (i = 0; i < 4; i++) |
782 | 247k | av_freep(&v->sr_rows[i >> 1][i & 1]); |
783 | 61.9k | ff_mpv_common_end(&v->s); |
784 | 61.9k | memset(v->s.block_index, 0, sizeof(v->s.block_index)); |
785 | 61.9k | av_freep(&v->mv_type_mb_plane); |
786 | 61.9k | av_freep(&v->direct_mb_plane); |
787 | 61.9k | av_freep(&v->forward_mb_plane); |
788 | 61.9k | av_freep(&v->fieldtx_plane); |
789 | 61.9k | av_freep(&v->acpred_plane); |
790 | 61.9k | av_freep(&v->over_flags_plane); |
791 | 61.9k | av_freep(&v->mb_type_base); |
792 | 61.9k | av_freep(&v->blk_mv_type_base); |
793 | 61.9k | av_freep(&v->mv_f_base); |
794 | 61.9k | av_freep(&v->mv_f_next_base); |
795 | 61.9k | av_freep(&v->block); |
796 | 61.9k | av_freep(&v->cbp_base); |
797 | 61.9k | av_freep(&v->ttblk_base); |
798 | 61.9k | av_freep(&v->is_intra_base); // FIXME use v->mb_type[] |
799 | 61.9k | av_freep(&v->luma_mv_base); |
800 | 61.9k | ff_intrax8_common_end(&v->x8); |
801 | 61.9k | } |
802 | | |
803 | | /** |
804 | | * Close a MSS2/VC1/WMV3 decoder |
805 | | */ |
806 | | av_cold int ff_vc1_decode_end(AVCodecContext *avctx) |
807 | 31.8k | { |
808 | 31.8k | vc1_decode_reset(avctx); |
809 | 31.8k | return ff_mpv_decode_close(avctx); |
810 | 31.8k | } |
811 | | |
812 | | /** Decode a VC1/WMV3 frame |
813 | | * @todo TODO: Handle VC-1 IDUs (Transport level?) |
814 | | */ |
815 | | static int vc1_decode_frame(AVCodecContext *avctx, AVFrame *pict, |
816 | | int *got_frame, AVPacket *avpkt) |
817 | 1.09M | { |
818 | 1.09M | const uint8_t *buf = avpkt->data; |
819 | 1.09M | int buf_size = avpkt->size, n_slices = 0, i, ret; |
820 | 1.09M | VC1Context *v = avctx->priv_data; |
821 | 1.09M | MpegEncContext *s = &v->s; |
822 | 1.09M | uint8_t *buf2 = NULL; |
823 | 1.09M | const uint8_t *buf_start = buf, *buf_start_second_field = NULL; |
824 | 1.09M | int mb_height, n_slices1=-1; |
825 | 1.09M | struct { |
826 | 1.09M | uint8_t *buf; |
827 | 1.09M | GetBitContext gb; |
828 | 1.09M | int mby_start; |
829 | 1.09M | const uint8_t *rawbuf; |
830 | 1.09M | int raw_size; |
831 | 1.09M | } *slices = NULL, *tmp; |
832 | 1.09M | unsigned slices_allocated = 0; |
833 | | |
834 | 1.09M | v->second_field = 0; |
835 | | |
836 | 1.09M | if(s->avctx->flags & AV_CODEC_FLAG_LOW_DELAY) |
837 | 0 | s->low_delay = 1; |
838 | | |
839 | 1.09M | if (buf_size >= 4 && AV_RB32(&buf[buf_size-4]) == VC1_CODE_ENDOFSEQ) |
840 | 7.62k | buf_size -= 4; |
841 | | |
842 | | /* no supplementary picture */ |
843 | 1.09M | if (buf_size == 0) { |
844 | | /* special case for last picture */ |
845 | 35.7k | if (s->low_delay == 0 && s->next_pic.ptr) { |
846 | 12.3k | if ((ret = av_frame_ref(pict, s->next_pic.ptr->f)) < 0) |
847 | 0 | return ret; |
848 | 12.3k | ff_mpv_unref_picture(&s->next_pic); |
849 | | |
850 | 12.3k | *got_frame = 1; |
851 | 12.3k | } |
852 | | |
853 | 35.7k | return buf_size; |
854 | 35.7k | } |
855 | | |
856 | | //for advanced profile we may need to parse and unescape data |
857 | 1.06M | if (avctx->codec_id == AV_CODEC_ID_VC1 || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) { |
858 | 701k | int buf_size2 = 0; |
859 | 701k | size_t next_allocated = 0; |
860 | 701k | buf2 = av_mallocz(buf_size + AV_INPUT_BUFFER_PADDING_SIZE); |
861 | 701k | if (!buf2) |
862 | 0 | return AVERROR(ENOMEM); |
863 | | |
864 | 701k | if (IS_MARKER(AV_RB32(buf))) { /* frame starts with marker and needs to be parsed */ |
865 | 449k | const uint8_t *start, *end, *next; |
866 | 449k | int size; |
867 | | |
868 | 449k | next = buf; |
869 | 2.12M | for (start = buf, end = buf + buf_size; next < end; start = next) { |
870 | 1.67M | next = find_next_marker(start + 4, end); |
871 | 1.67M | size = next - start - 4; |
872 | 1.67M | if (size <= 0) continue; |
873 | 1.65M | switch (AV_RB32(start)) { |
874 | 385k | case VC1_CODE_FRAME: |
875 | 385k | buf_start = start; |
876 | 385k | buf_size2 = v->vc1dsp.vc1_unescape_buffer(start + 4, size, buf2); |
877 | 385k | break; |
878 | 157k | case VC1_CODE_FIELD: { |
879 | 157k | int buf_size3; |
880 | 157k | buf_start_second_field = start; |
881 | 157k | av_size_mult(sizeof(*slices), n_slices+1, &next_allocated); |
882 | 157k | tmp = next_allocated ? av_fast_realloc(slices, &slices_allocated, next_allocated) : NULL; |
883 | 157k | if (!tmp) { |
884 | 0 | ret = AVERROR(ENOMEM); |
885 | 0 | goto err; |
886 | 0 | } |
887 | 157k | slices = tmp; |
888 | 157k | slices[n_slices].buf = av_mallocz(size + AV_INPUT_BUFFER_PADDING_SIZE); |
889 | 157k | if (!slices[n_slices].buf) { |
890 | 0 | ret = AVERROR(ENOMEM); |
891 | 0 | goto err; |
892 | 0 | } |
893 | 157k | buf_size3 = v->vc1dsp.vc1_unescape_buffer(start + 4, size, |
894 | 157k | slices[n_slices].buf); |
895 | 157k | ret = init_get_bits8(&slices[n_slices].gb, slices[n_slices].buf, buf_size3); |
896 | 157k | if (ret < 0) |
897 | 0 | goto err; |
898 | 157k | slices[n_slices].mby_start = avctx->coded_height + 31 >> 5; |
899 | 157k | slices[n_slices].rawbuf = start; |
900 | 157k | slices[n_slices].raw_size = size + 4; |
901 | 157k | n_slices1 = n_slices - 1; // index of the last slice of the first field |
902 | 157k | n_slices++; |
903 | 157k | break; |
904 | 157k | } |
905 | 153k | case VC1_CODE_ENTRYPOINT: /* it should be before frame data */ |
906 | 153k | buf_size2 = v->vc1dsp.vc1_unescape_buffer(start + 4, size, buf2); |
907 | 153k | ret = init_get_bits8(&v->gb, buf2, buf_size2); |
908 | 153k | if (ret < 0) |
909 | 0 | goto err; |
910 | 153k | ff_vc1_decode_entry_point(avctx, v, &v->gb); |
911 | 153k | break; |
912 | 425k | case VC1_CODE_SLICE: { |
913 | 425k | int buf_size3; |
914 | 425k | av_size_mult(sizeof(*slices), n_slices+1, &next_allocated); |
915 | 425k | tmp = next_allocated ? av_fast_realloc(slices, &slices_allocated, next_allocated) : NULL; |
916 | 425k | if (!tmp) { |
917 | 0 | ret = AVERROR(ENOMEM); |
918 | 0 | goto err; |
919 | 0 | } |
920 | 425k | slices = tmp; |
921 | 425k | slices[n_slices].buf = av_mallocz(size + AV_INPUT_BUFFER_PADDING_SIZE); |
922 | 425k | if (!slices[n_slices].buf) { |
923 | 0 | ret = AVERROR(ENOMEM); |
924 | 0 | goto err; |
925 | 0 | } |
926 | 425k | buf_size3 = v->vc1dsp.vc1_unescape_buffer(start + 4, size, |
927 | 425k | slices[n_slices].buf); |
928 | 425k | ret = init_get_bits8(&slices[n_slices].gb, slices[n_slices].buf, buf_size3); |
929 | 425k | if (ret < 0) |
930 | 0 | goto err; |
931 | 425k | slices[n_slices].mby_start = get_bits(&slices[n_slices].gb, 9); |
932 | 425k | slices[n_slices].rawbuf = start; |
933 | 425k | slices[n_slices].raw_size = size + 4; |
934 | 425k | n_slices++; |
935 | 425k | break; |
936 | 425k | } |
937 | 1.65M | } |
938 | 1.65M | } |
939 | 449k | } else if (v->interlace && ((buf[0] & 0xC0) == 0xC0)) { /* WVC1 interlaced stores both fields divided by marker */ |
940 | 16.4k | const uint8_t *divider; |
941 | 16.4k | int buf_size3; |
942 | | |
943 | 16.4k | divider = find_next_marker(buf, buf + buf_size); |
944 | 16.4k | if ((divider == (buf + buf_size)) || AV_RB32(divider) != VC1_CODE_FIELD) { |
945 | 11.9k | av_log(avctx, AV_LOG_ERROR, "Error in WVC1 interlaced frame\n"); |
946 | 11.9k | ret = AVERROR_INVALIDDATA; |
947 | 11.9k | goto err; |
948 | 11.9k | } else { // found field marker, unescape second field |
949 | 4.49k | buf_start_second_field = divider; |
950 | 4.49k | av_size_mult(sizeof(*slices), n_slices+1, &next_allocated); |
951 | 4.49k | tmp = next_allocated ? av_fast_realloc(slices, &slices_allocated, next_allocated) : NULL; |
952 | 4.49k | if (!tmp) { |
953 | 0 | ret = AVERROR(ENOMEM); |
954 | 0 | goto err; |
955 | 0 | } |
956 | 4.49k | slices = tmp; |
957 | 4.49k | slices[n_slices].buf = av_mallocz(buf_size + AV_INPUT_BUFFER_PADDING_SIZE); |
958 | 4.49k | if (!slices[n_slices].buf) { |
959 | 0 | ret = AVERROR(ENOMEM); |
960 | 0 | goto err; |
961 | 0 | } |
962 | 4.49k | buf_size3 = v->vc1dsp.vc1_unescape_buffer(divider + 4, buf + buf_size - divider - 4, slices[n_slices].buf); |
963 | 4.49k | ret = init_get_bits8(&slices[n_slices].gb, slices[n_slices].buf, buf_size3); |
964 | 4.49k | if (ret < 0) |
965 | 0 | goto err; |
966 | 4.49k | slices[n_slices].mby_start = s->mb_height + 1 >> 1; |
967 | 4.49k | slices[n_slices].rawbuf = divider; |
968 | 4.49k | slices[n_slices].raw_size = buf + buf_size - divider; |
969 | 4.49k | n_slices1 = n_slices - 1; |
970 | 4.49k | n_slices++; |
971 | 4.49k | } |
972 | 4.49k | buf_size2 = v->vc1dsp.vc1_unescape_buffer(buf, divider - buf, buf2); |
973 | 235k | } else { |
974 | 235k | buf_size2 = v->vc1dsp.vc1_unescape_buffer(buf, buf_size, buf2); |
975 | 235k | } |
976 | 689k | ret = init_get_bits8(&v->gb, buf2, buf_size2); |
977 | 689k | if (ret < 0) |
978 | 0 | goto err; |
979 | 689k | } else{ |
980 | 361k | ret = init_get_bits8(&v->gb, buf, buf_size); |
981 | 361k | if (ret < 0) |
982 | 0 | goto err; |
983 | 361k | } |
984 | | |
985 | 1.05M | if (v->res_sprite) { |
986 | 357k | v->new_sprite = !get_bits1(&v->gb); |
987 | 357k | v->two_sprites = get_bits1(&v->gb); |
988 | | /* res_sprite means a Windows Media Image stream, AV_CODEC_ID_*IMAGE means |
989 | | we're using the sprite compositor. These are intentionally kept separate |
990 | | so you can get the raw sprites by using the wmv3 decoder for WMVP or |
991 | | the vc1 one for WVP2 */ |
992 | 357k | if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) { |
993 | 337k | if (v->new_sprite) { |
994 | | // switch AVCodecContext parameters to those of the sprites |
995 | 245k | avctx->width = avctx->coded_width = v->sprite_width; |
996 | 245k | avctx->height = avctx->coded_height = v->sprite_height; |
997 | 245k | } else { |
998 | 92.6k | goto image; |
999 | 92.6k | } |
1000 | 337k | } |
1001 | 357k | } |
1002 | | |
1003 | 957k | if (s->context_initialized && |
1004 | 912k | (s->width != avctx->coded_width || |
1005 | 894k | s->height != avctx->coded_height)) { |
1006 | 30.1k | vc1_decode_reset(avctx); |
1007 | 30.1k | } |
1008 | | |
1009 | 957k | if (!s->context_initialized) { |
1010 | 75.9k | ret = ff_vc1_decode_init(avctx); |
1011 | 75.9k | if (ret < 0) |
1012 | 17.3k | goto err; |
1013 | | |
1014 | 58.5k | s->low_delay = !avctx->has_b_frames || v->res_sprite; |
1015 | | |
1016 | 58.5k | if (v->profile == PROFILE_ADVANCED) { |
1017 | 28.9k | if(avctx->coded_width<=1 || avctx->coded_height<=1) { |
1018 | 469 | ret = AVERROR_INVALIDDATA; |
1019 | 469 | goto err; |
1020 | 469 | } |
1021 | 28.4k | s->h_edge_pos = avctx->coded_width; |
1022 | 28.4k | s->v_edge_pos = avctx->coded_height; |
1023 | 28.4k | } |
1024 | 58.5k | } |
1025 | | |
1026 | | // do parse frame header |
1027 | 940k | v->pic_header_flag = 0; |
1028 | 940k | v->first_pic_header_flag = 1; |
1029 | 940k | if (v->profile < PROFILE_ADVANCED) { |
1030 | 589k | if ((ret = ff_vc1_parse_frame_header(v, &v->gb)) < 0) { |
1031 | 81.0k | goto err; |
1032 | 81.0k | } |
1033 | 589k | } else { |
1034 | 350k | if ((ret = ff_vc1_parse_frame_header_adv(v, &v->gb)) < 0) { |
1035 | 51.0k | goto err; |
1036 | 51.0k | } |
1037 | 350k | } |
1038 | 808k | v->first_pic_header_flag = 0; |
1039 | | |
1040 | 808k | if (avctx->debug & FF_DEBUG_PICT_INFO) |
1041 | 0 | av_log(v->s.avctx, AV_LOG_DEBUG, "pict_type: %c\n", av_get_picture_type_char(s->pict_type)); |
1042 | | |
1043 | 808k | if ((avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) |
1044 | 198k | && s->pict_type != AV_PICTURE_TYPE_I) { |
1045 | 48.6k | av_log(v->s.avctx, AV_LOG_ERROR, "Sprite decoder: expected I-frame\n"); |
1046 | 48.6k | ret = AVERROR_INVALIDDATA; |
1047 | 48.6k | goto err; |
1048 | 48.6k | } |
1049 | 759k | if ((avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) |
1050 | 149k | && v->field_mode) { |
1051 | 562 | av_log(v->s.avctx, AV_LOG_ERROR, "Sprite decoder: expected Frames not Fields\n"); |
1052 | 562 | ret = AVERROR_INVALIDDATA; |
1053 | 562 | goto err; |
1054 | 562 | } |
1055 | 758k | if ((s->mb_height >> v->field_mode) == 0) { |
1056 | 0 | av_log(v->s.avctx, AV_LOG_ERROR, "image too short\n"); |
1057 | 0 | ret = AVERROR_INVALIDDATA; |
1058 | 0 | goto err; |
1059 | 0 | } |
1060 | | |
1061 | | /* skip B-frames if we don't have reference frames */ |
1062 | 758k | if (!s->last_pic.ptr && s->pict_type == AV_PICTURE_TYPE_B) { |
1063 | 48.0k | av_log(v->s.avctx, AV_LOG_DEBUG, "Skipping B frame without reference frames\n"); |
1064 | 48.0k | goto end; |
1065 | 48.0k | } |
1066 | 710k | if ((avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B) || |
1067 | 710k | (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I) || |
1068 | 707k | avctx->skip_frame >= AVDISCARD_ALL) { |
1069 | 4.41k | goto end; |
1070 | 4.41k | } |
1071 | | |
1072 | 706k | if ((ret = ff_mpv_frame_start(s, avctx)) < 0) { |
1073 | 4.70k | goto err; |
1074 | 4.70k | } |
1075 | | |
1076 | 701k | v->s.cur_pic.ptr->field_picture = v->field_mode; |
1077 | 701k | v->s.cur_pic.ptr->f->flags |= AV_FRAME_FLAG_INTERLACED * (v->fcm != PROGRESSIVE); |
1078 | 701k | v->s.cur_pic.ptr->f->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST * !!v->tff; |
1079 | 701k | v->last_interlaced = v->s.last_pic.ptr ? v->s.last_pic.ptr->f->flags & AV_FRAME_FLAG_INTERLACED : 0; |
1080 | 701k | v->next_interlaced = v->s.next_pic.ptr ? v->s.next_pic.ptr->f->flags & AV_FRAME_FLAG_INTERLACED : 0; |
1081 | | |
1082 | | // process pulldown flags |
1083 | 701k | s->cur_pic.ptr->f->repeat_pict = 0; |
1084 | | // Pulldown flags are only valid when 'broadcast' has been set. |
1085 | 701k | if (v->rff) { |
1086 | | // repeat field |
1087 | 76.6k | s->cur_pic.ptr->f->repeat_pict = 1; |
1088 | 625k | } else if (v->rptfrm) { |
1089 | | // repeat frames |
1090 | 6.51k | s->cur_pic.ptr->f->repeat_pict = v->rptfrm * 2; |
1091 | 6.51k | } |
1092 | | |
1093 | 701k | if (avctx->hwaccel) { |
1094 | 0 | const FFHWAccel *hwaccel = ffhwaccel(avctx->hwaccel); |
1095 | 0 | s->mb_y = 0; |
1096 | 0 | if (v->field_mode && buf_start_second_field) { |
1097 | | // decode first field |
1098 | 0 | s->picture_structure = PICT_BOTTOM_FIELD - v->tff; |
1099 | 0 | ret = hwaccel->start_frame(avctx, avpkt->buf, buf_start, |
1100 | 0 | buf_start_second_field - buf_start); |
1101 | 0 | if (ret < 0) |
1102 | 0 | goto err; |
1103 | | |
1104 | 0 | if (n_slices1 == -1) { |
1105 | | // no slices, decode the field as-is |
1106 | 0 | ret = hwaccel->decode_slice(avctx, buf_start, |
1107 | 0 | buf_start_second_field - buf_start); |
1108 | 0 | if (ret < 0) |
1109 | 0 | goto err; |
1110 | 0 | } else { |
1111 | 0 | ret = hwaccel->decode_slice(avctx, buf_start, |
1112 | 0 | slices[0].rawbuf - buf_start); |
1113 | 0 | if (ret < 0) |
1114 | 0 | goto err; |
1115 | | |
1116 | 0 | for (i = 0 ; i < n_slices1 + 1; i++) { |
1117 | 0 | v->gb = slices[i].gb; |
1118 | 0 | s->mb_y = slices[i].mby_start; |
1119 | |
|
1120 | 0 | v->pic_header_flag = get_bits1(&v->gb); |
1121 | 0 | if (v->pic_header_flag) { |
1122 | 0 | if (ff_vc1_parse_frame_header_adv(v, &v->gb) < 0) { |
1123 | 0 | av_log(v->s.avctx, AV_LOG_ERROR, "Slice header damaged\n"); |
1124 | 0 | ret = AVERROR_INVALIDDATA; |
1125 | 0 | if (avctx->err_recognition & AV_EF_EXPLODE) |
1126 | 0 | goto err; |
1127 | 0 | continue; |
1128 | 0 | } |
1129 | 0 | } |
1130 | | |
1131 | 0 | ret = hwaccel->decode_slice(avctx, slices[i].rawbuf, |
1132 | 0 | slices[i].raw_size); |
1133 | 0 | if (ret < 0) |
1134 | 0 | goto err; |
1135 | 0 | } |
1136 | 0 | } |
1137 | | |
1138 | 0 | if ((ret = hwaccel->end_frame(avctx)) < 0) |
1139 | 0 | goto err; |
1140 | | |
1141 | | // decode second field |
1142 | 0 | v->gb = slices[n_slices1 + 1].gb; |
1143 | 0 | s->mb_y = slices[n_slices1 + 1].mby_start; |
1144 | 0 | s->picture_structure = PICT_TOP_FIELD + v->tff; |
1145 | 0 | v->second_field = 1; |
1146 | 0 | v->pic_header_flag = 0; |
1147 | 0 | if (ff_vc1_parse_frame_header_adv(v, &v->gb) < 0) { |
1148 | 0 | av_log(avctx, AV_LOG_ERROR, "parsing header for second field failed"); |
1149 | 0 | ret = AVERROR_INVALIDDATA; |
1150 | 0 | goto err; |
1151 | 0 | } |
1152 | 0 | v->s.cur_pic.ptr->f->pict_type = v->s.pict_type; |
1153 | |
|
1154 | 0 | ret = hwaccel->start_frame(avctx, avpkt->buf, buf_start_second_field, |
1155 | 0 | (buf + buf_size) - buf_start_second_field); |
1156 | 0 | if (ret < 0) |
1157 | 0 | goto err; |
1158 | | |
1159 | 0 | if (n_slices - n_slices1 == 2) { |
1160 | | // no slices, decode the field as-is |
1161 | 0 | ret = hwaccel->decode_slice(avctx, buf_start_second_field, |
1162 | 0 | (buf + buf_size) - buf_start_second_field); |
1163 | 0 | if (ret < 0) |
1164 | 0 | goto err; |
1165 | 0 | } else { |
1166 | 0 | ret = hwaccel->decode_slice(avctx, buf_start_second_field, |
1167 | 0 | slices[n_slices1 + 2].rawbuf - buf_start_second_field); |
1168 | 0 | if (ret < 0) |
1169 | 0 | goto err; |
1170 | | |
1171 | 0 | for (i = n_slices1 + 2; i < n_slices; i++) { |
1172 | 0 | v->gb = slices[i].gb; |
1173 | 0 | s->mb_y = slices[i].mby_start; |
1174 | |
|
1175 | 0 | v->pic_header_flag = get_bits1(&v->gb); |
1176 | 0 | if (v->pic_header_flag) { |
1177 | 0 | if (ff_vc1_parse_frame_header_adv(v, &v->gb) < 0) { |
1178 | 0 | av_log(v->s.avctx, AV_LOG_ERROR, "Slice header damaged\n"); |
1179 | 0 | ret = AVERROR_INVALIDDATA; |
1180 | 0 | if (avctx->err_recognition & AV_EF_EXPLODE) |
1181 | 0 | goto err; |
1182 | 0 | continue; |
1183 | 0 | } |
1184 | 0 | } |
1185 | | |
1186 | 0 | ret = hwaccel->decode_slice(avctx, slices[i].rawbuf, |
1187 | 0 | slices[i].raw_size); |
1188 | 0 | if (ret < 0) |
1189 | 0 | goto err; |
1190 | 0 | } |
1191 | 0 | } |
1192 | | |
1193 | 0 | if ((ret = hwaccel->end_frame(avctx)) < 0) |
1194 | 0 | goto err; |
1195 | 0 | } else { |
1196 | 0 | s->picture_structure = PICT_FRAME; |
1197 | 0 | ret = hwaccel->start_frame(avctx, avpkt->buf, buf_start, |
1198 | 0 | (buf + buf_size) - buf_start); |
1199 | 0 | if (ret < 0) |
1200 | 0 | goto err; |
1201 | | |
1202 | 0 | if (n_slices == 0) { |
1203 | | // no slices, decode the frame as-is |
1204 | 0 | ret = hwaccel->decode_slice(avctx, buf_start, |
1205 | 0 | (buf + buf_size) - buf_start); |
1206 | 0 | if (ret < 0) |
1207 | 0 | goto err; |
1208 | 0 | } else { |
1209 | | // decode the frame part as the first slice |
1210 | 0 | ret = hwaccel->decode_slice(avctx, buf_start, |
1211 | 0 | slices[0].rawbuf - buf_start); |
1212 | 0 | if (ret < 0) |
1213 | 0 | goto err; |
1214 | | |
1215 | | // and process the slices as additional slices afterwards |
1216 | 0 | for (i = 0 ; i < n_slices; i++) { |
1217 | 0 | v->gb = slices[i].gb; |
1218 | 0 | s->mb_y = slices[i].mby_start; |
1219 | |
|
1220 | 0 | v->pic_header_flag = get_bits1(&v->gb); |
1221 | 0 | if (v->pic_header_flag) { |
1222 | 0 | if (ff_vc1_parse_frame_header_adv(v, &v->gb) < 0) { |
1223 | 0 | av_log(v->s.avctx, AV_LOG_ERROR, "Slice header damaged\n"); |
1224 | 0 | ret = AVERROR_INVALIDDATA; |
1225 | 0 | if (avctx->err_recognition & AV_EF_EXPLODE) |
1226 | 0 | goto err; |
1227 | 0 | continue; |
1228 | 0 | } |
1229 | 0 | } |
1230 | | |
1231 | 0 | ret = hwaccel->decode_slice(avctx, slices[i].rawbuf, |
1232 | 0 | slices[i].raw_size); |
1233 | 0 | if (ret < 0) |
1234 | 0 | goto err; |
1235 | 0 | } |
1236 | 0 | } |
1237 | 0 | if ((ret = hwaccel->end_frame(avctx)) < 0) |
1238 | 0 | goto err; |
1239 | 0 | } |
1240 | 701k | } else { |
1241 | 701k | int header_ret = 0; |
1242 | | |
1243 | 701k | ff_mpeg_er_frame_start(s); |
1244 | | |
1245 | 701k | v->end_mb_x = s->mb_width; |
1246 | 701k | if (v->field_mode) { |
1247 | 45.1k | s->cur_pic.linesize[0] <<= 1; |
1248 | 45.1k | s->cur_pic.linesize[1] <<= 1; |
1249 | 45.1k | s->cur_pic.linesize[2] <<= 1; |
1250 | 45.1k | s->linesize <<= 1; |
1251 | 45.1k | s->uvlinesize <<= 1; |
1252 | 45.1k | } |
1253 | 701k | mb_height = s->mb_height >> v->field_mode; |
1254 | | |
1255 | 701k | av_assert0 (mb_height > 0); |
1256 | | |
1257 | 1.93M | for (i = 0; i <= n_slices; i++) { |
1258 | 1.23M | if (i > 0 && slices[i - 1].mby_start >= mb_height) { |
1259 | 62.6k | if (v->field_mode <= 0) { |
1260 | 28.1k | av_log(v->s.avctx, AV_LOG_ERROR, "Slice %d starts beyond " |
1261 | 28.1k | "picture boundary (%d >= %d)\n", i, |
1262 | 28.1k | slices[i - 1].mby_start, mb_height); |
1263 | 28.1k | continue; |
1264 | 28.1k | } |
1265 | 34.5k | v->second_field = 1; |
1266 | 34.5k | av_assert0((s->mb_height & 1) == 0); |
1267 | 34.5k | v->blocks_off = s->b8_stride * (s->mb_height&~1); |
1268 | 34.5k | v->mb_off = s->mb_stride * s->mb_height >> 1; |
1269 | 1.17M | } else { |
1270 | 1.17M | v->second_field = 0; |
1271 | 1.17M | v->blocks_off = 0; |
1272 | 1.17M | v->mb_off = 0; |
1273 | 1.17M | } |
1274 | 1.21M | if (i) { |
1275 | 509k | v->pic_header_flag = 0; |
1276 | 509k | if (v->field_mode && i == n_slices1 + 2) { |
1277 | 31.7k | if ((header_ret = ff_vc1_parse_frame_header_adv(v, &v->gb)) < 0) { |
1278 | 2.85k | av_log(v->s.avctx, AV_LOG_ERROR, "Field header damaged\n"); |
1279 | 2.85k | ret = AVERROR_INVALIDDATA; |
1280 | 2.85k | if (avctx->err_recognition & AV_EF_EXPLODE) |
1281 | 424 | goto err; |
1282 | 2.42k | continue; |
1283 | 2.85k | } |
1284 | 477k | } else if (get_bits1(&v->gb)) { |
1285 | 98.1k | v->pic_header_flag = 1; |
1286 | 98.1k | if ((header_ret = ff_vc1_parse_frame_header_adv(v, &v->gb)) < 0) { |
1287 | 13.8k | av_log(v->s.avctx, AV_LOG_ERROR, "Slice header damaged\n"); |
1288 | 13.8k | ret = AVERROR_INVALIDDATA; |
1289 | 13.8k | if (avctx->err_recognition & AV_EF_EXPLODE) |
1290 | 899 | goto err; |
1291 | 12.9k | continue; |
1292 | 13.8k | } |
1293 | 98.1k | } |
1294 | 509k | } |
1295 | 1.19M | if (header_ret < 0) |
1296 | 113k | continue; |
1297 | 1.08M | s->start_mb_y = (i == 0) ? 0 : FFMAX(0, slices[i-1].mby_start % mb_height); |
1298 | 1.08M | if (!v->field_mode || v->second_field) |
1299 | 1.03M | s->end_mb_y = (i == n_slices ) ? mb_height : FFMIN(mb_height, slices[i].mby_start % mb_height); |
1300 | 47.1k | else { |
1301 | 47.1k | if (i >= n_slices) { |
1302 | 14.8k | av_log(v->s.avctx, AV_LOG_ERROR, "first field slice count too large\n"); |
1303 | 14.8k | continue; |
1304 | 14.8k | } |
1305 | 32.2k | s->end_mb_y = (i == n_slices1 + 1) ? mb_height : FFMIN(mb_height, slices[i].mby_start % mb_height); |
1306 | 32.2k | } |
1307 | 1.06M | if (s->end_mb_y <= s->start_mb_y) { |
1308 | 271k | av_log(v->s.avctx, AV_LOG_ERROR, "end mb y %d %d invalid\n", s->end_mb_y, s->start_mb_y); |
1309 | 271k | continue; |
1310 | 271k | } |
1311 | 795k | if (((s->pict_type == AV_PICTURE_TYPE_P && !v->p_frame_skipped) || |
1312 | 502k | (s->pict_type == AV_PICTURE_TYPE_B && !v->bi_type)) && |
1313 | 409k | !v->cbpcy_vlc) { |
1314 | 0 | av_log(v->s.avctx, AV_LOG_ERROR, "missing cbpcy_vlc\n"); |
1315 | 0 | continue; |
1316 | 0 | } |
1317 | 795k | ff_vc1_decode_blocks(v); |
1318 | 795k | if (i != n_slices) { |
1319 | 123k | v->gb = slices[i].gb; |
1320 | 123k | } |
1321 | 795k | } |
1322 | 700k | if (v->field_mode) { |
1323 | 44.6k | v->second_field = 0; |
1324 | 44.6k | s->cur_pic.linesize[0] >>= 1; |
1325 | 44.6k | s->cur_pic.linesize[1] >>= 1; |
1326 | 44.6k | s->cur_pic.linesize[2] >>= 1; |
1327 | 44.6k | s->linesize >>= 1; |
1328 | 44.6k | s->uvlinesize >>= 1; |
1329 | 44.6k | if (v->s.pict_type != AV_PICTURE_TYPE_BI && v->s.pict_type != AV_PICTURE_TYPE_B) { |
1330 | 33.9k | FFSWAP(uint8_t *, v->mv_f_next[0], v->mv_f[0]); |
1331 | 33.9k | FFSWAP(uint8_t *, v->mv_f_next[1], v->mv_f[1]); |
1332 | 33.9k | } |
1333 | 44.6k | } |
1334 | 700k | ff_dlog(s->avctx, "Consumed %i/%i bits\n", |
1335 | 700k | get_bits_count(&v->gb), v->gb.size_in_bits); |
1336 | | // if (get_bits_count(&v->gb) > buf_size * 8) |
1337 | | // return -1; |
1338 | 700k | if(s->er.error_occurred && s->pict_type == AV_PICTURE_TYPE_B) { |
1339 | 69.9k | ret = AVERROR_INVALIDDATA; |
1340 | 69.9k | goto err; |
1341 | 69.9k | } |
1342 | 630k | if ( !v->field_mode |
1343 | 590k | && avctx->codec_id != AV_CODEC_ID_WMV3IMAGE |
1344 | 557k | && avctx->codec_id != AV_CODEC_ID_VC1IMAGE) |
1345 | 461k | ff_er_frame_end(&s->er, NULL); |
1346 | 630k | } |
1347 | | |
1348 | 630k | ff_mpv_frame_end(s); |
1349 | | |
1350 | 630k | if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) { |
1351 | 220k | image: |
1352 | 220k | avctx->width = avctx->coded_width = v->output_width; |
1353 | 220k | avctx->height = avctx->coded_height = v->output_height; |
1354 | 220k | if (avctx->skip_frame >= AVDISCARD_NONREF) |
1355 | 12.1k | goto end; |
1356 | 208k | if (!v->sprite_output_frame && |
1357 | 8.86k | !(v->sprite_output_frame = av_frame_alloc())) { |
1358 | 0 | ret = AVERROR(ENOMEM); |
1359 | 0 | goto err; |
1360 | 0 | } |
1361 | 208k | #if CONFIG_WMV3IMAGE_DECODER || CONFIG_VC1IMAGE_DECODER |
1362 | 208k | if ((ret = vc1_decode_sprites(v, &v->gb)) < 0) |
1363 | 126k | goto err; |
1364 | 82.7k | #endif |
1365 | 82.7k | if ((ret = av_frame_ref(pict, v->sprite_output_frame)) < 0) |
1366 | 0 | goto err; |
1367 | 82.7k | *got_frame = 1; |
1368 | 502k | } else { |
1369 | 502k | if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) { |
1370 | 76.6k | if ((ret = av_frame_ref(pict, s->cur_pic.ptr->f)) < 0) |
1371 | 0 | goto err; |
1372 | 76.6k | ff_print_debug_info(s, s->cur_pic.ptr, pict); |
1373 | 76.6k | *got_frame = 1; |
1374 | 425k | } else if (s->last_pic.ptr) { |
1375 | 309k | if ((ret = av_frame_ref(pict, s->last_pic.ptr->f)) < 0) |
1376 | 0 | goto err; |
1377 | 309k | ff_print_debug_info(s, s->last_pic.ptr, pict); |
1378 | 309k | *got_frame = 1; |
1379 | 309k | } |
1380 | 502k | } |
1381 | | |
1382 | 649k | end: |
1383 | 649k | ret = buf_size; |
1384 | 1.06M | err: |
1385 | 1.06M | av_free(buf2); |
1386 | 1.64M | for (i = 0; i < n_slices; i++) |
1387 | 586k | av_free(slices[i].buf); |
1388 | 1.06M | av_free(slices); |
1389 | 1.06M | return ret; |
1390 | 649k | } |
1391 | | |
1392 | | |
1393 | | const FFCodec ff_vc1_decoder = { |
1394 | | .p.name = "vc1", |
1395 | | CODEC_LONG_NAME("SMPTE VC-1"), |
1396 | | .p.type = AVMEDIA_TYPE_VIDEO, |
1397 | | .p.id = AV_CODEC_ID_VC1, |
1398 | | .priv_data_size = sizeof(VC1Context), |
1399 | | .init = vc1_decode_init, |
1400 | | .close = ff_vc1_decode_end, |
1401 | | FF_CODEC_DECODE_CB(vc1_decode_frame), |
1402 | | .flush = ff_mpeg_flush, |
1403 | | .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY, |
1404 | | .hw_configs = (const AVCodecHWConfigInternal *const []) { |
1405 | | #if CONFIG_VC1_DXVA2_HWACCEL |
1406 | | HWACCEL_DXVA2(vc1), |
1407 | | #endif |
1408 | | #if CONFIG_VC1_D3D11VA_HWACCEL |
1409 | | HWACCEL_D3D11VA(vc1), |
1410 | | #endif |
1411 | | #if CONFIG_VC1_D3D11VA2_HWACCEL |
1412 | | HWACCEL_D3D11VA2(vc1), |
1413 | | #endif |
1414 | | #if CONFIG_VC1_D3D12VA_HWACCEL |
1415 | | HWACCEL_D3D12VA(vc1), |
1416 | | #endif |
1417 | | #if CONFIG_VC1_NVDEC_HWACCEL |
1418 | | HWACCEL_NVDEC(vc1), |
1419 | | #endif |
1420 | | #if CONFIG_VC1_VAAPI_HWACCEL |
1421 | | HWACCEL_VAAPI(vc1), |
1422 | | #endif |
1423 | | #if CONFIG_VC1_VDPAU_HWACCEL |
1424 | | HWACCEL_VDPAU(vc1), |
1425 | | #endif |
1426 | | NULL |
1427 | | }, |
1428 | | .p.profiles = NULL_IF_CONFIG_SMALL(ff_vc1_profiles) |
1429 | | }; |
1430 | | |
1431 | | #if CONFIG_WMV3_DECODER |
1432 | | const FFCodec ff_wmv3_decoder = { |
1433 | | .p.name = "wmv3", |
1434 | | CODEC_LONG_NAME("Windows Media Video 9"), |
1435 | | .p.type = AVMEDIA_TYPE_VIDEO, |
1436 | | .p.id = AV_CODEC_ID_WMV3, |
1437 | | .priv_data_size = sizeof(VC1Context), |
1438 | | .init = vc1_decode_init, |
1439 | | .close = ff_vc1_decode_end, |
1440 | | FF_CODEC_DECODE_CB(vc1_decode_frame), |
1441 | | .flush = ff_mpeg_flush, |
1442 | | .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY, |
1443 | | .hw_configs = (const AVCodecHWConfigInternal *const []) { |
1444 | | #if CONFIG_WMV3_DXVA2_HWACCEL |
1445 | | HWACCEL_DXVA2(wmv3), |
1446 | | #endif |
1447 | | #if CONFIG_WMV3_D3D11VA_HWACCEL |
1448 | | HWACCEL_D3D11VA(wmv3), |
1449 | | #endif |
1450 | | #if CONFIG_WMV3_D3D11VA2_HWACCEL |
1451 | | HWACCEL_D3D11VA2(wmv3), |
1452 | | #endif |
1453 | | #if CONFIG_WMV3_D3D12VA_HWACCEL |
1454 | | HWACCEL_D3D12VA(wmv3), |
1455 | | #endif |
1456 | | #if CONFIG_WMV3_NVDEC_HWACCEL |
1457 | | HWACCEL_NVDEC(wmv3), |
1458 | | #endif |
1459 | | #if CONFIG_WMV3_VAAPI_HWACCEL |
1460 | | HWACCEL_VAAPI(wmv3), |
1461 | | #endif |
1462 | | #if CONFIG_WMV3_VDPAU_HWACCEL |
1463 | | HWACCEL_VDPAU(wmv3), |
1464 | | #endif |
1465 | | NULL |
1466 | | }, |
1467 | | .p.profiles = NULL_IF_CONFIG_SMALL(ff_vc1_profiles) |
1468 | | }; |
1469 | | #endif |
1470 | | |
1471 | | #if CONFIG_WMV3IMAGE_DECODER |
1472 | | const FFCodec ff_wmv3image_decoder = { |
1473 | | .p.name = "wmv3image", |
1474 | | CODEC_LONG_NAME("Windows Media Video 9 Image"), |
1475 | | .p.type = AVMEDIA_TYPE_VIDEO, |
1476 | | .p.id = AV_CODEC_ID_WMV3IMAGE, |
1477 | | .priv_data_size = sizeof(VC1Context), |
1478 | | .init = vc1_decode_init, |
1479 | | .close = ff_vc1_decode_end, |
1480 | | FF_CODEC_DECODE_CB(vc1_decode_frame), |
1481 | | .p.capabilities = AV_CODEC_CAP_DR1, |
1482 | | .flush = vc1_sprite_flush, |
1483 | | }; |
1484 | | #endif |
1485 | | |
1486 | | #if CONFIG_VC1IMAGE_DECODER |
1487 | | const FFCodec ff_vc1image_decoder = { |
1488 | | .p.name = "vc1image", |
1489 | | CODEC_LONG_NAME("Windows Media Video 9 Image v2"), |
1490 | | .p.type = AVMEDIA_TYPE_VIDEO, |
1491 | | .p.id = AV_CODEC_ID_VC1IMAGE, |
1492 | | .priv_data_size = sizeof(VC1Context), |
1493 | | .init = vc1_decode_init, |
1494 | | .close = ff_vc1_decode_end, |
1495 | | FF_CODEC_DECODE_CB(vc1_decode_frame), |
1496 | | .p.capabilities = AV_CODEC_CAP_DR1, |
1497 | | .flush = vc1_sprite_flush, |
1498 | | }; |
1499 | | #endif |