Statistics
| Branch: | Revision:

ffmpeg / libavcodec / msmpeg4.c @ bb71e317

History | View | Annotate | Download (36.9 KB)

1
/*
2
 * MSMPEG4 backend for ffmpeg encoder and decoder
3
 * Copyright (c) 2001 Gerard Lantau.
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 2 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program 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
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
 */
19
#include <stdlib.h>
20
#include <stdio.h>
21
#include "common.h"
22
#include "dsputil.h"
23
#include "mpegvideo.h"
24
#include "avcodec.h"
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
/* motion vector table */
36
typedef struct MVTable {
37
    int n;
38
    const UINT16 *table_mv_code;
39
    const UINT8 *table_mv_bits;
40
    const UINT8 *table_mvx;
41
    const UINT8 *table_mvy;
42
    UINT16 *table_mv_index; /* encoding: convert mv to index in table_mv */
43
    VLC vlc;                /* decoding: vlc */
44
} MVTable;
45

    
46
static UINT32 v2_dc_lum_table[512][2];
47
static UINT32 v2_dc_chroma_table[512][2];
48

    
49
static void msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n);
50
static int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
51
                                int n, int coded);
52
static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr);
53
static int msmpeg4_decode_motion(MpegEncContext * s, 
54
                                 int *mx_ptr, int *my_ptr);
55
static void msmpeg4v2_encode_motion(MpegEncContext * s, int val);
56
static void init_h263_dc_for_msmpeg4();
57

    
58

    
59
extern UINT32 inverse[256];
60

    
61
#ifdef DEBUG
62
int intra_count = 0;
63
int frame_count = 0;
64
#endif
65
/* XXX: move it to mpegvideo.h */
66

    
67
static int init_done = 0;
68

    
69
#include "msmpeg4data.h"
70

    
71
#ifdef STATS
72

    
73
const char *st_names[ST_NB] = {
74
    "unknown",
75
    "dc",
76
    "intra_ac",
77
    "inter_ac",
78
    "intra_mb",
79
    "inter_mb",
80
    "mv",
81
};
82

    
83
int st_current_index = 0;
84
unsigned int st_bit_counts[ST_NB];
85
unsigned int st_out_bit_counts[ST_NB];
86

    
87
#define set_stat(var) st_current_index = var;
88

    
89
void print_stats(void)
90
{
91
    unsigned int total;
92
    int i;
93

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

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

    
129
#else
130

    
131
#define set_stat(var)
132

    
133
#endif
134

    
135
/* build the table which associate a (x,y) motion vector to a vlc */
136
static void init_mv_table(MVTable *tab)
137
{
138
    int i, x, y;
139

    
140
    tab->table_mv_index = malloc(sizeof(UINT16) * 4096);
141
    /* mark all entries as not used */
142
    for(i=0;i<4096;i++)
143
        tab->table_mv_index[i] = tab->n;
144
    
145
    for(i=0;i<tab->n;i++) {
146
        x = tab->table_mvx[i];
147
        y = tab->table_mvy[i];
148
        tab->table_mv_index[(x << 6) | y] = i;
149
    }
150
}
151

    
152
static void code012(PutBitContext *pb, int n)
153
{
154
    if (n == 0) {
155
        put_bits(pb, 1, 0);
156
    } else {
157
        put_bits(pb, 1, 1);
158
        put_bits(pb, 1, (n >= 2));
159
    }
160
}
161

    
162
/* write MSMPEG4 V3 compatible frame header */
163
void msmpeg4_encode_picture_header(MpegEncContext * s, int picture_number)
164
{
165
    int i;
166

    
167
    align_put_bits(&s->pb);
168

    
169
    put_bits(&s->pb, 2, s->pict_type - 1);
170

    
171
    put_bits(&s->pb, 5, s->qscale);
172

    
173
    s->rl_table_index = 2;
174
    if(s->msmpeg4_version==2)
175
        s->rl_chroma_table_index = 2; /* only for I frame */
176
    else
177
        s->rl_chroma_table_index = 1; /* only for I frame */
178

    
179
    s->dc_table_index = 1;
180
    s->mv_table_index = 1; /* only if P frame */
181
    s->use_skip_mb_code = 1; /* only if P frame */
182
    
183
    if (s->pict_type == I_TYPE) {
184
        put_bits(&s->pb, 5, 0x17); /* indicate only one "slice" */
185

    
186
        if(s->msmpeg4_version!=2){
187
            code012(&s->pb, s->rl_chroma_table_index);
188
            code012(&s->pb, s->rl_table_index);
189

    
190
            put_bits(&s->pb, 1, s->dc_table_index);
191
        }
192
        s->no_rounding = 1;
193
    } else {
194
        put_bits(&s->pb, 1, s->use_skip_mb_code);
195
        
196
        s->rl_chroma_table_index = s->rl_table_index;
197
        if(s->msmpeg4_version!=2){
198
            code012(&s->pb, s->rl_table_index);
199

    
200
            put_bits(&s->pb, 1, s->dc_table_index);
201

    
202
            put_bits(&s->pb, 1, s->mv_table_index);
203
        }
204

    
205
        if(s->flipflop_rounding){
206
            s->no_rounding ^= 1;
207
        }else{
208
            s->no_rounding = 0;
209
        }
210
    }
211

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

    
220
        init_h263_dc_for_msmpeg4();
221
    }
222

    
223
#ifdef DEBUG
224
    intra_count = 0;
225
    printf("*****frame %d:\n", frame_count++);
226
#endif
227
}
228

    
229
void msmpeg4_encode_ext_header(MpegEncContext * s)
230
{
231
        s->flipflop_rounding=1;
232
        s->bitrate= 910; // FIXME
233

    
234
        put_bits(&s->pb, 5, s->frame_rate / FRAME_RATE_BASE); //yes 29.97 -> 29
235

    
236
        put_bits(&s->pb, 11, s->bitrate);
237

    
238
        put_bits(&s->pb, 1, s->flipflop_rounding);
239
}
240

    
241
/* predict coded block */
242
static inline int coded_block_pred(MpegEncContext * s, int n, UINT8 **coded_block_ptr)
243
{
244
    int xy, wrap, pred, a, b, c;
245

    
246
    xy = s->block_index[n];
247
    wrap = s->block_wrap[0];
248

    
249
    /* B C
250
     * A X 
251
     */
252
    a = s->coded_block[xy - 1       ];
253
    b = s->coded_block[xy - 1 - wrap];
254
    c = s->coded_block[xy     - wrap];
255
    
256
    if (b == c) {
257
        pred = a;
258
    } else {
259
        pred = c;
260
    }
261
    
262
    /* store value */
263
    *coded_block_ptr = &s->coded_block[xy];
264

    
265
    return pred;
266
}
267

    
268
static void msmpeg4_encode_motion(MpegEncContext * s, 
269
                                  int mx, int my)
270
{
271
    int code;
272
    MVTable *mv;
273

    
274
    /* modulo encoding */
275
    /* WARNING : you cannot reach all the MVs even with the modulo
276
       encoding. This is a somewhat strange compromise they took !!!  */
277
    if (mx <= -64)
278
        mx += 64;
279
    else if (mx >= 64)
280
        mx -= 64;
281
    if (my <= -64)
282
        my += 64;
283
    else if (my >= 64)
284
        my -= 64;
285
    
286
    mx += 32;
287
    my += 32;
288
#if 0
289
    if ((unsigned)mx >= 64 ||
290
        (unsigned)my >= 64) 
291
        fprintf(stderr, "error mx=%d my=%d\n", mx, my);
292
#endif
293
    mv = &mv_tables[s->mv_table_index];
294

    
295
    code = mv->table_mv_index[(mx << 6) | my];
296
    set_stat(ST_MV);
297
    put_bits(&s->pb, 
298
             mv->table_mv_bits[code], 
299
             mv->table_mv_code[code]);
300
    if (code == mv->n) {
301
        /* escape : code litterally */
302
        put_bits(&s->pb, 6, mx);
303
        put_bits(&s->pb, 6, my);
304
    }
305
}
306

    
307
void msmpeg4_encode_mb(MpegEncContext * s, 
308
                       DCTELEM block[6][64],
309
                       int motion_x, int motion_y)
310
{
311
    int cbp, coded_cbp, i;
312
    int pred_x, pred_y;
313
    UINT8 *coded_block;
314

    
315
    if (!s->mb_intra) {
316
        /* compute cbp */
317
        set_stat(ST_INTER_MB);
318
        cbp = 0;
319
        for (i = 0; i < 6; i++) {
320
            if (s->block_last_index[i] >= 0)
321
                cbp |= 1 << (5 - i);
322
        }
323
        if (s->use_skip_mb_code && (cbp | motion_x | motion_y) == 0) {
324
            /* skip macroblock */
325
            put_bits(&s->pb, 1, 1);
326
            return;
327
        }
328
        if (s->use_skip_mb_code)
329
            put_bits(&s->pb, 1, 0);        /* mb coded */
330
        
331
        if(s->msmpeg4_version==2){
332
            put_bits(&s->pb, 
333
                     v2_mb_type[cbp&3][1], 
334
                     v2_mb_type[cbp&3][0]);
335
            if((cbp&3) != 3) coded_cbp= cbp ^ 0x3C;
336
            else             coded_cbp= cbp;
337

    
338
            put_bits(&s->pb, 
339
                     cbpy_tab[coded_cbp>>2][1], 
340
                     cbpy_tab[coded_cbp>>2][0]);
341
                        
342
            h263_pred_motion(s, 0, &pred_x, &pred_y);
343
            msmpeg4v2_encode_motion(s, motion_x - pred_x);
344
            msmpeg4v2_encode_motion(s, motion_y - pred_y);
345
        }else{
346
            put_bits(&s->pb, 
347
                     table_mb_non_intra[cbp + 64][1], 
348
                     table_mb_non_intra[cbp + 64][0]);
349

    
350
            /* motion vector */
351
            h263_pred_motion(s, 0, &pred_x, &pred_y);
352
            msmpeg4_encode_motion(s, motion_x - pred_x, 
353
                                  motion_y - pred_y);
354
        }
355
    } else {
356
        /* compute cbp */
357
        cbp = 0;
358
        coded_cbp = 0;
359
        for (i = 0; i < 6; i++) {
360
            int val, pred;
361
            val = (s->block_last_index[i] >= 1);
362
            cbp |= val << (5 - i);
363
            if (i < 4) {
364
                /* predict value for close blocks only for luma */
365
                pred = coded_block_pred(s, i, &coded_block);
366
                *coded_block = val;
367
                val = val ^ pred;
368
            }
369
            coded_cbp |= val << (5 - i);
370
        }
371
#if 0
372
        if (coded_cbp)
373
            printf("cbp=%x %x\n", cbp, coded_cbp);
374
#endif
375

    
376
        if(s->msmpeg4_version==2){
377
            if (s->pict_type == I_TYPE) {
378
                put_bits(&s->pb, 
379
                         v2_intra_cbpc[cbp&3][1], v2_intra_cbpc[cbp&3][0]);
380
            } else {
381
                if (s->use_skip_mb_code)
382
                    put_bits(&s->pb, 1, 0);        /* mb coded */
383
                put_bits(&s->pb, 
384
                         v2_mb_type[(cbp&3) + 4][1], 
385
                         v2_mb_type[(cbp&3) + 4][0]);
386
            }
387
            put_bits(&s->pb, 1, 0);        /* no AC prediction yet */
388
            put_bits(&s->pb, 
389
                     cbpy_tab[cbp>>2][1], 
390
                     cbpy_tab[cbp>>2][0]);
391
        }else{
392
            if (s->pict_type == I_TYPE) {
393
                set_stat(ST_INTRA_MB);
394
                put_bits(&s->pb, 
395
                         table_mb_intra[coded_cbp][1], table_mb_intra[coded_cbp][0]);
396
            } else {
397
                if (s->use_skip_mb_code)
398
                    put_bits(&s->pb, 1, 0);        /* mb coded */
399
                put_bits(&s->pb, 
400
                         table_mb_non_intra[cbp][1], 
401
                         table_mb_non_intra[cbp][0]);
402
            }
403
            set_stat(ST_INTRA_MB);
404
            put_bits(&s->pb, 1, 0);        /* no AC prediction yet */
405
        }
406
    }
407

    
408
    for (i = 0; i < 6; i++) {
409
        msmpeg4_encode_block(s, block[i], i);
410
    }
411
}
412

    
413

    
414
#if 0
415
/* identical to mpeg4 for msmpeg4v3 but not msmpeg4v2 */
416
void msmpeg4_dc_scale(MpegEncContext * s)
417
{
418
    if (s->qscale < 5 || s->msmpeg4_version==2){
419
        s->y_dc_scale = 8;
420
        s->c_dc_scale = 8;
421
    }else if (s->qscale < 9){
422
        s->y_dc_scale = 2 * s->qscale;
423
        s->c_dc_scale = (s->qscale + 13)>>1;
424
    }else if(s->qscale < 25){
425
        s->y_dc_scale = s->qscale + 8;
426
        s->c_dc_scale = (s->qscale + 13)>>1;
427
    }else{
428
        s->y_dc_scale = 2 * s->qscale - 16;
429
        s->c_dc_scale = s->qscale - 6;
430
    }
431
}
432
#endif
433

    
434
/* dir = 0: left, dir = 1: top prediction */
435
static int msmpeg4_pred_dc(MpegEncContext * s, int n, 
436
                           INT16 **dc_val_ptr, int *dir_ptr)
437
{
438
    int a, b, c, wrap, pred, scale;
439
    INT16 *dc_val;
440

    
441
    /* find prediction */
442
    if (n < 4) {
443
        scale = s->y_dc_scale;
444
    } else {
445
        scale = s->c_dc_scale;
446
    }
447
    wrap = s->block_wrap[n];
448
    dc_val= s->dc_val[0] + s->block_index[n];
449

    
450
    /* B C
451
     * A X 
452
     */
453
    a = dc_val[ - 1];
454
    b = dc_val[ - 1 - wrap];
455
    c = dc_val[ - wrap];
456

    
457
    /* XXX: the following solution consumes divisions, but it does not
458
       necessitate to modify mpegvideo.c. The problem comes from the
459
       fact they decided to store the quantized DC (which would lead
460
       to problems if Q could vary !) */
461
#if defined ARCH_X86 && !defined PIC
462
    asm volatile(
463
        "movl %3, %%eax                \n\t"
464
        "shrl $1, %%eax                \n\t"
465
        "addl %%eax, %2                \n\t"
466
        "addl %%eax, %1                \n\t"
467
        "addl %0, %%eax                \n\t"
468
        "mull %4                \n\t"
469
        "movl %%edx, %0                \n\t"
470
        "movl %1, %%eax                \n\t"
471
        "mull %4                \n\t"
472
        "movl %%edx, %1                \n\t"
473
        "movl %2, %%eax                \n\t"
474
        "mull %4                \n\t"
475
        "movl %%edx, %2                \n\t"
476
        : "+b" (a), "+c" (b), "+D" (c)
477
        : "g" (scale), "S" (inverse[scale])
478
        : "%eax", "%edx"
479
    );
480
#else
481
    /* #elif defined (ARCH_ALPHA) */
482
    /* Divisions are extremely costly on Alpha; optimize the most
483
       common case. But they are costly everywhere...
484
     */
485
    if (scale == 8) {
486
        a = (a + (8 >> 1)) / 8;
487
        b = (b + (8 >> 1)) / 8;
488
        c = (c + (8 >> 1)) / 8;
489
    } else {
490
        a = (a + (scale >> 1)) / scale;
491
        b = (b + (scale >> 1)) / scale;
492
        c = (c + (scale >> 1)) / scale;
493
    }
494
#endif
495
    /* XXX: WARNING: they did not choose the same test as MPEG4. This
496
       is very important ! */
497
    if (abs(a - b) <= abs(b - c)) {
498
        pred = c;
499
        *dir_ptr = 1;
500
    } else {
501
        pred = a;
502
        *dir_ptr = 0;
503
    }
504

    
505
    /* update predictor */
506
    *dc_val_ptr = &dc_val[0];
507
    return pred;
508
}
509

    
510
#define DC_MAX 119
511

    
512
static void msmpeg4_encode_dc(MpegEncContext * s, int level, int n, int *dir_ptr)
513
{
514
    int sign, code;
515
    int pred;
516
    INT16 *dc_val;
517

    
518
    pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr);
519

    
520
    /* update predictor */
521
    if (n < 4) {
522
        *dc_val = level * s->y_dc_scale;
523
    } else {
524
        *dc_val = level * s->c_dc_scale;
525
    }
526

    
527
    /* do the prediction */
528
    level -= pred;
529

    
530
    if(s->msmpeg4_version==2){
531
        if (n < 4) {
532
            put_bits(&s->pb, 
533
                     v2_dc_lum_table[level+256][1],
534
                     v2_dc_lum_table[level+256][0]);
535
        }else{
536
            put_bits(&s->pb, 
537
                     v2_dc_chroma_table[level+256][1],
538
                     v2_dc_chroma_table[level+256][0]);
539
        }
540
    }else{
541
        sign = 0;
542
        if (level < 0) {
543
            level = -level;
544
            sign = 1;
545
        }
546
        code = level;
547
        if (code > DC_MAX) 
548
            code = DC_MAX;
549

    
550
        if (s->dc_table_index == 0) {
551
            if (n < 4) {
552
                put_bits(&s->pb, table0_dc_lum[code][1], table0_dc_lum[code][0]);
553
            } else {
554
                put_bits(&s->pb, table0_dc_chroma[code][1], table0_dc_chroma[code][0]);
555
            }
556
        } else {
557
            if (n < 4) {
558
                put_bits(&s->pb, table1_dc_lum[code][1], table1_dc_lum[code][0]);
559
            } else {
560
                put_bits(&s->pb, table1_dc_chroma[code][1], table1_dc_chroma[code][0]);
561
            }
562
        }
563
            
564
        if (code == DC_MAX)
565
            put_bits(&s->pb, 8, level);
566
            
567
        if (level != 0) {
568
            put_bits(&s->pb, 1, sign);
569
        }
570
    }
571
}
572

    
573
/* Encoding of a block. Very similar to MPEG4 except for a different
574
   escape coding (same as H263) and more vlc tables.
575
 */
576
static void msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n)
577
{
578
    int level, run, last, i, j, last_index;
579
    int last_non_zero, sign, slevel;
580
    int code, run_diff, dc_pred_dir;
581
    const RLTable *rl;
582

    
583
    if (s->mb_intra) {
584
        set_stat(ST_DC);
585
        msmpeg4_encode_dc(s, block[0], n, &dc_pred_dir);
586
        i = 1;
587
        if (n < 4) {
588
            rl = &rl_table[s->rl_table_index];
589
        } else {
590
            rl = &rl_table[3 + s->rl_chroma_table_index];
591
        }
592
        run_diff = 0;
593
        set_stat(ST_INTRA_AC);
594
    } else {
595
        i = 0;
596
        rl = &rl_table[3 + s->rl_table_index];
597
        if(s->msmpeg4_version==2)
598
            run_diff = 0;
599
        else
600
            run_diff = 1;
601
        set_stat(ST_INTER_AC);
602
    }
603

    
604
    /* AC coefs */
605
    last_index = s->block_last_index[n];
606
    last_non_zero = i - 1;
607
    for (; i <= last_index; i++) {
608
        j = zigzag_direct[i];
609
        level = block[j];
610
        if (level) {
611
            run = i - last_non_zero - 1;
612
            last = (i == last_index);
613
            sign = 0;
614
            slevel = level;
615
            if (level < 0) {
616
                sign = 1;
617
                level = -level;
618
            }
619
            code = get_rl_index(rl, last, run, level);
620
            put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
621
            if (code == rl->n) {
622
                int level1, run1;
623

    
624
                level1 = level - rl->max_level[last][run];
625
                if (level1 < 1) 
626
                    goto esc2;
627
                code = get_rl_index(rl, last, run, level1);
628
                if (code == rl->n) {
629
                esc2:
630
                    put_bits(&s->pb, 1, 0);
631
                    if (level > MAX_LEVEL)
632
                        goto esc3;
633
                    run1 = run - rl->max_run[last][level] - run_diff;
634
                    if (run1 < 0)
635
                        goto esc3;
636
                    code = get_rl_index(rl, last, run1, level);
637
                    if (code == rl->n) {
638
                    esc3:
639
                        /* third escape */
640
                        put_bits(&s->pb, 1, 0);
641
                        put_bits(&s->pb, 1, last);
642
                        put_bits(&s->pb, 6, run);
643
                        put_bits(&s->pb, 8, slevel & 0xff);
644
                    } else {
645
                        /* second escape */
646
                        put_bits(&s->pb, 1, 1);
647
                        put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
648
                        put_bits(&s->pb, 1, sign);
649
                    }
650
                } else {
651
                    /* first escape */
652
                    put_bits(&s->pb, 1, 1);
653
                    put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
654
                    put_bits(&s->pb, 1, sign);
655
                }
656
            } else {
657
                put_bits(&s->pb, 1, sign);
658
            }
659
            last_non_zero = i;
660
        }
661
    }
662
}
663

    
664
/****************************************/
665
/* decoding stuff */
666

    
667
static VLC mb_non_intra_vlc;
668
static VLC mb_intra_vlc;
669
static VLC dc_lum_vlc[2];
670
static VLC dc_chroma_vlc[2];
671
static VLC v2_dc_lum_vlc;
672
static VLC v2_dc_chroma_vlc;
673
static VLC cbpy_vlc;
674
static VLC v2_intra_cbpc_vlc;
675
static VLC v2_mb_type_vlc;
676
static VLC v2_mv_vlc;
677

    
678
/* this table is practically identical to the one from h263 except that its inverted */
679
static void init_h263_dc_for_msmpeg4()
680
{
681
    static int inited=0;
682
    
683
    if(!inited){
684
        int level, uni_code, uni_len;
685
        inited=1;
686

    
687
        for(level=-256; level<256; level++){
688
            int size, v, l;
689
            /* find number of bits */
690
            size = 0;
691
            v = abs(level);
692
            while (v) {
693
                v >>= 1;
694
                    size++;
695
            }
696

    
697
            if (level < 0)
698
                l= (-level) ^ ((1 << size) - 1);
699
            else
700
                l= level;
701

    
702
            /* luminance h263 */
703
            uni_code= DCtab_lum[size][0];
704
            uni_len = DCtab_lum[size][1];
705
            uni_code ^= (1<<uni_len)-1; //M$ doesnt like compatibility
706

    
707
            if (size > 0) {
708
                uni_code<<=size; uni_code|=l;
709
                uni_len+=size;
710
                if (size > 8){
711
                    uni_code<<=1; uni_code|=1;
712
                    uni_len++;
713
                }
714
            }
715
            v2_dc_lum_table[level+256][0]= uni_code;
716
            v2_dc_lum_table[level+256][1]= uni_len;
717

    
718
            /* chrominance h263 */
719
            uni_code= DCtab_chrom[size][0];
720
            uni_len = DCtab_chrom[size][1];
721
            uni_code ^= (1<<uni_len)-1; //M$ doesnt like compatibility
722
            
723
            if (size > 0) {
724
                uni_code<<=size; uni_code|=l;
725
                uni_len+=size;
726
                if (size > 8){
727
                    uni_code<<=1; uni_code|=1;
728
                    uni_len++;
729
                }
730
            }
731
            v2_dc_chroma_table[level+256][0]= uni_code;
732
            v2_dc_chroma_table[level+256][1]= uni_len;
733

    
734
        }
735
    }
736
}
737

    
738
/* init all vlc decoding tables */
739
int msmpeg4_decode_init_vlc(MpegEncContext *s)
740
{
741
    int i;
742
    MVTable *mv;
743

    
744
    for(i=0;i<NB_RL_TABLES;i++) {
745
        init_rl(&rl_table[i]);
746
        init_vlc_rl(&rl_table[i]);
747
    }
748
    for(i=0;i<2;i++) {
749
        mv = &mv_tables[i];
750
        init_vlc(&mv->vlc, 9, mv->n + 1, 
751
                 mv->table_mv_bits, 1, 1,
752
                 mv->table_mv_code, 2, 2);
753
    }
754

    
755
    init_vlc(&dc_lum_vlc[0], 9, 120, 
756
             &table0_dc_lum[0][1], 8, 4,
757
             &table0_dc_lum[0][0], 8, 4);
758
    init_vlc(&dc_chroma_vlc[0], 9, 120, 
759
             &table0_dc_chroma[0][1], 8, 4,
760
             &table0_dc_chroma[0][0], 8, 4);
761
    init_vlc(&dc_lum_vlc[1], 9, 120, 
762
             &table1_dc_lum[0][1], 8, 4,
763
             &table1_dc_lum[0][0], 8, 4);
764
    init_vlc(&dc_chroma_vlc[1], 9, 120, 
765
             &table1_dc_chroma[0][1], 8, 4,
766
             &table1_dc_chroma[0][0], 8, 4);
767
    
768
    init_h263_dc_for_msmpeg4();
769
    init_vlc(&v2_dc_lum_vlc, 9, 512, 
770
             &v2_dc_lum_table[0][1], 8, 4,
771
             &v2_dc_lum_table[0][0], 8, 4);
772
    init_vlc(&v2_dc_chroma_vlc, 9, 512, 
773
             &v2_dc_chroma_table[0][1], 8, 4,
774
             &v2_dc_chroma_table[0][0], 8, 4);
775
    
776
    init_vlc(&cbpy_vlc, 6, 16,
777
             &cbpy_tab[0][1], 2, 1,
778
             &cbpy_tab[0][0], 2, 1);
779
    init_vlc(&v2_intra_cbpc_vlc, 3, 4,
780
             &v2_intra_cbpc[0][1], 2, 1,
781
             &v2_intra_cbpc[0][0], 2, 1);
782
    init_vlc(&v2_mb_type_vlc, 5, 8,
783
             &v2_mb_type[0][1], 2, 1,
784
             &v2_mb_type[0][0], 2, 1);
785
    init_vlc(&v2_mv_vlc, 9, 33,
786
             &mvtab[0][1], 2, 1,
787
             &mvtab[0][0], 2, 1);
788

    
789
    init_vlc(&mb_non_intra_vlc, 9, 128, 
790
             &table_mb_non_intra[0][1], 8, 4,
791
             &table_mb_non_intra[0][0], 8, 4);
792
    init_vlc(&mb_intra_vlc, 9, 64, 
793
             &table_mb_intra[0][1], 4, 2,
794
             &table_mb_intra[0][0], 4, 2);
795
    return 0;
796
}
797

    
798
static int decode012(GetBitContext *gb)
799
{
800
    int n;
801
    n = get_bits1(gb);
802
    if (n == 0)
803
        return 0;
804
    else
805
        return get_bits1(gb) + 1;
806
}
807

    
808
int msmpeg4_decode_picture_header(MpegEncContext * s)
809
{
810
    int code, code2;
811

    
812
#if 0
813
{
814
int i;
815
for(i=0; i<s->gb.size*8; i++)
816
    printf("%d", get_bits1(&s->gb));
817
//    get_bits1(&s->gb);
818
printf("END\n");
819
return -1;
820
}
821
#endif
822
    s->pict_type = get_bits(&s->gb, 2) + 1;
823
    if (s->pict_type != I_TYPE &&
824
        s->pict_type != P_TYPE)
825
        return -1;
826

    
827
    s->qscale = get_bits(&s->gb, 5);
828

    
829
    if (s->pict_type == I_TYPE) {
830
        code = get_bits(&s->gb, 5); 
831
        /* 0x17: one slice, 0x18: two slices */
832
        if (code < 0x17)
833
            return -1;
834
        s->slice_height = s->mb_height / (code - 0x16);
835

    
836
        switch(s->msmpeg4_version){
837
        case 2:
838
            s->rl_chroma_table_index = 2;
839
            s->rl_table_index = 2;
840

    
841
            s->dc_table_index = 0; //not used
842
            break;
843
        case 3:
844
            s->rl_chroma_table_index = decode012(&s->gb);
845
            s->rl_table_index = decode012(&s->gb);
846

    
847
            s->dc_table_index = get_bits1(&s->gb);
848
            break;
849
        case 4:
850
            msmpeg4_decode_ext_header(s, 999 /* bufer size (useless here) */);
851
            printf("%X\n", show_bits(&s->gb, 24));
852
            code= get_bits(&s->gb, 2);
853
            if(code==1){
854
                code2= get_bits(&s->gb, 3);
855
                if(code2==7) skip_bits(&s->gb, 1);
856
            }
857
            printf("%X\n", show_bits(&s->gb, 24));
858
            s->rl_chroma_table_index = 2;
859
            s->rl_table_index = 2;
860

    
861
            s->dc_table_index = 0;
862
            break;
863
        }
864
        s->no_rounding = 1;
865
/*        printf(" %d %d %d %d     \n", 
866
                s->qscale,
867
                s->rl_chroma_table_index,
868
                s->rl_table_index, 
869
                s->dc_table_index);*/
870
    } else {
871
        s->use_skip_mb_code = get_bits1(&s->gb);
872
        
873
        if(s->msmpeg4_version==2){
874
            s->rl_table_index = 2;
875
            s->rl_chroma_table_index = s->rl_table_index;
876

    
877
            s->dc_table_index = 0; //not used
878

    
879
            s->mv_table_index = 0;
880
        }else{
881
            s->rl_table_index = decode012(&s->gb);
882
            s->rl_chroma_table_index = s->rl_table_index;
883

    
884
            s->dc_table_index = get_bits1(&s->gb);
885

    
886
            s->mv_table_index = get_bits1(&s->gb);
887
        }
888
/*        printf(" %d %d %d %d %d     \n", 
889
                s->use_skip_mb_code, 
890
                s->rl_table_index, 
891
                s->rl_chroma_table_index, 
892
                s->dc_table_index,
893
                s->mv_table_index);*/
894
        if(s->flipflop_rounding){
895
            s->no_rounding ^= 1;
896
        }else{
897
            s->no_rounding = 0;
898
        }
899
//        printf("%d", s->no_rounding);
900
//return -1;
901
    }
902
    
903
#if 0
904
if(s->msmpeg4_version==2)
905
{
906
int i;
907
for(i=0; i<s->gb.size*8; i++)
908
//    printf("%d", get_bits1(&s->gb));
909
    get_bits1(&s->gb);
910
printf("END\n");
911
return -1;
912
}
913
#endif
914

    
915
#ifdef DEBUG
916
    printf("*****frame %d:\n", frame_count++);
917
#endif
918
    return 0;
919
}
920

    
921
int msmpeg4_decode_ext_header(MpegEncContext * s, int buf_size)
922
{
923
    /* the alt_bitstream reader could read over the end so we need to check it */
924
    if(get_bits_count(&s->gb) + 16 < buf_size*8)
925
    {
926
        int fps;
927

    
928
        fps= get_bits(&s->gb, 5);
929
        s->bitrate= get_bits(&s->gb, 11);
930
        s->flipflop_rounding= get_bits1(&s->gb);
931

    
932
//        printf("fps:%2d bps:%2d roundingType:%1d\n", fps, s->bitrate, s->flipflop_rounding);
933
    }
934
    else
935
    {
936
        s->flipflop_rounding= 0;
937
        s->bitrate= 0;
938
    }
939

    
940
    return 0;
941
}
942

    
943
static inline void memsetw(short *tab, int val, int n)
944
{
945
    int i;
946
    for(i=0;i<n;i++)
947
        tab[i] = val;
948
}
949

    
950
static void msmpeg4v2_encode_motion(MpegEncContext * s, int val)
951
{
952
    int range, bit_size, sign, code, bits;
953

    
954
    if (val == 0) {
955
        /* zero vector */
956
        code = 0;
957
        put_bits(&s->pb, mvtab[code][1], mvtab[code][0]);
958
    } else {
959
        bit_size = s->f_code - 1;
960
        range = 1 << bit_size;
961
        if (val <= -64)
962
            val += 64;
963
        else if (val >= 64)
964
            val -= 64;
965

    
966
        if (val >= 0) {
967
            sign = 0;
968
        } else {
969
            val = -val;
970
            sign = 1;
971
        }
972
        val--;
973
        code = (val >> bit_size) + 1;
974
        bits = val & (range - 1);
975

    
976
        put_bits(&s->pb, mvtab[code][1] + 1, (mvtab[code][0] << 1) | sign); 
977
        if (bit_size > 0) {
978
            put_bits(&s->pb, bit_size, bits);
979
        }
980
    }
981
}
982

    
983
/* this is identical to h263 except that its range is multiplied by 2 */
984
static int msmpeg4v2_decode_motion(MpegEncContext * s, int pred, int f_code)
985
{
986
    int code, val, sign, shift;
987

    
988
    code = get_vlc(&s->gb, &v2_mv_vlc);
989
    if (code < 0)
990
        return 0xffff;
991

    
992
    if (code == 0)
993
        return pred;
994
    sign = get_bits1(&s->gb);
995
    shift = f_code - 1;
996
    val = (code - 1) << shift;
997
    if (shift > 0)
998
        val |= get_bits(&s->gb, shift);
999
    val++;
1000
    if (sign)
1001
        val = -val;
1002
    val += pred;
1003

    
1004
    if (val <= -64)
1005
        val += 64;
1006
    else if (val >= 64)
1007
        val -= 64;
1008

    
1009
    return val;
1010
}
1011

    
1012

    
1013
int msmpeg4v2_decode_mb(MpegEncContext *s, 
1014
                      DCTELEM block[6][64])
1015
{
1016
    int cbp, code, i;
1017
    if (s->pict_type == P_TYPE) {
1018
        if (s->use_skip_mb_code) {
1019
            if (get_bits1(&s->gb)) {
1020
                /* skip mb */
1021
                s->mb_intra = 0;
1022
                for(i=0;i<6;i++)
1023
                    s->block_last_index[i] = -1;
1024
                s->mv_dir = MV_DIR_FORWARD;
1025
                s->mv_type = MV_TYPE_16X16;
1026
                s->mv[0][0][0] = 0;
1027
                s->mv[0][0][1] = 0;
1028
                s->mb_skiped = 1;
1029
                return 0;
1030
            }
1031
        }
1032

    
1033
        code = get_vlc(&s->gb, &v2_mb_type_vlc);
1034
        s->mb_intra = code >>2;
1035
    
1036
        cbp = code & 0x3;
1037
    } else {
1038
        s->mb_intra = 1;
1039
        cbp= get_vlc(&s->gb, &v2_intra_cbpc_vlc);
1040
    }
1041

    
1042
    if (!s->mb_intra) {
1043
        int mx, my;
1044

    
1045
        cbp|= get_vlc(&s->gb, &cbpy_vlc)<<2;
1046
        if((cbp&3) != 3) cbp^= 0x3C;
1047
        
1048
        h263_pred_motion(s, 0, &mx, &my);
1049
        mx= msmpeg4v2_decode_motion(s, mx, 1);
1050
        my= msmpeg4v2_decode_motion(s, my, 1);
1051
        
1052
        s->mv_dir = MV_DIR_FORWARD;
1053
        s->mv_type = MV_TYPE_16X16;
1054
        s->mv[0][0][0] = mx;
1055
        s->mv[0][0][1] = my;
1056
    } else {
1057
        s->ac_pred = get_bits1(&s->gb);
1058
        cbp|= get_vlc(&s->gb, &cbpy_vlc)<<2;
1059
    }
1060

    
1061
    for (i = 0; i < 6; i++) {
1062
        if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1) < 0)
1063
        {
1064
             fprintf(stderr,"\nIgnoring error while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
1065
             return -1;
1066
        }
1067
    }
1068
    return 0;
1069
}
1070

    
1071
int msmpeg4_decode_mb(MpegEncContext *s, 
1072
                      DCTELEM block[6][64])
1073
{
1074
    int cbp, code, i;
1075
    UINT8 *coded_val;
1076

    
1077
    /* special slice handling */
1078
    if (s->mb_x == 0) {
1079
        if (s->slice_height && (s->mb_y % s->slice_height) == 0) {
1080
            int wrap;
1081
            /* reset DC pred (set previous line to 1024) */
1082
            wrap = 2 * s->mb_width + 2;
1083
            memsetw(&s->dc_val[0][(1) + (2 * s->mb_y) * wrap], 
1084
                    1024, 2 * s->mb_width);
1085
            wrap = s->mb_width + 2;
1086
            memsetw(&s->dc_val[1][(1) + (s->mb_y) * wrap], 
1087
                    1024, s->mb_width);
1088
            memsetw(&s->dc_val[2][(1) + (s->mb_y) * wrap], 
1089
                    1024, s->mb_width);
1090
            
1091
            /* reset AC pred (set previous line to 0) */
1092
            wrap = s->mb_width * 2 + 2;
1093
            memsetw(s->ac_val[0][0] + (1 + (2 * s->mb_y) * wrap)*16,
1094
                    0, 2 * s->mb_width*16);
1095
            wrap = s->mb_width + 2;
1096
            memsetw(s->ac_val[1][0] + (1 + (s->mb_y) * wrap)*16,
1097
                    0, s->mb_width*16);
1098
            memsetw(s->ac_val[2][0] + (1 + (s->mb_y) * wrap)*16,
1099
                    0, s->mb_width*16);
1100

    
1101
            s->first_slice_line = 1;
1102
        } else {
1103
            s->first_slice_line = 0; 
1104
        }
1105
    }
1106

    
1107
    if(s->msmpeg4_version==2) return msmpeg4v2_decode_mb(s, block); //FIXME merge if possible
1108
    
1109
    if (s->pict_type == P_TYPE) {
1110
        set_stat(ST_INTER_MB);
1111
        if (s->use_skip_mb_code) {
1112
            if (get_bits1(&s->gb)) {
1113
                /* skip mb */
1114
                s->mb_intra = 0;
1115
                for(i=0;i<6;i++)
1116
                    s->block_last_index[i] = -1;
1117
                s->mv_dir = MV_DIR_FORWARD;
1118
                s->mv_type = MV_TYPE_16X16;
1119
                s->mv[0][0][0] = 0;
1120
                s->mv[0][0][1] = 0;
1121
                s->mb_skiped = 1;
1122
                return 0;
1123
            }
1124
        }
1125
        
1126
        code = get_vlc(&s->gb, &mb_non_intra_vlc);
1127
        if (code < 0)
1128
            return -1;
1129
        //s->mb_intra = (code & 0x40) ? 0 : 1;
1130
        s->mb_intra = (~code & 0x40) >> 6;
1131
            
1132
        cbp = code & 0x3f;
1133
    } else {
1134
        set_stat(ST_INTRA_MB);
1135
        s->mb_intra = 1;
1136
        code = get_vlc(&s->gb, &mb_intra_vlc);
1137
        if (code < 0)
1138
            return -1;
1139
        /* predict coded block pattern */
1140
        cbp = 0;
1141
        for(i=0;i<6;i++) {
1142
            int val = ((code >> (5 - i)) & 1);
1143
            if (i < 4) {
1144
                int pred = coded_block_pred(s, i, &coded_val);
1145
                val = val ^ pred;
1146
                *coded_val = val;
1147
            }
1148
            cbp |= val << (5 - i);
1149
        }
1150
    }
1151

    
1152
    if (!s->mb_intra) {
1153
        int mx, my;
1154
        set_stat(ST_MV);
1155
        h263_pred_motion(s, 0, &mx, &my);
1156
        if (msmpeg4_decode_motion(s, &mx, &my) < 0)
1157
            return -1;
1158
        s->mv_dir = MV_DIR_FORWARD;
1159
        s->mv_type = MV_TYPE_16X16;
1160
        s->mv[0][0][0] = mx;
1161
        s->mv[0][0][1] = my;
1162
    } else {
1163
        set_stat(ST_INTRA_MB);
1164
        s->ac_pred = get_bits1(&s->gb);
1165
    }
1166

    
1167
    for (i = 0; i < 6; i++) {
1168
        if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1) < 0)
1169
        {
1170
            fprintf(stderr,"\nIgnoring error while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
1171
            // return -1;
1172
        }
1173
    }
1174
    return 0;
1175
}
1176

    
1177
static int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
1178
                              int n, int coded)
1179
{
1180
    int code, level, i, j, last, run, run_diff;
1181
    int dc_pred_dir;
1182
    RLTable *rl;
1183
    const UINT8 *scan_table;
1184
    int qmul, qadd;
1185

    
1186
    if (s->mb_intra) {
1187
        qmul=1;
1188
        qadd=0;
1189

    
1190
        /* DC coef */
1191
        set_stat(ST_DC);
1192
        level = msmpeg4_decode_dc(s, n, &dc_pred_dir);
1193
        if (level < 0)
1194
            return -1;
1195
        block[0] = level;
1196
        if (n < 4) {
1197
            rl = &rl_table[s->rl_table_index];
1198
        } else {
1199
            rl = &rl_table[3 + s->rl_chroma_table_index];
1200
        }
1201

    
1202
        run_diff = 0;
1203
        i = 1;
1204
        if (!coded) {
1205
            goto not_coded;
1206
        }
1207
        if (s->ac_pred) {
1208
            if (dc_pred_dir == 0) 
1209
                scan_table = ff_alternate_vertical_scan; /* left */
1210
            else
1211
                scan_table = ff_alternate_horizontal_scan; /* top */
1212
        } else {
1213
            scan_table = zigzag_direct;
1214
        }
1215
        set_stat(ST_INTRA_AC);
1216
    } else {
1217
        qmul = s->qscale << 1;
1218
        qadd = (s->qscale - 1) | 1;
1219
        i = 0;
1220
        rl = &rl_table[3 + s->rl_table_index];
1221

    
1222
        if(s->msmpeg4_version==2)
1223
            run_diff = 0;
1224
        else
1225
            run_diff = 1;
1226

    
1227
        if (!coded) {
1228
            s->block_last_index[n] = i - 1;
1229
            return 0;
1230
        }
1231
        scan_table = zigzag_direct;
1232
        set_stat(ST_INTER_AC);
1233
    }
1234

    
1235
    for(;;) {
1236
        code = get_vlc(&s->gb, &rl->vlc);
1237
        if (code < 0)
1238
            return -1;
1239
        if (code == rl->n) {
1240
            /* escape */
1241
            if (get_bits1(&s->gb) == 0) {
1242
                if (get_bits1(&s->gb) == 0) {
1243
                    /* third escape */
1244
                    last = get_bits1(&s->gb);
1245
                    run = get_bits(&s->gb, 6);
1246
                    level = get_bits(&s->gb, 8);
1247
                    level = (level << 24) >> 24; /* sign extend */
1248
                    //level = level * qmul + (level>0) * qadd - (level<=0) * qadd ;
1249
                    if (level>0) level= level * qmul + qadd;
1250
                    else        level= level * qmul - qadd;
1251
                } else {
1252
                    /* second escape */
1253
                    code = get_vlc(&s->gb, &rl->vlc);
1254
                    if (code < 0 || code >= rl->n)
1255
                        return -1;
1256
                    run = rl->table_run[code];
1257
                    level = rl->table_level[code];
1258
                    last = code >= rl->last;
1259
                    run += rl->max_run[last][level] + run_diff;
1260
                    level= level * qmul + qadd;
1261
                    if (get_bits1(&s->gb))
1262
                        level = -level;
1263
                }
1264
            } else {
1265
                /* first escape */
1266
                code = get_vlc(&s->gb, &rl->vlc);
1267
                if (code < 0 || code >= rl->n)
1268
                    return -1;
1269
                run = rl->table_run[code];
1270
                level = rl->table_level[code];
1271
                last = code >= rl->last;
1272
                level += rl->max_level[last][run];
1273
                level= level * qmul + qadd;
1274
                if (get_bits1(&s->gb))
1275
                    level = -level;
1276
            }
1277
        } else {
1278
            run = rl->table_run[code];
1279
            level = rl->table_level[code] * qmul + qadd;
1280
            last = code >= rl->last;
1281
            if (get_bits1(&s->gb))
1282
                level = -level;
1283
        }
1284
        i += run;
1285
        if (i >= 64)
1286
            return -1;
1287
//printf("RL:%d %d %d ", run, level, last);
1288
        j = scan_table[i];
1289
        block[j] = level;
1290
        i++;
1291
        if (last)
1292
            break;
1293
    }
1294
 not_coded:
1295
    if (s->mb_intra) {
1296
        mpeg4_pred_ac(s, block, n, dc_pred_dir);
1297
        if (s->ac_pred) {
1298
            i = 64; /* XXX: not optimal */
1299
        }
1300
    }
1301
    s->block_last_index[n] = i - 1;
1302

    
1303
    return 0;
1304
}
1305

    
1306
static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr)
1307
{
1308
    int level, pred;
1309
    INT16 *dc_val;
1310

    
1311
    if(s->msmpeg4_version==2){
1312
        if (n < 4) {
1313
            level = get_vlc(&s->gb, &v2_dc_lum_vlc);
1314
        } else {
1315
            level = get_vlc(&s->gb, &v2_dc_chroma_vlc);
1316
        }
1317
        if (level < 0) 
1318
            return -1;
1319
        level-=256;
1320
    }else{  //FIXME optimize use unified tables & index
1321
        if (n < 4) {
1322
            level = get_vlc(&s->gb, &dc_lum_vlc[s->dc_table_index]);
1323
        } else {
1324
            level = get_vlc(&s->gb, &dc_chroma_vlc[s->dc_table_index]);
1325
        }
1326
        if (level < 0)
1327
            return -1;
1328

    
1329
        if (level == DC_MAX) {
1330
            level = get_bits(&s->gb, 8);
1331
            if (get_bits1(&s->gb))
1332
                level = -level;
1333
        } else if (level != 0) {
1334
            if (get_bits1(&s->gb))
1335
                level = -level;
1336
        }
1337
    }
1338

    
1339
    pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr);
1340
    level += pred;
1341

    
1342
    /* update predictor */
1343
    if (n < 4) {
1344
        *dc_val = level * s->y_dc_scale;
1345
    } else {
1346
        *dc_val = level * s->c_dc_scale;
1347
    }
1348

    
1349
    return level;
1350
}
1351

    
1352
static int msmpeg4_decode_motion(MpegEncContext * s, 
1353
                                 int *mx_ptr, int *my_ptr)
1354
{
1355
    MVTable *mv;
1356
    int code, mx, my;
1357

    
1358
    mv = &mv_tables[s->mv_table_index];
1359

    
1360
    code = get_vlc(&s->gb, &mv->vlc);
1361
    if (code < 0)
1362
        return -1;
1363
    if (code == mv->n) {
1364
        mx = get_bits(&s->gb, 6);
1365
        my = get_bits(&s->gb, 6);
1366
    } else {
1367
        mx = mv->table_mvx[code];
1368
        my = mv->table_mvy[code];
1369
    }
1370

    
1371
    mx += *mx_ptr - 32;
1372
    my += *my_ptr - 32;
1373
    /* WARNING : they do not do exactly modulo encoding */
1374
    if (mx <= -64)
1375
        mx += 64;
1376
    else if (mx >= 64)
1377
        mx -= 64;
1378

    
1379
    if (my <= -64)
1380
        my += 64;
1381
    else if (my >= 64)
1382
        my -= 64;
1383
    *mx_ptr = mx;
1384
    *my_ptr = my;
1385
    return 0;
1386
}