fix cable and terrestrial scan
[enigma2.git] / lib / dvb / pvrparse.cpp
1 #include <lib/dvb/pvrparse.h>
2 #include <lib/base/eerror.h>
3 #include <byteswap.h>
4
5 #ifndef BYTE_ORDER
6 #error no byte order defined!
7 #endif
8
9 int eMPEGStreamInformation::save(const char *filename)
10 {
11         FILE *f = fopen(filename, "wb");
12         if (!f)
13                 return -1;
14         
15         for (std::map<off_t, pts_t>::const_iterator i(m_access_points.begin()); i != m_access_points.end(); ++i)
16         {
17                 unsigned long long d[2];
18 #if BYTE_ORDER == BIG_ENDIAN
19                 d[0] = i->first;
20                 d[1] = i->second;
21 #else
22                 d[0] = bswap_64(i->first);
23                 d[1] = bswap_64(i->second);
24 #endif
25                 fwrite(d, sizeof(d), 1, f);
26         }
27         fclose(f);
28         
29         return 0;
30 }
31
32 int eMPEGStreamInformation::load(const char *filename)
33 {
34         FILE *f = fopen(filename, "rb");
35         if (!f)
36                 return -1;
37         m_access_points.clear();
38         while (1)
39         {
40                 unsigned long long d[2];
41                 if (fread(d, sizeof(d), 1, f) < 1)
42                         break;
43                 
44 #if BYTE_ORDER == LITTLE_ENDIAN
45                 d[0] = bswap_64(d[0]);
46                 d[1] = bswap_64(d[1]);
47 #endif
48                 m_access_points[d[0]] = d[1];
49         }
50         fclose(f);
51         fixupDiscontinuties();
52         return 0;
53 }
54
55 bool eMPEGStreamInformation::empty()
56 {
57         return m_access_points.empty();
58 }
59
60 void eMPEGStreamInformation::fixupDiscontinuties()
61 {
62         m_timestamp_deltas.clear();
63         if (!m_access_points.size())
64                 return;
65                 
66         eDebug("Fixing discontinuities ...");
67
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))
70         {
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 */
74                 {
75                         off_t diff = second->first - first->first;
76                         pts_t tdiff = second->second - first->second;
77                         tdiff *= first->first;
78                         tdiff /= diff;
79                         m_timestamp_deltas[0] = first->second - tdiff;
80                         eDebug("first delta is %08llx", first->second - tdiff);
81                 }
82         }
83
84         if (m_timestamp_deltas.empty())
85                 m_timestamp_deltas[m_access_points.begin()->first] = m_access_points.begin()->second;
86
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)
89         {
90                 pts_t current = i->second - currentDelta;
91                 pts_t diff = current - lastpts_t;
92                 
93                 if (llabs(diff) > (90000*5)) // 5sec diff
94                 {
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;
99                 }
100                 lastpts_t = i->second - currentDelta;
101         }
102         
103         
104         eDebug("ok, found %d disconts.", m_timestamp_deltas.size());
105
106 #if 0   
107         for (off_t x=0x25807E34ULL; x < 0x25B3CF70; x+= 100000)
108         {
109                 off_t o = x;
110                 pts_t p;
111                 int r = getPTS(o, p);
112                 eDebug("%08llx -> %08llx | %08llx, %d, %08llx %08llx", x, getDelta(x), getInterpolated(x), r, o, p);
113         }
114 #endif
115 }
116
117 pts_t eMPEGStreamInformation::getDelta(off_t offset)
118 {
119         if (!m_timestamp_deltas.size())
120                 return 0;
121         std::map<off_t,pts_t>::iterator i = m_timestamp_deltas.upper_bound(offset);
122
123                 /* i can be the first when you query for something before the first PTS */
124         if (i != m_timestamp_deltas.begin())
125                 --i;
126         
127         return i->second;
128 }
129
130 int eMPEGStreamInformation::fixupPTS(const off_t &offset, pts_t &ts)
131 {
132         if (!m_timestamp_deltas.size())
133                 return -1;
134
135         std::map<off_t, pts_t>::const_iterator i = m_access_points.upper_bound(offset - 4 * 1024 * 1024), nearest = m_access_points.end();
136         
137         while (i != m_access_points.end())
138         {
139                 if ((nearest == m_access_points.end()) || (llabs(i->second - ts) < llabs(nearest->second - ts)))
140                         nearest = i;
141                 ++i;
142         }
143         if (nearest == m_access_points.end())
144                 return -1;
145         ts -= getDelta(nearest->first);
146         return 0;
147 }
148
149 int eMPEGStreamInformation::getPTS(off_t &offset, pts_t &pts)
150 {
151         std::map<off_t,pts_t>::iterator before = m_access_points.lower_bound(offset);
152         
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())
155                 --before;
156         
157         if (before == m_access_points.end())
158         {
159                 pts = 0;
160                 return -1;
161         }
162         
163         offset = before->first;
164         pts = before->second - getDelta(offset);
165         
166         return 0;
167 }
168
169 pts_t eMPEGStreamInformation::getInterpolated(off_t offset)
170 {
171                 /* get the PTS values before and after the offset. */
172         std::map<off_t,pts_t>::iterator before, after;
173         
174         after = m_access_points.upper_bound(offset);
175         before = after;
176
177         if (before != m_access_points.begin())
178                 --before;
179         else    /* we query before the first known timestamp ... FIXME */
180                 return 0;
181
182                 /* empty... */
183         if (before == m_access_points.end())
184                 return 0;
185
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);
189         
190         pts_t before_ts = before->second - getDelta(before->first);
191         pts_t after_ts = after->second - getDelta(after->first);
192         
193 //      eDebug("%08llx .. ? .. %08llx", before_ts, after_ts);
194 //      eDebug("%08llx .. %08llx .. %08llx", before->first, offset, after->first);
195         
196         pts_t diff = after_ts - before_ts;
197         off_t diff_off = after->first - before->first;
198         
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;
202 }
203  
204 off_t eMPEGStreamInformation::getAccessPoint(pts_t ts)
205 {
206                 /* FIXME: more efficient implementation */
207         pts_t delta = 0;
208         off_t last = 0;
209         for (std::map<off_t, pts_t>::const_iterator i(m_access_points.begin()); i != m_access_points.end(); ++i)
210         {
211                 std::map<off_t, pts_t>::const_iterator d = m_timestamp_deltas.find(i->first);
212                 if (d != m_timestamp_deltas.end())
213                         delta = d->second;
214                 pts_t c = i->second - delta;
215                 if (c > ts)
216                         break;
217                 last = i->first;
218         }
219         return last;
220 }
221
222 eMPEGStreamParserTS::eMPEGStreamParserTS(eMPEGStreamInformation &streaminfo): m_streaminfo(streaminfo), m_pktptr(0), m_pid(-1), m_need_next_packet(0), m_skip(0)
223 {
224 }
225
226 int eMPEGStreamParserTS::processPacket(const unsigned char *pkt, off_t offset)
227 {
228         if (!wantPacket(pkt))
229                 eWarning("something's wrong.");
230
231         const unsigned char *end = pkt + 188;
232         
233         if (!(pkt[3] & 0x10))
234         {
235                 eWarning("[TSPARSE] PUSI set but no payload.");
236                 return 0;
237         }
238         
239         if (pkt[3] & 0x20) // adaption field present?
240                 pkt += pkt[4] + 4 + 1;  /* skip adaption field and header */
241         else
242                 pkt += 4; /* skip header */
243
244         if (pkt > end)
245         {
246                 eWarning("[TSPARSE] dropping huge adaption field");
247                 return 0;
248         }
249         
250                 // ok, we now have the start of the payload, aligned with the PES packet start.
251         if (pkt[0] || pkt[1] || (pkt[2] != 1))
252         {
253                 eWarning("broken startcode");
254                 return 0;
255         }
256         
257         
258         pts_t pts = 0;
259         int ptsvalid = 0;
260         
261         if (pkt[7] & 0x80) // PTS present?
262         {
263                 pts  = ((unsigned long long)(pkt[ 9]&0xE))  << 29;
264                 pts |= ((unsigned long long)(pkt[10]&0xFF)) << 22;
265                 pts |= ((unsigned long long)(pkt[11]&0xFE)) << 14;
266                 pts |= ((unsigned long long)(pkt[12]&0xFF)) << 7;
267                 pts |= ((unsigned long long)(pkt[13]&0xFE)) >> 1;
268                 ptsvalid = 1;
269
270 #if 0           
271                 int sec = pts / 90000;
272                 int frm = pts % 90000;
273                 int min = sec / 60;
274                 sec %= 60;
275                 int hr = min / 60;
276                 min %= 60;
277                 int d = hr / 24;
278                 hr %= 24;
279                 
280                 eDebug("pts: %016llx %d:%02d:%02d:%02d:%05d", pts, d, hr, min, sec, frm);
281 #endif
282         }
283         
284                 /* advance to payload */
285         pkt += pkt[8] + 9;
286         
287                 /* if startcode found */
288         if (!(pkt[0] || pkt[1] || (pkt[2] != 1)))
289         {
290                 if (pkt[3] == 0xb3) /* sequence header */
291                 {
292                         if (ptsvalid)
293                         {
294                                 m_streaminfo.m_access_points[offset] = pts;
295                                 eDebug("Sequence header at %llx, pts %llx", offset, pts);
296                         } else
297                                 eDebug("Sequence header but no valid PTS value.");
298                 }
299         }
300         return 0;
301 }
302
303 inline int eMPEGStreamParserTS::wantPacket(const unsigned char *hdr) const
304 {
305         if (hdr[0] != 0x47)
306         {
307                 eDebug("missing sync!");
308                 return 0;
309         }
310         int ppid = ((hdr[1]&0x1F) << 8) | hdr[2];
311
312         if (ppid != m_pid)
313                 return 0;
314                 
315         if (m_need_next_packet)  /* next packet (on this pid) was required? */
316                 return 1;
317         
318         if (hdr[1] & 0x40)       /* pusi set: yes. */
319                 return 1;
320
321         return 0;
322 }
323
324 void eMPEGStreamParserTS::parseData(off_t offset, const void *data, unsigned int len)
325 {
326         const unsigned char *packet = (const unsigned char*)data;
327         const unsigned char *packet_start = packet;
328         
329                         /* sorry for the redundant code here, but there are too many special cases... */
330         while (len)
331         {
332                 if (m_pktptr)
333                 {
334                                 /* skip last packet */
335                         if (m_pktptr < 0)
336                         {
337                                 unsigned int skiplen = -m_pktptr;
338                                 if (skiplen > len)
339                                         skiplen = len;
340                                 packet += skiplen;
341                                 len -= skiplen;
342                                 m_pktptr += skiplen;
343                                 continue;
344                         } else if (m_pktptr < 4) /* header not complete, thus we don't know if we want this packet */
345                         {
346                                 unsigned int storelen = 4 - m_pktptr;
347                                 if (storelen > len)
348                                         storelen = len;
349                                 memcpy(m_pkt + m_pktptr, packet,  storelen);
350                                 
351                                 m_pktptr += storelen;
352                                 len -= storelen;
353                                 packet += storelen;
354                                 
355                                 if (m_pktptr == 4)
356                                         if (!wantPacket(m_pkt))
357                                         {
358                                                         /* skip packet */
359                                                 packet += 184;
360                                                 len -= 184;
361                                                 m_pktptr = 0;
362                                                 continue;
363                                         }
364                         }
365                                 /* otherwise we complete up to the full packet */
366                         unsigned int storelen = 188 - m_pktptr;
367                         if (storelen > len)
368                                 storelen = len;
369                         memcpy(m_pkt + m_pktptr, packet,  storelen);
370                         m_pktptr += storelen;
371                         len -= storelen;
372                         packet += storelen;
373                         
374                         if (m_pktptr == 188)
375                         {
376                                 m_need_next_packet = processPacket(m_pkt, offset + (packet - packet_start));
377                                 m_pktptr = 0;
378                         }
379                 } else if (len >= 4)  /* if we have a full header... */
380                 {
381                         if (wantPacket(packet))  /* decide wheter we need it ... */
382                         {
383                                 if (len >= 188)          /* packet complete? */
384                                 {
385                                         m_need_next_packet = processPacket(packet, offset + (packet - packet_start)); /* process it now. */
386                                 } else
387                                 {
388                                         memcpy(m_pkt, packet, len);  /* otherwise queue it up */
389                                         m_pktptr = len;
390                                 }
391                         }
392
393                                 /* skip packet */
394                         int sk = len;
395                         if (sk >= 188)
396                                 sk = 188;
397                         else if (!m_pktptr) /* we dont want this packet, otherwise m_pktptr = sk (=len) > 4 */
398                                 m_pktptr = sk - 188;
399
400                         len -= sk;
401                         packet += sk;
402                 } else             /* if we don't have a complete header */
403                 {
404                         memcpy(m_pkt, packet, len);   /* complete header next time */
405                         m_pktptr = len;
406                         packet += len;
407                         len = 0;
408                 }
409         }
410 }
411
412 void eMPEGStreamParserTS::setPid(int _pid)
413 {
414         m_pktptr = 0;
415         m_pid = _pid;
416 }