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"
23 DemuxerTS::DemuxerTS(int p_vID, int p_aID)
29 void DemuxerTS::flush()
38 int DemuxerTS::scan(UCHAR *buf, int len)
43 void DemuxerTS::setVID(int p_vID)
48 void DemuxerTS::setAID(int p_aID)
53 int DemuxerTS::findPTS(UCHAR* buf, int len, ULLONG* dest)
55 UINT LoPattern = 0x100 | PESTYPE_VID0,
56 HiPattern = 0x100 | PESTYPE_VIDMAX;
60 UINT pattern = *(UINT*)buf;
62 if (pattern < LoPattern || pattern > HiPattern) continue;
64 UINT framelength = ((UINT)buf[3] << 8) | buf[4];
67 if ( buf[1] & 0x80 ) // PTS_DTS_flags indicate that PTS is present
69 *dest = ( (ULLONG)(buf[3] & 0x0E) << 29 ) |
70 ( (ULLONG)(buf[4]) << 22 ) |
71 ( (ULLONG)(buf[5] & 0xFE) << 14 ) |
72 ( (ULLONG)(buf[6]) << 7 ) |
73 ( (ULLONG)(buf[7] & 0xFE) >> 1 );
77 buf += framelength; len -= framelength;
83 int DemuxerTS::put(UCHAR* buf, int len)
85 int ret = 0; // return number of bytes consumed
89 if (len >= TS_SIZE + 1 - partPacket)
90 { // Remainder of partial packet is available, plus one
91 memcpy(store+partPacket, buf, TS_SIZE - partPacket);
92 ret += TS_SIZE - partPacket;
93 buf += TS_SIZE - partPacket;
94 len -= TS_SIZE - partPacket;
97 { // Packet is properly terminated
98 int rc = processTS(store);
100 partPacket = 0; // Successfully processed
102 return ret; // Try again later.
105 { // Packet not terminated. Find another candidate, and shift store
106 Log::getInstance()->log("TS Demuxer", Log::ERR, "TS Misaligned!");
108 while (search < partPacket && store[search] != TS_SIG)
110 partPacket -= search;
111 if (partPacket) memcpy(store, store+search, partPacket);
115 { // Still don't have complete packet. Consume what we do have.
116 memcpy(store+partPacket, buf, len);
123 // Position ourselves at a candidate TS packet
124 while (*buf != TS_SIG && len)
126 Log::getInstance()->log("TS Demuxer", Log::ERR, "TS Misaligned!");
132 if (len < TS_SIZE + 1)
133 { // Not enough data. Store what we have.
134 memcpy(store, buf, len);
140 if (buf[TS_SIZE] != TS_SIG)
141 { // Not terminated correctly.
142 while (*buf != TS_SIG && len)
149 int rc = processTS(buf);
151 { // Successfully processed
152 buf += TS_SIZE; ret += TS_SIZE; len -= TS_SIZE;
155 { // Processing failed.
163 int DemuxerTS::processTS(UCHAR* buf)
165 int datalen = TS_SIZE - 4;
166 int pid = ( (buf[1] & 0x1F) << 8 ) | buf[2];
167 UCHAR payload = buf[1] & 0x40;
169 if (buf[3] & 0x20) // Adaptation field is present
170 datalen -= (buf[4] + 1);
171 if (datalen < 0) // Error in stream TODO log this
173 if (datalen == 0) // Null packet
175 buf += (TS_SIZE - datalen);
185 parsePacketDetails(vPacket);
188 rc = submitPacket(vPacket);
194 parsePacketDetails(aPacket);
197 rc = submitPacket(aPacket);
199 if (rc == 0) return 0;
205 buf += 6; datalen -= 6;
210 buf += 6; datalen -= 6;
214 PESPacket* packet = NULL;
215 if (pid == vID) packet = &vPacket;
216 if (pid == aID) packet = &aPacket;
219 if (packet->write(buf, datalen) == 0)
220 { // Writing to packet failed. It has overflowed.
223 parsePacketDetails(*packet);
226 if (submitPacket(*packet) == 0) return 0;
229 packet->write((UCHAR*)"\200\000\000", 3);
230 packet->write(buf, datalen);