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;
minor = 0;
build = 0;
revision = 3;
revision = 4;
}
@@ -1740,7 +1740,7 @@ Cues::~Cues()
}
void Cues::Init()
void Cues::Init() const
{
if (m_cue_points)
return;
@@ -1780,14 +1780,12 @@ void Cues::Init()
pos += size; //consume payload
assert(pos <= stop);
}
LoadCuePoint();
}
void Cues::PreloadCuePoint(
size_t& cue_points_size,
long long pos)
long long pos) const
{
assert(m_count == 0);
@@ -1832,42 +1830,12 @@ void Cues::PreloadCuePoint(
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;
}
#if 0
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()
bool Cues::LoadCuePoint() const
{
//odbgstream os;
//os << "Cues::LoadCuePoint" << endl;
@@ -1912,8 +1880,9 @@ bool Cues::LoadCuePoint()
CuePoint* const pCP = m_cue_points[m_count];
assert(pCP);
assert((pCP->GetTimeCode() >= 0) || (-pCP->GetTimeCode() == idpos));
pCP->Load(pReader, idpos);
pCP->Load(pReader);
++m_count;
--m_preload_count;
@@ -1936,7 +1905,7 @@ bool Cues::Find(
assert(time_ns >= 0);
assert(pTrack);
const_cast<Cues*>(this)->LoadCuePoint();
LoadCuePoint();
assert(m_cue_points);
assert(m_count > 0);
@@ -1971,7 +1940,7 @@ bool Cues::Find(
CuePoint* const pCP = *k;
assert(pCP);
pCP->Load(pReader, 0);
pCP->Load(pReader);
const long long t = pCP->GetTime(m_pSegment);
@@ -2064,93 +2033,107 @@ bool Cues::FindNext(
#endif
#if 0
const CuePoint* Cues::LoadCuePoint(
long long time_ns,
const Track* pTrack,
const CuePoint::TrackPosition*& pTP) const
const CuePoint* Cues::GetNext(const CuePoint* pCurr) const
{
assert(time_ns >= 0);
assert(pTrack);
if (pCurr == NULL)
return NULL;
//odbgstream os;
//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(pCurr->GetTimeCode() >= 0);
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;
CuePoint** i = ii;
size_t index = pCurr->m_index;
assert(index < count);
CuePoint** const jj = ii + m_preload_count;
CuePoint** j = jj;
CuePoint* const* const pp = m_cue_points;
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)
{
//INVARIANT:
//[ii, i) <= time_ns
//[i, j) ?
//[j, jj) > time_ns
//[ii, i) < pTP->m_pos
//[i, j) ?
//[j, jj) > pTP->m_pos
CuePoint** const k = i + (j - i) / 2;
Cluster** const k = i + (j - i) / 2;
assert(k < jj);
CuePoint* const pCP = *k;
assert(pCP);
Cluster* const pCluster = *k;
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;
else
else if (pos > tp.m_pos)
j = k;
assert(i <= j);
else
return pCluster->GetEntry(cp, tp);
}
assert(i == j);
assert(i <= jj);
assert(i > m_cue_points);
const CuePoint* const pCP = *--i;
assert(pCP);
assert(pCP->GetTime(m_pSegment) <= time_ns);
Cluster* const pCluster = Cluster::Parse(this, -1, tp.m_pos);
const ptrdiff_t idx = i - m_clusters;
pTP = pCP->Find(pTrack);
assert(pTP); //TODO
PreloadCluster(pCluster, idx);
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_track_positions(NULL),
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;
//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_count == 0);
long long pos_ = -1 * m_timecode;
assert((idpos <= 0) || (idpos == pos_));
long long pos_ = -m_timecode;
long long stop;
@@ -2321,9 +2303,9 @@ void CuePoint::TrackPosition::Parse(
assert(pos <= stop);
}
assert(m_track > 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))
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,
Track* pTrack,
Cluster*& pCluster,
const BlockEntry*& pBlockEntry)
const Track* pTrack)
{
assert(pTrack);
if (SearchCues(time_ns, pTrack, pCluster, pBlockEntry))
return;
if ((m_clusters == NULL) || (m_clusterCount <= 0))
{
pCluster = &m_eos;
pBlockEntry = pTrack->GetEOS();
return;
}
return pTrack->GetEOS();
Cluster** const i = m_clusters;
assert(i);
{
pCluster = *i;
Cluster* const pCluster = *i;
assert(pCluster);
assert(pCluster->m_index == 0);
assert(pCluster->m_index == 0); //m_clusterCount > 0
assert(pCluster->m_pSegment == this);
if (time_ns <= pCluster->GetTime())
{
pBlockEntry = pCluster->GetEntry(pTrack);
return;
}
return pCluster->GetEntry(pTrack);
}
Cluster** const j = i + m_clusterCount;
@@ -2703,12 +2672,11 @@ void Segment::GetCluster(
assert(lo > i);
assert(lo <= j);
pCluster = *--lo;
Cluster* const pCluster = *--lo;
assert(pCluster);
assert(pCluster->GetTime() <= time_ns);
pBlockEntry = pCluster->GetEntry(pTrack);
return;
return pCluster->GetEntry(pTrack);
}
assert(pTrack->GetType() == 1); //video
@@ -2743,12 +2711,12 @@ void Segment::GetCluster(
assert(lo > i);
assert(lo <= j);
pCluster = *--lo;
Cluster* pCluster = *--lo;
assert(pCluster);
assert(pCluster->GetTime() <= time_ns);
{
pBlockEntry = pCluster->GetEntry(pTrack);
const BlockEntry* const pBlockEntry = pCluster->GetEntry(pTrack);
assert(pBlockEntry);
if (!pBlockEntry->EOS()) //found a keyframe
@@ -2763,11 +2731,11 @@ void Segment::GetCluster(
//simply return the first keyframe we find.
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)
{
@@ -2775,26 +2743,28 @@ void Segment::GetCluster(
assert(pCluster);
assert(pCluster->GetTime() <= time_ns);
pBlockEntry = pCluster->GetMaxKey(pVideo);
const BlockEntry* const pBlockEntry = pCluster->GetMaxKey(pVideo);
assert(pBlockEntry);
if (!pBlockEntry->EOS())
return;
return pBlockEntry;
}
//weird: we're on the first cluster, but no keyframe found
//should never happen but we must return something anyway
pCluster = &m_eos;
pBlockEntry = pTrack->GetEOS();
return pTrack->GetEOS();
}
#if 0
bool Segment::SearchCues(
long long time_ns,
Track* pTrack,
Cluster*& pCluster,
const BlockEntry*& pBlockEntry)
const BlockEntry*& pBlockEntry,
const CuePoint*& pCP,
const CuePoint::TrackPosition*& pTP)
{
if (pTrack->GetType() != 1) //not video
return false; //TODO: for now, just handle video stream
@@ -2802,9 +2772,6 @@ bool Segment::SearchCues(
if (m_pCues == NULL)
return false;
const CuePoint* pCP;
const CuePoint::TrackPosition* pTP;
if (!m_pCues->Find(time_ns, pTrack, pCP, pTP))
return false; //weird
@@ -2816,60 +2783,9 @@ bool Segment::SearchCues(
//so we now need to search for the cluster having
//the indicated position.
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)
{
//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;
return GetCluster(pCP, pTP, pCluster, pBlockEntry);
}
#endif
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;
}
@@ -4104,23 +4020,85 @@ Cluster::GetEntry(
const CuePoint::TrackPosition& tp)
{
assert(m_pSegment);
assert(tp.m_block > 0);
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);
const size_t index = block - 1;
if (m_entries == NULL)
return NULL;
const BlockEntry* const pEntry = m_entries[index];
assert(pEntry);
assert(!pEntry->EOS());
assert(pEntry->GetBlock()->GetTrackNumber() == tp.m_track);
assert(pEntry->GetBlock()->GetTimeCode(this) == cp.GetTimeCode());
const long long count = m_entriesCount;
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;
};
class Cues;
class CuePoint
{
friend class Cues;
CuePoint(size_t, long long);
~CuePoint();
CuePoint(const CuePoint&);
CuePoint& operator=(const CuePoint&);
public:
explicit CuePoint(long long);
~CuePoint();
//void Parse(IMkvReader*, long long start, long long size);
void Load(IMkvReader*, long long);
void Load(IMkvReader*);
long long GetTimeCode() const; //absolute but unscaled
long long GetTime(Segment*) const; //absolute and scaled (ns units)
@@ -365,7 +366,7 @@ public:
const TrackPosition* Find(const Track*) const;
private:
//long long m_pos;
const size_t m_index;
long long m_timecode;
TrackPosition* m_track_positions;
size_t m_track_positions_count;
@@ -375,6 +376,11 @@ private:
class Cues
{
friend class Segment;
Cues(Segment*, long long start, long long size);
~Cues();
Cues(const Cues&);
Cues& operator=(const Cues&);
@@ -383,9 +389,6 @@ public:
const long long m_start;
const long long m_size;
Cues(Segment*, long long start, long long size);
~Cues();
bool Find( //lower bound of time_ns
long long time_ns,
const Track*,
@@ -400,29 +403,21 @@ public:
const CuePoint::TrackPosition*&) const;
#endif
#if 0
const CuePoint* GetFirst() const;
const CuePoint* GetLast() const;
#endif
const CuePoint* GetNext(const CuePoint*) const;
bool LoadCuePoint();
#if 0
const CuePoint* LoadCuePoint(
long long time_ns,
const Track*,
const CuePoint::TrackPosition*&) const;
#endif
const BlockEntry* GetBlock(
const CuePoint*,
const CuePoint::TrackPosition*) const;
private:
void Init();
void PreloadCuePoint(size_t&, long long);
void Init() const;
bool LoadCuePoint() const;
void PreloadCuePoint(size_t&, long long) const;
CuePoint** m_cue_points;
//size_t m_cue_points_size;
size_t m_count;
size_t m_preload_count;
long long m_pos;
mutable CuePoint** m_cue_points;
mutable size_t m_count;
mutable size_t m_preload_count;
mutable long long m_pos;
};
@@ -479,6 +474,8 @@ private:
class Segment
{
friend class Cues;
Segment(const Segment&);
Segment& operator=(const Segment&);
@@ -510,25 +507,17 @@ public:
Tracks* GetTracks() const;
const SegmentInfo* GetInfo() const;
const Cues* GetCues() const;
long long GetDuration() const;
//NOTE: this turned out to be too inefficient.
//long long Load(long long time_nanoseconds);
unsigned long GetCount() const;
Cluster* GetFirst();
Cluster* GetLast();
unsigned long GetCount() const;
Cluster* GetNext(const Cluster*);
Cluster* GetCluster(long long time_nanoseconds);
void GetCluster(
long long time_nanoseconds,
Track*,
Cluster*&,
const BlockEntry*&);
Cues* GetCues() const;
Cluster* FindCluster(long long time_nanoseconds);
const BlockEntry* Seek(long long time_nanoseconds, const Track*);
private:
@@ -548,11 +537,9 @@ private:
void ParseSeekEntry(long long pos, long long size);
void ParseCues(long long);
bool SearchCues(
long long time_ns,
Track*,
Cluster*&,
const BlockEntry*&);
const BlockEntry* GetBlock(
const CuePoint&,
const CuePoint::TrackPosition&);
};