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])
412 uint32_t * const mb_type_ptr = &s->current_picture.f.mb_type[s->mb_x + s->mb_y*s->mb_stride];
414 if (s->pict_type == AV_PICTURE_TYPE_P) {
415 if (s->use_skip_mb_code) {
416 if (get_bits1(&s->gb)) {
420 s->block_last_index[i] = -1;
421 s->mv_dir = MV_DIR_FORWARD;
422 s->mv_type = MV_TYPE_16X16;
426 *mb_type_ptr = MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
431 if(s->msmpeg4_version==2)
432 code = get_vlc2(&s->gb, v2_mb_type_vlc.table, V2_MB_TYPE_VLC_BITS, 1);
434 code = get_vlc2(&s->gb, ff_h263_inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
435 if(code<0 || code>7){
436 av_log(s->avctx, AV_LOG_ERROR, "cbpc %d invalid at %d %d\n", code, s->mb_x, s->mb_y);
440 s->mb_intra = code >>2;
445 if(s->msmpeg4_version==2)
446 cbp= get_vlc2(&s->gb, v2_intra_cbpc_vlc.table, V2_INTRA_CBPC_VLC_BITS, 1);
448 cbp= get_vlc2(&s->gb, ff_h263_intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 1);
450 av_log(s->avctx, AV_LOG_ERROR, "cbpc %d invalid at %d %d\n", cbp, s->mb_x, s->mb_y);
458 cbpy= get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
460 av_log(s->avctx, AV_LOG_ERROR, "cbpy %d invalid at %d %d\n", cbp, s->mb_x, s->mb_y);
465 if(s->msmpeg4_version==1 || (cbp&3) != 3) cbp^= 0x3C;
467 ff_h263_pred_motion(s, 0, 0, &mx, &my);
468 mx= msmpeg4v2_decode_motion(s, mx, 1);
469 my= msmpeg4v2_decode_motion(s, my, 1);
471 s->mv_dir = MV_DIR_FORWARD;
472 s->mv_type = MV_TYPE_16X16;
475 *mb_type_ptr = MB_TYPE_L0 | MB_TYPE_16x16;
477 if(s->msmpeg4_version==2){
478 s->ac_pred = get_bits1(&s->gb);
479 cbp|= get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1)<<2; //FIXME check errors
482 cbp|= get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1)<<2; //FIXME check errors
483 if(s->pict_type==AV_PICTURE_TYPE_P) cbp^=0x3C;
485 *mb_type_ptr = MB_TYPE_INTRA;
488 s->dsp.clear_blocks(s->block[0]);
489 for (i = 0; i < 6; i++) {
490 if (ff_msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, NULL) < 0)
492 av_log(s->avctx, AV_LOG_ERROR, "\nerror while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
499 static int msmpeg4v34_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
503 uint32_t * const mb_type_ptr = &s->current_picture.f.mb_type[s->mb_x + s->mb_y*s->mb_stride];
505 if (s->pict_type == AV_PICTURE_TYPE_P) {
506 if (s->use_skip_mb_code) {
507 if (get_bits1(&s->gb)) {
511 s->block_last_index[i] = -1;
512 s->mv_dir = MV_DIR_FORWARD;
513 s->mv_type = MV_TYPE_16X16;
517 *mb_type_ptr = MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
523 code = get_vlc2(&s->gb, ff_mb_non_intra_vlc[DEFAULT_INTER_INDEX].table, MB_NON_INTRA_VLC_BITS, 3);
526 //s->mb_intra = (code & 0x40) ? 0 : 1;
527 s->mb_intra = (~code & 0x40) >> 6;
532 code = get_vlc2(&s->gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
535 /* predict coded block pattern */
538 int val = ((code >> (5 - i)) & 1);
540 int pred = ff_msmpeg4_coded_block_pred(s, i, &coded_val);
544 cbp |= val << (5 - i);
550 //printf("P at %d %d\n", s->mb_x, s->mb_y);
551 if(s->per_mb_rl_table && cbp){
552 s->rl_table_index = decode012(&s->gb);
553 s->rl_chroma_table_index = s->rl_table_index;
555 ff_h263_pred_motion(s, 0, 0, &mx, &my);
556 if (ff_msmpeg4_decode_motion(s, &mx, &my) < 0)
558 s->mv_dir = MV_DIR_FORWARD;
559 s->mv_type = MV_TYPE_16X16;
562 *mb_type_ptr = MB_TYPE_L0 | MB_TYPE_16x16;
564 //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));
565 s->ac_pred = get_bits1(&s->gb);
566 *mb_type_ptr = MB_TYPE_INTRA;
567 if(s->inter_intra_pred){
568 s->h263_aic_dir= get_vlc2(&s->gb, ff_inter_intra_vlc.table, INTER_INTRA_VLC_BITS, 1);
569 // printf("%d%d %d %d/", s->ac_pred, s->h263_aic_dir, s->mb_x, s->mb_y);
571 if(s->per_mb_rl_table && cbp){
572 s->rl_table_index = decode012(&s->gb);
573 s->rl_chroma_table_index = s->rl_table_index;
577 s->dsp.clear_blocks(s->block[0]);
578 for (i = 0; i < 6; i++) {
579 if (ff_msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, NULL) < 0)
581 av_log(s->avctx, AV_LOG_ERROR, "\nerror while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
589 /* init all vlc decoding tables */
590 av_cold int ff_msmpeg4_decode_init(AVCodecContext *avctx)
592 MpegEncContext *s = avctx->priv_data;
597 if (ff_h263_decode_init(avctx) < 0)
600 ff_msmpeg4_common_init(s);
605 for(i=0;i<NB_RL_TABLES;i++) {
606 ff_init_rl(&ff_rl_table[i], ff_static_rl_table_store[i]);
608 INIT_VLC_RL(ff_rl_table[0], 642);
609 INIT_VLC_RL(ff_rl_table[1], 1104);
610 INIT_VLC_RL(ff_rl_table[2], 554);
611 INIT_VLC_RL(ff_rl_table[3], 940);
612 INIT_VLC_RL(ff_rl_table[4], 962);
613 INIT_VLC_RL(ff_rl_table[5], 554);
615 mv = &ff_mv_tables[0];
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, 3714);
619 mv = &ff_mv_tables[1];
620 INIT_VLC_STATIC(&mv->vlc, MV_VLC_BITS, mv->n + 1,
621 mv->table_mv_bits, 1, 1,
622 mv->table_mv_code, 2, 2, 2694);
624 INIT_VLC_STATIC(&ff_msmp4_dc_luma_vlc[0], DC_VLC_BITS, 120,
625 &ff_table0_dc_lum[0][1], 8, 4,
626 &ff_table0_dc_lum[0][0], 8, 4, 1158);
627 INIT_VLC_STATIC(&ff_msmp4_dc_chroma_vlc[0], DC_VLC_BITS, 120,
628 &ff_table0_dc_chroma[0][1], 8, 4,
629 &ff_table0_dc_chroma[0][0], 8, 4, 1118);
630 INIT_VLC_STATIC(&ff_msmp4_dc_luma_vlc[1], DC_VLC_BITS, 120,
631 &ff_table1_dc_lum[0][1], 8, 4,
632 &ff_table1_dc_lum[0][0], 8, 4, 1476);
633 INIT_VLC_STATIC(&ff_msmp4_dc_chroma_vlc[1], DC_VLC_BITS, 120,
634 &ff_table1_dc_chroma[0][1], 8, 4,
635 &ff_table1_dc_chroma[0][0], 8, 4, 1216);
637 INIT_VLC_STATIC(&v2_dc_lum_vlc, DC_VLC_BITS, 512,
638 &ff_v2_dc_lum_table[0][1], 8, 4,
639 &ff_v2_dc_lum_table[0][0], 8, 4, 1472);
640 INIT_VLC_STATIC(&v2_dc_chroma_vlc, DC_VLC_BITS, 512,
641 &ff_v2_dc_chroma_table[0][1], 8, 4,
642 &ff_v2_dc_chroma_table[0][0], 8, 4, 1506);
644 INIT_VLC_STATIC(&v2_intra_cbpc_vlc, V2_INTRA_CBPC_VLC_BITS, 4,
645 &ff_v2_intra_cbpc[0][1], 2, 1,
646 &ff_v2_intra_cbpc[0][0], 2, 1, 8);
647 INIT_VLC_STATIC(&v2_mb_type_vlc, V2_MB_TYPE_VLC_BITS, 8,
648 &ff_v2_mb_type[0][1], 2, 1,
649 &ff_v2_mb_type[0][0], 2, 1, 128);
650 INIT_VLC_STATIC(&v2_mv_vlc, V2_MV_VLC_BITS, 33,
651 &ff_mvtab[0][1], 2, 1,
652 &ff_mvtab[0][0], 2, 1, 538);
654 INIT_VLC_STATIC(&ff_mb_non_intra_vlc[0], MB_NON_INTRA_VLC_BITS, 128,
655 &ff_wmv2_inter_table[0][0][1], 8, 4,
656 &ff_wmv2_inter_table[0][0][0], 8, 4, 1636);
657 INIT_VLC_STATIC(&ff_mb_non_intra_vlc[1], MB_NON_INTRA_VLC_BITS, 128,
658 &ff_wmv2_inter_table[1][0][1], 8, 4,
659 &ff_wmv2_inter_table[1][0][0], 8, 4, 2648);
660 INIT_VLC_STATIC(&ff_mb_non_intra_vlc[2], MB_NON_INTRA_VLC_BITS, 128,
661 &ff_wmv2_inter_table[2][0][1], 8, 4,
662 &ff_wmv2_inter_table[2][0][0], 8, 4, 1532);
663 INIT_VLC_STATIC(&ff_mb_non_intra_vlc[3], MB_NON_INTRA_VLC_BITS, 128,
664 &ff_wmv2_inter_table[3][0][1], 8, 4,
665 &ff_wmv2_inter_table[3][0][0], 8, 4, 2488);
667 INIT_VLC_STATIC(&ff_msmp4_mb_i_vlc, MB_INTRA_VLC_BITS, 64,
668 &ff_msmp4_mb_i_table[0][1], 4, 2,
669 &ff_msmp4_mb_i_table[0][0], 4, 2, 536);
671 INIT_VLC_STATIC(&ff_inter_intra_vlc, INTER_INTRA_VLC_BITS, 4,
672 &ff_table_inter_intra[0][1], 2, 1,
673 &ff_table_inter_intra[0][0], 2, 1, 8);
676 switch(s->msmpeg4_version){
679 s->decode_mb= msmpeg4v12_decode_mb;
683 s->decode_mb= msmpeg4v34_decode_mb;
686 if (CONFIG_WMV2_DECODER)
687 s->decode_mb= ff_wmv2_decode_mb;
689 //FIXME + TODO VC1 decode mb
693 s->slice_height= s->mb_height; //to avoid 1/0 if the first frame is not a keyframe
698 int ff_msmpeg4_decode_picture_header(MpegEncContext * s)
702 if(s->msmpeg4_version==1){
703 int start_code = get_bits_long(&s->gb, 32);
704 if(start_code!=0x00000100){
705 av_log(s->avctx, AV_LOG_ERROR, "invalid startcode\n");
709 skip_bits(&s->gb, 5); // frame number */
712 s->pict_type = get_bits(&s->gb, 2) + 1;
713 if (s->pict_type != AV_PICTURE_TYPE_I &&
714 s->pict_type != AV_PICTURE_TYPE_P){
715 av_log(s->avctx, AV_LOG_ERROR, "invalid picture type\n");
721 if(s->pict_type == AV_PICTURE_TYPE_I) had_i=1;
722 if(!had_i) return -1;
725 s->chroma_qscale= s->qscale = get_bits(&s->gb, 5);
727 av_log(s->avctx, AV_LOG_ERROR, "invalid qscale\n");
731 if (s->pict_type == AV_PICTURE_TYPE_I) {
732 code = get_bits(&s->gb, 5);
733 if(s->msmpeg4_version==1){
734 if(code==0 || code>s->mb_height){
735 av_log(s->avctx, AV_LOG_ERROR, "invalid slice height %d\n", code);
739 s->slice_height = code;
741 /* 0x17: one slice, 0x18: two slices, ... */
743 av_log(s->avctx, AV_LOG_ERROR, "error, slice code was %X\n", code);
747 s->slice_height = s->mb_height / (code - 0x16);
750 switch(s->msmpeg4_version){
753 s->rl_chroma_table_index = 2;
754 s->rl_table_index = 2;
756 s->dc_table_index = 0; //not used
759 s->rl_chroma_table_index = decode012(&s->gb);
760 s->rl_table_index = decode012(&s->gb);
762 s->dc_table_index = get_bits1(&s->gb);
765 ff_msmpeg4_decode_ext_header(s, (2+5+5+17+7)/8);
767 if(s->bit_rate > MBAC_BITRATE) s->per_mb_rl_table= get_bits1(&s->gb);
768 else s->per_mb_rl_table= 0;
770 if(!s->per_mb_rl_table){
771 s->rl_chroma_table_index = decode012(&s->gb);
772 s->rl_table_index = decode012(&s->gb);
775 s->dc_table_index = get_bits1(&s->gb);
776 s->inter_intra_pred= 0;
780 if(s->avctx->debug&FF_DEBUG_PICT_INFO)
781 av_log(s->avctx, AV_LOG_DEBUG, "qscale:%d rlc:%d rl:%d dc:%d mbrl:%d slice:%d \n",
783 s->rl_chroma_table_index,
789 switch(s->msmpeg4_version){
792 if(s->msmpeg4_version==1)
793 s->use_skip_mb_code = 1;
795 s->use_skip_mb_code = get_bits1(&s->gb);
796 s->rl_table_index = 2;
797 s->rl_chroma_table_index = s->rl_table_index;
798 s->dc_table_index = 0; //not used
799 s->mv_table_index = 0;
802 s->use_skip_mb_code = get_bits1(&s->gb);
803 s->rl_table_index = decode012(&s->gb);
804 s->rl_chroma_table_index = s->rl_table_index;
806 s->dc_table_index = get_bits1(&s->gb);
808 s->mv_table_index = get_bits1(&s->gb);
811 s->use_skip_mb_code = get_bits1(&s->gb);
813 if(s->bit_rate > MBAC_BITRATE) s->per_mb_rl_table= get_bits1(&s->gb);
814 else s->per_mb_rl_table= 0;
816 if(!s->per_mb_rl_table){
817 s->rl_table_index = decode012(&s->gb);
818 s->rl_chroma_table_index = s->rl_table_index;
821 s->dc_table_index = get_bits1(&s->gb);
823 s->mv_table_index = get_bits1(&s->gb);
824 s->inter_intra_pred= (s->width*s->height < 320*240 && s->bit_rate<=II_BITRATE);
828 if(s->avctx->debug&FF_DEBUG_PICT_INFO)
829 av_log(s->avctx, AV_LOG_DEBUG, "skip:%d rl:%d rlc:%d dc:%d mv:%d mbrl:%d qp:%d \n",
832 s->rl_chroma_table_index,
838 if(s->flipflop_rounding){
844 //printf("%d %d %d %d %d\n", s->pict_type, s->bit_rate, s->inter_intra_pred, s->width, s->height);
846 s->esc3_level_length= 0;
847 s->esc3_run_length= 0;
852 int ff_msmpeg4_decode_ext_header(MpegEncContext * s, int buf_size)
854 int left= buf_size*8 - get_bits_count(&s->gb);
855 int length= s->msmpeg4_version>=3 ? 17 : 16;
856 /* the alt_bitstream reader could read over the end so we need to check it */
857 if(left>=length && left<length+8)
859 skip_bits(&s->gb, 5); /* fps */
860 s->bit_rate= get_bits(&s->gb, 11)*1024;
861 if(s->msmpeg4_version>=3)
862 s->flipflop_rounding= get_bits1(&s->gb);
864 s->flipflop_rounding= 0;
866 // printf("fps:%2d bps:%2d roundingType:%1d\n", fps, s->bit_rate/1024, s->flipflop_rounding);
868 else if(left<length+8)
870 s->flipflop_rounding= 0;
871 if(s->msmpeg4_version != 2)
872 av_log(s->avctx, AV_LOG_ERROR, "ext header missing, %d left\n", left);
876 av_log(s->avctx, AV_LOG_ERROR, "I frame too long, ignoring ext header\n");
882 static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr)
886 if(s->msmpeg4_version<=2){
888 level = get_vlc2(&s->gb, v2_dc_lum_vlc.table, DC_VLC_BITS, 3);
890 level = get_vlc2(&s->gb, v2_dc_chroma_vlc.table, DC_VLC_BITS, 3);
895 }else{ //FIXME optimize use unified tables & index
897 level = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
899 level = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
902 av_log(s->avctx, AV_LOG_ERROR, "illegal dc vlc\n");
906 if (level == DC_MAX) {
907 level = get_bits(&s->gb, 8);
908 if (get_bits1(&s->gb))
910 } else if (level != 0) {
911 if (get_bits1(&s->gb))
916 if(s->msmpeg4_version==1){
918 pred = msmpeg4v1_pred_dc(s, n, &dc_val);
921 /* update predictor */
925 pred = ff_msmpeg4_pred_dc(s, n, &dc_val, dir_ptr);
928 /* update predictor */
930 *dc_val = level * s->y_dc_scale;
932 *dc_val = level * s->c_dc_scale;
939 //#define ERROR_DETAILS
940 int ff_msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
941 int n, int coded, const uint8_t *scan_table)
943 int level, i, last, run, run_diff;
944 int av_uninit(dc_pred_dir);
954 level = msmpeg4_decode_dc(s, n, &dc_pred_dir);
957 av_log(s->avctx, AV_LOG_ERROR, "dc overflow- block: %d qscale: %d//\n", n, s->qscale);
958 if(s->inter_intra_pred) level=0;
962 rl = &ff_rl_table[s->rl_table_index];
963 if(level > 256*s->y_dc_scale){
964 av_log(s->avctx, AV_LOG_ERROR, "dc overflow+ L qscale: %d//\n", s->qscale);
965 if(!s->inter_intra_pred) return -1;
968 rl = &ff_rl_table[3 + s->rl_chroma_table_index];
969 if(level > 256*s->c_dc_scale){
970 av_log(s->avctx, AV_LOG_ERROR, "dc overflow+ C qscale: %d//\n", s->qscale);
971 if(!s->inter_intra_pred) return -1;
976 run_diff = s->msmpeg4_version >= 4;
982 if (dc_pred_dir == 0)
983 scan_table = s->intra_v_scantable.permutated; /* left */
985 scan_table = s->intra_h_scantable.permutated; /* top */
987 scan_table = s->intra_scantable.permutated;
989 rl_vlc= rl->rl_vlc[0];
991 qmul = s->qscale << 1;
992 qadd = (s->qscale - 1) | 1;
994 rl = &ff_rl_table[3 + s->rl_table_index];
996 if(s->msmpeg4_version==2)
1002 s->block_last_index[n] = i;
1006 scan_table = s->inter_scantable.permutated;
1007 rl_vlc= rl->rl_vlc[s->qscale];
1010 OPEN_READER(re, &s->gb);
1012 UPDATE_CACHE(re, &s->gb);
1013 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 0);
1016 cache= GET_CACHE(re, &s->gb);
1018 if (s->msmpeg4_version==1 || (cache&0x80000000)==0) {
1019 if (s->msmpeg4_version==1 || (cache&0x40000000)==0) {
1021 if(s->msmpeg4_version!=1) LAST_SKIP_BITS(re, &s->gb, 2);
1022 UPDATE_CACHE(re, &s->gb);
1023 if(s->msmpeg4_version<=3){
1024 last= SHOW_UBITS(re, &s->gb, 1); SKIP_CACHE(re, &s->gb, 1);
1025 run= SHOW_UBITS(re, &s->gb, 6); SKIP_CACHE(re, &s->gb, 6);
1026 level= SHOW_SBITS(re, &s->gb, 8);
1027 SKIP_COUNTER(re, &s->gb, 1+6+8);
1030 last= SHOW_UBITS(re, &s->gb, 1); SKIP_BITS(re, &s->gb, 1);
1031 if(!s->esc3_level_length){
1033 //printf("ESC-3 %X at %d %d\n", show_bits(&s->gb, 24), s->mb_x, s->mb_y);
1035 ll= SHOW_UBITS(re, &s->gb, 3); SKIP_BITS(re, &s->gb, 3);
1037 ll= 8+SHOW_UBITS(re, &s->gb, 1); SKIP_BITS(re, &s->gb, 1);
1041 while(ll<8 && SHOW_UBITS(re, &s->gb, 1)==0){
1043 SKIP_BITS(re, &s->gb, 1);
1045 if(ll<8) SKIP_BITS(re, &s->gb, 1);
1048 s->esc3_level_length= ll;
1049 s->esc3_run_length= SHOW_UBITS(re, &s->gb, 2) + 3; SKIP_BITS(re, &s->gb, 2);
1050 //printf("level length:%d, run length: %d\n", ll, s->esc3_run_length);
1051 UPDATE_CACHE(re, &s->gb);
1053 run= SHOW_UBITS(re, &s->gb, s->esc3_run_length);
1054 SKIP_BITS(re, &s->gb, s->esc3_run_length);
1056 sign= SHOW_UBITS(re, &s->gb, 1);
1057 SKIP_BITS(re, &s->gb, 1);
1059 level= SHOW_UBITS(re, &s->gb, s->esc3_level_length);
1060 SKIP_BITS(re, &s->gb, s->esc3_level_length);
1061 if(sign) level= -level;
1063 //printf("level: %d, run: %d at %d %d\n", level, run, s->mb_x, s->mb_y);
1064 #if 0 // waste of time / this will detect very few errors
1066 const int abs_level= FFABS(level);
1067 const int run1= run - rl->max_run[last][abs_level] - run_diff;
1068 if(abs_level<=MAX_LEVEL && run<=MAX_RUN){
1069 if(abs_level <= rl->max_level[last][run]){
1070 av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, vlc encoding possible\n");
1071 return DECODING_AC_LOST;
1073 if(abs_level <= rl->max_level[last][run]*2){
1074 av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 1 encoding possible\n");
1075 return DECODING_AC_LOST;
1077 if(run1>=0 && abs_level <= rl->max_level[last][run1]){
1078 av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 2 encoding possible\n");
1079 return DECODING_AC_LOST;
1084 //level = level * qmul + (level>0) * qadd - (level<=0) * qadd ;
1085 if (level>0) level= level * qmul + qadd;
1086 else level= level * qmul - qadd;
1087 #if 0 // waste of time too :(
1088 if(level>2048 || level<-2048){
1089 av_log(s->avctx, AV_LOG_ERROR, "|level| overflow in 3. esc\n");
1090 return DECODING_AC_LOST;
1095 #ifdef ERROR_DETAILS
1097 av_log(s->avctx, AV_LOG_ERROR, "illegal vlc code in ESC3 level=%d\n", level);
1098 else if((i>62 && i<192) || i>192+63)
1099 av_log(s->avctx, AV_LOG_ERROR, "run overflow in ESC3 i=%d run=%d level=%d\n", i, run, level);
1103 SKIP_BITS(re, &s->gb, 2);
1104 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
1105 i+= run + rl->max_run[run>>7][level/qmul] + run_diff; //FIXME opt indexing
1106 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1107 LAST_SKIP_BITS(re, &s->gb, 1);
1108 #ifdef ERROR_DETAILS
1110 av_log(s->avctx, AV_LOG_ERROR, "illegal vlc code in ESC2 level=%d\n", level);
1111 else if((i>62 && i<192) || i>192+63)
1112 av_log(s->avctx, AV_LOG_ERROR, "run overflow in ESC2 i=%d run=%d level=%d\n", i, run, level);
1117 SKIP_BITS(re, &s->gb, 1);
1118 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
1120 level = level + rl->max_level[run>>7][(run-1)&63] * qmul;//FIXME opt indexing
1121 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1122 LAST_SKIP_BITS(re, &s->gb, 1);
1123 #ifdef ERROR_DETAILS
1125 av_log(s->avctx, AV_LOG_ERROR, "illegal vlc code in ESC1 level=%d\n", level);
1126 else if((i>62 && i<192) || i>192+63)
1127 av_log(s->avctx, AV_LOG_ERROR, "run overflow in ESC1 i=%d run=%d level=%d\n", i, run, level);
1132 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1133 LAST_SKIP_BITS(re, &s->gb, 1);
1134 #ifdef ERROR_DETAILS
1136 av_log(s->avctx, AV_LOG_ERROR, "illegal vlc code level=%d\n", level);
1137 else if((i>62 && i<192) || i>192+63)
1138 av_log(s->avctx, AV_LOG_ERROR, "run overflow i=%d run=%d level=%d\n", i, run, level);
1144 const int left= get_bits_left(&s->gb);
1145 if(((i+192 == 64 && level/qmul==-1) || !(s->err_recognition&(AV_EF_BITSTREAM|AV_EF_COMPLIANT))) && left>=0){
1146 av_log(s->avctx, AV_LOG_ERROR, "ignoring overflow at %d %d\n", s->mb_x, s->mb_y);
1149 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
1154 block[scan_table[i]] = level;
1158 block[scan_table[i]] = level;
1160 CLOSE_READER(re, &s->gb);
1164 ff_mpeg4_pred_ac(s, block, n, dc_pred_dir);
1166 i = 63; /* XXX: not optimal */
1169 if(s->msmpeg4_version>=4 && i>0) i=63; //FIXME/XXX optimize
1170 s->block_last_index[n] = i;
1175 int ff_msmpeg4_decode_motion(MpegEncContext * s,
1176 int *mx_ptr, int *my_ptr)
1181 mv = &ff_mv_tables[s->mv_table_index];
1183 code = get_vlc2(&s->gb, mv->vlc.table, MV_VLC_BITS, 2);
1185 av_log(s->avctx, AV_LOG_ERROR, "illegal MV code at %d %d\n", s->mb_x, s->mb_y);
1188 if (code == mv->n) {
1189 //printf("MV ESC %X at %d %d\n", show_bits(&s->gb, 24), s->mb_x, s->mb_y);
1190 mx = get_bits(&s->gb, 6);
1191 my = get_bits(&s->gb, 6);
1193 mx = mv->table_mvx[code];
1194 my = mv->table_mvy[code];
1199 /* WARNING : they do not do exactly modulo encoding */
1214 AVCodec ff_msmpeg4v1_decoder = {
1215 .name = "msmpeg4v1",
1216 .type = AVMEDIA_TYPE_VIDEO,
1217 .id = AV_CODEC_ID_MSMPEG4V1,
1218 .priv_data_size = sizeof(MpegEncContext),
1219 .init = ff_msmpeg4_decode_init,
1220 .close = ff_h263_decode_end,
1221 .decode = ff_h263_decode_frame,
1222 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
1224 .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 1"),
1225 .pix_fmts = ff_pixfmt_list_420,
1228 AVCodec ff_msmpeg4v2_decoder = {
1229 .name = "msmpeg4v2",
1230 .type = AVMEDIA_TYPE_VIDEO,
1231 .id = AV_CODEC_ID_MSMPEG4V2,
1232 .priv_data_size = sizeof(MpegEncContext),
1233 .init = ff_msmpeg4_decode_init,
1234 .close = ff_h263_decode_end,
1235 .decode = ff_h263_decode_frame,
1236 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
1238 .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 2"),
1239 .pix_fmts = ff_pixfmt_list_420,
1242 AVCodec ff_msmpeg4v3_decoder = {
1244 .type = AVMEDIA_TYPE_VIDEO,
1245 .id = AV_CODEC_ID_MSMPEG4V3,
1246 .priv_data_size = sizeof(MpegEncContext),
1247 .init = ff_msmpeg4_decode_init,
1248 .close = ff_h263_decode_end,
1249 .decode = ff_h263_decode_frame,
1250 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
1252 .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 3"),
1253 .pix_fmts = ff_pixfmt_list_420,
1256 AVCodec ff_wmv1_decoder = {
1258 .type = AVMEDIA_TYPE_VIDEO,
1259 .id = AV_CODEC_ID_WMV1,
1260 .priv_data_size = sizeof(MpegEncContext),
1261 .init = ff_msmpeg4_decode_init,
1262 .close = ff_h263_decode_end,
1263 .decode = ff_h263_decode_frame,
1264 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
1266 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 7"),
1267 .pix_fmts = ff_pixfmt_list_420,