consumer_avformat.c: bugfix (kdenlive-28) a/v sync on non-whole frame rate.
[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 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library 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 GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 // Local header files
22 #include "consumer_avformat.h"
23
24 // mlt Header files
25 #include <framework/mlt_frame.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
36 // avformat header files
37 #include <avformat.h>
38 #ifdef SWSCALE
39 #include <swscale.h>
40 #endif
41
42 //
43 // This structure should be extended and made globally available in mlt
44 //
45
46 typedef struct
47 {
48 int16_t *buffer;
49 int size;
50 int used;
51 double time;
52 int frequency;
53 int channels;
54 }
55 *sample_fifo, sample_fifo_s;
56
57 sample_fifo sample_fifo_init( int frequency, int channels )
58 {
59 sample_fifo this = calloc( 1, sizeof( sample_fifo_s ) );
60 this->frequency = frequency;
61 this->channels = channels;
62 return this;
63 }
64
65 // sample_fifo_clear and check are temporarily aborted (not working as intended)
66
67 void sample_fifo_clear( sample_fifo this, double time )
68 {
69 int words = ( float )( time - this->time ) * this->frequency * this->channels;
70 if ( ( int )( ( float )time * 100 ) < ( int )( ( float )this->time * 100 ) && this->used > words && words > 0 )
71 {
72 memmove( this->buffer, &this->buffer[ words ], ( this->used - words ) * sizeof( int16_t ) );
73 this->used -= words;
74 this->time = time;
75 }
76 else if ( ( int )( ( float )time * 100 ) != ( int )( ( float )this->time * 100 ) )
77 {
78 this->used = 0;
79 this->time = time;
80 }
81 }
82
83 void sample_fifo_check( sample_fifo this, double time )
84 {
85 if ( this->used == 0 )
86 {
87 if ( ( int )( ( float )time * 100 ) < ( int )( ( float )this->time * 100 ) )
88 this->time = time;
89 }
90 }
91
92 void sample_fifo_append( sample_fifo this, int16_t *samples, int count )
93 {
94 if ( ( this->size - this->used ) < count )
95 {
96 this->size += count * 5;
97 this->buffer = realloc( this->buffer, this->size * sizeof( int16_t ) );
98 }
99
100 memcpy( &this->buffer[ this->used ], samples, count * sizeof( int16_t ) );
101 this->used += count;
102 }
103
104 int sample_fifo_used( sample_fifo this )
105 {
106 return this->used;
107 }
108
109 int sample_fifo_fetch( sample_fifo this, int16_t *samples, int count )
110 {
111 if ( count > this->used )
112 count = this->used;
113
114 memcpy( samples, this->buffer, count * sizeof( int16_t ) );
115 this->used -= count;
116 memmove( this->buffer, &this->buffer[ count ], this->used * sizeof( int16_t ) );
117
118 this->time += ( double )count / this->channels / this->frequency;
119
120 return count;
121 }
122
123 void sample_fifo_close( sample_fifo this )
124 {
125 free( this->buffer );
126 free( this );
127 }
128
129 // Forward references.
130 static int consumer_start( mlt_consumer this );
131 static int consumer_stop( mlt_consumer this );
132 static int consumer_is_stopped( mlt_consumer this );
133 static void *consumer_thread( void *arg );
134 static void consumer_close( mlt_consumer this );
135
136 /** Initialise the dv consumer.
137 */
138
139 mlt_consumer consumer_avformat_init( char *arg )
140 {
141 // Allocate the consumer
142 mlt_consumer this = mlt_consumer_new( );
143
144 // If memory allocated and initialises without error
145 if ( this != NULL )
146 {
147 // Get properties from the consumer
148 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
149
150 // Assign close callback
151 this->close = consumer_close;
152
153 // Interpret the argument
154 if ( arg != NULL )
155 mlt_properties_set( properties, "target", arg );
156
157 // sample and frame queue
158 mlt_properties_set_data( properties, "frame_queue", mlt_deque_init( ), 0, ( mlt_destructor )mlt_deque_close, NULL );
159
160 // Set avformat defaults (all lifted from ffmpeg.c)
161 mlt_properties_set_int( properties, "audio_bit_rate", 128000 );
162 mlt_properties_set_int( properties, "video_bit_rate", 200 * 1000 );
163 mlt_properties_set_int( properties, "video_bit_rate_tolerance", 4000 * 1000 );
164 mlt_properties_set_int( properties, "gop_size", 12 );
165 mlt_properties_set_int( properties, "b_frames", 0 );
166 mlt_properties_set_int( properties, "mb_decision", FF_MB_DECISION_SIMPLE );
167 mlt_properties_set_double( properties, "qscale", 0 );
168 mlt_properties_set_int( properties, "me_method", ME_EPZS );
169 mlt_properties_set_int( properties, "mb_cmp", FF_CMP_SAD );
170 mlt_properties_set_int( properties, "ildct_cmp", FF_CMP_VSAD );
171 mlt_properties_set_int( properties, "sub_cmp", FF_CMP_SAD );
172 mlt_properties_set_int( properties, "cmp", FF_CMP_SAD );
173 mlt_properties_set_int( properties, "pre_cmp", FF_CMP_SAD );
174 mlt_properties_set_int( properties, "pre_me", 0 );
175 mlt_properties_set_double( properties, "lumi_mask", 0 );
176 mlt_properties_set_double( properties, "dark_mask", 0 );
177 mlt_properties_set_double( properties, "scplx_mask", 0 );
178 mlt_properties_set_double( properties, "tcplx_mask", 0 );
179 mlt_properties_set_double( properties, "p_mask", 0 );
180 mlt_properties_set_int( properties, "qns", 0 );
181 mlt_properties_set_int( properties, "video_qmin", 2 );
182 mlt_properties_set_int( properties, "video_qmax", 31 );
183 mlt_properties_set_int( properties, "video_lmin", 2*FF_QP2LAMBDA );
184 mlt_properties_set_int( properties, "video_lmax", 31*FF_QP2LAMBDA );
185 mlt_properties_set_int( properties, "video_mb_qmin", 2 );
186 mlt_properties_set_int( properties, "video_mb_qmax", 31 );
187 mlt_properties_set_int( properties, "video_qdiff", 3 );
188 mlt_properties_set_double( properties, "video_qblur", 0.5 );
189 mlt_properties_set_double( properties, "video_qcomp", 0.5 );
190 mlt_properties_set_int( properties, "video_rc_max_rate", 0 );
191 mlt_properties_set_int( properties, "video_rc_min_rate", 0 );
192 mlt_properties_set_int( properties, "video_rc_buffer_size", 0 );
193 mlt_properties_set_double( properties, "video_rc_buffer_aggressivity", 1.0 );
194 mlt_properties_set_double( properties, "video_rc_initial_cplx", 0 );
195 mlt_properties_set_double( properties, "video_i_qfactor", -0.8 );
196 mlt_properties_set_double( properties, "video_b_qfactor", 1.25 );
197 mlt_properties_set_double( properties, "video_i_qoffset", 0 );
198 mlt_properties_set_double( properties, "video_b_qoffset", 1.25 );
199 mlt_properties_set_int( properties, "video_intra_quant_bias", FF_DEFAULT_QUANT_BIAS );
200 mlt_properties_set_int( properties, "video_inter_quant_bias", FF_DEFAULT_QUANT_BIAS );
201 mlt_properties_set_int( properties, "dct_algo", 0 );
202 mlt_properties_set_int( properties, "idct_algo", 0 );
203 mlt_properties_set_int( properties, "me_threshold", 0 );
204 mlt_properties_set_int( properties, "mb_threshold", 0 );
205 mlt_properties_set_int( properties, "intra_dc_precision", 0 );
206 mlt_properties_set_int( properties, "strict", 0 );
207 mlt_properties_set_int( properties, "error_rate", 0 );
208 mlt_properties_set_int( properties, "noise_reduction", 0 );
209 mlt_properties_set_int( properties, "sc_threshold", 0 );
210 mlt_properties_set_int( properties, "me_range", 0 );
211 mlt_properties_set_int( properties, "coder", 0 );
212 mlt_properties_set_int( properties, "context", 0 );
213 mlt_properties_set_int( properties, "predictor", 0 );
214 mlt_properties_set_int( properties, "ildct", 0 );
215 mlt_properties_set_int( properties, "ilme", 0 );
216
217 // Ensure termination at end of the stream
218 mlt_properties_set_int( properties, "terminate_on_pause", 1 );
219
220 // Set up start/stop/terminated callbacks
221 this->start = consumer_start;
222 this->stop = consumer_stop;
223 this->is_stopped = consumer_is_stopped;
224 }
225
226 // Return this
227 return this;
228 }
229
230 /** Start the consumer.
231 */
232
233 static int consumer_start( mlt_consumer this )
234 {
235 // Get the properties
236 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
237
238 // Check that we're not already running
239 if ( !mlt_properties_get_int( properties, "running" ) )
240 {
241 // Allocate a thread
242 pthread_t *thread = calloc( 1, sizeof( pthread_t ) );
243
244 // Get the width and height
245 int width = mlt_properties_get_int( properties, "width" );
246 int height = mlt_properties_get_int( properties, "height" );
247
248 // Obtain the size property
249 char *size = mlt_properties_get( properties, "size" );
250
251 // Interpret it
252 if ( size != NULL )
253 {
254 int tw, th;
255 if ( sscanf( size, "%dx%d", &tw, &th ) == 2 && tw > 0 && th > 0 )
256 {
257 width = tw;
258 height = th;
259 }
260 else
261 {
262 fprintf( stderr, "consumer_avformat: Invalid size property %s - ignoring.\n", size );
263 }
264 }
265
266 // Now ensure we honour the multiple of two requested by libavformat
267 mlt_properties_set_int( properties, "width", ( width / 2 ) * 2 );
268 mlt_properties_set_int( properties, "height", ( height / 2 ) * 2 );
269
270 // Assign the thread to properties
271 mlt_properties_set_data( properties, "thread", thread, sizeof( pthread_t ), free, NULL );
272
273 // Set the running state
274 mlt_properties_set_int( properties, "running", 1 );
275
276 // Create the thread
277 pthread_create( thread, NULL, consumer_thread, this );
278 }
279 return 0;
280 }
281
282 /** Stop the consumer.
283 */
284
285 static int consumer_stop( mlt_consumer this )
286 {
287 // Get the properties
288 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
289
290 // Check that we're running
291 if ( mlt_properties_get_int( properties, "running" ) )
292 {
293 // Get the thread
294 pthread_t *thread = mlt_properties_get_data( properties, "thread", NULL );
295
296 // Stop the thread
297 mlt_properties_set_int( properties, "running", 0 );
298
299 // Wait for termination
300 pthread_join( *thread, NULL );
301 }
302
303 return 0;
304 }
305
306 /** Determine if the consumer is stopped.
307 */
308
309 static int consumer_is_stopped( mlt_consumer this )
310 {
311 // Get the properties
312 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
313 return !mlt_properties_get_int( properties, "running" );
314 }
315
316 /** Add an audio output stream
317 */
318
319 static AVStream *add_audio_stream( mlt_consumer this, AVFormatContext *oc, int codec_id )
320 {
321 // Get the properties
322 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
323
324 // Create a new stream
325 AVStream *st = av_new_stream( oc, 1 );
326
327 // If created, then initialise from properties
328 if ( st != NULL )
329 {
330 AVCodecContext *c = st->codec;
331 c->codec_id = codec_id;
332 c->codec_type = CODEC_TYPE_AUDIO;
333
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" );
338
339 if (oc->oformat->flags & AVFMT_GLOBALHEADER)
340 c->flags |= CODEC_FLAG_GLOBAL_HEADER;
341
342 // Allow the user to override the audio fourcc
343 if ( mlt_properties_get( properties, "afourcc" ) )
344 {
345 char *tail = NULL;
346 char *arg = mlt_properties_get( properties, "afourcc" );
347 int tag = strtol( arg, &tail, 0);
348 if( !tail || *tail )
349 tag = arg[ 0 ] + ( arg[ 1 ] << 8 ) + ( arg[ 2 ] << 16 ) + ( arg[ 3 ] << 24 );
350 c->codec_tag = tag;
351 }
352 }
353 else
354 {
355 fprintf( stderr, "Could not allocate a stream for audio\n" );
356 }
357
358 return st;
359 }
360
361 static int open_audio( AVFormatContext *oc, AVStream *st, int audio_outbuf_size )
362 {
363 // We will return the audio input size from here
364 int audio_input_frame_size = 0;
365
366 // Get the context
367 AVCodecContext *c = st->codec;
368
369 // Find the encoder
370 AVCodec *codec = avcodec_find_encoder( c->codec_id );
371
372 // Continue if codec found and we can open it
373 if ( codec != NULL && avcodec_open(c, codec) >= 0 )
374 {
375 // ugly hack for PCM codecs (will be removed ASAP with new PCM
376 // support to compute the input frame size in samples
377 if ( c->frame_size <= 1 )
378 {
379 audio_input_frame_size = audio_outbuf_size / c->channels;
380 switch(st->codec->codec_id)
381 {
382 case CODEC_ID_PCM_S16LE:
383 case CODEC_ID_PCM_S16BE:
384 case CODEC_ID_PCM_U16LE:
385 case CODEC_ID_PCM_U16BE:
386 audio_input_frame_size >>= 1;
387 break;
388 default:
389 break;
390 }
391 }
392 else
393 {
394 audio_input_frame_size = c->frame_size;
395 }
396
397 // Some formats want stream headers to be seperate (hmm)
398 if( !strcmp( oc->oformat->name, "mp4" ) ||
399 !strcmp( oc->oformat->name, "mov" ) ||
400 !strcmp( oc->oformat->name, "3gp" ) )
401 c->flags |= CODEC_FLAG_GLOBAL_HEADER;
402 }
403 else
404 {
405 fprintf( stderr, "Unable to encode audio - disabling audio output.\n" );
406 }
407
408 return audio_input_frame_size;
409 }
410
411 static void close_audio( AVFormatContext *oc, AVStream *st )
412 {
413 avcodec_close( st->codec );
414 }
415
416 /** Add a video output stream
417 */
418
419 static AVStream *add_video_stream( mlt_consumer this, AVFormatContext *oc, int codec_id )
420 {
421 // Get the properties
422 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
423
424 // Create a new stream
425 AVStream *st = av_new_stream( oc, 0 );
426
427 if ( st != NULL )
428 {
429 char *pix_fmt = mlt_properties_get( properties, "pix_fmt" );
430 double ar = mlt_properties_get_double( properties, "display_ratio" );
431 AVCodecContext *c = st->codec;
432 c->codec_id = codec_id;
433 c->codec_type = CODEC_TYPE_VIDEO;
434
435 // put sample parameters
436 c->bit_rate = mlt_properties_get_int( properties, "video_bit_rate" );
437 c->bit_rate_tolerance = mlt_properties_get_int( properties, "video_bit_rate_tolerance" );
438 c->width = mlt_properties_get_int( properties, "width" );
439 c->height = mlt_properties_get_int( properties, "height" );
440 c->time_base.num = mlt_properties_get_int( properties, "frame_rate_den" );
441 c->time_base.den = mlt_properties_get_int( properties, "frame_rate_num" );
442 c->gop_size = mlt_properties_get_int( properties, "gop_size" );
443 c->pix_fmt = pix_fmt ? avcodec_get_pix_fmt( pix_fmt ) : PIX_FMT_YUV420P;
444
445 if ( mlt_properties_get_int( properties, "b_frames" ) )
446 {
447 c->max_b_frames = mlt_properties_get_int( properties, "b_frames" );
448 c->b_frame_strategy = 0;
449 c->b_quant_factor = 2.0;
450 }
451
452 c->mb_decision = mlt_properties_get_int( properties, "mb_decision" );
453 c->sample_aspect_ratio = av_d2q( ar * c->height / c->width , 255);
454 c->mb_cmp = mlt_properties_get_int( properties, "mb_cmp" );
455 c->ildct_cmp = mlt_properties_get_int( properties, "ildct_cmp" );
456 c->me_sub_cmp = mlt_properties_get_int( properties, "sub_cmp" );
457 c->me_cmp = mlt_properties_get_int( properties, "cmp" );
458 c->me_pre_cmp = mlt_properties_get_int( properties, "pre_cmp" );
459 c->pre_me = mlt_properties_get_int( properties, "pre_me" );
460 c->lumi_masking = mlt_properties_get_double( properties, "lumi_mask" );
461 c->dark_masking = mlt_properties_get_double( properties, "dark_mask" );
462 c->spatial_cplx_masking = mlt_properties_get_double( properties, "scplx_mask" );
463 c->temporal_cplx_masking = mlt_properties_get_double( properties, "tcplx_mask" );
464 c->p_masking = mlt_properties_get_double( properties, "p_mask" );
465 c->quantizer_noise_shaping= mlt_properties_get_int( properties, "qns" );
466 c->qmin = mlt_properties_get_int( properties, "video_qmin" );
467 c->qmax = mlt_properties_get_int( properties, "video_qmax" );
468 c->lmin = mlt_properties_get_int( properties, "video_lmin" );
469 c->lmax = mlt_properties_get_int( properties, "video_lmax" );
470 c->mb_qmin = mlt_properties_get_int( properties, "video_mb_qmin" );
471 c->mb_qmax = mlt_properties_get_int( properties, "video_mb_qmax" );
472 c->max_qdiff = mlt_properties_get_int( properties, "video_qdiff" );
473 c->qblur = mlt_properties_get_double( properties, "video_qblur" );
474 c->qcompress = mlt_properties_get_double( properties, "video_qcomp" );
475
476 if ( mlt_properties_get_double( properties, "qscale" ) > 0 )
477 {
478 c->flags |= CODEC_FLAG_QSCALE;
479 st->quality = FF_QP2LAMBDA * mlt_properties_get_double( properties, "qscale" );
480 }
481
482 // Allow the user to override the video fourcc
483 if ( mlt_properties_get( properties, "vfourcc" ) )
484 {
485 char *tail = NULL;
486 const char *arg = mlt_properties_get( properties, "vfourcc" );
487 int tag = strtol( arg, &tail, 0);
488 if( !tail || *tail )
489 tag = arg[ 0 ] + ( arg[ 1 ] << 8 ) + ( arg[ 2 ] << 16 ) + ( arg[ 3 ] << 24 );
490 c->codec_tag = tag;
491 }
492
493 // Some formats want stream headers to be seperate
494 if ( oc->oformat->flags & AVFMT_GLOBALHEADER )
495 c->flags |= CODEC_FLAG_GLOBAL_HEADER;
496
497 c->rc_max_rate = mlt_properties_get_int( properties, "video_rc_max_rate" );
498 c->rc_min_rate = mlt_properties_get_int( properties, "video_rc_min_rate" );
499 c->rc_buffer_size = mlt_properties_get_int( properties, "video_rc_buffer_size" );
500 c->rc_initial_buffer_occupancy = c->rc_buffer_size*3/4;
501 c->rc_buffer_aggressivity= mlt_properties_get_double( properties, "video_rc_buffer_aggressivity" );
502 c->rc_initial_cplx= mlt_properties_get_double( properties, "video_rc_initial_cplx" );
503 c->i_quant_factor = mlt_properties_get_double( properties, "video_i_qfactor" );
504 c->b_quant_factor = mlt_properties_get_double( properties, "video_b_qfactor" );
505 c->i_quant_offset = mlt_properties_get_double( properties, "video_i_qoffset" );
506 c->b_quant_offset = mlt_properties_get_double( properties, "video_b_qoffset" );
507 c->intra_quant_bias = mlt_properties_get_int( properties, "video_intra_quant_bias" );
508 c->inter_quant_bias = mlt_properties_get_int( properties, "video_inter_quant_bias" );
509 c->dct_algo = mlt_properties_get_int( properties, "dct_algo" );
510 c->idct_algo = mlt_properties_get_int( properties, "idct_algo" );
511 c->me_threshold= mlt_properties_get_int( properties, "me_threshold" );
512 c->mb_threshold= mlt_properties_get_int( properties, "mb_threshold" );
513 c->intra_dc_precision= mlt_properties_get_int( properties, "intra_dc_precision" );
514 c->strict_std_compliance = mlt_properties_get_int( properties, "strict" );
515 c->error_rate = mlt_properties_get_int( properties, "error_rate" );
516 c->noise_reduction= mlt_properties_get_int( properties, "noise_reduction" );
517 c->scenechange_threshold= mlt_properties_get_int( properties, "sc_threshold" );
518 c->me_range = mlt_properties_get_int( properties, "me_range" );
519 c->coder_type= mlt_properties_get_int( properties, "coder" );
520 c->context_model= mlt_properties_get_int( properties, "context" );
521 c->prediction_method= mlt_properties_get_int( properties, "predictor" );
522 c->me_method = mlt_properties_get_int( properties, "me_method" );
523 if ( mlt_properties_get_int( properties, "progressive" ) == 0 &&
524 mlt_properties_get_int( properties, "deinterlace" ) == 0 )
525 {
526 if ( mlt_properties_get_int( properties, "ildct" ) )
527 c->flags |= CODEC_FLAG_INTERLACED_DCT;
528 if ( mlt_properties_get_int( properties, "ilme" ) )
529 c->flags |= CODEC_FLAG_INTERLACED_ME;
530 }
531 }
532 else
533 {
534 fprintf( stderr, "Could not allocate a stream for video\n" );
535 }
536
537 return st;
538 }
539
540 static AVFrame *alloc_picture( int pix_fmt, int width, int height )
541 {
542 // Allocate a frame
543 AVFrame *picture = avcodec_alloc_frame();
544
545 // Determine size of the
546 int size = avpicture_get_size(pix_fmt, width, height);
547
548 // Allocate the picture buf
549 uint8_t *picture_buf = av_malloc(size);
550
551 // If we have both, then fill the image
552 if ( picture != NULL && picture_buf != NULL )
553 {
554 // Fill the frame with the allocated buffer
555 avpicture_fill( (AVPicture *)picture, picture_buf, pix_fmt, width, height);
556 }
557 else
558 {
559 // Something failed - clean up what we can
560 av_free( picture );
561 av_free( picture_buf );
562 picture = NULL;
563 }
564
565 return picture;
566 }
567
568 static int open_video(AVFormatContext *oc, AVStream *st)
569 {
570 // Get the codec
571 AVCodecContext *video_enc = st->codec;
572
573 // find the video encoder
574 AVCodec *codec = avcodec_find_encoder( video_enc->codec_id );
575
576 if( codec && codec->pix_fmts )
577 {
578 const enum PixelFormat *p = codec->pix_fmts;
579 for( ; *p!=-1; p++ )
580 {
581 if( *p == video_enc->pix_fmt )
582 break;
583 }
584 if( *p == -1 )
585 video_enc->pix_fmt = codec->pix_fmts[ 0 ];
586 }
587
588 // Open the codec safely
589 return codec != NULL && avcodec_open( video_enc, codec ) >= 0;
590 }
591
592 void close_video(AVFormatContext *oc, AVStream *st)
593 {
594 avcodec_close(st->codec);
595 }
596
597 static inline long time_difference( struct timeval *time1 )
598 {
599 struct timeval time2;
600 gettimeofday( &time2, NULL );
601 return time2.tv_sec * 1000000 + time2.tv_usec - time1->tv_sec * 1000000 - time1->tv_usec;
602 }
603
604 /** The main thread - the argument is simply the consumer.
605 */
606
607 static void *consumer_thread( void *arg )
608 {
609 // Map the argument to the object
610 mlt_consumer this = arg;
611
612 // Get the properties
613 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
614
615 // Get the terminate on pause property
616 int terminate_on_pause = mlt_properties_get_int( properties, "terminate_on_pause" );
617 int terminated = 0;
618
619 // Determine if feed is slow (for realtime stuff)
620 int real_time_output = mlt_properties_get_int( properties, "real_time" );
621
622 // Time structures
623 struct timeval ante;
624
625 // Get the frame rate
626 double fps = mlt_properties_get_double( properties, "fps" );
627
628 // Get width and height
629 int width = mlt_properties_get_int( properties, "width" );
630 int height = mlt_properties_get_int( properties, "height" );
631 int img_width = width;
632 int img_height = height;
633
634 // Get default audio properties
635 mlt_audio_format aud_fmt = mlt_audio_pcm;
636 int channels = mlt_properties_get_int( properties, "channels" );
637 int frequency = mlt_properties_get_int( properties, "frequency" );
638 int16_t *pcm = NULL;
639 int samples = 0;
640
641 // AVFormat audio buffer and frame size
642 int audio_outbuf_size = 10000;
643 uint8_t *audio_outbuf = av_malloc( audio_outbuf_size );
644 int audio_input_frame_size = 0;
645
646 // AVFormat video buffer and frame count
647 int frame_count = 0;
648 int video_outbuf_size = ( 1024 * 1024 );
649 uint8_t *video_outbuf = av_malloc( video_outbuf_size );
650
651 // Used for the frame properties
652 mlt_frame frame = NULL;
653 mlt_properties frame_properties = NULL;
654
655 // Get the queues
656 mlt_deque queue = mlt_properties_get_data( properties, "frame_queue", NULL );
657 sample_fifo fifo = mlt_properties_get_data( properties, "sample_fifo", NULL );
658
659 // Need two av pictures for converting
660 AVFrame *output = NULL;
661 AVFrame *input = alloc_picture( PIX_FMT_YUV422, width, height );
662
663 // For receiving images from an mlt_frame
664 uint8_t *image;
665 mlt_image_format img_fmt = mlt_image_yuv422;
666
667 // For receiving audio samples back from the fifo
668 int16_t *buffer = av_malloc( 48000 * 2 );
669 int count = 0;
670
671 // Allocate the context
672 AVFormatContext *oc = av_alloc_format_context( );
673
674 // Streams
675 AVStream *audio_st = NULL;
676 AVStream *video_st = NULL;
677
678 // Time stamps
679 double audio_pts = 0;
680 double video_pts = 0;
681
682 // Loop variable
683 int i;
684
685 // Frames despatched
686 long int frames = 0;
687 long int total_time = 0;
688
689 // Determine the format
690 AVOutputFormat *fmt = NULL;
691 char *filename = mlt_properties_get( properties, "target" );
692 char *format = mlt_properties_get( properties, "format" );
693 char *vcodec = mlt_properties_get( properties, "vcodec" );
694 char *acodec = mlt_properties_get( properties, "acodec" );
695
696 // Used to store and override codec ids
697 int audio_codec_id;
698 int video_codec_id;
699
700 // Check for user selected format first
701 if ( format != NULL )
702 fmt = guess_format( format, NULL, NULL );
703
704 // Otherwise check on the filename
705 if ( fmt == NULL && filename != NULL )
706 fmt = guess_format( NULL, filename, NULL );
707
708 // Otherwise default to mpeg
709 if ( fmt == NULL )
710 fmt = guess_format( "mpeg", NULL, NULL );
711
712 // We need a filename - default to stdout?
713 if ( filename == NULL || !strcmp( filename, "" ) )
714 filename = "pipe:";
715
716 // Get the codec ids selected
717 audio_codec_id = fmt->audio_codec;
718 video_codec_id = fmt->video_codec;
719
720 // Check for audio codec overides
721 if ( acodec != NULL )
722 {
723 AVCodec *p = first_avcodec;
724 while( p != NULL )
725 {
726 if ( !strcmp( p->name, acodec ) && p->type == CODEC_TYPE_AUDIO )
727 break;
728 p = p->next;
729 }
730 if ( p != NULL )
731 audio_codec_id = p->id;
732 else
733 fprintf( stderr, "consumer_avcodec: audio codec %s unrecognised - ignoring\n", acodec );
734 }
735
736 // Check for video codec overides
737 if ( vcodec != NULL )
738 {
739 AVCodec *p = first_avcodec;
740 while( p != NULL )
741 {
742 if ( !strcmp( p->name, vcodec ) && p->type == CODEC_TYPE_VIDEO )
743 break;
744 p = p->next;
745 }
746 if ( p != NULL )
747 video_codec_id = p->id;
748 else
749 fprintf( stderr, "consumer_avcodec: video codec %s unrecognised - ignoring\n", vcodec );
750 }
751
752 // Update the output context
753
754 // Write metadata
755 char *tmp = NULL;
756 int metavalue;
757
758 tmp = mlt_properties_get( properties, "meta.attr.title.markup");
759 if (tmp != NULL) snprintf( oc->title, sizeof(oc->title), "%s", tmp );
760
761 tmp = mlt_properties_get( properties, "meta.attr.comment.markup");
762 if (tmp != NULL) snprintf( oc->comment, sizeof(oc->comment), "%s", tmp );
763
764 tmp = mlt_properties_get( properties, "meta.attr.author.markup");
765 if (tmp != NULL) snprintf( oc->author, sizeof(oc->author), "%s", tmp );
766
767 tmp = mlt_properties_get( properties, "meta.attr.copyright.markup");
768 if (tmp != NULL) snprintf( oc->copyright, sizeof(oc->copyright), "%s", tmp );
769
770 tmp = mlt_properties_get( properties, "meta.attr.album.markup");
771 if (tmp != NULL) snprintf( oc->album, sizeof(oc->album), "%s", tmp );
772
773 metavalue = mlt_properties_get_int( properties, "meta.attr.year.markup");
774 if (metavalue != 0) oc->year = metavalue;
775
776 metavalue = mlt_properties_get_int( properties, "meta.attr.track.markup");
777 if (metavalue != 0) oc->track = metavalue;
778
779 oc->oformat = fmt;
780 snprintf( oc->filename, sizeof(oc->filename), "%s", filename );
781
782 // Add audio and video streams
783 if ( fmt->video_codec != CODEC_ID_NONE )
784 video_st = add_video_stream( this, oc, video_codec_id );
785 if ( fmt->audio_codec != CODEC_ID_NONE )
786 audio_st = add_audio_stream( this, oc, audio_codec_id );
787
788 // Set the parameters (even though we have none...)
789 if ( av_set_parameters(oc, NULL) >= 0 )
790 {
791 if ( video_st && !open_video( oc, video_st ) )
792 video_st = NULL;
793 if ( audio_st )
794 audio_input_frame_size = open_audio( oc, audio_st, audio_outbuf_size );
795
796 // Open the output file, if needed
797 if ( !( fmt->flags & AVFMT_NOFILE ) )
798 {
799 if (url_fopen(&oc->pb, filename, URL_WRONLY) < 0)
800 {
801 fprintf(stderr, "Could not open '%s'\n", filename);
802 mlt_properties_set_int( properties, "running", 0 );
803 }
804 }
805
806 // Write the stream header, if any
807 if ( mlt_properties_get_int( properties, "running" ) )
808 av_write_header( oc );
809 }
810 else
811 {
812 fprintf(stderr, "Invalid output format parameters\n");
813 mlt_properties_set_int( properties, "running", 0 );
814 }
815
816 // Allocate picture
817 if ( video_st )
818 output = alloc_picture( video_st->codec->pix_fmt, width, height );
819
820 // Last check - need at least one stream
821 if ( audio_st == NULL && video_st == NULL )
822 mlt_properties_set_int( properties, "running", 0 );
823
824 // Get the starting time (can ignore the times above)
825 gettimeofday( &ante, NULL );
826
827 // Loop while running
828 while( mlt_properties_get_int( properties, "running" ) && !terminated )
829 {
830 // Get the frame
831 frame = mlt_consumer_rt_frame( this );
832
833 // Check that we have a frame to work with
834 if ( frame != NULL )
835 {
836 // Increment frames despatched
837 frames ++;
838
839 // Default audio args
840 frame_properties = MLT_FRAME_PROPERTIES( frame );
841
842 // Check for the terminated condition
843 terminated = terminate_on_pause && mlt_properties_get_double( frame_properties, "_speed" ) == 0.0;
844
845 // Get audio and append to the fifo
846 if ( !terminated && audio_st )
847 {
848 samples = mlt_sample_calculator( fps, frequency, count ++ );
849 mlt_frame_get_audio( frame, &pcm, &aud_fmt, &frequency, &channels, &samples );
850
851 // Create the fifo if we don't have one
852 if ( fifo == NULL )
853 {
854 fifo = sample_fifo_init( frequency, channels );
855 mlt_properties_set_data( properties, "sample_fifo", fifo, 0, ( mlt_destructor )sample_fifo_close, NULL );
856 }
857
858 if ( mlt_properties_get_double( frame_properties, "_speed" ) != 1.0 )
859 memset( pcm, 0, samples * channels * 2 );
860
861 // Append the samples
862 sample_fifo_append( fifo, pcm, samples * channels );
863 total_time += ( samples * 1000000 ) / frequency;
864 }
865
866 // Encode the image
867 if ( !terminated && video_st )
868 mlt_deque_push_back( queue, frame );
869 else
870 mlt_frame_close( frame );
871 }
872
873 // While we have stuff to process, process...
874 while ( 1 )
875 {
876 if (audio_st)
877 audio_pts = (double)audio_st->pts.val * audio_st->time_base.num / audio_st->time_base.den;
878 else
879 audio_pts = 0.0;
880
881 if (video_st)
882 video_pts = (double)video_st->pts.val * video_st->time_base.num / video_st->time_base.den;
883 else
884 video_pts = 0.0;
885
886 // Write interleaved audio and video frames
887 if ( !video_st || ( video_st && audio_st && audio_pts < video_pts ) )
888 {
889 if ( channels * audio_input_frame_size < sample_fifo_used( fifo ) )
890 {
891 AVCodecContext *c;
892 AVPacket pkt;
893 av_init_packet( &pkt );
894
895 c = audio_st->codec;
896
897 sample_fifo_fetch( fifo, buffer, channels * audio_input_frame_size );
898
899 pkt.size = avcodec_encode_audio( c, audio_outbuf, audio_outbuf_size, buffer );
900 // Write the compressed frame in the media file
901 if ( c->coded_frame && c->coded_frame->pts != AV_NOPTS_VALUE )
902 pkt.pts = av_rescale_q( c->coded_frame->pts, c->time_base, audio_st->time_base );
903 pkt.flags |= PKT_FLAG_KEY;
904 pkt.stream_index= audio_st->index;
905 pkt.data= audio_outbuf;
906
907 if ( pkt.size )
908 if ( av_interleaved_write_frame( oc, &pkt ) != 0)
909 fprintf(stderr, "Error while writing audio frame\n");
910
911 audio_pts += c->frame_size;
912 }
913 else
914 {
915 break;
916 }
917 }
918 else if ( video_st )
919 {
920 if ( mlt_deque_count( queue ) )
921 {
922 int out_size, ret;
923 AVCodecContext *c;
924
925 frame = mlt_deque_pop_front( queue );
926 frame_properties = MLT_FRAME_PROPERTIES( frame );
927
928 c = video_st->codec;
929
930 if ( mlt_properties_get_int( frame_properties, "rendered" ) )
931 {
932 int i = 0;
933 int j = 0;
934 uint8_t *p;
935 uint8_t *q;
936
937 mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
938
939 mlt_frame_get_image( frame, &image, &img_fmt, &img_width, &img_height, 0 );
940
941 q = image;
942
943 // Convert the mlt frame to an AVPicture
944 for ( i = 0; i < height; i ++ )
945 {
946 p = input->data[ 0 ] + i * input->linesize[ 0 ];
947 j = width;
948 while( j -- )
949 {
950 *p ++ = *q ++;
951 *p ++ = *q ++;
952 }
953 }
954
955 // Do the colour space conversion
956 #ifdef SWSCALE
957 struct SwsContext *context = sws_getContext( width, height, PIX_FMT_YUV422,
958 width, height, video_st->codec->pix_fmt, SWS_FAST_BILINEAR, NULL, NULL, NULL);
959 sws_scale( context, input->data, input->linesize, 0, height,
960 output->data, output->linesize);
961 sws_freeContext( context );
962 #else
963 img_convert( ( AVPicture * )output, video_st->codec->pix_fmt, ( AVPicture * )input, PIX_FMT_YUV422, width, height );
964 #endif
965
966 // Apply the alpha if applicable
967 if ( video_st->codec->pix_fmt == PIX_FMT_RGBA32 )
968 {
969 uint8_t *alpha = mlt_frame_get_alpha_mask( frame );
970 register int n;
971
972 for ( i = 0; i < height; i ++ )
973 {
974 n = ( width + 7 ) / 8;
975 p = output->data[ 0 ] + i * output->linesize[ 0 ];
976
977 #ifndef __DARWIN__
978 p += 3;
979 #endif
980
981 switch( width % 8 )
982 {
983 case 0: do { *p = *alpha++; p += 4;
984 case 7: *p = *alpha++; p += 4;
985 case 6: *p = *alpha++; p += 4;
986 case 5: *p = *alpha++; p += 4;
987 case 4: *p = *alpha++; p += 4;
988 case 3: *p = *alpha++; p += 4;
989 case 2: *p = *alpha++; p += 4;
990 case 1: *p = *alpha++; p += 4;
991 }
992 while( --n );
993 }
994 }
995 }
996 }
997
998 if (oc->oformat->flags & AVFMT_RAWPICTURE)
999 {
1000 // raw video case. The API will change slightly in the near future for that
1001 AVPacket pkt;
1002 av_init_packet(&pkt);
1003
1004 pkt.flags |= PKT_FLAG_KEY;
1005 pkt.stream_index= video_st->index;
1006 pkt.data= (uint8_t *)output;
1007 pkt.size= sizeof(AVPicture);
1008
1009 ret = av_write_frame(oc, &pkt);
1010 video_pts += c->frame_size;
1011 }
1012 else
1013 {
1014 // Set the quality
1015 output->quality = video_st->quality;
1016
1017 // Set frame interlace hints
1018 output->interlaced_frame = !mlt_properties_get_int( frame_properties, "progressive" );
1019 output->top_field_first = mlt_properties_get_int( frame_properties, "top_field_first" );
1020
1021 // Encode the image
1022 out_size = avcodec_encode_video(c, video_outbuf, video_outbuf_size, output );
1023
1024 // If zero size, it means the image was buffered
1025 if (out_size > 0)
1026 {
1027 AVPacket pkt;
1028 av_init_packet( &pkt );
1029
1030 if ( c->coded_frame && c->coded_frame->pts != AV_NOPTS_VALUE )
1031 pkt.pts= av_rescale_q( c->coded_frame->pts, c->time_base, video_st->time_base );
1032 if( c->coded_frame && c->coded_frame->key_frame )
1033 pkt.flags |= PKT_FLAG_KEY;
1034 pkt.stream_index= video_st->index;
1035 pkt.data= video_outbuf;
1036 pkt.size= out_size;
1037
1038 // write the compressed frame in the media file
1039 ret = av_interleaved_write_frame(oc, &pkt);
1040 video_pts += c->frame_size;
1041 }
1042 else
1043 {
1044 fprintf( stderr, "Error with video encode\n" );
1045 }
1046 }
1047 frame_count++;
1048 mlt_frame_close( frame );
1049 }
1050 else
1051 {
1052 break;
1053 }
1054 }
1055 }
1056
1057 if ( real_time_output && frames % 12 == 0 )
1058 {
1059 long passed = time_difference( &ante );
1060 if ( fifo != NULL )
1061 {
1062 long pending = ( ( ( long )sample_fifo_used( fifo ) * 1000 ) / frequency ) * 1000;
1063 passed -= pending;
1064 }
1065 if ( passed < total_time )
1066 {
1067 long total = ( total_time - passed );
1068 struct timespec t = { total / 1000000, ( total % 1000000 ) * 1000 };
1069 nanosleep( &t, NULL );
1070 }
1071 }
1072 }
1073
1074 #ifdef FLUSH
1075 if ( ! real_time_output )
1076 {
1077 // Flush audio fifo
1078 if ( audio_st && audio_st->codec->frame_size > 1 ) for (;;)
1079 {
1080 AVCodecContext *c = audio_st->codec;
1081 AVPacket pkt;
1082 av_init_packet( &pkt );
1083 pkt.size = 0;
1084
1085 if ( /*( c->capabilities & CODEC_CAP_SMALL_LAST_FRAME ) &&*/
1086 ( channels * audio_input_frame_size < sample_fifo_used( fifo ) ) )
1087 {
1088 sample_fifo_fetch( fifo, buffer, channels * audio_input_frame_size );
1089 pkt.size = avcodec_encode_audio( c, audio_outbuf, audio_outbuf_size, buffer );
1090 }
1091 if ( pkt.size <= 0 )
1092 pkt.size = avcodec_encode_audio( c, audio_outbuf, audio_outbuf_size, NULL );
1093 if ( pkt.size <= 0 )
1094 break;
1095
1096 // Write the compressed frame in the media file
1097 if ( c->coded_frame && c->coded_frame->pts != AV_NOPTS_VALUE )
1098 pkt.pts = av_rescale_q( c->coded_frame->pts, c->time_base, audio_st->time_base );
1099 pkt.flags |= PKT_FLAG_KEY;
1100 pkt.stream_index = audio_st->index;
1101 pkt.data = audio_outbuf;
1102 if ( av_interleaved_write_frame( oc, &pkt ) != 0 )
1103 {
1104 fprintf(stderr, "Error while writing flushed audio frame\n");
1105 break;
1106 }
1107 }
1108
1109 // Flush video
1110 if ( video_st && !( oc->oformat->flags & AVFMT_RAWPICTURE ) ) for (;;)
1111 {
1112 AVCodecContext *c = video_st->codec;
1113 AVPacket pkt;
1114 av_init_packet( &pkt );
1115
1116 // Encode the image
1117 pkt.size = avcodec_encode_video( c, video_outbuf, video_outbuf_size, NULL );
1118 if ( pkt.size <= 0 )
1119 break;
1120
1121 if ( c->coded_frame && c->coded_frame->pts != AV_NOPTS_VALUE )
1122 pkt.pts= av_rescale_q( c->coded_frame->pts, c->time_base, video_st->time_base );
1123 if( c->coded_frame && c->coded_frame->key_frame )
1124 pkt.flags |= PKT_FLAG_KEY;
1125 pkt.stream_index = video_st->index;
1126 pkt.data = video_outbuf;
1127
1128 // write the compressed frame in the media file
1129 if ( av_interleaved_write_frame( oc, &pkt ) != 0 )
1130 {
1131 fprintf(stderr, "Error while writing flushed video frame\n");
1132 break;
1133 }
1134 }
1135 }
1136 #endif
1137
1138 // close each codec
1139 if (video_st)
1140 close_video(oc, video_st);
1141 if (audio_st)
1142 close_audio(oc, audio_st);
1143
1144 // Write the trailer, if any
1145 av_write_trailer(oc);
1146
1147 // Free the streams
1148 for(i = 0; i < oc->nb_streams; i++)
1149 av_freep(&oc->streams[i]);
1150
1151 // Close the output file
1152 if (!(fmt->flags & AVFMT_NOFILE))
1153 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(0<<8)+0)
1154 url_fclose(oc->pb);
1155 #else
1156 url_fclose(&oc->pb);
1157 #endif
1158
1159 // Clean up input and output frames
1160 if ( output )
1161 av_free( output->data[0] );
1162 av_free( output );
1163 av_free( input->data[0] );
1164 av_free( input );
1165 av_free( video_outbuf );
1166 av_free( buffer );
1167
1168 // Free the stream
1169 av_free(oc);
1170
1171 // Just in case we terminated on pause
1172 mlt_properties_set_int( properties, "running", 0 );
1173
1174 mlt_consumer_stopped( this );
1175
1176 return NULL;
1177 }
1178
1179 /** Close the consumer.
1180 */
1181
1182 static void consumer_close( mlt_consumer this )
1183 {
1184 // Stop the consumer
1185 mlt_consumer_stop( this );
1186
1187 // Close the parent
1188 mlt_consumer_close( this );
1189
1190 // Free the memory
1191 free( this );
1192 }