From e8db89319afbe1a755e04bf3a87ad49bc2e8c2e3 Mon Sep 17 00:00:00 2001 From: Thulinma Date: Wed, 26 Jul 2017 17:05:00 +0200 Subject: [PATCH 1/3] DTSC getInt is now uint64_t instead of int --- lib/dtsc.h | 4 ++-- lib/dtscmeta.cpp | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/dtsc.h b/lib/dtsc.h index 84517c28..b1404431 100644 --- a/lib/dtsc.h +++ b/lib/dtsc.h @@ -120,8 +120,8 @@ namespace DTSC { void genericFill(long long packTime, long long packOffset, long long packTrack, const char * packData, long long packDataSize, uint64_t packBytePos, bool isKeyframe); void getString(const char * identifier, char *& result, unsigned int & len) const; void getString(const char * identifier, std::string & result) const; - void getInt(const char * identifier, int & result) const; - int getInt(const char * identifier) const; + void getInt(const char * identifier, uint64_t & result) const; + uint64_t getInt(const char * identifier) const; void getFlag(const char * identifier, bool & result) const; bool getFlag(const char * identifier) const; bool hasMember(const char * identifier) const; diff --git a/lib/dtscmeta.cpp b/lib/dtscmeta.cpp index 985fc732..9aa90975 100644 --- a/lib/dtscmeta.cpp +++ b/lib/dtscmeta.cpp @@ -415,15 +415,15 @@ namespace DTSC { ///\brief Retrieves a single parameter as an integer ///\param identifier The name of the parameter ///\param result The result is stored in this integer - void Packet::getInt(const char * identifier, int & result) const { + void Packet::getInt(const char * identifier, uint64_t & result) const { result = getScan().getMember(identifier).asInt(); } ///\brief Retrieves a single parameter as an integer ///\param identifier The name of the parameter ///\result The requested parameter as an integer - int Packet::getInt(const char * identifier) const { - int result; + uint64_t Packet::getInt(const char * identifier) const { + uint64_t result; getInt(identifier, result); return result; } @@ -432,7 +432,7 @@ namespace DTSC { ///\param identifier The name of the parameter ///\param result The result is stored in this boolean void Packet::getFlag(const char * identifier, bool & result) const { - int result_; + uint64_t result_; getInt(identifier, result_); result = (bool)result_; } From 9be07e5ecbb637589f3c3565a8446815891a6fc4 Mon Sep 17 00:00:00 2001 From: Thulinma Date: Wed, 26 Jul 2017 17:05:37 +0200 Subject: [PATCH 2/3] TS::Packet FromFile 64-bit support and garbage data recovery improvements --- lib/ts_packet.cpp | 39 +++++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/lib/ts_packet.cpp b/lib/ts_packet.cpp index 5d528348..6a8c2f19 100644 --- a/lib/ts_packet.cpp +++ b/lib/ts_packet.cpp @@ -1,6 +1,7 @@ /// \file ts_packet.cpp /// Holds all code for the TS namespace. +#include "util.h" #include #include #include @@ -35,17 +36,35 @@ namespace TS { /// It fills the content with the next 188 bytes int he file. /// \param Data The data to be read into the packet. /// \return true if it was possible to read in a full packet, false otherwise. - bool Packet::FromFile(FILE * data) { - long long int bPos = ftell(data); - if (!fread((void *)strBuf, 188, 1, data)) { - return false; + bool Packet::FromFile(FILE * data){ + uint64_t bPos = Util::ftell(data); + uint16_t retries = 0; + while (retries < 256){ + if (!fread((void *)strBuf, 188, 1, data)) { + if (!feof(data)){ + FAIL_MSG("Could not read 188 bytes from file! %s", strerror(errno)); + } + return false; + } + if (strBuf[0] == 0x47){ + pos=188; + return true; + } + for (uint8_t i = 1; i < 188; ++i){ + if (strBuf[i] == 0x47){ + INFO_MSG("Shifting %u bytes", i); + memmove((void*)strBuf, (void*)(strBuf+i), 188-i); + if (!fread((void *)strBuf, i, 1, data)) { + return false; + } + pos=188; + return true; + } + } + INFO_MSG("Skipping invalid TS packet..."); } - if (strBuf[0] != 0x47){ - HIGH_MSG("Failed to read a good packet on pos %lld", bPos); - return false; - } - pos=188; - return true; + FAIL_MSG("Failed to read a good packet @ %lld bytes", bPos); + return false; } bool Packet::FromStream(std::istream & data) From cba764d8eb71c6ea4c16fd60f85023adfae28bb6 Mon Sep 17 00:00:00 2001 From: Thulinma Date: Thu, 27 Jul 2017 15:18:50 +0200 Subject: [PATCH 3/3] scanAnnexB speed optimize --- lib/nal.cpp | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/lib/nal.cpp b/lib/nal.cpp index 379a03f5..d8864b84 100644 --- a/lib/nal.cpp +++ b/lib/nal.cpp @@ -79,21 +79,24 @@ namespace nalu { ///Scan data for Annex B start code. Returns pointer to it when found, null otherwise. const char * scanAnnexB(const char * data, uint32_t dataSize){ - int offset = 0; - while(offset+2 < dataSize){ - const char * begin = data + offset; - bool t = (begin[2] == 1 && !begin[0] && !begin[1]); - if(!t){ - if (begin[2]){//skip three bytes if the last one isn't zero - offset +=3; - }else if (begin[1]){//skip two bytes if the second one isn't zero - offset +=2; - }else{//All other cases, skip one byte - offset++; - } - }else{ - return begin; + char * offset = (char*)data; + const char * maxData = data + dataSize - 2; + while(offset < maxData){ + if (offset[2] > 1){ + //We have no zero in the third byte, so we need to skip at least 3 bytes forward + offset += 3; + continue; } + if (!offset[2]){ + //We skip forward 1 or 2 bytes depending on contents of the second byte + offset += (offset[1]?2:1); + continue; + } + if (!offset[0] && !offset[1]){ + return offset; + } + //We have no zero in the third byte, so we need to skip at least 3 bytes forward + offset += 3; } return 0; }