]> git.sesse.net Git - mlt/blobdiff - src/modules/avformat/producer_avformat.c
Massive refactoring of image conversion.
[mlt] / src / modules / avformat / producer_avformat.c
index 14a97bf7d853b361a138a21b807b266e620cb846..5b2919dc82e7989db24bb4a8bf993f04abab77a1 100644 (file)
@@ -22,6 +22,8 @@
 // MLT Header files
 #include <framework/mlt_producer.h>
 #include <framework/mlt_frame.h>
+#include <framework/mlt_profile.h>
+#include <framework/mlt_log.h>
 
 // ffmpeg Header files
 #include <avformat.h>
 #include <stdlib.h>
 #include <string.h>
 #include <pthread.h>
-#include <math.h>
+
+#if LIBAVUTIL_VERSION_INT < (50<<16)
+#define PIX_FMT_RGB32 PIX_FMT_RGBA32
+#define PIX_FMT_YUYV422 PIX_FMT_YUV422
+#endif
 
 void avformat_lock( );
 void avformat_unlock( );
@@ -51,6 +57,41 @@ static int producer_get_frame( mlt_producer this, mlt_frame_ptr frame, int index
 
 mlt_producer producer_avformat_init( mlt_profile profile, char *file )
 {
+       int error = 0;
+
+       // Report information about available demuxers and codecs as YAML Tiny
+       if ( file && strstr( file, "f-list" ) )
+       {
+               fprintf( stderr, "---\nformats:\n" );
+               AVInputFormat *format = NULL;
+               while ( ( format = av_iformat_next( format ) ) )
+                       fprintf( stderr, "  - %s\n", format->name );
+               fprintf( stderr, "...\n" );
+               error = 1;
+       }
+       if ( file && strstr( file, "acodec-list" ) )
+       {
+               fprintf( stderr, "---\naudio_codecs:\n" );
+               AVCodec *codec = NULL;
+               while ( ( codec = av_codec_next( codec ) ) )
+                       if ( codec->decode && codec->type == CODEC_TYPE_AUDIO )
+                               fprintf( stderr, "  - %s\n", codec->name );
+               fprintf( stderr, "...\n" );
+               error = 1;
+       }
+       if ( file && strstr( file, "vcodec-list" ) )
+       {
+               fprintf( stderr, "---\nvideo_codecs:\n" );
+               AVCodec *codec = NULL;
+               while ( ( codec = av_codec_next( codec ) ) )
+                       if ( codec->decode && codec->type == CODEC_TYPE_VIDEO )
+                               fprintf( stderr, "  - %s\n", codec->name );
+               fprintf( stderr, "...\n" );
+               error = 1;
+       }
+       if ( error )
+               return NULL;
+
        mlt_producer this = NULL;
 
        // Check that we have a non-NULL argument
@@ -106,7 +147,7 @@ static mlt_properties find_default_streams( mlt_properties meta_media, AVFormatC
        mlt_properties_set_int( meta_media, "meta.media.nb_streams", context->nb_streams );
 
        // Allow for multiple audio and video streams in the file and select first of each (if available)
-       for( i = 0; i < context->nb_streams; i++ ) 
+       for( i = 0; i < context->nb_streams; i++ )
        {
                // Get the codec context
                AVStream *stream = context->streams[ i ];
@@ -127,8 +168,10 @@ static mlt_properties find_default_streams( mlt_properties meta_media, AVFormatC
                                mlt_properties_set( meta_media, key, "video" );
                                snprintf( key, sizeof(key), "meta.media.%d.stream.frame_rate", i );
                                mlt_properties_set_double( meta_media, key, av_q2d( context->streams[ i ]->r_frame_rate ) );
+#if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(21<<8)+0)
                                snprintf( key, sizeof(key), "meta.media.%d.stream.sample_aspect_ratio", i );
                                mlt_properties_set_double( meta_media, key, av_q2d( context->streams[ i ]->sample_aspect_ratio ) );
+#endif
                                snprintf( key, sizeof(key), "meta.media.%d.codec.pix_fmt", i );
                                mlt_properties_set( meta_media, key, avcodec_get_pix_fmt_name( codec_context->pix_fmt ) );
                                snprintf( key, sizeof(key), "meta.media.%d.codec.sample_aspect_ratio", i );
@@ -138,8 +181,10 @@ static mlt_properties find_default_streams( mlt_properties meta_media, AVFormatC
                                if ( *audio_index < 0 )
                                        *audio_index = i;
                                mlt_properties_set( meta_media, key, "audio" );
+#if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(71<<8)+0))
                                snprintf( key, sizeof(key), "meta.media.%d.codec.sample_fmt", i );
                                mlt_properties_set( meta_media, key, avcodec_get_sample_fmt_name( codec_context->sample_fmt ) );
+#endif
                                snprintf( key, sizeof(key), "meta.media.%d.codec.sample_rate", i );
                                mlt_properties_set_int( meta_media, key, codec_context->sample_rate );
                                snprintf( key, sizeof(key), "meta.media.%d.codec.channels", i );
@@ -152,8 +197,10 @@ static mlt_properties find_default_streams( mlt_properties meta_media, AVFormatC
 //             mlt_properties_set_double( meta_media, key, av_q2d( context->streams[ i ]->time_base ) );
                snprintf( key, sizeof(key), "meta.media.%d.codec.name", i );
                mlt_properties_set( meta_media, key, codec->name );
+#if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(55<<8)+0))
                snprintf( key, sizeof(key), "meta.media.%d.codec.long_name", i );
                mlt_properties_set( meta_media, key, codec->long_name );
+#endif
                snprintf( key, sizeof(key), "meta.media.%d.codec.bit_rate", i );
                mlt_properties_set_int( meta_media, key, codec_context->bit_rate );
 //             snprintf( key, sizeof(key), "meta.media.%d.codec.time_base", i );
@@ -304,7 +351,7 @@ static int producer_open( mlt_producer this, mlt_profile profile, char *file )
 
        // Lock the mutex now
        avformat_lock( );
-       
+
        // If "MRL", then create AVInputFormat
        AVInputFormat *format = NULL;
        AVFormatParameters *params = NULL;
@@ -313,27 +360,24 @@ static int producer_open( mlt_producer this, mlt_profile profile, 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 ) )
        {
                // 'file' becomes format abbreviation
                mrl[0] = 0;
-       
+
                // Lookup the format
                format = av_find_input_format( file );
-               
+
                // Eat the format designator
                file = ++mrl;
-               
+
                if ( format )
                {
                        // Allocate params
                        params = calloc( sizeof( AVFormatParameters ), 1 );
-                       
+
                        // These are required by video4linux (defaults)
                        params->width = 640;
                        params->height = 480;
@@ -342,7 +386,7 @@ static int producer_open( mlt_producer this, mlt_profile profile, char *file )
                        params->channels = 2;
                        params->sample_rate = 48000;
                }
-               
+
                // XXX: this does not work anymore since avdevice
                // TODO: make producer_avddevice?
                // Parse out params
@@ -386,7 +430,7 @@ static int producer_open( mlt_producer this, mlt_profile profile, char *file )
 
        // Now attempt to open the file
        error = av_open_input_file( &context, file, format, 0, params ) < 0;
-       
+
        // Cleanup AVFormatParameters
        free( standard );
        free( params );
@@ -406,7 +450,7 @@ static int producer_open( mlt_producer this, mlt_profile profile, char *file )
                        int av_bypass = 0;
 
                        // Now set properties where we can (use default unknowns if required)
-                       if ( context->duration != AV_NOPTS_VALUE ) 
+                       if ( context->duration != AV_NOPTS_VALUE )
                        {
                                // This isn't going to be accurate for all formats
                                mlt_position frames = ( mlt_position )( ( ( double )context->duration / ( double )AV_TIME_BASE ) * fps + 0.5 );
@@ -419,7 +463,7 @@ static int producer_open( mlt_producer this, mlt_profile profile, char *file )
 
                        if ( context->start_time != AV_NOPTS_VALUE )
                                mlt_properties_set_double( properties, "_start_time", context->start_time );
-                       
+
                        // Check if we're seekable (something funny about mpeg here :-/)
                        if ( strcmp( file, "pipe:" ) && strncmp( file, "http://", 6 )  && strncmp( file, "udp:", 4 )  && strncmp( file, "tcp:", 4 ) && strncmp( file, "rtsp:", 5 )  && strncmp( file, "rtp:", 4 ) )
                        {
@@ -467,21 +511,21 @@ static int producer_open( mlt_producer this, mlt_profile profile, char *file )
                        }
 
                        // Read Metadata
-                       if (context->title != NULL) 
+                       if (context->title != NULL)
                                mlt_properties_set(properties, "meta.attr.title.markup", context->title );
-                       if (context->author != NULL) 
+                       if (context->author != NULL)
                                mlt_properties_set(properties, "meta.attr.author.markup", context->author );
-                       if (context->copyright != NULL) 
+                       if (context->copyright != NULL)
                                mlt_properties_set(properties, "meta.attr.copyright.markup", context->copyright );
-                       if (context->comment != NULL) 
+                       if (context->comment != NULL)
                                mlt_properties_set(properties, "meta.attr.comment.markup", context->comment );
-                       if (context->album != NULL) 
+                       if (context->album != NULL)
                                mlt_properties_set(properties, "meta.attr.album.markup", context->album );
-                       if (context->year != 0) 
+                       if (context->year != 0)
                                mlt_properties_set_int(properties, "meta.attr.year.markup", context->year );
-                       if (context->track != 0) 
+                       if (context->track != 0)
                                mlt_properties_set_int(properties, "meta.attr.track.markup", context->track );
-                       
+
                        // We're going to cheat here - for a/v files, we will have two contexts (reasoning will be clear later)
                        if ( av == 0 && audio_index != -1 && video_index != -1 )
                        {
@@ -529,10 +573,21 @@ static double producer_time_of_frame( mlt_producer this, mlt_position position )
        return ( double )position / mlt_producer_get_fps( this );
 }
 
-static inline void convert_image( AVFrame *frame, uint8_t *buffer, int pix_fmt, mlt_image_format format, int width, int height )
+static inline void convert_image( AVFrame *frame, uint8_t *buffer, int pix_fmt, mlt_image_format *format, int width, int height )
 {
 #ifdef SWSCALE
-       if ( format == mlt_image_yuv420p )
+       if ( pix_fmt == PIX_FMT_RGB32 )
+       {
+               *format = mlt_image_rgb24a;
+               struct SwsContext *context = sws_getContext( width, height, pix_fmt,
+                       width, height, PIX_FMT_RGBA, SWS_FAST_BILINEAR, NULL, NULL, NULL);
+               AVPicture output;
+               avpicture_fill( &output, buffer, PIX_FMT_RGBA, width, height );
+               sws_scale( context, frame->data, frame->linesize, 0, height,
+                       output.data, output.linesize);
+               sws_freeContext( context );
+       }
+       else if ( *format == mlt_image_yuv420p )
        {
                struct SwsContext *context = sws_getContext( width, height, pix_fmt,
                        width, height, PIX_FMT_YUV420P, SWS_FAST_BILINEAR, NULL, NULL, NULL);
@@ -547,7 +602,7 @@ static inline void convert_image( AVFrame *frame, uint8_t *buffer, int pix_fmt,
                        output.data, output.linesize);
                sws_freeContext( context );
        }
-       else if ( format == mlt_image_rgb24 )
+       else if ( *format == mlt_image_rgb24 )
        {
                struct SwsContext *context = sws_getContext( width, height, pix_fmt,
                        width, height, PIX_FMT_RGB24, SWS_FAST_BILINEAR, NULL, NULL, NULL);
@@ -557,6 +612,16 @@ static inline void convert_image( AVFrame *frame, uint8_t *buffer, int pix_fmt,
                        output.data, output.linesize);
                sws_freeContext( context );
        }
+       else if ( *format == mlt_image_rgb24a || *format == mlt_image_opengl )
+       {
+               struct SwsContext *context = sws_getContext( width, height, pix_fmt,
+                       width, height, PIX_FMT_RGBA, SWS_FAST_BILINEAR, NULL, NULL, NULL);
+               AVPicture output;
+               avpicture_fill( &output, buffer, PIX_FMT_RGBA, width, height );
+               sws_scale( context, frame->data, frame->linesize, 0, height,
+                       output.data, output.linesize);
+               sws_freeContext( context );
+       }
        else
        {
                struct SwsContext *context = sws_getContext( width, height, pix_fmt,
@@ -568,7 +633,7 @@ static inline void convert_image( AVFrame *frame, uint8_t *buffer, int pix_fmt,
                sws_freeContext( context );
        }
 #else
-       if ( format == mlt_image_yuv420p )
+       if ( *format == mlt_image_yuv420p )
        {
                AVPicture pict;
                pict.data[0] = buffer;
@@ -579,17 +644,23 @@ 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 )
+       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 if ( format == mlt_image_rgb24a || format == mlt_image_opengl )
+       {
+               AVPicture output;
+               avpicture_fill( &output, buffer, PIX_FMT_RGB32, width, height );
+               img_convert( &output, PIX_FMT_RGB32, (AVPicture *)frame, pix_fmt, width, height );
+       }
        else
        {
                AVPicture output;
-               avpicture_fill( &output, buffer, PIX_FMT_YUV422, width, height );
-               img_convert( &output, PIX_FMT_YUV422, (AVPicture *)frame, pix_fmt, width, height );
+               avpicture_fill( &output, buffer, PIX_FMT_YUYV422, width, height );
+               img_convert( &output, PIX_FMT_YUYV422, (AVPicture *)frame, pix_fmt, width, height );
        }
 #endif
 }
@@ -609,7 +680,9 @@ static int allocate_buffer( mlt_properties frame_properties, AVCodecContext *cod
        mlt_properties_set_int( frame_properties, "width", *width );
        mlt_properties_set_int( frame_properties, "height", *height );
 
-       switch ( *format )
+       if ( codec_context->pix_fmt == PIX_FMT_RGB32 )
+               size = *width * ( *height + 1 ) * 4;
+       else switch ( *format )
        {
                case mlt_image_yuv420p:
                        size = *width * 3 * ( *height + 1 ) / 2;
@@ -617,6 +690,10 @@ static int allocate_buffer( mlt_properties frame_properties, AVCodecContext *cod
                case mlt_image_rgb24:
                        size = *width * ( *height + 1 ) * 3;
                        break;
+               case mlt_image_rgb24a:
+               case mlt_image_opengl:
+                       size = *width * ( *height + 1 ) * 4;
+                       break;
                default:
                        *format = mlt_image_yuv422;
                        size = *width * ( *height + 1 ) * 2;
@@ -644,7 +721,7 @@ static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_form
        // Obtain the frame number of this frame
        mlt_position position = mlt_properties_get_position( frame_properties, "avformat_position" );
 
-       // Get the producer 
+       // Get the producer
        mlt_producer this = mlt_properties_get_data( frame_properties, "avformat_producer", NULL );
 
        // Get the producer properties
@@ -694,12 +771,16 @@ static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_form
        int must_decode = 1;
 
        // Temporary hack to improve intra frame only
-       must_decode = strcmp( codec_context->codec->name, "mjpeg" ) &&
-                                 strcmp( codec_context->codec->name, "rawvideo" ) &&
-                                 strcmp( codec_context->codec->name, "dvvideo" );
+       must_decode = strcmp( codec_context->codec->name, "dnxhd" ) &&
+                                 strcmp( codec_context->codec->name, "dvvideo" ) &&
+                                 strcmp( codec_context->codec->name, "huffyuv" ) &&
+                                 strcmp( codec_context->codec->name, "mjpeg" ) &&
+                                 strcmp( codec_context->codec->name, "rawvideo" );
+
+       int last_position = mlt_properties_get_int( properties, "_last_position" );
 
        // Seek if necessary
-       if ( position != expected )
+       if ( position != expected || last_position == -1 )
        {
                if ( av_frame != NULL && position + 1 == expected )
                {
@@ -711,7 +792,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 = ( int )( ( position - expected ) / fps * source_fps );
                }
-               else if ( seekable && ( position < expected || position - expected >= 12 ) )
+               else if ( seekable && ( position < expected || position - expected >= 12 || last_position == -1 ) )
                {
                        // Calculate the timestamp for the requested frame
                        int64_t timestamp = ( int64_t )( ( double )req_position / source_fps * AV_TIME_BASE + 0.5 );
@@ -723,8 +804,10 @@ static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_form
                                timestamp = 0;
 
                        // Set to the timestamp
+                       mlt_log_debug( MLT_PRODUCER_SERVICE( this ), "seeking timestamp %lld position %d expected %d last_pos %d\n",
+                               timestamp, position, expected, last_position );
                        av_seek_frame( context, -1, timestamp, AVSEEK_FLAG_BACKWARD );
-       
+
                        // Remove the cached info relating to the previous position
                        mlt_properties_set_int( properties, "_current_position", -1 );
                        mlt_properties_set_int( properties, "_last_position", -1 );
@@ -740,7 +823,7 @@ static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_form
        {
                // Duplicate it
                if ( allocate_buffer( frame_properties, codec_context, buffer, format, width, height ) )
-                       convert_image( av_frame, *buffer, codec_context->pix_fmt, *format, *width, *height );
+                       convert_image( av_frame, *buffer, codec_context->pix_fmt, format, *width, *height );
                else
                        mlt_frame_get_image( frame, buffer, format, width, height, writable );
        }
@@ -765,12 +848,22 @@ static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_form
                        if ( ret >= 0 && pkt.stream_index == index && pkt.size > 0 )
                        {
                                // Determine time code of the packet
-                               int_position = ( int )( av_q2d( stream->time_base ) * pkt.dts * source_fps + 0.5 );
-                               if ( context->start_time != AV_NOPTS_VALUE )
-                                       int_position -= ( int )( context->start_time * source_fps / AV_TIME_BASE + 0.5 );
-                               int last_position = mlt_properties_get_int( properties, "_last_position" );
-                               if ( int_position == last_position )
-                                       int_position = last_position + 1;
+                               if (pkt.dts != AV_NOPTS_VALUE)
+                               {
+                                       int_position = ( int )( av_q2d( stream->time_base ) * pkt.dts * source_fps + 0.5 );
+                                       if ( context->start_time != AV_NOPTS_VALUE )
+                                               int_position -= ( int )( context->start_time * source_fps / AV_TIME_BASE + 0.5 );
+                                       last_position = mlt_properties_get_int( properties, "_last_position" );
+                                       if ( int_position == last_position )
+                                               int_position = last_position + 1;
+                               }
+                               else
+                               {
+                                       int_position = req_position;
+                               }
+                               // Make a dumb assumption on streams that contain wild timestamps
+                               if ( (unsigned) req_position - (unsigned) int_position > 999 )
+                                       int_position = req_position;
                                mlt_properties_set_int( properties, "_last_position", int_position );
 
                                // Decode the image
@@ -794,6 +887,8 @@ static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_form
                                                got_picture = 0;
                                        }
                                }
+                               mlt_log_debug( MLT_PRODUCER_SERVICE(this), "pkt.dts %llu req_pos %d cur_pos %d pkt_pos %d got_pic %d key %d\n",
+                                       pkt.dts, req_position, current_position, int_position, got_picture, pkt.flags & PKT_FLAG_KEY );
                                av_free_packet( &pkt );
                        }
                        else if ( ret >= 0 )
@@ -806,8 +901,9 @@ static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_form
                        {
                                if ( allocate_buffer( frame_properties, codec_context, buffer, format, width, height ) )
                                {
-                                       convert_image( av_frame, *buffer, codec_context->pix_fmt, *format, *width, *height );
-                                       mlt_properties_set_int( frame_properties, "progressive", !av_frame->interlaced_frame );
+                                       convert_image( av_frame, *buffer, codec_context->pix_fmt, format, *width, *height );
+                                       if ( !mlt_properties_get( properties, "force_progressive" ) )
+                                               mlt_properties_set_int( frame_properties, "progressive", !av_frame->interlaced_frame );
                                        mlt_properties_set_int( properties, "top_field_first", av_frame->top_field_first );
                                        mlt_properties_set_int( properties, "_current_position", int_position );
                                        mlt_properties_set_int( properties, "_got_picture", 1 );
@@ -843,7 +939,7 @@ static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_form
 static void apply_properties( void *obj, mlt_properties properties, int flags )
 {
        int i;
-       int count = mlt_properties_count( properties ); 
+       int count = mlt_properties_count( properties );
        for ( i = 0; i < count; i++ )
        {
                const char *opt_name = mlt_properties_get_name( properties, i );
@@ -928,11 +1024,11 @@ static void producer_set_up_video( mlt_producer this, mlt_frame frame )
                                get_aspect_ratio( context->streams[ index ], codec_context, NULL ) );
                        codec = NULL;
                }
-       
+
                // Initialise the codec if necessary
                if ( codec == NULL )
                {
-                       // Initialise multi-threading 
+                       // Initialise multi-threading
                        int thread_count = mlt_properties_get_int( properties, "threads" );
                        if ( thread_count == 0 && getenv( "MLT_AVFORMAT_THREADS" ) )
                                thread_count = atoi( getenv( "MLT_AVFORMAT_THREADS" ) );
@@ -941,7 +1037,7 @@ static void producer_set_up_video( mlt_producer this, mlt_frame frame )
                                avcodec_thread_init( codec_context, thread_count );
                                codec_context->thread_count = thread_count;
                        }
-                       
+
                        // Find the codec
                        codec = avcodec_find_decoder( codec_context->codec_id );
 
@@ -981,11 +1077,15 @@ static void producer_set_up_video( mlt_producer this, mlt_frame frame )
                        else
                                mlt_properties_set_double( properties, "source_fps", mlt_producer_get_fps( this ) );
                        mlt_properties_set_double( properties, "aspect_ratio", aspect_ratio );
-                       
+
                        // Set the width and height
                        mlt_properties_set_int( frame_properties, "width", codec_context->width );
                        mlt_properties_set_int( frame_properties, "height", codec_context->height );
+                       mlt_properties_set_int( frame_properties, "real_width", codec_context->width );
+                       mlt_properties_set_int( frame_properties, "real_height", codec_context->height );
                        mlt_properties_set_double( frame_properties, "aspect_ratio", aspect_ratio );
+                       if ( mlt_properties_get( properties, "force_progressive" ) )
+                               mlt_properties_set_int( frame_properties, "progressive", mlt_properties_get_int( properties, "force_progressive" ) );
 
                        mlt_frame_push_get_image( frame, producer_get_image );
                        mlt_properties_set_data( frame_properties, "avformat_producer", this, 0, NULL, NULL );
@@ -1012,7 +1112,7 @@ static int producer_get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_form
        // Obtain the frame number of this frame
        mlt_position position = mlt_properties_get_position( frame_properties, "avformat_position" );
 
-       // Get the producer 
+       // Get the producer
        mlt_producer this = mlt_properties_get_data( frame_properties, "avformat_producer", NULL );
 
        // Get the producer properties
@@ -1033,15 +1133,9 @@ static int producer_get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_form
        // Obtain the resample context if it exists (not always needed)
        ReSampleContext *resample = mlt_properties_get_data( properties, "audio_resample", NULL );
 
-#if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(71<<8)+0))
-       // Get the format converter context if it exists
-       AVAudioConvert *convert = mlt_properties_get_data( properties, "audio_convert", NULL );
-#endif
-
        // Obtain the audio buffers
        int16_t *audio_buffer = mlt_properties_get_data( properties, "audio_buffer", NULL );
        int16_t *decode_buffer = mlt_properties_get_data( properties, "decode_buffer", NULL );
-       int16_t *convert_buffer = mlt_properties_get_data( properties, "convert_buffer", NULL );
 
        // Get amount of audio used
        int audio_used =  mlt_properties_get_int( properties, "_audio_used" );
@@ -1061,34 +1155,31 @@ static int producer_get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_form
        // Number of frames to ignore (for ffwd)
        int ignore = 0;
 
-       // Flag for paused (silence) 
+       // Flag for paused (silence)
        int paused = 0;
 
        // Check for resample and create if necessary
        if ( resample == NULL && codec_context->channels <= 2 )
        {
                // Create the resampler
+#if (LIBAVCODEC_VERSION_INT >= ((52<<16)+(15<<8)+0))
+               resample = av_audio_resample_init( *channels, codec_context->channels, *frequency, codec_context->sample_rate,
+                       SAMPLE_FMT_S16, codec_context->sample_fmt, 16, 10, 0, 0.8 );
+#else
                resample = audio_resample_init( *channels, codec_context->channels, *frequency, codec_context->sample_rate );
+#endif
 
                // And store it on properties
                mlt_properties_set_data( properties, "audio_resample", resample, 0, ( mlt_destructor )audio_resample_close, NULL );
        }
        else if ( resample == NULL )
        {
-               *channels = codec_context->channels;
-               *frequency = codec_context->sample_rate;
-       }
+               // TODO: uncomment and remove following line when full multi-channel support is ready
+               // *channels = codec_context->channels;
+               codec_context->request_channels = *channels;
 
-#if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(71<<8)+0))
-       // Check for audio format converter and create if necessary
-       // TODO: support higher resolutions than 16-bit.
-       if ( convert == NULL && codec_context->sample_fmt != SAMPLE_FMT_S16 )
-       {
-               // Create single channel converter for interleaved with no mixing matrix
-               convert = av_audio_convert_alloc( SAMPLE_FMT_S16, 1, codec_context->sample_fmt, 1, NULL, 0 );
-               mlt_properties_set_data( properties, "audio_convert", convert, 0, ( mlt_destructor )av_audio_convert_free, NULL );
+               *frequency = codec_context->sample_rate;
        }
-#endif
 
        // Check for audio buffer and create if necessary
        if ( audio_buffer == NULL )
@@ -1110,18 +1201,6 @@ static int producer_get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_form
                mlt_properties_set_data( properties, "decode_buffer", decode_buffer, 0, ( mlt_destructor )av_free, NULL );
        }
 
-#if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(71<<8)+0))
-       // Check for format converter buffer and create if necessary
-       if ( resample && convert && convert_buffer == NULL )
-       {
-               // Allocate the audio buffer
-               convert_buffer = mlt_pool_alloc( AVCODEC_MAX_AUDIO_FRAME_SIZE * sizeof( int16_t ) );
-
-               // And store it on properties for reuse
-               mlt_properties_set_data( properties, "convert_buffer", convert_buffer, 0, ( mlt_destructor )mlt_pool_release, NULL );
-       }
-#endif
-
        // Seek if necessary
        if ( position != expected )
        {
@@ -1137,8 +1216,14 @@ static int producer_get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_form
                }
                else if ( position < expected || position - expected >= 12 )
                {
+                       int64_t timestamp = ( int64_t )( real_timecode * AV_TIME_BASE + 0.5 );
+                       if ( context->start_time != AV_NOPTS_VALUE )
+                               timestamp += context->start_time;
+                       if ( timestamp < 0 )
+                               timestamp = 0;
+
                        // Set to the real timecode
-                       if ( av_seek_frame( context, -1, mlt_properties_get_double( properties, "_start_time" ) + real_timecode * 1000000.0, AVSEEK_FLAG_BACKWARD ) != 0 )
+                       if ( av_seek_frame( context, -1, timestamp, AVSEEK_FLAG_BACKWARD ) != 0 )
                                paused = 1;
 
                        // Clear the usage in the audio buffer
@@ -1189,43 +1274,20 @@ static int producer_get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_form
                                len -= ret;
                                ptr += ret;
 
-                               if ( data_size > 0 )
+                               if ( data_size > 0 && ( audio_used * *channels + data_size < AVCODEC_MAX_AUDIO_FRAME_SIZE ) )
                                {
-                                       int src_stride[6]= { av_get_bits_per_sample_format( codec_context->sample_fmt ) / 8 };
-                                       int dst_stride[6]= { av_get_bits_per_sample_format( SAMPLE_FMT_S16 ) / 8 };
-
                                        if ( resample )
                                        {
                                                int16_t *source = decode_buffer;
                                                int16_t *dest = &audio_buffer[ audio_used * *channels ];
-                                               int convert_samples = data_size / src_stride[0];
+                                               int convert_samples = data_size / av_get_bits_per_sample_format( codec_context->sample_fmt ) * 8 / codec_context->channels;
 
-#if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(71<<8)+0))
-                                               if ( convert )
-                                               {
-                                                       const void *src_buf[6] = { decode_buffer };
-                                                       void *dst_buf[6] = { convert_buffer };
-                                                       av_audio_convert( convert, dst_buf, dst_stride, src_buf, src_stride, convert_samples );
-                                                       source = convert_buffer;
-                                               }
-#endif
-                                               audio_used += audio_resample( resample, dest, source, convert_samples / codec_context->channels );
+                                               audio_used += audio_resample( resample, dest, source, convert_samples );
                                        }
                                        else
                                        {
-#if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(71<<8)+0))
-                                               if ( convert )
-                                               {
-                                                       const void *src_buf[6] = { decode_buffer };
-                                                       void *dst_buf[6] = { &audio_buffer[ audio_used * *channels ] };
-                                                       av_audio_convert( convert, dst_buf, dst_stride, src_buf, src_stride, data_size / src_stride[0] );
-                                               }
-                                               else
-#endif
-                                               {
-                                                       memcpy( &audio_buffer[ audio_used * *channels ], decode_buffer, data_size );
-                                               }
-                                               audio_used += data_size / *channels / src_stride[0];
+                                               memcpy( &audio_buffer[ audio_used * *channels ], decode_buffer, data_size );
+                                               audio_used += data_size / *channels / av_get_bits_per_sample_format( codec_context->sample_fmt ) * 8;
                                        }
 
                                        // Handle ignore
@@ -1240,8 +1302,14 @@ static int producer_get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_form
                                // If we're behind, ignore this packet
                                if ( pkt.pts >= 0 )
                                {
-                                       float current_pts = av_q2d( stream->time_base ) * pkt.pts;
-                                       if ( seekable && ( !ignore && current_pts <= ( real_timecode - 0.02 ) ) )
+                                       double current_pts = av_q2d( stream->time_base ) * pkt.pts;
+                                       double source_fps = mlt_properties_get_double( properties, "source_fps" );
+                                       int req_position = ( int )( real_timecode * source_fps + 0.5 );
+                                       int int_position = ( int )( current_pts * source_fps + 0.5 );
+
+                                       if ( context->start_time != AV_NOPTS_VALUE )
+                                               int_position -= ( int )( context->start_time * source_fps / AV_TIME_BASE + 0.5 );
+                                       if ( seekable && !ignore && int_position < req_position )
                                                ignore = 1;
                                }
                        }
@@ -1264,7 +1332,7 @@ static int producer_get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_form
                {
                        memset( *buffer, 0, *samples * *channels * sizeof( int16_t ) );
                }
-               
+
                // Store the number of audio samples still available
                mlt_properties_set_int( properties, "_audio_used", audio_used );
        }