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>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 #include "consumer_avformat.h"
25 #include <framework/mlt_frame.h>
27 // System header files
35 // avformat header files
39 // This structure should be extended and made globally available in mlt
51 *sample_fifo, sample_fifo_s;
53 sample_fifo sample_fifo_init( int frequency, int channels )
55 sample_fifo this = calloc( 1, sizeof( sample_fifo_s ) );
56 this->frequency = frequency;
57 this->channels = channels;
61 // sample_fifo_clear and check are temporarily aborted (not working as intended)
63 void sample_fifo_clear( sample_fifo this, double time )
65 int words = ( float )( time - this->time ) * this->frequency * this->channels;
66 if ( ( int )( ( float )time * 100 ) < ( int )( ( float )this->time * 100 ) && this->used > words && words > 0 )
68 memmove( this->buffer, &this->buffer[ words ], ( this->used - words ) * sizeof( int16_t ) );
72 else if ( ( int )( ( float )time * 100 ) != ( int )( ( float )this->time * 100 ) )
79 void sample_fifo_check( sample_fifo this, double time )
81 if ( this->used == 0 )
83 if ( ( int )( ( float )time * 100 ) < ( int )( ( float )this->time * 100 ) )
88 void sample_fifo_append( sample_fifo this, int16_t *samples, int count )
90 if ( ( this->size - this->used ) < count )
92 this->size += count * 5;
93 this->buffer = realloc( this->buffer, this->size * sizeof( int16_t ) );
96 memcpy( &this->buffer[ this->used ], samples, count * sizeof( int16_t ) );
100 int sample_fifo_used( sample_fifo this )
105 int sample_fifo_fetch( sample_fifo this, int16_t *samples, int count )
107 if ( count > this->used )
110 memcpy( samples, this->buffer, count * sizeof( int16_t ) );
112 memmove( this->buffer, &this->buffer[ count ], this->used * sizeof( int16_t ) );
114 this->time += ( double )count / this->channels / this->frequency;
119 void sample_fifo_close( sample_fifo this )
121 free( this->buffer );
125 // Forward references.
126 static int consumer_start( mlt_consumer this );
127 static int consumer_stop( mlt_consumer this );
128 static int consumer_is_stopped( mlt_consumer this );
129 static void *consumer_thread( void *arg );
130 static void consumer_close( mlt_consumer this );
132 /** Initialise the dv consumer.
135 mlt_consumer consumer_avformat_init( char *arg )
137 // Allocate the consumer
138 mlt_consumer this = mlt_consumer_new( );
140 // If memory allocated and initialises without error
143 // Get properties from the consumer
144 mlt_properties properties = mlt_consumer_properties( this );
146 // Assign close callback
147 this->close = consumer_close;
149 // Interpret the argument
151 mlt_properties_set( properties, "target", arg );
153 // sample and frame queue
154 mlt_properties_set_data( properties, "frame_queue", mlt_deque_init( ), 0, ( mlt_destructor )mlt_deque_close, NULL );
156 // Set avformat defaults (all lifted from ffmpeg.c)
157 mlt_properties_set_int( properties, "audio_bit_rate", 128000 );
158 mlt_properties_set_int( properties, "video_bit_rate", 200 * 1000 );
159 mlt_properties_set_int( properties, "video_bit_rate_tolerance", 4000 * 1000 );
160 mlt_properties_set_int( properties, "frame_rate_base", 1 );
161 mlt_properties_set_int( properties, "gop_size", 12 );
162 mlt_properties_set_int( properties, "b_frames", 0 );
163 mlt_properties_set_int( properties, "mb_decision", FF_MB_DECISION_SIMPLE );
164 mlt_properties_set_double( properties, "qscale", 0 );
165 mlt_properties_set_int( properties, "me_method", ME_EPZS );
166 mlt_properties_set_int( properties, "mb_cmp", FF_CMP_SAD );
167 mlt_properties_set_int( properties, "ildct_cmp", FF_CMP_VSAD );
168 mlt_properties_set_int( properties, "sub_cmp", FF_CMP_SAD );
169 mlt_properties_set_int( properties, "cmp", FF_CMP_SAD );
170 mlt_properties_set_int( properties, "pre_cmp", FF_CMP_SAD );
171 mlt_properties_set_int( properties, "pre_me", 0 );
172 mlt_properties_set_double( properties, "lumi_mask", 0 );
173 mlt_properties_set_double( properties, "dark_mask", 0 );
174 mlt_properties_set_double( properties, "scplx_mask", 0 );
175 mlt_properties_set_double( properties, "tcplx_mask", 0 );
176 mlt_properties_set_double( properties, "p_mask", 0 );
177 mlt_properties_set_int( properties, "qns", 0 );
178 mlt_properties_set_int( properties, "video_qmin", 2 );
179 mlt_properties_set_int( properties, "video_qmax", 31 );
180 mlt_properties_set_int( properties, "video_lmin", 2*FF_QP2LAMBDA );
181 mlt_properties_set_int( properties, "video_lmax", 31*FF_QP2LAMBDA );
182 mlt_properties_set_int( properties, "video_mb_qmin", 2 );
183 mlt_properties_set_int( properties, "video_mb_qmax", 31 );
184 mlt_properties_set_int( properties, "video_qdiff", 3 );
185 mlt_properties_set_double( properties, "video_qblur", 0.5 );
186 mlt_properties_set_double( properties, "video_qcomp", 0.5 );
187 mlt_properties_set_int( properties, "video_rc_max_rate", 0 );
188 mlt_properties_set_int( properties, "video_rc_min_rate", 0 );
189 mlt_properties_set_int( properties, "video_rc_buffer_size", 0 );
190 mlt_properties_set_double( properties, "video_rc_buffer_aggressivity", 1.0 );
191 mlt_properties_set_double( properties, "video_rc_initial_cplx", 0 );
192 mlt_properties_set_double( properties, "video_i_qfactor", 1.25 );
193 mlt_properties_set_double( properties, "video_b_qfactor", 1.25 );
194 mlt_properties_set_double( properties, "video_i_qoffset", -0.8 );
195 mlt_properties_set_double( properties, "video_b_qoffset", 0 );
196 mlt_properties_set_int( properties, "video_intra_quant_bias", FF_DEFAULT_QUANT_BIAS );
197 mlt_properties_set_int( properties, "video_inter_quant_bias", FF_DEFAULT_QUANT_BIAS );
198 mlt_properties_set_int( properties, "dct_algo", 0 );
199 mlt_properties_set_int( properties, "idct_algo", 0 );
200 mlt_properties_set_int( properties, "me_threshold", 0 );
201 mlt_properties_set_int( properties, "mb_threshold", 0 );
202 mlt_properties_set_int( properties, "intra_dc_precision", 0 );
203 mlt_properties_set_int( properties, "strict", 0 );
204 mlt_properties_set_int( properties, "error_rate", 0 );
205 mlt_properties_set_int( properties, "noise_reduction", 0 );
206 mlt_properties_set_int( properties, "sc_threshold", 0 );
207 mlt_properties_set_int( properties, "me_range", 0 );
208 mlt_properties_set_int( properties, "coder", 0 );
209 mlt_properties_set_int( properties, "context", 0 );
210 mlt_properties_set_int( properties, "predictor", 0 );
212 // Ensure termination at end of the stream
213 mlt_properties_set_int( properties, "terminate_on_pause", 1 );
215 // Set up start/stop/terminated callbacks
216 this->start = consumer_start;
217 this->stop = consumer_stop;
218 this->is_stopped = consumer_is_stopped;
225 /** Start the consumer.
228 static int consumer_start( mlt_consumer this )
230 // Get the properties
231 mlt_properties properties = mlt_consumer_properties( this );
233 // Check that we're not already running
234 if ( !mlt_properties_get_int( properties, "running" ) )
237 pthread_t *thread = calloc( 1, sizeof( pthread_t ) );
238 pthread_attr_t thread_attributes;
240 // Get the width and height
241 int width = mlt_properties_get_int( properties, "width" );
242 int height = mlt_properties_get_int( properties, "height" );
244 // Obtain the size property
245 char *size = mlt_properties_get( properties, "size" );
251 if ( sscanf( size, "%dx%d", &tw, &th ) == 2 && tw > 0 && th > 0 )
258 fprintf( stderr, "consumer_avformat: Invalid size property %s - ignoring.\n", size );
262 // Now ensure we honour the multiple of two requested by libavformat
263 mlt_properties_set_int( properties, "width", ( width / 2 ) * 2 );
264 mlt_properties_set_int( properties, "height", ( height / 2 ) * 2 );
266 // Assign the thread to properties
267 mlt_properties_set_data( properties, "thread", thread, sizeof( pthread_t ), free, NULL );
269 // Set the running state
270 mlt_properties_set_int( properties, "running", 1 );
272 // Inherit the scheduling priority
273 pthread_attr_init( &thread_attributes );
274 pthread_attr_setinheritsched( &thread_attributes, PTHREAD_INHERIT_SCHED );
277 pthread_create( thread, &thread_attributes, consumer_thread, this );
282 /** Stop the consumer.
285 static int consumer_stop( mlt_consumer this )
287 // Get the properties
288 mlt_properties properties = mlt_consumer_properties( this );
290 // Check that we're running
291 if ( mlt_properties_get_int( properties, "running" ) )
294 pthread_t *thread = mlt_properties_get_data( properties, "thread", NULL );
297 mlt_properties_set_int( properties, "running", 0 );
299 // Wait for termination
300 pthread_join( *thread, NULL );
306 /** Determine if the consumer is stopped.
309 static int consumer_is_stopped( mlt_consumer this )
311 // Get the properties
312 mlt_properties properties = mlt_consumer_properties( this );
313 return !mlt_properties_get_int( properties, "running" );
316 /** Add an audio output stream
319 static AVStream *add_audio_stream( mlt_consumer this, AVFormatContext *oc, int codec_id )
321 // Get the properties
322 mlt_properties properties = mlt_consumer_properties( this );
324 // Create a new stream
325 AVStream *st = av_new_stream( oc, 1 );
327 // If created, then initialise from properties
330 AVCodecContext *c = &st->codec;
331 c->codec_id = codec_id;
332 c->codec_type = CODEC_TYPE_AUDIO;
334 // Put sample parameters
335 c->bit_rate = mlt_properties_get_int( properties, "audio_bit_rate" );
336 c->sample_rate = mlt_properties_get_int( properties, "frequency" );
337 c->channels = mlt_properties_get_int( properties, "channels" );
341 fprintf( stderr, "Could not allocate a stream for audio\n" );
347 static int open_audio( AVFormatContext *oc, AVStream *st, int audio_outbuf_size )
349 // We will return the audio input size from here
350 int audio_input_frame_size = 0;
353 AVCodecContext *c = &st->codec;
356 AVCodec *codec = avcodec_find_encoder( c->codec_id );
358 // Continue if codec found and we can open it
359 if ( codec != NULL && avcodec_open(c, codec) >= 0 )
361 // ugly hack for PCM codecs (will be removed ASAP with new PCM
362 // support to compute the input frame size in samples
363 if ( c->frame_size <= 1 )
365 audio_input_frame_size = audio_outbuf_size / c->channels;
366 switch(st->codec.codec_id)
368 case CODEC_ID_PCM_S16LE:
369 case CODEC_ID_PCM_S16BE:
370 case CODEC_ID_PCM_U16LE:
371 case CODEC_ID_PCM_U16BE:
372 audio_input_frame_size >>= 1;
380 audio_input_frame_size = c->frame_size;
383 // Some formats want stream headers to be seperate (hmm)
384 if( !strcmp( oc->oformat->name, "mp4" ) ||
385 !strcmp( oc->oformat->name, "mov" ) ||
386 !strcmp( oc->oformat->name, "3gp" ) )
387 c->flags |= CODEC_FLAG_GLOBAL_HEADER;
391 fprintf( stderr, "Unable to encode audio - disabling audio output.\n" );
394 return audio_input_frame_size;
397 static void close_audio( AVFormatContext *oc, AVStream *st )
399 avcodec_close( &st->codec );
402 /** Add a video output stream
405 static AVStream *add_video_stream( mlt_consumer this, AVFormatContext *oc, int codec_id )
407 // Get the properties
408 mlt_properties properties = mlt_consumer_properties( this );
410 // Create a new stream
411 AVStream *st = av_new_stream( oc, 0 );
415 AVCodecContext *c = &st->codec;
416 c->codec_id = codec_id;
417 c->codec_type = CODEC_TYPE_VIDEO;
419 // put sample parameters
420 c->bit_rate = mlt_properties_get_int( properties, "video_bit_rate" );
421 c->bit_rate_tolerance = mlt_properties_get_int( properties, "video_bit_rate_tolerance" );
422 c->width = mlt_properties_get_int( properties, "width" );
423 c->height = mlt_properties_get_int( properties, "height" );
424 c->frame_rate = mlt_properties_get_double( properties, "fps" );
425 c->frame_rate_base = mlt_properties_get_double( properties, "frame_rate_base" );
426 c->frame_rate_base = 1;
427 c->gop_size = mlt_properties_get_int( properties, "gop_size" );
429 if ( mlt_properties_get_int( properties, "b_frames" ) )
431 c->max_b_frames = mlt_properties_get_int( properties, "b_frames" );
432 c->b_frame_strategy = 0;
433 c->b_quant_factor = 2.0;
436 c->mb_decision = mlt_properties_get_int( properties, "mb_decision" );
437 c->sample_aspect_ratio = av_d2q( mlt_properties_get_double( properties, "aspect_ratio" ), 255 );
438 c->mb_cmp = mlt_properties_get_int( properties, "mb_cmp" );
439 c->ildct_cmp = mlt_properties_get_int( properties, "ildct_cmp" );
440 c->me_sub_cmp = mlt_properties_get_int( properties, "sub_cmp" );
441 c->me_cmp = mlt_properties_get_int( properties, "cmp" );
442 c->me_pre_cmp = mlt_properties_get_int( properties, "pre_cmp" );
443 c->pre_me = mlt_properties_get_int( properties, "pre_me" );
444 c->lumi_masking = mlt_properties_get_double( properties, "lumi_mask" );
445 c->dark_masking = mlt_properties_get_double( properties, "dark_mask" );
446 c->spatial_cplx_masking = mlt_properties_get_double( properties, "scplx_mask" );
447 c->temporal_cplx_masking = mlt_properties_get_double( properties, "tcplx_mask" );
448 c->p_masking = mlt_properties_get_double( properties, "p_mask" );
449 c->quantizer_noise_shaping= mlt_properties_get_int( properties, "qns" );
450 c->qmin = mlt_properties_get_int( properties, "video_qmin" );
451 c->qmax = mlt_properties_get_int( properties, "video_qmax" );
452 c->lmin = mlt_properties_get_int( properties, "video_lmin" );
453 c->lmax = mlt_properties_get_int( properties, "video_lmax" );
454 c->mb_qmin = mlt_properties_get_int( properties, "video_mb_qmin" );
455 c->mb_qmax = mlt_properties_get_int( properties, "video_mb_qmax" );
456 c->max_qdiff = mlt_properties_get_int( properties, "video_qdiff" );
457 c->qblur = mlt_properties_get_double( properties, "video_qblur" );
458 c->qcompress = mlt_properties_get_double( properties, "video_qcomp" );
460 if ( mlt_properties_get_double( properties, "qscale" ) > 0 )
462 c->flags |= CODEC_FLAG_QSCALE;
463 st->quality = FF_QP2LAMBDA * mlt_properties_get_double( properties, "qscale" );
466 // Some formats want stream headers to be seperate (hmm)
467 if( !strcmp( oc->oformat->name, "mp4" ) ||
468 !strcmp( oc->oformat->name, "mov" ) ||
469 !strcmp( oc->oformat->name, "3gp" ) )
470 c->flags |= CODEC_FLAG_GLOBAL_HEADER;
472 c->rc_max_rate = mlt_properties_get_int( properties, "video_rc_max_rate" );
473 c->rc_min_rate = mlt_properties_get_int( properties, "video_rc_min_rate" );
474 c->rc_buffer_size = mlt_properties_get_int( properties, "video_rc_buffer_size" );
475 c->rc_buffer_aggressivity= mlt_properties_get_double( properties, "video_rc_buffer_aggressivity" );
476 c->rc_initial_cplx= mlt_properties_get_double( properties, "video_rc_initial_cplx" );
477 c->i_quant_factor = mlt_properties_get_double( properties, "video_i_qfactor" );
478 c->b_quant_factor = mlt_properties_get_double( properties, "video_b_qfactor" );
479 c->i_quant_offset = mlt_properties_get_double( properties, "video_i_qoffset" );
480 c->b_quant_offset = mlt_properties_get_double( properties, "video_b_qoffset" );
481 c->intra_quant_bias = mlt_properties_get_int( properties, "video_intra_quant_bias" );
482 c->inter_quant_bias = mlt_properties_get_int( properties, "video_inter_quant_bias" );
483 c->dct_algo = mlt_properties_get_int( properties, "dct_algo" );
484 c->idct_algo = mlt_properties_get_int( properties, "idct_algo" );
485 c->me_threshold= mlt_properties_get_int( properties, "me_threshold" );
486 c->mb_threshold= mlt_properties_get_int( properties, "mb_threshold" );
487 c->intra_dc_precision= mlt_properties_get_int( properties, "intra_dc_precision" );
488 c->strict_std_compliance = mlt_properties_get_int( properties, "strict" );
489 c->error_rate = mlt_properties_get_int( properties, "error_rate" );
490 c->noise_reduction= mlt_properties_get_int( properties, "noise_reduction" );
491 c->scenechange_threshold= mlt_properties_get_int( properties, "sc_threshold" );
492 c->me_range = mlt_properties_get_int( properties, "me_range" );
493 c->coder_type= mlt_properties_get_int( properties, "coder" );
494 c->context_model= mlt_properties_get_int( properties, "context" );
495 c->prediction_method= mlt_properties_get_int( properties, "predictor" );
496 c->me_method = mlt_properties_get_int( properties, "me_method" );
500 fprintf( stderr, "Could not allocate a stream for video\n" );
506 static AVFrame *alloc_picture( int pix_fmt, int width, int height )
509 AVFrame *picture = avcodec_alloc_frame();
511 // Determine size of the
512 int size = avpicture_get_size(pix_fmt, width, height);
514 // Allocate the picture buf
515 uint8_t *picture_buf = av_malloc(size);
517 // If we have both, then fill the image
518 if ( picture != NULL && picture_buf != NULL )
520 // Fill the frame with the allocated buffer
521 avpicture_fill( (AVPicture *)picture, picture_buf, pix_fmt, width, height);
525 // Something failed - clean up what we can
527 av_free( picture_buf );
534 static int open_video(AVFormatContext *oc, AVStream *st)
537 AVCodecContext *c = &st->codec;
539 // find the video encoder
540 AVCodec *codec = avcodec_find_encoder(c->codec_id);
542 // Open the codec safely
543 return codec != NULL && avcodec_open(c, codec) >= 0;
546 void close_video(AVFormatContext *oc, AVStream *st)
548 avcodec_close(&st->codec);
551 static inline long time_difference( struct timeval *time1 )
553 struct timeval time2;
554 gettimeofday( &time2, NULL );
555 return time2.tv_sec * 1000000 + time2.tv_usec - time1->tv_sec * 1000000 - time1->tv_usec;
558 /** The main thread - the argument is simply the consumer.
561 static void *consumer_thread( void *arg )
563 // Map the argument to the object
564 mlt_consumer this = arg;
566 // Get the properties
567 mlt_properties properties = mlt_consumer_properties( this );
569 // Get the terminate on pause property
570 int terminate_on_pause = mlt_properties_get_int( properties, "terminate_on_pause" );
573 // Determine if feed is slow (for realtime stuff)
574 int real_time_output = mlt_properties_get_int( properties, "real_time" );
579 // Get the frame rate
580 int fps = mlt_properties_get_double( properties, "fps" );
582 // Get width and height
583 int width = mlt_properties_get_int( properties, "width" );
584 int height = mlt_properties_get_int( properties, "height" );
585 int img_width = width;
586 int img_height = height;
588 // Get default audio properties
589 mlt_audio_format aud_fmt = mlt_audio_pcm;
590 int channels = mlt_properties_get_int( properties, "channels" );
591 int frequency = mlt_properties_get_int( properties, "frequency" );
595 // AVFormat audio buffer and frame size
596 int audio_outbuf_size = 10000;
597 uint8_t *audio_outbuf = av_malloc( audio_outbuf_size );
598 int audio_input_frame_size = 0;
600 // AVFormat video buffer and frame count
602 int video_outbuf_size = ( 1024 * 1024 );
603 uint8_t *video_outbuf = av_malloc( video_outbuf_size );
605 // Used for the frame properties
606 mlt_frame frame = NULL;
607 mlt_properties frame_properties = NULL;
610 mlt_deque queue = mlt_properties_get_data( properties, "frame_queue", NULL );
611 sample_fifo fifo = mlt_properties_get_data( properties, "sample_fifo", NULL );
613 // Need two av pictures for converting
614 AVFrame *output = alloc_picture( PIX_FMT_YUV420P, width, height );
615 AVFrame *input = alloc_picture( PIX_FMT_YUV422, width, height );
617 // For receiving images from an mlt_frame
619 mlt_image_format img_fmt = mlt_image_yuv422;
621 // For receiving audio samples back from the fifo
622 int16_t *buffer = av_malloc( 48000 * 2 );
625 // Allocate the context
626 AVFormatContext *oc = av_alloc_format_context( );
629 AVStream *audio_st = NULL;
630 AVStream *video_st = NULL;
633 double audio_pts, video_pts;
640 long int total_time = 0;
642 // Determine the format
643 AVOutputFormat *fmt = NULL;
644 char *filename = mlt_properties_get( properties, "target" );
645 char *format = mlt_properties_get( properties, "format" );
646 char *vcodec = mlt_properties_get( properties, "vcodec" );
647 char *acodec = mlt_properties_get( properties, "acodec" );
649 // Used to store and override codec ids
653 // Check for user selected format first
654 if ( format != NULL )
655 fmt = guess_format( format, NULL, NULL );
657 // Otherwise check on the filename
658 if ( fmt == NULL && filename != NULL )
659 fmt = guess_format( NULL, filename, NULL );
661 // Otherwise default to mpeg
663 fmt = guess_format( "mpeg", NULL, NULL );
665 // We need a filename - default to stdout?
666 if ( filename == NULL || !strcmp( filename, "" ) )
669 // Get the codec ids selected
670 audio_codec_id = fmt->audio_codec;
671 video_codec_id = fmt->video_codec;
673 // Check for audio codec overides
674 if ( acodec != NULL )
676 AVCodec *p = first_avcodec;
679 if ( !strcmp( p->name, acodec ) && p->type == CODEC_TYPE_AUDIO )
684 audio_codec_id = p->id;
686 fprintf( stderr, "consumer_avcodec: audio codec %s unrecognised - ignoring\n", acodec );
689 // Check for video codec overides
690 if ( vcodec != NULL )
692 AVCodec *p = first_avcodec;
695 if ( !strcmp( p->name, vcodec ) && p->type == CODEC_TYPE_VIDEO )
700 video_codec_id = p->id;
702 fprintf( stderr, "consumer_avcodec: video codec %s unrecognised - ignoring\n", vcodec );
705 // Update the output context
707 snprintf( oc->filename, sizeof(oc->filename), "%s", filename );
709 // Add audio and video streams
710 if ( fmt->video_codec != CODEC_ID_NONE )
711 video_st = add_video_stream( this, oc, video_codec_id );
712 if ( fmt->audio_codec != CODEC_ID_NONE )
713 audio_st = add_audio_stream( this, oc, audio_codec_id );
715 // Set the parameters (even though we have none...)
716 if ( av_set_parameters(oc, NULL) >= 0 )
718 if ( video_st && !open_video( oc, video_st ) )
721 audio_input_frame_size = open_audio( oc, audio_st, audio_outbuf_size );
723 // Open the output file, if needed
724 if ( !( fmt->flags & AVFMT_NOFILE ) )
726 if (url_fopen(&oc->pb, filename, URL_WRONLY) < 0)
728 fprintf(stderr, "Could not open '%s'\n", filename);
729 mlt_properties_set_int( properties, "running", 0 );
733 // Write the stream header, if any
734 if ( mlt_properties_get_int( properties, "running" ) )
735 av_write_header( oc );
739 fprintf(stderr, "Invalid output format parameters\n");
740 mlt_properties_set_int( properties, "running", 0 );
743 // Last check - need at least one stream
744 if ( audio_st == NULL && video_st == NULL )
745 mlt_properties_set_int( properties, "running", 0 );
747 // Get the starting time (can ignore the times above)
748 gettimeofday( &ante, NULL );
750 // Loop while running
751 while( mlt_properties_get_int( properties, "running" ) && !terminated )
754 frame = mlt_consumer_rt_frame( this );
756 // Check that we have a frame to work with
759 // Increment frames despatched
762 // Default audio args
763 frame_properties = mlt_frame_properties( frame );
765 // Check for the terminated condition
766 terminated = terminate_on_pause && mlt_properties_get_double( frame_properties, "_speed" ) == 0.0;
768 // Get audio and append to the fifo
771 samples = mlt_sample_calculator( fps, frequency, count );
772 mlt_frame_get_audio( frame, &pcm, &aud_fmt, &frequency, &channels, &samples );
774 // Create the fifo if we don't have one
777 fifo = sample_fifo_init( frequency, channels );
778 mlt_properties_set_data( properties, "sample_fifo", fifo, 0, ( mlt_destructor )sample_fifo_close, NULL );
781 // Append the samples
782 sample_fifo_append( fifo, pcm, samples * channels );
783 total_time += ( samples * 1000000 ) / frequency;
788 mlt_deque_push_back( queue, frame );
790 mlt_frame_close( frame );
793 // While we have stuff to process, process...
796 // Compute current audio and video time
798 audio_pts = (double)audio_st->pts.val * audio_st->time_base.num / audio_st->time_base.den;
803 video_pts = (double)video_st->pts.val * video_st->time_base.num / video_st->time_base.den;
807 // Write interleaved audio and video frames
808 if ( !video_st || ( video_st && audio_st && audio_pts < video_pts ) )
810 if ( channels * audio_input_frame_size < sample_fifo_used( fifo ) )
814 av_init_packet( &pkt );
816 c = &audio_st->codec;
818 sample_fifo_fetch( fifo, buffer, channels * audio_input_frame_size );
820 pkt.size = avcodec_encode_audio( c, audio_outbuf, audio_outbuf_size, buffer );
821 // Write the compressed frame in the media file
822 if ( c->coded_frame )
823 pkt.pts= c->coded_frame->pts;
824 pkt.flags |= PKT_FLAG_KEY;
825 pkt.stream_index= audio_st->index;
826 pkt.data= audio_outbuf;
828 if ( av_interleaved_write_frame( oc, &pkt ) != 0)
829 fprintf(stderr, "Error while writing audio frame\n");
838 if ( mlt_deque_count( queue ) )
843 frame = mlt_deque_pop_front( queue );
844 frame_properties = mlt_frame_properties( frame );
846 c = &video_st->codec;
848 if ( mlt_properties_get_int( frame_properties, "rendered" ) )
855 mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
857 // This will cause some fx to go awry....
858 if ( mlt_properties_get_int( properties, "transcode" ) )
860 mlt_properties_set_int( mlt_frame_properties( frame ), "normalised_width", img_height * 4.0 / 3.0 );
861 mlt_properties_set_int( mlt_frame_properties( frame ), "normalised_height", img_height );
864 mlt_frame_get_image( frame, &image, &img_fmt, &img_width, &img_height, 0 );
868 for ( i = 0; i < height; i ++ )
870 p = input->data[ 0 ] + i * input->linesize[ 0 ];
879 img_convert( ( AVPicture * )output, PIX_FMT_YUV420P, ( AVPicture * )input, PIX_FMT_YUV422, width, height );
882 if (oc->oformat->flags & AVFMT_RAWPICTURE)
884 // raw video case. The API will change slightly in the near future for that
886 av_init_packet(&pkt);
888 pkt.flags |= PKT_FLAG_KEY;
889 pkt.stream_index= video_st->index;
890 pkt.data= (uint8_t *)output;
891 pkt.size= sizeof(AVPicture);
893 ret = av_write_frame(oc, &pkt);
898 output->quality = video_st->quality;
901 out_size = avcodec_encode_video(c, video_outbuf, video_outbuf_size, output );
903 // If zero size, it means the image was buffered
907 av_init_packet( &pkt );
909 if ( c->coded_frame )
910 pkt.pts= c->coded_frame->pts;
911 if(c->coded_frame->key_frame)
912 pkt.flags |= PKT_FLAG_KEY;
913 pkt.stream_index= video_st->index;
914 pkt.data= video_outbuf;
917 // write the compressed frame in the media file
918 ret = av_interleaved_write_frame(oc, &pkt);
922 mlt_frame_close( frame );
931 if ( real_time_output && frames % 25 == 0 )
933 long passed = time_difference( &ante );
936 long pending = ( ( ( long )sample_fifo_used( fifo ) * 1000 ) / frequency ) * 1000;
939 if ( passed < total_time )
941 long total = ( total_time - passed );
942 struct timespec t = { total / 1000000, ( total % 1000000 ) * 1000 };
943 nanosleep( &t, NULL );
950 close_video(oc, video_st);
952 close_audio(oc, audio_st);
954 // Write the trailer, if any
955 av_write_trailer(oc);
958 for(i = 0; i < oc->nb_streams; i++)
959 av_freep(&oc->streams[i]);
961 // Close the output file
962 if (!(fmt->flags & AVFMT_NOFILE))
965 // Clean up input and output frames
966 av_free( output->data[0] );
968 av_free( input->data[0] );
970 av_free( video_outbuf );
976 // Just in case we terminated on pause
977 mlt_properties_set_int( properties, "running", 0 );
979 mlt_consumer_stopped( this );
984 /** Close the consumer.
987 static void consumer_close( mlt_consumer this )
990 mlt_consumer_stop( this );
993 mlt_consumer_close( this );