avformat-cvs build fix and audio filter correction
[melted] / src / modules / avformat / consumer_avformat.c
index e511da3..021c501 100644 (file)
@@ -141,7 +141,7 @@ mlt_consumer consumer_avformat_init( char *arg )
        if ( this != NULL )
        {
                // Get properties from the consumer
-               mlt_properties properties = mlt_consumer_properties( this );
+               mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
 
                // Assign close callback
                this->close = consumer_close;
@@ -228,14 +228,13 @@ mlt_consumer consumer_avformat_init( char *arg )
 static int consumer_start( mlt_consumer this )
 {
        // Get the properties
-       mlt_properties properties = mlt_consumer_properties( this );
+       mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
 
        // Check that we're not already running
        if ( !mlt_properties_get_int( properties, "running" ) )
        {
                // Allocate a thread
                pthread_t *thread = calloc( 1, sizeof( pthread_t ) );
-               pthread_attr_t thread_attributes;
 
                // Get the width and height
                int width = mlt_properties_get_int( properties, "width" );
@@ -269,12 +268,8 @@ static int consumer_start( mlt_consumer this )
                // Set the running state
                mlt_properties_set_int( properties, "running", 1 );
 
-               // Inherit the scheduling priority
-               pthread_attr_init( &thread_attributes );
-               pthread_attr_setinheritsched( &thread_attributes, PTHREAD_INHERIT_SCHED );
-               
                // Create the thread
-               pthread_create( thread, &thread_attributes, consumer_thread, this );
+               pthread_create( thread, NULL, consumer_thread, this );
        }
        return 0;
 }
@@ -285,7 +280,7 @@ static int consumer_start( mlt_consumer this )
 static int consumer_stop( mlt_consumer this )
 {
        // Get the properties
-       mlt_properties properties = mlt_consumer_properties( this );
+       mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
 
        // Check that we're running
        if ( mlt_properties_get_int( properties, "running" ) )
@@ -309,7 +304,7 @@ static int consumer_stop( mlt_consumer this )
 static int consumer_is_stopped( mlt_consumer this )
 {
        // Get the properties
-       mlt_properties properties = mlt_consumer_properties( this );
+       mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
        return !mlt_properties_get_int( properties, "running" );
 }
 
@@ -319,7 +314,7 @@ static int consumer_is_stopped( mlt_consumer this )
 static AVStream *add_audio_stream( mlt_consumer this, AVFormatContext *oc, int codec_id )
 {
        // Get the properties
-       mlt_properties properties = mlt_consumer_properties( this );
+       mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
 
        // Create a new stream
        AVStream *st = av_new_stream( oc, 1 );
@@ -405,7 +400,7 @@ static void close_audio( AVFormatContext *oc, AVStream *st )
 static AVStream *add_video_stream( mlt_consumer this, AVFormatContext *oc, int codec_id )
 {
        // Get the properties
-       mlt_properties properties = mlt_consumer_properties( this );
+       mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
 
        // Create a new stream
        AVStream *st = av_new_stream( oc, 0 );
@@ -564,7 +559,7 @@ static void *consumer_thread( void *arg )
        mlt_consumer this = arg;
 
        // Get the properties
-       mlt_properties properties = mlt_consumer_properties( this );
+       mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
 
        // Get the terminate on pause property
        int terminate_on_pause = mlt_properties_get_int( properties, "terminate_on_pause" );
@@ -593,7 +588,7 @@ static void *consumer_thread( void *arg )
        int samples = 0;
 
        // AVFormat audio buffer and frame size
-       int audio_outbuf_size = 2 * 128 * 1024;
+       int audio_outbuf_size = 10000;
        uint8_t *audio_outbuf = av_malloc( audio_outbuf_size );
        int audio_input_frame_size = 0;
 
@@ -663,7 +658,7 @@ static void *consumer_thread( void *arg )
                fmt = guess_format( "mpeg", NULL, NULL );
 
        // We need a filename - default to stdout?
-       if ( filename == NULL )
+       if ( filename == NULL || !strcmp( filename, "" ) )
                filename = "pipe:";
 
        // Get the codec ids selected
@@ -723,7 +718,7 @@ static void *consumer_thread( void *arg )
                // Open the output file, if needed
                if ( !( fmt->flags & AVFMT_NOFILE ) ) 
                {
-                       if (url_fopen(&oc->pb, filename, URL_RDWR) < 0) 
+                       if (url_fopen(&oc->pb, filename, URL_WRONLY) < 0) 
                        {
                                fprintf(stderr, "Could not open '%s'\n", filename);
                                mlt_properties_set_int( properties, "running", 0 );
@@ -760,7 +755,7 @@ static void *consumer_thread( void *arg )
                        frames ++;
 
                        // Default audio args
-                       frame_properties = mlt_frame_properties( frame );
+                       frame_properties = MLT_FRAME_PROPERTIES( frame );
 
                        // Check for the terminated condition
                        terminated = terminate_on_pause && mlt_properties_get_double( frame_properties, "_speed" ) == 0.0;
@@ -778,6 +773,9 @@ static void *consumer_thread( void *arg )
                                        mlt_properties_set_data( properties, "sample_fifo", fifo, 0, ( mlt_destructor )sample_fifo_close, NULL );
                                }
 
+                               if ( mlt_properties_get_double( frame_properties, "_speed" ) != 1.0 )
+                                       memset( pcm, 0, samples * channels * 2 );
+
                                // Append the samples
                                sample_fifo_append( fifo, pcm, samples * channels );
                                total_time += ( samples * 1000000 ) / frequency;
@@ -809,17 +807,23 @@ static void *consumer_thread( void *arg )
                        {
                                if ( channels * audio_input_frame_size < sample_fifo_used( fifo ) )
                                {
-                                       int out_size;
                                        AVCodecContext *c;
+                                       AVPacket pkt;
+                                       av_init_packet( &pkt );
 
                                        c = &audio_st->codec;
 
                                        sample_fifo_fetch( fifo, buffer, channels * audio_input_frame_size );
 
-                                       out_size = avcodec_encode_audio( c, audio_outbuf, audio_outbuf_size, buffer );
-
+                                       pkt.size = avcodec_encode_audio( c, audio_outbuf, audio_outbuf_size, buffer );
                                        // Write the compressed frame in the media file
-                                       if (av_write_frame(oc, audio_st->index, audio_outbuf, out_size) != 0) 
+                                       if ( c->coded_frame )
+                                               pkt.pts= c->coded_frame->pts;
+                                       pkt.flags |= PKT_FLAG_KEY;
+                                       pkt.stream_index= audio_st->index;
+                                       pkt.data= audio_outbuf;
+
+                                       if ( av_interleaved_write_frame( oc, &pkt ) != 0) 
                                                fprintf(stderr, "Error while writing audio frame\n");
                                }
                                else
@@ -835,7 +839,7 @@ static void *consumer_thread( void *arg )
                                        AVCodecContext *c;
 
                                        frame = mlt_deque_pop_front( queue );
-                                       frame_properties = mlt_frame_properties( frame );
+                                       frame_properties = MLT_FRAME_PROPERTIES( frame );
 
                                        c = &video_st->codec;
                                        
@@ -846,6 +850,15 @@ static void *consumer_thread( void *arg )
                                                uint8_t *p;
                                                uint8_t *q;
 
+                                               mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
+
+                                               // This will cause some fx to go awry....
+                                               if ( mlt_properties_get_int( properties, "transcode" ) )
+                                               {
+                                                       mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "normalised_width", img_height * 4.0 / 3.0 );
+                                                       mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "normalised_height", img_height );
+                                               }
+
                                                mlt_frame_get_image( frame, &image, &img_fmt, &img_width, &img_height, 0 );
 
                                                q = image;
@@ -867,7 +880,15 @@ static void *consumer_thread( void *arg )
                                        if (oc->oformat->flags & AVFMT_RAWPICTURE) 
                                        {
                                                // raw video case. The API will change slightly in the near future for that
-                                               ret = av_write_frame(oc, video_st->index, (uint8_t *)output, sizeof(AVPicture));
+                                               AVPacket pkt;
+                                               av_init_packet(&pkt);
+        
+                                               pkt.flags |= PKT_FLAG_KEY;
+                                               pkt.stream_index= video_st->index;
+                                               pkt.data= (uint8_t *)output;
+                                               pkt.size= sizeof(AVPicture);
+
+                                               ret = av_write_frame(oc, &pkt);
                                        } 
                                        else 
                                        {
@@ -880,9 +901,19 @@ static void *consumer_thread( void *arg )
                                                // If zero size, it means the image was buffered
                                                if (out_size != 0) 
                                                {
-                                                       // write the compressed frame in the media file
-                                                       // XXX: in case of B frames, the pts is not yet valid
-                                                       ret = av_write_frame( oc, video_st->index, video_outbuf, out_size );
+                                                       AVPacket pkt;
+                                                       av_init_packet( &pkt );
+
+                                                       if ( c->coded_frame )
+                                                               pkt.pts= c->coded_frame->pts;
+                                                       if(c->coded_frame->key_frame)
+                                                               pkt.flags |= PKT_FLAG_KEY;
+                                                       pkt.stream_index= video_st->index;
+                                                       pkt.data= video_outbuf;
+                                                       pkt.size= out_size;
+
+                                       // write the compressed frame in the media file
+                                                       ret = av_interleaved_write_frame(oc, &pkt);
                                                } 
                                        }
                                        frame_count++;
@@ -895,11 +926,14 @@ static void *consumer_thread( void *arg )
                        }
                }
 
-               if ( real_time_output && frames % 25 == 0 )
+               if ( real_time_output && frames % 12 == 0 )
                {
                        long passed = time_difference( &ante );
-                       long pending = ( ( ( long )sample_fifo_used( fifo ) * 1000 ) / frequency ) * 1000;
-                       passed -= pending;
+                       if ( fifo != NULL )
+                       {
+                               long pending = ( ( ( long )sample_fifo_used( fifo ) * 1000 ) / frequency ) * 1000;
+                               passed -= pending;
+                       }
                        if ( passed < total_time )
                        {
                                long total = ( total_time - passed );
@@ -940,6 +974,8 @@ static void *consumer_thread( void *arg )
        // Just in case we terminated on pause
        mlt_properties_set_int( properties, "running", 0 );
 
+       mlt_consumer_stopped( this );
+
        return NULL;
 }