]> granicus.if.org Git - libx264/commitdiff
In N-pass mode if stat_in and stat_out are the same file, instead save to a temp...
authorLoren Merritt <pengvado@videolan.org>
Sat, 12 Feb 2005 12:26:52 +0000 (12:26 +0000)
committerLoren Merritt <pengvado@videolan.org>
Sat, 12 Feb 2005 12:26:52 +0000 (12:26 +0000)
git-svn-id: svn://svn.videolan.org/x264/trunk@122 df754926-b1dd-0310-bc7b-ec298dee348c

encoder/encoder.c
encoder/ratecontrol.c

index 3f48518572054e32d145622cf9932d930bba6669..799e483b39bbc3b1d9140577001cbffafc94d981 100644 (file)
@@ -1478,6 +1478,9 @@ void    x264_encoder_close  ( x264_t *h )
         x264_frame_delete( h->frames.reference[i] );
     }
 
+    /* rc */
+    x264_ratecontrol_delete( h );
+
     /* param */
     if( h->param.rc.psz_stat_out )
         free( h->param.rc.psz_stat_out );
@@ -1486,9 +1489,6 @@ void    x264_encoder_close  ( x264_t *h )
     if( h->param.rc.psz_rc_eq )
         free( h->param.rc.psz_rc_eq );
 
-    /* rc */
-    x264_ratecontrol_delete( h );
-
     x264_macroblock_cache_end( h );
     x264_free( h->out.p_bitstream );
     x264_free( h );
index c405cba1087f09ab7f9c1aba5125c709ea95412e..c541418e3475b432fb903106d98ab06acf27e249 100644 (file)
@@ -98,6 +98,7 @@ struct x264_ratecontrol_t
 
     /* 2pass stuff */
     FILE *p_stat_file_out;
+    char *psz_stat_file_tmpname;
 
     int num_entries;            /* number of ratecontrol_entry_ts */
     ratecontrol_entry_t *entry; /* FIXME: copy needed data and free this once init is done */
@@ -321,9 +322,20 @@ int x264_ratecontrol_new( x264_t *h )
     }
 
     /* Open output file */
+    /* If input and output files are the same, output to a temp file
+     * and move it to the real name only when it's complete */
     if( h->param.rc.b_stat_write )
     {
-        rc->p_stat_file_out = fopen( h->param.rc.psz_stat_out, "wb" );
+        if( h->param.rc.b_stat_read && !strcmp( h->param.rc.psz_stat_in, h->param.rc.psz_stat_out ) )
+        {
+            rc->psz_stat_file_tmpname = x264_malloc( strlen(h->param.rc.psz_stat_out) + 5 );
+            strcpy( rc->psz_stat_file_tmpname, h->param.rc.psz_stat_out );
+            strcat( rc->psz_stat_file_tmpname, ".new" );
+        }
+        else
+            rc->psz_stat_file_tmpname = h->param.rc.psz_stat_out;
+
+        rc->p_stat_file_out = fopen( rc->psz_stat_file_tmpname, "wb" );
         if( rc->p_stat_file_out == NULL )
         {
             x264_log(h, X264_LOG_ERROR, "ratecontrol_init: can't open stats file\n");
@@ -339,7 +351,15 @@ void x264_ratecontrol_delete( x264_t *h )
     x264_ratecontrol_t *rc = h->rc;
 
     if( rc->p_stat_file_out )
+    {
         fclose( rc->p_stat_file_out );
+        if( rc->psz_stat_file_tmpname != h->param.rc.psz_stat_out )
+        {
+            if( h->i_frame >= rc->num_entries - h->param.i_bframe )
+                rename( rc->psz_stat_file_tmpname, h->param.rc.psz_stat_out );
+            x264_free( rc->psz_stat_file_tmpname );
+        }
+    }
     if( rc->entry )
         x264_free(rc->entry);
     x264_free( rc );