+int eDVBTSTools::calcBitrate()
+{
+ calcBegin(); calcEnd();
+ if (!(m_begin_valid && m_end_valid))
+ return -1;
+
+ pts_t len_in_pts = m_pts_end - m_pts_begin;
+
+ /* wrap around? */
+ if (len_in_pts < 0)
+ len_in_pts += 0x200000000LL;
+ off_t len_in_bytes = m_offset_end - m_offset_begin;
+
+ if (!len_in_pts)
+ return -1;
+
+ unsigned long long bitrate = len_in_bytes * 90000 * 8 / len_in_pts;
+ if ((bitrate < 10000) || (bitrate > 100000000))
+ return -1;
+
+ return bitrate;
+}
+
+ /* pts, off */
+void eDVBTSTools::takeSamples()
+{
+ m_samples_taken = 1;
+ m_samples.clear();
+ pts_t dummy;
+ if (calcLen(dummy) == -1)
+ return;
+
+ int nr_samples = 30;
+ off_t bytes_per_sample = (m_offset_end - m_offset_begin) / (long long)nr_samples;
+ if (bytes_per_sample < 40*1024*1024)
+ bytes_per_sample = 40*1024*1024;
+
+ bytes_per_sample -= bytes_per_sample % 188;
+
+ for (off_t offset = m_offset_begin; offset < m_offset_end; offset += bytes_per_sample)
+ {
+ pts_t p;
+ takeSample(offset, p);
+ }
+ m_samples[0] = m_offset_begin;
+ m_samples[m_pts_end - m_pts_begin] = m_offset_end;
+
+// eDebug("begin, end: %llx %llx", m_offset_begin, m_offset_end);
+}
+
+ /* returns 0 when a sample was taken. */
+int eDVBTSTools::takeSample(off_t off, pts_t &p)
+{
+ 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 %llx %llx %llx (%lld %lld %lld)",
+ l->second, off, u->second, l->first, p, u->first);
+ return 1;
+ }
+ }
+ }
+
+
+ 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_file.valid())
+ {
+ eDebug(" file not valid");
+ return -1;
+ }
+
+ if (m_file.lseek(0, SEEK_SET) < 0)
+ {
+ eDebug("seek failed");
+ return -1;
+ }
+
+ int left = 5*1024*1024;
+
+ while (left >= 188)
+ {
+ unsigned char packet[188];
+ if (m_file.read(packet, 188) != 188)
+ {
+ eDebug("read error");
+ break;
+ }
+ left -= 188;
+
+ if (packet[0] != 0x47)
+ {
+ int i = 0;
+ while (i < 188)
+ {
+ if (packet[i] == 0x47)
+ break;
+ ++i;
+ }
+ m_file.lseek(i - 188, SEEK_CUR);
+ 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;
+}