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 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
27 * MSMPEG4 backend for encoder and decoder
32 #include "mpegvideo.h"
34 #include "libavutil/x86/asm.h"
36 #include "mpeg4video.h"
37 #include "msmpeg4data.h"
41 * You can also call this codec : MPEG4 with a twist !
44 * - (encoding) select best mv table (two choices)
45 * - (encoding) select best vlc/dc table
50 #define V2_INTRA_CBPC_VLC_BITS 3
51 #define V2_MB_TYPE_VLC_BITS 7
53 #define V2_MV_VLC_BITS 9
54 #define TEX_VLC_BITS 9
56 #define DEFAULT_INTER_INDEX 3
58 /* This table is practically identical to the one from h263
59 * except that it is inverted. */
60 static av_cold void init_h263_dc_for_msmpeg4(void)
62 int level, uni_code, uni_len;
64 for(level=-256; level<256; level++){
66 /* find number of bits */
75 l= (-level) ^ ((1 << size) - 1);
80 uni_code= ff_mpeg4_DCtab_lum[size][0];
81 uni_len = ff_mpeg4_DCtab_lum[size][1];
82 uni_code ^= (1<<uni_len)-1; //M$ does not like compatibility
85 uni_code<<=size; uni_code|=l;
88 uni_code<<=1; uni_code|=1;
92 ff_v2_dc_lum_table[level + 256][0] = uni_code;
93 ff_v2_dc_lum_table[level + 256][1] = uni_len;
95 /* chrominance h263 */
96 uni_code= ff_mpeg4_DCtab_chrom[size][0];
97 uni_len = ff_mpeg4_DCtab_chrom[size][1];
98 uni_code ^= (1<<uni_len)-1; //M$ does not like compatibility
101 uni_code<<=size; uni_code|=l;
104 uni_code<<=1; uni_code|=1;
108 ff_v2_dc_chroma_table[level + 256][0] = uni_code;
109 ff_v2_dc_chroma_table[level + 256][1] = uni_len;
114 av_cold void ff_msmpeg4_common_init(MpegEncContext *s)
116 static int initialized=0;
118 switch(s->msmpeg4_version){
122 s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
125 if(s->workaround_bugs){
126 s->y_dc_scale_table= ff_old_ff_y_dc_scale_table;
127 s->c_dc_scale_table= ff_wmv1_c_dc_scale_table;
129 s->y_dc_scale_table= ff_mpeg4_y_dc_scale_table;
130 s->c_dc_scale_table= ff_mpeg4_c_dc_scale_table;
135 s->y_dc_scale_table= ff_wmv1_y_dc_scale_table;
136 s->c_dc_scale_table= ff_wmv1_c_dc_scale_table;
138 #if CONFIG_VC1_DECODER
140 s->y_dc_scale_table= ff_wmv3_dc_scale_table;
141 s->c_dc_scale_table= ff_wmv3_dc_scale_table;
148 if(s->msmpeg4_version>=4){
149 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_wmv1_scantable[1]);
150 ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_wmv1_scantable[2]);
151 ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_wmv1_scantable[3]);
152 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_wmv1_scantable[0]);
154 //Note the default tables are set in common_init in mpegvideo.c
159 init_h263_dc_for_msmpeg4();
163 /* predict coded block */
164 int ff_msmpeg4_coded_block_pred(MpegEncContext * s, int n, uint8_t **coded_block_ptr)
166 int xy, wrap, pred, a, b, c;
168 xy = s->block_index[n];
174 a = s->coded_block[xy - 1 ];
175 b = s->coded_block[xy - 1 - wrap];
176 c = s->coded_block[xy - wrap];
185 *coded_block_ptr = &s->coded_block[xy];
190 static inline int msmpeg4v1_pred_dc(MpegEncContext * s, int n,
191 int32_t **dc_val_ptr)
201 *dc_val_ptr= &s->last_dc[i];
202 return s->last_dc[i];
205 static int get_dc(uint8_t *src, int stride, int scale)
212 sum+=src[x + y*stride];
215 return FASTDIV((sum + (scale>>1)), scale);
218 /* dir = 0: left, dir = 1: top prediction */
219 int ff_msmpeg4_pred_dc(MpegEncContext *s, int n,
220 int16_t **dc_val_ptr, int *dir_ptr)
222 int a, b, c, wrap, pred, scale;
225 /* find prediction */
227 scale = s->y_dc_scale;
229 scale = s->c_dc_scale;
232 wrap = s->block_wrap[n];
233 dc_val= s->dc_val[0] + s->block_index[n];
239 b = dc_val[ - 1 - wrap];
242 if(s->first_slice_line && (n&2)==0 && s->msmpeg4_version<4){
246 /* XXX: the following solution consumes divisions, but it does not
247 necessitate to modify mpegvideo.c. The problem comes from the
248 fact they decided to store the quantized DC (which would lead
249 to problems if Q could vary !) */
250 #if ARCH_X86 && HAVE_7REGS && HAVE_EBX_AVAILABLE
252 "movl %3, %%eax \n\t"
253 "shrl $1, %%eax \n\t"
254 "addl %%eax, %2 \n\t"
255 "addl %%eax, %1 \n\t"
256 "addl %0, %%eax \n\t"
258 "movl %%edx, %0 \n\t"
259 "movl %1, %%eax \n\t"
261 "movl %%edx, %1 \n\t"
262 "movl %2, %%eax \n\t"
264 "movl %%edx, %2 \n\t"
265 : "+b" (a), "+c" (b), "+D" (c)
266 : "g" (scale), "S" (ff_inverse[scale])
270 /* #elif ARCH_ALPHA */
271 /* Divisions are extremely costly on Alpha; optimize the most
272 common case. But they are costly everywhere...
275 a = (a + (8 >> 1)) / 8;
276 b = (b + (8 >> 1)) / 8;
277 c = (c + (8 >> 1)) / 8;
279 a = FASTDIV((a + (scale >> 1)), scale);
280 b = FASTDIV((b + (scale >> 1)), scale);
281 c = FASTDIV((c + (scale >> 1)), scale);
284 /* XXX: WARNING: they did not choose the same test as MPEG4. This
285 is very important ! */
286 if(s->msmpeg4_version>3){
287 if(s->inter_intra_pred){
298 if (abs(a - b) < abs(b - c)) {
308 dest= s->current_picture.f.data[0] + (((n >> 1) + 2*s->mb_y) * 8* wrap ) + ((n & 1) + 2*s->mb_x) * 8;
311 dest= s->current_picture.f.data[n - 3] + (s->mb_y * 8 * wrap) + s->mb_x * 8;
313 if(s->mb_x==0) a= (1024 + (scale>>1))/scale;
314 else a= get_dc(dest-8, wrap, scale*8);
315 if(s->mb_y==0) c= (1024 + (scale>>1))/scale;
316 else c= get_dc(dest-8*wrap, wrap, scale*8);
318 if (s->h263_aic_dir==0) {
321 }else if (s->h263_aic_dir==1) {
329 }else if (s->h263_aic_dir==2) {
343 if (abs(a - b) < abs(b - c)) {
352 if (abs(a - b) <= abs(b - c)) {
361 /* update predictor */
362 *dc_val_ptr = &dc_val[0];
366 /****************************************/
369 VLC ff_mb_non_intra_vlc[4];
370 static VLC v2_dc_lum_vlc;
371 static VLC v2_dc_chroma_vlc;
372 static VLC v2_intra_cbpc_vlc;
373 static VLC v2_mb_type_vlc;
374 static VLC v2_mv_vlc;
375 VLC ff_inter_intra_vlc;
377 /* This is identical to h263 except that its range is multiplied by 2. */
378 static int msmpeg4v2_decode_motion(MpegEncContext * s, int pred, int f_code)
380 int code, val, sign, shift;
382 code = get_vlc2(&s->gb, v2_mv_vlc.table, V2_MV_VLC_BITS, 2);
383 // printf("MV code %d at %d %d pred: %d\n", code, s->mb_x,s->mb_y, pred);
389 sign = get_bits1(&s->gb);
393 val = (val - 1) << shift;
394 val |= get_bits(&s->gb, shift);
409 static int msmpeg4v12_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
413 if (s->pict_type == AV_PICTURE_TYPE_P) {
414 if (s->use_skip_mb_code) {
415 if (get_bits1(&s->gb)) {
419 s->block_last_index[i] = -1;
420 s->mv_dir = MV_DIR_FORWARD;
421 s->mv_type = MV_TYPE_16X16;
429 if(s->msmpeg4_version==2)
430 code = get_vlc2(&s->gb, v2_mb_type_vlc.table, V2_MB_TYPE_VLC_BITS, 1);
432 code = get_vlc2(&s->gb, ff_h263_inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
433 if(code<0 || code>7){
434 av_log(s->avctx, AV_LOG_ERROR, "cbpc %d invalid at %d %d\n", code, s->mb_x, s->mb_y);
438 s->mb_intra = code >>2;
443 if(s->msmpeg4_version==2)
444 cbp= get_vlc2(&s->gb, v2_intra_cbpc_vlc.table, V2_INTRA_CBPC_VLC_BITS, 1);
446 cbp= get_vlc2(&s->gb, ff_h263_intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 1);
448 av_log(s->avctx, AV_LOG_ERROR, "cbpc %d invalid at %d %d\n", cbp, s->mb_x, s->mb_y);
456 cbpy= get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
458 av_log(s->avctx, AV_LOG_ERROR, "cbpy %d invalid at %d %d\n", cbp, s->mb_x, s->mb_y);
463 if(s->msmpeg4_version==1 || (cbp&3) != 3) cbp^= 0x3C;
465 ff_h263_pred_motion(s, 0, 0, &mx, &my);
466 mx= msmpeg4v2_decode_motion(s, mx, 1);
467 my= msmpeg4v2_decode_motion(s, my, 1);
469 s->mv_dir = MV_DIR_FORWARD;
470 s->mv_type = MV_TYPE_16X16;
474 if(s->msmpeg4_version==2){
475 s->ac_pred = get_bits1(&s->gb);
476 cbp|= get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1)<<2; //FIXME check errors
479 cbp|= get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1)<<2; //FIXME check errors
480 if(s->pict_type==AV_PICTURE_TYPE_P) cbp^=0x3C;
484 s->dsp.clear_blocks(s->block[0]);
485 for (i = 0; i < 6; i++) {
486 if (ff_msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, NULL) < 0)
488 av_log(s->avctx, AV_LOG_ERROR, "\nerror while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
495 static int msmpeg4v34_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
499 uint32_t * const mb_type_ptr = &s->current_picture.f.mb_type[s->mb_x + s->mb_y*s->mb_stride];
501 if (s->pict_type == AV_PICTURE_TYPE_P) {
502 if (s->use_skip_mb_code) {
503 if (get_bits1(&s->gb)) {
507 s->block_last_index[i] = -1;
508 s->mv_dir = MV_DIR_FORWARD;
509 s->mv_type = MV_TYPE_16X16;
513 *mb_type_ptr = MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
519 code = get_vlc2(&s->gb, ff_mb_non_intra_vlc[DEFAULT_INTER_INDEX].table, MB_NON_INTRA_VLC_BITS, 3);
522 //s->mb_intra = (code & 0x40) ? 0 : 1;
523 s->mb_intra = (~code & 0x40) >> 6;
528 code = get_vlc2(&s->gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
531 /* predict coded block pattern */
534 int val = ((code >> (5 - i)) & 1);
536 int pred = ff_msmpeg4_coded_block_pred(s, i, &coded_val);
540 cbp |= val << (5 - i);
546 //printf("P at %d %d\n", s->mb_x, s->mb_y);
547 if(s->per_mb_rl_table && cbp){
548 s->rl_table_index = decode012(&s->gb);
549 s->rl_chroma_table_index = s->rl_table_index;
551 ff_h263_pred_motion(s, 0, 0, &mx, &my);
552 if (ff_msmpeg4_decode_motion(s, &mx, &my) < 0)
554 s->mv_dir = MV_DIR_FORWARD;
555 s->mv_type = MV_TYPE_16X16;
558 *mb_type_ptr = MB_TYPE_L0 | MB_TYPE_16x16;
560 //printf("I at %d %d %d %06X\n", s->mb_x, s->mb_y, ((cbp&3)? 1 : 0) +((cbp&0x3C)? 2 : 0), show_bits(&s->gb, 24));
561 s->ac_pred = get_bits1(&s->gb);
562 *mb_type_ptr = MB_TYPE_INTRA;
563 if(s->inter_intra_pred){
564 s->h263_aic_dir= get_vlc2(&s->gb, ff_inter_intra_vlc.table, INTER_INTRA_VLC_BITS, 1);
565 // printf("%d%d %d %d/", s->ac_pred, s->h263_aic_dir, s->mb_x, s->mb_y);
567 if(s->per_mb_rl_table && cbp){
568 s->rl_table_index = decode012(&s->gb);
569 s->rl_chroma_table_index = s->rl_table_index;
573 s->dsp.clear_blocks(s->block[0]);
574 for (i = 0; i < 6; i++) {
575 if (ff_msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, NULL) < 0)
577 av_log(s->avctx, AV_LOG_ERROR, "\nerror while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
585 /* init all vlc decoding tables */
586 av_cold int ff_msmpeg4_decode_init(AVCodecContext *avctx)
588 MpegEncContext *s = avctx->priv_data;
593 if (ff_h263_decode_init(avctx) < 0)
596 ff_msmpeg4_common_init(s);
601 for(i=0;i<NB_RL_TABLES;i++) {
602 ff_init_rl(&ff_rl_table[i], ff_static_rl_table_store[i]);
604 INIT_VLC_RL(ff_rl_table[0], 642);
605 INIT_VLC_RL(ff_rl_table[1], 1104);
606 INIT_VLC_RL(ff_rl_table[2], 554);
607 INIT_VLC_RL(ff_rl_table[3], 940);
608 INIT_VLC_RL(ff_rl_table[4], 962);
609 INIT_VLC_RL(ff_rl_table[5], 554);
611 mv = &ff_mv_tables[0];
612 INIT_VLC_STATIC(&mv->vlc, MV_VLC_BITS, mv->n + 1,
613 mv->table_mv_bits, 1, 1,
614 mv->table_mv_code, 2, 2, 3714);
615 mv = &ff_mv_tables[1];
616 INIT_VLC_STATIC(&mv->vlc, MV_VLC_BITS, mv->n + 1,
617 mv->table_mv_bits, 1, 1,
618 mv->table_mv_code, 2, 2, 2694);
620 INIT_VLC_STATIC(&ff_msmp4_dc_luma_vlc[0], DC_VLC_BITS, 120,
621 &ff_table0_dc_lum[0][1], 8, 4,
622 &ff_table0_dc_lum[0][0], 8, 4, 1158);
623 INIT_VLC_STATIC(&ff_msmp4_dc_chroma_vlc[0], DC_VLC_BITS, 120,
624 &ff_table0_dc_chroma[0][1], 8, 4,
625 &ff_table0_dc_chroma[0][0], 8, 4, 1118);
626 INIT_VLC_STATIC(&ff_msmp4_dc_luma_vlc[1], DC_VLC_BITS, 120,
627 &ff_table1_dc_lum[0][1], 8, 4,
628 &ff_table1_dc_lum[0][0], 8, 4, 1476);
629 INIT_VLC_STATIC(&ff_msmp4_dc_chroma_vlc[1], DC_VLC_BITS, 120,
630 &ff_table1_dc_chroma[0][1], 8, 4,
631 &ff_table1_dc_chroma[0][0], 8, 4, 1216);
633 INIT_VLC_STATIC(&v2_dc_lum_vlc, DC_VLC_BITS, 512,
634 &ff_v2_dc_lum_table[0][1], 8, 4,
635 &ff_v2_dc_lum_table[0][0], 8, 4, 1472);
636 INIT_VLC_STATIC(&v2_dc_chroma_vlc, DC_VLC_BITS, 512,
637 &ff_v2_dc_chroma_table[0][1], 8, 4,
638 &ff_v2_dc_chroma_table[0][0], 8, 4, 1506);
640 INIT_VLC_STATIC(&v2_intra_cbpc_vlc, V2_INTRA_CBPC_VLC_BITS, 4,
641 &ff_v2_intra_cbpc[0][1], 2, 1,
642 &ff_v2_intra_cbpc[0][0], 2, 1, 8);
643 INIT_VLC_STATIC(&v2_mb_type_vlc, V2_MB_TYPE_VLC_BITS, 8,
644 &ff_v2_mb_type[0][1], 2, 1,
645 &ff_v2_mb_type[0][0], 2, 1, 128);
646 INIT_VLC_STATIC(&v2_mv_vlc, V2_MV_VLC_BITS, 33,
647 &ff_mvtab[0][1], 2, 1,
648 &ff_mvtab[0][0], 2, 1, 538);
650 INIT_VLC_STATIC(&ff_mb_non_intra_vlc[0], MB_NON_INTRA_VLC_BITS, 128,
651 &ff_wmv2_inter_table[0][0][1], 8, 4,
652 &ff_wmv2_inter_table[0][0][0], 8, 4, 1636);
653 INIT_VLC_STATIC(&ff_mb_non_intra_vlc[1], MB_NON_INTRA_VLC_BITS, 128,
654 &ff_wmv2_inter_table[1][0][1], 8, 4,
655 &ff_wmv2_inter_table[1][0][0], 8, 4, 2648);
656 INIT_VLC_STATIC(&ff_mb_non_intra_vlc[2], MB_NON_INTRA_VLC_BITS, 128,
657 &ff_wmv2_inter_table[2][0][1], 8, 4,
658 &ff_wmv2_inter_table[2][0][0], 8, 4, 1532);
659 INIT_VLC_STATIC(&ff_mb_non_intra_vlc[3], MB_NON_INTRA_VLC_BITS, 128,
660 &ff_wmv2_inter_table[3][0][1], 8, 4,
661 &ff_wmv2_inter_table[3][0][0], 8, 4, 2488);
663 INIT_VLC_STATIC(&ff_msmp4_mb_i_vlc, MB_INTRA_VLC_BITS, 64,
664 &ff_msmp4_mb_i_table[0][1], 4, 2,
665 &ff_msmp4_mb_i_table[0][0], 4, 2, 536);
667 INIT_VLC_STATIC(&ff_inter_intra_vlc, INTER_INTRA_VLC_BITS, 4,
668 &ff_table_inter_intra[0][1], 2, 1,
669 &ff_table_inter_intra[0][0], 2, 1, 8);
672 switch(s->msmpeg4_version){
675 s->decode_mb= msmpeg4v12_decode_mb;
679 s->decode_mb= msmpeg4v34_decode_mb;
682 if (CONFIG_WMV2_DECODER)
683 s->decode_mb= ff_wmv2_decode_mb;
685 //FIXME + TODO VC1 decode mb
689 s->slice_height= s->mb_height; //to avoid 1/0 if the first frame is not a keyframe
694 int ff_msmpeg4_decode_picture_header(MpegEncContext * s)
698 if(s->msmpeg4_version==1){
699 int start_code = get_bits_long(&s->gb, 32);
700 if(start_code!=0x00000100){
701 av_log(s->avctx, AV_LOG_ERROR, "invalid startcode\n");
705 skip_bits(&s->gb, 5); // frame number */
708 s->pict_type = get_bits(&s->gb, 2) + 1;
709 if (s->pict_type != AV_PICTURE_TYPE_I &&
710 s->pict_type != AV_PICTURE_TYPE_P){
711 av_log(s->avctx, AV_LOG_ERROR, "invalid picture type\n");
717 if(s->pict_type == AV_PICTURE_TYPE_I) had_i=1;
718 if(!had_i) return -1;
721 s->chroma_qscale= s->qscale = get_bits(&s->gb, 5);
723 av_log(s->avctx, AV_LOG_ERROR, "invalid qscale\n");
727 if (s->pict_type == AV_PICTURE_TYPE_I) {
728 code = get_bits(&s->gb, 5);
729 if(s->msmpeg4_version==1){
730 if(code==0 || code>s->mb_height){
731 av_log(s->avctx, AV_LOG_ERROR, "invalid slice height %d\n", code);
735 s->slice_height = code;
737 /* 0x17: one slice, 0x18: two slices, ... */
739 av_log(s->avctx, AV_LOG_ERROR, "error, slice code was %X\n", code);
743 s->slice_height = s->mb_height / (code - 0x16);
746 switch(s->msmpeg4_version){
749 s->rl_chroma_table_index = 2;
750 s->rl_table_index = 2;
752 s->dc_table_index = 0; //not used
755 s->rl_chroma_table_index = decode012(&s->gb);
756 s->rl_table_index = decode012(&s->gb);
758 s->dc_table_index = get_bits1(&s->gb);
761 ff_msmpeg4_decode_ext_header(s, (2+5+5+17+7)/8);
763 if(s->bit_rate > MBAC_BITRATE) s->per_mb_rl_table= get_bits1(&s->gb);
764 else s->per_mb_rl_table= 0;
766 if(!s->per_mb_rl_table){
767 s->rl_chroma_table_index = decode012(&s->gb);
768 s->rl_table_index = decode012(&s->gb);
771 s->dc_table_index = get_bits1(&s->gb);
772 s->inter_intra_pred= 0;
776 if(s->avctx->debug&FF_DEBUG_PICT_INFO)
777 av_log(s->avctx, AV_LOG_DEBUG, "qscale:%d rlc:%d rl:%d dc:%d mbrl:%d slice:%d \n",
779 s->rl_chroma_table_index,
785 switch(s->msmpeg4_version){
788 if(s->msmpeg4_version==1)
789 s->use_skip_mb_code = 1;
791 s->use_skip_mb_code = get_bits1(&s->gb);
792 s->rl_table_index = 2;
793 s->rl_chroma_table_index = s->rl_table_index;
794 s->dc_table_index = 0; //not used
795 s->mv_table_index = 0;
798 s->use_skip_mb_code = get_bits1(&s->gb);
799 s->rl_table_index = decode012(&s->gb);
800 s->rl_chroma_table_index = s->rl_table_index;
802 s->dc_table_index = get_bits1(&s->gb);
804 s->mv_table_index = get_bits1(&s->gb);
807 s->use_skip_mb_code = get_bits1(&s->gb);
809 if(s->bit_rate > MBAC_BITRATE) s->per_mb_rl_table= get_bits1(&s->gb);
810 else s->per_mb_rl_table= 0;
812 if(!s->per_mb_rl_table){
813 s->rl_table_index = decode012(&s->gb);
814 s->rl_chroma_table_index = s->rl_table_index;
817 s->dc_table_index = get_bits1(&s->gb);
819 s->mv_table_index = get_bits1(&s->gb);
820 s->inter_intra_pred= (s->width*s->height < 320*240 && s->bit_rate<=II_BITRATE);
824 if(s->avctx->debug&FF_DEBUG_PICT_INFO)
825 av_log(s->avctx, AV_LOG_DEBUG, "skip:%d rl:%d rlc:%d dc:%d mv:%d mbrl:%d qp:%d \n",
828 s->rl_chroma_table_index,
834 if(s->flipflop_rounding){
840 //printf("%d %d %d %d %d\n", s->pict_type, s->bit_rate, s->inter_intra_pred, s->width, s->height);
842 s->esc3_level_length= 0;
843 s->esc3_run_length= 0;
848 int ff_msmpeg4_decode_ext_header(MpegEncContext * s, int buf_size)
850 int left= buf_size*8 - get_bits_count(&s->gb);
851 int length= s->msmpeg4_version>=3 ? 17 : 16;
852 /* the alt_bitstream reader could read over the end so we need to check it */
853 if(left>=length && left<length+8)
855 skip_bits(&s->gb, 5); /* fps */
856 s->bit_rate= get_bits(&s->gb, 11)*1024;
857 if(s->msmpeg4_version>=3)
858 s->flipflop_rounding= get_bits1(&s->gb);
860 s->flipflop_rounding= 0;
862 // printf("fps:%2d bps:%2d roundingType:%1d\n", fps, s->bit_rate/1024, s->flipflop_rounding);
864 else if(left<length+8)
866 s->flipflop_rounding= 0;
867 if(s->msmpeg4_version != 2)
868 av_log(s->avctx, AV_LOG_ERROR, "ext header missing, %d left\n", left);
872 av_log(s->avctx, AV_LOG_ERROR, "I frame too long, ignoring ext header\n");
878 static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr)
882 if(s->msmpeg4_version<=2){
884 level = get_vlc2(&s->gb, v2_dc_lum_vlc.table, DC_VLC_BITS, 3);
886 level = get_vlc2(&s->gb, v2_dc_chroma_vlc.table, DC_VLC_BITS, 3);
891 }else{ //FIXME optimize use unified tables & index
893 level = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
895 level = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
898 av_log(s->avctx, AV_LOG_ERROR, "illegal dc vlc\n");
902 if (level == DC_MAX) {
903 level = get_bits(&s->gb, 8);
904 if (get_bits1(&s->gb))
906 } else if (level != 0) {
907 if (get_bits1(&s->gb))
912 if(s->msmpeg4_version==1){
914 pred = msmpeg4v1_pred_dc(s, n, &dc_val);
917 /* update predictor */
921 pred = ff_msmpeg4_pred_dc(s, n, &dc_val, dir_ptr);
924 /* update predictor */
926 *dc_val = level * s->y_dc_scale;
928 *dc_val = level * s->c_dc_scale;
935 //#define ERROR_DETAILS
936 int ff_msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
937 int n, int coded, const uint8_t *scan_table)
939 int level, i, last, run, run_diff;
940 int av_uninit(dc_pred_dir);
950 level = msmpeg4_decode_dc(s, n, &dc_pred_dir);
953 av_log(s->avctx, AV_LOG_ERROR, "dc overflow- block: %d qscale: %d//\n", n, s->qscale);
954 if(s->inter_intra_pred) level=0;
958 rl = &ff_rl_table[s->rl_table_index];
959 if(level > 256*s->y_dc_scale){
960 av_log(s->avctx, AV_LOG_ERROR, "dc overflow+ L qscale: %d//\n", s->qscale);
961 if(!s->inter_intra_pred) return -1;
964 rl = &ff_rl_table[3 + s->rl_chroma_table_index];
965 if(level > 256*s->c_dc_scale){
966 av_log(s->avctx, AV_LOG_ERROR, "dc overflow+ C qscale: %d//\n", s->qscale);
967 if(!s->inter_intra_pred) return -1;
972 run_diff = s->msmpeg4_version >= 4;
978 if (dc_pred_dir == 0)
979 scan_table = s->intra_v_scantable.permutated; /* left */
981 scan_table = s->intra_h_scantable.permutated; /* top */
983 scan_table = s->intra_scantable.permutated;
985 rl_vlc= rl->rl_vlc[0];
987 qmul = s->qscale << 1;
988 qadd = (s->qscale - 1) | 1;
990 rl = &ff_rl_table[3 + s->rl_table_index];
992 if(s->msmpeg4_version==2)
998 s->block_last_index[n] = i;
1002 scan_table = s->inter_scantable.permutated;
1003 rl_vlc= rl->rl_vlc[s->qscale];
1006 OPEN_READER(re, &s->gb);
1008 UPDATE_CACHE(re, &s->gb);
1009 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 0);
1012 cache= GET_CACHE(re, &s->gb);
1014 if (s->msmpeg4_version==1 || (cache&0x80000000)==0) {
1015 if (s->msmpeg4_version==1 || (cache&0x40000000)==0) {
1017 if(s->msmpeg4_version!=1) LAST_SKIP_BITS(re, &s->gb, 2);
1018 UPDATE_CACHE(re, &s->gb);
1019 if(s->msmpeg4_version<=3){
1020 last= SHOW_UBITS(re, &s->gb, 1); SKIP_CACHE(re, &s->gb, 1);
1021 run= SHOW_UBITS(re, &s->gb, 6); SKIP_CACHE(re, &s->gb, 6);
1022 level= SHOW_SBITS(re, &s->gb, 8);
1023 SKIP_COUNTER(re, &s->gb, 1+6+8);
1026 last= SHOW_UBITS(re, &s->gb, 1); SKIP_BITS(re, &s->gb, 1);
1027 if(!s->esc3_level_length){
1029 //printf("ESC-3 %X at %d %d\n", show_bits(&s->gb, 24), s->mb_x, s->mb_y);
1031 ll= SHOW_UBITS(re, &s->gb, 3); SKIP_BITS(re, &s->gb, 3);
1033 ll= 8+SHOW_UBITS(re, &s->gb, 1); SKIP_BITS(re, &s->gb, 1);
1037 while(ll<8 && SHOW_UBITS(re, &s->gb, 1)==0){
1039 SKIP_BITS(re, &s->gb, 1);
1041 if(ll<8) SKIP_BITS(re, &s->gb, 1);
1044 s->esc3_level_length= ll;
1045 s->esc3_run_length= SHOW_UBITS(re, &s->gb, 2) + 3; SKIP_BITS(re, &s->gb, 2);
1046 //printf("level length:%d, run length: %d\n", ll, s->esc3_run_length);
1047 UPDATE_CACHE(re, &s->gb);
1049 run= SHOW_UBITS(re, &s->gb, s->esc3_run_length);
1050 SKIP_BITS(re, &s->gb, s->esc3_run_length);
1052 sign= SHOW_UBITS(re, &s->gb, 1);
1053 SKIP_BITS(re, &s->gb, 1);
1055 level= SHOW_UBITS(re, &s->gb, s->esc3_level_length);
1056 SKIP_BITS(re, &s->gb, s->esc3_level_length);
1057 if(sign) level= -level;
1059 //printf("level: %d, run: %d at %d %d\n", level, run, s->mb_x, s->mb_y);
1060 #if 0 // waste of time / this will detect very few errors
1062 const int abs_level= FFABS(level);
1063 const int run1= run - rl->max_run[last][abs_level] - run_diff;
1064 if(abs_level<=MAX_LEVEL && run<=MAX_RUN){
1065 if(abs_level <= rl->max_level[last][run]){
1066 av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, vlc encoding possible\n");
1067 return DECODING_AC_LOST;
1069 if(abs_level <= rl->max_level[last][run]*2){
1070 av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 1 encoding possible\n");
1071 return DECODING_AC_LOST;
1073 if(run1>=0 && abs_level <= rl->max_level[last][run1]){
1074 av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 2 encoding possible\n");
1075 return DECODING_AC_LOST;
1080 //level = level * qmul + (level>0) * qadd - (level<=0) * qadd ;
1081 if (level>0) level= level * qmul + qadd;
1082 else level= level * qmul - qadd;
1083 #if 0 // waste of time too :(
1084 if(level>2048 || level<-2048){
1085 av_log(s->avctx, AV_LOG_ERROR, "|level| overflow in 3. esc\n");
1086 return DECODING_AC_LOST;
1091 #ifdef ERROR_DETAILS
1093 av_log(s->avctx, AV_LOG_ERROR, "illegal vlc code in ESC3 level=%d\n", level);
1094 else if((i>62 && i<192) || i>192+63)
1095 av_log(s->avctx, AV_LOG_ERROR, "run overflow in ESC3 i=%d run=%d level=%d\n", i, run, level);
1099 SKIP_BITS(re, &s->gb, 2);
1100 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
1101 i+= run + rl->max_run[run>>7][level/qmul] + run_diff; //FIXME opt indexing
1102 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1103 LAST_SKIP_BITS(re, &s->gb, 1);
1104 #ifdef ERROR_DETAILS
1106 av_log(s->avctx, AV_LOG_ERROR, "illegal vlc code in ESC2 level=%d\n", level);
1107 else if((i>62 && i<192) || i>192+63)
1108 av_log(s->avctx, AV_LOG_ERROR, "run overflow in ESC2 i=%d run=%d level=%d\n", i, run, level);
1113 SKIP_BITS(re, &s->gb, 1);
1114 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
1116 level = level + rl->max_level[run>>7][(run-1)&63] * qmul;//FIXME opt indexing
1117 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1118 LAST_SKIP_BITS(re, &s->gb, 1);
1119 #ifdef ERROR_DETAILS
1121 av_log(s->avctx, AV_LOG_ERROR, "illegal vlc code in ESC1 level=%d\n", level);
1122 else if((i>62 && i<192) || i>192+63)
1123 av_log(s->avctx, AV_LOG_ERROR, "run overflow in ESC1 i=%d run=%d level=%d\n", i, run, level);
1128 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1129 LAST_SKIP_BITS(re, &s->gb, 1);
1130 #ifdef ERROR_DETAILS
1132 av_log(s->avctx, AV_LOG_ERROR, "illegal vlc code level=%d\n", level);
1133 else if((i>62 && i<192) || i>192+63)
1134 av_log(s->avctx, AV_LOG_ERROR, "run overflow i=%d run=%d level=%d\n", i, run, level);
1140 const int left= get_bits_left(&s->gb);
1141 if(((i+192 == 64 && level/qmul==-1) || !(s->err_recognition&(AV_EF_BITSTREAM|AV_EF_COMPLIANT))) && left>=0){
1142 av_log(s->avctx, AV_LOG_ERROR, "ignoring overflow at %d %d\n", s->mb_x, s->mb_y);
1145 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
1150 block[scan_table[i]] = level;
1154 block[scan_table[i]] = level;
1156 CLOSE_READER(re, &s->gb);
1160 ff_mpeg4_pred_ac(s, block, n, dc_pred_dir);
1162 i = 63; /* XXX: not optimal */
1165 if(s->msmpeg4_version>=4 && i>0) i=63; //FIXME/XXX optimize
1166 s->block_last_index[n] = i;
1171 int ff_msmpeg4_decode_motion(MpegEncContext * s,
1172 int *mx_ptr, int *my_ptr)
1177 mv = &ff_mv_tables[s->mv_table_index];
1179 code = get_vlc2(&s->gb, mv->vlc.table, MV_VLC_BITS, 2);
1181 av_log(s->avctx, AV_LOG_ERROR, "illegal MV code at %d %d\n", s->mb_x, s->mb_y);
1184 if (code == mv->n) {
1185 //printf("MV ESC %X at %d %d\n", show_bits(&s->gb, 24), s->mb_x, s->mb_y);
1186 mx = get_bits(&s->gb, 6);
1187 my = get_bits(&s->gb, 6);
1189 mx = mv->table_mvx[code];
1190 my = mv->table_mvy[code];
1195 /* WARNING : they do not do exactly modulo encoding */
1210 AVCodec ff_msmpeg4v1_decoder = {
1211 .name = "msmpeg4v1",
1212 .type = AVMEDIA_TYPE_VIDEO,
1213 .id = AV_CODEC_ID_MSMPEG4V1,
1214 .priv_data_size = sizeof(MpegEncContext),
1215 .init = ff_msmpeg4_decode_init,
1216 .close = ff_h263_decode_end,
1217 .decode = ff_h263_decode_frame,
1218 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
1220 .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 1"),
1221 .pix_fmts = ff_pixfmt_list_420,
1224 AVCodec ff_msmpeg4v2_decoder = {
1225 .name = "msmpeg4v2",
1226 .type = AVMEDIA_TYPE_VIDEO,
1227 .id = AV_CODEC_ID_MSMPEG4V2,
1228 .priv_data_size = sizeof(MpegEncContext),
1229 .init = ff_msmpeg4_decode_init,
1230 .close = ff_h263_decode_end,
1231 .decode = ff_h263_decode_frame,
1232 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
1234 .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 2"),
1235 .pix_fmts = ff_pixfmt_list_420,
1238 AVCodec ff_msmpeg4v3_decoder = {
1240 .type = AVMEDIA_TYPE_VIDEO,
1241 .id = AV_CODEC_ID_MSMPEG4V3,
1242 .priv_data_size = sizeof(MpegEncContext),
1243 .init = ff_msmpeg4_decode_init,
1244 .close = ff_h263_decode_end,
1245 .decode = ff_h263_decode_frame,
1246 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
1248 .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 3"),
1249 .pix_fmts = ff_pixfmt_list_420,
1252 AVCodec ff_wmv1_decoder = {
1254 .type = AVMEDIA_TYPE_VIDEO,
1255 .id = AV_CODEC_ID_WMV1,
1256 .priv_data_size = sizeof(MpegEncContext),
1257 .init = ff_msmpeg4_decode_init,
1258 .close = ff_h263_decode_end,
1259 .decode = ff_h263_decode_frame,
1260 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
1262 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 7"),
1263 .pix_fmts = ff_pixfmt_list_420,