]> git.vomp.tv Git - vompclient.git/blob - demuxervdr.cc
Fix for possible crash when removing bar
[vompclient.git] / demuxervdr.cc
1 /*
2     Copyright 2005-2006 Mark Calderbank
3
4     This file is part of VOMP.
5
6     VOMP is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     VOMP is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with VOMP; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #include "demuxervdr.h"
22 #include "video.h"
23 #ifndef WIN32
24 #include <endian.h>
25 #else
26 #define __LITTLE_ENDIAN 1234
27 #define __BIG_ENDIAN  4321
28 #define __BYTE_ORDER __LITTLE_ENDIAN
29 #endif
30
31 #define PTS_JUMP_MARGIN   10000
32 #define PTS_ALLOWANCE 90000
33
34 // TODO: PTS class to handle wrapping arithmetic & comparisons?
35 static ULLONG PTSDistance(ULLONG pts1, ULLONG pts2)
36 {
37   // Assume pts1, pts2 < 2^33; calculate shortest distance between
38   ULLONG ret = (pts1 > pts2) ? pts1 - pts2 : pts2 - pts1;
39   if (ret > (1LL<<32)) ret = (1LL<<33) - ret;
40   return ret;
41 }
42
43 static ULLONG PTSDifference(ULLONG pts1, ULLONG pts2)
44 {
45   // Assume pts1, pts2 < 2^33; calculate pts1 - pts2
46   if (pts1 > pts2)
47     return pts1 - pts2;
48   else
49     return (1LL<<33) + pts1 - pts2;
50 }
51
52 DemuxerVDR::DemuxerVDR()
53 {
54   frameCounting = false;
55   packetCounting = false;
56 }
57
58 void DemuxerVDR::reset()
59 {
60   frameCounting = false;
61   packetCounting = false;
62   pts_map.clear();
63   Demuxer::reset();
64 }
65
66 void DemuxerVDR::flush()
67 {
68   state = 0;
69   submitting = false;
70   Demuxer::flush();
71 }
72
73 int DemuxerVDR::scan(UCHAR *buf, int len)
74 {
75   // Temporarily, just look for the lowest audio stream and return it
76   UCHAR HiByte = PESTYPE_AUDMAX;
77   int ret = 0;
78
79   while (len >= 4)
80   {
81     UINT pattern = *(UINT*)buf;
82     buf++; len--;
83
84 #if __BYTE_ORDER == __BIG_ENDIAN
85     if (pattern < (UINT)(0x100 | PESTYPE_AUD0) ||
86         pattern > (UINT)(0x100 | HiByte)) continue;
87     HiByte = pattern & 0xFF;
88 #else
89     if ((pattern & 0xFFFFFF) != 0x010000 ||
90          pattern < ((UINT)PESTYPE_AUD0 << 24) ||
91          pattern > ((UINT)HiByte << 24)) continue;
92     HiByte = pattern >> 24;
93 #endif
94     ret = HiByte;
95     if (HiByte == PESTYPE_AUD0) break;
96   }
97   return ret;
98 }
99
100 int DemuxerVDR::findPTS(UCHAR* buf, int len, ULLONG* dest)
101 {
102   while (len >= 14)
103   {
104     UINT pattern = *(UINT*)buf;
105     buf++; len--;
106 #if __BYTE_ORDER == __BIG_ENDIAN
107     if (pattern < (0x100 | PESTYPE_AUD0) ||
108         pattern > (0x100 | PESTYPE_VIDMAX)) continue;
109 #else
110     if ((pattern & 0xFFFFFF) != 0x010000 ||
111          pattern < ((UINT)PESTYPE_AUD0 << 24) ||
112          pattern > ((UINT)PESTYPE_VIDMAX << 24)) continue;
113 #endif
114     if ((buf[5] & 0xC0) != 0x80) continue;
115
116     UINT packetlength = ((UINT)buf[3] << 8) | buf[4];
117
118     if ( buf[6] & 0x80 ) // PTS_DTS_flags indicate that PTS is present
119     {
120       if ( (buf[8]  & 0x01) != 0x01 ||
121            (buf[10] & 0x01) != 0x01 ||
122            (buf[12] & 0x01) != 0x01) continue;
123
124       *dest = ( (ULLONG)(buf[8]  & 0x0E) << 29 ) |
125               ( (ULLONG)(buf[9])         << 22 ) |
126               ( (ULLONG)(buf[10] & 0xFE) << 14 ) |
127               ( (ULLONG)(buf[11])        <<  7 ) |
128               ( (ULLONG)(buf[12] & 0xFE) >>  1 );
129       return 1;
130     }
131
132     buf += 5; len -= 5;
133     buf += packetlength; len -= packetlength;
134   }
135   // No PTS found.
136   return 0;
137 }
138
139 void DemuxerVDR::setFrameNum(ULONG frame)
140 {
141   frameCounting = true;
142   frameNumber = frame;
143   Log::getInstance()->log("Demuxer", Log::DEBUG, "setFrameNum %d", frame);
144 }
145
146 void DemuxerVDR::setPacketNum(ULONG npacket)
147 {
148   packetCounting = true;
149   packetNumber = npacket;
150   Log::getInstance()->log("Demuxer", Log::DEBUG, "setPacketNum %d", npacket);
151 }
152
153 int DemuxerVDR::put(UCHAR* buf, int len)
154 {
155   int ret = 0;    // return number of bytes consumed
156   if (submitting)
157   {
158     if (packet.submit() == 0) // Still full!
159       return ret;
160     else
161       submitting = false;
162   }
163
164   if (state > 0) // We are half way through a PES packet.
165   {
166     if (len >= state) // The remainder of the packet is available.
167     {
168       packet.write(buf, state);
169       buf += state; len -= state; ret += state;
170       state = 0;
171       if (packet.submit() == 0) // Stream is full
172       {
173         submitting = true;
174         return ret;
175       }
176     }
177     else // Write what we have, then exit.
178     {
179       packet.write(buf, len);
180       state -= len;
181       return len;
182     }
183   }
184
185   while (len > 0)
186   {
187     switch (state)
188     {
189       case  0:
190       case -1:
191         if (*buf == 0x00) state--; else state = 0;
192         buf++; len--; ret++;
193         break;
194       case -2:
195         if (*buf == 0x01) state--; else if (*buf != 0x00) state = 0;
196         buf++; len--; ret++;
197         break;
198       case -3:
199         if ((*buf >= PESTYPE_VID0 && *buf <= PESTYPE_VIDMAX) ||
200             (*buf >= PESTYPE_AUD0 && *buf <= PESTYPE_AUDMAX) ||
201             (*buf == PESTYPE_PRIVATE_1))
202         {
203           packet.init(*buf);
204           state--;
205         }
206         else if (*buf == 0x00)
207           state = -1;
208         else
209           state = 0;
210         buf++; len--; ret++;
211         break;
212       case -4:
213         packetLength = ((UINT)*buf) << 8;
214         state--;
215         buf++; len--; ret++;
216         break;
217       case -5:
218         packetLength += *buf;
219         state--;
220         buf++; len--; ret++;
221         break;
222     }
223
224     if (state == -6) // Packet header complete
225     {
226       if (len >= packetLength) // The entire packet is available.
227       {
228         packet.write(buf, packetLength);
229         buf += packetLength; len -= packetLength; ret += packetLength;
230         state = 0;
231         if (packet.submit() == 0) // Stream is full
232         {
233           submitting = true;
234           return ret;
235         }
236       }
237       else // Write what we have.
238       {
239         packet.write(buf, len);
240         state = packetLength - len;
241         ret += len;
242         len = 0;
243       }
244     }
245   }
246   return ret;
247 }
248
249 ULONG DemuxerVDR::getPacketNum()
250 {
251   return packetNumber;
252 }
253
254 ULONG DemuxerVDR::getFrameNumFromPTS(ULLONG pts)
255 {
256   ULLONG difference = (1LL<<33);
257   ULONG ref_frame = 0;
258   int total = 0, actual = 0;
259   pts_map_mutex.Lock();
260   PTSMap::iterator iter = pts_map.begin();
261   while (iter != pts_map.end())
262   {
263     ++total;
264     if (PTSDifference(iter->pts, pts) < PTS_ALLOWANCE)
265     {
266       difference = 0;
267       ref_frame = iter->frame;
268       actual = total;
269       break;
270     }
271     ULLONG newdiff = PTSDifference(pts, iter->pts);
272     if (newdiff < difference)
273     {
274       difference = newdiff;
275       ref_frame = iter->frame;
276       actual = total;
277     }
278     ++iter;
279   }
280   if (total > 1 && actual == 1) // We are using the most recent PTS ref.
281   {                             // Delete the rest.
282     iter = pts_map.begin(); iter++;
283     pts_map.erase(iter, pts_map.end());
284   }
285   pts_map_mutex.Unlock();
286   if (total > 1 && actual == 1)
287 Log::getInstance()->log("Demuxer", Log::DEBUG, "DELETED REFERENCES");
288   if (actual > 1)
289 Log::getInstance()->log("Demuxer", Log::DEBUG, "STILL USING OLD REF");
290
291   if (difference == (1LL<<33))
292     return 0; // We cannot make sense of the pts
293   else
294     return ref_frame + difference * Video::getInstance()->getFPS() / 90000;
295 }
296
297 void DemuxerVDR::PESPacketVDR::parseDetails()
298 {
299   DemuxerVDR* dx = (DemuxerVDR*)(DemuxerVDR::getInstance());
300   PESPacket::parseDetails();
301
302   if (dx->packetCounting &&
303       packetType >= PESTYPE_AUD0 && packetType <= PESTYPE_AUDMAX)
304   {
305     dx->packetNumber++;
306   }
307
308   if (dx->frameCounting && findPictureHeader() &&
309       packetType >= PESTYPE_VID0 && packetType <= PESTYPE_VIDMAX)
310   {
311     ULONG frame_num = (dx->frameNumber)++;
312     if (seq_header && pts != PTS_INVALID)
313     {
314       PTSMapEntry me;
315       dx->pts_map_mutex.Lock();
316       if (dx->pts_map.empty())
317       {
318         me.pts = pts;
319         me.frame = frame_num;
320         dx->pts_map_mutex.Unlock();
321 Log::getInstance()->log("Demuxer", Log::DEBUG, "+* PTS INIT *+ %llu %u", me.pts, me.frame);
322         dx->pts_map_mutex.Lock();
323         dx->pts_map.push_front(me);
324       }
325       me = dx->pts_map.front();
326       dx->pts_map_mutex.Unlock();
327
328       UINT fps = Video::getInstance()->getFPS();
329       ULLONG pts_expected = me.pts + 90000*(frame_num - me.frame) / fps;
330       while (pts_expected > (1LL<<33)) pts_expected -= (1LL<<33);
331
332       if (PTSDistance(pts_expected, pts) > PTS_JUMP_MARGIN) // PTS jump!
333       {
334 Log::getInstance()->log("Demuxer", Log::DEBUG, "+* PTS JUMP *+ %llu %u", pts, frame_num);
335         me.pts = pts;
336         me.frame = frame_num;
337         dx->pts_map_mutex.Lock();
338         dx->pts_map.push_front(me);
339         dx->pts_map_mutex.Unlock();
340       }
341     }
342   }
343 }