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 == AV_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 == AV_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 
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 == AV_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 AVPixelFormat pixfmt_xvmc_mpg2_420[] = {
1187  AV_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 == AV_CODEC_ID_MPEG1VIDEO)
1198  return AV_PIX_FMT_VDPAU_MPEG1;
1199  else
1200  return AV_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 AV_PIX_FMT_YUV422P;
1206  else
1207  return AV_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  ff_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  if (avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
1251  //MPEG-1 fps
1254  //MPEG-1 aspect
1256  avctx->ticks_per_frame=1;
1257  } else {//MPEG-2
1258  //MPEG-2 fps
1260  &s->avctx->time_base.num,
1263  1 << 30);
1264  avctx->ticks_per_frame = 2;
1265  //MPEG-2 aspect
1266  if (s->aspect_ratio_info > 1) {
1267  AVRational dar =
1269  (AVRational) {s1->pan_scan.width, s1->pan_scan.height}),
1270  (AVRational) {s->width, s->height});
1271 
1272  // we ignore the spec here and guess a bit as reality does not match the spec, see for example
1273  // res_change_ffmpeg_aspect.ts and sequence-display-aspect.mpg
1274  // issue1613, 621, 562
1275  if ((s1->pan_scan.width == 0) || (s1->pan_scan.height == 0) ||
1276  (av_cmp_q(dar, (AVRational) {4, 3}) && av_cmp_q(dar, (AVRational) {16, 9}))) {
1279  (AVRational) {s->width, s->height});
1280  } else {
1283  (AVRational) {s1->pan_scan.width, s1->pan_scan.height});
1284 //issue1613 4/3 16/9 -> 16/9
1285 //res_change_ffmpeg_aspect.ts 4/3 225/44 ->4/3
1286 //widescreen-issue562.mpg 4/3 16/9 -> 16/9
1287 // s->avctx->sample_aspect_ratio = av_mul_q(s->avctx->sample_aspect_ratio, (AVRational) {s->width, s->height});
1288  av_dlog(avctx, "A %d/%d\n",
1290  av_dlog(avctx, "B %d/%d\n", s->avctx->sample_aspect_ratio.num,
1292  }
1293  } else {
1294  s->avctx->sample_aspect_ratio =
1295  ff_mpeg2_aspect[s->aspect_ratio_info];
1296  }
1297  } // MPEG-2
1298 
1299  avctx->pix_fmt = mpeg_get_pixelformat(avctx);
1300  avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
1301  // until then pix_fmt may be changed right after codec init
1302  if (avctx->pix_fmt == AV_PIX_FMT_XVMC_MPEG2_IDCT ||
1303  avctx->hwaccel ||
1304  s->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU)
1305  if (avctx->idct_algo == FF_IDCT_AUTO)
1306  avctx->idct_algo = FF_IDCT_SIMPLE;
1307 
1308  /* Quantization matrices may need reordering
1309  * if DCT permutation is changed. */
1310  memcpy(old_permutation, s->dsp.idct_permutation, 64 * sizeof(uint8_t));
1311 
1312  if (ff_MPV_common_init(s) < 0)
1313  return -2;
1314 
1315  quant_matrix_rebuild(s->intra_matrix, old_permutation, s->dsp.idct_permutation);
1316  quant_matrix_rebuild(s->inter_matrix, old_permutation, s->dsp.idct_permutation);
1317  quant_matrix_rebuild(s->chroma_intra_matrix, old_permutation, s->dsp.idct_permutation);
1318  quant_matrix_rebuild(s->chroma_inter_matrix, old_permutation, s->dsp.idct_permutation);
1319 
1320  s1->mpeg_enc_ctx_allocated = 1;
1321  }
1322  return 0;
1323 }
1324 
1326  const uint8_t *buf, int buf_size)
1327 {
1328  Mpeg1Context *s1 = avctx->priv_data;
1329  MpegEncContext *s = &s1->mpeg_enc_ctx;
1330  int ref, f_code, vbv_delay;
1331 
1332  init_get_bits(&s->gb, buf, buf_size*8);
1333 
1334  ref = get_bits(&s->gb, 10); /* temporal ref */
1335  s->pict_type = get_bits(&s->gb, 3);
1336  if (s->pict_type == 0 || s->pict_type > 3)
1337  return -1;
1338 
1339  vbv_delay = get_bits(&s->gb, 16);
1341  s->full_pel[0] = get_bits1(&s->gb);
1342  f_code = get_bits(&s->gb, 3);
1343  if (f_code == 0 && (avctx->err_recognition & AV_EF_BITSTREAM))
1344  return -1;
1345  s->mpeg_f_code[0][0] = f_code;
1346  s->mpeg_f_code[0][1] = f_code;
1347  }
1348  if (s->pict_type == AV_PICTURE_TYPE_B) {
1349  s->full_pel[1] = get_bits1(&s->gb);
1350  f_code = get_bits(&s->gb, 3);
1351  if (f_code == 0 && (avctx->err_recognition & AV_EF_BITSTREAM))
1352  return -1;
1353  s->mpeg_f_code[1][0] = f_code;
1354  s->mpeg_f_code[1][1] = f_code;
1355  }
1358 
1359  if (avctx->debug & FF_DEBUG_PICT_INFO)
1360  av_log(avctx, AV_LOG_DEBUG, "vbv_delay %d, ref %d type:%d\n", vbv_delay, ref, s->pict_type);
1361 
1362  s->y_dc_scale = 8;
1363  s->c_dc_scale = 8;
1364  return 0;
1365 }
1366 
1368 {
1369  MpegEncContext *s= &s1->mpeg_enc_ctx;
1370  int horiz_size_ext, vert_size_ext;
1371  int bit_rate_ext;
1372 
1373  skip_bits(&s->gb, 1); /* profile and level esc*/
1374  s->avctx->profile = get_bits(&s->gb, 3);
1375  s->avctx->level = get_bits(&s->gb, 4);
1376  s->progressive_sequence = get_bits1(&s->gb); /* progressive_sequence */
1377  s->chroma_format = get_bits(&s->gb, 2); /* chroma_format 1=420, 2=422, 3=444 */
1378  horiz_size_ext = get_bits(&s->gb, 2);
1379  vert_size_ext = get_bits(&s->gb, 2);
1380  s->width |= (horiz_size_ext << 12);
1381  s->height |= (vert_size_ext << 12);
1382 
1383  bit_rate_ext = get_bits(&s->gb, 12) << 18;
1384  if (bit_rate_ext < INT_MAX / 400 &&
1385  bit_rate_ext * 400 < INT_MAX - s->bit_rate) {
1386  s->bit_rate += bit_rate_ext * 400;
1387  } else {
1388  av_log(s->avctx, AV_LOG_WARNING, "Invalid bit rate extension value: %d\n",
1389  bit_rate_ext >> 18);
1390  s->bit_rate = 0;
1391  }
1392 
1393  skip_bits1(&s->gb); /* marker */
1394  s->avctx->rc_buffer_size += get_bits(&s->gb, 8) * 1024 * 16 << 10;
1395 
1396  s->low_delay = get_bits1(&s->gb);
1397  if (s->flags & CODEC_FLAG_LOW_DELAY)
1398  s->low_delay = 1;
1399 
1400  s1->frame_rate_ext.num = get_bits(&s->gb, 2) + 1;
1401  s1->frame_rate_ext.den = get_bits(&s->gb, 5) + 1;
1402 
1403  av_dlog(s->avctx, "sequence extension\n");
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_WARNING, "invalid frame_pred_frame_dct\n");
1546  }
1547 
1548  if (s->picture_structure == PICT_FRAME) {
1549  s->first_field = 0;
1550  s->v_edge_pos = 16 * s->mb_height;
1551  } else {
1552  s->first_field ^= 1;
1553  s->v_edge_pos = 8 * s->mb_height;
1554  memset(s->mbskip_table, 0, s->mb_stride * s->mb_height);
1555  }
1556 
1557  if (s->alternate_scan) {
1560  } else {
1563  }
1564 
1565  /* composite display not parsed */
1566  av_dlog(s->avctx, "intra_dc_precision=%d\n", s->intra_dc_precision);
1567  av_dlog(s->avctx, "picture_structure=%d\n", s->picture_structure);
1568  av_dlog(s->avctx, "top field first=%d\n", s->top_field_first);
1569  av_dlog(s->avctx, "repeat first field=%d\n", s->repeat_first_field);
1570  av_dlog(s->avctx, "conceal=%d\n", s->concealment_motion_vectors);
1571  av_dlog(s->avctx, "intra_vlc_format=%d\n", s->intra_vlc_format);
1572  av_dlog(s->avctx, "alternate_scan=%d\n", s->alternate_scan);
1573  av_dlog(s->avctx, "frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
1574  av_dlog(s->avctx, "progressive_frame=%d\n", s->progressive_frame);
1575 }
1576 
1577 static int mpeg_field_start(MpegEncContext *s, const uint8_t *buf, int buf_size)
1578 {
1579  AVCodecContext *avctx = s->avctx;
1580  Mpeg1Context *s1 = (Mpeg1Context*)s;
1581 
1582  /* start frame decoding */
1583  if (s->first_field || s->picture_structure == PICT_FRAME) {
1584  if (ff_MPV_frame_start(s, avctx) < 0)
1585  return -1;
1586 
1588 
1589  /* first check if we must repeat the frame */
1591  if (s->repeat_first_field) {
1592  if (s->progressive_sequence) {
1593  if (s->top_field_first)
1595  else
1597  } else if (s->progressive_frame) {
1599  }
1600  }
1601 
1603 
1605  ff_thread_finish_setup(avctx);
1606  } else { // second field
1607  int i;
1608 
1609  if (!s->current_picture_ptr) {
1610  av_log(s->avctx, AV_LOG_ERROR, "first field missing\n");
1611  return -1;
1612  }
1613 
1614  if (s->avctx->hwaccel &&
1616  if (s->avctx->hwaccel->end_frame(s->avctx) < 0)
1617  av_log(avctx, AV_LOG_ERROR, "hardware accelerator failed to decode first field\n");
1618  }
1619 
1620  for (i = 0; i < 4; i++) {
1624  }
1625  }
1626  }
1627 
1628  if (avctx->hwaccel) {
1629  if (avctx->hwaccel->start_frame(avctx, buf, buf_size) < 0)
1630  return -1;
1631  }
1632 
1633 // MPV_frame_start will call this function too,
1634 // but we need to call it on every field
1636  if (ff_xvmc_field_start(s, avctx) < 0)
1637  return -1;
1638 
1639  return 0;
1640 }
1641 
1642 #define DECODE_SLICE_ERROR -1
1643 #define DECODE_SLICE_OK 0
1644 
1651 static int mpeg_decode_slice(MpegEncContext *s, int mb_y,
1652  const uint8_t **buf, int buf_size)
1653 {
1654  AVCodecContext *avctx = s->avctx;
1655  const int field_pic = s->picture_structure != PICT_FRAME;
1656 
1657  s->resync_mb_x =
1658  s->resync_mb_y = -1;
1659 
1660  assert(mb_y < s->mb_height);
1661 
1662  init_get_bits(&s->gb, *buf, buf_size * 8);
1663 
1665  s->interlaced_dct = 0;
1666 
1667  s->qscale = get_qscale(s);
1668 
1669  if (s->qscale == 0) {
1670  av_log(s->avctx, AV_LOG_ERROR, "qscale == 0\n");
1671  return -1;
1672  }
1673 
1674  /* extra slice info */
1675  while (get_bits1(&s->gb) != 0) {
1676  skip_bits(&s->gb, 8);
1677  }
1678 
1679  s->mb_x = 0;
1680 
1681  if (mb_y == 0 && s->codec_tag == AV_RL32("SLIF")) {
1682  skip_bits1(&s->gb);
1683  } else {
1684  while (get_bits_left(&s->gb) > 0) {
1685  int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
1686  if (code < 0) {
1687  av_log(s->avctx, AV_LOG_ERROR, "first mb_incr damaged\n");
1688  return -1;
1689  }
1690  if (code >= 33) {
1691  if (code == 33) {
1692  s->mb_x += 33;
1693  }
1694  /* otherwise, stuffing, nothing to do */
1695  } else {
1696  s->mb_x += code;
1697  break;
1698  }
1699  }
1700  }
1701 
1702  if (s->mb_x >= (unsigned)s->mb_width) {
1703  av_log(s->avctx, AV_LOG_ERROR, "initial skip overflow\n");
1704  return -1;
1705  }
1706 
1707  if (avctx->hwaccel) {
1708  const uint8_t *buf_end, *buf_start = *buf - 4; /* include start_code */
1709  int start_code = -1;
1710  buf_end = avpriv_mpv_find_start_code(buf_start + 2, *buf + buf_size, &start_code);
1711  if (buf_end < *buf + buf_size)
1712  buf_end -= 4;
1713  s->mb_y = mb_y;
1714  if (avctx->hwaccel->decode_slice(avctx, buf_start, buf_end - buf_start) < 0)
1715  return DECODE_SLICE_ERROR;
1716  *buf = buf_end;
1717  return DECODE_SLICE_OK;
1718  }
1719 
1720  s->resync_mb_x = s->mb_x;
1721  s->resync_mb_y = s->mb_y = mb_y;
1722  s->mb_skip_run = 0;
1724 
1725  if (s->mb_y == 0 && s->mb_x == 0 && (s->first_field || s->picture_structure == PICT_FRAME)) {
1726  if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
1727  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",
1728  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],
1729  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")),
1730  s->progressive_sequence ? "ps" :"", s->progressive_frame ? "pf" : "", s->alternate_scan ? "alt" :"", s->top_field_first ? "top" :"",
1733  }
1734  }
1735 
1736  for (;;) {
1737  // If 1, we memcpy blocks in xvmcvideo.
1739  ff_xvmc_init_block(s); // set s->block
1740 
1741  if (mpeg_decode_mb(s, s->block) < 0)
1742  return -1;
1743 
1744  if (s->current_picture.f.motion_val[0] && !s->encoding) { // note motion_val is normally NULL unless we want to extract the MVs
1745  const int wrap = s->b8_stride;
1746  int xy = s->mb_x * 2 + s->mb_y * 2 * wrap;
1747  int b8_xy = 4 * (s->mb_x + s->mb_y * s->mb_stride);
1748  int motion_x, motion_y, dir, i;
1749 
1750  for (i = 0; i < 2; i++) {
1751  for (dir = 0; dir < 2; dir++) {
1752  if (s->mb_intra || (dir == 1 && s->pict_type != AV_PICTURE_TYPE_B)) {
1753  motion_x = motion_y = 0;
1754  } else if (s->mv_type == MV_TYPE_16X16 || (s->mv_type == MV_TYPE_FIELD && field_pic)) {
1755  motion_x = s->mv[dir][0][0];
1756  motion_y = s->mv[dir][0][1];
1757  } else /*if ((s->mv_type == MV_TYPE_FIELD) || (s->mv_type == MV_TYPE_16X8))*/ {
1758  motion_x = s->mv[dir][i][0];
1759  motion_y = s->mv[dir][i][1];
1760  }
1761 
1762  s->current_picture.f.motion_val[dir][xy ][0] = motion_x;
1763  s->current_picture.f.motion_val[dir][xy ][1] = motion_y;
1764  s->current_picture.f.motion_val[dir][xy + 1][0] = motion_x;
1765  s->current_picture.f.motion_val[dir][xy + 1][1] = motion_y;
1766  s->current_picture.f.ref_index [dir][b8_xy ] =
1767  s->current_picture.f.ref_index [dir][b8_xy + 1] = s->field_select[dir][i];
1768  assert(s->field_select[dir][i] == 0 || s->field_select[dir][i] == 1);
1769  }
1770  xy += wrap;
1771  b8_xy +=2;
1772  }
1773  }
1774 
1775  s->dest[0] += 16;
1776  s->dest[1] += 16 >> s->chroma_x_shift;
1777  s->dest[2] += 16 >> s->chroma_x_shift;
1778 
1779  ff_MPV_decode_mb(s, s->block);
1780 
1781  if (++s->mb_x >= s->mb_width) {
1782  const int mb_size = 16;
1783 
1784  ff_mpeg_draw_horiz_band(s, mb_size*(s->mb_y >> field_pic), mb_size);
1786 
1787  s->mb_x = 0;
1788  s->mb_y += 1 << field_pic;
1789 
1790  if (s->mb_y >= s->mb_height) {
1791  int left = get_bits_left(&s->gb);
1792  int is_d10 = s->chroma_format == 2 && s->pict_type == AV_PICTURE_TYPE_I && avctx->profile == 0 && avctx->level == 5
1793  && s->intra_dc_precision == 2 && s->q_scale_type == 1 && s->alternate_scan == 0
1794  && s->progressive_frame == 0 /* vbv_delay == 0xBBB || 0xE10*/;
1795 
1796  if (left < 0 || (left && show_bits(&s->gb, FFMIN(left, 23)) && !is_d10)
1797  || ((avctx->err_recognition & AV_EF_BUFFER) && left > 8)) {
1798  av_log(avctx, AV_LOG_ERROR, "end mismatch left=%d %0X\n", left, show_bits(&s->gb, FFMIN(left, 23)));
1799  return -1;
1800  } else
1801  goto eos;
1802  }
1803 
1805  }
1806 
1807  /* skip mb handling */
1808  if (s->mb_skip_run == -1) {
1809  /* read increment again */
1810  s->mb_skip_run = 0;
1811  for (;;) {
1812  int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
1813  if (code < 0) {
1814  av_log(s->avctx, AV_LOG_ERROR, "mb incr damaged\n");
1815  return -1;
1816  }
1817  if (code >= 33) {
1818  if (code == 33) {
1819  s->mb_skip_run += 33;
1820  } else if (code == 35) {
1821  if (s->mb_skip_run != 0 || show_bits(&s->gb, 15) != 0) {
1822  av_log(s->avctx, AV_LOG_ERROR, "slice mismatch\n");
1823  return -1;
1824  }
1825  goto eos; /* end of slice */
1826  }
1827  /* otherwise, stuffing, nothing to do */
1828  } else {
1829  s->mb_skip_run += code;
1830  break;
1831  }
1832  }
1833  if (s->mb_skip_run) {
1834  int i;
1835  if (s->pict_type == AV_PICTURE_TYPE_I) {
1836  av_log(s->avctx, AV_LOG_ERROR, "skipped MB in I frame at %d %d\n", s->mb_x, s->mb_y);
1837  return -1;
1838  }
1839 
1840  /* skip mb */
1841  s->mb_intra = 0;
1842  for (i = 0; i < 12; i++)
1843  s->block_last_index[i] = -1;
1845  s->mv_type = MV_TYPE_16X16;
1846  else
1847  s->mv_type = MV_TYPE_FIELD;
1848  if (s->pict_type == AV_PICTURE_TYPE_P) {
1849  /* if P type, zero motion vector is implied */
1850  s->mv_dir = MV_DIR_FORWARD;
1851  s->mv[0][0][0] = s->mv[0][0][1] = 0;
1852  s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
1853  s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
1854  s->field_select[0][0] = (s->picture_structure - 1) & 1;
1855  } else {
1856  /* if B type, reuse previous vectors and directions */
1857  s->mv[0][0][0] = s->last_mv[0][0][0];
1858  s->mv[0][0][1] = s->last_mv[0][0][1];
1859  s->mv[1][0][0] = s->last_mv[1][0][0];
1860  s->mv[1][0][1] = s->last_mv[1][0][1];
1861  }
1862  }
1863  }
1864  }
1865 eos: // end of slice
1866  *buf += (get_bits_count(&s->gb)-1)/8;
1867  av_dlog(s, "y %d %d %d %d\n", s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y);
1868  return 0;
1869 }
1870 
1871 static int slice_decode_thread(AVCodecContext *c, void *arg)
1872 {
1873  MpegEncContext *s = *(void**)arg;
1874  const uint8_t *buf = s->gb.buffer;
1875  int mb_y = s->start_mb_y;
1876  const int field_pic = s->picture_structure != PICT_FRAME;
1877 
1878  s->er.error_count = (3 * (s->end_mb_y - s->start_mb_y) * s->mb_width) >> field_pic;
1879 
1880  for (;;) {
1881  uint32_t start_code;
1882  int ret;
1883 
1884  ret = mpeg_decode_slice(s, mb_y, &buf, s->gb.buffer_end - buf);
1885  emms_c();
1886  av_dlog(c, "ret:%d resync:%d/%d mb:%d/%d ts:%d/%d ec:%d\n",
1887  ret, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y,
1888  s->start_mb_y, s->end_mb_y, s->er.error_count);
1889  if (ret < 0) {
1890  if (c->err_recognition & AV_EF_EXPLODE)
1891  return ret;
1892  if (s->resync_mb_x >= 0 && s->resync_mb_y >= 0)
1894  } else {
1896  }
1897 
1898  if (s->mb_y == s->end_mb_y)
1899  return 0;
1900 
1901  start_code = -1;
1902  buf = avpriv_mpv_find_start_code(buf, s->gb.buffer_end, &start_code);
1903  mb_y= (start_code - SLICE_MIN_START_CODE) << field_pic;
1905  mb_y++;
1906  if (mb_y < 0 || mb_y >= s->end_mb_y)
1907  return -1;
1908  }
1909 }
1910 
1915 static int slice_end(AVCodecContext *avctx, AVFrame *pict)
1916 {
1917  Mpeg1Context *s1 = avctx->priv_data;
1918  MpegEncContext *s = &s1->mpeg_enc_ctx;
1919 
1921  return 0;
1922 
1923  if (s->avctx->hwaccel) {
1924  if (s->avctx->hwaccel->end_frame(s->avctx) < 0)
1925  av_log(avctx, AV_LOG_ERROR, "hardware accelerator failed to decode picture\n");
1926  }
1927 
1929  ff_xvmc_field_end(s);
1930 
1931  /* end of slice reached */
1932  if (/*s->mb_y << field_pic == s->mb_height &&*/ !s->first_field) {
1933  /* end of image */
1934 
1936 
1937  ff_er_frame_end(&s->er);
1938 
1939  ff_MPV_frame_end(s);
1940 
1941  if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
1942  *pict = s->current_picture_ptr->f;
1943  ff_print_debug_info(s, pict);
1944  } else {
1945  if (avctx->active_thread_type & FF_THREAD_FRAME)
1946  s->picture_number++;
1947  /* latency of 1 frame for I- and P-frames */
1948  /* XXX: use another variable than picture_number */
1949  if (s->last_picture_ptr != NULL) {
1950  *pict = s->last_picture_ptr->f;
1951  ff_print_debug_info(s, pict);
1952  }
1953  }
1954 
1955  return 1;
1956  } else {
1957  return 0;
1958  }
1959 }
1960 
1962  const uint8_t *buf, int buf_size)
1963 {
1964  Mpeg1Context *s1 = avctx->priv_data;
1965  MpegEncContext *s = &s1->mpeg_enc_ctx;
1966  int width, height;
1967  int i, v, j;
1968 
1969  init_get_bits(&s->gb, buf, buf_size*8);
1970 
1971  width = get_bits(&s->gb, 12);
1972  height = get_bits(&s->gb, 12);
1973  if (width <= 0 || height <= 0)
1974  return -1;
1975  s->aspect_ratio_info = get_bits(&s->gb, 4);
1976  if (s->aspect_ratio_info == 0) {
1977  av_log(avctx, AV_LOG_ERROR, "aspect ratio has forbidden 0 value\n");
1978  if (avctx->err_recognition & AV_EF_BITSTREAM)
1979  return -1;
1980  }
1981  s->frame_rate_index = get_bits(&s->gb, 4);
1982  if (s->frame_rate_index == 0 || s->frame_rate_index > 13)
1983  return -1;
1984  s->bit_rate = get_bits(&s->gb, 18) * 400;
1985  if (get_bits1(&s->gb) == 0) /* marker */
1986  return -1;
1987  s->width = width;
1988  s->height = height;
1989 
1990  s->avctx->rc_buffer_size = get_bits(&s->gb, 10) * 1024 * 16;
1991  skip_bits(&s->gb, 1);
1992 
1993  /* get matrix */
1994  if (get_bits1(&s->gb)) {
1996  } else {
1997  for (i = 0; i < 64; i++) {
1998  j = s->dsp.idct_permutation[i];
2000  s->intra_matrix[j] = v;
2001  s->chroma_intra_matrix[j] = v;
2002  }
2003  }
2004  if (get_bits1(&s->gb)) {
2006  } else {
2007  for (i = 0; i < 64; i++) {
2008  int j = s->dsp.idct_permutation[i];
2010  s->inter_matrix[j] = v;
2011  s->chroma_inter_matrix[j] = v;
2012  }
2013  }
2014 
2015  if (show_bits(&s->gb, 23) != 0) {
2016  av_log(s->avctx, AV_LOG_ERROR, "sequence header damaged\n");
2017  return -1;
2018  }
2019 
2020  /* we set MPEG-2 parameters so that it emulates MPEG-1 */
2021  s->progressive_sequence = 1;
2022  s->progressive_frame = 1;
2024  s->frame_pred_frame_dct = 1;
2025  s->chroma_format = 1;
2027  s->out_format = FMT_MPEG1;
2028  s->swap_uv = 0; // AFAIK VCR2 does not have SEQ_HEADER
2029  if (s->flags & CODEC_FLAG_LOW_DELAY)
2030  s->low_delay = 1;
2031 
2032  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
2033  av_log(s->avctx, AV_LOG_DEBUG, "vbv buffer: %d, bitrate:%d\n",
2034  s->avctx->rc_buffer_size, s->bit_rate);
2035 
2036  return 0;
2037 }
2038 
2040 {
2041  Mpeg1Context *s1 = avctx->priv_data;
2042  MpegEncContext *s = &s1->mpeg_enc_ctx;
2043  int i, v;
2044 
2045  /* start new MPEG-1 context decoding */
2046  s->out_format = FMT_MPEG1;
2047  if (s1->mpeg_enc_ctx_allocated) {
2048  ff_MPV_common_end(s);
2049  }
2050  s->width = avctx->coded_width;
2051  s->height = avctx->coded_height;
2052  avctx->has_b_frames = 0; // true?
2053  s->low_delay = 1;
2054 
2055  avctx->pix_fmt = mpeg_get_pixelformat(avctx);
2056  avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
2057 
2058  if (avctx->pix_fmt == AV_PIX_FMT_XVMC_MPEG2_IDCT || avctx->hwaccel ||
2060  if (avctx->idct_algo == FF_IDCT_AUTO)
2061  avctx->idct_algo = FF_IDCT_SIMPLE;
2062 
2063  if (ff_MPV_common_init(s) < 0)
2064  return -1;
2065  exchange_uv(s); // common init reset pblocks, so we swap them here
2066  s->swap_uv = 1; // in case of xvmc we need to swap uv for each MB
2067  s1->mpeg_enc_ctx_allocated = 1;
2068 
2069  for (i = 0; i < 64; i++) {
2070  int j = s->dsp.idct_permutation[i];
2072  s->intra_matrix[j] = v;
2073  s->chroma_intra_matrix[j] = v;
2074 
2076  s->inter_matrix[j] = v;
2077  s->chroma_inter_matrix[j] = v;
2078  }
2079 
2080  s->progressive_sequence = 1;
2081  s->progressive_frame = 1;
2083  s->frame_pred_frame_dct = 1;
2084  s->chroma_format = 1;
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 }
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->frame_start_found = 0;
2187  pc->state=-1;
2188  return i+1;
2189  }
2190  if (pc->frame_start_found == 2 && state == SEQ_START_CODE)
2191  pc->frame_start_found = 0;
2192  if (pc->frame_start_found < 4 && state == EXT_START_CODE)
2193  pc->frame_start_found++;
2194  if (pc->frame_start_found == 4 && (state & 0xFFFFFF00) == 0x100) {
2195  if (state < SLICE_MIN_START_CODE || state > SLICE_MAX_START_CODE) {
2196  pc->frame_start_found = 0;
2197  pc->state = -1;
2198  return i - 3;
2199  }
2200  }
2201  if (pc->frame_start_found == 0 && s && state == PICTURE_START_CODE) {
2202  ff_fetch_timestamp(s, i - 3, 1);
2203  }
2204  }
2205  }
2206  pc->state = state;
2207  return END_NOT_FOUND;
2208 }
2209 
2210 static int decode_chunks(AVCodecContext *avctx,
2211  AVFrame *picture, int *got_output,
2212  const uint8_t *buf, int buf_size)
2213 {
2214  Mpeg1Context *s = avctx->priv_data;
2216  const uint8_t *buf_ptr = buf;
2217  const uint8_t *buf_end = buf + buf_size;
2218  int ret, input_size;
2219  int last_code = 0;
2220 
2221  for (;;) {
2222  /* find next start code */
2223  uint32_t start_code = -1;
2224  buf_ptr = avpriv_mpv_find_start_code(buf_ptr, buf_end, &start_code);
2225  if (start_code > 0x1ff) {
2226  if (s2->pict_type != AV_PICTURE_TYPE_B || avctx->skip_frame <= AVDISCARD_DEFAULT) {
2227  if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE)) {
2228  int i;
2229 
2230  avctx->execute(avctx, slice_decode_thread, &s2->thread_context[0], NULL, s->slice_count, sizeof(void*));
2231  for (i = 0; i < s->slice_count; i++)
2232  s2->er.error_count += s2->thread_context[i]->er.error_count;
2233  }
2234 
2236  ff_vdpau_mpeg_picture_complete(s2, buf, buf_size, s->slice_count);
2237 
2238  if (slice_end(avctx, picture)) {
2239  if (s2->last_picture_ptr || s2->low_delay) //FIXME merge with the stuff in mpeg_decode_slice
2240  *got_output = 1;
2241  }
2242  }
2243  s2->pict_type = 0;
2244  return FFMAX(0, buf_ptr - buf - s2->parse_context.last_index);
2245  }
2246 
2247  input_size = buf_end - buf_ptr;
2248 
2249  if (avctx->debug & FF_DEBUG_STARTCODE) {
2250  av_log(avctx, AV_LOG_DEBUG, "%3X at %td left %d\n", start_code, buf_ptr-buf, input_size);
2251  }
2252 
2253  /* prepare data for next start code */
2254  switch (start_code) {
2255  case SEQ_START_CODE:
2256  if (last_code == 0) {
2257  mpeg1_decode_sequence(avctx, buf_ptr, input_size);
2258  s->sync=1;
2259  } else {
2260  av_log(avctx, AV_LOG_ERROR, "ignoring SEQ_START_CODE after %X\n", last_code);
2261  if (avctx->err_recognition & AV_EF_EXPLODE)
2262  return AVERROR_INVALIDDATA;
2263  }
2264  break;
2265 
2266  case PICTURE_START_CODE:
2267  if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE) && s->slice_count) {
2268  int i;
2269 
2270  avctx->execute(avctx, slice_decode_thread,
2271  s2->thread_context, NULL,
2272  s->slice_count, sizeof(void*));
2273  for (i = 0; i < s->slice_count; i++)
2274  s2->er.error_count += s2->thread_context[i]->er.error_count;
2275  s->slice_count = 0;
2276  }
2277  if (last_code == 0 || last_code == SLICE_MIN_START_CODE) {
2278  ret = mpeg_decode_postinit(avctx);
2279  if (ret < 0) {
2280  av_log(avctx, AV_LOG_ERROR, "mpeg_decode_postinit() failure\n");
2281  return ret;
2282  }
2283 
2284  /* we have a complete image: we try to decompress it */
2285  if (mpeg1_decode_picture(avctx, buf_ptr, input_size) < 0)
2286  s2->pict_type = 0;
2287  s2->first_slice = 1;
2288  last_code = PICTURE_START_CODE;
2289  } else {
2290  av_log(avctx, AV_LOG_ERROR, "ignoring pic after %X\n", last_code);
2291  if (avctx->err_recognition & AV_EF_EXPLODE)
2292  return AVERROR_INVALIDDATA;
2293  }
2294  break;
2295  case EXT_START_CODE:
2296  init_get_bits(&s2->gb, buf_ptr, input_size*8);
2297 
2298  switch (get_bits(&s2->gb, 4)) {
2299  case 0x1:
2300  if (last_code == 0) {
2302  } else {
2303  av_log(avctx, AV_LOG_ERROR, "ignoring seq ext after %X\n", last_code);
2304  if (avctx->err_recognition & AV_EF_EXPLODE)
2305  return AVERROR_INVALIDDATA;
2306  }
2307  break;
2308  case 0x2:
2310  break;
2311  case 0x3:
2313  break;
2314  case 0x7:
2316  break;
2317  case 0x8:
2318  if (last_code == PICTURE_START_CODE) {
2320  } else {
2321  av_log(avctx, AV_LOG_ERROR, "ignoring pic cod ext after %X\n", last_code);
2322  if (avctx->err_recognition & AV_EF_EXPLODE)
2323  return AVERROR_INVALIDDATA;
2324  }
2325  break;
2326  }
2327  break;
2328  case USER_START_CODE:
2329  mpeg_decode_user_data(avctx, buf_ptr, input_size);
2330  break;
2331  case GOP_START_CODE:
2332  if (last_code == 0) {
2333  s2->first_field=0;
2334  mpeg_decode_gop(avctx, buf_ptr, input_size);
2335  s->sync=1;
2336  } else {
2337  av_log(avctx, AV_LOG_ERROR, "ignoring GOP_START_CODE after %X\n", last_code);
2338  if (avctx->err_recognition & AV_EF_EXPLODE)
2339  return AVERROR_INVALIDDATA;
2340  }
2341  break;
2342  default:
2343  if (start_code >= SLICE_MIN_START_CODE &&
2344  start_code <= SLICE_MAX_START_CODE && last_code != 0) {
2345  const int field_pic = s2->picture_structure != PICT_FRAME;
2346  int mb_y = (start_code - SLICE_MIN_START_CODE) << field_pic;
2347  last_code = SLICE_MIN_START_CODE;
2348 
2350  mb_y++;
2351 
2352  if (mb_y >= s2->mb_height) {
2353  av_log(s2->avctx, AV_LOG_ERROR, "slice below image (%d >= %d)\n", mb_y, s2->mb_height);
2354  return -1;
2355  }
2356 
2357  if (s2->last_picture_ptr == NULL) {
2358  /* Skip B-frames if we do not have reference frames and gop is not closed */
2359  if (s2->pict_type == AV_PICTURE_TYPE_B) {
2360  if (!s->closed_gop)
2361  break;
2362  }
2363  }
2364  if (s2->pict_type == AV_PICTURE_TYPE_I)
2365  s->sync=1;
2366  if (s2->next_picture_ptr == NULL) {
2367  /* Skip P-frames if we do not have a reference frame or we have an invalid header. */
2368  if (s2->pict_type == AV_PICTURE_TYPE_P && !s->sync) break;
2369  }
2370  if ((avctx->skip_frame >= AVDISCARD_NONREF && s2->pict_type == AV_PICTURE_TYPE_B) ||
2371  (avctx->skip_frame >= AVDISCARD_NONKEY && s2->pict_type != AV_PICTURE_TYPE_I) ||
2372  avctx->skip_frame >= AVDISCARD_ALL)
2373  break;
2374 
2375  if (!s->mpeg_enc_ctx_allocated)
2376  break;
2377 
2378  if (s2->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
2379  if (mb_y < avctx->skip_top || mb_y >= s2->mb_height - avctx->skip_bottom)
2380  break;
2381  }
2382 
2383  if (!s2->pict_type) {
2384  av_log(avctx, AV_LOG_ERROR, "Missing picture start code\n");
2385  if (avctx->err_recognition & AV_EF_EXPLODE)
2386  return AVERROR_INVALIDDATA;
2387  break;
2388  }
2389 
2390  if (s2->first_slice) {
2391  s2->first_slice = 0;
2392  if (mpeg_field_start(s2, buf, buf_size) < 0)
2393  return -1;
2394  }
2395  if (!s2->current_picture_ptr) {
2396  av_log(avctx, AV_LOG_ERROR, "current_picture not initialized\n");
2397  return AVERROR_INVALIDDATA;
2398  }
2399 
2400  if (avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU) {
2401  s->slice_count++;
2402  break;
2403  }
2404 
2405  if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE)) {
2406  int threshold = (s2->mb_height * s->slice_count +
2407  s2->slice_context_count / 2) /
2408  s2->slice_context_count;
2409  if (threshold <= mb_y) {
2410  MpegEncContext *thread_context = s2->thread_context[s->slice_count];
2411 
2412  thread_context->start_mb_y = mb_y;
2413  thread_context->end_mb_y = s2->mb_height;
2414  if (s->slice_count) {
2415  s2->thread_context[s->slice_count-1]->end_mb_y = mb_y;
2416  ret = ff_update_duplicate_context(thread_context,
2417  s2);
2418  if (ret < 0)
2419  return ret;
2420  }
2421  init_get_bits(&thread_context->gb, buf_ptr, input_size*8);
2422  s->slice_count++;
2423  }
2424  buf_ptr += 2; // FIXME add minimum number of bytes per slice
2425  } else {
2426  ret = mpeg_decode_slice(s2, mb_y, &buf_ptr, input_size);
2427  emms_c();
2428 
2429  if (ret < 0) {
2430  if (avctx->err_recognition & AV_EF_EXPLODE)
2431  return ret;
2432  if (s2->resync_mb_x >= 0 && s2->resync_mb_y >= 0)
2434  } else {
2435  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);
2436  }
2437  }
2438  }
2439  break;
2440  }
2441  }
2442 }
2443 
2445  void *data, int *got_output,
2446  AVPacket *avpkt)
2447 {
2448  const uint8_t *buf = avpkt->data;
2449  int buf_size = avpkt->size;
2450  Mpeg1Context *s = avctx->priv_data;
2451  AVFrame *picture = data;
2453  av_dlog(avctx, "fill_buffer\n");
2454 
2455  if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == SEQ_END_CODE)) {
2456  /* special case for last picture */
2457  if (s2->low_delay == 0 && s2->next_picture_ptr) {
2458  *picture = s2->next_picture_ptr->f;
2459  s2->next_picture_ptr = NULL;
2460 
2461  *got_output = 1;
2462  }
2463  return buf_size;
2464  }
2465 
2466  if (s2->flags & CODEC_FLAG_TRUNCATED) {
2467  int next = ff_mpeg1_find_frame_end(&s2->parse_context, buf, buf_size, NULL);
2468 
2469  if (ff_combine_frame(&s2->parse_context, next, (const uint8_t **)&buf, &buf_size) < 0)
2470  return buf_size;
2471  }
2472 
2473  if (s->mpeg_enc_ctx_allocated == 0 && avctx->codec_tag == AV_RL32("VCR2"))
2474  vcr2_init_sequence(avctx);
2475 
2476  s->slice_count = 0;
2477 
2478  if (avctx->extradata && !s->extradata_decoded) {
2479  int ret = decode_chunks(avctx, picture, got_output, avctx->extradata, avctx->extradata_size);
2480  s->extradata_decoded = 1;
2481  if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE))
2482  return ret;
2483  }
2484 
2485  return decode_chunks(avctx, picture, got_output, buf, buf_size);
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 = AV_CODEC_ID_MPEG1VIDEO,
2525  .priv_data_size = sizeof(Mpeg1Context),
2529  .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
2532  .flush = flush,
2533  .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video"),
2535 };
2536 
2538  .name = "mpeg2video",
2539  .type = AVMEDIA_TYPE_VIDEO,
2540  .id = AV_CODEC_ID_MPEG2VIDEO,
2541  .priv_data_size = sizeof(Mpeg1Context),
2545  .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
2548  .flush = flush,
2549  .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 video"),
2550  .profiles = NULL_IF_CONFIG_SMALL(mpeg2_video_profiles),
2551 };
2552 
2553 #if CONFIG_MPEG_XVMC_DECODER
2554 static av_cold int mpeg_mc_decode_init(AVCodecContext *avctx)
2555 {
2556  if (avctx->active_thread_type & FF_THREAD_SLICE)
2557  return -1;
2558  if (!(avctx->slice_flags & SLICE_FLAG_CODED_ORDER))
2559  return -1;
2560  if (!(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD)) {
2561  av_dlog(avctx, "mpeg12.c: XvMC decoder will work better if SLICE_FLAG_ALLOW_FIELD is set\n");
2562  }
2563  mpeg_decode_init(avctx);
2564 
2566  avctx->xvmc_acceleration = 2; // 2 - the blocks are packed!
2567 
2568  return 0;
2569 }
2570 
2571 AVCodec ff_mpeg_xvmc_decoder = {
2572  .name = "mpegvideo_xvmc",
2573  .type = AVMEDIA_TYPE_VIDEO,
2575  .priv_data_size = sizeof(Mpeg1Context),
2576  .init = mpeg_mc_decode_init,
2579  .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
2581  .flush = flush,
2582  .long_name = NULL_IF_CONFIG_SMALL("MPEG-1/2 video XvMC (X-Video Motion Compensation)"),
2583 };
2584 
2585 #endif
2586 
2587 #if CONFIG_MPEG_VDPAU_DECODER
2588 AVCodec ff_mpeg_vdpau_decoder = {
2589  .name = "mpegvideo_vdpau",
2590  .type = AVMEDIA_TYPE_VIDEO,
2591  .id = AV_CODEC_ID_MPEG2VIDEO,
2592  .priv_data_size = sizeof(Mpeg1Context),
2596  .capabilities = CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED |
2598  .flush = flush,
2599  .long_name = NULL_IF_CONFIG_SMALL("MPEG-1/2 video (VDPAU acceleration)"),
2600 };
2601 #endif
2602 
2603 #if CONFIG_MPEG1_VDPAU_DECODER
2604 AVCodec ff_mpeg1_vdpau_decoder = {
2605  .name = "mpeg1video_vdpau",
2606  .type = AVMEDIA_TYPE_VIDEO,
2607  .id = AV_CODEC_ID_MPEG1VIDEO,
2608  .priv_data_size = sizeof(Mpeg1Context),
2612  .capabilities = CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED |
2614  .flush = flush,
2615  .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video (VDPAU acceleration)"),
2616 };
2617 #endif
const uint16_t ff_mpeg1_default_non_intra_matrix[64]
Definition: mpeg12data.c:41
#define PICT_BOTTOM_FIELD
Definition: mpegvideo.h:628
static int get_qscale(MpegEncContext *s)
Definition: mpeg12.c:722
enum AVPixelFormat ff_hwaccel_pixfmt_list_420[]
Definition: mpegvideo.c:133
static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
Definition: mpeg12.c:1491
const struct AVCodec * codec
Definition: avcodec.h:1348
int table_size
Definition: get_bits.h:66
#define PICT_TOP_FIELD
Definition: mpegvideo.h:627
discard all frames except keyframes
Definition: avcodec.h:535
void ff_init_block_index(MpegEncContext *s)
Definition: mpegvideo.c:2543
int(* start_frame)(AVCodecContext *avctx, const uint8_t *buf, uint32_t buf_size)
Called at the beginning of each frame or field picture.
Definition: avcodec.h:3106
int aspect_ratio_info
Definition: mpegvideo.h:551
int picture_number
Definition: mpegvideo.h:246
#define SLICE_MAX_START_CODE
Definition: cavs.h:30
#define HAVE_THREADS
Definition: config.h:236
const uint8_t ff_zigzag_direct[64]
Definition: dsputil.c:59
AVCodec ff_mpeg2video_decoder
Definition: mpeg12.c:2537
This structure describes decoded (raw) audio or video data.
Definition: avcodec.h:989
static int mpeg_decode_update_thread_context(AVCodecContext *avctx, const AVCodecContext *avctx_from)
Definition: mpeg12.c:1150
AVPanScan * pan_scan
Pan scan.
Definition: avcodec.h:1260
VLC ff_dc_lum_vlc
Definition: mpeg12.c:669
int start_mb_y
start mb_y of this thread (so current thread should process start_mb_y <= row < end_mb_y) ...
Definition: mpegvideo.h:287
#define MV_TYPE_FIELD
2 vectors, one per field
Definition: mpegvideo.h:393
const uint8_t * y_dc_scale_table
qscale -> y_dc_scale table
Definition: mpegvideo.h:325
int last_mv[2][2][2]
last MV, used for MV prediction in MPEG1 & B-frame MPEG4
Definition: mpegvideo.h:402
mpeg2/4, h264 default
Definition: avcodec.h:585
int coded_width
Bitstream width / height, may be different from width/height.
Definition: avcodec.h:1515
av_cold int ff_MPV_common_init(MpegEncContext *s)
init common structure for both encoder and decoder.
Definition: mpegvideo.c:941
int sync
Did we reach a sync point like a GOP/SEQ/KEYFrame?
Definition: mpeg12.h:43
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:70
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:237
av_cold void ff_mpeg12_init_vlcs(void)
Definition: mpeg12.c:677
MPEG-2 HW decoding with VDPAU, data[0] contains a vdpau_render_state struct which contains the bitstr...
Definition: pixfmt.h:105
void ff_MPV_report_decode_progress(MpegEncContext *s)
Definition: mpegvideo.c:2837
int end_mb_y
end mb_y of this thread (so current thread should process start_mb_y <= row < end_mb_y) ...
Definition: mpegvideo.h:288
uint16_t chroma_intra_matrix[64]
Definition: mpegvideo.h:442
int qscale_type
Definition: avcodec.h:1150
static const AVProfile mpeg2_video_profiles[]
Definition: mpeg12.c:2508
#define MB_BTYPE_VLC_BITS
Definition: mpeg12.c:50
int height
Definition: avcodec.h:802
int v_edge_pos
horizontal / vertical position of the right/bottom edge (pixel replication)
Definition: mpegvideo.h:252
uint16_t chroma_inter_matrix[64]
Definition: mpegvideo.h:444
void ff_er_frame_end(ERContext *s)
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:2079
static int slice_end(AVCodecContext *avctx, AVFrame *pict)
Handle slice ends.
Definition: mpeg12.c:1915
int num
numerator
Definition: rational.h:44
int repeat_pict
When decoding, this signals how much the picture must be delayed.
Definition: avcodec.h:1225
void ff_xvmc_field_end(MpegEncContext *s)
Complete frame/field rendering by passing any remaining blocks.
const unsigned char ff_mpeg12_vlc_dc_lum_bits[12]
Definition: mpeg12data.c:55
void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
Definition: utils.c:149
int size
Definition: avcodec.h:916
enum AVCodecID codec_id
Definition: mpegvideo.h:228
void ff_init_rl(RLTable *rl, uint8_t static_store[2][2 *MAX_RUN+MAX_LEVEL+3])
Definition: mpegvideo.c:1250
int save_aspect_info
Definition: mpeg12.h:40
const uint8_t * buffer
Definition: get_bits.h:53
void ff_mpeg1_clean_buffers(MpegEncContext *s)
Definition: mpeg12.c:657
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel...
Definition: avcodec.h:1724
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1533
static int av_cmp_q(AVRational a, AVRational b)
Compare two rationals.
Definition: rational.h:55
#define wrap(func)
Definition: w64xmmtest.h:70
mpegvideo header.
av_dlog(ac->avr,"%d samples - audio_convert: %s to %s (%s)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic?ac->func_descr_generic:ac->func_descr)
static const uint8_t non_linear_qscale[32]
Definition: mpeg12decdata.h:86
discard all
Definition: avcodec.h:536
uint8_t permutated[64]
Definition: dsputil.h:183
const uint16_t ff_mpeg12_vlc_dc_lum_code[12]
Definition: mpeg12data.c:52
const int8_t * table_level
Definition: rl.h:43
uint8_t run
Definition: svq3.c:132
#define ER_MV_ERROR
static int mpeg2_fast_decode_block_non_intra(MpegEncContext *s, DCTELEM *block, int n)
Definition: mpeg12.c:392
#define SLICE_MIN_START_CODE
Definition: mpegvideo.h:79
int profile
profile
Definition: avcodec.h:2815
AVCodec.
Definition: avcodec.h:2960
int frame_start_found
Definition: parser.h:34
int ff_xvmc_field_start(MpegEncContext *s, AVCodecContext *avctx)
Find and store the surfaces that are used as reference frames.
int qscale
QP.
Definition: mpegvideo.h:343
RLTable.
Definition: rl.h:38
static int get_sbits(GetBitContext *s, int n)
Definition: get_bits.h:223
int chroma_x_shift
Definition: mpegvideo.h:644
int encoding
true if we are encoding (vs decoding)
Definition: mpegvideo.h:230
int field_select[2][2]
Definition: mpegvideo.h:401
#define USES_LIST(a, list)
does this mb use listX, note does not work if subMBs
Definition: mpegvideo.h:127
#define HAS_CBP(a)
Definition: mpegvideo.h:128
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1465
enum AVDiscard skip_frame
Definition: avcodec.h:2907
static VLC mv_vlc
Definition: mpeg12.c:52
#define INIT_2D_VLC_RL(rl, static_size)
Definition: mpeg12.c:605
struct AVHWAccel * hwaccel
Hardware accelerator in use.
Definition: avcodec.h:2622
void ff_mpeg_draw_horiz_band(MpegEncContext *s, int y, int h)
Definition: mpegvideo.c:2534
const uint8_t ff_alternate_vertical_scan[64]
Definition: dsputil.c:97
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:228
#define MB_TYPE_INTRA
Definition: mpegvideo.h:105
uint8_t
DCTELEM(*[12] pblocks)[64]
Definition: mpegvideo.h:660
#define PICT_FRAME
Definition: mpegvideo.h:629
RLTable ff_rl_mpeg2
Definition: mpeg12data.c:174
#define INIT_VLC_STATIC(vlc, bits, a, b, c, d, e, f, g, static_size)
Definition: get_bits.h:436
enum OutputFormat out_format
output format
Definition: mpegvideo.h:220
#define MV_VLC_BITS
Definition: mpeg12.c:46
#define AV_RB32
Definition: intreadwrite.h:130
const float ff_mpeg1_aspect[16]
Definition: mpeg12data.c:328
void ff_mpeg_er_frame_start(MpegEncContext *s)
Definition: mpegvideo.c:2843
#define emms_c()
Definition: internal.h:145
static VLC mb_btype_vlc
Definition: mpeg12.c:674
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1454
int full_pel[2]
Definition: mpegvideo.h:648
int interlaced_dct
Definition: mpegvideo.h:649
Picture current_picture
copy of the current picture structure.
Definition: mpegvideo.h:314
#define IS_QUANT(a)
Definition: mpegvideo.h:125
int intra_dc_precision
Definition: mpegvideo.h:631
int repeat_first_field
Definition: mpegvideo.h:638
static int get_dmv(MpegEncContext *s)
Definition: mpeg12.c:714
void ff_xvmc_init_block(MpegEncContext *s)
Initialize the block field of the MpegEncContext pointer passed as parameter after making sure that t...
mpeg1, jpeg, h263
Definition: avcodec.h:586
const char data[16]
Definition: mxf.c:66
uint8_t * data
Definition: avcodec.h:915
AVRational av_mul_q(AVRational b, AVRational c)
Multiply two rationals.
Definition: rational.c:80
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:192
static int flags
Definition: log.c:42
void ff_xvmc_pack_pblocks(MpegEncContext *s, int cbp)
Fill individual block pointers, so there are no gaps in the data_block array in case not all blocks i...
#define ER_MV_END
MPEG-1 HW decoding with VDPAU, data[0] contains a vdpau_render_state struct which contains the bitstr...
Definition: pixfmt.h:104
uint8_t idct_permutation[64]
idct input permutation.
Definition: dsputil.h:425
int flags2
AVCodecContext.flags2.
Definition: mpegvideo.h:232
#define DECODE_SLICE_ERROR
Definition: mpeg12.c:1642
int mb_height
number of MBs horizontally & vertically
Definition: mpegvideo.h:248
static void mpeg_decode_gop(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
Definition: mpeg12.c:2115
void ff_MPV_frame_end(MpegEncContext *s)
Definition: mpegvideo.c:1640
int codec_tag
internal codec_tag upper case converted from avctx codec_tag
Definition: mpegvideo.h:238
static av_cold int mpeg_decode_init(AVCodecContext *avctx)
Definition: mpeg12.c:1119
enum AVChromaLocation chroma_sample_location
This defines the location of chroma samples.
Definition: avcodec.h:2086
#define MAX_LEVEL
Definition: rl.h:35
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:335
void ff_fetch_timestamp(AVCodecParserContext *s, int off, int remove)
Fetch timestamps for a specific byte within the current access unit.
Definition: parser.c:89
static void mpeg_decode_picture_coding_extension(Mpeg1Context *s1)
Definition: mpeg12.c:1501
const uint16_t ff_mpeg1_default_intra_matrix[64]
Definition: mpeg12data.c:30
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:547
void ff_thread_finish_setup(AVCodecContext *avctx)
If the codec defines update_thread_context(), call this when they are ready for the next thread to st...
Definition: pthread.c:702
enum AVCodecID id
Definition: avcodec.h:2974
int slice_context_count
number of used thread_contexts
Definition: mpegvideo.h:290
int extradata_decoded
Definition: mpeg12.h:45
#define UPDATE_CACHE(name, gb)
Definition: get_bits.h:160
static int decode_chunks(AVCodecContext *avctx, AVFrame *picture, int *got_output, const uint8_t *buf, int buf_size)
Definition: mpeg12.c:2210
int has_b_frames
Size of the frame reordering buffer in the decoder.
Definition: avcodec.h:1634
int last_dc[3]
last DC values for MPEG1
Definition: mpegvideo.h:322
#define s2
Definition: regdef.h:39
void ff_er_add_slice(ERContext *s, int startx, int starty, int endx, int endy, int status)
Add a slice.
Multithreading support functions.
static int slice_decode_thread(AVCodecContext *c, void *arg)
Definition: mpeg12.c:1871
int mb_skipped
MUST BE SET only during DECODING.
Definition: mpegvideo.h:332
int chroma_y_shift
Definition: mpegvideo.h:645
int ff_combine_frame(ParseContext *pc, int next, const uint8_t **buf, int *buf_size)
Combine the (truncated) bitstream to a complete frame.
Definition: parser.c:215
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:88
int ff_mpeg1_decode_block_intra(MpegEncContext *s, DCTELEM *block, int n)
Definition: mpeg12.c:161
ERContext er
Definition: mpegvideo.h:704
int active_thread_type
Which multithreading methods are in use by the codec.
Definition: avcodec.h:2752
AVRational av_div_q(AVRational b, AVRational c)
Divide one rational by another.
Definition: rational.c:88
int capabilities
Codec capabilities.
Definition: avcodec.h:2979
static int load_matrix(MpegEncContext *s, uint16_t matrix0[64], uint16_t matrix1[64], int intra)
Definition: mpeg12.c:1469
const uint16_t ff_mpeg12_vlc_dc_chroma_code[12]
Definition: mpeg12data.c:59
int flags
CODEC_FLAG_*.
Definition: avcodec.h:1434
#define SEQ_END_CODE
Definition: mpegvideo.h:75
XVideo Motion Acceleration via common packet passing.
Definition: pixfmt.h:80
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
const char * name
Name of the codec implementation.
Definition: avcodec.h:2967
int width
width and height in 1/16 pel
Definition: avcodec.h:801
#define IS_INTRA(a)
Definition: mpegvideo.h:109
int low_delay
no reordering needed / has no b-frames
Definition: mpegvideo.h:558
static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred)
Definition: mpeg12.c:55
GetBitContext gb
Definition: mpegvideo.h:614
static int mpeg2_decode_block_non_intra(MpegEncContext *s, DCTELEM *block, int n)
Definition: mpeg12.c:315
VLC vlc
decoding only deprecated FIXME remove
Definition: rl.h:47
#define MB_TYPE_ZERO_MV
Definition: mpeg12decdata.h:35
#define CLOSE_READER(name, gb)
Definition: get_bits.h:140
static int decode_dc(GetBitContext *gb, int component)
Definition: mpeg12.h:53
int repeat_field
Definition: mpeg12.h:36
int8_t len
Definition: get_bits.h:71
Definition: get_bits.h:63
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:69
void ff_mpeg_flush(AVCodecContext *avctx)
Definition: mpegvideo.c:2575
#define SKIP_BITS(name, gb, num)
Definition: get_bits.h:175
#define ONLY_IF_THREADS_ENABLED(x)
Define a function with only the non-default version specified.
Definition: internal.h:128
int resync_mb_x
x position of last resync marker
Definition: mpegvideo.h:493
int rc_buffer_size
decoder bitstream buffer size
Definition: avcodec.h:2317
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition: rational.c:36
#define ER_AC_ERROR
int dtg_active_format
DTG active format information (additional aspect ratio information only used in DVB MPEG-2 transport ...
Definition: avcodec.h:1817
the normal 219*2^(n-8) "MPEG" YUV ranges
Definition: avcodec.h:573
int intra_vlc_format
Definition: mpegvideo.h:636
int n
number of entries of table_vlc minus 1
Definition: rl.h:39
int bit_rate
the average bitrate
Definition: avcodec.h:1404
static int mpeg_decode_frame(AVCodecContext *avctx, void *data, int *got_output, AVPacket *avpkt)
Definition: mpeg12.c:2444
static AVFrame * picture
int progressive_frame
Definition: mpegvideo.h:647
static DCTELEM block[64]
Definition: dct-test.c:169
AVRational av_d2q(double d, int max)
Convert a double precision floating point number to a rational.
Definition: rational.c:106
enum AVPictureType pict_type
Picture type of the frame, see ?_TYPE below.
Definition: avcodec.h:1065
int top_field_first
Definition: mpegvideo.h:633
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
Definition: avcodec.h:2602
int last_index
Definition: parser.h:31
uint8_t * mbskip_table
used to avoid copy if macroblock skipped (for black regions for example) and used for b-frame encodin...
Definition: mpegvideo.h:333
int idct_algo
IDCT algorithm, see FF_IDCT_* below.
Definition: avcodec.h:2661
const unsigned char ff_mpeg12_vlc_dc_chroma_bits[12]
Definition: mpeg12data.c:62
Picture * current_picture_ptr
pointer to the current picture
Definition: mpegvideo.h:318
#define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update)
Definition: get_bits.h:481
#define ER_DC_END
RLTable ff_rl_mpeg1
Definition: mpeg12data.c:166
int alternate_scan
Definition: mpegvideo.h:637
static int mpeg_decode_end(AVCodecContext *avctx)
Definition: mpeg12.c:2499
int save_height
Definition: mpeg12.h:41
#define GOP_START_CODE
Definition: mpegvideo.h:77
int32_t
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition: avcodec.h:2058
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
Definition: get_bits.h:251
const int8_t * table_run
Definition: rl.h:42
#define check_scantable_index(ctx, x)
Definition: mpeg12.c:83
static VLC mbincr_vlc
Definition: mpeg12.c:672
static void flush(AVCodecContext *avctx)
Definition: mpeg12.c:2489
int level
level
Definition: avcodec.h:2885
#define LAST_SKIP_BITS(name, gb, num)
Definition: get_bits.h:180
static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE(*table)[2], int bits, int max_depth)
Parse a vlc code.
Definition: get_bits.h:515
#define AV_RL32
Definition: intreadwrite.h:146
#define CONFIG_MPEG_VDPAU_DECODER
Definition: config.h:447
int block_last_index[12]
last non zero coefficient in block
Definition: mpegvideo.h:262
int ticks_per_frame
For some codecs, the time base is closer to the field rate than the frame rate.
Definition: avcodec.h:1474
int mpeg_f_code[2][2]
Definition: mpegvideo.h:624
#define EXT_START_CODE
Definition: cavs.h:31
int mpeg_enc_ctx_allocated
Definition: mpeg12.h:35
int ff_MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
generic function for encode/decode called after coding/decoding the header and before a frame is code...
Definition: mpegvideo.c:1429
int ff_mpeg_update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
Definition: mpegvideo.c:597
preferred ID for MPEG-1/2 video decoding
Definition: avcodec.h:100
RL_VLC_ELEM * rl_vlc[32]
decoding only
Definition: rl.h:48
#define SHOW_UBITS(name, gb, num)
Definition: get_bits.h:186
#define MBINCR_VLC_BITS
Definition: mpeg12.c:47
int xvmc_acceleration
XVideo Motion Acceleration.
Definition: avcodec.h:1875
static const float pred[4]
Definition: siprdata.h:259
int first_field
is 1 for the first field of a field picture 0 otherwise
Definition: mpegvideo.h:651
int save_width
Definition: mpeg12.h:41
static int mpeg1_decode_sequence(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
Definition: mpeg12.c:1961
uint32_t * mb_type
macroblock type table mb_type_base + mb_width + 2
Definition: avcodec.h:1180
#define MV_TYPE_16X16
1 vector for the whole mb
Definition: mpegvideo.h:390
int frame_pred_frame_dct
Definition: mpegvideo.h:632
NULL
Definition: eval.c:52
const uint8_t *const ff_mpeg2_dc_scale_table[4]
Definition: mpegvideo.c:121
static int width
Definition: utils.c:156
const uint8_t * avpriv_mpv_find_start_code(const uint8_t *restrict p, const uint8_t *end, uint32_t *restrict state)
Definition: mpegvideo.c:174
uint16_t inter_matrix[64]
Definition: mpegvideo.h:443
uint8_t * buffer
Definition: parser.h:29
int concealment_motion_vectors
Definition: mpegvideo.h:634
struct MpegEncContext * thread_context[MAX_THREADS]
Definition: mpegvideo.h:289
VLC ff_dc_chroma_vlc
Definition: mpeg12.c:670
external API header
AVRational frame_rate_ext
MPEG-2 specific framerate modificator.
Definition: mpeg12.h:42
enum AVCodecID codec_id
Definition: avcodec.h:1350
int linesize[AV_NUM_DATA_POINTERS]
Size, in bytes, of the data for each picture/channel plane.
Definition: avcodec.h:1008
#define DECODE_SLICE_OK
Definition: mpeg12.c:1643
int debug
debug
Definition: avcodec.h:2568
main external API structure.
Definition: avcodec.h:1339
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:326
ScanTable intra_scantable
Definition: mpegvideo.h:267
#define USER_START_CODE
Definition: cavs.h:32
int height
picture size. must be a multiple of 16
Definition: mpegvideo.h:216
MPEG1/2 tables.
#define MT_DMV
Definition: mpeg12.c:745
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1365
#define OPEN_READER(name, gb)
Definition: get_bits.h:124
int16_t(*[2] motion_val)[2]
motion vector table
Definition: avcodec.h:1172
#define MV_TYPE_16X8
2 vectors, one per 16x8 block
Definition: mpegvideo.h:392
int chroma_420_type
Definition: mpegvideo.h:639
static enum AVPixelFormat mpeg_get_pixelformat(AVCodecContext *avctx)
Definition: mpeg12.c:1189
uint32_t state
contains the last few bytes in MSB order
Definition: parser.h:33
int extradata_size
Definition: avcodec.h:1455
static void mpeg_decode_sequence_display_extension(Mpeg1Context *s1)
Definition: mpeg12.c:1412
int progressive_sequence
Definition: mpegvideo.h:623
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:268
static void skip_bits1(GetBitContext *s)
Definition: get_bits.h:293
int slice_flags
slice flags
Definition: avcodec.h:1865
int coded_height
Definition: avcodec.h:1515
static int mpeg1_decode_block_intra(MpegEncContext *s, DCTELEM *block, int n)
Definition: mpeg12.c:92
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:260
enum AVPixelFormat(* get_format)(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
callback to negotiate the pixelFormat
Definition: avcodec.h:1580
#define TEX_VLC_BITS
Definition: dvdata.h:100
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:2072
rational number numerator/denominator
Definition: rational.h:43
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition: avcodec.h:2065
static int vcr2_init_sequence(AVCodecContext *avctx)
Definition: mpeg12.c:2039
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:372
#define GET_CACHE(name, gb)
Definition: get_bits.h:190
MPEG1/2 decoder tables.
int ff_mpeg1_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size, AVCodecParserContext *s)
Find the end of the current frame in the bitstream.
Definition: mpeg12.c:2150
AVHWAccel * ff_find_hwaccel(enum AVCodecID codec_id, enum AVPixelFormat pix_fmt)
Return the hardware accelerated codec for codec codec_id and pixel format pix_fmt.
Definition: utils.c:2048
int save_progressive_seq
Definition: mpeg12.h:41
discard useless packets like 0 size packets in avi
Definition: avcodec.h:532
int8_t * ref_index[2]
motion reference frame index the order in which these are stored can depend on the codec...
Definition: avcodec.h:1195
DSPContext dsp
pointers for accelerated dsp functions
Definition: mpegvideo.h:362
static void init_2d_vlc_rl(RLTable *rl)
Definition: mpeg12.c:616
#define s1
Definition: regdef.h:38
#define END_NOT_FOUND
Definition: parser.h:40
#define ER_DC_ERROR
int closed_gop
GOP is closed.
Definition: mpeg12.h:44
static int mpeg1_decode_picture(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
Definition: mpeg12.c:1325
short DCTELEM
Definition: dsputil.h:39
#define MV_DIR_FORWARD
Definition: mpegvideo.h:386
int pict_type
AV_PICTURE_TYPE_I, AV_PICTURE_TYPE_P, AV_PICTURE_TYPE_B, ...
Definition: mpegvideo.h:350
int bit_rate
wanted bit rate
Definition: mpegvideo.h:219
DCTELEM(* block)[64]
points to one of the following blocks
Definition: mpegvideo.h:662
struct Mpeg1Context Mpeg1Context
static av_const int sign_extend(int val, unsigned bits)
Definition: mathops.h:123
int skip_bottom
Number of macroblock rows at the bottom which are skipped.
Definition: avcodec.h:1965
static uint32_t state
Definition: trasher.c:27
static const uint32_t btype2mb_type[11]
Definition: mpeg12decdata.h:72
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: avcodec.h:997
const uint8_t * c_dc_scale_table
qscale -> c_dc_scale table
Definition: mpegvideo.h:326
uint8_t level
Definition: svq3.c:133
#define DC_VLC_BITS
Definition: intrax8.c:34
int mv[2][4][2]
motion vectors for a macroblock first coordinate : 0 = forward 1 = backward second " : depend...
Definition: mpegvideo.h:400
int b8_stride
2*mb_width+1 used for some 8x8 block arrays to allow simple addressing
Definition: mpegvideo.h:250
void(* clear_blocks)(DCTELEM *blocks)
Definition: dsputil.h:219
int height
Definition: gxfenc.c:72
MpegEncContext.
Definition: mpegvideo.h:212
Picture * next_picture_ptr
pointer to the next picture (for bidir pred)
Definition: mpegvideo.h:317
uint8_t run
Definition: get_bits.h:72
#define MAX_RUN
Definition: rl.h:34
struct AVCodecContext * avctx
Definition: mpegvideo.h:214
#define MB_PAT_VLC_BITS
Definition: mpeg12.c:48
#define SHOW_SBITS(name, gb, num)
Definition: get_bits.h:187
discard all non reference
Definition: avcodec.h:533
const AVRational ff_mpeg2_aspect[16]
Definition: mpeg12data.c:349
AVCodec ff_mpeg1video_decoder
Definition: mpeg12.c:2521
static VLC mb_ptype_vlc
Definition: mpeg12.c:673
common internal api header.
int mb_stride
mb_width+1 used for some arrays to allow simple addressing of left & top MBs without sig11 ...
Definition: mpegvideo.h:249
uint8_t * dest[3]
Definition: mpegvideo.h:436
static void mpeg_decode_sequence_extension(Mpeg1Context *s1)
Definition: mpeg12.c:1367
static int mpeg_decode_slice(MpegEncContext *s, int mb_y, const uint8_t **buf, int buf_size)
Decode a slice.
Definition: mpeg12.c:1651
uint8_t ff_mpeg12_static_rl_table_store[2][2][2 *MAX_RUN+MAX_LEVEL+3]
Definition: mpeg12.c:603
const uint8_t * buffer_end
Definition: get_bits.h:53
void ff_vdpau_mpeg_picture_complete(MpegEncContext *s, const uint8_t *buf, int buf_size, int slice_count)
Definition: vdpau.c:187
Picture * last_picture_ptr
pointer to the previous picture.
Definition: mpegvideo.h:316
Bi-dir predicted.
Definition: avutil.h:247
AVProfile.
Definition: avcodec.h:2948
static int mpeg1_fast_decode_block_inter(MpegEncContext *s, DCTELEM *block, int n)
Definition: mpeg12.c:241
static int mpeg_field_start(MpegEncContext *s, const uint8_t *buf, int buf_size)
Definition: mpeg12.c:1577
static int mpeg_decode_postinit(AVCodecContext *avctx)
Definition: mpeg12.c:1213
const uint8_t ff_mpeg12_mbPatTable[64][2]
Definition: mpeg12data.c:221
int den
denominator
Definition: rational.h:45
int ff_update_duplicate_context(MpegEncContext *dst, MpegEncContext *src)
Definition: mpegvideo.c:574
DSP utils.
int16_t position[3][2]
position of the top left corner in 1/16 pel for up to 3 fields/frames
Definition: avcodec.h:809
void * priv_data
Definition: avcodec.h:1382
static int mpeg2_decode_block_intra(MpegEncContext *s, DCTELEM *block, int n)
Definition: mpeg12.c:456
int frame_rate_index
Definition: mpegvideo.h:354
void ff_mpeg12_common_init(MpegEncContext *s)
Definition: mpeg12.c:649
int picture_structure
Definition: mpegvideo.h:625
float re
Definition: fft-test.c:64
int(* execute)(struct AVCodecContext *c, int(*func)(struct AVCodecContext *c2, void *arg), void *arg2, int *ret, int count, int size)
The codec may call this to execute several independent things.
Definition: avcodec.h:2773
void ff_MPV_decode_mb(MpegEncContext *s, DCTELEM block[12][64])
Definition: mpegvideo.c:2446
void ff_MPV_common_end(MpegEncContext *s)
Definition: mpegvideo.c:1200
int len
void ff_print_debug_info(MpegEncContext *s, AVFrame *pict)
Print debugging info for the given picture.
Definition: mpegvideo.c:1802
#define MV_TYPE_DMV
2 vectors, special mpeg2 Dual Prime Vectors
Definition: mpegvideo.h:394
#define SEQ_START_CODE
Definition: mpegvideo.h:76
void ff_init_scantable(uint8_t *permutation, ScanTable *st, const uint8_t *src_scantable)
Definition: dsputil.c:122
#define MB_PTYPE_VLC_BITS
Definition: mpeg12.c:49
static VLC mb_pat_vlc
Definition: mpeg12.c:675
int resync_mb_y
y position of last resync marker
Definition: mpegvideo.h:494
static int mpeg2_fast_decode_block_intra(MpegEncContext *s, DCTELEM *block, int n)
Definition: mpeg12.c:532
ParseContext parse_context
Definition: mpegvideo.h:500
const uint8_t ff_mpeg12_mbAddrIncrTable[36][2]
Definition: mpeg12data.c:182
VLC_TYPE(* table)[2]
code, bits
Definition: get_bits.h:65
static const uint8_t table_mb_btype[11][2]
Definition: mpeg12decdata.h:58
int key_frame
1 -> keyframe, 0-> not
Definition: avcodec.h:1058
int16_t level
Definition: get_bits.h:70
int flags2
CODEC_FLAG2_*.
Definition: avcodec.h:1441
MpegEncContext mpeg_enc_ctx
Definition: mpeg12.h:34
int slice_count
Definition: mpeg12.h:38
struct AVFrame f
Definition: mpegvideo.h:96
int flags
AVCodecContext.flags (HQ, MV4, ...)
Definition: mpegvideo.h:231
uint16_t intra_matrix[64]
matrix transmitted in the bitstream
Definition: mpegvideo.h:441
static void exchange_uv(MpegEncContext *s)
Definition: mpeg12.c:732
static const uint32_t ptype2mb_type[7]
Definition: mpeg12decdata.h:48
ScanTable inter_scantable
if inter == intra then intra should be used to reduce tha cache usage
Definition: mpegvideo.h:266
const uint8_t ff_mpeg12_mbMotionVectorTable[17][2]
Definition: mpeg12data.c:288
#define PICTURE_START_CODE
Definition: mpegvideo.h:78
static const uint8_t table_mb_ptype[7][2]
Definition: mpeg12decdata.h:38
#define ER_AC_END
static void mpeg_decode_picture_display_extension(Mpeg1Context *s1)
Definition: mpeg12.c:1436
int(* decode_slice)(AVCodecContext *avctx, const uint8_t *buf, uint32_t buf_size)
Callback for each slice.
Definition: avcodec.h:3119
#define CONFIG_MPEG_XVMC_DECODER
Definition: config.h:442
static void mpeg_decode_user_data(AVCodecContext *avctx, const uint8_t *p, int buf_size)
Definition: mpeg12.c:2093
int(* end_frame)(AVCodecContext *avctx)
Called at the end of each frame or field picture.
Definition: avcodec.h:3130
static int mpeg_decode_mb(MpegEncContext *s, DCTELEM block[12][64])
Definition: mpeg12.c:747
static int mpeg1_decode_block_inter(MpegEncContext *s, DCTELEM *block, int n)
Definition: mpeg12.c:166
AVPixelFormat
Pixel format.
Definition: pixfmt.h:63
This structure stores compressed data.
Definition: avcodec.h:898
const AVRational ff_mpeg12_frame_rate_tab[16]
Definition: mpeg12data.c:308
#define MT_FRAME
Definition: mpeg12.c:743
Predicted.
Definition: avutil.h:246
static void quant_matrix_rebuild(uint16_t *matrix, const uint8_t *old_perm, const uint8_t *new_perm)
Definition: mpeg12.c:1171
void ff_MPV_decode_defaults(MpegEncContext *s)
Set the given MpegEncContext to defaults for decoding.
Definition: mpegvideo.c:745
#define MT_FIELD
Definition: mpeg12.c:742
AVPanScan pan_scan
some temporary storage for the panscan
Definition: mpeg12.h:37
if(!(ptr_align%ac->ptr_align)&&samples_align >=aligned_len)
static enum AVPixelFormat pixfmt_xvmc_mpg2_420[]
Definition: mpeg12.c:1184