2 * MSMPEG4 backend for encoder and decoder
3 * Copyright (c) 2001 Fabrice Bellard
4 * Copyright (c) 2002-2013 Michael Niedermayer <michaelni@gmx.at>
6 * msmpeg4v1 & v2 stuff by Michael Niedermayer <michaelni@gmx.at>
8 * This file is part of FFmpeg.
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.
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.
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
26 #include "mpegutils.h"
27 #include "mpegvideo.h"
29 #include "libavutil/imgutils.h"
30 #include "libavutil/x86/asm.h"
32 #include "mpeg4video.h"
33 #include "msmpeg4data.h"
37 #define V2_INTRA_CBPC_VLC_BITS 3
38 #define V2_MB_TYPE_VLC_BITS 7
40 #define V2_MV_VLC_BITS 9
41 #define TEX_VLC_BITS 9
43 #define DEFAULT_INTER_INDEX 3
45 static inline int msmpeg4v1_pred_dc(MpegEncContext * s, int n,
56 *dc_val_ptr= &s->last_dc[i];
60 /****************************************/
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;
69 VLC ff_inter_intra_vlc;
71 /* This is identical to h263 except that its range is multiplied by 2. */
72 static int msmpeg4v2_decode_motion(MpegEncContext * s, int pred, int f_code)
74 int code, val, sign, shift;
76 code = get_vlc2(&s->gb, v2_mv_vlc.table, V2_MV_VLC_BITS, 2);
77 av_dlog(s, "MV code %d at %d %d pred: %d\n", code, s->mb_x,s->mb_y, pred);
83 sign = get_bits1(&s->gb);
87 val = (val - 1) << shift;
88 val |= get_bits(&s->gb, shift);
103 static int msmpeg4v12_decode_mb(MpegEncContext *s, int16_t block[6][64])
106 uint32_t * const mb_type_ptr = &s->current_picture.mb_type[s->mb_x + s->mb_y*s->mb_stride];
108 if (s->pict_type == AV_PICTURE_TYPE_P) {
109 if (s->use_skip_mb_code) {
110 if (get_bits1(&s->gb)) {
114 s->block_last_index[i] = -1;
115 s->mv_dir = MV_DIR_FORWARD;
116 s->mv_type = MV_TYPE_16X16;
120 *mb_type_ptr = MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
125 if(s->msmpeg4_version==2)
126 code = get_vlc2(&s->gb, v2_mb_type_vlc.table, V2_MB_TYPE_VLC_BITS, 1);
128 code = get_vlc2(&s->gb, ff_h263_inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
129 if(code<0 || code>7){
130 av_log(s->avctx, AV_LOG_ERROR, "cbpc %d invalid at %d %d\n", code, s->mb_x, s->mb_y);
134 s->mb_intra = code >>2;
139 if(s->msmpeg4_version==2)
140 cbp= get_vlc2(&s->gb, v2_intra_cbpc_vlc.table, V2_INTRA_CBPC_VLC_BITS, 1);
142 cbp= get_vlc2(&s->gb, ff_h263_intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 1);
144 av_log(s->avctx, AV_LOG_ERROR, "cbpc %d invalid at %d %d\n", cbp, s->mb_x, s->mb_y);
152 cbpy= get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
154 av_log(s->avctx, AV_LOG_ERROR, "cbpy %d invalid at %d %d\n", cbp, s->mb_x, s->mb_y);
159 if(s->msmpeg4_version==1 || (cbp&3) != 3) cbp^= 0x3C;
161 ff_h263_pred_motion(s, 0, 0, &mx, &my);
162 mx= msmpeg4v2_decode_motion(s, mx, 1);
163 my= msmpeg4v2_decode_motion(s, my, 1);
165 s->mv_dir = MV_DIR_FORWARD;
166 s->mv_type = MV_TYPE_16X16;
169 *mb_type_ptr = MB_TYPE_L0 | MB_TYPE_16x16;
171 if(s->msmpeg4_version==2){
172 s->ac_pred = get_bits1(&s->gb);
173 cbp|= get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1)<<2; //FIXME check errors
176 cbp|= get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1)<<2; //FIXME check errors
177 if(s->pict_type==AV_PICTURE_TYPE_P) cbp^=0x3C;
179 *mb_type_ptr = MB_TYPE_INTRA;
182 s->bdsp.clear_blocks(s->block[0]);
183 for (i = 0; i < 6; i++) {
184 if (ff_msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, NULL) < 0)
186 av_log(s->avctx, AV_LOG_ERROR, "\nerror while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
193 static int msmpeg4v34_decode_mb(MpegEncContext *s, int16_t block[6][64])
197 uint32_t * const mb_type_ptr = &s->current_picture.mb_type[s->mb_x + s->mb_y*s->mb_stride];
199 if (s->pict_type == AV_PICTURE_TYPE_P) {
200 if (s->use_skip_mb_code) {
201 if (get_bits1(&s->gb)) {
205 s->block_last_index[i] = -1;
206 s->mv_dir = MV_DIR_FORWARD;
207 s->mv_type = MV_TYPE_16X16;
211 *mb_type_ptr = MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
217 code = get_vlc2(&s->gb, ff_mb_non_intra_vlc[DEFAULT_INTER_INDEX].table, MB_NON_INTRA_VLC_BITS, 3);
220 //s->mb_intra = (code & 0x40) ? 0 : 1;
221 s->mb_intra = (~code & 0x40) >> 6;
226 code = get_vlc2(&s->gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
229 /* predict coded block pattern */
232 int val = ((code >> (5 - i)) & 1);
234 int pred = ff_msmpeg4_coded_block_pred(s, i, &coded_val);
238 cbp |= val << (5 - i);
244 if(s->per_mb_rl_table && cbp){
245 s->rl_table_index = decode012(&s->gb);
246 s->rl_chroma_table_index = s->rl_table_index;
248 ff_h263_pred_motion(s, 0, 0, &mx, &my);
249 if (ff_msmpeg4_decode_motion(s, &mx, &my) < 0)
251 s->mv_dir = MV_DIR_FORWARD;
252 s->mv_type = MV_TYPE_16X16;
255 *mb_type_ptr = MB_TYPE_L0 | MB_TYPE_16x16;
257 av_dlog(s, "I at %d %d %d %06X\n", s->mb_x, s->mb_y,
258 ((cbp & 3) ? 1 : 0) +((cbp & 0x3C)? 2 : 0),
259 show_bits(&s->gb, 24));
260 s->ac_pred = get_bits1(&s->gb);
261 *mb_type_ptr = MB_TYPE_INTRA;
262 if(s->inter_intra_pred){
263 s->h263_aic_dir= get_vlc2(&s->gb, ff_inter_intra_vlc.table, INTER_INTRA_VLC_BITS, 1);
264 av_dlog(s, "%d%d %d %d/",
265 s->ac_pred, s->h263_aic_dir, s->mb_x, s->mb_y);
267 if(s->per_mb_rl_table && cbp){
268 s->rl_table_index = decode012(&s->gb);
269 s->rl_chroma_table_index = s->rl_table_index;
273 s->bdsp.clear_blocks(s->block[0]);
274 for (i = 0; i < 6; i++) {
275 if (ff_msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, NULL) < 0)
277 av_log(s->avctx, AV_LOG_ERROR, "\nerror while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
285 /* init all vlc decoding tables */
286 av_cold int ff_msmpeg4_decode_init(AVCodecContext *avctx)
288 MpegEncContext *s = avctx->priv_data;
289 static volatile int done = 0;
293 if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
296 if (ff_h263_decode_init(avctx) < 0)
299 ff_msmpeg4_common_init(s);
302 for(i=0;i<NB_RL_TABLES;i++) {
303 ff_init_rl(&ff_rl_table[i], ff_static_rl_table_store[i]);
305 INIT_VLC_RL(ff_rl_table[0], 642);
306 INIT_VLC_RL(ff_rl_table[1], 1104);
307 INIT_VLC_RL(ff_rl_table[2], 554);
308 INIT_VLC_RL(ff_rl_table[3], 940);
309 INIT_VLC_RL(ff_rl_table[4], 962);
310 INIT_VLC_RL(ff_rl_table[5], 554);
312 mv = &ff_mv_tables[0];
313 INIT_VLC_STATIC(&mv->vlc, MV_VLC_BITS, mv->n + 1,
314 mv->table_mv_bits, 1, 1,
315 mv->table_mv_code, 2, 2, 3714);
316 mv = &ff_mv_tables[1];
317 INIT_VLC_STATIC(&mv->vlc, MV_VLC_BITS, mv->n + 1,
318 mv->table_mv_bits, 1, 1,
319 mv->table_mv_code, 2, 2, 2694);
321 INIT_VLC_STATIC(&ff_msmp4_dc_luma_vlc[0], DC_VLC_BITS, 120,
322 &ff_table0_dc_lum[0][1], 8, 4,
323 &ff_table0_dc_lum[0][0], 8, 4, 1158);
324 INIT_VLC_STATIC(&ff_msmp4_dc_chroma_vlc[0], DC_VLC_BITS, 120,
325 &ff_table0_dc_chroma[0][1], 8, 4,
326 &ff_table0_dc_chroma[0][0], 8, 4, 1118);
327 INIT_VLC_STATIC(&ff_msmp4_dc_luma_vlc[1], DC_VLC_BITS, 120,
328 &ff_table1_dc_lum[0][1], 8, 4,
329 &ff_table1_dc_lum[0][0], 8, 4, 1476);
330 INIT_VLC_STATIC(&ff_msmp4_dc_chroma_vlc[1], DC_VLC_BITS, 120,
331 &ff_table1_dc_chroma[0][1], 8, 4,
332 &ff_table1_dc_chroma[0][0], 8, 4, 1216);
334 INIT_VLC_STATIC(&v2_dc_lum_vlc, DC_VLC_BITS, 512,
335 &ff_v2_dc_lum_table[0][1], 8, 4,
336 &ff_v2_dc_lum_table[0][0], 8, 4, 1472);
337 INIT_VLC_STATIC(&v2_dc_chroma_vlc, DC_VLC_BITS, 512,
338 &ff_v2_dc_chroma_table[0][1], 8, 4,
339 &ff_v2_dc_chroma_table[0][0], 8, 4, 1506);
341 INIT_VLC_STATIC(&v2_intra_cbpc_vlc, V2_INTRA_CBPC_VLC_BITS, 4,
342 &ff_v2_intra_cbpc[0][1], 2, 1,
343 &ff_v2_intra_cbpc[0][0], 2, 1, 8);
344 INIT_VLC_STATIC(&v2_mb_type_vlc, V2_MB_TYPE_VLC_BITS, 8,
345 &ff_v2_mb_type[0][1], 2, 1,
346 &ff_v2_mb_type[0][0], 2, 1, 128);
347 INIT_VLC_STATIC(&v2_mv_vlc, V2_MV_VLC_BITS, 33,
348 &ff_mvtab[0][1], 2, 1,
349 &ff_mvtab[0][0], 2, 1, 538);
351 INIT_VLC_STATIC(&ff_mb_non_intra_vlc[0], MB_NON_INTRA_VLC_BITS, 128,
352 &ff_wmv2_inter_table[0][0][1], 8, 4,
353 &ff_wmv2_inter_table[0][0][0], 8, 4, 1636);
354 INIT_VLC_STATIC(&ff_mb_non_intra_vlc[1], MB_NON_INTRA_VLC_BITS, 128,
355 &ff_wmv2_inter_table[1][0][1], 8, 4,
356 &ff_wmv2_inter_table[1][0][0], 8, 4, 2648);
357 INIT_VLC_STATIC(&ff_mb_non_intra_vlc[2], MB_NON_INTRA_VLC_BITS, 128,
358 &ff_wmv2_inter_table[2][0][1], 8, 4,
359 &ff_wmv2_inter_table[2][0][0], 8, 4, 1532);
360 INIT_VLC_STATIC(&ff_mb_non_intra_vlc[3], MB_NON_INTRA_VLC_BITS, 128,
361 &ff_wmv2_inter_table[3][0][1], 8, 4,
362 &ff_wmv2_inter_table[3][0][0], 8, 4, 2488);
364 INIT_VLC_STATIC(&ff_msmp4_mb_i_vlc, MB_INTRA_VLC_BITS, 64,
365 &ff_msmp4_mb_i_table[0][1], 4, 2,
366 &ff_msmp4_mb_i_table[0][0], 4, 2, 536);
368 INIT_VLC_STATIC(&ff_inter_intra_vlc, INTER_INTRA_VLC_BITS, 4,
369 &ff_table_inter_intra[0][1], 2, 1,
370 &ff_table_inter_intra[0][0], 2, 1, 8);
374 switch(s->msmpeg4_version){
377 s->decode_mb= msmpeg4v12_decode_mb;
381 s->decode_mb= msmpeg4v34_decode_mb;
384 if (CONFIG_WMV2_DECODER)
385 s->decode_mb= ff_wmv2_decode_mb;
387 //FIXME + TODO VC1 decode mb
391 s->slice_height= s->mb_height; //to avoid 1/0 if the first frame is not a keyframe
396 int ff_msmpeg4_decode_picture_header(MpegEncContext * s)
400 if(s->msmpeg4_version==1){
401 int start_code = get_bits_long(&s->gb, 32);
402 if(start_code!=0x00000100){
403 av_log(s->avctx, AV_LOG_ERROR, "invalid startcode\n");
407 skip_bits(&s->gb, 5); // frame number */
410 s->pict_type = get_bits(&s->gb, 2) + 1;
411 if (s->pict_type != AV_PICTURE_TYPE_I &&
412 s->pict_type != AV_PICTURE_TYPE_P){
413 av_log(s->avctx, AV_LOG_ERROR, "invalid picture type\n");
419 if(s->pict_type == AV_PICTURE_TYPE_I) had_i=1;
420 if(!had_i) return -1;
423 s->chroma_qscale= s->qscale = get_bits(&s->gb, 5);
425 av_log(s->avctx, AV_LOG_ERROR, "invalid qscale\n");
429 if (s->pict_type == AV_PICTURE_TYPE_I) {
430 code = get_bits(&s->gb, 5);
431 if(s->msmpeg4_version==1){
432 if(code==0 || code>s->mb_height){
433 av_log(s->avctx, AV_LOG_ERROR, "invalid slice height %d\n", code);
437 s->slice_height = code;
439 /* 0x17: one slice, 0x18: two slices, ... */
441 av_log(s->avctx, AV_LOG_ERROR, "error, slice code was %X\n", code);
445 s->slice_height = s->mb_height / (code - 0x16);
448 switch(s->msmpeg4_version){
451 s->rl_chroma_table_index = 2;
452 s->rl_table_index = 2;
454 s->dc_table_index = 0; //not used
457 s->rl_chroma_table_index = decode012(&s->gb);
458 s->rl_table_index = decode012(&s->gb);
460 s->dc_table_index = get_bits1(&s->gb);
463 ff_msmpeg4_decode_ext_header(s, (2+5+5+17+7)/8);
465 if(s->bit_rate > MBAC_BITRATE) s->per_mb_rl_table= get_bits1(&s->gb);
466 else s->per_mb_rl_table= 0;
468 if(!s->per_mb_rl_table){
469 s->rl_chroma_table_index = decode012(&s->gb);
470 s->rl_table_index = decode012(&s->gb);
473 s->dc_table_index = get_bits1(&s->gb);
474 s->inter_intra_pred= 0;
478 if(s->avctx->debug&FF_DEBUG_PICT_INFO)
479 av_log(s->avctx, AV_LOG_DEBUG, "qscale:%d rlc:%d rl:%d dc:%d mbrl:%d slice:%d \n",
481 s->rl_chroma_table_index,
487 switch(s->msmpeg4_version){
490 if(s->msmpeg4_version==1)
491 s->use_skip_mb_code = 1;
493 s->use_skip_mb_code = get_bits1(&s->gb);
494 s->rl_table_index = 2;
495 s->rl_chroma_table_index = s->rl_table_index;
496 s->dc_table_index = 0; //not used
497 s->mv_table_index = 0;
500 s->use_skip_mb_code = get_bits1(&s->gb);
501 s->rl_table_index = decode012(&s->gb);
502 s->rl_chroma_table_index = s->rl_table_index;
504 s->dc_table_index = get_bits1(&s->gb);
506 s->mv_table_index = get_bits1(&s->gb);
509 s->use_skip_mb_code = get_bits1(&s->gb);
511 if(s->bit_rate > MBAC_BITRATE) s->per_mb_rl_table= get_bits1(&s->gb);
512 else s->per_mb_rl_table= 0;
514 if(!s->per_mb_rl_table){
515 s->rl_table_index = decode012(&s->gb);
516 s->rl_chroma_table_index = s->rl_table_index;
519 s->dc_table_index = get_bits1(&s->gb);
521 s->mv_table_index = get_bits1(&s->gb);
522 s->inter_intra_pred= (s->width*s->height < 320*240 && s->bit_rate<=II_BITRATE);
526 if(s->avctx->debug&FF_DEBUG_PICT_INFO)
527 av_log(s->avctx, AV_LOG_DEBUG, "skip:%d rl:%d rlc:%d dc:%d mv:%d mbrl:%d qp:%d \n",
530 s->rl_chroma_table_index,
536 if(s->flipflop_rounding){
542 av_dlog(s->avctx, "%d %d %d %d %d\n", s->pict_type, s->bit_rate,
543 s->inter_intra_pred, s->width, s->height);
545 s->esc3_level_length= 0;
546 s->esc3_run_length= 0;
551 int ff_msmpeg4_decode_ext_header(MpegEncContext * s, int buf_size)
553 int left= buf_size*8 - get_bits_count(&s->gb);
554 int length= s->msmpeg4_version>=3 ? 17 : 16;
555 /* the alt_bitstream reader could read over the end so we need to check it */
556 if(left>=length && left<length+8)
558 skip_bits(&s->gb, 5); /* fps */
559 s->bit_rate= get_bits(&s->gb, 11)*1024;
560 if(s->msmpeg4_version>=3)
561 s->flipflop_rounding= get_bits1(&s->gb);
563 s->flipflop_rounding= 0;
565 else if(left<length+8)
567 s->flipflop_rounding= 0;
568 if(s->msmpeg4_version != 2)
569 av_log(s->avctx, AV_LOG_ERROR, "ext header missing, %d left\n", left);
573 av_log(s->avctx, AV_LOG_ERROR, "I frame too long, ignoring ext header\n");
579 static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr)
583 if(s->msmpeg4_version<=2){
585 level = get_vlc2(&s->gb, v2_dc_lum_vlc.table, DC_VLC_BITS, 3);
587 level = get_vlc2(&s->gb, v2_dc_chroma_vlc.table, DC_VLC_BITS, 3);
590 av_log(s->avctx, AV_LOG_ERROR, "illegal dc vlc\n");
595 }else{ //FIXME optimize use unified tables & index
597 level = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
599 level = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
602 av_log(s->avctx, AV_LOG_ERROR, "illegal dc vlc\n");
607 if (level == DC_MAX) {
608 level = get_bits(&s->gb, 8);
609 if (get_bits1(&s->gb))
611 } else if (level != 0) {
612 if (get_bits1(&s->gb))
617 if(s->msmpeg4_version==1){
619 pred = msmpeg4v1_pred_dc(s, n, &dc_val);
622 /* update predictor */
626 pred = ff_msmpeg4_pred_dc(s, n, &dc_val, dir_ptr);
629 /* update predictor */
631 *dc_val = level * s->y_dc_scale;
633 *dc_val = level * s->c_dc_scale;
640 //#define ERROR_DETAILS
641 int ff_msmpeg4_decode_block(MpegEncContext * s, int16_t * block,
642 int n, int coded, const uint8_t *scan_table)
644 int level, i, last, run, run_diff;
645 int av_uninit(dc_pred_dir);
655 level = msmpeg4_decode_dc(s, n, &dc_pred_dir);
658 av_log(s->avctx, AV_LOG_ERROR, "dc overflow- block: %d qscale: %d//\n", n, s->qscale);
659 if(s->inter_intra_pred) level=0;
662 rl = &ff_rl_table[s->rl_table_index];
663 if(level > 256*s->y_dc_scale){
664 av_log(s->avctx, AV_LOG_ERROR, "dc overflow+ L qscale: %d//\n", s->qscale);
665 if(!s->inter_intra_pred) return -1;
668 rl = &ff_rl_table[3 + s->rl_chroma_table_index];
669 if(level > 256*s->c_dc_scale){
670 av_log(s->avctx, AV_LOG_ERROR, "dc overflow+ C qscale: %d//\n", s->qscale);
671 if(!s->inter_intra_pred) return -1;
676 run_diff = s->msmpeg4_version >= 4;
682 if (dc_pred_dir == 0)
683 scan_table = s->intra_v_scantable.permutated; /* left */
685 scan_table = s->intra_h_scantable.permutated; /* top */
687 scan_table = s->intra_scantable.permutated;
689 rl_vlc= rl->rl_vlc[0];
691 qmul = s->qscale << 1;
692 qadd = (s->qscale - 1) | 1;
694 rl = &ff_rl_table[3 + s->rl_table_index];
696 if(s->msmpeg4_version==2)
702 s->block_last_index[n] = i;
706 scan_table = s->inter_scantable.permutated;
707 rl_vlc= rl->rl_vlc[s->qscale];
710 OPEN_READER(re, &s->gb);
712 UPDATE_CACHE(re, &s->gb);
713 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 0);
716 cache= GET_CACHE(re, &s->gb);
718 if (s->msmpeg4_version==1 || (cache&0x80000000)==0) {
719 if (s->msmpeg4_version==1 || (cache&0x40000000)==0) {
721 if(s->msmpeg4_version!=1) LAST_SKIP_BITS(re, &s->gb, 2);
722 UPDATE_CACHE(re, &s->gb);
723 if(s->msmpeg4_version<=3){
724 last= SHOW_UBITS(re, &s->gb, 1); SKIP_CACHE(re, &s->gb, 1);
725 run= SHOW_UBITS(re, &s->gb, 6); SKIP_CACHE(re, &s->gb, 6);
726 level= SHOW_SBITS(re, &s->gb, 8);
727 SKIP_COUNTER(re, &s->gb, 1+6+8);
730 last= SHOW_UBITS(re, &s->gb, 1); SKIP_BITS(re, &s->gb, 1);
731 if(!s->esc3_level_length){
733 av_dlog(s->avctx, "ESC-3 %X at %d %d\n",
734 show_bits(&s->gb, 24), s->mb_x, s->mb_y);
736 ll= SHOW_UBITS(re, &s->gb, 3); SKIP_BITS(re, &s->gb, 3);
738 ll= 8+SHOW_UBITS(re, &s->gb, 1); SKIP_BITS(re, &s->gb, 1);
742 while(ll<8 && SHOW_UBITS(re, &s->gb, 1)==0){
744 SKIP_BITS(re, &s->gb, 1);
746 if(ll<8) SKIP_BITS(re, &s->gb, 1);
749 s->esc3_level_length= ll;
750 s->esc3_run_length= SHOW_UBITS(re, &s->gb, 2) + 3; SKIP_BITS(re, &s->gb, 2);
751 UPDATE_CACHE(re, &s->gb);
753 run= SHOW_UBITS(re, &s->gb, s->esc3_run_length);
754 SKIP_BITS(re, &s->gb, s->esc3_run_length);
756 sign= SHOW_UBITS(re, &s->gb, 1);
757 SKIP_BITS(re, &s->gb, 1);
759 level= SHOW_UBITS(re, &s->gb, s->esc3_level_length);
760 SKIP_BITS(re, &s->gb, s->esc3_level_length);
761 if(sign) level= -level;
764 #if 0 // waste of time / this will detect very few errors
766 const int abs_level= FFABS(level);
767 const int run1= run - rl->max_run[last][abs_level] - run_diff;
768 if(abs_level<=MAX_LEVEL && run<=MAX_RUN){
769 if(abs_level <= rl->max_level[last][run]){
770 av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, vlc encoding possible\n");
771 return DECODING_AC_LOST;
773 if(abs_level <= rl->max_level[last][run]*2){
774 av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 1 encoding possible\n");
775 return DECODING_AC_LOST;
777 if(run1>=0 && abs_level <= rl->max_level[last][run1]){
778 av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 2 encoding possible\n");
779 return DECODING_AC_LOST;
784 //level = level * qmul + (level>0) * qadd - (level<=0) * qadd ;
785 if (level>0) level= level * qmul + qadd;
786 else level= level * qmul - qadd;
787 #if 0 // waste of time too :(
788 if(level>2048 || level<-2048){
789 av_log(s->avctx, AV_LOG_ERROR, "|level| overflow in 3. esc\n");
790 return DECODING_AC_LOST;
797 av_log(s->avctx, AV_LOG_ERROR, "illegal vlc code in ESC3 level=%d\n", level);
798 else if((i>62 && i<192) || i>192+63)
799 av_log(s->avctx, AV_LOG_ERROR, "run overflow in ESC3 i=%d run=%d level=%d\n", i, run, level);
803 SKIP_BITS(re, &s->gb, 2);
804 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
805 i+= run + rl->max_run[run>>7][level/qmul] + run_diff; //FIXME opt indexing
806 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
807 LAST_SKIP_BITS(re, &s->gb, 1);
810 av_log(s->avctx, AV_LOG_ERROR, "illegal vlc code in ESC2 level=%d\n", level);
811 else if((i>62 && i<192) || i>192+63)
812 av_log(s->avctx, AV_LOG_ERROR, "run overflow in ESC2 i=%d run=%d level=%d\n", i, run, level);
817 SKIP_BITS(re, &s->gb, 1);
818 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
820 level = level + rl->max_level[run>>7][(run-1)&63] * qmul;//FIXME opt indexing
821 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
822 LAST_SKIP_BITS(re, &s->gb, 1);
825 av_log(s->avctx, AV_LOG_ERROR, "illegal vlc code in ESC1 level=%d\n", level);
826 else if((i>62 && i<192) || i>192+63)
827 av_log(s->avctx, AV_LOG_ERROR, "run overflow in ESC1 i=%d run=%d level=%d\n", i, run, level);
832 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
833 LAST_SKIP_BITS(re, &s->gb, 1);
836 av_log(s->avctx, AV_LOG_ERROR, "illegal vlc code level=%d\n", level);
837 else if((i>62 && i<192) || i>192+63)
838 av_log(s->avctx, AV_LOG_ERROR, "run overflow i=%d run=%d level=%d\n", i, run, level);
844 const int left= get_bits_left(&s->gb);
845 if(((i+192 == 64 && level/qmul==-1) || !(s->err_recognition&(AV_EF_BITSTREAM|AV_EF_COMPLIANT))) && left>=0){
846 av_log(s->avctx, AV_LOG_ERROR, "ignoring overflow at %d %d\n", s->mb_x, s->mb_y);
850 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
855 block[scan_table[i]] = level;
859 block[scan_table[i]] = level;
861 CLOSE_READER(re, &s->gb);
865 ff_mpeg4_pred_ac(s, block, n, dc_pred_dir);
867 i = 63; /* XXX: not optimal */
870 if(s->msmpeg4_version>=4 && i>0) i=63; //FIXME/XXX optimize
871 s->block_last_index[n] = i;
876 int ff_msmpeg4_decode_motion(MpegEncContext * s,
877 int *mx_ptr, int *my_ptr)
882 mv = &ff_mv_tables[s->mv_table_index];
884 code = get_vlc2(&s->gb, mv->vlc.table, MV_VLC_BITS, 2);
886 av_log(s->avctx, AV_LOG_ERROR, "illegal MV code at %d %d\n", s->mb_x, s->mb_y);
890 mx = get_bits(&s->gb, 6);
891 my = get_bits(&s->gb, 6);
893 mx = mv->table_mvx[code];
894 my = mv->table_mvy[code];
899 /* WARNING : they do not do exactly modulo encoding */
914 AVCodec ff_msmpeg4v1_decoder = {
916 .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 1"),
917 .type = AVMEDIA_TYPE_VIDEO,
918 .id = AV_CODEC_ID_MSMPEG4V1,
919 .priv_data_size = sizeof(MpegEncContext),
920 .init = ff_msmpeg4_decode_init,
921 .close = ff_h263_decode_end,
922 .decode = ff_h263_decode_frame,
923 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
925 .pix_fmts = (const enum AVPixelFormat[]) {
931 AVCodec ff_msmpeg4v2_decoder = {
933 .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 2"),
934 .type = AVMEDIA_TYPE_VIDEO,
935 .id = AV_CODEC_ID_MSMPEG4V2,
936 .priv_data_size = sizeof(MpegEncContext),
937 .init = ff_msmpeg4_decode_init,
938 .close = ff_h263_decode_end,
939 .decode = ff_h263_decode_frame,
940 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
942 .pix_fmts = (const enum AVPixelFormat[]) {
948 AVCodec ff_msmpeg4v3_decoder = {
950 .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 3"),
951 .type = AVMEDIA_TYPE_VIDEO,
952 .id = AV_CODEC_ID_MSMPEG4V3,
953 .priv_data_size = sizeof(MpegEncContext),
954 .init = ff_msmpeg4_decode_init,
955 .close = ff_h263_decode_end,
956 .decode = ff_h263_decode_frame,
957 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
959 .pix_fmts = (const enum AVPixelFormat[]) {
965 AVCodec ff_wmv1_decoder = {
967 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 7"),
968 .type = AVMEDIA_TYPE_VIDEO,
969 .id = AV_CODEC_ID_WMV1,
970 .priv_data_size = sizeof(MpegEncContext),
971 .init = ff_msmpeg4_decode_init,
972 .close = ff_h263_decode_end,
973 .decode = ff_h263_decode_frame,
974 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
976 .pix_fmts = (const enum AVPixelFormat[]) {