Compare commits

..

3 Commits

Author SHA1 Message Date
matthewjheaney
6c9f4d11a1 set version to 1.0.0.6
Change-Id: I259171d7bce7f670cb937707a9864aa0b9eb0d22
2010-11-05 15:21:28 -04:00
matthewjheaney
95c134ad53 ensure const correctness
Change-Id: Ia401d66519d1d663e9630ad1e70f18adec0f0bb5
2010-11-04 19:15:16 -04:00
matthewjheaney
7adec5248a added lacing support
Change-Id: I79ce0b6820f62846b05edbc8a0cb381aa2bc32a6
2010-11-02 11:44:31 -04:00
2 changed files with 356 additions and 116 deletions

View File

@@ -25,7 +25,7 @@ void mkvparser::GetVersion(int& major, int& minor, int& build, int& revision)
major = 1;
minor = 0;
build = 0;
revision = 5;
revision = 6;
}
@@ -707,6 +707,7 @@ long long EBMLHeader::Parse(
if ((total - pos) < len)
return E_FILE_FORMAT_INVALID;
if ((available - pos) < len)
return pos + len; //try again later
@@ -769,7 +770,6 @@ long long EBMLHeader::Parse(
}
assert(pos == end);
return 0;
}
@@ -1029,17 +1029,15 @@ long long Segment::ParseHeaders()
}
#if 0
long Segment::ParseCluster(Cluster*& pCluster, long long& pos_) const
long Segment::ParseCluster(long long& off, long long& new_pos) const
{
pCluster = NULL;
pos_ = -1;
off = -1;
new_pos = -1;
const long long stop = m_start + m_size;
assert(m_pos <= stop);
long long pos = m_pos;
long long off = -1;
while (pos < stop)
{
@@ -1089,8 +1087,8 @@ long Segment::ParseCluster(Cluster*& pCluster, long long& pos_) const
if (off < 0) //we did not found any more clusters
{
pos_ = stop; //pos_ >= 0 here means EOF (cluster is NULL)
return 0; //TODO: confirm this return value
new_pos = stop; //pos >= 0 here means EOF (cluster is NULL)
return 0; //TODO: confirm this return value
}
//We found a cluster. Now read something, to ensure that it is
@@ -1122,7 +1120,7 @@ long Segment::ParseCluster(Cluster*& pCluster, long long& pos_) const
const int result = m_pReader->Read(pos - 1, 1, &b);
assert(result == 0);
pos_ = stop;
new_pos = stop;
}
else
{
@@ -1145,33 +1143,26 @@ long Segment::ParseCluster(Cluster*& pCluster, long long& pos_) const
if (size < 0) //error
return static_cast<long>(size);
pos_ = idpos;
new_pos = idpos;
}
//We found a cluster, and it has been completely loaded into the
//network cache. (We can guarantee this because we actually read
//the EBML tag that follows the cluster, or, if we reached EOF,
//because we actually read the last byte of the cluster).
Segment* const this_ = const_cast<Segment*>(this);
pCluster = Cluster::Parse(this_, m_clusterCount, off);
assert(pCluster);
assert(pCluster->m_index == m_clusterCount);
return 0;
}
bool Segment::AddCluster(Cluster* pCluster, long long pos)
bool Segment::AddCluster(long long off, long long pos)
{
assert(pos >= m_start);
const long long stop = m_start + m_size;
assert(pos <= stop);
if (pCluster)
if (off >= 0)
{
Cluster* const pCluster = Cluster::Parse(this, m_clusterCount, off);
assert(pCluster);
assert(pCluster->m_index == m_clusterCount);
AppendCluster(pCluster);
assert(m_clusters);
assert(m_clusterSize > pCluster->m_index);
@@ -1182,7 +1173,6 @@ bool Segment::AddCluster(Cluster* pCluster, long long pos)
return (pos >= stop);
}
#endif
long Segment::LoadCluster()
@@ -2408,7 +2398,7 @@ long long Segment::Unparsed() const
}
Cluster* Segment::GetFirst()
const Cluster* Segment::GetFirst() const
{
if ((m_clusters == NULL) || (m_clusterCount <= 0))
return &m_eos;
@@ -2420,7 +2410,7 @@ Cluster* Segment::GetFirst()
}
Cluster* Segment::GetLast()
const Cluster* Segment::GetLast() const
{
if ((m_clusters == NULL) || (m_clusterCount <= 0))
return &m_eos;
@@ -2440,7 +2430,7 @@ unsigned long Segment::GetCount() const
}
Cluster* Segment::GetNext(const Cluster* pCurr)
const Cluster* Segment::GetNext(const Cluster* pCurr)
{
assert(pCurr);
assert(pCurr != &m_eos);
@@ -2596,7 +2586,7 @@ Cluster* Segment::GetNext(const Cluster* pCurr)
}
Cluster* Segment::FindCluster(long long time_ns)
const Cluster* Segment::FindCluster(long long time_ns) const
{
if ((m_clusters == NULL) || (m_clusterCount <= 0))
return &m_eos;
@@ -2656,7 +2646,7 @@ Cluster* Segment::FindCluster(long long time_ns)
const BlockEntry* Segment::Seek(
long long time_ns,
const Track* pTrack)
const Track* pTrack) const
{
assert(pTrack);
@@ -3068,7 +3058,7 @@ bool Track::GetLacing() const
long Track::GetFirst(const BlockEntry*& pBlockEntry) const
{
Cluster* pCluster = m_pSegment->GetFirst();
const Cluster* pCluster = m_pSegment->GetFirst();
//If Segment::GetFirst returns NULL, then this must be a network
//download, and we haven't loaded any clusters yet. In this case,
@@ -3129,7 +3119,7 @@ long Track::GetNext(
const Block* const pCurrBlock = pCurrEntry->GetBlock();
assert(pCurrBlock->GetTrackNumber() == m_info.number);
Cluster* pCluster = pCurrEntry->GetCluster();
const Cluster* pCluster = pCurrEntry->GetCluster();
assert(pCluster);
assert(!pCluster->EOS());
@@ -3633,7 +3623,7 @@ Tracks::~Tracks()
}
Track* Tracks::GetTrackByNumber(unsigned long tn_) const
const Track* Tracks::GetTrackByNumber(unsigned long tn_) const
{
const long long tn = tn_;
@@ -3655,7 +3645,7 @@ Track* Tracks::GetTrackByNumber(unsigned long tn_) const
}
Track* Tracks::GetTrackByIndex(unsigned long idx) const
const Track* Tracks::GetTrackByIndex(unsigned long idx) const
{
const ptrdiff_t count = m_trackEntriesEnd - m_trackEntries;
@@ -3666,7 +3656,7 @@ Track* Tracks::GetTrackByIndex(unsigned long idx) const
}
void Cluster::Load()
void Cluster::Load() const
{
assert(m_pSegment);
assert(m_pos);
@@ -3808,7 +3798,7 @@ bool Cluster::EOS() const
}
void Cluster::LoadBlockEntries()
void Cluster::LoadBlockEntries() const
{
if (m_entries)
return;
@@ -3936,14 +3926,14 @@ void Cluster::LoadBlockEntries()
long long Cluster::GetTimeCode()
long long Cluster::GetTimeCode() const
{
Load();
return m_timecode;
}
long long Cluster::GetTime()
long long Cluster::GetTime() const
{
const long long tc = GetTimeCode();
assert(tc >= 0);
@@ -3960,7 +3950,7 @@ long long Cluster::GetTime()
}
long long Cluster::GetFirstTime()
long long Cluster::GetFirstTime() const
{
const BlockEntry* const pEntry = GetFirst();
@@ -3974,7 +3964,7 @@ long long Cluster::GetFirstTime()
}
long long Cluster::GetLastTime()
long long Cluster::GetLastTime() const
{
const BlockEntry* const pEntry = GetLast();
@@ -3988,40 +3978,40 @@ long long Cluster::GetLastTime()
}
void Cluster::ParseBlockGroup(long long start, long long size, size_t index)
void Cluster::ParseBlockGroup(long long st, long long sz, size_t idx) const
{
assert(m_entries);
assert(m_entriesCount);
assert(index < m_entriesCount);
assert(idx < m_entriesCount);
BlockGroup* const pGroup =
new (std::nothrow) BlockGroup(this, index, start, size);
assert(pGroup); //TODO
Cluster* const this_ = const_cast<Cluster*>(this);
m_entries[index] = pGroup;
BlockGroup* const e = new (std::nothrow) BlockGroup(this_, idx, st, sz);
assert(e); //TODO
m_entries[idx] = e;
}
void Cluster::ParseSimpleBlock(long long start, long long size, size_t index)
void Cluster::ParseSimpleBlock(long long st, long long sz, size_t idx) const
{
assert(m_entries);
assert(m_entriesCount);
assert(index < m_entriesCount);
assert(idx < m_entriesCount);
SimpleBlock* const pSimpleBlock =
new (std::nothrow) SimpleBlock(this, index, start, size);
assert(pSimpleBlock); //TODO
Cluster* const this_ = const_cast<Cluster*>(this);
m_entries[index] = pSimpleBlock;
SimpleBlock* const e = new (std::nothrow) SimpleBlock(this_, idx, st, sz);
assert(e); //TODO
m_entries[idx] = e;
}
const BlockEntry* Cluster::GetFirst()
const BlockEntry* Cluster::GetFirst() const
{
LoadBlockEntries();
//assert(m_entries);
//assert(m_entriesCount >= 1);
if ((m_entries == NULL) || (m_entriesCount == 0))
return NULL;
@@ -4033,11 +4023,9 @@ const BlockEntry* Cluster::GetFirst()
}
const BlockEntry* Cluster::GetLast()
const BlockEntry* Cluster::GetLast() const
{
LoadBlockEntries();
//assert(m_entries);
//assert(m_entriesCount >= 1);
if ((m_entries == NULL) || (m_entriesCount == 0))
return NULL;
@@ -4070,7 +4058,7 @@ const BlockEntry* Cluster::GetNext(const BlockEntry* pEntry) const
}
const BlockEntry* Cluster::GetEntry(const Track* pTrack)
const BlockEntry* Cluster::GetEntry(const Track* pTrack) const
{
assert(pTrack);
@@ -4110,7 +4098,7 @@ const BlockEntry* Cluster::GetEntry(const Track* pTrack)
const BlockEntry*
Cluster::GetEntry(
const CuePoint& cp,
const CuePoint::TrackPosition& tp)
const CuePoint::TrackPosition& tp) const
{
assert(m_pSegment);
@@ -4200,7 +4188,7 @@ Cluster::GetEntry(
}
const BlockEntry* Cluster::GetMaxKey(const VideoTrack* pTrack)
const BlockEntry* Cluster::GetMaxKey(const VideoTrack* pTrack) const
{
assert(pTrack);
@@ -4208,7 +4196,6 @@ const BlockEntry* Cluster::GetMaxKey(const VideoTrack* pTrack)
return pTrack->GetEOS();
LoadBlockEntries();
//assert(m_entries);
BlockEntry** i = m_entries + m_entriesCount;
BlockEntry** const j = m_entries;
@@ -4262,7 +4249,7 @@ bool SimpleBlock::EOS() const
}
Cluster* SimpleBlock::GetCluster() const
const Cluster* SimpleBlock::GetCluster() const
{
return m_pCluster;
}
@@ -4389,7 +4376,7 @@ bool BlockGroup::EOS() const
}
Cluster* BlockGroup::GetCluster() const
const Cluster* BlockGroup::GetCluster() const
{
return m_pCluster;
}
@@ -4425,7 +4412,6 @@ short BlockGroup::GetNextTimeCode() const
//}
Block::Block(long long start, long long size_, IMkvReader* pReader) :
m_start(start),
m_size(size_)
@@ -4447,30 +4433,238 @@ Block::Block(long long start, long long size_, IMkvReader* pReader) :
pos += 2;
assert((stop - pos) >= 1);
const long hr = pReader->Read(pos, 1, &m_flags);
assert(hr == 0L);
long status = pReader->Read(pos, 1, &m_flags);
assert(status == 0);
const int invisible = int(m_flags & 0x08);
const int invisible = int(m_flags & 0x08) >> 3;
invisible;
assert(!invisible);
assert(!invisible); //TODO
const int lacing = int(m_flags & 0x06);
lacing;
assert(!lacing);
const int lacing = int(m_flags & 0x06) >> 1;
++pos;
++pos; //consume flags byte
assert(pos <= stop);
m_frameOff = pos;
if (lacing == 0) //no lacing
{
m_frame_count = 1;
m_frames = new Frame[m_frame_count];
const long long frame_size = stop - pos;
assert(frame_size <= LONG_MAX);
Frame& f = m_frames[0];
f.pos = pos;
m_frameSize = static_cast<long>(frame_size);
const long long frame_size = stop - pos;
assert(frame_size <= LONG_MAX);
f.len = static_cast<long>(frame_size);
return;
}
assert(pos < stop);
unsigned char count;
status = pReader->Read(pos, 1, &count);
assert(status == 0);
++pos; //consume frame count
assert(pos <= stop);
m_frame_count = ++count;
m_frames = new Frame[m_frame_count];
if (lacing == 1) //Xiph
{
Frame* pf = m_frames;
Frame* const pf_end = pf + m_frame_count;
long size = 0;
while (count > 1)
{
long frame_size = 0;
for (;;)
{
unsigned char val;
status = pReader->Read(pos, 1, &val);
assert(status == 0);
++pos; //consume xiph size byte
frame_size += val;
if (val < 255)
break;
}
Frame& f = *pf++;
assert(pf < pf_end);
f.len = frame_size;
size += frame_size; //contribution of this frame
--count;
}
assert(pf < pf_end);
assert(pos < stop);
{
Frame& f = *pf++;
assert(pf == pf_end);
const long long total_size = stop - pos;
assert(total_size > size);
const long long frame_size = total_size - size;
assert(frame_size <= LONG_MAX);
f.len = static_cast<long>(frame_size);
}
pf = m_frames;
while (pf != pf_end)
{
Frame& f = *pf++;
assert((pos + f.len) <= stop);
f.pos = pos;
pos += f.len;
}
assert(pos == stop);
}
else if (lacing == 2) //fixed-size lacing
{
const long long total_size = stop - pos;
assert((total_size % m_frame_count) == 0);
const long long frame_size = total_size / m_frame_count;
assert(frame_size <= LONG_MAX);
Frame* pf = m_frames;
Frame* const pf_end = pf + m_frame_count;
while (pf != pf_end)
{
assert((pos + frame_size) <= stop);
Frame& f = *pf++;
f.pos = pos;
f.len = static_cast<long>(frame_size);
pos += frame_size;
}
assert(pos == stop);
}
else
{
assert(lacing == 3); //EBML lacing
assert(pos < stop);
long size = 0;
long long frame_size = ReadUInt(pReader, pos, len);
assert(frame_size > 0);
assert(frame_size <= LONG_MAX);
assert((pos + len) <= stop);
pos += len; //consume length of size of first frame
assert((pos + frame_size) <= stop);
Frame* pf = m_frames;
Frame* const pf_end = pf + m_frame_count;
{
Frame& curr = *pf;
curr.len = static_cast<long>(frame_size);
size += curr.len; //contribution of this frame
}
--count;
while (count > 1)
{
assert(pos < stop);
assert(pf < pf_end);
const Frame& prev = *pf++;
assert(pf < pf_end);
assert(prev.len == frame_size);
Frame& curr = *pf;
const long long delta_size_ = ReadUInt(pReader, pos, len);
assert(delta_size_ >= 0);
assert((pos + len) <= stop);
pos += len; //consume length of (delta) size
assert(pos <= stop);
const int exp = 7*len - 1;
const long long bias = (1LL << exp) - 1LL;
const long long delta_size = delta_size_ - bias;
frame_size += delta_size;
assert(frame_size > 0);
assert(frame_size <= LONG_MAX);
curr.len = static_cast<long>(frame_size);
size += curr.len; //contribution of this frame
--count;
}
{
assert(pos < stop);
assert(pf < pf_end);
const Frame& prev = *pf++;
assert(pf < pf_end);
assert(prev.len == frame_size);
Frame& curr = *pf++;
assert(pf == pf_end);
const long long total_size = stop - pos;
assert(total_size > 0);
assert(total_size > size);
frame_size = total_size - size;
assert(frame_size > 0);
assert(frame_size <= LONG_MAX);
curr.len = static_cast<long>(frame_size);
}
pf = m_frames;
while (pf != pf_end)
{
Frame& f = *pf++;
assert((pos + f.len) <= stop);
f.pos = pos;
pos += f.len;
}
assert(pos == stop);
}
}
long long Block::GetTimeCode(Cluster* pCluster) const
Block::~Block()
{
delete[] m_frames;
}
long long Block::GetTimeCode(const Cluster* pCluster) const
{
assert(pCluster);
@@ -4484,7 +4678,7 @@ long long Block::GetTimeCode(Cluster* pCluster) const
}
long long Block::GetTime(Cluster* pCluster) const
long long Block::GetTime(const Cluster* pCluster) const
{
assert(pCluster);
@@ -4524,6 +4718,7 @@ void Block::SetKey(bool bKey)
}
#if 0
long long Block::GetOffset() const
{
return m_frameOff;
@@ -4535,10 +4730,8 @@ long Block::GetSize() const
return m_frameSize;
}
long Block::Read(IMkvReader* pReader, unsigned char* buf) const
{
assert(pReader);
assert(buf);
@@ -4546,6 +4739,35 @@ long Block::Read(IMkvReader* pReader, unsigned char* buf) const
return hr;
}
#else
int Block::GetFrameCount() const
{
return m_frame_count;
}
const Block::Frame& Block::GetFrame(int idx) const
{
assert(idx >= 0);
assert(idx < m_frame_count);
const Frame& f = m_frames[idx];
assert(f.pos > 0);
assert(f.len > 0);
return f;
}
long Block::Frame::Read(IMkvReader* pReader, unsigned char* buf) const
{
assert(pReader);
assert(buf);
const long status = pReader->Read(pos, len, buf);
return status;
}
#endif
} //end namespace mkvparser

View File

@@ -73,23 +73,44 @@ public:
const long long m_size;
Block(long long start, long long size, IMkvReader*);
~Block();
long long GetTrackNumber() const;
long long GetTimeCode(Cluster*) const; //absolute, but not scaled
long long GetTime(Cluster*) const; //absolute, and scaled (ns units)
long long GetTimeCode(const Cluster*) const; //absolute, but not scaled
long long GetTime(const Cluster*) const; //absolute, and scaled (ns)
bool IsKey() const;
void SetKey(bool);
int GetFrameCount() const; //to index frames: [0, count)
struct Frame
{
long long pos; //absolute offset
long len;
long Read(IMkvReader*, unsigned char*) const;
};
#if 0
long long GetOffset() const;
long GetSize() const;
long Read(IMkvReader*, unsigned char*) const;
#else
const Frame& GetFrame(int frame_index) const;
#endif
private:
long long m_track; //Track::Number()
short m_timecode; //relative to cluster
unsigned char m_flags;
#if 0
long long m_frameOff;
long m_frameSize;
#else
Frame* m_frames;
int m_frame_count;
#endif
};
@@ -102,7 +123,7 @@ class BlockEntry
public:
virtual ~BlockEntry();
virtual bool EOS() const = 0;
virtual Cluster* GetCluster() const = 0;
virtual const Cluster* GetCluster() const = 0;
virtual size_t GetIndex() const = 0;
virtual const Block* GetBlock() const = 0;
//virtual bool IsBFrame() const = 0;
@@ -122,7 +143,7 @@ public:
SimpleBlock(Cluster*, size_t, long long start, long long size);
bool EOS() const;
Cluster* GetCluster() const;
const Cluster* GetCluster() const;
size_t GetIndex() const;
const Block* GetBlock() const;
//bool IsBFrame() const;
@@ -145,7 +166,7 @@ public:
~BlockGroup();
bool EOS() const;
Cluster* GetCluster() const;
const Cluster* GetCluster() const;
size_t GetIndex() const;
const Block* GetBlock() const;
//bool IsBFrame() const;
@@ -297,8 +318,8 @@ public:
Tracks(Segment*, long long start, long long size);
virtual ~Tracks();
Track* GetTrackByNumber(unsigned long tn) const;
Track* GetTrackByIndex(unsigned long idx) const;
const Track* GetTrackByNumber(unsigned long tn) const;
const Track* GetTrackByIndex(unsigned long idx) const;
private:
Track** m_trackEntries;
@@ -408,7 +429,6 @@ public:
const CuePoint* GetFirst() const;
const CuePoint* GetLast() const;
const CuePoint* GetNext(const CuePoint*) const;
const BlockEntry* GetBlock(
@@ -444,19 +464,19 @@ public:
bool EOS() const;
long long GetTimeCode(); //absolute, but not scaled
long long GetTime(); //absolute, and scaled (nanosecond units)
long long GetFirstTime(); //time (ns) of first (earliest) block
long long GetLastTime(); //time (ns) of last (latest) block
long long GetTimeCode() const; //absolute, but not scaled
long long GetTime() const; //absolute, and scaled (nanosecond units)
long long GetFirstTime() const; //time (ns) of first (earliest) block
long long GetLastTime() const; //time (ns) of last (latest) block
const BlockEntry* GetFirst();
const BlockEntry* GetLast();
const BlockEntry* GetFirst() const;
const BlockEntry* GetLast() const;
const BlockEntry* GetNext(const BlockEntry*) const;
const BlockEntry* GetEntry(const Track*);
const BlockEntry* GetEntry(const Track*) const;
const BlockEntry* GetEntry(
const CuePoint&,
const CuePoint::TrackPosition&);
const BlockEntry* GetMaxKey(const VideoTrack*);
const CuePoint::TrackPosition&) const;
const BlockEntry* GetMaxKey(const VideoTrack*) const;
protected:
Cluster(Segment*, long, long long off);
@@ -464,18 +484,18 @@ protected:
public:
//TODO: these should all be private, with public selector functions
long m_index;
long long m_pos;
long long m_size;
mutable long long m_pos;
mutable long long m_size;
private:
long long m_timecode;
BlockEntry** m_entries;
size_t m_entriesCount;
mutable long long m_timecode;
mutable BlockEntry** m_entries;
mutable size_t m_entriesCount;
void Load();
void LoadBlockEntries();
void ParseBlockGroup(long long, long long, size_t);
void ParseSimpleBlock(long long, long long, size_t);
void Load() const;
void LoadBlockEntries() const;
void ParseBlockGroup(long long, long long, size_t) const;
void ParseSimpleBlock(long long, long long, size_t) const;
};
@@ -506,12 +526,10 @@ public:
long long ParseHeaders(); //stops when first cluster is found
long LoadCluster(); //loads one cluster
#if 0
//This pair parses one cluster, but only changes the state of the
//segment object when the cluster is actually added to the index.
long ParseCluster(Cluster*&, long long& newpos) const;
bool AddCluster(Cluster*, long long);
#endif
long ParseCluster(long long& cluster_pos, long long& new_pos) const;
bool AddCluster(long long cluster_pos, long long new_pos);
Tracks* GetTracks() const;
const SegmentInfo* GetInfo() const;
@@ -520,12 +538,12 @@ public:
long long GetDuration() const;
unsigned long GetCount() const;
Cluster* GetFirst();
Cluster* GetLast();
Cluster* GetNext(const Cluster*);
const Cluster* GetFirst() const;
const Cluster* GetLast() const;
const Cluster* GetNext(const Cluster*);
Cluster* FindCluster(long long time_nanoseconds);
const BlockEntry* Seek(long long time_nanoseconds, const Track*);
const Cluster* FindCluster(long long time_nanoseconds) const;
const BlockEntry* Seek(long long time_nanoseconds, const Track*) const;
private: