Statistics
| Branch: | Revision:

ffmpeg / libavcodec / msmpeg4.c @ 1e98dffb

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

    
25
/*
26
 * You can also call this codec : MPEG4 with a twist ! 
27
 *
28
 * TODO: 
29
 *        - (encoding) select best mv table (two choices)
30
 *        - (encoding) select best vlc/dc table 
31
 *        - (decoding) handle slice indication
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 void msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n);
47
static int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
48
                                int n, int coded);
49
static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr);
50
static int msmpeg4_decode_motion(MpegEncContext * s, 
51
                                 int *mx_ptr, int *my_ptr);
52

    
53
#ifdef DEBUG
54
int intra_count = 0;
55
int frame_count = 0;
56
#endif
57
/* XXX: move it to mpegvideo.h */
58

    
59
static int init_done = 0;
60

    
61
#include "msmpeg4data.h"
62

    
63
#ifdef STATS
64

    
65
const char *st_names[ST_NB] = {
66
    "unknown",
67
    "dc",
68
    "intra_ac",
69
    "inter_ac",
70
    "intra_mb",
71
    "inter_mb",
72
    "mv",
73
};
74

    
75
int st_current_index = 0;
76
unsigned int st_bit_counts[ST_NB];
77
unsigned int st_out_bit_counts[ST_NB];
78

    
79
#define set_stat(var) st_current_index = var;
80

    
81
void print_stats(void)
82
{
83
    unsigned int total;
84
    int i;
85

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

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

    
121
#else
122

    
123
#define set_stat(var)
124

    
125
#endif
126

    
127
/* build the table which associate a (x,y) motion vector to a vlc */
128
static void init_mv_table(MVTable *tab)
129
{
130
    int i, x, y;
131

    
132
    tab->table_mv_index = malloc(sizeof(UINT16) * 4096);
133
    /* mark all entries as not used */
134
    for(i=0;i<4096;i++)
135
        tab->table_mv_index[i] = tab->n;
136
    
137
    for(i=0;i<tab->n;i++) {
138
        x = tab->table_mvx[i];
139
        y = tab->table_mvy[i];
140
        tab->table_mv_index[(x << 6) | y] = i;
141
    }
142
}
143

    
144
static void code012(PutBitContext *pb, int n)
145
{
146
    if (n == 0) {
147
        put_bits(pb, 1, 0);
148
    } else {
149
        put_bits(pb, 1, 1);
150
        put_bits(pb, 1, (n >= 2));
151
    }
152
}
153

    
154
/* write MSMPEG4 V3 compatible frame header */
155
void msmpeg4_encode_picture_header(MpegEncContext * s, int picture_number)
156
{
157
    int i;
158

    
159
    align_put_bits(&s->pb);
160

    
161
    put_bits(&s->pb, 2, s->pict_type - 1);
162

    
163
    put_bits(&s->pb, 5, s->qscale);
164

    
165
    s->rl_table_index = 2;
166
    s->rl_chroma_table_index = 1; /* only for I frame */
167
    s->dc_table_index = 1;
168
    s->mv_table_index = 1; /* only if P frame */
169
    s->use_skip_mb_code = 1; /* only if P frame */
170
    
171
    if (s->pict_type == I_TYPE) {
172
        put_bits(&s->pb, 5, 0x17); /* indicate only one "slice" */
173

    
174
        code012(&s->pb, s->rl_chroma_table_index);
175
        code012(&s->pb, s->rl_table_index);
176

    
177
        put_bits(&s->pb, 1, s->dc_table_index);
178
        s->no_rounding = 1;
179
    } else {
180
        put_bits(&s->pb, 1, s->use_skip_mb_code);
181
        
182
        s->rl_chroma_table_index = s->rl_table_index;
183
        code012(&s->pb, s->rl_table_index);
184

    
185
        put_bits(&s->pb, 1, s->dc_table_index);
186

    
187
        put_bits(&s->pb, 1, s->mv_table_index);
188
        
189
        if(s->flipflop_rounding){
190
            s->no_rounding ^= 1;
191
        }else{
192
            s->no_rounding = 0;
193
        }
194
    }
195

    
196
    if (!init_done) {
197
        /* init various encoding tables */
198
        init_done = 1;
199
        init_mv_table(&mv_tables[0]);
200
        init_mv_table(&mv_tables[1]);
201
        for(i=0;i<NB_RL_TABLES;i++)
202
            init_rl(&rl_table[i]);
203
    }
204

    
205
#ifdef DEBUG
206
    intra_count = 0;
207
    printf("*****frame %d:\n", frame_count++);
208
#endif
209
}
210

    
211
void msmpeg4_encode_ext_header(MpegEncContext * s)
212
{
213
    if(s->pict_type == P_TYPE)
214
    {
215
        return; // P-Frames dont seem to have them and not even a 0 bit
216
    }
217
    else
218
    {
219
        s->flipflop_rounding=1;
220
        s->bitrate= 910;
221

    
222
        put_bits(&s->pb, 1, 1); // ext header indicator
223

    
224
        put_bits(&s->pb, 4, 7); // ?
225

    
226
        put_bits(&s->pb, 11, s->bitrate);
227

    
228
        put_bits(&s->pb, 1, s->flipflop_rounding);
229
    }
230
}
231

    
232
/* predict coded block */
233
static inline int coded_block_pred(MpegEncContext * s, int n, UINT8 **coded_block_ptr)
234
{
235
    int x, y, wrap, pred, a, b, c;
236

    
237
    x = 2 * s->mb_x + 1 + (n & 1);
238
    y = 2 * s->mb_y + 1 + ((n & 2) >> 1);
239
    wrap = s->mb_width * 2 + 2;
240

    
241
    /* B C
242
     * A X 
243
     */
244
    a = s->coded_block[(x - 1) + (y) * wrap];
245
    b = s->coded_block[(x - 1) + (y - 1) * wrap];
246
    c = s->coded_block[(x) + (y - 1) * wrap];
247
    
248
    if (b == c) {
249
        pred = a;
250
    } else {
251
        pred = c;
252
    }
253
    
254
    /* store value */
255
    *coded_block_ptr = &s->coded_block[(x) + (y) * wrap];
256

    
257
    return pred;
258
}
259

    
260
static void msmpeg4_encode_motion(MpegEncContext * s, 
261
                                  int mx, int my)
262
{
263
    int code;
264
    MVTable *mv;
265

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

    
287
    code = mv->table_mv_index[(mx << 6) | my];
288
    set_stat(ST_MV);
289
    put_bits(&s->pb, 
290
             mv->table_mv_bits[code], 
291
             mv->table_mv_code[code]);
292
    if (code == mv->n) {
293
        /* escape : code litterally */
294
        put_bits(&s->pb, 6, mx);
295
        put_bits(&s->pb, 6, my);
296
    }
297
}
298

    
299
void msmpeg4_encode_mb(MpegEncContext * s, 
300
                       DCTELEM block[6][64],
301
                       int motion_x, int motion_y)
302
{
303
    int cbp, coded_cbp, i;
304
    int pred_x, pred_y;
305
    UINT8 *coded_block;
306

    
307
    if (!s->mb_intra) {
308
        /* compute cbp */
309
        set_stat(ST_INTER_MB);
310
        cbp = 0;
311
        for (i = 0; i < 6; i++) {
312
            if (s->block_last_index[i] >= 0)
313
                cbp |= 1 << (5 - i);
314
        }
315
        if (s->use_skip_mb_code && (cbp | motion_x | motion_y) == 0) {
316
            /* skip macroblock */
317
            put_bits(&s->pb, 1, 1);
318
            return;
319
        }
320
        if (s->use_skip_mb_code)
321
            put_bits(&s->pb, 1, 0);        /* mb coded */
322
        
323
        put_bits(&s->pb, 
324
                 table_mb_non_intra[cbp + 64][1], 
325
                 table_mb_non_intra[cbp + 64][0]);
326

    
327
        /* motion vector */
328
        h263_pred_motion(s, 0, &pred_x, &pred_y);
329
        msmpeg4_encode_motion(s, motion_x - pred_x, 
330
                              motion_y - pred_y);
331
    } else {
332
        /* compute cbp */
333
        cbp = 0;
334
        coded_cbp = 0;
335
        for (i = 0; i < 6; i++) {
336
            int val, pred;
337
            val = (s->block_last_index[i] >= 1);
338
            cbp |= val << (5 - i);
339
            if (i < 4) {
340
                /* predict value for close blocks only for luma */
341
                pred = coded_block_pred(s, i, &coded_block);
342
                *coded_block = val;
343
                val = val ^ pred;
344
            }
345
            coded_cbp |= val << (5 - i);
346
        }
347
#if 0
348
        if (coded_cbp)
349
            printf("cbp=%x %x\n", cbp, coded_cbp);
350
#endif
351

    
352
        if (s->pict_type == I_TYPE) {
353
            set_stat(ST_INTRA_MB);
354
            put_bits(&s->pb, 
355
                     table_mb_intra[coded_cbp][1], table_mb_intra[coded_cbp][0]);
356
        } else {
357
            if (s->use_skip_mb_code)
358
                put_bits(&s->pb, 1, 0);        /* mb coded */
359
            put_bits(&s->pb, 
360
                     table_mb_non_intra[cbp][1], 
361
                     table_mb_non_intra[cbp][0]);
362
        }
363
        set_stat(ST_INTRA_MB);
364
        put_bits(&s->pb, 1, 0);        /* no AC prediction yet */
365
    }
366

    
367
    for (i = 0; i < 6; i++) {
368
        msmpeg4_encode_block(s, block[i], i);
369
    }
370
}
371

    
372

    
373
/* strongly inspirated from MPEG4, but not exactly the same ! */
374
void msmpeg4_dc_scale(MpegEncContext * s)
375
{
376
    if (s->qscale < 5){
377
        s->y_dc_scale = 8;
378
        s->c_dc_scale = 8;
379
//        s->c_dc_scale = (s->qscale + 13)>>1;
380
    }else if (s->qscale < 9){
381
        s->y_dc_scale = 2 * s->qscale;
382
        s->c_dc_scale = (s->qscale + 13)>>1;
383
    }else{
384
        s->y_dc_scale = s->qscale + 8;
385
        s->c_dc_scale = (s->qscale + 13)>>1;
386
    }
387
    // this differs for quant >24 from mpeg4 
388
    
389
//    if(s->qscale==13) s->c_dc_scale=14;
390
    
391
//    if(s->qscale>=6)
392
//     printf("%d", s->qscale);
393
    
394
    /* s->c_dc_scale values (found by Michael Nidermayer)
395
     qscale=2 -> 8 (yes iam sure about that)
396
     qscale=3 -> 8
397
     qscale=4 -> 8
398
     qscale=5 -> 9
399
     qscale=6 -> 9 
400
     qscale=7 -> 10
401
     qscale=8 -> 10
402
     qscale=9 -> 11
403
     qscale=10-> 11
404
    */
405
}
406

    
407
/* dir = 0: left, dir = 1: top prediction */
408
static int msmpeg4_pred_dc(MpegEncContext * s, int n, 
409
                           INT16 **dc_val_ptr, int *dir_ptr)
410
{
411
    int a, b, c, x, y, wrap, pred, scale;
412
    INT16 *dc_val;
413

    
414
    /* find prediction */
415
    if (n < 4) {
416
        x = 2 * s->mb_x + 1 + (n & 1);
417
        y = 2 * s->mb_y + 1 + ((n & 2) >> 1);
418
        wrap = s->mb_width * 2 + 2;
419
        dc_val = s->dc_val[0];
420
        scale = s->y_dc_scale;
421
    } else {
422
        x = s->mb_x + 1;
423
        y = s->mb_y + 1;
424
        wrap = s->mb_width + 2;
425
        dc_val = s->dc_val[n - 4 + 1];
426
        scale = s->c_dc_scale;
427
    }
428

    
429
    /* B C
430
     * A X 
431
     */
432
    a = dc_val[(x - 1) + (y) * wrap];
433
    b = dc_val[(x - 1) + (y - 1) * wrap];
434
    c = dc_val[(x) + (y - 1) * wrap];
435

    
436
    /* XXX: the following solution consumes divisions, but it does not
437
       necessitate to modify mpegvideo.c. The problem comes from the
438
       fact they decided to store the quantized DC (which would lead
439
       to problems if Q could vary !) */
440
#ifdef ARCH_X86
441
    /* using 16bit divisions as they are large enough and 2x as fast */
442
    asm volatile(
443
        "movl %3, %%eax                \n\t"
444
        "shrl $1, %%eax                \n\t"
445
        "addl %%eax, %2                \n\t"
446
        "addl %%eax, %1                \n\t"
447
        "addl %0, %%eax                \n\t"
448
        "xorl %%edx, %%edx        \n\t"
449
        "divw %w3                \n\t"
450
        "movzwl %%ax, %0        \n\t"
451
        "movl %1, %%eax                \n\t"
452
        "xorl %%edx, %%edx        \n\t"
453
        "divw %w3                \n\t"
454
        "movzwl %%ax, %1        \n\t"
455
        "movl %2, %%eax                \n\t"
456
        "xorl %%edx, %%edx        \n\t"
457
        "divw %w3                \n\t"
458
        "movzwl %%ax, %2        \n\t"
459
        : "+r" (a), "+r" (b), "+r" (c)
460
        : "r" (scale)
461
        : "%eax", "%edx"
462
    );
463
#elif defined (ARCH_ALPHA)
464
    /* Divisions are extremely costly on Alpha; optimize the most
465
       common case.  */
466
    if (scale == 8) {
467
        a = (a + (8 >> 1)) / 8;
468
        b = (b + (8 >> 1)) / 8;
469
        c = (c + (8 >> 1)) / 8;
470
    } else {
471
        a = (a + (scale >> 1)) / scale;
472
        b = (b + (scale >> 1)) / scale;
473
        c = (c + (scale >> 1)) / scale;
474
    }
475
#else
476
    a = (a + (scale >> 1)) / scale;
477
    b = (b + (scale >> 1)) / scale;
478
    c = (c + (scale >> 1)) / scale;
479
#endif
480
    /* XXX: WARNING: they did not choose the same test as MPEG4. This
481
       is very important ! */
482
    if (abs(a - b) <= abs(b - c)) {
483
        pred = c;
484
        *dir_ptr = 1;
485
    } else {
486
        pred = a;
487
        *dir_ptr = 0;
488
    }
489

    
490
    /* update predictor */
491
    *dc_val_ptr = &dc_val[(x) + (y) * wrap];
492
    return pred;
493
}
494

    
495
#define DC_MAX 119
496

    
497
static void msmpeg4_encode_dc(MpegEncContext * s, int level, int n, int *dir_ptr)
498
{
499
    int sign, code;
500
    int pred;
501
    INT16 *dc_val;
502

    
503
    pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr);
504

    
505
    /* update predictor */
506
    if (n < 4) {
507
        *dc_val = level * s->y_dc_scale;
508
    } else {
509
        *dc_val = level * s->c_dc_scale;
510
    }
511

    
512
    /* do the prediction */
513
    level -= pred;
514

    
515
    sign = 0;
516
    if (level < 0) {
517
        level = -level;
518
        sign = 1;
519
    }
520
    
521
    code = level;
522
    if (code > DC_MAX) 
523
        code = DC_MAX;
524

    
525
    if (s->dc_table_index == 0) {
526
        if (n < 4) {
527
            put_bits(&s->pb, table0_dc_lum[code][1], table0_dc_lum[code][0]);
528
        } else {
529
            put_bits(&s->pb, table0_dc_chroma[code][1], table0_dc_chroma[code][0]);
530
        }
531
    } else {
532
        if (n < 4) {
533
            put_bits(&s->pb, table1_dc_lum[code][1], table1_dc_lum[code][0]);
534
        } else {
535
            put_bits(&s->pb, table1_dc_chroma[code][1], table1_dc_chroma[code][0]);
536
        }
537
    }
538
        
539
    if (code == DC_MAX)
540
        put_bits(&s->pb, 8, level);
541
        
542
    if (level != 0) {
543
        put_bits(&s->pb, 1, sign);
544
    }
545
}
546

    
547
/* Encoding of a block. Very similar to MPEG4 except for a different
548
   escape coding (same as H263) and more vlc tables.
549
 */
550
static void msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n)
551
{
552
    int level, run, last, i, j, last_index;
553
    int last_non_zero, sign, slevel;
554
    int code, run_diff, dc_pred_dir;
555
    const RLTable *rl;
556

    
557
    if (s->mb_intra) {
558
        set_stat(ST_DC);
559
        msmpeg4_encode_dc(s, block[0], n, &dc_pred_dir);
560
        i = 1;
561
        if (n < 4) {
562
            rl = &rl_table[s->rl_table_index];
563
        } else {
564
            rl = &rl_table[3 + s->rl_chroma_table_index];
565
        }
566
        run_diff = 0;
567
        set_stat(ST_INTRA_AC);
568
    } else {
569
        i = 0;
570
        rl = &rl_table[3 + s->rl_table_index];
571
        run_diff = 1;
572
        set_stat(ST_INTER_AC);
573
    }
574

    
575
    /* AC coefs */
576
    last_index = s->block_last_index[n];
577
    last_non_zero = i - 1;
578
    for (; i <= last_index; i++) {
579
        j = zigzag_direct[i];
580
        level = block[j];
581
        if (level) {
582
            run = i - last_non_zero - 1;
583
            last = (i == last_index);
584
            sign = 0;
585
            slevel = level;
586
            if (level < 0) {
587
                sign = 1;
588
                level = -level;
589
            }
590
            code = get_rl_index(rl, last, run, level);
591
            put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
592
            if (code == rl->n) {
593
                int level1, run1;
594

    
595
                level1 = level - rl->max_level[last][run];
596
                if (level1 < 1) 
597
                    goto esc2;
598
                code = get_rl_index(rl, last, run, level1);
599
                if (code == rl->n) {
600
                esc2:
601
                    put_bits(&s->pb, 1, 0);
602
                    if (level > MAX_LEVEL)
603
                        goto esc3;
604
                    run1 = run - rl->max_run[last][level] - run_diff;
605
                    if (run1 < 0)
606
                        goto esc3;
607
                    code = get_rl_index(rl, last, run1, level);
608
                    if (code == rl->n) {
609
                    esc3:
610
                        /* third escape */
611
                        put_bits(&s->pb, 1, 0);
612
                        put_bits(&s->pb, 1, last);
613
                        put_bits(&s->pb, 6, run);
614
                        put_bits(&s->pb, 8, slevel & 0xff);
615
                    } else {
616
                        /* second escape */
617
                        put_bits(&s->pb, 1, 1);
618
                        put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
619
                        put_bits(&s->pb, 1, sign);
620
                    }
621
                } else {
622
                    /* first escape */
623
                    put_bits(&s->pb, 1, 1);
624
                    put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
625
                    put_bits(&s->pb, 1, sign);
626
                }
627
            } else {
628
                put_bits(&s->pb, 1, sign);
629
            }
630
            last_non_zero = i;
631
        }
632
    }
633
}
634

    
635
/****************************************/
636
/* decoding stuff */
637

    
638
static VLC mb_non_intra_vlc;
639
static VLC mb_intra_vlc;
640
static VLC dc_lum_vlc[2];
641
static VLC dc_chroma_vlc[2];
642

    
643
/* init all vlc decoding tables */
644
int msmpeg4_decode_init_vlc(MpegEncContext *s)
645
{
646
    int i;
647
    MVTable *mv;
648

    
649
    for(i=0;i<NB_RL_TABLES;i++) {
650
        init_rl(&rl_table[i]);
651
        init_vlc_rl(&rl_table[i]);
652
    }
653
    for(i=0;i<2;i++) {
654
        mv = &mv_tables[i];
655
        init_vlc(&mv->vlc, 9, mv->n + 1, 
656
                 mv->table_mv_bits, 1, 1,
657
                 mv->table_mv_code, 2, 2);
658
    }
659

    
660
    init_vlc(&dc_lum_vlc[0], 9, 120, 
661
             &table0_dc_lum[0][1], 8, 4,
662
             &table0_dc_lum[0][0], 8, 4);
663
    init_vlc(&dc_chroma_vlc[0], 9, 120, 
664
             &table0_dc_chroma[0][1], 8, 4,
665
             &table0_dc_chroma[0][0], 8, 4);
666
    init_vlc(&dc_lum_vlc[1], 9, 120, 
667
             &table1_dc_lum[0][1], 8, 4,
668
             &table1_dc_lum[0][0], 8, 4);
669
    init_vlc(&dc_chroma_vlc[1], 9, 120, 
670
             &table1_dc_chroma[0][1], 8, 4,
671
             &table1_dc_chroma[0][0], 8, 4);
672

    
673
    init_vlc(&mb_non_intra_vlc, 9, 128, 
674
             &table_mb_non_intra[0][1], 8, 4,
675
             &table_mb_non_intra[0][0], 8, 4);
676
    init_vlc(&mb_intra_vlc, 9, 64, 
677
             &table_mb_intra[0][1], 4, 2,
678
             &table_mb_intra[0][0], 4, 2);
679
    return 0;
680
}
681

    
682
static int decode012(GetBitContext *gb)
683
{
684
    int n;
685
    n = get_bits1(gb);
686
    if (n == 0)
687
        return 0;
688
    else
689
        return get_bits1(gb) + 1;
690
}
691

    
692
int msmpeg4_decode_picture_header(MpegEncContext * s)
693
{
694
    int code;
695

    
696
    s->pict_type = get_bits(&s->gb, 2) + 1;
697
    if (s->pict_type != I_TYPE &&
698
        s->pict_type != P_TYPE)
699
        return -1;
700

    
701
    s->qscale = get_bits(&s->gb, 5);
702

    
703
    if (s->pict_type == I_TYPE) {
704
        code = get_bits(&s->gb, 5); 
705
        /* 0x17: one slice, 0x18: three slices */
706
        /* XXX: implement it */
707
        //printf("%d %d %d\n", code, s->slice_height, s->first_slice_line);
708
        if (code < 0x17)
709
            return -1;
710
        s->slice_height = s->mb_height / (code - 0x16);
711
        s->rl_chroma_table_index = decode012(&s->gb);
712
        s->rl_table_index = decode012(&s->gb);
713

    
714
        s->dc_table_index = get_bits1(&s->gb);
715
        s->no_rounding = 1;
716
/*        printf(" %d %d %d %d     \n", 
717
                s->qscale,
718
                s->rl_chroma_table_index,
719
                s->rl_table_index, 
720
                s->dc_table_index);*/
721
    } else {
722
        s->use_skip_mb_code = get_bits1(&s->gb);
723
        
724
        s->rl_table_index = decode012(&s->gb);
725
        s->rl_chroma_table_index = s->rl_table_index;
726

    
727
        s->dc_table_index = get_bits1(&s->gb);
728

    
729
        s->mv_table_index = get_bits1(&s->gb);
730
/*        printf(" %d %d %d %d %d     \n", 
731
                s->use_skip_mb_code, 
732
                s->rl_table_index, 
733
                s->rl_chroma_table_index, 
734
                s->dc_table_index,
735
                s->mv_table_index);*/
736
        if(s->flipflop_rounding){
737
            s->no_rounding ^= 1;
738
        }else{
739
            s->no_rounding = 0;
740
        }
741
//        printf("%d", s->no_rounding);
742
    }
743
    
744
   
745
#ifdef DEBUG
746
    printf("*****frame %d:\n", frame_count++);
747
#endif
748
    return 0;
749
}
750

    
751
int msmpeg4_decode_ext_header(MpegEncContext * s, int buf_size)
752
{
753
    int firstBit=0;
754
    
755
    /* the alt_bitstream reader could read over the end so we need to check it */
756
    if(get_bits_count(&s->gb) < buf_size*8) firstBit= get_bits1(&s->gb);
757
    
758
    if(s->pict_type == P_TYPE)
759
    {
760
        if(firstBit) return -1; // havnt seen ext headers in P-Frames yet ;)
761
    }
762
    else
763
    {
764
        int unk;
765
        if(!firstBit) // no header found
766
        {
767
            s->flipflop_rounding= 0;
768
            s->bitrate= 0;
769
            return 0;
770
        }
771
        
772
        unk= get_bits(&s->gb, 4);
773
        s->bitrate= get_bits(&s->gb, 11);
774
        
775
//        printf("%2d %4d ;; %1X\n", unk,s->bitrate, unk);
776
    
777
        s->flipflop_rounding= get_bits1(&s->gb);
778
    }
779
    
780
    return 0;
781
}
782

    
783
void memsetw(short *tab, int val, int n)
784
{
785
    int i;
786
    for(i=0;i<n;i++)
787
        tab[i] = val;
788
}
789

    
790
int msmpeg4_decode_mb(MpegEncContext *s, 
791
                      DCTELEM block[6][64])
792
{
793
    int cbp, code, i;
794
    int pred, val;
795
    UINT8 *coded_val;
796

    
797
    /* special slice handling */
798
    if (s->mb_x == 0) {
799
        if (s->slice_height && (s->mb_y % s->slice_height) == 0) {
800
            int wrap;
801
            /* reset DC pred (set previous line to 1024) */
802
            wrap = 2 * s->mb_width + 2;
803
            memsetw(&s->dc_val[0][(1) + (2 * s->mb_y) * wrap], 
804
                    1024, 2 * s->mb_width);
805
            wrap = s->mb_width + 2;
806
            memsetw(&s->dc_val[1][(1) + (s->mb_y) * wrap], 
807
                    1024, s->mb_width);
808
            memsetw(&s->dc_val[2][(1) + (s->mb_y) * wrap], 
809
                    1024, s->mb_width);
810
            
811
            /* reset AC pred (set previous line to 0) */
812
            wrap = s->mb_width * 2 + 2;
813
            memsetw(s->ac_val[0][0] + (1 + (2 * s->mb_y) * wrap)*16,
814
                    0, 2 * s->mb_width*16);
815
            wrap = s->mb_width + 2;
816
            memsetw(s->ac_val[1][0] + (1 + (s->mb_y) * wrap)*16,
817
                    0, s->mb_width*16);
818
            memsetw(s->ac_val[2][0] + (1 + (s->mb_y) * wrap)*16,
819
                    0, s->mb_width*16);
820

    
821
            s->first_slice_line = 1;
822
        } else {
823
            s->first_slice_line = 0; 
824
        }
825
    }
826

    
827
    if (s->pict_type == P_TYPE) {
828
        set_stat(ST_INTER_MB);
829
        if (s->use_skip_mb_code) {
830
            if (get_bits1(&s->gb)) {
831
                /* skip mb */
832
                s->mb_intra = 0;
833
                for(i=0;i<6;i++)
834
                    s->block_last_index[i] = -1;
835
                s->mv_dir = MV_DIR_FORWARD;
836
                s->mv_type = MV_TYPE_16X16;
837
                s->mv[0][0][0] = 0;
838
                s->mv[0][0][1] = 0;
839
                s->mb_skiped = 1;
840
                return 0;
841
            }
842
        }
843
        
844
        code = get_vlc(&s->gb, &mb_non_intra_vlc);
845
        if (code < 0)
846
            return -1;
847
        if (code & 0x40)
848
            s->mb_intra = 0;
849
        else
850
            s->mb_intra = 1;
851
            
852
        cbp = code & 0x3f;
853
    } else {
854
        set_stat(ST_INTRA_MB);
855
        s->mb_intra = 1;
856
        code = get_vlc(&s->gb, &mb_intra_vlc);
857
        if (code < 0)
858
            return -1;
859
        /* predict coded block pattern */
860
        cbp = 0;
861
        for(i=0;i<6;i++) {
862
            val = ((code >> (5 - i)) & 1);
863
            if (i < 4) {
864
                pred = coded_block_pred(s, i, &coded_val);
865
                val = val ^ pred;
866
                *coded_val = val;
867
            }
868
            cbp |= val << (5 - i);
869
        }
870
    }
871

    
872
    if (!s->mb_intra) {
873
        int mx, my;
874
        set_stat(ST_MV);
875
        h263_pred_motion(s, 0, &mx, &my);
876
        if (msmpeg4_decode_motion(s, &mx, &my) < 0)
877
            return -1;
878
        s->mv_dir = MV_DIR_FORWARD;
879
        s->mv_type = MV_TYPE_16X16;
880
        s->mv[0][0][0] = mx;
881
        s->mv[0][0][1] = my;
882
    } else {
883
        set_stat(ST_INTRA_MB);
884
        s->ac_pred = get_bits1(&s->gb);
885
    }
886

    
887
    for (i = 0; i < 6; i++) {
888
        if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1) < 0)
889
            return -1;
890
    }
891
    return 0;
892
}
893

    
894
static int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
895
                              int n, int coded)
896
{
897
    int code, level, i, j, last, run, run_diff;
898
    int dc_pred_dir;
899
    RLTable *rl;
900
    const UINT8 *scan_table;
901
    int qmul, qadd;
902

    
903
    if (s->mb_intra) {
904
        qmul=1;
905
        qadd=0;
906

    
907
        /* DC coef */
908
        set_stat(ST_DC);
909
        level = msmpeg4_decode_dc(s, n, &dc_pred_dir);
910
        if (level < 0)
911
            return -1;
912
        block[0] = level;
913
        if (n < 4) {
914
            rl = &rl_table[s->rl_table_index];
915
        } else {
916
            rl = &rl_table[3 + s->rl_chroma_table_index];
917
        }
918

    
919
        run_diff = 0;
920
        i = 1;
921
        if (!coded) {
922
            goto not_coded;
923
        }
924
        if (s->ac_pred) {
925
            if (dc_pred_dir == 0) 
926
                scan_table = ff_alternate_vertical_scan; /* left */
927
            else
928
                scan_table = ff_alternate_horizontal_scan; /* top */
929
        } else {
930
            scan_table = zigzag_direct;
931
        }
932
        set_stat(ST_INTRA_AC);
933
    } else {
934
        qmul = s->qscale << 1;
935
        qadd = (s->qscale - 1) | 1;
936
        i = 0;
937
        rl = &rl_table[3 + s->rl_table_index];
938
        run_diff = 1;
939
        if (!coded) {
940
            s->block_last_index[n] = i - 1;
941
            return 0;
942
        }
943
        scan_table = zigzag_direct;
944
        set_stat(ST_INTER_AC);
945
    }
946

    
947
    for(;;) {
948
        code = get_vlc(&s->gb, &rl->vlc);
949
        if (code < 0)
950
            return -1;
951
        if (code == rl->n) {
952
            /* escape */
953
            if (get_bits1(&s->gb) == 0) {
954
                if (get_bits1(&s->gb) == 0) {
955
                    /* third escape */
956
                    last = get_bits1(&s->gb);
957
                    run = get_bits(&s->gb, 6);
958
                    level = get_bits(&s->gb, 8);
959
                    level = (level << 24) >> 24; /* sign extend */
960
                    if(level>0) level= level * qmul + qadd;
961
                    else        level= level * qmul - qadd;
962
                } else {
963
                    /* second escape */
964
                    code = get_vlc(&s->gb, &rl->vlc);
965
                    if (code < 0 || code >= rl->n)
966
                        return -1;
967
                    run = rl->table_run[code];
968
                    level = rl->table_level[code];
969
                    last = code >= rl->last;
970
                    run += rl->max_run[last][level] + run_diff;
971
                    level= level * qmul + qadd;
972
                    if (get_bits1(&s->gb))
973
                        level = -level;
974
                }
975
            } else {
976
                /* first escape */
977
                code = get_vlc(&s->gb, &rl->vlc);
978
                if (code < 0 || code >= rl->n)
979
                    return -1;
980
                run = rl->table_run[code];
981
                level = rl->table_level[code];
982
                last = code >= rl->last;
983
                level += rl->max_level[last][run];
984
                level= level * qmul + qadd;
985
                if (get_bits1(&s->gb))
986
                    level = -level;
987
            }
988
        } else {
989
            run = rl->table_run[code];
990
            level = rl->table_level[code] * qmul + qadd;
991
            last = code >= rl->last;
992
            if (get_bits1(&s->gb))
993
                level = -level;
994
        }
995
        i += run;
996
        if (i >= 64)
997
            return -1;
998
        j = scan_table[i];
999
        block[j] = level;
1000
        i++;
1001
        if (last)
1002
            break;
1003
    }
1004
 not_coded:
1005
    if (s->mb_intra) {
1006
        mpeg4_pred_ac(s, block, n, dc_pred_dir);
1007
        if (s->ac_pred) {
1008
            i = 64; /* XXX: not optimal */
1009
        }
1010
    }
1011
    s->block_last_index[n] = i - 1;
1012

    
1013
    return 0;
1014
}
1015

    
1016
static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr)
1017
{
1018
    int level, pred;
1019
    INT16 *dc_val;
1020

    
1021
    if (n < 4) {
1022
        level = get_vlc(&s->gb, &dc_lum_vlc[s->dc_table_index]);
1023
    } else {
1024
        level = get_vlc(&s->gb, &dc_chroma_vlc[s->dc_table_index]);
1025
    }
1026
    if (level < 0)
1027
        return -1;
1028

    
1029
    if (level == DC_MAX) {
1030
        level = get_bits(&s->gb, 8);
1031
        if (get_bits1(&s->gb))
1032
            level = -level;
1033
    } else if (level != 0) {
1034
        if (get_bits1(&s->gb))
1035
            level = -level;
1036
    }
1037

    
1038
    pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr);
1039
    level += pred;
1040

    
1041
    /* update predictor */
1042
    if (n < 4) {
1043
        *dc_val = level * s->y_dc_scale;
1044
    } else {
1045
        *dc_val = level * s->c_dc_scale;
1046
    }
1047

    
1048
    return level;
1049
}
1050

    
1051
static int msmpeg4_decode_motion(MpegEncContext * s, 
1052
                                 int *mx_ptr, int *my_ptr)
1053
{
1054
    MVTable *mv;
1055
    int code, mx, my;
1056

    
1057
    mv = &mv_tables[s->mv_table_index];
1058

    
1059
    code = get_vlc(&s->gb, &mv->vlc);
1060
    if (code < 0)
1061
        return -1;
1062
    if (code == mv->n) {
1063
        mx = get_bits(&s->gb, 6);
1064
        my = get_bits(&s->gb, 6);
1065
    } else {
1066
        mx = mv->table_mvx[code];
1067
        my = mv->table_mvy[code];
1068
    }
1069

    
1070
    mx += *mx_ptr - 32;
1071
    my += *my_ptr - 32;
1072
    /* WARNING : they do not do exactly modulo encoding */
1073
    if (mx <= -64)
1074
        mx += 64;
1075
    else if (mx >= 64)
1076
        mx -= 64;
1077

    
1078
    if (my <= -64)
1079
        my += 64;
1080
    else if (my >= 64)
1081
        my -= 64;
1082
    *mx_ptr = mx;
1083
    *my_ptr = my;
1084
    return 0;
1085
}