2 * consumer_avformat.c -- an encoder based on avformat
3 * Copyright (C) 2003-2012 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
5 * Author: Dan Dennedy <dan@dennedy.org>
6 * Much code borrowed from ffmpeg.c: Copyright (c) 2000-2003 Fabrice Bellard
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <framework/mlt_consumer.h>
25 #include <framework/mlt_frame.h>
26 #include <framework/mlt_profile.h>
27 #include <framework/mlt_log.h>
28 #include <framework/mlt_events.h>
30 // System header files
39 // avformat header files
40 #include <libavformat/avformat.h>
41 #include <libavformat/avio.h>
42 #include <libswscale/swscale.h>
43 #include <libavutil/pixdesc.h>
44 #include <libavutil/mathematics.h>
45 #include <libavutil/samplefmt.h>
47 #if LIBAVCODEC_VERSION_MAJOR >= 53
48 #include <libavutil/opt.h>
49 #define CODEC_TYPE_VIDEO AVMEDIA_TYPE_VIDEO
50 #define CODEC_TYPE_AUDIO AVMEDIA_TYPE_AUDIO
51 #define PKT_FLAG_KEY AV_PKT_FLAG_KEY
53 #include <libavcodec/opt.h>
56 #if LIBAVCODEC_VERSION_MAJOR < 55
57 #define AV_CODEC_ID_PCM_S16LE CODEC_ID_PCM_S16LE
58 #define AV_CODEC_ID_PCM_S16BE CODEC_ID_PCM_S16BE
59 #define AV_CODEC_ID_PCM_U16LE CODEC_ID_PCM_U16LE
60 #define AV_CODEC_ID_PCM_U16BE CODEC_ID_PCM_U16BE
61 #define AV_CODEC_ID_H264 CODEC_ID_H264
62 #define AV_CODEC_ID_NONE CODEC_ID_NONE
63 #define AV_CODEC_ID_AC3 CODEC_ID_AC3
64 #define AV_CODEC_ID_VORBIS CODEC_ID_VORBIS
67 #define MAX_AUDIO_STREAMS (8)
68 #define AUDIO_ENCODE_BUFFER_SIZE (48000 * 2 * MAX_AUDIO_STREAMS)
69 #define AUDIO_BUFFER_SIZE (1024 * 42)
70 #define VIDEO_BUFFER_SIZE (2048 * 1024)
73 // This structure should be extended and made globally available in mlt
85 *sample_fifo, sample_fifo_s;
87 sample_fifo sample_fifo_init( int frequency, int channels )
89 sample_fifo fifo = calloc( 1, sizeof( sample_fifo_s ) );
90 fifo->frequency = frequency;
91 fifo->channels = channels;
95 // count is the number of samples multiplied by the number of bytes per sample
96 void sample_fifo_append( sample_fifo fifo, uint8_t *samples, int count )
98 if ( ( fifo->size - fifo->used ) < count )
100 fifo->size += count * 5;
101 fifo->buffer = realloc( fifo->buffer, fifo->size );
104 memcpy( &fifo->buffer[ fifo->used ], samples, count );
108 int sample_fifo_used( sample_fifo fifo )
113 int sample_fifo_fetch( sample_fifo fifo, uint8_t *samples, int count )
115 if ( count > fifo->used )
118 memcpy( samples, fifo->buffer, count );
120 memmove( fifo->buffer, &fifo->buffer[ count ], fifo->used );
122 fifo->time += ( double )count / fifo->channels / fifo->frequency;
127 void sample_fifo_close( sample_fifo fifo )
129 free( fifo->buffer );
133 // Forward references.
134 static void property_changed( mlt_properties owner, mlt_consumer self, char *name );
135 static int consumer_start( mlt_consumer consumer );
136 static int consumer_stop( mlt_consumer consumer );
137 static int consumer_is_stopped( mlt_consumer consumer );
138 static void *consumer_thread( void *arg );
139 static void consumer_close( mlt_consumer consumer );
141 /** Initialise the consumer.
144 mlt_consumer consumer_avformat_init( mlt_profile profile, char *arg )
146 // Allocate the consumer
147 mlt_consumer consumer = mlt_consumer_new( profile );
149 // If memory allocated and initialises without error
150 if ( consumer != NULL )
152 // Get properties from the consumer
153 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
155 // Assign close callback
156 consumer->close = consumer_close;
158 // Interpret the argument
160 mlt_properties_set( properties, "target", arg );
162 // sample and frame queue
163 mlt_properties_set_data( properties, "frame_queue", mlt_deque_init( ), 0, ( mlt_destructor )mlt_deque_close, NULL );
165 // Audio options not fully handled by AVOptions
166 #define QSCALE_NONE (-99999)
167 mlt_properties_set_int( properties, "aq", QSCALE_NONE );
169 // Video options not fully handled by AVOptions
170 mlt_properties_set_int( properties, "dc", 8 );
172 // Muxer options not fully handled by AVOptions
173 mlt_properties_set_double( properties, "muxdelay", 0.7 );
174 mlt_properties_set_double( properties, "muxpreload", 0.5 );
176 // Ensure termination at end of the stream
177 mlt_properties_set_int( properties, "terminate_on_pause", 1 );
179 // Default to separate processing threads for producer and consumer with no frame dropping!
180 mlt_properties_set_int( properties, "real_time", -1 );
181 mlt_properties_set_int( properties, "prefill", 1 );
183 // Set up start/stop/terminated callbacks
184 consumer->start = consumer_start;
185 consumer->stop = consumer_stop;
186 consumer->is_stopped = consumer_is_stopped;
188 mlt_events_register( properties, "consumer-fatal-error", NULL );
189 mlt_event event = mlt_events_listen( properties, consumer, "property-changed", ( mlt_listener )property_changed );
190 mlt_properties_set_data( properties, "property-changed event", event, 0, NULL, NULL );
197 static void property_changed( mlt_properties owner, mlt_consumer self, char *name )
199 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
201 if ( !strcmp( name, "s" ) )
203 // Obtain the size property
204 char *size = mlt_properties_get( properties, "s" );
205 int width = mlt_properties_get_int( properties, "width" );
206 int height = mlt_properties_get_int( properties, "height" );
209 if ( sscanf( size, "%dx%d", &tw, &th ) == 2 && tw > 0 && th > 0 )
216 mlt_log_warning( MLT_CONSUMER_SERVICE(self), "Invalid size property %s - ignoring.\n", size );
219 // Now ensure we honour the multiple of two requested by libavformat
220 width = ( width / 2 ) * 2;
221 height = ( height / 2 ) * 2;
222 mlt_properties_set_int( properties, "width", width );
223 mlt_properties_set_int( properties, "height", height );
225 // "-aspect" on ffmpeg command line is display aspect ratio
226 else if ( !strcmp( name, "aspect" ) )
228 double ar = mlt_properties_get_double( properties, "aspect" );
229 AVRational rational = av_d2q( ar, 255 );
230 int width = mlt_properties_get_int( properties, "width" );
231 int height = mlt_properties_get_int( properties, "height" );
233 // Update the profile and properties as well since this is an alias
234 // for mlt properties that correspond to profile settings
235 mlt_properties_set_int( properties, "display_aspect_num", rational.num );
236 mlt_properties_set_int( properties, "display_aspect_den", rational.den );
238 // Now compute the sample aspect ratio
239 rational = av_d2q( ar * height / width, 255 );
241 // Update the profile and properties as well since this is an alias
242 // for mlt properties that correspond to profile settings
243 mlt_properties_set_int( properties, "sample_aspect_num", rational.num );
244 mlt_properties_set_int( properties, "sample_aspect_den", rational.den );
246 // Handle the ffmpeg command line "-r" property for frame rate
247 else if ( !strcmp( name, "r" ) )
249 double frame_rate = mlt_properties_get_double( properties, "r" );
250 AVRational rational = av_d2q( frame_rate, 255 );
251 mlt_properties_set_int( properties, "frame_rate_num", rational.num );
252 mlt_properties_set_int( properties, "frame_rate_den", rational.den );
256 /** Start the consumer.
259 static int consumer_start( mlt_consumer consumer )
261 // Get the properties
262 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
265 // Report information about available muxers and codecs as YAML Tiny
266 char *s = mlt_properties_get( properties, "f" );
267 if ( s && strcmp( s, "list" ) == 0 )
269 mlt_properties doc = mlt_properties_new();
270 mlt_properties formats = mlt_properties_new();
272 AVOutputFormat *format = NULL;
274 mlt_properties_set_data( properties, "f", formats, 0, (mlt_destructor) mlt_properties_close, NULL );
275 mlt_properties_set_data( doc, "formats", formats, 0, NULL, NULL );
276 while ( ( format = av_oformat_next( format ) ) )
278 snprintf( key, sizeof(key), "%d", mlt_properties_count( formats ) );
279 mlt_properties_set( formats, key, format->name );
281 s = mlt_properties_serialise_yaml( doc );
282 fprintf( stdout, "%s", s );
284 mlt_properties_close( doc );
287 s = mlt_properties_get( properties, "acodec" );
288 if ( s && strcmp( s, "list" ) == 0 )
290 mlt_properties doc = mlt_properties_new();
291 mlt_properties codecs = mlt_properties_new();
293 AVCodec *codec = NULL;
295 mlt_properties_set_data( properties, "acodec", codecs, 0, (mlt_destructor) mlt_properties_close, NULL );
296 mlt_properties_set_data( doc, "audio_codecs", codecs, 0, NULL, NULL );
297 while ( ( codec = av_codec_next( codec ) ) )
298 #if (defined(FFUDIV) && LIBAVCODEC_VERSION_INT >= ((54<<16)+(56<<8)+100)) || (LIBAVCODEC_VERSION_INT >= ((54<<16)+(27<<8)+0))
299 if ( codec->encode2 && codec->type == CODEC_TYPE_AUDIO )
300 #elif LIBAVCODEC_VERSION_INT >= ((54<<16)+(0<<8)+0)
301 if ( ( codec->encode || codec->encode2 ) && codec->type == CODEC_TYPE_AUDIO )
303 if ( codec->encode && codec->type == CODEC_TYPE_AUDIO )
306 snprintf( key, sizeof(key), "%d", mlt_properties_count( codecs ) );
307 mlt_properties_set( codecs, key, codec->name );
309 s = mlt_properties_serialise_yaml( doc );
310 fprintf( stdout, "%s", s );
312 mlt_properties_close( doc );
315 s = mlt_properties_get( properties, "vcodec" );
316 if ( s && strcmp( s, "list" ) == 0 )
318 mlt_properties doc = mlt_properties_new();
319 mlt_properties codecs = mlt_properties_new();
321 AVCodec *codec = NULL;
323 mlt_properties_set_data( properties, "vcodec", codecs, 0, (mlt_destructor) mlt_properties_close, NULL );
324 mlt_properties_set_data( doc, "video_codecs", codecs, 0, NULL, NULL );
325 while ( ( codec = av_codec_next( codec ) ) )
326 #if (defined(FFUDIV) && LIBAVCODEC_VERSION_INT >= ((54<<16)+(56<<8)+100)) || (LIBAVCODEC_VERSION_INT >= ((54<<16)+(27<<8)+0))
327 if ( codec->encode2 && codec->type == CODEC_TYPE_VIDEO )
328 #elif LIBAVCODEC_VERSION_INT >= ((54<<16)+(0<<8)+0)
329 if ( (codec->encode || codec->encode2) && codec->type == CODEC_TYPE_VIDEO )
331 if ( codec->encode && codec->type == CODEC_TYPE_VIDEO )
334 snprintf( key, sizeof(key), "%d", mlt_properties_count( codecs ) );
335 mlt_properties_set( codecs, key, codec->name );
337 s = mlt_properties_serialise_yaml( doc );
338 fprintf( stdout, "%s", s );
340 mlt_properties_close( doc );
344 // Check that we're not already running
345 if ( !error && !mlt_properties_get_int( properties, "running" ) )
348 pthread_t *thread = calloc( 1, sizeof( pthread_t ) );
350 mlt_event_block( mlt_properties_get_data( properties, "property-changed event", NULL ) );
352 // Apply AVOptions that are synonyms for standard mlt_consumer options
353 if ( mlt_properties_get( properties, "ac" ) )
354 mlt_properties_set_int( properties, "channels", mlt_properties_get_int( properties, "ac" ) );
355 if ( mlt_properties_get( properties, "ar" ) )
356 mlt_properties_set_int( properties, "frequency", mlt_properties_get_int( properties, "ar" ) );
358 // Assign the thread to properties
359 mlt_properties_set_data( properties, "thread", thread, sizeof( pthread_t ), free, NULL );
362 pthread_create( thread, NULL, consumer_thread, consumer );
364 // Set the running state
365 mlt_properties_set_int( properties, "running", 1 );
370 /** Stop the consumer.
373 static int consumer_stop( mlt_consumer consumer )
375 // Get the properties
376 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
377 pthread_t *thread = mlt_properties_get_data( properties, "thread", NULL );
379 // Check that we're running
383 mlt_properties_set_int( properties, "running", 0 );
385 // Wait for termination
386 pthread_join( *thread, NULL );
388 mlt_properties_set_data( properties, "thread", NULL, 0, NULL, NULL );
389 mlt_event_unblock( mlt_properties_get_data( properties, "property-changed event", NULL ) );
395 /** Determine if the consumer is stopped.
398 static int consumer_is_stopped( mlt_consumer consumer )
400 // Get the properties
401 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
402 return !mlt_properties_get_int( properties, "running" );
405 /** Process properties as AVOptions and apply to AV context obj
408 static void apply_properties( void *obj, mlt_properties properties, int flags )
411 int count = mlt_properties_count( properties );
412 #if LIBAVUTIL_VERSION_INT < ((51<<16)+(12<<8)+0)
416 for ( i = 0; i < count; i++ )
418 const char *opt_name = mlt_properties_get_name( properties, i );
419 #if LIBAVUTIL_VERSION_INT >= ((51<<16)+(10<<8)+0)
420 const AVOption *opt = av_opt_find( obj, opt_name, NULL, flags, flags );
422 const AVOption *opt = av_find_opt( obj, opt_name, NULL, flags, flags );
425 // If option not found, see if it was prefixed with a or v (-vb)
427 ( opt_name[0] == 'v' && ( flags & AV_OPT_FLAG_VIDEO_PARAM ) ) ||
428 ( opt_name[0] == 'a' && ( flags & AV_OPT_FLAG_AUDIO_PARAM ) ) ) )
429 #if LIBAVUTIL_VERSION_INT >= ((51<<16)+(10<<8)+0)
430 opt = av_opt_find( obj, ++opt_name, NULL, flags, flags );
432 opt = av_find_opt( obj, ++opt_name, NULL, flags, flags );
434 // Apply option if found
436 #if LIBAVUTIL_VERSION_INT >= ((51<<16)+(12<<8)+0)
437 av_opt_set( obj, opt_name, mlt_properties_get_value( properties, i), 0 );
439 av_set_string3( obj, opt_name, mlt_properties_get_value( properties, i), alloc, NULL );
444 static int get_mlt_audio_format( int av_sample_fmt )
446 switch ( av_sample_fmt )
448 case AV_SAMPLE_FMT_U8:
450 case AV_SAMPLE_FMT_S32:
451 return mlt_audio_s32le;
452 case AV_SAMPLE_FMT_FLT:
453 return mlt_audio_f32le;
454 #if LIBAVUTIL_VERSION_INT >= ((51<<16)+(17<<8)+0)
455 case AV_SAMPLE_FMT_U8P:
457 case AV_SAMPLE_FMT_S32P:
458 return mlt_audio_s32le;
459 case AV_SAMPLE_FMT_FLTP:
460 return mlt_audio_f32le;
463 return mlt_audio_s16;
467 static int pick_sample_fmt( mlt_properties properties, AVCodec *codec )
469 int sample_fmt = AV_SAMPLE_FMT_S16;
470 const char *format = mlt_properties_get( properties, "mlt_audio_format" );
471 const int *p = codec->sample_fmts;
473 // get default av_sample_fmt from mlt_audio_format
476 if ( !strcmp( format, "s32le" ) )
477 sample_fmt = AV_SAMPLE_FMT_S32;
478 else if ( !strcmp( format, "f32le" ) )
479 sample_fmt = AV_SAMPLE_FMT_FLT;
480 else if ( !strcmp( format, "u8" ) )
481 sample_fmt = AV_SAMPLE_FMT_U8;
482 #if LIBAVUTIL_VERSION_INT >= ((51<<16)+(17<<8)+0)
483 else if ( !strcmp( format, "s32" ) )
484 sample_fmt = AV_SAMPLE_FMT_S32P;
485 else if ( !strcmp( format, "float" ) )
486 sample_fmt = AV_SAMPLE_FMT_FLTP;
489 // check if codec supports our mlt_audio_format
490 for ( ; *p != -1; p++ )
492 if ( *p == sample_fmt )
495 // no match - pick first one we support
496 for ( p = codec->sample_fmts; *p != -1; p++ )
500 case AV_SAMPLE_FMT_U8:
501 case AV_SAMPLE_FMT_S16:
502 case AV_SAMPLE_FMT_S32:
503 case AV_SAMPLE_FMT_FLT:
504 #if LIBAVUTIL_VERSION_INT >= ((51<<16)+(17<<8)+0)
505 case AV_SAMPLE_FMT_U8P:
506 case AV_SAMPLE_FMT_S16P:
507 case AV_SAMPLE_FMT_S32P:
508 case AV_SAMPLE_FMT_FLTP:
515 mlt_log_error( properties, "audio codec sample_fmt not compatible" );
517 return AV_SAMPLE_FMT_NONE;
520 static uint8_t* interleaved_to_planar( int samples, int channels, uint8_t* audio, int bytes_per_sample )
522 uint8_t *buffer = mlt_pool_alloc( AUDIO_ENCODE_BUFFER_SIZE );
526 memset( buffer, 0, AUDIO_ENCODE_BUFFER_SIZE );
527 for ( c = 0; c < channels; c++ )
529 uint8_t *q = audio + c * bytes_per_sample;
533 memcpy( p, q, bytes_per_sample );
534 p += bytes_per_sample;
535 q += channels * bytes_per_sample;
541 /** Add an audio output stream
544 static AVStream *add_audio_stream( mlt_consumer consumer, AVFormatContext *oc, AVCodec *codec, int channels )
546 // Get the properties
547 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
549 // Create a new stream
550 #if LIBAVFORMAT_VERSION_INT >= ((53<<16)+(10<<8)+0)
551 AVStream *st = avformat_new_stream( oc, codec );
553 AVStream *st = av_new_stream( oc, oc->nb_streams );
556 // If created, then initialise from properties
559 AVCodecContext *c = st->codec;
561 // Establish defaults from AVOptions
562 #if LIBAVCODEC_VERSION_MAJOR >= 53
563 avcodec_get_context_defaults3( c, codec );
565 avcodec_get_context_defaults2( c, CODEC_TYPE_AUDIO );
568 c->codec_id = codec->id;
569 c->codec_type = CODEC_TYPE_AUDIO;
570 c->sample_fmt = pick_sample_fmt( properties, codec );
572 #if 0 // disabled until some audio codecs are multi-threaded
573 // Setup multi-threading
574 int thread_count = mlt_properties_get_int( properties, "threads" );
575 if ( thread_count == 0 && getenv( "MLT_AVFORMAT_THREADS" ) )
576 thread_count = atoi( getenv( "MLT_AVFORMAT_THREADS" ) );
577 if ( thread_count > 1 )
578 c->thread_count = thread_count;
581 if (oc->oformat->flags & AVFMT_GLOBALHEADER)
582 c->flags |= CODEC_FLAG_GLOBAL_HEADER;
584 // Allow the user to override the audio fourcc
585 if ( mlt_properties_get( properties, "atag" ) )
588 char *arg = mlt_properties_get( properties, "atag" );
589 int tag = strtol( arg, &tail, 0);
591 tag = arg[ 0 ] + ( arg[ 1 ] << 8 ) + ( arg[ 2 ] << 16 ) + ( arg[ 3 ] << 24 );
595 // Process properties as AVOptions
596 char *apre = mlt_properties_get( properties, "apre" );
599 mlt_properties p = mlt_properties_load( apre );
600 apply_properties( c, p, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM );
601 mlt_properties_close( p );
603 apply_properties( c, properties, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM );
605 int audio_qscale = mlt_properties_get_int( properties, "aq" );
606 if ( audio_qscale > QSCALE_NONE )
608 c->flags |= CODEC_FLAG_QSCALE;
609 c->global_quality = FF_QP2LAMBDA * audio_qscale;
610 #if LIBAVFORMAT_VERSION_MAJOR < 53
611 st->quality = c->global_quality;
615 // Set parameters controlled by MLT
616 c->sample_rate = mlt_properties_get_int( properties, "frequency" );
617 c->time_base = ( AVRational ){ 1, c->sample_rate };
618 c->channels = channels;
620 if ( mlt_properties_get( properties, "alang" ) != NULL )
621 #if LIBAVUTIL_VERSION_INT >= ((51<<16)+(8<<8)+0)
622 av_dict_set( &oc->metadata, "language", mlt_properties_get( properties, "alang" ), 0 );
624 av_metadata_set2( &oc->metadata, "language", mlt_properties_get( properties, "alang" ), 0 );
629 mlt_log_error( MLT_CONSUMER_SERVICE( consumer ), "Could not allocate a stream for audio\n" );
635 static int open_audio( mlt_properties properties, AVFormatContext *oc, AVStream *st, int audio_outbuf_size, const char *codec_name )
637 // We will return the audio input size from here
638 int audio_input_frame_size = 0;
641 AVCodecContext *c = st->codec;
646 codec = avcodec_find_encoder_by_name( codec_name );
648 codec = avcodec_find_encoder( c->codec_id );
650 // Process properties as AVOptions on the AVCodec
651 if ( codec && codec->priv_class )
653 char *apre = mlt_properties_get( properties, "apre" );
654 if ( !c->priv_data && codec->priv_data_size )
656 c->priv_data = av_mallocz( codec->priv_data_size );
657 *(const AVClass **) c->priv_data = codec->priv_class;
658 // av_opt_set_defaults( c );
662 mlt_properties p = mlt_properties_load( apre );
663 apply_properties( c->priv_data, p, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM );
664 mlt_properties_close( p );
666 apply_properties( c->priv_data, properties, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM );
669 // Continue if codec found and we can open it
670 #if LIBAVCODEC_VERSION_INT >= ((53<<16)+(8<<8)+0)
671 if ( codec && avcodec_open2( c, codec, NULL ) >= 0 )
673 if ( codec && avcodec_open( c, codec ) >= 0 )
676 // ugly hack for PCM codecs (will be removed ASAP with new PCM
677 // support to compute the input frame size in samples
678 if ( c->frame_size <= 1 )
680 audio_input_frame_size = audio_outbuf_size / c->channels;
681 switch(st->codec->codec_id)
683 case AV_CODEC_ID_PCM_S16LE:
684 case AV_CODEC_ID_PCM_S16BE:
685 case AV_CODEC_ID_PCM_U16LE:
686 case AV_CODEC_ID_PCM_U16BE:
687 audio_input_frame_size >>= 1;
695 audio_input_frame_size = c->frame_size;
698 // Some formats want stream headers to be seperate (hmm)
699 if ( !strcmp( oc->oformat->name, "mp4" ) ||
700 !strcmp( oc->oformat->name, "mov" ) ||
701 !strcmp( oc->oformat->name, "3gp" ) )
702 c->flags |= CODEC_FLAG_GLOBAL_HEADER;
706 mlt_log_warning( NULL, "%s: Unable to encode audio - disabling audio output.\n", __FILE__ );
707 audio_input_frame_size = 0;
710 return audio_input_frame_size;
713 static void close_audio( AVFormatContext *oc, AVStream *st )
715 if ( st && st->codec )
716 avcodec_close( st->codec );
719 /** Add a video output stream
722 static AVStream *add_video_stream( mlt_consumer consumer, AVFormatContext *oc, AVCodec *codec )
724 // Get the properties
725 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
727 // Create a new stream
728 #if LIBAVFORMAT_VERSION_INT >= ((53<<16)+(10<<8)+0)
729 AVStream *st = avformat_new_stream( oc, codec );
731 AVStream *st = av_new_stream( oc, oc->nb_streams );
736 char *pix_fmt = mlt_properties_get( properties, "pix_fmt" );
737 AVCodecContext *c = st->codec;
739 // Establish defaults from AVOptions
740 #if LIBAVCODEC_VERSION_MAJOR >= 53
741 avcodec_get_context_defaults3( c, codec );
743 avcodec_get_context_defaults2( c, CODEC_TYPE_VIDEO );
746 c->codec_id = codec->id;
747 c->codec_type = CODEC_TYPE_VIDEO;
749 // Setup multi-threading
750 int thread_count = mlt_properties_get_int( properties, "threads" );
751 if ( thread_count == 0 && getenv( "MLT_AVFORMAT_THREADS" ) )
752 thread_count = atoi( getenv( "MLT_AVFORMAT_THREADS" ) );
753 if ( thread_count > 1 )
754 #if LIBAVCODEC_VERSION_MAJOR >= 53
755 c->thread_count = thread_count;
757 avcodec_thread_init( c, thread_count );
760 // Process properties as AVOptions
761 char *vpre = mlt_properties_get( properties, "vpre" );
764 mlt_properties p = mlt_properties_load( vpre );
766 if ( mlt_properties_count( p ) < 1 )
768 AVCodec *codec = avcodec_find_encoder( c->codec_id );
771 char *path = malloc( strlen(AVDATADIR) + strlen(codec->name) + strlen(vpre) + strlen(".ffpreset") + 2 );
772 strcpy( path, AVDATADIR );
773 strcat( path, codec->name );
775 strcat( path, vpre );
776 strcat( path, ".ffpreset" );
778 mlt_properties_close( p );
779 p = mlt_properties_load( path );
780 if ( mlt_properties_count( p ) > 0 )
781 mlt_properties_debug( p, path, stderr );
787 mlt_properties_debug( p, vpre, stderr );
790 apply_properties( c, p, AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM );
791 mlt_properties_close( p );
793 int colorspace = mlt_properties_get_int( properties, "colorspace" );
794 mlt_properties_set( properties, "colorspace", NULL );
795 apply_properties( c, properties, AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM );
796 mlt_properties_set_int( properties, "colorspace", colorspace );
798 // Set options controlled by MLT
799 c->width = mlt_properties_get_int( properties, "width" );
800 c->height = mlt_properties_get_int( properties, "height" );
801 c->time_base.num = mlt_properties_get_int( properties, "frame_rate_den" );
802 c->time_base.den = mlt_properties_get_int( properties, "frame_rate_num" );
803 if ( st->time_base.den == 0 )
804 st->time_base = c->time_base;
805 c->pix_fmt = pix_fmt ? av_get_pix_fmt( pix_fmt ) : PIX_FMT_YUV420P;
807 switch ( colorspace )
810 c->colorspace = AVCOL_SPC_SMPTE170M;
813 c->colorspace = AVCOL_SPC_SMPTE240M;
816 c->colorspace = AVCOL_SPC_BT470BG;
819 c->colorspace = ( 576 % c->height ) ? AVCOL_SPC_SMPTE170M : AVCOL_SPC_BT470BG;
822 c->colorspace = AVCOL_SPC_BT709;
826 if ( mlt_properties_get( properties, "aspect" ) )
828 // "-aspect" on ffmpeg command line is display aspect ratio
829 double ar = mlt_properties_get_double( properties, "aspect" );
830 c->sample_aspect_ratio = av_d2q( ar * c->height / c->width, 255 );
834 c->sample_aspect_ratio.num = mlt_properties_get_int( properties, "sample_aspect_num" );
835 c->sample_aspect_ratio.den = mlt_properties_get_int( properties, "sample_aspect_den" );
837 st->sample_aspect_ratio = c->sample_aspect_ratio;
839 if ( mlt_properties_get_double( properties, "qscale" ) > 0 )
841 c->flags |= CODEC_FLAG_QSCALE;
842 c->global_quality = FF_QP2LAMBDA * mlt_properties_get_double( properties, "qscale" );
843 #if LIBAVFORMAT_VERSION_MAJOR < 53
844 st->quality = c->global_quality;
848 // Allow the user to override the video fourcc
849 if ( mlt_properties_get( properties, "vtag" ) )
852 const char *arg = mlt_properties_get( properties, "vtag" );
853 int tag = strtol( arg, &tail, 0);
855 tag = arg[ 0 ] + ( arg[ 1 ] << 8 ) + ( arg[ 2 ] << 16 ) + ( arg[ 3 ] << 24 );
859 // Some formats want stream headers to be seperate
860 if ( oc->oformat->flags & AVFMT_GLOBALHEADER )
861 c->flags |= CODEC_FLAG_GLOBAL_HEADER;
863 // Translate these standard mlt consumer properties to ffmpeg
864 if ( mlt_properties_get_int( properties, "progressive" ) == 0 &&
865 mlt_properties_get_int( properties, "deinterlace" ) == 0 )
867 if ( ! mlt_properties_get( properties, "ildct" ) || mlt_properties_get_int( properties, "ildct" ) )
868 c->flags |= CODEC_FLAG_INTERLACED_DCT;
869 if ( ! mlt_properties_get( properties, "ilme" ) || mlt_properties_get_int( properties, "ilme" ) )
870 c->flags |= CODEC_FLAG_INTERLACED_ME;
873 // parse the ratecontrol override string
875 char *rc_override = mlt_properties_get( properties, "rc_override" );
876 for ( i = 0; rc_override; i++ )
879 int e = sscanf( rc_override, "%d,%d,%d", &start, &end, &q );
881 mlt_log_warning( MLT_CONSUMER_SERVICE( consumer ), "Error parsing rc_override\n" );
882 c->rc_override = av_realloc( c->rc_override, sizeof( RcOverride ) * ( i + 1 ) );
883 c->rc_override[i].start_frame = start;
884 c->rc_override[i].end_frame = end;
887 c->rc_override[i].qscale = q;
888 c->rc_override[i].quality_factor = 1.0;
892 c->rc_override[i].qscale = 0;
893 c->rc_override[i].quality_factor = -q / 100.0;
895 rc_override = strchr( rc_override, '/' );
899 c->rc_override_count = i;
900 if ( !c->rc_initial_buffer_occupancy )
901 c->rc_initial_buffer_occupancy = c->rc_buffer_size * 3/4;
902 c->intra_dc_precision = mlt_properties_get_int( properties, "dc" ) - 8;
905 i = mlt_properties_get_int( properties, "pass" );
907 c->flags |= CODEC_FLAG_PASS1;
909 c->flags |= CODEC_FLAG_PASS2;
910 if ( codec->id != AV_CODEC_ID_H264 && ( c->flags & ( CODEC_FLAG_PASS1 | CODEC_FLAG_PASS2 ) ) )
912 char logfilename[1024];
917 snprintf( logfilename, sizeof(logfilename), "%s_2pass.log",
918 mlt_properties_get( properties, "passlogfile" ) ? mlt_properties_get( properties, "passlogfile" ) : mlt_properties_get( properties, "target" ) );
919 if ( c->flags & CODEC_FLAG_PASS1 )
921 f = fopen( logfilename, "w" );
923 perror( logfilename );
925 mlt_properties_set_data( properties, "_logfile", f, 0, ( mlt_destructor )fclose, NULL );
929 /* read the log file */
930 f = fopen( logfilename, "r" );
937 mlt_properties_set( properties, "_logfilename", logfilename );
938 fseek( f, 0, SEEK_END );
940 fseek( f, 0, SEEK_SET );
941 logbuffer = av_malloc( size + 1 );
943 mlt_log_fatal( MLT_CONSUMER_SERVICE( consumer ), "Could not allocate log buffer\n" );
948 size = fread( logbuffer, 1, size, f );
949 logbuffer[size] = '\0';
950 c->stats_in = logbuffer;
960 mlt_log_error( MLT_CONSUMER_SERVICE( consumer ), "Could not allocate a stream for video\n" );
966 static AVFrame *alloc_picture( int pix_fmt, int width, int height )
969 AVFrame *picture = avcodec_alloc_frame();
971 // Determine size of the
972 int size = avpicture_get_size(pix_fmt, width, height);
974 // Allocate the picture buf
975 uint8_t *picture_buf = av_malloc(size);
977 // If we have both, then fill the image
978 if ( picture != NULL && picture_buf != NULL )
980 // Fill the frame with the allocated buffer
981 avpicture_fill( (AVPicture *)picture, picture_buf, pix_fmt, width, height);
985 // Something failed - clean up what we can
987 av_free( picture_buf );
994 static int open_video( mlt_properties properties, AVFormatContext *oc, AVStream *st, const char *codec_name )
997 AVCodecContext *video_enc = st->codec;
999 // find the video encoder
1002 codec = avcodec_find_encoder_by_name( codec_name );
1004 codec = avcodec_find_encoder( video_enc->codec_id );
1006 // Process properties as AVOptions on the AVCodec
1007 if ( codec && codec->priv_class )
1009 char *vpre = mlt_properties_get( properties, "vpre" );
1010 if ( !video_enc->priv_data && codec->priv_data_size )
1012 video_enc->priv_data = av_mallocz( codec->priv_data_size );
1013 *(const AVClass **) video_enc->priv_data = codec->priv_class;
1014 // av_opt_set_defaults( video_enc );
1018 mlt_properties p = mlt_properties_load( vpre );
1019 apply_properties( video_enc->priv_data, p, AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM );
1020 mlt_properties_close( p );
1022 apply_properties( video_enc->priv_data, properties, AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM );
1025 if( codec && codec->pix_fmts )
1027 const enum PixelFormat *p = codec->pix_fmts;
1028 for( ; *p!=-1; p++ )
1030 if( *p == video_enc->pix_fmt )
1034 video_enc->pix_fmt = codec->pix_fmts[ 0 ];
1037 #if LIBAVCODEC_VERSION_INT >= ((53<<16)+(8<<8)+0)
1038 int result = codec && avcodec_open2( video_enc, codec, NULL ) >= 0;
1040 int result = codec && avcodec_open( video_enc, codec ) >= 0;
1046 void close_video(AVFormatContext *oc, AVStream *st)
1048 if ( st && st->codec )
1050 av_freep( &st->codec->stats_in );
1051 avcodec_close(st->codec);
1055 static inline long time_difference( struct timeval *time1 )
1057 struct timeval time2;
1058 gettimeofday( &time2, NULL );
1059 return time2.tv_sec * 1000000 + time2.tv_usec - time1->tv_sec * 1000000 - time1->tv_usec;
1062 static int mlt_write(void *h, uint8_t *buf, int size)
1064 mlt_properties properties = (mlt_properties) h;
1065 mlt_events_fire( properties, "avformat-write", buf, size, NULL );
1069 static void write_transmitter( mlt_listener listener, mlt_properties owner, mlt_service service, void **args )
1071 listener( owner, service, (uint8_t*) args[0], (int) args[1] );
1074 /** The main thread - the argument is simply the consumer.
1077 static void *consumer_thread( void *arg )
1079 // Map the argument to the object
1080 mlt_consumer consumer = arg;
1082 // Get the properties
1083 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
1085 // Get the terminate on pause property
1086 int terminate_on_pause = mlt_properties_get_int( properties, "terminate_on_pause" );
1089 // Determine if feed is slow (for realtime stuff)
1090 int real_time_output = mlt_properties_get_int( properties, "real_time" );
1093 struct timeval ante;
1095 // Get the frame rate
1096 double fps = mlt_properties_get_double( properties, "fps" );
1098 // Get width and height
1099 int width = mlt_properties_get_int( properties, "width" );
1100 int height = mlt_properties_get_int( properties, "height" );
1101 int img_width = width;
1102 int img_height = height;
1104 // Get default audio properties
1105 int channels = mlt_properties_get_int( properties, "channels" );
1106 int total_channels = channels;
1107 int frequency = mlt_properties_get_int( properties, "frequency" );
1111 // AVFormat audio buffer and frame size
1112 int audio_outbuf_size = AUDIO_BUFFER_SIZE;
1113 uint8_t *audio_outbuf = av_malloc( audio_outbuf_size );
1114 int audio_input_nb_samples = 0;
1116 // AVFormat video buffer and frame count
1117 int frame_count = 0;
1118 int video_outbuf_size = VIDEO_BUFFER_SIZE;
1119 uint8_t *video_outbuf = av_malloc( video_outbuf_size );
1121 // Used for the frame properties
1122 mlt_frame frame = NULL;
1123 mlt_properties frame_properties = NULL;
1126 mlt_deque queue = mlt_properties_get_data( properties, "frame_queue", NULL );
1127 sample_fifo fifo = mlt_properties_get_data( properties, "sample_fifo", NULL );
1129 // Need two av pictures for converting
1130 AVFrame *converted_avframe = NULL;
1131 AVFrame *audio_avframe = NULL;
1132 AVFrame *video_avframe = alloc_picture( PIX_FMT_YUYV422, width, height );
1134 // For receiving images from an mlt_frame
1136 mlt_image_format img_fmt = mlt_image_yuv422;
1138 // For receiving audio samples back from the fifo
1139 uint8_t *audio_buf_1 = av_malloc( AUDIO_ENCODE_BUFFER_SIZE );
1140 uint8_t *audio_buf_2 = NULL;
1143 // Allocate the context
1144 AVFormatContext *oc = avformat_alloc_context( );
1147 AVStream *video_st = NULL;
1148 AVStream *audio_st[ MAX_AUDIO_STREAMS ];
1151 double audio_pts = 0;
1152 double video_pts = 0;
1154 // Frames dispatched
1155 long int frames = 0;
1156 long int total_time = 0;
1158 // Determine the format
1159 AVOutputFormat *fmt = NULL;
1160 const char *filename = mlt_properties_get( properties, "target" );
1161 char *format = mlt_properties_get( properties, "f" );
1162 char *vcodec = mlt_properties_get( properties, "vcodec" );
1163 char *acodec = mlt_properties_get( properties, "acodec" );
1164 AVCodec *audio_codec = NULL;
1165 AVCodec *video_codec = NULL;
1167 // Used to store and override codec ids
1173 mlt_properties frame_meta_properties = mlt_properties_new();
1175 // Initialize audio_st
1176 int i = MAX_AUDIO_STREAMS;
1180 // Check for user selected format first
1181 if ( format != NULL )
1182 fmt = av_guess_format( format, NULL, NULL );
1184 // Otherwise check on the filename
1185 if ( fmt == NULL && filename != NULL )
1186 fmt = av_guess_format( NULL, filename, NULL );
1188 // Otherwise default to mpeg
1190 fmt = av_guess_format( "mpeg", NULL, NULL );
1192 // We need a filename - default to stdout?
1193 if ( filename == NULL || !strcmp( filename, "" ) )
1196 // Get the codec ids selected
1197 audio_codec_id = fmt->audio_codec;
1198 video_codec_id = fmt->video_codec;
1200 // Check for audio codec overides
1201 if ( ( acodec && strcmp( acodec, "none" ) == 0 ) || mlt_properties_get_int( properties, "an" ) )
1202 audio_codec_id = AV_CODEC_ID_NONE;
1205 audio_codec = avcodec_find_encoder_by_name( acodec );
1208 audio_codec_id = audio_codec->id;
1209 if ( audio_codec_id == AV_CODEC_ID_AC3 && avcodec_find_encoder_by_name( "ac3_fixed" ) )
1211 mlt_properties_set( properties, "_acodec", "ac3_fixed" );
1212 acodec = mlt_properties_get( properties, "_acodec" );
1213 audio_codec = avcodec_find_encoder_by_name( acodec );
1215 else if ( !strcmp( acodec, "aac" ) )
1217 mlt_properties_set( properties, "astrict", "experimental" );
1222 audio_codec_id = AV_CODEC_ID_NONE;
1223 mlt_log_warning( MLT_CONSUMER_SERVICE( consumer ), "audio codec %s unrecognised - ignoring\n", acodec );
1228 audio_codec = avcodec_find_encoder( audio_codec_id );
1231 // Check for video codec overides
1232 if ( ( vcodec && strcmp( vcodec, "none" ) == 0 ) || mlt_properties_get_int( properties, "vn" ) )
1233 video_codec_id = AV_CODEC_ID_NONE;
1236 video_codec = avcodec_find_encoder_by_name( vcodec );
1239 video_codec_id = video_codec->id;
1243 video_codec_id = AV_CODEC_ID_NONE;
1244 mlt_log_warning( MLT_CONSUMER_SERVICE( consumer ), "video codec %s unrecognised - ignoring\n", vcodec );
1249 video_codec = avcodec_find_encoder( video_codec_id );
1253 for ( i = 0; i < mlt_properties_count( properties ); i++ )
1255 char *name = mlt_properties_get_name( properties, i );
1256 if ( name && !strncmp( name, "meta.attr.", 10 ) )
1258 char *key = strdup( name + 10 );
1259 char *markup = strrchr( key, '.' );
1260 if ( markup && !strcmp( markup, ".markup") )
1263 if ( !strstr( key, ".stream." ) )
1264 #if LIBAVUTIL_VERSION_INT >= ((51<<16)+(8<<8)+0)
1265 av_dict_set( &oc->metadata, key, mlt_properties_get_value( properties, i ), 0 );
1267 av_metadata_set2( &oc->metadata, key, mlt_properties_get_value( properties, i ), 0 );
1275 snprintf( oc->filename, sizeof(oc->filename), "%s", filename );
1277 // Get a frame now, so we can set some AVOptions from properties.
1278 frame = mlt_consumer_rt_frame( consumer );
1280 // Set the timecode from the MLT metadata if available.
1283 const char *timecode = mlt_properties_get( MLT_FRAME_PROPERTIES(frame), "meta.attr.vitc.markup" );
1284 if ( timecode && strcmp( timecode, "" ) )
1286 mlt_properties_set( properties, "timecode", timecode );
1287 if ( strchr( timecode, ';' ) )
1288 mlt_properties_set_int( properties, "drop_frame_timecode", 1 );
1292 // Add audio and video streams
1293 if ( video_codec_id != AV_CODEC_ID_NONE )
1294 video_st = add_video_stream( consumer, oc, video_codec );
1295 if ( audio_codec_id != AV_CODEC_ID_NONE )
1301 for ( i = 0; i < MAX_AUDIO_STREAMS; i++ )
1303 sprintf( key, "channels.%d", i );
1304 int j = mlt_properties_get_int( properties, key );
1308 total_channels += j;
1309 audio_st[i] = add_audio_stream( consumer, oc, audio_codec, j );
1315 audio_st[0] = add_audio_stream( consumer, oc, audio_codec, channels );
1316 total_channels = channels;
1319 mlt_properties_set_int( properties, "channels", total_channels );
1321 // Audio format is determined when adding the audio stream
1322 mlt_audio_format aud_fmt = mlt_audio_none;
1324 aud_fmt = get_mlt_audio_format( audio_st[0]->codec->sample_fmt );
1325 int sample_bytes = mlt_audio_format_size( aud_fmt, 1, 1 );
1326 sample_bytes = sample_bytes ? sample_bytes : 1; // prevent divide by zero
1328 // Set the parameters (even though we have none...)
1329 #if LIBAVFORMAT_VERSION_INT < ((53<<16)+(2<<8)+0)
1330 if ( av_set_parameters(oc, NULL) >= 0 )
1333 #if LIBAVFORMAT_VERSION_MAJOR >= 53
1334 if ( mlt_properties_get( properties, "muxpreload" ) && ! mlt_properties_get( properties, "preload" ) )
1335 mlt_properties_set_double( properties, "preload", mlt_properties_get_double( properties, "muxpreload" ) );
1337 oc->preload = ( int )( mlt_properties_get_double( properties, "muxpreload" ) * AV_TIME_BASE );
1339 oc->max_delay= ( int )( mlt_properties_get_double( properties, "muxdelay" ) * AV_TIME_BASE );
1341 // Process properties as AVOptions
1342 char *fpre = mlt_properties_get( properties, "fpre" );
1345 mlt_properties p = mlt_properties_load( fpre );
1346 apply_properties( oc, p, AV_OPT_FLAG_ENCODING_PARAM );
1347 if ( oc->oformat && oc->oformat->priv_class && oc->priv_data )
1348 apply_properties( oc->priv_data, p, AV_OPT_FLAG_ENCODING_PARAM );
1349 mlt_properties_close( p );
1351 apply_properties( oc, properties, AV_OPT_FLAG_ENCODING_PARAM );
1352 if ( oc->oformat && oc->oformat->priv_class && oc->priv_data )
1353 apply_properties( oc->priv_data, properties, AV_OPT_FLAG_ENCODING_PARAM );
1355 if ( video_st && !open_video( properties, oc, video_st, vcodec? vcodec : NULL ) )
1357 for ( i = 0; i < MAX_AUDIO_STREAMS && audio_st[i]; i++ )
1359 audio_input_nb_samples = open_audio( properties, oc, audio_st[i], audio_outbuf_size,
1360 acodec? acodec : NULL );
1361 if ( !audio_input_nb_samples )
1363 // Remove the audio stream from the output context
1365 for ( j = 0; j < oc->nb_streams; j++ )
1367 if ( oc->streams[j] == audio_st[i] )
1368 av_freep( &oc->streams[j] );
1375 // Setup custom I/O if redirecting
1376 if ( mlt_properties_get_int( properties, "redirect" ) )
1378 int buffer_size = 32768;
1379 unsigned char *buffer = av_malloc( buffer_size );
1380 #if LIBAVFORMAT_VERSION_MAJOR >= 53
1381 AVIOContext* io = avio_alloc_context( buffer, buffer_size, 1, properties, NULL, mlt_write, NULL );
1383 ByteIOContext* io = av_alloc_put_byte( buffer, buffer_size, 1, properties, NULL, mlt_write, NULL );
1388 #if LIBAVFORMAT_VERSION_MAJOR >= 53
1389 oc->flags |= AVFMT_FLAG_CUSTOM_IO;
1391 mlt_properties_set_data( properties, "avio_buffer", buffer, buffer_size, av_free, NULL );
1392 mlt_properties_set_data( properties, "avio_context", io, 0, av_free, NULL );
1393 mlt_events_register( properties, "avformat-write", (mlt_transmitter) write_transmitter );
1398 mlt_log_error( MLT_CONSUMER_SERVICE(consumer), "failed to setup output redirection\n" );
1401 // Open the output file, if needed
1402 else if ( !( fmt->flags & AVFMT_NOFILE ) )
1404 #if LIBAVFORMAT_VERSION_MAJOR >= 53
1405 if ( avio_open( &oc->pb, filename, AVIO_FLAG_WRITE ) < 0 )
1407 if ( url_fopen( &oc->pb, filename, URL_WRONLY ) < 0 )
1410 mlt_log_error( MLT_CONSUMER_SERVICE( consumer ), "Could not open '%s'\n", filename );
1411 mlt_events_fire( properties, "consumer-fatal-error", NULL );
1412 goto on_fatal_error;
1416 // Write the stream header.
1417 if ( mlt_properties_get_int( properties, "running" ) )
1418 #if LIBAVFORMAT_VERSION_INT >= ((53<<16)+(2<<8)+0)
1419 avformat_write_header( oc, NULL );
1421 av_write_header( oc );
1424 #if LIBAVFORMAT_VERSION_INT < ((53<<16)+(2<<8)+0)
1427 mlt_log_error( MLT_CONSUMER_SERVICE( consumer ), "Invalid output format parameters\n" );
1428 mlt_events_fire( properties, "consumer-fatal-error", NULL );
1429 goto on_fatal_error;
1433 // Last check - need at least one stream
1434 if ( !audio_st[0] && !video_st )
1436 mlt_events_fire( properties, "consumer-fatal-error", NULL );
1437 goto on_fatal_error;
1442 converted_avframe = alloc_picture( video_st->codec->pix_fmt, width, height );
1444 #if LIBAVCODEC_VERSION_MAJOR >= 55
1445 // Allocate audio AVFrame
1448 audio_avframe = avcodec_alloc_frame();
1449 if ( audio_avframe ) {
1450 AVCodecContext *c = audio_st[0]->codec;
1451 audio_avframe->format = c->sample_fmt;
1452 audio_avframe->nb_samples = audio_input_nb_samples;
1453 audio_avframe->channel_layout = c->channel_layout;
1455 mlt_log_error( MLT_CONSUMER_SERVICE(consumer), "failed to allocate audio AVFrame\n" );
1456 mlt_events_fire( properties, "consumer-fatal-error", NULL );
1457 goto on_fatal_error;
1462 // Get the starting time (can ignore the times above)
1463 gettimeofday( &ante, NULL );
1465 // Loop while running
1466 while( mlt_properties_get_int( properties, "running" ) &&
1467 ( !terminated || ( video_st && mlt_deque_count( queue ) ) ) )
1470 frame = mlt_consumer_rt_frame( consumer );
1472 // Check that we have a frame to work with
1473 if ( frame != NULL )
1475 // Increment frames dispatched
1478 // Default audio args
1479 frame_properties = MLT_FRAME_PROPERTIES( frame );
1481 // Check for the terminated condition
1482 terminated = terminate_on_pause && mlt_properties_get_double( frame_properties, "_speed" ) == 0.0;
1484 // Get audio and append to the fifo
1485 if ( !terminated && audio_st[0] )
1487 samples = mlt_sample_calculator( fps, frequency, count ++ );
1488 channels = total_channels;
1489 mlt_frame_get_audio( frame, &pcm, &aud_fmt, &frequency, &channels, &samples );
1491 // Save the audio channel remap properties for later
1492 mlt_properties_pass( frame_meta_properties, frame_properties, "meta.map.audio." );
1494 // Create the fifo if we don't have one
1497 fifo = sample_fifo_init( frequency, channels );
1498 mlt_properties_set_data( properties, "sample_fifo", fifo, 0, ( mlt_destructor )sample_fifo_close, NULL );
1502 // Silence if not normal forward speed
1503 if ( mlt_properties_get_double( frame_properties, "_speed" ) != 1.0 )
1504 memset( pcm, 0, samples * channels * sample_bytes );
1506 // Append the samples
1507 sample_fifo_append( fifo, pcm, samples * channels * sample_bytes );
1508 total_time += ( samples * 1000000 ) / frequency;
1511 mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
1515 if ( !terminated && video_st )
1516 mlt_deque_push_back( queue, frame );
1518 mlt_frame_close( frame );
1522 // While we have stuff to process, process...
1525 // Write interleaved audio and video frames
1526 if ( !video_st || ( video_st && audio_st[0] && audio_pts < video_pts ) )
1529 if ( ( video_st && terminated ) || ( channels * audio_input_nb_samples ) < sample_fifo_used( fifo ) / sample_bytes )
1531 int j = 0; // channel offset into interleaved source buffer
1532 int n = FFMIN( FFMIN( channels * audio_input_nb_samples, sample_fifo_used( fifo ) / sample_bytes ), AUDIO_ENCODE_BUFFER_SIZE );
1534 // Get the audio samples
1537 sample_fifo_fetch( fifo, audio_buf_1, n * sample_bytes );
1539 else if ( audio_codec_id == AV_CODEC_ID_VORBIS && terminated )
1541 // This prevents an infinite loop when some versions of vorbis do not
1542 // increment pts when encoding silence.
1543 audio_pts = video_pts;
1548 memset( audio_buf_1, 0, AUDIO_ENCODE_BUFFER_SIZE );
1550 samples = n / channels;
1552 // For each output stream
1553 for ( i = 0; i < MAX_AUDIO_STREAMS && audio_st[i] && j < total_channels; i++ )
1555 AVStream *stream = audio_st[i];
1556 AVCodecContext *codec = stream->codec;
1559 av_init_packet( &pkt );
1560 pkt.data = audio_outbuf;
1561 pkt.size = audio_outbuf_size;
1563 // Optimized for single track and no channel remap
1564 if ( !audio_st[1] && !mlt_properties_count( frame_meta_properties ) )
1566 void* p = audio_buf_1;
1567 #if LIBAVUTIL_VERSION_INT >= ((51<<16)+(17<<8)+0)
1568 if ( codec->sample_fmt == AV_SAMPLE_FMT_FLTP )
1569 p = interleaved_to_planar( samples, channels, p, sizeof( float ) );
1570 else if ( codec->sample_fmt == AV_SAMPLE_FMT_S16P )
1571 p = interleaved_to_planar( samples, channels, p, sizeof( int16_t ) );
1572 else if ( codec->sample_fmt == AV_SAMPLE_FMT_S32P )
1573 p = interleaved_to_planar( samples, channels, p, sizeof( int32_t ) );
1574 else if ( codec->sample_fmt == AV_SAMPLE_FMT_U8P )
1575 p = interleaved_to_planar( samples, channels, p, sizeof( uint8_t ) );
1577 #if LIBAVCODEC_VERSION_MAJOR >= 55
1578 audio_avframe->nb_samples = samples;
1579 avcodec_fill_audio_frame( audio_avframe, codec->channels, codec->sample_fmt,
1580 (const uint8_t*) p, AUDIO_ENCODE_BUFFER_SIZE, 0 );
1582 int ret = avcodec_encode_audio2( codec, &pkt, audio_avframe, &got_packet );
1585 else if ( !got_packet )
1588 codec->frame_size = samples;
1589 pkt.size = avcodec_encode_audio( codec, audio_outbuf, audio_outbuf_size, p );
1590 pkt.pts = codec->coded_frame? codec->coded_frame->pts : AV_NOPTS_VALUE;
1591 pkt.flags |= PKT_FLAG_KEY;
1594 #if LIBAVUTIL_VERSION_INT >= ((51<<16)+(17<<8)+0)
1595 if ( p != audio_buf_1 )
1596 mlt_pool_release( p );
1601 // Extract the audio channels according to channel mapping
1602 int dest_offset = 0; // channel offset into interleaved dest buffer
1604 // Get the number of channels for this stream
1605 sprintf( key, "channels.%d", i );
1606 int current_channels = mlt_properties_get_int( properties, key );
1608 // Clear the destination audio buffer.
1610 audio_buf_2 = av_mallocz( AUDIO_ENCODE_BUFFER_SIZE );
1612 memset( audio_buf_2, 0, AUDIO_ENCODE_BUFFER_SIZE );
1614 // For each output channel
1615 while ( dest_offset < current_channels && j < total_channels )
1617 int map_start = -1, map_channels = 0;
1618 int source_offset = 0;
1621 // Look for a mapping that starts at j
1622 for ( k = 0; k < (MAX_AUDIO_STREAMS * 2) && map_start != j; k++ )
1624 sprintf( key, "%d.channels", k );
1625 map_channels = mlt_properties_get_int( frame_meta_properties, key );
1626 sprintf( key, "%d.start", k );
1627 if ( mlt_properties_get( frame_meta_properties, key ) )
1628 map_start = mlt_properties_get_int( frame_meta_properties, key );
1629 if ( map_start != j )
1630 source_offset += map_channels;
1634 if ( map_start != j )
1636 map_channels = current_channels;
1640 // Copy samples if source offset valid
1641 if ( source_offset < channels )
1643 // Interleave the audio buffer with the # channels for this stream/mapping.
1644 for ( k = 0; k < map_channels; k++, j++, source_offset++, dest_offset++ )
1646 void *src = audio_buf_1 + source_offset * sample_bytes;
1647 void *dest = audio_buf_2 + dest_offset * sample_bytes;
1648 int s = samples + 1;
1651 memcpy( dest, src, sample_bytes );
1652 dest += current_channels * sample_bytes;
1653 src += channels * sample_bytes;
1657 // Otherwise silence
1660 j += current_channels;
1661 dest_offset += current_channels;
1664 #if LIBAVCODEC_VERSION_MAJOR >= 55
1665 avcodec_fill_audio_frame( audio_avframe, codec->channels, codec->sample_fmt,
1666 (const uint8_t*) audio_buf_2, AUDIO_ENCODE_BUFFER_SIZE, 0 );
1668 int ret = avcodec_encode_audio2( codec, &pkt, audio_avframe, &got_packet );
1671 else if ( !got_packet )
1674 pkt.size = avcodec_encode_audio( codec, audio_outbuf, audio_outbuf_size, (short*) audio_buf_2 );
1675 pkt.pts = codec->coded_frame? codec->coded_frame->pts : AV_NOPTS_VALUE;
1676 pkt.flags |= PKT_FLAG_KEY;
1682 // Write the compressed frame in the media file
1683 if ( pkt.pts != AV_NOPTS_VALUE )
1684 pkt.pts = av_rescale_q( pkt.pts, codec->time_base, stream->time_base );
1685 #if LIBAVCODEC_VERSION_MAJOR >= 55
1686 if ( pkt.dts != AV_NOPTS_VALUE )
1687 pkt.dts = av_rescale_q( pkt.dts, codec->time_base, stream->time_base );
1688 if ( pkt.duration > 0 )
1689 pkt.duration = av_rescale_q( pkt.duration, codec->time_base, stream->time_base );
1691 pkt.stream_index = stream->index;
1692 if ( av_interleaved_write_frame( oc, &pkt ) )
1694 mlt_log_fatal( MLT_CONSUMER_SERVICE( consumer ), "error writing audio frame\n" );
1695 mlt_events_fire( properties, "consumer-fatal-error", NULL );
1696 goto on_fatal_error;
1699 else if ( pkt.size < 0 )
1701 mlt_log_warning( MLT_CONSUMER_SERVICE( consumer ), "error with audio encode %d\n", frame_count );
1704 mlt_log_debug( MLT_CONSUMER_SERVICE( consumer ), " frame_size %d\n", codec->frame_size );
1707 audio_pts = (double)stream->pts.val * av_q2d( stream->time_base );
1716 else if ( video_st )
1719 if ( mlt_deque_count( queue ) )
1724 frame = mlt_deque_pop_front( queue );
1725 frame_properties = MLT_FRAME_PROPERTIES( frame );
1727 c = video_st->codec;
1729 if ( mlt_properties_get_int( frame_properties, "rendered" ) )
1735 mlt_frame_get_image( frame, &image, &img_fmt, &img_width, &img_height, 0 );
1739 // Convert the mlt frame to an AVPicture
1740 for ( i = 0; i < height; i ++ )
1742 p = video_avframe->data[ 0 ] + i * video_avframe->linesize[ 0 ];
1743 memcpy( p, q, width * 2 );
1747 // Do the colour space conversion
1748 int flags = SWS_BICUBIC;
1750 flags |= SWS_CPU_CAPS_MMX;
1753 flags |= SWS_CPU_CAPS_MMX2;
1755 struct SwsContext *context = sws_getContext( width, height, PIX_FMT_YUYV422,
1756 width, height, video_st->codec->pix_fmt, flags, NULL, NULL, NULL);
1757 sws_scale( context, (const uint8_t* const*) video_avframe->data, video_avframe->linesize, 0, height,
1758 converted_avframe->data, converted_avframe->linesize);
1759 sws_freeContext( context );
1761 mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
1763 // Apply the alpha if applicable
1764 if ( video_st->codec->pix_fmt == PIX_FMT_RGB32 )
1766 uint8_t *alpha = mlt_frame_get_alpha_mask( frame );
1769 for ( i = 0; i < height; i ++ )
1771 n = ( width + 7 ) / 8;
1772 p = converted_avframe->data[ 0 ] + i * converted_avframe->linesize[ 0 ] + 3;
1776 case 0: do { *p = *alpha++; p += 4;
1777 case 7: *p = *alpha++; p += 4;
1778 case 6: *p = *alpha++; p += 4;
1779 case 5: *p = *alpha++; p += 4;
1780 case 4: *p = *alpha++; p += 4;
1781 case 3: *p = *alpha++; p += 4;
1782 case 2: *p = *alpha++; p += 4;
1783 case 1: *p = *alpha++; p += 4;
1791 if (oc->oformat->flags & AVFMT_RAWPICTURE)
1793 // raw video case. The API will change slightly in the near future for that
1795 av_init_packet(&pkt);
1797 // Set frame interlace hints
1798 c->coded_frame->interlaced_frame = !mlt_properties_get_int( frame_properties, "progressive" );
1799 c->coded_frame->top_field_first = mlt_properties_get_int( frame_properties, "top_field_first" );
1800 #if LIBAVCODEC_VERSION_INT >= ((53<<16)+(61<<8)+100)
1801 if ( mlt_properties_get_int( frame_properties, "progressive" ) )
1802 c->field_order = AV_FIELD_PROGRESSIVE;
1804 c->field_order = (mlt_properties_get_int( frame_properties, "top_field_first" )) ? AV_FIELD_TT : AV_FIELD_BB;
1806 pkt.flags |= PKT_FLAG_KEY;
1807 pkt.stream_index = video_st->index;
1808 pkt.data = (uint8_t *)converted_avframe;
1809 pkt.size = sizeof(AVPicture);
1811 ret = av_write_frame(oc, &pkt);
1812 video_pts += c->frame_size;
1817 av_init_packet( &pkt );
1818 pkt.data = video_outbuf;
1819 pkt.size = video_outbuf_size;
1822 converted_avframe->quality = c->global_quality;
1824 // Set frame interlace hints
1825 converted_avframe->interlaced_frame = !mlt_properties_get_int( frame_properties, "progressive" );
1826 converted_avframe->top_field_first = mlt_properties_get_int( frame_properties, "top_field_first" );
1827 converted_avframe->pts = frame_count;
1830 #if LIBAVCODEC_VERSION_MAJOR >= 55
1832 ret = avcodec_encode_video2( c, &pkt, converted_avframe, &got_packet );
1835 else if ( !got_packet )
1838 pkt.size = avcodec_encode_video(c, video_outbuf, video_outbuf_size, converted_avframe );
1839 pkt.pts = c->coded_frame? c->coded_frame->pts : AV_NOPTS_VALUE;
1840 if ( c->coded_frame && c->coded_frame->key_frame )
1841 pkt.flags |= PKT_FLAG_KEY;
1844 // If zero size, it means the image was buffered
1847 if ( pkt.pts != AV_NOPTS_VALUE )
1848 pkt.pts = av_rescale_q( pkt.pts, c->time_base, video_st->time_base );
1849 #if LIBAVCODEC_VERSION_MAJOR >= 55
1850 if ( pkt.dts != AV_NOPTS_VALUE )
1851 pkt.dts = av_rescale_q( pkt.dts, c->time_base, video_st->time_base );
1853 pkt.stream_index = video_st->index;
1855 // write the compressed frame in the media file
1856 ret = av_interleaved_write_frame(oc, &pkt);
1857 mlt_log_debug( MLT_CONSUMER_SERVICE( consumer ), " frame_size %d\n", c->frame_size );
1858 video_pts = (double)video_st->pts.val * av_q2d( video_st->time_base );
1860 // Dual pass logging
1861 if ( mlt_properties_get_data( properties, "_logfile", NULL ) && c->stats_out )
1862 fprintf( mlt_properties_get_data( properties, "_logfile", NULL ), "%s", c->stats_out );
1864 else if ( pkt.size < 0 )
1866 mlt_log_warning( MLT_CONSUMER_SERVICE( consumer ), "error with video encode %d\n", frame_count );
1873 mlt_log_fatal( MLT_CONSUMER_SERVICE( consumer ), "error writing video frame\n" );
1874 mlt_events_fire( properties, "consumer-fatal-error", NULL );
1875 goto on_fatal_error;
1877 mlt_frame_close( frame );
1886 mlt_log_debug( MLT_CONSUMER_SERVICE( consumer ), "audio pts %"PRId64" (%f) ", audio_st[0]->pts.val, audio_pts );
1888 mlt_log_debug( MLT_CONSUMER_SERVICE( consumer ), "video pts %"PRId64" (%f) ", video_st->pts.val, video_pts );
1889 mlt_log_debug( MLT_CONSUMER_SERVICE( consumer ), "\n" );
1892 if ( real_time_output == 1 && frames % 2 == 0 )
1894 long passed = time_difference( &ante );
1897 long pending = ( ( ( long )sample_fifo_used( fifo ) / sample_bytes * 1000 ) / frequency ) * 1000;
1900 if ( passed < total_time )
1902 long total = ( total_time - passed );
1903 struct timespec t = { total / 1000000, ( total % 1000000 ) * 1000 };
1904 nanosleep( &t, NULL );
1909 // Flush the encoder buffers
1910 if ( real_time_output <= 0 )
1913 // TODO: flush all audio streams
1914 if ( audio_st[0] && audio_st[0]->codec->frame_size > 1 ) for (;;)
1916 AVCodecContext *c = audio_st[0]->codec;
1918 av_init_packet( &pkt );
1919 pkt.data = audio_outbuf;
1923 ( channels * audio_input_nb_samples < sample_fifo_used( fifo ) / sample_bytes ) )
1925 sample_fifo_fetch( fifo, audio_buf_1, channels * audio_input_nb_samples * sample_bytes );
1926 void* p = audio_buf_1;
1927 #if LIBAVUTIL_VERSION_INT >= ((51<<16)+(17<<8)+0)
1928 if ( c->sample_fmt == AV_SAMPLE_FMT_FLTP )
1929 p = interleaved_to_planar( audio_input_nb_samples, channels, p, sizeof( float ) );
1930 else if ( c->sample_fmt == AV_SAMPLE_FMT_S16P )
1931 p = interleaved_to_planar( audio_input_nb_samples, channels, p, sizeof( int16_t ) );
1932 else if ( c->sample_fmt == AV_SAMPLE_FMT_S32P )
1933 p = interleaved_to_planar( audio_input_nb_samples, channels, p, sizeof( int32_t ) );
1934 else if ( c->sample_fmt == AV_SAMPLE_FMT_U8P )
1935 p = interleaved_to_planar( audio_input_nb_samples, channels, p, sizeof( uint8_t ) );
1937 #if LIBAVCODEC_VERSION_MAJOR >= 55
1938 pkt.size = audio_outbuf_size;
1939 avcodec_fill_audio_frame( audio_avframe, c->channels, c->sample_fmt,
1940 (const uint8_t*) p, AUDIO_ENCODE_BUFFER_SIZE, 0 );
1942 int ret = avcodec_encode_audio2( c, &pkt, audio_avframe, &got_packet );
1945 else if ( !got_packet )
1948 pkt.size = avcodec_encode_audio( c, audio_outbuf, audio_outbuf_size, p );
1950 #if LIBAVUTIL_VERSION_INT >= ((51<<16)+(17<<8)+0)
1951 if ( p != audio_buf_1 )
1952 mlt_pool_release( p );
1955 if ( pkt.size <= 0 ) {
1956 #if LIBAVCODEC_VERSION_MAJOR >= 55
1957 pkt.size = audio_outbuf_size;
1959 int ret = avcodec_encode_audio2( c, &pkt, NULL, &got_packet );
1962 else if ( !got_packet )
1965 pkt.size = avcodec_encode_audio( c, audio_outbuf, audio_outbuf_size, NULL );
1966 pkt.pts = c->coded_frame? c->coded_frame->pts : AV_NOPTS_VALUE;
1967 pkt.flags |= PKT_FLAG_KEY;
1970 mlt_log_debug( MLT_CONSUMER_SERVICE( consumer ), "flushing audio size %d\n", pkt.size );
1971 if ( pkt.size <= 0 )
1974 // Write the compressed frame in the media file
1975 if ( pkt.pts != AV_NOPTS_VALUE )
1976 pkt.pts = av_rescale_q( pkt.pts, c->time_base, audio_st[0]->time_base );
1977 #if LIBAVCODEC_VERSION_MAJOR >= 55
1978 if ( pkt.dts != AV_NOPTS_VALUE )
1979 pkt.dts = av_rescale_q( pkt.dts, c->time_base, audio_st[0]->time_base );
1980 if ( pkt.duration > 0 )
1981 pkt.duration = av_rescale_q( pkt.duration, c->time_base, audio_st[0]->time_base );
1983 pkt.stream_index = audio_st[0]->index;
1984 if ( av_interleaved_write_frame( oc, &pkt ) != 0 )
1986 mlt_log_fatal( MLT_CONSUMER_SERVICE( consumer ), "error writing flushed audio frame\n" );
1987 mlt_events_fire( properties, "consumer-fatal-error", NULL );
1988 goto on_fatal_error;
1993 if ( video_st && !( oc->oformat->flags & AVFMT_RAWPICTURE ) ) for (;;)
1995 AVCodecContext *c = video_st->codec;
1997 av_init_packet( &pkt );
1998 pkt.data = video_outbuf;
1999 pkt.size = video_outbuf_size;
2002 #if LIBAVCODEC_VERSION_MAJOR >= 55
2004 int ret = avcodec_encode_video2( c, &pkt, NULL, &got_packet );
2007 else if ( !got_packet )
2010 pkt.size = avcodec_encode_video( c, video_outbuf, video_outbuf_size, NULL );
2011 pkt.pts = c->coded_frame? c->coded_frame->pts : AV_NOPTS_VALUE;
2012 if( c->coded_frame && c->coded_frame->key_frame )
2013 pkt.flags |= PKT_FLAG_KEY;
2015 mlt_log_debug( MLT_CONSUMER_SERVICE( consumer ), "flushing video size %d\n", pkt.size );
2016 if ( pkt.size <= 0 )
2019 if ( pkt.pts != AV_NOPTS_VALUE )
2020 pkt.pts = av_rescale_q( pkt.pts, c->time_base, video_st->time_base );
2021 #if LIBAVCODEC_VERSION_MAJOR >= 55
2022 if ( pkt.dts != AV_NOPTS_VALUE )
2023 pkt.dts = av_rescale_q( pkt.dts, c->time_base, video_st->time_base );
2025 pkt.stream_index = video_st->index;
2027 // write the compressed frame in the media file
2028 if ( av_interleaved_write_frame( oc, &pkt ) != 0 )
2030 mlt_log_fatal( MLT_CONSUMER_SERVICE(consumer), "error writing flushed video frame\n" );
2031 mlt_events_fire( properties, "consumer-fatal-error", NULL );
2032 goto on_fatal_error;
2034 // Dual pass logging
2035 if ( mlt_properties_get_data( properties, "_logfile", NULL ) && c->stats_out )
2036 fprintf( mlt_properties_get_data( properties, "_logfile", NULL ), "%s", c->stats_out );
2042 // Write the trailer, if any
2044 av_write_trailer( oc );
2048 close_video(oc, video_st);
2049 for ( i = 0; i < MAX_AUDIO_STREAMS && audio_st[i]; i++ )
2050 close_audio( oc, audio_st[i] );
2053 for ( i = 0; i < oc->nb_streams; i++ )
2054 av_freep( &oc->streams[i] );
2056 // Close the output file
2057 if ( !( fmt->flags & AVFMT_NOFILE ) &&
2058 !mlt_properties_get_int( properties, "redirect" ) )
2060 #if LIBAVFORMAT_VERSION_MAJOR >= 53
2061 if ( oc->pb ) avio_close( oc->pb );
2063 if ( oc->pb ) url_fclose( oc->pb );
2067 // Clean up input and output frames
2068 if ( converted_avframe )
2069 av_free( converted_avframe->data[0] );
2070 av_free( converted_avframe );
2071 av_free( video_avframe->data[0] );
2072 av_free( video_avframe );
2073 av_free( video_outbuf );
2074 av_free( audio_avframe );
2075 av_free( audio_buf_1 );
2076 av_free( audio_buf_2 );
2081 // Just in case we terminated on pause
2082 mlt_consumer_stopped( consumer );
2083 mlt_properties_close( frame_meta_properties );
2085 if ( mlt_properties_get_int( properties, "pass" ) > 1 )
2087 // Remove the dual pass log file
2088 if ( mlt_properties_get( properties, "_logfilename" ) )
2089 remove( mlt_properties_get( properties, "_logfilename" ) );
2091 // Remove the x264 dual pass logs
2092 char *cwd = getcwd( NULL, 0 );
2093 const char *file = "x264_2pass.log";
2094 char *full = malloc( strlen( cwd ) + strlen( file ) + 2 );
2095 sprintf( full, "%s/%s", cwd, file );
2098 file = "x264_2pass.log.temp";
2099 full = malloc( strlen( cwd ) + strlen( file ) + 2 );
2100 sprintf( full, "%s/%s", cwd, file );
2103 file = "x264_2pass.log.mbtree";
2104 full = malloc( strlen( cwd ) + strlen( file ) + 2 );
2105 sprintf( full, "%s/%s", cwd, file );
2109 remove( "x264_2pass.log.temp" );
2112 while ( ( frame = mlt_deque_pop_back( queue ) ) )
2113 mlt_frame_close( frame );
2118 /** Close the consumer.
2121 static void consumer_close( mlt_consumer consumer )
2123 // Stop the consumer
2124 mlt_consumer_stop( consumer );
2127 mlt_consumer_close( consumer );