]> git.sesse.net Git - mlt/blob - src/modules/avformat/consumer_avformat.c
Fix avformat compile warns on const and 64-bit string formatting.
[mlt] / src / modules / avformat / consumer_avformat.c
1 /*
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
6  *
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.
11  *
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.
16  *
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
20  */
21
22 // mlt Header files
23 #include <framework/mlt_consumer.h>
24 #include <framework/mlt_frame.h>
25 #include <framework/mlt_profile.h>
26 #include <framework/mlt_log.h>
27 #include <framework/mlt_events.h>
28
29 // System header files
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <limits.h>
34 #include <pthread.h>
35 #include <sys/time.h>
36 #include <unistd.h>
37
38 // avformat header files
39 #include <libavformat/avformat.h>
40 #ifdef SWSCALE
41 #include <libswscale/swscale.h>
42 #endif
43 #if LIBAVUTIL_VERSION_INT >= ((50<<16)+(8<<8)+0)
44 #include <libavutil/pixdesc.h>
45 #endif
46
47 #if LIBAVUTIL_VERSION_INT < (50<<16)
48 #define PIX_FMT_RGB32 PIX_FMT_RGBA32
49 #define PIX_FMT_YUYV422 PIX_FMT_YUV422
50 #endif
51
52 #if LIBAVCODEC_VERSION_MAJOR > 52
53 #include <libavutil/opt.h>
54 #define CODEC_TYPE_VIDEO      AVMEDIA_TYPE_VIDEO
55 #define CODEC_TYPE_AUDIO      AVMEDIA_TYPE_AUDIO
56 #define PKT_FLAG_KEY AV_PKT_FLAG_KEY
57 #else
58 #include <libavcodec/opt.h>
59 #endif
60
61 #define MAX_AUDIO_STREAMS (8)
62 #define AUDIO_ENCODE_BUFFER_SIZE (48000 * 2 * MAX_AUDIO_STREAMS)
63 #define AUDIO_BUFFER_SIZE (1024 * 42)
64 #define VIDEO_BUFFER_SIZE (2048 * 1024)
65
66 void avformat_lock( );
67 void avformat_unlock( );
68
69 //
70 // This structure should be extended and made globally available in mlt
71 //
72
73 typedef struct
74 {
75         int16_t *buffer;
76         int size;
77         int used;
78         double time;
79         int frequency;
80         int channels;
81 }
82 *sample_fifo, sample_fifo_s;
83
84 sample_fifo sample_fifo_init( int frequency, int channels )
85 {
86         sample_fifo fifo = calloc( 1, sizeof( sample_fifo_s ) );
87         fifo->frequency = frequency;
88         fifo->channels = channels;
89         return fifo;
90 }
91
92 // sample_fifo_clear and check are temporarily aborted (not working as intended)
93
94 void sample_fifo_clear( sample_fifo fifo, double time )
95 {
96         int words = ( float )( time - fifo->time ) * fifo->frequency * fifo->channels;
97         if ( ( int )( ( float )time * 100 ) < ( int )( ( float )fifo->time * 100 ) && fifo->used > words && words > 0 )
98         {
99                 memmove( fifo->buffer, &fifo->buffer[ words ], ( fifo->used - words ) * sizeof( int16_t ) );
100                 fifo->used -= words;
101                 fifo->time = time;
102         }
103         else if ( ( int )( ( float )time * 100 ) != ( int )( ( float )fifo->time * 100 ) )
104         {
105                 fifo->used = 0;
106                 fifo->time = time;
107         }
108 }
109
110 void sample_fifo_check( sample_fifo fifo, double time )
111 {
112         if ( fifo->used == 0 )
113         {
114                 if ( ( int )( ( float )time * 100 ) < ( int )( ( float )fifo->time * 100 ) )
115                         fifo->time = time;
116         }
117 }
118
119 void sample_fifo_append( sample_fifo fifo, int16_t *samples, int count )
120 {
121         if ( ( fifo->size - fifo->used ) < count )
122         {
123                 fifo->size += count * 5;
124                 fifo->buffer = realloc( fifo->buffer, fifo->size * sizeof( int16_t ) );
125         }
126
127         memcpy( &fifo->buffer[ fifo->used ], samples, count * sizeof( int16_t ) );
128         fifo->used += count;
129 }
130
131 int sample_fifo_used( sample_fifo fifo )
132 {
133         return fifo->used;
134 }
135
136 int sample_fifo_fetch( sample_fifo fifo, int16_t *samples, int count )
137 {
138         if ( count > fifo->used )
139                 count = fifo->used;
140
141         memcpy( samples, fifo->buffer, count * sizeof( int16_t ) );
142         fifo->used -= count;
143         memmove( fifo->buffer, &fifo->buffer[ count ], fifo->used * sizeof( int16_t ) );
144
145         fifo->time += ( double )count / fifo->channels / fifo->frequency;
146
147         return count;
148 }
149
150 void sample_fifo_close( sample_fifo fifo )
151 {
152         free( fifo->buffer );
153         free( fifo );
154 }
155
156 // Forward references.
157 static int consumer_start( mlt_consumer consumer );
158 static int consumer_stop( mlt_consumer consumer );
159 static int consumer_is_stopped( mlt_consumer consumer );
160 static void *consumer_thread( void *arg );
161 static void consumer_close( mlt_consumer consumer );
162
163 /** Initialise the consumer.
164 */
165
166 mlt_consumer consumer_avformat_init( mlt_profile profile, char *arg )
167 {
168         // Allocate the consumer
169         mlt_consumer consumer = mlt_consumer_new( profile );
170
171         // If memory allocated and initialises without error
172         if ( consumer != NULL )
173         {
174                 // Get properties from the consumer
175                 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
176
177                 // Assign close callback
178                 consumer->close = consumer_close;
179
180                 // Interpret the argument
181                 if ( arg != NULL )
182                         mlt_properties_set( properties, "target", arg );
183
184                 // sample and frame queue
185                 mlt_properties_set_data( properties, "frame_queue", mlt_deque_init( ), 0, ( mlt_destructor )mlt_deque_close, NULL );
186
187                 // Audio options not fully handled by AVOptions
188 #define QSCALE_NONE (-99999)
189                 mlt_properties_set_int( properties, "aq", QSCALE_NONE );
190                 
191                 // Video options not fully handled by AVOptions
192                 mlt_properties_set_int( properties, "dc", 8 );
193                 
194                 // Muxer options not fully handled by AVOptions
195                 mlt_properties_set_double( properties, "muxdelay", 0.7 );
196                 mlt_properties_set_double( properties, "muxpreload", 0.5 );
197
198                 // Ensure termination at end of the stream
199                 mlt_properties_set_int( properties, "terminate_on_pause", 1 );
200                 
201                 // Default to separate processing threads for producer and consumer with no frame dropping!
202                 mlt_properties_set_int( properties, "real_time", -1 );
203                 mlt_properties_set_int( properties, "prefill", 1 );
204
205                 // Set up start/stop/terminated callbacks
206                 consumer->start = consumer_start;
207                 consumer->stop = consumer_stop;
208                 consumer->is_stopped = consumer_is_stopped;
209                 
210                 mlt_events_register( properties, "consumer-fatal-error", NULL );
211         }
212
213         // Return consumer
214         return consumer;
215 }
216
217 /** Start the consumer.
218 */
219
220 static int consumer_start( mlt_consumer consumer )
221 {
222         // Get the properties
223         mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
224         int error = 0;
225
226         // Report information about available muxers and codecs as YAML Tiny
227         char *s = mlt_properties_get( properties, "f" );
228         if ( s && strcmp( s, "list" ) == 0 )
229         {
230                 mlt_properties doc = mlt_properties_new();
231                 mlt_properties formats = mlt_properties_new();
232                 char key[20];
233                 AVOutputFormat *format = NULL;
234                 
235                 mlt_properties_set_data( properties, "f", formats, 0, (mlt_destructor) mlt_properties_close, NULL );
236                 mlt_properties_set_data( doc, "formats", formats, 0, NULL, NULL );
237                 while ( ( format = av_oformat_next( format ) ) )
238                 {
239                         snprintf( key, sizeof(key), "%d", mlt_properties_count( formats ) );
240                         mlt_properties_set( formats, key, format->name );
241                 }
242                 fprintf( stderr, "%s", mlt_properties_serialise_yaml( doc ) );
243                 mlt_properties_close( doc );
244                 error = 1;
245         }
246         s = mlt_properties_get( properties, "acodec" );
247         if ( s && strcmp( s, "list" ) == 0 )
248         {
249                 mlt_properties doc = mlt_properties_new();
250                 mlt_properties codecs = mlt_properties_new();
251                 char key[20];
252                 AVCodec *codec = NULL;
253
254                 mlt_properties_set_data( properties, "acodec", codecs, 0, (mlt_destructor) mlt_properties_close, NULL );
255                 mlt_properties_set_data( doc, "audio_codecs", codecs, 0, NULL, NULL );
256                 while ( ( codec = av_codec_next( codec ) ) )
257                         if ( codec->encode && codec->type == CODEC_TYPE_AUDIO )
258                         {
259                                 snprintf( key, sizeof(key), "%d", mlt_properties_count( codecs ) );
260                                 mlt_properties_set( codecs, key, codec->name );
261                         }
262                 fprintf( stderr, "%s", mlt_properties_serialise_yaml( doc ) );
263                 mlt_properties_close( doc );
264                 error = 1;
265         }
266         s = mlt_properties_get( properties, "vcodec" );
267         if ( s && strcmp( s, "list" ) == 0 )
268         {
269                 mlt_properties doc = mlt_properties_new();
270                 mlt_properties codecs = mlt_properties_new();
271                 char key[20];
272                 AVCodec *codec = NULL;
273
274                 mlt_properties_set_data( properties, "vcodec", codecs, 0, (mlt_destructor) mlt_properties_close, NULL );
275                 mlt_properties_set_data( doc, "video_codecs", codecs, 0, NULL, NULL );
276                 while ( ( codec = av_codec_next( codec ) ) )
277                         if ( codec->encode && codec->type == CODEC_TYPE_VIDEO )
278                         {
279                                 snprintf( key, sizeof(key), "%d", mlt_properties_count( codecs ) );
280                                 mlt_properties_set( codecs, key, codec->name );
281                         }
282                 fprintf( stderr, "%s", mlt_properties_serialise_yaml( doc ) );
283                 mlt_properties_close( doc );
284                 error = 1;
285         }
286
287         // Check that we're not already running
288         if ( !error && !mlt_properties_get_int( properties, "running" ) )
289         {
290                 // Allocate a thread
291                 pthread_t *thread = calloc( 1, sizeof( pthread_t ) );
292
293                 // Get the width and height
294                 int width = mlt_properties_get_int( properties, "width" );
295                 int height = mlt_properties_get_int( properties, "height" );
296
297                 // Obtain the size property
298                 char *size = mlt_properties_get( properties, "s" );
299
300                 // Interpret it
301                 if ( size != NULL )
302                 {
303                         int tw, th;
304                         if ( sscanf( size, "%dx%d", &tw, &th ) == 2 && tw > 0 && th > 0 )
305                         {
306                                 width = tw;
307                                 height = th;
308                         }
309                         else
310                         {
311                                 mlt_log_warning( MLT_CONSUMER_SERVICE( consumer ), "Invalid size property %s - ignoring.\n", size );
312                         }
313                 }
314                 
315                 // Now ensure we honour the multiple of two requested by libavformat
316                 width = ( width / 2 ) * 2;
317                 height = ( height / 2 ) * 2;
318                 mlt_properties_set_int( properties, "width", width );
319                 mlt_properties_set_int( properties, "height", height );
320
321                 // We need to set these on the profile as well because the s property is
322                 // an alias to mlt properties that correspond to profile settings.
323                 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( consumer ) );
324                 if ( profile )
325                 {
326                         profile->width = width;
327                         profile->height = height;
328                 }
329
330                 // Handle the ffmpeg command line "-r" property for frame rate
331                 if ( mlt_properties_get( properties, "r" ) )
332                 {
333                         double frame_rate = mlt_properties_get_double( properties, "r" );
334                         AVRational rational = av_d2q( frame_rate, 255 );
335                         mlt_properties_set_int( properties, "frame_rate_num", rational.num );
336                         mlt_properties_set_int( properties, "frame_rate_den", rational.den );
337                         if ( profile )
338                         {
339                                 profile->frame_rate_num = rational.num;
340                                 profile->frame_rate_den = rational.den;
341                                 mlt_properties_set_double( properties, "fps", mlt_profile_fps( profile ) );
342                         }
343                 }
344                 
345                 // Apply AVOptions that are synonyms for standard mlt_consumer options
346                 if ( mlt_properties_get( properties, "ac" ) )
347                         mlt_properties_set_int( properties, "channels", mlt_properties_get_int( properties, "ac" ) );
348                 if ( mlt_properties_get( properties, "ar" ) )
349                         mlt_properties_set_int( properties, "frequency", mlt_properties_get_int( properties, "ar" ) );
350
351                 // Assign the thread to properties
352                 mlt_properties_set_data( properties, "thread", thread, sizeof( pthread_t ), free, NULL );
353
354                 // Set the running state
355                 mlt_properties_set_int( properties, "running", 1 );
356
357                 // Create the thread
358                 pthread_create( thread, NULL, consumer_thread, consumer );
359         }
360         return error;
361 }
362
363 /** Stop the consumer.
364 */
365
366 static int consumer_stop( mlt_consumer consumer )
367 {
368         // Get the properties
369         mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
370
371         // Check that we're running
372         if ( mlt_properties_get_int( properties, "running" ) )
373         {
374                 // Get the thread
375                 pthread_t *thread = mlt_properties_get_data( properties, "thread", NULL );
376
377                 // Stop the thread
378                 mlt_properties_set_int( properties, "running", 0 );
379
380                 // Wait for termination
381                 pthread_join( *thread, NULL );
382         }
383
384         return 0;
385 }
386
387 /** Determine if the consumer is stopped.
388 */
389
390 static int consumer_is_stopped( mlt_consumer consumer )
391 {
392         // Get the properties
393         mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
394         return !mlt_properties_get_int( properties, "running" );
395 }
396
397 /** Process properties as AVOptions and apply to AV context obj
398 */
399
400 static void apply_properties( void *obj, mlt_properties properties, int flags, int alloc )
401 {
402         int i;
403         int count = mlt_properties_count( properties ); 
404         for ( i = 0; i < count; i++ )
405         {
406                 const char *opt_name = mlt_properties_get_name( properties, i );
407                 const AVOption *opt = av_find_opt( obj, opt_name, NULL, flags, flags );
408                 if ( opt != NULL )
409 #if LIBAVCODEC_VERSION_INT >= ((52<<16)+(7<<8)+0)
410                         av_set_string3( obj, opt_name, mlt_properties_get_value( properties, i), alloc, NULL );
411 #elif LIBAVCODEC_VERSION_INT >= ((51<<16)+(59<<8)+0)
412                         av_set_string2( obj, opt_name, mlt_properties_get_value( properties, i), alloc );
413 #else
414                         av_set_string( obj, opt_name, mlt_properties_get_value( properties, i) );
415 #endif
416         }
417 }
418
419 /** Add an audio output stream
420 */
421
422 static AVStream *add_audio_stream( mlt_consumer consumer, AVFormatContext *oc, int codec_id, int channels )
423 {
424         // Get the properties
425         mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
426
427         // Create a new stream
428         AVStream *st = av_new_stream( oc, oc->nb_streams );
429
430         // If created, then initialise from properties
431         if ( st != NULL ) 
432         {
433                 AVCodecContext *c = st->codec;
434
435                 // Establish defaults from AVOptions
436                 avcodec_get_context_defaults2( c, CODEC_TYPE_AUDIO );
437
438                 c->codec_id = codec_id;
439                 c->codec_type = CODEC_TYPE_AUDIO;
440                 c->sample_fmt = SAMPLE_FMT_S16;
441
442 #if 0 // disabled until some audio codecs are multi-threaded
443                 // Setup multi-threading
444                 int thread_count = mlt_properties_get_int( properties, "threads" );
445                 if ( thread_count == 0 && getenv( "MLT_AVFORMAT_THREADS" ) )
446                         thread_count = atoi( getenv( "MLT_AVFORMAT_THREADS" ) );
447                 if ( thread_count > 1 )
448                         c->thread_count = thread_count;
449 #endif
450         
451                 if (oc->oformat->flags & AVFMT_GLOBALHEADER) 
452                         c->flags |= CODEC_FLAG_GLOBAL_HEADER;
453                 
454                 // Allow the user to override the audio fourcc
455                 if ( mlt_properties_get( properties, "atag" ) )
456                 {
457                         char *tail = NULL;
458                         char *arg = mlt_properties_get( properties, "atag" );
459                         int tag = strtol( arg, &tail, 0);
460                         if( !tail || *tail )
461                                 tag = arg[ 0 ] + ( arg[ 1 ] << 8 ) + ( arg[ 2 ] << 16 ) + ( arg[ 3 ] << 24 );
462                         c->codec_tag = tag;
463                 }
464
465                 // Process properties as AVOptions
466                 char *apre = mlt_properties_get( properties, "apre" );
467                 if ( apre )
468                 {
469                         mlt_properties p = mlt_properties_load( apre );
470                         apply_properties( c, p, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM, 1 );
471                         mlt_properties_close( p );
472                 }
473                 apply_properties( c, properties, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM, 0 );
474
475                 int audio_qscale = mlt_properties_get_int( properties, "aq" );
476         if ( audio_qscale > QSCALE_NONE )
477                 {
478                         c->flags |= CODEC_FLAG_QSCALE;
479                         c->global_quality = st->quality = FF_QP2LAMBDA * audio_qscale;
480                 }
481
482                 // Set parameters controlled by MLT
483                 c->sample_rate = mlt_properties_get_int( properties, "frequency" );
484                 c->time_base = ( AVRational ){ 1, c->sample_rate };
485                 c->channels = channels;
486
487                 if ( mlt_properties_get( properties, "alang" ) != NULL )
488 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(43<<8)+0)
489                         av_metadata_set2( &oc->metadata, "language", mlt_properties_get( properties, "alang" ), 0 );
490 #else
491
492                         strncpy( st->language, mlt_properties_get( properties, "alang" ), sizeof( st->language ) );
493 #endif
494         }
495         else
496         {
497                 mlt_log_error( MLT_CONSUMER_SERVICE( consumer ), "Could not allocate a stream for audio\n" );
498         }
499
500         return st;
501 }
502
503 static int open_audio( AVFormatContext *oc, AVStream *st, int audio_outbuf_size, const char *codec_name )
504 {
505         // We will return the audio input size from here
506         int audio_input_frame_size = 0;
507
508         // Get the context
509         AVCodecContext *c = st->codec;
510
511         // Find the encoder
512         AVCodec *codec;
513         if ( codec_name )
514                 codec = avcodec_find_encoder_by_name( codec_name );
515         else
516                 codec = avcodec_find_encoder( c->codec_id );
517
518         avformat_lock();
519         
520         // Continue if codec found and we can open it
521         if ( codec != NULL && avcodec_open( c, codec ) >= 0 )
522         {
523                 // ugly hack for PCM codecs (will be removed ASAP with new PCM
524                 // support to compute the input frame size in samples
525                 if ( c->frame_size <= 1 ) 
526                 {
527                         audio_input_frame_size = audio_outbuf_size / c->channels;
528                         switch(st->codec->codec_id) 
529                         {
530                                 case CODEC_ID_PCM_S16LE:
531                                 case CODEC_ID_PCM_S16BE:
532                                 case CODEC_ID_PCM_U16LE:
533                                 case CODEC_ID_PCM_U16BE:
534                                         audio_input_frame_size >>= 1;
535                                         break;
536                                 default:
537                                         break;
538                         }
539                 } 
540                 else 
541                 {
542                         audio_input_frame_size = c->frame_size;
543                 }
544
545                 // Some formats want stream headers to be seperate (hmm)
546                 if ( !strcmp( oc->oformat->name, "mp4" ) ||
547                          !strcmp( oc->oformat->name, "mov" ) ||
548                          !strcmp( oc->oformat->name, "3gp" ) )
549                         c->flags |= CODEC_FLAG_GLOBAL_HEADER;
550         }
551         else
552         {
553                 mlt_log_warning( NULL, "%s: Unable to encode audio - disabling audio output.\n", __FILE__ );
554         }
555         
556         avformat_unlock();
557
558         return audio_input_frame_size;
559 }
560
561 static void close_audio( AVFormatContext *oc, AVStream *st )
562 {
563         if ( st && st->codec )
564         {
565                 avformat_lock();
566                 avcodec_close( st->codec );
567                 avformat_unlock();
568         }
569 }
570
571 /** Add a video output stream 
572 */
573
574 static AVStream *add_video_stream( mlt_consumer consumer, AVFormatContext *oc, int codec_id )
575 {
576         // Get the properties
577         mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
578
579         // Create a new stream
580         AVStream *st = av_new_stream( oc, oc->nb_streams );
581
582         if ( st != NULL ) 
583         {
584                 char *pix_fmt = mlt_properties_get( properties, "pix_fmt" );
585                 AVCodecContext *c = st->codec;
586
587                 // Establish defaults from AVOptions
588                 avcodec_get_context_defaults2( c, CODEC_TYPE_VIDEO );
589
590                 c->codec_id = codec_id;
591                 c->codec_type = CODEC_TYPE_VIDEO;
592                 
593                 // Setup multi-threading
594                 int thread_count = mlt_properties_get_int( properties, "threads" );
595                 if ( thread_count == 0 && getenv( "MLT_AVFORMAT_THREADS" ) )
596                         thread_count = atoi( getenv( "MLT_AVFORMAT_THREADS" ) );
597                 if ( thread_count > 1 )
598                         c->thread_count = thread_count;
599         
600                 // Process properties as AVOptions
601                 char *vpre = mlt_properties_get( properties, "vpre" );
602                 if ( vpre )
603                 {
604                         mlt_properties p = mlt_properties_load( vpre );
605 #ifdef AVDATADIR
606                         if ( mlt_properties_count( p ) < 1 )
607                         {
608                                 AVCodec *codec = avcodec_find_encoder( c->codec_id );
609                                 if ( codec )
610                                 {
611                                         char *path = malloc( strlen(AVDATADIR) + strlen(codec->name) + strlen(vpre) + strlen(".ffpreset") + 2 );
612                                         strcpy( path, AVDATADIR );
613                                         strcat( path, codec->name );
614                                         strcat( path, "-" );
615                                         strcat( path, vpre );
616                                         strcat( path, ".ffpreset" );
617                                         
618                                         mlt_properties_close( p );
619                                         p = mlt_properties_load( path );
620                                         mlt_properties_debug( p, path, stderr );
621                                         free( path );   
622                                 }
623                         }
624                         else
625                         {
626                                 mlt_properties_debug( p, vpre, stderr );                        
627                         }
628 #endif
629                         apply_properties( c, p, AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM, 1 );
630                         mlt_properties_close( p );
631                 }
632                 int colorspace = mlt_properties_get_int( properties, "colorspace" );
633                 mlt_properties_set( properties, "colorspace", NULL );
634                 apply_properties( c, properties, AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM, 0 );
635                 mlt_properties_set_int( properties, "colorspace", colorspace );
636
637                 // Set options controlled by MLT
638                 c->width = mlt_properties_get_int( properties, "width" );
639                 c->height = mlt_properties_get_int( properties, "height" );
640                 c->time_base.num = mlt_properties_get_int( properties, "frame_rate_den" );
641                 c->time_base.den = mlt_properties_get_int( properties, "frame_rate_num" );
642                 if ( st->time_base.den == 0 )
643                         st->time_base = c->time_base;
644 #if LIBAVUTIL_VERSION_INT >= ((50<<16)+(8<<8)+0)
645                 c->pix_fmt = pix_fmt ? av_get_pix_fmt( pix_fmt ) : PIX_FMT_YUV420P;
646 #else
647                 c->pix_fmt = pix_fmt ? avcodec_get_pix_fmt( pix_fmt ) : PIX_FMT_YUV420P;
648 #endif
649                 
650 #if LIBAVCODEC_VERSION_INT > ((52<<16)+(28<<8)+0)
651                 switch ( colorspace )
652                 {
653                 case 170:
654                         c->colorspace = AVCOL_SPC_SMPTE170M;
655                         break;
656                 case 240:
657                         c->colorspace = AVCOL_SPC_SMPTE240M;
658                         break;
659                 case 470:
660                         c->colorspace = AVCOL_SPC_BT470BG;
661                         break;
662                 case 601:
663                         c->colorspace = ( 576 % c->height ) ? AVCOL_SPC_SMPTE170M : AVCOL_SPC_BT470BG;
664                         break;
665                 case 709:
666                         c->colorspace = AVCOL_SPC_BT709;
667                         break;
668                 }
669 #endif
670
671                 if ( mlt_properties_get( properties, "aspect" ) )
672                 {
673                         // "-aspect" on ffmpeg command line is display aspect ratio
674                         double ar = mlt_properties_get_double( properties, "aspect" );
675                         AVRational rational = av_d2q( ar, 255 );
676
677                         // Update the profile and properties as well since this is an alias 
678                         // for mlt properties that correspond to profile settings
679                         mlt_properties_set_int( properties, "display_aspect_num", rational.num );
680                         mlt_properties_set_int( properties, "display_aspect_den", rational.den );
681                         mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( consumer ) );
682                         if ( profile )
683                         {
684                                 profile->display_aspect_num = rational.num;
685                                 profile->display_aspect_den = rational.den;
686                                 mlt_properties_set_double( properties, "display_ratio", mlt_profile_dar( profile ) );
687                         }
688
689                         // Now compute the sample aspect ratio
690                         rational = av_d2q( ar * c->height / c->width, 255 );
691                         c->sample_aspect_ratio = rational;
692                         // Update the profile and properties as well since this is an alias 
693                         // for mlt properties that correspond to profile settings
694                         mlt_properties_set_int( properties, "sample_aspect_num", rational.num );
695                         mlt_properties_set_int( properties, "sample_aspect_den", rational.den );
696                         if ( profile )
697                         {
698                                 profile->sample_aspect_num = rational.num;
699                                 profile->sample_aspect_den = rational.den;
700                                 mlt_properties_set_double( properties, "aspect_ratio", mlt_profile_sar( profile ) );
701                         }
702                 }
703                 else
704                 {
705                         c->sample_aspect_ratio.num = mlt_properties_get_int( properties, "sample_aspect_num" );
706                         c->sample_aspect_ratio.den = mlt_properties_get_int( properties, "sample_aspect_den" );
707                 }
708 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(21<<8)+0)
709                 st->sample_aspect_ratio = c->sample_aspect_ratio;
710 #endif
711
712                 if ( mlt_properties_get_double( properties, "qscale" ) > 0 )
713                 {
714                         c->flags |= CODEC_FLAG_QSCALE;
715                         st->quality = FF_QP2LAMBDA * mlt_properties_get_double( properties, "qscale" );
716                 }
717
718                 // Allow the user to override the video fourcc
719                 if ( mlt_properties_get( properties, "vtag" ) )
720                 {
721                         char *tail = NULL;
722                         const char *arg = mlt_properties_get( properties, "vtag" );
723                         int tag = strtol( arg, &tail, 0);
724                         if( !tail || *tail )
725                                 tag = arg[ 0 ] + ( arg[ 1 ] << 8 ) + ( arg[ 2 ] << 16 ) + ( arg[ 3 ] << 24 );
726                         c->codec_tag = tag;
727                 }
728
729                 // Some formats want stream headers to be seperate
730                 if ( oc->oformat->flags & AVFMT_GLOBALHEADER ) 
731                         c->flags |= CODEC_FLAG_GLOBAL_HEADER;
732
733                 // Translate these standard mlt consumer properties to ffmpeg
734                 if ( mlt_properties_get_int( properties, "progressive" ) == 0 &&
735                      mlt_properties_get_int( properties, "deinterlace" ) == 0 )
736                 {
737                         if ( ! mlt_properties_get( properties, "ildct" ) || mlt_properties_get_int( properties, "ildct" ) )
738                                 c->flags |= CODEC_FLAG_INTERLACED_DCT;
739                         if ( ! mlt_properties_get( properties, "ilme" ) || mlt_properties_get_int( properties, "ilme" ) )
740                                 c->flags |= CODEC_FLAG_INTERLACED_ME;
741                 }
742                 
743                 // parse the ratecontrol override string
744                 int i;
745                 char *rc_override = mlt_properties_get( properties, "rc_override" );
746                 for ( i = 0; rc_override; i++ )
747                 {
748                         int start, end, q;
749                         int e = sscanf( rc_override, "%d,%d,%d", &start, &end, &q );
750                         if ( e != 3 )
751                                 mlt_log_warning( MLT_CONSUMER_SERVICE( consumer ), "Error parsing rc_override\n" );
752                         c->rc_override = av_realloc( c->rc_override, sizeof( RcOverride ) * ( i + 1 ) );
753                         c->rc_override[i].start_frame = start;
754                         c->rc_override[i].end_frame = end;
755                         if ( q > 0 )
756                         {
757                                 c->rc_override[i].qscale = q;
758                                 c->rc_override[i].quality_factor = 1.0;
759                         }
760                         else
761                         {
762                                 c->rc_override[i].qscale = 0;
763                                 c->rc_override[i].quality_factor = -q / 100.0;
764                         }
765                         rc_override = strchr( rc_override, '/' );
766                         if ( rc_override )
767                                 rc_override++;
768                 }
769                 c->rc_override_count = i;
770                 if ( !c->rc_initial_buffer_occupancy )
771                         c->rc_initial_buffer_occupancy = c->rc_buffer_size * 3/4;
772                 c->intra_dc_precision = mlt_properties_get_int( properties, "dc" ) - 8;
773
774                 // Setup dual-pass
775                 i = mlt_properties_get_int( properties, "pass" );
776                 if ( i == 1 )
777                         c->flags |= CODEC_FLAG_PASS1;
778                 else if ( i == 2 )
779                         c->flags |= CODEC_FLAG_PASS2;
780                 if ( codec_id != CODEC_ID_H264 && ( c->flags & ( CODEC_FLAG_PASS1 | CODEC_FLAG_PASS2 ) ) )
781                 {
782                         char logfilename[1024];
783                         FILE *f;
784                         int size;
785                         char *logbuffer;
786
787                         snprintf( logfilename, sizeof(logfilename), "%s_2pass.log",
788                                 mlt_properties_get( properties, "passlogfile" ) ? mlt_properties_get( properties, "passlogfile" ) : mlt_properties_get( properties, "target" ) );
789                         if ( c->flags & CODEC_FLAG_PASS1 )
790                         {
791                                 f = fopen( logfilename, "w" );
792                                 if ( !f )
793                                         perror( logfilename );
794                                 else
795                                         mlt_properties_set_data( properties, "_logfile", f, 0, ( mlt_destructor )fclose, NULL );
796                         }
797                         else
798                         {
799                                 /* read the log file */
800                                 f = fopen( logfilename, "r" );
801                                 if ( !f )
802                                 {
803                                         perror(logfilename);
804                                 }
805                                 else
806                                 {
807                                         mlt_properties_set( properties, "_logfilename", logfilename );
808                                         fseek( f, 0, SEEK_END );
809                                         size = ftell( f );
810                                         fseek( f, 0, SEEK_SET );
811                                         logbuffer = av_malloc( size + 1 );
812                                         if ( !logbuffer )
813                                                 mlt_log_fatal( MLT_CONSUMER_SERVICE( consumer ), "Could not allocate log buffer\n" );
814                                         else
815                                         {
816                                                 size = fread( logbuffer, 1, size, f );
817                                                 fclose( f );
818                                                 logbuffer[size] = '\0';
819                                                 c->stats_in = logbuffer;
820                                                 mlt_properties_set_data( properties, "_logbuffer", logbuffer, 0, ( mlt_destructor )av_free, NULL );
821                                         }
822                                 }
823                         }
824                 }
825         }
826         else
827         {
828                 mlt_log_error( MLT_CONSUMER_SERVICE( consumer ), "Could not allocate a stream for video\n" );
829         }
830  
831         return st;
832 }
833
834 static AVFrame *alloc_picture( int pix_fmt, int width, int height )
835 {
836         // Allocate a frame
837         AVFrame *picture = avcodec_alloc_frame();
838
839         // Determine size of the 
840         int size = avpicture_get_size(pix_fmt, width, height);
841
842         // Allocate the picture buf
843         uint8_t *picture_buf = av_malloc(size);
844
845         // If we have both, then fill the image
846         if ( picture != NULL && picture_buf != NULL )
847         {
848                 // Fill the frame with the allocated buffer
849                 avpicture_fill( (AVPicture *)picture, picture_buf, pix_fmt, width, height);
850         }
851         else
852         {
853                 // Something failed - clean up what we can
854                 av_free( picture );
855                 av_free( picture_buf );
856                 picture = NULL;
857         }
858
859         return picture;
860 }
861         
862 static int open_video(AVFormatContext *oc, AVStream *st, const char *codec_name)
863 {
864         // Get the codec
865         AVCodecContext *video_enc = st->codec;
866
867         // find the video encoder
868         AVCodec *codec;
869         if ( codec_name )
870                 codec = avcodec_find_encoder_by_name( codec_name );
871         else
872                 codec = avcodec_find_encoder( video_enc->codec_id );
873
874         if( codec && codec->pix_fmts )
875         {
876                 const enum PixelFormat *p = codec->pix_fmts;
877                 for( ; *p!=-1; p++ )
878                 {
879                         if( *p == video_enc->pix_fmt )
880                                 break;
881                 }
882                 if( *p == -1 )
883                         video_enc->pix_fmt = codec->pix_fmts[ 0 ];
884         }
885
886         // Open the codec safely
887         avformat_lock();
888         int result = codec != NULL && avcodec_open( video_enc, codec ) >= 0;
889         avformat_unlock();
890         
891         return result;
892 }
893
894 void close_video(AVFormatContext *oc, AVStream *st)
895 {
896         if ( st && st->codec )
897         {
898                 avformat_lock();
899                 avcodec_close(st->codec);
900                 avformat_unlock();
901         }
902 }
903
904 static inline long time_difference( struct timeval *time1 )
905 {
906         struct timeval time2;
907         gettimeofday( &time2, NULL );
908         return time2.tv_sec * 1000000 + time2.tv_usec - time1->tv_sec * 1000000 - time1->tv_usec;
909 }
910
911 /** The main thread - the argument is simply the consumer.
912 */
913
914 static void *consumer_thread( void *arg )
915 {
916         // Map the argument to the object
917         mlt_consumer consumer = arg;
918
919         // Get the properties
920         mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
921
922         // Get the terminate on pause property
923         int terminate_on_pause = mlt_properties_get_int( properties, "terminate_on_pause" );
924         int terminated = 0;
925
926         // Determine if feed is slow (for realtime stuff)
927         int real_time_output = mlt_properties_get_int( properties, "real_time" );
928
929         // Time structures
930         struct timeval ante;
931
932         // Get the frame rate
933         double fps = mlt_properties_get_double( properties, "fps" );
934
935         // Get width and height
936         int width = mlt_properties_get_int( properties, "width" );
937         int height = mlt_properties_get_int( properties, "height" );
938         int img_width = width;
939         int img_height = height;
940
941         // Get default audio properties
942         mlt_audio_format aud_fmt = mlt_audio_s16;
943         int channels = mlt_properties_get_int( properties, "channels" );
944         int total_channels = channels;
945         int frequency = mlt_properties_get_int( properties, "frequency" );
946         int16_t *pcm = NULL;
947         int samples = 0;
948
949         // AVFormat audio buffer and frame size
950         int audio_outbuf_size = AUDIO_BUFFER_SIZE;
951         uint8_t *audio_outbuf = av_malloc( audio_outbuf_size );
952         int audio_input_frame_size = 0;
953
954         // AVFormat video buffer and frame count
955         int frame_count = 0;
956         int video_outbuf_size = VIDEO_BUFFER_SIZE;
957         uint8_t *video_outbuf = av_malloc( video_outbuf_size );
958
959         // Used for the frame properties
960         mlt_frame frame = NULL;
961         mlt_properties frame_properties = NULL;
962
963         // Get the queues
964         mlt_deque queue = mlt_properties_get_data( properties, "frame_queue", NULL );
965         sample_fifo fifo = mlt_properties_get_data( properties, "sample_fifo", NULL );
966
967         // Need two av pictures for converting
968         AVFrame *output = NULL;
969         AVFrame *input = alloc_picture( PIX_FMT_YUYV422, width, height );
970
971         // For receiving images from an mlt_frame
972         uint8_t *image;
973         mlt_image_format img_fmt = mlt_image_yuv422;
974
975         // For receiving audio samples back from the fifo
976         int16_t *audio_buf_1 = av_malloc( AUDIO_ENCODE_BUFFER_SIZE );
977         int16_t *audio_buf_2 = NULL;
978         int count = 0;
979
980         // Allocate the context
981 #if (LIBAVFORMAT_VERSION_INT >= ((52<<16)+(26<<8)+0))
982         AVFormatContext *oc = avformat_alloc_context( );
983 #else
984         AVFormatContext *oc = av_alloc_format_context( );
985 #endif
986
987         // Streams
988         AVStream *video_st = NULL;
989         AVStream *audio_st[ MAX_AUDIO_STREAMS ];
990
991         // Time stamps
992         double audio_pts = 0;
993         double video_pts = 0;
994
995         // Frames dispatched
996         long int frames = 0;
997         long int total_time = 0;
998
999         // Determine the format
1000         AVOutputFormat *fmt = NULL;
1001         const char *filename = mlt_properties_get( properties, "target" );
1002         char *format = mlt_properties_get( properties, "f" );
1003         char *vcodec = mlt_properties_get( properties, "vcodec" );
1004         char *acodec = mlt_properties_get( properties, "acodec" );
1005         
1006         // Used to store and override codec ids
1007         int audio_codec_id;
1008         int video_codec_id;
1009
1010         // Misc
1011         char key[27];
1012         mlt_properties frame_meta_properties = mlt_properties_new();
1013
1014         // Initialize audio_st
1015         int i = MAX_AUDIO_STREAMS;
1016         while ( i-- )
1017                 audio_st[i] = NULL;
1018
1019         // Check for user selected format first
1020         if ( format != NULL )
1021 #if LIBAVFORMAT_VERSION_INT < ((52<<16)+(45<<8)+0)
1022                 fmt = guess_format( format, NULL, NULL );
1023 #else
1024                 fmt = av_guess_format( format, NULL, NULL );
1025 #endif
1026
1027         // Otherwise check on the filename
1028         if ( fmt == NULL && filename != NULL )
1029 #if LIBAVFORMAT_VERSION_INT < ((52<<16)+(45<<8)+0)
1030                 fmt = guess_format( NULL, filename, NULL );
1031 #else
1032                 fmt = av_guess_format( NULL, filename, NULL );
1033 #endif
1034
1035         // Otherwise default to mpeg
1036         if ( fmt == NULL )
1037 #if LIBAVFORMAT_VERSION_INT < ((52<<16)+(45<<8)+0)
1038                 fmt = guess_format( "mpeg", NULL, NULL );
1039 #else
1040                 fmt = av_guess_format( "mpeg", NULL, NULL );
1041 #endif
1042
1043         // We need a filename - default to stdout?
1044         if ( filename == NULL || !strcmp( filename, "" ) )
1045                 filename = "pipe:";
1046
1047         // Get the codec ids selected
1048         audio_codec_id = fmt->audio_codec;
1049         video_codec_id = fmt->video_codec;
1050
1051         // Check for audio codec overides
1052         if ( ( acodec && strcmp( acodec, "none" ) == 0 ) || mlt_properties_get_int( properties, "an" ) )
1053                 audio_codec_id = CODEC_ID_NONE;
1054         else if ( acodec )
1055         {
1056                 AVCodec *p = avcodec_find_encoder_by_name( acodec );
1057                 if ( p != NULL )
1058                 {
1059                         audio_codec_id = p->id;
1060                         if ( audio_codec_id == CODEC_ID_AC3 && avcodec_find_encoder_by_name( "ac3_fixed" ) )
1061                         {
1062                                 mlt_properties_set( properties, "_acodec", "ac3_fixed" );
1063                                 acodec = mlt_properties_get( properties, "_acodec" );
1064                         }
1065                 }
1066                 else
1067                 {
1068                         audio_codec_id = CODEC_ID_NONE;
1069                         mlt_log_warning( MLT_CONSUMER_SERVICE( consumer ), "audio codec %s unrecognised - ignoring\n", acodec );
1070                 }
1071         }
1072
1073         // Check for video codec overides
1074         if ( ( vcodec && strcmp( vcodec, "none" ) == 0 ) || mlt_properties_get_int( properties, "vn" ) )
1075                 video_codec_id = CODEC_ID_NONE;
1076         else if ( vcodec )
1077         {
1078                 AVCodec *p = avcodec_find_encoder_by_name( vcodec );
1079                 if ( p != NULL )
1080                 {
1081                         video_codec_id = p->id;
1082                 }
1083                 else
1084                 {
1085                         video_codec_id = CODEC_ID_NONE;
1086                         mlt_log_warning( MLT_CONSUMER_SERVICE( consumer ), "video codec %s unrecognised - ignoring\n", vcodec );
1087                 }
1088         }
1089
1090         // Write metadata
1091 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(31<<8)+0)
1092         for ( i = 0; i < mlt_properties_count( properties ); i++ )
1093         {
1094                 char *name = mlt_properties_get_name( properties, i );
1095                 if ( name && !strncmp( name, "meta.attr.", 10 ) )
1096                 {
1097                         char *key = strdup( name + 10 );
1098                         char *markup = strrchr( key, '.' );
1099                         if ( markup && !strcmp( markup, ".markup") )
1100                         {
1101                                 markup[0] = '\0';
1102                                 if ( !strstr( key, ".stream." ) )
1103 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(43<<8)+0)
1104                                         av_metadata_set2( &oc->metadata, key, mlt_properties_get_value( properties, i ), 0 );
1105 #else
1106                                         av_metadata_set( &oc->metadata, key, mlt_properties_get_value( properties, i ) );
1107 #endif
1108                         }
1109                         free( key );
1110                 }
1111         }
1112 #else
1113         char *tmp = NULL;
1114         int metavalue;
1115
1116         tmp = mlt_properties_get( properties, "meta.attr.title.markup");
1117         if (tmp != NULL) snprintf( oc->title, sizeof(oc->title), "%s", tmp );
1118
1119         tmp = mlt_properties_get( properties, "meta.attr.comment.markup");
1120         if (tmp != NULL) snprintf( oc->comment, sizeof(oc->comment), "%s", tmp );
1121
1122         tmp = mlt_properties_get( properties, "meta.attr.author.markup");
1123         if (tmp != NULL) snprintf( oc->author, sizeof(oc->author), "%s", tmp );
1124
1125         tmp = mlt_properties_get( properties, "meta.attr.copyright.markup");
1126         if (tmp != NULL) snprintf( oc->copyright, sizeof(oc->copyright), "%s", tmp );
1127
1128         tmp = mlt_properties_get( properties, "meta.attr.album.markup");
1129         if (tmp != NULL) snprintf( oc->album, sizeof(oc->album), "%s", tmp );
1130
1131         metavalue = mlt_properties_get_int( properties, "meta.attr.year.markup");
1132         if (metavalue != 0) oc->year = metavalue;
1133
1134         metavalue = mlt_properties_get_int( properties, "meta.attr.track.markup");
1135         if (metavalue != 0) oc->track = metavalue;
1136 #endif
1137
1138         oc->oformat = fmt;
1139         snprintf( oc->filename, sizeof(oc->filename), "%s", filename );
1140
1141         // Add audio and video streams
1142         if ( video_codec_id != CODEC_ID_NONE )
1143                 video_st = add_video_stream( consumer, oc, video_codec_id );
1144         if ( audio_codec_id != CODEC_ID_NONE )
1145         {
1146                 int is_multi = 0;
1147
1148                 total_channels = 0;
1149                 // multitrack audio
1150                 for ( i = 0; i < MAX_AUDIO_STREAMS; i++ )
1151                 {
1152                         sprintf( key, "channels.%d", i );
1153                         int j = mlt_properties_get_int( properties, key );
1154                         if ( j )
1155                         {
1156                                 is_multi = 1;
1157                                 total_channels += j;
1158                                 audio_st[i] = add_audio_stream( consumer, oc, audio_codec_id, j );
1159                         }
1160                 }
1161                 // single track
1162                 if ( !is_multi )
1163                 {
1164                         audio_st[0] = add_audio_stream( consumer, oc, audio_codec_id, channels );
1165                         total_channels = channels;
1166                 }
1167         }
1168
1169         // Set the parameters (even though we have none...)
1170         if ( av_set_parameters(oc, NULL) >= 0 ) 
1171         {
1172                 oc->preload = ( int )( mlt_properties_get_double( properties, "muxpreload" ) * AV_TIME_BASE );
1173                 oc->max_delay= ( int )( mlt_properties_get_double( properties, "muxdelay" ) * AV_TIME_BASE );
1174
1175                 // Process properties as AVOptions
1176                 char *fpre = mlt_properties_get( properties, "fpre" );
1177                 if ( fpre )
1178                 {
1179                         mlt_properties p = mlt_properties_load( fpre );
1180                         apply_properties( oc, p, AV_OPT_FLAG_ENCODING_PARAM, 1 );
1181                         mlt_properties_close( p );
1182                 }
1183                 apply_properties( oc, properties, AV_OPT_FLAG_ENCODING_PARAM, 0 );
1184
1185                 if ( video_st && !open_video( oc, video_st, vcodec? vcodec : NULL ) )
1186                         video_st = NULL;
1187                 for ( i = 0; i < MAX_AUDIO_STREAMS && audio_st[i]; i++ )
1188                 {
1189                         audio_input_frame_size = open_audio( oc, audio_st[i], audio_outbuf_size,
1190                                 acodec? acodec : NULL );
1191                         if ( !audio_input_frame_size )
1192                                 audio_st[i] = NULL;
1193                 }
1194
1195                 // Open the output file, if needed
1196                 if ( !( fmt->flags & AVFMT_NOFILE ) ) 
1197                 {
1198                         if ( url_fopen( &oc->pb, filename, URL_WRONLY ) < 0 ) 
1199                         {
1200                                 mlt_log_error( MLT_CONSUMER_SERVICE( consumer ), "Could not open '%s'\n", filename );
1201                                 mlt_properties_set_int( properties, "running", 0 );
1202                         }
1203                 }
1204         
1205                 // Write the stream header.
1206                 if ( mlt_properties_get_int( properties, "running" ) )
1207                         av_write_header( oc );
1208         }
1209         else
1210         {
1211                 mlt_log_error( MLT_CONSUMER_SERVICE( consumer ), "Invalid output format parameters\n" );
1212                 mlt_properties_set_int( properties, "running", 0 );
1213         }
1214
1215         // Allocate picture
1216         if ( video_st )
1217                 output = alloc_picture( video_st->codec->pix_fmt, width, height );
1218
1219         // Last check - need at least one stream
1220         if ( !audio_st[0] && !video_st )
1221                 mlt_properties_set_int( properties, "running", 0 );
1222
1223         // Get the starting time (can ignore the times above)
1224         gettimeofday( &ante, NULL );
1225
1226         // Loop while running
1227         while( mlt_properties_get_int( properties, "running" ) &&
1228                ( !terminated || ( video_st && mlt_deque_count( queue ) ) ) )
1229         {
1230                 frame = mlt_consumer_rt_frame( consumer );
1231
1232                 // Check that we have a frame to work with
1233                 if ( frame != NULL )
1234                 {
1235                         // Increment frames dispatched
1236                         frames ++;
1237
1238                         // Default audio args
1239                         frame_properties = MLT_FRAME_PROPERTIES( frame );
1240
1241                         // Check for the terminated condition
1242                         terminated = terminate_on_pause && mlt_properties_get_double( frame_properties, "_speed" ) == 0.0;
1243
1244                         // Get audio and append to the fifo
1245                         if ( !terminated && audio_st[0] )
1246                         {
1247                                 samples = mlt_sample_calculator( fps, frequency, count ++ );
1248                                 mlt_frame_get_audio( frame, (void**) &pcm, &aud_fmt, &frequency, &channels, &samples );
1249
1250                                 // Save the audio channel remap properties for later
1251                                 mlt_properties_pass( frame_meta_properties, frame_properties, "meta.map.audio." );
1252
1253                                 // Create the fifo if we don't have one
1254                                 if ( fifo == NULL )
1255                                 {
1256                                         fifo = sample_fifo_init( frequency, channels );
1257                                         mlt_properties_set_data( properties, "sample_fifo", fifo, 0, ( mlt_destructor )sample_fifo_close, NULL );
1258                                 }
1259
1260                                 // Silence if not normal forward speed
1261                                 if ( mlt_properties_get_double( frame_properties, "_speed" ) != 1.0 )
1262                                         memset( pcm, 0, samples * channels * 2 );
1263
1264                                 // Append the samples
1265                                 sample_fifo_append( fifo, pcm, samples * channels );
1266                                 total_time += ( samples * 1000000 ) / frequency;
1267
1268                                 if ( !video_st )
1269                                         mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
1270                         }
1271
1272                         // Encode the image
1273                         if ( !terminated && video_st )
1274                                 mlt_deque_push_back( queue, frame );
1275                         else
1276                                 mlt_frame_close( frame );
1277                 }
1278
1279                 // While we have stuff to process, process...
1280                 while ( 1 )
1281                 {
1282                         // Write interleaved audio and video frames
1283                         if ( !video_st || ( video_st && audio_st[0] && audio_pts < video_pts ) )
1284                         {
1285                                 // Write audio
1286                                 if ( ( video_st && terminated ) || ( channels * audio_input_frame_size ) < sample_fifo_used( fifo ) )
1287                                 {
1288                                         int j = 0; // channel offset into interleaved source buffer
1289                                         int n = FFMIN( FFMIN( channels * audio_input_frame_size, sample_fifo_used( fifo ) ), AUDIO_ENCODE_BUFFER_SIZE );
1290
1291                                         // Get the audio samples
1292                                         if ( n > 0 )
1293                                         {
1294                                                 sample_fifo_fetch( fifo, audio_buf_1, n );
1295                                         }
1296                                         else if ( audio_codec_id == CODEC_ID_VORBIS && terminated )
1297                                         {
1298                                                 // This prevents an infinite loop when some versions of vorbis do not
1299                                                 // increment pts when encoding silence.
1300                                                 audio_pts = video_pts;
1301                                                 break;
1302                                         }
1303                                         else
1304                                         {
1305                                                 memset( audio_buf_1, 0, AUDIO_ENCODE_BUFFER_SIZE );
1306                                         }
1307                                         samples = n / channels;
1308
1309                                         // For each output stream
1310                                         for ( i = 0; i < MAX_AUDIO_STREAMS && audio_st[i] && j < total_channels; i++ )
1311                                         {
1312                                                 AVStream *stream = audio_st[i];
1313                                                 AVCodecContext *codec = stream->codec;
1314                                                 AVPacket pkt;
1315
1316                                                 av_init_packet( &pkt );
1317
1318                                                 // Optimized for single track and no channel remap
1319                                                 if ( !audio_st[1] && !mlt_properties_count( frame_meta_properties ) )
1320                                                 {
1321                                                         pkt.size = avcodec_encode_audio( codec, audio_outbuf, audio_outbuf_size, audio_buf_1 );
1322                                                 }
1323                                                 else
1324                                                 {
1325                                                         // Extract the audio channels according to channel mapping
1326                                                         int dest_offset = 0; // channel offset into interleaved dest buffer
1327
1328                                                         // Get the number of channels for this stream
1329                                                         sprintf( key, "channels.%d", i );
1330                                                         int current_channels = mlt_properties_get_int( properties, key );
1331
1332                                                         // Clear the destination audio buffer.
1333                                                         if ( !audio_buf_2 )
1334                                                                 audio_buf_2 = av_mallocz( AUDIO_ENCODE_BUFFER_SIZE );
1335                                                         else
1336                                                                 memset( audio_buf_2, 0, AUDIO_ENCODE_BUFFER_SIZE );
1337
1338                                                         // For each output channel
1339                                                         while ( dest_offset < current_channels && j < total_channels )
1340                                                         {
1341                                                                 int map_start = -1, map_channels = 0;
1342                                                                 int source_offset = 0;
1343                                                                 int k;
1344
1345                                                                 // Look for a mapping that starts at j
1346                                                                 for ( k = 0; k < (MAX_AUDIO_STREAMS * 2) && map_start != j; k++ )
1347                                                                 {
1348                                                                         sprintf( key, "%d.channels", k );
1349                                                                         map_channels = mlt_properties_get_int( frame_meta_properties, key );
1350                                                                         sprintf( key, "%d.start", k );
1351                                                                         if ( mlt_properties_get( frame_meta_properties, key ) )
1352                                                                                 map_start = mlt_properties_get_int( frame_meta_properties, key );
1353                                                                         if ( map_start != j )
1354                                                                                 source_offset += map_channels;
1355                                                                 }
1356
1357                                                                 // If no mapping
1358                                                                 if ( map_start != j )
1359                                                                 {
1360                                                                         map_channels = current_channels;
1361                                                                         source_offset = j;
1362                                                                 }
1363
1364                                                                 // Copy samples if source offset valid
1365                                                                 if ( source_offset < channels )
1366                                                                 {
1367                                                                         // Interleave the audio buffer with the # channels for this stream/mapping.
1368                                                                         for ( k = 0; k < map_channels; k++, j++, source_offset++, dest_offset++ )
1369                                                                         {
1370                                                                                 int16_t *src = audio_buf_1 + source_offset;
1371                                                                                 int16_t *dest = audio_buf_2 + dest_offset;
1372                                                                                 int s = samples + 1;
1373
1374                                                                                 while ( --s ) {
1375                                                                                         *dest = *src;
1376                                                                                         dest += current_channels;
1377                                                                                         src += channels;
1378                                                                                 }
1379                                                                         }
1380                                                                 }
1381                                                                 // Otherwise silence
1382                                                                 else
1383                                                                 {
1384                                                                         j += current_channels;
1385                                                                         dest_offset += current_channels;
1386                                                                 }
1387                                                         }
1388                                                         pkt.size = avcodec_encode_audio( codec, audio_outbuf, audio_outbuf_size, audio_buf_2 );
1389                                                 }
1390
1391                                                 // Write the compressed frame in the media file
1392                                                 if ( codec->coded_frame && codec->coded_frame->pts != AV_NOPTS_VALUE )
1393                                                 {
1394                                                         pkt.pts = av_rescale_q( codec->coded_frame->pts, codec->time_base, stream->time_base );
1395                                                         mlt_log_debug( MLT_CONSUMER_SERVICE( consumer ), "audio stream %d pkt pts %"PRId64" frame pts %"PRId64,
1396                                                                 stream->index, pkt.pts, codec->coded_frame->pts );
1397                                                 }
1398                                                 pkt.flags |= PKT_FLAG_KEY;
1399                                                 pkt.stream_index = stream->index;
1400                                                 pkt.data = audio_outbuf;
1401
1402                                                 if ( pkt.size > 0 )
1403                                                 {
1404                                                         if ( av_interleaved_write_frame( oc, &pkt ) )
1405                                                         {
1406                                                                 mlt_log_fatal( MLT_CONSUMER_SERVICE( consumer ), "error writing audio frame\n" );
1407                                                                 mlt_events_fire( properties, "consumer-fatal-error", NULL );
1408                                                                 goto on_fatal_error;
1409                                                         }
1410                                                 }
1411
1412                                                 mlt_log_debug( MLT_CONSUMER_SERVICE( consumer ), " frame_size %d\n", codec->frame_size );
1413                                                 if ( i == 0 )
1414                                                 {
1415                                                         audio_pts = (double)stream->pts.val * av_q2d( stream->time_base );
1416                                                 }
1417                                         }
1418                                 }
1419                                 else
1420                                 {
1421                                         break;
1422                                 }
1423                         }
1424                         else if ( video_st )
1425                         {
1426                                 // Write video
1427                                 if ( mlt_deque_count( queue ) )
1428                                 {
1429                                         int out_size, ret = 0;
1430                                         AVCodecContext *c;
1431
1432                                         frame = mlt_deque_pop_front( queue );
1433                                         frame_properties = MLT_FRAME_PROPERTIES( frame );
1434
1435                                         c = video_st->codec;
1436                                         
1437                                         if ( mlt_properties_get_int( frame_properties, "rendered" ) )
1438                                         {
1439                                                 int i = 0;
1440                                                 uint8_t *p;
1441                                                 uint8_t *q;
1442
1443                                                 mlt_frame_get_image( frame, &image, &img_fmt, &img_width, &img_height, 0 );
1444
1445                                                 q = image;
1446
1447                                                 // Convert the mlt frame to an AVPicture
1448                                                 for ( i = 0; i < height; i ++ )
1449                                                 {
1450                                                         p = input->data[ 0 ] + i * input->linesize[ 0 ];
1451                                                         memcpy( p, q, width * 2 );
1452                                                         q += width * 2;
1453                                                 }
1454
1455                                                 // Do the colour space conversion
1456 #ifdef SWSCALE
1457                                                 int flags = SWS_BILINEAR;
1458 #ifdef USE_MMX
1459                                                 flags |= SWS_CPU_CAPS_MMX;
1460 #endif
1461 #ifdef USE_SSE
1462                                                 flags |= SWS_CPU_CAPS_MMX2;
1463 #endif
1464                                                 struct SwsContext *context = sws_getContext( width, height, PIX_FMT_YUYV422,
1465                                                         width, height, video_st->codec->pix_fmt, flags, NULL, NULL, NULL);
1466                                                 sws_scale( context, (const uint8_t* const*) input->data, input->linesize, 0, height,
1467                                                         output->data, output->linesize);
1468                                                 sws_freeContext( context );
1469 #else
1470                                                 img_convert( ( AVPicture * )output, video_st->codec->pix_fmt, ( AVPicture * )input, PIX_FMT_YUYV422, width, height );
1471 #endif
1472
1473                                                 mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
1474
1475                                                 // Apply the alpha if applicable
1476                                                 if ( video_st->codec->pix_fmt == PIX_FMT_RGB32 )
1477                                                 {
1478                                                         uint8_t *alpha = mlt_frame_get_alpha_mask( frame );
1479                                                         register int n;
1480
1481                                                         for ( i = 0; i < height; i ++ )
1482                                                         {
1483                                                                 n = ( width + 7 ) / 8;
1484                                                                 p = output->data[ 0 ] + i * output->linesize[ 0 ] + 3;
1485
1486                                                                 switch( width % 8 )
1487                                                                 {
1488                                                                         case 0: do { *p = *alpha++; p += 4;
1489                                                                         case 7:          *p = *alpha++; p += 4;
1490                                                                         case 6:          *p = *alpha++; p += 4;
1491                                                                         case 5:          *p = *alpha++; p += 4;
1492                                                                         case 4:          *p = *alpha++; p += 4;
1493                                                                         case 3:          *p = *alpha++; p += 4;
1494                                                                         case 2:          *p = *alpha++; p += 4;
1495                                                                         case 1:          *p = *alpha++; p += 4;
1496                                                                                         }
1497                                                                                         while( --n );
1498                                                                 }
1499                                                         }
1500                                                 }
1501                                         }
1502
1503                                         if (oc->oformat->flags & AVFMT_RAWPICTURE) 
1504                                         {
1505                                                 // raw video case. The API will change slightly in the near future for that
1506                                                 AVPacket pkt;
1507                                                 av_init_packet(&pkt);
1508
1509                                                 pkt.flags |= PKT_FLAG_KEY;
1510                                                 pkt.stream_index= video_st->index;
1511                                                 pkt.data= (uint8_t *)output;
1512                                                 pkt.size= sizeof(AVPicture);
1513
1514                                                 ret = av_write_frame(oc, &pkt);
1515                                                 video_pts += c->frame_size;
1516                                         } 
1517                                         else 
1518                                         {
1519                                                 // Set the quality
1520                                                 output->quality = video_st->quality;
1521
1522                                                 // Set frame interlace hints
1523                                                 output->interlaced_frame = !mlt_properties_get_int( frame_properties, "progressive" );
1524                                                 output->top_field_first = mlt_properties_get_int( frame_properties, "top_field_first" );
1525                                                 output->pts = frame_count;
1526
1527                                                 // Encode the image
1528                                                 out_size = avcodec_encode_video(c, video_outbuf, video_outbuf_size, output );
1529
1530                                                 // If zero size, it means the image was buffered
1531                                                 if ( out_size > 0 )
1532                                                 {
1533                                                         AVPacket pkt;
1534                                                         av_init_packet( &pkt );
1535
1536                                                         if ( c->coded_frame && c->coded_frame->pts != AV_NOPTS_VALUE )
1537                                                                 pkt.pts= av_rescale_q( c->coded_frame->pts, c->time_base, video_st->time_base );
1538                                                         mlt_log_debug( MLT_CONSUMER_SERVICE( consumer ), "video pkt pts %"PRId64" frame pts %"PRId64, pkt.pts, c->coded_frame->pts );
1539                                                         if( c->coded_frame && c->coded_frame->key_frame )
1540                                                                 pkt.flags |= PKT_FLAG_KEY;
1541                                                         pkt.stream_index= video_st->index;
1542                                                         pkt.data= video_outbuf;
1543                                                         pkt.size= out_size;
1544
1545                                                         // write the compressed frame in the media file
1546                                                         ret = av_interleaved_write_frame(oc, &pkt);
1547                                                         mlt_log_debug( MLT_CONSUMER_SERVICE( consumer ), " frame_size %d\n", c->frame_size );
1548                                                         video_pts = (double)video_st->pts.val * av_q2d( video_st->time_base );
1549                                                         
1550                                                         // Dual pass logging
1551                                                         if ( mlt_properties_get_data( properties, "_logfile", NULL ) && c->stats_out )
1552                                                                 fprintf( mlt_properties_get_data( properties, "_logfile", NULL ), "%s", c->stats_out );
1553                                                 } 
1554                                                 else if ( out_size < 0 )
1555                                                 {
1556                                                         mlt_log_warning( MLT_CONSUMER_SERVICE( consumer ), "error with video encode %d\n", frame_count );
1557                                                 }
1558                                         }
1559                                         frame_count++;
1560                                         if ( ret )
1561                                         {
1562                                                 mlt_log_fatal( MLT_CONSUMER_SERVICE( consumer ), "error writing video frame\n" );
1563                                                 mlt_events_fire( properties, "consumer-fatal-error", NULL );
1564                                                 goto on_fatal_error;
1565                                         }
1566                                         mlt_frame_close( frame );
1567                                 }
1568                                 else
1569                                 {
1570                                         break;
1571                                 }
1572                         }
1573                         if ( audio_st[0] )
1574                                 mlt_log_debug( MLT_CONSUMER_SERVICE( consumer ), "audio pts %"PRId64" (%f) ", audio_st[0]->pts.val, audio_pts );
1575                         if ( video_st )
1576                                 mlt_log_debug( MLT_CONSUMER_SERVICE( consumer ), "video pts %"PRId64" (%f) ", video_st->pts.val, video_pts );
1577                         mlt_log_debug( MLT_CONSUMER_SERVICE( consumer ), "\n" );
1578                 }
1579
1580                 if ( real_time_output == 1 && frames % 2 == 0 )
1581                 {
1582                         long passed = time_difference( &ante );
1583                         if ( fifo != NULL )
1584                         {
1585                                 long pending = ( ( ( long )sample_fifo_used( fifo ) * 1000 ) / frequency ) * 1000;
1586                                 passed -= pending;
1587                         }
1588                         if ( passed < total_time )
1589                         {
1590                                 long total = ( total_time - passed );
1591                                 struct timespec t = { total / 1000000, ( total % 1000000 ) * 1000 };
1592                                 nanosleep( &t, NULL );
1593                         }
1594                 }
1595         }
1596
1597         // Flush the encoder buffers
1598         if ( real_time_output <= 0 )
1599         {
1600                 // Flush audio fifo
1601                 // TODO: flush all audio streams
1602                 if ( audio_st[0] && audio_st[0]->codec->frame_size > 1 ) for (;;)
1603                 {
1604                         AVCodecContext *c = audio_st[0]->codec;
1605                         AVPacket pkt;
1606                         av_init_packet( &pkt );
1607                         pkt.size = 0;
1608
1609                         if ( /*( c->capabilities & CODEC_CAP_SMALL_LAST_FRAME ) &&*/
1610                                 ( channels * audio_input_frame_size < sample_fifo_used( fifo ) ) )
1611                         {
1612                                 sample_fifo_fetch( fifo, audio_buf_1, channels * audio_input_frame_size );
1613                                 pkt.size = avcodec_encode_audio( c, audio_outbuf, audio_outbuf_size, audio_buf_1 );
1614                         }
1615                         if ( pkt.size <= 0 )
1616                                 pkt.size = avcodec_encode_audio( c, audio_outbuf, audio_outbuf_size, NULL );
1617                         mlt_log_debug( MLT_CONSUMER_SERVICE( consumer ), "flushing audio size %d\n", pkt.size );
1618                         if ( pkt.size <= 0 )
1619                                 break;
1620
1621                         // Write the compressed frame in the media file
1622                         if ( c->coded_frame && c->coded_frame->pts != AV_NOPTS_VALUE )
1623                                 pkt.pts = av_rescale_q( c->coded_frame->pts, c->time_base, audio_st[0]->time_base );
1624                         pkt.flags |= PKT_FLAG_KEY;
1625                         pkt.stream_index = audio_st[0]->index;
1626                         pkt.data = audio_outbuf;
1627                         if ( av_interleaved_write_frame( oc, &pkt ) != 0 )
1628                         {
1629                                 mlt_log_fatal( MLT_CONSUMER_SERVICE( consumer ), "error writing flushed audio frame\n" );
1630                                 mlt_events_fire( properties, "consumer-fatal-error", NULL );
1631                                 goto on_fatal_error;
1632                         }
1633                 }
1634
1635                 // Flush video
1636                 if ( video_st && !( oc->oformat->flags & AVFMT_RAWPICTURE ) ) for (;;)
1637                 {
1638                         AVCodecContext *c = video_st->codec;
1639                         AVPacket pkt;
1640                         av_init_packet( &pkt );
1641
1642                         // Encode the image
1643                         pkt.size = avcodec_encode_video( c, video_outbuf, video_outbuf_size, NULL );
1644                         mlt_log_debug( MLT_CONSUMER_SERVICE( consumer ), "flushing video size %d\n", pkt.size );
1645                         if ( pkt.size <= 0 )
1646                                 break;
1647
1648                         if ( c->coded_frame && c->coded_frame->pts != AV_NOPTS_VALUE )
1649                                 pkt.pts= av_rescale_q( c->coded_frame->pts, c->time_base, video_st->time_base );
1650                         if( c->coded_frame && c->coded_frame->key_frame )
1651                                 pkt.flags |= PKT_FLAG_KEY;
1652                         pkt.stream_index = video_st->index;
1653                         pkt.data = video_outbuf;
1654
1655                         // write the compressed frame in the media file
1656                         if ( av_interleaved_write_frame( oc, &pkt ) != 0 )
1657                         {
1658                                 mlt_log_fatal( MLT_CONSUMER_SERVICE(consumer), "error writing flushed video frame\n" );
1659                                 mlt_events_fire( properties, "consumer-fatal-error", NULL );
1660                                 goto on_fatal_error;
1661                         }
1662                         // Dual pass logging
1663                         if ( mlt_properties_get_data( properties, "_logfile", NULL ) && c->stats_out )
1664                                 fprintf( mlt_properties_get_data( properties, "_logfile", NULL ), "%s", c->stats_out );
1665                 }
1666         }
1667
1668 on_fatal_error:
1669         
1670         // Write the trailer, if any
1671         av_write_trailer( oc );
1672
1673         // close each codec
1674         if ( video_st )
1675                 close_video(oc, video_st);
1676         for ( i = 0; i < MAX_AUDIO_STREAMS && audio_st[i]; i++ )
1677                 close_audio( oc, audio_st[i] );
1678
1679         // Free the streams
1680         for ( i = 0; i < oc->nb_streams; i++ )
1681                 av_freep( &oc->streams[i] );
1682
1683         // Close the output file
1684         if ( !( fmt->flags & AVFMT_NOFILE ) )
1685 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(0<<8)+0)
1686                 url_fclose( oc->pb );
1687 #else
1688                 url_fclose( &oc->pb );
1689 #endif
1690
1691         // Clean up input and output frames
1692         if ( output )
1693                 av_free( output->data[0] );
1694         av_free( output );
1695         av_free( input->data[0] );
1696         av_free( input );
1697         av_free( video_outbuf );
1698         av_free( audio_buf_1 );
1699         av_free( audio_buf_2 );
1700
1701         // Free the stream
1702         av_free( oc );
1703
1704         // Just in case we terminated on pause
1705         mlt_properties_set_int( properties, "running", 0 );
1706
1707         mlt_consumer_stopped( consumer );
1708         mlt_properties_close( frame_meta_properties );
1709
1710         if ( mlt_properties_get_int( properties, "pass" ) > 1 )
1711         {
1712                 // Remove the dual pass log file
1713                 if ( mlt_properties_get( properties, "_logfilename" ) )
1714                         remove( mlt_properties_get( properties, "_logfilename" ) );
1715
1716                 // Remove the x264 dual pass logs
1717                 char *cwd = getcwd( NULL, 0 );
1718                 const char *file = "x264_2pass.log";
1719                 char *full = malloc( strlen( cwd ) + strlen( file ) + 2 );
1720                 sprintf( full, "%s/%s", cwd, file );
1721                 remove( full );
1722                 free( full );
1723                 file = "x264_2pass.log.temp";
1724                 full = malloc( strlen( cwd ) + strlen( file ) + 2 );
1725                 sprintf( full, "%s/%s", cwd, file );
1726                 remove( full );
1727                 free( full );
1728                 file = "x264_2pass.log.mbtree";
1729                 full = malloc( strlen( cwd ) + strlen( file ) + 2 );
1730                 sprintf( full, "%s/%s", cwd, file );
1731                 remove( full );
1732                 free( full );
1733                 free( cwd );
1734                 remove( "x264_2pass.log.temp" );
1735         }
1736
1737         return NULL;
1738 }
1739
1740 /** Close the consumer.
1741 */
1742
1743 static void consumer_close( mlt_consumer consumer )
1744 {
1745         // Stop the consumer
1746         mlt_consumer_stop( consumer );
1747
1748         // Close the parent
1749         mlt_consumer_close( consumer );
1750
1751         // Free the memory
1752         free( consumer );
1753 }