Merge commit 'b4d24b471bc52f1f78a43ee330199e70483e51c3'
[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             return -1;
591         level-=256;
592     }else{  //FIXME optimize use unified tables & index
593         if (n < 4) {
594             level = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
595         } else {
596             level = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
597         }
598         if (level < 0){
599             av_log(s->avctx, AV_LOG_ERROR, "illegal dc vlc\n");
600             return -1;
601         }
602
603         if (level == DC_MAX) {
604             level = get_bits(&s->gb, 8);
605             if (get_bits1(&s->gb))
606                 level = -level;
607         } else if (level != 0) {
608             if (get_bits1(&s->gb))
609                 level = -level;
610         }
611     }
612
613     if(s->msmpeg4_version==1){
614         int32_t *dc_val;
615         pred = msmpeg4v1_pred_dc(s, n, &dc_val);
616         level += pred;
617
618         /* update predictor */
619         *dc_val= level;
620     }else{
621         int16_t *dc_val;
622         pred   = ff_msmpeg4_pred_dc(s, n, &dc_val, dir_ptr);
623         level += pred;
624
625         /* update predictor */
626         if (n < 4) {
627             *dc_val = level * s->y_dc_scale;
628         } else {
629             *dc_val = level * s->c_dc_scale;
630         }
631     }
632
633     return level;
634 }
635
636 //#define ERROR_DETAILS
637 int ff_msmpeg4_decode_block(MpegEncContext * s, int16_t * block,
638                               int n, int coded, const uint8_t *scan_table)
639 {
640     int level, i, last, run, run_diff;
641     int av_uninit(dc_pred_dir);
642     RLTable *rl;
643     RL_VLC_ELEM *rl_vlc;
644     int qmul, qadd;
645
646     if (s->mb_intra) {
647         qmul=1;
648         qadd=0;
649
650         /* DC coef */
651         level = msmpeg4_decode_dc(s, n, &dc_pred_dir);
652
653         if (level < 0){
654             av_log(s->avctx, AV_LOG_ERROR, "dc overflow- block: %d qscale: %d//\n", n, s->qscale);
655             if(s->inter_intra_pred) level=0;
656             else                    return -1;
657         }
658         if (n < 4) {
659             rl = &ff_rl_table[s->rl_table_index];
660             if(level > 256*s->y_dc_scale){
661                 av_log(s->avctx, AV_LOG_ERROR, "dc overflow+ L qscale: %d//\n", s->qscale);
662                 if(!s->inter_intra_pred) return -1;
663             }
664         } else {
665             rl = &ff_rl_table[3 + s->rl_chroma_table_index];
666             if(level > 256*s->c_dc_scale){
667                 av_log(s->avctx, AV_LOG_ERROR, "dc overflow+ C qscale: %d//\n", s->qscale);
668                 if(!s->inter_intra_pred) return -1;
669             }
670         }
671         block[0] = level;
672
673         run_diff = s->msmpeg4_version >= 4;
674         i = 0;
675         if (!coded) {
676             goto not_coded;
677         }
678         if (s->ac_pred) {
679             if (dc_pred_dir == 0)
680                 scan_table = s->intra_v_scantable.permutated; /* left */
681             else
682                 scan_table = s->intra_h_scantable.permutated; /* top */
683         } else {
684             scan_table = s->intra_scantable.permutated;
685         }
686         rl_vlc= rl->rl_vlc[0];
687     } else {
688         qmul = s->qscale << 1;
689         qadd = (s->qscale - 1) | 1;
690         i = -1;
691         rl = &ff_rl_table[3 + s->rl_table_index];
692
693         if(s->msmpeg4_version==2)
694             run_diff = 0;
695         else
696             run_diff = 1;
697
698         if (!coded) {
699             s->block_last_index[n] = i;
700             return 0;
701         }
702         if(!scan_table)
703             scan_table = s->inter_scantable.permutated;
704         rl_vlc= rl->rl_vlc[s->qscale];
705     }
706   {
707     OPEN_READER(re, &s->gb);
708     for(;;) {
709         UPDATE_CACHE(re, &s->gb);
710         GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 0);
711         if (level==0) {
712             int cache;
713             cache= GET_CACHE(re, &s->gb);
714             /* escape */
715             if (s->msmpeg4_version==1 || (cache&0x80000000)==0) {
716                 if (s->msmpeg4_version==1 || (cache&0x40000000)==0) {
717                     /* third escape */
718                     if(s->msmpeg4_version!=1) LAST_SKIP_BITS(re, &s->gb, 2);
719                     UPDATE_CACHE(re, &s->gb);
720                     if(s->msmpeg4_version<=3){
721                         last=  SHOW_UBITS(re, &s->gb, 1); SKIP_CACHE(re, &s->gb, 1);
722                         run=   SHOW_UBITS(re, &s->gb, 6); SKIP_CACHE(re, &s->gb, 6);
723                         level= SHOW_SBITS(re, &s->gb, 8);
724                         SKIP_COUNTER(re, &s->gb, 1+6+8);
725                     }else{
726                         int sign;
727                         last=  SHOW_UBITS(re, &s->gb, 1); SKIP_BITS(re, &s->gb, 1);
728                         if(!s->esc3_level_length){
729                             int ll;
730                             av_dlog(s->avctx, "ESC-3 %X at %d %d\n",
731                                     show_bits(&s->gb, 24), s->mb_x, s->mb_y);
732                             if(s->qscale<8){
733                                 ll= SHOW_UBITS(re, &s->gb, 3); SKIP_BITS(re, &s->gb, 3);
734                                 if(ll==0){
735                                     ll= 8+SHOW_UBITS(re, &s->gb, 1); SKIP_BITS(re, &s->gb, 1);
736                                 }
737                             }else{
738                                 ll=2;
739                                 while(ll<8 && SHOW_UBITS(re, &s->gb, 1)==0){
740                                     ll++;
741                                     SKIP_BITS(re, &s->gb, 1);
742                                 }
743                                 if(ll<8) SKIP_BITS(re, &s->gb, 1);
744                             }
745
746                             s->esc3_level_length= ll;
747                             s->esc3_run_length= SHOW_UBITS(re, &s->gb, 2) + 3; SKIP_BITS(re, &s->gb, 2);
748                             UPDATE_CACHE(re, &s->gb);
749                         }
750                         run=   SHOW_UBITS(re, &s->gb, s->esc3_run_length);
751                         SKIP_BITS(re, &s->gb, s->esc3_run_length);
752
753                         sign=  SHOW_UBITS(re, &s->gb, 1);
754                         SKIP_BITS(re, &s->gb, 1);
755
756                         level= SHOW_UBITS(re, &s->gb, s->esc3_level_length);
757                         SKIP_BITS(re, &s->gb, s->esc3_level_length);
758                         if(sign) level= -level;
759                     }
760
761 #if 0 // waste of time / this will detect very few errors
762                     {
763                         const int abs_level= FFABS(level);
764                         const int run1= run - rl->max_run[last][abs_level] - run_diff;
765                         if(abs_level<=MAX_LEVEL && run<=MAX_RUN){
766                             if(abs_level <= rl->max_level[last][run]){
767                                 av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, vlc encoding possible\n");
768                                 return DECODING_AC_LOST;
769                             }
770                             if(abs_level <= rl->max_level[last][run]*2){
771                                 av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 1 encoding possible\n");
772                                 return DECODING_AC_LOST;
773                             }
774                             if(run1>=0 && abs_level <= rl->max_level[last][run1]){
775                                 av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 2 encoding possible\n");
776                                 return DECODING_AC_LOST;
777                             }
778                         }
779                     }
780 #endif
781                     //level = level * qmul + (level>0) * qadd - (level<=0) * qadd ;
782                     if (level>0) level= level * qmul + qadd;
783                     else         level= level * qmul - qadd;
784 #if 0 // waste of time too :(
785                     if(level>2048 || level<-2048){
786                         av_log(s->avctx, AV_LOG_ERROR, "|level| overflow in 3. esc\n");
787                         return DECODING_AC_LOST;
788                     }
789 #endif
790                     i+= run + 1;
791                     if(last) i+=192;
792 #ifdef ERROR_DETAILS
793                 if(run==66)
794                     av_log(s->avctx, AV_LOG_ERROR, "illegal vlc code in ESC3 level=%d\n", level);
795                 else if((i>62 && i<192) || i>192+63)
796                     av_log(s->avctx, AV_LOG_ERROR, "run overflow in ESC3 i=%d run=%d level=%d\n", i, run, level);
797 #endif
798                 } else {
799                     /* second escape */
800                     SKIP_BITS(re, &s->gb, 2);
801                     GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
802                     i+= run + rl->max_run[run>>7][level/qmul] + run_diff; //FIXME opt indexing
803                     level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
804                     LAST_SKIP_BITS(re, &s->gb, 1);
805 #ifdef ERROR_DETAILS
806                 if(run==66)
807                     av_log(s->avctx, AV_LOG_ERROR, "illegal vlc code in ESC2 level=%d\n", level);
808                 else if((i>62 && i<192) || i>192+63)
809                     av_log(s->avctx, AV_LOG_ERROR, "run overflow in ESC2 i=%d run=%d level=%d\n", i, run, level);
810 #endif
811                 }
812             } else {
813                 /* first escape */
814                 SKIP_BITS(re, &s->gb, 1);
815                 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
816                 i+= run;
817                 level = level + rl->max_level[run>>7][(run-1)&63] * qmul;//FIXME opt indexing
818                 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
819                 LAST_SKIP_BITS(re, &s->gb, 1);
820 #ifdef ERROR_DETAILS
821                 if(run==66)
822                     av_log(s->avctx, AV_LOG_ERROR, "illegal vlc code in ESC1 level=%d\n", level);
823                 else if((i>62 && i<192) || i>192+63)
824                     av_log(s->avctx, AV_LOG_ERROR, "run overflow in ESC1 i=%d run=%d level=%d\n", i, run, level);
825 #endif
826             }
827         } else {
828             i+= run;
829             level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
830             LAST_SKIP_BITS(re, &s->gb, 1);
831 #ifdef ERROR_DETAILS
832                 if(run==66)
833                     av_log(s->avctx, AV_LOG_ERROR, "illegal vlc code level=%d\n", level);
834                 else if((i>62 && i<192) || i>192+63)
835                     av_log(s->avctx, AV_LOG_ERROR, "run overflow i=%d run=%d level=%d\n", i, run, level);
836 #endif
837         }
838         if (i > 62){
839             i-= 192;
840             if(i&(~63)){
841                 const int left= get_bits_left(&s->gb);
842                 if(((i+192 == 64 && level/qmul==-1) || !(s->err_recognition&(AV_EF_BITSTREAM|AV_EF_COMPLIANT))) && left>=0){
843                     av_log(s->avctx, AV_LOG_ERROR, "ignoring overflow at %d %d\n", s->mb_x, s->mb_y);
844                     i = 63;
845                     break;
846                 }else{
847                     av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
848                     return -1;
849                 }
850             }
851
852             block[scan_table[i]] = level;
853             break;
854         }
855
856         block[scan_table[i]] = level;
857     }
858     CLOSE_READER(re, &s->gb);
859   }
860  not_coded:
861     if (s->mb_intra) {
862         ff_mpeg4_pred_ac(s, block, n, dc_pred_dir);
863         if (s->ac_pred) {
864             i = 63; /* XXX: not optimal */
865         }
866     }
867     if(s->msmpeg4_version>=4 && i>0) i=63; //FIXME/XXX optimize
868     s->block_last_index[n] = i;
869
870     return 0;
871 }
872
873 int ff_msmpeg4_decode_motion(MpegEncContext * s,
874                                  int *mx_ptr, int *my_ptr)
875 {
876     MVTable *mv;
877     int code, mx, my;
878
879     mv = &ff_mv_tables[s->mv_table_index];
880
881     code = get_vlc2(&s->gb, mv->vlc.table, MV_VLC_BITS, 2);
882     if (code < 0){
883         av_log(s->avctx, AV_LOG_ERROR, "illegal MV code at %d %d\n", s->mb_x, s->mb_y);
884         return -1;
885     }
886     if (code == mv->n) {
887         mx = get_bits(&s->gb, 6);
888         my = get_bits(&s->gb, 6);
889     } else {
890         mx = mv->table_mvx[code];
891         my = mv->table_mvy[code];
892     }
893
894     mx += *mx_ptr - 32;
895     my += *my_ptr - 32;
896     /* WARNING : they do not do exactly modulo encoding */
897     if (mx <= -64)
898         mx += 64;
899     else if (mx >= 64)
900         mx -= 64;
901
902     if (my <= -64)
903         my += 64;
904     else if (my >= 64)
905         my -= 64;
906     *mx_ptr = mx;
907     *my_ptr = my;
908     return 0;
909 }
910
911 AVCodec ff_msmpeg4v1_decoder = {
912     .name           = "msmpeg4v1",
913     .type           = AVMEDIA_TYPE_VIDEO,
914     .id             = AV_CODEC_ID_MSMPEG4V1,
915     .priv_data_size = sizeof(MpegEncContext),
916     .init           = ff_msmpeg4_decode_init,
917     .close          = ff_h263_decode_end,
918     .decode         = ff_h263_decode_frame,
919     .capabilities   = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
920     .max_lowres     = 3,
921     .long_name      = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 1"),
922     .pix_fmts       = ff_pixfmt_list_420,
923 };
924
925 AVCodec ff_msmpeg4v2_decoder = {
926     .name           = "msmpeg4v2",
927     .type           = AVMEDIA_TYPE_VIDEO,
928     .id             = AV_CODEC_ID_MSMPEG4V2,
929     .priv_data_size = sizeof(MpegEncContext),
930     .init           = ff_msmpeg4_decode_init,
931     .close          = ff_h263_decode_end,
932     .decode         = ff_h263_decode_frame,
933     .capabilities   = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
934     .max_lowres     = 3,
935     .long_name      = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 2"),
936     .pix_fmts       = ff_pixfmt_list_420,
937 };
938
939 AVCodec ff_msmpeg4v3_decoder = {
940     .name           = "msmpeg4",
941     .type           = AVMEDIA_TYPE_VIDEO,
942     .id             = AV_CODEC_ID_MSMPEG4V3,
943     .priv_data_size = sizeof(MpegEncContext),
944     .init           = ff_msmpeg4_decode_init,
945     .close          = ff_h263_decode_end,
946     .decode         = ff_h263_decode_frame,
947     .capabilities   = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
948     .max_lowres     = 3,
949     .long_name      = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 3"),
950     .pix_fmts       = ff_pixfmt_list_420,
951 };
952
953 AVCodec ff_wmv1_decoder = {
954     .name           = "wmv1",
955     .type           = AVMEDIA_TYPE_VIDEO,
956     .id             = AV_CODEC_ID_WMV1,
957     .priv_data_size = sizeof(MpegEncContext),
958     .init           = ff_msmpeg4_decode_init,
959     .close          = ff_h263_decode_end,
960     .decode         = ff_h263_decode_frame,
961     .capabilities   = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
962     .max_lowres     = 3,
963     .long_name      = NULL_IF_CONFIG_SMALL("Windows Media Video 7"),
964     .pix_fmts       = ff_pixfmt_list_420,
965 };