diff --git a/lib/nal.cpp b/lib/nal.cpp index 25ae8d7f..f4d75d0d 100644 --- a/lib/nal.cpp +++ b/lib/nal.cpp @@ -69,6 +69,56 @@ namespace nalu { return dataSize; } + const char* nalEndPosition(const char * data, uint32_t dataSize){ + while(dataSize > 0 && memcmp(data+dataSize-1, "\000",1) == 0 ){ + dataSize--; + } + + return data+dataSize; + } + + ///scan data stream for startcode. return pointer to location when found, NULL otherwise + void scanAnnexB(const char * data, uint32_t dataSize, const char *& packetPointer){ + int offset = 0; + + while(offset+2 < dataSize){ + const char * begin = data + offset; +// int t = ((((int*)begin)[0]) >> 8) & 0x00FFFFFF; + int t = (int)((begin[0] << 8)|((begin[1]) << 8)|(begin[2])); + //int t = (int)((begin[0]|begin[1]) << 1)|(begin[2]); + //search for startcode + + //if(memcmp(begin, "\000\000\001",3) != 0){ + if(t != 1){ + + //if((t & 0x0000FF != 0 )) + if((int)begin[2] != 0 ) //XX1 + { + offset +=3; + }else if(((int)begin[1] == 1) && ((int)begin[2] ==0)){ //X10 + offset +=2; + }else{ + offset++; //[X00]? incr with 1 because the startcode could be one at 1byte offset. + } +/* + if(t != 0 ) + { + offset += 3; + }else{ + offset++; + } +*/ + +// offset++; + }else{ + packetPointer = begin; + return; + } + } + + packetPointer = NULL; + } + unsigned long fromAnnexB(const char * data, unsigned long dataSize, char *& result){ const char * lastCheck = data + dataSize - 3; if (!result){ diff --git a/lib/nal.h b/lib/nal.h index e1ad63ff..28a3e246 100644 --- a/lib/nal.h +++ b/lib/nal.h @@ -16,4 +16,6 @@ namespace nalu { unsigned long toAnnexB(const char * data, unsigned long dataSize, char *& result); unsigned long fromAnnexB(const char * data, unsigned long dataSize, char *& result); + void scanAnnexB(const char * data, uint32_t dataSize, const char *& packetPointer); + const char* nalEndPosition(const char * data, uint32_t dataSize); } diff --git a/lib/ts_packet.cpp b/lib/ts_packet.cpp index bd77c471..5d528348 100644 --- a/lib/ts_packet.cpp +++ b/lib/ts_packet.cpp @@ -48,6 +48,26 @@ namespace TS { return true; } + bool Packet::FromStream(std::istream & data) + { + long long int bPos = data.tellg(); + if(!data.read (strBuf,188)) + { + HIGH_MSG("failed to read 188 bytes"); + return false; + } + + if(strBuf[0] != 0x47) + { + HIGH_MSG("Failed to read a good packet on pos %lld", bPos); + return false; + } + + pos = 188; + return true; + } + + ///This funtion fills a Packet from ///a char array. It fills the content with ///the first 188 characters of a char array diff --git a/lib/ts_packet.h b/lib/ts_packet.h index 633afe92..fad0f231 100644 --- a/lib/ts_packet.h +++ b/lib/ts_packet.h @@ -12,6 +12,7 @@ #include #include "dtsc.h" #include "checksum.h" +#include /// Holds all TS processing related code. namespace TS { @@ -26,6 +27,7 @@ namespace TS { ~Packet(); bool FromPointer(const char * data); bool FromFile(FILE * data); + bool FromStream(std::istream & data); //Base properties void setPID(int NewPID);