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