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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include "demuxerts.h"
24 DemuxerTS::DemuxerTS(int p_vID, int p_aID)
31 void DemuxerTS::flush()
36 vPacket.init(PESTYPE_VID0);
39 aPacket.init(PESTYPE_PRIVATE_1,PESTYPE_SUBSTREAM_AC30);break;
42 aPacket.init(PESTYPE_AUD0);break;
48 int DemuxerTS::scan(UCHAR *buf, int len)
52 return PESTYPE_PRIVATE_1;
60 void DemuxerTS::setVID(int p_vID)
63 vPacket.init(PESTYPE_VID0);
67 void DemuxerTS::setAID(int p_aID, int type)
73 aPacket.init(PESTYPE_PRIVATE_1,PESTYPE_SUBSTREAM_AC30);
74 setAudioStream(PESTYPE_SUBSTREAM_AC30);break;
77 aPacket.init(PESTYPE_AUD0);
78 setAudioStream(PESTYPE_AUD0);
84 int DemuxerTS::findPTS(UCHAR* buf, int len, ULLONG* dest)
86 UINT LoPattern = 0x100 | PESTYPE_VID0,
87 HiPattern = 0x100 | PESTYPE_VIDMAX;
91 UINT pattern = *(UINT*)buf;
93 if (pattern < LoPattern || pattern > HiPattern) continue;
95 UINT framelength = ((UINT)buf[3] << 8) | buf[4];
98 if ( buf[1] & 0x80 ) // PTS_DTS_flags indicate that PTS is present
100 *dest = ( (ULLONG)(buf[3] & 0x0E) << 29 ) |
101 ( (ULLONG)(buf[4]) << 22 ) |
102 ( (ULLONG)(buf[5] & 0xFE) << 14 ) |
103 ( (ULLONG)(buf[6]) << 7 ) |
104 ( (ULLONG)(buf[7] & 0xFE) >> 1 );
108 buf += framelength; len -= framelength;
114 int DemuxerTS::put(UCHAR* buf, int len)
116 int ret = 0; // return number of bytes consumed
120 if (len >= TS_SIZE + 1 - partPacket)
121 { // Remainder of partial packet is available, plus one
122 memcpy(store+partPacket, buf, TS_SIZE - partPacket);
123 ret += TS_SIZE - partPacket;
124 buf += TS_SIZE - partPacket;
125 len -= TS_SIZE - partPacket;
126 partPacket = TS_SIZE;
128 { // Packet is properly terminated
129 int rc = processTS(store);
131 partPacket = 0; // Successfully processed
133 return ret; // Try again later.
136 { // Packet not terminated. Find another candidate, and shift store
137 Log::getInstance()->log("TS Demuxer", Log::ERR, "TS Misaligned!");
139 while (search < partPacket && store[search] != TS_SIG)
141 partPacket -= search;
142 if (partPacket) memcpy(store, store+search, partPacket);
146 { // Still don't have complete packet. Consume what we do have.
147 memcpy(store+partPacket, buf, len);
154 // Position ourselves at a candidate TS packet
155 while (len > 0 && *buf != TS_SIG)
157 Log::getInstance()->log("TS Demuxer", Log::ERR, "TS Misaligned!");
163 if (len < TS_SIZE + 1)
164 { // Not enough data. Store what we have.
165 memcpy(store, buf, len);
171 if (buf[TS_SIZE] != TS_SIG)
172 { // Not terminated correctly.
174 while (len > 0 && *buf != TS_SIG)
181 int rc = processTS(buf);
183 { // Successfully processed
184 buf += TS_SIZE; ret += TS_SIZE; len -= TS_SIZE;
187 { // Processing failed.
195 int DemuxerTS::processTS(UCHAR* buf)
197 int datalen = TS_SIZE - 4;
198 int pid = ( (buf[1] & 0x1F) << 8 ) | buf[2];
199 UCHAR payload = buf[1] & 0x40;
201 if (buf[3] & 0x20) // Adaptation field is present
202 datalen -= (buf[4] + 1);
203 if (datalen < 0) // Error in stream TODO log this
205 if (datalen == 0) // Null packet
207 buf += (TS_SIZE - datalen);
219 parsePacketDetails(vPacket);
222 rc = submitPacket(vPacket);
232 parsePacketDetails(aPacket);
235 rc = submitPacket(aPacket);
239 if (rc == 0) return 0;
244 vPacket.init(PESTYPE_VID0);
245 buf += 6; datalen -= 6;
251 aPacket.init(PESTYPE_PRIVATE_1,PESTYPE_SUBSTREAM_AC30);break;
254 aPacket.init(PESTYPE_AUD0);break;
256 buf += 6; datalen -= 6;
260 if ( (pid == vID && vActive) ||
261 (pid == aID && aActive) )
263 PESPacket* packet = NULL;
264 if (pid == vID) packet = &vPacket;
265 if (pid == aID) packet = &aPacket;
268 if (packet->write(buf, datalen) == 0)
269 { // Writing to packet failed. It has overflowed.
272 parsePacketDetails(*packet);
275 if (submitPacket(*packet) == 0) return 0;
278 packet->write((UCHAR*)"\200\000\000", 3);
279 packet->write(buf, datalen);