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