Statistics
| Branch: | Revision:

ffmpeg / libavcodec / msmpeg4.c @ 983e3246

History | View | Annotate | Download (60.1 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 983e3246 Michael Niedermayer
22
/**
23
 * @file msmpeg4.c
24
 * MSMPEG4 backend for ffmpeg encoder and decoder.
25
 */
26
27 6000abfa Fabrice Bellard
#include "avcodec.h"
28 de6d9b64 Fabrice Bellard
#include "dsputil.h"
29
#include "mpegvideo.h"
30 bd5e1c72 Michael Niedermayer
//#define PRINT_MB
31 92ba5ffb Michael Niedermayer
32 de6d9b64 Fabrice Bellard
/*
33
 * You can also call this codec : MPEG4 with a twist ! 
34
 *
35
 * TODO: 
36
 *        - (encoding) select best mv table (two choices)
37
 *        - (encoding) select best vlc/dc table 
38
 */
39
//#define DEBUG
40
41 08dce7b0 Michael Niedermayer
#define DC_VLC_BITS 9
42
#define CBPY_VLC_BITS 6
43
#define INTER_INTRA_VLC_BITS 3
44
#define V1_INTRA_CBPC_VLC_BITS 6
45
#define V1_INTER_CBPC_VLC_BITS 6
46
#define V2_INTRA_CBPC_VLC_BITS 3
47
#define V2_MB_TYPE_VLC_BITS 7
48
#define MV_VLC_BITS 9
49
#define V2_MV_VLC_BITS 9
50
#define TEX_VLC_BITS 9
51
#define MB_NON_INTRA_VLC_BITS 9
52
#define MB_INTRA_VLC_BITS 9
53
54 05174fd4 Michael Niedermayer
#define II_BITRATE 128*1024
55
#define MBAC_BITRATE 50*1024
56
57 1457ab52 Michael Niedermayer
#define DEFAULT_INTER_INDEX 3
58
59 0c1a9eda Zdenek Kabelac
static uint32_t v2_dc_lum_table[512][2];
60
static uint32_t v2_dc_chroma_table[512][2];
61 84afee34 Michael Niedermayer
62 7604246d Wolfgang Hesseler
#ifdef CONFIG_ENCODERS
63 f5957f3f Michael Niedermayer
static inline void msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n);
64 7604246d Wolfgang Hesseler
#endif //CONFIG_ENCODERS
65 f5957f3f Michael Niedermayer
static inline int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
66 1457ab52 Michael Niedermayer
                                       int n, int coded, const uint8_t *scantable);
67 de6d9b64 Fabrice Bellard
static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr);
68
static int msmpeg4_decode_motion(MpegEncContext * s, 
69
                                 int *mx_ptr, int *my_ptr);
70 7604246d Wolfgang Hesseler
#ifdef CONFIG_ENCODERS
71 3825cd1d Michael Niedermayer
static void msmpeg4v2_encode_motion(MpegEncContext * s, int val);
72 7604246d Wolfgang Hesseler
#endif //CONFIG_ENCODERS
73 20695ec9 Falk Hüffner
static void init_h263_dc_for_msmpeg4(void);
74 de0f2f4c Michael Niedermayer
static inline void msmpeg4_memsetw(short *tab, int val, int n);
75 7604246d Wolfgang Hesseler
#ifdef CONFIG_ENCODERS
76 62959862 Michael Niedermayer
static int get_size_of_code(MpegEncContext * s, RLTable *rl, int last, int run, int level, int intra);
77 7604246d Wolfgang Hesseler
#endif //CONFIG_ENCODERS
78 4d2858de Michael Niedermayer
static int msmpeg4v12_decode_mb(MpegEncContext *s, DCTELEM block[6][64]);
79
static int msmpeg4v34_decode_mb(MpegEncContext *s, DCTELEM block[6][64]);
80 1457ab52 Michael Niedermayer
static int wmv2_decode_mb(MpegEncContext *s, DCTELEM block[6][64]);
81 de6d9b64 Fabrice Bellard
82 0c1a9eda Zdenek Kabelac
extern uint32_t inverse[256];
83 6fe84b43 Michael Niedermayer
84 62959862 Michael Niedermayer
85 de6d9b64 Fabrice Bellard
#ifdef DEBUG
86
int intra_count = 0;
87
int frame_count = 0;
88
#endif
89
90
#include "msmpeg4data.h"
91
92 0c1a9eda Zdenek Kabelac
static uint8_t rl_length[NB_RL_TABLES][MAX_LEVEL+1][MAX_RUN+1][2];
93 62959862 Michael Niedermayer
94 de6d9b64 Fabrice Bellard
#ifdef STATS
95
96
const char *st_names[ST_NB] = {
97
    "unknown",
98
    "dc",
99
    "intra_ac",
100
    "inter_ac",
101
    "intra_mb",
102
    "inter_mb",
103
    "mv",
104
};
105
106
int st_current_index = 0;
107
unsigned int st_bit_counts[ST_NB];
108
unsigned int st_out_bit_counts[ST_NB];
109
110
#define set_stat(var) st_current_index = var;
111
112
void print_stats(void)
113
{
114
    unsigned int total;
115
    int i;
116
117
    printf("Input:\n");
118
    total = 0;
119
    for(i=0;i<ST_NB;i++)
120
        total += st_bit_counts[i];
121
    if (total == 0)
122
        total = 1;
123
    for(i=0;i<ST_NB;i++) {
124
        printf("%-10s : %10.1f %5.1f%%\n", 
125
               st_names[i], 
126
               (double)st_bit_counts[i] / 8.0, 
127
               (double)st_bit_counts[i] * 100.0 / total);
128
    }
129
    printf("%-10s : %10.1f %5.1f%%\n",
130
           "total", 
131
           (double)total / 8.0, 
132
           100.0);
133
134
    printf("Output:\n");
135
    total = 0;
136
    for(i=0;i<ST_NB;i++)
137
        total += st_out_bit_counts[i];
138
    if (total == 0)
139
        total = 1;
140
    for(i=0;i<ST_NB;i++) {
141
        printf("%-10s : %10.1f %5.1f%%\n", 
142
               st_names[i], 
143
               (double)st_out_bit_counts[i] / 8.0, 
144
               (double)st_out_bit_counts[i] * 100.0 / total);
145
    }
146
    printf("%-10s : %10.1f %5.1f%%\n",
147
           "total", 
148
           (double)total / 8.0, 
149
           100.0);
150
}
151
152
#else
153
154
#define set_stat(var)
155
156
#endif
157
158 f5957f3f Michael Niedermayer
static void common_init(MpegEncContext * s)
159
{
160
    static int inited=0;
161
    
162
    switch(s->msmpeg4_version){
163
    case 1:
164
    case 2:
165
        s->y_dc_scale_table=
166
        s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
167
        break;
168
    case 3:
169
        if(s->workaround_bugs){
170
            s->y_dc_scale_table= old_ff_y_dc_scale_table;
171
            s->c_dc_scale_table= old_ff_c_dc_scale_table;
172
        } else{
173
            s->y_dc_scale_table= ff_mpeg4_y_dc_scale_table;
174
            s->c_dc_scale_table= ff_mpeg4_c_dc_scale_table;
175
        }
176
        break;
177
    case 4:
178 1457ab52 Michael Niedermayer
    case 5:
179 f5957f3f Michael Niedermayer
        s->y_dc_scale_table= wmv1_y_dc_scale_table;
180
        s->c_dc_scale_table= wmv1_c_dc_scale_table;
181
        break;
182
    }
183
184 2ad1516a Michael Niedermayer
    
185 1457ab52 Michael Niedermayer
    if(s->msmpeg4_version>=4){
186 2ad1516a Michael Niedermayer
        ff_init_scantable(s, &s->intra_scantable  , wmv1_scantable[1]);
187
        ff_init_scantable(s, &s->intra_h_scantable, wmv1_scantable[2]);
188
        ff_init_scantable(s, &s->intra_v_scantable, wmv1_scantable[3]);
189
        ff_init_scantable(s, &s->inter_scantable  , wmv1_scantable[0]);
190 f5957f3f Michael Niedermayer
    }
191 2ad1516a Michael Niedermayer
    //Note the default tables are set in common_init in mpegvideo.c
192 f5957f3f Michael Niedermayer
    
193
    if(!inited){
194
        inited=1;
195
196
        init_h263_dc_for_msmpeg4();
197
    }
198
}
199
200 7604246d Wolfgang Hesseler
#ifdef CONFIG_ENCODERS
201
202 de6d9b64 Fabrice Bellard
/* build the table which associate a (x,y) motion vector to a vlc */
203
static void init_mv_table(MVTable *tab)
204
{
205
    int i, x, y;
206
207 0c1a9eda Zdenek Kabelac
    tab->table_mv_index = av_malloc(sizeof(uint16_t) * 4096);
208 de6d9b64 Fabrice Bellard
    /* mark all entries as not used */
209
    for(i=0;i<4096;i++)
210
        tab->table_mv_index[i] = tab->n;
211
    
212
    for(i=0;i<tab->n;i++) {
213
        x = tab->table_mvx[i];
214
        y = tab->table_mvy[i];
215
        tab->table_mv_index[(x << 6) | y] = i;
216
    }
217
}
218
219
static void code012(PutBitContext *pb, int n)
220
{
221
    if (n == 0) {
222
        put_bits(pb, 1, 0);
223
    } else {
224
        put_bits(pb, 1, 1);
225
        put_bits(pb, 1, (n >= 2));
226
    }
227
}
228
229 f5957f3f Michael Niedermayer
void ff_msmpeg4_encode_init(MpegEncContext *s)
230
{
231
    static int init_done=0;
232
    int i;
233
234
    common_init(s);
235
    if(s->msmpeg4_version>=4){
236
        s->min_qcoeff= -255;
237
        s->max_qcoeff=  255;
238
    }
239
240
    if (!init_done) {
241
        /* init various encoding tables */
242
        init_done = 1;
243
        init_mv_table(&mv_tables[0]);
244
        init_mv_table(&mv_tables[1]);
245
        for(i=0;i<NB_RL_TABLES;i++)
246
            init_rl(&rl_table[i]);
247 62959862 Michael Niedermayer
248
        for(i=0; i<NB_RL_TABLES; i++){
249
            int level;
250
            for(level=0; level<=MAX_LEVEL; level++){
251
                int run;
252
                for(run=0; run<=MAX_RUN; run++){
253
                    int last;
254
                    for(last=0; last<2; last++){
255 060f89b3 Michael Niedermayer
                        rl_length[i][level][run][last]= get_size_of_code(s, &rl_table[  i], last, run, level, 0);
256 62959862 Michael Niedermayer
                    }
257
                }
258
            }
259
        }
260 f5957f3f Michael Niedermayer
    }
261
}
262
263
static int get_size_of_code(MpegEncContext * s, RLTable *rl, int last, int run, int level, int intra){
264
    int size=0;
265
    int code;
266
    int run_diff= intra ? 0 : 1;
267
    
268
    code = get_rl_index(rl, last, run, level);
269
    size+= rl->table_vlc[code][1];
270
    if (code == rl->n) {
271
        int level1, run1;
272
273
        level1 = level - rl->max_level[last][run];
274
        if (level1 < 1) 
275
            goto esc2;
276
        code = get_rl_index(rl, last, run, level1);
277
        if (code == rl->n) {
278
            esc2:
279
            size++;
280
            if (level > MAX_LEVEL)
281
                goto esc3;
282
            run1 = run - rl->max_run[last][level] - run_diff;
283
            if (run1 < 0)
284
                goto esc3;
285
            code = get_rl_index(rl, last, run1, level);
286
            if (code == rl->n) {
287
            esc3:
288
                /* third escape */
289
                size+=1+1+6+8;
290
            } else {
291
                /* second escape */
292
                size+= 1+1+ rl->table_vlc[code][1];
293
            }
294
        } else {
295
            /* first escape */
296
            size+= 1+1+ rl->table_vlc[code][1];
297
        }
298
    } else {
299
        size++;
300
    }
301
    return size;
302
}
303
304
static void find_best_tables(MpegEncContext * s)
305
{
306
    int i;
307
    int best       =-1, best_size       =9999999;
308
    int chroma_best=-1, best_chroma_size=9999999;
309 62959862 Michael Niedermayer
310 f5957f3f Michael Niedermayer
    for(i=0; i<3; i++){
311
        int level;
312
        int chroma_size=0;
313
        int size=0;
314
315
        if(i>0){// ;)
316
            size++; 
317
            chroma_size++;
318
        }
319
        for(level=0; level<=MAX_LEVEL; level++){
320
            int run;
321
            for(run=0; run<=MAX_RUN; run++){
322
                int last;
323 62959862 Michael Niedermayer
                const int last_size= size + chroma_size;
324 f5957f3f Michael Niedermayer
                for(last=0; last<2; last++){
325
                    int inter_count       = s->ac_stats[0][0][level][run][last] + s->ac_stats[0][1][level][run][last];
326
                    int intra_luma_count  = s->ac_stats[1][0][level][run][last];
327
                    int intra_chroma_count= s->ac_stats[1][1][level][run][last];
328 62959862 Michael Niedermayer
                    
329 f5957f3f Michael Niedermayer
                    if(s->pict_type==I_TYPE){
330 060f89b3 Michael Niedermayer
                        size       += intra_luma_count  *rl_length[i  ][level][run][last];
331
                        chroma_size+= intra_chroma_count*rl_length[i+3][level][run][last];
332 f5957f3f Michael Niedermayer
                    }else{
333 060f89b3 Michael Niedermayer
                        size+=        intra_luma_count  *rl_length[i  ][level][run][last]
334
                                     +intra_chroma_count*rl_length[i+3][level][run][last]
335
                                     +inter_count       *rl_length[i+3][level][run][last];
336 f5957f3f Michael Niedermayer
                    }                   
337
                }
338 62959862 Michael Niedermayer
                if(last_size == size+chroma_size) break;
339 f5957f3f Michael Niedermayer
            }
340
        }
341
        if(size<best_size){
342
            best_size= size;
343
            best= i;
344
        }
345
        if(chroma_size<best_chroma_size){
346
            best_chroma_size= chroma_size;
347
            chroma_best= i;
348
        }
349
    }
350 62959862 Michael Niedermayer
351 f5957f3f Michael Niedermayer
//    printf("type:%d, best:%d, qp:%d, var:%d, mcvar:%d, size:%d //\n", 
352
//           s->pict_type, best, s->qscale, s->mb_var_sum, s->mc_mb_var_sum, best_size);
353
           
354
    if(s->pict_type==P_TYPE) chroma_best= best;
355
356
    memset(s->ac_stats, 0, sizeof(int)*(MAX_LEVEL+1)*(MAX_RUN+1)*2*2*2);
357
358
    s->rl_table_index       =        best;
359
    s->rl_chroma_table_index= chroma_best;
360
    
361
    if(s->pict_type != s->last_non_b_pict_type){
362
        s->rl_table_index= 2;
363
        if(s->pict_type==I_TYPE)
364
            s->rl_chroma_table_index= 1;
365
        else
366
            s->rl_chroma_table_index= 2;
367
    }
368
369
}
370
371 287229e5 Michael Niedermayer
/* write MSMPEG4 compatible frame header */
372 de6d9b64 Fabrice Bellard
void msmpeg4_encode_picture_header(MpegEncContext * s, int picture_number)
373
{
374 f5957f3f Michael Niedermayer
    find_best_tables(s);
375 de6d9b64 Fabrice Bellard
376
    align_put_bits(&s->pb);
377
    put_bits(&s->pb, 2, s->pict_type - 1);
378
379
    put_bits(&s->pb, 5, s->qscale);
380 f5957f3f Michael Niedermayer
    if(s->msmpeg4_version<=2){
381
        s->rl_table_index = 2;
382
        s->rl_chroma_table_index = 2;
383
    }
384 3825cd1d Michael Niedermayer
385 de6d9b64 Fabrice Bellard
    s->dc_table_index = 1;
386
    s->mv_table_index = 1; /* only if P frame */
387
    s->use_skip_mb_code = 1; /* only if P frame */
388 f5957f3f Michael Niedermayer
    s->per_mb_rl_table = 0;
389 fc48cbad Michael Niedermayer
    if(s->msmpeg4_version==4)
390
        s->inter_intra_pred= (s->width*s->height < 320*240 && s->bit_rate<=II_BITRATE && s->pict_type==P_TYPE);
391 1457ab52 Michael Niedermayer
//printf("%d %d %d %d %d\n", s->pict_type, s->bit_rate, s->inter_intra_pred, s->width, s->height);
392 f5957f3f Michael Niedermayer
393 de6d9b64 Fabrice Bellard
    if (s->pict_type == I_TYPE) {
394 de0f2f4c Michael Niedermayer
        s->slice_height= s->mb_height/1;
395
        put_bits(&s->pb, 5, 0x16 + s->mb_height/s->slice_height);
396 f5957f3f Michael Niedermayer
        
397
        if(s->msmpeg4_version==4){
398
            msmpeg4_encode_ext_header(s);
399 05174fd4 Michael Niedermayer
            if(s->bit_rate>MBAC_BITRATE)
400 de0f2f4c Michael Niedermayer
                put_bits(&s->pb, 1, s->per_mb_rl_table);
401 f5957f3f Michael Niedermayer
        }
402 de6d9b64 Fabrice Bellard
403 287229e5 Michael Niedermayer
        if(s->msmpeg4_version>2){
404 f5957f3f Michael Niedermayer
            if(!s->per_mb_rl_table){
405
                code012(&s->pb, s->rl_chroma_table_index);
406
                code012(&s->pb, s->rl_table_index);
407
            }
408 de6d9b64 Fabrice Bellard
409 3825cd1d Michael Niedermayer
            put_bits(&s->pb, 1, s->dc_table_index);
410
        }
411 de6d9b64 Fabrice Bellard
    } else {
412
        put_bits(&s->pb, 1, s->use_skip_mb_code);
413
        
414 05174fd4 Michael Niedermayer
        if(s->msmpeg4_version==4 && s->bit_rate>MBAC_BITRATE)
415 f5957f3f Michael Niedermayer
            put_bits(&s->pb, 1, s->per_mb_rl_table);
416
417 287229e5 Michael Niedermayer
        if(s->msmpeg4_version>2){
418 f5957f3f Michael Niedermayer
            if(!s->per_mb_rl_table)
419
                code012(&s->pb, s->rl_table_index);
420 de6d9b64 Fabrice Bellard
421 3825cd1d Michael Niedermayer
            put_bits(&s->pb, 1, s->dc_table_index);
422
423
            put_bits(&s->pb, 1, s->mv_table_index);
424
        }
425 de6d9b64 Fabrice Bellard
    }
426
427 f5957f3f Michael Niedermayer
    s->esc3_level_length= 0;
428
    s->esc3_run_length= 0;
429 de6d9b64 Fabrice Bellard
430
#ifdef DEBUG
431
    intra_count = 0;
432
    printf("*****frame %d:\n", frame_count++);
433
#endif
434
}
435
436 ae40484c Michael Niedermayer
void msmpeg4_encode_ext_header(MpegEncContext * s)
437
{
438 2b9ab1d5 Michael Niedermayer
        put_bits(&s->pb, 5, s->frame_rate / FRAME_RATE_BASE); //yes 29.97 -> 29
439 ae40484c Michael Niedermayer
440 b8a78f41 Michael Niedermayer
        put_bits(&s->pb, 11, FFMIN(s->bit_rate/1024, 2047));
441 ae40484c Michael Niedermayer
442 287229e5 Michael Niedermayer
        if(s->msmpeg4_version<3)
443
            s->flipflop_rounding=0;
444
        else{
445
            s->flipflop_rounding=1;
446
            put_bits(&s->pb, 1, s->flipflop_rounding);
447
        }
448 ae40484c Michael Niedermayer
}
449
450 7604246d Wolfgang Hesseler
#endif //CONFIG_ENCODERS
451
452 de6d9b64 Fabrice Bellard
/* predict coded block */
453 0c1a9eda Zdenek Kabelac
static inline int coded_block_pred(MpegEncContext * s, int n, uint8_t **coded_block_ptr)
454 de6d9b64 Fabrice Bellard
{
455 dbbe8999 Michael Niedermayer
    int xy, wrap, pred, a, b, c;
456 de6d9b64 Fabrice Bellard
457 dbbe8999 Michael Niedermayer
    xy = s->block_index[n];
458
    wrap = s->block_wrap[0];
459 de6d9b64 Fabrice Bellard
460
    /* B C
461
     * A X 
462
     */
463 dbbe8999 Michael Niedermayer
    a = s->coded_block[xy - 1       ];
464
    b = s->coded_block[xy - 1 - wrap];
465
    c = s->coded_block[xy     - wrap];
466 de6d9b64 Fabrice Bellard
    
467
    if (b == c) {
468
        pred = a;
469
    } else {
470
        pred = c;
471
    }
472
    
473
    /* store value */
474 dbbe8999 Michael Niedermayer
    *coded_block_ptr = &s->coded_block[xy];
475 de6d9b64 Fabrice Bellard
476
    return pred;
477
}
478
479 7604246d Wolfgang Hesseler
#ifdef CONFIG_ENCODERS
480
481 de6d9b64 Fabrice Bellard
static void msmpeg4_encode_motion(MpegEncContext * s, 
482
                                  int mx, int my)
483
{
484
    int code;
485
    MVTable *mv;
486
487
    /* modulo encoding */
488
    /* WARNING : you cannot reach all the MVs even with the modulo
489
       encoding. This is a somewhat strange compromise they took !!!  */
490
    if (mx <= -64)
491
        mx += 64;
492
    else if (mx >= 64)
493
        mx -= 64;
494
    if (my <= -64)
495
        my += 64;
496
    else if (my >= 64)
497
        my -= 64;
498
    
499
    mx += 32;
500
    my += 32;
501
#if 0
502
    if ((unsigned)mx >= 64 ||
503
        (unsigned)my >= 64) 
504
        fprintf(stderr, "error mx=%d my=%d\n", mx, my);
505
#endif
506
    mv = &mv_tables[s->mv_table_index];
507
508
    code = mv->table_mv_index[(mx << 6) | my];
509
    set_stat(ST_MV);
510
    put_bits(&s->pb, 
511
             mv->table_mv_bits[code], 
512
             mv->table_mv_code[code]);
513
    if (code == mv->n) {
514
        /* escape : code litterally */
515
        put_bits(&s->pb, 6, mx);
516
        put_bits(&s->pb, 6, my);
517
    }
518
}
519
520 de0f2f4c Michael Niedermayer
static inline void handle_slices(MpegEncContext *s){
521
    if (s->mb_x == 0) {
522
        if (s->slice_height && (s->mb_y % s->slice_height) == 0) {
523 28269849 Michael Niedermayer
            if(s->msmpeg4_version < 4){
524 4d2858de Michael Niedermayer
                ff_mpeg4_clean_buffers(s);
525 de0f2f4c Michael Niedermayer
            }
526
            s->first_slice_line = 1;
527
        } else {
528
            s->first_slice_line = 0; 
529
        }
530
    }
531
}
532
533 de6d9b64 Fabrice Bellard
void msmpeg4_encode_mb(MpegEncContext * s, 
534
                       DCTELEM block[6][64],
535
                       int motion_x, int motion_y)
536
{
537
    int cbp, coded_cbp, i;
538
    int pred_x, pred_y;
539 0c1a9eda Zdenek Kabelac
    uint8_t *coded_block;
540 de6d9b64 Fabrice Bellard
541 de0f2f4c Michael Niedermayer
    handle_slices(s);
542
    
543 de6d9b64 Fabrice Bellard
    if (!s->mb_intra) {
544
        /* compute cbp */
545
        set_stat(ST_INTER_MB);
546
        cbp = 0;
547
        for (i = 0; i < 6; i++) {
548
            if (s->block_last_index[i] >= 0)
549
                cbp |= 1 << (5 - i);
550
        }
551
        if (s->use_skip_mb_code && (cbp | motion_x | motion_y) == 0) {
552
            /* skip macroblock */
553
            put_bits(&s->pb, 1, 1);
554
            return;
555
        }
556
        if (s->use_skip_mb_code)
557
            put_bits(&s->pb, 1, 0);        /* mb coded */
558
        
559 287229e5 Michael Niedermayer
        if(s->msmpeg4_version<=2){
560 3825cd1d Michael Niedermayer
            put_bits(&s->pb, 
561
                     v2_mb_type[cbp&3][1], 
562
                     v2_mb_type[cbp&3][0]);
563
            if((cbp&3) != 3) coded_cbp= cbp ^ 0x3C;
564
            else             coded_cbp= cbp;
565
566
            put_bits(&s->pb, 
567
                     cbpy_tab[coded_cbp>>2][1], 
568
                     cbpy_tab[coded_cbp>>2][0]);
569
                        
570
            h263_pred_motion(s, 0, &pred_x, &pred_y);
571
            msmpeg4v2_encode_motion(s, motion_x - pred_x);
572
            msmpeg4v2_encode_motion(s, motion_y - pred_y);
573
        }else{
574
            put_bits(&s->pb, 
575
                     table_mb_non_intra[cbp + 64][1], 
576
                     table_mb_non_intra[cbp + 64][0]);
577
578
            /* motion vector */
579
            h263_pred_motion(s, 0, &pred_x, &pred_y);
580
            msmpeg4_encode_motion(s, motion_x - pred_x, 
581
                                  motion_y - pred_y);
582
        }
583 de6d9b64 Fabrice Bellard
    } else {
584
        /* compute cbp */
585
        cbp = 0;
586
        coded_cbp = 0;
587
        for (i = 0; i < 6; i++) {
588
            int val, pred;
589
            val = (s->block_last_index[i] >= 1);
590
            cbp |= val << (5 - i);
591
            if (i < 4) {
592
                /* predict value for close blocks only for luma */
593
                pred = coded_block_pred(s, i, &coded_block);
594
                *coded_block = val;
595
                val = val ^ pred;
596
            }
597
            coded_cbp |= val << (5 - i);
598
        }
599
#if 0
600
        if (coded_cbp)
601
            printf("cbp=%x %x\n", cbp, coded_cbp);
602
#endif
603
604 287229e5 Michael Niedermayer
        if(s->msmpeg4_version<=2){
605 3825cd1d Michael Niedermayer
            if (s->pict_type == I_TYPE) {
606
                put_bits(&s->pb, 
607
                         v2_intra_cbpc[cbp&3][1], v2_intra_cbpc[cbp&3][0]);
608
            } else {
609
                if (s->use_skip_mb_code)
610
                    put_bits(&s->pb, 1, 0);        /* mb coded */
611
                put_bits(&s->pb, 
612
                         v2_mb_type[(cbp&3) + 4][1], 
613
                         v2_mb_type[(cbp&3) + 4][0]);
614
            }
615
            put_bits(&s->pb, 1, 0);        /* no AC prediction yet */
616 de6d9b64 Fabrice Bellard
            put_bits(&s->pb, 
617 3825cd1d Michael Niedermayer
                     cbpy_tab[cbp>>2][1], 
618
                     cbpy_tab[cbp>>2][0]);
619
        }else{
620
            if (s->pict_type == I_TYPE) {
621
                set_stat(ST_INTRA_MB);
622
                put_bits(&s->pb, 
623
                         table_mb_intra[coded_cbp][1], table_mb_intra[coded_cbp][0]);
624
            } else {
625
                if (s->use_skip_mb_code)
626
                    put_bits(&s->pb, 1, 0);        /* mb coded */
627
                put_bits(&s->pb, 
628
                         table_mb_non_intra[cbp][1], 
629
                         table_mb_non_intra[cbp][0]);
630
            }
631
            set_stat(ST_INTRA_MB);
632
            put_bits(&s->pb, 1, 0);        /* no AC prediction yet */
633 05174fd4 Michael Niedermayer
            if(s->inter_intra_pred){
634
                s->h263_aic_dir=0;
635
                put_bits(&s->pb, table_inter_intra[s->h263_aic_dir][1], table_inter_intra[s->h263_aic_dir][0]);
636
            }
637 de6d9b64 Fabrice Bellard
        }
638
    }
639
640
    for (i = 0; i < 6; i++) {
641
        msmpeg4_encode_block(s, block[i], i);
642
    }
643
}
644
645 7604246d Wolfgang Hesseler
#endif //CONFIG_ENCODERS
646
647 92ba5ffb Michael Niedermayer
/* old ffmpeg msmpeg4v3 mode */
648 5c91a675 Zdenek Kabelac
static void ff_old_msmpeg4_dc_scale(MpegEncContext * s)
649 de6d9b64 Fabrice Bellard
{
650 92ba5ffb Michael Niedermayer
    if (s->qscale < 5){
651 6fb904ce Michael Niedermayer
        s->y_dc_scale = 8;
652
        s->c_dc_scale = 8;
653
    }else if (s->qscale < 9){
654
        s->y_dc_scale = 2 * s->qscale;
655
        s->c_dc_scale = (s->qscale + 13)>>1;
656 92ba5ffb Michael Niedermayer
    }else{
657 6fb904ce Michael Niedermayer
        s->y_dc_scale = s->qscale + 8;
658
        s->c_dc_scale = (s->qscale + 13)>>1;
659
    }
660 de6d9b64 Fabrice Bellard
}
661
662 f5957f3f Michael Niedermayer
static inline int msmpeg4v1_pred_dc(MpegEncContext * s, int n, 
663 0c1a9eda Zdenek Kabelac
                                    int32_t **dc_val_ptr)
664 287229e5 Michael Niedermayer
{
665
    int i;
666
667
    if (n < 4) {
668
        i= 0;
669
    } else {
670
        i= n-3;
671
    }
672
    
673
    *dc_val_ptr= &s->last_dc[i];
674
    return s->last_dc[i]; 
675
}
676
677 de0f2f4c Michael Niedermayer
static int get_dc(uint8_t *src, int stride, int scale)
678
{
679
    int y;
680
    int sum=0;
681
    for(y=0; y<8; y++){
682
        int x;
683
        for(x=0; x<8; x++){
684
            sum+=src[x + y*stride];
685
        }
686
    }
687
    return (sum + (scale>>1))/scale;
688
}
689
690 de6d9b64 Fabrice Bellard
/* dir = 0: left, dir = 1: top prediction */
691 f5957f3f Michael Niedermayer
static inline int msmpeg4_pred_dc(MpegEncContext * s, int n, 
692 0c1a9eda Zdenek Kabelac
                             uint16_t **dc_val_ptr, int *dir_ptr)
693 de6d9b64 Fabrice Bellard
{
694 dbbe8999 Michael Niedermayer
    int a, b, c, wrap, pred, scale;
695 0c1a9eda Zdenek Kabelac
    int16_t *dc_val;
696 de6d9b64 Fabrice Bellard
697
    /* find prediction */
698
    if (n < 4) {
699
        scale = s->y_dc_scale;
700
    } else {
701
        scale = s->c_dc_scale;
702
    }
703 287229e5 Michael Niedermayer
    
704 dbbe8999 Michael Niedermayer
    wrap = s->block_wrap[n];
705
    dc_val= s->dc_val[0] + s->block_index[n];
706 de6d9b64 Fabrice Bellard
707
    /* B C
708
     * A X 
709
     */
710 dbbe8999 Michael Niedermayer
    a = dc_val[ - 1];
711
    b = dc_val[ - 1 - wrap];
712
    c = dc_val[ - wrap];
713 4d2858de Michael Niedermayer
    
714 28269849 Michael Niedermayer
    if(s->first_slice_line && (n&2)==0 && s->msmpeg4_version<4){
715 4d2858de Michael Niedermayer
        b=c=1024;
716
    }
717 de6d9b64 Fabrice Bellard
718
    /* XXX: the following solution consumes divisions, but it does not
719
       necessitate to modify mpegvideo.c. The problem comes from the
720
       fact they decided to store the quantized DC (which would lead
721
       to problems if Q could vary !) */
722 320680d4 Zdenek Kabelac
#if defined ARCH_X86 && !defined PIC
723 6f903d8e Michael Niedermayer
    asm volatile(
724
        "movl %3, %%eax                \n\t"
725
        "shrl $1, %%eax                \n\t"
726
        "addl %%eax, %2                \n\t"
727
        "addl %%eax, %1                \n\t"
728
        "addl %0, %%eax                \n\t"
729 6fe84b43 Michael Niedermayer
        "mull %4                \n\t"
730
        "movl %%edx, %0                \n\t"
731 6f903d8e Michael Niedermayer
        "movl %1, %%eax                \n\t"
732 6fe84b43 Michael Niedermayer
        "mull %4                \n\t"
733
        "movl %%edx, %1                \n\t"
734 6f903d8e Michael Niedermayer
        "movl %2, %%eax                \n\t"
735 6fe84b43 Michael Niedermayer
        "mull %4                \n\t"
736
        "movl %%edx, %2                \n\t"
737 fa778d53 Michael Niedermayer
        : "+b" (a), "+c" (b), "+D" (c)
738
        : "g" (scale), "S" (inverse[scale])
739 6f903d8e Michael Niedermayer
        : "%eax", "%edx"
740
    );
741 320680d4 Zdenek Kabelac
#else
742
    /* #elif defined (ARCH_ALPHA) */
743 1e98dffb Nick Kurshev
    /* Divisions are extremely costly on Alpha; optimize the most
744 320680d4 Zdenek Kabelac
       common case. But they are costly everywhere...
745
     */
746 1e98dffb Nick Kurshev
    if (scale == 8) {
747
        a = (a + (8 >> 1)) / 8;
748
        b = (b + (8 >> 1)) / 8;
749
        c = (c + (8 >> 1)) / 8;
750
    } else {
751
        a = (a + (scale >> 1)) / scale;
752
        b = (b + (scale >> 1)) / scale;
753
        c = (c + (scale >> 1)) / scale;
754
    }
755 6f903d8e Michael Niedermayer
#endif
756 de6d9b64 Fabrice Bellard
    /* XXX: WARNING: they did not choose the same test as MPEG4. This
757
       is very important ! */
758 bd5e1c72 Michael Niedermayer
    if(s->msmpeg4_version>3){
759 de0f2f4c Michael Niedermayer
        if(s->inter_intra_pred){
760
            uint8_t *dest;
761
            int wrap;
762
            
763
            if(n==1){
764
                pred=a;
765
                *dir_ptr = 0;
766
            }else if(n==2){
767
                pred=c;
768
                *dir_ptr = 1;
769
            }else if(n==3){
770
                if (abs(a - b) < abs(b - c)) {
771
                    pred = c;
772
                    *dir_ptr = 1;
773
                } else {
774
                    pred = a;
775
                    *dir_ptr = 0;
776
                }
777
            }else{
778
                if(n<4){
779
                    wrap= s->linesize;
780 1e491e29 Michael Niedermayer
                    dest= s->current_picture.data[0] + (((n>>1) + 2*s->mb_y) * 8*  wrap ) + ((n&1) + 2*s->mb_x) * 8;
781 de0f2f4c Michael Niedermayer
                }else{
782 0fd90455 Michael Niedermayer
                    wrap= s->uvlinesize;
783 1e491e29 Michael Niedermayer
                    dest= s->current_picture.data[n-3] + (s->mb_y * 8 * wrap) + s->mb_x * 8;
784 de0f2f4c Michael Niedermayer
                }
785
                if(s->mb_x==0) a= (1024 + (scale>>1))/scale;
786
                else           a= get_dc(dest-8, wrap, scale*8);
787
                if(s->mb_y==0) c= (1024 + (scale>>1))/scale;
788
                else           c= get_dc(dest-8*wrap, wrap, scale*8);
789
                
790
                if (s->h263_aic_dir==0) {
791
                    pred= a;
792
                    *dir_ptr = 0;
793
                }else if (s->h263_aic_dir==1) {
794
                    if(n==0){
795
                        pred= c;
796
                        *dir_ptr = 1;
797
                    }else{
798
                        pred= a;
799
                        *dir_ptr = 0;
800
                    }
801
                }else if (s->h263_aic_dir==2) {
802
                    if(n==0){
803
                        pred= a;
804
                        *dir_ptr = 0;
805
                    }else{
806
                        pred= c;
807
                        *dir_ptr = 1;
808
                    }
809
                } else {
810
                    pred= c;
811
                    *dir_ptr = 1;
812
                }
813
            }
814
        }else{
815
            if (abs(a - b) < abs(b - c)) {
816
                pred = c;
817
                *dir_ptr = 1;
818
            } else {
819
                pred = a;
820
                *dir_ptr = 0;
821
            }
822 bd5e1c72 Michael Niedermayer
        }
823
    }else{
824
        if (abs(a - b) <= abs(b - c)) {
825
            pred = c;
826
            *dir_ptr = 1;
827
        } else {
828
            pred = a;
829
            *dir_ptr = 0;
830
        }
831 de6d9b64 Fabrice Bellard
    }
832
833
    /* update predictor */
834 dbbe8999 Michael Niedermayer
    *dc_val_ptr = &dc_val[0];
835 de6d9b64 Fabrice Bellard
    return pred;
836
}
837
838
#define DC_MAX 119
839
840 7604246d Wolfgang Hesseler
#ifdef CONFIG_ENCODERS
841
842 de6d9b64 Fabrice Bellard
static void msmpeg4_encode_dc(MpegEncContext * s, int level, int n, int *dir_ptr)
843
{
844
    int sign, code;
845
    int pred;
846
847 287229e5 Michael Niedermayer
    if(s->msmpeg4_version==1){
848 0c1a9eda Zdenek Kabelac
        int32_t *dc_val;
849 287229e5 Michael Niedermayer
        pred = msmpeg4v1_pred_dc(s, n, &dc_val);
850
        
851
        /* update predictor */
852
        *dc_val= level;
853
    }else{
854 0c1a9eda Zdenek Kabelac
        uint16_t *dc_val;
855 bd5e1c72 Michael Niedermayer
        pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr);
856 de6d9b64 Fabrice Bellard
857 287229e5 Michael Niedermayer
        /* update predictor */
858
        if (n < 4) {
859
            *dc_val = level * s->y_dc_scale;
860
        } else {
861
            *dc_val = level * s->c_dc_scale;
862
        }
863 de6d9b64 Fabrice Bellard
    }
864
865
    /* do the prediction */
866
    level -= pred;
867
868 287229e5 Michael Niedermayer
    if(s->msmpeg4_version<=2){
869 de6d9b64 Fabrice Bellard
        if (n < 4) {
870 3825cd1d Michael Niedermayer
            put_bits(&s->pb, 
871
                     v2_dc_lum_table[level+256][1],
872
                     v2_dc_lum_table[level+256][0]);
873
        }else{
874
            put_bits(&s->pb, 
875
                     v2_dc_chroma_table[level+256][1],
876
                     v2_dc_chroma_table[level+256][0]);
877 de6d9b64 Fabrice Bellard
        }
878 3825cd1d Michael Niedermayer
    }else{
879
        sign = 0;
880
        if (level < 0) {
881
            level = -level;
882
            sign = 1;
883
        }
884
        code = level;
885
        if (code > DC_MAX) 
886
            code = DC_MAX;
887
888
        if (s->dc_table_index == 0) {
889
            if (n < 4) {
890
                put_bits(&s->pb, table0_dc_lum[code][1], table0_dc_lum[code][0]);
891
            } else {
892
                put_bits(&s->pb, table0_dc_chroma[code][1], table0_dc_chroma[code][0]);
893
            }
894 de6d9b64 Fabrice Bellard
        } else {
895 3825cd1d Michael Niedermayer
            if (n < 4) {
896
                put_bits(&s->pb, table1_dc_lum[code][1], table1_dc_lum[code][0]);
897
            } else {
898
                put_bits(&s->pb, table1_dc_chroma[code][1], table1_dc_chroma[code][0]);
899
            }
900
        }
901
            
902
        if (code == DC_MAX)
903
            put_bits(&s->pb, 8, level);
904
            
905
        if (level != 0) {
906
            put_bits(&s->pb, 1, sign);
907 de6d9b64 Fabrice Bellard
        }
908
    }
909
}
910
911
/* Encoding of a block. Very similar to MPEG4 except for a different
912
   escape coding (same as H263) and more vlc tables.
913
 */
914 f5957f3f Michael Niedermayer
static inline void msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n)
915 de6d9b64 Fabrice Bellard
{
916
    int level, run, last, i, j, last_index;
917
    int last_non_zero, sign, slevel;
918
    int code, run_diff, dc_pred_dir;
919
    const RLTable *rl;
920 0c1a9eda Zdenek Kabelac
    const uint8_t *scantable;
921 de6d9b64 Fabrice Bellard
922
    if (s->mb_intra) {
923
        set_stat(ST_DC);
924
        msmpeg4_encode_dc(s, block[0], n, &dc_pred_dir);
925
        i = 1;
926
        if (n < 4) {
927
            rl = &rl_table[s->rl_table_index];
928
        } else {
929
            rl = &rl_table[3 + s->rl_chroma_table_index];
930
        }
931
        run_diff = 0;
932 2ad1516a Michael Niedermayer
        scantable= s->intra_scantable.permutated;
933 de6d9b64 Fabrice Bellard
        set_stat(ST_INTRA_AC);
934
    } else {
935
        i = 0;
936
        rl = &rl_table[3 + s->rl_table_index];
937 287229e5 Michael Niedermayer
        if(s->msmpeg4_version<=2)
938 3825cd1d Michael Niedermayer
            run_diff = 0;
939
        else
940
            run_diff = 1;
941 2ad1516a Michael Niedermayer
        scantable= s->inter_scantable.permutated;
942 de6d9b64 Fabrice Bellard
        set_stat(ST_INTER_AC);
943
    }
944
945 f5957f3f Michael Niedermayer
    /* recalculate block_last_index for M$ wmv1 */
946 1457ab52 Michael Niedermayer
    if(s->msmpeg4_version>=4 && s->block_last_index[n]>0){
947 f5957f3f Michael Niedermayer
        for(last_index=63; last_index>=0; last_index--){
948
            if(block[scantable[last_index]]) break;
949
        }
950 4d2858de Michael Niedermayer
        s->block_last_index[n]= last_index;
951 f5957f3f Michael Niedermayer
    }else
952
        last_index = s->block_last_index[n];
953 de6d9b64 Fabrice Bellard
    /* AC coefs */
954
    last_non_zero = i - 1;
955
    for (; i <= last_index; i++) {
956 f5957f3f Michael Niedermayer
        j = scantable[i];
957 de6d9b64 Fabrice Bellard
        level = block[j];
958
        if (level) {
959
            run = i - last_non_zero - 1;
960
            last = (i == last_index);
961
            sign = 0;
962
            slevel = level;
963
            if (level < 0) {
964
                sign = 1;
965
                level = -level;
966
            }
967 62959862 Michael Niedermayer
968 f5957f3f Michael Niedermayer
            if(level<=MAX_LEVEL && run<=MAX_RUN){
969
                s->ac_stats[s->mb_intra][n>3][level][run][last]++;
970
            }
971
#if 0
972
else
973
    s->ac_stats[s->mb_intra][n>3][40][63][0]++; //esc3 like
974
#endif
975 de6d9b64 Fabrice Bellard
            code = get_rl_index(rl, last, run, level);
976
            put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
977
            if (code == rl->n) {
978
                int level1, run1;
979
980
                level1 = level - rl->max_level[last][run];
981
                if (level1 < 1) 
982
                    goto esc2;
983
                code = get_rl_index(rl, last, run, level1);
984
                if (code == rl->n) {
985
                esc2:
986
                    put_bits(&s->pb, 1, 0);
987
                    if (level > MAX_LEVEL)
988
                        goto esc3;
989
                    run1 = run - rl->max_run[last][level] - run_diff;
990
                    if (run1 < 0)
991
                        goto esc3;
992
                    code = get_rl_index(rl, last, run1, level);
993
                    if (code == rl->n) {
994
                    esc3:
995
                        /* third escape */
996
                        put_bits(&s->pb, 1, 0);
997
                        put_bits(&s->pb, 1, last);
998 1457ab52 Michael Niedermayer
                        if(s->msmpeg4_version>=4){
999 f5957f3f Michael Niedermayer
                            if(s->esc3_level_length==0){
1000
                                s->esc3_level_length=8;
1001
                                s->esc3_run_length= 6;
1002
                                if(s->qscale<8)
1003
                                    put_bits(&s->pb, 6, 3);
1004
                                else
1005
                                    put_bits(&s->pb, 8, 3);
1006
                            }
1007
                            put_bits(&s->pb, s->esc3_run_length, run);
1008
                            put_bits(&s->pb, 1, sign);
1009
                            put_bits(&s->pb, s->esc3_level_length, level);
1010
                        }else{
1011
                            put_bits(&s->pb, 6, run);
1012
                            put_bits(&s->pb, 8, slevel & 0xff);
1013
                        }
1014 de6d9b64 Fabrice Bellard
                    } else {
1015
                        /* second escape */
1016
                        put_bits(&s->pb, 1, 1);
1017
                        put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
1018
                        put_bits(&s->pb, 1, sign);
1019
                    }
1020
                } else {
1021
                    /* first escape */
1022
                    put_bits(&s->pb, 1, 1);
1023
                    put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
1024
                    put_bits(&s->pb, 1, sign);
1025
                }
1026
            } else {
1027
                put_bits(&s->pb, 1, sign);
1028
            }
1029
            last_non_zero = i;
1030
        }
1031
    }
1032
}
1033
1034 7604246d Wolfgang Hesseler
#endif //CONFIG_ENCODERS
1035
1036 de6d9b64 Fabrice Bellard
/****************************************/
1037
/* decoding stuff */
1038
1039 1457ab52 Michael Niedermayer
static VLC mb_non_intra_vlc[4];
1040 de6d9b64 Fabrice Bellard
static VLC mb_intra_vlc;
1041
static VLC dc_lum_vlc[2];
1042
static VLC dc_chroma_vlc[2];
1043 84afee34 Michael Niedermayer
static VLC v2_dc_lum_vlc;
1044
static VLC v2_dc_chroma_vlc;
1045
static VLC cbpy_vlc;
1046
static VLC v2_intra_cbpc_vlc;
1047
static VLC v2_mb_type_vlc;
1048
static VLC v2_mv_vlc;
1049 287229e5 Michael Niedermayer
static VLC v1_intra_cbpc_vlc;
1050
static VLC v1_inter_cbpc_vlc;
1051 de0f2f4c Michael Niedermayer
static VLC inter_intra_vlc;
1052 84afee34 Michael Niedermayer
1053
/* this table is practically identical to the one from h263 except that its inverted */
1054 20695ec9 Falk Hüffner
static void init_h263_dc_for_msmpeg4(void)
1055 84afee34 Michael Niedermayer
{
1056
        int level, uni_code, uni_len;
1057
1058 2ed627e6 Michael Niedermayer
        for(level=-256; level<256; level++){
1059 84afee34 Michael Niedermayer
            int size, v, l;
1060
            /* find number of bits */
1061
            size = 0;
1062
            v = abs(level);
1063
            while (v) {
1064
                v >>= 1;
1065
                    size++;
1066
            }
1067
1068
            if (level < 0)
1069
                l= (-level) ^ ((1 << size) - 1);
1070
            else
1071
                l= level;
1072
1073
            /* luminance h263 */
1074
            uni_code= DCtab_lum[size][0];
1075
            uni_len = DCtab_lum[size][1];
1076
            uni_code ^= (1<<uni_len)-1; //M$ doesnt like compatibility
1077
1078
            if (size > 0) {
1079
                uni_code<<=size; uni_code|=l;
1080
                uni_len+=size;
1081
                if (size > 8){
1082
                    uni_code<<=1; uni_code|=1;
1083
                    uni_len++;
1084
                }
1085
            }
1086
            v2_dc_lum_table[level+256][0]= uni_code;
1087
            v2_dc_lum_table[level+256][1]= uni_len;
1088
1089
            /* chrominance h263 */
1090
            uni_code= DCtab_chrom[size][0];
1091
            uni_len = DCtab_chrom[size][1];
1092
            uni_code ^= (1<<uni_len)-1; //M$ doesnt like compatibility
1093
            
1094
            if (size > 0) {
1095
                uni_code<<=size; uni_code|=l;
1096
                uni_len+=size;
1097
                if (size > 8){
1098
                    uni_code<<=1; uni_code|=1;
1099
                    uni_len++;
1100
                }
1101
            }
1102
            v2_dc_chroma_table[level+256][0]= uni_code;
1103
            v2_dc_chroma_table[level+256][1]= uni_len;
1104
1105
        }
1106
}
1107 de6d9b64 Fabrice Bellard
1108
/* init all vlc decoding tables */
1109 f5957f3f Michael Niedermayer
int ff_msmpeg4_decode_init(MpegEncContext *s)
1110 de6d9b64 Fabrice Bellard
{
1111 d81c5983 Fabrice Bellard
    static int done = 0;
1112 de6d9b64 Fabrice Bellard
    int i;
1113
    MVTable *mv;
1114
1115 f5957f3f Michael Niedermayer
    common_init(s);
1116 de6d9b64 Fabrice Bellard
1117 d81c5983 Fabrice Bellard
    if (!done) {
1118
        done = 1;
1119
1120
        for(i=0;i<NB_RL_TABLES;i++) {
1121
            init_rl(&rl_table[i]);
1122
            init_vlc_rl(&rl_table[i]);
1123
        }
1124
        for(i=0;i<2;i++) {
1125
            mv = &mv_tables[i];
1126 08dce7b0 Michael Niedermayer
            init_vlc(&mv->vlc, MV_VLC_BITS, mv->n + 1, 
1127 d81c5983 Fabrice Bellard
                     mv->table_mv_bits, 1, 1,
1128
                     mv->table_mv_code, 2, 2);
1129
        }
1130
1131 08dce7b0 Michael Niedermayer
        init_vlc(&dc_lum_vlc[0], DC_VLC_BITS, 120, 
1132 d81c5983 Fabrice Bellard
                 &table0_dc_lum[0][1], 8, 4,
1133
                 &table0_dc_lum[0][0], 8, 4);
1134 08dce7b0 Michael Niedermayer
        init_vlc(&dc_chroma_vlc[0], DC_VLC_BITS, 120, 
1135 d81c5983 Fabrice Bellard
                 &table0_dc_chroma[0][1], 8, 4,
1136
                 &table0_dc_chroma[0][0], 8, 4);
1137 08dce7b0 Michael Niedermayer
        init_vlc(&dc_lum_vlc[1], DC_VLC_BITS, 120, 
1138 d81c5983 Fabrice Bellard
                 &table1_dc_lum[0][1], 8, 4,
1139
                 &table1_dc_lum[0][0], 8, 4);
1140 08dce7b0 Michael Niedermayer
        init_vlc(&dc_chroma_vlc[1], DC_VLC_BITS, 120, 
1141 d81c5983 Fabrice Bellard
                 &table1_dc_chroma[0][1], 8, 4,
1142
                 &table1_dc_chroma[0][0], 8, 4);
1143 84afee34 Michael Niedermayer
    
1144 08dce7b0 Michael Niedermayer
        init_vlc(&v2_dc_lum_vlc, DC_VLC_BITS, 512, 
1145 d81c5983 Fabrice Bellard
                 &v2_dc_lum_table[0][1], 8, 4,
1146
                 &v2_dc_lum_table[0][0], 8, 4);
1147 08dce7b0 Michael Niedermayer
        init_vlc(&v2_dc_chroma_vlc, DC_VLC_BITS, 512, 
1148 d81c5983 Fabrice Bellard
                 &v2_dc_chroma_table[0][1], 8, 4,
1149
                 &v2_dc_chroma_table[0][0], 8, 4);
1150 84afee34 Michael Niedermayer
    
1151 08dce7b0 Michael Niedermayer
        init_vlc(&cbpy_vlc, CBPY_VLC_BITS, 16,
1152 d81c5983 Fabrice Bellard
                 &cbpy_tab[0][1], 2, 1,
1153
                 &cbpy_tab[0][0], 2, 1);
1154 08dce7b0 Michael Niedermayer
        init_vlc(&v2_intra_cbpc_vlc, V2_INTRA_CBPC_VLC_BITS, 4,
1155 d81c5983 Fabrice Bellard
                 &v2_intra_cbpc[0][1], 2, 1,
1156
                 &v2_intra_cbpc[0][0], 2, 1);
1157 08dce7b0 Michael Niedermayer
        init_vlc(&v2_mb_type_vlc, V2_MB_TYPE_VLC_BITS, 8,
1158 d81c5983 Fabrice Bellard
                 &v2_mb_type[0][1], 2, 1,
1159
                 &v2_mb_type[0][0], 2, 1);
1160 08dce7b0 Michael Niedermayer
        init_vlc(&v2_mv_vlc, V2_MV_VLC_BITS, 33,
1161 d81c5983 Fabrice Bellard
                 &mvtab[0][1], 2, 1,
1162
                 &mvtab[0][0], 2, 1);
1163
1164 1457ab52 Michael Niedermayer
        for(i=0; i<4; i++){
1165
            init_vlc(&mb_non_intra_vlc[i], MB_NON_INTRA_VLC_BITS, 128, 
1166
                     &wmv2_inter_table[i][0][1], 8, 4,
1167
                     &wmv2_inter_table[i][0][0], 8, 4); //FIXME name?
1168
        }
1169
        
1170 08dce7b0 Michael Niedermayer
        init_vlc(&mb_intra_vlc, MB_INTRA_VLC_BITS, 64, 
1171 d81c5983 Fabrice Bellard
                 &table_mb_intra[0][1], 4, 2,
1172
                 &table_mb_intra[0][0], 4, 2);
1173 287229e5 Michael Niedermayer
        
1174 08dce7b0 Michael Niedermayer
        init_vlc(&v1_intra_cbpc_vlc, V1_INTRA_CBPC_VLC_BITS, 8, 
1175 d81c5983 Fabrice Bellard
                 intra_MCBPC_bits, 1, 1,
1176
                 intra_MCBPC_code, 1, 1);
1177 08dce7b0 Michael Niedermayer
        init_vlc(&v1_inter_cbpc_vlc, V1_INTER_CBPC_VLC_BITS, 25, 
1178 d81c5983 Fabrice Bellard
                 inter_MCBPC_bits, 1, 1,
1179
                 inter_MCBPC_code, 1, 1);
1180 de0f2f4c Michael Niedermayer
        
1181 08dce7b0 Michael Niedermayer
        init_vlc(&inter_intra_vlc, INTER_INTRA_VLC_BITS, 4, 
1182 de0f2f4c Michael Niedermayer
                 &table_inter_intra[0][1], 2, 1,
1183
                 &table_inter_intra[0][0], 2, 1);
1184 d81c5983 Fabrice Bellard
    }
1185 4d2858de Michael Niedermayer
    
1186
    switch(s->msmpeg4_version){
1187
    case 1:
1188
    case 2:
1189
        s->decode_mb= msmpeg4v12_decode_mb;
1190
        break;
1191
    case 3:
1192
    case 4:
1193
        s->decode_mb= msmpeg4v34_decode_mb;
1194
        break;
1195 1457ab52 Michael Niedermayer
    case 5:
1196
        s->decode_mb= wmv2_decode_mb;
1197
        break;
1198 4d2858de Michael Niedermayer
    }
1199
    
1200 917f5827 Michael Niedermayer
    s->slice_height= s->mb_height; //to avoid 1/0 if the first frame isnt a keyframe
1201
    
1202 de6d9b64 Fabrice Bellard
    return 0;
1203
}
1204
1205
static int decode012(GetBitContext *gb)
1206
{
1207
    int n;
1208 612476ef Arpi
    n = get_bits1(gb);
1209 de6d9b64 Fabrice Bellard
    if (n == 0)
1210
        return 0;
1211
    else
1212 612476ef Arpi
        return get_bits1(gb) + 1;
1213 de6d9b64 Fabrice Bellard
}
1214
1215 84afee34 Michael Niedermayer
int msmpeg4_decode_picture_header(MpegEncContext * s)
1216 de6d9b64 Fabrice Bellard
{
1217 f5957f3f Michael Niedermayer
    int code;
1218 de6d9b64 Fabrice Bellard
1219 e1a9dbff Michael Niedermayer
#if 0
1220
{
1221
int i;
1222 68f593b4 Michael Niedermayer
for(i=0; i<s->gb.size_in_bits; i++)
1223 e1a9dbff Michael Niedermayer
    printf("%d", get_bits1(&s->gb));
1224
//    get_bits1(&s->gb);
1225
printf("END\n");
1226
return -1;
1227
}
1228
#endif
1229 287229e5 Michael Niedermayer
1230
    if(s->msmpeg4_version==1){
1231
        int start_code, num;
1232
        start_code = (get_bits(&s->gb, 16)<<16) | get_bits(&s->gb, 16);
1233
        if(start_code!=0x00000100){
1234
            fprintf(stderr, "invalid startcode\n");
1235
            return -1;
1236
        }
1237
1238
        num= get_bits(&s->gb, 5); // frame number */
1239
    }
1240
1241 de6d9b64 Fabrice Bellard
    s->pict_type = get_bits(&s->gb, 2) + 1;
1242
    if (s->pict_type != I_TYPE &&
1243 287229e5 Michael Niedermayer
        s->pict_type != P_TYPE){
1244
        fprintf(stderr, "invalid picture type\n");
1245 de6d9b64 Fabrice Bellard
        return -1;
1246 287229e5 Michael Niedermayer
    }
1247 de0f2f4c Michael Niedermayer
#if 0
1248
{
1249
    static int had_i=0;
1250
    if(s->pict_type == I_TYPE) had_i=1;
1251
    if(!had_i) return -1;
1252
}
1253
#endif
1254 de6d9b64 Fabrice Bellard
    s->qscale = get_bits(&s->gb, 5);
1255 ae2d2d6c Michael Niedermayer
    if(s->qscale==0){
1256
        fprintf(stderr, "invalid qscale\n");
1257
        return -1;
1258
    }
1259 de6d9b64 Fabrice Bellard
1260
    if (s->pict_type == I_TYPE) {
1261
        code = get_bits(&s->gb, 5); 
1262 287229e5 Michael Niedermayer
        if(s->msmpeg4_version==1){
1263
            if(code==0 || code>s->mb_height){
1264
                fprintf(stderr, "invalid slice height %d\n", code);
1265
                return -1;
1266
            }
1267
1268
            s->slice_height = code;
1269
        }else{
1270
            /* 0x17: one slice, 0x18: two slices, ... */
1271 de0f2f4c Michael Niedermayer
            if (code < 0x17){
1272
                fprintf(stderr, "error, slice code was %X\n", code);
1273 287229e5 Michael Niedermayer
                return -1;
1274 de0f2f4c Michael Niedermayer
            }
1275 287229e5 Michael Niedermayer
1276
            s->slice_height = s->mb_height / (code - 0x16);
1277
        }
1278 e1a9dbff Michael Niedermayer
1279
        switch(s->msmpeg4_version){
1280 287229e5 Michael Niedermayer
        case 1:
1281 e1a9dbff Michael Niedermayer
        case 2:
1282 84afee34 Michael Niedermayer
            s->rl_chroma_table_index = 2;
1283
            s->rl_table_index = 2;
1284 7f89b6fb Michael Niedermayer
1285 84afee34 Michael Niedermayer
            s->dc_table_index = 0; //not used
1286 e1a9dbff Michael Niedermayer
            break;
1287
        case 3:
1288 84afee34 Michael Niedermayer
            s->rl_chroma_table_index = decode012(&s->gb);
1289
            s->rl_table_index = decode012(&s->gb);
1290 7f89b6fb Michael Niedermayer
1291 84afee34 Michael Niedermayer
            s->dc_table_index = get_bits1(&s->gb);
1292 e1a9dbff Michael Niedermayer
            break;
1293
        case 4:
1294 f5957f3f Michael Niedermayer
            msmpeg4_decode_ext_header(s, (2+5+5+17+7)/8);
1295
1296 05174fd4 Michael Niedermayer
            if(s->bit_rate > MBAC_BITRATE) s->per_mb_rl_table= get_bits1(&s->gb);
1297
            else                           s->per_mb_rl_table= 0;
1298 de0f2f4c Michael Niedermayer
            
1299 f5957f3f Michael Niedermayer
            if(!s->per_mb_rl_table){
1300
                s->rl_chroma_table_index = decode012(&s->gb);
1301
                s->rl_table_index = decode012(&s->gb);
1302 e1a9dbff Michael Niedermayer
            }
1303
1304 f5957f3f Michael Niedermayer
            s->dc_table_index = get_bits1(&s->gb);
1305 de0f2f4c Michael Niedermayer
            s->inter_intra_pred= 0;
1306 e1a9dbff Michael Niedermayer
            break;
1307 84afee34 Michael Niedermayer
        }
1308 de6d9b64 Fabrice Bellard
        s->no_rounding = 1;
1309 de0f2f4c Michael Niedermayer
/*        printf("qscale:%d rlc:%d rl:%d dc:%d mbrl:%d slice:%d   \n", 
1310 badaf88e Michael Niedermayer
                s->qscale,
1311
                s->rl_chroma_table_index,
1312
                s->rl_table_index, 
1313 f5957f3f Michael Niedermayer
                s->dc_table_index,
1314 de0f2f4c Michael Niedermayer
                s->per_mb_rl_table,
1315
                s->slice_height);*/
1316 de6d9b64 Fabrice Bellard
    } else {
1317 287229e5 Michael Niedermayer
        switch(s->msmpeg4_version){
1318
        case 1:
1319
        case 2:
1320
            if(s->msmpeg4_version==1)
1321
                s->use_skip_mb_code = 1;
1322
            else
1323
                s->use_skip_mb_code = get_bits1(&s->gb);
1324 84afee34 Michael Niedermayer
            s->rl_table_index = 2;
1325
            s->rl_chroma_table_index = s->rl_table_index;
1326
            s->dc_table_index = 0; //not used
1327
            s->mv_table_index = 0;
1328 287229e5 Michael Niedermayer
            break;
1329
        case 3:
1330
            s->use_skip_mb_code = get_bits1(&s->gb);
1331 84afee34 Michael Niedermayer
            s->rl_table_index = decode012(&s->gb);
1332
            s->rl_chroma_table_index = s->rl_table_index;
1333 de6d9b64 Fabrice Bellard
1334 84afee34 Michael Niedermayer
            s->dc_table_index = get_bits1(&s->gb);
1335 de6d9b64 Fabrice Bellard
1336 84afee34 Michael Niedermayer
            s->mv_table_index = get_bits1(&s->gb);
1337 287229e5 Michael Niedermayer
            break;
1338 f5957f3f Michael Niedermayer
        case 4:
1339
            s->use_skip_mb_code = get_bits1(&s->gb);
1340 de0f2f4c Michael Niedermayer
1341 05174fd4 Michael Niedermayer
            if(s->bit_rate > MBAC_BITRATE) s->per_mb_rl_table= get_bits1(&s->gb);
1342
            else                           s->per_mb_rl_table= 0;
1343 de0f2f4c Michael Niedermayer
1344 f5957f3f Michael Niedermayer
            if(!s->per_mb_rl_table){
1345
                s->rl_table_index = decode012(&s->gb);
1346
                s->rl_chroma_table_index = s->rl_table_index;
1347
            }
1348
1349
            s->dc_table_index = get_bits1(&s->gb);
1350
1351
            s->mv_table_index = get_bits1(&s->gb);
1352 05174fd4 Michael Niedermayer
            s->inter_intra_pred= (s->width*s->height < 320*240 && s->bit_rate<=II_BITRATE);
1353 f5957f3f Michael Niedermayer
            break;
1354 84afee34 Michael Niedermayer
        }
1355 de0f2f4c Michael Niedermayer
/*        printf("skip:%d rl:%d rlc:%d dc:%d mv:%d mbrl:%d qp:%d   \n", 
1356 badaf88e Michael Niedermayer
                s->use_skip_mb_code, 
1357
                s->rl_table_index, 
1358
                s->rl_chroma_table_index, 
1359
                s->dc_table_index,
1360 f5957f3f Michael Niedermayer
                s->mv_table_index,
1361 de0f2f4c Michael Niedermayer
                s->per_mb_rl_table,
1362
                s->qscale);*/
1363 ae40484c Michael Niedermayer
        if(s->flipflop_rounding){
1364
            s->no_rounding ^= 1;
1365
        }else{
1366
            s->no_rounding = 0;
1367
        }
1368 de6d9b64 Fabrice Bellard
    }
1369 1457ab52 Michael Niedermayer
//printf("%d %d %d %d %d\n", s->pict_type, s->bit_rate, s->inter_intra_pred, s->width, s->height);
1370 f5957f3f Michael Niedermayer
1371
    s->esc3_level_length= 0;
1372
    s->esc3_run_length= 0;
1373 84afee34 Michael Niedermayer
1374 de6d9b64 Fabrice Bellard
#ifdef DEBUG
1375
    printf("*****frame %d:\n", frame_count++);
1376
#endif
1377
    return 0;
1378
}
1379
1380 ae40484c Michael Niedermayer
int msmpeg4_decode_ext_header(MpegEncContext * s, int buf_size)
1381
{
1382 287229e5 Michael Niedermayer
    int left= buf_size*8 - get_bits_count(&s->gb);
1383
    int length= s->msmpeg4_version>=3 ? 17 : 16;
1384 ae40484c Michael Niedermayer
    /* the alt_bitstream reader could read over the end so we need to check it */
1385 287229e5 Michael Niedermayer
    if(left>=length && left<length+8)
1386 ae40484c Michael Niedermayer
    {
1387 2b9ab1d5 Michael Niedermayer
        int fps;
1388
1389
        fps= get_bits(&s->gb, 5);
1390 05174fd4 Michael Niedermayer
        s->bit_rate= get_bits(&s->gb, 11)*1024;
1391 287229e5 Michael Niedermayer
        if(s->msmpeg4_version>=3)
1392
            s->flipflop_rounding= get_bits1(&s->gb);
1393
        else
1394
            s->flipflop_rounding= 0;
1395 2b9ab1d5 Michael Niedermayer
1396 05174fd4 Michael Niedermayer
//        printf("fps:%2d bps:%2d roundingType:%1d\n", fps, s->bit_rate/1024, s->flipflop_rounding);
1397 ae40484c Michael Niedermayer
    }
1398 287229e5 Michael Niedermayer
    else if(left<length+8)
1399 ae40484c Michael Niedermayer
    {
1400 2b9ab1d5 Michael Niedermayer
        s->flipflop_rounding= 0;
1401 287229e5 Michael Niedermayer
        printf("ext header missing, %d left\n", left);
1402
    }
1403
    else
1404
    {
1405
        fprintf(stderr, "I frame too long, ignoring ext header\n");
1406 ae40484c Michael Niedermayer
    }
1407 2b9ab1d5 Michael Niedermayer
1408 ae40484c Michael Niedermayer
    return 0;
1409
}
1410
1411 cd4af68a Zdenek Kabelac
static inline void msmpeg4_memsetw(short *tab, int val, int n)
1412 de6d9b64 Fabrice Bellard
{
1413
    int i;
1414
    for(i=0;i<n;i++)
1415
        tab[i] = val;
1416
}
1417
1418 7604246d Wolfgang Hesseler
#ifdef CONFIG_ENCODERS
1419
1420 3825cd1d Michael Niedermayer
static void msmpeg4v2_encode_motion(MpegEncContext * s, int val)
1421
{
1422
    int range, bit_size, sign, code, bits;
1423
1424
    if (val == 0) {
1425
        /* zero vector */
1426
        code = 0;
1427
        put_bits(&s->pb, mvtab[code][1], mvtab[code][0]);
1428
    } else {
1429
        bit_size = s->f_code - 1;
1430
        range = 1 << bit_size;
1431
        if (val <= -64)
1432
            val += 64;
1433
        else if (val >= 64)
1434
            val -= 64;
1435
1436
        if (val >= 0) {
1437
            sign = 0;
1438
        } else {
1439
            val = -val;
1440
            sign = 1;
1441
        }
1442
        val--;
1443
        code = (val >> bit_size) + 1;
1444
        bits = val & (range - 1);
1445
1446
        put_bits(&s->pb, mvtab[code][1] + 1, (mvtab[code][0] << 1) | sign); 
1447
        if (bit_size > 0) {
1448
            put_bits(&s->pb, bit_size, bits);
1449
        }
1450
    }
1451
}
1452
1453 7604246d Wolfgang Hesseler
#endif //CONFIG_ENCODERS
1454
1455 84afee34 Michael Niedermayer
/* this is identical to h263 except that its range is multiplied by 2 */
1456
static int msmpeg4v2_decode_motion(MpegEncContext * s, int pred, int f_code)
1457
{
1458
    int code, val, sign, shift;
1459
1460 08dce7b0 Michael Niedermayer
    code = get_vlc2(&s->gb, v2_mv_vlc.table, V2_MV_VLC_BITS, 2);
1461 287229e5 Michael Niedermayer
//     printf("MV code %d at %d %d pred: %d\n", code, s->mb_x,s->mb_y, pred);
1462 84afee34 Michael Niedermayer
    if (code < 0)
1463
        return 0xffff;
1464
1465
    if (code == 0)
1466
        return pred;
1467
    sign = get_bits1(&s->gb);
1468
    shift = f_code - 1;
1469
    val = (code - 1) << shift;
1470
    if (shift > 0)
1471
        val |= get_bits(&s->gb, shift);
1472
    val++;
1473
    if (sign)
1474
        val = -val;
1475
1476 287229e5 Michael Niedermayer
    val += pred;
1477 84afee34 Michael Niedermayer
    if (val <= -64)
1478
        val += 64;
1479
    else if (val >= 64)
1480
        val -= 64;
1481
1482
    return val;
1483
}
1484
1485 4d2858de Michael Niedermayer
static int msmpeg4v12_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
1486 84afee34 Michael Niedermayer
{
1487
    int cbp, code, i;
1488 4d2858de Michael Niedermayer
    
1489
    s->error_status_table[s->mb_x + s->mb_y*s->mb_width]= 0;
1490
    
1491 84afee34 Michael Niedermayer
    if (s->pict_type == P_TYPE) {
1492
        if (s->use_skip_mb_code) {
1493
            if (get_bits1(&s->gb)) {
1494
                /* skip mb */
1495
                s->mb_intra = 0;
1496
                for(i=0;i<6;i++)
1497
                    s->block_last_index[i] = -1;
1498
                s->mv_dir = MV_DIR_FORWARD;
1499
                s->mv_type = MV_TYPE_16X16;
1500
                s->mv[0][0][0] = 0;
1501
                s->mv[0][0][1] = 0;
1502
                s->mb_skiped = 1;
1503
                return 0;
1504
            }
1505
        }
1506
1507 287229e5 Michael Niedermayer
        if(s->msmpeg4_version==2)
1508 08dce7b0 Michael Niedermayer
            code = get_vlc2(&s->gb, v2_mb_type_vlc.table, V2_MB_TYPE_VLC_BITS, 1);
1509 287229e5 Michael Niedermayer
        else
1510 08dce7b0 Michael Niedermayer
            code = get_vlc2(&s->gb, v1_inter_cbpc_vlc.table, V1_INTER_CBPC_VLC_BITS, 3);
1511 287229e5 Michael Niedermayer
        if(code<0 || code>7){
1512
            fprintf(stderr, "cbpc %d invalid at %d %d\n", code, s->mb_x, s->mb_y);
1513
            return -1;
1514
        }
1515
1516 84afee34 Michael Niedermayer
        s->mb_intra = code >>2;
1517
    
1518
        cbp = code & 0x3;
1519
    } else {
1520
        s->mb_intra = 1;
1521 287229e5 Michael Niedermayer
        if(s->msmpeg4_version==2)
1522 08dce7b0 Michael Niedermayer
            cbp= get_vlc2(&s->gb, v2_intra_cbpc_vlc.table, V2_INTRA_CBPC_VLC_BITS, 1);
1523 287229e5 Michael Niedermayer
        else
1524 08dce7b0 Michael Niedermayer
            cbp= get_vlc2(&s->gb, v1_intra_cbpc_vlc.table, V1_INTRA_CBPC_VLC_BITS, 1);
1525 287229e5 Michael Niedermayer
        if(cbp<0 || cbp>3){
1526
            fprintf(stderr, "cbpc %d invalid at %d %d\n", cbp, s->mb_x, s->mb_y);
1527
            return -1;
1528
        }
1529 84afee34 Michael Niedermayer
    }
1530
1531
    if (!s->mb_intra) {
1532 287229e5 Michael Niedermayer
        int mx, my, cbpy;
1533
        
1534 08dce7b0 Michael Niedermayer
        cbpy= get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1);
1535 287229e5 Michael Niedermayer
        if(cbpy<0){
1536
            fprintf(stderr, "cbpy %d invalid at %d %d\n", cbp, s->mb_x, s->mb_y);
1537
            return -1;
1538
        }
1539 84afee34 Michael Niedermayer
1540 287229e5 Michael Niedermayer
        cbp|= cbpy<<2;
1541
        if(s->msmpeg4_version==1 || (cbp&3) != 3) cbp^= 0x3C;
1542 84afee34 Michael Niedermayer
        
1543
        h263_pred_motion(s, 0, &mx, &my);
1544
        mx= msmpeg4v2_decode_motion(s, mx, 1);
1545
        my= msmpeg4v2_decode_motion(s, my, 1);
1546
        
1547
        s->mv_dir = MV_DIR_FORWARD;
1548
        s->mv_type = MV_TYPE_16X16;
1549
        s->mv[0][0][0] = mx;
1550
        s->mv[0][0][1] = my;
1551
    } else {
1552 287229e5 Michael Niedermayer
        if(s->msmpeg4_version==2){
1553
            s->ac_pred = get_bits1(&s->gb);
1554 08dce7b0 Michael Niedermayer
            cbp|= get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1)<<2; //FIXME check errors
1555 287229e5 Michael Niedermayer
        } else{
1556
            s->ac_pred = 0;
1557 08dce7b0 Michael Niedermayer
            cbp|= get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1)<<2; //FIXME check errors
1558 287229e5 Michael Niedermayer
            if(s->pict_type==P_TYPE) cbp^=0x3C;
1559
        }
1560 84afee34 Michael Niedermayer
    }
1561
1562
    for (i = 0; i < 6; i++) {
1563 1457ab52 Michael Niedermayer
        if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, NULL) < 0)
1564 84afee34 Michael Niedermayer
        {
1565 287229e5 Michael Niedermayer
             fprintf(stderr,"\nerror while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
1566 84afee34 Michael Niedermayer
             return -1;
1567
        }
1568
    }
1569
    return 0;
1570
}
1571
1572 4d2858de Michael Niedermayer
static int msmpeg4v34_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
1573 de6d9b64 Fabrice Bellard
{
1574
    int cbp, code, i;
1575 0c1a9eda Zdenek Kabelac
    uint8_t *coded_val;
1576 de6d9b64 Fabrice Bellard
1577 bd5e1c72 Michael Niedermayer
#ifdef PRINT_MB
1578
if(s->mb_x==0){
1579
    printf("\n");
1580
    if(s->mb_y==0) printf("\n");
1581
}
1582
#endif
1583 de6d9b64 Fabrice Bellard
1584 4d2858de Michael Niedermayer
    s->error_status_table[s->mb_x + s->mb_y*s->mb_width]= 0;
1585 84afee34 Michael Niedermayer
    
1586 de6d9b64 Fabrice Bellard
    if (s->pict_type == P_TYPE) {
1587
        set_stat(ST_INTER_MB);
1588
        if (s->use_skip_mb_code) {
1589 612476ef Arpi
            if (get_bits1(&s->gb)) {
1590 de6d9b64 Fabrice Bellard
                /* skip mb */
1591
                s->mb_intra = 0;
1592
                for(i=0;i<6;i++)
1593
                    s->block_last_index[i] = -1;
1594
                s->mv_dir = MV_DIR_FORWARD;
1595
                s->mv_type = MV_TYPE_16X16;
1596
                s->mv[0][0][0] = 0;
1597
                s->mv[0][0][1] = 0;
1598 3bb4e23a Fabrice Bellard
                s->mb_skiped = 1;
1599 bd5e1c72 Michael Niedermayer
#ifdef PRINT_MB
1600
printf("S ");
1601
#endif
1602 de6d9b64 Fabrice Bellard
                return 0;
1603
            }
1604
        }
1605
        
1606 1457ab52 Michael Niedermayer
        code = get_vlc2(&s->gb, mb_non_intra_vlc[DEFAULT_INTER_INDEX].table, MB_NON_INTRA_VLC_BITS, 3);
1607 de6d9b64 Fabrice Bellard
        if (code < 0)
1608
            return -1;
1609 38d171eb Zdenek Kabelac
        //s->mb_intra = (code & 0x40) ? 0 : 1;
1610
        s->mb_intra = (~code & 0x40) >> 6;
1611 de6d9b64 Fabrice Bellard
            
1612
        cbp = code & 0x3f;
1613
    } else {
1614
        set_stat(ST_INTRA_MB);
1615
        s->mb_intra = 1;
1616 08dce7b0 Michael Niedermayer
        code = get_vlc2(&s->gb, mb_intra_vlc.table, MB_INTRA_VLC_BITS, 2);
1617 de6d9b64 Fabrice Bellard
        if (code < 0)
1618
            return -1;
1619
        /* predict coded block pattern */
1620
        cbp = 0;
1621
        for(i=0;i<6;i++) {
1622 38d171eb Zdenek Kabelac
            int val = ((code >> (5 - i)) & 1);
1623 de6d9b64 Fabrice Bellard
            if (i < 4) {
1624 38d171eb Zdenek Kabelac
                int pred = coded_block_pred(s, i, &coded_val);
1625 de6d9b64 Fabrice Bellard
                val = val ^ pred;
1626
                *coded_val = val;
1627
            }
1628
            cbp |= val << (5 - i);
1629
        }
1630
    }
1631
1632
    if (!s->mb_intra) {
1633
        int mx, my;
1634 f5957f3f Michael Niedermayer
//printf("P at %d %d\n", s->mb_x, s->mb_y);
1635
        if(s->per_mb_rl_table && cbp){
1636
            s->rl_table_index = decode012(&s->gb);
1637
            s->rl_chroma_table_index = s->rl_table_index;
1638
        }
1639 de6d9b64 Fabrice Bellard
        set_stat(ST_MV);
1640
        h263_pred_motion(s, 0, &mx, &my);
1641
        if (msmpeg4_decode_motion(s, &mx, &my) < 0)
1642
            return -1;
1643
        s->mv_dir = MV_DIR_FORWARD;
1644
        s->mv_type = MV_TYPE_16X16;
1645
        s->mv[0][0][0] = mx;
1646
        s->mv[0][0][1] = my;
1647 bd5e1c72 Michael Niedermayer
#ifdef PRINT_MB
1648
printf("P ");
1649
#endif
1650 de6d9b64 Fabrice Bellard
    } else {
1651 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));
1652 de6d9b64 Fabrice Bellard
        set_stat(ST_INTRA_MB);
1653 612476ef Arpi
        s->ac_pred = get_bits1(&s->gb);
1654 bd5e1c72 Michael Niedermayer
#ifdef PRINT_MB
1655
printf("%c", s->ac_pred ? 'A' : 'I');
1656
#endif
1657 de0f2f4c Michael Niedermayer
        if(s->inter_intra_pred){
1658 08dce7b0 Michael Niedermayer
            s->h263_aic_dir= get_vlc2(&s->gb, inter_intra_vlc.table, INTER_INTRA_VLC_BITS, 1);
1659 de0f2f4c Michael Niedermayer
//            printf("%d%d %d %d/", s->ac_pred, s->h263_aic_dir, s->mb_x, s->mb_y);
1660
        }
1661 f5957f3f Michael Niedermayer
        if(s->per_mb_rl_table && cbp){
1662
            s->rl_table_index = decode012(&s->gb);
1663
            s->rl_chroma_table_index = s->rl_table_index;
1664
        }
1665 de6d9b64 Fabrice Bellard
    }
1666
1667
    for (i = 0; i < 6; i++) {
1668 1457ab52 Michael Niedermayer
        if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, NULL) < 0)
1669 38d171eb Zdenek Kabelac
        {
1670 287229e5 Michael Niedermayer
            fprintf(stderr,"\nerror while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
1671
            return -1;
1672 38d171eb Zdenek Kabelac
        }
1673 de6d9b64 Fabrice Bellard
    }
1674 bd5e1c72 Michael Niedermayer
    
1675 de6d9b64 Fabrice Bellard
    return 0;
1676
}
1677 1a013246 Michael Niedermayer
//#define ERROR_DETAILS
1678 f5957f3f Michael Niedermayer
static inline int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
1679 1457ab52 Michael Niedermayer
                              int n, int coded, const uint8_t *scan_table)
1680 de6d9b64 Fabrice Bellard
{
1681 45a82edb Michael Niedermayer
    int level, i, last, run, run_diff;
1682 de6d9b64 Fabrice Bellard
    int dc_pred_dir;
1683
    RLTable *rl;
1684 45a82edb Michael Niedermayer
    RL_VLC_ELEM *rl_vlc;
1685 badaf88e Michael Niedermayer
    int qmul, qadd;
1686 de6d9b64 Fabrice Bellard
1687
    if (s->mb_intra) {
1688 badaf88e Michael Niedermayer
        qmul=1;
1689
        qadd=0;
1690
1691 de6d9b64 Fabrice Bellard
        /* DC coef */
1692
        set_stat(ST_DC);
1693
        level = msmpeg4_decode_dc(s, n, &dc_pred_dir);
1694 bd5e1c72 Michael Niedermayer
#ifdef PRINT_MB
1695
{
1696
    static int c;
1697
    if(n==0) c=0;
1698
    if(n==4) printf("%X", c);
1699
    c+= c +dc_pred_dir;
1700
}
1701
#endif
1702 287229e5 Michael Niedermayer
        if (level < 0){
1703 bd5e1c72 Michael Niedermayer
            fprintf(stderr, "dc overflow- block: %d qscale: %d//\n", n, s->qscale);
1704 de0f2f4c Michael Niedermayer
            if(s->inter_intra_pred) level=0;
1705
            else                    return -1;
1706 287229e5 Michael Niedermayer
        }
1707 de6d9b64 Fabrice Bellard
        if (n < 4) {
1708
            rl = &rl_table[s->rl_table_index];
1709 287229e5 Michael Niedermayer
            if(level > 256*s->y_dc_scale){
1710 bd5e1c72 Michael Niedermayer
                fprintf(stderr, "dc overflow+ L qscale: %d//\n", s->qscale);
1711 de0f2f4c Michael Niedermayer
                if(!s->inter_intra_pred) return -1;
1712 287229e5 Michael Niedermayer
            }
1713 de6d9b64 Fabrice Bellard
        } else {
1714
            rl = &rl_table[3 + s->rl_chroma_table_index];
1715 287229e5 Michael Niedermayer
            if(level > 256*s->c_dc_scale){
1716 bd5e1c72 Michael Niedermayer
                fprintf(stderr, "dc overflow+ C qscale: %d//\n", s->qscale);
1717 de0f2f4c Michael Niedermayer
                if(!s->inter_intra_pred) return -1;
1718 287229e5 Michael Niedermayer
            }
1719 de6d9b64 Fabrice Bellard
        }
1720 287229e5 Michael Niedermayer
        block[0] = level;
1721 badaf88e Michael Niedermayer
1722 de6d9b64 Fabrice Bellard
        run_diff = 0;
1723 45a82edb Michael Niedermayer
        i = 0;
1724 de6d9b64 Fabrice Bellard
        if (!coded) {
1725
            goto not_coded;
1726
        }
1727
        if (s->ac_pred) {
1728
            if (dc_pred_dir == 0) 
1729 2ad1516a Michael Niedermayer
                scan_table = s->intra_v_scantable.permutated; /* left */
1730 de6d9b64 Fabrice Bellard
            else
1731 2ad1516a Michael Niedermayer
                scan_table = s->intra_h_scantable.permutated; /* top */
1732 de6d9b64 Fabrice Bellard
        } else {
1733 2ad1516a Michael Niedermayer
            scan_table = s->intra_scantable.permutated;
1734 de6d9b64 Fabrice Bellard
        }
1735
        set_stat(ST_INTRA_AC);
1736 45a82edb Michael Niedermayer
        rl_vlc= rl->rl_vlc[0];
1737 de6d9b64 Fabrice Bellard
    } else {
1738 badaf88e Michael Niedermayer
        qmul = s->qscale << 1;
1739
        qadd = (s->qscale - 1) | 1;
1740 45a82edb Michael Niedermayer
        i = -1;
1741 de6d9b64 Fabrice Bellard
        rl = &rl_table[3 + s->rl_table_index];
1742 84afee34 Michael Niedermayer
1743
        if(s->msmpeg4_version==2)
1744
            run_diff = 0;
1745
        else
1746
            run_diff = 1;
1747
1748 de6d9b64 Fabrice Bellard
        if (!coded) {
1749 45a82edb Michael Niedermayer
            s->block_last_index[n] = i;
1750 de6d9b64 Fabrice Bellard
            return 0;
1751
        }
1752 1457ab52 Michael Niedermayer
        if(!scan_table)
1753
            scan_table = s->inter_scantable.permutated;
1754 de6d9b64 Fabrice Bellard
        set_stat(ST_INTER_AC);
1755 45a82edb Michael Niedermayer
        rl_vlc= rl->rl_vlc[s->qscale];
1756 de6d9b64 Fabrice Bellard
    }
1757 45a82edb Michael Niedermayer
  {
1758
    OPEN_READER(re, &s->gb);
1759 de6d9b64 Fabrice Bellard
    for(;;) {
1760 45a82edb Michael Niedermayer
        UPDATE_CACHE(re, &s->gb);
1761
        GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2);
1762
        if (level==0) {
1763
            int cache;
1764
            cache= GET_CACHE(re, &s->gb);
1765 de6d9b64 Fabrice Bellard
            /* escape */
1766 45a82edb Michael Niedermayer
            if (s->msmpeg4_version==1 || (cache&0x80000000)==0) {
1767
                if (s->msmpeg4_version==1 || (cache&0x40000000)==0) {
1768 de6d9b64 Fabrice Bellard
                    /* third escape */
1769 45a82edb Michael Niedermayer
                    if(s->msmpeg4_version!=1) LAST_SKIP_BITS(re, &s->gb, 2);
1770
                    UPDATE_CACHE(re, &s->gb);
1771 f5957f3f Michael Niedermayer
                    if(s->msmpeg4_version<=3){
1772 45a82edb Michael Niedermayer
                        last=  SHOW_UBITS(re, &s->gb, 1); SKIP_CACHE(re, &s->gb, 1);
1773
                        run=   SHOW_UBITS(re, &s->gb, 6); SKIP_CACHE(re, &s->gb, 6);
1774
                        level= SHOW_SBITS(re, &s->gb, 8); LAST_SKIP_CACHE(re, &s->gb, 8);
1775
                        SKIP_COUNTER(re, &s->gb, 1+6+8);
1776
                    }else{                        
1777 f5957f3f Michael Niedermayer
                        int sign;
1778 45a82edb Michael Niedermayer
                        last=  SHOW_UBITS(re, &s->gb, 1); SKIP_BITS(re, &s->gb, 1);
1779 f5957f3f Michael Niedermayer
                        if(!s->esc3_level_length){
1780
                            int ll;
1781
                            //printf("ESC-3 %X at %d %d\n", show_bits(&s->gb, 24), s->mb_x, s->mb_y);
1782
                            if(s->qscale<8){
1783 45a82edb Michael Niedermayer
                                ll= SHOW_UBITS(re, &s->gb, 3); SKIP_BITS(re, &s->gb, 3);
1784 f5957f3f Michael Niedermayer
                                if(ll==0){
1785 45a82edb Michael Niedermayer
                                    if(SHOW_UBITS(re, &s->gb, 1)) printf("cool a new vlc code ,contact the ffmpeg developers and upload the file\n");
1786
                                    SKIP_BITS(re, &s->gb, 1);
1787 f5957f3f Michael Niedermayer
                                    ll=8;
1788
                                }
1789
                            }else{
1790
                                ll=2;
1791 45a82edb Michael Niedermayer
                                while(ll<8 && SHOW_UBITS(re, &s->gb, 1)==0){
1792
                                    ll++;
1793
                                    SKIP_BITS(re, &s->gb, 1);
1794
                                }
1795 05174fd4 Michael Niedermayer
                                if(ll<8) SKIP_BITS(re, &s->gb, 1);
1796 f5957f3f Michael Niedermayer
                            }
1797
1798
                            s->esc3_level_length= ll;
1799 45a82edb Michael Niedermayer
                            s->esc3_run_length= SHOW_UBITS(re, &s->gb, 2) + 3; SKIP_BITS(re, &s->gb, 2);
1800 f5957f3f Michael Niedermayer
//printf("level length:%d, run length: %d\n", ll, s->esc3_run_length);
1801 05174fd4 Michael Niedermayer
                            UPDATE_CACHE(re, &s->gb);
1802 f5957f3f Michael Niedermayer
                        }
1803 45a82edb Michael Niedermayer
                        run=   SHOW_UBITS(re, &s->gb, s->esc3_run_length); 
1804
                        SKIP_BITS(re, &s->gb, s->esc3_run_length);
1805
                        
1806
                        sign=  SHOW_UBITS(re, &s->gb, 1); 
1807
                        SKIP_BITS(re, &s->gb, 1);
1808
                        
1809
                        level= SHOW_UBITS(re, &s->gb, s->esc3_level_length); 
1810
                        SKIP_BITS(re, &s->gb, s->esc3_level_length);
1811 f5957f3f Michael Niedermayer
                        if(sign) level= -level;
1812
                    }
1813
//printf("level: %d, run: %d at %d %d\n", level, run, s->mb_x, s->mb_y);
1814 287229e5 Michael Niedermayer
#if 0 // waste of time / this will detect very few errors
1815
                    {
1816
                        const int abs_level= ABS(level);
1817
                        const int run1= run - rl->max_run[last][abs_level] - run_diff;
1818
                        if(abs_level<=MAX_LEVEL && run<=MAX_RUN){
1819
                            if(abs_level <= rl->max_level[last][run]){
1820
                                fprintf(stderr, "illegal 3. esc, vlc encoding possible\n");
1821
                                return DECODING_AC_LOST;
1822
                            }
1823
                            if(abs_level <= rl->max_level[last][run]*2){
1824
                                fprintf(stderr, "illegal 3. esc, esc 1 encoding possible\n");
1825
                                return DECODING_AC_LOST;
1826
                            }
1827 f5957f3f Michael Niedermayer
                            if(run1>=0 && abs_level <= rl->max_level[last][run1]){
1828 287229e5 Michael Niedermayer
                                fprintf(stderr, "illegal 3. esc, esc 2 encoding possible\n");
1829
                                return DECODING_AC_LOST;
1830
                            }
1831
                        }
1832
                    }
1833
#endif
1834 38d171eb Zdenek Kabelac
                    //level = level * qmul + (level>0) * qadd - (level<=0) * qadd ;
1835
                    if (level>0) level= level * qmul + qadd;
1836 287229e5 Michael Niedermayer
                    else         level= level * qmul - qadd;
1837
#if 0 // waste of time too :(
1838
                    if(level>2048 || level<-2048){
1839
                        fprintf(stderr, "|level| overflow in 3. esc\n");
1840
                        return DECODING_AC_LOST;
1841
                    }
1842
#endif
1843 45a82edb Michael Niedermayer
                    i+= run + 1;
1844
                    if(last) i+=192;
1845 1a013246 Michael Niedermayer
#ifdef ERROR_DETAILS
1846
                if(run==66)
1847
                    fprintf(stderr, "illegal vlc code in ESC3 level=%d\n", level);
1848
                else if((i>62 && i<192) || i>192+63)
1849
                    fprintf(stderr, "run overflow in ESC3 i=%d run=%d level=%d\n", i, run, level);
1850
#endif
1851 de6d9b64 Fabrice Bellard
                } else {
1852
                    /* second escape */
1853 45a82edb Michael Niedermayer
#if MIN_CACHE_BITS < 23
1854
                    LAST_SKIP_BITS(re, &s->gb, 2);
1855
                    UPDATE_CACHE(re, &s->gb);
1856
#else
1857
                    SKIP_BITS(re, &s->gb, 2);
1858
#endif
1859
                    GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2);
1860
                    i+= run + rl->max_run[run>>7][level/qmul] + run_diff; //FIXME opt indexing
1861
                    level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1862
                    LAST_SKIP_BITS(re, &s->gb, 1);
1863 1a013246 Michael Niedermayer
#ifdef ERROR_DETAILS
1864
                if(run==66)
1865
                    fprintf(stderr, "illegal vlc code in ESC2 level=%d\n", level);
1866
                else if((i>62 && i<192) || i>192+63)
1867
                    fprintf(stderr, "run overflow in ESC2 i=%d run=%d level=%d\n", i, run, level);
1868
#endif
1869 de6d9b64 Fabrice Bellard
                }
1870
            } else {
1871
                /* first escape */
1872 45a82edb Michael Niedermayer
#if MIN_CACHE_BITS < 22
1873
                LAST_SKIP_BITS(re, &s->gb, 1);
1874
                UPDATE_CACHE(re, &s->gb);
1875
#else
1876
                SKIP_BITS(re, &s->gb, 1);
1877
#endif
1878
                GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2);
1879
                i+= run;
1880
                level = level + rl->max_level[run>>7][(run-1)&63] * qmul;//FIXME opt indexing
1881
                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1882
                LAST_SKIP_BITS(re, &s->gb, 1);
1883 1a013246 Michael Niedermayer
#ifdef ERROR_DETAILS
1884
                if(run==66)
1885
                    fprintf(stderr, "illegal vlc code in ESC1 level=%d\n", level);
1886
                else if((i>62 && i<192) || i>192+63)
1887
                    fprintf(stderr, "run overflow in ESC1 i=%d run=%d level=%d\n", i, run, level);
1888
#endif
1889 de6d9b64 Fabrice Bellard
            }
1890
        } else {
1891 45a82edb Michael Niedermayer
            i+= run;
1892
            level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1893
            LAST_SKIP_BITS(re, &s->gb, 1);
1894 1a013246 Michael Niedermayer
#ifdef ERROR_DETAILS
1895
                if(run==66)
1896
                    fprintf(stderr, "illegal vlc code level=%d\n", level);
1897
                else if((i>62 && i<192) || i>192+63)
1898
                    fprintf(stderr, "run overflow i=%d run=%d level=%d\n", i, run, level);
1899
#endif
1900 f5957f3f Michael Niedermayer
        }
1901 45a82edb Michael Niedermayer
        if (i > 62){
1902
            i-= 192;
1903
            if(i&(~63)){
1904 68f593b4 Michael Niedermayer
                const int left= s->gb.size_in_bits - get_bits_count(&s->gb);
1905 4d2858de Michael Niedermayer
                if(((i+192 == 64 && level/qmul==-1) || s->error_resilience<=1) && left>=0){
1906 1a013246 Michael Niedermayer
                    fprintf(stderr, "ignoring overflow at %d %d\n", s->mb_x, s->mb_y);
1907
                    break;
1908
                }else{
1909
                    fprintf(stderr, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
1910
                    return -1;
1911
                }
1912 45a82edb Michael Niedermayer
            }
1913 287229e5 Michael Niedermayer
1914 45a82edb Michael Niedermayer
            block[scan_table[i]] = level;
1915 de6d9b64 Fabrice Bellard
            break;
1916 45a82edb Michael Niedermayer
        }
1917
1918
        block[scan_table[i]] = level;
1919 de6d9b64 Fabrice Bellard
    }
1920 45a82edb Michael Niedermayer
    CLOSE_READER(re, &s->gb);
1921
  }
1922 de6d9b64 Fabrice Bellard
 not_coded:
1923
    if (s->mb_intra) {
1924
        mpeg4_pred_ac(s, block, n, dc_pred_dir);
1925
        if (s->ac_pred) {
1926 45a82edb Michael Niedermayer
            i = 63; /* XXX: not optimal */
1927 de6d9b64 Fabrice Bellard
        }
1928
    }
1929 1457ab52 Michael Niedermayer
    if(s->msmpeg4_version>=4 && i>0) i=63; //FIXME/XXX optimize
1930 45a82edb Michael Niedermayer
    s->block_last_index[n] = i;
1931 f5957f3f Michael Niedermayer
    
1932 de6d9b64 Fabrice Bellard
    return 0;
1933
}
1934
1935
static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr)
1936
{
1937
    int level, pred;
1938
1939 287229e5 Michael Niedermayer
    if(s->msmpeg4_version<=2){
1940 84afee34 Michael Niedermayer
        if (n < 4) {
1941 08dce7b0 Michael Niedermayer
            level = get_vlc2(&s->gb, v2_dc_lum_vlc.table, DC_VLC_BITS, 3);
1942 84afee34 Michael Niedermayer
        } else {
1943 08dce7b0 Michael Niedermayer
            level = get_vlc2(&s->gb, v2_dc_chroma_vlc.table, DC_VLC_BITS, 3);
1944 84afee34 Michael Niedermayer
        }
1945 2ed627e6 Michael Niedermayer
        if (level < 0) 
1946 84afee34 Michael Niedermayer
            return -1;
1947
        level-=256;
1948
    }else{  //FIXME optimize use unified tables & index
1949
        if (n < 4) {
1950 08dce7b0 Michael Niedermayer
            level = get_vlc2(&s->gb, dc_lum_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
1951 84afee34 Michael Niedermayer
        } else {
1952 08dce7b0 Michael Niedermayer
            level = get_vlc2(&s->gb, dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
1953 84afee34 Michael Niedermayer
        }
1954 287229e5 Michael Niedermayer
        if (level < 0){
1955
            fprintf(stderr, "illegal dc vlc\n");
1956 84afee34 Michael Niedermayer
            return -1;
1957 287229e5 Michael Niedermayer
        }
1958 84afee34 Michael Niedermayer
1959
        if (level == DC_MAX) {
1960
            level = get_bits(&s->gb, 8);
1961
            if (get_bits1(&s->gb))
1962
                level = -level;
1963
        } else if (level != 0) {
1964
            if (get_bits1(&s->gb))
1965
                level = -level;
1966
        }
1967 de6d9b64 Fabrice Bellard
    }
1968
1969 287229e5 Michael Niedermayer
    if(s->msmpeg4_version==1){
1970 0c1a9eda Zdenek Kabelac
        int32_t *dc_val;
1971 287229e5 Michael Niedermayer
        pred = msmpeg4v1_pred_dc(s, n, &dc_val);
1972
        level += pred;
1973
        
1974
        /* update predictor */
1975
        *dc_val= level;
1976
    }else{
1977 0c1a9eda Zdenek Kabelac
        uint16_t *dc_val;
1978 bd5e1c72 Michael Niedermayer
        pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr);
1979 287229e5 Michael Niedermayer
        level += pred;
1980 de6d9b64 Fabrice Bellard
1981 287229e5 Michael Niedermayer
        /* update predictor */
1982
        if (n < 4) {
1983
            *dc_val = level * s->y_dc_scale;
1984
        } else {
1985
            *dc_val = level * s->c_dc_scale;
1986
        }
1987 de6d9b64 Fabrice Bellard
    }
1988
1989
    return level;
1990
}
1991
1992
static int msmpeg4_decode_motion(MpegEncContext * s, 
1993
                                 int *mx_ptr, int *my_ptr)
1994
{
1995
    MVTable *mv;
1996
    int code, mx, my;
1997
1998
    mv = &mv_tables[s->mv_table_index];
1999
2000 08dce7b0 Michael Niedermayer
    code = get_vlc2(&s->gb, mv->vlc.table, MV_VLC_BITS, 2);
2001 f5957f3f Michael Niedermayer
    if (code < 0){
2002
        fprintf(stderr, "illegal MV code at %d %d\n", s->mb_x, s->mb_y);
2003 de6d9b64 Fabrice Bellard
        return -1;
2004 f5957f3f Michael Niedermayer
    }
2005 de6d9b64 Fabrice Bellard
    if (code == mv->n) {
2006 f5957f3f Michael Niedermayer
//printf("MV ESC %X at %d %d\n", show_bits(&s->gb, 24), s->mb_x, s->mb_y);
2007 de6d9b64 Fabrice Bellard
        mx = get_bits(&s->gb, 6);
2008
        my = get_bits(&s->gb, 6);
2009
    } else {
2010
        mx = mv->table_mvx[code];
2011
        my = mv->table_mvy[code];
2012
    }
2013
2014
    mx += *mx_ptr - 32;
2015
    my += *my_ptr - 32;
2016
    /* WARNING : they do not do exactly modulo encoding */
2017
    if (mx <= -64)
2018
        mx += 64;
2019
    else if (mx >= 64)
2020
        mx -= 64;
2021
2022
    if (my <= -64)
2023
        my += 64;
2024
    else if (my >= 64)
2025
        my -= 64;
2026
    *mx_ptr = mx;
2027
    *my_ptr = my;
2028
    return 0;
2029
}
2030 1457ab52 Michael Niedermayer
2031
/* cleanest way to support it
2032
 * there is too much shared between versions so that we cant have 1 file per version & 1 common
2033
 * as allmost everything would be in the common file 
2034
 */
2035
#include "wmv2.c"