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