2 * MSMPEG4 backend for ffmpeg encoder and decoder
3 * Copyright (c) 2001 Fabrice Bellard.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 * msmpeg4v1 & v2 stuff by Michael Niedermayer <michaelni@gmx.at>
24 * MSMPEG4 backend for ffmpeg encoder and decoder.
29 #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 #ifdef CONFIG_ENCODERS
63 static inline void msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n);
64 #endif //CONFIG_ENCODERS
65 static inline int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
66 int n, int coded, const uint8_t *scantable);
67 static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr);
68 static int msmpeg4_decode_motion(MpegEncContext * s,
69 int *mx_ptr, int *my_ptr);
70 #ifdef CONFIG_ENCODERS
71 static void msmpeg4v2_encode_motion(MpegEncContext * s, int val);
72 #endif //CONFIG_ENCODERS
73 static void init_h263_dc_for_msmpeg4(void);
74 static inline void msmpeg4_memsetw(short *tab, int val, int n);
75 #ifdef CONFIG_ENCODERS
76 static int get_size_of_code(MpegEncContext * s, RLTable *rl, int last, int run, int level, int intra);
77 #endif //CONFIG_ENCODERS
78 static int msmpeg4v12_decode_mb(MpegEncContext *s, DCTELEM block[6][64]);
79 static int msmpeg4v34_decode_mb(MpegEncContext *s, DCTELEM block[6][64]);
80 static int wmv2_decode_mb(MpegEncContext *s, DCTELEM block[6][64]);
82 extern uint32_t inverse[256];
90 #include "msmpeg4data.h"
92 static uint8_t rl_length[NB_RL_TABLES][MAX_LEVEL+1][MAX_RUN+1][2];
96 const char *st_names[ST_NB] = {
106 int st_current_index = 0;
107 unsigned int st_bit_counts[ST_NB];
108 unsigned int st_out_bit_counts[ST_NB];
110 #define set_stat(var) st_current_index = var;
112 void print_stats(void)
120 total += st_bit_counts[i];
123 for(i=0;i<ST_NB;i++) {
124 printf("%-10s : %10.1f %5.1f%%\n",
126 (double)st_bit_counts[i] / 8.0,
127 (double)st_bit_counts[i] * 100.0 / total);
129 printf("%-10s : %10.1f %5.1f%%\n",
137 total += st_out_bit_counts[i];
140 for(i=0;i<ST_NB;i++) {
141 printf("%-10s : %10.1f %5.1f%%\n",
143 (double)st_out_bit_counts[i] / 8.0,
144 (double)st_out_bit_counts[i] * 100.0 / total);
146 printf("%-10s : %10.1f %5.1f%%\n",
154 #define set_stat(var)
158 static void common_init(MpegEncContext * s)
162 switch(s->msmpeg4_version){
166 s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
169 if(s->workaround_bugs){
170 s->y_dc_scale_table= old_ff_y_dc_scale_table;
171 s->c_dc_scale_table= old_ff_c_dc_scale_table;
173 s->y_dc_scale_table= ff_mpeg4_y_dc_scale_table;
174 s->c_dc_scale_table= ff_mpeg4_c_dc_scale_table;
179 s->y_dc_scale_table= wmv1_y_dc_scale_table;
180 s->c_dc_scale_table= wmv1_c_dc_scale_table;
185 if(s->msmpeg4_version>=4){
186 ff_init_scantable(s, &s->intra_scantable , wmv1_scantable[1]);
187 ff_init_scantable(s, &s->intra_h_scantable, wmv1_scantable[2]);
188 ff_init_scantable(s, &s->intra_v_scantable, wmv1_scantable[3]);
189 ff_init_scantable(s, &s->inter_scantable , wmv1_scantable[0]);
191 //Note the default tables are set in common_init in mpegvideo.c
196 init_h263_dc_for_msmpeg4();
200 #ifdef CONFIG_ENCODERS
202 /* build the table which associate a (x,y) motion vector to a vlc */
203 static void init_mv_table(MVTable *tab)
207 tab->table_mv_index = av_malloc(sizeof(uint16_t) * 4096);
208 /* mark all entries as not used */
210 tab->table_mv_index[i] = tab->n;
212 for(i=0;i<tab->n;i++) {
213 x = tab->table_mvx[i];
214 y = tab->table_mvy[i];
215 tab->table_mv_index[(x << 6) | y] = i;
219 static void code012(PutBitContext *pb, int n)
225 put_bits(pb, 1, (n >= 2));
229 void ff_msmpeg4_encode_init(MpegEncContext *s)
231 static int init_done=0;
235 if(s->msmpeg4_version>=4){
241 /* init various encoding tables */
243 init_mv_table(&mv_tables[0]);
244 init_mv_table(&mv_tables[1]);
245 for(i=0;i<NB_RL_TABLES;i++)
246 init_rl(&rl_table[i]);
248 for(i=0; i<NB_RL_TABLES; i++){
250 for(level=0; level<=MAX_LEVEL; level++){
252 for(run=0; run<=MAX_RUN; run++){
254 for(last=0; last<2; last++){
255 rl_length[i][level][run][last]= get_size_of_code(s, &rl_table[ i], last, run, level, 0);
263 static int get_size_of_code(MpegEncContext * s, RLTable *rl, int last, int run, int level, int intra){
266 int run_diff= intra ? 0 : 1;
268 code = get_rl_index(rl, last, run, level);
269 size+= rl->table_vlc[code][1];
273 level1 = level - rl->max_level[last][run];
276 code = get_rl_index(rl, last, run, level1);
280 if (level > MAX_LEVEL)
282 run1 = run - rl->max_run[last][level] - run_diff;
285 code = get_rl_index(rl, last, run1, level);
292 size+= 1+1+ rl->table_vlc[code][1];
296 size+= 1+1+ rl->table_vlc[code][1];
304 static void find_best_tables(MpegEncContext * s)
307 int best =-1, best_size =9999999;
308 int chroma_best=-1, best_chroma_size=9999999;
319 for(level=0; level<=MAX_LEVEL; level++){
321 for(run=0; run<=MAX_RUN; run++){
323 const int last_size= size + chroma_size;
324 for(last=0; last<2; last++){
325 int inter_count = s->ac_stats[0][0][level][run][last] + s->ac_stats[0][1][level][run][last];
326 int intra_luma_count = s->ac_stats[1][0][level][run][last];
327 int intra_chroma_count= s->ac_stats[1][1][level][run][last];
329 if(s->pict_type==I_TYPE){
330 size += intra_luma_count *rl_length[i ][level][run][last];
331 chroma_size+= intra_chroma_count*rl_length[i+3][level][run][last];
333 size+= intra_luma_count *rl_length[i ][level][run][last]
334 +intra_chroma_count*rl_length[i+3][level][run][last]
335 +inter_count *rl_length[i+3][level][run][last];
338 if(last_size == size+chroma_size) break;
345 if(chroma_size<best_chroma_size){
346 best_chroma_size= chroma_size;
351 // printf("type:%d, best:%d, qp:%d, var:%d, mcvar:%d, size:%d //\n",
352 // s->pict_type, best, s->qscale, s->mb_var_sum, s->mc_mb_var_sum, best_size);
354 if(s->pict_type==P_TYPE) chroma_best= best;
356 memset(s->ac_stats, 0, sizeof(int)*(MAX_LEVEL+1)*(MAX_RUN+1)*2*2*2);
358 s->rl_table_index = best;
359 s->rl_chroma_table_index= chroma_best;
361 if(s->pict_type != s->last_non_b_pict_type){
362 s->rl_table_index= 2;
363 if(s->pict_type==I_TYPE)
364 s->rl_chroma_table_index= 1;
366 s->rl_chroma_table_index= 2;
371 /* write MSMPEG4 compatible frame header */
372 void msmpeg4_encode_picture_header(MpegEncContext * s, int picture_number)
376 align_put_bits(&s->pb);
377 put_bits(&s->pb, 2, s->pict_type - 1);
379 put_bits(&s->pb, 5, s->qscale);
380 if(s->msmpeg4_version<=2){
381 s->rl_table_index = 2;
382 s->rl_chroma_table_index = 2;
385 s->dc_table_index = 1;
386 s->mv_table_index = 1; /* only if P frame */
387 s->use_skip_mb_code = 1; /* only if P frame */
388 s->per_mb_rl_table = 0;
389 if(s->msmpeg4_version==4)
390 s->inter_intra_pred= (s->width*s->height < 320*240 && s->bit_rate<=II_BITRATE && s->pict_type==P_TYPE);
391 //printf("%d %d %d %d %d\n", s->pict_type, s->bit_rate, s->inter_intra_pred, s->width, s->height);
393 if (s->pict_type == I_TYPE) {
394 s->slice_height= s->mb_height/1;
395 put_bits(&s->pb, 5, 0x16 + s->mb_height/s->slice_height);
397 if(s->msmpeg4_version==4){
398 msmpeg4_encode_ext_header(s);
399 if(s->bit_rate>MBAC_BITRATE)
400 put_bits(&s->pb, 1, s->per_mb_rl_table);
403 if(s->msmpeg4_version>2){
404 if(!s->per_mb_rl_table){
405 code012(&s->pb, s->rl_chroma_table_index);
406 code012(&s->pb, s->rl_table_index);
409 put_bits(&s->pb, 1, s->dc_table_index);
412 put_bits(&s->pb, 1, s->use_skip_mb_code);
414 if(s->msmpeg4_version==4 && s->bit_rate>MBAC_BITRATE)
415 put_bits(&s->pb, 1, s->per_mb_rl_table);
417 if(s->msmpeg4_version>2){
418 if(!s->per_mb_rl_table)
419 code012(&s->pb, s->rl_table_index);
421 put_bits(&s->pb, 1, s->dc_table_index);
423 put_bits(&s->pb, 1, s->mv_table_index);
427 s->esc3_level_length= 0;
428 s->esc3_run_length= 0;
432 printf("*****frame %d:\n", frame_count++);
436 void msmpeg4_encode_ext_header(MpegEncContext * s)
438 put_bits(&s->pb, 5, s->avctx->frame_rate / s->avctx->frame_rate_base); //yes 29.97 -> 29
440 put_bits(&s->pb, 11, FFMIN(s->bit_rate/1024, 2047));
442 if(s->msmpeg4_version<3)
443 s->flipflop_rounding=0;
445 s->flipflop_rounding=1;
446 put_bits(&s->pb, 1, s->flipflop_rounding);
450 #endif //CONFIG_ENCODERS
452 /* predict coded block */
453 static inline int coded_block_pred(MpegEncContext * s, int n, uint8_t **coded_block_ptr)
455 int xy, wrap, pred, a, b, c;
457 xy = s->block_index[n];
458 wrap = s->block_wrap[0];
463 a = s->coded_block[xy - 1 ];
464 b = s->coded_block[xy - 1 - wrap];
465 c = s->coded_block[xy - wrap];
474 *coded_block_ptr = &s->coded_block[xy];
479 #ifdef CONFIG_ENCODERS
481 static void msmpeg4_encode_motion(MpegEncContext * s,
487 /* modulo encoding */
488 /* WARNING : you cannot reach all the MVs even with the modulo
489 encoding. This is a somewhat strange compromise they took !!! */
502 if ((unsigned)mx >= 64 ||
504 fprintf(stderr, "error mx=%d my=%d\n", mx, my);
506 mv = &mv_tables[s->mv_table_index];
508 code = mv->table_mv_index[(mx << 6) | my];
511 mv->table_mv_bits[code],
512 mv->table_mv_code[code]);
514 /* escape : code litterally */
515 put_bits(&s->pb, 6, mx);
516 put_bits(&s->pb, 6, my);
520 static inline void handle_slices(MpegEncContext *s){
522 if (s->slice_height && (s->mb_y % s->slice_height) == 0) {
523 if(s->msmpeg4_version < 4){
524 ff_mpeg4_clean_buffers(s);
526 s->first_slice_line = 1;
528 s->first_slice_line = 0;
533 void msmpeg4_encode_mb(MpegEncContext * s,
534 DCTELEM block[6][64],
535 int motion_x, int motion_y)
537 int cbp, coded_cbp, i;
539 uint8_t *coded_block;
545 set_stat(ST_INTER_MB);
547 for (i = 0; i < 6; i++) {
548 if (s->block_last_index[i] >= 0)
551 if (s->use_skip_mb_code && (cbp | motion_x | motion_y) == 0) {
552 /* skip macroblock */
553 put_bits(&s->pb, 1, 1);
556 if (s->use_skip_mb_code)
557 put_bits(&s->pb, 1, 0); /* mb coded */
559 if(s->msmpeg4_version<=2){
561 v2_mb_type[cbp&3][1],
562 v2_mb_type[cbp&3][0]);
563 if((cbp&3) != 3) coded_cbp= cbp ^ 0x3C;
567 cbpy_tab[coded_cbp>>2][1],
568 cbpy_tab[coded_cbp>>2][0]);
570 h263_pred_motion(s, 0, &pred_x, &pred_y);
571 msmpeg4v2_encode_motion(s, motion_x - pred_x);
572 msmpeg4v2_encode_motion(s, motion_y - pred_y);
575 table_mb_non_intra[cbp + 64][1],
576 table_mb_non_intra[cbp + 64][0]);
579 h263_pred_motion(s, 0, &pred_x, &pred_y);
580 msmpeg4_encode_motion(s, motion_x - pred_x,
587 for (i = 0; i < 6; i++) {
589 val = (s->block_last_index[i] >= 1);
590 cbp |= val << (5 - i);
592 /* predict value for close blocks only for luma */
593 pred = coded_block_pred(s, i, &coded_block);
597 coded_cbp |= val << (5 - i);
601 printf("cbp=%x %x\n", cbp, coded_cbp);
604 if(s->msmpeg4_version<=2){
605 if (s->pict_type == I_TYPE) {
607 v2_intra_cbpc[cbp&3][1], v2_intra_cbpc[cbp&3][0]);
609 if (s->use_skip_mb_code)
610 put_bits(&s->pb, 1, 0); /* mb coded */
612 v2_mb_type[(cbp&3) + 4][1],
613 v2_mb_type[(cbp&3) + 4][0]);
615 put_bits(&s->pb, 1, 0); /* no AC prediction yet */
618 cbpy_tab[cbp>>2][0]);
620 if (s->pict_type == I_TYPE) {
621 set_stat(ST_INTRA_MB);
623 table_mb_intra[coded_cbp][1], table_mb_intra[coded_cbp][0]);
625 if (s->use_skip_mb_code)
626 put_bits(&s->pb, 1, 0); /* mb coded */
628 table_mb_non_intra[cbp][1],
629 table_mb_non_intra[cbp][0]);
631 set_stat(ST_INTRA_MB);
632 put_bits(&s->pb, 1, 0); /* no AC prediction yet */
633 if(s->inter_intra_pred){
635 put_bits(&s->pb, table_inter_intra[s->h263_aic_dir][1], table_inter_intra[s->h263_aic_dir][0]);
640 for (i = 0; i < 6; i++) {
641 msmpeg4_encode_block(s, block[i], i);
645 #endif //CONFIG_ENCODERS
647 /* old ffmpeg msmpeg4v3 mode */
648 static void ff_old_msmpeg4_dc_scale(MpegEncContext * s)
653 }else if (s->qscale < 9){
654 s->y_dc_scale = 2 * s->qscale;
655 s->c_dc_scale = (s->qscale + 13)>>1;
657 s->y_dc_scale = s->qscale + 8;
658 s->c_dc_scale = (s->qscale + 13)>>1;
662 static inline int msmpeg4v1_pred_dc(MpegEncContext * s, int n,
663 int32_t **dc_val_ptr)
673 *dc_val_ptr= &s->last_dc[i];
674 return s->last_dc[i];
677 static int get_dc(uint8_t *src, int stride, int scale)
684 sum+=src[x + y*stride];
687 return (sum + (scale>>1))/scale;
690 /* dir = 0: left, dir = 1: top prediction */
691 static inline int msmpeg4_pred_dc(MpegEncContext * s, int n,
692 uint16_t **dc_val_ptr, int *dir_ptr)
694 int a, b, c, wrap, pred, scale;
697 /* find prediction */
699 scale = s->y_dc_scale;
701 scale = s->c_dc_scale;
704 wrap = s->block_wrap[n];
705 dc_val= s->dc_val[0] + s->block_index[n];
711 b = dc_val[ - 1 - wrap];
714 if(s->first_slice_line && (n&2)==0 && s->msmpeg4_version<4){
718 /* XXX: the following solution consumes divisions, but it does not
719 necessitate to modify mpegvideo.c. The problem comes from the
720 fact they decided to store the quantized DC (which would lead
721 to problems if Q could vary !) */
722 #if defined ARCH_X86 && !defined PIC
724 "movl %3, %%eax \n\t"
725 "shrl $1, %%eax \n\t"
726 "addl %%eax, %2 \n\t"
727 "addl %%eax, %1 \n\t"
728 "addl %0, %%eax \n\t"
730 "movl %%edx, %0 \n\t"
731 "movl %1, %%eax \n\t"
733 "movl %%edx, %1 \n\t"
734 "movl %2, %%eax \n\t"
736 "movl %%edx, %2 \n\t"
737 : "+b" (a), "+c" (b), "+D" (c)
738 : "g" (scale), "S" (inverse[scale])
742 /* #elif defined (ARCH_ALPHA) */
743 /* Divisions are extremely costly on Alpha; optimize the most
744 common case. But they are costly everywhere...
747 a = (a + (8 >> 1)) / 8;
748 b = (b + (8 >> 1)) / 8;
749 c = (c + (8 >> 1)) / 8;
751 a = (a + (scale >> 1)) / scale;
752 b = (b + (scale >> 1)) / scale;
753 c = (c + (scale >> 1)) / scale;
756 /* XXX: WARNING: they did not choose the same test as MPEG4. This
757 is very important ! */
758 if(s->msmpeg4_version>3){
759 if(s->inter_intra_pred){
770 if (abs(a - b) < abs(b - c)) {
780 dest= s->current_picture.data[0] + (((n>>1) + 2*s->mb_y) * 8* wrap ) + ((n&1) + 2*s->mb_x) * 8;
783 dest= s->current_picture.data[n-3] + (s->mb_y * 8 * wrap) + s->mb_x * 8;
785 if(s->mb_x==0) a= (1024 + (scale>>1))/scale;
786 else a= get_dc(dest-8, wrap, scale*8);
787 if(s->mb_y==0) c= (1024 + (scale>>1))/scale;
788 else c= get_dc(dest-8*wrap, wrap, scale*8);
790 if (s->h263_aic_dir==0) {
793 }else if (s->h263_aic_dir==1) {
801 }else if (s->h263_aic_dir==2) {
815 if (abs(a - b) < abs(b - c)) {
824 if (abs(a - b) <= abs(b - c)) {
833 /* update predictor */
834 *dc_val_ptr = &dc_val[0];
840 #ifdef CONFIG_ENCODERS
842 static void msmpeg4_encode_dc(MpegEncContext * s, int level, int n, int *dir_ptr)
847 if(s->msmpeg4_version==1){
849 pred = msmpeg4v1_pred_dc(s, n, &dc_val);
851 /* update predictor */
855 pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr);
857 /* update predictor */
859 *dc_val = level * s->y_dc_scale;
861 *dc_val = level * s->c_dc_scale;
865 /* do the prediction */
868 if(s->msmpeg4_version<=2){
871 v2_dc_lum_table[level+256][1],
872 v2_dc_lum_table[level+256][0]);
875 v2_dc_chroma_table[level+256][1],
876 v2_dc_chroma_table[level+256][0]);
888 if (s->dc_table_index == 0) {
890 put_bits(&s->pb, table0_dc_lum[code][1], table0_dc_lum[code][0]);
892 put_bits(&s->pb, table0_dc_chroma[code][1], table0_dc_chroma[code][0]);
896 put_bits(&s->pb, table1_dc_lum[code][1], table1_dc_lum[code][0]);
898 put_bits(&s->pb, table1_dc_chroma[code][1], table1_dc_chroma[code][0]);
903 put_bits(&s->pb, 8, level);
906 put_bits(&s->pb, 1, sign);
911 /* Encoding of a block. Very similar to MPEG4 except for a different
912 escape coding (same as H263) and more vlc tables.
914 static inline void msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n)
916 int level, run, last, i, j, last_index;
917 int last_non_zero, sign, slevel;
918 int code, run_diff, dc_pred_dir;
920 const uint8_t *scantable;
924 msmpeg4_encode_dc(s, block[0], n, &dc_pred_dir);
927 rl = &rl_table[s->rl_table_index];
929 rl = &rl_table[3 + s->rl_chroma_table_index];
932 scantable= s->intra_scantable.permutated;
933 set_stat(ST_INTRA_AC);
936 rl = &rl_table[3 + s->rl_table_index];
937 if(s->msmpeg4_version<=2)
941 scantable= s->inter_scantable.permutated;
942 set_stat(ST_INTER_AC);
945 /* recalculate block_last_index for M$ wmv1 */
946 if(s->msmpeg4_version>=4 && s->block_last_index[n]>0){
947 for(last_index=63; last_index>=0; last_index--){
948 if(block[scantable[last_index]]) break;
950 s->block_last_index[n]= last_index;
952 last_index = s->block_last_index[n];
954 last_non_zero = i - 1;
955 for (; i <= last_index; i++) {
959 run = i - last_non_zero - 1;
960 last = (i == last_index);
968 if(level<=MAX_LEVEL && run<=MAX_RUN){
969 s->ac_stats[s->mb_intra][n>3][level][run][last]++;
973 s->ac_stats[s->mb_intra][n>3][40][63][0]++; //esc3 like
975 code = get_rl_index(rl, last, run, level);
976 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
980 level1 = level - rl->max_level[last][run];
983 code = get_rl_index(rl, last, run, level1);
986 put_bits(&s->pb, 1, 0);
987 if (level > MAX_LEVEL)
989 run1 = run - rl->max_run[last][level] - run_diff;
992 code = get_rl_index(rl, last, run1, level);
996 put_bits(&s->pb, 1, 0);
997 put_bits(&s->pb, 1, last);
998 if(s->msmpeg4_version>=4){
999 if(s->esc3_level_length==0){
1000 s->esc3_level_length=8;
1001 s->esc3_run_length= 6;
1003 put_bits(&s->pb, 6, 3);
1005 put_bits(&s->pb, 8, 3);
1007 put_bits(&s->pb, s->esc3_run_length, run);
1008 put_bits(&s->pb, 1, sign);
1009 put_bits(&s->pb, s->esc3_level_length, level);
1011 put_bits(&s->pb, 6, run);
1012 put_bits(&s->pb, 8, slevel & 0xff);
1016 put_bits(&s->pb, 1, 1);
1017 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
1018 put_bits(&s->pb, 1, sign);
1022 put_bits(&s->pb, 1, 1);
1023 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
1024 put_bits(&s->pb, 1, sign);
1027 put_bits(&s->pb, 1, sign);
1034 #endif //CONFIG_ENCODERS
1036 /****************************************/
1037 /* decoding stuff */
1039 static VLC mb_non_intra_vlc[4];
1040 static VLC mb_intra_vlc;
1041 static VLC dc_lum_vlc[2];
1042 static VLC dc_chroma_vlc[2];
1043 static VLC v2_dc_lum_vlc;
1044 static VLC v2_dc_chroma_vlc;
1045 static VLC cbpy_vlc;
1046 static VLC v2_intra_cbpc_vlc;
1047 static VLC v2_mb_type_vlc;
1048 static VLC v2_mv_vlc;
1049 static VLC v1_intra_cbpc_vlc;
1050 static VLC v1_inter_cbpc_vlc;
1051 static VLC inter_intra_vlc;
1053 /* this table is practically identical to the one from h263 except that its inverted */
1054 static void init_h263_dc_for_msmpeg4(void)
1056 int level, uni_code, uni_len;
1058 for(level=-256; level<256; level++){
1060 /* find number of bits */
1069 l= (-level) ^ ((1 << size) - 1);
1073 /* luminance h263 */
1074 uni_code= DCtab_lum[size][0];
1075 uni_len = DCtab_lum[size][1];
1076 uni_code ^= (1<<uni_len)-1; //M$ doesnt like compatibility
1079 uni_code<<=size; uni_code|=l;
1082 uni_code<<=1; uni_code|=1;
1086 v2_dc_lum_table[level+256][0]= uni_code;
1087 v2_dc_lum_table[level+256][1]= uni_len;
1089 /* chrominance h263 */
1090 uni_code= DCtab_chrom[size][0];
1091 uni_len = DCtab_chrom[size][1];
1092 uni_code ^= (1<<uni_len)-1; //M$ doesnt like compatibility
1095 uni_code<<=size; uni_code|=l;
1098 uni_code<<=1; uni_code|=1;
1102 v2_dc_chroma_table[level+256][0]= uni_code;
1103 v2_dc_chroma_table[level+256][1]= uni_len;
1108 /* init all vlc decoding tables */
1109 int ff_msmpeg4_decode_init(MpegEncContext *s)
1111 static int done = 0;
1120 for(i=0;i<NB_RL_TABLES;i++) {
1121 init_rl(&rl_table[i]);
1122 init_vlc_rl(&rl_table[i]);
1126 init_vlc(&mv->vlc, MV_VLC_BITS, mv->n + 1,
1127 mv->table_mv_bits, 1, 1,
1128 mv->table_mv_code, 2, 2);
1131 init_vlc(&dc_lum_vlc[0], DC_VLC_BITS, 120,
1132 &table0_dc_lum[0][1], 8, 4,
1133 &table0_dc_lum[0][0], 8, 4);
1134 init_vlc(&dc_chroma_vlc[0], DC_VLC_BITS, 120,
1135 &table0_dc_chroma[0][1], 8, 4,
1136 &table0_dc_chroma[0][0], 8, 4);
1137 init_vlc(&dc_lum_vlc[1], DC_VLC_BITS, 120,
1138 &table1_dc_lum[0][1], 8, 4,
1139 &table1_dc_lum[0][0], 8, 4);
1140 init_vlc(&dc_chroma_vlc[1], DC_VLC_BITS, 120,
1141 &table1_dc_chroma[0][1], 8, 4,
1142 &table1_dc_chroma[0][0], 8, 4);
1144 init_vlc(&v2_dc_lum_vlc, DC_VLC_BITS, 512,
1145 &v2_dc_lum_table[0][1], 8, 4,
1146 &v2_dc_lum_table[0][0], 8, 4);
1147 init_vlc(&v2_dc_chroma_vlc, DC_VLC_BITS, 512,
1148 &v2_dc_chroma_table[0][1], 8, 4,
1149 &v2_dc_chroma_table[0][0], 8, 4);
1151 init_vlc(&cbpy_vlc, CBPY_VLC_BITS, 16,
1152 &cbpy_tab[0][1], 2, 1,
1153 &cbpy_tab[0][0], 2, 1);
1154 init_vlc(&v2_intra_cbpc_vlc, V2_INTRA_CBPC_VLC_BITS, 4,
1155 &v2_intra_cbpc[0][1], 2, 1,
1156 &v2_intra_cbpc[0][0], 2, 1);
1157 init_vlc(&v2_mb_type_vlc, V2_MB_TYPE_VLC_BITS, 8,
1158 &v2_mb_type[0][1], 2, 1,
1159 &v2_mb_type[0][0], 2, 1);
1160 init_vlc(&v2_mv_vlc, V2_MV_VLC_BITS, 33,
1162 &mvtab[0][0], 2, 1);
1165 init_vlc(&mb_non_intra_vlc[i], MB_NON_INTRA_VLC_BITS, 128,
1166 &wmv2_inter_table[i][0][1], 8, 4,
1167 &wmv2_inter_table[i][0][0], 8, 4); //FIXME name?
1170 init_vlc(&mb_intra_vlc, MB_INTRA_VLC_BITS, 64,
1171 &table_mb_intra[0][1], 4, 2,
1172 &table_mb_intra[0][0], 4, 2);
1174 init_vlc(&v1_intra_cbpc_vlc, V1_INTRA_CBPC_VLC_BITS, 8,
1175 intra_MCBPC_bits, 1, 1,
1176 intra_MCBPC_code, 1, 1);
1177 init_vlc(&v1_inter_cbpc_vlc, V1_INTER_CBPC_VLC_BITS, 25,
1178 inter_MCBPC_bits, 1, 1,
1179 inter_MCBPC_code, 1, 1);
1181 init_vlc(&inter_intra_vlc, INTER_INTRA_VLC_BITS, 4,
1182 &table_inter_intra[0][1], 2, 1,
1183 &table_inter_intra[0][0], 2, 1);
1186 switch(s->msmpeg4_version){
1189 s->decode_mb= msmpeg4v12_decode_mb;
1193 s->decode_mb= msmpeg4v34_decode_mb;
1196 s->decode_mb= wmv2_decode_mb;
1200 s->slice_height= s->mb_height; //to avoid 1/0 if the first frame isnt a keyframe
1205 static int decode012(GetBitContext *gb)
1212 return get_bits1(gb) + 1;
1215 int msmpeg4_decode_picture_header(MpegEncContext * s)
1222 for(i=0; i<s->gb.size_in_bits; i++)
1223 printf("%d", get_bits1(&s->gb));
1224 // get_bits1(&s->gb);
1230 if(s->msmpeg4_version==1){
1231 int start_code, num;
1232 start_code = (get_bits(&s->gb, 16)<<16) | get_bits(&s->gb, 16);
1233 if(start_code!=0x00000100){
1234 fprintf(stderr, "invalid startcode\n");
1238 num= get_bits(&s->gb, 5); // frame number */
1241 s->pict_type = get_bits(&s->gb, 2) + 1;
1242 if (s->pict_type != I_TYPE &&
1243 s->pict_type != P_TYPE){
1244 fprintf(stderr, "invalid picture type\n");
1250 if(s->pict_type == I_TYPE) had_i=1;
1251 if(!had_i) return -1;
1254 s->qscale = get_bits(&s->gb, 5);
1256 fprintf(stderr, "invalid qscale\n");
1260 if (s->pict_type == I_TYPE) {
1261 code = get_bits(&s->gb, 5);
1262 if(s->msmpeg4_version==1){
1263 if(code==0 || code>s->mb_height){
1264 fprintf(stderr, "invalid slice height %d\n", code);
1268 s->slice_height = code;
1270 /* 0x17: one slice, 0x18: two slices, ... */
1272 fprintf(stderr, "error, slice code was %X\n", code);
1276 s->slice_height = s->mb_height / (code - 0x16);
1279 switch(s->msmpeg4_version){
1282 s->rl_chroma_table_index = 2;
1283 s->rl_table_index = 2;
1285 s->dc_table_index = 0; //not used
1288 s->rl_chroma_table_index = decode012(&s->gb);
1289 s->rl_table_index = decode012(&s->gb);
1291 s->dc_table_index = get_bits1(&s->gb);
1294 msmpeg4_decode_ext_header(s, (2+5+5+17+7)/8);
1296 if(s->bit_rate > MBAC_BITRATE) s->per_mb_rl_table= get_bits1(&s->gb);
1297 else s->per_mb_rl_table= 0;
1299 if(!s->per_mb_rl_table){
1300 s->rl_chroma_table_index = decode012(&s->gb);
1301 s->rl_table_index = decode012(&s->gb);
1304 s->dc_table_index = get_bits1(&s->gb);
1305 s->inter_intra_pred= 0;
1309 /* printf("qscale:%d rlc:%d rl:%d dc:%d mbrl:%d slice:%d \n",
1311 s->rl_chroma_table_index,
1317 switch(s->msmpeg4_version){
1320 if(s->msmpeg4_version==1)
1321 s->use_skip_mb_code = 1;
1323 s->use_skip_mb_code = get_bits1(&s->gb);
1324 s->rl_table_index = 2;
1325 s->rl_chroma_table_index = s->rl_table_index;
1326 s->dc_table_index = 0; //not used
1327 s->mv_table_index = 0;
1330 s->use_skip_mb_code = get_bits1(&s->gb);
1331 s->rl_table_index = decode012(&s->gb);
1332 s->rl_chroma_table_index = s->rl_table_index;
1334 s->dc_table_index = get_bits1(&s->gb);
1336 s->mv_table_index = get_bits1(&s->gb);
1339 s->use_skip_mb_code = get_bits1(&s->gb);
1341 if(s->bit_rate > MBAC_BITRATE) s->per_mb_rl_table= get_bits1(&s->gb);
1342 else s->per_mb_rl_table= 0;
1344 if(!s->per_mb_rl_table){
1345 s->rl_table_index = decode012(&s->gb);
1346 s->rl_chroma_table_index = s->rl_table_index;
1349 s->dc_table_index = get_bits1(&s->gb);
1351 s->mv_table_index = get_bits1(&s->gb);
1352 s->inter_intra_pred= (s->width*s->height < 320*240 && s->bit_rate<=II_BITRATE);
1355 /* printf("skip:%d rl:%d rlc:%d dc:%d mv:%d mbrl:%d qp:%d \n",
1356 s->use_skip_mb_code,
1358 s->rl_chroma_table_index,
1363 if(s->flipflop_rounding){
1364 s->no_rounding ^= 1;
1369 //printf("%d %d %d %d %d\n", s->pict_type, s->bit_rate, s->inter_intra_pred, s->width, s->height);
1371 s->esc3_level_length= 0;
1372 s->esc3_run_length= 0;
1375 printf("*****frame %d:\n", frame_count++);
1380 int msmpeg4_decode_ext_header(MpegEncContext * s, int buf_size)
1382 int left= buf_size*8 - get_bits_count(&s->gb);
1383 int length= s->msmpeg4_version>=3 ? 17 : 16;
1384 /* the alt_bitstream reader could read over the end so we need to check it */
1385 if(left>=length && left<length+8)
1389 fps= get_bits(&s->gb, 5);
1390 s->bit_rate= get_bits(&s->gb, 11)*1024;
1391 if(s->msmpeg4_version>=3)
1392 s->flipflop_rounding= get_bits1(&s->gb);
1394 s->flipflop_rounding= 0;
1396 // printf("fps:%2d bps:%2d roundingType:%1d\n", fps, s->bit_rate/1024, s->flipflop_rounding);
1398 else if(left<length+8)
1400 s->flipflop_rounding= 0;
1401 printf("ext header missing, %d left\n", left);
1405 fprintf(stderr, "I frame too long, ignoring ext header\n");
1411 static inline void msmpeg4_memsetw(short *tab, int val, int n)
1418 #ifdef CONFIG_ENCODERS
1420 static void msmpeg4v2_encode_motion(MpegEncContext * s, int val)
1422 int range, bit_size, sign, code, bits;
1427 put_bits(&s->pb, mvtab[code][1], mvtab[code][0]);
1429 bit_size = s->f_code - 1;
1430 range = 1 << bit_size;
1443 code = (val >> bit_size) + 1;
1444 bits = val & (range - 1);
1446 put_bits(&s->pb, mvtab[code][1] + 1, (mvtab[code][0] << 1) | sign);
1448 put_bits(&s->pb, bit_size, bits);
1453 #endif //CONFIG_ENCODERS
1455 /* this is identical to h263 except that its range is multiplied by 2 */
1456 static int msmpeg4v2_decode_motion(MpegEncContext * s, int pred, int f_code)
1458 int code, val, sign, shift;
1460 code = get_vlc2(&s->gb, v2_mv_vlc.table, V2_MV_VLC_BITS, 2);
1461 // printf("MV code %d at %d %d pred: %d\n", code, s->mb_x,s->mb_y, pred);
1467 sign = get_bits1(&s->gb);
1469 val = (code - 1) << shift;
1471 val |= get_bits(&s->gb, shift);
1485 static int msmpeg4v12_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
1489 if (s->pict_type == P_TYPE) {
1490 if (s->use_skip_mb_code) {
1491 if (get_bits1(&s->gb)) {
1495 s->block_last_index[i] = -1;
1496 s->mv_dir = MV_DIR_FORWARD;
1497 s->mv_type = MV_TYPE_16X16;
1505 if(s->msmpeg4_version==2)
1506 code = get_vlc2(&s->gb, v2_mb_type_vlc.table, V2_MB_TYPE_VLC_BITS, 1);
1508 code = get_vlc2(&s->gb, v1_inter_cbpc_vlc.table, V1_INTER_CBPC_VLC_BITS, 3);
1509 if(code<0 || code>7){
1510 fprintf(stderr, "cbpc %d invalid at %d %d\n", code, s->mb_x, s->mb_y);
1514 s->mb_intra = code >>2;
1519 if(s->msmpeg4_version==2)
1520 cbp= get_vlc2(&s->gb, v2_intra_cbpc_vlc.table, V2_INTRA_CBPC_VLC_BITS, 1);
1522 cbp= get_vlc2(&s->gb, v1_intra_cbpc_vlc.table, V1_INTRA_CBPC_VLC_BITS, 1);
1524 fprintf(stderr, "cbpc %d invalid at %d %d\n", cbp, s->mb_x, s->mb_y);
1532 cbpy= get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1);
1534 fprintf(stderr, "cbpy %d invalid at %d %d\n", cbp, s->mb_x, s->mb_y);
1539 if(s->msmpeg4_version==1 || (cbp&3) != 3) cbp^= 0x3C;
1541 h263_pred_motion(s, 0, &mx, &my);
1542 mx= msmpeg4v2_decode_motion(s, mx, 1);
1543 my= msmpeg4v2_decode_motion(s, my, 1);
1545 s->mv_dir = MV_DIR_FORWARD;
1546 s->mv_type = MV_TYPE_16X16;
1547 s->mv[0][0][0] = mx;
1548 s->mv[0][0][1] = my;
1550 if(s->msmpeg4_version==2){
1551 s->ac_pred = get_bits1(&s->gb);
1552 cbp|= get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1)<<2; //FIXME check errors
1555 cbp|= get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1)<<2; //FIXME check errors
1556 if(s->pict_type==P_TYPE) cbp^=0x3C;
1560 for (i = 0; i < 6; i++) {
1561 if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, NULL) < 0)
1563 fprintf(stderr,"\nerror while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
1570 static int msmpeg4v34_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
1578 if(s->mb_y==0) printf("\n");
1582 if (s->pict_type == P_TYPE) {
1583 set_stat(ST_INTER_MB);
1584 if (s->use_skip_mb_code) {
1585 if (get_bits1(&s->gb)) {
1589 s->block_last_index[i] = -1;
1590 s->mv_dir = MV_DIR_FORWARD;
1591 s->mv_type = MV_TYPE_16X16;
1602 code = get_vlc2(&s->gb, mb_non_intra_vlc[DEFAULT_INTER_INDEX].table, MB_NON_INTRA_VLC_BITS, 3);
1605 //s->mb_intra = (code & 0x40) ? 0 : 1;
1606 s->mb_intra = (~code & 0x40) >> 6;
1610 set_stat(ST_INTRA_MB);
1612 code = get_vlc2(&s->gb, mb_intra_vlc.table, MB_INTRA_VLC_BITS, 2);
1615 /* predict coded block pattern */
1618 int val = ((code >> (5 - i)) & 1);
1620 int pred = coded_block_pred(s, i, &coded_val);
1624 cbp |= val << (5 - i);
1630 //printf("P at %d %d\n", s->mb_x, s->mb_y);
1631 if(s->per_mb_rl_table && cbp){
1632 s->rl_table_index = decode012(&s->gb);
1633 s->rl_chroma_table_index = s->rl_table_index;
1636 h263_pred_motion(s, 0, &mx, &my);
1637 if (msmpeg4_decode_motion(s, &mx, &my) < 0)
1639 s->mv_dir = MV_DIR_FORWARD;
1640 s->mv_type = MV_TYPE_16X16;
1641 s->mv[0][0][0] = mx;
1642 s->mv[0][0][1] = my;
1647 //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));
1648 set_stat(ST_INTRA_MB);
1649 s->ac_pred = get_bits1(&s->gb);
1651 printf("%c", s->ac_pred ? 'A' : 'I');
1653 if(s->inter_intra_pred){
1654 s->h263_aic_dir= get_vlc2(&s->gb, inter_intra_vlc.table, INTER_INTRA_VLC_BITS, 1);
1655 // printf("%d%d %d %d/", s->ac_pred, s->h263_aic_dir, s->mb_x, s->mb_y);
1657 if(s->per_mb_rl_table && cbp){
1658 s->rl_table_index = decode012(&s->gb);
1659 s->rl_chroma_table_index = s->rl_table_index;
1663 for (i = 0; i < 6; i++) {
1664 if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, NULL) < 0)
1666 fprintf(stderr,"\nerror while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
1673 //#define ERROR_DETAILS
1674 static inline int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
1675 int n, int coded, const uint8_t *scan_table)
1677 int level, i, last, run, run_diff;
1680 RL_VLC_ELEM *rl_vlc;
1689 level = msmpeg4_decode_dc(s, n, &dc_pred_dir);
1694 if(n==4) printf("%X", c);
1699 fprintf(stderr, "dc overflow- block: %d qscale: %d//\n", n, s->qscale);
1700 if(s->inter_intra_pred) level=0;
1704 rl = &rl_table[s->rl_table_index];
1705 if(level > 256*s->y_dc_scale){
1706 fprintf(stderr, "dc overflow+ L qscale: %d//\n", s->qscale);
1707 if(!s->inter_intra_pred) return -1;
1710 rl = &rl_table[3 + s->rl_chroma_table_index];
1711 if(level > 256*s->c_dc_scale){
1712 fprintf(stderr, "dc overflow+ C qscale: %d//\n", s->qscale);
1713 if(!s->inter_intra_pred) return -1;
1724 if (dc_pred_dir == 0)
1725 scan_table = s->intra_v_scantable.permutated; /* left */
1727 scan_table = s->intra_h_scantable.permutated; /* top */
1729 scan_table = s->intra_scantable.permutated;
1731 set_stat(ST_INTRA_AC);
1732 rl_vlc= rl->rl_vlc[0];
1734 qmul = s->qscale << 1;
1735 qadd = (s->qscale - 1) | 1;
1737 rl = &rl_table[3 + s->rl_table_index];
1739 if(s->msmpeg4_version==2)
1745 s->block_last_index[n] = i;
1749 scan_table = s->inter_scantable.permutated;
1750 set_stat(ST_INTER_AC);
1751 rl_vlc= rl->rl_vlc[s->qscale];
1754 OPEN_READER(re, &s->gb);
1756 UPDATE_CACHE(re, &s->gb);
1757 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2);
1760 cache= GET_CACHE(re, &s->gb);
1762 if (s->msmpeg4_version==1 || (cache&0x80000000)==0) {
1763 if (s->msmpeg4_version==1 || (cache&0x40000000)==0) {
1765 if(s->msmpeg4_version!=1) LAST_SKIP_BITS(re, &s->gb, 2);
1766 UPDATE_CACHE(re, &s->gb);
1767 if(s->msmpeg4_version<=3){
1768 last= SHOW_UBITS(re, &s->gb, 1); SKIP_CACHE(re, &s->gb, 1);
1769 run= SHOW_UBITS(re, &s->gb, 6); SKIP_CACHE(re, &s->gb, 6);
1770 level= SHOW_SBITS(re, &s->gb, 8); LAST_SKIP_CACHE(re, &s->gb, 8);
1771 SKIP_COUNTER(re, &s->gb, 1+6+8);
1774 last= SHOW_UBITS(re, &s->gb, 1); SKIP_BITS(re, &s->gb, 1);
1775 if(!s->esc3_level_length){
1777 //printf("ESC-3 %X at %d %d\n", show_bits(&s->gb, 24), s->mb_x, s->mb_y);
1779 ll= SHOW_UBITS(re, &s->gb, 3); SKIP_BITS(re, &s->gb, 3);
1781 if(SHOW_UBITS(re, &s->gb, 1)) printf("cool a new vlc code ,contact the ffmpeg developers and upload the file\n");
1782 SKIP_BITS(re, &s->gb, 1);
1787 while(ll<8 && SHOW_UBITS(re, &s->gb, 1)==0){
1789 SKIP_BITS(re, &s->gb, 1);
1791 if(ll<8) SKIP_BITS(re, &s->gb, 1);
1794 s->esc3_level_length= ll;
1795 s->esc3_run_length= SHOW_UBITS(re, &s->gb, 2) + 3; SKIP_BITS(re, &s->gb, 2);
1796 //printf("level length:%d, run length: %d\n", ll, s->esc3_run_length);
1797 UPDATE_CACHE(re, &s->gb);
1799 run= SHOW_UBITS(re, &s->gb, s->esc3_run_length);
1800 SKIP_BITS(re, &s->gb, s->esc3_run_length);
1802 sign= SHOW_UBITS(re, &s->gb, 1);
1803 SKIP_BITS(re, &s->gb, 1);
1805 level= SHOW_UBITS(re, &s->gb, s->esc3_level_length);
1806 SKIP_BITS(re, &s->gb, s->esc3_level_length);
1807 if(sign) level= -level;
1809 //printf("level: %d, run: %d at %d %d\n", level, run, s->mb_x, s->mb_y);
1810 #if 0 // waste of time / this will detect very few errors
1812 const int abs_level= ABS(level);
1813 const int run1= run - rl->max_run[last][abs_level] - run_diff;
1814 if(abs_level<=MAX_LEVEL && run<=MAX_RUN){
1815 if(abs_level <= rl->max_level[last][run]){
1816 fprintf(stderr, "illegal 3. esc, vlc encoding possible\n");
1817 return DECODING_AC_LOST;
1819 if(abs_level <= rl->max_level[last][run]*2){
1820 fprintf(stderr, "illegal 3. esc, esc 1 encoding possible\n");
1821 return DECODING_AC_LOST;
1823 if(run1>=0 && abs_level <= rl->max_level[last][run1]){
1824 fprintf(stderr, "illegal 3. esc, esc 2 encoding possible\n");
1825 return DECODING_AC_LOST;
1830 //level = level * qmul + (level>0) * qadd - (level<=0) * qadd ;
1831 if (level>0) level= level * qmul + qadd;
1832 else level= level * qmul - qadd;
1833 #if 0 // waste of time too :(
1834 if(level>2048 || level<-2048){
1835 fprintf(stderr, "|level| overflow in 3. esc\n");
1836 return DECODING_AC_LOST;
1841 #ifdef ERROR_DETAILS
1843 fprintf(stderr, "illegal vlc code in ESC3 level=%d\n", level);
1844 else if((i>62 && i<192) || i>192+63)
1845 fprintf(stderr, "run overflow in ESC3 i=%d run=%d level=%d\n", i, run, level);
1849 #if MIN_CACHE_BITS < 23
1850 LAST_SKIP_BITS(re, &s->gb, 2);
1851 UPDATE_CACHE(re, &s->gb);
1853 SKIP_BITS(re, &s->gb, 2);
1855 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2);
1856 i+= run + rl->max_run[run>>7][level/qmul] + run_diff; //FIXME opt indexing
1857 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1858 LAST_SKIP_BITS(re, &s->gb, 1);
1859 #ifdef ERROR_DETAILS
1861 fprintf(stderr, "illegal vlc code in ESC2 level=%d\n", level);
1862 else if((i>62 && i<192) || i>192+63)
1863 fprintf(stderr, "run overflow in ESC2 i=%d run=%d level=%d\n", i, run, level);
1868 #if MIN_CACHE_BITS < 22
1869 LAST_SKIP_BITS(re, &s->gb, 1);
1870 UPDATE_CACHE(re, &s->gb);
1872 SKIP_BITS(re, &s->gb, 1);
1874 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2);
1876 level = level + rl->max_level[run>>7][(run-1)&63] * qmul;//FIXME opt indexing
1877 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1878 LAST_SKIP_BITS(re, &s->gb, 1);
1879 #ifdef ERROR_DETAILS
1881 fprintf(stderr, "illegal vlc code in ESC1 level=%d\n", level);
1882 else if((i>62 && i<192) || i>192+63)
1883 fprintf(stderr, "run overflow in ESC1 i=%d run=%d level=%d\n", i, run, level);
1888 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1889 LAST_SKIP_BITS(re, &s->gb, 1);
1890 #ifdef ERROR_DETAILS
1892 fprintf(stderr, "illegal vlc code level=%d\n", level);
1893 else if((i>62 && i<192) || i>192+63)
1894 fprintf(stderr, "run overflow i=%d run=%d level=%d\n", i, run, level);
1900 const int left= s->gb.size_in_bits - get_bits_count(&s->gb);
1901 if(((i+192 == 64 && level/qmul==-1) || s->error_resilience<=1) && left>=0){
1902 fprintf(stderr, "ignoring overflow at %d %d\n", s->mb_x, s->mb_y);
1905 fprintf(stderr, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
1910 block[scan_table[i]] = level;
1914 block[scan_table[i]] = level;
1916 CLOSE_READER(re, &s->gb);
1920 mpeg4_pred_ac(s, block, n, dc_pred_dir);
1922 i = 63; /* XXX: not optimal */
1925 if(s->msmpeg4_version>=4 && i>0) i=63; //FIXME/XXX optimize
1926 s->block_last_index[n] = i;
1931 static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr)
1935 if(s->msmpeg4_version<=2){
1937 level = get_vlc2(&s->gb, v2_dc_lum_vlc.table, DC_VLC_BITS, 3);
1939 level = get_vlc2(&s->gb, v2_dc_chroma_vlc.table, DC_VLC_BITS, 3);
1944 }else{ //FIXME optimize use unified tables & index
1946 level = get_vlc2(&s->gb, dc_lum_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
1948 level = get_vlc2(&s->gb, dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
1951 fprintf(stderr, "illegal dc vlc\n");
1955 if (level == DC_MAX) {
1956 level = get_bits(&s->gb, 8);
1957 if (get_bits1(&s->gb))
1959 } else if (level != 0) {
1960 if (get_bits1(&s->gb))
1965 if(s->msmpeg4_version==1){
1967 pred = msmpeg4v1_pred_dc(s, n, &dc_val);
1970 /* update predictor */
1974 pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr);
1977 /* update predictor */
1979 *dc_val = level * s->y_dc_scale;
1981 *dc_val = level * s->c_dc_scale;
1988 static int msmpeg4_decode_motion(MpegEncContext * s,
1989 int *mx_ptr, int *my_ptr)
1994 mv = &mv_tables[s->mv_table_index];
1996 code = get_vlc2(&s->gb, mv->vlc.table, MV_VLC_BITS, 2);
1998 fprintf(stderr, "illegal MV code at %d %d\n", s->mb_x, s->mb_y);
2001 if (code == mv->n) {
2002 //printf("MV ESC %X at %d %d\n", show_bits(&s->gb, 24), s->mb_x, s->mb_y);
2003 mx = get_bits(&s->gb, 6);
2004 my = get_bits(&s->gb, 6);
2006 mx = mv->table_mvx[code];
2007 my = mv->table_mvy[code];
2012 /* WARNING : they do not do exactly modulo encoding */
2027 /* cleanest way to support it
2028 * there is too much shared between versions so that we cant have 1 file per version & 1 common
2029 * as allmost everything would be in the common file