diff --git a/src/input/input_ebml.cpp b/src/input/input_ebml.cpp index 8401c0c8..5b44caf4 100644 --- a/src/input/input_ebml.cpp +++ b/src/input/input_ebml.cpp @@ -40,6 +40,7 @@ namespace Mist{ lastClusterBPos = 0; lastClusterTime = 0; bufferedPacks = 0; + wantBlocks = true; } std::string ASStoSRT(const char * ptr, uint32_t len){ @@ -349,6 +350,15 @@ namespace Mist{ bool isVideo = (Trk.type == "video"); bool isAudio = (Trk.type == "audio"); bool isASS = (Trk.codec == "subtitle" && Trk.init.size()); + //If this is a new video keyframe, flush the corresponding trackPredictor + if (isVideo && B.isKeyframe()){ + while (TP.hasPackets(true)){ + packetData &C = TP.getPacketData(true); + myMeta.update(C.time, C.offset, C.track, C.dsize, C.bpos, C.key); + TP.remove(); + } + TP.flush(); + } for (uint64_t frameNo = 0; frameNo < B.getFrameCount(); ++frameNo){ if (frameNo){ if (Trk.codec == "AAC"){ @@ -463,28 +473,32 @@ namespace Mist{ } } EBML::Block B; - do{ - if (!readElement()){ - // Make sure we empty our buffer first - if (bufferedPacks && packBuf.size()){ - for (std::map::iterator it = packBuf.begin(); - it != packBuf.end(); ++it){ - trackPredictor &TP = it->second; - if (TP.hasPackets(true)){ - packetData &C = TP.getPacketData(myMeta.tracks[it->first].type == "video"); - fillPacket(C); - TP.remove(); - --bufferedPacks; - return; + if (wantBlocks){ + do{ + if (!readElement()){ + // Make sure we empty our buffer first + if (bufferedPacks && packBuf.size()){ + for (std::map::iterator it = packBuf.begin(); + it != packBuf.end(); ++it){ + trackPredictor &TP = it->second; + if (TP.hasPackets(true)){ + packetData &C = TP.getPacketData(myMeta.tracks[it->first].type == "video"); + fillPacket(C); + TP.remove(); + --bufferedPacks; + return; + } } } + // No more buffer? Set to empty + thisPacket.null(); + return; } - // No more buffer? Set to empty - thisPacket.null(); - return; - } + B = EBML::Block(ptr); + }while (!B || B.getType() != EBML::ELEM_BLOCK || !selectedTracks.count(B.getTrackNum())); + }else{ B = EBML::Block(ptr); - }while (!B || B.getType() != EBML::ELEM_BLOCK || !selectedTracks.count(B.getTrackNum())); + } uint64_t tNum = B.getTrackNum(); uint64_t newTime = lastClusterTime + B.getTimecode(); @@ -493,6 +507,24 @@ namespace Mist{ bool isVideo = (Trk.type == "video"); bool isAudio = (Trk.type == "audio"); bool isASS = (Trk.codec == "subtitle" && Trk.init.size()); + + //If this is a new video keyframe, flush the corresponding trackPredictor + if (isVideo && B.isKeyframe() && bufferedPacks){ + if (TP.hasPackets(true)){ + wantBlocks = false; + packetData &C = TP.getPacketData(true); + fillPacket(C); + TP.remove(); + --bufferedPacks; + return; + } + } + if (isVideo && B.isKeyframe()){ + TP.flush(); + } + wantBlocks = true; + + for (uint64_t frameNo = 0; frameNo < B.getFrameCount(); ++frameNo){ if (frameNo){ if (Trk.codec == "AAC"){ @@ -531,6 +563,7 @@ namespace Mist{ } void InputEBML::seek(int seekTime){ + wantBlocks = true; packBuf.clear(); bufferedPacks = 0; uint64_t mainTrack = getMainSelectedTrack(); @@ -541,17 +574,6 @@ namespace Mist{ uint64_t partCount = 0; for (unsigned int i = 0; i < Trk.keys.size(); i++){ if (Trk.keys[i].getTime() > seekTime){ - if (i > 1){ - partCount -= Trk.keys[i-1].getParts() + Trk.keys[i-2].getParts(); - uint64_t partEnd = partCount + Trk.keys[i-2].getParts(); - uint64_t partTime = Trk.keys[i-2].getTime(); - for (uint64_t prt = partCount; prt < partEnd; ++prt){ - INSANE_MSG("Replay part %llu, timestamp: %llu+%llu", prt, partTime, Trk.parts[prt].getOffset()); - packBuf[mainTrack].add(partTime, Trk.parts[prt].getOffset(), mainTrack, 0, 0, false, isVideo, (void *)0); - packBuf[mainTrack].remove(); - partTime += Trk.parts[prt].getDuration(); - } - } break; } partCount += Trk.keys[i].getParts(); diff --git a/src/input/input_ebml.h b/src/input/input_ebml.h index 057bcd8c..4d4af639 100644 --- a/src/input/input_ebml.h +++ b/src/input/input_ebml.h @@ -40,15 +40,13 @@ namespace Mist{ class trackPredictor{ public: packetData pkts[PKT_COUNT]; + uint64_t frameOffset; uint16_t smallestFrame; uint64_t lastTime; uint64_t ctr; uint64_t rem; trackPredictor(){ - smallestFrame = 0; - lastTime = 0; - ctr = 0; - rem = 0; + flush(); } bool hasPackets(bool finished = false){ if (finished){ @@ -57,6 +55,14 @@ namespace Mist{ return (ctr - rem > 12); } } + /// Clears all internal values, for reuse as-new. + void flush(){ + frameOffset = 0; + smallestFrame = 0; + lastTime = 0; + ctr = 0; + rem = 0; + } packetData & getPacketData(bool mustCalcOffsets){ frameOffsetKnown = true; //grab the next packet to output @@ -74,13 +80,20 @@ namespace Mist{ //we assume it's just time stamp drift due to lack of precision. p.time = (lastTime + smallestFrame); }else{ - p.offset = maxEBMLFrameOffset; + p.time -= frameOffset?frameOffset + smallestFrame:0; + p.offset = maxEBMLFrameOffset + frameOffset + smallestFrame; } } lastTime = p.time; + DONTEVEN_MSG("Outputting%s %llu + %llu, offset %llu (%llu -> %llu), display at %llu", (p.key?" (KEY)":""), p.time, frameOffset, p.offset, rem, rem % PKT_COUNT, p.time+p.offset); return p; } void add(uint64_t packTime, uint64_t packOffset, uint64_t packTrack, uint64_t packDataSize, uint64_t packBytePos, bool isKeyframe, bool isVideo, void * dataPtr = 0){ + //If no packets have been removed yet and there is more than one packet, calculate frameOffset + if (!rem && ctr && packTime < pkts[0].time){ + frameOffset = pkts[0].time - packTime; + INSANE_MSG("Setting frameOffset to %llu", frameOffset); + } if (isVideo && ctr && ctr >= rem){ int32_t currOffset = packTime - pkts[(ctr-1)%PKT_COUNT].time; if (currOffset < 0){currOffset *= -1;} @@ -131,6 +144,7 @@ namespace Mist{ bool openStreamSource(){return true;} bool needHeader(){return needsLock() && !readExistingHeader();} double timeScale; + bool wantBlocks; }; }