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::findVideoPTS(UCHAR* buf, int len, ULLONG* dest)
102 UINT pattern = *(UINT*)buf;
104 #if __BYTE_ORDER == __BIG_ENDIAN
105 if (pattern < (0x100 | PESTYPE_VID0) ||
106 pattern > (0x100 | PESTYPE_VIDMAX)) continue;
108 if ((pattern & 0xFFFFFF) != 0x010000 ||
109 pattern < ((UINT)PESTYPE_VID0 << 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);
145 int DemuxerVDR::put(UCHAR* buf, int len)
147 int DemuxerVDR::put(UCHAR* buf, int len, ULLONG cur_pos)
150 int ret = 0; // return number of bytes consumed
153 ULLONG current_position = cur_pos;
159 if (packet.submit() == 0) // Still full!
161 if (packet.submit(current_position) == 0) // Still full!
168 if (state > 0) // We are half way through a PES packet.
170 if (len >= state) // The remainder of the packet is available.
172 packet.write(buf, state);
173 buf += state; len -= state; ret += state;
176 if (packet.submit() == 0) // Stream is full
178 if (packet.submit(current_position) == 0) // Stream is full
185 else // Write what we have, then exit.
187 packet.write(buf, len);
199 if (*buf == 0x00) state--; else state = 0;
203 if (*buf == 0x01) state--; else if (*buf != 0x00) state = 0;
207 if ((*buf >= PESTYPE_VID0 && *buf <= PESTYPE_VIDMAX) ||
208 (*buf >= PESTYPE_AUD0 && *buf <= PESTYPE_AUDMAX) ||
209 (*buf == PESTYPE_PRIVATE_1))
214 else if (*buf == 0x00)
221 packetLength = ((UINT)*buf) << 8;
226 packetLength += *buf;
236 if (state == -6) // Packet header complete
238 if (len >= packetLength) // The entire packet is available.
240 packet.write(buf, packetLength);
241 buf += packetLength; len -= packetLength; ret += packetLength;
243 current_position+=(ULLONG)packetLength;
247 if (packet.submit() == 0) // Stream is full
249 if (packet.submit(current_position) == 0) // Stream is full
256 else // Write what we have.
258 packet.write(buf, len);
259 state = packetLength - len;
268 ULONG DemuxerVDR::getFrameNumFromPTS(ULLONG pts)
270 ULLONG difference = (1LL<<33);
272 int total = 0, actual = 0;
273 pthread_mutex_lock(&pts_map_mutex);
274 PTSMap::iterator iter = pts_map.begin();
275 while (iter != pts_map.end())
278 if (PTSDifference(iter->pts, pts) < PTS_ALLOWANCE)
281 ref_frame = iter->frame;
285 ULLONG newdiff = PTSDifference(pts, iter->pts);
286 if (newdiff < difference)
288 difference = newdiff;
289 ref_frame = iter->frame;
294 if (total > 1 && actual == 1) // We are using the most recent PTS ref.
295 { // Delete the rest.
296 iter = pts_map.begin(); iter++;
297 pts_map.erase(iter, pts_map.end());
299 pthread_mutex_unlock(&pts_map_mutex);
300 if (total > 1 && actual == 1)
301 Log::getInstance()->log("Demuxer", Log::DEBUG, "DELETED REFERENCES");
303 Log::getInstance()->log("Demuxer", Log::DEBUG, "STILL USING OLD REF");
305 //ULONG ret = ref_frame + difference * Video::getInstance()->getFPS() / 90000;
306 //Log::getInstance()->log("Demuxer", Log::DEBUG, "%llu: FOUND %d from %d, diff %llu", pts, ret, ref_frame, difference);
308 if (difference == (1LL<<33))
309 return 0; // We cannot make sense of the pts
311 return ref_frame + difference * Video::getInstance()->getFPS() / 90000;
314 void DemuxerVDR::PESPacketVDR::parseDetails()
316 DemuxerVDR* dx = (DemuxerVDR*)(DemuxerVDR::getInstance());
317 PESPacket::parseDetails();
319 if (dx->frameCounting && findPictureHeader() &&
320 packetType >= PESTYPE_VID0 && packetType <= PESTYPE_VIDMAX)
322 ULONG frame_num = (dx->frameNumber)++;
323 if (seq_header && pts != PTS_INVALID)
326 pthread_mutex_lock(&(dx->pts_map_mutex));
327 if (dx->pts_map.empty())
330 me.frame = frame_num;
331 pthread_mutex_unlock(&(dx->pts_map_mutex));
332 Log::getInstance()->log("Demuxer", Log::DEBUG, "+* PTS INIT *+ %llu %u", me.pts, me.frame);
333 pthread_mutex_lock(&(dx->pts_map_mutex));
334 dx->pts_map.push_front(me);
336 me = dx->pts_map.front();
337 pthread_mutex_unlock(&(dx->pts_map_mutex));
339 UINT fps = Video::getInstance()->getFPS();
340 ULLONG pts_expected = me.pts + 90000*(frame_num - me.frame) / fps;
341 while (pts_expected > (1LL<<33)) pts_expected -= (1LL<<33);
343 if (PTSDistance(pts_expected, pts) > PTS_JUMP_MARGIN) // PTS jump!
345 Log::getInstance()->log("Demuxer", Log::DEBUG, "+* PTS JUMP *+ %llu %u", pts, frame_num);
347 me.frame = frame_num;
348 pthread_mutex_lock(&(dx->pts_map_mutex));
349 dx->pts_map.push_front(me);
350 pthread_mutex_unlock(&(dx->pts_map_mutex));