Statistics
| Branch: | Revision:

ffmpeg / libavcodec / msmpeg4.c @ 7604246d

History | View | Annotate | Download (60 KB)

1
/*
2
 * MSMPEG4 backend for ffmpeg encoder and decoder
3
 * Copyright (c) 2001 Fabrice Bellard.
4
 *
5
 * This library is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU Lesser General Public
7
 * License as published by the Free Software Foundation; either
8
 * version 2 of the License, or (at your option) any later version.
9
 *
10
 * This library is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General Public
16
 * License along with this library; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
 *
19
 * msmpeg4v1 & v2 stuff by Michael Niedermayer <michaelni@gmx.at>
20
 */
21
#include "avcodec.h"
22
#include "dsputil.h"
23
#include "mpegvideo.h"
24
//#define PRINT_MB
25

    
26
/*
27
 * You can also call this codec : MPEG4 with a twist ! 
28
 *
29
 * TODO: 
30
 *        - (encoding) select best mv table (two choices)
31
 *        - (encoding) select best vlc/dc table 
32
 */
33
//#define DEBUG
34

    
35
#define DC_VLC_BITS 9
36
#define CBPY_VLC_BITS 6
37
#define INTER_INTRA_VLC_BITS 3
38
#define V1_INTRA_CBPC_VLC_BITS 6
39
#define V1_INTER_CBPC_VLC_BITS 6
40
#define V2_INTRA_CBPC_VLC_BITS 3
41
#define V2_MB_TYPE_VLC_BITS 7
42
#define MV_VLC_BITS 9
43
#define V2_MV_VLC_BITS 9
44
#define TEX_VLC_BITS 9
45
#define MB_NON_INTRA_VLC_BITS 9
46
#define MB_INTRA_VLC_BITS 9
47

    
48
#define II_BITRATE 128*1024
49
#define MBAC_BITRATE 50*1024
50

    
51
#define DEFAULT_INTER_INDEX 3
52

    
53
static uint32_t v2_dc_lum_table[512][2];
54
static uint32_t v2_dc_chroma_table[512][2];
55

    
56
#ifdef CONFIG_ENCODERS
57
static inline void msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n);
58
#endif //CONFIG_ENCODERS
59
static inline int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
60
                                       int n, int coded, const uint8_t *scantable);
61
static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr);
62
static int msmpeg4_decode_motion(MpegEncContext * s, 
63
                                 int *mx_ptr, int *my_ptr);
64
#ifdef CONFIG_ENCODERS
65
static void msmpeg4v2_encode_motion(MpegEncContext * s, int val);
66
#endif //CONFIG_ENCODERS
67
static void init_h263_dc_for_msmpeg4(void);
68
static inline void msmpeg4_memsetw(short *tab, int val, int n);
69
#ifdef CONFIG_ENCODERS
70
static int get_size_of_code(MpegEncContext * s, RLTable *rl, int last, int run, int level, int intra);
71
#endif //CONFIG_ENCODERS
72
static int msmpeg4v12_decode_mb(MpegEncContext *s, DCTELEM block[6][64]);
73
static int msmpeg4v34_decode_mb(MpegEncContext *s, DCTELEM block[6][64]);
74
static int wmv2_decode_mb(MpegEncContext *s, DCTELEM block[6][64]);
75

    
76
extern uint32_t inverse[256];
77

    
78

    
79
#ifdef DEBUG
80
int intra_count = 0;
81
int frame_count = 0;
82
#endif
83

    
84
#include "msmpeg4data.h"
85

    
86
static uint8_t rl_length[NB_RL_TABLES][MAX_LEVEL+1][MAX_RUN+1][2];
87

    
88
#ifdef STATS
89

    
90
const char *st_names[ST_NB] = {
91
    "unknown",
92
    "dc",
93
    "intra_ac",
94
    "inter_ac",
95
    "intra_mb",
96
    "inter_mb",
97
    "mv",
98
};
99

    
100
int st_current_index = 0;
101
unsigned int st_bit_counts[ST_NB];
102
unsigned int st_out_bit_counts[ST_NB];
103

    
104
#define set_stat(var) st_current_index = var;
105

    
106
void print_stats(void)
107
{
108
    unsigned int total;
109
    int i;
110

    
111
    printf("Input:\n");
112
    total = 0;
113
    for(i=0;i<ST_NB;i++)
114
        total += st_bit_counts[i];
115
    if (total == 0)
116
        total = 1;
117
    for(i=0;i<ST_NB;i++) {
118
        printf("%-10s : %10.1f %5.1f%%\n", 
119
               st_names[i], 
120
               (double)st_bit_counts[i] / 8.0, 
121
               (double)st_bit_counts[i] * 100.0 / total);
122
    }
123
    printf("%-10s : %10.1f %5.1f%%\n",
124
           "total", 
125
           (double)total / 8.0, 
126
           100.0);
127

    
128
    printf("Output:\n");
129
    total = 0;
130
    for(i=0;i<ST_NB;i++)
131
        total += st_out_bit_counts[i];
132
    if (total == 0)
133
        total = 1;
134
    for(i=0;i<ST_NB;i++) {
135
        printf("%-10s : %10.1f %5.1f%%\n", 
136
               st_names[i], 
137
               (double)st_out_bit_counts[i] / 8.0, 
138
               (double)st_out_bit_counts[i] * 100.0 / total);
139
    }
140
    printf("%-10s : %10.1f %5.1f%%\n",
141
           "total", 
142
           (double)total / 8.0, 
143
           100.0);
144
}
145

    
146
#else
147

    
148
#define set_stat(var)
149

    
150
#endif
151

    
152
static void common_init(MpegEncContext * s)
153
{
154
    static int inited=0;
155
    
156
    switch(s->msmpeg4_version){
157
    case 1:
158
    case 2:
159
        s->y_dc_scale_table=
160
        s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
161
        break;
162
    case 3:
163
        if(s->workaround_bugs){
164
            s->y_dc_scale_table= old_ff_y_dc_scale_table;
165
            s->c_dc_scale_table= old_ff_c_dc_scale_table;
166
        } else{
167
            s->y_dc_scale_table= ff_mpeg4_y_dc_scale_table;
168
            s->c_dc_scale_table= ff_mpeg4_c_dc_scale_table;
169
        }
170
        break;
171
    case 4:
172
    case 5:
173
        s->y_dc_scale_table= wmv1_y_dc_scale_table;
174
        s->c_dc_scale_table= wmv1_c_dc_scale_table;
175
        break;
176
    }
177

    
178
    
179
    if(s->msmpeg4_version>=4){
180
        ff_init_scantable(s, &s->intra_scantable  , wmv1_scantable[1]);
181
        ff_init_scantable(s, &s->intra_h_scantable, wmv1_scantable[2]);
182
        ff_init_scantable(s, &s->intra_v_scantable, wmv1_scantable[3]);
183
        ff_init_scantable(s, &s->inter_scantable  , wmv1_scantable[0]);
184
    }
185
    //Note the default tables are set in common_init in mpegvideo.c
186
    
187
    if(!inited){
188
        inited=1;
189

    
190
        init_h263_dc_for_msmpeg4();
191
    }
192
}
193

    
194
#ifdef CONFIG_ENCODERS
195

    
196
/* build the table which associate a (x,y) motion vector to a vlc */
197
static void init_mv_table(MVTable *tab)
198
{
199
    int i, x, y;
200

    
201
    tab->table_mv_index = av_malloc(sizeof(uint16_t) * 4096);
202
    /* mark all entries as not used */
203
    for(i=0;i<4096;i++)
204
        tab->table_mv_index[i] = tab->n;
205
    
206
    for(i=0;i<tab->n;i++) {
207
        x = tab->table_mvx[i];
208
        y = tab->table_mvy[i];
209
        tab->table_mv_index[(x << 6) | y] = i;
210
    }
211
}
212

    
213
static void code012(PutBitContext *pb, int n)
214
{
215
    if (n == 0) {
216
        put_bits(pb, 1, 0);
217
    } else {
218
        put_bits(pb, 1, 1);
219
        put_bits(pb, 1, (n >= 2));
220
    }
221
}
222

    
223
void ff_msmpeg4_encode_init(MpegEncContext *s)
224
{
225
    static int init_done=0;
226
    int i;
227

    
228
    common_init(s);
229
    if(s->msmpeg4_version>=4){
230
        s->min_qcoeff= -255;
231
        s->max_qcoeff=  255;
232
    }
233

    
234
    if (!init_done) {
235
        /* init various encoding tables */
236
        init_done = 1;
237
        init_mv_table(&mv_tables[0]);
238
        init_mv_table(&mv_tables[1]);
239
        for(i=0;i<NB_RL_TABLES;i++)
240
            init_rl(&rl_table[i]);
241

    
242
        for(i=0; i<NB_RL_TABLES; i++){
243
            int level;
244
            for(level=0; level<=MAX_LEVEL; level++){
245
                int run;
246
                for(run=0; run<=MAX_RUN; run++){
247
                    int last;
248
                    for(last=0; last<2; last++){
249
                        rl_length[i][level][run][last]= get_size_of_code(s, &rl_table[  i], last, run, level, 0);
250
                    }
251
                }
252
            }
253
        }
254
    }
255
}
256

    
257
static int get_size_of_code(MpegEncContext * s, RLTable *rl, int last, int run, int level, int intra){
258
    int size=0;
259
    int code;
260
    int run_diff= intra ? 0 : 1;
261
    
262
    code = get_rl_index(rl, last, run, level);
263
    size+= rl->table_vlc[code][1];
264
    if (code == rl->n) {
265
        int level1, run1;
266

    
267
        level1 = level - rl->max_level[last][run];
268
        if (level1 < 1) 
269
            goto esc2;
270
        code = get_rl_index(rl, last, run, level1);
271
        if (code == rl->n) {
272
            esc2:
273
            size++;
274
            if (level > MAX_LEVEL)
275
                goto esc3;
276
            run1 = run - rl->max_run[last][level] - run_diff;
277
            if (run1 < 0)
278
                goto esc3;
279
            code = get_rl_index(rl, last, run1, level);
280
            if (code == rl->n) {
281
            esc3:
282
                /* third escape */
283
                size+=1+1+6+8;
284
            } else {
285
                /* second escape */
286
                size+= 1+1+ rl->table_vlc[code][1];
287
            }
288
        } else {
289
            /* first escape */
290
            size+= 1+1+ rl->table_vlc[code][1];
291
        }
292
    } else {
293
        size++;
294
    }
295
    return size;
296
}
297

    
298
static void find_best_tables(MpegEncContext * s)
299
{
300
    int i;
301
    int best       =-1, best_size       =9999999;
302
    int chroma_best=-1, best_chroma_size=9999999;
303

    
304
    for(i=0; i<3; i++){
305
        int level;
306
        int chroma_size=0;
307
        int size=0;
308

    
309
        if(i>0){// ;)
310
            size++; 
311
            chroma_size++;
312
        }
313
        for(level=0; level<=MAX_LEVEL; level++){
314
            int run;
315
            for(run=0; run<=MAX_RUN; run++){
316
                int last;
317
                const int last_size= size + chroma_size;
318
                for(last=0; last<2; last++){
319
                    int inter_count       = s->ac_stats[0][0][level][run][last] + s->ac_stats[0][1][level][run][last];
320
                    int intra_luma_count  = s->ac_stats[1][0][level][run][last];
321
                    int intra_chroma_count= s->ac_stats[1][1][level][run][last];
322
                    
323
                    if(s->pict_type==I_TYPE){
324
                        size       += intra_luma_count  *rl_length[i  ][level][run][last];
325
                        chroma_size+= intra_chroma_count*rl_length[i+3][level][run][last];
326
                    }else{
327
                        size+=        intra_luma_count  *rl_length[i  ][level][run][last]
328
                                     +intra_chroma_count*rl_length[i+3][level][run][last]
329
                                     +inter_count       *rl_length[i+3][level][run][last];
330
                    }                   
331
                }
332
                if(last_size == size+chroma_size) break;
333
            }
334
        }
335
        if(size<best_size){
336
            best_size= size;
337
            best= i;
338
        }
339
        if(chroma_size<best_chroma_size){
340
            best_chroma_size= chroma_size;
341
            chroma_best= i;
342
        }
343
    }
344

    
345
//    printf("type:%d, best:%d, qp:%d, var:%d, mcvar:%d, size:%d //\n", 
346
//           s->pict_type, best, s->qscale, s->mb_var_sum, s->mc_mb_var_sum, best_size);
347
           
348
    if(s->pict_type==P_TYPE) chroma_best= best;
349

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

    
352
    s->rl_table_index       =        best;
353
    s->rl_chroma_table_index= chroma_best;
354
    
355
    if(s->pict_type != s->last_non_b_pict_type){
356
        s->rl_table_index= 2;
357
        if(s->pict_type==I_TYPE)
358
            s->rl_chroma_table_index= 1;
359
        else
360
            s->rl_chroma_table_index= 2;
361
    }
362

    
363
}
364

    
365
/* write MSMPEG4 compatible frame header */
366
void msmpeg4_encode_picture_header(MpegEncContext * s, int picture_number)
367
{
368
    find_best_tables(s);
369

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

    
373
    put_bits(&s->pb, 5, s->qscale);
374
    if(s->msmpeg4_version<=2){
375
        s->rl_table_index = 2;
376
        s->rl_chroma_table_index = 2;
377
    }
378

    
379
    s->dc_table_index = 1;
380
    s->mv_table_index = 1; /* only if P frame */
381
    s->use_skip_mb_code = 1; /* only if P frame */
382
    s->per_mb_rl_table = 0;
383
    if(s->msmpeg4_version==4)
384
        s->inter_intra_pred= (s->width*s->height < 320*240 && s->bit_rate<=II_BITRATE && s->pict_type==P_TYPE);
385
//printf("%d %d %d %d %d\n", s->pict_type, s->bit_rate, s->inter_intra_pred, s->width, s->height);
386

    
387
    if (s->pict_type == I_TYPE) {
388
        s->slice_height= s->mb_height/1;
389
        put_bits(&s->pb, 5, 0x16 + s->mb_height/s->slice_height);
390
        
391
        if(s->msmpeg4_version==4){
392
            msmpeg4_encode_ext_header(s);
393
            if(s->bit_rate>MBAC_BITRATE)
394
                put_bits(&s->pb, 1, s->per_mb_rl_table);
395
        }
396

    
397
        if(s->msmpeg4_version>2){
398
            if(!s->per_mb_rl_table){
399
                code012(&s->pb, s->rl_chroma_table_index);
400
                code012(&s->pb, s->rl_table_index);
401
            }
402

    
403
            put_bits(&s->pb, 1, s->dc_table_index);
404
        }
405
    } else {
406
        put_bits(&s->pb, 1, s->use_skip_mb_code);
407
        
408
        if(s->msmpeg4_version==4 && s->bit_rate>MBAC_BITRATE)
409
            put_bits(&s->pb, 1, s->per_mb_rl_table);
410

    
411
        if(s->msmpeg4_version>2){
412
            if(!s->per_mb_rl_table)
413
                code012(&s->pb, s->rl_table_index);
414

    
415
            put_bits(&s->pb, 1, s->dc_table_index);
416

    
417
            put_bits(&s->pb, 1, s->mv_table_index);
418
        }
419
    }
420

    
421
    s->esc3_level_length= 0;
422
    s->esc3_run_length= 0;
423

    
424
#ifdef DEBUG
425
    intra_count = 0;
426
    printf("*****frame %d:\n", frame_count++);
427
#endif
428
}
429

    
430
void msmpeg4_encode_ext_header(MpegEncContext * s)
431
{
432
        put_bits(&s->pb, 5, s->frame_rate / FRAME_RATE_BASE); //yes 29.97 -> 29
433

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

    
436
        if(s->msmpeg4_version<3)
437
            s->flipflop_rounding=0;
438
        else{
439
            s->flipflop_rounding=1;
440
            put_bits(&s->pb, 1, s->flipflop_rounding);
441
        }
442
}
443

    
444
#endif //CONFIG_ENCODERS
445

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

    
451
    xy = s->block_index[n];
452
    wrap = s->block_wrap[0];
453

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

    
470
    return pred;
471
}
472

    
473
#ifdef CONFIG_ENCODERS
474

    
475
static void msmpeg4_encode_motion(MpegEncContext * s, 
476
                                  int mx, int my)
477
{
478
    int code;
479
    MVTable *mv;
480

    
481
    /* modulo encoding */
482
    /* WARNING : you cannot reach all the MVs even with the modulo
483
       encoding. This is a somewhat strange compromise they took !!!  */
484
    if (mx <= -64)
485
        mx += 64;
486
    else if (mx >= 64)
487
        mx -= 64;
488
    if (my <= -64)
489
        my += 64;
490
    else if (my >= 64)
491
        my -= 64;
492
    
493
    mx += 32;
494
    my += 32;
495
#if 0
496
    if ((unsigned)mx >= 64 ||
497
        (unsigned)my >= 64) 
498
        fprintf(stderr, "error mx=%d my=%d\n", mx, my);
499
#endif
500
    mv = &mv_tables[s->mv_table_index];
501

    
502
    code = mv->table_mv_index[(mx << 6) | my];
503
    set_stat(ST_MV);
504
    put_bits(&s->pb, 
505
             mv->table_mv_bits[code], 
506
             mv->table_mv_code[code]);
507
    if (code == mv->n) {
508
        /* escape : code litterally */
509
        put_bits(&s->pb, 6, mx);
510
        put_bits(&s->pb, 6, my);
511
    }
512
}
513

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

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

    
535
    handle_slices(s);
536
    
537
    if (!s->mb_intra) {
538
        /* compute cbp */
539
        set_stat(ST_INTER_MB);
540
        cbp = 0;
541
        for (i = 0; i < 6; i++) {
542
            if (s->block_last_index[i] >= 0)
543
                cbp |= 1 << (5 - i);
544
        }
545
        if (s->use_skip_mb_code && (cbp | motion_x | motion_y) == 0) {
546
            /* skip macroblock */
547
            put_bits(&s->pb, 1, 1);
548
            return;
549
        }
550
        if (s->use_skip_mb_code)
551
            put_bits(&s->pb, 1, 0);        /* mb coded */
552
        
553
        if(s->msmpeg4_version<=2){
554
            put_bits(&s->pb, 
555
                     v2_mb_type[cbp&3][1], 
556
                     v2_mb_type[cbp&3][0]);
557
            if((cbp&3) != 3) coded_cbp= cbp ^ 0x3C;
558
            else             coded_cbp= cbp;
559

    
560
            put_bits(&s->pb, 
561
                     cbpy_tab[coded_cbp>>2][1], 
562
                     cbpy_tab[coded_cbp>>2][0]);
563
                        
564
            h263_pred_motion(s, 0, &pred_x, &pred_y);
565
            msmpeg4v2_encode_motion(s, motion_x - pred_x);
566
            msmpeg4v2_encode_motion(s, motion_y - pred_y);
567
        }else{
568
            put_bits(&s->pb, 
569
                     table_mb_non_intra[cbp + 64][1], 
570
                     table_mb_non_intra[cbp + 64][0]);
571

    
572
            /* motion vector */
573
            h263_pred_motion(s, 0, &pred_x, &pred_y);
574
            msmpeg4_encode_motion(s, motion_x - pred_x, 
575
                                  motion_y - pred_y);
576
        }
577
    } else {
578
        /* compute cbp */
579
        cbp = 0;
580
        coded_cbp = 0;
581
        for (i = 0; i < 6; i++) {
582
            int val, pred;
583
            val = (s->block_last_index[i] >= 1);
584
            cbp |= val << (5 - i);
585
            if (i < 4) {
586
                /* predict value for close blocks only for luma */
587
                pred = coded_block_pred(s, i, &coded_block);
588
                *coded_block = val;
589
                val = val ^ pred;
590
            }
591
            coded_cbp |= val << (5 - i);
592
        }
593
#if 0
594
        if (coded_cbp)
595
            printf("cbp=%x %x\n", cbp, coded_cbp);
596
#endif
597

    
598
        if(s->msmpeg4_version<=2){
599
            if (s->pict_type == I_TYPE) {
600
                put_bits(&s->pb, 
601
                         v2_intra_cbpc[cbp&3][1], v2_intra_cbpc[cbp&3][0]);
602
            } else {
603
                if (s->use_skip_mb_code)
604
                    put_bits(&s->pb, 1, 0);        /* mb coded */
605
                put_bits(&s->pb, 
606
                         v2_mb_type[(cbp&3) + 4][1], 
607
                         v2_mb_type[(cbp&3) + 4][0]);
608
            }
609
            put_bits(&s->pb, 1, 0);        /* no AC prediction yet */
610
            put_bits(&s->pb, 
611
                     cbpy_tab[cbp>>2][1], 
612
                     cbpy_tab[cbp>>2][0]);
613
        }else{
614
            if (s->pict_type == I_TYPE) {
615
                set_stat(ST_INTRA_MB);
616
                put_bits(&s->pb, 
617
                         table_mb_intra[coded_cbp][1], table_mb_intra[coded_cbp][0]);
618
            } else {
619
                if (s->use_skip_mb_code)
620
                    put_bits(&s->pb, 1, 0);        /* mb coded */
621
                put_bits(&s->pb, 
622
                         table_mb_non_intra[cbp][1], 
623
                         table_mb_non_intra[cbp][0]);
624
            }
625
            set_stat(ST_INTRA_MB);
626
            put_bits(&s->pb, 1, 0);        /* no AC prediction yet */
627
            if(s->inter_intra_pred){
628
                s->h263_aic_dir=0;
629
                put_bits(&s->pb, table_inter_intra[s->h263_aic_dir][1], table_inter_intra[s->h263_aic_dir][0]);
630
            }
631
        }
632
    }
633

    
634
    for (i = 0; i < 6; i++) {
635
        msmpeg4_encode_block(s, block[i], i);
636
    }
637
}
638

    
639
#endif //CONFIG_ENCODERS
640

    
641
/* old ffmpeg msmpeg4v3 mode */
642
static void ff_old_msmpeg4_dc_scale(MpegEncContext * s)
643
{
644
    if (s->qscale < 5){
645
        s->y_dc_scale = 8;
646
        s->c_dc_scale = 8;
647
    }else if (s->qscale < 9){
648
        s->y_dc_scale = 2 * s->qscale;
649
        s->c_dc_scale = (s->qscale + 13)>>1;
650
    }else{
651
        s->y_dc_scale = s->qscale + 8;
652
        s->c_dc_scale = (s->qscale + 13)>>1;
653
    }
654
}
655

    
656
static inline int msmpeg4v1_pred_dc(MpegEncContext * s, int n, 
657
                                    int32_t **dc_val_ptr)
658
{
659
    int i;
660

    
661
    if (n < 4) {
662
        i= 0;
663
    } else {
664
        i= n-3;
665
    }
666
    
667
    *dc_val_ptr= &s->last_dc[i];
668
    return s->last_dc[i]; 
669
}
670

    
671
static int get_dc(uint8_t *src, int stride, int scale)
672
{
673
    int y;
674
    int sum=0;
675
    for(y=0; y<8; y++){
676
        int x;
677
        for(x=0; x<8; x++){
678
            sum+=src[x + y*stride];
679
        }
680
    }
681
    return (sum + (scale>>1))/scale;
682
}
683

    
684
/* dir = 0: left, dir = 1: top prediction */
685
static inline int msmpeg4_pred_dc(MpegEncContext * s, int n, 
686
                             uint16_t **dc_val_ptr, int *dir_ptr)
687
{
688
    int a, b, c, wrap, pred, scale;
689
    int16_t *dc_val;
690

    
691
    /* find prediction */
692
    if (n < 4) {
693
        scale = s->y_dc_scale;
694
    } else {
695
        scale = s->c_dc_scale;
696
    }
697
    
698
    wrap = s->block_wrap[n];
699
    dc_val= s->dc_val[0] + s->block_index[n];
700

    
701
    /* B C
702
     * A X 
703
     */
704
    a = dc_val[ - 1];
705
    b = dc_val[ - 1 - wrap];
706
    c = dc_val[ - wrap];
707
    
708
    if(s->first_slice_line && (n&2)==0 && s->msmpeg4_version<4){
709
        b=c=1024;
710
    }
711

    
712
    /* XXX: the following solution consumes divisions, but it does not
713
       necessitate to modify mpegvideo.c. The problem comes from the
714
       fact they decided to store the quantized DC (which would lead
715
       to problems if Q could vary !) */
716
#if defined ARCH_X86 && !defined PIC
717
    asm volatile(
718
        "movl %3, %%eax                \n\t"
719
        "shrl $1, %%eax                \n\t"
720
        "addl %%eax, %2                \n\t"
721
        "addl %%eax, %1                \n\t"
722
        "addl %0, %%eax                \n\t"
723
        "mull %4                \n\t"
724
        "movl %%edx, %0                \n\t"
725
        "movl %1, %%eax                \n\t"
726
        "mull %4                \n\t"
727
        "movl %%edx, %1                \n\t"
728
        "movl %2, %%eax                \n\t"
729
        "mull %4                \n\t"
730
        "movl %%edx, %2                \n\t"
731
        : "+b" (a), "+c" (b), "+D" (c)
732
        : "g" (scale), "S" (inverse[scale])
733
        : "%eax", "%edx"
734
    );
735
#else
736
    /* #elif defined (ARCH_ALPHA) */
737
    /* Divisions are extremely costly on Alpha; optimize the most
738
       common case. But they are costly everywhere...
739
     */
740
    if (scale == 8) {
741
        a = (a + (8 >> 1)) / 8;
742
        b = (b + (8 >> 1)) / 8;
743
        c = (c + (8 >> 1)) / 8;
744
    } else {
745
        a = (a + (scale >> 1)) / scale;
746
        b = (b + (scale >> 1)) / scale;
747
        c = (c + (scale >> 1)) / scale;
748
    }
749
#endif
750
    /* XXX: WARNING: they did not choose the same test as MPEG4. This
751
       is very important ! */
752
    if(s->msmpeg4_version>3){
753
        if(s->inter_intra_pred){
754
            uint8_t *dest;
755
            int wrap;
756
            
757
            if(n==1){
758
                pred=a;
759
                *dir_ptr = 0;
760
            }else if(n==2){
761
                pred=c;
762
                *dir_ptr = 1;
763
            }else if(n==3){
764
                if (abs(a - b) < abs(b - c)) {
765
                    pred = c;
766
                    *dir_ptr = 1;
767
                } else {
768
                    pred = a;
769
                    *dir_ptr = 0;
770
                }
771
            }else{
772
                if(n<4){
773
                    wrap= s->linesize;
774
                    dest= s->current_picture.data[0] + (((n>>1) + 2*s->mb_y) * 8*  wrap ) + ((n&1) + 2*s->mb_x) * 8;
775
                }else{
776
                    wrap= s->uvlinesize;
777
                    dest= s->current_picture.data[n-3] + (s->mb_y * 8 * wrap) + s->mb_x * 8;
778
                }
779
                if(s->mb_x==0) a= (1024 + (scale>>1))/scale;
780
                else           a= get_dc(dest-8, wrap, scale*8);
781
                if(s->mb_y==0) c= (1024 + (scale>>1))/scale;
782
                else           c= get_dc(dest-8*wrap, wrap, scale*8);
783
                
784
                if (s->h263_aic_dir==0) {
785
                    pred= a;
786
                    *dir_ptr = 0;
787
                }else if (s->h263_aic_dir==1) {
788
                    if(n==0){
789
                        pred= c;
790
                        *dir_ptr = 1;
791
                    }else{
792
                        pred= a;
793
                        *dir_ptr = 0;
794
                    }
795
                }else if (s->h263_aic_dir==2) {
796
                    if(n==0){
797
                        pred= a;
798
                        *dir_ptr = 0;
799
                    }else{
800
                        pred= c;
801
                        *dir_ptr = 1;
802
                    }
803
                } else {
804
                    pred= c;
805
                    *dir_ptr = 1;
806
                }
807
            }
808
        }else{
809
            if (abs(a - b) < abs(b - c)) {
810
                pred = c;
811
                *dir_ptr = 1;
812
            } else {
813
                pred = a;
814
                *dir_ptr = 0;
815
            }
816
        }
817
    }else{
818
        if (abs(a - b) <= abs(b - c)) {
819
            pred = c;
820
            *dir_ptr = 1;
821
        } else {
822
            pred = a;
823
            *dir_ptr = 0;
824
        }
825
    }
826

    
827
    /* update predictor */
828
    *dc_val_ptr = &dc_val[0];
829
    return pred;
830
}
831

    
832
#define DC_MAX 119
833

    
834
#ifdef CONFIG_ENCODERS
835

    
836
static void msmpeg4_encode_dc(MpegEncContext * s, int level, int n, int *dir_ptr)
837
{
838
    int sign, code;
839
    int pred;
840

    
841
    if(s->msmpeg4_version==1){
842
        int32_t *dc_val;
843
        pred = msmpeg4v1_pred_dc(s, n, &dc_val);
844
        
845
        /* update predictor */
846
        *dc_val= level;
847
    }else{
848
        uint16_t *dc_val;
849
        pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr);
850

    
851
        /* update predictor */
852
        if (n < 4) {
853
            *dc_val = level * s->y_dc_scale;
854
        } else {
855
            *dc_val = level * s->c_dc_scale;
856
        }
857
    }
858

    
859
    /* do the prediction */
860
    level -= pred;
861

    
862
    if(s->msmpeg4_version<=2){
863
        if (n < 4) {
864
            put_bits(&s->pb, 
865
                     v2_dc_lum_table[level+256][1],
866
                     v2_dc_lum_table[level+256][0]);
867
        }else{
868
            put_bits(&s->pb, 
869
                     v2_dc_chroma_table[level+256][1],
870
                     v2_dc_chroma_table[level+256][0]);
871
        }
872
    }else{
873
        sign = 0;
874
        if (level < 0) {
875
            level = -level;
876
            sign = 1;
877
        }
878
        code = level;
879
        if (code > DC_MAX) 
880
            code = DC_MAX;
881

    
882
        if (s->dc_table_index == 0) {
883
            if (n < 4) {
884
                put_bits(&s->pb, table0_dc_lum[code][1], table0_dc_lum[code][0]);
885
            } else {
886
                put_bits(&s->pb, table0_dc_chroma[code][1], table0_dc_chroma[code][0]);
887
            }
888
        } else {
889
            if (n < 4) {
890
                put_bits(&s->pb, table1_dc_lum[code][1], table1_dc_lum[code][0]);
891
            } else {
892
                put_bits(&s->pb, table1_dc_chroma[code][1], table1_dc_chroma[code][0]);
893
            }
894
        }
895
            
896
        if (code == DC_MAX)
897
            put_bits(&s->pb, 8, level);
898
            
899
        if (level != 0) {
900
            put_bits(&s->pb, 1, sign);
901
        }
902
    }
903
}
904

    
905
/* Encoding of a block. Very similar to MPEG4 except for a different
906
   escape coding (same as H263) and more vlc tables.
907
 */
908
static inline void msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n)
909
{
910
    int level, run, last, i, j, last_index;
911
    int last_non_zero, sign, slevel;
912
    int code, run_diff, dc_pred_dir;
913
    const RLTable *rl;
914
    const uint8_t *scantable;
915

    
916
    if (s->mb_intra) {
917
        set_stat(ST_DC);
918
        msmpeg4_encode_dc(s, block[0], n, &dc_pred_dir);
919
        i = 1;
920
        if (n < 4) {
921
            rl = &rl_table[s->rl_table_index];
922
        } else {
923
            rl = &rl_table[3 + s->rl_chroma_table_index];
924
        }
925
        run_diff = 0;
926
        scantable= s->intra_scantable.permutated;
927
        set_stat(ST_INTRA_AC);
928
    } else {
929
        i = 0;
930
        rl = &rl_table[3 + s->rl_table_index];
931
        if(s->msmpeg4_version<=2)
932
            run_diff = 0;
933
        else
934
            run_diff = 1;
935
        scantable= s->inter_scantable.permutated;
936
        set_stat(ST_INTER_AC);
937
    }
938

    
939
    /* recalculate block_last_index for M$ wmv1 */
940
    if(s->msmpeg4_version>=4 && s->block_last_index[n]>0){
941
        for(last_index=63; last_index>=0; last_index--){
942
            if(block[scantable[last_index]]) break;
943
        }
944
        s->block_last_index[n]= last_index;
945
    }else
946
        last_index = s->block_last_index[n];
947
    /* AC coefs */
948
    last_non_zero = i - 1;
949
    for (; i <= last_index; i++) {
950
        j = scantable[i];
951
        level = block[j];
952
        if (level) {
953
            run = i - last_non_zero - 1;
954
            last = (i == last_index);
955
            sign = 0;
956
            slevel = level;
957
            if (level < 0) {
958
                sign = 1;
959
                level = -level;
960
            }
961

    
962
            if(level<=MAX_LEVEL && run<=MAX_RUN){
963
                s->ac_stats[s->mb_intra][n>3][level][run][last]++;
964
            }
965
#if 0
966
else
967
    s->ac_stats[s->mb_intra][n>3][40][63][0]++; //esc3 like
968
#endif
969
            code = get_rl_index(rl, last, run, level);
970
            put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
971
            if (code == rl->n) {
972
                int level1, run1;
973

    
974
                level1 = level - rl->max_level[last][run];
975
                if (level1 < 1) 
976
                    goto esc2;
977
                code = get_rl_index(rl, last, run, level1);
978
                if (code == rl->n) {
979
                esc2:
980
                    put_bits(&s->pb, 1, 0);
981
                    if (level > MAX_LEVEL)
982
                        goto esc3;
983
                    run1 = run - rl->max_run[last][level] - run_diff;
984
                    if (run1 < 0)
985
                        goto esc3;
986
                    code = get_rl_index(rl, last, run1, level);
987
                    if (code == rl->n) {
988
                    esc3:
989
                        /* third escape */
990
                        put_bits(&s->pb, 1, 0);
991
                        put_bits(&s->pb, 1, last);
992
                        if(s->msmpeg4_version>=4){
993
                            if(s->esc3_level_length==0){
994
                                s->esc3_level_length=8;
995
                                s->esc3_run_length= 6;
996
                                if(s->qscale<8)
997
                                    put_bits(&s->pb, 6, 3);
998
                                else
999
                                    put_bits(&s->pb, 8, 3);
1000
                            }
1001
                            put_bits(&s->pb, s->esc3_run_length, run);
1002
                            put_bits(&s->pb, 1, sign);
1003
                            put_bits(&s->pb, s->esc3_level_length, level);
1004
                        }else{
1005
                            put_bits(&s->pb, 6, run);
1006
                            put_bits(&s->pb, 8, slevel & 0xff);
1007
                        }
1008
                    } else {
1009
                        /* second escape */
1010
                        put_bits(&s->pb, 1, 1);
1011
                        put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
1012
                        put_bits(&s->pb, 1, sign);
1013
                    }
1014
                } else {
1015
                    /* first escape */
1016
                    put_bits(&s->pb, 1, 1);
1017
                    put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
1018
                    put_bits(&s->pb, 1, sign);
1019
                }
1020
            } else {
1021
                put_bits(&s->pb, 1, sign);
1022
            }
1023
            last_non_zero = i;
1024
        }
1025
    }
1026
}
1027

    
1028
#endif //CONFIG_ENCODERS
1029

    
1030
/****************************************/
1031
/* decoding stuff */
1032

    
1033
static VLC mb_non_intra_vlc[4];
1034
static VLC mb_intra_vlc;
1035
static VLC dc_lum_vlc[2];
1036
static VLC dc_chroma_vlc[2];
1037
static VLC v2_dc_lum_vlc;
1038
static VLC v2_dc_chroma_vlc;
1039
static VLC cbpy_vlc;
1040
static VLC v2_intra_cbpc_vlc;
1041
static VLC v2_mb_type_vlc;
1042
static VLC v2_mv_vlc;
1043
static VLC v1_intra_cbpc_vlc;
1044
static VLC v1_inter_cbpc_vlc;
1045
static VLC inter_intra_vlc;
1046

    
1047
/* this table is practically identical to the one from h263 except that its inverted */
1048
static void init_h263_dc_for_msmpeg4(void)
1049
{
1050
        int level, uni_code, uni_len;
1051

    
1052
        for(level=-256; level<256; level++){
1053
            int size, v, l;
1054
            /* find number of bits */
1055
            size = 0;
1056
            v = abs(level);
1057
            while (v) {
1058
                v >>= 1;
1059
                    size++;
1060
            }
1061

    
1062
            if (level < 0)
1063
                l= (-level) ^ ((1 << size) - 1);
1064
            else
1065
                l= level;
1066

    
1067
            /* luminance h263 */
1068
            uni_code= DCtab_lum[size][0];
1069
            uni_len = DCtab_lum[size][1];
1070
            uni_code ^= (1<<uni_len)-1; //M$ doesnt like compatibility
1071

    
1072
            if (size > 0) {
1073
                uni_code<<=size; uni_code|=l;
1074
                uni_len+=size;
1075
                if (size > 8){
1076
                    uni_code<<=1; uni_code|=1;
1077
                    uni_len++;
1078
                }
1079
            }
1080
            v2_dc_lum_table[level+256][0]= uni_code;
1081
            v2_dc_lum_table[level+256][1]= uni_len;
1082

    
1083
            /* chrominance h263 */
1084
            uni_code= DCtab_chrom[size][0];
1085
            uni_len = DCtab_chrom[size][1];
1086
            uni_code ^= (1<<uni_len)-1; //M$ doesnt like compatibility
1087
            
1088
            if (size > 0) {
1089
                uni_code<<=size; uni_code|=l;
1090
                uni_len+=size;
1091
                if (size > 8){
1092
                    uni_code<<=1; uni_code|=1;
1093
                    uni_len++;
1094
                }
1095
            }
1096
            v2_dc_chroma_table[level+256][0]= uni_code;
1097
            v2_dc_chroma_table[level+256][1]= uni_len;
1098

    
1099
        }
1100
}
1101

    
1102
/* init all vlc decoding tables */
1103
int ff_msmpeg4_decode_init(MpegEncContext *s)
1104
{
1105
    static int done = 0;
1106
    int i;
1107
    MVTable *mv;
1108

    
1109
    common_init(s);
1110

    
1111
    if (!done) {
1112
        done = 1;
1113

    
1114
        for(i=0;i<NB_RL_TABLES;i++) {
1115
            init_rl(&rl_table[i]);
1116
            init_vlc_rl(&rl_table[i]);
1117
        }
1118
        for(i=0;i<2;i++) {
1119
            mv = &mv_tables[i];
1120
            init_vlc(&mv->vlc, MV_VLC_BITS, mv->n + 1, 
1121
                     mv->table_mv_bits, 1, 1,
1122
                     mv->table_mv_code, 2, 2);
1123
        }
1124

    
1125
        init_vlc(&dc_lum_vlc[0], DC_VLC_BITS, 120, 
1126
                 &table0_dc_lum[0][1], 8, 4,
1127
                 &table0_dc_lum[0][0], 8, 4);
1128
        init_vlc(&dc_chroma_vlc[0], DC_VLC_BITS, 120, 
1129
                 &table0_dc_chroma[0][1], 8, 4,
1130
                 &table0_dc_chroma[0][0], 8, 4);
1131
        init_vlc(&dc_lum_vlc[1], DC_VLC_BITS, 120, 
1132
                 &table1_dc_lum[0][1], 8, 4,
1133
                 &table1_dc_lum[0][0], 8, 4);
1134
        init_vlc(&dc_chroma_vlc[1], DC_VLC_BITS, 120, 
1135
                 &table1_dc_chroma[0][1], 8, 4,
1136
                 &table1_dc_chroma[0][0], 8, 4);
1137
    
1138
        init_vlc(&v2_dc_lum_vlc, DC_VLC_BITS, 512, 
1139
                 &v2_dc_lum_table[0][1], 8, 4,
1140
                 &v2_dc_lum_table[0][0], 8, 4);
1141
        init_vlc(&v2_dc_chroma_vlc, DC_VLC_BITS, 512, 
1142
                 &v2_dc_chroma_table[0][1], 8, 4,
1143
                 &v2_dc_chroma_table[0][0], 8, 4);
1144
    
1145
        init_vlc(&cbpy_vlc, CBPY_VLC_BITS, 16,
1146
                 &cbpy_tab[0][1], 2, 1,
1147
                 &cbpy_tab[0][0], 2, 1);
1148
        init_vlc(&v2_intra_cbpc_vlc, V2_INTRA_CBPC_VLC_BITS, 4,
1149
                 &v2_intra_cbpc[0][1], 2, 1,
1150
                 &v2_intra_cbpc[0][0], 2, 1);
1151
        init_vlc(&v2_mb_type_vlc, V2_MB_TYPE_VLC_BITS, 8,
1152
                 &v2_mb_type[0][1], 2, 1,
1153
                 &v2_mb_type[0][0], 2, 1);
1154
        init_vlc(&v2_mv_vlc, V2_MV_VLC_BITS, 33,
1155
                 &mvtab[0][1], 2, 1,
1156
                 &mvtab[0][0], 2, 1);
1157

    
1158
        for(i=0; i<4; i++){
1159
            init_vlc(&mb_non_intra_vlc[i], MB_NON_INTRA_VLC_BITS, 128, 
1160
                     &wmv2_inter_table[i][0][1], 8, 4,
1161
                     &wmv2_inter_table[i][0][0], 8, 4); //FIXME name?
1162
        }
1163
        
1164
        init_vlc(&mb_intra_vlc, MB_INTRA_VLC_BITS, 64, 
1165
                 &table_mb_intra[0][1], 4, 2,
1166
                 &table_mb_intra[0][0], 4, 2);
1167
        
1168
        init_vlc(&v1_intra_cbpc_vlc, V1_INTRA_CBPC_VLC_BITS, 8, 
1169
                 intra_MCBPC_bits, 1, 1,
1170
                 intra_MCBPC_code, 1, 1);
1171
        init_vlc(&v1_inter_cbpc_vlc, V1_INTER_CBPC_VLC_BITS, 25, 
1172
                 inter_MCBPC_bits, 1, 1,
1173
                 inter_MCBPC_code, 1, 1);
1174
        
1175
        init_vlc(&inter_intra_vlc, INTER_INTRA_VLC_BITS, 4, 
1176
                 &table_inter_intra[0][1], 2, 1,
1177
                 &table_inter_intra[0][0], 2, 1);
1178
    }
1179
    
1180
    switch(s->msmpeg4_version){
1181
    case 1:
1182
    case 2:
1183
        s->decode_mb= msmpeg4v12_decode_mb;
1184
        break;
1185
    case 3:
1186
    case 4:
1187
        s->decode_mb= msmpeg4v34_decode_mb;
1188
        break;
1189
    case 5:
1190
        s->decode_mb= wmv2_decode_mb;
1191
        break;
1192
    }
1193
    
1194
    s->slice_height= s->mb_height; //to avoid 1/0 if the first frame isnt a keyframe
1195
    
1196
    return 0;
1197
}
1198

    
1199
static int decode012(GetBitContext *gb)
1200
{
1201
    int n;
1202
    n = get_bits1(gb);
1203
    if (n == 0)
1204
        return 0;
1205
    else
1206
        return get_bits1(gb) + 1;
1207
}
1208

    
1209
int msmpeg4_decode_picture_header(MpegEncContext * s)
1210
{
1211
    int code;
1212

    
1213
#if 0
1214
{
1215
int i;
1216
for(i=0; i<s->gb.size_in_bits; i++)
1217
    printf("%d", get_bits1(&s->gb));
1218
//    get_bits1(&s->gb);
1219
printf("END\n");
1220
return -1;
1221
}
1222
#endif
1223

    
1224
    if(s->msmpeg4_version==1){
1225
        int start_code, num;
1226
        start_code = (get_bits(&s->gb, 16)<<16) | get_bits(&s->gb, 16);
1227
        if(start_code!=0x00000100){
1228
            fprintf(stderr, "invalid startcode\n");
1229
            return -1;
1230
        }
1231

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

    
1235
    s->pict_type = get_bits(&s->gb, 2) + 1;
1236
    if (s->pict_type != I_TYPE &&
1237
        s->pict_type != P_TYPE){
1238
        fprintf(stderr, "invalid picture type\n");
1239
        return -1;
1240
    }
1241
#if 0
1242
{
1243
    static int had_i=0;
1244
    if(s->pict_type == I_TYPE) had_i=1;
1245
    if(!had_i) return -1;
1246
}
1247
#endif
1248
    s->qscale = get_bits(&s->gb, 5);
1249
    if(s->qscale==0){
1250
        fprintf(stderr, "invalid qscale\n");
1251
        return -1;
1252
    }
1253

    
1254
    if (s->pict_type == I_TYPE) {
1255
        code = get_bits(&s->gb, 5); 
1256
        if(s->msmpeg4_version==1){
1257
            if(code==0 || code>s->mb_height){
1258
                fprintf(stderr, "invalid slice height %d\n", code);
1259
                return -1;
1260
            }
1261

    
1262
            s->slice_height = code;
1263
        }else{
1264
            /* 0x17: one slice, 0x18: two slices, ... */
1265
            if (code < 0x17){
1266
                fprintf(stderr, "error, slice code was %X\n", code);
1267
                return -1;
1268
            }
1269

    
1270
            s->slice_height = s->mb_height / (code - 0x16);
1271
        }
1272

    
1273
        switch(s->msmpeg4_version){
1274
        case 1:
1275
        case 2:
1276
            s->rl_chroma_table_index = 2;
1277
            s->rl_table_index = 2;
1278

    
1279
            s->dc_table_index = 0; //not used
1280
            break;
1281
        case 3:
1282
            s->rl_chroma_table_index = decode012(&s->gb);
1283
            s->rl_table_index = decode012(&s->gb);
1284

    
1285
            s->dc_table_index = get_bits1(&s->gb);
1286
            break;
1287
        case 4:
1288
            msmpeg4_decode_ext_header(s, (2+5+5+17+7)/8);
1289

    
1290
            if(s->bit_rate > MBAC_BITRATE) s->per_mb_rl_table= get_bits1(&s->gb);
1291
            else                           s->per_mb_rl_table= 0;
1292
            
1293
            if(!s->per_mb_rl_table){
1294
                s->rl_chroma_table_index = decode012(&s->gb);
1295
                s->rl_table_index = decode012(&s->gb);
1296
            }
1297

    
1298
            s->dc_table_index = get_bits1(&s->gb);
1299
            s->inter_intra_pred= 0;
1300
            break;
1301
        }
1302
        s->no_rounding = 1;
1303
/*        printf("qscale:%d rlc:%d rl:%d dc:%d mbrl:%d slice:%d   \n", 
1304
                s->qscale,
1305
                s->rl_chroma_table_index,
1306
                s->rl_table_index, 
1307
                s->dc_table_index,
1308
                s->per_mb_rl_table,
1309
                s->slice_height);*/
1310
    } else {
1311
        switch(s->msmpeg4_version){
1312
        case 1:
1313
        case 2:
1314
            if(s->msmpeg4_version==1)
1315
                s->use_skip_mb_code = 1;
1316
            else
1317
                s->use_skip_mb_code = get_bits1(&s->gb);
1318
            s->rl_table_index = 2;
1319
            s->rl_chroma_table_index = s->rl_table_index;
1320
            s->dc_table_index = 0; //not used
1321
            s->mv_table_index = 0;
1322
            break;
1323
        case 3:
1324
            s->use_skip_mb_code = get_bits1(&s->gb);
1325
            s->rl_table_index = decode012(&s->gb);
1326
            s->rl_chroma_table_index = s->rl_table_index;
1327

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

    
1330
            s->mv_table_index = get_bits1(&s->gb);
1331
            break;
1332
        case 4:
1333
            s->use_skip_mb_code = get_bits1(&s->gb);
1334

    
1335
            if(s->bit_rate > MBAC_BITRATE) s->per_mb_rl_table= get_bits1(&s->gb);
1336
            else                           s->per_mb_rl_table= 0;
1337

    
1338
            if(!s->per_mb_rl_table){
1339
                s->rl_table_index = decode012(&s->gb);
1340
                s->rl_chroma_table_index = s->rl_table_index;
1341
            }
1342

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

    
1345
            s->mv_table_index = get_bits1(&s->gb);
1346
            s->inter_intra_pred= (s->width*s->height < 320*240 && s->bit_rate<=II_BITRATE);
1347
            break;
1348
        }
1349
/*        printf("skip:%d rl:%d rlc:%d dc:%d mv:%d mbrl:%d qp:%d   \n", 
1350
                s->use_skip_mb_code, 
1351
                s->rl_table_index, 
1352
                s->rl_chroma_table_index, 
1353
                s->dc_table_index,
1354
                s->mv_table_index,
1355
                s->per_mb_rl_table,
1356
                s->qscale);*/
1357
        if(s->flipflop_rounding){
1358
            s->no_rounding ^= 1;
1359
        }else{
1360
            s->no_rounding = 0;
1361
        }
1362
    }
1363
//printf("%d %d %d %d %d\n", s->pict_type, s->bit_rate, s->inter_intra_pred, s->width, s->height);
1364

    
1365
    s->esc3_level_length= 0;
1366
    s->esc3_run_length= 0;
1367

    
1368
#ifdef DEBUG
1369
    printf("*****frame %d:\n", frame_count++);
1370
#endif
1371
    return 0;
1372
}
1373

    
1374
int msmpeg4_decode_ext_header(MpegEncContext * s, int buf_size)
1375
{
1376
    int left= buf_size*8 - get_bits_count(&s->gb);
1377
    int length= s->msmpeg4_version>=3 ? 17 : 16;
1378
    /* the alt_bitstream reader could read over the end so we need to check it */
1379
    if(left>=length && left<length+8)
1380
    {
1381
        int fps;
1382

    
1383
        fps= get_bits(&s->gb, 5);
1384
        s->bit_rate= get_bits(&s->gb, 11)*1024;
1385
        if(s->msmpeg4_version>=3)
1386
            s->flipflop_rounding= get_bits1(&s->gb);
1387
        else
1388
            s->flipflop_rounding= 0;
1389

    
1390
//        printf("fps:%2d bps:%2d roundingType:%1d\n", fps, s->bit_rate/1024, s->flipflop_rounding);
1391
    }
1392
    else if(left<length+8)
1393
    {
1394
        s->flipflop_rounding= 0;
1395
        printf("ext header missing, %d left\n", left);
1396
    }
1397
    else
1398
    {
1399
        fprintf(stderr, "I frame too long, ignoring ext header\n");
1400
    }
1401

    
1402
    return 0;
1403
}
1404

    
1405
static inline void msmpeg4_memsetw(short *tab, int val, int n)
1406
{
1407
    int i;
1408
    for(i=0;i<n;i++)
1409
        tab[i] = val;
1410
}
1411

    
1412
#ifdef CONFIG_ENCODERS
1413

    
1414
static void msmpeg4v2_encode_motion(MpegEncContext * s, int val)
1415
{
1416
    int range, bit_size, sign, code, bits;
1417

    
1418
    if (val == 0) {
1419
        /* zero vector */
1420
        code = 0;
1421
        put_bits(&s->pb, mvtab[code][1], mvtab[code][0]);
1422
    } else {
1423
        bit_size = s->f_code - 1;
1424
        range = 1 << bit_size;
1425
        if (val <= -64)
1426
            val += 64;
1427
        else if (val >= 64)
1428
            val -= 64;
1429

    
1430
        if (val >= 0) {
1431
            sign = 0;
1432
        } else {
1433
            val = -val;
1434
            sign = 1;
1435
        }
1436
        val--;
1437
        code = (val >> bit_size) + 1;
1438
        bits = val & (range - 1);
1439

    
1440
        put_bits(&s->pb, mvtab[code][1] + 1, (mvtab[code][0] << 1) | sign); 
1441
        if (bit_size > 0) {
1442
            put_bits(&s->pb, bit_size, bits);
1443
        }
1444
    }
1445
}
1446

    
1447
#endif //CONFIG_ENCODERS
1448

    
1449
/* this is identical to h263 except that its range is multiplied by 2 */
1450
static int msmpeg4v2_decode_motion(MpegEncContext * s, int pred, int f_code)
1451
{
1452
    int code, val, sign, shift;
1453

    
1454
    code = get_vlc2(&s->gb, v2_mv_vlc.table, V2_MV_VLC_BITS, 2);
1455
//     printf("MV code %d at %d %d pred: %d\n", code, s->mb_x,s->mb_y, pred);
1456
    if (code < 0)
1457
        return 0xffff;
1458

    
1459
    if (code == 0)
1460
        return pred;
1461
    sign = get_bits1(&s->gb);
1462
    shift = f_code - 1;
1463
    val = (code - 1) << shift;
1464
    if (shift > 0)
1465
        val |= get_bits(&s->gb, shift);
1466
    val++;
1467
    if (sign)
1468
        val = -val;
1469

    
1470
    val += pred;
1471
    if (val <= -64)
1472
        val += 64;
1473
    else if (val >= 64)
1474
        val -= 64;
1475

    
1476
    return val;
1477
}
1478

    
1479
static int msmpeg4v12_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
1480
{
1481
    int cbp, code, i;
1482
    
1483
    s->error_status_table[s->mb_x + s->mb_y*s->mb_width]= 0;
1484
    
1485
    if (s->pict_type == P_TYPE) {
1486
        if (s->use_skip_mb_code) {
1487
            if (get_bits1(&s->gb)) {
1488
                /* skip mb */
1489
                s->mb_intra = 0;
1490
                for(i=0;i<6;i++)
1491
                    s->block_last_index[i] = -1;
1492
                s->mv_dir = MV_DIR_FORWARD;
1493
                s->mv_type = MV_TYPE_16X16;
1494
                s->mv[0][0][0] = 0;
1495
                s->mv[0][0][1] = 0;
1496
                s->mb_skiped = 1;
1497
                return 0;
1498
            }
1499
        }
1500

    
1501
        if(s->msmpeg4_version==2)
1502
            code = get_vlc2(&s->gb, v2_mb_type_vlc.table, V2_MB_TYPE_VLC_BITS, 1);
1503
        else
1504
            code = get_vlc2(&s->gb, v1_inter_cbpc_vlc.table, V1_INTER_CBPC_VLC_BITS, 3);
1505
        if(code<0 || code>7){
1506
            fprintf(stderr, "cbpc %d invalid at %d %d\n", code, s->mb_x, s->mb_y);
1507
            return -1;
1508
        }
1509

    
1510
        s->mb_intra = code >>2;
1511
    
1512
        cbp = code & 0x3;
1513
    } else {
1514
        s->mb_intra = 1;
1515
        if(s->msmpeg4_version==2)
1516
            cbp= get_vlc2(&s->gb, v2_intra_cbpc_vlc.table, V2_INTRA_CBPC_VLC_BITS, 1);
1517
        else
1518
            cbp= get_vlc2(&s->gb, v1_intra_cbpc_vlc.table, V1_INTRA_CBPC_VLC_BITS, 1);
1519
        if(cbp<0 || cbp>3){
1520
            fprintf(stderr, "cbpc %d invalid at %d %d\n", cbp, s->mb_x, s->mb_y);
1521
            return -1;
1522
        }
1523
    }
1524

    
1525
    if (!s->mb_intra) {
1526
        int mx, my, cbpy;
1527
        
1528
        cbpy= get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1);
1529
        if(cbpy<0){
1530
            fprintf(stderr, "cbpy %d invalid at %d %d\n", cbp, s->mb_x, s->mb_y);
1531
            return -1;
1532
        }
1533

    
1534
        cbp|= cbpy<<2;
1535
        if(s->msmpeg4_version==1 || (cbp&3) != 3) cbp^= 0x3C;
1536
        
1537
        h263_pred_motion(s, 0, &mx, &my);
1538
        mx= msmpeg4v2_decode_motion(s, mx, 1);
1539
        my= msmpeg4v2_decode_motion(s, my, 1);
1540
        
1541
        s->mv_dir = MV_DIR_FORWARD;
1542
        s->mv_type = MV_TYPE_16X16;
1543
        s->mv[0][0][0] = mx;
1544
        s->mv[0][0][1] = my;
1545
    } else {
1546
        if(s->msmpeg4_version==2){
1547
            s->ac_pred = get_bits1(&s->gb);
1548
            cbp|= get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1)<<2; //FIXME check errors
1549
        } else{
1550
            s->ac_pred = 0;
1551
            cbp|= get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1)<<2; //FIXME check errors
1552
            if(s->pict_type==P_TYPE) cbp^=0x3C;
1553
        }
1554
    }
1555

    
1556
    for (i = 0; i < 6; i++) {
1557
        if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, NULL) < 0)
1558
        {
1559
             fprintf(stderr,"\nerror while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
1560
             return -1;
1561
        }
1562
    }
1563
    return 0;
1564
}
1565

    
1566
static int msmpeg4v34_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
1567
{
1568
    int cbp, code, i;
1569
    uint8_t *coded_val;
1570

    
1571
#ifdef PRINT_MB
1572
if(s->mb_x==0){
1573
    printf("\n");
1574
    if(s->mb_y==0) printf("\n");
1575
}
1576
#endif
1577

    
1578
    s->error_status_table[s->mb_x + s->mb_y*s->mb_width]= 0;
1579
    
1580
    if (s->pict_type == P_TYPE) {
1581
        set_stat(ST_INTER_MB);
1582
        if (s->use_skip_mb_code) {
1583
            if (get_bits1(&s->gb)) {
1584
                /* skip mb */
1585
                s->mb_intra = 0;
1586
                for(i=0;i<6;i++)
1587
                    s->block_last_index[i] = -1;
1588
                s->mv_dir = MV_DIR_FORWARD;
1589
                s->mv_type = MV_TYPE_16X16;
1590
                s->mv[0][0][0] = 0;
1591
                s->mv[0][0][1] = 0;
1592
                s->mb_skiped = 1;
1593
#ifdef PRINT_MB
1594
printf("S ");
1595
#endif
1596
                return 0;
1597
            }
1598
        }
1599
        
1600
        code = get_vlc2(&s->gb, mb_non_intra_vlc[DEFAULT_INTER_INDEX].table, MB_NON_INTRA_VLC_BITS, 3);
1601
        if (code < 0)
1602
            return -1;
1603
        //s->mb_intra = (code & 0x40) ? 0 : 1;
1604
        s->mb_intra = (~code & 0x40) >> 6;
1605
            
1606
        cbp = code & 0x3f;
1607
    } else {
1608
        set_stat(ST_INTRA_MB);
1609
        s->mb_intra = 1;
1610
        code = get_vlc2(&s->gb, mb_intra_vlc.table, MB_INTRA_VLC_BITS, 2);
1611
        if (code < 0)
1612
            return -1;
1613
        /* predict coded block pattern */
1614
        cbp = 0;
1615
        for(i=0;i<6;i++) {
1616
            int val = ((code >> (5 - i)) & 1);
1617
            if (i < 4) {
1618
                int pred = coded_block_pred(s, i, &coded_val);
1619
                val = val ^ pred;
1620
                *coded_val = val;
1621
            }
1622
            cbp |= val << (5 - i);
1623
        }
1624
    }
1625

    
1626
    if (!s->mb_intra) {
1627
        int mx, my;
1628
//printf("P at %d %d\n", s->mb_x, s->mb_y);
1629
        if(s->per_mb_rl_table && cbp){
1630
            s->rl_table_index = decode012(&s->gb);
1631
            s->rl_chroma_table_index = s->rl_table_index;
1632
        }
1633
        set_stat(ST_MV);
1634
        h263_pred_motion(s, 0, &mx, &my);
1635
        if (msmpeg4_decode_motion(s, &mx, &my) < 0)
1636
            return -1;
1637
        s->mv_dir = MV_DIR_FORWARD;
1638
        s->mv_type = MV_TYPE_16X16;
1639
        s->mv[0][0][0] = mx;
1640
        s->mv[0][0][1] = my;
1641
#ifdef PRINT_MB
1642
printf("P ");
1643
#endif
1644
    } else {
1645
//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));
1646
        set_stat(ST_INTRA_MB);
1647
        s->ac_pred = get_bits1(&s->gb);
1648
#ifdef PRINT_MB
1649
printf("%c", s->ac_pred ? 'A' : 'I');
1650
#endif
1651
        if(s->inter_intra_pred){
1652
            s->h263_aic_dir= get_vlc2(&s->gb, inter_intra_vlc.table, INTER_INTRA_VLC_BITS, 1);
1653
//            printf("%d%d %d %d/", s->ac_pred, s->h263_aic_dir, s->mb_x, s->mb_y);
1654
        }
1655
        if(s->per_mb_rl_table && cbp){
1656
            s->rl_table_index = decode012(&s->gb);
1657
            s->rl_chroma_table_index = s->rl_table_index;
1658
        }
1659
    }
1660

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

    
1681
    if (s->mb_intra) {
1682
        qmul=1;
1683
        qadd=0;
1684

    
1685
        /* DC coef */
1686
        set_stat(ST_DC);
1687
        level = msmpeg4_decode_dc(s, n, &dc_pred_dir);
1688
#ifdef PRINT_MB
1689
{
1690
    static int c;
1691
    if(n==0) c=0;
1692
    if(n==4) printf("%X", c);
1693
    c+= c +dc_pred_dir;
1694
}
1695
#endif
1696
        if (level < 0){
1697
            fprintf(stderr, "dc overflow- block: %d qscale: %d//\n", n, s->qscale);
1698
            if(s->inter_intra_pred) level=0;
1699
            else                    return -1;
1700
        }
1701
        if (n < 4) {
1702
            rl = &rl_table[s->rl_table_index];
1703
            if(level > 256*s->y_dc_scale){
1704
                fprintf(stderr, "dc overflow+ L qscale: %d//\n", s->qscale);
1705
                if(!s->inter_intra_pred) return -1;
1706
            }
1707
        } else {
1708
            rl = &rl_table[3 + s->rl_chroma_table_index];
1709
            if(level > 256*s->c_dc_scale){
1710
                fprintf(stderr, "dc overflow+ C qscale: %d//\n", s->qscale);
1711
                if(!s->inter_intra_pred) return -1;
1712
            }
1713
        }
1714
        block[0] = level;
1715

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

    
1737
        if(s->msmpeg4_version==2)
1738
            run_diff = 0;
1739
        else
1740
            run_diff = 1;
1741

    
1742
        if (!coded) {
1743
            s->block_last_index[n] = i;
1744
            return 0;
1745
        }
1746
        if(!scan_table)
1747
            scan_table = s->inter_scantable.permutated;
1748
        set_stat(ST_INTER_AC);
1749
        rl_vlc= rl->rl_vlc[s->qscale];
1750
    }
1751
  {
1752
    OPEN_READER(re, &s->gb);
1753
    for(;;) {
1754
        UPDATE_CACHE(re, &s->gb);
1755
        GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2);
1756
        if (level==0) {
1757
            int cache;
1758
            cache= GET_CACHE(re, &s->gb);
1759
            /* escape */
1760
            if (s->msmpeg4_version==1 || (cache&0x80000000)==0) {
1761
                if (s->msmpeg4_version==1 || (cache&0x40000000)==0) {
1762
                    /* third escape */
1763
                    if(s->msmpeg4_version!=1) LAST_SKIP_BITS(re, &s->gb, 2);
1764
                    UPDATE_CACHE(re, &s->gb);
1765
                    if(s->msmpeg4_version<=3){
1766
                        last=  SHOW_UBITS(re, &s->gb, 1); SKIP_CACHE(re, &s->gb, 1);
1767
                        run=   SHOW_UBITS(re, &s->gb, 6); SKIP_CACHE(re, &s->gb, 6);
1768
                        level= SHOW_SBITS(re, &s->gb, 8); LAST_SKIP_CACHE(re, &s->gb, 8);
1769
                        SKIP_COUNTER(re, &s->gb, 1+6+8);
1770
                    }else{                        
1771
                        int sign;
1772
                        last=  SHOW_UBITS(re, &s->gb, 1); SKIP_BITS(re, &s->gb, 1);
1773
                        if(!s->esc3_level_length){
1774
                            int ll;
1775
                            //printf("ESC-3 %X at %d %d\n", show_bits(&s->gb, 24), s->mb_x, s->mb_y);
1776
                            if(s->qscale<8){
1777
                                ll= SHOW_UBITS(re, &s->gb, 3); SKIP_BITS(re, &s->gb, 3);
1778
                                if(ll==0){
1779
                                    if(SHOW_UBITS(re, &s->gb, 1)) printf("cool a new vlc code ,contact the ffmpeg developers and upload the file\n");
1780
                                    SKIP_BITS(re, &s->gb, 1);
1781
                                    ll=8;
1782
                                }
1783
                            }else{
1784
                                ll=2;
1785
                                while(ll<8 && SHOW_UBITS(re, &s->gb, 1)==0){
1786
                                    ll++;
1787
                                    SKIP_BITS(re, &s->gb, 1);
1788
                                }
1789
                                if(ll<8) SKIP_BITS(re, &s->gb, 1);
1790
                            }
1791

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

    
1908
            block[scan_table[i]] = level;
1909
            break;
1910
        }
1911

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

    
1929
static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr)
1930
{
1931
    int level, pred;
1932

    
1933
    if(s->msmpeg4_version<=2){
1934
        if (n < 4) {
1935
            level = get_vlc2(&s->gb, v2_dc_lum_vlc.table, DC_VLC_BITS, 3);
1936
        } else {
1937
            level = get_vlc2(&s->gb, v2_dc_chroma_vlc.table, DC_VLC_BITS, 3);
1938
        }
1939
        if (level < 0) 
1940
            return -1;
1941
        level-=256;
1942
    }else{  //FIXME optimize use unified tables & index
1943
        if (n < 4) {
1944
            level = get_vlc2(&s->gb, dc_lum_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
1945
        } else {
1946
            level = get_vlc2(&s->gb, dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
1947
        }
1948
        if (level < 0){
1949
            fprintf(stderr, "illegal dc vlc\n");
1950
            return -1;
1951
        }
1952

    
1953
        if (level == DC_MAX) {
1954
            level = get_bits(&s->gb, 8);
1955
            if (get_bits1(&s->gb))
1956
                level = -level;
1957
        } else if (level != 0) {
1958
            if (get_bits1(&s->gb))
1959
                level = -level;
1960
        }
1961
    }
1962

    
1963
    if(s->msmpeg4_version==1){
1964
        int32_t *dc_val;
1965
        pred = msmpeg4v1_pred_dc(s, n, &dc_val);
1966
        level += pred;
1967
        
1968
        /* update predictor */
1969
        *dc_val= level;
1970
    }else{
1971
        uint16_t *dc_val;
1972
        pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr);
1973
        level += pred;
1974

    
1975
        /* update predictor */
1976
        if (n < 4) {
1977
            *dc_val = level * s->y_dc_scale;
1978
        } else {
1979
            *dc_val = level * s->c_dc_scale;
1980
        }
1981
    }
1982

    
1983
    return level;
1984
}
1985

    
1986
static int msmpeg4_decode_motion(MpegEncContext * s, 
1987
                                 int *mx_ptr, int *my_ptr)
1988
{
1989
    MVTable *mv;
1990
    int code, mx, my;
1991

    
1992
    mv = &mv_tables[s->mv_table_index];
1993

    
1994
    code = get_vlc2(&s->gb, mv->vlc.table, MV_VLC_BITS, 2);
1995
    if (code < 0){
1996
        fprintf(stderr, "illegal MV code at %d %d\n", s->mb_x, s->mb_y);
1997
        return -1;
1998
    }
1999
    if (code == mv->n) {
2000
//printf("MV ESC %X at %d %d\n", show_bits(&s->gb, 24), s->mb_x, s->mb_y);
2001
        mx = get_bits(&s->gb, 6);
2002
        my = get_bits(&s->gb, 6);
2003
    } else {
2004
        mx = mv->table_mvx[code];
2005
        my = mv->table_mvy[code];
2006
    }
2007

    
2008
    mx += *mx_ptr - 32;
2009
    my += *my_ptr - 32;
2010
    /* WARNING : they do not do exactly modulo encoding */
2011
    if (mx <= -64)
2012
        mx += 64;
2013
    else if (mx >= 64)
2014
        mx -= 64;
2015

    
2016
    if (my <= -64)
2017
        my += 64;
2018
    else if (my >= 64)
2019
        my -= 64;
2020
    *mx_ptr = mx;
2021
    *my_ptr = my;
2022
    return 0;
2023
}
2024

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