2 Copyright 2006-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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "demuxerts.h"
24 DemuxerTS::DemuxerTS(int p_vID, int p_aID)
30 void DemuxerTS::flush()
35 vPacket.init(PESTYPE_VID0);
36 aPacket.init(PESTYPE_AUD0);
41 int DemuxerTS::scan(UCHAR *buf, int len)
46 void DemuxerTS::setVID(int p_vID)
49 vPacket.init(PESTYPE_VID0);
53 void DemuxerTS::setAID(int p_aID)
56 aPacket.init(PESTYPE_AUD0);
60 int DemuxerTS::findPTS(UCHAR* buf, int len, ULLONG* dest)
62 UINT LoPattern = 0x100 | PESTYPE_VID0,
63 HiPattern = 0x100 | PESTYPE_VIDMAX;
67 UINT pattern = *(UINT*)buf;
69 if (pattern < LoPattern || pattern > HiPattern) continue;
71 UINT framelength = ((UINT)buf[3] << 8) | buf[4];
74 if ( buf[1] & 0x80 ) // PTS_DTS_flags indicate that PTS is present
76 *dest = ( (ULLONG)(buf[3] & 0x0E) << 29 ) |
77 ( (ULLONG)(buf[4]) << 22 ) |
78 ( (ULLONG)(buf[5] & 0xFE) << 14 ) |
79 ( (ULLONG)(buf[6]) << 7 ) |
80 ( (ULLONG)(buf[7] & 0xFE) >> 1 );
84 buf += framelength; len -= framelength;
90 int DemuxerTS::put(UCHAR* buf, int len)
92 int ret = 0; // return number of bytes consumed
96 if (len >= TS_SIZE + 1 - partPacket)
97 { // Remainder of partial packet is available, plus one
98 memcpy(store+partPacket, buf, TS_SIZE - partPacket);
99 ret += TS_SIZE - partPacket;
100 buf += TS_SIZE - partPacket;
101 len -= TS_SIZE - partPacket;
102 partPacket = TS_SIZE;
104 { // Packet is properly terminated
105 int rc = processTS(store);
107 partPacket = 0; // Successfully processed
109 return ret; // Try again later.
112 { // Packet not terminated. Find another candidate, and shift store
113 Log::getInstance()->log("TS Demuxer", Log::ERR, "TS Misaligned!");
115 while (search < partPacket && store[search] != TS_SIG)
117 partPacket -= search;
118 if (partPacket) memcpy(store, store+search, partPacket);
122 { // Still don't have complete packet. Consume what we do have.
123 memcpy(store+partPacket, buf, len);
130 // Position ourselves at a candidate TS packet
131 while (len > 0 && *buf != TS_SIG)
133 Log::getInstance()->log("TS Demuxer", Log::ERR, "TS Misaligned!");
139 if (len < TS_SIZE + 1)
140 { // Not enough data. Store what we have.
141 memcpy(store, buf, len);
147 if (buf[TS_SIZE] != TS_SIG)
148 { // Not terminated correctly.
150 while (len > 0 && *buf != TS_SIG)
157 int rc = processTS(buf);
159 { // Successfully processed
160 buf += TS_SIZE; ret += TS_SIZE; len -= TS_SIZE;
163 { // Processing failed.
171 int DemuxerTS::processTS(UCHAR* buf)
173 int datalen = TS_SIZE - 4;
174 int pid = ( (buf[1] & 0x1F) << 8 ) | buf[2];
175 UCHAR payload = buf[1] & 0x40;
177 if (buf[3] & 0x20) // Adaptation field is present
178 datalen -= (buf[4] + 1);
179 if (datalen < 0) // Error in stream TODO log this
181 if (datalen == 0) // Null packet
183 buf += (TS_SIZE - datalen);
195 parsePacketDetails(vPacket);
198 rc = submitPacket(vPacket);
208 parsePacketDetails(aPacket);
211 rc = submitPacket(aPacket);
215 if (rc == 0) return 0;
220 vPacket.init(PESTYPE_VID0);
221 buf += 6; datalen -= 6;
225 aPacket.init(PESTYPE_AUD0);
226 buf += 6; datalen -= 6;
230 if ( (pid == vID && vActive) ||
231 (pid == aID && aActive) )
233 PESPacket* packet = NULL;
234 if (pid == vID) packet = &vPacket;
235 if (pid == aID) packet = &aPacket;
238 if (packet->write(buf, datalen) == 0)
239 { // Writing to packet failed. It has overflowed.
242 parsePacketDetails(*packet);
245 if (submitPacket(*packet) == 0) return 0;
248 packet->write((UCHAR*)"\200\000\000", 3);
249 packet->write(buf, datalen);