mpeg12.c 89.3 KB
Newer Older
Fabrice Bellard's avatar
Fabrice Bellard committed
1
/*
2
 * MPEG-1/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
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 libavcodec/mpeg12.c
25
 * MPEG-1/2 decoder
Michael Niedermayer's avatar
Michael Niedermayer committed
26
 */
27

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

34
#include "mpeg12.h"
Fabrice Bellard's avatar
Fabrice Bellard committed
35
#include "mpeg12data.h"
36
#include "mpeg12decdata.h"
37
#include "bytestream.h"
38
#include "vdpau_internal.h"
39
#include "xvmc_internal.h"
Fabrice Bellard's avatar
Fabrice Bellard committed
40

41 42 43
//#undef NDEBUG
//#include <assert.h>

44

45 46 47 48 49 50
#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

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

Måns Rullgård's avatar
Måns Rullgård committed
69
static const enum PixelFormat pixfmt_xvmc_mpg2_420[] = {
Ivan Kalvachev's avatar
Ivan Kalvachev committed
70 71
                                           PIX_FMT_XVMC_MPEG2_IDCT,
                                           PIX_FMT_XVMC_MPEG2_MC,
72
                                           PIX_FMT_NONE};
73

74
uint8_t ff_mpeg12_static_rl_table_store[2][2][2*MAX_RUN + MAX_LEVEL + 3];
75

76 77 78 79 80 81 82 83 84 85 86 87

#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);\
}

88
static void init_2d_vlc_rl(RLTable *rl)
89
{
90
    int i;
91

92 93 94 95
    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;
96

97 98 99 100 101 102 103 104 105 106 107
        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
108 109
                run= 0;
                level= 127;
110 111 112 113 114 115 116 117 118 119 120
            }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;
    }
}

121
void ff_mpeg12_common_init(MpegEncContext *s)
Fabrice Bellard's avatar
Fabrice Bellard committed
122
{
123

124
    s->y_dc_scale_table=
125
    s->c_dc_scale_table= ff_mpeg2_dc_scale_table[s->intra_dc_precision];
Michael Niedermayer's avatar
Michael Niedermayer committed
126

127
}
128

Michael Niedermayer's avatar
Michael Niedermayer committed
129 130 131 132 133 134 135
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
136 137 138 139 140 141 142 143 144 145

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

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

146
av_cold void ff_mpeg12_init_vlcs(void)
Fabrice Bellard's avatar
Fabrice Bellard committed
147 148 149 150
{
    static int done = 0;

    if (!done) {
151
        done = 1;
Fabrice Bellard's avatar
Fabrice Bellard committed
152

153
        INIT_VLC_STATIC(&dc_lum_vlc, DC_VLC_BITS, 12,
154
                 ff_mpeg12_vlc_dc_lum_bits, 1, 1,
155 156
                 ff_mpeg12_vlc_dc_lum_code, 2, 2, 512);
        INIT_VLC_STATIC(&dc_chroma_vlc,  DC_VLC_BITS, 12,
157
                 ff_mpeg12_vlc_dc_chroma_bits, 1, 1,
158 159
                 ff_mpeg12_vlc_dc_chroma_code, 2, 2, 514);
        INIT_VLC_STATIC(&mv_vlc, MV_VLC_BITS, 17,
160
                 &ff_mpeg12_mbMotionVectorTable[0][1], 2, 1,
161 162
                 &ff_mpeg12_mbMotionVectorTable[0][0], 2, 1, 518);
        INIT_VLC_STATIC(&mbincr_vlc, MBINCR_VLC_BITS, 36,
163
                 &ff_mpeg12_mbAddrIncrTable[0][1], 2, 1,
164 165
                 &ff_mpeg12_mbAddrIncrTable[0][0], 2, 1, 538);
        INIT_VLC_STATIC(&mb_pat_vlc, MB_PAT_VLC_BITS, 64,
166
                 &ff_mpeg12_mbPatTable[0][1], 2, 1,
167
                 &ff_mpeg12_mbPatTable[0][0], 2, 1, 512);
168

169
        INIT_VLC_STATIC(&mb_ptype_vlc, MB_PTYPE_VLC_BITS, 7,
Fabrice Bellard's avatar
Fabrice Bellard committed
170
                 &table_mb_ptype[0][1], 2, 1,
171 172
                 &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
173
                 &table_mb_btype[0][1], 2, 1,
174
                 &table_mb_btype[0][0], 2, 1, 64);
175 176
        init_rl(&ff_rl_mpeg1, ff_mpeg12_static_rl_table_store[0]);
        init_rl(&ff_rl_mpeg2, ff_mpeg12_static_rl_table_store[1]);
177

178 179
        INIT_2D_VLC_RL(ff_rl_mpeg1, 680);
        INIT_2D_VLC_RL(ff_rl_mpeg2, 674);
Fabrice Bellard's avatar
Fabrice Bellard committed
180 181 182 183 184
    }
}

static inline int get_dmv(MpegEncContext *s)
{
185
    if(get_bits1(&s->gb))
186
        return 1 - (get_bits1(&s->gb) << 1);
Fabrice Bellard's avatar
Fabrice Bellard committed
187 188 189 190
    else
        return 0;
}

Fabrice Bellard's avatar
Fabrice Bellard committed
191 192
static inline int get_qscale(MpegEncContext *s)
{
193
    int qscale = get_bits(&s->gb, 5);
Michael Niedermayer's avatar
Michael Niedermayer committed
194 195 196 197
    if (s->q_scale_type) {
        return non_linear_qscale[qscale];
    } else {
        return qscale << 1;
Fabrice Bellard's avatar
Fabrice Bellard committed
198 199 200
    }
}

201
/* motion type (for MPEG-2) */
Fabrice Bellard's avatar
Fabrice Bellard committed
202 203 204 205 206 207
#define MT_FIELD 1
#define MT_FRAME 2
#define MT_16X8  2
#define MT_DMV   3

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

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

215
    assert(s->mb_skipped==0);
216

217
    if (s->mb_skip_run-- != 0) {
218
        if (s->pict_type == FF_P_TYPE) {
219
            s->mb_skipped = 1;
220
            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
221
        } else {
222
            int mb_type;
223

224 225 226
            if(s->mb_x)
                mb_type= s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride - 1];
            else
227
                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
228 229
            if(IS_INTRA(mb_type))
                return -1;
230 231

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

235
            if((s->mv[0][0][0]|s->mv[0][0][1]|s->mv[1][0][0]|s->mv[1][0][1])==0)
236
                s->mb_skipped = 1;
Fabrice Bellard's avatar
Fabrice Bellard committed
237
        }
Michael Niedermayer's avatar
Michael Niedermayer committed
238

Fabrice Bellard's avatar
Fabrice Bellard committed
239 240 241 242 243
        return 0;
    }

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

277 278 279
        if(!s->chroma_y_shift){
            s->dsp.clear_blocks(s->block[6]);
        }
280

281 282
        /* compute DCT type */
        if (s->picture_structure == PICT_FRAME && //FIXME add an interlaced_dct coded var?
283 284 285
            !s->frame_pred_frame_dct) {
            s->interlaced_dct = get_bits1(&s->gb);
        }
Fabrice Bellard's avatar
Fabrice Bellard committed
286

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

Fabrice Bellard's avatar
Fabrice Bellard committed
290 291
        if (s->concealment_motion_vectors) {
            /* just parse them */
292
            if (s->picture_structure != PICT_FRAME)
293
                skip_bits1(&s->gb); /* field select */
294 295

            s->mv[0][0][0]= s->last_mv[0][0][0]= s->last_mv[0][1][0] =
296
                mpeg_decode_motion(s, s->mpeg_f_code[0][0], s->last_mv[0][0][0]);
297
            s->mv[0][0][1]= s->last_mv[0][0][1]= s->last_mv[0][1][1] =
298 299
                mpeg_decode_motion(s, s->mpeg_f_code[0][1], s->last_mv[0][0][1]);

300
            skip_bits1(&s->gb); /* marker */
301 302
        }else
            memset(s->last_mv, 0, sizeof(s->last_mv)); /* reset mv prediction */
Fabrice Bellard's avatar
Fabrice Bellard committed
303
        s->mb_intra = 1;
304
        //if 1, we memcpy blocks in xvmcvideo
305
        if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1){
306
            ff_xvmc_pack_pblocks(s,-1);//inter are always full blocks
307 308 309 310
            if(s->swap_uv){
                exchange_uv(s);
            }
        }
311

Michael Niedermayer's avatar
Michael Niedermayer committed
312
        if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
313 314
            if(s->flags2 & CODEC_FLAG2_FAST){
                for(i=0;i<6;i++) {
315
                    mpeg2_fast_decode_block_intra(s, *s->pblocks[i], i);
316 317 318
                }
            }else{
                for(i=0;i<mb_block_count;i++) {
319
                    if (mpeg2_decode_block_intra(s, *s->pblocks[i], i) < 0)
320 321
                        return -1;
                }
322 323 324
            }
        } else {
            for(i=0;i<6;i++) {
325
                if (mpeg1_decode_block_intra(s, *s->pblocks[i], i) < 0)
326 327 328
                    return -1;
            }
        }
Fabrice Bellard's avatar
Fabrice Bellard committed
329
    } else {
330
        if (mb_type & MB_TYPE_ZERO_MV){
331
            assert(mb_type & MB_TYPE_CBP);
332 333

            s->mv_dir = MV_DIR_FORWARD;
Michael Niedermayer's avatar
Michael Niedermayer committed
334 335 336
            if(s->picture_structure == PICT_FRAME){
                if(!s->frame_pred_frame_dct)
                    s->interlaced_dct = get_bits1(&s->gb);
337
                s->mv_type = MV_TYPE_16X16;
Michael Niedermayer's avatar
Michael Niedermayer committed
338
            }else{
339 340 341 342
                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
343 344 345 346

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

347 348 349 350 351 352 353 354 355
            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
356
            /* get additional motion vector type */
357
            if (s->frame_pred_frame_dct)
358 359 360
                motion_type = MT_FRAME;
            else{
                motion_type = get_bits(&s->gb, 2);
Michael Niedermayer's avatar
Michael Niedermayer committed
361 362
                if (s->picture_structure == PICT_FRAME && HAS_CBP(mb_type))
                    s->interlaced_dct = get_bits1(&s->gb);
363 364 365 366 367 368
            }

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

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

Michael Niedermayer's avatar
change  
Michael Niedermayer committed
453 454 455 456 457 458
                        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],
459
                                                s->last_mv[i][0][1] >> my_shift);
Michael Niedermayer's avatar
change  
Michael Niedermayer committed
460 461 462
                        dmy = get_dmv(s);


463 464
                        s->last_mv[i][0][1] = my<<my_shift;
                        s->last_mv[i][1][1] = my<<my_shift;
Michael Niedermayer's avatar
change  
Michael Niedermayer committed
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

                        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
492 493 494
                        }
                    }
                }
Michael Niedermayer's avatar
change  
Michael Niedermayer committed
495 496 497 498
                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
499 500
            }
        }
501

502
        s->mb_intra = 0;
503
        if (HAS_CBP(mb_type)) {
504
            s->dsp.clear_blocks(s->block[0]);
505

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

517
            //if 1, we memcpy blocks in xvmcvideo
518
            if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1){
519
                ff_xvmc_pack_pblocks(s,cbp);
520 521 522
                if(s->swap_uv){
                    exchange_uv(s);
                }
523
            }
524

Michael Niedermayer's avatar
Michael Niedermayer committed
525
            if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
526 527 528
                if(s->flags2 & CODEC_FLAG2_FAST){
                    for(i=0;i<6;i++) {
                        if(cbp & 32) {
529
                            mpeg2_fast_decode_block_non_intra(s, *s->pblocks[i], i);
530 531 532 533 534 535 536
                        } else {
                            s->block_last_index[i] = -1;
                        }
                        cbp+=cbp;
                    }
                }else{
                    cbp<<= 12-mb_block_count;
537

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

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

Fabrice Bellard's avatar
Fabrice Bellard committed
578 579 580
    return 0;
}

581
/* as H.263, but only 17 codes */
Fabrice Bellard's avatar
Fabrice Bellard committed
582 583
static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred)
{
584
    int code, sign, val, l, shift;
Fabrice Bellard's avatar
Fabrice Bellard committed
585

586
    code = get_vlc2(&s->gb, mv_vlc.table, MV_VLC_BITS, 2);
Fabrice Bellard's avatar
Fabrice Bellard committed
587 588 589
    if (code == 0) {
        return pred;
    }
590 591 592 593
    if (code < 0) {
        return 0xffff;
    }

594
    sign = get_bits1(&s->gb);
Fabrice Bellard's avatar
Fabrice Bellard committed
595
    shift = fcode - 1;
596 597 598
    val = code;
    if (shift) {
        val = (val - 1) << shift;
Fabrice Bellard's avatar
Fabrice Bellard committed
599
        val |= get_bits(&s->gb, shift);
600 601
        val++;
    }
Fabrice Bellard's avatar
Fabrice Bellard committed
602 603 604
    if (sign)
        val = -val;
    val += pred;
605

Fabrice Bellard's avatar
Fabrice Bellard committed
606
    /* modulo decoding */
Michael Niedermayer's avatar
Michael Niedermayer committed
607 608
    l= INT_BIT - 5 - shift;
    val = (val<<l)>>l;
Fabrice Bellard's avatar
Fabrice Bellard committed
609 610 611
    return val;
}

612
static inline int mpeg1_decode_block_intra(MpegEncContext *s,
613
                               DCTELEM *block,
Fabrice Bellard's avatar
Fabrice Bellard committed
614 615 616
                               int n)
{
    int level, dc, diff, i, j, run;
617
    int component;
618
    RLTable *rl = &ff_rl_mpeg1;
619 620
    uint8_t * const scantable= s->intra_scantable.permutated;
    const uint16_t *quant_matrix= s->intra_matrix;
621
    const int qscale= s->qscale;
Fabrice Bellard's avatar
Fabrice Bellard committed
622

623
    /* DC coefficient */
624
    component = (n <= 3 ? 0 : n - 4 + 1);
625
    diff = decode_dc(&s->gb, component);
626 627 628 629 630
    if (diff >= 0xffff)
        return -1;
    dc = s->last_dc[component];
    dc += diff;
    s->last_dc[component] = dc;
631
    block[0] = dc*quant_matrix[0];
Michel Bardiaux's avatar
Michel Bardiaux committed
632
    dprintf(s->avctx, "dc=%d diff=%d\n", dc, diff);
633 634
    i = 0;
    {
635
        OPEN_READER(re, &s->gb);
636
        /* now quantify & encode AC coefficients */
637 638
        for(;;) {
            UPDATE_CACHE(re, &s->gb);
639
            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
640

641 642 643 644 645
            if(level == 127){
                break;
            } else if(level != 0) {
                i += run;
                j = scantable[i];
Michael Niedermayer's avatar
Michael Niedermayer committed
646
                level= (level*qscale*quant_matrix[j])>>4;
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
                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
664
                    level= (level*qscale*quant_matrix[j])>>4;
665 666 667
                    level= (level-1)|1;
                    level= -level;
                }else{
Michael Niedermayer's avatar
Michael Niedermayer committed
668
                    level= (level*qscale*quant_matrix[j])>>4;
669 670 671 672
                    level= (level-1)|1;
                }
            }
            if (i > 63){
673
                av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
674 675 676 677 678 679 680 681 682 683 684
                return -1;
            }

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

685 686 687 688 689 690 691
int ff_mpeg1_decode_block_intra(MpegEncContext *s,
                                DCTELEM *block,
                                int n)
{
    return mpeg1_decode_block_intra(s, block, n);
}

692 693
static inline int mpeg1_decode_block_inter(MpegEncContext *s,
                               DCTELEM *block,
694 695 696
                               int n)
{
    int level, i, j, run;
697
    RLTable *rl = &ff_rl_mpeg1;
698 699
    uint8_t * const scantable= s->intra_scantable.permutated;
    const uint16_t *quant_matrix= s->inter_matrix;
700 701 702
    const int qscale= s->qscale;

    {
703
        OPEN_READER(re, &s->gb);
704
        i = -1;
705
        // special case for first coefficient, no need to add second VLC table
706
        UPDATE_CACHE(re, &s->gb);
707
        if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
Michael Niedermayer's avatar
Michael Niedermayer committed
708
            level= (3*qscale*quant_matrix[0])>>5;
709
            level= (level-1)|1;
710
            if(GET_CACHE(re, &s->gb)&0x40000000)
711
                level= -level;
712
            block[0] = level;
713
            i++;
714 715 716
            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
717
        }
718 719 720
#if MIN_CACHE_BITS < 19
        UPDATE_CACHE(re, &s->gb);
#endif
721
        /* now quantify & encode AC coefficients */
722
        for(;;) {
723
            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
724

725
            if(level != 0) {
726 727
                i += run;
                j = scantable[i];
Michael Niedermayer's avatar
Michael Niedermayer committed
728
                level= ((level*2+1)*qscale*quant_matrix[j])>>5;
729 730
                level= (level-1)|1;
                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
731
                SKIP_BITS(re, &s->gb, 1);
732 733 734 735 736 737
            } 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) {
738
                    level = SHOW_UBITS(re, &s->gb, 8) - 256; SKIP_BITS(re, &s->gb, 8);
739
                } else if (level == 0) {
740
                    level = SHOW_UBITS(re, &s->gb, 8)      ; SKIP_BITS(re, &s->gb, 8);
741 742 743 744 745
                }
                i += run;
                j = scantable[i];
                if(level<0){
                    level= -level;
Michael Niedermayer's avatar
Michael Niedermayer committed
746
                    level= ((level*2+1)*qscale*quant_matrix[j])>>5;
747 748 749
                    level= (level-1)|1;
                    level= -level;
                }else{
Michael Niedermayer's avatar
Michael Niedermayer committed
750
                    level= ((level*2+1)*qscale*quant_matrix[j])>>5;
751 752
                    level= (level-1)|1;
                }
Fabrice Bellard's avatar
Fabrice Bellard committed
753
            }
754
            if (i > 63){
755
                av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
756 757 758 759
                return -1;
            }

            block[j] = level;
760 761 762
#if MIN_CACHE_BITS < 19
            UPDATE_CACHE(re, &s->gb);
#endif
763 764
            if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
                break;
765
#if MIN_CACHE_BITS >= 19
766
            UPDATE_CACHE(re, &s->gb);