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 -1;
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 -1;
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 -1;
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 
737  av_dlog(s->avctx, "decode_mb: x=%d y=%d\n", s->mb_x, s->mb_y);
738 
739  av_assert2(s->mb_skipped == 0);
740 
741  if (s->mb_skip_run-- != 0) {
742  if (s->pict_type == AV_PICTURE_TYPE_P) {
743  s->mb_skipped = 1;
744  s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride] =
746  } else {
747  int mb_type;
748 
749  if (s->mb_x)
750  mb_type = s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride - 1];
751  else
752  // FIXME not sure if this is allowed in MPEG at all
753  mb_type = s->current_picture.mb_type[s->mb_width + (s->mb_y - 1) * s->mb_stride - 1];
754  if (IS_INTRA(mb_type)) {
755  av_log(s->avctx, AV_LOG_ERROR, "skip with previntra\n");
756  return -1;
757  }
758  s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride] =
759  mb_type | MB_TYPE_SKIP;
760 
761  if ((s->mv[0][0][0] | s->mv[0][0][1] | s->mv[1][0][0] | s->mv[1][0][1]) == 0)
762  s->mb_skipped = 1;
763  }
764 
765  return 0;
766  }
767 
768  switch (s->pict_type) {
769  default:
770  case AV_PICTURE_TYPE_I:
771  if (get_bits1(&s->gb) == 0) {
772  if (get_bits1(&s->gb) == 0) {
774  "invalid mb type in I Frame at %d %d\n",
775  s->mb_x, s->mb_y);
776  return -1;
777  }
778  mb_type = MB_TYPE_QUANT | MB_TYPE_INTRA;
779  } else {
780  mb_type = MB_TYPE_INTRA;
781  }
782  break;
783  case AV_PICTURE_TYPE_P:
784  mb_type = get_vlc2(&s->gb, ff_mb_ptype_vlc.table, MB_PTYPE_VLC_BITS, 1);
785  if (mb_type < 0) {
787  "invalid mb type in P Frame at %d %d\n", s->mb_x, s->mb_y);
788  return -1;
789  }
790  mb_type = ptype2mb_type[mb_type];
791  break;
792  case AV_PICTURE_TYPE_B:
793  mb_type = get_vlc2(&s->gb, ff_mb_btype_vlc.table, MB_BTYPE_VLC_BITS, 1);
794  if (mb_type < 0) {
796  "invalid mb type in B Frame at %d %d\n", s->mb_x, s->mb_y);
797  return -1;
798  }
799  mb_type = btype2mb_type[mb_type];
800  break;
801  }
802  av_dlog(s->avctx, "mb_type=%x\n", mb_type);
803 // motion_type = 0; /* avoid warning */
804  if (IS_INTRA(mb_type)) {
805  s->bdsp.clear_blocks(s->block[0]);
806 
807  if (!s->chroma_y_shift)
808  s->bdsp.clear_blocks(s->block[6]);
809 
810  /* compute DCT type */
811  // FIXME: add an interlaced_dct coded var?
812  if (s->picture_structure == PICT_FRAME &&
814  s->interlaced_dct = get_bits1(&s->gb);
815 
816  if (IS_QUANT(mb_type))
817  s->qscale = get_qscale(s);
818 
820  /* just parse them */
821  if (s->picture_structure != PICT_FRAME)
822  skip_bits1(&s->gb); /* field select */
823 
824  s->mv[0][0][0] =
825  s->last_mv[0][0][0] =
826  s->last_mv[0][1][0] = mpeg_decode_motion(s, s->mpeg_f_code[0][0],
827  s->last_mv[0][0][0]);
828  s->mv[0][0][1] =
829  s->last_mv[0][0][1] =
830  s->last_mv[0][1][1] = mpeg_decode_motion(s, s->mpeg_f_code[0][1],
831  s->last_mv[0][0][1]);
832 
833  skip_bits1(&s->gb); /* marker */
834  } else {
835  /* reset mv prediction */
836  memset(s->last_mv, 0, sizeof(s->last_mv));
837  }
838  s->mb_intra = 1;
839  // if 1, we memcpy blocks in xvmcvideo
840  if ((CONFIG_MPEG1_XVMC_HWACCEL || CONFIG_MPEG2_XVMC_HWACCEL) && s->pack_pblocks)
841  ff_xvmc_pack_pblocks(s, -1); // inter are always full blocks
842 
843  if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
844  if (s->flags2 & CODEC_FLAG2_FAST) {
845  for (i = 0; i < 6; i++)
847  } else {
848  for (i = 0; i < mb_block_count; i++)
849  if (mpeg2_decode_block_intra(s, *s->pblocks[i], i) < 0)
850  return -1;
851  }
852  } else {
853  for (i = 0; i < 6; i++)
854  if (mpeg1_decode_block_intra(s, *s->pblocks[i], i) < 0)
855  return -1;
856  }
857  } else {
858  if (mb_type & MB_TYPE_ZERO_MV) {
859  av_assert2(mb_type & MB_TYPE_CBP);
860 
861  s->mv_dir = MV_DIR_FORWARD;
862  if (s->picture_structure == PICT_FRAME) {
863  if (s->picture_structure == PICT_FRAME
864  && !s->frame_pred_frame_dct)
865  s->interlaced_dct = get_bits1(&s->gb);
866  s->mv_type = MV_TYPE_16X16;
867  } else {
868  s->mv_type = MV_TYPE_FIELD;
869  mb_type |= MB_TYPE_INTERLACED;
870  s->field_select[0][0] = s->picture_structure - 1;
871  }
872 
873  if (IS_QUANT(mb_type))
874  s->qscale = get_qscale(s);
875 
876  s->last_mv[0][0][0] = 0;
877  s->last_mv[0][0][1] = 0;
878  s->last_mv[0][1][0] = 0;
879  s->last_mv[0][1][1] = 0;
880  s->mv[0][0][0] = 0;
881  s->mv[0][0][1] = 0;
882  } else {
883  av_assert2(mb_type & MB_TYPE_L0L1);
884  // FIXME decide if MBs in field pictures are MB_TYPE_INTERLACED
885  /* get additional motion vector type */
887  motion_type = MT_FRAME;
888  } else {
889  motion_type = get_bits(&s->gb, 2);
890  if (s->picture_structure == PICT_FRAME && HAS_CBP(mb_type))
891  s->interlaced_dct = get_bits1(&s->gb);
892  }
893 
894  if (IS_QUANT(mb_type))
895  s->qscale = get_qscale(s);
896 
897  /* motion vectors */
898  s->mv_dir = (mb_type >> 13) & 3;
899  av_dlog(s->avctx, "motion_type=%d\n", motion_type);
900  switch (motion_type) {
901  case MT_FRAME: /* or MT_16X8 */
902  if (s->picture_structure == PICT_FRAME) {
903  mb_type |= MB_TYPE_16x16;
904  s->mv_type = MV_TYPE_16X16;
905  for (i = 0; i < 2; i++) {
906  if (USES_LIST(mb_type, i)) {
907  /* MT_FRAME */
908  s->mv[i][0][0] =
909  s->last_mv[i][0][0] =
910  s->last_mv[i][1][0] =
911  mpeg_decode_motion(s, s->mpeg_f_code[i][0],
912  s->last_mv[i][0][0]);
913  s->mv[i][0][1] =
914  s->last_mv[i][0][1] =
915  s->last_mv[i][1][1] =
916  mpeg_decode_motion(s, s->mpeg_f_code[i][1],
917  s->last_mv[i][0][1]);
918  /* full_pel: only for MPEG-1 */
919  if (s->full_pel[i]) {
920  s->mv[i][0][0] <<= 1;
921  s->mv[i][0][1] <<= 1;
922  }
923  }
924  }
925  } else {
926  mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
927  s->mv_type = MV_TYPE_16X8;
928  for (i = 0; i < 2; i++) {
929  if (USES_LIST(mb_type, i)) {
930  /* MT_16X8 */
931  for (j = 0; j < 2; j++) {
932  s->field_select[i][j] = get_bits1(&s->gb);
933  for (k = 0; k < 2; k++) {
934  val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
935  s->last_mv[i][j][k]);
936  s->last_mv[i][j][k] = val;
937  s->mv[i][j][k] = val;
938  }
939  }
940  }
941  }
942  }
943  break;
944  case MT_FIELD:
945  s->mv_type = MV_TYPE_FIELD;
946  if (s->picture_structure == PICT_FRAME) {
947  mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
948  for (i = 0; i < 2; i++) {
949  if (USES_LIST(mb_type, i)) {
950  for (j = 0; j < 2; j++) {
951  s->field_select[i][j] = get_bits1(&s->gb);
952  val = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
953  s->last_mv[i][j][0]);
954  s->last_mv[i][j][0] = val;
955  s->mv[i][j][0] = val;
956  av_dlog(s->avctx, "fmx=%d\n", val);
957  val = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
958  s->last_mv[i][j][1] >> 1);
959  s->last_mv[i][j][1] = val << 1;
960  s->mv[i][j][1] = val;
961  av_dlog(s->avctx, "fmy=%d\n", val);
962  }
963  }
964  }
965  } else {
967  mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
968  for (i = 0; i < 2; i++) {
969  if (USES_LIST(mb_type, i)) {
970  s->field_select[i][0] = get_bits1(&s->gb);
971  for (k = 0; k < 2; k++) {
972  val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
973  s->last_mv[i][0][k]);
974  s->last_mv[i][0][k] = val;
975  s->last_mv[i][1][k] = val;
976  s->mv[i][0][k] = val;
977  }
978  }
979  }
980  }
981  break;
982  case MT_DMV:
983  if (s->progressive_sequence){
984  av_log(s->avctx, AV_LOG_ERROR, "MT_DMV in progressive_sequence\n");
985  return -1;
986  }
987  s->mv_type = MV_TYPE_DMV;
988  for (i = 0; i < 2; i++) {
989  if (USES_LIST(mb_type, i)) {
990  int dmx, dmy, mx, my, m;
991  const int my_shift = s->picture_structure == PICT_FRAME;
992 
993  mx = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
994  s->last_mv[i][0][0]);
995  s->last_mv[i][0][0] = mx;
996  s->last_mv[i][1][0] = mx;
997  dmx = get_dmv(s);
998  my = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
999  s->last_mv[i][0][1] >> my_shift);
1000  dmy = get_dmv(s);
1001 
1002 
1003  s->last_mv[i][0][1] = my << my_shift;
1004  s->last_mv[i][1][1] = my << my_shift;
1005 
1006  s->mv[i][0][0] = mx;
1007  s->mv[i][0][1] = my;
1008  s->mv[i][1][0] = mx; // not used
1009  s->mv[i][1][1] = my; // not used
1010 
1011  if (s->picture_structure == PICT_FRAME) {
1012  mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
1013 
1014  // m = 1 + 2 * s->top_field_first;
1015  m = s->top_field_first ? 1 : 3;
1016 
1017  /* top -> top pred */
1018  s->mv[i][2][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
1019  s->mv[i][2][1] = ((my * m + (my > 0)) >> 1) + dmy - 1;
1020  m = 4 - m;
1021  s->mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
1022  s->mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1;
1023  } else {
1024  mb_type |= MB_TYPE_16x16;
1025 
1026  s->mv[i][2][0] = ((mx + (mx > 0)) >> 1) + dmx;
1027  s->mv[i][2][1] = ((my + (my > 0)) >> 1) + dmy;
1029  s->mv[i][2][1]--;
1030  else
1031  s->mv[i][2][1]++;
1032  }
1033  }
1034  }
1035  break;
1036  default:
1038  "00 motion_type at %d %d\n", s->mb_x, s->mb_y);
1039  return -1;
1040  }
1041  }
1042 
1043  s->mb_intra = 0;
1044  if (HAS_CBP(mb_type)) {
1045  s->bdsp.clear_blocks(s->block[0]);
1046 
1047  cbp = get_vlc2(&s->gb, ff_mb_pat_vlc.table, MB_PAT_VLC_BITS, 1);
1048  if (mb_block_count > 6) {
1049  cbp <<= mb_block_count - 6;
1050  cbp |= get_bits(&s->gb, mb_block_count - 6);
1051  s->bdsp.clear_blocks(s->block[6]);
1052  }
1053  if (cbp <= 0) {
1055  "invalid cbp %d at %d %d\n", cbp, s->mb_x, s->mb_y);
1056  return -1;
1057  }
1058 
1059  // if 1, we memcpy blocks in xvmcvideo
1060  if ((CONFIG_MPEG1_XVMC_HWACCEL || CONFIG_MPEG2_XVMC_HWACCEL) && s->pack_pblocks)
1061  ff_xvmc_pack_pblocks(s, cbp);
1062 
1063  if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
1064  if (s->flags2 & CODEC_FLAG2_FAST) {
1065  for (i = 0; i < 6; i++) {
1066  if (cbp & 32)
1068  else
1069  s->block_last_index[i] = -1;
1070  cbp += cbp;
1071  }
1072  } else {
1073  cbp <<= 12 - mb_block_count;
1074 
1075  for (i = 0; i < mb_block_count; i++) {
1076  if (cbp & (1 << 11)) {
1077  if (mpeg2_decode_block_non_intra(s, *s->pblocks[i], i) < 0)
1078  return -1;
1079  } else {
1080  s->block_last_index[i] = -1;
1081  }
1082  cbp += cbp;
1083  }
1084  }
1085  } else {
1086  if (s->flags2 & CODEC_FLAG2_FAST) {
1087  for (i = 0; i < 6; i++) {
1088  if (cbp & 32)
1089  mpeg1_fast_decode_block_inter(s, *s->pblocks[i], i);
1090  else
1091  s->block_last_index[i] = -1;
1092  cbp += cbp;
1093  }
1094  } else {
1095  for (i = 0; i < 6; i++) {
1096  if (cbp & 32) {
1097  if (mpeg1_decode_block_inter(s, *s->pblocks[i], i) < 0)
1098  return -1;
1099  } else {
1100  s->block_last_index[i] = -1;
1101  }
1102  cbp += cbp;
1103  }
1104  }
1105  }
1106  } else {
1107  for (i = 0; i < 12; i++)
1108  s->block_last_index[i] = -1;
1109  }
1110  }
1111 
1112  s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride] = mb_type;
1113 
1114  return 0;
1115 }
1116 
1118 {
1119  Mpeg1Context *s = avctx->priv_data;
1121 
1123  ff_mpv_decode_init(s2, avctx);
1124 
1125  s->mpeg_enc_ctx.avctx = avctx;
1126 
1127  /* we need some permutation to store matrices,
1128  * until the decoder sets the real permutation. */
1129  ff_mpv_idct_init(s2);
1132 
1133  s->mpeg_enc_ctx_allocated = 0;
1135  s->repeat_field = 0;
1136  s->mpeg_enc_ctx.codec_id = avctx->codec->id;
1137  avctx->color_range = AVCOL_RANGE_MPEG;
1138  if (avctx->codec->id == AV_CODEC_ID_MPEG1VIDEO)
1140  else
1142  return 0;
1143 }
1144 
1146  const AVCodecContext *avctx_from)
1147 {
1148  Mpeg1Context *ctx = avctx->priv_data, *ctx_from = avctx_from->priv_data;
1149  MpegEncContext *s = &ctx->mpeg_enc_ctx, *s1 = &ctx_from->mpeg_enc_ctx;
1150  int err;
1151 
1152  if (avctx == avctx_from ||
1153  !ctx_from->mpeg_enc_ctx_allocated ||
1154  !s1->context_initialized)
1155  return 0;
1156 
1157  err = ff_mpeg_update_thread_context(avctx, avctx_from);
1158  if (err)
1159  return err;
1160 
1161  if (!ctx->mpeg_enc_ctx_allocated)
1162  memcpy(s + 1, s1 + 1, sizeof(Mpeg1Context) - sizeof(MpegEncContext));
1163 
1164  if (!(s->pict_type == AV_PICTURE_TYPE_B || s->low_delay))
1165  s->picture_number++;
1166 
1167  return 0;
1168 }
1169 
1170 static void quant_matrix_rebuild(uint16_t *matrix, const uint8_t *old_perm,
1171  const uint8_t *new_perm)
1172 {
1173  uint16_t temp_matrix[64];
1174  int i;
1175 
1176  memcpy(temp_matrix, matrix, 64 * sizeof(uint16_t));
1177 
1178  for (i = 0; i < 64; i++)
1179  matrix[new_perm[i]] = temp_matrix[old_perm[i]];
1180 }
1181 
1183 #if CONFIG_MPEG1_XVMC_HWACCEL
1185 #endif
1186 #if CONFIG_MPEG1_VDPAU_HWACCEL
1189 #endif
1192 };
1193 
1195 #if CONFIG_MPEG2_XVMC_HWACCEL
1197 #endif
1198 #if CONFIG_MPEG2_VDPAU_HWACCEL
1201 #endif
1202 #if CONFIG_MPEG2_DXVA2_HWACCEL
1204 #endif
1205 #if CONFIG_MPEG2_VAAPI_HWACCEL
1207 #endif
1210 };
1211 
1212 static const enum AVPixelFormat mpeg12_pixfmt_list_422[] = {
1215 };
1216 
1217 static const enum AVPixelFormat mpeg12_pixfmt_list_444[] = {
1220 };
1221 
1222 static inline int uses_vdpau(AVCodecContext *avctx) {
1223  return avctx->pix_fmt == AV_PIX_FMT_VDPAU_MPEG1 || avctx->pix_fmt == AV_PIX_FMT_VDPAU_MPEG2;
1224 }
1225 
1227 {
1228  Mpeg1Context *s1 = avctx->priv_data;
1229  MpegEncContext *s = &s1->mpeg_enc_ctx;
1230  const enum AVPixelFormat *pix_fmts;
1231 
1232  if (s->chroma_format < 2)
1233  pix_fmts = avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO ?
1236  else if (s->chroma_format == 2)
1237  pix_fmts = mpeg12_pixfmt_list_422;
1238  else
1239  pix_fmts = mpeg12_pixfmt_list_444;
1240 
1241  return ff_thread_get_format(avctx, pix_fmts);
1242 }
1243 
1245 {
1246  // until then pix_fmt may be changed right after codec init
1247  if (avctx->hwaccel || uses_vdpau(avctx))
1248  if (avctx->idct_algo == FF_IDCT_AUTO)
1249  avctx->idct_algo = FF_IDCT_SIMPLE;
1250 
1251  if (avctx->hwaccel && avctx->pix_fmt == AV_PIX_FMT_XVMC) {
1252  Mpeg1Context *s1 = avctx->priv_data;
1253  MpegEncContext *s = &s1->mpeg_enc_ctx;
1254 
1255  s->pack_pblocks = 1;
1256 #if FF_API_XVMC
1257  avctx->xvmc_acceleration = 2;
1258 #endif /* FF_API_XVMC */
1259  }
1260 }
1261 
1262 /* Call this function when we know all parameters.
1263  * It may be called in different places for MPEG-1 and MPEG-2. */
1265 {
1266  Mpeg1Context *s1 = avctx->priv_data;
1267  MpegEncContext *s = &s1->mpeg_enc_ctx;
1268  uint8_t old_permutation[64];
1269  int ret;
1270 
1271  if ((s1->mpeg_enc_ctx_allocated == 0) ||
1272  avctx->coded_width != s->width ||
1273  avctx->coded_height != s->height ||
1274  s1->save_width != s->width ||
1275  s1->save_height != s->height ||
1276  s1->save_aspect_info != s->aspect_ratio_info ||
1277  (s1->save_progressive_seq != s->progressive_sequence && FFALIGN(s->height, 16) != FFALIGN(s->height, 32)) ||
1278  0) {
1279  if (s1->mpeg_enc_ctx_allocated) {
1280  ParseContext pc = s->parse_context;
1281  s->parse_context.buffer = 0;
1282  ff_mpv_common_end(s);
1283  s->parse_context = pc;
1284  s1->mpeg_enc_ctx_allocated = 0;
1285  }
1286 
1287  if ((s->width == 0) || (s->height == 0))
1288  return -2;
1289 
1290  ret = ff_set_dimensions(avctx, s->width, s->height);
1291  if (ret < 0)
1292  return ret;
1293 
1294  if (avctx->codec_id == AV_CODEC_ID_MPEG2VIDEO && s->bit_rate) {
1295  avctx->rc_max_rate = s->bit_rate;
1296  } else if (avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO && s->bit_rate &&
1297  (s->bit_rate != 0x3FFFF*400 || s->vbv_delay != 0xFFFF)) {
1298  avctx->bit_rate = s->bit_rate;
1299  }
1301  s1->save_width = s->width;
1302  s1->save_height = s->height;
1304 
1305  /* low_delay may be forced, in this case we will have B-frames
1306  * that behave like P-frames. */
1307  avctx->has_b_frames = !s->low_delay;
1308 
1309  if (avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
1310  // MPEG-1 fps
1313  // MPEG-1 aspect
1315  avctx->ticks_per_frame = 1;
1316  } else { // MPEG-2
1317  // MPEG-2 fps
1319  &s->avctx->time_base.num,
1322  1 << 30);
1323  avctx->ticks_per_frame = 2;
1324  // MPEG-2 aspect
1325  if (s->aspect_ratio_info > 1) {
1326  AVRational dar =
1328  (AVRational) { s1->pan_scan.width,
1329  s1->pan_scan.height }),
1330  (AVRational) { s->width, s->height });
1331 
1332  /* We ignore the spec here and guess a bit as reality does not
1333  * match the spec, see for example res_change_ffmpeg_aspect.ts
1334  * and sequence-display-aspect.mpg.
1335  * issue1613, 621, 562 */
1336  if ((s1->pan_scan.width == 0) || (s1->pan_scan.height == 0) ||
1337  (av_cmp_q(dar, (AVRational) { 4, 3 }) &&
1338  av_cmp_q(dar, (AVRational) { 16, 9 }))) {
1341  (AVRational) { s->width, s->height });
1342  } else {
1345  (AVRational) { s1->pan_scan.width, s1->pan_scan.height });
1346 // issue1613 4/3 16/9 -> 16/9
1347 // res_change_ffmpeg_aspect.ts 4/3 225/44 ->4/3
1348 // widescreen-issue562.mpg 4/3 16/9 -> 16/9
1349 // s->avctx->sample_aspect_ratio = av_mul_q(s->avctx->sample_aspect_ratio, (AVRational) {s->width, s->height});
1350  av_dlog(avctx, "A %d/%d\n",
1353  av_dlog(avctx, "B %d/%d\n", s->avctx->sample_aspect_ratio.num,
1355  }
1356  } else {
1357  s->avctx->sample_aspect_ratio =
1358  ff_mpeg2_aspect[s->aspect_ratio_info];
1359  }
1360  } // MPEG-2
1361 
1362  ff_set_sar(s->avctx, s->avctx->sample_aspect_ratio);
1363 
1364  avctx->pix_fmt = mpeg_get_pixelformat(avctx);
1365  setup_hwaccel_for_pixfmt(avctx);
1366 
1367  /* Quantization matrices may need reordering
1368  * if DCT permutation is changed. */
1369  memcpy(old_permutation, s->idsp.idct_permutation, 64 * sizeof(uint8_t));
1370 
1372  if (ff_mpv_common_init(s) < 0)
1373  return -2;
1374 
1375  quant_matrix_rebuild(s->intra_matrix, old_permutation, s->idsp.idct_permutation);
1376  quant_matrix_rebuild(s->inter_matrix, old_permutation, s->idsp.idct_permutation);
1377  quant_matrix_rebuild(s->chroma_intra_matrix, old_permutation, s->idsp.idct_permutation);
1378  quant_matrix_rebuild(s->chroma_inter_matrix, old_permutation, s->idsp.idct_permutation);
1379 
1380  s1->mpeg_enc_ctx_allocated = 1;
1381  }
1382  return 0;
1383 }
1384 
1386  int buf_size)
1387 {
1388  Mpeg1Context *s1 = avctx->priv_data;
1389  MpegEncContext *s = &s1->mpeg_enc_ctx;
1390  int ref, f_code, vbv_delay;
1391 
1392  init_get_bits(&s->gb, buf, buf_size * 8);
1393 
1394  ref = get_bits(&s->gb, 10); /* temporal ref */
1395  s->pict_type = get_bits(&s->gb, 3);
1396  if (s->pict_type == 0 || s->pict_type > 3)
1397  return -1;
1398 
1399  vbv_delay = get_bits(&s->gb, 16);
1400  s->vbv_delay = vbv_delay;
1401  if (s->pict_type == AV_PICTURE_TYPE_P ||
1402  s->pict_type == AV_PICTURE_TYPE_B) {
1403  s->full_pel[0] = get_bits1(&s->gb);
1404  f_code = get_bits(&s->gb, 3);
1405  if (f_code == 0 && (avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT)))
1406  return -1;
1407  f_code += !f_code;
1408  s->mpeg_f_code[0][0] = f_code;
1409  s->mpeg_f_code[0][1] = f_code;
1410  }
1411  if (s->pict_type == AV_PICTURE_TYPE_B) {
1412  s->full_pel[1] = get_bits1(&s->gb);
1413  f_code = get_bits(&s->gb, 3);
1414  if (f_code == 0 && (avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT)))
1415  return -1;
1416  f_code += !f_code;
1417  s->mpeg_f_code[1][0] = f_code;
1418  s->mpeg_f_code[1][1] = f_code;
1419  }
1422 
1423  if (avctx->debug & FF_DEBUG_PICT_INFO)
1424  av_log(avctx, AV_LOG_DEBUG,
1425  "vbv_delay %d, ref %d type:%d\n", vbv_delay, ref, s->pict_type);
1426 
1427  s->y_dc_scale = 8;
1428  s->c_dc_scale = 8;
1429  return 0;
1430 }
1431 
1433 {
1434  MpegEncContext *s = &s1->mpeg_enc_ctx;
1435  int horiz_size_ext, vert_size_ext;
1436  int bit_rate_ext;
1437 
1438  skip_bits(&s->gb, 1); /* profile and level esc*/
1439  s->avctx->profile = get_bits(&s->gb, 3);
1440  s->avctx->level = get_bits(&s->gb, 4);
1441  s->progressive_sequence = get_bits1(&s->gb); /* progressive_sequence */
1442  s->chroma_format = get_bits(&s->gb, 2); /* chroma_format 1=420, 2=422, 3=444 */
1443  horiz_size_ext = get_bits(&s->gb, 2);
1444  vert_size_ext = get_bits(&s->gb, 2);
1445  s->width |= (horiz_size_ext << 12);
1446  s->height |= (vert_size_ext << 12);
1447  bit_rate_ext = get_bits(&s->gb, 12); /* XXX: handle it */
1448  s->bit_rate += (bit_rate_ext << 18) * 400;
1449  skip_bits1(&s->gb); /* marker */
1450  s->avctx->rc_buffer_size += get_bits(&s->gb, 8) * 1024 * 16 << 10;
1451 
1452  s->low_delay = get_bits1(&s->gb);
1453  if (s->flags & CODEC_FLAG_LOW_DELAY)
1454  s->low_delay = 1;
1455 
1456  s1->frame_rate_ext.num = get_bits(&s->gb, 2) + 1;
1457  s1->frame_rate_ext.den = get_bits(&s->gb, 5) + 1;
1458 
1459  av_dlog(s->avctx, "sequence extension\n");
1461 
1462  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1464  "profile: %d, level: %d ps: %d cf:%d vbv buffer: %d, bitrate:%d\n",
1466  s->avctx->rc_buffer_size, s->bit_rate);
1467 }
1468 
1470 {
1471  MpegEncContext *s = &s1->mpeg_enc_ctx;
1472  int color_description, w, h;
1473 
1474  skip_bits(&s->gb, 3); /* video format */
1475  color_description = get_bits1(&s->gb);
1476  if (color_description) {
1477  s->avctx->color_primaries = get_bits(&s->gb, 8);
1478  s->avctx->color_trc = get_bits(&s->gb, 8);
1479  s->avctx->colorspace = get_bits(&s->gb, 8);
1480  }
1481  w = get_bits(&s->gb, 14);
1482  skip_bits(&s->gb, 1); // marker
1483  h = get_bits(&s->gb, 14);
1484  // remaining 3 bits are zero padding
1485 
1486  s1->pan_scan.width = 16 * w;
1487  s1->pan_scan.height = 16 * h;
1488 
1489  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1490  av_log(s->avctx, AV_LOG_DEBUG, "sde w:%d, h:%d\n", w, h);
1491 }
1492 
1494 {
1495  MpegEncContext *s = &s1->mpeg_enc_ctx;
1496  int i, nofco;
1497 
1498  nofco = 1;
1499  if (s->progressive_sequence) {
1500  if (s->repeat_first_field) {
1501  nofco++;
1502  if (s->top_field_first)
1503  nofco++;
1504  }
1505  } else {
1506  if (s->picture_structure == PICT_FRAME) {
1507  nofco++;
1508  if (s->repeat_first_field)
1509  nofco++;
1510  }
1511  }
1512  for (i = 0; i < nofco; i++) {
1513  s1->pan_scan.position[i][0] = get_sbits(&s->gb, 16);
1514  skip_bits(&s->gb, 1); // marker
1515  s1->pan_scan.position[i][1] = get_sbits(&s->gb, 16);
1516  skip_bits(&s->gb, 1); // marker
1517  }
1518 
1519  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1521  "pde (%"PRId16",%"PRId16") (%"PRId16",%"PRId16") (%"PRId16",%"PRId16")\n",
1522  s1->pan_scan.position[0][0], s1->pan_scan.position[0][1],
1523  s1->pan_scan.position[1][0], s1->pan_scan.position[1][1],
1524  s1->pan_scan.position[2][0], s1->pan_scan.position[2][1]);
1525 }
1526 
1527 static int load_matrix(MpegEncContext *s, uint16_t matrix0[64],
1528  uint16_t matrix1[64], int intra)
1529 {
1530  int i;
1531 
1532  for (i = 0; i < 64; i++) {
1533  int j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
1534  int v = get_bits(&s->gb, 8);
1535  if (v == 0) {
1536  av_log(s->avctx, AV_LOG_ERROR, "matrix damaged\n");
1537  return -1;
1538  }
1539  if (intra && i == 0 && v != 8) {
1540  av_log(s->avctx, AV_LOG_DEBUG, "intra matrix specifies invalid DC quantizer %d, ignoring\n", v);
1541  v = 8; // needed by pink.mpg / issue1046
1542  }
1543  matrix0[j] = v;
1544  if (matrix1)
1545  matrix1[j] = v;
1546  }
1547  return 0;
1548 }
1549 
1551 {
1552  av_dlog(s->avctx, "matrix extension\n");
1553 
1554  if (get_bits1(&s->gb))
1556  if (get_bits1(&s->gb))
1558  if (get_bits1(&s->gb))
1559  load_matrix(s, s->chroma_intra_matrix, NULL, 1);
1560  if (get_bits1(&s->gb))
1561  load_matrix(s, s->chroma_inter_matrix, NULL, 0);
1562 }
1563 
1565 {
1566  MpegEncContext *s = &s1->mpeg_enc_ctx;
1567 
1568  s->full_pel[0] = s->full_pel[1] = 0;
1569  s->mpeg_f_code[0][0] = get_bits(&s->gb, 4);
1570  s->mpeg_f_code[0][1] = get_bits(&s->gb, 4);
1571  s->mpeg_f_code[1][0] = get_bits(&s->gb, 4);
1572  s->mpeg_f_code[1][1] = get_bits(&s->gb, 4);
1573  if (!s->pict_type && s1->mpeg_enc_ctx_allocated) {
1575  "Missing picture start code, guessing missing values\n");
1576  if (s->mpeg_f_code[1][0] == 15 && s->mpeg_f_code[1][1] == 15) {
1577  if (s->mpeg_f_code[0][0] == 15 && s->mpeg_f_code[0][1] == 15)
1579  else
1581  } else
1585  }
1586  s->mpeg_f_code[0][0] += !s->mpeg_f_code[0][0];
1587  s->mpeg_f_code[0][1] += !s->mpeg_f_code[0][1];
1588  s->mpeg_f_code[1][0] += !s->mpeg_f_code[1][0];
1589  s->mpeg_f_code[1][1] += !s->mpeg_f_code[1][1];
1590 
1591  s->intra_dc_precision = get_bits(&s->gb, 2);
1592  s->picture_structure = get_bits(&s->gb, 2);
1593  s->top_field_first = get_bits1(&s->gb);
1594  s->frame_pred_frame_dct = get_bits1(&s->gb);
1596  s->q_scale_type = get_bits1(&s->gb);
1597  s->intra_vlc_format = get_bits1(&s->gb);
1598  s->alternate_scan = get_bits1(&s->gb);
1599  s->repeat_first_field = get_bits1(&s->gb);
1600  s->chroma_420_type = get_bits1(&s->gb);
1601  s->progressive_frame = get_bits1(&s->gb);
1602 
1603  if (s->alternate_scan) {
1606  } else {
1609  }
1610 
1611  /* composite display not parsed */
1612  av_dlog(s->avctx, "intra_dc_precision=%d\n", s->intra_dc_precision);
1613  av_dlog(s->avctx, "picture_structure=%d\n", s->picture_structure);
1614  av_dlog(s->avctx, "top field first=%d\n", s->top_field_first);
1615  av_dlog(s->avctx, "repeat first field=%d\n", s->repeat_first_field);
1616  av_dlog(s->avctx, "conceal=%d\n", s->concealment_motion_vectors);
1617  av_dlog(s->avctx, "intra_vlc_format=%d\n", s->intra_vlc_format);
1618  av_dlog(s->avctx, "alternate_scan=%d\n", s->alternate_scan);
1619  av_dlog(s->avctx, "frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
1620  av_dlog(s->avctx, "progressive_frame=%d\n", s->progressive_frame);
1621 }
1622 
1623 static int mpeg_field_start(MpegEncContext *s, const uint8_t *buf, int buf_size)
1624 {
1625  AVCodecContext *avctx = s->avctx;
1626  Mpeg1Context *s1 = (Mpeg1Context *) s;
1627 
1628  /* start frame decoding */
1629  if (s->first_field || s->picture_structure == PICT_FRAME) {
1630  AVFrameSideData *pan_scan;
1631 
1632  if (ff_mpv_frame_start(s, avctx) < 0)
1633  return -1;
1634 
1636 
1637  /* first check if we must repeat the frame */
1639  if (s->repeat_first_field) {
1640  if (s->progressive_sequence) {
1641  if (s->top_field_first)
1643  else
1645  } else if (s->progressive_frame) {
1647  }
1648  }
1649 
1652  sizeof(s1->pan_scan));
1653  if (!pan_scan)
1654  return AVERROR(ENOMEM);
1655  memcpy(pan_scan->data, &s1->pan_scan, sizeof(s1->pan_scan));
1656 
1657  if (s1->a53_caption) {
1660  s1->a53_caption_size);
1661  if (sd)
1662  memcpy(sd->data, s1->a53_caption, s1->a53_caption_size);
1663  av_freep(&s1->a53_caption);
1664  }
1665 
1666  if (s1->has_stereo3d) {
1668  if (!stereo)
1669  return AVERROR(ENOMEM);
1670 
1671  *stereo = s1->stereo3d;
1672  s1->has_stereo3d = 0;
1673  }
1674 
1675  if (s1->has_afd) {
1676  AVFrameSideData *sd =
1678  AV_FRAME_DATA_AFD, 1);
1679  if (!sd)
1680  return AVERROR(ENOMEM);
1681 
1682  *sd->data = s1->afd;
1683  s1->has_afd = 0;
1684  }
1685 
1686  if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_FRAME))
1687  ff_thread_finish_setup(avctx);
1688  } else { // second field
1689  int i;
1690 
1691  if (!s->current_picture_ptr) {
1692  av_log(s->avctx, AV_LOG_ERROR, "first field missing\n");
1693  return -1;
1694  }
1695 
1696  if (s->avctx->hwaccel &&
1698  if (s->avctx->hwaccel->end_frame(s->avctx) < 0)
1699  av_log(avctx, AV_LOG_ERROR,
1700  "hardware accelerator failed to decode first field\n");
1701  }
1702 
1703  for (i = 0; i < 4; i++) {
1704  s->current_picture.f->data[i] = s->current_picture_ptr->f->data[i];
1706  s->current_picture.f->data[i] +=
1707  s->current_picture_ptr->f->linesize[i];
1708  }
1709  }
1710 
1711  if (avctx->hwaccel) {
1712  if (avctx->hwaccel->start_frame(avctx, buf, buf_size) < 0)
1713  return -1;
1714  }
1715 
1716  return 0;
1717 }
1718 
1719 #define DECODE_SLICE_ERROR -1
1720 #define DECODE_SLICE_OK 0
1721 
1722 /**
1723  * Decode a slice.
1724  * MpegEncContext.mb_y must be set to the MB row from the startcode.
1725  * @return DECODE_SLICE_ERROR if the slice is damaged,
1726  * DECODE_SLICE_OK if this slice is OK
1727  */
1728 static int mpeg_decode_slice(MpegEncContext *s, int mb_y,
1729  const uint8_t **buf, int buf_size)
1730 {
1731  AVCodecContext *avctx = s->avctx;
1732  const int lowres = s->avctx->lowres;
1733  const int field_pic = s->picture_structure != PICT_FRAME;
1734 
1735  s->resync_mb_x =
1736  s->resync_mb_y = -1;
1737 
1738  av_assert0(mb_y < s->mb_height);
1739 
1740  init_get_bits(&s->gb, *buf, buf_size * 8);
1741  if (s->codec_id != AV_CODEC_ID_MPEG1VIDEO && s->mb_height > 2800/16)
1742  skip_bits(&s->gb, 3);
1743 
1745  s->interlaced_dct = 0;
1746 
1747  s->qscale = get_qscale(s);
1748 
1749  if (s->qscale == 0) {
1750  av_log(s->avctx, AV_LOG_ERROR, "qscale == 0\n");
1751  return -1;
1752  }
1753 
1754  /* extra slice info */
1755  if (skip_1stop_8data_bits(&s->gb) < 0)
1756  return AVERROR_INVALIDDATA;
1757 
1758  s->mb_x = 0;
1759 
1760  if (mb_y == 0 && s->codec_tag == AV_RL32("SLIF")) {
1761  skip_bits1(&s->gb);
1762  } else {
1763  while (get_bits_left(&s->gb) > 0) {
1764  int code = get_vlc2(&s->gb, ff_mbincr_vlc.table,
1765  MBINCR_VLC_BITS, 2);
1766  if (code < 0) {
1767  av_log(s->avctx, AV_LOG_ERROR, "first mb_incr damaged\n");
1768  return -1;
1769  }
1770  if (code >= 33) {
1771  if (code == 33)
1772  s->mb_x += 33;
1773  /* otherwise, stuffing, nothing to do */
1774  } else {
1775  s->mb_x += code;
1776  break;
1777  }
1778  }
1779  }
1780 
1781  if (s->mb_x >= (unsigned) s->mb_width) {
1782  av_log(s->avctx, AV_LOG_ERROR, "initial skip overflow\n");
1783  return -1;
1784  }
1785 
1786  if (avctx->hwaccel && avctx->hwaccel->decode_slice) {
1787  const uint8_t *buf_end, *buf_start = *buf - 4; /* include start_code */
1788  int start_code = -1;
1789  buf_end = avpriv_find_start_code(buf_start + 2, *buf + buf_size, &start_code);
1790  if (buf_end < *buf + buf_size)
1791  buf_end -= 4;
1792  s->mb_y = mb_y;
1793  if (avctx->hwaccel->decode_slice(avctx, buf_start, buf_end - buf_start) < 0)
1794  return DECODE_SLICE_ERROR;
1795  *buf = buf_end;
1796  return DECODE_SLICE_OK;
1797  }
1798 
1799  s->resync_mb_x = s->mb_x;
1800  s->resync_mb_y = s->mb_y = mb_y;
1801  s->mb_skip_run = 0;
1803 
1804  if (s->mb_y == 0 && s->mb_x == 0 && (s->first_field || s->picture_structure == PICT_FRAME)) {
1805  if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
1807  "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",
1808  s->qscale,
1809  s->mpeg_f_code[0][0], s->mpeg_f_code[0][1],
1810  s->mpeg_f_code[1][0], s->mpeg_f_code[1][1],
1811  s->pict_type == AV_PICTURE_TYPE_I ? "I" :
1812  (s->pict_type == AV_PICTURE_TYPE_P ? "P" :
1813  (s->pict_type == AV_PICTURE_TYPE_B ? "B" : "S")),
1814  s->progressive_sequence ? "ps" : "",
1815  s->progressive_frame ? "pf" : "",
1816  s->alternate_scan ? "alt" : "",
1817  s->top_field_first ? "top" : "",
1821  s->repeat_first_field, s->chroma_420_type ? "420" : "");
1822  }
1823  }
1824 
1825  for (;;) {
1826  // If 1, we memcpy blocks in xvmcvideo.
1827  if ((CONFIG_MPEG1_XVMC_HWACCEL || CONFIG_MPEG2_XVMC_HWACCEL) && s->pack_pblocks)
1828  ff_xvmc_init_block(s); // set s->block
1829 
1830  if (mpeg_decode_mb(s, s->block) < 0)
1831  return -1;
1832 
1833  // Note motion_val is normally NULL unless we want to extract the MVs.
1834  if (s->current_picture.motion_val[0] && !s->encoding) {
1835  const int wrap = s->b8_stride;
1836  int xy = s->mb_x * 2 + s->mb_y * 2 * wrap;
1837  int b8_xy = 4 * (s->mb_x + s->mb_y * s->mb_stride);
1838  int motion_x, motion_y, dir, i;
1839 
1840  for (i = 0; i < 2; i++) {
1841  for (dir = 0; dir < 2; dir++) {
1842  if (s->mb_intra ||
1843  (dir == 1 && s->pict_type != AV_PICTURE_TYPE_B)) {
1844  motion_x = motion_y = 0;
1845  } else if (s->mv_type == MV_TYPE_16X16 ||
1846  (s->mv_type == MV_TYPE_FIELD && field_pic)) {
1847  motion_x = s->mv[dir][0][0];
1848  motion_y = s->mv[dir][0][1];
1849  } else { /* if ((s->mv_type == MV_TYPE_FIELD) || (s->mv_type == MV_TYPE_16X8)) */
1850  motion_x = s->mv[dir][i][0];
1851  motion_y = s->mv[dir][i][1];
1852  }
1853 
1854  s->current_picture.motion_val[dir][xy][0] = motion_x;
1855  s->current_picture.motion_val[dir][xy][1] = motion_y;
1856  s->current_picture.motion_val[dir][xy + 1][0] = motion_x;
1857  s->current_picture.motion_val[dir][xy + 1][1] = motion_y;
1858  s->current_picture.ref_index [dir][b8_xy] =
1859  s->current_picture.ref_index [dir][b8_xy + 1] = s->field_select[dir][i];
1860  av_assert2(s->field_select[dir][i] == 0 ||
1861  s->field_select[dir][i] == 1);
1862  }
1863  xy += wrap;
1864  b8_xy += 2;
1865  }
1866  }
1867 
1868  s->dest[0] += 16 >> lowres;
1869  s->dest[1] +=(16 >> lowres) >> s->chroma_x_shift;
1870  s->dest[2] +=(16 >> lowres) >> s->chroma_x_shift;
1871 
1872  ff_mpv_decode_mb(s, s->block);
1873 
1874  if (++s->mb_x >= s->mb_width) {
1875  const int mb_size = 16 >> s->avctx->lowres;
1876 
1877  ff_mpeg_draw_horiz_band(s, mb_size * (s->mb_y >> field_pic), mb_size);
1879 
1880  s->mb_x = 0;
1881  s->mb_y += 1 << field_pic;
1882 
1883  if (s->mb_y >= s->mb_height) {
1884  int left = get_bits_left(&s->gb);
1885  int is_d10 = s->chroma_format == 2 &&
1886  s->pict_type == AV_PICTURE_TYPE_I &&
1887  avctx->profile == 0 && avctx->level == 5 &&
1888  s->intra_dc_precision == 2 &&
1889  s->q_scale_type == 1 && s->alternate_scan == 0 &&
1890  s->progressive_frame == 0
1891  /* vbv_delay == 0xBBB || 0xE10 */;
1892 
1893  if (left >= 32 && !is_d10) {
1894  GetBitContext gb = s->gb;
1895  align_get_bits(&gb);
1896  if (show_bits(&gb, 24) == 0x060E2B) {
1897  av_log(avctx, AV_LOG_DEBUG, "Invalid MXF data found in video stream\n");
1898  is_d10 = 1;
1899  }
1900  }
1901 
1902  if (left < 0 ||
1903  (left && show_bits(&s->gb, FFMIN(left, 23)) && !is_d10) ||
1904  ((avctx->err_recognition & (AV_EF_BITSTREAM | AV_EF_AGGRESSIVE)) && left > 8)) {
1905  av_log(avctx, AV_LOG_ERROR, "end mismatch left=%d %0X\n",
1906  left, show_bits(&s->gb, FFMIN(left, 23)));
1907  return -1;
1908  } else
1909  goto eos;
1910  }
1911  // There are some files out there which are missing the last slice
1912  // in cases where the slice is completely outside the visible
1913  // area, we detect this here instead of running into the end expecting
1914  // more data
1915  if (s->mb_y >= ((s->height + 15) >> 4) &&
1916  s->progressive_frame &&
1917  !s->progressive_sequence &&
1918  get_bits_left(&s->gb) <= 8 &&
1919  get_bits_left(&s->gb) >= 0 &&
1920  s->mb_skip_run == -1 &&
1921  show_bits(&s->gb, 8) == 0)
1922  goto eos;
1923 
1925  }
1926 
1927  /* skip mb handling */
1928  if (s->mb_skip_run == -1) {
1929  /* read increment again */
1930  s->mb_skip_run = 0;
1931  for (;;) {
1932  int code = get_vlc2(&s->gb, ff_mbincr_vlc.table,
1933  MBINCR_VLC_BITS, 2);
1934  if (code < 0) {
1935  av_log(s->avctx, AV_LOG_ERROR, "mb incr damaged\n");
1936  return -1;
1937  }
1938  if (code >= 33) {
1939  if (code == 33) {
1940  s->mb_skip_run += 33;
1941  } else if (code == 35) {
1942  if (s->mb_skip_run != 0 || show_bits(&s->gb, 15) != 0) {
1943  av_log(s->avctx, AV_LOG_ERROR, "slice mismatch\n");
1944  return -1;
1945  }
1946  goto eos; /* end of slice */
1947  }
1948  /* otherwise, stuffing, nothing to do */
1949  } else {
1950  s->mb_skip_run += code;
1951  break;
1952  }
1953  }
1954  if (s->mb_skip_run) {
1955  int i;
1956  if (s->pict_type == AV_PICTURE_TYPE_I) {
1958  "skipped MB in I frame at %d %d\n", s->mb_x, s->mb_y);
1959  return -1;
1960  }
1961 
1962  /* skip mb */
1963  s->mb_intra = 0;
1964  for (i = 0; i < 12; i++)
1965  s->block_last_index[i] = -1;
1967  s->mv_type = MV_TYPE_16X16;
1968  else
1969  s->mv_type = MV_TYPE_FIELD;
1970  if (s->pict_type == AV_PICTURE_TYPE_P) {
1971  /* if P type, zero motion vector is implied */
1972  s->mv_dir = MV_DIR_FORWARD;
1973  s->mv[0][0][0] = s->mv[0][0][1] = 0;
1974  s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
1975  s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
1976  s->field_select[0][0] = (s->picture_structure - 1) & 1;
1977  } else {
1978  /* if B type, reuse previous vectors and directions */
1979  s->mv[0][0][0] = s->last_mv[0][0][0];
1980  s->mv[0][0][1] = s->last_mv[0][0][1];
1981  s->mv[1][0][0] = s->last_mv[1][0][0];
1982  s->mv[1][0][1] = s->last_mv[1][0][1];
1983  }
1984  }
1985  }
1986  }
1987 eos: // end of slice
1988  if (get_bits_left(&s->gb) < 0) {
1989  av_log(s, AV_LOG_ERROR, "overread %d\n", -get_bits_left(&s->gb));
1990  return AVERROR_INVALIDDATA;
1991  }
1992  *buf += (get_bits_count(&s->gb) - 1) / 8;
1993  av_dlog(s, "y %d %d %d %d\n", s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y);
1994  return 0;
1995 }
1996 
1998 {
1999  MpegEncContext *s = *(void **) arg;
2000  const uint8_t *buf = s->gb.buffer;
2001  int mb_y = s->start_mb_y;
2002  const int field_pic = s->picture_structure != PICT_FRAME;
2003 
2004  s->er.error_count = (3 * (s->end_mb_y - s->start_mb_y) * s->mb_width) >> field_pic;
2005 
2006  for (;;) {
2007  uint32_t start_code;
2008  int ret;
2009 
2010  ret = mpeg_decode_slice(s, mb_y, &buf, s->gb.buffer_end - buf);
2011  emms_c();
2012  av_dlog(c, "ret:%d resync:%d/%d mb:%d/%d ts:%d/%d ec:%d\n",
2013  ret, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y,
2014  s->start_mb_y, s->end_mb_y, s->er.error_count);
2015  if (ret < 0) {
2016  if (c->err_recognition & AV_EF_EXPLODE)
2017  return ret;
2018  if (s->resync_mb_x >= 0 && s->resync_mb_y >= 0)
2020  s->mb_x, s->mb_y,
2022  } else {
2024  s->mb_x - 1, s->mb_y,
2026  }
2027 
2028  if (s->mb_y == s->end_mb_y)
2029  return 0;
2030 
2031  start_code = -1;
2032  buf = avpriv_find_start_code(buf, s->gb.buffer_end, &start_code);
2033  mb_y = start_code - SLICE_MIN_START_CODE;
2034  if (s->codec_id != AV_CODEC_ID_MPEG1VIDEO && s->mb_height > 2800/16)
2035  mb_y += (*buf&0xE0)<<2;
2036  mb_y <<= field_pic;
2038  mb_y++;
2039  if (mb_y < 0 || mb_y >= s->end_mb_y)
2040  return -1;
2041  }
2042 }
2043 
2044 /**
2045  * Handle slice ends.
2046  * @return 1 if it seems to be the last slice
2047  */
2048 static int slice_end(AVCodecContext *avctx, AVFrame *pict)
2049 {
2050  Mpeg1Context *s1 = avctx->priv_data;
2051  MpegEncContext *s = &s1->mpeg_enc_ctx;
2052 
2054  return 0;
2055 
2056  if (s->avctx->hwaccel) {
2057  if (s->avctx->hwaccel->end_frame(s->avctx) < 0)
2058  av_log(avctx, AV_LOG_ERROR,
2059  "hardware accelerator failed to decode picture\n");
2060  }
2061 
2062  /* end of slice reached */
2063  if (/* s->mb_y << field_pic == s->mb_height && */ !s->first_field && !s1->first_slice) {
2064  /* end of image */
2065 
2066  ff_er_frame_end(&s->er);
2067 
2068  ff_mpv_frame_end(s);
2069 
2070  if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
2071  int ret = av_frame_ref(pict, s->current_picture_ptr->f);
2072  if (ret < 0)
2073  return ret;
2076  } else {
2077  if (avctx->active_thread_type & FF_THREAD_FRAME)
2078  s->picture_number++;
2079  /* latency of 1 frame for I- and P-frames */
2080  /* XXX: use another variable than picture_number */
2081  if (s->last_picture_ptr) {
2082  int ret = av_frame_ref(pict, s->last_picture_ptr->f);
2083  if (ret < 0)
2084  return ret;
2087  }
2088  }
2089 
2090  return 1;
2091  } else {
2092  return 0;
2093  }
2094 }
2095 
2097  const uint8_t *buf, int buf_size)
2098 {
2099  Mpeg1Context *s1 = avctx->priv_data;
2100  MpegEncContext *s = &s1->mpeg_enc_ctx;
2101  int width, height;
2102  int i, v, j;
2103 
2104  init_get_bits(&s->gb, buf, buf_size * 8);
2105 
2106  width = get_bits(&s->gb, 12);
2107  height = get_bits(&s->gb, 12);
2108  if (width == 0 || height == 0) {
2109  av_log(avctx, AV_LOG_WARNING,
2110  "Invalid horizontal or vertical size value.\n");
2112  return AVERROR_INVALIDDATA;
2113  }
2114  s->aspect_ratio_info = get_bits(&s->gb, 4);
2115  if (s->aspect_ratio_info == 0) {
2116  av_log(avctx, AV_LOG_ERROR, "aspect ratio has forbidden 0 value\n");
2118  return -1;
2119  }
2120  s->frame_rate_index = get_bits(&s->gb, 4);
2121  if (s->frame_rate_index == 0 || s->frame_rate_index > 13)
2122  return -1;
2123  s->bit_rate = get_bits(&s->gb, 18) * 400;
2124  if (get_bits1(&s->gb) == 0) /* marker */
2125  return -1;
2126  s->width = width;
2127  s->height = height;
2128 
2129  s->avctx->rc_buffer_size = get_bits(&s->gb, 10) * 1024 * 16;
2130  skip_bits(&s->gb, 1);
2131 
2132  /* get matrix */
2133  if (get_bits1(&s->gb)) {
2135  } else {
2136  for (i = 0; i < 64; i++) {
2137  j = s->idsp.idct_permutation[i];
2139  s->intra_matrix[j] = v;
2140  s->chroma_intra_matrix[j] = v;
2141  }
2142  }
2143  if (get_bits1(&s->gb)) {
2145  } else {
2146  for (i = 0; i < 64; i++) {
2147  int j = s->idsp.idct_permutation[i];
2149  s->inter_matrix[j] = v;
2150  s->chroma_inter_matrix[j] = v;
2151  }
2152  }
2153 
2154  if (show_bits(&s->gb, 23) != 0) {
2155  av_log(s->avctx, AV_LOG_ERROR, "sequence header damaged\n");
2156  return -1;
2157  }
2158 
2159  /* We set MPEG-2 parameters so that it emulates MPEG-1. */
2160  s->progressive_sequence = 1;
2161  s->progressive_frame = 1;
2163  s->first_field = 0;
2164  s->frame_pred_frame_dct = 1;
2165  s->chroma_format = 1;
2166  s->codec_id =
2168  s->out_format = FMT_MPEG1;
2169  s->swap_uv = 0; // AFAIK VCR2 does not have SEQ_HEADER
2170  if (s->flags & CODEC_FLAG_LOW_DELAY)
2171  s->low_delay = 1;
2172 
2173  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
2174  av_log(s->avctx, AV_LOG_DEBUG, "vbv buffer: %d, bitrate:%d\n",
2175  s->avctx->rc_buffer_size, s->bit_rate);
2176 
2177  return 0;
2178 }
2179 
2181 {
2182  Mpeg1Context *s1 = avctx->priv_data;
2183  MpegEncContext *s = &s1->mpeg_enc_ctx;
2184  int i, v;
2185 
2186  /* start new MPEG-1 context decoding */
2187  s->out_format = FMT_MPEG1;
2188  if (s1->mpeg_enc_ctx_allocated) {
2189  ff_mpv_common_end(s);
2190  s1->mpeg_enc_ctx_allocated = 0;
2191  }
2192  s->width = avctx->coded_width;
2193  s->height = avctx->coded_height;
2194  avctx->has_b_frames = 0; // true?
2195  s->low_delay = 1;
2196 
2197  avctx->pix_fmt = mpeg_get_pixelformat(avctx);
2198  setup_hwaccel_for_pixfmt(avctx);
2199 
2200  ff_mpv_idct_init(s);
2201  if (ff_mpv_common_init(s) < 0)
2202  return -1;
2203  s1->mpeg_enc_ctx_allocated = 1;
2204 
2205  for (i = 0; i < 64; i++) {
2206  int j = s->idsp.idct_permutation[i];
2208  s->intra_matrix[j] = v;
2209  s->chroma_intra_matrix[j] = v;
2210 
2212  s->inter_matrix[j] = v;
2213  s->chroma_inter_matrix[j] = v;
2214  }
2215 
2216  s->progressive_sequence = 1;
2217  s->progressive_frame = 1;
2219  s->first_field = 0;
2220  s->frame_pred_frame_dct = 1;
2221  s->chroma_format = 1;
2222  if (s->codec_tag == AV_RL32("BW10")) {
2224  } else {
2225  s->swap_uv = 1; // in case of xvmc we need to swap uv for each MB
2227  }
2228  s1->save_width = s->width;
2229  s1->save_height = s->height;
2231  return 0;
2232 }
2233 
2235  const uint8_t *p, int buf_size)
2236 {
2237  Mpeg1Context *s1 = avctx->priv_data;
2238 
2239  if (buf_size >= 6 &&
2240  p[0] == 'G' && p[1] == 'A' && p[2] == '9' && p[3] == '4' &&
2241  p[4] == 3 && (p[5] & 0x40)) {
2242  /* extract A53 Part 4 CC data */
2243  int cc_count = p[5] & 0x1f;
2244  if (cc_count > 0 && buf_size >= 7 + cc_count * 3) {
2245  av_freep(&s1->a53_caption);
2246  s1->a53_caption_size = cc_count * 3;
2248  if (s1->a53_caption)
2249  memcpy(s1->a53_caption, p + 7, s1->a53_caption_size);
2250  }
2251  return 1;
2252  } else if (buf_size >= 11 &&
2253  p[0] == 'C' && p[1] == 'C' && p[2] == 0x01 && p[3] == 0xf8) {
2254  /* extract DVD CC data */
2255  int cc_count = 0;
2256  int i;
2257  // There is a caption count field in the data, but it is often
2258  // incorect. So count the number of captions present.
2259  for (i = 5; i + 6 <= buf_size && ((p[i] & 0xfe) == 0xfe); i += 6)
2260  cc_count++;
2261  // Transform the DVD format into A53 Part 4 format
2262  if (cc_count > 0) {
2263  av_freep(&s1->a53_caption);
2264  s1->a53_caption_size = cc_count * 6;
2266  if (s1->a53_caption) {
2267  uint8_t field1 = !!(p[4] & 0x80);
2268  uint8_t *cap = s1->a53_caption;
2269  p += 5;
2270  for (i = 0; i < cc_count; i++) {
2271  cap[0] = (p[0] == 0xff && field1) ? 0xfc : 0xfd;
2272  cap[1] = p[1];
2273  cap[2] = p[2];
2274  cap[3] = (p[3] == 0xff && !field1) ? 0xfc : 0xfd;
2275  cap[4] = p[4];
2276  cap[5] = p[5];
2277  cap += 6;
2278  p += 6;
2279  }
2280  }
2281  }
2282  return 1;
2283  }
2284  return 0;
2285 }
2286 
2288  const uint8_t *p, int buf_size)
2289 {
2290  Mpeg1Context *s = avctx->priv_data;
2291  const uint8_t *buf_end = p + buf_size;
2292  Mpeg1Context *s1 = avctx->priv_data;
2293 
2294  if (buf_size > 29){
2295  int i;
2296  for(i=0; i<20; i++)
2297  if (!memcmp(p+i, "\0TMPGEXS\0", 9)){
2298  s->tmpgexs= 1;
2299  }
2300 
2301 /* for(i=0; !(!p[i-2] && !p[i-1] && p[i]==1) && i<buf_size; i++){
2302  av_log(avctx, AV_LOG_ERROR, "%c", p[i]);
2303  }
2304  av_log(avctx, AV_LOG_ERROR, "\n");*/
2305  }
2306 
2307  /* we parse the DTG active format information */
2308  if (buf_end - p >= 5 &&
2309  p[0] == 'D' && p[1] == 'T' && p[2] == 'G' && p[3] == '1') {
2310  int flags = p[4];
2311  p += 5;
2312  if (flags & 0x80) {
2313  /* skip event id */
2314  p += 2;
2315  }
2316  if (flags & 0x40) {
2317  if (buf_end - p < 1)
2318  return;
2319 #if FF_API_AFD
2321  avctx->dtg_active_format = p[0] & 0x0f;
2323 #endif /* FF_API_AFD */
2324  s1->has_afd = 1;
2325  s1->afd = p[0] & 0x0f;
2326  }
2327  } else if (buf_end - p >= 6 &&
2328  p[0] == 'J' && p[1] == 'P' && p[2] == '3' && p[3] == 'D' &&
2329  p[4] == 0x03) { // S3D_video_format_length
2330  // the 0x7F mask ignores the reserved_bit value
2331  const uint8_t S3D_video_format_type = p[5] & 0x7F;
2332 
2333  if (S3D_video_format_type == 0x03 ||
2334  S3D_video_format_type == 0x04 ||
2335  S3D_video_format_type == 0x08 ||
2336  S3D_video_format_type == 0x23) {
2337 
2338  s1->has_stereo3d = 1;
2339 
2340  switch (S3D_video_format_type) {
2341  case 0x03:
2343  break;
2344  case 0x04:
2346  break;
2347  case 0x08:
2349  break;
2350  case 0x23:
2352  break;
2353  }
2354  }
2355  } else if (mpeg_decode_a53_cc(avctx, p, buf_size)) {
2356  return;
2357  }
2358 }
2359 
2360 static void mpeg_decode_gop(AVCodecContext *avctx,
2361  const uint8_t *buf, int buf_size)
2362 {
2363  Mpeg1Context *s1 = avctx->priv_data;
2364  MpegEncContext *s = &s1->mpeg_enc_ctx;
2365  int broken_link;
2366  int64_t tc;
2367 
2368  init_get_bits(&s->gb, buf, buf_size * 8);
2369 
2370  tc = avctx->timecode_frame_start = get_bits(&s->gb, 25);
2371 
2372  s->closed_gop = get_bits1(&s->gb);
2373  /* broken_link indicate that after editing the
2374  * reference frames of the first B-Frames after GOP I-Frame
2375  * are missing (open gop) */
2376  broken_link = get_bits1(&s->gb);
2377 
2378  if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
2379  char tcbuf[AV_TIMECODE_STR_SIZE];
2382  "GOP (%s) closed_gop=%d broken_link=%d\n",
2383  tcbuf, s->closed_gop, broken_link);
2384  }
2385 }
2386 
2387 static int decode_chunks(AVCodecContext *avctx, AVFrame *picture,
2388  int *got_output, const uint8_t *buf, int buf_size)
2389 {
2390  Mpeg1Context *s = avctx->priv_data;
2392  const uint8_t *buf_ptr = buf;
2393  const uint8_t *buf_end = buf + buf_size;
2394  int ret, input_size;
2395  int last_code = 0, skip_frame = 0;
2396  int picture_start_code_seen = 0;
2397 
2398  for (;;) {
2399  /* find next start code */
2400  uint32_t start_code = -1;
2401  buf_ptr = avpriv_find_start_code(buf_ptr, buf_end, &start_code);
2402  if (start_code > 0x1ff) {
2403  if (!skip_frame) {
2404  if (HAVE_THREADS &&
2405  (avctx->active_thread_type & FF_THREAD_SLICE) &&
2406  !avctx->hwaccel) {
2407  int i;
2408  av_assert0(avctx->thread_count > 1);
2409 
2410  avctx->execute(avctx, slice_decode_thread,
2411  &s2->thread_context[0], NULL,
2412  s->slice_count, sizeof(void *));
2413  for (i = 0; i < s->slice_count; i++)
2414  s2->er.error_count += s2->thread_context[i]->er.error_count;
2415  }
2416 
2417  if ((CONFIG_MPEG_VDPAU_DECODER || CONFIG_MPEG1_VDPAU_DECODER)
2418  && uses_vdpau(avctx))
2419  ff_vdpau_mpeg_picture_complete(s2, buf, buf_size, s->slice_count);
2420 
2421  ret = slice_end(avctx, picture);
2422  if (ret < 0)
2423  return ret;
2424  else if (ret) {
2425  // FIXME: merge with the stuff in mpeg_decode_slice
2426  if (s2->last_picture_ptr || s2->low_delay)
2427  *got_output = 1;
2428  }
2429  }
2430  s2->pict_type = 0;
2431 
2432  if (avctx->err_recognition & AV_EF_EXPLODE && s2->er.error_count)
2433  return AVERROR_INVALIDDATA;
2434 
2435  return FFMAX(0, buf_ptr - buf - s2->parse_context.last_index);
2436  }
2437 
2438  input_size = buf_end - buf_ptr;
2439 
2440  if (avctx->debug & FF_DEBUG_STARTCODE)
2441  av_log(avctx, AV_LOG_DEBUG, "%3"PRIX32" at %"PTRDIFF_SPECIFIER" left %d\n",
2442  start_code, buf_ptr - buf, input_size);
2443 
2444  /* prepare data for next start code */
2445  switch (start_code) {
2446  case SEQ_START_CODE:
2447  if (last_code == 0) {
2448  mpeg1_decode_sequence(avctx, buf_ptr, input_size);
2449  if (buf != avctx->extradata)
2450  s->sync = 1;
2451  } else {
2452  av_log(avctx, AV_LOG_ERROR,
2453  "ignoring SEQ_START_CODE after %X\n", last_code);
2454  if (avctx->err_recognition & AV_EF_EXPLODE)
2455  return AVERROR_INVALIDDATA;
2456  }
2457  break;
2458 
2459  case PICTURE_START_CODE:
2460  if (picture_start_code_seen && s2->picture_structure == PICT_FRAME) {
2461  /* If it's a frame picture, there can't be more than one picture header.
2462  Yet, it does happen and we need to handle it. */
2463  av_log(avctx, AV_LOG_WARNING, "ignoring extra picture following a frame-picture\n");
2464  break;
2465  }
2466  picture_start_code_seen = 1;
2467 
2468  if (s2->width <= 0 || s2->height <= 0) {
2469  av_log(avctx, AV_LOG_ERROR, "Invalid frame dimensions %dx%d.\n",
2470  s2->width, s2->height);
2471  return AVERROR_INVALIDDATA;
2472  }
2473 
2474  if (s->tmpgexs){
2475  s2->intra_dc_precision= 3;
2476  s2->intra_matrix[0]= 1;
2477  }
2478  if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE) &&
2479  !avctx->hwaccel && s->slice_count) {
2480  int i;
2481 
2482  avctx->execute(avctx, slice_decode_thread,
2483  s2->thread_context, NULL,
2484  s->slice_count, sizeof(void *));
2485  for (i = 0; i < s->slice_count; i++)
2486  s2->er.error_count += s2->thread_context[i]->er.error_count;
2487  s->slice_count = 0;
2488  }
2489  if (last_code == 0 || last_code == SLICE_MIN_START_CODE) {
2490  ret = mpeg_decode_postinit(avctx);
2491  if (ret < 0) {
2492  av_log(avctx, AV_LOG_ERROR,
2493  "mpeg_decode_postinit() failure\n");
2494  return ret;
2495  }
2496 
2497  /* We have a complete image: we try to decompress it. */
2498  if (mpeg1_decode_picture(avctx, buf_ptr, input_size) < 0)
2499  s2->pict_type = 0;
2500  s->first_slice = 1;
2501  last_code = PICTURE_START_CODE;
2502  } else {
2503  av_log(avctx, AV_LOG_ERROR,
2504  "ignoring pic after %X\n", last_code);
2505  if (avctx->err_recognition & AV_EF_EXPLODE)
2506  return AVERROR_INVALIDDATA;
2507  }
2508  break;
2509  case EXT_START_CODE:
2510  init_get_bits(&s2->gb, buf_ptr, input_size * 8);
2511 
2512  switch (get_bits(&s2->gb, 4)) {
2513  case 0x1:
2514  if (last_code == 0) {
2516  } else {
2517  av_log(avctx, AV_LOG_ERROR,
2518  "ignoring seq ext after %X\n", last_code);
2519  if (avctx->err_recognition & AV_EF_EXPLODE)
2520  return AVERROR_INVALIDDATA;
2521  }
2522  break;
2523  case 0x2:
2525  break;
2526  case 0x3:
2528  break;
2529  case 0x7:
2531  break;
2532  case 0x8:
2533  if (last_code == PICTURE_START_CODE) {
2535  } else {
2536  av_log(avctx, AV_LOG_ERROR,
2537  "ignoring pic cod ext after %X\n", last_code);
2538  if (avctx->err_recognition & AV_EF_EXPLODE)
2539  return AVERROR_INVALIDDATA;
2540  }
2541  break;
2542  }
2543  break;
2544  case USER_START_CODE:
2545  mpeg_decode_user_data(avctx, buf_ptr, input_size);
2546  break;
2547  case GOP_START_CODE:
2548  if (last_code == 0) {
2549  s2->first_field = 0;
2550  mpeg_decode_gop(avctx, buf_ptr, input_size);
2551  s->sync = 1;
2552  } else {
2553  av_log(avctx, AV_LOG_ERROR,
2554  "ignoring GOP_START_CODE after %X\n", last_code);
2555  if (avctx->err_recognition & AV_EF_EXPLODE)
2556  return AVERROR_INVALIDDATA;
2557  }
2558  break;
2559  default:
2560  if (start_code >= SLICE_MIN_START_CODE &&
2561  start_code <= SLICE_MAX_START_CODE && last_code == PICTURE_START_CODE) {
2562  if (s2->progressive_sequence && !s2->progressive_frame) {
2563  s2->progressive_frame = 1;
2564  av_log(s2->avctx, AV_LOG_ERROR,
2565  "interlaced frame in progressive sequence, ignoring\n");
2566  }
2567 
2568  if (s2->picture_structure == 0 ||
2570  av_log(s2->avctx, AV_LOG_ERROR,
2571  "picture_structure %d invalid, ignoring\n",
2572  s2->picture_structure);
2574  }
2575 
2577  av_log(s2->avctx, AV_LOG_WARNING, "invalid frame_pred_frame_dct\n");
2578 
2579  if (s2->picture_structure == PICT_FRAME) {
2580  s2->first_field = 0;
2581  s2->v_edge_pos = 16 * s2->mb_height;
2582  } else {
2583  s2->first_field ^= 1;
2584  s2->v_edge_pos = 8 * s2->mb_height;
2585  memset(s2->mbskip_table, 0, s2->mb_stride * s2->mb_height);
2586  }
2587  }
2588  if (start_code >= SLICE_MIN_START_CODE &&
2589  start_code <= SLICE_MAX_START_CODE && last_code != 0) {
2590  const int field_pic = s2->picture_structure != PICT_FRAME;
2591  int mb_y = start_code - SLICE_MIN_START_CODE;
2592  last_code = SLICE_MIN_START_CODE;
2593  if (s2->codec_id != AV_CODEC_ID_MPEG1VIDEO && s2->mb_height > 2800/16)
2594  mb_y += (*buf_ptr&0xE0)<<2;
2595 
2596  mb_y <<= field_pic;
2598  mb_y++;
2599 
2600  if (buf_end - buf_ptr < 2) {
2601  av_log(s2->avctx, AV_LOG_ERROR, "slice too small\n");
2602  return AVERROR_INVALIDDATA;
2603  }
2604 
2605  if (mb_y >= s2->mb_height) {
2606  av_log(s2->avctx, AV_LOG_ERROR,
2607  "slice below image (%d >= %d)\n", mb_y, s2->mb_height);
2608  return -1;
2609  }
2610 
2611  if (!s2->last_picture_ptr) {
2612  /* Skip B-frames if we do not have reference frames and
2613  * GOP is not closed. */
2614  if (s2->pict_type == AV_PICTURE_TYPE_B) {
2615  if (!s2->closed_gop) {
2616  skip_frame = 1;
2617  break;
2618  }
2619  }
2620  }
2622  s->sync = 1;
2623  if (!s2->next_picture_ptr) {
2624  /* Skip P-frames if we do not have a reference frame or
2625  * we have an invalid header. */
2626  if (s2->pict_type == AV_PICTURE_TYPE_P && !s->sync) {
2627  skip_frame = 1;
2628  break;
2629  }
2630  }
2631  if ((avctx->skip_frame >= AVDISCARD_NONREF &&
2632  s2->pict_type == AV_PICTURE_TYPE_B) ||
2633  (avctx->skip_frame >= AVDISCARD_NONKEY &&
2634  s2->pict_type != AV_PICTURE_TYPE_I) ||
2635  avctx->skip_frame >= AVDISCARD_ALL) {
2636  skip_frame = 1;
2637  break;
2638  }
2639 
2640  if (!s->mpeg_enc_ctx_allocated)
2641  break;
2642 
2643  if (s2->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
2644  if (mb_y < avctx->skip_top ||
2645  mb_y >= s2->mb_height - avctx->skip_bottom)
2646  break;
2647  }
2648 
2649  if (!s2->pict_type) {
2650  av_log(avctx, AV_LOG_ERROR, "Missing picture start code\n");
2651  if (avctx->err_recognition & AV_EF_EXPLODE)
2652  return AVERROR_INVALIDDATA;
2653  break;
2654  }
2655 
2656  if (s->first_slice) {
2657  skip_frame = 0;
2658  s->first_slice = 0;
2659  if (mpeg_field_start(s2, buf, buf_size) < 0)
2660  return -1;
2661  }
2662  if (!s2->current_picture_ptr) {
2663  av_log(avctx, AV_LOG_ERROR,
2664  "current_picture not initialized\n");
2665  return AVERROR_INVALIDDATA;
2666  }
2667 
2668  if (uses_vdpau(avctx)) {
2669  s->slice_count++;
2670  break;
2671  }
2672 
2673  if (HAVE_THREADS &&
2674  (avctx->active_thread_type & FF_THREAD_SLICE) &&
2675  !avctx->hwaccel) {
2676  int threshold = (s2->mb_height * s->slice_count +
2677  s2->slice_context_count / 2) /
2678  s2->slice_context_count;
2679  av_assert0(avctx->thread_count > 1);
2680  if (threshold <= mb_y) {
2681  MpegEncContext *thread_context = s2->thread_context[s->slice_count];
2682 
2683  thread_context->start_mb_y = mb_y;
2684  thread_context->end_mb_y = s2->mb_height;
2685  if (s->slice_count) {
2686  s2->thread_context[s->slice_count - 1]->end_mb_y = mb_y;
2687  ret = ff_update_duplicate_context(thread_context, s2);
2688  if (ret < 0)
2689  return ret;
2690  }
2691  init_get_bits(&thread_context->gb, buf_ptr, input_size * 8);
2692  s->slice_count++;
2693  }
2694  buf_ptr += 2; // FIXME add minimum number of bytes per slice
2695  } else {
2696  ret = mpeg_decode_slice(s2, mb_y, &buf_ptr, input_size);
2697  emms_c();
2698 
2699  if (ret < 0) {
2700  if (avctx->err_recognition & AV_EF_EXPLODE)
2701  return ret;
2702  if (s2->resync_mb_x >= 0 && s2->resync_mb_y >= 0)
2703  ff_er_add_slice(&s2->er, s2->resync_mb_x,
2704  s2->resync_mb_y, s2->mb_x, s2->mb_y,
2706  } else {
2707  ff_er_add_slice(&s2->er, s2->resync_mb_x,
2708  s2->resync_mb_y, s2->mb_x - 1, s2->mb_y,
2710  }
2711  }
2712  }
2713  break;
2714  }
2715  }
2716 }
2717 
2718 static int mpeg_decode_frame(AVCodecContext *avctx, void *data,
2719  int *got_output, AVPacket *avpkt)
2720 {
2721  const uint8_t *buf = avpkt->data;
2722  int ret;
2723  int buf_size = avpkt->size;
2724  Mpeg1Context *s = avctx->priv_data;
2725  AVFrame *picture = data;
2727 
2728  if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == SEQ_END_CODE)) {
2729  /* special case for last picture */
2730  if (s2->low_delay == 0 && s2->next_picture_ptr) {
2731  int ret = av_frame_ref(picture, s2->next_picture_ptr->f);
2732  if (ret < 0)
2733  return ret;
2734 
2735  s2->next_picture_ptr = NULL;
2736 
2737  *got_output = 1;
2738  }
2739  return buf_size;
2740  }
2741 
2742  if (s2->flags & CODEC_FLAG_TRUNCATED) {
2743  int next = ff_mpeg1_find_frame_end(&s2->parse_context, buf,
2744  buf_size, NULL);
2745 
2746  if (ff_combine_frame(&s2->parse_context, next,
2747  (const uint8_t **) &buf, &buf_size) < 0)
2748  return buf_size;
2749  }
2750 
2751  s2->codec_tag = avpriv_toupper4(avctx->codec_tag);
2752  if (s->mpeg_enc_ctx_allocated == 0 && ( s2->codec_tag == AV_RL32("VCR2")
2753  || s2->codec_tag == AV_RL32("BW10")
2754  ))
2755  vcr2_init_sequence(avctx);
2756 
2757  s->slice_count = 0;
2758 
2759  if (avctx->extradata && !s->extradata_decoded) {
2760  ret = decode_chunks(avctx, picture, got_output,
2761  avctx->extradata, avctx->extradata_size);
2762  if (*got_output) {
2763  av_log(avctx, AV_LOG_ERROR, "picture in extradata\n");
2764  *got_output = 0;
2765  }
2766  s->extradata_decoded = 1;
2767  if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE)) {
2768  s2->current_picture_ptr = NULL;
2769  return ret;
2770  }
2771  }
2772 
2773  ret = decode_chunks(avctx, picture, got_output, buf, buf_size);
2774  if (ret<0 || *got_output)
2775  s2->current_picture_ptr = NULL;
2776 
2777  return ret;
2778 }
2779 
2780 static void flush(AVCodecContext *avctx)
2781 {
2782  Mpeg1Context *s = avctx->priv_data;
2783 
2784  s->sync = 0;
2785 
2786  ff_mpeg_flush(avctx);
2787 }
2788 
2790 {
2791  Mpeg1Context *s = avctx->priv_data;
2792 
2793  if (s->mpeg_enc_ctx_allocated)
2795  av_freep(&s->a53_caption);
2796  return 0;
2797 }
2798 
2800  { FF_PROFILE_MPEG2_422, "4:2:2" },
2801  { FF_PROFILE_MPEG2_HIGH, "High" },
2802  { FF_PROFILE_MPEG2_SS, "Spatially Scalable" },
2803  { FF_PROFILE_MPEG2_SNR_SCALABLE, "SNR Scalable" },
2804  { FF_PROFILE_MPEG2_MAIN, "Main" },
2805  { FF_PROFILE_MPEG2_SIMPLE, "Simple" },
2806  { FF_PROFILE_RESERVED, "Reserved" },
2807  { FF_PROFILE_RESERVED, "Reserved" },
2808  { FF_PROFILE_UNKNOWN },
2809 };
2810 
2812  .name = "mpeg1video",
2813  .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video"),
2814  .type = AVMEDIA_TYPE_VIDEO,
2815  .id = AV_CODEC_ID_MPEG1VIDEO,
2816  .priv_data_size = sizeof(Mpeg1Context),
2820  .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
2823  .flush = flush,
2824  .max_lowres = 3,
2826 };
2827 
2829  .name = "mpeg2video",
2830  .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 video"),
2831  .type = AVMEDIA_TYPE_VIDEO,
2832  .id = AV_CODEC_ID_MPEG2VIDEO,
2833  .priv_data_size = sizeof(Mpeg1Context),
2837  .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
2840  .flush = flush,
2841  .max_lowres = 3,
2842  .profiles = NULL_IF_CONFIG_SMALL(mpeg2_video_profiles),
2843 };
2844 
2845 //legacy decoder
2847  .name = "mpegvideo",
2848  .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video"),
2849  .type = AVMEDIA_TYPE_VIDEO,
2850  .id = AV_CODEC_ID_MPEG2VIDEO,
2851  .priv_data_size = sizeof(Mpeg1Context),
2856  .flush = flush,
2857  .max_lowres = 3,
2858 };
2859 
2860 #if FF_API_XVMC
2861 #if CONFIG_MPEG_XVMC_DECODER
2862 static av_cold int mpeg_mc_decode_init(AVCodecContext *avctx)
2863 {
2864  if (avctx->active_thread_type & FF_THREAD_SLICE)
2865  return -1;
2866  if (!(avctx->slice_flags & SLICE_FLAG_CODED_ORDER))
2867  return -1;
2868  if (!(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD)) {
2869  av_dlog(avctx, "mpeg12.c: XvMC decoder will work better if SLICE_FLAG_ALLOW_FIELD is set\n");
2870  }
2871  mpeg_decode_init(avctx);
2872 
2874  avctx->xvmc_acceleration = 2; // 2 - the blocks are packed!
2875 
2876  return 0;
2877 }
2878 
2879 AVCodec ff_mpeg_xvmc_decoder = {
2880  .name = "mpegvideo_xvmc",
2881  .long_name = NULL_IF_CONFIG_SMALL("MPEG-1/2 video XvMC (X-Video Motion Compensation)"),
2882  .type = AVMEDIA_TYPE_VIDEO,
2883  .id = AV_CODEC_ID_MPEG2VIDEO_XVMC,
2884  .priv_data_size = sizeof(Mpeg1Context),
2885  .init = mpeg_mc_decode_init,
2888  .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
2889  CODEC_CAP_TRUNCATED | CODEC_CAP_HWACCEL | CODEC_CAP_DELAY,
2890  .flush = flush,
2891 };
2892 
2893 #endif
2894 #endif /* FF_API_XVMC */
2895 
2896 #if CONFIG_MPEG_VDPAU_DECODER
2897 AVCodec ff_mpeg_vdpau_decoder = {
2898  .name = "mpegvideo_vdpau",
2899  .long_name = NULL_IF_CONFIG_SMALL("MPEG-1/2 video (VDPAU acceleration)"),
2900  .type = AVMEDIA_TYPE_VIDEO,
2901  .id = AV_CODEC_ID_MPEG2VIDEO,
2902  .priv_data_size = sizeof(Mpeg1Context),
2906  .capabilities = CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED |
2908  .flush = flush,
2909 };
2910 #endif
2911 
2912 #if CONFIG_MPEG1_VDPAU_DECODER
2913 AVCodec ff_mpeg1_vdpau_decoder = {
2914  .name = "mpeg1video_vdpau",
2915  .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video (VDPAU acceleration)"),
2916  .type = AVMEDIA_TYPE_VIDEO,
2917  .id = AV_CODEC_ID_MPEG1VIDEO,
2918  .priv_data_size = sizeof(Mpeg1Context),
2922  .capabilities = CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED |
2924  .flush = flush,
2925 };
2926 #endif