]> git.videolan.org Git - x264.git/commitdiff
If frame count is known at init time (cli & vfw), then abort if the 2nd pass
authorLoren Merritt <pengvado@videolan.org>
Wed, 26 Oct 2005 07:04:59 +0000 (07:04 +0000)
committerLoren Merritt <pengvado@videolan.org>
Wed, 26 Oct 2005 07:04:59 +0000 (07:04 +0000)
exceeds the length of the 1st pass.
If it's not known (mencoder), then report a non-fatal error when we run off the
end of the 1st pass stats, and switch to constant QP.

git-svn-id: svn://svn.videolan.org/x264/trunk@340 df754926-b1dd-0310-bc7b-ec298dee348c

encoder/encoder.c
encoder/ratecontrol.c
vfw/codec.c
vfw/x264vfw.h
x264.c
x264.h

index 0618761c7568bd0182fb721108c08fce09006688..43964e2712f4c96d0e98b910caae40b4fbda3919 100644 (file)
@@ -382,6 +382,7 @@ static int x264_validate_parameters( x264_t *h )
     h->param.i_bframe = x264_clip3( h->param.i_bframe, 0, X264_BFRAME_MAX );
     h->param.i_bframe_bias = x264_clip3( h->param.i_bframe_bias, -90, 100 );
     h->param.b_bframe_pyramid = h->param.b_bframe_pyramid && h->param.i_bframe > 1;
+    h->param.b_bframe_adaptive = h->param.b_bframe_adaptive && h->param.i_bframe > 0;
 
     h->param.i_deblocking_filter_alphac0 = x264_clip3( h->param.i_deblocking_filter_alphac0, -6, 6 );
     h->param.i_deblocking_filter_beta    = x264_clip3( h->param.i_deblocking_filter_beta, -6, 6 );
index 8650a103f03bdcd31d0c151d39339377c3ab11b6..a349217d67eec208cf1aa50515d697dce79f81e2 100644 (file)
@@ -277,11 +277,25 @@ int x264_ratecontrol_new( x264_t *h )
             x264_log(h, X264_LOG_ERROR, "empty stats file\n");
             return -1;
         }
-        i += h->param.i_bframe;
-        rc->entry = (ratecontrol_entry_t*) x264_malloc(i*sizeof(ratecontrol_entry_t));
-        memset(rc->entry, 0, i*sizeof(ratecontrol_entry_t));
-        /* FIXME: num_entries is sometimes treated as number of frames in the video */
-        rc->num_entries= i;
+        rc->num_entries = i;
+
+        if( h->param.i_frame_total < rc->num_entries )
+        {
+            x264_log( h, X264_LOG_WARNING, "2nd pass has fewer frames than 1st pass (%d vs %d)\n",
+                      h->param.i_frame_total, rc->num_entries );
+        }
+        if( h->param.i_frame_total > rc->num_entries + h->param.i_bframe )
+        {
+            x264_log( h, X264_LOG_ERROR, "2nd pass has more frames than 1st pass (%d vs %d)\n",
+                      h->param.i_frame_total, rc->num_entries );
+            return -1;
+        }
+
+        /* FIXME: ugly padding because VfW drops delayed B-frames */
+        rc->num_entries += h->param.i_bframe;
+
+        rc->entry = (ratecontrol_entry_t*) x264_malloc(rc->num_entries * sizeof(ratecontrol_entry_t));
+        memset(rc->entry, 0, rc->num_entries * sizeof(ratecontrol_entry_t));
 
         /* init all to skipped p frames */
         for(i=0; i<rc->num_entries; i++){
@@ -495,20 +509,42 @@ int x264_ratecontrol_qp( x264_t *h )
 /* In 2pass, force the same frame types as in the 1st pass */
 int x264_ratecontrol_slice_type( x264_t *h, int frame_num )
 {
+    x264_ratecontrol_t *rc = h->rc;
     if( h->param.rc.b_stat_read )
     {
-        if( frame_num >= h->rc->num_entries )
+        if( frame_num >= rc->num_entries )
         {
-            x264_log(h, X264_LOG_ERROR, "More input frames than in the 1st pass\n");
+            /* We could try to initialize everything required for ABR and
+             * adaptive B-frames, but that would be complicated.
+             * So just calculate the average QP used so far. */
+
+            h->param.rc.i_qp_constant = (h->stat.i_slice_count[SLICE_TYPE_P] == 0) ? 24
+                                      : 1 + h->stat.i_slice_qp[SLICE_TYPE_P] / h->stat.i_slice_count[SLICE_TYPE_P];
+            rc->qp_constant[SLICE_TYPE_P] = x264_clip3( h->param.rc.i_qp_constant, 0, 51 );
+            rc->qp_constant[SLICE_TYPE_I] = x264_clip3( (int)( qscale2qp( qp2qscale( h->param.rc.i_qp_constant ) / fabs( h->param.rc.f_ip_factor )) + 0.5 ), 0, 51 );
+            rc->qp_constant[SLICE_TYPE_B] = x264_clip3( (int)( qscale2qp( qp2qscale( h->param.rc.i_qp_constant ) * fabs( h->param.rc.f_pb_factor )) + 0.5 ), 0, 51 );
+
+            x264_log(h, X264_LOG_ERROR, "2nd pass has more frames than 1st pass (%d)\n", rc->num_entries);
+            x264_log(h, X264_LOG_ERROR, "continuing anyway, at constant QP=%d\n", h->param.rc.i_qp_constant);
+            if( h->param.b_bframe_adaptive )
+                x264_log(h, X264_LOG_ERROR, "disabling adaptive B-frames\n");
+
+            rc->b_abr = 0;
+            rc->b_2pass = 0;
+            h->param.rc.b_cbr = 0;
+            h->param.rc.b_stat_read = 0;
+            h->param.b_bframe_adaptive = 0;
+            if( h->param.i_bframe > 1 )
+                h->param.i_bframe = 1;
             return X264_TYPE_P;
         }
-        switch( h->rc->entry[frame_num].pict_type )
+        switch( rc->entry[frame_num].pict_type )
         {
             case SLICE_TYPE_I:
-                return h->rc->entry[frame_num].kept_as_ref ? X264_TYPE_IDR : X264_TYPE_I;
+                return rc->entry[frame_num].kept_as_ref ? X264_TYPE_IDR : X264_TYPE_I;
 
             case SLICE_TYPE_B:
-                return h->rc->entry[frame_num].kept_as_ref ? X264_TYPE_BREF : X264_TYPE_B;
+                return rc->entry[frame_num].kept_as_ref ? X264_TYPE_BREF : X264_TYPE_B;
 
             case SLICE_TYPE_P:
             default:
index 9ad897e8f0a60876bcd75ced6fd220bd1ebc25a6..27b85822304e0fdfa49a976704b721e8418a7907 100644 (file)
@@ -130,6 +130,7 @@ LRESULT compress_frames_info(CODEC * codec, ICCOMPRESSFRAMES * icf )
 {
     codec->fincr = icf->dwScale;
     codec->fbase = icf->dwRate;
+    codec->config.i_frame_total = icf->lFrameCount;
     return ICERR_OK;
 }
 
@@ -213,6 +214,7 @@ LRESULT compress_begin(CODEC * codec, BITMAPINFO * lpbiInput, BITMAPINFO * lpbiO
 
     param.i_fps_num = codec->fbase;
     param.i_fps_den = codec->fincr;
+    param.i_frame_total = config->i_frame_total;
 
     param.i_frame_reference = config->i_refmax;
     param.i_keyint_min = config->i_keyint_min;
index c853cc7828311b1be411b7a98ea6841981984291..6ce83e5456bcd4718c64d93ecddf1a681bd38258 100644 (file)
@@ -46,6 +46,8 @@ typedef struct
     int b_fast1pass;    /* turns off some flags during 1st pass */    
     int b_updatestats;  /* updates the statsfile during 2nd pass */
 
+    int i_frame_total;
+
     /* Our config */
     int i_refmax;
     int i_keyint_max;
diff --git a/x264.c b/x264.c
index 1c31ed2632cba047632ef3f7c30690cb5cf9b6b9..e5aacd8fbb06a8e74168927255d34475a0fc5d22 100644 (file)
--- a/x264.c
+++ b/x264.c
@@ -73,7 +73,6 @@ static void    SigIntHandler( int a )
 typedef struct {
     int b_decompress;
     int b_progress;
-    int i_maxframes;
     int i_seek;
     hnd_t hin;
     hnd_t hout;
@@ -637,7 +636,7 @@ static int  Parse( int argc, char **argv,
                 param->cpu = 0;
                 break;
             case OPT_FRAMES:
-                opt->i_maxframes = atoi( optarg );
+                param->i_frame_total = atoi( optarg );
                 break;
             case OPT_SEEK:
                 opt->i_seek = atoi( optarg );
@@ -1041,6 +1040,11 @@ static int  Encode( x264_param_t *param, cli_opt_t *opt )
     int     i_progress;
 
     i_frame_total = p_get_frame_total( opt->hin, param->i_width, param->i_height );
+    i_frame_total -= opt->i_seek;
+    if( ( i_frame_total == 0 || param->i_frame_total < i_frame_total )
+        && param->i_frame_total > 0 )
+        i_frame_total = param->i_frame_total;
+    param->i_frame_total = i_frame_total;
 
     if( ( h = x264_encoder_open( param ) ) == NULL )
     {
@@ -1062,10 +1066,8 @@ static int  Encode( x264_param_t *param, cli_opt_t *opt )
     x264_picture_alloc( &pic, X264_CSP_I420, param->i_width, param->i_height );
 
     i_start = x264_mdate();
+
     /* Encode frames */
-    i_frame_total -= opt->i_seek;
-    if( opt->i_maxframes > 0 && opt->i_maxframes < i_frame_total )
-        i_frame_total = opt->i_maxframes;
     for( i_frame = 0, i_file = 0, i_progress = 0;
          b_ctrl_c == 0 && (i_frame < i_frame_total || i_frame_total == 0); )
     {
diff --git a/x264.h b/x264.h
index 13039883bec0c81a9968fe2d5617d6314c97355d..ebbb4d2be8279f33fc2c008e8db10a1e53dd8d3e 100644 (file)
--- a/x264.h
+++ b/x264.h
@@ -35,7 +35,7 @@
 
 #include <stdarg.h>
 
-#define X264_BUILD 37
+#define X264_BUILD 38
 
 /* x264_t:
  *      opaque handler for decoder and encoder */
@@ -127,6 +127,7 @@ typedef struct
     int         i_height;
     int         i_csp;  /* CSP of encoded bitstream, only i420 supported */
     int         i_level_idc; 
+    int         i_frame_total; /* number of frames to encode if known, else 0 */
 
     struct
     {