Merge commit '136034d86b5cb1819a2c3e6ecdfeb05dcba7140d'
[ffmpeg.git] / libavcodec / msmpeg4dec.c
1 /*
2  * MSMPEG4 backend for encoder and decoder
3  * Copyright (c) 2001 Fabrice Bellard
4  * Copyright (c) 2002-2013 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 #include "avcodec.h"
26 #include "dsputil.h"
27 #include "mpegvideo.h"
28 #include "msmpeg4.h"
29 #include "libavutil/imgutils.h"
30 #include "libavutil/x86/asm.h"
31 #include "h263.h"
32 #include "mpeg4video.h"
33 #include "msmpeg4data.h"
34 #include "vc1data.h"
35
36 #define DC_VLC_BITS 9
37 #define V2_INTRA_CBPC_VLC_BITS 3
38 #define V2_MB_TYPE_VLC_BITS 7
39 #define MV_VLC_BITS 9
40 #define V2_MV_VLC_BITS 9
41 #define TEX_VLC_BITS 9
42
43 #define DEFAULT_INTER_INDEX 3
44
45 static inline int msmpeg4v1_pred_dc(MpegEncContext * s, int n,
46                                     int32_t **dc_val_ptr)
47 {
48     int i;
49
50     if (n < 4) {
51         i= 0;
52     } else {
53         i= n-3;
54     }
55
56     *dc_val_ptr= &s->last_dc[i];
57     return s->last_dc[i];
58 }
59
60 /****************************************/
61 /* decoding stuff */
62
63 VLC ff_mb_non_intra_vlc[4];
64 static VLC v2_dc_lum_vlc;
65 static VLC v2_dc_chroma_vlc;
66 static VLC v2_intra_cbpc_vlc;
67 static VLC v2_mb_type_vlc;
68 static VLC v2_mv_vlc;
69 VLC ff_inter_intra_vlc;
70
71 /* This is identical to h263 except that its range is multiplied by 2. */
72 static int msmpeg4v2_decode_motion(MpegEncContext * s, int pred, int f_code)
73 {
74     int code, val, sign, shift;
75
76     code = get_vlc2(&s->gb, v2_mv_vlc.table, V2_MV_VLC_BITS, 2);
77     av_dlog(s, "MV code %d at %d %d pred: %d\n", code, s->mb_x,s->mb_y, pred);
78     if (code < 0)
79         return 0xffff;
80
81     if (code == 0)
82         return pred;
83     sign = get_bits1(&s->gb);
84     shift = f_code - 1;
85     val = code;
86     if (shift) {
87         val = (val - 1) << shift;
88         val |= get_bits(&s->gb, shift);
89         val++;
90     }
91     if (sign)
92         val = -val;
93
94     val += pred;
95     if (val <= -64)
96         val += 64;
97     else if (val >= 64)
98         val -= 64;
99
100     return val;
101 }
102
103 static int msmpeg4v12_decode_mb(MpegEncContext *s, int16_t block[6][64])
104 {
105     int cbp, code, i;
106     uint32_t * const mb_type_ptr = &s->current_picture.mb_type[s->mb_x + s->mb_y*s->mb_stride];
107
108     if (s->pict_type == AV_PICTURE_TYPE_P) {
109         if (s->use_skip_mb_code) {
110             if (get_bits1(&s->gb)) {
111                 /* skip mb */
112                 s->mb_intra = 0;
113                 for(i=0;i<6;i++)
114                     s->block_last_index[i] = -1;
115                 s->mv_dir = MV_DIR_FORWARD;
116                 s->mv_type = MV_TYPE_16X16;
117                 s->mv[0][0][0] = 0;
118                 s->mv[0][0][1] = 0;
119                 s->mb_skipped = 1;
120                 *mb_type_ptr = MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
121                 return 0;
122             }
123         }
124
125         if(s->msmpeg4_version==2)
126             code = get_vlc2(&s->gb, v2_mb_type_vlc.table, V2_MB_TYPE_VLC_BITS, 1);
127         else
128             code = get_vlc2(&s->gb, ff_h263_inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
129         if(code<0 || code>7){
130             av_log(s->avctx, AV_LOG_ERROR, "cbpc %d invalid at %d %d\n", code, s->mb_x, s->mb_y);
131             return -1;
132         }
133
134         s->mb_intra = code >>2;
135
136         cbp = code & 0x3;
137     } else {
138         s->mb_intra = 1;
139         if(s->msmpeg4_version==2)
140             cbp= get_vlc2(&s->gb, v2_intra_cbpc_vlc.table, V2_INTRA_CBPC_VLC_BITS, 1);
141         else
142             cbp= get_vlc2(&s->gb, ff_h263_intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 1);
143         if(cbp<0 || cbp>3){
144             av_log(s->avctx, AV_LOG_ERROR, "cbpc %d invalid at %d %d\n", cbp, s->mb_x, s->mb_y);
145             return -1;
146         }
147     }
148
149     if (!s->mb_intra) {
150         int mx, my, cbpy;
151
152         cbpy= get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
153         if(cbpy<0){
154             av_log(s->avctx, AV_LOG_ERROR, "cbpy %d invalid at %d %d\n", cbp, s->mb_x, s->mb_y);
155             return -1;
156         }
157
158         cbp|= cbpy<<2;
159         if(s->msmpeg4_version==1 || (cbp&3) != 3) cbp^= 0x3C;
160
161         ff_h263_pred_motion(s, 0, 0, &mx, &my);
162         mx= msmpeg4v2_decode_motion(s, mx, 1);
163         my= msmpeg4v2_decode_motion(s, my, 1);
164
165         s->mv_dir = MV_DIR_FORWARD;
166         s->mv_type = MV_TYPE_16X16;
167         s->mv[0][0][0] = mx;
168         s->mv[0][0][1] = my;
169         *mb_type_ptr = MB_TYPE_L0 | MB_TYPE_16x16;
170     } else {
171         if(s->msmpeg4_version==2){
172             s->ac_pred = get_bits1(&s->gb);
173             cbp|= get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1)<<2; //FIXME check errors
174         } else{
175             s->ac_pred = 0;
176             cbp|= get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1)<<2; //FIXME check errors
177             if(s->pict_type==AV_PICTURE_TYPE_P) cbp^=0x3C;
178         }
179         *mb_type_ptr = MB_TYPE_INTRA;
180     }
181
182     s->dsp.clear_blocks(s->block[0]);
183     for (i = 0; i < 6; i++) {
184         if (ff_msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, NULL) < 0)
185         {
186              av_log(s->avctx, AV_LOG_ERROR, "\nerror while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
187              return -1;
188         }
189     }
190     return 0;
191 }
192
193 static int msmpeg4v34_decode_mb(MpegEncContext *s, int16_t block[6][64])
194 {
195     int cbp, code, i;
196     uint8_t *coded_val;
197     uint32_t * const mb_type_ptr = &s->current_picture.mb_type[s->mb_x + s->mb_y*s->mb_stride];
198
199     if (s->pict_type == AV_PICTURE_TYPE_P) {
200         if (s->use_skip_mb_code) {
201             if (get_bits1(&s->gb)) {
202                 /* skip mb */
203                 s->mb_intra = 0;
204                 for(i=0;i<6;i++)
205                     s->block_last_index[i] = -1;
206                 s->mv_dir = MV_DIR_FORWARD;
207                 s->mv_type = MV_TYPE_16X16;
208                 s->mv[0][0][0] = 0;
209                 s->mv[0][0][1] = 0;
210                 s->mb_skipped = 1;
211                 *mb_type_ptr = MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
212
213                 return 0;
214             }
215         }
216
217         code = get_vlc2(&s->gb, ff_mb_non_intra_vlc[DEFAULT_INTER_INDEX].table, MB_NON_INTRA_VLC_BITS, 3);
218         if (code < 0)
219             return -1;
220         //s->mb_intra = (code & 0x40) ? 0 : 1;
221         s->mb_intra = (~code & 0x40) >> 6;
222
223         cbp = code & 0x3f;
224     } else {
225         s->mb_intra = 1;
226         code = get_vlc2(&s->gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
227         if (code < 0)
228             return -1;
229         /* predict coded block pattern */
230         cbp = 0;
231         for(i=0;i<6;i++) {
232             int val = ((code >> (5 - i)) & 1);
233             if (i < 4) {
234                 int pred = ff_msmpeg4_coded_block_pred(s, i, &coded_val);
235                 val = val ^ pred;
236                 *coded_val = val;
237             }
238             cbp |= val << (5 - i);
239         }
240     }
241
242     if (!s->mb_intra) {
243         int mx, my;
244         if(s->per_mb_rl_table && cbp){
245             s->rl_table_index = decode012(&s->gb);
246             s->rl_chroma_table_index = s->rl_table_index;
247         }
248         ff_h263_pred_motion(s, 0, 0, &mx, &my);
249         if (ff_msmpeg4_decode_motion(s, &mx, &my) < 0)
250             return -1;
251         s->mv_dir = MV_DIR_FORWARD;
252         s->mv_type = MV_TYPE_16X16;
253         s->mv[0][0][0] = mx;
254         s->mv[0][0][1] = my;
255         *mb_type_ptr = MB_TYPE_L0 | MB_TYPE_16x16;
256     } else {
257         av_dlog(s, "I at %d %d %d %06X\n", s->mb_x, s->mb_y,
258                 ((cbp & 3) ? 1 : 0) +((cbp & 0x3C)? 2 : 0),
259                 show_bits(&s->gb, 24));
260         s->ac_pred = get_bits1(&s->gb);
261         *mb_type_ptr = MB_TYPE_INTRA;
262         if(s->inter_intra_pred){
263             s->h263_aic_dir= get_vlc2(&s->gb, ff_inter_intra_vlc.table, INTER_INTRA_VLC_BITS, 1);
264             av_dlog(s, "%d%d %d %d/",
265                     s->ac_pred, s->h263_aic_dir, s->mb_x, s->mb_y);
266         }
267         if(s->per_mb_rl_table && cbp){
268             s->rl_table_index = decode012(&s->gb);
269             s->rl_chroma_table_index = s->rl_table_index;
270         }
271     }
272
273     s->dsp.clear_blocks(s->block[0]);
274     for (i = 0; i < 6; i++) {
275         if (ff_msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, NULL) < 0)
276         {
277             av_log(s->avctx, AV_LOG_ERROR, "\nerror while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
278             return -1;
279         }
280     }
281
282     return 0;
283 }
284
285 /* init all vlc decoding tables */
286 av_cold int ff_msmpeg4_decode_init(AVCodecContext *avctx)
287 {
288     MpegEncContext *s = avctx->priv_data;
289     static volatile int done = 0;
290     int i, ret;
291     MVTable *mv;
292
293     if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
294         return ret;
295
296     if (ff_h263_decode_init(avctx) < 0)
297         return -1;
298
299     ff_msmpeg4_common_init(s);
300
301     if (!done) {
302         for(i=0;i<NB_RL_TABLES;i++) {
303             ff_init_rl(&ff_rl_table[i], ff_static_rl_table_store[i]);
304         }
305         INIT_VLC_RL(ff_rl_table[0], 642);
306         INIT_VLC_RL(ff_rl_table[1], 1104);
307         INIT_VLC_RL(ff_rl_table[2], 554);
308         INIT_VLC_RL(ff_rl_table[3], 940);
309         INIT_VLC_RL(ff_rl_table[4], 962);
310         INIT_VLC_RL(ff_rl_table[5], 554);
311
312         mv = &ff_mv_tables[0];
313         INIT_VLC_STATIC(&mv->vlc, MV_VLC_BITS, mv->n + 1,
314                     mv->table_mv_bits, 1, 1,
315                     mv->table_mv_code, 2, 2, 3714);
316         mv = &ff_mv_tables[1];
317         INIT_VLC_STATIC(&mv->vlc, MV_VLC_BITS, mv->n + 1,
318                     mv->table_mv_bits, 1, 1,
319                     mv->table_mv_code, 2, 2, 2694);
320
321         INIT_VLC_STATIC(&ff_msmp4_dc_luma_vlc[0], DC_VLC_BITS, 120,
322                  &ff_table0_dc_lum[0][1], 8, 4,
323                  &ff_table0_dc_lum[0][0], 8, 4, 1158);
324         INIT_VLC_STATIC(&ff_msmp4_dc_chroma_vlc[0], DC_VLC_BITS, 120,
325                  &ff_table0_dc_chroma[0][1], 8, 4,
326                  &ff_table0_dc_chroma[0][0], 8, 4, 1118);
327         INIT_VLC_STATIC(&ff_msmp4_dc_luma_vlc[1], DC_VLC_BITS, 120,
328                  &ff_table1_dc_lum[0][1], 8, 4,
329                  &ff_table1_dc_lum[0][0], 8, 4, 1476);
330         INIT_VLC_STATIC(&ff_msmp4_dc_chroma_vlc[1], DC_VLC_BITS, 120,
331                  &ff_table1_dc_chroma[0][1], 8, 4,
332                  &ff_table1_dc_chroma[0][0], 8, 4, 1216);
333
334         INIT_VLC_STATIC(&v2_dc_lum_vlc, DC_VLC_BITS, 512,
335                  &ff_v2_dc_lum_table[0][1], 8, 4,
336                  &ff_v2_dc_lum_table[0][0], 8, 4, 1472);
337         INIT_VLC_STATIC(&v2_dc_chroma_vlc, DC_VLC_BITS, 512,
338                  &ff_v2_dc_chroma_table[0][1], 8, 4,
339                  &ff_v2_dc_chroma_table[0][0], 8, 4, 1506);
340
341         INIT_VLC_STATIC(&v2_intra_cbpc_vlc, V2_INTRA_CBPC_VLC_BITS, 4,
342                  &ff_v2_intra_cbpc[0][1], 2, 1,
343                  &ff_v2_intra_cbpc[0][0], 2, 1, 8);
344         INIT_VLC_STATIC(&v2_mb_type_vlc, V2_MB_TYPE_VLC_BITS, 8,
345                  &ff_v2_mb_type[0][1], 2, 1,
346                  &ff_v2_mb_type[0][0], 2, 1, 128);
347         INIT_VLC_STATIC(&v2_mv_vlc, V2_MV_VLC_BITS, 33,
348                  &ff_mvtab[0][1], 2, 1,
349                  &ff_mvtab[0][0], 2, 1, 538);
350
351         INIT_VLC_STATIC(&ff_mb_non_intra_vlc[0], MB_NON_INTRA_VLC_BITS, 128,
352                      &ff_wmv2_inter_table[0][0][1], 8, 4,
353                      &ff_wmv2_inter_table[0][0][0], 8, 4, 1636);
354         INIT_VLC_STATIC(&ff_mb_non_intra_vlc[1], MB_NON_INTRA_VLC_BITS, 128,
355                      &ff_wmv2_inter_table[1][0][1], 8, 4,
356                      &ff_wmv2_inter_table[1][0][0], 8, 4, 2648);
357         INIT_VLC_STATIC(&ff_mb_non_intra_vlc[2], MB_NON_INTRA_VLC_BITS, 128,
358                      &ff_wmv2_inter_table[2][0][1], 8, 4,
359                      &ff_wmv2_inter_table[2][0][0], 8, 4, 1532);
360         INIT_VLC_STATIC(&ff_mb_non_intra_vlc[3], MB_NON_INTRA_VLC_BITS, 128,
361                      &ff_wmv2_inter_table[3][0][1], 8, 4,
362                      &ff_wmv2_inter_table[3][0][0], 8, 4, 2488);
363
364         INIT_VLC_STATIC(&ff_msmp4_mb_i_vlc, MB_INTRA_VLC_BITS, 64,
365                  &ff_msmp4_mb_i_table[0][1], 4, 2,
366                  &ff_msmp4_mb_i_table[0][0], 4, 2, 536);
367
368         INIT_VLC_STATIC(&ff_inter_intra_vlc, INTER_INTRA_VLC_BITS, 4,
369                  &ff_table_inter_intra[0][1], 2, 1,
370                  &ff_table_inter_intra[0][0], 2, 1, 8);
371         done = 1;
372     }
373
374     switch(s->msmpeg4_version){
375     case 1:
376     case 2:
377         s->decode_mb= msmpeg4v12_decode_mb;
378         break;
379     case 3:
380     case 4:
381         s->decode_mb= msmpeg4v34_decode_mb;
382         break;
383     case 5:
384         if (CONFIG_WMV2_DECODER)
385             s->decode_mb= ff_wmv2_decode_mb;
386     case 6:
387         //FIXME + TODO VC1 decode mb
388         break;
389     }
390
391     s->slice_height= s->mb_height; //to avoid 1/0 if the first frame is not a keyframe
392
393     return 0;
394 }
395
396 int ff_msmpeg4_decode_picture_header(MpegEncContext * s)
397 {
398     int code;
399
400     if(s->msmpeg4_version==1){
401         int start_code = get_bits_long(&s->gb, 32);
402         if(start_code!=0x00000100){
403             av_log(s->avctx, AV_LOG_ERROR, "invalid startcode\n");
404             return -1;
405         }
406
407         skip_bits(&s->gb, 5); // frame number */
408     }
409
410     s->pict_type = get_bits(&s->gb, 2) + 1;
411     if (s->pict_type != AV_PICTURE_TYPE_I &&
412         s->pict_type != AV_PICTURE_TYPE_P){
413         av_log(s->avctx, AV_LOG_ERROR, "invalid picture type\n");
414         return -1;
415     }
416 #if 0
417 {
418     static int had_i=0;
419     if(s->pict_type == AV_PICTURE_TYPE_I) had_i=1;
420     if(!had_i) return -1;
421 }
422 #endif
423     s->chroma_qscale= s->qscale = get_bits(&s->gb, 5);
424     if(s->qscale==0){
425         av_log(s->avctx, AV_LOG_ERROR, "invalid qscale\n");
426         return -1;
427     }
428
429     if (s->pict_type == AV_PICTURE_TYPE_I) {
430         code = get_bits(&s->gb, 5);
431         if(s->msmpeg4_version==1){
432             if(code==0 || code>s->mb_height){
433                 av_log(s->avctx, AV_LOG_ERROR, "invalid slice height %d\n", code);
434                 return -1;
435             }
436
437             s->slice_height = code;
438         }else{
439             /* 0x17: one slice, 0x18: two slices, ... */
440             if (code < 0x17){
441                 av_log(s->avctx, AV_LOG_ERROR, "error, slice code was %X\n", code);
442                 return -1;
443             }
444
445             s->slice_height = s->mb_height / (code - 0x16);
446         }
447
448         switch(s->msmpeg4_version){
449         case 1:
450         case 2:
451             s->rl_chroma_table_index = 2;
452             s->rl_table_index = 2;
453
454             s->dc_table_index = 0; //not used
455             break;
456         case 3:
457             s->rl_chroma_table_index = decode012(&s->gb);
458             s->rl_table_index = decode012(&s->gb);
459
460             s->dc_table_index = get_bits1(&s->gb);
461             break;
462         case 4:
463             ff_msmpeg4_decode_ext_header(s, (2+5+5+17+7)/8);
464
465             if(s->bit_rate > MBAC_BITRATE) s->per_mb_rl_table= get_bits1(&s->gb);
466             else                           s->per_mb_rl_table= 0;
467
468             if(!s->per_mb_rl_table){
469                 s->rl_chroma_table_index = decode012(&s->gb);
470                 s->rl_table_index = decode012(&s->gb);
471             }
472
473             s->dc_table_index = get_bits1(&s->gb);
474             s->inter_intra_pred= 0;
475             break;
476         }
477         s->no_rounding = 1;
478         if(s->avctx->debug&FF_DEBUG_PICT_INFO)
479             av_log(s->avctx, AV_LOG_DEBUG, "qscale:%d rlc:%d rl:%d dc:%d mbrl:%d slice:%d   \n",
480                 s->qscale,
481                 s->rl_chroma_table_index,
482                 s->rl_table_index,
483                 s->dc_table_index,
484                 s->per_mb_rl_table,
485                 s->slice_height);
486     } else {
487         switch(s->msmpeg4_version){
488         case 1:
489         case 2:
490             if(s->msmpeg4_version==1)
491                 s->use_skip_mb_code = 1;
492             else
493                 s->use_skip_mb_code = get_bits1(&s->gb);
494             s->rl_table_index = 2;
495             s->rl_chroma_table_index = s->rl_table_index;
496             s->dc_table_index = 0; //not used
497             s->mv_table_index = 0;
498             break;
499         case 3:
500             s->use_skip_mb_code = get_bits1(&s->gb);
501             s->rl_table_index = decode012(&s->gb);
502             s->rl_chroma_table_index = s->rl_table_index;
503
504             s->dc_table_index = get_bits1(&s->gb);
505
506             s->mv_table_index = get_bits1(&s->gb);
507             break;
508         case 4:
509             s->use_skip_mb_code = get_bits1(&s->gb);
510
511             if(s->bit_rate > MBAC_BITRATE) s->per_mb_rl_table= get_bits1(&s->gb);
512             else                           s->per_mb_rl_table= 0;
513
514             if(!s->per_mb_rl_table){
515                 s->rl_table_index = decode012(&s->gb);
516                 s->rl_chroma_table_index = s->rl_table_index;
517             }
518
519             s->dc_table_index = get_bits1(&s->gb);
520
521             s->mv_table_index = get_bits1(&s->gb);
522             s->inter_intra_pred= (s->width*s->height < 320*240 && s->bit_rate<=II_BITRATE);
523             break;
524         }
525
526         if(s->avctx->debug&FF_DEBUG_PICT_INFO)
527             av_log(s->avctx, AV_LOG_DEBUG, "skip:%d rl:%d rlc:%d dc:%d mv:%d mbrl:%d qp:%d   \n",
528                 s->use_skip_mb_code,
529                 s->rl_table_index,
530                 s->rl_chroma_table_index,
531                 s->dc_table_index,
532                 s->mv_table_index,
533                 s->per_mb_rl_table,
534                 s->qscale);
535
536         if(s->flipflop_rounding){
537             s->no_rounding ^= 1;
538         }else{
539             s->no_rounding = 0;
540         }
541     }
542     av_dlog(s->avctx, "%d %d %d %d %d\n", s->pict_type, s->bit_rate,
543             s->inter_intra_pred, s->width, s->height);
544
545     s->esc3_level_length= 0;
546     s->esc3_run_length= 0;
547
548     return 0;
549 }
550
551 int ff_msmpeg4_decode_ext_header(MpegEncContext * s, int buf_size)
552 {
553     int left= buf_size*8 - get_bits_count(&s->gb);
554     int length= s->msmpeg4_version>=3 ? 17 : 16;
555     /* the alt_bitstream reader could read over the end so we need to check it */
556     if(left>=length && left<length+8)
557     {
558         skip_bits(&s->gb, 5); /* fps */
559         s->bit_rate= get_bits(&s->gb, 11)*1024;
560         if(s->msmpeg4_version>=3)
561             s->flipflop_rounding= get_bits1(&s->gb);
562         else
563             s->flipflop_rounding= 0;
564     }
565     else if(left<length+8)
566     {
567         s->flipflop_rounding= 0;
568         if(s->msmpeg4_version != 2)
569             av_log(s->avctx, AV_LOG_ERROR, "ext header missing, %d left\n", left);
570     }
571     else
572     {
573         av_log(s->avctx, AV_LOG_ERROR, "I frame too long, ignoring ext header\n");
574     }
575
576     return 0;
577 }
578
579 static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr)
580 {
581     int level, pred;
582
583     if(s->msmpeg4_version<=2){
584         if (n < 4) {
585             level = get_vlc2(&s->gb, v2_dc_lum_vlc.table, DC_VLC_BITS, 3);
586         } else {
587             level = get_vlc2(&s->gb, v2_dc_chroma_vlc.table, DC_VLC_BITS, 3);
588         }
589         if (level < 0) {
590             av_log(s->avctx, AV_LOG_ERROR, "illegal dc vlc\n");
591             *dir_ptr = 0;
592             return -1;
593         }
594         level-=256;
595     }else{  //FIXME optimize use unified tables & index
596         if (n < 4) {
597             level = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
598         } else {
599             level = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
600         }
601         if (level < 0){
602             av_log(s->avctx, AV_LOG_ERROR, "illegal dc vlc\n");
603             *dir_ptr = 0;
604             return -1;
605         }
606
607         if (level == DC_MAX) {
608             level = get_bits(&s->gb, 8);
609             if (get_bits1(&s->gb))
610                 level = -level;
611         } else if (level != 0) {
612             if (get_bits1(&s->gb))
613                 level = -level;
614         }
615     }
616
617     if(s->msmpeg4_version==1){
618         int32_t *dc_val;
619         pred = msmpeg4v1_pred_dc(s, n, &dc_val);
620         level += pred;
621
622         /* update predictor */
623         *dc_val= level;
624     }else{
625         int16_t *dc_val;
626         pred   = ff_msmpeg4_pred_dc(s, n, &dc_val, dir_ptr);
627         level += pred;
628
629         /* update predictor */
630         if (n < 4) {
631             *dc_val = level * s->y_dc_scale;
632         } else {
633             *dc_val = level * s->c_dc_scale;
634         }
635     }
636
637     return level;
638 }
639
640 //#define ERROR_DETAILS
641 int ff_msmpeg4_decode_block(MpegEncContext * s, int16_t * block,
642                               int n, int coded, const uint8_t *scan_table)
643 {
644     int level, i, last, run, run_diff;
645     int av_uninit(dc_pred_dir);
646     RLTable *rl;
647     RL_VLC_ELEM *rl_vlc;
648     int qmul, qadd;
649
650     if (s->mb_intra) {
651         qmul=1;
652         qadd=0;
653
654         /* DC coef */
655         level = msmpeg4_decode_dc(s, n, &dc_pred_dir);
656
657         if (level < 0){
658             av_log(s->avctx, AV_LOG_ERROR, "dc overflow- block: %d qscale: %d//\n", n, s->qscale);
659             if(s->inter_intra_pred) level=0;
660         }
661         if (n < 4) {
662             rl = &ff_rl_table[s->rl_table_index];
663             if(level > 256*s->y_dc_scale){
664                 av_log(s->avctx, AV_LOG_ERROR, "dc overflow+ L qscale: %d//\n", s->qscale);
665                 if(!s->inter_intra_pred) return -1;
666             }
667         } else {
668             rl = &ff_rl_table[3 + s->rl_chroma_table_index];
669             if(level > 256*s->c_dc_scale){
670                 av_log(s->avctx, AV_LOG_ERROR, "dc overflow+ C qscale: %d//\n", s->qscale);
671                 if(!s->inter_intra_pred) return -1;
672             }
673         }
674         block[0] = level;
675
676         run_diff = s->msmpeg4_version >= 4;
677         i = 0;
678         if (!coded) {
679             goto not_coded;
680         }
681         if (s->ac_pred) {
682             if (dc_pred_dir == 0)
683                 scan_table = s->intra_v_scantable.permutated; /* left */
684             else
685                 scan_table = s->intra_h_scantable.permutated; /* top */
686         } else {
687             scan_table = s->intra_scantable.permutated;
688         }
689         rl_vlc= rl->rl_vlc[0];
690     } else {
691         qmul = s->qscale << 1;
692         qadd = (s->qscale - 1) | 1;
693         i = -1;
694         rl = &ff_rl_table[3 + s->rl_table_index];
695
696         if(s->msmpeg4_version==2)
697             run_diff = 0;
698         else
699             run_diff = 1;
700
701         if (!coded) {
702             s->block_last_index[n] = i;
703             return 0;
704         }
705         if(!scan_table)
706             scan_table = s->inter_scantable.permutated;
707         rl_vlc= rl->rl_vlc[s->qscale];
708     }
709   {
710     OPEN_READER(re, &s->gb);
711     for(;;) {
712         UPDATE_CACHE(re, &s->gb);
713         GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 0);
714         if (level==0) {
715             int cache;
716             cache= GET_CACHE(re, &s->gb);
717             /* escape */
718             if (s->msmpeg4_version==1 || (cache&0x80000000)==0) {
719                 if (s->msmpeg4_version==1 || (cache&0x40000000)==0) {
720                     /* third escape */
721                     if(s->msmpeg4_version!=1) LAST_SKIP_BITS(re, &s->gb, 2);
722                     UPDATE_CACHE(re, &s->gb);
723                     if(s->msmpeg4_version<=3){
724                         last=  SHOW_UBITS(re, &s->gb, 1); SKIP_CACHE(re, &s->gb, 1);
725                         run=   SHOW_UBITS(re, &s->gb, 6); SKIP_CACHE(re, &s->gb, 6);
726                         level= SHOW_SBITS(re, &s->gb, 8);
727                         SKIP_COUNTER(re, &s->gb, 1+6+8);
728                     }else{
729                         int sign;
730                         last=  SHOW_UBITS(re, &s->gb, 1); SKIP_BITS(re, &s->gb, 1);
731                         if(!s->esc3_level_length){
732                             int ll;
733                             av_dlog(s->avctx, "ESC-3 %X at %d %d\n",
734                                     show_bits(&s->gb, 24), s->mb_x, s->mb_y);
735                             if(s->qscale<8){
736                                 ll= SHOW_UBITS(re, &s->gb, 3); SKIP_BITS(re, &s->gb, 3);
737                                 if(ll==0){
738                                     ll= 8+SHOW_UBITS(re, &s->gb, 1); SKIP_BITS(re, &s->gb, 1);
739                                 }
740                             }else{
741                                 ll=2;
742                                 while(ll<8 && SHOW_UBITS(re, &s->gb, 1)==0){
743                                     ll++;
744                                     SKIP_BITS(re, &s->gb, 1);
745                                 }
746                                 if(ll<8) SKIP_BITS(re, &s->gb, 1);
747                             }
748
749                             s->esc3_level_length= ll;
750                             s->esc3_run_length= SHOW_UBITS(re, &s->gb, 2) + 3; SKIP_BITS(re, &s->gb, 2);
751                             UPDATE_CACHE(re, &s->gb);
752                         }
753                         run=   SHOW_UBITS(re, &s->gb, s->esc3_run_length);
754                         SKIP_BITS(re, &s->gb, s->esc3_run_length);
755
756                         sign=  SHOW_UBITS(re, &s->gb, 1);
757                         SKIP_BITS(re, &s->gb, 1);
758
759                         level= SHOW_UBITS(re, &s->gb, s->esc3_level_length);
760                         SKIP_BITS(re, &s->gb, s->esc3_level_length);
761                         if(sign) level= -level;
762                     }
763
764 #if 0 // waste of time / this will detect very few errors
765                     {
766                         const int abs_level= FFABS(level);
767                         const int run1= run - rl->max_run[last][abs_level] - run_diff;
768                         if(abs_level<=MAX_LEVEL && run<=MAX_RUN){
769                             if(abs_level <= rl->max_level[last][run]){
770                                 av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, vlc encoding possible\n");
771                                 return DECODING_AC_LOST;
772                             }
773                             if(abs_level <= rl->max_level[last][run]*2){
774                                 av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 1 encoding possible\n");
775                                 return DECODING_AC_LOST;
776                             }
777                             if(run1>=0 && abs_level <= rl->max_level[last][run1]){
778                                 av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 2 encoding possible\n");
779                                 return DECODING_AC_LOST;
780                             }
781                         }
782                     }
783 #endif
784                     //level = level * qmul + (level>0) * qadd - (level<=0) * qadd ;
785                     if (level>0) level= level * qmul + qadd;
786                     else         level= level * qmul - qadd;
787 #if 0 // waste of time too :(
788                     if(level>2048 || level<-2048){
789                         av_log(s->avctx, AV_LOG_ERROR, "|level| overflow in 3. esc\n");
790                         return DECODING_AC_LOST;
791                     }
792 #endif
793                     i+= run + 1;
794                     if(last) i+=192;
795 #ifdef ERROR_DETAILS
796                 if(run==66)
797                     av_log(s->avctx, AV_LOG_ERROR, "illegal vlc code in ESC3 level=%d\n", level);
798                 else if((i>62 && i<192) || i>192+63)
799                     av_log(s->avctx, AV_LOG_ERROR, "run overflow in ESC3 i=%d run=%d level=%d\n", i, run, level);
800 #endif
801                 } else {
802                     /* second escape */
803                     SKIP_BITS(re, &s->gb, 2);
804                     GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
805                     i+= run + rl->max_run[run>>7][level/qmul] + run_diff; //FIXME opt indexing
806                     level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
807                     LAST_SKIP_BITS(re, &s->gb, 1);
808 #ifdef ERROR_DETAILS
809                 if(run==66)
810                     av_log(s->avctx, AV_LOG_ERROR, "illegal vlc code in ESC2 level=%d\n", level);
811                 else if((i>62 && i<192) || i>192+63)
812                     av_log(s->avctx, AV_LOG_ERROR, "run overflow in ESC2 i=%d run=%d level=%d\n", i, run, level);
813 #endif
814                 }
815             } else {
816                 /* first escape */
817                 SKIP_BITS(re, &s->gb, 1);
818                 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
819                 i+= run;
820                 level = level + rl->max_level[run>>7][(run-1)&63] * qmul;//FIXME opt indexing
821                 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
822                 LAST_SKIP_BITS(re, &s->gb, 1);
823 #ifdef ERROR_DETAILS
824                 if(run==66)
825                     av_log(s->avctx, AV_LOG_ERROR, "illegal vlc code in ESC1 level=%d\n", level);
826                 else if((i>62 && i<192) || i>192+63)
827                     av_log(s->avctx, AV_LOG_ERROR, "run overflow in ESC1 i=%d run=%d level=%d\n", i, run, level);
828 #endif
829             }
830         } else {
831             i+= run;
832             level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
833             LAST_SKIP_BITS(re, &s->gb, 1);
834 #ifdef ERROR_DETAILS
835                 if(run==66)
836                     av_log(s->avctx, AV_LOG_ERROR, "illegal vlc code level=%d\n", level);
837                 else if((i>62 && i<192) || i>192+63)
838                     av_log(s->avctx, AV_LOG_ERROR, "run overflow i=%d run=%d level=%d\n", i, run, level);
839 #endif
840         }
841         if (i > 62){
842             i-= 192;
843             if(i&(~63)){
844                 const int left= get_bits_left(&s->gb);
845                 if(((i+192 == 64 && level/qmul==-1) || !(s->err_recognition&(AV_EF_BITSTREAM|AV_EF_COMPLIANT))) && left>=0){
846                     av_log(s->avctx, AV_LOG_ERROR, "ignoring overflow at %d %d\n", s->mb_x, s->mb_y);
847                     i = 63;
848                     break;
849                 }else{
850                     av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
851                     return -1;
852                 }
853             }
854
855             block[scan_table[i]] = level;
856             break;
857         }
858
859         block[scan_table[i]] = level;
860     }
861     CLOSE_READER(re, &s->gb);
862   }
863  not_coded:
864     if (s->mb_intra) {
865         ff_mpeg4_pred_ac(s, block, n, dc_pred_dir);
866         if (s->ac_pred) {
867             i = 63; /* XXX: not optimal */
868         }
869     }
870     if(s->msmpeg4_version>=4 && i>0) i=63; //FIXME/XXX optimize
871     s->block_last_index[n] = i;
872
873     return 0;
874 }
875
876 int ff_msmpeg4_decode_motion(MpegEncContext * s,
877                                  int *mx_ptr, int *my_ptr)
878 {
879     MVTable *mv;
880     int code, mx, my;
881
882     mv = &ff_mv_tables[s->mv_table_index];
883
884     code = get_vlc2(&s->gb, mv->vlc.table, MV_VLC_BITS, 2);
885     if (code < 0){
886         av_log(s->avctx, AV_LOG_ERROR, "illegal MV code at %d %d\n", s->mb_x, s->mb_y);
887         return -1;
888     }
889     if (code == mv->n) {
890         mx = get_bits(&s->gb, 6);
891         my = get_bits(&s->gb, 6);
892     } else {
893         mx = mv->table_mvx[code];
894         my = mv->table_mvy[code];
895     }
896
897     mx += *mx_ptr - 32;
898     my += *my_ptr - 32;
899     /* WARNING : they do not do exactly modulo encoding */
900     if (mx <= -64)
901         mx += 64;
902     else if (mx >= 64)
903         mx -= 64;
904
905     if (my <= -64)
906         my += 64;
907     else if (my >= 64)
908         my -= 64;
909     *mx_ptr = mx;
910     *my_ptr = my;
911     return 0;
912 }
913
914 AVCodec ff_msmpeg4v1_decoder = {
915     .name           = "msmpeg4v1",
916     .long_name      = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 1"),
917     .type           = AVMEDIA_TYPE_VIDEO,
918     .id             = AV_CODEC_ID_MSMPEG4V1,
919     .priv_data_size = sizeof(MpegEncContext),
920     .init           = ff_msmpeg4_decode_init,
921     .close          = ff_h263_decode_end,
922     .decode         = ff_h263_decode_frame,
923     .capabilities   = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
924     .max_lowres     = 3,
925     .pix_fmts       = ff_pixfmt_list_420,
926 };
927
928 AVCodec ff_msmpeg4v2_decoder = {
929     .name           = "msmpeg4v2",
930     .long_name      = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 2"),
931     .type           = AVMEDIA_TYPE_VIDEO,
932     .id             = AV_CODEC_ID_MSMPEG4V2,
933     .priv_data_size = sizeof(MpegEncContext),
934     .init           = ff_msmpeg4_decode_init,
935     .close          = ff_h263_decode_end,
936     .decode         = ff_h263_decode_frame,
937     .capabilities   = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
938     .max_lowres     = 3,
939     .pix_fmts       = ff_pixfmt_list_420,
940 };
941
942 AVCodec ff_msmpeg4v3_decoder = {
943     .name           = "msmpeg4",
944     .long_name      = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 3"),
945     .type           = AVMEDIA_TYPE_VIDEO,
946     .id             = AV_CODEC_ID_MSMPEG4V3,
947     .priv_data_size = sizeof(MpegEncContext),
948     .init           = ff_msmpeg4_decode_init,
949     .close          = ff_h263_decode_end,
950     .decode         = ff_h263_decode_frame,
951     .capabilities   = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
952     .max_lowres     = 3,
953     .pix_fmts       = ff_pixfmt_list_420,
954 };
955
956 AVCodec ff_wmv1_decoder = {
957     .name           = "wmv1",
958     .long_name      = NULL_IF_CONFIG_SMALL("Windows Media Video 7"),
959     .type           = AVMEDIA_TYPE_VIDEO,
960     .id             = AV_CODEC_ID_WMV1,
961     .priv_data_size = sizeof(MpegEncContext),
962     .init           = ff_msmpeg4_decode_init,
963     .close          = ff_h263_decode_end,
964     .decode         = ff_h263_decode_frame,
965     .capabilities   = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
966     .max_lowres     = 3,
967     .pix_fmts       = ff_pixfmt_list_420,
968 };