changeset 498:0b4450c15067 libavcodec

dc scale simplification/optimization
author michaelni
date Tue, 18 Jun 2002 00:46:02 +0000
parents 5b33d11bd1fb
children 8b7a54d58549
files h263.c h263data.h h263dec.c mpeg12.c mpeg12data.h mpeg4data.h
diffstat 6 files changed, 129 insertions(+), 92 deletions(-) [+]
line wrap: on
line diff
--- a/h263.c	Mon Jun 17 08:29:03 2002 +0000
+++ b/h263.c	Tue Jun 18 00:46:02 2002 +0000
@@ -50,7 +50,6 @@
 static inline int mpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
                               int n, int coded);
 static int h263_pred_dc(MpegEncContext * s, int n, UINT16 **dc_val_ptr);
-static inline int mpeg4_pred_dc(MpegEncContext * s, int n, UINT16 **dc_val_ptr, int *dir_ptr);
 static void mpeg4_inv_pred_ac(MpegEncContext * s, INT16 *block, int n,
                               int dir);
 static void mpeg4_decode_sprite_trajectory(MpegEncContext * s);
@@ -176,6 +175,14 @@
     }
 
     put_bits(&s->pb, 1, 0);	/* no PEI */
+
+    if(s->h263_aic){
+         s->y_dc_scale_table= 
+         s->c_dc_scale_table= h263_aic_dc_scale_table;
+    }else{
+        s->y_dc_scale_table=
+        s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
+    }
 }
 
 int h263_encode_gob_header(MpegEncContext * s, int mb_line)
@@ -496,7 +503,7 @@
             const int level= block[i][0];
             UINT16 *dc_ptr;
 
-            dc_diff[i]= level - mpeg4_pred_dc(s, i, &dc_ptr, &dir[i]);
+            dc_diff[i]= level - ff_mpeg4_pred_dc(s, i, &dc_ptr, &dir[i]);
             if (i < 4) {
                 *dc_ptr = level * s->y_dc_scale;
             } else {
@@ -1098,9 +1105,12 @@
         s->min_qcoeff= -128;
         s->max_qcoeff=  127;
         break;
+        //Note for mpeg4 & h263 the dc-scale table will be set per frame as needed later 
     default: //nothing needed default table allready set in mpegvideo.c
         s->min_qcoeff= -128;
         s->max_qcoeff=  127;
+        s->y_dc_scale_table=
+        s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
     }
 
     /* h263 type bias */
@@ -1326,44 +1336,18 @@
     if (s->pict_type == B_TYPE)
 	put_bits(&s->pb, 3, s->b_code);	/* fcode_back */
     //    printf("****frame %d\n", picture_number);
+
+     s->y_dc_scale_table= ff_mpeg4_y_dc_scale_table; //FIXME add short header support 
+     s->c_dc_scale_table= ff_mpeg4_c_dc_scale_table;
 }
 
-void h263_dc_scale(MpegEncContext * s)
+static void h263_dc_scale(MpegEncContext * s)
 {
-#if 1
-    const static UINT8 y_tab[32]={
-    //  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
-        0, 8, 8, 8, 8,10,12,14,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,34,36,38,40,42,44,46
-    };
-    const static UINT8 c_tab[32]={
-    //  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
-        0, 8, 8, 8, 8, 9, 9,10,10,11,11,12,12,13,13,14,14,15,15,16,16,17,17,18,18,19,20,21,22,23,24,25
-    };
-    s->y_dc_scale = y_tab[s->qscale];
-    s->c_dc_scale = c_tab[s->qscale];
-#else
-    int quant;
-    quant = s->qscale;
-    /* luminance */
-    if (quant < 5)
-	s->y_dc_scale = 8;
-    else if (quant > 4 && quant < 9)
-	s->y_dc_scale = (2 * quant);
-    else if (quant > 8 && quant < 25)
-	s->y_dc_scale = (quant + 8);
-    else
-	s->y_dc_scale = (2 * quant - 16);
-    /* chrominance */
-    if (quant < 5)
-	s->c_dc_scale = 8;
-    else if (quant > 4 && quant < 25)
-	s->c_dc_scale = ((quant + 13) / 2);
-    else
-	s->c_dc_scale = (quant - 6);
-#endif
+    s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
+    s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
 }
 
-static inline int mpeg4_pred_dc(MpegEncContext * s, int n, UINT16 **dc_val_ptr, int *dir_ptr)
+inline int ff_mpeg4_pred_dc(MpegEncContext * s, int n, UINT16 **dc_val_ptr, int *dir_ptr)
 {
     int a, b, c, wrap, pred, scale;
     UINT16 *dc_val;
@@ -2653,10 +2637,6 @@
             if (s->ac_pred && s->h263_aic)
                 s->h263_aic_dir = get_bits1(&s->gb);
         }
-        if (s->h263_aic) {
-            s->y_dc_scale = 2 * s->qscale;
-            s->c_dc_scale = 2 * s->qscale;
-        }
         cbpy = get_vlc(&s->gb, &cbpy_vlc);
         if(cbpy<0) return -1;
         cbp = (cbpc & 3) | (cbpy << 2);
@@ -2867,7 +2847,7 @@
         }
     }
 
-    pred = mpeg4_pred_dc(s, n, &dc_val, dir_ptr);
+    pred = ff_mpeg4_pred_dc(s, n, &dc_val, dir_ptr);
     level += pred;
     if (level < 0)
         level = 0;
@@ -2956,8 +2936,8 @@
 #if 1 
                     {
                         const int abs_level= ABS(level);
-                        int run1;
                         if(abs_level<=MAX_LEVEL && run<=MAX_RUN && s->error_resilience>=0){
+                            const int run1= run - rl->max_run[last][abs_level] - 1;
                             if(abs_level <= rl->max_level[last][run]){
                                 fprintf(stderr, "illegal 3. esc, vlc encoding possible\n");
                                 return DECODING_AC_LOST;
@@ -2966,7 +2946,6 @@
                                 fprintf(stderr, "illegal 3. esc, esc 1 encoding possible\n");
                                 return DECODING_AC_LOST;
                             }
-                            run1 = run - rl->max_run[last][abs_level] - 1;
                             if(run1 >= 0 && abs_level <= rl->max_level[last][run1]){
                                 fprintf(stderr, "illegal 3. esc, esc 2 encoding possible\n");
                                 return DECODING_AC_LOST;
@@ -3185,6 +3164,15 @@
         skip_bits(&s->gb, 8);
     }
     s->f_code = 1;
+    
+    if(s->h263_aic){
+         s->y_dc_scale_table= 
+         s->c_dc_scale_table= h263_aic_dc_scale_table;
+    }else{
+        s->y_dc_scale_table=
+        s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
+    }
+
     return 0;
 }
 
@@ -3626,7 +3614,7 @@
                 }else{
                     printf("hmm, i havnt seen that version of divx yet, lets assume they fixed these bugs ...\n"
                            "using mpeg4 decoder, if it fails contact the developers (of ffmpeg)\n");
-#endif 
+#endif
                 }
             }
         }
@@ -3760,6 +3748,9 @@
      s->picture_number++; // better than pic number==0 allways ;)
 //printf("done\n");
 
+     s->y_dc_scale_table= ff_mpeg4_y_dc_scale_table; //FIXME add short header support 
+     s->c_dc_scale_table= ff_mpeg4_c_dc_scale_table;
+
      return 0;
 }
 
--- a/h263data.h	Mon Jun 17 08:29:03 2002 +0000
+++ b/h263data.h	Tue Jun 18 00:46:02 2002 +0000
@@ -202,3 +202,9 @@
     { 704, 576 },
     { 1408, 1152 },
 };
+
+static UINT8 h263_aic_dc_scale_table[32]={
+//  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
+    0, 2, 4, 6, 8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58,60,62
+};
+
--- a/h263dec.c	Mon Jun 17 08:29:03 2002 +0000
+++ b/h263dec.c	Tue Jun 18 00:46:02 2002 +0000
@@ -76,6 +76,11 @@
         s->h263_pred = 1;
         s->msmpeg4_version=4;
         break;
+    case CODEC_ID_WMV2:
+        s->h263_msmpeg4 = 1;
+        s->h263_pred = 1;
+        s->msmpeg4_version=5;
+        break;
     case CODEC_ID_H263I:
         s->h263_intel = 1;
         break;
@@ -91,7 +96,7 @@
             return -1;
 
     if (s->h263_msmpeg4)
-        msmpeg4_decode_init_vlc(s);
+        ff_msmpeg4_decode_init(s);
     else
         h263_decode_init_vlc(s);
     
@@ -169,8 +174,10 @@
 
     if(ret==FRAME_SKIPED) return 0;
     /* skip if the header was thrashed */
-    if (ret < 0)
+    if (ret < 0){
+        fprintf(stderr, "header damaged\n");
         return -1;
+    }
     /* skip b frames if we dont have reference frames */
     if(s->num_available_buffers<2 && s->pict_type==B_TYPE) return 0;
     /* skip b frames if we are in a hurry */
@@ -216,6 +223,9 @@
             s->last_dc[2]= 128;
         }
 
+        s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
+        s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
+
         s->block_index[0]= s->block_wrap[0]*(s->mb_y*2 + 1) - 1;
         s->block_index[1]= s->block_wrap[0]*(s->mb_y*2 + 1);
         s->block_index[2]= s->block_wrap[0]*(s->mb_y*2 + 2) - 1;
@@ -245,6 +255,9 @@
                         }
                     }
                     s->qscale= s->next_resync_qscale;
+                    s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
+                    s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
+
                     s->gb= s->next_resync_gb;
                     s->resync_mb_x= s->mb_x; //we know that the marker is here cuz mb_num_left was the distance to it
                     s->resync_mb_y= s->mb_y;
@@ -265,18 +278,6 @@
 
             //fprintf(stderr,"\nFrame: %d\tMB: %d",avctx->frame_number, (s->mb_y * s->mb_width) + s->mb_x);
             /* DCT & quantize */
-            if (s->h263_pred && !(s->msmpeg4_version==1 || s->msmpeg4_version==2)) {
-                /* old ffmpeg encoded msmpeg4v3 workaround */
-                if(s->workaround_bugs==1 && s->msmpeg4_version==3) 
-                    ff_old_msmpeg4_dc_scale(s);
-                else
-                    h263_dc_scale(s);                
-            } else {
-                /* default quantization values */
-                s->y_dc_scale = 8;
-                s->c_dc_scale = 8;
-            }
-
             if(s->decoding_error!=DECODING_DESYNC){
                 int last_error= s->decoding_error;
                 clear_blocks(s->block[0]);
@@ -521,6 +522,18 @@
     CODEC_CAP_DRAW_HORIZ_BAND,
 };
 
+AVCodec wmv2_decoder = {
+    "wmv2",
+    CODEC_TYPE_VIDEO,
+    CODEC_ID_WMV2,
+    sizeof(MpegEncContext),
+    h263_decode_init,
+    NULL,
+    h263_decode_end,
+    h263_decode_frame,
+    CODEC_CAP_DRAW_HORIZ_BAND,
+};
+
 AVCodec h263i_decoder = {
     "h263i",
     CODEC_TYPE_VIDEO,
--- a/mpeg12.c	Mon Jun 17 08:29:03 2002 +0000
+++ b/mpeg12.c	Tue Jun 18 00:46:02 2002 +0000
@@ -184,41 +184,14 @@
     put_bits(&s->pb, 1, 1); 
 }
 
+static void common_init(MpegEncContext *s)
+{
+    s->y_dc_scale_table=
+    s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
+}
+
 void mpeg1_encode_picture_header(MpegEncContext *s, int picture_number)
 {
-    static int done=0;
-
-    if (!done) {
-	int i;
-        done = 1;
-        init_rl(&rl_mpeg1);
-	
-	for(i=0; i<64; i++)
-	{
-		mpeg1_max_level[0][i]= rl_mpeg1.max_level[0][i];
-		mpeg1_index_run[0][i]= rl_mpeg1.index_run[0][i];
-	}
-
-	/* build unified dc encoding tables */
-	for(i=-255; i<256; i++)
-	{
-		int adiff, index;
-		int bits, code;
-		int diff=i;
-
-		adiff = ABS(diff);
-		if(diff<0) diff--;
-		index = vlc_dc_table[adiff];
-
-		bits= vlc_dc_lum_bits[index] + index;
-		code= (vlc_dc_lum_code[index]<<index) + (diff & ((1 << index) - 1));
-		mpeg1_lum_dc_uni[i+255]= bits + (code<<8);
-		
-		bits= vlc_dc_chroma_bits[index] + index;
-		code= (vlc_dc_chroma_code[index]<<index) + (diff & ((1 << index) - 1));
-		mpeg1_chr_dc_uni[i+255]= bits + (code<<8);
-	}
-    }
     mpeg1_encode_sequence_header(s);
 
     /* mpeg1 picture header */
@@ -354,14 +327,46 @@
     }
 }
 
-void mpeg1_encode_init(MpegEncContext *s)
+void ff_mpeg1_encode_init(MpegEncContext *s)
 {
     static int done=0;
+
+    common_init(s);
+
     if(!done){
         int f_code;
         int mv;
+	int i;
 
         done=1;
+        init_rl(&rl_mpeg1);
+	
+	for(i=0; i<64; i++)
+	{
+		mpeg1_max_level[0][i]= rl_mpeg1.max_level[0][i];
+		mpeg1_index_run[0][i]= rl_mpeg1.index_run[0][i];
+	}
+
+	/* build unified dc encoding tables */
+	for(i=-255; i<256; i++)
+	{
+		int adiff, index;
+		int bits, code;
+		int diff=i;
+
+		adiff = ABS(diff);
+		if(diff<0) diff--;
+		index = vlc_dc_table[adiff];
+
+		bits= vlc_dc_lum_bits[index] + index;
+		code= (vlc_dc_lum_code[index]<<index) + (diff & ((1 << index) - 1));
+		mpeg1_lum_dc_uni[i+255]= bits + (code<<8);
+		
+		bits= vlc_dc_chroma_bits[index] + index;
+		code= (vlc_dc_chroma_code[index]<<index) + (diff & ((1 << index) - 1));
+		mpeg1_chr_dc_uni[i+255]= bits + (code<<8);
+	}
+
         for(f_code=1; f_code<=MAX_FCODE; f_code++){
             for(mv=-MAX_MV; mv<=MAX_MV; mv++){
                 int len;
@@ -403,7 +408,7 @@
     s->intra_quant_bias= 3<<(QUANT_BIAS_SHIFT-3); //(a + x*3/8)/x
     s->inter_quant_bias= 0;
 }
- 
+
 static inline void encode_dc(MpegEncContext *s, int diff, int component)
 {
     if (component == 0) {
@@ -1183,6 +1188,8 @@
 static int mpeg_decode_init(AVCodecContext *avctx)
 {
     Mpeg1Context *s = avctx->priv_data;
+    
+    common_init(&s->mpeg_enc_ctx);
 
     s->header_state = 0xff;
     s->mpeg_enc_ctx_allocated = 0;
--- a/mpeg12data.h	Mon Jun 17 08:29:03 2002 +0000
+++ b/mpeg12data.h	Tue Jun 18 00:46:02 2002 +0000
@@ -402,3 +402,13 @@
     24,28,32,36,40,44,48,52,
     56,64,72,80,88,96,104,112,
 };
+
+UINT8 ff_mpeg1_dc_scale_table[128]={ // MN: mpeg2 really can have such large qscales?
+//  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
+    8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
+    8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
+    8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
+    8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
+};
+
+
--- a/mpeg4data.h	Mon Jun 17 08:29:03 2002 +0000
+++ b/mpeg4data.h	Tue Jun 18 00:46:02 2002 +0000
@@ -153,3 +153,13 @@
  23, 24, 25, 27, 28, 30, 31, 33,
 };
 
+UINT8 ff_mpeg4_y_dc_scale_table[32]={
+//  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
+    0, 8, 8, 8, 8,10,12,14,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,34,36,38,40,42,44,46
+};
+UINT8 ff_mpeg4_c_dc_scale_table[32]={
+//  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
+    0, 8, 8, 8, 8, 9, 9,10,10,11,11,12,12,13,13,14,14,15,15,16,16,17,17,18,18,19,20,21,22,23,24,25
+};
+
+