changeset 286:91f7c38f5f37 libavcodec

statistics for 2-pass encoding
author michaelni
date Sat, 23 Mar 2002 17:43:30 +0000
parents 39b78867b71f
children 32a3dcce2e9b
files avcodec.h h263.c mpegvideo.c mpegvideo.h
diffstat 4 files changed, 75 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/avcodec.h	Sat Mar 23 13:00:08 2002 +0000
+++ b/avcodec.h	Sat Mar 23 17:43:30 2002 +0000
@@ -145,6 +145,17 @@
     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 */
--- a/h263.c	Sat Mar 23 13:00:08 2002 +0000
+++ b/h263.c	Sat Mar 23 17:43:30 2002 +0000
@@ -245,6 +245,7 @@
 		    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 @@
         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 @@
         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 @@
         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 @@
         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++){
--- a/mpegvideo.c	Sat Mar 23 13:00:08 2002 +0000
+++ b/mpegvideo.c	Sat Mar 23 17:43:30 2002 +0000
@@ -542,7 +542,15 @@
 
     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 @@
     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 @@
     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 @@
 
         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 @@
         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 @@
         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 @@
                 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 @@
             s->first_gob_line = 0;
         }
     }
-    
+
     if (s->h263_msmpeg4 && s->pict_type == I_TYPE)
         msmpeg4_encode_ext_header(s);
 
--- a/mpegvideo.h	Sat Mar 23 13:00:08 2002 +0000
+++ b/mpegvideo.h	Sat Mar 23 17:43:30 2002 +0000
@@ -178,6 +178,17 @@
     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;