Statistics
| Branch: | Revision:

ffmpeg / libavcodec / msmpeg4.c @ 983e3246

History | View | Annotate | Download (60.1 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
//#define PRINT_MB
31

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

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

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

    
57
#define DEFAULT_INTER_INDEX 3
58

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

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

    
82
extern uint32_t inverse[256];
83

    
84

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

    
90
#include "msmpeg4data.h"
91

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

    
94
#ifdef STATS
95

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

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

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

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

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

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

    
152
#else
153

    
154
#define set_stat(var)
155

    
156
#endif
157

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

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

    
196
        init_h263_dc_for_msmpeg4();
197
    }
198
}
199

    
200
#ifdef CONFIG_ENCODERS
201

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
369
}
370

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
442
        if(s->msmpeg4_version<3)
443
            s->flipflop_rounding=0;
444
        else{
445
            s->flipflop_rounding=1;
446
            put_bits(&s->pb, 1, s->flipflop_rounding);
447
        }
448
}
449

    
450
#endif //CONFIG_ENCODERS
451

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

    
457
    xy = s->block_index[n];
458
    wrap = s->block_wrap[0];
459

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

    
476
    return pred;
477
}
478

    
479
#ifdef CONFIG_ENCODERS
480

    
481
static void msmpeg4_encode_motion(MpegEncContext * s, 
482
                                  int mx, int my)
483
{
484
    int code;
485
    MVTable *mv;
486

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

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

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

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

    
541
    handle_slices(s);
542
    
543
    if (!s->mb_intra) {
544
        /* compute cbp */
545
        set_stat(ST_INTER_MB);
546
        cbp = 0;
547
        for (i = 0; i < 6; i++) {
548
            if (s->block_last_index[i] >= 0)
549
                cbp |= 1 << (5 - i);
550
        }
551
        if (s->use_skip_mb_code && (cbp | motion_x | motion_y) == 0) {
552
            /* skip macroblock */
553
            put_bits(&s->pb, 1, 1);
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
            h263_pred_motion(s, 0, &pred_x, &pred_y);
571
            msmpeg4v2_encode_motion(s, motion_x - pred_x);
572
            msmpeg4v2_encode_motion(s, motion_y - pred_y);
573
        }else{
574
            put_bits(&s->pb, 
575
                     table_mb_non_intra[cbp + 64][1], 
576
                     table_mb_non_intra[cbp + 64][0]);
577

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

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

    
640
    for (i = 0; i < 6; i++) {
641
        msmpeg4_encode_block(s, block[i], i);
642
    }
643
}
644

    
645
#endif //CONFIG_ENCODERS
646

    
647
/* old ffmpeg msmpeg4v3 mode */
648
static void ff_old_msmpeg4_dc_scale(MpegEncContext * s)
649
{
650
    if (s->qscale < 5){
651
        s->y_dc_scale = 8;
652
        s->c_dc_scale = 8;
653
    }else if (s->qscale < 9){
654
        s->y_dc_scale = 2 * s->qscale;
655
        s->c_dc_scale = (s->qscale + 13)>>1;
656
    }else{
657
        s->y_dc_scale = s->qscale + 8;
658
        s->c_dc_scale = (s->qscale + 13)>>1;
659
    }
660
}
661

    
662
static inline int msmpeg4v1_pred_dc(MpegEncContext * s, int n, 
663
                                    int32_t **dc_val_ptr)
664
{
665
    int i;
666

    
667
    if (n < 4) {
668
        i= 0;
669
    } else {
670
        i= n-3;
671
    }
672
    
673
    *dc_val_ptr= &s->last_dc[i];
674
    return s->last_dc[i]; 
675
}
676

    
677
static int get_dc(uint8_t *src, int stride, int scale)
678
{
679
    int y;
680
    int sum=0;
681
    for(y=0; y<8; y++){
682
        int x;
683
        for(x=0; x<8; x++){
684
            sum+=src[x + y*stride];
685
        }
686
    }
687
    return (sum + (scale>>1))/scale;
688
}
689

    
690
/* dir = 0: left, dir = 1: top prediction */
691
static inline int msmpeg4_pred_dc(MpegEncContext * s, int n, 
692
                             uint16_t **dc_val_ptr, int *dir_ptr)
693
{
694
    int a, b, c, wrap, pred, scale;
695
    int16_t *dc_val;
696

    
697
    /* find prediction */
698
    if (n < 4) {
699
        scale = s->y_dc_scale;
700
    } else {
701
        scale = s->c_dc_scale;
702
    }
703
    
704
    wrap = s->block_wrap[n];
705
    dc_val= s->dc_val[0] + s->block_index[n];
706

    
707
    /* B C
708
     * A X 
709
     */
710
    a = dc_val[ - 1];
711
    b = dc_val[ - 1 - wrap];
712
    c = dc_val[ - wrap];
713
    
714
    if(s->first_slice_line && (n&2)==0 && s->msmpeg4_version<4){
715
        b=c=1024;
716
    }
717

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

    
833
    /* update predictor */
834
    *dc_val_ptr = &dc_val[0];
835
    return pred;
836
}
837

    
838
#define DC_MAX 119
839

    
840
#ifdef CONFIG_ENCODERS
841

    
842
static void msmpeg4_encode_dc(MpegEncContext * s, int level, int n, int *dir_ptr)
843
{
844
    int sign, code;
845
    int pred;
846

    
847
    if(s->msmpeg4_version==1){
848
        int32_t *dc_val;
849
        pred = msmpeg4v1_pred_dc(s, n, &dc_val);
850
        
851
        /* update predictor */
852
        *dc_val= level;
853
    }else{
854
        uint16_t *dc_val;
855
        pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr);
856

    
857
        /* update predictor */
858
        if (n < 4) {
859
            *dc_val = level * s->y_dc_scale;
860
        } else {
861
            *dc_val = level * s->c_dc_scale;
862
        }
863
    }
864

    
865
    /* do the prediction */
866
    level -= pred;
867

    
868
    if(s->msmpeg4_version<=2){
869
        if (n < 4) {
870
            put_bits(&s->pb, 
871
                     v2_dc_lum_table[level+256][1],
872
                     v2_dc_lum_table[level+256][0]);
873
        }else{
874
            put_bits(&s->pb, 
875
                     v2_dc_chroma_table[level+256][1],
876
                     v2_dc_chroma_table[level+256][0]);
877
        }
878
    }else{
879
        sign = 0;
880
        if (level < 0) {
881
            level = -level;
882
            sign = 1;
883
        }
884
        code = level;
885
        if (code > DC_MAX) 
886
            code = DC_MAX;
887

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

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

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

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

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

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

    
1034
#endif //CONFIG_ENCODERS
1035

    
1036
/****************************************/
1037
/* decoding stuff */
1038

    
1039
static VLC mb_non_intra_vlc[4];
1040
static VLC mb_intra_vlc;
1041
static VLC dc_lum_vlc[2];
1042
static VLC dc_chroma_vlc[2];
1043
static VLC v2_dc_lum_vlc;
1044
static VLC v2_dc_chroma_vlc;
1045
static VLC cbpy_vlc;
1046
static VLC v2_intra_cbpc_vlc;
1047
static VLC v2_mb_type_vlc;
1048
static VLC v2_mv_vlc;
1049
static VLC v1_intra_cbpc_vlc;
1050
static VLC v1_inter_cbpc_vlc;
1051
static VLC inter_intra_vlc;
1052

    
1053
/* this table is practically identical to the one from h263 except that its inverted */
1054
static void init_h263_dc_for_msmpeg4(void)
1055
{
1056
        int level, uni_code, uni_len;
1057

    
1058
        for(level=-256; level<256; level++){
1059
            int size, v, l;
1060
            /* find number of bits */
1061
            size = 0;
1062
            v = abs(level);
1063
            while (v) {
1064
                v >>= 1;
1065
                    size++;
1066
            }
1067

    
1068
            if (level < 0)
1069
                l= (-level) ^ ((1 << size) - 1);
1070
            else
1071
                l= level;
1072

    
1073
            /* luminance h263 */
1074
            uni_code= DCtab_lum[size][0];
1075
            uni_len = DCtab_lum[size][1];
1076
            uni_code ^= (1<<uni_len)-1; //M$ doesnt like compatibility
1077

    
1078
            if (size > 0) {
1079
                uni_code<<=size; uni_code|=l;
1080
                uni_len+=size;
1081
                if (size > 8){
1082
                    uni_code<<=1; uni_code|=1;
1083
                    uni_len++;
1084
                }
1085
            }
1086
            v2_dc_lum_table[level+256][0]= uni_code;
1087
            v2_dc_lum_table[level+256][1]= uni_len;
1088

    
1089
            /* chrominance h263 */
1090
            uni_code= DCtab_chrom[size][0];
1091
            uni_len = DCtab_chrom[size][1];
1092
            uni_code ^= (1<<uni_len)-1; //M$ doesnt like compatibility
1093
            
1094
            if (size > 0) {
1095
                uni_code<<=size; uni_code|=l;
1096
                uni_len+=size;
1097
                if (size > 8){
1098
                    uni_code<<=1; uni_code|=1;
1099
                    uni_len++;
1100
                }
1101
            }
1102
            v2_dc_chroma_table[level+256][0]= uni_code;
1103
            v2_dc_chroma_table[level+256][1]= uni_len;
1104

    
1105
        }
1106
}
1107

    
1108
/* init all vlc decoding tables */
1109
int ff_msmpeg4_decode_init(MpegEncContext *s)
1110
{
1111
    static int done = 0;
1112
    int i;
1113
    MVTable *mv;
1114

    
1115
    common_init(s);
1116

    
1117
    if (!done) {
1118
        done = 1;
1119

    
1120
        for(i=0;i<NB_RL_TABLES;i++) {
1121
            init_rl(&rl_table[i]);
1122
            init_vlc_rl(&rl_table[i]);
1123
        }
1124
        for(i=0;i<2;i++) {
1125
            mv = &mv_tables[i];
1126
            init_vlc(&mv->vlc, MV_VLC_BITS, mv->n + 1, 
1127
                     mv->table_mv_bits, 1, 1,
1128
                     mv->table_mv_code, 2, 2);
1129
        }
1130

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

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

    
1205
static int decode012(GetBitContext *gb)
1206
{
1207
    int n;
1208
    n = get_bits1(gb);
1209
    if (n == 0)
1210
        return 0;
1211
    else
1212
        return get_bits1(gb) + 1;
1213
}
1214

    
1215
int msmpeg4_decode_picture_header(MpegEncContext * s)
1216
{
1217
    int code;
1218

    
1219
#if 0
1220
{
1221
int i;
1222
for(i=0; i<s->gb.size_in_bits; i++)
1223
    printf("%d", get_bits1(&s->gb));
1224
//    get_bits1(&s->gb);
1225
printf("END\n");
1226
return -1;
1227
}
1228
#endif
1229

    
1230
    if(s->msmpeg4_version==1){
1231
        int start_code, num;
1232
        start_code = (get_bits(&s->gb, 16)<<16) | get_bits(&s->gb, 16);
1233
        if(start_code!=0x00000100){
1234
            fprintf(stderr, "invalid startcode\n");
1235
            return -1;
1236
        }
1237

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

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

    
1260
    if (s->pict_type == I_TYPE) {
1261
        code = get_bits(&s->gb, 5); 
1262
        if(s->msmpeg4_version==1){
1263
            if(code==0 || code>s->mb_height){
1264
                fprintf(stderr, "invalid slice height %d\n", code);
1265
                return -1;
1266
            }
1267

    
1268
            s->slice_height = code;
1269
        }else{
1270
            /* 0x17: one slice, 0x18: two slices, ... */
1271
            if (code < 0x17){
1272
                fprintf(stderr, "error, slice code was %X\n", code);
1273
                return -1;
1274
            }
1275

    
1276
            s->slice_height = s->mb_height / (code - 0x16);
1277
        }
1278

    
1279
        switch(s->msmpeg4_version){
1280
        case 1:
1281
        case 2:
1282
            s->rl_chroma_table_index = 2;
1283
            s->rl_table_index = 2;
1284

    
1285
            s->dc_table_index = 0; //not used
1286
            break;
1287
        case 3:
1288
            s->rl_chroma_table_index = decode012(&s->gb);
1289
            s->rl_table_index = decode012(&s->gb);
1290

    
1291
            s->dc_table_index = get_bits1(&s->gb);
1292
            break;
1293
        case 4:
1294
            msmpeg4_decode_ext_header(s, (2+5+5+17+7)/8);
1295

    
1296
            if(s->bit_rate > MBAC_BITRATE) s->per_mb_rl_table= get_bits1(&s->gb);
1297
            else                           s->per_mb_rl_table= 0;
1298
            
1299
            if(!s->per_mb_rl_table){
1300
                s->rl_chroma_table_index = decode012(&s->gb);
1301
                s->rl_table_index = decode012(&s->gb);
1302
            }
1303

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

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

    
1336
            s->mv_table_index = get_bits1(&s->gb);
1337
            break;
1338
        case 4:
1339
            s->use_skip_mb_code = get_bits1(&s->gb);
1340

    
1341
            if(s->bit_rate > MBAC_BITRATE) s->per_mb_rl_table= get_bits1(&s->gb);
1342
            else                           s->per_mb_rl_table= 0;
1343

    
1344
            if(!s->per_mb_rl_table){
1345
                s->rl_table_index = decode012(&s->gb);
1346
                s->rl_chroma_table_index = s->rl_table_index;
1347
            }
1348

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

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

    
1371
    s->esc3_level_length= 0;
1372
    s->esc3_run_length= 0;
1373

    
1374
#ifdef DEBUG
1375
    printf("*****frame %d:\n", frame_count++);
1376
#endif
1377
    return 0;
1378
}
1379

    
1380
int msmpeg4_decode_ext_header(MpegEncContext * s, int buf_size)
1381
{
1382
    int left= buf_size*8 - get_bits_count(&s->gb);
1383
    int length= s->msmpeg4_version>=3 ? 17 : 16;
1384
    /* the alt_bitstream reader could read over the end so we need to check it */
1385
    if(left>=length && left<length+8)
1386
    {
1387
        int fps;
1388

    
1389
        fps= get_bits(&s->gb, 5);
1390
        s->bit_rate= get_bits(&s->gb, 11)*1024;
1391
        if(s->msmpeg4_version>=3)
1392
            s->flipflop_rounding= get_bits1(&s->gb);
1393
        else
1394
            s->flipflop_rounding= 0;
1395

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

    
1408
    return 0;
1409
}
1410

    
1411
static inline void msmpeg4_memsetw(short *tab, int val, int n)
1412
{
1413
    int i;
1414
    for(i=0;i<n;i++)
1415
        tab[i] = val;
1416
}
1417

    
1418
#ifdef CONFIG_ENCODERS
1419

    
1420
static void msmpeg4v2_encode_motion(MpegEncContext * s, int val)
1421
{
1422
    int range, bit_size, sign, code, bits;
1423

    
1424
    if (val == 0) {
1425
        /* zero vector */
1426
        code = 0;
1427
        put_bits(&s->pb, mvtab[code][1], mvtab[code][0]);
1428
    } else {
1429
        bit_size = s->f_code - 1;
1430
        range = 1 << bit_size;
1431
        if (val <= -64)
1432
            val += 64;
1433
        else if (val >= 64)
1434
            val -= 64;
1435

    
1436
        if (val >= 0) {
1437
            sign = 0;
1438
        } else {
1439
            val = -val;
1440
            sign = 1;
1441
        }
1442
        val--;
1443
        code = (val >> bit_size) + 1;
1444
        bits = val & (range - 1);
1445

    
1446
        put_bits(&s->pb, mvtab[code][1] + 1, (mvtab[code][0] << 1) | sign); 
1447
        if (bit_size > 0) {
1448
            put_bits(&s->pb, bit_size, bits);
1449
        }
1450
    }
1451
}
1452

    
1453
#endif //CONFIG_ENCODERS
1454

    
1455
/* this is identical to h263 except that its range is multiplied by 2 */
1456
static int msmpeg4v2_decode_motion(MpegEncContext * s, int pred, int f_code)
1457
{
1458
    int code, val, sign, shift;
1459

    
1460
    code = get_vlc2(&s->gb, v2_mv_vlc.table, V2_MV_VLC_BITS, 2);
1461
//     printf("MV code %d at %d %d pred: %d\n", code, s->mb_x,s->mb_y, pred);
1462
    if (code < 0)
1463
        return 0xffff;
1464

    
1465
    if (code == 0)
1466
        return pred;
1467
    sign = get_bits1(&s->gb);
1468
    shift = f_code - 1;
1469
    val = (code - 1) << shift;
1470
    if (shift > 0)
1471
        val |= get_bits(&s->gb, shift);
1472
    val++;
1473
    if (sign)
1474
        val = -val;
1475

    
1476
    val += pred;
1477
    if (val <= -64)
1478
        val += 64;
1479
    else if (val >= 64)
1480
        val -= 64;
1481

    
1482
    return val;
1483
}
1484

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

    
1507
        if(s->msmpeg4_version==2)
1508
            code = get_vlc2(&s->gb, v2_mb_type_vlc.table, V2_MB_TYPE_VLC_BITS, 1);
1509
        else
1510
            code = get_vlc2(&s->gb, v1_inter_cbpc_vlc.table, V1_INTER_CBPC_VLC_BITS, 3);
1511
        if(code<0 || code>7){
1512
            fprintf(stderr, "cbpc %d invalid at %d %d\n", code, s->mb_x, s->mb_y);
1513
            return -1;
1514
        }
1515

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

    
1531
    if (!s->mb_intra) {
1532
        int mx, my, cbpy;
1533
        
1534
        cbpy= get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1);
1535
        if(cbpy<0){
1536
            fprintf(stderr, "cbpy %d invalid at %d %d\n", cbp, s->mb_x, s->mb_y);
1537
            return -1;
1538
        }
1539

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

    
1562
    for (i = 0; i < 6; i++) {
1563
        if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, NULL) < 0)
1564
        {
1565
             fprintf(stderr,"\nerror while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
1566
             return -1;
1567
        }
1568
    }
1569
    return 0;
1570
}
1571

    
1572
static int msmpeg4v34_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
1573
{
1574
    int cbp, code, i;
1575
    uint8_t *coded_val;
1576

    
1577
#ifdef PRINT_MB
1578
if(s->mb_x==0){
1579
    printf("\n");
1580
    if(s->mb_y==0) printf("\n");
1581
}
1582
#endif
1583

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

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

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

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

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

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

    
1743
        if(s->msmpeg4_version==2)
1744
            run_diff = 0;
1745
        else
1746
            run_diff = 1;
1747

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

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

    
1914
            block[scan_table[i]] = level;
1915
            break;
1916
        }
1917

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

    
1935
static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr)
1936
{
1937
    int level, pred;
1938

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

    
1959
        if (level == DC_MAX) {
1960
            level = get_bits(&s->gb, 8);
1961
            if (get_bits1(&s->gb))
1962
                level = -level;
1963
        } else if (level != 0) {
1964
            if (get_bits1(&s->gb))
1965
                level = -level;
1966
        }
1967
    }
1968

    
1969
    if(s->msmpeg4_version==1){
1970
        int32_t *dc_val;
1971
        pred = msmpeg4v1_pred_dc(s, n, &dc_val);
1972
        level += pred;
1973
        
1974
        /* update predictor */
1975
        *dc_val= level;
1976
    }else{
1977
        uint16_t *dc_val;
1978
        pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr);
1979
        level += pred;
1980

    
1981
        /* update predictor */
1982
        if (n < 4) {
1983
            *dc_val = level * s->y_dc_scale;
1984
        } else {
1985
            *dc_val = level * s->c_dc_scale;
1986
        }
1987
    }
1988

    
1989
    return level;
1990
}
1991

    
1992
static int msmpeg4_decode_motion(MpegEncContext * s, 
1993
                                 int *mx_ptr, int *my_ptr)
1994
{
1995
    MVTable *mv;
1996
    int code, mx, my;
1997

    
1998
    mv = &mv_tables[s->mv_table_index];
1999

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

    
2014
    mx += *mx_ptr - 32;
2015
    my += *my_ptr - 32;
2016
    /* WARNING : they do not do exactly modulo encoding */
2017
    if (mx <= -64)
2018
        mx += 64;
2019
    else if (mx >= 64)
2020
        mx -= 64;
2021

    
2022
    if (my <= -64)
2023
        my += 64;
2024
    else if (my >= 64)
2025
        my -= 64;
2026
    *mx_ptr = mx;
2027
    *my_ptr = my;
2028
    return 0;
2029
}
2030

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