FFMPEG revisions to match current CVS (part 1)
[melted] / src / modules / avformat / consumer_avformat.c
index 318aec6..d1d8106 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 );
@@ -421,10 +416,11 @@ static AVStream *add_video_stream( mlt_consumer this, AVFormatContext *oc, int c
                c->bit_rate_tolerance = mlt_properties_get_int( properties, "video_bit_rate_tolerance" );
                c->width = mlt_properties_get_int( properties, "width" );
                c->height = mlt_properties_get_int( properties, "height" );
-               c->frame_rate = mlt_properties_get_double( properties, "fps" );
-               c->frame_rate_base = mlt_properties_get_double( properties, "frame_rate_base" );
-               c->frame_rate_base = 1;
+               c->time_base.den = mlt_properties_get_double( properties, "fps" );
+               c->time_base.num = mlt_properties_get_double( properties, "frame_rate_base" );
+               c->time_base.num = 1;
                c->gop_size = mlt_properties_get_int( properties, "gop_size" );
+               c->pix_fmt = PIX_FMT_YUV420P;
 
                if ( mlt_properties_get_int( properties, "b_frames" ) )
                {
@@ -564,7 +560,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" );
@@ -630,7 +626,8 @@ static void *consumer_thread( void *arg )
        AVStream *video_st = NULL;
 
        // Time stamps
-       double audio_pts, video_pts;
+       double audio_pts = 0;
+       double video_pts = 0;
 
        // Loop variable
        int i;
@@ -760,7 +757,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 +775,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;
@@ -793,16 +793,15 @@ static void *consumer_thread( void *arg )
                // While we have stuff to process, process...
                while ( 1 )
                {
-                       // Compute current audio and video time
-                       if (audio_st)
-                               audio_pts = (double)audio_st->pts.val * audio_st->time_base.num / audio_st->time_base.den;
+                       if (audio_st)
+                               audio_pts = (double)audio_st->pts.val * audio_st->time_base.num / audio_st->time_base.den;
                        else
-                               audio_pts = 0.0;
-       
+                               audio_pts = 0.0;
+        
                        if (video_st)
-                               video_pts = (double)video_st->pts.val * video_st->time_base.num / video_st->time_base.den;
-                       else
-                               video_pts = 0.0;
+                               video_pts = (double)video_st->pts.val * video_st->time_base.num / video_st->time_base.den;
+                       else
+                               video_pts = 0.0;
 
                        // Write interleaved audio and video frames
                        if ( !video_st || ( video_st && audio_st && audio_pts < video_pts ) )
@@ -820,13 +819,15 @@ static void *consumer_thread( void *arg )
                                        pkt.size = avcodec_encode_audio( c, audio_outbuf, audio_outbuf_size, buffer );
                                        // Write the compressed frame in the media file
                                        if ( c->coded_frame )
-                                               pkt.pts= c->coded_frame->pts;
+                                               pkt.pts = av_rescale_q( c->coded_frame->pts, c->time_base, audio_st->time_base );
                                        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");
+
+                                       audio_pts += c->frame_size;
                                }
                                else
                                {
@@ -841,7 +842,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;
                                        
@@ -853,6 +854,14 @@ static void *consumer_thread( void *arg )
                                                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;
@@ -883,6 +892,7 @@ static void *consumer_thread( void *arg )
                                                pkt.size= sizeof(AVPicture);
 
                                                ret = av_write_frame(oc, &pkt);
+                                               video_pts += c->frame_size;
                                        } 
                                        else 
                                        {
@@ -899,7 +909,7 @@ static void *consumer_thread( void *arg )
                                                        av_init_packet( &pkt );
 
                                                        if ( c->coded_frame )
-                                                               pkt.pts= c->coded_frame->pts;
+                                                               pkt.pts= av_rescale_q( c->coded_frame->pts, c->time_base, video_st->time_base );
                                                        if(c->coded_frame->key_frame)
                                                                pkt.flags |= PKT_FLAG_KEY;
                                                        pkt.stream_index= video_st->index;
@@ -908,6 +918,7 @@ static void *consumer_thread( void *arg )
 
                                        // write the compressed frame in the media file
                                                        ret = av_interleaved_write_frame(oc, &pkt);
+                                                       video_pts += c->frame_size;
                                                } 
                                        }
                                        frame_count++;
@@ -920,7 +931,7 @@ 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 );
                        if ( fifo != NULL )