Index: TsFileSeek.cpp
===================================================================
--- TsFileSeek.cpp	(revision 27986)
+++ TsFileSeek.cpp	(working copy)
@@ -38,7 +38,6 @@
 CTsFileSeek::CTsFileSeek( CTsDuration& duration)
 :m_duration(duration)
 {
-  m_useBinarySearch = true;
 }
 
 CTsFileSeek::~CTsFileSeek(void)
@@ -73,49 +72,35 @@
   filePos/=188;
   filePos*=188;
 
-  if( m_duration.FirstStartPcr() > m_duration.EndPcr() )
-  {
-    // no PCR rollover is allowed when using binary search with seeking
-    m_useBinarySearch = false;
-  }
-  else
-  {
-    m_useBinarySearch = true;
-  }
-
   seekTimeStamp /= 1000.0f; // convert to seconds.
 
   m_seekPid=m_duration.GetPid();
   LogDebug("seek to %f filepos:%x pid:%x", seekTimeStamp,(DWORD)filePos, m_seekPid);
   
   byte buffer[188*10];
-  if (filePos<=0)
-  {
-    //no need to seek for timestamp 0, 
-    //simply set the pointer at the beginning of the file
-    m_reader->SetFilePointer(0,FILE_BEGIN);
-    return;
-  }
-  if (filePos+sizeof(buffer) > m_reader->GetFileSize())
-  {
-    //no need to seek when we want to seek to end of file
-    //simply set the pointer at the end of the file
-    m_reader->SetFilePointer(0,FILE_END);
-    return;
-  }
-  __int64 prevfilePos=filePos;
   __int64 binaryMax=m_reader->GetFileSize();
   __int64 binaryMin=0;
   __int64 lastFilePos=0;
   int seekingIteration=0;
 
-  SeekState state=FindPcr;
   Reset() ;   // Reset "PacketSync"
   while (true)
   {
     //sanity checks
-    if (filePos<0) return;
-    if (filePos+sizeof(buffer) > m_reader->GetFileSize()) return;
+    if (filePos<=0)
+    {
+      //no need to seek for timestamp 0,
+      //simply set the pointer at the beginning of the file
+      m_reader->SetFilePointer(0,FILE_BEGIN);
+      return;
+    }
+    if (filePos+sizeof(buffer) > m_reader->GetFileSize())
+    {
+      //no need to seek when we want to seek to end of file
+      //simply set the pointer at the end of the file
+      m_reader->SetFilePointer(0,FILE_END);
+      return;
+    }
 
     //set filepointer to filePos
     m_reader->SetFilePointer(filePos,FILE_BEGIN);
@@ -139,131 +124,56 @@
     {
       //yes. pcr found
       double clockFound=m_pcrFound.ToClock();
-      if( m_useBinarySearch )
+      double diff = clockFound - seekTimeStamp;
+      //LogDebug(" got %f at filepos %x diff %f ( %I64x, %I64x )", clockFound, (DWORD)filePos, diff, binaryMin, binaryMax);
+
+      // Make sure that seeking position is at least the target one
+      if (0 <= diff && diff <= SEEKING_ACCURACY)
       {
-        double diff = clockFound - seekTimeStamp;
-        //LogDebug(" got %f at filepos %x diff %f ( %I64x, %I64x )", clockFound, (DWORD)filePos, diff, binaryMin, binaryMax);
-          
-        // Make sure that seeking position is at least the target one
-        if (0 <= diff && diff <= SEEKING_ACCURACY)
-        {
-          LogDebug(" stop seek: %f at %x - target: %f, diff: %f", 
-            clockFound, (DWORD)filePos, seekTimeStamp, diff);
-          m_reader->SetFilePointer(filePos,FILE_BEGIN);
-          return;
-        }
+        LogDebug(" stop seek: %f at %x - target: %f, diff: %f",
+          clockFound, (DWORD)filePos, seekTimeStamp, diff);
+        m_reader->SetFilePointer(filePos,FILE_BEGIN);
+        return;
+      }
 
-        seekingIteration++;
-        if( seekingIteration > MAX_SEEKING_ITERATIONS )
-        {
-          LogDebug(" stop seek max iterations reached (%d): %f at %x - target: %f, diff: %f", 
-            MAX_SEEKING_ITERATIONS, clockFound, (DWORD)filePos, seekTimeStamp, diff);
-          m_reader->SetFilePointer(filePos,FILE_BEGIN);
-          return;
-        }
+      seekingIteration++;
+      if( seekingIteration > MAX_SEEKING_ITERATIONS )
+      {
+        LogDebug(" stop seek max iterations reached (%d): %f at %x - target: %f, diff: %f",
+          MAX_SEEKING_ITERATIONS, clockFound, (DWORD)filePos, seekTimeStamp, diff);
+        m_reader->SetFilePointer(filePos,FILE_BEGIN);
+        return;
+      }
 
-        // lower bound becomes valid
-        if( clockFound > seekTimeStamp ) 
-        {
-          if (filePos < binaryMax) binaryMax = filePos-1;
-        }
-        else
-        {
-          if (filePos > binaryMin) binaryMin = filePos+1;
-        }
-
-        if (lastFilePos==filePos)
-        {
-          LogDebug(" stop seek closer target found : %f at %x - target: %f, diff: %f", 
-            clockFound, (DWORD)filePos, seekTimeStamp, diff);
-          m_reader->SetFilePointer(filePos,FILE_BEGIN);
-          return;
-        }
-
-        lastFilePos=filePos;
-        filePos = binaryMin + ( binaryMax - binaryMin ) / 2;
-        Reset() ;   // Random jump, Reset "PacketSync"
+      // lower bound becomes valid
+      if( clockFound > seekTimeStamp )
+      {
+        if (filePos < binaryMax) binaryMax = filePos-1;
       }
       else
       {
-        if (state==FindPcr)
-        {
-          prevfilePos=filePos;
-          //we found the pcr.
-          //compare it with the timestamp we want to seek to
-          if (clockFound < seekTimeStamp)
-          {
-            // pcr found is too low, move forward in file and seek next pcr
-            state=FindNextPcr;
-          
-            //LogDebug(" got %f at filepos %x ->find next", clockFound, (DWORD)filePos);
-            filePos += sizeof(buffer);
-          }
-          else if (clockFound > seekTimeStamp)
-          {
-            // pcr found is too high, move backward in file and seek previous pcr
-            //LogDebug(" got %f at filepos %x ->find prev", clockFound, (DWORD)filePos);
-            state=FindPreviousPcr;
-            filePos -= sizeof(buffer);
-            Reset() ;   // Backward jump, Reset "PacketSync"
-          }
-          else
-          {
-            //pcr is correct, just return
-            //LogDebug(" got %f", clockFound);
-            m_reader->SetFilePointer(filePos,FILE_BEGIN);
-            return;
-          }
-        }
-        else
-        {
-          //pcr found, check state
-          if (state==FindNextPcr)
-          {
-            //LogDebug(" got %f at filepos %x", clockFound, (DWORD)filePos);
-            //looking for a pcr > seektime
-            if (clockFound > seekTimeStamp)
-            {
-              //found it..
-              //LogDebug(" stop seek too big: %f at %x", clockFound, (DWORD)filePos);
-              m_reader->SetFilePointer(prevfilePos,FILE_BEGIN);
-              return;
-            }
-            prevfilePos=filePos;
-            filePos+=sizeof(buffer);
-          }
-          else if (state==FindPreviousPcr)
-          {
-            //LogDebug(" got %f at filepos %x", clockFound, (DWORD)filePos);
-            //looking for a pcr < seektime
-            if (clockFound < seekTimeStamp)
-            {
-              //found it...
-              //LogDebug(" stop seek too small: %f at %x", clockFound, (DWORD)filePos);
-              m_reader->SetFilePointer(filePos,FILE_BEGIN);
-              return;
-            }
-            prevfilePos=filePos;
-            filePos-=sizeof(buffer);
-            Reset() ;   // Backward jump, Reset "PacketSync"
-          }
-        }
+        if (filePos > binaryMin) binaryMin = filePos+1;
       }
+
+      lastFilePos=filePos;
+      filePos = binaryMin + ( binaryMax - binaryMin ) / 2;
+      filePos/=188;
+      filePos*=188;
+
+      if (lastFilePos==filePos)
+      {
+        LogDebug(" stop seek closer target found : %f at %x - target: %f, diff: %f",
+          clockFound, (DWORD)filePos, seekTimeStamp, diff);
+        m_reader->SetFilePointer(filePos,FILE_BEGIN);
+        return;
+      }
+
+      Reset() ;   // Random jump, Reset "PacketSync"
     }
     else // no first PCR
     {
-      //no pcr found.
-      if (state == FindPreviousPcr)
-      {
-        //move filepointer back and continue searching for a PCR
-        filePos -=sizeof(buffer);
-        Reset() ;   // Backward jump, Reset "PacketSync"
-      }
-      else
-      {
-        //move filepointer forward and continue searching for a PCR
-        filePos += sizeof(buffer);
-      }
+      //move filepointer forward and continue searching for a PCR
+      filePos += sizeof(buffer);
     }
   }
 }
@@ -290,7 +200,7 @@
     {
       // pid is valid
       // did we have a pcr rollover ??
-      if (m_duration.MaxPcr().IsValid)
+      if (m_duration.FirstStartPcr() > m_duration.EndPcr())
       {
         //pcr rollover occured.
         //next we need to convert the pcr into filestamp
@@ -298,23 +208,28 @@
         //but the file can start with any pcr timestamp
         if (field.Pcr.ToClock() <=m_duration.EndPcr().ToClock())
         {
-          // pcr < endpcr
-          //   pcrFound= (pcr-startpcr)
+          // pcr < endpcr (second half of the file)
+          //   pcrFound= pcr+(MAXIMUM_PCR - startpcr)
+          // StartPcr------>(0x1ffffffff;0x1ff), (0x0,0x0)--------->EndPcr
           m_pcrFound=field.Pcr;
           double d1=m_pcrFound.ToClock();
-          double start=m_duration.StartPcr().ToClock();//earliest pcr available in the file
-          d1-=start;
+          CPcr pcr2;
+          pcr2.PcrReferenceBase = 0x1ffffffffULL;
+          pcr2.PcrReferenceExtension = 0x1ffULL;
+          double start=pcr2.ToClock()- m_duration.StartPcr().ToClock();
+          d1+=start;
           m_pcrFound.FromClock(d1);
         }
         else
         {
-          //PCR > endpcr
-          //   pcrFound- pcr+(maxPcr-startpcr)
-          // StartPcr------>MaxPcr--->0--------->EndPcr
+          //PCR > endpcr (first half of the file)
+          //   pcrFound= (pcr-startpcr)
           m_pcrFound=field.Pcr;
           double d1=m_pcrFound.ToClock();
-          double start=m_duration.MaxPcr().ToClock()- m_duration.StartPcr().ToClock();
-          d1+=start;
+          double start=m_duration.StartPcr().ToClock();//earliest pcr available in the file
+          LogDebug(" found clock %f earliest is %f", d1, start);
+          d1-=start;
+          LogDebug(" after sub %f", d1);
           m_pcrFound.FromClock(d1);
         }
       }
Index: TsFileSeek.h
===================================================================
--- TsFileSeek.h	(revision 27986)
+++ TsFileSeek.h	(working copy)
@@ -8,12 +8,6 @@
 class CTsFileSeek: public CPacketSync
 {
 public:
-  enum SeekState
-  {
-    FindPreviousPcr=-1,
-    FindPcr=0,
-    FindNextPcr=1
-  };
   CTsFileSeek( CTsDuration& duration );
   virtual ~CTsFileSeek(void);
 	void OnTsPacket(byte* tsPacket);
@@ -25,5 +19,4 @@
   CTsDuration&  m_duration;
   CPcr          m_pcrFound;
   int           m_seekPid;
-  bool          m_useBinarySearch;
 };
