Skip to content

Commit af66147

Browse files
committed
mkvalidator: Fix checking of cues
The earlier code had several flaws: 1. It was very slow, because finding a matching block always started with the very first block in the first cluster and checked every block until an acceptable block (one with the right timestamp from the right track) had been found. 2. Only one CueTrackPositions (namely the first one) has been checked per CuePoint, although there can be multiple CueTrackPositions. 3. If e.g. no CueTrack element was present, one got a generic error (error code 0x200), because a mandatory element was missing; but MATROSKA_CueTrackNum returned -1 (for invalid TrackNum) and so one also received a nonsense error 0x312: "CueEntry Track #-1 and timecode xxx ms not found". 4. The CueClusterPosition and CueRelativePosition elements have not been checked at all. All four points have been corrected. (mkvmerge versions 5.9 to 6.3 (inclusive) wrote CueRelativePosition elements that (in case of BlockGroups) pointed to the inner blocks and not the outer BlockGroups. mkvalidator now detects such files as invalid.) Signed-off-by: Andreas Rheinhardt <[email protected]>
1 parent 4667ef8 commit af66147

File tree

1 file changed

+112
-21
lines changed

1 file changed

+112
-21
lines changed

mkvalidator/mkvalidator.c

Lines changed: 112 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -854,42 +854,133 @@ static int CheckLacingKeyframe(void)
854854
return Result;
855855
}
856856

857-
static int CheckCueEntries(ebml_master *Cues)
857+
static int CheckCueEntries(ebml_master *Cues, filepos_t SegmentDataOffset)
858858
{
859859
int Result = 0;
860-
timecode_t TimecodeEntry, PrevTimecode = INVALID_TIMECODE_T;
861-
int16_t TrackNumEntry;
862-
matroska_cluster **Cluster;
863-
matroska_block *Block;
864-
int ClustNum = 0;
865860

866861
if (!RSegmentInfo)
867862
Result |= OutputError(0x310,T("A Cues (index) is defined but no SegmentInfo was found"));
868863
else if (ARRAYCOUNT(RClusters,matroska_cluster*))
869864
{
870-
matroska_cuepoint *CuePoint = (matroska_cuepoint*)EBML_MasterFindChild(Cues, &MATROSKA_ContextCuePoint);
865+
matroska_cluster **Cluster = ARRAYBEGIN(RClusters,matroska_cluster*);
866+
matroska_cuepoint *CuePoint = (matroska_cuepoint*)EBML_MasterFindChild(Cues, &MATROSKA_ContextCuePoint);
867+
timecode_t TimecodeEntry, PrevTimecode = INVALID_TIMECODE_T;
868+
int CueNum = 0;
869+
871870
while (CuePoint)
872871
{
873-
if (!Quiet && ClustNum++ % 24 == 0)
872+
if (!Quiet && CueNum++ % 24 == 0)
874873
TextWrite(StdErr,T("."));
875874
MATROSKA_LinkCueSegmentInfo(CuePoint,RSegmentInfo);
876875
TimecodeEntry = MATROSKA_CueTimecode(CuePoint);
877-
TrackNumEntry = MATROSKA_CueTrackNum(CuePoint);
876+
877+
if (TimecodeEntry == INVALID_TIMECODE_T)
878+
goto NextCuePoint;
878879

879880
if (TimecodeEntry < PrevTimecode && PrevTimecode != INVALID_TIMECODE_T)
880881
OutputWarning(0x311,T("The Cues entry for timecode %") TPRId64 T(" ms is listed after entry %") TPRId64 T(" ms"),Scale64(TimecodeEntry,1,1000000),Scale64(PrevTimecode,1,1000000));
881882

882-
// find a matching Block
883-
for (Cluster = ARRAYBEGIN(RClusters,matroska_cluster*);Cluster != ARRAYEND(RClusters,matroska_cluster*); ++Cluster)
884-
{
885-
Block = MATROSKA_GetBlockForTimecode(*Cluster, TimecodeEntry, TrackNumEntry);
886-
if (Block)
887-
break;
888-
}
889-
if (Cluster == ARRAYEND(RClusters,matroska_cluster*))
890-
Result |= OutputError(0x312,T("CueEntry Track #%d and timecode %") TPRId64 T(" ms not found"),(int)TrackNumEntry,Scale64(TimecodeEntry,1,1000000));
891-
PrevTimecode = TimecodeEntry;
892-
CuePoint = (matroska_cuepoint*)EBML_MasterFindNextElt(Cues, (ebml_element*)CuePoint, 0, 0);
883+
for (ebml_element *CueTrackPositions = EBML_MasterFindChild(CuePoint, &MATROSKA_ContextCueTrackPositions);
884+
CueTrackPositions; CueTrackPositions = EBML_MasterNextChild(CuePoint, CueTrackPositions))
885+
{
886+
ebml_element *Block = NULL, *Element = EBML_MasterFindChild(CueTrackPositions,&MATROSKA_ContextCueTrack);
887+
filepos_t ClusterOffset, CueRelativePosition;
888+
int Direction;
889+
int16_t TrackNumEntry;
890+
tchar_t Error[384];
891+
892+
if (!Element)
893+
continue;
894+
895+
TrackNumEntry = EL_Int(Element);
896+
897+
Element = EBML_MasterFindChild(CueTrackPositions, &MATROSKA_ContextCueClusterPosition);
898+
if (!Element)
899+
continue;
900+
901+
ClusterOffset = EL_Int(Element) + SegmentDataOffset;
902+
903+
Element = EBML_MasterFindChild(CueTrackPositions, &MATROSKA_ContextCueRelativePosition);
904+
CueRelativePosition = Element ? EL_Int(Element) : INVALID_FILEPOS_T;
905+
906+
Direction = EL_Pos(*Cluster) <= ClusterOffset ? 1 : -1;
907+
908+
do
909+
{
910+
if (EL_Pos(*Cluster) == ClusterOffset)
911+
{
912+
Block = MATROSKA_GetNextBlockForTimecode(*Cluster, NULL, TimecodeEntry, TrackNumEntry, 1);
913+
break;
914+
}
915+
916+
if ((Direction == 1 && (EL_Pos(*Cluster) > ClusterOffset || Cluster == ARRAYEND(RClusters, matroska_cluster*) - 1)) ||
917+
(Direction ==-1 && (EL_Pos(*Cluster) < ClusterOffset || Cluster == ARRAYBEGIN(RClusters, matroska_cluster*))))
918+
break;
919+
920+
Cluster += Direction;
921+
}
922+
while (1);
923+
924+
if (Block)
925+
{
926+
ebml_element *PreviousBlock;
927+
928+
if (CueRelativePosition == INVALID_FILEPOS_T || EL_Pos(Block) - EBML_ElementPositionData((ebml_element*)*Cluster) == CueRelativePosition)
929+
continue;
930+
931+
while (Block && EL_Pos(Block) - EBML_ElementPositionData((ebml_element*)*Cluster) < CueRelativePosition)
932+
{
933+
PreviousBlock = Block;
934+
Block = MATROSKA_GetNextBlockForTimecode(*Cluster, PreviousBlock, TimecodeEntry, TrackNumEntry, 1);
935+
}
936+
937+
Block = Block ? Block : PreviousBlock;
938+
939+
if (EL_Pos(Block) - EBML_ElementPositionData((ebml_element*)*Cluster) == CueRelativePosition)
940+
continue;
941+
}
942+
943+
stprintf_s(Error, TSIZEOF(Error), T("An invalid Cues entry points to a (Simple)Block in track #%d with timecode %")
944+
TPRId64 T(" ms supposedly in the cluster with offset %") TPRId64, TrackNumEntry, Scale64(TimecodeEntry,1,1000000), ClusterOffset);
945+
946+
if (CueRelativePosition != INVALID_FILEPOS_T)
947+
stcatprintf_s(Error, TSIZEOF(Error), T(" (relative offset %") TPRId64 T(")"), CueRelativePosition);
948+
949+
if (Block)
950+
{
951+
stcatprintf_s(Error, TSIZEOF(Error), T(". The actual relative offset is %") TPRId64 T("."),
952+
EL_Pos(Block) - EBML_ElementPositionData((ebml_element*)*Cluster));
953+
Result |= OutputError(0x314, Error);
954+
continue;
955+
}
956+
957+
if (EL_Pos(*Cluster) != ClusterOffset)
958+
tcscat_s(Error, TSIZEOF(Error), T(". No cluster with this offset exists"));
959+
960+
for (Cluster = ARRAYBEGIN(RClusters, matroska_cluster*); Cluster != ARRAYEND(RClusters, matroska_cluster*); ++Cluster)
961+
{
962+
Block = MATROSKA_GetNextBlockForTimecode(*Cluster, NULL, TimecodeEntry, TrackNumEntry, 1);
963+
if (Block)
964+
break;
965+
}
966+
967+
if (!Block)
968+
{
969+
tcscat_s(Error, TSIZEOF(Error), T(". No (Simple)Block with this timecode exists in this track at all."));
970+
Result |= OutputError(0x312, Error);
971+
}
972+
else
973+
{
974+
stcatprintf_s(Error, TSIZEOF(Error), T(". A (Simple)Block with this timecode is in the cluster with offset %") TPRId64
975+
T(" (relative offset %") TPRId64 T(")."), EL_Pos(*Cluster), EL_Pos(Block) - EBML_ElementPositionData((ebml_element*)*Cluster));
976+
Result |= OutputError(0x313, Error);
977+
}
978+
}
979+
980+
PrevTimecode = TimecodeEntry;
981+
982+
NextCuePoint:
983+
CuePoint = (matroska_cuepoint*)EBML_MasterFindNextElt(Cues, (ebml_element*)CuePoint, 0, 0);
893984
}
894985
}
895986
return Result;
@@ -1384,7 +1475,7 @@ int main(int argc, const char *argv[])
13841475
OutputWarning(0x800,T("The segment has Clusters but no Cues section (bad for seeking)"));
13851476
}
13861477
else
1387-
Result |= CheckCueEntries(RCues);
1478+
Result |= CheckCueEntries(RCues, EBML_ElementPositionData((ebml_element*)RSegment));
13881479
if (!RTrackInfo)
13891480
{
13901481
Result = OutputError(0x41,T("The segment has Clusters but no TrackInfo section"));

0 commit comments

Comments
 (0)