2 * MSMPEG4 backend for ffmpeg encoder and decoder
3 * Copyright (c) 2001 Fabrice Bellard.
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * msmpeg4v1 & v2 stuff by Michael Niedermayer <michaelni@gmx.at>
25 * MSMPEG4 backend for ffmpeg encoder and decoder.
30 #include "mpegvideo.h"
33 * You can also call this codec : MPEG4 with a twist !
36 * - (encoding) select best mv table (two choices)
37 * - (encoding) select best vlc/dc table
42 #define CBPY_VLC_BITS 6
43 #define INTER_INTRA_VLC_BITS 3
44 #define V1_INTRA_CBPC_VLC_BITS 6
45 #define V1_INTER_CBPC_VLC_BITS 6
46 #define V2_INTRA_CBPC_VLC_BITS 3
47 #define V2_MB_TYPE_VLC_BITS 7
49 #define V2_MV_VLC_BITS 9
50 #define TEX_VLC_BITS 9
51 #define MB_NON_INTRA_VLC_BITS 9
52 #define MB_INTRA_VLC_BITS 9
54 #define II_BITRATE 128*1024
55 #define MBAC_BITRATE 50*1024
57 #define DEFAULT_INTER_INDEX 3
59 static uint32_t v2_dc_lum_table
[512][2];
60 static uint32_t v2_dc_chroma_table
[512][2];
62 static inline void msmpeg4_encode_block(MpegEncContext
* s
, DCTELEM
* block
, int n
);
63 static inline int msmpeg4_decode_block(MpegEncContext
* s
, DCTELEM
* block
,
64 int n
, int coded
, const uint8_t *scantable
);
65 static int msmpeg4_decode_dc(MpegEncContext
* s
, int n
, int *dir_ptr
);
66 static int msmpeg4_decode_motion(MpegEncContext
* s
,
67 int *mx_ptr
, int *my_ptr
);
68 static void msmpeg4v2_encode_motion(MpegEncContext
* s
, int val
);
69 static void init_h263_dc_for_msmpeg4(void);
70 static inline void msmpeg4_memsetw(short *tab
, int val
, int n
);
71 #ifdef CONFIG_ENCODERS
72 static int get_size_of_code(MpegEncContext
* s
, RLTable
*rl
, int last
, int run
, int level
, int intra
);
73 #endif //CONFIG_ENCODERS
74 static int msmpeg4v12_decode_mb(MpegEncContext
*s
, DCTELEM block
[6][64]);
75 static int msmpeg4v34_decode_mb(MpegEncContext
*s
, DCTELEM block
[6][64]);
76 static int wmv2_decode_mb(MpegEncContext
*s
, DCTELEM block
[6][64]);
79 extern uint8_t wmv3_dc_scale_table
[32];
86 #include "msmpeg4data.h"
88 #ifdef CONFIG_ENCODERS //strangely gcc includes this even if its not references
89 static uint8_t rl_length
[NB_RL_TABLES
][MAX_LEVEL
+1][MAX_RUN
+1][2];
90 #endif //CONFIG_ENCODERS
94 const char *st_names
[ST_NB
] = {
104 int st_current_index
= 0;
105 unsigned int st_bit_counts
[ST_NB
];
106 unsigned int st_out_bit_counts
[ST_NB
];
108 #define set_stat(var) st_current_index = var;
110 void print_stats(void)
118 total
+= st_bit_counts
[i
];
121 for(i
=0;i
<ST_NB
;i
++) {
122 printf("%-10s : %10.1f %5.1f%%\n",
124 (double)st_bit_counts
[i
] / 8.0,
125 (double)st_bit_counts
[i
] * 100.0 / total
);
127 printf("%-10s : %10.1f %5.1f%%\n",
135 total
+= st_out_bit_counts
[i
];
138 for(i
=0;i
<ST_NB
;i
++) {
139 printf("%-10s : %10.1f %5.1f%%\n",
141 (double)st_out_bit_counts
[i
] / 8.0,
142 (double)st_out_bit_counts
[i
] * 100.0 / total
);
144 printf("%-10s : %10.1f %5.1f%%\n",
152 #define set_stat(var)
156 static void common_init(MpegEncContext
* s
)
160 switch(s
->msmpeg4_version
){
164 s
->c_dc_scale_table
= ff_mpeg1_dc_scale_table
;
167 if(s
->workaround_bugs
){
168 s
->y_dc_scale_table
= old_ff_y_dc_scale_table
;
169 s
->c_dc_scale_table
= old_ff_c_dc_scale_table
;
171 s
->y_dc_scale_table
= ff_mpeg4_y_dc_scale_table
;
172 s
->c_dc_scale_table
= ff_mpeg4_c_dc_scale_table
;
177 s
->y_dc_scale_table
= wmv1_y_dc_scale_table
;
178 s
->c_dc_scale_table
= wmv1_c_dc_scale_table
;
180 #if defined(CONFIG_WMV3_DECODER)||defined(CONFIG_VC9_DECODER)
182 s
->y_dc_scale_table
= wmv3_dc_scale_table
;
183 s
->c_dc_scale_table
= wmv3_dc_scale_table
;
190 if(s
->msmpeg4_version
>=4){
191 ff_init_scantable(s
->dsp
.idct_permutation
, &s
->intra_scantable
, wmv1_scantable
[1]);
192 ff_init_scantable(s
->dsp
.idct_permutation
, &s
->intra_h_scantable
, wmv1_scantable
[2]);
193 ff_init_scantable(s
->dsp
.idct_permutation
, &s
->intra_v_scantable
, wmv1_scantable
[3]);
194 ff_init_scantable(s
->dsp
.idct_permutation
, &s
->inter_scantable
, wmv1_scantable
[0]);
196 //Note the default tables are set in common_init in mpegvideo.c
201 init_h263_dc_for_msmpeg4();
205 #ifdef CONFIG_ENCODERS
207 /* build the table which associate a (x,y) motion vector to a vlc */
208 static void init_mv_table(MVTable
*tab
)
212 tab
->table_mv_index
= av_malloc(sizeof(uint16_t) * 4096);
213 /* mark all entries as not used */
215 tab
->table_mv_index
[i
] = tab
->n
;
217 for(i
=0;i
<tab
->n
;i
++) {
218 x
= tab
->table_mvx
[i
];
219 y
= tab
->table_mvy
[i
];
220 tab
->table_mv_index
[(x
<< 6) | y
] = i
;
224 static void code012(PutBitContext
*pb
, int n
)
230 put_bits(pb
, 1, (n
>= 2));
234 void ff_msmpeg4_encode_init(MpegEncContext
*s
)
236 static int init_done
=0;
240 if(s
->msmpeg4_version
>=4){
246 /* init various encoding tables */
248 init_mv_table(&mv_tables
[0]);
249 init_mv_table(&mv_tables
[1]);
250 for(i
=0;i
<NB_RL_TABLES
;i
++)
251 init_rl(&rl_table
[i
], 1);
253 for(i
=0; i
<NB_RL_TABLES
; i
++){
255 for(level
=0; level
<=MAX_LEVEL
; level
++){
257 for(run
=0; run
<=MAX_RUN
; run
++){
259 for(last
=0; last
<2; last
++){
260 rl_length
[i
][level
][run
][last
]= get_size_of_code(s
, &rl_table
[ i
], last
, run
, level
, 0);
268 static int get_size_of_code(MpegEncContext
* s
, RLTable
*rl
, int last
, int run
, int level
, int intra
){
271 int run_diff
= intra
? 0 : 1;
273 code
= get_rl_index(rl
, last
, run
, level
);
274 size
+= rl
->table_vlc
[code
][1];
278 level1
= level
- rl
->max_level
[last
][run
];
281 code
= get_rl_index(rl
, last
, run
, level1
);
285 if (level
> MAX_LEVEL
)
287 run1
= run
- rl
->max_run
[last
][level
] - run_diff
;
290 code
= get_rl_index(rl
, last
, run1
, level
);
297 size
+= 1+1+ rl
->table_vlc
[code
][1];
301 size
+= 1+1+ rl
->table_vlc
[code
][1];
309 static void find_best_tables(MpegEncContext
* s
)
312 int best
=-1, best_size
=9999999;
313 int chroma_best
=-1, best_chroma_size
=9999999;
324 for(level
=0; level
<=MAX_LEVEL
; level
++){
326 for(run
=0; run
<=MAX_RUN
; run
++){
328 const int last_size
= size
+ chroma_size
;
329 for(last
=0; last
<2; last
++){
330 int inter_count
= s
->ac_stats
[0][0][level
][run
][last
] + s
->ac_stats
[0][1][level
][run
][last
];
331 int intra_luma_count
= s
->ac_stats
[1][0][level
][run
][last
];
332 int intra_chroma_count
= s
->ac_stats
[1][1][level
][run
][last
];
334 if(s
->pict_type
==I_TYPE
){
335 size
+= intra_luma_count
*rl_length
[i
][level
][run
][last
];
336 chroma_size
+= intra_chroma_count
*rl_length
[i
+3][level
][run
][last
];
338 size
+= intra_luma_count
*rl_length
[i
][level
][run
][last
]
339 +intra_chroma_count
*rl_length
[i
+3][level
][run
][last
]
340 +inter_count
*rl_length
[i
+3][level
][run
][last
];
343 if(last_size
== size
+chroma_size
) break;
350 if(chroma_size
<best_chroma_size
){
351 best_chroma_size
= chroma_size
;
356 // printf("type:%d, best:%d, qp:%d, var:%d, mcvar:%d, size:%d //\n",
357 // s->pict_type, best, s->qscale, s->mb_var_sum, s->mc_mb_var_sum, best_size);
359 if(s
->pict_type
==P_TYPE
) chroma_best
= best
;
361 memset(s
->ac_stats
, 0, sizeof(int)*(MAX_LEVEL
+1)*(MAX_RUN
+1)*2*2*2);
363 s
->rl_table_index
= best
;
364 s
->rl_chroma_table_index
= chroma_best
;
366 if(s
->pict_type
!= s
->last_non_b_pict_type
){
367 s
->rl_table_index
= 2;
368 if(s
->pict_type
==I_TYPE
)
369 s
->rl_chroma_table_index
= 1;
371 s
->rl_chroma_table_index
= 2;
376 /* write MSMPEG4 compatible frame header */
377 void msmpeg4_encode_picture_header(MpegEncContext
* s
, int picture_number
)
381 align_put_bits(&s
->pb
);
382 put_bits(&s
->pb
, 2, s
->pict_type
- 1);
384 put_bits(&s
->pb
, 5, s
->qscale
);
385 if(s
->msmpeg4_version
<=2){
386 s
->rl_table_index
= 2;
387 s
->rl_chroma_table_index
= 2;
390 s
->dc_table_index
= 1;
391 s
->mv_table_index
= 1; /* only if P frame */
392 s
->use_skip_mb_code
= 1; /* only if P frame */
393 s
->per_mb_rl_table
= 0;
394 if(s
->msmpeg4_version
==4)
395 s
->inter_intra_pred
= (s
->width
*s
->height
< 320*240 && s
->bit_rate
<=II_BITRATE
&& s
->pict_type
==P_TYPE
);
396 //printf("%d %d %d %d %d\n", s->pict_type, s->bit_rate, s->inter_intra_pred, s->width, s->height);
398 if (s
->pict_type
== I_TYPE
) {
399 s
->slice_height
= s
->mb_height
/1;
400 put_bits(&s
->pb
, 5, 0x16 + s
->mb_height
/s
->slice_height
);
402 if(s
->msmpeg4_version
==4){
403 msmpeg4_encode_ext_header(s
);
404 if(s
->bit_rate
>MBAC_BITRATE
)
405 put_bits(&s
->pb
, 1, s
->per_mb_rl_table
);
408 if(s
->msmpeg4_version
>2){
409 if(!s
->per_mb_rl_table
){
410 code012(&s
->pb
, s
->rl_chroma_table_index
);
411 code012(&s
->pb
, s
->rl_table_index
);
414 put_bits(&s
->pb
, 1, s
->dc_table_index
);
417 put_bits(&s
->pb
, 1, s
->use_skip_mb_code
);
419 if(s
->msmpeg4_version
==4 && s
->bit_rate
>MBAC_BITRATE
)
420 put_bits(&s
->pb
, 1, s
->per_mb_rl_table
);
422 if(s
->msmpeg4_version
>2){
423 if(!s
->per_mb_rl_table
)
424 code012(&s
->pb
, s
->rl_table_index
);
426 put_bits(&s
->pb
, 1, s
->dc_table_index
);
428 put_bits(&s
->pb
, 1, s
->mv_table_index
);
432 s
->esc3_level_length
= 0;
433 s
->esc3_run_length
= 0;
437 printf("*****frame %d:\n", frame_count
++);
441 void msmpeg4_encode_ext_header(MpegEncContext
* s
)
443 put_bits(&s
->pb
, 5, s
->avctx
->time_base
.den
/ s
->avctx
->time_base
.num
); //yes 29.97 -> 29
445 put_bits(&s
->pb
, 11, FFMIN(s
->bit_rate
/1024, 2047));
447 if(s
->msmpeg4_version
>=3)
448 put_bits(&s
->pb
, 1, s
->flipflop_rounding
);
450 assert(s
->flipflop_rounding
==0);
453 #endif //CONFIG_ENCODERS
455 /* predict coded block */
456 static inline int coded_block_pred(MpegEncContext
* s
, int n
, uint8_t **coded_block_ptr
)
458 int xy
, wrap
, pred
, a
, b
, c
;
460 xy
= s
->block_index
[n
];
466 a
= s
->coded_block
[xy
- 1 ];
467 b
= s
->coded_block
[xy
- 1 - wrap
];
468 c
= s
->coded_block
[xy
- wrap
];
477 *coded_block_ptr
= &s
->coded_block
[xy
];
482 #ifdef CONFIG_ENCODERS
484 static void msmpeg4_encode_motion(MpegEncContext
* s
,
490 /* modulo encoding */
491 /* WARNING : you cannot reach all the MVs even with the modulo
492 encoding. This is a somewhat strange compromise they took !!! */
505 if ((unsigned)mx
>= 64 ||
507 fprintf(stderr
, "error mx=%d my=%d\n", mx
, my
);
509 mv
= &mv_tables
[s
->mv_table_index
];
511 code
= mv
->table_mv_index
[(mx
<< 6) | my
];
514 mv
->table_mv_bits
[code
],
515 mv
->table_mv_code
[code
]);
517 /* escape : code litterally */
518 put_bits(&s
->pb
, 6, mx
);
519 put_bits(&s
->pb
, 6, my
);
523 static inline void handle_slices(MpegEncContext
*s
){
525 if (s
->slice_height
&& (s
->mb_y
% s
->slice_height
) == 0) {
526 if(s
->msmpeg4_version
< 4){
527 ff_mpeg4_clean_buffers(s
);
529 s
->first_slice_line
= 1;
531 s
->first_slice_line
= 0;
536 void msmpeg4_encode_mb(MpegEncContext
* s
,
537 DCTELEM block
[6][64],
538 int motion_x
, int motion_y
)
540 int cbp
, coded_cbp
, i
;
542 uint8_t *coded_block
;
548 set_stat(ST_INTER_MB
);
550 for (i
= 0; i
< 6; i
++) {
551 if (s
->block_last_index
[i
] >= 0)
554 if (s
->use_skip_mb_code
&& (cbp
| motion_x
| motion_y
) == 0) {
555 /* skip macroblock */
556 put_bits(&s
->pb
, 1, 1);
563 if (s
->use_skip_mb_code
)
564 put_bits(&s
->pb
, 1, 0); /* mb coded */
566 if(s
->msmpeg4_version
<=2){
568 v2_mb_type
[cbp
&3][1],
569 v2_mb_type
[cbp
&3][0]);
570 if((cbp
&3) != 3) coded_cbp
= cbp
^ 0x3C;
574 cbpy_tab
[coded_cbp
>>2][1],
575 cbpy_tab
[coded_cbp
>>2][0]);
577 s
->misc_bits
+= get_bits_diff(s
);
579 h263_pred_motion(s
, 0, 0, &pred_x
, &pred_y
);
580 msmpeg4v2_encode_motion(s
, motion_x
- pred_x
);
581 msmpeg4v2_encode_motion(s
, motion_y
- pred_y
);
584 table_mb_non_intra
[cbp
+ 64][1],
585 table_mb_non_intra
[cbp
+ 64][0]);
587 s
->misc_bits
+= get_bits_diff(s
);
590 h263_pred_motion(s
, 0, 0, &pred_x
, &pred_y
);
591 msmpeg4_encode_motion(s
, motion_x
- pred_x
,
595 s
->mv_bits
+= get_bits_diff(s
);
597 for (i
= 0; i
< 6; i
++) {
598 msmpeg4_encode_block(s
, block
[i
], i
);
600 s
->p_tex_bits
+= get_bits_diff(s
);
605 for (i
= 0; i
< 6; i
++) {
607 val
= (s
->block_last_index
[i
] >= 1);
608 cbp
|= val
<< (5 - i
);
610 /* predict value for close blocks only for luma */
611 pred
= coded_block_pred(s
, i
, &coded_block
);
615 coded_cbp
|= val
<< (5 - i
);
619 printf("cbp=%x %x\n", cbp
, coded_cbp
);
622 if(s
->msmpeg4_version
<=2){
623 if (s
->pict_type
== I_TYPE
) {
625 v2_intra_cbpc
[cbp
&3][1], v2_intra_cbpc
[cbp
&3][0]);
627 if (s
->use_skip_mb_code
)
628 put_bits(&s
->pb
, 1, 0); /* mb coded */
630 v2_mb_type
[(cbp
&3) + 4][1],
631 v2_mb_type
[(cbp
&3) + 4][0]);
633 put_bits(&s
->pb
, 1, 0); /* no AC prediction yet */
636 cbpy_tab
[cbp
>>2][0]);
638 if (s
->pict_type
== I_TYPE
) {
639 set_stat(ST_INTRA_MB
);
641 ff_msmp4_mb_i_table
[coded_cbp
][1], ff_msmp4_mb_i_table
[coded_cbp
][0]);
643 if (s
->use_skip_mb_code
)
644 put_bits(&s
->pb
, 1, 0); /* mb coded */
646 table_mb_non_intra
[cbp
][1],
647 table_mb_non_intra
[cbp
][0]);
649 set_stat(ST_INTRA_MB
);
650 put_bits(&s
->pb
, 1, 0); /* no AC prediction yet */
651 if(s
->inter_intra_pred
){
653 put_bits(&s
->pb
, table_inter_intra
[s
->h263_aic_dir
][1], table_inter_intra
[s
->h263_aic_dir
][0]);
656 s
->misc_bits
+= get_bits_diff(s
);
658 for (i
= 0; i
< 6; i
++) {
659 msmpeg4_encode_block(s
, block
[i
], i
);
661 s
->i_tex_bits
+= get_bits_diff(s
);
666 #endif //CONFIG_ENCODERS
668 static inline int msmpeg4v1_pred_dc(MpegEncContext
* s
, int n
,
669 int32_t **dc_val_ptr
)
679 *dc_val_ptr
= &s
->last_dc
[i
];
680 return s
->last_dc
[i
];
683 static int get_dc(uint8_t *src
, int stride
, int scale
)
690 sum
+=src
[x
+ y
*stride
];
693 return FASTDIV((sum
+ (scale
>>1)), scale
);
696 /* dir = 0: left, dir = 1: top prediction */
697 static inline int msmpeg4_pred_dc(MpegEncContext
* s
, int n
,
698 uint16_t **dc_val_ptr
, int *dir_ptr
)
700 int a
, b
, c
, wrap
, pred
, scale
;
703 /* find prediction */
705 scale
= s
->y_dc_scale
;
707 scale
= s
->c_dc_scale
;
710 wrap
= s
->block_wrap
[n
];
711 dc_val
= s
->dc_val
[0] + s
->block_index
[n
];
717 b
= dc_val
[ - 1 - wrap
];
720 if(s
->first_slice_line
&& (n
&2)==0 && s
->msmpeg4_version
<4){
724 /* XXX: the following solution consumes divisions, but it does not
725 necessitate to modify mpegvideo.c. The problem comes from the
726 fact they decided to store the quantized DC (which would lead
727 to problems if Q could vary !) */
728 #if (defined(ARCH_X86) || defined(ARCH_X86_64)) && !defined PIC
730 "movl %3, %%eax \n\t"
731 "shrl $1, %%eax \n\t"
732 "addl %%eax, %2 \n\t"
733 "addl %%eax, %1 \n\t"
734 "addl %0, %%eax \n\t"
736 "movl %%edx, %0 \n\t"
737 "movl %1, %%eax \n\t"
739 "movl %%edx, %1 \n\t"
740 "movl %2, %%eax \n\t"
742 "movl %%edx, %2 \n\t"
743 : "+b" (a
), "+c" (b
), "+D" (c
)
744 : "g" (scale
), "S" (inverse
[scale
])
748 /* #elif defined (ARCH_ALPHA) */
749 /* Divisions are extremely costly on Alpha; optimize the most
750 common case. But they are costly everywhere...
753 a
= (a
+ (8 >> 1)) / 8;
754 b
= (b
+ (8 >> 1)) / 8;
755 c
= (c
+ (8 >> 1)) / 8;
757 a
= FASTDIV((a
+ (scale
>> 1)), scale
);
758 b
= FASTDIV((b
+ (scale
>> 1)), scale
);
759 c
= FASTDIV((c
+ (scale
>> 1)), scale
);
762 /* XXX: WARNING: they did not choose the same test as MPEG4. This
763 is very important ! */
764 if(s
->msmpeg4_version
>3){
765 if(s
->inter_intra_pred
){
776 if (abs(a
- b
) < abs(b
- c
)) {
786 dest
= s
->current_picture
.data
[0] + (((n
>>1) + 2*s
->mb_y
) * 8* wrap
) + ((n
&1) + 2*s
->mb_x
) * 8;
789 dest
= s
->current_picture
.data
[n
-3] + (s
->mb_y
* 8 * wrap
) + s
->mb_x
* 8;
791 if(s
->mb_x
==0) a
= (1024 + (scale
>>1))/scale
;
792 else a
= get_dc(dest
-8, wrap
, scale
*8);
793 if(s
->mb_y
==0) c
= (1024 + (scale
>>1))/scale
;
794 else c
= get_dc(dest
-8*wrap
, wrap
, scale
*8);
796 if (s
->h263_aic_dir
==0) {
799 }else if (s
->h263_aic_dir
==1) {
807 }else if (s
->h263_aic_dir
==2) {
821 if (abs(a
- b
) < abs(b
- c
)) {
830 if (abs(a
- b
) <= abs(b
- c
)) {
839 /* update predictor */
840 *dc_val_ptr
= &dc_val
[0];
846 static void msmpeg4_encode_dc(MpegEncContext
* s
, int level
, int n
, int *dir_ptr
)
851 if(s
->msmpeg4_version
==1){
853 pred
= msmpeg4v1_pred_dc(s
, n
, &dc_val
);
855 /* update predictor */
859 pred
= msmpeg4_pred_dc(s
, n
, &dc_val
, dir_ptr
);
861 /* update predictor */
863 *dc_val
= level
* s
->y_dc_scale
;
865 *dc_val
= level
* s
->c_dc_scale
;
869 /* do the prediction */
872 if(s
->msmpeg4_version
<=2){
875 v2_dc_lum_table
[level
+256][1],
876 v2_dc_lum_table
[level
+256][0]);
879 v2_dc_chroma_table
[level
+256][1],
880 v2_dc_chroma_table
[level
+256][0]);
892 if (s
->dc_table_index
== 0) {
894 put_bits(&s
->pb
, ff_table0_dc_lum
[code
][1], ff_table0_dc_lum
[code
][0]);
896 put_bits(&s
->pb
, ff_table0_dc_chroma
[code
][1], ff_table0_dc_chroma
[code
][0]);
900 put_bits(&s
->pb
, ff_table1_dc_lum
[code
][1], ff_table1_dc_lum
[code
][0]);
902 put_bits(&s
->pb
, ff_table1_dc_chroma
[code
][1], ff_table1_dc_chroma
[code
][0]);
907 put_bits(&s
->pb
, 8, level
);
910 put_bits(&s
->pb
, 1, sign
);
915 /* Encoding of a block. Very similar to MPEG4 except for a different
916 escape coding (same as H263) and more vlc tables.
918 static inline void msmpeg4_encode_block(MpegEncContext
* s
, DCTELEM
* block
, int n
)
920 int level
, run
, last
, i
, j
, last_index
;
921 int last_non_zero
, sign
, slevel
;
922 int code
, run_diff
, dc_pred_dir
;
924 const uint8_t *scantable
;
928 msmpeg4_encode_dc(s
, block
[0], n
, &dc_pred_dir
);
931 rl
= &rl_table
[s
->rl_table_index
];
933 rl
= &rl_table
[3 + s
->rl_chroma_table_index
];
936 scantable
= s
->intra_scantable
.permutated
;
937 set_stat(ST_INTRA_AC
);
940 rl
= &rl_table
[3 + s
->rl_table_index
];
941 if(s
->msmpeg4_version
<=2)
945 scantable
= s
->inter_scantable
.permutated
;
946 set_stat(ST_INTER_AC
);
949 /* recalculate block_last_index for M$ wmv1 */
950 if(s
->msmpeg4_version
>=4 && s
->block_last_index
[n
]>0){
951 for(last_index
=63; last_index
>=0; last_index
--){
952 if(block
[scantable
[last_index
]]) break;
954 s
->block_last_index
[n
]= last_index
;
956 last_index
= s
->block_last_index
[n
];
958 last_non_zero
= i
- 1;
959 for (; i
<= last_index
; i
++) {
963 run
= i
- last_non_zero
- 1;
964 last
= (i
== last_index
);
972 if(level
<=MAX_LEVEL
&& run
<=MAX_RUN
){
973 s
->ac_stats
[s
->mb_intra
][n
>3][level
][run
][last
]++;
977 s
->ac_stats
[s
->mb_intra
][n
>3][40][63][0]++; //esc3 like
979 code
= get_rl_index(rl
, last
, run
, level
);
980 put_bits(&s
->pb
, rl
->table_vlc
[code
][1], rl
->table_vlc
[code
][0]);
984 level1
= level
- rl
->max_level
[last
][run
];
987 code
= get_rl_index(rl
, last
, run
, level1
);
990 put_bits(&s
->pb
, 1, 0);
991 if (level
> MAX_LEVEL
)
993 run1
= run
- rl
->max_run
[last
][level
] - run_diff
;
996 code
= get_rl_index(rl
, last
, run1
, level
);
1000 put_bits(&s
->pb
, 1, 0);
1001 put_bits(&s
->pb
, 1, last
);
1002 if(s
->msmpeg4_version
>=4){
1003 if(s
->esc3_level_length
==0){
1004 s
->esc3_level_length
=8;
1005 s
->esc3_run_length
= 6;
1007 put_bits(&s
->pb
, 6, 3);
1009 put_bits(&s
->pb
, 8, 3);
1011 put_bits(&s
->pb
, s
->esc3_run_length
, run
);
1012 put_bits(&s
->pb
, 1, sign
);
1013 put_bits(&s
->pb
, s
->esc3_level_length
, level
);
1015 put_bits(&s
->pb
, 6, run
);
1016 put_bits(&s
->pb
, 8, slevel
& 0xff);
1020 put_bits(&s
->pb
, 1, 1);
1021 put_bits(&s
->pb
, rl
->table_vlc
[code
][1], rl
->table_vlc
[code
][0]);
1022 put_bits(&s
->pb
, 1, sign
);
1026 put_bits(&s
->pb
, 1, 1);
1027 put_bits(&s
->pb
, rl
->table_vlc
[code
][1], rl
->table_vlc
[code
][0]);
1028 put_bits(&s
->pb
, 1, sign
);
1031 put_bits(&s
->pb
, 1, sign
);
1038 /****************************************/
1039 /* decoding stuff */
1041 static VLC mb_non_intra_vlc
[4];
1042 VLC ff_msmp4_mb_i_vlc
;
1043 VLC ff_msmp4_dc_luma_vlc
[2];
1044 VLC ff_msmp4_dc_chroma_vlc
[2];
1045 static VLC v2_dc_lum_vlc
;
1046 static VLC v2_dc_chroma_vlc
;
1047 static VLC cbpy_vlc
;
1048 static VLC v2_intra_cbpc_vlc
;
1049 static VLC v2_mb_type_vlc
;
1050 static VLC v2_mv_vlc
;
1051 static VLC v1_intra_cbpc_vlc
;
1052 static VLC v1_inter_cbpc_vlc
;
1053 static VLC inter_intra_vlc
;
1055 /* this table is practically identical to the one from h263 except that its inverted */
1056 static void init_h263_dc_for_msmpeg4(void)
1058 int level
, uni_code
, uni_len
;
1060 for(level
=-256; level
<256; level
++){
1062 /* find number of bits */
1071 l
= (-level
) ^ ((1 << size
) - 1);
1075 /* luminance h263 */
1076 uni_code
= DCtab_lum
[size
][0];
1077 uni_len
= DCtab_lum
[size
][1];
1078 uni_code
^= (1<<uni_len
)-1; //M$ doesnt like compatibility
1081 uni_code
<<=size
; uni_code
|=l
;
1084 uni_code
<<=1; uni_code
|=1;
1088 v2_dc_lum_table
[level
+256][0]= uni_code
;
1089 v2_dc_lum_table
[level
+256][1]= uni_len
;
1091 /* chrominance h263 */
1092 uni_code
= DCtab_chrom
[size
][0];
1093 uni_len
= DCtab_chrom
[size
][1];
1094 uni_code
^= (1<<uni_len
)-1; //M$ doesnt like compatibility
1097 uni_code
<<=size
; uni_code
|=l
;
1100 uni_code
<<=1; uni_code
|=1;
1104 v2_dc_chroma_table
[level
+256][0]= uni_code
;
1105 v2_dc_chroma_table
[level
+256][1]= uni_len
;
1110 /* init all vlc decoding tables */
1111 int ff_msmpeg4_decode_init(MpegEncContext
*s
)
1113 static int done
= 0;
1122 for(i
=0;i
<NB_RL_TABLES
;i
++) {
1123 init_rl(&rl_table
[i
], 1);
1124 init_vlc_rl(&rl_table
[i
], 1);
1128 init_vlc(&mv
->vlc
, MV_VLC_BITS
, mv
->n
+ 1,
1129 mv
->table_mv_bits
, 1, 1,
1130 mv
->table_mv_code
, 2, 2, 1);
1133 init_vlc(&ff_msmp4_dc_luma_vlc
[0], DC_VLC_BITS
, 120,
1134 &ff_table0_dc_lum
[0][1], 8, 4,
1135 &ff_table0_dc_lum
[0][0], 8, 4, 1);
1136 init_vlc(&ff_msmp4_dc_chroma_vlc
[0], DC_VLC_BITS
, 120,
1137 &ff_table0_dc_chroma
[0][1], 8, 4,
1138 &ff_table0_dc_chroma
[0][0], 8, 4, 1);
1139 init_vlc(&ff_msmp4_dc_luma_vlc
[1], DC_VLC_BITS
, 120,
1140 &ff_table1_dc_lum
[0][1], 8, 4,
1141 &ff_table1_dc_lum
[0][0], 8, 4, 1);
1142 init_vlc(&ff_msmp4_dc_chroma_vlc
[1], DC_VLC_BITS
, 120,
1143 &ff_table1_dc_chroma
[0][1], 8, 4,
1144 &ff_table1_dc_chroma
[0][0], 8, 4, 1);
1146 init_vlc(&v2_dc_lum_vlc
, DC_VLC_BITS
, 512,
1147 &v2_dc_lum_table
[0][1], 8, 4,
1148 &v2_dc_lum_table
[0][0], 8, 4, 1);
1149 init_vlc(&v2_dc_chroma_vlc
, DC_VLC_BITS
, 512,
1150 &v2_dc_chroma_table
[0][1], 8, 4,
1151 &v2_dc_chroma_table
[0][0], 8, 4, 1);
1153 init_vlc(&cbpy_vlc
, CBPY_VLC_BITS
, 16,
1154 &cbpy_tab
[0][1], 2, 1,
1155 &cbpy_tab
[0][0], 2, 1, 1);
1156 init_vlc(&v2_intra_cbpc_vlc
, V2_INTRA_CBPC_VLC_BITS
, 4,
1157 &v2_intra_cbpc
[0][1], 2, 1,
1158 &v2_intra_cbpc
[0][0], 2, 1, 1);
1159 init_vlc(&v2_mb_type_vlc
, V2_MB_TYPE_VLC_BITS
, 8,
1160 &v2_mb_type
[0][1], 2, 1,
1161 &v2_mb_type
[0][0], 2, 1, 1);
1162 init_vlc(&v2_mv_vlc
, V2_MV_VLC_BITS
, 33,
1164 &mvtab
[0][0], 2, 1, 1);
1167 init_vlc(&mb_non_intra_vlc
[i
], MB_NON_INTRA_VLC_BITS
, 128,
1168 &wmv2_inter_table
[i
][0][1], 8, 4,
1169 &wmv2_inter_table
[i
][0][0], 8, 4, 1); //FIXME name?
1172 init_vlc(&ff_msmp4_mb_i_vlc
, MB_INTRA_VLC_BITS
, 64,
1173 &ff_msmp4_mb_i_table
[0][1], 4, 2,
1174 &ff_msmp4_mb_i_table
[0][0], 4, 2, 1);
1176 init_vlc(&v1_intra_cbpc_vlc
, V1_INTRA_CBPC_VLC_BITS
, 8,
1177 intra_MCBPC_bits
, 1, 1,
1178 intra_MCBPC_code
, 1, 1, 1);
1179 init_vlc(&v1_inter_cbpc_vlc
, V1_INTER_CBPC_VLC_BITS
, 25,
1180 inter_MCBPC_bits
, 1, 1,
1181 inter_MCBPC_code
, 1, 1, 1);
1183 init_vlc(&inter_intra_vlc
, INTER_INTRA_VLC_BITS
, 4,
1184 &table_inter_intra
[0][1], 2, 1,
1185 &table_inter_intra
[0][0], 2, 1, 1);
1188 switch(s
->msmpeg4_version
){
1191 s
->decode_mb
= msmpeg4v12_decode_mb
;
1195 s
->decode_mb
= msmpeg4v34_decode_mb
;
1198 s
->decode_mb
= wmv2_decode_mb
;
1200 //FIXME + TODO VC9 decode mb
1204 s
->slice_height
= s
->mb_height
; //to avoid 1/0 if the first frame isnt a keyframe
1209 int msmpeg4_decode_picture_header(MpegEncContext
* s
)
1216 for(i
=0; i
<s
->gb
.size_in_bits
; i
++)
1217 printf("%d", get_bits1(&s
->gb
));
1218 // get_bits1(&s->gb);
1224 if(s
->msmpeg4_version
==1){
1225 int start_code
, num
;
1226 start_code
= (get_bits(&s
->gb
, 16)<<16) | get_bits(&s
->gb
, 16);
1227 if(start_code
!=0x00000100){
1228 av_log(s
->avctx
, AV_LOG_ERROR
, "invalid startcode\n");
1232 num
= get_bits(&s
->gb
, 5); // frame number */
1235 s
->pict_type
= get_bits(&s
->gb
, 2) + 1;
1236 if (s
->pict_type
!= I_TYPE
&&
1237 s
->pict_type
!= P_TYPE
){
1238 av_log(s
->avctx
, AV_LOG_ERROR
, "invalid picture type\n");
1244 if(s
->pict_type
== I_TYPE
) had_i
=1;
1245 if(!had_i
) return -1;
1248 s
->chroma_qscale
= s
->qscale
= get_bits(&s
->gb
, 5);
1250 av_log(s
->avctx
, AV_LOG_ERROR
, "invalid qscale\n");
1254 if (s
->pict_type
== I_TYPE
) {
1255 code
= get_bits(&s
->gb
, 5);
1256 if(s
->msmpeg4_version
==1){
1257 if(code
==0 || code
>s
->mb_height
){
1258 av_log(s
->avctx
, AV_LOG_ERROR
, "invalid slice height %d\n", code
);
1262 s
->slice_height
= code
;
1264 /* 0x17: one slice, 0x18: two slices, ... */
1266 av_log(s
->avctx
, AV_LOG_ERROR
, "error, slice code was %X\n", code
);
1270 s
->slice_height
= s
->mb_height
/ (code
- 0x16);
1273 switch(s
->msmpeg4_version
){
1276 s
->rl_chroma_table_index
= 2;
1277 s
->rl_table_index
= 2;
1279 s
->dc_table_index
= 0; //not used
1282 s
->rl_chroma_table_index
= decode012(&s
->gb
);
1283 s
->rl_table_index
= decode012(&s
->gb
);
1285 s
->dc_table_index
= get_bits1(&s
->gb
);
1288 msmpeg4_decode_ext_header(s
, (2+5+5+17+7)/8);
1290 if(s
->bit_rate
> MBAC_BITRATE
) s
->per_mb_rl_table
= get_bits1(&s
->gb
);
1291 else s
->per_mb_rl_table
= 0;
1293 if(!s
->per_mb_rl_table
){
1294 s
->rl_chroma_table_index
= decode012(&s
->gb
);
1295 s
->rl_table_index
= decode012(&s
->gb
);
1298 s
->dc_table_index
= get_bits1(&s
->gb
);
1299 s
->inter_intra_pred
= 0;
1303 if(s
->avctx
->debug
&FF_DEBUG_PICT_INFO
)
1304 av_log(s
->avctx
, AV_LOG_DEBUG
, "qscale:%d rlc:%d rl:%d dc:%d mbrl:%d slice:%d \n",
1306 s
->rl_chroma_table_index
,
1312 switch(s
->msmpeg4_version
){
1315 if(s
->msmpeg4_version
==1)
1316 s
->use_skip_mb_code
= 1;
1318 s
->use_skip_mb_code
= get_bits1(&s
->gb
);
1319 s
->rl_table_index
= 2;
1320 s
->rl_chroma_table_index
= s
->rl_table_index
;
1321 s
->dc_table_index
= 0; //not used
1322 s
->mv_table_index
= 0;
1325 s
->use_skip_mb_code
= get_bits1(&s
->gb
);
1326 s
->rl_table_index
= decode012(&s
->gb
);
1327 s
->rl_chroma_table_index
= s
->rl_table_index
;
1329 s
->dc_table_index
= get_bits1(&s
->gb
);
1331 s
->mv_table_index
= get_bits1(&s
->gb
);
1334 s
->use_skip_mb_code
= get_bits1(&s
->gb
);
1336 if(s
->bit_rate
> MBAC_BITRATE
) s
->per_mb_rl_table
= get_bits1(&s
->gb
);
1337 else s
->per_mb_rl_table
= 0;
1339 if(!s
->per_mb_rl_table
){
1340 s
->rl_table_index
= decode012(&s
->gb
);
1341 s
->rl_chroma_table_index
= s
->rl_table_index
;
1344 s
->dc_table_index
= get_bits1(&s
->gb
);
1346 s
->mv_table_index
= get_bits1(&s
->gb
);
1347 s
->inter_intra_pred
= (s
->width
*s
->height
< 320*240 && s
->bit_rate
<=II_BITRATE
);
1351 if(s
->avctx
->debug
&FF_DEBUG_PICT_INFO
)
1352 av_log(s
->avctx
, AV_LOG_DEBUG
, "skip:%d rl:%d rlc:%d dc:%d mv:%d mbrl:%d qp:%d \n",
1353 s
->use_skip_mb_code
,
1355 s
->rl_chroma_table_index
,
1361 if(s
->flipflop_rounding
){
1362 s
->no_rounding
^= 1;
1367 //printf("%d %d %d %d %d\n", s->pict_type, s->bit_rate, s->inter_intra_pred, s->width, s->height);
1369 s
->esc3_level_length
= 0;
1370 s
->esc3_run_length
= 0;
1373 printf("*****frame %d:\n", frame_count
++);
1378 int msmpeg4_decode_ext_header(MpegEncContext
* s
, int buf_size
)
1380 int left
= buf_size
*8 - get_bits_count(&s
->gb
);
1381 int length
= s
->msmpeg4_version
>=3 ? 17 : 16;
1382 /* the alt_bitstream reader could read over the end so we need to check it */
1383 if(left
>=length
&& left
<length
+8)
1387 fps
= get_bits(&s
->gb
, 5);
1388 s
->bit_rate
= get_bits(&s
->gb
, 11)*1024;
1389 if(s
->msmpeg4_version
>=3)
1390 s
->flipflop_rounding
= get_bits1(&s
->gb
);
1392 s
->flipflop_rounding
= 0;
1394 // printf("fps:%2d bps:%2d roundingType:%1d\n", fps, s->bit_rate/1024, s->flipflop_rounding);
1396 else if(left
<length
+8)
1398 s
->flipflop_rounding
= 0;
1399 if(s
->msmpeg4_version
!= 2)
1400 av_log(s
->avctx
, AV_LOG_ERROR
, "ext header missing, %d left\n", left
);
1404 av_log(s
->avctx
, AV_LOG_ERROR
, "I frame too long, ignoring ext header\n");
1410 static inline void msmpeg4_memsetw(short *tab
, int val
, int n
)
1417 static void msmpeg4v2_encode_motion(MpegEncContext
* s
, int val
)
1419 int range
, bit_size
, sign
, code
, bits
;
1424 put_bits(&s
->pb
, mvtab
[code
][1], mvtab
[code
][0]);
1426 bit_size
= s
->f_code
- 1;
1427 range
= 1 << bit_size
;
1440 code
= (val
>> bit_size
) + 1;
1441 bits
= val
& (range
- 1);
1443 put_bits(&s
->pb
, mvtab
[code
][1] + 1, (mvtab
[code
][0] << 1) | sign
);
1445 put_bits(&s
->pb
, bit_size
, bits
);
1450 /* this is identical to h263 except that its range is multiplied by 2 */
1451 static int msmpeg4v2_decode_motion(MpegEncContext
* s
, int pred
, int f_code
)
1453 int code
, val
, sign
, shift
;
1455 code
= get_vlc2(&s
->gb
, v2_mv_vlc
.table
, V2_MV_VLC_BITS
, 2);
1456 // printf("MV code %d at %d %d pred: %d\n", code, s->mb_x,s->mb_y, pred);
1462 sign
= get_bits1(&s
->gb
);
1466 val
= (val
- 1) << shift
;
1467 val
|= get_bits(&s
->gb
, shift
);
1482 static int msmpeg4v12_decode_mb(MpegEncContext
*s
, DCTELEM block
[6][64])
1486 if (s
->pict_type
== P_TYPE
) {
1487 if (s
->use_skip_mb_code
) {
1488 if (get_bits1(&s
->gb
)) {
1492 s
->block_last_index
[i
] = -1;
1493 s
->mv_dir
= MV_DIR_FORWARD
;
1494 s
->mv_type
= MV_TYPE_16X16
;
1502 if(s
->msmpeg4_version
==2)
1503 code
= get_vlc2(&s
->gb
, v2_mb_type_vlc
.table
, V2_MB_TYPE_VLC_BITS
, 1);
1505 code
= get_vlc2(&s
->gb
, v1_inter_cbpc_vlc
.table
, V1_INTER_CBPC_VLC_BITS
, 3);
1506 if(code
<0 || code
>7){
1507 av_log(s
->avctx
, AV_LOG_ERROR
, "cbpc %d invalid at %d %d\n", code
, s
->mb_x
, s
->mb_y
);
1511 s
->mb_intra
= code
>>2;
1516 if(s
->msmpeg4_version
==2)
1517 cbp
= get_vlc2(&s
->gb
, v2_intra_cbpc_vlc
.table
, V2_INTRA_CBPC_VLC_BITS
, 1);
1519 cbp
= get_vlc2(&s
->gb
, v1_intra_cbpc_vlc
.table
, V1_INTRA_CBPC_VLC_BITS
, 1);
1521 av_log(s
->avctx
, AV_LOG_ERROR
, "cbpc %d invalid at %d %d\n", cbp
, s
->mb_x
, s
->mb_y
);
1529 cbpy
= get_vlc2(&s
->gb
, cbpy_vlc
.table
, CBPY_VLC_BITS
, 1);
1531 av_log(s
->avctx
, AV_LOG_ERROR
, "cbpy %d invalid at %d %d\n", cbp
, s
->mb_x
, s
->mb_y
);
1536 if(s
->msmpeg4_version
==1 || (cbp
&3) != 3) cbp
^= 0x3C;
1538 h263_pred_motion(s
, 0, 0, &mx
, &my
);
1539 mx
= msmpeg4v2_decode_motion(s
, mx
, 1);
1540 my
= msmpeg4v2_decode_motion(s
, my
, 1);
1542 s
->mv_dir
= MV_DIR_FORWARD
;
1543 s
->mv_type
= MV_TYPE_16X16
;
1544 s
->mv
[0][0][0] = mx
;
1545 s
->mv
[0][0][1] = my
;
1547 if(s
->msmpeg4_version
==2){
1548 s
->ac_pred
= get_bits1(&s
->gb
);
1549 cbp
|= get_vlc2(&s
->gb
, cbpy_vlc
.table
, CBPY_VLC_BITS
, 1)<<2; //FIXME check errors
1552 cbp
|= get_vlc2(&s
->gb
, cbpy_vlc
.table
, CBPY_VLC_BITS
, 1)<<2; //FIXME check errors
1553 if(s
->pict_type
==P_TYPE
) cbp
^=0x3C;
1557 s
->dsp
.clear_blocks(s
->block
[0]);
1558 for (i
= 0; i
< 6; i
++) {
1559 if (msmpeg4_decode_block(s
, block
[i
], i
, (cbp
>> (5 - i
)) & 1, NULL
) < 0)
1561 av_log(s
->avctx
, AV_LOG_ERROR
, "\nerror while decoding block: %d x %d (%d)\n", s
->mb_x
, s
->mb_y
, i
);
1568 static int msmpeg4v34_decode_mb(MpegEncContext
*s
, DCTELEM block
[6][64])
1572 uint32_t * const mb_type_ptr
= &s
->current_picture
.mb_type
[ s
->mb_x
+ s
->mb_y
*s
->mb_stride
];
1574 if (s
->pict_type
== P_TYPE
) {
1575 set_stat(ST_INTER_MB
);
1576 if (s
->use_skip_mb_code
) {
1577 if (get_bits1(&s
->gb
)) {
1581 s
->block_last_index
[i
] = -1;
1582 s
->mv_dir
= MV_DIR_FORWARD
;
1583 s
->mv_type
= MV_TYPE_16X16
;
1587 *mb_type_ptr
= MB_TYPE_SKIP
| MB_TYPE_L0
| MB_TYPE_16x16
;
1593 code
= get_vlc2(&s
->gb
, mb_non_intra_vlc
[DEFAULT_INTER_INDEX
].table
, MB_NON_INTRA_VLC_BITS
, 3);
1596 //s->mb_intra = (code & 0x40) ? 0 : 1;
1597 s
->mb_intra
= (~code
& 0x40) >> 6;
1601 set_stat(ST_INTRA_MB
);
1603 code
= get_vlc2(&s
->gb
, ff_msmp4_mb_i_vlc
.table
, MB_INTRA_VLC_BITS
, 2);
1606 /* predict coded block pattern */
1609 int val
= ((code
>> (5 - i
)) & 1);
1611 int pred
= coded_block_pred(s
, i
, &coded_val
);
1615 cbp
|= val
<< (5 - i
);
1621 //printf("P at %d %d\n", s->mb_x, s->mb_y);
1622 if(s
->per_mb_rl_table
&& cbp
){
1623 s
->rl_table_index
= decode012(&s
->gb
);
1624 s
->rl_chroma_table_index
= s
->rl_table_index
;
1627 h263_pred_motion(s
, 0, 0, &mx
, &my
);
1628 if (msmpeg4_decode_motion(s
, &mx
, &my
) < 0)
1630 s
->mv_dir
= MV_DIR_FORWARD
;
1631 s
->mv_type
= MV_TYPE_16X16
;
1632 s
->mv
[0][0][0] = mx
;
1633 s
->mv
[0][0][1] = my
;
1634 *mb_type_ptr
= MB_TYPE_L0
| MB_TYPE_16x16
;
1636 //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));
1637 set_stat(ST_INTRA_MB
);
1638 s
->ac_pred
= get_bits1(&s
->gb
);
1639 *mb_type_ptr
= MB_TYPE_INTRA
;
1640 if(s
->inter_intra_pred
){
1641 s
->h263_aic_dir
= get_vlc2(&s
->gb
, inter_intra_vlc
.table
, INTER_INTRA_VLC_BITS
, 1);
1642 // printf("%d%d %d %d/", s->ac_pred, s->h263_aic_dir, s->mb_x, s->mb_y);
1644 if(s
->per_mb_rl_table
&& cbp
){
1645 s
->rl_table_index
= decode012(&s
->gb
);
1646 s
->rl_chroma_table_index
= s
->rl_table_index
;
1650 s
->dsp
.clear_blocks(s
->block
[0]);
1651 for (i
= 0; i
< 6; i
++) {
1652 if (msmpeg4_decode_block(s
, block
[i
], i
, (cbp
>> (5 - i
)) & 1, NULL
) < 0)
1654 av_log(s
->avctx
, AV_LOG_ERROR
, "\nerror while decoding block: %d x %d (%d)\n", s
->mb_x
, s
->mb_y
, i
);
1661 //#define ERROR_DETAILS
1662 static inline int msmpeg4_decode_block(MpegEncContext
* s
, DCTELEM
* block
,
1663 int n
, int coded
, const uint8_t *scan_table
)
1665 int level
, i
, last
, run
, run_diff
;
1668 RL_VLC_ELEM
*rl_vlc
;
1677 level
= msmpeg4_decode_dc(s
, n
, &dc_pred_dir
);
1680 av_log(s
->avctx
, AV_LOG_ERROR
, "dc overflow- block: %d qscale: %d//\n", n
, s
->qscale
);
1681 if(s
->inter_intra_pred
) level
=0;
1685 rl
= &rl_table
[s
->rl_table_index
];
1686 if(level
> 256*s
->y_dc_scale
){
1687 av_log(s
->avctx
, AV_LOG_ERROR
, "dc overflow+ L qscale: %d//\n", s
->qscale
);
1688 if(!s
->inter_intra_pred
) return -1;
1691 rl
= &rl_table
[3 + s
->rl_chroma_table_index
];
1692 if(level
> 256*s
->c_dc_scale
){
1693 av_log(s
->avctx
, AV_LOG_ERROR
, "dc overflow+ C qscale: %d//\n", s
->qscale
);
1694 if(!s
->inter_intra_pred
) return -1;
1705 if (dc_pred_dir
== 0)
1706 scan_table
= s
->intra_v_scantable
.permutated
; /* left */
1708 scan_table
= s
->intra_h_scantable
.permutated
; /* top */
1710 scan_table
= s
->intra_scantable
.permutated
;
1712 set_stat(ST_INTRA_AC
);
1713 rl_vlc
= rl
->rl_vlc
[0];
1715 qmul
= s
->qscale
<< 1;
1716 qadd
= (s
->qscale
- 1) | 1;
1718 rl
= &rl_table
[3 + s
->rl_table_index
];
1720 if(s
->msmpeg4_version
==2)
1726 s
->block_last_index
[n
] = i
;
1730 scan_table
= s
->inter_scantable
.permutated
;
1731 set_stat(ST_INTER_AC
);
1732 rl_vlc
= rl
->rl_vlc
[s
->qscale
];
1735 OPEN_READER(re
, &s
->gb
);
1737 UPDATE_CACHE(re
, &s
->gb
);
1738 GET_RL_VLC(level
, run
, re
, &s
->gb
, rl_vlc
, TEX_VLC_BITS
, 2, 0);
1741 cache
= GET_CACHE(re
, &s
->gb
);
1743 if (s
->msmpeg4_version
==1 || (cache
&0x80000000)==0) {
1744 if (s
->msmpeg4_version
==1 || (cache
&0x40000000)==0) {
1746 if(s
->msmpeg4_version
!=1) LAST_SKIP_BITS(re
, &s
->gb
, 2);
1747 UPDATE_CACHE(re
, &s
->gb
);
1748 if(s
->msmpeg4_version
<=3){
1749 last
= SHOW_UBITS(re
, &s
->gb
, 1); SKIP_CACHE(re
, &s
->gb
, 1);
1750 run
= SHOW_UBITS(re
, &s
->gb
, 6); SKIP_CACHE(re
, &s
->gb
, 6);
1751 level
= SHOW_SBITS(re
, &s
->gb
, 8); LAST_SKIP_CACHE(re
, &s
->gb
, 8);
1752 SKIP_COUNTER(re
, &s
->gb
, 1+6+8);
1755 last
= SHOW_UBITS(re
, &s
->gb
, 1); SKIP_BITS(re
, &s
->gb
, 1);
1756 if(!s
->esc3_level_length
){
1758 //printf("ESC-3 %X at %d %d\n", show_bits(&s->gb, 24), s->mb_x, s->mb_y);
1760 ll
= SHOW_UBITS(re
, &s
->gb
, 3); SKIP_BITS(re
, &s
->gb
, 3);
1762 if(SHOW_UBITS(re
, &s
->gb
, 1)) av_log(s
->avctx
, AV_LOG_ERROR
, "cool a new vlc code ,contact the ffmpeg developers and upload the file\n");
1763 SKIP_BITS(re
, &s
->gb
, 1);
1768 while(ll
<8 && SHOW_UBITS(re
, &s
->gb
, 1)==0){
1770 SKIP_BITS(re
, &s
->gb
, 1);
1772 if(ll
<8) SKIP_BITS(re
, &s
->gb
, 1);
1775 s
->esc3_level_length
= ll
;
1776 s
->esc3_run_length
= SHOW_UBITS(re
, &s
->gb
, 2) + 3; SKIP_BITS(re
, &s
->gb
, 2);
1777 //printf("level length:%d, run length: %d\n", ll, s->esc3_run_length);
1778 UPDATE_CACHE(re
, &s
->gb
);
1780 run
= SHOW_UBITS(re
, &s
->gb
, s
->esc3_run_length
);
1781 SKIP_BITS(re
, &s
->gb
, s
->esc3_run_length
);
1783 sign
= SHOW_UBITS(re
, &s
->gb
, 1);
1784 SKIP_BITS(re
, &s
->gb
, 1);
1786 level
= SHOW_UBITS(re
, &s
->gb
, s
->esc3_level_length
);
1787 SKIP_BITS(re
, &s
->gb
, s
->esc3_level_length
);
1788 if(sign
) level
= -level
;
1790 //printf("level: %d, run: %d at %d %d\n", level, run, s->mb_x, s->mb_y);
1791 #if 0 // waste of time / this will detect very few errors
1793 const int abs_level
= ABS(level
);
1794 const int run1
= run
- rl
->max_run
[last
][abs_level
] - run_diff
;
1795 if(abs_level
<=MAX_LEVEL
&& run
<=MAX_RUN
){
1796 if(abs_level
<= rl
->max_level
[last
][run
]){
1797 fprintf(stderr
, "illegal 3. esc, vlc encoding possible\n");
1798 return DECODING_AC_LOST
;
1800 if(abs_level
<= rl
->max_level
[last
][run
]*2){
1801 fprintf(stderr
, "illegal 3. esc, esc 1 encoding possible\n");
1802 return DECODING_AC_LOST
;
1804 if(run1
>=0 && abs_level
<= rl
->max_level
[last
][run1
]){
1805 fprintf(stderr
, "illegal 3. esc, esc 2 encoding possible\n");
1806 return DECODING_AC_LOST
;
1811 //level = level * qmul + (level>0) * qadd - (level<=0) * qadd ;
1812 if (level
>0) level
= level
* qmul
+ qadd
;
1813 else level
= level
* qmul
- qadd
;
1814 #if 0 // waste of time too :(
1815 if(level
>2048 || level
<-2048){
1816 fprintf(stderr
, "|level| overflow in 3. esc\n");
1817 return DECODING_AC_LOST
;
1822 #ifdef ERROR_DETAILS
1824 fprintf(stderr
, "illegal vlc code in ESC3 level=%d\n", level
);
1825 else if((i
>62 && i
<192) || i
>192+63)
1826 fprintf(stderr
, "run overflow in ESC3 i=%d run=%d level=%d\n", i
, run
, level
);
1830 #if MIN_CACHE_BITS < 23
1831 LAST_SKIP_BITS(re
, &s
->gb
, 2);
1832 UPDATE_CACHE(re
, &s
->gb
);
1834 SKIP_BITS(re
, &s
->gb
, 2);
1836 GET_RL_VLC(level
, run
, re
, &s
->gb
, rl_vlc
, TEX_VLC_BITS
, 2, 1);
1837 i
+= run
+ rl
->max_run
[run
>>7][level
/qmul
] + run_diff
; //FIXME opt indexing
1838 level
= (level
^ SHOW_SBITS(re
, &s
->gb
, 1)) - SHOW_SBITS(re
, &s
->gb
, 1);
1839 LAST_SKIP_BITS(re
, &s
->gb
, 1);
1840 #ifdef ERROR_DETAILS
1842 fprintf(stderr
, "illegal vlc code in ESC2 level=%d\n", level
);
1843 else if((i
>62 && i
<192) || i
>192+63)
1844 fprintf(stderr
, "run overflow in ESC2 i=%d run=%d level=%d\n", i
, run
, level
);
1849 #if MIN_CACHE_BITS < 22
1850 LAST_SKIP_BITS(re
, &s
->gb
, 1);
1851 UPDATE_CACHE(re
, &s
->gb
);
1853 SKIP_BITS(re
, &s
->gb
, 1);
1855 GET_RL_VLC(level
, run
, re
, &s
->gb
, rl_vlc
, TEX_VLC_BITS
, 2, 1);
1857 level
= level
+ rl
->max_level
[run
>>7][(run
-1)&63] * qmul
;//FIXME opt indexing
1858 level
= (level
^ SHOW_SBITS(re
, &s
->gb
, 1)) - SHOW_SBITS(re
, &s
->gb
, 1);
1859 LAST_SKIP_BITS(re
, &s
->gb
, 1);
1860 #ifdef ERROR_DETAILS
1862 fprintf(stderr
, "illegal vlc code in ESC1 level=%d\n", level
);
1863 else if((i
>62 && i
<192) || i
>192+63)
1864 fprintf(stderr
, "run overflow in ESC1 i=%d run=%d level=%d\n", i
, run
, level
);
1869 level
= (level
^ SHOW_SBITS(re
, &s
->gb
, 1)) - SHOW_SBITS(re
, &s
->gb
, 1);
1870 LAST_SKIP_BITS(re
, &s
->gb
, 1);
1871 #ifdef ERROR_DETAILS
1873 fprintf(stderr
, "illegal vlc code level=%d\n", level
);
1874 else if((i
>62 && i
<192) || i
>192+63)
1875 fprintf(stderr
, "run overflow i=%d run=%d level=%d\n", i
, run
, level
);
1881 const int left
= s
->gb
.size_in_bits
- get_bits_count(&s
->gb
);
1882 if(((i
+192 == 64 && level
/qmul
==-1) || s
->error_resilience
<=1) && left
>=0){
1883 av_log(s
->avctx
, AV_LOG_ERROR
, "ignoring overflow at %d %d\n", s
->mb_x
, s
->mb_y
);
1886 av_log(s
->avctx
, AV_LOG_ERROR
, "ac-tex damaged at %d %d\n", s
->mb_x
, s
->mb_y
);
1891 block
[scan_table
[i
]] = level
;
1895 block
[scan_table
[i
]] = level
;
1897 CLOSE_READER(re
, &s
->gb
);
1901 mpeg4_pred_ac(s
, block
, n
, dc_pred_dir
);
1903 i
= 63; /* XXX: not optimal */
1906 if(s
->msmpeg4_version
>=4 && i
>0) i
=63; //FIXME/XXX optimize
1907 s
->block_last_index
[n
] = i
;
1912 static int msmpeg4_decode_dc(MpegEncContext
* s
, int n
, int *dir_ptr
)
1916 if(s
->msmpeg4_version
<=2){
1918 level
= get_vlc2(&s
->gb
, v2_dc_lum_vlc
.table
, DC_VLC_BITS
, 3);
1920 level
= get_vlc2(&s
->gb
, v2_dc_chroma_vlc
.table
, DC_VLC_BITS
, 3);
1925 }else{ //FIXME optimize use unified tables & index
1927 level
= get_vlc2(&s
->gb
, ff_msmp4_dc_luma_vlc
[s
->dc_table_index
].table
, DC_VLC_BITS
, 3);
1929 level
= get_vlc2(&s
->gb
, ff_msmp4_dc_chroma_vlc
[s
->dc_table_index
].table
, DC_VLC_BITS
, 3);
1932 av_log(s
->avctx
, AV_LOG_ERROR
, "illegal dc vlc\n");
1936 if (level
== DC_MAX
) {
1937 level
= get_bits(&s
->gb
, 8);
1938 if (get_bits1(&s
->gb
))
1940 } else if (level
!= 0) {
1941 if (get_bits1(&s
->gb
))
1946 if(s
->msmpeg4_version
==1){
1948 pred
= msmpeg4v1_pred_dc(s
, n
, &dc_val
);
1951 /* update predictor */
1955 pred
= msmpeg4_pred_dc(s
, n
, &dc_val
, dir_ptr
);
1958 /* update predictor */
1960 *dc_val
= level
* s
->y_dc_scale
;
1962 *dc_val
= level
* s
->c_dc_scale
;
1969 static int msmpeg4_decode_motion(MpegEncContext
* s
,
1970 int *mx_ptr
, int *my_ptr
)
1975 mv
= &mv_tables
[s
->mv_table_index
];
1977 code
= get_vlc2(&s
->gb
, mv
->vlc
.table
, MV_VLC_BITS
, 2);
1979 av_log(s
->avctx
, AV_LOG_ERROR
, "illegal MV code at %d %d\n", s
->mb_x
, s
->mb_y
);
1982 if (code
== mv
->n
) {
1983 //printf("MV ESC %X at %d %d\n", show_bits(&s->gb, 24), s->mb_x, s->mb_y);
1984 mx
= get_bits(&s
->gb
, 6);
1985 my
= get_bits(&s
->gb
, 6);
1987 mx
= mv
->table_mvx
[code
];
1988 my
= mv
->table_mvy
[code
];
1993 /* WARNING : they do not do exactly modulo encoding */
2008 /* cleanest way to support it
2009 * there is too much shared between versions so that we cant have 1 file per version & 1 common
2010 * as allmost everything would be in the common file