mpeg12.c 82.5 KB
Newer Older
Fabrice Bellard's avatar
Fabrice Bellard committed
1
/*
2
 * MPEG1/2 decoder
3
 * Copyright (c) 2000,2001 Fabrice Bellard.
4
 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
Fabrice Bellard's avatar
Fabrice Bellard committed
5
 *
6 7 8
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
9 10
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
11
 * version 2.1 of the License, or (at your option) any later version.
Fabrice Bellard's avatar
Fabrice Bellard committed
12
 *
13
 * FFmpeg is distributed in the hope that it will be useful,
Fabrice Bellard's avatar
Fabrice Bellard committed
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
Fabrice Bellard's avatar
Fabrice Bellard committed
17
 *
18
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with FFmpeg; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Fabrice Bellard's avatar
Fabrice Bellard committed
21
 */
22

Michael Niedermayer's avatar
Michael Niedermayer committed
23 24
/**
 * @file mpeg12.c
25
 * MPEG1/2 decoder
Michael Niedermayer's avatar
Michael Niedermayer committed
26
 */
27

Fabrice Bellard's avatar
Fabrice Bellard committed
28
//#define DEBUG
Fabrice Bellard's avatar
Fabrice Bellard committed
29 30 31 32
#include "avcodec.h"
#include "dsputil.h"
#include "mpegvideo.h"

33
#include "mpeg12.h"
Fabrice Bellard's avatar
Fabrice Bellard committed
34
#include "mpeg12data.h"
35
#include "mpeg12decdata.h"
36
#include "bytestream.h"
Fabrice Bellard's avatar
Fabrice Bellard committed
37

38 39 40
//#undef NDEBUG
//#include <assert.h>

41

42 43 44 45 46 47 48 49
#define DC_VLC_BITS 9
#define MV_VLC_BITS 9
#define MBINCR_VLC_BITS 9
#define MB_PAT_VLC_BITS 9
#define MB_PTYPE_VLC_BITS 6
#define MB_BTYPE_VLC_BITS 6
#define TEX_VLC_BITS 9

50 51
static inline int mpeg1_decode_block_inter(MpegEncContext *s,
                              DCTELEM *block,
52
                              int n);
53 54
static inline int mpeg1_decode_block_intra(MpegEncContext *s,
                              DCTELEM *block,
Fabrice Bellard's avatar
Fabrice Bellard committed
55
                              int n);
56
static inline int mpeg1_fast_decode_block_inter(MpegEncContext *s, DCTELEM *block, int n);
57 58
static inline int mpeg2_decode_block_non_intra(MpegEncContext *s,
                                        DCTELEM *block,
Fabrice Bellard's avatar
Fabrice Bellard committed
59
                                        int n);
60 61
static inline int mpeg2_decode_block_intra(MpegEncContext *s,
                                    DCTELEM *block,
Fabrice Bellard's avatar
Fabrice Bellard committed
62
                                    int n);
63
static inline int mpeg2_fast_decode_block_non_intra(MpegEncContext *s, DCTELEM *block, int n);
64
static inline int mpeg2_fast_decode_block_intra(MpegEncContext *s, DCTELEM *block, int n);
Fabrice Bellard's avatar
Fabrice Bellard committed
65
static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred);
Ivan Kalvachev's avatar
Ivan Kalvachev committed
66
static void exchange_uv(MpegEncContext *s);
Fabrice Bellard's avatar
Fabrice Bellard committed
67

Ivan Kalvachev's avatar
Ivan Kalvachev committed
68 69
extern int XVMC_field_start(MpegEncContext *s, AVCodecContext *avctx);
extern int XVMC_field_end(MpegEncContext *s);
70
extern void XVMC_pack_pblocks(MpegEncContext *s,int cbp);
Ivan Kalvachev's avatar
Ivan Kalvachev committed
71
extern void XVMC_init_block(MpegEncContext *s);//set s->block
Ivan Kalvachev's avatar
Ivan Kalvachev committed
72

73 74 75 76
static const enum PixelFormat pixfmt_yuv_420[]= {PIX_FMT_YUV420P,-1};
static const enum PixelFormat pixfmt_yuv_422[]= {PIX_FMT_YUV422P,-1};
static const enum PixelFormat pixfmt_yuv_444[]= {PIX_FMT_YUV444P,-1};
static const enum PixelFormat pixfmt_xvmc_mpg2_420[] = {
Ivan Kalvachev's avatar
Ivan Kalvachev committed
77 78
                                           PIX_FMT_XVMC_MPEG2_IDCT,
                                           PIX_FMT_XVMC_MPEG2_MC,
79
                                           -1};
80

81
uint8_t ff_mpeg12_static_rl_table_store[2][2][2*MAX_RUN + MAX_LEVEL + 3];
82

83
static void init_2d_vlc_rl(RLTable *rl, int use_static)
84
{
85
    int i;
86 87

    init_vlc(&rl->vlc, TEX_VLC_BITS, rl->n + 2,
88
             &rl->table_vlc[0][1], 4, 2,
89 90
             &rl->table_vlc[0][0], 4, 2, use_static);

91
    if(use_static)
92 93 94
        rl->rl_vlc[0]= av_mallocz_static(rl->vlc.table_size*sizeof(RL_VLC_ELEM));
    else
        rl->rl_vlc[0]= av_malloc(rl->vlc.table_size*sizeof(RL_VLC_ELEM));
95 96 97 98 99

    for(i=0; i<rl->vlc.table_size; i++){
        int code= rl->vlc.table[i][0];
        int len = rl->vlc.table[i][1];
        int level, run;
100

101 102 103 104 105 106 107 108 109 110 111
        if(len==0){ // illegal code
            run= 65;
            level= MAX_LEVEL;
        }else if(len<0){ //more bits needed
            run= 0;
            level= code;
        }else{
            if(code==rl->n){ //esc
                run= 65;
                level= 0;
            }else if(code==rl->n+1){ //eob
112 113
                run= 0;
                level= 127;
114 115 116 117 118 119 120 121 122 123 124
            }else{
                run=   rl->table_run  [code] + 1;
                level= rl->table_level[code];
            }
        }
        rl->rl_vlc[0][i].len= len;
        rl->rl_vlc[0][i].level= level;
        rl->rl_vlc[0][i].run= run;
    }
}

125
void ff_mpeg12_common_init(MpegEncContext *s)
Fabrice Bellard's avatar
Fabrice Bellard committed
126
{
127

128
    s->y_dc_scale_table=
129
    s->c_dc_scale_table= mpeg2_dc_scale_table[s->intra_dc_precision];
Michael Niedermayer's avatar
Michael Niedermayer committed
130

131
}
132

133 134 135 136 137 138 139
void ff_mpeg1_clean_buffers(MpegEncContext *s){
    s->last_dc[0] = 1 << (7 + s->intra_dc_precision);
    s->last_dc[1] = s->last_dc[0];
    s->last_dc[2] = s->last_dc[0];
    memset(s->last_mv, 0, sizeof(s->last_mv));
}

Fabrice Bellard's avatar
Fabrice Bellard committed
140 141 142 143 144 145 146 147 148 149 150 151

/******************************************/
/* decoding */

static VLC dc_lum_vlc;
static VLC dc_chroma_vlc;
static VLC mv_vlc;
static VLC mbincr_vlc;
static VLC mb_ptype_vlc;
static VLC mb_btype_vlc;
static VLC mb_pat_vlc;

Falk Hüffner's avatar
Falk Hüffner committed
152
static void init_vlcs(void)
Fabrice Bellard's avatar
Fabrice Bellard committed
153 154 155 156
{
    static int done = 0;

    if (!done) {
157
        done = 1;
Fabrice Bellard's avatar
Fabrice Bellard committed
158

159
        init_vlc(&dc_lum_vlc, DC_VLC_BITS, 12,
160 161
                 ff_mpeg12_vlc_dc_lum_bits, 1, 1,
                 ff_mpeg12_vlc_dc_lum_code, 2, 2, 1);
162
        init_vlc(&dc_chroma_vlc,  DC_VLC_BITS, 12,
163 164
                 ff_mpeg12_vlc_dc_chroma_bits, 1, 1,
                 ff_mpeg12_vlc_dc_chroma_code, 2, 2, 1);
165
        init_vlc(&mv_vlc, MV_VLC_BITS, 17,
166 167
                 &ff_mpeg12_mbMotionVectorTable[0][1], 2, 1,
                 &ff_mpeg12_mbMotionVectorTable[0][0], 2, 1, 1);
168
        init_vlc(&mbincr_vlc, MBINCR_VLC_BITS, 36,
169 170
                 &ff_mpeg12_mbAddrIncrTable[0][1], 2, 1,
                 &ff_mpeg12_mbAddrIncrTable[0][0], 2, 1, 1);
171
        init_vlc(&mb_pat_vlc, MB_PAT_VLC_BITS, 64,
172 173
                 &ff_mpeg12_mbPatTable[0][1], 2, 1,
                 &ff_mpeg12_mbPatTable[0][0], 2, 1, 1);
174 175

        init_vlc(&mb_ptype_vlc, MB_PTYPE_VLC_BITS, 7,
Fabrice Bellard's avatar
Fabrice Bellard committed
176
                 &table_mb_ptype[0][1], 2, 1,
177
                 &table_mb_ptype[0][0], 2, 1, 1);
178
        init_vlc(&mb_btype_vlc, MB_BTYPE_VLC_BITS, 11,
Fabrice Bellard's avatar
Fabrice Bellard committed
179
                 &table_mb_btype[0][1], 2, 1,
180
                 &table_mb_btype[0][0], 2, 1, 1);
181 182
        init_rl(&ff_rl_mpeg1, ff_mpeg12_static_rl_table_store[0]);
        init_rl(&ff_rl_mpeg2, ff_mpeg12_static_rl_table_store[1]);
183

184 185
        init_2d_vlc_rl(&ff_rl_mpeg1, 1);
        init_2d_vlc_rl(&ff_rl_mpeg2, 1);
Fabrice Bellard's avatar
Fabrice Bellard committed
186 187 188 189 190
    }
}

static inline int get_dmv(MpegEncContext *s)
{
191
    if(get_bits1(&s->gb))
192
        return 1 - (get_bits1(&s->gb) << 1);
Fabrice Bellard's avatar
Fabrice Bellard committed
193 194 195 196
    else
        return 0;
}

197 198
static inline int get_qscale(MpegEncContext *s)
{
199
    int qscale = get_bits(&s->gb, 5);
Michael Niedermayer's avatar
Michael Niedermayer committed
200 201 202 203
    if (s->q_scale_type) {
        return non_linear_qscale[qscale];
    } else {
        return qscale << 1;
204 205 206
    }
}

Fabrice Bellard's avatar
Fabrice Bellard committed
207 208 209 210 211 212 213
/* motion type (for mpeg2) */
#define MT_FIELD 1
#define MT_FRAME 2
#define MT_16X8  2
#define MT_DMV   3

static int mpeg_decode_mb(MpegEncContext *s,
214
                          DCTELEM block[12][64])
Fabrice Bellard's avatar
Fabrice Bellard committed
215
{
Michael Niedermayer's avatar
Michael Niedermayer committed
216
    int i, j, k, cbp, val, mb_type, motion_type;
217
    const int mb_block_count = 4 + (1<< s->chroma_format);
218

219
    dprintf(s->avctx, "decode_mb: x=%d y=%d\n", s->mb_x, s->mb_y);
Fabrice Bellard's avatar
Fabrice Bellard committed
220

221
    assert(s->mb_skipped==0);
222

223
    if (s->mb_skip_run-- != 0) {
Fabrice Bellard's avatar
Fabrice Bellard committed
224
        if (s->pict_type == P_TYPE) {
225
            s->mb_skipped = 1;
226
            s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]= MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
Fabrice Bellard's avatar
Fabrice Bellard committed
227
        } else {
228
            int mb_type;
229

230 231 232
            if(s->mb_x)
                mb_type= s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride - 1];
            else
233
                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,
234 235
            if(IS_INTRA(mb_type))
                return -1;
236 237

            s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]=
238
                mb_type | MB_TYPE_SKIP;
239 240
//            assert(s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride - 1]&(MB_TYPE_16x16|MB_TYPE_16x8));

241
            if((s->mv[0][0][0]|s->mv[0][0][1]|s->mv[1][0][0]|s->mv[1][0][1])==0)
242
                s->mb_skipped = 1;
Fabrice Bellard's avatar
Fabrice Bellard committed
243
        }
Michael Niedermayer's avatar
Michael Niedermayer committed
244

Fabrice Bellard's avatar
Fabrice Bellard committed
245 246 247 248 249 250
        return 0;
    }

    switch(s->pict_type) {
    default:
    case I_TYPE:
251
        if (get_bits1(&s->gb) == 0) {
252
            if (get_bits1(&s->gb) == 0){
253
                av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in I Frame at %d %d\n", s->mb_x, s->mb_y);
Fabrice Bellard's avatar
Fabrice Bellard committed
254
                return -1;
255
            }
256
            mb_type = MB_TYPE_QUANT | MB_TYPE_INTRA;
Fabrice Bellard's avatar
Fabrice Bellard committed
257
        } else {
258
            mb_type = MB_TYPE_INTRA;
Fabrice Bellard's avatar
Fabrice Bellard committed
259 260 261
        }
        break;
    case P_TYPE:
262
        mb_type = get_vlc2(&s->gb, mb_ptype_vlc.table, MB_PTYPE_VLC_BITS, 1);
263
        if (mb_type < 0){
264
            av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in P Frame at %d %d\n", s->mb_x, s->mb_y);
Fabrice Bellard's avatar
Fabrice Bellard committed
265
            return -1;
266
        }
267
        mb_type = ptype2mb_type[ mb_type ];
Fabrice Bellard's avatar
Fabrice Bellard committed
268 269
        break;
    case B_TYPE:
270
        mb_type = get_vlc2(&s->gb, mb_btype_vlc.table, MB_BTYPE_VLC_BITS, 1);
271
        if (mb_type < 0){
272
            av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in B Frame at %d %d\n", s->mb_x, s->mb_y);
Fabrice Bellard's avatar
Fabrice Bellard committed
273
            return -1;
274
        }
275
        mb_type = btype2mb_type[ mb_type ];
Fabrice Bellard's avatar
Fabrice Bellard committed
276 277
        break;
    }
278
    dprintf(s->avctx, "mb_type=%x\n", mb_type);
279 280
//    motion_type = 0; /* avoid warning */
    if (IS_INTRA(mb_type)) {
281
        s->dsp.clear_blocks(s->block[0]);
282

283 284 285
        if(!s->chroma_y_shift){
            s->dsp.clear_blocks(s->block[6]);
        }
286

287 288 289 290 291
        /* compute dct type */
        if (s->picture_structure == PICT_FRAME && //FIXME add a interlaced_dct coded var?
            !s->frame_pred_frame_dct) {
            s->interlaced_dct = get_bits1(&s->gb);
        }
Fabrice Bellard's avatar
Fabrice Bellard committed
292

293 294
        if (IS_QUANT(mb_type))
            s->qscale = get_qscale(s);
295

Fabrice Bellard's avatar
Fabrice Bellard committed
296 297
        if (s->concealment_motion_vectors) {
            /* just parse them */
298
            if (s->picture_structure != PICT_FRAME)
299
                skip_bits1(&s->gb); /* field select */
300 301

            s->mv[0][0][0]= s->last_mv[0][0][0]= s->last_mv[0][1][0] =
302
                mpeg_decode_motion(s, s->mpeg_f_code[0][0], s->last_mv[0][0][0]);
303
            s->mv[0][0][1]= s->last_mv[0][0][1]= s->last_mv[0][1][1] =
304 305
                mpeg_decode_motion(s, s->mpeg_f_code[0][1], s->last_mv[0][0][1]);

306
            skip_bits1(&s->gb); /* marker */
307 308
        }else
            memset(s->last_mv, 0, sizeof(s->last_mv)); /* reset mv prediction */
Fabrice Bellard's avatar
Fabrice Bellard committed
309
        s->mb_intra = 1;
310 311 312 313 314 315 316 317 318
#ifdef HAVE_XVMC
        //one 1 we memcpy blocks in xvmcvideo
        if(s->avctx->xvmc_acceleration > 1){
            XVMC_pack_pblocks(s,-1);//inter are always full blocks
            if(s->swap_uv){
                exchange_uv(s);
            }
        }
#endif
319

Michael Niedermayer's avatar
Michael Niedermayer committed
320
        if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
321 322 323 324 325 326 327 328 329
            if(s->flags2 & CODEC_FLAG2_FAST){
                for(i=0;i<6;i++) {
                    mpeg2_fast_decode_block_intra(s, s->pblocks[i], i);
                }
            }else{
                for(i=0;i<mb_block_count;i++) {
                    if (mpeg2_decode_block_intra(s, s->pblocks[i], i) < 0)
                        return -1;
                }
330 331 332
            }
        } else {
            for(i=0;i<6;i++) {
333
                if (mpeg1_decode_block_intra(s, s->pblocks[i], i) < 0)
334 335 336
                    return -1;
            }
        }
Fabrice Bellard's avatar
Fabrice Bellard committed
337
    } else {
338
        if (mb_type & MB_TYPE_ZERO_MV){
339
            assert(mb_type & MB_TYPE_CBP);
340 341

            s->mv_dir = MV_DIR_FORWARD;
Michael Niedermayer's avatar
Michael Niedermayer committed
342 343 344
            if(s->picture_structure == PICT_FRAME){
                if(!s->frame_pred_frame_dct)
                    s->interlaced_dct = get_bits1(&s->gb);
345
                s->mv_type = MV_TYPE_16X16;
Michael Niedermayer's avatar
Michael Niedermayer committed
346
            }else{
347 348 349 350
                s->mv_type = MV_TYPE_FIELD;
                mb_type |= MB_TYPE_INTERLACED;
                s->field_select[0][0]= s->picture_structure - 1;
            }
Michael Niedermayer's avatar
Michael Niedermayer committed
351 352 353 354

            if (IS_QUANT(mb_type))
                s->qscale = get_qscale(s);

355 356 357 358 359 360 361 362 363
            s->last_mv[0][0][0] = 0;
            s->last_mv[0][0][1] = 0;
            s->last_mv[0][1][0] = 0;
            s->last_mv[0][1][1] = 0;
            s->mv[0][0][0] = 0;
            s->mv[0][0][1] = 0;
        }else{
            assert(mb_type & MB_TYPE_L0L1);
//FIXME decide if MBs in field pictures are MB_TYPE_INTERLACED
Diego Biurrun's avatar
Diego Biurrun committed
364
            /* get additional motion vector type */
365
            if (s->frame_pred_frame_dct)
366 367 368
                motion_type = MT_FRAME;
            else{
                motion_type = get_bits(&s->gb, 2);
369 370
                if (s->picture_structure == PICT_FRAME && HAS_CBP(mb_type))
                    s->interlaced_dct = get_bits1(&s->gb);
371 372 373 374 375 376
            }

            if (IS_QUANT(mb_type))
                s->qscale = get_qscale(s);

            /* motion vectors */
377
            s->mv_dir= (mb_type>>13)&3;
Michael Niedermayer's avatar
Michael Niedermayer committed
378 379 380
            dprintf(s->avctx, "motion_type=%d\n", motion_type);
            switch(motion_type) {
            case MT_FRAME: /* or MT_16X8 */
381 382 383 384 385
                if (s->picture_structure == PICT_FRAME) {
                    mb_type |= MB_TYPE_16x16;
                    s->mv_type = MV_TYPE_16X16;
                    for(i=0;i<2;i++) {
                        if (USES_LIST(mb_type, i)) {
386
                            /* MT_FRAME */
387
                            s->mv[i][0][0]= s->last_mv[i][0][0]= s->last_mv[i][1][0] =
388
                                mpeg_decode_motion(s, s->mpeg_f_code[i][0], s->last_mv[i][0][0]);
389
                            s->mv[i][0][1]= s->last_mv[i][0][1]= s->last_mv[i][1][1] =
390
                                mpeg_decode_motion(s, s->mpeg_f_code[i][1], s->last_mv[i][0][1]);
Fabrice Bellard's avatar
Fabrice Bellard committed
391
                            /* full_pel: only for mpeg1 */
392 393 394 395
                            if (s->full_pel[i]){
                                s->mv[i][0][0] <<= 1;
                                s->mv[i][0][1] <<= 1;
                            }
396 397 398 399 400 401 402
                        }
                    }
                } else {
                    mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
                    s->mv_type = MV_TYPE_16X8;
                    for(i=0;i<2;i++) {
                        if (USES_LIST(mb_type, i)) {
403 404 405 406 407 408 409 410 411 412
                            /* MT_16X8 */
                            for(j=0;j<2;j++) {
                                s->field_select[i][j] = get_bits1(&s->gb);
                                for(k=0;k<2;k++) {
                                    val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
                                                             s->last_mv[i][j][k]);
                                    s->last_mv[i][j][k] = val;
                                    s->mv[i][j][k] = val;
                                }
                            }
Fabrice Bellard's avatar
Fabrice Bellard committed
413
                        }
Michael Niedermayer's avatar
Michael Niedermayer committed
414 415 416 417 418
                    }
                }
                break;
            case MT_FIELD:
                s->mv_type = MV_TYPE_FIELD;
419 420 421 422
                if (s->picture_structure == PICT_FRAME) {
                    mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
                    for(i=0;i<2;i++) {
                        if (USES_LIST(mb_type, i)) {
423 424 425 426 427 428
                            for(j=0;j<2;j++) {
                                s->field_select[i][j] = get_bits1(&s->gb);
                                val = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
                                                         s->last_mv[i][j][0]);
                                s->last_mv[i][j][0] = val;
                                s->mv[i][j][0] = val;
429
                                dprintf(s->avctx, "fmx=%d\n", val);
430 431 432 433
                                val = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
                                                         s->last_mv[i][j][1] >> 1);
                                s->last_mv[i][j][1] = val << 1;
                                s->mv[i][j][1] = val;
434
                                dprintf(s->avctx, "fmy=%d\n", val);
435
                            }
436 437 438 439 440 441
                        }
                    }
                } else {
                    mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
                    for(i=0;i<2;i++) {
                        if (USES_LIST(mb_type, i)) {
442
                            s->field_select[i][0] = get_bits1(&s->gb);
Fabrice Bellard's avatar
Fabrice Bellard committed
443 444
                            for(k=0;k<2;k++) {
                                val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
445 446 447 448
                                                         s->last_mv[i][0][k]);
                                s->last_mv[i][0][k] = val;
                                s->last_mv[i][1][k] = val;
                                s->mv[i][0][k] = val;
Fabrice Bellard's avatar
Fabrice Bellard committed
449 450
                            }
                        }
Michael Niedermayer's avatar
Michael Niedermayer committed
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
                    }
                }
                break;
            case MT_DMV:
                s->mv_type = MV_TYPE_DMV;
                for(i=0;i<2;i++) {
                    if (USES_LIST(mb_type, i)) {
                        int dmx, dmy, mx, my, m;
                        mx = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
                                                s->last_mv[i][0][0]);
                        s->last_mv[i][0][0] = mx;
                        s->last_mv[i][1][0] = mx;
                        dmx = get_dmv(s);
                        my = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
                                                s->last_mv[i][0][1] >> 1);
                        dmy = get_dmv(s);


                        s->last_mv[i][0][1] = my<<1;
                        s->last_mv[i][1][1] = my<<1;

                        s->mv[i][0][0] = mx;
                        s->mv[i][0][1] = my;
                        s->mv[i][1][0] = mx;//not used
                        s->mv[i][1][1] = my;//not used

                        if (s->picture_structure == PICT_FRAME) {
                            mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;

                            //m = 1 + 2 * s->top_field_first;
                            m = s->top_field_first ? 1 : 3;

                            /* top -> top pred */
                            s->mv[i][2][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
                            s->mv[i][2][1] = ((my * m + (my > 0)) >> 1) + dmy - 1;
                            m = 4 - m;
                            s->mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
                            s->mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1;
                        } else {
                            mb_type |= MB_TYPE_16x16;

                            s->mv[i][2][0] = ((mx + (mx > 0)) >> 1) + dmx;
                            s->mv[i][2][1] = ((my + (my > 0)) >> 1) + dmy;
                            if(s->picture_structure == PICT_TOP_FIELD)
                                s->mv[i][2][1]--;
                            else
                                s->mv[i][2][1]++;
Fabrice Bellard's avatar
Fabrice Bellard committed
498 499 500
                        }
                    }
                }
Michael Niedermayer's avatar
Michael Niedermayer committed
501 502 503 504
                break;
            default:
                av_log(s->avctx, AV_LOG_ERROR, "00 motion_type at %d %d\n", s->mb_x, s->mb_y);
                return -1;
Fabrice Bellard's avatar
Fabrice Bellard committed
505 506
            }
        }
507

508
        s->mb_intra = 0;
509
        if (HAS_CBP(mb_type)) {
510
            s->dsp.clear_blocks(s->block[0]);
511

512
            cbp = get_vlc2(&s->gb, mb_pat_vlc.table, MB_PAT_VLC_BITS, 1);
513
            if(mb_block_count > 6){
514 515
                 cbp<<= mb_block_count-6;
                 cbp |= get_bits(&s->gb, mb_block_count-6);
516
                 s->dsp.clear_blocks(s->block[6]);
517
            }
518 519 520 521
            if (cbp <= 0){
                av_log(s->avctx, AV_LOG_ERROR, "invalid cbp at %d %d\n", s->mb_x, s->mb_y);
                return -1;
            }
522

523 524 525 526 527 528 529
#ifdef HAVE_XVMC
            //on 1 we memcpy blocks in xvmcvideo
            if(s->avctx->xvmc_acceleration > 1){
                XVMC_pack_pblocks(s,cbp);
                if(s->swap_uv){
                    exchange_uv(s);
                }
530
            }
531 532
#endif

Michael Niedermayer's avatar
Michael Niedermayer committed
533
            if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
534 535 536 537 538 539 540 541 542 543 544
                if(s->flags2 & CODEC_FLAG2_FAST){
                    for(i=0;i<6;i++) {
                        if(cbp & 32) {
                            mpeg2_fast_decode_block_non_intra(s, s->pblocks[i], i);
                        } else {
                            s->block_last_index[i] = -1;
                        }
                        cbp+=cbp;
                    }
                }else{
                    cbp<<= 12-mb_block_count;
545

546 547 548 549 550 551 552 553
                    for(i=0;i<mb_block_count;i++) {
                        if ( cbp & (1<<11) ) {
                            if (mpeg2_decode_block_non_intra(s, s->pblocks[i], i) < 0)
                                return -1;
                        } else {
                            s->block_last_index[i] = -1;
                        }
                        cbp+=cbp;
554
                    }
Fabrice Bellard's avatar
Fabrice Bellard committed
555
                }
556
            } else {
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
                if(s->flags2 & CODEC_FLAG2_FAST){
                    for(i=0;i<6;i++) {
                        if (cbp & 32) {
                            mpeg1_fast_decode_block_inter(s, s->pblocks[i], i);
                        } else {
                            s->block_last_index[i] = -1;
                        }
                        cbp+=cbp;
                    }
                }else{
                    for(i=0;i<6;i++) {
                        if (cbp & 32) {
                            if (mpeg1_decode_block_inter(s, s->pblocks[i], i) < 0)
                                return -1;
                        } else {
                            s->block_last_index[i] = -1;
                        }
                        cbp+=cbp;
575
                    }
576
                }
Fabrice Bellard's avatar
Fabrice Bellard committed
577
            }
578
        }else{
579
            for(i=0;i<12;i++)
580
                s->block_last_index[i] = -1;
Fabrice Bellard's avatar
Fabrice Bellard committed
581 582
        }
    }
583 584 585

    s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]= mb_type;

Fabrice Bellard's avatar
Fabrice Bellard committed
586 587 588 589 590 591
    return 0;
}

/* as h263, but only 17 codes */
static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred)
{
592
    int code, sign, val, l, shift;
Fabrice Bellard's avatar
Fabrice Bellard committed
593

594
    code = get_vlc2(&s->gb, mv_vlc.table, MV_VLC_BITS, 2);
Fabrice Bellard's avatar
Fabrice Bellard committed
595 596 597
    if (code == 0) {
        return pred;
    }
598 599 600 601
    if (code < 0) {
        return 0xffff;
    }

602
    sign = get_bits1(&s->gb);
Fabrice Bellard's avatar
Fabrice Bellard committed
603
    shift = fcode - 1;
604 605 606
    val = code;
    if (shift) {
        val = (val - 1) << shift;
Fabrice Bellard's avatar
Fabrice Bellard committed
607
        val |= get_bits(&s->gb, shift);
608 609
        val++;
    }
Fabrice Bellard's avatar
Fabrice Bellard committed
610 611 612
    if (sign)
        val = -val;
    val += pred;
613

Fabrice Bellard's avatar
Fabrice Bellard committed
614
    /* modulo decoding */
Michael Niedermayer's avatar
Michael Niedermayer committed
615 616
    l= INT_BIT - 5 - shift;
    val = (val<<l)>>l;
Fabrice Bellard's avatar
Fabrice Bellard committed
617 618 619
    return val;
}

620
static inline int decode_dc(GetBitContext *gb, int component)
Fabrice Bellard's avatar
Fabrice Bellard committed
621 622 623 624
{
    int code, diff;

    if (component == 0) {
625
        code = get_vlc2(gb, dc_lum_vlc.table, DC_VLC_BITS, 2);
Fabrice Bellard's avatar
Fabrice Bellard committed
626
    } else {
627
        code = get_vlc2(gb, dc_chroma_vlc.table, DC_VLC_BITS, 2);
Fabrice Bellard's avatar
Fabrice Bellard committed
628
    }
629
    if (code < 0){
630
        av_log(NULL, AV_LOG_ERROR, "invalid dc code at\n");
Fabrice Bellard's avatar
Fabrice Bellard committed
631
        return 0xffff;
632
    }
Fabrice Bellard's avatar
Fabrice Bellard committed
633 634 635
    if (code == 0) {
        diff = 0;
    } else {
636
        diff = get_xbits(gb, code);
Fabrice Bellard's avatar
Fabrice Bellard committed
637 638 639 640
    }
    return diff;
}

641 642
static inline int mpeg1_decode_block_intra(MpegEncContext *s,
                               DCTELEM *block,
Fabrice Bellard's avatar
Fabrice Bellard committed
643 644 645
                               int n)
{
    int level, dc, diff, i, j, run;
646
    int component;
647
    RLTable *rl = &ff_rl_mpeg1;
648 649
    uint8_t * const scantable= s->intra_scantable.permutated;
    const uint16_t *quant_matrix= s->intra_matrix;
650
    const int qscale= s->qscale;
Fabrice Bellard's avatar
Fabrice Bellard committed
651

652 653
    /* DC coef */
    component = (n <= 3 ? 0 : n - 4 + 1);
654
    diff = decode_dc(&s->gb, component);
655 656 657 658 659 660
    if (diff >= 0xffff)
        return -1;
    dc = s->last_dc[component];
    dc += diff;
    s->last_dc[component] = dc;
    block[0] = dc<<3;
661
    dprintf(s->avctx, "dc=%d diff=%d\n", dc, diff);
662 663
    i = 0;
    {
664
        OPEN_READER(re, &s->gb);
665 666 667
        /* now quantify & encode AC coefs */
        for(;;) {
            UPDATE_CACHE(re, &s->gb);
668
            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
669

670 671 672 673 674
            if(level == 127){
                break;
            } else if(level != 0) {
                i += run;
                j = scantable[i];
Michael Niedermayer's avatar
Michael Niedermayer committed
675
                level= (level*qscale*quant_matrix[j])>>4;
676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
                level= (level-1)|1;
                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
                LAST_SKIP_BITS(re, &s->gb, 1);
            } else {
                /* escape */
                run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
                UPDATE_CACHE(re, &s->gb);
                level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
                if (level == -128) {
                    level = SHOW_UBITS(re, &s->gb, 8) - 256; LAST_SKIP_BITS(re, &s->gb, 8);
                } else if (level == 0) {
                    level = SHOW_UBITS(re, &s->gb, 8)      ; LAST_SKIP_BITS(re, &s->gb, 8);
                }
                i += run;
                j = scantable[i];
                if(level<0){
                    level= -level;
Michael Niedermayer's avatar
Michael Niedermayer committed
693
                    level= (level*qscale*quant_matrix[j])>>4;
694 695 696
                    level= (level-1)|1;
                    level= -level;
                }else{
Michael Niedermayer's avatar
Michael Niedermayer committed
697
                    level= (level*qscale*quant_matrix[j])>>4;
698 699 700 701
                    level= (level-1)|1;
                }
            }
            if (i > 63){
702
                av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
703 704 705 706 707 708 709 710 711 712 713
                return -1;
            }

            block[j] = level;
        }
        CLOSE_READER(re, &s->gb);
    }
    s->block_last_index[n] = i;
   return 0;
}

714 715
static inline int mpeg1_decode_block_inter(MpegEncContext *s,
                               DCTELEM *block,
716 717 718
                               int n)
{
    int level, i, j, run;
719
    RLTable *rl = &ff_rl_mpeg1;
720 721
    uint8_t * const scantable= s->intra_scantable.permutated;
    const uint16_t *quant_matrix= s->inter_matrix;
722 723 724
    const int qscale= s->qscale;

    {
725
        OPEN_READER(re, &s->gb);
726
        i = -1;
Fabrice Bellard's avatar
Fabrice Bellard committed
727
        /* special case for the first coef. no need to add a second vlc table */
728
        UPDATE_CACHE(re, &s->gb);
729
        if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
Michael Niedermayer's avatar
Michael Niedermayer committed
730
            level= (3*qscale*quant_matrix[0])>>5;
731
            level= (level-1)|1;
732
            if(GET_CACHE(re, &s->gb)&0x40000000)
733
                level= -level;
734
            block[0] = level;
735
            i++;
736 737 738
            SKIP_BITS(re, &s->gb, 2);
            if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
                goto end;
Fabrice Bellard's avatar
Fabrice Bellard committed
739
        }
740 741 742
#if MIN_CACHE_BITS < 19
        UPDATE_CACHE(re, &s->gb);
#endif
743 744
        /* now quantify & encode AC coefs */
        for(;;) {
745
            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
746

747
            if(level != 0) {
748 749
                i += run;
                j = scantable[i];
Michael Niedermayer's avatar
Michael Niedermayer committed
750
                level= ((level*2+1)*qscale*quant_matrix[j])>>5;
751 752
                level= (level-1)|1;
                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
753
                SKIP_BITS(re, &s->gb, 1);
754 755 756 757 758 759
            } else {
                /* escape */
                run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
                UPDATE_CACHE(re, &s->gb);
                level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
                if (level == -128) {
760
                    level = SHOW_UBITS(re, &s->gb, 8) - 256; SKIP_BITS(re, &s->gb, 8);
761
                } else if (level == 0) {
762
                    level = SHOW_UBITS(re, &s->gb, 8)      ; SKIP_BITS(re, &s->gb, 8);
763 764 765 766 767
                }
                i += run;
                j = scantable[i];
                if(level<0){
                    level= -level;
Michael Niedermayer's avatar
Michael Niedermayer committed
768
                    level= ((level*2+1)*qscale*quant_matrix[j])>>5;
769 770 771
                    level= (level-1)|1;
                    level= -level;
                }else{
Michael Niedermayer's avatar
Michael Niedermayer committed
772
                    level= ((level*2+1)*qscale*quant_matrix[j])>>5;
773 774
                    level= (level-1)|1;
                }
Fabrice Bellard's avatar
Fabrice Bellard committed
775
            }
776
            if (i > 63){
777
                av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
778 779 780 781
                return -1;
            }

            block[j] = level;
782 783 784
#if MIN_CACHE_BITS < 19
            UPDATE_CACHE(re, &s->gb);
#endif
785 786
            if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
                break;
787
#if MIN_CACHE_BITS >= 19
788
            UPDATE_CACHE(re, &s->gb);
789
#endif
Fabrice Bellard's avatar
Fabrice Bellard committed
790
        }
791 792
end:
        LAST_SKIP_BITS(re, &s->gb, 2);
793
        CLOSE_READER(re, &s->gb);
Fabrice Bellard's avatar
Fabrice Bellard committed
794
    }
795
    s->block_last_index[n] = i;
Fabrice Bellard's avatar
Fabrice Bellard committed
796 797 798
    return 0;
}

799 800 801
static inline int mpeg1_fast_decode_block_inter(MpegEncContext *s, DCTELEM *block, int n)
{
    int level, i, j, run;
802
    RLTable *rl = &ff_rl_mpeg1;
803 804 805 806 807 808 809 810
    uint8_t * const scantable= s->intra_scantable.permutated;
    const int qscale= s->qscale;

    {
        OPEN_READER(re, &s->gb);
        i = -1;
        /* special case for the first coef. no need to add a second vlc table */
        UPDATE_CACHE(re, &s->gb);
811
        if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
Michael Niedermayer's avatar
Michael Niedermayer committed
812
            level= (3*qscale)>>1;
813
            level= (level-1)|1;
814
            if(GET_CACHE(re, &s->gb)&0x40000000)
815 816 817
                level= -level;
            block[0] = level;
            i++;
818 819 820
            SKIP_BITS(re, &s->gb, 2);
            if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
                goto end;
821
        }
822 823 824
#if MIN_CACHE_BITS < 19
        UPDATE_CACHE(re, &s->gb);
#endif
825 826 827

        /* now quantify & encode AC coefs */
        for(;;) {
828
            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
829

830
            if(level != 0) {
831 832 833 834 835
                i += run;
                j = scantable[i];
                level= ((level*2+1)*qscale)>>1;
                level= (level-1)|1;
                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
836
                SKIP_BITS(re, &s->gb, 1);
837 838 839 840 841 842
            } else {
                /* escape */
                run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
                UPDATE_CACHE(re, &s->gb);
                level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
                if (level == -128) {
843
                    level = SHOW_UBITS(re, &s->gb, 8) - 256; SKIP_BITS(re, &s->gb, 8);
844
                } else if (level == 0) {
845
                    level = SHOW_UBITS(re, &s->gb, 8)      ; SKIP_BITS(re, &s->gb, 8);
846 847 848 849 850 851 852 853 854 855 856 857 858 859 860
                }
                i += run;
                j = scantable[i];
                if(level<0){
                    level= -level;
                    level= ((level*2+1)*qscale)>>1;
                    level= (level-1)|1;
                    level= -level;
                }else{
                    level= ((level*2+1)*qscale)>>1;
                    level= (level-1)|1;
                }
            }

            block[j] = level;
861 862 863
#if MIN_CACHE_BITS < 19
            UPDATE_CACHE(re, &s->gb);
#endif
864 865
            if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
                break;
866
#if MIN_CACHE_BITS >= 19
867
            UPDATE_CACHE(re, &s->gb);
868
#endif
869
        }
870 871
end:
        LAST_SKIP_BITS(re, &s->gb, 2);
872 873 874 875 876 877 878
        CLOSE_READER(re, &s->gb);
    }
    s->block_last_index[n] = i;
    return 0;
}


879 880
static inline int mpeg2_decode_block_non_intra(MpegEncContext *s,
                               DCTELEM *block,
881
                               int n)
Fabrice Bellard's avatar
Fabrice Bellard committed
882 883
{
    int level, i, j, run;
884
    RLTable *rl = &ff_rl_mpeg1;
885 886
    uint8_t * const scantable= s->intra_scantable.permutated;
    const uint16_t *quant_matrix;
887
    const int qscale= s->qscale;
Fabrice Bellard's avatar
Fabrice Bellard committed
888 889 890 891 892
    int mismatch;

    mismatch = 1;

    {
893
        OPEN_READER(re, &s->gb);
894
        i = -1;
895
        if (n < 4)
896
            quant_matrix = s->inter_matrix;
Fabrice Bellard's avatar
Fabrice Bellard committed
897
        else
898
            quant_matrix = s->chroma_inter_matrix;
899

Fabrice Bellard's avatar
Fabrice Bellard committed
900
        /* special case for the first coef. no need to add a second vlc table */
901
        UPDATE_CACHE(re, &s->gb);
902
        if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
903
            level= (3*qscale*quant_matrix[0])>>5;
904
            if(GET_CACHE(re, &s->gb)&0x40000000)
905 906 907 908
                level= -level;
            block[0] = level;
            mismatch ^= level;
            i++;
909 910 911
            SKIP_BITS(re, &s->gb, 2);
            if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
                goto end;
Fabrice Bellard's avatar
Fabrice Bellard committed
912
        }
913 914 915
#if MIN_CACHE_BITS < 19
        UPDATE_CACHE(re, &s->gb);
#endif
Fabrice Bellard's avatar
Fabrice Bellard committed
916

917 918
        /* now quantify & encode AC coefs */
        for(;;) {
919
            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
920

921
            if(level != 0) {
922 923 924 925
                i += run;
                j = scantable[i];
                level= ((level*2+1)*qscale*quant_matrix[j])>>5;
                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
926
                SKIP_BITS(re, &s->gb, 1);
927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942
            } else {
                /* escape */
                run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
                UPDATE_CACHE(re, &s->gb);
                level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);

                i += run;
                j = scantable[i];
                if(level<0){
                    level= ((-level*2+1)*qscale*quant_matrix[j])>>5;
                    level= -level;
                }else{
                    level= ((level*2+1)*qscale*quant_matrix[j])>>5;
                }
            }
            if (i > 63){
943
                av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
944 945
                return -1;
            }
946

947 948
            mismatch ^= level;
            block[j] = level;
949 950 951
#if MIN_CACHE_BITS < 19
            UPDATE_CACHE(re, &s->gb);
#endif
952 953
            if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
                break;
954
#if MIN_CACHE_BITS >= 19
955
            UPDATE_CACHE(re, &s->gb);
956
#endif
957
        }
958 959
end:
        LAST_SKIP_BITS(re, &s->gb, 2);
960
        CLOSE_READER(re, &s->gb);
Fabrice Bellard's avatar
Fabrice Bellard committed
961 962
    }
    block[63] ^= (mismatch & 1);
963

Fabrice Bellard's avatar
Fabrice Bellard committed
964 965 966 967
    s->block_last_index[n] = i;
    return 0;
}

968 969
static inline int mpeg2_fast_decode_block_non_intra(MpegEncContext *s,
                               DCTELEM *block,
970 971 972
                               int n)
{
    int level, i, j, run;
973
    RLTable *rl = &ff_rl_mpeg1;
974 975 976 977 978 979 980
    uint8_t * const scantable= s->intra_scantable.permutated;
    const int qscale= s->qscale;
    OPEN_READER(re, &s->gb);
    i = -1;

    /* special case for the first coef. no need to add a second vlc table */
    UPDATE_CACHE(re, &s->gb);
981
    if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
982
        level= (3*qscale)>>1;
983
        if(GET_CACHE(re, &s->gb)&0x40000000)
984 985 986
            level= -level;
        block[0] = level;
        i++;
987 988 989
        SKIP_BITS(re, &s->gb, 2);
        if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
            goto end;
990
    }
991 992 993
#if MIN_CACHE_BITS < 19
    UPDATE_CACHE(re, &s->gb);
#endif
994 995 996

    /* now quantify & encode AC coefs */
    for(;;) {
997
        GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
998

999
        if(level != 0) {
1000 1001 1002 1003
            i += run;
            j = scantable[i];
            level= ((level*2+1)*qscale)>>1;
            level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1004
            SKIP_BITS(re, &s->gb, 1);
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
        } else {
            /* escape */
            run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
            UPDATE_CACHE(re, &s->gb);
            level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);

            i += run;
            j = scantable[i];
            if(level<0){
                level= ((-level*2+1)*qscale)>>1;
                level= -level;
            }else{
                level= ((level*2+1)*qscale)>>1;
            }
        }
1020

1021
        block[j] = level;
1022 1023 1024
#if MIN_CACHE_BITS < 19
        UPDATE_CACHE(re, &s->gb);
#endif
1025 1026
        if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
            break;
1027
#if MIN_CACHE_BITS >=19
1028
        UPDATE_CACHE(re, &s->gb);
1029
#endif
1030
    }
1031
end:
1032
    LAST_SKIP_BITS(re, &s->gb, 2);
1033 1034 1035 1036 1037 1038
    CLOSE_READER(re, &s->gb);
    s->block_last_index[n] = i;
    return 0;
}


1039 1040
static inline int mpeg2_decode_block_intra(MpegEncContext *s,
                               DCTELEM *block,
1041
                               int n)
Fabrice Bellard's avatar
Fabrice Bellard committed
1042 1043
{
    int level, dc, diff, i, j, run;
1044
    int component;
Fabrice Bellard's avatar
Fabrice Bellard committed
1045
    RLTable *rl;
1046 1047
    uint8_t * const scantable= s->intra_scantable.permutated;
    const uint16_t *quant_matrix;
1048
    const int qscale= s->qscale;
Fabrice Bellard's avatar
Fabrice Bellard committed
1049 1050 1051
    int mismatch;

    /* DC coef */
1052 1053
    if (n < 4){
        quant_matrix = s->intra_matrix;
1054
        component = 0;
1055 1056
    }else{
        quant_matrix = s->chroma_intra_matrix;
1057
        component = (n&1) + 1;
1058
    }
1059
    diff = decode_dc(&s->gb, component);
Fabrice Bellard's avatar
Fabrice Bellard committed
1060 1061 1062 1063 1064 1065
    if (diff >= 0xffff)
        return -1;
    dc = s->last_dc[component];
    dc += diff;
    s->last_dc[component] = dc;
    block[0] = dc << (3 - s->intra_dc_precision);
1066
    dprintf(s->avctx, "dc=%d\n", block[0]);
1067
    mismatch = block[0] ^ 1;
1068
    i = 0;
Fabrice Bellard's avatar
Fabrice Bellard committed
1069
    if (s->intra_vlc_format)
1070
        rl = &ff_rl_mpeg2;
Fabrice Bellard's avatar
Fabrice Bellard committed
1071
    else
1072
        rl = &ff_rl_mpeg1;
1073

1074
    {
1075
        OPEN_READER(re, &s->gb);
1076 1077 1078
        /* now quantify & encode AC coefs */
        for(;;) {
            UPDATE_CACHE(re, &s->gb);
1079
            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
1080

1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
            if(level == 127){
                break;
            } else if(level != 0) {
                i += run;
                j = scantable[i];
                level= (level*qscale*quant_matrix[j])>>4;
                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
                LAST_SKIP_BITS(re, &s->gb, 1);
            } else {
                /* escape */
                run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
                UPDATE_CACHE(re, &s->gb);
                level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
                i += run;
                j = scantable[i];
                if(level<0){
                    level= (-level*qscale*quant_matrix[j])>>4;
                    level= -level;
                }else{
                    level= (level*qscale*quant_matrix[j])>>4;
                }
            }
            if (i > 63){
1104
                av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
1105 1106
                return -1;
            }
1107

1108 1109
            mismatch^= level;
            block[j] = level;
1110
        }
1111
        CLOSE_READER(re, &s->gb);
Fabrice Bellard's avatar
Fabrice Bellard committed
1112
    }
1113
    block[63]^= mismatch&1;
1114

Fabrice Bellard's avatar
Fabrice Bellard committed
1115 1116 1117 1118
    s->block_last_index[n] = i;
    return 0;
}

1119 1120
static inline int mpeg2_fast_decode_block_intra(MpegEncContext *s,
                               DCTELEM *block,
1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132
                               int n)
{
    int level, dc, diff, j, run;
    int component;
    RLTable *rl;
    uint8_t * scantable= s->intra_scantable.permutated;
    const uint16_t *quant_matrix;
    const int qscale= s->qscale;

    /* DC coef */
    if (n < 4){
        quant_matrix = s->intra_matrix;
1133
        component = 0;
1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145
    }else{
        quant_matrix = s->chroma_intra_matrix;
        component = (n&1) + 1;
    }
    diff = decode_dc(&s->gb, component);
    if (diff >= 0xffff)
        return -1;
    dc = s->last_dc[component];
    dc += diff;
    s->last_dc[component] = dc;
    block[0] = dc << (3 - s->intra_dc_precision);
    if (s->intra_vlc_format)
1146
        rl = &ff_rl_mpeg2;
1147
    else
1148
        rl = &ff_rl_mpeg1;
1149 1150

    {
1151
        OPEN_READER(re, &s->gb);
1152 1153 1154 1155
        /* now quantify & encode AC coefs */
        for(;;) {
            UPDATE_CACHE(re, &s->gb);
            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
1156

1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178
            if(level == 127){
                break;
            } else if(level != 0) {
                scantable += run;
                j = *scantable;
                level= (level*qscale*quant_matrix[j])>>4;
                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
                LAST_SKIP_BITS(re, &s->gb, 1);
            } else {
                /* escape */
                run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
                UPDATE_CACHE(re, &s->gb);
                level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
                scantable += run;
                j = *scantable;
                if(level<0){
                    level= (-level*qscale*quant_matrix[j])>>4;
                    level= -level;
                }else{
                    level= (level*qscale*quant_matrix[j])>>4;
                }
            }
1179

1180 1181 1182 1183
            block[j] = level;
        }
        CLOSE_READER(re, &s->gb);
    }
1184

1185 1186 1187 1188
    s->block_last_index[n] = scantable - s->intra_scantable.permutated;
    return 0;
}

Fabrice Bellard's avatar
Fabrice Bellard committed
1189 1190 1191
typedef struct Mpeg1Context {
    MpegEncContext mpeg_enc_ctx;
    int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
1192
    int repeat_field; /* true if we must repeat the field */
1193
    AVPanScan pan_scan; /** some temporary storage for the panscan */
1194
    int slice_count;
1195 1196
    int swap_uv;//indicate VCR2
    int save_aspect_info;
1197
    int save_width, save_height;
Michael Niedermayer's avatar
Michael Niedermayer committed
1198
    AVRational frame_rate_ext;       ///< MPEG-2 specific framerate modificator
1199

Fabrice Bellard's avatar
Fabrice Bellard committed
1200 1201 1202 1203 1204
} Mpeg1Context;

static int mpeg_decode_init(AVCodecContext *avctx)
{
    Mpeg1Context *s = avctx->priv_data;
Michael Niedermayer's avatar
Michael Niedermayer committed
1205
    MpegEncContext *s2 = &s->mpeg_enc_ctx;
Ivan Kalvachev's avatar
Ivan Kalvachev committed
1206
    int i;
1207

Ivan Kalvachev's avatar
Ivan Kalvachev committed
1208 1209
    //we need some parmutation to store
    //matrixes, until MPV_common_init()
1210
    //set the real permutatuon
Ivan Kalvachev's avatar
Ivan Kalvachev committed
1211 1212 1213
    for(i=0;i<64;i++)
       s2->dsp.idct_permutation[i]=i;

Michael Niedermayer's avatar
Michael Niedermayer committed
1214
    MPV_decode_defaults(s2);
1215

1216
    s->mpeg_enc_ctx.avctx= avctx;
Michael Niedermayer's avatar
Michael Niedermayer committed
1217
    s->mpeg_enc_ctx.flags= avctx->flags;
1218
    s->mpeg_enc_ctx.flags2= avctx->flags2;
1219
    ff_mpeg12_common_init(&s->mpeg_enc_ctx);
1220
    init_vlcs();
Fabrice Bellard's avatar
Fabrice Bellard committed
1221 1222 1223

    s->mpeg_enc_ctx_allocated = 0;
    s->mpeg_enc_ctx.picture_number = 0;
1224
    s->repeat_field = 0;
1225
    s->mpeg_enc_ctx.codec_id= avctx->codec->id;
Fabrice Bellard's avatar
Fabrice Bellard committed
1226 1227 1228
    return 0;
}

1229
static void quant_matrix_rebuild(uint16_t *matrix, const uint8_t *old_perm,
1230
                                     const uint8_t *new_perm){
Michael Niedermayer's avatar
Michael Niedermayer committed
1231 1232
    uint16_t temp_matrix[64];
    int i;
1233 1234

    memcpy(temp_matrix,matrix,64*sizeof(uint16_t));
1235

1236 1237
    for(i=0;i<64;i++){
        matrix[new_perm[i]] = temp_matrix[old_perm[i]];
1238
    }
1239 1240 1241 1242 1243
}

//Call this function when we know all parameters
//it may be called in different places for mpeg1 and mpeg2
static int mpeg_decode_postinit(AVCodecContext *avctx){
Michael Niedermayer's avatar
Michael Niedermayer committed
1244 1245 1246
    Mpeg1Context *s1 = avctx->priv_data;
    MpegEncContext *s = &s1->mpeg_enc_ctx;
    uint8_t old_permutation[64];
1247 1248

    if (
1249
        (s1->mpeg_enc_ctx_allocated == 0)||
1250 1251
        avctx->coded_width  != s->width ||
        avctx->coded_height != s->height||
1252 1253
        s1->save_width != s->width ||
        s1->save_height != s->height ||
1254
        s1->save_aspect_info != s->aspect_ratio_info||
1255 1256
        0)
    {
1257

1258
        if (s1->mpeg_enc_ctx_allocated) {
1259 1260
            ParseContext pc= s->parse_context;
            s->parse_context.buffer=0;
1261
            MPV_common_end(s);
1262
            s->parse_context= pc;
1263 1264
        }

1265 1266
        if( (s->width == 0 )||(s->height == 0))
            return -2;
1267

1268
        avcodec_set_dimensions(avctx, s->width, s->height);
1269 1270
        avctx->bit_rate = s->bit_rate;
        s1->save_aspect_info = s->aspect_ratio_info;
1271 1272
        s1->save_width = s->width;
        s1->save_height = s->height;
1273 1274 1275 1276 1277 1278 1279

     //low_delay may be forced, in this case we will have B frames
     //that behave like P frames
        avctx->has_b_frames = !(s->low_delay);

        if(avctx->sub_id==1){//s->codec_id==avctx->codec_id==CODEC_ID
            //mpeg1 fps
1280 1281
            avctx->time_base.den= ff_frame_rate_tab[s->frame_rate_index].num;
            avctx->time_base.num= ff_frame_rate_tab[s->frame_rate_index].den;
1282 1283
            //mpeg1 aspect
            avctx->sample_aspect_ratio= av_d2q(
1284
                    1.0/ff_mpeg1_aspect[s->aspect_ratio_info], 255);
1285 1286 1287 1288

        }else{//mpeg2
        //mpeg2 fps
            av_reduce(
1289 1290
                &s->avctx->time_base.den,
                &s->avctx->time_base.num,
1291 1292
                ff_frame_rate_tab[s->frame_rate_index].num * s1->frame_rate_ext.num,
                ff_frame_rate_tab[s->frame_rate_index].den * s1->frame_rate_ext.den,
1293 1294 1295
                1<<30);
        //mpeg2 aspect
            if(s->aspect_ratio_info > 1){
1296
                if( (s1->pan_scan.width == 0 )||(s1->pan_scan.height == 0) ){
1297
                    s->avctx->sample_aspect_ratio=
1298
                        av_div_q(
1299
                         ff_mpeg2_aspect[s->aspect_ratio_info],
1300 1301
                         (AVRational){s->width, s->height}
                         );
1302
                }else{
1303
                    s->avctx->sample_aspect_ratio=
1304
                        av_div_q(
1305
                         ff_mpeg2_aspect[s->aspect_ratio_info],
1306
                         (AVRational){s1->pan_scan.width, s1->pan_scan.height}
1307
                        );
1308
                }
1309
            }else{
1310
                s->avctx->sample_aspect_ratio=
1311
                    ff_mpeg2_aspect[s->aspect_ratio_info];
1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332
            }
        }//mpeg2

        if(avctx->xvmc_acceleration){
            avctx->pix_fmt = avctx->get_format(avctx,pixfmt_xvmc_mpg2_420);
        }else{
            if(s->chroma_format <  2){
                avctx->pix_fmt = avctx->get_format(avctx,pixfmt_yuv_420);
            }else
            if(s->chroma_format == 2){
                avctx->pix_fmt = avctx->get_format(avctx,pixfmt_yuv_422);
            }else
            if(s->chroma_format >  2){
                avctx->pix_fmt = avctx->get_format(avctx,pixfmt_yuv_444);
            }
        }
        //until then pix_fmt may be changed right after codec init
        if( avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT )
            if( avctx->idct_algo == FF_IDCT_AUTO )
                avctx->idct_algo = FF_IDCT_SIMPLE;

1333
        //quantization matrixes may need reordering
1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349
        //if dct permutation is changed
        memcpy(old_permutation,s->dsp.idct_permutation,64*sizeof(uint8_t));

        if (MPV_common_init(s) < 0)
            return -2;

        quant_matrix_rebuild(s->intra_matrix,       old_permutation,s->dsp.idct_permutation);
        quant_matrix_rebuild(s->inter_matrix,       old_permutation,s->dsp.idct_permutation);
        quant_matrix_rebuild(s->chroma_intra_matrix,old_permutation,s->dsp.idct_permutation);
        quant_matrix_rebuild(s->chroma_inter_matrix,old_permutation,s->dsp.idct_permutation);

        s1->mpeg_enc_ctx_allocated = 1;
    }
    return 0;
}

1350
static int mpeg1_decode_picture(AVCodecContext *avctx,
Zdenek Kabelac's avatar
Zdenek Kabelac committed
1351
                                const uint8_t *buf, int buf_size)
Fabrice Bellard's avatar
Fabrice Bellard committed
1352 1353 1354
{
    Mpeg1Context *s1 = avctx->priv_data;
    MpegEncContext *s = &s1->mpeg_enc_ctx;
Michael Niedermayer's avatar
Michael Niedermayer committed
1355
    int ref, f_code, vbv_delay;
Fabrice Bellard's avatar
Fabrice Bellard committed
1356

1357
    if(mpeg_decode_postinit(s->avctx) < 0)
1358 1359
       return -2;

1360
    init_get_bits(&s->gb, buf, buf_size*8);
Fabrice Bellard's avatar
Fabrice Bellard committed
1361 1362 1363

    ref = get_bits(&s->gb, 10); /* temporal ref */
    s->pict_type = get_bits(&s->gb, 3);
1364 1365
    if(s->pict_type == 0 || s->pict_type > 3)
        return -1;
Michael Niedermayer's avatar
Michael Niedermayer committed
1366

Michael Niedermayer's avatar
Michael Niedermayer committed
1367
    vbv_delay= get_bits(&s->gb, 16);
Fabrice Bellard's avatar
Fabrice Bellard committed
1368
    if (s->pict_type == P_TYPE || s->pict_type == B_TYPE) {
1369
        s->full_pel[0] = get_bits1(&s->gb);
Fabrice Bellard's avatar
Fabrice Bellard committed
1370
        f_code = get_bits(&s->gb, 3);
1371
        if (f_code == 0 && avctx->error_resilience >= FF_ER_COMPLIANT)
Fabrice Bellard's avatar
Fabrice Bellard committed
1372 1373 1374 1375 1376
            return -1;
        s->mpeg_f_code[0][0] = f_code;
        s->mpeg_f_code[0][1] = f_code;
    }
    if (s->pict_type == B_TYPE) {
1377
        s->full_pel[1] = get_bits1(&s->gb);
Fabrice Bellard's avatar
Fabrice Bellard committed
1378
        f_code = get_bits(&s->gb, 3);
1379
        if (f_code == 0 && avctx->error_resilience >= FF_ER_COMPLIANT)
Fabrice Bellard's avatar
Fabrice Bellard committed
1380 1381 1382 1383
            return -1;
        s->mpeg_f_code[1][0] = f_code;
        s->mpeg_f_code[1][1] = f_code;
    }
Michael Niedermayer's avatar
Michael Niedermayer committed
1384 1385
    s->current_picture.pict_type= s->pict_type;
    s->current_picture.key_frame= s->pict_type == I_TYPE;
1386

1387 1388
    if(avctx->debug & FF_DEBUG_PICT_INFO)
        av_log(avctx, AV_LOG_DEBUG, "vbv_delay %d, ref %d type:%d\n", vbv_delay, ref, s->pict_type);
1389

Fabrice Bellard's avatar
Fabrice Bellard committed
1390 1391 1392 1393 1394 1395
    s->y_dc_scale = 8;
    s->c_dc_scale = 8;
    s->first_slice = 1;
    return 0;
}

Michael Niedermayer's avatar
Michael Niedermayer committed
1396
static void mpeg_decode_sequence_extension(Mpeg1Context *s1)
Fabrice Bellard's avatar
Fabrice Bellard committed
1397
{
Michael Niedermayer's avatar
Michael Niedermayer committed
1398
    MpegEncContext *s= &s1->mpeg_enc_ctx;
Fabrice Bellard's avatar
Fabrice Bellard committed
1399
    int horiz_size_ext, vert_size_ext;
1400
    int bit_rate_ext;
Fabrice Bellard's avatar
Fabrice Bellard committed
1401

Michael Niedermayer's avatar
Michael Niedermayer committed
1402
    skip_bits(&s->gb, 1); /* profil and level esc*/
1403 1404
    s->avctx->profile= get_bits(&s->gb, 3);
    s->avctx->level= get_bits(&s->gb, 4);
1405
    s->progressive_sequence = get_bits1(&s->gb); /* progressive_sequence */
1406
    s->chroma_format = get_bits(&s->gb, 2); /* chroma_format 1=420, 2=422, 3=444 */
Fabrice Bellard's avatar
Fabrice Bellard committed
1407 1408 1409 1410 1411
    horiz_size_ext = get_bits(&s->gb, 2);
    vert_size_ext = get_bits(&s->gb, 2);
    s->width |= (horiz_size_ext << 12);
    s->height |= (vert_size_ext << 12);
    bit_rate_ext = get_bits(&s->gb, 12);  /* XXX: handle it */
Michael Niedermayer's avatar
Michael Niedermayer committed
1412
    s->bit_rate += (bit_rate_ext << 18) * 400;
1413
    skip_bits1(&s->gb); /* marker */
1414
    s->avctx->rc_buffer_size += get_bits(&s->gb, 8)*1024*16<<10;
1415

1416
    s->low_delay = get_bits1(&s->gb);
1417 1418
    if(s->flags & CODEC_FLAG_LOW_DELAY) s->low_delay=1;

Michael Niedermayer's avatar
Michael Niedermayer committed
1419 1420
    s1->frame_rate_ext.num = get_bits(&s->gb, 2)+1;
    s1->frame_rate_ext.den = get_bits(&s->gb, 5)+1;
1421

1422
    dprintf(s->avctx, "sequence extension\n");
Michael Niedermayer's avatar
Michael Niedermayer committed
1423
    s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG2VIDEO;
1424
    s->avctx->sub_id = 2; /* indicates mpeg2 found */
1425

Michael Niedermayer's avatar
Michael Niedermayer committed
1426
    if(s->avctx->debug & FF_DEBUG_PICT_INFO)
1427
        av_log(s->avctx, AV_LOG_DEBUG, "profile: %d, level: %d vbv buffer: %d, bitrate:%d\n",
1428
               s->avctx->profile, s->avctx->level, s->avctx->rc_buffer_size, s->bit_rate);
1429

Fabrice Bellard's avatar
Fabrice Bellard committed
1430 1431
}

1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447
static void mpeg_decode_sequence_display_extension(Mpeg1Context *s1)
{
    MpegEncContext *s= &s1->mpeg_enc_ctx;
    int color_description, w, h;

    skip_bits(&s->gb, 3); /* video format */
    color_description= get_bits1(&s->gb);
    if(color_description){
        skip_bits(&s->gb, 8); /* color primaries */
        skip_bits(&s->gb, 8); /* transfer_characteristics */
        skip_bits(&s->gb, 8); /* matrix_coefficients */
    }
    w= get_bits(&s->gb, 14);
    skip_bits(&s->gb, 1); //marker
    h= get_bits(&s->gb, 14);
    skip_bits(&s->gb, 1); //marker
1448

1449 1450
    s1->pan_scan.width= 16*w;
    s1->pan_scan.height=16*h;
1451

1452
    if(s->avctx->debug & FF_DEBUG_PICT_INFO)
1453
        av_log(s->avctx, AV_LOG_DEBUG, "sde w:%d, h:%d\n", w, h);
1454 1455 1456 1457 1458
}

static void mpeg_decode_picture_display_extension(Mpeg1Context *s1)
{
    MpegEncContext *s= &s1->mpeg_enc_ctx;
1459 1460 1461 1462 1463
    int i,nofco;

    nofco = 1;
    if(s->progressive_sequence){
        if(s->repeat_first_field){
1464 1465 1466 1467
            nofco++;
            if(s->top_field_first)
                nofco++;
        }
1468 1469 1470
    }else{
        if(s->picture_structure == PICT_FRAME){
            nofco++;
1471 1472 1473
            if(s->repeat_first_field)
                nofco++;
        }
1474 1475
    }
    for(i=0; i<nofco; i++){
1476 1477 1478 1479 1480
        s1->pan_scan.position[i][0]= get_sbits(&s->gb, 16);
        skip_bits(&s->gb, 1); //marker
        s1->pan_scan.position[i][1]= get_sbits(&s->gb, 16);
        skip_bits(&s->gb, 1); //marker
    }
1481

1482
    if(s->avctx->debug & FF_DEBUG_PICT_INFO)
1483 1484 1485
        av_log(s->avctx, AV_LOG_DEBUG, "pde (%d,%d) (%d,%d) (%d,%d)\n",
            s1->pan_scan.position[0][0], s1->pan_scan.position[0][1],
            s1->pan_scan.position[1][0], s1->pan_scan.position[1][1],
1486 1487 1488 1489
            s1->pan_scan.position[2][0], s1->pan_scan.position[2][1]
        );
}

Fabrice Bellard's avatar
Fabrice Bellard committed
1490 1491
static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
{
1492
    int i, v, j;
Fabrice Bellard's avatar
Fabrice Bellard committed
1493

1494
    dprintf(s->avctx, "matrix extension\n");
1495

1496
    if (get_bits1(&s->gb)) {
Fabrice Bellard's avatar
Fabrice Bellard committed
1497 1498
        for(i=0;i<64;i++) {
            v = get_bits(&s->gb, 8);
1499
            j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
1500 1501
            s->intra_matrix[j] = v;
            s->chroma_intra_matrix[j] = v;
Fabrice Bellard's avatar
Fabrice Bellard committed
1502 1503
        }
    }
1504
    if (get_bits1(&s->gb)) {
Fabrice Bellard's avatar
Fabrice Bellard committed
1505 1506
        for(i=0;i<64;i++) {
            v = get_bits(&s->gb, 8);
1507
            j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
1508 1509
            s->inter_matrix[j] = v;
            s->chroma_inter_matrix[j] = v;
Fabrice Bellard's avatar
Fabrice Bellard committed
1510 1511
        }
    }
1512
    if (get_bits1(&s->gb)) {
Fabrice Bellard's avatar
Fabrice Bellard committed
1513 1514
        for(i=0;i<64;i++) {
            v = get_bits(&s->gb, 8);
1515
            j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
1516
            s->chroma_intra_matrix[j] = v;
Fabrice Bellard's avatar
Fabrice Bellard committed
1517 1518
        }
    }
1519
    if (get_bits1(&s->gb)) {
Fabrice Bellard's avatar
Fabrice Bellard committed
1520 1521
        for(i=0;i<64;i++) {
            v = get_bits(&s->gb, 8);
1522
            j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
1523
            s->chroma_inter_matrix[j] = v;
Fabrice Bellard's avatar
Fabrice Bellard committed
1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536
        }
    }
}

static void mpeg_decode_picture_coding_extension(MpegEncContext *s)
{
    s->full_pel[0] = s->full_pel[1] = 0;
    s->mpeg_f_code[0][0] = get_bits(&s->gb, 4);
    s->mpeg_f_code[0][1] = get_bits(&s->gb, 4);
    s->mpeg_f_code[1][0] = get_bits(&s->gb, 4);
    s->mpeg_f_code[1][1] = get_bits(&s->gb, 4);
    s->intra_dc_precision = get_bits(&s->gb, 2);
    s->picture_structure = get_bits(&s->gb, 2);
1537 1538 1539 1540 1541 1542 1543 1544 1545
    s->top_field_first = get_bits1(&s->gb);
    s->frame_pred_frame_dct = get_bits1(&s->gb);
    s->concealment_motion_vectors = get_bits1(&s->gb);
    s->q_scale_type = get_bits1(&s->gb);
    s->intra_vlc_format = get_bits1(&s->gb);
    s->alternate_scan = get_bits1(&s->gb);
    s->repeat_first_field = get_bits1(&s->gb);
    s->chroma_420_type = get_bits1(&s->gb);
    s->progressive_frame = get_bits1(&s->gb);
1546

1547
    if(s->picture_structure == PICT_FRAME){
1548
        s->first_field=0;
1549 1550
        s->v_edge_pos= 16*s->mb_height;
    }else{
1551
        s->first_field ^= 1;
1552
        s->v_edge_pos=  8*s->mb_height;
1553
        memset(s->mbskip_table, 0, s->mb_stride*s->mb_height);
1554
    }
1555

1556
    if(s->alternate_scan){
Michael Niedermayer's avatar
Michael Niedermayer committed
1557 1558
        ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable  , ff_alternate_vertical_scan);
        ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable  , ff_alternate_vertical_scan);
1559
    }else{
Michael Niedermayer's avatar
Michael Niedermayer committed
1560 1561
        ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable  , ff_zigzag_direct);
        ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable  , ff_zigzag_direct);
1562
    }
1563

Fabrice Bellard's avatar
Fabrice Bellard committed
1564
    /* composite display not parsed */
1565 1566 1567 1568 1569 1570 1571 1572 1573
    dprintf(s->avctx, "intra_dc_precision=%d\n", s->intra_dc_precision);
    dprintf(s->avctx, "picture_structure=%d\n", s->picture_structure);
    dprintf(s->avctx, "top field first=%d\n", s->top_field_first);
    dprintf(s->avctx, "repeat first field=%d\n", s->repeat_first_field);
    dprintf(s->avctx, "conceal=%d\n", s->concealment_motion_vectors);
    dprintf(s->avctx, "intra_vlc_format=%d\n", s->intra_vlc_format);
    dprintf(s->avctx, "alternate_scan=%d\n", s->alternate_scan);
    dprintf(s->avctx, "frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
    dprintf(s->avctx, "progressive_frame=%d\n", s->progressive_frame);
Fabrice Bellard's avatar
Fabrice Bellard committed
1574 1575
}

1576
static void mpeg_decode_extension(AVCodecContext *avctx,
Zdenek Kabelac's avatar
Zdenek Kabelac committed
1577
                                  const uint8_t *buf, int buf_size)
Fabrice Bellard's avatar
Fabrice Bellard committed
1578 1579 1580 1581 1582
{
    Mpeg1Context *s1 = avctx->priv_data;
    MpegEncContext *s = &s1->mpeg_enc_ctx;
    int ext_type;

1583
    init_get_bits(&s->gb, buf, buf_size*8);
1584

Fabrice Bellard's avatar
Fabrice Bellard committed
1585 1586 1587
    ext_type = get_bits(&s->gb, 4);
    switch(ext_type) {
    case 0x1:
Michael Niedermayer's avatar
Michael Niedermayer committed
1588
        mpeg_decode_sequence_extension(s1);
Fabrice Bellard's avatar
Fabrice Bellard committed
1589
        break;
1590 1591 1592
    case 0x2:
        mpeg_decode_sequence_display_extension(s1);
        break;
Fabrice Bellard's avatar
Fabrice Bellard committed
1593 1594 1595
    case 0x3:
        mpeg_decode_quant_matrix_extension(s);
        break;
1596 1597 1598
    case 0x7:
        mpeg_decode_picture_display_extension(s1);
        break;
Fabrice Bellard's avatar
Fabrice Bellard committed
1599 1600 1601 1602 1603 1604
    case 0x8:
        mpeg_decode_picture_coding_extension(s);
        break;
    }
}

1605
static void exchange_uv(MpegEncContext *s){
Michael Niedermayer's avatar
Michael Niedermayer committed
1606
    short * tmp = s->pblocks[4];
1607 1608
    s->pblocks[4] = s->pblocks[5];
    s->pblocks[5] = tmp;
Michael Niedermayer's avatar
Michael Niedermayer committed
1609 1610
}

1611 1612 1613
static int mpeg_field_start(MpegEncContext *s){
    AVCodecContext *avctx= s->avctx;
    Mpeg1Context *s1 = (Mpeg1Context*)s;
Fabrice Bellard's avatar
Fabrice Bellard committed
1614 1615

    /* start frame decoding */
1616
    if(s->first_field || s->picture_structure==PICT_FRAME){
1617
        if(MPV_frame_start(s, avctx) < 0)
1618
            return -1;
1619 1620 1621

        ff_er_frame_start(s);

1622
        /* first check if we must repeat the frame */
1623
        s->current_picture_ptr->repeat_pict = 0;
1624 1625 1626
        if (s->repeat_first_field) {
            if (s->progressive_sequence) {
                if (s->top_field_first)
1627
                    s->current_picture_ptr->repeat_pict = 4;
1628
                else
1629
                    s->current_picture_ptr->repeat_pict = 2;
1630
            } else if (s->progressive_frame) {
1631
                s->current_picture_ptr->repeat_pict = 1;
1632
            }
1633
        }
1634 1635

        *s->current_picture_ptr->pan_scan= s1->pan_scan;
1636
    }else{ //second field
1637
            int i;
1638

1639
            if(!s->current_picture_ptr){
1640
                av_log(s->avctx, AV_LOG_ERROR, "first field missing\n");
1641 1642
                return -1;
            }
1643

1644 1645 1646 1647
            for(i=0; i<4; i++){
                s->current_picture.data[i] = s->current_picture_ptr->data[i];
                if(s->picture_structure == PICT_BOTTOM_FIELD){
                    s->current_picture.data[i] += s->current_picture_ptr->linesize[i];
1648
                }
1649
            }
1650
    }
Ivan Kalvachev's avatar
Ivan Kalvachev committed
1651 1652 1653
#ifdef HAVE_XVMC
// MPV_frame_start will call this function too,
// but we need to call it on every field
1654
    if(s->avctx->xvmc_acceleration)
Ivan Kalvachev's avatar
Ivan Kalvachev committed
1655 1656
         XVMC_field_start(s,avctx);
#endif
Fabrice Bellard's avatar
Fabrice Bellard committed
1657

1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669
    return 0;
}

#define DECODE_SLICE_ERROR -1
#define DECODE_SLICE_OK 0

/**
 * decodes a slice. MpegEncContext.mb_y must be set to the MB row from the startcode
 * @return DECODE_SLICE_ERROR if the slice is damaged<br>
 *         DECODE_SLICE_OK if this slice is ok<br>
 */
static int mpeg_decode_slice(Mpeg1Context *s1, int mb_y,
Zdenek Kabelac's avatar
Zdenek Kabelac committed
1670
                             const uint8_t **buf, int buf_size)
1671 1672 1673 1674
{
    MpegEncContext *s = &s1->mpeg_enc_ctx;
    AVCodecContext *avctx= s->avctx;
    const int field_pic= s->picture_structure != PICT_FRAME;
1675
    const int lowres= s->avctx->lowres;
1676 1677 1678 1679

    s->resync_mb_x=
    s->resync_mb_y= -1;

1680 1681
    if (mb_y<<field_pic >= s->mb_height){
        av_log(s->avctx, AV_LOG_ERROR, "slice below image (%d >= %d)\n", mb_y, s->mb_height);
1682 1683
        return -1;
    }
1684

1685
    init_get_bits(&s->gb, *buf, buf_size*8);
Fabrice Bellard's avatar
Fabrice Bellard committed
1686

1687 1688 1689
    ff_mpeg1_clean_buffers(s);
    s->interlaced_dct = 0;

1690
    s->qscale = get_qscale(s);
1691

1692
    if(s->qscale == 0){
1693
        av_log(s->avctx, AV_LOG_ERROR, "qscale == 0\n");
1694
        return -1;
1695
    }
1696

Fabrice Bellard's avatar
Fabrice Bellard committed
1697
    /* extra slice info */
1698 1699
    while (get_bits1(&s->gb) != 0) {
        skip_bits(&s->gb, 8);
Fabrice Bellard's avatar
Fabrice Bellard committed
1700
    }
1701

1702
    s->mb_x=0;
1703

1704 1705
    for(;;) {
        int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
1706
        if (code < 0){
1707
            av_log(s->avctx, AV_LOG_ERROR, "first mb_incr damaged\n");
1708
            return -1;
1709
        }
1710 1711 1712 1713 1714 1715 1716 1717 1718 1719
        if (code >= 33) {
            if (code == 33) {
                s->mb_x += 33;
            }
            /* otherwise, stuffing, nothing to do */
        } else {
            s->mb_x += code;
            break;
        }
    }
1720 1721 1722 1723
    if(s->mb_x >= (unsigned)s->mb_width){
        av_log(s->avctx, AV_LOG_ERROR, "initial skip overflow\n");
        return -1;
    }
1724

1725
    s->resync_mb_x= s->mb_x;
1726
    s->resync_mb_y= s->mb_y= mb_y;
1727
    s->mb_skip_run= 0;
1728
    ff_init_block_index(s);
1729

1730 1731
    if (s->mb_y==0 && s->mb_x==0 && (s->first_field || s->picture_structure==PICT_FRAME)) {
        if(s->avctx->debug&FF_DEBUG_PICT_INFO){
1732
             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",
1733
                 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],
1734 1735
                 s->pict_type == I_TYPE ? "I" : (s->pict_type == P_TYPE ? "P" : (s->pict_type == B_TYPE ? "B" : "S")),
                 s->progressive_sequence ? "ps" :"", s->progressive_frame ? "pf" : "", s->alternate_scan ? "alt" :"", s->top_field_first ? "top" :"",
1736 1737 1738
                 s->intra_dc_precision, s->picture_structure, s->frame_pred_frame_dct, s->concealment_motion_vectors,
                 s->q_scale_type, s->intra_vlc_format, s->repeat_first_field, s->chroma_420_type ? "420" :"");
        }
1739 1740
    }

Fabrice Bellard's avatar
Fabrice Bellard committed
1741
    for(;;) {
1742 1743 1744 1745 1746 1747
#ifdef HAVE_XVMC
        //one 1 we memcpy blocks in xvmcvideo
        if(s->avctx->xvmc_acceleration > 1)
            XVMC_init_block(s);//set s->block
#endif

1748
        if(mpeg_decode_mb(s, s->block) < 0)
Fabrice Bellard's avatar
Fabrice Bellard committed
1749
            return -1;
1750

1751
        if(s->current_picture.motion_val[0] && !s->encoding){ //note motion_val is normally NULL unless we want to extract the MVs
1752 1753
            const int wrap = field_pic ? 2*s->b8_stride : s->b8_stride;
            int xy = s->mb_x*2 + s->mb_y*2*wrap;
1754
            int motion_x, motion_y, dir, i;
1755 1756 1757
            if(field_pic && !s->first_field)
                xy += wrap/2;

Michael Niedermayer's avatar
Michael Niedermayer committed
1758 1759
            for(i=0; i<2; i++){
                for(dir=0; dir<2; dir++){
1760 1761
                    if (s->mb_intra || (dir==1 && s->pict_type != B_TYPE)) {
                        motion_x = motion_y = 0;
1762
                    }else if (s->mv_type == MV_TYPE_16X16 || (s->mv_type == MV_TYPE_FIELD && field_pic)){
1763 1764 1765 1766 1767 1768
                        motion_x = s->mv[dir][0][0];
                        motion_y = s->mv[dir][0][1];
                    } else /*if ((s->mv_type == MV_TYPE_FIELD) || (s->mv_type == MV_TYPE_16X8))*/ {
                        motion_x = s->mv[dir][i][0];
                        motion_y = s->mv[dir][i][1];
                    }
1769

1770 1771 1772 1773
                    s->current_picture.motion_val[dir][xy    ][0] = motion_x;
                    s->current_picture.motion_val[dir][xy    ][1] = motion_y;
                    s->current_picture.motion_val[dir][xy + 1][0] = motion_x;
                    s->current_picture.motion_val[dir][xy + 1][1] = motion_y;
1774 1775
                    s->current_picture.ref_index [dir][xy    ]=
                    s->current_picture.ref_index [dir][xy + 1]= s->field_select[dir][i];
Michael Niedermayer's avatar
Michael Niedermayer committed
1776
                    assert(s->field_select[dir][i]==0 || s->field_select[dir][i]==1);
1777
                }
Michael Niedermayer's avatar
Michael Niedermayer committed
1778
                xy += wrap;
1779 1780
            }
        }
1781

1782
        s->dest[0] += 16 >> lowres;
1783 1784
        s->dest[1] +=(16 >> lowres) >> s->chroma_x_shift;
        s->dest[2] +=(16 >> lowres) >> s->chroma_x_shift;
1785

1786
        MPV_decode_mb(s, s->block);
1787

1788
        if (++s->mb_x >= s->mb_width) {
Michael Niedermayer's avatar
Michael Niedermayer committed
1789
            const int mb_size= 16>>s->avctx->lowres;
Michael Niedermayer's avatar
Michael Niedermayer committed
1790

Michael Niedermayer's avatar
Michael Niedermayer committed
1791
            ff_draw_horiz_band(s, mb_size*s->mb_y, mb_size);
1792 1793 1794

            s->mb_x = 0;
            s->mb_y++;
1795 1796 1797

            if(s->mb_y<<field_pic >= s->mb_height){
                int left= s->gb.size_in_bits - get_bits_count(&s->gb);
Michael Niedermayer's avatar
Michael Niedermayer committed
1798 1799 1800
                int is_d10= s->chroma_format==2 && s->pict_type==I_TYPE && avctx->profile==0 && avctx->level==5
                            && s->intra_dc_precision == 2 && s->q_scale_type == 1 && s->alternate_scan == 0
                            && s->progressive_frame == 0 /* vbv_delay == 0xBBB || 0xE10*/;
1801

Michael Niedermayer's avatar
Michael Niedermayer committed
1802
                if(left < 0 || (left && show_bits(&s->gb, FFMIN(left, 23)) && !is_d10)
1803
                   || (avctx->error_resilience >= FF_ER_AGGRESSIVE && left>8)){
Michael Niedermayer's avatar
Michael Niedermayer committed
1804
                    av_log(avctx, AV_LOG_ERROR, "end mismatch left=%d %0X\n", left, show_bits(&s->gb, FFMIN(left, 23)));
1805 1806 1807 1808
                    return -1;
                }else
                    goto eos;
            }
1809

1810
            ff_init_block_index(s);
1811 1812 1813
        }

        /* skip mb handling */
1814
        if (s->mb_skip_run == -1) {
1815
            /* read again increment */
1816
            s->mb_skip_run = 0;
1817 1818
            for(;;) {
                int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
1819
                if (code < 0){
1820
                    av_log(s->avctx, AV_LOG_ERROR, "mb incr damaged\n");
1821
                    return -1;
1822
                }
1823 1824
                if (code >= 33) {
                    if (code == 33) {
1825
                        s->mb_skip_run += 33;
1826 1827
                    }else if(code == 35){
                        if(s->mb_skip_run != 0 || show_bits(&s->gb, 15) != 0){
1828
                            av_log(s->avctx, AV_LOG_ERROR, "slice mismatch\n");
1829 1830 1831
                            return -1;
                        }
                        goto eos; /* end of slice */
1832 1833 1834
                    }
                    /* otherwise, stuffing, nothing to do */
                } else {
1835
                    s->mb_skip_run += code;
1836 1837 1838
                    break;
                }
            }
1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868
            if(s->mb_skip_run){
                int i;
                if(s->pict_type == I_TYPE){
                    av_log(s->avctx, AV_LOG_ERROR, "skipped MB in I frame at %d %d\n", s->mb_x, s->mb_y);
                    return -1;
                }

                /* skip mb */
                s->mb_intra = 0;
                for(i=0;i<12;i++)
                    s->block_last_index[i] = -1;
                if(s->picture_structure == PICT_FRAME)
                    s->mv_type = MV_TYPE_16X16;
                else
                    s->mv_type = MV_TYPE_FIELD;
                if (s->pict_type == P_TYPE) {
                    /* if P type, zero motion vector is implied */
                    s->mv_dir = MV_DIR_FORWARD;
                    s->mv[0][0][0] = s->mv[0][0][1] = 0;
                    s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
                    s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
                    s->field_select[0][0]= s->picture_structure - 1;
                } else {
                    /* if B type, reuse previous vectors and directions */
                    s->mv[0][0][0] = s->last_mv[0][0][0];
                    s->mv[0][0][1] = s->last_mv[0][0][1];
                    s->mv[1][0][0] = s->last_mv[1][0][0];
                    s->mv[1][0][1] = s->last_mv[1][0][1];
                }
            }
1869
        }
Fabrice Bellard's avatar
Fabrice Bellard committed
1870
    }
1871 1872
eos: // end of slice
    *buf += get_bits_count(&s->gb)/8 - 1;
1873
//printf("y %d %d %d %d\n", s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y);
1874 1875
    return 0;
}
1876

1877 1878
static int slice_decode_thread(AVCodecContext *c, void *arg){
    MpegEncContext *s= arg;
1879
    const uint8_t *buf= s->gb.buffer;
1880 1881 1882 1883 1884
    int mb_y= s->start_mb_y;

    s->error_count= 3*(s->end_mb_y - s->start_mb_y)*s->mb_width;

    for(;;){
1885 1886
        uint32_t start_code;
        int ret;
1887 1888 1889

        ret= mpeg_decode_slice((Mpeg1Context*)s, mb_y, &buf, s->gb.buffer_end - buf);
        emms_c();
1890
//av_log(c, AV_LOG_DEBUG, "ret:%d resync:%d/%d mb:%d/%d ts:%d/%d ec:%d\n",
1891 1892 1893 1894 1895 1896 1897
//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);
        if(ret < 0){
            if(s->resync_mb_x>=0 && s->resync_mb_y>=0)
                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);
        }else{
            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);
        }
1898

1899 1900
        if(s->mb_y == s->end_mb_y)
            return 0;
1901

1902 1903
        start_code= -1;
        buf = ff_find_start_code(buf, s->gb.buffer_end, &start_code);
1904 1905 1906 1907
        mb_y= start_code - SLICE_MIN_START_CODE;
        if(mb_y < 0 || mb_y >= s->end_mb_y)
            return -1;
    }
1908

1909 1910 1911
    return 0; //not reached
}

1912 1913
/**
 * handles slice ends.
1914
 * @return 1 if it seems to be the last slice of
1915 1916 1917 1918 1919
 */
static int slice_end(AVCodecContext *avctx, AVFrame *pict)
{
    Mpeg1Context *s1 = avctx->priv_data;
    MpegEncContext *s = &s1->mpeg_enc_ctx;
1920

1921
    if (!s1->mpeg_enc_ctx_allocated || !s->current_picture_ptr)
1922 1923
        return 0;

Ivan Kalvachev's avatar
Ivan Kalvachev committed
1924 1925 1926 1927
#ifdef HAVE_XVMC
    if(s->avctx->xvmc_acceleration)
        XVMC_field_end(s);
#endif
Fabrice Bellard's avatar
Fabrice Bellard committed
1928
    /* end of slice reached */
1929
    if (/*s->mb_y<<field_pic == s->mb_height &&*/ !s->first_field) {
Fabrice Bellard's avatar
Fabrice Bellard committed
1930
        /* end of image */
Michael Niedermayer's avatar
Michael Niedermayer committed
1931

Michael Niedermayer's avatar
Michael Niedermayer committed
1932
        s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_MPEG2;
Michael Niedermayer's avatar
Michael Niedermayer committed
1933

1934
        ff_er_frame_end(s);
Fabrice Bellard's avatar
Fabrice Bellard committed
1935 1936 1937

        MPV_frame_end(s);

Michael Niedermayer's avatar
Michael Niedermayer committed
1938
        if (s->pict_type == B_TYPE || s->low_delay) {
Michael Niedermayer's avatar
Michael Niedermayer committed
1939
            *pict= *(AVFrame*)s->current_picture_ptr;
1940
            ff_print_debug_info(s, pict);
Fabrice Bellard's avatar
Fabrice Bellard committed
1941
        } else {
Michael Niedermayer's avatar
Michael Niedermayer committed
1942
            s->picture_number++;
Fabrice Bellard's avatar
Fabrice Bellard committed
1943 1944
            /* latency of 1 frame for I and P frames */
            /* XXX: use another variable than picture_number */
1945
            if (s->last_picture_ptr != NULL) {
Michael Niedermayer's avatar
Michael Niedermayer committed
1946
                *pict= *(AVFrame*)s->last_picture_ptr;
1947
                 ff_print_debug_info(s, pict);
Fabrice Bellard's avatar
Fabrice Bellard committed
1948 1949
            }
        }
Michael Niedermayer's avatar
Michael Niedermayer committed
1950

1951
        return 1;
Fabrice Bellard's avatar
Fabrice Bellard committed
1952
    } else {
1953
        return 0;
Fabrice Bellard's avatar
Fabrice Bellard committed
1954 1955 1956
    }
}

1957
static int mpeg1_decode_sequence(AVCodecContext *avctx,
Zdenek Kabelac's avatar
Zdenek Kabelac committed
1958
                                 const uint8_t *buf, int buf_size)
Fabrice Bellard's avatar
Fabrice Bellard committed
1959 1960 1961
{
    Mpeg1Context *s1 = avctx->priv_data;
    MpegEncContext *s = &s1->mpeg_enc_ctx;
1962
    int width,height;
1963
    int i, v, j;
1964

1965
    init_get_bits(&s->gb, buf, buf_size*8);
Fabrice Bellard's avatar
Fabrice Bellard committed
1966

1967 1968 1969 1970 1971
    width = get_bits(&s->gb, 12);
    height = get_bits(&s->gb, 12);
    if (width <= 0 || height <= 0 ||
        (width % 2) != 0 || (height % 2) != 0)
        return -1;
1972
    s->aspect_ratio_info= get_bits(&s->gb, 4);
1973 1974
    if (s->aspect_ratio_info == 0)
        return -1;
Fabrice Bellard's avatar
Fabrice Bellard committed
1975
    s->frame_rate_index = get_bits(&s->gb, 4);
1976
    if (s->frame_rate_index == 0 || s->frame_rate_index > 13)
Fabrice Bellard's avatar
Fabrice Bellard committed
1977 1978
        return -1;
    s->bit_rate = get_bits(&s->gb, 18) * 400;
1979
    if (get_bits1(&s->gb) == 0) /* marker */
Fabrice Bellard's avatar
Fabrice Bellard committed
1980
        return -1;
1981 1982
    s->width = width;
    s->height = height;
Fabrice Bellard's avatar
Fabrice Bellard committed
1983

1984
    s->avctx->rc_buffer_size= get_bits(&s->gb, 10) * 1024*16;
1985
    skip_bits(&s->gb, 1);
Fabrice Bellard's avatar
Fabrice Bellard committed
1986 1987

    /* get matrix */
1988
    if (get_bits1(&s->gb)) {
Fabrice Bellard's avatar
Fabrice Bellard committed
1989 1990
        for(i=0;i<64;i++) {
            v = get_bits(&s->gb, 8);
1991 1992 1993 1994
            if(v==0){
                av_log(s->avctx, AV_LOG_ERROR, "intra matrix damaged\n");
                return -1;
            }
1995
            j = s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
1996 1997
            s->intra_matrix[j] = v;
            s->chroma_intra_matrix[j] = v;
Fabrice Bellard's avatar
Fabrice Bellard committed
1998
        }
1999
#ifdef DEBUG
2000
        dprintf(s->avctx, "intra matrix present\n");
2001
        for(i=0;i<64;i++)
2002 2003
            dprintf(s->avctx, " %d", s->intra_matrix[s->dsp.idct_permutation[i]]);
        dprintf(s->avctx, "\n");
2004
#endif
Fabrice Bellard's avatar
Fabrice Bellard committed
2005 2006
    } else {
        for(i=0;i<64;i++) {
2007
            j = s->dsp.idct_permutation[i];
2008
            v = ff_mpeg1_default_intra_matrix[i];
2009 2010
            s->intra_matrix[j] = v;
            s->chroma_intra_matrix[j] = v;
Fabrice Bellard's avatar
Fabrice Bellard committed
2011 2012
        }
    }
2013
    if (get_bits1(&s->gb)) {
Fabrice Bellard's avatar
Fabrice Bellard committed
2014 2015
        for(i=0;i<64;i++) {
            v = get_bits(&s->gb, 8);
2016 2017 2018 2019
            if(v==0){
                av_log(s->avctx, AV_LOG_ERROR, "inter matrix damaged\n");
                return -1;
            }
2020
            j = s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
2021 2022
            s->inter_matrix[j] = v;
            s->chroma_inter_matrix[j] = v;
Fabrice Bellard's avatar
Fabrice Bellard committed
2023
        }
2024
#ifdef DEBUG
2025
        dprintf(s->avctx, "non intra matrix present\n");
2026
        for(i=0;i<64;i++)
2027 2028
            dprintf(s->avctx, " %d", s->inter_matrix[s->dsp.idct_permutation[i]]);
        dprintf(s->avctx, "\n");
2029
#endif
Fabrice Bellard's avatar
Fabrice Bellard committed
2030 2031
    } else {
        for(i=0;i<64;i++) {
2032
            int j= s->dsp.idct_permutation[i];
2033
            v = ff_mpeg1_default_non_intra_matrix[i];
2034 2035
            s->inter_matrix[j] = v;
            s->chroma_inter_matrix[j] = v;
Fabrice Bellard's avatar
Fabrice Bellard committed
2036 2037
        }
    }
2038

2039 2040 2041 2042
    if(show_bits(&s->gb, 23) != 0){
        av_log(s->avctx, AV_LOG_ERROR, "sequence header damaged\n");
        return -1;
    }
Fabrice Bellard's avatar
Fabrice Bellard committed
2043 2044 2045 2046 2047 2048

    /* we set mpeg2 parameters so that it emulates mpeg1 */
    s->progressive_sequence = 1;
    s->progressive_frame = 1;
    s->picture_structure = PICT_FRAME;
    s->frame_pred_frame_dct = 1;
2049
    s->chroma_format = 1;
Michael Niedermayer's avatar
Michael Niedermayer committed
2050
    s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG1VIDEO;
2051
    avctx->sub_id = 1; /* indicates mpeg1 */
2052 2053
    s->out_format = FMT_MPEG1;
    s->swap_uv = 0;//AFAIK VCR2 don't have SEQ_HEADER
2054
    if(s->flags & CODEC_FLAG_LOW_DELAY) s->low_delay=1;
2055

2056
    if(s->avctx->debug & FF_DEBUG_PICT_INFO)
2057
        av_log(s->avctx, AV_LOG_DEBUG, "vbv buffer: %d, bitrate:%d\n",
2058
               s->avctx->rc_buffer_size, s->bit_rate);
2059

Fabrice Bellard's avatar
Fabrice Bellard committed
2060 2061 2062
    return 0;
}

2063 2064 2065 2066
static int vcr2_init_sequence(AVCodecContext *avctx)
{
    Mpeg1Context *s1 = avctx->priv_data;
    MpegEncContext *s = &s1->mpeg_enc_ctx;
Michael Niedermayer's avatar
Michael Niedermayer committed
2067
    int i, v;
2068 2069 2070 2071 2072 2073

    /* start new mpeg1 context decoding */
    s->out_format = FMT_MPEG1;
    if (s1->mpeg_enc_ctx_allocated) {
        MPV_common_end(s);
    }
2074 2075
    s->width  = avctx->coded_width;
    s->height = avctx->coded_height;
2076
    avctx->has_b_frames= 0; //true?
Michael Niedermayer's avatar
Michael Niedermayer committed
2077
    s->low_delay= 1;
Ivan Kalvachev's avatar
Ivan Kalvachev committed
2078

Ivan Kalvachev's avatar
Ivan Kalvachev committed
2079 2080 2081 2082 2083 2084
    if(avctx->xvmc_acceleration){
        avctx->pix_fmt = avctx->get_format(avctx,pixfmt_xvmc_mpg2_420);
    }else{
        avctx->pix_fmt = avctx->get_format(avctx,pixfmt_yuv_420);
    }

Ivan Kalvachev's avatar
Ivan Kalvachev committed
2085
    if( avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT )
2086 2087
        if( avctx->idct_algo == FF_IDCT_AUTO )
            avctx->idct_algo = FF_IDCT_SIMPLE;
2088

2089 2090
    if (MPV_common_init(s) < 0)
        return -1;
2091
    exchange_uv(s);//common init reset pblocks, so we swap them here
2092
    s->swap_uv = 1;// in case of xvmc we need to swap uv for each MB
2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109
    s1->mpeg_enc_ctx_allocated = 1;

    for(i=0;i<64;i++) {
        int j= s->dsp.idct_permutation[i];
        v = ff_mpeg1_default_intra_matrix[i];
        s->intra_matrix[j] = v;
        s->chroma_intra_matrix[j] = v;

        v = ff_mpeg1_default_non_intra_matrix[i];
        s->inter_matrix[j] = v;
        s->chroma_inter_matrix[j] = v;
    }

    s->progressive_sequence = 1;
    s->progressive_frame = 1;
    s->picture_structure = PICT_FRAME;
    s->frame_pred_frame_dct = 1;
2110
    s->chroma_format = 1;
Michael Niedermayer's avatar
Michael Niedermayer committed
2111
    s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG2VIDEO;
Michael Niedermayer's avatar
Michael Niedermayer committed
2112
    avctx->sub_id = 2; /* indicates mpeg2 */
2113 2114 2115 2116
    return 0;
}


2117
static void mpeg_decode_user_data(AVCodecContext *avctx,
2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145
                                  const uint8_t *buf, int buf_size)
{
    const uint8_t *p;
    int len, flags;
    p = buf;
    len = buf_size;

    /* we parse the DTG active format information */
    if (len >= 5 &&
        p[0] == 'D' && p[1] == 'T' && p[2] == 'G' && p[3] == '1') {
        flags = p[4];
        p += 5;
        len -= 5;
        if (flags & 0x80) {
            /* skip event id */
            if (len < 2)
                return;
            p += 2;
            len -= 2;
        }
        if (flags & 0x40) {
            if (len < 1)
                return;
            avctx->dtg_active_format = p[0] & 0x0f;
        }
    }
}

2146
static void mpeg_decode_gop(AVCodecContext *avctx,
2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158
                            const uint8_t *buf, int buf_size){
    Mpeg1Context *s1 = avctx->priv_data;
    MpegEncContext *s = &s1->mpeg_enc_ctx;

    int drop_frame_flag;
    int time_code_hours, time_code_minutes;
    int time_code_seconds, time_code_pictures;
    int broken_link;

    init_get_bits(&s->gb, buf, buf_size*8);

    drop_frame_flag = get_bits1(&s->gb);
2159

2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172
    time_code_hours=get_bits(&s->gb,5);
    time_code_minutes = get_bits(&s->gb,6);
    skip_bits1(&s->gb);//marker bit
    time_code_seconds = get_bits(&s->gb,6);
    time_code_pictures = get_bits(&s->gb,6);

    /*broken_link indicate that after editing the
      reference frames of the first B-Frames after GOP I-Frame
      are missing (open gop)*/
    broken_link = get_bits1(&s->gb);

    if(s->avctx->debug & FF_DEBUG_PICT_INFO)
        av_log(s->avctx, AV_LOG_DEBUG, "GOP (%2d:%02d:%02d.[%02d]) broken_link=%d\n",
2173 2174
            time_code_hours, time_code_minutes, time_code_seconds,
            time_code_pictures, broken_link);
2175
}
2176 2177 2178 2179
/**
 * finds the end of the current frame in the bitstream.
 * @return the position of the first byte of the next frame, or -1
 */
2180
int ff_mpeg1_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size)
2181
{
2182
    int i;
2183
    uint32_t state= pc->state;
2184

2185 2186 2187
    i=0;
    if(!pc->frame_start_found){
        for(i=0; i<buf_size; i++){
2188
            i= ff_find_start_code(buf+i, buf+buf_size, &state) - buf - 1;
2189 2190 2191 2192 2193
            if(state >= SLICE_MIN_START_CODE && state <= SLICE_MAX_START_CODE){
                i++;
                pc->frame_start_found=1;
                break;
            }
2194 2195 2196 2197
            if(state == SEQ_END_CODE){
                pc->state=-1;
                return i+1;
            }
2198 2199
        }
    }
2200

2201
    if(pc->frame_start_found){
2202 2203 2204
        /* EOF considered as end of frame */
        if (buf_size == 0)
            return 0;
2205
        for(; i<buf_size; i++){
2206
            i= ff_find_start_code(buf+i, buf+buf_size, &state) - buf - 1;
2207 2208 2209
            if((state&0xFFFFFF00) == 0x100){
                if(state < SLICE_MIN_START_CODE || state > SLICE_MAX_START_CODE){
                    pc->frame_start_found=0;
2210
                    pc->state=-1;
2211 2212 2213 2214
                    return i-3;
                }
            }
        }
2215
    }
2216
    pc->state= state;
2217
    return END_NOT_FOUND;
2218 2219
}

Fabrice Bellard's avatar
Fabrice Bellard committed
2220
/* handle buffering and image synchronisation */
2221
static int mpeg_decode_frame(AVCodecContext *avctx,
Fabrice Bellard's avatar
Fabrice Bellard committed
2222
                             void *data, int *data_size,
2223
                             uint8_t *buf, int buf_size)
Fabrice Bellard's avatar
Fabrice Bellard committed
2224 2225
{
    Mpeg1Context *s = avctx->priv_data;
Zdenek Kabelac's avatar
Zdenek Kabelac committed
2226 2227
    const uint8_t *buf_end;
    const uint8_t *buf_ptr;
2228 2229
    uint32_t start_code;
    int ret, input_size;
2230
    AVFrame *picture = data;
2231
    MpegEncContext *s2 = &s->mpeg_enc_ctx;
2232
    dprintf(avctx, "fill_buffer\n");
Fabrice Bellard's avatar
Fabrice Bellard committed
2233

2234
    if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == SEQ_END_CODE)) {
2235 2236 2237 2238
        /* special case for last picture */
        if (s2->low_delay==0 && s2->next_picture_ptr) {
            *picture= *(AVFrame*)s2->next_picture_ptr;
            s2->next_picture_ptr= NULL;
Michael Niedermayer's avatar
Michael Niedermayer committed
2239

2240 2241
            *data_size = sizeof(AVFrame);
        }
Fabrice Bellard's avatar
Fabrice Bellard committed
2242 2243 2244
        return 0;
    }

2245
    if(s2->flags&CODEC_FLAG_TRUNCATED){
2246
        int next= ff_mpeg1_find_frame_end(&s2->parse_context, buf, buf_size);
2247

2248
        if( ff_combine_frame(&s2->parse_context, next, (const uint8_t **)&buf, &buf_size) < 0 )
2249
            return buf_size;
2250 2251
    }

Fabrice Bellard's avatar
Fabrice Bellard committed
2252 2253
    buf_ptr = buf;
    buf_end = buf + buf_size;
2254

2255 2256
#if 0
    if (s->repeat_field % 2 == 1) {
2257 2258
        s->repeat_field++;
        //fprintf(stderr,"\nRepeating last frame: %d -> %d! pict: %d %d", avctx->frame_number-1, avctx->frame_number,
2259 2260 2261 2262 2263
        //        s2->picture_number, s->repeat_field);
        if (avctx->flags & CODEC_FLAG_REPEAT_FIELD) {
            *data_size = sizeof(AVPicture);
            goto the_end;
        }
2264
    }
2265
#endif
2266 2267 2268

    if(s->mpeg_enc_ctx_allocated==0 && avctx->codec_tag == ff_get_fourcc("VCR2"))
        vcr2_init_sequence(avctx);
2269

2270
    s->slice_count= 0;
2271

2272
    for(;;) {
Fabrice Bellard's avatar
Fabrice Bellard committed
2273
        /* find start next code */
2274 2275
        start_code = -1;
        buf_ptr = ff_find_start_code(buf_ptr,buf_end, &start_code);
2276
        if (start_code > 0x1ff){
Michael Niedermayer's avatar
Michael Niedermayer committed
2277
            if(s2->pict_type != B_TYPE || avctx->skip_frame <= AVDISCARD_DEFAULT){
2278 2279 2280 2281 2282 2283 2284
                if(avctx->thread_count > 1){
                    int i;

                    avctx->execute(avctx, slice_decode_thread,  (void**)&(s2->thread_context[0]), NULL, s->slice_count);
                    for(i=0; i<s->slice_count; i++)
                        s2->error_count += s2->thread_context[i]->error_count;
                }
Michael Niedermayer's avatar
Michael Niedermayer committed
2285
                if (slice_end(avctx, picture)) {
2286
                    if(s2->last_picture_ptr || s2->low_delay) //FIXME merge with the stuff in mpeg_decode_slice
Michael Niedermayer's avatar
Michael Niedermayer committed
2287 2288
                        *data_size = sizeof(AVPicture);
                }
2289
            }
Michael Niedermayer's avatar
Michael Niedermayer committed
2290
            return FFMAX(0, buf_ptr - buf - s2->parse_context.last_index);
Fabrice Bellard's avatar
Fabrice Bellard committed
2291
        }
2292

2293 2294 2295
        input_size = buf_end - buf_ptr;

        if(avctx->debug & FF_DEBUG_STARTCODE){
Falk Hüffner's avatar
Falk Hüffner committed
2296
            av_log(avctx, AV_LOG_DEBUG, "%3X at %zd left %d\n", start_code, buf_ptr-buf, input_size);
2297
        }
2298

2299 2300 2301 2302 2303 2304
        /* prepare data for next start code */
        switch(start_code) {
        case SEQ_START_CODE:
            mpeg1_decode_sequence(avctx, buf_ptr,
                                    input_size);
            break;
2305

2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329
        case PICTURE_START_CODE:
            /* we have a complete image : we try to decompress it */
            mpeg1_decode_picture(avctx,
                                    buf_ptr, input_size);
            break;
        case EXT_START_CODE:
            mpeg_decode_extension(avctx,
                                    buf_ptr, input_size);
            break;
        case USER_START_CODE:
            mpeg_decode_user_data(avctx,
                                    buf_ptr, input_size);
            break;
        case GOP_START_CODE:
            s2->first_field=0;
            mpeg_decode_gop(avctx,
                                    buf_ptr, input_size);
            break;
        default:
            if (start_code >= SLICE_MIN_START_CODE &&
                start_code <= SLICE_MAX_START_CODE) {
                int mb_y= start_code - SLICE_MIN_START_CODE;

                if(s2->last_picture_ptr==NULL){
Diego Biurrun's avatar
Diego Biurrun committed
2330
                /* Skip B-frames if we do not have reference frames. */
2331
                    if(s2->pict_type==B_TYPE) break;
Diego Biurrun's avatar
Diego Biurrun committed
2332
                /* Skip P-frames if we do not have reference frame no valid header. */
2333 2334
//                    if(s2->pict_type==P_TYPE && s2->first_field && !s2->first_slice) break;
                }
Diego Biurrun's avatar
Diego Biurrun committed
2335
                /* Skip B-frames if we are in a hurry. */
2336 2337 2338 2339
                if(avctx->hurry_up && s2->pict_type==B_TYPE) break;
                if(  (avctx->skip_frame >= AVDISCARD_NONREF && s2->pict_type==B_TYPE)
                    ||(avctx->skip_frame >= AVDISCARD_NONKEY && s2->pict_type!=I_TYPE)
                    || avctx->skip_frame >= AVDISCARD_ALL)
Michael Niedermayer's avatar
Michael Niedermayer committed
2340
                    break;
Diego Biurrun's avatar
Diego Biurrun committed
2341
                /* Skip everything if we are in a hurry>=5. */
2342
                if(avctx->hurry_up>=5) break;
2343

2344 2345 2346 2347 2348 2349
                if (!s->mpeg_enc_ctx_allocated) break;

                if(s2->codec_id == CODEC_ID_MPEG2VIDEO){
                    if(mb_y < avctx->skip_top || mb_y >= s2->mb_height - avctx->skip_bottom)
                        break;
                }
2350

2351 2352 2353 2354 2355
                if(s2->first_slice){
                    s2->first_slice=0;
                            if(mpeg_field_start(s2) < 0)
                        return -1;
                    }
2356
                if(!s2->current_picture_ptr){
Diego Biurrun's avatar
Diego Biurrun committed
2357
                    av_log(avctx, AV_LOG_ERROR, "current_picture not initialized\n");
2358 2359
                    return -1;
                }
2360

2361 2362 2363 2364 2365 2366 2367 2368 2369 2370
                if(avctx->thread_count > 1){
                    int threshold= (s2->mb_height*s->slice_count + avctx->thread_count/2) / avctx->thread_count;
                    if(threshold <= mb_y){
                        MpegEncContext *thread_context= s2->thread_context[s->slice_count];

                        thread_context->start_mb_y= mb_y;
                        thread_context->end_mb_y  = s2->mb_height;
                        if(s->slice_count){
                            s2->thread_context[s->slice_count-1]->end_mb_y= mb_y;
                            ff_update_duplicate_context(thread_context, s2);
Fabrice Bellard's avatar
Fabrice Bellard committed
2371
                        }
2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384
                        init_get_bits(&thread_context->gb, buf_ptr, input_size*8);
                        s->slice_count++;
                    }
                    buf_ptr += 2; //FIXME add minimum num of bytes per slice
                }else{
                    ret = mpeg_decode_slice(s, mb_y, &buf_ptr, input_size);
                    emms_c();

                    if(ret < 0){
                        if(s2->resync_mb_x>=0 && s2->resync_mb_y>=0)
                            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);
                    }else{
                        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);
Fabrice Bellard's avatar
Fabrice Bellard committed
2385 2386
                    }
                }
2387 2388 2389
            }
            break;
        }
Fabrice Bellard's avatar
Fabrice Bellard committed
2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401
    }
}

static int mpeg_decode_end(AVCodecContext *avctx)
{
    Mpeg1Context *s = avctx->priv_data;

    if (s->mpeg_enc_ctx_allocated)
        MPV_common_end(&s->mpeg_enc_ctx);
    return 0;
}

2402 2403
AVCodec mpeg1video_decoder = {
    "mpeg1video",
Fabrice Bellard's avatar
Fabrice Bellard committed
2404 2405 2406 2407 2408 2409 2410
    CODEC_TYPE_VIDEO,
    CODEC_ID_MPEG1VIDEO,
    sizeof(Mpeg1Context),
    mpeg_decode_init,
    NULL,
    mpeg_decode_end,
    mpeg_decode_frame,
2411
    CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423
    .flush= ff_mpeg_flush,
};

AVCodec mpeg2video_decoder = {
    "mpeg2video",
    CODEC_TYPE_VIDEO,
    CODEC_ID_MPEG2VIDEO,
    sizeof(Mpeg1Context),
    mpeg_decode_init,
    NULL,
    mpeg_decode_end,
    mpeg_decode_frame,
2424
    CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
Ivan Kalvachev's avatar
Ivan Kalvachev committed
2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437
    .flush= ff_mpeg_flush,
};

//legacy decoder
AVCodec mpegvideo_decoder = {
    "mpegvideo",
    CODEC_TYPE_VIDEO,
    CODEC_ID_MPEG2VIDEO,
    sizeof(Mpeg1Context),
    mpeg_decode_init,
    NULL,
    mpeg_decode_end,
    mpeg_decode_frame,
2438
    CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
Michael Niedermayer's avatar
Michael Niedermayer committed
2439
    .flush= ff_mpeg_flush,
Fabrice Bellard's avatar
Fabrice Bellard committed
2440
};
Ivan Kalvachev's avatar
Ivan Kalvachev committed
2441 2442 2443 2444 2445

#ifdef HAVE_XVMC
static int mpeg_mc_decode_init(AVCodecContext *avctx){
    Mpeg1Context *s;

2446
    if( avctx->thread_count > 1)
2447
        return -1;
Ivan Kalvachev's avatar
Ivan Kalvachev committed
2448 2449
    if( !(avctx->slice_flags & SLICE_FLAG_CODED_ORDER) )
        return -1;
2450
    if( !(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD) ){
2451
        dprintf(avctx, "mpeg12.c: XvMC decoder will work better if SLICE_FLAG_ALLOW_FIELD is set\n");
2452
    }
Ivan Kalvachev's avatar
Ivan Kalvachev committed
2453 2454 2455 2456
    mpeg_decode_init(avctx);
    s = avctx->priv_data;

    avctx->pix_fmt = PIX_FMT_XVMC_MPEG2_IDCT;
2457
    avctx->xvmc_acceleration = 2;//2 - the blocks are packed!
Ivan Kalvachev's avatar
Ivan Kalvachev committed
2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470

    return 0;
}

AVCodec mpeg_xvmc_decoder = {
    "mpegvideo_xvmc",
    CODEC_TYPE_VIDEO,
    CODEC_ID_MPEG2VIDEO_XVMC,
    sizeof(Mpeg1Context),
    mpeg_mc_decode_init,
    NULL,
    mpeg_decode_end,
    mpeg_decode_frame,
2471
    CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED| CODEC_CAP_HWACCEL | CODEC_CAP_DELAY,
2472
    .flush= ff_mpeg_flush,
Ivan Kalvachev's avatar
Ivan Kalvachev committed
2473 2474 2475
};

#endif
2476

2477
/* this is ugly i know, but the alternative is too make
2478 2479
   hundreds of vars global and prefix them with ff_mpeg1_
   which is far uglier. */
2480
#include "mdec.c"