D3D11va: add a Direct3D11 video decoder similar to DXVA2
[libav.git] / libavcodec / mpeg12dec.c
CommitLineData
1b6d6674
DB
1/*
2 * MPEG-1/2 decoder
3 * Copyright (c) 2000, 2001 Fabrice Bellard
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
5 *
6 * This file is part of Libav.
7 *
8 * Libav is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * Libav is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with Libav; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23/**
24 * @file
25 * MPEG-1/2 decoder
26 */
27
cc8163e1
DB
28#include <inttypes.h>
29
bd8ac882 30#include "libavutil/attributes.h"
1b6d6674 31#include "libavutil/internal.h"
bacc2869 32#include "libavutil/stereo3d.h"
a91d3658 33
1b6d6674 34#include "avcodec.h"
a91d3658 35#include "bytestream.h"
1b6d6674 36#include "error_resilience.h"
e3fcb143 37#include "idctdsp.h"
a91d3658 38#include "internal.h"
7b9ef8d7 39#include "mpeg_er.h"
1b6d6674
DB
40#include "mpeg12.h"
41#include "mpeg12data.h"
e0c16e4e 42#include "mpegutils.h"
a91d3658 43#include "mpegvideo.h"
1b6d6674 44#include "thread.h"
19e30a58 45#include "version.h"
a91d3658 46#include "xvmc_internal.h"
1b6d6674 47
e557584a
DB
48typedef struct Mpeg1Context {
49 MpegEncContext mpeg_enc_ctx;
50 int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
a91d3658
VG
51 int repeat_field; /* true if we must repeat the field */
52 AVPanScan pan_scan; /* some temporary storage for the panscan */
73eca738
JG
53 AVStereo3D stereo3d;
54 int has_stereo3d;
1eaac1d6
JS
55 uint8_t *a53_caption;
56 int a53_caption_size;
1ef9e837
KK
57 uint8_t afd;
58 int has_afd;
e557584a 59 int slice_count;
e557584a
DB
60 int save_aspect_info;
61 int save_width, save_height, save_progressive_seq;
a91d3658
VG
62 AVRational frame_rate_ext; /* MPEG-2 specific framerate modificator */
63 int sync; /* Did we reach a sync point like a GOP/SEQ/KEYFrame? */
64 int closed_gop; /* GOP is closed */
a097f004 65 int first_slice;
e557584a
DB
66 int extradata_decoded;
67} Mpeg1Context;
68
38f64c03
DB
69#define MB_TYPE_ZERO_MV 0x20000000
70
71static const uint32_t ptype2mb_type[7] = {
72 MB_TYPE_INTRA,
73 MB_TYPE_L0 | MB_TYPE_CBP | MB_TYPE_ZERO_MV | MB_TYPE_16x16,
74 MB_TYPE_L0,
75 MB_TYPE_L0 | MB_TYPE_CBP,
76 MB_TYPE_QUANT | MB_TYPE_INTRA,
77 MB_TYPE_QUANT | MB_TYPE_L0 | MB_TYPE_CBP | MB_TYPE_ZERO_MV | MB_TYPE_16x16,
78 MB_TYPE_QUANT | MB_TYPE_L0 | MB_TYPE_CBP,
79};
80
81static const uint32_t btype2mb_type[11] = {
82 MB_TYPE_INTRA,
83 MB_TYPE_L1,
84 MB_TYPE_L1 | MB_TYPE_CBP,
85 MB_TYPE_L0,
86 MB_TYPE_L0 | MB_TYPE_CBP,
87 MB_TYPE_L0L1,
88 MB_TYPE_L0L1 | MB_TYPE_CBP,
89 MB_TYPE_QUANT | MB_TYPE_INTRA,
90 MB_TYPE_QUANT | MB_TYPE_L1 | MB_TYPE_CBP,
91 MB_TYPE_QUANT | MB_TYPE_L0 | MB_TYPE_CBP,
92 MB_TYPE_QUANT | MB_TYPE_L0L1 | MB_TYPE_CBP,
93};
94
95static const uint8_t non_linear_qscale[32] = {
a91d3658
VG
96 0, 1, 2, 3, 4, 5, 6, 7,
97 8, 10, 12, 14, 16, 18, 20, 22,
98 24, 28, 32, 36, 40, 44, 48, 52,
99 56, 64, 72, 80, 88, 96, 104, 112,
38f64c03
DB
100};
101
1b6d6674
DB
102/* as H.263, but only 17 codes */
103static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred)
104{
105 int code, sign, val, shift;
106
107 code = get_vlc2(&s->gb, ff_mv_vlc.table, MV_VLC_BITS, 2);
a91d3658 108 if (code == 0)
1b6d6674 109 return pred;
a91d3658 110 if (code < 0)
1b6d6674 111 return 0xffff;
1b6d6674
DB
112
113 sign = get_bits1(&s->gb);
114 shift = fcode - 1;
115 val = code;
116 if (shift) {
117 val = (val - 1) << shift;
118 val |= get_bits(&s->gb, shift);
119 val++;
120 }
121 if (sign)
122 val = -val;
123 val += pred;
124
125 /* modulo decoding */
126 return sign_extend(val, 5 + shift);
127}
128
a91d3658
VG
129#define check_scantable_index(ctx, x) \
130 do { \
131 if ((x) > 63) { \
132 av_log(ctx->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", \
133 ctx->mb_x, ctx->mb_y); \
134 return AVERROR_INVALIDDATA; \
135 } \
136 } while (0)
137
138static inline int mpeg1_decode_block_intra(MpegEncContext *s,
139 int16_t *block, int n)
1b6d6674
DB
140{
141 int level, dc, diff, i, j, run;
142 int component;
a91d3658
VG
143 RLTable *rl = &ff_rl_mpeg1;
144 uint8_t *const scantable = s->intra_scantable.permutated;
1b6d6674
DB
145 const uint16_t *quant_matrix = s->intra_matrix;
146 const int qscale = s->qscale;
147
148 /* DC coefficient */
149 component = (n <= 3 ? 0 : n - 4 + 1);
150 diff = decode_dc(&s->gb, component);
151 if (diff >= 0xffff)
3a56bcee 152 return AVERROR_INVALIDDATA;
1b6d6674
DB
153 dc = s->last_dc[component];
154 dc += diff;
155 s->last_dc[component] = dc;
156 block[0] = dc * quant_matrix[0];
6a85dfc8 157 ff_dlog(s->avctx, "dc=%d diff=%d\n", dc, diff);
1b6d6674
DB
158 i = 0;
159 {
160 OPEN_READER(re, &s->gb);
161 /* now quantify & encode AC coefficients */
162 for (;;) {
163 UPDATE_CACHE(re, &s->gb);
a91d3658
VG
164 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
165 TEX_VLC_BITS, 2, 0);
1b6d6674
DB
166
167 if (level == 127) {
168 break;
169 } else if (level != 0) {
170 i += run;
6d93307f 171 check_scantable_index(s, i);
1b6d6674
DB
172 j = scantable[i];
173 level = (level * qscale * quant_matrix[j]) >> 4;
174 level = (level - 1) | 1;
a91d3658
VG
175 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
176 SHOW_SBITS(re, &s->gb, 1);
1b6d6674
DB
177 LAST_SKIP_BITS(re, &s->gb, 1);
178 } else {
179 /* escape */
a91d3658
VG
180 run = SHOW_UBITS(re, &s->gb, 6) + 1;
181 LAST_SKIP_BITS(re, &s->gb, 6);
1b6d6674 182 UPDATE_CACHE(re, &s->gb);
a91d3658
VG
183 level = SHOW_SBITS(re, &s->gb, 8);
184 SKIP_BITS(re, &s->gb, 8);
1b6d6674 185 if (level == -128) {
a91d3658
VG
186 level = SHOW_UBITS(re, &s->gb, 8) - 256;
187 LAST_SKIP_BITS(re, &s->gb, 8);
1b6d6674 188 } else if (level == 0) {
a91d3658
VG
189 level = SHOW_UBITS(re, &s->gb, 8);
190 LAST_SKIP_BITS(re, &s->gb, 8);
1b6d6674
DB
191 }
192 i += run;
6d93307f 193 check_scantable_index(s, i);
1b6d6674
DB
194 j = scantable[i];
195 if (level < 0) {
196 level = -level;
197 level = (level * qscale * quant_matrix[j]) >> 4;
198 level = (level - 1) | 1;
199 level = -level;
200 } else {
201 level = (level * qscale * quant_matrix[j]) >> 4;
202 level = (level - 1) | 1;
203 }
204 }
1b6d6674
DB
205
206 block[j] = level;
207 }
208 CLOSE_READER(re, &s->gb);
209 }
210 s->block_last_index[n] = i;
a91d3658 211 return 0;
1b6d6674
DB
212}
213
214int ff_mpeg1_decode_block_intra(MpegEncContext *s, int16_t *block, int n)
215{
216 return mpeg1_decode_block_intra(s, block, n);
217}
218
a91d3658
VG
219static inline int mpeg1_decode_block_inter(MpegEncContext *s,
220 int16_t *block, int n)
1b6d6674
DB
221{
222 int level, i, j, run;
a91d3658
VG
223 RLTable *rl = &ff_rl_mpeg1;
224 uint8_t *const scantable = s->intra_scantable.permutated;
1b6d6674
DB
225 const uint16_t *quant_matrix = s->inter_matrix;
226 const int qscale = s->qscale;
227
228 {
229 OPEN_READER(re, &s->gb);
230 i = -1;
231 // special case for first coefficient, no need to add second VLC table
232 UPDATE_CACHE(re, &s->gb);
a91d3658 233 if (((int32_t) GET_CACHE(re, &s->gb)) < 0) {
1b6d6674
DB
234 level = (3 * qscale * quant_matrix[0]) >> 5;
235 level = (level - 1) | 1;
236 if (GET_CACHE(re, &s->gb) & 0x40000000)
237 level = -level;
238 block[0] = level;
239 i++;
240 SKIP_BITS(re, &s->gb, 2);
a91d3658 241 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
1b6d6674
DB
242 goto end;
243 }
244 /* now quantify & encode AC coefficients */
245 for (;;) {
a91d3658
VG
246 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
247 TEX_VLC_BITS, 2, 0);
1b6d6674
DB
248
249 if (level != 0) {
250 i += run;
6d93307f 251 check_scantable_index(s, i);
1b6d6674
DB
252 j = scantable[i];
253 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
254 level = (level - 1) | 1;
a91d3658
VG
255 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
256 SHOW_SBITS(re, &s->gb, 1);
1b6d6674
DB
257 SKIP_BITS(re, &s->gb, 1);
258 } else {
259 /* escape */
a91d3658
VG
260 run = SHOW_UBITS(re, &s->gb, 6) + 1;
261 LAST_SKIP_BITS(re, &s->gb, 6);
1b6d6674 262 UPDATE_CACHE(re, &s->gb);
a91d3658
VG
263 level = SHOW_SBITS(re, &s->gb, 8);
264 SKIP_BITS(re, &s->gb, 8);
1b6d6674 265 if (level == -128) {
a91d3658
VG
266 level = SHOW_UBITS(re, &s->gb, 8) - 256;
267 SKIP_BITS(re, &s->gb, 8);
1b6d6674 268 } else if (level == 0) {
a91d3658
VG
269 level = SHOW_UBITS(re, &s->gb, 8);
270 SKIP_BITS(re, &s->gb, 8);
1b6d6674
DB
271 }
272 i += run;
6d93307f 273 check_scantable_index(s, i);
1b6d6674
DB
274 j = scantable[i];
275 if (level < 0) {
276 level = -level;
277 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
278 level = (level - 1) | 1;
279 level = -level;
280 } else {
281 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
282 level = (level - 1) | 1;
283 }
284 }
1b6d6674
DB
285
286 block[j] = level;
a91d3658 287 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
1b6d6674
DB
288 break;
289 UPDATE_CACHE(re, &s->gb);
290 }
291end:
292 LAST_SKIP_BITS(re, &s->gb, 2);
293 CLOSE_READER(re, &s->gb);
294 }
295 s->block_last_index[n] = i;
296 return 0;
297}
298
a91d3658
VG
299static inline int mpeg1_fast_decode_block_inter(MpegEncContext *s,
300 int16_t *block, int n)
1b6d6674
DB
301{
302 int level, i, j, run;
a91d3658
VG
303 RLTable *rl = &ff_rl_mpeg1;
304 uint8_t *const scantable = s->intra_scantable.permutated;
305 const int qscale = s->qscale;
1b6d6674
DB
306
307 {
308 OPEN_READER(re, &s->gb);
309 i = -1;
a91d3658 310 // Special case for first coefficient, no need to add second VLC table.
1b6d6674 311 UPDATE_CACHE(re, &s->gb);
a91d3658 312 if (((int32_t) GET_CACHE(re, &s->gb)) < 0) {
1b6d6674
DB
313 level = (3 * qscale) >> 1;
314 level = (level - 1) | 1;
315 if (GET_CACHE(re, &s->gb) & 0x40000000)
316 level = -level;
317 block[0] = level;
318 i++;
319 SKIP_BITS(re, &s->gb, 2);
a91d3658 320 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
1b6d6674
DB
321 goto end;
322 }
323
324 /* now quantify & encode AC coefficients */
325 for (;;) {
a91d3658
VG
326 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
327 TEX_VLC_BITS, 2, 0);
1b6d6674
DB
328
329 if (level != 0) {
330 i += run;
6d93307f 331 check_scantable_index(s, i);
1b6d6674
DB
332 j = scantable[i];
333 level = ((level * 2 + 1) * qscale) >> 1;
334 level = (level - 1) | 1;
a91d3658
VG
335 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
336 SHOW_SBITS(re, &s->gb, 1);
1b6d6674
DB
337 SKIP_BITS(re, &s->gb, 1);
338 } else {
339 /* escape */
a91d3658
VG
340 run = SHOW_UBITS(re, &s->gb, 6) + 1;
341 LAST_SKIP_BITS(re, &s->gb, 6);
1b6d6674 342 UPDATE_CACHE(re, &s->gb);
a91d3658
VG
343 level = SHOW_SBITS(re, &s->gb, 8);
344 SKIP_BITS(re, &s->gb, 8);
1b6d6674 345 if (level == -128) {
a91d3658
VG
346 level = SHOW_UBITS(re, &s->gb, 8) - 256;
347 SKIP_BITS(re, &s->gb, 8);
1b6d6674 348 } else if (level == 0) {
a91d3658
VG
349 level = SHOW_UBITS(re, &s->gb, 8);
350 SKIP_BITS(re, &s->gb, 8);
1b6d6674
DB
351 }
352 i += run;
6d93307f 353 check_scantable_index(s, i);
1b6d6674
DB
354 j = scantable[i];
355 if (level < 0) {
356 level = -level;
357 level = ((level * 2 + 1) * qscale) >> 1;
358 level = (level - 1) | 1;
359 level = -level;
360 } else {
361 level = ((level * 2 + 1) * qscale) >> 1;
362 level = (level - 1) | 1;
363 }
364 }
365
366 block[j] = level;
a91d3658 367 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
1b6d6674
DB
368 break;
369 UPDATE_CACHE(re, &s->gb);
370 }
371end:
372 LAST_SKIP_BITS(re, &s->gb, 2);
373 CLOSE_READER(re, &s->gb);
374 }
375 s->block_last_index[n] = i;
376 return 0;
377}
378
a91d3658
VG
379static inline int mpeg2_decode_block_non_intra(MpegEncContext *s,
380 int16_t *block, int n)
1b6d6674
DB
381{
382 int level, i, j, run;
383 RLTable *rl = &ff_rl_mpeg1;
a91d3658 384 uint8_t *const scantable = s->intra_scantable.permutated;
1b6d6674
DB
385 const uint16_t *quant_matrix;
386 const int qscale = s->qscale;
387 int mismatch;
388
389 mismatch = 1;
390
391 {
392 OPEN_READER(re, &s->gb);
393 i = -1;
394 if (n < 4)
395 quant_matrix = s->inter_matrix;
396 else
397 quant_matrix = s->chroma_inter_matrix;
398
a91d3658 399 // Special case for first coefficient, no need to add second VLC table.
1b6d6674 400 UPDATE_CACHE(re, &s->gb);
a91d3658
VG
401 if (((int32_t) GET_CACHE(re, &s->gb)) < 0) {
402 level = (3 * qscale * quant_matrix[0]) >> 5;
1b6d6674
DB
403 if (GET_CACHE(re, &s->gb) & 0x40000000)
404 level = -level;
405 block[0] = level;
406 mismatch ^= level;
407 i++;
408 SKIP_BITS(re, &s->gb, 2);
a91d3658 409 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
1b6d6674
DB
410 goto end;
411 }
412
413 /* now quantify & encode AC coefficients */
414 for (;;) {
a91d3658
VG
415 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
416 TEX_VLC_BITS, 2, 0);
1b6d6674
DB
417
418 if (level != 0) {
419 i += run;
6d93307f 420 check_scantable_index(s, i);
1b6d6674
DB
421 j = scantable[i];
422 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
a91d3658
VG
423 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
424 SHOW_SBITS(re, &s->gb, 1);
1b6d6674
DB
425 SKIP_BITS(re, &s->gb, 1);
426 } else {
427 /* escape */
a91d3658
VG
428 run = SHOW_UBITS(re, &s->gb, 6) + 1;
429 LAST_SKIP_BITS(re, &s->gb, 6);
1b6d6674 430 UPDATE_CACHE(re, &s->gb);
a91d3658
VG
431 level = SHOW_SBITS(re, &s->gb, 12);
432 SKIP_BITS(re, &s->gb, 12);
1b6d6674
DB
433
434 i += run;
6d93307f 435 check_scantable_index(s, i);
1b6d6674
DB
436 j = scantable[i];
437 if (level < 0) {
438 level = ((-level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
439 level = -level;
440 } else {
441 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
442 }
443 }
1b6d6674
DB
444
445 mismatch ^= level;
446 block[j] = level;
a91d3658 447 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
1b6d6674
DB
448 break;
449 UPDATE_CACHE(re, &s->gb);
450 }
451end:
452 LAST_SKIP_BITS(re, &s->gb, 2);
453 CLOSE_READER(re, &s->gb);
454 }
455 block[63] ^= (mismatch & 1);
456
457 s->block_last_index[n] = i;
458 return 0;
459}
460
461static inline int mpeg2_fast_decode_block_non_intra(MpegEncContext *s,
462 int16_t *block, int n)
463{
464 int level, i, j, run;
a91d3658
VG
465 RLTable *rl = &ff_rl_mpeg1;
466 uint8_t *const scantable = s->intra_scantable.permutated;
467 const int qscale = s->qscale;
1b6d6674
DB
468 OPEN_READER(re, &s->gb);
469 i = -1;
470
471 // special case for first coefficient, no need to add second VLC table
472 UPDATE_CACHE(re, &s->gb);
a91d3658 473 if (((int32_t) GET_CACHE(re, &s->gb)) < 0) {
1b6d6674
DB
474 level = (3 * qscale) >> 1;
475 if (GET_CACHE(re, &s->gb) & 0x40000000)
476 level = -level;
477 block[0] = level;
478 i++;
479 SKIP_BITS(re, &s->gb, 2);
a91d3658 480 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
1b6d6674
DB
481 goto end;
482 }
483
484 /* now quantify & encode AC coefficients */
485 for (;;) {
486 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
487
488 if (level != 0) {
489 i += run;
6d93307f 490 check_scantable_index(s, i);
a91d3658 491 j = scantable[i];
1b6d6674 492 level = ((level * 2 + 1) * qscale) >> 1;
a91d3658
VG
493 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
494 SHOW_SBITS(re, &s->gb, 1);
1b6d6674
DB
495 SKIP_BITS(re, &s->gb, 1);
496 } else {
497 /* escape */
a91d3658
VG
498 run = SHOW_UBITS(re, &s->gb, 6) + 1;
499 LAST_SKIP_BITS(re, &s->gb, 6);
1b6d6674 500 UPDATE_CACHE(re, &s->gb);
a91d3658
VG
501 level = SHOW_SBITS(re, &s->gb, 12);
502 SKIP_BITS(re, &s->gb, 12);
1b6d6674
DB
503
504 i += run;
6d93307f 505 check_scantable_index(s, i);
a91d3658 506 j = scantable[i];
1b6d6674
DB
507 if (level < 0) {
508 level = ((-level * 2 + 1) * qscale) >> 1;
509 level = -level;
510 } else {
511 level = ((level * 2 + 1) * qscale) >> 1;
512 }
513 }
514
515 block[j] = level;
a91d3658 516 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
1b6d6674
DB
517 break;
518 UPDATE_CACHE(re, &s->gb);
519 }
520end:
521 LAST_SKIP_BITS(re, &s->gb, 2);
522 CLOSE_READER(re, &s->gb);
523 s->block_last_index[n] = i;
524 return 0;
525}
526
a91d3658
VG
527static inline int mpeg2_decode_block_intra(MpegEncContext *s,
528 int16_t *block, int n)
1b6d6674
DB
529{
530 int level, dc, diff, i, j, run;
531 int component;
532 RLTable *rl;
a91d3658 533 uint8_t *const scantable = s->intra_scantable.permutated;
1b6d6674
DB
534 const uint16_t *quant_matrix;
535 const int qscale = s->qscale;
536 int mismatch;
537
538 /* DC coefficient */
539 if (n < 4) {
540 quant_matrix = s->intra_matrix;
a91d3658 541 component = 0;
1b6d6674
DB
542 } else {
543 quant_matrix = s->chroma_intra_matrix;
a91d3658 544 component = (n & 1) + 1;
1b6d6674
DB
545 }
546 diff = decode_dc(&s->gb, component);
547 if (diff >= 0xffff)
3a56bcee 548 return AVERROR_INVALIDDATA;
1b6d6674
DB
549 dc = s->last_dc[component];
550 dc += diff;
551 s->last_dc[component] = dc;
552 block[0] = dc << (3 - s->intra_dc_precision);
6a85dfc8 553 ff_dlog(s->avctx, "dc=%d\n", block[0]);
1b6d6674
DB
554 mismatch = block[0] ^ 1;
555 i = 0;
556 if (s->intra_vlc_format)
557 rl = &ff_rl_mpeg2;
558 else
559 rl = &ff_rl_mpeg1;
560
561 {
562 OPEN_READER(re, &s->gb);
563 /* now quantify & encode AC coefficients */
564 for (;;) {
565 UPDATE_CACHE(re, &s->gb);
a91d3658
VG
566 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
567 TEX_VLC_BITS, 2, 0);
1b6d6674
DB
568
569 if (level == 127) {
570 break;
571 } else if (level != 0) {
572 i += run;
6d93307f 573 check_scantable_index(s, i);
a91d3658 574 j = scantable[i];
1b6d6674 575 level = (level * qscale * quant_matrix[j]) >> 4;
a91d3658
VG
576 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
577 SHOW_SBITS(re, &s->gb, 1);
1b6d6674
DB
578 LAST_SKIP_BITS(re, &s->gb, 1);
579 } else {
580 /* escape */
a91d3658
VG
581 run = SHOW_UBITS(re, &s->gb, 6) + 1;
582 LAST_SKIP_BITS(re, &s->gb, 6);
1b6d6674 583 UPDATE_CACHE(re, &s->gb);
a91d3658
VG
584 level = SHOW_SBITS(re, &s->gb, 12);
585 SKIP_BITS(re, &s->gb, 12);
1b6d6674 586 i += run;
6d93307f 587 check_scantable_index(s, i);
a91d3658 588 j = scantable[i];
1b6d6674
DB
589 if (level < 0) {
590 level = (-level * qscale * quant_matrix[j]) >> 4;
591 level = -level;
592 } else {
593 level = (level * qscale * quant_matrix[j]) >> 4;
594 }
595 }
1b6d6674
DB
596
597 mismatch ^= level;
598 block[j] = level;
599 }
600 CLOSE_READER(re, &s->gb);
601 }
602 block[63] ^= mismatch & 1;
603
604 s->block_last_index[n] = i;
605 return 0;
606}
607
a91d3658
VG
608static inline int mpeg2_fast_decode_block_intra(MpegEncContext *s,
609 int16_t *block, int n)
1b6d6674 610{
6d93307f 611 int level, dc, diff, i, j, run;
1b6d6674
DB
612 int component;
613 RLTable *rl;
a91d3658 614 uint8_t *const scantable = s->intra_scantable.permutated;
1b6d6674
DB
615 const uint16_t *quant_matrix;
616 const int qscale = s->qscale;
617
618 /* DC coefficient */
619 if (n < 4) {
620 quant_matrix = s->intra_matrix;
a91d3658 621 component = 0;
1b6d6674
DB
622 } else {
623 quant_matrix = s->chroma_intra_matrix;
a91d3658 624 component = (n & 1) + 1;
1b6d6674
DB
625 }
626 diff = decode_dc(&s->gb, component);
627 if (diff >= 0xffff)
3a56bcee 628 return AVERROR_INVALIDDATA;
1b6d6674
DB
629 dc = s->last_dc[component];
630 dc += diff;
631 s->last_dc[component] = dc;
632 block[0] = dc << (3 - s->intra_dc_precision);
6d93307f 633 i = 0;
1b6d6674
DB
634 if (s->intra_vlc_format)
635 rl = &ff_rl_mpeg2;
636 else
637 rl = &ff_rl_mpeg1;
638
639 {
640 OPEN_READER(re, &s->gb);
641 /* now quantify & encode AC coefficients */
642 for (;;) {
643 UPDATE_CACHE(re, &s->gb);
a91d3658
VG
644 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
645 TEX_VLC_BITS, 2, 0);
1b6d6674
DB
646
647 if (level == 127) {
648 break;
649 } else if (level != 0) {
6d93307f
JG
650 i += run;
651 check_scantable_index(s, i);
a91d3658 652 j = scantable[i];
1b6d6674 653 level = (level * qscale * quant_matrix[j]) >> 4;
a91d3658
VG
654 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
655 SHOW_SBITS(re, &s->gb, 1);
1b6d6674
DB
656 LAST_SKIP_BITS(re, &s->gb, 1);
657 } else {
658 /* escape */
a91d3658
VG
659 run = SHOW_UBITS(re, &s->gb, 6) + 1;
660 LAST_SKIP_BITS(re, &s->gb, 6);
1b6d6674 661 UPDATE_CACHE(re, &s->gb);
a91d3658
VG
662 level = SHOW_SBITS(re, &s->gb, 12);
663 SKIP_BITS(re, &s->gb, 12);
6d93307f
JG
664 i += run;
665 check_scantable_index(s, i);
a91d3658 666 j = scantable[i];
1b6d6674
DB
667 if (level < 0) {
668 level = (-level * qscale * quant_matrix[j]) >> 4;
669 level = -level;
670 } else {
671 level = (level * qscale * quant_matrix[j]) >> 4;
672 }
673 }
674
675 block[j] = level;
676 }
677 CLOSE_READER(re, &s->gb);
678 }
679
6d93307f 680 s->block_last_index[n] = i;
1b6d6674
DB
681 return 0;
682}
683
684/******************************************/
685/* decoding */
686
687static inline int get_dmv(MpegEncContext *s)
688{
689 if (get_bits1(&s->gb))
690 return 1 - (get_bits1(&s->gb) << 1);
691 else
692 return 0;
693}
694
695static inline int get_qscale(MpegEncContext *s)
696{
697 int qscale = get_bits(&s->gb, 5);
a91d3658 698 if (s->q_scale_type)
1b6d6674 699 return non_linear_qscale[qscale];
a91d3658 700 else
1b6d6674 701 return qscale << 1;
1b6d6674
DB
702}
703
1b6d6674
DB
704/* motion type (for MPEG-2) */
705#define MT_FIELD 1
706#define MT_FRAME 2
707#define MT_16X8 2
708#define MT_DMV 3
709
710static int mpeg_decode_mb(MpegEncContext *s, int16_t block[12][64])
711{
712 int i, j, k, cbp, val, mb_type, motion_type;
713 const int mb_block_count = 4 + (1 << s->chroma_format);
3a56bcee 714 int ret;
1b6d6674 715
6a85dfc8 716 ff_dlog(s->avctx, "decode_mb: x=%d y=%d\n", s->mb_x, s->mb_y);
1b6d6674
DB
717
718 assert(s->mb_skipped == 0);
719
720 if (s->mb_skip_run-- != 0) {
721 if (s->pict_type == AV_PICTURE_TYPE_P) {
722 s->mb_skipped = 1;
a91d3658
VG
723 s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride] =
724 MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
1b6d6674
DB
725 } else {
726 int mb_type;
727
728 if (s->mb_x)
729 mb_type = s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride - 1];
730 else
a91d3658
VG
731 // FIXME not sure if this is allowed in MPEG at all
732 mb_type = s->current_picture.mb_type[s->mb_width + (s->mb_y - 1) * s->mb_stride - 1];
1b6d6674 733 if (IS_INTRA(mb_type))
3a56bcee 734 return AVERROR_INVALIDDATA;
a91d3658 735 s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride] =
1b6d6674
DB
736 mb_type | MB_TYPE_SKIP;
737// assert(s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride - 1] & (MB_TYPE_16x16 | MB_TYPE_16x8));
738
739 if ((s->mv[0][0][0] | s->mv[0][0][1] | s->mv[1][0][0] | s->mv[1][0][1]) == 0)
740 s->mb_skipped = 1;
741 }
742
743 return 0;
744 }
745
746 switch (s->pict_type) {
747 default:
748 case AV_PICTURE_TYPE_I:
749 if (get_bits1(&s->gb) == 0) {
750 if (get_bits1(&s->gb) == 0) {
a91d3658
VG
751 av_log(s->avctx, AV_LOG_ERROR,
752 "invalid mb type in I Frame at %d %d\n",
753 s->mb_x, s->mb_y);
3a56bcee 754 return AVERROR_INVALIDDATA;
1b6d6674
DB
755 }
756 mb_type = MB_TYPE_QUANT | MB_TYPE_INTRA;
757 } else {
758 mb_type = MB_TYPE_INTRA;
759 }
760 break;
761 case AV_PICTURE_TYPE_P:
762 mb_type = get_vlc2(&s->gb, ff_mb_ptype_vlc.table, MB_PTYPE_VLC_BITS, 1);
763 if (mb_type < 0) {
a91d3658
VG
764 av_log(s->avctx, AV_LOG_ERROR,
765 "invalid mb type in P Frame at %d %d\n", s->mb_x, s->mb_y);
3a56bcee 766 return AVERROR_INVALIDDATA;
1b6d6674
DB
767 }
768 mb_type = ptype2mb_type[mb_type];
769 break;
770 case AV_PICTURE_TYPE_B:
771 mb_type = get_vlc2(&s->gb, ff_mb_btype_vlc.table, MB_BTYPE_VLC_BITS, 1);
772 if (mb_type < 0) {
a91d3658
VG
773 av_log(s->avctx, AV_LOG_ERROR,
774 "invalid mb type in B Frame at %d %d\n", s->mb_x, s->mb_y);
3a56bcee 775 return AVERROR_INVALIDDATA;
1b6d6674
DB
776 }
777 mb_type = btype2mb_type[mb_type];
778 break;
779 }
6a85dfc8 780 ff_dlog(s->avctx, "mb_type=%x\n", mb_type);
1b6d6674
DB
781// motion_type = 0; /* avoid warning */
782 if (IS_INTRA(mb_type)) {
e74433a8 783 s->bdsp.clear_blocks(s->block[0]);
1b6d6674 784
a91d3658 785 if (!s->chroma_y_shift)
e74433a8 786 s->bdsp.clear_blocks(s->block[6]);
1b6d6674
DB
787
788 /* compute DCT type */
a91d3658
VG
789 // FIXME: add an interlaced_dct coded var?
790 if (s->picture_structure == PICT_FRAME &&
791 !s->frame_pred_frame_dct)
1b6d6674 792 s->interlaced_dct = get_bits1(&s->gb);
1b6d6674
DB
793
794 if (IS_QUANT(mb_type))
795 s->qscale = get_qscale(s);
796
797 if (s->concealment_motion_vectors) {
798 /* just parse them */
799 if (s->picture_structure != PICT_FRAME)
a91d3658 800 skip_bits1(&s->gb); /* field select */
1b6d6674 801
a91d3658
VG
802 s->mv[0][0][0] =
803 s->last_mv[0][0][0] =
804 s->last_mv[0][1][0] = mpeg_decode_motion(s, s->mpeg_f_code[0][0],
805 s->last_mv[0][0][0]);
806 s->mv[0][0][1] =
807 s->last_mv[0][0][1] =
808 s->last_mv[0][1][1] = mpeg_decode_motion(s, s->mpeg_f_code[0][1],
809 s->last_mv[0][0][1]);
1b6d6674
DB
810
811 skip_bits1(&s->gb); /* marker */
a91d3658
VG
812 } else {
813 /* reset mv prediction */
814 memset(s->last_mv, 0, sizeof(s->last_mv));
815 }
1b6d6674 816 s->mb_intra = 1;
19e30a58
DB
817#if FF_API_XVMC
818FF_DISABLE_DEPRECATION_WARNINGS
1b6d6674 819 // if 1, we memcpy blocks in xvmcvideo
a91d3658 820 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1)
1b6d6674 821 ff_xvmc_pack_pblocks(s, -1); // inter are always full blocks
19e30a58
DB
822FF_ENABLE_DEPRECATION_WARNINGS
823#endif /* FF_API_XVMC */
1b6d6674
DB
824
825 if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
848e86f7 826 if (s->avctx->flags2 & CODEC_FLAG2_FAST) {
a91d3658 827 for (i = 0; i < 6; i++)
1b6d6674 828 mpeg2_fast_decode_block_intra(s, *s->pblocks[i], i);
1b6d6674 829 } else {
a91d3658 830 for (i = 0; i < mb_block_count; i++)
3a56bcee
MN
831 if ((ret = mpeg2_decode_block_intra(s, *s->pblocks[i], i)) < 0)
832 return ret;
1b6d6674
DB
833 }
834 } else {
a91d3658 835 for (i = 0; i < 6; i++)
3a56bcee
MN
836 if ((ret = mpeg1_decode_block_intra(s, *s->pblocks[i], i)) < 0)
837 return ret;
1b6d6674
DB
838 }
839 } else {
840 if (mb_type & MB_TYPE_ZERO_MV) {
841 assert(mb_type & MB_TYPE_CBP);
842
843 s->mv_dir = MV_DIR_FORWARD;
844 if (s->picture_structure == PICT_FRAME) {
845 if (!s->frame_pred_frame_dct)
846 s->interlaced_dct = get_bits1(&s->gb);
847 s->mv_type = MV_TYPE_16X16;
848 } else {
a91d3658
VG
849 s->mv_type = MV_TYPE_FIELD;
850 mb_type |= MB_TYPE_INTERLACED;
1b6d6674
DB
851 s->field_select[0][0] = s->picture_structure - 1;
852 }
853
854 if (IS_QUANT(mb_type))
855 s->qscale = get_qscale(s);
856
857 s->last_mv[0][0][0] = 0;
858 s->last_mv[0][0][1] = 0;
859 s->last_mv[0][1][0] = 0;
860 s->last_mv[0][1][1] = 0;
a91d3658
VG
861 s->mv[0][0][0] = 0;
862 s->mv[0][0][1] = 0;
1b6d6674
DB
863 } else {
864 assert(mb_type & MB_TYPE_L0L1);
865 // FIXME decide if MBs in field pictures are MB_TYPE_INTERLACED
866 /* get additional motion vector type */
a91d3658 867 if (s->frame_pred_frame_dct) {
1b6d6674 868 motion_type = MT_FRAME;
a91d3658 869 } else {
1b6d6674
DB
870 motion_type = get_bits(&s->gb, 2);
871 if (s->picture_structure == PICT_FRAME && HAS_CBP(mb_type))
872 s->interlaced_dct = get_bits1(&s->gb);
873 }
874
875 if (IS_QUANT(mb_type))
876 s->qscale = get_qscale(s);
877
878 /* motion vectors */
879 s->mv_dir = (mb_type >> 13) & 3;
6a85dfc8 880 ff_dlog(s->avctx, "motion_type=%d\n", motion_type);
1b6d6674
DB
881 switch (motion_type) {
882 case MT_FRAME: /* or MT_16X8 */
883 if (s->picture_structure == PICT_FRAME) {
a91d3658 884 mb_type |= MB_TYPE_16x16;
1b6d6674
DB
885 s->mv_type = MV_TYPE_16X16;
886 for (i = 0; i < 2; i++) {
887 if (USES_LIST(mb_type, i)) {
888 /* MT_FRAME */
a91d3658
VG
889 s->mv[i][0][0] =
890 s->last_mv[i][0][0] =
891 s->last_mv[i][1][0] =
892 mpeg_decode_motion(s, s->mpeg_f_code[i][0],
893 s->last_mv[i][0][0]);
894 s->mv[i][0][1] =
895 s->last_mv[i][0][1] =
896 s->last_mv[i][1][1] =
897 mpeg_decode_motion(s, s->mpeg_f_code[i][1],
898 s->last_mv[i][0][1]);
1b6d6674
DB
899 /* full_pel: only for MPEG-1 */
900 if (s->full_pel[i]) {
901 s->mv[i][0][0] <<= 1;
902 s->mv[i][0][1] <<= 1;
903 }
904 }
905 }
906 } else {
a91d3658 907 mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
1b6d6674
DB
908 s->mv_type = MV_TYPE_16X8;
909 for (i = 0; i < 2; i++) {
910 if (USES_LIST(mb_type, i)) {
911 /* MT_16X8 */
912 for (j = 0; j < 2; j++) {
913 s->field_select[i][j] = get_bits1(&s->gb);
914 for (k = 0; k < 2; k++) {
915 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
916 s->last_mv[i][j][k]);
917 s->last_mv[i][j][k] = val;
918 s->mv[i][j][k] = val;
919 }
920 }
921 }
922 }
923 }
924 break;
925 case MT_FIELD:
926 s->mv_type = MV_TYPE_FIELD;
927 if (s->picture_structure == PICT_FRAME) {
928 mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
929 for (i = 0; i < 2; i++) {
930 if (USES_LIST(mb_type, i)) {
931 for (j = 0; j < 2; j++) {
932 s->field_select[i][j] = get_bits1(&s->gb);
933 val = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
934 s->last_mv[i][j][0]);
935 s->last_mv[i][j][0] = val;
936 s->mv[i][j][0] = val;
6a85dfc8 937 ff_dlog(s->avctx, "fmx=%d\n", val);
1b6d6674
DB
938 val = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
939 s->last_mv[i][j][1] >> 1);
940 s->last_mv[i][j][1] = val << 1;
941 s->mv[i][j][1] = val;
6a85dfc8 942 ff_dlog(s->avctx, "fmy=%d\n", val);
1b6d6674
DB
943 }
944 }
945 }
946 } else {
947 mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
948 for (i = 0; i < 2; i++) {
949 if (USES_LIST(mb_type, i)) {
950 s->field_select[i][0] = get_bits1(&s->gb);
951 for (k = 0; k < 2; k++) {
952 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
953 s->last_mv[i][0][k]);
954 s->last_mv[i][0][k] = val;
955 s->last_mv[i][1][k] = val;
956 s->mv[i][0][k] = val;
957 }
958 }
959 }
960 }
961 break;
962 case MT_DMV:
963 s->mv_type = MV_TYPE_DMV;
964 for (i = 0; i < 2; i++) {
965 if (USES_LIST(mb_type, i)) {
966 int dmx, dmy, mx, my, m;
967 const int my_shift = s->picture_structure == PICT_FRAME;
968
969 mx = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
970 s->last_mv[i][0][0]);
971 s->last_mv[i][0][0] = mx;
972 s->last_mv[i][1][0] = mx;
973 dmx = get_dmv(s);
974 my = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
975 s->last_mv[i][0][1] >> my_shift);
976 dmy = get_dmv(s);
977
978
979 s->last_mv[i][0][1] = my << my_shift;
980 s->last_mv[i][1][1] = my << my_shift;
981
982 s->mv[i][0][0] = mx;
983 s->mv[i][0][1] = my;
984 s->mv[i][1][0] = mx; // not used
985 s->mv[i][1][1] = my; // not used
986
987 if (s->picture_structure == PICT_FRAME) {
988 mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
989
990 // m = 1 + 2 * s->top_field_first;
991 m = s->top_field_first ? 1 : 3;
992
993 /* top -> top pred */
994 s->mv[i][2][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
995 s->mv[i][2][1] = ((my * m + (my > 0)) >> 1) + dmy - 1;
996 m = 4 - m;
997 s->mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
998 s->mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1;
999 } else {
1000 mb_type |= MB_TYPE_16x16;
1001
1002 s->mv[i][2][0] = ((mx + (mx > 0)) >> 1) + dmx;
1003 s->mv[i][2][1] = ((my + (my > 0)) >> 1) + dmy;
1004 if (s->picture_structure == PICT_TOP_FIELD)
1005 s->mv[i][2][1]--;
1006 else
1007 s->mv[i][2][1]++;
1008 }
1009 }
1010 }
1011 break;
1012 default:
a91d3658
VG
1013 av_log(s->avctx, AV_LOG_ERROR,
1014 "00 motion_type at %d %d\n", s->mb_x, s->mb_y);
3a56bcee 1015 return AVERROR_INVALIDDATA;
1b6d6674
DB
1016 }
1017 }
1018
1019 s->mb_intra = 0;
1020 if (HAS_CBP(mb_type)) {
e74433a8 1021 s->bdsp.clear_blocks(s->block[0]);
1b6d6674
DB
1022
1023 cbp = get_vlc2(&s->gb, ff_mb_pat_vlc.table, MB_PAT_VLC_BITS, 1);
1024 if (mb_block_count > 6) {
a91d3658
VG
1025 cbp <<= mb_block_count - 6;
1026 cbp |= get_bits(&s->gb, mb_block_count - 6);
e74433a8 1027 s->bdsp.clear_blocks(s->block[6]);
1b6d6674
DB
1028 }
1029 if (cbp <= 0) {
a91d3658
VG
1030 av_log(s->avctx, AV_LOG_ERROR,
1031 "invalid cbp at %d %d\n", s->mb_x, s->mb_y);
3a56bcee 1032 return AVERROR_INVALIDDATA;
1b6d6674
DB
1033 }
1034
19e30a58
DB
1035#if FF_API_XVMC
1036FF_DISABLE_DEPRECATION_WARNINGS
a91d3658
VG
1037 // if 1, we memcpy blocks in xvmcvideo
1038 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1)
1b6d6674 1039 ff_xvmc_pack_pblocks(s, cbp);
19e30a58
DB
1040FF_ENABLE_DEPRECATION_WARNINGS
1041#endif /* FF_API_XVMC */
1b6d6674
DB
1042
1043 if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
848e86f7 1044 if (s->avctx->flags2 & CODEC_FLAG2_FAST) {
1b6d6674 1045 for (i = 0; i < 6; i++) {
a91d3658 1046 if (cbp & 32)
1b6d6674 1047 mpeg2_fast_decode_block_non_intra(s, *s->pblocks[i], i);
a91d3658 1048 else
1b6d6674 1049 s->block_last_index[i] = -1;
1b6d6674
DB
1050 cbp += cbp;
1051 }
1052 } else {
a91d3658 1053 cbp <<= 12 - mb_block_count;
1b6d6674
DB
1054
1055 for (i = 0; i < mb_block_count; i++) {
1056 if (cbp & (1 << 11)) {
3a56bcee
MN
1057 if ((ret = mpeg2_decode_block_non_intra(s, *s->pblocks[i], i)) < 0)
1058 return ret;
1b6d6674
DB
1059 } else {
1060 s->block_last_index[i] = -1;
1061 }
1062 cbp += cbp;
1063 }
1064 }
1065 } else {
848e86f7 1066 if (s->avctx->flags2 & CODEC_FLAG2_FAST) {
1b6d6674 1067 for (i = 0; i < 6; i++) {
a91d3658 1068 if (cbp & 32)
1b6d6674 1069 mpeg1_fast_decode_block_inter(s, *s->pblocks[i], i);
a91d3658 1070 else
1b6d6674 1071 s->block_last_index[i] = -1;
1b6d6674
DB
1072 cbp += cbp;
1073 }
1074 } else {
1075 for (i = 0; i < 6; i++) {
1076 if (cbp & 32) {
3a56bcee
MN
1077 if ((ret = mpeg1_decode_block_inter(s, *s->pblocks[i], i)) < 0)
1078 return ret;
1b6d6674
DB
1079 } else {
1080 s->block_last_index[i] = -1;
1081 }
1082 cbp += cbp;
1083 }
1084 }
1085 }
1086 } else {
1087 for (i = 0; i < 12; i++)
1088 s->block_last_index[i] = -1;
1089 }
1090 }
1091
1092 s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride] = mb_type;
1093
1094 return 0;
1095}
1096
1097static av_cold int mpeg_decode_init(AVCodecContext *avctx)
1098{
a91d3658 1099 Mpeg1Context *s = avctx->priv_data;
1b6d6674 1100 MpegEncContext *s2 = &s->mpeg_enc_ctx;
1b6d6674 1101
835f798c 1102 ff_mpv_decode_defaults(s2);
1b6d6674
DB
1103
1104 s->mpeg_enc_ctx.avctx = avctx;
998c9f15
JS
1105
1106 /* we need some permutation to store matrices,
1107 * until the decoder sets the real permutation. */
1108 ff_mpv_idct_init(s2);
1b6d6674
DB
1109 ff_mpeg12_common_init(&s->mpeg_enc_ctx);
1110 ff_mpeg12_init_vlcs();
1111
1112 s->mpeg_enc_ctx_allocated = 0;
1113 s->mpeg_enc_ctx.picture_number = 0;
1114 s->repeat_field = 0;
1115 s->mpeg_enc_ctx.codec_id = avctx->codec->id;
a91d3658 1116 avctx->color_range = AVCOL_RANGE_MPEG;
1b6d6674
DB
1117 if (avctx->codec->id == AV_CODEC_ID_MPEG1VIDEO)
1118 avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
1119 else
1120 avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
1121 return 0;
1122}
1123
a91d3658
VG
1124static int mpeg_decode_update_thread_context(AVCodecContext *avctx,
1125 const AVCodecContext *avctx_from)
1b6d6674
DB
1126{
1127 Mpeg1Context *ctx = avctx->priv_data, *ctx_from = avctx_from->priv_data;
1128 MpegEncContext *s = &ctx->mpeg_enc_ctx, *s1 = &ctx_from->mpeg_enc_ctx;
1129 int err;
1130
a91d3658
VG
1131 if (avctx == avctx_from ||
1132 !ctx_from->mpeg_enc_ctx_allocated ||
1133 !s1->context_initialized)
1b6d6674
DB
1134 return 0;
1135
1136 err = ff_mpeg_update_thread_context(avctx, avctx_from);
a91d3658
VG
1137 if (err)
1138 return err;
1b6d6674 1139
5d29efe4
VG
1140 if (!ctx->mpeg_enc_ctx_allocated) {
1141 // copy the whole context after the initial MpegEncContext structure
1142 memcpy(ctx, ctx_from, sizeof(*ctx));
1143 memset(&ctx->mpeg_enc_ctx, 0, sizeof(ctx->mpeg_enc_ctx));
1144 }
1b6d6674
DB
1145
1146 if (!(s->pict_type == AV_PICTURE_TYPE_B || s->low_delay))
1147 s->picture_number++;
1148
1149 return 0;
1150}
1151
1152static void quant_matrix_rebuild(uint16_t *matrix, const uint8_t *old_perm,
1153 const uint8_t *new_perm)
1154{
1155 uint16_t temp_matrix[64];
1156 int i;
1157
1158 memcpy(temp_matrix, matrix, 64 * sizeof(uint16_t));
1159
a91d3658 1160 for (i = 0; i < 64; i++)
1b6d6674 1161 matrix[new_perm[i]] = temp_matrix[old_perm[i]];
1b6d6674
DB
1162}
1163
19e30a58 1164#if FF_API_XVMC
1b6d6674
DB
1165static const enum AVPixelFormat pixfmt_xvmc_mpg2_420[] = {
1166 AV_PIX_FMT_XVMC_MPEG2_IDCT,
1167 AV_PIX_FMT_XVMC_MPEG2_MC,
a91d3658
VG
1168 AV_PIX_FMT_NONE
1169};
19e30a58 1170#endif /* FF_API_XVMC */
1b6d6674
DB
1171
1172static const enum AVPixelFormat mpeg12_hwaccel_pixfmt_list_420[] = {
1173#if CONFIG_MPEG2_DXVA2_HWACCEL
1174 AV_PIX_FMT_DXVA2_VLD,
1175#endif
d8039ef8
SL
1176#if CONFIG_MPEG2_D3D11VA_HWACCEL
1177 AV_PIX_FMT_D3D11VA_VLD,
1178#endif
1b6d6674
DB
1179#if CONFIG_MPEG2_VAAPI_HWACCEL
1180 AV_PIX_FMT_VAAPI_VLD,
1181#endif
1182#if CONFIG_MPEG1_VDPAU_HWACCEL | CONFIG_MPEG2_VDPAU_HWACCEL
1183 AV_PIX_FMT_VDPAU,
1184#endif
1185 AV_PIX_FMT_YUV420P,
1186 AV_PIX_FMT_NONE
1187};
1188
4e03b209
RDC
1189static const enum AVPixelFormat mpeg12_pixfmt_list_422[] = {
1190 AV_PIX_FMT_YUV422P,
1191 AV_PIX_FMT_NONE
1192};
1193
1194static const enum AVPixelFormat mpeg12_pixfmt_list_444[] = {
1195 AV_PIX_FMT_YUV444P,
1196 AV_PIX_FMT_NONE
1197};
1198
1b6d6674
DB
1199static enum AVPixelFormat mpeg_get_pixelformat(AVCodecContext *avctx)
1200{
a91d3658 1201 Mpeg1Context *s1 = avctx->priv_data;
1b6d6674 1202 MpegEncContext *s = &s1->mpeg_enc_ctx;
4e03b209 1203 const enum AVPixelFormat *pix_fmts;
1b6d6674 1204
19e30a58
DB
1205#if FF_API_XVMC
1206FF_DISABLE_DEPRECATION_WARNINGS
1b6d6674 1207 if (avctx->xvmc_acceleration)
632ad224 1208 return ff_get_format(avctx, pixfmt_xvmc_mpg2_420);
19e30a58
DB
1209FF_ENABLE_DEPRECATION_WARNINGS
1210#endif /* FF_API_XVMC */
1211
a91d3658 1212 if (s->chroma_format < 2)
4e03b209 1213 pix_fmts = mpeg12_hwaccel_pixfmt_list_420;
19e30a58 1214 else if (s->chroma_format == 2)
4e03b209 1215 pix_fmts = mpeg12_pixfmt_list_422;
19e30a58 1216 else
4e03b209
RDC
1217 pix_fmts = mpeg12_pixfmt_list_444;
1218
1219 return ff_get_format(avctx, pix_fmts);
1b6d6674
DB
1220}
1221
1222/* Call this function when we know all parameters.
1223 * It may be called in different places for MPEG-1 and MPEG-2. */
1224static int mpeg_decode_postinit(AVCodecContext *avctx)
1225{
a91d3658 1226 Mpeg1Context *s1 = avctx->priv_data;
1b6d6674
DB
1227 MpegEncContext *s = &s1->mpeg_enc_ctx;
1228 uint8_t old_permutation[64];
6c725472 1229 int ret;
1b6d6674 1230
a91d3658
VG
1231 if ((s1->mpeg_enc_ctx_allocated == 0) ||
1232 avctx->coded_width != s->width ||
1233 avctx->coded_height != s->height ||
1b6d6674
DB
1234 s1->save_width != s->width ||
1235 s1->save_height != s->height ||
1236 s1->save_aspect_info != s->aspect_ratio_info ||
1237 s1->save_progressive_seq != s->progressive_sequence ||
a91d3658 1238 0) {
1b6d6674
DB
1239 if (s1->mpeg_enc_ctx_allocated) {
1240 ParseContext pc = s->parse_context;
1241 s->parse_context.buffer = 0;
835f798c 1242 ff_mpv_common_end(s);
1b6d6674
DB
1243 s->parse_context = pc;
1244 }
1245
6c725472
AK
1246 ret = ff_set_dimensions(avctx, s->width, s->height);
1247 if (ret < 0)
1248 return ret;
1249
1b6d6674
DB
1250 avctx->bit_rate = s->bit_rate;
1251 s1->save_aspect_info = s->aspect_ratio_info;
1252 s1->save_width = s->width;
1253 s1->save_height = s->height;
1254 s1->save_progressive_seq = s->progressive_sequence;
1255
1256 /* low_delay may be forced, in this case we will have B-frames
1257 * that behave like P-frames. */
1258 avctx->has_b_frames = !s->low_delay;
1259
1260 if (avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
a91d3658 1261 // MPEG-1 fps
7ea1b347 1262 avctx->framerate = ff_mpeg12_frame_rate_tab[s->frame_rate_index];
a91d3658
VG
1263 // MPEG-1 aspect
1264 avctx->sample_aspect_ratio = av_d2q(1.0 / ff_mpeg1_aspect[s->aspect_ratio_info], 255);
1265 avctx->ticks_per_frame = 1;
1266 } else { // MPEG-2
1267 // MPEG-2 fps
7ea1b347
AK
1268 av_reduce(&s->avctx->framerate.num,
1269 &s->avctx->framerate.den,
a91d3658 1270 ff_mpeg12_frame_rate_tab[s->frame_rate_index].num * s1->frame_rate_ext.num * 2,
1b6d6674
DB
1271 ff_mpeg12_frame_rate_tab[s->frame_rate_index].den * s1->frame_rate_ext.den,
1272 1 << 30);
1273 avctx->ticks_per_frame = 2;
a91d3658 1274 // MPEG-2 aspect
1b6d6674
DB
1275 if (s->aspect_ratio_info > 1) {
1276 AVRational dar =
1277 av_mul_q(av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
a91d3658
VG
1278 (AVRational) { s1->pan_scan.width,
1279 s1->pan_scan.height }),
1280 (AVRational) { s->width, s->height });
1281
1282 /* We ignore the spec here and guess a bit as reality does not
1283 * match the spec, see for example res_change_ffmpeg_aspect.ts
1284 * and sequence-display-aspect.mpg.
1285 * issue1613, 621, 562 */
1b6d6674 1286 if ((s1->pan_scan.width == 0) || (s1->pan_scan.height == 0) ||
a91d3658
VG
1287 (av_cmp_q(dar, (AVRational) { 4, 3 }) &&
1288 av_cmp_q(dar, (AVRational) { 16, 9 }))) {
1b6d6674
DB
1289 s->avctx->sample_aspect_ratio =
1290 av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
a91d3658 1291 (AVRational) { s->width, s->height });
1b6d6674
DB
1292 } else {
1293 s->avctx->sample_aspect_ratio =
1294 av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
a91d3658
VG
1295 (AVRational) { s1->pan_scan.width, s1->pan_scan.height });
1296// issue1613 4/3 16/9 -> 16/9
1297// res_change_ffmpeg_aspect.ts 4/3 225/44 ->4/3
1298// widescreen-issue562.mpg 4/3 16/9 -> 16/9
1b6d6674 1299// s->avctx->sample_aspect_ratio = av_mul_q(s->avctx->sample_aspect_ratio, (AVRational) {s->width, s->height});
6a85dfc8 1300 ff_dlog(avctx, "A %d/%d\n",
a91d3658
VG
1301 ff_mpeg2_aspect[s->aspect_ratio_info].num,
1302 ff_mpeg2_aspect[s->aspect_ratio_info].den);
6a85dfc8 1303 ff_dlog(avctx, "B %d/%d\n", s->avctx->sample_aspect_ratio.num,
1b6d6674
DB
1304 s->avctx->sample_aspect_ratio.den);
1305 }
1306 } else {
1307 s->avctx->sample_aspect_ratio =
1308 ff_mpeg2_aspect[s->aspect_ratio_info];
1309 }
1310 } // MPEG-2
1311
9e500efd
JR
1312 ff_set_sar(s->avctx, s->avctx->sample_aspect_ratio);
1313
1b6d6674 1314 avctx->pix_fmt = mpeg_get_pixelformat(avctx);
1b6d6674 1315 // until then pix_fmt may be changed right after codec init
19e30a58
DB
1316#if FF_API_XVMC
1317 if ((avctx->pix_fmt == AV_PIX_FMT_XVMC_MPEG2_IDCT ||
a91d3658 1318 avctx->hwaccel) && avctx->idct_algo == FF_IDCT_AUTO)
19e30a58
DB
1319#else
1320 if (avctx->hwaccel && avctx->idct_algo == FF_IDCT_AUTO)
1321#endif /* FF_API_XVMC */
1322 avctx->idct_algo = FF_IDCT_SIMPLE;
1b6d6674
DB
1323
1324 /* Quantization matrices may need reordering
1325 * if DCT permutation is changed. */
e3fcb143 1326 memcpy(old_permutation, s->idsp.idct_permutation, 64 * sizeof(uint8_t));
1b6d6674 1327
998c9f15 1328 ff_mpv_idct_init(s);
3a56bcee
MN
1329 if ((ret = ff_mpv_common_init(s)) < 0)
1330 return ret;
1b6d6674 1331
e3fcb143
DB
1332 quant_matrix_rebuild(s->intra_matrix, old_permutation, s->idsp.idct_permutation);
1333 quant_matrix_rebuild(s->inter_matrix, old_permutation, s->idsp.idct_permutation);
1334 quant_matrix_rebuild(s->chroma_intra_matrix, old_permutation, s->idsp.idct_permutation);
1335 quant_matrix_rebuild(s->chroma_inter_matrix, old_permutation, s->idsp.idct_permutation);
1b6d6674
DB
1336
1337 s1->mpeg_enc_ctx_allocated = 1;
1338 }
1339 return 0;
1340}
1341
a91d3658
VG
1342static int mpeg1_decode_picture(AVCodecContext *avctx, const uint8_t *buf,
1343 int buf_size)
1b6d6674 1344{
a91d3658 1345 Mpeg1Context *s1 = avctx->priv_data;
1b6d6674
DB
1346 MpegEncContext *s = &s1->mpeg_enc_ctx;
1347 int ref, f_code, vbv_delay;
1348
a91d3658 1349 init_get_bits(&s->gb, buf, buf_size * 8);
1b6d6674
DB
1350
1351 ref = get_bits(&s->gb, 10); /* temporal ref */
1352 s->pict_type = get_bits(&s->gb, 3);
1353 if (s->pict_type == 0 || s->pict_type > 3)
3a56bcee 1354 return AVERROR_INVALIDDATA;
1b6d6674
DB
1355
1356 vbv_delay = get_bits(&s->gb, 16);
a91d3658
VG
1357 if (s->pict_type == AV_PICTURE_TYPE_P ||
1358 s->pict_type == AV_PICTURE_TYPE_B) {
1b6d6674
DB
1359 s->full_pel[0] = get_bits1(&s->gb);
1360 f_code = get_bits(&s->gb, 3);
1361 if (f_code == 0 && (avctx->err_recognition & AV_EF_BITSTREAM))
3a56bcee 1362 return AVERROR_INVALIDDATA;
1b6d6674
DB
1363 s->mpeg_f_code[0][0] = f_code;
1364 s->mpeg_f_code[0][1] = f_code;
1365 }
1366 if (s->pict_type == AV_PICTURE_TYPE_B) {
1367 s->full_pel[1] = get_bits1(&s->gb);
1368 f_code = get_bits(&s->gb, 3);
1369 if (f_code == 0 && (avctx->err_recognition & AV_EF_BITSTREAM))
3a56bcee 1370 return AVERROR_INVALIDDATA;
1b6d6674
DB
1371 s->mpeg_f_code[1][0] = f_code;
1372 s->mpeg_f_code[1][1] = f_code;
1373 }
f6774f90 1374 s->current_picture.f->pict_type = s->pict_type;
1375 s->current_picture.f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
1b6d6674
DB
1376
1377 if (avctx->debug & FF_DEBUG_PICT_INFO)
a91d3658
VG
1378 av_log(avctx, AV_LOG_DEBUG,
1379 "vbv_delay %d, ref %d type:%d\n", vbv_delay, ref, s->pict_type);
1b6d6674
DB
1380
1381 s->y_dc_scale = 8;
1382 s->c_dc_scale = 8;
1383 return 0;
1384}
1385
1386static void mpeg_decode_sequence_extension(Mpeg1Context *s1)
1387{
a91d3658 1388 MpegEncContext *s = &s1->mpeg_enc_ctx;
1b6d6674
DB
1389 int horiz_size_ext, vert_size_ext;
1390 int bit_rate_ext;
1391
1392 skip_bits(&s->gb, 1); /* profile and level esc*/
1393 s->avctx->profile = get_bits(&s->gb, 3);
1394 s->avctx->level = get_bits(&s->gb, 4);
a91d3658 1395 s->progressive_sequence = get_bits1(&s->gb); /* progressive_sequence */
1b6d6674
DB
1396 s->chroma_format = get_bits(&s->gb, 2); /* chroma_format 1=420, 2=422, 3=444 */
1397 horiz_size_ext = get_bits(&s->gb, 2);
1398 vert_size_ext = get_bits(&s->gb, 2);
1399 s->width |= (horiz_size_ext << 12);
1400 s->height |= (vert_size_ext << 12);
1401 bit_rate_ext = get_bits(&s->gb, 12); /* XXX: handle it */
1402 s->bit_rate += (bit_rate_ext << 18) * 400;
1403 skip_bits1(&s->gb); /* marker */
1404 s->avctx->rc_buffer_size += get_bits(&s->gb, 8) * 1024 * 16 << 10;
1405
1406 s->low_delay = get_bits1(&s->gb);
848e86f7 1407 if (s->avctx->flags & CODEC_FLAG_LOW_DELAY)
1b6d6674
DB
1408 s->low_delay = 1;
1409
1410 s1->frame_rate_ext.num = get_bits(&s->gb, 2) + 1;
1411 s1->frame_rate_ext.den = get_bits(&s->gb, 5) + 1;
1412
6a85dfc8 1413 ff_dlog(s->avctx, "sequence extension\n");
a91d3658 1414 s->codec_id = s->avctx->codec_id = AV_CODEC_ID_MPEG2VIDEO;
1b6d6674
DB
1415
1416 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
a91d3658
VG
1417 av_log(s->avctx, AV_LOG_DEBUG,
1418 "profile: %d, level: %d vbv buffer: %d, bitrate:%d\n",
1419 s->avctx->profile, s->avctx->level,
1420 s->avctx->rc_buffer_size, s->bit_rate);
1b6d6674
DB
1421}
1422
1423static void mpeg_decode_sequence_display_extension(Mpeg1Context *s1)
1424{
1425 MpegEncContext *s = &s1->mpeg_enc_ctx;
1426 int color_description, w, h;
1427
1428 skip_bits(&s->gb, 3); /* video format */
1429 color_description = get_bits1(&s->gb);
1430 if (color_description) {
1431 s->avctx->color_primaries = get_bits(&s->gb, 8);
1432 s->avctx->color_trc = get_bits(&s->gb, 8);
1433 s->avctx->colorspace = get_bits(&s->gb, 8);
1434 }
1435 w = get_bits(&s->gb, 14);
a91d3658 1436 skip_bits(&s->gb, 1); // marker
1b6d6674
DB
1437 h = get_bits(&s->gb, 14);
1438 // remaining 3 bits are zero padding
1439
1440 s1->pan_scan.width = 16 * w;
1441 s1->pan_scan.height = 16 * h;
1442
1443 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1444 av_log(s->avctx, AV_LOG_DEBUG, "sde w:%d, h:%d\n", w, h);
1445}
1446
1447static void mpeg_decode_picture_display_extension(Mpeg1Context *s1)
1448{
1449 MpegEncContext *s = &s1->mpeg_enc_ctx;
1450 int i, nofco;
1451
1452 nofco = 1;
1453 if (s->progressive_sequence) {
1454 if (s->repeat_first_field) {
1455 nofco++;
1456 if (s->top_field_first)
1457 nofco++;
1458 }
1459 } else {
1460 if (s->picture_structure == PICT_FRAME) {
1461 nofco++;
1462 if (s->repeat_first_field)
1463 nofco++;
1464 }
1465 }
1466 for (i = 0; i < nofco; i++) {
1467 s1->pan_scan.position[i][0] = get_sbits(&s->gb, 16);
1468 skip_bits(&s->gb, 1); // marker
1469 s1->pan_scan.position[i][1] = get_sbits(&s->gb, 16);
1470 skip_bits(&s->gb, 1); // marker
1471 }
1472
1473 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
cc8163e1
DB
1474 av_log(s->avctx, AV_LOG_DEBUG,
1475 "pde (%"PRId16",%"PRId16") (%"PRId16",%"PRId16") (%"PRId16",%"PRId16")\n",
1b6d6674
DB
1476 s1->pan_scan.position[0][0], s1->pan_scan.position[0][1],
1477 s1->pan_scan.position[1][0], s1->pan_scan.position[1][1],
1478 s1->pan_scan.position[2][0], s1->pan_scan.position[2][1]);
1479}
1480
a91d3658
VG
1481static int load_matrix(MpegEncContext *s, uint16_t matrix0[64],
1482 uint16_t matrix1[64], int intra)
1b6d6674
DB
1483{
1484 int i;
1485
1486 for (i = 0; i < 64; i++) {
e3fcb143 1487 int j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
1b6d6674
DB
1488 int v = get_bits(&s->gb, 8);
1489 if (v == 0) {
1490 av_log(s->avctx, AV_LOG_ERROR, "matrix damaged\n");
3a56bcee 1491 return AVERROR_INVALIDDATA;
1b6d6674
DB
1492 }
1493 if (intra && i == 0 && v != 8) {
1494 av_log(s->avctx, AV_LOG_ERROR, "intra matrix invalid, ignoring\n");
1495 v = 8; // needed by pink.mpg / issue1046
1496 }
1497 matrix0[j] = v;
1498 if (matrix1)
1499 matrix1[j] = v;
1500 }
1501 return 0;
1502}
1503
1504static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
1505{
6a85dfc8 1506 ff_dlog(s->avctx, "matrix extension\n");
1b6d6674 1507
a91d3658
VG
1508 if (get_bits1(&s->gb))
1509 load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
1510 if (get_bits1(&s->gb))
1511 load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
1512 if (get_bits1(&s->gb))
1513 load_matrix(s, s->chroma_intra_matrix, NULL, 1);
1514 if (get_bits1(&s->gb))
1515 load_matrix(s, s->chroma_inter_matrix, NULL, 0);
1b6d6674
DB
1516}
1517
1518static void mpeg_decode_picture_coding_extension(Mpeg1Context *s1)
1519{
1520 MpegEncContext *s = &s1->mpeg_enc_ctx;
1521
a91d3658 1522 s->full_pel[0] = s->full_pel[1] = 0;
1b6d6674
DB
1523 s->mpeg_f_code[0][0] = get_bits(&s->gb, 4);
1524 s->mpeg_f_code[0][1] = get_bits(&s->gb, 4);
1525 s->mpeg_f_code[1][0] = get_bits(&s->gb, 4);
1526 s->mpeg_f_code[1][1] = get_bits(&s->gb, 4);
1527 if (!s->pict_type && s1->mpeg_enc_ctx_allocated) {
a91d3658
VG
1528 av_log(s->avctx, AV_LOG_ERROR,
1529 "Missing picture start code, guessing missing values\n");
1b6d6674
DB
1530 if (s->mpeg_f_code[1][0] == 15 && s->mpeg_f_code[1][1] == 15) {
1531 if (s->mpeg_f_code[0][0] == 15 && s->mpeg_f_code[0][1] == 15)
1532 s->pict_type = AV_PICTURE_TYPE_I;
1533 else
1534 s->pict_type = AV_PICTURE_TYPE_P;
1535 } else
1536 s->pict_type = AV_PICTURE_TYPE_B;
f6774f90 1537 s->current_picture.f->pict_type = s->pict_type;
1538 s->current_picture.f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
1b6d6674
DB
1539 }
1540 s->intra_dc_precision = get_bits(&s->gb, 2);
1541 s->picture_structure = get_bits(&s->gb, 2);
1542 s->top_field_first = get_bits1(&s->gb);
1543 s->frame_pred_frame_dct = get_bits1(&s->gb);
1544 s->concealment_motion_vectors = get_bits1(&s->gb);
1545 s->q_scale_type = get_bits1(&s->gb);
1546 s->intra_vlc_format = get_bits1(&s->gb);
1547 s->alternate_scan = get_bits1(&s->gb);
1548 s->repeat_first_field = get_bits1(&s->gb);
1549 s->chroma_420_type = get_bits1(&s->gb);
1550 s->progressive_frame = get_bits1(&s->gb);
1551
1552 if (s->progressive_sequence && !s->progressive_frame) {
1553 s->progressive_frame = 1;
a91d3658
VG
1554 av_log(s->avctx, AV_LOG_ERROR,
1555 "interlaced frame in progressive sequence, ignoring\n");
1b6d6674
DB
1556 }
1557
a91d3658
VG
1558 if (s->picture_structure == 0 ||
1559 (s->progressive_frame && s->picture_structure != PICT_FRAME)) {
1560 av_log(s->avctx, AV_LOG_ERROR,
1561 "picture_structure %d invalid, ignoring\n",
1562 s->picture_structure);
1b6d6674
DB
1563 s->picture_structure = PICT_FRAME;
1564 }
1565
a91d3658 1566 if (s->progressive_sequence && !s->frame_pred_frame_dct)
1b6d6674 1567 av_log(s->avctx, AV_LOG_WARNING, "invalid frame_pred_frame_dct\n");
1b6d6674
DB
1568
1569 if (s->picture_structure == PICT_FRAME) {
1570 s->first_field = 0;
1571 s->v_edge_pos = 16 * s->mb_height;
1572 } else {
1573 s->first_field ^= 1;
1574 s->v_edge_pos = 8 * s->mb_height;
1575 memset(s->mbskip_table, 0, s->mb_stride * s->mb_height);
1576 }
1577
1578 if (s->alternate_scan) {
e3fcb143
DB
1579 ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_alternate_vertical_scan);
1580 ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_alternate_vertical_scan);
1b6d6674 1581 } else {
e3fcb143
DB
1582 ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_zigzag_direct);
1583 ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_zigzag_direct);
1b6d6674
DB
1584 }
1585
1586 /* composite display not parsed */
6a85dfc8
VG
1587 ff_dlog(s->avctx, "intra_dc_precision=%d\n", s->intra_dc_precision);
1588 ff_dlog(s->avctx, "picture_structure=%d\n", s->picture_structure);
1589 ff_dlog(s->avctx, "top field first=%d\n", s->top_field_first);
1590 ff_dlog(s->avctx, "repeat first field=%d\n", s->repeat_first_field);
1591 ff_dlog(s->avctx, "conceal=%d\n", s->concealment_motion_vectors);
1592 ff_dlog(s->avctx, "intra_vlc_format=%d\n", s->intra_vlc_format);
1593 ff_dlog(s->avctx, "alternate_scan=%d\n", s->alternate_scan);
1594 ff_dlog(s->avctx, "frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
1595 ff_dlog(s->avctx, "progressive_frame=%d\n", s->progressive_frame);
1b6d6674
DB
1596}
1597
1598static int mpeg_field_start(MpegEncContext *s, const uint8_t *buf, int buf_size)
1599{
1600 AVCodecContext *avctx = s->avctx;
a91d3658 1601 Mpeg1Context *s1 = (Mpeg1Context *) s;
3a56bcee 1602 int ret;
1b6d6674
DB
1603
1604 /* start frame decoding */
1605 if (s->first_field || s->picture_structure == PICT_FRAME) {
1606 AVFrameSideData *pan_scan;
1607
3a56bcee
MN
1608 if ((ret = ff_mpv_frame_start(s, avctx)) < 0)
1609 return ret;
1b6d6674
DB
1610
1611 ff_mpeg_er_frame_start(s);
1612
1613 /* first check if we must repeat the frame */
f6774f90 1614 s->current_picture_ptr->f->repeat_pict = 0;
1b6d6674
DB
1615 if (s->repeat_first_field) {
1616 if (s->progressive_sequence) {
1617 if (s->top_field_first)
f6774f90 1618 s->current_picture_ptr->f->repeat_pict = 4;
1b6d6674 1619 else
f6774f90 1620 s->current_picture_ptr->f->repeat_pict = 2;
1b6d6674 1621 } else if (s->progressive_frame) {
f6774f90 1622 s->current_picture_ptr->f->repeat_pict = 1;
1b6d6674
DB
1623 }
1624 }
1625
f6774f90 1626 pan_scan = av_frame_new_side_data(s->current_picture_ptr->f,
1b6d6674
DB
1627 AV_FRAME_DATA_PANSCAN,
1628 sizeof(s1->pan_scan));
1629 if (!pan_scan)
1630 return AVERROR(ENOMEM);
1631 memcpy(pan_scan->data, &s1->pan_scan, sizeof(s1->pan_scan));
1632
1eaac1d6
JS
1633 if (s1->a53_caption) {
1634 AVFrameSideData *sd = av_frame_new_side_data(
f6774f90 1635 s->current_picture_ptr->f, AV_FRAME_DATA_A53_CC,
1eaac1d6
JS
1636 s1->a53_caption_size);
1637 if (sd)
1638 memcpy(sd->data, s1->a53_caption, s1->a53_caption_size);
1639 av_freep(&s1->a53_caption);
1640 }
73eca738
JG
1641
1642 if (s1->has_stereo3d) {
f6774f90 1643 AVStereo3D *stereo = av_stereo3d_create_side_data(s->current_picture_ptr->f);
73eca738
JG
1644 if (!stereo)
1645 return AVERROR(ENOMEM);
1646
1647 *stereo = s1->stereo3d;
1648 s1->has_stereo3d = 0;
1649 }
1ef9e837
KK
1650
1651 if (s1->has_afd) {
1652 AVFrameSideData *sd =
1653 av_frame_new_side_data(s->current_picture_ptr->f,
1654 AV_FRAME_DATA_AFD, 1);
1655 if (!sd)
1656 return AVERROR(ENOMEM);
1657
1658 *sd->data = s1->afd;
1659 s1->has_afd = 0;
1660 }
1661
1b6d6674
DB
1662 if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_FRAME))
1663 ff_thread_finish_setup(avctx);
1664 } else { // second field
1665 int i;
1666
1667 if (!s->current_picture_ptr) {
1668 av_log(s->avctx, AV_LOG_ERROR, "first field missing\n");
3a56bcee 1669 return AVERROR_INVALIDDATA;
1b6d6674
DB
1670 }
1671
1672 if (s->avctx->hwaccel &&
1673 (s->avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD)) {
1674 if (s->avctx->hwaccel->end_frame(s->avctx) < 0)
a91d3658
VG
1675 av_log(avctx, AV_LOG_ERROR,
1676 "hardware accelerator failed to decode first field\n");
1b6d6674
DB
1677 }
1678
1679 for (i = 0; i < 4; i++) {
f6774f90 1680 s->current_picture.f->data[i] = s->current_picture_ptr->f->data[i];
a91d3658 1681 if (s->picture_structure == PICT_BOTTOM_FIELD)
f6774f90 1682 s->current_picture.f->data[i] +=
1683 s->current_picture_ptr->f->linesize[i];
1b6d6674
DB
1684 }
1685 }
1686
1687 if (avctx->hwaccel) {
3a56bcee
MN
1688 if ((ret = avctx->hwaccel->start_frame(avctx, buf, buf_size)) < 0)
1689 return ret;
1b6d6674
DB
1690 }
1691
19e30a58
DB
1692#if FF_API_XVMC
1693FF_DISABLE_DEPRECATION_WARNINGS
835f798c 1694// ff_mpv_frame_start will call this function too,
1b6d6674
DB
1695// but we need to call it on every field
1696 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
1697 if (ff_xvmc_field_start(s, avctx) < 0)
1698 return -1;
19e30a58
DB
1699FF_ENABLE_DEPRECATION_WARNINGS
1700#endif /* FF_API_XVMC */
1b6d6674
DB
1701
1702 return 0;
1703}
1704
1705#define DECODE_SLICE_ERROR -1
1706#define DECODE_SLICE_OK 0
1707
1708/**
1709 * Decode a slice.
1710 * MpegEncContext.mb_y must be set to the MB row from the startcode.
1711 * @return DECODE_SLICE_ERROR if the slice is damaged,
1712 * DECODE_SLICE_OK if this slice is OK
1713 */
1714static int mpeg_decode_slice(MpegEncContext *s, int mb_y,
1715 const uint8_t **buf, int buf_size)
1716{
1717 AVCodecContext *avctx = s->avctx;
1718 const int field_pic = s->picture_structure != PICT_FRAME;
3a56bcee 1719 int ret;
1b6d6674
DB
1720
1721 s->resync_mb_x =
1722 s->resync_mb_y = -1;
1723
1724 assert(mb_y < s->mb_height);
1725
1726 init_get_bits(&s->gb, *buf, buf_size * 8);
1727
1728 ff_mpeg1_clean_buffers(s);
1729 s->interlaced_dct = 0;
1730
1731 s->qscale = get_qscale(s);
1732
1733 if (s->qscale == 0) {
1734 av_log(s->avctx, AV_LOG_ERROR, "qscale == 0\n");
3a56bcee 1735 return AVERROR_INVALIDDATA;
1b6d6674
DB
1736 }
1737
1738 /* extra slice info */
a91d3658 1739 while (get_bits1(&s->gb) != 0)
1b6d6674 1740 skip_bits(&s->gb, 8);
1b6d6674
DB
1741
1742 s->mb_x = 0;
1743
1744 if (mb_y == 0 && s->codec_tag == AV_RL32("SLIF")) {
1745 skip_bits1(&s->gb);
1746 } else {
1747 while (get_bits_left(&s->gb) > 0) {
1748 int code = get_vlc2(&s->gb, ff_mbincr_vlc.table,
1749 MBINCR_VLC_BITS, 2);
1750 if (code < 0) {
1751 av_log(s->avctx, AV_LOG_ERROR, "first mb_incr damaged\n");
3a56bcee 1752 return AVERROR_INVALIDDATA;
1b6d6674
DB
1753 }
1754 if (code >= 33) {
a91d3658 1755 if (code == 33)
1b6d6674 1756 s->mb_x += 33;
1b6d6674
DB
1757 /* otherwise, stuffing, nothing to do */
1758 } else {
1759 s->mb_x += code;
1760 break;
1761 }
1762 }
1763 }
1764
a91d3658 1765 if (s->mb_x >= (unsigned) s->mb_width) {
1b6d6674 1766 av_log(s->avctx, AV_LOG_ERROR, "initial skip overflow\n");
3a56bcee 1767 return AVERROR_INVALIDDATA;
1b6d6674
DB
1768 }
1769
1770 if (avctx->hwaccel) {
1771 const uint8_t *buf_end, *buf_start = *buf - 4; /* include start_code */
1772 int start_code = -1;
1773 buf_end = avpriv_find_start_code(buf_start + 2, *buf + buf_size, &start_code);
1774 if (buf_end < *buf + buf_size)
1775 buf_end -= 4;
1776 s->mb_y = mb_y;
1777 if (avctx->hwaccel->decode_slice(avctx, buf_start, buf_end - buf_start) < 0)
1778 return DECODE_SLICE_ERROR;
1779 *buf = buf_end;
1780 return DECODE_SLICE_OK;
1781 }
1782
1783 s->resync_mb_x = s->mb_x;
1784 s->resync_mb_y = s->mb_y = mb_y;
1785 s->mb_skip_run = 0;
1786 ff_init_block_index(s);
1787
1788 if (s->mb_y == 0 && s->mb_x == 0 && (s->first_field || s->picture_structure == PICT_FRAME)) {
1789 if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
a91d3658
VG
1790 av_log(s->avctx, AV_LOG_DEBUG,
1791 "qp:%d fc:%2d%2d%2d%2d %s %s %s %s %s dc:%d pstruct:%d fdct:%d cmv:%d qtype:%d ivlc:%d rff:%d %s\n",
1792 s->qscale,
1793 s->mpeg_f_code[0][0], s->mpeg_f_code[0][1],
1794 s->mpeg_f_code[1][0], s->mpeg_f_code[1][1],
1795 s->pict_type == AV_PICTURE_TYPE_I ? "I" :
1796 (s->pict_type == AV_PICTURE_TYPE_P ? "P" :
1797 (s->pict_type == AV_PICTURE_TYPE_B ? "B" : "S")),
1798 s->progressive_sequence ? "ps" : "",
1799 s->progressive_frame ? "pf" : "",
1800 s->alternate_scan ? "alt" : "",
1801 s->top_field_first ? "top" : "",
1802 s->intra_dc_precision, s->picture_structure,
1803 s->frame_pred_frame_dct, s->concealment_motion_vectors,
1804 s->q_scale_type, s->intra_vlc_format,
1805 s->repeat_first_field, s->chroma_420_type ? "420" : "");
1b6d6674
DB
1806 }
1807 }
1808
1809 for (;;) {
19e30a58
DB
1810#if FF_API_XVMC
1811FF_DISABLE_DEPRECATION_WARNINGS
1b6d6674
DB
1812 // If 1, we memcpy blocks in xvmcvideo.
1813 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1)
1814 ff_xvmc_init_block(s); // set s->block
19e30a58
DB
1815FF_ENABLE_DEPRECATION_WARNINGS
1816#endif /* FF_API_XVMC */
1b6d6674 1817
3a56bcee
MN
1818 if ((ret = mpeg_decode_mb(s, s->block)) < 0)
1819 return ret;
1b6d6674 1820
a91d3658
VG
1821 // Note motion_val is normally NULL unless we want to extract the MVs.
1822 if (s->current_picture.motion_val[0] && !s->encoding) {
1b6d6674
DB
1823 const int wrap = s->b8_stride;
1824 int xy = s->mb_x * 2 + s->mb_y * 2 * wrap;
1825 int b8_xy = 4 * (s->mb_x + s->mb_y * s->mb_stride);
1826 int motion_x, motion_y, dir, i;
1827
1828 for (i = 0; i < 2; i++) {
1829 for (dir = 0; dir < 2; dir++) {
a91d3658
VG
1830 if (s->mb_intra ||
1831 (dir == 1 && s->pict_type != AV_PICTURE_TYPE_B)) {
1b6d6674 1832 motion_x = motion_y = 0;
a91d3658
VG
1833 } else if (s->mv_type == MV_TYPE_16X16 ||
1834 (s->mv_type == MV_TYPE_FIELD && field_pic)) {
1b6d6674
DB
1835 motion_x = s->mv[dir][0][0];
1836 motion_y = s->mv[dir][0][1];
a91d3658 1837 } else { /* if ((s->mv_type == MV_TYPE_FIELD) || (s->mv_type == MV_TYPE_16X8)) */
1b6d6674
DB
1838 motion_x = s->mv[dir][i][0];
1839 motion_y = s->mv[dir][i][1];
1840 }
1841
a91d3658
VG
1842 s->current_picture.motion_val[dir][xy][0] = motion_x;
1843 s->current_picture.motion_val[dir][xy][1] = motion_y;
1b6d6674
DB
1844 s->current_picture.motion_val[dir][xy + 1][0] = motion_x;
1845 s->current_picture.motion_val[dir][xy + 1][1] = motion_y;
a91d3658 1846 s->current_picture.ref_index [dir][b8_xy] =
1b6d6674 1847 s->current_picture.ref_index [dir][b8_xy + 1] = s->field_select[dir][i];
a91d3658
VG
1848 assert(s->field_select[dir][i] == 0 ||
1849 s->field_select[dir][i] == 1);
1b6d6674 1850 }
a91d3658
VG
1851 xy += wrap;
1852 b8_xy += 2;
1b6d6674
DB
1853 }
1854 }
1855
1856 s->dest[0] += 16;
1857 s->dest[1] += 16 >> s->chroma_x_shift;
1858 s->dest[2] += 16 >> s->chroma_x_shift;
1859
835f798c 1860 ff_mpv_decode_mb(s, s->block);
1b6d6674
DB
1861
1862 if (++s->mb_x >= s->mb_width) {
1863 const int mb_size = 16;
1864
a91d3658 1865 ff_mpeg_draw_horiz_band(s, mb_size * (s->mb_y >> field_pic), mb_size);
835f798c 1866 ff_mpv_report_decode_progress(s);
1b6d6674 1867
a91d3658 1868 s->mb_x = 0;
1b6d6674
DB
1869 s->mb_y += 1 << field_pic;
1870
1871 if (s->mb_y >= s->mb_height) {
1872 int left = get_bits_left(&s->gb);
a91d3658
VG
1873 int is_d10 = s->chroma_format == 2 &&
1874 s->pict_type == AV_PICTURE_TYPE_I &&
1875 avctx->profile == 0 && avctx->level == 5 &&
1876 s->intra_dc_precision == 2 &&
1877 s->q_scale_type == 1 && s->alternate_scan == 0 &&
1878 s->progressive_frame == 0
1879 /* vbv_delay == 0xBBB || 0xE10 */;
1880
1881 if (left < 0 ||
1882 (left && show_bits(&s->gb, FFMIN(left, 23)) && !is_d10) ||
1883 ((avctx->err_recognition & AV_EF_BUFFER) && left > 8)) {
1884 av_log(avctx, AV_LOG_ERROR, "end mismatch left=%d %0X\n",
1885 left, show_bits(&s->gb, FFMIN(left, 23)));
3a56bcee 1886 return AVERROR_INVALIDDATA;
1b6d6674
DB
1887 } else
1888 goto eos;
1889 }
1890
1891 ff_init_block_index(s);
1892 }
1893
1894 /* skip mb handling */
1895 if (s->mb_skip_run == -1) {
1896 /* read increment again */
1897 s->mb_skip_run = 0;
1898 for (;;) {
1899 int code = get_vlc2(&s->gb, ff_mbincr_vlc.table,
1900 MBINCR_VLC_BITS, 2);
1901 if (code < 0) {
1902 av_log(s->avctx, AV_LOG_ERROR, "mb incr damaged\n");
3a56bcee 1903 return AVERROR_INVALIDDATA;
1b6d6674
DB
1904 }
1905 if (code >= 33) {
1906 if (code == 33) {
1907 s->mb_skip_run += 33;
1908 } else if (code == 35) {
1909 if (s->mb_skip_run != 0 || show_bits(&s->gb, 15) != 0) {
1910 av_log(s->avctx, AV_LOG_ERROR, "slice mismatch\n");
3a56bcee 1911 return AVERROR_INVALIDDATA;
1b6d6674
DB
1912 }
1913 goto eos; /* end of slice */
1914 }
1915 /* otherwise, stuffing, nothing to do */
1916 } else {
1917 s->mb_skip_run += code;
1918 break;
1919 }
1920 }
1921 if (s->mb_skip_run) {
1922 int i;
1923 if (s->pict_type == AV_PICTURE_TYPE_I) {
a91d3658
VG
1924 av_log(s->avctx, AV_LOG_ERROR,
1925 "skipped MB in I frame at %d %d\n", s->mb_x, s->mb_y);
3a56bcee 1926 return AVERROR_INVALIDDATA;
1b6d6674
DB
1927 }
1928
1929 /* skip mb */
1930 s->mb_intra = 0;
1931 for (i = 0; i < 12; i++)
1932 s->block_last_index[i] = -1;
1933 if (s->picture_structure == PICT_FRAME)
1934 s->mv_type = MV_TYPE_16X16;
1935 else
1936 s->mv_type = MV_TYPE_FIELD;
1937 if (s->pict_type == AV_PICTURE_TYPE_P) {
1938 /* if P type, zero motion vector is implied */
1939 s->mv_dir = MV_DIR_FORWARD;
1940 s->mv[0][0][0] = s->mv[0][0][1] = 0;
1941 s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
1942 s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
1943 s->field_select[0][0] = (s->picture_structure - 1) & 1;
1944 } else {
1945 /* if B type, reuse previous vectors and directions */
1946 s->mv[0][0][0] = s->last_mv[0][0][0];
1947 s->mv[0][0][1] = s->last_mv[0][0][1];
1948 s->mv[1][0][0] = s->last_mv[1][0][0];
1949 s->mv[1][0][1] = s->last_mv[1][0][1];
1950 }
1951 }
1952 }
1953 }
1954eos: // end of slice
a91d3658 1955 *buf += (get_bits_count(&s->gb) - 1) / 8;
6a85dfc8 1956 ff_dlog(s, "y %d %d %d %d\n", s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y);
1b6d6674
DB
1957 return 0;
1958}
1959
1960static int slice_decode_thread(AVCodecContext *c, void *arg)
1961{
a91d3658 1962 MpegEncContext *s = *(void **) arg;
1b6d6674
DB
1963 const uint8_t *buf = s->gb.buffer;
1964 int mb_y = s->start_mb_y;
1965 const int field_pic = s->picture_structure != PICT_FRAME;
1966
1967 s->er.error_count = (3 * (s->end_mb_y - s->start_mb_y) * s->mb_width) >> field_pic;
1968
1969 for (;;) {
1970 uint32_t start_code;
1971 int ret;
1972
1973 ret = mpeg_decode_slice(s, mb_y, &buf, s->gb.buffer_end - buf);
1974 emms_c();
6a85dfc8 1975 ff_dlog(c, "ret:%d resync:%d/%d mb:%d/%d ts:%d/%d ec:%d\n",
1b6d6674
DB
1976 ret, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y,
1977 s->start_mb_y, s->end_mb_y, s->er.error_count);
1978 if (ret < 0) {
1979 if (c->err_recognition & AV_EF_EXPLODE)
1980 return ret;
1981 if (s->resync_mb_x >= 0 && s->resync_mb_y >= 0)
a91d3658
VG
1982 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
1983 s->mb_x, s->mb_y,
1984 ER_AC_ERROR | ER_DC_ERROR | ER_MV_ERROR);
1b6d6674 1985 } else {
a91d3658
VG
1986 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
1987 s->mb_x - 1, s->mb_y,
1988 ER_AC_END | ER_DC_END | ER_MV_END);
1b6d6674
DB
1989 }
1990
1991 if (s->mb_y == s->end_mb_y)
1992 return 0;
1993
1994 start_code = -1;
a91d3658
VG
1995 buf = avpriv_find_start_code(buf, s->gb.buffer_end, &start_code);
1996 mb_y = (start_code - SLICE_MIN_START_CODE) << field_pic;
1b6d6674
DB
1997 if (s->picture_structure == PICT_BOTTOM_FIELD)
1998 mb_y++;
1999 if (mb_y < 0 || mb_y >= s->end_mb_y)
3a56bcee 2000 return AVERROR_INVALIDDATA;
1b6d6674
DB
2001 }
2002}
2003
2004/**
2005 * Handle slice ends.
2006 * @return 1 if it seems to be the last slice
2007 */
2008static int slice_end(AVCodecContext *avctx, AVFrame *pict)
2009{
a91d3658 2010 Mpeg1Context *s1 = avctx->priv_data;
1b6d6674
DB
2011 MpegEncContext *s = &s1->mpeg_enc_ctx;
2012
2013 if (!s1->mpeg_enc_ctx_allocated || !s->current_picture_ptr)
2014 return 0;
2015
2016 if (s->avctx->hwaccel) {
2017 if (s->avctx->hwaccel->end_frame(s->avctx) < 0)
a91d3658
VG
2018 av_log(avctx, AV_LOG_ERROR,
2019 "hardware accelerator failed to decode picture\n");
1b6d6674
DB
2020 }
2021
19e30a58
DB
2022#if FF_API_XVMC
2023FF_DISABLE_DEPRECATION_WARNINGS
1b6d6674
DB
2024 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
2025 ff_xvmc_field_end(s);
19e30a58
DB
2026FF_ENABLE_DEPRECATION_WARNINGS
2027#endif /* FF_API_XVMC */
1b6d6674
DB
2028
2029 /* end of slice reached */
a91d3658 2030 if (/* s->mb_y << field_pic == s->mb_height && */ !s->first_field) {
1b6d6674
DB
2031 /* end of image */
2032
2033 ff_er_frame_end(&s->er);
2034
835f798c 2035 ff_mpv_frame_end(s);
1b6d6674
DB
2036
2037 if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
f6774f90 2038 int ret = av_frame_ref(pict, s->current_picture_ptr->f);
1b6d6674
DB
2039 if (ret < 0)
2040 return ret;
2041 ff_print_debug_info(s, s->current_picture_ptr);
2042 } else {
2043 if (avctx->active_thread_type & FF_THREAD_FRAME)
2044 s->picture_number++;
2045 /* latency of 1 frame for I- and P-frames */
2046 /* XXX: use another variable than picture_number */
4b1f5e50 2047 if (s->last_picture_ptr) {
f6774f90 2048 int ret = av_frame_ref(pict, s->last_picture_ptr->f);
1b6d6674
DB
2049 if (ret < 0)
2050 return ret;
2051 ff_print_debug_info(s, s->last_picture_ptr);
2052 }
2053 }
2054
2055 return 1;
2056 } else {
2057 return 0;
2058 }
2059}
2060
2061static int mpeg1_decode_sequence(AVCodecContext *avctx,
2062 const uint8_t *buf, int buf_size)
2063{
a91d3658 2064 Mpeg1Context *s1 = avctx->priv_data;
1b6d6674
DB
2065 MpegEncContext *s = &s1->mpeg_enc_ctx;
2066 int width, height;
2067 int i, v, j;
2068
a91d3658 2069 init_get_bits(&s->gb, buf, buf_size * 8);
1b6d6674
DB
2070
2071 width = get_bits(&s->gb, 12);
2072 height = get_bits(&s->gb, 12);
2073 if (width == 0 || height == 0) {
a91d3658
VG
2074 av_log(avctx, AV_LOG_WARNING,
2075 "Invalid horizontal or vertical size value.\n");
1b6d6674
DB
2076 if (avctx->err_recognition & AV_EF_BITSTREAM)
2077 return AVERROR_INVALIDDATA;
2078 }
2079 s->aspect_ratio_info = get_bits(&s->gb, 4);
2080 if (s->aspect_ratio_info == 0) {
2081 av_log(avctx, AV_LOG_ERROR, "aspect ratio has forbidden 0 value\n");
2082 if (avctx->err_recognition & AV_EF_BITSTREAM)
3a56bcee 2083 return AVERROR_INVALIDDATA;
1b6d6674
DB
2084 }
2085 s->frame_rate_index = get_bits(&s->gb, 4);
b56afb48
MN
2086 if (s->frame_rate_index == 0 || s->frame_rate_index > 13) {
2087 av_log(avctx, AV_LOG_WARNING,
2088 "frame_rate_index %d is invalid\n", s->frame_rate_index);
3a56bcee 2089 return AVERROR_INVALIDDATA;
b56afb48 2090 }
1b6d6674 2091 s->bit_rate = get_bits(&s->gb, 18) * 400;
b56afb48
MN
2092 if (get_bits1(&s->gb) == 0) { /* marker */
2093 av_log(avctx, AV_LOG_ERROR, "Marker in sequence header missing\n");
3a56bcee 2094 return AVERROR_INVALIDDATA;
b56afb48 2095 }
1b6d6674
DB
2096 s->width = width;
2097 s->height = height;
2098
2099 s->avctx->rc_buffer_size = get_bits(&s->gb, 10) * 1024 * 16;
2100 skip_bits(&s->gb, 1);
2101
2102 /* get matrix */
2103 if (get_bits1(&s->gb)) {
2104 load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
2105 } else {
2106 for (i = 0; i < 64; i++) {
e3fcb143 2107 j = s->idsp.idct_permutation[i];
1b6d6674
DB
2108 v = ff_mpeg1_default_intra_matrix[i];
2109 s->intra_matrix[j] = v;
2110 s->chroma_intra_matrix[j] = v;
2111 }
2112 }
2113 if (get_bits1(&s->gb)) {
2114 load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
2115 } else {
2116 for (i = 0; i < 64; i++) {
e3fcb143 2117 int j = s->idsp.idct_permutation[i];
1b6d6674
DB
2118 v = ff_mpeg1_default_non_intra_matrix[i];
2119 s->inter_matrix[j] = v;
2120 s->chroma_inter_matrix[j] = v;
2121 }
2122 }
2123
2124 if (show_bits(&s->gb, 23) != 0) {
2125 av_log(s->avctx, AV_LOG_ERROR, "sequence header damaged\n");
3a56bcee 2126 return AVERROR_INVALIDDATA;
1b6d6674
DB
2127 }
2128
a91d3658 2129 /* We set MPEG-2 parameters so that it emulates MPEG-1. */
1b6d6674
DB
2130 s->progressive_sequence = 1;
2131 s->progressive_frame = 1;
2132 s->picture_structure = PICT_FRAME;
2133 s->frame_pred_frame_dct = 1;
2134 s->chroma_format = 1;
a91d3658
VG
2135 s->codec_id =
2136 s->avctx->codec_id = AV_CODEC_ID_MPEG1VIDEO;
1b6d6674 2137 s->out_format = FMT_MPEG1;
848e86f7 2138 if (s->avctx->flags & CODEC_FLAG_LOW_DELAY)
1b6d6674
DB
2139 s->low_delay = 1;
2140
2141 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
2142 av_log(s->avctx, AV_LOG_DEBUG, "vbv buffer: %d, bitrate:%d\n",
2143 s->avctx->rc_buffer_size, s->bit_rate);
2144
2145 return 0;
2146}
2147
2148static int vcr2_init_sequence(AVCodecContext *avctx)
2149{
a91d3658 2150 Mpeg1Context *s1 = avctx->priv_data;
1b6d6674 2151 MpegEncContext *s = &s1->mpeg_enc_ctx;
3a56bcee 2152 int i, v, ret;
1b6d6674
DB
2153
2154 /* start new MPEG-1 context decoding */
2155 s->out_format = FMT_MPEG1;
2156 if (s1->mpeg_enc_ctx_allocated) {
835f798c 2157 ff_mpv_common_end(s);
1b6d6674 2158 }
a91d3658
VG
2159 s->width = avctx->coded_width;
2160 s->height = avctx->coded_height;
1b6d6674 2161 avctx->has_b_frames = 0; // true?
a91d3658 2162 s->low_delay = 1;
1b6d6674
DB
2163
2164 avctx->pix_fmt = mpeg_get_pixelformat(avctx);
1b6d6674 2165
19e30a58
DB
2166#if FF_API_XVMC
2167 if ((avctx->pix_fmt == AV_PIX_FMT_XVMC_MPEG2_IDCT || avctx->hwaccel) &&
2168 avctx->idct_algo == FF_IDCT_AUTO)
2169#else
2170 if (avctx->hwaccel && avctx->idct_algo == FF_IDCT_AUTO)
2171#endif /* FF_API_XVMC */
2172 avctx->idct_algo = FF_IDCT_SIMPLE;
1b6d6674 2173
998c9f15 2174 ff_mpv_idct_init(s);
3a56bcee
MN
2175 if ((ret = ff_mpv_common_init(s)) < 0)
2176 return ret;
1b6d6674
DB
2177 s1->mpeg_enc_ctx_allocated = 1;
2178
2179 for (i = 0; i < 64; i++) {
e3fcb143 2180 int j = s->idsp.idct_permutation[i];
1b6d6674
DB
2181 v = ff_mpeg1_default_intra_matrix[i];
2182 s->intra_matrix[j] = v;
2183 s->chroma_intra_matrix[j] = v;
2184
2185 v = ff_mpeg1_default_non_intra_matrix[i];
2186 s->inter_matrix[j] = v;
2187 s->chroma_inter_matrix[j] = v;
2188 }
2189
2190 s->progressive_sequence = 1;
2191 s->progressive_frame = 1;
2192 s->picture_structure = PICT_FRAME;
2193 s->frame_pred_frame_dct = 1;
2194 s->chroma_format = 1;
2195 s->codec_id = s->avctx->codec_id = AV_CODEC_ID_MPEG2VIDEO;
2196 s1->save_width = s->width;
2197 s1->save_height = s->height;
2198 s1->save_progressive_seq = s->progressive_sequence;
2199 return 0;
2200}
2201
1eaac1d6
JS
2202static int mpeg_decode_a53_cc(AVCodecContext *avctx,
2203 const uint8_t *p, int buf_size)
2204{
2205 Mpeg1Context *s1 = avctx->priv_data;
2206
2207 if (buf_size >= 6 &&
2208 p[0] == 'G' && p[1] == 'A' && p[2] == '9' && p[3] == '4' &&
2209 p[4] == 3 && (p[5] & 0x40)) {
2210 /* extract A53 Part 4 CC data */
2211 int cc_count = p[5] & 0x1f;
2212 if (cc_count > 0 && buf_size >= 7 + cc_count * 3) {
2213 av_freep(&s1->a53_caption);
2214 s1->a53_caption_size = cc_count * 3;
a91d3658
VG
2215 s1->a53_caption = av_malloc(s1->a53_caption_size);
2216 if (s1->a53_caption)
1eaac1d6 2217 memcpy(s1->a53_caption, p + 7, s1->a53_caption_size);
1eaac1d6
JS
2218 }
2219 return 1;
2220 } else if (buf_size >= 11 &&
a91d3658 2221 p[0] == 'C' && p[1] == 'C' && p[2] == 0x01 && p[3] == 0xf8) {
1eaac1d6
JS
2222 /* extract DVD CC data */
2223 int cc_count = 0;
2224 int i;
2225 // There is a caption count field in the data, but it is often
2226 // incorect. So count the number of captions present.
2227 for (i = 5; i + 6 <= buf_size && ((p[i] & 0xfe) == 0xfe); i += 6)
2228 cc_count++;
2229 // Transform the DVD format into A53 Part 4 format
2230 if (cc_count > 0) {
2231 av_freep(&s1->a53_caption);
2232 s1->a53_caption_size = cc_count * 6;
a91d3658 2233 s1->a53_caption = av_malloc(s1->a53_caption_size);
1eaac1d6
JS
2234 if (s1->a53_caption) {
2235 uint8_t field1 = !!(p[4] & 0x80);
2236 uint8_t *cap = s1->a53_caption;
2237 p += 5;
2238 for (i = 0; i < cc_count; i++) {
2239 cap[0] = (p[0] == 0xff && field1) ? 0xfc : 0xfd;
2240 cap[1] = p[1];
2241 cap[2] = p[2];
2242 cap[3] = (p[3] == 0xff && !field1) ? 0xfc : 0xfd;
2243 cap[4] = p[4];
2244 cap[5] = p[5];
2245 cap += 6;
2246 p += 6;
2247 }
2248 }
2249 }
2250 return 1;
2251 }
2252 return 0;
2253}
2254
1b6d6674
DB
2255static void mpeg_decode_user_data(AVCodecContext *avctx,
2256 const uint8_t *p, int buf_size)
2257{
2258 const uint8_t *buf_end = p + buf_size;
1ef9e837 2259 Mpeg1Context *s1 = avctx->priv_data;
1b6d6674
DB
2260
2261 /* we parse the DTG active format information */
2262 if (buf_end - p >= 5 &&
2263 p[0] == 'D' && p[1] == 'T' && p[2] == 'G' && p[3] == '1') {
2264 int flags = p[4];
2265 p += 5;
2266 if (flags & 0x80) {
2267 /* skip event id */
2268 p += 2;
2269 }
2270 if (flags & 0x40) {
2271 if (buf_end - p < 1)
2272 return;
1ef9e837 2273#if FF_API_AFD
990e2f35 2274FF_DISABLE_DEPRECATION_WARNINGS
1b6d6674 2275 avctx->dtg_active_format = p[0] & 0x0f;
990e2f35 2276FF_ENABLE_DEPRECATION_WARNINGS
1ef9e837
KK
2277#endif /* FF_API_AFD */
2278 s1->has_afd = 1;
2279 s1->afd = p[0] & 0x0f;
1b6d6674 2280 }
bacc2869
VG
2281 } else if (buf_end - p >= 6 &&
2282 p[0] == 'J' && p[1] == 'P' && p[2] == '3' && p[3] == 'D' &&
2283 p[4] == 0x03) { // S3D_video_format_length
2284 // the 0x7F mask ignores the reserved_bit value
2285 const uint8_t S3D_video_format_type = p[5] & 0x7F;
2286
2287 if (S3D_video_format_type == 0x03 ||
2288 S3D_video_format_type == 0x04 ||
2289 S3D_video_format_type == 0x08 ||
2290 S3D_video_format_type == 0x23) {
73eca738
JG
2291
2292 s1->has_stereo3d = 1;
bacc2869
VG
2293
2294 switch (S3D_video_format_type) {
2295 case 0x03:
73eca738 2296 s1->stereo3d.type = AV_STEREO3D_SIDEBYSIDE;
bacc2869
VG
2297 break;
2298 case 0x04:
73eca738 2299 s1->stereo3d.type = AV_STEREO3D_TOPBOTTOM;
bacc2869
VG
2300 break;
2301 case 0x08:
73eca738 2302 s1->stereo3d.type = AV_STEREO3D_2D;
bacc2869
VG
2303 break;
2304 case 0x23:
73eca738 2305 s1->stereo3d.type = AV_STEREO3D_SIDEBYSIDE_QUINCUNX;
bacc2869
VG
2306 break;
2307 }
2308 }
1eaac1d6
JS
2309 } else if (mpeg_decode_a53_cc(avctx, p, buf_size)) {
2310 return;
1b6d6674
DB
2311 }
2312}
2313
2314static void mpeg_decode_gop(AVCodecContext *avctx,
2315 const uint8_t *buf, int buf_size)
2316{
2317 Mpeg1Context *s1 = avctx->priv_data;
2318 MpegEncContext *s = &s1->mpeg_enc_ctx;
2319
2320 int time_code_hours, time_code_minutes;
2321 int time_code_seconds, time_code_pictures;
2322 int broken_link;
2323
a91d3658 2324 init_get_bits(&s->gb, buf, buf_size * 8);
1b6d6674
DB
2325
2326 skip_bits1(&s->gb); /* drop_frame_flag */
2327
2328 time_code_hours = get_bits(&s->gb, 5);
2329 time_code_minutes = get_bits(&s->gb, 6);
2330 skip_bits1(&s->gb); // marker bit
2331 time_code_seconds = get_bits(&s->gb, 6);
2332 time_code_pictures = get_bits(&s->gb, 6);
2333
2334 s1->closed_gop = get_bits1(&s->gb);
a91d3658
VG
2335 /* broken_link indicate that after editing the
2336 * reference frames of the first B-Frames after GOP I-Frame
2337 * are missing (open gop) */
1b6d6674
DB
2338 broken_link = get_bits1(&s->gb);
2339
2340 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
a91d3658
VG
2341 av_log(s->avctx, AV_LOG_DEBUG,
2342 "GOP (%2d:%02d:%02d.[%02d]) closed_gop=%d broken_link=%d\n",
1b6d6674
DB
2343 time_code_hours, time_code_minutes, time_code_seconds,
2344 time_code_pictures, s1->closed_gop, broken_link);
2345}
2346
a91d3658
VG
2347static int decode_chunks(AVCodecContext *avctx, AVFrame *picture,
2348 int *got_output, const uint8_t *buf, int buf_size)
1b6d6674
DB
2349{
2350 Mpeg1Context *s = avctx->priv_data;
2351 MpegEncContext *s2 = &s->mpeg_enc_ctx;
2352 const uint8_t *buf_ptr = buf;
2353 const uint8_t *buf_end = buf + buf_size;
2354 int ret, input_size;
bf20cdbd 2355 int last_code = 0, skip_frame = 0;
1b6d6674
DB
2356
2357 for (;;) {
2358 /* find next start code */
2359 uint32_t start_code = -1;
2360 buf_ptr = avpriv_find_start_code(buf_ptr, buf_end, &start_code);
2361 if (start_code > 0x1ff) {
bf20cdbd 2362 if (!skip_frame) {
a91d3658
VG
2363 if (HAVE_THREADS &&
2364 (avctx->active_thread_type & FF_THREAD_SLICE) &&
93a51984 2365 !avctx->hwaccel) {
1b6d6674
DB
2366 int i;
2367
a91d3658
VG
2368 avctx->execute(avctx, slice_decode_thread,
2369 &s2->thread_context[0], NULL,
2370 s->slice_count, sizeof(void *));
1b6d6674
DB
2371 for (i = 0; i < s->slice_count; i++)
2372 s2->er.error_count += s2->thread_context[i]->er.error_count;
2373 }
2374
1b6d6674
DB
2375 ret = slice_end(avctx, picture);
2376 if (ret < 0)
2377 return ret;
2378 else if (ret) {
a91d3658
VG
2379 // FIXME: merge with the stuff in mpeg_decode_slice
2380 if (s2->last_picture_ptr || s2->low_delay)
1b6d6674
DB
2381 *got_output = 1;
2382 }
2383 }
2384 s2->pict_type = 0;
2385 return FFMAX(0, buf_ptr - buf - s2->parse_context.last_index);
2386 }
2387
2388 input_size = buf_end - buf_ptr;
2389
a91d3658 2390 if (avctx->debug & FF_DEBUG_STARTCODE)
cc8163e1 2391 av_log(avctx, AV_LOG_DEBUG, "%3"PRIX32" at %td left %d\n",
a91d3658 2392 start_code, buf_ptr - buf, input_size);
1b6d6674
DB
2393
2394 /* prepare data for next start code */
2395 switch (start_code) {
2396 case SEQ_START_CODE:
2397 if (last_code == 0) {
2398 mpeg1_decode_sequence(avctx, buf_ptr, input_size);
a91d3658 2399 s->sync = 1;
1b6d6674 2400 } else {
a91d3658
VG
2401 av_log(avctx, AV_LOG_ERROR,
2402 "ignoring SEQ_START_CODE after %X\n", last_code);
1b6d6674
DB
2403 if (avctx->err_recognition & AV_EF_EXPLODE)
2404 return AVERROR_INVALIDDATA;
2405 }
2406 break;
2407
2408 case PICTURE_START_CODE:
2409 if (s2->width <= 0 || s2->height <= 0) {
2410 av_log(avctx, AV_LOG_ERROR, "Invalid frame dimensions %dx%d.\n",
2411 s2->width, s2->height);
2412 return AVERROR_INVALIDDATA;
2413 }
2414
93a51984
RDC
2415 if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE) &&
2416 !avctx->hwaccel && s->slice_count) {
1b6d6674
DB
2417 int i;
2418
2419 avctx->execute(avctx, slice_decode_thread,
2420 s2->thread_context, NULL,
a91d3658 2421 s->slice_count, sizeof(void *));
1b6d6674
DB
2422 for (i = 0; i < s->slice_count; i++)
2423 s2->er.error_count += s2->thread_context[i]->er.error_count;
2424 s->slice_count = 0;
2425 }
2426 if (last_code == 0 || last_code == SLICE_MIN_START_CODE) {
2427 ret = mpeg_decode_postinit(avctx);
2428 if (ret < 0) {
a91d3658
VG
2429 av_log(avctx, AV_LOG_ERROR,
2430 "mpeg_decode_postinit() failure\n");
1b6d6674
DB
2431 return ret;
2432 }
2433
a91d3658 2434 /* We have a complete image: we try to decompress it. */
1b6d6674
DB
2435 if (mpeg1_decode_picture(avctx, buf_ptr, input_size) < 0)
2436 s2->pict_type = 0;
a097f004 2437 s->first_slice = 1;
a91d3658 2438 last_code = PICTURE_START_CODE;
1b6d6674 2439 } else {
a91d3658
VG
2440 av_log(avctx, AV_LOG_ERROR,
2441 "ignoring pic after %X\n", last_code);
1b6d6674
DB
2442 if (avctx->err_recognition & AV_EF_EXPLODE)
2443 return AVERROR_INVALIDDATA;
2444 }
2445 break;
2446 case EXT_START_CODE:
a91d3658 2447 init_get_bits(&s2->gb, buf_ptr, input_size * 8);
1b6d6674
DB
2448
2449 switch (get_bits(&s2->gb, 4)) {
2450 case 0x1:
2451 if (last_code == 0) {
a91d3658 2452 mpeg_decode_sequence_extension(s);
1b6d6674 2453 } else {
a91d3658
VG
2454 av_log(avctx, AV_LOG_ERROR,
2455 "ignoring seq ext after %X\n", last_code);
1b6d6674
DB
2456 if (avctx->err_recognition & AV_EF_EXPLODE)
2457 return AVERROR_INVALIDDATA;
2458 }
2459 break;
2460 case 0x2:
2461 mpeg_decode_sequence_display_extension(s);
2462 break;
2463 case 0x3:
2464 mpeg_decode_quant_matrix_extension(s2);
2465 break;
2466 case 0x7:
2467 mpeg_decode_picture_display_extension(s);
2468 break;
2469 case 0x8:
2470 if (last_code == PICTURE_START_CODE) {
2471 mpeg_decode_picture_coding_extension(s);
2472 } else {
a91d3658
VG
2473 av_log(avctx, AV_LOG_ERROR,
2474 "ignoring pic cod ext after %X\n", last_code);
1b6d6674
DB
2475 if (avctx->err_recognition & AV_EF_EXPLODE)
2476 return AVERROR_INVALIDDATA;
2477 }
2478 break;
2479 }
2480 break;
2481 case USER_START_CODE:
2482 mpeg_decode_user_data(avctx, buf_ptr, input_size);
2483 break;
2484 case GOP_START_CODE:
2485 if (last_code == 0) {
a91d3658 2486 s2->first_field = 0;
1b6d6674 2487 mpeg_decode_gop(avctx, buf_ptr, input_size);
a91d3658 2488 s->sync = 1;
1b6d6674 2489 } else {
a91d3658
VG
2490 av_log(avctx, AV_LOG_ERROR,
2491 "ignoring GOP_START_CODE after %X\n", last_code);
1b6d6674
DB
2492 if (avctx->err_recognition & AV_EF_EXPLODE)
2493 return AVERROR_INVALIDDATA;
2494 }
2495 break;
2496 default:
2497 if (start_code >= SLICE_MIN_START_CODE &&
2498 start_code <= SLICE_MAX_START_CODE && last_code != 0) {
2499 const int field_pic = s2->picture_structure != PICT_FRAME;
2500 int mb_y = (start_code - SLICE_MIN_START_CODE) << field_pic;
2501 last_code = SLICE_MIN_START_CODE;
2502
2503 if (s2->picture_structure == PICT_BOTTOM_FIELD)
2504 mb_y++;
2505
2506 if (mb_y >= s2->mb_height) {
a91d3658
VG
2507 av_log(s2->avctx, AV_LOG_ERROR,
2508 "slice below image (%d >= %d)\n", mb_y, s2->mb_height);
3a56bcee 2509 return AVERROR_INVALIDDATA;
1b6d6674
DB
2510 }
2511
f929ab05 2512 if (!s2->last_picture_ptr) {
a91d3658
VG
2513 /* Skip B-frames if we do not have reference frames and
2514 * GOP is not closed. */
1b6d6674 2515 if (s2->pict_type == AV_PICTURE_TYPE_B) {
bf20cdbd
JG
2516 if (!s->closed_gop) {
2517 skip_frame = 1;
1b6d6674 2518 break;
bf20cdbd 2519 }
1b6d6674
DB
2520 }
2521 }
2522 if (s2->pict_type == AV_PICTURE_TYPE_I)
a91d3658 2523 s->sync = 1;
f929ab05 2524 if (!s2->next_picture_ptr) {
a91d3658
VG
2525 /* Skip P-frames if we do not have a reference frame or
2526 * we have an invalid header. */
bf20cdbd
JG
2527 if (s2->pict_type == AV_PICTURE_TYPE_P && !s->sync) {
2528 skip_frame = 1;
2529 break;
2530 }
1b6d6674 2531 }
a91d3658
VG
2532 if ((avctx->skip_frame >= AVDISCARD_NONREF &&
2533 s2->pict_type == AV_PICTURE_TYPE_B) ||
2534 (avctx->skip_frame >= AVDISCARD_NONKEY &&
2535 s2->pict_type != AV_PICTURE_TYPE_I) ||
2536 avctx->skip_frame >= AVDISCARD_ALL) {
bf20cdbd 2537 skip_frame = 1;
1b6d6674 2538 break;
bf20cdbd 2539 }
1b6d6674
DB
2540
2541 if (!s->mpeg_enc_ctx_allocated)
2542 break;
2543
2544 if (s2->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
a91d3658
VG
2545 if (mb_y < avctx->skip_top ||
2546 mb_y >= s2->mb_height - avctx->skip_bottom)
1b6d6674
DB
2547 break;
2548 }
2549
2550 if (!s2->pict_type) {
2551 av_log(avctx, AV_LOG_ERROR, "Missing picture start code\n");
2552 if (avctx->err_recognition & AV_EF_EXPLODE)
2553 return AVERROR_INVALIDDATA;
2554 break;
2555 }
2556
a097f004 2557 if (s->first_slice) {
a91d3658 2558 skip_frame = 0;
a097f004 2559 s->first_slice = 0;
3a56bcee
MN
2560 if ((ret = mpeg_field_start(s2, buf, buf_size)) < 0)
2561 return ret;
1b6d6674
DB
2562 }
2563 if (!s2->current_picture_ptr) {
a91d3658
VG
2564 av_log(avctx, AV_LOG_ERROR,
2565 "current_picture not initialized\n");
1b6d6674
DB
2566 return AVERROR_INVALIDDATA;
2567 }
2568
a91d3658
VG
2569 if (HAVE_THREADS &&
2570 (avctx->active_thread_type & FF_THREAD_SLICE) &&
93a51984 2571 !avctx->hwaccel) {
1b6d6674
DB
2572 int threshold = (s2->mb_height * s->slice_count +
2573 s2->slice_context_count / 2) /
2574 s2->slice_context_count;
2575 if (threshold <= mb_y) {
2576 MpegEncContext *thread_context = s2->thread_context[s->slice_count];
2577
2578 thread_context->start_mb_y = mb_y;
2579 thread_context->end_mb_y = s2->mb_height;
2580 if (s->slice_count) {
a91d3658
VG
2581 s2->thread_context[s->slice_count - 1]->end_mb_y = mb_y;
2582 ret = ff_update_duplicate_context(thread_context, s2);
1b6d6674
DB
2583 if (ret < 0)
2584 return ret;
2585 }
a91d3658 2586 init_get_bits(&thread_context->gb, buf_ptr, input_size * 8);
1b6d6674
DB
2587 s->slice_count++;
2588 }
2589 buf_ptr += 2; // FIXME add minimum number of bytes per slice
2590 } else {
2591 ret = mpeg_decode_slice(s2, mb_y, &buf_ptr, input_size);
2592 emms_c();
2593
2594 if (ret < 0) {
2595 if (avctx->err_recognition & AV_EF_EXPLODE)
2596 return ret;
2597 if (s2->resync_mb_x >= 0 && s2->resync_mb_y >= 0)
a91d3658
VG
2598 ff_er_add_slice(&s2->er, s2->resync_mb_x,
2599 s2->resync_mb_y, s2->mb_x, s2->mb_y,
2600 ER_AC_ERROR | ER_DC_ERROR | ER_MV_ERROR);
1b6d6674 2601 } else {
a91d3658
VG
2602 ff_er_add_slice(&s2->er, s2->resync_mb_x,
2603 s2->resync_mb_y, s2->mb_x - 1, s2->mb_y,
2604 ER_AC_END | ER_DC_END | ER_MV_END);
1b6d6674
DB
2605 }
2606 }
2607 }
2608 break;
2609 }
2610 }
2611}
2612
a91d3658
VG
2613static int mpeg_decode_frame(AVCodecContext *avctx, void *data,
2614 int *got_output, AVPacket *avpkt)
1b6d6674
DB
2615{
2616 const uint8_t *buf = avpkt->data;
2617 int buf_size = avpkt->size;
2618 Mpeg1Context *s = avctx->priv_data;
2619 AVFrame *picture = data;
2620 MpegEncContext *s2 = &s->mpeg_enc_ctx;
6a85dfc8 2621 ff_dlog(avctx, "fill_buffer\n");
1b6d6674
DB
2622
2623 if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == SEQ_END_CODE)) {
2624 /* special case for last picture */
2625 if (s2->low_delay == 0 && s2->next_picture_ptr) {
f6774f90 2626 int ret = av_frame_ref(picture, s2->next_picture_ptr->f);
1b6d6674
DB
2627 if (ret < 0)
2628 return ret;
2629
2630 s2->next_picture_ptr = NULL;
2631
2632 *got_output = 1;
2633 }
2634 return buf_size;
2635 }
2636
848e86f7 2637 if (s2->avctx->flags & CODEC_FLAG_TRUNCATED) {
a91d3658
VG
2638 int next = ff_mpeg1_find_frame_end(&s2->parse_context, buf,
2639 buf_size, NULL);
1b6d6674 2640
a91d3658
VG
2641 if (ff_combine_frame(&s2->parse_context, next,
2642 (const uint8_t **) &buf, &buf_size) < 0)
1b6d6674
DB
2643 return buf_size;
2644 }
2645
2646 if (s->mpeg_enc_ctx_allocated == 0 && avctx->codec_tag == AV_RL32("VCR2"))
2647 vcr2_init_sequence(avctx);
2648
2649 s->slice_count = 0;
2650
2651 if (avctx->extradata && !s->extradata_decoded) {
a91d3658
VG
2652 int ret = decode_chunks(avctx, picture, got_output,
2653 avctx->extradata, avctx->extradata_size);
1b6d6674
DB
2654 s->extradata_decoded = 1;
2655 if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE))
2656 return ret;
2657 }
2658
2659 return decode_chunks(avctx, picture, got_output, buf, buf_size);
2660}
2661
1b6d6674
DB
2662static void flush(AVCodecContext *avctx)
2663{
2664 Mpeg1Context *s = avctx->priv_data;
2665
a91d3658 2666 s->sync = 0;
1b6d6674
DB
2667 s->closed_gop = 0;
2668
2669 ff_mpeg_flush(avctx);
2670}
2671
bd8ac882 2672static av_cold int mpeg_decode_end(AVCodecContext *avctx)
1b6d6674
DB
2673{
2674 Mpeg1Context *s = avctx->priv_data;
2675
2676 if (s->mpeg_enc_ctx_allocated)
835f798c 2677 ff_mpv_common_end(&s->mpeg_enc_ctx);
1eaac1d6 2678 av_freep(&s->a53_caption);
1b6d6674
DB
2679 return 0;
2680}
2681
2682static const AVProfile mpeg2_video_profiles[] = {
2683 { FF_PROFILE_MPEG2_422, "4:2:2" },
2684 { FF_PROFILE_MPEG2_HIGH, "High" },
2685 { FF_PROFILE_MPEG2_SS, "Spatially Scalable" },
2686 { FF_PROFILE_MPEG2_SNR_SCALABLE, "SNR Scalable" },
2687 { FF_PROFILE_MPEG2_MAIN, "Main" },
2688 { FF_PROFILE_MPEG2_SIMPLE, "Simple" },
2689 { FF_PROFILE_RESERVED, "Reserved" },
2690 { FF_PROFILE_RESERVED, "Reserved" },
a91d3658 2691 { FF_PROFILE_UNKNOWN },
1b6d6674
DB
2692};
2693
1b6d6674
DB
2694AVCodec ff_mpeg1video_decoder = {
2695 .name = "mpeg1video",
b2bed932 2696 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video"),
1b6d6674
DB
2697 .type = AVMEDIA_TYPE_VIDEO,
2698 .id = AV_CODEC_ID_MPEG1VIDEO,
2699 .priv_data_size = sizeof(Mpeg1Context),
2700 .init = mpeg_decode_init,
2701 .close = mpeg_decode_end,
2702 .decode = mpeg_decode_frame,
2703 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
2704 CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY |
2705 CODEC_CAP_SLICE_THREADS,
2706 .flush = flush,
1b6d6674
DB
2707 .update_thread_context = ONLY_IF_THREADS_ENABLED(mpeg_decode_update_thread_context)
2708};
2709
2710AVCodec ff_mpeg2video_decoder = {
2711 .name = "mpeg2video",
b2bed932 2712 .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 video"),
1b6d6674
DB
2713 .type = AVMEDIA_TYPE_VIDEO,
2714 .id = AV_CODEC_ID_MPEG2VIDEO,
2715 .priv_data_size = sizeof(Mpeg1Context),
2716 .init = mpeg_decode_init,
2717 .close = mpeg_decode_end,
2718 .decode = mpeg_decode_frame,
2719 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
2720 CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY |
2721 CODEC_CAP_SLICE_THREADS,
2722 .flush = flush,
1b6d6674
DB
2723 .profiles = NULL_IF_CONFIG_SMALL(mpeg2_video_profiles),
2724};
2725
19e30a58 2726#if FF_API_XVMC
1b6d6674
DB
2727#if CONFIG_MPEG_XVMC_DECODER
2728static av_cold int mpeg_mc_decode_init(AVCodecContext *avctx)
2729{
2730 if (avctx->active_thread_type & FF_THREAD_SLICE)
2731 return -1;
2732 if (!(avctx->slice_flags & SLICE_FLAG_CODED_ORDER))
2733 return -1;
2734 if (!(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD)) {
6a85dfc8 2735 ff_dlog(avctx, "mpeg12.c: XvMC decoder will work better if SLICE_FLAG_ALLOW_FIELD is set\n");
1b6d6674
DB
2736 }
2737 mpeg_decode_init(avctx);
2738
2739 avctx->pix_fmt = AV_PIX_FMT_XVMC_MPEG2_IDCT;
2740 avctx->xvmc_acceleration = 2; // 2 - the blocks are packed!
2741
2742 return 0;
2743}
2744
2745AVCodec ff_mpeg_xvmc_decoder = {
2746 .name = "mpegvideo_xvmc",
b2bed932 2747 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1/2 video XvMC (X-Video Motion Compensation)"),
1b6d6674
DB
2748 .type = AVMEDIA_TYPE_VIDEO,
2749 .id = AV_CODEC_ID_MPEG2VIDEO_XVMC,
2750 .priv_data_size = sizeof(Mpeg1Context),
2751 .init = mpeg_mc_decode_init,
2752 .close = mpeg_decode_end,
2753 .decode = mpeg_decode_frame,
2754 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
a91d3658 2755 CODEC_CAP_TRUNCATED | CODEC_CAP_HWACCEL | CODEC_CAP_DELAY,
1b6d6674 2756 .flush = flush,
1b6d6674
DB
2757};
2758
2759#endif
19e30a58 2760#endif /* FF_API_XVMC */