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