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