2 Copyright 2006-2008 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 Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include "demuxerts.h"
26 #define PTS_JUMP_MARGIN 10000
27 #define PTS_ALLOWANCE 90000
29 // TODO: PTS class to handle wrapping arithmetic & comparisons?
30 static ULLONG PTSDistance(ULLONG pts1, ULLONG pts2)
32 // Assume pts1, pts2 < 2^33; calculate shortest distance between
33 ULLONG ret = (pts1 > pts2) ? pts1 - pts2 : pts2 - pts1;
34 if (ret > (1LL<<32)) ret = (1LL<<33) - ret;
38 static ULLONG PTSDifference(ULLONG pts1, ULLONG pts2)
40 // Assume pts1, pts2 < 2^33; calculate pts1 - pts2
44 return (1LL<<33) + pts1 - pts2;
47 DemuxerTS::DemuxerTS(int p_vID, int p_aID, int p_subID, int p_tID)
49 vID = p_vID; vActive = false;
50 aID = p_aID; aActive = false;
51 subID = p_subID; subActive = false;
55 havechannelinfo=false;
58 void DemuxerTS::flush()
62 havechannelinfo=false;
64 vPacket.init(PESTYPE_VID0);
68 aPacket.init(PESTYPE_PRIVATE_1, PESTYPE_SUBSTREAM_AC30);
72 aPacket.init(PESTYPE_AUD0);
75 subPacket.init(PESTYPE_PRIVATE_1);
76 tPacket.init(PESTYPE_PRIVATE_1,PESTYPE_SUBSTREAM_TELETEXTMAX);
85 int DemuxerTS::scan(UCHAR *buf, int len)
90 return PESTYPE_PRIVATE_1;
97 void DemuxerTS::setVID(int p_vID)
100 vPacket.init(PESTYPE_VID0);
104 void DemuxerTS::setAID(int p_aID, int type)
111 aPacket.init(PESTYPE_PRIVATE_1, PESTYPE_SUBSTREAM_AC30);
112 setAudioStream(PESTYPE_SUBSTREAM_AC30);
116 aPacket.init(PESTYPE_AUD0);
117 setAudioStream(PESTYPE_AUD0);
123 void DemuxerTS::setSubID(int p_subID)
126 subPacket.init(PESTYPE_PRIVATE_1);
131 void DemuxerTS::setTID(int p_tID)
134 tPacket.init(PESTYPE_PRIVATE_1,PESTYPE_SUBSTREAM_TELETEXTMAX);
142 int DemuxerTS::findPTS(UCHAR* buf, int len, ULLONG* dest)
146 while (len >= TS_SIZE)
148 if (*buf != TS_SIG) {buf++;len--; continue;}
150 //Pattern scanning won't work for ts
153 int datalen = TS_SIZE - 4;
154 int pid = ( (buf[1] & 0x1F) << 8 ) | buf[2];
155 UCHAR payload = buf[1] & 0x40;
157 if (buf[3] & 0x20) // Adaptation field is present
158 datalen -= (buf[4] + 1);
160 UCHAR* curbuf =buf+ (TS_SIZE - datalen);
164 if (pid == 0x00) {//PAT, only take first program number, ignore the rest
165 int pmtpid = (*(curbuf+11)<< 8) | *(curbuf+12);
166 if ((pmtpid >> 13) != 0x07)
168 Log::getInstance()->log("findPTS", Log::DEBUG, "PMTPID=%02x %02x TRAILING 111 not set but %x", *(curbuf+11),*(curbuf+12), (pmtpid >> 13));
172 pmtpid = pmtpid & 0x1FFF; //clear upper 3 bits
176 } else if (pid == PMTPID) { //PMT
177 int sectionlength = ((*(curbuf+2) << 8) & 0x0F ) | *(buf+3);
178 //sectionlength += 4; //include header but subtract crc in the end...
179 int p = 13; //skip fixed part of pmt
180 while ( p < sectionlength) {
181 int streamtype = *(curbuf+p);
183 int foundpid = (*(curbuf+p)<< 8) | *(curbuf+p+1);
184 p += 2; //skip ES Pid
185 int eslength = ((*(curbuf+p) << 8) & 0x0F ) | *(curbuf+p+1);
186 p += 2; //skip ES length
187 if ((foundpid >> 13) != 0x07)
189 Log::getInstance()->log("findPTS", Log::DEBUG, "FOUNDPID=%02x %02x TRAILING 111 not set but %x", *(buf+p),*(buf+p+1), (foundpid >> 13));
193 foundpid = foundpid & 0x1FFF; //clear upper 3 bits
195 if (streamtype==3 || streamtype ==4) {
199 p += eslength; //skip es descriptor
201 } else if (pid == scanaid) {
202 UINT framelength = ((UINT)curbuf[4] << 8) | curbuf[5];
204 if ( curbuf[7] & 0x80 ) // PTS_DTS_flags indicate that PTS is present
206 *dest = ( (ULLONG)(curbuf[9] & 0x0E) << 29 ) |
207 ( (ULLONG)(curbuf[10]) << 22 ) |
208 ( (ULLONG)(curbuf[11] & 0xFE) << 14 ) |
209 ( (ULLONG)(curbuf[12]) << 7 ) |
210 ( (ULLONG)(curbuf[13] & 0xFE) >> 1 );
222 void DemuxerTS::setFrameNum(ULONG frame)
224 frameCounting = true;
226 Log::getInstance()->log("DemuxerTS", Log::DEBUG, "setFrameNum %d", frame);
229 void DemuxerTS::setPacketNum(ULONG npacket)
231 packetCounting = true;
232 packetNumber = npacket;
233 Log::getInstance()->log("DemuxerTS", Log::DEBUG, "setPacketNum %d", npacket);
237 int DemuxerTS::put(UCHAR* buf, int len)
239 int ret = 0; // return number of bytes consumed
243 if (len >= TS_SIZE + 1 - partPacket)
244 { // Remainder of partial packet is available, plus one
245 memcpy(store+partPacket, buf, TS_SIZE - partPacket);
246 ret += TS_SIZE - partPacket;
247 buf += TS_SIZE - partPacket;
248 len -= TS_SIZE - partPacket;
249 partPacket = TS_SIZE;
251 { // Packet is properly terminated
252 int rc = processTS(store);
254 partPacket = 0; // Successfully processed
256 return ret; // Try again later.
259 { // Packet not terminated. Find another candidate, and shift store
260 Log::getInstance()->log("TS Demuxer", Log::ERR, "TS Misaligned!");
262 while (search < partPacket && store[search] != TS_SIG)
264 partPacket -= search;
265 if (partPacket) memcpy(store, store+search, partPacket);
269 { // Still don't have complete packet. Consume what we do have.
270 memcpy(store+partPacket, buf, len);
277 // Position ourselves at a candidate TS packet
278 while (len > 0 && *buf != TS_SIG)
280 Log::getInstance()->log("TS Demuxer", Log::ERR, "TS Misaligned!");
286 if (len < TS_SIZE + 1)
287 { // Not enough data. Store what we have.
288 memcpy(store, buf, len);
294 if (buf[TS_SIZE] != TS_SIG)
295 { // Not terminated correctly.
297 while (len > 0 && *buf != TS_SIG)
304 int rc = processTS(buf);
306 { // Successfully processed
307 buf += TS_SIZE; ret += TS_SIZE; len -= TS_SIZE;
310 { // Processing failed.
318 int DemuxerTS::processTS(UCHAR* buf)
320 int datalen = TS_SIZE - 4;
322 int pid = ( (buf[1] & 0x1F) << 8 ) | buf[2];
323 UCHAR payload = buf[1] & 0x40;
325 if (buf[3] & 0x20) // Adaptation field is present
326 datalen -= (buf[4] + 1);
327 if (datalen < 0) // Error in stream TODO log this
329 if (datalen == 0) // Null packet
331 buf += (TS_SIZE - datalen);
336 if (pid == 0x00) {//PAT, only take first program number, ignore the rest
337 int pmtpid = (*(buf+11)<< 8) | *(buf+12);
338 if ((pmtpid >> 13) != 0x07)
340 Log::getInstance()->log("ProcessTS", Log::DEBUG, "PMTPID=%02x %02x TRAILING 111 not set but %x", *(buf+11),*(buf+12), (pmtpid >> 13));
344 pmtpid = pmtpid & 0x1FFF; //clear upper 3 bits
351 int sectionlength = ((*(buf+2) << 8) & 0x0F ) | *(buf+3);
352 //sectionlength += 4; //include header but subtract crc in the end...
353 int p = 13; //skip fixed part of pmt
354 Channel new_channelinfo;
355 new_channelinfo.numAPids=0;
356 new_channelinfo.numDPids=0;
357 new_channelinfo.numSPids=0;
358 new_channelinfo.number=0;
359 new_channelinfo.type=VDR::RADIO;
360 new_channelinfo.name=NULL;
361 new_channelinfo.tpid=0xFFFFF; //unused, check this
362 new_channelinfo.vpid=0xFFFFF; //unused, check this
363 new_channelinfo.index=NULL;
365 new_channelinfo.apids.clear();
366 new_channelinfo.dpids.clear();
367 new_channelinfo.spids.clear();
369 while ( p < sectionlength) {
370 int streamtype = *(buf+p);
372 int foundpid = (*(buf+p)<< 8) | *(buf+p+1);
373 p += 2; //skip ES Pid
374 int eslength = ((*(buf+p) << 8) & 0x0F ) | *(buf+p+1);
375 p += 2; //skip ES length
376 if ((foundpid >> 13) != 0x07)
378 Log::getInstance()->log("ProcessTS", Log::DEBUG, "FOUNDPID=%02x %02x TRAILING 111 not set but %x", *(buf+p),*(buf+p+1), (foundpid >> 13));
382 foundpid = foundpid & 0x1FFF; //clear upper 3 bits
385 // Log::getInstance()->log("ProcessTS", Log::DEBUG, "FOUNDPID is %x", foundpid);
388 // case 0x1B: //MPEG 4 for future use
391 if (foundpid != getVID())
393 new_channelinfo.type=VDR::VIDEO;
394 new_channelinfo.vpid=foundpid;
396 // Log::getInstance()->log("ProcessTS", Log::DEBUG, "Set video PID to %x", foundpid);
401 newapid.pid = foundpid;
402 newapid.name = NULL; //set it in player
403 new_channelinfo.apids.push_back(newapid);
404 new_channelinfo.numAPids++;
405 if (getAID() == 0) { //set unset AID to first audio pid that reports itself
407 Log::getInstance()->log("ProcessTS", Log::DEBUG, "Set audio PID to %x", foundpid);
411 case 6: { //Private Data
413 newapid.pid = foundpid;
414 newapid.name = NULL; //set it in player
418 while (pos< eslength && notfound) {
419 switch (buf[p+pos]) {
420 case 0x6A: {//Ac3 descriptor
421 new_channelinfo.dpids.push_back(newapid);
422 new_channelinfo.numDPids++;
425 case 0x59: {//SubtitlingDescriptor
426 new_channelinfo.spids.push_back(newapid);
427 new_channelinfo.numSPids++;
431 new_channelinfo.tpid=foundpid;
439 default://TODO how about subtitles and second audio pids
444 p += eslength; //skip es descriptor
448 bool audioPIDpresent=false; //Check if pids chnages
450 for (i=0;i<channelinfo.numAPids;i++) {
451 if (aID ==channelinfo.apids[i].pid) {
452 audioPIDpresent=true;
455 for (i=0;i<channelinfo.numDPids && (! audioPIDpresent);i++) {
456 if (aID ==channelinfo.dpids[i].pid) {
457 audioPIDpresent=true;
460 if (! audioPIDpresent) {
461 if (channelinfo.numAPids>0) {
462 setAID(channelinfo.apids[0].pid,0);
463 } else if (channelinfo.numDPids>0) {
464 setAID(channelinfo.dpids[0].pid,1);
468 channelinfo=new_channelinfo;
469 havechannelinfo=true;
483 parseTSPacketDetails(vPacket);
486 rc = submitPacket(vPacket);
496 parseTSPacketDetails(aPacket);
499 rc = submitPacket(aPacket);
509 parseTSPacketDetails(subPacket);
512 rc = submitPacket(subPacket);
516 if (isteletextdecoded && pid == tID)
522 parseTSPacketDetails(tPacket);
525 rc = submitPacket(tPacket);
529 if (rc == 0) return 0;
534 vPacket.init(PESTYPE_VID0);
535 buf += 6; datalen -= 6;
542 aPacket.init(PESTYPE_PRIVATE_1, PESTYPE_SUBSTREAM_AC30);
546 aPacket.init(PESTYPE_AUD0);
549 buf += 6; datalen -= 6;
553 subPacket.init(PESTYPE_PRIVATE_1);
554 subLength = (buf[4] << 8) + buf[5];
555 buf += 6; datalen -= 6;
557 if (isteletextdecoded && pid == tID)
559 tPacket.init(PESTYPE_PRIVATE_1,PESTYPE_SUBSTREAM_TELETEXTMAX);
560 buf += 6; datalen -= 6;
564 if ( (pid == vID && vActive) ||
565 (pid == aID && aActive) ||
566 (pid == tID && tActive) ||
567 (pid == subID && subActive) )
569 PESPacket* packet = NULL;
570 if (pid == vID) packet = &vPacket;
571 if (pid == aID) packet = &aPacket;
575 if (pid == tID) packet = &tPacket;
578 if (packet->write(buf, datalen) == 0)
579 { // Writing to packet failed. It has overflowed.
582 parseTSPacketDetails(*packet);
585 if (submitPacket(*packet) == 0) return 0;
588 packet->write((UCHAR*)"\200\000\000", 3);
589 packet->write(buf, datalen);
594 if (pid == subID && subActive && subPacket.getLength() == subLength)
596 parsePacketDetails(subPacket);
597 Log::getInstance()->log("DEMUXERTS", Log::DEBUG, "SUBMITTING A SUBTITLE PACKET %d %x", subLength, subPacket.getSubstream());
598 submitPacket(subPacket);
605 ULONG DemuxerTS::getPacketNum()
610 ULONG DemuxerTS::getFrameNumFromPTS(ULLONG pts)
612 ULLONG difference = (1LL<<33);
614 int total = 0, actual = 0;
615 pts_map_mutex.Lock();
616 PTSMap::iterator iter = pts_map.begin();
617 while (iter != pts_map.end())
620 if (PTSDifference(iter->pts, pts) < PTS_ALLOWANCE)
623 ref_frame = iter->frame;
627 ULLONG newdiff = PTSDifference(pts, iter->pts);
628 if (newdiff < difference)
630 difference = newdiff;
631 ref_frame = iter->frame;
636 if (total > 1 && actual == 1) // We are using the most recent PTS ref.
637 { // Delete the rest.
638 iter = pts_map.begin(); iter++;
639 pts_map.erase(iter, pts_map.end());
641 pts_map_mutex.Unlock();
643 if (difference == (1LL<<33))
644 return 0; // We cannot make sense of the pts
646 return ref_frame + difference * Video::getInstance()->getFPS() / 90000;
650 void DemuxerTS::parseTSPacketDetails(PESPacket packet) // Only important stuff for paket counting reminas
652 parsePacketDetails(packet);
653 if (packetCounting && packet.getPacketType() >= PESTYPE_AUD0 &&
654 packet.getPacketType() <= PESTYPE_AUDMAX)
659 if (frameCounting && packet.findPictureHeader() &&
660 packet.getPacketType() >= PESTYPE_VID0 &&
661 packet.getPacketType() <= PESTYPE_VIDMAX)
663 ULONG frame_num = (frameNumber)++;
664 if (packet.findSeqHeader() > 1 && packet.hasPTS())
667 pts_map_mutex.Lock();
670 me.pts = packet.getPTS();
671 me.frame = frame_num;
672 pts_map_mutex.Unlock();
673 pts_map_mutex.Lock();
674 pts_map.push_front(me);
676 me = pts_map.front();
677 pts_map_mutex.Unlock();
679 UINT fps = Video::getInstance()->getFPS();
680 ULLONG pts_expected = me.pts + 90000*(frame_num - me.frame) / fps;
681 while (pts_expected > (1LL<<33)) pts_expected -= (1LL<<33);
683 if (PTSDistance(pts_expected, packet.getPTS()) > PTS_JUMP_MARGIN) // PTS jump!
685 me.pts = packet.getPTS();
686 me.frame = frame_num;
687 pts_map_mutex.Lock();
688 pts_map.push_front(me);
689 pts_map_mutex.Unlock();
696 bool DemuxerTS::scanForVideo(UCHAR* buf, UINT len)
700 while (len >= TS_SIZE)
702 if (*buf != TS_SIG) {buf++;len--; continue;}
704 //Pattern scanning won't work for ts
707 int datalen = TS_SIZE - 4;
708 int pid = ( (buf[1] & 0x1F) << 8 ) | buf[2];
709 UCHAR payload = buf[1] & 0x40;
711 if (buf[3] & 0x20) // Adaptation field is present
712 datalen -= (buf[4] + 1);
714 UCHAR* curbuf =buf+ (TS_SIZE - datalen);
717 if (pid == 0x00) {//PAT, only take first program number, ignore the rest
718 int pmtpid = (*(curbuf+11)<< 8) | *(curbuf+12);
719 if ((pmtpid >> 13) != 0x07)
721 Log::getInstance()->log("findPTS", Log::DEBUG, "PMTPID=%02x %02x TRAILING 111 not set but %x", *(curbuf+11),*(curbuf+12), (pmtpid >> 13));
725 pmtpid = pmtpid & 0x1FFF; //clear upper 3 bits
729 } else if (pid == pmtpidy) { //PMT
730 int sectionlength = ((*(curbuf+2) << 8) & 0x0F ) | *(buf+3);
731 //sectionlength += 4; //include header but subtract crc in the end...
732 int p = 13; //skip fixed part of pmt
733 while ( p < sectionlength) {
734 int streamtype = *(curbuf+p);
736 int foundpid = (*(curbuf+p)<< 8) | *(curbuf+p+1);
737 p += 2; //skip ES Pid
738 int eslength = ((*(curbuf+p) << 8) & 0x0F ) | *(curbuf+p+1);
739 p += 2; //skip ES length
740 if ((foundpid >> 13) != 0x07)
742 Log::getInstance()->log("findPTS", Log::DEBUG, "FOUNDPID=%02x %02x TRAILING 111 not set but %x", *(buf+p),*(buf+p+1), (foundpid >> 13));
746 foundpid = foundpid & 0x1FFF; //clear upper 3 bits
748 if (streamtype==1 || streamtype ==2) {
752 p += eslength; //skip es descriptor
763 UINT DemuxerTS::stripAudio(UCHAR* buf, UINT len) //it has to be adapted
765 //This function strips all TS Headers and non video payload
770 while (readpos < len ) {
771 if (buf[readpos] != TS_SIG) {readpos++; continue;}
772 UINT oldreadpos=readpos;
774 int datalen = TS_SIZE - 4;
775 int pid = ( (buf[readpos+1] & 0x1F) << 8 ) | buf[readpos+2];
776 UCHAR payload = buf[readpos+1] & 0x40;
777 if (buf[readpos+3] & 0x20) { // Adaptation field is present
778 datalen -= (buf[readpos+4] + 1);
780 if (datalen < 0) {// Error in stream TODO log this
783 if (datalen == 0) {// Null packet
784 readpos=oldreadpos+TS_SIZE;
787 readpos += (TS_SIZE - datalen);
788 UINT towrite=min(datalen,len-readpos);
792 parsePacketDetails(destpaket);
793 memcpy(buf+writepos,destpaket.getData(),destpaket.getSize());
794 writepos+=destpaket.getSize();
796 destpaket.init(PESTYPE_VID0);
797 readpos += 6; towrite -= 6;
802 if (!destpaket.write(buf+readpos,towrite)) {
803 parsePacketDetails(destpaket);
804 memcpy(buf+writepos,destpaket.getData(),destpaket.getSize());
805 writepos+=destpaket.getSize();
806 destpaket.truncate();
807 destpaket.write((UCHAR*)"\200\000\000", 3);
808 destpaket.write(buf+readpos,towrite);
816 readpos=oldreadpos+TS_SIZE;
818 parsePacketDetails(destpaket);
819 memcpy(buf+writepos,destpaket.getData(),destpaket.getSize());
820 writepos+=destpaket.getSize();