Apply cosmetic cleanup part of ldflags_order patch from Alberto Villa.
[melted] / src / modules / avformat / producer_avformat.c
index 3d94ed4..ae4f7c2 100644 (file)
@@ -22,6 +22,7 @@
 // MLT Header files
 #include <framework/mlt_producer.h>
 #include <framework/mlt_frame.h>
+#include <framework/mlt_profile.h>
 
 // ffmpeg Header files
 #include <avformat.h>
 #include <pthread.h>
 #include <math.h>
 
+#if LIBAVUTIL_VERSION_INT < (50<<16)
+#define PIX_FMT_YUYV422 PIX_FMT_YUV422
+#endif
+
 void avformat_lock( );
 void avformat_unlock( );
 
@@ -629,8 +634,8 @@ static inline void convert_image( AVFrame *frame, uint8_t *buffer, int pix_fmt,
        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
 }
@@ -1180,8 +1185,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
@@ -1283,8 +1294,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;
                                }
                        }