2 Copyright 2006-2008 Mark Calderbank
\r
4 This file is part of VOMP.
\r
6 VOMP is free software; you can redistribute it and/or modify
\r
7 it under the terms of the GNU General Public License as published by
\r
8 the Free Software Foundation; either version 2 of the License, or
\r
9 (at your option) any later version.
\r
11 VOMP is distributed in the hope that it will be useful,
\r
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 GNU General Public License for more details.
\r
16 You should have received a copy of the GNU General Public License
\r
17 along with VOMP; if not, write to the Free Software Foundation, Inc.,
\r
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
\r
21 #include "demuxerts.h"
\r
27 #define PTS_JUMP_MARGIN 10000
\r
28 #define PTS_ALLOWANCE 90000
\r
30 // TODO: PTS class to handle wrapping arithmetic & comparisons?
\r
31 static ULLONG PTSDistance(ULLONG pts1, ULLONG pts2)
\r
33 // Assume pts1, pts2 < 2^33; calculate shortest distance between
\r
34 ULLONG ret = (pts1 > pts2) ? pts1 - pts2 : pts2 - pts1;
\r
35 if (ret > (1LL<<32)) ret = (1LL<<33) - ret;
\r
39 static ULLONG PTSDifference(ULLONG pts1, ULLONG pts2)
\r
41 // Assume pts1, pts2 < 2^33; calculate pts1 - pts2
\r
45 return (1LL<<33) + pts1 - pts2;
\r
48 DemuxerTS::DemuxerTS(int p_vID, int p_aID, int p_subID, int p_tID)
\r
50 vID = p_vID; vActive = false;
\r
51 aID = p_aID; aActive = false;
\r
52 subID = p_subID; subActive = false;
\r
56 havechannelinfo=false;
\r
57 //doubledframerate=false;
\r
61 void DemuxerTS::flush()
\r
65 havechannelinfo=false;
\r
67 vPacket.init(PESTYPE_VID0);
\r
71 aPacket.init(PESTYPE_PRIVATE_1, PESTYPE_SUBSTREAM_AC30);
\r
75 aPacket.init(PESTYPE_AUD0);
\r
78 subPacket.init(PESTYPE_PRIVATE_1);
\r
79 tPacket.init(PESTYPE_PRIVATE_1,PESTYPE_SUBSTREAM_TELETEXTMAX);
\r
86 // doubledframerate=false;
\r
90 int DemuxerTS::scan(UCHAR *buf, int len)
\r
95 return PESTYPE_PRIVATE_1;
\r
98 return PESTYPE_AUD0;
\r
102 void DemuxerTS::setVID(int p_vID)
\r
105 vPacket.init(PESTYPE_VID0);
\r
109 void DemuxerTS::setAID(int p_aID, int type, int streamtype)
\r
113 astreamtype = streamtype;
\r
117 aPacket.init(PESTYPE_PRIVATE_1, PESTYPE_SUBSTREAM_AC30);
\r
118 setAudioStream(PESTYPE_SUBSTREAM_AC30);
\r
122 aPacket.init(PESTYPE_AUD0);
\r
123 setAudioStream(PESTYPE_AUD0);
\r
129 void DemuxerTS::setSubID(int p_subID)
\r
132 subPacket.init(PESTYPE_PRIVATE_1);
\r
137 void DemuxerTS::setTID(int p_tID)
\r
140 tPacket.init(PESTYPE_PRIVATE_1,PESTYPE_SUBSTREAM_TELETEXTMAX);
\r
148 int DemuxerTS::findPTS(UCHAR* buf, int len, ULLONG* dest)
\r
152 while (len >= TS_SIZE)
\r
154 if (*buf != TS_SIG) {buf++;len--; continue;}
\r
156 //Pattern scanning won't work for ts
\r
159 int datalen = TS_SIZE - 4;
\r
160 int pid = ( (buf[1] & 0x1F) << 8 ) | buf[2];
\r
161 UCHAR payload = buf[1] & 0x40;
\r
163 if (buf[3] & 0x20) // Adaptation field is present
\r
164 datalen -= (buf[4] + 1);
\r
166 UCHAR* curbuf =buf+ (TS_SIZE - datalen);
\r
170 if (pid == 0x00) {//PAT, only take first program number, ignore the rest
\r
171 int pmtpid = (*(curbuf+11)<< 8) | *(curbuf+12);
\r
172 if ((pmtpid >> 13) != 0x07)
\r
174 Log::getInstance()->log("findPTS", Log::DEBUG, "PMTPID=%02x %02x TRAILING 111 not set but %x", *(curbuf+11),*(curbuf+12), (pmtpid >> 13));
\r
178 pmtpid = pmtpid & 0x1FFF; //clear upper 3 bits
\r
182 } else if (pid == PMTPID) { //PMT
\r
183 int sectionlength = ((*(curbuf+2) << 8) & 0x0F ) | *(buf+3);
\r
184 //sectionlength += 4; //include header but subtract crc in the end...
\r
185 int p = 13; //skip fixed part of pmt
\r
186 while ( p < sectionlength) {
\r
187 int streamtype = *(curbuf+p);
\r
189 int foundpid = (*(curbuf+p)<< 8) | *(curbuf+p+1);
\r
190 p += 2; //skip ES Pid
\r
191 int eslength = ((*(curbuf+p) << 8) & 0x0F ) | *(curbuf+p+1);
\r
192 p += 2; //skip ES length
\r
193 if ((foundpid >> 13) != 0x07)
\r
195 Log::getInstance()->log("findPTS", Log::DEBUG, "FOUNDPID=%02x %02x TRAILING 111 not set but %x", *(buf+p),*(buf+p+1), (foundpid >> 13));
\r
199 foundpid = foundpid & 0x1FFF; //clear upper 3 bits
\r
200 //int pos=0; UNUSED?
\r
201 if (streamtype==3 || streamtype ==4) {
\r
205 p += eslength; //skip es descriptor
\r
207 } else if (pid == scanaid) {
\r
208 // UINT framelength = ((UINT)curbuf[4] << 8) | curbuf[5]; UNUSED?
\r
210 if ( curbuf[7] & 0x80 ) // PTS_DTS_flags indicate that PTS is present
\r
212 *dest = ( (ULLONG)(curbuf[9] & 0x0E) << 29 ) |
\r
213 ( (ULLONG)(curbuf[10]) << 22 ) |
\r
214 ( (ULLONG)(curbuf[11] & 0xFE) << 14 ) |
\r
215 ( (ULLONG)(curbuf[12]) << 7 ) |
\r
216 ( (ULLONG)(curbuf[13] & 0xFE) >> 1 );
\r
228 void DemuxerTS::setFrameNum(ULONG frame)
\r
230 frameCounting = true;
\r
231 frameNumber = frame;
\r
233 Log::getInstance()->log("DemuxerTS", Log::DEBUG, "setFrameNum %d", frame);
\r
236 void DemuxerTS::setPacketNum(ULONG npacket)
\r
238 packetCounting = true;
\r
239 packetNumber = npacket;
\r
240 Log::getInstance()->log("DemuxerTS", Log::DEBUG, "setPacketNum %d", npacket);
\r
243 int DemuxerTS::put(UCHAR* buf, int len)
\r
245 int ret = 0; // return number of bytes consumed
\r
247 bool misaligned_mess=false;
\r
250 if (len >= TS_SIZE + 1 - partPacket)
\r
251 { // Remainder of partial packet is available, plus one
\r
252 memcpy(store+partPacket, buf, TS_SIZE - partPacket);
\r
253 ret += TS_SIZE - partPacket;
\r
254 buf += TS_SIZE - partPacket;
\r
255 len -= TS_SIZE - partPacket;
\r
256 partPacket = TS_SIZE;
\r
257 if (*buf == TS_SIG)
\r
258 { // Packet is properly terminated
\r
259 int rc = processTS(store);
\r
261 partPacket = 0; // Successfully processed
\r
263 return ret; // Try again later.
\r
266 { // Packet not terminated. Find another candidate, and shift store
\r
267 if (!misaligned_mess) {
\r
268 Log::getInstance()->log("TS Demuxer", Log::ERR, "TS Misaligned!A");
\r
269 misaligned_mess=true; // do not alarm more than once
\r
272 while (search < partPacket && store[search] != TS_SIG)
\r
274 partPacket -= search;
\r
275 if (partPacket) memcpy(store, store+search, partPacket);
\r
279 { // Still don't have complete packet. Consume what we do have.
\r
280 memcpy(store+partPacket, buf, len);
\r
287 // Position ourselves at a candidate TS packet
\r
288 while (len > 0 && *buf != TS_SIG)
\r
290 if (!misaligned_mess) {
\r
291 Log::getInstance()->log("TS Demuxer", Log::ERR, "TS Misaligned!B");
\r
292 misaligned_mess=true; // do not alarm more than once
\r
294 buf++; ret++; len--;
\r
299 if (len < TS_SIZE + 1)
\r
300 { // Not enough data. Store what we have.
\r
301 memcpy(store, buf, len);
\r
307 if (buf[TS_SIZE] != TS_SIG)
\r
308 { // Not terminated correctly.
\r
309 buf++; ret++; len--;
\r
310 while (len > 0 && *buf != TS_SIG)
\r
312 buf++; ret++; len--;
\r
317 int rc = processTS(buf);
\r
319 { // Successfully processed
\r
320 buf += TS_SIZE; ret += TS_SIZE; len -= TS_SIZE;
\r
323 { // Processing failed.
\r
332 int DemuxerTS::processTS(UCHAR* buf)
\r
334 int datalen = TS_SIZE - 4;
\r
336 int pid = ( (buf[1] & 0x1F) << 8 ) | buf[2];
\r
337 UCHAR payload = buf[1] & 0x40;
\r
340 if (buf[3] & 0x20) // Adaptation field is present
\r
341 datalen -= (buf[4] + 1);
\r
342 if (datalen < 0) // Error in stream TODO log this
\r
344 if (datalen == 0) // Null packet
\r
346 // if (!(buf[3] &0x10)) return 1; // no payload
\r
347 buf += (TS_SIZE - datalen);
\r
352 if (pid == 0x00) {//PAT, only take first program number, ignore the rest
\r
353 int pmtpid = (*(buf+11)<< 8) | *(buf+12);
\r
354 if ((pmtpid >> 13) != 0x07)
\r
356 Log::getInstance()->log("ProcessTS", Log::DEBUG, "PMTPID=%02x %02x TRAILING 111 not set but %x", *(buf+11),*(buf+12), (pmtpid >> 13));
\r
360 pmtpid = pmtpid & 0x1FFF; //clear upper 3 bits
\r
365 if (pid == PMTPID)
\r
367 int sectionlength = ((*(buf+2) << 8) & 0x0F ) | *(buf+3);
\r
368 //sectionlength += 4; //include header but subtract crc in the end...
\r
369 int p = 13; //skip fixed part of pmt
\r
370 Channel new_channelinfo;
\r
371 new_channelinfo.numAPids=0;
\r
372 new_channelinfo.numDPids=0;
\r
373 new_channelinfo.numSPids=0;
\r
374 new_channelinfo.number=0;
\r
375 new_channelinfo.type=VDR::RADIO;
\r
376 new_channelinfo.name=NULL;
\r
377 new_channelinfo.tpid=0xFFFFF; //unused, check this
\r
378 new_channelinfo.vpid=0xFFFFF; //unused, check this
\r
379 new_channelinfo.index=0;
\r
381 new_channelinfo.apids.clear();
\r
382 new_channelinfo.dpids.clear();
\r
383 new_channelinfo.spids.clear();
\r
385 while ( p < sectionlength) {
\r
386 int streamtype = *(buf+p);
\r
388 int foundpid = (*(buf+p)<< 8) | *(buf+p+1);
\r
389 p += 2; //skip ES Pid
\r
390 int eslength = ((*(buf+p) << 8) & 0x0F ) | *(buf+p+1);
\r
391 p += 2; //skip ES length
\r
392 if ((foundpid >> 13) != 0x07)
\r
394 Log::getInstance()->log("ProcessTS", Log::DEBUG, "FOUNDPID=%02x %02x TRAILING 111 not set but %x", *(buf+p),*(buf+p+1), (foundpid >> 13));
\r
398 foundpid = foundpid & 0x1FFF; //clear upper 3 bits
\r
399 bool notfound=false;
\r
402 // Log::getInstance()->log("ProcessTS", Log::DEBUG, "FOUNDPID is %x %x", foundpid,streamtype);
\r
403 switch (streamtype)
\r
405 case 0x1B: //MPEG 4 for future use
\r
408 if (foundpid != getVID())
\r
410 new_channelinfo.type=VDR::VIDEO;
\r
411 new_channelinfo.vstreamtype=streamtype;
\r
412 new_channelinfo.vpid=foundpid;
\r
413 if (streamtype==0x1b) h264=true;
\r
416 // Log::getInstance()->log("ProcessTS", Log::DEBUG, "Set video PID to %x", foundpid);
\r
418 case 0x0F: //AAC ADTS packaging
\r
419 case 0x11: // LATM packaging
\r
423 newapid.pid = foundpid;
\r
425 newapid.type=streamtype;
\r
428 while (pos< eslength && nolang) {
\r
429 switch (buf[p+pos]) {
\r
431 newapid.desc[0]=buf[p+pos+2];
\r
432 newapid.desc[1]=buf[p+pos+3];
\r
433 newapid.desc[2]=buf[p+pos+4];
\r
436 // Log::getInstance()->log("ProcessTS", Log::DEBUG, "FOUNDLANG is %s", newapid.desc);
\r
440 pos+=2+buf[p+pos+1];
\r
443 new_channelinfo.apids.push_back(newapid);
\r
444 new_channelinfo.numAPids++;
\r
445 if (getAID() == 0 && Audio::getInstance()->streamTypeSupported(streamtype)) { //set unset AID to first audio pid that reports itself
\r
446 setAID(foundpid,0,streamtype);
\r
447 Log::getInstance()->log("ProcessTS", Log::DEBUG, "Set audio PID to %x", foundpid);
\r
451 case 6: { //Private Data
\r
453 newapid.pid = foundpid;
\r
454 newapid.desc[0]=0; //set it in player
\r
460 while (pos< eslength && (notfound || nolang)) {
\r
461 switch (buf[p+pos]) {
\r
462 case 0x7A: // Enhanced Ac3 Desriptor
\r
463 case 0x6A: {//Ac3 descriptor
\r
464 newapid.type=buf[p+pos];
\r
469 case 0x59: {//SubtitlingDescriptor
\r
471 newapid.type=buf[p+pos];
\r
472 newapid.desc[0]=buf[p+pos+2];
\r
473 newapid.desc[1]=buf[p+pos+3];
\r
474 newapid.desc[2]=buf[p+pos+4];
\r
476 newapid.data1=(buf[p+pos+5]<<8) |(buf[p+pos+6]);
\r
477 newapid.data2=(buf[p+pos+7]<<8) |(buf[p+pos+8]);
\r
478 // Log::getInstance()->log("ProcessTS", Log::DEBUG, "FOUNDSUB is %s", newapid.desc);
\r
483 newapid.desc[0]=buf[p+pos+2];
\r
484 newapid.desc[1]=buf[p+pos+3];
\r
485 newapid.desc[2]=buf[p+pos+4];
\r
488 // Log::getInstance()->log("ProcessTS", Log::DEBUG, "FOUNDLANG is %s", newapid.desc);
\r
492 new_channelinfo.tpid=foundpid;
\r
496 pos+=2+buf[p+pos+1];
\r
499 new_channelinfo.dpids.push_back(newapid);
\r
500 new_channelinfo.numDPids++;
\r
501 } else if (type==2) {
\r
502 new_channelinfo.spids.push_back(newapid);
\r
503 new_channelinfo.numSPids++;
\r
507 default://TODO how about subtitles and second audio pids
\r
512 p += eslength; //skip es descriptor
\r
516 bool audioPIDpresent=false; //Check if pids chnages
\r
518 for (i=0;i<channelinfo.numAPids;i++) {
\r
519 if (aID == (int)channelinfo.apids[i].pid) {
\r
520 audioPIDpresent=true;
\r
523 for (i=0;i<channelinfo.numDPids && (! audioPIDpresent);i++) {
\r
524 if (aID == (int)channelinfo.dpids[i].pid) {
\r
525 audioPIDpresent=true;
\r
528 if (! audioPIDpresent) {
\r
530 if (channelinfo.numAPids>0) {
\r
532 while (j<channelinfo.numAPids && !found) {
\r
533 if (Audio::getInstance()->streamTypeSupported(channelinfo.apids[j].type)) {
\r
535 setAID(channelinfo.apids[j].pid,0,channelinfo.apids[j].type);
\r
541 if (channelinfo.numDPids>0 && !found) {
\r
543 while (j<channelinfo.numDPids && !found) {
\r
544 if (Audio::getInstance()->streamTypeSupported(channelinfo.dpids[j].type)) {
\r
546 setAID(channelinfo.dpids[j].pid,1,channelinfo.dpids[j].type);
\r
553 channelinfo=new_channelinfo;
\r
556 havechannelinfo=true;
\r
570 parseTSPacketDetails(vPacket);
\r
573 rc = submitPacket(vPacket);
\r
583 parseTSPacketDetails(aPacket);
\r
586 rc = submitPacket(aPacket);
\r
596 parseTSPacketDetails(subPacket);
\r
599 rc = submitPacket(subPacket);
\r
603 if (isteletextdecoded && pid == tID)
\r
609 parseTSPacketDetails(tPacket);
\r
612 rc = submitPacket(tPacket);
\r
616 if (rc == 0) return 0;
\r
621 vPacket.init(PESTYPE_VID0);
\r
622 buf += 6; datalen -= 6;
\r
629 aPacket.init(PESTYPE_PRIVATE_1, PESTYPE_SUBSTREAM_AC30);
\r
633 aPacket.init(PESTYPE_AUD0);
\r
636 buf += 6; datalen -= 6;
\r
640 subPacket.init(PESTYPE_PRIVATE_1);
\r
641 subLength = (buf[4] << 8) + buf[5];
\r
642 buf += 6; datalen -= 6;
\r
644 if (isteletextdecoded && pid == tID)
\r
646 tPacket.init(PESTYPE_PRIVATE_1,PESTYPE_SUBSTREAM_TELETEXTMAX);
\r
647 buf += 6; datalen -= 6;
\r
651 if ( (pid == vID && vActive) ||
\r
652 (pid == aID && aActive) ||
\r
653 (pid == tID && tActive) ||
\r
654 (pid == subID && subActive) )
\r
656 PESPacket* packet = NULL;
\r
657 if (pid == vID) packet = &vPacket;
\r
658 if (pid == aID) packet = &aPacket;
\r
659 if (pid == subID) {
\r
660 packet = &subPacket;
\r
662 if (pid == tID) packet = &tPacket;
\r
663 if (packet != NULL)
\r
665 if (packet->write(buf, datalen) == 0)
\r
666 { // Writing to packet failed. It has overflowed.
\r
669 parseTSPacketDetails(*packet);
\r
672 if (submitPacket(*packet) == 0) return 0;
\r
674 packet->truncate();
\r
675 packet->write((UCHAR*)"\200\000\000", 3);
\r
676 packet->write(buf, datalen);
\r
681 if (pid == subID && subActive && subPacket.getLength() == subLength)
\r
683 parsePacketDetails(subPacket);
\r
684 Log::getInstance()->log("DEMUXERTS", Log::DEBUG, "SUBMITTING A SUBTITLE PACKET %d %x", subLength, subPacket.getSubstream());
\r
685 submitPacket(subPacket);
\r
692 ULONG DemuxerTS::getPacketNum()
\r
694 return packetNumber;
\r
697 ULONG DemuxerTS::getFrameNumFromPTS(ULLONG pts)
\r
699 ULLONG difference = (1LL<<33);
\r
700 ULONG ref_frame = 0;
\r
701 int total = 0, actual = 0;
\r
702 if (pts==0) return 0; //we are in startup
\r
703 pts_map_mutex.Lock();
\r
704 PTSMap::iterator iter = pts_map.begin();
\r
705 while (iter != pts_map.end())
\r
708 //Log::getInstance()->log("DemuxerTS", Log::DEBUG, "getFrameNumfromPTS pts1 %lld pts2 %lld", pts, iter->pts);
\r
709 if (PTSDifference(iter->pts, pts) < PTS_ALLOWANCE)
\r
712 ref_frame = iter->frame;
\r
716 ULLONG newdiff = PTSDifference(pts, iter->pts);
\r
717 if (newdiff < difference)
\r
719 difference = newdiff;
\r
720 ref_frame = iter->frame;
\r
725 if (total > 1 && actual == 1) // We are using the most recent PTS ref.
\r
726 { // Delete the rest.
\r
727 iter = pts_map.begin(); iter++;
\r
728 pts_map.erase(iter, pts_map.end());
\r
730 pts_map_mutex.Unlock();
\r
732 Log::getInstance()->log("DemuxerTS", Log::DEBUG, "getFrameNumfromPTS pts %lld deleted %d difference %lld", pts, total,difference);
\r
734 if (difference == (1LL<<33))
\r
735 return 0; // We cannot make sense of the pts
\r
737 return ref_frame + difference * fps / 90000;
\r
741 void DemuxerTS::parseTSPacketDetails(PESPacket &packet) // Only important stuff for paket counting reminas
\r
743 parsePacketDetails(packet);
\r
744 if (packetCounting && packet.getPacketType() >= PESTYPE_AUD0 &&
\r
745 packet.getPacketType() <= PESTYPE_AUDMAX)
\r
749 UINT pictsinpacket=packet.countPictureHeaders(h264);
\r
752 /* if (!doubledframerate)
\r
754 numpicts=pictsinpacket;
\r
758 numpicts=(pictsinpacket+framereserve)>>1;
\r
759 framereserve=(pictsinpacket+framereserve)%2;
\r
763 if (frameCounting && numpicts &&
\r
764 packet.getPacketType() >= PESTYPE_VID0 &&
\r
765 packet.getPacketType() <= PESTYPE_VIDMAX)
\r
767 frameNumber+=numpicts;
\r
768 ULONG frame_num = frameNumber;
\r
769 if ((h264 || packet.findSeqHeader(h264) > 1) && packet.hasPTS())
\r
772 pts_map_mutex.Lock();
\r
773 if (pts_map.empty())
\r
775 me.pts = packet.getPTS();
\r
776 me.frame = frame_num;
\r
777 pts_map_mutex.Unlock();
\r
778 pts_map_mutex.Lock();
\r
779 pts_map.push_front(me);
\r
781 me = pts_map.front();
\r
782 pts_map_mutex.Unlock();
\r
784 //UINT fps = Video::getInstance()->getFPS();
\r
786 // if (doubledframerate) tfps*=2.;
\r
787 ULLONG pts_expected = me.pts + 90000*((int)(((double)(frame_num - me.frame)) / tfps));
\r
788 while (pts_expected > (1LL<<33)) pts_expected -= (1LL<<33);
\r
790 // this was a workaround for a vdr bug, now fixed, for older recordings deleter the index file
\r
793 if (!doubledframerate
\r
794 &&(abs((long long)PTSDistance(pts_expected, packet.getPTS())-1800) <= 1
\r
795 || abs((long long)PTSDistance(pts_expected, packet.getPTS())-1501) <= 1)) {
\r
796 doubledframerate=true; //Detected p50 or p60
\r
799 if (PTSDistance(pts_expected, packet.getPTS()) > PTS_JUMP_MARGIN) // PTS jump!
\r
801 me.pts = packet.getPTS();
\r
802 me.frame = frame_num;
\r
803 pts_map_mutex.Lock();
\r
804 pts_map.push_front(me);
\r
805 pts_map_mutex.Unlock();
\r
812 bool DemuxerTS::scanForVideo(UCHAR* buf, UINT len, bool &ish264)
\r
816 while (len >= TS_SIZE)
\r
818 if (*buf != TS_SIG) {buf++;len--; continue;}
\r
820 //Pattern scanning won't work for ts
\r
823 int datalen = TS_SIZE - 4;
\r
824 int pid = ( (buf[1] & 0x1F) << 8 ) | buf[2];
\r
825 UCHAR payload = buf[1] & 0x40;
\r
827 if (buf[3] & 0x20) // Adaptation field is present
\r
828 datalen -= (buf[4] + 1);
\r
830 UCHAR* curbuf =buf+ (TS_SIZE - datalen);
\r
833 if (pid == 0x00) {//PAT, only take first program number, ignore the rest
\r
834 int pmtpid = (*(curbuf+11)<< 8) | *(curbuf+12);
\r
835 if ((pmtpid >> 13) != 0x07)
\r
837 Log::getInstance()->log("DemuxerTS", Log::DEBUG, "PMTPID=%02x %02x TRAILING 111 not set but %x", *(curbuf+11),*(curbuf+12), (pmtpid >> 13));
\r
841 pmtpid = pmtpid & 0x1FFF; //clear upper 3 bits
\r
843 Log::getInstance()->log("DemuxerTS", Log::DEBUG, "PMT pid%02x",pmtpid );
\r
846 } else if (pid == pmtpidy) { //PMT
\r
847 int sectionlength = ((*(curbuf+2) << 8) & 0x0F ) | *(buf+3);
\r
848 //sectionlength += 4; //include header but subtract crc in the end...
\r
849 int p = 13; //skip fixed part of pmt
\r
850 while ( p < sectionlength) {
\r
851 int streamtype = *(curbuf+p);
\r
853 int foundpid = (*(curbuf+p)<< 8) | *(curbuf+p+1);
\r
854 p += 2; //skip ES Pid
\r
855 int eslength = ((*(curbuf+p) << 8) & 0x0F ) | *(curbuf+p+1);
\r
856 p += 2; //skip ES length
\r
857 if ((foundpid >> 13) != 0x07)
\r
859 Log::getInstance()->log("DemuxerTS", Log::DEBUG, "FOUNDPID=%02x %02x TRAILING 111 not set but %x", *(buf+p),*(buf+p+1), (foundpid >> 13));
\r
863 foundpid = foundpid & 0x1FFF; //clear upper 3 bits
\r
864 // int pos=0; UNUSED?
\r
865 Log::getInstance()->log("DemuxerTS", Log::DEBUG, "Pid found %02x type %02x",foundpid ,streamtype);
\r
866 if (streamtype==1 || streamtype ==2) {
\r
868 Log::getInstance()->log("DemuxerTS", Log::DEBUG, "Found Mpeg2 Video");
\r
871 if (streamtype==0x1b) {
\r
873 Log::getInstance()->log("DemuxerTS", Log::DEBUG, "Found h264 Video");
\r
877 p += eslength; //skip es descriptor
\r
890 UINT DemuxerTS::stripAudio(UCHAR* buf, UINT len) //it has to be adapted
\r
892 //This function strips all TS Headers and non video payload
\r
895 PESPacket destpaket;
\r
897 while (readpos < len ) {
\r
898 if (buf[readpos] != TS_SIG) {readpos++; continue;}
\r
899 UINT oldreadpos=readpos;
\r
901 int datalen = TS_SIZE - 4;
\r
902 int pid = ( (buf[readpos+1] & 0x1F) << 8 ) | buf[readpos+2];
\r
903 UCHAR payload = buf[readpos+1] & 0x40;
\r
904 if (buf[readpos+3] & 0x20) { // Adaptation field is present
\r
905 datalen -= (buf[readpos+4] + 1);
\r
907 if (datalen < 0) {// Error in stream TODO log this
\r
910 if (datalen == 0) {// Null packet
\r
911 readpos=oldreadpos+TS_SIZE;
\r
914 readpos += (TS_SIZE - datalen);
\r
915 UINT towrite=min(datalen,len-readpos);
\r
919 parsePacketDetails(destpaket);
\r
920 memcpy(buf+writepos,destpaket.getData(),destpaket.getSize());
\r
921 writepos+=destpaket.getSize();
\r
923 destpaket.init(PESTYPE_VID0);
\r
924 readpos += 6; towrite -= 6;
\r
929 if (!destpaket.write(buf+readpos,towrite)) {
\r
930 parsePacketDetails(destpaket);
\r
931 memcpy(buf+writepos,destpaket.getData(),destpaket.getSize());
\r
932 writepos+=destpaket.getSize();
\r
933 destpaket.truncate();
\r
934 destpaket.write((UCHAR*)"\200\000\000", 3);
\r
935 destpaket.write(buf+readpos,towrite);
\r
943 readpos=oldreadpos+TS_SIZE;
\r
945 parsePacketDetails(destpaket);
\r
946 memcpy(buf+writepos,destpaket.getData(),destpaket.getSize());
\r
947 writepos+=destpaket.getSize();
\r