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