]> git.sesse.net Git - mlt/blobdiff - src/modules/avformat/consumer_avformat.c
Fix crash on reading uncompressed (rawvideo).
[mlt] / src / modules / avformat / consumer_avformat.c
index 7757bb7bf9f15e9f440bdc823f36c7f5a2cf6599..9dacb50a52991f1a6ce3ca1af66b9fbc76c533d8 100644 (file)
@@ -23,6 +23,7 @@
 #include <framework/mlt_consumer.h>
 #include <framework/mlt_frame.h>
 #include <framework/mlt_profile.h>
+#include <framework/mlt_log.h>
 
 // System header files
 #include <stdio.h>
@@ -31,7 +32,6 @@
 #include <limits.h>
 #include <pthread.h>
 #include <sys/time.h>
-#include <math.h>
 #include <unistd.h>
 
 // avformat header files
@@ -46,6 +46,8 @@
 #define PIX_FMT_YUYV422 PIX_FMT_YUV422
 #endif
 
+#define AUDIO_ENCODE_BUFFER_SIZE (48000 * 2)
+
 //
 // This structure should be extended and made globally available in mlt
 //
@@ -259,7 +261,7 @@ static int consumer_start( mlt_consumer this )
                        }
                        else
                        {
-                               fprintf( stderr, "%s: Invalid size property %s - ignoring.\n", __FILE__, size );
+                               mlt_log_warning( MLT_CONSUMER_SERVICE( this ), "Invalid size property %s - ignoring.\n", size );
                        }
                }
                
@@ -348,7 +350,7 @@ static int consumer_is_stopped( mlt_consumer this )
 /** Process properties as AVOptions and apply to AV context obj
 */
 
-static void apply_properties( void *obj, mlt_properties properties, int flags )
+static void apply_properties( void *obj, mlt_properties properties, int flags, int alloc )
 {
        int i;
        int count = mlt_properties_count( properties ); 
@@ -358,9 +360,9 @@ static void apply_properties( void *obj, mlt_properties properties, int flags )
                const AVOption *opt = av_find_opt( obj, opt_name, NULL, flags, flags );
                if ( opt != NULL )
 #if LIBAVCODEC_VERSION_INT >= ((52<<16)+(7<<8)+0)
-                       av_set_string3( obj, opt_name, mlt_properties_get( properties, opt_name), 0, NULL );
+                       av_set_string3( obj, opt_name, mlt_properties_get( properties, opt_name), alloc, NULL );
 #elif LIBAVCODEC_VERSION_INT >= ((51<<16)+(59<<8)+0)
-                       av_set_string2( obj, opt_name, mlt_properties_get( properties, opt_name), 0 );
+                       av_set_string2( obj, opt_name, mlt_properties_get( properties, opt_name), alloc );
 #else
                        av_set_string( obj, opt_name, mlt_properties_get( properties, opt_name) );
 #endif
@@ -388,13 +390,16 @@ static AVStream *add_audio_stream( mlt_consumer this, AVFormatContext *oc, int c
 
                c->codec_id = codec_id;
                c->codec_type = CODEC_TYPE_AUDIO;
+               c->sample_fmt = SAMPLE_FMT_S16;
 
+#if 0 // disabled until some audio codecs are multi-threaded
                // Setup multi-threading
                int thread_count = mlt_properties_get_int( properties, "threads" );
                if ( thread_count == 0 && getenv( "MLT_AVFORMAT_THREADS" ) )
                        thread_count = atoi( getenv( "MLT_AVFORMAT_THREADS" ) );
                if ( thread_count > 1 )
                        avcodec_thread_init( c, thread_count );         
+#endif
        
                if (oc->oformat->flags & AVFMT_GLOBALHEADER) 
                        c->flags |= CODEC_FLAG_GLOBAL_HEADER;
@@ -411,7 +416,14 @@ static AVStream *add_audio_stream( mlt_consumer this, AVFormatContext *oc, int c
                }
 
                // Process properties as AVOptions
-               apply_properties( c, properties, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM );
+               char *apre = mlt_properties_get( properties, "apre" );
+               if ( apre )
+               {
+                       mlt_properties p = mlt_properties_load( apre );
+                       apply_properties( c, p, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM, 1 );
+                       mlt_properties_close( p );
+               }
+               apply_properties( c, properties, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM, 0 );
 
                int audio_qscale = mlt_properties_get_int( properties, "aq" );
         if ( audio_qscale > QSCALE_NONE )
@@ -422,6 +434,7 @@ static AVStream *add_audio_stream( mlt_consumer this, AVFormatContext *oc, int c
 
                // Set parameters controlled by MLT
                c->sample_rate = mlt_properties_get_int( properties, "frequency" );
+               c->time_base = ( AVRational ){ 1, c->sample_rate };
                c->channels = mlt_properties_get_int( properties, "channels" );
 
                if ( mlt_properties_get( properties, "alang" ) != NULL )
@@ -429,7 +442,7 @@ static AVStream *add_audio_stream( mlt_consumer this, AVFormatContext *oc, int c
        }
        else
        {
-               fprintf( stderr, "%s: Could not allocate a stream for audio\n", __FILE__ );
+               mlt_log_error( MLT_CONSUMER_SERVICE( this ), "Could not allocate a stream for audio\n" );
        }
 
        return st;
@@ -472,14 +485,14 @@ static int open_audio( AVFormatContext *oc, AVStream *st, int audio_outbuf_size
                }
 
                // Some formats want stream headers to be seperate (hmm)
-               if( !strcmp( oc->oformat->name, "mp4" ) || 
-                       !strcmp( oc->oformat->name, "mov" ) || 
-                       !strcmp( oc->oformat->name, "3gp" ) )
+               if ( !strcmp( oc->oformat->name, "mp4" ) ||
+                        !strcmp( oc->oformat->name, "mov" ) ||
+                        !strcmp( oc->oformat->name, "3gp" ) )
                        c->flags |= CODEC_FLAG_GLOBAL_HEADER;
        }
        else
        {
-               fprintf( stderr, "%s: Unable to encode audio - disabling audio output.\n", __FILE__ );
+               mlt_log_warning( NULL, "%s: Unable to encode audio - disabling audio output.\n", __FILE__ );
        }
 
        return audio_input_frame_size;
@@ -487,7 +500,8 @@ static int open_audio( AVFormatContext *oc, AVStream *st, int audio_outbuf_size
 
 static void close_audio( AVFormatContext *oc, AVStream *st )
 {
-       avcodec_close( st->codec );
+       if ( st && st->codec )
+               avcodec_close( st->codec );
 }
 
 /** Add a video output stream 
@@ -520,7 +534,14 @@ static AVStream *add_video_stream( mlt_consumer this, AVFormatContext *oc, int c
                        avcodec_thread_init( c, thread_count );         
        
                // Process properties as AVOptions
-               apply_properties( c, properties, AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM );
+               char *vpre = mlt_properties_get( properties, "vpre" );
+               if ( vpre )
+               {
+                       mlt_properties p = mlt_properties_load( vpre );
+                       apply_properties( c, p, AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM, 1 );
+                       mlt_properties_close( p );
+               }
+               apply_properties( c, properties, AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM, 0 );
 
                // Set options controlled by MLT
                c->width = mlt_properties_get_int( properties, "width" );
@@ -597,9 +618,9 @@ static AVStream *add_video_stream( mlt_consumer this, AVFormatContext *oc, int c
                if ( mlt_properties_get_int( properties, "progressive" ) == 0 &&
                     mlt_properties_get_int( properties, "deinterlace" ) == 0 )
                {
-                       if ( mlt_properties_get_int( properties, "ildct" ) )
+                       if ( ! mlt_properties_get( properties, "ildct" ) || mlt_properties_get_int( properties, "ildct" ) )
                                c->flags |= CODEC_FLAG_INTERLACED_DCT;
-                       if ( mlt_properties_get_int( properties, "ilme" ) )
+                       if ( ! mlt_properties_get( properties, "ilme" ) || mlt_properties_get_int( properties, "ilme" ) )
                                c->flags |= CODEC_FLAG_INTERLACED_ME;
                }
                
@@ -611,7 +632,7 @@ static AVStream *add_video_stream( mlt_consumer this, AVFormatContext *oc, int c
                        int start, end, q;
                        int e = sscanf( rc_override, "%d,%d,%d", &start, &end, &q );
                        if ( e != 3 )
-                               fprintf( stderr, "%s: Error parsing rc_override\n", __FILE__ );
+                               mlt_log_warning( MLT_CONSUMER_SERVICE( this ), "Error parsing rc_override\n" );
                        c->rc_override = av_realloc( c->rc_override, sizeof( RcOverride ) * ( i + 1 ) );
                        c->rc_override[i].start_frame = start;
                        c->rc_override[i].end_frame = end;
@@ -673,7 +694,7 @@ static AVStream *add_video_stream( mlt_consumer this, AVFormatContext *oc, int c
                                        fseek( f, 0, SEEK_SET );
                                        logbuffer = av_malloc( size + 1 );
                                        if ( !logbuffer )
-                                               fprintf( stderr, "%s: Could not allocate log buffer\n", __FILE__ );
+                                               mlt_log_fatal( MLT_CONSUMER_SERVICE( this ), "Could not allocate log buffer\n" );
                                        else
                                        {
                                                size = fread( logbuffer, 1, size, f );
@@ -688,7 +709,7 @@ static AVStream *add_video_stream( mlt_consumer this, AVFormatContext *oc, int c
        }
        else
        {
-               fprintf( stderr, "%s: Could not allocate a stream for video\n", __FILE__ );
+               mlt_log_error( MLT_CONSUMER_SERVICE( this ), "Could not allocate a stream for video\n" );
        }
  
        return st;
@@ -748,7 +769,8 @@ static int open_video(AVFormatContext *oc, AVStream *st)
 
 void close_video(AVFormatContext *oc, AVStream *st)
 {
-       avcodec_close(st->codec);
+       if ( st && st->codec )
+               avcodec_close(st->codec);
 }
 
 static inline long time_difference( struct timeval *time1 )
@@ -789,14 +811,14 @@ static void *consumer_thread( void *arg )
        int img_height = height;
 
        // Get default audio properties
-       mlt_audio_format aud_fmt = mlt_audio_pcm;
+       mlt_audio_format aud_fmt = mlt_audio_s16;
        int channels = mlt_properties_get_int( properties, "channels" );
        int frequency = mlt_properties_get_int( properties, "frequency" );
        int16_t *pcm = NULL;
        int samples = 0;
 
        // AVFormat audio buffer and frame size
-       int audio_outbuf_size = 10000;
+       int audio_outbuf_size = ( 1024 * 42 );
        uint8_t *audio_outbuf = av_malloc( audio_outbuf_size );
        int audio_input_frame_size = 0;
 
@@ -822,11 +844,15 @@ static void *consumer_thread( void *arg )
        mlt_image_format img_fmt = mlt_image_yuv422;
 
        // For receiving audio samples back from the fifo
-       int16_t *buffer = av_malloc( 48000 * 2 );
+       int16_t *buffer = av_malloc( AUDIO_ENCODE_BUFFER_SIZE );
        int count = 0;
 
        // Allocate the context
+#if (LIBAVFORMAT_VERSION_INT >= ((52<<16)+(26<<8)+0))
+       AVFormatContext *oc = avformat_alloc_context( );
+#else
        AVFormatContext *oc = av_alloc_format_context( );
+#endif
 
        // Streams
        AVStream *audio_st = NULL;
@@ -845,7 +871,7 @@ static void *consumer_thread( void *arg )
 
        // Determine the format
        AVOutputFormat *fmt = NULL;
-       char *filename = mlt_properties_get( properties, "target" );
+       const char *filename = mlt_properties_get( properties, "target" );
        char *format = mlt_properties_get( properties, "f" );
        char *vcodec = mlt_properties_get( properties, "vcodec" );
        char *acodec = mlt_properties_get( properties, "acodec" );
@@ -856,15 +882,27 @@ static void *consumer_thread( void *arg )
 
        // Check for user selected format first
        if ( format != NULL )
+#if LIBAVFORMAT_VERSION_INT < ((52<<16)+(45<<8)+0)
                fmt = guess_format( format, NULL, NULL );
+#else
+               fmt = av_guess_format( format, NULL, NULL );
+#endif
 
        // Otherwise check on the filename
        if ( fmt == NULL && filename != NULL )
+#if LIBAVFORMAT_VERSION_INT < ((52<<16)+(45<<8)+0)
                fmt = guess_format( NULL, filename, NULL );
+#else
+               fmt = av_guess_format( NULL, filename, NULL );
+#endif
 
        // Otherwise default to mpeg
        if ( fmt == NULL )
+#if LIBAVFORMAT_VERSION_INT < ((52<<16)+(45<<8)+0)
                fmt = guess_format( "mpeg", NULL, NULL );
+#else
+               fmt = av_guess_format( "mpeg", NULL, NULL );
+#endif
 
        // We need a filename - default to stdout?
        if ( filename == NULL || !strcmp( filename, "" ) )
@@ -875,7 +913,7 @@ static void *consumer_thread( void *arg )
        video_codec_id = fmt->video_codec;
 
        // Check for audio codec overides
-       if ( ( acodec && strcmp( "acodec", "none" ) == 0 ) || mlt_properties_get_int( properties, "an" ) )
+       if ( ( acodec && strcmp( acodec, "none" ) == 0 ) || mlt_properties_get_int( properties, "an" ) )
                audio_codec_id = CODEC_ID_NONE;
        else if ( acodec )
        {
@@ -883,11 +921,11 @@ static void *consumer_thread( void *arg )
                if ( p != NULL )
                        audio_codec_id = p->id;
                else
-                       fprintf( stderr, "%s: audio codec %s unrecognised - ignoring\n", __FILE__, acodec );
+                       mlt_log_warning( MLT_CONSUMER_SERVICE( this ), "audio codec %s unrecognised - ignoring\n", acodec );
        }
 
        // Check for video codec overides
-       if ( ( vcodec && strcmp( "vcodec", "none" ) == 0 ) || mlt_properties_get_int( properties, "vn" ) )
+       if ( ( vcodec && strcmp( vcodec, "none" ) == 0 ) || mlt_properties_get_int( properties, "vn" ) )
                video_codec_id = CODEC_ID_NONE;
        else if ( vcodec )
        {
@@ -895,7 +933,7 @@ static void *consumer_thread( void *arg )
                if ( p != NULL )
                        video_codec_id = p->id;
                else
-                       fprintf( stderr, "%s: video codec %s unrecognised - ignoring\n", __FILE__, vcodec );
+                       mlt_log_warning( MLT_CONSUMER_SERVICE( this ), "video codec %s unrecognised - ignoring\n", vcodec );
        }
 
        // Write metadata
@@ -939,19 +977,30 @@ static void *consumer_thread( void *arg )
                oc->max_delay= ( int )( mlt_properties_get_double( properties, "muxdelay" ) * AV_TIME_BASE );
 
                // Process properties as AVOptions
-               apply_properties( oc, properties, AV_OPT_FLAG_ENCODING_PARAM );
+               char *fpre = mlt_properties_get( properties, "fpre" );
+               if ( fpre )
+               {
+                       mlt_properties p = mlt_properties_load( fpre );
+                       apply_properties( oc, p, AV_OPT_FLAG_ENCODING_PARAM, 1 );
+                       mlt_properties_close( p );
+               }
+               apply_properties( oc, properties, AV_OPT_FLAG_ENCODING_PARAM, 0 );
 
                if ( video_st && !open_video( oc, video_st ) )
                        video_st = NULL;
                if ( audio_st )
+               {
                        audio_input_frame_size = open_audio( oc, audio_st, audio_outbuf_size );
+                       if ( !audio_input_frame_size )
+                               audio_st = NULL;
+               }
 
                // Open the output file, if needed
                if ( !( fmt->flags & AVFMT_NOFILE ) ) 
                {
                        if ( url_fopen( &oc->pb, filename, URL_WRONLY ) < 0 ) 
                        {
-                               fprintf( stderr, "%s: Could not open '%s'\n", __FILE__, filename );
+                               mlt_log_error( MLT_CONSUMER_SERVICE( this ), "Could not open '%s'\n", filename );
                                mlt_properties_set_int( properties, "running", 0 );
                        }
                }
@@ -962,7 +1011,7 @@ static void *consumer_thread( void *arg )
        }
        else
        {
-               fprintf( stderr, "%s: Invalid output format parameters\n", __FILE__ );
+               mlt_log_error( MLT_CONSUMER_SERVICE( this ), "Invalid output format parameters\n" );
                mlt_properties_set_int( properties, "running", 0 );
        }
 
@@ -978,7 +1027,8 @@ static void *consumer_thread( void *arg )
        gettimeofday( &ante, NULL );
 
        // Loop while running
-       while( mlt_properties_get_int( properties, "running" ) && !terminated )
+       while( mlt_properties_get_int( properties, "running" ) &&
+              ( !terminated || ( video_st && mlt_deque_count( queue ) ) ) )
        {
                // Get the frame
                frame = mlt_consumer_rt_frame( this );
@@ -999,7 +1049,7 @@ static void *consumer_thread( void *arg )
                        if ( !terminated && audio_st )
                        {
                                samples = mlt_sample_calculator( fps, frequency, count ++ );
-                               mlt_frame_get_audio( frame, &pcm, &aud_fmt, &frequency, &channels, &samples );
+                               mlt_frame_get_audio( frame, (void**) &pcm, &aud_fmt, &frequency, &channels, &samples );
 
                                // Create the fifo if we don't have one
                                if ( fifo == NULL )
@@ -1026,42 +1076,42 @@ static void *consumer_thread( void *arg )
                // While we have stuff to process, process...
                while ( 1 )
                {
-                       if (audio_st)
-                               audio_pts = (double)audio_st->pts.val * audio_st->time_base.num / audio_st->time_base.den;
-                       else
-                               audio_pts = 0.0;
-        
-                       if (video_st)
-                               video_pts = (double)video_st->pts.val * video_st->time_base.num / video_st->time_base.den;
-                       else
-                               video_pts = 0.0;
-
                        // Write interleaved audio and video frames
                        if ( !video_st || ( video_st && audio_st && audio_pts < video_pts ) )
                        {
-                               if ( channels * audio_input_frame_size < sample_fifo_used( fifo ) )
+                               if ( ( video_st && terminated ) || ( channels * audio_input_frame_size ) < sample_fifo_used( fifo ) )
                                {
-                                       AVCodecContext *c;
+                                       AVCodecContext *c = audio_st->codec;
                                        AVPacket pkt;
-                                       av_init_packet( &pkt );
-
-                                       c = audio_st->codec;
-
-                                       sample_fifo_fetch( fifo, buffer, channels * audio_input_frame_size );
+                                       int n = FFMIN( FFMIN( channels * audio_input_frame_size, sample_fifo_used( fifo ) ), AUDIO_ENCODE_BUFFER_SIZE );
 
+                                       if ( n > 0 )
+                                               sample_fifo_fetch( fifo, buffer, n );
+                                       else
+                                               memset( buffer, 0, AUDIO_ENCODE_BUFFER_SIZE );
+                                       
+                                       av_init_packet( &pkt );
                                        pkt.size = avcodec_encode_audio( c, audio_outbuf, audio_outbuf_size, buffer );
+                                       
                                        // Write the compressed frame in the media file
                                        if ( c->coded_frame && c->coded_frame->pts != AV_NOPTS_VALUE )
+                                       {
                                                pkt.pts = av_rescale_q( c->coded_frame->pts, c->time_base, audio_st->time_base );
+                                               mlt_log_debug( MLT_CONSUMER_SERVICE( this ), "audio pkt pts %lld frame pts %lld", pkt.pts, c->coded_frame->pts );
+                                       }
                                        pkt.flags |= PKT_FLAG_KEY;
                                        pkt.stream_index= audio_st->index;
                                        pkt.data= audio_outbuf;
 
-                                       if ( pkt.size )
-                                               if ( av_interleaved_write_frame( oc, &pkt ) != 0
-                                                       fprintf( stderr, "%s: Error while writing audio frame\n", __FILE__ );
+                                       if ( pkt.size > 0 )
+                                               if ( av_interleaved_write_frame( oc, &pkt ) != 0 )
+                                                       mlt_log_error( MLT_CONSUMER_SERVICE( this ), "error writing audio frame\n" );
 
-                                       audio_pts += c->frame_size;
+                                       mlt_log_debug( MLT_CONSUMER_SERVICE( this ), " frame_size %d\n", c->frame_size );
+                                       if ( audio_codec_id == CODEC_ID_VORBIS )
+                                               audio_pts = (double)c->coded_frame->pts * av_q2d( audio_st->time_base );
+                                       else
+                                               audio_pts = (double)audio_st->pts.val * av_q2d( audio_st->time_base );
                                }
                                else
                                {
@@ -1125,11 +1175,7 @@ static void *consumer_thread( void *arg )
                                                        for ( i = 0; i < height; i ++ )
                                                        {
                                                                n = ( width + 7 ) / 8;
-                                                               p = output->data[ 0 ] + i * output->linesize[ 0 ];
-
-                                                               #ifndef __DARWIN__
-                                                               p += 3;
-                                                               #endif
+                                                               p = output->data[ 0 ] + i * output->linesize[ 0 ] + 3;
 
                                                                switch( width % 8 )
                                                                {
@@ -1170,18 +1216,20 @@ static void *consumer_thread( void *arg )
                                                // Set frame interlace hints
                                                output->interlaced_frame = !mlt_properties_get_int( frame_properties, "progressive" );
                                                output->top_field_first = mlt_properties_get_int( frame_properties, "top_field_first" );
+                                               output->pts = frame_count;
 
                                                // Encode the image
                                                out_size = avcodec_encode_video(c, video_outbuf, video_outbuf_size, output );
 
                                                // If zero size, it means the image was buffered
-                                               if (out_size > 0) 
+                                               if ( out_size > 0 )
                                                {
                                                        AVPacket pkt;
                                                        av_init_packet( &pkt );
 
                                                        if ( c->coded_frame && c->coded_frame->pts != AV_NOPTS_VALUE )
                                                                pkt.pts= av_rescale_q( c->coded_frame->pts, c->time_base, video_st->time_base );
+                                                       mlt_log_debug( MLT_CONSUMER_SERVICE( this ), "video pkt pts %lld frame pts %lld", pkt.pts, c->coded_frame->pts );
                                                        if( c->coded_frame && c->coded_frame->key_frame )
                                                                pkt.flags |= PKT_FLAG_KEY;
                                                        pkt.stream_index= video_st->index;
@@ -1190,15 +1238,16 @@ static void *consumer_thread( void *arg )
 
                                                        // write the compressed frame in the media file
                                                        ret = av_interleaved_write_frame(oc, &pkt);
-                                                       video_pts += c->frame_size;
+                                                       mlt_log_debug( MLT_CONSUMER_SERVICE( this ), " frame_size %d\n", c->frame_size );
+                                                       video_pts = (double)video_st->pts.val * av_q2d( video_st->time_base );
                                                        
                                                        // Dual pass logging
-                                                       if ( mlt_properties_get_data( properties, "_logfile", NULL ) && c->stats_out)
+                                                       if ( mlt_properties_get_data( properties, "_logfile", NULL ) && c->stats_out )
                                                                fprintf( mlt_properties_get_data( properties, "_logfile", NULL ), "%s", c->stats_out );
                                                } 
-                                               else
+                                               else if ( out_size < 0 )
                                                {
-                                                       fprintf( stderr, "%s: error with video encode\n", __FILE__ );
+                                                       mlt_log_warning( MLT_CONSUMER_SERVICE( this ), "error with video encode %d\n", frame_count );
                                                }
                                        }
                                        frame_count++;
@@ -1209,9 +1258,14 @@ static void *consumer_thread( void *arg )
                                        break;
                                }
                        }
+                       if ( audio_st )
+                               mlt_log_debug( MLT_CONSUMER_SERVICE( this ), "audio pts %lld (%f) ", audio_st->pts.val, audio_pts );
+                       if ( video_st )
+                               mlt_log_debug( MLT_CONSUMER_SERVICE( this ), "video pts %lld (%f) ", video_st->pts.val, video_pts );
+                       mlt_log_debug( MLT_CONSUMER_SERVICE( this ), "\n" );
                }
 
-               if ( real_time_output == 1 && frames % 12 == 0 )
+               if ( real_time_output == 1 && frames % 2 == 0 )
                {
                        long passed = time_difference( &ante );
                        if ( fifo != NULL )
@@ -1285,15 +1339,22 @@ static void *consumer_thread( void *arg )
                        // write the compressed frame in the media file
                        if ( av_interleaved_write_frame( oc, &pkt ) != 0 )
                        {
-                               fprintf( stderr, "%s: Error while writing flushed video frame\n". __FILE__ );
+                               mlt_log_error( MLT_CONSUMER_SERVICE(this), "error while writing flushing video frame\n" );
                                break;
                        }
                }
        }
 #endif
 
-       // close each codec 
-       if (video_st)
+       // XXX ugly hack to prevent x264 from crashing on multi-threaded encoding
+       int pass = mlt_properties_get_int( properties, "pass" );
+       int thread_count = mlt_properties_get_int( properties, "threads" );
+       if ( thread_count == 0 && getenv( "MLT_AVFORMAT_THREADS" ) )
+               thread_count = atoi( getenv( "MLT_AVFORMAT_THREADS" ) );
+       int multithreaded_x264 = ( video_codec_id == CODEC_ID_H264 && thread_count > 1 );
+       
+       // close each codec
+       if ( video_st && !multithreaded_x264 )
                close_video(oc, video_st);
        if (audio_st)
                close_audio(oc, audio_st);
@@ -1330,7 +1391,7 @@ static void *consumer_thread( void *arg )
 
        mlt_consumer_stopped( this );
 
-       if ( mlt_properties_get_int( properties, "pass" ) == 2 )
+       if ( pass == 2 )
        {
                // Remove the dual pass log file
                if ( mlt_properties_get( properties, "_logfilename" ) )
@@ -1338,7 +1399,7 @@ static void *consumer_thread( void *arg )
 
                // Remove the x264 dual pass logs
                char *cwd = getcwd( NULL, 0 );
-               char *file = "x264_2pass.log";
+               const char *file = "x264_2pass.log";
                char *full = malloc( strlen( cwd ) + strlen( file ) + 2 );
                sprintf( full, "%s/%s", cwd, file );
                remove( full );
@@ -1348,6 +1409,11 @@ static void *consumer_thread( void *arg )
                sprintf( full, "%s/%s", cwd, file );
                remove( full );
                free( full );
+               file = "x264_2pass.log.mbtree";
+               full = malloc( strlen( cwd ) + strlen( file ) + 2 );
+               sprintf( full, "%s/%s", cwd, file );
+               remove( full );
+               free( full );
                free( cwd );
                remove( "x264_2pass.log.temp" );
        }