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