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