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  bit_rate_ext = get_bits(&s->gb, 12); /* XXX: handle it */
1383  s->bit_rate += (bit_rate_ext << 18) * 400;
1384  skip_bits1(&s->gb); /* marker */
1385  s->avctx->rc_buffer_size += get_bits(&s->gb, 8) * 1024 * 16 << 10;
1386 
1387  s->low_delay = get_bits1(&s->gb);
1388  if (s->flags & CODEC_FLAG_LOW_DELAY)
1389  s->low_delay = 1;
1390 
1391  s1->frame_rate_ext.num = get_bits(&s->gb, 2) + 1;
1392  s1->frame_rate_ext.den = get_bits(&s->gb, 5) + 1;
1393 
1394  av_dlog(s->avctx, "sequence extension\n");
1396 
1397  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1398  av_log(s->avctx, AV_LOG_DEBUG, "profile: %d, level: %d vbv buffer: %d, bitrate:%d\n",
1399  s->avctx->profile, s->avctx->level, s->avctx->rc_buffer_size, s->bit_rate);
1400 
1401 }
1402 
1404 {
1405  MpegEncContext *s = &s1->mpeg_enc_ctx;
1406  int color_description, w, h;
1407 
1408  skip_bits(&s->gb, 3); /* video format */
1409  color_description = get_bits1(&s->gb);
1410  if (color_description) {
1411  s->avctx->color_primaries = get_bits(&s->gb, 8);
1412  s->avctx->color_trc = get_bits(&s->gb, 8);
1413  s->avctx->colorspace = get_bits(&s->gb, 8);
1414  }
1415  w = get_bits(&s->gb, 14);
1416  skip_bits(&s->gb, 1); //marker
1417  h = get_bits(&s->gb, 14);
1418  // remaining 3 bits are zero padding
1419 
1420  s1->pan_scan.width = 16 * w;
1421  s1->pan_scan.height = 16 * h;
1422 
1423  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1424  av_log(s->avctx, AV_LOG_DEBUG, "sde w:%d, h:%d\n", w, h);
1425 }
1426 
1428 {
1429  MpegEncContext *s = &s1->mpeg_enc_ctx;
1430  int i, nofco;
1431 
1432  nofco = 1;
1433  if (s->progressive_sequence) {
1434  if (s->repeat_first_field) {
1435  nofco++;
1436  if (s->top_field_first)
1437  nofco++;
1438  }
1439  } else {
1440  if (s->picture_structure == PICT_FRAME) {
1441  nofco++;
1442  if (s->repeat_first_field)
1443  nofco++;
1444  }
1445  }
1446  for (i = 0; i < nofco; i++) {
1447  s1->pan_scan.position[i][0] = get_sbits(&s->gb, 16);
1448  skip_bits(&s->gb, 1); // marker
1449  s1->pan_scan.position[i][1] = get_sbits(&s->gb, 16);
1450  skip_bits(&s->gb, 1); // marker
1451  }
1452 
1453  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1454  av_log(s->avctx, AV_LOG_DEBUG, "pde (%d,%d) (%d,%d) (%d,%d)\n",
1455  s1->pan_scan.position[0][0], s1->pan_scan.position[0][1],
1456  s1->pan_scan.position[1][0], s1->pan_scan.position[1][1],
1457  s1->pan_scan.position[2][0], s1->pan_scan.position[2][1]);
1458 }
1459 
1460 static int load_matrix(MpegEncContext *s, uint16_t matrix0[64], uint16_t matrix1[64], int intra)
1461 {
1462  int i;
1463 
1464  for (i = 0; i < 64; i++) {
1465  int j = s->dsp.idct_permutation[ff_zigzag_direct[i]];
1466  int v = get_bits(&s->gb, 8);
1467  if (v == 0) {
1468  av_log(s->avctx, AV_LOG_ERROR, "matrix damaged\n");
1469  return -1;
1470  }
1471  if (intra && i == 0 && v != 8) {
1472  av_log(s->avctx, AV_LOG_ERROR, "intra matrix invalid, ignoring\n");
1473  v = 8; // needed by pink.mpg / issue1046
1474  }
1475  matrix0[j] = v;
1476  if (matrix1)
1477  matrix1[j] = v;
1478  }
1479  return 0;
1480 }
1481 
1483 {
1484  av_dlog(s->avctx, "matrix extension\n");
1485 
1486  if (get_bits1(&s->gb)) load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
1487  if (get_bits1(&s->gb)) load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
1488  if (get_bits1(&s->gb)) load_matrix(s, s->chroma_intra_matrix, NULL , 1);
1489  if (get_bits1(&s->gb)) load_matrix(s, s->chroma_inter_matrix, NULL , 0);
1490 }
1491 
1493 {
1494  MpegEncContext *s = &s1->mpeg_enc_ctx;
1495 
1496  s->full_pel[0] = s->full_pel[1] = 0;
1497  s->mpeg_f_code[0][0] = get_bits(&s->gb, 4);
1498  s->mpeg_f_code[0][1] = get_bits(&s->gb, 4);
1499  s->mpeg_f_code[1][0] = get_bits(&s->gb, 4);
1500  s->mpeg_f_code[1][1] = get_bits(&s->gb, 4);
1501  if (!s->pict_type && s1->mpeg_enc_ctx_allocated) {
1502  av_log(s->avctx, AV_LOG_ERROR, "Missing picture start code, guessing missing values\n");
1503  if (s->mpeg_f_code[1][0] == 15 && s->mpeg_f_code[1][1] == 15) {
1504  if (s->mpeg_f_code[0][0] == 15 && s->mpeg_f_code[0][1] == 15)
1506  else
1508  } else
1512  }
1513  s->intra_dc_precision = get_bits(&s->gb, 2);
1514  s->picture_structure = get_bits(&s->gb, 2);
1515  s->top_field_first = get_bits1(&s->gb);
1516  s->frame_pred_frame_dct = get_bits1(&s->gb);
1518  s->q_scale_type = get_bits1(&s->gb);
1519  s->intra_vlc_format = get_bits1(&s->gb);
1520  s->alternate_scan = get_bits1(&s->gb);
1521  s->repeat_first_field = get_bits1(&s->gb);
1522  s->chroma_420_type = get_bits1(&s->gb);
1523  s->progressive_frame = get_bits1(&s->gb);
1524 
1525  if (s->progressive_sequence && !s->progressive_frame) {
1526  s->progressive_frame = 1;
1527  av_log(s->avctx, AV_LOG_ERROR, "interlaced frame in progressive sequence, ignoring\n");
1528  }
1529 
1530  if (s->picture_structure == 0 || (s->progressive_frame && s->picture_structure != PICT_FRAME)) {
1531  av_log(s->avctx, AV_LOG_ERROR, "picture_structure %d invalid, ignoring\n", s->picture_structure);
1533  }
1534 
1536  av_log(s->avctx, AV_LOG_WARNING, "invalid frame_pred_frame_dct\n");
1537  }
1538 
1539  if (s->picture_structure == PICT_FRAME) {
1540  s->first_field = 0;
1541  s->v_edge_pos = 16 * s->mb_height;
1542  } else {
1543  s->first_field ^= 1;
1544  s->v_edge_pos = 8 * s->mb_height;
1545  memset(s->mbskip_table, 0, s->mb_stride * s->mb_height);
1546  }
1547 
1548  if (s->alternate_scan) {
1551  } else {
1554  }
1555 
1556  /* composite display not parsed */
1557  av_dlog(s->avctx, "intra_dc_precision=%d\n", s->intra_dc_precision);
1558  av_dlog(s->avctx, "picture_structure=%d\n", s->picture_structure);
1559  av_dlog(s->avctx, "top field first=%d\n", s->top_field_first);
1560  av_dlog(s->avctx, "repeat first field=%d\n", s->repeat_first_field);
1561  av_dlog(s->avctx, "conceal=%d\n", s->concealment_motion_vectors);
1562  av_dlog(s->avctx, "intra_vlc_format=%d\n", s->intra_vlc_format);
1563  av_dlog(s->avctx, "alternate_scan=%d\n", s->alternate_scan);
1564  av_dlog(s->avctx, "frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
1565  av_dlog(s->avctx, "progressive_frame=%d\n", s->progressive_frame);
1566 }
1567 
1568 static int mpeg_field_start(MpegEncContext *s, const uint8_t *buf, int buf_size)
1569 {
1570  AVCodecContext *avctx = s->avctx;
1571  Mpeg1Context *s1 = (Mpeg1Context*)s;
1572 
1573  /* start frame decoding */
1574  if (s->first_field || s->picture_structure == PICT_FRAME) {
1575  if (ff_MPV_frame_start(s, avctx) < 0)
1576  return -1;
1577 
1578  ff_er_frame_start(s);
1579 
1580  /* first check if we must repeat the frame */
1582  if (s->repeat_first_field) {
1583  if (s->progressive_sequence) {
1584  if (s->top_field_first)
1586  else
1588  } else if (s->progressive_frame) {
1590  }
1591  }
1592 
1594 
1596  ff_thread_finish_setup(avctx);
1597  } else { // second field
1598  int i;
1599 
1600  if (!s->current_picture_ptr) {
1601  av_log(s->avctx, AV_LOG_ERROR, "first field missing\n");
1602  return -1;
1603  }
1604 
1605  if (s->avctx->hwaccel &&
1607  if (s->avctx->hwaccel->end_frame(s->avctx) < 0)
1608  av_log(avctx, AV_LOG_ERROR, "hardware accelerator failed to decode first field\n");
1609  }
1610 
1611  for (i = 0; i < 4; i++) {
1615  }
1616  }
1617  }
1618 
1619  if (avctx->hwaccel) {
1620  if (avctx->hwaccel->start_frame(avctx, buf, buf_size) < 0)
1621  return -1;
1622  }
1623 
1624 // MPV_frame_start will call this function too,
1625 // but we need to call it on every field
1627  if (ff_xvmc_field_start(s, avctx) < 0)
1628  return -1;
1629 
1630  return 0;
1631 }
1632 
1633 #define DECODE_SLICE_ERROR -1
1634 #define DECODE_SLICE_OK 0
1635 
1642 static int mpeg_decode_slice(MpegEncContext *s, int mb_y,
1643  const uint8_t **buf, int buf_size)
1644 {
1645  AVCodecContext *avctx = s->avctx;
1646  const int field_pic = s->picture_structure != PICT_FRAME;
1647 
1648  s->resync_mb_x =
1649  s->resync_mb_y = -1;
1650 
1651  assert(mb_y < s->mb_height);
1652 
1653  init_get_bits(&s->gb, *buf, buf_size * 8);
1654 
1656  s->interlaced_dct = 0;
1657 
1658  s->qscale = get_qscale(s);
1659 
1660  if (s->qscale == 0) {
1661  av_log(s->avctx, AV_LOG_ERROR, "qscale == 0\n");
1662  return -1;
1663  }
1664 
1665  /* extra slice info */
1666  while (get_bits1(&s->gb) != 0) {
1667  skip_bits(&s->gb, 8);
1668  }
1669 
1670  s->mb_x = 0;
1671 
1672  if (mb_y == 0 && s->codec_tag == AV_RL32("SLIF")) {
1673  skip_bits1(&s->gb);
1674  } else {
1675  while (get_bits_left(&s->gb) > 0) {
1676  int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
1677  if (code < 0) {
1678  av_log(s->avctx, AV_LOG_ERROR, "first mb_incr damaged\n");
1679  return -1;
1680  }
1681  if (code >= 33) {
1682  if (code == 33) {
1683  s->mb_x += 33;
1684  }
1685  /* otherwise, stuffing, nothing to do */
1686  } else {
1687  s->mb_x += code;
1688  break;
1689  }
1690  }
1691  }
1692 
1693  if (s->mb_x >= (unsigned)s->mb_width) {
1694  av_log(s->avctx, AV_LOG_ERROR, "initial skip overflow\n");
1695  return -1;
1696  }
1697 
1698  if (avctx->hwaccel) {
1699  const uint8_t *buf_end, *buf_start = *buf - 4; /* include start_code */
1700  int start_code = -1;
1701  buf_end = avpriv_mpv_find_start_code(buf_start + 2, *buf + buf_size, &start_code);
1702  if (buf_end < *buf + buf_size)
1703  buf_end -= 4;
1704  s->mb_y = mb_y;
1705  if (avctx->hwaccel->decode_slice(avctx, buf_start, buf_end - buf_start) < 0)
1706  return DECODE_SLICE_ERROR;
1707  *buf = buf_end;
1708  return DECODE_SLICE_OK;
1709  }
1710 
1711  s->resync_mb_x = s->mb_x;
1712  s->resync_mb_y = s->mb_y = mb_y;
1713  s->mb_skip_run = 0;
1715 
1716  if (s->mb_y == 0 && s->mb_x == 0 && (s->first_field || s->picture_structure == PICT_FRAME)) {
1717  if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
1718  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",
1719  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],
1720  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")),
1721  s->progressive_sequence ? "ps" :"", s->progressive_frame ? "pf" : "", s->alternate_scan ? "alt" :"", s->top_field_first ? "top" :"",
1724  }
1725  }
1726 
1727  for (;;) {
1728  // If 1, we memcpy blocks in xvmcvideo.
1730  ff_xvmc_init_block(s); // set s->block
1731 
1732  if (mpeg_decode_mb(s, s->block) < 0)
1733  return -1;
1734 
1735  if (s->current_picture.f.motion_val[0] && !s->encoding) { // note motion_val is normally NULL unless we want to extract the MVs
1736  const int wrap = s->b8_stride;
1737  int xy = s->mb_x * 2 + s->mb_y * 2 * wrap;
1738  int b8_xy = 4 * (s->mb_x + s->mb_y * s->mb_stride);
1739  int motion_x, motion_y, dir, i;
1740 
1741  for (i = 0; i < 2; i++) {
1742  for (dir = 0; dir < 2; dir++) {
1743  if (s->mb_intra || (dir == 1 && s->pict_type != AV_PICTURE_TYPE_B)) {
1744  motion_x = motion_y = 0;
1745  } else if (s->mv_type == MV_TYPE_16X16 || (s->mv_type == MV_TYPE_FIELD && field_pic)) {
1746  motion_x = s->mv[dir][0][0];
1747  motion_y = s->mv[dir][0][1];
1748  } else /*if ((s->mv_type == MV_TYPE_FIELD) || (s->mv_type == MV_TYPE_16X8))*/ {
1749  motion_x = s->mv[dir][i][0];
1750  motion_y = s->mv[dir][i][1];
1751  }
1752 
1753  s->current_picture.f.motion_val[dir][xy ][0] = motion_x;
1754  s->current_picture.f.motion_val[dir][xy ][1] = motion_y;
1755  s->current_picture.f.motion_val[dir][xy + 1][0] = motion_x;
1756  s->current_picture.f.motion_val[dir][xy + 1][1] = motion_y;
1757  s->current_picture.f.ref_index [dir][b8_xy ] =
1758  s->current_picture.f.ref_index [dir][b8_xy + 1] = s->field_select[dir][i];
1759  assert(s->field_select[dir][i] == 0 || s->field_select[dir][i] == 1);
1760  }
1761  xy += wrap;
1762  b8_xy +=2;
1763  }
1764  }
1765 
1766  s->dest[0] += 16;
1767  s->dest[1] += 16 >> s->chroma_x_shift;
1768  s->dest[2] += 16 >> s->chroma_x_shift;
1769 
1770  ff_MPV_decode_mb(s, s->block);
1771 
1772  if (++s->mb_x >= s->mb_width) {
1773  const int mb_size = 16;
1774 
1775  ff_draw_horiz_band(s, mb_size*(s->mb_y >> field_pic), mb_size);
1777 
1778  s->mb_x = 0;
1779  s->mb_y += 1 << field_pic;
1780 
1781  if (s->mb_y >= s->mb_height) {
1782  int left = get_bits_left(&s->gb);
1783  int is_d10 = s->chroma_format == 2 && s->pict_type == AV_PICTURE_TYPE_I && avctx->profile == 0 && avctx->level == 5
1784  && s->intra_dc_precision == 2 && s->q_scale_type == 1 && s->alternate_scan == 0
1785  && s->progressive_frame == 0 /* vbv_delay == 0xBBB || 0xE10*/;
1786 
1787  if (left < 0 || (left && show_bits(&s->gb, FFMIN(left, 23)) && !is_d10)
1788  || ((avctx->err_recognition & AV_EF_BUFFER) && left > 8)) {
1789  av_log(avctx, AV_LOG_ERROR, "end mismatch left=%d %0X\n", left, show_bits(&s->gb, FFMIN(left, 23)));
1790  return -1;
1791  } else
1792  goto eos;
1793  }
1794 
1796  }
1797 
1798  /* skip mb handling */
1799  if (s->mb_skip_run == -1) {
1800  /* read increment again */
1801  s->mb_skip_run = 0;
1802  for (;;) {
1803  int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
1804  if (code < 0) {
1805  av_log(s->avctx, AV_LOG_ERROR, "mb incr damaged\n");
1806  return -1;
1807  }
1808  if (code >= 33) {
1809  if (code == 33) {
1810  s->mb_skip_run += 33;
1811  } else if (code == 35) {
1812  if (s->mb_skip_run != 0 || show_bits(&s->gb, 15) != 0) {
1813  av_log(s->avctx, AV_LOG_ERROR, "slice mismatch\n");
1814  return -1;
1815  }
1816  goto eos; /* end of slice */
1817  }
1818  /* otherwise, stuffing, nothing to do */
1819  } else {
1820  s->mb_skip_run += code;
1821  break;
1822  }
1823  }
1824  if (s->mb_skip_run) {
1825  int i;
1826  if (s->pict_type == AV_PICTURE_TYPE_I) {
1827  av_log(s->avctx, AV_LOG_ERROR, "skipped MB in I frame at %d %d\n", s->mb_x, s->mb_y);
1828  return -1;
1829  }
1830 
1831  /* skip mb */
1832  s->mb_intra = 0;
1833  for (i = 0; i < 12; i++)
1834  s->block_last_index[i] = -1;
1836  s->mv_type = MV_TYPE_16X16;
1837  else
1838  s->mv_type = MV_TYPE_FIELD;
1839  if (s->pict_type == AV_PICTURE_TYPE_P) {
1840  /* if P type, zero motion vector is implied */
1841  s->mv_dir = MV_DIR_FORWARD;
1842  s->mv[0][0][0] = s->mv[0][0][1] = 0;
1843  s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
1844  s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
1845  s->field_select[0][0] = (s->picture_structure - 1) & 1;
1846  } else {
1847  /* if B type, reuse previous vectors and directions */
1848  s->mv[0][0][0] = s->last_mv[0][0][0];
1849  s->mv[0][0][1] = s->last_mv[0][0][1];
1850  s->mv[1][0][0] = s->last_mv[1][0][0];
1851  s->mv[1][0][1] = s->last_mv[1][0][1];
1852  }
1853  }
1854  }
1855  }
1856 eos: // end of slice
1857  *buf += (get_bits_count(&s->gb)-1)/8;
1858  av_dlog(s, "y %d %d %d %d\n", s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y);
1859  return 0;
1860 }
1861 
1862 static int slice_decode_thread(AVCodecContext *c, void *arg)
1863 {
1864  MpegEncContext *s = *(void**)arg;
1865  const uint8_t *buf = s->gb.buffer;
1866  int mb_y = s->start_mb_y;
1867  const int field_pic = s->picture_structure != PICT_FRAME;
1868 
1869  s->error_count = (3 * (s->end_mb_y - s->start_mb_y) * s->mb_width) >> field_pic;
1870 
1871  for (;;) {
1872  uint32_t start_code;
1873  int ret;
1874 
1875  ret = mpeg_decode_slice(s, mb_y, &buf, s->gb.buffer_end - buf);
1876  emms_c();
1877  av_dlog(c, "ret:%d resync:%d/%d mb:%d/%d ts:%d/%d ec:%d\n",
1878  ret, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y,
1879  s->start_mb_y, s->end_mb_y, s->error_count);
1880  if (ret < 0) {
1881  if (c->err_recognition & AV_EF_EXPLODE)
1882  return ret;
1883  if (s->resync_mb_x >= 0 && s->resync_mb_y >= 0)
1885  } else {
1887  }
1888 
1889  if (s->mb_y == s->end_mb_y)
1890  return 0;
1891 
1892  start_code = -1;
1893  buf = avpriv_mpv_find_start_code(buf, s->gb.buffer_end, &start_code);
1894  mb_y= (start_code - SLICE_MIN_START_CODE) << field_pic;
1896  mb_y++;
1897  if (mb_y < 0 || mb_y >= s->end_mb_y)
1898  return -1;
1899  }
1900 }
1901 
1906 static int slice_end(AVCodecContext *avctx, AVFrame *pict)
1907 {
1908  Mpeg1Context *s1 = avctx->priv_data;
1909  MpegEncContext *s = &s1->mpeg_enc_ctx;
1910 
1912  return 0;
1913 
1914  if (s->avctx->hwaccel) {
1915  if (s->avctx->hwaccel->end_frame(s->avctx) < 0)
1916  av_log(avctx, AV_LOG_ERROR, "hardware accelerator failed to decode picture\n");
1917  }
1918 
1920  ff_xvmc_field_end(s);
1921 
1922  /* end of slice reached */
1923  if (/*s->mb_y << field_pic == s->mb_height &&*/ !s->first_field) {
1924  /* end of image */
1925 
1927 
1928  ff_er_frame_end(s);
1929 
1930  ff_MPV_frame_end(s);
1931 
1932  if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
1933  *pict = s->current_picture_ptr->f;
1934  ff_print_debug_info(s, pict);
1935  } else {
1936  if (avctx->active_thread_type & FF_THREAD_FRAME)
1937  s->picture_number++;
1938  /* latency of 1 frame for I- and P-frames */
1939  /* XXX: use another variable than picture_number */
1940  if (s->last_picture_ptr != NULL) {
1941  *pict = s->last_picture_ptr->f;
1942  ff_print_debug_info(s, pict);
1943  }
1944  }
1945 
1946  return 1;
1947  } else {
1948  return 0;
1949  }
1950 }
1951 
1953  const uint8_t *buf, int buf_size)
1954 {
1955  Mpeg1Context *s1 = avctx->priv_data;
1956  MpegEncContext *s = &s1->mpeg_enc_ctx;
1957  int width, height;
1958  int i, v, j;
1959 
1960  init_get_bits(&s->gb, buf, buf_size*8);
1961 
1962  width = get_bits(&s->gb, 12);
1963  height = get_bits(&s->gb, 12);
1964  if (width <= 0 || height <= 0)
1965  return -1;
1966  s->aspect_ratio_info = get_bits(&s->gb, 4);
1967  if (s->aspect_ratio_info == 0) {
1968  av_log(avctx, AV_LOG_ERROR, "aspect ratio has forbidden 0 value\n");
1969  if (avctx->err_recognition & AV_EF_BITSTREAM)
1970  return -1;
1971  }
1972  s->frame_rate_index = get_bits(&s->gb, 4);
1973  if (s->frame_rate_index == 0 || s->frame_rate_index > 13)
1974  return -1;
1975  s->bit_rate = get_bits(&s->gb, 18) * 400;
1976  if (get_bits1(&s->gb) == 0) /* marker */
1977  return -1;
1978  s->width = width;
1979  s->height = height;
1980 
1981  s->avctx->rc_buffer_size = get_bits(&s->gb, 10) * 1024 * 16;
1982  skip_bits(&s->gb, 1);
1983 
1984  /* get matrix */
1985  if (get_bits1(&s->gb)) {
1987  } else {
1988  for (i = 0; i < 64; i++) {
1989  j = s->dsp.idct_permutation[i];
1991  s->intra_matrix[j] = v;
1992  s->chroma_intra_matrix[j] = v;
1993  }
1994  }
1995  if (get_bits1(&s->gb)) {
1997  } else {
1998  for (i = 0; i < 64; i++) {
1999  int j = s->dsp.idct_permutation[i];
2001  s->inter_matrix[j] = v;
2002  s->chroma_inter_matrix[j] = v;
2003  }
2004  }
2005 
2006  if (show_bits(&s->gb, 23) != 0) {
2007  av_log(s->avctx, AV_LOG_ERROR, "sequence header damaged\n");
2008  return -1;
2009  }
2010 
2011  /* we set MPEG-2 parameters so that it emulates MPEG-1 */
2012  s->progressive_sequence = 1;
2013  s->progressive_frame = 1;
2015  s->frame_pred_frame_dct = 1;
2016  s->chroma_format = 1;
2018  s->out_format = FMT_MPEG1;
2019  s->swap_uv = 0; // AFAIK VCR2 does not have SEQ_HEADER
2020  if (s->flags & CODEC_FLAG_LOW_DELAY)
2021  s->low_delay = 1;
2022 
2023  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
2024  av_log(s->avctx, AV_LOG_DEBUG, "vbv buffer: %d, bitrate:%d\n",
2025  s->avctx->rc_buffer_size, s->bit_rate);
2026 
2027  return 0;
2028 }
2029 
2031 {
2032  Mpeg1Context *s1 = avctx->priv_data;
2033  MpegEncContext *s = &s1->mpeg_enc_ctx;
2034  int i, v;
2035 
2036  /* start new MPEG-1 context decoding */
2037  s->out_format = FMT_MPEG1;
2038  if (s1->mpeg_enc_ctx_allocated) {
2039  ff_MPV_common_end(s);
2040  }
2041  s->width = avctx->coded_width;
2042  s->height = avctx->coded_height;
2043  avctx->has_b_frames = 0; // true?
2044  s->low_delay = 1;
2045 
2046  avctx->pix_fmt = mpeg_get_pixelformat(avctx);
2047  avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
2048 
2049  if (avctx->pix_fmt == AV_PIX_FMT_XVMC_MPEG2_IDCT || avctx->hwaccel ||
2051  if (avctx->idct_algo == FF_IDCT_AUTO)
2052  avctx->idct_algo = FF_IDCT_SIMPLE;
2053 
2054  if (ff_MPV_common_init(s) < 0)
2055  return -1;
2056  exchange_uv(s); // common init reset pblocks, so we swap them here
2057  s->swap_uv = 1; // in case of xvmc we need to swap uv for each MB
2058  s1->mpeg_enc_ctx_allocated = 1;
2059 
2060  for (i = 0; i < 64; i++) {
2061  int j = s->dsp.idct_permutation[i];
2063  s->intra_matrix[j] = v;
2064  s->chroma_intra_matrix[j] = v;
2065 
2067  s->inter_matrix[j] = v;
2068  s->chroma_inter_matrix[j] = v;
2069  }
2070 
2071  s->progressive_sequence = 1;
2072  s->progressive_frame = 1;
2074  s->frame_pred_frame_dct = 1;
2075  s->chroma_format = 1;
2077  s1->save_width = s->width;
2078  s1->save_height = s->height;
2080  return 0;
2081 }
2082 
2083 
2085  const uint8_t *p, int buf_size)
2086 {
2087  const uint8_t *buf_end = p + buf_size;
2088 
2089  /* we parse the DTG active format information */
2090  if (buf_end - p >= 5 &&
2091  p[0] == 'D' && p[1] == 'T' && p[2] == 'G' && p[3] == '1') {
2092  int flags = p[4];
2093  p += 5;
2094  if (flags & 0x80) {
2095  /* skip event id */
2096  p += 2;
2097  }
2098  if (flags & 0x40) {
2099  if (buf_end - p < 1)
2100  return;
2101  avctx->dtg_active_format = p[0] & 0x0f;
2102  }
2103  }
2104 }
2105 
2106 static void mpeg_decode_gop(AVCodecContext *avctx,
2107  const uint8_t *buf, int buf_size)
2108 {
2109  Mpeg1Context *s1 = avctx->priv_data;
2110  MpegEncContext *s = &s1->mpeg_enc_ctx;
2111 
2112  int time_code_hours, time_code_minutes;
2113  int time_code_seconds, time_code_pictures;
2114  int broken_link;
2115 
2116  init_get_bits(&s->gb, buf, buf_size*8);
2117 
2118  skip_bits1(&s->gb); /* drop_frame_flag */
2119 
2120  time_code_hours = get_bits(&s->gb, 5);
2121  time_code_minutes = get_bits(&s->gb, 6);
2122  skip_bits1(&s->gb); // marker bit
2123  time_code_seconds = get_bits(&s->gb, 6);
2124  time_code_pictures = get_bits(&s->gb, 6);
2125 
2126  s1->closed_gop = get_bits1(&s->gb);
2127  /*broken_link indicate that after editing the
2128  reference frames of the first B-Frames after GOP I-Frame
2129  are missing (open gop)*/
2130  broken_link = get_bits1(&s->gb);
2131 
2132  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
2133  av_log(s->avctx, AV_LOG_DEBUG, "GOP (%2d:%02d:%02d.[%02d]) closed_gop=%d broken_link=%d\n",
2134  time_code_hours, time_code_minutes, time_code_seconds,
2135  time_code_pictures, s1->closed_gop, broken_link);
2136 }
2142 {
2143  int i;
2144  uint32_t state = pc->state;
2145 
2146  /* EOF considered as end of frame */
2147  if (buf_size == 0)
2148  return 0;
2149 
2150 /*
2151  0 frame start -> 1/4
2152  1 first_SEQEXT -> 0/2
2153  2 first field start -> 3/0
2154  3 second_SEQEXT -> 2/0
2155  4 searching end
2156 */
2157 
2158  for (i = 0; i < buf_size; i++) {
2159  assert(pc->frame_start_found >= 0 && pc->frame_start_found <= 4);
2160  if (pc->frame_start_found & 1) {
2161  if (state == EXT_START_CODE && (buf[i] & 0xF0) != 0x80)
2162  pc->frame_start_found--;
2163  else if (state == EXT_START_CODE + 2) {
2164  if ((buf[i] & 3) == 3)
2165  pc->frame_start_found = 0;
2166  else
2167  pc->frame_start_found = (pc->frame_start_found + 1) & 3;
2168  }
2169  state++;
2170  } else {
2171  i = avpriv_mpv_find_start_code(buf + i, buf + buf_size, &state) - buf - 1;
2172  if (pc->frame_start_found == 0 && state >= SLICE_MIN_START_CODE && state <= SLICE_MAX_START_CODE) {
2173  i++;
2174  pc->frame_start_found = 4;
2175  }
2176  if (state == SEQ_END_CODE) {
2177  pc->frame_start_found = 0;
2178  pc->state=-1;
2179  return i+1;
2180  }
2181  if (pc->frame_start_found == 2 && state == SEQ_START_CODE)
2182  pc->frame_start_found = 0;
2183  if (pc->frame_start_found < 4 && state == EXT_START_CODE)
2184  pc->frame_start_found++;
2185  if (pc->frame_start_found == 4 && (state & 0xFFFFFF00) == 0x100) {
2186  if (state < SLICE_MIN_START_CODE || state > SLICE_MAX_START_CODE) {
2187  pc->frame_start_found = 0;
2188  pc->state = -1;
2189  return i - 3;
2190  }
2191  }
2192  if (pc->frame_start_found == 0 && s && state == PICTURE_START_CODE) {
2193  ff_fetch_timestamp(s, i - 3, 1);
2194  }
2195  }
2196  }
2197  pc->state = state;
2198  return END_NOT_FOUND;
2199 }
2200 
2201 static int decode_chunks(AVCodecContext *avctx,
2202  AVFrame *picture, int *got_output,
2203  const uint8_t *buf, int buf_size)
2204 {
2205  Mpeg1Context *s = avctx->priv_data;
2207  const uint8_t *buf_ptr = buf;
2208  const uint8_t *buf_end = buf + buf_size;
2209  int ret, input_size;
2210  int last_code = 0;
2211 
2212  for (;;) {
2213  /* find next start code */
2214  uint32_t start_code = -1;
2215  buf_ptr = avpriv_mpv_find_start_code(buf_ptr, buf_end, &start_code);
2216  if (start_code > 0x1ff) {
2217  if (s2->pict_type != AV_PICTURE_TYPE_B || avctx->skip_frame <= AVDISCARD_DEFAULT) {
2218  if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE)) {
2219  int i;
2220 
2221  avctx->execute(avctx, slice_decode_thread, &s2->thread_context[0], NULL, s->slice_count, sizeof(void*));
2222  for (i = 0; i < s->slice_count; i++)
2223  s2->error_count += s2->thread_context[i]->error_count;
2224  }
2225 
2227  ff_vdpau_mpeg_picture_complete(s2, buf, buf_size, s->slice_count);
2228 
2229  if (slice_end(avctx, picture)) {
2230  if (s2->last_picture_ptr || s2->low_delay) //FIXME merge with the stuff in mpeg_decode_slice
2231  *got_output = 1;
2232  }
2233  }
2234  s2->pict_type = 0;
2235  return FFMAX(0, buf_ptr - buf - s2->parse_context.last_index);
2236  }
2237 
2238  input_size = buf_end - buf_ptr;
2239 
2240  if (avctx->debug & FF_DEBUG_STARTCODE) {
2241  av_log(avctx, AV_LOG_DEBUG, "%3X at %td left %d\n", start_code, buf_ptr-buf, input_size);
2242  }
2243 
2244  /* prepare data for next start code */
2245  switch (start_code) {
2246  case SEQ_START_CODE:
2247  if (last_code == 0) {
2248  mpeg1_decode_sequence(avctx, buf_ptr, input_size);
2249  s->sync=1;
2250  } else {
2251  av_log(avctx, AV_LOG_ERROR, "ignoring SEQ_START_CODE after %X\n", last_code);
2252  if (avctx->err_recognition & AV_EF_EXPLODE)
2253  return AVERROR_INVALIDDATA;
2254  }
2255  break;
2256 
2257  case PICTURE_START_CODE:
2258  if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE) && s->slice_count) {
2259  int i;
2260 
2261  avctx->execute(avctx, slice_decode_thread,
2262  s2->thread_context, NULL,
2263  s->slice_count, sizeof(void*));
2264  for (i = 0; i < s->slice_count; i++)
2265  s2->error_count += s2->thread_context[i]->error_count;
2266  s->slice_count = 0;
2267  }
2268  if (last_code == 0 || last_code == SLICE_MIN_START_CODE) {
2269  ret = mpeg_decode_postinit(avctx);
2270  if (ret < 0) {
2271  av_log(avctx, AV_LOG_ERROR, "mpeg_decode_postinit() failure\n");
2272  return ret;
2273  }
2274 
2275  /* we have a complete image: we try to decompress it */
2276  if (mpeg1_decode_picture(avctx, buf_ptr, input_size) < 0)
2277  s2->pict_type = 0;
2278  s2->first_slice = 1;
2279  last_code = PICTURE_START_CODE;
2280  } else {
2281  av_log(avctx, AV_LOG_ERROR, "ignoring pic after %X\n", last_code);
2282  if (avctx->err_recognition & AV_EF_EXPLODE)
2283  return AVERROR_INVALIDDATA;
2284  }
2285  break;
2286  case EXT_START_CODE:
2287  init_get_bits(&s2->gb, buf_ptr, input_size*8);
2288 
2289  switch (get_bits(&s2->gb, 4)) {
2290  case 0x1:
2291  if (last_code == 0) {
2293  } else {
2294  av_log(avctx, AV_LOG_ERROR, "ignoring seq ext after %X\n", last_code);
2295  if (avctx->err_recognition & AV_EF_EXPLODE)
2296  return AVERROR_INVALIDDATA;
2297  }
2298  break;
2299  case 0x2:
2301  break;
2302  case 0x3:
2304  break;
2305  case 0x7:
2307  break;
2308  case 0x8:
2309  if (last_code == PICTURE_START_CODE) {
2311  } else {
2312  av_log(avctx, AV_LOG_ERROR, "ignoring pic cod ext after %X\n", last_code);
2313  if (avctx->err_recognition & AV_EF_EXPLODE)
2314  return AVERROR_INVALIDDATA;
2315  }
2316  break;
2317  }
2318  break;
2319  case USER_START_CODE:
2320  mpeg_decode_user_data(avctx, buf_ptr, input_size);
2321  break;
2322  case GOP_START_CODE:
2323  if (last_code == 0) {
2324  s2->first_field=0;
2325  mpeg_decode_gop(avctx, buf_ptr, input_size);
2326  s->sync=1;
2327  } else {
2328  av_log(avctx, AV_LOG_ERROR, "ignoring GOP_START_CODE after %X\n", last_code);
2329  if (avctx->err_recognition & AV_EF_EXPLODE)
2330  return AVERROR_INVALIDDATA;
2331  }
2332  break;
2333  default:
2334  if (start_code >= SLICE_MIN_START_CODE &&
2335  start_code <= SLICE_MAX_START_CODE && last_code != 0) {
2336  const int field_pic = s2->picture_structure != PICT_FRAME;
2337  int mb_y = (start_code - SLICE_MIN_START_CODE) << field_pic;
2338  last_code = SLICE_MIN_START_CODE;
2339 
2341  mb_y++;
2342 
2343  if (mb_y >= s2->mb_height) {
2344  av_log(s2->avctx, AV_LOG_ERROR, "slice below image (%d >= %d)\n", mb_y, s2->mb_height);
2345  return -1;
2346  }
2347 
2348  if (s2->last_picture_ptr == NULL) {
2349  /* Skip B-frames if we do not have reference frames and gop is not closed */
2350  if (s2->pict_type == AV_PICTURE_TYPE_B) {
2351  if (!s->closed_gop)
2352  break;
2353  }
2354  }
2355  if (s2->pict_type == AV_PICTURE_TYPE_I)
2356  s->sync=1;
2357  if (s2->next_picture_ptr == NULL) {
2358  /* Skip P-frames if we do not have a reference frame or we have an invalid header. */
2359  if (s2->pict_type == AV_PICTURE_TYPE_P && !s->sync) break;
2360  }
2361  if ((avctx->skip_frame >= AVDISCARD_NONREF && s2->pict_type == AV_PICTURE_TYPE_B) ||
2362  (avctx->skip_frame >= AVDISCARD_NONKEY && s2->pict_type != AV_PICTURE_TYPE_I) ||
2363  avctx->skip_frame >= AVDISCARD_ALL)
2364  break;
2365 
2366  if (!s->mpeg_enc_ctx_allocated)
2367  break;
2368 
2369  if (s2->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
2370  if (mb_y < avctx->skip_top || mb_y >= s2->mb_height - avctx->skip_bottom)
2371  break;
2372  }
2373 
2374  if (!s2->pict_type) {
2375  av_log(avctx, AV_LOG_ERROR, "Missing picture start code\n");
2376  if (avctx->err_recognition & AV_EF_EXPLODE)
2377  return AVERROR_INVALIDDATA;
2378  break;
2379  }
2380 
2381  if (s2->first_slice) {
2382  s2->first_slice = 0;
2383  if (mpeg_field_start(s2, buf, buf_size) < 0)
2384  return -1;
2385  }
2386  if (!s2->current_picture_ptr) {
2387  av_log(avctx, AV_LOG_ERROR, "current_picture not initialized\n");
2388  return AVERROR_INVALIDDATA;
2389  }
2390 
2391  if (avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU) {
2392  s->slice_count++;
2393  break;
2394  }
2395 
2396  if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE)) {
2397  int threshold = (s2->mb_height * s->slice_count +
2398  s2->slice_context_count / 2) /
2399  s2->slice_context_count;
2400  if (threshold <= mb_y) {
2401  MpegEncContext *thread_context = s2->thread_context[s->slice_count];
2402 
2403  thread_context->start_mb_y = mb_y;
2404  thread_context->end_mb_y = s2->mb_height;
2405  if (s->slice_count) {
2406  s2->thread_context[s->slice_count-1]->end_mb_y = mb_y;
2407  ret = ff_update_duplicate_context(thread_context,
2408  s2);
2409  if (ret < 0)
2410  return ret;
2411  }
2412  init_get_bits(&thread_context->gb, buf_ptr, input_size*8);
2413  s->slice_count++;
2414  }
2415  buf_ptr += 2; // FIXME add minimum number of bytes per slice
2416  } else {
2417  ret = mpeg_decode_slice(s2, mb_y, &buf_ptr, input_size);
2418  emms_c();
2419 
2420  if (ret < 0) {
2421  if (avctx->err_recognition & AV_EF_EXPLODE)
2422  return ret;
2423  if (s2->resync_mb_x >= 0 && s2->resync_mb_y >= 0)
2425  } else {
2427  }
2428  }
2429  }
2430  break;
2431  }
2432  }
2433 }
2434 
2436  void *data, int *got_output,
2437  AVPacket *avpkt)
2438 {
2439  const uint8_t *buf = avpkt->data;
2440  int buf_size = avpkt->size;
2441  Mpeg1Context *s = avctx->priv_data;
2442  AVFrame *picture = data;
2444  av_dlog(avctx, "fill_buffer\n");
2445 
2446  if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == SEQ_END_CODE)) {
2447  /* special case for last picture */
2448  if (s2->low_delay == 0 && s2->next_picture_ptr) {
2449  *picture = s2->next_picture_ptr->f;
2450  s2->next_picture_ptr = NULL;
2451 
2452  *got_output = 1;
2453  }
2454  return buf_size;
2455  }
2456 
2457  if (s2->flags & CODEC_FLAG_TRUNCATED) {
2458  int next = ff_mpeg1_find_frame_end(&s2->parse_context, buf, buf_size, NULL);
2459 
2460  if (ff_combine_frame(&s2->parse_context, next, (const uint8_t **)&buf, &buf_size) < 0)
2461  return buf_size;
2462  }
2463 
2464  if (s->mpeg_enc_ctx_allocated == 0 && avctx->codec_tag == AV_RL32("VCR2"))
2465  vcr2_init_sequence(avctx);
2466 
2467  s->slice_count = 0;
2468 
2469  if (avctx->extradata && !s->extradata_decoded) {
2470  int ret = decode_chunks(avctx, picture, got_output, avctx->extradata, avctx->extradata_size);
2471  s->extradata_decoded = 1;
2472  if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE))
2473  return ret;
2474  }
2475 
2476  return decode_chunks(avctx, picture, got_output, buf, buf_size);
2477 }
2478 
2479 
2480 static void flush(AVCodecContext *avctx)
2481 {
2482  Mpeg1Context *s = avctx->priv_data;
2483 
2484  s->sync=0;
2485  s->closed_gop = 0;
2486 
2487  ff_mpeg_flush(avctx);
2488 }
2489 
2491 {
2492  Mpeg1Context *s = avctx->priv_data;
2493 
2494  if (s->mpeg_enc_ctx_allocated)
2496  return 0;
2497 }
2498 
2500  { FF_PROFILE_MPEG2_422, "4:2:2" },
2501  { FF_PROFILE_MPEG2_HIGH, "High" },
2502  { FF_PROFILE_MPEG2_SS, "Spatially Scalable" },
2503  { FF_PROFILE_MPEG2_SNR_SCALABLE, "SNR Scalable" },
2504  { FF_PROFILE_MPEG2_MAIN, "Main" },
2505  { FF_PROFILE_MPEG2_SIMPLE, "Simple" },
2506  { FF_PROFILE_RESERVED, "Reserved" },
2507  { FF_PROFILE_RESERVED, "Reserved" },
2508  { FF_PROFILE_UNKNOWN },
2509 };
2510 
2511 
2513  .name = "mpeg1video",
2514  .type = AVMEDIA_TYPE_VIDEO,
2515  .id = AV_CODEC_ID_MPEG1VIDEO,
2516  .priv_data_size = sizeof(Mpeg1Context),
2520  .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
2523  .flush = flush,
2524  .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video"),
2526 };
2527 
2529  .name = "mpeg2video",
2530  .type = AVMEDIA_TYPE_VIDEO,
2531  .id = AV_CODEC_ID_MPEG2VIDEO,
2532  .priv_data_size = sizeof(Mpeg1Context),
2536  .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
2539  .flush = flush,
2540  .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 video"),
2541  .profiles = NULL_IF_CONFIG_SMALL(mpeg2_video_profiles),
2542 };
2543 
2544 #if CONFIG_MPEG_XVMC_DECODER
2545 static av_cold int mpeg_mc_decode_init(AVCodecContext *avctx)
2546 {
2547  if (avctx->active_thread_type & FF_THREAD_SLICE)
2548  return -1;
2549  if (!(avctx->slice_flags & SLICE_FLAG_CODED_ORDER))
2550  return -1;
2551  if (!(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD)) {
2552  av_dlog(avctx, "mpeg12.c: XvMC decoder will work better if SLICE_FLAG_ALLOW_FIELD is set\n");
2553  }
2554  mpeg_decode_init(avctx);
2555 
2557  avctx->xvmc_acceleration = 2; // 2 - the blocks are packed!
2558 
2559  return 0;
2560 }
2561 
2562 AVCodec ff_mpeg_xvmc_decoder = {
2563  .name = "mpegvideo_xvmc",
2564  .type = AVMEDIA_TYPE_VIDEO,
2566  .priv_data_size = sizeof(Mpeg1Context),
2567  .init = mpeg_mc_decode_init,
2570  .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
2572  .flush = flush,
2573  .long_name = NULL_IF_CONFIG_SMALL("MPEG-1/2 video XvMC (X-Video Motion Compensation)"),
2574 };
2575 
2576 #endif
2577 
2578 #if CONFIG_MPEG_VDPAU_DECODER
2579 AVCodec ff_mpeg_vdpau_decoder = {
2580  .name = "mpegvideo_vdpau",
2581  .type = AVMEDIA_TYPE_VIDEO,
2582  .id = AV_CODEC_ID_MPEG2VIDEO,
2583  .priv_data_size = sizeof(Mpeg1Context),
2587  .capabilities = CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED |
2589  .flush = flush,
2590  .long_name = NULL_IF_CONFIG_SMALL("MPEG-1/2 video (VDPAU acceleration)"),
2591 };
2592 #endif
2593 
2594 #if CONFIG_MPEG1_VDPAU_DECODER
2595 AVCodec ff_mpeg1_vdpau_decoder = {
2596  .name = "mpeg1video_vdpau",
2597  .type = AVMEDIA_TYPE_VIDEO,
2598  .id = AV_CODEC_ID_MPEG1VIDEO,
2599  .priv_data_size = sizeof(Mpeg1Context),
2603  .capabilities = CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED |
2605  .flush = flush,
2606  .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video (VDPAU acceleration)"),
2607 };
2608 #endif
const uint16_t ff_mpeg1_default_non_intra_matrix[64]
Definition: mpeg12data.c:41
#define PICT_BOTTOM_FIELD
Definition: mpegvideo.h:640
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:1482
#define ER_AC_END
Definition: mpegvideo.h:498
const struct AVCodec * codec
Definition: avcodec.h:1348
int table_size
Definition: get_bits.h:66
#define PICT_TOP_FIELD
Definition: mpegvideo.h:639
discard all frames except keyframes
Definition: avcodec.h:535
void ff_init_block_index(MpegEncContext *s)
Definition: mpegvideo.c:2466
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:563
int picture_number
Definition: mpegvideo.h:245
#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:2528
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:286
#define MV_TYPE_FIELD
2 vectors, one per field
Definition: mpegvideo.h:392
const uint8_t * y_dc_scale_table
qscale -> y_dc_scale table
Definition: mpegvideo.h:324
int last_mv[2][2][2]
last MV, used for MV prediction in MPEG1 & B-frame MPEG4
Definition: mpegvideo.h:401
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:882
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:2760
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:287
uint16_t chroma_intra_matrix[64]
Definition: mpegvideo.h:441
int qscale_type
Definition: avcodec.h:1150
static const AVProfile mpeg2_video_profiles[]
Definition: mpeg12.c:2499
#define ER_MV_END
Definition: mpegvideo.h:500
#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:251
uint16_t chroma_inter_matrix[64]
Definition: mpegvideo.h:443
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:1906
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:227
void ff_init_rl(RLTable *rl, uint8_t static_store[2][2 *MAX_RUN+MAX_LEVEL+3])
Definition: mpegvideo.c:1191
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:124
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:78
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:342
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:656
int encoding
true if we are encoding (vs decoding)
Definition: mpegvideo.h:229
int field_select[2][2]
Definition: mpegvideo.h:400
#define USES_LIST(a, list)
does this mb use listX, note does not work if subMBs
Definition: mpegvideo.h:126
#define HAS_CBP(a)
Definition: mpegvideo.h:127
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
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:104
uint8_t
DCTELEM(*[12] pblocks)[64]
Definition: mpegvideo.h:672
#define PICT_FRAME
Definition: mpegvideo.h:641
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:219
#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
#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:660
int interlaced_dct
Definition: mpegvideo.h:661
Picture current_picture
copy of the current picture structure.
Definition: mpegvideo.h:313
#define IS_QUANT(a)
Definition: mpegvideo.h:124
int intra_dc_precision
Definition: mpegvideo.h:643
int repeat_first_field
Definition: mpegvideo.h:650
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...
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:231
#define DECODE_SLICE_ERROR
Definition: mpeg12.c:1633
int mb_height
number of MBs horizontally & vertically
Definition: mpegvideo.h:247
static void mpeg_decode_gop(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
Definition: mpeg12.c:2106
void ff_MPV_frame_end(MpegEncContext *s)
Definition: mpegvideo.c:1581
int codec_tag
internal codec_tag upper case converted from avctx codec_tag
Definition: mpegvideo.h:237
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:1492
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:289
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:2201
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:321
#define s2
Definition: regdef.h:39
Multithreading support functions.
static int slice_decode_thread(AVCodecContext *c, void *arg)
Definition: mpeg12.c:1862
int mb_skipped
MUST BE SET only during DECODING.
Definition: mpegvideo.h:331
int chroma_y_shift
Definition: mpegvideo.h:657
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
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:1460
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:74
XVideo Motion Acceleration via common packet passing.
Definition: pixfmt.h:80
#define ER_MV_ERROR
Definition: mpegvideo.h:497
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
#define ER_DC_ERROR
Definition: mpegvideo.h:496
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:108
int low_delay
no reordering needed / has no b-frames
Definition: mpegvideo.h:570
static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred)
Definition: mpeg12.c:55
GetBitContext gb
Definition: mpegvideo.h:626
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:2498
#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:505
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
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
void ff_er_add_slice(MpegEncContext *s, int startx, int starty, int endx, int endy, int status)
Add a slice.
int intra_vlc_format
Definition: mpegvideo.h:648
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:2435
static AVFrame * picture
int progressive_frame
Definition: mpegvideo.h:659
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:645
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:332
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:317
#define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update)
Definition: get_bits.h:481
RLTable ff_rl_mpeg1
Definition: mpeg12data.c:166
int alternate_scan
Definition: mpegvideo.h:649
static int mpeg_decode_end(AVCodecContext *avctx)
Definition: mpeg12.c:2490
int save_height
Definition: mpeg12.h:41
#define GOP_START_CODE
Definition: mpegvideo.h:76
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
void ff_er_frame_end(MpegEncContext *s)
static VLC mbincr_vlc
Definition: mpeg12.c:672
static void flush(AVCodecContext *avctx)
Definition: mpeg12.c:2480
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:261
#define ER_AC_ERROR
Definition: mpegvideo.h:495
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:636
#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:1370
int ff_mpeg_update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
Definition: mpegvideo.c:570
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:663
int save_width
Definition: mpeg12.h:41
static int mpeg1_decode_sequence(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
Definition: mpeg12.c:1952
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:389
int frame_pred_frame_dct
Definition: mpegvideo.h:644
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:147
uint16_t inter_matrix[64]
Definition: mpegvideo.h:442
uint8_t * buffer
Definition: parser.h:29
int concealment_motion_vectors
Definition: mpegvideo.h:646
struct MpegEncContext * thread_context[MAX_THREADS]
Definition: mpegvideo.h:288
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:1634
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:266
#define USER_START_CODE
Definition: cavs.h:32
int height
picture size. must be a multiple of 16
Definition: mpegvideo.h:215
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:391
int chroma_420_type
Definition: mpegvideo.h:651
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:1403
int progressive_sequence
Definition: mpegvideo.h:635
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
void ff_draw_horiz_band(MpegEncContext *s, int y, int h)
Definition: mpegvideo.c:2399
#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:2030
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:2141
void ff_er_frame_start(MpegEncContext *s)
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:361
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
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:385
int pict_type
AV_PICTURE_TYPE_I, AV_PICTURE_TYPE_P, AV_PICTURE_TYPE_B, ...
Definition: mpegvideo.h:349
int bit_rate
wanted bit rate
Definition: mpegvideo.h:218
DCTELEM(* block)[64]
points to one of the following blocks
Definition: mpegvideo.h:674
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:325
uint8_t level
Definition: svq3.c:125
#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:399
int b8_stride
2*mb_width+1 used for some 8x8 block arrays to allow simple addressing
Definition: mpegvideo.h:249
void(* clear_blocks)(DCTELEM *blocks)
Definition: dsputil.h:219
int height
Definition: gxfenc.c:72
MpegEncContext.
Definition: mpegvideo.h:211
Picture * next_picture_ptr
pointer to the next picture (for bidir pred)
Definition: mpegvideo.h:316
uint8_t run
Definition: get_bits.h:72
#define MAX_RUN
Definition: rl.h:34
struct AVCodecContext * avctx
Definition: mpegvideo.h:213
#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:2512
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:248
uint8_t * dest[3]
Definition: mpegvideo.h:435
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:1642
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:193
#define ER_DC_END
Definition: mpegvideo.h:499
Picture * last_picture_ptr
pointer to the previous picture.
Definition: mpegvideo.h:315
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:1568
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:547
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:353
void ff_mpeg12_common_init(MpegEncContext *s)
Definition: mpeg12.c:649
int picture_structure
Definition: mpegvideo.h:637
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:2387
void ff_MPV_common_end(MpegEncContext *s)
Definition: mpegvideo.c:1141
int len
void ff_print_debug_info(MpegEncContext *s, AVFrame *pict)
Print debugging info for the given picture.
Definition: mpegvideo.c:1743
#define MV_TYPE_DMV
2 vectors, special mpeg2 Dual Prime Vectors
Definition: mpegvideo.h:393
#define SEQ_START_CODE
Definition: mpegvideo.h:75
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:506
static int mpeg2_fast_decode_block_intra(MpegEncContext *s, DCTELEM *block, int n)
Definition: mpeg12.c:532
ParseContext parse_context
Definition: mpegvideo.h:512
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:95
int flags
AVCodecContext.flags (HQ, MV4, ...)
Definition: mpegvideo.h:230
uint16_t intra_matrix[64]
matrix transmitted in the bitstream
Definition: mpegvideo.h:440
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:265
const uint8_t ff_mpeg12_mbMotionVectorTable[17][2]
Definition: mpeg12data.c:288
#define PICTURE_START_CODE
Definition: mpegvideo.h:77
static const uint8_t table_mb_ptype[7][2]
Definition: mpeg12decdata.h:38
static void mpeg_decode_picture_display_extension(Mpeg1Context *s1)
Definition: mpeg12.c:1427
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:2084
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:718
#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