2 Copyright 2005-2007 Mark Calderbank
4 This file is part of VOMP.
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include "demuxervdr.h"
29 #define __LITTLE_ENDIAN 1234
30 #define __BIG_ENDIAN 4321
31 #define __BYTE_ORDER __LITTLE_ENDIAN
34 #define PTS_JUMP_MARGIN 10000
35 #define PTS_ALLOWANCE 90000
37 // TODO: PTS class to handle wrapping arithmetic & comparisons?
38 static ULLONG PTSDistance(ULLONG pts1, ULLONG pts2)
40 // Assume pts1, pts2 < 2^33; calculate shortest distance between
41 ULLONG ret = (pts1 > pts2) ? pts1 - pts2 : pts2 - pts1;
42 if (ret > (1LL<<32)) ret = (1LL<<33) - ret;
46 static ULLONG PTSDifference(ULLONG pts1, ULLONG pts2)
48 // Assume pts1, pts2 < 2^33; calculate pts1 - pts2
52 return (1LL<<33) + pts1 - pts2;
55 DemuxerVDR::DemuxerVDR()
57 frameCounting = false;
58 packetCounting = false;
61 void DemuxerVDR::reset()
63 frameCounting = false;
64 packetCounting = false;
69 void DemuxerVDR::flush()
76 int DemuxerVDR::scan(UCHAR *buf, int len)
78 // Temporarily, just look for the lowest audio stream and return it
79 UCHAR HiByte = PESTYPE_AUDMAX;
84 UINT pattern = *(UINT*)buf;
87 #if __BYTE_ORDER == __BIG_ENDIAN
88 if (pattern < (UINT)(0x100 | PESTYPE_AUD0) ||
89 pattern > (UINT)(0x100 | HiByte)) continue;
90 HiByte = pattern & 0xFF;
92 if ((pattern & 0xFFFFFF) != 0x010000 ||
93 pattern < ((UINT)PESTYPE_AUD0 << 24) ||
94 pattern > ((UINT)HiByte << 24)) continue;
95 HiByte = pattern >> 24;
98 if (HiByte == PESTYPE_AUD0) break;
103 int DemuxerVDR::findPTS(UCHAR* buf, int len, ULLONG* dest)
107 UINT pattern = *(UINT*)buf;
109 #if __BYTE_ORDER == __BIG_ENDIAN
110 if (pattern < (0x100 | PESTYPE_AUD0) ||
111 pattern > (0x100 | PESTYPE_VIDMAX)) continue;
113 if ((pattern & 0xFFFFFF) != 0x010000 ||
114 pattern < ((UINT)PESTYPE_AUD0 << 24) ||
115 pattern > ((UINT)PESTYPE_VIDMAX << 24)) continue;
117 if ((buf[5] & 0xC0) != 0x80) continue;
119 UINT packetlength = ((UINT)buf[3] << 8) | buf[4];
121 if ( buf[6] & 0x80 ) // PTS_DTS_flags indicate that PTS is present
123 if ( (buf[8] & 0x01) != 0x01 ||
124 (buf[10] & 0x01) != 0x01 ||
125 (buf[12] & 0x01) != 0x01) continue;
127 *dest = ( (ULLONG)(buf[8] & 0x0E) << 29 ) |
128 ( (ULLONG)(buf[9]) << 22 ) |
129 ( (ULLONG)(buf[10] & 0xFE) << 14 ) |
130 ( (ULLONG)(buf[11]) << 7 ) |
131 ( (ULLONG)(buf[12] & 0xFE) >> 1 );
136 buf += packetlength; len -= packetlength;
142 void DemuxerVDR::setFrameNum(ULONG frame)
144 frameCounting = true;
146 Log::getInstance()->log("Demuxer", Log::DEBUG, "setFrameNum %d", frame);
149 void DemuxerVDR::setPacketNum(ULONG npacket)
151 packetCounting = true;
152 packetNumber = npacket;
153 Log::getInstance()->log("Demuxer", Log::DEBUG, "setPacketNum %d", npacket);
156 int DemuxerVDR::put(UCHAR* buf, int len)
158 int ret = 0; // return number of bytes consumed
161 if (submitPacket(packet) == 0) // Still full!
167 if (state > 0) // We are half way through a PES packet.
169 if (len >= state) // The remainder of the packet is available.
171 packet.write(buf, state);
172 buf += state; len -= state; ret += state;
174 parseVDRPacketDetails();
175 if (submitPacket(packet) == 0) // Stream is full
181 else // Write what we have, then exit.
183 packet.write(buf, len);
195 if (*buf == 0x00) state--; else state = 0;
199 if (*buf == 0x01) state--; else if (*buf != 0x00) state = 0;
203 if ((*buf >= PESTYPE_VID0 && *buf <= PESTYPE_VIDMAX) ||
204 (*buf >= PESTYPE_AUD0 && *buf <= PESTYPE_AUDMAX) ||
205 (*buf == PESTYPE_PRIVATE_1))
210 else if (*buf == 0x00)
217 packetLength = ((UINT)*buf) << 8;
222 packetLength += *buf;
228 if (state == -6) // Packet header complete
230 if (len >= packetLength) // The entire packet is available.
232 packet.write(buf, packetLength);
233 buf += packetLength; len -= packetLength; ret += packetLength;
235 parseVDRPacketDetails();
236 if (submitPacket(packet) == 0) // Stream is full
242 else // Write what we have.
244 packet.write(buf, len);
245 state = packetLength - len;
254 ULONG DemuxerVDR::getPacketNum()
259 ULONG DemuxerVDR::getFrameNumFromPTS(ULLONG pts)
261 ULLONG difference = (1LL<<33);
263 int total = 0, actual = 0;
264 pts_map_mutex.Lock();
265 PTSMap::iterator iter = pts_map.begin();
266 while (iter != pts_map.end())
269 if (PTSDifference(iter->pts, pts) < PTS_ALLOWANCE)
272 ref_frame = iter->frame;
276 ULLONG newdiff = PTSDifference(pts, iter->pts);
277 if (newdiff < difference)
279 difference = newdiff;
280 ref_frame = iter->frame;
285 if (total > 1 && actual == 1) // We are using the most recent PTS ref.
286 { // Delete the rest.
287 iter = pts_map.begin(); iter++;
288 pts_map.erase(iter, pts_map.end());
290 pts_map_mutex.Unlock();
291 if (total > 1 && actual == 1)
292 Log::getInstance()->log("Demuxer", Log::DEBUG, "DELETED REFERENCES");
294 Log::getInstance()->log("Demuxer", Log::DEBUG, "STILL USING OLD REF");
296 if (difference == (1LL<<33))
297 return 0; // We cannot make sense of the pts
299 return ref_frame + difference * Video::getInstance()->getFPS() / 90000;
302 void DemuxerVDR::parseVDRPacketDetails()
304 parsePacketDetails(packet);
306 if (packetCounting && packet.getPacketType() >= PESTYPE_AUD0 &&
307 packet.getPacketType() <= PESTYPE_AUDMAX)
312 if (frameCounting && packet.findPictureHeader() &&
313 packet.getPacketType() >= PESTYPE_VID0 &&
314 packet.getPacketType() <= PESTYPE_VIDMAX)
316 ULONG frame_num = (frameNumber)++;
317 if (packet.findSeqHeader() > 1 && packet.hasPTS())
320 pts_map_mutex.Lock();
323 me.pts = packet.getPTS();
324 me.frame = frame_num;
325 pts_map_mutex.Unlock();
326 Log::getInstance()->log("Demuxer", Log::DEBUG, "+* PTS INIT *+ %llu %u", me.pts, me.frame);
327 pts_map_mutex.Lock();
328 pts_map.push_front(me);
330 me = pts_map.front();
331 pts_map_mutex.Unlock();
333 UINT fps = Video::getInstance()->getFPS();
334 ULLONG pts_expected = me.pts + 90000*(frame_num - me.frame) / fps;
335 while (pts_expected > (1LL<<33)) pts_expected -= (1LL<<33);
337 if (PTSDistance(pts_expected, packet.getPTS()) > PTS_JUMP_MARGIN) // PTS jump!
339 Log::getInstance()->log("Demuxer", Log::DEBUG, "+* PTS JUMP *+ %llu %u", packet.getPTS(), frame_num);
340 me.pts = packet.getPTS();
341 me.frame = frame_num;
342 pts_map_mutex.Lock();
343 pts_map.push_front(me);
344 pts_map_mutex.Unlock();