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
43 #if LIBAVUTIL_VERSION_INT >= ((50<<16)+(8<<8)+0)
44 #include <libavutil/pixdesc.h>
47 #if LIBAVUTIL_VERSION_INT < (50<<16)
48 #define PIX_FMT_RGB32 PIX_FMT_RGBA32
49 #define PIX_FMT_YUYV422 PIX_FMT_YUV422
52 #define MAX_AUDIO_STREAMS (8)
53 #define AUDIO_ENCODE_BUFFER_SIZE (48000 * 2 * MAX_AUDIO_STREAMS)
56 // This structure should be extended and made globally available in mlt
68 *sample_fifo, sample_fifo_s;
70 sample_fifo sample_fifo_init( int frequency, int channels )
72 sample_fifo this = calloc( 1, sizeof( sample_fifo_s ) );
73 this->frequency = frequency;
74 this->channels = channels;
78 // sample_fifo_clear and check are temporarily aborted (not working as intended)
80 void sample_fifo_clear( sample_fifo this, double time )
82 int words = ( float )( time - this->time ) * this->frequency * this->channels;
83 if ( ( int )( ( float )time * 100 ) < ( int )( ( float )this->time * 100 ) && this->used > words && words > 0 )
85 memmove( this->buffer, &this->buffer[ words ], ( this->used - words ) * sizeof( int16_t ) );
89 else if ( ( int )( ( float )time * 100 ) != ( int )( ( float )this->time * 100 ) )
96 void sample_fifo_check( sample_fifo this, double time )
98 if ( this->used == 0 )
100 if ( ( int )( ( float )time * 100 ) < ( int )( ( float )this->time * 100 ) )
105 void sample_fifo_append( sample_fifo this, int16_t *samples, int count )
107 if ( ( this->size - this->used ) < count )
109 this->size += count * 5;
110 this->buffer = realloc( this->buffer, this->size * sizeof( int16_t ) );
113 memcpy( &this->buffer[ this->used ], samples, count * sizeof( int16_t ) );
117 int sample_fifo_used( sample_fifo this )
122 int sample_fifo_fetch( sample_fifo this, int16_t *samples, int count )
124 if ( count > this->used )
127 memcpy( samples, this->buffer, count * sizeof( int16_t ) );
129 memmove( this->buffer, &this->buffer[ count ], this->used * sizeof( int16_t ) );
131 this->time += ( double )count / this->channels / this->frequency;
136 void sample_fifo_close( sample_fifo this )
138 free( this->buffer );
142 // Forward references.
143 static int consumer_start( mlt_consumer this );
144 static int consumer_stop( mlt_consumer this );
145 static int consumer_is_stopped( mlt_consumer this );
146 static void *consumer_thread( void *arg );
147 static void consumer_close( mlt_consumer this );
149 /** Initialise the consumer.
152 mlt_consumer consumer_avformat_init( mlt_profile profile, char *arg )
154 // Allocate the consumer
155 mlt_consumer this = mlt_consumer_new( profile );
157 // If memory allocated and initialises without error
160 // Get properties from the consumer
161 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
163 // Assign close callback
164 this->close = consumer_close;
166 // Interpret the argument
168 mlt_properties_set( properties, "target", arg );
170 // sample and frame queue
171 mlt_properties_set_data( properties, "frame_queue", mlt_deque_init( ), 0, ( mlt_destructor )mlt_deque_close, NULL );
173 // Audio options not fully handled by AVOptions
174 #define QSCALE_NONE (-99999)
175 mlt_properties_set_int( properties, "aq", QSCALE_NONE );
177 // Video options not fully handled by AVOptions
178 mlt_properties_set_int( properties, "dc", 8 );
180 // Muxer options not fully handled by AVOptions
181 mlt_properties_set_double( properties, "muxdelay", 0.7 );
182 mlt_properties_set_double( properties, "muxpreload", 0.5 );
184 // Ensure termination at end of the stream
185 mlt_properties_set_int( properties, "terminate_on_pause", 1 );
187 // Default to separate processing threads for producer and consumer with no frame dropping!
188 mlt_properties_set_int( properties, "real_time", -1 );
189 mlt_properties_set_int( properties, "prefill", 1 );
191 // Set up start/stop/terminated callbacks
192 this->start = consumer_start;
193 this->stop = consumer_stop;
194 this->is_stopped = consumer_is_stopped;
201 /** Start the consumer.
204 static int consumer_start( mlt_consumer this )
206 // Get the properties
207 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
210 // Report information about available muxers and codecs as YAML Tiny
211 char *s = mlt_properties_get( properties, "f" );
212 if ( s && strcmp( s, "list" ) == 0 )
214 fprintf( stderr, "---\nformats:\n" );
215 AVOutputFormat *format = NULL;
216 while ( ( format = av_oformat_next( format ) ) )
217 fprintf( stderr, " - %s\n", format->name );
218 fprintf( stderr, "...\n" );
221 s = mlt_properties_get( properties, "acodec" );
222 if ( s && strcmp( s, "list" ) == 0 )
224 fprintf( stderr, "---\naudio_codecs:\n" );
225 AVCodec *codec = NULL;
226 while ( ( codec = av_codec_next( codec ) ) )
227 if ( codec->encode && codec->type == CODEC_TYPE_AUDIO )
228 fprintf( stderr, " - %s\n", codec->name );
229 fprintf( stderr, "...\n" );
232 s = mlt_properties_get( properties, "vcodec" );
233 if ( s && strcmp( s, "list" ) == 0 )
235 fprintf( stderr, "---\nvideo_codecs:\n" );
236 AVCodec *codec = NULL;
237 while ( ( codec = av_codec_next( codec ) ) )
238 if ( codec->encode && codec->type == CODEC_TYPE_VIDEO )
239 fprintf( stderr, " - %s\n", codec->name );
240 fprintf( stderr, "...\n" );
244 // Check that we're not already running
245 if ( !error && !mlt_properties_get_int( properties, "running" ) )
248 pthread_t *thread = calloc( 1, sizeof( pthread_t ) );
250 // Get the width and height
251 int width = mlt_properties_get_int( properties, "width" );
252 int height = mlt_properties_get_int( properties, "height" );
254 // Obtain the size property
255 char *size = mlt_properties_get( properties, "s" );
261 if ( sscanf( size, "%dx%d", &tw, &th ) == 2 && tw > 0 && th > 0 )
268 mlt_log_warning( MLT_CONSUMER_SERVICE( this ), "Invalid size property %s - ignoring.\n", size );
272 // Now ensure we honour the multiple of two requested by libavformat
273 width = ( width / 2 ) * 2;
274 height = ( height / 2 ) * 2;
275 mlt_properties_set_int( properties, "width", width );
276 mlt_properties_set_int( properties, "height", height );
278 // We need to set these on the profile as well because the s property is
279 // an alias to mlt properties that correspond to profile settings.
280 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
283 profile->width = width;
284 profile->height = height;
287 // Handle the ffmpeg command line "-r" property for frame rate
288 if ( mlt_properties_get( properties, "r" ) )
290 double frame_rate = mlt_properties_get_double( properties, "r" );
291 AVRational rational = av_d2q( frame_rate, 255 );
292 mlt_properties_set_int( properties, "frame_rate_num", rational.num );
293 mlt_properties_set_int( properties, "frame_rate_den", rational.den );
296 profile->frame_rate_num = rational.num;
297 profile->frame_rate_den = rational.den;
298 mlt_properties_set_double( properties, "fps", mlt_profile_fps( profile ) );
302 // Apply AVOptions that are synonyms for standard mlt_consumer options
303 if ( mlt_properties_get( properties, "ac" ) )
304 mlt_properties_set_int( properties, "channels", mlt_properties_get_int( properties, "ac" ) );
305 if ( mlt_properties_get( properties, "ar" ) )
306 mlt_properties_set_int( properties, "frequency", mlt_properties_get_int( properties, "ar" ) );
308 // Assign the thread to properties
309 mlt_properties_set_data( properties, "thread", thread, sizeof( pthread_t ), free, NULL );
311 // Set the running state
312 mlt_properties_set_int( properties, "running", 1 );
315 pthread_create( thread, NULL, consumer_thread, this );
320 /** Stop the consumer.
323 static int consumer_stop( mlt_consumer this )
325 // Get the properties
326 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
328 // Check that we're running
329 if ( mlt_properties_get_int( properties, "running" ) )
332 pthread_t *thread = mlt_properties_get_data( properties, "thread", NULL );
335 mlt_properties_set_int( properties, "running", 0 );
337 // Wait for termination
338 pthread_join( *thread, NULL );
344 /** Determine if the consumer is stopped.
347 static int consumer_is_stopped( mlt_consumer this )
349 // Get the properties
350 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
351 return !mlt_properties_get_int( properties, "running" );
354 /** Process properties as AVOptions and apply to AV context obj
357 static void apply_properties( void *obj, mlt_properties properties, int flags, int alloc )
360 int count = mlt_properties_count( properties );
361 for ( i = 0; i < count; i++ )
363 const char *opt_name = mlt_properties_get_name( properties, i );
364 const AVOption *opt = av_find_opt( obj, opt_name, NULL, flags, flags );
366 #if LIBAVCODEC_VERSION_INT >= ((52<<16)+(7<<8)+0)
367 av_set_string3( obj, opt_name, mlt_properties_get( properties, opt_name), alloc, NULL );
368 #elif LIBAVCODEC_VERSION_INT >= ((51<<16)+(59<<8)+0)
369 av_set_string2( obj, opt_name, mlt_properties_get( properties, opt_name), alloc );
371 av_set_string( obj, opt_name, mlt_properties_get( properties, opt_name) );
376 /** Add an audio output stream
379 static AVStream *add_audio_stream( mlt_consumer this, AVFormatContext *oc, int codec_id, int channels )
381 // Get the properties
382 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
384 // Create a new stream
385 AVStream *st = av_new_stream( oc, oc->nb_streams );
387 // If created, then initialise from properties
390 AVCodecContext *c = st->codec;
392 // Establish defaults from AVOptions
393 avcodec_get_context_defaults2( c, CODEC_TYPE_AUDIO );
395 c->codec_id = codec_id;
396 c->codec_type = CODEC_TYPE_AUDIO;
397 c->sample_fmt = SAMPLE_FMT_S16;
399 #if 0 // disabled until some audio codecs are multi-threaded
400 // Setup multi-threading
401 int thread_count = mlt_properties_get_int( properties, "threads" );
402 if ( thread_count == 0 && getenv( "MLT_AVFORMAT_THREADS" ) )
403 thread_count = atoi( getenv( "MLT_AVFORMAT_THREADS" ) );
404 if ( thread_count > 1 )
405 avcodec_thread_init( c, thread_count );
408 if (oc->oformat->flags & AVFMT_GLOBALHEADER)
409 c->flags |= CODEC_FLAG_GLOBAL_HEADER;
411 // Allow the user to override the audio fourcc
412 if ( mlt_properties_get( properties, "atag" ) )
415 char *arg = mlt_properties_get( properties, "atag" );
416 int tag = strtol( arg, &tail, 0);
418 tag = arg[ 0 ] + ( arg[ 1 ] << 8 ) + ( arg[ 2 ] << 16 ) + ( arg[ 3 ] << 24 );
422 // Process properties as AVOptions
423 char *apre = mlt_properties_get( properties, "apre" );
426 mlt_properties p = mlt_properties_load( apre );
427 apply_properties( c, p, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM, 1 );
428 mlt_properties_close( p );
430 apply_properties( c, properties, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM, 0 );
432 int audio_qscale = mlt_properties_get_int( properties, "aq" );
433 if ( audio_qscale > QSCALE_NONE )
435 c->flags |= CODEC_FLAG_QSCALE;
436 c->global_quality = st->quality = FF_QP2LAMBDA * audio_qscale;
439 // Set parameters controlled by MLT
440 c->sample_rate = mlt_properties_get_int( properties, "frequency" );
441 c->time_base = ( AVRational ){ 1, c->sample_rate };
442 c->channels = channels;
444 if ( mlt_properties_get( properties, "alang" ) != NULL )
445 strncpy( st->language, mlt_properties_get( properties, "alang" ), sizeof( st->language ) );
449 mlt_log_error( MLT_CONSUMER_SERVICE( this ), "Could not allocate a stream for audio\n" );
455 static int open_audio( AVFormatContext *oc, AVStream *st, int audio_outbuf_size )
457 // We will return the audio input size from here
458 int audio_input_frame_size = 0;
461 AVCodecContext *c = st->codec;
464 AVCodec *codec = avcodec_find_encoder( c->codec_id );
466 // Continue if codec found and we can open it
467 if ( codec != NULL && avcodec_open( c, codec ) >= 0 )
469 // ugly hack for PCM codecs (will be removed ASAP with new PCM
470 // support to compute the input frame size in samples
471 if ( c->frame_size <= 1 )
473 audio_input_frame_size = audio_outbuf_size / c->channels;
474 switch(st->codec->codec_id)
476 case CODEC_ID_PCM_S16LE:
477 case CODEC_ID_PCM_S16BE:
478 case CODEC_ID_PCM_U16LE:
479 case CODEC_ID_PCM_U16BE:
480 audio_input_frame_size >>= 1;
488 audio_input_frame_size = c->frame_size;
491 // Some formats want stream headers to be seperate (hmm)
492 if ( !strcmp( oc->oformat->name, "mp4" ) ||
493 !strcmp( oc->oformat->name, "mov" ) ||
494 !strcmp( oc->oformat->name, "3gp" ) )
495 c->flags |= CODEC_FLAG_GLOBAL_HEADER;
499 mlt_log_warning( NULL, "%s: Unable to encode audio - disabling audio output.\n", __FILE__ );
502 return audio_input_frame_size;
505 static void close_audio( AVFormatContext *oc, AVStream *st )
507 if ( st && st->codec )
508 avcodec_close( st->codec );
511 /** Add a video output stream
514 static AVStream *add_video_stream( mlt_consumer this, AVFormatContext *oc, int codec_id )
516 // Get the properties
517 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
519 // Create a new stream
520 AVStream *st = av_new_stream( oc, oc->nb_streams );
524 char *pix_fmt = mlt_properties_get( properties, "pix_fmt" );
525 AVCodecContext *c = st->codec;
527 // Establish defaults from AVOptions
528 avcodec_get_context_defaults2( c, CODEC_TYPE_VIDEO );
530 c->codec_id = codec_id;
531 c->codec_type = CODEC_TYPE_VIDEO;
533 // Setup multi-threading
534 int thread_count = mlt_properties_get_int( properties, "threads" );
535 if ( thread_count == 0 && getenv( "MLT_AVFORMAT_THREADS" ) )
536 thread_count = atoi( getenv( "MLT_AVFORMAT_THREADS" ) );
537 if ( thread_count > 1 )
538 avcodec_thread_init( c, thread_count );
540 // Process properties as AVOptions
541 char *vpre = mlt_properties_get( properties, "vpre" );
544 mlt_properties p = mlt_properties_load( vpre );
545 apply_properties( c, p, AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM, 1 );
546 mlt_properties_close( p );
548 apply_properties( c, properties, AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM, 0 );
550 // Set options controlled by MLT
551 c->width = mlt_properties_get_int( properties, "width" );
552 c->height = mlt_properties_get_int( properties, "height" );
553 c->time_base.num = mlt_properties_get_int( properties, "frame_rate_den" );
554 c->time_base.den = mlt_properties_get_int( properties, "frame_rate_num" );
555 if ( st->time_base.den == 0 )
556 st->time_base = c->time_base;
557 #if LIBAVUTIL_VERSION_INT >= ((50<<16)+(8<<8)+0)
558 c->pix_fmt = pix_fmt ? av_get_pix_fmt( pix_fmt ) : PIX_FMT_YUV420P;
560 c->pix_fmt = pix_fmt ? avcodec_get_pix_fmt( pix_fmt ) : PIX_FMT_YUV420P;
563 if ( mlt_properties_get( properties, "aspect" ) )
565 // "-aspect" on ffmpeg command line is display aspect ratio
566 double ar = mlt_properties_get_double( properties, "aspect" );
567 AVRational rational = av_d2q( ar, 255 );
569 // Update the profile and properties as well since this is an alias
570 // for mlt properties that correspond to profile settings
571 mlt_properties_set_int( properties, "display_aspect_num", rational.num );
572 mlt_properties_set_int( properties, "display_aspect_den", rational.den );
573 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
576 profile->display_aspect_num = rational.num;
577 profile->display_aspect_den = rational.den;
578 mlt_properties_set_double( properties, "display_ratio", mlt_profile_dar( profile ) );
581 // Now compute the sample aspect ratio
582 rational = av_d2q( ar * c->height / c->width, 255 );
583 c->sample_aspect_ratio = rational;
584 // Update the profile and properties as well since this is an alias
585 // for mlt properties that correspond to profile settings
586 mlt_properties_set_int( properties, "sample_aspect_num", rational.num );
587 mlt_properties_set_int( properties, "sample_aspect_den", rational.den );
590 profile->sample_aspect_num = rational.num;
591 profile->sample_aspect_den = rational.den;
592 mlt_properties_set_double( properties, "aspect_ratio", mlt_profile_sar( profile ) );
597 c->sample_aspect_ratio.num = mlt_properties_get_int( properties, "sample_aspect_num" );
598 c->sample_aspect_ratio.den = mlt_properties_get_int( properties, "sample_aspect_den" );
600 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(21<<8)+0)
601 st->sample_aspect_ratio = c->sample_aspect_ratio;
604 if ( mlt_properties_get_double( properties, "qscale" ) > 0 )
606 c->flags |= CODEC_FLAG_QSCALE;
607 st->quality = FF_QP2LAMBDA * mlt_properties_get_double( properties, "qscale" );
610 // Allow the user to override the video fourcc
611 if ( mlt_properties_get( properties, "vtag" ) )
614 const char *arg = mlt_properties_get( properties, "vtag" );
615 int tag = strtol( arg, &tail, 0);
617 tag = arg[ 0 ] + ( arg[ 1 ] << 8 ) + ( arg[ 2 ] << 16 ) + ( arg[ 3 ] << 24 );
621 // Some formats want stream headers to be seperate
622 if ( oc->oformat->flags & AVFMT_GLOBALHEADER )
623 c->flags |= CODEC_FLAG_GLOBAL_HEADER;
625 // Translate these standard mlt consumer properties to ffmpeg
626 if ( mlt_properties_get_int( properties, "progressive" ) == 0 &&
627 mlt_properties_get_int( properties, "deinterlace" ) == 0 )
629 if ( ! mlt_properties_get( properties, "ildct" ) || mlt_properties_get_int( properties, "ildct" ) )
630 c->flags |= CODEC_FLAG_INTERLACED_DCT;
631 if ( ! mlt_properties_get( properties, "ilme" ) || mlt_properties_get_int( properties, "ilme" ) )
632 c->flags |= CODEC_FLAG_INTERLACED_ME;
635 // parse the ratecontrol override string
637 char *rc_override = mlt_properties_get( properties, "rc_override" );
638 for ( i = 0; rc_override; i++ )
641 int e = sscanf( rc_override, "%d,%d,%d", &start, &end, &q );
643 mlt_log_warning( MLT_CONSUMER_SERVICE( this ), "Error parsing rc_override\n" );
644 c->rc_override = av_realloc( c->rc_override, sizeof( RcOverride ) * ( i + 1 ) );
645 c->rc_override[i].start_frame = start;
646 c->rc_override[i].end_frame = end;
649 c->rc_override[i].qscale = q;
650 c->rc_override[i].quality_factor = 1.0;
654 c->rc_override[i].qscale = 0;
655 c->rc_override[i].quality_factor = -q / 100.0;
657 rc_override = strchr( rc_override, '/' );
661 c->rc_override_count = i;
662 if ( !c->rc_initial_buffer_occupancy )
663 c->rc_initial_buffer_occupancy = c->rc_buffer_size * 3/4;
664 c->intra_dc_precision = mlt_properties_get_int( properties, "dc" ) - 8;
667 i = mlt_properties_get_int( properties, "pass" );
669 c->flags |= CODEC_FLAG_PASS1;
671 c->flags |= CODEC_FLAG_PASS2;
672 if ( codec_id != CODEC_ID_H264 && ( c->flags & ( CODEC_FLAG_PASS1 | CODEC_FLAG_PASS2 ) ) )
674 char logfilename[1024];
679 snprintf( logfilename, sizeof(logfilename), "%s_2pass.log",
680 mlt_properties_get( properties, "passlogfile" ) ? mlt_properties_get( properties, "passlogfile" ) : mlt_properties_get( properties, "target" ) );
681 if ( c->flags & CODEC_FLAG_PASS1 )
683 f = fopen( logfilename, "w" );
685 perror( logfilename );
687 mlt_properties_set_data( properties, "_logfile", f, 0, ( mlt_destructor )fclose, NULL );
691 /* read the log file */
692 f = fopen( logfilename, "r" );
699 mlt_properties_set( properties, "_logfilename", logfilename );
700 fseek( f, 0, SEEK_END );
702 fseek( f, 0, SEEK_SET );
703 logbuffer = av_malloc( size + 1 );
705 mlt_log_fatal( MLT_CONSUMER_SERVICE( this ), "Could not allocate log buffer\n" );
708 size = fread( logbuffer, 1, size, f );
710 logbuffer[size] = '\0';
711 c->stats_in = logbuffer;
712 mlt_properties_set_data( properties, "_logbuffer", logbuffer, 0, ( mlt_destructor )av_free, NULL );
720 mlt_log_error( MLT_CONSUMER_SERVICE( this ), "Could not allocate a stream for video\n" );
726 static AVFrame *alloc_picture( int pix_fmt, int width, int height )
729 AVFrame *picture = avcodec_alloc_frame();
731 // Determine size of the
732 int size = avpicture_get_size(pix_fmt, width, height);
734 // Allocate the picture buf
735 uint8_t *picture_buf = av_malloc(size);
737 // If we have both, then fill the image
738 if ( picture != NULL && picture_buf != NULL )
740 // Fill the frame with the allocated buffer
741 avpicture_fill( (AVPicture *)picture, picture_buf, pix_fmt, width, height);
745 // Something failed - clean up what we can
747 av_free( picture_buf );
754 static int open_video(AVFormatContext *oc, AVStream *st)
757 AVCodecContext *video_enc = st->codec;
759 // find the video encoder
760 AVCodec *codec = avcodec_find_encoder( video_enc->codec_id );
762 if( codec && codec->pix_fmts )
764 const enum PixelFormat *p = codec->pix_fmts;
767 if( *p == video_enc->pix_fmt )
771 video_enc->pix_fmt = codec->pix_fmts[ 0 ];
774 // Open the codec safely
775 return codec != NULL && avcodec_open( video_enc, codec ) >= 0;
778 void close_video(AVFormatContext *oc, AVStream *st)
780 if ( st && st->codec )
781 avcodec_close(st->codec);
784 static inline long time_difference( struct timeval *time1 )
786 struct timeval time2;
787 gettimeofday( &time2, NULL );
788 return time2.tv_sec * 1000000 + time2.tv_usec - time1->tv_sec * 1000000 - time1->tv_usec;
791 /** The main thread - the argument is simply the consumer.
794 static void *consumer_thread( void *arg )
796 // Map the argument to the object
797 mlt_consumer this = arg;
799 // Get the properties
800 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
802 // Get the terminate on pause property
803 int terminate_on_pause = mlt_properties_get_int( properties, "terminate_on_pause" );
806 // Determine if feed is slow (for realtime stuff)
807 int real_time_output = mlt_properties_get_int( properties, "real_time" );
812 // Get the frame rate
813 double fps = mlt_properties_get_double( properties, "fps" );
815 // Get width and height
816 int width = mlt_properties_get_int( properties, "width" );
817 int height = mlt_properties_get_int( properties, "height" );
818 int img_width = width;
819 int img_height = height;
821 // Get default audio properties
822 mlt_audio_format aud_fmt = mlt_audio_s16;
823 int channels = mlt_properties_get_int( properties, "channels" );
824 int total_channels = channels;
825 int frequency = mlt_properties_get_int( properties, "frequency" );
829 // AVFormat audio buffer and frame size
830 int audio_outbuf_size = ( 1024 * 42 );
831 uint8_t *audio_outbuf = av_malloc( audio_outbuf_size );
832 int audio_input_frame_size = 0;
834 // AVFormat video buffer and frame count
836 int video_outbuf_size = ( 1024 * 1024 );
837 uint8_t *video_outbuf = av_malloc( video_outbuf_size );
839 // Used for the frame properties
840 mlt_frame frame = NULL;
841 mlt_properties frame_properties = NULL;
844 mlt_deque queue = mlt_properties_get_data( properties, "frame_queue", NULL );
845 sample_fifo fifo = mlt_properties_get_data( properties, "sample_fifo", NULL );
847 // Need two av pictures for converting
848 AVFrame *output = NULL;
849 AVFrame *input = alloc_picture( PIX_FMT_YUYV422, width, height );
851 // For receiving images from an mlt_frame
853 mlt_image_format img_fmt = mlt_image_yuv422;
855 // For receiving audio samples back from the fifo
856 int16_t *audio_buf_1 = av_malloc( AUDIO_ENCODE_BUFFER_SIZE );
857 int16_t *audio_buf_2 = NULL;
860 // Allocate the context
861 #if (LIBAVFORMAT_VERSION_INT >= ((52<<16)+(26<<8)+0))
862 AVFormatContext *oc = avformat_alloc_context( );
864 AVFormatContext *oc = av_alloc_format_context( );
868 AVStream *video_st = NULL;
869 AVStream *audio_st[ MAX_AUDIO_STREAMS ];
872 double audio_pts = 0;
873 double video_pts = 0;
877 long int total_time = 0;
879 // Determine the format
880 AVOutputFormat *fmt = NULL;
881 const char *filename = mlt_properties_get( properties, "target" );
882 char *format = mlt_properties_get( properties, "f" );
883 char *vcodec = mlt_properties_get( properties, "vcodec" );
884 char *acodec = mlt_properties_get( properties, "acodec" );
886 // Used to store and override codec ids
892 mlt_properties frame_meta_properties = mlt_properties_new();
894 // Initialize audio_st
895 int i = MAX_AUDIO_STREAMS;
899 // Check for user selected format first
900 if ( format != NULL )
901 #if LIBAVFORMAT_VERSION_INT < ((52<<16)+(45<<8)+0)
902 fmt = guess_format( format, NULL, NULL );
904 fmt = av_guess_format( format, NULL, NULL );
907 // Otherwise check on the filename
908 if ( fmt == NULL && filename != NULL )
909 #if LIBAVFORMAT_VERSION_INT < ((52<<16)+(45<<8)+0)
910 fmt = guess_format( NULL, filename, NULL );
912 fmt = av_guess_format( NULL, filename, NULL );
915 // Otherwise default to mpeg
917 #if LIBAVFORMAT_VERSION_INT < ((52<<16)+(45<<8)+0)
918 fmt = guess_format( "mpeg", NULL, NULL );
920 fmt = av_guess_format( "mpeg", NULL, NULL );
923 // We need a filename - default to stdout?
924 if ( filename == NULL || !strcmp( filename, "" ) )
927 // Get the codec ids selected
928 audio_codec_id = fmt->audio_codec;
929 video_codec_id = fmt->video_codec;
931 // Check for audio codec overides
932 if ( ( acodec && strcmp( acodec, "none" ) == 0 ) || mlt_properties_get_int( properties, "an" ) )
933 audio_codec_id = CODEC_ID_NONE;
936 AVCodec *p = avcodec_find_encoder_by_name( acodec );
938 audio_codec_id = p->id;
940 mlt_log_warning( MLT_CONSUMER_SERVICE( this ), "audio codec %s unrecognised - ignoring\n", acodec );
943 // Check for video codec overides
944 if ( ( vcodec && strcmp( vcodec, "none" ) == 0 ) || mlt_properties_get_int( properties, "vn" ) )
945 video_codec_id = CODEC_ID_NONE;
948 AVCodec *p = avcodec_find_encoder_by_name( vcodec );
950 video_codec_id = p->id;
952 mlt_log_warning( MLT_CONSUMER_SERVICE( this ), "video codec %s unrecognised - ignoring\n", vcodec );
959 tmp = mlt_properties_get( properties, "meta.attr.title.markup");
960 if (tmp != NULL) snprintf( oc->title, sizeof(oc->title), "%s", tmp );
962 tmp = mlt_properties_get( properties, "meta.attr.comment.markup");
963 if (tmp != NULL) snprintf( oc->comment, sizeof(oc->comment), "%s", tmp );
965 tmp = mlt_properties_get( properties, "meta.attr.author.markup");
966 if (tmp != NULL) snprintf( oc->author, sizeof(oc->author), "%s", tmp );
968 tmp = mlt_properties_get( properties, "meta.attr.copyright.markup");
969 if (tmp != NULL) snprintf( oc->copyright, sizeof(oc->copyright), "%s", tmp );
971 tmp = mlt_properties_get( properties, "meta.attr.album.markup");
972 if (tmp != NULL) snprintf( oc->album, sizeof(oc->album), "%s", tmp );
974 metavalue = mlt_properties_get_int( properties, "meta.attr.year.markup");
975 if (metavalue != 0) oc->year = metavalue;
977 metavalue = mlt_properties_get_int( properties, "meta.attr.track.markup");
978 if (metavalue != 0) oc->track = metavalue;
981 snprintf( oc->filename, sizeof(oc->filename), "%s", filename );
983 // Add audio and video streams
984 if ( video_codec_id != CODEC_ID_NONE )
985 video_st = add_video_stream( this, oc, video_codec_id );
986 if ( audio_codec_id != CODEC_ID_NONE )
992 for ( i = 0; i < MAX_AUDIO_STREAMS; i++ )
994 sprintf( key, "channels.%d", i );
995 int j = mlt_properties_get_int( properties, key );
1000 audio_st[i] = add_audio_stream( this, oc, audio_codec_id, j );
1006 audio_st[0] = add_audio_stream( this, oc, audio_codec_id, channels );
1007 total_channels = channels;
1011 // Set the parameters (even though we have none...)
1012 if ( av_set_parameters(oc, NULL) >= 0 )
1014 oc->preload = ( int )( mlt_properties_get_double( properties, "muxpreload" ) * AV_TIME_BASE );
1015 oc->max_delay= ( int )( mlt_properties_get_double( properties, "muxdelay" ) * AV_TIME_BASE );
1017 // Process properties as AVOptions
1018 char *fpre = mlt_properties_get( properties, "fpre" );
1021 mlt_properties p = mlt_properties_load( fpre );
1022 apply_properties( oc, p, AV_OPT_FLAG_ENCODING_PARAM, 1 );
1023 mlt_properties_close( p );
1025 apply_properties( oc, properties, AV_OPT_FLAG_ENCODING_PARAM, 0 );
1027 if ( video_st && !open_video( oc, video_st ) )
1029 for ( i = 0; i < MAX_AUDIO_STREAMS && audio_st[i]; i++ )
1031 audio_input_frame_size = open_audio( oc, audio_st[i], audio_outbuf_size );
1032 if ( !audio_input_frame_size )
1036 // Open the output file, if needed
1037 if ( !( fmt->flags & AVFMT_NOFILE ) )
1039 if ( url_fopen( &oc->pb, filename, URL_WRONLY ) < 0 )
1041 mlt_log_error( MLT_CONSUMER_SERVICE( this ), "Could not open '%s'\n", filename );
1042 mlt_properties_set_int( properties, "running", 0 );
1046 // Write the stream header.
1047 if ( mlt_properties_get_int( properties, "running" ) )
1048 av_write_header( oc );
1052 mlt_log_error( MLT_CONSUMER_SERVICE( this ), "Invalid output format parameters\n" );
1053 mlt_properties_set_int( properties, "running", 0 );
1058 output = alloc_picture( video_st->codec->pix_fmt, width, height );
1060 // Last check - need at least one stream
1061 if ( !audio_st[0] && !video_st )
1062 mlt_properties_set_int( properties, "running", 0 );
1064 // Get the starting time (can ignore the times above)
1065 gettimeofday( &ante, NULL );
1067 // Loop while running
1068 while( mlt_properties_get_int( properties, "running" ) &&
1069 ( !terminated || ( video_st && mlt_deque_count( queue ) ) ) )
1071 frame = mlt_consumer_rt_frame( this );
1073 // Check that we have a frame to work with
1074 if ( frame != NULL )
1076 // Increment frames dispatched
1079 // Default audio args
1080 frame_properties = MLT_FRAME_PROPERTIES( frame );
1082 // Check for the terminated condition
1083 terminated = terminate_on_pause && mlt_properties_get_double( frame_properties, "_speed" ) == 0.0;
1085 // Get audio and append to the fifo
1086 if ( !terminated && audio_st[0] )
1088 samples = mlt_sample_calculator( fps, frequency, count ++ );
1089 mlt_frame_get_audio( frame, (void**) &pcm, &aud_fmt, &frequency, &channels, &samples );
1091 // Save the audio channel remap properties for later
1092 mlt_properties_pass( frame_meta_properties, frame_properties, "meta.map.audio." );
1094 // Create the fifo if we don't have one
1097 fifo = sample_fifo_init( frequency, channels );
1098 mlt_properties_set_data( properties, "sample_fifo", fifo, 0, ( mlt_destructor )sample_fifo_close, NULL );
1101 // Silence if not normal forward speed
1102 if ( mlt_properties_get_double( frame_properties, "_speed" ) != 1.0 )
1103 memset( pcm, 0, samples * channels * 2 );
1105 // Append the samples
1106 sample_fifo_append( fifo, pcm, samples * channels );
1107 total_time += ( samples * 1000000 ) / frequency;
1111 if ( !terminated && video_st )
1112 mlt_deque_push_back( queue, frame );
1114 mlt_frame_close( frame );
1117 // While we have stuff to process, process...
1120 // Write interleaved audio and video frames
1121 if ( !video_st || ( video_st && audio_st[0] && audio_pts < video_pts ) )
1124 if ( ( video_st && terminated ) || ( channels * audio_input_frame_size ) < sample_fifo_used( fifo ) )
1126 int j = 0; // channel offset into interleaved source buffer
1127 int n = FFMIN( FFMIN( channels * audio_input_frame_size, sample_fifo_used( fifo ) ), AUDIO_ENCODE_BUFFER_SIZE );
1129 // Get the audio samples
1131 sample_fifo_fetch( fifo, audio_buf_1, n );
1133 memset( audio_buf_1, 0, AUDIO_ENCODE_BUFFER_SIZE );
1134 samples = n / channels;
1136 // For each output stream
1137 for ( i = 0; i < MAX_AUDIO_STREAMS && audio_st[i] && j < total_channels; i++ )
1139 AVStream *stream = audio_st[i];
1140 AVCodecContext *codec = stream->codec;
1143 av_init_packet( &pkt );
1145 // Optimized for single track and no channel remap
1146 if ( !audio_st[1] && !mlt_properties_count( frame_meta_properties ) )
1148 pkt.size = avcodec_encode_audio( codec, audio_outbuf, audio_outbuf_size, audio_buf_1 );
1152 // Extract the audio channels according to channel mapping
1153 int dest_offset = 0; // channel offset into interleaved dest buffer
1155 // Get the number of channels for this stream
1156 sprintf( key, "channels.%d", i );
1157 int current_channels = mlt_properties_get_int( properties, key );
1159 // Clear the destination audio buffer.
1161 audio_buf_2 = av_mallocz( AUDIO_ENCODE_BUFFER_SIZE );
1163 memset( audio_buf_2, 0, AUDIO_ENCODE_BUFFER_SIZE );
1165 // For each output channel
1166 while ( dest_offset < current_channels && j < total_channels )
1168 int map_start = -1, map_channels = 0;
1169 int source_offset = 0;
1172 // Look for a mapping that starts at j
1173 for ( k = 0; k < (MAX_AUDIO_STREAMS * 2) && map_start != j; k++ )
1175 sprintf( key, "%d.channels", k );
1176 map_channels = mlt_properties_get_int( frame_meta_properties, key );
1177 sprintf( key, "%d.start", k );
1178 if ( mlt_properties_get( frame_meta_properties, key ) )
1179 map_start = mlt_properties_get_int( frame_meta_properties, key );
1180 if ( map_start != j )
1181 source_offset += map_channels;
1185 if ( map_start != j )
1187 map_channels = current_channels;
1191 // Copy samples if source offset valid
1192 if ( source_offset < channels )
1194 // Interleave the audio buffer with the # channels for this stream/mapping.
1195 for ( k = 0; k < map_channels; k++, j++, source_offset++, dest_offset++ )
1197 int16_t *src = audio_buf_1 + source_offset;
1198 int16_t *dest = audio_buf_2 + dest_offset;
1199 int s = samples + 1;
1203 dest += current_channels;
1208 // Otherwise silence
1211 j += current_channels;
1212 dest_offset += current_channels;
1215 pkt.size = avcodec_encode_audio( codec, audio_outbuf, audio_outbuf_size, audio_buf_2 );
1218 // Write the compressed frame in the media file
1219 if ( codec->coded_frame && codec->coded_frame->pts != AV_NOPTS_VALUE )
1221 pkt.pts = av_rescale_q( codec->coded_frame->pts, codec->time_base, stream->time_base );
1222 mlt_log_debug( MLT_CONSUMER_SERVICE( this ), "audio stream %d pkt pts %lld frame pts %lld",
1223 stream->index, pkt.pts, codec->coded_frame->pts );
1225 pkt.flags |= PKT_FLAG_KEY;
1226 pkt.stream_index = stream->index;
1227 pkt.data = audio_outbuf;
1231 if ( av_interleaved_write_frame( oc, &pkt ) )
1232 mlt_log_error( MLT_CONSUMER_SERVICE( this ), "error writing audio frame %d\n", frames - 1 );
1235 mlt_log_debug( MLT_CONSUMER_SERVICE( this ), " frame_size %d\n", codec->frame_size );
1238 audio_pts = (double)stream->pts.val * av_q2d( stream->time_base );
1247 else if ( video_st )
1250 if ( mlt_deque_count( queue ) )
1255 frame = mlt_deque_pop_front( queue );
1256 frame_properties = MLT_FRAME_PROPERTIES( frame );
1258 c = video_st->codec;
1260 if ( mlt_properties_get_int( frame_properties, "rendered" ) )
1267 mlt_frame_get_image( frame, &image, &img_fmt, &img_width, &img_height, 0 );
1271 // Convert the mlt frame to an AVPicture
1272 for ( i = 0; i < height; i ++ )
1274 p = input->data[ 0 ] + i * input->linesize[ 0 ];
1283 // Do the colour space conversion
1285 struct SwsContext *context = sws_getContext( width, height, PIX_FMT_YUYV422,
1286 width, height, video_st->codec->pix_fmt, SWS_FAST_BILINEAR, NULL, NULL, NULL);
1287 sws_scale( context, input->data, input->linesize, 0, height,
1288 output->data, output->linesize);
1289 sws_freeContext( context );
1291 img_convert( ( AVPicture * )output, video_st->codec->pix_fmt, ( AVPicture * )input, PIX_FMT_YUYV422, width, height );
1294 mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
1296 // Apply the alpha if applicable
1297 if ( video_st->codec->pix_fmt == PIX_FMT_RGB32 )
1299 uint8_t *alpha = mlt_frame_get_alpha_mask( frame );
1302 for ( i = 0; i < height; i ++ )
1304 n = ( width + 7 ) / 8;
1305 p = output->data[ 0 ] + i * output->linesize[ 0 ] + 3;
1309 case 0: do { *p = *alpha++; p += 4;
1310 case 7: *p = *alpha++; p += 4;
1311 case 6: *p = *alpha++; p += 4;
1312 case 5: *p = *alpha++; p += 4;
1313 case 4: *p = *alpha++; p += 4;
1314 case 3: *p = *alpha++; p += 4;
1315 case 2: *p = *alpha++; p += 4;
1316 case 1: *p = *alpha++; p += 4;
1324 if (oc->oformat->flags & AVFMT_RAWPICTURE)
1326 // raw video case. The API will change slightly in the near future for that
1328 av_init_packet(&pkt);
1330 pkt.flags |= PKT_FLAG_KEY;
1331 pkt.stream_index= video_st->index;
1332 pkt.data= (uint8_t *)output;
1333 pkt.size= sizeof(AVPicture);
1335 ret = av_write_frame(oc, &pkt);
1336 video_pts += c->frame_size;
1341 output->quality = video_st->quality;
1343 // Set frame interlace hints
1344 output->interlaced_frame = !mlt_properties_get_int( frame_properties, "progressive" );
1345 output->top_field_first = mlt_properties_get_int( frame_properties, "top_field_first" );
1346 output->pts = frame_count;
1349 out_size = avcodec_encode_video(c, video_outbuf, video_outbuf_size, output );
1351 // If zero size, it means the image was buffered
1355 av_init_packet( &pkt );
1357 if ( c->coded_frame && c->coded_frame->pts != AV_NOPTS_VALUE )
1358 pkt.pts= av_rescale_q( c->coded_frame->pts, c->time_base, video_st->time_base );
1359 mlt_log_debug( MLT_CONSUMER_SERVICE( this ), "video pkt pts %lld frame pts %lld", pkt.pts, c->coded_frame->pts );
1360 if( c->coded_frame && c->coded_frame->key_frame )
1361 pkt.flags |= PKT_FLAG_KEY;
1362 pkt.stream_index= video_st->index;
1363 pkt.data= video_outbuf;
1366 // write the compressed frame in the media file
1367 ret = av_interleaved_write_frame(oc, &pkt);
1368 mlt_log_debug( MLT_CONSUMER_SERVICE( this ), " frame_size %d\n", c->frame_size );
1369 video_pts = (double)video_st->pts.val * av_q2d( video_st->time_base );
1371 // Dual pass logging
1372 if ( mlt_properties_get_data( properties, "_logfile", NULL ) && c->stats_out )
1373 fprintf( mlt_properties_get_data( properties, "_logfile", NULL ), "%s", c->stats_out );
1375 else if ( out_size < 0 )
1377 mlt_log_warning( MLT_CONSUMER_SERVICE( this ), "error with video encode %d\n", frame_count );
1381 mlt_frame_close( frame );
1389 mlt_log_debug( MLT_CONSUMER_SERVICE( this ), "audio pts %lld (%f) ", audio_st[0]->pts.val, audio_pts );
1391 mlt_log_debug( MLT_CONSUMER_SERVICE( this ), "video pts %lld (%f) ", video_st->pts.val, video_pts );
1392 mlt_log_debug( MLT_CONSUMER_SERVICE( this ), "\n" );
1395 if ( real_time_output == 1 && frames % 2 == 0 )
1397 long passed = time_difference( &ante );
1400 long pending = ( ( ( long )sample_fifo_used( fifo ) * 1000 ) / frequency ) * 1000;
1403 if ( passed < total_time )
1405 long total = ( total_time - passed );
1406 struct timespec t = { total / 1000000, ( total % 1000000 ) * 1000 };
1407 nanosleep( &t, NULL );
1412 // Flush the encoder buffers
1413 if ( real_time_output <= 0 )
1416 // TODO: flush all audio streams
1417 if ( audio_st[0] && audio_st[0]->codec->frame_size > 1 ) for (;;)
1419 AVCodecContext *c = audio_st[0]->codec;
1421 av_init_packet( &pkt );
1424 if ( /*( c->capabilities & CODEC_CAP_SMALL_LAST_FRAME ) &&*/
1425 ( channels * audio_input_frame_size < sample_fifo_used( fifo ) ) )
1427 sample_fifo_fetch( fifo, audio_buf_1, channels * audio_input_frame_size );
1428 pkt.size = avcodec_encode_audio( c, audio_outbuf, audio_outbuf_size, audio_buf_1 );
1430 if ( pkt.size <= 0 )
1431 pkt.size = avcodec_encode_audio( c, audio_outbuf, audio_outbuf_size, NULL );
1432 mlt_log_debug( MLT_CONSUMER_SERVICE( this ), "flushing audio size %d\n", pkt.size );
1433 if ( pkt.size <= 0 )
1436 // Write the compressed frame in the media file
1437 if ( c->coded_frame && c->coded_frame->pts != AV_NOPTS_VALUE )
1438 pkt.pts = av_rescale_q( c->coded_frame->pts, c->time_base, audio_st[0]->time_base );
1439 pkt.flags |= PKT_FLAG_KEY;
1440 pkt.stream_index = audio_st[0]->index;
1441 pkt.data = audio_outbuf;
1442 if ( av_interleaved_write_frame( oc, &pkt ) != 0 )
1444 mlt_log_error( MLT_CONSUMER_SERVICE( this ), "%s: error writing flushed audio frame\n", __FILE__ );
1450 if ( video_st && !( oc->oformat->flags & AVFMT_RAWPICTURE ) ) for (;;)
1452 AVCodecContext *c = video_st->codec;
1454 av_init_packet( &pkt );
1457 pkt.size = avcodec_encode_video( c, video_outbuf, video_outbuf_size, NULL );
1458 mlt_log_debug( MLT_CONSUMER_SERVICE( this ), "flushing video size %d\n", pkt.size );
1459 if ( pkt.size <= 0 )
1462 if ( c->coded_frame && c->coded_frame->pts != AV_NOPTS_VALUE )
1463 pkt.pts= av_rescale_q( c->coded_frame->pts, c->time_base, video_st->time_base );
1464 if( c->coded_frame && c->coded_frame->key_frame )
1465 pkt.flags |= PKT_FLAG_KEY;
1466 pkt.stream_index = video_st->index;
1467 pkt.data = video_outbuf;
1469 // write the compressed frame in the media file
1470 if ( av_interleaved_write_frame( oc, &pkt ) != 0 )
1472 mlt_log_error( MLT_CONSUMER_SERVICE(this), "error writing flushed video frame\n" );
1475 // Dual pass logging
1476 if ( mlt_properties_get_data( properties, "_logfile", NULL ) && c->stats_out )
1477 fprintf( mlt_properties_get_data( properties, "_logfile", NULL ), "%s", c->stats_out );
1481 // Write the trailer, if any
1482 av_write_trailer( oc );
1486 close_video(oc, video_st);
1487 for ( i = 0; i < MAX_AUDIO_STREAMS && audio_st[i]; i++ )
1488 close_audio( oc, audio_st[i] );
1491 for ( i = 0; i < oc->nb_streams; i++ )
1492 av_freep( &oc->streams[i] );
1494 // Close the output file
1495 if ( !( fmt->flags & AVFMT_NOFILE ) )
1496 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(0<<8)+0)
1497 url_fclose( oc->pb );
1499 url_fclose( &oc->pb );
1502 // Clean up input and output frames
1504 av_free( output->data[0] );
1506 av_free( input->data[0] );
1508 av_free( video_outbuf );
1509 av_free( audio_buf_1 );
1510 av_free( audio_buf_2 );
1515 // Just in case we terminated on pause
1516 mlt_properties_set_int( properties, "running", 0 );
1518 mlt_consumer_stopped( this );
1519 mlt_properties_close( frame_meta_properties );
1521 if ( mlt_properties_get_int( properties, "pass" ) > 1 )
1523 // Remove the dual pass log file
1524 if ( mlt_properties_get( properties, "_logfilename" ) )
1525 remove( mlt_properties_get( properties, "_logfilename" ) );
1527 // Remove the x264 dual pass logs
1528 char *cwd = getcwd( NULL, 0 );
1529 const char *file = "x264_2pass.log";
1530 char *full = malloc( strlen( cwd ) + strlen( file ) + 2 );
1531 sprintf( full, "%s/%s", cwd, file );
1534 file = "x264_2pass.log.temp";
1535 full = malloc( strlen( cwd ) + strlen( file ) + 2 );
1536 sprintf( full, "%s/%s", cwd, file );
1539 file = "x264_2pass.log.mbtree";
1540 full = malloc( strlen( cwd ) + strlen( file ) + 2 );
1541 sprintf( full, "%s/%s", cwd, file );
1545 remove( "x264_2pass.log.temp" );
1551 /** Close the consumer.
1554 static void consumer_close( mlt_consumer this )
1556 // Stop the consumer
1557 mlt_consumer_stop( this );
1560 mlt_consumer_close( this );