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>
27 // System header files
36 // avformat header files
43 #if LIBAVUTIL_VERSION_INT < (50<<16)
44 #define PIX_FMT_RGB32 PIX_FMT_RGBA32
45 #define PIX_FMT_YUYV422 PIX_FMT_YUV422
49 // This structure should be extended and made globally available in mlt
61 *sample_fifo, sample_fifo_s;
63 sample_fifo sample_fifo_init( int frequency, int channels )
65 sample_fifo this = calloc( 1, sizeof( sample_fifo_s ) );
66 this->frequency = frequency;
67 this->channels = channels;
71 // sample_fifo_clear and check are temporarily aborted (not working as intended)
73 void sample_fifo_clear( sample_fifo this, double time )
75 int words = ( float )( time - this->time ) * this->frequency * this->channels;
76 if ( ( int )( ( float )time * 100 ) < ( int )( ( float )this->time * 100 ) && this->used > words && words > 0 )
78 memmove( this->buffer, &this->buffer[ words ], ( this->used - words ) * sizeof( int16_t ) );
82 else if ( ( int )( ( float )time * 100 ) != ( int )( ( float )this->time * 100 ) )
89 void sample_fifo_check( sample_fifo this, double time )
91 if ( this->used == 0 )
93 if ( ( int )( ( float )time * 100 ) < ( int )( ( float )this->time * 100 ) )
98 void sample_fifo_append( sample_fifo this, int16_t *samples, int count )
100 if ( ( this->size - this->used ) < count )
102 this->size += count * 5;
103 this->buffer = realloc( this->buffer, this->size * sizeof( int16_t ) );
106 memcpy( &this->buffer[ this->used ], samples, count * sizeof( int16_t ) );
110 int sample_fifo_used( sample_fifo this )
115 int sample_fifo_fetch( sample_fifo this, int16_t *samples, int count )
117 if ( count > this->used )
120 memcpy( samples, this->buffer, count * sizeof( int16_t ) );
122 memmove( this->buffer, &this->buffer[ count ], this->used * sizeof( int16_t ) );
124 this->time += ( double )count / this->channels / this->frequency;
129 void sample_fifo_close( sample_fifo this )
131 free( this->buffer );
135 // Forward references.
136 static int consumer_start( mlt_consumer this );
137 static int consumer_stop( mlt_consumer this );
138 static int consumer_is_stopped( mlt_consumer this );
139 static void *consumer_thread( void *arg );
140 static void consumer_close( mlt_consumer this );
142 /** Initialise the dv consumer.
145 mlt_consumer consumer_avformat_init( mlt_profile profile, char *arg )
147 // Allocate the consumer
148 mlt_consumer this = mlt_consumer_new( profile );
150 // If memory allocated and initialises without error
153 // Get properties from the consumer
154 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
156 // Assign close callback
157 this->close = consumer_close;
159 // Interpret the argument
161 mlt_properties_set( properties, "target", arg );
163 // sample and frame queue
164 mlt_properties_set_data( properties, "frame_queue", mlt_deque_init( ), 0, ( mlt_destructor )mlt_deque_close, NULL );
166 // Audio options not fully handled by AVOptions
167 #define QSCALE_NONE (-99999)
168 mlt_properties_set_int( properties, "aq", QSCALE_NONE );
170 // Video options not fully handled by AVOptions
171 mlt_properties_set_int( properties, "dc", 8 );
173 // Muxer options not fully handled by AVOptions
174 mlt_properties_set_double( properties, "muxdelay", 0.7 );
175 mlt_properties_set_double( properties, "muxpreload", 0.5 );
177 // Ensure termination at end of the stream
178 mlt_properties_set_int( properties, "terminate_on_pause", 1 );
180 // Default to separate processing threads for producer and consumer with no frame dropping!
181 mlt_properties_set_int( properties, "real_time", -1 );
182 mlt_properties_set_int( properties, "prefill", 1 );
184 // Set up start/stop/terminated callbacks
185 this->start = consumer_start;
186 this->stop = consumer_stop;
187 this->is_stopped = consumer_is_stopped;
194 /** Start the consumer.
197 static int consumer_start( mlt_consumer this )
199 // Get the properties
200 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
203 // Report information about available muxers and codecs as YAML Tiny
204 char *s = mlt_properties_get( properties, "f" );
205 if ( s && strcmp( s, "list" ) == 0 )
207 fprintf( stderr, "---\nformats:\n" );
208 AVOutputFormat *format = NULL;
209 while ( ( format = av_oformat_next( format ) ) )
210 fprintf( stderr, " - %s\n", format->name );
211 fprintf( stderr, "...\n" );
214 s = mlt_properties_get( properties, "acodec" );
215 if ( s && strcmp( s, "list" ) == 0 )
217 fprintf( stderr, "---\naudio_codecs:\n" );
218 AVCodec *codec = NULL;
219 while ( ( codec = av_codec_next( codec ) ) )
220 if ( codec->encode && codec->type == CODEC_TYPE_AUDIO )
221 fprintf( stderr, " - %s\n", codec->name );
222 fprintf( stderr, "...\n" );
225 s = mlt_properties_get( properties, "vcodec" );
226 if ( s && strcmp( s, "list" ) == 0 )
228 fprintf( stderr, "---\nvideo_codecs:\n" );
229 AVCodec *codec = NULL;
230 while ( ( codec = av_codec_next( codec ) ) )
231 if ( codec->encode && codec->type == CODEC_TYPE_VIDEO )
232 fprintf( stderr, " - %s\n", codec->name );
233 fprintf( stderr, "...\n" );
237 // Check that we're not already running
238 if ( !error && !mlt_properties_get_int( properties, "running" ) )
241 pthread_t *thread = calloc( 1, sizeof( pthread_t ) );
243 // Get the width and height
244 int width = mlt_properties_get_int( properties, "width" );
245 int height = mlt_properties_get_int( properties, "height" );
247 // Obtain the size property
248 char *size = mlt_properties_get( properties, "s" );
254 if ( sscanf( size, "%dx%d", &tw, &th ) == 2 && tw > 0 && th > 0 )
261 fprintf( stderr, "%s: Invalid size property %s - ignoring.\n", __FILE__, size );
265 // Now ensure we honour the multiple of two requested by libavformat
266 width = ( width / 2 ) * 2;
267 height = ( height / 2 ) * 2;
268 mlt_properties_set_int( properties, "width", width );
269 mlt_properties_set_int( properties, "height", height );
271 // We need to set these on the profile as well because the s property is
272 // an alias to mlt properties that correspond to profile settings.
273 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
276 profile->width = width;
277 profile->height = height;
280 // Handle the ffmpeg command line "-r" property for frame rate
281 if ( mlt_properties_get( properties, "r" ) )
283 double frame_rate = mlt_properties_get_double( properties, "r" );
284 AVRational rational = av_d2q( frame_rate, 255 );
285 mlt_properties_set_int( properties, "frame_rate_num", rational.num );
286 mlt_properties_set_int( properties, "frame_rate_den", rational.den );
289 profile->frame_rate_num = rational.num;
290 profile->frame_rate_den = rational.den;
291 mlt_properties_set_double( properties, "fps", mlt_profile_fps( profile ) );
295 // Apply AVOptions that are synonyms for standard mlt_consumer options
296 if ( mlt_properties_get( properties, "ac" ) )
297 mlt_properties_set_int( properties, "channels", mlt_properties_get_int( properties, "ac" ) );
298 if ( mlt_properties_get( properties, "ar" ) )
299 mlt_properties_set_int( properties, "frequency", mlt_properties_get_int( properties, "ar" ) );
301 // Assign the thread to properties
302 mlt_properties_set_data( properties, "thread", thread, sizeof( pthread_t ), free, NULL );
304 // Set the running state
305 mlt_properties_set_int( properties, "running", 1 );
308 pthread_create( thread, NULL, consumer_thread, this );
313 /** Stop the consumer.
316 static int consumer_stop( mlt_consumer this )
318 // Get the properties
319 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
321 // Check that we're running
322 if ( mlt_properties_get_int( properties, "running" ) )
325 pthread_t *thread = mlt_properties_get_data( properties, "thread", NULL );
328 mlt_properties_set_int( properties, "running", 0 );
330 // Wait for termination
331 pthread_join( *thread, NULL );
337 /** Determine if the consumer is stopped.
340 static int consumer_is_stopped( mlt_consumer this )
342 // Get the properties
343 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
344 return !mlt_properties_get_int( properties, "running" );
347 /** Process properties as AVOptions and apply to AV context obj
350 static void apply_properties( void *obj, mlt_properties properties, int flags )
353 int count = mlt_properties_count( properties );
354 for ( i = 0; i < count; i++ )
356 const char *opt_name = mlt_properties_get_name( properties, i );
357 const AVOption *opt = av_find_opt( obj, opt_name, NULL, flags, flags );
359 #if LIBAVCODEC_VERSION_INT >= ((52<<16)+(7<<8)+0)
360 av_set_string3( obj, opt_name, mlt_properties_get( properties, opt_name), 0, NULL );
361 #elif LIBAVCODEC_VERSION_INT >= ((51<<16)+(59<<8)+0)
362 av_set_string2( obj, opt_name, mlt_properties_get( properties, opt_name), 0 );
364 av_set_string( obj, opt_name, mlt_properties_get( properties, opt_name) );
369 /** Add an audio output stream
372 static AVStream *add_audio_stream( mlt_consumer this, AVFormatContext *oc, int codec_id )
374 // Get the properties
375 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
377 // Create a new stream
378 AVStream *st = av_new_stream( oc, 1 );
380 // If created, then initialise from properties
383 AVCodecContext *c = st->codec;
385 // Establish defaults from AVOptions
386 avcodec_get_context_defaults2( c, CODEC_TYPE_AUDIO );
388 c->codec_id = codec_id;
389 c->codec_type = CODEC_TYPE_AUDIO;
391 // Setup multi-threading
392 int thread_count = mlt_properties_get_int( properties, "threads" );
393 if ( thread_count == 0 && getenv( "MLT_AVFORMAT_THREADS" ) )
394 thread_count = atoi( getenv( "MLT_AVFORMAT_THREADS" ) );
395 if ( thread_count > 1 )
396 avcodec_thread_init( c, thread_count );
398 if (oc->oformat->flags & AVFMT_GLOBALHEADER)
399 c->flags |= CODEC_FLAG_GLOBAL_HEADER;
401 // Allow the user to override the audio fourcc
402 if ( mlt_properties_get( properties, "atag" ) )
405 char *arg = mlt_properties_get( properties, "atag" );
406 int tag = strtol( arg, &tail, 0);
408 tag = arg[ 0 ] + ( arg[ 1 ] << 8 ) + ( arg[ 2 ] << 16 ) + ( arg[ 3 ] << 24 );
412 // Process properties as AVOptions
413 apply_properties( c, properties, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM );
415 int audio_qscale = mlt_properties_get_int( properties, "aq" );
416 if ( audio_qscale > QSCALE_NONE )
418 c->flags |= CODEC_FLAG_QSCALE;
419 c->global_quality = st->quality = FF_QP2LAMBDA * audio_qscale;
422 // Set parameters controlled by MLT
423 c->sample_rate = mlt_properties_get_int( properties, "frequency" );
424 c->channels = mlt_properties_get_int( properties, "channels" );
426 if ( mlt_properties_get( properties, "alang" ) != NULL )
427 strncpy( st->language, mlt_properties_get( properties, "alang" ), sizeof( st->language ) );
431 fprintf( stderr, "%s: Could not allocate a stream for audio\n", __FILE__ );
437 static int open_audio( AVFormatContext *oc, AVStream *st, int audio_outbuf_size )
439 // We will return the audio input size from here
440 int audio_input_frame_size = 0;
443 AVCodecContext *c = st->codec;
446 AVCodec *codec = avcodec_find_encoder( c->codec_id );
448 // Continue if codec found and we can open it
449 if ( codec != NULL && avcodec_open( c, codec ) >= 0 )
451 // ugly hack for PCM codecs (will be removed ASAP with new PCM
452 // support to compute the input frame size in samples
453 if ( c->frame_size <= 1 )
455 audio_input_frame_size = audio_outbuf_size / c->channels;
456 switch(st->codec->codec_id)
458 case CODEC_ID_PCM_S16LE:
459 case CODEC_ID_PCM_S16BE:
460 case CODEC_ID_PCM_U16LE:
461 case CODEC_ID_PCM_U16BE:
462 audio_input_frame_size >>= 1;
470 audio_input_frame_size = c->frame_size;
473 // Some formats want stream headers to be seperate (hmm)
474 if( !strcmp( oc->oformat->name, "mp4" ) ||
475 !strcmp( oc->oformat->name, "mov" ) ||
476 !strcmp( oc->oformat->name, "3gp" ) )
477 c->flags |= CODEC_FLAG_GLOBAL_HEADER;
481 fprintf( stderr, "%s: Unable to encode audio - disabling audio output.\n", __FILE__ );
484 return audio_input_frame_size;
487 static void close_audio( AVFormatContext *oc, AVStream *st )
489 avcodec_close( st->codec );
492 /** Add a video output stream
495 static AVStream *add_video_stream( mlt_consumer this, AVFormatContext *oc, int codec_id )
497 // Get the properties
498 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
500 // Create a new stream
501 AVStream *st = av_new_stream( oc, 0 );
505 char *pix_fmt = mlt_properties_get( properties, "pix_fmt" );
506 AVCodecContext *c = st->codec;
508 // Establish defaults from AVOptions
509 avcodec_get_context_defaults2( c, CODEC_TYPE_VIDEO );
511 c->codec_id = codec_id;
512 c->codec_type = CODEC_TYPE_VIDEO;
514 // Setup multi-threading
515 int thread_count = mlt_properties_get_int( properties, "threads" );
516 if ( thread_count == 0 && getenv( "MLT_AVFORMAT_THREADS" ) )
517 thread_count = atoi( getenv( "MLT_AVFORMAT_THREADS" ) );
518 if ( thread_count > 1 )
519 avcodec_thread_init( c, thread_count );
521 // Process properties as AVOptions
522 apply_properties( c, properties, AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM );
524 // Set options controlled by MLT
525 c->width = mlt_properties_get_int( properties, "width" );
526 c->height = mlt_properties_get_int( properties, "height" );
527 c->time_base.num = mlt_properties_get_int( properties, "frame_rate_den" );
528 c->time_base.den = mlt_properties_get_int( properties, "frame_rate_num" );
529 if ( st->time_base.den == 0 )
530 st->time_base = c->time_base;
531 c->pix_fmt = pix_fmt ? avcodec_get_pix_fmt( pix_fmt ) : PIX_FMT_YUV420P;
533 if ( mlt_properties_get( properties, "aspect" ) )
535 // "-aspect" on ffmpeg command line is display aspect ratio
536 double ar = mlt_properties_get_double( properties, "aspect" );
537 AVRational rational = av_d2q( ar, 255 );
539 // Update the profile and properties as well since this is an alias
540 // for mlt properties that correspond to profile settings
541 mlt_properties_set_int( properties, "display_aspect_num", rational.num );
542 mlt_properties_set_int( properties, "display_aspect_den", rational.den );
543 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
546 profile->display_aspect_num = rational.num;
547 profile->display_aspect_den = rational.den;
548 mlt_properties_set_double( properties, "display_ratio", mlt_profile_dar( profile ) );
551 // Now compute the sample aspect ratio
552 rational = av_d2q( ar * c->height / c->width, 255 );
553 c->sample_aspect_ratio = rational;
554 // Update the profile and properties as well since this is an alias
555 // for mlt properties that correspond to profile settings
556 mlt_properties_set_int( properties, "sample_aspect_num", rational.num );
557 mlt_properties_set_int( properties, "sample_aspect_den", rational.den );
560 profile->sample_aspect_num = rational.num;
561 profile->sample_aspect_den = rational.den;
562 mlt_properties_set_double( properties, "aspect_ratio", mlt_profile_sar( profile ) );
567 c->sample_aspect_ratio.num = mlt_properties_get_int( properties, "sample_aspect_num" );
568 c->sample_aspect_ratio.den = mlt_properties_get_int( properties, "sample_aspect_den" );
570 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(21<<8)+0)
571 st->sample_aspect_ratio = c->sample_aspect_ratio;
574 if ( mlt_properties_get_double( properties, "qscale" ) > 0 )
576 c->flags |= CODEC_FLAG_QSCALE;
577 st->quality = FF_QP2LAMBDA * mlt_properties_get_double( properties, "qscale" );
580 // Allow the user to override the video fourcc
581 if ( mlt_properties_get( properties, "vtag" ) )
584 const char *arg = mlt_properties_get( properties, "vtag" );
585 int tag = strtol( arg, &tail, 0);
587 tag = arg[ 0 ] + ( arg[ 1 ] << 8 ) + ( arg[ 2 ] << 16 ) + ( arg[ 3 ] << 24 );
591 // Some formats want stream headers to be seperate
592 if ( oc->oformat->flags & AVFMT_GLOBALHEADER )
593 c->flags |= CODEC_FLAG_GLOBAL_HEADER;
595 // Translate these standard mlt consumer properties to ffmpeg
596 if ( mlt_properties_get_int( properties, "progressive" ) == 0 &&
597 mlt_properties_get_int( properties, "deinterlace" ) == 0 )
599 if ( ! mlt_properties_get( properties, "ildct" ) || mlt_properties_get_int( properties, "ildct" ) )
600 c->flags |= CODEC_FLAG_INTERLACED_DCT;
601 if ( ! mlt_properties_get( properties, "ilme" ) || mlt_properties_get_int( properties, "ilme" ) )
602 c->flags |= CODEC_FLAG_INTERLACED_ME;
605 // parse the ratecontrol override string
607 char *rc_override = mlt_properties_get( properties, "rc_override" );
608 for ( i = 0; rc_override; i++ )
611 int e = sscanf( rc_override, "%d,%d,%d", &start, &end, &q );
613 fprintf( stderr, "%s: Error parsing rc_override\n", __FILE__ );
614 c->rc_override = av_realloc( c->rc_override, sizeof( RcOverride ) * ( i + 1 ) );
615 c->rc_override[i].start_frame = start;
616 c->rc_override[i].end_frame = end;
619 c->rc_override[i].qscale = q;
620 c->rc_override[i].quality_factor = 1.0;
624 c->rc_override[i].qscale = 0;
625 c->rc_override[i].quality_factor = -q / 100.0;
627 rc_override = strchr( rc_override, '/' );
631 c->rc_override_count = i;
632 if ( !c->rc_initial_buffer_occupancy )
633 c->rc_initial_buffer_occupancy = c->rc_buffer_size * 3/4;
634 c->intra_dc_precision = mlt_properties_get_int( properties, "dc" ) - 8;
637 i = mlt_properties_get_int( properties, "pass" );
639 c->flags |= CODEC_FLAG_PASS1;
641 c->flags |= CODEC_FLAG_PASS2;
642 if ( codec_id != CODEC_ID_H264 && ( c->flags & ( CODEC_FLAG_PASS1 | CODEC_FLAG_PASS2 ) ) )
644 char logfilename[1024];
649 snprintf( logfilename, sizeof(logfilename), "%s_2pass.log",
650 mlt_properties_get( properties, "passlogfile" ) ? mlt_properties_get( properties, "passlogfile" ) : mlt_properties_get( properties, "target" ) );
651 if ( c->flags & CODEC_FLAG_PASS1 )
653 f = fopen( logfilename, "w" );
655 perror( logfilename );
657 mlt_properties_set_data( properties, "_logfile", f, 0, ( mlt_destructor )fclose, NULL );
661 /* read the log file */
662 f = fopen( logfilename, "r" );
669 mlt_properties_set( properties, "_logfilename", logfilename );
670 fseek( f, 0, SEEK_END );
672 fseek( f, 0, SEEK_SET );
673 logbuffer = av_malloc( size + 1 );
675 fprintf( stderr, "%s: Could not allocate log buffer\n", __FILE__ );
678 size = fread( logbuffer, 1, size, f );
680 logbuffer[size] = '\0';
681 c->stats_in = logbuffer;
682 mlt_properties_set_data( properties, "_logbuffer", logbuffer, 0, ( mlt_destructor )av_free, NULL );
690 fprintf( stderr, "%s: Could not allocate a stream for video\n", __FILE__ );
696 static AVFrame *alloc_picture( int pix_fmt, int width, int height )
699 AVFrame *picture = avcodec_alloc_frame();
701 // Determine size of the
702 int size = avpicture_get_size(pix_fmt, width, height);
704 // Allocate the picture buf
705 uint8_t *picture_buf = av_malloc(size);
707 // If we have both, then fill the image
708 if ( picture != NULL && picture_buf != NULL )
710 // Fill the frame with the allocated buffer
711 avpicture_fill( (AVPicture *)picture, picture_buf, pix_fmt, width, height);
715 // Something failed - clean up what we can
717 av_free( picture_buf );
724 static int open_video(AVFormatContext *oc, AVStream *st)
727 AVCodecContext *video_enc = st->codec;
729 // find the video encoder
730 AVCodec *codec = avcodec_find_encoder( video_enc->codec_id );
732 if( codec && codec->pix_fmts )
734 const enum PixelFormat *p = codec->pix_fmts;
737 if( *p == video_enc->pix_fmt )
741 video_enc->pix_fmt = codec->pix_fmts[ 0 ];
744 // Open the codec safely
745 return codec != NULL && avcodec_open( video_enc, codec ) >= 0;
748 void close_video(AVFormatContext *oc, AVStream *st)
750 avcodec_close(st->codec);
753 static inline long time_difference( struct timeval *time1 )
755 struct timeval time2;
756 gettimeofday( &time2, NULL );
757 return time2.tv_sec * 1000000 + time2.tv_usec - time1->tv_sec * 1000000 - time1->tv_usec;
760 /** The main thread - the argument is simply the consumer.
763 static void *consumer_thread( void *arg )
765 // Map the argument to the object
766 mlt_consumer this = arg;
768 // Get the properties
769 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
771 // Get the terminate on pause property
772 int terminate_on_pause = mlt_properties_get_int( properties, "terminate_on_pause" );
775 // Determine if feed is slow (for realtime stuff)
776 int real_time_output = mlt_properties_get_int( properties, "real_time" );
781 // Get the frame rate
782 double fps = mlt_properties_get_double( properties, "fps" );
784 // Get width and height
785 int width = mlt_properties_get_int( properties, "width" );
786 int height = mlt_properties_get_int( properties, "height" );
787 int img_width = width;
788 int img_height = height;
790 // Get default audio properties
791 mlt_audio_format aud_fmt = mlt_audio_pcm;
792 int channels = mlt_properties_get_int( properties, "channels" );
793 int frequency = mlt_properties_get_int( properties, "frequency" );
797 // AVFormat audio buffer and frame size
798 int audio_outbuf_size = 10000;
799 uint8_t *audio_outbuf = av_malloc( audio_outbuf_size );
800 int audio_input_frame_size = 0;
802 // AVFormat video buffer and frame count
804 int video_outbuf_size = ( 1024 * 1024 );
805 uint8_t *video_outbuf = av_malloc( video_outbuf_size );
807 // Used for the frame properties
808 mlt_frame frame = NULL;
809 mlt_properties frame_properties = NULL;
812 mlt_deque queue = mlt_properties_get_data( properties, "frame_queue", NULL );
813 sample_fifo fifo = mlt_properties_get_data( properties, "sample_fifo", NULL );
815 // Need two av pictures for converting
816 AVFrame *output = NULL;
817 AVFrame *input = alloc_picture( PIX_FMT_YUYV422, width, height );
819 // For receiving images from an mlt_frame
821 mlt_image_format img_fmt = mlt_image_yuv422;
823 // For receiving audio samples back from the fifo
824 int16_t *buffer = av_malloc( 48000 * 2 );
827 // Allocate the context
828 AVFormatContext *oc = av_alloc_format_context( );
831 AVStream *audio_st = NULL;
832 AVStream *video_st = NULL;
835 double audio_pts = 0;
836 double video_pts = 0;
843 long int total_time = 0;
845 // Determine the format
846 AVOutputFormat *fmt = NULL;
847 const char *filename = mlt_properties_get( properties, "target" );
848 char *format = mlt_properties_get( properties, "f" );
849 char *vcodec = mlt_properties_get( properties, "vcodec" );
850 char *acodec = mlt_properties_get( properties, "acodec" );
852 // Used to store and override codec ids
856 // Check for user selected format first
857 if ( format != NULL )
858 fmt = guess_format( format, NULL, NULL );
860 // Otherwise check on the filename
861 if ( fmt == NULL && filename != NULL )
862 fmt = guess_format( NULL, filename, NULL );
864 // Otherwise default to mpeg
866 fmt = guess_format( "mpeg", NULL, NULL );
868 // We need a filename - default to stdout?
869 if ( filename == NULL || !strcmp( filename, "" ) )
872 // Get the codec ids selected
873 audio_codec_id = fmt->audio_codec;
874 video_codec_id = fmt->video_codec;
876 // Check for audio codec overides
877 if ( ( acodec && strcmp( "acodec", "none" ) == 0 ) || mlt_properties_get_int( properties, "an" ) )
878 audio_codec_id = CODEC_ID_NONE;
881 AVCodec *p = avcodec_find_encoder_by_name( acodec );
883 audio_codec_id = p->id;
885 fprintf( stderr, "%s: audio codec %s unrecognised - ignoring\n", __FILE__, acodec );
888 // Check for video codec overides
889 if ( ( vcodec && strcmp( "vcodec", "none" ) == 0 ) || mlt_properties_get_int( properties, "vn" ) )
890 video_codec_id = CODEC_ID_NONE;
893 AVCodec *p = avcodec_find_encoder_by_name( vcodec );
895 video_codec_id = p->id;
897 fprintf( stderr, "%s: video codec %s unrecognised - ignoring\n", __FILE__, vcodec );
904 tmp = mlt_properties_get( properties, "meta.attr.title.markup");
905 if (tmp != NULL) snprintf( oc->title, sizeof(oc->title), "%s", tmp );
907 tmp = mlt_properties_get( properties, "meta.attr.comment.markup");
908 if (tmp != NULL) snprintf( oc->comment, sizeof(oc->comment), "%s", tmp );
910 tmp = mlt_properties_get( properties, "meta.attr.author.markup");
911 if (tmp != NULL) snprintf( oc->author, sizeof(oc->author), "%s", tmp );
913 tmp = mlt_properties_get( properties, "meta.attr.copyright.markup");
914 if (tmp != NULL) snprintf( oc->copyright, sizeof(oc->copyright), "%s", tmp );
916 tmp = mlt_properties_get( properties, "meta.attr.album.markup");
917 if (tmp != NULL) snprintf( oc->album, sizeof(oc->album), "%s", tmp );
919 metavalue = mlt_properties_get_int( properties, "meta.attr.year.markup");
920 if (metavalue != 0) oc->year = metavalue;
922 metavalue = mlt_properties_get_int( properties, "meta.attr.track.markup");
923 if (metavalue != 0) oc->track = metavalue;
926 snprintf( oc->filename, sizeof(oc->filename), "%s", filename );
928 // Add audio and video streams
929 if ( video_codec_id != CODEC_ID_NONE )
930 video_st = add_video_stream( this, oc, video_codec_id );
931 if ( audio_codec_id != CODEC_ID_NONE )
932 audio_st = add_audio_stream( this, oc, audio_codec_id );
934 // Set the parameters (even though we have none...)
935 if ( av_set_parameters(oc, NULL) >= 0 )
937 oc->preload = ( int )( mlt_properties_get_double( properties, "muxpreload" ) * AV_TIME_BASE );
938 oc->max_delay= ( int )( mlt_properties_get_double( properties, "muxdelay" ) * AV_TIME_BASE );
940 // Process properties as AVOptions
941 apply_properties( oc, properties, AV_OPT_FLAG_ENCODING_PARAM );
943 if ( video_st && !open_video( oc, video_st ) )
946 audio_input_frame_size = open_audio( oc, audio_st, audio_outbuf_size );
948 // Open the output file, if needed
949 if ( !( fmt->flags & AVFMT_NOFILE ) )
951 if ( url_fopen( &oc->pb, filename, URL_WRONLY ) < 0 )
953 fprintf( stderr, "%s: Could not open '%s'\n", __FILE__, filename );
954 mlt_properties_set_int( properties, "running", 0 );
958 // Write the stream header, if any
959 if ( mlt_properties_get_int( properties, "running" ) )
960 av_write_header( oc );
964 fprintf( stderr, "%s: Invalid output format parameters\n", __FILE__ );
965 mlt_properties_set_int( properties, "running", 0 );
970 output = alloc_picture( video_st->codec->pix_fmt, width, height );
972 // Last check - need at least one stream
973 if ( audio_st == NULL && video_st == NULL )
974 mlt_properties_set_int( properties, "running", 0 );
976 // Get the starting time (can ignore the times above)
977 gettimeofday( &ante, NULL );
979 // Loop while running
980 while( mlt_properties_get_int( properties, "running" ) && !terminated )
983 frame = mlt_consumer_rt_frame( this );
985 // Check that we have a frame to work with
988 // Increment frames despatched
991 // Default audio args
992 frame_properties = MLT_FRAME_PROPERTIES( frame );
994 // Check for the terminated condition
995 terminated = terminate_on_pause && mlt_properties_get_double( frame_properties, "_speed" ) == 0.0;
997 // Get audio and append to the fifo
998 if ( !terminated && audio_st )
1000 samples = mlt_sample_calculator( fps, frequency, count ++ );
1001 mlt_frame_get_audio( frame, &pcm, &aud_fmt, &frequency, &channels, &samples );
1003 // Create the fifo if we don't have one
1006 fifo = sample_fifo_init( frequency, channels );
1007 mlt_properties_set_data( properties, "sample_fifo", fifo, 0, ( mlt_destructor )sample_fifo_close, NULL );
1010 if ( mlt_properties_get_double( frame_properties, "_speed" ) != 1.0 )
1011 memset( pcm, 0, samples * channels * 2 );
1013 // Append the samples
1014 sample_fifo_append( fifo, pcm, samples * channels );
1015 total_time += ( samples * 1000000 ) / frequency;
1019 if ( !terminated && video_st )
1020 mlt_deque_push_back( queue, frame );
1022 mlt_frame_close( frame );
1025 // While we have stuff to process, process...
1029 audio_pts = (double)audio_st->pts.val * audio_st->time_base.num / audio_st->time_base.den;
1034 video_pts = (double)video_st->pts.val * video_st->time_base.num / video_st->time_base.den;
1038 // Write interleaved audio and video frames
1039 if ( !video_st || ( video_st && audio_st && audio_pts < video_pts ) )
1041 if ( channels * audio_input_frame_size < sample_fifo_used( fifo ) )
1045 av_init_packet( &pkt );
1047 c = audio_st->codec;
1049 sample_fifo_fetch( fifo, buffer, channels * audio_input_frame_size );
1051 pkt.size = avcodec_encode_audio( c, audio_outbuf, audio_outbuf_size, buffer );
1052 // Write the compressed frame in the media file
1053 if ( c->coded_frame && c->coded_frame->pts != AV_NOPTS_VALUE )
1054 pkt.pts = av_rescale_q( c->coded_frame->pts, c->time_base, audio_st->time_base );
1055 pkt.flags |= PKT_FLAG_KEY;
1056 pkt.stream_index= audio_st->index;
1057 pkt.data= audio_outbuf;
1060 if ( av_interleaved_write_frame( oc, &pkt ) != 0)
1061 fprintf( stderr, "%s: Error while writing audio frame\n", __FILE__ );
1063 audio_pts += c->frame_size;
1070 else if ( video_st )
1072 if ( mlt_deque_count( queue ) )
1077 frame = mlt_deque_pop_front( queue );
1078 frame_properties = MLT_FRAME_PROPERTIES( frame );
1080 c = video_st->codec;
1082 if ( mlt_properties_get_int( frame_properties, "rendered" ) )
1089 mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
1091 mlt_frame_get_image( frame, &image, &img_fmt, &img_width, &img_height, 0 );
1095 // Convert the mlt frame to an AVPicture
1096 for ( i = 0; i < height; i ++ )
1098 p = input->data[ 0 ] + i * input->linesize[ 0 ];
1107 // Do the colour space conversion
1109 struct SwsContext *context = sws_getContext( width, height, PIX_FMT_YUYV422,
1110 width, height, video_st->codec->pix_fmt, SWS_FAST_BILINEAR, NULL, NULL, NULL);
1111 sws_scale( context, input->data, input->linesize, 0, height,
1112 output->data, output->linesize);
1113 sws_freeContext( context );
1115 img_convert( ( AVPicture * )output, video_st->codec->pix_fmt, ( AVPicture * )input, PIX_FMT_YUYV422, width, height );
1118 // Apply the alpha if applicable
1119 if ( video_st->codec->pix_fmt == PIX_FMT_RGB32 )
1121 uint8_t *alpha = mlt_frame_get_alpha_mask( frame );
1124 for ( i = 0; i < height; i ++ )
1126 n = ( width + 7 ) / 8;
1127 p = output->data[ 0 ] + i * output->linesize[ 0 ];
1135 case 0: do { *p = *alpha++; p += 4;
1136 case 7: *p = *alpha++; p += 4;
1137 case 6: *p = *alpha++; p += 4;
1138 case 5: *p = *alpha++; p += 4;
1139 case 4: *p = *alpha++; p += 4;
1140 case 3: *p = *alpha++; p += 4;
1141 case 2: *p = *alpha++; p += 4;
1142 case 1: *p = *alpha++; p += 4;
1150 if (oc->oformat->flags & AVFMT_RAWPICTURE)
1152 // raw video case. The API will change slightly in the near future for that
1154 av_init_packet(&pkt);
1156 pkt.flags |= PKT_FLAG_KEY;
1157 pkt.stream_index= video_st->index;
1158 pkt.data= (uint8_t *)output;
1159 pkt.size= sizeof(AVPicture);
1161 ret = av_write_frame(oc, &pkt);
1162 video_pts += c->frame_size;
1167 output->quality = video_st->quality;
1169 // Set frame interlace hints
1170 output->interlaced_frame = !mlt_properties_get_int( frame_properties, "progressive" );
1171 output->top_field_first = mlt_properties_get_int( frame_properties, "top_field_first" );
1174 out_size = avcodec_encode_video(c, video_outbuf, video_outbuf_size, output );
1176 // If zero size, it means the image was buffered
1180 av_init_packet( &pkt );
1182 if ( c->coded_frame && c->coded_frame->pts != AV_NOPTS_VALUE )
1183 pkt.pts= av_rescale_q( c->coded_frame->pts, c->time_base, video_st->time_base );
1184 if( c->coded_frame && c->coded_frame->key_frame )
1185 pkt.flags |= PKT_FLAG_KEY;
1186 pkt.stream_index= video_st->index;
1187 pkt.data= video_outbuf;
1190 // write the compressed frame in the media file
1191 ret = av_interleaved_write_frame(oc, &pkt);
1192 video_pts += c->frame_size;
1194 // Dual pass logging
1195 if ( mlt_properties_get_data( properties, "_logfile", NULL ) && c->stats_out)
1196 fprintf( mlt_properties_get_data( properties, "_logfile", NULL ), "%s", c->stats_out );
1200 fprintf( stderr, "%s: error with video encode\n", __FILE__ );
1204 mlt_frame_close( frame );
1213 if ( real_time_output == 1 && frames % 12 == 0 )
1215 long passed = time_difference( &ante );
1218 long pending = ( ( ( long )sample_fifo_used( fifo ) * 1000 ) / frequency ) * 1000;
1221 if ( passed < total_time )
1223 long total = ( total_time - passed );
1224 struct timespec t = { total / 1000000, ( total % 1000000 ) * 1000 };
1225 nanosleep( &t, NULL );
1231 if ( ! real_time_output )
1234 if ( audio_st && audio_st->codec->frame_size > 1 ) for (;;)
1236 AVCodecContext *c = audio_st->codec;
1238 av_init_packet( &pkt );
1241 if ( /*( c->capabilities & CODEC_CAP_SMALL_LAST_FRAME ) &&*/
1242 ( channels * audio_input_frame_size < sample_fifo_used( fifo ) ) )
1244 sample_fifo_fetch( fifo, buffer, channels * audio_input_frame_size );
1245 pkt.size = avcodec_encode_audio( c, audio_outbuf, audio_outbuf_size, buffer );
1247 if ( pkt.size <= 0 )
1248 pkt.size = avcodec_encode_audio( c, audio_outbuf, audio_outbuf_size, NULL );
1249 if ( pkt.size <= 0 )
1252 // Write the compressed frame in the media file
1253 if ( c->coded_frame && c->coded_frame->pts != AV_NOPTS_VALUE )
1254 pkt.pts = av_rescale_q( c->coded_frame->pts, c->time_base, audio_st->time_base );
1255 pkt.flags |= PKT_FLAG_KEY;
1256 pkt.stream_index = audio_st->index;
1257 pkt.data = audio_outbuf;
1258 if ( av_interleaved_write_frame( oc, &pkt ) != 0 )
1260 fprintf( stderr, "%s: Error while writing flushed audio frame\n", __FILE__ );
1266 if ( video_st && !( oc->oformat->flags & AVFMT_RAWPICTURE ) ) for (;;)
1268 AVCodecContext *c = video_st->codec;
1270 av_init_packet( &pkt );
1273 pkt.size = avcodec_encode_video( c, video_outbuf, video_outbuf_size, NULL );
1274 if ( pkt.size <= 0 )
1277 if ( c->coded_frame && c->coded_frame->pts != AV_NOPTS_VALUE )
1278 pkt.pts= av_rescale_q( c->coded_frame->pts, c->time_base, video_st->time_base );
1279 if( c->coded_frame && c->coded_frame->key_frame )
1280 pkt.flags |= PKT_FLAG_KEY;
1281 pkt.stream_index = video_st->index;
1282 pkt.data = video_outbuf;
1284 // write the compressed frame in the media file
1285 if ( av_interleaved_write_frame( oc, &pkt ) != 0 )
1287 fprintf( stderr, "%s: Error while writing flushed video frame\n". __FILE__ );
1296 close_video(oc, video_st);
1298 close_audio(oc, audio_st);
1300 // Write the trailer, if any
1301 av_write_trailer(oc);
1304 for(i = 0; i < oc->nb_streams; i++)
1305 av_freep(&oc->streams[i]);
1307 // Close the output file
1308 if (!(fmt->flags & AVFMT_NOFILE))
1309 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(0<<8)+0)
1312 url_fclose(&oc->pb);
1315 // Clean up input and output frames
1317 av_free( output->data[0] );
1319 av_free( input->data[0] );
1321 av_free( video_outbuf );
1327 // Just in case we terminated on pause
1328 mlt_properties_set_int( properties, "running", 0 );
1330 mlt_consumer_stopped( this );
1332 if ( mlt_properties_get_int( properties, "pass" ) == 2 )
1334 // Remove the dual pass log file
1335 if ( mlt_properties_get( properties, "_logfilename" ) )
1336 remove( mlt_properties_get( properties, "_logfilename" ) );
1338 // Remove the x264 dual pass logs
1339 char *cwd = getcwd( NULL, 0 );
1340 const char *file = "x264_2pass.log";
1341 char *full = malloc( strlen( cwd ) + strlen( file ) + 2 );
1342 sprintf( full, "%s/%s", cwd, file );
1345 file = "x264_2pass.log.temp";
1346 full = malloc( strlen( cwd ) + strlen( file ) + 2 );
1347 sprintf( full, "%s/%s", cwd, file );
1351 remove( "x264_2pass.log.temp" );
1357 /** Close the consumer.
1360 static void consumer_close( mlt_consumer this )
1362 // Stop the consumer
1363 mlt_consumer_stop( this );
1366 mlt_consumer_close( this );