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