statistics for 2-pass encoding
authorMichael Niedermayer <michaelni@gmx.at>
Sat, 23 Mar 2002 17:43:30 +0000 (17:43 +0000)
committerMichael Niedermayer <michaelni@gmx.at>
Sat, 23 Mar 2002 17:43:30 +0000 (17:43 +0000)
Originally committed as revision 354 to svn://svn.ffmpeg.org/ffmpeg/trunk

libavcodec/avcodec.h
libavcodec/h263.c
libavcodec/mpegvideo.c
libavcodec/mpegvideo.h

index beb9936e7313fa99144496f5d219470c676e254a..0bca2b4fdad6909cc4c159ea4b3ca209388c3981 100644 (file)
@@ -145,6 +145,17 @@ typedef struct AVCodecContext {
     float psnr_y;
     float psnr_cb;
     float psnr_cr;
+    
+    /* statistics, used for 2-pass encoding */
+    int mv_bits;
+    int header_bits;
+    int i_tex_bits;
+    int p_tex_bits;
+    int i_count;
+    int p_count;
+    int skip_count;
+    int misc_bits; // cbp, mb_type
+    int frame_bits;
                  
     /* the following fields are ignored */
     void *opaque;   /* can be used to carry app specific stuff */
index 2b6400fc99fe63e1aeeb48784e9a710d010cc4c2..329db317be58cf6df7beaeb867077aa0a84583e9 100644 (file)
@@ -245,6 +245,7 @@ void mpeg4_encode_mb(MpegEncContext * s,
                    int motion_x, int motion_y)
 {
     int cbpc, cbpy, i, cbp, pred_x, pred_y;
+    int bits;
     
     //    printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y);
     if (!s->mb_intra) {
@@ -257,6 +258,9 @@ void mpeg4_encode_mb(MpegEncContext * s,
         if ((cbp | motion_x | motion_y) == 0) {
             /* skip macroblock */
             put_bits(&s->pb, 1, 1);
+            s->misc_bits++;
+            s->last_bits++;
+            s->skip_count++;
             return;
         }
         put_bits(&s->pb, 1, 0);        /* mb coded */
@@ -267,6 +271,10 @@ void mpeg4_encode_mb(MpegEncContext * s,
         cbpy = cbp >> 2;
         cbpy ^= 0xf;
         put_bits(&s->pb, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]);
+            
+        bits= get_bit_count(&s->pb);
+        s->misc_bits+= bits - s->last_bits;
+        s->last_bits=bits;
 
         /* motion vectors: 16x16 mode only now */
         h263_pred_motion(s, 0, &pred_x, &pred_y);
@@ -274,10 +282,18 @@ void mpeg4_encode_mb(MpegEncContext * s,
         h263_encode_motion(s, motion_x - pred_x);
         h263_encode_motion(s, motion_y - pred_y);
 
+        bits= get_bit_count(&s->pb);
+        s->mv_bits+= bits - s->last_bits;
+        s->last_bits=bits;
+
         /* encode each block */
         for (i = 0; i < 6; i++) {
             mpeg4_encode_block(s, block[i], i, 0, zigzag_direct);
         }
+        bits= get_bit_count(&s->pb);
+        s->p_tex_bits+= bits - s->last_bits;
+        s->last_bits=bits;
+        s->p_count++;
     } else {
         int dc_diff[6];   //dc values with the dc prediction subtracted 
         int dir[6];  //prediction direction
@@ -340,11 +356,20 @@ void mpeg4_encode_mb(MpegEncContext * s,
         cbpy = cbp >> 2;
         put_bits(&s->pb, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]);
 
+        bits= get_bit_count(&s->pb);
+        s->misc_bits+= bits - s->last_bits;
+        s->last_bits=bits;
+
         /* encode each block */
         for (i = 0; i < 6; i++) {
             mpeg4_encode_block(s, block[i], i, dc_diff[i], scan_table[i]);
         }
 
+        bits= get_bit_count(&s->pb);
+        s->i_tex_bits+= bits - s->last_bits;
+        s->last_bits=bits;
+        s->i_count++;
+
         /* restore ac coeffs & last_index stuff if we messed them up with the prediction */
         if(s->ac_pred){
             for(i=0; i<6; i++){
index 9510ca1847c7aef841fa201a92183bbce49a3579..839fe1a0e4e60f8d315d5ec6523fe34df946dcd9 100644 (file)
@@ -542,7 +542,15 @@ int MPV_encode_picture(AVCodecContext *avctx,
 
     encode_picture(s, s->picture_number);
     avctx->key_frame = (s->pict_type == I_TYPE);
-    
+    avctx->header_bits = s->header_bits;
+    avctx->mv_bits     = s->mv_bits;
+    avctx->misc_bits   = s->misc_bits;
+    avctx->i_tex_bits  = s->i_tex_bits;
+    avctx->p_tex_bits  = s->p_tex_bits;
+    avctx->i_count     = s->i_count;
+    avctx->p_count     = s->p_count;
+    avctx->skip_count  = s->skip_count;
+
     MPV_frame_end(s);
     s->picture_number++;
     s->picture_in_gop_number++;
@@ -554,6 +562,9 @@ int MPV_encode_picture(AVCodecContext *avctx,
     s->last_frame_bits= s->frame_bits;
     s->frame_bits  = (pbBufPtr(&s->pb) - s->pb.buf) * 8;
     s->total_bits += s->frame_bits;
+    avctx->frame_bits  = s->frame_bits;
+//printf("fcode: %d, type: %d, head: %d, mv: %d, misc: %d, frame: %d, itex: %d, ptex: %d\n", 
+//s->f_code, avctx->key_frame, s->header_bits, s->mv_bits, s->misc_bits, s->frame_bits, s->i_tex_bits, s->p_tex_bits);
 
     avctx->quality = s->qscale;
     if (avctx->get_psnr) {
@@ -1071,6 +1082,7 @@ static void encode_picture(MpegEncContext *s, int picture_number)
     int mb_x, mb_y, wrap, last_gob, pdif = 0;
     UINT8 *ptr;
     int i, motion_x, motion_y;
+    int bits;
 
     s->picture_number = picture_number;
 
@@ -1134,7 +1146,7 @@ static void encode_picture(MpegEncContext *s, int picture_number)
 
         for(i=MAX_FCODE; i>1; i--){
             loose+= mv_num[i];
-            if(loose > 4) break; //FIXME this is pretty ineffective
+            if(loose > 10) break; //FIXME this is pretty ineffective
         }
         s->f_code= i;
     }else{
@@ -1179,6 +1191,7 @@ static void encode_picture(MpegEncContext *s, int picture_number)
         convert_matrix(s->q_non_intra_matrix, s->q_non_intra_matrix16, s->non_intra_matrix, s->qscale);
     }
 
+    s->last_bits= get_bit_count(&s->pb);
     switch(s->out_format) {
     case FMT_MJPEG:
         mjpeg_picture_header(s);
@@ -1197,7 +1210,17 @@ static void encode_picture(MpegEncContext *s, int picture_number)
         mpeg1_encode_picture_header(s, picture_number);
         break;
     }
-        
+    bits= get_bit_count(&s->pb);
+    s->header_bits= bits - s->last_bits;
+    s->last_bits= bits;
+    s->mv_bits=0;
+    s->misc_bits=0;
+    s->i_tex_bits=0;
+    s->p_tex_bits=0;
+    s->i_count=0;
+    s->p_count=0;
+    s->skip_count=0;
+
     /* init last dc values */
     /* note: quant matrix value (8) is implied here */
     s->last_dc[0] = 128;
@@ -1372,7 +1395,7 @@ static void encode_picture(MpegEncContext *s, int picture_number)
                 mjpeg_encode_mb(s, s->block);
                 break;
             }
-
+            
             /* decompress blocks so that we keep the state of the decoder */
             s->mv[0][0][0] = motion_x;
             s->mv[0][0][1] = motion_y;
@@ -1394,7 +1417,7 @@ static void encode_picture(MpegEncContext *s, int picture_number)
             s->first_gob_line = 0;
         }
     }
-    
+
     if (s->h263_msmpeg4 && s->pict_type == I_TYPE)
         msmpeg4_encode_ext_header(s);
 
index 1d0dd4f0274ceef3c32fc3e33a3279987ea79403..b784081929004a29ac5db9cb8d4e5e2593b90815 100644 (file)
@@ -178,6 +178,17 @@ typedef struct MpegEncContext {
     double short_term_qsum;   /* sum of recent qscales */
     double short_term_qcount; /* count of recent qscales */
 
+    /* statistics, used for 2-pass encoding */
+    int mv_bits;
+    int header_bits;
+    int i_tex_bits;
+    int p_tex_bits;
+    int i_count;
+    int p_count;
+    int skip_count;
+    int misc_bits; // cbp, mb_type
+    int last_bits; //temp var used for calculating the above vars
+
     /* H.263 specific */
     int gob_number;
     int gob_index;