]> git.vomp.tv Git - vompclient.git/blob - demuxervdr.cc
*** empty log message ***
[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 }
56
57 void DemuxerVDR::reset()
58 {
59   frameCounting = false;
60   pts_map.clear();
61   Demuxer::reset();
62 }
63
64 void DemuxerVDR::flush()
65 {
66   state = 0;
67   submitting = false;
68   Demuxer::flush();
69 }
70
71 int DemuxerVDR::scan(UCHAR *buf, int len)
72 {
73   // Temporarily, just look for the lowest audio stream and return it
74   UCHAR HiByte = PESTYPE_AUDMAX;
75   int ret = 0;
76
77   while (len >= 4)
78   {
79     UINT pattern = *(UINT*)buf;
80     buf++; len--;
81
82 #if __BYTE_ORDER == __BIG_ENDIAN
83     if (pattern < (UINT)(0x100 | PESTYPE_AUD0) ||
84         pattern > (UINT)(0x100 | HiByte)) continue;
85     HiByte = pattern & 0xFF;
86 #else
87     if ((pattern & 0xFFFFFF) != 0x010000 ||
88          pattern < ((UINT)PESTYPE_AUD0 << 24) ||
89          pattern > ((UINT)HiByte << 24)) continue;
90     HiByte = pattern >> 24;
91 #endif
92     ret = HiByte;
93     if (HiByte == PESTYPE_AUD0) break;
94   }
95   return ret;
96 }
97
98 int DemuxerVDR::findVideoPTS(UCHAR* buf, int len, ULLONG* dest)
99 {
100   while (len >= 14)
101   {
102     UINT pattern = *(UINT*)buf;
103     buf++; len--;
104 #if __BYTE_ORDER == __BIG_ENDIAN
105     if (pattern < (0x100 | PESTYPE_VID0) ||
106         pattern > (0x100 | PESTYPE_VIDMAX)) continue;
107 #else
108     if ((pattern & 0xFFFFFF) != 0x010000 ||
109          pattern < ((UINT)PESTYPE_VID0 << 24) ||
110          pattern > ((UINT)PESTYPE_VIDMAX << 24)) continue;
111 #endif
112     if ((buf[5] & 0xC0) != 0x80) continue;
113
114     UINT packetlength = ((UINT)buf[3] << 8) | buf[4];
115
116     if ( buf[6] & 0x80 ) // PTS_DTS_flags indicate that PTS is present
117     {
118       if ( (buf[8]  & 0x01) != 0x01 ||
119            (buf[10] & 0x01) != 0x01 ||
120            (buf[12] & 0x01) != 0x01) continue;
121
122       *dest = ( (ULLONG)(buf[8]  & 0x0E) << 29 ) |
123               ( (ULLONG)(buf[9])         << 22 ) |
124               ( (ULLONG)(buf[10] & 0xFE) << 14 ) |
125               ( (ULLONG)(buf[11])        <<  7 ) |
126               ( (ULLONG)(buf[12] & 0xFE) >>  1 );
127       return 1;
128     }
129
130     buf += 5; len -= 5;
131     buf += packetlength; len -= packetlength;
132   }
133   // No PTS found.
134   return 0;
135 }
136
137 void DemuxerVDR::setFrameNum(ULONG frame)
138 {
139   frameCounting = true;
140   frameNumber = frame;
141   Log::getInstance()->log("Demuxer", Log::DEBUG, "setFrameNum %d", frame);
142 }
143
144 int DemuxerVDR::put(UCHAR* buf, int len)
145 {
146   int ret = 0;    // return number of bytes consumed
147   if (submitting)
148   {
149     if (packet.submit() == 0) // Still full!
150       return ret;
151     else
152       submitting = false;
153   }
154
155   if (state > 0) // We are half way through a PES packet.
156   {
157     if (len >= state) // The remainder of the packet is available.
158     {
159       packet.write(buf, state);
160       buf += state; len -= state; ret += state;
161       state = 0;
162       if (packet.submit() == 0) // Stream is full
163       {
164         submitting = true;
165         return ret;
166       }
167     }
168     else // Write what we have, then exit.
169     {
170       packet.write(buf, len);
171       state -= len;
172       return len;
173     }
174   }
175
176   while (len > 0)
177   {
178     switch (state)
179     {
180       case  0:
181       case -1:
182         if (*buf == 0x00) state--; else state = 0;
183         buf++; len--; ret++;
184         break;
185       case -2:
186         if (*buf == 0x01) state--; else if (*buf != 0x00) state = 0;
187         buf++; len--; ret++;
188         break;
189       case -3:
190         if ((*buf >= PESTYPE_VID0 && *buf <= PESTYPE_VIDMAX) ||
191             (*buf >= PESTYPE_AUD0 && *buf <= PESTYPE_AUDMAX) ||
192             (*buf == PESTYPE_PRIVATE_1))
193         {
194           packet.init(*buf);
195           state--;
196         }
197         else if (*buf == 0x00)
198           state = -1;
199         else
200           state = 0;
201         buf++; len--; ret++;
202         break;
203       case -4:
204         packetLength = ((UINT)*buf) << 8;
205         state--;
206         buf++; len--; ret++;
207         break;
208       case -5:
209         packetLength += *buf;
210         state--;
211         buf++; len--; ret++;
212         break;
213     }
214
215     if (state == -6) // Packet header complete
216     {
217       if (len >= packetLength) // The entire packet is available.
218       {
219         packet.write(buf, packetLength);
220         buf += packetLength; len -= packetLength; ret += packetLength;
221         state = 0;
222         if (packet.submit() == 0) // Stream is full
223         {
224           submitting = true;
225           return ret;
226         }
227       }
228       else // Write what we have.
229       {
230         packet.write(buf, len);
231         state = packetLength - len;
232         ret += len;
233         len = 0;
234       }
235     }
236   }
237   return ret;
238 }
239
240 ULONG DemuxerVDR::getFrameNumFromPTS(ULLONG pts)
241 {
242   ULLONG difference = (1LL<<33);
243   ULONG ref_frame = 0;
244   int total = 0, actual = 0;
245   pts_map_mutex.Lock();
246   PTSMap::iterator iter = pts_map.begin();
247   while (iter != pts_map.end())
248   {
249     ++total;
250     if (PTSDifference(iter->pts, pts) < PTS_ALLOWANCE)
251     {
252       difference = 0;
253       ref_frame = iter->frame;
254       actual = total;
255       break;
256     }
257     ULLONG newdiff = PTSDifference(pts, iter->pts);
258     if (newdiff < difference)
259     {
260       difference = newdiff;
261       ref_frame = iter->frame;
262       actual = total;
263     }
264     ++iter;
265   }
266   if (total > 1 && actual == 1) // We are using the most recent PTS ref.
267   {                             // Delete the rest.
268     iter = pts_map.begin(); iter++;
269     pts_map.erase(iter, pts_map.end());
270   }
271   pts_map_mutex.Unlock();
272   if (total > 1 && actual == 1)
273 Log::getInstance()->log("Demuxer", Log::DEBUG, "DELETED REFERENCES");
274   if (actual > 1)
275 Log::getInstance()->log("Demuxer", Log::DEBUG, "STILL USING OLD REF");
276
277 //ULONG ret = ref_frame + difference * Video::getInstance()->getFPS() / 90000;
278 //Log::getInstance()->log("Demuxer", Log::DEBUG, "%llu: FOUND %d from %d, diff %llu", pts, ret, ref_frame, difference);
279
280   if (difference == (1LL<<33))
281     return 0; // We cannot make sense of the pts
282   else
283     return ref_frame + difference * Video::getInstance()->getFPS() / 90000;
284 }
285
286 void DemuxerVDR::PESPacketVDR::parseDetails()
287 {
288   DemuxerVDR* dx = (DemuxerVDR*)(DemuxerVDR::getInstance());
289   PESPacket::parseDetails();
290
291   if (dx->frameCounting && findPictureHeader() &&
292       packetType >= PESTYPE_VID0 && packetType <= PESTYPE_VIDMAX)
293   {
294     ULONG frame_num = (dx->frameNumber)++;
295     if (seq_header && pts != PTS_INVALID)
296     {
297       PTSMapEntry me;
298       dx->pts_map_mutex.Lock();
299       if (dx->pts_map.empty())
300       {
301         me.pts = pts;
302         me.frame = frame_num;
303         dx->pts_map_mutex.Unlock();
304 Log::getInstance()->log("Demuxer", Log::DEBUG, "+* PTS INIT *+ %llu %u", me.pts, me.frame);
305         dx->pts_map_mutex.Lock();
306         dx->pts_map.push_front(me);
307       }
308       me = dx->pts_map.front();
309       dx->pts_map_mutex.Unlock();
310
311       UINT fps = Video::getInstance()->getFPS();
312       ULLONG pts_expected = me.pts + 90000*(frame_num - me.frame) / fps;
313       while (pts_expected > (1LL<<33)) pts_expected -= (1LL<<33);
314
315       if (PTSDistance(pts_expected, pts) > PTS_JUMP_MARGIN) // PTS jump!
316       {
317 Log::getInstance()->log("Demuxer", Log::DEBUG, "+* PTS JUMP *+ %llu %u", pts, frame_num);
318         me.pts = pts;
319         me.frame = frame_num;
320         dx->pts_map_mutex.Lock();
321         dx->pts_map.push_front(me);
322         dx->pts_map_mutex.Unlock();
323       }
324     }
325   }
326 }