msmpeg4.c 60.9 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 8 9
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
Fabrice Bellard's avatar
Fabrice Bellard committed
10
 *
11
 * This library is distributed in the hope that it will be useful,
Fabrice Bellard's avatar
Fabrice Bellard committed
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
Fabrice Bellard's avatar
Fabrice Bellard committed
15
 *
16 17 18
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
 *
Michael Niedermayer's avatar
Michael Niedermayer committed
20
 * msmpeg4v1 & v2 stuff by Michael Niedermayer <michaelni@gmx.at>
Fabrice Bellard's avatar
Fabrice Bellard committed
21
 */
Michael Niedermayer's avatar
Michael Niedermayer committed
22 23 24 25 26 27

/**
 * @file msmpeg4.c
 * MSMPEG4 backend for ffmpeg encoder and decoder.
 */

28
#include "avcodec.h"
Fabrice Bellard's avatar
Fabrice Bellard committed
29 30
#include "dsputil.h"
#include "mpegvideo.h"
31

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

41 42 43 44 45 46 47 48 49 50 51 52 53
#define DC_VLC_BITS 9
#define CBPY_VLC_BITS 6
#define INTER_INTRA_VLC_BITS 3
#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
#define MB_NON_INTRA_VLC_BITS 9
#define MB_INTRA_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

Michael Niedermayer's avatar
Michael Niedermayer committed
62 63
static inline void msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n);
static inline int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
Michael Niedermayer's avatar
Michael Niedermayer committed
64
                                       int n, int coded, const uint8_t *scantable);
Fabrice Bellard's avatar
Fabrice Bellard committed
65 66 67
static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr);
static int msmpeg4_decode_motion(MpegEncContext * s, 
                                 int *mx_ptr, int *my_ptr);
Michael Niedermayer's avatar
Michael Niedermayer committed
68
static void msmpeg4v2_encode_motion(MpegEncContext * s, int val);
Falk Hüffner's avatar
Falk Hüffner committed
69
static void init_h263_dc_for_msmpeg4(void);
70
static inline void msmpeg4_memsetw(short *tab, int val, int n);
71
#ifdef CONFIG_ENCODERS
72
static int get_size_of_code(MpegEncContext * s, RLTable *rl, int last, int run, int level, int intra);
73
#endif //CONFIG_ENCODERS
74 75
static int msmpeg4v12_decode_mb(MpegEncContext *s, DCTELEM block[6][64]);
static int msmpeg4v34_decode_mb(MpegEncContext *s, DCTELEM block[6][64]);
Michael Niedermayer's avatar
Michael Niedermayer committed
76
static int wmv2_decode_mb(MpegEncContext *s, DCTELEM block[6][64]);
Fabrice Bellard's avatar
Fabrice Bellard committed
77

78

Fabrice Bellard's avatar
Fabrice Bellard committed
79 80 81 82 83 84 85
#ifdef DEBUG
int intra_count = 0;
int frame_count = 0;
#endif

#include "msmpeg4data.h"

86
#ifdef CONFIG_ENCODERS //strangely gcc includes this even if its not references
87
static uint8_t rl_length[NB_RL_TABLES][MAX_LEVEL+1][MAX_RUN+1][2];
88
#endif //CONFIG_ENCODERS
89

Fabrice Bellard's avatar
Fabrice Bellard committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
#ifdef STATS

const char *st_names[ST_NB] = {
    "unknown",
    "dc",
    "intra_ac",
    "inter_ac",
    "intra_mb",
    "inter_mb",
    "mv",
};

int st_current_index = 0;
unsigned int st_bit_counts[ST_NB];
unsigned int st_out_bit_counts[ST_NB];

#define set_stat(var) st_current_index = var;

void print_stats(void)
{
    unsigned int total;
    int i;

    printf("Input:\n");
    total = 0;
    for(i=0;i<ST_NB;i++)
        total += st_bit_counts[i];
    if (total == 0)
        total = 1;
    for(i=0;i<ST_NB;i++) {
        printf("%-10s : %10.1f %5.1f%%\n", 
               st_names[i], 
               (double)st_bit_counts[i] / 8.0, 
               (double)st_bit_counts[i] * 100.0 / total);
    }
    printf("%-10s : %10.1f %5.1f%%\n",
           "total", 
           (double)total / 8.0, 
           100.0);

    printf("Output:\n");
    total = 0;
    for(i=0;i<ST_NB;i++)
        total += st_out_bit_counts[i];
    if (total == 0)
        total = 1;
    for(i=0;i<ST_NB;i++) {
        printf("%-10s : %10.1f %5.1f%%\n", 
               st_names[i], 
               (double)st_out_bit_counts[i] / 8.0, 
               (double)st_out_bit_counts[i] * 100.0 / total);
    }
    printf("%-10s : %10.1f %5.1f%%\n",
           "total", 
           (double)total / 8.0, 
           100.0);
}

#else

#define set_stat(var)

#endif

Michael Niedermayer's avatar
Michael Niedermayer committed
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
static void common_init(MpegEncContext * s)
{
    static int inited=0;
    
    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;
            s->c_dc_scale_table= old_ff_c_dc_scale_table;
        } 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
174
    case 5:
Michael Niedermayer's avatar
Michael Niedermayer committed
175 176 177 178 179
        s->y_dc_scale_table= wmv1_y_dc_scale_table;
        s->c_dc_scale_table= wmv1_c_dc_scale_table;
        break;
    }

180
    
Michael Niedermayer's avatar
Michael Niedermayer committed
181
    if(s->msmpeg4_version>=4){
Michael Niedermayer's avatar
Michael Niedermayer committed
182 183 184 185
        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
186
    }
187
    //Note the default tables are set in common_init in mpegvideo.c
Michael Niedermayer's avatar
Michael Niedermayer committed
188 189 190 191 192 193 194 195
    
    if(!inited){
        inited=1;

        init_h263_dc_for_msmpeg4();
    }
}

196 197
#ifdef CONFIG_ENCODERS

Fabrice Bellard's avatar
Fabrice Bellard committed
198 199 200 201 202
/* build the table which associate a (x,y) motion vector to a vlc */
static void init_mv_table(MVTable *tab)
{
    int i, x, y;

203
    tab->table_mv_index = av_malloc(sizeof(uint16_t) * 4096);
Fabrice Bellard's avatar
Fabrice Bellard committed
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
    /* mark all entries as not used */
    for(i=0;i<4096;i++)
        tab->table_mv_index[i] = tab->n;
    
    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;
    }
}

static void code012(PutBitContext *pb, int n)
{
    if (n == 0) {
        put_bits(pb, 1, 0);
    } else {
        put_bits(pb, 1, 1);
        put_bits(pb, 1, (n >= 2));
    }
}

Michael Niedermayer's avatar
Michael Niedermayer committed
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
void ff_msmpeg4_encode_init(MpegEncContext *s)
{
    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++)
            init_rl(&rl_table[i]);
243 244 245 246 247 248 249 250

        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++){
251
                        rl_length[i][level][run][last]= get_size_of_code(s, &rl_table[  i], last, run, level, 0);
252 253 254 255
                    }
                }
            }
        }
Michael Niedermayer's avatar
Michael Niedermayer committed
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
    }
}

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;
    
    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];
        if (level1 < 1) 
            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;
}

static void find_best_tables(MpegEncContext * s)
{
    int i;
    int best       =-1, best_size       =9999999;
    int chroma_best=-1, best_chroma_size=9999999;
305

Michael Niedermayer's avatar
Michael Niedermayer committed
306 307 308 309 310 311 312 313 314 315 316 317 318
    for(i=0; i<3; i++){
        int level;
        int chroma_size=0;
        int size=0;

        if(i>0){// ;)
            size++; 
            chroma_size++;
        }
        for(level=0; level<=MAX_LEVEL; level++){
            int run;
            for(run=0; run<=MAX_RUN; run++){
                int last;
319
                const int last_size= size + chroma_size;
Michael Niedermayer's avatar
Michael Niedermayer committed
320 321 322 323
                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];
324
                    
Michael Niedermayer's avatar
Michael Niedermayer committed
325
                    if(s->pict_type==I_TYPE){
326 327
                        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
328
                    }else{
329 330 331
                        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];
Michael Niedermayer's avatar
Michael Niedermayer committed
332 333
                    }                   
                }
334
                if(last_size == size+chroma_size) break;
Michael Niedermayer's avatar
Michael Niedermayer committed
335 336 337 338 339 340 341 342 343 344 345
            }
        }
        if(size<best_size){
            best_size= size;
            best= i;
        }
        if(chroma_size<best_chroma_size){
            best_chroma_size= chroma_size;
            chroma_best= i;
        }
    }
346

Michael Niedermayer's avatar
Michael Niedermayer committed
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
//    printf("type:%d, best:%d, qp:%d, var:%d, mcvar:%d, size:%d //\n", 
//           s->pict_type, best, s->qscale, s->mb_var_sum, s->mc_mb_var_sum, best_size);
           
    if(s->pict_type==P_TYPE) chroma_best= best;

    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;
    
    if(s->pict_type != s->last_non_b_pict_type){
        s->rl_table_index= 2;
        if(s->pict_type==I_TYPE)
            s->rl_chroma_table_index= 1;
        else
            s->rl_chroma_table_index= 2;
    }

}

Michael Niedermayer's avatar
Michael Niedermayer committed
367
/* write MSMPEG4 compatible frame header */
Fabrice Bellard's avatar
Fabrice Bellard committed
368 369
void msmpeg4_encode_picture_header(MpegEncContext * s, int picture_number)
{
Michael Niedermayer's avatar
Michael Niedermayer committed
370
    find_best_tables(s);
Fabrice Bellard's avatar
Fabrice Bellard committed
371 372 373 374 375

    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
376 377 378 379
    if(s->msmpeg4_version<=2){
        s->rl_table_index = 2;
        s->rl_chroma_table_index = 2;
    }
Michael Niedermayer's avatar
Michael Niedermayer committed
380

Fabrice Bellard's avatar
Fabrice Bellard committed
381 382 383
    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
384
    s->per_mb_rl_table = 0;
385 386
    if(s->msmpeg4_version==4)
        s->inter_intra_pred= (s->width*s->height < 320*240 && s->bit_rate<=II_BITRATE && s->pict_type==P_TYPE);
Michael Niedermayer's avatar
Michael Niedermayer committed
387
//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
388

Fabrice Bellard's avatar
Fabrice Bellard committed
389
    if (s->pict_type == I_TYPE) {
390 391
        s->slice_height= s->mb_height/1;
        put_bits(&s->pb, 5, 0x16 + s->mb_height/s->slice_height);
Michael Niedermayer's avatar
Michael Niedermayer committed
392 393 394
        
        if(s->msmpeg4_version==4){
            msmpeg4_encode_ext_header(s);
395
            if(s->bit_rate>MBAC_BITRATE)
396
                put_bits(&s->pb, 1, s->per_mb_rl_table);
Michael Niedermayer's avatar
Michael Niedermayer committed
397
        }
Fabrice Bellard's avatar
Fabrice Bellard committed
398

Michael Niedermayer's avatar
Michael Niedermayer committed
399
        if(s->msmpeg4_version>2){
Michael Niedermayer's avatar
Michael Niedermayer committed
400 401 402 403
            if(!s->per_mb_rl_table){
                code012(&s->pb, s->rl_chroma_table_index);
                code012(&s->pb, s->rl_table_index);
            }
Fabrice Bellard's avatar
Fabrice Bellard committed
404

Michael Niedermayer's avatar
Michael Niedermayer committed
405 406
            put_bits(&s->pb, 1, s->dc_table_index);
        }
Fabrice Bellard's avatar
Fabrice Bellard committed
407 408 409
    } else {
        put_bits(&s->pb, 1, s->use_skip_mb_code);
        
410
        if(s->msmpeg4_version==4 && s->bit_rate>MBAC_BITRATE)
Michael Niedermayer's avatar
Michael Niedermayer committed
411 412
            put_bits(&s->pb, 1, s->per_mb_rl_table);

Michael Niedermayer's avatar
Michael Niedermayer committed
413
        if(s->msmpeg4_version>2){
Michael Niedermayer's avatar
Michael Niedermayer committed
414 415
            if(!s->per_mb_rl_table)
                code012(&s->pb, s->rl_table_index);
Fabrice Bellard's avatar
Fabrice Bellard committed
416

Michael Niedermayer's avatar
Michael Niedermayer committed
417 418 419 420
            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
421 422
    }

Michael Niedermayer's avatar
Michael Niedermayer committed
423 424
    s->esc3_level_length= 0;
    s->esc3_run_length= 0;
Fabrice Bellard's avatar
Fabrice Bellard committed
425 426 427 428 429 430 431

#ifdef DEBUG
    intra_count = 0;
    printf("*****frame %d:\n", frame_count++);
#endif
}

432 433
void msmpeg4_encode_ext_header(MpegEncContext * s)
{
434
        put_bits(&s->pb, 5, s->avctx->frame_rate / s->avctx->frame_rate_base); //yes 29.97 -> 29
435

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

438
        if(s->msmpeg4_version>=3)
Michael Niedermayer's avatar
Michael Niedermayer committed
439
            put_bits(&s->pb, 1, s->flipflop_rounding);
440 441
        else
            assert(s->flipflop_rounding==0);
442 443
}

444 445
#endif //CONFIG_ENCODERS

Fabrice Bellard's avatar
Fabrice Bellard committed
446
/* predict coded block */
447
static inline int coded_block_pred(MpegEncContext * s, int n, uint8_t **coded_block_ptr)
Fabrice Bellard's avatar
Fabrice Bellard committed
448
{
449
    int xy, wrap, pred, a, b, c;
Fabrice Bellard's avatar
Fabrice Bellard committed
450

451
    xy = s->block_index[n];
452
    wrap = s->b8_stride;
Fabrice Bellard's avatar
Fabrice Bellard committed
453 454 455 456

    /* B C
     * A X 
     */
457 458 459
    a = s->coded_block[xy - 1       ];
    b = s->coded_block[xy - 1 - wrap];
    c = s->coded_block[xy     - wrap];
Fabrice Bellard's avatar
Fabrice Bellard committed
460 461 462 463 464 465 466 467
    
    if (b == c) {
        pred = a;
    } else {
        pred = c;
    }
    
    /* store value */
468
    *coded_block_ptr = &s->coded_block[xy];
Fabrice Bellard's avatar
Fabrice Bellard committed
469 470 471 472

    return pred;
}

473 474
#ifdef CONFIG_ENCODERS

Fabrice Bellard's avatar
Fabrice Bellard committed
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
static void msmpeg4_encode_motion(MpegEncContext * s, 
                                  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;
    
    mx += 32;
    my += 32;
#if 0
    if ((unsigned)mx >= 64 ||
        (unsigned)my >= 64) 
        fprintf(stderr, "error mx=%d my=%d\n", mx, my);
#endif
    mv = &mv_tables[s->mv_table_index];

    code = mv->table_mv_index[(mx << 6) | my];
    set_stat(ST_MV);
    put_bits(&s->pb, 
             mv->table_mv_bits[code], 
             mv->table_mv_code[code]);
    if (code == mv->n) {
        /* escape : code litterally */
        put_bits(&s->pb, 6, mx);
        put_bits(&s->pb, 6, my);
    }
}

514 515 516
static inline void handle_slices(MpegEncContext *s){
    if (s->mb_x == 0) {
        if (s->slice_height && (s->mb_y % s->slice_height) == 0) {
Michael Niedermayer's avatar
Michael Niedermayer committed
517
            if(s->msmpeg4_version < 4){
518
                ff_mpeg4_clean_buffers(s);
519 520 521 522 523 524 525 526
            }
            s->first_slice_line = 1;
        } else {
            s->first_slice_line = 0; 
        }
    }
}

Fabrice Bellard's avatar
Fabrice Bellard committed
527 528 529 530 531 532
void msmpeg4_encode_mb(MpegEncContext * s, 
                       DCTELEM block[6][64],
                       int motion_x, int motion_y)
{
    int cbp, coded_cbp, i;
    int pred_x, pred_y;
533
    uint8_t *coded_block;
Fabrice Bellard's avatar
Fabrice Bellard committed
534

535 536
    handle_slices(s);
    
Fabrice Bellard's avatar
Fabrice Bellard committed
537 538 539 540 541 542 543 544 545 546 547
    if (!s->mb_intra) {
	/* compute cbp */
        set_stat(ST_INTER_MB);
	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);
548 549
            s->last_bits++;
	    s->misc_bits++;
550
            s->skip_count++;
551

Fabrice Bellard's avatar
Fabrice Bellard committed
552 553 554 555 556
	    return;
	}
        if (s->use_skip_mb_code)
            put_bits(&s->pb, 1, 0);	/* mb coded */
        
Michael Niedermayer's avatar
Michael Niedermayer committed
557
        if(s->msmpeg4_version<=2){
Michael Niedermayer's avatar
Michael Niedermayer committed
558 559 560 561 562 563 564 565 566
            put_bits(&s->pb, 
                     v2_mb_type[cbp&3][1], 
                     v2_mb_type[cbp&3][0]);
            if((cbp&3) != 3) coded_cbp= cbp ^ 0x3C;
            else             coded_cbp= cbp;

            put_bits(&s->pb, 
                     cbpy_tab[coded_cbp>>2][1], 
                     cbpy_tab[coded_cbp>>2][0]);
567 568 569

            s->misc_bits += get_bits_diff(s);

570
            h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
Michael Niedermayer's avatar
Michael Niedermayer committed
571 572 573 574 575 576 577
            msmpeg4v2_encode_motion(s, motion_x - pred_x);
            msmpeg4v2_encode_motion(s, motion_y - pred_y);
        }else{
            put_bits(&s->pb, 
                     table_mb_non_intra[cbp + 64][1], 
                     table_mb_non_intra[cbp + 64][0]);

578 579
            s->misc_bits += get_bits_diff(s);

Michael Niedermayer's avatar
Michael Niedermayer committed
580
            /* motion vector */
581
            h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
Michael Niedermayer's avatar
Michael Niedermayer committed
582 583 584
            msmpeg4_encode_motion(s, motion_x - pred_x, 
                                  motion_y - pred_y);
        }
585 586 587 588 589 590 591

        s->mv_bits += get_bits_diff(s);

        for (i = 0; i < 6; i++) {
            msmpeg4_encode_block(s, block[i], i);
        }
        s->p_tex_bits += get_bits_diff(s);
Fabrice Bellard's avatar
Fabrice Bellard committed
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
    } else {
	/* compute cbp */
	cbp = 0;
        coded_cbp = 0;
	for (i = 0; i < 6; i++) {
            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 */
                pred = coded_block_pred(s, i, &coded_block);
                *coded_block = val;
                val = val ^ pred;
            }
            coded_cbp |= val << (5 - i);
	}
#if 0
        if (coded_cbp)
            printf("cbp=%x %x\n", cbp, coded_cbp);
#endif

Michael Niedermayer's avatar
Michael Niedermayer committed
613
        if(s->msmpeg4_version<=2){
Michael Niedermayer's avatar
Michael Niedermayer committed
614 615 616 617 618 619 620 621 622 623 624
            if (s->pict_type == I_TYPE) {
                put_bits(&s->pb, 
                         v2_intra_cbpc[cbp&3][1], v2_intra_cbpc[cbp&3][0]);
            } else {
                if (s->use_skip_mb_code)
                    put_bits(&s->pb, 1, 0);	/* mb coded */
                put_bits(&s->pb, 
                         v2_mb_type[(cbp&3) + 4][1], 
                         v2_mb_type[(cbp&3) + 4][0]);
            }
            put_bits(&s->pb, 1, 0);	/* no AC prediction yet */
Fabrice Bellard's avatar
Fabrice Bellard committed
625
            put_bits(&s->pb, 
Michael Niedermayer's avatar
Michael Niedermayer committed
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
                     cbpy_tab[cbp>>2][1], 
                     cbpy_tab[cbp>>2][0]);
        }else{
            if (s->pict_type == I_TYPE) {
                set_stat(ST_INTRA_MB);
                put_bits(&s->pb, 
                         table_mb_intra[coded_cbp][1], table_mb_intra[coded_cbp][0]);
            } else {
                if (s->use_skip_mb_code)
                    put_bits(&s->pb, 1, 0);	/* mb coded */
                put_bits(&s->pb, 
                         table_mb_non_intra[cbp][1], 
                         table_mb_non_intra[cbp][0]);
            }
            set_stat(ST_INTRA_MB);
            put_bits(&s->pb, 1, 0);	/* no AC prediction yet */
642 643 644 645
            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
646
        }
647
        s->misc_bits += get_bits_diff(s);
Fabrice Bellard's avatar
Fabrice Bellard committed
648

649 650 651 652
        for (i = 0; i < 6; i++) {
            msmpeg4_encode_block(s, block[i], i);
        }
        s->i_tex_bits += get_bits_diff(s);
653
        s->i_count++;
Fabrice Bellard's avatar
Fabrice Bellard committed
654 655 656
    }
}

657 658
#endif //CONFIG_ENCODERS

659
/* old ffmpeg msmpeg4v3 mode */
660
static void ff_old_msmpeg4_dc_scale(MpegEncContext * s)
Fabrice Bellard's avatar
Fabrice Bellard committed
661
{
662
    if (s->qscale < 5){
663 664 665 666 667
        s->y_dc_scale = 8;
        s->c_dc_scale = 8;
    }else if (s->qscale < 9){
        s->y_dc_scale = 2 * s->qscale;
        s->c_dc_scale = (s->qscale + 13)>>1;
668
    }else{
669 670 671
        s->y_dc_scale = s->qscale + 8;
        s->c_dc_scale = (s->qscale + 13)>>1;
    }
Fabrice Bellard's avatar
Fabrice Bellard committed
672 673
}

Michael Niedermayer's avatar
Michael Niedermayer committed
674
static inline int msmpeg4v1_pred_dc(MpegEncContext * s, int n, 
675
                                    int32_t **dc_val_ptr)
Michael Niedermayer's avatar
Michael Niedermayer committed
676 677 678 679 680 681 682 683 684 685 686 687 688
{
    int i;

    if (n < 4) {
        i= 0;
    } else {
        i= n-3;
    }
    
    *dc_val_ptr= &s->last_dc[i];
    return s->last_dc[i]; 
}

689 690 691 692 693 694 695 696 697 698
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];
        }
    }
699
    return FASTDIV((sum + (scale>>1)), scale);
700 701
}

Fabrice Bellard's avatar
Fabrice Bellard committed
702
/* dir = 0: left, dir = 1: top prediction */
Michael Niedermayer's avatar
Michael Niedermayer committed
703
static inline int msmpeg4_pred_dc(MpegEncContext * s, int n, 
704
                             uint16_t **dc_val_ptr, int *dir_ptr)
Fabrice Bellard's avatar
Fabrice Bellard committed
705
{
706
    int a, b, c, wrap, pred, scale;
707
    int16_t *dc_val;
Fabrice Bellard's avatar
Fabrice Bellard committed
708 709 710 711 712 713 714

    /* find prediction */
    if (n < 4) {
	scale = s->y_dc_scale;
    } else {
	scale = s->c_dc_scale;
    }
Michael Niedermayer's avatar
Michael Niedermayer committed
715
    
716 717
    wrap = s->block_wrap[n];
    dc_val= s->dc_val[0] + s->block_index[n];
Fabrice Bellard's avatar
Fabrice Bellard committed
718 719 720 721

    /* B C
     * A X 
     */
722 723 724
    a = dc_val[ - 1];
    b = dc_val[ - 1 - wrap];
    c = dc_val[ - wrap];
725
    
Michael Niedermayer's avatar
Michael Niedermayer committed
726
    if(s->first_slice_line && (n&2)==0 && s->msmpeg4_version<4){
727 728
        b=c=1024;
    }
Fabrice Bellard's avatar
Fabrice Bellard committed
729 730 731 732 733

    /* 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 !) */
734
#if defined ARCH_X86 && !defined PIC
735 736 737 738 739 740
    asm volatile(
        "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"
741 742
	"mull %4		\n\t"
	"movl %%edx, %0		\n\t"
743
	"movl %1, %%eax		\n\t"
744 745
	"mull %4		\n\t"
	"movl %%edx, %1		\n\t"
746
	"movl %2, %%eax		\n\t"
747 748
	"mull %4		\n\t"
	"movl %%edx, %2		\n\t"
749 750
	: "+b" (a), "+c" (b), "+D" (c)
	: "g" (scale), "S" (inverse[scale])
751 752
	: "%eax", "%edx"
    );
753 754
#else
    /* #elif defined (ARCH_ALPHA) */
755
    /* Divisions are extremely costly on Alpha; optimize the most
756 757
       common case. But they are costly everywhere...
     */
758 759 760 761 762
    if (scale == 8) {
	a = (a + (8 >> 1)) / 8;
	b = (b + (8 >> 1)) / 8;
	c = (c + (8 >> 1)) / 8;
    } else {
763 764 765
	a = FASTDIV((a + (scale >> 1)), scale);
	b = FASTDIV((b + (scale >> 1)), scale);
	c = FASTDIV((c + (scale >> 1)), scale);
766
    }
767
#endif
Fabrice Bellard's avatar
Fabrice Bellard committed
768 769
    /* XXX: WARNING: they did not choose the same test as MPEG4. This
       is very important ! */
770
    if(s->msmpeg4_version>3){
771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791
        if(s->inter_intra_pred){
            uint8_t *dest;
            int wrap;
            
            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
792
                    dest= s->current_picture.data[0] + (((n>>1) + 2*s->mb_y) * 8*  wrap ) + ((n&1) + 2*s->mb_x) * 8;
793
                }else{
Michael Niedermayer's avatar
Michael Niedermayer committed
794
                    wrap= s->uvlinesize;
Michael Niedermayer's avatar
Michael Niedermayer committed
795
                    dest= s->current_picture.data[n-3] + (s->mb_y * 8 * wrap) + s->mb_x * 8;
796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833
                }
                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);
                
                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;
            }
834 835 836 837 838 839 840 841 842
        }
    }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
843 844 845
    }

    /* update predictor */
846
    *dc_val_ptr = &dc_val[0];
Fabrice Bellard's avatar
Fabrice Bellard committed
847 848 849 850 851 852 853 854 855 856
    return pred;
}

#define DC_MAX 119

static void msmpeg4_encode_dc(MpegEncContext * s, int level, int n, int *dir_ptr)
{
    int sign, code;
    int pred;

Michael Niedermayer's avatar
Michael Niedermayer committed
857
    if(s->msmpeg4_version==1){
858
        int32_t *dc_val;
Michael Niedermayer's avatar
Michael Niedermayer committed
859 860 861 862 863
        pred = msmpeg4v1_pred_dc(s, n, &dc_val);
        
        /* update predictor */
        *dc_val= level;
    }else{
864
        uint16_t *dc_val;
865
        pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr);
Fabrice Bellard's avatar
Fabrice Bellard committed
866

Michael Niedermayer's avatar
Michael Niedermayer committed
867 868 869 870 871 872
        /* 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
873 874 875 876 877
    }

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

Michael Niedermayer's avatar
Michael Niedermayer committed
878
    if(s->msmpeg4_version<=2){
Fabrice Bellard's avatar
Fabrice Bellard committed
879
        if (n < 4) {
Michael Niedermayer's avatar
Michael Niedermayer committed
880 881 882 883 884 885 886
            put_bits(&s->pb, 
                     v2_dc_lum_table[level+256][1],
                     v2_dc_lum_table[level+256][0]);
        }else{
            put_bits(&s->pb, 
                     v2_dc_chroma_table[level+256][1],
                     v2_dc_chroma_table[level+256][0]);
Fabrice Bellard's avatar
Fabrice Bellard committed
887
        }
Michael Niedermayer's avatar
Michael Niedermayer committed
888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903
    }else{
        sign = 0;
        if (level < 0) {
            level = -level;
            sign = 1;
        }
        code = level;
        if (code > DC_MAX) 
            code = DC_MAX;

        if (s->dc_table_index == 0) {
            if (n < 4) {
                put_bits(&s->pb, table0_dc_lum[code][1], table0_dc_lum[code][0]);
            } else {
                put_bits(&s->pb, table0_dc_chroma[code][1], table0_dc_chroma[code][0]);
            }
Fabrice Bellard's avatar
Fabrice Bellard committed
904
        } else {
Michael Niedermayer's avatar
Michael Niedermayer committed
905 906 907 908 909 910 911 912 913 914 915 916
            if (n < 4) {
                put_bits(&s->pb, table1_dc_lum[code][1], table1_dc_lum[code][0]);
            } else {
                put_bits(&s->pb, table1_dc_chroma[code][1], table1_dc_chroma[code][0]);
            }
        }
            
        if (code == DC_MAX)
            put_bits(&s->pb, 8, level);
            
        if (level != 0) {
            put_bits(&s->pb, 1, sign);
Fabrice Bellard's avatar
Fabrice Bellard committed
917 918 919 920 921 922 923
        }
    }
}

/* Encoding of a block. Very similar to MPEG4 except for a different
   escape coding (same as H263) and more vlc tables.
 */
Michael Niedermayer's avatar
Michael Niedermayer committed
924
static inline void msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n)
Fabrice Bellard's avatar
Fabrice Bellard committed
925 926 927 928 929
{
    int level, run, last, i, j, last_index;
    int last_non_zero, sign, slevel;
    int code, run_diff, dc_pred_dir;
    const RLTable *rl;
930
    const uint8_t *scantable;
Fabrice Bellard's avatar
Fabrice Bellard committed
931 932 933 934 935 936 937 938 939 940 941

    if (s->mb_intra) {
        set_stat(ST_DC);
        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];
        }
        run_diff = 0;
942
        scantable= s->intra_scantable.permutated;
Fabrice Bellard's avatar
Fabrice Bellard committed
943 944 945 946
        set_stat(ST_INTRA_AC);
    } else {
        i = 0;
        rl = &rl_table[3 + s->rl_table_index];
Michael Niedermayer's avatar
Michael Niedermayer committed
947
        if(s->msmpeg4_version<=2)
Michael Niedermayer's avatar
Michael Niedermayer committed
948 949 950
            run_diff = 0;
        else
            run_diff = 1;
951
        scantable= s->inter_scantable.permutated;
Fabrice Bellard's avatar
Fabrice Bellard committed
952 953 954
        set_stat(ST_INTER_AC);
    }

Michael Niedermayer's avatar
Michael Niedermayer committed
955
    /* recalculate block_last_index for M$ wmv1 */
Michael Niedermayer's avatar
Michael Niedermayer committed
956
    if(s->msmpeg4_version>=4 && s->block_last_index[n]>0){
Michael Niedermayer's avatar
Michael Niedermayer committed
957 958 959
        for(last_index=63; last_index>=0; last_index--){
            if(block[scantable[last_index]]) break;
        }
960
        s->block_last_index[n]= last_index;
Michael Niedermayer's avatar
Michael Niedermayer committed
961 962
    }else
        last_index = s->block_last_index[n];
Fabrice Bellard's avatar
Fabrice Bellard committed
963 964 965
    /* AC coefs */
    last_non_zero = i - 1;
    for (; i <= last_index; i++) {
Michael Niedermayer's avatar
Michael Niedermayer committed
966
	j = scantable[i];
Fabrice Bellard's avatar
Fabrice Bellard committed
967 968 969 970 971 972 973 974 975 976
	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;
	    }
977

Michael Niedermayer's avatar
Michael Niedermayer committed
978 979 980 981 982 983 984
            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
985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007
            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];
                if (level1 < 1) 
                    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;
                    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
1008
                        if(s->msmpeg4_version>=4){
Michael Niedermayer's avatar
Michael Niedermayer committed
1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
                            if(s->esc3_level_length==0){
                                s->esc3_level_length=8;
                                s->esc3_run_length= 6;
                                if(s->qscale<8)
                                    put_bits(&s->pb, 6, 3);
                                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);
                            put_bits(&s->pb, 8, slevel & 0xff);
                        }
Fabrice Bellard's avatar
Fabrice Bellard committed
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046
                    } 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);
            }
	    last_non_zero = i;
	}
    }
}

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

Michael Niedermayer's avatar
Michael Niedermayer committed
1047
static VLC mb_non_intra_vlc[4];
Fabrice Bellard's avatar
Fabrice Bellard committed
1048 1049 1050
static VLC mb_intra_vlc;
static VLC dc_lum_vlc[2];
static VLC dc_chroma_vlc[2];
1051 1052 1053 1054 1055 1056
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
1057 1058
static VLC v1_intra_cbpc_vlc;
static VLC v1_inter_cbpc_vlc;
1059
static VLC inter_intra_vlc;
1060 1061

/* this table is practically identical to the one from h263 except that its inverted */
Falk Hüffner's avatar
Falk Hüffner committed
1062
static void init_h263_dc_for_msmpeg4(void)
1063 1064 1065
{
        int level, uni_code, uni_len;

1066
        for(level=-256; level<256; level++){
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114
            int size, v, l;
            /* find number of bits */
            size = 0;
            v = abs(level);
            while (v) {
                v >>= 1;
		    size++;
            }

            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];
            uni_code ^= (1<<uni_len)-1; //M$ doesnt like compatibility

            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];
            uni_code ^= (1<<uni_len)-1; //M$ doesnt like compatibility
            
            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
1115 1116

/* init all vlc decoding tables */
Michael Niedermayer's avatar
Michael Niedermayer committed
1117
int ff_msmpeg4_decode_init(MpegEncContext *s)
Fabrice Bellard's avatar
Fabrice Bellard committed
1118
{
1119
    static int done = 0;
Fabrice Bellard's avatar
Fabrice Bellard committed
1120 1121 1122
    int i;
    MVTable *mv;

Michael Niedermayer's avatar
Michael Niedermayer committed
1123
    common_init(s);
Fabrice Bellard's avatar
Fabrice Bellard committed
1124

1125 1126 1127 1128 1129 1130 1131 1132 1133
    if (!done) {
        done = 1;

        for(i=0;i<NB_RL_TABLES;i++) {
            init_rl(&rl_table[i]);
            init_vlc_rl(&rl_table[i]);
        }
        for(i=0;i<2;i++) {
            mv = &mv_tables[i];
1134
            init_vlc(&mv->vlc, MV_VLC_BITS, mv->n + 1, 
1135 1136 1137 1138
                     mv->table_mv_bits, 1, 1,
                     mv->table_mv_code, 2, 2);
        }

1139
        init_vlc(&dc_lum_vlc[0], DC_VLC_BITS, 120, 
1140 1141
                 &table0_dc_lum[0][1], 8, 4,
                 &table0_dc_lum[0][0], 8, 4);
1142
        init_vlc(&dc_chroma_vlc[0], DC_VLC_BITS, 120, 
1143 1144
                 &table0_dc_chroma[0][1], 8, 4,
                 &table0_dc_chroma[0][0], 8, 4);
1145
        init_vlc(&dc_lum_vlc[1], DC_VLC_BITS, 120, 
1146 1147
                 &table1_dc_lum[0][1], 8, 4,
                 &table1_dc_lum[0][0], 8, 4);
1148
        init_vlc(&dc_chroma_vlc[1], DC_VLC_BITS, 120, 
1149 1150
                 &table1_dc_chroma[0][1], 8, 4,
                 &table1_dc_chroma[0][0], 8, 4);
1151
    
1152
        init_vlc(&v2_dc_lum_vlc, DC_VLC_BITS, 512, 
1153 1154
                 &v2_dc_lum_table[0][1], 8, 4,
                 &v2_dc_lum_table[0][0], 8, 4);
1155
        init_vlc(&v2_dc_chroma_vlc, DC_VLC_BITS, 512, 
1156 1157
                 &v2_dc_chroma_table[0][1], 8, 4,
                 &v2_dc_chroma_table[0][0], 8, 4);
1158
    
1159
        init_vlc(&cbpy_vlc, CBPY_VLC_BITS, 16,
1160 1161
                 &cbpy_tab[0][1], 2, 1,
                 &cbpy_tab[0][0], 2, 1);
1162
        init_vlc(&v2_intra_cbpc_vlc, V2_INTRA_CBPC_VLC_BITS, 4,
1163 1164
                 &v2_intra_cbpc[0][1], 2, 1,
                 &v2_intra_cbpc[0][0], 2, 1);
1165
        init_vlc(&v2_mb_type_vlc, V2_MB_TYPE_VLC_BITS, 8,
1166 1167
                 &v2_mb_type[0][1], 2, 1,
                 &v2_mb_type[0][0], 2, 1);
1168
        init_vlc(&v2_mv_vlc, V2_MV_VLC_BITS, 33,
1169 1170 1171
                 &mvtab[0][1], 2, 1,
                 &mvtab[0][0], 2, 1);

Michael Niedermayer's avatar
Michael Niedermayer committed
1172 1173 1174 1175 1176 1177
        for(i=0; i<4; i++){
            init_vlc(&mb_non_intra_vlc[i], MB_NON_INTRA_VLC_BITS, 128, 
                     &wmv2_inter_table[i][0][1], 8, 4,
                     &wmv2_inter_table[i][0][0], 8, 4); //FIXME name?
        }
        
1178
        init_vlc(&mb_intra_vlc, MB_INTRA_VLC_BITS, 64, 
1179 1180
                 &table_mb_intra[0][1], 4, 2,
                 &table_mb_intra[0][0], 4, 2);
Michael Niedermayer's avatar
Michael Niedermayer committed
1181
        
1182
        init_vlc(&v1_intra_cbpc_vlc, V1_INTRA_CBPC_VLC_BITS, 8, 
1183 1184
                 intra_MCBPC_bits, 1, 1,
                 intra_MCBPC_code, 1, 1);
1185
        init_vlc(&v1_inter_cbpc_vlc, V1_INTER_CBPC_VLC_BITS, 25, 
1186 1187
                 inter_MCBPC_bits, 1, 1,
                 inter_MCBPC_code, 1, 1);
1188
        
1189
        init_vlc(&inter_intra_vlc, INTER_INTRA_VLC_BITS, 4, 
1190 1191
                 &table_inter_intra[0][1], 2, 1,
                 &table_inter_intra[0][0], 2, 1);
1192
    }
1193 1194 1195 1196 1197 1198 1199 1200 1201 1202
    
    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
1203 1204 1205
    case 5:
        s->decode_mb= wmv2_decode_mb;
        break;
1206 1207
    }
    
1208 1209
    s->slice_height= s->mb_height; //to avoid 1/0 if the first frame isnt a keyframe
    
Fabrice Bellard's avatar
Fabrice Bellard committed
1210 1211 1212 1213 1214 1215
    return 0;
}

static int decode012(GetBitContext *gb)
{
    int n;
1216
    n = get_bits1(gb);
Fabrice Bellard's avatar
Fabrice Bellard committed
1217 1218 1219
    if (n == 0)
        return 0;
    else
1220
        return get_bits1(gb) + 1;
Fabrice Bellard's avatar
Fabrice Bellard committed
1221 1222
}

1223
int msmpeg4_decode_picture_header(MpegEncContext * s)
Fabrice Bellard's avatar
Fabrice Bellard committed
1224
{
Michael Niedermayer's avatar
Michael Niedermayer committed
1225
    int code;
Fabrice Bellard's avatar
Fabrice Bellard committed
1226

1227 1228 1229
#if 0
{
int i;
1230
for(i=0; i<s->gb.size_in_bits; i++)
1231 1232 1233 1234 1235 1236
    printf("%d", get_bits1(&s->gb));
//    get_bits1(&s->gb);
printf("END\n");
return -1;
}
#endif
Michael Niedermayer's avatar
Michael Niedermayer committed
1237 1238 1239 1240 1241

    if(s->msmpeg4_version==1){
        int start_code, num;
        start_code = (get_bits(&s->gb, 16)<<16) | get_bits(&s->gb, 16);
        if(start_code!=0x00000100){
1242
            av_log(s->avctx, AV_LOG_ERROR, "invalid startcode\n");
Michael Niedermayer's avatar
Michael Niedermayer committed
1243 1244 1245 1246 1247 1248
            return -1;
        }

        num= get_bits(&s->gb, 5); // frame number */
    }

Fabrice Bellard's avatar
Fabrice Bellard committed
1249 1250
    s->pict_type = get_bits(&s->gb, 2) + 1;
    if (s->pict_type != I_TYPE &&
Michael Niedermayer's avatar
Michael Niedermayer committed
1251
        s->pict_type != P_TYPE){
1252
        av_log(s->avctx, AV_LOG_ERROR, "invalid picture type\n");
Fabrice Bellard's avatar
Fabrice Bellard committed
1253
        return -1;
Michael Niedermayer's avatar
Michael Niedermayer committed
1254
    }
1255 1256 1257 1258 1259 1260 1261
#if 0
{
    static int had_i=0;
    if(s->pict_type == I_TYPE) had_i=1;
    if(!had_i) return -1;
}
#endif
1262
    s->chroma_qscale= s->qscale = get_bits(&s->gb, 5);
1263
    if(s->qscale==0){
1264
        av_log(s->avctx, AV_LOG_ERROR, "invalid qscale\n");
1265 1266
        return -1;
    }
Fabrice Bellard's avatar
Fabrice Bellard committed
1267 1268 1269

    if (s->pict_type == I_TYPE) {
        code = get_bits(&s->gb, 5); 
Michael Niedermayer's avatar
Michael Niedermayer committed
1270 1271
        if(s->msmpeg4_version==1){
            if(code==0 || code>s->mb_height){
1272
                av_log(s->avctx, AV_LOG_ERROR, "invalid slice height %d\n", code);
Michael Niedermayer's avatar
Michael Niedermayer committed
1273 1274 1275 1276 1277 1278
                return -1;
            }

            s->slice_height = code;
        }else{
            /* 0x17: one slice, 0x18: two slices, ... */
1279
            if (code < 0x17){
1280
                av_log(s->avctx, AV_LOG_ERROR, "error, slice code was %X\n", code);
Michael Niedermayer's avatar
Michael Niedermayer committed
1281
                return -1;
1282
            }
Michael Niedermayer's avatar
Michael Niedermayer committed
1283 1284 1285

            s->slice_height = s->mb_height / (code - 0x16);
        }
1286 1287

        switch(s->msmpeg4_version){
Michael Niedermayer's avatar
Michael Niedermayer committed
1288
        case 1:
1289
        case 2:
1290 1291
            s->rl_chroma_table_index = 2;
            s->rl_table_index = 2;
1292

1293
            s->dc_table_index = 0; //not used
1294 1295
            break;
        case 3:
1296 1297
            s->rl_chroma_table_index = decode012(&s->gb);
            s->rl_table_index = decode012(&s->gb);
1298

1299
            s->dc_table_index = get_bits1(&s->gb);
1300 1301
            break;
        case 4:
Michael Niedermayer's avatar
Michael Niedermayer committed
1302 1303
            msmpeg4_decode_ext_header(s, (2+5+5+17+7)/8);

1304 1305
            if(s->bit_rate > MBAC_BITRATE) s->per_mb_rl_table= get_bits1(&s->gb);
            else                           s->per_mb_rl_table= 0;
1306
            
Michael Niedermayer's avatar
Michael Niedermayer committed
1307 1308 1309
            if(!s->per_mb_rl_table){
                s->rl_chroma_table_index = decode012(&s->gb);
                s->rl_table_index = decode012(&s->gb);
1310 1311
            }

Michael Niedermayer's avatar
Michael Niedermayer committed
1312
            s->dc_table_index = get_bits1(&s->gb);
1313
            s->inter_intra_pred= 0;
1314
            break;
1315
        }
Fabrice Bellard's avatar
Fabrice Bellard committed
1316
        s->no_rounding = 1;
Michael Niedermayer's avatar
Michael Niedermayer committed
1317
        if(s->avctx->debug&FF_DEBUG_PICT_INFO)
1318
	    av_log(s->avctx, AV_LOG_DEBUG, "qscale:%d rlc:%d rl:%d dc:%d mbrl:%d slice:%d   \n", 
1319 1320 1321
		s->qscale,
		s->rl_chroma_table_index,
		s->rl_table_index, 
Michael Niedermayer's avatar
Michael Niedermayer committed
1322
		s->dc_table_index,
1323
                s->per_mb_rl_table,
Michael Niedermayer's avatar
Michael Niedermayer committed
1324
                s->slice_height);
Fabrice Bellard's avatar
Fabrice Bellard committed
1325
    } else {
Michael Niedermayer's avatar
Michael Niedermayer committed
1326 1327 1328 1329 1330 1331 1332
        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);
1333 1334 1335 1336
            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
1337 1338 1339
            break;
        case 3:
            s->use_skip_mb_code = get_bits1(&s->gb);
1340 1341
            s->rl_table_index = decode012(&s->gb);
            s->rl_chroma_table_index = s->rl_table_index;
Fabrice Bellard's avatar
Fabrice Bellard committed
1342

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

Michael Niedermayer's avatar
Michael Niedermayer committed
1345 1346 1347 1348
            s->mv_table_index = get_bits1(&s->gb);
            break;
        case 4:
            s->use_skip_mb_code = get_bits1(&s->gb);
1349

1350 1351
            if(s->bit_rate > MBAC_BITRATE) s->per_mb_rl_table= get_bits1(&s->gb);
            else                           s->per_mb_rl_table= 0;
1352

Michael Niedermayer's avatar
Michael Niedermayer committed
1353 1354 1355 1356 1357 1358 1359
            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);

1360
            s->mv_table_index = get_bits1(&s->gb);
1361
            s->inter_intra_pred= (s->width*s->height < 320*240 && s->bit_rate<=II_BITRATE);
Michael Niedermayer's avatar
Michael Niedermayer committed
1362
            break;
1363
        }
Michael Niedermayer's avatar
Michael Niedermayer committed
1364 1365
        
        if(s->avctx->debug&FF_DEBUG_PICT_INFO)
1366
	    av_log(s->avctx, AV_LOG_DEBUG, "skip:%d rl:%d rlc:%d dc:%d mv:%d mbrl:%d qp:%d   \n", 
1367 1368 1369 1370
		s->use_skip_mb_code, 
		s->rl_table_index, 
		s->rl_chroma_table_index, 
		s->dc_table_index,
Michael Niedermayer's avatar
Michael Niedermayer committed
1371
		s->mv_table_index,
1372
                s->per_mb_rl_table,
Michael Niedermayer's avatar
Michael Niedermayer committed
1373 1374
                s->qscale);

1375 1376 1377 1378 1379
	if(s->flipflop_rounding){
	    s->no_rounding ^= 1;
	}else{
	    s->no_rounding = 0;
	}
Fabrice Bellard's avatar
Fabrice Bellard committed
1380
    }
Michael Niedermayer's avatar
Michael Niedermayer committed
1381
//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
1382 1383 1384

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

Fabrice Bellard's avatar
Fabrice Bellard committed
1386 1387 1388 1389 1390 1391
#ifdef DEBUG
    printf("*****frame %d:\n", frame_count++);
#endif
    return 0;
}

1392 1393
int msmpeg4_decode_ext_header(MpegEncContext * s, int buf_size)
{
Michael Niedermayer's avatar
Michael Niedermayer committed
1394 1395
    int left= buf_size*8 - get_bits_count(&s->gb);
    int length= s->msmpeg4_version>=3 ? 17 : 16;
1396
    /* the alt_bitstream reader could read over the end so we need to check it */
Michael Niedermayer's avatar
Michael Niedermayer committed
1397
    if(left>=length && left<length+8)
1398
    {
1399 1400 1401
        int fps;

        fps= get_bits(&s->gb, 5);
1402
        s->bit_rate= get_bits(&s->gb, 11)*1024;
Michael Niedermayer's avatar
Michael Niedermayer committed
1403 1404 1405 1406
        if(s->msmpeg4_version>=3)
            s->flipflop_rounding= get_bits1(&s->gb);
        else
            s->flipflop_rounding= 0;
1407

1408
//        printf("fps:%2d bps:%2d roundingType:%1d\n", fps, s->bit_rate/1024, s->flipflop_rounding);
1409
    }
Michael Niedermayer's avatar
Michael Niedermayer committed
1410
    else if(left<length+8)
1411
    {
1412
        s->flipflop_rounding= 0;
1413
        if(s->msmpeg4_version != 2)
1414
            av_log(s->avctx, AV_LOG_ERROR, "ext header missing, %d left\n", left);
Michael Niedermayer's avatar
Michael Niedermayer committed
1415 1416 1417
    }
    else
    {
1418
        av_log(s->avctx, AV_LOG_ERROR, "I frame too long, ignoring ext header\n");
1419
    }
1420

1421 1422 1423
    return 0;
}

1424
static inline void msmpeg4_memsetw(short *tab, int val, int n)
Fabrice Bellard's avatar
Fabrice Bellard committed
1425 1426 1427 1428 1429 1430
{
    int i;
    for(i=0;i<n;i++)
        tab[i] = val;
}

Michael Niedermayer's avatar
Michael Niedermayer committed
1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463
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);

        put_bits(&s->pb, mvtab[code][1] + 1, (mvtab[code][0] << 1) | sign); 
        if (bit_size > 0) {
            put_bits(&s->pb, bit_size, bits);
        }
    }
}

1464 1465 1466 1467 1468
/* this is identical to h263 except that its range is multiplied by 2 */
static int msmpeg4v2_decode_motion(MpegEncContext * s, int pred, int f_code)
{
    int code, val, sign, shift;

1469
    code = get_vlc2(&s->gb, v2_mv_vlc.table, V2_MV_VLC_BITS, 2);
Michael Niedermayer's avatar
Michael Niedermayer committed
1470
//     printf("MV code %d at %d %d pred: %d\n", code, s->mb_x,s->mb_y, pred);
1471 1472 1473 1474 1475 1476 1477
    if (code < 0)
        return 0xffff;

    if (code == 0)
        return pred;
    sign = get_bits1(&s->gb);
    shift = f_code - 1;
1478 1479 1480
    val = code;
    if (shift) {
        val = (val - 1) << shift;
1481
        val |= get_bits(&s->gb, shift);
1482 1483
        val++;
    }
1484 1485 1486
    if (sign)
        val = -val;

Michael Niedermayer's avatar
Michael Niedermayer committed
1487
    val += pred;
1488 1489 1490 1491 1492 1493 1494 1495
    if (val <= -64)
        val += 64;
    else if (val >= 64)
        val -= 64;

    return val;
}

1496
static int msmpeg4v12_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
1497 1498
{
    int cbp, code, i;
1499
    
1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515
    if (s->pict_type == P_TYPE) {
        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;
                s->mb_skiped = 1;
                return 0;
            }
        }

Michael Niedermayer's avatar
Michael Niedermayer committed
1516
        if(s->msmpeg4_version==2)
1517
            code = get_vlc2(&s->gb, v2_mb_type_vlc.table, V2_MB_TYPE_VLC_BITS, 1);
Michael Niedermayer's avatar
Michael Niedermayer committed
1518
        else
1519
            code = get_vlc2(&s->gb, v1_inter_cbpc_vlc.table, V1_INTER_CBPC_VLC_BITS, 3);
Michael Niedermayer's avatar
Michael Niedermayer committed
1520
        if(code<0 || code>7){
1521
            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
1522 1523 1524
            return -1;
        }

1525 1526 1527 1528 1529
        s->mb_intra = code >>2;
    
        cbp = code & 0x3;
    } else {
        s->mb_intra = 1;
Michael Niedermayer's avatar
Michael Niedermayer committed
1530
        if(s->msmpeg4_version==2)
1531
            cbp= get_vlc2(&s->gb, v2_intra_cbpc_vlc.table, V2_INTRA_CBPC_VLC_BITS, 1);
Michael Niedermayer's avatar
Michael Niedermayer committed
1532
        else
1533
            cbp= get_vlc2(&s->gb, v1_intra_cbpc_vlc.table, V1_INTRA_CBPC_VLC_BITS, 1);
Michael Niedermayer's avatar
Michael Niedermayer committed
1534
        if(cbp<0 || cbp>3){
1535
            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
1536 1537
            return -1;
        }
1538 1539 1540
    }

    if (!s->mb_intra) {
Michael Niedermayer's avatar
Michael Niedermayer committed
1541 1542
        int mx, my, cbpy;
        
1543
        cbpy= get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1);
Michael Niedermayer's avatar
Michael Niedermayer committed
1544
        if(cbpy<0){
1545
            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
1546 1547
            return -1;
        }
1548

Michael Niedermayer's avatar
Michael Niedermayer committed
1549 1550
        cbp|= cbpy<<2;
        if(s->msmpeg4_version==1 || (cbp&3) != 3) cbp^= 0x3C;
1551
        
1552
        h263_pred_motion(s, 0, 0, &mx, &my);
1553 1554 1555 1556 1557 1558 1559 1560
        mx= msmpeg4v2_decode_motion(s, mx, 1);
        my= msmpeg4v2_decode_motion(s, my, 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;
    } else {
Michael Niedermayer's avatar
Michael Niedermayer committed
1561 1562
        if(s->msmpeg4_version==2){
            s->ac_pred = get_bits1(&s->gb);
1563
            cbp|= get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1)<<2; //FIXME check errors
Michael Niedermayer's avatar
Michael Niedermayer committed
1564 1565
        } else{
            s->ac_pred = 0;
1566
            cbp|= get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1)<<2; //FIXME check errors
Michael Niedermayer's avatar
Michael Niedermayer committed
1567 1568
            if(s->pict_type==P_TYPE) cbp^=0x3C;
        }
1569 1570 1571
    }

    for (i = 0; i < 6; i++) {
Michael Niedermayer's avatar
Michael Niedermayer committed
1572
        if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, NULL) < 0)
1573
	{
1574
             av_log(s->avctx, AV_LOG_ERROR, "\nerror while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
1575 1576 1577 1578 1579 1580
             return -1;
	}
    }
    return 0;
}

1581
static int msmpeg4v34_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
Fabrice Bellard's avatar
Fabrice Bellard committed
1582 1583
{
    int cbp, code, i;
1584
    uint8_t *coded_val;
1585
    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
1586 1587 1588 1589

    if (s->pict_type == P_TYPE) {
        set_stat(ST_INTER_MB);
        if (s->use_skip_mb_code) {
1590
            if (get_bits1(&s->gb)) {
Fabrice Bellard's avatar
Fabrice Bellard committed
1591 1592 1593 1594 1595 1596 1597 1598
                /* 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;
1599
                s->mb_skiped = 1;
1600 1601
                *mb_type_ptr = MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;

Fabrice Bellard's avatar
Fabrice Bellard committed
1602 1603 1604 1605
                return 0;
            }
        }
        
Michael Niedermayer's avatar
Michael Niedermayer committed
1606
        code = get_vlc2(&s->gb, mb_non_intra_vlc[DEFAULT_INTER_INDEX].table, MB_NON_INTRA_VLC_BITS, 3);
Fabrice Bellard's avatar
Fabrice Bellard committed
1607 1608
        if (code < 0)
            return -1;
Zdenek Kabelac's avatar
Zdenek Kabelac committed
1609 1610
	//s->mb_intra = (code & 0x40) ? 0 : 1;
	s->mb_intra = (~code & 0x40) >> 6;
Fabrice Bellard's avatar
Fabrice Bellard committed
1611 1612 1613 1614 1615
            
        cbp = code & 0x3f;
    } else {
        set_stat(ST_INTRA_MB);
        s->mb_intra = 1;
1616
        code = get_vlc2(&s->gb, mb_intra_vlc.table, MB_INTRA_VLC_BITS, 2);
Fabrice Bellard's avatar
Fabrice Bellard committed
1617 1618 1619 1620 1621
        if (code < 0)
            return -1;
        /* predict coded block pattern */
        cbp = 0;
        for(i=0;i<6;i++) {
Zdenek Kabelac's avatar
Zdenek Kabelac committed
1622
            int val = ((code >> (5 - i)) & 1);
Fabrice Bellard's avatar
Fabrice Bellard committed
1623
            if (i < 4) {
Zdenek Kabelac's avatar
Zdenek Kabelac committed
1624
                int pred = coded_block_pred(s, i, &coded_val);
Fabrice Bellard's avatar
Fabrice Bellard committed
1625 1626 1627 1628 1629 1630 1631 1632 1633
                val = val ^ pred;
                *coded_val = val;
            }
            cbp |= val << (5 - i);
        }
    }

    if (!s->mb_intra) {
        int mx, my;
Michael Niedermayer's avatar
Michael Niedermayer committed
1634 1635 1636 1637 1638
//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;
        }
Fabrice Bellard's avatar
Fabrice Bellard committed
1639
        set_stat(ST_MV);
1640
        h263_pred_motion(s, 0, 0, &mx, &my);
Fabrice Bellard's avatar
Fabrice Bellard committed
1641 1642 1643 1644 1645 1646
        if (msmpeg4_decode_motion(s, &mx, &my) < 0)
            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;
1647
        *mb_type_ptr = MB_TYPE_L0 | MB_TYPE_16x16;
Fabrice Bellard's avatar
Fabrice Bellard committed
1648
    } else {
Michael Niedermayer's avatar
Michael Niedermayer committed
1649
//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));
Fabrice Bellard's avatar
Fabrice Bellard committed
1650
        set_stat(ST_INTRA_MB);
1651
        s->ac_pred = get_bits1(&s->gb);
1652
        *mb_type_ptr = MB_TYPE_INTRA;
1653
        if(s->inter_intra_pred){
1654
            s->h263_aic_dir= get_vlc2(&s->gb, inter_intra_vlc.table, INTER_INTRA_VLC_BITS, 1);
1655 1656
//            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
1657 1658 1659 1660
        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
1661 1662 1663
    }

    for (i = 0; i < 6; i++) {
Michael Niedermayer's avatar
Michael Niedermayer committed
1664
        if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, NULL) < 0)
Zdenek Kabelac's avatar
Zdenek Kabelac committed
1665
	{
1666
	    av_log(s->avctx, AV_LOG_ERROR, "\nerror while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
Michael Niedermayer's avatar
Michael Niedermayer committed
1667
	    return -1;
Zdenek Kabelac's avatar
Zdenek Kabelac committed
1668
	}
Fabrice Bellard's avatar
Fabrice Bellard committed
1669
    }
1670
    
Fabrice Bellard's avatar
Fabrice Bellard committed
1671 1672
    return 0;
}
1673
//#define ERROR_DETAILS
Michael Niedermayer's avatar
Michael Niedermayer committed
1674
static inline int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
Michael Niedermayer's avatar
Michael Niedermayer committed
1675
                              int n, int coded, const uint8_t *scan_table)
Fabrice Bellard's avatar
Fabrice Bellard committed
1676
{
1677
    int level, i, last, run, run_diff;
Fabrice Bellard's avatar
Fabrice Bellard committed
1678 1679
    int dc_pred_dir;
    RLTable *rl;
1680
    RL_VLC_ELEM *rl_vlc;
1681
    int qmul, qadd;
Fabrice Bellard's avatar
Fabrice Bellard committed
1682 1683

    if (s->mb_intra) {
1684 1685 1686
        qmul=1;
        qadd=0;

Fabrice Bellard's avatar
Fabrice Bellard committed
1687 1688 1689
	/* DC coef */
        set_stat(ST_DC);
        level = msmpeg4_decode_dc(s, n, &dc_pred_dir);
1690
        
Michael Niedermayer's avatar
Michael Niedermayer committed
1691
        if (level < 0){
1692
            av_log(s->avctx, AV_LOG_ERROR, "dc overflow- block: %d qscale: %d//\n", n, s->qscale);
1693 1694
            if(s->inter_intra_pred) level=0;
            else                    return -1;
Michael Niedermayer's avatar
Michael Niedermayer committed
1695
        }
Fabrice Bellard's avatar
Fabrice Bellard committed
1696 1697
        if (n < 4) {
            rl = &rl_table[s->rl_table_index];
Michael Niedermayer's avatar
Michael Niedermayer committed
1698
            if(level > 256*s->y_dc_scale){
1699
                av_log(s->avctx, AV_LOG_ERROR, "dc overflow+ L qscale: %d//\n", s->qscale);
1700
                if(!s->inter_intra_pred) return -1;
Michael Niedermayer's avatar
Michael Niedermayer committed
1701
            }
Fabrice Bellard's avatar
Fabrice Bellard committed
1702 1703
        } else {
            rl = &rl_table[3 + s->rl_chroma_table_index];
Michael Niedermayer's avatar
Michael Niedermayer committed
1704
            if(level > 256*s->c_dc_scale){
1705
                av_log(s->avctx, AV_LOG_ERROR, "dc overflow+ C qscale: %d//\n", s->qscale);
1706
                if(!s->inter_intra_pred) return -1;
Michael Niedermayer's avatar
Michael Niedermayer committed
1707
            }
Fabrice Bellard's avatar
Fabrice Bellard committed
1708
        }
Michael Niedermayer's avatar
Michael Niedermayer committed
1709
        block[0] = level;
1710

Fabrice Bellard's avatar
Fabrice Bellard committed
1711
        run_diff = 0;
1712
        i = 0;
Fabrice Bellard's avatar
Fabrice Bellard committed
1713 1714 1715 1716 1717
        if (!coded) {
            goto not_coded;
        }
        if (s->ac_pred) {
            if (dc_pred_dir == 0) 
1718
                scan_table = s->intra_v_scantable.permutated; /* left */
Fabrice Bellard's avatar
Fabrice Bellard committed
1719
            else
1720
                scan_table = s->intra_h_scantable.permutated; /* top */
Fabrice Bellard's avatar
Fabrice Bellard committed
1721
        } else {
1722
            scan_table = s->intra_scantable.permutated;
Fabrice Bellard's avatar
Fabrice Bellard committed
1723 1724
        }
        set_stat(ST_INTRA_AC);
1725
        rl_vlc= rl->rl_vlc[0];
Fabrice Bellard's avatar
Fabrice Bellard committed
1726
    } else {
1727 1728
        qmul = s->qscale << 1;
        qadd = (s->qscale - 1) | 1;
1729
        i = -1;
Fabrice Bellard's avatar
Fabrice Bellard committed
1730
        rl = &rl_table[3 + s->rl_table_index];
1731 1732 1733 1734 1735 1736

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

Fabrice Bellard's avatar
Fabrice Bellard committed
1737
        if (!coded) {
1738
            s->block_last_index[n] = i;
Fabrice Bellard's avatar
Fabrice Bellard committed
1739 1740
            return 0;
        }
Michael Niedermayer's avatar
Michael Niedermayer committed
1741 1742
        if(!scan_table)
            scan_table = s->inter_scantable.permutated;
Fabrice Bellard's avatar
Fabrice Bellard committed
1743
        set_stat(ST_INTER_AC);
1744
        rl_vlc= rl->rl_vlc[s->qscale];
Fabrice Bellard's avatar
Fabrice Bellard committed
1745
    }
1746 1747
  {
    OPEN_READER(re, &s->gb);
Fabrice Bellard's avatar
Fabrice Bellard committed
1748
    for(;;) {
1749 1750 1751 1752 1753
        UPDATE_CACHE(re, &s->gb);
        GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2);
        if (level==0) {
            int cache;
            cache= GET_CACHE(re, &s->gb);
Fabrice Bellard's avatar
Fabrice Bellard committed
1754
            /* escape */
1755 1756
            if (s->msmpeg4_version==1 || (cache&0x80000000)==0) {
                if (s->msmpeg4_version==1 || (cache&0x40000000)==0) {
Fabrice Bellard's avatar
Fabrice Bellard committed
1757
                    /* third escape */
1758 1759
                    if(s->msmpeg4_version!=1) LAST_SKIP_BITS(re, &s->gb, 2);
                    UPDATE_CACHE(re, &s->gb);
Michael Niedermayer's avatar
Michael Niedermayer committed
1760
                    if(s->msmpeg4_version<=3){
1761 1762 1763 1764 1765
                        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);
                    }else{                        
Michael Niedermayer's avatar
Michael Niedermayer committed
1766
                        int sign;
1767
                        last=  SHOW_UBITS(re, &s->gb, 1); SKIP_BITS(re, &s->gb, 1);
Michael Niedermayer's avatar
Michael Niedermayer committed
1768 1769 1770 1771
                        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){
1772
                                ll= SHOW_UBITS(re, &s->gb, 3); SKIP_BITS(re, &s->gb, 3);
Michael Niedermayer's avatar
Michael Niedermayer committed
1773
                                if(ll==0){
1774
                                    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");
1775
                                    SKIP_BITS(re, &s->gb, 1);
Michael Niedermayer's avatar
Michael Niedermayer committed
1776 1777 1778 1779
                                    ll=8;
                                }
                            }else{
                                ll=2;
1780 1781 1782 1783
                                while(ll<8 && SHOW_UBITS(re, &s->gb, 1)==0){
                                    ll++;
                                    SKIP_BITS(re, &s->gb, 1);
                                }
1784
                                if(ll<8) SKIP_BITS(re, &s->gb, 1);
Michael Niedermayer's avatar
Michael Niedermayer committed
1785 1786 1787
                            }

                            s->esc3_level_length= ll;
1788
                            s->esc3_run_length= SHOW_UBITS(re, &s->gb, 2) + 3; SKIP_BITS(re, &s->gb, 2);
Michael Niedermayer's avatar
Michael Niedermayer committed
1789
//printf("level length:%d, run length: %d\n", ll, s->esc3_run_length);
1790
                            UPDATE_CACHE(re, &s->gb);
Michael Niedermayer's avatar
Michael Niedermayer committed
1791
                        }
1792 1793 1794 1795 1796 1797 1798 1799
                        run=   SHOW_UBITS(re, &s->gb, s->esc3_run_length); 
                        SKIP_BITS(re, &s->gb, s->esc3_run_length);
                        
                        sign=  SHOW_UBITS(re, &s->gb, 1); 
                        SKIP_BITS(re, &s->gb, 1);
                        
                        level= SHOW_UBITS(re, &s->gb, s->esc3_level_length); 
                        SKIP_BITS(re, &s->gb, s->esc3_level_length);
Michael Niedermayer's avatar
Michael Niedermayer committed
1800 1801 1802
                        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
1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815
#if 0 // waste of time / this will detect very few errors
                    {
                        const int abs_level= ABS(level);
                        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]){
                                fprintf(stderr, "illegal 3. esc, vlc encoding possible\n");
                                return DECODING_AC_LOST;
                            }
                            if(abs_level <= rl->max_level[last][run]*2){
                                fprintf(stderr, "illegal 3. esc, esc 1 encoding possible\n");
                                return DECODING_AC_LOST;
                            }
Michael Niedermayer's avatar
Michael Niedermayer committed
1816
                            if(run1>=0 && abs_level <= rl->max_level[last][run1]){
Michael Niedermayer's avatar
Michael Niedermayer committed
1817 1818 1819 1820 1821 1822
                                fprintf(stderr, "illegal 3. esc, esc 2 encoding possible\n");
                                return DECODING_AC_LOST;
                            }
                        }
                    }
#endif
Zdenek Kabelac's avatar
Zdenek Kabelac committed
1823 1824
		    //level = level * qmul + (level>0) * qadd - (level<=0) * qadd ;
		    if (level>0) level= level * qmul + qadd;
Michael Niedermayer's avatar
Michael Niedermayer committed
1825 1826 1827 1828 1829 1830 1831
                    else         level= level * qmul - qadd;
#if 0 // waste of time too :(
                    if(level>2048 || level<-2048){
                        fprintf(stderr, "|level| overflow in 3. esc\n");
                        return DECODING_AC_LOST;
                    }
#endif
1832 1833
                    i+= run + 1;
                    if(last) i+=192;
1834 1835 1836 1837 1838 1839
#ifdef ERROR_DETAILS
                if(run==66)
                    fprintf(stderr, "illegal vlc code in ESC3 level=%d\n", level);
                else if((i>62 && i<192) || i>192+63)
                    fprintf(stderr, "run overflow in ESC3 i=%d run=%d level=%d\n", i, run, level);
#endif
Fabrice Bellard's avatar
Fabrice Bellard committed
1840 1841
                } else {
                    /* second escape */
1842 1843 1844 1845 1846 1847 1848 1849 1850 1851
#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
                    GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2);
                    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);
1852 1853 1854 1855 1856 1857
#ifdef ERROR_DETAILS
                if(run==66)
                    fprintf(stderr, "illegal vlc code in ESC2 level=%d\n", level);
                else if((i>62 && i<192) || i>192+63)
                    fprintf(stderr, "run overflow in ESC2 i=%d run=%d level=%d\n", i, run, level);
#endif
Fabrice Bellard's avatar
Fabrice Bellard committed
1858 1859 1860
                }
            } else {
                /* first escape */
1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871
#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
                GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2);
                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);
1872 1873 1874 1875 1876 1877
#ifdef ERROR_DETAILS
                if(run==66)
                    fprintf(stderr, "illegal vlc code in ESC1 level=%d\n", level);
                else if((i>62 && i<192) || i>192+63)
                    fprintf(stderr, "run overflow in ESC1 i=%d run=%d level=%d\n", i, run, level);
#endif
Fabrice Bellard's avatar
Fabrice Bellard committed
1878 1879
            }
        } else {
1880 1881 1882
            i+= run;
            level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
            LAST_SKIP_BITS(re, &s->gb, 1);
1883 1884 1885 1886 1887 1888
#ifdef ERROR_DETAILS
                if(run==66)
                    fprintf(stderr, "illegal vlc code level=%d\n", level);
                else if((i>62 && i<192) || i>192+63)
                    fprintf(stderr, "run overflow i=%d run=%d level=%d\n", i, run, level);
#endif
Michael Niedermayer's avatar
Michael Niedermayer committed
1889
        }
1890 1891 1892
        if (i > 62){
            i-= 192;
            if(i&(~63)){
1893
                const int left= s->gb.size_in_bits - get_bits_count(&s->gb);
1894
                if(((i+192 == 64 && level/qmul==-1) || s->error_resilience<=1) && left>=0){
1895
                    av_log(s->avctx, AV_LOG_ERROR, "ignoring overflow at %d %d\n", s->mb_x, s->mb_y);
1896 1897
                    break;
                }else{
1898
                    av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
1899 1900
                    return -1;
                }
1901
            }
Michael Niedermayer's avatar
Michael Niedermayer committed
1902

1903
            block[scan_table[i]] = level;
Fabrice Bellard's avatar
Fabrice Bellard committed
1904
            break;
1905 1906 1907
        }

        block[scan_table[i]] = level;
Fabrice Bellard's avatar
Fabrice Bellard committed
1908
    }
1909 1910
    CLOSE_READER(re, &s->gb);
  }
Fabrice Bellard's avatar
Fabrice Bellard committed
1911 1912 1913 1914
 not_coded:
    if (s->mb_intra) {
        mpeg4_pred_ac(s, block, n, dc_pred_dir);
        if (s->ac_pred) {
1915
            i = 63; /* XXX: not optimal */
Fabrice Bellard's avatar
Fabrice Bellard committed
1916 1917
        }
    }
Michael Niedermayer's avatar
Michael Niedermayer committed
1918
    if(s->msmpeg4_version>=4 && i>0) i=63; //FIXME/XXX optimize
1919
    s->block_last_index[n] = i;
Michael Niedermayer's avatar
Michael Niedermayer committed
1920
    
Fabrice Bellard's avatar
Fabrice Bellard committed
1921 1922 1923 1924 1925 1926 1927
    return 0;
}

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

Michael Niedermayer's avatar
Michael Niedermayer committed
1928
    if(s->msmpeg4_version<=2){
1929
        if (n < 4) {
1930
            level = get_vlc2(&s->gb, v2_dc_lum_vlc.table, DC_VLC_BITS, 3);
1931
        } else {
1932
            level = get_vlc2(&s->gb, v2_dc_chroma_vlc.table, DC_VLC_BITS, 3);
1933
        }
1934
        if (level < 0) 
1935 1936 1937 1938
            return -1;
        level-=256;
    }else{  //FIXME optimize use unified tables & index
        if (n < 4) {
1939
            level = get_vlc2(&s->gb, dc_lum_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
1940
        } else {
1941
            level = get_vlc2(&s->gb, dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
1942
        }
Michael Niedermayer's avatar
Michael Niedermayer committed
1943
        if (level < 0){
1944
            av_log(s->avctx, AV_LOG_ERROR, "illegal dc vlc\n");
1945
            return -1;
Michael Niedermayer's avatar
Michael Niedermayer committed
1946
        }
1947 1948 1949 1950 1951 1952 1953 1954 1955

        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
1956 1957
    }

Michael Niedermayer's avatar
Michael Niedermayer committed
1958
    if(s->msmpeg4_version==1){
1959
        int32_t *dc_val;
Michael Niedermayer's avatar
Michael Niedermayer committed
1960 1961 1962 1963 1964 1965
        pred = msmpeg4v1_pred_dc(s, n, &dc_val);
        level += pred;
        
        /* update predictor */
        *dc_val= level;
    }else{
1966
        uint16_t *dc_val;
1967
        pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr);
Michael Niedermayer's avatar
Michael Niedermayer committed
1968
        level += pred;
Fabrice Bellard's avatar
Fabrice Bellard committed
1969

Michael Niedermayer's avatar
Michael Niedermayer committed
1970 1971 1972 1973 1974 1975
        /* 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
1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988
    }

    return level;
}

static int msmpeg4_decode_motion(MpegEncContext * s, 
                                 int *mx_ptr, int *my_ptr)
{
    MVTable *mv;
    int code, mx, my;

    mv = &mv_tables[s->mv_table_index];

1989
    code = get_vlc2(&s->gb, mv->vlc.table, MV_VLC_BITS, 2);
Michael Niedermayer's avatar
Michael Niedermayer committed
1990
    if (code < 0){
1991
        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
1992
        return -1;
Michael Niedermayer's avatar
Michael Niedermayer committed
1993
    }
Fabrice Bellard's avatar
Fabrice Bellard committed
1994
    if (code == mv->n) {
Michael Niedermayer's avatar
Michael Niedermayer committed
1995
//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
1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018
        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;
}
Michael Niedermayer's avatar
Michael Niedermayer committed
2019 2020 2021 2022 2023 2024

/* cleanest way to support it
 * there is too much shared between versions so that we cant have 1 file per version & 1 common
 * as allmost everything would be in the common file 
 */
#include "wmv2.c"