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