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