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