diff --git a/x264.c b/x264.c index e8febc2..1f9c79c 100644 --- a/x264.c +++ b/x264.c @@ -23,7 +23,7 @@ #include #include - +#include #include #define _GNU_SOURCE #include @@ -47,15 +47,26 @@ uint8_t *mux_buffer = NULL; int mux_buffer_size = 0; /* Ctrl-C handler */ -static int b_ctrl_c = 0; -static int b_exit_on_ctrl_c = 0; -static void SigIntHandler( int a ) +static int b_ctrl_c = 0; +static void sig_int( int a ) { - if( b_exit_on_ctrl_c ) - exit(0); b_ctrl_c = 1; } +static uint64_t time_start, time_end, time_paused; +static void sig_tstp( int a ) +{ + time_paused = x264_mdate(); + kill( getpid(), SIGSTOP ); +} +static void sig_cont( int a ) +{ + if( !time_paused ) + return; + time_start += x264_mdate() - time_paused; + time_paused = 0; +} + typedef struct { int b_progress; int i_seek; @@ -107,8 +118,10 @@ int main( int argc, char **argv ) if( Parse( argc, argv, ¶m, &opt ) < 0 ) return -1; - /* Control-C handler */ - signal( SIGINT, SigIntHandler ); + if( !param.b_visualize ) + signal( SIGINT, sig_int ); + signal( SIGTSTP, sig_tstp ); + signal( SIGCONT, sig_cont ); ret = Encode( ¶m, &opt ); @@ -575,7 +588,6 @@ static int Parse( int argc, char **argv, case OPT_VISUALIZE: #ifdef VISUALIZE param->b_visualize = 1; - b_exit_on_ctrl_c = 1; #else fprintf( stderr, "x264 [warning]: not compiled with visualization support\n" ); #endif @@ -786,7 +798,6 @@ static int Encode( x264_param_t *param, cli_opt_t *opt ) x264_picture_t pic; int i_frame, i_frame_total; - int64_t i_start, i_end; int64_t i_file; int i_frame_size; int i_update_interval; @@ -819,7 +830,7 @@ static int Encode( x264_param_t *param, cli_opt_t *opt ) /* Create a new pic */ x264_picture_alloc( &pic, X264_CSP_I420, param->i_width, param->i_height ); - i_start = x264_mdate(); + time_start = x264_mdate(); /* Encode frames */ for( i_frame = 0, i_file = 0; b_ctrl_c == 0 && (i_frame < i_frame_total || i_frame_total == 0); ) @@ -845,7 +856,7 @@ static int Encode( x264_param_t *param, cli_opt_t *opt ) /* update status line (up to 1000 times per input file) */ if( opt->b_progress && i_frame % i_update_interval == 0 ) { - int64_t i_elapsed = x264_mdate() - i_start; + int64_t i_elapsed = x264_mdate() - time_start; double fps = i_elapsed > 0 ? i_frame * 1000000. / i_elapsed : 0; double bitrate = (double) i_file * 8 * param->i_fps_num / ( (double) param->i_fps_den * i_frame * 1000 ); if( i_frame_total ) @@ -870,7 +881,7 @@ static int Encode( x264_param_t *param, cli_opt_t *opt ) i_frame_size = Encode_frame( h, opt->hout, NULL ); } while( i_frame_size ); - i_end = x264_mdate(); + time_end = x264_mdate(); x264_picture_clean( &pic ); /* Erase progress indicator before printing encoding stats. */ if( opt->b_progress ) @@ -888,7 +899,7 @@ static int Encode( x264_param_t *param, cli_opt_t *opt ) if( i_frame > 0 ) { double fps = (double)i_frame * (double)1000000 / - (double)( i_end - i_start ); + (double)( time_end - time_start ); fprintf( stderr, "encoded %d frames, %.2f fps, %.2f kb/s\n", i_frame, fps, (double) i_file * 8 * param->i_fps_num /