2 Copyright 2005-2006 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "demuxervdr.h"
26 #define __LITTLE_ENDIAN 1234
27 #define __BIG_ENDIAN 4321
28 #define __BYTE_ORDER __LITTLE_ENDIAN
31 #define PTS_JUMP_MARGIN 10000
32 #define PTS_ALLOWANCE 90000
34 // TODO: PTS class to handle wrapping arithmetic & comparisons?
35 static ULLONG PTSDistance(ULLONG pts1, ULLONG pts2)
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;
43 static ULLONG PTSDifference(ULLONG pts1, ULLONG pts2)
45 // Assume pts1, pts2 < 2^33; calculate pts1 - pts2
49 return (1LL<<33) + pts1 - pts2;
52 DemuxerVDR::DemuxerVDR()
54 frameCounting = false;
55 packetCounting = false;
58 void DemuxerVDR::reset()
60 frameCounting = false;
61 packetCounting = false;
66 void DemuxerVDR::flush()
73 int DemuxerVDR::scan(UCHAR *buf, int len)
75 // Temporarily, just look for the lowest audio stream and return it
76 UCHAR HiByte = PESTYPE_AUDMAX;
81 UINT pattern = *(UINT*)buf;
84 #if __BYTE_ORDER == __BIG_ENDIAN
85 if (pattern < (UINT)(0x100 | PESTYPE_AUD0) ||
86 pattern > (UINT)(0x100 | HiByte)) continue;
87 HiByte = pattern & 0xFF;
89 if ((pattern & 0xFFFFFF) != 0x010000 ||
90 pattern < ((UINT)PESTYPE_AUD0 << 24) ||
91 pattern > ((UINT)HiByte << 24)) continue;
92 HiByte = pattern >> 24;
95 if (HiByte == PESTYPE_AUD0) break;
100 int DemuxerVDR::findPTS(UCHAR* buf, int len, ULLONG* dest)
104 UINT pattern = *(UINT*)buf;
106 #if __BYTE_ORDER == __BIG_ENDIAN
107 if (pattern < (0x100 | PESTYPE_AUD0) ||
108 pattern > (0x100 | PESTYPE_VIDMAX)) continue;
110 if ((pattern & 0xFFFFFF) != 0x010000 ||
111 pattern < ((UINT)PESTYPE_AUD0 << 24) ||
112 pattern > ((UINT)PESTYPE_VIDMAX << 24)) continue;
114 if ((buf[5] & 0xC0) != 0x80) continue;
116 UINT packetlength = ((UINT)buf[3] << 8) | buf[4];
118 if ( buf[6] & 0x80 ) // PTS_DTS_flags indicate that PTS is present
120 if ( (buf[8] & 0x01) != 0x01 ||
121 (buf[10] & 0x01) != 0x01 ||
122 (buf[12] & 0x01) != 0x01) continue;
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 );
133 buf += packetlength; len -= packetlength;
139 void DemuxerVDR::setFrameNum(ULONG frame)
141 frameCounting = true;
143 Log::getInstance()->log("Demuxer", Log::DEBUG, "setFrameNum %d", frame);
146 void DemuxerVDR::setPacketNum(ULONG npacket)
148 packetCounting = true;
149 packetNumber = npacket;
150 Log::getInstance()->log("Demuxer", Log::DEBUG, "setPacketNum %d", npacket);
153 int DemuxerVDR::put(UCHAR* buf, int len)
155 int ret = 0; // return number of bytes consumed
158 if (packet.submit() == 0) // Still full!
164 if (state > 0) // We are half way through a PES packet.
166 if (len >= state) // The remainder of the packet is available.
168 packet.write(buf, state);
169 buf += state; len -= state; ret += state;
171 if (packet.submit() == 0) // Stream is full
177 else // Write what we have, then exit.
179 packet.write(buf, len);
191 if (*buf == 0x00) state--; else state = 0;
195 if (*buf == 0x01) state--; else if (*buf != 0x00) state = 0;
199 if ((*buf >= PESTYPE_VID0 && *buf <= PESTYPE_VIDMAX) ||
200 (*buf >= PESTYPE_AUD0 && *buf <= PESTYPE_AUDMAX) ||
201 (*buf == PESTYPE_PRIVATE_1))
206 else if (*buf == 0x00)
213 packetLength = ((UINT)*buf) << 8;
218 packetLength += *buf;
224 if (state == -6) // Packet header complete
226 if (len >= packetLength) // The entire packet is available.
228 packet.write(buf, packetLength);
229 buf += packetLength; len -= packetLength; ret += packetLength;
231 if (packet.submit() == 0) // Stream is full
237 else // Write what we have.
239 packet.write(buf, len);
240 state = packetLength - len;
249 ULONG DemuxerVDR::getPacketNum()
254 ULONG DemuxerVDR::getFrameNumFromPTS(ULLONG pts)
256 ULLONG difference = (1LL<<33);
258 int total = 0, actual = 0;
259 pts_map_mutex.Lock();
260 PTSMap::iterator iter = pts_map.begin();
261 while (iter != pts_map.end())
264 if (PTSDifference(iter->pts, pts) < PTS_ALLOWANCE)
267 ref_frame = iter->frame;
271 ULLONG newdiff = PTSDifference(pts, iter->pts);
272 if (newdiff < difference)
274 difference = newdiff;
275 ref_frame = iter->frame;
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());
285 pts_map_mutex.Unlock();
286 if (total > 1 && actual == 1)
287 Log::getInstance()->log("Demuxer", Log::DEBUG, "DELETED REFERENCES");
289 Log::getInstance()->log("Demuxer", Log::DEBUG, "STILL USING OLD REF");
291 //ULONG ret = ref_frame + difference * Video::getInstance()->getFPS() / 90000;
292 //Log::getInstance()->log("Demuxer", Log::DEBUG, "%llu: FOUND %d from %d, diff %llu", pts, ret, ref_frame, difference);
294 if (difference == (1LL<<33))
295 return 0; // We cannot make sense of the pts
297 return ref_frame + difference * Video::getInstance()->getFPS() / 90000;
300 void DemuxerVDR::PESPacketVDR::parseDetails()
302 DemuxerVDR* dx = (DemuxerVDR*)(DemuxerVDR::getInstance());
303 PESPacket::parseDetails();
305 if (dx->packetCounting &&
306 packetType >= PESTYPE_AUD0 && packetType <= PESTYPE_AUDMAX)
311 if (dx->frameCounting && findPictureHeader() &&
312 packetType >= PESTYPE_VID0 && packetType <= PESTYPE_VIDMAX)
314 ULONG frame_num = (dx->frameNumber)++;
315 if (seq_header && pts != PTS_INVALID)
318 dx->pts_map_mutex.Lock();
319 if (dx->pts_map.empty())
322 me.frame = frame_num;
323 dx->pts_map_mutex.Unlock();
324 Log::getInstance()->log("Demuxer", Log::DEBUG, "+* PTS INIT *+ %llu %u", me.pts, me.frame);
325 dx->pts_map_mutex.Lock();
326 dx->pts_map.push_front(me);
328 me = dx->pts_map.front();
329 dx->pts_map_mutex.Unlock();
331 UINT fps = Video::getInstance()->getFPS();
332 ULLONG pts_expected = me.pts + 90000*(frame_num - me.frame) / fps;
333 while (pts_expected > (1LL<<33)) pts_expected -= (1LL<<33);
335 if (PTSDistance(pts_expected, pts) > PTS_JUMP_MARGIN) // PTS jump!
337 Log::getInstance()->log("Demuxer", Log::DEBUG, "+* PTS JUMP *+ %llu %u", pts, frame_num);
339 me.frame = frame_num;
340 dx->pts_map_mutex.Lock();
341 dx->pts_map.push_front(me);
342 dx->pts_map_mutex.Unlock();