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