Statistics
| Branch: | Revision:

ffmpeg / libavcodec / msmpeg4.c @ bd5e1c72

History | View | Annotate | Download (51.5 KB)

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

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

    
35
static UINT32 v2_dc_lum_table[512][2];
36
static UINT32 v2_dc_chroma_table[512][2];
37

    
38
static inline void msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n);
39
static inline int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
40
                                       int n, int coded);
41
static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr);
42
static int msmpeg4_decode_motion(MpegEncContext * s, 
43
                                 int *mx_ptr, int *my_ptr);
44
static void msmpeg4v2_encode_motion(MpegEncContext * s, int val);
45
static void init_h263_dc_for_msmpeg4(void);
46

    
47

    
48
extern UINT32 inverse[256];
49

    
50
#ifdef DEBUG
51
int intra_count = 0;
52
int frame_count = 0;
53
#endif
54

    
55
#include "msmpeg4data.h"
56

    
57
#ifdef STATS
58

    
59
const char *st_names[ST_NB] = {
60
    "unknown",
61
    "dc",
62
    "intra_ac",
63
    "inter_ac",
64
    "intra_mb",
65
    "inter_mb",
66
    "mv",
67
};
68

    
69
int st_current_index = 0;
70
unsigned int st_bit_counts[ST_NB];
71
unsigned int st_out_bit_counts[ST_NB];
72

    
73
#define set_stat(var) st_current_index = var;
74

    
75
void print_stats(void)
76
{
77
    unsigned int total;
78
    int i;
79

    
80
    printf("Input:\n");
81
    total = 0;
82
    for(i=0;i<ST_NB;i++)
83
        total += st_bit_counts[i];
84
    if (total == 0)
85
        total = 1;
86
    for(i=0;i<ST_NB;i++) {
87
        printf("%-10s : %10.1f %5.1f%%\n", 
88
               st_names[i], 
89
               (double)st_bit_counts[i] / 8.0, 
90
               (double)st_bit_counts[i] * 100.0 / total);
91
    }
92
    printf("%-10s : %10.1f %5.1f%%\n",
93
           "total", 
94
           (double)total / 8.0, 
95
           100.0);
96

    
97
    printf("Output:\n");
98
    total = 0;
99
    for(i=0;i<ST_NB;i++)
100
        total += st_out_bit_counts[i];
101
    if (total == 0)
102
        total = 1;
103
    for(i=0;i<ST_NB;i++) {
104
        printf("%-10s : %10.1f %5.1f%%\n", 
105
               st_names[i], 
106
               (double)st_out_bit_counts[i] / 8.0, 
107
               (double)st_out_bit_counts[i] * 100.0 / total);
108
    }
109
    printf("%-10s : %10.1f %5.1f%%\n",
110
           "total", 
111
           (double)total / 8.0, 
112
           100.0);
113
}
114

    
115
#else
116

    
117
#define set_stat(var)
118

    
119
#endif
120

    
121
static void common_init(MpegEncContext * s)
122
{
123
    static int inited=0;
124
    
125
    switch(s->msmpeg4_version){
126
    case 1:
127
    case 2:
128
        s->y_dc_scale_table=
129
        s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
130
        break;
131
    case 3:
132
        if(s->workaround_bugs){
133
            s->y_dc_scale_table= old_ff_y_dc_scale_table;
134
            s->c_dc_scale_table= old_ff_c_dc_scale_table;
135
        } else{
136
            s->y_dc_scale_table= ff_mpeg4_y_dc_scale_table;
137
            s->c_dc_scale_table= ff_mpeg4_c_dc_scale_table;
138
        }
139
        break;
140
    case 4:
141
        s->y_dc_scale_table= wmv1_y_dc_scale_table;
142
        s->c_dc_scale_table= wmv1_c_dc_scale_table;
143
        break;
144
    }
145

    
146
    if(s->msmpeg4_version==4){
147
        s->intra_scantable  = wmv1_scantable[1];
148
        s->intra_h_scantable= wmv1_scantable[2];
149
        s->intra_v_scantable= wmv1_scantable[3];
150
        s->inter_scantable  = wmv1_scantable[0];
151
    }else{
152
        s->intra_scantable  = zigzag_direct; 
153
        s->intra_h_scantable= ff_alternate_horizontal_scan; 
154
        s->intra_v_scantable= ff_alternate_vertical_scan; 
155
        s->inter_scantable  = zigzag_direct; 
156
    }
157
    
158
    if(!inited){
159
        int i;
160
        inited=1;
161

    
162
        init_h263_dc_for_msmpeg4();
163

    
164
        /* permute for IDCT */
165
        for(i=0; i<WMV1_SCANTABLE_COUNT; i++){
166
            int k;
167
            for(k=0;k<64;k++) {
168
                int j = wmv1_scantable[i][k];
169
                wmv1_scantable[i][k]= block_permute_op(j);
170
            }
171
        }
172

    
173
    }
174
}
175

    
176
/* build the table which associate a (x,y) motion vector to a vlc */
177
static void init_mv_table(MVTable *tab)
178
{
179
    int i, x, y;
180

    
181
    tab->table_mv_index = av_malloc(sizeof(UINT16) * 4096);
182
    /* mark all entries as not used */
183
    for(i=0;i<4096;i++)
184
        tab->table_mv_index[i] = tab->n;
185
    
186
    for(i=0;i<tab->n;i++) {
187
        x = tab->table_mvx[i];
188
        y = tab->table_mvy[i];
189
        tab->table_mv_index[(x << 6) | y] = i;
190
    }
191
}
192

    
193
static void code012(PutBitContext *pb, int n)
194
{
195
    if (n == 0) {
196
        put_bits(pb, 1, 0);
197
    } else {
198
        put_bits(pb, 1, 1);
199
        put_bits(pb, 1, (n >= 2));
200
    }
201
}
202

    
203
void ff_msmpeg4_encode_init(MpegEncContext *s)
204
{
205
    static int init_done=0;
206
    int i;
207

    
208
    common_init(s);
209
    if(s->msmpeg4_version>=4){
210
        s->min_qcoeff= -255;
211
        s->max_qcoeff=  255;
212
    }
213

    
214
    if (!init_done) {
215
        /* init various encoding tables */
216
        init_done = 1;
217
        init_mv_table(&mv_tables[0]);
218
        init_mv_table(&mv_tables[1]);
219
        for(i=0;i<NB_RL_TABLES;i++)
220
            init_rl(&rl_table[i]);
221
    }
222
}
223

    
224
static int get_size_of_code(MpegEncContext * s, RLTable *rl, int last, int run, int level, int intra){
225
    int size=0;
226
    int code;
227
    int run_diff= intra ? 0 : 1;
228
    
229
    code = get_rl_index(rl, last, run, level);
230
    size+= rl->table_vlc[code][1];
231
    if (code == rl->n) {
232
        int level1, run1;
233

    
234
        level1 = level - rl->max_level[last][run];
235
        if (level1 < 1) 
236
            goto esc2;
237
        code = get_rl_index(rl, last, run, level1);
238
        if (code == rl->n) {
239
            esc2:
240
            size++;
241
            if (level > MAX_LEVEL)
242
                goto esc3;
243
            run1 = run - rl->max_run[last][level] - run_diff;
244
            if (run1 < 0)
245
                goto esc3;
246
            code = get_rl_index(rl, last, run1, level);
247
            if (code == rl->n) {
248
            esc3:
249
                /* third escape */
250
                size+=1+1+6+8;
251
            } else {
252
                /* second escape */
253
                size+= 1+1+ rl->table_vlc[code][1];
254
            }
255
        } else {
256
            /* first escape */
257
            size+= 1+1+ rl->table_vlc[code][1];
258
        }
259
    } else {
260
        size++;
261
    }
262
    return size;
263
}
264

    
265
static void find_best_tables(MpegEncContext * s)
266
{
267
    int i;
268
    int best       =-1, best_size       =9999999;
269
    int chroma_best=-1, best_chroma_size=9999999;
270
    int last_size=0;
271
    
272
    for(i=0; i<3; i++){
273
        int level;
274
        int chroma_size=0;
275
        int size=0;
276

    
277
        if(i>0){// ;)
278
            size++; 
279
            chroma_size++;
280
        }
281
        for(level=0; level<=MAX_LEVEL; level++){
282
            int run;
283
            for(run=0; run<=MAX_RUN; run++){
284
                int last;
285
                for(last=0; last<2; last++){
286
                    int inter_count       = s->ac_stats[0][0][level][run][last] + s->ac_stats[0][1][level][run][last];
287
                    int intra_luma_count  = s->ac_stats[1][0][level][run][last];
288
                    int intra_chroma_count= s->ac_stats[1][1][level][run][last];
289

    
290
                    if(s->pict_type==I_TYPE){
291
                        size       += intra_luma_count  *get_size_of_code(s, &rl_table[  i], last, run, level,1);
292
                        chroma_size+= intra_chroma_count*get_size_of_code(s, &rl_table[3+i], last, run, level,1);
293
                    }else{
294
                        size+=        intra_luma_count  *get_size_of_code(s, &rl_table[  i], last, run, level,1)
295
                                     +intra_chroma_count*get_size_of_code(s, &rl_table[3+i], last, run, level,1)
296
                                     +inter_count       *get_size_of_code(s, &rl_table[3+i], last, run, level,0);
297
                    }                   
298
                }
299
            }
300
        }
301
        if(size<best_size){
302
            best_size= size;
303
            best= i;
304
        }
305
        if(chroma_size<best_chroma_size){
306
            best_chroma_size= chroma_size;
307
            chroma_best= i;
308
        }
309
    }
310
//    printf("type:%d, best:%d, qp:%d, var:%d, mcvar:%d, size:%d //\n", 
311
//           s->pict_type, best, s->qscale, s->mb_var_sum, s->mc_mb_var_sum, best_size);
312
           
313
    if(s->pict_type==P_TYPE) chroma_best= best;
314

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

    
317
    s->rl_table_index       =        best;
318
    s->rl_chroma_table_index= chroma_best;
319
    
320
    if(s->pict_type != s->last_non_b_pict_type){
321
        s->rl_table_index= 2;
322
        if(s->pict_type==I_TYPE)
323
            s->rl_chroma_table_index= 1;
324
        else
325
            s->rl_chroma_table_index= 2;
326
    }
327

    
328
}
329

    
330
/* write MSMPEG4 compatible frame header */
331
void msmpeg4_encode_picture_header(MpegEncContext * s, int picture_number)
332
{
333
    find_best_tables(s);
334

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

    
338
    put_bits(&s->pb, 5, s->qscale);
339
    if(s->msmpeg4_version<=2){
340
        s->rl_table_index = 2;
341
        s->rl_chroma_table_index = 2;
342
    }
343

    
344
    s->dc_table_index = 1;
345
    s->mv_table_index = 1; /* only if P frame */
346
    s->use_skip_mb_code = 1; /* only if P frame */
347
    s->per_mb_rl_table = 0;
348

    
349
    if (s->pict_type == I_TYPE) {
350
        s->no_rounding = 1;
351
        put_bits(&s->pb, 5, 0x17); /* indicate only one "slice" */
352
        
353
        if(s->msmpeg4_version==4){
354
            msmpeg4_encode_ext_header(s);
355
            put_bits(&s->pb, 1, s->per_mb_rl_table);
356
        }
357

    
358
        if(s->msmpeg4_version>2){
359
            if(!s->per_mb_rl_table){
360
                code012(&s->pb, s->rl_chroma_table_index);
361
                code012(&s->pb, s->rl_table_index);
362
            }
363

    
364
            put_bits(&s->pb, 1, s->dc_table_index);
365
        }
366
    } else {
367
        put_bits(&s->pb, 1, s->use_skip_mb_code);
368
        
369
        if(s->msmpeg4_version==4)
370
            put_bits(&s->pb, 1, s->per_mb_rl_table);
371

    
372
        if(s->msmpeg4_version>2){
373
            if(!s->per_mb_rl_table)
374
                code012(&s->pb, s->rl_table_index);
375

    
376
            put_bits(&s->pb, 1, s->dc_table_index);
377

    
378
            put_bits(&s->pb, 1, s->mv_table_index);
379
        }
380

    
381
        if(s->flipflop_rounding){
382
            s->no_rounding ^= 1;
383
        }else{
384
            s->no_rounding = 0;
385
        }
386
    }
387

    
388
    s->esc3_level_length= 0;
389
    s->esc3_run_length= 0;
390

    
391
#ifdef DEBUG
392
    intra_count = 0;
393
    printf("*****frame %d:\n", frame_count++);
394
#endif
395
}
396

    
397
void msmpeg4_encode_ext_header(MpegEncContext * s)
398
{
399
        put_bits(&s->pb, 5, s->frame_rate / FRAME_RATE_BASE); //yes 29.97 -> 29
400

    
401
        put_bits(&s->pb, 11, MIN(s->bit_rate, 2047));
402

    
403
        if(s->msmpeg4_version<3)
404
            s->flipflop_rounding=0;
405
        else{
406
            s->flipflop_rounding=1;
407
            put_bits(&s->pb, 1, s->flipflop_rounding);
408
        }
409
}
410

    
411
/* predict coded block */
412
static inline int coded_block_pred(MpegEncContext * s, int n, UINT8 **coded_block_ptr)
413
{
414
    int xy, wrap, pred, a, b, c;
415

    
416
    xy = s->block_index[n];
417
    wrap = s->block_wrap[0];
418

    
419
    /* B C
420
     * A X 
421
     */
422
    a = s->coded_block[xy - 1       ];
423
    b = s->coded_block[xy - 1 - wrap];
424
    c = s->coded_block[xy     - wrap];
425
    
426
    if (b == c) {
427
        pred = a;
428
    } else {
429
        pred = c;
430
    }
431
    
432
    /* store value */
433
    *coded_block_ptr = &s->coded_block[xy];
434

    
435
    return pred;
436
}
437

    
438
static void msmpeg4_encode_motion(MpegEncContext * s, 
439
                                  int mx, int my)
440
{
441
    int code;
442
    MVTable *mv;
443

    
444
    /* modulo encoding */
445
    /* WARNING : you cannot reach all the MVs even with the modulo
446
       encoding. This is a somewhat strange compromise they took !!!  */
447
    if (mx <= -64)
448
        mx += 64;
449
    else if (mx >= 64)
450
        mx -= 64;
451
    if (my <= -64)
452
        my += 64;
453
    else if (my >= 64)
454
        my -= 64;
455
    
456
    mx += 32;
457
    my += 32;
458
#if 0
459
    if ((unsigned)mx >= 64 ||
460
        (unsigned)my >= 64) 
461
        fprintf(stderr, "error mx=%d my=%d\n", mx, my);
462
#endif
463
    mv = &mv_tables[s->mv_table_index];
464

    
465
    code = mv->table_mv_index[(mx << 6) | my];
466
    set_stat(ST_MV);
467
    put_bits(&s->pb, 
468
             mv->table_mv_bits[code], 
469
             mv->table_mv_code[code]);
470
    if (code == mv->n) {
471
        /* escape : code litterally */
472
        put_bits(&s->pb, 6, mx);
473
        put_bits(&s->pb, 6, my);
474
    }
475
}
476

    
477
void msmpeg4_encode_mb(MpegEncContext * s, 
478
                       DCTELEM block[6][64],
479
                       int motion_x, int motion_y)
480
{
481
    int cbp, coded_cbp, i;
482
    int pred_x, pred_y;
483
    UINT8 *coded_block;
484

    
485
    if (!s->mb_intra) {
486
        /* compute cbp */
487
        set_stat(ST_INTER_MB);
488
        cbp = 0;
489
        for (i = 0; i < 6; i++) {
490
            if (s->block_last_index[i] >= 0)
491
                cbp |= 1 << (5 - i);
492
        }
493
        if (s->use_skip_mb_code && (cbp | motion_x | motion_y) == 0) {
494
            /* skip macroblock */
495
            put_bits(&s->pb, 1, 1);
496
            return;
497
        }
498
        if (s->use_skip_mb_code)
499
            put_bits(&s->pb, 1, 0);        /* mb coded */
500
        
501
        if(s->msmpeg4_version<=2){
502
            put_bits(&s->pb, 
503
                     v2_mb_type[cbp&3][1], 
504
                     v2_mb_type[cbp&3][0]);
505
            if((cbp&3) != 3) coded_cbp= cbp ^ 0x3C;
506
            else             coded_cbp= cbp;
507

    
508
            put_bits(&s->pb, 
509
                     cbpy_tab[coded_cbp>>2][1], 
510
                     cbpy_tab[coded_cbp>>2][0]);
511
                        
512
            h263_pred_motion(s, 0, &pred_x, &pred_y);
513
            msmpeg4v2_encode_motion(s, motion_x - pred_x);
514
            msmpeg4v2_encode_motion(s, motion_y - pred_y);
515
        }else{
516
            put_bits(&s->pb, 
517
                     table_mb_non_intra[cbp + 64][1], 
518
                     table_mb_non_intra[cbp + 64][0]);
519

    
520
            /* motion vector */
521
            h263_pred_motion(s, 0, &pred_x, &pred_y);
522
            msmpeg4_encode_motion(s, motion_x - pred_x, 
523
                                  motion_y - pred_y);
524
        }
525
    } else {
526
        /* compute cbp */
527
        cbp = 0;
528
        coded_cbp = 0;
529
        for (i = 0; i < 6; i++) {
530
            int val, pred;
531
            val = (s->block_last_index[i] >= 1);
532
            cbp |= val << (5 - i);
533
            if (i < 4) {
534
                /* predict value for close blocks only for luma */
535
                pred = coded_block_pred(s, i, &coded_block);
536
                *coded_block = val;
537
                val = val ^ pred;
538
            }
539
            coded_cbp |= val << (5 - i);
540
        }
541
#if 0
542
        if (coded_cbp)
543
            printf("cbp=%x %x\n", cbp, coded_cbp);
544
#endif
545

    
546
        if(s->msmpeg4_version<=2){
547
            if (s->pict_type == I_TYPE) {
548
                put_bits(&s->pb, 
549
                         v2_intra_cbpc[cbp&3][1], v2_intra_cbpc[cbp&3][0]);
550
            } else {
551
                if (s->use_skip_mb_code)
552
                    put_bits(&s->pb, 1, 0);        /* mb coded */
553
                put_bits(&s->pb, 
554
                         v2_mb_type[(cbp&3) + 4][1], 
555
                         v2_mb_type[(cbp&3) + 4][0]);
556
            }
557
            put_bits(&s->pb, 1, 0);        /* no AC prediction yet */
558
            put_bits(&s->pb, 
559
                     cbpy_tab[cbp>>2][1], 
560
                     cbpy_tab[cbp>>2][0]);
561
        }else{
562
            if (s->pict_type == I_TYPE) {
563
                set_stat(ST_INTRA_MB);
564
                put_bits(&s->pb, 
565
                         table_mb_intra[coded_cbp][1], table_mb_intra[coded_cbp][0]);
566
            } else {
567
                if (s->use_skip_mb_code)
568
                    put_bits(&s->pb, 1, 0);        /* mb coded */
569
                put_bits(&s->pb, 
570
                         table_mb_non_intra[cbp][1], 
571
                         table_mb_non_intra[cbp][0]);
572
            }
573
            set_stat(ST_INTRA_MB);
574
            put_bits(&s->pb, 1, 0);        /* no AC prediction yet */
575
        }
576
    }
577

    
578
    for (i = 0; i < 6; i++) {
579
        msmpeg4_encode_block(s, block[i], i);
580
    }
581
}
582

    
583
/* old ffmpeg msmpeg4v3 mode */
584
void ff_old_msmpeg4_dc_scale(MpegEncContext * s)
585
{
586
    if (s->qscale < 5){
587
        s->y_dc_scale = 8;
588
        s->c_dc_scale = 8;
589
    }else if (s->qscale < 9){
590
        s->y_dc_scale = 2 * s->qscale;
591
        s->c_dc_scale = (s->qscale + 13)>>1;
592
    }else{
593
        s->y_dc_scale = s->qscale + 8;
594
        s->c_dc_scale = (s->qscale + 13)>>1;
595
    }
596
}
597

    
598
static inline int msmpeg4v1_pred_dc(MpegEncContext * s, int n, 
599
                                    INT32 **dc_val_ptr)
600
{
601
    int i;
602

    
603
    if (n < 4) {
604
        i= 0;
605
    } else {
606
        i= n-3;
607
    }
608
    
609
    *dc_val_ptr= &s->last_dc[i];
610
    return s->last_dc[i]; 
611
}
612

    
613
/* dir = 0: left, dir = 1: top prediction */
614
static inline int msmpeg4_pred_dc(MpegEncContext * s, int n, 
615
                             UINT16 **dc_val_ptr, int *dir_ptr)
616
{
617
    int a, b, c, wrap, pred, scale;
618
    INT16 *dc_val;
619

    
620
    /* find prediction */
621
    if (n < 4) {
622
        scale = s->y_dc_scale;
623
    } else {
624
        scale = s->c_dc_scale;
625
    }
626
    
627
    wrap = s->block_wrap[n];
628
    dc_val= s->dc_val[0] + s->block_index[n];
629

    
630
    /* B C
631
     * A X 
632
     */
633
    a = dc_val[ - 1];
634
    b = dc_val[ - 1 - wrap];
635
    c = dc_val[ - wrap];
636

    
637
    /* XXX: the following solution consumes divisions, but it does not
638
       necessitate to modify mpegvideo.c. The problem comes from the
639
       fact they decided to store the quantized DC (which would lead
640
       to problems if Q could vary !) */
641
#if defined ARCH_X86 && !defined PIC
642
    asm volatile(
643
        "movl %3, %%eax                \n\t"
644
        "shrl $1, %%eax                \n\t"
645
        "addl %%eax, %2                \n\t"
646
        "addl %%eax, %1                \n\t"
647
        "addl %0, %%eax                \n\t"
648
        "mull %4                \n\t"
649
        "movl %%edx, %0                \n\t"
650
        "movl %1, %%eax                \n\t"
651
        "mull %4                \n\t"
652
        "movl %%edx, %1                \n\t"
653
        "movl %2, %%eax                \n\t"
654
        "mull %4                \n\t"
655
        "movl %%edx, %2                \n\t"
656
        : "+b" (a), "+c" (b), "+D" (c)
657
        : "g" (scale), "S" (inverse[scale])
658
        : "%eax", "%edx"
659
    );
660
#else
661
    /* #elif defined (ARCH_ALPHA) */
662
    /* Divisions are extremely costly on Alpha; optimize the most
663
       common case. But they are costly everywhere...
664
     */
665
    if (scale == 8) {
666
        a = (a + (8 >> 1)) / 8;
667
        b = (b + (8 >> 1)) / 8;
668
        c = (c + (8 >> 1)) / 8;
669
    } else {
670
        a = (a + (scale >> 1)) / scale;
671
        b = (b + (scale >> 1)) / scale;
672
        c = (c + (scale >> 1)) / scale;
673
    }
674
#endif
675
    /* XXX: WARNING: they did not choose the same test as MPEG4. This
676
       is very important ! */
677
    if(s->msmpeg4_version>3){
678
        if (abs(a - b) < abs(b - c)) {
679
            pred = c;
680
            *dir_ptr = 1;
681
        } else {
682
            pred = a;
683
            *dir_ptr = 0;
684
        }
685
    }else{
686
        if (abs(a - b) <= abs(b - c)) {
687
            pred = c;
688
            *dir_ptr = 1;
689
        } else {
690
            pred = a;
691
            *dir_ptr = 0;
692
        }
693
    }
694

    
695
    /* update predictor */
696
    *dc_val_ptr = &dc_val[0];
697
    return pred;
698
}
699

    
700
#define DC_MAX 119
701

    
702
static void msmpeg4_encode_dc(MpegEncContext * s, int level, int n, int *dir_ptr)
703
{
704
    int sign, code;
705
    int pred;
706

    
707
    if(s->msmpeg4_version==1){
708
        INT32 *dc_val;
709
        pred = msmpeg4v1_pred_dc(s, n, &dc_val);
710
        
711
        /* update predictor */
712
        *dc_val= level;
713
    }else{
714
        UINT16 *dc_val;
715
        pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr);
716

    
717
        /* update predictor */
718
        if (n < 4) {
719
            *dc_val = level * s->y_dc_scale;
720
        } else {
721
            *dc_val = level * s->c_dc_scale;
722
        }
723
    }
724

    
725
    /* do the prediction */
726
    level -= pred;
727

    
728
    if(s->msmpeg4_version<=2){
729
        if (n < 4) {
730
            put_bits(&s->pb, 
731
                     v2_dc_lum_table[level+256][1],
732
                     v2_dc_lum_table[level+256][0]);
733
        }else{
734
            put_bits(&s->pb, 
735
                     v2_dc_chroma_table[level+256][1],
736
                     v2_dc_chroma_table[level+256][0]);
737
        }
738
    }else{
739
        sign = 0;
740
        if (level < 0) {
741
            level = -level;
742
            sign = 1;
743
        }
744
        code = level;
745
        if (code > DC_MAX) 
746
            code = DC_MAX;
747

    
748
        if (s->dc_table_index == 0) {
749
            if (n < 4) {
750
                put_bits(&s->pb, table0_dc_lum[code][1], table0_dc_lum[code][0]);
751
            } else {
752
                put_bits(&s->pb, table0_dc_chroma[code][1], table0_dc_chroma[code][0]);
753
            }
754
        } else {
755
            if (n < 4) {
756
                put_bits(&s->pb, table1_dc_lum[code][1], table1_dc_lum[code][0]);
757
            } else {
758
                put_bits(&s->pb, table1_dc_chroma[code][1], table1_dc_chroma[code][0]);
759
            }
760
        }
761
            
762
        if (code == DC_MAX)
763
            put_bits(&s->pb, 8, level);
764
            
765
        if (level != 0) {
766
            put_bits(&s->pb, 1, sign);
767
        }
768
    }
769
}
770

    
771
/* Encoding of a block. Very similar to MPEG4 except for a different
772
   escape coding (same as H263) and more vlc tables.
773
 */
774
static inline void msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n)
775
{
776
    int level, run, last, i, j, last_index;
777
    int last_non_zero, sign, slevel;
778
    int code, run_diff, dc_pred_dir;
779
    const RLTable *rl;
780
    const UINT8 *scantable;
781

    
782
    if (s->mb_intra) {
783
        set_stat(ST_DC);
784
        msmpeg4_encode_dc(s, block[0], n, &dc_pred_dir);
785
        i = 1;
786
        if (n < 4) {
787
            rl = &rl_table[s->rl_table_index];
788
        } else {
789
            rl = &rl_table[3 + s->rl_chroma_table_index];
790
        }
791
        run_diff = 0;
792
        scantable= s->intra_scantable;
793
        set_stat(ST_INTRA_AC);
794
    } else {
795
        i = 0;
796
        rl = &rl_table[3 + s->rl_table_index];
797
        if(s->msmpeg4_version<=2)
798
            run_diff = 0;
799
        else
800
            run_diff = 1;
801
        scantable= s->inter_scantable;
802
        set_stat(ST_INTER_AC);
803
    }
804

    
805
    /* recalculate block_last_index for M$ wmv1 */
806
    if(scantable!=zigzag_direct && s->block_last_index[n]>0){
807
        for(last_index=63; last_index>=0; last_index--){
808
            if(block[scantable[last_index]]) break;
809
        }
810
    }else
811
        last_index = s->block_last_index[n];
812
    /* AC coefs */
813
    last_non_zero = i - 1;
814
    for (; i <= last_index; i++) {
815
        j = scantable[i];
816
        level = block[j];
817
        if (level) {
818
            run = i - last_non_zero - 1;
819
            last = (i == last_index);
820
            sign = 0;
821
            slevel = level;
822
            if (level < 0) {
823
                sign = 1;
824
                level = -level;
825
            }
826
            if(level<=MAX_LEVEL && run<=MAX_RUN){
827
                s->ac_stats[s->mb_intra][n>3][level][run][last]++;
828
            }
829
#if 0
830
else
831
    s->ac_stats[s->mb_intra][n>3][40][63][0]++; //esc3 like
832
#endif
833
            code = get_rl_index(rl, last, run, level);
834
            put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
835
            if (code == rl->n) {
836
                int level1, run1;
837

    
838
                level1 = level - rl->max_level[last][run];
839
                if (level1 < 1) 
840
                    goto esc2;
841
                code = get_rl_index(rl, last, run, level1);
842
                if (code == rl->n) {
843
                esc2:
844
                    put_bits(&s->pb, 1, 0);
845
                    if (level > MAX_LEVEL)
846
                        goto esc3;
847
                    run1 = run - rl->max_run[last][level] - run_diff;
848
                    if (run1 < 0)
849
                        goto esc3;
850
                    code = get_rl_index(rl, last, run1, level);
851
                    if (code == rl->n) {
852
                    esc3:
853
                        /* third escape */
854
                        put_bits(&s->pb, 1, 0);
855
                        put_bits(&s->pb, 1, last);
856
                        if(s->msmpeg4_version==4){
857
                            if(s->esc3_level_length==0){
858
                                s->esc3_level_length=8;
859
                                s->esc3_run_length= 6;
860
                                if(s->qscale<8)
861
                                    put_bits(&s->pb, 6, 3);
862
                                else
863
                                    put_bits(&s->pb, 8, 3);
864
                            }
865
                            put_bits(&s->pb, s->esc3_run_length, run);
866
                            put_bits(&s->pb, 1, sign);
867
                            put_bits(&s->pb, s->esc3_level_length, level);
868
                        }else{
869
                            put_bits(&s->pb, 6, run);
870
                            put_bits(&s->pb, 8, slevel & 0xff);
871
                        }
872
                    } else {
873
                        /* second escape */
874
                        put_bits(&s->pb, 1, 1);
875
                        put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
876
                        put_bits(&s->pb, 1, sign);
877
                    }
878
                } else {
879
                    /* first escape */
880
                    put_bits(&s->pb, 1, 1);
881
                    put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
882
                    put_bits(&s->pb, 1, sign);
883
                }
884
            } else {
885
                put_bits(&s->pb, 1, sign);
886
            }
887
            last_non_zero = i;
888
        }
889
    }
890
}
891

    
892
/****************************************/
893
/* decoding stuff */
894

    
895
static VLC mb_non_intra_vlc;
896
static VLC mb_intra_vlc;
897
static VLC dc_lum_vlc[2];
898
static VLC dc_chroma_vlc[2];
899
static VLC v2_dc_lum_vlc;
900
static VLC v2_dc_chroma_vlc;
901
static VLC cbpy_vlc;
902
static VLC v2_intra_cbpc_vlc;
903
static VLC v2_mb_type_vlc;
904
static VLC v2_mv_vlc;
905
static VLC v1_intra_cbpc_vlc;
906
static VLC v1_inter_cbpc_vlc;
907

    
908
/* this table is practically identical to the one from h263 except that its inverted */
909
static void init_h263_dc_for_msmpeg4(void)
910
{
911
        int level, uni_code, uni_len;
912

    
913
        for(level=-256; level<256; level++){
914
            int size, v, l;
915
            /* find number of bits */
916
            size = 0;
917
            v = abs(level);
918
            while (v) {
919
                v >>= 1;
920
                    size++;
921
            }
922

    
923
            if (level < 0)
924
                l= (-level) ^ ((1 << size) - 1);
925
            else
926
                l= level;
927

    
928
            /* luminance h263 */
929
            uni_code= DCtab_lum[size][0];
930
            uni_len = DCtab_lum[size][1];
931
            uni_code ^= (1<<uni_len)-1; //M$ doesnt like compatibility
932

    
933
            if (size > 0) {
934
                uni_code<<=size; uni_code|=l;
935
                uni_len+=size;
936
                if (size > 8){
937
                    uni_code<<=1; uni_code|=1;
938
                    uni_len++;
939
                }
940
            }
941
            v2_dc_lum_table[level+256][0]= uni_code;
942
            v2_dc_lum_table[level+256][1]= uni_len;
943

    
944
            /* chrominance h263 */
945
            uni_code= DCtab_chrom[size][0];
946
            uni_len = DCtab_chrom[size][1];
947
            uni_code ^= (1<<uni_len)-1; //M$ doesnt like compatibility
948
            
949
            if (size > 0) {
950
                uni_code<<=size; uni_code|=l;
951
                uni_len+=size;
952
                if (size > 8){
953
                    uni_code<<=1; uni_code|=1;
954
                    uni_len++;
955
                }
956
            }
957
            v2_dc_chroma_table[level+256][0]= uni_code;
958
            v2_dc_chroma_table[level+256][1]= uni_len;
959

    
960
        }
961
}
962

    
963
/* init all vlc decoding tables */
964
int ff_msmpeg4_decode_init(MpegEncContext *s)
965
{
966
    static int done = 0;
967
    int i;
968
    MVTable *mv;
969

    
970
    common_init(s);
971

    
972
    if (!done) {
973
        done = 1;
974

    
975
        for(i=0;i<NB_RL_TABLES;i++) {
976
            init_rl(&rl_table[i]);
977
            init_vlc_rl(&rl_table[i]);
978
        }
979
        for(i=0;i<2;i++) {
980
            mv = &mv_tables[i];
981
            init_vlc(&mv->vlc, 9, mv->n + 1, 
982
                     mv->table_mv_bits, 1, 1,
983
                     mv->table_mv_code, 2, 2);
984
        }
985

    
986
        init_vlc(&dc_lum_vlc[0], 9, 120, 
987
                 &table0_dc_lum[0][1], 8, 4,
988
                 &table0_dc_lum[0][0], 8, 4);
989
        init_vlc(&dc_chroma_vlc[0], 9, 120, 
990
                 &table0_dc_chroma[0][1], 8, 4,
991
                 &table0_dc_chroma[0][0], 8, 4);
992
        init_vlc(&dc_lum_vlc[1], 9, 120, 
993
                 &table1_dc_lum[0][1], 8, 4,
994
                 &table1_dc_lum[0][0], 8, 4);
995
        init_vlc(&dc_chroma_vlc[1], 9, 120, 
996
                 &table1_dc_chroma[0][1], 8, 4,
997
                 &table1_dc_chroma[0][0], 8, 4);
998
    
999
        init_vlc(&v2_dc_lum_vlc, 9, 512, 
1000
                 &v2_dc_lum_table[0][1], 8, 4,
1001
                 &v2_dc_lum_table[0][0], 8, 4);
1002
        init_vlc(&v2_dc_chroma_vlc, 9, 512, 
1003
                 &v2_dc_chroma_table[0][1], 8, 4,
1004
                 &v2_dc_chroma_table[0][0], 8, 4);
1005
    
1006
        init_vlc(&cbpy_vlc, 6, 16,
1007
                 &cbpy_tab[0][1], 2, 1,
1008
                 &cbpy_tab[0][0], 2, 1);
1009
        init_vlc(&v2_intra_cbpc_vlc, 3, 4,
1010
                 &v2_intra_cbpc[0][1], 2, 1,
1011
                 &v2_intra_cbpc[0][0], 2, 1);
1012
        init_vlc(&v2_mb_type_vlc, 5, 8,
1013
                 &v2_mb_type[0][1], 2, 1,
1014
                 &v2_mb_type[0][0], 2, 1);
1015
        init_vlc(&v2_mv_vlc, 9, 33,
1016
                 &mvtab[0][1], 2, 1,
1017
                 &mvtab[0][0], 2, 1);
1018

    
1019
        init_vlc(&mb_non_intra_vlc, 9, 128, 
1020
                 &table_mb_non_intra[0][1], 8, 4,
1021
                 &table_mb_non_intra[0][0], 8, 4);
1022
        init_vlc(&mb_intra_vlc, 9, 64, 
1023
                 &table_mb_intra[0][1], 4, 2,
1024
                 &table_mb_intra[0][0], 4, 2);
1025
        
1026
        init_vlc(&v1_intra_cbpc_vlc, 6, 8, 
1027
                 intra_MCBPC_bits, 1, 1,
1028
                 intra_MCBPC_code, 1, 1);
1029
        init_vlc(&v1_inter_cbpc_vlc, 6, 25, 
1030
                 inter_MCBPC_bits, 1, 1,
1031
                 inter_MCBPC_code, 1, 1);
1032
    }
1033
    return 0;
1034
}
1035

    
1036
static int decode012(GetBitContext *gb)
1037
{
1038
    int n;
1039
    n = get_bits1(gb);
1040
    if (n == 0)
1041
        return 0;
1042
    else
1043
        return get_bits1(gb) + 1;
1044
}
1045

    
1046
int msmpeg4_decode_picture_header(MpegEncContext * s)
1047
{
1048
    int code;
1049

    
1050
#if 0
1051
{
1052
int i;
1053
for(i=0; i<s->gb.size*8; i++)
1054
    printf("%d", get_bits1(&s->gb));
1055
//    get_bits1(&s->gb);
1056
printf("END\n");
1057
return -1;
1058
}
1059
#endif
1060

    
1061
    if(s->msmpeg4_version==1){
1062
        int start_code, num;
1063
        start_code = (get_bits(&s->gb, 16)<<16) | get_bits(&s->gb, 16);
1064
        if(start_code!=0x00000100){
1065
            fprintf(stderr, "invalid startcode\n");
1066
            return -1;
1067
        }
1068

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

    
1072
    s->pict_type = get_bits(&s->gb, 2) + 1;
1073
    if (s->pict_type != I_TYPE &&
1074
        s->pict_type != P_TYPE){
1075
        fprintf(stderr, "invalid picture type\n");
1076
        return -1;
1077
    }
1078

    
1079
    s->qscale = get_bits(&s->gb, 5);
1080

    
1081
    if (s->pict_type == I_TYPE) {
1082
        code = get_bits(&s->gb, 5); 
1083
        if(s->msmpeg4_version==1){
1084
            if(code==0 || code>s->mb_height){
1085
                fprintf(stderr, "invalid slice height %d\n", code);
1086
                return -1;
1087
            }
1088

    
1089
            s->slice_height = code;
1090
        }else{
1091
            /* 0x17: one slice, 0x18: two slices, ... */
1092
            if (code < 0x17)
1093
                return -1;
1094

    
1095
            s->slice_height = s->mb_height / (code - 0x16);
1096
        }
1097

    
1098
        switch(s->msmpeg4_version){
1099
        case 1:
1100
        case 2:
1101
            s->rl_chroma_table_index = 2;
1102
            s->rl_table_index = 2;
1103

    
1104
            s->dc_table_index = 0; //not used
1105
            break;
1106
        case 3:
1107
            s->rl_chroma_table_index = decode012(&s->gb);
1108
            s->rl_table_index = decode012(&s->gb);
1109

    
1110
            s->dc_table_index = get_bits1(&s->gb);
1111
            break;
1112
        case 4:
1113
            msmpeg4_decode_ext_header(s, (2+5+5+17+7)/8);
1114

    
1115
            s->per_mb_rl_table= get_bits1(&s->gb);
1116
            if(!s->per_mb_rl_table){
1117
                s->rl_chroma_table_index = decode012(&s->gb);
1118
                s->rl_table_index = decode012(&s->gb);
1119
            }
1120

    
1121
            s->dc_table_index = get_bits1(&s->gb);
1122
            break;
1123
        }
1124
        s->no_rounding = 1;
1125
/*        printf(" %d %d %d %d %d    \n", 
1126
                s->qscale,
1127
                s->rl_chroma_table_index,
1128
                s->rl_table_index, 
1129
                s->dc_table_index,
1130
                s->per_mb_rl_table);*/
1131
    } else {
1132
        
1133
        switch(s->msmpeg4_version){
1134
        case 1:
1135
        case 2:
1136
            if(s->msmpeg4_version==1)
1137
                s->use_skip_mb_code = 1;
1138
            else
1139
                s->use_skip_mb_code = get_bits1(&s->gb);
1140
            s->rl_table_index = 2;
1141
            s->rl_chroma_table_index = s->rl_table_index;
1142
            s->dc_table_index = 0; //not used
1143
            s->mv_table_index = 0;
1144
            break;
1145
        case 3:
1146
            s->use_skip_mb_code = get_bits1(&s->gb);
1147
            s->rl_table_index = decode012(&s->gb);
1148
            s->rl_chroma_table_index = s->rl_table_index;
1149

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

    
1152
            s->mv_table_index = get_bits1(&s->gb);
1153
            break;
1154
        case 4:
1155
            s->use_skip_mb_code = get_bits1(&s->gb);
1156
            s->per_mb_rl_table= get_bits1(&s->gb);
1157
            if(!s->per_mb_rl_table){
1158
                s->rl_table_index = decode012(&s->gb);
1159
                s->rl_chroma_table_index = s->rl_table_index;
1160
            }
1161

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

    
1164
            s->mv_table_index = get_bits1(&s->gb);
1165
            break;
1166
        }
1167
/*        printf(" %d %d %d %d %d %d    \n", 
1168
                s->use_skip_mb_code, 
1169
                s->rl_table_index, 
1170
                s->rl_chroma_table_index, 
1171
                s->dc_table_index,
1172
                s->mv_table_index,
1173
                s->per_mb_rl_table);*/
1174
        if(s->flipflop_rounding){
1175
            s->no_rounding ^= 1;
1176
        }else{
1177
            s->no_rounding = 0;
1178
        }
1179
    }
1180

    
1181
    s->esc3_level_length= 0;
1182
    s->esc3_run_length= 0;
1183

    
1184
#ifdef DEBUG
1185
    printf("*****frame %d:\n", frame_count++);
1186
#endif
1187
    return 0;
1188
}
1189

    
1190
int msmpeg4_decode_ext_header(MpegEncContext * s, int buf_size)
1191
{
1192
    int left= buf_size*8 - get_bits_count(&s->gb);
1193
    int length= s->msmpeg4_version>=3 ? 17 : 16;
1194
    /* the alt_bitstream reader could read over the end so we need to check it */
1195
    if(left>=length && left<length+8)
1196
    {
1197
        int fps;
1198

    
1199
        fps= get_bits(&s->gb, 5);
1200
        s->bit_rate= get_bits(&s->gb, 11);
1201
        if(s->msmpeg4_version>=3)
1202
            s->flipflop_rounding= get_bits1(&s->gb);
1203
        else
1204
            s->flipflop_rounding= 0;
1205

    
1206
//        printf("fps:%2d bps:%2d roundingType:%1d\n", fps, s->bit_rate, s->flipflop_rounding);
1207
    }
1208
    else if(left<length+8)
1209
    {
1210
        s->flipflop_rounding= 0;
1211
        printf("ext header missing, %d left\n", left);
1212
    }
1213
    else
1214
    {
1215
        fprintf(stderr, "I frame too long, ignoring ext header\n");
1216
    }
1217

    
1218
    return 0;
1219
}
1220

    
1221
static inline void msmpeg4_memsetw(short *tab, int val, int n)
1222
{
1223
    int i;
1224
    for(i=0;i<n;i++)
1225
        tab[i] = val;
1226
}
1227

    
1228
static void msmpeg4v2_encode_motion(MpegEncContext * s, int val)
1229
{
1230
    int range, bit_size, sign, code, bits;
1231

    
1232
    if (val == 0) {
1233
        /* zero vector */
1234
        code = 0;
1235
        put_bits(&s->pb, mvtab[code][1], mvtab[code][0]);
1236
    } else {
1237
        bit_size = s->f_code - 1;
1238
        range = 1 << bit_size;
1239
        if (val <= -64)
1240
            val += 64;
1241
        else if (val >= 64)
1242
            val -= 64;
1243

    
1244
        if (val >= 0) {
1245
            sign = 0;
1246
        } else {
1247
            val = -val;
1248
            sign = 1;
1249
        }
1250
        val--;
1251
        code = (val >> bit_size) + 1;
1252
        bits = val & (range - 1);
1253

    
1254
        put_bits(&s->pb, mvtab[code][1] + 1, (mvtab[code][0] << 1) | sign); 
1255
        if (bit_size > 0) {
1256
            put_bits(&s->pb, bit_size, bits);
1257
        }
1258
    }
1259
}
1260

    
1261
/* this is identical to h263 except that its range is multiplied by 2 */
1262
static int msmpeg4v2_decode_motion(MpegEncContext * s, int pred, int f_code)
1263
{
1264
    int code, val, sign, shift;
1265

    
1266
    code = get_vlc(&s->gb, &v2_mv_vlc);
1267
//     printf("MV code %d at %d %d pred: %d\n", code, s->mb_x,s->mb_y, pred);
1268
    if (code < 0)
1269
        return 0xffff;
1270

    
1271
    if (code == 0)
1272
        return pred;
1273
    sign = get_bits1(&s->gb);
1274
    shift = f_code - 1;
1275
    val = (code - 1) << shift;
1276
    if (shift > 0)
1277
        val |= get_bits(&s->gb, shift);
1278
    val++;
1279
    if (sign)
1280
        val = -val;
1281

    
1282
    val += pred;
1283
    if (val <= -64)
1284
        val += 64;
1285
    else if (val >= 64)
1286
        val -= 64;
1287

    
1288
    return val;
1289
}
1290

    
1291

    
1292
static int msmpeg4v12_decode_mb(MpegEncContext *s, 
1293
                      DCTELEM block[6][64])
1294
{
1295
    int cbp, code, i;
1296
    if (s->pict_type == P_TYPE) {
1297
        if (s->use_skip_mb_code) {
1298
            if (get_bits1(&s->gb)) {
1299
                /* skip mb */
1300
                s->mb_intra = 0;
1301
                for(i=0;i<6;i++)
1302
                    s->block_last_index[i] = -1;
1303
                s->mv_dir = MV_DIR_FORWARD;
1304
                s->mv_type = MV_TYPE_16X16;
1305
                s->mv[0][0][0] = 0;
1306
                s->mv[0][0][1] = 0;
1307
                s->mb_skiped = 1;
1308
                return 0;
1309
            }
1310
        }
1311

    
1312
        if(s->msmpeg4_version==2)
1313
            code = get_vlc(&s->gb, &v2_mb_type_vlc);
1314
        else
1315
            code = get_vlc(&s->gb, &v1_inter_cbpc_vlc);
1316
        if(code<0 || code>7){
1317
            fprintf(stderr, "cbpc %d invalid at %d %d\n", code, s->mb_x, s->mb_y);
1318
            return -1;
1319
        }
1320

    
1321
        s->mb_intra = code >>2;
1322
    
1323
        cbp = code & 0x3;
1324
    } else {
1325
        s->mb_intra = 1;
1326
        if(s->msmpeg4_version==2)
1327
            cbp= get_vlc(&s->gb, &v2_intra_cbpc_vlc);
1328
        else
1329
            cbp= get_vlc(&s->gb, &v1_intra_cbpc_vlc);
1330
        if(cbp<0 || cbp>3){
1331
            fprintf(stderr, "cbpc %d invalid at %d %d\n", cbp, s->mb_x, s->mb_y);
1332
            return -1;
1333
        }
1334
    }
1335

    
1336
    if (!s->mb_intra) {
1337
        int mx, my, cbpy;
1338
        
1339
        cbpy= get_vlc(&s->gb, &cbpy_vlc);
1340
        if(cbpy<0){
1341
            fprintf(stderr, "cbpy %d invalid at %d %d\n", cbp, s->mb_x, s->mb_y);
1342
            return -1;
1343
        }
1344

    
1345
        cbp|= cbpy<<2;
1346
        if(s->msmpeg4_version==1 || (cbp&3) != 3) cbp^= 0x3C;
1347
        
1348
        h263_pred_motion(s, 0, &mx, &my);
1349
        mx= msmpeg4v2_decode_motion(s, mx, 1);
1350
        my= msmpeg4v2_decode_motion(s, my, 1);
1351
        
1352
        s->mv_dir = MV_DIR_FORWARD;
1353
        s->mv_type = MV_TYPE_16X16;
1354
        s->mv[0][0][0] = mx;
1355
        s->mv[0][0][1] = my;
1356
    } else {
1357
        if(s->msmpeg4_version==2){
1358
            s->ac_pred = get_bits1(&s->gb);
1359
            cbp|= get_vlc(&s->gb, &cbpy_vlc)<<2; //FIXME check errors
1360
        } else{
1361
            s->ac_pred = 0;
1362
            cbp|= get_vlc(&s->gb, &cbpy_vlc)<<2; //FIXME check errors
1363
            if(s->pict_type==P_TYPE) cbp^=0x3C;
1364
        }
1365
    }
1366

    
1367
    for (i = 0; i < 6; i++) {
1368
        if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1) < 0)
1369
        {
1370
             fprintf(stderr,"\nerror while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
1371
             return -1;
1372
        }
1373
    }
1374
    return 0;
1375
}
1376

    
1377
int msmpeg4_decode_mb(MpegEncContext *s, 
1378
                      DCTELEM block[6][64])
1379
{
1380
    int cbp, code, i;
1381
    UINT8 *coded_val;
1382

    
1383
#ifdef PRINT_MB
1384
if(s->mb_x==0){
1385
    printf("\n");
1386
    if(s->mb_y==0) printf("\n");
1387
}
1388
#endif
1389
    /* special slice handling */
1390
    if (s->mb_x == 0) {
1391
        if (s->slice_height && (s->mb_y % s->slice_height) == 0) {
1392
            int wrap;
1393
            /* reset DC pred (set previous line to 1024) */
1394
            wrap = 2 * s->mb_width + 2;
1395
            msmpeg4_memsetw(&s->dc_val[0][(1) + (2 * s->mb_y) * wrap],
1396
                            1024, 2 * s->mb_width);
1397
            wrap = s->mb_width + 2;
1398
            msmpeg4_memsetw(&s->dc_val[1][(1) + (s->mb_y) * wrap],
1399
                            1024, s->mb_width);
1400
            msmpeg4_memsetw(&s->dc_val[2][(1) + (s->mb_y) * wrap],
1401
                            1024, s->mb_width);
1402

    
1403
            /* reset AC pred (set previous line to 0) */
1404
            wrap = s->mb_width * 2 + 2;
1405
            msmpeg4_memsetw(s->ac_val[0][0] + (1 + (2 * s->mb_y) * wrap)*16,
1406
                            0, 2 * s->mb_width*16);
1407
            wrap = s->mb_width + 2;
1408
            msmpeg4_memsetw(s->ac_val[1][0] + (1 + (s->mb_y) * wrap)*16,
1409
                            0, s->mb_width*16);
1410
            msmpeg4_memsetw(s->ac_val[2][0] + (1 + (s->mb_y) * wrap)*16,
1411
                            0, s->mb_width*16);
1412

    
1413
            s->first_slice_line = 1;
1414
        } else {
1415
            s->first_slice_line = 0; 
1416
        }
1417
    }
1418

    
1419
    if(s->msmpeg4_version<=2) return msmpeg4v12_decode_mb(s, block); //FIXME export function & call from outside perhaps
1420
    
1421
    if (s->pict_type == P_TYPE) {
1422
        set_stat(ST_INTER_MB);
1423
        if (s->use_skip_mb_code) {
1424
            if (get_bits1(&s->gb)) {
1425
                /* skip mb */
1426
                s->mb_intra = 0;
1427
                for(i=0;i<6;i++)
1428
                    s->block_last_index[i] = -1;
1429
                s->mv_dir = MV_DIR_FORWARD;
1430
                s->mv_type = MV_TYPE_16X16;
1431
                s->mv[0][0][0] = 0;
1432
                s->mv[0][0][1] = 0;
1433
                s->mb_skiped = 1;
1434
#ifdef PRINT_MB
1435
printf("S ");
1436
#endif
1437
                return 0;
1438
            }
1439
        }
1440
        
1441
        code = get_vlc(&s->gb, &mb_non_intra_vlc);
1442
        if (code < 0)
1443
            return -1;
1444
        //s->mb_intra = (code & 0x40) ? 0 : 1;
1445
        s->mb_intra = (~code & 0x40) >> 6;
1446
            
1447
        cbp = code & 0x3f;
1448
    } else {
1449
        set_stat(ST_INTRA_MB);
1450
        s->mb_intra = 1;
1451
        code = get_vlc(&s->gb, &mb_intra_vlc);
1452
        if (code < 0)
1453
            return -1;
1454
        /* predict coded block pattern */
1455
        cbp = 0;
1456
        for(i=0;i<6;i++) {
1457
            int val = ((code >> (5 - i)) & 1);
1458
            if (i < 4) {
1459
                int pred = coded_block_pred(s, i, &coded_val);
1460
                val = val ^ pred;
1461
                *coded_val = val;
1462
            }
1463
            cbp |= val << (5 - i);
1464
        }
1465
    }
1466

    
1467
    if (!s->mb_intra) {
1468
        int mx, my;
1469
//printf("P at %d %d\n", s->mb_x, s->mb_y);
1470
        if(s->per_mb_rl_table && cbp){
1471
            s->rl_table_index = decode012(&s->gb);
1472
            s->rl_chroma_table_index = s->rl_table_index;
1473
        }
1474
        set_stat(ST_MV);
1475
        h263_pred_motion(s, 0, &mx, &my);
1476
        if (msmpeg4_decode_motion(s, &mx, &my) < 0)
1477
            return -1;
1478
        s->mv_dir = MV_DIR_FORWARD;
1479
        s->mv_type = MV_TYPE_16X16;
1480
        s->mv[0][0][0] = mx;
1481
        s->mv[0][0][1] = my;
1482
#ifdef PRINT_MB
1483
printf("P ");
1484
#endif
1485
    } else {
1486
//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));
1487
        set_stat(ST_INTRA_MB);
1488
        s->ac_pred = get_bits1(&s->gb);
1489
#ifdef PRINT_MB
1490
printf("%c", s->ac_pred ? 'A' : 'I');
1491
#endif
1492
        if(s->per_mb_rl_table && cbp){
1493
            s->rl_table_index = decode012(&s->gb);
1494
            s->rl_chroma_table_index = s->rl_table_index;
1495
        }
1496
    }
1497

    
1498
    for (i = 0; i < 6; i++) {
1499
        if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1) < 0)
1500
        {
1501
            fprintf(stderr,"\nerror while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
1502
            return -1;
1503
        }
1504
    }
1505
    
1506
    return 0;
1507
}
1508

    
1509
static inline int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
1510
                              int n, int coded)
1511
{
1512
    int code, level, i, j, last, run, run_diff;
1513
    int dc_pred_dir;
1514
    RLTable *rl;
1515
    const UINT8 *scan_table;
1516
    int qmul, qadd;
1517

    
1518
    if (s->mb_intra) {
1519
        qmul=1;
1520
        qadd=0;
1521

    
1522
        /* DC coef */
1523
        set_stat(ST_DC);
1524
        level = msmpeg4_decode_dc(s, n, &dc_pred_dir);
1525
#ifdef PRINT_MB
1526
{
1527
    static int c;
1528
    if(n==0) c=0;
1529
    if(n==4) printf("%X", c);
1530
    c+= c +dc_pred_dir;
1531
}
1532
#endif
1533
        if (level < 0){
1534
            fprintf(stderr, "dc overflow- block: %d qscale: %d//\n", n, s->qscale);
1535
            return -1;
1536
        }
1537
        if (n < 4) {
1538
            rl = &rl_table[s->rl_table_index];
1539
            if(level > 256*s->y_dc_scale){
1540
                fprintf(stderr, "dc overflow+ L qscale: %d//\n", s->qscale);
1541
                return -1;
1542
            }
1543
        } else {
1544
            rl = &rl_table[3 + s->rl_chroma_table_index];
1545
            if(level > 256*s->c_dc_scale){
1546
                fprintf(stderr, "dc overflow+ C qscale: %d//\n", s->qscale);
1547
                return -1;
1548
            }
1549
        }
1550
        block[0] = level;
1551

    
1552
        run_diff = 0;
1553
        i = 1;
1554
        if (!coded) {
1555
            goto not_coded;
1556
        }
1557
        if (s->ac_pred) {
1558
            if (dc_pred_dir == 0) 
1559
                scan_table = s->intra_v_scantable; /* left */
1560
            else
1561
                scan_table = s->intra_h_scantable; /* top */
1562
        } else {
1563
            scan_table = s->intra_scantable;
1564
        }
1565
        set_stat(ST_INTRA_AC);
1566
    } else {
1567
        qmul = s->qscale << 1;
1568
        qadd = (s->qscale - 1) | 1;
1569
        i = 0;
1570
        rl = &rl_table[3 + s->rl_table_index];
1571

    
1572
        if(s->msmpeg4_version==2)
1573
            run_diff = 0;
1574
        else
1575
            run_diff = 1;
1576

    
1577
        if (!coded) {
1578
            s->block_last_index[n] = i - 1;
1579
            return 0;
1580
        }
1581
        scan_table = s->inter_scantable;
1582
        set_stat(ST_INTER_AC);
1583
    }
1584

    
1585
    for(;;) {
1586
        code = get_vlc(&s->gb, &rl->vlc);
1587
        if (code < 0){
1588
            fprintf(stderr, "illegal AC-VLC code at %d %d\n", s->mb_x, s->mb_y);
1589
            return -1;
1590
        }
1591
        if (code == rl->n) {
1592
            /* escape */
1593
            if (s->msmpeg4_version==1 || get_bits1(&s->gb) == 0) {
1594
                if (s->msmpeg4_version==1 || get_bits1(&s->gb) == 0) {
1595
                    /* third escape */
1596
                    if(s->msmpeg4_version<=3){
1597
                        last= get_bits1(&s->gb);
1598
                        run= get_bits(&s->gb, 6);  
1599
                        level= get_bits(&s->gb, 8);
1600
                        level= ((int8_t)level);
1601
                    }else{
1602
                        int sign;
1603
                        last= get_bits1(&s->gb);
1604
                        if(!s->esc3_level_length){
1605
                            int ll;
1606
                            //printf("ESC-3 %X at %d %d\n", show_bits(&s->gb, 24), s->mb_x, s->mb_y);
1607
                            if(s->qscale<8){
1608
                                ll= get_bits(&s->gb, 3);
1609
                                if(ll==0){
1610
                                    if(get_bits1(&s->gb)) printf("cool a new vlc code ,contact the ffmpeg developers and upload the file\n");
1611
                                    ll=8;
1612
                                }
1613
                            }else{
1614
                                ll=2;
1615
                                while(ll<8 && get_bits1(&s->gb)==0) ll++;
1616
                            }
1617

    
1618
                            s->esc3_level_length= ll;
1619
                            s->esc3_run_length= get_bits(&s->gb, 2) + 3;
1620
//printf("level length:%d, run length: %d\n", ll, s->esc3_run_length);
1621
                        }
1622
                        run= get_bits(&s->gb, s->esc3_run_length);  
1623
                        sign= get_bits1(&s->gb);
1624
                        level= get_bits(&s->gb, s->esc3_level_length);
1625
                        if(sign) level= -level;
1626
                    }
1627
//printf("level: %d, run: %d at %d %d\n", level, run, s->mb_x, s->mb_y);
1628
#if 0 // waste of time / this will detect very few errors
1629
                    {
1630
                        const int abs_level= ABS(level);
1631
                        const int run1= run - rl->max_run[last][abs_level] - run_diff;
1632
                        if(abs_level<=MAX_LEVEL && run<=MAX_RUN){
1633
                            if(abs_level <= rl->max_level[last][run]){
1634
                                fprintf(stderr, "illegal 3. esc, vlc encoding possible\n");
1635
                                return DECODING_AC_LOST;
1636
                            }
1637
                            if(abs_level <= rl->max_level[last][run]*2){
1638
                                fprintf(stderr, "illegal 3. esc, esc 1 encoding possible\n");
1639
                                return DECODING_AC_LOST;
1640
                            }
1641
                            if(run1>=0 && abs_level <= rl->max_level[last][run1]){
1642
                                fprintf(stderr, "illegal 3. esc, esc 2 encoding possible\n");
1643
                                return DECODING_AC_LOST;
1644
                            }
1645
                        }
1646
                    }
1647
#endif
1648
                    //level = level * qmul + (level>0) * qadd - (level<=0) * qadd ;
1649
                    if (level>0) level= level * qmul + qadd;
1650
                    else         level= level * qmul - qadd;
1651
#if 0 // waste of time too :(
1652
                    if(level>2048 || level<-2048){
1653
                        fprintf(stderr, "|level| overflow in 3. esc\n");
1654
                        return DECODING_AC_LOST;
1655
                    }
1656
#endif
1657
                } else {
1658
                    /* second escape */
1659
                    code = get_vlc(&s->gb, &rl->vlc);
1660
                    if (code < 0 || code >= rl->n){
1661
                        fprintf(stderr, "illegal ESC2-VLC code %d at %d %d\n", code, s->mb_x, s->mb_y);
1662
                        return -1;
1663
                    }
1664
                    run = rl->table_run[code];
1665
                    level = rl->table_level[code];
1666
                    last = code >= rl->last;
1667
                    run += rl->max_run[last][level] + run_diff;
1668
                    level= level * qmul + qadd;
1669
                    if (get_bits1(&s->gb))
1670
                        level = -level;
1671
                }
1672
            } else {
1673
                /* first escape */
1674
                code = get_vlc(&s->gb, &rl->vlc);
1675
                if (code < 0 || code >= rl->n){
1676
                    fprintf(stderr, "illegal ESC2-VLC code %d at %d %d\n", code, s->mb_x, s->mb_y);
1677
                    return -1;
1678
                }
1679
                run = rl->table_run[code];
1680
                level = rl->table_level[code];
1681
                last = code >= rl->last;
1682
                level += rl->max_level[last][run];
1683
                level= level * qmul + qadd;
1684
                if (get_bits1(&s->gb))
1685
                    level = -level;
1686
            }
1687
        } else {
1688
            run = rl->table_run[code];
1689
            level = rl->table_level[code] * qmul + qadd;
1690
            last = code >= rl->last;
1691
            if (get_bits1(&s->gb))
1692
                level = -level;
1693
        }
1694
        i += run;
1695
        if (i >= 64){
1696
            fprintf(stderr, "run too long at %d %d\n", s->mb_x, s->mb_y);
1697
            return -1;
1698
        }
1699

    
1700
        j = scan_table[i];
1701
        block[j] = level;
1702
        i++;
1703
        if (last)
1704
            break;
1705
    }
1706
 not_coded:
1707
    if (s->mb_intra) {
1708
        mpeg4_pred_ac(s, block, n, dc_pred_dir);
1709
        if (s->ac_pred) {
1710
            i = 64; /* XXX: not optimal */
1711
        }
1712
    }
1713
    if(s->msmpeg4_version==4 && i>1) i=64; //FIXME/XXX optimize
1714
    s->block_last_index[n] = i - 1;
1715
    
1716
    return 0;
1717
}
1718

    
1719
static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr)
1720
{
1721
    int level, pred;
1722

    
1723
    if(s->msmpeg4_version<=2){
1724
        if (n < 4) {
1725
            level = get_vlc(&s->gb, &v2_dc_lum_vlc);
1726
        } else {
1727
            level = get_vlc(&s->gb, &v2_dc_chroma_vlc);
1728
        }
1729
        if (level < 0) 
1730
            return -1;
1731
        level-=256;
1732
    }else{  //FIXME optimize use unified tables & index
1733
        if (n < 4) {
1734
            level = get_vlc(&s->gb, &dc_lum_vlc[s->dc_table_index]);
1735
        } else {
1736
            level = get_vlc(&s->gb, &dc_chroma_vlc[s->dc_table_index]);
1737
        }
1738
        if (level < 0){
1739
            fprintf(stderr, "illegal dc vlc\n");
1740
            return -1;
1741
        }
1742

    
1743
        if (level == DC_MAX) {
1744
            level = get_bits(&s->gb, 8);
1745
            if (get_bits1(&s->gb))
1746
                level = -level;
1747
        } else if (level != 0) {
1748
            if (get_bits1(&s->gb))
1749
                level = -level;
1750
        }
1751
    }
1752

    
1753
    if(s->msmpeg4_version==1){
1754
        INT32 *dc_val;
1755
        pred = msmpeg4v1_pred_dc(s, n, &dc_val);
1756
        level += pred;
1757
        
1758
        /* update predictor */
1759
        *dc_val= level;
1760
    }else{
1761
        UINT16 *dc_val;
1762
        pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr);
1763
        level += pred;
1764

    
1765
        /* update predictor */
1766
        if (n < 4) {
1767
            *dc_val = level * s->y_dc_scale;
1768
        } else {
1769
            *dc_val = level * s->c_dc_scale;
1770
        }
1771
    }
1772

    
1773
    return level;
1774
}
1775

    
1776
static int msmpeg4_decode_motion(MpegEncContext * s, 
1777
                                 int *mx_ptr, int *my_ptr)
1778
{
1779
    MVTable *mv;
1780
    int code, mx, my;
1781

    
1782
    mv = &mv_tables[s->mv_table_index];
1783

    
1784
    code = get_vlc(&s->gb, &mv->vlc);
1785
    if (code < 0){
1786
        fprintf(stderr, "illegal MV code at %d %d\n", s->mb_x, s->mb_y);
1787
        return -1;
1788
    }
1789
    if (code == mv->n) {
1790
//printf("MV ESC %X at %d %d\n", show_bits(&s->gb, 24), s->mb_x, s->mb_y);
1791
        mx = get_bits(&s->gb, 6);
1792
        my = get_bits(&s->gb, 6);
1793
    } else {
1794
        mx = mv->table_mvx[code];
1795
        my = mv->table_mvy[code];
1796
    }
1797

    
1798
    mx += *mx_ptr - 32;
1799
    my += *my_ptr - 32;
1800
    /* WARNING : they do not do exactly modulo encoding */
1801
    if (mx <= -64)
1802
        mx += 64;
1803
    else if (mx >= 64)
1804
        mx -= 64;
1805

    
1806
    if (my <= -64)
1807
        my += 64;
1808
    else if (my >= 64)
1809
        my -= 64;
1810
    *mx_ptr = mx;
1811
    *my_ptr = my;
1812
    return 0;
1813
}