+ if (takeSample(offset, p) && retries--)
+ continue;
+ retries = 2;
+ offset += bytes_per_sample;
+ }
+ m_samples[0] = m_offset_begin;
+ m_samples[m_pts_end - m_pts_begin] = m_offset_end;
+}
+
+ /* returns 0 when a sample was taken. */
+int eDVBTSTools::takeSample(off_t off, pts_t &p)
+{
+ off_t offset_org = off;
+
+ if (!eDVBTSTools::getPTS(off, p, 1))
+ {
+ /* as we are happily mixing PTS and PCR values (no comment, please), we might
+ end up with some "negative" segments.
+
+ so check if this new sample is between the previous and the next field*/
+
+ std::map<pts_t, off_t>::const_iterator l = m_samples.lower_bound(p);
+ std::map<pts_t, off_t>::const_iterator u = l;
+
+ if (l != m_samples.begin())
+ {
+ --l;
+ if (u != m_samples.end())
+ {
+ if ((l->second > off) || (u->second < off))
+ {
+ eDebug("ignoring sample %lld %lld %lld (%llx %llx %llx)",
+ l->second, off, u->second, l->first, p, u->first);
+ return 1;
+ }
+ }
+ }
+
+ eDebug("adding sample %lld: pts 0x%llx -> pos %lld (diff %lld bytes)", offset_org, p, off, off-offset_org);
+ m_samples[p] = off;
+ return 0;
+ }
+ return -1;
+}
+
+int eDVBTSTools::findPMT(int &pmt_pid, int &service_id)
+{
+ /* FIXME: this will be factored out soon! */
+ if (!m_source || !m_source->valid())
+ {
+ eDebug(" file not valid");
+ return -1;
+ }
+
+ off_t position=0;
+
+ int left = 5*1024*1024;
+
+ while (left >= 188)
+ {
+ unsigned char packet[188];
+ int ret = m_source->read(position, packet, 188);
+ if (ret != 188)
+ {
+ eDebug("read error");
+ break;
+ }
+ left -= 188;
+ position += 188;
+
+ if (packet[0] != 0x47)
+ {
+ int i = 0;
+ while (i < 188)
+ {
+ if (packet[i] == 0x47)
+ break;
+ --position;
+ ++i;
+ }
+ continue;
+ }
+ int pid = ((packet[1] << 8) | packet[2]) & 0x1FFF;
+
+ int pusi = !!(packet[1] & 0x40);
+
+ if (!pusi)
+ continue;
+
+ /* ok, now we have a PES header or section header*/
+ unsigned char *sec;
+
+ /* check for adaption field */
+ if (packet[3] & 0x20)
+ {
+ if (packet[4] >= 183)
+ continue;
+ sec = packet + packet[4] + 4 + 1;
+ } else
+ sec = packet + 4;
+
+ if (sec[0]) /* table pointer, assumed to be 0 */
+ continue;
+
+ if (sec[1] == 0x02) /* program map section */
+ {
+ pmt_pid = pid;
+ service_id = (sec[4] << 8) | sec[5];
+ return 0;
+ }
+ }
+
+ return -1;
+}
+
+int eDVBTSTools::findFrame(off_t &_offset, size_t &len, int &direction, int frame_types)
+{
+ off_t offset = _offset;
+ int nr_frames = 0;
+// eDebug("trying to find iFrame at %llx", offset);
+
+ if (!m_use_streaminfo)
+ {
+// eDebug("can't get next iframe without streaminfo");
+ return -1;
+ }
+
+ /* let's find the iframe before the given offset */
+ unsigned long long data;
+
+ if (direction < 0)
+ offset--;
+
+ while (1)
+ {
+ if (m_streaminfo.getStructureEntry(offset, data, (direction == 0) ? 1 : 0))