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