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 
1382  bit_rate_ext = get_bits(&s->gb, 12) << 18;
1383  if (bit_rate_ext < INT_MAX / 400 &&
1384  bit_rate_ext * 400 < INT_MAX - s->bit_rate) {
1385  s->bit_rate += bit_rate_ext * 400;
1386  } else {
1387  av_log(s->avctx, AV_LOG_WARNING, "Invalid bit rate extension value: %d\n",
1388  bit_rate_ext >> 18);
1389  s->bit_rate = 0;
1390  }
1391 
1392  skip_bits1(&s->gb); /* marker */
1393  s->avctx->rc_buffer_size += get_bits(&s->gb, 8) * 1024 * 16 << 10;
1394 
1395  s->low_delay = get_bits1(&s->gb);
1396  if (s->flags & CODEC_FLAG_LOW_DELAY)
1397  s->low_delay = 1;
1398 
1399  s1->frame_rate_ext.num = get_bits(&s->gb, 2) + 1;
1400  s1->frame_rate_ext.den = get_bits(&s->gb, 5) + 1;
1401 
1402  av_dlog(s->avctx, "sequence extension\n");
1404  s->avctx->sub_id = 2; /* indicates MPEG-2 found */
1405 
1406  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1407  av_log(s->avctx, AV_LOG_DEBUG, "profile: %d, level: %d vbv buffer: %d, bitrate:%d\n",
1408  s->avctx->profile, s->avctx->level, s->avctx->rc_buffer_size, s->bit_rate);
1409 
1410 }
1411 
1413 {
1414  MpegEncContext *s = &s1->mpeg_enc_ctx;
1415  int color_description, w, h;
1416 
1417  skip_bits(&s->gb, 3); /* video format */
1418  color_description = get_bits1(&s->gb);
1419  if (color_description) {
1420  s->avctx->color_primaries = get_bits(&s->gb, 8);
1421  s->avctx->color_trc = get_bits(&s->gb, 8);
1422  s->avctx->colorspace = get_bits(&s->gb, 8);
1423  }
1424  w = get_bits(&s->gb, 14);
1425  skip_bits(&s->gb, 1); //marker
1426  h = get_bits(&s->gb, 14);
1427  // remaining 3 bits are zero padding
1428 
1429  s1->pan_scan.width = 16 * w;
1430  s1->pan_scan.height = 16 * h;
1431 
1432  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1433  av_log(s->avctx, AV_LOG_DEBUG, "sde w:%d, h:%d\n", w, h);
1434 }
1435 
1437 {
1438  MpegEncContext *s = &s1->mpeg_enc_ctx;
1439  int i, nofco;
1440 
1441  nofco = 1;
1442  if (s->progressive_sequence) {
1443  if (s->repeat_first_field) {
1444  nofco++;
1445  if (s->top_field_first)
1446  nofco++;
1447  }
1448  } else {
1449  if (s->picture_structure == PICT_FRAME) {
1450  nofco++;
1451  if (s->repeat_first_field)
1452  nofco++;
1453  }
1454  }
1455  for (i = 0; i < nofco; i++) {
1456  s1->pan_scan.position[i][0] = get_sbits(&s->gb, 16);
1457  skip_bits(&s->gb, 1); // marker
1458  s1->pan_scan.position[i][1] = get_sbits(&s->gb, 16);
1459  skip_bits(&s->gb, 1); // marker
1460  }
1461 
1462  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1463  av_log(s->avctx, AV_LOG_DEBUG, "pde (%d,%d) (%d,%d) (%d,%d)\n",
1464  s1->pan_scan.position[0][0], s1->pan_scan.position[0][1],
1465  s1->pan_scan.position[1][0], s1->pan_scan.position[1][1],
1466  s1->pan_scan.position[2][0], s1->pan_scan.position[2][1]);
1467 }
1468 
1469 static int load_matrix(MpegEncContext *s, uint16_t matrix0[64], uint16_t matrix1[64], int intra)
1470 {
1471  int i;
1472 
1473  for (i = 0; i < 64; i++) {
1474  int j = s->dsp.idct_permutation[ff_zigzag_direct[i]];
1475  int v = get_bits(&s->gb, 8);
1476  if (v == 0) {
1477  av_log(s->avctx, AV_LOG_ERROR, "matrix damaged\n");
1478  return -1;
1479  }
1480  if (intra && i == 0 && v != 8) {
1481  av_log(s->avctx, AV_LOG_ERROR, "intra matrix invalid, ignoring\n");
1482  v = 8; // needed by pink.mpg / issue1046
1483  }
1484  matrix0[j] = v;
1485  if (matrix1)
1486  matrix1[j] = v;
1487  }
1488  return 0;
1489 }
1490 
1492 {
1493  av_dlog(s->avctx, "matrix extension\n");
1494 
1495  if (get_bits1(&s->gb)) load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
1496  if (get_bits1(&s->gb)) load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
1497  if (get_bits1(&s->gb)) load_matrix(s, s->chroma_intra_matrix, NULL , 1);
1498  if (get_bits1(&s->gb)) load_matrix(s, s->chroma_inter_matrix, NULL , 0);
1499 }
1500 
1502 {
1503  MpegEncContext *s = &s1->mpeg_enc_ctx;
1504 
1505  s->full_pel[0] = s->full_pel[1] = 0;
1506  s->mpeg_f_code[0][0] = get_bits(&s->gb, 4);
1507  s->mpeg_f_code[0][1] = get_bits(&s->gb, 4);
1508  s->mpeg_f_code[1][0] = get_bits(&s->gb, 4);
1509  s->mpeg_f_code[1][1] = get_bits(&s->gb, 4);
1510  if (!s->pict_type && s1->mpeg_enc_ctx_allocated) {
1511  av_log(s->avctx, AV_LOG_ERROR, "Missing picture start code, guessing missing values\n");
1512  if (s->mpeg_f_code[1][0] == 15 && s->mpeg_f_code[1][1] == 15) {
1513  if (s->mpeg_f_code[0][0] == 15 && s->mpeg_f_code[0][1] == 15)
1515  else
1517  } else
1521  }
1522  s->intra_dc_precision = get_bits(&s->gb, 2);
1523  s->picture_structure = get_bits(&s->gb, 2);
1524  s->top_field_first = get_bits1(&s->gb);
1525  s->frame_pred_frame_dct = get_bits1(&s->gb);
1527  s->q_scale_type = get_bits1(&s->gb);
1528  s->intra_vlc_format = get_bits1(&s->gb);
1529  s->alternate_scan = get_bits1(&s->gb);
1530  s->repeat_first_field = get_bits1(&s->gb);
1531  s->chroma_420_type = get_bits1(&s->gb);
1532  s->progressive_frame = get_bits1(&s->gb);
1533 
1534  if (s->progressive_sequence && !s->progressive_frame) {
1535  s->progressive_frame = 1;
1536  av_log(s->avctx, AV_LOG_ERROR, "interlaced frame in progressive sequence, ignoring\n");
1537  }
1538 
1539  if (s->picture_structure == 0 || (s->progressive_frame && s->picture_structure != PICT_FRAME)) {
1540  av_log(s->avctx, AV_LOG_ERROR, "picture_structure %d invalid, ignoring\n", s->picture_structure);
1542  }
1543 
1545  av_log(s->avctx, AV_LOG_ERROR, "invalid frame_pred_frame_dct\n");
1546  s->frame_pred_frame_dct = 1;
1547  }
1548 
1549  if (s->picture_structure == PICT_FRAME) {
1550  s->v_edge_pos = 16 * s->mb_height;
1551  } else {
1552  s->v_edge_pos = 8 * s->mb_height;
1553  memset(s->mbskip_table, 0, s->mb_stride * s->mb_height);
1554  }
1555 
1556  if (s->alternate_scan) {
1559  } else {
1562  }
1563 
1564  /* composite display not parsed */
1565  av_dlog(s->avctx, "intra_dc_precision=%d\n", s->intra_dc_precision);
1566  av_dlog(s->avctx, "picture_structure=%d\n", s->picture_structure);
1567  av_dlog(s->avctx, "top field first=%d\n", s->top_field_first);
1568  av_dlog(s->avctx, "repeat first field=%d\n", s->repeat_first_field);
1569  av_dlog(s->avctx, "conceal=%d\n", s->concealment_motion_vectors);
1570  av_dlog(s->avctx, "intra_vlc_format=%d\n", s->intra_vlc_format);
1571  av_dlog(s->avctx, "alternate_scan=%d\n", s->alternate_scan);
1572  av_dlog(s->avctx, "frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
1573  av_dlog(s->avctx, "progressive_frame=%d\n", s->progressive_frame);
1574 }
1575 
1576 static int mpeg_field_start(MpegEncContext *s, const uint8_t *buf, int buf_size)
1577 {
1578  AVCodecContext *avctx = s->avctx;
1579  Mpeg1Context *s1 = (Mpeg1Context*)s;
1580 
1581  if (s->picture_structure == PICT_FRAME)
1582  s->first_field = 0;
1583  else
1584  s->first_field ^= 1;
1585 
1586  /* start frame decoding */
1587  if (s->first_field || s->picture_structure == PICT_FRAME) {
1588  if (MPV_frame_start(s, avctx) < 0)
1589  return -1;
1590 
1591  ff_er_frame_start(s);
1592 
1593  /* first check if we must repeat the frame */
1595  if (s->repeat_first_field) {
1596  if (s->progressive_sequence) {
1597  if (s->top_field_first)
1599  else
1601  } else if (s->progressive_frame) {
1603  }
1604  }
1605 
1607 
1609  ff_thread_finish_setup(avctx);
1610  } else { // second field
1611  int i;
1612 
1613  if (!s->current_picture_ptr) {
1614  av_log(s->avctx, AV_LOG_ERROR, "first field missing\n");
1615  return -1;
1616  }
1617 
1618  for (i = 0; i < 4; i++) {
1622  }
1623  }
1624  }
1625 
1626  if (avctx->hwaccel) {
1627  if (avctx->hwaccel->start_frame(avctx, buf, buf_size) < 0)
1628  return -1;
1629  }
1630 
1631 // MPV_frame_start will call this function too,
1632 // but we need to call it on every field
1634  if (ff_xvmc_field_start(s, avctx) < 0)
1635  return -1;
1636 
1637  return 0;
1638 }
1639 
1640 #define DECODE_SLICE_ERROR -1
1641 #define DECODE_SLICE_OK 0
1642 
1649 static int mpeg_decode_slice(MpegEncContext *s, int mb_y,
1650  const uint8_t **buf, int buf_size)
1651 {
1652  AVCodecContext *avctx = s->avctx;
1653  const int lowres = s->avctx->lowres;
1654  const int field_pic = s->picture_structure != PICT_FRAME;
1655 
1656  s->resync_mb_x =
1657  s->resync_mb_y = -1;
1658 
1659  assert(mb_y < s->mb_height);
1660 
1661  init_get_bits(&s->gb, *buf, buf_size * 8);
1662 
1664  s->interlaced_dct = 0;
1665 
1666  s->qscale = get_qscale(s);
1667 
1668  if (s->qscale == 0) {
1669  av_log(s->avctx, AV_LOG_ERROR, "qscale == 0\n");
1670  return -1;
1671  }
1672 
1673  /* extra slice info */
1674  while (get_bits1(&s->gb) != 0) {
1675  skip_bits(&s->gb, 8);
1676  }
1677 
1678  s->mb_x = 0;
1679 
1680  if (mb_y == 0 && s->codec_tag == AV_RL32("SLIF")) {
1681  skip_bits1(&s->gb);
1682  } else {
1683  while (get_bits_left(&s->gb) > 0) {
1684  int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
1685  if (code < 0) {
1686  av_log(s->avctx, AV_LOG_ERROR, "first mb_incr damaged\n");
1687  return -1;
1688  }
1689  if (code >= 33) {
1690  if (code == 33) {
1691  s->mb_x += 33;
1692  }
1693  /* otherwise, stuffing, nothing to do */
1694  } else {
1695  s->mb_x += code;
1696  break;
1697  }
1698  }
1699  }
1700 
1701  if (s->mb_x >= (unsigned)s->mb_width) {
1702  av_log(s->avctx, AV_LOG_ERROR, "initial skip overflow\n");
1703  return -1;
1704  }
1705 
1706  if (avctx->hwaccel) {
1707  const uint8_t *buf_end, *buf_start = *buf - 4; /* include start_code */
1708  int start_code = -1;
1709  buf_end = avpriv_mpv_find_start_code(buf_start + 2, *buf + buf_size, &start_code);
1710  if (buf_end < *buf + buf_size)
1711  buf_end -= 4;
1712  s->mb_y = mb_y;
1713  if (avctx->hwaccel->decode_slice(avctx, buf_start, buf_end - buf_start) < 0)
1714  return DECODE_SLICE_ERROR;
1715  *buf = buf_end;
1716  return DECODE_SLICE_OK;
1717  }
1718 
1719  s->resync_mb_x = s->mb_x;
1720  s->resync_mb_y = s->mb_y = mb_y;
1721  s->mb_skip_run = 0;
1723 
1724  if (s->mb_y == 0 && s->mb_x == 0 && (s->first_field || s->picture_structure == PICT_FRAME)) {
1725  if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
1726  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",
1727  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],
1728  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")),
1729  s->progressive_sequence ? "ps" :"", s->progressive_frame ? "pf" : "", s->alternate_scan ? "alt" :"", s->top_field_first ? "top" :"",
1732  }
1733  }
1734 
1735  for (;;) {
1736  // If 1, we memcpy blocks in xvmcvideo.
1738  ff_xvmc_init_block(s); // set s->block
1739 
1740  if (mpeg_decode_mb(s, s->block) < 0)
1741  return -1;
1742 
1743  if (s->current_picture.f.motion_val[0] && !s->encoding) { // note motion_val is normally NULL unless we want to extract the MVs
1744  const int wrap = s->b8_stride;
1745  int xy = s->mb_x * 2 + s->mb_y * 2 * wrap;
1746  int b8_xy = 4 * (s->mb_x + s->mb_y * s->mb_stride);
1747  int motion_x, motion_y, dir, i;
1748 
1749  for (i = 0; i < 2; i++) {
1750  for (dir = 0; dir < 2; dir++) {
1751  if (s->mb_intra || (dir == 1 && s->pict_type != AV_PICTURE_TYPE_B)) {
1752  motion_x = motion_y = 0;
1753  } else if (s->mv_type == MV_TYPE_16X16 || (s->mv_type == MV_TYPE_FIELD && field_pic)) {
1754  motion_x = s->mv[dir][0][0];
1755  motion_y = s->mv[dir][0][1];
1756  } else /*if ((s->mv_type == MV_TYPE_FIELD) || (s->mv_type == MV_TYPE_16X8))*/ {
1757  motion_x = s->mv[dir][i][0];
1758  motion_y = s->mv[dir][i][1];
1759  }
1760 
1761  s->current_picture.f.motion_val[dir][xy ][0] = motion_x;
1762  s->current_picture.f.motion_val[dir][xy ][1] = motion_y;
1763  s->current_picture.f.motion_val[dir][xy + 1][0] = motion_x;
1764  s->current_picture.f.motion_val[dir][xy + 1][1] = motion_y;
1765  s->current_picture.f.ref_index [dir][b8_xy ] =
1766  s->current_picture.f.ref_index [dir][b8_xy + 1] = s->field_select[dir][i];
1767  assert(s->field_select[dir][i] == 0 || s->field_select[dir][i] == 1);
1768  }
1769  xy += wrap;
1770  b8_xy +=2;
1771  }
1772  }
1773 
1774  s->dest[0] += 16 >> lowres;
1775  s->dest[1] +=(16 >> lowres) >> s->chroma_x_shift;
1776  s->dest[2] +=(16 >> lowres) >> s->chroma_x_shift;
1777 
1778  MPV_decode_mb(s, s->block);
1779 
1780  if (++s->mb_x >= s->mb_width) {
1781  const int mb_size = 16 >> s->avctx->lowres;
1782 
1783  ff_draw_horiz_band(s, mb_size*(s->mb_y >> field_pic), mb_size);
1785 
1786  s->mb_x = 0;
1787  s->mb_y += 1 << field_pic;
1788 
1789  if (s->mb_y >= s->mb_height) {
1790  int left = get_bits_left(&s->gb);
1791  int is_d10 = s->chroma_format == 2 && s->pict_type == AV_PICTURE_TYPE_I && avctx->profile == 0 && avctx->level == 5
1792  && s->intra_dc_precision == 2 && s->q_scale_type == 1 && s->alternate_scan == 0
1793  && s->progressive_frame == 0 /* vbv_delay == 0xBBB || 0xE10*/;
1794 
1795  if (left < 0 || (left && show_bits(&s->gb, FFMIN(left, 23)) && !is_d10)
1796  || ((avctx->err_recognition & AV_EF_BUFFER) && left > 8)) {
1797  av_log(avctx, AV_LOG_ERROR, "end mismatch left=%d %0X\n", left, show_bits(&s->gb, FFMIN(left, 23)));
1798  return -1;
1799  } else
1800  goto eos;
1801  }
1802 
1804  }
1805 
1806  /* skip mb handling */
1807  if (s->mb_skip_run == -1) {
1808  /* read increment again */
1809  s->mb_skip_run = 0;
1810  for (;;) {
1811  int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
1812  if (code < 0) {
1813  av_log(s->avctx, AV_LOG_ERROR, "mb incr damaged\n");
1814  return -1;
1815  }
1816  if (code >= 33) {
1817  if (code == 33) {
1818  s->mb_skip_run += 33;
1819  } else if (code == 35) {
1820  if (s->mb_skip_run != 0 || show_bits(&s->gb, 15) != 0) {
1821  av_log(s->avctx, AV_LOG_ERROR, "slice mismatch\n");
1822  return -1;
1823  }
1824  goto eos; /* end of slice */
1825  }
1826  /* otherwise, stuffing, nothing to do */
1827  } else {
1828  s->mb_skip_run += code;
1829  break;
1830  }
1831  }
1832  if (s->mb_skip_run) {
1833  int i;
1834  if (s->pict_type == AV_PICTURE_TYPE_I) {
1835  av_log(s->avctx, AV_LOG_ERROR, "skipped MB in I frame at %d %d\n", s->mb_x, s->mb_y);
1836  return -1;
1837  }
1838 
1839  /* skip mb */
1840  s->mb_intra = 0;
1841  for (i = 0; i < 12; i++)
1842  s->block_last_index[i] = -1;
1843  if (s->picture_structure == PICT_FRAME)
1844  s->mv_type = MV_TYPE_16X16;
1845  else
1846  s->mv_type = MV_TYPE_FIELD;
1847  if (s->pict_type == AV_PICTURE_TYPE_P) {
1848  /* if P type, zero motion vector is implied */
1849  s->mv_dir = MV_DIR_FORWARD;
1850  s->mv[0][0][0] = s->mv[0][0][1] = 0;
1851  s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
1852  s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
1853  s->field_select[0][0] = (s->picture_structure - 1) & 1;
1854  } else {
1855  /* if B type, reuse previous vectors and directions */
1856  s->mv[0][0][0] = s->last_mv[0][0][0];
1857  s->mv[0][0][1] = s->last_mv[0][0][1];
1858  s->mv[1][0][0] = s->last_mv[1][0][0];
1859  s->mv[1][0][1] = s->last_mv[1][0][1];
1860  }
1861  }
1862  }
1863  }
1864 eos: // end of slice
1865  *buf += (get_bits_count(&s->gb)-1)/8;
1866 //printf("y %d %d %d %d\n", s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y);
1867  return 0;
1868 }
1869 
1870 static int slice_decode_thread(AVCodecContext *c, void *arg)
1871 {
1872  MpegEncContext *s = *(void**)arg;
1873  const uint8_t *buf = s->gb.buffer;
1874  int mb_y = s->start_mb_y;
1875  const int field_pic = s->picture_structure != PICT_FRAME;
1876 
1877  s->error_count = (3 * (s->end_mb_y - s->start_mb_y) * s->mb_width) >> field_pic;
1878 
1879  for (;;) {
1880  uint32_t start_code;
1881  int ret;
1882 
1883  ret = mpeg_decode_slice(s, mb_y, &buf, s->gb.buffer_end - buf);
1884  emms_c();
1885 //av_log(c, AV_LOG_DEBUG, "ret:%d resync:%d/%d mb:%d/%d ts:%d/%d ec:%d\n",
1886 //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);
1887  if (ret < 0) {
1888  if (c->err_recognition & AV_EF_EXPLODE)
1889  return ret;
1890  if (s->resync_mb_x >= 0 && s->resync_mb_y >= 0)
1892  } else {
1894  }
1895 
1896  if (s->mb_y == s->end_mb_y)
1897  return 0;
1898 
1899  start_code = -1;
1900  buf = avpriv_mpv_find_start_code(buf, s->gb.buffer_end, &start_code);
1901  mb_y= (start_code - SLICE_MIN_START_CODE) << field_pic;
1903  mb_y++;
1904  if (mb_y < 0 || mb_y >= s->end_mb_y)
1905  return -1;
1906  }
1907 }
1908 
1913 static int slice_end(AVCodecContext *avctx, AVFrame *pict)
1914 {
1915  Mpeg1Context *s1 = avctx->priv_data;
1916  MpegEncContext *s = &s1->mpeg_enc_ctx;
1917 
1919  return 0;
1920 
1921  if (s->avctx->hwaccel) {
1922  if (s->avctx->hwaccel->end_frame(s->avctx) < 0)
1923  av_log(avctx, AV_LOG_ERROR, "hardware accelerator failed to decode picture\n");
1924  }
1925 
1927  ff_xvmc_field_end(s);
1928 
1929  /* end of slice reached */
1930  if (/*s->mb_y << field_pic == s->mb_height &&*/ !s->first_field) {
1931  /* end of image */
1932 
1934 
1935  ff_er_frame_end(s);
1936 
1937  MPV_frame_end(s);
1938 
1939  if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
1940  *pict = *(AVFrame*)s->current_picture_ptr;
1941  ff_print_debug_info(s, pict);
1942  } else {
1943  if (avctx->active_thread_type & FF_THREAD_FRAME)
1944  s->picture_number++;
1945  /* latency of 1 frame for I- and P-frames */
1946  /* XXX: use another variable than picture_number */
1947  if (s->last_picture_ptr != NULL) {
1948  *pict = *(AVFrame*)s->last_picture_ptr;
1949  ff_print_debug_info(s, pict);
1950  }
1951  }
1952 
1953  return 1;
1954  } else {
1955  return 0;
1956  }
1957 }
1958 
1960  const uint8_t *buf, int buf_size)
1961 {
1962  Mpeg1Context *s1 = avctx->priv_data;
1963  MpegEncContext *s = &s1->mpeg_enc_ctx;
1964  int width, height;
1965  int i, v, j;
1966 
1967  init_get_bits(&s->gb, buf, buf_size*8);
1968 
1969  width = get_bits(&s->gb, 12);
1970  height = get_bits(&s->gb, 12);
1971  if (width <= 0 || height <= 0)
1972  return -1;
1973  s->aspect_ratio_info = get_bits(&s->gb, 4);
1974  if (s->aspect_ratio_info == 0) {
1975  av_log(avctx, AV_LOG_ERROR, "aspect ratio has forbidden 0 value\n");
1976  if (avctx->err_recognition & AV_EF_BITSTREAM)
1977  return -1;
1978  }
1979  s->frame_rate_index = get_bits(&s->gb, 4);
1980  if (s->frame_rate_index == 0 || s->frame_rate_index > 13)
1981  return -1;
1982  s->bit_rate = get_bits(&s->gb, 18) * 400;
1983  if (get_bits1(&s->gb) == 0) /* marker */
1984  return -1;
1985  s->width = width;
1986  s->height = height;
1987 
1988  s->avctx->rc_buffer_size = get_bits(&s->gb, 10) * 1024 * 16;
1989  skip_bits(&s->gb, 1);
1990 
1991  /* get matrix */
1992  if (get_bits1(&s->gb)) {
1994  } else {
1995  for (i = 0; i < 64; i++) {
1996  j = s->dsp.idct_permutation[i];
1998  s->intra_matrix[j] = v;
1999  s->chroma_intra_matrix[j] = v;
2000  }
2001  }
2002  if (get_bits1(&s->gb)) {
2004  } else {
2005  for (i = 0; i < 64; i++) {
2006  int j = s->dsp.idct_permutation[i];
2008  s->inter_matrix[j] = v;
2009  s->chroma_inter_matrix[j] = v;
2010  }
2011  }
2012 
2013  if (show_bits(&s->gb, 23) != 0) {
2014  av_log(s->avctx, AV_LOG_ERROR, "sequence header damaged\n");
2015  return -1;
2016  }
2017 
2018  /* we set MPEG-2 parameters so that it emulates MPEG-1 */
2019  s->progressive_sequence = 1;
2020  s->progressive_frame = 1;
2022  s->frame_pred_frame_dct = 1;
2023  s->chroma_format = 1;
2025  avctx->sub_id = 1; /* indicates MPEG-1 */
2026  s->out_format = FMT_MPEG1;
2027  s->swap_uv = 0; // AFAIK VCR2 does not have SEQ_HEADER
2028  if (s->flags & CODEC_FLAG_LOW_DELAY)
2029  s->low_delay = 1;
2030 
2031  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
2032  av_log(s->avctx, AV_LOG_DEBUG, "vbv buffer: %d, bitrate:%d\n",
2033  s->avctx->rc_buffer_size, s->bit_rate);
2034 
2035  return 0;
2036 }
2037 
2039 {
2040  Mpeg1Context *s1 = avctx->priv_data;
2041  MpegEncContext *s = &s1->mpeg_enc_ctx;
2042  int i, v;
2043 
2044  /* start new MPEG-1 context decoding */
2045  s->out_format = FMT_MPEG1;
2046  if (s1->mpeg_enc_ctx_allocated) {
2047  MPV_common_end(s);
2048  }
2049  s->width = avctx->coded_width;
2050  s->height = avctx->coded_height;
2051  avctx->has_b_frames = 0; // true?
2052  s->low_delay = 1;
2053 
2054  avctx->pix_fmt = mpeg_get_pixelformat(avctx);
2055  avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
2056 
2057  if (avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT || avctx->hwaccel ||
2059  if (avctx->idct_algo == FF_IDCT_AUTO)
2060  avctx->idct_algo = FF_IDCT_SIMPLE;
2061 
2062  if (MPV_common_init(s) < 0)
2063  return -1;
2064  exchange_uv(s); // common init reset pblocks, so we swap them here
2065  s->swap_uv = 1; // in case of xvmc we need to swap uv for each MB
2066  s1->mpeg_enc_ctx_allocated = 1;
2067 
2068  for (i = 0; i < 64; i++) {
2069  int j = s->dsp.idct_permutation[i];
2071  s->intra_matrix[j] = v;
2072  s->chroma_intra_matrix[j] = v;
2073 
2075  s->inter_matrix[j] = v;
2076  s->chroma_inter_matrix[j] = v;
2077  }
2078 
2079  s->progressive_sequence = 1;
2080  s->progressive_frame = 1;
2082  s->frame_pred_frame_dct = 1;
2083  s->chroma_format = 1;
2085  avctx->sub_id = 2; /* indicates MPEG-2 */
2086  s1->save_width = s->width;
2087  s1->save_height = s->height;
2089  return 0;
2090 }
2091 
2092 
2094  const uint8_t *p, int buf_size)
2095 {
2096  const uint8_t *buf_end = p + buf_size;
2097 
2098  /* we parse the DTG active format information */
2099  if (buf_end - p >= 5 &&
2100  p[0] == 'D' && p[1] == 'T' && p[2] == 'G' && p[3] == '1') {
2101  int flags = p[4];
2102  p += 5;
2103  if (flags & 0x80) {
2104  /* skip event id */
2105  p += 2;
2106  }
2107  if (flags & 0x40) {
2108  if (buf_end - p < 1)
2109  return;
2110  avctx->dtg_active_format = p[0] & 0x0f;
2111  }
2112  }
2113 }
2114 
2115 static void mpeg_decode_gop(AVCodecContext *avctx,
2116  const uint8_t *buf, int buf_size)
2117 {
2118  Mpeg1Context *s1 = avctx->priv_data;
2119  MpegEncContext *s = &s1->mpeg_enc_ctx;
2120 
2121  int time_code_hours, time_code_minutes;
2122  int time_code_seconds, time_code_pictures;
2123  int broken_link;
2124 
2125  init_get_bits(&s->gb, buf, buf_size*8);
2126 
2127  skip_bits1(&s->gb); /* drop_frame_flag */
2128 
2129  time_code_hours = get_bits(&s->gb, 5);
2130  time_code_minutes = get_bits(&s->gb, 6);
2131  skip_bits1(&s->gb); // marker bit
2132  time_code_seconds = get_bits(&s->gb, 6);
2133  time_code_pictures = get_bits(&s->gb, 6);
2134 
2135  s1->closed_gop = get_bits1(&s->gb);
2136  /*broken_link indicate that after editing the
2137  reference frames of the first B-Frames after GOP I-Frame
2138  are missing (open gop)*/
2139  broken_link = get_bits1(&s->gb);
2140 
2141  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
2142  av_log(s->avctx, AV_LOG_DEBUG, "GOP (%2d:%02d:%02d.[%02d]) closed_gop=%d broken_link=%d\n",
2143  time_code_hours, time_code_minutes, time_code_seconds,
2144  time_code_pictures, s1->closed_gop, broken_link);
2145 }
2150 int ff_mpeg1_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size, AVCodecParserContext *s)
2151 {
2152  int i;
2153  uint32_t state = pc->state;
2154 
2155  /* EOF considered as end of frame */
2156  if (buf_size == 0)
2157  return 0;
2158 
2159 /*
2160  0 frame start -> 1/4
2161  1 first_SEQEXT -> 0/2
2162  2 first field start -> 3/0
2163  3 second_SEQEXT -> 2/0
2164  4 searching end
2165 */
2166 
2167  for (i = 0; i < buf_size; i++) {
2168  assert(pc->frame_start_found >= 0 && pc->frame_start_found <= 4);
2169  if (pc->frame_start_found & 1) {
2170  if (state == EXT_START_CODE && (buf[i] & 0xF0) != 0x80)
2171  pc->frame_start_found--;
2172  else if (state == EXT_START_CODE + 2) {
2173  if ((buf[i] & 3) == 3)
2174  pc->frame_start_found = 0;
2175  else
2176  pc->frame_start_found = (pc->frame_start_found + 1) & 3;
2177  }
2178  state++;
2179  } else {
2180  i = avpriv_mpv_find_start_code(buf + i, buf + buf_size, &state) - buf - 1;
2181  if (pc->frame_start_found == 0 && state >= SLICE_MIN_START_CODE && state <= SLICE_MAX_START_CODE) {
2182  i++;
2183  pc->frame_start_found = 4;
2184  }
2185  if (state == SEQ_END_CODE) {
2186  pc->state=-1;
2187  return i+1;
2188  }
2189  if (pc->frame_start_found == 2 && state == SEQ_START_CODE)
2190  pc->frame_start_found = 0;
2191  if (pc->frame_start_found < 4 && state == EXT_START_CODE)
2192  pc->frame_start_found++;
2193  if (pc->frame_start_found == 4 && (state & 0xFFFFFF00) == 0x100) {
2194  if (state < SLICE_MIN_START_CODE || state > SLICE_MAX_START_CODE) {
2195  pc->frame_start_found = 0;
2196  pc->state = -1;
2197  return i - 3;
2198  }
2199  }
2200  if (pc->frame_start_found == 0 && s && state == PICTURE_START_CODE) {
2201  ff_fetch_timestamp(s, i - 3, 1);
2202  }
2203  }
2204  }
2205  pc->state = state;
2206  return END_NOT_FOUND;
2207 }
2208 
2209 static int decode_chunks(AVCodecContext *avctx,
2210  AVFrame *picture, int *data_size,
2211  const uint8_t *buf, int buf_size);
2212 
2213 /* handle buffering and image synchronisation */
2215  void *data, int *data_size,
2216  AVPacket *avpkt)
2217 {
2218  const uint8_t *buf = avpkt->data;
2219  int buf_size = avpkt->size;
2220  Mpeg1Context *s = avctx->priv_data;
2221  AVFrame *picture = data;
2223  av_dlog(avctx, "fill_buffer\n");
2224 
2225  if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == SEQ_END_CODE)) {
2226  /* special case for last picture */
2227  if (s2->low_delay == 0 && s2->next_picture_ptr) {
2228  *picture = *(AVFrame*)s2->next_picture_ptr;
2229  s2->next_picture_ptr = NULL;
2230 
2231  *data_size = sizeof(AVFrame);
2232  }
2233  return buf_size;
2234  }
2235 
2236  if (s2->flags & CODEC_FLAG_TRUNCATED) {
2237  int next = ff_mpeg1_find_frame_end(&s2->parse_context, buf, buf_size, NULL);
2238 
2239  if (ff_combine_frame(&s2->parse_context, next, (const uint8_t **)&buf, &buf_size) < 0)
2240  return buf_size;
2241  }
2242 
2243  if (s->mpeg_enc_ctx_allocated == 0 && avctx->codec_tag == AV_RL32("VCR2"))
2244  vcr2_init_sequence(avctx);
2245 
2246  s->slice_count = 0;
2247 
2248  if (avctx->extradata && !s->extradata_decoded) {
2249  int ret = decode_chunks(avctx, picture, data_size, avctx->extradata, avctx->extradata_size);
2250  s->extradata_decoded = 1;
2251  if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE))
2252  return ret;
2253  }
2254 
2255  return decode_chunks(avctx, picture, data_size, buf, buf_size);
2256 }
2257 
2258 static int decode_chunks(AVCodecContext *avctx,
2259  AVFrame *picture, int *data_size,
2260  const uint8_t *buf, int buf_size)
2261 {
2262  Mpeg1Context *s = avctx->priv_data;
2264  const uint8_t *buf_ptr = buf;
2265  const uint8_t *buf_end = buf + buf_size;
2266  int ret, input_size;
2267  int last_code = 0;
2268 
2269  for (;;) {
2270  /* find next start code */
2271  uint32_t start_code = -1;
2272  buf_ptr = avpriv_mpv_find_start_code(buf_ptr, buf_end, &start_code);
2273  if (start_code > 0x1ff) {
2274  if (s2->pict_type != AV_PICTURE_TYPE_B || avctx->skip_frame <= AVDISCARD_DEFAULT) {
2275  if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE)) {
2276  int i;
2277 
2278  avctx->execute(avctx, slice_decode_thread, &s2->thread_context[0], NULL, s->slice_count, sizeof(void*));
2279  for (i = 0; i < s->slice_count; i++)
2280  s2->error_count += s2->thread_context[i]->error_count;
2281  }
2282 
2284  ff_vdpau_mpeg_picture_complete(s2, buf, buf_size, s->slice_count);
2285 
2286  if (slice_end(avctx, picture)) {
2287  if (s2->last_picture_ptr || s2->low_delay) //FIXME merge with the stuff in mpeg_decode_slice
2288  *data_size = sizeof(AVPicture);
2289  }
2290  }
2291  s2->pict_type = 0;
2292  return FFMAX(0, buf_ptr - buf - s2->parse_context.last_index);
2293  }
2294 
2295  input_size = buf_end - buf_ptr;
2296 
2297  if (avctx->debug & FF_DEBUG_STARTCODE) {
2298  av_log(avctx, AV_LOG_DEBUG, "%3X at %td left %d\n", start_code, buf_ptr-buf, input_size);
2299  }
2300 
2301  /* prepare data for next start code */
2302  switch (start_code) {
2303  case SEQ_START_CODE:
2304  if (last_code == 0) {
2305  mpeg1_decode_sequence(avctx, buf_ptr, input_size);
2306  s->sync=1;
2307  } else {
2308  av_log(avctx, AV_LOG_ERROR, "ignoring SEQ_START_CODE after %X\n", last_code);
2309  if (avctx->err_recognition & AV_EF_EXPLODE)
2310  return AVERROR_INVALIDDATA;
2311  }
2312  break;
2313 
2314  case PICTURE_START_CODE:
2315  if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE) && s->slice_count) {
2316  int i;
2317 
2318  avctx->execute(avctx, slice_decode_thread,
2319  s2->thread_context, NULL,
2320  s->slice_count, sizeof(void*));
2321  for (i = 0; i < s->slice_count; i++)
2322  s2->error_count += s2->thread_context[i]->error_count;
2323  s->slice_count = 0;
2324  }
2325  if (last_code == 0 || last_code == SLICE_MIN_START_CODE) {
2326  ret = mpeg_decode_postinit(avctx);
2327  if (ret < 0) {
2328  av_log(avctx, AV_LOG_ERROR, "mpeg_decode_postinit() failure\n");
2329  return ret;
2330  }
2331 
2332  /* we have a complete image: we try to decompress it */
2333  if (mpeg1_decode_picture(avctx, buf_ptr, input_size) < 0)
2334  s2->pict_type = 0;
2335  s2->first_slice = 1;
2336  last_code = PICTURE_START_CODE;
2337  } else {
2338  av_log(avctx, AV_LOG_ERROR, "ignoring pic after %X\n", last_code);
2339  if (avctx->err_recognition & AV_EF_EXPLODE)
2340  return AVERROR_INVALIDDATA;
2341  }
2342  break;
2343  case EXT_START_CODE:
2344  init_get_bits(&s2->gb, buf_ptr, input_size*8);
2345 
2346  switch (get_bits(&s2->gb, 4)) {
2347  case 0x1:
2348  if (last_code == 0) {
2350  } else {
2351  av_log(avctx, AV_LOG_ERROR, "ignoring seq ext after %X\n", last_code);
2352  if (avctx->err_recognition & AV_EF_EXPLODE)
2353  return AVERROR_INVALIDDATA;
2354  }
2355  break;
2356  case 0x2:
2358  break;
2359  case 0x3:
2361  break;
2362  case 0x7:
2364  break;
2365  case 0x8:
2366  if (last_code == PICTURE_START_CODE) {
2368  } else {
2369  av_log(avctx, AV_LOG_ERROR, "ignoring pic cod ext after %X\n", last_code);
2370  if (avctx->err_recognition & AV_EF_EXPLODE)
2371  return AVERROR_INVALIDDATA;
2372  }
2373  break;
2374  }
2375  break;
2376  case USER_START_CODE:
2377  mpeg_decode_user_data(avctx, buf_ptr, input_size);
2378  break;
2379  case GOP_START_CODE:
2380  if (last_code == 0) {
2381  s2->first_field=0;
2382  mpeg_decode_gop(avctx, buf_ptr, input_size);
2383  s->sync=1;
2384  } else {
2385  av_log(avctx, AV_LOG_ERROR, "ignoring GOP_START_CODE after %X\n", last_code);
2386  if (avctx->err_recognition & AV_EF_EXPLODE)
2387  return AVERROR_INVALIDDATA;
2388  }
2389  break;
2390  default:
2391  if (start_code >= SLICE_MIN_START_CODE &&
2392  start_code <= SLICE_MAX_START_CODE && last_code != 0) {
2393  const int field_pic = s2->picture_structure != PICT_FRAME;
2394  int mb_y = (start_code - SLICE_MIN_START_CODE) << field_pic;
2395  last_code = SLICE_MIN_START_CODE;
2396 
2398  mb_y++;
2399 
2400  if (mb_y >= s2->mb_height) {
2401  av_log(s2->avctx, AV_LOG_ERROR, "slice below image (%d >= %d)\n", mb_y, s2->mb_height);
2402  return -1;
2403  }
2404 
2405  if (s2->last_picture_ptr == NULL) {
2406  /* Skip B-frames if we do not have reference frames and gop is not closed */
2407  if (s2->pict_type == AV_PICTURE_TYPE_B) {
2408  if (!s->closed_gop)
2409  break;
2410  }
2411  }
2412  if (s2->pict_type == AV_PICTURE_TYPE_I)
2413  s->sync=1;
2414  if (s2->next_picture_ptr == NULL) {
2415  /* Skip P-frames if we do not have a reference frame or we have an invalid header. */
2416  if (s2->pict_type == AV_PICTURE_TYPE_P && !s->sync) break;
2417  }
2418  if ((avctx->skip_frame >= AVDISCARD_NONREF && s2->pict_type == AV_PICTURE_TYPE_B) ||
2419  (avctx->skip_frame >= AVDISCARD_NONKEY && s2->pict_type != AV_PICTURE_TYPE_I) ||
2420  avctx->skip_frame >= AVDISCARD_ALL)
2421  break;
2422 
2423  if (!s->mpeg_enc_ctx_allocated)
2424  break;
2425 
2426  if (s2->codec_id == CODEC_ID_MPEG2VIDEO) {
2427  if (mb_y < avctx->skip_top || mb_y >= s2->mb_height - avctx->skip_bottom)
2428  break;
2429  }
2430 
2431  if (!s2->pict_type) {
2432  av_log(avctx, AV_LOG_ERROR, "Missing picture start code\n");
2433  if (avctx->err_recognition & AV_EF_EXPLODE)
2434  return AVERROR_INVALIDDATA;
2435  break;
2436  }
2437 
2438  if (s2->first_slice) {
2439  s2->first_slice = 0;
2440  if (mpeg_field_start(s2, buf, buf_size) < 0)
2441  return -1;
2442  }
2443  if (!s2->current_picture_ptr) {
2444  av_log(avctx, AV_LOG_ERROR, "current_picture not initialized\n");
2445  return AVERROR_INVALIDDATA;
2446  }
2447 
2448  if (avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU) {
2449  s->slice_count++;
2450  break;
2451  }
2452 
2453  if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE)) {
2454  int threshold = (s2->mb_height * s->slice_count +
2455  s2->slice_context_count / 2) /
2456  s2->slice_context_count;
2457  if (threshold <= mb_y) {
2458  MpegEncContext *thread_context = s2->thread_context[s->slice_count];
2459 
2460  thread_context->start_mb_y = mb_y;
2461  thread_context->end_mb_y = s2->mb_height;
2462  if (s->slice_count) {
2463  s2->thread_context[s->slice_count-1]->end_mb_y = mb_y;
2464  ff_update_duplicate_context(thread_context, s2);
2465  }
2466  init_get_bits(&thread_context->gb, buf_ptr, input_size*8);
2467  s->slice_count++;
2468  }
2469  buf_ptr += 2; // FIXME add minimum number of bytes per slice
2470  } else {
2471  ret = mpeg_decode_slice(s2, mb_y, &buf_ptr, input_size);
2472  emms_c();
2473 
2474  if (ret < 0) {
2475  if (avctx->err_recognition & AV_EF_EXPLODE)
2476  return ret;
2477  if (s2->resync_mb_x >= 0 && s2->resync_mb_y >= 0)
2479  } else {
2481  }
2482  }
2483  }
2484  break;
2485  }
2486  }
2487 }
2488 
2489 static void flush(AVCodecContext *avctx)
2490 {
2491  Mpeg1Context *s = avctx->priv_data;
2492 
2493  s->sync=0;
2494  s->closed_gop = 0;
2495 
2496  ff_mpeg_flush(avctx);
2497 }
2498 
2500 {
2501  Mpeg1Context *s = avctx->priv_data;
2502 
2503  if (s->mpeg_enc_ctx_allocated)
2505  return 0;
2506 }
2507 
2509  { FF_PROFILE_MPEG2_422, "4:2:2" },
2510  { FF_PROFILE_MPEG2_HIGH, "High" },
2511  { FF_PROFILE_MPEG2_SS, "Spatially Scalable" },
2512  { FF_PROFILE_MPEG2_SNR_SCALABLE, "SNR Scalable" },
2513  { FF_PROFILE_MPEG2_MAIN, "Main" },
2514  { FF_PROFILE_MPEG2_SIMPLE, "Simple" },
2515  { FF_PROFILE_RESERVED, "Reserved" },
2516  { FF_PROFILE_RESERVED, "Reserved" },
2517  { FF_PROFILE_UNKNOWN },
2518 };
2519 
2520 
2522  .name = "mpeg1video",
2523  .type = AVMEDIA_TYPE_VIDEO,
2524  .id = CODEC_ID_MPEG1VIDEO,
2525  .priv_data_size = sizeof(Mpeg1Context),
2530  .flush = flush,
2531  .max_lowres = 3,
2532  .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video"),
2534 };
2535 
2537  .name = "mpeg2video",
2538  .type = AVMEDIA_TYPE_VIDEO,
2539  .id = CODEC_ID_MPEG2VIDEO,
2540  .priv_data_size = sizeof(Mpeg1Context),
2545  .flush = flush,
2546  .max_lowres = 3,
2547  .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 video"),
2548  .profiles = NULL_IF_CONFIG_SMALL(mpeg2_video_profiles),
2549 };
2550 
2551 #if CONFIG_MPEG_XVMC_DECODER
2552 static av_cold int mpeg_mc_decode_init(AVCodecContext *avctx)
2553 {
2554  if (avctx->active_thread_type & FF_THREAD_SLICE)
2555  return -1;
2556  if (!(avctx->slice_flags & SLICE_FLAG_CODED_ORDER))
2557  return -1;
2558  if (!(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD)) {
2559  av_dlog(avctx, "mpeg12.c: XvMC decoder will work better if SLICE_FLAG_ALLOW_FIELD is set\n");
2560  }
2561  mpeg_decode_init(avctx);
2562 
2564  avctx->xvmc_acceleration = 2; // 2 - the blocks are packed!
2565 
2566  return 0;
2567 }
2568 
2569 AVCodec ff_mpeg_xvmc_decoder = {
2570  .name = "mpegvideo_xvmc",
2571  .type = AVMEDIA_TYPE_VIDEO,
2573  .priv_data_size = sizeof(Mpeg1Context),
2574  .init = mpeg_mc_decode_init,
2578  .flush = flush,
2579  .long_name = NULL_IF_CONFIG_SMALL("MPEG-1/2 video XvMC (X-Video Motion Compensation)"),
2580 };
2581 
2582 #endif
2583 
2584 #if CONFIG_MPEG_VDPAU_DECODER
2585 AVCodec ff_mpeg_vdpau_decoder = {
2586  .name = "mpegvideo_vdpau",
2587  .type = AVMEDIA_TYPE_VIDEO,
2588  .id = CODEC_ID_MPEG2VIDEO,
2589  .priv_data_size = sizeof(Mpeg1Context),
2594  .flush = flush,
2595  .long_name = NULL_IF_CONFIG_SMALL("MPEG-1/2 video (VDPAU acceleration)"),
2596 };
2597 #endif
2598 
2599 #if CONFIG_MPEG1_VDPAU_DECODER
2600 AVCodec ff_mpeg1_vdpau_decoder = {
2601  .name = "mpeg1video_vdpau",
2602  .type = AVMEDIA_TYPE_VIDEO,
2603  .id = CODEC_ID_MPEG1VIDEO,
2604  .priv_data_size = sizeof(Mpeg1Context),
2609  .flush = flush,
2610  .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video (VDPAU acceleration)"),
2611 };
2612 #endif
2613