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