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