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