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;
57 void DemuxerVDR::reset()
59 frameCounting = false;
64 void DemuxerVDR::flush()
71 int DemuxerVDR::scan(UCHAR *buf, int len)
73 // Temporarily, just look for the lowest audio stream and return it
74 UCHAR HiByte = PESTYPE_AUDMAX;
79 UINT pattern = *(UINT*)buf;
82 #if __BYTE_ORDER == __BIG_ENDIAN
83 if (pattern < (UINT)(0x100 | PESTYPE_AUD0) ||
84 pattern > (UINT)(0x100 | HiByte)) continue;
85 HiByte = pattern & 0xFF;
87 if ((pattern & 0xFFFFFF) != 0x010000 ||
88 pattern < ((UINT)PESTYPE_AUD0 << 24) ||
89 pattern > ((UINT)HiByte << 24)) continue;
90 HiByte = pattern >> 24;
93 if (HiByte == PESTYPE_AUD0) break;
98 int DemuxerVDR::findPTS(UCHAR* buf, int len, ULLONG* dest)
102 UINT pattern = *(UINT*)buf;
104 #if __BYTE_ORDER == __BIG_ENDIAN
105 if (pattern < (0x100 | PESTYPE_AUD0) ||
106 pattern > (0x100 | PESTYPE_VIDMAX)) continue;
108 if ((pattern & 0xFFFFFF) != 0x010000 ||
109 pattern < ((UINT)PESTYPE_AUD0 << 24) ||
110 pattern > ((UINT)PESTYPE_VIDMAX << 24)) continue;
112 if ((buf[5] & 0xC0) != 0x80) continue;
114 UINT packetlength = ((UINT)buf[3] << 8) | buf[4];
116 if ( buf[6] & 0x80 ) // PTS_DTS_flags indicate that PTS is present
118 if ( (buf[8] & 0x01) != 0x01 ||
119 (buf[10] & 0x01) != 0x01 ||
120 (buf[12] & 0x01) != 0x01) continue;
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 );
131 buf += packetlength; len -= packetlength;
137 void DemuxerVDR::setFrameNum(ULONG frame)
139 frameCounting = true;
141 Log::getInstance()->log("Demuxer", Log::DEBUG, "setFrameNum %d", frame);
144 int DemuxerVDR::put(UCHAR* buf, int len)
146 int ret = 0; // return number of bytes consumed
149 if (packet.submit() == 0) // Still full!
155 if (state > 0) // We are half way through a PES packet.
157 if (len >= state) // The remainder of the packet is available.
159 packet.write(buf, state);
160 buf += state; len -= state; ret += state;
162 if (packet.submit() == 0) // Stream is full
168 else // Write what we have, then exit.
170 packet.write(buf, len);
182 if (*buf == 0x00) state--; else state = 0;
186 if (*buf == 0x01) state--; else if (*buf != 0x00) state = 0;
190 if ((*buf >= PESTYPE_VID0 && *buf <= PESTYPE_VIDMAX) ||
191 (*buf >= PESTYPE_AUD0 && *buf <= PESTYPE_AUDMAX) ||
192 (*buf == PESTYPE_PRIVATE_1))
197 else if (*buf == 0x00)
204 packetLength = ((UINT)*buf) << 8;
209 packetLength += *buf;
215 if (state == -6) // Packet header complete
217 if (len >= packetLength) // The entire packet is available.
219 packet.write(buf, packetLength);
220 buf += packetLength; len -= packetLength; ret += packetLength;
222 if (packet.submit() == 0) // Stream is full
228 else // Write what we have.
230 packet.write(buf, len);
231 state = packetLength - len;
240 ULONG DemuxerVDR::getFrameNumFromPTS(ULLONG pts)
242 ULLONG difference = (1LL<<33);
244 int total = 0, actual = 0;
245 pts_map_mutex.Lock();
246 PTSMap::iterator iter = pts_map.begin();
247 while (iter != pts_map.end())
250 if (PTSDifference(iter->pts, pts) < PTS_ALLOWANCE)
253 ref_frame = iter->frame;
257 ULLONG newdiff = PTSDifference(pts, iter->pts);
258 if (newdiff < difference)
260 difference = newdiff;
261 ref_frame = iter->frame;
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());
271 pts_map_mutex.Unlock();
272 if (total > 1 && actual == 1)
273 Log::getInstance()->log("Demuxer", Log::DEBUG, "DELETED REFERENCES");
275 Log::getInstance()->log("Demuxer", Log::DEBUG, "STILL USING OLD REF");
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);
280 if (difference == (1LL<<33))
281 return 0; // We cannot make sense of the pts
283 return ref_frame + difference * Video::getInstance()->getFPS() / 90000;
286 void DemuxerVDR::PESPacketVDR::parseDetails()
288 DemuxerVDR* dx = (DemuxerVDR*)(DemuxerVDR::getInstance());
289 PESPacket::parseDetails();
291 if (dx->frameCounting && findPictureHeader() &&
292 packetType >= PESTYPE_VID0 && packetType <= PESTYPE_VIDMAX)
294 ULONG frame_num = (dx->frameNumber)++;
295 if (seq_header && pts != PTS_INVALID)
298 dx->pts_map_mutex.Lock();
299 if (dx->pts_map.empty())
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);
308 me = dx->pts_map.front();
309 dx->pts_map_mutex.Unlock();
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);
315 if (PTSDistance(pts_expected, pts) > PTS_JUMP_MARGIN) // PTS jump!
317 Log::getInstance()->log("Demuxer", Log::DEBUG, "+* PTS JUMP *+ %llu %u", pts, frame_num);
319 me.frame = frame_num;
320 dx->pts_map_mutex.Lock();
321 dx->pts_map.push_front(me);
322 dx->pts_map_mutex.Unlock();