mpeg12.c 85.9 KB
Newer Older
glantau's avatar
glantau committed
1
/*
2
 * MPEG-1/2 decoder
3
 * Copyright (c) 2000,2001 Fabrice Bellard
4
 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
glantau's avatar
glantau committed
5
 *
6 7 8
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
glantau's avatar
glantau 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.
glantau's avatar
glantau committed
12
 *
13
 * FFmpeg is distributed in the hope that it will be useful,
glantau's avatar
glantau committed
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
glantau's avatar
glantau committed
15 16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
glantau's avatar
glantau committed
17
 *
glantau's avatar
glantau 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
glantau's avatar
glantau committed
21
 */
22

michaelni's avatar
michaelni committed
23
/**
24
 * @file libavcodec/mpeg12.c
25
 * MPEG-1/2 decoder
michaelni's avatar
michaelni committed
26
 */
27

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

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

michael's avatar
michael committed
41 42 43
//#undef NDEBUG
//#include <assert.h>

michaelni's avatar
michaelni committed
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,
michaelni's avatar
michaelni committed
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,
glantau's avatar
glantau committed
57
                                        int n);
58 59
static inline int mpeg2_decode_block_intra(MpegEncContext *s,
                                    DCTELEM *block,
glantau's avatar
glantau committed
60
                                    int n);
61
static inline int mpeg2_fast_decode_block_non_intra(MpegEncContext *s, DCTELEM *block, int n);
michael's avatar
michael committed
62
static inline int mpeg2_fast_decode_block_intra(MpegEncContext *s, DCTELEM *block, int n);
glantau's avatar
glantau committed
63
static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred);
iive's avatar
iive committed
64
static void exchange_uv(MpegEncContext *s);
glantau's avatar
glantau committed
65

mru's avatar
mru committed
66
static const enum PixelFormat pixfmt_xvmc_mpg2_420[] = {
iive's avatar
iive 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);\
}

michael's avatar
michael committed
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
michaelni's avatar
michaelni committed
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)
glantau's avatar
glantau committed
119
{
120

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

michaelni's avatar
 
michaelni committed
124
}
125

michaelni's avatar
michaelni 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));
}

glantau's avatar
glantau 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)
glantau's avatar
glantau committed
144 145 146 147
{
    static int done = 0;

    if (!done) {
bellard's avatar
bellard committed
148
        done = 1;
glantau's avatar
glantau committed
149

michael's avatar
michael committed
150
        INIT_VLC_STATIC(&dc_lum_vlc, DC_VLC_BITS, 12,
151
                 ff_mpeg12_vlc_dc_lum_bits, 1, 1,
michael's avatar
michael committed
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,
michael's avatar
michael committed
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,
michael's avatar
michael committed
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,
michael's avatar
michael committed
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,
michael's avatar
michael committed
164
                 &ff_mpeg12_mbPatTable[0][0], 2, 1, 512);
165

michael's avatar
michael committed
166
        INIT_VLC_STATIC(&mb_ptype_vlc, MB_PTYPE_VLC_BITS, 7,
glantau's avatar
glantau committed
167
                 &table_mb_ptype[0][1], 2, 1,
michael's avatar
michael committed
168 169
                 &table_mb_ptype[0][0], 2, 1, 64);
        INIT_VLC_STATIC(&mb_btype_vlc, MB_BTYPE_VLC_BITS, 11,
glantau's avatar
glantau committed
170
                 &table_mb_btype[0][1], 2, 1,
michael's avatar
michael committed
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);
glantau's avatar
glantau 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);
glantau's avatar
glantau committed
184 185 186 187
    else
        return 0;
}

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

198
/* motion type (for MPEG-2) */
glantau's avatar
glantau 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])
glantau's avatar
glantau committed
206
{
michaelni's avatar
michaelni committed
207
    int i, j, k, cbp, val, mb_type, motion_type;
208
    const int mb_block_count = 4 + (1<< s->chroma_format);
209

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

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

michaelni's avatar
michaelni committed
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;
glantau's avatar
glantau 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;
glantau's avatar
glantau committed
234
        }
michaelni's avatar
michaelni committed
235

glantau's avatar
glantau 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) {
michaelni's avatar
michaelni committed
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);
glantau's avatar
glantau committed
245
                return -1;
michaelni's avatar
michaelni committed
246
            }
247
            mb_type = MB_TYPE_QUANT | MB_TYPE_INTRA;
glantau's avatar
glantau committed
248
        } else {
249
            mb_type = MB_TYPE_INTRA;
glantau's avatar
glantau 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);
michaelni's avatar
michaelni committed
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);
glantau's avatar
glantau committed
256
            return -1;
michaelni's avatar
michaelni committed
257
        }
258
        mb_type = ptype2mb_type[ mb_type ];
glantau's avatar
glantau committed
259
        break;
260
    case FF_B_TYPE:
261
        mb_type = get_vlc2(&s->gb, mb_btype_vlc.table, MB_BTYPE_VLC_BITS, 1);
michaelni's avatar
michaelni committed
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);
glantau's avatar
glantau committed
264
            return -1;
michaelni's avatar
michaelni committed
265
        }
266
        mb_type = btype2mb_type[ mb_type ];
glantau's avatar
glantau committed
267 268
        break;
    }
mbardiaux's avatar
mbardiaux 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);
        }
glantau's avatar
glantau committed
283

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

glantau's avatar
glantau 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 */
glantau's avatar
glantau 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

michaelni's avatar
michaelni committed
309
        if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
michael's avatar
michael committed
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);
michael's avatar
michael committed
313 314 315
                }
            }else{
                for(i=0;i<mb_block_count;i++) {
316
                    if (mpeg2_decode_block_intra(s, *s->pblocks[i], i) < 0)
michael's avatar
michael committed
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;
            }
        }
glantau's avatar
glantau 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's avatar
michael committed
331 332 333
            if(s->picture_structure == PICT_FRAME){
                if(!s->frame_pred_frame_dct)
                    s->interlaced_dct = get_bits1(&s->gb);
michael's avatar
michael committed
334
                s->mv_type = MV_TYPE_16X16;
michael's avatar
michael committed
335
            }else{
michael's avatar
michael committed
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's avatar
michael 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's avatar
diego 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's avatar
michael 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's avatar
michael committed
366
            s->mv_dir= (mb_type>>13)&3;
michael's avatar
michael 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;
                                }
                            }
glantau's avatar
glantau committed
402
                        }
michael's avatar
michael 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;
mbardiaux's avatar
mbardiaux 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;
mbardiaux's avatar
mbardiaux 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);
glantau's avatar
glantau 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;
glantau's avatar
glantau committed
438 439
                            }
                        }
michael's avatar
michael 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]++;
glantau's avatar
glantau committed
487 488 489
                        }
                    }
                }
michael's avatar
michael 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;
glantau's avatar
glantau 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

michaelni's avatar
michaelni 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
                    }
glantau's avatar
glantau 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
                    }
michaelni's avatar
michaelni committed
563
                }
glantau's avatar
glantau committed
564
            }
565
        }else{
566
            for(i=0;i<12;i++)
567
                s->block_last_index[i] = -1;
glantau's avatar
glantau committed
568 569
        }
    }
570 571 572

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

glantau's avatar
glantau committed
573 574 575
    return 0;
}

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

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

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

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

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

618
    /* DC coefficient */
michaelni's avatar
michaelni committed
619
    component = (n <= 3 ? 0 : n - 4 + 1);
620
    diff = decode_dc(&s->gb, component);
michaelni's avatar
michaelni committed
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];
mbardiaux's avatar
mbardiaux committed
627
    dprintf(s->avctx, "dc=%d diff=%d\n", dc, diff);
michaelni's avatar
michaelni committed
628 629
    i = 0;
    {
630
        OPEN_READER(re, &s->gb);
631
        /* now quantify & encode AC coefficients */
michaelni's avatar
michaelni committed
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

michaelni's avatar
michaelni committed
636 637 638 639 640
            if(level == 127){
                break;
            } else if(level != 0) {
                i += run;
                j = scantable[i];
michael's avatar
michael committed
641
                level= (level*qscale*quant_matrix[j])>>4;
michaelni's avatar
michaelni committed
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's avatar
michael committed
659
                    level= (level*qscale*quant_matrix[j])>>4;
michaelni's avatar
michaelni committed
660 661 662
                    level= (level-1)|1;
                    level= -level;
                }else{
michael's avatar
michael committed
663
                    level= (level*qscale*quant_matrix[j])>>4;
michaelni's avatar
michaelni committed
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);
michaelni's avatar
michaelni committed
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,
michaelni's avatar
michaelni committed
682 683 684
                               int n)
{
    int level, i, j, run;
685
    RLTable *rl = &ff_rl_mpeg1;
kabi's avatar
kabi committed
686 687
    uint8_t * const scantable= s->intra_scantable.permutated;
    const uint16_t *quant_matrix= s->inter_matrix;
michaelni's avatar
michaelni committed
688 689 690
    const int qscale= s->qscale;

    {
691
        OPEN_READER(re, &s->gb);
michaelni's avatar
michaelni committed
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's avatar
michael committed
696
            level= (3*qscale*quant_matrix[0])>>5;
michaelni's avatar
michaelni committed
697
            level= (level-1)|1;
698
            if(GET_CACHE(re, &s->gb)&0x40000000)
michaelni's avatar
michaelni committed
699
                level= -level;
700
            block[0] = level;
michaelni's avatar
michaelni committed
701
            i++;
702 703 704
            SKIP_BITS(re, &s->gb, 2);
            if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
                goto end;
glantau's avatar
glantau committed
705
        }
706 707 708
#if MIN_CACHE_BITS < 19
        UPDATE_CACHE(re, &s->gb);
#endif
709
        /* now quantify & encode AC coefficients */
michaelni's avatar
michaelni committed
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) {
michaelni's avatar
michaelni committed
714 715
                i += run;
                j = scantable[i];
michael's avatar
michael committed
716
                level= ((level*2+1)*qscale*quant_matrix[j])>>5;
michaelni's avatar
michaelni committed
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);
michaelni's avatar
michaelni committed
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);
michaelni's avatar
michaelni committed
727
                } else if (level == 0) {
728
                    level = SHOW_UBITS(re, &s->gb, 8)      ; SKIP_BITS(re, &s->gb, 8);
michaelni's avatar
michaelni committed
729 730 731 732 733
                }
                i += run;
                j = scantable[i];
                if(level<0){
                    level= -level;
michael's avatar
michael committed
734
                    level= ((level*2+1)*qscale*quant_matrix[j])>>5;
michaelni's avatar
michaelni committed
735 736 737
                    level= (level-1)|1;
                    level= -level;
                }else{
michael's avatar
michael committed
738
                    level= ((level*2+1)*qscale*quant_matrix[j])>>5;
michaelni's avatar
michaelni committed
739 740
                    level= (level-1)|1;
                }
glantau's avatar
glantau committed
741
            }
michaelni's avatar
michaelni committed
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);
michaelni's avatar
michaelni committed
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
glantau's avatar
glantau committed
756
        }
757 758
end:
        LAST_SKIP_BITS(re, &s->gb, 2);
michaelni's avatar
michaelni committed
759
        CLOSE_READER(re, &s->gb);
glantau's avatar
glantau committed
760
    }
michaelni's avatar
michaelni committed
761
    s->block_last_index[n] = i;
glantau's avatar
glantau 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;
768
    RLTable *rl = &ff_rl_mpeg1;
769 770 771 772 773 774
    uint8_t * const scantable= s->intra_scantable.permutated;
    const int qscale= s->qscale;

    {
        OPEN_READER(re, &s->gb);
        i = -1;
775
        // special case for first coefficient, no need to add second VLC table
776
        UPDATE_CACHE(re, &s->gb);
777
        if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
michael's avatar
typo  
michael committed
778
            level= (3*qscale)>>1;
779
            level= (level-1)|1;
780
            if(GET_CACHE(re, &s->gb)&0x40000000)
781 782 783
                level= -level;
            block[0] = level;
            i++;
784 785 786
            SKIP_BITS(re, &s->gb, 2);
            if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
                goto end;
787
        }
788 789 790
#if MIN_CACHE_BITS < 19
        UPDATE_CACHE(re, &s->gb);
#endif
791

792
        /* now quantify & encode AC coefficients */
793
        for(;;) {
794
            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
795

796
            if(level != 0) {
797 798 799 800 801
                i += run;
                j = scantable[i];
                level= ((level*2+1)*qscale)>>1;
                level= (level-1)|1;
                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
802
                SKIP_BITS(re, &s->gb, 1);
803 804 805 806 807 808
            } 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) {
809
                    level = SHOW_UBITS(re, &s->gb, 8) - 256; SKIP_BITS(re, &s->gb, 8);
810
                } else if (level == 0) {
811
                    level = SHOW_UBITS(re, &s->gb, 8)      ; SKIP_BITS(re, &s->gb, 8);
812 813 814 815 816 817 818 819 820 821 822 823 824 825 826
                }
                i += run;
                j = scantable[i];
                if(level<0){
                    level= -level;
                    level= ((level*2+1)*qscale)>>1;
                    level= (level-1)|1;
                    level= -level;
                }else{
                    level= ((level*2+1)*qscale)>>1;
                    level= (level-1)|1;
                }
            }

            block[j] = level;
827 828 829
#if MIN_CACHE_BITS < 19
            UPDATE_CACHE(re, &s->gb);
#endif
830 831
            if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
                break;
832
#if MIN_CACHE_BITS >= 19
833
            UPDATE_CACHE(re, &s->gb);
834
#endif
835
        }
836 837
end:
        LAST_SKIP_BITS(re, &s->gb, 2);
838 839 840 841 842 843 844
        CLOSE_READER(re, &s->gb);
    }
    s->block_last_index[n] = i;
    return 0;
}


845 846
static inline int mpeg2_decode_block_non_intra(MpegEncContext *s,
                               DCTELEM *block,
847
                               int n)
glantau's avatar
glantau committed
848 849
{
    int level, i, j, run;
850
    RLTable *rl = &ff_rl_mpeg1;
kabi's avatar
kabi committed
851 852
    uint8_t * const scantable= s->intra_scantable.permutated;
    const uint16_t *quant_matrix;
853
    const int qscale= s->qscale;
glantau's avatar
glantau committed
854 855 856 857 858
    int mismatch;

    mismatch = 1;

    {
859
        OPEN_READER(re, &s->gb);
860
        i = -1;
861
        if (n < 4)
862
            quant_matrix = s->inter_matrix;
glantau's avatar
glantau committed
863
        else
864
            quant_matrix = s->chroma_inter_matrix;
865

866
        // special case for first coefficient, no need to add second VLC table
867
        UPDATE_CACHE(re, &s->gb);
868
        if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
869
            level= (3*qscale*quant_matrix[0])>>5;
870
            if(GET_CACHE(re, &s->gb)&0x40000000)
871 872 873 874
                level= -level;
            block[0] = level;
            mismatch ^= level;
            i++;
875 876 877
            SKIP_BITS(re, &s->gb, 2);
            if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
                goto end;
glantau's avatar
glantau committed
878
        }
879 880 881
#if MIN_CACHE_BITS < 19
        UPDATE_CACHE(re, &s->gb);
#endif
glantau's avatar
glantau committed
882

883
        /* now quantify & encode AC coefficients */
884
        for(;;) {
885
            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
886

887
            if(level != 0) {
888 889 890 891
                i += run;
                j = scantable[i];
                level= ((level*2+1)*qscale*quant_matrix[j])>>5;
                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
892
                SKIP_BITS(re, &s->gb, 1);
893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908
            } else {
                /* escape */
                run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
                UPDATE_CACHE(re, &s->gb);
                level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);

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

913 914
            mismatch ^= level;
            block[j] = level;
915 916 917
#if MIN_CACHE_BITS < 19
            UPDATE_CACHE(re, &s->gb);
#endif
918 919
            if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
                break;
920
#if MIN_CACHE_BITS >= 19
921
            UPDATE_CACHE(re, &s->gb);
922
#endif
923
        }
924 925
end:
        LAST_SKIP_BITS(re, &s->gb, 2);
926
        CLOSE_READER(re, &s->gb);
glantau's avatar
glantau committed
927 928
    }
    block[63] ^= (mismatch & 1);
929

glantau's avatar
glantau committed
930 931 932 933
    s->block_last_index[n] = i;
    return 0;
}

934 935
static inline int mpeg2_fast_decode_block_non_intra(MpegEncContext *s,
                               DCTELEM *block,
936 937 938
                               int n)
{
    int level, i, j, run;
939
    RLTable *rl = &ff_rl_mpeg1;
940 941 942 943 944
    uint8_t * const scantable= s->intra_scantable.permutated;
    const int qscale= s->qscale;
    OPEN_READER(re, &s->gb);
    i = -1;

945
    // special case for first coefficient, no need to add second VLC table
946
    UPDATE_CACHE(re, &s->gb);
947
    if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
948
        level= (3*qscale)>>1;
949
        if(GET_CACHE(re, &s->gb)&0x40000000)
950 951 952
            level= -level;
        block[0] = level;
        i++;
953 954 955
        SKIP_BITS(re, &s->gb, 2);
        if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
            goto end;
956
    }
957 958 959
#if MIN_CACHE_BITS < 19
    UPDATE_CACHE(re, &s->gb);
#endif
960

961
    /* now quantify & encode AC coefficients */
962
    for(;;) {
963
        GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
964

965
        if(level != 0) {
966 967 968 969
            i += run;
            j = scantable[i];
            level= ((level*2+1)*qscale)>>1;
            level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
970
            SKIP_BITS(re, &s->gb, 1);
971 972 973 974 975 976 977 978 979 980 981 982 983 984 985
        } else {
            /* escape */
            run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
            UPDATE_CACHE(re, &s->gb);
            level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);

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

987
        block[j] = level;
988 989 990
#if MIN_CACHE_BITS < 19
        UPDATE_CACHE(re, &s->gb);
#endif
991 992
        if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
            break;
993
#if MIN_CACHE_BITS >=19
994
        UPDATE_CACHE(re, &s->gb);
995
#endif
996
    }
997
end:
998
    LAST_SKIP_BITS(re, &s->gb, 2);
999 1000 1001 1002 1003 1004
    CLOSE_READER(re, &s->gb);
    s->block_last_index[n] = i;
    return 0;
}


1005 1006
static inline int mpeg2_decode_block_intra(MpegEncContext *s,
                               DCTELEM *block,
1007
                               int n)
glantau's avatar
glantau committed
1008 1009
{
    int level, dc, diff, i, j, run;
1010
    int component;
glantau's avatar
glantau committed
1011
    RLTable *rl;
kabi's avatar
kabi committed
1012 1013
    uint8_t * const scantable= s->intra_scantable.permutated;
    const uint16_t *quant_matrix;
1014
    const int qscale= s->qscale;
glantau's avatar
glantau committed
1015 1016
    int mismatch;

1017
    /* DC coefficient */
1018 1019
    if (n < 4){
        quant_matrix = s->intra_matrix;
1020
        component = 0;
1021 1022
    }else{
        quant_matrix = s->chroma_intra_matrix;
1023
        component = (n&1) + 1;
1024
    }
1025
    diff = decode_dc(&s->gb, component);
glantau's avatar
glantau committed
1026 1027 1028 1029 1030 1031
    if (diff >= 0xffff)
        return -1;
    dc = s->last_dc[component];
    dc += diff;
    s->last_dc[component] = dc;
    block[0] = dc << (3 - s->intra_dc_precision);
mbardiaux's avatar
mbardiaux committed
1032
    dprintf(s->avctx, "dc=%d\n", block[0]);
1033
    mismatch = block[0] ^ 1;
1034
    i = 0;
glantau's avatar
glantau committed
1035
    if (s->intra_vlc_format)
1036
        rl = &ff_rl_mpeg2;
glantau's avatar
glantau committed
1037
    else
1038
        rl = &ff_rl_mpeg1;
1039

1040
    {
1041
        OPEN_READER(re, &s->gb);
1042
        /* now quantify & encode AC coefficients */
1043 1044
        for(;;) {
            UPDATE_CACHE(re, &s->gb);
1045
            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
1046

1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
            if(level == 127){
                break;
            } else if(level != 0) {
                i += run;
                j = scantable[i];
                level= (level*qscale*quant_matrix[j])>>4;
                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
                LAST_SKIP_BITS(re, &s->gb, 1);
            } else {
                /* escape */
                run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
                UPDATE_CACHE(re, &s->gb);
                level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
                i += run;
                j = scantable[i];
                if(level<0){
                    level= (-level*qscale*quant_matrix[j])>>4;
                    level= -level;
                }else{
                    level= (level*qscale*quant_matrix[j])>>4;
                }
            }
            if (i > 63){
1070
                av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
1071 1072
                return -1;
            }
1073

1074 1075
            mismatch^= level;
            block[j] = level;
michaelni's avatar
michaelni committed
1076
        }
1077
        CLOSE_READER(re, &s->gb);
glantau's avatar
glantau committed
1078
    }
1079
    block[63]^= mismatch&1;
1080

glantau's avatar
glantau committed
1081 1082 1083 1084
    s->block_last_index[n] = i;
    return 0;
}

1085 1086
static inline int mpeg2_fast_decode_block_intra(MpegEncContext *s,
                               DCTELEM *block,
michael's avatar
michael committed
1087 1088 1089 1090 1091 1092 1093 1094 1095
                               int n)
{
    int level, dc, diff, j, run;
    int component;
    RLTable *rl;
    uint8_t * scantable= s->intra_scantable.permutated;
    const uint16_t *quant_matrix;
    const int qscale= s->qscale;

1096
    /* DC coefficient */
michael's avatar
michael committed
1097 1098
    if (n < 4){
        quant_matrix = s->intra_matrix;
1099
        component = 0;
michael's avatar
michael committed
1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
    }else{
        quant_matrix = s->chroma_intra_matrix;
        component = (n&1) + 1;
    }
    diff = decode_dc(&s->gb, component);
    if (diff >= 0xffff)
        return -1;
    dc = s->last_dc[component];
    dc += diff;
    s->last_dc[component] = dc;
    block[0] = dc << (3 - s->intra_dc_precision);
    if (s->intra_vlc_format)
1112
        rl = &ff_rl_mpeg2;
michael's avatar
michael committed
1113
    else
1114
        rl = &ff_rl_mpeg1;
michael's avatar
michael committed
1115 1116

    {
1117
        OPEN_READER(re, &s->gb);
1118
        /* now quantify & encode AC coefficients */
michael's avatar
michael committed
1119 1120 1121
        for(;;) {
            UPDATE_CACHE(re, &s->gb);
            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
1122

michael's avatar
michael committed
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
            if(level == 127){
                break;
            } else if(level != 0) {
                scantable += run;
                j = *scantable;
                level= (level*qscale*quant_matrix[j])>>4;
                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
                LAST_SKIP_BITS(re, &s->gb, 1);
            } else {
                /* escape */
                run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
                UPDATE_CACHE(re, &s->gb);
                level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
                scantable += run;
                j = *scantable;
                if(level<0){
                    level= (-level*qscale*quant_matrix[j])>>4;
                    level= -level;
                }else{
                    level= (level*qscale*quant_matrix[j])>>4;
                }
            }
1145

michael's avatar
michael committed
1146 1147 1148 1149
            block[j] = level;
        }
        CLOSE_READER(re, &s->gb);
    }
1150

michael's avatar
michael committed
1151 1152 1153 1154
    s->block_last_index[n] = scantable - s->intra_scantable.permutated;
    return 0;
}

glantau's avatar
glantau committed
1155 1156 1157
typedef struct Mpeg1Context {
    MpegEncContext mpeg_enc_ctx;
    int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
1158
    int repeat_field; /* true if we must repeat the field */
1159
    AVPanScan pan_scan; /** some temporary storage for the panscan */
michael's avatar
michael committed
1160
    int slice_count;
1161 1162
    int swap_uv;//indicate VCR2
    int save_aspect_info;
benoit's avatar
 
benoit committed
1163
    int save_width, save_height;
michael's avatar
michael committed
1164
    AVRational frame_rate_ext;       ///< MPEG-2 specific framerate modificator
1165

glantau's avatar
glantau committed
1166 1167
} Mpeg1Context;

1168
static av_cold int mpeg_decode_init(AVCodecContext *avctx)
glantau's avatar
glantau committed
1169 1170
{
    Mpeg1Context *s = avctx->priv_data;
michael's avatar
michael committed
1171
    MpegEncContext *s2 = &s->mpeg_enc_ctx;
iive's avatar
iive committed
1172
    int i;
1173

1174 1175
    /* we need some permutation to store matrices,
     * until MPV_common_init() sets the real permutation. */
iive's avatar
iive committed
1176 1177 1178
    for(i=0;i<64;i++)
       s2->dsp.idct_permutation[i]=i;

michael's avatar
michael committed
1179
    MPV_decode_defaults(s2);
1180

1181
    s->mpeg_enc_ctx.avctx= avctx;
michaelni's avatar
dr1  
michaelni committed
1182
    s->mpeg_enc_ctx.flags= avctx->flags;
1183
    s->mpeg_enc_ctx.flags2= avctx->flags2;
1184
    ff_mpeg12_common_init(&s->mpeg_enc_ctx);
1185
    ff_mpeg12_init_vlcs();
glantau's avatar
glantau committed
1186 1187 1188

    s->mpeg_enc_ctx_allocated = 0;
    s->mpeg_enc_ctx.picture_number = 0;
1189
    s->repeat_field = 0;
1190
    s->mpeg_enc_ctx.codec_id= avctx->codec->id;
glantau's avatar
glantau committed
1191 1192 1193
    return 0;
}

1194
static void quant_matrix_rebuild(uint16_t *matrix, const uint8_t *old_perm,
1195
                                     const uint8_t *new_perm){
michael's avatar
michael committed
1196 1197
    uint16_t temp_matrix[64];
    int i;
1198 1199

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

1201 1202
    for(i=0;i<64;i++){
        matrix[new_perm[i]] = temp_matrix[old_perm[i]];
1203
    }
1204 1205
}

1206
static enum PixelFormat mpeg_get_pixelformat(AVCodecContext *avctx){
1207 1208 1209 1210 1211
    Mpeg1Context *s1 = avctx->priv_data;
    MpegEncContext *s = &s1->mpeg_enc_ctx;

    if(avctx->xvmc_acceleration)
        return avctx->get_format(avctx,pixfmt_xvmc_mpg2_420);
1212 1213 1214 1215 1216 1217
    else if(avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU){
        if(avctx->codec_id == CODEC_ID_MPEG1VIDEO)
            return PIX_FMT_VDPAU_MPEG1;
        else
            return PIX_FMT_VDPAU_MPEG2;
    }else{
1218 1219 1220 1221 1222 1223 1224 1225 1226
        if(s->chroma_format <  2)
            return PIX_FMT_YUV420P;
        else if(s->chroma_format == 2)
            return PIX_FMT_YUV422P;
        else
            return PIX_FMT_YUV444P;
    }
}

1227 1228
/* Call this function when we know all parameters.
 * It may be called in different places for MPEG-1 and MPEG-2. */
1229
static int mpeg_decode_postinit(AVCodecContext *avctx){
michael's avatar
michael committed
1230 1231 1232
    Mpeg1Context *s1 = avctx->priv_data;
    MpegEncContext *s = &s1->mpeg_enc_ctx;
    uint8_t old_permutation[64];
1233 1234

    if (
1235
        (s1->mpeg_enc_ctx_allocated == 0)||
1236 1237
        avctx->coded_width  != s->width ||
        avctx->coded_height != s->height||
benoit's avatar
 
benoit committed
1238 1239
        s1->save_width != s->width ||
        s1->save_height != s->height ||
iive's avatar
iive committed
1240
        s1->save_aspect_info != s->aspect_ratio_info||
1241 1242
        0)
    {
1243

1244
        if (s1->mpeg_enc_ctx_allocated) {
michael's avatar
michael committed
1245 1246
            ParseContext pc= s->parse_context;
            s->parse_context.buffer=0;
1247
            MPV_common_end(s);
michael's avatar
michael committed
1248
            s->parse_context= pc;
1249 1250
        }

1251 1252
        if( (s->width == 0 )||(s->height == 0))
            return -2;
1253

1254
        avcodec_set_dimensions(avctx, s->width, s->height);
1255 1256
        avctx->bit_rate = s->bit_rate;
        s1->save_aspect_info = s->aspect_ratio_info;
benoit's avatar
 
benoit committed
1257 1258
        s1->save_width = s->width;
        s1->save_height = s->height;
1259

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

1264 1265
        assert((avctx->sub_id==1) == (avctx->codec_id==CODEC_ID_MPEG1VIDEO));
        if(avctx->codec_id==CODEC_ID_MPEG1VIDEO){
1266
            //MPEG-1 fps
1267 1268
            avctx->time_base.den= ff_frame_rate_tab[s->frame_rate_index].num;
            avctx->time_base.num= ff_frame_rate_tab[s->frame_rate_index].den;
1269
            //MPEG-1 aspect
1270
            avctx->sample_aspect_ratio= av_d2q(
1271
                    1.0/ff_mpeg1_aspect[s->aspect_ratio_info], 255);
1272

1273 1274
        }else{//MPEG-2
        //MPEG-2 fps
1275
            av_reduce(
1276 1277
                &s->avctx->time_base.den,
                &s->avctx->time_base.num,
1278 1279
                ff_frame_rate_tab[s->frame_rate_index].num * s1->frame_rate_ext.num,
                ff_frame_rate_tab[s->frame_rate_index].den * s1->frame_rate_ext.den,
1280
                1<<30);
1281
        //MPEG-2 aspect
1282
            if(s->aspect_ratio_info > 1){
1283 1284 1285
                //we ignore the spec here as reality does not match the spec, see for example
                // res_change_ffmpeg_aspect.ts and sequence-display-aspect.mpg
                if( (s1->pan_scan.width == 0 )||(s1->pan_scan.height == 0) || 1){
1286
                    s->avctx->sample_aspect_ratio=
1287
                        av_div_q(
1288
                         ff_mpeg2_aspect[s->aspect_ratio_info],
1289 1290
                         (AVRational){s->width, s->height}
                         );
1291
                }else{
1292
                    s->avctx->sample_aspect_ratio=
1293
                        av_div_q(
1294
                         ff_mpeg2_aspect[s->aspect_ratio_info],
1295
                         (AVRational){s1->pan_scan.width, s1->pan_scan.height}
1296
                        );
1297
                }
1298
            }else{
1299
                s->avctx->sample_aspect_ratio=
1300
                    ff_mpeg2_aspect[s->aspect_ratio_info];
1301
            }
1302
        }//MPEG-2
1303

1304
        avctx->pix_fmt = mpeg_get_pixelformat(avctx);
1305
        //until then pix_fmt may be changed right after codec init
1306
        if( avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT ||
1307
            avctx->hwaccel ||
1308
            s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU )
1309 1310 1311
            if( avctx->idct_algo == FF_IDCT_AUTO )
                avctx->idct_algo = FF_IDCT_SIMPLE;

1312 1313
        /* Quantization matrices may need reordering
         * if DCT permutation is changed. */
1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328
        memcpy(old_permutation,s->dsp.idct_permutation,64*sizeof(uint8_t));

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

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

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

1329
static int mpeg1_decode_picture(AVCodecContext *avctx,
kabi's avatar
kabi committed
1330
                                const uint8_t *buf, int buf_size)
glantau's avatar
glantau committed
1331 1332 1333
{
    Mpeg1Context *s1 = avctx->priv_data;
    MpegEncContext *s = &s1->mpeg_enc_ctx;
michael's avatar
michael committed
1334
    int ref, f_code, vbv_delay;
glantau's avatar
glantau committed
1335

1336
    if(mpeg_decode_postinit(s->avctx) < 0)
1337 1338
       return -2;

1339
    init_get_bits(&s->gb, buf, buf_size*8);
glantau's avatar
glantau committed
1340 1341 1342

    ref = get_bits(&s->gb, 10); /* temporal ref */
    s->pict_type = get_bits(&s->gb, 3);
1343 1344
    if(s->pict_type == 0 || s->pict_type > 3)
        return -1;
michaelni's avatar
michaelni committed
1345

michael's avatar
michael committed
1346
    vbv_delay= get_bits(&s->gb, 16);
1347
    if (s->pict_type == FF_P_TYPE || s->pict_type == FF_B_TYPE) {
1348
        s->full_pel[0] = get_bits1(&s->gb);
glantau's avatar
glantau committed
1349
        f_code = get_bits(&s->gb, 3);
1350
        if (f_code == 0 && avctx->error_recognition >= FF_ER_COMPLIANT)
glantau's avatar
glantau committed
1351 1352 1353 1354
            return -1;
        s->mpeg_f_code[0][0] = f_code;
        s->mpeg_f_code[0][1] = f_code;
    }
1355
    if (s->pict_type == FF_B_TYPE) {
1356
        s->full_pel[1] = get_bits1(&s->gb);
glantau's avatar
glantau committed
1357
        f_code = get_bits(&s->gb, 3);
1358
        if (f_code == 0 && avctx->error_recognition >= FF_ER_COMPLIANT)
glantau's avatar
glantau committed
1359 1360 1361 1362
            return -1;
        s->mpeg_f_code[1][0] = f_code;
        s->mpeg_f_code[1][1] = f_code;
    }
michaelni's avatar
michaelni committed
1363
    s->current_picture.pict_type= s->pict_type;
1364
    s->current_picture.key_frame= s->pict_type == FF_I_TYPE;
1365

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

glantau's avatar
glantau committed
1369 1370 1371 1372 1373 1374
    s->y_dc_scale = 8;
    s->c_dc_scale = 8;
    s->first_slice = 1;
    return 0;
}

michael's avatar
michael committed
1375
static void mpeg_decode_sequence_extension(Mpeg1Context *s1)
glantau's avatar
glantau committed
1376
{
michael's avatar
michael committed
1377
    MpegEncContext *s= &s1->mpeg_enc_ctx;
glantau's avatar
glantau committed
1378
    int horiz_size_ext, vert_size_ext;
michael's avatar
michael committed
1379
    int bit_rate_ext;
glantau's avatar
glantau committed
1380

1381
    skip_bits(&s->gb, 1); /* profile and level esc*/
michael's avatar
michael committed
1382 1383
    s->avctx->profile= get_bits(&s->gb, 3);
    s->avctx->level= get_bits(&s->gb, 4);
1384
    s->progressive_sequence = get_bits1(&s->gb); /* progressive_sequence */
1385
    s->chroma_format = get_bits(&s->gb, 2); /* chroma_format 1=420, 2=422, 3=444 */
glantau's avatar
glantau committed
1386 1387 1388 1389 1390
    horiz_size_ext = get_bits(&s->gb, 2);
    vert_size_ext = get_bits(&s->gb, 2);
    s->width |= (horiz_size_ext << 12);
    s->height |= (vert_size_ext << 12);
    bit_rate_ext = get_bits(&s->gb, 12);  /* XXX: handle it */
michael's avatar
typo  
michael committed
1391
    s->bit_rate += (bit_rate_ext << 18) * 400;
1392
    skip_bits1(&s->gb); /* marker */
1393
    s->avctx->rc_buffer_size += get_bits(&s->gb, 8)*1024*16<<10;
michaelni's avatar
michaelni committed
1394

michaelni's avatar
michaelni committed
1395
    s->low_delay = get_bits1(&s->gb);
michaelni's avatar
michaelni committed
1396 1397
    if(s->flags & CODEC_FLAG_LOW_DELAY) s->low_delay=1;

michael's avatar
michael committed
1398 1399
    s1->frame_rate_ext.num = get_bits(&s->gb, 2)+1;
    s1->frame_rate_ext.den = get_bits(&s->gb, 5)+1;
1400

mbardiaux's avatar
mbardiaux committed
1401
    dprintf(s->avctx, "sequence extension\n");
michaelni's avatar
michaelni committed
1402
    s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG2VIDEO;
1403
    s->avctx->sub_id = 2; /* indicates MPEG-2 found */
michaelni's avatar
michaelni committed
1404

michaelni's avatar
michaelni committed
1405
    if(s->avctx->debug & FF_DEBUG_PICT_INFO)
1406
        av_log(s->avctx, AV_LOG_DEBUG, "profile: %d, level: %d vbv buffer: %d, bitrate:%d\n",
michael's avatar
michael committed
1407
               s->avctx->profile, s->avctx->level, s->avctx->rc_buffer_size, s->bit_rate);
1408

glantau's avatar
glantau committed
1409 1410
}

1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426
static void mpeg_decode_sequence_display_extension(Mpeg1Context *s1)
{
    MpegEncContext *s= &s1->mpeg_enc_ctx;
    int color_description, w, h;

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

1428 1429
    s1->pan_scan.width= 16*w;
    s1->pan_scan.height=16*h;
1430

1431
    if(s->avctx->debug & FF_DEBUG_PICT_INFO)
1432
        av_log(s->avctx, AV_LOG_DEBUG, "sde w:%d, h:%d\n", w, h);
1433 1434 1435 1436 1437
}

static void mpeg_decode_picture_display_extension(Mpeg1Context *s1)
{
    MpegEncContext *s= &s1->mpeg_enc_ctx;
1438 1439 1440 1441 1442
    int i,nofco;

    nofco = 1;
    if(s->progressive_sequence){
        if(s->repeat_first_field){
1443 1444 1445 1446
            nofco++;
            if(s->top_field_first)
                nofco++;
        }
1447 1448 1449
    }else{
        if(s->picture_structure == PICT_FRAME){
            nofco++;
1450 1451 1452
            if(s->repeat_first_field)
                nofco++;
        }
1453 1454
    }
    for(i=0; i<nofco; i++){
1455 1456 1457 1458 1459
        s1->pan_scan.position[i][0]= get_sbits(&s->gb, 16);
        skip_bits(&s->gb, 1); //marker
        s1->pan_scan.position[i][1]= get_sbits(&s->gb, 16);
        skip_bits(&s->gb, 1); //marker
    }
1460

1461
    if(s->avctx->debug & FF_DEBUG_PICT_INFO)
1462 1463 1464
        av_log(s->avctx, AV_LOG_DEBUG, "pde (%d,%d) (%d,%d) (%d,%d)\n",
            s1->pan_scan.position[0][0], s1->pan_scan.position[0][1],
            s1->pan_scan.position[1][0], s1->pan_scan.position[1][1],
1465 1466 1467 1468
            s1->pan_scan.position[2][0], s1->pan_scan.position[2][1]
        );
}

glantau's avatar
glantau committed
1469 1470
static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
{
1471
    int i, v, j;
glantau's avatar
glantau committed
1472

mbardiaux's avatar
mbardiaux committed
1473
    dprintf(s->avctx, "matrix extension\n");
1474

1475
    if (get_bits1(&s->gb)) {
glantau's avatar
glantau committed
1476 1477
        for(i=0;i<64;i++) {
            v = get_bits(&s->gb, 8);
1478
            j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
1479 1480
            s->intra_matrix[j] = v;
            s->chroma_intra_matrix[j] = v;
glantau's avatar
glantau committed
1481 1482
        }
    }
1483
    if (get_bits1(&s->gb)) {
glantau's avatar
glantau committed
1484 1485
        for(i=0;i<64;i++) {
            v = get_bits(&s->gb, 8);
1486
            j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
1487 1488
            s->inter_matrix[j] = v;
            s->chroma_inter_matrix[j] = v;
glantau's avatar
glantau committed
1489 1490
        }
    }
1491
    if (get_bits1(&s->gb)) {
glantau's avatar
glantau committed
1492 1493
        for(i=0;i<64;i++) {
            v = get_bits(&s->gb, 8);
1494
            j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
1495
            s->chroma_intra_matrix[j] = v;
glantau's avatar
glantau committed
1496 1497
        }
    }
1498
    if (get_bits1(&s->gb)) {
glantau's avatar
glantau committed
1499 1500
        for(i=0;i<64;i++) {
            v = get_bits(&s->gb, 8);
1501
            j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
1502
            s->chroma_inter_matrix[j] = v;
glantau's avatar
glantau committed
1503 1504 1505 1506
        }
    }
}

1507
static void mpeg_decode_picture_coding_extension(Mpeg1Context *s1)
glantau's avatar
glantau committed
1508
{
1509 1510
    MpegEncContext *s= &s1->mpeg_enc_ctx;

glantau's avatar
glantau committed
1511 1512 1513 1514 1515
    s->full_pel[0] = s->full_pel[1] = 0;
    s->mpeg_f_code[0][0] = get_bits(&s->gb, 4);
    s->mpeg_f_code[0][1] = get_bits(&s->gb, 4);
    s->mpeg_f_code[1][0] = get_bits(&s->gb, 4);
    s->mpeg_f_code[1][1] = get_bits(&s->gb, 4);
1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528
    if(!s->pict_type && s1->mpeg_enc_ctx_allocated){
        av_log(s->avctx, AV_LOG_ERROR, "Missing picture start code, guessing missing values\n");
        if(s->mpeg_f_code[1][0] == 15 && s->mpeg_f_code[1][1]==15){
            if(s->mpeg_f_code[0][0] == 15 && s->mpeg_f_code[0][1] == 15)
                s->pict_type= FF_I_TYPE;
            else
                s->pict_type= FF_P_TYPE;
        }else
            s->pict_type= FF_B_TYPE;
        s->current_picture.pict_type= s->pict_type;
        s->current_picture.key_frame= s->pict_type == FF_I_TYPE;
        s->first_slice= 1;
    }
glantau's avatar
glantau committed
1529 1530
    s->intra_dc_precision = get_bits(&s->gb, 2);
    s->picture_structure = get_bits(&s->gb, 2);
1531 1532 1533 1534 1535 1536 1537 1538 1539
    s->top_field_first = get_bits1(&s->gb);
    s->frame_pred_frame_dct = get_bits1(&s->gb);
    s->concealment_motion_vectors = get_bits1(&s->gb);
    s->q_scale_type = get_bits1(&s->gb);
    s->intra_vlc_format = get_bits1(&s->gb);
    s->alternate_scan = get_bits1(&s->gb);
    s->repeat_first_field = get_bits1(&s->gb);
    s->chroma_420_type = get_bits1(&s->gb);
    s->progressive_frame = get_bits1(&s->gb);
michael's avatar
michael committed
1540

1541
    if(s->picture_structure == PICT_FRAME){
1542
        s->first_field=0;
1543 1544
        s->v_edge_pos= 16*s->mb_height;
    }else{
1545
        s->first_field ^= 1;
1546
        s->v_edge_pos=  8*s->mb_height;
1547
        memset(s->mbskip_table, 0, s->mb_stride*s->mb_height);
1548
    }
1549

1550
    if(s->alternate_scan){
michaelni's avatar
michaelni committed
1551 1552
        ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable  , ff_alternate_vertical_scan);
        ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable  , ff_alternate_vertical_scan);
1553
    }else{
michaelni's avatar
michaelni committed
1554 1555
        ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable  , ff_zigzag_direct);
        ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable  , ff_zigzag_direct);
1556
    }
1557

glantau's avatar
glantau committed
1558
    /* composite display not parsed */
mbardiaux's avatar
mbardiaux committed
1559 1560 1561 1562 1563 1564 1565 1566 1567
    dprintf(s->avctx, "intra_dc_precision=%d\n", s->intra_dc_precision);
    dprintf(s->avctx, "picture_structure=%d\n", s->picture_structure);
    dprintf(s->avctx, "top field first=%d\n", s->top_field_first);
    dprintf(s->avctx, "repeat first field=%d\n", s->repeat_first_field);
    dprintf(s->avctx, "conceal=%d\n", s->concealment_motion_vectors);
    dprintf(s->avctx, "intra_vlc_format=%d\n", s->intra_vlc_format);
    dprintf(s->avctx, "alternate_scan=%d\n", s->alternate_scan);
    dprintf(s->avctx, "frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
    dprintf(s->avctx, "progressive_frame=%d\n", s->progressive_frame);
glantau's avatar
glantau committed
1568 1569
}

1570
static void mpeg_decode_extension(AVCodecContext *avctx,
kabi's avatar
kabi committed
1571
                                  const uint8_t *buf, int buf_size)
glantau's avatar
glantau committed
1572 1573 1574 1575 1576
{
    Mpeg1Context *s1 = avctx->priv_data;
    MpegEncContext *s = &s1->mpeg_enc_ctx;
    int ext_type;

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

glantau's avatar
glantau committed
1579 1580 1581
    ext_type = get_bits(&s->gb, 4);
    switch(ext_type) {
    case 0x1:
michael's avatar
michael committed
1582
        mpeg_decode_sequence_extension(s1);
glantau's avatar
glantau committed
1583
        break;
1584 1585 1586
    case 0x2:
        mpeg_decode_sequence_display_extension(s1);
        break;
glantau's avatar
glantau committed
1587 1588 1589
    case 0x3:
        mpeg_decode_quant_matrix_extension(s);
        break;
1590 1591 1592
    case 0x7:
        mpeg_decode_picture_display_extension(s1);
        break;
glantau's avatar
glantau committed
1593
    case 0x8:
1594
        mpeg_decode_picture_coding_extension(s1);
glantau's avatar
glantau committed
1595 1596 1597 1598
        break;
    }
}

1599
static void exchange_uv(MpegEncContext *s){
1600 1601 1602
    DCTELEM (*tmp)[64];

    tmp           = s->pblocks[4];
1603 1604
    s->pblocks[4] = s->pblocks[5];
    s->pblocks[5] = tmp;
michaelni's avatar
michaelni committed
1605 1606
}

michael's avatar
michael committed
1607 1608 1609
static int mpeg_field_start(MpegEncContext *s){
    AVCodecContext *avctx= s->avctx;
    Mpeg1Context *s1 = (Mpeg1Context*)s;
glantau's avatar
glantau committed
1610 1611

    /* start frame decoding */
michael's avatar
michael committed
1612
    if(s->first_field || s->picture_structure==PICT_FRAME){
1613
        if(MPV_frame_start(s, avctx) < 0)
michael's avatar
michael committed
1614
            return -1;
1615 1616 1617

        ff_er_frame_start(s);

1618
        /* first check if we must repeat the frame */
bellard's avatar
bellard committed
1619
        s->current_picture_ptr->repeat_pict = 0;
1620 1621 1622
        if (s->repeat_first_field) {
            if (s->progressive_sequence) {
                if (s->top_field_first)
bellard's avatar
bellard committed
1623
                    s->current_picture_ptr->repeat_pict = 4;
1624
                else
bellard's avatar
bellard committed
1625
                    s->current_picture_ptr->repeat_pict = 2;
1626
            } else if (s->progressive_frame) {
bellard's avatar
bellard committed
1627
                s->current_picture_ptr->repeat_pict = 1;
1628
            }
1629
        }
1630 1631

        *s->current_picture_ptr->pan_scan= s1->pan_scan;
michael's avatar
michael committed
1632
    }else{ //second field
michaelni's avatar
michaelni committed
1633
            int i;
1634

1635
            if(!s->current_picture_ptr){
1636
                av_log(s->avctx, AV_LOG_ERROR, "first field missing\n");
1637 1638
                return -1;
            }
1639

michaelni's avatar
michaelni committed
1640 1641 1642 1643
            for(i=0; i<4; i++){
                s->current_picture.data[i] = s->current_picture_ptr->data[i];
                if(s->picture_structure == PICT_BOTTOM_FIELD){
                    s->current_picture.data[i] += s->current_picture_ptr->linesize[i];
1644
                }
michaelni's avatar
michaelni committed
1645
            }
michael's avatar
michael committed
1646
    }
iive's avatar
iive committed
1647 1648
// MPV_frame_start will call this function too,
// but we need to call it on every field
1649
    if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
iive's avatar
iive committed
1650
        if(ff_xvmc_field_start(s,avctx) < 0)
1651
            return -1;
glantau's avatar
glantau committed
1652

michael's avatar
michael committed
1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664
    return 0;
}

#define DECODE_SLICE_ERROR -1
#define DECODE_SLICE_OK 0

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

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

1675 1676
    if (mb_y<<field_pic >= s->mb_height){
        av_log(s->avctx, AV_LOG_ERROR, "slice below image (%d >= %d)\n", mb_y, s->mb_height);
michael's avatar
michael committed
1677 1678
        return -1;
    }
1679

michaelni's avatar
michaelni committed
1680
    init_get_bits(&s->gb, *buf, buf_size*8);
glantau's avatar
glantau committed
1681

michael's avatar
michael committed
1682 1683 1684
    ff_mpeg1_clean_buffers(s);
    s->interlaced_dct = 0;

glantau's avatar
glantau committed
1685
    s->qscale = get_qscale(s);
1686

1687
    if(s->qscale == 0){
1688
        av_log(s->avctx, AV_LOG_ERROR, "qscale == 0\n");
1689
        return -1;
1690
    }
1691

glantau's avatar
glantau committed
1692
    /* extra slice info */
1693 1694
    while (get_bits1(&s->gb) != 0) {
        skip_bits(&s->gb, 8);
glantau's avatar
glantau committed
1695
    }
1696

michaelni's avatar
michaelni committed
1697
    s->mb_x=0;
1698

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

1720
    s->resync_mb_x= s->mb_x;
michael's avatar
michael committed
1721
    s->resync_mb_y= s->mb_y= mb_y;
michaelni's avatar
michaelni committed
1722
    s->mb_skip_run= 0;
1723
    ff_init_block_index(s);
michaelni's avatar
michaelni committed
1724

michael's avatar
michael committed
1725 1726
    if (s->mb_y==0 && s->mb_x==0 && (s->first_field || s->picture_structure==PICT_FRAME)) {
        if(s->avctx->debug&FF_DEBUG_PICT_INFO){
1727
             av_log(s->avctx, AV_LOG_DEBUG, "qp:%d fc:%2d%2d%2d%2d %s %s %s %s %s dc:%d pstruct:%d fdct:%d cmv:%d qtype:%d ivlc:%d rff:%d %s\n",
michael's avatar
michael committed
1728
                 s->qscale, s->mpeg_f_code[0][0],s->mpeg_f_code[0][1],s->mpeg_f_code[1][0],s->mpeg_f_code[1][1],
1729
                 s->pict_type == FF_I_TYPE ? "I" : (s->pict_type == FF_P_TYPE ? "P" : (s->pict_type == FF_B_TYPE ? "B" : "S")),
1730
                 s->progressive_sequence ? "ps" :"", s->progressive_frame ? "pf" : "", s->alternate_scan ? "alt" :"", s->top_field_first ? "top" :"",
michael's avatar
michael committed
1731 1732 1733
                 s->intra_dc_precision, s->picture_structure, s->frame_pred_frame_dct, s->concealment_motion_vectors,
                 s->q_scale_type, s->intra_vlc_format, s->repeat_first_field, s->chroma_420_type ? "420" :"");
        }
1734 1735
    }

glantau's avatar
glantau committed
1736
    for(;;) {
1737
        //If 1, we memcpy blocks in xvmcvideo.
1738
        if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1)
1739
            ff_xvmc_init_block(s);//set s->block
1740

michael's avatar
michael committed
1741
        if(mpeg_decode_mb(s, s->block) < 0)
glantau's avatar
glantau committed
1742
            return -1;
1743

1744
        if(s->current_picture.motion_val[0] && !s->encoding){ //note motion_val is normally NULL unless we want to extract the MVs
1745 1746
            const int wrap = field_pic ? 2*s->b8_stride : s->b8_stride;
            int xy = s->mb_x*2 + s->mb_y*2*wrap;
michael's avatar
michael committed
1747
            int motion_x, motion_y, dir, i;
1748 1749 1750
            if(field_pic && !s->first_field)
                xy += wrap/2;

michael's avatar
michael committed
1751 1752
            for(i=0; i<2; i++){
                for(dir=0; dir<2; dir++){
1753
                    if (s->mb_intra || (dir==1 && s->pict_type != FF_B_TYPE)) {
michael's avatar
michael committed
1754
                        motion_x = motion_y = 0;
michael's avatar
michael committed
1755
                    }else if (s->mv_type == MV_TYPE_16X16 || (s->mv_type == MV_TYPE_FIELD && field_pic)){
michael's avatar
michael committed
1756 1757 1758 1759 1760 1761
                        motion_x = s->mv[dir][0][0];
                        motion_y = s->mv[dir][0][1];
                    } else /*if ((s->mv_type == MV_TYPE_FIELD) || (s->mv_type == MV_TYPE_16X8))*/ {
                        motion_x = s->mv[dir][i][0];
                        motion_y = s->mv[dir][i][1];
                    }
1762

michael's avatar
michael committed
1763 1764 1765 1766
                    s->current_picture.motion_val[dir][xy    ][0] = motion_x;
                    s->current_picture.motion_val[dir][xy    ][1] = motion_y;
                    s->current_picture.motion_val[dir][xy + 1][0] = motion_x;
                    s->current_picture.motion_val[dir][xy + 1][1] = motion_y;
1767 1768
                    s->current_picture.ref_index [dir][xy    ]=
                    s->current_picture.ref_index [dir][xy + 1]= s->field_select[dir][i];
michael's avatar
michael committed
1769
                    assert(s->field_select[dir][i]==0 || s->field_select[dir][i]==1);
michael's avatar
michael committed
1770
                }
michael's avatar
michael committed
1771
                xy += wrap;
1772 1773
            }
        }
1774

michael's avatar
michael committed
1775
        s->dest[0] += 16 >> lowres;
1776 1777
        s->dest[1] +=(16 >> lowres) >> s->chroma_x_shift;
        s->dest[2] +=(16 >> lowres) >> s->chroma_x_shift;
michaelni's avatar
michaelni committed
1778

1779
        MPV_decode_mb(s, s->block);
1780

michaelni's avatar
michaelni committed
1781
        if (++s->mb_x >= s->mb_width) {
michael's avatar
michael committed
1782
            const int mb_size= 16>>s->avctx->lowres;
michaelni's avatar
michaelni committed
1783

michael's avatar
michael committed
1784
            ff_draw_horiz_band(s, mb_size*s->mb_y, mb_size);
michaelni's avatar
michaelni committed
1785 1786 1787

            s->mb_x = 0;
            s->mb_y++;
1788 1789 1790

            if(s->mb_y<<field_pic >= s->mb_height){
                int left= s->gb.size_in_bits - get_bits_count(&s->gb);
1791
                int is_d10= s->chroma_format==2 && s->pict_type==FF_I_TYPE && avctx->profile==0 && avctx->level==5
michael's avatar
michael committed
1792 1793
                            && s->intra_dc_precision == 2 && s->q_scale_type == 1 && s->alternate_scan == 0
                            && s->progressive_frame == 0 /* vbv_delay == 0xBBB || 0xE10*/;
1794

michael's avatar
michael committed
1795
                if(left < 0 || (left && show_bits(&s->gb, FFMIN(left, 23)) && !is_d10)
1796
                   || (avctx->error_recognition >= FF_ER_AGGRESSIVE && left>8)){
michael's avatar
michael committed
1797
                    av_log(avctx, AV_LOG_ERROR, "end mismatch left=%d %0X\n", left, show_bits(&s->gb, FFMIN(left, 23)));
1798 1799 1800 1801
                    return -1;
                }else
                    goto eos;
            }
1802

1803
            ff_init_block_index(s);
michaelni's avatar
michaelni committed
1804 1805 1806
        }

        /* skip mb handling */
michaelni's avatar
michaelni committed
1807
        if (s->mb_skip_run == -1) {
1808
            /* read increment again */
michaelni's avatar
michaelni committed
1809
            s->mb_skip_run = 0;
michaelni's avatar
michaelni committed
1810 1811
            for(;;) {
                int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
1812
                if (code < 0){
1813
                    av_log(s->avctx, AV_LOG_ERROR, "mb incr damaged\n");
michaelni's avatar
michaelni committed
1814
                    return -1;
1815
                }
michaelni's avatar
michaelni committed
1816 1817
                if (code >= 33) {
                    if (code == 33) {
michaelni's avatar
michaelni committed
1818
                        s->mb_skip_run += 33;
michaelni's avatar
michaelni committed
1819 1820
                    }else if(code == 35){
                        if(s->mb_skip_run != 0 || show_bits(&s->gb, 15) != 0){
1821
                            av_log(s->avctx, AV_LOG_ERROR, "slice mismatch\n");
michaelni's avatar
michaelni committed
1822 1823 1824
                            return -1;
                        }
                        goto eos; /* end of slice */
michaelni's avatar
michaelni committed
1825 1826 1827
                    }
                    /* otherwise, stuffing, nothing to do */
                } else {
michaelni's avatar
michaelni committed
1828
                    s->mb_skip_run += code;
michaelni's avatar
michaelni committed
1829 1830 1831
                    break;
                }
            }
1832 1833
            if(s->mb_skip_run){
                int i;
1834
                if(s->pict_type == FF_I_TYPE){
1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846
                    av_log(s->avctx, AV_LOG_ERROR, "skipped MB in I frame at %d %d\n", s->mb_x, s->mb_y);
                    return -1;
                }

                /* skip mb */
                s->mb_intra = 0;
                for(i=0;i<12;i++)
                    s->block_last_index[i] = -1;
                if(s->picture_structure == PICT_FRAME)
                    s->mv_type = MV_TYPE_16X16;
                else
                    s->mv_type = MV_TYPE_FIELD;
1847
                if (s->pict_type == FF_P_TYPE) {
1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861
                    /* if P type, zero motion vector is implied */
                    s->mv_dir = MV_DIR_FORWARD;
                    s->mv[0][0][0] = s->mv[0][0][1] = 0;
                    s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
                    s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
                    s->field_select[0][0]= s->picture_structure - 1;
                } else {
                    /* if B type, reuse previous vectors and directions */
                    s->mv[0][0][0] = s->last_mv[0][0][0];
                    s->mv[0][0][1] = s->last_mv[0][0][1];
                    s->mv[1][0][0] = s->last_mv[1][0][0];
                    s->mv[1][0][1] = s->last_mv[1][0][1];
                }
            }
michaelni's avatar
michaelni committed
1862
        }
glantau's avatar
glantau committed
1863
    }
1864
eos: // end of slice
1865
    *buf += (get_bits_count(&s->gb)-1)/8;
1866
//printf("y %d %d %d %d\n", s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y);
1867 1868
    return 0;
}
1869

michael's avatar
michael committed
1870
static int slice_decode_thread(AVCodecContext *c, void *arg){
1871
    MpegEncContext *s= *(void**)arg;
kabi's avatar
kabi committed
1872
    const uint8_t *buf= s->gb.buffer;
michael's avatar
michael committed
1873 1874 1875 1876 1877
    int mb_y= s->start_mb_y;

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

    for(;;){
mru's avatar
mru committed
1878 1879
        uint32_t start_code;
        int ret;
michael's avatar
michael committed
1880 1881 1882

        ret= mpeg_decode_slice((Mpeg1Context*)s, mb_y, &buf, s->gb.buffer_end - buf);
        emms_c();
1883
//av_log(c, AV_LOG_DEBUG, "ret:%d resync:%d/%d mb:%d/%d ts:%d/%d ec:%d\n",
michael's avatar
michael committed
1884 1885 1886 1887 1888 1889 1890
//ret, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, s->start_mb_y, s->end_mb_y, s->error_count);
        if(ret < 0){
            if(s->resync_mb_x>=0 && s->resync_mb_y>=0)
                ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, AC_ERROR|DC_ERROR|MV_ERROR);
        }else{
            ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, AC_END|DC_END|MV_END);
        }
1891

michael's avatar
michael committed
1892 1893
        if(s->mb_y == s->end_mb_y)
            return 0;
1894

michael's avatar
michael committed
1895 1896
        start_code= -1;
        buf = ff_find_start_code(buf, s->gb.buffer_end, &start_code);
michael's avatar
michael committed
1897 1898 1899 1900
        mb_y= start_code - SLICE_MIN_START_CODE;
        if(mb_y < 0 || mb_y >= s->end_mb_y)
            return -1;
    }
1901

michael's avatar
michael committed
1902 1903 1904
    return 0; //not reached
}

1905
/**
1906 1907
 * Handles slice ends.
 * @return 1 if it seems to be the last slice
1908 1909 1910 1911 1912
 */
static int slice_end(AVCodecContext *avctx, AVFrame *pict)
{
    Mpeg1Context *s1 = avctx->priv_data;
    MpegEncContext *s = &s1->mpeg_enc_ctx;
1913

romansh's avatar
 
romansh committed
1914
    if (!s1->mpeg_enc_ctx_allocated || !s->current_picture_ptr)
1915 1916
        return 0;

1917
    if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
1918
        ff_xvmc_field_end(s);
1919

glantau's avatar
glantau committed
1920
    /* end of slice reached */
1921
    if (/*s->mb_y<<field_pic == s->mb_height &&*/ !s->first_field) {
glantau's avatar
glantau committed
1922
        /* end of image */
michaelni's avatar
michaelni committed
1923

michael's avatar
michael committed
1924
        s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_MPEG2;
michaelni's avatar
michaelni committed
1925

1926
        ff_er_frame_end(s);
glantau's avatar
glantau committed
1927 1928 1929

        MPV_frame_end(s);

1930
        if (s->pict_type == FF_B_TYPE || s->low_delay) {
michaelni's avatar
michaelni committed
1931
            *pict= *(AVFrame*)s->current_picture_ptr;
1932
            ff_print_debug_info(s, pict);
glantau's avatar
glantau committed
1933
        } else {
michaelni's avatar
michaelni committed
1934
            s->picture_number++;
1935
            /* latency of 1 frame for I- and P-frames */
glantau's avatar
glantau committed
1936
            /* XXX: use another variable than picture_number */
michaelni's avatar
michaelni committed
1937
            if (s->last_picture_ptr != NULL) {
michaelni's avatar
michaelni committed
1938
                *pict= *(AVFrame*)s->last_picture_ptr;
1939
                 ff_print_debug_info(s, pict);
glantau's avatar
glantau committed
1940 1941
            }
        }
michaelni's avatar
michaelni committed
1942

1943
        return 1;
glantau's avatar
glantau committed
1944
    } else {
1945
        return 0;
glantau's avatar
glantau committed
1946 1947 1948
    }
}

1949
static int mpeg1_decode_sequence(AVCodecContext *avctx,
kabi's avatar
kabi committed
1950
                                 const uint8_t *buf, int buf_size)
glantau's avatar
glantau committed
1951 1952 1953
{
    Mpeg1Context *s1 = avctx->priv_data;
    MpegEncContext *s = &s1->mpeg_enc_ctx;
1954
    int width,height;
1955
    int i, v, j;
1956

1957
    init_get_bits(&s->gb, buf, buf_size*8);
glantau's avatar
glantau committed
1958

1959 1960
    width = get_bits(&s->gb, 12);
    height = get_bits(&s->gb, 12);
michael's avatar
michael committed
1961
    if (width <= 0 || height <= 0)
1962
        return -1;
michaelni's avatar
michaelni committed
1963
    s->aspect_ratio_info= get_bits(&s->gb, 4);
1964 1965
    if (s->aspect_ratio_info == 0) {
        av_log(avctx, AV_LOG_ERROR, "aspect ratio has forbidden 0 value\n");
1966
        if (avctx->error_recognition >= FF_ER_COMPLIANT)
1967 1968
            return -1;
    }
glantau's avatar
glantau committed
1969
    s->frame_rate_index = get_bits(&s->gb, 4);
michael's avatar
michael committed
1970
    if (s->frame_rate_index == 0 || s->frame_rate_index > 13)
glantau's avatar
glantau committed
1971 1972
        return -1;
    s->bit_rate = get_bits(&s->gb, 18) * 400;
1973
    if (get_bits1(&s->gb) == 0) /* marker */
glantau's avatar
glantau committed
1974
        return -1;
1975 1976
    s->width = width;
    s->height = height;
glantau's avatar
glantau committed
1977

1978
    s->avctx->rc_buffer_size= get_bits(&s->gb, 10) * 1024*16;
1979
    skip_bits(&s->gb, 1);
glantau's avatar
glantau committed
1980 1981

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

2033 2034 2035 2036
    if(show_bits(&s->gb, 23) != 0){
        av_log(s->avctx, AV_LOG_ERROR, "sequence header damaged\n");
        return -1;
    }
glantau's avatar
glantau committed
2037

2038
    /* we set MPEG-2 parameters so that it emulates MPEG-1 */
glantau's avatar
glantau committed
2039 2040 2041 2042
    s->progressive_sequence = 1;
    s->progressive_frame = 1;
    s->picture_structure = PICT_FRAME;
    s->frame_pred_frame_dct = 1;
iive's avatar
iive committed
2043
    s->chroma_format = 1;
michaelni's avatar
michaelni committed
2044
    s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG1VIDEO;
2045
    avctx->sub_id = 1; /* indicates MPEG-1 */
2046
    s->out_format = FMT_MPEG1;
2047
    s->swap_uv = 0;//AFAIK VCR2 does not have SEQ_HEADER
bellard's avatar
bellard committed
2048
    if(s->flags & CODEC_FLAG_LOW_DELAY) s->low_delay=1;
2049

2050
    if(s->avctx->debug & FF_DEBUG_PICT_INFO)
2051
        av_log(s->avctx, AV_LOG_DEBUG, "vbv buffer: %d, bitrate:%d\n",
2052
               s->avctx->rc_buffer_size, s->bit_rate);
2053

glantau's avatar
glantau committed
2054 2055 2056
    return 0;
}

michaelni's avatar
michaelni committed
2057 2058 2059 2060
static int vcr2_init_sequence(AVCodecContext *avctx)
{
    Mpeg1Context *s1 = avctx->priv_data;
    MpegEncContext *s = &s1->mpeg_enc_ctx;
michaelni's avatar
michaelni committed
2061
    int i, v;
michaelni's avatar
michaelni committed
2062

2063
    /* start new MPEG-1 context decoding */
michaelni's avatar
michaelni committed
2064 2065 2066 2067
    s->out_format = FMT_MPEG1;
    if (s1->mpeg_enc_ctx_allocated) {
        MPV_common_end(s);
    }
2068 2069
    s->width  = avctx->coded_width;
    s->height = avctx->coded_height;
michaelni's avatar
michaelni committed
2070
    avctx->has_b_frames= 0; //true?
michaelni's avatar
michaelni committed
2071
    s->low_delay= 1;
iive's avatar
iive committed
2072

2073
    avctx->pix_fmt = mpeg_get_pixelformat(avctx);
iive's avatar
iive committed
2074

2075 2076
    if( avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT ||
        s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU )
2077 2078
        if( avctx->idct_algo == FF_IDCT_AUTO )
            avctx->idct_algo = FF_IDCT_SIMPLE;
2079

michaelni's avatar
michaelni committed
2080 2081
    if (MPV_common_init(s) < 0)
        return -1;
2082
    exchange_uv(s);//common init reset pblocks, so we swap them here
2083
    s->swap_uv = 1;// in case of xvmc we need to swap uv for each MB
michaelni's avatar
michaelni committed
2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100
    s1->mpeg_enc_ctx_allocated = 1;

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

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

    s->progressive_sequence = 1;
    s->progressive_frame = 1;
    s->picture_structure = PICT_FRAME;
    s->frame_pred_frame_dct = 1;
iive's avatar
iive committed
2101
    s->chroma_format = 1;
michaelni's avatar
michaelni committed
2102
    s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG2VIDEO;
2103
    avctx->sub_id = 2; /* indicates MPEG-2 */
michaelni's avatar
michaelni committed
2104 2105 2106 2107
    return 0;
}


2108
static void mpeg_decode_user_data(AVCodecContext *avctx,
2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136
                                  const uint8_t *buf, int buf_size)
{
    const uint8_t *p;
    int len, flags;
    p = buf;
    len = buf_size;

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

2137
static void mpeg_decode_gop(AVCodecContext *avctx,
2138 2139 2140 2141 2142 2143 2144
                            const uint8_t *buf, int buf_size){
    Mpeg1Context *s1 = avctx->priv_data;
    MpegEncContext *s = &s1->mpeg_enc_ctx;

    int drop_frame_flag;
    int time_code_hours, time_code_minutes;
    int time_code_seconds, time_code_pictures;
2145
    int closed_gop, broken_link;
2146 2147 2148 2149

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

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

2151 2152 2153 2154 2155 2156
    time_code_hours=get_bits(&s->gb,5);
    time_code_minutes = get_bits(&s->gb,6);
    skip_bits1(&s->gb);//marker bit
    time_code_seconds = get_bits(&s->gb,6);
    time_code_pictures = get_bits(&s->gb,6);

2157
    closed_gop  = get_bits1(&s->gb);
2158 2159 2160 2161 2162 2163
    /*broken_link indicate that after editing the
      reference frames of the first B-Frames after GOP I-Frame
      are missing (open gop)*/
    broken_link = get_bits1(&s->gb);

    if(s->avctx->debug & FF_DEBUG_PICT_INFO)
2164
        av_log(s->avctx, AV_LOG_DEBUG, "GOP (%2d:%02d:%02d.[%02d]) closed_gop=%d broken_link=%d\n",
2165
            time_code_hours, time_code_minutes, time_code_seconds,
2166
            time_code_pictures, closed_gop, broken_link);
2167
}
michaelni's avatar
michaelni committed
2168
/**
2169
 * Finds the end of the current frame in the bitstream.
michaelni's avatar
michaelni committed
2170 2171
 * @return the position of the first byte of the next frame, or -1
 */
2172
int ff_mpeg1_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size)
2173
{
michaelni's avatar
michaelni committed
2174
    int i;
michael's avatar
michael committed
2175
    uint32_t state= pc->state;
2176

2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199
    /* EOF considered as end of frame */
    if (buf_size == 0)
        return 0;

/*
 0  frame start         -> 1/4
 1  first_SEQEXT        -> 0/2
 2  first field start   -> 3/0
 3  second_SEQEXT       -> 2/0
 4  searching end
*/

    for(i=0; i<buf_size; i++){
        assert(pc->frame_start_found>=0 && pc->frame_start_found<=4);
        if(pc->frame_start_found&1){
            if(state == EXT_START_CODE && (buf[i]&0xF0) != 0x80)
                pc->frame_start_found--;
            else if(state == EXT_START_CODE+2){
                if((buf[i]&3) == 3) pc->frame_start_found= 0;
                else                pc->frame_start_found= (pc->frame_start_found+1)&3;
            }
            state++;
        }else{
michael's avatar
michael committed
2200
            i= ff_find_start_code(buf+i, buf+buf_size, &state) - buf - 1;
2201
            if(pc->frame_start_found==0 && state >= SLICE_MIN_START_CODE && state <= SLICE_MAX_START_CODE){
michaelni's avatar
michaelni committed
2202
                i++;
2203
                pc->frame_start_found=4;
michaelni's avatar
michaelni committed
2204
            }
2205 2206 2207 2208
            if(state == SEQ_END_CODE){
                pc->state=-1;
                return i+1;
            }
2209 2210 2211 2212 2213
            if(pc->frame_start_found==2 && state == SEQ_START_CODE)
                pc->frame_start_found= 0;
            if(pc->frame_start_found<4 && state == EXT_START_CODE)
                pc->frame_start_found++;
            if(pc->frame_start_found == 4 && (state&0xFFFFFF00) == 0x100){
michaelni's avatar
michaelni committed
2214 2215
                if(state < SLICE_MIN_START_CODE || state > SLICE_MAX_START_CODE){
                    pc->frame_start_found=0;
2216
                    pc->state=-1;
michaelni's avatar
michaelni committed
2217 2218 2219 2220
                    return i-3;
                }
            }
        }
2221
    }
michaelni's avatar
michaelni committed
2222
    pc->state= state;
2223
    return END_NOT_FOUND;
michaelni's avatar
michaelni committed
2224 2225
}

michael's avatar
michael committed
2226 2227 2228 2229
static int decode_chunks(AVCodecContext *avctx,
                             AVFrame *picture, int *data_size,
                             const uint8_t *buf, int buf_size);

glantau's avatar
glantau committed
2230
/* handle buffering and image synchronisation */
2231
static int mpeg_decode_frame(AVCodecContext *avctx,
glantau's avatar
glantau committed
2232
                             void *data, int *data_size,
michael's avatar
const  
michael committed
2233
                             const uint8_t *buf, int buf_size)
glantau's avatar
glantau committed
2234 2235
{
    Mpeg1Context *s = avctx->priv_data;
michaelni's avatar
michaelni committed
2236
    AVFrame *picture = data;
2237
    MpegEncContext *s2 = &s->mpeg_enc_ctx;
mbardiaux's avatar
mbardiaux committed
2238
    dprintf(avctx, "fill_buffer\n");
glantau's avatar
glantau committed
2239

2240
    if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == SEQ_END_CODE)) {
2241 2242 2243 2244
        /* special case for last picture */
        if (s2->low_delay==0 && s2->next_picture_ptr) {
            *picture= *(AVFrame*)s2->next_picture_ptr;
            s2->next_picture_ptr= NULL;
michaelni's avatar
michaelni committed
2245

2246 2247
            *data_size = sizeof(AVFrame);
        }
ramiro's avatar
ramiro committed
2248
        return buf_size;
glantau's avatar
glantau committed
2249 2250
    }

michaelni's avatar
michaelni committed
2251
    if(s2->flags&CODEC_FLAG_TRUNCATED){
2252
        int next= ff_mpeg1_find_frame_end(&s2->parse_context, buf, buf_size);
2253

2254
        if( ff_combine_frame(&s2->parse_context, next, (const uint8_t **)&buf, &buf_size) < 0 )
michaelni's avatar
michaelni committed
2255
            return buf_size;
2256 2257 2258 2259
    }

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

2270
    if(s->mpeg_enc_ctx_allocated==0 && avctx->codec_tag == AV_RL32("VCR2"))
michaelni's avatar
michaelni committed
2271
        vcr2_init_sequence(avctx);
2272

michael's avatar
michael committed
2273
    s->slice_count= 0;
2274

2275 2276 2277
    if(avctx->extradata && !avctx->frame_number)
        decode_chunks(avctx, picture, data_size, avctx->extradata, avctx->extradata_size);

michael's avatar
michael committed
2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290
    return decode_chunks(avctx, picture, data_size, buf, buf_size);
}

static int decode_chunks(AVCodecContext *avctx,
                             AVFrame *picture, int *data_size,
                             const uint8_t *buf, int buf_size)
{
    Mpeg1Context *s = avctx->priv_data;
    MpegEncContext *s2 = &s->mpeg_enc_ctx;
    const uint8_t *buf_ptr = buf;
    const uint8_t *buf_end = buf + buf_size;
    int ret, input_size;

michaelni's avatar
michaelni committed
2291
    for(;;) {
2292
        /* find next start code */
michael's avatar
michael committed
2293
        uint32_t start_code = -1;
michael's avatar
michael committed
2294
        buf_ptr = ff_find_start_code(buf_ptr,buf_end, &start_code);
mru's avatar
mru committed
2295
        if (start_code > 0x1ff){
2296
            if(s2->pict_type != FF_B_TYPE || avctx->skip_frame <= AVDISCARD_DEFAULT){
michael's avatar
michael committed
2297 2298 2299
                if(avctx->thread_count > 1){
                    int i;

2300
                    avctx->execute(avctx, slice_decode_thread,  (void**)&(s2->thread_context[0]), NULL, s->slice_count, sizeof(void*));
michael's avatar
michael committed
2301 2302 2303
                    for(i=0; i<s->slice_count; i++)
                        s2->error_count += s2->thread_context[i]->error_count;
                }
2304

2305
                if (CONFIG_MPEG_VDPAU_DECODER && avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
2306 2307
                    ff_vdpau_mpeg_picture_complete(s2, buf, buf_size, s->slice_count);

michaelni's avatar
michaelni committed
2308
                if (slice_end(avctx, picture)) {
bellard's avatar
bellard committed
2309
                    if(s2->last_picture_ptr || s2->low_delay) //FIXME merge with the stuff in mpeg_decode_slice
michaelni's avatar
michaelni committed
2310 2311
                        *data_size = sizeof(AVPicture);
                }
2312
            }
2313
            s2->pict_type= 0;
michaelni's avatar
10l  
michaelni committed
2314
            return FFMAX(0, buf_ptr - buf - s2->parse_context.last_index);
glantau's avatar
glantau committed
2315
        }
2316

2317 2318 2319
        input_size = buf_end - buf_ptr;

        if(avctx->debug & FF_DEBUG_STARTCODE){
2320
            av_log(avctx, AV_LOG_DEBUG, "%3X at %td left %d\n", start_code, buf_ptr-buf, input_size);
2321
        }
michaelni's avatar
michaelni committed
2322

2323 2324 2325 2326 2327 2328
        /* prepare data for next start code */
        switch(start_code) {
        case SEQ_START_CODE:
            mpeg1_decode_sequence(avctx, buf_ptr,
                                    input_size);
            break;
2329

2330
        case PICTURE_START_CODE:
2331
            /* we have a complete image: we try to decompress it */
2332 2333 2334
            if(mpeg1_decode_picture(avctx,
                                    buf_ptr, input_size) < 0)
                s2->pict_type=0;
2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354
            break;
        case EXT_START_CODE:
            mpeg_decode_extension(avctx,
                                    buf_ptr, input_size);
            break;
        case USER_START_CODE:
            mpeg_decode_user_data(avctx,
                                    buf_ptr, input_size);
            break;
        case GOP_START_CODE:
            s2->first_field=0;
            mpeg_decode_gop(avctx,
                                    buf_ptr, input_size);
            break;
        default:
            if (start_code >= SLICE_MIN_START_CODE &&
                start_code <= SLICE_MAX_START_CODE) {
                int mb_y= start_code - SLICE_MIN_START_CODE;

                if(s2->last_picture_ptr==NULL){
diego's avatar
diego committed
2355
                /* Skip B-frames if we do not have reference frames. */
2356
                    if(s2->pict_type==FF_B_TYPE) break;
2357 2358
                }
                if(s2->next_picture_ptr==NULL){
2359
                /* Skip P-frames if we do not have a reference frame or we have an invalid header. */
2360
                    if(s2->pict_type==FF_P_TYPE && (s2->first_field || s2->picture_structure==PICT_FRAME)) break;
2361
                }
diego's avatar
diego committed
2362
                /* Skip B-frames if we are in a hurry. */
2363 2364 2365
                if(avctx->hurry_up && s2->pict_type==FF_B_TYPE) break;
                if(  (avctx->skip_frame >= AVDISCARD_NONREF && s2->pict_type==FF_B_TYPE)
                    ||(avctx->skip_frame >= AVDISCARD_NONKEY && s2->pict_type!=FF_I_TYPE)
2366
                    || avctx->skip_frame >= AVDISCARD_ALL)
michaelni's avatar
michaelni committed
2367
                    break;
diego's avatar
diego committed
2368
                /* Skip everything if we are in a hurry>=5. */
2369
                if(avctx->hurry_up>=5) break;
2370

2371 2372 2373 2374 2375 2376
                if (!s->mpeg_enc_ctx_allocated) break;

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

2378 2379 2380 2381 2382
                if(!s2->pict_type){
                    av_log(avctx, AV_LOG_ERROR, "Missing picture start code\n");
                    break;
                }

2383 2384
                if(s2->first_slice){
                    s2->first_slice=0;
iive's avatar
iive committed
2385
                    if(mpeg_field_start(s2) < 0)
2386
                        return -1;
iive's avatar
iive committed
2387
                }
2388
                if(!s2->current_picture_ptr){
diego's avatar
diego committed
2389
                    av_log(avctx, AV_LOG_ERROR, "current_picture not initialized\n");
2390 2391
                    return -1;
                }
2392

2393 2394 2395 2396 2397
                if (avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU) {
                    s->slice_count++;
                    break;
                }

2398 2399 2400 2401 2402 2403 2404 2405 2406 2407
                if(avctx->thread_count > 1){
                    int threshold= (s2->mb_height*s->slice_count + avctx->thread_count/2) / avctx->thread_count;
                    if(threshold <= mb_y){
                        MpegEncContext *thread_context= s2->thread_context[s->slice_count];

                        thread_context->start_mb_y= mb_y;
                        thread_context->end_mb_y  = s2->mb_height;
                        if(s->slice_count){
                            s2->thread_context[s->slice_count-1]->end_mb_y= mb_y;
                            ff_update_duplicate_context(thread_context, s2);
glantau's avatar
glantau committed
2408
                        }
2409 2410 2411
                        init_get_bits(&thread_context->gb, buf_ptr, input_size*8);
                        s->slice_count++;
                    }
2412
                    buf_ptr += 2; //FIXME add minimum number of bytes per slice
2413 2414 2415 2416 2417 2418 2419 2420 2421
                }else{
                    ret = mpeg_decode_slice(s, mb_y, &buf_ptr, input_size);
                    emms_c();

                    if(ret < 0){
                        if(s2->resync_mb_x>=0 && s2->resync_mb_y>=0)
                            ff_er_add_slice(s2, s2->resync_mb_x, s2->resync_mb_y, s2->mb_x, s2->mb_y, AC_ERROR|DC_ERROR|MV_ERROR);
                    }else{
                        ff_er_add_slice(s2, s2->resync_mb_x, s2->resync_mb_y, s2->mb_x-1, s2->mb_y, AC_END|DC_END|MV_END);
glantau's avatar
glantau committed
2422 2423
                    }
                }
2424 2425 2426
            }
            break;
        }
glantau's avatar
glantau committed
2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438
    }
}

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

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

michaelni's avatar
michaelni committed
2439 2440
AVCodec mpeg1video_decoder = {
    "mpeg1video",
glantau's avatar
glantau committed
2441 2442 2443 2444 2445 2446 2447
    CODEC_TYPE_VIDEO,
    CODEC_ID_MPEG1VIDEO,
    sizeof(Mpeg1Context),
    mpeg_decode_init,
    NULL,
    mpeg_decode_end,
    mpeg_decode_frame,
2448
    CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
michaelni's avatar
michaelni committed
2449
    .flush= ff_mpeg_flush,
2450
    .long_name= NULL_IF_CONFIG_SMALL("MPEG-1 video"),
michaelni's avatar
michaelni committed
2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461
};

AVCodec mpeg2video_decoder = {
    "mpeg2video",
    CODEC_TYPE_VIDEO,
    CODEC_ID_MPEG2VIDEO,
    sizeof(Mpeg1Context),
    mpeg_decode_init,
    NULL,
    mpeg_decode_end,
    mpeg_decode_frame,
2462
    CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
iive's avatar
iive committed
2463
    .flush= ff_mpeg_flush,
2464
    .long_name= NULL_IF_CONFIG_SMALL("MPEG-2 video"),
iive's avatar
iive committed
2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476
};

//legacy decoder
AVCodec mpegvideo_decoder = {
    "mpegvideo",
    CODEC_TYPE_VIDEO,
    CODEC_ID_MPEG2VIDEO,
    sizeof(Mpeg1Context),
    mpeg_decode_init,
    NULL,
    mpeg_decode_end,
    mpeg_decode_frame,
2477
    CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
michaelni's avatar
michaelni committed
2478
    .flush= ff_mpeg_flush,
2479
    .long_name= NULL_IF_CONFIG_SMALL("MPEG-1 video"),
glantau's avatar
glantau committed
2480
};
iive's avatar
iive committed
2481

2482
#if CONFIG_MPEG_XVMC_DECODER
2483
static av_cold int mpeg_mc_decode_init(AVCodecContext *avctx){
iive's avatar
iive committed
2484 2485
    Mpeg1Context *s;

2486
    if( avctx->thread_count > 1)
2487
        return -1;
iive's avatar
iive committed
2488 2489
    if( !(avctx->slice_flags & SLICE_FLAG_CODED_ORDER) )
        return -1;
2490
    if( !(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD) ){
mbardiaux's avatar
mbardiaux committed
2491
        dprintf(avctx, "mpeg12.c: XvMC decoder will work better if SLICE_FLAG_ALLOW_FIELD is set\n");
2492
    }
iive's avatar
iive committed
2493 2494 2495 2496
    mpeg_decode_init(avctx);
    s = avctx->priv_data;

    avctx->pix_fmt = PIX_FMT_XVMC_MPEG2_IDCT;
2497
    avctx->xvmc_acceleration = 2;//2 - the blocks are packed!
iive's avatar
iive committed
2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510

    return 0;
}

AVCodec mpeg_xvmc_decoder = {
    "mpegvideo_xvmc",
    CODEC_TYPE_VIDEO,
    CODEC_ID_MPEG2VIDEO_XVMC,
    sizeof(Mpeg1Context),
    mpeg_mc_decode_init,
    NULL,
    mpeg_decode_end,
    mpeg_decode_frame,
2511
    CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED| CODEC_CAP_HWACCEL | CODEC_CAP_DELAY,
2512
    .flush= ff_mpeg_flush,
2513
    .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video XvMC (X-Video Motion Compensation)"),
iive's avatar
iive committed
2514 2515 2516
};

#endif
2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533

#if CONFIG_MPEG_VDPAU_DECODER
AVCodec mpeg_vdpau_decoder = {
    "mpegvideo_vdpau",
    CODEC_TYPE_VIDEO,
    CODEC_ID_MPEG2VIDEO,
    sizeof(Mpeg1Context),
    mpeg_decode_init,
    NULL,
    mpeg_decode_end,
    mpeg_decode_frame,
    CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_HWACCEL_VDPAU | CODEC_CAP_DELAY,
    .flush= ff_mpeg_flush,
    .long_name = NULL_IF_CONFIG_SMALL("MPEG-1/2 video (VDPAU acceleration)"),
};
#endif

cehoyos's avatar
cehoyos committed
2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549
#if CONFIG_MPEG1_VDPAU_DECODER
AVCodec mpeg1_vdpau_decoder = {
    "mpeg1video_vdpau",
    CODEC_TYPE_VIDEO,
    CODEC_ID_MPEG1VIDEO,
    sizeof(Mpeg1Context),
    mpeg_decode_init,
    NULL,
    mpeg_decode_end,
    mpeg_decode_frame,
    CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_HWACCEL_VDPAU | CODEC_CAP_DELAY,
    .flush= ff_mpeg_flush,
    .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video (VDPAU acceleration)"),
};
#endif