Compare commits

...

9 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
matthewjheaney
093b78faf2 set version to 1.0.0.5
Change-Id: I3643a0019110b1ff7359a8d414bf0cd2ba9b5d54
2010-10-29 14:31:47 -04:00
matthewjheaney
ed90de0d52 check lacing bits
Change-Id: Iaa3a65429b7f3211868a86bbb83ce8018350554a
2010-10-29 14:04:43 -04:00
matthewjheaney
a01e568293 removed IsBFrame selector
Change-Id: Iae51165c318997a9131e9af5c667cfac1f37e773
2010-10-28 17:28:59 -04:00
matthewjheaney
d7ce23a019 handle empty clusters when seeking
Change-Id: I9bb39ff95e308639402e1c7f9aec59c81350d091
2010-10-28 14:45:27 -04:00
matthewjheaney
acf7ddb273 handle empty clusters
Change-Id: I57b085367e1b900acb0ddd6ee419e317f261718a
2010-10-27 16:47:14 -04:00
matthewjheaney
fc12207e15 handle case when no duration
Change-Id: I694f6ce6ae9ecc6fc3b90b954dd7041e82fb9ac0
2010-10-22 13:46:39 -04:00
2 changed files with 494 additions and 141 deletions

View File

@@ -10,6 +10,7 @@
#include <cassert> #include <cassert>
#include <cstring> #include <cstring>
#include <new> #include <new>
#include <climits>
//#include <windows.h> //#include <windows.h>
//#include "odbgstream.hpp" //#include "odbgstream.hpp"
//using std::endl; //using std::endl;
@@ -24,7 +25,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 = 4; revision = 6;
} }
@@ -706,6 +707,7 @@ long long EBMLHeader::Parse(
if ((total - pos) < len) if ((total - pos) < len)
return E_FILE_FORMAT_INVALID; return E_FILE_FORMAT_INVALID;
if ((available - pos) < len) if ((available - pos) < len)
return pos + len; //try again later return pos + len; //try again later
@@ -768,7 +770,6 @@ long long EBMLHeader::Parse(
} }
assert(pos == end); assert(pos == end);
return 0; return 0;
} }
@@ -1028,17 +1029,15 @@ long long Segment::ParseHeaders()
} }
#if 0 long Segment::ParseCluster(long long& off, long long& new_pos) const
long Segment::ParseCluster(Cluster*& pCluster, long long& pos_) const
{ {
pCluster = NULL; off = -1;
pos_ = -1; new_pos = -1;
const long long stop = m_start + m_size; const long long stop = m_start + m_size;
assert(m_pos <= stop); assert(m_pos <= stop);
long long pos = m_pos; long long pos = m_pos;
long long off = -1;
while (pos < stop) while (pos < stop)
{ {
@@ -1088,8 +1087,8 @@ long Segment::ParseCluster(Cluster*& pCluster, long long& pos_) const
if (off < 0) //we did not found any more clusters if (off < 0) //we did not found any more clusters
{ {
pos_ = stop; //pos_ >= 0 here means EOF (cluster is NULL) new_pos = stop; //pos >= 0 here means EOF (cluster is NULL)
return 0; //TODO: confirm this return value return 0; //TODO: confirm this return value
} }
//We found a cluster. Now read something, to ensure that it is //We found a cluster. Now read something, to ensure that it is
@@ -1121,7 +1120,7 @@ long Segment::ParseCluster(Cluster*& pCluster, long long& pos_) const
const int result = m_pReader->Read(pos - 1, 1, &b); const int result = m_pReader->Read(pos - 1, 1, &b);
assert(result == 0); assert(result == 0);
pos_ = stop; new_pos = stop;
} }
else else
{ {
@@ -1144,33 +1143,26 @@ long Segment::ParseCluster(Cluster*& pCluster, long long& pos_) const
if (size < 0) //error if (size < 0) //error
return static_cast<long>(size); 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; return 0;
} }
bool Segment::AddCluster(Cluster* pCluster, long long pos) bool Segment::AddCluster(long long off, long long pos)
{ {
assert(pos >= m_start); assert(pos >= m_start);
const long long stop = m_start + m_size; const long long stop = m_start + m_size;
assert(pos <= stop); 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); AppendCluster(pCluster);
assert(m_clusters); assert(m_clusters);
assert(m_clusterSize > pCluster->m_index); assert(m_clusterSize > pCluster->m_index);
@@ -1181,7 +1173,6 @@ bool Segment::AddCluster(Cluster* pCluster, long long pos)
return (pos >= stop); return (pos >= stop);
} }
#endif
long Segment::LoadCluster() long Segment::LoadCluster()
@@ -2033,6 +2024,50 @@ bool Cues::FindNext(
#endif #endif
const CuePoint* Cues::GetFirst() const
{
LoadCuePoint(); //init cues
const size_t count = m_count + m_preload_count;
if (count == 0) //weird
return NULL;
CuePoint* const* const pp = m_cue_points;
assert(pp);
CuePoint* const pCP = pp[0];
assert(pCP);
assert(pCP->GetTimeCode() >= 0);
return pCP;
}
const CuePoint* Cues::GetLast() const
{
LoadCuePoint(); //init cues
const size_t count = m_count + m_preload_count;
if (count == 0) //weird
return NULL;
const size_t index = count - 1;
CuePoint* const* const pp = m_cue_points;
assert(pp);
CuePoint* const pCP = pp[index];
assert(pCP);
pCP->Load(m_pSegment->m_pReader);
assert(pCP->GetTimeCode() >= 0);
return pCP;
}
const CuePoint* Cues::GetNext(const CuePoint* pCurr) const const CuePoint* Cues::GetNext(const CuePoint* pCurr) const
{ {
if (pCurr == NULL) if (pCurr == NULL)
@@ -2304,8 +2339,8 @@ void CuePoint::TrackPosition::Parse(
} }
assert(m_pos >= 0); assert(m_pos >= 0);
//assert(m_track > 0); assert(m_track > 0);
//assert(m_block > 0); assert(m_block > 0);
} }
@@ -2363,7 +2398,7 @@ long long Segment::Unparsed() const
} }
Cluster* Segment::GetFirst() const Cluster* Segment::GetFirst() const
{ {
if ((m_clusters == NULL) || (m_clusterCount <= 0)) if ((m_clusters == NULL) || (m_clusterCount <= 0))
return &m_eos; return &m_eos;
@@ -2375,7 +2410,7 @@ Cluster* Segment::GetFirst()
} }
Cluster* Segment::GetLast() const Cluster* Segment::GetLast() const
{ {
if ((m_clusters == NULL) || (m_clusterCount <= 0)) if ((m_clusters == NULL) || (m_clusterCount <= 0))
return &m_eos; return &m_eos;
@@ -2395,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);
assert(pCurr != &m_eos); assert(pCurr != &m_eos);
@@ -2551,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)) if ((m_clusters == NULL) || (m_clusterCount <= 0))
return &m_eos; return &m_eos;
@@ -2611,7 +2646,7 @@ Cluster* Segment::FindCluster(long long time_ns)
const BlockEntry* Segment::Seek( const BlockEntry* Segment::Seek(
long long time_ns, long long time_ns,
const Track* pTrack) const Track* pTrack) const
{ {
assert(pTrack); assert(pTrack);
@@ -2672,11 +2707,21 @@ const BlockEntry* Segment::Seek(
assert(lo > i); assert(lo > i);
assert(lo <= j); assert(lo <= j);
Cluster* const pCluster = *--lo; while (lo > i)
assert(pCluster); {
assert(pCluster->GetTime() <= time_ns); Cluster* const pCluster = *--lo;
assert(pCluster);
assert(pCluster->GetTime() <= time_ns);
return pCluster->GetEntry(pTrack); const BlockEntry* const pBE = pCluster->GetEntry(pTrack);
if ((pBE != 0) && !pBE->EOS())
return pBE;
//landed on empty cluster (no entries)
}
return pTrack->GetEOS(); //weird
} }
assert(pTrack->GetType() == 1); //video assert(pTrack->GetType() == 1); //video
@@ -2717,17 +2762,16 @@ const BlockEntry* Segment::Seek(
{ {
const BlockEntry* const pBlockEntry = pCluster->GetEntry(pTrack); const BlockEntry* const pBlockEntry = pCluster->GetEntry(pTrack);
assert(pBlockEntry);
if (!pBlockEntry->EOS()) //found a keyframe if ((pBlockEntry != 0) && !pBlockEntry->EOS()) //found a keyframe
{ {
const Block* const pBlock = pBlockEntry->GetBlock(); const Block* const pBlock = pBlockEntry->GetBlock();
assert(pBlock); assert(pBlock);
//TODO: this isn't necessarily the keyframe we want, //NOTE: this isn't necessarily the keyframe we want,
//since there might another keyframe on this same //since there might another keyframe on this same
//cluster with a greater timecode that but that is //cluster with a greater timecode, that is still
//still less than the requested time. For now we //less than the requested time. For now we
//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)
@@ -2744,9 +2788,8 @@ const BlockEntry* Segment::Seek(
assert(pCluster->GetTime() <= time_ns); assert(pCluster->GetTime() <= time_ns);
const BlockEntry* const pBlockEntry = pCluster->GetMaxKey(pVideo); const BlockEntry* const pBlockEntry = pCluster->GetMaxKey(pVideo);
assert(pBlockEntry);
if (!pBlockEntry->EOS()) if ((pBlockEntry != 0) && !pBlockEntry->EOS())
return pBlockEntry; return pBlockEntry;
} }
@@ -2827,7 +2870,7 @@ SegmentInfo::SegmentInfo(Segment* pSegment, long long start, long long size_) :
const long long stop = start + size_; const long long stop = start + size_;
m_timecodeScale = 1000000; m_timecodeScale = 1000000;
m_duration = 0; m_duration = -1;
while (pos < stop) while (pos < stop)
{ {
@@ -2843,8 +2886,9 @@ SegmentInfo::SegmentInfo(Segment* pSegment, long long start, long long size_) :
else if (Match(pReader, pos, 0x1741, m_pWritingAppAsUTF8)) //[57][41] else if (Match(pReader, pos, 0x1741, m_pWritingAppAsUTF8)) //[57][41]
assert(m_pWritingAppAsUTF8); assert(m_pWritingAppAsUTF8);
else if (Match(pReader, pos, 0x3BA9, m_pTitleAsUTF8)) //[7B][A9] else if (Match(pReader, pos, 0x3BA9, m_pTitleAsUTF8)) //[7B][A9]
assert(m_pTitleAsUTF8); assert(m_pTitleAsUTF8);
else else
{ {
long len; long len;
@@ -2898,7 +2942,9 @@ long long SegmentInfo::GetTimeCodeScale() const
long long SegmentInfo::GetDuration() const long long SegmentInfo::GetDuration() const
{ {
assert(m_duration >= 0); if (m_duration < 0)
return -1;
assert(m_timecodeScale >= 1); assert(m_timecodeScale >= 1);
const double dd = double(m_duration) * double(m_timecodeScale); const double dd = double(m_duration) * double(m_timecodeScale);
@@ -2944,6 +2990,7 @@ Track::Info::Info():
codecPrivate(NULL), codecPrivate(NULL),
codecPrivateSize(0), codecPrivateSize(0),
codecNameAsUTF8(NULL) codecNameAsUTF8(NULL)
//lacing(false)
{ {
} }
@@ -3003,9 +3050,15 @@ const unsigned char* Track::GetCodecPrivate(size_t& size) const
} }
bool Track::GetLacing() const
{
return m_info.lacing;
}
long Track::GetFirst(const BlockEntry*& pBlockEntry) 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 //If Segment::GetFirst returns NULL, then this must be a network
//download, and we haven't loaded any clusters yet. In this case, //download, and we haven't loaded any clusters yet. In this case,
@@ -3066,7 +3119,7 @@ long Track::GetNext(
const Block* const pCurrBlock = pCurrEntry->GetBlock(); const Block* const pCurrBlock = pCurrEntry->GetBlock();
assert(pCurrBlock->GetTrackNumber() == m_info.number); assert(pCurrBlock->GetTrackNumber() == m_info.number);
Cluster* pCluster = pCurrEntry->GetCluster(); const Cluster* pCluster = pCurrEntry->GetCluster();
assert(pCluster); assert(pCluster);
assert(!pCluster->EOS()); assert(!pCluster->EOS());
@@ -3406,7 +3459,13 @@ Tracks::Tracks(Segment* pSegment, long long start, long long size_) :
//pos now desinates start of element //pos now desinates start of element
if (id == 0x2E) //TrackEntry ID if (id == 0x2E) //TrackEntry ID
ParseTrackEntry(pos, size1, *m_trackEntriesEnd++); {
Track*& pTrack = *m_trackEntriesEnd;
ParseTrackEntry(pos, size1, pTrack);
if (pTrack)
++m_trackEntriesEnd;
}
pos += size1; //consume payload pos += size1; //consume payload
assert(pos <= stop); assert(pos <= stop);
@@ -3441,6 +3500,8 @@ void Tracks::ParseTrackEntry(
Track::Settings audioSettings; Track::Settings audioSettings;
audioSettings.start = -1; audioSettings.start = -1;
long long lacing = 1; //default is true
while (pos < stop) while (pos < stop)
{ {
#ifdef _DEBUG #ifdef _DEBUG
@@ -3459,6 +3520,8 @@ void Tracks::ParseTrackEntry(
assert(i.nameAsUTF8); assert(i.nameAsUTF8);
else if (Match(pReader, pos, 0x06, i.codecId)) else if (Match(pReader, pos, 0x06, i.codecId))
; ;
else if (Match(pReader, pos, 0x1C, lacing))
assert(lacing <= 1);
else if (Match(pReader, else if (Match(pReader,
pos, pos,
0x23A2, 0x23A2,
@@ -3505,6 +3568,8 @@ void Tracks::ParseTrackEntry(
//and that it is unique among all tracks. //and that it is unique among all tracks.
assert(i.number > 0); assert(i.number > 0);
i.lacing = (lacing > 0) ? true : false;
//TODO: vet settings, to ensure that video settings (0x60) //TODO: vet settings, to ensure that video settings (0x60)
//were specified when type = 1, and that audio settings (0x61) //were specified when type = 1, and that audio settings (0x61)
//were specified when type = 2. //were specified when type = 2.
@@ -3558,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_; const long long tn = tn_;
@@ -3580,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; const ptrdiff_t count = m_trackEntriesEnd - m_trackEntries;
@@ -3591,7 +3656,7 @@ Track* Tracks::GetTrackByIndex(unsigned long idx) const
} }
void Cluster::Load() void Cluster::Load() const
{ {
assert(m_pSegment); assert(m_pSegment);
assert(m_pos); assert(m_pos);
@@ -3733,7 +3798,7 @@ bool Cluster::EOS() const
} }
void Cluster::LoadBlockEntries() void Cluster::LoadBlockEntries() const
{ {
if (m_entries) if (m_entries)
return; return;
@@ -3861,14 +3926,14 @@ void Cluster::LoadBlockEntries()
long long Cluster::GetTimeCode() long long Cluster::GetTimeCode() const
{ {
Load(); Load();
return m_timecode; return m_timecode;
} }
long long Cluster::GetTime() long long Cluster::GetTime() const
{ {
const long long tc = GetTimeCode(); const long long tc = GetTimeCode();
assert(tc >= 0); assert(tc >= 0);
@@ -3885,7 +3950,7 @@ long long Cluster::GetTime()
} }
long long Cluster::GetFirstTime() long long Cluster::GetFirstTime() const
{ {
const BlockEntry* const pEntry = GetFirst(); const BlockEntry* const pEntry = GetFirst();
@@ -3899,43 +3964,57 @@ long long Cluster::GetFirstTime()
} }
long long Cluster::GetLastTime() const
{
const BlockEntry* const pEntry = GetLast();
void Cluster::ParseBlockGroup(long long start, long long size, size_t index) if (pEntry == NULL) //empty cluster
return GetTime();
const Block* const pBlock = pEntry->GetBlock();
assert(pBlock);
return pBlock->GetTime(this);
}
void Cluster::ParseBlockGroup(long long st, long long sz, size_t idx) const
{ {
assert(m_entries); assert(m_entries);
assert(m_entriesCount); assert(m_entriesCount);
assert(index < m_entriesCount); assert(idx < m_entriesCount);
BlockGroup* const pGroup = Cluster* const this_ = const_cast<Cluster*>(this);
new (std::nothrow) BlockGroup(this, index, start, size);
assert(pGroup); //TODO
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_entries);
assert(m_entriesCount); assert(m_entriesCount);
assert(index < m_entriesCount); assert(idx < m_entriesCount);
SimpleBlock* const pSimpleBlock = Cluster* const this_ = const_cast<Cluster*>(this);
new (std::nothrow) SimpleBlock(this, index, start, size);
assert(pSimpleBlock); //TODO
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
{ {
//TODO: handle empty cluster
LoadBlockEntries(); LoadBlockEntries();
assert(m_entries);
assert(m_entriesCount >= 1); if ((m_entries == NULL) || (m_entriesCount == 0))
return NULL;
const BlockEntry* const pFirst = m_entries[0]; const BlockEntry* const pFirst = m_entries[0];
assert(pFirst); assert(pFirst);
@@ -3944,13 +4023,12 @@ const BlockEntry* Cluster::GetFirst()
} }
const BlockEntry* Cluster::GetLast() const BlockEntry* Cluster::GetLast() const
{ {
//TODO: handle empty cluster
LoadBlockEntries(); LoadBlockEntries();
assert(m_entries);
assert(m_entriesCount >= 1); if ((m_entries == NULL) || (m_entriesCount == 0))
return NULL;
const size_t idx = m_entriesCount - 1; const size_t idx = m_entriesCount - 1;
@@ -3980,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); assert(pTrack);
@@ -3989,6 +4067,9 @@ const BlockEntry* Cluster::GetEntry(const Track* pTrack)
LoadBlockEntries(); LoadBlockEntries();
if ((m_entries == NULL) || (m_entriesCount == 0))
return NULL;
BlockEntry** i = m_entries; BlockEntry** i = m_entries;
assert(i); assert(i);
@@ -4017,7 +4098,7 @@ const BlockEntry* Cluster::GetEntry(const Track* pTrack)
const BlockEntry* const BlockEntry*
Cluster::GetEntry( Cluster::GetEntry(
const CuePoint& cp, const CuePoint& cp,
const CuePoint::TrackPosition& tp) const CuePoint::TrackPosition& tp) const
{ {
assert(m_pSegment); assert(m_pSegment);
@@ -4057,6 +4138,11 @@ Cluster::GetEntry(
while (i != j) while (i != j)
{ {
#ifdef _DEBUG
const ptrdiff_t idx = i - m_entries;
idx;
#endif
const BlockEntry* const pEntry = *i++; const BlockEntry* const pEntry = *i++;
assert(pEntry); assert(pEntry);
assert(!pEntry->EOS()); assert(!pEntry->EOS());
@@ -4102,7 +4188,7 @@ Cluster::GetEntry(
} }
const BlockEntry* Cluster::GetMaxKey(const VideoTrack* pTrack) const BlockEntry* Cluster::GetMaxKey(const VideoTrack* pTrack) const
{ {
assert(pTrack); assert(pTrack);
@@ -4110,7 +4196,6 @@ const BlockEntry* Cluster::GetMaxKey(const VideoTrack* pTrack)
return pTrack->GetEOS(); return pTrack->GetEOS();
LoadBlockEntries(); LoadBlockEntries();
assert(m_entries);
BlockEntry** i = m_entries + m_entriesCount; BlockEntry** i = m_entries + m_entriesCount;
BlockEntry** const j = m_entries; BlockEntry** const j = m_entries;
@@ -4164,7 +4249,7 @@ bool SimpleBlock::EOS() const
} }
Cluster* SimpleBlock::GetCluster() const const Cluster* SimpleBlock::GetCluster() const
{ {
return m_pCluster; return m_pCluster;
} }
@@ -4182,10 +4267,10 @@ const Block* SimpleBlock::GetBlock() const
} }
bool SimpleBlock::IsBFrame() const //bool SimpleBlock::IsBFrame() const
{ //{
return false; // return false;
} //}
BlockGroup::BlockGroup( BlockGroup::BlockGroup(
@@ -4291,7 +4376,7 @@ bool BlockGroup::EOS() const
} }
Cluster* BlockGroup::GetCluster() const const Cluster* BlockGroup::GetCluster() const
{ {
return m_pCluster; return m_pCluster;
} }
@@ -4321,11 +4406,10 @@ short BlockGroup::GetNextTimeCode() const
} }
bool BlockGroup::IsBFrame() const //bool BlockGroup::IsBFrame() const
{ //{
return (m_nextTimeCode > 0); // return (m_nextTimeCode > 0);
} //}
Block::Block(long long start, long long size_, IMkvReader* pReader) : Block::Block(long long start, long long size_, IMkvReader* pReader) :
@@ -4349,23 +4433,238 @@ Block::Block(long long start, long long size_, IMkvReader* pReader) :
pos += 2; pos += 2;
assert((stop - pos) >= 1); assert((stop - pos) >= 1);
const long hr = pReader->Read(pos, 1, &m_flags); long status = pReader->Read(pos, 1, &m_flags);
assert(hr == 0L); assert(status == 0);
++pos; const int invisible = int(m_flags & 0x08) >> 3;
invisible;
assert(!invisible); //TODO
const int lacing = int(m_flags & 0x06) >> 1;
++pos; //consume flags byte
assert(pos <= stop); 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; Frame& f = m_frames[0];
f.pos = pos;
assert(frame_size <= 2147483647L); const long long frame_size = stop - pos;
assert(frame_size <= LONG_MAX);
m_frameSize = static_cast<long>(frame_size); 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); assert(pCluster);
@@ -4379,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); assert(pCluster);
@@ -4419,6 +4718,7 @@ void Block::SetKey(bool bKey)
} }
#if 0
long long Block::GetOffset() const long long Block::GetOffset() const
{ {
return m_frameOff; return m_frameOff;
@@ -4430,10 +4730,8 @@ long Block::GetSize() const
return m_frameSize; return m_frameSize;
} }
long Block::Read(IMkvReader* pReader, unsigned char* buf) const long Block::Read(IMkvReader* pReader, unsigned char* buf) const
{ {
assert(pReader); assert(pReader);
assert(buf); assert(buf);
@@ -4441,6 +4739,35 @@ long Block::Read(IMkvReader* pReader, unsigned char* buf) const
return hr; 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 } //end namespace mkvparser

View File

@@ -73,23 +73,44 @@ public:
const long long m_size; const long long m_size;
Block(long long start, long long size, IMkvReader*); Block(long long start, long long size, IMkvReader*);
~Block();
long long GetTrackNumber() const; long long GetTrackNumber() const;
long long GetTimeCode(Cluster*) const; //absolute, but not scaled long long GetTimeCode(const Cluster*) const; //absolute, but not scaled
long long GetTime(Cluster*) const; //absolute, and scaled (ns units) long long GetTime(const Cluster*) const; //absolute, and scaled (ns)
bool IsKey() const; bool IsKey() const;
void SetKey(bool); 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 long GetOffset() const;
long GetSize() const; long GetSize() const;
long Read(IMkvReader*, unsigned char*) const; long Read(IMkvReader*, unsigned char*) const;
#else
const Frame& GetFrame(int frame_index) const;
#endif
private: private:
long long m_track; //Track::Number() long long m_track; //Track::Number()
short m_timecode; //relative to cluster short m_timecode; //relative to cluster
unsigned char m_flags; unsigned char m_flags;
#if 0
long long m_frameOff; long long m_frameOff;
long m_frameSize; long m_frameSize;
#else
Frame* m_frames;
int m_frame_count;
#endif
}; };
@@ -102,10 +123,10 @@ class BlockEntry
public: public:
virtual ~BlockEntry(); virtual ~BlockEntry();
virtual bool EOS() const = 0; virtual bool EOS() const = 0;
virtual Cluster* GetCluster() const = 0; virtual const Cluster* GetCluster() const = 0;
virtual size_t GetIndex() const = 0; virtual size_t GetIndex() const = 0;
virtual const Block* GetBlock() const = 0; virtual const Block* GetBlock() const = 0;
virtual bool IsBFrame() const = 0; //virtual bool IsBFrame() const = 0;
protected: protected:
BlockEntry(); BlockEntry();
@@ -122,10 +143,10 @@ public:
SimpleBlock(Cluster*, size_t, long long start, long long size); SimpleBlock(Cluster*, size_t, long long start, long long size);
bool EOS() const; bool EOS() const;
Cluster* GetCluster() const; const Cluster* GetCluster() const;
size_t GetIndex() const; size_t GetIndex() const;
const Block* GetBlock() const; const Block* GetBlock() const;
bool IsBFrame() const; //bool IsBFrame() const;
protected: protected:
Cluster* const m_pCluster; Cluster* const m_pCluster;
@@ -145,10 +166,10 @@ public:
~BlockGroup(); ~BlockGroup();
bool EOS() const; bool EOS() const;
Cluster* GetCluster() const; const Cluster* GetCluster() const;
size_t GetIndex() const; size_t GetIndex() const;
const Block* GetBlock() const; const Block* GetBlock() const;
bool IsBFrame() const; //bool IsBFrame() const;
short GetPrevTimeCode() const; //relative to block's time short GetPrevTimeCode() const; //relative to block's time
short GetNextTimeCode() const; //as above short GetNextTimeCode() const; //as above
@@ -192,6 +213,7 @@ public:
const char* GetCodecNameAsUTF8() const; const char* GetCodecNameAsUTF8() const;
const char* GetCodecId() const; const char* GetCodecId() const;
const unsigned char* GetCodecPrivate(size_t&) const; const unsigned char* GetCodecPrivate(size_t&) const;
bool GetLacing() const;
const BlockEntry* GetEOS() const; const BlockEntry* GetEOS() const;
@@ -211,7 +233,9 @@ public:
unsigned char* codecPrivate; unsigned char* codecPrivate;
size_t codecPrivateSize; size_t codecPrivateSize;
char* codecNameAsUTF8; char* codecNameAsUTF8;
bool lacing;
Settings settings; Settings settings;
Info(); Info();
void Clear(); void Clear();
}; };
@@ -294,8 +318,8 @@ public:
Tracks(Segment*, long long start, long long size); Tracks(Segment*, long long start, long long size);
virtual ~Tracks(); virtual ~Tracks();
Track* GetTrackByNumber(unsigned long tn) const; const Track* GetTrackByNumber(unsigned long tn) const;
Track* GetTrackByIndex(unsigned long idx) const; const Track* GetTrackByIndex(unsigned long idx) const;
private: private:
Track** m_trackEntries; Track** m_trackEntries;
@@ -403,6 +427,8 @@ public:
const CuePoint::TrackPosition*&) const; const CuePoint::TrackPosition*&) const;
#endif #endif
const CuePoint* GetFirst() const;
const CuePoint* GetLast() const;
const CuePoint* GetNext(const CuePoint*) const; const CuePoint* GetNext(const CuePoint*) const;
const BlockEntry* GetBlock( const BlockEntry* GetBlock(
@@ -429,7 +455,6 @@ class Cluster
public: public:
Segment* const m_pSegment; Segment* const m_pSegment;
long m_index;
public: public:
static Cluster* Parse(Segment*, long, long long off); static Cluster* Parse(Segment*, long, long long off);
@@ -439,35 +464,38 @@ public:
bool EOS() const; bool EOS() const;
long long GetTimeCode(); //absolute, but not scaled long long GetTimeCode() const; //absolute, but not scaled
long long GetTime(); //absolute, and scaled (nanosecond units) long long GetTime() const; //absolute, and scaled (nanosecond units)
long long GetFirstTime(); //time (ns) of first (earliest) block 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* GetFirst() const;
const BlockEntry* GetLast(); const BlockEntry* GetLast() const;
const BlockEntry* GetNext(const BlockEntry*) const; const BlockEntry* GetNext(const BlockEntry*) const;
const BlockEntry* GetEntry(const Track*); const BlockEntry* GetEntry(const Track*) const;
const BlockEntry* GetEntry( const BlockEntry* GetEntry(
const CuePoint&, const CuePoint&,
const CuePoint::TrackPosition&); const CuePoint::TrackPosition&) const;
const BlockEntry* GetMaxKey(const VideoTrack*); const BlockEntry* GetMaxKey(const VideoTrack*) const;
protected: protected:
Cluster(Segment*, long, long long off); Cluster(Segment*, long, long long off);
public: public:
long long m_pos; //TODO: these should all be private, with public selector functions
long long m_size; long m_index;
mutable long long m_pos;
mutable long long m_size;
private: private:
long long m_timecode; mutable long long m_timecode;
BlockEntry** m_entries; mutable BlockEntry** m_entries;
size_t m_entriesCount; mutable size_t m_entriesCount;
void Load(); void Load() const;
void LoadBlockEntries(); void LoadBlockEntries() const;
void ParseBlockGroup(long long, long long, size_t); void ParseBlockGroup(long long, long long, size_t) const;
void ParseSimpleBlock(long long, long long, size_t); void ParseSimpleBlock(long long, long long, size_t) const;
}; };
@@ -498,12 +526,10 @@ public:
long long ParseHeaders(); //stops when first cluster is found long long ParseHeaders(); //stops when first cluster is found
long LoadCluster(); //loads one cluster long LoadCluster(); //loads one cluster
#if 0
//This pair parses one cluster, but only changes the state of the //This pair parses one cluster, but only changes the state of the
//segment object when the cluster is actually added to the index. //segment object when the cluster is actually added to the index.
long ParseCluster(Cluster*&, long long& newpos) const; long ParseCluster(long long& cluster_pos, long long& new_pos) const;
bool AddCluster(Cluster*, long long); bool AddCluster(long long cluster_pos, long long new_pos);
#endif
Tracks* GetTracks() const; Tracks* GetTracks() const;
const SegmentInfo* GetInfo() const; const SegmentInfo* GetInfo() const;
@@ -512,12 +538,12 @@ public:
long long GetDuration() const; long long GetDuration() const;
unsigned long GetCount() const; unsigned long GetCount() const;
Cluster* GetFirst(); const Cluster* GetFirst() const;
Cluster* GetLast(); const Cluster* GetLast() const;
Cluster* GetNext(const Cluster*); const Cluster* GetNext(const Cluster*);
Cluster* FindCluster(long long time_nanoseconds); const Cluster* FindCluster(long long time_nanoseconds) const;
const BlockEntry* Seek(long long time_nanoseconds, const Track*); const BlockEntry* Seek(long long time_nanoseconds, const Track*) const;
private: private: