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