Statistics
| Branch: | Revision:

ffmpeg / libavcodec / msmpeg4.c @ bd5e1c72

History | View | Annotate | Download (51.5 KB)

1 de6d9b64 Fabrice Bellard
/*
2
 * MSMPEG4 backend for ffmpeg encoder and decoder
3 ff4ec49e Fabrice Bellard
 * Copyright (c) 2001 Fabrice Bellard.
4 de6d9b64 Fabrice Bellard
 *
5 ff4ec49e Fabrice Bellard
 * 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 de6d9b64 Fabrice Bellard
 *
10 ff4ec49e Fabrice Bellard
 * This library is distributed in the hope that it will be useful,
11 de6d9b64 Fabrice Bellard
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ff4ec49e Fabrice Bellard
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * Lesser General Public License for more details.
14 de6d9b64 Fabrice Bellard
 *
15 ff4ec49e Fabrice Bellard
 * 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 92ba5ffb Michael Niedermayer
 *
19 287229e5 Michael Niedermayer
 * msmpeg4v1 & v2 stuff by Michael Niedermayer <michaelni@gmx.at>
20 de6d9b64 Fabrice Bellard
 */
21 6000abfa Fabrice Bellard
#include "avcodec.h"
22 de6d9b64 Fabrice Bellard
#include "dsputil.h"
23
#include "mpegvideo.h"
24 bd5e1c72 Michael Niedermayer
//#define PRINT_MB
25 92ba5ffb Michael Niedermayer
26 de6d9b64 Fabrice Bellard
/*
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 84afee34 Michael Niedermayer
static UINT32 v2_dc_lum_table[512][2];
36
static UINT32 v2_dc_chroma_table[512][2];
37
38 f5957f3f Michael Niedermayer
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 de6d9b64 Fabrice Bellard
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 3825cd1d Michael Niedermayer
static void msmpeg4v2_encode_motion(MpegEncContext * s, int val);
45 20695ec9 Falk Hüffner
static void init_h263_dc_for_msmpeg4(void);
46 3825cd1d Michael Niedermayer
47 de6d9b64 Fabrice Bellard
48 6fe84b43 Michael Niedermayer
extern UINT32 inverse[256];
49
50 de6d9b64 Fabrice Bellard
#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 f5957f3f Michael Niedermayer
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 de6d9b64 Fabrice Bellard
/* 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 6000abfa Fabrice Bellard
    tab->table_mv_index = av_malloc(sizeof(UINT16) * 4096);
182 de6d9b64 Fabrice Bellard
    /* 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 f5957f3f Michael Niedermayer
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 287229e5 Michael Niedermayer
/* write MSMPEG4 compatible frame header */
331 de6d9b64 Fabrice Bellard
void msmpeg4_encode_picture_header(MpegEncContext * s, int picture_number)
332
{
333 f5957f3f Michael Niedermayer
    find_best_tables(s);
334 de6d9b64 Fabrice Bellard
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 f5957f3f Michael Niedermayer
    if(s->msmpeg4_version<=2){
340
        s->rl_table_index = 2;
341
        s->rl_chroma_table_index = 2;
342
    }
343 3825cd1d Michael Niedermayer
344 de6d9b64 Fabrice Bellard
    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 f5957f3f Michael Niedermayer
    s->per_mb_rl_table = 0;
348
349 de6d9b64 Fabrice Bellard
    if (s->pict_type == I_TYPE) {
350 f5957f3f Michael Niedermayer
        s->no_rounding = 1;
351 de6d9b64 Fabrice Bellard
        put_bits(&s->pb, 5, 0x17); /* indicate only one "slice" */
352 f5957f3f Michael Niedermayer
        
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 de6d9b64 Fabrice Bellard
358 287229e5 Michael Niedermayer
        if(s->msmpeg4_version>2){
359 f5957f3f Michael Niedermayer
            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 de6d9b64 Fabrice Bellard
364 3825cd1d Michael Niedermayer
            put_bits(&s->pb, 1, s->dc_table_index);
365
        }
366 de6d9b64 Fabrice Bellard
    } else {
367
        put_bits(&s->pb, 1, s->use_skip_mb_code);
368
        
369 f5957f3f Michael Niedermayer
        if(s->msmpeg4_version==4)
370
            put_bits(&s->pb, 1, s->per_mb_rl_table);
371
372 287229e5 Michael Niedermayer
        if(s->msmpeg4_version>2){
373 f5957f3f Michael Niedermayer
            if(!s->per_mb_rl_table)
374
                code012(&s->pb, s->rl_table_index);
375 de6d9b64 Fabrice Bellard
376 3825cd1d Michael Niedermayer
            put_bits(&s->pb, 1, s->dc_table_index);
377
378
            put_bits(&s->pb, 1, s->mv_table_index);
379
        }
380 de6d9b64 Fabrice Bellard
381 ae40484c Michael Niedermayer
        if(s->flipflop_rounding){
382
            s->no_rounding ^= 1;
383
        }else{
384
            s->no_rounding = 0;
385
        }
386 de6d9b64 Fabrice Bellard
    }
387
388 f5957f3f Michael Niedermayer
    s->esc3_level_length= 0;
389
    s->esc3_run_length= 0;
390 de6d9b64 Fabrice Bellard
391
#ifdef DEBUG
392
    intra_count = 0;
393
    printf("*****frame %d:\n", frame_count++);
394
#endif
395
}
396
397 ae40484c Michael Niedermayer
void msmpeg4_encode_ext_header(MpegEncContext * s)
398
{
399 2b9ab1d5 Michael Niedermayer
        put_bits(&s->pb, 5, s->frame_rate / FRAME_RATE_BASE); //yes 29.97 -> 29
400 ae40484c Michael Niedermayer
401 287229e5 Michael Niedermayer
        put_bits(&s->pb, 11, MIN(s->bit_rate, 2047));
402 ae40484c Michael Niedermayer
403 287229e5 Michael Niedermayer
        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 ae40484c Michael Niedermayer
}
410
411 de6d9b64 Fabrice Bellard
/* predict coded block */
412
static inline int coded_block_pred(MpegEncContext * s, int n, UINT8 **coded_block_ptr)
413
{
414 dbbe8999 Michael Niedermayer
    int xy, wrap, pred, a, b, c;
415 de6d9b64 Fabrice Bellard
416 dbbe8999 Michael Niedermayer
    xy = s->block_index[n];
417
    wrap = s->block_wrap[0];
418 de6d9b64 Fabrice Bellard
419
    /* B C
420
     * A X 
421
     */
422 dbbe8999 Michael Niedermayer
    a = s->coded_block[xy - 1       ];
423
    b = s->coded_block[xy - 1 - wrap];
424
    c = s->coded_block[xy     - wrap];
425 de6d9b64 Fabrice Bellard
    
426
    if (b == c) {
427
        pred = a;
428
    } else {
429
        pred = c;
430
    }
431
    
432
    /* store value */
433 dbbe8999 Michael Niedermayer
    *coded_block_ptr = &s->coded_block[xy];
434 de6d9b64 Fabrice Bellard
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 287229e5 Michael Niedermayer
        if(s->msmpeg4_version<=2){
502 3825cd1d Michael Niedermayer
            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 de6d9b64 Fabrice Bellard
    } 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 287229e5 Michael Niedermayer
        if(s->msmpeg4_version<=2){
547 3825cd1d Michael Niedermayer
            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 de6d9b64 Fabrice Bellard
            put_bits(&s->pb, 
559 3825cd1d Michael Niedermayer
                     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 de6d9b64 Fabrice Bellard
        }
576
    }
577
578
    for (i = 0; i < 6; i++) {
579
        msmpeg4_encode_block(s, block[i], i);
580
    }
581
}
582
583 92ba5ffb Michael Niedermayer
/* old ffmpeg msmpeg4v3 mode */
584
void ff_old_msmpeg4_dc_scale(MpegEncContext * s)
585 de6d9b64 Fabrice Bellard
{
586 92ba5ffb Michael Niedermayer
    if (s->qscale < 5){
587 6fb904ce Michael Niedermayer
        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 92ba5ffb Michael Niedermayer
    }else{
593 6fb904ce Michael Niedermayer
        s->y_dc_scale = s->qscale + 8;
594
        s->c_dc_scale = (s->qscale + 13)>>1;
595
    }
596 de6d9b64 Fabrice Bellard
}
597
598 f5957f3f Michael Niedermayer
static inline int msmpeg4v1_pred_dc(MpegEncContext * s, int n, 
599
                                    INT32 **dc_val_ptr)
600 287229e5 Michael Niedermayer
{
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 de6d9b64 Fabrice Bellard
/* dir = 0: left, dir = 1: top prediction */
614 f5957f3f Michael Niedermayer
static inline int msmpeg4_pred_dc(MpegEncContext * s, int n, 
615
                             UINT16 **dc_val_ptr, int *dir_ptr)
616 de6d9b64 Fabrice Bellard
{
617 dbbe8999 Michael Niedermayer
    int a, b, c, wrap, pred, scale;
618 98be975d Fabrice Bellard
    INT16 *dc_val;
619 de6d9b64 Fabrice Bellard
620
    /* find prediction */
621
    if (n < 4) {
622
        scale = s->y_dc_scale;
623
    } else {
624
        scale = s->c_dc_scale;
625
    }
626 287229e5 Michael Niedermayer
    
627 dbbe8999 Michael Niedermayer
    wrap = s->block_wrap[n];
628
    dc_val= s->dc_val[0] + s->block_index[n];
629 de6d9b64 Fabrice Bellard
630
    /* B C
631
     * A X 
632
     */
633 dbbe8999 Michael Niedermayer
    a = dc_val[ - 1];
634
    b = dc_val[ - 1 - wrap];
635
    c = dc_val[ - wrap];
636 de6d9b64 Fabrice Bellard
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 320680d4 Zdenek Kabelac
#if defined ARCH_X86 && !defined PIC
642 6f903d8e Michael Niedermayer
    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 6fe84b43 Michael Niedermayer
        "mull %4                \n\t"
649
        "movl %%edx, %0                \n\t"
650 6f903d8e Michael Niedermayer
        "movl %1, %%eax                \n\t"
651 6fe84b43 Michael Niedermayer
        "mull %4                \n\t"
652
        "movl %%edx, %1                \n\t"
653 6f903d8e Michael Niedermayer
        "movl %2, %%eax                \n\t"
654 6fe84b43 Michael Niedermayer
        "mull %4                \n\t"
655
        "movl %%edx, %2                \n\t"
656 fa778d53 Michael Niedermayer
        : "+b" (a), "+c" (b), "+D" (c)
657
        : "g" (scale), "S" (inverse[scale])
658 6f903d8e Michael Niedermayer
        : "%eax", "%edx"
659
    );
660 320680d4 Zdenek Kabelac
#else
661
    /* #elif defined (ARCH_ALPHA) */
662 1e98dffb Nick Kurshev
    /* Divisions are extremely costly on Alpha; optimize the most
663 320680d4 Zdenek Kabelac
       common case. But they are costly everywhere...
664
     */
665 1e98dffb Nick Kurshev
    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 6f903d8e Michael Niedermayer
#endif
675 de6d9b64 Fabrice Bellard
    /* XXX: WARNING: they did not choose the same test as MPEG4. This
676
       is very important ! */
677 bd5e1c72 Michael Niedermayer
    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 de6d9b64 Fabrice Bellard
    }
694
695
    /* update predictor */
696 dbbe8999 Michael Niedermayer
    *dc_val_ptr = &dc_val[0];
697 de6d9b64 Fabrice Bellard
    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 287229e5 Michael Niedermayer
    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 f5957f3f Michael Niedermayer
        UINT16 *dc_val;
715 bd5e1c72 Michael Niedermayer
        pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr);
716 de6d9b64 Fabrice Bellard
717 287229e5 Michael Niedermayer
        /* 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 de6d9b64 Fabrice Bellard
    }
724
725
    /* do the prediction */
726
    level -= pred;
727
728 287229e5 Michael Niedermayer
    if(s->msmpeg4_version<=2){
729 de6d9b64 Fabrice Bellard
        if (n < 4) {
730 3825cd1d Michael Niedermayer
            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 de6d9b64 Fabrice Bellard
        }
738 3825cd1d Michael Niedermayer
    }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 de6d9b64 Fabrice Bellard
        } else {
755 3825cd1d Michael Niedermayer
            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 de6d9b64 Fabrice Bellard
        }
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 f5957f3f Michael Niedermayer
static inline void msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n)
775 de6d9b64 Fabrice Bellard
{
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 f5957f3f Michael Niedermayer
    const UINT8 *scantable;
781 de6d9b64 Fabrice Bellard
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 f5957f3f Michael Niedermayer
        scantable= s->intra_scantable;
793 de6d9b64 Fabrice Bellard
        set_stat(ST_INTRA_AC);
794
    } else {
795
        i = 0;
796
        rl = &rl_table[3 + s->rl_table_index];
797 287229e5 Michael Niedermayer
        if(s->msmpeg4_version<=2)
798 3825cd1d Michael Niedermayer
            run_diff = 0;
799
        else
800
            run_diff = 1;
801 f5957f3f Michael Niedermayer
        scantable= s->inter_scantable;
802 de6d9b64 Fabrice Bellard
        set_stat(ST_INTER_AC);
803
    }
804
805 f5957f3f Michael Niedermayer
    /* 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 de6d9b64 Fabrice Bellard
    /* AC coefs */
813
    last_non_zero = i - 1;
814
    for (; i <= last_index; i++) {
815 f5957f3f Michael Niedermayer
        j = scantable[i];
816 de6d9b64 Fabrice Bellard
        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 f5957f3f Michael Niedermayer
            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 de6d9b64 Fabrice Bellard
            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 f5957f3f Michael Niedermayer
                        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 de6d9b64 Fabrice Bellard
                    } 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 84afee34 Michael Niedermayer
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 287229e5 Michael Niedermayer
static VLC v1_intra_cbpc_vlc;
906
static VLC v1_inter_cbpc_vlc;
907 84afee34 Michael Niedermayer
908
/* this table is practically identical to the one from h263 except that its inverted */
909 20695ec9 Falk Hüffner
static void init_h263_dc_for_msmpeg4(void)
910 84afee34 Michael Niedermayer
{
911
        int level, uni_code, uni_len;
912
913 2ed627e6 Michael Niedermayer
        for(level=-256; level<256; level++){
914 84afee34 Michael Niedermayer
            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 de6d9b64 Fabrice Bellard
963
/* init all vlc decoding tables */
964 f5957f3f Michael Niedermayer
int ff_msmpeg4_decode_init(MpegEncContext *s)
965 de6d9b64 Fabrice Bellard
{
966 d81c5983 Fabrice Bellard
    static int done = 0;
967 de6d9b64 Fabrice Bellard
    int i;
968
    MVTable *mv;
969
970 f5957f3f Michael Niedermayer
    common_init(s);
971 de6d9b64 Fabrice Bellard
972 d81c5983 Fabrice Bellard
    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 84afee34 Michael Niedermayer
    
999 d81c5983 Fabrice Bellard
        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 84afee34 Michael Niedermayer
    
1006 d81c5983 Fabrice Bellard
        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 287229e5 Michael Niedermayer
        
1026 d81c5983 Fabrice Bellard
        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 de6d9b64 Fabrice Bellard
    return 0;
1034
}
1035
1036
static int decode012(GetBitContext *gb)
1037
{
1038
    int n;
1039 612476ef Arpi
    n = get_bits1(gb);
1040 de6d9b64 Fabrice Bellard
    if (n == 0)
1041
        return 0;
1042
    else
1043 612476ef Arpi
        return get_bits1(gb) + 1;
1044 de6d9b64 Fabrice Bellard
}
1045
1046 84afee34 Michael Niedermayer
int msmpeg4_decode_picture_header(MpegEncContext * s)
1047 de6d9b64 Fabrice Bellard
{
1048 f5957f3f Michael Niedermayer
    int code;
1049 de6d9b64 Fabrice Bellard
1050 e1a9dbff Michael Niedermayer
#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 287229e5 Michael Niedermayer
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 de6d9b64 Fabrice Bellard
    s->pict_type = get_bits(&s->gb, 2) + 1;
1073
    if (s->pict_type != I_TYPE &&
1074 287229e5 Michael Niedermayer
        s->pict_type != P_TYPE){
1075
        fprintf(stderr, "invalid picture type\n");
1076 de6d9b64 Fabrice Bellard
        return -1;
1077 287229e5 Michael Niedermayer
    }
1078 de6d9b64 Fabrice Bellard
1079
    s->qscale = get_bits(&s->gb, 5);
1080
1081
    if (s->pict_type == I_TYPE) {
1082
        code = get_bits(&s->gb, 5); 
1083 287229e5 Michael Niedermayer
        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 e1a9dbff Michael Niedermayer
1098
        switch(s->msmpeg4_version){
1099 287229e5 Michael Niedermayer
        case 1:
1100 e1a9dbff Michael Niedermayer
        case 2:
1101 84afee34 Michael Niedermayer
            s->rl_chroma_table_index = 2;
1102
            s->rl_table_index = 2;
1103 7f89b6fb Michael Niedermayer
1104 84afee34 Michael Niedermayer
            s->dc_table_index = 0; //not used
1105 e1a9dbff Michael Niedermayer
            break;
1106
        case 3:
1107 84afee34 Michael Niedermayer
            s->rl_chroma_table_index = decode012(&s->gb);
1108
            s->rl_table_index = decode012(&s->gb);
1109 7f89b6fb Michael Niedermayer
1110 84afee34 Michael Niedermayer
            s->dc_table_index = get_bits1(&s->gb);
1111 e1a9dbff Michael Niedermayer
            break;
1112
        case 4:
1113 f5957f3f Michael Niedermayer
            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 e1a9dbff Michael Niedermayer
            }
1120
1121 f5957f3f Michael Niedermayer
            s->dc_table_index = get_bits1(&s->gb);
1122 e1a9dbff Michael Niedermayer
            break;
1123 84afee34 Michael Niedermayer
        }
1124 de6d9b64 Fabrice Bellard
        s->no_rounding = 1;
1125 f5957f3f Michael Niedermayer
/*        printf(" %d %d %d %d %d    \n", 
1126 badaf88e Michael Niedermayer
                s->qscale,
1127
                s->rl_chroma_table_index,
1128
                s->rl_table_index, 
1129 f5957f3f Michael Niedermayer
                s->dc_table_index,
1130
                s->per_mb_rl_table);*/
1131 de6d9b64 Fabrice Bellard
    } else {
1132
        
1133 287229e5 Michael Niedermayer
        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 84afee34 Michael Niedermayer
            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 287229e5 Michael Niedermayer
            break;
1145
        case 3:
1146
            s->use_skip_mb_code = get_bits1(&s->gb);
1147 84afee34 Michael Niedermayer
            s->rl_table_index = decode012(&s->gb);
1148
            s->rl_chroma_table_index = s->rl_table_index;
1149 de6d9b64 Fabrice Bellard
1150 84afee34 Michael Niedermayer
            s->dc_table_index = get_bits1(&s->gb);
1151 de6d9b64 Fabrice Bellard
1152 84afee34 Michael Niedermayer
            s->mv_table_index = get_bits1(&s->gb);
1153 287229e5 Michael Niedermayer
            break;
1154 f5957f3f Michael Niedermayer
        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 84afee34 Michael Niedermayer
        }
1167 f5957f3f Michael Niedermayer
/*        printf(" %d %d %d %d %d %d    \n", 
1168 badaf88e Michael Niedermayer
                s->use_skip_mb_code, 
1169
                s->rl_table_index, 
1170
                s->rl_chroma_table_index, 
1171
                s->dc_table_index,
1172 f5957f3f Michael Niedermayer
                s->mv_table_index,
1173
                s->per_mb_rl_table);*/
1174 ae40484c Michael Niedermayer
        if(s->flipflop_rounding){
1175
            s->no_rounding ^= 1;
1176
        }else{
1177
            s->no_rounding = 0;
1178
        }
1179 de6d9b64 Fabrice Bellard
    }
1180 f5957f3f Michael Niedermayer
1181
    s->esc3_level_length= 0;
1182
    s->esc3_run_length= 0;
1183 84afee34 Michael Niedermayer
1184 de6d9b64 Fabrice Bellard
#ifdef DEBUG
1185
    printf("*****frame %d:\n", frame_count++);
1186
#endif
1187
    return 0;
1188
}
1189
1190 ae40484c Michael Niedermayer
int msmpeg4_decode_ext_header(MpegEncContext * s, int buf_size)
1191
{
1192 287229e5 Michael Niedermayer
    int left= buf_size*8 - get_bits_count(&s->gb);
1193
    int length= s->msmpeg4_version>=3 ? 17 : 16;
1194 ae40484c Michael Niedermayer
    /* the alt_bitstream reader could read over the end so we need to check it */
1195 287229e5 Michael Niedermayer
    if(left>=length && left<length+8)
1196 ae40484c Michael Niedermayer
    {
1197 2b9ab1d5 Michael Niedermayer
        int fps;
1198
1199
        fps= get_bits(&s->gb, 5);
1200 287229e5 Michael Niedermayer
        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 2b9ab1d5 Michael Niedermayer
1206 287229e5 Michael Niedermayer
//        printf("fps:%2d bps:%2d roundingType:%1d\n", fps, s->bit_rate, s->flipflop_rounding);
1207 ae40484c Michael Niedermayer
    }
1208 287229e5 Michael Niedermayer
    else if(left<length+8)
1209 ae40484c Michael Niedermayer
    {
1210 2b9ab1d5 Michael Niedermayer
        s->flipflop_rounding= 0;
1211 287229e5 Michael Niedermayer
        printf("ext header missing, %d left\n", left);
1212
    }
1213
    else
1214
    {
1215
        fprintf(stderr, "I frame too long, ignoring ext header\n");
1216 ae40484c Michael Niedermayer
    }
1217 2b9ab1d5 Michael Niedermayer
1218 ae40484c Michael Niedermayer
    return 0;
1219
}
1220
1221 cd4af68a Zdenek Kabelac
static inline void msmpeg4_memsetw(short *tab, int val, int n)
1222 de6d9b64 Fabrice Bellard
{
1223
    int i;
1224
    for(i=0;i<n;i++)
1225
        tab[i] = val;
1226
}
1227
1228 3825cd1d Michael Niedermayer
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 84afee34 Michael Niedermayer
/* 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 287229e5 Michael Niedermayer
//     printf("MV code %d at %d %d pred: %d\n", code, s->mb_x,s->mb_y, pred);
1268 84afee34 Michael Niedermayer
    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 287229e5 Michael Niedermayer
    val += pred;
1283 84afee34 Michael Niedermayer
    if (val <= -64)
1284
        val += 64;
1285
    else if (val >= 64)
1286
        val -= 64;
1287
1288
    return val;
1289
}
1290
1291
1292 287229e5 Michael Niedermayer
static int msmpeg4v12_decode_mb(MpegEncContext *s, 
1293 84afee34 Michael Niedermayer
                      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 287229e5 Michael Niedermayer
        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 84afee34 Michael Niedermayer
        s->mb_intra = code >>2;
1322
    
1323
        cbp = code & 0x3;
1324
    } else {
1325
        s->mb_intra = 1;
1326 287229e5 Michael Niedermayer
        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 84afee34 Michael Niedermayer
    }
1335
1336
    if (!s->mb_intra) {
1337 287229e5 Michael Niedermayer
        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 84afee34 Michael Niedermayer
1345 287229e5 Michael Niedermayer
        cbp|= cbpy<<2;
1346
        if(s->msmpeg4_version==1 || (cbp&3) != 3) cbp^= 0x3C;
1347 84afee34 Michael Niedermayer
        
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 287229e5 Michael Niedermayer
        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 84afee34 Michael Niedermayer
    }
1366
1367
    for (i = 0; i < 6; i++) {
1368
        if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1) < 0)
1369
        {
1370 287229e5 Michael Niedermayer
             fprintf(stderr,"\nerror while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
1371 84afee34 Michael Niedermayer
             return -1;
1372
        }
1373
    }
1374
    return 0;
1375
}
1376
1377 de6d9b64 Fabrice Bellard
int msmpeg4_decode_mb(MpegEncContext *s, 
1378
                      DCTELEM block[6][64])
1379
{
1380
    int cbp, code, i;
1381
    UINT8 *coded_val;
1382
1383 bd5e1c72 Michael Niedermayer
#ifdef PRINT_MB
1384
if(s->mb_x==0){
1385
    printf("\n");
1386
    if(s->mb_y==0) printf("\n");
1387
}
1388
#endif
1389 de6d9b64 Fabrice Bellard
    /* special slice handling */
1390
    if (s->mb_x == 0) {
1391 525782f3 Zdenek Kabelac
        if (s->slice_height && (s->mb_y % s->slice_height) == 0) {
1392 de6d9b64 Fabrice Bellard
            int wrap;
1393
            /* reset DC pred (set previous line to 1024) */
1394
            wrap = 2 * s->mb_width + 2;
1395 cd4af68a Zdenek Kabelac
            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 de6d9b64 Fabrice Bellard
1413
            s->first_slice_line = 1;
1414
        } else {
1415
            s->first_slice_line = 0; 
1416
        }
1417
    }
1418
1419 287229e5 Michael Niedermayer
    if(s->msmpeg4_version<=2) return msmpeg4v12_decode_mb(s, block); //FIXME export function & call from outside perhaps
1420 84afee34 Michael Niedermayer
    
1421 de6d9b64 Fabrice Bellard
    if (s->pict_type == P_TYPE) {
1422
        set_stat(ST_INTER_MB);
1423
        if (s->use_skip_mb_code) {
1424 612476ef Arpi
            if (get_bits1(&s->gb)) {
1425 de6d9b64 Fabrice Bellard
                /* 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 3bb4e23a Fabrice Bellard
                s->mb_skiped = 1;
1434 bd5e1c72 Michael Niedermayer
#ifdef PRINT_MB
1435
printf("S ");
1436
#endif
1437 de6d9b64 Fabrice Bellard
                return 0;
1438
            }
1439
        }
1440
        
1441
        code = get_vlc(&s->gb, &mb_non_intra_vlc);
1442
        if (code < 0)
1443
            return -1;
1444 38d171eb Zdenek Kabelac
        //s->mb_intra = (code & 0x40) ? 0 : 1;
1445
        s->mb_intra = (~code & 0x40) >> 6;
1446 de6d9b64 Fabrice Bellard
            
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 38d171eb Zdenek Kabelac
            int val = ((code >> (5 - i)) & 1);
1458 de6d9b64 Fabrice Bellard
            if (i < 4) {
1459 38d171eb Zdenek Kabelac
                int pred = coded_block_pred(s, i, &coded_val);
1460 de6d9b64 Fabrice Bellard
                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 f5957f3f Michael Niedermayer
//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 de6d9b64 Fabrice Bellard
        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 bd5e1c72 Michael Niedermayer
#ifdef PRINT_MB
1483
printf("P ");
1484
#endif
1485 de6d9b64 Fabrice Bellard
    } else {
1486 f5957f3f Michael Niedermayer
//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 de6d9b64 Fabrice Bellard
        set_stat(ST_INTRA_MB);
1488 612476ef Arpi
        s->ac_pred = get_bits1(&s->gb);
1489 bd5e1c72 Michael Niedermayer
#ifdef PRINT_MB
1490
printf("%c", s->ac_pred ? 'A' : 'I');
1491
#endif
1492 f5957f3f Michael Niedermayer
        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 de6d9b64 Fabrice Bellard
    }
1497
1498
    for (i = 0; i < 6; i++) {
1499
        if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1) < 0)
1500 38d171eb Zdenek Kabelac
        {
1501 287229e5 Michael Niedermayer
            fprintf(stderr,"\nerror while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
1502
            return -1;
1503 38d171eb Zdenek Kabelac
        }
1504 de6d9b64 Fabrice Bellard
    }
1505 bd5e1c72 Michael Niedermayer
    
1506 de6d9b64 Fabrice Bellard
    return 0;
1507
}
1508
1509 f5957f3f Michael Niedermayer
static inline int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
1510 de6d9b64 Fabrice Bellard
                              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 badaf88e Michael Niedermayer
    int qmul, qadd;
1517 de6d9b64 Fabrice Bellard
1518
    if (s->mb_intra) {
1519 badaf88e Michael Niedermayer
        qmul=1;
1520
        qadd=0;
1521
1522 de6d9b64 Fabrice Bellard
        /* DC coef */
1523
        set_stat(ST_DC);
1524
        level = msmpeg4_decode_dc(s, n, &dc_pred_dir);
1525 bd5e1c72 Michael Niedermayer
#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 287229e5 Michael Niedermayer
        if (level < 0){
1534 bd5e1c72 Michael Niedermayer
            fprintf(stderr, "dc overflow- block: %d qscale: %d//\n", n, s->qscale);
1535 de6d9b64 Fabrice Bellard
            return -1;
1536 287229e5 Michael Niedermayer
        }
1537 de6d9b64 Fabrice Bellard
        if (n < 4) {
1538
            rl = &rl_table[s->rl_table_index];
1539 287229e5 Michael Niedermayer
            if(level > 256*s->y_dc_scale){
1540 bd5e1c72 Michael Niedermayer
                fprintf(stderr, "dc overflow+ L qscale: %d//\n", s->qscale);
1541 287229e5 Michael Niedermayer
                return -1;
1542
            }
1543 de6d9b64 Fabrice Bellard
        } else {
1544
            rl = &rl_table[3 + s->rl_chroma_table_index];
1545 287229e5 Michael Niedermayer
            if(level > 256*s->c_dc_scale){
1546 bd5e1c72 Michael Niedermayer
                fprintf(stderr, "dc overflow+ C qscale: %d//\n", s->qscale);
1547 287229e5 Michael Niedermayer
                return -1;
1548
            }
1549 de6d9b64 Fabrice Bellard
        }
1550 287229e5 Michael Niedermayer
        block[0] = level;
1551 badaf88e Michael Niedermayer
1552 de6d9b64 Fabrice Bellard
        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 f5957f3f Michael Niedermayer
                scan_table = s->intra_v_scantable; /* left */
1560 de6d9b64 Fabrice Bellard
            else
1561 f5957f3f Michael Niedermayer
                scan_table = s->intra_h_scantable; /* top */
1562 de6d9b64 Fabrice Bellard
        } else {
1563 f5957f3f Michael Niedermayer
            scan_table = s->intra_scantable;
1564 de6d9b64 Fabrice Bellard
        }
1565
        set_stat(ST_INTRA_AC);
1566
    } else {
1567 badaf88e Michael Niedermayer
        qmul = s->qscale << 1;
1568
        qadd = (s->qscale - 1) | 1;
1569 de6d9b64 Fabrice Bellard
        i = 0;
1570
        rl = &rl_table[3 + s->rl_table_index];
1571 84afee34 Michael Niedermayer
1572
        if(s->msmpeg4_version==2)
1573
            run_diff = 0;
1574
        else
1575
            run_diff = 1;
1576
1577 de6d9b64 Fabrice Bellard
        if (!coded) {
1578
            s->block_last_index[n] = i - 1;
1579
            return 0;
1580
        }
1581 f5957f3f Michael Niedermayer
        scan_table = s->inter_scantable;
1582 de6d9b64 Fabrice Bellard
        set_stat(ST_INTER_AC);
1583
    }
1584
1585
    for(;;) {
1586
        code = get_vlc(&s->gb, &rl->vlc);
1587 f5957f3f Michael Niedermayer
        if (code < 0){
1588
            fprintf(stderr, "illegal AC-VLC code at %d %d\n", s->mb_x, s->mb_y);
1589 de6d9b64 Fabrice Bellard
            return -1;
1590 f5957f3f Michael Niedermayer
        }
1591 de6d9b64 Fabrice Bellard
        if (code == rl->n) {
1592
            /* escape */
1593 287229e5 Michael Niedermayer
            if (s->msmpeg4_version==1 || get_bits1(&s->gb) == 0) {
1594
                if (s->msmpeg4_version==1 || get_bits1(&s->gb) == 0) {
1595 de6d9b64 Fabrice Bellard
                    /* third escape */
1596 f5957f3f Michael Niedermayer
                    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 287229e5 Michael Niedermayer
#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 f5957f3f Michael Niedermayer
                            if(run1>=0 && abs_level <= rl->max_level[last][run1]){
1642 287229e5 Michael Niedermayer
                                fprintf(stderr, "illegal 3. esc, esc 2 encoding possible\n");
1643
                                return DECODING_AC_LOST;
1644
                            }
1645
                        }
1646
                    }
1647
#endif
1648 38d171eb Zdenek Kabelac
                    //level = level * qmul + (level>0) * qadd - (level<=0) * qadd ;
1649
                    if (level>0) level= level * qmul + qadd;
1650 287229e5 Michael Niedermayer
                    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 de6d9b64 Fabrice Bellard
                } else {
1658
                    /* second escape */
1659
                    code = get_vlc(&s->gb, &rl->vlc);
1660 f5957f3f Michael Niedermayer
                    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 de6d9b64 Fabrice Bellard
                        return -1;
1663 f5957f3f Michael Niedermayer
                    }
1664 de6d9b64 Fabrice Bellard
                    run = rl->table_run[code];
1665 99180fe3 Michael Niedermayer
                    level = rl->table_level[code];
1666 de6d9b64 Fabrice Bellard
                    last = code >= rl->last;
1667
                    run += rl->max_run[last][level] + run_diff;
1668 99180fe3 Michael Niedermayer
                    level= level * qmul + qadd;
1669 612476ef Arpi
                    if (get_bits1(&s->gb))
1670 de6d9b64 Fabrice Bellard
                        level = -level;
1671
                }
1672
            } else {
1673
                /* first escape */
1674
                code = get_vlc(&s->gb, &rl->vlc);
1675 f5957f3f Michael Niedermayer
                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 de6d9b64 Fabrice Bellard
                    return -1;
1678 f5957f3f Michael Niedermayer
                }
1679 de6d9b64 Fabrice Bellard
                run = rl->table_run[code];
1680
                level = rl->table_level[code];
1681
                last = code >= rl->last;
1682
                level += rl->max_level[last][run];
1683 badaf88e Michael Niedermayer
                level= level * qmul + qadd;
1684 612476ef Arpi
                if (get_bits1(&s->gb))
1685 de6d9b64 Fabrice Bellard
                    level = -level;
1686
            }
1687
        } else {
1688
            run = rl->table_run[code];
1689 badaf88e Michael Niedermayer
            level = rl->table_level[code] * qmul + qadd;
1690 de6d9b64 Fabrice Bellard
            last = code >= rl->last;
1691 612476ef Arpi
            if (get_bits1(&s->gb))
1692 de6d9b64 Fabrice Bellard
                level = -level;
1693
        }
1694
        i += run;
1695 f5957f3f Michael Niedermayer
        if (i >= 64){
1696
            fprintf(stderr, "run too long at %d %d\n", s->mb_x, s->mb_y);
1697 de6d9b64 Fabrice Bellard
            return -1;
1698 f5957f3f Michael Niedermayer
        }
1699 287229e5 Michael Niedermayer
1700 de6d9b64 Fabrice Bellard
        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 f5957f3f Michael Niedermayer
    if(s->msmpeg4_version==4 && i>1) i=64; //FIXME/XXX optimize
1714 de6d9b64 Fabrice Bellard
    s->block_last_index[n] = i - 1;
1715 f5957f3f Michael Niedermayer
    
1716 de6d9b64 Fabrice Bellard
    return 0;
1717
}
1718
1719
static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr)
1720
{
1721
    int level, pred;
1722
1723 287229e5 Michael Niedermayer
    if(s->msmpeg4_version<=2){
1724 84afee34 Michael Niedermayer
        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 2ed627e6 Michael Niedermayer
        if (level < 0) 
1730 84afee34 Michael Niedermayer
            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 287229e5 Michael Niedermayer
        if (level < 0){
1739
            fprintf(stderr, "illegal dc vlc\n");
1740 84afee34 Michael Niedermayer
            return -1;
1741 287229e5 Michael Niedermayer
        }
1742 84afee34 Michael Niedermayer
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 de6d9b64 Fabrice Bellard
    }
1752
1753 287229e5 Michael Niedermayer
    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 f5957f3f Michael Niedermayer
        UINT16 *dc_val;
1762 bd5e1c72 Michael Niedermayer
        pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr);
1763 287229e5 Michael Niedermayer
        level += pred;
1764 de6d9b64 Fabrice Bellard
1765 287229e5 Michael Niedermayer
        /* 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 de6d9b64 Fabrice Bellard
    }
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 f5957f3f Michael Niedermayer
    if (code < 0){
1786
        fprintf(stderr, "illegal MV code at %d %d\n", s->mb_x, s->mb_y);
1787 de6d9b64 Fabrice Bellard
        return -1;
1788 f5957f3f Michael Niedermayer
    }
1789 de6d9b64 Fabrice Bellard
    if (code == mv->n) {
1790 f5957f3f Michael Niedermayer
//printf("MV ESC %X at %d %d\n", show_bits(&s->gb, 24), s->mb_x, s->mb_y);
1791 de6d9b64 Fabrice Bellard
        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
}