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