msmpeg4.c 61.3 KB
Newer Older
Fabrice Bellard's avatar
Fabrice Bellard committed
1 2
/*
 * MSMPEG4 backend for ffmpeg encoder and decoder
3
 * Copyright (c) 2001 Fabrice Bellard
4
 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
Fabrice Bellard's avatar
Fabrice Bellard committed
5
 *
6 7
 * msmpeg4v1 & v2 stuff by Michael Niedermayer <michaelni@gmx.at>
 *
8 9 10
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
11 12
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
13
 * version 2.1 of the License, or (at your option) any later version.
Fabrice Bellard's avatar
Fabrice Bellard committed
14
 *
15
 * FFmpeg is distributed in the hope that it will be useful,
Fabrice Bellard's avatar
Fabrice Bellard committed
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
Fabrice Bellard's avatar
Fabrice Bellard committed
19
 *
20
 * You should have received a copy of the GNU Lesser General Public
21
 * License along with FFmpeg; if not, write to the Free Software
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Fabrice Bellard's avatar
Fabrice Bellard committed
23
 */
Michael Niedermayer's avatar
Michael Niedermayer committed
24 25

/**
26
 * @file libavcodec/msmpeg4.c
Michael Niedermayer's avatar
Michael Niedermayer committed
27 28 29
 * MSMPEG4 backend for ffmpeg encoder and decoder.
 */

30
#include "avcodec.h"
Fabrice Bellard's avatar
Fabrice Bellard committed
31 32
#include "dsputil.h"
#include "mpegvideo.h"
33
#include "msmpeg4.h"
34

Fabrice Bellard's avatar
Fabrice Bellard committed
35
/*
36
 * You can also call this codec : MPEG4 with a twist !
Fabrice Bellard's avatar
Fabrice Bellard committed
37
 *
38
 * TODO:
Fabrice Bellard's avatar
Fabrice Bellard committed
39
 *        - (encoding) select best mv table (two choices)
40
 *        - (encoding) select best vlc/dc table
Fabrice Bellard's avatar
Fabrice Bellard committed
41 42 43
 */
//#define DEBUG

44 45 46 47 48 49 50 51 52 53
#define DC_VLC_BITS 9
#define CBPY_VLC_BITS 6
#define V1_INTRA_CBPC_VLC_BITS 6
#define V1_INTER_CBPC_VLC_BITS 6
#define V2_INTRA_CBPC_VLC_BITS 3
#define V2_MB_TYPE_VLC_BITS 7
#define MV_VLC_BITS 9
#define V2_MV_VLC_BITS 9
#define TEX_VLC_BITS 9

54 55 56
#define II_BITRATE 128*1024
#define MBAC_BITRATE 50*1024

Michael Niedermayer's avatar
Michael Niedermayer committed
57 58
#define DEFAULT_INTER_INDEX 3

59 60
static uint32_t v2_dc_lum_table[512][2];
static uint32_t v2_dc_chroma_table[512][2];
61

Fabrice Bellard's avatar
Fabrice Bellard committed
62
static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr);
Falk Hüffner's avatar
Falk Hüffner committed
63
static void init_h263_dc_for_msmpeg4(void);
64
static inline void msmpeg4_memsetw(short *tab, int val, int n);
65
#if CONFIG_ENCODERS
66
static void msmpeg4v2_encode_motion(MpegEncContext * s, int val);
67
static int get_size_of_code(MpegEncContext * s, RLTable *rl, int last, int run, int level, int intra);
68
#endif //CONFIG_ENCODERS
69 70
static int msmpeg4v12_decode_mb(MpegEncContext *s, DCTELEM block[6][64]);
static int msmpeg4v34_decode_mb(MpegEncContext *s, DCTELEM block[6][64]);
Fabrice Bellard's avatar
Fabrice Bellard committed
71

72
/* vc1 externs */
73
extern const uint8_t wmv3_dc_scale_table[32];
74

Fabrice Bellard's avatar
Fabrice Bellard committed
75 76 77 78 79 80 81
#ifdef DEBUG
int intra_count = 0;
int frame_count = 0;
#endif

#include "msmpeg4data.h"

82
#if CONFIG_ENCODERS //strangely gcc includes this even if it is not referenced
83
static uint8_t rl_length[NB_RL_TABLES][MAX_LEVEL+1][MAX_RUN+1][2];
84
#endif //CONFIG_ENCODERS
85

86 87
static uint8_t static_rl_table_store[NB_RL_TABLES][2][2*MAX_RUN + MAX_LEVEL + 3];

88
static av_cold void common_init(MpegEncContext * s)
Michael Niedermayer's avatar
Michael Niedermayer committed
89
{
90
    static int initialized=0;
91

Michael Niedermayer's avatar
Michael Niedermayer committed
92 93 94 95 96 97 98 99 100
    switch(s->msmpeg4_version){
    case 1:
    case 2:
        s->y_dc_scale_table=
        s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
        break;
    case 3:
        if(s->workaround_bugs){
            s->y_dc_scale_table= old_ff_y_dc_scale_table;
Stefan Gehrer's avatar
Stefan Gehrer committed
101
            s->c_dc_scale_table= wmv1_c_dc_scale_table;
Michael Niedermayer's avatar
Michael Niedermayer committed
102 103 104 105 106 107
        } else{
            s->y_dc_scale_table= ff_mpeg4_y_dc_scale_table;
            s->c_dc_scale_table= ff_mpeg4_c_dc_scale_table;
        }
        break;
    case 4:
Michael Niedermayer's avatar
Michael Niedermayer committed
108
    case 5:
Michael Niedermayer's avatar
Michael Niedermayer committed
109 110 111
        s->y_dc_scale_table= wmv1_y_dc_scale_table;
        s->c_dc_scale_table= wmv1_c_dc_scale_table;
        break;
112
#if CONFIG_WMV3_DECODER || CONFIG_VC1_DECODER
anonymous's avatar
anonymous committed
113 114 115 116
    case 6:
        s->y_dc_scale_table= wmv3_dc_scale_table;
        s->c_dc_scale_table= wmv3_dc_scale_table;
        break;
117
#endif
anonymous's avatar
anonymous committed
118

Michael Niedermayer's avatar
Michael Niedermayer committed
119 120
    }

121

Michael Niedermayer's avatar
Michael Niedermayer committed
122
    if(s->msmpeg4_version>=4){
Michael Niedermayer's avatar
Michael Niedermayer committed
123 124 125 126
        ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable  , wmv1_scantable[1]);
        ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, wmv1_scantable[2]);
        ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, wmv1_scantable[3]);
        ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable  , wmv1_scantable[0]);
Michael Niedermayer's avatar
Michael Niedermayer committed
127
    }
128
    //Note the default tables are set in common_init in mpegvideo.c
129

130 131
    if(!initialized){
        initialized=1;
Michael Niedermayer's avatar
Michael Niedermayer committed
132 133 134 135 136

        init_h263_dc_for_msmpeg4();
    }
}

137
#if CONFIG_ENCODERS
138

Fabrice Bellard's avatar
Fabrice Bellard committed
139 140 141 142 143
/* build the table which associate a (x,y) motion vector to a vlc */
static void init_mv_table(MVTable *tab)
{
    int i, x, y;

144
    tab->table_mv_index = av_malloc(sizeof(uint16_t) * 4096);
Fabrice Bellard's avatar
Fabrice Bellard committed
145 146 147
    /* mark all entries as not used */
    for(i=0;i<4096;i++)
        tab->table_mv_index[i] = tab->n;
148

Fabrice Bellard's avatar
Fabrice Bellard committed
149 150 151 152 153 154 155
    for(i=0;i<tab->n;i++) {
        x = tab->table_mvx[i];
        y = tab->table_mvy[i];
        tab->table_mv_index[(x << 6) | y] = i;
    }
}

156
void ff_msmpeg4_code012(PutBitContext *pb, int n)
Fabrice Bellard's avatar
Fabrice Bellard committed
157 158 159 160 161 162 163 164 165
{
    if (n == 0) {
        put_bits(pb, 1, 0);
    } else {
        put_bits(pb, 1, 1);
        put_bits(pb, 1, (n >= 2));
    }
}

166
av_cold void ff_msmpeg4_encode_init(MpegEncContext *s)
Michael Niedermayer's avatar
Michael Niedermayer committed
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
{
    static int init_done=0;
    int i;

    common_init(s);
    if(s->msmpeg4_version>=4){
        s->min_qcoeff= -255;
        s->max_qcoeff=  255;
    }

    if (!init_done) {
        /* init various encoding tables */
        init_done = 1;
        init_mv_table(&mv_tables[0]);
        init_mv_table(&mv_tables[1]);
        for(i=0;i<NB_RL_TABLES;i++)
183
            init_rl(&rl_table[i], static_rl_table_store[i]);
184 185 186 187 188 189 190 191

        for(i=0; i<NB_RL_TABLES; i++){
            int level;
            for(level=0; level<=MAX_LEVEL; level++){
                int run;
                for(run=0; run<=MAX_RUN; run++){
                    int last;
                    for(last=0; last<2; last++){
192
                        rl_length[i][level][run][last]= get_size_of_code(s, &rl_table[  i], last, run, level, 0);
193 194 195 196
                    }
                }
            }
        }
Michael Niedermayer's avatar
Michael Niedermayer committed
197 198 199 200 201 202 203
    }
}

static int get_size_of_code(MpegEncContext * s, RLTable *rl, int last, int run, int level, int intra){
    int size=0;
    int code;
    int run_diff= intra ? 0 : 1;
204

Michael Niedermayer's avatar
Michael Niedermayer committed
205 206 207 208 209 210
    code = get_rl_index(rl, last, run, level);
    size+= rl->table_vlc[code][1];
    if (code == rl->n) {
        int level1, run1;

        level1 = level - rl->max_level[last][run];
211
        if (level1 < 1)
Michael Niedermayer's avatar
Michael Niedermayer committed
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
            goto esc2;
        code = get_rl_index(rl, last, run, level1);
        if (code == rl->n) {
            esc2:
            size++;
            if (level > MAX_LEVEL)
                goto esc3;
            run1 = run - rl->max_run[last][level] - run_diff;
            if (run1 < 0)
                goto esc3;
            code = get_rl_index(rl, last, run1, level);
            if (code == rl->n) {
            esc3:
                /* third escape */
                size+=1+1+6+8;
            } else {
                /* second escape */
                size+= 1+1+ rl->table_vlc[code][1];
            }
        } else {
            /* first escape */
            size+= 1+1+ rl->table_vlc[code][1];
        }
    } else {
        size++;
    }
    return size;
}

241
static void find_best_tables(MpegEncContext * s)
Michael Niedermayer's avatar
Michael Niedermayer committed
242 243 244 245
{
    int i;
    int best       =-1, best_size       =9999999;
    int chroma_best=-1, best_chroma_size=9999999;
246

Michael Niedermayer's avatar
Michael Niedermayer committed
247 248 249 250 251 252
    for(i=0; i<3; i++){
        int level;
        int chroma_size=0;
        int size=0;

        if(i>0){// ;)
253
            size++;
Michael Niedermayer's avatar
Michael Niedermayer committed
254 255 256 257 258 259
            chroma_size++;
        }
        for(level=0; level<=MAX_LEVEL; level++){
            int run;
            for(run=0; run<=MAX_RUN; run++){
                int last;
260
                const int last_size= size + chroma_size;
Michael Niedermayer's avatar
Michael Niedermayer committed
261 262 263 264
                for(last=0; last<2; last++){
                    int inter_count       = s->ac_stats[0][0][level][run][last] + s->ac_stats[0][1][level][run][last];
                    int intra_luma_count  = s->ac_stats[1][0][level][run][last];
                    int intra_chroma_count= s->ac_stats[1][1][level][run][last];
265

266
                    if(s->pict_type==FF_I_TYPE){
267 268
                        size       += intra_luma_count  *rl_length[i  ][level][run][last];
                        chroma_size+= intra_chroma_count*rl_length[i+3][level][run][last];
Michael Niedermayer's avatar
Michael Niedermayer committed
269
                    }else{
270 271 272
                        size+=        intra_luma_count  *rl_length[i  ][level][run][last]
                                     +intra_chroma_count*rl_length[i+3][level][run][last]
                                     +inter_count       *rl_length[i+3][level][run][last];
273
                    }
Michael Niedermayer's avatar
Michael Niedermayer committed
274
                }
275
                if(last_size == size+chroma_size) break;
Michael Niedermayer's avatar
Michael Niedermayer committed
276 277 278 279 280 281 282 283 284 285 286
            }
        }
        if(size<best_size){
            best_size= size;
            best= i;
        }
        if(chroma_size<best_chroma_size){
            best_chroma_size= chroma_size;
            chroma_best= i;
        }
    }
287

288
//    printf("type:%d, best:%d, qp:%d, var:%d, mcvar:%d, size:%d //\n",
Michael Niedermayer's avatar
Michael Niedermayer committed
289
//           s->pict_type, best, s->qscale, s->mb_var_sum, s->mc_mb_var_sum, best_size);
290

291
    if(s->pict_type==FF_P_TYPE) chroma_best= best;
Michael Niedermayer's avatar
Michael Niedermayer committed
292 293 294 295 296

    memset(s->ac_stats, 0, sizeof(int)*(MAX_LEVEL+1)*(MAX_RUN+1)*2*2*2);

    s->rl_table_index       =        best;
    s->rl_chroma_table_index= chroma_best;
297

Michael Niedermayer's avatar
Michael Niedermayer committed
298 299
    if(s->pict_type != s->last_non_b_pict_type){
        s->rl_table_index= 2;
300
        if(s->pict_type==FF_I_TYPE)
Michael Niedermayer's avatar
Michael Niedermayer committed
301 302 303 304 305 306 307
            s->rl_chroma_table_index= 1;
        else
            s->rl_chroma_table_index= 2;
    }

}

Michael Niedermayer's avatar
Michael Niedermayer committed
308
/* write MSMPEG4 compatible frame header */
Fabrice Bellard's avatar
Fabrice Bellard committed
309 310
void msmpeg4_encode_picture_header(MpegEncContext * s, int picture_number)
{
311
    find_best_tables(s);
Fabrice Bellard's avatar
Fabrice Bellard committed
312 313 314 315 316

    align_put_bits(&s->pb);
    put_bits(&s->pb, 2, s->pict_type - 1);

    put_bits(&s->pb, 5, s->qscale);
Michael Niedermayer's avatar
Michael Niedermayer committed
317 318 319 320
    if(s->msmpeg4_version<=2){
        s->rl_table_index = 2;
        s->rl_chroma_table_index = 2;
    }
Michael Niedermayer's avatar
Michael Niedermayer committed
321

Fabrice Bellard's avatar
Fabrice Bellard committed
322 323 324
    s->dc_table_index = 1;
    s->mv_table_index = 1; /* only if P frame */
    s->use_skip_mb_code = 1; /* only if P frame */
Michael Niedermayer's avatar
Michael Niedermayer committed
325
    s->per_mb_rl_table = 0;
326
    if(s->msmpeg4_version==4)
327
        s->inter_intra_pred= (s->width*s->height < 320*240 && s->bit_rate<=II_BITRATE && s->pict_type==FF_P_TYPE);
Michael Niedermayer's avatar
Michael Niedermayer committed
328
//printf("%d %d %d %d %d\n", s->pict_type, s->bit_rate, s->inter_intra_pred, s->width, s->height);
Michael Niedermayer's avatar
Michael Niedermayer committed
329

330
    if (s->pict_type == FF_I_TYPE) {
331 332
        s->slice_height= s->mb_height/1;
        put_bits(&s->pb, 5, 0x16 + s->mb_height/s->slice_height);
333

Michael Niedermayer's avatar
Michael Niedermayer committed
334 335
        if(s->msmpeg4_version==4){
            msmpeg4_encode_ext_header(s);
336
            if(s->bit_rate>MBAC_BITRATE)
337
                put_bits(&s->pb, 1, s->per_mb_rl_table);
Michael Niedermayer's avatar
Michael Niedermayer committed
338
        }
Fabrice Bellard's avatar
Fabrice Bellard committed
339

Michael Niedermayer's avatar
Michael Niedermayer committed
340
        if(s->msmpeg4_version>2){
Michael Niedermayer's avatar
Michael Niedermayer committed
341
            if(!s->per_mb_rl_table){
342 343
                ff_msmpeg4_code012(&s->pb, s->rl_chroma_table_index);
                ff_msmpeg4_code012(&s->pb, s->rl_table_index);
Michael Niedermayer's avatar
Michael Niedermayer committed
344
            }
Fabrice Bellard's avatar
Fabrice Bellard committed
345

Michael Niedermayer's avatar
Michael Niedermayer committed
346 347
            put_bits(&s->pb, 1, s->dc_table_index);
        }
Fabrice Bellard's avatar
Fabrice Bellard committed
348 349
    } else {
        put_bits(&s->pb, 1, s->use_skip_mb_code);
350

351
        if(s->msmpeg4_version==4 && s->bit_rate>MBAC_BITRATE)
Michael Niedermayer's avatar
Michael Niedermayer committed
352 353
            put_bits(&s->pb, 1, s->per_mb_rl_table);

Michael Niedermayer's avatar
Michael Niedermayer committed
354
        if(s->msmpeg4_version>2){
Michael Niedermayer's avatar
Michael Niedermayer committed
355
            if(!s->per_mb_rl_table)
356
                ff_msmpeg4_code012(&s->pb, s->rl_table_index);
Fabrice Bellard's avatar
Fabrice Bellard committed
357

Michael Niedermayer's avatar
Michael Niedermayer committed
358 359 360 361
            put_bits(&s->pb, 1, s->dc_table_index);

            put_bits(&s->pb, 1, s->mv_table_index);
        }
Fabrice Bellard's avatar
Fabrice Bellard committed
362 363
    }

Michael Niedermayer's avatar
Michael Niedermayer committed
364 365
    s->esc3_level_length= 0;
    s->esc3_run_length= 0;
Fabrice Bellard's avatar
Fabrice Bellard committed
366 367 368

#ifdef DEBUG
    intra_count = 0;
369
    av_log(s->avctx, AV_LOG_DEBUG, "*****frame %d:\n", frame_count++);
Fabrice Bellard's avatar
Fabrice Bellard committed
370 371 372
#endif
}

373 374
void msmpeg4_encode_ext_header(MpegEncContext * s)
{
375
        put_bits(&s->pb, 5, s->avctx->time_base.den / s->avctx->time_base.num); //yes 29.97 -> 29
376

377
        put_bits(&s->pb, 11, FFMIN(s->bit_rate/1024, 2047));
378

379
        if(s->msmpeg4_version>=3)
Michael Niedermayer's avatar
Michael Niedermayer committed
380
            put_bits(&s->pb, 1, s->flipflop_rounding);
381 382
        else
            assert(s->flipflop_rounding==0);
383 384
}

385 386
#endif //CONFIG_ENCODERS

Fabrice Bellard's avatar
Fabrice Bellard committed
387
/* predict coded block */
388
int ff_msmpeg4_coded_block_pred(MpegEncContext * s, int n, uint8_t **coded_block_ptr)
Fabrice Bellard's avatar
Fabrice Bellard committed
389
{
390
    int xy, wrap, pred, a, b, c;
Fabrice Bellard's avatar
Fabrice Bellard committed
391

392
    xy = s->block_index[n];
393
    wrap = s->b8_stride;
Fabrice Bellard's avatar
Fabrice Bellard committed
394 395

    /* B C
396
     * A X
Fabrice Bellard's avatar
Fabrice Bellard committed
397
     */
398 399 400
    a = s->coded_block[xy - 1       ];
    b = s->coded_block[xy - 1 - wrap];
    c = s->coded_block[xy     - wrap];
401

Fabrice Bellard's avatar
Fabrice Bellard committed
402 403 404 405 406
    if (b == c) {
        pred = a;
    } else {
        pred = c;
    }
407

Fabrice Bellard's avatar
Fabrice Bellard committed
408
    /* store value */
409
    *coded_block_ptr = &s->coded_block[xy];
Fabrice Bellard's avatar
Fabrice Bellard committed
410 411 412 413

    return pred;
}

414
#if CONFIG_ENCODERS
415

416
void ff_msmpeg4_encode_motion(MpegEncContext * s,
Fabrice Bellard's avatar
Fabrice Bellard committed
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
                                  int mx, int my)
{
    int code;
    MVTable *mv;

    /* modulo encoding */
    /* WARNING : you cannot reach all the MVs even with the modulo
       encoding. This is a somewhat strange compromise they took !!!  */
    if (mx <= -64)
        mx += 64;
    else if (mx >= 64)
        mx -= 64;
    if (my <= -64)
        my += 64;
    else if (my >= 64)
        my -= 64;
433

Fabrice Bellard's avatar
Fabrice Bellard committed
434 435 436 437
    mx += 32;
    my += 32;
#if 0
    if ((unsigned)mx >= 64 ||
438
        (unsigned)my >= 64)
439
        av_log(s->avctx, AV_LOG_ERROR, "error mx=%d my=%d\n", mx, my);
Fabrice Bellard's avatar
Fabrice Bellard committed
440 441 442 443
#endif
    mv = &mv_tables[s->mv_table_index];

    code = mv->table_mv_index[(mx << 6) | my];
444 445
    put_bits(&s->pb,
             mv->table_mv_bits[code],
Fabrice Bellard's avatar
Fabrice Bellard committed
446 447
             mv->table_mv_code[code]);
    if (code == mv->n) {
Diego Biurrun's avatar
Diego Biurrun committed
448
        /* escape : code literally */
Fabrice Bellard's avatar
Fabrice Bellard committed
449 450 451 452 453
        put_bits(&s->pb, 6, mx);
        put_bits(&s->pb, 6, my);
    }
}

454
void ff_msmpeg4_handle_slices(MpegEncContext *s){
455 456
    if (s->mb_x == 0) {
        if (s->slice_height && (s->mb_y % s->slice_height) == 0) {
Michael Niedermayer's avatar
Michael Niedermayer committed
457
            if(s->msmpeg4_version < 4){
458
                ff_mpeg4_clean_buffers(s);
459 460 461
            }
            s->first_slice_line = 1;
        } else {
462
            s->first_slice_line = 0;
463 464 465 466
        }
    }
}

467
void msmpeg4_encode_mb(MpegEncContext * s,
Fabrice Bellard's avatar
Fabrice Bellard committed
468 469 470 471 472
                       DCTELEM block[6][64],
                       int motion_x, int motion_y)
{
    int cbp, coded_cbp, i;
    int pred_x, pred_y;
473
    uint8_t *coded_block;
Fabrice Bellard's avatar
Fabrice Bellard committed
474

475
    ff_msmpeg4_handle_slices(s);
476

Fabrice Bellard's avatar
Fabrice Bellard committed
477
    if (!s->mb_intra) {
478 479 480 481 482 483 484 485 486
        /* compute cbp */
        cbp = 0;
        for (i = 0; i < 6; i++) {
            if (s->block_last_index[i] >= 0)
                cbp |= 1 << (5 - i);
        }
        if (s->use_skip_mb_code && (cbp | motion_x | motion_y) == 0) {
            /* skip macroblock */
            put_bits(&s->pb, 1, 1);
487
            s->last_bits++;
488
            s->misc_bits++;
489
            s->skip_count++;
490

491 492
            return;
        }
Fabrice Bellard's avatar
Fabrice Bellard committed
493
        if (s->use_skip_mb_code)
494
            put_bits(&s->pb, 1, 0);     /* mb coded */
495

Michael Niedermayer's avatar
Michael Niedermayer committed
496
        if(s->msmpeg4_version<=2){
497 498
            put_bits(&s->pb,
                     v2_mb_type[cbp&3][1],
Michael Niedermayer's avatar
Michael Niedermayer committed
499 500 501 502
                     v2_mb_type[cbp&3][0]);
            if((cbp&3) != 3) coded_cbp= cbp ^ 0x3C;
            else             coded_cbp= cbp;

503 504
            put_bits(&s->pb,
                     cbpy_tab[coded_cbp>>2][1],
Michael Niedermayer's avatar
Michael Niedermayer committed
505
                     cbpy_tab[coded_cbp>>2][0]);
506 507 508

            s->misc_bits += get_bits_diff(s);

509
            h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
Michael Niedermayer's avatar
Michael Niedermayer committed
510 511 512
            msmpeg4v2_encode_motion(s, motion_x - pred_x);
            msmpeg4v2_encode_motion(s, motion_y - pred_y);
        }else{
513 514
            put_bits(&s->pb,
                     table_mb_non_intra[cbp + 64][1],
Michael Niedermayer's avatar
Michael Niedermayer committed
515 516
                     table_mb_non_intra[cbp + 64][0]);

517 518
            s->misc_bits += get_bits_diff(s);

Michael Niedermayer's avatar
Michael Niedermayer committed
519
            /* motion vector */
520
            h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
521
            ff_msmpeg4_encode_motion(s, motion_x - pred_x,
Michael Niedermayer's avatar
Michael Niedermayer committed
522 523
                                  motion_y - pred_y);
        }
524 525 526 527

        s->mv_bits += get_bits_diff(s);

        for (i = 0; i < 6; i++) {
Denis Fortin's avatar
Denis Fortin committed
528
            ff_msmpeg4_encode_block(s, block[i], i);
529 530
        }
        s->p_tex_bits += get_bits_diff(s);
Fabrice Bellard's avatar
Fabrice Bellard committed
531
    } else {
532 533
        /* compute cbp */
        cbp = 0;
Fabrice Bellard's avatar
Fabrice Bellard committed
534
        coded_cbp = 0;
535
        for (i = 0; i < 6; i++) {
Fabrice Bellard's avatar
Fabrice Bellard committed
536 537 538 539 540
            int val, pred;
            val = (s->block_last_index[i] >= 1);
            cbp |= val << (5 - i);
            if (i < 4) {
                /* predict value for close blocks only for luma */
541
                pred = ff_msmpeg4_coded_block_pred(s, i, &coded_block);
Fabrice Bellard's avatar
Fabrice Bellard committed
542 543 544 545
                *coded_block = val;
                val = val ^ pred;
            }
            coded_cbp |= val << (5 - i);
546
        }
Fabrice Bellard's avatar
Fabrice Bellard committed
547 548 549 550 551
#if 0
        if (coded_cbp)
            printf("cbp=%x %x\n", cbp, coded_cbp);
#endif

Michael Niedermayer's avatar
Michael Niedermayer committed
552
        if(s->msmpeg4_version<=2){
553
            if (s->pict_type == FF_I_TYPE) {
554
                put_bits(&s->pb,
Michael Niedermayer's avatar
Michael Niedermayer committed
555 556 557
                         v2_intra_cbpc[cbp&3][1], v2_intra_cbpc[cbp&3][0]);
            } else {
                if (s->use_skip_mb_code)
558
                    put_bits(&s->pb, 1, 0);     /* mb coded */
559 560
                put_bits(&s->pb,
                         v2_mb_type[(cbp&3) + 4][1],
Michael Niedermayer's avatar
Michael Niedermayer committed
561 562
                         v2_mb_type[(cbp&3) + 4][0]);
            }
563
            put_bits(&s->pb, 1, 0);             /* no AC prediction yet */
564 565
            put_bits(&s->pb,
                     cbpy_tab[cbp>>2][1],
Michael Niedermayer's avatar
Michael Niedermayer committed
566 567
                     cbpy_tab[cbp>>2][0]);
        }else{
568
            if (s->pict_type == FF_I_TYPE) {
569
                put_bits(&s->pb,
anonymous's avatar
anonymous committed
570
                         ff_msmp4_mb_i_table[coded_cbp][1], ff_msmp4_mb_i_table[coded_cbp][0]);
Michael Niedermayer's avatar
Michael Niedermayer committed
571 572
            } else {
                if (s->use_skip_mb_code)
573
                    put_bits(&s->pb, 1, 0);     /* mb coded */
574 575
                put_bits(&s->pb,
                         table_mb_non_intra[cbp][1],
Michael Niedermayer's avatar
Michael Niedermayer committed
576 577
                         table_mb_non_intra[cbp][0]);
            }
578
            put_bits(&s->pb, 1, 0);             /* no AC prediction yet */
579 580 581 582
            if(s->inter_intra_pred){
                s->h263_aic_dir=0;
                put_bits(&s->pb, table_inter_intra[s->h263_aic_dir][1], table_inter_intra[s->h263_aic_dir][0]);
            }
Fabrice Bellard's avatar
Fabrice Bellard committed
583
        }
584
        s->misc_bits += get_bits_diff(s);
Fabrice Bellard's avatar
Fabrice Bellard committed
585

586
        for (i = 0; i < 6; i++) {
Denis Fortin's avatar
Denis Fortin committed
587
            ff_msmpeg4_encode_block(s, block[i], i);
588 589
        }
        s->i_tex_bits += get_bits_diff(s);
590
        s->i_count++;
Fabrice Bellard's avatar
Fabrice Bellard committed
591 592 593
    }
}

594 595
#endif //CONFIG_ENCODERS

596
static inline int msmpeg4v1_pred_dc(MpegEncContext * s, int n,
597
                                    int32_t **dc_val_ptr)
Michael Niedermayer's avatar
Michael Niedermayer committed
598 599 600 601 602 603 604 605
{
    int i;

    if (n < 4) {
        i= 0;
    } else {
        i= n-3;
    }
606

Michael Niedermayer's avatar
Michael Niedermayer committed
607
    *dc_val_ptr= &s->last_dc[i];
608
    return s->last_dc[i];
Michael Niedermayer's avatar
Michael Niedermayer committed
609 610
}

611 612 613 614 615 616 617 618 619 620
static int get_dc(uint8_t *src, int stride, int scale)
{
    int y;
    int sum=0;
    for(y=0; y<8; y++){
        int x;
        for(x=0; x<8; x++){
            sum+=src[x + y*stride];
        }
    }
621
    return FASTDIV((sum + (scale>>1)), scale);
622 623
}

Fabrice Bellard's avatar
Fabrice Bellard committed
624
/* dir = 0: left, dir = 1: top prediction */
625
static inline int msmpeg4_pred_dc(MpegEncContext * s, int n,
626
                             int16_t **dc_val_ptr, int *dir_ptr)
Fabrice Bellard's avatar
Fabrice Bellard committed
627
{
628
    int a, b, c, wrap, pred, scale;
629
    int16_t *dc_val;
Fabrice Bellard's avatar
Fabrice Bellard committed
630 631 632

    /* find prediction */
    if (n < 4) {
633
        scale = s->y_dc_scale;
Fabrice Bellard's avatar
Fabrice Bellard committed
634
    } else {
635
        scale = s->c_dc_scale;
Fabrice Bellard's avatar
Fabrice Bellard committed
636
    }
637

638 639
    wrap = s->block_wrap[n];
    dc_val= s->dc_val[0] + s->block_index[n];
Fabrice Bellard's avatar
Fabrice Bellard committed
640 641

    /* B C
642
     * A X
Fabrice Bellard's avatar
Fabrice Bellard committed
643
     */
644 645 646
    a = dc_val[ - 1];
    b = dc_val[ - 1 - wrap];
    c = dc_val[ - wrap];
647

Michael Niedermayer's avatar
Michael Niedermayer committed
648
    if(s->first_slice_line && (n&2)==0 && s->msmpeg4_version<4){
649 650
        b=c=1024;
    }
Fabrice Bellard's avatar
Fabrice Bellard committed
651 652 653 654 655

    /* XXX: the following solution consumes divisions, but it does not
       necessitate to modify mpegvideo.c. The problem comes from the
       fact they decided to store the quantized DC (which would lead
       to problems if Q could vary !) */
656
#if ARCH_X86 && !defined PIC
657
    __asm__ volatile(
658 659 660 661 662 663 664 665 666 667 668 669 670 671
        "movl %3, %%eax         \n\t"
        "shrl $1, %%eax         \n\t"
        "addl %%eax, %2         \n\t"
        "addl %%eax, %1         \n\t"
        "addl %0, %%eax         \n\t"
        "mull %4                \n\t"
        "movl %%edx, %0         \n\t"
        "movl %1, %%eax         \n\t"
        "mull %4                \n\t"
        "movl %%edx, %1         \n\t"
        "movl %2, %%eax         \n\t"
        "mull %4                \n\t"
        "movl %%edx, %2         \n\t"
        : "+b" (a), "+c" (b), "+D" (c)
672
        : "g" (scale), "S" (ff_inverse[scale])
673
        : "%eax", "%edx"
674
    );
675
#else
676
    /* #elif ARCH_ALPHA */
677
    /* Divisions are extremely costly on Alpha; optimize the most
678 679
       common case. But they are costly everywhere...
     */
680
    if (scale == 8) {
681 682 683
        a = (a + (8 >> 1)) / 8;
        b = (b + (8 >> 1)) / 8;
        c = (c + (8 >> 1)) / 8;
684
    } else {
685 686 687
        a = FASTDIV((a + (scale >> 1)), scale);
        b = FASTDIV((b + (scale >> 1)), scale);
        c = FASTDIV((c + (scale >> 1)), scale);
688
    }
689
#endif
Fabrice Bellard's avatar
Fabrice Bellard committed
690 691
    /* XXX: WARNING: they did not choose the same test as MPEG4. This
       is very important ! */
692
    if(s->msmpeg4_version>3){
693 694 695
        if(s->inter_intra_pred){
            uint8_t *dest;
            int wrap;
696

697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
            if(n==1){
                pred=a;
                *dir_ptr = 0;
            }else if(n==2){
                pred=c;
                *dir_ptr = 1;
            }else if(n==3){
                if (abs(a - b) < abs(b - c)) {
                    pred = c;
                    *dir_ptr = 1;
                } else {
                    pred = a;
                    *dir_ptr = 0;
                }
            }else{
                if(n<4){
                    wrap= s->linesize;
Michael Niedermayer's avatar
Michael Niedermayer committed
714
                    dest= s->current_picture.data[0] + (((n>>1) + 2*s->mb_y) * 8*  wrap ) + ((n&1) + 2*s->mb_x) * 8;
715
                }else{
Michael Niedermayer's avatar
Michael Niedermayer committed
716
                    wrap= s->uvlinesize;
Michael Niedermayer's avatar
Michael Niedermayer committed
717
                    dest= s->current_picture.data[n-3] + (s->mb_y * 8 * wrap) + s->mb_x * 8;
718 719 720 721 722
                }
                if(s->mb_x==0) a= (1024 + (scale>>1))/scale;
                else           a= get_dc(dest-8, wrap, scale*8);
                if(s->mb_y==0) c= (1024 + (scale>>1))/scale;
                else           c= get_dc(dest-8*wrap, wrap, scale*8);
723

724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755
                if (s->h263_aic_dir==0) {
                    pred= a;
                    *dir_ptr = 0;
                }else if (s->h263_aic_dir==1) {
                    if(n==0){
                        pred= c;
                        *dir_ptr = 1;
                    }else{
                        pred= a;
                        *dir_ptr = 0;
                    }
                }else if (s->h263_aic_dir==2) {
                    if(n==0){
                        pred= a;
                        *dir_ptr = 0;
                    }else{
                        pred= c;
                        *dir_ptr = 1;
                    }
                } else {
                    pred= c;
                    *dir_ptr = 1;
                }
            }
        }else{
            if (abs(a - b) < abs(b - c)) {
                pred = c;
                *dir_ptr = 1;
            } else {
                pred = a;
                *dir_ptr = 0;
            }
756 757 758 759 760 761 762 763 764
        }
    }else{
        if (abs(a - b) <= abs(b - c)) {
            pred = c;
            *dir_ptr = 1;
        } else {
            pred = a;
            *dir_ptr = 0;
        }
Fabrice Bellard's avatar
Fabrice Bellard committed
765 766 767
    }

    /* update predictor */
768
    *dc_val_ptr = &dc_val[0];
Fabrice Bellard's avatar
Fabrice Bellard committed
769 770 771 772 773 774 775 776
    return pred;
}

#define DC_MAX 119

static void msmpeg4_encode_dc(MpegEncContext * s, int level, int n, int *dir_ptr)
{
    int sign, code;
777 778
    int pred, extquant;
    int extrabits = 0;
Fabrice Bellard's avatar
Fabrice Bellard committed
779

Michael Niedermayer's avatar
Michael Niedermayer committed
780
    if(s->msmpeg4_version==1){
781
        int32_t *dc_val;
Michael Niedermayer's avatar
Michael Niedermayer committed
782
        pred = msmpeg4v1_pred_dc(s, n, &dc_val);
783

Michael Niedermayer's avatar
Michael Niedermayer committed
784 785 786
        /* update predictor */
        *dc_val= level;
    }else{
787
        int16_t *dc_val;
788
        pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr);
Fabrice Bellard's avatar
Fabrice Bellard committed
789

Michael Niedermayer's avatar
Michael Niedermayer committed
790 791 792 793 794 795
        /* update predictor */
        if (n < 4) {
            *dc_val = level * s->y_dc_scale;
        } else {
            *dc_val = level * s->c_dc_scale;
        }
Fabrice Bellard's avatar
Fabrice Bellard committed
796 797 798 799 800
    }

    /* do the prediction */
    level -= pred;

Michael Niedermayer's avatar
Michael Niedermayer committed
801
    if(s->msmpeg4_version<=2){
Fabrice Bellard's avatar
Fabrice Bellard committed
802
        if (n < 4) {
803
            put_bits(&s->pb,
Michael Niedermayer's avatar
Michael Niedermayer committed
804 805 806
                     v2_dc_lum_table[level+256][1],
                     v2_dc_lum_table[level+256][0]);
        }else{
807
            put_bits(&s->pb,
Michael Niedermayer's avatar
Michael Niedermayer committed
808 809
                     v2_dc_chroma_table[level+256][1],
                     v2_dc_chroma_table[level+256][0]);
Fabrice Bellard's avatar
Fabrice Bellard committed
810
        }
Michael Niedermayer's avatar
Michael Niedermayer committed
811 812 813 814 815 816 817
    }else{
        sign = 0;
        if (level < 0) {
            level = -level;
            sign = 1;
        }
        code = level;
818
        if (code > DC_MAX)
Michael Niedermayer's avatar
Michael Niedermayer committed
819
            code = DC_MAX;
820 821 822 823 824 825 826 827 828
        else if( s->msmpeg4_version>=6 ) {
            if( s->qscale == 1 ) {
                extquant = (level + 3) & 0x3;
                code  = ((level+3)>>2);
            } else if( s->qscale == 2 ) {
                extquant = (level + 1) & 0x1;
                code  = ((level+1)>>1);
            }
        }
Michael Niedermayer's avatar
Michael Niedermayer committed
829 830 831

        if (s->dc_table_index == 0) {
            if (n < 4) {
832
                put_bits(&s->pb, ff_table0_dc_lum[code][1], ff_table0_dc_lum[code][0]);
Michael Niedermayer's avatar
Michael Niedermayer committed
833
            } else {
834
                put_bits(&s->pb, ff_table0_dc_chroma[code][1], ff_table0_dc_chroma[code][0]);
Michael Niedermayer's avatar
Michael Niedermayer committed
835
            }
Fabrice Bellard's avatar
Fabrice Bellard committed
836
        } else {
Michael Niedermayer's avatar
Michael Niedermayer committed
837
            if (n < 4) {
838
                put_bits(&s->pb, ff_table1_dc_lum[code][1], ff_table1_dc_lum[code][0]);
Michael Niedermayer's avatar
Michael Niedermayer committed
839
            } else {
840
                put_bits(&s->pb, ff_table1_dc_chroma[code][1], ff_table1_dc_chroma[code][0]);
Michael Niedermayer's avatar
Michael Niedermayer committed
841 842
            }
        }
843

844 845 846
        if(s->msmpeg4_version>=6 && s->qscale<=2)
            extrabits = 3 - s->qscale;

Michael Niedermayer's avatar
Michael Niedermayer committed
847
        if (code == DC_MAX)
848 849 850
            put_bits(&s->pb, 8 + extrabits, level);
        else if(extrabits > 0)//== VC1 && s->qscale<=2
            put_bits(&s->pb, extrabits, extquant);
851

Michael Niedermayer's avatar
Michael Niedermayer committed
852 853
        if (level != 0) {
            put_bits(&s->pb, 1, sign);
Fabrice Bellard's avatar
Fabrice Bellard committed
854 855 856 857 858 859 860
        }
    }
}

/* Encoding of a block. Very similar to MPEG4 except for a different
   escape coding (same as H263) and more vlc tables.
 */
Denis Fortin's avatar
Denis Fortin committed
861
void ff_msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n)
Fabrice Bellard's avatar
Fabrice Bellard committed
862 863 864 865 866
{
    int level, run, last, i, j, last_index;
    int last_non_zero, sign, slevel;
    int code, run_diff, dc_pred_dir;
    const RLTable *rl;
867
    const uint8_t *scantable;
Fabrice Bellard's avatar
Fabrice Bellard committed
868 869 870 871 872 873 874 875 876

    if (s->mb_intra) {
        msmpeg4_encode_dc(s, block[0], n, &dc_pred_dir);
        i = 1;
        if (n < 4) {
            rl = &rl_table[s->rl_table_index];
        } else {
            rl = &rl_table[3 + s->rl_chroma_table_index];
        }
877
        run_diff = s->msmpeg4_version>=4;
878
        scantable= s->intra_scantable.permutated;
Fabrice Bellard's avatar
Fabrice Bellard committed
879 880 881
    } else {
        i = 0;
        rl = &rl_table[3 + s->rl_table_index];
Michael Niedermayer's avatar
Michael Niedermayer committed
882
        if(s->msmpeg4_version<=2)
Michael Niedermayer's avatar
Michael Niedermayer committed
883 884 885
            run_diff = 0;
        else
            run_diff = 1;
886
        scantable= s->inter_scantable.permutated;
Fabrice Bellard's avatar
Fabrice Bellard committed
887 888
    }

Michael Niedermayer's avatar
Michael Niedermayer committed
889
    /* recalculate block_last_index for M$ wmv1 */
890
    if(s->msmpeg4_version>=4 && s->msmpeg4_version<6 && s->block_last_index[n]>0){
Michael Niedermayer's avatar
Michael Niedermayer committed
891 892 893
        for(last_index=63; last_index>=0; last_index--){
            if(block[scantable[last_index]]) break;
        }
894
        s->block_last_index[n]= last_index;
Michael Niedermayer's avatar
Michael Niedermayer committed
895 896
    }else
        last_index = s->block_last_index[n];
Fabrice Bellard's avatar
Fabrice Bellard committed
897 898 899
    /* AC coefs */
    last_non_zero = i - 1;
    for (; i <= last_index; i++) {
900 901 902 903 904 905 906 907 908 909 910
        j = scantable[i];
        level = block[j];
        if (level) {
            run = i - last_non_zero - 1;
            last = (i == last_index);
            sign = 0;
            slevel = level;
            if (level < 0) {
                sign = 1;
                level = -level;
            }
911

Michael Niedermayer's avatar
Michael Niedermayer committed
912 913 914 915 916 917 918
            if(level<=MAX_LEVEL && run<=MAX_RUN){
                s->ac_stats[s->mb_intra][n>3][level][run][last]++;
            }
#if 0
else
    s->ac_stats[s->mb_intra][n>3][40][63][0]++; //esc3 like
#endif
Fabrice Bellard's avatar
Fabrice Bellard committed
919 920 921 922 923 924
            code = get_rl_index(rl, last, run, level);
            put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
            if (code == rl->n) {
                int level1, run1;

                level1 = level - rl->max_level[last][run];
925
                if (level1 < 1)
Fabrice Bellard's avatar
Fabrice Bellard committed
926 927 928 929 930 931 932 933 934 935
                    goto esc2;
                code = get_rl_index(rl, last, run, level1);
                if (code == rl->n) {
                esc2:
                    put_bits(&s->pb, 1, 0);
                    if (level > MAX_LEVEL)
                        goto esc3;
                    run1 = run - rl->max_run[last][level] - run_diff;
                    if (run1 < 0)
                        goto esc3;
936 937 938
                    code = get_rl_index(rl, last, run1+1, level);
                    if (s->msmpeg4_version == 4 && code == rl->n)
                        goto esc3;
Fabrice Bellard's avatar
Fabrice Bellard committed
939 940 941 942 943 944
                    code = get_rl_index(rl, last, run1, level);
                    if (code == rl->n) {
                    esc3:
                        /* third escape */
                        put_bits(&s->pb, 1, 0);
                        put_bits(&s->pb, 1, last);
Michael Niedermayer's avatar
Michael Niedermayer committed
945
                        if(s->msmpeg4_version>=4){
Michael Niedermayer's avatar
Michael Niedermayer committed
946 947 948
                            if(s->esc3_level_length==0){
                                s->esc3_level_length=8;
                                s->esc3_run_length= 6;
949
                                //ESCLVLSZ + ESCRUNSZ
Michael Niedermayer's avatar
Michael Niedermayer committed
950
                                if(s->qscale<8)
951
                                    put_bits(&s->pb, 6 + (s->msmpeg4_version>=6), 3);
Michael Niedermayer's avatar
Michael Niedermayer committed
952 953 954 955 956 957 958 959
                                else
                                    put_bits(&s->pb, 8, 3);
                            }
                            put_bits(&s->pb, s->esc3_run_length, run);
                            put_bits(&s->pb, 1, sign);
                            put_bits(&s->pb, s->esc3_level_length, level);
                        }else{
                            put_bits(&s->pb, 6, run);
960
                            put_sbits(&s->pb, 8, slevel);
Michael Niedermayer's avatar
Michael Niedermayer committed
961
                        }
Fabrice Bellard's avatar
Fabrice Bellard committed
962 963 964 965 966 967 968 969 970 971 972 973 974 975 976
                    } else {
                        /* second escape */
                        put_bits(&s->pb, 1, 1);
                        put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
                        put_bits(&s->pb, 1, sign);
                    }
                } else {
                    /* first escape */
                    put_bits(&s->pb, 1, 1);
                    put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
                    put_bits(&s->pb, 1, sign);
                }
            } else {
                put_bits(&s->pb, 1, sign);
            }
977 978
            last_non_zero = i;
        }
Fabrice Bellard's avatar
Fabrice Bellard committed
979 980 981 982 983 984
    }
}

/****************************************/
/* decoding stuff */

985
VLC ff_mb_non_intra_vlc[4];
986 987 988 989 990 991
static VLC v2_dc_lum_vlc;
static VLC v2_dc_chroma_vlc;
static VLC cbpy_vlc;
static VLC v2_intra_cbpc_vlc;
static VLC v2_mb_type_vlc;
static VLC v2_mv_vlc;
Michael Niedermayer's avatar
Michael Niedermayer committed
992 993
static VLC v1_intra_cbpc_vlc;
static VLC v1_inter_cbpc_vlc;
994
VLC ff_inter_intra_vlc;
995

Diego Biurrun's avatar
Diego Biurrun committed
996 997
/* This table is practically identical to the one from h263
 * except that it is inverted. */
998
static av_cold void init_h263_dc_for_msmpeg4(void)
999 1000 1001
{
        int level, uni_code, uni_len;

1002
        for(level=-256; level<256; level++){
1003 1004 1005 1006 1007 1008
            int size, v, l;
            /* find number of bits */
            size = 0;
            v = abs(level);
            while (v) {
                v >>= 1;
1009
                    size++;
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
            }

            if (level < 0)
                l= (-level) ^ ((1 << size) - 1);
            else
                l= level;

            /* luminance h263 */
            uni_code= DCtab_lum[size][0];
            uni_len = DCtab_lum[size][1];
Diego Biurrun's avatar
Diego Biurrun committed
1020
            uni_code ^= (1<<uni_len)-1; //M$ does not like compatibility
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035

            if (size > 0) {
                uni_code<<=size; uni_code|=l;
                uni_len+=size;
                if (size > 8){
                    uni_code<<=1; uni_code|=1;
                    uni_len++;
                }
            }
            v2_dc_lum_table[level+256][0]= uni_code;
            v2_dc_lum_table[level+256][1]= uni_len;

            /* chrominance h263 */
            uni_code= DCtab_chrom[size][0];
            uni_len = DCtab_chrom[size][1];
Diego Biurrun's avatar
Diego Biurrun committed
1036
            uni_code ^= (1<<uni_len)-1; //M$ does not like compatibility
1037

1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
            if (size > 0) {
                uni_code<<=size; uni_code|=l;
                uni_len+=size;
                if (size > 8){
                    uni_code<<=1; uni_code|=1;
                    uni_len++;
                }
            }
            v2_dc_chroma_table[level+256][0]= uni_code;
            v2_dc_chroma_table[level+256][1]= uni_len;

        }
}
Fabrice Bellard's avatar
Fabrice Bellard committed
1051 1052

/* init all vlc decoding tables */
1053
av_cold int ff_msmpeg4_decode_init(MpegEncContext *s)
Fabrice Bellard's avatar
Fabrice Bellard committed
1054
{
1055
    static int done = 0;
Fabrice Bellard's avatar
Fabrice Bellard committed
1056 1057 1058
    int i;
    MVTable *mv;

Michael Niedermayer's avatar
Michael Niedermayer committed
1059
    common_init(s);
Fabrice Bellard's avatar
Fabrice Bellard committed
1060

1061 1062 1063 1064
    if (!done) {
        done = 1;

        for(i=0;i<NB_RL_TABLES;i++) {
1065
            init_rl(&rl_table[i], static_rl_table_store[i]);
1066
        }
1067 1068 1069 1070 1071 1072
        INIT_VLC_RL(rl_table[0], 642);
        INIT_VLC_RL(rl_table[1], 1104);
        INIT_VLC_RL(rl_table[2], 554);
        INIT_VLC_RL(rl_table[3], 940);
        INIT_VLC_RL(rl_table[4], 962);
        INIT_VLC_RL(rl_table[5], 554);
1073

1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
        mv = &mv_tables[0];
        INIT_VLC_STATIC(&mv->vlc, MV_VLC_BITS, mv->n + 1,
                    mv->table_mv_bits, 1, 1,
                    mv->table_mv_code, 2, 2, 3714);
        mv = &mv_tables[1];
        INIT_VLC_STATIC(&mv->vlc, MV_VLC_BITS, mv->n + 1,
                    mv->table_mv_bits, 1, 1,
                    mv->table_mv_code, 2, 2, 2694);

        INIT_VLC_STATIC(&ff_msmp4_dc_luma_vlc[0], DC_VLC_BITS, 120,
1084
                 &ff_table0_dc_lum[0][1], 8, 4,
1085 1086
                 &ff_table0_dc_lum[0][0], 8, 4, 1158);
        INIT_VLC_STATIC(&ff_msmp4_dc_chroma_vlc[0], DC_VLC_BITS, 120,
1087
                 &ff_table0_dc_chroma[0][1], 8, 4,
1088 1089
                 &ff_table0_dc_chroma[0][0], 8, 4, 1118);
        INIT_VLC_STATIC(&ff_msmp4_dc_luma_vlc[1], DC_VLC_BITS, 120,
1090
                 &ff_table1_dc_lum[0][1], 8, 4,
1091 1092
                 &ff_table1_dc_lum[0][0], 8, 4, 1476);
        INIT_VLC_STATIC(&ff_msmp4_dc_chroma_vlc[1], DC_VLC_BITS, 120,
1093
                 &ff_table1_dc_chroma[0][1], 8, 4,
1094
                 &ff_table1_dc_chroma[0][0], 8, 4, 1216);
1095

1096
        INIT_VLC_STATIC(&v2_dc_lum_vlc, DC_VLC_BITS, 512,
1097
                 &v2_dc_lum_table[0][1], 8, 4,
1098 1099
                 &v2_dc_lum_table[0][0], 8, 4, 1472);
        INIT_VLC_STATIC(&v2_dc_chroma_vlc, DC_VLC_BITS, 512,
1100
                 &v2_dc_chroma_table[0][1], 8, 4,
1101
                 &v2_dc_chroma_table[0][0], 8, 4, 1506);
1102

1103
        INIT_VLC_STATIC(&cbpy_vlc, CBPY_VLC_BITS, 16,
1104
                 &cbpy_tab[0][1], 2, 1,
1105 1106
                 &cbpy_tab[0][0], 2, 1, 64);
        INIT_VLC_STATIC(&v2_intra_cbpc_vlc, V2_INTRA_CBPC_VLC_BITS, 4,
1107
                 &v2_intra_cbpc[0][1], 2, 1,
1108 1109
                 &v2_intra_cbpc[0][0], 2, 1, 8);
        INIT_VLC_STATIC(&v2_mb_type_vlc, V2_MB_TYPE_VLC_BITS, 8,
1110
                 &v2_mb_type[0][1], 2, 1,
1111 1112
                 &v2_mb_type[0][0], 2, 1, 128);
        INIT_VLC_STATIC(&v2_mv_vlc, V2_MV_VLC_BITS, 33,
1113
                 &mvtab[0][1], 2, 1,
1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
                 &mvtab[0][0], 2, 1, 538);

        INIT_VLC_STATIC(&ff_mb_non_intra_vlc[0], MB_NON_INTRA_VLC_BITS, 128,
                     &wmv2_inter_table[0][0][1], 8, 4,
                     &wmv2_inter_table[0][0][0], 8, 4, 1636);
        INIT_VLC_STATIC(&ff_mb_non_intra_vlc[1], MB_NON_INTRA_VLC_BITS, 128,
                     &wmv2_inter_table[1][0][1], 8, 4,
                     &wmv2_inter_table[1][0][0], 8, 4, 2648);
        INIT_VLC_STATIC(&ff_mb_non_intra_vlc[2], MB_NON_INTRA_VLC_BITS, 128,
                     &wmv2_inter_table[2][0][1], 8, 4,
                     &wmv2_inter_table[2][0][0], 8, 4, 1532);
        INIT_VLC_STATIC(&ff_mb_non_intra_vlc[3], MB_NON_INTRA_VLC_BITS, 128,
                     &wmv2_inter_table[3][0][1], 8, 4,
                     &wmv2_inter_table[3][0][0], 8, 4, 2488);

        INIT_VLC_STATIC(&ff_msmp4_mb_i_vlc, MB_INTRA_VLC_BITS, 64,
anonymous's avatar
anonymous committed
1130
                 &ff_msmp4_mb_i_table[0][1], 4, 2,
1131
                 &ff_msmp4_mb_i_table[0][0], 4, 2, 536);
1132

1133
        INIT_VLC_STATIC(&v1_intra_cbpc_vlc, V1_INTRA_CBPC_VLC_BITS, 8,
1134
                 intra_MCBPC_bits, 1, 1,
1135 1136
                 intra_MCBPC_code, 1, 1, 64);
        INIT_VLC_STATIC(&v1_inter_cbpc_vlc, V1_INTER_CBPC_VLC_BITS, 25,
1137
                 inter_MCBPC_bits, 1, 1,
1138
                 inter_MCBPC_code, 1, 1, 104);
1139

1140
        INIT_VLC_STATIC(&ff_inter_intra_vlc, INTER_INTRA_VLC_BITS, 4,
1141
                 &table_inter_intra[0][1], 2, 1,
1142
                 &table_inter_intra[0][0], 2, 1, 8);
1143
    }
1144

1145 1146 1147 1148 1149 1150 1151 1152 1153
    switch(s->msmpeg4_version){
    case 1:
    case 2:
        s->decode_mb= msmpeg4v12_decode_mb;
        break;
    case 3:
    case 4:
        s->decode_mb= msmpeg4v34_decode_mb;
        break;
Michael Niedermayer's avatar
Michael Niedermayer committed
1154
    case 5:
1155
        if (CONFIG_WMV2_DECODER)
Aurelien Jacobs's avatar
Aurelien Jacobs committed
1156
            s->decode_mb= ff_wmv2_decode_mb;
anonymous's avatar
anonymous committed
1157
    case 6:
1158
        //FIXME + TODO VC1 decode mb
Michael Niedermayer's avatar
Michael Niedermayer committed
1159
        break;
1160
    }
1161

Diego Biurrun's avatar
Diego Biurrun committed
1162
    s->slice_height= s->mb_height; //to avoid 1/0 if the first frame is not a keyframe
1163

Fabrice Bellard's avatar
Fabrice Bellard committed
1164 1165 1166
    return 0;
}

1167
int msmpeg4_decode_picture_header(MpegEncContext * s)
Fabrice Bellard's avatar
Fabrice Bellard committed
1168
{
Michael Niedermayer's avatar
Michael Niedermayer committed
1169
    int code;
Fabrice Bellard's avatar
Fabrice Bellard committed
1170

1171 1172 1173
#if 0
{
int i;
1174
for(i=0; i<s->gb.size_in_bits; i++)
1175
    av_log(s->avctx, AV_LOG_DEBUG, "%d", get_bits1(&s->gb));
1176
//    get_bits1(&s->gb);
1177
av_log(s->avctx, AV_LOG_DEBUG, "END\n");
1178 1179 1180
return -1;
}
#endif
Michael Niedermayer's avatar
Michael Niedermayer committed
1181 1182

    if(s->msmpeg4_version==1){
1183
        int start_code;
Michael Niedermayer's avatar
Michael Niedermayer committed
1184 1185
        start_code = (get_bits(&s->gb, 16)<<16) | get_bits(&s->gb, 16);
        if(start_code!=0x00000100){
1186
            av_log(s->avctx, AV_LOG_ERROR, "invalid startcode\n");
Michael Niedermayer's avatar
Michael Niedermayer committed
1187 1188 1189
            return -1;
        }

1190
        skip_bits(&s->gb, 5); // frame number */
Michael Niedermayer's avatar
Michael Niedermayer committed
1191 1192
    }

Fabrice Bellard's avatar
Fabrice Bellard committed
1193
    s->pict_type = get_bits(&s->gb, 2) + 1;
1194 1195
    if (s->pict_type != FF_I_TYPE &&
        s->pict_type != FF_P_TYPE){
1196
        av_log(s->avctx, AV_LOG_ERROR, "invalid picture type\n");
Fabrice Bellard's avatar
Fabrice Bellard committed
1197
        return -1;
Michael Niedermayer's avatar
Michael Niedermayer committed
1198
    }
1199 1200 1201
#if 0
{
    static int had_i=0;
1202
    if(s->pict_type == FF_I_TYPE) had_i=1;
1203 1204 1205
    if(!had_i) return -1;
}
#endif
1206
    s->chroma_qscale= s->qscale = get_bits(&s->gb, 5);
1207
    if(s->qscale==0){
1208
        av_log(s->avctx, AV_LOG_ERROR, "invalid qscale\n");
1209 1210
        return -1;
    }
Fabrice Bellard's avatar
Fabrice Bellard committed
1211

1212
    if (s->pict_type == FF_I_TYPE) {
1213
        code = get_bits(&s->gb, 5);
Michael Niedermayer's avatar
Michael Niedermayer committed
1214 1215
        if(s->msmpeg4_version==1){
            if(code==0 || code>s->mb_height){
1216
                av_log(s->avctx, AV_LOG_ERROR, "invalid slice height %d\n", code);
Michael Niedermayer's avatar
Michael Niedermayer committed
1217 1218 1219 1220 1221 1222
                return -1;
            }

            s->slice_height = code;
        }else{
            /* 0x17: one slice, 0x18: two slices, ... */
1223
            if (code < 0x17){
1224
                av_log(s->avctx, AV_LOG_ERROR, "error, slice code was %X\n", code);
Michael Niedermayer's avatar
Michael Niedermayer committed
1225
                return -1;
1226
            }
Michael Niedermayer's avatar
Michael Niedermayer committed
1227 1228 1229

            s->slice_height = s->mb_height / (code - 0x16);
        }
1230 1231

        switch(s->msmpeg4_version){
Michael Niedermayer's avatar
Michael Niedermayer committed
1232
        case 1:
1233
        case 2:
1234 1235
            s->rl_chroma_table_index = 2;
            s->rl_table_index = 2;
1236

1237
            s->dc_table_index = 0; //not used
1238 1239
            break;
        case 3:
1240 1241
            s->rl_chroma_table_index = decode012(&s->gb);
            s->rl_table_index = decode012(&s->gb);
1242

1243
            s->dc_table_index = get_bits1(&s->gb);
1244 1245
            break;
        case 4:
Michael Niedermayer's avatar
Michael Niedermayer committed
1246 1247
            msmpeg4_decode_ext_header(s, (2+5+5+17+7)/8);

1248 1249
            if(s->bit_rate > MBAC_BITRATE) s->per_mb_rl_table= get_bits1(&s->gb);
            else                           s->per_mb_rl_table= 0;
1250

Michael Niedermayer's avatar
Michael Niedermayer committed
1251 1252 1253
            if(!s->per_mb_rl_table){
                s->rl_chroma_table_index = decode012(&s->gb);
                s->rl_table_index = decode012(&s->gb);
1254 1255
            }

Michael Niedermayer's avatar
Michael Niedermayer committed
1256
            s->dc_table_index = get_bits1(&s->gb);
1257
            s->inter_intra_pred= 0;
1258
            break;
1259
        }
Fabrice Bellard's avatar
Fabrice Bellard committed
1260
        s->no_rounding = 1;
Michael Niedermayer's avatar
Michael Niedermayer committed
1261
        if(s->avctx->debug&FF_DEBUG_PICT_INFO)
1262 1263 1264 1265 1266
            av_log(s->avctx, AV_LOG_DEBUG, "qscale:%d rlc:%d rl:%d dc:%d mbrl:%d slice:%d   \n",
                s->qscale,
                s->rl_chroma_table_index,
                s->rl_table_index,
                s->dc_table_index,
1267
                s->per_mb_rl_table,
Michael Niedermayer's avatar
Michael Niedermayer committed
1268
                s->slice_height);
Fabrice Bellard's avatar
Fabrice Bellard committed
1269
    } else {
Michael Niedermayer's avatar
Michael Niedermayer committed
1270 1271 1272 1273 1274 1275 1276
        switch(s->msmpeg4_version){
        case 1:
        case 2:
            if(s->msmpeg4_version==1)
                s->use_skip_mb_code = 1;
            else
                s->use_skip_mb_code = get_bits1(&s->gb);
1277 1278 1279 1280
            s->rl_table_index = 2;
            s->rl_chroma_table_index = s->rl_table_index;
            s->dc_table_index = 0; //not used
            s->mv_table_index = 0;
Michael Niedermayer's avatar
Michael Niedermayer committed
1281 1282 1283
            break;
        case 3:
            s->use_skip_mb_code = get_bits1(&s->gb);
1284 1285
            s->rl_table_index = decode012(&s->gb);
            s->rl_chroma_table_index = s->rl_table_index;
Fabrice Bellard's avatar
Fabrice Bellard committed
1286

1287
            s->dc_table_index = get_bits1(&s->gb);
Fabrice Bellard's avatar
Fabrice Bellard committed
1288

Michael Niedermayer's avatar
Michael Niedermayer committed
1289 1290 1291 1292
            s->mv_table_index = get_bits1(&s->gb);
            break;
        case 4:
            s->use_skip_mb_code = get_bits1(&s->gb);
1293

1294 1295
            if(s->bit_rate > MBAC_BITRATE) s->per_mb_rl_table= get_bits1(&s->gb);
            else                           s->per_mb_rl_table= 0;
1296

Michael Niedermayer's avatar
Michael Niedermayer committed
1297 1298 1299 1300 1301 1302 1303
            if(!s->per_mb_rl_table){
                s->rl_table_index = decode012(&s->gb);
                s->rl_chroma_table_index = s->rl_table_index;
            }

            s->dc_table_index = get_bits1(&s->gb);

1304
            s->mv_table_index = get_bits1(&s->gb);
1305
            s->inter_intra_pred= (s->width*s->height < 320*240 && s->bit_rate<=II_BITRATE);
Michael Niedermayer's avatar
Michael Niedermayer committed
1306
            break;
1307
        }
1308

Michael Niedermayer's avatar
Michael Niedermayer committed
1309
        if(s->avctx->debug&FF_DEBUG_PICT_INFO)
1310 1311 1312 1313 1314 1315
            av_log(s->avctx, AV_LOG_DEBUG, "skip:%d rl:%d rlc:%d dc:%d mv:%d mbrl:%d qp:%d   \n",
                s->use_skip_mb_code,
                s->rl_table_index,
                s->rl_chroma_table_index,
                s->dc_table_index,
                s->mv_table_index,
1316
                s->per_mb_rl_table,
Michael Niedermayer's avatar
Michael Niedermayer committed
1317 1318
                s->qscale);

1319 1320 1321 1322 1323
        if(s->flipflop_rounding){
            s->no_rounding ^= 1;
        }else{
            s->no_rounding = 0;
        }
Fabrice Bellard's avatar
Fabrice Bellard committed
1324
    }
Michael Niedermayer's avatar
Michael Niedermayer committed
1325
//printf("%d %d %d %d %d\n", s->pict_type, s->bit_rate, s->inter_intra_pred, s->width, s->height);
Michael Niedermayer's avatar
Michael Niedermayer committed
1326 1327 1328

    s->esc3_level_length= 0;
    s->esc3_run_length= 0;
1329

Fabrice Bellard's avatar
Fabrice Bellard committed
1330
#ifdef DEBUG
1331
    av_log(s->avctx, AV_LOG_DEBUG, "*****frame %d:\n", frame_count++);
Fabrice Bellard's avatar
Fabrice Bellard committed
1332 1333 1334 1335
#endif
    return 0;
}

1336 1337
int msmpeg4_decode_ext_header(MpegEncContext * s, int buf_size)
{
Michael Niedermayer's avatar
Michael Niedermayer committed
1338 1339
    int left= buf_size*8 - get_bits_count(&s->gb);
    int length= s->msmpeg4_version>=3 ? 17 : 16;
1340
    /* the alt_bitstream reader could read over the end so we need to check it */
Michael Niedermayer's avatar
Michael Niedermayer committed
1341
    if(left>=length && left<length+8)
1342
    {
1343 1344 1345
        int fps;

        fps= get_bits(&s->gb, 5);
1346
        s->bit_rate= get_bits(&s->gb, 11)*1024;
Michael Niedermayer's avatar
Michael Niedermayer committed
1347 1348 1349 1350
        if(s->msmpeg4_version>=3)
            s->flipflop_rounding= get_bits1(&s->gb);
        else
            s->flipflop_rounding= 0;
1351

1352
//        printf("fps:%2d bps:%2d roundingType:%1d\n", fps, s->bit_rate/1024, s->flipflop_rounding);
1353
    }
Michael Niedermayer's avatar
Michael Niedermayer committed
1354
    else if(left<length+8)
1355
    {
1356
        s->flipflop_rounding= 0;
1357
        if(s->msmpeg4_version != 2)
1358
            av_log(s->avctx, AV_LOG_ERROR, "ext header missing, %d left\n", left);
Michael Niedermayer's avatar
Michael Niedermayer committed
1359 1360 1361
    }
    else
    {
1362
        av_log(s->avctx, AV_LOG_ERROR, "I frame too long, ignoring ext header\n");
1363
    }
1364

1365 1366 1367
    return 0;
}

1368
static inline void msmpeg4_memsetw(short *tab, int val, int n)
Fabrice Bellard's avatar
Fabrice Bellard committed
1369 1370 1371 1372 1373 1374
{
    int i;
    for(i=0;i<n;i++)
        tab[i] = val;
}

1375
#if CONFIG_ENCODERS
Michael Niedermayer's avatar
Michael Niedermayer committed
1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401
static void msmpeg4v2_encode_motion(MpegEncContext * s, int val)
{
    int range, bit_size, sign, code, bits;

    if (val == 0) {
        /* zero vector */
        code = 0;
        put_bits(&s->pb, mvtab[code][1], mvtab[code][0]);
    } else {
        bit_size = s->f_code - 1;
        range = 1 << bit_size;
        if (val <= -64)
            val += 64;
        else if (val >= 64)
            val -= 64;

        if (val >= 0) {
            sign = 0;
        } else {
            val = -val;
            sign = 1;
        }
        val--;
        code = (val >> bit_size) + 1;
        bits = val & (range - 1);

1402
        put_bits(&s->pb, mvtab[code][1] + 1, (mvtab[code][0] << 1) | sign);
Michael Niedermayer's avatar
Michael Niedermayer committed
1403 1404 1405 1406 1407
        if (bit_size > 0) {
            put_bits(&s->pb, bit_size, bits);
        }
    }
}
1408
#endif
Michael Niedermayer's avatar
Michael Niedermayer committed
1409

Diego Biurrun's avatar
Diego Biurrun committed
1410
/* This is identical to h263 except that its range is multiplied by 2. */
1411 1412 1413 1414
static int msmpeg4v2_decode_motion(MpegEncContext * s, int pred, int f_code)
{
    int code, val, sign, shift;

1415
    code = get_vlc2(&s->gb, v2_mv_vlc.table, V2_MV_VLC_BITS, 2);
Michael Niedermayer's avatar
Michael Niedermayer committed
1416
//     printf("MV code %d at %d %d pred: %d\n", code, s->mb_x,s->mb_y, pred);
1417 1418 1419 1420 1421 1422 1423
    if (code < 0)
        return 0xffff;

    if (code == 0)
        return pred;
    sign = get_bits1(&s->gb);
    shift = f_code - 1;
1424 1425 1426
    val = code;
    if (shift) {
        val = (val - 1) << shift;
1427
        val |= get_bits(&s->gb, shift);
1428 1429
        val++;
    }
1430 1431 1432
    if (sign)
        val = -val;

Michael Niedermayer's avatar
Michael Niedermayer committed
1433
    val += pred;
1434 1435 1436 1437 1438 1439 1440 1441
    if (val <= -64)
        val += 64;
    else if (val >= 64)
        val -= 64;

    return val;
}

1442
static int msmpeg4v12_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
1443 1444
{
    int cbp, code, i;
1445

1446
    if (s->pict_type == FF_P_TYPE) {
1447 1448 1449 1450 1451 1452 1453 1454 1455 1456
        if (s->use_skip_mb_code) {
            if (get_bits1(&s->gb)) {
                /* skip mb */
                s->mb_intra = 0;
                for(i=0;i<6;i++)
                    s->block_last_index[i] = -1;
                s->mv_dir = MV_DIR_FORWARD;
                s->mv_type = MV_TYPE_16X16;
                s->mv[0][0][0] = 0;
                s->mv[0][0][1] = 0;
1457
                s->mb_skipped = 1;
1458 1459 1460 1461
                return 0;
            }
        }

Michael Niedermayer's avatar
Michael Niedermayer committed
1462
        if(s->msmpeg4_version==2)
1463
            code = get_vlc2(&s->gb, v2_mb_type_vlc.table, V2_MB_TYPE_VLC_BITS, 1);
Michael Niedermayer's avatar
Michael Niedermayer committed
1464
        else
1465
            code = get_vlc2(&s->gb, v1_inter_cbpc_vlc.table, V1_INTER_CBPC_VLC_BITS, 3);
Michael Niedermayer's avatar
Michael Niedermayer committed
1466
        if(code<0 || code>7){
1467
            av_log(s->avctx, AV_LOG_ERROR, "cbpc %d invalid at %d %d\n", code, s->mb_x, s->mb_y);
Michael Niedermayer's avatar
Michael Niedermayer committed
1468 1469 1470
            return -1;
        }

1471
        s->mb_intra = code >>2;
1472

1473 1474 1475
        cbp = code & 0x3;
    } else {
        s->mb_intra = 1;
Michael Niedermayer's avatar
Michael Niedermayer committed
1476
        if(s->msmpeg4_version==2)
1477
            cbp= get_vlc2(&s->gb, v2_intra_cbpc_vlc.table, V2_INTRA_CBPC_VLC_BITS, 1);
Michael Niedermayer's avatar
Michael Niedermayer committed
1478
        else
1479
            cbp= get_vlc2(&s->gb, v1_intra_cbpc_vlc.table, V1_INTRA_CBPC_VLC_BITS, 1);
Michael Niedermayer's avatar
Michael Niedermayer committed
1480
        if(cbp<0 || cbp>3){
1481
            av_log(s->avctx, AV_LOG_ERROR, "cbpc %d invalid at %d %d\n", cbp, s->mb_x, s->mb_y);
Michael Niedermayer's avatar
Michael Niedermayer committed
1482 1483
            return -1;
        }
1484 1485 1486
    }

    if (!s->mb_intra) {
Michael Niedermayer's avatar
Michael Niedermayer committed
1487
        int mx, my, cbpy;
1488

1489
        cbpy= get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1);
Michael Niedermayer's avatar
Michael Niedermayer committed
1490
        if(cbpy<0){
1491
            av_log(s->avctx, AV_LOG_ERROR, "cbpy %d invalid at %d %d\n", cbp, s->mb_x, s->mb_y);
Michael Niedermayer's avatar
Michael Niedermayer committed
1492 1493
            return -1;
        }
1494

Michael Niedermayer's avatar
Michael Niedermayer committed
1495 1496
        cbp|= cbpy<<2;
        if(s->msmpeg4_version==1 || (cbp&3) != 3) cbp^= 0x3C;
1497

1498
        h263_pred_motion(s, 0, 0, &mx, &my);
1499 1500
        mx= msmpeg4v2_decode_motion(s, mx, 1);
        my= msmpeg4v2_decode_motion(s, my, 1);
1501

1502 1503 1504 1505 1506
        s->mv_dir = MV_DIR_FORWARD;
        s->mv_type = MV_TYPE_16X16;
        s->mv[0][0][0] = mx;
        s->mv[0][0][1] = my;
    } else {
Michael Niedermayer's avatar
Michael Niedermayer committed
1507 1508
        if(s->msmpeg4_version==2){
            s->ac_pred = get_bits1(&s->gb);
1509
            cbp|= get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1)<<2; //FIXME check errors
Michael Niedermayer's avatar
Michael Niedermayer committed
1510 1511
        } else{
            s->ac_pred = 0;
1512
            cbp|= get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1)<<2; //FIXME check errors
1513
            if(s->pict_type==FF_P_TYPE) cbp^=0x3C;
Michael Niedermayer's avatar
Michael Niedermayer committed
1514
        }
1515 1516
    }

1517
    s->dsp.clear_blocks(s->block[0]);
1518
    for (i = 0; i < 6; i++) {
1519
        if (ff_msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, NULL) < 0)
1520
        {
1521
             av_log(s->avctx, AV_LOG_ERROR, "\nerror while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
1522
             return -1;
1523
        }
1524 1525 1526 1527
    }
    return 0;
}

1528
static int msmpeg4v34_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
Fabrice Bellard's avatar
Fabrice Bellard committed
1529 1530
{
    int cbp, code, i;
1531
    uint8_t *coded_val;
1532
    uint32_t * const mb_type_ptr= &s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ];
Fabrice Bellard's avatar
Fabrice Bellard committed
1533

1534
    if (s->pict_type == FF_P_TYPE) {
Fabrice Bellard's avatar
Fabrice Bellard committed
1535
        if (s->use_skip_mb_code) {
1536
            if (get_bits1(&s->gb)) {
Fabrice Bellard's avatar
Fabrice Bellard committed
1537 1538 1539 1540 1541 1542 1543 1544
                /* skip mb */
                s->mb_intra = 0;
                for(i=0;i<6;i++)
                    s->block_last_index[i] = -1;
                s->mv_dir = MV_DIR_FORWARD;
                s->mv_type = MV_TYPE_16X16;
                s->mv[0][0][0] = 0;
                s->mv[0][0][1] = 0;
1545
                s->mb_skipped = 1;
1546 1547
                *mb_type_ptr = MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;

Fabrice Bellard's avatar
Fabrice Bellard committed
1548 1549 1550
                return 0;
            }
        }
1551

1552
        code = get_vlc2(&s->gb, ff_mb_non_intra_vlc[DEFAULT_INTER_INDEX].table, MB_NON_INTRA_VLC_BITS, 3);
Fabrice Bellard's avatar
Fabrice Bellard committed
1553 1554
        if (code < 0)
            return -1;
1555 1556
        //s->mb_intra = (code & 0x40) ? 0 : 1;
        s->mb_intra = (~code & 0x40) >> 6;
1557

Fabrice Bellard's avatar
Fabrice Bellard committed
1558 1559 1560
        cbp = code & 0x3f;
    } else {
        s->mb_intra = 1;
anonymous's avatar
anonymous committed
1561
        code = get_vlc2(&s->gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
Fabrice Bellard's avatar
Fabrice Bellard committed
1562 1563 1564 1565 1566
        if (code < 0)
            return -1;
        /* predict coded block pattern */
        cbp = 0;
        for(i=0;i<6;i++) {
Zdenek Kabelac's avatar
Zdenek Kabelac committed
1567
            int val = ((code >> (5 - i)) & 1);
Fabrice Bellard's avatar
Fabrice Bellard committed
1568
            if (i < 4) {
1569
                int pred = ff_msmpeg4_coded_block_pred(s, i, &coded_val);
Fabrice Bellard's avatar
Fabrice Bellard committed
1570 1571 1572 1573 1574 1575 1576 1577 1578
                val = val ^ pred;
                *coded_val = val;
            }
            cbp |= val << (5 - i);
        }
    }

    if (!s->mb_intra) {
        int mx, my;
Michael Niedermayer's avatar
Michael Niedermayer committed
1579 1580 1581 1582 1583
//printf("P at %d %d\n", s->mb_x, s->mb_y);
        if(s->per_mb_rl_table && cbp){
            s->rl_table_index = decode012(&s->gb);
            s->rl_chroma_table_index = s->rl_table_index;
        }
1584
        h263_pred_motion(s, 0, 0, &mx, &my);
1585
        if (ff_msmpeg4_decode_motion(s, &mx, &my) < 0)
Fabrice Bellard's avatar
Fabrice Bellard committed
1586 1587 1588 1589 1590
            return -1;
        s->mv_dir = MV_DIR_FORWARD;
        s->mv_type = MV_TYPE_16X16;
        s->mv[0][0][0] = mx;
        s->mv[0][0][1] = my;
1591
        *mb_type_ptr = MB_TYPE_L0 | MB_TYPE_16x16;
Fabrice Bellard's avatar
Fabrice Bellard committed
1592
    } else {
Michael Niedermayer's avatar
Michael Niedermayer committed
1593
//printf("I at %d %d %d %06X\n", s->mb_x, s->mb_y, ((cbp&3)? 1 : 0) +((cbp&0x3C)? 2 : 0), show_bits(&s->gb, 24));
1594
        s->ac_pred = get_bits1(&s->gb);
1595
        *mb_type_ptr = MB_TYPE_INTRA;
1596
        if(s->inter_intra_pred){
1597
            s->h263_aic_dir= get_vlc2(&s->gb, ff_inter_intra_vlc.table, INTER_INTRA_VLC_BITS, 1);
1598 1599
//            printf("%d%d %d %d/", s->ac_pred, s->h263_aic_dir, s->mb_x, s->mb_y);
        }
Michael Niedermayer's avatar
Michael Niedermayer committed
1600 1601 1602 1603
        if(s->per_mb_rl_table && cbp){
            s->rl_table_index = decode012(&s->gb);
            s->rl_chroma_table_index = s->rl_table_index;
        }
Fabrice Bellard's avatar
Fabrice Bellard committed
1604 1605
    }

1606
    s->dsp.clear_blocks(s->block[0]);
Fabrice Bellard's avatar
Fabrice Bellard committed
1607
    for (i = 0; i < 6; i++) {
1608
        if (ff_msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, NULL) < 0)
1609 1610 1611 1612
        {
            av_log(s->avctx, AV_LOG_ERROR, "\nerror while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
            return -1;
        }
Fabrice Bellard's avatar
Fabrice Bellard committed
1613
    }
1614

Fabrice Bellard's avatar
Fabrice Bellard committed
1615 1616
    return 0;
}
1617
//#define ERROR_DETAILS
1618
int ff_msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
Michael Niedermayer's avatar
Michael Niedermayer committed
1619
                              int n, int coded, const uint8_t *scan_table)
Fabrice Bellard's avatar
Fabrice Bellard committed
1620
{
1621
    int level, i, last, run, run_diff;
1622
    int av_uninit(dc_pred_dir);
Fabrice Bellard's avatar
Fabrice Bellard committed
1623
    RLTable *rl;
1624
    RL_VLC_ELEM *rl_vlc;
1625
    int qmul, qadd;
Fabrice Bellard's avatar
Fabrice Bellard committed
1626 1627

    if (s->mb_intra) {
1628 1629 1630
        qmul=1;
        qadd=0;

1631
        /* DC coef */
Fabrice Bellard's avatar
Fabrice Bellard committed
1632
        level = msmpeg4_decode_dc(s, n, &dc_pred_dir);
1633

Michael Niedermayer's avatar
Michael Niedermayer committed
1634
        if (level < 0){
1635
            av_log(s->avctx, AV_LOG_ERROR, "dc overflow- block: %d qscale: %d//\n", n, s->qscale);
1636 1637
            if(s->inter_intra_pred) level=0;
            else                    return -1;
Michael Niedermayer's avatar
Michael Niedermayer committed
1638
        }
Fabrice Bellard's avatar
Fabrice Bellard committed
1639 1640
        if (n < 4) {
            rl = &rl_table[s->rl_table_index];
Michael Niedermayer's avatar
Michael Niedermayer committed
1641
            if(level > 256*s->y_dc_scale){
1642
                av_log(s->avctx, AV_LOG_ERROR, "dc overflow+ L qscale: %d//\n", s->qscale);
1643
                if(!s->inter_intra_pred) return -1;
Michael Niedermayer's avatar
Michael Niedermayer committed
1644
            }
Fabrice Bellard's avatar
Fabrice Bellard committed
1645 1646
        } else {
            rl = &rl_table[3 + s->rl_chroma_table_index];
Michael Niedermayer's avatar
Michael Niedermayer committed
1647
            if(level > 256*s->c_dc_scale){
1648
                av_log(s->avctx, AV_LOG_ERROR, "dc overflow+ C qscale: %d//\n", s->qscale);
1649
                if(!s->inter_intra_pred) return -1;
Michael Niedermayer's avatar
Michael Niedermayer committed
1650
            }
Fabrice Bellard's avatar
Fabrice Bellard committed
1651
        }
Michael Niedermayer's avatar
Michael Niedermayer committed
1652
        block[0] = level;
1653

1654
        run_diff = s->msmpeg4_version >= 4;
1655
        i = 0;
Fabrice Bellard's avatar
Fabrice Bellard committed
1656 1657 1658 1659
        if (!coded) {
            goto not_coded;
        }
        if (s->ac_pred) {
1660
            if (dc_pred_dir == 0)
1661
                scan_table = s->intra_v_scantable.permutated; /* left */
Fabrice Bellard's avatar
Fabrice Bellard committed
1662
            else
1663
                scan_table = s->intra_h_scantable.permutated; /* top */
Fabrice Bellard's avatar
Fabrice Bellard committed
1664
        } else {
1665
            scan_table = s->intra_scantable.permutated;
Fabrice Bellard's avatar
Fabrice Bellard committed
1666
        }
1667
        rl_vlc= rl->rl_vlc[0];
Fabrice Bellard's avatar
Fabrice Bellard committed
1668
    } else {
1669 1670
        qmul = s->qscale << 1;
        qadd = (s->qscale - 1) | 1;
1671
        i = -1;
Fabrice Bellard's avatar
Fabrice Bellard committed
1672
        rl = &rl_table[3 + s->rl_table_index];
1673 1674 1675 1676 1677 1678

        if(s->msmpeg4_version==2)
            run_diff = 0;
        else
            run_diff = 1;

Fabrice Bellard's avatar
Fabrice Bellard committed
1679
        if (!coded) {
1680
            s->block_last_index[n] = i;
Fabrice Bellard's avatar
Fabrice Bellard committed
1681 1682
            return 0;
        }
Michael Niedermayer's avatar
Michael Niedermayer committed
1683 1684
        if(!scan_table)
            scan_table = s->inter_scantable.permutated;
1685
        rl_vlc= rl->rl_vlc[s->qscale];
Fabrice Bellard's avatar
Fabrice Bellard committed
1686
    }
1687 1688
  {
    OPEN_READER(re, &s->gb);
Fabrice Bellard's avatar
Fabrice Bellard committed
1689
    for(;;) {
1690
        UPDATE_CACHE(re, &s->gb);
1691
        GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 0);
1692 1693 1694
        if (level==0) {
            int cache;
            cache= GET_CACHE(re, &s->gb);
Fabrice Bellard's avatar
Fabrice Bellard committed
1695
            /* escape */
1696 1697
            if (s->msmpeg4_version==1 || (cache&0x80000000)==0) {
                if (s->msmpeg4_version==1 || (cache&0x40000000)==0) {
Fabrice Bellard's avatar
Fabrice Bellard committed
1698
                    /* third escape */
1699 1700
                    if(s->msmpeg4_version!=1) LAST_SKIP_BITS(re, &s->gb, 2);
                    UPDATE_CACHE(re, &s->gb);
Michael Niedermayer's avatar
Michael Niedermayer committed
1701
                    if(s->msmpeg4_version<=3){
1702 1703 1704 1705
                        last=  SHOW_UBITS(re, &s->gb, 1); SKIP_CACHE(re, &s->gb, 1);
                        run=   SHOW_UBITS(re, &s->gb, 6); SKIP_CACHE(re, &s->gb, 6);
                        level= SHOW_SBITS(re, &s->gb, 8); LAST_SKIP_CACHE(re, &s->gb, 8);
                        SKIP_COUNTER(re, &s->gb, 1+6+8);
1706
                    }else{
Michael Niedermayer's avatar
Michael Niedermayer committed
1707
                        int sign;
1708
                        last=  SHOW_UBITS(re, &s->gb, 1); SKIP_BITS(re, &s->gb, 1);
Michael Niedermayer's avatar
Michael Niedermayer committed
1709 1710 1711 1712
                        if(!s->esc3_level_length){
                            int ll;
                            //printf("ESC-3 %X at %d %d\n", show_bits(&s->gb, 24), s->mb_x, s->mb_y);
                            if(s->qscale<8){
1713
                                ll= SHOW_UBITS(re, &s->gb, 3); SKIP_BITS(re, &s->gb, 3);
Michael Niedermayer's avatar
Michael Niedermayer committed
1714
                                if(ll==0){
1715
                                    if(SHOW_UBITS(re, &s->gb, 1)) av_log(s->avctx, AV_LOG_ERROR, "cool a new vlc code ,contact the ffmpeg developers and upload the file\n");
1716
                                    SKIP_BITS(re, &s->gb, 1);
Michael Niedermayer's avatar
Michael Niedermayer committed
1717 1718 1719 1720
                                    ll=8;
                                }
                            }else{
                                ll=2;
1721 1722 1723 1724
                                while(ll<8 && SHOW_UBITS(re, &s->gb, 1)==0){
                                    ll++;
                                    SKIP_BITS(re, &s->gb, 1);
                                }
1725
                                if(ll<8) SKIP_BITS(re, &s->gb, 1);
Michael Niedermayer's avatar
Michael Niedermayer committed
1726 1727 1728
                            }

                            s->esc3_level_length= ll;
1729
                            s->esc3_run_length= SHOW_UBITS(re, &s->gb, 2) + 3; SKIP_BITS(re, &s->gb, 2);
Michael Niedermayer's avatar
Michael Niedermayer committed
1730
//printf("level length:%d, run length: %d\n", ll, s->esc3_run_length);
1731
                            UPDATE_CACHE(re, &s->gb);
Michael Niedermayer's avatar
Michael Niedermayer committed
1732
                        }
1733
                        run=   SHOW_UBITS(re, &s->gb, s->esc3_run_length);
1734
                        SKIP_BITS(re, &s->gb, s->esc3_run_length);
1735 1736

                        sign=  SHOW_UBITS(re, &s->gb, 1);
1737
                        SKIP_BITS(re, &s->gb, 1);
1738 1739

                        level= SHOW_UBITS(re, &s->gb, s->esc3_level_length);
1740
                        SKIP_BITS(re, &s->gb, s->esc3_level_length);
Michael Niedermayer's avatar
Michael Niedermayer committed
1741 1742 1743
                        if(sign) level= -level;
                    }
//printf("level: %d, run: %d at %d %d\n", level, run, s->mb_x, s->mb_y);
Michael Niedermayer's avatar
Michael Niedermayer committed
1744 1745
#if 0 // waste of time / this will detect very few errors
                    {
1746
                        const int abs_level= FFABS(level);
Michael Niedermayer's avatar
Michael Niedermayer committed
1747 1748 1749
                        const int run1= run - rl->max_run[last][abs_level] - run_diff;
                        if(abs_level<=MAX_LEVEL && run<=MAX_RUN){
                            if(abs_level <= rl->max_level[last][run]){
1750
                                av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, vlc encoding possible\n");
Michael Niedermayer's avatar
Michael Niedermayer committed
1751 1752 1753
                                return DECODING_AC_LOST;
                            }
                            if(abs_level <= rl->max_level[last][run]*2){
1754
                                av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 1 encoding possible\n");
Michael Niedermayer's avatar
Michael Niedermayer committed
1755 1756
                                return DECODING_AC_LOST;
                            }
Michael Niedermayer's avatar
Michael Niedermayer committed
1757
                            if(run1>=0 && abs_level <= rl->max_level[last][run1]){
1758
                                av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 2 encoding possible\n");
Michael Niedermayer's avatar
Michael Niedermayer committed
1759 1760 1761 1762 1763
                                return DECODING_AC_LOST;
                            }
                        }
                    }
#endif
1764 1765
                    //level = level * qmul + (level>0) * qadd - (level<=0) * qadd ;
                    if (level>0) level= level * qmul + qadd;
Michael Niedermayer's avatar
Michael Niedermayer committed
1766 1767 1768
                    else         level= level * qmul - qadd;
#if 0 // waste of time too :(
                    if(level>2048 || level<-2048){
1769
                        av_log(s->avctx, AV_LOG_ERROR, "|level| overflow in 3. esc\n");
Michael Niedermayer's avatar
Michael Niedermayer committed
1770 1771 1772
                        return DECODING_AC_LOST;
                    }
#endif
1773 1774
                    i+= run + 1;
                    if(last) i+=192;
1775 1776
#ifdef ERROR_DETAILS
                if(run==66)
1777
                    av_log(s->avctx, AV_LOG_ERROR, "illegal vlc code in ESC3 level=%d\n", level);
1778
                else if((i>62 && i<192) || i>192+63)
1779
                    av_log(s->avctx, AV_LOG_ERROR, "run overflow in ESC3 i=%d run=%d level=%d\n", i, run, level);
1780
#endif
Fabrice Bellard's avatar
Fabrice Bellard committed
1781 1782
                } else {
                    /* second escape */
1783 1784 1785 1786 1787 1788
#if MIN_CACHE_BITS < 23
                    LAST_SKIP_BITS(re, &s->gb, 2);
                    UPDATE_CACHE(re, &s->gb);
#else
                    SKIP_BITS(re, &s->gb, 2);
#endif
1789
                    GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
1790 1791 1792
                    i+= run + rl->max_run[run>>7][level/qmul] + run_diff; //FIXME opt indexing
                    level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
                    LAST_SKIP_BITS(re, &s->gb, 1);
1793 1794
#ifdef ERROR_DETAILS
                if(run==66)
1795
                    av_log(s->avctx, AV_LOG_ERROR, "illegal vlc code in ESC2 level=%d\n", level);
1796
                else if((i>62 && i<192) || i>192+63)
1797
                    av_log(s->avctx, AV_LOG_ERROR, "run overflow in ESC2 i=%d run=%d level=%d\n", i, run, level);
1798
#endif
Fabrice Bellard's avatar
Fabrice Bellard committed
1799 1800 1801
                }
            } else {
                /* first escape */
1802 1803 1804 1805 1806 1807
#if MIN_CACHE_BITS < 22
                LAST_SKIP_BITS(re, &s->gb, 1);
                UPDATE_CACHE(re, &s->gb);
#else
                SKIP_BITS(re, &s->gb, 1);
#endif
1808
                GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
1809 1810 1811 1812
                i+= run;
                level = level + rl->max_level[run>>7][(run-1)&63] * qmul;//FIXME opt indexing
                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
                LAST_SKIP_BITS(re, &s->gb, 1);
1813 1814
#ifdef ERROR_DETAILS
                if(run==66)
1815
                    av_log(s->avctx, AV_LOG_ERROR, "illegal vlc code in ESC1 level=%d\n", level);
1816
                else if((i>62 && i<192) || i>192+63)
1817
                    av_log(s->avctx, AV_LOG_ERROR, "run overflow in ESC1 i=%d run=%d level=%d\n", i, run, level);
1818
#endif
Fabrice Bellard's avatar
Fabrice Bellard committed
1819 1820
            }
        } else {
1821 1822 1823
            i+= run;
            level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
            LAST_SKIP_BITS(re, &s->gb, 1);
1824 1825
#ifdef ERROR_DETAILS
                if(run==66)
1826
                    av_log(s->avctx, AV_LOG_ERROR, "illegal vlc code level=%d\n", level);
1827
                else if((i>62 && i<192) || i>192+63)
1828
                    av_log(s->avctx, AV_LOG_ERROR, "run overflow i=%d run=%d level=%d\n", i, run, level);
1829
#endif
Michael Niedermayer's avatar
Michael Niedermayer committed
1830
        }
1831 1832 1833
        if (i > 62){
            i-= 192;
            if(i&(~63)){
1834
                const int left= s->gb.size_in_bits - get_bits_count(&s->gb);
1835
                if(((i+192 == 64 && level/qmul==-1) || s->error_recognition<=1) && left>=0){
1836
                    av_log(s->avctx, AV_LOG_ERROR, "ignoring overflow at %d %d\n", s->mb_x, s->mb_y);
1837 1838
                    break;
                }else{
1839
                    av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
1840 1841
                    return -1;
                }
1842
            }
Michael Niedermayer's avatar
Michael Niedermayer committed
1843

1844
            block[scan_table[i]] = level;
Fabrice Bellard's avatar
Fabrice Bellard committed
1845
            break;
1846 1847 1848
        }

        block[scan_table[i]] = level;
Fabrice Bellard's avatar
Fabrice Bellard committed
1849
    }
1850 1851
    CLOSE_READER(re, &s->gb);
  }
Fabrice Bellard's avatar
Fabrice Bellard committed
1852 1853 1854 1855
 not_coded:
    if (s->mb_intra) {
        mpeg4_pred_ac(s, block, n, dc_pred_dir);
        if (s->ac_pred) {
1856
            i = 63; /* XXX: not optimal */
Fabrice Bellard's avatar
Fabrice Bellard committed
1857 1858
        }
    }
Michael Niedermayer's avatar
Michael Niedermayer committed
1859
    if(s->msmpeg4_version>=4 && i>0) i=63; //FIXME/XXX optimize
1860
    s->block_last_index[n] = i;
1861

Fabrice Bellard's avatar
Fabrice Bellard committed
1862 1863 1864 1865 1866 1867 1868
    return 0;
}

static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr)
{
    int level, pred;

Michael Niedermayer's avatar
Michael Niedermayer committed
1869
    if(s->msmpeg4_version<=2){
1870
        if (n < 4) {
1871
            level = get_vlc2(&s->gb, v2_dc_lum_vlc.table, DC_VLC_BITS, 3);
1872
        } else {
1873
            level = get_vlc2(&s->gb, v2_dc_chroma_vlc.table, DC_VLC_BITS, 3);
1874
        }
1875
        if (level < 0)
1876 1877 1878 1879
            return -1;
        level-=256;
    }else{  //FIXME optimize use unified tables & index
        if (n < 4) {
anonymous's avatar
anonymous committed
1880
            level = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
1881
        } else {
anonymous's avatar
anonymous committed
1882
            level = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
1883
        }
Michael Niedermayer's avatar
Michael Niedermayer committed
1884
        if (level < 0){
1885
            av_log(s->avctx, AV_LOG_ERROR, "illegal dc vlc\n");
1886
            return -1;
Michael Niedermayer's avatar
Michael Niedermayer committed
1887
        }
1888 1889 1890 1891 1892 1893 1894 1895 1896

        if (level == DC_MAX) {
            level = get_bits(&s->gb, 8);
            if (get_bits1(&s->gb))
                level = -level;
        } else if (level != 0) {
            if (get_bits1(&s->gb))
                level = -level;
        }
Fabrice Bellard's avatar
Fabrice Bellard committed
1897 1898
    }

Michael Niedermayer's avatar
Michael Niedermayer committed
1899
    if(s->msmpeg4_version==1){
1900
        int32_t *dc_val;
Michael Niedermayer's avatar
Michael Niedermayer committed
1901 1902
        pred = msmpeg4v1_pred_dc(s, n, &dc_val);
        level += pred;
1903

Michael Niedermayer's avatar
Michael Niedermayer committed
1904 1905 1906
        /* update predictor */
        *dc_val= level;
    }else{
1907
        int16_t *dc_val;
1908
        pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr);
Michael Niedermayer's avatar
Michael Niedermayer committed
1909
        level += pred;
Fabrice Bellard's avatar
Fabrice Bellard committed
1910

Michael Niedermayer's avatar
Michael Niedermayer committed
1911 1912 1913 1914 1915 1916
        /* update predictor */
        if (n < 4) {
            *dc_val = level * s->y_dc_scale;
        } else {
            *dc_val = level * s->c_dc_scale;
        }
Fabrice Bellard's avatar
Fabrice Bellard committed
1917 1918 1919 1920 1921
    }

    return level;
}

1922
int ff_msmpeg4_decode_motion(MpegEncContext * s,
Fabrice Bellard's avatar
Fabrice Bellard committed
1923 1924 1925 1926 1927 1928 1929
                                 int *mx_ptr, int *my_ptr)
{
    MVTable *mv;
    int code, mx, my;

    mv = &mv_tables[s->mv_table_index];

1930
    code = get_vlc2(&s->gb, mv->vlc.table, MV_VLC_BITS, 2);
Michael Niedermayer's avatar
Michael Niedermayer committed
1931
    if (code < 0){
1932
        av_log(s->avctx, AV_LOG_ERROR, "illegal MV code at %d %d\n", s->mb_x, s->mb_y);
Fabrice Bellard's avatar
Fabrice Bellard committed
1933
        return -1;
Michael Niedermayer's avatar
Michael Niedermayer committed
1934
    }
Fabrice Bellard's avatar
Fabrice Bellard committed
1935
    if (code == mv->n) {
Michael Niedermayer's avatar
Michael Niedermayer committed
1936
//printf("MV ESC %X at %d %d\n", show_bits(&s->gb, 24), s->mb_x, s->mb_y);
Fabrice Bellard's avatar
Fabrice Bellard committed
1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959
        mx = get_bits(&s->gb, 6);
        my = get_bits(&s->gb, 6);
    } else {
        mx = mv->table_mvx[code];
        my = mv->table_mvy[code];
    }

    mx += *mx_ptr - 32;
    my += *my_ptr - 32;
    /* WARNING : they do not do exactly modulo encoding */
    if (mx <= -64)
        mx += 64;
    else if (mx >= 64)
        mx -= 64;

    if (my <= -64)
        my += 64;
    else if (my >= 64)
        my -= 64;
    *mx_ptr = mx;
    *my_ptr = my;
    return 0;
}