mpeg12.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 //#define DEBUG
29 #include "internal.h"
30 #include "avcodec.h"
31 #include "dsputil.h"
32 #include "mpegvideo.h"
33 
34 #include "mpeg12.h"
35 #include "mpeg12data.h"
36 #include "mpeg12decdata.h"
37 #include "bytestream.h"
38 #include "vdpau_internal.h"
39 #include "xvmc_internal.h"
40 #include "thread.h"
41 
42 //#undef NDEBUG
43 //#include <assert.h>
44 
45 
46 #define MV_VLC_BITS 9
47 #define MBINCR_VLC_BITS 9
48 #define MB_PAT_VLC_BITS 9
49 #define MB_PTYPE_VLC_BITS 6
50 #define MB_BTYPE_VLC_BITS 6
51 
52 static VLC mv_vlc;
53 
54 /* as H.263, but only 17 codes */
55 static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred)
56 {
57  int code, sign, val, shift;
58 
59  code = get_vlc2(&s->gb, mv_vlc.table, MV_VLC_BITS, 2);
60  if (code == 0) {
61  return pred;
62  }
63  if (code < 0) {
64  return 0xffff;
65  }
66 
67  sign = get_bits1(&s->gb);
68  shift = fcode - 1;
69  val = code;
70  if (shift) {
71  val = (val - 1) << shift;
72  val |= get_bits(&s->gb, shift);
73  val++;
74  }
75  if (sign)
76  val = -val;
77  val += pred;
78 
79  /* modulo decoding */
80  return sign_extend(val, 5 + shift);
81 }
82 
83 #define check_scantable_index(ctx, x) \
84  do { \
85  if ((x) > 63) { \
86  av_log(ctx->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", \
87  ctx->mb_x, ctx->mb_y); \
88  return AVERROR_INVALIDDATA; \
89  } \
90  } while (0) \
91 
92 static inline int mpeg1_decode_block_intra(MpegEncContext *s, DCTELEM *block, int n)
93 {
94  int level, dc, diff, i, j, run;
95  int component;
96  RLTable *rl = &ff_rl_mpeg1;
97  uint8_t * const scantable = s->intra_scantable.permutated;
98  const uint16_t *quant_matrix = s->intra_matrix;
99  const int qscale = s->qscale;
100 
101  /* DC coefficient */
102  component = (n <= 3 ? 0 : n - 4 + 1);
103  diff = decode_dc(&s->gb, component);
104  if (diff >= 0xffff)
105  return -1;
106  dc = s->last_dc[component];
107  dc += diff;
108  s->last_dc[component] = dc;
109  block[0] = dc * quant_matrix[0];
110  av_dlog(s->avctx, "dc=%d diff=%d\n", dc, diff);
111  i = 0;
112  {
113  OPEN_READER(re, &s->gb);
114  /* now quantify & encode AC coefficients */
115  for (;;) {
116  UPDATE_CACHE(re, &s->gb);
117  GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
118 
119  if (level == 127) {
120  break;
121  } else if (level != 0) {
122  i += run;
123  check_scantable_index(s, i);
124  j = scantable[i];
125  level = (level * qscale * quant_matrix[j]) >> 4;
126  level = (level - 1) | 1;
127  level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
128  LAST_SKIP_BITS(re, &s->gb, 1);
129  } else {
130  /* escape */
131  run = SHOW_UBITS(re, &s->gb, 6) + 1; LAST_SKIP_BITS(re, &s->gb, 6);
132  UPDATE_CACHE(re, &s->gb);
133  level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
134  if (level == -128) {
135  level = SHOW_UBITS(re, &s->gb, 8) - 256; LAST_SKIP_BITS(re, &s->gb, 8);
136  } else if (level == 0) {
137  level = SHOW_UBITS(re, &s->gb, 8) ; LAST_SKIP_BITS(re, &s->gb, 8);
138  }
139  i += run;
140  check_scantable_index(s, i);
141  j = scantable[i];
142  if (level < 0) {
143  level = -level;
144  level = (level * qscale * quant_matrix[j]) >> 4;
145  level = (level - 1) | 1;
146  level = -level;
147  } else {
148  level = (level * qscale * quant_matrix[j]) >> 4;
149  level = (level - 1) | 1;
150  }
151  }
152 
153  block[j] = level;
154  }
155  CLOSE_READER(re, &s->gb);
156  }
157  s->block_last_index[n] = i;
158  return 0;
159 }
160 
162 {
163  return mpeg1_decode_block_intra(s, block, n);
164 }
165 
166 static inline int mpeg1_decode_block_inter(MpegEncContext *s, DCTELEM *block, int n)
167 {
168  int level, i, j, run;
169  RLTable *rl = &ff_rl_mpeg1;
170  uint8_t * const scantable = s->intra_scantable.permutated;
171  const uint16_t *quant_matrix = s->inter_matrix;
172  const int qscale = s->qscale;
173 
174  {
175  OPEN_READER(re, &s->gb);
176  i = -1;
177  // special case for first coefficient, no need to add second VLC table
178  UPDATE_CACHE(re, &s->gb);
179  if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
180  level = (3 * qscale * quant_matrix[0]) >> 5;
181  level = (level - 1) | 1;
182  if (GET_CACHE(re, &s->gb) & 0x40000000)
183  level = -level;
184  block[0] = level;
185  i++;
186  SKIP_BITS(re, &s->gb, 2);
187  if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
188  goto end;
189  }
190  /* now quantify & encode AC coefficients */
191  for (;;) {
192  GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
193 
194  if (level != 0) {
195  i += run;
196  j = scantable[i];
197  level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
198  level = (level - 1) | 1;
199  level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
200  SKIP_BITS(re, &s->gb, 1);
201  } else {
202  /* escape */
203  run = SHOW_UBITS(re, &s->gb, 6) + 1; LAST_SKIP_BITS(re, &s->gb, 6);
204  UPDATE_CACHE(re, &s->gb);
205  level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
206  if (level == -128) {
207  level = SHOW_UBITS(re, &s->gb, 8) - 256; SKIP_BITS(re, &s->gb, 8);
208  } else if (level == 0) {
209  level = SHOW_UBITS(re, &s->gb, 8) ; SKIP_BITS(re, &s->gb, 8);
210  }
211  i += run;
212  j = scantable[i];
213  if (level < 0) {
214  level = -level;
215  level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
216  level = (level - 1) | 1;
217  level = -level;
218  } else {
219  level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
220  level = (level - 1) | 1;
221  }
222  }
223  if (i > 63) {
224  av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
225  return -1;
226  }
227 
228  block[j] = level;
229  if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
230  break;
231  UPDATE_CACHE(re, &s->gb);
232  }
233 end:
234  LAST_SKIP_BITS(re, &s->gb, 2);
235  CLOSE_READER(re, &s->gb);
236  }
237  s->block_last_index[n] = i;
238  return 0;
239 }
240 
242 {
243  int level, i, j, run;
244  RLTable *rl = &ff_rl_mpeg1;
245  uint8_t * const scantable = s->intra_scantable.permutated;
246  const int qscale = s->qscale;
247 
248  {
249  OPEN_READER(re, &s->gb);
250  i = -1;
251  // special case for first coefficient, no need to add second VLC table
252  UPDATE_CACHE(re, &s->gb);
253  if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
254  level = (3 * qscale) >> 1;
255  level = (level - 1) | 1;
256  if (GET_CACHE(re, &s->gb) & 0x40000000)
257  level = -level;
258  block[0] = level;
259  i++;
260  SKIP_BITS(re, &s->gb, 2);
261  if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
262  goto end;
263  }
264 
265  /* now quantify & encode AC coefficients */
266  for (;;) {
267  GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
268 
269  if (level != 0) {
270  i += run;
271  check_scantable_index(s, i);
272  j = scantable[i];
273  level = ((level * 2 + 1) * qscale) >> 1;
274  level = (level - 1) | 1;
275  level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
276  SKIP_BITS(re, &s->gb, 1);
277  } else {
278  /* escape */
279  run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
280  UPDATE_CACHE(re, &s->gb);
281  level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
282  if (level == -128) {
283  level = SHOW_UBITS(re, &s->gb, 8) - 256; SKIP_BITS(re, &s->gb, 8);
284  } else if (level == 0) {
285  level = SHOW_UBITS(re, &s->gb, 8) ; SKIP_BITS(re, &s->gb, 8);
286  }
287  i += run;
288  check_scantable_index(s, i);
289  j = scantable[i];
290  if (level < 0) {
291  level = -level;
292  level = ((level * 2 + 1) * qscale) >> 1;
293  level = (level - 1) | 1;
294  level = -level;
295  } else {
296  level = ((level * 2 + 1) * qscale) >> 1;
297  level = (level - 1) | 1;
298  }
299  }
300 
301  block[j] = level;
302  if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
303  break;
304  UPDATE_CACHE(re, &s->gb);
305  }
306 end:
307  LAST_SKIP_BITS(re, &s->gb, 2);
308  CLOSE_READER(re, &s->gb);
309  }
310  s->block_last_index[n] = i;
311  return 0;
312 }
313 
314 
316 {
317  int level, i, j, run;
318  RLTable *rl = &ff_rl_mpeg1;
319  uint8_t * const scantable = s->intra_scantable.permutated;
320  const uint16_t *quant_matrix;
321  const int qscale = s->qscale;
322  int mismatch;
323 
324  mismatch = 1;
325 
326  {
327  OPEN_READER(re, &s->gb);
328  i = -1;
329  if (n < 4)
330  quant_matrix = s->inter_matrix;
331  else
332  quant_matrix = s->chroma_inter_matrix;
333 
334  // special case for first coefficient, no need to add second VLC table
335  UPDATE_CACHE(re, &s->gb);
336  if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
337  level= (3 * qscale * quant_matrix[0]) >> 5;
338  if (GET_CACHE(re, &s->gb) & 0x40000000)
339  level = -level;
340  block[0] = level;
341  mismatch ^= level;
342  i++;
343  SKIP_BITS(re, &s->gb, 2);
344  if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
345  goto end;
346  }
347 
348  /* now quantify & encode AC coefficients */
349  for (;;) {
350  GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
351 
352  if (level != 0) {
353  i += run;
354  check_scantable_index(s, i);
355  j = scantable[i];
356  level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
357  level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
358  SKIP_BITS(re, &s->gb, 1);
359  } else {
360  /* escape */
361  run = SHOW_UBITS(re, &s->gb, 6) + 1; LAST_SKIP_BITS(re, &s->gb, 6);
362  UPDATE_CACHE(re, &s->gb);
363  level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
364 
365  i += run;
366  check_scantable_index(s, i);
367  j = scantable[i];
368  if (level < 0) {
369  level = ((-level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
370  level = -level;
371  } else {
372  level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
373  }
374  }
375 
376  mismatch ^= level;
377  block[j] = level;
378  if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
379  break;
380  UPDATE_CACHE(re, &s->gb);
381  }
382 end:
383  LAST_SKIP_BITS(re, &s->gb, 2);
384  CLOSE_READER(re, &s->gb);
385  }
386  block[63] ^= (mismatch & 1);
387 
388  s->block_last_index[n] = i;
389  return 0;
390 }
391 
393  DCTELEM *block, int n)
394 {
395  int level, i, j, run;
396  RLTable *rl = &ff_rl_mpeg1;
397  uint8_t * const scantable = s->intra_scantable.permutated;
398  const int qscale = s->qscale;
399  OPEN_READER(re, &s->gb);
400  i = -1;
401 
402  // special case for first coefficient, no need to add second VLC table
403  UPDATE_CACHE(re, &s->gb);
404  if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
405  level = (3 * qscale) >> 1;
406  if (GET_CACHE(re, &s->gb) & 0x40000000)
407  level = -level;
408  block[0] = level;
409  i++;
410  SKIP_BITS(re, &s->gb, 2);
411  if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
412  goto end;
413  }
414 
415  /* now quantify & encode AC coefficients */
416  for (;;) {
417  GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
418 
419  if (level != 0) {
420  i += run;
421  check_scantable_index(s, i);
422  j = scantable[i];
423  level = ((level * 2 + 1) * qscale) >> 1;
424  level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
425  SKIP_BITS(re, &s->gb, 1);
426  } else {
427  /* escape */
428  run = SHOW_UBITS(re, &s->gb, 6) + 1; LAST_SKIP_BITS(re, &s->gb, 6);
429  UPDATE_CACHE(re, &s->gb);
430  level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
431 
432  i += run;
433  check_scantable_index(s, i);
434  j = scantable[i];
435  if (level < 0) {
436  level = ((-level * 2 + 1) * qscale) >> 1;
437  level = -level;
438  } else {
439  level = ((level * 2 + 1) * qscale) >> 1;
440  }
441  }
442 
443  block[j] = level;
444  if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
445  break;
446  UPDATE_CACHE(re, &s->gb);
447  }
448 end:
449  LAST_SKIP_BITS(re, &s->gb, 2);
450  CLOSE_READER(re, &s->gb);
451  s->block_last_index[n] = i;
452  return 0;
453 }
454 
455 
456 static inline int mpeg2_decode_block_intra(MpegEncContext *s, DCTELEM *block, int n)
457 {
458  int level, dc, diff, i, j, run;
459  int component;
460  RLTable *rl;
461  uint8_t * const scantable = s->intra_scantable.permutated;
462  const uint16_t *quant_matrix;
463  const int qscale = s->qscale;
464  int mismatch;
465 
466  /* DC coefficient */
467  if (n < 4) {
468  quant_matrix = s->intra_matrix;
469  component = 0;
470  } else {
471  quant_matrix = s->chroma_intra_matrix;
472  component = (n & 1) + 1;
473  }
474  diff = decode_dc(&s->gb, component);
475  if (diff >= 0xffff)
476  return -1;
477  dc = s->last_dc[component];
478  dc += diff;
479  s->last_dc[component] = dc;
480  block[0] = dc << (3 - s->intra_dc_precision);
481  av_dlog(s->avctx, "dc=%d\n", block[0]);
482  mismatch = block[0] ^ 1;
483  i = 0;
484  if (s->intra_vlc_format)
485  rl = &ff_rl_mpeg2;
486  else
487  rl = &ff_rl_mpeg1;
488 
489  {
490  OPEN_READER(re, &s->gb);
491  /* now quantify & encode AC coefficients */
492  for (;;) {
493  UPDATE_CACHE(re, &s->gb);
494  GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
495 
496  if (level == 127) {
497  break;
498  } else if (level != 0) {
499  i += run;
500  check_scantable_index(s, i);
501  j = scantable[i];
502  level = (level * qscale * quant_matrix[j]) >> 4;
503  level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
504  LAST_SKIP_BITS(re, &s->gb, 1);
505  } else {
506  /* escape */
507  run = SHOW_UBITS(re, &s->gb, 6) + 1; LAST_SKIP_BITS(re, &s->gb, 6);
508  UPDATE_CACHE(re, &s->gb);
509  level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
510  i += run;
511  check_scantable_index(s, i);
512  j = scantable[i];
513  if (level < 0) {
514  level = (-level * qscale * quant_matrix[j]) >> 4;
515  level = -level;
516  } else {
517  level = (level * qscale * quant_matrix[j]) >> 4;
518  }
519  }
520 
521  mismatch ^= level;
522  block[j] = level;
523  }
524  CLOSE_READER(re, &s->gb);
525  }
526  block[63] ^= mismatch & 1;
527 
528  s->block_last_index[n] = i;
529  return 0;
530 }
531 
533 {
534  int level, dc, diff, i, j, run;
535  int component;
536  RLTable *rl;
537  uint8_t * const scantable = s->intra_scantable.permutated;
538  const uint16_t *quant_matrix;
539  const int qscale = s->qscale;
540 
541  /* DC coefficient */
542  if (n < 4) {
543  quant_matrix = s->intra_matrix;
544  component = 0;
545  } else {
546  quant_matrix = s->chroma_intra_matrix;
547  component = (n & 1) + 1;
548  }
549  diff = decode_dc(&s->gb, component);
550  if (diff >= 0xffff)
551  return -1;
552  dc = s->last_dc[component];
553  dc += diff;
554  s->last_dc[component] = dc;
555  block[0] = dc << (3 - s->intra_dc_precision);
556  i = 0;
557  if (s->intra_vlc_format)
558  rl = &ff_rl_mpeg2;
559  else
560  rl = &ff_rl_mpeg1;
561 
562  {
563  OPEN_READER(re, &s->gb);
564  /* now quantify & encode AC coefficients */
565  for (;;) {
566  UPDATE_CACHE(re, &s->gb);
567  GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
568 
569  if (level == 127) {
570  break;
571  } else if (level != 0) {
572  i += run;
573  check_scantable_index(s, i);
574  j = scantable[i];
575  level = (level * qscale * quant_matrix[j]) >> 4;
576  level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
577  LAST_SKIP_BITS(re, &s->gb, 1);
578  } else {
579  /* escape */
580  run = SHOW_UBITS(re, &s->gb, 6) + 1; LAST_SKIP_BITS(re, &s->gb, 6);
581  UPDATE_CACHE(re, &s->gb);
582  level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
583  i += run;
584  check_scantable_index(s, i);
585  j = scantable[i];
586  if (level < 0) {
587  level = (-level * qscale * quant_matrix[j]) >> 4;
588  level = -level;
589  } else {
590  level = (level * qscale * quant_matrix[j]) >> 4;
591  }
592  }
593 
594  block[j] = level;
595  }
596  CLOSE_READER(re, &s->gb);
597  }
598 
599  s->block_last_index[n] = i;
600  return 0;
601 }
602 
604 
605 #define INIT_2D_VLC_RL(rl, static_size)\
606 {\
607  static RL_VLC_ELEM rl_vlc_table[static_size];\
608  INIT_VLC_STATIC(&rl.vlc, TEX_VLC_BITS, rl.n + 2,\
609  &rl.table_vlc[0][1], 4, 2,\
610  &rl.table_vlc[0][0], 4, 2, static_size);\
611 \
612  rl.rl_vlc[0] = rl_vlc_table;\
613  init_2d_vlc_rl(&rl);\
614 }
615 
616 static void init_2d_vlc_rl(RLTable *rl)
617 {
618  int i;
619 
620  for (i = 0; i < rl->vlc.table_size; i++) {
621  int code = rl->vlc.table[i][0];
622  int len = rl->vlc.table[i][1];
623  int level, run;
624 
625  if (len == 0) { // illegal code
626  run = 65;
627  level = MAX_LEVEL;
628  } else if (len<0) { //more bits needed
629  run = 0;
630  level = code;
631  } else {
632  if (code == rl->n) { //esc
633  run = 65;
634  level = 0;
635  } else if (code == rl->n+1) { //eob
636  run = 0;
637  level = 127;
638  } else {
639  run = rl->table_run [code] + 1;
640  level = rl->table_level[code];
641  }
642  }
643  rl->rl_vlc[0][i].len = len;
644  rl->rl_vlc[0][i].level = level;
645  rl->rl_vlc[0][i].run = run;
646  }
647 }
648 
650 {
651 
652  s->y_dc_scale_table =
654 
655 }
656 
658 {
659  s->last_dc[0] = 1 << (7 + s->intra_dc_precision);
660  s->last_dc[1] = s->last_dc[0];
661  s->last_dc[2] = s->last_dc[0];
662  memset(s->last_mv, 0, sizeof(s->last_mv));
663 }
664 
665 
666 /******************************************/
667 /* decoding */
668 
671 
676 
678 {
679  static int done = 0;
680 
681  if (!done) {
682  done = 1;
683 
684  INIT_VLC_STATIC(&ff_dc_lum_vlc, DC_VLC_BITS, 12,
686  ff_mpeg12_vlc_dc_lum_code, 2, 2, 512);
687  INIT_VLC_STATIC(&ff_dc_chroma_vlc, DC_VLC_BITS, 12,
689  ff_mpeg12_vlc_dc_chroma_code, 2, 2, 514);
690  INIT_VLC_STATIC(&mv_vlc, MV_VLC_BITS, 17,
691  &ff_mpeg12_mbMotionVectorTable[0][1], 2, 1,
692  &ff_mpeg12_mbMotionVectorTable[0][0], 2, 1, 518);
693  INIT_VLC_STATIC(&mbincr_vlc, MBINCR_VLC_BITS, 36,
694  &ff_mpeg12_mbAddrIncrTable[0][1], 2, 1,
695  &ff_mpeg12_mbAddrIncrTable[0][0], 2, 1, 538);
696  INIT_VLC_STATIC(&mb_pat_vlc, MB_PAT_VLC_BITS, 64,
697  &ff_mpeg12_mbPatTable[0][1], 2, 1,
698  &ff_mpeg12_mbPatTable[0][0], 2, 1, 512);
699 
700  INIT_VLC_STATIC(&mb_ptype_vlc, MB_PTYPE_VLC_BITS, 7,
701  &table_mb_ptype[0][1], 2, 1,
702  &table_mb_ptype[0][0], 2, 1, 64);
703  INIT_VLC_STATIC(&mb_btype_vlc, MB_BTYPE_VLC_BITS, 11,
704  &table_mb_btype[0][1], 2, 1,
705  &table_mb_btype[0][0], 2, 1, 64);
708 
711  }
712 }
713 
714 static inline int get_dmv(MpegEncContext *s)
715 {
716  if (get_bits1(&s->gb))
717  return 1 - (get_bits1(&s->gb) << 1);
718  else
719  return 0;
720 }
721 
722 static inline int get_qscale(MpegEncContext *s)
723 {
724  int qscale = get_bits(&s->gb, 5);
725  if (s->q_scale_type) {
726  return non_linear_qscale[qscale];
727  } else {
728  return qscale << 1;
729  }
730 }
731 
733 {
734  DCTELEM (*tmp)[64];
735 
736  tmp = s->pblocks[4];
737  s->pblocks[4] = s->pblocks[5];
738  s->pblocks[5] = tmp;
739 }
740 
741 /* motion type (for MPEG-2) */
742 #define MT_FIELD 1
743 #define MT_FRAME 2
744 #define MT_16X8 2
745 #define MT_DMV 3
746 
748 {
749  int i, j, k, cbp, val, mb_type, motion_type;
750  const int mb_block_count = 4 + (1 << s->chroma_format);
751 
752  av_dlog(s->avctx, "decode_mb: x=%d y=%d\n", s->mb_x, s->mb_y);
753 
754  assert(s->mb_skipped == 0);
755 
756  if (s->mb_skip_run-- != 0) {
757  if (s->pict_type == AV_PICTURE_TYPE_P) {
758  s->mb_skipped = 1;
760  } else {
761  int mb_type;
762 
763  if (s->mb_x)
764  mb_type = s->current_picture.f.mb_type[s->mb_x + s->mb_y * s->mb_stride - 1];
765  else
766  mb_type = s->current_picture.f.mb_type[s->mb_width + (s->mb_y - 1) * s->mb_stride - 1]; // FIXME not sure if this is allowed in MPEG at all
767  if (IS_INTRA(mb_type))
768  return -1;
769  s->current_picture.f.mb_type[s->mb_x + s->mb_y*s->mb_stride] =
770  mb_type | MB_TYPE_SKIP;
771 // assert(s->current_picture.f.mb_type[s->mb_x + s->mb_y * s->mb_stride - 1] & (MB_TYPE_16x16 | MB_TYPE_16x8));
772 
773  if ((s->mv[0][0][0] | s->mv[0][0][1] | s->mv[1][0][0] | s->mv[1][0][1]) == 0)
774  s->mb_skipped = 1;
775  }
776 
777  return 0;
778  }
779 
780  switch (s->pict_type) {
781  default:
782  case AV_PICTURE_TYPE_I:
783  if (get_bits1(&s->gb) == 0) {
784  if (get_bits1(&s->gb) == 0) {
785  av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in I Frame at %d %d\n", s->mb_x, s->mb_y);
786  return -1;
787  }
788  mb_type = MB_TYPE_QUANT | MB_TYPE_INTRA;
789  } else {
790  mb_type = MB_TYPE_INTRA;
791  }
792  break;
793  case AV_PICTURE_TYPE_P:
794  mb_type = get_vlc2(&s->gb, mb_ptype_vlc.table, MB_PTYPE_VLC_BITS, 1);
795  if (mb_type < 0) {
796  av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in P Frame at %d %d\n", s->mb_x, s->mb_y);
797  return -1;
798  }
799  mb_type = ptype2mb_type[mb_type];
800  break;
801  case AV_PICTURE_TYPE_B:
802  mb_type = get_vlc2(&s->gb, mb_btype_vlc.table, MB_BTYPE_VLC_BITS, 1);
803  if (mb_type < 0) {
804  av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in B Frame at %d %d\n", s->mb_x, s->mb_y);
805  return -1;
806  }
807  mb_type = btype2mb_type[mb_type];
808  break;
809  }
810  av_dlog(s->avctx, "mb_type=%x\n", mb_type);
811 // motion_type = 0; /* avoid warning */
812  if (IS_INTRA(mb_type)) {
813  s->dsp.clear_blocks(s->block[0]);
814 
815  if (!s->chroma_y_shift) {
816  s->dsp.clear_blocks(s->block[6]);
817  }
818 
819  /* compute DCT type */
820  if (s->picture_structure == PICT_FRAME && // FIXME add an interlaced_dct coded var?
821  !s->frame_pred_frame_dct) {
822  s->interlaced_dct = get_bits1(&s->gb);
823  }
824 
825  if (IS_QUANT(mb_type))
826  s->qscale = get_qscale(s);
827 
829  /* just parse them */
830  if (s->picture_structure != PICT_FRAME)
831  skip_bits1(&s->gb); /* field select */
832 
833  s->mv[0][0][0]= s->last_mv[0][0][0]= s->last_mv[0][1][0] =
834  mpeg_decode_motion(s, s->mpeg_f_code[0][0], s->last_mv[0][0][0]);
835  s->mv[0][0][1]= s->last_mv[0][0][1]= s->last_mv[0][1][1] =
836  mpeg_decode_motion(s, s->mpeg_f_code[0][1], s->last_mv[0][0][1]);
837 
838  skip_bits1(&s->gb); /* marker */
839  } else
840  memset(s->last_mv, 0, sizeof(s->last_mv)); /* reset mv prediction */
841  s->mb_intra = 1;
842  // if 1, we memcpy blocks in xvmcvideo
844  ff_xvmc_pack_pblocks(s, -1); // inter are always full blocks
845  if (s->swap_uv) {
846  exchange_uv(s);
847  }
848  }
849 
850  if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
851  if (s->flags2 & CODEC_FLAG2_FAST) {
852  for (i = 0; i < 6; i++) {
854  }
855  } else {
856  for (i = 0; i < mb_block_count; i++) {
857  if (mpeg2_decode_block_intra(s, *s->pblocks[i], i) < 0)
858  return -1;
859  }
860  }
861  } else {
862  for (i = 0; i < 6; i++) {
863  if (mpeg1_decode_block_intra(s, *s->pblocks[i], i) < 0)
864  return -1;
865  }
866  }
867  } else {
868  if (mb_type & MB_TYPE_ZERO_MV) {
869  assert(mb_type & MB_TYPE_CBP);
870 
871  s->mv_dir = MV_DIR_FORWARD;
872  if (s->picture_structure == PICT_FRAME) {
873  if (!s->frame_pred_frame_dct)
874  s->interlaced_dct = get_bits1(&s->gb);
875  s->mv_type = MV_TYPE_16X16;
876  } else {
877  s->mv_type = MV_TYPE_FIELD;
878  mb_type |= MB_TYPE_INTERLACED;
879  s->field_select[0][0] = s->picture_structure - 1;
880  }
881 
882  if (IS_QUANT(mb_type))
883  s->qscale = get_qscale(s);
884 
885  s->last_mv[0][0][0] = 0;
886  s->last_mv[0][0][1] = 0;
887  s->last_mv[0][1][0] = 0;
888  s->last_mv[0][1][1] = 0;
889  s->mv[0][0][0] = 0;
890  s->mv[0][0][1] = 0;
891  } else {
892  assert(mb_type & MB_TYPE_L0L1);
893  // FIXME decide if MBs in field pictures are MB_TYPE_INTERLACED
894  /* get additional motion vector type */
895  if (s->frame_pred_frame_dct)
896  motion_type = MT_FRAME;
897  else {
898  motion_type = get_bits(&s->gb, 2);
899  if (s->picture_structure == PICT_FRAME && HAS_CBP(mb_type))
900  s->interlaced_dct = get_bits1(&s->gb);
901  }
902 
903  if (IS_QUANT(mb_type))
904  s->qscale = get_qscale(s);
905 
906  /* motion vectors */
907  s->mv_dir = (mb_type >> 13) & 3;
908  av_dlog(s->avctx, "motion_type=%d\n", motion_type);
909  switch (motion_type) {
910  case MT_FRAME: /* or MT_16X8 */
911  if (s->picture_structure == PICT_FRAME) {
912  mb_type |= MB_TYPE_16x16;
913  s->mv_type = MV_TYPE_16X16;
914  for (i = 0; i < 2; i++) {
915  if (USES_LIST(mb_type, i)) {
916  /* MT_FRAME */
917  s->mv[i][0][0]= s->last_mv[i][0][0]= s->last_mv[i][1][0] =
918  mpeg_decode_motion(s, s->mpeg_f_code[i][0], s->last_mv[i][0][0]);
919  s->mv[i][0][1]= s->last_mv[i][0][1]= s->last_mv[i][1][1] =
920  mpeg_decode_motion(s, s->mpeg_f_code[i][1], s->last_mv[i][0][1]);
921  /* full_pel: only for MPEG-1 */
922  if (s->full_pel[i]) {
923  s->mv[i][0][0] <<= 1;
924  s->mv[i][0][1] <<= 1;
925  }
926  }
927  }
928  } else {
929  mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
930  s->mv_type = MV_TYPE_16X8;
931  for (i = 0; i < 2; i++) {
932  if (USES_LIST(mb_type, i)) {
933  /* MT_16X8 */
934  for (j = 0; j < 2; j++) {
935  s->field_select[i][j] = get_bits1(&s->gb);
936  for (k = 0; k < 2; k++) {
937  val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
938  s->last_mv[i][j][k]);
939  s->last_mv[i][j][k] = val;
940  s->mv[i][j][k] = val;
941  }
942  }
943  }
944  }
945  }
946  break;
947  case MT_FIELD:
948  s->mv_type = MV_TYPE_FIELD;
949  if (s->picture_structure == PICT_FRAME) {
950  mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
951  for (i = 0; i < 2; i++) {
952  if (USES_LIST(mb_type, i)) {
953  for (j = 0; j < 2; j++) {
954  s->field_select[i][j] = get_bits1(&s->gb);
955  val = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
956  s->last_mv[i][j][0]);
957  s->last_mv[i][j][0] = val;
958  s->mv[i][j][0] = val;
959  av_dlog(s->avctx, "fmx=%d\n", val);
960  val = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
961  s->last_mv[i][j][1] >> 1);
962  s->last_mv[i][j][1] = val << 1;
963  s->mv[i][j][1] = val;
964  av_dlog(s->avctx, "fmy=%d\n", val);
965  }
966  }
967  }
968  } else {
969  mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
970  for (i = 0; i < 2; i++) {
971  if (USES_LIST(mb_type, i)) {
972  s->field_select[i][0] = get_bits1(&s->gb);
973  for (k = 0; k < 2; k++) {
974  val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
975  s->last_mv[i][0][k]);
976  s->last_mv[i][0][k] = val;
977  s->last_mv[i][1][k] = val;
978  s->mv[i][0][k] = val;
979  }
980  }
981  }
982  }
983  break;
984  case MT_DMV:
985  s->mv_type = MV_TYPE_DMV;
986  for (i = 0; i < 2; i++) {
987  if (USES_LIST(mb_type, i)) {
988  int dmx, dmy, mx, my, m;
989  const int my_shift = s->picture_structure == PICT_FRAME;
990 
991  mx = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
992  s->last_mv[i][0][0]);
993  s->last_mv[i][0][0] = mx;
994  s->last_mv[i][1][0] = mx;
995  dmx = get_dmv(s);
996  my = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
997  s->last_mv[i][0][1] >> my_shift);
998  dmy = get_dmv(s);
999 
1000 
1001  s->last_mv[i][0][1] = my << my_shift;
1002  s->last_mv[i][1][1] = my << my_shift;
1003 
1004  s->mv[i][0][0] = mx;
1005  s->mv[i][0][1] = my;
1006  s->mv[i][1][0] = mx; // not used
1007  s->mv[i][1][1] = my; // not used
1008 
1009  if (s->picture_structure == PICT_FRAME) {
1010  mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
1011 
1012  // m = 1 + 2 * s->top_field_first;
1013  m = s->top_field_first ? 1 : 3;
1014 
1015  /* top -> top pred */
1016  s->mv[i][2][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
1017  s->mv[i][2][1] = ((my * m + (my > 0)) >> 1) + dmy - 1;
1018  m = 4 - m;
1019  s->mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
1020  s->mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1;
1021  } else {
1022  mb_type |= MB_TYPE_16x16;
1023 
1024  s->mv[i][2][0] = ((mx + (mx > 0)) >> 1) + dmx;
1025  s->mv[i][2][1] = ((my + (my > 0)) >> 1) + dmy;
1027  s->mv[i][2][1]--;
1028  else
1029  s->mv[i][2][1]++;
1030  }
1031  }
1032  }
1033  break;
1034  default:
1035  av_log(s->avctx, AV_LOG_ERROR, "00 motion_type at %d %d\n", s->mb_x, s->mb_y);
1036  return -1;
1037  }
1038  }
1039 
1040  s->mb_intra = 0;
1041  if (HAS_CBP(mb_type)) {
1042  s->dsp.clear_blocks(s->block[0]);
1043 
1044  cbp = get_vlc2(&s->gb, mb_pat_vlc.table, MB_PAT_VLC_BITS, 1);
1045  if (mb_block_count > 6) {
1046  cbp <<= mb_block_count - 6;
1047  cbp |= get_bits(&s->gb, mb_block_count - 6);
1048  s->dsp.clear_blocks(s->block[6]);
1049  }
1050  if (cbp <= 0) {
1051  av_log(s->avctx, AV_LOG_ERROR, "invalid cbp at %d %d\n", s->mb_x, s->mb_y);
1052  return -1;
1053  }
1054 
1055  //if 1, we memcpy blocks in xvmcvideo
1057  ff_xvmc_pack_pblocks(s, cbp);
1058  if (s->swap_uv) {
1059  exchange_uv(s);
1060  }
1061  }
1062 
1063  if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
1064  if (s->flags2 & CODEC_FLAG2_FAST) {
1065  for (i = 0; i < 6; i++) {
1066  if (cbp & 32) {
1068  } else {
1069  s->block_last_index[i] = -1;
1070  }
1071  cbp += cbp;
1072  }
1073  } else {
1074  cbp <<= 12-mb_block_count;
1075 
1076  for (i = 0; i < mb_block_count; i++) {
1077  if (cbp & (1 << 11)) {
1078  if (mpeg2_decode_block_non_intra(s, *s->pblocks[i], i) < 0)
1079  return -1;
1080  } else {
1081  s->block_last_index[i] = -1;
1082  }
1083  cbp += cbp;
1084  }
1085  }
1086  } else {
1087  if (s->flags2 & CODEC_FLAG2_FAST) {
1088  for (i = 0; i < 6; i++) {
1089  if (cbp & 32) {
1090  mpeg1_fast_decode_block_inter(s, *s->pblocks[i], i);
1091  } else {
1092  s->block_last_index[i] = -1;
1093  }
1094  cbp += cbp;
1095  }
1096  } else {
1097  for (i = 0; i < 6; i++) {
1098  if (cbp & 32) {
1099  if (mpeg1_decode_block_inter(s, *s->pblocks[i], i) < 0)
1100  return -1;
1101  } else {
1102  s->block_last_index[i] = -1;
1103  }
1104  cbp += cbp;
1105  }
1106  }
1107  }
1108  } else {
1109  for (i = 0; i < 12; i++)
1110  s->block_last_index[i] = -1;
1111  }
1112  }
1113 
1114  s->current_picture.f.mb_type[s->mb_x + s->mb_y * s->mb_stride] = mb_type;
1115 
1116  return 0;
1117 }
1118 
1120 {
1121  Mpeg1Context *s = avctx->priv_data;
1123  int i;
1124 
1125  /* we need some permutation to store matrices,
1126  * until MPV_common_init() sets the real permutation. */
1127  for (i = 0; i < 64; i++)
1128  s2->dsp.idct_permutation[i]=i;
1129 
1130  MPV_decode_defaults(s2);
1131 
1132  s->mpeg_enc_ctx.avctx = avctx;
1133  s->mpeg_enc_ctx.flags = avctx->flags;
1134  s->mpeg_enc_ctx.flags2 = avctx->flags2;
1137 
1138  s->mpeg_enc_ctx_allocated = 0;
1140  s->repeat_field = 0;
1141  s->mpeg_enc_ctx.codec_id = avctx->codec->id;
1142  avctx->color_range = AVCOL_RANGE_MPEG;
1143  if (avctx->codec->id == CODEC_ID_MPEG1VIDEO)
1145  else
1147  return 0;
1148 }
1149 
1151 {
1152  Mpeg1Context *ctx = avctx->priv_data, *ctx_from = avctx_from->priv_data;
1153  MpegEncContext *s = &ctx->mpeg_enc_ctx, *s1 = &ctx_from->mpeg_enc_ctx;
1154  int err;
1155 
1156  if (avctx == avctx_from || !ctx_from->mpeg_enc_ctx_allocated || !s1->context_initialized)
1157  return 0;
1158 
1159  err = ff_mpeg_update_thread_context(avctx, avctx_from);
1160  if (err) return err;
1161 
1162  if (!ctx->mpeg_enc_ctx_allocated)
1163  memcpy(s + 1, s1 + 1, sizeof(Mpeg1Context) - sizeof(MpegEncContext));
1164 
1165  if (!(s->pict_type == AV_PICTURE_TYPE_B || s->low_delay))
1166  s->picture_number++;
1167 
1168  return 0;
1169 }
1170 
1171 static void quant_matrix_rebuild(uint16_t *matrix, const uint8_t *old_perm,
1172  const uint8_t *new_perm)
1173 {
1174  uint16_t temp_matrix[64];
1175  int i;
1176 
1177  memcpy(temp_matrix, matrix, 64 * sizeof(uint16_t));
1178 
1179  for (i = 0; i < 64; i++) {
1180  matrix[new_perm[i]] = temp_matrix[old_perm[i]];
1181  }
1182 }
1183 
1184 static const enum PixelFormat pixfmt_xvmc_mpg2_420[] = {
1187  PIX_FMT_NONE };
1188 
1190 {
1191  Mpeg1Context *s1 = avctx->priv_data;
1192  MpegEncContext *s = &s1->mpeg_enc_ctx;
1193 
1194  if (avctx->xvmc_acceleration)
1195  return avctx->get_format(avctx, pixfmt_xvmc_mpg2_420);
1196  else if (avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU) {
1197  if (avctx->codec_id == CODEC_ID_MPEG1VIDEO)
1198  return PIX_FMT_VDPAU_MPEG1;
1199  else
1200  return PIX_FMT_VDPAU_MPEG2;
1201  } else {
1202  if (s->chroma_format < 2)
1203  return avctx->get_format(avctx, ff_hwaccel_pixfmt_list_420);
1204  else if (s->chroma_format == 2)
1205  return PIX_FMT_YUV422P;
1206  else
1207  return PIX_FMT_YUV444P;
1208  }
1209 }
1210 
1211 /* Call this function when we know all parameters.
1212  * It may be called in different places for MPEG-1 and MPEG-2. */
1214 {
1215  Mpeg1Context *s1 = avctx->priv_data;
1216  MpegEncContext *s = &s1->mpeg_enc_ctx;
1217  uint8_t old_permutation[64];
1218 
1219  if ((s1->mpeg_enc_ctx_allocated == 0) ||
1220  avctx->coded_width != s->width ||
1221  avctx->coded_height != s->height ||
1222  s1->save_width != s->width ||
1223  s1->save_height != s->height ||
1224  s1->save_aspect_info != s->aspect_ratio_info ||
1226  0)
1227  {
1228 
1229  if (s1->mpeg_enc_ctx_allocated) {
1230  ParseContext pc = s->parse_context;
1231  s->parse_context.buffer = 0;
1232  MPV_common_end(s);
1233  s->parse_context = pc;
1234  }
1235 
1236  if ((s->width == 0) || (s->height == 0))
1237  return -2;
1238 
1239  avcodec_set_dimensions(avctx, s->width, s->height);
1240  avctx->bit_rate = s->bit_rate;
1242  s1->save_width = s->width;
1243  s1->save_height = s->height;
1245 
1246  /* low_delay may be forced, in this case we will have B-frames
1247  * that behave like P-frames. */
1248  avctx->has_b_frames = !s->low_delay;
1249 
1250  assert((avctx->sub_id == 1) == (avctx->codec_id == CODEC_ID_MPEG1VIDEO));
1251  if (avctx->codec_id == CODEC_ID_MPEG1VIDEO) {
1252  //MPEG-1 fps
1255  //MPEG-1 aspect
1257  avctx->ticks_per_frame=1;
1258  } else {//MPEG-2
1259  //MPEG-2 fps
1261  &s->avctx->time_base.num,
1264  1 << 30);
1265  avctx->ticks_per_frame = 2;
1266  //MPEG-2 aspect
1267  if (s->aspect_ratio_info > 1) {
1268  AVRational dar =
1270  (AVRational) {s1->pan_scan.width, s1->pan_scan.height}),
1271  (AVRational) {s->width, s->height});
1272 
1273  // we ignore the spec here and guess a bit as reality does not match the spec, see for example
1274  // res_change_ffmpeg_aspect.ts and sequence-display-aspect.mpg
1275  // issue1613, 621, 562
1276  if ((s1->pan_scan.width == 0) || (s1->pan_scan.height == 0) ||
1277  (av_cmp_q(dar, (AVRational) {4, 3}) && av_cmp_q(dar, (AVRational) {16, 9}))) {
1280  (AVRational) {s->width, s->height});
1281  } else {
1284  (AVRational) {s1->pan_scan.width, s1->pan_scan.height});
1285 //issue1613 4/3 16/9 -> 16/9
1286 //res_change_ffmpeg_aspect.ts 4/3 225/44 ->4/3
1287 //widescreen-issue562.mpg 4/3 16/9 -> 16/9
1288 // s->avctx->sample_aspect_ratio = av_mul_q(s->avctx->sample_aspect_ratio, (AVRational) {s->width, s->height});
1289 //av_log(NULL, AV_LOG_ERROR, "A %d/%d\n", ff_mpeg2_aspect[s->aspect_ratio_info].num, ff_mpeg2_aspect[s->aspect_ratio_info].den);
1290 //av_log(NULL, AV_LOG_ERROR, "B %d/%d\n", s->avctx->sample_aspect_ratio.num, s->avctx->sample_aspect_ratio.den);
1291  }
1292  } else {
1293  s->avctx->sample_aspect_ratio =
1294  ff_mpeg2_aspect[s->aspect_ratio_info];
1295  }
1296  } // MPEG-2
1297 
1298  avctx->pix_fmt = mpeg_get_pixelformat(avctx);
1299  avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
1300  // until then pix_fmt may be changed right after codec init
1301  if (avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT ||
1302  avctx->hwaccel ||
1303  s->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU)
1304  if (avctx->idct_algo == FF_IDCT_AUTO)
1305  avctx->idct_algo = FF_IDCT_SIMPLE;
1306 
1307  /* Quantization matrices may need reordering
1308  * if DCT permutation is changed. */
1309  memcpy(old_permutation, s->dsp.idct_permutation, 64 * sizeof(uint8_t));
1310 
1311  if (MPV_common_init(s) < 0)
1312  return -2;
1313 
1314  quant_matrix_rebuild(s->intra_matrix, old_permutation, s->dsp.idct_permutation);
1315  quant_matrix_rebuild(s->inter_matrix, old_permutation, s->dsp.idct_permutation);
1316  quant_matrix_rebuild(s->chroma_intra_matrix, old_permutation, s->dsp.idct_permutation);
1317  quant_matrix_rebuild(s->chroma_inter_matrix, old_permutation, s->dsp.idct_permutation);
1318 
1319  s1->mpeg_enc_ctx_allocated = 1;
1320  }
1321  return 0;
1322 }
1323 
1325  const uint8_t *buf, int buf_size)
1326 {
1327  Mpeg1Context *s1 = avctx->priv_data;
1328  MpegEncContext *s = &s1->mpeg_enc_ctx;
1329  int ref, f_code, vbv_delay;
1330 
1331  init_get_bits(&s->gb, buf, buf_size*8);
1332 
1333  ref = get_bits(&s->gb, 10); /* temporal ref */
1334  s->pict_type = get_bits(&s->gb, 3);
1335  if (s->pict_type == 0 || s->pict_type > 3)
1336  return -1;
1337 
1338  vbv_delay = get_bits(&s->gb, 16);
1340  s->full_pel[0] = get_bits1(&s->gb);
1341  f_code = get_bits(&s->gb, 3);
1342  if (f_code == 0 && (avctx->err_recognition & AV_EF_BITSTREAM))
1343  return -1;
1344  s->mpeg_f_code[0][0] = f_code;
1345  s->mpeg_f_code[0][1] = f_code;
1346  }
1347  if (s->pict_type == AV_PICTURE_TYPE_B) {
1348  s->full_pel[1] = get_bits1(&s->gb);
1349  f_code = get_bits(&s->gb, 3);
1350  if (f_code == 0 && (avctx->err_recognition & AV_EF_BITSTREAM))
1351  return -1;
1352  s->mpeg_f_code[1][0] = f_code;
1353  s->mpeg_f_code[1][1] = f_code;
1354  }
1357 
1358  if (avctx->debug & FF_DEBUG_PICT_INFO)
1359  av_log(avctx, AV_LOG_DEBUG, "vbv_delay %d, ref %d type:%d\n", vbv_delay, ref, s->pict_type);
1360 
1361  s->y_dc_scale = 8;
1362  s->c_dc_scale = 8;
1363  return 0;
1364 }
1365 
1367 {
1368  MpegEncContext *s= &s1->mpeg_enc_ctx;
1369  int horiz_size_ext, vert_size_ext;
1370  int bit_rate_ext;
1371 
1372  skip_bits(&s->gb, 1); /* profile and level esc*/
1373  s->avctx->profile = get_bits(&s->gb, 3);
1374  s->avctx->level = get_bits(&s->gb, 4);
1375  s->progressive_sequence = get_bits1(&s->gb); /* progressive_sequence */
1376  s->chroma_format = get_bits(&s->gb, 2); /* chroma_format 1=420, 2=422, 3=444 */
1377  horiz_size_ext = get_bits(&s->gb, 2);
1378  vert_size_ext = get_bits(&s->gb, 2);
1379  s->width |= (horiz_size_ext << 12);
1380  s->height |= (vert_size_ext << 12);
1381  bit_rate_ext = get_bits(&s->gb, 12); /* XXX: handle it */
1382  s->bit_rate += (bit_rate_ext << 18) * 400;
1383  skip_bits1(&s->gb); /* marker */
1384  s->avctx->rc_buffer_size += get_bits(&s->gb, 8) * 1024 * 16 << 10;
1385 
1386  s->low_delay = get_bits1(&s->gb);
1387  if (s->flags & CODEC_FLAG_LOW_DELAY)
1388  s->low_delay = 1;
1389 
1390  s1->frame_rate_ext.num = get_bits(&s->gb, 2) + 1;
1391  s1->frame_rate_ext.den = get_bits(&s->gb, 5) + 1;
1392 
1393  av_dlog(s->avctx, "sequence extension\n");
1395  s->avctx->sub_id = 2; /* indicates MPEG-2 found */
1396 
1397  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1398  av_log(s->avctx, AV_LOG_DEBUG, "profile: %d, level: %d vbv buffer: %d, bitrate:%d\n",
1399  s->avctx->profile, s->avctx->level, s->avctx->rc_buffer_size, s->bit_rate);
1400 
1401 }
1402 
1404 {
1405  MpegEncContext *s = &s1->mpeg_enc_ctx;
1406  int color_description, w, h;
1407 
1408  skip_bits(&s->gb, 3); /* video format */
1409  color_description = get_bits1(&s->gb);
1410  if (color_description) {
1411  s->avctx->color_primaries = get_bits(&s->gb, 8);
1412  s->avctx->color_trc = get_bits(&s->gb, 8);
1413  s->avctx->colorspace = get_bits(&s->gb, 8);
1414  }
1415  w = get_bits(&s->gb, 14);
1416  skip_bits(&s->gb, 1); //marker
1417  h = get_bits(&s->gb, 14);
1418  // remaining 3 bits are zero padding
1419 
1420  s1->pan_scan.width = 16 * w;
1421  s1->pan_scan.height = 16 * h;
1422 
1423  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1424  av_log(s->avctx, AV_LOG_DEBUG, "sde w:%d, h:%d\n", w, h);
1425 }
1426 
1428 {
1429  MpegEncContext *s = &s1->mpeg_enc_ctx;
1430  int i, nofco;
1431 
1432  nofco = 1;
1433  if (s->progressive_sequence) {
1434  if (s->repeat_first_field) {
1435  nofco++;
1436  if (s->top_field_first)
1437  nofco++;
1438  }
1439  } else {
1440  if (s->picture_structure == PICT_FRAME) {
1441  nofco++;
1442  if (s->repeat_first_field)
1443  nofco++;
1444  }
1445  }
1446  for (i = 0; i < nofco; i++) {
1447  s1->pan_scan.position[i][0] = get_sbits(&s->gb, 16);
1448  skip_bits(&s->gb, 1); // marker
1449  s1->pan_scan.position[i][1] = get_sbits(&s->gb, 16);
1450  skip_bits(&s->gb, 1); // marker
1451  }
1452 
1453  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1454  av_log(s->avctx, AV_LOG_DEBUG, "pde (%d,%d) (%d,%d) (%d,%d)\n",
1455  s1->pan_scan.position[0][0], s1->pan_scan.position[0][1],
1456  s1->pan_scan.position[1][0], s1->pan_scan.position[1][1],
1457  s1->pan_scan.position[2][0], s1->pan_scan.position[2][1]);
1458 }
1459 
1460 static int load_matrix(MpegEncContext *s, uint16_t matrix0[64], uint16_t matrix1[64], int intra)
1461 {
1462  int i;
1463 
1464  for (i = 0; i < 64; i++) {
1465  int j = s->dsp.idct_permutation[ff_zigzag_direct[i]];
1466  int v = get_bits(&s->gb, 8);
1467  if (v == 0) {
1468  av_log(s->avctx, AV_LOG_ERROR, "matrix damaged\n");
1469  return -1;
1470  }
1471  if (intra && i == 0 && v != 8) {
1472  av_log(s->avctx, AV_LOG_ERROR, "intra matrix invalid, ignoring\n");
1473  v = 8; // needed by pink.mpg / issue1046
1474  }
1475  matrix0[j] = v;
1476  if (matrix1)
1477  matrix1[j] = v;
1478  }
1479  return 0;
1480 }
1481 
1483 {
1484  av_dlog(s->avctx, "matrix extension\n");
1485 
1486  if (get_bits1(&s->gb)) load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
1487  if (get_bits1(&s->gb)) load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
1488  if (get_bits1(&s->gb)) load_matrix(s, s->chroma_intra_matrix, NULL , 1);
1489  if (get_bits1(&s->gb)) load_matrix(s, s->chroma_inter_matrix, NULL , 0);
1490 }
1491 
1493 {
1494  MpegEncContext *s = &s1->mpeg_enc_ctx;
1495 
1496  s->full_pel[0] = s->full_pel[1] = 0;
1497  s->mpeg_f_code[0][0] = get_bits(&s->gb, 4);
1498  s->mpeg_f_code[0][1] = get_bits(&s->gb, 4);
1499  s->mpeg_f_code[1][0] = get_bits(&s->gb, 4);
1500  s->mpeg_f_code[1][1] = get_bits(&s->gb, 4);
1501  if (!s->pict_type && s1->mpeg_enc_ctx_allocated) {
1502  av_log(s->avctx, AV_LOG_ERROR, "Missing picture start code, guessing missing values\n");
1503  if (s->mpeg_f_code[1][0] == 15 && s->mpeg_f_code[1][1] == 15) {
1504  if (s->mpeg_f_code[0][0] == 15 && s->mpeg_f_code[0][1] == 15)
1506  else
1508  } else
1512  }
1513  s->intra_dc_precision = get_bits(&s->gb, 2);
1514  s->picture_structure = get_bits(&s->gb, 2);
1515  s->top_field_first = get_bits1(&s->gb);
1516  s->frame_pred_frame_dct = get_bits1(&s->gb);
1518  s->q_scale_type = get_bits1(&s->gb);
1519  s->intra_vlc_format = get_bits1(&s->gb);
1520  s->alternate_scan = get_bits1(&s->gb);
1521  s->repeat_first_field = get_bits1(&s->gb);
1522  s->chroma_420_type = get_bits1(&s->gb);
1523  s->progressive_frame = get_bits1(&s->gb);
1524 
1525  if (s->progressive_sequence && !s->progressive_frame) {
1526  s->progressive_frame = 1;
1527  av_log(s->avctx, AV_LOG_ERROR, "interlaced frame in progressive sequence, ignoring\n");
1528  }
1529 
1530  if (s->picture_structure == 0 || (s->progressive_frame && s->picture_structure != PICT_FRAME)) {
1531  av_log(s->avctx, AV_LOG_ERROR, "picture_structure %d invalid, ignoring\n", s->picture_structure);
1533  }
1534 
1536  av_log(s->avctx, AV_LOG_ERROR, "invalid frame_pred_frame_dct\n");
1537  s->frame_pred_frame_dct = 1;
1538  }
1539 
1540  if (s->picture_structure == PICT_FRAME) {
1541  s->first_field = 0;
1542  s->v_edge_pos = 16 * s->mb_height;
1543  } else {
1544  s->first_field ^= 1;
1545  s->v_edge_pos = 8 * s->mb_height;
1546  memset(s->mbskip_table, 0, s->mb_stride * s->mb_height);
1547  }
1548 
1549  if (s->alternate_scan) {
1552  } else {
1555  }
1556 
1557  /* composite display not parsed */
1558  av_dlog(s->avctx, "intra_dc_precision=%d\n", s->intra_dc_precision);
1559  av_dlog(s->avctx, "picture_structure=%d\n", s->picture_structure);
1560  av_dlog(s->avctx, "top field first=%d\n", s->top_field_first);
1561  av_dlog(s->avctx, "repeat first field=%d\n", s->repeat_first_field);
1562  av_dlog(s->avctx, "conceal=%d\n", s->concealment_motion_vectors);
1563  av_dlog(s->avctx, "intra_vlc_format=%d\n", s->intra_vlc_format);
1564  av_dlog(s->avctx, "alternate_scan=%d\n", s->alternate_scan);
1565  av_dlog(s->avctx, "frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
1566  av_dlog(s->avctx, "progressive_frame=%d\n", s->progressive_frame);
1567 }
1568 
1569 static int mpeg_field_start(MpegEncContext *s, const uint8_t *buf, int buf_size)
1570 {
1571  AVCodecContext *avctx = s->avctx;
1572  Mpeg1Context *s1 = (Mpeg1Context*)s;
1573 
1574  /* start frame decoding */
1575  if (s->first_field || s->picture_structure == PICT_FRAME) {
1576  if (MPV_frame_start(s, avctx) < 0)
1577  return -1;
1578 
1579  ff_er_frame_start(s);
1580 
1581  /* first check if we must repeat the frame */
1583  if (s->repeat_first_field) {
1584  if (s->progressive_sequence) {
1585  if (s->top_field_first)
1587  else
1589  } else if (s->progressive_frame) {
1591  }
1592  }
1593 
1595 
1597  ff_thread_finish_setup(avctx);
1598  } else { // second field
1599  int i;
1600 
1601  if (!s->current_picture_ptr) {
1602  av_log(s->avctx, AV_LOG_ERROR, "first field missing\n");
1603  return -1;
1604  }
1605 
1606  for (i = 0; i < 4; i++) {
1610  }
1611  }
1612  }
1613 
1614  if (avctx->hwaccel) {
1615  if (avctx->hwaccel->start_frame(avctx, buf, buf_size) < 0)
1616  return -1;
1617  }
1618 
1619 // MPV_frame_start will call this function too,
1620 // but we need to call it on every field
1622  if (ff_xvmc_field_start(s, avctx) < 0)
1623  return -1;
1624 
1625  return 0;
1626 }
1627 
1628 #define DECODE_SLICE_ERROR -1
1629 #define DECODE_SLICE_OK 0
1630 
1637 static int mpeg_decode_slice(MpegEncContext *s, int mb_y,
1638  const uint8_t **buf, int buf_size)
1639 {
1640  AVCodecContext *avctx = s->avctx;
1641  const int lowres = s->avctx->lowres;
1642  const int field_pic = s->picture_structure != PICT_FRAME;
1643 
1644  s->resync_mb_x =
1645  s->resync_mb_y = -1;
1646 
1647  assert(mb_y < s->mb_height);
1648 
1649  init_get_bits(&s->gb, *buf, buf_size * 8);
1650 
1652  s->interlaced_dct = 0;
1653 
1654  s->qscale = get_qscale(s);
1655 
1656  if (s->qscale == 0) {
1657  av_log(s->avctx, AV_LOG_ERROR, "qscale == 0\n");
1658  return -1;
1659  }
1660 
1661  /* extra slice info */
1662  while (get_bits1(&s->gb) != 0) {
1663  skip_bits(&s->gb, 8);
1664  }
1665 
1666  s->mb_x = 0;
1667 
1668  if (mb_y == 0 && s->codec_tag == AV_RL32("SLIF")) {
1669  skip_bits1(&s->gb);
1670  } else {
1671  while (get_bits_left(&s->gb) > 0) {
1672  int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
1673  if (code < 0) {
1674  av_log(s->avctx, AV_LOG_ERROR, "first mb_incr damaged\n");
1675  return -1;
1676  }
1677  if (code >= 33) {
1678  if (code == 33) {
1679  s->mb_x += 33;
1680  }
1681  /* otherwise, stuffing, nothing to do */
1682  } else {
1683  s->mb_x += code;
1684  break;
1685  }
1686  }
1687  }
1688 
1689  if (s->mb_x >= (unsigned)s->mb_width) {
1690  av_log(s->avctx, AV_LOG_ERROR, "initial skip overflow\n");
1691  return -1;
1692  }
1693 
1694  if (avctx->hwaccel) {
1695  const uint8_t *buf_end, *buf_start = *buf - 4; /* include start_code */
1696  int start_code = -1;
1697  buf_end = avpriv_mpv_find_start_code(buf_start + 2, *buf + buf_size, &start_code);
1698  if (buf_end < *buf + buf_size)
1699  buf_end -= 4;
1700  s->mb_y = mb_y;
1701  if (avctx->hwaccel->decode_slice(avctx, buf_start, buf_end - buf_start) < 0)
1702  return DECODE_SLICE_ERROR;
1703  *buf = buf_end;
1704  return DECODE_SLICE_OK;
1705  }
1706 
1707  s->resync_mb_x = s->mb_x;
1708  s->resync_mb_y = s->mb_y = mb_y;
1709  s->mb_skip_run = 0;
1711 
1712  if (s->mb_y == 0 && s->mb_x == 0 && (s->first_field || s->picture_structure == PICT_FRAME)) {
1713  if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
1714  av_log(s->avctx, AV_LOG_DEBUG, "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",
1715  s->qscale, s->mpeg_f_code[0][0], s->mpeg_f_code[0][1], s->mpeg_f_code[1][0], s->mpeg_f_code[1][1],
1716  s->pict_type == AV_PICTURE_TYPE_I ? "I" : (s->pict_type == AV_PICTURE_TYPE_P ? "P" : (s->pict_type == AV_PICTURE_TYPE_B ? "B" : "S")),
1717  s->progressive_sequence ? "ps" :"", s->progressive_frame ? "pf" : "", s->alternate_scan ? "alt" :"", s->top_field_first ? "top" :"",
1720  }
1721  }
1722 
1723  for (;;) {
1724  // If 1, we memcpy blocks in xvmcvideo.
1726  ff_xvmc_init_block(s); // set s->block
1727 
1728  if (mpeg_decode_mb(s, s->block) < 0)
1729  return -1;
1730 
1731  if (s->current_picture.f.motion_val[0] && !s->encoding) { // note motion_val is normally NULL unless we want to extract the MVs
1732  const int wrap = s->b8_stride;
1733  int xy = s->mb_x * 2 + s->mb_y * 2 * wrap;
1734  int b8_xy = 4 * (s->mb_x + s->mb_y * s->mb_stride);
1735  int motion_x, motion_y, dir, i;
1736 
1737  for (i = 0; i < 2; i++) {
1738  for (dir = 0; dir < 2; dir++) {
1739  if (s->mb_intra || (dir == 1 && s->pict_type != AV_PICTURE_TYPE_B)) {
1740  motion_x = motion_y = 0;
1741  } else if (s->mv_type == MV_TYPE_16X16 || (s->mv_type == MV_TYPE_FIELD && field_pic)) {
1742  motion_x = s->mv[dir][0][0];
1743  motion_y = s->mv[dir][0][1];
1744  } else /*if ((s->mv_type == MV_TYPE_FIELD) || (s->mv_type == MV_TYPE_16X8))*/ {
1745  motion_x = s->mv[dir][i][0];
1746  motion_y = s->mv[dir][i][1];
1747  }
1748 
1749  s->current_picture.f.motion_val[dir][xy ][0] = motion_x;
1750  s->current_picture.f.motion_val[dir][xy ][1] = motion_y;
1751  s->current_picture.f.motion_val[dir][xy + 1][0] = motion_x;
1752  s->current_picture.f.motion_val[dir][xy + 1][1] = motion_y;
1753  s->current_picture.f.ref_index [dir][b8_xy ] =
1754  s->current_picture.f.ref_index [dir][b8_xy + 1] = s->field_select[dir][i];
1755  assert(s->field_select[dir][i] == 0 || s->field_select[dir][i] == 1);
1756  }
1757  xy += wrap;
1758  b8_xy +=2;
1759  }
1760  }
1761 
1762  s->dest[0] += 16 >> lowres;
1763  s->dest[1] +=(16 >> lowres) >> s->chroma_x_shift;
1764  s->dest[2] +=(16 >> lowres) >> s->chroma_x_shift;
1765 
1766  MPV_decode_mb(s, s->block);
1767 
1768  if (++s->mb_x >= s->mb_width) {
1769  const int mb_size = 16 >> s->avctx->lowres;
1770 
1771  ff_draw_horiz_band(s, mb_size*(s->mb_y >> field_pic), mb_size);
1773 
1774  s->mb_x = 0;
1775  s->mb_y += 1 << field_pic;
1776 
1777  if (s->mb_y >= s->mb_height) {
1778  int left = get_bits_left(&s->gb);
1779  int is_d10 = s->chroma_format == 2 && s->pict_type == AV_PICTURE_TYPE_I && avctx->profile == 0 && avctx->level == 5
1780  && s->intra_dc_precision == 2 && s->q_scale_type == 1 && s->alternate_scan == 0
1781  && s->progressive_frame == 0 /* vbv_delay == 0xBBB || 0xE10*/;
1782 
1783  if (left < 0 || (left && show_bits(&s->gb, FFMIN(left, 23)) && !is_d10)
1784  || ((avctx->err_recognition & AV_EF_BUFFER) && left > 8)) {
1785  av_log(avctx, AV_LOG_ERROR, "end mismatch left=%d %0X\n", left, show_bits(&s->gb, FFMIN(left, 23)));
1786  return -1;
1787  } else
1788  goto eos;
1789  }
1790 
1792  }
1793 
1794  /* skip mb handling */
1795  if (s->mb_skip_run == -1) {
1796  /* read increment again */
1797  s->mb_skip_run = 0;
1798  for (;;) {
1799  int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
1800  if (code < 0) {
1801  av_log(s->avctx, AV_LOG_ERROR, "mb incr damaged\n");
1802  return -1;
1803  }
1804  if (code >= 33) {
1805  if (code == 33) {
1806  s->mb_skip_run += 33;
1807  } else if (code == 35) {
1808  if (s->mb_skip_run != 0 || show_bits(&s->gb, 15) != 0) {
1809  av_log(s->avctx, AV_LOG_ERROR, "slice mismatch\n");
1810  return -1;
1811  }
1812  goto eos; /* end of slice */
1813  }
1814  /* otherwise, stuffing, nothing to do */
1815  } else {
1816  s->mb_skip_run += code;
1817  break;
1818  }
1819  }
1820  if (s->mb_skip_run) {
1821  int i;
1822  if (s->pict_type == AV_PICTURE_TYPE_I) {
1823  av_log(s->avctx, AV_LOG_ERROR, "skipped MB in I frame at %d %d\n", s->mb_x, s->mb_y);
1824  return -1;
1825  }
1826 
1827  /* skip mb */
1828  s->mb_intra = 0;
1829  for (i = 0; i < 12; i++)
1830  s->block_last_index[i] = -1;
1831  if (s->picture_structure == PICT_FRAME)
1832  s->mv_type = MV_TYPE_16X16;
1833  else
1834  s->mv_type = MV_TYPE_FIELD;
1835  if (s->pict_type == AV_PICTURE_TYPE_P) {
1836  /* if P type, zero motion vector is implied */
1837  s->mv_dir = MV_DIR_FORWARD;
1838  s->mv[0][0][0] = s->mv[0][0][1] = 0;
1839  s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
1840  s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
1841  s->field_select[0][0] = (s->picture_structure - 1) & 1;
1842  } else {
1843  /* if B type, reuse previous vectors and directions */
1844  s->mv[0][0][0] = s->last_mv[0][0][0];
1845  s->mv[0][0][1] = s->last_mv[0][0][1];
1846  s->mv[1][0][0] = s->last_mv[1][0][0];
1847  s->mv[1][0][1] = s->last_mv[1][0][1];
1848  }
1849  }
1850  }
1851  }
1852 eos: // end of slice
1853  *buf += (get_bits_count(&s->gb)-1)/8;
1854 //printf("y %d %d %d %d\n", s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y);
1855  return 0;
1856 }
1857 
1858 static int slice_decode_thread(AVCodecContext *c, void *arg)
1859 {
1860  MpegEncContext *s = *(void**)arg;
1861  const uint8_t *buf = s->gb.buffer;
1862  int mb_y = s->start_mb_y;
1863  const int field_pic = s->picture_structure != PICT_FRAME;
1864 
1865  s->error_count = (3 * (s->end_mb_y - s->start_mb_y) * s->mb_width) >> field_pic;
1866 
1867  for (;;) {
1868  uint32_t start_code;
1869  int ret;
1870 
1871  ret = mpeg_decode_slice(s, mb_y, &buf, s->gb.buffer_end - buf);
1872  emms_c();
1873 //av_log(c, AV_LOG_DEBUG, "ret:%d resync:%d/%d mb:%d/%d ts:%d/%d ec:%d\n",
1874 //ret, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, s->start_mb_y, s->end_mb_y, s->error_count);
1875  if (ret < 0) {
1876  if (c->err_recognition & AV_EF_EXPLODE)
1877  return ret;
1878  if (s->resync_mb_x >= 0 && s->resync_mb_y >= 0)
1880  } else {
1882  }
1883 
1884  if (s->mb_y == s->end_mb_y)
1885  return 0;
1886 
1887  start_code = -1;
1888  buf = avpriv_mpv_find_start_code(buf, s->gb.buffer_end, &start_code);
1889  mb_y= (start_code - SLICE_MIN_START_CODE) << field_pic;
1891  mb_y++;
1892  if (mb_y < 0 || mb_y >= s->end_mb_y)
1893  return -1;
1894  }
1895 }
1896 
1901 static int slice_end(AVCodecContext *avctx, AVFrame *pict)
1902 {
1903  Mpeg1Context *s1 = avctx->priv_data;
1904  MpegEncContext *s = &s1->mpeg_enc_ctx;
1905 
1907  return 0;
1908 
1909  if (s->avctx->hwaccel) {
1910  if (s->avctx->hwaccel->end_frame(s->avctx) < 0)
1911  av_log(avctx, AV_LOG_ERROR, "hardware accelerator failed to decode picture\n");
1912  }
1913 
1915  ff_xvmc_field_end(s);
1916 
1917  /* end of slice reached */
1918  if (/*s->mb_y << field_pic == s->mb_height &&*/ !s->first_field) {
1919  /* end of image */
1920 
1922 
1923  ff_er_frame_end(s);
1924 
1925  MPV_frame_end(s);
1926 
1927  if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
1928  *pict = *(AVFrame*)s->current_picture_ptr;
1929  ff_print_debug_info(s, pict);
1930  } else {
1931  if (avctx->active_thread_type & FF_THREAD_FRAME)
1932  s->picture_number++;
1933  /* latency of 1 frame for I- and P-frames */
1934  /* XXX: use another variable than picture_number */
1935  if (s->last_picture_ptr != NULL) {
1936  *pict = *(AVFrame*)s->last_picture_ptr;
1937  ff_print_debug_info(s, pict);
1938  }
1939  }
1940 
1941  return 1;
1942  } else {
1943  return 0;
1944  }
1945 }
1946 
1948  const uint8_t *buf, int buf_size)
1949 {
1950  Mpeg1Context *s1 = avctx->priv_data;
1951  MpegEncContext *s = &s1->mpeg_enc_ctx;
1952  int width, height;
1953  int i, v, j;
1954 
1955  init_get_bits(&s->gb, buf, buf_size*8);
1956 
1957  width = get_bits(&s->gb, 12);
1958  height = get_bits(&s->gb, 12);
1959  if (width <= 0 || height <= 0)
1960  return -1;
1961  s->aspect_ratio_info = get_bits(&s->gb, 4);
1962  if (s->aspect_ratio_info == 0) {
1963  av_log(avctx, AV_LOG_ERROR, "aspect ratio has forbidden 0 value\n");
1964  if (avctx->err_recognition & AV_EF_BITSTREAM)
1965  return -1;
1966  }
1967  s->frame_rate_index = get_bits(&s->gb, 4);
1968  if (s->frame_rate_index == 0 || s->frame_rate_index > 13)
1969  return -1;
1970  s->bit_rate = get_bits(&s->gb, 18) * 400;
1971  if (get_bits1(&s->gb) == 0) /* marker */
1972  return -1;
1973  s->width = width;
1974  s->height = height;
1975 
1976  s->avctx->rc_buffer_size = get_bits(&s->gb, 10) * 1024 * 16;
1977  skip_bits(&s->gb, 1);
1978 
1979  /* get matrix */
1980  if (get_bits1(&s->gb)) {
1982  } else {
1983  for (i = 0; i < 64; i++) {
1984  j = s->dsp.idct_permutation[i];
1986  s->intra_matrix[j] = v;
1987  s->chroma_intra_matrix[j] = v;
1988  }
1989  }
1990  if (get_bits1(&s->gb)) {
1992  } else {
1993  for (i = 0; i < 64; i++) {
1994  int j = s->dsp.idct_permutation[i];
1996  s->inter_matrix[j] = v;
1997  s->chroma_inter_matrix[j] = v;
1998  }
1999  }
2000 
2001  if (show_bits(&s->gb, 23) != 0) {
2002  av_log(s->avctx, AV_LOG_ERROR, "sequence header damaged\n");
2003  return -1;
2004  }
2005 
2006  /* we set MPEG-2 parameters so that it emulates MPEG-1 */
2007  s->progressive_sequence = 1;
2008  s->progressive_frame = 1;
2010  s->frame_pred_frame_dct = 1;
2011  s->chroma_format = 1;
2013  avctx->sub_id = 1; /* indicates MPEG-1 */
2014  s->out_format = FMT_MPEG1;
2015  s->swap_uv = 0; // AFAIK VCR2 does not have SEQ_HEADER
2016  if (s->flags & CODEC_FLAG_LOW_DELAY)
2017  s->low_delay = 1;
2018 
2019  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
2020  av_log(s->avctx, AV_LOG_DEBUG, "vbv buffer: %d, bitrate:%d\n",
2021  s->avctx->rc_buffer_size, s->bit_rate);
2022 
2023  return 0;
2024 }
2025 
2027 {
2028  Mpeg1Context *s1 = avctx->priv_data;
2029  MpegEncContext *s = &s1->mpeg_enc_ctx;
2030  int i, v;
2031 
2032  /* start new MPEG-1 context decoding */
2033  s->out_format = FMT_MPEG1;
2034  if (s1->mpeg_enc_ctx_allocated) {
2035  MPV_common_end(s);
2036  }
2037  s->width = avctx->coded_width;
2038  s->height = avctx->coded_height;
2039  avctx->has_b_frames = 0; // true?
2040  s->low_delay = 1;
2041 
2042  avctx->pix_fmt = mpeg_get_pixelformat(avctx);
2043  avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
2044 
2045  if (avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT || avctx->hwaccel ||
2047  if (avctx->idct_algo == FF_IDCT_AUTO)
2048  avctx->idct_algo = FF_IDCT_SIMPLE;
2049 
2050  if (MPV_common_init(s) < 0)
2051  return -1;
2052  exchange_uv(s); // common init reset pblocks, so we swap them here
2053  s->swap_uv = 1; // in case of xvmc we need to swap uv for each MB
2054  s1->mpeg_enc_ctx_allocated = 1;
2055 
2056  for (i = 0; i < 64; i++) {
2057  int j = s->dsp.idct_permutation[i];
2059  s->intra_matrix[j] = v;
2060  s->chroma_intra_matrix[j] = v;
2061 
2063  s->inter_matrix[j] = v;
2064  s->chroma_inter_matrix[j] = v;
2065  }
2066 
2067  s->progressive_sequence = 1;
2068  s->progressive_frame = 1;
2070  s->frame_pred_frame_dct = 1;
2071  s->chroma_format = 1;
2073  avctx->sub_id = 2; /* indicates MPEG-2 */
2074  s1->save_width = s->width;
2075  s1->save_height = s->height;
2077  return 0;
2078 }
2079 
2080 
2082  const uint8_t *p, int buf_size)
2083 {
2084  const uint8_t *buf_end = p + buf_size;
2085 
2086  /* we parse the DTG active format information */
2087  if (buf_end - p >= 5 &&
2088  p[0] == 'D' && p[1] == 'T' && p[2] == 'G' && p[3] == '1') {
2089  int flags = p[4];
2090  p += 5;
2091  if (flags & 0x80) {
2092  /* skip event id */
2093  p += 2;
2094  }
2095  if (flags & 0x40) {
2096  if (buf_end - p < 1)
2097  return;
2098  avctx->dtg_active_format = p[0] & 0x0f;
2099  }
2100  }
2101 }
2102 
2103 static void mpeg_decode_gop(AVCodecContext *avctx,
2104  const uint8_t *buf, int buf_size)
2105 {
2106  Mpeg1Context *s1 = avctx->priv_data;
2107  MpegEncContext *s = &s1->mpeg_enc_ctx;
2108 
2109  int time_code_hours, time_code_minutes;
2110  int time_code_seconds, time_code_pictures;
2111  int broken_link;
2112 
2113  init_get_bits(&s->gb, buf, buf_size*8);
2114 
2115  skip_bits1(&s->gb); /* drop_frame_flag */
2116 
2117  time_code_hours = get_bits(&s->gb, 5);
2118  time_code_minutes = get_bits(&s->gb, 6);
2119  skip_bits1(&s->gb); // marker bit
2120  time_code_seconds = get_bits(&s->gb, 6);
2121  time_code_pictures = get_bits(&s->gb, 6);
2122 
2123  s1->closed_gop = get_bits1(&s->gb);
2124  /*broken_link indicate that after editing the
2125  reference frames of the first B-Frames after GOP I-Frame
2126  are missing (open gop)*/
2127  broken_link = get_bits1(&s->gb);
2128 
2129  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
2130  av_log(s->avctx, AV_LOG_DEBUG, "GOP (%2d:%02d:%02d.[%02d]) closed_gop=%d broken_link=%d\n",
2131  time_code_hours, time_code_minutes, time_code_seconds,
2132  time_code_pictures, s1->closed_gop, broken_link);
2133 }
2138 int ff_mpeg1_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size, AVCodecParserContext *s)
2139 {
2140  int i;
2141  uint32_t state = pc->state;
2142 
2143  /* EOF considered as end of frame */
2144  if (buf_size == 0)
2145  return 0;
2146 
2147 /*
2148  0 frame start -> 1/4
2149  1 first_SEQEXT -> 0/2
2150  2 first field start -> 3/0
2151  3 second_SEQEXT -> 2/0
2152  4 searching end
2153 */
2154 
2155  for (i = 0; i < buf_size; i++) {
2156  assert(pc->frame_start_found >= 0 && pc->frame_start_found <= 4);
2157  if (pc->frame_start_found & 1) {
2158  if (state == EXT_START_CODE && (buf[i] & 0xF0) != 0x80)
2159  pc->frame_start_found--;
2160  else if (state == EXT_START_CODE + 2) {
2161  if ((buf[i] & 3) == 3)
2162  pc->frame_start_found = 0;
2163  else
2164  pc->frame_start_found = (pc->frame_start_found + 1) & 3;
2165  }
2166  state++;
2167  } else {
2168  i = avpriv_mpv_find_start_code(buf + i, buf + buf_size, &state) - buf - 1;
2169  if (pc->frame_start_found == 0 && state >= SLICE_MIN_START_CODE && state <= SLICE_MAX_START_CODE) {
2170  i++;
2171  pc->frame_start_found = 4;
2172  }
2173  if (state == SEQ_END_CODE) {
2174  pc->state=-1;
2175  return i+1;
2176  }
2177  if (pc->frame_start_found == 2 && state == SEQ_START_CODE)
2178  pc->frame_start_found = 0;
2179  if (pc->frame_start_found < 4 && state == EXT_START_CODE)
2180  pc->frame_start_found++;
2181  if (pc->frame_start_found == 4 && (state & 0xFFFFFF00) == 0x100) {
2182  if (state < SLICE_MIN_START_CODE || state > SLICE_MAX_START_CODE) {
2183  pc->frame_start_found = 0;
2184  pc->state = -1;
2185  return i - 3;
2186  }
2187  }
2188  if (pc->frame_start_found == 0 && s && state == PICTURE_START_CODE) {
2189  ff_fetch_timestamp(s, i - 3, 1);
2190  }
2191  }
2192  }
2193  pc->state = state;
2194  return END_NOT_FOUND;
2195 }
2196 
2197 static int decode_chunks(AVCodecContext *avctx,
2198  AVFrame *picture, int *data_size,
2199  const uint8_t *buf, int buf_size);
2200 
2201 /* handle buffering and image synchronisation */
2203  void *data, int *data_size,
2204  AVPacket *avpkt)
2205 {
2206  const uint8_t *buf = avpkt->data;
2207  int buf_size = avpkt->size;
2208  Mpeg1Context *s = avctx->priv_data;
2209  AVFrame *picture = data;
2211  av_dlog(avctx, "fill_buffer\n");
2212 
2213  if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == SEQ_END_CODE)) {
2214  /* special case for last picture */
2215  if (s2->low_delay == 0 && s2->next_picture_ptr) {
2216  *picture = *(AVFrame*)s2->next_picture_ptr;
2217  s2->next_picture_ptr = NULL;
2218 
2219  *data_size = sizeof(AVFrame);
2220  }
2221  return buf_size;
2222  }
2223 
2224  if (s2->flags & CODEC_FLAG_TRUNCATED) {
2225  int next = ff_mpeg1_find_frame_end(&s2->parse_context, buf, buf_size, NULL);
2226 
2227  if (ff_combine_frame(&s2->parse_context, next, (const uint8_t **)&buf, &buf_size) < 0)
2228  return buf_size;
2229  }
2230 
2231  if (s->mpeg_enc_ctx_allocated == 0 && avctx->codec_tag == AV_RL32("VCR2"))
2232  vcr2_init_sequence(avctx);
2233 
2234  s->slice_count = 0;
2235 
2236  if (avctx->extradata && !s->extradata_decoded) {
2237  int ret = decode_chunks(avctx, picture, data_size, avctx->extradata, avctx->extradata_size);
2238  s->extradata_decoded = 1;
2239  if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE))
2240  return ret;
2241  }
2242 
2243  return decode_chunks(avctx, picture, data_size, buf, buf_size);
2244 }
2245 
2246 static int decode_chunks(AVCodecContext *avctx,
2247  AVFrame *picture, int *data_size,
2248  const uint8_t *buf, int buf_size)
2249 {
2250  Mpeg1Context *s = avctx->priv_data;
2252  const uint8_t *buf_ptr = buf;
2253  const uint8_t *buf_end = buf + buf_size;
2254  int ret, input_size;
2255  int last_code = 0;
2256 
2257  for (;;) {
2258  /* find next start code */
2259  uint32_t start_code = -1;
2260  buf_ptr = avpriv_mpv_find_start_code(buf_ptr, buf_end, &start_code);
2261  if (start_code > 0x1ff) {
2262  if (s2->pict_type != AV_PICTURE_TYPE_B || avctx->skip_frame <= AVDISCARD_DEFAULT) {
2263  if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE)) {
2264  int i;
2265 
2266  avctx->execute(avctx, slice_decode_thread, &s2->thread_context[0], NULL, s->slice_count, sizeof(void*));
2267  for (i = 0; i < s->slice_count; i++)
2268  s2->error_count += s2->thread_context[i]->error_count;
2269  }
2270 
2272  ff_vdpau_mpeg_picture_complete(s2, buf, buf_size, s->slice_count);
2273 
2274  if (slice_end(avctx, picture)) {
2275  if (s2->last_picture_ptr || s2->low_delay) //FIXME merge with the stuff in mpeg_decode_slice
2276  *data_size = sizeof(AVPicture);
2277  }
2278  }
2279  s2->pict_type = 0;
2280  return FFMAX(0, buf_ptr - buf - s2->parse_context.last_index);
2281  }
2282 
2283  input_size = buf_end - buf_ptr;
2284 
2285  if (avctx->debug & FF_DEBUG_STARTCODE) {
2286  av_log(avctx, AV_LOG_DEBUG, "%3X at %td left %d\n", start_code, buf_ptr-buf, input_size);
2287  }
2288 
2289  /* prepare data for next start code */
2290  switch (start_code) {
2291  case SEQ_START_CODE:
2292  if (last_code == 0) {
2293  mpeg1_decode_sequence(avctx, buf_ptr, input_size);
2294  s->sync=1;
2295  } else {
2296  av_log(avctx, AV_LOG_ERROR, "ignoring SEQ_START_CODE after %X\n", last_code);
2297  if (avctx->err_recognition & AV_EF_EXPLODE)
2298  return AVERROR_INVALIDDATA;
2299  }
2300  break;
2301 
2302  case PICTURE_START_CODE:
2303  if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE) && s->slice_count) {
2304  int i;
2305 
2306  avctx->execute(avctx, slice_decode_thread,
2307  s2->thread_context, NULL,
2308  s->slice_count, sizeof(void*));
2309  for (i = 0; i < s->slice_count; i++)
2310  s2->error_count += s2->thread_context[i]->error_count;
2311  s->slice_count = 0;
2312  }
2313  if (last_code == 0 || last_code == SLICE_MIN_START_CODE) {
2314  ret = mpeg_decode_postinit(avctx);
2315  if (ret < 0) {
2316  av_log(avctx, AV_LOG_ERROR, "mpeg_decode_postinit() failure\n");
2317  return ret;
2318  }
2319 
2320  /* we have a complete image: we try to decompress it */
2321  if (mpeg1_decode_picture(avctx, buf_ptr, input_size) < 0)
2322  s2->pict_type = 0;
2323  s2->first_slice = 1;
2324  last_code = PICTURE_START_CODE;
2325  } else {
2326  av_log(avctx, AV_LOG_ERROR, "ignoring pic after %X\n", last_code);
2327  if (avctx->err_recognition & AV_EF_EXPLODE)
2328  return AVERROR_INVALIDDATA;
2329  }
2330  break;
2331  case EXT_START_CODE:
2332  init_get_bits(&s2->gb, buf_ptr, input_size*8);
2333 
2334  switch (get_bits(&s2->gb, 4)) {
2335  case 0x1:
2336  if (last_code == 0) {
2338  } else {
2339  av_log(avctx, AV_LOG_ERROR, "ignoring seq ext after %X\n", last_code);
2340  if (avctx->err_recognition & AV_EF_EXPLODE)
2341  return AVERROR_INVALIDDATA;
2342  }
2343  break;
2344  case 0x2:
2346  break;
2347  case 0x3:
2349  break;
2350  case 0x7:
2352  break;
2353  case 0x8:
2354  if (last_code == PICTURE_START_CODE) {
2356  } else {
2357  av_log(avctx, AV_LOG_ERROR, "ignoring pic cod ext after %X\n", last_code);
2358  if (avctx->err_recognition & AV_EF_EXPLODE)
2359  return AVERROR_INVALIDDATA;
2360  }
2361  break;
2362  }
2363  break;
2364  case USER_START_CODE:
2365  mpeg_decode_user_data(avctx, buf_ptr, input_size);
2366  break;
2367  case GOP_START_CODE:
2368  if (last_code == 0) {
2369  s2->first_field=0;
2370  mpeg_decode_gop(avctx, buf_ptr, input_size);
2371  s->sync=1;
2372  } else {
2373  av_log(avctx, AV_LOG_ERROR, "ignoring GOP_START_CODE after %X\n", last_code);
2374  if (avctx->err_recognition & AV_EF_EXPLODE)
2375  return AVERROR_INVALIDDATA;
2376  }
2377  break;
2378  default:
2379  if (start_code >= SLICE_MIN_START_CODE &&
2380  start_code <= SLICE_MAX_START_CODE && last_code != 0) {
2381  const int field_pic = s2->picture_structure != PICT_FRAME;
2382  int mb_y = (start_code - SLICE_MIN_START_CODE) << field_pic;
2383  last_code = SLICE_MIN_START_CODE;
2384 
2386  mb_y++;
2387 
2388  if (mb_y >= s2->mb_height) {
2389  av_log(s2->avctx, AV_LOG_ERROR, "slice below image (%d >= %d)\n", mb_y, s2->mb_height);
2390  return -1;
2391  }
2392 
2393  if (s2->last_picture_ptr == NULL) {
2394  /* Skip B-frames if we do not have reference frames and gop is not closed */
2395  if (s2->pict_type == AV_PICTURE_TYPE_B) {
2396  if (!s->closed_gop)
2397  break;
2398  }
2399  }
2400  if (s2->pict_type == AV_PICTURE_TYPE_I)
2401  s->sync=1;
2402  if (s2->next_picture_ptr == NULL) {
2403  /* Skip P-frames if we do not have a reference frame or we have an invalid header. */
2404  if (s2->pict_type == AV_PICTURE_TYPE_P && !s->sync) break;
2405  }
2406  if ((avctx->skip_frame >= AVDISCARD_NONREF && s2->pict_type == AV_PICTURE_TYPE_B) ||
2407  (avctx->skip_frame >= AVDISCARD_NONKEY && s2->pict_type != AV_PICTURE_TYPE_I) ||
2408  avctx->skip_frame >= AVDISCARD_ALL)
2409  break;
2410 
2411  if (!s->mpeg_enc_ctx_allocated)
2412  break;
2413 
2414  if (s2->codec_id == CODEC_ID_MPEG2VIDEO) {
2415  if (mb_y < avctx->skip_top || mb_y >= s2->mb_height - avctx->skip_bottom)
2416  break;
2417  }
2418 
2419  if (!s2->pict_type) {
2420  av_log(avctx, AV_LOG_ERROR, "Missing picture start code\n");
2421  if (avctx->err_recognition & AV_EF_EXPLODE)
2422  return AVERROR_INVALIDDATA;
2423  break;
2424  }
2425 
2426  if (s2->first_slice) {
2427  s2->first_slice = 0;
2428  if (mpeg_field_start(s2, buf, buf_size) < 0)
2429  return -1;
2430  }
2431  if (!s2->current_picture_ptr) {
2432  av_log(avctx, AV_LOG_ERROR, "current_picture not initialized\n");
2433  return AVERROR_INVALIDDATA;
2434  }
2435 
2436  if (avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU) {
2437  s->slice_count++;
2438  break;
2439  }
2440 
2441  if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE)) {
2442  int threshold = (s2->mb_height * s->slice_count +
2443  s2->slice_context_count / 2) /
2444  s2->slice_context_count;
2445  if (threshold <= mb_y) {
2446  MpegEncContext *thread_context = s2->thread_context[s->slice_count];
2447 
2448  thread_context->start_mb_y = mb_y;
2449  thread_context->end_mb_y = s2->mb_height;
2450  if (s->slice_count) {
2451  s2->thread_context[s->slice_count-1]->end_mb_y = mb_y;
2452  ff_update_duplicate_context(thread_context, s2);
2453  }
2454  init_get_bits(&thread_context->gb, buf_ptr, input_size*8);
2455  s->slice_count++;
2456  }
2457  buf_ptr += 2; // FIXME add minimum number of bytes per slice
2458  } else {
2459  ret = mpeg_decode_slice(s2, mb_y, &buf_ptr, input_size);
2460  emms_c();
2461 
2462  if (ret < 0) {
2463  if (avctx->err_recognition & AV_EF_EXPLODE)
2464  return ret;
2465  if (s2->resync_mb_x >= 0 && s2->resync_mb_y >= 0)
2467  } else {
2469  }
2470  }
2471  }
2472  break;
2473  }
2474  }
2475 }
2476 
2477 static void flush(AVCodecContext *avctx)
2478 {
2479  Mpeg1Context *s = avctx->priv_data;
2480 
2481  s->sync=0;
2482  s->closed_gop = 0;
2483 
2484  ff_mpeg_flush(avctx);
2485 }
2486 
2488 {
2489  Mpeg1Context *s = avctx->priv_data;
2490 
2491  if (s->mpeg_enc_ctx_allocated)
2493  return 0;
2494 }
2495 
2497  { FF_PROFILE_MPEG2_422, "4:2:2" },
2498  { FF_PROFILE_MPEG2_HIGH, "High" },
2499  { FF_PROFILE_MPEG2_SS, "Spatially Scalable" },
2500  { FF_PROFILE_MPEG2_SNR_SCALABLE, "SNR Scalable" },
2501  { FF_PROFILE_MPEG2_MAIN, "Main" },
2502  { FF_PROFILE_MPEG2_SIMPLE, "Simple" },
2503  { FF_PROFILE_RESERVED, "Reserved" },
2504  { FF_PROFILE_RESERVED, "Reserved" },
2505  { FF_PROFILE_UNKNOWN },
2506 };
2507 
2508 
2510  .name = "mpeg1video",
2511  .type = AVMEDIA_TYPE_VIDEO,
2512  .id = CODEC_ID_MPEG1VIDEO,
2513  .priv_data_size = sizeof(Mpeg1Context),
2518  .flush = flush,
2519  .max_lowres = 3,
2520  .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video"),
2522 };
2523 
2525  .name = "mpeg2video",
2526  .type = AVMEDIA_TYPE_VIDEO,
2527  .id = CODEC_ID_MPEG2VIDEO,
2528  .priv_data_size = sizeof(Mpeg1Context),
2533  .flush = flush,
2534  .max_lowres = 3,
2535  .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 video"),
2536  .profiles = NULL_IF_CONFIG_SMALL(mpeg2_video_profiles),
2537 };
2538 
2539 #if CONFIG_MPEG_XVMC_DECODER
2540 static av_cold int mpeg_mc_decode_init(AVCodecContext *avctx)
2541 {
2542  if (avctx->active_thread_type & FF_THREAD_SLICE)
2543  return -1;
2544  if (!(avctx->slice_flags & SLICE_FLAG_CODED_ORDER))
2545  return -1;
2546  if (!(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD)) {
2547  av_dlog(avctx, "mpeg12.c: XvMC decoder will work better if SLICE_FLAG_ALLOW_FIELD is set\n");
2548  }
2549  mpeg_decode_init(avctx);
2550 
2552  avctx->xvmc_acceleration = 2; // 2 - the blocks are packed!
2553 
2554  return 0;
2555 }
2556 
2557 AVCodec ff_mpeg_xvmc_decoder = {
2558  .name = "mpegvideo_xvmc",
2559  .type = AVMEDIA_TYPE_VIDEO,
2561  .priv_data_size = sizeof(Mpeg1Context),
2562  .init = mpeg_mc_decode_init,
2566  .flush = flush,
2567  .long_name = NULL_IF_CONFIG_SMALL("MPEG-1/2 video XvMC (X-Video Motion Compensation)"),
2568 };
2569 
2570 #endif
2571 
2572 #if CONFIG_MPEG_VDPAU_DECODER
2573 AVCodec ff_mpeg_vdpau_decoder = {
2574  .name = "mpegvideo_vdpau",
2575  .type = AVMEDIA_TYPE_VIDEO,
2576  .id = CODEC_ID_MPEG2VIDEO,
2577  .priv_data_size = sizeof(Mpeg1Context),
2582  .flush = flush,
2583  .long_name = NULL_IF_CONFIG_SMALL("MPEG-1/2 video (VDPAU acceleration)"),
2584 };
2585 #endif
2586 
2587 #if CONFIG_MPEG1_VDPAU_DECODER
2588 AVCodec ff_mpeg1_vdpau_decoder = {
2589  .name = "mpeg1video_vdpau",
2590  .type = AVMEDIA_TYPE_VIDEO,
2591  .id = CODEC_ID_MPEG1VIDEO,
2592  .priv_data_size = sizeof(Mpeg1Context),
2597  .flush = flush,
2598  .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video (VDPAU acceleration)"),
2599 };
2600 #endif
2601