mpeg12.c 86.4 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
static inline int mpeg1_decode_block_inter(MpegEncContext *s,
                              DCTELEM *block,
53
                              int n);
54
static inline int mpeg1_fast_decode_block_inter(MpegEncContext *s, DCTELEM *block, int n);
55 56
static inline int mpeg2_decode_block_non_intra(MpegEncContext *s,
                                        DCTELEM *block,
Fabrice Bellard's avatar
Fabrice Bellard committed
57
                                        int n);
58 59
static inline int mpeg2_decode_block_intra(MpegEncContext *s,
                                    DCTELEM *block,
Fabrice Bellard's avatar
Fabrice Bellard committed
60
                                    int n);
61
static inline int mpeg2_fast_decode_block_non_intra(MpegEncContext *s, DCTELEM *block, int n);
62
static inline int mpeg2_fast_decode_block_intra(MpegEncContext *s, DCTELEM *block, int n);
Fabrice Bellard's avatar
Fabrice Bellard committed
63
static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred);
Ivan Kalvachev's avatar
Ivan Kalvachev committed
64
static void exchange_uv(MpegEncContext *s);
Fabrice Bellard's avatar
Fabrice Bellard committed
65

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

71
uint8_t ff_mpeg12_static_rl_table_store[2][2][2*MAX_RUN + MAX_LEVEL + 3];
72

73 74 75 76 77 78 79 80 81 82 83 84

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

85
static void init_2d_vlc_rl(RLTable *rl)
86
{
87
    int i;
88

89 90 91 92
    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;
93

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

118
void ff_mpeg12_common_init(MpegEncContext *s)
Fabrice Bellard's avatar
Fabrice Bellard committed
119
{
120

121
    s->y_dc_scale_table=
122
    s->c_dc_scale_table= mpeg2_dc_scale_table[s->intra_dc_precision];
Michael Niedermayer's avatar
Michael Niedermayer committed
123

124
}
125

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

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

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

143
av_cold void ff_mpeg12_init_vlcs(void)
Fabrice Bellard's avatar
Fabrice Bellard committed
144 145 146 147
{
    static int done = 0;

    if (!done) {
148
        done = 1;
Fabrice Bellard's avatar
Fabrice Bellard committed
149

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

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

175 176
        INIT_2D_VLC_RL(ff_rl_mpeg1, 680);
        INIT_2D_VLC_RL(ff_rl_mpeg2, 674);
Fabrice Bellard's avatar
Fabrice Bellard committed
177 178 179 180 181
    }
}

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

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

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

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

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

212
    assert(s->mb_skipped==0);
213

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

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

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

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

Fabrice Bellard's avatar
Fabrice Bellard committed
236 237 238 239 240
        return 0;
    }

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

274 275 276
        if(!s->chroma_y_shift){
            s->dsp.clear_blocks(s->block[6]);
        }
277

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

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

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

            s->mv[0][0][0]= s->last_mv[0][0][0]= s->last_mv[0][1][0] =
293
                mpeg_decode_motion(s, s->mpeg_f_code[0][0], s->last_mv[0][0][0]);
294
            s->mv[0][0][1]= s->last_mv[0][0][1]= s->last_mv[0][1][1] =
295 296
                mpeg_decode_motion(s, s->mpeg_f_code[0][1], s->last_mv[0][0][1]);

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

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

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

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

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

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

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

497
        s->mb_intra = 0;
498
        if (HAS_CBP(mb_type)) {
499
            s->dsp.clear_blocks(s->block[0]);
500

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

512
            //if 1, we memcpy blocks in xvmcvideo
513
            if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1){
514
                ff_xvmc_pack_pblocks(s,cbp);
515 516 517
                if(s->swap_uv){
                    exchange_uv(s);
                }
518
            }
519

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

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

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

Fabrice Bellard's avatar
Fabrice Bellard committed
573 574 575
    return 0;
}

576
/* as H.263, but only 17 codes */
Fabrice Bellard's avatar
Fabrice Bellard committed
577 578
static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred)
{
579
    int code, sign, val, l, shift;
Fabrice Bellard's avatar
Fabrice Bellard committed
580

581
    code = get_vlc2(&s->gb, mv_vlc.table, MV_VLC_BITS, 2);
Fabrice Bellard's avatar
Fabrice Bellard committed
582 583 584
    if (code == 0) {
        return pred;
    }
585 586 587 588
    if (code < 0) {
        return 0xffff;
    }

589
    sign = get_bits1(&s->gb);
Fabrice Bellard's avatar
Fabrice Bellard committed
590
    shift = fcode - 1;
591 592 593
    val = code;
    if (shift) {
        val = (val - 1) << shift;
Fabrice Bellard's avatar
Fabrice Bellard committed
594
        val |= get_bits(&s->gb, shift);
595 596
        val++;
    }
Fabrice Bellard's avatar
Fabrice Bellard committed
597 598 599
    if (sign)
        val = -val;
    val += pred;
600

Fabrice Bellard's avatar
Fabrice Bellard committed
601
    /* modulo decoding */
Michael Niedermayer's avatar
Michael Niedermayer committed
602 603
    l= INT_BIT - 5 - shift;
    val = (val<<l)>>l;
Fabrice Bellard's avatar
Fabrice Bellard committed
604 605 606
    return val;
}

607
inline int ff_mpeg1_decode_block_intra(MpegEncContext *s,
608
                               DCTELEM *block,
Fabrice Bellard's avatar
Fabrice Bellard committed
609 610 611
                               int n)
{
    int level, dc, diff, i, j, run;
612
    int component;
613
    RLTable *rl = &ff_rl_mpeg1;
614 615
    uint8_t * const scantable= s->intra_scantable.permutated;
    const uint16_t *quant_matrix= s->intra_matrix;
616
    const int qscale= s->qscale;
Fabrice Bellard's avatar
Fabrice Bellard committed
617

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

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

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

680 681
static inline int mpeg1_decode_block_inter(MpegEncContext *s,
                               DCTELEM *block,
682 683 684
                               int n)
{
    int level, i, j, run;
685
    RLTable *rl = &ff_rl_mpeg1;
686 687
    uint8_t * const scantable= s->intra_scantable.permutated;
    const uint16_t *quant_matrix= s->inter_matrix;
688 689 690
    const int qscale= s->qscale;

    {
691
        OPEN_READER(re, &s->gb);
692
        i = -1;
693
        // special case for first coefficient, no need to add second VLC table
694
        UPDATE_CACHE(re, &s->gb);
695
        if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
Michael Niedermayer's avatar
Michael Niedermayer committed
696
            level= (3*qscale*quant_matrix[0])>>5;
697
            level= (level-1)|1;
698
            if(GET_CACHE(re, &s->gb)&0x40000000)
699
                level= -level;
700
            block[0] = level;
701
            i++;
702 703 704
            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
705
        }
706 707 708
#if MIN_CACHE_BITS < 19
        UPDATE_CACHE(re, &s->gb);
#endif
709
        /* now quantify & encode AC coefficients */
710
        for(;;) {
711
            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
712

713
            if(level != 0) {
714 715
                i += run;
                j = scantable[i];
Michael Niedermayer's avatar
Michael Niedermayer committed
716
                level= ((level*2+1)*qscale*quant_matrix[j])>>5;
717 718
                level= (level-1)|1;
                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
719
                SKIP_BITS(re, &s->gb, 1);
720 721 722 723 724 725
            } 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) {
726
                    level = SHOW_UBITS(re, &s->gb, 8) - 256; SKIP_BITS(re, &s->gb, 8);
727
                } else if (level == 0) {
728
                    level = SHOW_UBITS(re, &s->gb, 8)      ; SKIP_BITS(re, &s->gb, 8);
729 730 731 732 733
                }
                i += run;
                j = scantable[i];
                if(level<0){
                    level= -level;
Michael Niedermayer's avatar
Michael Niedermayer committed
734
                    level= ((level*2+1)*qscale*quant_matrix[j])>>5;
735 736 737
                    level= (level-1)|1;
                    level= -level;
                }else{
Michael Niedermayer's avatar
Michael Niedermayer committed
738
                    level= ((level*2+1)*qscale*quant_matrix[j])>>5;
739 740
                    level= (level-1)|1;
                }
Fabrice Bellard's avatar
Fabrice Bellard committed
741
            }
742
            if (i > 63){
743
                av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
744 745 746 747
                return -1;
            }

            block[j] = level;
748 749 750
#if MIN_CACHE_BITS < 19
            UPDATE_CACHE(re, &s->gb);
#endif
751 752
            if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
                break;
753
#if MIN_CACHE_BITS >= 19
754
            UPDATE_CACHE(re, &s->gb);
755
#endif
Fabrice Bellard's avatar
Fabrice Bellard committed
756
        }
757 758
end:
        LAST_SKIP_BITS(re, &s->gb, 2);
759
        CLOSE_READER(re, &s->gb);
Fabrice Bellard's avatar
Fabrice Bellard committed
760
    }
761
    s->block_last_index[n] = i;
Fabrice Bellard's avatar
Fabrice Bellard committed
762 763 764
    return 0;
}

765 766 767
static inline int mpeg1_fast_decode_block_inter(MpegEncContext *s, DCTELEM *block, int n)
{
    int level, i, j, run;