From: Loren Merritt Date: Sat, 12 Feb 2005 12:26:52 +0000 (+0000) Subject: In N-pass mode if stat_in and stat_out are the same file, instead save to a temp... X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=d81fa19a0af848bd97b2250e1405b5fac54820b1;p=libx264 In N-pass mode if stat_in and stat_out are the same file, instead save to a temp file and overwrite stat_in only when the encode finishes. git-svn-id: svn://svn.videolan.org/x264/trunk@122 df754926-b1dd-0310-bc7b-ec298dee348c --- diff --git a/encoder/encoder.c b/encoder/encoder.c index 3f485185..799e483b 100644 --- a/encoder/encoder.c +++ b/encoder/encoder.c @@ -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 ); diff --git a/encoder/ratecontrol.c b/encoder/ratecontrol.c index c405cba1..c541418e 100644 --- a/encoder/ratecontrol.c +++ b/encoder/ratecontrol.c @@ -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 );