== Audaspace / FFMPEG ==
authorPeter Schlaile <peter@schlaile.de>
Sat, 16 Oct 2010 15:01:01 +0000 (15:01 +0000)
committerPeter Schlaile <peter@schlaile.de>
Sat, 16 Oct 2010 15:01:01 +0000 (15:01 +0000)
Audaspace audio seek code wasn't taking start_time into account.
(Most common symptom: HDV MPEG2TS files always start audio playback at the
beginning regardless of seek position.)

intern/audaspace/ffmpeg/AUD_FFMPEGReader.cpp

index 623e99d5edc10639c01a56c4e708895b783d59d0..09fdb31e93838e7fa9f9d0d5823b4abb859370d2 100644 (file)
@@ -246,13 +246,23 @@ void AUD_FFMPEGReader::seek(int position)
 {
        if(position >= 0)
        {
+               uint64_t st_time = m_formatCtx->streams[m_stream]->start_time;
+               double time_base = 
+                       av_q2d(m_formatCtx->streams[m_stream]->time_base);
+               uint64_t seek_pos = position / time_base / m_specs.rate;
+
+               if (seek_pos < 0) {
+                       seek_pos = 0;
+               }
+
+               if (st_time != AV_NOPTS_VALUE) {
+                       seek_pos += st_time;
+               }
+
+
                // a value < 0 tells us that seeking failed
-               if(av_seek_frame(m_formatCtx,
-                                                -1,
-                                                (uint64_t)(((uint64_t)position *
-                                                                        (uint64_t)AV_TIME_BASE) /
-                                                                       (uint64_t)m_specs.rate),
-                                                AVSEEK_FLAG_BACKWARD | AVSEEK_FLAG_ANY) >= 0)
+               if(av_seek_frame(m_formatCtx, m_stream, seek_pos,
+                                AVSEEK_FLAG_BACKWARD | AVSEEK_FLAG_ANY) >= 0)
                {
                        avcodec_flush_buffers(m_codecCtx);
                        m_position = position;
@@ -273,9 +283,8 @@ void AUD_FFMPEGReader::seek(int position)
                                        if(packet.pts != AV_NOPTS_VALUE)
                                        {
                                                // calculate real position, and read to frame!
-                                               m_position = packet.pts *
-                                                       av_q2d(m_formatCtx->streams[m_stream]->time_base) *
-                                                       m_specs.rate;
+                                               m_position = (packet.pts - 
+                                                       ((st_time != AV_NOPTS_VALUE) ? st_time : 0)) * time_base * m_specs.rate;
 
                                                if(m_position < position)
                                                {