Compare commits

...

5 Commits

Author SHA1 Message Date
matthewjheaney
c2f6bea0d8 changed version to 1.0.0.4
Change-Id: I8580a5de258d74b6a83505046d014ad9c6abfc23
2010-10-14 19:34:37 -04:00
matthewjheaney
16934eb76f separate cue-based searches
Change-Id: I5d98be1c9c5bc33b4ef216f48de22220f38c0f36
2010-10-13 13:44:13 -04:00
matthewjheaney
9c15c0f40b Segment::GetCluster returns CuePoint too
Change-Id: Id1b865a9efdcee6b6ef68d4fb323da50f1942f2a
2010-10-12 15:29:28 -04:00
matthewjheaney
43f77d54bd defend against badly-formatted cue points
Change-Id: I72bab89f0828b2c5275a4c4b3ac22ee61a173ddf
2010-10-11 19:09:27 -04:00
matthewjheaney
aec650fed7 made Cues member variables mutable
Change-Id: I405b845f37c9deaffd67e50752fb9b2aa7124484
2010-10-11 12:59:17 -04:00
2 changed files with 218 additions and 253 deletions

View File

@@ -24,7 +24,7 @@ void mkvparser::GetVersion(int& major, int& minor, int& build, int& revision)
major = 1; major = 1;
minor = 0; minor = 0;
build = 0; build = 0;
revision = 3; revision = 4;
} }
@@ -1740,7 +1740,7 @@ Cues::~Cues()
} }
void Cues::Init() void Cues::Init() const
{ {
if (m_cue_points) if (m_cue_points)
return; return;
@@ -1780,14 +1780,12 @@ void Cues::Init()
pos += size; //consume payload pos += size; //consume payload
assert(pos <= stop); assert(pos <= stop);
} }
LoadCuePoint();
} }
void Cues::PreloadCuePoint( void Cues::PreloadCuePoint(
size_t& cue_points_size, size_t& cue_points_size,
long long pos) long long pos) const
{ {
assert(m_count == 0); assert(m_count == 0);
@@ -1832,42 +1830,12 @@ void Cues::PreloadCuePoint(
cue_points_size = n; cue_points_size = n;
} }
CuePoint* const pCP = new CuePoint(pos); CuePoint* const pCP = new CuePoint(m_preload_count, pos);
m_cue_points[m_preload_count++] = pCP; m_cue_points[m_preload_count++] = pCP;
} }
#if 0 bool Cues::LoadCuePoint() const
const CuePoint* Cues::GetFirst() const
{
if (m_count < 1)
return NULL;
CuePoint* const pCP = m_cue_points[0];
assert(pCP);
assert(pCP->GetTimeCode() >= 0);
return pCP;
}
const CuePoint* Cues::GetLast() const
{
if (m_count < 1)
return NULL;
const size_t idx = m_count - 1;
CuePoint* const pCP = m_cue_points[idx];
assert(pCP);
assert(pCP->GetTimeCode() >= 0);
return pCP;
}
#endif
bool Cues::LoadCuePoint()
{ {
//odbgstream os; //odbgstream os;
//os << "Cues::LoadCuePoint" << endl; //os << "Cues::LoadCuePoint" << endl;
@@ -1912,8 +1880,9 @@ bool Cues::LoadCuePoint()
CuePoint* const pCP = m_cue_points[m_count]; CuePoint* const pCP = m_cue_points[m_count];
assert(pCP); assert(pCP);
assert((pCP->GetTimeCode() >= 0) || (-pCP->GetTimeCode() == idpos));
pCP->Load(pReader, idpos); pCP->Load(pReader);
++m_count; ++m_count;
--m_preload_count; --m_preload_count;
@@ -1936,7 +1905,7 @@ bool Cues::Find(
assert(time_ns >= 0); assert(time_ns >= 0);
assert(pTrack); assert(pTrack);
const_cast<Cues*>(this)->LoadCuePoint(); LoadCuePoint();
assert(m_cue_points); assert(m_cue_points);
assert(m_count > 0); assert(m_count > 0);
@@ -1971,7 +1940,7 @@ bool Cues::Find(
CuePoint* const pCP = *k; CuePoint* const pCP = *k;
assert(pCP); assert(pCP);
pCP->Load(pReader, 0); pCP->Load(pReader);
const long long t = pCP->GetTime(m_pSegment); const long long t = pCP->GetTime(m_pSegment);
@@ -2064,93 +2033,107 @@ bool Cues::FindNext(
#endif #endif
#if 0 const CuePoint* Cues::GetNext(const CuePoint* pCurr) const
const CuePoint* Cues::LoadCuePoint(
long long time_ns,
const Track* pTrack,
const CuePoint::TrackPosition*& pTP) const
{ {
assert(time_ns >= 0); if (pCurr == NULL)
assert(pTrack); return NULL;
//odbgstream os; assert(pCurr->GetTimeCode() >= 0);
//os << "Cues::LoadCuePoint: time[sec]="
// << (double(time_ns) / 1000000000)
// << " count=" << m_count
// << " preload_count=" << m_preload_count
// << endl;
if (m_count > 0)
{
const CuePoint* pCP = GetLast();
assert(pCP);
const long long ns = pCP->GetTime(m_pSegment);
if ((ns >= time_ns) || (m_preload_count == 0))
{
const bool bFound = Find(time_ns, pTrack, pCP, pTP);
assert(bFound); //TODO
assert(pCP);
assert(pTP);
return pCP;
}
}
assert(m_preload_count > 0);
assert(m_cue_points); assert(m_cue_points);
assert(m_count >= 1);
IMkvReader* const pReader = m_pSegment->m_pReader; const size_t count = m_count + m_preload_count;
CuePoint** const ii = m_cue_points + m_count; size_t index = pCurr->m_index;
CuePoint** i = ii; assert(index < count);
CuePoint** const jj = ii + m_preload_count; CuePoint* const* const pp = m_cue_points;
CuePoint** j = jj; assert(pp);
assert(pp[index] == pCurr);
++index;
if (index >= count)
return NULL;
CuePoint* const pNext = pp[index];
assert(pNext);
pNext->Load(m_pSegment->m_pReader);
return pNext;
}
const BlockEntry* Cues::GetBlock(
const CuePoint* pCP,
const CuePoint::TrackPosition* pTP) const
{
if (pCP == NULL)
return NULL;
if (pTP == NULL)
return NULL;
return m_pSegment->GetBlock(*pCP, *pTP);
}
const BlockEntry* Segment::GetBlock(
const CuePoint& cp,
const CuePoint::TrackPosition& tp)
{
Cluster** const ii = m_clusters;
Cluster** i = ii;
const long count = m_clusterCount + m_clusterPreloadCount;
Cluster** const jj = ii + count;
Cluster** j = jj;
while (i < j) while (i < j)
{ {
//INVARIANT: //INVARIANT:
//[ii, i) <= time_ns //[ii, i) < pTP->m_pos
//[i, j) ? //[i, j) ?
//[j, jj) > time_ns //[j, jj) > pTP->m_pos
CuePoint** const k = i + (j - i) / 2; Cluster** const k = i + (j - i) / 2;
assert(k < jj); assert(k < jj);
CuePoint* const pCP = *k; Cluster* const pCluster = *k;
assert(pCP); assert(pCluster);
pCP->Load(pReader, 0); const long long pos_ = pCluster->m_pos;
assert(pos_);
const long long t = pCP->GetTime(m_pSegment); const long long pos = pos_ * ((pos_ < 0) ? -1 : 1);
if (t <= time_ns) if (pos < tp.m_pos)
i = k + 1; i = k + 1;
else else if (pos > tp.m_pos)
j = k; j = k;
else
assert(i <= j); return pCluster->GetEntry(cp, tp);
} }
assert(i == j); assert(i == j);
assert(i <= jj);
assert(i > m_cue_points);
const CuePoint* const pCP = *--i; Cluster* const pCluster = Cluster::Parse(this, -1, tp.m_pos);
assert(pCP); const ptrdiff_t idx = i - m_clusters;
assert(pCP->GetTime(m_pSegment) <= time_ns);
pTP = pCP->Find(pTrack); PreloadCluster(pCluster, idx);
assert(pTP); //TODO assert(m_clusters);
assert(m_clusterPreloadCount > 0);
assert(m_clusters[idx] == pCluster);
return pCP; return pCluster->GetEntry(cp, tp);
} }
#endif
CuePoint::CuePoint(long long pos) :
CuePoint::CuePoint(size_t idx, long long pos) :
m_index(idx),
m_timecode(-1 * pos), m_timecode(-1 * pos),
m_track_positions(NULL), m_track_positions(NULL),
m_track_positions_count(0) m_track_positions_count(0)
@@ -2165,7 +2148,7 @@ CuePoint::~CuePoint()
} }
void CuePoint::Load(IMkvReader* pReader, long long idpos) void CuePoint::Load(IMkvReader* pReader)
{ {
//odbgstream os; //odbgstream os;
//os << "CuePoint::Load(begin): timecode=" << m_timecode << endl; //os << "CuePoint::Load(begin): timecode=" << m_timecode << endl;
@@ -2176,8 +2159,7 @@ void CuePoint::Load(IMkvReader* pReader, long long idpos)
assert(m_track_positions == NULL); assert(m_track_positions == NULL);
assert(m_track_positions_count == 0); assert(m_track_positions_count == 0);
long long pos_ = -1 * m_timecode; long long pos_ = -m_timecode;
assert((idpos <= 0) || (idpos == pos_));
long long stop; long long stop;
@@ -2321,9 +2303,9 @@ void CuePoint::TrackPosition::Parse(
assert(pos <= stop); assert(pos <= stop);
} }
assert(m_track > 0);
assert(m_pos >= 0); assert(m_pos >= 0);
assert(m_block > 0); //assert(m_track > 0);
//assert(m_block > 0);
} }
@@ -2569,7 +2551,7 @@ Cluster* Segment::GetNext(const Cluster* pCurr)
} }
Cluster* Segment::GetCluster(long long time_ns) Cluster* Segment::FindCluster(long long time_ns)
{ {
if ((m_clusters == NULL) || (m_clusterCount <= 0)) if ((m_clusters == NULL) || (m_clusterCount <= 0))
return &m_eos; return &m_eos;
@@ -2627,39 +2609,26 @@ Cluster* Segment::GetCluster(long long time_ns)
} }
void Segment::GetCluster( const BlockEntry* Segment::Seek(
long long time_ns, long long time_ns,
Track* pTrack, const Track* pTrack)
Cluster*& pCluster,
const BlockEntry*& pBlockEntry)
{ {
assert(pTrack); assert(pTrack);
if (SearchCues(time_ns, pTrack, pCluster, pBlockEntry))
return;
if ((m_clusters == NULL) || (m_clusterCount <= 0)) if ((m_clusters == NULL) || (m_clusterCount <= 0))
{ return pTrack->GetEOS();
pCluster = &m_eos;
pBlockEntry = pTrack->GetEOS();
return;
}
Cluster** const i = m_clusters; Cluster** const i = m_clusters;
assert(i); assert(i);
{ {
pCluster = *i; Cluster* const pCluster = *i;
assert(pCluster); assert(pCluster);
assert(pCluster->m_index == 0); assert(pCluster->m_index == 0); //m_clusterCount > 0
assert(pCluster->m_pSegment == this); assert(pCluster->m_pSegment == this);
if (time_ns <= pCluster->GetTime()) if (time_ns <= pCluster->GetTime())
{ return pCluster->GetEntry(pTrack);
pBlockEntry = pCluster->GetEntry(pTrack);
return;
}
} }
Cluster** const j = i + m_clusterCount; Cluster** const j = i + m_clusterCount;
@@ -2703,12 +2672,11 @@ void Segment::GetCluster(
assert(lo > i); assert(lo > i);
assert(lo <= j); assert(lo <= j);
pCluster = *--lo; Cluster* const pCluster = *--lo;
assert(pCluster); assert(pCluster);
assert(pCluster->GetTime() <= time_ns); assert(pCluster->GetTime() <= time_ns);
pBlockEntry = pCluster->GetEntry(pTrack); return pCluster->GetEntry(pTrack);
return;
} }
assert(pTrack->GetType() == 1); //video assert(pTrack->GetType() == 1); //video
@@ -2743,12 +2711,12 @@ void Segment::GetCluster(
assert(lo > i); assert(lo > i);
assert(lo <= j); assert(lo <= j);
pCluster = *--lo; Cluster* pCluster = *--lo;
assert(pCluster); assert(pCluster);
assert(pCluster->GetTime() <= time_ns); assert(pCluster->GetTime() <= time_ns);
{ {
pBlockEntry = pCluster->GetEntry(pTrack); const BlockEntry* const pBlockEntry = pCluster->GetEntry(pTrack);
assert(pBlockEntry); assert(pBlockEntry);
if (!pBlockEntry->EOS()) //found a keyframe if (!pBlockEntry->EOS()) //found a keyframe
@@ -2763,11 +2731,11 @@ void Segment::GetCluster(
//simply return the first keyframe we find. //simply return the first keyframe we find.
if (pBlock->GetTime(pCluster) <= time_ns) if (pBlock->GetTime(pCluster) <= time_ns)
return; return pBlockEntry;
} }
} }
const VideoTrack* const pVideo = static_cast<VideoTrack*>(pTrack); const VideoTrack* const pVideo = static_cast<const VideoTrack*>(pTrack);
while (lo != i) while (lo != i)
{ {
@@ -2775,26 +2743,28 @@ void Segment::GetCluster(
assert(pCluster); assert(pCluster);
assert(pCluster->GetTime() <= time_ns); assert(pCluster->GetTime() <= time_ns);
pBlockEntry = pCluster->GetMaxKey(pVideo); const BlockEntry* const pBlockEntry = pCluster->GetMaxKey(pVideo);
assert(pBlockEntry); assert(pBlockEntry);
if (!pBlockEntry->EOS()) if (!pBlockEntry->EOS())
return; return pBlockEntry;
} }
//weird: we're on the first cluster, but no keyframe found //weird: we're on the first cluster, but no keyframe found
//should never happen but we must return something anyway //should never happen but we must return something anyway
pCluster = &m_eos; return pTrack->GetEOS();
pBlockEntry = pTrack->GetEOS();
} }
#if 0
bool Segment::SearchCues( bool Segment::SearchCues(
long long time_ns, long long time_ns,
Track* pTrack, Track* pTrack,
Cluster*& pCluster, Cluster*& pCluster,
const BlockEntry*& pBlockEntry) const BlockEntry*& pBlockEntry,
const CuePoint*& pCP,
const CuePoint::TrackPosition*& pTP)
{ {
if (pTrack->GetType() != 1) //not video if (pTrack->GetType() != 1) //not video
return false; //TODO: for now, just handle video stream return false; //TODO: for now, just handle video stream
@@ -2802,9 +2772,6 @@ bool Segment::SearchCues(
if (m_pCues == NULL) if (m_pCues == NULL)
return false; return false;
const CuePoint* pCP;
const CuePoint::TrackPosition* pTP;
if (!m_pCues->Find(time_ns, pTrack, pCP, pTP)) if (!m_pCues->Find(time_ns, pTrack, pCP, pTP))
return false; //weird return false; //weird
@@ -2816,60 +2783,9 @@ bool Segment::SearchCues(
//so we now need to search for the cluster having //so we now need to search for the cluster having
//the indicated position. //the indicated position.
Cluster** const ii = m_clusters; return GetCluster(pCP, pTP, pCluster, pBlockEntry);
Cluster** i = ii;
const long count = m_clusterCount + m_clusterPreloadCount;
Cluster** const jj = ii + count;
Cluster** j = jj;
while (i < j)
{
//INVARIANT:
//[ii, i) < pTP->m_pos
//[i, j) ?
//[j, jj) > pTP->m_pos
Cluster** const k = i + (j - i) / 2;
assert(k < jj);
pCluster = *k;
assert(pCluster);
const long long pos_ = pCluster->m_pos;
assert(pos_);
const long long pos = pos_ * ((pos_ < 0) ? -1 : 1);
if (pos < pTP->m_pos)
i = k + 1;
else if (pos > pTP->m_pos)
j = k;
else
{
pBlockEntry = pCluster->GetEntry(*pCP, *pTP);
assert(pBlockEntry);
return true;
}
}
assert(i == j);
pCluster = Cluster::Parse(this, -1, pTP->m_pos);
const ptrdiff_t idx = i - m_clusters;
PreloadCluster(pCluster, idx);
assert(m_clusters);
assert(m_clusterPreloadCount > 0);
assert(m_clusters[idx] == pCluster);
pBlockEntry = pCluster->GetEntry(*pCP, *pTP);
assert(pBlockEntry);
return true;
} }
#endif
Tracks* Segment::GetTracks() const Tracks* Segment::GetTracks() const
@@ -2884,7 +2800,7 @@ const SegmentInfo* Segment::GetInfo() const
} }
Cues* Segment::GetCues() const const Cues* Segment::GetCues() const
{ {
return m_pCues; return m_pCues;
} }
@@ -4104,23 +4020,85 @@ Cluster::GetEntry(
const CuePoint::TrackPosition& tp) const CuePoint::TrackPosition& tp)
{ {
assert(m_pSegment); assert(m_pSegment);
assert(tp.m_block > 0);
LoadBlockEntries(); LoadBlockEntries();
assert(m_entries); //TODO: handle empty cluster
assert(m_entriesCount > 0);
assert(tp.m_block <= (long long)m_entriesCount); //blocks are 1-based
const size_t block = static_cast<size_t>(tp.m_block); if (m_entries == NULL)
const size_t index = block - 1; return NULL;
const BlockEntry* const pEntry = m_entries[index]; const long long count = m_entriesCount;
assert(pEntry);
assert(!pEntry->EOS());
assert(pEntry->GetBlock()->GetTrackNumber() == tp.m_track);
assert(pEntry->GetBlock()->GetTimeCode(this) == cp.GetTimeCode());
return pEntry; if (count <= 0)
return NULL;
const long long tc = cp.GetTimeCode();
if ((tp.m_block > 0) && (tp.m_block <= count))
{
const size_t block = static_cast<size_t>(tp.m_block);
const size_t index = block - 1;
const BlockEntry* const pEntry = m_entries[index];
assert(pEntry);
assert(!pEntry->EOS());
const Block* const pBlock = pEntry->GetBlock();
assert(pBlock);
if ((pBlock->GetTrackNumber() == tp.m_track) &&
(pBlock->GetTimeCode(this) == tc))
{
return pEntry;
}
}
const BlockEntry* const* i = m_entries;
const BlockEntry* const* const j = i + count;
while (i != j)
{
const BlockEntry* const pEntry = *i++;
assert(pEntry);
assert(!pEntry->EOS());
const Block* const pBlock = pEntry->GetBlock();
assert(pBlock);
if (pBlock->GetTrackNumber() != tp.m_track)
continue;
const long long tc_ = pBlock->GetTimeCode(this);
if (tc_ < tc)
continue;
if (tc_ > tc)
return NULL;
const Tracks* const pTracks = m_pSegment->GetTracks();
assert(pTracks);
const long tn = static_cast<long>(tp.m_track);
const Track* const pTrack = pTracks->GetTrackByNumber(tn);
if (pTrack == NULL)
return NULL;
const long long type = pTrack->GetType();
if (type == 2) //audio
return pEntry;
if (type != 1) //not video
return NULL;
if (!pBlock->IsKey())
return NULL;
return pEntry;
}
return NULL;
} }

View File

@@ -334,18 +334,19 @@ private:
char* m_pTitleAsUTF8; char* m_pTitleAsUTF8;
}; };
class Cues;
class CuePoint class CuePoint
{ {
friend class Cues;
CuePoint(size_t, long long);
~CuePoint();
CuePoint(const CuePoint&); CuePoint(const CuePoint&);
CuePoint& operator=(const CuePoint&); CuePoint& operator=(const CuePoint&);
public: public:
explicit CuePoint(long long); void Load(IMkvReader*);
~CuePoint();
//void Parse(IMkvReader*, long long start, long long size);
void Load(IMkvReader*, long long);
long long GetTimeCode() const; //absolute but unscaled long long GetTimeCode() const; //absolute but unscaled
long long GetTime(Segment*) const; //absolute and scaled (ns units) long long GetTime(Segment*) const; //absolute and scaled (ns units)
@@ -365,7 +366,7 @@ public:
const TrackPosition* Find(const Track*) const; const TrackPosition* Find(const Track*) const;
private: private:
//long long m_pos; const size_t m_index;
long long m_timecode; long long m_timecode;
TrackPosition* m_track_positions; TrackPosition* m_track_positions;
size_t m_track_positions_count; size_t m_track_positions_count;
@@ -375,6 +376,11 @@ private:
class Cues class Cues
{ {
friend class Segment;
Cues(Segment*, long long start, long long size);
~Cues();
Cues(const Cues&); Cues(const Cues&);
Cues& operator=(const Cues&); Cues& operator=(const Cues&);
@@ -383,9 +389,6 @@ public:
const long long m_start; const long long m_start;
const long long m_size; const long long m_size;
Cues(Segment*, long long start, long long size);
~Cues();
bool Find( //lower bound of time_ns bool Find( //lower bound of time_ns
long long time_ns, long long time_ns,
const Track*, const Track*,
@@ -400,29 +403,21 @@ public:
const CuePoint::TrackPosition*&) const; const CuePoint::TrackPosition*&) const;
#endif #endif
#if 0 const CuePoint* GetNext(const CuePoint*) const;
const CuePoint* GetFirst() const;
const CuePoint* GetLast() const;
#endif
bool LoadCuePoint(); const BlockEntry* GetBlock(
const CuePoint*,
#if 0 const CuePoint::TrackPosition*) const;
const CuePoint* LoadCuePoint(
long long time_ns,
const Track*,
const CuePoint::TrackPosition*&) const;
#endif
private: private:
void Init(); void Init() const;
void PreloadCuePoint(size_t&, long long); bool LoadCuePoint() const;
void PreloadCuePoint(size_t&, long long) const;
CuePoint** m_cue_points; mutable CuePoint** m_cue_points;
//size_t m_cue_points_size; mutable size_t m_count;
size_t m_count; mutable size_t m_preload_count;
size_t m_preload_count; mutable long long m_pos;
long long m_pos;
}; };
@@ -479,6 +474,8 @@ private:
class Segment class Segment
{ {
friend class Cues;
Segment(const Segment&); Segment(const Segment&);
Segment& operator=(const Segment&); Segment& operator=(const Segment&);
@@ -510,25 +507,17 @@ public:
Tracks* GetTracks() const; Tracks* GetTracks() const;
const SegmentInfo* GetInfo() const; const SegmentInfo* GetInfo() const;
const Cues* GetCues() const;
long long GetDuration() const; long long GetDuration() const;
//NOTE: this turned out to be too inefficient. unsigned long GetCount() const;
//long long Load(long long time_nanoseconds);
Cluster* GetFirst(); Cluster* GetFirst();
Cluster* GetLast(); Cluster* GetLast();
unsigned long GetCount() const;
Cluster* GetNext(const Cluster*); Cluster* GetNext(const Cluster*);
Cluster* GetCluster(long long time_nanoseconds);
void GetCluster( Cluster* FindCluster(long long time_nanoseconds);
long long time_nanoseconds, const BlockEntry* Seek(long long time_nanoseconds, const Track*);
Track*,
Cluster*&,
const BlockEntry*&);
Cues* GetCues() const;
private: private:
@@ -548,11 +537,9 @@ private:
void ParseSeekEntry(long long pos, long long size); void ParseSeekEntry(long long pos, long long size);
void ParseCues(long long); void ParseCues(long long);
bool SearchCues( const BlockEntry* GetBlock(
long long time_ns, const CuePoint&,
Track*, const CuePoint::TrackPosition&);
Cluster*&,
const BlockEntry*&);
}; };