a6be5c3e597e6751bc1f3b183f42f058b9c91c07
[openblackhole/openblackhole-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         m_pts_to_offset.clear();
39         pts_t last = -(1LL<<62);
40         int loaded = 0, skipped = 0;
41         while (1)
42         {
43                 unsigned long long d[2];
44                 if (fread(d, sizeof(d), 1, f) < 1)
45                         break;
46                 
47 #if BYTE_ORDER == LITTLE_ENDIAN
48                 d[0] = bswap_64(d[0]);
49                 d[1] = bswap_64(d[1]);
50 #endif
51                 if ((d[1] - last) > 90000/2)
52                 {
53                         m_access_points[d[0]] = d[1];
54                         m_pts_to_offset.insert(std::pair<pts_t,off_t>(d[1], d[0]));
55                         last = d[1];
56                         loaded++;
57                 } else
58                         skipped++;
59         }
60         eDebug("loaded %d, skipped %d", loaded, skipped);
61         fclose(f);
62         fixupDiscontinuties();
63         return 0;
64 }
65
66 bool eMPEGStreamInformation::empty()
67 {
68         return m_access_points.empty();
69 }
70
71 void eMPEGStreamInformation::fixupDiscontinuties()
72 {
73         m_timestamp_deltas.clear();
74         if (!m_access_points.size())
75                 return;
76                 
77 //      eDebug("Fixing discontinuities ...");
78
79                         /* if we have no delta at the beginning, extrapolate it */
80         if ((m_access_points.find(0) == m_access_points.end()) && (m_access_points.size() > 1))
81         {
82                 std::map<off_t,pts_t>::const_iterator second = m_access_points.begin();
83                 std::map<off_t,pts_t>::const_iterator first  = second++;
84                 if (first->first < second->first) /* i.e., not equal or broken */
85                 {
86                         off_t diff = second->first - first->first;
87                         pts_t tdiff = second->second - first->second;
88                         tdiff *= first->first;
89                         tdiff /= diff;
90                         m_timestamp_deltas[0] = first->second - tdiff;
91 //                      eDebug("first delta is %08llx", first->second - tdiff);
92                 }
93         }
94
95         if (m_timestamp_deltas.empty())
96                 m_timestamp_deltas[m_access_points.begin()->first] = m_access_points.begin()->second;
97
98         pts_t currentDelta = m_timestamp_deltas.begin()->second, lastpts_t = 0;
99         for (std::map<off_t,pts_t>::const_iterator i(m_access_points.begin()); i != m_access_points.end(); ++i)
100         {
101                 pts_t current = i->second - currentDelta;
102                 pts_t diff = current - lastpts_t;
103                 
104                 if (llabs(diff) > (90000*5)) // 5sec diff
105                 {
106 //                      eDebug("%llx < %llx, have discont. new timestamp is %llx (diff is %llx)!", current, lastpts_t, i->second, diff);
107                         currentDelta = i->second - lastpts_t; /* FIXME: should be the extrapolated new timestamp, based on the current rate */
108 //                      eDebug("current delta now %llx, making current to %llx", currentDelta, i->second - currentDelta);
109                         m_timestamp_deltas[i->first] = currentDelta;
110                 }
111                 lastpts_t = i->second - currentDelta;
112         }
113         
114         
115 //      eDebug("ok, found %d disconts.", m_timestamp_deltas.size());
116
117 #if 0   
118         for (off_t x=0x25807E34ULL; x < 0x25B3CF70; x+= 100000)
119         {
120                 off_t o = x;
121                 pts_t p;
122                 int r = getPTS(o, p);
123                 eDebug("%08llx -> %08llx | %08llx, %d, %08llx %08llx", x, getDelta(x), getInterpolated(x), r, o, p);
124         }
125 #endif
126 }
127
128 pts_t eMPEGStreamInformation::getDelta(off_t offset)
129 {
130         if (!m_timestamp_deltas.size())
131                 return 0;
132         std::map<off_t,pts_t>::iterator i = m_timestamp_deltas.upper_bound(offset);
133
134                 /* i can be the first when you query for something before the first PTS */
135         if (i != m_timestamp_deltas.begin())
136                 --i;
137
138         return i->second;
139 }
140
141 int eMPEGStreamInformation::fixupPTS(const off_t &offset, pts_t &ts)
142 {
143         if (!m_timestamp_deltas.size())
144                 return -1;
145
146         std::multimap<pts_t, off_t>::const_iterator 
147                 l = m_pts_to_offset.upper_bound(ts - 60 * 90000), 
148                 u = m_pts_to_offset.upper_bound(ts + 60 * 90000), 
149                 nearest = m_pts_to_offset.end();
150
151         while (l != u)
152         {
153                 if ((nearest == m_pts_to_offset.end()) || (llabs(l->first - ts) < llabs(nearest->first - ts)))
154                         nearest = l;
155                 ++l;
156         }
157         if (nearest == m_pts_to_offset.end())
158                 return 1;
159
160         ts -= getDelta(nearest->second);
161
162         return 0;
163 }
164
165 int eMPEGStreamInformation::getPTS(off_t &offset, pts_t &pts)
166 {
167         std::map<off_t,pts_t>::iterator before = m_access_points.lower_bound(offset);
168
169                 /* usually, we prefer the AP before the given offset. however if there is none, we take any. */
170         if (before != m_access_points.begin())
171                 --before;
172         
173         if (before == m_access_points.end())
174         {
175                 pts = 0;
176                 return -1;
177         }
178         
179         offset = before->first;
180         pts = before->second - getDelta(offset);
181         
182         return 0;
183 }
184
185 pts_t eMPEGStreamInformation::getInterpolated(off_t offset)
186 {
187                 /* get the PTS values before and after the offset. */
188         std::map<off_t,pts_t>::iterator before, after;
189         after = m_access_points.upper_bound(offset);
190         before = after;
191
192         if (before != m_access_points.begin())
193                 --before;
194         else    /* we query before the first known timestamp ... FIXME */
195                 return 0;
196
197                 /* empty... */
198         if (before == m_access_points.end())
199                 return 0;
200
201                 /* if after == end, then we need to extrapolate ... FIXME */
202         if ((before->first == offset) || (after == m_access_points.end()))
203                 return before->second - getDelta(offset);
204         
205         pts_t before_ts = before->second - getDelta(before->first);
206         pts_t after_ts = after->second - getDelta(after->first);
207         
208 //      eDebug("%08llx .. ? .. %08llx", before_ts, after_ts);
209 //      eDebug("%08llx .. %08llx .. %08llx", before->first, offset, after->first);
210         
211         pts_t diff = after_ts - before_ts;
212         off_t diff_off = after->first - before->first;
213         
214         diff = (offset - before->first) * diff / diff_off;
215 //      eDebug("%08llx .. %08llx .. %08llx", before_ts, before_ts + diff, after_ts);
216         return before_ts + diff;
217 }
218  
219 off_t eMPEGStreamInformation::getAccessPoint(pts_t ts)
220 {
221                 /* FIXME: more efficient implementation */
222         pts_t delta = 0;
223         off_t last = 0;
224         for (std::map<off_t, pts_t>::const_iterator i(m_access_points.begin()); i != m_access_points.end(); ++i)
225         {
226                 pts_t delta = getDelta(i->first);
227                 pts_t c = i->second - delta;
228                 if (c > ts)
229                         break;
230                 last = i->first;
231         }
232         return last;
233 }
234
235 int eMPEGStreamInformation::getNextAccessPoint(pts_t &ts, const pts_t &start, int direction)
236 {
237         off_t offset = getAccessPoint(start);
238         std::map<off_t, pts_t>::const_iterator i = m_access_points.find(offset);
239         if (i == m_access_points.end())
240         {
241                 eDebug("getNextAccessPoint: initial AP not found");
242                 return -1;
243         }
244         while (direction)
245         {
246                 if (direction > 0)
247                 {
248                         if (i == m_access_points.end())
249                                 return -1;
250                         ++i;
251                         direction--;
252                 }
253                 if (direction < 0)
254                 {
255                         if (i == m_access_points.begin())
256                         {
257                                 eDebug("at start");
258                                 return -1;
259                         }
260                         --i;
261                         direction++;
262                 }
263         }
264         ts = i->second - getDelta(i->first);
265         eDebug("fine, at %llx - %llx = %llx", ts, i->second, getDelta(i->first));
266         eDebug("fine, at %lld - %lld = %lld", ts, i->second, getDelta(i->first));
267         return 0;
268 }
269
270 eMPEGStreamParserTS::eMPEGStreamParserTS(eMPEGStreamInformation &streaminfo): m_streaminfo(streaminfo), m_pktptr(0), m_pid(-1), m_need_next_packet(0), m_skip(0)
271 {
272 }
273
274 int eMPEGStreamParserTS::processPacket(const unsigned char *pkt, off_t offset)
275 {
276         if (!wantPacket(pkt))
277                 eWarning("something's wrong.");
278
279         const unsigned char *end = pkt + 188;
280         
281         if (!(pkt[3] & 0x10))
282         {
283                 eWarning("[TSPARSE] PUSI set but no payload.");
284                 return 0;
285         }
286         
287         if (pkt[3] & 0x20) // adaption field present?
288                 pkt += pkt[4] + 4 + 1;  /* skip adaption field and header */
289         else
290                 pkt += 4; /* skip header */
291
292         if (pkt > end)
293         {
294                 eWarning("[TSPARSE] dropping huge adaption field");
295                 return 0;
296         }
297         
298                 // ok, we now have the start of the payload, aligned with the PES packet start.
299         if (pkt[0] || pkt[1] || (pkt[2] != 1))
300         {
301                 eWarning("broken startcode");
302                 return 0;
303         }
304         
305         
306         pts_t pts = 0;
307         int ptsvalid = 0;
308         
309         if (pkt[7] & 0x80) // PTS present?
310         {
311                 pts  = ((unsigned long long)(pkt[ 9]&0xE))  << 29;
312                 pts |= ((unsigned long long)(pkt[10]&0xFF)) << 22;
313                 pts |= ((unsigned long long)(pkt[11]&0xFE)) << 14;
314                 pts |= ((unsigned long long)(pkt[12]&0xFF)) << 7;
315                 pts |= ((unsigned long long)(pkt[13]&0xFE)) >> 1;
316                 ptsvalid = 1;
317
318 #if 0           
319                 int sec = pts / 90000;
320                 int frm = pts % 90000;
321                 int min = sec / 60;
322                 sec %= 60;
323                 int hr = min / 60;
324                 min %= 60;
325                 int d = hr / 24;
326                 hr %= 24;
327                 
328                 eDebug("pts: %016llx %d:%02d:%02d:%02d:%05d", pts, d, hr, min, sec, frm);
329 #endif
330         }
331         
332                 /* advance to payload */
333         pkt += pkt[8] + 9;
334
335                 /* sometimes, there are zeros before the startcode. */
336         while (pkt < (end-4))
337                 if (pkt[0] || pkt[1] || pkt[2])
338                         break;
339                 else
340                         pkt++;
341
342                 /* if startcode found */
343 //      eDebug("%02x %02x %02x %02x", pkt[0], pkt[1], pkt[2], pkt[3]);
344         if (!(pkt[0] || pkt[1] || (pkt[2] != 1)))
345         {
346                 if (pkt[3] == 0xb3) /* sequence header */
347                 {
348                         if (ptsvalid)
349                         {
350                                 m_streaminfo.m_access_points[offset] = pts;
351                                 eDebug("Sequence header at %llx, pts %llx", offset, pts);
352                         } else
353                                 eDebug("Sequence header but no valid PTS value.");
354                 }
355
356                 if (pkt[3] == 0x09) /* MPEG4 AVC unit access delimiter */
357                 {
358                         if (ptsvalid)
359                         {
360                                 m_streaminfo.m_access_points[offset] = pts;
361                                 eDebug("MPEG4 AVC UAD at %llx, pts %llx", offset, pts);
362                         } else
363                                 eDebug("MPEG4 AVC UAD but no valid PTS value.");
364                 }
365         }
366         return 0;
367 }
368
369 inline int eMPEGStreamParserTS::wantPacket(const unsigned char *hdr) const
370 {
371         if (hdr[0] != 0x47)
372         {
373                 eDebug("missing sync!");
374                 return 0;
375         }
376         int ppid = ((hdr[1]&0x1F) << 8) | hdr[2];
377
378         if (ppid != m_pid)
379                 return 0;
380                 
381         if (m_need_next_packet)  /* next packet (on this pid) was required? */
382                 return 1;
383         
384         if (hdr[1] & 0x40)       /* pusi set: yes. */
385                 return 1;
386
387         return 0;
388 }
389
390 void eMPEGStreamParserTS::parseData(off_t offset, const void *data, unsigned int len)
391 {
392         const unsigned char *packet = (const unsigned char*)data;
393         const unsigned char *packet_start = packet;
394         
395                         /* sorry for the redundant code here, but there are too many special cases... */
396         while (len)
397         {
398                         /* emergency resync. usually, this should not happen, because the data should 
399                            be sync-aligned.
400                            
401                            to make this code work for non-strictly-sync-aligned data, (for example, bad 
402                            files) we fix a possible resync here by skipping data until the next 0x47.
403                            
404                            if this is a false 0x47, the packet will be dropped by wantPacket, and the
405                            next time, sync will be re-established. */
406                 int skipped = 0;
407                 while (!m_pktptr && len)
408                 {
409                         if (packet[0] == 0x47)
410                                 break;
411                         len--;
412                         packet++;
413                         skipped++;
414                 }
415                 
416                 if (skipped)
417                         eDebug("SYNC LOST: skipped %d bytes.", skipped);
418                 
419                 if (!len)
420                         break;
421                 
422                 if (m_pktptr)
423                 {
424                                 /* skip last packet */
425                         if (m_pktptr < 0)
426                         {
427                                 unsigned int skiplen = -m_pktptr;
428                                 if (skiplen > len)
429                                         skiplen = len;
430                                 packet += skiplen;
431                                 len -= skiplen;
432                                 m_pktptr += skiplen;
433                                 continue;
434                         } else if (m_pktptr < 4) /* header not complete, thus we don't know if we want this packet */
435                         {
436                                 unsigned int storelen = 4 - m_pktptr;
437                                 if (storelen > len)
438                                         storelen = len;
439                                 memcpy(m_pkt + m_pktptr, packet,  storelen);
440                                 
441                                 m_pktptr += storelen;
442                                 len -= storelen;
443                                 packet += storelen;
444                                 
445                                 if (m_pktptr == 4)
446                                         if (!wantPacket(m_pkt))
447                                         {
448                                                         /* skip packet */
449                                                 packet += 184;
450                                                 len -= 184;
451                                                 m_pktptr = 0;
452                                                 continue;
453                                         }
454                         }
455                                 /* otherwise we complete up to the full packet */
456                         unsigned int storelen = 188 - m_pktptr;
457                         if (storelen > len)
458                                 storelen = len;
459                         memcpy(m_pkt + m_pktptr, packet,  storelen);
460                         m_pktptr += storelen;
461                         len -= storelen;
462                         packet += storelen;
463                         
464                         if (m_pktptr == 188)
465                         {
466                                 m_need_next_packet = processPacket(m_pkt, offset + (packet - packet_start));
467                                 m_pktptr = 0;
468                         }
469                 } else if (len >= 4)  /* if we have a full header... */
470                 {
471                         if (wantPacket(packet))  /* decide wheter we need it ... */
472                         {
473                                 if (len >= 188)          /* packet complete? */
474                                 {
475                                         m_need_next_packet = processPacket(packet, offset + (packet - packet_start)); /* process it now. */
476                                 } else
477                                 {
478                                         memcpy(m_pkt, packet, len);  /* otherwise queue it up */
479                                         m_pktptr = len;
480                                 }
481                         }
482
483                                 /* skip packet */
484                         int sk = len;
485                         if (sk >= 188)
486                                 sk = 188;
487                         else if (!m_pktptr) /* we dont want this packet, otherwise m_pktptr = sk (=len) > 4 */
488                                 m_pktptr = sk - 188;
489
490                         len -= sk;
491                         packet += sk;
492                 } else             /* if we don't have a complete header */
493                 {
494                         memcpy(m_pkt, packet, len);   /* complete header next time */
495                         m_pktptr = len;
496                         packet += len;
497                         len = 0;
498                 }
499         }
500 }
501
502 void eMPEGStreamParserTS::setPid(int _pid)
503 {
504         m_pktptr = 0;
505         m_pid = _pid;
506 }