FFmpeg  3.4.9
mpeg12dec.c
Go to the documentation of this file.
1 /*
2  * MPEG-1/2 decoder
3  * Copyright (c) 2000, 2001 Fabrice Bellard
4  * Copyright (c) 2002-2013 Michael Niedermayer <michaelni@gmx.at>
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 /**
24  * @file
25  * MPEG-1/2 decoder
26  */
27 
28 #define UNCHECKED_BITSTREAM_READER 1
29 #include <inttypes.h>
30 
31 #include "libavutil/attributes.h"
32 #include "libavutil/imgutils.h"
33 #include "libavutil/internal.h"
34 #include "libavutil/stereo3d.h"
35 
36 #include "avcodec.h"
37 #include "bytestream.h"
38 #include "error_resilience.h"
39 #include "idctdsp.h"
40 #include "internal.h"
41 #include "mpeg_er.h"
42 #include "mpeg12.h"
43 #include "mpeg12data.h"
44 #include "mpegutils.h"
45 #include "mpegvideo.h"
46 #include "mpegvideodata.h"
47 #include "profiles.h"
48 #include "thread.h"
49 #include "version.h"
50 #include "vdpau_compat.h"
51 #include "xvmc_internal.h"
52 
53 typedef struct Mpeg1Context {
55  int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
56  int repeat_field; /* true if we must repeat the field */
57  AVPanScan pan_scan; /* some temporary storage for the panscan */
63  int has_afd;
67  AVRational frame_rate_ext; /* MPEG-2 specific framerate modificator */
68  int sync; /* Did we reach a sync point like a GOP/SEQ/KEYFrame? */
69  int tmpgexs;
72 } Mpeg1Context;
73 
74 #define MB_TYPE_ZERO_MV 0x20000000
75 
76 static const uint32_t ptype2mb_type[7] = {
79  MB_TYPE_L0,
80  MB_TYPE_L0 | MB_TYPE_CBP,
83  MB_TYPE_QUANT | MB_TYPE_L0 | MB_TYPE_CBP,
84 };
85 
86 static const uint32_t btype2mb_type[11] = {
88  MB_TYPE_L1,
89  MB_TYPE_L1 | MB_TYPE_CBP,
90  MB_TYPE_L0,
91  MB_TYPE_L0 | MB_TYPE_CBP,
93  MB_TYPE_L0L1 | MB_TYPE_CBP,
95  MB_TYPE_QUANT | MB_TYPE_L1 | MB_TYPE_CBP,
96  MB_TYPE_QUANT | MB_TYPE_L0 | MB_TYPE_CBP,
97  MB_TYPE_QUANT | MB_TYPE_L0L1 | MB_TYPE_CBP,
98 };
99 
100 /* as H.263, but only 17 codes */
101 static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred)
102 {
103  int code, sign, val, shift;
104 
105  code = get_vlc2(&s->gb, ff_mv_vlc.table, MV_VLC_BITS, 2);
106  if (code == 0)
107  return pred;
108  if (code < 0)
109  return 0xffff;
110 
111  sign = get_bits1(&s->gb);
112  shift = fcode - 1;
113  val = code;
114  if (shift) {
115  val = (val - 1) << shift;
116  val |= get_bits(&s->gb, shift);
117  val++;
118  }
119  if (sign)
120  val = -val;
121  val += pred;
122 
123  /* modulo decoding */
124  return sign_extend(val, 5 + shift);
125 }
126 
127 #define MAX_INDEX (64 - 1)
128 #define check_scantable_index(ctx, x) \
129  do { \
130  if ((x) > MAX_INDEX) { \
131  av_log(ctx->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", \
132  ctx->mb_x, ctx->mb_y); \
133  return AVERROR_INVALIDDATA; \
134  } \
135  } while (0)
136 
138  int16_t *block, int n)
139 {
140  int level, i, j, run;
141  RLTable *rl = &ff_rl_mpeg1;
142  uint8_t *const scantable = s->intra_scantable.permutated;
143  const uint16_t *quant_matrix = s->inter_matrix;
144  const int qscale = s->qscale;
145 
146  {
147  OPEN_READER(re, &s->gb);
148  i = -1;
149  // special case for first coefficient, no need to add second VLC table
150  UPDATE_CACHE(re, &s->gb);
151  if (((int32_t) GET_CACHE(re, &s->gb)) < 0) {
152  level = (3 * qscale * quant_matrix[0]) >> 5;
153  level = (level - 1) | 1;
154  if (GET_CACHE(re, &s->gb) & 0x40000000)
155  level = -level;
156  block[0] = level;
157  i++;
158  SKIP_BITS(re, &s->gb, 2);
159  if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
160  goto end;
161  }
162  /* now quantify & encode AC coefficients */
163  for (;;) {
164  GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
165  TEX_VLC_BITS, 2, 0);
166 
167  if (level != 0) {
168  i += run;
169  if (i > MAX_INDEX)
170  break;
171  j = scantable[i];
172  level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
173  level = (level - 1) | 1;
174  level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
175  SHOW_SBITS(re, &s->gb, 1);
176  SKIP_BITS(re, &s->gb, 1);
177  } else {
178  /* escape */
179  run = SHOW_UBITS(re, &s->gb, 6) + 1;
180  LAST_SKIP_BITS(re, &s->gb, 6);
181  UPDATE_CACHE(re, &s->gb);
182  level = SHOW_SBITS(re, &s->gb, 8);
183  SKIP_BITS(re, &s->gb, 8);
184  if (level == -128) {
185  level = SHOW_UBITS(re, &s->gb, 8) - 256;
186  SKIP_BITS(re, &s->gb, 8);
187  } else if (level == 0) {
188  level = SHOW_UBITS(re, &s->gb, 8);
189  SKIP_BITS(re, &s->gb, 8);
190  }
191  i += run;
192  if (i > MAX_INDEX)
193  break;
194  j = scantable[i];
195  if (level < 0) {
196  level = -level;
197  level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
198  level = (level - 1) | 1;
199  level = -level;
200  } else {
201  level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
202  level = (level - 1) | 1;
203  }
204  }
205 
206  block[j] = level;
207  if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
208  break;
209  UPDATE_CACHE(re, &s->gb);
210  }
211 end:
212  LAST_SKIP_BITS(re, &s->gb, 2);
213  CLOSE_READER(re, &s->gb);
214  }
215 
216  check_scantable_index(s, i);
217 
218  s->block_last_index[n] = i;
219  return 0;
220 }
221 
222 /**
223  * Changing this would eat up any speed benefits it has.
224  * Do not use "fast" flag if you need the code to be robust.
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 
397 /**
398  * Changing this would eat up any speed benefits it has.
399  * Do not use "fast" flag if you need the code to be robust.
400  */
402  int16_t *block, int n)
403 {
404  int level, i, j, run;
405  RLTable *rl = &ff_rl_mpeg1;
406  uint8_t *const scantable = s->intra_scantable.permutated;
407  const int qscale = s->qscale;
408  OPEN_READER(re, &s->gb);
409  i = -1;
410 
411  // special case for first coefficient, no need to add second VLC table
412  UPDATE_CACHE(re, &s->gb);
413  if (((int32_t) GET_CACHE(re, &s->gb)) < 0) {
414  level = (3 * qscale) >> 1;
415  if (GET_CACHE(re, &s->gb) & 0x40000000)
416  level = -level;
417  block[0] = level;
418  i++;
419  SKIP_BITS(re, &s->gb, 2);
420  if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
421  goto end;
422  }
423 
424  /* now quantify & encode AC coefficients */
425  for (;;) {
426  GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
427 
428  if (level != 0) {
429  i += run;
430  if (i > MAX_INDEX)
431  break;
432  j = scantable[i];
433  level = ((level * 2 + 1) * qscale) >> 1;
434  level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
435  SHOW_SBITS(re, &s->gb, 1);
436  SKIP_BITS(re, &s->gb, 1);
437  } else {
438  /* escape */
439  run = SHOW_UBITS(re, &s->gb, 6) + 1;
440  LAST_SKIP_BITS(re, &s->gb, 6);
441  UPDATE_CACHE(re, &s->gb);
442  level = SHOW_SBITS(re, &s->gb, 12);
443  SKIP_BITS(re, &s->gb, 12);
444 
445  i += run;
446  if (i > MAX_INDEX)
447  break;
448  j = scantable[i];
449  if (level < 0) {
450  level = ((-level * 2 + 1) * qscale) >> 1;
451  level = -level;
452  } else {
453  level = ((level * 2 + 1) * qscale) >> 1;
454  }
455  }
456 
457  block[j] = level;
458  if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF || i > 63)
459  break;
460 
461  UPDATE_CACHE(re, &s->gb);
462  }
463 end:
464  LAST_SKIP_BITS(re, &s->gb, 2);
465  CLOSE_READER(re, &s->gb);
466 
467  check_scantable_index(s, i);
468 
469  s->block_last_index[n] = i;
470  return 0;
471 }
472 
474  int16_t *block, int n)
475 {
476  int level, dc, diff, i, j, run;
477  int component;
478  RLTable *rl;
479  uint8_t *const scantable = s->intra_scantable.permutated;
480  const uint16_t *quant_matrix;
481  const int qscale = s->qscale;
482  int mismatch;
483 
484  /* DC coefficient */
485  if (n < 4) {
486  quant_matrix = s->intra_matrix;
487  component = 0;
488  } else {
489  quant_matrix = s->chroma_intra_matrix;
490  component = (n & 1) + 1;
491  }
492  diff = decode_dc(&s->gb, component);
493  if (diff >= 0xffff)
494  return AVERROR_INVALIDDATA;
495  dc = s->last_dc[component];
496  dc += diff;
497  s->last_dc[component] = dc;
498  block[0] = dc * (1 << (3 - s->intra_dc_precision));
499  ff_tlog(s->avctx, "dc=%d\n", block[0]);
500  mismatch = block[0] ^ 1;
501  i = 0;
502  if (s->intra_vlc_format)
503  rl = &ff_rl_mpeg2;
504  else
505  rl = &ff_rl_mpeg1;
506 
507  {
508  OPEN_READER(re, &s->gb);
509  /* now quantify & encode AC coefficients */
510  for (;;) {
511  UPDATE_CACHE(re, &s->gb);
512  GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
513  TEX_VLC_BITS, 2, 0);
514 
515  if (level == 127) {
516  break;
517  } else if (level != 0) {
518  i += run;
519  if (i > MAX_INDEX)
520  break;
521  j = scantable[i];
522  level = (level * qscale * quant_matrix[j]) >> 4;
523  level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
524  SHOW_SBITS(re, &s->gb, 1);
525  LAST_SKIP_BITS(re, &s->gb, 1);
526  } else {
527  /* escape */
528  run = SHOW_UBITS(re, &s->gb, 6) + 1;
529  LAST_SKIP_BITS(re, &s->gb, 6);
530  UPDATE_CACHE(re, &s->gb);
531  level = SHOW_SBITS(re, &s->gb, 12);
532  SKIP_BITS(re, &s->gb, 12);
533  i += run;
534  if (i > MAX_INDEX)
535  break;
536  j = scantable[i];
537  if (level < 0) {
538  level = (-level * qscale * quant_matrix[j]) >> 4;
539  level = -level;
540  } else {
541  level = (level * qscale * quant_matrix[j]) >> 4;
542  }
543  }
544 
545  mismatch ^= level;
546  block[j] = level;
547  }
548  CLOSE_READER(re, &s->gb);
549  }
550  block[63] ^= mismatch & 1;
551 
552  check_scantable_index(s, i);
553 
554  s->block_last_index[n] = i;
555  return 0;
556 }
557 
558 /**
559  * Changing this would eat up any speed benefits it has.
560  * Do not use "fast" flag if you need the code to be robust.
561  */
563  int16_t *block, int n)
564 {
565  int level, dc, diff, i, j, run;
566  int component;
567  RLTable *rl;
568  uint8_t *const scantable = s->intra_scantable.permutated;
569  const uint16_t *quant_matrix;
570  const int qscale = s->qscale;
571 
572  /* DC coefficient */
573  if (n < 4) {
574  quant_matrix = s->intra_matrix;
575  component = 0;
576  } else {
577  quant_matrix = s->chroma_intra_matrix;
578  component = (n & 1) + 1;
579  }
580  diff = decode_dc(&s->gb, component);
581  if (diff >= 0xffff)
582  return AVERROR_INVALIDDATA;
583  dc = s->last_dc[component];
584  dc += diff;
585  s->last_dc[component] = dc;
586  block[0] = dc * (1 << (3 - s->intra_dc_precision));
587  i = 0;
588  if (s->intra_vlc_format)
589  rl = &ff_rl_mpeg2;
590  else
591  rl = &ff_rl_mpeg1;
592 
593  {
594  OPEN_READER(re, &s->gb);
595  /* now quantify & encode AC coefficients */
596  for (;;) {
597  UPDATE_CACHE(re, &s->gb);
598  GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
599  TEX_VLC_BITS, 2, 0);
600 
601  if (level >= 64 || i > 63) {
602  break;
603  } else if (level != 0) {
604  i += run;
605  j = scantable[i];
606  level = (level * qscale * quant_matrix[j]) >> 4;
607  level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
608  SHOW_SBITS(re, &s->gb, 1);
609  LAST_SKIP_BITS(re, &s->gb, 1);
610  } else {
611  /* escape */
612  run = SHOW_UBITS(re, &s->gb, 6) + 1;
613  LAST_SKIP_BITS(re, &s->gb, 6);
614  UPDATE_CACHE(re, &s->gb);
615  level = SHOW_SBITS(re, &s->gb, 12);
616  SKIP_BITS(re, &s->gb, 12);
617  i += run;
618  j = scantable[i];
619  if (level < 0) {
620  level = (-level * qscale * quant_matrix[j]) >> 4;
621  level = -level;
622  } else {
623  level = (level * qscale * quant_matrix[j]) >> 4;
624  }
625  }
626 
627  block[j] = level;
628  }
629  CLOSE_READER(re, &s->gb);
630  }
631 
632  check_scantable_index(s, i);
633 
634  s->block_last_index[n] = i;
635  return 0;
636 }
637 
638 /******************************************/
639 /* decoding */
640 
641 static inline int get_dmv(MpegEncContext *s)
642 {
643  if (get_bits1(&s->gb))
644  return 1 - (get_bits1(&s->gb) << 1);
645  else
646  return 0;
647 }
648 
649 static inline int get_qscale(MpegEncContext *s)
650 {
651  int qscale = get_bits(&s->gb, 5);
652  if (s->q_scale_type)
653  return ff_mpeg2_non_linear_qscale[qscale];
654  else
655  return qscale << 1;
656 }
657 
658 
659 /* motion type (for MPEG-2) */
660 #define MT_FIELD 1
661 #define MT_FRAME 2
662 #define MT_16X8 2
663 #define MT_DMV 3
664 
665 static int mpeg_decode_mb(MpegEncContext *s, int16_t block[12][64])
666 {
667  int i, j, k, cbp, val, mb_type, motion_type;
668  const int mb_block_count = 4 + (1 << s->chroma_format);
669  int ret;
670 
671  ff_tlog(s->avctx, "decode_mb: x=%d y=%d\n", s->mb_x, s->mb_y);
672 
673  av_assert2(s->mb_skipped == 0);
674 
675  if (s->mb_skip_run-- != 0) {
676  if (s->pict_type == AV_PICTURE_TYPE_P) {
677  s->mb_skipped = 1;
678  s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride] =
680  } else {
681  int mb_type;
682 
683  if (s->mb_x)
684  mb_type = s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride - 1];
685  else
686  // FIXME not sure if this is allowed in MPEG at all
687  mb_type = s->current_picture.mb_type[s->mb_width + (s->mb_y - 1) * s->mb_stride - 1];
688  if (IS_INTRA(mb_type)) {
689  av_log(s->avctx, AV_LOG_ERROR, "skip with previntra\n");
690  return AVERROR_INVALIDDATA;
691  }
692  s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride] =
693  mb_type | MB_TYPE_SKIP;
694 
695  if ((s->mv[0][0][0] | s->mv[0][0][1] | s->mv[1][0][0] | s->mv[1][0][1]) == 0)
696  s->mb_skipped = 1;
697  }
698 
699  return 0;
700  }
701 
702  switch (s->pict_type) {
703  default:
704  case AV_PICTURE_TYPE_I:
705  if (get_bits1(&s->gb) == 0) {
706  if (get_bits1(&s->gb) == 0) {
708  "Invalid mb type in I-frame at %d %d\n",
709  s->mb_x, s->mb_y);
710  return AVERROR_INVALIDDATA;
711  }
712  mb_type = MB_TYPE_QUANT | MB_TYPE_INTRA;
713  } else {
714  mb_type = MB_TYPE_INTRA;
715  }
716  break;
717  case AV_PICTURE_TYPE_P:
718  mb_type = get_vlc2(&s->gb, ff_mb_ptype_vlc.table, MB_PTYPE_VLC_BITS, 1);
719  if (mb_type < 0) {
721  "Invalid mb type in P-frame at %d %d\n", s->mb_x, s->mb_y);
722  return AVERROR_INVALIDDATA;
723  }
724  mb_type = ptype2mb_type[mb_type];
725  break;
726  case AV_PICTURE_TYPE_B:
727  mb_type = get_vlc2(&s->gb, ff_mb_btype_vlc.table, MB_BTYPE_VLC_BITS, 1);
728  if (mb_type < 0) {
730  "Invalid mb type in B-frame at %d %d\n", s->mb_x, s->mb_y);
731  return AVERROR_INVALIDDATA;
732  }
733  mb_type = btype2mb_type[mb_type];
734  break;
735  }
736  ff_tlog(s->avctx, "mb_type=%x\n", mb_type);
737 // motion_type = 0; /* avoid warning */
738  if (IS_INTRA(mb_type)) {
739  s->bdsp.clear_blocks(s->block[0]);
740 
741  if (!s->chroma_y_shift)
742  s->bdsp.clear_blocks(s->block[6]);
743 
744  /* compute DCT type */
745  // FIXME: add an interlaced_dct coded var?
746  if (s->picture_structure == PICT_FRAME &&
748  s->interlaced_dct = get_bits1(&s->gb);
749 
750  if (IS_QUANT(mb_type))
751  s->qscale = get_qscale(s);
752 
754  /* just parse them */
755  if (s->picture_structure != PICT_FRAME)
756  skip_bits1(&s->gb); /* field select */
757 
758  s->mv[0][0][0] =
759  s->last_mv[0][0][0] =
760  s->last_mv[0][1][0] = mpeg_decode_motion(s, s->mpeg_f_code[0][0],
761  s->last_mv[0][0][0]);
762  s->mv[0][0][1] =
763  s->last_mv[0][0][1] =
764  s->last_mv[0][1][1] = mpeg_decode_motion(s, s->mpeg_f_code[0][1],
765  s->last_mv[0][0][1]);
766 
767  check_marker(s->avctx, &s->gb, "after concealment_motion_vectors");
768  } else {
769  /* reset mv prediction */
770  memset(s->last_mv, 0, sizeof(s->last_mv));
771  }
772  s->mb_intra = 1;
773  // if 1, we memcpy blocks in xvmcvideo
775  ff_xvmc_pack_pblocks(s, -1); // inter are always full blocks
776 
777  if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
778  if (s->avctx->flags2 & AV_CODEC_FLAG2_FAST) {
779  for (i = 0; i < 6; i++)
781  } else {
782  for (i = 0; i < mb_block_count; i++)
783  if ((ret = mpeg2_decode_block_intra(s, *s->pblocks[i], i)) < 0)
784  return ret;
785  }
786  } else {
787  for (i = 0; i < 6; i++) {
789  s->intra_matrix,
791  s->last_dc, *s->pblocks[i],
792  i, s->qscale);
793  if (ret < 0) {
794  av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n",
795  s->mb_x, s->mb_y);
796  return ret;
797  }
798 
799  s->block_last_index[i] = ret;
800  }
801  }
802  } else {
803  if (mb_type & MB_TYPE_ZERO_MV) {
804  av_assert2(mb_type & MB_TYPE_CBP);
805 
806  s->mv_dir = MV_DIR_FORWARD;
807  if (s->picture_structure == PICT_FRAME) {
808  if (s->picture_structure == PICT_FRAME
809  && !s->frame_pred_frame_dct)
810  s->interlaced_dct = get_bits1(&s->gb);
811  s->mv_type = MV_TYPE_16X16;
812  } else {
813  s->mv_type = MV_TYPE_FIELD;
814  mb_type |= MB_TYPE_INTERLACED;
815  s->field_select[0][0] = s->picture_structure - 1;
816  }
817 
818  if (IS_QUANT(mb_type))
819  s->qscale = get_qscale(s);
820 
821  s->last_mv[0][0][0] = 0;
822  s->last_mv[0][0][1] = 0;
823  s->last_mv[0][1][0] = 0;
824  s->last_mv[0][1][1] = 0;
825  s->mv[0][0][0] = 0;
826  s->mv[0][0][1] = 0;
827  } else {
828  av_assert2(mb_type & MB_TYPE_L0L1);
829  // FIXME decide if MBs in field pictures are MB_TYPE_INTERLACED
830  /* get additional motion vector type */
832  motion_type = MT_FRAME;
833  } else {
834  motion_type = get_bits(&s->gb, 2);
835  if (s->picture_structure == PICT_FRAME && HAS_CBP(mb_type))
836  s->interlaced_dct = get_bits1(&s->gb);
837  }
838 
839  if (IS_QUANT(mb_type))
840  s->qscale = get_qscale(s);
841 
842  /* motion vectors */
843  s->mv_dir = (mb_type >> 13) & 3;
844  ff_tlog(s->avctx, "motion_type=%d\n", motion_type);
845  switch (motion_type) {
846  case MT_FRAME: /* or MT_16X8 */
847  if (s->picture_structure == PICT_FRAME) {
848  mb_type |= MB_TYPE_16x16;
849  s->mv_type = MV_TYPE_16X16;
850  for (i = 0; i < 2; i++) {
851  if (USES_LIST(mb_type, i)) {
852  /* MT_FRAME */
853  s->mv[i][0][0] =
854  s->last_mv[i][0][0] =
855  s->last_mv[i][1][0] =
856  mpeg_decode_motion(s, s->mpeg_f_code[i][0],
857  s->last_mv[i][0][0]);
858  s->mv[i][0][1] =
859  s->last_mv[i][0][1] =
860  s->last_mv[i][1][1] =
861  mpeg_decode_motion(s, s->mpeg_f_code[i][1],
862  s->last_mv[i][0][1]);
863  /* full_pel: only for MPEG-1 */
864  if (s->full_pel[i]) {
865  s->mv[i][0][0] *= 2;
866  s->mv[i][0][1] *= 2;
867  }
868  }
869  }
870  } else {
871  mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
872  s->mv_type = MV_TYPE_16X8;
873  for (i = 0; i < 2; i++) {
874  if (USES_LIST(mb_type, i)) {
875  /* MT_16X8 */
876  for (j = 0; j < 2; j++) {
877  s->field_select[i][j] = get_bits1(&s->gb);
878  for (k = 0; k < 2; k++) {
879  val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
880  s->last_mv[i][j][k]);
881  s->last_mv[i][j][k] = val;
882  s->mv[i][j][k] = val;
883  }
884  }
885  }
886  }
887  }
888  break;
889  case MT_FIELD:
890  s->mv_type = MV_TYPE_FIELD;
891  if (s->picture_structure == PICT_FRAME) {
892  mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
893  for (i = 0; i < 2; i++) {
894  if (USES_LIST(mb_type, i)) {
895  for (j = 0; j < 2; j++) {
896  s->field_select[i][j] = get_bits1(&s->gb);
897  val = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
898  s->last_mv[i][j][0]);
899  s->last_mv[i][j][0] = val;
900  s->mv[i][j][0] = val;
901  ff_tlog(s->avctx, "fmx=%d\n", val);
902  val = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
903  s->last_mv[i][j][1] >> 1);
904  s->last_mv[i][j][1] = 2 * val;
905  s->mv[i][j][1] = val;
906  ff_tlog(s->avctx, "fmy=%d\n", val);
907  }
908  }
909  }
910  } else {
912  mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
913  for (i = 0; i < 2; i++) {
914  if (USES_LIST(mb_type, i)) {
915  s->field_select[i][0] = get_bits1(&s->gb);
916  for (k = 0; k < 2; k++) {
917  val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
918  s->last_mv[i][0][k]);
919  s->last_mv[i][0][k] = val;
920  s->last_mv[i][1][k] = val;
921  s->mv[i][0][k] = val;
922  }
923  }
924  }
925  }
926  break;
927  case MT_DMV:
928  if (s->progressive_sequence){
929  av_log(s->avctx, AV_LOG_ERROR, "MT_DMV in progressive_sequence\n");
930  return AVERROR_INVALIDDATA;
931  }
932  s->mv_type = MV_TYPE_DMV;
933  for (i = 0; i < 2; i++) {
934  if (USES_LIST(mb_type, i)) {
935  int dmx, dmy, mx, my, m;
936  const int my_shift = s->picture_structure == PICT_FRAME;
937 
938  mx = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
939  s->last_mv[i][0][0]);
940  s->last_mv[i][0][0] = mx;
941  s->last_mv[i][1][0] = mx;
942  dmx = get_dmv(s);
943  my = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
944  s->last_mv[i][0][1] >> my_shift);
945  dmy = get_dmv(s);
946 
947 
948  s->last_mv[i][0][1] = my * (1 << my_shift);
949  s->last_mv[i][1][1] = my * (1 << my_shift);
950 
951  s->mv[i][0][0] = mx;
952  s->mv[i][0][1] = my;
953  s->mv[i][1][0] = mx; // not used
954  s->mv[i][1][1] = my; // not used
955 
956  if (s->picture_structure == PICT_FRAME) {
957  mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
958 
959  // m = 1 + 2 * s->top_field_first;
960  m = s->top_field_first ? 1 : 3;
961 
962  /* top -> top pred */
963  s->mv[i][2][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
964  s->mv[i][2][1] = ((my * m + (my > 0)) >> 1) + dmy - 1;
965  m = 4 - m;
966  s->mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
967  s->mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1;
968  } else {
969  mb_type |= MB_TYPE_16x16;
970 
971  s->mv[i][2][0] = ((mx + (mx > 0)) >> 1) + dmx;
972  s->mv[i][2][1] = ((my + (my > 0)) >> 1) + dmy;
974  s->mv[i][2][1]--;
975  else
976  s->mv[i][2][1]++;
977  }
978  }
979  }
980  break;
981  default:
983  "00 motion_type at %d %d\n", s->mb_x, s->mb_y);
984  return AVERROR_INVALIDDATA;
985  }
986  }
987 
988  s->mb_intra = 0;
989  if (HAS_CBP(mb_type)) {
990  s->bdsp.clear_blocks(s->block[0]);
991 
993  if (mb_block_count > 6) {
994  cbp *= 1 << mb_block_count - 6;
995  cbp |= get_bits(&s->gb, mb_block_count - 6);
996  s->bdsp.clear_blocks(s->block[6]);
997  }
998  if (cbp <= 0) {
1000  "invalid cbp %d at %d %d\n", cbp, s->mb_x, s->mb_y);
1001  return AVERROR_INVALIDDATA;
1002  }
1003 
1004  // if 1, we memcpy blocks in xvmcvideo
1006  ff_xvmc_pack_pblocks(s, cbp);
1007 
1008  if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
1009  if (s->avctx->flags2 & AV_CODEC_FLAG2_FAST) {
1010  for (i = 0; i < 6; i++) {
1011  if (cbp & 32)
1013  else
1014  s->block_last_index[i] = -1;
1015  cbp += cbp;
1016  }
1017  } else {
1018  cbp <<= 12 - mb_block_count;
1019 
1020  for (i = 0; i < mb_block_count; i++) {
1021  if (cbp & (1 << 11)) {
1022  if ((ret = mpeg2_decode_block_non_intra(s, *s->pblocks[i], i)) < 0)
1023  return ret;
1024  } else {
1025  s->block_last_index[i] = -1;
1026  }
1027  cbp += cbp;
1028  }
1029  }
1030  } else {
1031  if (s->avctx->flags2 & AV_CODEC_FLAG2_FAST) {
1032  for (i = 0; i < 6; i++) {
1033  if (cbp & 32)
1034  mpeg1_fast_decode_block_inter(s, *s->pblocks[i], i);
1035  else
1036  s->block_last_index[i] = -1;
1037  cbp += cbp;
1038  }
1039  } else {
1040  for (i = 0; i < 6; i++) {
1041  if (cbp & 32) {
1042  if ((ret = mpeg1_decode_block_inter(s, *s->pblocks[i], i)) < 0)
1043  return ret;
1044  } else {
1045  s->block_last_index[i] = -1;
1046  }
1047  cbp += cbp;
1048  }
1049  }
1050  }
1051  } else {
1052  for (i = 0; i < 12; i++)
1053  s->block_last_index[i] = -1;
1054  }
1055  }
1056 
1057  s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride] = mb_type;
1058 
1059  return 0;
1060 }
1061 
1063 {
1064  Mpeg1Context *s = avctx->priv_data;
1066 
1068 
1069  if ( avctx->codec_tag != AV_RL32("VCR2")
1070  && avctx->codec_tag != AV_RL32("BW10"))
1071  avctx->coded_width = avctx->coded_height = 0; // do not trust dimensions from input
1072  ff_mpv_decode_init(s2, avctx);
1073 
1074  s->mpeg_enc_ctx.avctx = avctx;
1075 
1076  /* we need some permutation to store matrices,
1077  * until the decoder sets the real permutation. */
1078  ff_mpv_idct_init(s2);
1081 
1082  s2->chroma_format = 1;
1083  s->mpeg_enc_ctx_allocated = 0;
1085  s->repeat_field = 0;
1086  s->mpeg_enc_ctx.codec_id = avctx->codec->id;
1087  avctx->color_range = AVCOL_RANGE_MPEG;
1088  return 0;
1089 }
1090 
1091 #if HAVE_THREADS
1092 static int mpeg_decode_update_thread_context(AVCodecContext *avctx,
1093  const AVCodecContext *avctx_from)
1094 {
1095  Mpeg1Context *ctx = avctx->priv_data, *ctx_from = avctx_from->priv_data;
1096  MpegEncContext *s = &ctx->mpeg_enc_ctx, *s1 = &ctx_from->mpeg_enc_ctx;
1097  int err;
1098 
1099  if (avctx == avctx_from ||
1100  !ctx_from->mpeg_enc_ctx_allocated ||
1101  !s1->context_initialized)
1102  return 0;
1103 
1104  err = ff_mpeg_update_thread_context(avctx, avctx_from);
1105  if (err)
1106  return err;
1107 
1108  if (!ctx->mpeg_enc_ctx_allocated)
1109  memcpy(s + 1, s1 + 1, sizeof(Mpeg1Context) - sizeof(MpegEncContext));
1110 
1111  if (!(s->pict_type == AV_PICTURE_TYPE_B || s->low_delay))
1112  s->picture_number++;
1113 
1114  return 0;
1115 }
1116 #endif
1117 
1118 static void quant_matrix_rebuild(uint16_t *matrix, const uint8_t *old_perm,
1119  const uint8_t *new_perm)
1120 {
1121  uint16_t temp_matrix[64];
1122  int i;
1123 
1124  memcpy(temp_matrix, matrix, 64 * sizeof(uint16_t));
1125 
1126  for (i = 0; i < 64; i++)
1127  matrix[new_perm[i]] = temp_matrix[old_perm[i]];
1128 }
1129 
1131 #if CONFIG_MPEG1_XVMC_HWACCEL
1133 #endif
1134 #if CONFIG_MPEG1_VDPAU_DECODER && FF_API_VDPAU
1136 #endif
1137 #if CONFIG_MPEG1_VDPAU_HWACCEL
1139 #endif
1142 };
1143 
1145 #if CONFIG_MPEG2_XVMC_HWACCEL
1147 #endif
1148 #if CONFIG_MPEG_VDPAU_DECODER && FF_API_VDPAU
1150 #endif
1151 #if CONFIG_MPEG2_VDPAU_HWACCEL
1153 #endif
1154 #if CONFIG_MPEG2_DXVA2_HWACCEL
1156 #endif
1157 #if CONFIG_MPEG2_D3D11VA_HWACCEL
1160 #endif
1161 #if CONFIG_MPEG2_VAAPI_HWACCEL
1163 #endif
1164 #if CONFIG_MPEG2_VIDEOTOOLBOX_HWACCEL
1166 #endif
1169 };
1170 
1171 static const enum AVPixelFormat mpeg12_pixfmt_list_422[] = {
1174 };
1175 
1176 static const enum AVPixelFormat mpeg12_pixfmt_list_444[] = {
1179 };
1180 
1181 #if FF_API_VDPAU
1182 static inline int uses_vdpau(AVCodecContext *avctx) {
1183  return avctx->pix_fmt == AV_PIX_FMT_VDPAU_MPEG1 || avctx->pix_fmt == AV_PIX_FMT_VDPAU_MPEG2;
1184 }
1185 #endif
1186 
1188 {
1189  Mpeg1Context *s1 = avctx->priv_data;
1190  MpegEncContext *s = &s1->mpeg_enc_ctx;
1191  const enum AVPixelFormat *pix_fmts;
1192 
1193  if (CONFIG_GRAY && (avctx->flags & AV_CODEC_FLAG_GRAY))
1194  return AV_PIX_FMT_GRAY8;
1195 
1196  if (s->chroma_format < 2)
1197  pix_fmts = avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO ?
1200  else if (s->chroma_format == 2)
1201  pix_fmts = mpeg12_pixfmt_list_422;
1202  else
1203  pix_fmts = mpeg12_pixfmt_list_444;
1204 
1205  return ff_thread_get_format(avctx, pix_fmts);
1206 }
1207 
1209 {
1210  // until then pix_fmt may be changed right after codec init
1211  if (avctx->hwaccel
1212 #if FF_API_VDPAU
1213  || uses_vdpau(avctx)
1214 #endif
1215  )
1216  if (avctx->idct_algo == FF_IDCT_AUTO)
1217  avctx->idct_algo = FF_IDCT_NONE;
1218 
1219  if (avctx->hwaccel && avctx->pix_fmt == AV_PIX_FMT_XVMC) {
1220  Mpeg1Context *s1 = avctx->priv_data;
1221  MpegEncContext *s = &s1->mpeg_enc_ctx;
1222 
1223  s->pack_pblocks = 1;
1224 #if FF_API_XVMC
1226  avctx->xvmc_acceleration = 2;
1228 #endif /* FF_API_XVMC */
1229  }
1230 }
1231 
1232 /* Call this function when we know all parameters.
1233  * It may be called in different places for MPEG-1 and MPEG-2. */
1235 {
1236  Mpeg1Context *s1 = avctx->priv_data;
1237  MpegEncContext *s = &s1->mpeg_enc_ctx;
1238  uint8_t old_permutation[64];
1239  int ret;
1240 
1241  if (avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
1242  // MPEG-1 aspect
1243  AVRational aspect_inv = av_d2q(ff_mpeg1_aspect[s->aspect_ratio_info], 255);
1244  avctx->sample_aspect_ratio = (AVRational) { aspect_inv.den, aspect_inv.num };
1245  } else { // MPEG-2
1246  // MPEG-2 aspect
1247  if (s->aspect_ratio_info > 1) {
1248  AVRational dar =
1250  (AVRational) { s1->pan_scan.width,
1251  s1->pan_scan.height }),
1252  (AVRational) { s->width, s->height });
1253 
1254  /* We ignore the spec here and guess a bit as reality does not
1255  * match the spec, see for example res_change_ffmpeg_aspect.ts
1256  * and sequence-display-aspect.mpg.
1257  * issue1613, 621, 562 */
1258  if ((s1->pan_scan.width == 0) || (s1->pan_scan.height == 0) ||
1259  (av_cmp_q(dar, (AVRational) { 4, 3 }) &&
1260  av_cmp_q(dar, (AVRational) { 16, 9 }))) {
1263  (AVRational) { s->width, s->height });
1264  } else {
1267  (AVRational) { s1->pan_scan.width, s1->pan_scan.height });
1268 // issue1613 4/3 16/9 -> 16/9
1269 // res_change_ffmpeg_aspect.ts 4/3 225/44 ->4/3
1270 // widescreen-issue562.mpg 4/3 16/9 -> 16/9
1271 // s->avctx->sample_aspect_ratio = av_mul_q(s->avctx->sample_aspect_ratio, (AVRational) {s->width, s->height});
1272  ff_dlog(avctx, "aspect A %d/%d\n",
1275  ff_dlog(avctx, "aspect B %d/%d\n", s->avctx->sample_aspect_ratio.num,
1277  }
1278  } else {
1281  }
1282  } // MPEG-2
1283 
1284  if (av_image_check_sar(s->width, s->height,
1285  avctx->sample_aspect_ratio) < 0) {
1286  av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
1287  avctx->sample_aspect_ratio.num,
1288  avctx->sample_aspect_ratio.den);
1289  avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
1290  }
1291 
1292  if ((s1->mpeg_enc_ctx_allocated == 0) ||
1293  avctx->coded_width != s->width ||
1294  avctx->coded_height != s->height ||
1295  s1->save_width != s->width ||
1296  s1->save_height != s->height ||
1298  (s1->save_progressive_seq != s->progressive_sequence && FFALIGN(s->height, 16) != FFALIGN(s->height, 32)) ||
1299  0) {
1300  if (s1->mpeg_enc_ctx_allocated) {
1301  ParseContext pc = s->parse_context;
1302  s->parse_context.buffer = 0;
1303  ff_mpv_common_end(s);
1304  s->parse_context = pc;
1305  s1->mpeg_enc_ctx_allocated = 0;
1306  }
1307 
1308  ret = ff_set_dimensions(avctx, s->width, s->height);
1309  if (ret < 0)
1310  return ret;
1311 
1312  if (avctx->codec_id == AV_CODEC_ID_MPEG2VIDEO && s->bit_rate) {
1313  avctx->rc_max_rate = s->bit_rate;
1314  } else if (avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO && s->bit_rate &&
1315  (s->bit_rate != 0x3FFFF*400 || s->vbv_delay != 0xFFFF)) {
1316  avctx->bit_rate = s->bit_rate;
1317  }
1319  s1->save_width = s->width;
1320  s1->save_height = s->height;
1322 
1323  /* low_delay may be forced, in this case we will have B-frames
1324  * that behave like P-frames. */
1325  avctx->has_b_frames = !s->low_delay;
1326 
1327  if (avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
1328  // MPEG-1 fps
1330  avctx->ticks_per_frame = 1;
1331 
1333  } else { // MPEG-2
1334  // MPEG-2 fps
1336  &s->avctx->framerate.den,
1339  1 << 30);
1340  avctx->ticks_per_frame = 2;
1341 
1342  switch (s->chroma_format) {
1343  case 1: avctx->chroma_sample_location = AVCHROMA_LOC_LEFT; break;
1344  case 2:
1345  case 3: avctx->chroma_sample_location = AVCHROMA_LOC_TOPLEFT; break;
1346  default: av_assert0(0);
1347  }
1348  } // MPEG-2
1349 
1350  avctx->pix_fmt = mpeg_get_pixelformat(avctx);
1351  setup_hwaccel_for_pixfmt(avctx);
1352 
1353  /* Quantization matrices may need reordering
1354  * if DCT permutation is changed. */
1355  memcpy(old_permutation, s->idsp.idct_permutation, 64 * sizeof(uint8_t));
1356 
1357  ff_mpv_idct_init(s);
1358  if ((ret = ff_mpv_common_init(s)) < 0)
1359  return ret;
1360 
1361  quant_matrix_rebuild(s->intra_matrix, old_permutation, s->idsp.idct_permutation);
1362  quant_matrix_rebuild(s->inter_matrix, old_permutation, s->idsp.idct_permutation);
1365 
1366  s1->mpeg_enc_ctx_allocated = 1;
1367  }
1368  return 0;
1369 }
1370 
1372  int buf_size)
1373 {
1374  Mpeg1Context *s1 = avctx->priv_data;
1375  MpegEncContext *s = &s1->mpeg_enc_ctx;
1376  int ref, f_code, vbv_delay;
1377 
1378  init_get_bits(&s->gb, buf, buf_size * 8);
1379 
1380  ref = get_bits(&s->gb, 10); /* temporal ref */
1381  s->pict_type = get_bits(&s->gb, 3);
1382  if (s->pict_type == 0 || s->pict_type > 3)
1383  return AVERROR_INVALIDDATA;
1384 
1385  vbv_delay = get_bits(&s->gb, 16);
1386  s->vbv_delay = vbv_delay;
1387  if (s->pict_type == AV_PICTURE_TYPE_P ||
1388  s->pict_type == AV_PICTURE_TYPE_B) {
1389  s->full_pel[0] = get_bits1(&s->gb);
1390  f_code = get_bits(&s->gb, 3);
1391  if (f_code == 0 && (avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT)))
1392  return AVERROR_INVALIDDATA;
1393  f_code += !f_code;
1394  s->mpeg_f_code[0][0] = f_code;
1395  s->mpeg_f_code[0][1] = f_code;
1396  }
1397  if (s->pict_type == AV_PICTURE_TYPE_B) {
1398  s->full_pel[1] = get_bits1(&s->gb);
1399  f_code = get_bits(&s->gb, 3);
1400  if (f_code == 0 && (avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT)))
1401  return AVERROR_INVALIDDATA;
1402  f_code += !f_code;
1403  s->mpeg_f_code[1][0] = f_code;
1404  s->mpeg_f_code[1][1] = f_code;
1405  }
1408 
1409  if (avctx->debug & FF_DEBUG_PICT_INFO)
1410  av_log(avctx, AV_LOG_DEBUG,
1411  "vbv_delay %d, ref %d type:%d\n", vbv_delay, ref, s->pict_type);
1412 
1413  s->y_dc_scale = 8;
1414  s->c_dc_scale = 8;
1415  return 0;
1416 }
1417 
1419 {
1420  MpegEncContext *s = &s1->mpeg_enc_ctx;
1421  int horiz_size_ext, vert_size_ext;
1422  int bit_rate_ext;
1423 
1424  skip_bits(&s->gb, 1); /* profile and level esc*/
1425  s->avctx->profile = get_bits(&s->gb, 3);
1426  s->avctx->level = get_bits(&s->gb, 4);
1427  s->progressive_sequence = get_bits1(&s->gb); /* progressive_sequence */
1428  s->chroma_format = get_bits(&s->gb, 2); /* chroma_format 1=420, 2=422, 3=444 */
1429 
1430  if (!s->chroma_format) {
1431  s->chroma_format = 1;
1432  av_log(s->avctx, AV_LOG_WARNING, "Chroma format invalid\n");
1433  }
1434 
1435  horiz_size_ext = get_bits(&s->gb, 2);
1436  vert_size_ext = get_bits(&s->gb, 2);
1437  s->width |= (horiz_size_ext << 12);
1438  s->height |= (vert_size_ext << 12);
1439  bit_rate_ext = get_bits(&s->gb, 12); /* XXX: handle it */
1440  s->bit_rate += (bit_rate_ext << 18) * 400LL;
1441  check_marker(s->avctx, &s->gb, "after bit rate extension");
1442  s->avctx->rc_buffer_size += get_bits(&s->gb, 8) * 1024 * 16 << 10;
1443 
1444  s->low_delay = get_bits1(&s->gb);
1446  s->low_delay = 1;
1447 
1448  s1->frame_rate_ext.num = get_bits(&s->gb, 2) + 1;
1449  s1->frame_rate_ext.den = get_bits(&s->gb, 5) + 1;
1450 
1451  ff_dlog(s->avctx, "sequence extension\n");
1453 
1454  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1456  "profile: %d, level: %d ps: %d cf:%d vbv buffer: %d, bitrate:%"PRId64"\n",
1458  s->avctx->rc_buffer_size, s->bit_rate);
1459 }
1460 
1462 {
1463  MpegEncContext *s = &s1->mpeg_enc_ctx;
1464  int color_description, w, h;
1465 
1466  skip_bits(&s->gb, 3); /* video format */
1467  color_description = get_bits1(&s->gb);
1468  if (color_description) {
1469  s->avctx->color_primaries = get_bits(&s->gb, 8);
1470  s->avctx->color_trc = get_bits(&s->gb, 8);
1471  s->avctx->colorspace = get_bits(&s->gb, 8);
1472  }
1473  w = get_bits(&s->gb, 14);
1474  skip_bits(&s->gb, 1); // marker
1475  h = get_bits(&s->gb, 14);
1476  // remaining 3 bits are zero padding
1477 
1478  s1->pan_scan.width = 16 * w;
1479  s1->pan_scan.height = 16 * h;
1480 
1481  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1482  av_log(s->avctx, AV_LOG_DEBUG, "sde w:%d, h:%d\n", w, h);
1483 }
1484 
1486 {
1487  MpegEncContext *s = &s1->mpeg_enc_ctx;
1488  int i, nofco;
1489 
1490  nofco = 1;
1491  if (s->progressive_sequence) {
1492  if (s->repeat_first_field) {
1493  nofco++;
1494  if (s->top_field_first)
1495  nofco++;
1496  }
1497  } else {
1498  if (s->picture_structure == PICT_FRAME) {
1499  nofco++;
1500  if (s->repeat_first_field)
1501  nofco++;
1502  }
1503  }
1504  for (i = 0; i < nofco; i++) {
1505  s1->pan_scan.position[i][0] = get_sbits(&s->gb, 16);
1506  skip_bits(&s->gb, 1); // marker
1507  s1->pan_scan.position[i][1] = get_sbits(&s->gb, 16);
1508  skip_bits(&s->gb, 1); // marker
1509  }
1510 
1511  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1513  "pde (%"PRId16",%"PRId16") (%"PRId16",%"PRId16") (%"PRId16",%"PRId16")\n",
1514  s1->pan_scan.position[0][0], s1->pan_scan.position[0][1],
1515  s1->pan_scan.position[1][0], s1->pan_scan.position[1][1],
1516  s1->pan_scan.position[2][0], s1->pan_scan.position[2][1]);
1517 }
1518 
1519 static int load_matrix(MpegEncContext *s, uint16_t matrix0[64],
1520  uint16_t matrix1[64], int intra)
1521 {
1522  int i;
1523 
1524  for (i = 0; i < 64; i++) {
1525  int j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
1526  int v = get_bits(&s->gb, 8);
1527  if (v == 0) {
1528  av_log(s->avctx, AV_LOG_ERROR, "matrix damaged\n");
1529  return AVERROR_INVALIDDATA;
1530  }
1531  if (intra && i == 0 && v != 8) {
1532  av_log(s->avctx, AV_LOG_DEBUG, "intra matrix specifies invalid DC quantizer %d, ignoring\n", v);
1533  v = 8; // needed by pink.mpg / issue1046
1534  }
1535  matrix0[j] = v;
1536  if (matrix1)
1537  matrix1[j] = v;
1538  }
1539  return 0;
1540 }
1541 
1543 {
1544  ff_dlog(s->avctx, "matrix extension\n");
1545 
1546  if (get_bits1(&s->gb))
1548  if (get_bits1(&s->gb))
1550  if (get_bits1(&s->gb))
1552  if (get_bits1(&s->gb))
1554 }
1555 
1557 {
1558  MpegEncContext *s = &s1->mpeg_enc_ctx;
1559 
1560  s->full_pel[0] = s->full_pel[1] = 0;
1561  s->mpeg_f_code[0][0] = get_bits(&s->gb, 4);
1562  s->mpeg_f_code[0][1] = get_bits(&s->gb, 4);
1563  s->mpeg_f_code[1][0] = get_bits(&s->gb, 4);
1564  s->mpeg_f_code[1][1] = get_bits(&s->gb, 4);
1565  s->mpeg_f_code[0][0] += !s->mpeg_f_code[0][0];
1566  s->mpeg_f_code[0][1] += !s->mpeg_f_code[0][1];
1567  s->mpeg_f_code[1][0] += !s->mpeg_f_code[1][0];
1568  s->mpeg_f_code[1][1] += !s->mpeg_f_code[1][1];
1569  if (!s->pict_type && s1->mpeg_enc_ctx_allocated) {
1571  "Missing picture start code, guessing missing values\n");
1572  if (s->mpeg_f_code[1][0] == 15 && s->mpeg_f_code[1][1] == 15) {
1573  if (s->mpeg_f_code[0][0] == 15 && s->mpeg_f_code[0][1] == 15)
1575  else
1577  } else
1581  }
1582 
1583  s->intra_dc_precision = get_bits(&s->gb, 2);
1584  s->picture_structure = get_bits(&s->gb, 2);
1585  s->top_field_first = get_bits1(&s->gb);
1586  s->frame_pred_frame_dct = get_bits1(&s->gb);
1588  s->q_scale_type = get_bits1(&s->gb);
1589  s->intra_vlc_format = get_bits1(&s->gb);
1590  s->alternate_scan = get_bits1(&s->gb);
1591  s->repeat_first_field = get_bits1(&s->gb);
1592  s->chroma_420_type = get_bits1(&s->gb);
1593  s->progressive_frame = get_bits1(&s->gb);
1594 
1595  if (s->alternate_scan) {
1598  } else {
1601  }
1602 
1603  /* composite display not parsed */
1604  ff_dlog(s->avctx, "intra_dc_precision=%d\n", s->intra_dc_precision);
1605  ff_dlog(s->avctx, "picture_structure=%d\n", s->picture_structure);
1606  ff_dlog(s->avctx, "top field first=%d\n", s->top_field_first);
1607  ff_dlog(s->avctx, "repeat first field=%d\n", s->repeat_first_field);
1608  ff_dlog(s->avctx, "conceal=%d\n", s->concealment_motion_vectors);
1609  ff_dlog(s->avctx, "intra_vlc_format=%d\n", s->intra_vlc_format);
1610  ff_dlog(s->avctx, "alternate_scan=%d\n", s->alternate_scan);
1611  ff_dlog(s->avctx, "frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
1612  ff_dlog(s->avctx, "progressive_frame=%d\n", s->progressive_frame);
1613 }
1614 
1615 static int mpeg_field_start(MpegEncContext *s, const uint8_t *buf, int buf_size)
1616 {
1617  AVCodecContext *avctx = s->avctx;
1618  Mpeg1Context *s1 = (Mpeg1Context *) s;
1619  int ret;
1620 
1621  /* start frame decoding */
1622  if (s->first_field || s->picture_structure == PICT_FRAME) {
1624 
1625  if ((ret = ff_mpv_frame_start(s, avctx)) < 0)
1626  return ret;
1627 
1629 
1630  /* first check if we must repeat the frame */
1632  if (s->repeat_first_field) {
1633  if (s->progressive_sequence) {
1634  if (s->top_field_first)
1636  else
1638  } else if (s->progressive_frame) {
1640  }
1641  }
1642 
1645  sizeof(s1->pan_scan));
1646  if (!pan_scan)
1647  return AVERROR(ENOMEM);
1648  memcpy(pan_scan->data, &s1->pan_scan, sizeof(s1->pan_scan));
1649 
1650  if (s1->a53_caption) {
1653  s1->a53_caption_size);
1654  if (sd)
1655  memcpy(sd->data, s1->a53_caption, s1->a53_caption_size);
1656  av_freep(&s1->a53_caption);
1657  }
1658 
1659  if (s1->has_stereo3d) {
1661  if (!stereo)
1662  return AVERROR(ENOMEM);
1663 
1664  *stereo = s1->stereo3d;
1665  s1->has_stereo3d = 0;
1666  }
1667 
1668  if (s1->has_afd) {
1669  AVFrameSideData *sd =
1671  AV_FRAME_DATA_AFD, 1);
1672  if (!sd)
1673  return AVERROR(ENOMEM);
1674 
1675  *sd->data = s1->afd;
1676  s1->has_afd = 0;
1677  }
1678 
1680  ff_thread_finish_setup(avctx);
1681  } else { // second field
1682  int i;
1683 
1684  if (!s->current_picture_ptr) {
1685  av_log(s->avctx, AV_LOG_ERROR, "first field missing\n");
1686  return AVERROR_INVALIDDATA;
1687  }
1688 
1689  if (s->avctx->hwaccel &&
1691  if ((ret = s->avctx->hwaccel->end_frame(s->avctx)) < 0) {
1692  av_log(avctx, AV_LOG_ERROR,
1693  "hardware accelerator failed to decode first field\n");
1694  return ret;
1695  }
1696  }
1697 
1698  for (i = 0; i < 4; i++) {
1699  s->current_picture.f->data[i] = s->current_picture_ptr->f->data[i];
1701  s->current_picture.f->data[i] +=
1702  s->current_picture_ptr->f->linesize[i];
1703  }
1704  }
1705 
1706  if (avctx->hwaccel) {
1707  if ((ret = avctx->hwaccel->start_frame(avctx, buf, buf_size)) < 0)
1708  return ret;
1709  }
1710 
1711  return 0;
1712 }
1713 
1714 #define DECODE_SLICE_ERROR -1
1715 #define DECODE_SLICE_OK 0
1716 
1717 /**
1718  * Decode a slice.
1719  * MpegEncContext.mb_y must be set to the MB row from the startcode.
1720  * @return DECODE_SLICE_ERROR if the slice is damaged,
1721  * DECODE_SLICE_OK if this slice is OK
1722  */
1723 static int mpeg_decode_slice(MpegEncContext *s, int mb_y,
1724  const uint8_t **buf, int buf_size)
1725 {
1726  AVCodecContext *avctx = s->avctx;
1727  const int lowres = s->avctx->lowres;
1728  const int field_pic = s->picture_structure != PICT_FRAME;
1729  int ret;
1730 
1731  s->resync_mb_x =
1732  s->resync_mb_y = -1;
1733 
1734  av_assert0(mb_y < s->mb_height);
1735 
1736  init_get_bits(&s->gb, *buf, buf_size * 8);
1737  if (s->codec_id != AV_CODEC_ID_MPEG1VIDEO && s->mb_height > 2800/16)
1738  skip_bits(&s->gb, 3);
1739 
1741  s->interlaced_dct = 0;
1742 
1743  s->qscale = get_qscale(s);
1744 
1745  if (s->qscale == 0) {
1746  av_log(s->avctx, AV_LOG_ERROR, "qscale == 0\n");
1747  return AVERROR_INVALIDDATA;
1748  }
1749 
1750  /* extra slice info */
1751  if (skip_1stop_8data_bits(&s->gb) < 0)
1752  return AVERROR_INVALIDDATA;
1753 
1754  s->mb_x = 0;
1755 
1756  if (mb_y == 0 && s->codec_tag == AV_RL32("SLIF")) {
1757  skip_bits1(&s->gb);
1758  } else {
1759  while (get_bits_left(&s->gb) > 0) {
1760  int code = get_vlc2(&s->gb, ff_mbincr_vlc.table,
1761  MBINCR_VLC_BITS, 2);
1762  if (code < 0) {
1763  av_log(s->avctx, AV_LOG_ERROR, "first mb_incr damaged\n");
1764  return AVERROR_INVALIDDATA;
1765  }
1766  if (code >= 33) {
1767  if (code == 33)
1768  s->mb_x += 33;
1769  /* otherwise, stuffing, nothing to do */
1770  } else {
1771  s->mb_x += code;
1772  break;
1773  }
1774  }
1775  }
1776 
1777  if (s->mb_x >= (unsigned) s->mb_width) {
1778  av_log(s->avctx, AV_LOG_ERROR, "initial skip overflow\n");
1779  return AVERROR_INVALIDDATA;
1780  }
1781 
1782  if (avctx->hwaccel && avctx->hwaccel->decode_slice) {
1783  const uint8_t *buf_end, *buf_start = *buf - 4; /* include start_code */
1784  int start_code = -1;
1785  buf_end = avpriv_find_start_code(buf_start + 2, *buf + buf_size, &start_code);
1786  if (buf_end < *buf + buf_size)
1787  buf_end -= 4;
1788  s->mb_y = mb_y;
1789  if (avctx->hwaccel->decode_slice(avctx, buf_start, buf_end - buf_start) < 0)
1790  return DECODE_SLICE_ERROR;
1791  *buf = buf_end;
1792  return DECODE_SLICE_OK;
1793  }
1794 
1795  s->resync_mb_x = s->mb_x;
1796  s->resync_mb_y = s->mb_y = mb_y;
1797  s->mb_skip_run = 0;
1799 
1800  if (s->mb_y == 0 && s->mb_x == 0 && (s->first_field || s->picture_structure == PICT_FRAME)) {
1801  if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
1803  "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",
1804  s->qscale,
1805  s->mpeg_f_code[0][0], s->mpeg_f_code[0][1],
1806  s->mpeg_f_code[1][0], s->mpeg_f_code[1][1],
1807  s->pict_type == AV_PICTURE_TYPE_I ? "I" :
1808  (s->pict_type == AV_PICTURE_TYPE_P ? "P" :
1809  (s->pict_type == AV_PICTURE_TYPE_B ? "B" : "S")),
1810  s->progressive_sequence ? "ps" : "",
1811  s->progressive_frame ? "pf" : "",
1812  s->alternate_scan ? "alt" : "",
1813  s->top_field_first ? "top" : "",
1817  s->repeat_first_field, s->chroma_420_type ? "420" : "");
1818  }
1819  }
1820 
1821  for (;;) {
1822  // If 1, we memcpy blocks in xvmcvideo.
1824  ff_xvmc_init_block(s); // set s->block
1825 
1826  if ((ret = mpeg_decode_mb(s, s->block)) < 0)
1827  return ret;
1828 
1829  // Note motion_val is normally NULL unless we want to extract the MVs.
1830  if (s->current_picture.motion_val[0] && !s->encoding) {
1831  const int wrap = s->b8_stride;
1832  int xy = s->mb_x * 2 + s->mb_y * 2 * wrap;
1833  int b8_xy = 4 * (s->mb_x + s->mb_y * s->mb_stride);
1834  int motion_x, motion_y, dir, i;
1835 
1836  for (i = 0; i < 2; i++) {
1837  for (dir = 0; dir < 2; dir++) {
1838  if (s->mb_intra ||
1839  (dir == 1 && s->pict_type != AV_PICTURE_TYPE_B)) {
1840  motion_x = motion_y = 0;
1841  } else if (s->mv_type == MV_TYPE_16X16 ||
1842  (s->mv_type == MV_TYPE_FIELD && field_pic)) {
1843  motion_x = s->mv[dir][0][0];
1844  motion_y = s->mv[dir][0][1];
1845  } else { /* if ((s->mv_type == MV_TYPE_FIELD) || (s->mv_type == MV_TYPE_16X8)) */
1846  motion_x = s->mv[dir][i][0];
1847  motion_y = s->mv[dir][i][1];
1848  }
1849 
1850  s->current_picture.motion_val[dir][xy][0] = motion_x;
1851  s->current_picture.motion_val[dir][xy][1] = motion_y;
1852  s->current_picture.motion_val[dir][xy + 1][0] = motion_x;
1853  s->current_picture.motion_val[dir][xy + 1][1] = motion_y;
1854  s->current_picture.ref_index [dir][b8_xy] =
1855  s->current_picture.ref_index [dir][b8_xy + 1] = s->field_select[dir][i];
1856  av_assert2(s->field_select[dir][i] == 0 ||
1857  s->field_select[dir][i] == 1);
1858  }
1859  xy += wrap;
1860  b8_xy += 2;
1861  }
1862  }
1863 
1864  s->dest[0] += 16 >> lowres;
1865  s->dest[1] +=(16 >> lowres) >> s->chroma_x_shift;
1866  s->dest[2] +=(16 >> lowres) >> s->chroma_x_shift;
1867 
1869 
1870  if (++s->mb_x >= s->mb_width) {
1871  const int mb_size = 16 >> s->avctx->lowres;
1872  int left;
1873 
1874  ff_mpeg_draw_horiz_band(s, mb_size * (s->mb_y >> field_pic), mb_size);
1876 
1877  s->mb_x = 0;
1878  s->mb_y += 1 << field_pic;
1879 
1880  if (s->mb_y >= s->mb_height) {
1881  int left = get_bits_left(&s->gb);
1882  int is_d10 = s->chroma_format == 2 &&
1883  s->pict_type == AV_PICTURE_TYPE_I &&
1884  avctx->profile == 0 && avctx->level == 5 &&
1885  s->intra_dc_precision == 2 &&
1886  s->q_scale_type == 1 && s->alternate_scan == 0 &&
1887  s->progressive_frame == 0
1888  /* vbv_delay == 0xBBB || 0xE10 */;
1889 
1890  if (left >= 32 && !is_d10) {
1891  GetBitContext gb = s->gb;
1892  align_get_bits(&gb);
1893  if (show_bits(&gb, 24) == 0x060E2B) {
1894  av_log(avctx, AV_LOG_DEBUG, "Invalid MXF data found in video stream\n");
1895  is_d10 = 1;
1896  }
1897  if (left > 32 && show_bits_long(&gb, 32) == 0x201) {
1898  av_log(avctx, AV_LOG_DEBUG, "skipping m704 alpha (unsupported)\n");
1899  goto eos;
1900  }
1901  }
1902 
1903  if (left < 0 ||
1904  (left && show_bits(&s->gb, FFMIN(left, 23)) && !is_d10) ||
1905  ((avctx->err_recognition & (AV_EF_BITSTREAM | AV_EF_AGGRESSIVE)) && left > 8)) {
1906  av_log(avctx, AV_LOG_ERROR, "end mismatch left=%d %0X at %d %d\n",
1907  left, left>0 ? show_bits(&s->gb, FFMIN(left, 23)) : 0, s->mb_x, s->mb_y);
1908  return AVERROR_INVALIDDATA;
1909  } else
1910  goto eos;
1911  }
1912  // There are some files out there which are missing the last slice
1913  // in cases where the slice is completely outside the visible
1914  // area, we detect this here instead of running into the end expecting
1915  // more data
1916  left = get_bits_left(&s->gb);
1917  if (s->mb_y >= ((s->height + 15) >> 4) &&
1918  !s->progressive_sequence &&
1919  left <= 25 &&
1920  left >= 0 &&
1921  s->mb_skip_run == -1 &&
1922  (!left || show_bits(&s->gb, left) == 0))
1923  goto eos;
1924 
1926  }
1927 
1928  /* skip mb handling */
1929  if (s->mb_skip_run == -1) {
1930  /* read increment again */
1931  s->mb_skip_run = 0;
1932  for (;;) {
1933  int code = get_vlc2(&s->gb, ff_mbincr_vlc.table,
1934  MBINCR_VLC_BITS, 2);
1935  if (code < 0) {
1936  av_log(s->avctx, AV_LOG_ERROR, "mb incr damaged\n");
1937  return AVERROR_INVALIDDATA;
1938  }
1939  if (code >= 33) {
1940  if (code == 33) {
1941  s->mb_skip_run += 33;
1942  } else if (code == 35) {
1943  if (s->mb_skip_run != 0 || show_bits(&s->gb, 15) != 0) {
1944  av_log(s->avctx, AV_LOG_ERROR, "slice mismatch\n");
1945  return AVERROR_INVALIDDATA;
1946  }
1947  goto eos; /* end of slice */
1948  }
1949  /* otherwise, stuffing, nothing to do */
1950  } else {
1951  s->mb_skip_run += code;
1952  break;
1953  }
1954  }
1955  if (s->mb_skip_run) {
1956  int i;
1957  if (s->pict_type == AV_PICTURE_TYPE_I) {
1959  "skipped MB in I-frame at %d %d\n", s->mb_x, s->mb_y);
1960  return AVERROR_INVALIDDATA;
1961  }
1962 
1963  /* skip mb */
1964  s->mb_intra = 0;
1965  for (i = 0; i < 12; i++)
1966  s->block_last_index[i] = -1;
1968  s->mv_type = MV_TYPE_16X16;
1969  else
1970  s->mv_type = MV_TYPE_FIELD;
1971  if (s->pict_type == AV_PICTURE_TYPE_P) {
1972  /* if P type, zero motion vector is implied */
1973  s->mv_dir = MV_DIR_FORWARD;
1974  s->mv[0][0][0] = s->mv[0][0][1] = 0;
1975  s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
1976  s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
1977  s->field_select[0][0] = (s->picture_structure - 1) & 1;
1978  } else {
1979  /* if B type, reuse previous vectors and directions */
1980  s->mv[0][0][0] = s->last_mv[0][0][0];
1981  s->mv[0][0][1] = s->last_mv[0][0][1];
1982  s->mv[1][0][0] = s->last_mv[1][0][0];
1983  s->mv[1][0][1] = s->last_mv[1][0][1];
1984  s->field_select[0][0] = (s->picture_structure - 1) & 1;
1985  s->field_select[1][0] = (s->picture_structure - 1) & 1;
1986  }
1987  }
1988  }
1989  }
1990 eos: // end of slice
1991  if (get_bits_left(&s->gb) < 0) {
1992  av_log(s, AV_LOG_ERROR, "overread %d\n", -get_bits_left(&s->gb));
1993  return AVERROR_INVALIDDATA;
1994  }
1995  *buf += (get_bits_count(&s->gb) - 1) / 8;
1996  ff_dlog(s, "Slice start:%d %d end:%d %d\n", s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y);
1997  return 0;
1998 }
1999 
2001 {
2002  MpegEncContext *s = *(void **) arg;
2003  const uint8_t *buf = s->gb.buffer;
2004  int mb_y = s->start_mb_y;
2005  const int field_pic = s->picture_structure != PICT_FRAME;
2006 
2007  s->er.error_count = (3 * (s->end_mb_y - s->start_mb_y) * s->mb_width) >> field_pic;
2008 
2009  for (;;) {
2010  uint32_t start_code;
2011  int ret;
2012 
2013  ret = mpeg_decode_slice(s, mb_y, &buf, s->gb.buffer_end - buf);
2014  emms_c();
2015  ff_dlog(c, "ret:%d resync:%d/%d mb:%d/%d ts:%d/%d ec:%d\n",
2016  ret, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y,
2017  s->start_mb_y, s->end_mb_y, s->er.error_count);
2018  if (ret < 0) {
2019  if (c->err_recognition & AV_EF_EXPLODE)
2020  return ret;
2021  if (s->resync_mb_x >= 0 && s->resync_mb_y >= 0)
2023  s->mb_x, s->mb_y,
2025  } else {
2027  s->mb_x - 1, s->mb_y,
2029  }
2030 
2031  if (s->mb_y == s->end_mb_y)
2032  return 0;
2033 
2034  start_code = -1;
2035  buf = avpriv_find_start_code(buf, s->gb.buffer_end, &start_code);
2036  mb_y = start_code - SLICE_MIN_START_CODE;
2037  if (s->codec_id != AV_CODEC_ID_MPEG1VIDEO && s->mb_height > 2800/16)
2038  mb_y += (*buf&0xE0)<<2;
2039  mb_y <<= field_pic;
2041  mb_y++;
2042  if (mb_y < 0 || mb_y >= s->end_mb_y)
2043  return AVERROR_INVALIDDATA;
2044  }
2045 }
2046 
2047 /**
2048  * Handle slice ends.
2049  * @return 1 if it seems to be the last slice
2050  */
2051 static int slice_end(AVCodecContext *avctx, AVFrame *pict)
2052 {
2053  Mpeg1Context *s1 = avctx->priv_data;
2054  MpegEncContext *s = &s1->mpeg_enc_ctx;
2055 
2057  return 0;
2058 
2059  if (s->avctx->hwaccel) {
2060  int ret = s->avctx->hwaccel->end_frame(s->avctx);
2061  if (ret < 0) {
2062  av_log(avctx, AV_LOG_ERROR,
2063  "hardware accelerator failed to decode picture\n");
2064  return ret;
2065  }
2066  }
2067 
2068  /* end of slice reached */
2069  if (/* s->mb_y << field_pic == s->mb_height && */ !s->first_field && !s1->first_slice) {
2070  /* end of image */
2071 
2072  ff_er_frame_end(&s->er);
2073 
2074  ff_mpv_frame_end(s);
2075 
2076  if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
2077  int ret = av_frame_ref(pict, s->current_picture_ptr->f);
2078  if (ret < 0)
2079  return ret;
2082  } else {
2083  if (avctx->active_thread_type & FF_THREAD_FRAME)
2084  s->picture_number++;
2085  /* latency of 1 frame for I- and P-frames */
2086  /* XXX: use another variable than picture_number */
2087  if (s->last_picture_ptr) {
2088  int ret = av_frame_ref(pict, s->last_picture_ptr->f);
2089  if (ret < 0)
2090  return ret;
2093  }
2094  }
2095 
2096  return 1;
2097  } else {
2098  return 0;
2099  }
2100 }
2101 
2103  const uint8_t *buf, int buf_size)
2104 {
2105  Mpeg1Context *s1 = avctx->priv_data;
2106  MpegEncContext *s = &s1->mpeg_enc_ctx;
2107  int width, height;
2108  int i, v, j;
2109 
2110  init_get_bits(&s->gb, buf, buf_size * 8);
2111 
2112  width = get_bits(&s->gb, 12);
2113  height = get_bits(&s->gb, 12);
2114  if (width == 0 || height == 0) {
2115  av_log(avctx, AV_LOG_WARNING,
2116  "Invalid horizontal or vertical size value.\n");
2118  return AVERROR_INVALIDDATA;
2119  }
2120  s->aspect_ratio_info = get_bits(&s->gb, 4);
2121  if (s->aspect_ratio_info == 0) {
2122  av_log(avctx, AV_LOG_ERROR, "aspect ratio has forbidden 0 value\n");
2124  return AVERROR_INVALIDDATA;
2125  }
2126  s->frame_rate_index = get_bits(&s->gb, 4);
2127  if (s->frame_rate_index == 0 || s->frame_rate_index > 13) {
2128  av_log(avctx, AV_LOG_WARNING,
2129  "frame_rate_index %d is invalid\n", s->frame_rate_index);
2130  s->frame_rate_index = 1;
2131  }
2132  s->bit_rate = get_bits(&s->gb, 18) * 400LL;
2133  if (check_marker(s->avctx, &s->gb, "in sequence header") == 0) {
2134  return AVERROR_INVALIDDATA;
2135  }
2136 
2137  s->avctx->rc_buffer_size = get_bits(&s->gb, 10) * 1024 * 16;
2138  skip_bits(&s->gb, 1);
2139 
2140  /* get matrix */
2141  if (get_bits1(&s->gb)) {
2143  } else {
2144  for (i = 0; i < 64; i++) {
2145  j = s->idsp.idct_permutation[i];
2147  s->intra_matrix[j] = v;
2148  s->chroma_intra_matrix[j] = v;
2149  }
2150  }
2151  if (get_bits1(&s->gb)) {
2153  } else {
2154  for (i = 0; i < 64; i++) {
2155  int j = s->idsp.idct_permutation[i];
2157  s->inter_matrix[j] = v;
2158  s->chroma_inter_matrix[j] = v;
2159  }
2160  }
2161 
2162  if (show_bits(&s->gb, 23) != 0) {
2163  av_log(s->avctx, AV_LOG_ERROR, "sequence header damaged\n");
2164  return AVERROR_INVALIDDATA;
2165  }
2166 
2167  s->width = width;
2168  s->height = height;
2169 
2170  /* We set MPEG-2 parameters so that it emulates MPEG-1. */
2171  s->progressive_sequence = 1;
2172  s->progressive_frame = 1;
2174  s->first_field = 0;
2175  s->frame_pred_frame_dct = 1;
2176  s->chroma_format = 1;
2177  s->codec_id =
2179  s->out_format = FMT_MPEG1;
2180  s->swap_uv = 0; // AFAIK VCR2 does not have SEQ_HEADER
2182  s->low_delay = 1;
2183 
2184  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
2185  av_log(s->avctx, AV_LOG_DEBUG, "vbv buffer: %d, bitrate:%"PRId64", aspect_ratio_info: %d \n",
2187 
2188  return 0;
2189 }
2190 
2192 {
2193  Mpeg1Context *s1 = avctx->priv_data;
2194  MpegEncContext *s = &s1->mpeg_enc_ctx;
2195  int i, v, ret;
2196 
2197  /* start new MPEG-1 context decoding */
2198  s->out_format = FMT_MPEG1;
2199  if (s1->mpeg_enc_ctx_allocated) {
2200  ff_mpv_common_end(s);
2201  s1->mpeg_enc_ctx_allocated = 0;
2202  }
2203  s->width = avctx->coded_width;
2204  s->height = avctx->coded_height;
2205  avctx->has_b_frames = 0; // true?
2206  s->low_delay = 1;
2207 
2208  avctx->pix_fmt = mpeg_get_pixelformat(avctx);
2209  setup_hwaccel_for_pixfmt(avctx);
2210 
2211  ff_mpv_idct_init(s);
2212  if ((ret = ff_mpv_common_init(s)) < 0)
2213  return ret;
2214  s1->mpeg_enc_ctx_allocated = 1;
2215 
2216  for (i = 0; i < 64; i++) {
2217  int j = s->idsp.idct_permutation[i];
2219  s->intra_matrix[j] = v;
2220  s->chroma_intra_matrix[j] = v;
2221 
2223  s->inter_matrix[j] = v;
2224  s->chroma_inter_matrix[j] = v;
2225  }
2226 
2227  s->progressive_sequence = 1;
2228  s->progressive_frame = 1;
2230  s->first_field = 0;
2231  s->frame_pred_frame_dct = 1;
2232  s->chroma_format = 1;
2233  if (s->codec_tag == AV_RL32("BW10")) {
2235  } else {
2236  s->swap_uv = 1; // in case of xvmc we need to swap uv for each MB
2238  }
2239  s1->save_width = s->width;
2240  s1->save_height = s->height;
2242  return 0;
2243 }
2244 
2246  const uint8_t *p, int buf_size)
2247 {
2248  Mpeg1Context *s1 = avctx->priv_data;
2249 
2250  if (buf_size >= 6 &&
2251  p[0] == 'G' && p[1] == 'A' && p[2] == '9' && p[3] == '4' &&
2252  p[4] == 3 && (p[5] & 0x40)) {
2253  /* extract A53 Part 4 CC data */
2254  int cc_count = p[5] & 0x1f;
2255  if (cc_count > 0 && buf_size >= 7 + cc_count * 3) {
2256  av_freep(&s1->a53_caption);
2257  s1->a53_caption_size = cc_count * 3;
2259  if (s1->a53_caption)
2260  memcpy(s1->a53_caption, p + 7, s1->a53_caption_size);
2262  }
2263  return 1;
2264  } else if (buf_size >= 11 &&
2265  p[0] == 'C' && p[1] == 'C' && p[2] == 0x01 && p[3] == 0xf8) {
2266  /* extract DVD CC data
2267  *
2268  * uint32_t user_data_start_code 0x000001B2 (big endian)
2269  * uint16_t user_identifier 0x4343 "CC"
2270  * uint8_t user_data_type_code 0x01
2271  * uint8_t caption_block_size 0xF8
2272  * uint8_t
2273  * bit 7 caption_odd_field_first 1=odd field (CC1/CC2) first 0=even field (CC3/CC4) first
2274  * bit 6 caption_filler 0
2275  * bit 5:1 caption_block_count number of caption blocks (pairs of caption words = frames). Most DVDs use 15 per start of GOP.
2276  * bit 0 caption_extra_field_added 1=one additional caption word
2277  *
2278  * struct caption_field_block {
2279  * uint8_t
2280  * bit 7:1 caption_filler 0x7F (all 1s)
2281  * bit 0 caption_field_odd 1=odd field (this is CC1/CC2) 0=even field (this is CC3/CC4)
2282  * uint8_t caption_first_byte
2283  * uint8_t caption_second_byte
2284  * } caption_block[(caption_block_count * 2) + caption_extra_field_added];
2285  *
2286  * Some DVDs encode caption data for both fields with caption_field_odd=1. The only way to decode the fields
2287  * correctly is to start on the field indicated by caption_odd_field_first and count between odd/even fields.
2288  * Don't assume that the first caption word is the odd field. There do exist MPEG files in the wild that start
2289  * on the even field. There also exist DVDs in the wild that encode an odd field count and the
2290  * caption_extra_field_added/caption_odd_field_first bits change per packet to allow that. */
2291  int cc_count = 0;
2292  int i;
2293  // There is a caption count field in the data, but it is often
2294  // incorrect. So count the number of captions present.
2295  for (i = 5; i + 6 <= buf_size && ((p[i] & 0xfe) == 0xfe); i += 6)
2296  cc_count++;
2297  // Transform the DVD format into A53 Part 4 format
2298  if (cc_count > 0) {
2299  av_freep(&s1->a53_caption);
2300  s1->a53_caption_size = cc_count * 6;
2302  if (s1->a53_caption) {
2303  uint8_t field1 = !!(p[4] & 0x80);
2304  uint8_t *cap = s1->a53_caption;
2305  p += 5;
2306  for (i = 0; i < cc_count; i++) {
2307  cap[0] = (p[0] == 0xff && field1) ? 0xfc : 0xfd;
2308  cap[1] = p[1];
2309  cap[2] = p[2];
2310  cap[3] = (p[3] == 0xff && !field1) ? 0xfc : 0xfd;
2311  cap[4] = p[4];
2312  cap[5] = p[5];
2313  cap += 6;
2314  p += 6;
2315  }
2316  }
2318  }
2319  return 1;
2320  }
2321  return 0;
2322 }
2323 
2325  const uint8_t *p, int buf_size)
2326 {
2327  Mpeg1Context *s = avctx->priv_data;
2328  const uint8_t *buf_end = p + buf_size;
2329  Mpeg1Context *s1 = avctx->priv_data;
2330 
2331 #if 0
2332  int i;
2333  for(i=0; !(!p[i-2] && !p[i-1] && p[i]==1) && i<buf_size; i++){
2334  av_log(avctx, AV_LOG_ERROR, "%c", p[i]);
2335  }
2336  av_log(avctx, AV_LOG_ERROR, "\n");
2337 #endif
2338 
2339  if (buf_size > 29){
2340  int i;
2341  for(i=0; i<20; i++)
2342  if (!memcmp(p+i, "\0TMPGEXS\0", 9)){
2343  s->tmpgexs= 1;
2344  }
2345  }
2346  /* we parse the DTG active format information */
2347  if (buf_end - p >= 5 &&
2348  p[0] == 'D' && p[1] == 'T' && p[2] == 'G' && p[3] == '1') {
2349  int flags = p[4];
2350  p += 5;
2351  if (flags & 0x80) {
2352  /* skip event id */
2353  p += 2;
2354  }
2355  if (flags & 0x40) {
2356  if (buf_end - p < 1)
2357  return;
2358 #if FF_API_AFD
2360  avctx->dtg_active_format = p[0] & 0x0f;
2362 #endif /* FF_API_AFD */
2363  s1->has_afd = 1;
2364  s1->afd = p[0] & 0x0f;
2365  }
2366  } else if (buf_end - p >= 6 &&
2367  p[0] == 'J' && p[1] == 'P' && p[2] == '3' && p[3] == 'D' &&
2368  p[4] == 0x03) { // S3D_video_format_length
2369  // the 0x7F mask ignores the reserved_bit value
2370  const uint8_t S3D_video_format_type = p[5] & 0x7F;
2371 
2372  if (S3D_video_format_type == 0x03 ||
2373  S3D_video_format_type == 0x04 ||
2374  S3D_video_format_type == 0x08 ||
2375  S3D_video_format_type == 0x23) {
2376 
2377  s1->has_stereo3d = 1;
2378 
2379  switch (S3D_video_format_type) {
2380  case 0x03:
2382  break;
2383  case 0x04:
2385  break;
2386  case 0x08:
2388  break;
2389  case 0x23:
2391  break;
2392  }
2393  }
2394  } else if (mpeg_decode_a53_cc(avctx, p, buf_size)) {
2395  return;
2396  }
2397 }
2398 
2399 static void mpeg_decode_gop(AVCodecContext *avctx,
2400  const uint8_t *buf, int buf_size)
2401 {
2402  Mpeg1Context *s1 = avctx->priv_data;
2403  MpegEncContext *s = &s1->mpeg_enc_ctx;
2404  int broken_link;
2405  int64_t tc;
2406 
2407  init_get_bits(&s->gb, buf, buf_size * 8);
2408 
2409  tc = s-> timecode_frame_start = get_bits(&s->gb, 25);
2410 
2411 #if FF_API_PRIVATE_OPT
2413  avctx->timecode_frame_start = tc;
2415 #endif
2416 
2417  s->closed_gop = get_bits1(&s->gb);
2418  /* broken_link indicates that after editing the
2419  * reference frames of the first B-Frames after GOP I-Frame
2420  * are missing (open gop) */
2421  broken_link = get_bits1(&s->gb);
2422 
2423  if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
2424  char tcbuf[AV_TIMECODE_STR_SIZE];
2427  "GOP (%s) closed_gop=%d broken_link=%d\n",
2428  tcbuf, s->closed_gop, broken_link);
2429  }
2430 }
2431 
2432 static int decode_chunks(AVCodecContext *avctx, AVFrame *picture,
2433  int *got_output, const uint8_t *buf, int buf_size)
2434 {
2435  Mpeg1Context *s = avctx->priv_data;
2437  const uint8_t *buf_ptr = buf;
2438  const uint8_t *buf_end = buf + buf_size;
2439  int ret, input_size;
2440  int last_code = 0, skip_frame = 0;
2441  int picture_start_code_seen = 0;
2442 
2443  for (;;) {
2444  /* find next start code */
2445  uint32_t start_code = -1;
2446  buf_ptr = avpriv_find_start_code(buf_ptr, buf_end, &start_code);
2447  if (start_code > 0x1ff) {
2448  if (!skip_frame) {
2449  if (HAVE_THREADS &&
2450  (avctx->active_thread_type & FF_THREAD_SLICE) &&
2451  !avctx->hwaccel) {
2452  int i;
2453  av_assert0(avctx->thread_count > 1);
2454 
2455  avctx->execute(avctx, slice_decode_thread,
2456  &s2->thread_context[0], NULL,
2457  s->slice_count, sizeof(void *));
2458  for (i = 0; i < s->slice_count; i++)
2459  s2->er.error_count += s2->thread_context[i]->er.error_count;
2460  }
2461 
2462 #if FF_API_VDPAU
2464  && uses_vdpau(avctx))
2465  ff_vdpau_mpeg_picture_complete(s2, buf, buf_size, s->slice_count);
2466 #endif
2467 
2468  ret = slice_end(avctx, picture);
2469  if (ret < 0)
2470  return ret;
2471  else if (ret) {
2472  // FIXME: merge with the stuff in mpeg_decode_slice
2473  if (s2->last_picture_ptr || s2->low_delay || s2->pict_type == AV_PICTURE_TYPE_B)
2474  *got_output = 1;
2475  }
2476  }
2477  s2->pict_type = 0;
2478 
2479  if (avctx->err_recognition & AV_EF_EXPLODE && s2->er.error_count)
2480  return AVERROR_INVALIDDATA;
2481 
2482  return FFMAX(0, buf_ptr - buf - s2->parse_context.last_index);
2483  }
2484 
2485  input_size = buf_end - buf_ptr;
2486 
2487  if (avctx->debug & FF_DEBUG_STARTCODE)
2488  av_log(avctx, AV_LOG_DEBUG, "%3"PRIX32" at %"PTRDIFF_SPECIFIER" left %d\n",
2489  start_code, buf_ptr - buf, input_size);
2490 
2491  /* prepare data for next start code */
2492  switch (start_code) {
2493  case SEQ_START_CODE:
2494  if (last_code == 0) {
2495  mpeg1_decode_sequence(avctx, buf_ptr, input_size);
2496  if (buf != avctx->extradata)
2497  s->sync = 1;
2498  } else {
2499  av_log(avctx, AV_LOG_ERROR,
2500  "ignoring SEQ_START_CODE after %X\n", last_code);
2501  if (avctx->err_recognition & AV_EF_EXPLODE)
2502  return AVERROR_INVALIDDATA;
2503  }
2504  break;
2505 
2506  case PICTURE_START_CODE:
2507  if (picture_start_code_seen && s2->picture_structure == PICT_FRAME) {
2508  /* If it's a frame picture, there can't be more than one picture header.
2509  Yet, it does happen and we need to handle it. */
2510  av_log(avctx, AV_LOG_WARNING, "ignoring extra picture following a frame-picture\n");
2511  break;
2512  }
2513  picture_start_code_seen = 1;
2514 
2515  if (s2->width <= 0 || s2->height <= 0) {
2516  av_log(avctx, AV_LOG_ERROR, "Invalid frame dimensions %dx%d.\n",
2517  s2->width, s2->height);
2518  return AVERROR_INVALIDDATA;
2519  }
2520 
2521  if (s->tmpgexs){
2522  s2->intra_dc_precision= 3;
2523  s2->intra_matrix[0]= 1;
2524  }
2525  if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE) &&
2526  !avctx->hwaccel && s->slice_count) {
2527  int i;
2528 
2529  avctx->execute(avctx, slice_decode_thread,
2530  s2->thread_context, NULL,
2531  s->slice_count, sizeof(void *));
2532  for (i = 0; i < s->slice_count; i++)
2533  s2->er.error_count += s2->thread_context[i]->er.error_count;
2534  s->slice_count = 0;
2535  }
2536  if (last_code == 0 || last_code == SLICE_MIN_START_CODE) {
2537  ret = mpeg_decode_postinit(avctx);
2538  if (ret < 0) {
2539  av_log(avctx, AV_LOG_ERROR,
2540  "mpeg_decode_postinit() failure\n");
2541  return ret;
2542  }
2543 
2544  /* We have a complete image: we try to decompress it. */
2545  if (mpeg1_decode_picture(avctx, buf_ptr, input_size) < 0)
2546  s2->pict_type = 0;
2547  s->first_slice = 1;
2548  last_code = PICTURE_START_CODE;
2549  } else {
2550  av_log(avctx, AV_LOG_ERROR,
2551  "ignoring pic after %X\n", last_code);
2552  if (avctx->err_recognition & AV_EF_EXPLODE)
2553  return AVERROR_INVALIDDATA;
2554  }
2555  break;
2556  case EXT_START_CODE:
2557  init_get_bits(&s2->gb, buf_ptr, input_size * 8);
2558 
2559  switch (get_bits(&s2->gb, 4)) {
2560  case 0x1:
2561  if (last_code == 0) {
2563  } else {
2564  av_log(avctx, AV_LOG_ERROR,
2565  "ignoring seq ext after %X\n", last_code);
2566  if (avctx->err_recognition & AV_EF_EXPLODE)
2567  return AVERROR_INVALIDDATA;
2568  }
2569  break;
2570  case 0x2:
2572  break;
2573  case 0x3:
2575  break;
2576  case 0x7:
2578  break;
2579  case 0x8:
2580  if (last_code == PICTURE_START_CODE) {
2582  } else {
2583  av_log(avctx, AV_LOG_ERROR,
2584  "ignoring pic cod ext after %X\n", last_code);
2585  if (avctx->err_recognition & AV_EF_EXPLODE)
2586  return AVERROR_INVALIDDATA;
2587  }
2588  break;
2589  }
2590  break;
2591  case USER_START_CODE:
2592  mpeg_decode_user_data(avctx, buf_ptr, input_size);
2593  break;
2594  case GOP_START_CODE:
2595  if (last_code == 0) {
2596  s2->first_field = 0;
2597  mpeg_decode_gop(avctx, buf_ptr, input_size);
2598  s->sync = 1;
2599  } else {
2600  av_log(avctx, AV_LOG_ERROR,
2601  "ignoring GOP_START_CODE after %X\n", last_code);
2602  if (avctx->err_recognition & AV_EF_EXPLODE)
2603  return AVERROR_INVALIDDATA;
2604  }
2605  break;
2606  default:
2607  if (start_code >= SLICE_MIN_START_CODE &&
2608  start_code <= SLICE_MAX_START_CODE && last_code == PICTURE_START_CODE) {
2609  if (s2->progressive_sequence && !s2->progressive_frame) {
2610  s2->progressive_frame = 1;
2611  av_log(s2->avctx, AV_LOG_ERROR,
2612  "interlaced frame in progressive sequence, ignoring\n");
2613  }
2614 
2615  if (s2->picture_structure == 0 ||
2617  av_log(s2->avctx, AV_LOG_ERROR,
2618  "picture_structure %d invalid, ignoring\n",
2619  s2->picture_structure);
2621  }
2622 
2624  av_log(s2->avctx, AV_LOG_WARNING, "invalid frame_pred_frame_dct\n");
2625 
2626  if (s2->picture_structure == PICT_FRAME) {
2627  s2->first_field = 0;
2628  s2->v_edge_pos = 16 * s2->mb_height;
2629  } else {
2630  s2->first_field ^= 1;
2631  s2->v_edge_pos = 8 * s2->mb_height;
2632  memset(s2->mbskip_table, 0, s2->mb_stride * s2->mb_height);
2633  }
2634  }
2635  if (start_code >= SLICE_MIN_START_CODE &&
2636  start_code <= SLICE_MAX_START_CODE && last_code != 0) {
2637  const int field_pic = s2->picture_structure != PICT_FRAME;
2638  int mb_y = start_code - SLICE_MIN_START_CODE;
2639  last_code = SLICE_MIN_START_CODE;
2640  if (s2->codec_id != AV_CODEC_ID_MPEG1VIDEO && s2->mb_height > 2800/16)
2641  mb_y += (*buf_ptr&0xE0)<<2;
2642 
2643  mb_y <<= field_pic;
2645  mb_y++;
2646 
2647  if (buf_end - buf_ptr < 2) {
2648  av_log(s2->avctx, AV_LOG_ERROR, "slice too small\n");
2649  return AVERROR_INVALIDDATA;
2650  }
2651 
2652  if (mb_y >= s2->mb_height) {
2653  av_log(s2->avctx, AV_LOG_ERROR,
2654  "slice below image (%d >= %d)\n", mb_y, s2->mb_height);
2655  return AVERROR_INVALIDDATA;
2656  }
2657 
2658  if (!s2->last_picture_ptr) {
2659  /* Skip B-frames if we do not have reference frames and
2660  * GOP is not closed. */
2661  if (s2->pict_type == AV_PICTURE_TYPE_B) {
2662  if (!s2->closed_gop) {
2663  skip_frame = 1;
2664  av_log(s2->avctx, AV_LOG_DEBUG,
2665  "Skipping B slice due to open GOP\n");
2666  break;
2667  }
2668  }
2669  }
2671  s->sync = 1;
2672  if (!s2->next_picture_ptr) {
2673  /* Skip P-frames if we do not have a reference frame or
2674  * we have an invalid header. */
2675  if (s2->pict_type == AV_PICTURE_TYPE_P && !s->sync) {
2676  skip_frame = 1;
2677  av_log(s2->avctx, AV_LOG_DEBUG,
2678  "Skipping P slice due to !sync\n");
2679  break;
2680  }
2681  }
2682  if ((avctx->skip_frame >= AVDISCARD_NONREF &&
2683  s2->pict_type == AV_PICTURE_TYPE_B) ||
2684  (avctx->skip_frame >= AVDISCARD_NONKEY &&
2685  s2->pict_type != AV_PICTURE_TYPE_I) ||
2686  avctx->skip_frame >= AVDISCARD_ALL) {
2687  skip_frame = 1;
2688  break;
2689  }
2690 
2691  if (!s->mpeg_enc_ctx_allocated)
2692  break;
2693 
2694  if (s2->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
2695  if (mb_y < avctx->skip_top ||
2696  mb_y >= s2->mb_height - avctx->skip_bottom)
2697  break;
2698  }
2699 
2700  if (!s2->pict_type) {
2701  av_log(avctx, AV_LOG_ERROR, "Missing picture start code\n");
2702  if (avctx->err_recognition & AV_EF_EXPLODE)
2703  return AVERROR_INVALIDDATA;
2704  break;
2705  }
2706 
2707  if (s->first_slice) {
2708  skip_frame = 0;
2709  s->first_slice = 0;
2710  if ((ret = mpeg_field_start(s2, buf, buf_size)) < 0)
2711  return ret;
2712  }
2713  if (!s2->current_picture_ptr) {
2714  av_log(avctx, AV_LOG_ERROR,
2715  "current_picture not initialized\n");
2716  return AVERROR_INVALIDDATA;
2717  }
2718 
2719 #if FF_API_VDPAU
2720  if (uses_vdpau(avctx)) {
2721  s->slice_count++;
2722  break;
2723  }
2724 #endif
2725 
2726  if (HAVE_THREADS &&
2727  (avctx->active_thread_type & FF_THREAD_SLICE) &&
2728  !avctx->hwaccel) {
2729  int threshold = (s2->mb_height * s->slice_count +
2730  s2->slice_context_count / 2) /
2731  s2->slice_context_count;
2732  av_assert0(avctx->thread_count > 1);
2733  if (threshold <= mb_y) {
2734  MpegEncContext *thread_context = s2->thread_context[s->slice_count];
2735 
2736  thread_context->start_mb_y = mb_y;
2737  thread_context->end_mb_y = s2->mb_height;
2738  if (s->slice_count) {
2739  s2->thread_context[s->slice_count - 1]->end_mb_y = mb_y;
2740  ret = ff_update_duplicate_context(thread_context, s2);
2741  if (ret < 0)
2742  return ret;
2743  }
2744  init_get_bits(&thread_context->gb, buf_ptr, input_size * 8);
2745  s->slice_count++;
2746  }
2747  buf_ptr += 2; // FIXME add minimum number of bytes per slice
2748  } else {
2749  ret = mpeg_decode_slice(s2, mb_y, &buf_ptr, input_size);
2750  emms_c();
2751 
2752  if (ret < 0) {
2753  if (avctx->err_recognition & AV_EF_EXPLODE)
2754  return ret;
2755  if (s2->resync_mb_x >= 0 && s2->resync_mb_y >= 0)
2756  ff_er_add_slice(&s2->er, s2->resync_mb_x,
2757  s2->resync_mb_y, s2->mb_x, s2->mb_y,
2759  } else {
2760  ff_er_add_slice(&s2->er, s2->resync_mb_x,
2761  s2->resync_mb_y, s2->mb_x - 1, s2->mb_y,
2763  }
2764  }
2765  }
2766  break;
2767  }
2768  }
2769 }
2770 
2771 static int mpeg_decode_frame(AVCodecContext *avctx, void *data,
2772  int *got_output, AVPacket *avpkt)
2773 {
2774  const uint8_t *buf = avpkt->data;
2775  int ret;
2776  int buf_size = avpkt->size;
2777  Mpeg1Context *s = avctx->priv_data;
2778  AVFrame *picture = data;
2780 
2781  if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == SEQ_END_CODE)) {
2782  /* special case for last picture */
2783  if (s2->low_delay == 0 && s2->next_picture_ptr) {
2784  int ret = av_frame_ref(picture, s2->next_picture_ptr->f);
2785  if (ret < 0)
2786  return ret;
2787 
2788  s2->next_picture_ptr = NULL;
2789 
2790  *got_output = 1;
2791  }
2792  return buf_size;
2793  }
2794 
2795  if (s2->avctx->flags & AV_CODEC_FLAG_TRUNCATED) {
2796  int next = ff_mpeg1_find_frame_end(&s2->parse_context, buf,
2797  buf_size, NULL);
2798 
2799  if (ff_combine_frame(&s2->parse_context, next,
2800  (const uint8_t **) &buf, &buf_size) < 0)
2801  return buf_size;
2802  }
2803 
2804  s2->codec_tag = avpriv_toupper4(avctx->codec_tag);
2805  if (s->mpeg_enc_ctx_allocated == 0 && ( s2->codec_tag == AV_RL32("VCR2")
2806  || s2->codec_tag == AV_RL32("BW10")
2807  ))
2808  vcr2_init_sequence(avctx);
2809 
2810  s->slice_count = 0;
2811 
2812  if (avctx->extradata && !s->extradata_decoded) {
2813  ret = decode_chunks(avctx, picture, got_output,
2814  avctx->extradata, avctx->extradata_size);
2815  if (*got_output) {
2816  av_log(avctx, AV_LOG_ERROR, "picture in extradata\n");
2817  av_frame_unref(picture);
2818  *got_output = 0;
2819  }
2820  s->extradata_decoded = 1;
2821  if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE)) {
2822  s2->current_picture_ptr = NULL;
2823  return ret;
2824  }
2825  }
2826 
2827  ret = decode_chunks(avctx, picture, got_output, buf, buf_size);
2828  if (ret<0 || *got_output) {
2829  s2->current_picture_ptr = NULL;
2830 
2831  if (s2->timecode_frame_start != -1 && *got_output) {
2832  AVFrameSideData *tcside = av_frame_new_side_data(picture,
2834  sizeof(int64_t));
2835  if (!tcside)
2836  return AVERROR(ENOMEM);
2837  memcpy(tcside->data, &s2->timecode_frame_start, sizeof(int64_t));
2838 
2839  s2->timecode_frame_start = -1;
2840  }
2841  }
2842 
2843  return ret;
2844 }
2845 
2846 static void flush(AVCodecContext *avctx)
2847 {
2848  Mpeg1Context *s = avctx->priv_data;
2849 
2850  s->sync = 0;
2851 
2852  ff_mpeg_flush(avctx);
2853 }
2854 
2856 {
2857  Mpeg1Context *s = avctx->priv_data;
2858 
2859  if (s->mpeg_enc_ctx_allocated)
2861  av_freep(&s->a53_caption);
2862  return 0;
2863 }
2864 
2866  .name = "mpeg1video",
2867  .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video"),
2868  .type = AVMEDIA_TYPE_VIDEO,
2869  .id = AV_CODEC_ID_MPEG1VIDEO,
2870  .priv_data_size = sizeof(Mpeg1Context),
2872  .close = mpeg_decode_end,
2877  .caps_internal = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
2878  .flush = flush,
2879  .max_lowres = 3,
2880  .update_thread_context = ONLY_IF_THREADS_ENABLED(mpeg_decode_update_thread_context)
2881 };
2882 
2884  .name = "mpeg2video",
2885  .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 video"),
2886  .type = AVMEDIA_TYPE_VIDEO,
2887  .id = AV_CODEC_ID_MPEG2VIDEO,
2888  .priv_data_size = sizeof(Mpeg1Context),
2890  .close = mpeg_decode_end,
2895  .caps_internal = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
2896  .flush = flush,
2897  .max_lowres = 3,
2899 };
2900 
2901 //legacy decoder
2903  .name = "mpegvideo",
2904  .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video"),
2905  .type = AVMEDIA_TYPE_VIDEO,
2906  .id = AV_CODEC_ID_MPEG2VIDEO,
2907  .priv_data_size = sizeof(Mpeg1Context),
2909  .close = mpeg_decode_end,
2912  .caps_internal = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
2913  .flush = flush,
2914  .max_lowres = 3,
2915 };
2916 
2917 #if FF_API_XVMC
2918 #if CONFIG_MPEG_XVMC_DECODER
2920 static av_cold int mpeg_mc_decode_init(AVCodecContext *avctx)
2921 {
2922  if (avctx->active_thread_type & FF_THREAD_SLICE)
2923  return -1;
2924  if (!(avctx->slice_flags & SLICE_FLAG_CODED_ORDER))
2925  return -1;
2926  if (!(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD)) {
2927  ff_dlog(avctx, "mpeg12.c: XvMC decoder will work better if SLICE_FLAG_ALLOW_FIELD is set\n");
2928  }
2929  mpeg_decode_init(avctx);
2930 
2932  avctx->xvmc_acceleration = 2; // 2 - the blocks are packed!
2933 
2934  return 0;
2935 }
2936 
2937 AVCodec ff_mpeg_xvmc_decoder = {
2938  .name = "mpegvideo_xvmc",
2939  .long_name = NULL_IF_CONFIG_SMALL("MPEG-1/2 video XvMC (X-Video Motion Compensation)"),
2940  .type = AVMEDIA_TYPE_VIDEO,
2941  .id = AV_CODEC_ID_MPEG2VIDEO_XVMC,
2942  .priv_data_size = sizeof(Mpeg1Context),
2943  .init = mpeg_mc_decode_init,
2944  .close = mpeg_decode_end,
2947  AV_CODEC_CAP_TRUNCATED | CODEC_CAP_HWACCEL |
2949  .flush = flush,
2950 };
2952 #endif
2953 #endif /* FF_API_XVMC */
2954 
2955 #if CONFIG_MPEG_VDPAU_DECODER && FF_API_VDPAU
2956 AVCodec ff_mpeg_vdpau_decoder = {
2957  .name = "mpegvideo_vdpau",
2958  .long_name = NULL_IF_CONFIG_SMALL("MPEG-1/2 video (VDPAU acceleration)"),
2959  .type = AVMEDIA_TYPE_VIDEO,
2960  .id = AV_CODEC_ID_MPEG2VIDEO,
2961  .priv_data_size = sizeof(Mpeg1Context),
2963  .close = mpeg_decode_end,
2965  .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_TRUNCATED |
2967  .flush = flush,
2968 };
2969 #endif
2970 
2971 #if CONFIG_MPEG1_VDPAU_DECODER && FF_API_VDPAU
2972 AVCodec ff_mpeg1_vdpau_decoder = {
2973  .name = "mpeg1video_vdpau",
2974  .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video (VDPAU acceleration)"),
2975  .type = AVMEDIA_TYPE_VIDEO,
2976  .id = AV_CODEC_ID_MPEG1VIDEO,
2977  .priv_data_size = sizeof(Mpeg1Context),
2979  .close = mpeg_decode_end,
2981  .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_TRUNCATED |
2983  .flush = flush,
2984 };
2985 #endif
static const uint32_t btype2mb_type[11]
Definition: mpeg12dec.c:86
const uint16_t ff_mpeg1_default_non_intra_matrix[64]
Definition: mpeg12data.c:41
static unsigned int show_bits_long(GetBitContext *s, int n)
Show 0-32 bits.
Definition: get_bits.h:398
#define AV_EF_AGGRESSIVE
consider things that a sane encoder should not do as an error
Definition: avcodec.h:3066
#define ff_tlog(ctx,...)
Definition: internal.h:75
IDCTDSPContext idsp
Definition: mpegvideo.h:227
#define NULL
Definition: coverity.c:32
const struct AVCodec * codec
Definition: avcodec.h:1770
AVRational framerate
Definition: avcodec.h:3460
#define MB_TYPE_SKIP
Definition: avcodec.h:1307
const char const char void * val
Definition: avisynth_c.h:771
discard all frames except keyframes
Definition: avcodec.h:829
int8_t * ref_index[2]
Definition: mpegpicture.h:62
void ff_init_block_index(MpegEncContext *s)
Definition: mpegvideo.c:2746
int64_t timecode_frame_start
GOP timecode frame start number, in non drop frame format.
Definition: mpegvideo.h:460
int aspect_ratio_info
Definition: mpegvideo.h:400
int picture_number
Definition: mpegvideo.h:124
const char * s
Definition: avisynth_c.h:768
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define SLICE_MAX_START_CODE
Definition: cavs.h:32
#define FF_API_VDPAU
Definition: version.h:110
#define SLICE_FLAG_ALLOW_FIELD
allow draw_horiz_band() with field slices (MPEG-2 field pics)
Definition: avcodec.h:2306
static int shift(int a, int b)
Definition: sonic.c:82
AVRational av_div_q(AVRational b, AVRational c)
Divide one rational by another.
Definition: rational.c:88
MPEG-2/4 4:2:0, H.264 default for 4:2:0.
Definition: pixfmt.h:535
#define MAX_INDEX
Definition: mpeg12dec.c:127
This structure describes decoded (raw) audio or video data.
Definition: frame.h:201
static void quant_matrix_rebuild(uint16_t *matrix, const uint8_t *old_perm, const uint8_t *new_perm)
Definition: mpeg12dec.c:1118
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:150
#define MV_TYPE_FIELD
2 vectors, one per field
Definition: mpegvideo.h:269
int last_mv[2][2][2]
last MV, used for MV prediction in MPEG-1 & B-frame MPEG-4
Definition: mpegvideo.h:278
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:1963
float re
Definition: fft.c:82
static void mpeg_decode_sequence_display_extension(Mpeg1Context *s1)
Definition: mpeg12dec.c:1461
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:71
misc image utilities
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:262
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
av_cold void ff_mpeg12_init_vlcs(void)
Definition: mpeg12.c:137
MPEG-2 HW decoding with VDPAU, data[0] contains a vdpau_render_state struct which contains the bitstr...
Definition: pixfmt.h:110
int64_t bit_rate
the average bitrate
Definition: avcodec.h:1826
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:211
uint8_t afd
Definition: mpeg12dec.c:62
hardware decoding through Videotoolbox
Definition: pixfmt.h:300
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:151
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
uint8_t * a53_caption
Definition: mpeg12dec.c:60
uint16_t chroma_intra_matrix[64]
Definition: mpegvideo.h:301
int height
Definition: avcodec.h:1339
int v_edge_pos
horizontal / vertical position of the right/bottom edge (pixel replication)
Definition: mpegvideo.h:129
uint16_t chroma_inter_matrix[64]
Definition: mpegvideo.h:303
void ff_er_frame_end(ERContext *s)
static int mpeg2_fast_decode_block_non_intra(MpegEncContext *s, int16_t *block, int n)
Changing this would eat up any speed benefits it has.
Definition: mpeg12dec.c:401
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:2498
static int check_marker(void *logctx, GetBitContext *s, const char *msg)
Definition: get_bits.h:408
int num
Numerator.
Definition: rational.h:59
int repeat_pict
When decoding, this signals how much the picture must be delayed.
Definition: frame.h:343
Views are next to each other, but when upscaling apply a checkerboard pattern.
Definition: stereo3d.h:117
int size
Definition: avcodec.h:1680
enum AVCodecID codec_id
Definition: mpegvideo.h:109
#define AV_EF_COMPLIANT
consider all spec non compliances as errors
Definition: avcodec.h:3065
static void mpeg_decode_picture_coding_extension(Mpeg1Context *s1)
Definition: mpeg12dec.c:1556
const uint8_t * buffer
Definition: get_bits.h:57
void ff_mpeg1_clean_buffers(MpegEncContext *s)
Definition: mpeg12.c:115
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:2172
#define MB_TYPE_INTRA
Definition: mpegutils.h:75
AVCodec ff_mpeg1video_decoder
Definition: mpeg12dec.c:2865
char * av_timecode_make_mpeg_tc_string(char *buf, uint32_t tc25bit)
Get the timecode string from the 25-bit timecode format (MPEG GOP format).
Definition: timecode.c:130
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1989
#define tc
Definition: regdef.h:69
static av_cold int mpeg_decode_init(AVCodecContext *avctx)
Definition: mpeg12dec.c:1062
#define MB_TYPE_QUANT
Definition: avcodec.h:1315
#define AV_EF_BITSTREAM
detect bitstream specification deviations
Definition: avcodec.h:3059
mpegvideo header.
#define HAS_CBP(a)
Definition: mpegutils.h:103
discard all
Definition: avcodec.h:830
uint8_t permutated[64]
Definition: idctdsp.h:33
Views are next to each other.
Definition: stereo3d.h:67
uint8_t run
Definition: svq3.c:206
#define ER_MV_ERROR
static void mpeg_decode_gop(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
Definition: mpeg12dec.c:2399
#define SLICE_MIN_START_CODE
Definition: mpegvideo.h:70
int profile
profile
Definition: avcodec.h:3266
AVCodec.
Definition: avcodec.h:3739
static int get_dmv(MpegEncContext *s)
Definition: mpeg12dec.c:641
int qscale
QP.
Definition: mpegvideo.h:201
RLTable.
Definition: rl.h:39
static int get_sbits(GetBitContext *s, int n)
Definition: get_bits.h:247
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:42
#define CONFIG_MPEG1_XVMC_HWACCEL
Definition: config.h:2039
int chroma_x_shift
Definition: mpegvideo.h:476
int encoding
true if we are encoding (vs decoding)
Definition: mpegvideo.h:111
int field_select[2][2]
Definition: mpegvideo.h:277
Macro definitions for various function/variable attributes.
enum AVDiscard skip_frame
Skip decoding for selected frames.
Definition: avcodec.h:3386
const uint8_t ff_mpeg2_non_linear_qscale[32]
Definition: mpegvideodata.c:27
static int16_t block[64]
Definition: dct.c:115
struct AVHWAccel * hwaccel
Hardware accelerator in use.
Definition: avcodec.h:3082
#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:1027
#define USES_LIST(a, list)
Definition: mpegutils.h:101
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
void ff_mpeg_draw_horiz_band(MpegEncContext *s, int y, int h)
Definition: mpegvideo.c:2739
#define MBINCR_VLC_BITS
Definition: mpeg12vlc.h:37
static const uint32_t ptype2mb_type[7]
Definition: mpeg12dec.c:76
uint8_t
#define av_cold
Definition: attributes.h:82
#define av_malloc(s)
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:64
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:157
enum OutputFormat out_format
output format
Definition: mpegvideo.h:101
#define FF_DEBUG_PICT_INFO
Definition: avcodec.h:3004
#define AV_RB32
Definition: intreadwrite.h:130
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
const float ff_mpeg1_aspect[16]
Definition: mpeg12data.c:374
#define CONFIG_MPEG1_VDPAU_DECODER
Definition: config.h:812
Multithreading support functions.
#define AV_CODEC_FLAG_LOW_DELAY
Force low delay.
Definition: avcodec.h:921
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:395
#define emms_c()
Definition: internal.h:54
static int load_matrix(MpegEncContext *s, uint16_t matrix0[64], uint16_t matrix1[64], int intra)
Definition: mpeg12dec.c:1519
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1876
int full_pel[2]
Definition: mpegvideo.h:480
int interlaced_dct
Definition: mpegvideo.h:481
static enum AVPixelFormat mpeg_get_pixelformat(AVCodecContext *avctx)
Definition: mpeg12dec.c:1187
static int mpeg1_fast_decode_block_inter(MpegEncContext *s, int16_t *block, int n)
Changing this would eat up any speed benefits it has.
Definition: mpeg12dec.c:226
Picture current_picture
copy of the current picture structure.
Definition: mpegvideo.h:177
int first_slice
Definition: mpeg12dec.c:70
The data is the AVPanScan struct defined in libavcodec.
Definition: frame.h:52
int intra_dc_precision
Definition: mpegvideo.h:461
int repeat_first_field
Definition: mpegvideo.h:470
void ff_xvmc_init_block(MpegEncContext *s)
Initialize the block field of the MpegEncContext pointer passed as parameter after making sure that t...
const char data[16]
Definition: mxf.c:90
Structure to hold side data for an AVFrame.
Definition: frame.h:163
int av_image_check_sar(unsigned int w, unsigned int h, AVRational sar)
Check if the given sample aspect ratio of an image is valid.
Definition: imgutils.c:286
#define PICT_BOTTOM_FIELD
Definition: mpegutils.h:38
#define height
uint8_t * data
Definition: avcodec.h:1679
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:200
static int flags
Definition: log.c:57
void ff_xvmc_pack_pblocks(MpegEncContext *s, int cbp)
Fill individual block pointers, so there are no gaps in the data_block array in case not all blocks i...
#define AV_CODEC_CAP_HWACCEL_VDPAU
Codec can export data for HW decoding (VDPAU).
Definition: avcodec.h:1038
#define ER_MV_END
#define ff_dlog(a,...)
MPEG-1 HW decoding with VDPAU, data[0] contains a vdpau_render_state struct which contains the bitstr...
Definition: pixfmt.h:109
av_cold void ff_mpv_idct_init(MpegEncContext *s)
Definition: mpegvideo.c:330
int mb_height
number of MBs horizontally & vertically
Definition: mpegvideo.h:126
int lowres
low resolution decoding, 1-> 1/2 size, 2->1/4 size
Definition: avcodec.h:3172
Video is not stereoscopic (and metadata has to be there).
Definition: stereo3d.h:55
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
int codec_tag
internal codec_tag upper case converted from avctx codec_tag
Definition: mpegvideo.h:117
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...
#define AV_CODEC_FLAG_GRAY
Only decode/encode grayscale.
Definition: avcodec.h:904
Active Format Description data consisting of a single byte as specified in ETSI TS 101 154 using AVAc...
Definition: frame.h:89
VLC ff_mv_vlc
Definition: mpeg12.c:127
enum AVChromaLocation chroma_sample_location
This defines the location of chroma samples.
Definition: avcodec.h:2505
#define FFALIGN(x, a)
Definition: macros.h:48
#define av_log(a,...)
void ff_vdpau_mpeg_picture_complete(MpegEncContext *s, const uint8_t *buf, int buf_size, int slice_count)
const uint8_t * avpriv_find_start_code(const uint8_t *p, const uint8_t *end, uint32_t *state)
#define FF_IDCT_AUTO
Definition: avcodec.h:3122
static void setup_hwaccel_for_pixfmt(AVCodecContext *avctx)
Definition: mpeg12dec.c:1208
Libavcodec version macros.
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:589
enum AVCodecID id
Definition: avcodec.h:3753
int slice_context_count
number of used thread_contexts
Definition: mpegvideo.h:153
int extradata_decoded
Definition: mpeg12dec.c:71
#define UPDATE_CACHE(name, gb)
Definition: get_bits.h:161
#define FF_IDCT_NONE
Definition: avcodec.h:3149
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
int has_b_frames
Size of the frame reordering buffer in the decoder.
Definition: avcodec.h:2083
int last_dc[3]
last DC values for MPEG-1
Definition: mpegvideo.h:182
#define CONFIG_MPEG_VDPAU_DECODER
Definition: config.h:811
#define s2
Definition: regdef.h:39
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:660
#define PTRDIFF_SPECIFIER
Definition: internal.h:256
int mb_skipped
MUST BE SET only during DECODING.
Definition: mpegvideo.h:192
int chroma_y_shift
Definition: mpegvideo.h:477
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:251
#define AVERROR(e)
Definition: error.h:43
static void mpeg_decode_sequence_extension(Mpeg1Context *s1)
Definition: mpeg12dec.c:1418
void(* clear_blocks)(int16_t *blocks)
Definition: blockdsp.h:37
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:181
ERContext er
Definition: mpegvideo.h:551
static int mpeg1_decode_block_inter(MpegEncContext *s, int16_t *block, int n)
Definition: mpeg12dec.c:137
int active_thread_type
Which multithreading methods are in use by the codec.
Definition: avcodec.h:3211
AVCodec ff_mpegvideo_decoder
Definition: mpeg12dec.c:2902
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
ATSC A53 Part 4 Closed Captions.
Definition: frame.h:58
static int get_qscale(MpegEncContext *s)
Definition: mpeg12dec.c:649
const char * arg
Definition: jacosubdec.c:66
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:1856
#define wrap(func)
Definition: neontest.h:65
uint16_t width
Definition: gdv.c:47
#define SEQ_END_CODE
Definition: mpegvideo.h:66
#define PICT_TOP_FIELD
Definition: mpegutils.h:37
const char * name
Name of the codec implementation.
Definition: avcodec.h:3746
static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred)
Definition: mpeg12dec.c:101
int width
width and height in 1/16 pel
Definition: avcodec.h:1338
int low_delay
no reordering needed / has no B-frames
Definition: mpegvideo.h:404
static void mpeg_decode_user_data(AVCodecContext *avctx, const uint8_t *p, int buf_size)
Definition: mpeg12dec.c:2324
GetBitContext gb
Definition: mpegvideo.h:446
The GOP timecode in 25 bit timecode format.
Definition: frame.h:124
#define CLOSE_READER(name, gb)
Definition: get_bits.h:132
void ff_mpv_common_end(MpegEncContext *s)
Definition: mpegvideo.c:1122
VLC ff_mb_pat_vlc
Definition: mpeg12.c:135
#define FFMAX(a, b)
Definition: common.h:94
static int decode_dc(GetBitContext *gb, int component)
Definition: mpeg12.h:41
int repeat_field
Definition: mpeg12dec.c:56
attribute_deprecated int64_t timecode_frame_start
Definition: avcodec.h:2874
#define DECODE_SLICE_ERROR
Definition: mpeg12dec.c:1714
#define DECODE_SLICE_OK
Definition: mpeg12dec.c:1715
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:70
void ff_mpeg_flush(AVCodecContext *avctx)
Definition: mpegvideo.c:2778
#define SKIP_BITS(name, gb, num)
Definition: get_bits.h:176
#define ONLY_IF_THREADS_ENABLED(x)
Define a function with only the non-default version specified.
Definition: internal.h:220
int resync_mb_x
x position of last resync marker
Definition: mpegvideo.h:356
int rc_buffer_size
decoder bitstream buffer size
Definition: avcodec.h:2739
AVStereo3D * av_stereo3d_create_side_data(AVFrame *frame)
Allocate a complete AVFrameSideData and add it to the frame.
Definition: stereo3d.c:33
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:1615
int intra_vlc_format
Definition: mpegvideo.h:467
const AVProfile ff_mpeg2_video_profiles[]
Definition: profiles.c:95
#define AV_CODEC_CAP_DRAW_HORIZ_BAND
Decoder can use draw_horiz_band callback.
Definition: avcodec.h:996
static int mpeg2_decode_block_non_intra(MpegEncContext *s, int16_t *block, int n)
Definition: mpeg12dec.c:311
int ff_mpv_export_qp_table(MpegEncContext *s, AVFrame *f, Picture *p, int qp_type)
Definition: mpegvideo.c:1973
int progressive_frame
Definition: mpegvideo.h:479
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:284
#define MB_PAT_VLC_BITS
Definition: mpeg12vlc.h:38
int top_field_first
Definition: mpegvideo.h:463
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
Definition: avcodec.h:3050
#define FF_THREAD_FRAME
Decode more than one frame at once.
Definition: avcodec.h:3203
#define FFMIN(a, b)
Definition: common.h:96
int last_index
Definition: parser.h:31
static int vcr2_init_sequence(AVCodecContext *avctx)
Definition: mpeg12dec.c:2191
static const uint8_t start_code[]
Definition: h264dec.c:531
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:193
int idct_algo
IDCT algorithm, see FF_IDCT_* below.
Definition: avcodec.h:3121
#define MB_TYPE_INTERLACED
Definition: avcodec.h:1303
int16_t(*[2] motion_val)[2]
Definition: mpegpicture.h:53
Picture * current_picture_ptr
pointer to the current picture
Definition: mpegvideo.h:181
void ff_mpeg_er_frame_start(MpegEncContext *s)
Definition: mpeg_er.c:46
#define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update)
Definition: get_bits.h:510
#define ER_DC_END
static int mpeg_decode_a53_cc(AVCodecContext *avctx, const uint8_t *p, int buf_size)
Definition: mpeg12dec.c:2245
RLTable ff_rl_mpeg1
Definition: mpeg12data.c:166
ITU-R 601, SMPTE 274M 296M S314M(DV 4:1:1), mpeg2 4:2:2.
Definition: pixfmt.h:537
int alternate_scan
Definition: mpegvideo.h:468
int save_height
Definition: mpeg12dec.c:66
#define MB_BTYPE_VLC_BITS
Definition: mpeg12vlc.h:40
#define GOP_START_CODE
Definition: mpegvideo.h:68
int32_t
AVFormatContext * ctx
Definition: movenc.c:48
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition: avcodec.h:2477
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
Definition: get_bits.h:297
int a53_caption_size
Definition: mpeg12dec.c:61
#define MB_TYPE_L0L1
Definition: avcodec.h:1314
#define FF_THREAD_SLICE
Decode more than one part of a single frame at once.
Definition: avcodec.h:3204
int level
level
Definition: avcodec.h:3364
#define LAST_SKIP_BITS(name, gb, num)
Definition: get_bits.h:182
static av_cold int mpeg_decode_end(AVCodecContext *avctx)
Definition: mpeg12dec.c:2855
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:556
static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
Definition: mpeg12dec.c:1542
#define AV_RL32
Definition: intreadwrite.h:146
int16_t(*[12] pblocks)[64]
Definition: mpegvideo.h:496
#define CONFIG_GRAY
Definition: config.h:528
int block_last_index[12]
last non zero coefficient in block
Definition: mpegvideo.h:83
#define AV_EF_EXPLODE
abort decoding on minor error detection
Definition: avcodec.h:3061
int n
Definition: avisynth_c.h:684
uint8_t idct_permutation[64]
IDCT input permutation.
Definition: idctdsp.h:96
int ticks_per_frame
For some codecs, the time base is closer to the field rate than the frame rate.
Definition: avcodec.h:1907
int mpeg_f_code[2][2]
Definition: mpegvideo.h:455
#define EXT_START_CODE
Definition: cavs.h:33
#define MB_TYPE_L1
Definition: avcodec.h:1313
int mpeg_enc_ctx_allocated
Definition: mpeg12dec.c:55
#define check_scantable_index(ctx, x)
Definition: mpeg12dec.c:128
if(ret< 0)
Definition: vf_mcdeint.c:279
int ff_mpeg_update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
Definition: mpegvideo.c:483
preferred ID for MPEG-1/2 video decoding
Definition: avcodec.h:219
RL_VLC_ELEM * rl_vlc[32]
decoding only
Definition: rl.h:48
#define SHOW_UBITS(name, gb, num)
Definition: get_bits.h:194
void ff_mpv_decode_defaults(MpegEncContext *s)
Set the given MpegEncContext to defaults for decoding.
Definition: mpegvideo.c:657
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
Definition: avcodec.h:3192
static int mpeg1_decode_sequence(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
Definition: mpeg12dec.c:2102
static const float pred[4]
Definition: siprdata.h:259
AVRational save_aspect
Definition: mpeg12dec.c:65
int first_field
is 1 for the first field of a field picture 0 otherwise
Definition: mpegvideo.h:482
#define AV_CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
Definition: avcodec.h:1069
int save_width
Definition: mpeg12dec.c:66
#define MV_TYPE_16X16
1 vector for the whole mb
Definition: mpegvideo.h:266
#define MV_VLC_BITS
Definition: ituh263dec.c:54
int frame_pred_frame_dct
Definition: mpegvideo.h:462
static int mpeg2_decode_block_intra(MpegEncContext *s, int16_t *block, int n)
Definition: mpeg12dec.c:473
enum AVStereo3DType type
How views are packed within the video.
Definition: stereo3d.h:161
uint16_t inter_matrix[64]
Definition: mpegvideo.h:302
uint8_t * buffer
Definition: parser.h:29
static enum AVPixelFormat mpeg2_hwaccel_pixfmt_list_420[]
Definition: mpeg12dec.c:1144
int concealment_motion_vectors
Definition: mpegvideo.h:464
struct MpegEncContext * thread_context[MAX_THREADS]
Definition: mpegvideo.h:152
Libavcodec external API header.
Views are on top of each other.
Definition: stereo3d.h:79
int(* end_frame)(AVCodecContext *avctx)
Called at the end of each frame or field picture.
Definition: avcodec.h:3955
AVRational frame_rate_ext
Definition: mpeg12dec.c:67
enum AVCodecID codec_id
Definition: avcodec.h:1778
BlockDSPContext bdsp
Definition: mpegvideo.h:223
static int mpeg2_fast_decode_block_intra(MpegEncContext *s, int16_t *block, int n)
Changing this would eat up any speed benefits it has.
Definition: mpeg12dec.c:562
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:232
#define AV_CODEC_FLAG2_FAST
Allow non spec compliant speedup tricks.
Definition: avcodec.h:944
int debug
debug
Definition: avcodec.h:3003
#define MB_PTYPE_VLC_BITS
Definition: mpeg12vlc.h:39
main external API structure.
Definition: avcodec.h:1761
ScanTable intra_scantable
Definition: mpegvideo.h:88
#define USER_START_CODE
Definition: cavs.h:34
int height
picture size. must be a multiple of 16
Definition: mpegvideo.h:97
#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:1793
#define OPEN_READER(name, gb)
Definition: get_bits.h:121
uint8_t * data
Definition: frame.h:165
#define MV_TYPE_16X8
2 vectors, one per 16x8 block
Definition: mpegvideo.h:268
int chroma_420_type
Definition: mpegvideo.h:471
void * buf
Definition: avisynth_c.h:690
static int mpeg_decode_slice(MpegEncContext *s, int mb_y, const uint8_t **buf, int buf_size)
Decode a slice.
Definition: mpeg12dec.c:1723
void ff_print_debug_info(MpegEncContext *s, Picture *p, AVFrame *pict)
Definition: mpegvideo.c:1966
int extradata_size
Definition: avcodec.h:1877
int progressive_sequence
Definition: mpegvideo.h:454
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:314
static void skip_bits1(GetBitContext *s)
Definition: get_bits.h:339
int slice_flags
slice flags
Definition: avcodec.h:2304
int coded_height
Definition: avcodec.h:1963
#define FF_CODEC_PROPERTY_CLOSED_CAPTIONS
Definition: avcodec.h:3583
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:307
static const AVProfile profiles[]
AVFrameSideData * av_frame_new_side_data(AVFrame *frame, enum AVFrameSideDataType type, int size)
Add a new side data to a frame.
Definition: frame.c:674
int closed_gop
MPEG1/2 GOP is closed.
Definition: mpegvideo.h:208
int has_stereo3d
Definition: mpeg12dec.c:59
VLC ff_mb_ptype_vlc
Definition: mpeg12.c:133
unsigned int avpriv_toupper4(unsigned int x)
Definition: utils.c:2119
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:2491
Rational number (pair of numerator and denominator).
Definition: rational.h:58
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition: avcodec.h:2484
struct AVFrame * f
Definition: mpegpicture.h:46
HW acceleration through VDPAU, Picture.data[3] contains a VdpVideoSurface.
Definition: pixfmt.h:213
static enum AVPixelFormat mpeg12_pixfmt_list_422[]
Definition: mpeg12dec.c:1171
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:426
#define GET_CACHE(name, gb)
Definition: get_bits.h:198
const uint16_t ff_mpeg1_default_intra_matrix[256]
Definition: mpeg12data.c:30
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:178
#define MB_TYPE_16x16
Definition: avcodec.h:1299
int save_progressive_seq
Definition: mpeg12dec.c:66
const uint8_t ff_zigzag_direct[64]
Definition: mathtables.c:98
#define MT_DMV
Definition: mpeg12dec.c:663
enum AVPixelFormat ff_thread_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
Wrapper around get_format() for frame-multithreaded codecs.
#define s1
Definition: regdef.h:38
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:1196
attribute_deprecated int dtg_active_format
DTG active format information (additional aspect ratio information only used in DVB MPEG-2 transport ...
Definition: avcodec.h:2267
#define ER_DC_ERROR
HW decoding through DXVA2, Picture.data[3] contains a LPDIRECT3DSURFACE9 pointer. ...
Definition: pixfmt.h:152
static int mpeg_decode_frame(AVCodecContext *avctx, void *data, int *got_output, AVPacket *avpkt)
Definition: mpeg12dec.c:2771
#define MV_DIR_FORWARD
Definition: mpegvideo.h:262
static int decode_chunks(AVCodecContext *avctx, AVFrame *picture, int *got_output, const uint8_t *buf, int buf_size)
Definition: mpeg12dec.c:2432
int pict_type
AV_PICTURE_TYPE_I, AV_PICTURE_TYPE_P, AV_PICTURE_TYPE_B, ...
Definition: mpegvideo.h:209
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:266
av_cold void ff_mpeg12_common_init(MpegEncContext *s)
Definition: mpeg12.c:107
#define AV_CODEC_CAP_TRUNCATED
Definition: avcodec.h:1003
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:130
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:505
int skip_bottom
Number of macroblock rows at the bottom which are skipped.
Definition: avcodec.h:2386
int(* decode_slice)(AVCodecContext *avctx, const uint8_t *buf, uint32_t buf_size)
Callback for each slice.
Definition: avcodec.h:3944
Pan Scan area.
Definition: avcodec.h:1325
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:215
uint8_t level
Definition: svq3.c:207
AVRational av_d2q(double d, int max)
Convert a double precision floating point number to a rational.
Definition: rational.c:106
int mv[2][4][2]
motion vectors for a macroblock first coordinate : 0 = forward 1 = backward second " : depend...
Definition: mpegvideo.h:276
int b8_stride
2*mb_width+1 used for some 8x8 block arrays to allow simple addressing
Definition: mpegvideo.h:128
Hardware surfaces for Direct3D11.
Definition: pixfmt.h:331
the normal 219*2^(n-8) "MPEG" YUV ranges
Definition: pixfmt.h:513
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:3930
MpegEncContext.
Definition: mpegvideo.h:78
Picture * next_picture_ptr
pointer to the next picture (for bidir pred)
Definition: mpegvideo.h:180
struct AVCodecContext * avctx
Definition: mpegvideo.h:95
#define SHOW_SBITS(name, gb, num)
Definition: get_bits.h:195
#define MB_TYPE_CBP
Definition: avcodec.h:1316
static int av_cmp_q(AVRational a, AVRational b)
Compare two rationals.
Definition: rational.h:89
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:240
discard all non reference
Definition: avcodec.h:826
const AVRational ff_mpeg2_aspect[16]
Definition: mpeg12data.c:395
volatile int error_count
static enum AVPixelFormat mpeg1_hwaccel_pixfmt_list_420[]
Definition: mpeg12dec.c:1130
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
Y , 8bpp.
Definition: pixfmt.h:74
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:83
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:127
void ff_mpv_decode_init(MpegEncContext *s, AVCodecContext *avctx)
Definition: mpegvideo.c:662
static int ref[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:107
#define FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM
The decoder extracts and fills its parameters even if the frame is skipped due to the skip_frame sett...
Definition: internal.h:60
AVCodec ff_mpeg2video_decoder
Definition: mpeg12dec.c:2883
#define TEX_VLC_BITS
Definition: dv.h:96
static int mpeg_decode_mb(MpegEncContext *s, int16_t block[12][64])
Definition: mpeg12dec.c:665
uint8_t * dest[3]
Definition: mpegvideo.h:295
static double c[64]
const uint8_t * buffer_end
Definition: get_bits.h:57
static void flush(AVCodecContext *avctx)
Definition: mpeg12dec.c:2846
VLC ff_mbincr_vlc
Definition: mpeg12.c:132
Picture * last_picture_ptr
pointer to the previous picture.
Definition: mpegvideo.h:179
Bi-dir predicted.
Definition: avutil.h:276
Stereoscopic video.
static int mpeg1_decode_picture(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
Definition: mpeg12dec.c:1371
unsigned properties
Properties of the stream that gets decoded.
Definition: avcodec.h:3581
int den
Denominator.
Definition: rational.h:60
const uint8_t ff_alternate_vertical_scan[64]
#define MB_TYPE_16x8
Definition: avcodec.h:1300
int ff_update_duplicate_context(MpegEncContext *dst, MpegEncContext *src)
Definition: mpegvideo.c:455
AVStereo3D stereo3d
Definition: mpeg12dec.c:58
static int lowres
Definition: ffplay.c:331
#define IS_INTRA(x, y)
static int slice_end(AVCodecContext *avctx, AVFrame *pict)
Handle slice ends.
Definition: mpeg12dec.c:2051
int16_t position[3][2]
position of the top left corner in 1/16 pel for up to 3 fields/frames
Definition: avcodec.h:1346
void * priv_data
Definition: avcodec.h:1803
#define PICT_FRAME
Definition: mpegutils.h:39
int frame_rate_index
Definition: mpegvideo.h:214
av_cold int ff_mpv_common_init(MpegEncContext *s)
init common structure for both encoder and decoder.
Definition: mpegvideo.c:877
static int mpeg_decode_postinit(AVCodecContext *avctx)
Definition: mpeg12dec.c:1234
static av_always_inline int diff(const uint32_t a, const uint32_t b)
int picture_structure
Definition: mpegvideo.h:458
av_cold void ff_init_scantable(uint8_t *permutation, ScanTable *st, const uint8_t *src_scantable)
Definition: idctdsp.c:29
#define FF_DEBUG_STARTCODE
Definition: avcodec.h:3017
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:84
void ff_mpv_frame_end(MpegEncContext *s)
Definition: mpegvideo.c:1424
#define MT_FRAME
Definition: mpeg12dec.c:661
#define MV_TYPE_DMV
2 vectors, special mpeg2 Dual Prime Vectors
Definition: mpegvideo.h:270
void ff_mpv_reconstruct_mb(MpegEncContext *s, int16_t block[12][64])
Definition: mpegvideo.c:2727
#define SEQ_START_CODE
Definition: mpegvideo.h:67
int resync_mb_y
y position of last resync marker
Definition: mpegvideo.h:357
int16_t(* block)[64]
points to one of the following blocks
Definition: mpegvideo.h:498
ParseContext parse_context
Definition: mpegvideo.h:362
#define FF_QSCALE_TYPE_MPEG2
Definition: avcodec.h:1389
VLC_TYPE(* table)[2]
code, bits
Definition: vlc.h:28
int64_t bit_rate
wanted bit rate
Definition: mpegvideo.h:100
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:279
static const uint8_t * align_get_bits(GetBitContext *s)
Definition: get_bits.h:465
int flags2
AV_CODEC_FLAG2_*.
Definition: avcodec.h:1863
#define HAVE_THREADS
Definition: config.h:385
MpegEncContext mpeg_enc_ctx
Definition: mpeg12dec.c:54
int slice_count
Definition: mpeg12dec.c:64
#define SLICE_FLAG_CODED_ORDER
draw_horiz_band() is called in coded order instead of display
Definition: avcodec.h:2305
AVRational av_mul_q(AVRational b, AVRational c)
Multiply two rationals.
Definition: rational.c:80
#define MB_TYPE_ZERO_MV
Definition: mpeg12dec.c:74
#define AV_CODEC_FLAG_TRUNCATED
Input bitstream might be truncated at a random location instead of only at frame boundaries.
Definition: avcodec.h:913
#define AV_CODEC_FLAG2_SHOW_ALL
Show all frames before the first keyframe.
Definition: avcodec.h:972
uint16_t intra_matrix[64]
matrix transmitted in the bitstream
Definition: mpegvideo.h:300
uint32_t * mb_type
types and macros are defined in mpegutils.h
Definition: mpegpicture.h:56
#define av_freep(p)
ScanTable inter_scantable
if inter == intra then intra should be used to reduce the cache usage
Definition: mpegvideo.h:87
#define PICTURE_START_CODE
Definition: mpegvideo.h:69
static int skip_1stop_8data_bits(GetBitContext *gb)
Definition: get_bits.h:594
HW decoding through Direct3D11 via old API, Picture.data[3] contains a ID3D11VideoDecoderOutputView p...
Definition: pixfmt.h:247
MPEG-1 4:2:0, JPEG 4:2:0, H.263 4:2:0.
Definition: pixfmt.h:536
#define ER_AC_END
static int slice_decode_thread(AVCodecContext *c, void *arg)
Definition: mpeg12dec.c:2000
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:3232
static enum AVPixelFormat mpeg12_pixfmt_list_444[]
Definition: mpeg12dec.c:1176
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
This structure stores compressed data.
Definition: avcodec.h:1656
void ff_mpv_report_decode_progress(MpegEncContext *s)
Definition: mpegvideo.c:2823
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:1002
#define CONFIG_MPEG2_XVMC_HWACCEL
Definition: config.h:2043
#define MB_TYPE_L0
Definition: avcodec.h:1312
static void mpeg_decode_picture_display_extension(Mpeg1Context *s1)
Definition: mpeg12dec.c:1485
Predicted.
Definition: avutil.h:275
#define AV_TIMECODE_STR_SIZE
Definition: timecode.h:33
int64_t rc_max_rate
maximum bitrate
Definition: avcodec.h:2762
const AVRational ff_mpeg12_frame_rate_tab[]
AVPanScan pan_scan
Definition: mpeg12dec.c:57
VLC ff_mb_btype_vlc
Definition: mpeg12.c:134