]> git.sesse.net Git - x264/blobdiff - muxers.c
Fix RD early-skip
[x264] / muxers.c
index c16b2ffae2e962f4b8cac38df6846ef158b29ccd..5c74f132b77845c85e1fa1c7058696a756d731d4 100644 (file)
--- a/muxers.c
+++ b/muxers.c
@@ -63,6 +63,8 @@ typedef struct {
 int open_file_yuv( char *psz_filename, hnd_t *p_handle, x264_param_t *p_param )
 {
     yuv_input_t *h = malloc(sizeof(yuv_input_t));
+    if( !h )
+        return -1;
     h->width = p_param->i_width;
     h->height = p_param->i_height;
     h->next_frame = 0;
@@ -138,10 +140,11 @@ typedef struct {
 int open_file_y4m( char *psz_filename, hnd_t *p_handle, x264_param_t *p_param )
 {
     int  i, n, d;
-    int  interlaced;
     char header[MAX_YUV4_HEADER+10];
     char *tokstart, *tokend, *header_end;
     y4m_input_t *h = malloc(sizeof(y4m_input_t));
+    if( !h )
+        return -1;
 
     h->next_frame = 0;
 
@@ -197,12 +200,12 @@ int open_file_y4m( char *psz_filename, hnd_t *p_handle, x264_param_t *p_param )
         case 'I': /* Interlace type */
             switch(*tokstart++)
             {
-            case 'p': interlaced = 0; break;
+            case 'p': break;
             case '?':
             case 't':
             case 'b':
             case 'm':
-            default: interlaced = 1;
+            default:
                 fprintf(stderr, "Warning, this sequence might be interlaced\n");
             }
             break;
@@ -216,7 +219,8 @@ int open_file_y4m( char *psz_filename, hnd_t *p_handle, x264_param_t *p_param )
             tokstart = strchr(tokstart, 0x20);
             break;
         case 'A': /* Pixel aspect - 0:0 if unknown */
-            if( sscanf(tokstart, "%d:%d", &n, &d) == 2 && n && d )
+            /* Don't override the aspect ratio if sar has been explicitly set on the commandline. */
+            if( sscanf(tokstart, "%d:%d", &n, &d) == 2 && n && d && !p_param->vui.i_sar_width && !p_param->vui.i_sar_height )
             {
                 x264_reduce_fraction( &n, &d );
                 p_param->vui.i_sar_width = n;
@@ -285,15 +289,15 @@ int read_frame_y4m( x264_picture_t *p_pic, hnd_t handle, int i_frame )
     /* Read frame header - without terminating '\n' */
     if (fread(header, 1, slen, h->fh) != slen)
         return -1;
-    
+
     header[slen] = 0;
     if (strncmp(header, Y4M_FRAME_MAGIC, slen))
     {
-        fprintf(stderr, "Bad header magic (%08X <=> %s)\n",
+        fprintf(stderr, "Bad header magic (%"PRIx32" <=> %s)\n",
                 *((uint32_t*)header), header);
         return -1;
     }
-  
+
     /* Skip most of it */
     while (i<MAX_FRAME_HEADER && fgetc(h->fh) != '\n')
         i++;
@@ -335,6 +339,8 @@ typedef struct {
 int open_file_avis( char *psz_filename, hnd_t *p_handle, x264_param_t *p_param )
 {
     avis_input_t *h = malloc(sizeof(avis_input_t));
+    if( !h )
+        return -1;
     AVISTREAMINFO info;
     int i;
 
@@ -426,6 +432,7 @@ typedef struct {
     x264_pthread_t tid;
     int next_frame;
     int frame_total;
+    int in_progress;
     struct thread_input_arg_t *next_args;
 } thread_input_t;
 
@@ -439,12 +446,19 @@ typedef struct thread_input_arg_t {
 int open_file_thread( char *psz_filename, hnd_t *p_handle, x264_param_t *p_param )
 {
     thread_input_t *h = malloc(sizeof(thread_input_t));
-    x264_picture_alloc( &h->pic, X264_CSP_I420, p_param->i_width, p_param->i_height );
+    if( !h || x264_picture_alloc( &h->pic, X264_CSP_I420, p_param->i_width, p_param->i_height ) < 0 )
+    {
+        fprintf( stderr, "x264 [error]: malloc failed\n" );
+        return -1;
+    }
     h->p_read_frame = p_read_frame;
     h->p_close_infile = p_close_infile;
     h->p_handle = *p_handle;
+    h->in_progress = 0;
     h->next_frame = -1;
     h->next_args = malloc(sizeof(thread_input_arg_t));
+    if( !h->next_args )
+        return -1;
     h->next_args->h = h;
     h->next_args->status = 0;
     h->frame_total = p_get_frame_total( h->p_handle );
@@ -459,7 +473,7 @@ int get_frame_total_thread( hnd_t handle )
     return h->frame_total;
 }
 
-void read_frame_thread_int( thread_input_arg_t *i )
+static void read_frame_thread_int( thread_input_arg_t *i )
 {
     i->status = i->h->p_read_frame( i->pic, i->h->p_handle, i->i_frame );
 }
@@ -467,13 +481,13 @@ void read_frame_thread_int( thread_input_arg_t *i )
 int read_frame_thread( x264_picture_t *p_pic, hnd_t handle, int i_frame )
 {
     thread_input_t *h = handle;
-    UNUSED void *stuff;
     int ret = 0;
 
     if( h->next_frame >= 0 )
     {
-        x264_pthread_join( h->tid, &stuff );
+        x264_pthread_join( h->tid, NULL );
         ret |= h->next_args->status;
+        h->in_progress = 0;
     }
 
     if( h->next_frame == i_frame )
@@ -490,7 +504,9 @@ int read_frame_thread( x264_picture_t *p_pic, hnd_t handle, int i_frame )
         h->next_frame =
         h->next_args->i_frame = i_frame+1;
         h->next_args->pic = &h->pic;
-        x264_pthread_create( &h->tid, NULL, (void*)read_frame_thread_int, h->next_args );
+        if( x264_pthread_create( &h->tid, NULL, (void*)read_frame_thread_int, h->next_args ) )
+            return -1;
+        h->in_progress = 1;
     }
     else
         h->next_frame = -1;
@@ -501,8 +517,11 @@ int read_frame_thread( x264_picture_t *p_pic, hnd_t handle, int i_frame )
 int close_file_thread( hnd_t handle )
 {
     thread_input_t *h = handle;
+    if( h->in_progress )
+        x264_pthread_join( h->tid, NULL );
     h->p_close_infile( h->p_handle );
     x264_picture_clean( &h->pic );
+    free( h->next_args );
     free( h );
     return 0;
 }
@@ -561,7 +580,7 @@ typedef struct
 } mp4_t;
 
 
-void recompute_bitrate_mp4(GF_ISOFile *p_file, int i_track)
+static void recompute_bitrate_mp4(GF_ISOFile *p_file, int i_track)
 {
     u32 i, count, di, timescale, time_wnd, rate;
     u64 offset;
@@ -724,8 +743,12 @@ int write_nalu_mp4( hnd_t handle, uint8_t *p_nalu, int i_size )
             p_mp4->p_config->profile_compatibility = p_nalu[6];
             p_mp4->p_config->AVCLevelIndication = p_nalu[7];
             p_slot = (GF_AVCConfigSlot *)malloc(sizeof(GF_AVCConfigSlot));
+            if( !p_slot )
+                return -1;
             p_slot->size = i_size - 4;
             p_slot->data = (char *)malloc(p_slot->size);
+            if( !p_slot->data )
+                return -1;
             memcpy(p_slot->data, p_nalu + 4, i_size - 4);
             gf_list_add(p_mp4->p_config->sequenceParameterSets, p_slot);
             p_slot = NULL;
@@ -738,8 +761,12 @@ int write_nalu_mp4( hnd_t handle, uint8_t *p_nalu, int i_size )
         if (!p_mp4->b_pps)
         {
             p_slot = (GF_AVCConfigSlot *)malloc(sizeof(GF_AVCConfigSlot));
+            if( !p_slot )
+                return -1;
             p_slot->size = i_size - 4;
             p_slot->data = (char *)malloc(p_slot->size);
+            if( !p_slot->data )
+                return -1;
             memcpy(p_slot->data, p_nalu + 4, i_size - 4);
             gf_list_add(p_mp4->p_config->pictureParameterSets, p_slot);
             p_slot = NULL;
@@ -804,7 +831,7 @@ typedef struct
     char      b_writing_frame;
 } mkv_t;
 
-int write_header_mkv( mkv_t *p_mkv )
+static int write_header_mkv( mkv_t *p_mkv )
 {
     int       ret;
     uint8_t   *avcC;