From a6b993a500a0d5b688c3c3021e3fccb4b01fdeca Mon Sep 17 00:00:00 2001 From: Steve Lhomme Date: Fri, 1 Mar 2024 11:31:59 +0100 Subject: [PATCH 1/5] use FindChild instead of calls to FindElt/FindFirstElt --- src/KaxBlock.cpp | 14 +++++++------- src/KaxCluster.cpp | 4 ++-- src/KaxCues.cpp | 2 +- src/KaxCuesData.cpp | 22 +++++++++++----------- src/KaxSeekHead.cpp | 10 +++++----- test/mux/test8.cpp | 6 +++--- 6 files changed, 29 insertions(+), 29 deletions(-) diff --git a/src/KaxBlock.cpp b/src/KaxBlock.cpp index b03ce312..2cc3a074 100644 --- a/src/KaxBlock.cpp +++ b/src/KaxBlock.cpp @@ -830,13 +830,13 @@ bool KaxBlockGroup::AddFrame(const KaxTrackEntry & track, std::uint64_t timestam std::uint64_t KaxBlockGroup::GlobalTimestamp() const { assert(ParentCluster); // impossible otherwise - auto MyBlock = static_cast(this->FindElt(EBML_INFO(KaxBlock))); + auto MyBlock = FindChild(*this); return MyBlock->GlobalTimestamp(); } std::uint16_t KaxBlockGroup::TrackNumber() const { - auto MyBlock = static_cast(this->FindElt(EBML_INFO(KaxBlock))); + auto MyBlock = FindChild(*this); return MyBlock->TrackNum(); } @@ -855,7 +855,7 @@ std::uint64_t KaxInternalBlock::ClusterPosition() const unsigned int KaxBlockGroup::ReferenceCount() const { unsigned int Result = 0; - auto MyBlockAdds = static_cast(FindFirstElt(EBML_INFO(KaxReferenceBlock))); + auto MyBlockAdds = FindChild(*this); if (MyBlockAdds) { Result++; while ((MyBlockAdds = static_cast(FindNextElt(*MyBlockAdds)))) { @@ -867,7 +867,7 @@ unsigned int KaxBlockGroup::ReferenceCount() const const KaxReferenceBlock & KaxBlockGroup::Reference(unsigned int Index) const { - auto MyBlockAdds = static_cast(FindFirstElt(EBML_INFO(KaxReferenceBlock))); + auto MyBlockAdds = FindChild(*this); assert(MyBlockAdds); // call of a non existing reference while (Index != 0) { @@ -880,7 +880,7 @@ const KaxReferenceBlock & KaxBlockGroup::Reference(unsigned int Index) const void KaxBlockGroup::ReleaseFrames() { - auto MyBlock = static_cast(this->FindElt(EBML_INFO(KaxBlock))); + auto MyBlock = FindChild(*this); MyBlock->ReleaseFrames(); } @@ -900,13 +900,13 @@ void KaxBlockGroup::SetBlockDuration(std::uint64_t TimeLength) { assert(ParentTrack); const std::int64_t scale = ParentTrack->GlobalTimestampScale(); - const auto myDuration = static_cast(FindFirstElt(EBML_INFO(KaxBlockDuration), true)); + const auto myDuration = FindChild(*this); myDuration->SetValue(TimeLength / static_cast(scale)); } bool KaxBlockGroup::GetBlockDuration(std::uint64_t &TheTimestamp) const { - const auto myDuration = static_cast(FindElt(EBML_INFO(KaxBlockDuration))); + const auto myDuration = FindChild(*this); if (!myDuration) { return false; } diff --git a/src/KaxCluster.cpp b/src/KaxCluster.cpp index 5b802bf4..c5f2c44f 100644 --- a/src/KaxCluster.cpp +++ b/src/KaxCluster.cpp @@ -118,7 +118,7 @@ filepos_t KaxCluster::Render(IOCallback & output, KaxCues & CueToUpdate, const S filepos_t Result = 0; // update the timestamp of the Cluster before writing - auto ClusterTimestamp = static_cast(this->FindElt(EBML_INFO(KaxClusterTimestamp))); + auto ClusterTimestamp = FindChild(*this); ClusterTimestamp->SetValue(GlobalTimestamp() / GlobalTimestampScale()); if (Blobs.empty()) { @@ -181,7 +181,7 @@ std::int16_t KaxCluster::GetBlockLocalTimestamp(std::uint64_t aGlobalTimestamp) std::uint64_t KaxCluster::GetBlockGlobalTimestamp(std::int16_t LocalTimestamp) { if (!bFirstFrameInside) { - auto ClusterTimestamp = static_cast(this->FindElt(EBML_INFO(KaxClusterTimestamp))); + auto ClusterTimestamp = FindChild(*this); assert (bFirstFrameInside); // use the InitTimestamp() hack for now MinTimestamp = MaxTimestamp = PreviousTimestamp = static_cast(*static_cast(ClusterTimestamp)); bFirstFrameInside = true; diff --git a/src/KaxCues.cpp b/src/KaxCues.cpp index 18c347bb..3c1bdf4a 100644 --- a/src/KaxCues.cpp +++ b/src/KaxCues.cpp @@ -97,7 +97,7 @@ const KaxCuePoint * KaxCues::GetTimestampPoint(std::uint64_t aTimestamp) const if (EbmlId(*e) == EBML_ID(KaxCuePoint)) { auto tmp = static_cast(e); // check the tile - auto aTime = static_cast(tmp->FindFirstElt(EBML_INFO(KaxCueTime))); + auto aTime = FindChild(*tmp); if (aTime) { auto _Time = static_cast(*aTime); if (_Time > aPrevTime && _Time < TimestampToLocate) { diff --git a/src/KaxCuesData.cpp b/src/KaxCuesData.cpp index 986d7ee1..fc0aa83a 100644 --- a/src/KaxCuesData.cpp +++ b/src/KaxCuesData.cpp @@ -45,7 +45,7 @@ void KaxCuePoint::PositionSet(const KaxBlockGroup & BlockReference, std::uint64_ } } - auto CodecState = static_cast(BlockReference.FindFirstElt(EBML_INFO(KaxCodecState))); + auto CodecState = FindChild(BlockReference); if (CodecState) { auto &CueCodecState = AddNewChild(NewPositions); CueCodecState.SetValue(BlockReference.GetParentCluster()->GetParentSegment()->GetRelativePosition(CodecState->GetElementPosition())); @@ -96,7 +96,7 @@ void KaxCuePoint::PositionSet(const KaxInternalBlock & BlockReference, const Kax #endif // MATROSKA_VERSION if (BlockGroup) { - const auto CodecState = static_cast(BlockGroup->FindFirstElt(EBML_INFO(KaxCodecState))); + const auto CodecState = FindChild(*BlockGroup); if (CodecState) { auto &CueCodecState = AddNewChild(NewPositions); CueCodecState.SetValue(BlockGroup->GetParentCluster()->GetParentSegment()->GetRelativePosition(CodecState->GetElementPosition())); @@ -127,11 +127,11 @@ bool KaxCuePoint::IsSmallerThan(const EbmlElement * Cmp) const auto theCmp = static_cast(Cmp); // compare timestamp - auto TimestampA = static_cast(FindElt(EBML_INFO(KaxCueTime))); + auto TimestampA = FindChild(*this); if (!TimestampA) return false; - auto TimestampB = static_cast(theCmp->FindElt(EBML_INFO(KaxCueTime))); + auto TimestampB = FindChild(*theCmp); if (!TimestampB) return false; @@ -142,11 +142,11 @@ bool KaxCuePoint::IsSmallerThan(const EbmlElement * Cmp) const return false; // compare tracks (timestamp are equal) - const auto TrackA = static_cast(FindElt(EBML_INFO(KaxCueTrack))); + const auto TrackA = FindChild(*this); if (!TrackA) return false; - const auto TrackB = static_cast(theCmp->FindElt(EBML_INFO(KaxCueTrack))); + const auto TrackB = FindChild(*theCmp); if (!TrackB) return false; @@ -161,7 +161,7 @@ bool KaxCuePoint::IsSmallerThan(const EbmlElement * Cmp) const bool KaxCuePoint::Timestamp(std::uint64_t & aTimestamp, std::uint64_t GlobalTimestampScale) const { - const auto aTime = static_cast(FindFirstElt(EBML_INFO(KaxCueTime))); + const auto aTime = FindChild(*this); if (!aTime) return false; aTimestamp = static_cast(*aTime) * GlobalTimestampScale; @@ -176,9 +176,9 @@ const KaxCueTrackPositions * KaxCuePoint::GetSeekPosition() const const KaxCueTrackPositions * result = nullptr; std::uint64_t aPosition = 0xFFFFFFFFFFFFFFFLL; // find the position of the "earlier" Cluster - auto aPoss = static_cast(FindFirstElt(EBML_INFO(KaxCueTrackPositions))); + auto aPoss = FindChild(*this); while (aPoss) { - auto aPos = static_cast(aPoss->FindFirstElt(EBML_INFO(KaxCueClusterPosition))); + auto aPos = FindChild(*aPoss); if (aPos && static_cast(*aPos) < aPosition) { aPosition = static_cast(*aPos); result = aPoss; @@ -191,7 +191,7 @@ const KaxCueTrackPositions * KaxCuePoint::GetSeekPosition() const std::uint64_t KaxCueTrackPositions::ClusterPosition() const { - const auto aPos = static_cast(FindFirstElt(EBML_INFO(KaxCueClusterPosition))); + const auto aPos = FindChild(*this); if (!aPos) return 0; @@ -200,7 +200,7 @@ std::uint64_t KaxCueTrackPositions::ClusterPosition() const std::uint16_t KaxCueTrackPositions::TrackNumber() const { - const auto aTrack = static_cast(FindFirstElt(EBML_INFO(KaxCueTrack))); + const auto aTrack = FindChild(*this); if (!aTrack) return 0; diff --git a/src/KaxSeekHead.cpp b/src/KaxSeekHead.cpp index c23cae6e..c950674e 100644 --- a/src/KaxSeekHead.cpp +++ b/src/KaxSeekHead.cpp @@ -39,7 +39,7 @@ KaxSeek * KaxSeekHead::IndexThis(const EbmlElement & aElt, const KaxSegment & Pa KaxSeek * KaxSeekHead::FindFirstOf(const EbmlCallbacks & Callbacks) const { // parse all the Entries and find the first to match the type - auto aElt = static_cast(FindFirstElt(EBML_INFO(KaxSeek))); + auto aElt = FindChild(*this); while (aElt) { auto it = std::find_if(aElt->begin(), aElt->end(), [&](auto Elt) { return (EbmlId(*Elt) == EBML_ID(KaxSeekID)); }); @@ -73,7 +73,7 @@ KaxSeek * KaxSeekHead::FindNextOf(const KaxSeek &aPrev) const std::int64_t KaxSeek::Location() const { - auto aPos = static_cast(FindFirstElt(EBML_INFO(KaxSeekPosition))); + auto aPos = FindChild(*this); if (!aPos) return 0; return static_cast(*aPos); @@ -81,7 +81,7 @@ std::int64_t KaxSeek::Location() const bool KaxSeek::IsEbmlId(const EbmlId & aId) const { - auto _Id = static_cast(FindFirstElt(EBML_INFO(KaxSeekID))); + auto _Id = FindChild(*this); if (!_Id) return false; const auto aEbmlId = EbmlId(EbmlId::FromBuffer(_Id->GetBuffer(), _Id->GetSize())); @@ -90,10 +90,10 @@ bool KaxSeek::IsEbmlId(const EbmlId & aId) const bool KaxSeek::IsEbmlId(const KaxSeek & aPoint) const { - auto _IdA = static_cast(FindFirstElt(EBML_INFO(KaxSeekID))); + auto _IdA = FindChild(*this); if (!_IdA) return false; - auto _IdB = static_cast(aPoint.FindFirstElt(EBML_INFO(KaxSeekID))); + auto _IdB = FindChild(aPoint); if (!_IdB) return false; const auto aEbmlIdA = EbmlId(EbmlId::FromBuffer(_IdA->GetBuffer(), _IdA->GetSize())); diff --git a/test/mux/test8.cpp b/test/mux/test8.cpp index c75fc210..49454f03 100644 --- a/test/mux/test8.cpp +++ b/test/mux/test8.cpp @@ -307,7 +307,7 @@ int main(int argc, char **argv) // Extract the valuable data from the Block aBlockGroup.Read(aStream, EBML_CLASS_CONTEXT(KaxBlockGroup), UpperElementLevel, ElementLevel3, bAllowDummy); - KaxBlock * DataBlock = static_cast(aBlockGroup.FindElt(EBML_INFO(KaxBlock))); + KaxBlock * DataBlock = FindChild(aBlockGroup); if (DataBlock != NULL) { // DataBlock->ReadData(aStream.I_O()); DataBlock->SetParent(*SegmentCluster); @@ -315,11 +315,11 @@ int main(int argc, char **argv) } else { printf(" A BlockGroup without a Block !!!"); } - KaxBlockDuration * BlockDuration = static_cast(aBlockGroup.FindElt(EBML_INFO(KaxBlockDuration))); + KaxBlockDuration * BlockDuration = FindChild(aBlockGroup); if (BlockDuration != NULL) { printf(" Block Duration %d scaled ticks : %ld ns\n", std::uint32_t(*BlockDuration), std::uint32_t(*BlockDuration) * TimestampScale); } - KaxReferenceBlock * RefTime = static_cast(aBlockGroup.FindElt(EBML_INFO(KaxReferenceBlock))); + KaxReferenceBlock * RefTime = FindChild(aBlockGroup); if (RefTime != NULL) { printf(" Reference frame at scaled (%d) timestamp %ld\n", std::int32_t(*RefTime), std::int32_t(std::int64_t(*RefTime) * TimestampScale)); } From fa9572d6eb558ae8c20c42a9293e0e02d13379f6 Mon Sep 17 00:00:00 2001 From: Steve Lhomme Date: Fri, 1 Mar 2024 11:15:06 +0100 Subject: [PATCH 2/5] replace FindElt calls with FindFirstElt --- matroska/KaxTracks.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/matroska/KaxTracks.h b/matroska/KaxTracks.h index 54ab6407..85fcd043 100644 --- a/matroska/KaxTracks.h +++ b/matroska/KaxTracks.h @@ -18,7 +18,7 @@ namespace libmatroska { DECLARE_MKX_MASTER(KaxTrackEntry) public: - libebml::EbmlUInteger & TrackNumber() const { return *(static_cast(FindElt(EBML_INFO(KaxTrackNumber)))); } + libebml::EbmlUInteger & TrackNumber() const { return *(static_cast(FindFirstElt(EBML_INFO(KaxTrackNumber)))); } void EnableLacing(bool bEnable = true); From f8d96b2d4fed91fbf481030156e2c44811981598 Mon Sep 17 00:00:00 2001 From: Steve Lhomme Date: Fri, 1 Mar 2024 14:59:31 +0100 Subject: [PATCH 3/5] use FindNextChild instead of calls to FindNextElt --- src/KaxBlock.cpp | 4 ++-- src/KaxCuesData.cpp | 2 +- src/KaxSeekHead.cpp | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/KaxBlock.cpp b/src/KaxBlock.cpp index 2cc3a074..80f6d529 100644 --- a/src/KaxBlock.cpp +++ b/src/KaxBlock.cpp @@ -858,7 +858,7 @@ unsigned int KaxBlockGroup::ReferenceCount() const auto MyBlockAdds = FindChild(*this); if (MyBlockAdds) { Result++; - while ((MyBlockAdds = static_cast(FindNextElt(*MyBlockAdds)))) { + while ((MyBlockAdds = FindNextChild(*this, *MyBlockAdds))) { Result++; } } @@ -871,7 +871,7 @@ const KaxReferenceBlock & KaxBlockGroup::Reference(unsigned int Index) const assert(MyBlockAdds); // call of a non existing reference while (Index != 0) { - MyBlockAdds = static_cast(FindNextElt(*MyBlockAdds)); + MyBlockAdds = FindNextChild(*this, *MyBlockAdds); assert(MyBlockAdds); Index--; } diff --git a/src/KaxCuesData.cpp b/src/KaxCuesData.cpp index fc0aa83a..ae26ae61 100644 --- a/src/KaxCuesData.cpp +++ b/src/KaxCuesData.cpp @@ -184,7 +184,7 @@ const KaxCueTrackPositions * KaxCuePoint::GetSeekPosition() const result = aPoss; } - aPoss = static_cast(FindNextElt(*aPoss)); + aPoss = FindNextChild(*this, *aPoss); } return result; } diff --git a/src/KaxSeekHead.cpp b/src/KaxSeekHead.cpp index c950674e..437e21f1 100644 --- a/src/KaxSeekHead.cpp +++ b/src/KaxSeekHead.cpp @@ -50,7 +50,7 @@ KaxSeek * KaxSeekHead::FindFirstOf(const EbmlCallbacks & Callbacks) const return aElt; } } - aElt = static_cast(FindNextElt(*aElt)); + aElt = FindNextChild(*this,*aElt); } return nullptr; From bcfc893ada046d669c4492445c4e09c7f4935a32 Mon Sep 17 00:00:00 2001 From: Steve Lhomme Date: Fri, 1 Mar 2024 15:02:37 +0100 Subject: [PATCH 4/5] use FindChild to get the KaxSeekID inside a KaxSeek This is the same algorithm used in FindFirstElt(). --- src/KaxSeekHead.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/KaxSeekHead.cpp b/src/KaxSeekHead.cpp index 437e21f1..971c2421 100644 --- a/src/KaxSeekHead.cpp +++ b/src/KaxSeekHead.cpp @@ -41,10 +41,8 @@ KaxSeek * KaxSeekHead::FindFirstOf(const EbmlCallbacks & Callbacks) const // parse all the Entries and find the first to match the type auto aElt = FindChild(*this); while (aElt) { - auto it = std::find_if(aElt->begin(), aElt->end(), [&](auto Elt) - { return (EbmlId(*Elt) == EBML_ID(KaxSeekID)); }); - if (it != aElt->end()) { - const auto aId = static_cast(*it); + const auto aId = FindChild(*aElt); + if (aId != nullptr) { const auto aEbmlId = EbmlId(EbmlId::FromBuffer(aId->GetBuffer(), aId->GetSize())); if (aEbmlId == EBML_INFO_ID(Callbacks)) { return aElt; From b2efa89da6c0c66ffe92c51c09af9477c1e71900 Mon Sep 17 00:00:00 2001 From: Steve Lhomme Date: Fri, 1 Mar 2024 15:13:53 +0100 Subject: [PATCH 5/5] use Child helpers in test00 --- test/ebml/test00.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/test/ebml/test00.cpp b/test/ebml/test00.cpp index 09cd46e4..bd537a0e 100644 --- a/test/ebml/test00.cpp +++ b/test/ebml/test00.cpp @@ -59,21 +59,21 @@ int main(void) // size is unknown and will always be, we can render it right away FirstSegment.Render(Ebml_file); - KaxAttachments * pAllAttachments = static_cast(FirstSegment.FindFirstElt(EBML_INFO(KaxAttachments), true)); + KaxAttachments * pAllAttachments = &GetChild(FirstSegment); if (pAllAttachments == NULL) return -1; pAllAttachments->SetSizeInfinite(); // size is unknown and will always be, we can render it right away pAllAttachments->Render(Ebml_file); - KaxAttached * pAttachment1 = static_cast(pAllAttachments->FindFirstElt(EBML_INFO(KaxAttached), true)); + KaxAttached * pAttachment1 = &GetChild(*pAllAttachments); if (pAttachment1 == NULL) return -1; - KaxFileName * pFileName1 = static_cast(pAttachment1->FindFirstElt(EBML_INFO(KaxFileName), true)); + KaxFileName * pFileName1 = &GetChild(*pAttachment1); if (pFileName1 == NULL) return -1; pFileName1->SetValue(UTFstring{L"file1.txt"}); - KaxFileData * pFileData1 = static_cast(pAttachment1->FindFirstElt(EBML_INFO(KaxFileData), true)); + KaxFileData * pFileData1 = &GetChild(*pAttachment1); if (pFileData1 == NULL) return -1; char Buffer1[] = "Ah ah ah !"; @@ -81,15 +81,15 @@ int main(void) // should produce an error if the size is not infinite and the data has been rendered pAttachment1->Render(Ebml_file); - KaxAttached * pAttachment2 = static_cast(pAllAttachments->AddNewElt(EBML_INFO(KaxAttached))); + KaxAttached * pAttachment2 = &AddNewChild(*pAllAttachments); if (pAttachment2 == NULL) return -1; - KaxFileName * pFileName2 = static_cast(pAttachment2->FindFirstElt(EBML_INFO(KaxFileName), true)); + KaxFileName * pFileName2 = &GetChild(*pAttachment2); if (pFileName2 == NULL) return -1; pFileName2->SetValue(UTFstring{L"file2.txt"}); // Add a void element (data is discarded) - EbmlVoid * pVoid = static_cast(pAttachment2->FindFirstElt(EBML_INFO(EbmlVoid), true)); + EbmlVoid * pVoid = &GetChild(*pAttachment2); if (pVoid == NULL) return -1; static_cast(pVoid)->SetBuffer((const binary*) Buffer1, countof(Buffer1));