]> git.sesse.net Git - mlt/blob - src/modules/avformat/consumer_avformat.c
Fix over- and under-linking.
[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
27 // System header files
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <limits.h>
32 #include <pthread.h>
33 #include <sys/time.h>
34 #include <unistd.h>
35
36 // avformat header files
37 #include <avformat.h>
38 #ifdef SWSCALE
39 #include <swscale.h>
40 #endif
41 #include <opt.h>
42
43 #if LIBAVUTIL_VERSION_INT < (50<<16)
44 #define PIX_FMT_RGB32 PIX_FMT_RGBA32
45 #define PIX_FMT_YUYV422 PIX_FMT_YUV422
46 #endif
47
48 //
49 // This structure should be extended and made globally available in mlt
50 //
51
52 typedef struct
53 {
54         int16_t *buffer;
55         int size;
56         int used;
57         double time;
58         int frequency;
59         int channels;
60 }
61 *sample_fifo, sample_fifo_s;
62
63 sample_fifo sample_fifo_init( int frequency, int channels )
64 {
65         sample_fifo this = calloc( 1, sizeof( sample_fifo_s ) );
66         this->frequency = frequency;
67         this->channels = channels;
68         return this;
69 }
70
71 // sample_fifo_clear and check are temporarily aborted (not working as intended)
72
73 void sample_fifo_clear( sample_fifo this, double time )
74 {
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 )
77         {
78                 memmove( this->buffer, &this->buffer[ words ], ( this->used - words ) * sizeof( int16_t ) );
79                 this->used -= words;
80                 this->time = time;
81         }
82         else if ( ( int )( ( float )time * 100 ) != ( int )( ( float )this->time * 100 ) )
83         {
84                 this->used = 0;
85                 this->time = time;
86         }
87 }
88
89 void sample_fifo_check( sample_fifo this, double time )
90 {
91         if ( this->used == 0 )
92         {
93                 if ( ( int )( ( float )time * 100 ) < ( int )( ( float )this->time * 100 ) )
94                         this->time = time;
95         }
96 }
97
98 void sample_fifo_append( sample_fifo this, int16_t *samples, int count )
99 {
100         if ( ( this->size - this->used ) < count )
101         {
102                 this->size += count * 5;
103                 this->buffer = realloc( this->buffer, this->size * sizeof( int16_t ) );
104         }
105
106         memcpy( &this->buffer[ this->used ], samples, count * sizeof( int16_t ) );
107         this->used += count;
108 }
109
110 int sample_fifo_used( sample_fifo this )
111 {
112         return this->used;
113 }
114
115 int sample_fifo_fetch( sample_fifo this, int16_t *samples, int count )
116 {
117         if ( count > this->used )
118                 count = this->used;
119
120         memcpy( samples, this->buffer, count * sizeof( int16_t ) );
121         this->used -= count;
122         memmove( this->buffer, &this->buffer[ count ], this->used * sizeof( int16_t ) );
123
124         this->time += ( double )count / this->channels / this->frequency;
125
126         return count;
127 }
128
129 void sample_fifo_close( sample_fifo this )
130 {
131         free( this->buffer );
132         free( this );
133 }
134
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 );
141
142 /** Initialise the dv consumer.
143 */
144
145 mlt_consumer consumer_avformat_init( mlt_profile profile, char *arg )
146 {
147         // Allocate the consumer
148         mlt_consumer this = mlt_consumer_new( profile );
149
150         // If memory allocated and initialises without error
151         if ( this != NULL )
152         {
153                 // Get properties from the consumer
154                 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
155
156                 // Assign close callback
157                 this->close = consumer_close;
158
159                 // Interpret the argument
160                 if ( arg != NULL )
161                         mlt_properties_set( properties, "target", arg );
162
163                 // sample and frame queue
164                 mlt_properties_set_data( properties, "frame_queue", mlt_deque_init( ), 0, ( mlt_destructor )mlt_deque_close, NULL );
165
166                 // Audio options not fully handled by AVOptions
167 #define QSCALE_NONE (-99999)
168                 mlt_properties_set_int( properties, "aq", QSCALE_NONE );
169                 
170                 // Video options not fully handled by AVOptions
171                 mlt_properties_set_int( properties, "dc", 8 );
172                 
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 );
176
177                 // Ensure termination at end of the stream
178                 mlt_properties_set_int( properties, "terminate_on_pause", 1 );
179                 
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 );
183
184                 // Set up start/stop/terminated callbacks
185                 this->start = consumer_start;
186                 this->stop = consumer_stop;
187                 this->is_stopped = consumer_is_stopped;
188         }
189
190         // Return this
191         return this;
192 }
193
194 /** Start the consumer.
195 */
196
197 static int consumer_start( mlt_consumer this )
198 {
199         // Get the properties
200         mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
201         int error = 0;
202
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 )
206         {
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" );
212                 error = 1;
213         }
214         s = mlt_properties_get( properties, "acodec" );
215         if ( s && strcmp( s, "list" ) == 0 )
216         {
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" );
223                 error = 1;
224         }
225         s = mlt_properties_get( properties, "vcodec" );
226         if ( s && strcmp( s, "list" ) == 0 )
227         {
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" );
234                 error = 1;
235         }
236
237         // Check that we're not already running
238         if ( !error && !mlt_properties_get_int( properties, "running" ) )
239         {
240                 // Allocate a thread
241                 pthread_t *thread = calloc( 1, sizeof( pthread_t ) );
242
243                 // Get the width and height
244                 int width = mlt_properties_get_int( properties, "width" );
245                 int height = mlt_properties_get_int( properties, "height" );
246
247                 // Obtain the size property
248                 char *size = mlt_properties_get( properties, "s" );
249
250                 // Interpret it
251                 if ( size != NULL )
252                 {
253                         int tw, th;
254                         if ( sscanf( size, "%dx%d", &tw, &th ) == 2 && tw > 0 && th > 0 )
255                         {
256                                 width = tw;
257                                 height = th;
258                         }
259                         else
260                         {
261                                 fprintf( stderr, "%s: Invalid size property %s - ignoring.\n", __FILE__, size );
262                         }
263                 }
264                 
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 );
270
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 ) );
274                 if ( profile )
275                 {
276                         profile->width = width;
277                         profile->height = height;
278                 }
279
280                 // Handle the ffmpeg command line "-r" property for frame rate
281                 if ( mlt_properties_get( properties, "r" ) )
282                 {
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 );
287                         if ( profile )
288                         {
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 ) );
292                         }
293                 }
294                 
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" ) );
300
301                 // Assign the thread to properties
302                 mlt_properties_set_data( properties, "thread", thread, sizeof( pthread_t ), free, NULL );
303
304                 // Set the running state
305                 mlt_properties_set_int( properties, "running", 1 );
306
307                 // Create the thread
308                 pthread_create( thread, NULL, consumer_thread, this );
309         }
310         return error;
311 }
312
313 /** Stop the consumer.
314 */
315
316 static int consumer_stop( mlt_consumer this )
317 {
318         // Get the properties
319         mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
320
321         // Check that we're running
322         if ( mlt_properties_get_int( properties, "running" ) )
323         {
324                 // Get the thread
325                 pthread_t *thread = mlt_properties_get_data( properties, "thread", NULL );
326
327                 // Stop the thread
328                 mlt_properties_set_int( properties, "running", 0 );
329
330                 // Wait for termination
331                 pthread_join( *thread, NULL );
332         }
333
334         return 0;
335 }
336
337 /** Determine if the consumer is stopped.
338 */
339
340 static int consumer_is_stopped( mlt_consumer this )
341 {
342         // Get the properties
343         mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
344         return !mlt_properties_get_int( properties, "running" );
345 }
346
347 /** Process properties as AVOptions and apply to AV context obj
348 */
349
350 static void apply_properties( void *obj, mlt_properties properties, int flags )
351 {
352         int i;
353         int count = mlt_properties_count( properties ); 
354         for ( i = 0; i < count; i++ )
355         {
356                 const char *opt_name = mlt_properties_get_name( properties, i );
357                 const AVOption *opt = av_find_opt( obj, opt_name, NULL, flags, flags );
358                 if ( opt != NULL )
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 );
363 #else
364                         av_set_string( obj, opt_name, mlt_properties_get( properties, opt_name) );
365 #endif
366         }
367 }
368
369 /** Add an audio output stream
370 */
371
372 static AVStream *add_audio_stream( mlt_consumer this, AVFormatContext *oc, int codec_id )
373 {
374         // Get the properties
375         mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
376
377         // Create a new stream
378         AVStream *st = av_new_stream( oc, 1 );
379
380         // If created, then initialise from properties
381         if ( st != NULL ) 
382         {
383                 AVCodecContext *c = st->codec;
384
385                 // Establish defaults from AVOptions
386                 avcodec_get_context_defaults2( c, CODEC_TYPE_AUDIO );
387
388                 c->codec_id = codec_id;
389                 c->codec_type = CODEC_TYPE_AUDIO;
390
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 );         
397         
398                 if (oc->oformat->flags & AVFMT_GLOBALHEADER) 
399                         c->flags |= CODEC_FLAG_GLOBAL_HEADER;
400                 
401                 // Allow the user to override the audio fourcc
402                 if ( mlt_properties_get( properties, "atag" ) )
403                 {
404                         char *tail = NULL;
405                         char *arg = mlt_properties_get( properties, "atag" );
406                         int tag = strtol( arg, &tail, 0);
407                         if( !tail || *tail )
408                                 tag = arg[ 0 ] + ( arg[ 1 ] << 8 ) + ( arg[ 2 ] << 16 ) + ( arg[ 3 ] << 24 );
409                         c->codec_tag = tag;
410                 }
411
412                 // Process properties as AVOptions
413                 apply_properties( c, properties, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM );
414
415                 int audio_qscale = mlt_properties_get_int( properties, "aq" );
416         if ( audio_qscale > QSCALE_NONE )
417                 {
418                         c->flags |= CODEC_FLAG_QSCALE;
419                         c->global_quality = st->quality = FF_QP2LAMBDA * audio_qscale;
420                 }
421
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" );
425
426                 if ( mlt_properties_get( properties, "alang" ) != NULL )
427                         strncpy( st->language, mlt_properties_get( properties, "alang" ), sizeof( st->language ) );
428         }
429         else
430         {
431                 fprintf( stderr, "%s: Could not allocate a stream for audio\n", __FILE__ );
432         }
433
434         return st;
435 }
436
437 static int open_audio( AVFormatContext *oc, AVStream *st, int audio_outbuf_size )
438 {
439         // We will return the audio input size from here
440         int audio_input_frame_size = 0;
441
442         // Get the context
443         AVCodecContext *c = st->codec;
444
445         // Find the encoder
446         AVCodec *codec = avcodec_find_encoder( c->codec_id );
447
448         // Continue if codec found and we can open it
449         if ( codec != NULL && avcodec_open( c, codec ) >= 0 )
450         {
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 ) 
454                 {
455                         audio_input_frame_size = audio_outbuf_size / c->channels;
456                         switch(st->codec->codec_id) 
457                         {
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;
463                                         break;
464                                 default:
465                                         break;
466                         }
467                 } 
468                 else 
469                 {
470                         audio_input_frame_size = c->frame_size;
471                 }
472
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;
478         }
479         else
480         {
481                 fprintf( stderr, "%s: Unable to encode audio - disabling audio output.\n", __FILE__ );
482         }
483
484         return audio_input_frame_size;
485 }
486
487 static void close_audio( AVFormatContext *oc, AVStream *st )
488 {
489         avcodec_close( st->codec );
490 }
491
492 /** Add a video output stream 
493 */
494
495 static AVStream *add_video_stream( mlt_consumer this, AVFormatContext *oc, int codec_id )
496 {
497         // Get the properties
498         mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
499
500         // Create a new stream
501         AVStream *st = av_new_stream( oc, 0 );
502
503         if ( st != NULL ) 
504         {
505                 char *pix_fmt = mlt_properties_get( properties, "pix_fmt" );
506                 AVCodecContext *c = st->codec;
507
508                 // Establish defaults from AVOptions
509                 avcodec_get_context_defaults2( c, CODEC_TYPE_VIDEO );
510
511                 c->codec_id = codec_id;
512                 c->codec_type = CODEC_TYPE_VIDEO;
513                 
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 );         
520         
521                 // Process properties as AVOptions
522                 apply_properties( c, properties, AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM );
523
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;
532
533                 if ( mlt_properties_get( properties, "aspect" ) )
534                 {
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 );
538
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 ) );
544                         if ( profile )
545                         {
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 ) );
549                         }
550
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 );
558                         if ( profile )
559                         {
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 ) );
563                         }
564                 }
565                 else
566                 {
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" );
569                 }
570 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(21<<8)+0)
571                 st->sample_aspect_ratio = c->sample_aspect_ratio;
572 #endif
573
574                 if ( mlt_properties_get_double( properties, "qscale" ) > 0 )
575                 {
576                         c->flags |= CODEC_FLAG_QSCALE;
577                         st->quality = FF_QP2LAMBDA * mlt_properties_get_double( properties, "qscale" );
578                 }
579
580                 // Allow the user to override the video fourcc
581                 if ( mlt_properties_get( properties, "vtag" ) )
582                 {
583                         char *tail = NULL;
584                         const char *arg = mlt_properties_get( properties, "vtag" );
585                         int tag = strtol( arg, &tail, 0);
586                         if( !tail || *tail )
587                                 tag = arg[ 0 ] + ( arg[ 1 ] << 8 ) + ( arg[ 2 ] << 16 ) + ( arg[ 3 ] << 24 );
588                         c->codec_tag = tag;
589                 }
590
591                 // Some formats want stream headers to be seperate
592                 if ( oc->oformat->flags & AVFMT_GLOBALHEADER ) 
593                         c->flags |= CODEC_FLAG_GLOBAL_HEADER;
594
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 )
598                 {
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;
603                 }
604                 
605                 // parse the ratecontrol override string
606                 int i;
607                 char *rc_override = mlt_properties_get( properties, "rc_override" );
608                 for ( i = 0; rc_override; i++ )
609                 {
610                         int start, end, q;
611                         int e = sscanf( rc_override, "%d,%d,%d", &start, &end, &q );
612                         if ( e != 3 )
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;
617                         if ( q > 0 )
618                         {
619                                 c->rc_override[i].qscale = q;
620                                 c->rc_override[i].quality_factor = 1.0;
621                         }
622                         else
623                         {
624                                 c->rc_override[i].qscale = 0;
625                                 c->rc_override[i].quality_factor = -q / 100.0;
626                         }
627                         rc_override = strchr( rc_override, '/' );
628                         if ( rc_override )
629                                 rc_override++;
630                 }
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;
635
636                 // Setup dual-pass
637                 i = mlt_properties_get_int( properties, "pass" );
638                 if ( i == 1 )
639                         c->flags |= CODEC_FLAG_PASS1;
640                 else if ( i == 2 )
641                         c->flags |= CODEC_FLAG_PASS2;
642                 if ( codec_id != CODEC_ID_H264 && ( c->flags & ( CODEC_FLAG_PASS1 | CODEC_FLAG_PASS2 ) ) )
643                 {
644                         char logfilename[1024];
645                         FILE *f;
646                         int size;
647                         char *logbuffer;
648
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 )
652                         {
653                                 f = fopen( logfilename, "w" );
654                                 if ( !f )
655                                         perror( logfilename );
656                                 else
657                                         mlt_properties_set_data( properties, "_logfile", f, 0, ( mlt_destructor )fclose, NULL );
658                         }
659                         else
660                         {
661                                 /* read the log file */
662                                 f = fopen( logfilename, "r" );
663                                 if ( !f )
664                                 {
665                                         perror(logfilename);
666                                 }
667                                 else
668                                 {
669                                         mlt_properties_set( properties, "_logfilename", logfilename );
670                                         fseek( f, 0, SEEK_END );
671                                         size = ftell( f );
672                                         fseek( f, 0, SEEK_SET );
673                                         logbuffer = av_malloc( size + 1 );
674                                         if ( !logbuffer )
675                                                 fprintf( stderr, "%s: Could not allocate log buffer\n", __FILE__ );
676                                         else
677                                         {
678                                                 size = fread( logbuffer, 1, size, f );
679                                                 fclose( f );
680                                                 logbuffer[size] = '\0';
681                                                 c->stats_in = logbuffer;
682                                                 mlt_properties_set_data( properties, "_logbuffer", logbuffer, 0, ( mlt_destructor )av_free, NULL );
683                                         }
684                                 }
685                         }
686                 }
687         }
688         else
689         {
690                 fprintf( stderr, "%s: Could not allocate a stream for video\n", __FILE__ );
691         }
692  
693         return st;
694 }
695
696 static AVFrame *alloc_picture( int pix_fmt, int width, int height )
697 {
698         // Allocate a frame
699         AVFrame *picture = avcodec_alloc_frame();
700
701         // Determine size of the 
702         int size = avpicture_get_size(pix_fmt, width, height);
703
704         // Allocate the picture buf
705         uint8_t *picture_buf = av_malloc(size);
706
707         // If we have both, then fill the image
708         if ( picture != NULL && picture_buf != NULL )
709         {
710                 // Fill the frame with the allocated buffer
711                 avpicture_fill( (AVPicture *)picture, picture_buf, pix_fmt, width, height);
712         }
713         else
714         {
715                 // Something failed - clean up what we can
716                 av_free( picture );
717                 av_free( picture_buf );
718                 picture = NULL;
719         }
720
721         return picture;
722 }
723         
724 static int open_video(AVFormatContext *oc, AVStream *st)
725 {
726         // Get the codec
727         AVCodecContext *video_enc = st->codec;
728
729         // find the video encoder
730         AVCodec *codec = avcodec_find_encoder( video_enc->codec_id );
731
732         if( codec && codec->pix_fmts )
733         {
734                 const enum PixelFormat *p = codec->pix_fmts;
735                 for( ; *p!=-1; p++ )
736                 {
737                         if( *p == video_enc->pix_fmt )
738                                 break;
739                 }
740                 if( *p == -1 )
741                         video_enc->pix_fmt = codec->pix_fmts[ 0 ];
742         }
743
744         // Open the codec safely
745         return codec != NULL && avcodec_open( video_enc, codec ) >= 0;
746 }
747
748 void close_video(AVFormatContext *oc, AVStream *st)
749 {
750         avcodec_close(st->codec);
751 }
752
753 static inline long time_difference( struct timeval *time1 )
754 {
755         struct timeval time2;
756         gettimeofday( &time2, NULL );
757         return time2.tv_sec * 1000000 + time2.tv_usec - time1->tv_sec * 1000000 - time1->tv_usec;
758 }
759
760 /** The main thread - the argument is simply the consumer.
761 */
762
763 static void *consumer_thread( void *arg )
764 {
765         // Map the argument to the object
766         mlt_consumer this = arg;
767
768         // Get the properties
769         mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
770
771         // Get the terminate on pause property
772         int terminate_on_pause = mlt_properties_get_int( properties, "terminate_on_pause" );
773         int terminated = 0;
774
775         // Determine if feed is slow (for realtime stuff)
776         int real_time_output = mlt_properties_get_int( properties, "real_time" );
777
778         // Time structures
779         struct timeval ante;
780
781         // Get the frame rate
782         double fps = mlt_properties_get_double( properties, "fps" );
783
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;
789
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" );
794         int16_t *pcm = NULL;
795         int samples = 0;
796
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;
801
802         // AVFormat video buffer and frame count
803         int frame_count = 0;
804         int video_outbuf_size = ( 1024 * 1024 );
805         uint8_t *video_outbuf = av_malloc( video_outbuf_size );
806
807         // Used for the frame properties
808         mlt_frame frame = NULL;
809         mlt_properties frame_properties = NULL;
810
811         // Get the queues
812         mlt_deque queue = mlt_properties_get_data( properties, "frame_queue", NULL );
813         sample_fifo fifo = mlt_properties_get_data( properties, "sample_fifo", NULL );
814
815         // Need two av pictures for converting
816         AVFrame *output = NULL;
817         AVFrame *input = alloc_picture( PIX_FMT_YUYV422, width, height );
818
819         // For receiving images from an mlt_frame
820         uint8_t *image;
821         mlt_image_format img_fmt = mlt_image_yuv422;
822
823         // For receiving audio samples back from the fifo
824         int16_t *buffer = av_malloc( 48000 * 2 );
825         int count = 0;
826
827         // Allocate the context
828         AVFormatContext *oc = av_alloc_format_context( );
829
830         // Streams
831         AVStream *audio_st = NULL;
832         AVStream *video_st = NULL;
833
834         // Time stamps
835         double audio_pts = 0;
836         double video_pts = 0;
837
838         // Loop variable
839         int i;
840
841         // Frames despatched
842         long int frames = 0;
843         long int total_time = 0;
844
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" );
851         
852         // Used to store and override codec ids
853         int audio_codec_id;
854         int video_codec_id;
855
856         // Check for user selected format first
857         if ( format != NULL )
858                 fmt = guess_format( format, NULL, NULL );
859
860         // Otherwise check on the filename
861         if ( fmt == NULL && filename != NULL )
862                 fmt = guess_format( NULL, filename, NULL );
863
864         // Otherwise default to mpeg
865         if ( fmt == NULL )
866                 fmt = guess_format( "mpeg", NULL, NULL );
867
868         // We need a filename - default to stdout?
869         if ( filename == NULL || !strcmp( filename, "" ) )
870                 filename = "pipe:";
871
872         // Get the codec ids selected
873         audio_codec_id = fmt->audio_codec;
874         video_codec_id = fmt->video_codec;
875
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;
879         else if ( acodec )
880         {
881                 AVCodec *p = avcodec_find_encoder_by_name( acodec );
882                 if ( p != NULL )
883                         audio_codec_id = p->id;
884                 else
885                         fprintf( stderr, "%s: audio codec %s unrecognised - ignoring\n", __FILE__, acodec );
886         }
887
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;
891         else if ( vcodec )
892         {
893                 AVCodec *p = avcodec_find_encoder_by_name( vcodec );
894                 if ( p != NULL )
895                         video_codec_id = p->id;
896                 else
897                         fprintf( stderr, "%s: video codec %s unrecognised - ignoring\n", __FILE__, vcodec );
898         }
899
900         // Write metadata
901         char *tmp = NULL;
902         int metavalue;
903
904         tmp = mlt_properties_get( properties, "meta.attr.title.markup");
905         if (tmp != NULL) snprintf( oc->title, sizeof(oc->title), "%s", tmp );
906
907         tmp = mlt_properties_get( properties, "meta.attr.comment.markup");
908         if (tmp != NULL) snprintf( oc->comment, sizeof(oc->comment), "%s", tmp );
909
910         tmp = mlt_properties_get( properties, "meta.attr.author.markup");
911         if (tmp != NULL) snprintf( oc->author, sizeof(oc->author), "%s", tmp );
912
913         tmp = mlt_properties_get( properties, "meta.attr.copyright.markup");
914         if (tmp != NULL) snprintf( oc->copyright, sizeof(oc->copyright), "%s", tmp );
915
916         tmp = mlt_properties_get( properties, "meta.attr.album.markup");
917         if (tmp != NULL) snprintf( oc->album, sizeof(oc->album), "%s", tmp );
918
919         metavalue = mlt_properties_get_int( properties, "meta.attr.year.markup");
920         if (metavalue != 0) oc->year = metavalue;
921
922         metavalue = mlt_properties_get_int( properties, "meta.attr.track.markup");
923         if (metavalue != 0) oc->track = metavalue;
924
925         oc->oformat = fmt;
926         snprintf( oc->filename, sizeof(oc->filename), "%s", filename );
927
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 );
933
934         // Set the parameters (even though we have none...)
935         if ( av_set_parameters(oc, NULL) >= 0 ) 
936         {
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 );
939
940                 // Process properties as AVOptions
941                 apply_properties( oc, properties, AV_OPT_FLAG_ENCODING_PARAM );
942
943                 if ( video_st && !open_video( oc, video_st ) )
944                         video_st = NULL;
945                 if ( audio_st )
946                         audio_input_frame_size = open_audio( oc, audio_st, audio_outbuf_size );
947
948                 // Open the output file, if needed
949                 if ( !( fmt->flags & AVFMT_NOFILE ) ) 
950                 {
951                         if ( url_fopen( &oc->pb, filename, URL_WRONLY ) < 0 ) 
952                         {
953                                 fprintf( stderr, "%s: Could not open '%s'\n", __FILE__, filename );
954                                 mlt_properties_set_int( properties, "running", 0 );
955                         }
956                 }
957         
958                 // Write the stream header, if any
959                 if ( mlt_properties_get_int( properties, "running" ) )
960                         av_write_header( oc );
961         }
962         else
963         {
964                 fprintf( stderr, "%s: Invalid output format parameters\n", __FILE__ );
965                 mlt_properties_set_int( properties, "running", 0 );
966         }
967
968         // Allocate picture
969         if ( video_st )
970                 output = alloc_picture( video_st->codec->pix_fmt, width, height );
971
972         // Last check - need at least one stream
973         if ( audio_st == NULL && video_st == NULL )
974                 mlt_properties_set_int( properties, "running", 0 );
975
976         // Get the starting time (can ignore the times above)
977         gettimeofday( &ante, NULL );
978
979         // Loop while running
980         while( mlt_properties_get_int( properties, "running" ) && !terminated )
981         {
982                 // Get the frame
983                 frame = mlt_consumer_rt_frame( this );
984
985                 // Check that we have a frame to work with
986                 if ( frame != NULL )
987                 {
988                         // Increment frames despatched
989                         frames ++;
990
991                         // Default audio args
992                         frame_properties = MLT_FRAME_PROPERTIES( frame );
993
994                         // Check for the terminated condition
995                         terminated = terminate_on_pause && mlt_properties_get_double( frame_properties, "_speed" ) == 0.0;
996
997                         // Get audio and append to the fifo
998                         if ( !terminated && audio_st )
999                         {
1000                                 samples = mlt_sample_calculator( fps, frequency, count ++ );
1001                                 mlt_frame_get_audio( frame, &pcm, &aud_fmt, &frequency, &channels, &samples );
1002
1003                                 // Create the fifo if we don't have one
1004                                 if ( fifo == NULL )
1005                                 {
1006                                         fifo = sample_fifo_init( frequency, channels );
1007                                         mlt_properties_set_data( properties, "sample_fifo", fifo, 0, ( mlt_destructor )sample_fifo_close, NULL );
1008                                 }
1009
1010                                 if ( mlt_properties_get_double( frame_properties, "_speed" ) != 1.0 )
1011                                         memset( pcm, 0, samples * channels * 2 );
1012
1013                                 // Append the samples
1014                                 sample_fifo_append( fifo, pcm, samples * channels );
1015                                 total_time += ( samples * 1000000 ) / frequency;
1016                         }
1017
1018                         // Encode the image
1019                         if ( !terminated && video_st )
1020                                 mlt_deque_push_back( queue, frame );
1021                         else
1022                                 mlt_frame_close( frame );
1023                 }
1024
1025                 // While we have stuff to process, process...
1026                 while ( 1 )
1027                 {
1028                         if (audio_st)
1029                                 audio_pts = (double)audio_st->pts.val * audio_st->time_base.num / audio_st->time_base.den;
1030                         else
1031                                 audio_pts = 0.0;
1032         
1033                         if (video_st)
1034                                 video_pts = (double)video_st->pts.val * video_st->time_base.num / video_st->time_base.den;
1035                         else
1036                                 video_pts = 0.0;
1037
1038                         // Write interleaved audio and video frames
1039                         if ( !video_st || ( video_st && audio_st && audio_pts < video_pts ) )
1040                         {
1041                                 if ( channels * audio_input_frame_size < sample_fifo_used( fifo ) )
1042                                 {
1043                                         AVCodecContext *c;
1044                                         AVPacket pkt;
1045                                         av_init_packet( &pkt );
1046
1047                                         c = audio_st->codec;
1048
1049                                         sample_fifo_fetch( fifo, buffer, channels * audio_input_frame_size );
1050
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;
1058
1059                                         if ( pkt.size )
1060                                                 if ( av_interleaved_write_frame( oc, &pkt ) != 0) 
1061                                                         fprintf( stderr, "%s: Error while writing audio frame\n", __FILE__ );
1062
1063                                         audio_pts += c->frame_size;
1064                                 }
1065                                 else
1066                                 {
1067                                         break;
1068                                 }
1069                         }
1070                         else if ( video_st )
1071                         {
1072                                 if ( mlt_deque_count( queue ) )
1073                                 {
1074                                         int out_size, ret;
1075                                         AVCodecContext *c;
1076
1077                                         frame = mlt_deque_pop_front( queue );
1078                                         frame_properties = MLT_FRAME_PROPERTIES( frame );
1079
1080                                         c = video_st->codec;
1081                                         
1082                                         if ( mlt_properties_get_int( frame_properties, "rendered" ) )
1083                                         {
1084                                                 int i = 0;
1085                                                 int j = 0;
1086                                                 uint8_t *p;
1087                                                 uint8_t *q;
1088
1089                                                 mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
1090
1091                                                 mlt_frame_get_image( frame, &image, &img_fmt, &img_width, &img_height, 0 );
1092
1093                                                 q = image;
1094
1095                                                 // Convert the mlt frame to an AVPicture
1096                                                 for ( i = 0; i < height; i ++ )
1097                                                 {
1098                                                         p = input->data[ 0 ] + i * input->linesize[ 0 ];
1099                                                         j = width;
1100                                                         while( j -- )
1101                                                         {
1102                                                                 *p ++ = *q ++;
1103                                                                 *p ++ = *q ++;
1104                                                         }
1105                                                 }
1106
1107                                                 // Do the colour space conversion
1108 #ifdef SWSCALE
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 );
1114 #else
1115                                                 img_convert( ( AVPicture * )output, video_st->codec->pix_fmt, ( AVPicture * )input, PIX_FMT_YUYV422, width, height );
1116 #endif
1117
1118                                                 // Apply the alpha if applicable
1119                                                 if ( video_st->codec->pix_fmt == PIX_FMT_RGB32 )
1120                                                 {
1121                                                         uint8_t *alpha = mlt_frame_get_alpha_mask( frame );
1122                                                         register int n;
1123
1124                                                         for ( i = 0; i < height; i ++ )
1125                                                         {
1126                                                                 n = ( width + 7 ) / 8;
1127                                                                 p = output->data[ 0 ] + i * output->linesize[ 0 ];
1128
1129                                                                 #ifndef __DARWIN__
1130                                                                 p += 3;
1131                                                                 #endif
1132
1133                                                                 switch( width % 8 )
1134                                                                 {
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;
1143                                                                                         }
1144                                                                                         while( --n );
1145                                                                 }
1146                                                         }
1147                                                 }
1148                                         }
1149
1150                                         if (oc->oformat->flags & AVFMT_RAWPICTURE) 
1151                                         {
1152                                                 // raw video case. The API will change slightly in the near future for that
1153                                                 AVPacket pkt;
1154                                                 av_init_packet(&pkt);
1155
1156                                                 pkt.flags |= PKT_FLAG_KEY;
1157                                                 pkt.stream_index= video_st->index;
1158                                                 pkt.data= (uint8_t *)output;
1159                                                 pkt.size= sizeof(AVPicture);
1160
1161                                                 ret = av_write_frame(oc, &pkt);
1162                                                 video_pts += c->frame_size;
1163                                         } 
1164                                         else 
1165                                         {
1166                                                 // Set the quality
1167                                                 output->quality = video_st->quality;
1168
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" );
1172
1173                                                 // Encode the image
1174                                                 out_size = avcodec_encode_video(c, video_outbuf, video_outbuf_size, output );
1175
1176                                                 // If zero size, it means the image was buffered
1177                                                 if (out_size > 0) 
1178                                                 {
1179                                                         AVPacket pkt;
1180                                                         av_init_packet( &pkt );
1181
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;
1188                                                         pkt.size= out_size;
1189
1190                                                         // write the compressed frame in the media file
1191                                                         ret = av_interleaved_write_frame(oc, &pkt);
1192                                                         video_pts += c->frame_size;
1193                                                         
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 );
1197                                                 } 
1198                                                 else
1199                                                 {
1200                                                         fprintf( stderr, "%s: error with video encode\n", __FILE__ );
1201                                                 }
1202                                         }
1203                                         frame_count++;
1204                                         mlt_frame_close( frame );
1205                                 }
1206                                 else
1207                                 {
1208                                         break;
1209                                 }
1210                         }
1211                 }
1212
1213                 if ( real_time_output == 1 && frames % 12 == 0 )
1214                 {
1215                         long passed = time_difference( &ante );
1216                         if ( fifo != NULL )
1217                         {
1218                                 long pending = ( ( ( long )sample_fifo_used( fifo ) * 1000 ) / frequency ) * 1000;
1219                                 passed -= pending;
1220                         }
1221                         if ( passed < total_time )
1222                         {
1223                                 long total = ( total_time - passed );
1224                                 struct timespec t = { total / 1000000, ( total % 1000000 ) * 1000 };
1225                                 nanosleep( &t, NULL );
1226                         }
1227                 }
1228         }
1229
1230 #ifdef FLUSH
1231         if ( ! real_time_output )
1232         {
1233                 // Flush audio fifo
1234                 if ( audio_st && audio_st->codec->frame_size > 1 ) for (;;)
1235                 {
1236                         AVCodecContext *c = audio_st->codec;
1237                         AVPacket pkt;
1238                         av_init_packet( &pkt );
1239                         pkt.size = 0;
1240
1241                         if ( /*( c->capabilities & CODEC_CAP_SMALL_LAST_FRAME ) &&*/
1242                                 ( channels * audio_input_frame_size < sample_fifo_used( fifo ) ) )
1243                         {
1244                                 sample_fifo_fetch( fifo, buffer, channels * audio_input_frame_size );
1245                                 pkt.size = avcodec_encode_audio( c, audio_outbuf, audio_outbuf_size, buffer );
1246                         }
1247                         if ( pkt.size <= 0 )
1248                                 pkt.size = avcodec_encode_audio( c, audio_outbuf, audio_outbuf_size, NULL );
1249                         if ( pkt.size <= 0 )
1250                                 break;
1251
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 )
1259                         {
1260                                 fprintf( stderr, "%s: Error while writing flushed audio frame\n", __FILE__ );
1261                                 break;
1262                         }
1263                 }
1264
1265                 // Flush video
1266                 if ( video_st && !( oc->oformat->flags & AVFMT_RAWPICTURE ) ) for (;;)
1267                 {
1268                         AVCodecContext *c = video_st->codec;
1269                         AVPacket pkt;
1270                         av_init_packet( &pkt );
1271
1272                         // Encode the image
1273                         pkt.size = avcodec_encode_video( c, video_outbuf, video_outbuf_size, NULL );
1274                         if ( pkt.size <= 0 )
1275                                 break;
1276
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;
1283
1284                         // write the compressed frame in the media file
1285                         if ( av_interleaved_write_frame( oc, &pkt ) != 0 )
1286                         {
1287                                 fprintf( stderr, "%s: Error while writing flushed video frame\n". __FILE__ );
1288                                 break;
1289                         }
1290                 }
1291         }
1292 #endif
1293
1294         // close each codec 
1295         if (video_st)
1296                 close_video(oc, video_st);
1297         if (audio_st)
1298                 close_audio(oc, audio_st);
1299
1300         // Write the trailer, if any
1301         av_write_trailer(oc);
1302
1303         // Free the streams
1304         for(i = 0; i < oc->nb_streams; i++)
1305                 av_freep(&oc->streams[i]);
1306
1307         // Close the output file
1308         if (!(fmt->flags & AVFMT_NOFILE))
1309 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(0<<8)+0)
1310                 url_fclose(oc->pb);
1311 #else
1312                 url_fclose(&oc->pb);
1313 #endif
1314
1315         // Clean up input and output frames
1316         if ( output )
1317                 av_free( output->data[0] );
1318         av_free( output );
1319         av_free( input->data[0] );
1320         av_free( input );
1321         av_free( video_outbuf );
1322         av_free( buffer );
1323
1324         // Free the stream
1325         av_free(oc);
1326
1327         // Just in case we terminated on pause
1328         mlt_properties_set_int( properties, "running", 0 );
1329
1330         mlt_consumer_stopped( this );
1331
1332         if ( mlt_properties_get_int( properties, "pass" ) == 2 )
1333         {
1334                 // Remove the dual pass log file
1335                 if ( mlt_properties_get( properties, "_logfilename" ) )
1336                         remove( mlt_properties_get( properties, "_logfilename" ) );
1337
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 );
1343                 remove( full );
1344                 free( full );
1345                 file = "x264_2pass.log.temp";
1346                 full = malloc( strlen( cwd ) + strlen( file ) + 2 );
1347                 sprintf( full, "%s/%s", cwd, file );
1348                 remove( full );
1349                 free( full );
1350                 free( cwd );
1351                 remove( "x264_2pass.log.temp" );
1352         }
1353
1354         return NULL;
1355 }
1356
1357 /** Close the consumer.
1358 */
1359
1360 static void consumer_close( mlt_consumer this )
1361 {
1362         // Stop the consumer
1363         mlt_consumer_stop( this );
1364
1365         // Close the parent
1366         mlt_consumer_close( this );
1367
1368         // Free the memory
1369         free( this );
1370 }