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