mpeg12.c 89.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
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

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

140 141 142
VLC ff_dc_lum_vlc;
VLC ff_dc_chroma_vlc;

Fabrice Bellard's avatar
Fabrice Bellard committed
143 144 145 146 147 148
static VLC mv_vlc;
static VLC mbincr_vlc;
static VLC mb_ptype_vlc;
static VLC mb_btype_vlc;
static VLC mb_pat_vlc;

149
av_cold void ff_mpeg12_init_vlcs(void)
Fabrice Bellard's avatar
Fabrice Bellard committed
150 151 152 153
{
    static int done = 0;

    if (!done) {
154
        done = 1;
Fabrice Bellard's avatar
Fabrice Bellard committed
155

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

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

181 182
        INIT_2D_VLC_RL(ff_rl_mpeg1, 680);
        INIT_2D_VLC_RL(ff_rl_mpeg2, 674);
Fabrice Bellard's avatar
Fabrice Bellard committed
183 184 185 186 187
    }
}

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

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

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

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

Luca Barbato's avatar
Luca Barbato committed
216
    av_dlog(s->avctx, "decode_mb: x=%d y=%d\n", s->mb_x, s->mb_y);
Fabrice Bellard's avatar
Fabrice Bellard committed
217

218
    assert(s->mb_skipped==0);
219

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

227 228 229
            if(s->mb_x)
                mb_type= s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride - 1];
            else
230
                mb_type= s->current_picture.mb_type[ s->mb_width + (s->mb_y-1)*s->mb_stride - 1]; // FIXME not sure if this is allowed in MPEG at all
231 232
            if(IS_INTRA(mb_type))
                return -1;
233 234

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

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

Fabrice Bellard's avatar
Fabrice Bellard committed
242 243 244 245 246
        return 0;
    }

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

280 281 282
        if(!s->chroma_y_shift){
            s->dsp.clear_blocks(s->block[6]);
        }
283

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

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

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

            s->mv[0][0][0]= s->last_mv[0][0][0]= s->last_mv[0][1][0] =
299
                mpeg_decode_motion(s, s->mpeg_f_code[0][0], s->last_mv[0][0][0]);
300
            s->mv[0][0][1]= s->last_mv[0][0][1]= s->last_mv[0][1][1] =
301 302
                mpeg_decode_motion(s, s->mpeg_f_code[0][1], s->last_mv[0][0][1]);

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

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

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

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

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

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

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

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


466 467
                        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
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

                        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
495 496 497
                        }
                    }
                }
Michael Niedermayer's avatar
change  
Michael Niedermayer committed
498 499 500 501
                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
502 503
            }
        }
504

505
        s->mb_intra = 0;
506
        if (HAS_CBP(mb_type)) {
507
            s->dsp.clear_blocks(s->block[0]);
508

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

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

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

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

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

Fabrice Bellard's avatar
Fabrice Bellard committed
581 582 583
    return 0;
}

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

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

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

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

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

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

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

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

688 689 690 691 692 693 694
int ff_mpeg1_decode_block_intra(MpegEncContext *s,
                                DCTELEM *block,
                                int n)
{
    return mpeg1_decode_block_intra(s, block, n);
}

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

    {
706
        OPEN_READER(re, &s->gb);
707
        i = -1;
708
        // special case for first coefficient, no need to add second VLC table
709
        UPDATE_CACHE(re, &s->gb);
710
        if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
Michael Niedermayer's avatar
Michael Niedermayer committed
711
            level= (3*qscale*quant_matrix[0])>>5;
712
            level= (level-1)|1;
713
            if(GET_CACHE(re, &s->gb)&0x40000000)
714
                level= -level;
715
            block[0] = level;
716
            i++;
717 718 719
            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
720
        }
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(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
                break;
            UPDATE_CACHE(re, &s->gb);
Fabrice Bellard's avatar
Fabrice Bellard committed
763
        }