Merge branch 'master' into tmbinc/FixTimingBugs
[enigma2.git] / lib / dvb / pvrparse.cpp
index 58b215ba00cabcca9278c5eae018dfe347e01269..c7a37460a44619de77dc4ad9633ef15c2d4e7d1b 100644 (file)
@@ -35,6 +35,7 @@ int eMPEGStreamInformation::load(const char *filename)
        if (!f)
                return -1;
        m_access_points.clear();
+       m_pts_to_offset.clear();
        while (1)
        {
                unsigned long long d[2];
@@ -46,6 +47,7 @@ int eMPEGStreamInformation::load(const char *filename)
                d[1] = bswap_64(d[1]);
 #endif
                m_access_points[d[0]] = d[1];
+               m_pts_to_offset.insert(std::pair<pts_t,off_t>(d[1], d[0]));
        }
        fclose(f);
        fixupDiscontinuties();
@@ -63,7 +65,7 @@ void eMPEGStreamInformation::fixupDiscontinuties()
        if (!m_access_points.size())
                return;
                
-       eDebug("Fixing discontinuities ...");
+//     eDebug("Fixing discontinuities ...");
 
                        /* if we have no delta at the beginning, extrapolate it */
        if ((m_access_points.find(0) == m_access_points.end()) && (m_access_points.size() > 1))
@@ -77,7 +79,7 @@ void eMPEGStreamInformation::fixupDiscontinuties()
                        tdiff *= first->first;
                        tdiff /= diff;
                        m_timestamp_deltas[0] = first->second - tdiff;
-                       eDebug("first delta is %08llx", first->second - tdiff);
+//                     eDebug("first delta is %08llx", first->second - tdiff);
                }
        }
 
@@ -92,16 +94,16 @@ void eMPEGStreamInformation::fixupDiscontinuties()
                
                if (llabs(diff) > (90000*5)) // 5sec diff
                {
-                       eDebug("%llx < %llx, have discont. new timestamp is %llx (diff is %llx)!", current, lastpts_t, i->second, diff);
+//                     eDebug("%llx < %llx, have discont. new timestamp is %llx (diff is %llx)!", current, lastpts_t, i->second, diff);
                        currentDelta = i->second - lastpts_t; /* FIXME: should be the extrapolated new timestamp, based on the current rate */
-                       eDebug("current delta now %llx, making current to %llx", currentDelta, i->second - currentDelta);
+//                     eDebug("current delta now %llx, making current to %llx", currentDelta, i->second - currentDelta);
                        m_timestamp_deltas[i->first] = currentDelta;
                }
                lastpts_t = i->second - currentDelta;
        }
        
        
-       eDebug("ok, found %d disconts.", m_timestamp_deltas.size());
+//     eDebug("ok, found %d disconts.", m_timestamp_deltas.size());
 
 #if 0  
        for (off_t x=0x25807E34ULL; x < 0x25B3CF70; x+= 100000)
@@ -123,7 +125,7 @@ pts_t eMPEGStreamInformation::getDelta(off_t offset)
                /* i can be the first when you query for something before the first PTS */
        if (i != m_timestamp_deltas.begin())
                --i;
-       
+
        return i->second;
 }
 
@@ -132,24 +134,29 @@ int eMPEGStreamInformation::fixupPTS(const off_t &offset, pts_t &ts)
        if (!m_timestamp_deltas.size())
                return -1;
 
-       std::map<off_t, pts_t>::const_iterator i = m_access_points.upper_bound(offset - 4 * 1024 * 1024), nearest = m_access_points.end();
-       
-       while (i != m_access_points.end())
+       std::multimap<pts_t, off_t>::const_iterator 
+               l = m_pts_to_offset.upper_bound(ts - 60 * 90000), 
+               u = m_pts_to_offset.upper_bound(ts + 60 * 90000), 
+               nearest = m_pts_to_offset.end();
+
+       while (l != u)
        {
-               if ((nearest == m_access_points.end()) || (llabs(i->second - ts) < llabs(nearest->second - ts)))
-                       nearest = i;
-               ++i;
+               if ((nearest == m_pts_to_offset.end()) || (llabs(l->first - ts) < llabs(nearest->first - ts)))
+                       nearest = l;
+               ++l;
        }
-       if (nearest == m_access_points.end())
-               return -1;
-       ts -= getDelta(nearest->first);
+       if (nearest == m_pts_to_offset.end())
+               return 1;
+
+       ts -= getDelta(nearest->second);
+
        return 0;
 }
 
 int eMPEGStreamInformation::getPTS(off_t &offset, pts_t &pts)
 {
        std::map<off_t,pts_t>::iterator before = m_access_points.lower_bound(offset);
-       
+
                /* usually, we prefer the AP before the given offset. however if there is none, we take any. */
        if (before != m_access_points.begin())
                --before;
@@ -170,7 +177,6 @@ pts_t eMPEGStreamInformation::getInterpolated(off_t offset)
 {
                /* get the PTS values before and after the offset. */
        std::map<off_t,pts_t>::iterator before, after;
-       
        after = m_access_points.upper_bound(offset);
        before = after;
 
@@ -201,22 +207,81 @@ pts_t eMPEGStreamInformation::getInterpolated(off_t offset)
        return before_ts + diff;
 }
  
-off_t eMPEGStreamInformation::getAccessPoint(pts_t ts)
+off_t eMPEGStreamInformation::getAccessPoint(pts_t ts, int marg)
 {
                /* FIXME: more efficient implementation */
-       pts_t delta = 0;
        off_t last = 0;
+       off_t last2 = 0;
+       pts_t lastc = 0;
        for (std::map<off_t, pts_t>::const_iterator i(m_access_points.begin()); i != m_access_points.end(); ++i)
        {
-               std::map<off_t, pts_t>::const_iterator d = m_timestamp_deltas.find(i->first);
-               if (d != m_timestamp_deltas.end())
-                       delta = d->second;
+               pts_t delta = getDelta(i->first);
                pts_t c = i->second - delta;
-               if (c > ts)
-                       break;
+               if (c > ts) {
+                       if (marg > 0)
+                               return (last + i->first)/376*188;
+                       else if (marg < 0)
+                               return (last + last2)/376*188;
+                       else
+                               return last;
+               }
+               lastc = c;
+               last2 = last;
                last = i->first;
        }
-       return last;
+       if (marg < 0)
+               return (last + last2)/376*188;
+       else
+               return last;
+}
+
+int eMPEGStreamInformation::getNextAccessPoint(pts_t &ts, const pts_t &start, int direction)
+{
+       off_t offset = getAccessPoint(start);
+       pts_t c1, c2;
+       std::map<off_t, pts_t>::const_iterator i = m_access_points.find(offset);
+       if (i == m_access_points.end())
+       {
+               eDebug("getNextAccessPoint: initial AP not found");
+               return -1;
+       }
+       c1 = i->second - getDelta(i->first);
+       while (direction)
+       {
+               if (direction > 0)
+               {
+                       if (i == m_access_points.end())
+                               return -1;
+                       ++i;
+                       c2 = i->second - getDelta(i->first);
+                       if (c1 == c2) { // Discontinuity
+                               ++i;
+                               c2 = i->second - getDelta(i->first);
+                       }
+                       c1 = c2;
+                       direction--;
+               }
+               if (direction < 0)
+               {
+                       if (i == m_access_points.begin())
+                       {
+                               eDebug("at start");
+                               return -1;
+                       }
+                       --i;
+                       c2 = i->second - getDelta(i->first);
+                       if (c1 == c2) { // Discontinuity
+                               --i;
+                               c2 = i->second - getDelta(i->first);
+                       }
+                       c1 = c2;
+                       direction++;
+               }
+       }
+       ts = i->second - getDelta(i->first);
+       eDebug("fine, at %llx - %llx = %llx", ts, i->second, getDelta(i->first));
+       eDebug("fine, at %lld - %lld = %lld", ts, i->second, getDelta(i->first));
+       return 0;
 }
 
 eMPEGStreamParserTS::eMPEGStreamParserTS(eMPEGStreamInformation &streaminfo): m_streaminfo(streaminfo), m_pktptr(0), m_pid(-1), m_need_next_packet(0), m_skip(0)
@@ -283,8 +348,16 @@ int eMPEGStreamParserTS::processPacket(const unsigned char *pkt, off_t offset)
        
                /* advance to payload */
        pkt += pkt[8] + 9;
-       
+
+               /* sometimes, there are zeros before the startcode. */
+       while (pkt < (end-4))
+               if (pkt[0] || pkt[1] || pkt[2])
+                       break;
+               else
+                       pkt++;
+
                /* if startcode found */
+//     eDebug("%02x %02x %02x %02x", pkt[0], pkt[1], pkt[2], pkt[3]);
        if (!(pkt[0] || pkt[1] || (pkt[2] != 1)))
        {
                if (pkt[3] == 0xb3) /* sequence header */
@@ -292,9 +365,20 @@ int eMPEGStreamParserTS::processPacket(const unsigned char *pkt, off_t offset)
                        if (ptsvalid)
                        {
                                m_streaminfo.m_access_points[offset] = pts;
-                               eDebug("Sequence header at %llx, pts %llx", offset, pts);
+//                             eDebug("Sequence header at %llx, pts %llx", offset, pts);
+                       } else
+                               /*eDebug("Sequence header but no valid PTS value.")*/;
+               }
+
+               if (pkt[3] == 0x09 &&   /* MPEG4 AVC unit access delimiter */
+                        (pkt[4] >> 5) == 0) /* and I-frame */
+               {
+                       if (ptsvalid)
+                       {
+                               m_streaminfo.m_access_points[offset] = pts;
+//                             eDebug("MPEG4 AVC UAD at %llx, pts %llx", offset, pts);
                        } else
-                               eDebug("Sequence header but no valid PTS value.");
+                               /*eDebug("MPEG4 AVC UAD but no valid PTS value.")*/;
                }
        }
        return 0;
@@ -329,6 +413,30 @@ void eMPEGStreamParserTS::parseData(off_t offset, const void *data, unsigned int
                        /* sorry for the redundant code here, but there are too many special cases... */
        while (len)
        {
+                       /* emergency resync. usually, this should not happen, because the data should 
+                          be sync-aligned.
+                          
+                          to make this code work for non-strictly-sync-aligned data, (for example, bad 
+                          files) we fix a possible resync here by skipping data until the next 0x47.
+                          
+                          if this is a false 0x47, the packet will be dropped by wantPacket, and the
+                          next time, sync will be re-established. */
+               int skipped = 0;
+               while (!m_pktptr && len)
+               {
+                       if (packet[0] == 0x47)
+                               break;
+                       len--;
+                       packet++;
+                       skipped++;
+               }
+               
+               if (skipped)
+                       eDebug("SYNC LOST: skipped %d bytes.", skipped);
+               
+               if (!len)
+                       break;
+               
                if (m_pktptr)
                {
                                /* skip last packet */