[x264-devel] [PATCH 2/2] Add an option to sync output when possible

Corentin Chary corentin.chary at gmail.com
Fri Aug 5 16:45:14 CEST 2011


---
 output/output.h |    1 +
 output/raw.c    |   52 ++++++++++++++++++++++++++++++++++++++++++++++------
 x264.c          |    6 +++++-
 3 files changed, 52 insertions(+), 7 deletions(-)

diff --git a/output/output.h b/output/output.h
index c7fa758..45377ac 100644
--- a/output/output.h
+++ b/output/output.h
@@ -32,6 +32,7 @@
 typedef struct
 {
     int use_dts_compress;
+    int sync;
 } cli_output_opt_t;
 
 typedef struct
diff --git a/output/raw.c b/output/raw.c
index d098419..b9db906 100644
--- a/output/raw.c
+++ b/output/raw.c
@@ -26,12 +26,33 @@
 
 #include "output.h"
 
+typedef struct
+{
+    FILE *p_file;
+    int b_sync;
+} raw_hnd_t;
+
 static int open_file( char *psz_filename, hnd_t *p_handle, cli_output_opt_t *opt )
 {
+    raw_hnd_t *p_raw;
+    FILE *fh;
+
+    *p_handle = NULL;
+
     if( !strcmp( psz_filename, "-" ) )
-        *p_handle = stdout;
-    else if( !(*p_handle = fopen( psz_filename, "w+b" )) )
+        fh = stdout;
+    else if( !(fh = fopen( psz_filename, "w+b" )) )
+        return -1;
+
+    if( !(p_raw = malloc( sizeof(raw_hnd_t) )) ) {
+        fclose(fh);
         return -1;
+    }
+
+    p_raw->p_file = fh;
+    p_raw->b_sync = opt->sync;
+
+    *p_handle = p_raw;
 
     return 0;
 }
@@ -43,26 +64,45 @@ static int set_param( hnd_t handle, x264_param_t *p_param )
 
 static int write_headers( hnd_t handle, x264_nal_t *p_nal )
 {
+    raw_hnd_t *p_raw = handle;
     int size = p_nal[0].i_payload + p_nal[1].i_payload + p_nal[2].i_payload;
 
-    if( fwrite( p_nal[0].p_payload, size, 1, (FILE*)handle ) )
+    if( fwrite( p_nal[0].p_payload, size, 1, p_raw->p_file ) ) {
+        if ( p_raw->b_sync )
+            fflush( p_raw->p_file );
         return size;
+    }
     return -1;
 }
 
 static int write_frame( hnd_t handle, uint8_t *p_nalu, int i_size, x264_picture_t *p_picture )
 {
-    if( fwrite( p_nalu, i_size, 1, (FILE*)handle ) )
+    raw_hnd_t *p_raw = handle;
+
+    if( fwrite( p_nalu, i_size, 1, p_raw->p_file ) )
+    {
+        if ( p_raw->b_sync )
+            fflush( p_raw->p_file );
         return i_size;
+    }
     return -1;
 }
 
 static int close_file( hnd_t handle, int64_t largest_pts, int64_t second_largest_pts )
 {
-    if( !handle || handle == stdout )
+    raw_hnd_t *p_raw = handle;
+    FILE *fh;
+
+    if( !p_raw || !p_raw->p_file )
         return 0;
 
-    return fclose( (FILE*)handle );
+    fh = p_raw->p_file;
+    free( p_raw );
+
+    if ( fh == stdout )
+        return fflush( fh );
+
+    return fclose( fh );
 }
 
 const cli_output_t raw_output = { open_file, set_param, write_headers, write_frame, close_file };
diff --git a/x264.c b/x264.c
index 355954d..c666a10 100644
--- a/x264.c
+++ b/x264.c
@@ -816,7 +816,8 @@ typedef enum
     OPT_INPUT_CSP,
     OPT_INPUT_DEPTH,
     OPT_DTS_COMPRESSION,
-    OPT_OUTPUT_CSP
+    OPT_OUTPUT_CSP,
+    OPT_OUTPUT_SYNC
 } OptionsOPT;
 
 static char short_options[] = "8A:B:b:f:hI:i:m:o:p:q:r:t:Vvw";
@@ -975,6 +976,7 @@ static struct option long_options[] =
     { "input-depth", required_argument, NULL, OPT_INPUT_DEPTH },
     { "dts-compress",      no_argument, NULL, OPT_DTS_COMPRESSION },
     { "output-csp",  required_argument, NULL, OPT_OUTPUT_CSP },
+    { "output-sync",       no_argument, NULL, OPT_OUTPUT_SYNC },
     {0, 0, 0, 0}
 };
 
@@ -1366,6 +1368,8 @@ static int parse( int argc, char **argv, x264_param_t *param, cli_opt_t *opt )
                 FAIL_IF_ERROR( parse_enum_value( optarg, output_csp_names, &output_csp ), "Unknown output csp `%s'\n", optarg )
                 // correct the parsed value to the libx264 csp value
                 output_csp = !output_csp ? X264_CSP_I420 : (output_csp == 1 ? X264_CSP_I444 : X264_CSP_RGB);
+            case OPT_OUTPUT_SYNC:
+                output_opt.sync = 1;
                 break;
             default:
 generic_option:
-- 
1.7.3.4



More information about the x264-devel mailing list