From 891696df83f67801f050325ea4d60d57d16c26f0 Mon Sep 17 00:00:00 2001 From: Thulinma Date: Fri, 17 Nov 2017 15:48:07 +0100 Subject: [PATCH] Added MP4 subtitle input durations --- src/input/input_mp4.cpp | 31 ++++++++++++++++++++++++------- src/input/input_mp4.h | 5 +++-- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/input/input_mp4.cpp b/src/input/input_mp4.cpp index 3399372f..69a49706 100644 --- a/src/input/input_mp4.cpp +++ b/src/input/input_mp4.cpp @@ -60,7 +60,7 @@ namespace Mist{ hasCTTS = cttsBox.isType("ctts"); } - void mp4TrackHeader::getPart(uint64_t index, uint64_t & offset, uint32_t & size, uint64_t & timestamp, int32_t & timeOffset){ + void mp4TrackHeader::getPart(uint64_t index, uint64_t & offset, uint32_t & size, uint64_t & timestamp, int32_t & timeOffset, uint64_t & duration){ if (index < sampleIndex){ sampleIndex = 0; stscStart = 0; @@ -114,6 +114,24 @@ namespace Mist{ ++deltaIndex; } timestamp = ((deltaTotal + ((index-deltaPos) * tmpSTTS.sampleDelta))*1000) / timeScale; + duration = 0; + + { + uint64_t tmpIndex = deltaIndex; + uint64_t tmpPos = deltaPos; + uint64_t tmpTotal = deltaTotal; + while (tmpIndex < sttsCount){ + tmpSTTS = sttsBox.getSTTSEntry(tmpIndex); + if ((index+1 - tmpPos) < tmpSTTS.sampleCount){ + duration = (((tmpTotal + ((index+1-tmpPos) * tmpSTTS.sampleDelta))*1000) / timeScale) - timestamp; + break; + } + tmpTotal += tmpSTTS.sampleCount * tmpSTTS.sampleDelta; + tmpPos += tmpSTTS.sampleCount; + ++tmpIndex; + } + } + initialised = true; if (index < offsetPos){ @@ -486,11 +504,10 @@ namespace Mist{ thisPack["trackid"] = (long long)curPart.trackID; thisPack["bpos"] = (long long)curPart.bpos; //(long long)fileSource.tellg(); thisPack["data"] = std::string(data+2,txtLen); -// thisPack["index"] = index; thisPack["time"] = (long long)curPart.time; - thisPack["duration"] = 1000; - - // thisPack["time"] = (long long)timestamp; + if (curPart.duration){ + thisPack["duration"] = (long long)curPart.duration; + } thisPack["keyframe"] = true; // Write the json value to lastpack std::string tmpStr = thisPack.toNetPacked(); @@ -506,7 +523,7 @@ namespace Mist{ //get the next part for this track curPart.index ++; if (curPart.index < headerData[curPart.trackID].size()){ - headerData[curPart.trackID].getPart(curPart.index, curPart.bpos, curPart.size, curPart.time, curPart.offset); + headerData[curPart.trackID].getPart(curPart.index, curPart.bpos, curPart.size, curPart.time, curPart.offset, curPart.duration); curPositions.insert(curPart); } } @@ -525,7 +542,7 @@ namespace Mist{ //for all indexes in those tracks for (unsigned int i = 0; i < headerData[*it].size(); i++){ //if time > seekTime - headerData[*it].getPart(i, addPart.bpos, addPart.size, addPart.time, addPart.offset); + headerData[*it].getPart(i, addPart.bpos, addPart.size, addPart.time, addPart.offset, addPart.duration); //check for keyframe time in myMeta and update nextKeyframe // if (myMeta.tracks[*it].keys[(nextKeyframe[*it])].getTime() < addPart.time){ diff --git a/src/input/input_mp4.h b/src/input/input_mp4.h index f415eb61..32468adb 100644 --- a/src/input/input_mp4.h +++ b/src/input/input_mp4.h @@ -5,7 +5,7 @@ namespace Mist { class mp4PartTime{ public: - mp4PartTime() : time(0), offset(0), trackID(0), bpos(0), size(0), index(0) {} + mp4PartTime() : time(0), offset(0), trackID(0), bpos(0), size(0), index(0), duration(0) {} bool operator < (const mp4PartTime & rhs) const { if (time < rhs.time){ return true; @@ -19,6 +19,7 @@ namespace Mist { return (trackID == rhs.trackID && bpos < rhs.bpos); } uint64_t time; + uint64_t duration; int32_t offset; size_t trackID; uint64_t bpos; @@ -60,7 +61,7 @@ namespace Mist { MP4::CTTS cttsBox; MP4::STSC stscBox; uint64_t timeScale; - void getPart(uint64_t index, uint64_t & offset, uint32_t & size, uint64_t & timestamp, int32_t & timeOffset); + void getPart(uint64_t index, uint64_t & offset, uint32_t & size, uint64_t & timestamp, int32_t & timeOffset, uint64_t & duration); uint64_t size(); private: bool initialised;