event fix for playlist and consumer-stopped event
[melted] / src / modules / avformat / producer_avformat.c
index f72d9fa..bd6927a 100644 (file)
@@ -173,6 +173,9 @@ static int producer_open( mlt_producer this, char *file )
        // AV option (0 = both, 1 = video, 2 = audio)
        int av = 0;
        
+       // Setting lowest log level
+       av_log_set_level( -1 );
+
        // Only if there is not a protocol specification that avformat can handle
        if ( mrl && !url_exist( file ) )
        {
@@ -259,6 +262,7 @@ static int producer_open( mlt_producer this, char *file )
                        // We will default to the first audio and video streams found
                        int audio_index = -1;
                        int video_index = -1;
+                       int av_bypass = 0;
 
                        // Now set properties where we can (use default unknowns if required)
                        if ( context->duration != AV_NOPTS_VALUE ) 
@@ -276,15 +280,17 @@ static int producer_open( mlt_producer this, char *file )
                 mlt_properties_set_double( properties, "start_time", context->start_time );
                        
                        // Check if we're seekable (something funny about mpeg here :-/)
-                       if ( strcmp( file, "pipe:" ) )
+                       if ( strcmp( file, "pipe:" ) && strncmp( file, "http://", 6 ) )
                                mlt_properties_set_int( properties, "seekable", av_seek_frame( context, -1, mlt_properties_get_double( properties, "start_time" ) ) >= 0 );
+                       else
+                               av_bypass = 1;
 
                        // Store selected audio and video indexes on properties
                        mlt_properties_set_int( properties, "audio_index", audio_index );
                        mlt_properties_set_int( properties, "video_index", video_index );
                        
                        // We're going to cheat here - for a/v files, we will have two contexts (reasoning will be clear later)
-                       if ( av == 0 && strcmp( file, "pipe:" ) && audio_index != -1 && video_index != -1 )
+                       if ( av == 0 && !av_bypass && audio_index != -1 && video_index != -1 )
                        {
                                // We'll use the open one as our video_context
                                mlt_properties_set_data( properties, "video_context", context, 0, producer_file_close, NULL );
@@ -311,6 +317,8 @@ static int producer_open( mlt_producer this, char *file )
                                // Something has gone wrong
                                error = -1;
                        }
+
+                       mlt_properties_set_int( properties, "av_bypass", av_bypass );
                }
        }
 
@@ -392,6 +400,12 @@ static inline void convert_image( AVFrame *frame, uint8_t *buffer, int pix_fmt,
                pict.linesize[2] = width >> 1;
                img_convert( &pict, PIX_FMT_YUV420P, (AVPicture *)frame, pix_fmt, width, height );
        }
+       else if ( format == mlt_image_rgb24 )
+       {
+               AVPicture output;
+               avpicture_fill( &output, buffer, PIX_FMT_RGB24, width, height );
+               img_convert( &output, PIX_FMT_RGB24, (AVPicture *)frame, pix_fmt, width, height );
+       }
        else
        {
                AVPicture output;
@@ -459,13 +473,26 @@ static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_form
        // Generate the size in bytes
        int size = 0; 
 
+       // Hopefully provide better support for streams...
+       int av_bypass = mlt_properties_get_int( properties, "av_bypass" );
+
        // Set the result arguments that we know here (only *buffer is now required)
-       if ( *format != mlt_image_yuv422 && *format != mlt_image_yuv420p )
-               *format = mlt_image_yuv422;
        *width = codec_context->width;
        *height = codec_context->height;
 
-       size = *width * ( *height + 1 ) * 2;
+       switch ( *format )
+       {
+               case mlt_image_yuv420p:
+                       size = *width * 3 * ( *height + 1 ) / 2;
+                       break;
+               case mlt_image_rgb24:
+                       size = *width * ( *height + 1 ) * 3;
+                       break;
+               default:
+                       *format = mlt_image_yuv422;
+                       size = *width * ( *height + 1 ) * 2;
+                       break;
+       }
 
        // Set this on the frame properties
        mlt_properties_set_int( frame_properties, "width", *width );
@@ -487,7 +514,7 @@ static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_form
                        // Fast forward - seeking is inefficient for small distances - just ignore following frames
                        ignore = position - expected;
                }
-               else if ( codec_context->gop_size == 0 || ( position < expected || position - expected >= 12 ) )
+               else if ( seekable && ( position < expected || position - expected >= 12 ) )
                {
                        // Set to the real timecode
                        av_seek_frame( context, -1, mlt_properties_get_double( properties, "start_time" ) + real_timecode * 1000000.0 );
@@ -502,7 +529,7 @@ static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_form
        
        // Duplicate the last image if necessary
        if ( av_frame != NULL && ( paused || mlt_properties_get_double( properties, "current_time" ) >= real_timecode ) &&
-                strcmp( mlt_properties_get( properties, "resource" ), "pipe:" ) )
+                av_bypass == 0 )
        {
                // Duplicate it
                convert_image( av_frame, *buffer, codec_context->pix_fmt, *format, *width, *height );
@@ -514,6 +541,11 @@ static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_form
        {
                int ret = 0;
                int got_picture = 0;
+               int must_decode = 1;
+
+               // Temporary hack to improve intra frame only
+               if ( !strcmp( codec_context->codec->name, "mjpeg" ) )
+                       must_decode = 0;
 
                memset( &pkt, 0, sizeof( pkt ) );
 
@@ -522,7 +554,6 @@ static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_form
                {
                        av_frame = calloc( 1, sizeof( AVFrame ) );
                        mlt_properties_set_data( properties, "av_frame", av_frame, 0, free, NULL );
-                       paused = 0;
                }
 
                while( ret >= 0 && !got_picture )
@@ -533,16 +564,18 @@ static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_form
                        // We only deal with video from the selected video_index
                        if ( ret >= 0 && pkt.stream_index == index && pkt.size > 0 )
                        {
+                               // Determine time code of the packet
+                               if ( pkt.pts != AV_NOPTS_VALUE )
+                                       current_time = ( double )pkt.pts / 1000000.0;
+                               else
+                                       current_time = real_timecode;
+
                                // Decode the image
-                               ret = avcodec_decode_video( codec_context, av_frame, &got_picture, pkt.data, pkt.size );
+                               if ( must_decode || current_time >= real_timecode )
+                                       ret = avcodec_decode_video( codec_context, av_frame, &got_picture, pkt.data, pkt.size );
 
                                if ( got_picture )
                                {
-                                       if ( pkt.pts != AV_NOPTS_VALUE )
-                                               current_time = ( double )pkt.pts / 1000000.0;
-                                       else
-                                               current_time = real_timecode;
-
                                        // Handle ignore
                                        if ( ( int )( current_time * 100 ) < ( int )( real_timecode * 100 ) - 7 )
                                        {
@@ -551,7 +584,6 @@ static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_form
                                        }
                                        else if ( current_time >= real_timecode )
                                        {
-                                               //current_time = real_timecode;
                                                ignore = 0;
                                        }
                                        else if ( ignore -- )