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