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
36 // avformat header files
40 // This structure should be extended and made globally available in mlt
52 *sample_fifo
, sample_fifo_s
;
54 sample_fifo
sample_fifo_init( int frequency
, int channels
)
56 sample_fifo
this = calloc( 1, sizeof( sample_fifo_s
) );
57 this->frequency
= frequency
;
58 this->channels
= channels
;
62 // sample_fifo_clear and check are temporarily aborted (not working as intended)
64 void sample_fifo_clear( sample_fifo
this, double time
)
66 int words
= ( float )( time
- this->time
) * this->frequency
* this->channels
;
67 if ( ( int )( ( float )time
* 100 ) < ( int )( ( float )this->time
* 100 ) && this->used
> words
&& words
> 0 )
69 memmove( this->buffer
, &this->buffer
[ words
], ( this->used
- words
) * sizeof( int16_t ) );
73 else if ( ( int )( ( float )time
* 100 ) != ( int )( ( float )this->time
* 100 ) )
80 void sample_fifo_check( sample_fifo
this, double time
)
82 if ( this->used
== 0 )
84 if ( ( int )( ( float )time
* 100 ) < ( int )( ( float )this->time
* 100 ) )
89 void sample_fifo_append( sample_fifo
this, int16_t *samples
, int count
)
91 if ( ( this->size
- this->used
) < count
)
93 this->size
+= count
* 5;
94 this->buffer
= realloc( this->buffer
, this->size
* sizeof( int16_t ) );
97 memcpy( &this->buffer
[ this->used
], samples
, count
* sizeof( int16_t ) );
101 int sample_fifo_used( sample_fifo
this )
106 int sample_fifo_fetch( sample_fifo
this, int16_t *samples
, int count
)
108 if ( count
> this->used
)
111 memcpy( samples
, this->buffer
, count
* sizeof( int16_t ) );
113 memmove( this->buffer
, &this->buffer
[ count
], this->used
* sizeof( int16_t ) );
115 this->time
+= ( double )count
/ this->channels
/ this->frequency
;
120 void sample_fifo_close( sample_fifo
this )
122 free( this->buffer
);
126 // Forward references.
127 static int consumer_start( mlt_consumer
this );
128 static int consumer_stop( mlt_consumer
this );
129 static int consumer_is_stopped( mlt_consumer
this );
130 static void *consumer_thread( void *arg
);
131 static void consumer_close( mlt_consumer
this );
133 /** Initialise the dv consumer.
136 mlt_consumer
consumer_avformat_init( char *arg
)
138 // Allocate the consumer
139 mlt_consumer
this = mlt_consumer_new( );
141 // If memory allocated and initialises without error
144 // Get properties from the consumer
145 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
147 // Assign close callback
148 this->close
= consumer_close
;
150 // Interpret the argument
152 mlt_properties_set( properties
, "target", arg
);
154 // sample and frame queue
155 mlt_properties_set_data( properties
, "frame_queue", mlt_deque_init( ), 0, ( mlt_destructor
)mlt_deque_close
, NULL
);
157 // Set avformat defaults (all lifted from ffmpeg.c)
158 mlt_properties_set_int( properties
, "audio_bit_rate", 128000 );
159 mlt_properties_set_int( properties
, "video_bit_rate", 200 * 1000 );
160 mlt_properties_set_int( properties
, "video_bit_rate_tolerance", 4000 * 1000 );
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
) );
239 // Get the width and height
240 int width
= mlt_properties_get_int( properties
, "width" );
241 int height
= mlt_properties_get_int( properties
, "height" );
243 // Obtain the size property
244 char *size
= mlt_properties_get( properties
, "size" );
250 if ( sscanf( size
, "%dx%d", &tw
, &th
) == 2 && tw
> 0 && th
> 0 )
257 fprintf( stderr
, "consumer_avformat: Invalid size property %s - ignoring.\n", size
);
261 // Now ensure we honour the multiple of two requested by libavformat
262 mlt_properties_set_int( properties
, "width", ( width
/ 2 ) * 2 );
263 mlt_properties_set_int( properties
, "height", ( height
/ 2 ) * 2 );
265 // Assign the thread to properties
266 mlt_properties_set_data( properties
, "thread", thread
, sizeof( pthread_t
), free
, NULL
);
268 // Set the running state
269 mlt_properties_set_int( properties
, "running", 1 );
272 pthread_create( thread
, NULL
, consumer_thread
, this );
277 /** Stop the consumer.
280 static int consumer_stop( mlt_consumer
this )
282 // Get the properties
283 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
285 // Check that we're running
286 if ( mlt_properties_get_int( properties
, "running" ) )
289 pthread_t
*thread
= mlt_properties_get_data( properties
, "thread", NULL
);
292 mlt_properties_set_int( properties
, "running", 0 );
294 // Wait for termination
295 pthread_join( *thread
, NULL
);
301 /** Determine if the consumer is stopped.
304 static int consumer_is_stopped( mlt_consumer
this )
306 // Get the properties
307 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
308 return !mlt_properties_get_int( properties
, "running" );
311 /** Add an audio output stream
314 static AVStream
*add_audio_stream( mlt_consumer
this, AVFormatContext
*oc
, int codec_id
)
316 // Get the properties
317 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
319 // Create a new stream
320 AVStream
*st
= av_new_stream( oc
, 1 );
322 // If created, then initialise from properties
325 AVCodecContext
*c
= &st
->codec
;
326 c
->codec_id
= codec_id
;
327 c
->codec_type
= CODEC_TYPE_AUDIO
;
329 // Put sample parameters
330 c
->bit_rate
= mlt_properties_get_int( properties
, "audio_bit_rate" );
331 c
->sample_rate
= mlt_properties_get_int( properties
, "frequency" );
332 c
->channels
= mlt_properties_get_int( properties
, "channels" );
334 // Allow the user to override the audio fourcc
335 if ( mlt_properties_get( properties
, "afourcc" ) )
338 char *arg
= mlt_properties_get( properties
, "afourcc" );
339 int tag
= strtol( arg
, &tail
, 0);
341 tag
= arg
[ 0 ] + ( arg
[ 1 ] << 8 ) + ( arg
[ 2 ] << 16 ) + ( arg
[ 3 ] << 24 );
347 fprintf( stderr
, "Could not allocate a stream for audio\n" );
353 static int open_audio( AVFormatContext
*oc
, AVStream
*st
, int audio_outbuf_size
)
355 // We will return the audio input size from here
356 int audio_input_frame_size
= 0;
359 AVCodecContext
*c
= &st
->codec
;
362 AVCodec
*codec
= avcodec_find_encoder( c
->codec_id
);
364 // Continue if codec found and we can open it
365 if ( codec
!= NULL
&& avcodec_open(c
, codec
) >= 0 )
367 // ugly hack for PCM codecs (will be removed ASAP with new PCM
368 // support to compute the input frame size in samples
369 if ( c
->frame_size
<= 1 )
371 audio_input_frame_size
= audio_outbuf_size
/ c
->channels
;
372 switch(st
->codec
.codec_id
)
374 case CODEC_ID_PCM_S16LE
:
375 case CODEC_ID_PCM_S16BE
:
376 case CODEC_ID_PCM_U16LE
:
377 case CODEC_ID_PCM_U16BE
:
378 audio_input_frame_size
>>= 1;
386 audio_input_frame_size
= c
->frame_size
;
389 // Some formats want stream headers to be seperate (hmm)
390 if( !strcmp( oc
->oformat
->name
, "mp4" ) ||
391 !strcmp( oc
->oformat
->name
, "mov" ) ||
392 !strcmp( oc
->oformat
->name
, "3gp" ) )
393 c
->flags
|= CODEC_FLAG_GLOBAL_HEADER
;
397 fprintf( stderr
, "Unable to encode audio - disabling audio output.\n" );
400 return audio_input_frame_size
;
403 static void close_audio( AVFormatContext
*oc
, AVStream
*st
)
405 avcodec_close( &st
->codec
);
408 /** Add a video output stream
411 static AVStream
*add_video_stream( mlt_consumer
this, AVFormatContext
*oc
, int codec_id
)
413 // Get the properties
414 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
416 // Create a new stream
417 AVStream
*st
= av_new_stream( oc
, 0 );
421 AVCodecContext
*c
= &st
->codec
;
422 c
->codec_id
= codec_id
;
423 c
->codec_type
= CODEC_TYPE_VIDEO
;
425 // put sample parameters
426 c
->bit_rate
= mlt_properties_get_int( properties
, "video_bit_rate" );
427 c
->bit_rate_tolerance
= mlt_properties_get_int( properties
, "video_bit_rate_tolerance" );
428 c
->width
= mlt_properties_get_int( properties
, "width" );
429 c
->height
= mlt_properties_get_int( properties
, "height" );
430 c
->time_base
.num
= mlt_properties_get_int( properties
, "frame_rate_den" );
431 c
->time_base
.den
= mlt_properties_get_int( properties
, "frame_rate_num" );
432 c
->gop_size
= mlt_properties_get_int( properties
, "gop_size" );
433 c
->pix_fmt
= PIX_FMT_YUV420P
;
435 if ( mlt_properties_get_int( properties
, "b_frames" ) )
437 c
->max_b_frames
= mlt_properties_get_int( properties
, "b_frames" );
438 c
->b_frame_strategy
= 0;
439 c
->b_quant_factor
= 2.0;
442 c
->mb_decision
= mlt_properties_get_int( properties
, "mb_decision" );
443 c
->sample_aspect_ratio
= av_d2q( mlt_properties_get_double( properties
, "display_ratio" ) * c
->height
/ c
->width
, 255 );
444 c
->mb_cmp
= mlt_properties_get_int( properties
, "mb_cmp" );
445 c
->ildct_cmp
= mlt_properties_get_int( properties
, "ildct_cmp" );
446 c
->me_sub_cmp
= mlt_properties_get_int( properties
, "sub_cmp" );
447 c
->me_cmp
= mlt_properties_get_int( properties
, "cmp" );
448 c
->me_pre_cmp
= mlt_properties_get_int( properties
, "pre_cmp" );
449 c
->pre_me
= mlt_properties_get_int( properties
, "pre_me" );
450 c
->lumi_masking
= mlt_properties_get_double( properties
, "lumi_mask" );
451 c
->dark_masking
= mlt_properties_get_double( properties
, "dark_mask" );
452 c
->spatial_cplx_masking
= mlt_properties_get_double( properties
, "scplx_mask" );
453 c
->temporal_cplx_masking
= mlt_properties_get_double( properties
, "tcplx_mask" );
454 c
->p_masking
= mlt_properties_get_double( properties
, "p_mask" );
455 c
->quantizer_noise_shaping
= mlt_properties_get_int( properties
, "qns" );
456 c
->qmin
= mlt_properties_get_int( properties
, "video_qmin" );
457 c
->qmax
= mlt_properties_get_int( properties
, "video_qmax" );
458 c
->lmin
= mlt_properties_get_int( properties
, "video_lmin" );
459 c
->lmax
= mlt_properties_get_int( properties
, "video_lmax" );
460 c
->mb_qmin
= mlt_properties_get_int( properties
, "video_mb_qmin" );
461 c
->mb_qmax
= mlt_properties_get_int( properties
, "video_mb_qmax" );
462 c
->max_qdiff
= mlt_properties_get_int( properties
, "video_qdiff" );
463 c
->qblur
= mlt_properties_get_double( properties
, "video_qblur" );
464 c
->qcompress
= mlt_properties_get_double( properties
, "video_qcomp" );
466 if ( mlt_properties_get_double( properties
, "qscale" ) > 0 )
468 c
->flags
|= CODEC_FLAG_QSCALE
;
469 st
->quality
= FF_QP2LAMBDA
* mlt_properties_get_double( properties
, "qscale" );
472 // Allow the user to override the video fourcc
473 if ( mlt_properties_get( properties
, "vfourcc" ) )
476 const char *arg
= mlt_properties_get( properties
, "vfourcc" );
477 int tag
= strtol( arg
, &tail
, 0);
479 tag
= arg
[ 0 ] + ( arg
[ 1 ] << 8 ) + ( arg
[ 2 ] << 16 ) + ( arg
[ 3 ] << 24 );
483 // Some formats want stream headers to be seperate
484 if ( oc
->oformat
->flags
& AVFMT_GLOBALHEADER
)
485 c
->flags
|= CODEC_FLAG_GLOBAL_HEADER
;
487 c
->rc_max_rate
= mlt_properties_get_int( properties
, "video_rc_max_rate" );
488 c
->rc_min_rate
= mlt_properties_get_int( properties
, "video_rc_min_rate" );
489 c
->rc_buffer_size
= mlt_properties_get_int( properties
, "video_rc_buffer_size" );
490 c
->rc_buffer_aggressivity
= mlt_properties_get_double( properties
, "video_rc_buffer_aggressivity" );
491 c
->rc_initial_cplx
= mlt_properties_get_double( properties
, "video_rc_initial_cplx" );
492 c
->i_quant_factor
= mlt_properties_get_double( properties
, "video_i_qfactor" );
493 c
->b_quant_factor
= mlt_properties_get_double( properties
, "video_b_qfactor" );
494 c
->i_quant_offset
= mlt_properties_get_double( properties
, "video_i_qoffset" );
495 c
->b_quant_offset
= mlt_properties_get_double( properties
, "video_b_qoffset" );
496 c
->intra_quant_bias
= mlt_properties_get_int( properties
, "video_intra_quant_bias" );
497 c
->inter_quant_bias
= mlt_properties_get_int( properties
, "video_inter_quant_bias" );
498 c
->dct_algo
= mlt_properties_get_int( properties
, "dct_algo" );
499 c
->idct_algo
= mlt_properties_get_int( properties
, "idct_algo" );
500 c
->me_threshold
= mlt_properties_get_int( properties
, "me_threshold" );
501 c
->mb_threshold
= mlt_properties_get_int( properties
, "mb_threshold" );
502 c
->intra_dc_precision
= mlt_properties_get_int( properties
, "intra_dc_precision" );
503 c
->strict_std_compliance
= mlt_properties_get_int( properties
, "strict" );
504 c
->error_rate
= mlt_properties_get_int( properties
, "error_rate" );
505 c
->noise_reduction
= mlt_properties_get_int( properties
, "noise_reduction" );
506 c
->scenechange_threshold
= mlt_properties_get_int( properties
, "sc_threshold" );
507 c
->me_range
= mlt_properties_get_int( properties
, "me_range" );
508 c
->coder_type
= mlt_properties_get_int( properties
, "coder" );
509 c
->context_model
= mlt_properties_get_int( properties
, "context" );
510 c
->prediction_method
= mlt_properties_get_int( properties
, "predictor" );
511 c
->me_method
= mlt_properties_get_int( properties
, "me_method" );
515 fprintf( stderr
, "Could not allocate a stream for video\n" );
521 static AVFrame
*alloc_picture( int pix_fmt
, int width
, int height
)
524 AVFrame
*picture
= avcodec_alloc_frame();
526 // Determine size of the
527 int size
= avpicture_get_size(pix_fmt
, width
, height
);
529 // Allocate the picture buf
530 uint8_t *picture_buf
= av_malloc(size
);
532 // If we have both, then fill the image
533 if ( picture
!= NULL
&& picture_buf
!= NULL
)
535 // Fill the frame with the allocated buffer
536 avpicture_fill( (AVPicture
*)picture
, picture_buf
, pix_fmt
, width
, height
);
540 // Something failed - clean up what we can
542 av_free( picture_buf
);
549 static int open_video(AVFormatContext
*oc
, AVStream
*st
)
552 AVCodecContext
*video_enc
= &st
->codec
;
554 // find the video encoder
555 AVCodec
*codec
= avcodec_find_encoder( video_enc
->codec_id
);
557 if( codec
&& codec
->supported_framerates
)
559 const AVRational
*p
= codec
->supported_framerates
;
560 AVRational req
= ( AVRational
){ video_enc
->time_base
.num
, video_enc
->time_base
.den
};
561 const AVRational
*best
= NULL
;
562 AVRational best_error
= (AVRational
){ INT_MAX
, 1 };
563 for( ; p
->den
!=0; p
++ )
565 AVRational error
= av_sub_q( req
, *p
);
568 if( av_cmp_q( error
, best_error
) < 0 )
574 video_enc
->time_base
.num
= best
->num
;
575 video_enc
->time_base
.den
= best
->den
;
578 if( codec
&& codec
->pix_fmts
)
580 const enum PixelFormat
*p
= codec
->pix_fmts
;
583 if( *p
== video_enc
->pix_fmt
)
587 video_enc
->pix_fmt
= codec
->pix_fmts
[ 0 ];
590 // Open the codec safely
591 return codec
!= NULL
&& avcodec_open( video_enc
, codec
) >= 0;
594 void close_video(AVFormatContext
*oc
, AVStream
*st
)
596 avcodec_close(&st
->codec
);
599 static inline long time_difference( struct timeval
*time1
)
601 struct timeval time2
;
602 gettimeofday( &time2
, NULL
);
603 return time2
.tv_sec
* 1000000 + time2
.tv_usec
- time1
->tv_sec
* 1000000 - time1
->tv_usec
;
606 /** The main thread - the argument is simply the consumer.
609 static void *consumer_thread( void *arg
)
611 // Map the argument to the object
612 mlt_consumer
this = arg
;
614 // Get the properties
615 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
617 // Get the terminate on pause property
618 int terminate_on_pause
= mlt_properties_get_int( properties
, "terminate_on_pause" );
621 // Determine if feed is slow (for realtime stuff)
622 int real_time_output
= mlt_properties_get_int( properties
, "real_time" );
627 // Get the frame rate
628 int fps
= mlt_properties_get_double( properties
, "fps" );
630 // Get width and height
631 int width
= mlt_properties_get_int( properties
, "width" );
632 int height
= mlt_properties_get_int( properties
, "height" );
633 int img_width
= width
;
634 int img_height
= height
;
636 // Get default audio properties
637 mlt_audio_format aud_fmt
= mlt_audio_pcm
;
638 int channels
= mlt_properties_get_int( properties
, "channels" );
639 int frequency
= mlt_properties_get_int( properties
, "frequency" );
643 // AVFormat audio buffer and frame size
644 int audio_outbuf_size
= 10000;
645 uint8_t *audio_outbuf
= av_malloc( audio_outbuf_size
);
646 int audio_input_frame_size
= 0;
648 // AVFormat video buffer and frame count
650 int video_outbuf_size
= ( 1024 * 1024 );
651 uint8_t *video_outbuf
= av_malloc( video_outbuf_size
);
653 // Used for the frame properties
654 mlt_frame frame
= NULL
;
655 mlt_properties frame_properties
= NULL
;
658 mlt_deque queue
= mlt_properties_get_data( properties
, "frame_queue", NULL
);
659 sample_fifo fifo
= mlt_properties_get_data( properties
, "sample_fifo", NULL
);
661 // Need two av pictures for converting
662 AVFrame
*output
= NULL
;
663 AVFrame
*input
= alloc_picture( PIX_FMT_YUV422
, width
, height
);
665 // For receiving images from an mlt_frame
667 mlt_image_format img_fmt
= mlt_image_yuv422
;
669 // For receiving audio samples back from the fifo
670 int16_t *buffer
= av_malloc( 48000 * 2 );
673 // Allocate the context
674 AVFormatContext
*oc
= av_alloc_format_context( );
677 AVStream
*audio_st
= NULL
;
678 AVStream
*video_st
= NULL
;
681 double audio_pts
= 0;
682 double video_pts
= 0;
689 long int total_time
= 0;
691 // Determine the format
692 AVOutputFormat
*fmt
= NULL
;
693 char *filename
= mlt_properties_get( properties
, "target" );
694 char *format
= mlt_properties_get( properties
, "format" );
695 char *vcodec
= mlt_properties_get( properties
, "vcodec" );
696 char *acodec
= mlt_properties_get( properties
, "acodec" );
698 // Used to store and override codec ids
702 // Check for user selected format first
703 if ( format
!= NULL
)
704 fmt
= guess_format( format
, NULL
, NULL
);
706 // Otherwise check on the filename
707 if ( fmt
== NULL
&& filename
!= NULL
)
708 fmt
= guess_format( NULL
, filename
, NULL
);
710 // Otherwise default to mpeg
712 fmt
= guess_format( "mpeg", NULL
, NULL
);
714 // We need a filename - default to stdout?
715 if ( filename
== NULL
|| !strcmp( filename
, "" ) )
718 // Get the codec ids selected
719 audio_codec_id
= fmt
->audio_codec
;
720 video_codec_id
= fmt
->video_codec
;
722 // Check for audio codec overides
723 if ( acodec
!= NULL
)
725 AVCodec
*p
= first_avcodec
;
728 if ( !strcmp( p
->name
, acodec
) && p
->type
== CODEC_TYPE_AUDIO
)
733 audio_codec_id
= p
->id
;
735 fprintf( stderr
, "consumer_avcodec: audio codec %s unrecognised - ignoring\n", acodec
);
738 // Check for video codec overides
739 if ( vcodec
!= NULL
)
741 AVCodec
*p
= first_avcodec
;
744 if ( !strcmp( p
->name
, vcodec
) && p
->type
== CODEC_TYPE_VIDEO
)
749 video_codec_id
= p
->id
;
751 fprintf( stderr
, "consumer_avcodec: video codec %s unrecognised - ignoring\n", vcodec
);
754 // Update the output context
756 snprintf( oc
->filename
, sizeof(oc
->filename
), "%s", filename
);
758 // Add audio and video streams
759 if ( fmt
->video_codec
!= CODEC_ID_NONE
)
760 video_st
= add_video_stream( this, oc
, video_codec_id
);
761 if ( fmt
->audio_codec
!= CODEC_ID_NONE
)
762 audio_st
= add_audio_stream( this, oc
, audio_codec_id
);
764 // Set the parameters (even though we have none...)
765 if ( av_set_parameters(oc
, NULL
) >= 0 )
767 if ( video_st
&& !open_video( oc
, video_st
) )
770 audio_input_frame_size
= open_audio( oc
, audio_st
, audio_outbuf_size
);
772 // Open the output file, if needed
773 if ( !( fmt
->flags
& AVFMT_NOFILE
) )
775 if (url_fopen(&oc
->pb
, filename
, URL_WRONLY
) < 0)
777 fprintf(stderr
, "Could not open '%s'\n", filename
);
778 mlt_properties_set_int( properties
, "running", 0 );
782 // Write the stream header, if any
783 if ( mlt_properties_get_int( properties
, "running" ) )
784 av_write_header( oc
);
788 fprintf(stderr
, "Invalid output format parameters\n");
789 mlt_properties_set_int( properties
, "running", 0 );
794 output
= alloc_picture( video_st
->codec
.pix_fmt
, width
, height
);
796 // Last check - need at least one stream
797 if ( audio_st
== NULL
&& video_st
== NULL
)
798 mlt_properties_set_int( properties
, "running", 0 );
800 // Get the starting time (can ignore the times above)
801 gettimeofday( &ante
, NULL
);
803 // Loop while running
804 while( mlt_properties_get_int( properties
, "running" ) && !terminated
)
807 frame
= mlt_consumer_rt_frame( this );
809 // Check that we have a frame to work with
812 // Increment frames despatched
815 // Default audio args
816 frame_properties
= MLT_FRAME_PROPERTIES( frame
);
818 // Check for the terminated condition
819 terminated
= terminate_on_pause
&& mlt_properties_get_double( frame_properties
, "_speed" ) == 0.0;
821 // Get audio and append to the fifo
822 if ( !terminated
&& audio_st
)
824 samples
= mlt_sample_calculator( fps
, frequency
, count
);
825 mlt_frame_get_audio( frame
, &pcm
, &aud_fmt
, &frequency
, &channels
, &samples
);
827 // Create the fifo if we don't have one
830 fifo
= sample_fifo_init( frequency
, channels
);
831 mlt_properties_set_data( properties
, "sample_fifo", fifo
, 0, ( mlt_destructor
)sample_fifo_close
, NULL
);
834 if ( mlt_properties_get_double( frame_properties
, "_speed" ) != 1.0 )
835 memset( pcm
, 0, samples
* channels
* 2 );
837 // Append the samples
838 sample_fifo_append( fifo
, pcm
, samples
* channels
);
839 total_time
+= ( samples
* 1000000 ) / frequency
;
843 if ( !terminated
&& video_st
)
844 mlt_deque_push_back( queue
, frame
);
846 mlt_frame_close( frame
);
849 // While we have stuff to process, process...
853 audio_pts
= (double)audio_st
->pts
.val
* audio_st
->time_base
.num
/ audio_st
->time_base
.den
;
858 video_pts
= (double)video_st
->pts
.val
* video_st
->time_base
.num
/ video_st
->time_base
.den
;
862 // Write interleaved audio and video frames
863 if ( !video_st
|| ( video_st
&& audio_st
&& audio_pts
< video_pts
) )
865 if ( channels
* audio_input_frame_size
< sample_fifo_used( fifo
) )
869 av_init_packet( &pkt
);
871 c
= &audio_st
->codec
;
873 sample_fifo_fetch( fifo
, buffer
, channels
* audio_input_frame_size
);
875 pkt
.size
= avcodec_encode_audio( c
, audio_outbuf
, audio_outbuf_size
, buffer
);
876 // Write the compressed frame in the media file
877 if ( c
->coded_frame
)
878 pkt
.pts
= av_rescale_q( c
->coded_frame
->pts
, c
->time_base
, audio_st
->time_base
);
879 pkt
.flags
|= PKT_FLAG_KEY
;
880 pkt
.stream_index
= audio_st
->index
;
881 pkt
.data
= audio_outbuf
;
883 if ( av_interleaved_write_frame( oc
, &pkt
) != 0)
884 fprintf(stderr
, "Error while writing audio frame\n");
886 audio_pts
+= c
->frame_size
;
895 if ( mlt_deque_count( queue
) )
900 frame
= mlt_deque_pop_front( queue
);
901 frame_properties
= MLT_FRAME_PROPERTIES( frame
);
903 c
= &video_st
->codec
;
905 if ( mlt_properties_get_int( frame_properties
, "rendered" ) )
912 mlt_events_fire( properties
, "consumer-frame-show", frame
, NULL
);
914 // This will cause some fx to go awry....
915 if ( mlt_properties_get_int( properties
, "transcode" ) )
917 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame
), "normalised_width", img_height
* 4.0 / 3.0 );
918 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame
), "normalised_height", img_height
);
921 mlt_frame_get_image( frame
, &image
, &img_fmt
, &img_width
, &img_height
, 0 );
925 for ( i
= 0; i
< height
; i
++ )
927 p
= input
->data
[ 0 ] + i
* input
->linesize
[ 0 ];
936 img_convert( ( AVPicture
* )output
, video_st
->codec
.pix_fmt
, ( AVPicture
* )input
, PIX_FMT_YUV422
, width
, height
);
939 if (oc
->oformat
->flags
& AVFMT_RAWPICTURE
)
941 // raw video case. The API will change slightly in the near future for that
943 av_init_packet(&pkt
);
945 pkt
.flags
|= PKT_FLAG_KEY
;
946 pkt
.stream_index
= video_st
->index
;
947 pkt
.data
= (uint8_t *)output
;
948 pkt
.size
= sizeof(AVPicture
);
950 ret
= av_write_frame(oc
, &pkt
);
951 video_pts
+= c
->frame_size
;
956 output
->quality
= video_st
->quality
;
959 out_size
= avcodec_encode_video(c
, video_outbuf
, video_outbuf_size
, output
);
961 // If zero size, it means the image was buffered
965 av_init_packet( &pkt
);
967 if ( c
->coded_frame
)
968 pkt
.pts
= av_rescale_q( c
->coded_frame
->pts
, c
->time_base
, video_st
->time_base
);
969 if(c
->coded_frame
->key_frame
)
970 pkt
.flags
|= PKT_FLAG_KEY
;
971 pkt
.stream_index
= video_st
->index
;
972 pkt
.data
= video_outbuf
;
975 // write the compressed frame in the media file
976 ret
= av_interleaved_write_frame(oc
, &pkt
);
977 video_pts
+= c
->frame_size
;
981 mlt_frame_close( frame
);
990 if ( real_time_output
&& frames
% 12 == 0 )
992 long passed
= time_difference( &ante
);
995 long pending
= ( ( ( long )sample_fifo_used( fifo
) * 1000 ) / frequency
) * 1000;
998 if ( passed
< total_time
)
1000 long total
= ( total_time
- passed
);
1001 struct timespec t
= { total
/ 1000000, ( total
% 1000000 ) * 1000 };
1002 nanosleep( &t
, NULL
);
1009 close_video(oc
, video_st
);
1011 close_audio(oc
, audio_st
);
1013 // Write the trailer, if any
1014 av_write_trailer(oc
);
1017 for(i
= 0; i
< oc
->nb_streams
; i
++)
1018 av_freep(&oc
->streams
[i
]);
1020 // Close the output file
1021 if (!(fmt
->flags
& AVFMT_NOFILE
))
1022 url_fclose(&oc
->pb
);
1024 // Clean up input and output frames
1026 av_free( output
->data
[0] );
1028 av_free( input
->data
[0] );
1030 av_free( video_outbuf
);
1036 // Just in case we terminated on pause
1037 mlt_properties_set_int( properties
, "running", 0 );
1039 mlt_consumer_stopped( this );
1044 /** Close the consumer.
1047 static void consumer_close( mlt_consumer
this )
1049 // Stop the consumer
1050 mlt_consumer_stop( this );
1053 mlt_consumer_close( this );