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