Commit d81fa19a authored by Loren Merritt's avatar Loren Merritt

In N-pass mode if stat_in and stat_out are the same file, instead save to a...

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
parent dc270b76
......@@ -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 );
......
......@@ -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 );
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment