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