2 * consumer_avformat.c -- an encoder based on avformat
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
5 * Much code borrowed from ffmpeg.c: Copyright (c) 2000-2003 Fabrice Bellard
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <framework/mlt_consumer.h>
24 #include <framework/mlt_frame.h>
25 #include <framework/mlt_profile.h>
26 #include <framework/mlt_log.h>
28 // System header files
37 // avformat header files
44 #if LIBAVUTIL_VERSION_INT < (50<<16)
45 #define PIX_FMT_RGB32 PIX_FMT_RGBA32
46 #define PIX_FMT_YUYV422 PIX_FMT_YUV422
49 #define MAX_AUDIO_STREAMS (8)
50 #define AUDIO_ENCODE_BUFFER_SIZE (48000 * 2 * MAX_AUDIO_STREAMS)
53 // This structure should be extended and made globally available in mlt
65 *sample_fifo, sample_fifo_s;
67 sample_fifo sample_fifo_init( int frequency, int channels )
69 sample_fifo this = calloc( 1, sizeof( sample_fifo_s ) );
70 this->frequency = frequency;
71 this->channels = channels;
75 // sample_fifo_clear and check are temporarily aborted (not working as intended)
77 void sample_fifo_clear( sample_fifo this, double time )
79 int words = ( float )( time - this->time ) * this->frequency * this->channels;
80 if ( ( int )( ( float )time * 100 ) < ( int )( ( float )this->time * 100 ) && this->used > words && words > 0 )
82 memmove( this->buffer, &this->buffer[ words ], ( this->used - words ) * sizeof( int16_t ) );
86 else if ( ( int )( ( float )time * 100 ) != ( int )( ( float )this->time * 100 ) )
93 void sample_fifo_check( sample_fifo this, double time )
95 if ( this->used == 0 )
97 if ( ( int )( ( float )time * 100 ) < ( int )( ( float )this->time * 100 ) )
102 void sample_fifo_append( sample_fifo this, int16_t *samples, int count )
104 if ( ( this->size - this->used ) < count )
106 this->size += count * 5;
107 this->buffer = realloc( this->buffer, this->size * sizeof( int16_t ) );
110 memcpy( &this->buffer[ this->used ], samples, count * sizeof( int16_t ) );
114 int sample_fifo_used( sample_fifo this )
119 int sample_fifo_fetch( sample_fifo this, int16_t *samples, int count )
121 if ( count > this->used )
124 memcpy( samples, this->buffer, count * sizeof( int16_t ) );
126 memmove( this->buffer, &this->buffer[ count ], this->used * sizeof( int16_t ) );
128 this->time += ( double )count / this->channels / this->frequency;
133 void sample_fifo_close( sample_fifo this )
135 free( this->buffer );
139 // Forward references.
140 static int consumer_start( mlt_consumer this );
141 static int consumer_stop( mlt_consumer this );
142 static int consumer_is_stopped( mlt_consumer this );
143 static void *consumer_thread( void *arg );
144 static void consumer_close( mlt_consumer this );
146 /** Initialise the consumer.
149 mlt_consumer consumer_avformat_init( mlt_profile profile, char *arg )
151 // Allocate the consumer
152 mlt_consumer this = mlt_consumer_new( profile );
154 // If memory allocated and initialises without error
157 // Get properties from the consumer
158 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
160 // Assign close callback
161 this->close = consumer_close;
163 // Interpret the argument
165 mlt_properties_set( properties, "target", arg );
167 // sample and frame queue
168 mlt_properties_set_data( properties, "frame_queue", mlt_deque_init( ), 0, ( mlt_destructor )mlt_deque_close, NULL );
170 // Audio options not fully handled by AVOptions
171 #define QSCALE_NONE (-99999)
172 mlt_properties_set_int( properties, "aq", QSCALE_NONE );
174 // Video options not fully handled by AVOptions
175 mlt_properties_set_int( properties, "dc", 8 );
177 // Muxer options not fully handled by AVOptions
178 mlt_properties_set_double( properties, "muxdelay", 0.7 );
179 mlt_properties_set_double( properties, "muxpreload", 0.5 );
181 // Ensure termination at end of the stream
182 mlt_properties_set_int( properties, "terminate_on_pause", 1 );
184 // Default to separate processing threads for producer and consumer with no frame dropping!
185 mlt_properties_set_int( properties, "real_time", -1 );
186 mlt_properties_set_int( properties, "prefill", 1 );
188 // Set up start/stop/terminated callbacks
189 this->start = consumer_start;
190 this->stop = consumer_stop;
191 this->is_stopped = consumer_is_stopped;
198 /** Start the consumer.
201 static int consumer_start( mlt_consumer this )
203 // Get the properties
204 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
207 // Report information about available muxers and codecs as YAML Tiny
208 char *s = mlt_properties_get( properties, "f" );
209 if ( s && strcmp( s, "list" ) == 0 )
211 fprintf( stderr, "---\nformats:\n" );
212 AVOutputFormat *format = NULL;
213 while ( ( format = av_oformat_next( format ) ) )
214 fprintf( stderr, " - %s\n", format->name );
215 fprintf( stderr, "...\n" );
218 s = mlt_properties_get( properties, "acodec" );
219 if ( s && strcmp( s, "list" ) == 0 )
221 fprintf( stderr, "---\naudio_codecs:\n" );
222 AVCodec *codec = NULL;
223 while ( ( codec = av_codec_next( codec ) ) )
224 if ( codec->encode && codec->type == CODEC_TYPE_AUDIO )
225 fprintf( stderr, " - %s\n", codec->name );
226 fprintf( stderr, "...\n" );
229 s = mlt_properties_get( properties, "vcodec" );
230 if ( s && strcmp( s, "list" ) == 0 )
232 fprintf( stderr, "---\nvideo_codecs:\n" );
233 AVCodec *codec = NULL;
234 while ( ( codec = av_codec_next( codec ) ) )
235 if ( codec->encode && codec->type == CODEC_TYPE_VIDEO )
236 fprintf( stderr, " - %s\n", codec->name );
237 fprintf( stderr, "...\n" );
241 // Check that we're not already running
242 if ( !error && !mlt_properties_get_int( properties, "running" ) )
245 pthread_t *thread = calloc( 1, sizeof( pthread_t ) );
247 // Get the width and height
248 int width = mlt_properties_get_int( properties, "width" );
249 int height = mlt_properties_get_int( properties, "height" );
251 // Obtain the size property
252 char *size = mlt_properties_get( properties, "s" );
258 if ( sscanf( size, "%dx%d", &tw, &th ) == 2 && tw > 0 && th > 0 )
265 mlt_log_warning( MLT_CONSUMER_SERVICE( this ), "Invalid size property %s - ignoring.\n", size );
269 // Now ensure we honour the multiple of two requested by libavformat
270 width = ( width / 2 ) * 2;
271 height = ( height / 2 ) * 2;
272 mlt_properties_set_int( properties, "width", width );
273 mlt_properties_set_int( properties, "height", height );
275 // We need to set these on the profile as well because the s property is
276 // an alias to mlt properties that correspond to profile settings.
277 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
280 profile->width = width;
281 profile->height = height;
284 // Handle the ffmpeg command line "-r" property for frame rate
285 if ( mlt_properties_get( properties, "r" ) )
287 double frame_rate = mlt_properties_get_double( properties, "r" );
288 AVRational rational = av_d2q( frame_rate, 255 );
289 mlt_properties_set_int( properties, "frame_rate_num", rational.num );
290 mlt_properties_set_int( properties, "frame_rate_den", rational.den );
293 profile->frame_rate_num = rational.num;
294 profile->frame_rate_den = rational.den;
295 mlt_properties_set_double( properties, "fps", mlt_profile_fps( profile ) );
299 // Apply AVOptions that are synonyms for standard mlt_consumer options
300 if ( mlt_properties_get( properties, "ac" ) )
301 mlt_properties_set_int( properties, "channels", mlt_properties_get_int( properties, "ac" ) );
302 if ( mlt_properties_get( properties, "ar" ) )
303 mlt_properties_set_int( properties, "frequency", mlt_properties_get_int( properties, "ar" ) );
305 // Assign the thread to properties
306 mlt_properties_set_data( properties, "thread", thread, sizeof( pthread_t ), free, NULL );
308 // Set the running state
309 mlt_properties_set_int( properties, "running", 1 );
312 pthread_create( thread, NULL, consumer_thread, this );
317 /** Stop the consumer.
320 static int consumer_stop( mlt_consumer this )
322 // Get the properties
323 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
325 // Check that we're running
326 if ( mlt_properties_get_int( properties, "running" ) )
329 pthread_t *thread = mlt_properties_get_data( properties, "thread", NULL );
332 mlt_properties_set_int( properties, "running", 0 );
334 // Wait for termination
335 pthread_join( *thread, NULL );
341 /** Determine if the consumer is stopped.
344 static int consumer_is_stopped( mlt_consumer this )
346 // Get the properties
347 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
348 return !mlt_properties_get_int( properties, "running" );
351 /** Process properties as AVOptions and apply to AV context obj
354 static void apply_properties( void *obj, mlt_properties properties, int flags, int alloc )
357 int count = mlt_properties_count( properties );
358 for ( i = 0; i < count; i++ )
360 const char *opt_name = mlt_properties_get_name( properties, i );
361 const AVOption *opt = av_find_opt( obj, opt_name, NULL, flags, flags );
363 #if LIBAVCODEC_VERSION_INT >= ((52<<16)+(7<<8)+0)
364 av_set_string3( obj, opt_name, mlt_properties_get( properties, opt_name), alloc, NULL );
365 #elif LIBAVCODEC_VERSION_INT >= ((51<<16)+(59<<8)+0)
366 av_set_string2( obj, opt_name, mlt_properties_get( properties, opt_name), alloc );
368 av_set_string( obj, opt_name, mlt_properties_get( properties, opt_name) );
373 /** Add an audio output stream
376 static AVStream *add_audio_stream( mlt_consumer this, AVFormatContext *oc, int codec_id, int channels )
378 // Get the properties
379 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
381 // Create a new stream
382 AVStream *st = av_new_stream( oc, oc->nb_streams );
384 // If created, then initialise from properties
387 AVCodecContext *c = st->codec;
389 // Establish defaults from AVOptions
390 avcodec_get_context_defaults2( c, CODEC_TYPE_AUDIO );
392 c->codec_id = codec_id;
393 c->codec_type = CODEC_TYPE_AUDIO;
394 c->sample_fmt = SAMPLE_FMT_S16;
396 #if 0 // disabled until some audio codecs are multi-threaded
397 // Setup multi-threading
398 int thread_count = mlt_properties_get_int( properties, "threads" );
399 if ( thread_count == 0 && getenv( "MLT_AVFORMAT_THREADS" ) )
400 thread_count = atoi( getenv( "MLT_AVFORMAT_THREADS" ) );
401 if ( thread_count > 1 )
402 avcodec_thread_init( c, thread_count );
405 if (oc->oformat->flags & AVFMT_GLOBALHEADER)
406 c->flags |= CODEC_FLAG_GLOBAL_HEADER;
408 // Allow the user to override the audio fourcc
409 if ( mlt_properties_get( properties, "atag" ) )
412 char *arg = mlt_properties_get( properties, "atag" );
413 int tag = strtol( arg, &tail, 0);
415 tag = arg[ 0 ] + ( arg[ 1 ] << 8 ) + ( arg[ 2 ] << 16 ) + ( arg[ 3 ] << 24 );
419 // Process properties as AVOptions
420 char *apre = mlt_properties_get( properties, "apre" );
423 mlt_properties p = mlt_properties_load( apre );
424 apply_properties( c, p, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM, 1 );
425 mlt_properties_close( p );
427 apply_properties( c, properties, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM, 0 );
429 int audio_qscale = mlt_properties_get_int( properties, "aq" );
430 if ( audio_qscale > QSCALE_NONE )
432 c->flags |= CODEC_FLAG_QSCALE;
433 c->global_quality = st->quality = FF_QP2LAMBDA * audio_qscale;
436 // Set parameters controlled by MLT
437 c->sample_rate = mlt_properties_get_int( properties, "frequency" );
438 c->time_base = ( AVRational ){ 1, c->sample_rate };
439 c->channels = channels;
441 if ( mlt_properties_get( properties, "alang" ) != NULL )
442 strncpy( st->language, mlt_properties_get( properties, "alang" ), sizeof( st->language ) );
446 mlt_log_error( MLT_CONSUMER_SERVICE( this ), "Could not allocate a stream for audio\n" );
452 static int open_audio( AVFormatContext *oc, AVStream *st, int audio_outbuf_size )
454 // We will return the audio input size from here
455 int audio_input_frame_size = 0;
458 AVCodecContext *c = st->codec;
461 AVCodec *codec = avcodec_find_encoder( c->codec_id );
463 // Continue if codec found and we can open it
464 if ( codec != NULL && avcodec_open( c, codec ) >= 0 )
466 // ugly hack for PCM codecs (will be removed ASAP with new PCM
467 // support to compute the input frame size in samples
468 if ( c->frame_size <= 1 )
470 audio_input_frame_size = audio_outbuf_size / c->channels;
471 switch(st->codec->codec_id)
473 case CODEC_ID_PCM_S16LE:
474 case CODEC_ID_PCM_S16BE:
475 case CODEC_ID_PCM_U16LE:
476 case CODEC_ID_PCM_U16BE:
477 audio_input_frame_size >>= 1;
485 audio_input_frame_size = c->frame_size;
488 // Some formats want stream headers to be seperate (hmm)
489 if ( !strcmp( oc->oformat->name, "mp4" ) ||
490 !strcmp( oc->oformat->name, "mov" ) ||
491 !strcmp( oc->oformat->name, "3gp" ) )
492 c->flags |= CODEC_FLAG_GLOBAL_HEADER;
496 mlt_log_warning( NULL, "%s: Unable to encode audio - disabling audio output.\n", __FILE__ );
499 return audio_input_frame_size;
502 static void close_audio( AVFormatContext *oc, AVStream *st )
504 if ( st && st->codec )
505 avcodec_close( st->codec );
508 /** Add a video output stream
511 static AVStream *add_video_stream( mlt_consumer this, AVFormatContext *oc, int codec_id )
513 // Get the properties
514 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
516 // Create a new stream
517 AVStream *st = av_new_stream( oc, oc->nb_streams );
521 char *pix_fmt = mlt_properties_get( properties, "pix_fmt" );
522 AVCodecContext *c = st->codec;
524 // Establish defaults from AVOptions
525 avcodec_get_context_defaults2( c, CODEC_TYPE_VIDEO );
527 c->codec_id = codec_id;
528 c->codec_type = CODEC_TYPE_VIDEO;
530 // Setup multi-threading
531 int thread_count = mlt_properties_get_int( properties, "threads" );
532 if ( thread_count == 0 && getenv( "MLT_AVFORMAT_THREADS" ) )
533 thread_count = atoi( getenv( "MLT_AVFORMAT_THREADS" ) );
534 if ( thread_count > 1 )
535 avcodec_thread_init( c, thread_count );
537 // Process properties as AVOptions
538 char *vpre = mlt_properties_get( properties, "vpre" );
541 mlt_properties p = mlt_properties_load( vpre );
542 apply_properties( c, p, AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM, 1 );
543 mlt_properties_close( p );
545 apply_properties( c, properties, AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM, 0 );
547 // Set options controlled by MLT
548 c->width = mlt_properties_get_int( properties, "width" );
549 c->height = mlt_properties_get_int( properties, "height" );
550 c->time_base.num = mlt_properties_get_int( properties, "frame_rate_den" );
551 c->time_base.den = mlt_properties_get_int( properties, "frame_rate_num" );
552 if ( st->time_base.den == 0 )
553 st->time_base = c->time_base;
554 c->pix_fmt = pix_fmt ? avcodec_get_pix_fmt( pix_fmt ) : PIX_FMT_YUV420P;
556 if ( mlt_properties_get( properties, "aspect" ) )
558 // "-aspect" on ffmpeg command line is display aspect ratio
559 double ar = mlt_properties_get_double( properties, "aspect" );
560 AVRational rational = av_d2q( ar, 255 );
562 // Update the profile and properties as well since this is an alias
563 // for mlt properties that correspond to profile settings
564 mlt_properties_set_int( properties, "display_aspect_num", rational.num );
565 mlt_properties_set_int( properties, "display_aspect_den", rational.den );
566 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
569 profile->display_aspect_num = rational.num;
570 profile->display_aspect_den = rational.den;
571 mlt_properties_set_double( properties, "display_ratio", mlt_profile_dar( profile ) );
574 // Now compute the sample aspect ratio
575 rational = av_d2q( ar * c->height / c->width, 255 );
576 c->sample_aspect_ratio = rational;
577 // Update the profile and properties as well since this is an alias
578 // for mlt properties that correspond to profile settings
579 mlt_properties_set_int( properties, "sample_aspect_num", rational.num );
580 mlt_properties_set_int( properties, "sample_aspect_den", rational.den );
583 profile->sample_aspect_num = rational.num;
584 profile->sample_aspect_den = rational.den;
585 mlt_properties_set_double( properties, "aspect_ratio", mlt_profile_sar( profile ) );
590 c->sample_aspect_ratio.num = mlt_properties_get_int( properties, "sample_aspect_num" );
591 c->sample_aspect_ratio.den = mlt_properties_get_int( properties, "sample_aspect_den" );
593 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(21<<8)+0)
594 st->sample_aspect_ratio = c->sample_aspect_ratio;
597 if ( mlt_properties_get_double( properties, "qscale" ) > 0 )
599 c->flags |= CODEC_FLAG_QSCALE;
600 st->quality = FF_QP2LAMBDA * mlt_properties_get_double( properties, "qscale" );
603 // Allow the user to override the video fourcc
604 if ( mlt_properties_get( properties, "vtag" ) )
607 const char *arg = mlt_properties_get( properties, "vtag" );
608 int tag = strtol( arg, &tail, 0);
610 tag = arg[ 0 ] + ( arg[ 1 ] << 8 ) + ( arg[ 2 ] << 16 ) + ( arg[ 3 ] << 24 );
614 // Some formats want stream headers to be seperate
615 if ( oc->oformat->flags & AVFMT_GLOBALHEADER )
616 c->flags |= CODEC_FLAG_GLOBAL_HEADER;
618 // Translate these standard mlt consumer properties to ffmpeg
619 if ( mlt_properties_get_int( properties, "progressive" ) == 0 &&
620 mlt_properties_get_int( properties, "deinterlace" ) == 0 )
622 if ( ! mlt_properties_get( properties, "ildct" ) || mlt_properties_get_int( properties, "ildct" ) )
623 c->flags |= CODEC_FLAG_INTERLACED_DCT;
624 if ( ! mlt_properties_get( properties, "ilme" ) || mlt_properties_get_int( properties, "ilme" ) )
625 c->flags |= CODEC_FLAG_INTERLACED_ME;
628 // parse the ratecontrol override string
630 char *rc_override = mlt_properties_get( properties, "rc_override" );
631 for ( i = 0; rc_override; i++ )
634 int e = sscanf( rc_override, "%d,%d,%d", &start, &end, &q );
636 mlt_log_warning( MLT_CONSUMER_SERVICE( this ), "Error parsing rc_override\n" );
637 c->rc_override = av_realloc( c->rc_override, sizeof( RcOverride ) * ( i + 1 ) );
638 c->rc_override[i].start_frame = start;
639 c->rc_override[i].end_frame = end;
642 c->rc_override[i].qscale = q;
643 c->rc_override[i].quality_factor = 1.0;
647 c->rc_override[i].qscale = 0;
648 c->rc_override[i].quality_factor = -q / 100.0;
650 rc_override = strchr( rc_override, '/' );
654 c->rc_override_count = i;
655 if ( !c->rc_initial_buffer_occupancy )
656 c->rc_initial_buffer_occupancy = c->rc_buffer_size * 3/4;
657 c->intra_dc_precision = mlt_properties_get_int( properties, "dc" ) - 8;
660 i = mlt_properties_get_int( properties, "pass" );
662 c->flags |= CODEC_FLAG_PASS1;
664 c->flags |= CODEC_FLAG_PASS2;
665 if ( codec_id != CODEC_ID_H264 && ( c->flags & ( CODEC_FLAG_PASS1 | CODEC_FLAG_PASS2 ) ) )
667 char logfilename[1024];
672 snprintf( logfilename, sizeof(logfilename), "%s_2pass.log",
673 mlt_properties_get( properties, "passlogfile" ) ? mlt_properties_get( properties, "passlogfile" ) : mlt_properties_get( properties, "target" ) );
674 if ( c->flags & CODEC_FLAG_PASS1 )
676 f = fopen( logfilename, "w" );
678 perror( logfilename );
680 mlt_properties_set_data( properties, "_logfile", f, 0, ( mlt_destructor )fclose, NULL );
684 /* read the log file */
685 f = fopen( logfilename, "r" );
692 mlt_properties_set( properties, "_logfilename", logfilename );
693 fseek( f, 0, SEEK_END );
695 fseek( f, 0, SEEK_SET );
696 logbuffer = av_malloc( size + 1 );
698 mlt_log_fatal( MLT_CONSUMER_SERVICE( this ), "Could not allocate log buffer\n" );
701 size = fread( logbuffer, 1, size, f );
703 logbuffer[size] = '\0';
704 c->stats_in = logbuffer;
705 mlt_properties_set_data( properties, "_logbuffer", logbuffer, 0, ( mlt_destructor )av_free, NULL );
713 mlt_log_error( MLT_CONSUMER_SERVICE( this ), "Could not allocate a stream for video\n" );
719 static AVFrame *alloc_picture( int pix_fmt, int width, int height )
722 AVFrame *picture = avcodec_alloc_frame();
724 // Determine size of the
725 int size = avpicture_get_size(pix_fmt, width, height);
727 // Allocate the picture buf
728 uint8_t *picture_buf = av_malloc(size);
730 // If we have both, then fill the image
731 if ( picture != NULL && picture_buf != NULL )
733 // Fill the frame with the allocated buffer
734 avpicture_fill( (AVPicture *)picture, picture_buf, pix_fmt, width, height);
738 // Something failed - clean up what we can
740 av_free( picture_buf );
747 static int open_video(AVFormatContext *oc, AVStream *st)
750 AVCodecContext *video_enc = st->codec;
752 // find the video encoder
753 AVCodec *codec = avcodec_find_encoder( video_enc->codec_id );
755 if( codec && codec->pix_fmts )
757 const enum PixelFormat *p = codec->pix_fmts;
760 if( *p == video_enc->pix_fmt )
764 video_enc->pix_fmt = codec->pix_fmts[ 0 ];
767 // Open the codec safely
768 return codec != NULL && avcodec_open( video_enc, codec ) >= 0;
771 void close_video(AVFormatContext *oc, AVStream *st)
773 if ( st && st->codec )
774 avcodec_close(st->codec);
777 static inline long time_difference( struct timeval *time1 )
779 struct timeval time2;
780 gettimeofday( &time2, NULL );
781 return time2.tv_sec * 1000000 + time2.tv_usec - time1->tv_sec * 1000000 - time1->tv_usec;
784 /** The main thread - the argument is simply the consumer.
787 static void *consumer_thread( void *arg )
789 // Map the argument to the object
790 mlt_consumer this = arg;
792 // Get the properties
793 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
795 // Get the terminate on pause property
796 int terminate_on_pause = mlt_properties_get_int( properties, "terminate_on_pause" );
799 // Determine if feed is slow (for realtime stuff)
800 int real_time_output = mlt_properties_get_int( properties, "real_time" );
805 // Get the frame rate
806 double fps = mlt_properties_get_double( properties, "fps" );
808 // Get width and height
809 int width = mlt_properties_get_int( properties, "width" );
810 int height = mlt_properties_get_int( properties, "height" );
811 int img_width = width;
812 int img_height = height;
814 // Get default audio properties
815 mlt_audio_format aud_fmt = mlt_audio_s16;
816 int channels = mlt_properties_get_int( properties, "channels" );
817 int total_channels = channels;
818 int frequency = mlt_properties_get_int( properties, "frequency" );
822 // AVFormat audio buffer and frame size
823 int audio_outbuf_size = ( 1024 * 42 );
824 uint8_t *audio_outbuf = av_malloc( audio_outbuf_size );
825 int audio_input_frame_size = 0;
827 // AVFormat video buffer and frame count
829 int video_outbuf_size = ( 1024 * 1024 );
830 uint8_t *video_outbuf = av_malloc( video_outbuf_size );
832 // Used for the frame properties
833 mlt_frame frame = NULL;
834 mlt_properties frame_properties = NULL;
837 mlt_deque queue = mlt_properties_get_data( properties, "frame_queue", NULL );
838 sample_fifo fifo = mlt_properties_get_data( properties, "sample_fifo", NULL );
840 // Need two av pictures for converting
841 AVFrame *output = NULL;
842 AVFrame *input = alloc_picture( PIX_FMT_YUYV422, width, height );
844 // For receiving images from an mlt_frame
846 mlt_image_format img_fmt = mlt_image_yuv422;
848 // For receiving audio samples back from the fifo
849 int16_t *audio_buf_1 = av_malloc( AUDIO_ENCODE_BUFFER_SIZE );
850 int16_t *audio_buf_2 = NULL;
853 // Allocate the context
854 #if (LIBAVFORMAT_VERSION_INT >= ((52<<16)+(26<<8)+0))
855 AVFormatContext *oc = avformat_alloc_context( );
857 AVFormatContext *oc = av_alloc_format_context( );
861 AVStream *video_st = NULL;
862 AVStream *audio_st[ MAX_AUDIO_STREAMS ];
863 int is_audio_initialized = 0;
866 double audio_pts = 0;
867 double video_pts = 0;
871 long int total_time = 0;
873 // Determine the format
874 AVOutputFormat *fmt = NULL;
875 const char *filename = mlt_properties_get( properties, "target" );
876 char *format = mlt_properties_get( properties, "f" );
877 char *vcodec = mlt_properties_get( properties, "vcodec" );
878 char *acodec = mlt_properties_get( properties, "acodec" );
880 // Used to store and override codec ids
886 mlt_properties frame_meta_properties = mlt_properties_new();
888 // Initialize audio_st
889 int i = MAX_AUDIO_STREAMS;
893 // Check for user selected format first
894 if ( format != NULL )
895 #if LIBAVFORMAT_VERSION_INT < ((52<<16)+(45<<8)+0)
896 fmt = guess_format( format, NULL, NULL );
898 fmt = av_guess_format( format, NULL, NULL );
901 // Otherwise check on the filename
902 if ( fmt == NULL && filename != NULL )
903 #if LIBAVFORMAT_VERSION_INT < ((52<<16)+(45<<8)+0)
904 fmt = guess_format( NULL, filename, NULL );
906 fmt = av_guess_format( NULL, filename, NULL );
909 // Otherwise default to mpeg
911 #if LIBAVFORMAT_VERSION_INT < ((52<<16)+(45<<8)+0)
912 fmt = guess_format( "mpeg", NULL, NULL );
914 fmt = av_guess_format( "mpeg", NULL, NULL );
917 // We need a filename - default to stdout?
918 if ( filename == NULL || !strcmp( filename, "" ) )
921 // Get the codec ids selected
922 audio_codec_id = fmt->audio_codec;
923 video_codec_id = fmt->video_codec;
925 // Check for audio codec overides
926 if ( ( acodec && strcmp( acodec, "none" ) == 0 ) || mlt_properties_get_int( properties, "an" ) )
927 audio_codec_id = CODEC_ID_NONE;
930 AVCodec *p = avcodec_find_encoder_by_name( acodec );
932 audio_codec_id = p->id;
934 mlt_log_warning( MLT_CONSUMER_SERVICE( this ), "audio codec %s unrecognised - ignoring\n", acodec );
937 // Check for video codec overides
938 if ( ( vcodec && strcmp( vcodec, "none" ) == 0 ) || mlt_properties_get_int( properties, "vn" ) )
939 video_codec_id = CODEC_ID_NONE;
942 AVCodec *p = avcodec_find_encoder_by_name( vcodec );
944 video_codec_id = p->id;
946 mlt_log_warning( MLT_CONSUMER_SERVICE( this ), "video codec %s unrecognised - ignoring\n", vcodec );
953 tmp = mlt_properties_get( properties, "meta.attr.title.markup");
954 if (tmp != NULL) snprintf( oc->title, sizeof(oc->title), "%s", tmp );
956 tmp = mlt_properties_get( properties, "meta.attr.comment.markup");
957 if (tmp != NULL) snprintf( oc->comment, sizeof(oc->comment), "%s", tmp );
959 tmp = mlt_properties_get( properties, "meta.attr.author.markup");
960 if (tmp != NULL) snprintf( oc->author, sizeof(oc->author), "%s", tmp );
962 tmp = mlt_properties_get( properties, "meta.attr.copyright.markup");
963 if (tmp != NULL) snprintf( oc->copyright, sizeof(oc->copyright), "%s", tmp );
965 tmp = mlt_properties_get( properties, "meta.attr.album.markup");
966 if (tmp != NULL) snprintf( oc->album, sizeof(oc->album), "%s", tmp );
968 metavalue = mlt_properties_get_int( properties, "meta.attr.year.markup");
969 if (metavalue != 0) oc->year = metavalue;
971 metavalue = mlt_properties_get_int( properties, "meta.attr.track.markup");
972 if (metavalue != 0) oc->track = metavalue;
975 snprintf( oc->filename, sizeof(oc->filename), "%s", filename );
977 // Add audio and video streams
978 if ( video_codec_id != CODEC_ID_NONE )
979 video_st = add_video_stream( this, oc, video_codec_id );
980 if ( audio_codec_id != CODEC_ID_NONE )
986 for ( i = 0; i < MAX_AUDIO_STREAMS; i++ )
988 sprintf( key, "channels.%d", i );
989 int j = mlt_properties_get_int( properties, key );
994 audio_st[i] = add_audio_stream( this, oc, audio_codec_id, j );
1000 audio_st[0] = add_audio_stream( this, oc, audio_codec_id, channels );
1001 total_channels = channels;
1005 // Set the parameters (even though we have none...)
1006 if ( av_set_parameters(oc, NULL) >= 0 )
1008 oc->preload = ( int )( mlt_properties_get_double( properties, "muxpreload" ) * AV_TIME_BASE );
1009 oc->max_delay= ( int )( mlt_properties_get_double( properties, "muxdelay" ) * AV_TIME_BASE );
1011 // Process properties as AVOptions
1012 char *fpre = mlt_properties_get( properties, "fpre" );
1015 mlt_properties p = mlt_properties_load( fpre );
1016 apply_properties( oc, p, AV_OPT_FLAG_ENCODING_PARAM, 1 );
1017 mlt_properties_close( p );
1019 apply_properties( oc, properties, AV_OPT_FLAG_ENCODING_PARAM, 0 );
1021 if ( video_st && !open_video( oc, video_st ) )
1023 for ( i = 0; i < MAX_AUDIO_STREAMS && audio_st[i]; i++ )
1025 audio_input_frame_size = open_audio( oc, audio_st[i], audio_outbuf_size );
1026 if ( !audio_input_frame_size )
1030 // Open the output file, if needed
1031 if ( !( fmt->flags & AVFMT_NOFILE ) )
1033 if ( url_fopen( &oc->pb, filename, URL_WRONLY ) < 0 )
1035 mlt_log_error( MLT_CONSUMER_SERVICE( this ), "Could not open '%s'\n", filename );
1036 mlt_properties_set_int( properties, "running", 0 );
1040 // Write the stream header.
1041 if ( mlt_properties_get_int( properties, "running" ) )
1042 av_write_header( oc );
1046 mlt_log_error( MLT_CONSUMER_SERVICE( this ), "Invalid output format parameters\n" );
1047 mlt_properties_set_int( properties, "running", 0 );
1052 output = alloc_picture( video_st->codec->pix_fmt, width, height );
1054 // Last check - need at least one stream
1055 if ( !audio_st[0] && !video_st )
1056 mlt_properties_set_int( properties, "running", 0 );
1058 // Get the starting time (can ignore the times above)
1059 gettimeofday( &ante, NULL );
1061 // Loop while running
1062 while( mlt_properties_get_int( properties, "running" ) &&
1063 ( !terminated || ( video_st && mlt_deque_count( queue ) ) ) )
1065 frame = mlt_consumer_rt_frame( this );
1067 // Check that we have a frame to work with
1068 if ( frame != NULL )
1070 // Increment frames dispatched
1073 // Default audio args
1074 frame_properties = MLT_FRAME_PROPERTIES( frame );
1076 // Check for the terminated condition
1077 terminated = terminate_on_pause && mlt_properties_get_double( frame_properties, "_speed" ) == 0.0;
1079 // Get audio and append to the fifo
1080 if ( !terminated && audio_st[0] )
1082 samples = mlt_sample_calculator( fps, frequency, count ++ );
1083 mlt_frame_get_audio( frame, (void**) &pcm, &aud_fmt, &frequency, &channels, &samples );
1085 // Save the audio channel remap properties for later
1086 mlt_properties_pass( frame_meta_properties, frame_properties, "meta.map.audio." );
1088 // Create the fifo if we don't have one
1091 fifo = sample_fifo_init( frequency, channels );
1092 mlt_properties_set_data( properties, "sample_fifo", fifo, 0, ( mlt_destructor )sample_fifo_close, NULL );
1095 // Silence if not normal forward speed
1096 if ( mlt_properties_get_double( frame_properties, "_speed" ) != 1.0 )
1097 memset( pcm, 0, samples * channels * 2 );
1099 // Append the samples
1100 sample_fifo_append( fifo, pcm, samples * channels );
1101 total_time += ( samples * 1000000 ) / frequency;
1105 if ( !terminated && video_st )
1106 mlt_deque_push_back( queue, frame );
1108 mlt_frame_close( frame );
1111 // While we have stuff to process, process...
1114 // Write interleaved audio and video frames
1115 if ( !video_st || ( video_st && audio_st[0] && audio_pts < video_pts ) )
1118 if ( ( video_st && terminated ) || ( channels * audio_input_frame_size ) < sample_fifo_used( fifo ) )
1120 int j = 0; // channel offset into interleaved source buffer
1121 int n = FFMIN( FFMIN( channels * audio_input_frame_size, sample_fifo_used( fifo ) ), AUDIO_ENCODE_BUFFER_SIZE );
1123 // Get the audio samples
1125 sample_fifo_fetch( fifo, audio_buf_1, n );
1127 memset( audio_buf_1, 0, AUDIO_ENCODE_BUFFER_SIZE );
1128 samples = n / channels;
1130 // For each output stream
1131 for ( i = 0; i < MAX_AUDIO_STREAMS && audio_st[i] && j < total_channels; i++ )
1133 AVStream *stream = audio_st[i];
1134 AVCodecContext *codec = stream->codec;
1137 av_init_packet( &pkt );
1139 // Optimized for single track and no channel remap
1140 if ( !audio_st[1] && !mlt_properties_count( frame_meta_properties ) )
1142 pkt.size = avcodec_encode_audio( codec, audio_outbuf, audio_outbuf_size, audio_buf_1 );
1146 // Extract the audio channels according to channel mapping
1147 int dest_offset = 0; // channel offset into interleaved dest buffer
1149 // Get the number of channels for this stream
1150 sprintf( key, "channels.%d", i );
1151 int current_channels = mlt_properties_get_int( properties, key );
1153 // Clear the destination audio buffer.
1155 audio_buf_2 = av_mallocz( AUDIO_ENCODE_BUFFER_SIZE );
1157 memset( audio_buf_2, 0, AUDIO_ENCODE_BUFFER_SIZE );
1159 // For each output channel
1160 while ( dest_offset < current_channels && j < total_channels )
1162 int map_start = -1, map_channels = 0;
1163 int source_offset = 0;
1166 // Look for a mapping that starts at j
1167 for ( k = 0; k < (MAX_AUDIO_STREAMS * 2) && map_start != j; k++ )
1169 sprintf( key, "%d.channels", k );
1170 map_channels = mlt_properties_get_int( frame_meta_properties, key );
1171 sprintf( key, "%d.start", k );
1172 if ( mlt_properties_get( frame_meta_properties, key ) )
1173 map_start = mlt_properties_get_int( frame_meta_properties, key );
1174 if ( map_start != j )
1175 source_offset += map_channels;
1179 if ( map_start != j )
1181 map_channels = current_channels;
1185 // Copy samples if source offset valid
1186 if ( source_offset < channels )
1188 // Interleave the audio buffer with the # channels for this stream/mapping.
1189 for ( k = 0; k < map_channels; k++, j++, source_offset++, dest_offset++ )
1191 int16_t *src = audio_buf_1 + source_offset;
1192 int16_t *dest = audio_buf_2 + dest_offset;
1193 int s = samples + 1;
1197 dest += current_channels;
1202 // Otherwise silence
1205 j += current_channels;
1206 dest_offset += current_channels;
1209 pkt.size = avcodec_encode_audio( codec, audio_outbuf, audio_outbuf_size, audio_buf_2 );
1212 // Write the compressed frame in the media file
1213 if ( codec->coded_frame && codec->coded_frame->pts != AV_NOPTS_VALUE )
1215 pkt.pts = av_rescale_q( codec->coded_frame->pts, codec->time_base, stream->time_base );
1216 mlt_log_debug( MLT_CONSUMER_SERVICE( this ), "audio stream %d pkt pts %lld frame pts %lld",
1217 stream->index, pkt.pts, codec->coded_frame->pts );
1219 pkt.flags |= PKT_FLAG_KEY;
1220 pkt.stream_index = stream->index;
1221 pkt.data = audio_outbuf;
1223 if ( pkt.size > 0 && stream->pts.den )
1225 if ( av_interleaved_write_frame( oc, &pkt ) )
1226 mlt_log_error( MLT_CONSUMER_SERVICE( this ), "error writing audio frame %d\n", frames - 1 );
1229 mlt_log_debug( MLT_CONSUMER_SERVICE( this ), " frame_size %d\n", codec->frame_size );
1232 if ( audio_codec_id == CODEC_ID_VORBIS )
1233 audio_pts = (double)codec->coded_frame->pts * av_q2d( stream->time_base );
1235 audio_pts = (double)stream->pts.val * av_q2d( stream->time_base );
1244 else if ( video_st )
1247 if ( mlt_deque_count( queue ) )
1252 frame = mlt_deque_pop_front( queue );
1253 frame_properties = MLT_FRAME_PROPERTIES( frame );
1255 c = video_st->codec;
1257 if ( mlt_properties_get_int( frame_properties, "rendered" ) )
1264 mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
1266 mlt_frame_get_image( frame, &image, &img_fmt, &img_width, &img_height, 0 );
1270 // Convert the mlt frame to an AVPicture
1271 for ( i = 0; i < height; i ++ )
1273 p = input->data[ 0 ] + i * input->linesize[ 0 ];
1282 // Do the colour space conversion
1284 struct SwsContext *context = sws_getContext( width, height, PIX_FMT_YUYV422,
1285 width, height, video_st->codec->pix_fmt, SWS_FAST_BILINEAR, NULL, NULL, NULL);
1286 sws_scale( context, input->data, input->linesize, 0, height,
1287 output->data, output->linesize);
1288 sws_freeContext( context );
1290 img_convert( ( AVPicture * )output, video_st->codec->pix_fmt, ( AVPicture * )input, PIX_FMT_YUYV422, width, height );
1293 // Apply the alpha if applicable
1294 if ( video_st->codec->pix_fmt == PIX_FMT_RGB32 )
1296 uint8_t *alpha = mlt_frame_get_alpha_mask( frame );
1299 for ( i = 0; i < height; i ++ )
1301 n = ( width + 7 ) / 8;
1302 p = output->data[ 0 ] + i * output->linesize[ 0 ] + 3;
1306 case 0: do { *p = *alpha++; p += 4;
1307 case 7: *p = *alpha++; p += 4;
1308 case 6: *p = *alpha++; p += 4;
1309 case 5: *p = *alpha++; p += 4;
1310 case 4: *p = *alpha++; p += 4;
1311 case 3: *p = *alpha++; p += 4;
1312 case 2: *p = *alpha++; p += 4;
1313 case 1: *p = *alpha++; p += 4;
1321 if (oc->oformat->flags & AVFMT_RAWPICTURE)
1323 // raw video case. The API will change slightly in the near future for that
1325 av_init_packet(&pkt);
1327 pkt.flags |= PKT_FLAG_KEY;
1328 pkt.stream_index= video_st->index;
1329 pkt.data= (uint8_t *)output;
1330 pkt.size= sizeof(AVPicture);
1332 ret = av_write_frame(oc, &pkt);
1333 video_pts += c->frame_size;
1338 output->quality = video_st->quality;
1340 // Set frame interlace hints
1341 output->interlaced_frame = !mlt_properties_get_int( frame_properties, "progressive" );
1342 output->top_field_first = mlt_properties_get_int( frame_properties, "top_field_first" );
1343 output->pts = frame_count;
1346 out_size = avcodec_encode_video(c, video_outbuf, video_outbuf_size, output );
1348 // If zero size, it means the image was buffered
1352 av_init_packet( &pkt );
1354 if ( c->coded_frame && c->coded_frame->pts != AV_NOPTS_VALUE )
1355 pkt.pts= av_rescale_q( c->coded_frame->pts, c->time_base, video_st->time_base );
1356 mlt_log_debug( MLT_CONSUMER_SERVICE( this ), "video pkt pts %lld frame pts %lld", pkt.pts, c->coded_frame->pts );
1357 if( c->coded_frame && c->coded_frame->key_frame )
1358 pkt.flags |= PKT_FLAG_KEY;
1359 pkt.stream_index= video_st->index;
1360 pkt.data= video_outbuf;
1363 // write the compressed frame in the media file
1364 ret = av_interleaved_write_frame(oc, &pkt);
1365 mlt_log_debug( MLT_CONSUMER_SERVICE( this ), " frame_size %d\n", c->frame_size );
1366 video_pts = (double)video_st->pts.val * av_q2d( video_st->time_base );
1368 // Dual pass logging
1369 if ( mlt_properties_get_data( properties, "_logfile", NULL ) && c->stats_out )
1370 fprintf( mlt_properties_get_data( properties, "_logfile", NULL ), "%s", c->stats_out );
1372 else if ( out_size < 0 )
1374 mlt_log_warning( MLT_CONSUMER_SERVICE( this ), "error with video encode %d\n", frame_count );
1378 mlt_frame_close( frame );
1386 mlt_log_debug( MLT_CONSUMER_SERVICE( this ), "audio pts %lld (%f) ", audio_st[0]->pts.val, audio_pts );
1388 mlt_log_debug( MLT_CONSUMER_SERVICE( this ), "video pts %lld (%f) ", video_st->pts.val, video_pts );
1389 mlt_log_debug( MLT_CONSUMER_SERVICE( this ), "\n" );
1392 if ( real_time_output == 1 && frames % 2 == 0 )
1394 long passed = time_difference( &ante );
1397 long pending = ( ( ( long )sample_fifo_used( fifo ) * 1000 ) / frequency ) * 1000;
1400 if ( passed < total_time )
1402 long total = ( total_time - passed );
1403 struct timespec t = { total / 1000000, ( total % 1000000 ) * 1000 };
1404 nanosleep( &t, NULL );
1410 if ( ! real_time_output )
1413 if ( audio_st && audio_st->codec->frame_size > 1 ) for (;;)
1415 AVCodecContext *c = audio_st->codec;
1417 av_init_packet( &pkt );
1420 if ( /*( c->capabilities & CODEC_CAP_SMALL_LAST_FRAME ) &&*/
1421 ( channels * audio_input_frame_size < sample_fifo_used( fifo ) ) )
1423 sample_fifo_fetch( fifo, buffer, channels * audio_input_frame_size );
1424 pkt.size = avcodec_encode_audio( c, audio_outbuf, audio_outbuf_size, buffer );
1426 if ( pkt.size <= 0 )
1427 pkt.size = avcodec_encode_audio( c, audio_outbuf, audio_outbuf_size, NULL );
1428 if ( pkt.size <= 0 )
1431 // Write the compressed frame in the media file
1432 if ( c->coded_frame && c->coded_frame->pts != AV_NOPTS_VALUE )
1433 pkt.pts = av_rescale_q( c->coded_frame->pts, c->time_base, audio_st->time_base );
1434 pkt.flags |= PKT_FLAG_KEY;
1435 pkt.stream_index = audio_st->index;
1436 pkt.data = audio_outbuf;
1437 if ( av_interleaved_write_frame( oc, &pkt ) != 0 )
1439 fprintf( stderr, "%s: Error while writing flushed audio frame\n", __FILE__ );
1445 if ( video_st && !( oc->oformat->flags & AVFMT_RAWPICTURE ) ) for (;;)
1447 AVCodecContext *c = video_st->codec;
1449 av_init_packet( &pkt );
1452 pkt.size = avcodec_encode_video( c, video_outbuf, video_outbuf_size, NULL );
1453 if ( pkt.size <= 0 )
1456 if ( c->coded_frame && c->coded_frame->pts != AV_NOPTS_VALUE )
1457 pkt.pts= av_rescale_q( c->coded_frame->pts, c->time_base, video_st->time_base );
1458 if( c->coded_frame && c->coded_frame->key_frame )
1459 pkt.flags |= PKT_FLAG_KEY;
1460 pkt.stream_index = video_st->index;
1461 pkt.data = video_outbuf;
1463 // write the compressed frame in the media file
1464 if ( av_interleaved_write_frame( oc, &pkt ) != 0 )
1466 mlt_log_error( MLT_CONSUMER_SERVICE(this), "error while writing flushing video frame\n" );
1473 // Write the trailer, if any
1474 av_write_trailer( oc );
1476 // XXX ugly hack to prevent x264 from crashing on multi-threaded encoding
1477 int pass = mlt_properties_get_int( properties, "pass" );
1478 int thread_count = mlt_properties_get_int( properties, "threads" );
1479 if ( thread_count == 0 && getenv( "MLT_AVFORMAT_THREADS" ) )
1480 thread_count = atoi( getenv( "MLT_AVFORMAT_THREADS" ) );
1481 int multithreaded_x264 = ( video_codec_id == CODEC_ID_H264 && thread_count > 1 );
1484 if ( video_st && !multithreaded_x264 )
1485 close_video(oc, video_st);
1486 for ( i = 0; i < MAX_AUDIO_STREAMS && audio_st[i]; i++ )
1487 close_audio( oc, audio_st[i] );
1490 for ( i = 0; i < oc->nb_streams; i++ )
1491 av_freep( &oc->streams[i] );
1493 // Close the output file
1494 if ( !( fmt->flags & AVFMT_NOFILE ) )
1495 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(0<<8)+0)
1496 url_fclose( oc->pb );
1498 url_fclose( &oc->pb );
1501 // Clean up input and output frames
1503 av_free( output->data[0] );
1505 av_free( input->data[0] );
1507 av_free( video_outbuf );
1508 av_free( audio_buf_1 );
1509 av_free( audio_buf_2 );
1514 // Just in case we terminated on pause
1515 mlt_properties_set_int( properties, "running", 0 );
1517 mlt_consumer_stopped( this );
1518 mlt_properties_close( frame_meta_properties );
1522 // Remove the dual pass log file
1523 if ( mlt_properties_get( properties, "_logfilename" ) )
1524 remove( mlt_properties_get( properties, "_logfilename" ) );
1526 // Remove the x264 dual pass logs
1527 char *cwd = getcwd( NULL, 0 );
1528 const char *file = "x264_2pass.log";
1529 char *full = malloc( strlen( cwd ) + strlen( file ) + 2 );
1530 sprintf( full, "%s/%s", cwd, file );
1533 file = "x264_2pass.log.temp";
1534 full = malloc( strlen( cwd ) + strlen( file ) + 2 );
1535 sprintf( full, "%s/%s", cwd, file );
1538 file = "x264_2pass.log.mbtree";
1539 full = malloc( strlen( cwd ) + strlen( file ) + 2 );
1540 sprintf( full, "%s/%s", cwd, file );
1544 remove( "x264_2pass.log.temp" );
1550 /** Close the consumer.
1553 static void consumer_close( mlt_consumer this )
1555 // Stop the consumer
1556 mlt_consumer_stop( this );
1559 mlt_consumer_close( this );