mkvparser.h 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112
  1. // Copyright (c) 2012 The WebM project authors. All Rights Reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style license
  4. // that can be found in the LICENSE file in the root of the source
  5. // tree. An additional intellectual property rights grant can be found
  6. // in the file PATENTS. All contributing project authors may
  7. // be found in the AUTHORS file in the root of the source tree.
  8. #ifndef MKVPARSER_MKVPARSER_H_
  9. #define MKVPARSER_MKVPARSER_H_
  10. #include <stddef.h>
  11. namespace mkvparser {
  12. const int E_PARSE_FAILED = -1;
  13. const int E_FILE_FORMAT_INVALID = -2;
  14. const int E_BUFFER_NOT_FULL = -3;
  15. class IMkvReader {
  16. public:
  17. virtual int Read(long long pos, long len, unsigned char* buf) = 0;
  18. virtual int Length(long long* total, long long* available) = 0;
  19. virtual ~IMkvReader();
  20. };
  21. template <typename Type>
  22. Type* SafeArrayAlloc(unsigned long long num_elements,
  23. unsigned long long element_size);
  24. long long GetUIntLength(IMkvReader*, long long, long&);
  25. long long ReadUInt(IMkvReader*, long long, long&);
  26. long long ReadID(IMkvReader* pReader, long long pos, long& len);
  27. long long UnserializeUInt(IMkvReader*, long long pos, long long size);
  28. long UnserializeFloat(IMkvReader*, long long pos, long long size, double&);
  29. long UnserializeInt(IMkvReader*, long long pos, long long size,
  30. long long& result);
  31. long UnserializeString(IMkvReader*, long long pos, long long size, char*& str);
  32. long ParseElementHeader(IMkvReader* pReader,
  33. long long& pos, // consume id and size fields
  34. long long stop, // if you know size of element's parent
  35. long long& id, long long& size);
  36. bool Match(IMkvReader*, long long&, unsigned long, long long&);
  37. bool Match(IMkvReader*, long long&, unsigned long, unsigned char*&, size_t&);
  38. void GetVersion(int& major, int& minor, int& build, int& revision);
  39. struct EBMLHeader {
  40. EBMLHeader();
  41. ~EBMLHeader();
  42. long long m_version;
  43. long long m_readVersion;
  44. long long m_maxIdLength;
  45. long long m_maxSizeLength;
  46. char* m_docType;
  47. long long m_docTypeVersion;
  48. long long m_docTypeReadVersion;
  49. long long Parse(IMkvReader*, long long&);
  50. void Init();
  51. };
  52. class Segment;
  53. class Track;
  54. class Cluster;
  55. class Block {
  56. Block(const Block&);
  57. Block& operator=(const Block&);
  58. public:
  59. const long long m_start;
  60. const long long m_size;
  61. Block(long long start, long long size, long long discard_padding);
  62. ~Block();
  63. long Parse(const Cluster*);
  64. long long GetTrackNumber() const;
  65. long long GetTimeCode(const Cluster*) const; // absolute, but not scaled
  66. long long GetTime(const Cluster*) const; // absolute, and scaled (ns)
  67. bool IsKey() const;
  68. void SetKey(bool);
  69. bool IsInvisible() const;
  70. enum Lacing { kLacingNone, kLacingXiph, kLacingFixed, kLacingEbml };
  71. Lacing GetLacing() const;
  72. int GetFrameCount() const; // to index frames: [0, count)
  73. struct Frame {
  74. long long pos; // absolute offset
  75. long len;
  76. long Read(IMkvReader*, unsigned char*) const;
  77. };
  78. const Frame& GetFrame(int frame_index) const;
  79. long long GetDiscardPadding() const;
  80. private:
  81. long long m_track; // Track::Number()
  82. short m_timecode; // relative to cluster
  83. unsigned char m_flags;
  84. Frame* m_frames;
  85. int m_frame_count;
  86. protected:
  87. const long long m_discard_padding;
  88. };
  89. class BlockEntry {
  90. BlockEntry(const BlockEntry&);
  91. BlockEntry& operator=(const BlockEntry&);
  92. protected:
  93. BlockEntry(Cluster*, long index);
  94. public:
  95. virtual ~BlockEntry();
  96. bool EOS() const { return (GetKind() == kBlockEOS); }
  97. const Cluster* GetCluster() const;
  98. long GetIndex() const;
  99. virtual const Block* GetBlock() const = 0;
  100. enum Kind { kBlockEOS, kBlockSimple, kBlockGroup };
  101. virtual Kind GetKind() const = 0;
  102. protected:
  103. Cluster* const m_pCluster;
  104. const long m_index;
  105. };
  106. class SimpleBlock : public BlockEntry {
  107. SimpleBlock(const SimpleBlock&);
  108. SimpleBlock& operator=(const SimpleBlock&);
  109. public:
  110. SimpleBlock(Cluster*, long index, long long start, long long size);
  111. long Parse();
  112. Kind GetKind() const;
  113. const Block* GetBlock() const;
  114. protected:
  115. Block m_block;
  116. };
  117. class BlockGroup : public BlockEntry {
  118. BlockGroup(const BlockGroup&);
  119. BlockGroup& operator=(const BlockGroup&);
  120. public:
  121. BlockGroup(Cluster*, long index,
  122. long long block_start, // absolute pos of block's payload
  123. long long block_size, // size of block's payload
  124. long long prev, long long next, long long duration,
  125. long long discard_padding);
  126. long Parse();
  127. Kind GetKind() const;
  128. const Block* GetBlock() const;
  129. long long GetPrevTimeCode() const; // relative to block's time
  130. long long GetNextTimeCode() const; // as above
  131. long long GetDurationTimeCode() const;
  132. private:
  133. Block m_block;
  134. const long long m_prev;
  135. const long long m_next;
  136. const long long m_duration;
  137. };
  138. ///////////////////////////////////////////////////////////////
  139. // ContentEncoding element
  140. // Elements used to describe if the track data has been encrypted or
  141. // compressed with zlib or header stripping.
  142. class ContentEncoding {
  143. public:
  144. enum { kCTR = 1 };
  145. ContentEncoding();
  146. ~ContentEncoding();
  147. // ContentCompression element names
  148. struct ContentCompression {
  149. ContentCompression();
  150. ~ContentCompression();
  151. unsigned long long algo;
  152. unsigned char* settings;
  153. long long settings_len;
  154. };
  155. // ContentEncAESSettings element names
  156. struct ContentEncAESSettings {
  157. ContentEncAESSettings() : cipher_mode(kCTR) {}
  158. ~ContentEncAESSettings() {}
  159. unsigned long long cipher_mode;
  160. };
  161. // ContentEncryption element names
  162. struct ContentEncryption {
  163. ContentEncryption();
  164. ~ContentEncryption();
  165. unsigned long long algo;
  166. unsigned char* key_id;
  167. long long key_id_len;
  168. unsigned char* signature;
  169. long long signature_len;
  170. unsigned char* sig_key_id;
  171. long long sig_key_id_len;
  172. unsigned long long sig_algo;
  173. unsigned long long sig_hash_algo;
  174. ContentEncAESSettings aes_settings;
  175. };
  176. // Returns ContentCompression represented by |idx|. Returns NULL if |idx|
  177. // is out of bounds.
  178. const ContentCompression* GetCompressionByIndex(unsigned long idx) const;
  179. // Returns number of ContentCompression elements in this ContentEncoding
  180. // element.
  181. unsigned long GetCompressionCount() const;
  182. // Parses the ContentCompression element from |pReader|. |start| is the
  183. // starting offset of the ContentCompression payload. |size| is the size in
  184. // bytes of the ContentCompression payload. |compression| is where the parsed
  185. // values will be stored.
  186. long ParseCompressionEntry(long long start, long long size,
  187. IMkvReader* pReader,
  188. ContentCompression* compression);
  189. // Returns ContentEncryption represented by |idx|. Returns NULL if |idx|
  190. // is out of bounds.
  191. const ContentEncryption* GetEncryptionByIndex(unsigned long idx) const;
  192. // Returns number of ContentEncryption elements in this ContentEncoding
  193. // element.
  194. unsigned long GetEncryptionCount() const;
  195. // Parses the ContentEncAESSettings element from |pReader|. |start| is the
  196. // starting offset of the ContentEncAESSettings payload. |size| is the
  197. // size in bytes of the ContentEncAESSettings payload. |encryption| is
  198. // where the parsed values will be stored.
  199. long ParseContentEncAESSettingsEntry(long long start, long long size,
  200. IMkvReader* pReader,
  201. ContentEncAESSettings* aes);
  202. // Parses the ContentEncoding element from |pReader|. |start| is the
  203. // starting offset of the ContentEncoding payload. |size| is the size in
  204. // bytes of the ContentEncoding payload. Returns true on success.
  205. long ParseContentEncodingEntry(long long start, long long size,
  206. IMkvReader* pReader);
  207. // Parses the ContentEncryption element from |pReader|. |start| is the
  208. // starting offset of the ContentEncryption payload. |size| is the size in
  209. // bytes of the ContentEncryption payload. |encryption| is where the parsed
  210. // values will be stored.
  211. long ParseEncryptionEntry(long long start, long long size,
  212. IMkvReader* pReader, ContentEncryption* encryption);
  213. unsigned long long encoding_order() const { return encoding_order_; }
  214. unsigned long long encoding_scope() const { return encoding_scope_; }
  215. unsigned long long encoding_type() const { return encoding_type_; }
  216. private:
  217. // Member variables for list of ContentCompression elements.
  218. ContentCompression** compression_entries_;
  219. ContentCompression** compression_entries_end_;
  220. // Member variables for list of ContentEncryption elements.
  221. ContentEncryption** encryption_entries_;
  222. ContentEncryption** encryption_entries_end_;
  223. // ContentEncoding element names
  224. unsigned long long encoding_order_;
  225. unsigned long long encoding_scope_;
  226. unsigned long long encoding_type_;
  227. // LIBWEBM_DISALLOW_COPY_AND_ASSIGN(ContentEncoding);
  228. ContentEncoding(const ContentEncoding&);
  229. ContentEncoding& operator=(const ContentEncoding&);
  230. };
  231. class Track {
  232. Track(const Track&);
  233. Track& operator=(const Track&);
  234. public:
  235. class Info;
  236. static long Create(Segment*, const Info&, long long element_start,
  237. long long element_size, Track*&);
  238. enum Type { kVideo = 1, kAudio = 2, kSubtitle = 0x11, kMetadata = 0x21 };
  239. Segment* const m_pSegment;
  240. const long long m_element_start;
  241. const long long m_element_size;
  242. virtual ~Track();
  243. long GetType() const;
  244. long GetNumber() const;
  245. unsigned long long GetUid() const;
  246. const char* GetNameAsUTF8() const;
  247. const char* GetLanguage() const;
  248. const char* GetCodecNameAsUTF8() const;
  249. const char* GetCodecId() const;
  250. const unsigned char* GetCodecPrivate(size_t&) const;
  251. bool GetLacing() const;
  252. unsigned long long GetDefaultDuration() const;
  253. unsigned long long GetCodecDelay() const;
  254. unsigned long long GetSeekPreRoll() const;
  255. const BlockEntry* GetEOS() const;
  256. struct Settings {
  257. long long start;
  258. long long size;
  259. };
  260. class Info {
  261. public:
  262. Info();
  263. ~Info();
  264. int Copy(Info&) const;
  265. void Clear();
  266. long type;
  267. long number;
  268. unsigned long long uid;
  269. unsigned long long defaultDuration;
  270. unsigned long long codecDelay;
  271. unsigned long long seekPreRoll;
  272. char* nameAsUTF8;
  273. char* language;
  274. char* codecId;
  275. char* codecNameAsUTF8;
  276. unsigned char* codecPrivate;
  277. size_t codecPrivateSize;
  278. bool lacing;
  279. Settings settings;
  280. private:
  281. Info(const Info&);
  282. Info& operator=(const Info&);
  283. int CopyStr(char* Info::*str, Info&) const;
  284. };
  285. long GetFirst(const BlockEntry*&) const;
  286. long GetNext(const BlockEntry* pCurr, const BlockEntry*& pNext) const;
  287. virtual bool VetEntry(const BlockEntry*) const;
  288. virtual long Seek(long long time_ns, const BlockEntry*&) const;
  289. const ContentEncoding* GetContentEncodingByIndex(unsigned long idx) const;
  290. unsigned long GetContentEncodingCount() const;
  291. long ParseContentEncodingsEntry(long long start, long long size);
  292. protected:
  293. Track(Segment*, long long element_start, long long element_size);
  294. Info m_info;
  295. class EOSBlock : public BlockEntry {
  296. public:
  297. EOSBlock();
  298. Kind GetKind() const;
  299. const Block* GetBlock() const;
  300. };
  301. EOSBlock m_eos;
  302. private:
  303. ContentEncoding** content_encoding_entries_;
  304. ContentEncoding** content_encoding_entries_end_;
  305. };
  306. struct PrimaryChromaticity {
  307. PrimaryChromaticity() : x(0), y(0) {}
  308. ~PrimaryChromaticity() {}
  309. static bool Parse(IMkvReader* reader, long long read_pos,
  310. long long value_size, bool is_x,
  311. PrimaryChromaticity** chromaticity);
  312. float x;
  313. float y;
  314. };
  315. struct MasteringMetadata {
  316. static const float kValueNotPresent;
  317. MasteringMetadata()
  318. : r(NULL),
  319. g(NULL),
  320. b(NULL),
  321. white_point(NULL),
  322. luminance_max(kValueNotPresent),
  323. luminance_min(kValueNotPresent) {}
  324. ~MasteringMetadata() {
  325. delete r;
  326. delete g;
  327. delete b;
  328. delete white_point;
  329. }
  330. static bool Parse(IMkvReader* reader, long long element_start,
  331. long long element_size,
  332. MasteringMetadata** mastering_metadata);
  333. PrimaryChromaticity* r;
  334. PrimaryChromaticity* g;
  335. PrimaryChromaticity* b;
  336. PrimaryChromaticity* white_point;
  337. float luminance_max;
  338. float luminance_min;
  339. };
  340. struct Colour {
  341. static const long long kValueNotPresent;
  342. // Unless otherwise noted all values assigned upon construction are the
  343. // equivalent of unspecified/default.
  344. Colour()
  345. : matrix_coefficients(kValueNotPresent),
  346. bits_per_channel(kValueNotPresent),
  347. chroma_subsampling_horz(kValueNotPresent),
  348. chroma_subsampling_vert(kValueNotPresent),
  349. cb_subsampling_horz(kValueNotPresent),
  350. cb_subsampling_vert(kValueNotPresent),
  351. chroma_siting_horz(kValueNotPresent),
  352. chroma_siting_vert(kValueNotPresent),
  353. range(kValueNotPresent),
  354. transfer_characteristics(kValueNotPresent),
  355. primaries(kValueNotPresent),
  356. max_cll(kValueNotPresent),
  357. max_fall(kValueNotPresent),
  358. mastering_metadata(NULL) {}
  359. ~Colour() {
  360. delete mastering_metadata;
  361. mastering_metadata = NULL;
  362. }
  363. static bool Parse(IMkvReader* reader, long long element_start,
  364. long long element_size, Colour** colour);
  365. long long matrix_coefficients;
  366. long long bits_per_channel;
  367. long long chroma_subsampling_horz;
  368. long long chroma_subsampling_vert;
  369. long long cb_subsampling_horz;
  370. long long cb_subsampling_vert;
  371. long long chroma_siting_horz;
  372. long long chroma_siting_vert;
  373. long long range;
  374. long long transfer_characteristics;
  375. long long primaries;
  376. long long max_cll;
  377. long long max_fall;
  378. MasteringMetadata* mastering_metadata;
  379. };
  380. class VideoTrack : public Track {
  381. VideoTrack(const VideoTrack&);
  382. VideoTrack& operator=(const VideoTrack&);
  383. VideoTrack(Segment*, long long element_start, long long element_size);
  384. public:
  385. virtual ~VideoTrack();
  386. static long Parse(Segment*, const Info&, long long element_start,
  387. long long element_size, VideoTrack*&);
  388. long long GetWidth() const;
  389. long long GetHeight() const;
  390. long long GetDisplayWidth() const;
  391. long long GetDisplayHeight() const;
  392. long long GetDisplayUnit() const;
  393. long long GetStereoMode() const;
  394. double GetFrameRate() const;
  395. bool VetEntry(const BlockEntry*) const;
  396. long Seek(long long time_ns, const BlockEntry*&) const;
  397. Colour* GetColour() const;
  398. private:
  399. long long m_width;
  400. long long m_height;
  401. long long m_display_width;
  402. long long m_display_height;
  403. long long m_display_unit;
  404. long long m_stereo_mode;
  405. double m_rate;
  406. Colour* m_colour;
  407. };
  408. class AudioTrack : public Track {
  409. AudioTrack(const AudioTrack&);
  410. AudioTrack& operator=(const AudioTrack&);
  411. AudioTrack(Segment*, long long element_start, long long element_size);
  412. public:
  413. static long Parse(Segment*, const Info&, long long element_start,
  414. long long element_size, AudioTrack*&);
  415. double GetSamplingRate() const;
  416. long long GetChannels() const;
  417. long long GetBitDepth() const;
  418. private:
  419. double m_rate;
  420. long long m_channels;
  421. long long m_bitDepth;
  422. };
  423. class Tracks {
  424. Tracks(const Tracks&);
  425. Tracks& operator=(const Tracks&);
  426. public:
  427. Segment* const m_pSegment;
  428. const long long m_start;
  429. const long long m_size;
  430. const long long m_element_start;
  431. const long long m_element_size;
  432. Tracks(Segment*, long long start, long long size, long long element_start,
  433. long long element_size);
  434. ~Tracks();
  435. long Parse();
  436. unsigned long GetTracksCount() const;
  437. const Track* GetTrackByNumber(long tn) const;
  438. const Track* GetTrackByIndex(unsigned long idx) const;
  439. private:
  440. Track** m_trackEntries;
  441. Track** m_trackEntriesEnd;
  442. long ParseTrackEntry(long long payload_start, long long payload_size,
  443. long long element_start, long long element_size,
  444. Track*&) const;
  445. };
  446. class Chapters {
  447. Chapters(const Chapters&);
  448. Chapters& operator=(const Chapters&);
  449. public:
  450. Segment* const m_pSegment;
  451. const long long m_start;
  452. const long long m_size;
  453. const long long m_element_start;
  454. const long long m_element_size;
  455. Chapters(Segment*, long long payload_start, long long payload_size,
  456. long long element_start, long long element_size);
  457. ~Chapters();
  458. long Parse();
  459. class Atom;
  460. class Edition;
  461. class Display {
  462. friend class Atom;
  463. Display();
  464. Display(const Display&);
  465. ~Display();
  466. Display& operator=(const Display&);
  467. public:
  468. const char* GetString() const;
  469. const char* GetLanguage() const;
  470. const char* GetCountry() const;
  471. private:
  472. void Init();
  473. void ShallowCopy(Display&) const;
  474. void Clear();
  475. long Parse(IMkvReader*, long long pos, long long size);
  476. char* m_string;
  477. char* m_language;
  478. char* m_country;
  479. };
  480. class Atom {
  481. friend class Edition;
  482. Atom();
  483. Atom(const Atom&);
  484. ~Atom();
  485. Atom& operator=(const Atom&);
  486. public:
  487. unsigned long long GetUID() const;
  488. const char* GetStringUID() const;
  489. long long GetStartTimecode() const;
  490. long long GetStopTimecode() const;
  491. long long GetStartTime(const Chapters*) const;
  492. long long GetStopTime(const Chapters*) const;
  493. int GetDisplayCount() const;
  494. const Display* GetDisplay(int index) const;
  495. private:
  496. void Init();
  497. void ShallowCopy(Atom&) const;
  498. void Clear();
  499. long Parse(IMkvReader*, long long pos, long long size);
  500. static long long GetTime(const Chapters*, long long timecode);
  501. long ParseDisplay(IMkvReader*, long long pos, long long size);
  502. bool ExpandDisplaysArray();
  503. char* m_string_uid;
  504. unsigned long long m_uid;
  505. long long m_start_timecode;
  506. long long m_stop_timecode;
  507. Display* m_displays;
  508. int m_displays_size;
  509. int m_displays_count;
  510. };
  511. class Edition {
  512. friend class Chapters;
  513. Edition();
  514. Edition(const Edition&);
  515. ~Edition();
  516. Edition& operator=(const Edition&);
  517. public:
  518. int GetAtomCount() const;
  519. const Atom* GetAtom(int index) const;
  520. private:
  521. void Init();
  522. void ShallowCopy(Edition&) const;
  523. void Clear();
  524. long Parse(IMkvReader*, long long pos, long long size);
  525. long ParseAtom(IMkvReader*, long long pos, long long size);
  526. bool ExpandAtomsArray();
  527. Atom* m_atoms;
  528. int m_atoms_size;
  529. int m_atoms_count;
  530. };
  531. int GetEditionCount() const;
  532. const Edition* GetEdition(int index) const;
  533. private:
  534. long ParseEdition(long long pos, long long size);
  535. bool ExpandEditionsArray();
  536. Edition* m_editions;
  537. int m_editions_size;
  538. int m_editions_count;
  539. };
  540. class Tags {
  541. Tags(const Tags&);
  542. Tags& operator=(const Tags&);
  543. public:
  544. Segment* const m_pSegment;
  545. const long long m_start;
  546. const long long m_size;
  547. const long long m_element_start;
  548. const long long m_element_size;
  549. Tags(Segment*, long long payload_start, long long payload_size,
  550. long long element_start, long long element_size);
  551. ~Tags();
  552. long Parse();
  553. class Tag;
  554. class SimpleTag;
  555. class SimpleTag {
  556. friend class Tag;
  557. SimpleTag();
  558. SimpleTag(const SimpleTag&);
  559. ~SimpleTag();
  560. SimpleTag& operator=(const SimpleTag&);
  561. public:
  562. const char* GetTagName() const;
  563. const char* GetTagString() const;
  564. private:
  565. void Init();
  566. void ShallowCopy(SimpleTag&) const;
  567. void Clear();
  568. long Parse(IMkvReader*, long long pos, long long size);
  569. char* m_tag_name;
  570. char* m_tag_string;
  571. };
  572. class Tag {
  573. friend class Tags;
  574. Tag();
  575. Tag(const Tag&);
  576. ~Tag();
  577. Tag& operator=(const Tag&);
  578. public:
  579. int GetSimpleTagCount() const;
  580. const SimpleTag* GetSimpleTag(int index) const;
  581. private:
  582. void Init();
  583. void ShallowCopy(Tag&) const;
  584. void Clear();
  585. long Parse(IMkvReader*, long long pos, long long size);
  586. long ParseSimpleTag(IMkvReader*, long long pos, long long size);
  587. bool ExpandSimpleTagsArray();
  588. SimpleTag* m_simple_tags;
  589. int m_simple_tags_size;
  590. int m_simple_tags_count;
  591. };
  592. int GetTagCount() const;
  593. const Tag* GetTag(int index) const;
  594. private:
  595. long ParseTag(long long pos, long long size);
  596. bool ExpandTagsArray();
  597. Tag* m_tags;
  598. int m_tags_size;
  599. int m_tags_count;
  600. };
  601. class SegmentInfo {
  602. SegmentInfo(const SegmentInfo&);
  603. SegmentInfo& operator=(const SegmentInfo&);
  604. public:
  605. Segment* const m_pSegment;
  606. const long long m_start;
  607. const long long m_size;
  608. const long long m_element_start;
  609. const long long m_element_size;
  610. SegmentInfo(Segment*, long long start, long long size,
  611. long long element_start, long long element_size);
  612. ~SegmentInfo();
  613. long Parse();
  614. long long GetTimeCodeScale() const;
  615. long long GetDuration() const; // scaled
  616. const char* GetMuxingAppAsUTF8() const;
  617. const char* GetWritingAppAsUTF8() const;
  618. const char* GetTitleAsUTF8() const;
  619. private:
  620. long long m_timecodeScale;
  621. double m_duration;
  622. char* m_pMuxingAppAsUTF8;
  623. char* m_pWritingAppAsUTF8;
  624. char* m_pTitleAsUTF8;
  625. };
  626. class SeekHead {
  627. SeekHead(const SeekHead&);
  628. SeekHead& operator=(const SeekHead&);
  629. public:
  630. Segment* const m_pSegment;
  631. const long long m_start;
  632. const long long m_size;
  633. const long long m_element_start;
  634. const long long m_element_size;
  635. SeekHead(Segment*, long long start, long long size, long long element_start,
  636. long long element_size);
  637. ~SeekHead();
  638. long Parse();
  639. struct Entry {
  640. // the SeekHead entry payload
  641. long long id;
  642. long long pos;
  643. // absolute pos of SeekEntry ID
  644. long long element_start;
  645. // SeekEntry ID size + size size + payload
  646. long long element_size;
  647. };
  648. int GetCount() const;
  649. const Entry* GetEntry(int idx) const;
  650. struct VoidElement {
  651. // absolute pos of Void ID
  652. long long element_start;
  653. // ID size + size size + payload size
  654. long long element_size;
  655. };
  656. int GetVoidElementCount() const;
  657. const VoidElement* GetVoidElement(int idx) const;
  658. private:
  659. Entry* m_entries;
  660. int m_entry_count;
  661. VoidElement* m_void_elements;
  662. int m_void_element_count;
  663. static bool ParseEntry(IMkvReader*,
  664. long long pos, // payload
  665. long long size, Entry*);
  666. };
  667. class Cues;
  668. class CuePoint {
  669. friend class Cues;
  670. CuePoint(long, long long);
  671. ~CuePoint();
  672. CuePoint(const CuePoint&);
  673. CuePoint& operator=(const CuePoint&);
  674. public:
  675. long long m_element_start;
  676. long long m_element_size;
  677. bool Load(IMkvReader*);
  678. long long GetTimeCode() const; // absolute but unscaled
  679. long long GetTime(const Segment*) const; // absolute and scaled (ns units)
  680. struct TrackPosition {
  681. long long m_track;
  682. long long m_pos; // of cluster
  683. long long m_block;
  684. // codec_state //defaults to 0
  685. // reference = clusters containing req'd referenced blocks
  686. // reftime = timecode of the referenced block
  687. bool Parse(IMkvReader*, long long, long long);
  688. };
  689. const TrackPosition* Find(const Track*) const;
  690. private:
  691. const long m_index;
  692. long long m_timecode;
  693. TrackPosition* m_track_positions;
  694. size_t m_track_positions_count;
  695. };
  696. class Cues {
  697. friend class Segment;
  698. Cues(Segment*, long long start, long long size, long long element_start,
  699. long long element_size);
  700. ~Cues();
  701. Cues(const Cues&);
  702. Cues& operator=(const Cues&);
  703. public:
  704. Segment* const m_pSegment;
  705. const long long m_start;
  706. const long long m_size;
  707. const long long m_element_start;
  708. const long long m_element_size;
  709. bool Find( // lower bound of time_ns
  710. long long time_ns, const Track*, const CuePoint*&,
  711. const CuePoint::TrackPosition*&) const;
  712. const CuePoint* GetFirst() const;
  713. const CuePoint* GetLast() const;
  714. const CuePoint* GetNext(const CuePoint*) const;
  715. const BlockEntry* GetBlock(const CuePoint*,
  716. const CuePoint::TrackPosition*) const;
  717. bool LoadCuePoint() const;
  718. long GetCount() const; // loaded only
  719. // long GetTotal() const; //loaded + preloaded
  720. bool DoneParsing() const;
  721. private:
  722. bool Init() const;
  723. bool PreloadCuePoint(long&, long long) const;
  724. mutable CuePoint** m_cue_points;
  725. mutable long m_count;
  726. mutable long m_preload_count;
  727. mutable long long m_pos;
  728. };
  729. class Cluster {
  730. friend class Segment;
  731. Cluster(const Cluster&);
  732. Cluster& operator=(const Cluster&);
  733. public:
  734. Segment* const m_pSegment;
  735. public:
  736. static Cluster* Create(Segment*,
  737. long index, // index in segment
  738. long long off); // offset relative to segment
  739. // long long element_size);
  740. Cluster(); // EndOfStream
  741. ~Cluster();
  742. bool EOS() const;
  743. long long GetTimeCode() const; // absolute, but not scaled
  744. long long GetTime() const; // absolute, and scaled (nanosecond units)
  745. long long GetFirstTime() const; // time (ns) of first (earliest) block
  746. long long GetLastTime() const; // time (ns) of last (latest) block
  747. long GetFirst(const BlockEntry*&) const;
  748. long GetLast(const BlockEntry*&) const;
  749. long GetNext(const BlockEntry* curr, const BlockEntry*& next) const;
  750. const BlockEntry* GetEntry(const Track*, long long ns = -1) const;
  751. const BlockEntry* GetEntry(const CuePoint&,
  752. const CuePoint::TrackPosition&) const;
  753. // const BlockEntry* GetMaxKey(const VideoTrack*) const;
  754. // static bool HasBlockEntries(const Segment*, long long);
  755. static long HasBlockEntries(const Segment*, long long idoff, long long& pos,
  756. long& size);
  757. long GetEntryCount() const;
  758. long Load(long long& pos, long& size) const;
  759. long Parse(long long& pos, long& size) const;
  760. long GetEntry(long index, const mkvparser::BlockEntry*&) const;
  761. protected:
  762. Cluster(Segment*, long index, long long element_start);
  763. // long long element_size);
  764. public:
  765. const long long m_element_start;
  766. long long GetPosition() const; // offset relative to segment
  767. long GetIndex() const;
  768. long long GetElementSize() const;
  769. // long long GetPayloadSize() const;
  770. // long long Unparsed() const;
  771. private:
  772. long m_index;
  773. mutable long long m_pos;
  774. // mutable long long m_size;
  775. mutable long long m_element_size;
  776. mutable long long m_timecode;
  777. mutable BlockEntry** m_entries;
  778. mutable long m_entries_size;
  779. mutable long m_entries_count;
  780. long ParseSimpleBlock(long long, long long&, long&);
  781. long ParseBlockGroup(long long, long long&, long&);
  782. long CreateBlock(long long id, long long pos, long long size,
  783. long long discard_padding);
  784. long CreateBlockGroup(long long start_offset, long long size,
  785. long long discard_padding);
  786. long CreateSimpleBlock(long long, long long);
  787. };
  788. class Segment {
  789. friend class Cues;
  790. friend class Track;
  791. friend class VideoTrack;
  792. Segment(const Segment&);
  793. Segment& operator=(const Segment&);
  794. private:
  795. Segment(IMkvReader*, long long elem_start,
  796. // long long elem_size,
  797. long long pos, long long size);
  798. public:
  799. IMkvReader* const m_pReader;
  800. const long long m_element_start;
  801. // const long long m_element_size;
  802. const long long m_start; // posn of segment payload
  803. const long long m_size; // size of segment payload
  804. Cluster m_eos; // TODO: make private?
  805. static long long CreateInstance(IMkvReader*, long long, Segment*&);
  806. ~Segment();
  807. long Load(); // loads headers and all clusters
  808. // for incremental loading
  809. // long long Unparsed() const;
  810. bool DoneParsing() const;
  811. long long ParseHeaders(); // stops when first cluster is found
  812. // long FindNextCluster(long long& pos, long& size) const;
  813. long LoadCluster(long long& pos, long& size); // load one cluster
  814. long LoadCluster();
  815. long ParseNext(const Cluster* pCurr, const Cluster*& pNext, long long& pos,
  816. long& size);
  817. const SeekHead* GetSeekHead() const;
  818. const Tracks* GetTracks() const;
  819. const SegmentInfo* GetInfo() const;
  820. const Cues* GetCues() const;
  821. const Chapters* GetChapters() const;
  822. const Tags* GetTags() const;
  823. long long GetDuration() const;
  824. unsigned long GetCount() const;
  825. const Cluster* GetFirst() const;
  826. const Cluster* GetLast() const;
  827. const Cluster* GetNext(const Cluster*);
  828. const Cluster* FindCluster(long long time_nanoseconds) const;
  829. // const BlockEntry* Seek(long long time_nanoseconds, const Track*) const;
  830. const Cluster* FindOrPreloadCluster(long long pos);
  831. long ParseCues(long long cues_off, // offset relative to start of segment
  832. long long& parse_pos, long& parse_len);
  833. private:
  834. long long m_pos; // absolute file posn; what has been consumed so far
  835. Cluster* m_pUnknownSize;
  836. SeekHead* m_pSeekHead;
  837. SegmentInfo* m_pInfo;
  838. Tracks* m_pTracks;
  839. Cues* m_pCues;
  840. Chapters* m_pChapters;
  841. Tags* m_pTags;
  842. Cluster** m_clusters;
  843. long m_clusterCount; // number of entries for which m_index >= 0
  844. long m_clusterPreloadCount; // number of entries for which m_index < 0
  845. long m_clusterSize; // array size
  846. long DoLoadCluster(long long&, long&);
  847. long DoLoadClusterUnknownSize(long long&, long&);
  848. long DoParseNext(const Cluster*&, long long&, long&);
  849. bool AppendCluster(Cluster*);
  850. bool PreloadCluster(Cluster*, ptrdiff_t);
  851. // void ParseSeekHead(long long pos, long long size);
  852. // void ParseSeekEntry(long long pos, long long size);
  853. // void ParseCues(long long);
  854. const BlockEntry* GetBlock(const CuePoint&, const CuePoint::TrackPosition&);
  855. };
  856. } // namespace mkvparser
  857. inline long mkvparser::Segment::LoadCluster() {
  858. long long pos;
  859. long size;
  860. return LoadCluster(pos, size);
  861. }
  862. #endif // MKVPARSER_MKVPARSER_H_