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