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