2
0

mkvparser.h 30 KB

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