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