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