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 );
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 );
/* 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 */
}
/* 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");
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 );