mpeg12.c 84.3 KB
Newer Older
Fabrice Bellard's avatar
Fabrice Bellard committed
1
/*
2
 * MPEG1/2 decoder
Fabrice Bellard's avatar
Fabrice Bellard committed
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
Fabrice Bellard's avatar
Fabrice Bellard committed
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
Fabrice Bellard's avatar
Fabrice Bellard committed
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
 *
Fabrice Bellard's avatar
Fabrice Bellard committed
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
static const enum PixelFormat pixfmt_yuv_420[]= {PIX_FMT_YUV420P,PIX_FMT_NONE};
static const enum PixelFormat pixfmt_yuv_422[]= {PIX_FMT_YUV422P,PIX_FMT_NONE};
static const enum PixelFormat pixfmt_yuv_444[]= {PIX_FMT_YUV444P,PIX_FMT_NONE};
Måns Rullgård's avatar
Måns Rullgård committed
76
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
                                           PIX_FMT_NONE};
80

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

83 84 85 86 87 88 89 90 91 92 93 94

#define INIT_2D_VLC_RL(rl, static_size)\
{\
    static RL_VLC_ELEM rl_vlc_table[static_size];\
    INIT_VLC_STATIC(&rl.vlc, TEX_VLC_BITS, rl.n + 2,\
             &rl.table_vlc[0][1], 4, 2,\
             &rl.table_vlc[0][0], 4, 2, static_size);\
\
    rl.rl_vlc[0]= rl_vlc_table;\
    init_2d_vlc_rl(&rl);\
}

95
static void init_2d_vlc_rl(RLTable *rl)
96
{
97
    int i;
98

99 100 101 102
    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;
103

104 105 106 107 108 109 110 111 112 113 114
        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
115 116
                run= 0;
                level= 127;
117 118 119 120 121 122 123 124 125 126 127
            }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;
    }
}

128
void ff_mpeg12_common_init(MpegEncContext *s)
Fabrice Bellard's avatar
Fabrice Bellard committed
129
{
130

131
    s->y_dc_scale_table=
132
    s->c_dc_scale_table= mpeg2_dc_scale_table[s->intra_dc_precision];
Michael Niedermayer's avatar
Michael Niedermayer committed
133

134
}
135

Michael Niedermayer's avatar
Michael Niedermayer committed
136 137 138 139 140 141 142
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
143 144 145 146 147 148 149 150 151 152 153 154

/******************************************/
/* 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
155
static void init_vlcs(void)
Fabrice Bellard's avatar
Fabrice Bellard committed
156 157 158 159
{
    static int done = 0;

    if (!done) {
160
        done = 1;
Fabrice Bellard's avatar
Fabrice Bellard committed
161

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

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

187 188
        INIT_2D_VLC_RL(ff_rl_mpeg1, 680);
        INIT_2D_VLC_RL(ff_rl_mpeg2, 674);
Fabrice Bellard's avatar
Fabrice Bellard committed
189 190 191 192 193
    }
}

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

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

Fabrice Bellard's avatar
Fabrice Bellard committed
210 211 212 213 214 215 216
/* 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,
217
                          DCTELEM block[12][64])
Fabrice Bellard's avatar
Fabrice Bellard committed
218
{
Michael Niedermayer's avatar
Michael Niedermayer committed
219
    int i, j, k, cbp, val, mb_type, motion_type;
220
    const int mb_block_count = 4 + (1<< s->chroma_format);
221

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

224
    assert(s->mb_skipped==0);
225

226
    if (s->mb_skip_run-- != 0) {
227
        if (s->pict_type == FF_P_TYPE) {
228
            s->mb_skipped = 1;
229
            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
230
        } else {
231
            int mb_type;
232

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

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

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

Fabrice Bellard's avatar
Fabrice Bellard committed
248 249 250 251 252
        return 0;
    }

    switch(s->pict_type) {
    default:
253
    case FF_I_TYPE:
254
        if (get_bits1(&s->gb) == 0) {
255
            if (get_bits1(&s->gb) == 0){
256
                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
257
                return -1;
258
            }
259
            mb_type = MB_TYPE_QUANT | MB_TYPE_INTRA;
Fabrice Bellard's avatar
Fabrice Bellard committed
260
        } else {
261
            mb_type = MB_TYPE_INTRA;
Fabrice Bellard's avatar
Fabrice Bellard committed
262 263
        }
        break;
264
    case FF_P_TYPE:
265
        mb_type = get_vlc2(&s->gb, mb_ptype_vlc.table, MB_PTYPE_VLC_BITS, 1);
266
        if (mb_type < 0){
267
            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
268
            return -1;
269
        }
270
        mb_type = ptype2mb_type[ mb_type ];
Fabrice Bellard's avatar
Fabrice Bellard committed
271
        break;
272
    case FF_B_TYPE:
273
        mb_type = get_vlc2(&s->gb, mb_btype_vlc.table, MB_BTYPE_VLC_BITS, 1);
274
        if (mb_type < 0){
275
            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
276
            return -1;
277
        }
278
        mb_type = btype2mb_type[ mb_type ];
Fabrice Bellard's avatar
Fabrice Bellard committed
279 280
        break;
    }
Michel Bardiaux's avatar
Michel Bardiaux committed
281
    dprintf(s->avctx, "mb_type=%x\n", mb_type);
282 283
//    motion_type = 0; /* avoid warning */
    if (IS_INTRA(mb_type)) {
284
        s->dsp.clear_blocks(s->block[0]);
285

286 287 288
        if(!s->chroma_y_shift){
            s->dsp.clear_blocks(s->block[6]);
        }
289

290 291 292 293 294
        /* 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
295

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

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

            s->mv[0][0][0]= s->last_mv[0][0][0]= s->last_mv[0][1][0] =
305
                mpeg_decode_motion(s, s->mpeg_f_code[0][0], s->last_mv[0][0][0]);
306
            s->mv[0][0][1]= s->last_mv[0][0][1]= s->last_mv[0][1][1] =
307 308
                mpeg_decode_motion(s, s->mpeg_f_code[0][1], s->last_mv[0][0][1]);

309
            skip_bits1(&s->gb); /* marker */
310 311
        }else
            memset(s->last_mv, 0, sizeof(s->last_mv)); /* reset mv prediction */
Fabrice Bellard's avatar
Fabrice Bellard committed
312
        s->mb_intra = 1;
313 314 315 316 317 318 319 320 321
#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
322

Michael Niedermayer's avatar
Michael Niedermayer committed
323
        if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
324 325 326 327 328 329 330 331 332
            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;
                }
333 334 335
            }
        } else {
            for(i=0;i<6;i++) {
336
                if (mpeg1_decode_block_intra(s, s->pblocks[i], i) < 0)
337 338 339
                    return -1;
            }
        }
Fabrice Bellard's avatar
Fabrice Bellard committed
340
    } else {
341
        if (mb_type & MB_TYPE_ZERO_MV){
342
            assert(mb_type & MB_TYPE_CBP);
343 344

            s->mv_dir = MV_DIR_FORWARD;
Michael Niedermayer's avatar
Michael Niedermayer committed
345 346 347
            if(s->picture_structure == PICT_FRAME){
                if(!s->frame_pred_frame_dct)
                    s->interlaced_dct = get_bits1(&s->gb);
348
                s->mv_type = MV_TYPE_16X16;
Michael Niedermayer's avatar
Michael Niedermayer committed
349
            }else{
350 351 352 353
                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
354 355 356 357

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

358 359 360 361 362 363 364 365 366
            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
367
            /* get additional motion vector type */
368
            if (s->frame_pred_frame_dct)
369 370 371
                motion_type = MT_FRAME;
            else{
                motion_type = get_bits(&s->gb, 2);
Michael Niedermayer's avatar
Michael Niedermayer committed
372 373
                if (s->picture_structure == PICT_FRAME && HAS_CBP(mb_type))
                    s->interlaced_dct = get_bits1(&s->gb);
374 375 376 377 378 379
            }

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

            /* motion vectors */
Michael Niedermayer's avatar
Michael Niedermayer committed
380
            s->mv_dir= (mb_type>>13)&3;
Michael Niedermayer's avatar
change  
Michael Niedermayer committed
381 382 383
            dprintf(s->avctx, "motion_type=%d\n", motion_type);
            switch(motion_type) {
            case MT_FRAME: /* or MT_16X8 */
384 385 386 387 388
                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)) {
389
                            /* MT_FRAME */
390
                            s->mv[i][0][0]= s->last_mv[i][0][0]= s->last_mv[i][1][0] =
391
                                mpeg_decode_motion(s, s->mpeg_f_code[i][0], s->last_mv[i][0][0]);
392
                            s->mv[i][0][1]= s->last_mv[i][0][1]= s->last_mv[i][1][1] =
393
                                mpeg_decode_motion(s, s->mpeg_f_code[i][1], s->last_mv[i][0][1]);
Fabrice Bellard's avatar
Fabrice Bellard committed
394
                            /* full_pel: only for mpeg1 */
395 396 397 398
                            if (s->full_pel[i]){
                                s->mv[i][0][0] <<= 1;
                                s->mv[i][0][1] <<= 1;
                            }
399 400 401 402 403 404 405
                        }
                    }
                } 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)) {
406 407 408 409 410 411 412 413 414 415
                            /* 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
416
                        }
Michael Niedermayer's avatar
change  
Michael Niedermayer committed
417 418 419 420 421
                    }
                }
                break;
            case MT_FIELD:
                s->mv_type = MV_TYPE_FIELD;
422 423 424 425
                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)) {
426 427 428 429 430 431
                            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;
Michel Bardiaux's avatar
Michel Bardiaux committed
432
                                dprintf(s->avctx, "fmx=%d\n", val);
433 434 435 436
                                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;
Michel Bardiaux's avatar
Michel Bardiaux committed
437
                                dprintf(s->avctx, "fmy=%d\n", val);
438
                            }
439 440 441 442 443 444
                        }
                    }
                } else {
                    mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
                    for(i=0;i<2;i++) {
                        if (USES_LIST(mb_type, i)) {
445
                            s->field_select[i][0] = get_bits1(&s->gb);
Fabrice Bellard's avatar
Fabrice Bellard committed
446 447
                            for(k=0;k<2;k++) {
                                val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
448 449 450 451
                                                         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
452 453
                            }
                        }
Michael Niedermayer's avatar
change  
Michael Niedermayer committed
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 498 499 500
                    }
                }
                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
501 502 503
                        }
                    }
                }
Michael Niedermayer's avatar
change  
Michael Niedermayer committed
504 505 506 507
                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
508 509
            }
        }
510

511
        s->mb_intra = 0;
512
        if (HAS_CBP(mb_type)) {
513
            s->dsp.clear_blocks(s->block[0]);
514

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

526 527 528 529 530 531 532
#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);
                }
533
            }
534 535
#endif

Michael Niedermayer's avatar
Michael Niedermayer committed
536
            if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
537 538 539 540 541 542 543 544 545 546 547
                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;
548

549 550 551 552 553 554 555 556
                    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;
557
                    }
Fabrice Bellard's avatar
Fabrice Bellard committed
558
                }
559
            } else {
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
                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;
578
                    }
579
                }
Fabrice Bellard's avatar
Fabrice Bellard committed
580
            }
581
        }else{
582
            for(i=0;i<12;i++)
583
                s->block_last_index[i] = -1;
Fabrice Bellard's avatar
Fabrice Bellard committed
584 585
        }
    }
586 587 588

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

Fabrice Bellard's avatar
Fabrice Bellard committed
589 590 591 592 593 594
    return 0;
}

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

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

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

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

623
static inline int decode_dc(GetBitContext *gb, int component)
Fabrice Bellard's avatar
Fabrice Bellard committed
624 625 626 627
{
    int code, diff;

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

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

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

673 674 675 676 677
            if(level == 127){
                break;
            } else if(level != 0) {
                i += run;
                j = scantable[i];
Michael Niedermayer's avatar
Michael Niedermayer committed
678
                level= (level*qscale*quant_matrix[j])>>4;
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
                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
696
                    level= (level*qscale*quant_matrix[j])>>4;
697 698 699
                    level= (level-1)|1;
                    level= -level;
                }else{
Michael Niedermayer's avatar
Michael Niedermayer committed
700
                    level= (level*qscale*quant_matrix[j])>>4;
701 702 703 704
                    level= (level-1)|1;
                }
            }
            if (i > 63){
705
                av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
706 707 708 709 710 711 712 713 714 715 716
                return -1;
            }

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

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

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

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

            block[j] = level;
785 786 787
#if MIN_CACHE_BITS < 19
            UPDATE_CACHE(re, &s->gb);
#endif
788 789
            if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
                break;
790
#if MIN_CACHE_BITS >= 19
791
            UPDATE_CACHE(re, &s->gb);
792
#endif
Fabrice Bellard's avatar
Fabrice Bellard committed
793
        }