12fdd35794bdddd0a74749a933c402a416da34954James Dong// Copyright (c) 2010 The WebM project authors. All Rights Reserved.
22fdd35794bdddd0a74749a933c402a416da34954James Dong//
32fdd35794bdddd0a74749a933c402a416da34954James Dong// Use of this source code is governed by a BSD-style license
42fdd35794bdddd0a74749a933c402a416da34954James Dong// that can be found in the LICENSE file in the root of the source
52fdd35794bdddd0a74749a933c402a416da34954James Dong// tree. An additional intellectual property rights grant can be found
62fdd35794bdddd0a74749a933c402a416da34954James Dong// in the file PATENTS.  All contributing project authors may
72fdd35794bdddd0a74749a933c402a416da34954James Dong// be found in the AUTHORS file in the root of the source tree.
82fdd35794bdddd0a74749a933c402a416da34954James Dong
92fdd35794bdddd0a74749a933c402a416da34954James Dong#ifndef MKVPARSER_HPP
102fdd35794bdddd0a74749a933c402a416da34954James Dong#define MKVPARSER_HPP
112fdd35794bdddd0a74749a933c402a416da34954James Dong
122fdd35794bdddd0a74749a933c402a416da34954James Dong#include <cstdlib>
132fdd35794bdddd0a74749a933c402a416da34954James Dong#include <cstdio>
1411e84402b62246a2dcfe6add4b0c1063df90d9bfJohann#include <cstddef>
152fdd35794bdddd0a74749a933c402a416da34954James Dong
162fdd35794bdddd0a74749a933c402a416da34954James Dongnamespace mkvparser
172fdd35794bdddd0a74749a933c402a416da34954James Dong{
182fdd35794bdddd0a74749a933c402a416da34954James Dong
192fdd35794bdddd0a74749a933c402a416da34954James Dongconst int E_FILE_FORMAT_INVALID = -2;
202fdd35794bdddd0a74749a933c402a416da34954James Dongconst int E_BUFFER_NOT_FULL = -3;
212fdd35794bdddd0a74749a933c402a416da34954James Dong
222fdd35794bdddd0a74749a933c402a416da34954James Dongclass IMkvReader
232fdd35794bdddd0a74749a933c402a416da34954James Dong{
242fdd35794bdddd0a74749a933c402a416da34954James Dongpublic:
252fdd35794bdddd0a74749a933c402a416da34954James Dong    virtual int Read(long long pos, long len, unsigned char* buf) = 0;
262fdd35794bdddd0a74749a933c402a416da34954James Dong    virtual int Length(long long* total, long long* available) = 0;
272fdd35794bdddd0a74749a933c402a416da34954James Dongprotected:
282fdd35794bdddd0a74749a933c402a416da34954James Dong    virtual ~IMkvReader();
292fdd35794bdddd0a74749a933c402a416da34954James Dong};
302fdd35794bdddd0a74749a933c402a416da34954James Dong
312fdd35794bdddd0a74749a933c402a416da34954James Donglong long GetUIntLength(IMkvReader*, long long, long&);
322fdd35794bdddd0a74749a933c402a416da34954James Donglong long ReadUInt(IMkvReader*, long long, long&);
332fdd35794bdddd0a74749a933c402a416da34954James Donglong long UnserializeUInt(IMkvReader*, long long pos, long long size);
3411e84402b62246a2dcfe6add4b0c1063df90d9bfJohann
3511e84402b62246a2dcfe6add4b0c1063df90d9bfJohannlong UnserializeFloat(IMkvReader*, long long pos, long long size, double&);
3611e84402b62246a2dcfe6add4b0c1063df90d9bfJohannlong UnserializeInt(IMkvReader*, long long pos, long len, long long& result);
3711e84402b62246a2dcfe6add4b0c1063df90d9bfJohann
3811e84402b62246a2dcfe6add4b0c1063df90d9bfJohannlong UnserializeString(
3911e84402b62246a2dcfe6add4b0c1063df90d9bfJohann        IMkvReader*,
4011e84402b62246a2dcfe6add4b0c1063df90d9bfJohann        long long pos,
4111e84402b62246a2dcfe6add4b0c1063df90d9bfJohann        long long size,
4211e84402b62246a2dcfe6add4b0c1063df90d9bfJohann        char*& str);
4311e84402b62246a2dcfe6add4b0c1063df90d9bfJohann
4411e84402b62246a2dcfe6add4b0c1063df90d9bfJohannlong ParseElementHeader(
4511e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    IMkvReader* pReader,
4611e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    long long& pos,  //consume id and size fields
4711e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    long long stop,  //if you know size of element's parent
4811e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    long long& id,
4911e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    long long& size);
5011e84402b62246a2dcfe6add4b0c1063df90d9bfJohann
512fdd35794bdddd0a74749a933c402a416da34954James Dongbool Match(IMkvReader*, long long&, unsigned long, long long&);
529a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huberbool Match(IMkvReader*, long long&, unsigned long, unsigned char*&, size_t&);
532fdd35794bdddd0a74749a933c402a416da34954James Dong
542fdd35794bdddd0a74749a933c402a416da34954James Dongvoid GetVersion(int& major, int& minor, int& build, int& revision);
552fdd35794bdddd0a74749a933c402a416da34954James Dong
562fdd35794bdddd0a74749a933c402a416da34954James Dongstruct EBMLHeader
572fdd35794bdddd0a74749a933c402a416da34954James Dong{
582fdd35794bdddd0a74749a933c402a416da34954James Dong    EBMLHeader();
592fdd35794bdddd0a74749a933c402a416da34954James Dong    ~EBMLHeader();
602fdd35794bdddd0a74749a933c402a416da34954James Dong    long long m_version;
612fdd35794bdddd0a74749a933c402a416da34954James Dong    long long m_readVersion;
622fdd35794bdddd0a74749a933c402a416da34954James Dong    long long m_maxIdLength;
632fdd35794bdddd0a74749a933c402a416da34954James Dong    long long m_maxSizeLength;
642fdd35794bdddd0a74749a933c402a416da34954James Dong    char* m_docType;
652fdd35794bdddd0a74749a933c402a416da34954James Dong    long long m_docTypeVersion;
662fdd35794bdddd0a74749a933c402a416da34954James Dong    long long m_docTypeReadVersion;
672fdd35794bdddd0a74749a933c402a416da34954James Dong
682fdd35794bdddd0a74749a933c402a416da34954James Dong    long long Parse(IMkvReader*, long long&);
699a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    void Init();
702fdd35794bdddd0a74749a933c402a416da34954James Dong};
712fdd35794bdddd0a74749a933c402a416da34954James Dong
722fdd35794bdddd0a74749a933c402a416da34954James Dong
732fdd35794bdddd0a74749a933c402a416da34954James Dongclass Segment;
742fdd35794bdddd0a74749a933c402a416da34954James Dongclass Track;
752fdd35794bdddd0a74749a933c402a416da34954James Dongclass Cluster;
762fdd35794bdddd0a74749a933c402a416da34954James Dong
772fdd35794bdddd0a74749a933c402a416da34954James Dongclass Block
782fdd35794bdddd0a74749a933c402a416da34954James Dong{
792fdd35794bdddd0a74749a933c402a416da34954James Dong    Block(const Block&);
802fdd35794bdddd0a74749a933c402a416da34954James Dong    Block& operator=(const Block&);
812fdd35794bdddd0a74749a933c402a416da34954James Dong
822fdd35794bdddd0a74749a933c402a416da34954James Dongpublic:
832fdd35794bdddd0a74749a933c402a416da34954James Dong    const long long m_start;
842fdd35794bdddd0a74749a933c402a416da34954James Dong    const long long m_size;
852fdd35794bdddd0a74749a933c402a416da34954James Dong
8611e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    Block(long long start, long long size);
879a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    ~Block();
882fdd35794bdddd0a74749a933c402a416da34954James Dong
8911e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    long Parse(IMkvReader*);
9011e84402b62246a2dcfe6add4b0c1063df90d9bfJohann
912fdd35794bdddd0a74749a933c402a416da34954James Dong    long long GetTrackNumber() const;
929a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    long long GetTimeCode(const Cluster*) const;  //absolute, but not scaled
939a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    long long GetTime(const Cluster*) const;      //absolute, and scaled (ns)
942fdd35794bdddd0a74749a933c402a416da34954James Dong    bool IsKey() const;
952fdd35794bdddd0a74749a933c402a416da34954James Dong    void SetKey(bool);
969a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    bool IsInvisible() const;
972fdd35794bdddd0a74749a933c402a416da34954James Dong
9811e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    enum Lacing { kLacingNone, kLacingXiph, kLacingFixed, kLacingEbml };
9911e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    Lacing GetLacing() const;
10011e84402b62246a2dcfe6add4b0c1063df90d9bfJohann
1019a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    int GetFrameCount() const;  //to index frames: [0, count)
1022fdd35794bdddd0a74749a933c402a416da34954James Dong
1039a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    struct Frame
1049a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    {
1059a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber        long long pos;  //absolute offset
1069a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber        long len;
1079a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber
1089a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber        long Read(IMkvReader*, unsigned char*) const;
1099a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    };
1109a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber
1119a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    const Frame& GetFrame(int frame_index) const;
1122fdd35794bdddd0a74749a933c402a416da34954James Dong
1132fdd35794bdddd0a74749a933c402a416da34954James Dongprivate:
1142fdd35794bdddd0a74749a933c402a416da34954James Dong    long long m_track;   //Track::Number()
1152fdd35794bdddd0a74749a933c402a416da34954James Dong    short m_timecode;  //relative to cluster
1162fdd35794bdddd0a74749a933c402a416da34954James Dong    unsigned char m_flags;
1179a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber
1189a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    Frame* m_frames;
1199a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    int m_frame_count;
1202fdd35794bdddd0a74749a933c402a416da34954James Dong
1212fdd35794bdddd0a74749a933c402a416da34954James Dong};
1222fdd35794bdddd0a74749a933c402a416da34954James Dong
1232fdd35794bdddd0a74749a933c402a416da34954James Dong
1242fdd35794bdddd0a74749a933c402a416da34954James Dongclass BlockEntry
1252fdd35794bdddd0a74749a933c402a416da34954James Dong{
1262fdd35794bdddd0a74749a933c402a416da34954James Dong    BlockEntry(const BlockEntry&);
1272fdd35794bdddd0a74749a933c402a416da34954James Dong    BlockEntry& operator=(const BlockEntry&);
1282fdd35794bdddd0a74749a933c402a416da34954James Dong
12911e84402b62246a2dcfe6add4b0c1063df90d9bfJohannprotected:
13011e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    BlockEntry(Cluster*, long index);
13111e84402b62246a2dcfe6add4b0c1063df90d9bfJohann
1322fdd35794bdddd0a74749a933c402a416da34954James Dongpublic:
1332fdd35794bdddd0a74749a933c402a416da34954James Dong    virtual ~BlockEntry();
13411e84402b62246a2dcfe6add4b0c1063df90d9bfJohann
13511e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    bool EOS() const;
13611e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    const Cluster* GetCluster() const;
13711e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    long GetIndex() const;
1382fdd35794bdddd0a74749a933c402a416da34954James Dong    virtual const Block* GetBlock() const = 0;
13911e84402b62246a2dcfe6add4b0c1063df90d9bfJohann
14011e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    enum Kind { kBlockEOS, kBlockSimple, kBlockGroup };
14111e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    virtual Kind GetKind() const = 0;
1422fdd35794bdddd0a74749a933c402a416da34954James Dong
1432fdd35794bdddd0a74749a933c402a416da34954James Dongprotected:
14411e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    Cluster* const m_pCluster;
14511e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    const long m_index;
1462fdd35794bdddd0a74749a933c402a416da34954James Dong
1472fdd35794bdddd0a74749a933c402a416da34954James Dong};
1482fdd35794bdddd0a74749a933c402a416da34954James Dong
1492fdd35794bdddd0a74749a933c402a416da34954James Dong
1502fdd35794bdddd0a74749a933c402a416da34954James Dongclass SimpleBlock : public BlockEntry
1512fdd35794bdddd0a74749a933c402a416da34954James Dong{
1522fdd35794bdddd0a74749a933c402a416da34954James Dong    SimpleBlock(const SimpleBlock&);
1532fdd35794bdddd0a74749a933c402a416da34954James Dong    SimpleBlock& operator=(const SimpleBlock&);
1542fdd35794bdddd0a74749a933c402a416da34954James Dong
1552fdd35794bdddd0a74749a933c402a416da34954James Dongpublic:
15611e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    SimpleBlock(Cluster*, long index, long long start, long long size);
15711e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    long Parse();
1582fdd35794bdddd0a74749a933c402a416da34954James Dong
15911e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    Kind GetKind() const;
1602fdd35794bdddd0a74749a933c402a416da34954James Dong    const Block* GetBlock() const;
1612fdd35794bdddd0a74749a933c402a416da34954James Dong
1622fdd35794bdddd0a74749a933c402a416da34954James Dongprotected:
1632fdd35794bdddd0a74749a933c402a416da34954James Dong    Block m_block;
1642fdd35794bdddd0a74749a933c402a416da34954James Dong
1652fdd35794bdddd0a74749a933c402a416da34954James Dong};
1662fdd35794bdddd0a74749a933c402a416da34954James Dong
1672fdd35794bdddd0a74749a933c402a416da34954James Dong
1682fdd35794bdddd0a74749a933c402a416da34954James Dongclass BlockGroup : public BlockEntry
1692fdd35794bdddd0a74749a933c402a416da34954James Dong{
1702fdd35794bdddd0a74749a933c402a416da34954James Dong    BlockGroup(const BlockGroup&);
1712fdd35794bdddd0a74749a933c402a416da34954James Dong    BlockGroup& operator=(const BlockGroup&);
1722fdd35794bdddd0a74749a933c402a416da34954James Dong
1732fdd35794bdddd0a74749a933c402a416da34954James Dongpublic:
17411e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    BlockGroup(
17511e84402b62246a2dcfe6add4b0c1063df90d9bfJohann        Cluster*,
17611e84402b62246a2dcfe6add4b0c1063df90d9bfJohann        long index,
17711e84402b62246a2dcfe6add4b0c1063df90d9bfJohann        long long block_start, //absolute pos of block's payload
17811e84402b62246a2dcfe6add4b0c1063df90d9bfJohann        long long block_size,  //size of block's payload
17911e84402b62246a2dcfe6add4b0c1063df90d9bfJohann        long long prev,
18011e84402b62246a2dcfe6add4b0c1063df90d9bfJohann        long long next,
18111e84402b62246a2dcfe6add4b0c1063df90d9bfJohann        long long duration);
1822fdd35794bdddd0a74749a933c402a416da34954James Dong
18311e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    long Parse();
1842fdd35794bdddd0a74749a933c402a416da34954James Dong
18511e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    Kind GetKind() const;
18611e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    const Block* GetBlock() const;
1872fdd35794bdddd0a74749a933c402a416da34954James Dong
18811e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    long long GetPrevTimeCode() const;  //relative to block's time
18911e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    long long GetNextTimeCode() const;  //as above
19011e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    long long GetDuration() const;
1912fdd35794bdddd0a74749a933c402a416da34954James Dong
1922fdd35794bdddd0a74749a933c402a416da34954James Dongprivate:
19311e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    Block m_block;
19411e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    const long long m_prev;
19511e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    const long long m_next;
19611e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    const long long m_duration;
1972fdd35794bdddd0a74749a933c402a416da34954James Dong
19811e84402b62246a2dcfe6add4b0c1063df90d9bfJohann};
1992fdd35794bdddd0a74749a933c402a416da34954James Dong
20011e84402b62246a2dcfe6add4b0c1063df90d9bfJohann///////////////////////////////////////////////////////////////
20111e84402b62246a2dcfe6add4b0c1063df90d9bfJohann// ContentEncoding element
20211e84402b62246a2dcfe6add4b0c1063df90d9bfJohann// Elements used to describe if the track data has been encrypted or
20311e84402b62246a2dcfe6add4b0c1063df90d9bfJohann// compressed with zlib or header stripping.
20411e84402b62246a2dcfe6add4b0c1063df90d9bfJohannclass ContentEncoding {
20511e84402b62246a2dcfe6add4b0c1063df90d9bfJohannpublic:
20611e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    ContentEncoding();
20711e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    ~ContentEncoding();
2082fdd35794bdddd0a74749a933c402a416da34954James Dong
20911e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    // ContentCompression element names
21011e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    struct ContentCompression {
21111e84402b62246a2dcfe6add4b0c1063df90d9bfJohann        ContentCompression();
21211e84402b62246a2dcfe6add4b0c1063df90d9bfJohann        ~ContentCompression();
21311e84402b62246a2dcfe6add4b0c1063df90d9bfJohann
21411e84402b62246a2dcfe6add4b0c1063df90d9bfJohann        unsigned long long algo;
21511e84402b62246a2dcfe6add4b0c1063df90d9bfJohann        unsigned char* settings;
21611e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    };
21711e84402b62246a2dcfe6add4b0c1063df90d9bfJohann
21811e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    // ContentEncryption element names
21911e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    struct ContentEncryption {
22011e84402b62246a2dcfe6add4b0c1063df90d9bfJohann        ContentEncryption();
22111e84402b62246a2dcfe6add4b0c1063df90d9bfJohann        ~ContentEncryption();
22211e84402b62246a2dcfe6add4b0c1063df90d9bfJohann
22311e84402b62246a2dcfe6add4b0c1063df90d9bfJohann        unsigned long long algo;
22411e84402b62246a2dcfe6add4b0c1063df90d9bfJohann        unsigned char* key_id;
22511e84402b62246a2dcfe6add4b0c1063df90d9bfJohann        long long key_id_len;
22611e84402b62246a2dcfe6add4b0c1063df90d9bfJohann        unsigned char* signature;
22711e84402b62246a2dcfe6add4b0c1063df90d9bfJohann        long long signature_len;
22811e84402b62246a2dcfe6add4b0c1063df90d9bfJohann        unsigned char* sig_key_id;
22911e84402b62246a2dcfe6add4b0c1063df90d9bfJohann        long long sig_key_id_len;
23011e84402b62246a2dcfe6add4b0c1063df90d9bfJohann        unsigned long long sig_algo;
23111e84402b62246a2dcfe6add4b0c1063df90d9bfJohann        unsigned long long sig_hash_algo;
23211e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    };
23311e84402b62246a2dcfe6add4b0c1063df90d9bfJohann
23411e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    // Returns ContentCompression represented by |idx|. Returns NULL if |idx|
23511e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    // is out of bounds.
23611e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    const ContentCompression* GetCompressionByIndex(unsigned long idx) const;
23711e84402b62246a2dcfe6add4b0c1063df90d9bfJohann
23811e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    // Returns number of ContentCompression elements in this ContentEncoding
23911e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    // element.
24011e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    unsigned long GetCompressionCount() const;
24111e84402b62246a2dcfe6add4b0c1063df90d9bfJohann
24211e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    // Returns ContentEncryption represented by |idx|. Returns NULL if |idx|
24311e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    // is out of bounds.
24411e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    const ContentEncryption* GetEncryptionByIndex(unsigned long idx) const;
24511e84402b62246a2dcfe6add4b0c1063df90d9bfJohann
24611e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    // Returns number of ContentEncryption elements in this ContentEncoding
24711e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    // element.
24811e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    unsigned long GetEncryptionCount() const;
24911e84402b62246a2dcfe6add4b0c1063df90d9bfJohann
25011e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    // Parses the ContentEncoding element from |pReader|. |start| is the
25111e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    // starting offset of the ContentEncoding payload. |size| is the size in
25211e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    // bytes of the ContentEncoding payload. Returns true on success.
25311e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    bool ParseContentEncodingEntry(long long start,
25411e84402b62246a2dcfe6add4b0c1063df90d9bfJohann                                   long long size,
25511e84402b62246a2dcfe6add4b0c1063df90d9bfJohann                                   IMkvReader* const pReader);
25611e84402b62246a2dcfe6add4b0c1063df90d9bfJohann
25711e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    // Parses the ContentEncryption element from |pReader|. |start| is the
25811e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    // starting offset of the ContentEncryption payload. |size| is the size in
25911e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    // bytes of the ContentEncryption payload. |encryption| is where the parsed
26011e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    // values will be stored.
26111e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    void ParseEncryptionEntry(long long start,
26211e84402b62246a2dcfe6add4b0c1063df90d9bfJohann                              long long size,
26311e84402b62246a2dcfe6add4b0c1063df90d9bfJohann                              IMkvReader* const pReader,
26411e84402b62246a2dcfe6add4b0c1063df90d9bfJohann                              ContentEncryption* const encryption);
26511e84402b62246a2dcfe6add4b0c1063df90d9bfJohann
26611e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    unsigned long long encoding_order() const { return encoding_order_; }
26711e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    unsigned long long encoding_scope() const { return encoding_scope_; }
26811e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    unsigned long long encoding_type() const { return encoding_type_; }
2692fdd35794bdddd0a74749a933c402a416da34954James Dong
27011e84402b62246a2dcfe6add4b0c1063df90d9bfJohannprivate:
27111e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    // Member variables for list of ContentCompression elements.
27211e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    ContentCompression** compression_entries_;
27311e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    ContentCompression** compression_entries_end_;
27411e84402b62246a2dcfe6add4b0c1063df90d9bfJohann
27511e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    // Member variables for list of ContentEncryption elements.
27611e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    ContentEncryption** encryption_entries_;
27711e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    ContentEncryption** encryption_entries_end_;
27811e84402b62246a2dcfe6add4b0c1063df90d9bfJohann
27911e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    // ContentEncoding element names
28011e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    unsigned long long encoding_order_;
28111e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    unsigned long long encoding_scope_;
28211e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    unsigned long long encoding_type_;
28311e84402b62246a2dcfe6add4b0c1063df90d9bfJohann
28411e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    // LIBWEBM_DISALLOW_COPY_AND_ASSIGN(ContentEncoding);
28511e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    ContentEncoding(const ContentEncoding&);
28611e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    ContentEncoding& operator=(const ContentEncoding&);
28711e84402b62246a2dcfe6add4b0c1063df90d9bfJohann};
2882fdd35794bdddd0a74749a933c402a416da34954James Dong
2892fdd35794bdddd0a74749a933c402a416da34954James Dongclass Track
2902fdd35794bdddd0a74749a933c402a416da34954James Dong{
2912fdd35794bdddd0a74749a933c402a416da34954James Dong    Track(const Track&);
2922fdd35794bdddd0a74749a933c402a416da34954James Dong    Track& operator=(const Track&);
2932fdd35794bdddd0a74749a933c402a416da34954James Dong
2942fdd35794bdddd0a74749a933c402a416da34954James Dongpublic:
29511e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    enum Type { kVideo = 1, kAudio = 2 };
29611e84402b62246a2dcfe6add4b0c1063df90d9bfJohann
2972fdd35794bdddd0a74749a933c402a416da34954James Dong    Segment* const m_pSegment;
2989a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    const long long m_element_start;
2999a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    const long long m_element_size;
3002fdd35794bdddd0a74749a933c402a416da34954James Dong    virtual ~Track();
3012fdd35794bdddd0a74749a933c402a416da34954James Dong
30211e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    long GetType() const;
30311e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    long GetNumber() const;
3049a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    unsigned long long GetUid() const;
3052fdd35794bdddd0a74749a933c402a416da34954James Dong    const char* GetNameAsUTF8() const;
3062fdd35794bdddd0a74749a933c402a416da34954James Dong    const char* GetCodecNameAsUTF8() const;
3072fdd35794bdddd0a74749a933c402a416da34954James Dong    const char* GetCodecId() const;
3082fdd35794bdddd0a74749a933c402a416da34954James Dong    const unsigned char* GetCodecPrivate(size_t&) const;
3099a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    bool GetLacing() const;
3102fdd35794bdddd0a74749a933c402a416da34954James Dong
3112fdd35794bdddd0a74749a933c402a416da34954James Dong    const BlockEntry* GetEOS() const;
3122fdd35794bdddd0a74749a933c402a416da34954James Dong
3132fdd35794bdddd0a74749a933c402a416da34954James Dong    struct Settings
3142fdd35794bdddd0a74749a933c402a416da34954James Dong    {
3152fdd35794bdddd0a74749a933c402a416da34954James Dong        long long start;
3162fdd35794bdddd0a74749a933c402a416da34954James Dong        long long size;
3172fdd35794bdddd0a74749a933c402a416da34954James Dong    };
3182fdd35794bdddd0a74749a933c402a416da34954James Dong
31911e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    class Info
3202fdd35794bdddd0a74749a933c402a416da34954James Dong    {
32111e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    public:
32211e84402b62246a2dcfe6add4b0c1063df90d9bfJohann        Info();
32311e84402b62246a2dcfe6add4b0c1063df90d9bfJohann        ~Info();
32411e84402b62246a2dcfe6add4b0c1063df90d9bfJohann        int Copy(Info&) const;
32511e84402b62246a2dcfe6add4b0c1063df90d9bfJohann        void Clear();
32611e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    private:
32711e84402b62246a2dcfe6add4b0c1063df90d9bfJohann        Info(const Info&);
32811e84402b62246a2dcfe6add4b0c1063df90d9bfJohann        Info& operator=(const Info&);
32911e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    public:
33011e84402b62246a2dcfe6add4b0c1063df90d9bfJohann        long type;
33111e84402b62246a2dcfe6add4b0c1063df90d9bfJohann        long number;
3329a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber        unsigned long long uid;
3332fdd35794bdddd0a74749a933c402a416da34954James Dong        char* nameAsUTF8;
3342fdd35794bdddd0a74749a933c402a416da34954James Dong        char* codecId;
33511e84402b62246a2dcfe6add4b0c1063df90d9bfJohann        char* codecNameAsUTF8;
3362fdd35794bdddd0a74749a933c402a416da34954James Dong        unsigned char* codecPrivate;
3372fdd35794bdddd0a74749a933c402a416da34954James Dong        size_t codecPrivateSize;
3389a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber        bool lacing;
3392fdd35794bdddd0a74749a933c402a416da34954James Dong        Settings settings;
34011e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    private:
34111e84402b62246a2dcfe6add4b0c1063df90d9bfJohann        int CopyStr(char* Info::*str, Info&) const;
3422fdd35794bdddd0a74749a933c402a416da34954James Dong    };
3432fdd35794bdddd0a74749a933c402a416da34954James Dong
3442fdd35794bdddd0a74749a933c402a416da34954James Dong    long GetFirst(const BlockEntry*&) const;
3452fdd35794bdddd0a74749a933c402a416da34954James Dong    long GetNext(const BlockEntry* pCurr, const BlockEntry*& pNext) const;
3462fdd35794bdddd0a74749a933c402a416da34954James Dong    virtual bool VetEntry(const BlockEntry*) const = 0;
3479a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    virtual long Seek(long long time_ns, const BlockEntry*&) const = 0;
3482fdd35794bdddd0a74749a933c402a416da34954James Dong
34911e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    const ContentEncoding* GetContentEncodingByIndex(unsigned long idx) const;
35011e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    unsigned long GetContentEncodingCount() const;
35111e84402b62246a2dcfe6add4b0c1063df90d9bfJohann
35211e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    void ParseContentEncodingsEntry(long long start, long long size);
35311e84402b62246a2dcfe6add4b0c1063df90d9bfJohann
3542fdd35794bdddd0a74749a933c402a416da34954James Dongprotected:
3559a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    Track(
3569a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber        Segment*,
3579a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber        long long element_start,
3589a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber        long long element_size);
35911e84402b62246a2dcfe6add4b0c1063df90d9bfJohann
36011e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    Info m_info;
3612fdd35794bdddd0a74749a933c402a416da34954James Dong
3622fdd35794bdddd0a74749a933c402a416da34954James Dong    class EOSBlock : public BlockEntry
3632fdd35794bdddd0a74749a933c402a416da34954James Dong    {
3642fdd35794bdddd0a74749a933c402a416da34954James Dong    public:
3652fdd35794bdddd0a74749a933c402a416da34954James Dong        EOSBlock();
3662fdd35794bdddd0a74749a933c402a416da34954James Dong
36711e84402b62246a2dcfe6add4b0c1063df90d9bfJohann        Kind GetKind() const;
3682fdd35794bdddd0a74749a933c402a416da34954James Dong        const Block* GetBlock() const;
3692fdd35794bdddd0a74749a933c402a416da34954James Dong    };
3702fdd35794bdddd0a74749a933c402a416da34954James Dong
3712fdd35794bdddd0a74749a933c402a416da34954James Dong    EOSBlock m_eos;
3722fdd35794bdddd0a74749a933c402a416da34954James Dong
37311e84402b62246a2dcfe6add4b0c1063df90d9bfJohannprivate:
37411e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    ContentEncoding** content_encoding_entries_;
37511e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    ContentEncoding** content_encoding_entries_end_;
3762fdd35794bdddd0a74749a933c402a416da34954James Dong};
3772fdd35794bdddd0a74749a933c402a416da34954James Dong
3782fdd35794bdddd0a74749a933c402a416da34954James Dong
3792fdd35794bdddd0a74749a933c402a416da34954James Dongclass VideoTrack : public Track
3802fdd35794bdddd0a74749a933c402a416da34954James Dong{
3812fdd35794bdddd0a74749a933c402a416da34954James Dong    VideoTrack(const VideoTrack&);
3822fdd35794bdddd0a74749a933c402a416da34954James Dong    VideoTrack& operator=(const VideoTrack&);
3832fdd35794bdddd0a74749a933c402a416da34954James Dong
3849a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    VideoTrack(
3859a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber        Segment*,
3869a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber        long long element_start,
3879a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber        long long element_size);
38811e84402b62246a2dcfe6add4b0c1063df90d9bfJohann
38911e84402b62246a2dcfe6add4b0c1063df90d9bfJohannpublic:
39011e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    static long Parse(
39111e84402b62246a2dcfe6add4b0c1063df90d9bfJohann        Segment*,
39211e84402b62246a2dcfe6add4b0c1063df90d9bfJohann        const Info&,
39311e84402b62246a2dcfe6add4b0c1063df90d9bfJohann        long long element_start,
39411e84402b62246a2dcfe6add4b0c1063df90d9bfJohann        long long element_size,
39511e84402b62246a2dcfe6add4b0c1063df90d9bfJohann        VideoTrack*&);
39611e84402b62246a2dcfe6add4b0c1063df90d9bfJohann
3972fdd35794bdddd0a74749a933c402a416da34954James Dong    long long GetWidth() const;
3982fdd35794bdddd0a74749a933c402a416da34954James Dong    long long GetHeight() const;
3992fdd35794bdddd0a74749a933c402a416da34954James Dong    double GetFrameRate() const;
4002fdd35794bdddd0a74749a933c402a416da34954James Dong
4012fdd35794bdddd0a74749a933c402a416da34954James Dong    bool VetEntry(const BlockEntry*) const;
4029a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    long Seek(long long time_ns, const BlockEntry*&) const;
4032fdd35794bdddd0a74749a933c402a416da34954James Dong
4042fdd35794bdddd0a74749a933c402a416da34954James Dongprivate:
4052fdd35794bdddd0a74749a933c402a416da34954James Dong    long long m_width;
4062fdd35794bdddd0a74749a933c402a416da34954James Dong    long long m_height;
4072fdd35794bdddd0a74749a933c402a416da34954James Dong    double m_rate;
4082fdd35794bdddd0a74749a933c402a416da34954James Dong
4092fdd35794bdddd0a74749a933c402a416da34954James Dong};
4102fdd35794bdddd0a74749a933c402a416da34954James Dong
4112fdd35794bdddd0a74749a933c402a416da34954James Dong
4122fdd35794bdddd0a74749a933c402a416da34954James Dongclass AudioTrack : public Track
4132fdd35794bdddd0a74749a933c402a416da34954James Dong{
4142fdd35794bdddd0a74749a933c402a416da34954James Dong    AudioTrack(const AudioTrack&);
4152fdd35794bdddd0a74749a933c402a416da34954James Dong    AudioTrack& operator=(const AudioTrack&);
4162fdd35794bdddd0a74749a933c402a416da34954James Dong
4179a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    AudioTrack(
4189a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber        Segment*,
4199a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber        long long element_start,
4209a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber        long long element_size);
42111e84402b62246a2dcfe6add4b0c1063df90d9bfJohannpublic:
42211e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    static long Parse(
42311e84402b62246a2dcfe6add4b0c1063df90d9bfJohann        Segment*,
42411e84402b62246a2dcfe6add4b0c1063df90d9bfJohann        const Info&,
42511e84402b62246a2dcfe6add4b0c1063df90d9bfJohann        long long element_start,
42611e84402b62246a2dcfe6add4b0c1063df90d9bfJohann        long long element_size,
42711e84402b62246a2dcfe6add4b0c1063df90d9bfJohann        AudioTrack*&);
42811e84402b62246a2dcfe6add4b0c1063df90d9bfJohann
4292fdd35794bdddd0a74749a933c402a416da34954James Dong    double GetSamplingRate() const;
4302fdd35794bdddd0a74749a933c402a416da34954James Dong    long long GetChannels() const;
4312fdd35794bdddd0a74749a933c402a416da34954James Dong    long long GetBitDepth() const;
4322fdd35794bdddd0a74749a933c402a416da34954James Dong    bool VetEntry(const BlockEntry*) const;
4339a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    long Seek(long long time_ns, const BlockEntry*&) const;
4342fdd35794bdddd0a74749a933c402a416da34954James Dong
4352fdd35794bdddd0a74749a933c402a416da34954James Dongprivate:
4362fdd35794bdddd0a74749a933c402a416da34954James Dong    double m_rate;
4372fdd35794bdddd0a74749a933c402a416da34954James Dong    long long m_channels;
4382fdd35794bdddd0a74749a933c402a416da34954James Dong    long long m_bitDepth;
4392fdd35794bdddd0a74749a933c402a416da34954James Dong};
4402fdd35794bdddd0a74749a933c402a416da34954James Dong
4412fdd35794bdddd0a74749a933c402a416da34954James Dong
4422fdd35794bdddd0a74749a933c402a416da34954James Dongclass Tracks
4432fdd35794bdddd0a74749a933c402a416da34954James Dong{
4442fdd35794bdddd0a74749a933c402a416da34954James Dong    Tracks(const Tracks&);
4452fdd35794bdddd0a74749a933c402a416da34954James Dong    Tracks& operator=(const Tracks&);
4462fdd35794bdddd0a74749a933c402a416da34954James Dong
4472fdd35794bdddd0a74749a933c402a416da34954James Dongpublic:
4482fdd35794bdddd0a74749a933c402a416da34954James Dong    Segment* const m_pSegment;
4492fdd35794bdddd0a74749a933c402a416da34954James Dong    const long long m_start;
4502fdd35794bdddd0a74749a933c402a416da34954James Dong    const long long m_size;
4519a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    const long long m_element_start;
4529a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    const long long m_element_size;
4539a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber
4549a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    Tracks(
4559a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber        Segment*,
4569a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber        long long start,
4579a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber        long long size,
4589a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber        long long element_start,
4599a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber        long long element_size);
4602fdd35794bdddd0a74749a933c402a416da34954James Dong
46111e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    ~Tracks();
46211e84402b62246a2dcfe6add4b0c1063df90d9bfJohann
46311e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    long Parse();
46411e84402b62246a2dcfe6add4b0c1063df90d9bfJohann
46511e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    unsigned long GetTracksCount() const;
46611e84402b62246a2dcfe6add4b0c1063df90d9bfJohann
46711e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    const Track* GetTrackByNumber(long tn) const;
4689a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    const Track* GetTrackByIndex(unsigned long idx) const;
4692fdd35794bdddd0a74749a933c402a416da34954James Dong
4702fdd35794bdddd0a74749a933c402a416da34954James Dongprivate:
4712fdd35794bdddd0a74749a933c402a416da34954James Dong    Track** m_trackEntries;
4722fdd35794bdddd0a74749a933c402a416da34954James Dong    Track** m_trackEntriesEnd;
4732fdd35794bdddd0a74749a933c402a416da34954James Dong
47411e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    long ParseTrackEntry(
47511e84402b62246a2dcfe6add4b0c1063df90d9bfJohann        long long payload_start,
47611e84402b62246a2dcfe6add4b0c1063df90d9bfJohann        long long payload_size,
4779a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber        long long element_start,
47811e84402b62246a2dcfe6add4b0c1063df90d9bfJohann        long long element_size,
47911e84402b62246a2dcfe6add4b0c1063df90d9bfJohann        Track*&) const;
4802fdd35794bdddd0a74749a933c402a416da34954James Dong
4812fdd35794bdddd0a74749a933c402a416da34954James Dong};
4822fdd35794bdddd0a74749a933c402a416da34954James Dong
4832fdd35794bdddd0a74749a933c402a416da34954James Dong
4842fdd35794bdddd0a74749a933c402a416da34954James Dongclass SegmentInfo
4852fdd35794bdddd0a74749a933c402a416da34954James Dong{
4862fdd35794bdddd0a74749a933c402a416da34954James Dong    SegmentInfo(const SegmentInfo&);
4872fdd35794bdddd0a74749a933c402a416da34954James Dong    SegmentInfo& operator=(const SegmentInfo&);
4882fdd35794bdddd0a74749a933c402a416da34954James Dong
4892fdd35794bdddd0a74749a933c402a416da34954James Dongpublic:
4902fdd35794bdddd0a74749a933c402a416da34954James Dong    Segment* const m_pSegment;
4912fdd35794bdddd0a74749a933c402a416da34954James Dong    const long long m_start;
4922fdd35794bdddd0a74749a933c402a416da34954James Dong    const long long m_size;
4939a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    const long long m_element_start;
4949a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    const long long m_element_size;
4959a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber
4969a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    SegmentInfo(
4979a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber        Segment*,
4989a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber        long long start,
4999a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber        long long size,
5009a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber        long long element_start,
5019a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber        long long element_size);
5022fdd35794bdddd0a74749a933c402a416da34954James Dong
5032fdd35794bdddd0a74749a933c402a416da34954James Dong    ~SegmentInfo();
5049a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber
50511e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    long Parse();
50611e84402b62246a2dcfe6add4b0c1063df90d9bfJohann
5072fdd35794bdddd0a74749a933c402a416da34954James Dong    long long GetTimeCodeScale() const;
5082fdd35794bdddd0a74749a933c402a416da34954James Dong    long long GetDuration() const;  //scaled
5092fdd35794bdddd0a74749a933c402a416da34954James Dong    const char* GetMuxingAppAsUTF8() const;
5102fdd35794bdddd0a74749a933c402a416da34954James Dong    const char* GetWritingAppAsUTF8() const;
5112fdd35794bdddd0a74749a933c402a416da34954James Dong    const char* GetTitleAsUTF8() const;
5122fdd35794bdddd0a74749a933c402a416da34954James Dong
5132fdd35794bdddd0a74749a933c402a416da34954James Dongprivate:
5142fdd35794bdddd0a74749a933c402a416da34954James Dong    long long m_timecodeScale;
5152fdd35794bdddd0a74749a933c402a416da34954James Dong    double m_duration;
5162fdd35794bdddd0a74749a933c402a416da34954James Dong    char* m_pMuxingAppAsUTF8;
5172fdd35794bdddd0a74749a933c402a416da34954James Dong    char* m_pWritingAppAsUTF8;
5182fdd35794bdddd0a74749a933c402a416da34954James Dong    char* m_pTitleAsUTF8;
5192fdd35794bdddd0a74749a933c402a416da34954James Dong};
5202fdd35794bdddd0a74749a933c402a416da34954James Dong
5219a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber
5229a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huberclass SeekHead
5239a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber{
5249a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    SeekHead(const SeekHead&);
5259a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    SeekHead& operator=(const SeekHead&);
5269a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber
5279a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huberpublic:
5289a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    Segment* const m_pSegment;
5299a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    const long long m_start;
5309a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    const long long m_size;
5319a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    const long long m_element_start;
5329a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    const long long m_element_size;
5339a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber
5349a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    SeekHead(
5359a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber        Segment*,
5369a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber        long long start,
5379a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber        long long size,
5389a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber        long long element_start,
5399a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber        long long element_size);
5409a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber
5419a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    ~SeekHead();
5429a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber
54311e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    long Parse();
54411e84402b62246a2dcfe6add4b0c1063df90d9bfJohann
5459a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    struct Entry
5469a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    {
54711e84402b62246a2dcfe6add4b0c1063df90d9bfJohann        //the SeekHead entry payload
5489a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber        long long id;
5499a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber        long long pos;
55011e84402b62246a2dcfe6add4b0c1063df90d9bfJohann
55111e84402b62246a2dcfe6add4b0c1063df90d9bfJohann        //absolute pos of SeekEntry ID
55211e84402b62246a2dcfe6add4b0c1063df90d9bfJohann        long long element_start;
55311e84402b62246a2dcfe6add4b0c1063df90d9bfJohann
55411e84402b62246a2dcfe6add4b0c1063df90d9bfJohann        //SeekEntry ID size + size size + payload
55511e84402b62246a2dcfe6add4b0c1063df90d9bfJohann        long long element_size;
5569a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    };
5579a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber
5589a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    int GetCount() const;
5599a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    const Entry* GetEntry(int idx) const;
5609a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber
56111e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    struct VoidElement
56211e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    {
56311e84402b62246a2dcfe6add4b0c1063df90d9bfJohann        //absolute pos of Void ID
56411e84402b62246a2dcfe6add4b0c1063df90d9bfJohann        long long element_start;
56511e84402b62246a2dcfe6add4b0c1063df90d9bfJohann
56611e84402b62246a2dcfe6add4b0c1063df90d9bfJohann        //ID size + size size + payload size
56711e84402b62246a2dcfe6add4b0c1063df90d9bfJohann        long long element_size;
56811e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    };
56911e84402b62246a2dcfe6add4b0c1063df90d9bfJohann
57011e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    int GetVoidElementCount() const;
57111e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    const VoidElement* GetVoidElement(int idx) const;
57211e84402b62246a2dcfe6add4b0c1063df90d9bfJohann
5739a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huberprivate:
5749a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    Entry* m_entries;
57511e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    int m_entry_count;
57611e84402b62246a2dcfe6add4b0c1063df90d9bfJohann
57711e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    VoidElement* m_void_elements;
57811e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    int m_void_element_count;
5799a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber
58011e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    static bool ParseEntry(
5819a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber        IMkvReader*,
58211e84402b62246a2dcfe6add4b0c1063df90d9bfJohann        long long pos,  //payload
5839a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber        long long size,
58411e84402b62246a2dcfe6add4b0c1063df90d9bfJohann        Entry*);
5859a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber
5869a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber};
5879a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber
5882fdd35794bdddd0a74749a933c402a416da34954James Dongclass Cues;
5892fdd35794bdddd0a74749a933c402a416da34954James Dongclass CuePoint
5902fdd35794bdddd0a74749a933c402a416da34954James Dong{
5912fdd35794bdddd0a74749a933c402a416da34954James Dong    friend class Cues;
5922fdd35794bdddd0a74749a933c402a416da34954James Dong
5939a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    CuePoint(long, long long);
5942fdd35794bdddd0a74749a933c402a416da34954James Dong    ~CuePoint();
5952fdd35794bdddd0a74749a933c402a416da34954James Dong
5962fdd35794bdddd0a74749a933c402a416da34954James Dong    CuePoint(const CuePoint&);
5972fdd35794bdddd0a74749a933c402a416da34954James Dong    CuePoint& operator=(const CuePoint&);
5982fdd35794bdddd0a74749a933c402a416da34954James Dong
5992fdd35794bdddd0a74749a933c402a416da34954James Dongpublic:
6009a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    long long m_element_start;
6019a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    long long m_element_size;
6029a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber
6032fdd35794bdddd0a74749a933c402a416da34954James Dong    void Load(IMkvReader*);
6042fdd35794bdddd0a74749a933c402a416da34954James Dong
6052fdd35794bdddd0a74749a933c402a416da34954James Dong    long long GetTimeCode() const;      //absolute but unscaled
6069a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    long long GetTime(const Segment*) const;  //absolute and scaled (ns units)
6072fdd35794bdddd0a74749a933c402a416da34954James Dong
6082fdd35794bdddd0a74749a933c402a416da34954James Dong    struct TrackPosition
6092fdd35794bdddd0a74749a933c402a416da34954James Dong    {
6102fdd35794bdddd0a74749a933c402a416da34954James Dong        long long m_track;
6112fdd35794bdddd0a74749a933c402a416da34954James Dong        long long m_pos;  //of cluster
6122fdd35794bdddd0a74749a933c402a416da34954James Dong        long long m_block;
6132fdd35794bdddd0a74749a933c402a416da34954James Dong        //codec_state  //defaults to 0
6142fdd35794bdddd0a74749a933c402a416da34954James Dong        //reference = clusters containing req'd referenced blocks
6152fdd35794bdddd0a74749a933c402a416da34954James Dong        //  reftime = timecode of the referenced block
6162fdd35794bdddd0a74749a933c402a416da34954James Dong
6172fdd35794bdddd0a74749a933c402a416da34954James Dong        void Parse(IMkvReader*, long long, long long);
6182fdd35794bdddd0a74749a933c402a416da34954James Dong    };
6192fdd35794bdddd0a74749a933c402a416da34954James Dong
6202fdd35794bdddd0a74749a933c402a416da34954James Dong    const TrackPosition* Find(const Track*) const;
6212fdd35794bdddd0a74749a933c402a416da34954James Dong
6222fdd35794bdddd0a74749a933c402a416da34954James Dongprivate:
6239a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    const long m_index;
6242fdd35794bdddd0a74749a933c402a416da34954James Dong    long long m_timecode;
6252fdd35794bdddd0a74749a933c402a416da34954James Dong    TrackPosition* m_track_positions;
6262fdd35794bdddd0a74749a933c402a416da34954James Dong    size_t m_track_positions_count;
6272fdd35794bdddd0a74749a933c402a416da34954James Dong
6282fdd35794bdddd0a74749a933c402a416da34954James Dong};
6292fdd35794bdddd0a74749a933c402a416da34954James Dong
6302fdd35794bdddd0a74749a933c402a416da34954James Dong
6312fdd35794bdddd0a74749a933c402a416da34954James Dongclass Cues
6322fdd35794bdddd0a74749a933c402a416da34954James Dong{
6332fdd35794bdddd0a74749a933c402a416da34954James Dong    friend class Segment;
6342fdd35794bdddd0a74749a933c402a416da34954James Dong
6359a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    Cues(
6369a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber        Segment*,
6379a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber        long long start,
6389a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber        long long size,
6399a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber        long long element_start,
6409a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber        long long element_size);
6412fdd35794bdddd0a74749a933c402a416da34954James Dong    ~Cues();
6422fdd35794bdddd0a74749a933c402a416da34954James Dong
6432fdd35794bdddd0a74749a933c402a416da34954James Dong    Cues(const Cues&);
6442fdd35794bdddd0a74749a933c402a416da34954James Dong    Cues& operator=(const Cues&);
6452fdd35794bdddd0a74749a933c402a416da34954James Dong
6462fdd35794bdddd0a74749a933c402a416da34954James Dongpublic:
6472fdd35794bdddd0a74749a933c402a416da34954James Dong    Segment* const m_pSegment;
6482fdd35794bdddd0a74749a933c402a416da34954James Dong    const long long m_start;
6492fdd35794bdddd0a74749a933c402a416da34954James Dong    const long long m_size;
6509a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    const long long m_element_start;
6519a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    const long long m_element_size;
6522fdd35794bdddd0a74749a933c402a416da34954James Dong
6532fdd35794bdddd0a74749a933c402a416da34954James Dong    bool Find(  //lower bound of time_ns
6542fdd35794bdddd0a74749a933c402a416da34954James Dong        long long time_ns,
6552fdd35794bdddd0a74749a933c402a416da34954James Dong        const Track*,
6562fdd35794bdddd0a74749a933c402a416da34954James Dong        const CuePoint*&,
6572fdd35794bdddd0a74749a933c402a416da34954James Dong        const CuePoint::TrackPosition*&) const;
6582fdd35794bdddd0a74749a933c402a416da34954James Dong
6592fdd35794bdddd0a74749a933c402a416da34954James Dong#if 0
6602fdd35794bdddd0a74749a933c402a416da34954James Dong    bool FindNext(  //upper_bound of time_ns
6612fdd35794bdddd0a74749a933c402a416da34954James Dong        long long time_ns,
6622fdd35794bdddd0a74749a933c402a416da34954James Dong        const Track*,
6632fdd35794bdddd0a74749a933c402a416da34954James Dong        const CuePoint*&,
6642fdd35794bdddd0a74749a933c402a416da34954James Dong        const CuePoint::TrackPosition*&) const;
6652fdd35794bdddd0a74749a933c402a416da34954James Dong#endif
6662fdd35794bdddd0a74749a933c402a416da34954James Dong
6672fdd35794bdddd0a74749a933c402a416da34954James Dong    const CuePoint* GetFirst() const;
6682fdd35794bdddd0a74749a933c402a416da34954James Dong    const CuePoint* GetLast() const;
6692fdd35794bdddd0a74749a933c402a416da34954James Dong    const CuePoint* GetNext(const CuePoint*) const;
6702fdd35794bdddd0a74749a933c402a416da34954James Dong
6712fdd35794bdddd0a74749a933c402a416da34954James Dong    const BlockEntry* GetBlock(
6722fdd35794bdddd0a74749a933c402a416da34954James Dong                        const CuePoint*,
6732fdd35794bdddd0a74749a933c402a416da34954James Dong                        const CuePoint::TrackPosition*) const;
6742fdd35794bdddd0a74749a933c402a416da34954James Dong
6759a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    bool LoadCuePoint() const;
6769a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    long GetCount() const;  //loaded only
6779a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    //long GetTotal() const;  //loaded + preloaded
6789a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    bool DoneParsing() const;
6799a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber
6802fdd35794bdddd0a74749a933c402a416da34954James Dongprivate:
6812fdd35794bdddd0a74749a933c402a416da34954James Dong    void Init() const;
6829a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    void PreloadCuePoint(long&, long long) const;
6832fdd35794bdddd0a74749a933c402a416da34954James Dong
6842fdd35794bdddd0a74749a933c402a416da34954James Dong    mutable CuePoint** m_cue_points;
6859a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    mutable long m_count;
6869a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    mutable long m_preload_count;
6872fdd35794bdddd0a74749a933c402a416da34954James Dong    mutable long long m_pos;
6882fdd35794bdddd0a74749a933c402a416da34954James Dong
6892fdd35794bdddd0a74749a933c402a416da34954James Dong};
6902fdd35794bdddd0a74749a933c402a416da34954James Dong
6912fdd35794bdddd0a74749a933c402a416da34954James Dong
6922fdd35794bdddd0a74749a933c402a416da34954James Dongclass Cluster
6932fdd35794bdddd0a74749a933c402a416da34954James Dong{
6949a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    friend class Segment;
6959a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber
6962fdd35794bdddd0a74749a933c402a416da34954James Dong    Cluster(const Cluster&);
6972fdd35794bdddd0a74749a933c402a416da34954James Dong    Cluster& operator=(const Cluster&);
6982fdd35794bdddd0a74749a933c402a416da34954James Dong
6992fdd35794bdddd0a74749a933c402a416da34954James Dongpublic:
7002fdd35794bdddd0a74749a933c402a416da34954James Dong    Segment* const m_pSegment;
7012fdd35794bdddd0a74749a933c402a416da34954James Dong
7022fdd35794bdddd0a74749a933c402a416da34954James Dongpublic:
7039a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    static Cluster* Create(
7049a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber        Segment*,
7059a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber        long index,       //index in segment
7069a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber        long long off);   //offset relative to segment
7079a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber        //long long element_size);
7082fdd35794bdddd0a74749a933c402a416da34954James Dong
7092fdd35794bdddd0a74749a933c402a416da34954James Dong    Cluster();  //EndOfStream
7102fdd35794bdddd0a74749a933c402a416da34954James Dong    ~Cluster();
7112fdd35794bdddd0a74749a933c402a416da34954James Dong
7122fdd35794bdddd0a74749a933c402a416da34954James Dong    bool EOS() const;
7132fdd35794bdddd0a74749a933c402a416da34954James Dong
7149a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    long long GetTimeCode() const;   //absolute, but not scaled
7159a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    long long GetTime() const;       //absolute, and scaled (nanosecond units)
7169a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    long long GetFirstTime() const;  //time (ns) of first (earliest) block
7179a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    long long GetLastTime() const;   //time (ns) of last (latest) block
7182fdd35794bdddd0a74749a933c402a416da34954James Dong
71911e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    long GetFirst(const BlockEntry*&) const;
72011e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    long GetLast(const BlockEntry*&) const;
72111e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    long GetNext(const BlockEntry* curr, const BlockEntry*& next) const;
72211e84402b62246a2dcfe6add4b0c1063df90d9bfJohann
7239a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    const BlockEntry* GetEntry(const Track*, long long ns = -1) const;
7242fdd35794bdddd0a74749a933c402a416da34954James Dong    const BlockEntry* GetEntry(
7252fdd35794bdddd0a74749a933c402a416da34954James Dong        const CuePoint&,
7269a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber        const CuePoint::TrackPosition&) const;
72711e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    //const BlockEntry* GetMaxKey(const VideoTrack*) const;
7289a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber
7299a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber//    static bool HasBlockEntries(const Segment*, long long);
7309a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber
7319a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    static long HasBlockEntries(
7329a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber            const Segment*,
7339a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber            long long idoff,
7349a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber            long long& pos,
7359a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber            long& size);
7369a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber
7379a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    long GetEntryCount() const;
7389a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber
7399a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    long Load(long long& pos, long& size) const;
7409a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber
7419a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    long Parse(long long& pos, long& size) const;
7429a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    long GetEntry(long index, const mkvparser::BlockEntry*&) const;
7432fdd35794bdddd0a74749a933c402a416da34954James Dong
7442fdd35794bdddd0a74749a933c402a416da34954James Dongprotected:
7459a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    Cluster(
7469a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber        Segment*,
7479a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber        long index,
7489a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber        long long element_start);
7499a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber        //long long element_size);
7502fdd35794bdddd0a74749a933c402a416da34954James Dong
7512fdd35794bdddd0a74749a933c402a416da34954James Dongpublic:
7529a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    const long long m_element_start;
7539a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    long long GetPosition() const;  //offset relative to segment
7549a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber
7559a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    long GetIndex() const;
7569a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    long long GetElementSize() const;
7579a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    //long long GetPayloadSize() const;
7589a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber
7599a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    //long long Unparsed() const;
7602fdd35794bdddd0a74749a933c402a416da34954James Dong
7612fdd35794bdddd0a74749a933c402a416da34954James Dongprivate:
7629a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    long m_index;
7639a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    mutable long long m_pos;
7649a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    //mutable long long m_size;
7659a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    mutable long long m_element_size;
7669a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    mutable long long m_timecode;
7679a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    mutable BlockEntry** m_entries;
7689a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    mutable long m_entries_size;
7699a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    mutable long m_entries_count;
7702fdd35794bdddd0a74749a933c402a416da34954James Dong
77111e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    long ParseSimpleBlock(long long, long long&, long&);
77211e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    long ParseBlockGroup(long long, long long&, long&);
7739a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber
77411e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    long CreateBlock(long long id, long long pos, long long size);
77511e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    long CreateBlockGroup(long long, long long);
77611e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    long CreateSimpleBlock(long long, long long);
7772fdd35794bdddd0a74749a933c402a416da34954James Dong
7782fdd35794bdddd0a74749a933c402a416da34954James Dong};
7792fdd35794bdddd0a74749a933c402a416da34954James Dong
7802fdd35794bdddd0a74749a933c402a416da34954James Dong
7812fdd35794bdddd0a74749a933c402a416da34954James Dongclass Segment
7822fdd35794bdddd0a74749a933c402a416da34954James Dong{
7832fdd35794bdddd0a74749a933c402a416da34954James Dong    friend class Cues;
7849a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    friend class VideoTrack;
7859a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    friend class AudioTrack;
7862fdd35794bdddd0a74749a933c402a416da34954James Dong
7872fdd35794bdddd0a74749a933c402a416da34954James Dong    Segment(const Segment&);
7882fdd35794bdddd0a74749a933c402a416da34954James Dong    Segment& operator=(const Segment&);
7892fdd35794bdddd0a74749a933c402a416da34954James Dong
7902fdd35794bdddd0a74749a933c402a416da34954James Dongprivate:
79111e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    Segment(
79211e84402b62246a2dcfe6add4b0c1063df90d9bfJohann        IMkvReader*,
79311e84402b62246a2dcfe6add4b0c1063df90d9bfJohann        long long elem_start,
79411e84402b62246a2dcfe6add4b0c1063df90d9bfJohann        //long long elem_size,
79511e84402b62246a2dcfe6add4b0c1063df90d9bfJohann        long long pos,
79611e84402b62246a2dcfe6add4b0c1063df90d9bfJohann        long long size);
7972fdd35794bdddd0a74749a933c402a416da34954James Dong
7982fdd35794bdddd0a74749a933c402a416da34954James Dongpublic:
7992fdd35794bdddd0a74749a933c402a416da34954James Dong    IMkvReader* const m_pReader;
80011e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    const long long m_element_start;
80111e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    //const long long m_element_size;
8022fdd35794bdddd0a74749a933c402a416da34954James Dong    const long long m_start;  //posn of segment payload
8032fdd35794bdddd0a74749a933c402a416da34954James Dong    const long long m_size;   //size of segment payload
8042fdd35794bdddd0a74749a933c402a416da34954James Dong    Cluster m_eos;  //TODO: make private?
8052fdd35794bdddd0a74749a933c402a416da34954James Dong
8062fdd35794bdddd0a74749a933c402a416da34954James Dong    static long long CreateInstance(IMkvReader*, long long, Segment*&);
8072fdd35794bdddd0a74749a933c402a416da34954James Dong    ~Segment();
8082fdd35794bdddd0a74749a933c402a416da34954James Dong
8092fdd35794bdddd0a74749a933c402a416da34954James Dong    long Load();  //loads headers and all clusters
8102fdd35794bdddd0a74749a933c402a416da34954James Dong
8119a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    //for incremental loading
81211e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    //long long Unparsed() const;
81311e84402b62246a2dcfe6add4b0c1063df90d9bfJohann    bool DoneParsing() const;
8142fdd35794bdddd0a74749a933c402a416da34954James Dong    long long ParseHeaders();  //stops when first cluster is found
8159a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    //long FindNextCluster(long long& pos, long& size) const;
8169a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    long LoadCluster(long long& pos, long& size);  //load one cluster
8179a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    long LoadCluster();
8189a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber
8199a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    long ParseNext(
8209a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber            const Cluster* pCurr,
8219a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber            const Cluster*& pNext,
8229a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber            long long& pos,
8239a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber            long& size);
8242fdd35794bdddd0a74749a933c402a416da34954James Dong
8252fdd35794bdddd0a74749a933c402a416da34954James Dong#if 0
8262fdd35794bdddd0a74749a933c402a416da34954James Dong    //This pair parses one cluster, but only changes the state of the
8272fdd35794bdddd0a74749a933c402a416da34954James Dong    //segment object when the cluster is actually added to the index.
8289a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    long ParseCluster(long long& cluster_pos, long long& new_pos) const;
8299a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    bool AddCluster(long long cluster_pos, long long new_pos);
8302fdd35794bdddd0a74749a933c402a416da34954James Dong#endif
8312fdd35794bdddd0a74749a933c402a416da34954James Dong
8329a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    const SeekHead* GetSeekHead() const;
8339a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    const Tracks* GetTracks() const;
8342fdd35794bdddd0a74749a933c402a416da34954James Dong    const SegmentInfo* GetInfo() const;
8352fdd35794bdddd0a74749a933c402a416da34954James Dong    const Cues* GetCues() const;
8362fdd35794bdddd0a74749a933c402a416da34954James Dong
8372fdd35794bdddd0a74749a933c402a416da34954James Dong    long long GetDuration() const;
8382fdd35794bdddd0a74749a933c402a416da34954James Dong
8392fdd35794bdddd0a74749a933c402a416da34954James Dong    unsigned long GetCount() const;
8409a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    const Cluster* GetFirst() const;
8419a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    const Cluster* GetLast() const;
8429a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    const Cluster* GetNext(const Cluster*);
8439a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber
8449a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    const Cluster* FindCluster(long long time_nanoseconds) const;
8459a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    //const BlockEntry* Seek(long long time_nanoseconds, const Track*) const;
8462fdd35794bdddd0a74749a933c402a416da34954James Dong
8479a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    const Cluster* FindOrPreloadCluster(long long pos);
8489a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber
8499a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    long ParseCues(
8509a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber        long long cues_off,  //offset relative to start of segment
8519a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber        long long& parse_pos,
8529a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber        long& parse_len);
8532fdd35794bdddd0a74749a933c402a416da34954James Dong
8542fdd35794bdddd0a74749a933c402a416da34954James Dongprivate:
8552fdd35794bdddd0a74749a933c402a416da34954James Dong
8562fdd35794bdddd0a74749a933c402a416da34954James Dong    long long m_pos;  //absolute file posn; what has been consumed so far
8579a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    Cluster* m_pUnknownSize;
8589a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber
8599a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    SeekHead* m_pSeekHead;
8602fdd35794bdddd0a74749a933c402a416da34954James Dong    SegmentInfo* m_pInfo;
8612fdd35794bdddd0a74749a933c402a416da34954James Dong    Tracks* m_pTracks;
8622fdd35794bdddd0a74749a933c402a416da34954James Dong    Cues* m_pCues;
8632fdd35794bdddd0a74749a933c402a416da34954James Dong    Cluster** m_clusters;
8642fdd35794bdddd0a74749a933c402a416da34954James Dong    long m_clusterCount;         //number of entries for which m_index >= 0
8652fdd35794bdddd0a74749a933c402a416da34954James Dong    long m_clusterPreloadCount;  //number of entries for which m_index < 0
8662fdd35794bdddd0a74749a933c402a416da34954James Dong    long m_clusterSize;          //array size
8672fdd35794bdddd0a74749a933c402a416da34954James Dong
8689a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    long DoLoadCluster(long long&, long&);
8699a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    long DoLoadClusterUnknownSize(long long&, long&);
8709a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    long DoParseNext(const Cluster*&, long long&, long&);
8719a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber
8722fdd35794bdddd0a74749a933c402a416da34954James Dong    void AppendCluster(Cluster*);
8732fdd35794bdddd0a74749a933c402a416da34954James Dong    void PreloadCluster(Cluster*, ptrdiff_t);
8742fdd35794bdddd0a74749a933c402a416da34954James Dong
8759a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    //void ParseSeekHead(long long pos, long long size);
8769a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    //void ParseSeekEntry(long long pos, long long size);
8779a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    //void ParseCues(long long);
8782fdd35794bdddd0a74749a933c402a416da34954James Dong
8792fdd35794bdddd0a74749a933c402a416da34954James Dong    const BlockEntry* GetBlock(
8802fdd35794bdddd0a74749a933c402a416da34954James Dong        const CuePoint&,
8812fdd35794bdddd0a74749a933c402a416da34954James Dong        const CuePoint::TrackPosition&);
8822fdd35794bdddd0a74749a933c402a416da34954James Dong
8832fdd35794bdddd0a74749a933c402a416da34954James Dong};
8842fdd35794bdddd0a74749a933c402a416da34954James Dong
8852fdd35794bdddd0a74749a933c402a416da34954James Dong}  //end namespace mkvparser
8862fdd35794bdddd0a74749a933c402a416da34954James Dong
8879a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huberinline long mkvparser::Segment::LoadCluster()
8889a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber{
8899a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    long long pos;
8909a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    long size;
8919a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber
8929a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber    return LoadCluster(pos, size);
8939a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber}
8949a61bcc59a71a4a623711ef7fb43570abc73f575Andreas Huber
8952fdd35794bdddd0a74749a933c402a416da34954James Dong#endif  //MKVPARSER_HPP
896