2 Copyright 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 "demuxerts.h"
23 DemuxerTS::DemuxerTS(int p_vID, int p_aID)
29 void DemuxerTS::flush()
37 int DemuxerTS::scan(UCHAR *buf, int len)
42 void DemuxerTS::setVID(int p_vID)
47 void DemuxerTS::setAID(int p_aID)
52 int DemuxerTS::findPTS(UCHAR* buf, int len, ULLONG* dest)
54 UINT LoPattern = 0x100 | PESTYPE_VID0,
55 HiPattern = 0x100 | PESTYPE_VIDMAX;
59 UINT pattern = *(UINT*)buf;
61 if (pattern < LoPattern || pattern > HiPattern) continue;
63 UINT framelength = ((UINT)buf[3] << 8) | buf[4];
66 if ( buf[1] & 0x80 ) // PTS_DTS_flags indicate that PTS is present
68 *dest = ( (ULLONG)(buf[3] & 0x0E) << 29 ) |
69 ( (ULLONG)(buf[4]) << 22 ) |
70 ( (ULLONG)(buf[5] & 0xFE) << 14 ) |
71 ( (ULLONG)(buf[6]) << 7 ) |
72 ( (ULLONG)(buf[7] & 0xFE) >> 1 );
76 buf += framelength; len -= framelength;
82 int DemuxerTS::put(UCHAR* buf, int len)
84 int ret = 0; // return number of bytes consumed
88 if (len >= TS_SIZE + 1 - partPacket)
89 { // Remainder of partial packet is available, plus one
90 memcpy(store+partPacket, buf, TS_SIZE - partPacket);
91 ret += TS_SIZE - partPacket;
92 buf += TS_SIZE - partPacket;
93 len -= TS_SIZE - partPacket;
96 { // Packet is properly terminated
97 int rc = processTS(store);
99 partPacket = 0; // Successfully processed
101 return ret; // Try again later.
104 { // Packet not terminated. Find another candidate, and shift store
105 Log::getInstance()->log("TS Demuxer", Log::ERR, "TS Misaligned!");
107 while (search < partPacket && store[search] != TS_SIG)
109 partPacket -= search;
110 if (partPacket) memcpy(store, store+search, partPacket);
114 { // Still don't have complete packet. Consume what we do have.
115 memcpy(store+partPacket, buf, len);
122 // Position ourselves at a candidate TS packet
123 while (*buf != TS_SIG && len)
125 Log::getInstance()->log("TS Demuxer", Log::ERR, "TS Misaligned!");
131 if (len < TS_SIZE + 1)
132 { // Not enough data. Store what we have.
133 memcpy(store, buf, len);
139 if (buf[TS_SIZE] != TS_SIG)
140 { // Not terminated correctly.
141 while (*buf != TS_SIG && len)
148 int rc = processTS(buf);
150 { // Successfully processed
151 buf += TS_SIZE; ret += TS_SIZE; len -= TS_SIZE;
154 { // Processing failed.
162 int DemuxerTS::processTS(UCHAR* buf)
164 int datalen = TS_SIZE - 4;
165 int pid = ( (buf[1] & 0x1F) << 8 ) | buf[2];
166 UCHAR payload = buf[1] & 0x40;
168 if (buf[3] & 0x20) // Adaptation field is present
169 datalen -= (buf[4] + 1);
170 if (datalen < 0) // Error in stream TODO log this
172 if (datalen == 0) // Null packet
174 buf += (TS_SIZE - datalen);
180 if (pid == vID) rc = vPacket.submit();
181 if (pid == aID) rc = aPacket.submit();
182 if (rc == 0) return 0;
187 buf += 6; datalen -= 6;
192 buf += 6; datalen -= 6;
196 PESPacket* packet = NULL;
197 if (pid == vID) packet = &vPacket;
198 if (pid == aID) packet = &aPacket;
201 if (packet->write(buf, datalen) == 0)
202 { // Writing to packet failed. It has overflowed.
203 if (packet->submit() == 0)
206 packet->write((UCHAR*)"\200\000\000", 3);
207 packet->write(buf, datalen);