Statistics
| Branch: | Revision:

ffmpeg / libavcodec / msmpeg4.c @ 7bc9090a

History | View | Annotate | Download (60.3 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

    
22
/**
23
 * @file msmpeg4.c
24
 * MSMPEG4 backend for ffmpeg encoder and decoder.
25
 */
26

    
27
#include "avcodec.h"
28
#include "dsputil.h"
29
#include "mpegvideo.h"
30

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

    
40
#define DC_VLC_BITS 9
41
#define CBPY_VLC_BITS 6
42
#define INTER_INTRA_VLC_BITS 3
43
#define V1_INTRA_CBPC_VLC_BITS 6
44
#define V1_INTER_CBPC_VLC_BITS 6
45
#define V2_INTRA_CBPC_VLC_BITS 3
46
#define V2_MB_TYPE_VLC_BITS 7
47
#define MV_VLC_BITS 9
48
#define V2_MV_VLC_BITS 9
49
#define TEX_VLC_BITS 9
50
#define MB_NON_INTRA_VLC_BITS 9
51
#define MB_INTRA_VLC_BITS 9
52

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

    
56
#define DEFAULT_INTER_INDEX 3
57

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

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

    
81
extern uint32_t inverse[256];
82

    
83

    
84
#ifdef DEBUG
85
int intra_count = 0;
86
int frame_count = 0;
87
#endif
88

    
89
#include "msmpeg4data.h"
90

    
91
static uint8_t rl_length[NB_RL_TABLES][MAX_LEVEL+1][MAX_RUN+1][2];
92

    
93
#ifdef STATS
94

    
95
const char *st_names[ST_NB] = {
96
    "unknown",
97
    "dc",
98
    "intra_ac",
99
    "inter_ac",
100
    "intra_mb",
101
    "inter_mb",
102
    "mv",
103
};
104

    
105
int st_current_index = 0;
106
unsigned int st_bit_counts[ST_NB];
107
unsigned int st_out_bit_counts[ST_NB];
108

    
109
#define set_stat(var) st_current_index = var;
110

    
111
void print_stats(void)
112
{
113
    unsigned int total;
114
    int i;
115

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

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

    
151
#else
152

    
153
#define set_stat(var)
154

    
155
#endif
156

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

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

    
195
        init_h263_dc_for_msmpeg4();
196
    }
197
}
198

    
199
#ifdef CONFIG_ENCODERS
200

    
201
/* build the table which associate a (x,y) motion vector to a vlc */
202
static void init_mv_table(MVTable *tab)
203
{
204
    int i, x, y;
205

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

    
218
static void code012(PutBitContext *pb, int n)
219
{
220
    if (n == 0) {
221
        put_bits(pb, 1, 0);
222
    } else {
223
        put_bits(pb, 1, 1);
224
        put_bits(pb, 1, (n >= 2));
225
    }
226
}
227

    
228
void ff_msmpeg4_encode_init(MpegEncContext *s)
229
{
230
    static int init_done=0;
231
    int i;
232

    
233
    common_init(s);
234
    if(s->msmpeg4_version>=4){
235
        s->min_qcoeff= -255;
236
        s->max_qcoeff=  255;
237
    }
238

    
239
    if (!init_done) {
240
        /* init various encoding tables */
241
        init_done = 1;
242
        init_mv_table(&mv_tables[0]);
243
        init_mv_table(&mv_tables[1]);
244
        for(i=0;i<NB_RL_TABLES;i++)
245
            init_rl(&rl_table[i]);
246

    
247
        for(i=0; i<NB_RL_TABLES; i++){
248
            int level;
249
            for(level=0; level<=MAX_LEVEL; level++){
250
                int run;
251
                for(run=0; run<=MAX_RUN; run++){
252
                    int last;
253
                    for(last=0; last<2; last++){
254
                        rl_length[i][level][run][last]= get_size_of_code(s, &rl_table[  i], last, run, level, 0);
255
                    }
256
                }
257
            }
258
        }
259
    }
260
}
261

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

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

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

    
309
    for(i=0; i<3; i++){
310
        int level;
311
        int chroma_size=0;
312
        int size=0;
313

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

    
350
//    printf("type:%d, best:%d, qp:%d, var:%d, mcvar:%d, size:%d //\n", 
351
//           s->pict_type, best, s->qscale, s->mb_var_sum, s->mc_mb_var_sum, best_size);
352
           
353
    if(s->pict_type==P_TYPE) chroma_best= best;
354

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

    
357
    s->rl_table_index       =        best;
358
    s->rl_chroma_table_index= chroma_best;
359
    
360
    if(s->pict_type != s->last_non_b_pict_type){
361
        s->rl_table_index= 2;
362
        if(s->pict_type==I_TYPE)
363
            s->rl_chroma_table_index= 1;
364
        else
365
            s->rl_chroma_table_index= 2;
366
    }
367

    
368
}
369

    
370
/* write MSMPEG4 compatible frame header */
371
void msmpeg4_encode_picture_header(MpegEncContext * s, int picture_number)
372
{
373
    find_best_tables(s);
374

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

    
378
    put_bits(&s->pb, 5, s->qscale);
379
    if(s->msmpeg4_version<=2){
380
        s->rl_table_index = 2;
381
        s->rl_chroma_table_index = 2;
382
    }
383

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

    
392
    if (s->pict_type == I_TYPE) {
393
        s->slice_height= s->mb_height/1;
394
        put_bits(&s->pb, 5, 0x16 + s->mb_height/s->slice_height);
395
        
396
        if(s->msmpeg4_version==4){
397
            msmpeg4_encode_ext_header(s);
398
            if(s->bit_rate>MBAC_BITRATE)
399
                put_bits(&s->pb, 1, s->per_mb_rl_table);
400
        }
401

    
402
        if(s->msmpeg4_version>2){
403
            if(!s->per_mb_rl_table){
404
                code012(&s->pb, s->rl_chroma_table_index);
405
                code012(&s->pb, s->rl_table_index);
406
            }
407

    
408
            put_bits(&s->pb, 1, s->dc_table_index);
409
        }
410
    } else {
411
        put_bits(&s->pb, 1, s->use_skip_mb_code);
412
        
413
        if(s->msmpeg4_version==4 && s->bit_rate>MBAC_BITRATE)
414
            put_bits(&s->pb, 1, s->per_mb_rl_table);
415

    
416
        if(s->msmpeg4_version>2){
417
            if(!s->per_mb_rl_table)
418
                code012(&s->pb, s->rl_table_index);
419

    
420
            put_bits(&s->pb, 1, s->dc_table_index);
421

    
422
            put_bits(&s->pb, 1, s->mv_table_index);
423
        }
424
    }
425

    
426
    s->esc3_level_length= 0;
427
    s->esc3_run_length= 0;
428

    
429
#ifdef DEBUG
430
    intra_count = 0;
431
    printf("*****frame %d:\n", frame_count++);
432
#endif
433
}
434

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

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

    
441
        if(s->msmpeg4_version>=3)
442
            put_bits(&s->pb, 1, s->flipflop_rounding);
443
        else
444
            assert(s->flipflop_rounding==0);
445
}
446

    
447
#endif //CONFIG_ENCODERS
448

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

    
454
    xy = s->block_index[n];
455
    wrap = s->block_wrap[0];
456

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

    
473
    return pred;
474
}
475

    
476
#ifdef CONFIG_ENCODERS
477

    
478
static void msmpeg4_encode_motion(MpegEncContext * s, 
479
                                  int mx, int my)
480
{
481
    int code;
482
    MVTable *mv;
483

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

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

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

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

    
538
    handle_slices(s);
539
    
540
    if (!s->mb_intra) {
541
        /* compute cbp */
542
        set_stat(ST_INTER_MB);
543
        cbp = 0;
544
        for (i = 0; i < 6; i++) {
545
            if (s->block_last_index[i] >= 0)
546
                cbp |= 1 << (5 - i);
547
        }
548
        if (s->use_skip_mb_code && (cbp | motion_x | motion_y) == 0) {
549
            /* skip macroblock */
550
            put_bits(&s->pb, 1, 1);
551
            s->last_bits++;
552
            s->misc_bits++;
553

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

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

    
570
            s->misc_bits += get_bits_diff(s);
571

    
572
            h263_pred_motion(s, 0, &pred_x, &pred_y);
573
            msmpeg4v2_encode_motion(s, motion_x - pred_x);
574
            msmpeg4v2_encode_motion(s, motion_y - pred_y);
575
        }else{
576
            put_bits(&s->pb, 
577
                     table_mb_non_intra[cbp + 64][1], 
578
                     table_mb_non_intra[cbp + 64][0]);
579

    
580
            s->misc_bits += get_bits_diff(s);
581

    
582
            /* motion vector */
583
            h263_pred_motion(s, 0, &pred_x, &pred_y);
584
            msmpeg4_encode_motion(s, motion_x - pred_x, 
585
                                  motion_y - pred_y);
586
        }
587

    
588
        s->mv_bits += get_bits_diff(s);
589

    
590
        for (i = 0; i < 6; i++) {
591
            msmpeg4_encode_block(s, block[i], i);
592
        }
593
        s->p_tex_bits += get_bits_diff(s);
594
    } else {
595
        /* compute cbp */
596
        cbp = 0;
597
        coded_cbp = 0;
598
        for (i = 0; i < 6; i++) {
599
            int val, pred;
600
            val = (s->block_last_index[i] >= 1);
601
            cbp |= val << (5 - i);
602
            if (i < 4) {
603
                /* predict value for close blocks only for luma */
604
                pred = coded_block_pred(s, i, &coded_block);
605
                *coded_block = val;
606
                val = val ^ pred;
607
            }
608
            coded_cbp |= val << (5 - i);
609
        }
610
#if 0
611
        if (coded_cbp)
612
            printf("cbp=%x %x\n", cbp, coded_cbp);
613
#endif
614

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

    
651
        for (i = 0; i < 6; i++) {
652
            msmpeg4_encode_block(s, block[i], i);
653
        }
654
        s->i_tex_bits += get_bits_diff(s);
655
    }
656
}
657

    
658
#endif //CONFIG_ENCODERS
659

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

    
675
static inline int msmpeg4v1_pred_dc(MpegEncContext * s, int n, 
676
                                    int32_t **dc_val_ptr)
677
{
678
    int i;
679

    
680
    if (n < 4) {
681
        i= 0;
682
    } else {
683
        i= n-3;
684
    }
685
    
686
    *dc_val_ptr= &s->last_dc[i];
687
    return s->last_dc[i]; 
688
}
689

    
690
static int get_dc(uint8_t *src, int stride, int scale)
691
{
692
    int y;
693
    int sum=0;
694
    for(y=0; y<8; y++){
695
        int x;
696
        for(x=0; x<8; x++){
697
            sum+=src[x + y*stride];
698
        }
699
    }
700
    return (sum + (scale>>1))/scale;
701
}
702

    
703
/* dir = 0: left, dir = 1: top prediction */
704
static inline int msmpeg4_pred_dc(MpegEncContext * s, int n, 
705
                             uint16_t **dc_val_ptr, int *dir_ptr)
706
{
707
    int a, b, c, wrap, pred, scale;
708
    int16_t *dc_val;
709

    
710
    /* find prediction */
711
    if (n < 4) {
712
        scale = s->y_dc_scale;
713
    } else {
714
        scale = s->c_dc_scale;
715
    }
716
    
717
    wrap = s->block_wrap[n];
718
    dc_val= s->dc_val[0] + s->block_index[n];
719

    
720
    /* B C
721
     * A X 
722
     */
723
    a = dc_val[ - 1];
724
    b = dc_val[ - 1 - wrap];
725
    c = dc_val[ - wrap];
726
    
727
    if(s->first_slice_line && (n&2)==0 && s->msmpeg4_version<4){
728
        b=c=1024;
729
    }
730

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

    
846
    /* update predictor */
847
    *dc_val_ptr = &dc_val[0];
848
    return pred;
849
}
850

    
851
#define DC_MAX 119
852

    
853
#ifdef CONFIG_ENCODERS
854

    
855
static void msmpeg4_encode_dc(MpegEncContext * s, int level, int n, int *dir_ptr)
856
{
857
    int sign, code;
858
    int pred;
859

    
860
    if(s->msmpeg4_version==1){
861
        int32_t *dc_val;
862
        pred = msmpeg4v1_pred_dc(s, n, &dc_val);
863
        
864
        /* update predictor */
865
        *dc_val= level;
866
    }else{
867
        uint16_t *dc_val;
868
        pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr);
869

    
870
        /* update predictor */
871
        if (n < 4) {
872
            *dc_val = level * s->y_dc_scale;
873
        } else {
874
            *dc_val = level * s->c_dc_scale;
875
        }
876
    }
877

    
878
    /* do the prediction */
879
    level -= pred;
880

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

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

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

    
935
    if (s->mb_intra) {
936
        set_stat(ST_DC);
937
        msmpeg4_encode_dc(s, block[0], n, &dc_pred_dir);
938
        i = 1;
939
        if (n < 4) {
940
            rl = &rl_table[s->rl_table_index];
941
        } else {
942
            rl = &rl_table[3 + s->rl_chroma_table_index];
943
        }
944
        run_diff = 0;
945
        scantable= s->intra_scantable.permutated;
946
        set_stat(ST_INTRA_AC);
947
    } else {
948
        i = 0;
949
        rl = &rl_table[3 + s->rl_table_index];
950
        if(s->msmpeg4_version<=2)
951
            run_diff = 0;
952
        else
953
            run_diff = 1;
954
        scantable= s->inter_scantable.permutated;
955
        set_stat(ST_INTER_AC);
956
    }
957

    
958
    /* recalculate block_last_index for M$ wmv1 */
959
    if(s->msmpeg4_version>=4 && s->block_last_index[n]>0){
960
        for(last_index=63; last_index>=0; last_index--){
961
            if(block[scantable[last_index]]) break;
962
        }
963
        s->block_last_index[n]= last_index;
964
    }else
965
        last_index = s->block_last_index[n];
966
    /* AC coefs */
967
    last_non_zero = i - 1;
968
    for (; i <= last_index; i++) {
969
        j = scantable[i];
970
        level = block[j];
971
        if (level) {
972
            run = i - last_non_zero - 1;
973
            last = (i == last_index);
974
            sign = 0;
975
            slevel = level;
976
            if (level < 0) {
977
                sign = 1;
978
                level = -level;
979
            }
980

    
981
            if(level<=MAX_LEVEL && run<=MAX_RUN){
982
                s->ac_stats[s->mb_intra][n>3][level][run][last]++;
983
            }
984
#if 0
985
else
986
    s->ac_stats[s->mb_intra][n>3][40][63][0]++; //esc3 like
987
#endif
988
            code = get_rl_index(rl, last, run, level);
989
            put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
990
            if (code == rl->n) {
991
                int level1, run1;
992

    
993
                level1 = level - rl->max_level[last][run];
994
                if (level1 < 1) 
995
                    goto esc2;
996
                code = get_rl_index(rl, last, run, level1);
997
                if (code == rl->n) {
998
                esc2:
999
                    put_bits(&s->pb, 1, 0);
1000
                    if (level > MAX_LEVEL)
1001
                        goto esc3;
1002
                    run1 = run - rl->max_run[last][level] - run_diff;
1003
                    if (run1 < 0)
1004
                        goto esc3;
1005
                    code = get_rl_index(rl, last, run1, level);
1006
                    if (code == rl->n) {
1007
                    esc3:
1008
                        /* third escape */
1009
                        put_bits(&s->pb, 1, 0);
1010
                        put_bits(&s->pb, 1, last);
1011
                        if(s->msmpeg4_version>=4){
1012
                            if(s->esc3_level_length==0){
1013
                                s->esc3_level_length=8;
1014
                                s->esc3_run_length= 6;
1015
                                if(s->qscale<8)
1016
                                    put_bits(&s->pb, 6, 3);
1017
                                else
1018
                                    put_bits(&s->pb, 8, 3);
1019
                            }
1020
                            put_bits(&s->pb, s->esc3_run_length, run);
1021
                            put_bits(&s->pb, 1, sign);
1022
                            put_bits(&s->pb, s->esc3_level_length, level);
1023
                        }else{
1024
                            put_bits(&s->pb, 6, run);
1025
                            put_bits(&s->pb, 8, slevel & 0xff);
1026
                        }
1027
                    } else {
1028
                        /* second escape */
1029
                        put_bits(&s->pb, 1, 1);
1030
                        put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
1031
                        put_bits(&s->pb, 1, sign);
1032
                    }
1033
                } else {
1034
                    /* first escape */
1035
                    put_bits(&s->pb, 1, 1);
1036
                    put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
1037
                    put_bits(&s->pb, 1, sign);
1038
                }
1039
            } else {
1040
                put_bits(&s->pb, 1, sign);
1041
            }
1042
            last_non_zero = i;
1043
        }
1044
    }
1045
}
1046

    
1047
#endif //CONFIG_ENCODERS
1048

    
1049
/****************************************/
1050
/* decoding stuff */
1051

    
1052
static VLC mb_non_intra_vlc[4];
1053
static VLC mb_intra_vlc;
1054
static VLC dc_lum_vlc[2];
1055
static VLC dc_chroma_vlc[2];
1056
static VLC v2_dc_lum_vlc;
1057
static VLC v2_dc_chroma_vlc;
1058
static VLC cbpy_vlc;
1059
static VLC v2_intra_cbpc_vlc;
1060
static VLC v2_mb_type_vlc;
1061
static VLC v2_mv_vlc;
1062
static VLC v1_intra_cbpc_vlc;
1063
static VLC v1_inter_cbpc_vlc;
1064
static VLC inter_intra_vlc;
1065

    
1066
/* this table is practically identical to the one from h263 except that its inverted */
1067
static void init_h263_dc_for_msmpeg4(void)
1068
{
1069
        int level, uni_code, uni_len;
1070

    
1071
        for(level=-256; level<256; level++){
1072
            int size, v, l;
1073
            /* find number of bits */
1074
            size = 0;
1075
            v = abs(level);
1076
            while (v) {
1077
                v >>= 1;
1078
                    size++;
1079
            }
1080

    
1081
            if (level < 0)
1082
                l= (-level) ^ ((1 << size) - 1);
1083
            else
1084
                l= level;
1085

    
1086
            /* luminance h263 */
1087
            uni_code= DCtab_lum[size][0];
1088
            uni_len = DCtab_lum[size][1];
1089
            uni_code ^= (1<<uni_len)-1; //M$ doesnt like compatibility
1090

    
1091
            if (size > 0) {
1092
                uni_code<<=size; uni_code|=l;
1093
                uni_len+=size;
1094
                if (size > 8){
1095
                    uni_code<<=1; uni_code|=1;
1096
                    uni_len++;
1097
                }
1098
            }
1099
            v2_dc_lum_table[level+256][0]= uni_code;
1100
            v2_dc_lum_table[level+256][1]= uni_len;
1101

    
1102
            /* chrominance h263 */
1103
            uni_code= DCtab_chrom[size][0];
1104
            uni_len = DCtab_chrom[size][1];
1105
            uni_code ^= (1<<uni_len)-1; //M$ doesnt like compatibility
1106
            
1107
            if (size > 0) {
1108
                uni_code<<=size; uni_code|=l;
1109
                uni_len+=size;
1110
                if (size > 8){
1111
                    uni_code<<=1; uni_code|=1;
1112
                    uni_len++;
1113
                }
1114
            }
1115
            v2_dc_chroma_table[level+256][0]= uni_code;
1116
            v2_dc_chroma_table[level+256][1]= uni_len;
1117

    
1118
        }
1119
}
1120

    
1121
/* init all vlc decoding tables */
1122
int ff_msmpeg4_decode_init(MpegEncContext *s)
1123
{
1124
    static int done = 0;
1125
    int i;
1126
    MVTable *mv;
1127

    
1128
    common_init(s);
1129

    
1130
    if (!done) {
1131
        done = 1;
1132

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

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

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

    
1218
static int decode012(GetBitContext *gb)
1219
{
1220
    int n;
1221
    n = get_bits1(gb);
1222
    if (n == 0)
1223
        return 0;
1224
    else
1225
        return get_bits1(gb) + 1;
1226
}
1227

    
1228
int msmpeg4_decode_picture_header(MpegEncContext * s)
1229
{
1230
    int code;
1231

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

    
1243
    if(s->msmpeg4_version==1){
1244
        int start_code, num;
1245
        start_code = (get_bits(&s->gb, 16)<<16) | get_bits(&s->gb, 16);
1246
        if(start_code!=0x00000100){
1247
            fprintf(stderr, "invalid startcode\n");
1248
            return -1;
1249
        }
1250

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

    
1254
    s->pict_type = get_bits(&s->gb, 2) + 1;
1255
    if (s->pict_type != I_TYPE &&
1256
        s->pict_type != P_TYPE){
1257
        fprintf(stderr, "invalid picture type\n");
1258
        return -1;
1259
    }
1260
#if 0
1261
{
1262
    static int had_i=0;
1263
    if(s->pict_type == I_TYPE) had_i=1;
1264
    if(!had_i) return -1;
1265
}
1266
#endif
1267
    s->qscale = get_bits(&s->gb, 5);
1268
    if(s->qscale==0){
1269
        fprintf(stderr, "invalid qscale\n");
1270
        return -1;
1271
    }
1272

    
1273
    if (s->pict_type == I_TYPE) {
1274
        code = get_bits(&s->gb, 5); 
1275
        if(s->msmpeg4_version==1){
1276
            if(code==0 || code>s->mb_height){
1277
                fprintf(stderr, "invalid slice height %d\n", code);
1278
                return -1;
1279
            }
1280

    
1281
            s->slice_height = code;
1282
        }else{
1283
            /* 0x17: one slice, 0x18: two slices, ... */
1284
            if (code < 0x17){
1285
                fprintf(stderr, "error, slice code was %X\n", code);
1286
                return -1;
1287
            }
1288

    
1289
            s->slice_height = s->mb_height / (code - 0x16);
1290
        }
1291

    
1292
        switch(s->msmpeg4_version){
1293
        case 1:
1294
        case 2:
1295
            s->rl_chroma_table_index = 2;
1296
            s->rl_table_index = 2;
1297

    
1298
            s->dc_table_index = 0; //not used
1299
            break;
1300
        case 3:
1301
            s->rl_chroma_table_index = decode012(&s->gb);
1302
            s->rl_table_index = decode012(&s->gb);
1303

    
1304
            s->dc_table_index = get_bits1(&s->gb);
1305
            break;
1306
        case 4:
1307
            msmpeg4_decode_ext_header(s, (2+5+5+17+7)/8);
1308

    
1309
            if(s->bit_rate > MBAC_BITRATE) s->per_mb_rl_table= get_bits1(&s->gb);
1310
            else                           s->per_mb_rl_table= 0;
1311
            
1312
            if(!s->per_mb_rl_table){
1313
                s->rl_chroma_table_index = decode012(&s->gb);
1314
                s->rl_table_index = decode012(&s->gb);
1315
            }
1316

    
1317
            s->dc_table_index = get_bits1(&s->gb);
1318
            s->inter_intra_pred= 0;
1319
            break;
1320
        }
1321
        s->no_rounding = 1;
1322
/*        printf("qscale:%d rlc:%d rl:%d dc:%d mbrl:%d slice:%d   \n", 
1323
                s->qscale,
1324
                s->rl_chroma_table_index,
1325
                s->rl_table_index, 
1326
                s->dc_table_index,
1327
                s->per_mb_rl_table,
1328
                s->slice_height);*/
1329
    } else {
1330
        switch(s->msmpeg4_version){
1331
        case 1:
1332
        case 2:
1333
            if(s->msmpeg4_version==1)
1334
                s->use_skip_mb_code = 1;
1335
            else
1336
                s->use_skip_mb_code = get_bits1(&s->gb);
1337
            s->rl_table_index = 2;
1338
            s->rl_chroma_table_index = s->rl_table_index;
1339
            s->dc_table_index = 0; //not used
1340
            s->mv_table_index = 0;
1341
            break;
1342
        case 3:
1343
            s->use_skip_mb_code = get_bits1(&s->gb);
1344
            s->rl_table_index = decode012(&s->gb);
1345
            s->rl_chroma_table_index = s->rl_table_index;
1346

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

    
1349
            s->mv_table_index = get_bits1(&s->gb);
1350
            break;
1351
        case 4:
1352
            s->use_skip_mb_code = get_bits1(&s->gb);
1353

    
1354
            if(s->bit_rate > MBAC_BITRATE) s->per_mb_rl_table= get_bits1(&s->gb);
1355
            else                           s->per_mb_rl_table= 0;
1356

    
1357
            if(!s->per_mb_rl_table){
1358
                s->rl_table_index = decode012(&s->gb);
1359
                s->rl_chroma_table_index = s->rl_table_index;
1360
            }
1361

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

    
1364
            s->mv_table_index = get_bits1(&s->gb);
1365
            s->inter_intra_pred= (s->width*s->height < 320*240 && s->bit_rate<=II_BITRATE);
1366
            break;
1367
        }
1368
/*        printf("skip:%d rl:%d rlc:%d dc:%d mv:%d mbrl:%d qp:%d   \n", 
1369
                s->use_skip_mb_code, 
1370
                s->rl_table_index, 
1371
                s->rl_chroma_table_index, 
1372
                s->dc_table_index,
1373
                s->mv_table_index,
1374
                s->per_mb_rl_table,
1375
                s->qscale);*/
1376
        if(s->flipflop_rounding){
1377
            s->no_rounding ^= 1;
1378
        }else{
1379
            s->no_rounding = 0;
1380
        }
1381
    }
1382
//printf("%d %d %d %d %d\n", s->pict_type, s->bit_rate, s->inter_intra_pred, s->width, s->height);
1383

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

    
1387
#ifdef DEBUG
1388
    printf("*****frame %d:\n", frame_count++);
1389
#endif
1390
    return 0;
1391
}
1392

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

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

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

    
1421
    return 0;
1422
}
1423

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

    
1431
#ifdef CONFIG_ENCODERS
1432

    
1433
static void msmpeg4v2_encode_motion(MpegEncContext * s, int val)
1434
{
1435
    int range, bit_size, sign, code, bits;
1436

    
1437
    if (val == 0) {
1438
        /* zero vector */
1439
        code = 0;
1440
        put_bits(&s->pb, mvtab[code][1], mvtab[code][0]);
1441
    } else {
1442
        bit_size = s->f_code - 1;
1443
        range = 1 << bit_size;
1444
        if (val <= -64)
1445
            val += 64;
1446
        else if (val >= 64)
1447
            val -= 64;
1448

    
1449
        if (val >= 0) {
1450
            sign = 0;
1451
        } else {
1452
            val = -val;
1453
            sign = 1;
1454
        }
1455
        val--;
1456
        code = (val >> bit_size) + 1;
1457
        bits = val & (range - 1);
1458

    
1459
        put_bits(&s->pb, mvtab[code][1] + 1, (mvtab[code][0] << 1) | sign); 
1460
        if (bit_size > 0) {
1461
            put_bits(&s->pb, bit_size, bits);
1462
        }
1463
    }
1464
}
1465

    
1466
#endif //CONFIG_ENCODERS
1467

    
1468
/* this is identical to h263 except that its range is multiplied by 2 */
1469
static int msmpeg4v2_decode_motion(MpegEncContext * s, int pred, int f_code)
1470
{
1471
    int code, val, sign, shift;
1472

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

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

    
1489
    val += pred;
1490
    if (val <= -64)
1491
        val += 64;
1492
    else if (val >= 64)
1493
        val -= 64;
1494

    
1495
    return val;
1496
}
1497

    
1498
static int msmpeg4v12_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
1499
{
1500
    int cbp, code, i;
1501
    
1502
    if (s->pict_type == P_TYPE) {
1503
        if (s->use_skip_mb_code) {
1504
            if (get_bits1(&s->gb)) {
1505
                /* skip mb */
1506
                s->mb_intra = 0;
1507
                for(i=0;i<6;i++)
1508
                    s->block_last_index[i] = -1;
1509
                s->mv_dir = MV_DIR_FORWARD;
1510
                s->mv_type = MV_TYPE_16X16;
1511
                s->mv[0][0][0] = 0;
1512
                s->mv[0][0][1] = 0;
1513
                s->mb_skiped = 1;
1514
                return 0;
1515
            }
1516
        }
1517

    
1518
        if(s->msmpeg4_version==2)
1519
            code = get_vlc2(&s->gb, v2_mb_type_vlc.table, V2_MB_TYPE_VLC_BITS, 1);
1520
        else
1521
            code = get_vlc2(&s->gb, v1_inter_cbpc_vlc.table, V1_INTER_CBPC_VLC_BITS, 3);
1522
        if(code<0 || code>7){
1523
            fprintf(stderr, "cbpc %d invalid at %d %d\n", code, s->mb_x, s->mb_y);
1524
            return -1;
1525
        }
1526

    
1527
        s->mb_intra = code >>2;
1528
    
1529
        cbp = code & 0x3;
1530
    } else {
1531
        s->mb_intra = 1;
1532
        if(s->msmpeg4_version==2)
1533
            cbp= get_vlc2(&s->gb, v2_intra_cbpc_vlc.table, V2_INTRA_CBPC_VLC_BITS, 1);
1534
        else
1535
            cbp= get_vlc2(&s->gb, v1_intra_cbpc_vlc.table, V1_INTRA_CBPC_VLC_BITS, 1);
1536
        if(cbp<0 || cbp>3){
1537
            fprintf(stderr, "cbpc %d invalid at %d %d\n", cbp, s->mb_x, s->mb_y);
1538
            return -1;
1539
        }
1540
    }
1541

    
1542
    if (!s->mb_intra) {
1543
        int mx, my, cbpy;
1544
        
1545
        cbpy= get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1);
1546
        if(cbpy<0){
1547
            fprintf(stderr, "cbpy %d invalid at %d %d\n", cbp, s->mb_x, s->mb_y);
1548
            return -1;
1549
        }
1550

    
1551
        cbp|= cbpy<<2;
1552
        if(s->msmpeg4_version==1 || (cbp&3) != 3) cbp^= 0x3C;
1553
        
1554
        h263_pred_motion(s, 0, &mx, &my);
1555
        mx= msmpeg4v2_decode_motion(s, mx, 1);
1556
        my= msmpeg4v2_decode_motion(s, my, 1);
1557
        
1558
        s->mv_dir = MV_DIR_FORWARD;
1559
        s->mv_type = MV_TYPE_16X16;
1560
        s->mv[0][0][0] = mx;
1561
        s->mv[0][0][1] = my;
1562
    } else {
1563
        if(s->msmpeg4_version==2){
1564
            s->ac_pred = get_bits1(&s->gb);
1565
            cbp|= get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1)<<2; //FIXME check errors
1566
        } else{
1567
            s->ac_pred = 0;
1568
            cbp|= get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1)<<2; //FIXME check errors
1569
            if(s->pict_type==P_TYPE) cbp^=0x3C;
1570
        }
1571
    }
1572

    
1573
    for (i = 0; i < 6; i++) {
1574
        if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, NULL) < 0)
1575
        {
1576
             fprintf(stderr,"\nerror while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
1577
             return -1;
1578
        }
1579
    }
1580
    return 0;
1581
}
1582

    
1583
static int msmpeg4v34_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
1584
{
1585
    int cbp, code, i;
1586
    uint8_t *coded_val;
1587
    uint32_t * const mb_type_ptr= &s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ];
1588

    
1589
    if (s->pict_type == P_TYPE) {
1590
        set_stat(ST_INTER_MB);
1591
        if (s->use_skip_mb_code) {
1592
            if (get_bits1(&s->gb)) {
1593
                /* skip mb */
1594
                s->mb_intra = 0;
1595
                for(i=0;i<6;i++)
1596
                    s->block_last_index[i] = -1;
1597
                s->mv_dir = MV_DIR_FORWARD;
1598
                s->mv_type = MV_TYPE_16X16;
1599
                s->mv[0][0][0] = 0;
1600
                s->mv[0][0][1] = 0;
1601
                s->mb_skiped = 1;
1602
                *mb_type_ptr = MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
1603

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

    
1634
    if (!s->mb_intra) {
1635
        int mx, my;
1636
//printf("P at %d %d\n", s->mb_x, s->mb_y);
1637
        if(s->per_mb_rl_table && cbp){
1638
            s->rl_table_index = decode012(&s->gb);
1639
            s->rl_chroma_table_index = s->rl_table_index;
1640
        }
1641
        set_stat(ST_MV);
1642
        h263_pred_motion(s, 0, &mx, &my);
1643
        if (msmpeg4_decode_motion(s, &mx, &my) < 0)
1644
            return -1;
1645
        s->mv_dir = MV_DIR_FORWARD;
1646
        s->mv_type = MV_TYPE_16X16;
1647
        s->mv[0][0][0] = mx;
1648
        s->mv[0][0][1] = my;
1649
        *mb_type_ptr = MB_TYPE_L0 | MB_TYPE_16x16;
1650
    } else {
1651
//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));
1652
        set_stat(ST_INTRA_MB);
1653
        s->ac_pred = get_bits1(&s->gb);
1654
        *mb_type_ptr = MB_TYPE_INTRA;
1655
        if(s->inter_intra_pred){
1656
            s->h263_aic_dir= get_vlc2(&s->gb, inter_intra_vlc.table, INTER_INTRA_VLC_BITS, 1);
1657
//            printf("%d%d %d %d/", s->ac_pred, s->h263_aic_dir, s->mb_x, s->mb_y);
1658
        }
1659
        if(s->per_mb_rl_table && cbp){
1660
            s->rl_table_index = decode012(&s->gb);
1661
            s->rl_chroma_table_index = s->rl_table_index;
1662
        }
1663
    }
1664

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

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

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

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

    
1734
        if(s->msmpeg4_version==2)
1735
            run_diff = 0;
1736
        else
1737
            run_diff = 1;
1738

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

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

    
1905
            block[scan_table[i]] = level;
1906
            break;
1907
        }
1908

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

    
1926
static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr)
1927
{
1928
    int level, pred;
1929

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

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

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

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

    
1980
    return level;
1981
}
1982

    
1983
static int msmpeg4_decode_motion(MpegEncContext * s, 
1984
                                 int *mx_ptr, int *my_ptr)
1985
{
1986
    MVTable *mv;
1987
    int code, mx, my;
1988

    
1989
    mv = &mv_tables[s->mv_table_index];
1990

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

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

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

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