2 * MSMPEG4 backend for encoder and decoder
3 * Copyright (c) 2001 Fabrice Bellard
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
6 * msmpeg4v1 & v2 stuff by Michael Niedermayer <michaelni@gmx.at>
8 * This file is part of Libav.
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.
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.
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
26 #include "mpegutils.h"
27 #include "mpegvideo.h"
29 #include "libavutil/x86/asm.h"
31 #include "mpeg4video.h"
32 #include "msmpeg4data.h"
36 #define V2_INTRA_CBPC_VLC_BITS 3
37 #define V2_MB_TYPE_VLC_BITS 7
39 #define V2_MV_VLC_BITS 9
40 #define TEX_VLC_BITS 9
42 #define DEFAULT_INTER_INDEX 3
44 static inline int msmpeg4v1_pred_dc(MpegEncContext * s, int n,
55 *dc_val_ptr= &s->last_dc[i];
59 /****************************************/
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;
68 VLC ff_inter_intra_vlc;
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)
73 int code, val, sign, shift;
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);
82 sign = get_bits1(&s->gb);
86 val = (val - 1) << shift;
87 val |= get_bits(&s->gb, shift);
102 static int msmpeg4v12_decode_mb(MpegEncContext *s, int16_t block[6][64])
106 if (s->pict_type == AV_PICTURE_TYPE_P) {
107 if (s->use_skip_mb_code) {
108 if (get_bits1(&s->gb)) {
112 s->block_last_index[i] = -1;
113 s->mv_dir = MV_DIR_FORWARD;
114 s->mv_type = MV_TYPE_16X16;
122 if(s->msmpeg4_version==2)
123 code = get_vlc2(&s->gb, v2_mb_type_vlc.table, V2_MB_TYPE_VLC_BITS, 1);
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);
131 s->mb_intra = code >>2;
136 if(s->msmpeg4_version==2)
137 cbp= get_vlc2(&s->gb, v2_intra_cbpc_vlc.table, V2_INTRA_CBPC_VLC_BITS, 1);
139 cbp= get_vlc2(&s->gb, ff_h263_intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 1);
141 av_log(s->avctx, AV_LOG_ERROR, "cbpc %d invalid at %d %d\n", cbp, s->mb_x, s->mb_y);
149 cbpy= get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
151 av_log(s->avctx, AV_LOG_ERROR, "cbpy %d invalid at %d %d\n", cbp, s->mb_x, s->mb_y);
156 if(s->msmpeg4_version==1 || (cbp&3) != 3) cbp^= 0x3C;
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);
162 s->mv_dir = MV_DIR_FORWARD;
163 s->mv_type = MV_TYPE_16X16;
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
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;
177 s->bdsp.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)
181 av_log(s->avctx, AV_LOG_ERROR, "\nerror while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
188 static int msmpeg4v34_decode_mb(MpegEncContext *s, int16_t block[6][64])
192 uint32_t * const mb_type_ptr = &s->current_picture.mb_type[s->mb_x + s->mb_y*s->mb_stride];
194 if (s->pict_type == AV_PICTURE_TYPE_P) {
195 if (s->use_skip_mb_code) {
196 if (get_bits1(&s->gb)) {
200 s->block_last_index[i] = -1;
201 s->mv_dir = MV_DIR_FORWARD;
202 s->mv_type = MV_TYPE_16X16;
206 *mb_type_ptr = MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
212 code = get_vlc2(&s->gb, ff_mb_non_intra_vlc[DEFAULT_INTER_INDEX].table, MB_NON_INTRA_VLC_BITS, 3);
215 //s->mb_intra = (code & 0x40) ? 0 : 1;
216 s->mb_intra = (~code & 0x40) >> 6;
221 code = get_vlc2(&s->gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
224 /* predict coded block pattern */
227 int val = ((code >> (5 - i)) & 1);
229 int pred = ff_msmpeg4_coded_block_pred(s, i, &coded_val);
233 cbp |= val << (5 - i);
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;
243 ff_h263_pred_motion(s, 0, 0, &mx, &my);
244 if (ff_msmpeg4_decode_motion(s, &mx, &my) < 0)
246 s->mv_dir = MV_DIR_FORWARD;
247 s->mv_type = MV_TYPE_16X16;
250 *mb_type_ptr = MB_TYPE_L0 | MB_TYPE_16x16;
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);
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;
268 s->bdsp.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)
272 av_log(s->avctx, AV_LOG_ERROR, "\nerror while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
280 /* init all vlc decoding tables */
281 av_cold int ff_msmpeg4_decode_init(AVCodecContext *avctx)
283 MpegEncContext *s = avctx->priv_data;
288 if (ff_h263_decode_init(avctx) < 0)
291 ff_msmpeg4_common_init(s);
296 for(i=0;i<NB_RL_TABLES;i++) {
297 ff_init_rl(&ff_rl_table[i], ff_static_rl_table_store[i]);
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);
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);
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);
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);
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);
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);
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);
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);
367 switch(s->msmpeg4_version){
370 s->decode_mb= msmpeg4v12_decode_mb;
374 s->decode_mb= msmpeg4v34_decode_mb;
377 if (CONFIG_WMV2_DECODER)
378 s->decode_mb= ff_wmv2_decode_mb;
380 //FIXME + TODO VC1 decode mb
384 s->slice_height= s->mb_height; //to avoid 1/0 if the first frame is not a keyframe
389 int ff_msmpeg4_decode_picture_header(MpegEncContext * s)
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");
400 skip_bits(&s->gb, 5); // frame number */
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");
412 if(s->pict_type == AV_PICTURE_TYPE_I) had_i=1;
413 if(!had_i) return -1;
416 s->chroma_qscale= s->qscale = get_bits(&s->gb, 5);
418 av_log(s->avctx, AV_LOG_ERROR, "invalid qscale\n");
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);
430 s->slice_height = code;
432 /* 0x17: one slice, 0x18: two slices, ... */
434 av_log(s->avctx, AV_LOG_ERROR, "error, slice code was %X\n", code);
438 s->slice_height = s->mb_height / (code - 0x16);
441 switch(s->msmpeg4_version){
444 s->rl_chroma_table_index = 2;
445 s->rl_table_index = 2;
447 s->dc_table_index = 0; //not used
450 s->rl_chroma_table_index = decode012(&s->gb);
451 s->rl_table_index = decode012(&s->gb);
453 s->dc_table_index = get_bits1(&s->gb);
456 ff_msmpeg4_decode_ext_header(s, (2+5+5+17+7)/8);
458 if(s->bit_rate > MBAC_BITRATE) s->per_mb_rl_table= get_bits1(&s->gb);
459 else s->per_mb_rl_table= 0;
461 if(!s->per_mb_rl_table){
462 s->rl_chroma_table_index = decode012(&s->gb);
463 s->rl_table_index = decode012(&s->gb);
466 s->dc_table_index = get_bits1(&s->gb);
467 s->inter_intra_pred= 0;
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",
474 s->rl_chroma_table_index,
480 switch(s->msmpeg4_version){
483 if(s->msmpeg4_version==1)
484 s->use_skip_mb_code = 1;
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;
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;
497 s->dc_table_index = get_bits1(&s->gb);
499 s->mv_table_index = get_bits1(&s->gb);
502 s->use_skip_mb_code = get_bits1(&s->gb);
504 if(s->bit_rate > MBAC_BITRATE) s->per_mb_rl_table= get_bits1(&s->gb);
505 else s->per_mb_rl_table= 0;
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;
512 s->dc_table_index = get_bits1(&s->gb);
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);
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",
523 s->rl_chroma_table_index,
529 if(s->flipflop_rounding){
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);
538 s->esc3_level_length= 0;
539 s->esc3_run_length= 0;
544 int ff_msmpeg4_decode_ext_header(MpegEncContext * s, int buf_size)
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)
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);
556 s->flipflop_rounding= 0;
558 else if(left<length+8)
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);
566 av_log(s->avctx, AV_LOG_ERROR, "I frame too long, ignoring ext header\n");
572 static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr)
576 if(s->msmpeg4_version<=2){
578 level = get_vlc2(&s->gb, v2_dc_lum_vlc.table, DC_VLC_BITS, 3);
580 level = get_vlc2(&s->gb, v2_dc_chroma_vlc.table, DC_VLC_BITS, 3);
585 }else{ //FIXME optimize use unified tables & index
587 level = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
589 level = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
592 av_log(s->avctx, AV_LOG_ERROR, "illegal dc vlc\n");
596 if (level == DC_MAX) {
597 level = get_bits(&s->gb, 8);
598 if (get_bits1(&s->gb))
600 } else if (level != 0) {
601 if (get_bits1(&s->gb))
606 if(s->msmpeg4_version==1){
608 pred = msmpeg4v1_pred_dc(s, n, &dc_val);
611 /* update predictor */
615 pred = ff_msmpeg4_pred_dc(s, n, &dc_val, dir_ptr);
618 /* update predictor */
620 *dc_val = level * s->y_dc_scale;
622 *dc_val = level * s->c_dc_scale;
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)
633 int level, i, last, run, run_diff;
634 int av_uninit(dc_pred_dir);
644 level = msmpeg4_decode_dc(s, n, &dc_pred_dir);
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;
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;
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;
666 run_diff = s->msmpeg4_version >= 4;
672 if (dc_pred_dir == 0)
673 scan_table = s->intra_v_scantable.permutated; /* left */
675 scan_table = s->intra_h_scantable.permutated; /* top */
677 scan_table = s->intra_scantable.permutated;
679 rl_vlc= rl->rl_vlc[0];
681 qmul = s->qscale << 1;
682 qadd = (s->qscale - 1) | 1;
684 rl = &ff_rl_table[3 + s->rl_table_index];
686 if(s->msmpeg4_version==2)
692 s->block_last_index[n] = i;
696 scan_table = s->inter_scantable.permutated;
697 rl_vlc= rl->rl_vlc[s->qscale];
700 OPEN_READER(re, &s->gb);
702 UPDATE_CACHE(re, &s->gb);
703 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 0);
706 cache= GET_CACHE(re, &s->gb);
708 if (s->msmpeg4_version==1 || (cache&0x80000000)==0) {
709 if (s->msmpeg4_version==1 || (cache&0x40000000)==0) {
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);
720 last= SHOW_UBITS(re, &s->gb, 1); SKIP_BITS(re, &s->gb, 1);
721 if(!s->esc3_level_length){
723 av_dlog(s->avctx, "ESC-3 %X at %d %d\n",
724 show_bits(&s->gb, 24), s->mb_x, s->mb_y);
726 ll= SHOW_UBITS(re, &s->gb, 3); SKIP_BITS(re, &s->gb, 3);
728 ll= 8+SHOW_UBITS(re, &s->gb, 1); SKIP_BITS(re, &s->gb, 1);
732 while(ll<8 && SHOW_UBITS(re, &s->gb, 1)==0){
734 SKIP_BITS(re, &s->gb, 1);
736 if(ll<8) SKIP_BITS(re, &s->gb, 1);
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);
743 run= SHOW_UBITS(re, &s->gb, s->esc3_run_length);
744 SKIP_BITS(re, &s->gb, s->esc3_run_length);
746 sign= SHOW_UBITS(re, &s->gb, 1);
747 SKIP_BITS(re, &s->gb, 1);
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;
754 #if 0 // waste of time / this will detect very few errors
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;
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;
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;
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;
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);
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);
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);
807 SKIP_BITS(re, &s->gb, 1);
808 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
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);
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);
822 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
823 LAST_SKIP_BITS(re, &s->gb, 1);
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);
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);
839 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
844 block[scan_table[i]] = level;
848 block[scan_table[i]] = level;
850 CLOSE_READER(re, &s->gb);
854 ff_mpeg4_pred_ac(s, block, n, dc_pred_dir);
856 i = 63; /* XXX: not optimal */
859 if(s->msmpeg4_version>=4 && i>0) i=63; //FIXME/XXX optimize
860 s->block_last_index[n] = i;
865 int ff_msmpeg4_decode_motion(MpegEncContext * s,
866 int *mx_ptr, int *my_ptr)
871 mv = &ff_mv_tables[s->mv_table_index];
873 code = get_vlc2(&s->gb, mv->vlc.table, MV_VLC_BITS, 2);
875 av_log(s->avctx, AV_LOG_ERROR, "illegal MV code at %d %d\n", s->mb_x, s->mb_y);
879 mx = get_bits(&s->gb, 6);
880 my = get_bits(&s->gb, 6);
882 mx = mv->table_mvx[code];
883 my = mv->table_mvy[code];
888 /* WARNING : they do not do exactly modulo encoding */
903 AVCodec ff_msmpeg4v1_decoder = {
905 .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 1"),
906 .type = AVMEDIA_TYPE_VIDEO,
907 .id = AV_CODEC_ID_MSMPEG4V1,
908 .priv_data_size = sizeof(MpegEncContext),
909 .init = ff_msmpeg4_decode_init,
910 .close = ff_h263_decode_end,
911 .decode = ff_h263_decode_frame,
912 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
913 .pix_fmts = (const enum AVPixelFormat[]) {
919 AVCodec ff_msmpeg4v2_decoder = {
921 .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 2"),
922 .type = AVMEDIA_TYPE_VIDEO,
923 .id = AV_CODEC_ID_MSMPEG4V2,
924 .priv_data_size = sizeof(MpegEncContext),
925 .init = ff_msmpeg4_decode_init,
926 .close = ff_h263_decode_end,
927 .decode = ff_h263_decode_frame,
928 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
929 .pix_fmts = (const enum AVPixelFormat[]) {
935 AVCodec ff_msmpeg4v3_decoder = {
937 .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 3"),
938 .type = AVMEDIA_TYPE_VIDEO,
939 .id = AV_CODEC_ID_MSMPEG4V3,
940 .priv_data_size = sizeof(MpegEncContext),
941 .init = ff_msmpeg4_decode_init,
942 .close = ff_h263_decode_end,
943 .decode = ff_h263_decode_frame,
944 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
945 .pix_fmts = (const enum AVPixelFormat[]) {
951 AVCodec ff_wmv1_decoder = {
953 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 7"),
954 .type = AVMEDIA_TYPE_VIDEO,
955 .id = AV_CODEC_ID_WMV1,
956 .priv_data_size = sizeof(MpegEncContext),
957 .init = ff_msmpeg4_decode_init,
958 .close = ff_h263_decode_end,
959 .decode = ff_h263_decode_frame,
960 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
961 .pix_fmts = (const enum AVPixelFormat[]) {