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