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