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