]> git.sesse.net Git - mlt/blob - src/modules/avformat/consumer_avformat.c
Minor optimisations, consumer avformat experimentation
[mlt] / 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 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.
10  *
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.
15  *
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.
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 <pthread.h>
32
33 #include <math.h>
34
35 // avformat header files
36 #include <ffmpeg/avformat.h>
37
38 typedef struct
39 {
40         int16_t *buffer;
41         int size;
42         int used;
43 }
44 *sample_fifo, sample_fifo_s;
45
46 sample_fifo sample_fifo_init( )
47 {
48         return calloc( 1, sizeof( sample_fifo_s ) );
49 }
50
51 void sample_fifo_append( sample_fifo this, int16_t *samples, int count )
52 {
53         if ( ( this->size - this->used ) < count )
54         {
55                 this->size += count * 5;
56                 this->buffer = realloc( this->buffer, this->size * sizeof( int16_t ) );
57         }
58
59         memcpy( &this->buffer[ this->used ], samples, count * sizeof( int16_t ) );
60         this->used += count;
61 }
62
63 int sample_fifo_used( sample_fifo this )
64 {
65         return this->used;
66 }
67
68 int sample_fifo_fetch( sample_fifo this, int16_t *samples, int count )
69 {
70         if ( count > this->used )
71                 count = this->used;
72
73         memcpy( samples, this->buffer, count * sizeof( int16_t ) );
74         this->used -= count;
75         memmove( this->buffer, &this->buffer[ count ], this->used * sizeof( int16_t ) );
76
77         return count;
78 }
79
80 void sample_fifo_close( sample_fifo this )
81 {
82         free( this->buffer );
83         free( this );
84 }
85
86 // Forward references.
87 static int consumer_start( mlt_consumer this );
88 static int consumer_stop( mlt_consumer this );
89 static int consumer_is_stopped( mlt_consumer this );
90 static void *consumer_thread( void *arg );
91 static void consumer_close( mlt_consumer this );
92
93 /** Initialise the dv consumer.
94 */
95
96 mlt_consumer consumer_avformat_init( char *arg )
97 {
98         // Allocate the consumer
99         mlt_consumer this = calloc( 1, sizeof( struct mlt_consumer_s ) );
100
101         // If memory allocated and initialises without error
102         if ( this != NULL && mlt_consumer_init( this, NULL ) == 0 )
103         {
104                 // Get properties from the consumer
105                 mlt_properties properties = mlt_consumer_properties( this );
106
107                 // Assign close callback
108                 this->close = consumer_close;
109
110                 // Interpret the argument
111                 if ( arg != NULL )
112                         mlt_properties_set( properties, "target", arg );
113
114                 // sample and frame queue
115                 mlt_properties_set_data( properties, "sample_fifo", sample_fifo_init( ), 0, ( mlt_destructor )sample_fifo_close, NULL );
116                 mlt_properties_set_data( properties, "frame_queue", mlt_deque_init( ), 0, ( mlt_destructor )mlt_deque_close, NULL );
117
118                 // Set avformat defaults
119                 mlt_properties_set_int( properties, "audio_bit_rate", 128000 );
120                 mlt_properties_set_int( properties, "video_bit_rate", 200 * 1000 );
121                 mlt_properties_set_int( properties, "video_bit_rate_tolerance", 4000 * 1000 );
122                 mlt_properties_set_int( properties, "frame_rate_base", 1 );
123                 mlt_properties_set_int( properties, "gop_size", 12 );
124                 mlt_properties_set_int( properties, "max_b_frames", 0 );
125                 mlt_properties_set_int( properties, "mb_decision", 0 );
126                 mlt_properties_set_double( properties, "qscale", 0 );
127                 mlt_properties_set_int( properties, "me_method", ME_EPZS );
128
129                 // Ensure termination at end of the stream
130                 mlt_properties_set_int( properties, "terminate_on_pause", 1 );
131
132                 // Set up start/stop/terminated callbacks
133                 this->start = consumer_start;
134                 this->stop = consumer_stop;
135                 this->is_stopped = consumer_is_stopped;
136         }
137         else
138         {
139                 // Clean up in case of init failure
140                 free( this );
141                 this = NULL;
142         }
143
144         // Return this
145         return this;
146 }
147
148 /** Start the consumer.
149 */
150
151 static int consumer_start( mlt_consumer this )
152 {
153         // Get the properties
154         mlt_properties properties = mlt_consumer_properties( this );
155
156         // Check that we're not already running
157         if ( !mlt_properties_get_int( properties, "running" ) )
158         {
159                 // Allocate a thread
160                 pthread_t *thread = calloc( 1, sizeof( pthread_t ) );
161                 pthread_attr_t thread_attributes;
162
163                 // Get the width and height
164                 int width = mlt_properties_get_int( properties, "width" );
165                 int height = mlt_properties_get_int( properties, "height" );
166
167                 // Obtain the size property
168                 char *size = mlt_properties_get( properties, "size" );
169
170                 // Interpret it
171                 if ( size != NULL )
172                 {
173                         int tw, th;
174                         if ( sscanf( size, "%dx%d", &tw, &th ) == 2 && tw > 0 && th > 0 )
175                         {
176                                 width = tw;
177                                 height = th;
178                         }
179                         else
180                         {
181                                 fprintf( stderr, "consumer_avformat: Invalid size property %s - ignoring.\n", size );
182                         }
183                 }
184                 
185                 // Now ensure we honour the multiple of two requested by libavformat
186                 mlt_properties_set_int( properties, "width", ( width / 2 ) * 2 );
187                 mlt_properties_set_int( properties, "height", ( height / 2 ) * 2 );
188
189                 // Assign the thread to properties
190                 mlt_properties_set_data( properties, "thread", thread, sizeof( pthread_t ), free, NULL );
191
192                 // Set the running state
193                 mlt_properties_set_int( properties, "running", 1 );
194
195                 // Inherit the scheduling priority
196                 pthread_attr_init( &thread_attributes );
197                 pthread_attr_setinheritsched( &thread_attributes, PTHREAD_INHERIT_SCHED );
198                 
199                 // Create the thread
200                 pthread_create( thread, &thread_attributes, consumer_thread, this );
201         }
202         return 0;
203 }
204
205 /** Stop the consumer.
206 */
207
208 static int consumer_stop( mlt_consumer this )
209 {
210         // Get the properties
211         mlt_properties properties = mlt_consumer_properties( this );
212
213         // Check that we're running
214         if ( mlt_properties_get_int( properties, "running" ) )
215         {
216                 // Get the thread
217                 pthread_t *thread = mlt_properties_get_data( properties, "thread", NULL );
218
219                 // Stop the thread
220                 mlt_properties_set_int( properties, "running", 0 );
221
222                 // Wait for termination
223                 pthread_join( *thread, NULL );
224         }
225
226         return 0;
227 }
228
229 /** Determine if the consumer is stopped.
230 */
231
232 static int consumer_is_stopped( mlt_consumer this )
233 {
234         // Get the properties
235         mlt_properties properties = mlt_consumer_properties( this );
236         return !mlt_properties_get_int( properties, "running" );
237 }
238
239 /** Add an audio output stream
240 */
241
242 static AVStream *add_audio_stream( mlt_consumer this, AVFormatContext *oc, int codec_id )
243 {
244         // Get the properties
245         mlt_properties properties = mlt_consumer_properties( this );
246
247         // Create a new stream
248         AVStream *st = av_new_stream( oc, 1 );
249
250         // If created, then initialise from properties
251         if ( st != NULL ) 
252         {
253                 AVCodecContext *c = &st->codec;
254                 c->codec_id = codec_id;
255                 c->codec_type = CODEC_TYPE_AUDIO;
256
257                 // Put sample parameters
258                 c->bit_rate = mlt_properties_get_int( properties, "audio_bit_rate" );
259                 c->sample_rate = mlt_properties_get_int( properties, "frequency" );
260                 c->channels = mlt_properties_get_int( properties, "channels" );
261         }
262         else
263         {
264                 fprintf( stderr, "Could not allocate a stream for audio\n" );
265         }
266
267         return st;
268 }
269
270 static int open_audio( AVFormatContext *oc, AVStream *st, int audio_outbuf_size )
271 {
272         // We will return the audio input size from here
273         int audio_input_frame_size = 0;
274
275         // Get the context
276         AVCodecContext *c = &st->codec;
277
278         // Find the encoder
279         AVCodec *codec = avcodec_find_encoder( c->codec_id );
280
281         // Continue if codec found and we can open it
282         if ( codec != NULL && avcodec_open(c, codec) >= 0 )
283         {
284                 // ugly hack for PCM codecs (will be removed ASAP with new PCM
285                 // support to compute the input frame size in samples
286                 if ( c->frame_size <= 1 ) 
287                 {
288                         audio_input_frame_size = audio_outbuf_size / c->channels;
289                         switch(st->codec.codec_id) 
290                         {
291                                 case CODEC_ID_PCM_S16LE:
292                                 case CODEC_ID_PCM_S16BE:
293                                 case CODEC_ID_PCM_U16LE:
294                                 case CODEC_ID_PCM_U16BE:
295                                         audio_input_frame_size >>= 1;
296                                         break;
297                                 default:
298                                         break;
299                         }
300                 } 
301                 else 
302                 {
303                         audio_input_frame_size = c->frame_size;
304                 }
305
306                 // Some formats want stream headers to be seperate (hmm)
307                 if( !strcmp( oc->oformat->name, "mp4" ) || 
308                         !strcmp( oc->oformat->name, "mov" ) || 
309                         !strcmp( oc->oformat->name, "3gp" ) )
310                         c->flags |= CODEC_FLAG_GLOBAL_HEADER;
311         }
312         else
313         {
314                 fprintf( stderr, "Unable to encode audio - disabling audio output.\n" );
315         }
316
317         return audio_input_frame_size;
318 }
319
320 static void close_audio( AVFormatContext *oc, AVStream *st )
321 {
322         avcodec_close( &st->codec );
323 }
324
325 /** Add a video output stream 
326 */
327
328 static AVStream *add_video_stream( mlt_consumer this, AVFormatContext *oc, int codec_id )
329 {
330         // Get the properties
331         mlt_properties properties = mlt_consumer_properties( this );
332
333         // Create a new stream
334         AVStream *st = av_new_stream( oc, 0 );
335
336         if ( st != NULL ) 
337         {
338                 AVCodecContext *c = &st->codec;
339                 c->codec_id = codec_id;
340                 c->codec_type = CODEC_TYPE_VIDEO;
341
342                 // put sample parameters
343                 c->bit_rate = mlt_properties_get_int( properties, "video_bit_rate" );
344                 c->bit_rate_tolerance = mlt_properties_get_int( properties, "video_bit_rate_tolerance" );
345                 c->width = mlt_properties_get_int( properties, "width" );
346                 c->height = mlt_properties_get_int( properties, "height" );
347                 c->frame_rate = mlt_properties_get_double( properties, "fps" );
348                 c->frame_rate_base = mlt_properties_get_double( properties, "frame_rate_base" );
349                 c->frame_rate_base = 1;
350                 c->gop_size = mlt_properties_get_int( properties, "gop_size" );
351                 c->max_b_frames = mlt_properties_get_int( properties, "max_b_frames" );
352                 if ( c->max_b_frames )
353                 {
354                         c->b_frame_strategy = 0;
355                         c->b_quant_factor = 2.0;
356                 }
357
358                 c->mb_decision = mlt_properties_get_int( properties, "mb_decision" );
359                 c->sample_aspect_ratio = av_d2q( mlt_properties_get_double( properties, "aspect_ratio" ), 255 );
360
361
362                 if ( mlt_properties_get_double( properties, "qscale" ) > 0 )
363                 {
364                         c->flags |= CODEC_FLAG_QSCALE;
365                         st->quality = FF_QP2LAMBDA * mlt_properties_get_double( properties, "qscale" );
366                 }
367
368                 // Some formats want stream headers to be seperate (hmm)
369                 if( !strcmp( oc->oformat->name, "mp4" ) || 
370                         !strcmp( oc->oformat->name, "mov" ) || 
371                         !strcmp( oc->oformat->name, "3gp" ) )
372                         c->flags |= CODEC_FLAG_GLOBAL_HEADER;
373
374                 c->me_method = mlt_properties_get_int( properties, "me_method" );
375         }
376         else
377         {
378                 fprintf( stderr, "Could not allocate a stream for video\n" );
379         }
380  
381         return st;
382 }
383
384 static AVFrame *alloc_picture( int pix_fmt, int width, int height )
385 {
386         // Allocate a frame
387         AVFrame *picture = avcodec_alloc_frame();
388
389         // Determine size of the 
390         int size = avpicture_get_size(pix_fmt, width, height);
391
392         // Allocate the picture buf
393         uint8_t *picture_buf = av_malloc(size);
394
395         // If we have both, then fill the image
396         if ( picture != NULL && picture_buf != NULL )
397         {
398                 // Fill the frame with the allocated buffer
399                 avpicture_fill( (AVPicture *)picture, picture_buf, pix_fmt, width, height);
400         }
401         else
402         {
403                 // Something failed - clean up what we can
404                 av_free( picture );
405                 av_free( picture_buf );
406                 picture = NULL;
407         }
408
409         return picture;
410 }
411         
412 static int open_video(AVFormatContext *oc, AVStream *st)
413 {
414         // Get the codec
415         AVCodecContext *c = &st->codec;
416
417         // find the video encoder
418         AVCodec *codec = avcodec_find_encoder(c->codec_id);
419
420         // Open the codec safely
421         return codec != NULL && avcodec_open(c, codec) >= 0;
422 }
423
424 void close_video(AVFormatContext *oc, AVStream *st)
425 {
426         avcodec_close(&st->codec);
427 }
428
429 /** The main thread - the argument is simply the consumer.
430 */
431
432 static void *consumer_thread( void *arg )
433 {
434         // Map the argument to the object
435         mlt_consumer this = arg;
436
437         // Get the properties
438         mlt_properties properties = mlt_consumer_properties( this );
439
440         // Get the terminate on pause property
441         int terminate_on_pause = mlt_properties_get_int( properties, "terminate_on_pause" );
442
443         // Get the frame rate
444         int fps = mlt_properties_get_double( properties, "fps" );
445
446         // Get width and height
447         int width = mlt_properties_get_int( properties, "width" );
448         int height = mlt_properties_get_int( properties, "height" );
449         int img_width = width;
450         int img_height = height;
451
452         // Get default audio properties
453         mlt_audio_format aud_fmt = mlt_audio_pcm;
454         int channels = mlt_properties_get_int( properties, "channels" );
455         int frequency = mlt_properties_get_int( properties, "frequency" );
456         int16_t *pcm = NULL;
457         int samples = 0;
458
459         // AVFormat audio buffer and frame size
460         int audio_outbuf_size = 2 * 128 * 1024;
461         uint8_t *audio_outbuf = av_malloc( audio_outbuf_size );
462         int audio_input_frame_size = 0;
463
464         // AVFormat video buffer and frame count
465         int frame_count = 0;
466         int video_outbuf_size = ( 1024 * 1024 );
467         uint8_t *video_outbuf = av_malloc( video_outbuf_size );
468
469         // Used for the frame properties
470         mlt_frame frame = NULL;
471         mlt_properties frame_properties = NULL;
472
473         // Get the queues
474         mlt_deque queue = mlt_properties_get_data( properties, "frame_queue", NULL );
475         sample_fifo fifo = mlt_properties_get_data( properties, "sample_fifo", NULL );
476
477         // Need two av pictures for converting
478         AVFrame *output = alloc_picture( PIX_FMT_YUV420P, width, height );
479         AVFrame *input = alloc_picture( PIX_FMT_YUV422, width, height );
480
481         // For receiving images from an mlt_frame
482         uint8_t *image;
483         mlt_image_format img_fmt = mlt_image_yuv422;
484
485         // Fo receiving audio samples back from the fifo
486         int16_t buffer[ 48000 * 2 ];
487         int count = 0;
488
489         // Allocate the context
490         AVFormatContext *oc = av_alloc_format_context( );
491
492         // Streams
493         AVStream *audio_st = NULL;
494         AVStream *video_st = NULL;
495
496         // Time stamps
497         double audio_pts, video_pts;
498
499         // Loop variable
500         int i;
501
502         // Determine the format
503         AVOutputFormat *fmt = NULL;
504         char *filename = mlt_properties_get( properties, "target" );
505         char *format = mlt_properties_get( properties, "format" );
506         char *vcodec = mlt_properties_get( properties, "vcodec" );
507         char *acodec = mlt_properties_get( properties, "acodec" );
508
509         // Used to store and override codec ids
510         int audio_codec_id;
511         int video_codec_id;
512
513         // Check for user selected format first
514         if ( format != NULL )
515                 fmt = guess_format( format, NULL, NULL );
516
517         // Otherwise check on the filename
518         if ( fmt == NULL && filename != NULL )
519                 fmt = guess_format( NULL, filename, NULL );
520
521         // Otherwise default to mpeg
522         if ( fmt == NULL )
523                 fmt = guess_format( "mpeg", NULL, NULL );
524
525         // We need a filename - default to stdout?
526         if ( filename == NULL )
527                 filename = "pipe:";
528
529         // Get the codec ids selected
530         audio_codec_id = fmt->audio_codec;
531         video_codec_id = fmt->video_codec;
532
533         // Check for audio codec overides
534         if ( acodec != NULL )
535         {
536                 AVCodec *p = first_avcodec;
537                 while( p != NULL ) 
538                 {
539                         if ( !strcmp( p->name, acodec ) && p->type == CODEC_TYPE_AUDIO )
540                                 break;
541                         p = p->next;
542                 }
543                 if ( p != NULL )
544                         audio_codec_id = p->id;
545                 else
546                         fprintf( stderr, "consumer_avcodec: audio codec %s unrecognised - ignoring\n", acodec );
547         }
548
549         // Check for video codec overides
550         if ( vcodec != NULL )
551         {
552                 AVCodec *p = first_avcodec;
553                 while( p != NULL ) 
554                 {
555                         if ( !strcmp( p->name, vcodec ) && p->type == CODEC_TYPE_VIDEO )
556                                 break;
557                         p = p->next;
558                 }
559                 if ( p != NULL )
560                         video_codec_id = p->id;
561                 else
562                         fprintf( stderr, "consumer_avcodec: video codec %s unrecognised - ignoring\n", vcodec );
563         }
564
565         // Update the output context
566         oc->oformat = fmt;
567         snprintf( oc->filename, sizeof(oc->filename), "%s", filename );
568
569         // Add audio and video streams 
570         if ( fmt->video_codec != CODEC_ID_NONE )
571                 video_st = add_video_stream( this, oc, video_codec_id );
572         if ( fmt->audio_codec != CODEC_ID_NONE )
573                 audio_st = add_audio_stream( this, oc, audio_codec_id );
574
575         // Set the parameters (even though we have none...)
576         if ( av_set_parameters(oc, NULL) >= 0 ) 
577         {
578                 if ( video_st && !open_video( oc, video_st ) )
579                         video_st = NULL;
580                 if ( audio_st )
581                         audio_input_frame_size = open_audio( oc, audio_st, audio_outbuf_size );
582
583                 // Open the output file, if needed
584                 if ( !( fmt->flags & AVFMT_NOFILE ) ) 
585                 {
586                         if (url_fopen(&oc->pb, filename, URL_RDWR) < 0) 
587                         {
588                                 fprintf(stderr, "Could not open '%s'\n", filename);
589                                 mlt_properties_set_int( properties, "running", 0 );
590                         }
591                 }
592         
593                 if ( url_is_streamed( &oc->pb ) )
594                         fprintf( stderr, "FUCK!\n" );
595
596                 // Write the stream header, if any
597                 if ( mlt_properties_get_int( properties, "running" ) )
598                         av_write_header( oc );
599         }
600         else
601         {
602                 fprintf(stderr, "Invalid output format parameters\n");
603                 mlt_properties_set_int( properties, "running", 0 );
604         }
605
606         // Last check - need at least one stream
607         if ( audio_st == NULL && video_st == NULL )
608                 mlt_properties_set_int( properties, "running", 0 );
609
610         // Loop while running
611         while( mlt_properties_get_int( properties, "running" ) )
612         {
613                 // Get the frame
614                 frame = mlt_consumer_rt_frame( this );
615
616                 // Check that we have a frame to work with
617                 if ( frame != NULL )
618                 {
619                         // Default audio args
620                         frame_properties = mlt_frame_properties( frame );
621
622                         // Get audio and append to the fifo
623                         if ( audio_st )
624                         {
625                                 samples = mlt_sample_calculator( fps, frequency, count );
626                                 mlt_frame_get_audio( frame, &pcm, &aud_fmt, &frequency, &channels, &samples );
627                                 sample_fifo_append( fifo, pcm, samples * channels );
628                         }
629
630                         // Encode the image
631                         if ( video_st )
632                                 mlt_deque_push_back( queue, frame );
633                         else
634                                 mlt_frame_close( frame );
635
636                         // While we have stuff to process, process...
637                         while ( 1 )
638                         {
639                                 // Compute current audio and video time
640                                 if (audio_st)
641                                         audio_pts = (double)audio_st->pts.val * oc->pts_num / oc->pts_den;
642                                 else
643                                         audio_pts = 0.0;
644          
645                                 if (video_st)
646                                         video_pts = (double)video_st->pts.val * oc->pts_num / oc->pts_den;
647                                 else
648                                         video_pts = 0.0;
649
650                                 // Write interleaved audio and video frames
651                                 if ( !video_st || ( video_st && audio_st && audio_pts < video_pts ) )
652                                 {
653                                         if ( channels * audio_input_frame_size < sample_fifo_used( fifo ) )
654                                         {
655                                                 int out_size;
656                                                 AVCodecContext *c;
657
658                                                 c = &audio_st->codec;
659
660                                                 sample_fifo_fetch( fifo, buffer, channels * audio_input_frame_size );
661
662                                                 out_size = avcodec_encode_audio( c, audio_outbuf, audio_outbuf_size, buffer );
663
664                                                 // Write the compressed frame in the media file
665                                                 if (av_write_frame(oc, audio_st->index, audio_outbuf, out_size) != 0) 
666                                                         fprintf(stderr, "Error while writing audio frame\n");
667                                         }
668                                         else
669                                         {
670                                                 break;
671                                         }
672                                 }
673                                 else if ( video_st )
674                                 {
675                                         if ( mlt_deque_count( queue ) )
676                                         {
677                                                 int out_size, ret;
678                                                 AVCodecContext *c;
679  
680                                                 frame = mlt_deque_pop_front( queue );
681                                                 frame_properties = mlt_frame_properties( frame );
682
683                                                 if ( terminate_on_pause && mlt_properties_get_double( frame_properties, "_speed" ) == 0.0 )
684                                                 {
685                                                         mlt_properties_set_int( properties, "running", 0 );
686                                                         break;
687                                                 }
688
689                                                 c = &video_st->codec;
690                                                 
691                                                 if ( mlt_properties_get_int( frame_properties, "rendered" ) )
692                                                 {
693                                                         int i = 0;
694                                                         int j = 0;
695                                                         uint8_t *p;
696                                                         uint8_t *q;
697
698                                                         mlt_frame_get_image( frame, &image, &img_fmt, &img_width, &img_height, 0 );
699
700                                                         q = image;
701
702                                                         for ( i = 0; i < height; i ++ )
703                                                         {
704                                                                 p = input->data[ 0 ] + i * input->linesize[ 0 ];
705                                                                 j = width;
706                                                                 while( j -- )
707                                                                 {
708                                                                         *p ++ = *q ++;
709                                                                         *p ++ = *q ++;
710                                                                 }
711                                                         }
712
713                                                         img_convert( ( AVPicture * )output, PIX_FMT_YUV420P, ( AVPicture * )input, PIX_FMT_YUV422, width, height );
714                                                 }
715  
716                                                 if (oc->oformat->flags & AVFMT_RAWPICTURE) 
717                                                 {
718                                                         // raw video case. The API will change slightly in the near future for that
719                                                         ret = av_write_frame(oc, video_st->index, (uint8_t *)output, sizeof(AVPicture));
720                                                 } 
721                                                 else 
722                                                 {
723                                                         // Encode the image
724                                                         out_size = avcodec_encode_video(c, video_outbuf, video_outbuf_size, output );
725
726                                                         // If zero size, it means the image was buffered
727                                                         if (out_size != 0) 
728                                                         {
729                                                                 // write the compressed frame in the media file
730                                                                 // XXX: in case of B frames, the pts is not yet valid
731                                                                 ret = av_write_frame( oc, video_st->index, video_outbuf, out_size );
732                                                         } 
733                                                 }
734                                                 frame_count++;
735                                                 mlt_frame_close( frame );
736                                         }
737                                         else
738                                         {
739                                                 break;
740                                         }
741                                 }
742                         }
743                 }
744         }
745
746         // close each codec 
747         if (video_st)
748                 close_video(oc, video_st);
749         if (audio_st)
750                 close_audio(oc, audio_st);
751
752         // Write the trailer, if any
753         av_write_trailer(oc);
754
755         // Free the streams
756         for(i = 0; i < oc->nb_streams; i++)
757                 av_freep(&oc->streams[i]);
758
759         // Close the output file
760         if (!(fmt->flags & AVFMT_NOFILE))
761                 url_fclose(&oc->pb);
762
763         // Clean up input and output frames
764         av_free( output->data[0] );
765         av_free( output );
766         av_free( input->data[0] );
767         av_free( input );
768         av_free( video_outbuf );
769
770         // Free the stream
771         av_free(oc);
772
773         return NULL;
774 }
775
776 /** Close the consumer.
777 */
778
779 static void consumer_close( mlt_consumer this )
780 {
781         // Stop the consumer
782         mlt_consumer_stop( this );
783
784         // Close the parent
785         mlt_consumer_close( this );
786
787         // Free the memory
788         free( this );
789 }