1 #include <lib/dvb/pvrparse.h>
2 #include <lib/base/eerror.h>
6 #error no byte order defined!
9 int eMPEGStreamInformation::save(const char *filename)
11 FILE *f = fopen(filename, "wb");
15 for (std::map<off_t, pts_t>::const_iterator i(m_access_points.begin()); i != m_access_points.end(); ++i)
17 unsigned long long d[2];
18 #if BYTE_ORDER == BIG_ENDIAN
22 d[0] = bswap_64(i->first);
23 d[1] = bswap_64(i->second);
25 fwrite(d, sizeof(d), 1, f);
32 int eMPEGStreamInformation::load(const char *filename)
34 FILE *f = fopen(filename, "rb");
37 m_access_points.clear();
40 unsigned long long d[2];
41 if (fread(d, sizeof(d), 1, f) < 1)
44 #if BYTE_ORDER == LITTLE_ENDIAN
45 d[0] = bswap_64(d[0]);
46 d[1] = bswap_64(d[1]);
48 m_access_points[d[0]] = d[1];
51 fixupDiscontinuties();
55 bool eMPEGStreamInformation::empty()
57 return m_access_points.empty();
60 void eMPEGStreamInformation::fixupDiscontinuties()
62 m_timestamp_deltas.clear();
63 if (!m_access_points.size())
66 // eDebug("Fixing discontinuities ...");
68 /* if we have no delta at the beginning, extrapolate it */
69 if ((m_access_points.find(0) == m_access_points.end()) && (m_access_points.size() > 1))
71 std::map<off_t,pts_t>::const_iterator second = m_access_points.begin();
72 std::map<off_t,pts_t>::const_iterator first = second++;
73 if (first->first < second->first) /* i.e., not equal or broken */
75 off_t diff = second->first - first->first;
76 pts_t tdiff = second->second - first->second;
77 tdiff *= first->first;
79 m_timestamp_deltas[0] = first->second - tdiff;
80 // eDebug("first delta is %08llx", first->second - tdiff);
84 if (m_timestamp_deltas.empty())
85 m_timestamp_deltas[m_access_points.begin()->first] = m_access_points.begin()->second;
87 pts_t currentDelta = m_timestamp_deltas.begin()->second, lastpts_t = 0;
88 for (std::map<off_t,pts_t>::const_iterator i(m_access_points.begin()); i != m_access_points.end(); ++i)
90 pts_t current = i->second - currentDelta;
91 pts_t diff = current - lastpts_t;
93 if (llabs(diff) > (90000*5)) // 5sec diff
95 // eDebug("%llx < %llx, have discont. new timestamp is %llx (diff is %llx)!", current, lastpts_t, i->second, diff);
96 currentDelta = i->second - lastpts_t; /* FIXME: should be the extrapolated new timestamp, based on the current rate */
97 // eDebug("current delta now %llx, making current to %llx", currentDelta, i->second - currentDelta);
98 m_timestamp_deltas[i->first] = currentDelta;
100 lastpts_t = i->second - currentDelta;
104 // eDebug("ok, found %d disconts.", m_timestamp_deltas.size());
107 for (off_t x=0x25807E34ULL; x < 0x25B3CF70; x+= 100000)
111 int r = getPTS(o, p);
112 eDebug("%08llx -> %08llx | %08llx, %d, %08llx %08llx", x, getDelta(x), getInterpolated(x), r, o, p);
117 pts_t eMPEGStreamInformation::getDelta(off_t offset)
119 if (!m_timestamp_deltas.size())
121 std::map<off_t,pts_t>::iterator i = m_timestamp_deltas.upper_bound(offset);
123 /* i can be the first when you query for something before the first PTS */
124 if (i != m_timestamp_deltas.begin())
130 int eMPEGStreamInformation::fixupPTS(const off_t &offset, pts_t &ts)
132 if (!m_timestamp_deltas.size())
135 std::map<off_t, pts_t>::const_iterator i = m_access_points.upper_bound(offset - 4 * 1024 * 1024), nearest = m_access_points.end();
137 while (i != m_access_points.end())
139 if ((nearest == m_access_points.end()) || (llabs(i->second - ts) < llabs(nearest->second - ts)))
143 if (nearest == m_access_points.end())
145 ts -= getDelta(nearest->first);
149 int eMPEGStreamInformation::getPTS(off_t &offset, pts_t &pts)
151 std::map<off_t,pts_t>::iterator before = m_access_points.lower_bound(offset);
153 /* usually, we prefer the AP before the given offset. however if there is none, we take any. */
154 if (before != m_access_points.begin())
157 if (before == m_access_points.end())
163 offset = before->first;
164 pts = before->second - getDelta(offset);
169 pts_t eMPEGStreamInformation::getInterpolated(off_t offset)
171 /* get the PTS values before and after the offset. */
172 std::map<off_t,pts_t>::iterator before, after;
174 after = m_access_points.upper_bound(offset);
177 if (before != m_access_points.begin())
179 else /* we query before the first known timestamp ... FIXME */
183 if (before == m_access_points.end())
186 /* if after == end, then we need to extrapolate ... FIXME */
187 if ((before->first == offset) || (after == m_access_points.end()))
188 return before->second - getDelta(offset);
190 pts_t before_ts = before->second - getDelta(before->first);
191 pts_t after_ts = after->second - getDelta(after->first);
193 // eDebug("%08llx .. ? .. %08llx", before_ts, after_ts);
194 // eDebug("%08llx .. %08llx .. %08llx", before->first, offset, after->first);
196 pts_t diff = after_ts - before_ts;
197 off_t diff_off = after->first - before->first;
199 diff = (offset - before->first) * diff / diff_off;
200 // eDebug("%08llx .. %08llx .. %08llx", before_ts, before_ts + diff, after_ts);
201 return before_ts + diff;
204 off_t eMPEGStreamInformation::getAccessPoint(pts_t ts)
206 /* FIXME: more efficient implementation */
209 for (std::map<off_t, pts_t>::const_iterator i(m_access_points.begin()); i != m_access_points.end(); ++i)
211 pts_t delta = getDelta(i->first);
212 pts_t c = i->second - delta;
220 int eMPEGStreamInformation::getNextAccessPoint(pts_t &ts, const pts_t &start, int direction)
222 off_t offset = getAccessPoint(start);
223 std::map<off_t, pts_t>::const_iterator i = m_access_points.find(offset);
224 if (i == m_access_points.end())
226 eDebug("getNextAccessPoint: initial AP not found");
233 if (i == m_access_points.end())
240 if (i == m_access_points.begin())
249 ts = i->second - getDelta(i->first);
250 eDebug("fine, at %llx - %llx = %llx", ts, i->second, getDelta(i->first));
251 eDebug("fine, at %lld - %lld = %lld", ts, i->second, getDelta(i->first));
255 eMPEGStreamParserTS::eMPEGStreamParserTS(eMPEGStreamInformation &streaminfo): m_streaminfo(streaminfo), m_pktptr(0), m_pid(-1), m_need_next_packet(0), m_skip(0)
259 int eMPEGStreamParserTS::processPacket(const unsigned char *pkt, off_t offset)
261 if (!wantPacket(pkt))
262 eWarning("something's wrong.");
264 const unsigned char *end = pkt + 188;
266 if (!(pkt[3] & 0x10))
268 eWarning("[TSPARSE] PUSI set but no payload.");
272 if (pkt[3] & 0x20) // adaption field present?
273 pkt += pkt[4] + 4 + 1; /* skip adaption field and header */
275 pkt += 4; /* skip header */
279 eWarning("[TSPARSE] dropping huge adaption field");
283 // ok, we now have the start of the payload, aligned with the PES packet start.
284 if (pkt[0] || pkt[1] || (pkt[2] != 1))
286 eWarning("broken startcode");
294 if (pkt[7] & 0x80) // PTS present?
296 pts = ((unsigned long long)(pkt[ 9]&0xE)) << 29;
297 pts |= ((unsigned long long)(pkt[10]&0xFF)) << 22;
298 pts |= ((unsigned long long)(pkt[11]&0xFE)) << 14;
299 pts |= ((unsigned long long)(pkt[12]&0xFF)) << 7;
300 pts |= ((unsigned long long)(pkt[13]&0xFE)) >> 1;
304 int sec = pts / 90000;
305 int frm = pts % 90000;
313 eDebug("pts: %016llx %d:%02d:%02d:%02d:%05d", pts, d, hr, min, sec, frm);
317 /* advance to payload */
320 /* if startcode found */
321 if (!(pkt[0] || pkt[1] || (pkt[2] != 1)))
323 if (pkt[3] == 0xb3) /* sequence header */
327 m_streaminfo.m_access_points[offset] = pts;
328 eDebug("Sequence header at %llx, pts %llx", offset, pts);
330 eDebug("Sequence header but no valid PTS value.");
336 inline int eMPEGStreamParserTS::wantPacket(const unsigned char *hdr) const
340 eDebug("missing sync!");
343 int ppid = ((hdr[1]&0x1F) << 8) | hdr[2];
348 if (m_need_next_packet) /* next packet (on this pid) was required? */
351 if (hdr[1] & 0x40) /* pusi set: yes. */
357 void eMPEGStreamParserTS::parseData(off_t offset, const void *data, unsigned int len)
359 const unsigned char *packet = (const unsigned char*)data;
360 const unsigned char *packet_start = packet;
362 /* sorry for the redundant code here, but there are too many special cases... */
365 /* emergency resync. usually, this should not happen, because the data should
368 to make this code work for non-strictly-sync-aligned data, (for example, bad
369 files) we fix a possible resync here by skipping data until the next 0x47.
371 if this is a false 0x47, the packet will be dropped by wantPacket, and the
372 next time, sync will be re-established. */
374 while (!m_pktptr && len)
376 if (packet[0] == 0x47)
384 eDebug("SYNC LOST: skipped %d bytes.", skipped);
391 /* skip last packet */
394 unsigned int skiplen = -m_pktptr;
401 } else if (m_pktptr < 4) /* header not complete, thus we don't know if we want this packet */
403 unsigned int storelen = 4 - m_pktptr;
406 memcpy(m_pkt + m_pktptr, packet, storelen);
408 m_pktptr += storelen;
413 if (!wantPacket(m_pkt))
422 /* otherwise we complete up to the full packet */
423 unsigned int storelen = 188 - m_pktptr;
426 memcpy(m_pkt + m_pktptr, packet, storelen);
427 m_pktptr += storelen;
433 m_need_next_packet = processPacket(m_pkt, offset + (packet - packet_start));
436 } else if (len >= 4) /* if we have a full header... */
438 if (wantPacket(packet)) /* decide wheter we need it ... */
440 if (len >= 188) /* packet complete? */
442 m_need_next_packet = processPacket(packet, offset + (packet - packet_start)); /* process it now. */
445 memcpy(m_pkt, packet, len); /* otherwise queue it up */
454 else if (!m_pktptr) /* we dont want this packet, otherwise m_pktptr = sk (=len) > 4 */
459 } else /* if we don't have a complete header */
461 memcpy(m_pkt, packet, len); /* complete header next time */
469 void eMPEGStreamParserTS::setPid(int _pid)