xref: /aosp_15_r20/external/libwebm/mkvmuxer/mkvmuxer.h (revision 103e46e4cd4b6efcf6001f23fa8665fb110abf8d)
1*103e46e4SHarish Mahendrakar // Copyright (c) 2012 The WebM project authors. All Rights Reserved.
2*103e46e4SHarish Mahendrakar //
3*103e46e4SHarish Mahendrakar // Use of this source code is governed by a BSD-style license
4*103e46e4SHarish Mahendrakar // that can be found in the LICENSE file in the root of the source
5*103e46e4SHarish Mahendrakar // tree. An additional intellectual property rights grant can be found
6*103e46e4SHarish Mahendrakar // in the file PATENTS.  All contributing project authors may
7*103e46e4SHarish Mahendrakar // be found in the AUTHORS file in the root of the source tree.
8*103e46e4SHarish Mahendrakar 
9*103e46e4SHarish Mahendrakar #ifndef MKVMUXER_MKVMUXER_H_
10*103e46e4SHarish Mahendrakar #define MKVMUXER_MKVMUXER_H_
11*103e46e4SHarish Mahendrakar 
12*103e46e4SHarish Mahendrakar #include <stdint.h>
13*103e46e4SHarish Mahendrakar 
14*103e46e4SHarish Mahendrakar #include <cstddef>
15*103e46e4SHarish Mahendrakar #include <list>
16*103e46e4SHarish Mahendrakar #include <map>
17*103e46e4SHarish Mahendrakar 
18*103e46e4SHarish Mahendrakar #include "common/webmids.h"
19*103e46e4SHarish Mahendrakar #include "mkvmuxer/mkvmuxertypes.h"
20*103e46e4SHarish Mahendrakar 
21*103e46e4SHarish Mahendrakar // For a description of the WebM elements see
22*103e46e4SHarish Mahendrakar // http://www.webmproject.org/code/specs/container/.
23*103e46e4SHarish Mahendrakar 
24*103e46e4SHarish Mahendrakar namespace mkvparser {
25*103e46e4SHarish Mahendrakar class IMkvReader;
26*103e46e4SHarish Mahendrakar }  // namespace mkvparser
27*103e46e4SHarish Mahendrakar 
28*103e46e4SHarish Mahendrakar namespace mkvmuxer {
29*103e46e4SHarish Mahendrakar 
30*103e46e4SHarish Mahendrakar class MkvWriter;
31*103e46e4SHarish Mahendrakar class Segment;
32*103e46e4SHarish Mahendrakar 
33*103e46e4SHarish Mahendrakar const uint64_t kMaxTrackNumber = 126;
34*103e46e4SHarish Mahendrakar 
35*103e46e4SHarish Mahendrakar ///////////////////////////////////////////////////////////////
36*103e46e4SHarish Mahendrakar // Interface used by the mkvmuxer to write out the Mkv data.
37*103e46e4SHarish Mahendrakar class IMkvWriter {
38*103e46e4SHarish Mahendrakar  public:
39*103e46e4SHarish Mahendrakar   // Writes out |len| bytes of |buf|. Returns 0 on success.
40*103e46e4SHarish Mahendrakar   virtual int32 Write(const void* buf, uint32 len) = 0;
41*103e46e4SHarish Mahendrakar 
42*103e46e4SHarish Mahendrakar   // Returns the offset of the output position from the beginning of the
43*103e46e4SHarish Mahendrakar   // output.
44*103e46e4SHarish Mahendrakar   virtual int64 Position() const = 0;
45*103e46e4SHarish Mahendrakar 
46*103e46e4SHarish Mahendrakar   // Set the current File position. Returns 0 on success.
47*103e46e4SHarish Mahendrakar   virtual int32 Position(int64 position) = 0;
48*103e46e4SHarish Mahendrakar 
49*103e46e4SHarish Mahendrakar   // Returns true if the writer is seekable.
50*103e46e4SHarish Mahendrakar   virtual bool Seekable() const = 0;
51*103e46e4SHarish Mahendrakar 
52*103e46e4SHarish Mahendrakar   // Element start notification. Called whenever an element identifier is about
53*103e46e4SHarish Mahendrakar   // to be written to the stream. |element_id| is the element identifier, and
54*103e46e4SHarish Mahendrakar   // |position| is the location in the WebM stream where the first octet of the
55*103e46e4SHarish Mahendrakar   // element identifier will be written.
56*103e46e4SHarish Mahendrakar   // Note: the |MkvId| enumeration in webmids.hpp defines element values.
57*103e46e4SHarish Mahendrakar   virtual void ElementStartNotify(uint64 element_id, int64 position) = 0;
58*103e46e4SHarish Mahendrakar 
59*103e46e4SHarish Mahendrakar  protected:
60*103e46e4SHarish Mahendrakar   IMkvWriter();
61*103e46e4SHarish Mahendrakar   virtual ~IMkvWriter();
62*103e46e4SHarish Mahendrakar 
63*103e46e4SHarish Mahendrakar  private:
64*103e46e4SHarish Mahendrakar   LIBWEBM_DISALLOW_COPY_AND_ASSIGN(IMkvWriter);
65*103e46e4SHarish Mahendrakar };
66*103e46e4SHarish Mahendrakar 
67*103e46e4SHarish Mahendrakar // Writes out the EBML header for a WebM file, but allows caller to specify
68*103e46e4SHarish Mahendrakar // DocType. This function must be called before any other libwebm writing
69*103e46e4SHarish Mahendrakar // functions are called.
70*103e46e4SHarish Mahendrakar bool WriteEbmlHeader(IMkvWriter* writer, uint64_t doc_type_version,
71*103e46e4SHarish Mahendrakar                      const char* const doc_type);
72*103e46e4SHarish Mahendrakar 
73*103e46e4SHarish Mahendrakar // Writes out the EBML header for a WebM file. This function must be called
74*103e46e4SHarish Mahendrakar // before any other libwebm writing functions are called.
75*103e46e4SHarish Mahendrakar bool WriteEbmlHeader(IMkvWriter* writer, uint64_t doc_type_version);
76*103e46e4SHarish Mahendrakar 
77*103e46e4SHarish Mahendrakar // Deprecated. Writes out EBML header with doc_type_version as
78*103e46e4SHarish Mahendrakar // kDefaultDocTypeVersion. Exists for backward compatibility.
79*103e46e4SHarish Mahendrakar bool WriteEbmlHeader(IMkvWriter* writer);
80*103e46e4SHarish Mahendrakar 
81*103e46e4SHarish Mahendrakar // Copies in Chunk from source to destination between the given byte positions
82*103e46e4SHarish Mahendrakar bool ChunkedCopy(mkvparser::IMkvReader* source, IMkvWriter* dst, int64_t start,
83*103e46e4SHarish Mahendrakar                  int64_t size);
84*103e46e4SHarish Mahendrakar 
85*103e46e4SHarish Mahendrakar ///////////////////////////////////////////////////////////////
86*103e46e4SHarish Mahendrakar // Class to hold data the will be written to a block.
87*103e46e4SHarish Mahendrakar class Frame {
88*103e46e4SHarish Mahendrakar  public:
89*103e46e4SHarish Mahendrakar   Frame();
90*103e46e4SHarish Mahendrakar   ~Frame();
91*103e46e4SHarish Mahendrakar 
92*103e46e4SHarish Mahendrakar   // Sets this frame's contents based on |frame|. Returns true on success. On
93*103e46e4SHarish Mahendrakar   // failure, this frame's existing contents may be lost.
94*103e46e4SHarish Mahendrakar   bool CopyFrom(const Frame& frame);
95*103e46e4SHarish Mahendrakar 
96*103e46e4SHarish Mahendrakar   // Copies |frame| data into |frame_|. Returns true on success.
97*103e46e4SHarish Mahendrakar   bool Init(const uint8_t* frame, uint64_t length);
98*103e46e4SHarish Mahendrakar 
99*103e46e4SHarish Mahendrakar   // Copies |additional| data into |additional_|. Returns true on success.
100*103e46e4SHarish Mahendrakar   bool AddAdditionalData(const uint8_t* additional, uint64_t length,
101*103e46e4SHarish Mahendrakar                          uint64_t add_id);
102*103e46e4SHarish Mahendrakar 
103*103e46e4SHarish Mahendrakar   // Returns true if the frame has valid parameters.
104*103e46e4SHarish Mahendrakar   bool IsValid() const;
105*103e46e4SHarish Mahendrakar 
106*103e46e4SHarish Mahendrakar   // Returns true if the frame can be written as a SimpleBlock based on current
107*103e46e4SHarish Mahendrakar   // parameters.
108*103e46e4SHarish Mahendrakar   bool CanBeSimpleBlock() const;
109*103e46e4SHarish Mahendrakar 
add_id()110*103e46e4SHarish Mahendrakar   uint64_t add_id() const { return add_id_; }
additional()111*103e46e4SHarish Mahendrakar   const uint8_t* additional() const { return additional_; }
additional_length()112*103e46e4SHarish Mahendrakar   uint64_t additional_length() const { return additional_length_; }
113*103e46e4SHarish Mahendrakar   void set_duration(uint64_t duration);
duration()114*103e46e4SHarish Mahendrakar   uint64_t duration() const { return duration_; }
duration_set()115*103e46e4SHarish Mahendrakar   bool duration_set() const { return duration_set_; }
frame()116*103e46e4SHarish Mahendrakar   const uint8_t* frame() const { return frame_; }
set_is_key(bool key)117*103e46e4SHarish Mahendrakar   void set_is_key(bool key) { is_key_ = key; }
is_key()118*103e46e4SHarish Mahendrakar   bool is_key() const { return is_key_; }
length()119*103e46e4SHarish Mahendrakar   uint64_t length() const { return length_; }
set_track_number(uint64_t track_number)120*103e46e4SHarish Mahendrakar   void set_track_number(uint64_t track_number) { track_number_ = track_number; }
track_number()121*103e46e4SHarish Mahendrakar   uint64_t track_number() const { return track_number_; }
set_timestamp(uint64_t timestamp)122*103e46e4SHarish Mahendrakar   void set_timestamp(uint64_t timestamp) { timestamp_ = timestamp; }
timestamp()123*103e46e4SHarish Mahendrakar   uint64_t timestamp() const { return timestamp_; }
set_discard_padding(int64_t discard_padding)124*103e46e4SHarish Mahendrakar   void set_discard_padding(int64_t discard_padding) {
125*103e46e4SHarish Mahendrakar     discard_padding_ = discard_padding;
126*103e46e4SHarish Mahendrakar   }
discard_padding()127*103e46e4SHarish Mahendrakar   int64_t discard_padding() const { return discard_padding_; }
128*103e46e4SHarish Mahendrakar   void set_reference_block_timestamp(int64_t reference_block_timestamp);
reference_block_timestamp()129*103e46e4SHarish Mahendrakar   int64_t reference_block_timestamp() const {
130*103e46e4SHarish Mahendrakar     return reference_block_timestamp_;
131*103e46e4SHarish Mahendrakar   }
reference_block_timestamp_set()132*103e46e4SHarish Mahendrakar   bool reference_block_timestamp_set() const {
133*103e46e4SHarish Mahendrakar     return reference_block_timestamp_set_;
134*103e46e4SHarish Mahendrakar   }
135*103e46e4SHarish Mahendrakar 
136*103e46e4SHarish Mahendrakar  private:
137*103e46e4SHarish Mahendrakar   // Id of the Additional data.
138*103e46e4SHarish Mahendrakar   uint64_t add_id_;
139*103e46e4SHarish Mahendrakar 
140*103e46e4SHarish Mahendrakar   // Pointer to additional data. Owned by this class.
141*103e46e4SHarish Mahendrakar   uint8_t* additional_;
142*103e46e4SHarish Mahendrakar 
143*103e46e4SHarish Mahendrakar   // Length of the additional data.
144*103e46e4SHarish Mahendrakar   uint64_t additional_length_;
145*103e46e4SHarish Mahendrakar 
146*103e46e4SHarish Mahendrakar   // Duration of the frame in nanoseconds.
147*103e46e4SHarish Mahendrakar   uint64_t duration_;
148*103e46e4SHarish Mahendrakar 
149*103e46e4SHarish Mahendrakar   // Flag indicating that |duration_| has been set. Setting duration causes the
150*103e46e4SHarish Mahendrakar   // frame to be written out as a Block with BlockDuration instead of as a
151*103e46e4SHarish Mahendrakar   // SimpleBlock.
152*103e46e4SHarish Mahendrakar   bool duration_set_;
153*103e46e4SHarish Mahendrakar 
154*103e46e4SHarish Mahendrakar   // Pointer to the data. Owned by this class.
155*103e46e4SHarish Mahendrakar   uint8_t* frame_;
156*103e46e4SHarish Mahendrakar 
157*103e46e4SHarish Mahendrakar   // Flag telling if the data should set the key flag of a block.
158*103e46e4SHarish Mahendrakar   bool is_key_;
159*103e46e4SHarish Mahendrakar 
160*103e46e4SHarish Mahendrakar   // Length of the data.
161*103e46e4SHarish Mahendrakar   uint64_t length_;
162*103e46e4SHarish Mahendrakar 
163*103e46e4SHarish Mahendrakar   // Mkv track number the data is associated with.
164*103e46e4SHarish Mahendrakar   uint64_t track_number_;
165*103e46e4SHarish Mahendrakar 
166*103e46e4SHarish Mahendrakar   // Timestamp of the data in nanoseconds.
167*103e46e4SHarish Mahendrakar   uint64_t timestamp_;
168*103e46e4SHarish Mahendrakar 
169*103e46e4SHarish Mahendrakar   // Discard padding for the frame.
170*103e46e4SHarish Mahendrakar   int64_t discard_padding_;
171*103e46e4SHarish Mahendrakar 
172*103e46e4SHarish Mahendrakar   // Reference block timestamp.
173*103e46e4SHarish Mahendrakar   int64_t reference_block_timestamp_;
174*103e46e4SHarish Mahendrakar 
175*103e46e4SHarish Mahendrakar   // Flag indicating if |reference_block_timestamp_| has been set.
176*103e46e4SHarish Mahendrakar   bool reference_block_timestamp_set_;
177*103e46e4SHarish Mahendrakar 
178*103e46e4SHarish Mahendrakar   LIBWEBM_DISALLOW_COPY_AND_ASSIGN(Frame);
179*103e46e4SHarish Mahendrakar };
180*103e46e4SHarish Mahendrakar 
181*103e46e4SHarish Mahendrakar ///////////////////////////////////////////////////////////////
182*103e46e4SHarish Mahendrakar // Class to hold one cue point in a Cues element.
183*103e46e4SHarish Mahendrakar class CuePoint {
184*103e46e4SHarish Mahendrakar  public:
185*103e46e4SHarish Mahendrakar   CuePoint();
186*103e46e4SHarish Mahendrakar   ~CuePoint();
187*103e46e4SHarish Mahendrakar 
188*103e46e4SHarish Mahendrakar   // Returns the size in bytes for the entire CuePoint element.
189*103e46e4SHarish Mahendrakar   uint64_t Size() const;
190*103e46e4SHarish Mahendrakar 
191*103e46e4SHarish Mahendrakar   // Output the CuePoint element to the writer. Returns true on success.
192*103e46e4SHarish Mahendrakar   bool Write(IMkvWriter* writer) const;
193*103e46e4SHarish Mahendrakar 
set_time(uint64_t time)194*103e46e4SHarish Mahendrakar   void set_time(uint64_t time) { time_ = time; }
time()195*103e46e4SHarish Mahendrakar   uint64_t time() const { return time_; }
set_track(uint64_t track)196*103e46e4SHarish Mahendrakar   void set_track(uint64_t track) { track_ = track; }
track()197*103e46e4SHarish Mahendrakar   uint64_t track() const { return track_; }
set_cluster_pos(uint64_t cluster_pos)198*103e46e4SHarish Mahendrakar   void set_cluster_pos(uint64_t cluster_pos) { cluster_pos_ = cluster_pos; }
cluster_pos()199*103e46e4SHarish Mahendrakar   uint64_t cluster_pos() const { return cluster_pos_; }
set_block_number(uint64_t block_number)200*103e46e4SHarish Mahendrakar   void set_block_number(uint64_t block_number) { block_number_ = block_number; }
block_number()201*103e46e4SHarish Mahendrakar   uint64_t block_number() const { return block_number_; }
set_output_block_number(bool output_block_number)202*103e46e4SHarish Mahendrakar   void set_output_block_number(bool output_block_number) {
203*103e46e4SHarish Mahendrakar     output_block_number_ = output_block_number;
204*103e46e4SHarish Mahendrakar   }
output_block_number()205*103e46e4SHarish Mahendrakar   bool output_block_number() const { return output_block_number_; }
206*103e46e4SHarish Mahendrakar 
207*103e46e4SHarish Mahendrakar  private:
208*103e46e4SHarish Mahendrakar   // Returns the size in bytes for the payload of the CuePoint element.
209*103e46e4SHarish Mahendrakar   uint64_t PayloadSize() const;
210*103e46e4SHarish Mahendrakar 
211*103e46e4SHarish Mahendrakar   // Absolute timecode according to the segment time base.
212*103e46e4SHarish Mahendrakar   uint64_t time_;
213*103e46e4SHarish Mahendrakar 
214*103e46e4SHarish Mahendrakar   // The Track element associated with the CuePoint.
215*103e46e4SHarish Mahendrakar   uint64_t track_;
216*103e46e4SHarish Mahendrakar 
217*103e46e4SHarish Mahendrakar   // The position of the Cluster containing the Block.
218*103e46e4SHarish Mahendrakar   uint64_t cluster_pos_;
219*103e46e4SHarish Mahendrakar 
220*103e46e4SHarish Mahendrakar   // Number of the Block within the Cluster, starting from 1.
221*103e46e4SHarish Mahendrakar   uint64_t block_number_;
222*103e46e4SHarish Mahendrakar 
223*103e46e4SHarish Mahendrakar   // If true the muxer will write out the block number for the cue if the
224*103e46e4SHarish Mahendrakar   // block number is different than the default of 1. Default is set to true.
225*103e46e4SHarish Mahendrakar   bool output_block_number_;
226*103e46e4SHarish Mahendrakar 
227*103e46e4SHarish Mahendrakar   LIBWEBM_DISALLOW_COPY_AND_ASSIGN(CuePoint);
228*103e46e4SHarish Mahendrakar };
229*103e46e4SHarish Mahendrakar 
230*103e46e4SHarish Mahendrakar ///////////////////////////////////////////////////////////////
231*103e46e4SHarish Mahendrakar // Cues element.
232*103e46e4SHarish Mahendrakar class Cues {
233*103e46e4SHarish Mahendrakar  public:
234*103e46e4SHarish Mahendrakar   Cues();
235*103e46e4SHarish Mahendrakar   ~Cues();
236*103e46e4SHarish Mahendrakar 
237*103e46e4SHarish Mahendrakar   // Adds a cue point to the Cues element. Returns true on success.
238*103e46e4SHarish Mahendrakar   bool AddCue(CuePoint* cue);
239*103e46e4SHarish Mahendrakar 
240*103e46e4SHarish Mahendrakar   // Returns the cue point by index. Returns NULL if there is no cue point
241*103e46e4SHarish Mahendrakar   // match.
242*103e46e4SHarish Mahendrakar   CuePoint* GetCueByIndex(int32_t index) const;
243*103e46e4SHarish Mahendrakar 
244*103e46e4SHarish Mahendrakar   // Returns the total size of the Cues element
245*103e46e4SHarish Mahendrakar   uint64_t Size();
246*103e46e4SHarish Mahendrakar 
247*103e46e4SHarish Mahendrakar   // Output the Cues element to the writer. Returns true on success.
248*103e46e4SHarish Mahendrakar   bool Write(IMkvWriter* writer) const;
249*103e46e4SHarish Mahendrakar 
cue_entries_size()250*103e46e4SHarish Mahendrakar   int32_t cue_entries_size() const { return cue_entries_size_; }
set_output_block_number(bool output_block_number)251*103e46e4SHarish Mahendrakar   void set_output_block_number(bool output_block_number) {
252*103e46e4SHarish Mahendrakar     output_block_number_ = output_block_number;
253*103e46e4SHarish Mahendrakar   }
output_block_number()254*103e46e4SHarish Mahendrakar   bool output_block_number() const { return output_block_number_; }
255*103e46e4SHarish Mahendrakar 
256*103e46e4SHarish Mahendrakar  private:
257*103e46e4SHarish Mahendrakar   // Number of allocated elements in |cue_entries_|.
258*103e46e4SHarish Mahendrakar   int32_t cue_entries_capacity_;
259*103e46e4SHarish Mahendrakar 
260*103e46e4SHarish Mahendrakar   // Number of CuePoints in |cue_entries_|.
261*103e46e4SHarish Mahendrakar   int32_t cue_entries_size_;
262*103e46e4SHarish Mahendrakar 
263*103e46e4SHarish Mahendrakar   // CuePoint list.
264*103e46e4SHarish Mahendrakar   CuePoint** cue_entries_;
265*103e46e4SHarish Mahendrakar 
266*103e46e4SHarish Mahendrakar   // If true the muxer will write out the block number for the cue if the
267*103e46e4SHarish Mahendrakar   // block number is different than the default of 1. Default is set to true.
268*103e46e4SHarish Mahendrakar   bool output_block_number_;
269*103e46e4SHarish Mahendrakar 
270*103e46e4SHarish Mahendrakar   LIBWEBM_DISALLOW_COPY_AND_ASSIGN(Cues);
271*103e46e4SHarish Mahendrakar };
272*103e46e4SHarish Mahendrakar 
273*103e46e4SHarish Mahendrakar ///////////////////////////////////////////////////////////////
274*103e46e4SHarish Mahendrakar // ContentEncAESSettings element
275*103e46e4SHarish Mahendrakar class ContentEncAESSettings {
276*103e46e4SHarish Mahendrakar  public:
277*103e46e4SHarish Mahendrakar   enum { kCTR = 1 };
278*103e46e4SHarish Mahendrakar 
279*103e46e4SHarish Mahendrakar   ContentEncAESSettings();
~ContentEncAESSettings()280*103e46e4SHarish Mahendrakar   ~ContentEncAESSettings() {}
281*103e46e4SHarish Mahendrakar 
282*103e46e4SHarish Mahendrakar   // Returns the size in bytes for the ContentEncAESSettings element.
283*103e46e4SHarish Mahendrakar   uint64_t Size() const;
284*103e46e4SHarish Mahendrakar 
285*103e46e4SHarish Mahendrakar   // Writes out the ContentEncAESSettings element to |writer|. Returns true on
286*103e46e4SHarish Mahendrakar   // success.
287*103e46e4SHarish Mahendrakar   bool Write(IMkvWriter* writer) const;
288*103e46e4SHarish Mahendrakar 
cipher_mode()289*103e46e4SHarish Mahendrakar   uint64_t cipher_mode() const { return cipher_mode_; }
290*103e46e4SHarish Mahendrakar 
291*103e46e4SHarish Mahendrakar  private:
292*103e46e4SHarish Mahendrakar   // Returns the size in bytes for the payload of the ContentEncAESSettings
293*103e46e4SHarish Mahendrakar   // element.
294*103e46e4SHarish Mahendrakar   uint64_t PayloadSize() const;
295*103e46e4SHarish Mahendrakar 
296*103e46e4SHarish Mahendrakar   // Sub elements
297*103e46e4SHarish Mahendrakar   uint64_t cipher_mode_;
298*103e46e4SHarish Mahendrakar 
299*103e46e4SHarish Mahendrakar   LIBWEBM_DISALLOW_COPY_AND_ASSIGN(ContentEncAESSettings);
300*103e46e4SHarish Mahendrakar };
301*103e46e4SHarish Mahendrakar 
302*103e46e4SHarish Mahendrakar ///////////////////////////////////////////////////////////////
303*103e46e4SHarish Mahendrakar // ContentEncoding element
304*103e46e4SHarish Mahendrakar // Elements used to describe if the track data has been encrypted or
305*103e46e4SHarish Mahendrakar // compressed with zlib or header stripping.
306*103e46e4SHarish Mahendrakar // Currently only whole frames can be encrypted with AES. This dictates that
307*103e46e4SHarish Mahendrakar // ContentEncodingOrder will be 0, ContentEncodingScope will be 1,
308*103e46e4SHarish Mahendrakar // ContentEncodingType will be 1, and ContentEncAlgo will be 5.
309*103e46e4SHarish Mahendrakar class ContentEncoding {
310*103e46e4SHarish Mahendrakar  public:
311*103e46e4SHarish Mahendrakar   ContentEncoding();
312*103e46e4SHarish Mahendrakar   ~ContentEncoding();
313*103e46e4SHarish Mahendrakar 
314*103e46e4SHarish Mahendrakar   // Sets the content encryption id. Copies |length| bytes from |id| to
315*103e46e4SHarish Mahendrakar   // |enc_key_id_|. Returns true on success.
316*103e46e4SHarish Mahendrakar   bool SetEncryptionID(const uint8_t* id, uint64_t length);
317*103e46e4SHarish Mahendrakar 
318*103e46e4SHarish Mahendrakar   // Returns the size in bytes for the ContentEncoding element.
319*103e46e4SHarish Mahendrakar   uint64_t Size() const;
320*103e46e4SHarish Mahendrakar 
321*103e46e4SHarish Mahendrakar   // Writes out the ContentEncoding element to |writer|. Returns true on
322*103e46e4SHarish Mahendrakar   // success.
323*103e46e4SHarish Mahendrakar   bool Write(IMkvWriter* writer) const;
324*103e46e4SHarish Mahendrakar 
enc_algo()325*103e46e4SHarish Mahendrakar   uint64_t enc_algo() const { return enc_algo_; }
encoding_order()326*103e46e4SHarish Mahendrakar   uint64_t encoding_order() const { return encoding_order_; }
encoding_scope()327*103e46e4SHarish Mahendrakar   uint64_t encoding_scope() const { return encoding_scope_; }
encoding_type()328*103e46e4SHarish Mahendrakar   uint64_t encoding_type() const { return encoding_type_; }
enc_aes_settings()329*103e46e4SHarish Mahendrakar   ContentEncAESSettings* enc_aes_settings() { return &enc_aes_settings_; }
330*103e46e4SHarish Mahendrakar 
331*103e46e4SHarish Mahendrakar  private:
332*103e46e4SHarish Mahendrakar   // Returns the size in bytes for the encoding elements.
333*103e46e4SHarish Mahendrakar   uint64_t EncodingSize(uint64_t compression_size,
334*103e46e4SHarish Mahendrakar                         uint64_t encryption_size) const;
335*103e46e4SHarish Mahendrakar 
336*103e46e4SHarish Mahendrakar   // Returns the size in bytes for the encryption elements.
337*103e46e4SHarish Mahendrakar   uint64_t EncryptionSize() const;
338*103e46e4SHarish Mahendrakar 
339*103e46e4SHarish Mahendrakar   // Track element names
340*103e46e4SHarish Mahendrakar   uint64_t enc_algo_;
341*103e46e4SHarish Mahendrakar   uint8_t* enc_key_id_;
342*103e46e4SHarish Mahendrakar   uint64_t encoding_order_;
343*103e46e4SHarish Mahendrakar   uint64_t encoding_scope_;
344*103e46e4SHarish Mahendrakar   uint64_t encoding_type_;
345*103e46e4SHarish Mahendrakar 
346*103e46e4SHarish Mahendrakar   // ContentEncAESSettings element.
347*103e46e4SHarish Mahendrakar   ContentEncAESSettings enc_aes_settings_;
348*103e46e4SHarish Mahendrakar 
349*103e46e4SHarish Mahendrakar   // Size of the ContentEncKeyID data in bytes.
350*103e46e4SHarish Mahendrakar   uint64_t enc_key_id_length_;
351*103e46e4SHarish Mahendrakar 
352*103e46e4SHarish Mahendrakar   LIBWEBM_DISALLOW_COPY_AND_ASSIGN(ContentEncoding);
353*103e46e4SHarish Mahendrakar };
354*103e46e4SHarish Mahendrakar 
355*103e46e4SHarish Mahendrakar ///////////////////////////////////////////////////////////////
356*103e46e4SHarish Mahendrakar // Colour element.
357*103e46e4SHarish Mahendrakar class PrimaryChromaticity {
358*103e46e4SHarish Mahendrakar  public:
359*103e46e4SHarish Mahendrakar   static const float kChromaticityMin;
360*103e46e4SHarish Mahendrakar   static const float kChromaticityMax;
361*103e46e4SHarish Mahendrakar 
PrimaryChromaticity(float x_val,float y_val)362*103e46e4SHarish Mahendrakar   PrimaryChromaticity(float x_val, float y_val) : x_(x_val), y_(y_val) {}
PrimaryChromaticity()363*103e46e4SHarish Mahendrakar   PrimaryChromaticity() : x_(0), y_(0) {}
~PrimaryChromaticity()364*103e46e4SHarish Mahendrakar   ~PrimaryChromaticity() {}
365*103e46e4SHarish Mahendrakar 
366*103e46e4SHarish Mahendrakar   // Returns sum of |x_id| and |y_id| element id sizes and payload sizes.
367*103e46e4SHarish Mahendrakar   uint64_t PrimaryChromaticitySize(libwebm::MkvId x_id,
368*103e46e4SHarish Mahendrakar                                    libwebm::MkvId y_id) const;
369*103e46e4SHarish Mahendrakar   bool Valid() const;
370*103e46e4SHarish Mahendrakar   bool Write(IMkvWriter* writer, libwebm::MkvId x_id,
371*103e46e4SHarish Mahendrakar              libwebm::MkvId y_id) const;
372*103e46e4SHarish Mahendrakar 
x()373*103e46e4SHarish Mahendrakar   float x() const { return x_; }
set_x(float new_x)374*103e46e4SHarish Mahendrakar   void set_x(float new_x) { x_ = new_x; }
y()375*103e46e4SHarish Mahendrakar   float y() const { return y_; }
set_y(float new_y)376*103e46e4SHarish Mahendrakar   void set_y(float new_y) { y_ = new_y; }
377*103e46e4SHarish Mahendrakar 
378*103e46e4SHarish Mahendrakar  private:
379*103e46e4SHarish Mahendrakar   float x_;
380*103e46e4SHarish Mahendrakar   float y_;
381*103e46e4SHarish Mahendrakar };
382*103e46e4SHarish Mahendrakar 
383*103e46e4SHarish Mahendrakar class MasteringMetadata {
384*103e46e4SHarish Mahendrakar  public:
385*103e46e4SHarish Mahendrakar   static const float kValueNotPresent;
386*103e46e4SHarish Mahendrakar   static const float kMinLuminance;
387*103e46e4SHarish Mahendrakar   static const float kMinLuminanceMax;
388*103e46e4SHarish Mahendrakar   static const float kMaxLuminanceMax;
389*103e46e4SHarish Mahendrakar 
MasteringMetadata()390*103e46e4SHarish Mahendrakar   MasteringMetadata()
391*103e46e4SHarish Mahendrakar       : luminance_max_(kValueNotPresent),
392*103e46e4SHarish Mahendrakar         luminance_min_(kValueNotPresent),
393*103e46e4SHarish Mahendrakar         r_(NULL),
394*103e46e4SHarish Mahendrakar         g_(NULL),
395*103e46e4SHarish Mahendrakar         b_(NULL),
396*103e46e4SHarish Mahendrakar         white_point_(NULL) {}
~MasteringMetadata()397*103e46e4SHarish Mahendrakar   ~MasteringMetadata() {
398*103e46e4SHarish Mahendrakar     delete r_;
399*103e46e4SHarish Mahendrakar     delete g_;
400*103e46e4SHarish Mahendrakar     delete b_;
401*103e46e4SHarish Mahendrakar     delete white_point_;
402*103e46e4SHarish Mahendrakar   }
403*103e46e4SHarish Mahendrakar 
404*103e46e4SHarish Mahendrakar   // Returns total size of the MasteringMetadata element.
405*103e46e4SHarish Mahendrakar   uint64_t MasteringMetadataSize() const;
406*103e46e4SHarish Mahendrakar   bool Valid() const;
407*103e46e4SHarish Mahendrakar   bool Write(IMkvWriter* writer) const;
408*103e46e4SHarish Mahendrakar 
409*103e46e4SHarish Mahendrakar   // Copies non-null chromaticity.
410*103e46e4SHarish Mahendrakar   bool SetChromaticity(const PrimaryChromaticity* r,
411*103e46e4SHarish Mahendrakar                        const PrimaryChromaticity* g,
412*103e46e4SHarish Mahendrakar                        const PrimaryChromaticity* b,
413*103e46e4SHarish Mahendrakar                        const PrimaryChromaticity* white_point);
r()414*103e46e4SHarish Mahendrakar   const PrimaryChromaticity* r() const { return r_; }
g()415*103e46e4SHarish Mahendrakar   const PrimaryChromaticity* g() const { return g_; }
b()416*103e46e4SHarish Mahendrakar   const PrimaryChromaticity* b() const { return b_; }
white_point()417*103e46e4SHarish Mahendrakar   const PrimaryChromaticity* white_point() const { return white_point_; }
418*103e46e4SHarish Mahendrakar 
luminance_max()419*103e46e4SHarish Mahendrakar   float luminance_max() const { return luminance_max_; }
set_luminance_max(float luminance_max)420*103e46e4SHarish Mahendrakar   void set_luminance_max(float luminance_max) {
421*103e46e4SHarish Mahendrakar     luminance_max_ = luminance_max;
422*103e46e4SHarish Mahendrakar   }
luminance_min()423*103e46e4SHarish Mahendrakar   float luminance_min() const { return luminance_min_; }
set_luminance_min(float luminance_min)424*103e46e4SHarish Mahendrakar   void set_luminance_min(float luminance_min) {
425*103e46e4SHarish Mahendrakar     luminance_min_ = luminance_min;
426*103e46e4SHarish Mahendrakar   }
427*103e46e4SHarish Mahendrakar 
428*103e46e4SHarish Mahendrakar  private:
429*103e46e4SHarish Mahendrakar   // Returns size of MasteringMetadata child elements.
430*103e46e4SHarish Mahendrakar   uint64_t PayloadSize() const;
431*103e46e4SHarish Mahendrakar 
432*103e46e4SHarish Mahendrakar   float luminance_max_;
433*103e46e4SHarish Mahendrakar   float luminance_min_;
434*103e46e4SHarish Mahendrakar   PrimaryChromaticity* r_;
435*103e46e4SHarish Mahendrakar   PrimaryChromaticity* g_;
436*103e46e4SHarish Mahendrakar   PrimaryChromaticity* b_;
437*103e46e4SHarish Mahendrakar   PrimaryChromaticity* white_point_;
438*103e46e4SHarish Mahendrakar };
439*103e46e4SHarish Mahendrakar 
440*103e46e4SHarish Mahendrakar class Colour {
441*103e46e4SHarish Mahendrakar  public:
442*103e46e4SHarish Mahendrakar   enum MatrixCoefficients {
443*103e46e4SHarish Mahendrakar     kGbr = 0,
444*103e46e4SHarish Mahendrakar     kBt709 = 1,
445*103e46e4SHarish Mahendrakar     kUnspecifiedMc = 2,
446*103e46e4SHarish Mahendrakar     kReserved = 3,
447*103e46e4SHarish Mahendrakar     kFcc = 4,
448*103e46e4SHarish Mahendrakar     kBt470bg = 5,
449*103e46e4SHarish Mahendrakar     kSmpte170MMc = 6,
450*103e46e4SHarish Mahendrakar     kSmpte240MMc = 7,
451*103e46e4SHarish Mahendrakar     kYcocg = 8,
452*103e46e4SHarish Mahendrakar     kBt2020NonConstantLuminance = 9,
453*103e46e4SHarish Mahendrakar     kBt2020ConstantLuminance = 10,
454*103e46e4SHarish Mahendrakar   };
455*103e46e4SHarish Mahendrakar   enum ChromaSitingHorz {
456*103e46e4SHarish Mahendrakar     kUnspecifiedCsh = 0,
457*103e46e4SHarish Mahendrakar     kLeftCollocated = 1,
458*103e46e4SHarish Mahendrakar     kHalfCsh = 2,
459*103e46e4SHarish Mahendrakar   };
460*103e46e4SHarish Mahendrakar   enum ChromaSitingVert {
461*103e46e4SHarish Mahendrakar     kUnspecifiedCsv = 0,
462*103e46e4SHarish Mahendrakar     kTopCollocated = 1,
463*103e46e4SHarish Mahendrakar     kHalfCsv = 2,
464*103e46e4SHarish Mahendrakar   };
465*103e46e4SHarish Mahendrakar   enum Range {
466*103e46e4SHarish Mahendrakar     kUnspecifiedCr = 0,
467*103e46e4SHarish Mahendrakar     kBroadcastRange = 1,
468*103e46e4SHarish Mahendrakar     kFullRange = 2,
469*103e46e4SHarish Mahendrakar     kMcTcDefined = 3,  // Defined by MatrixCoefficients/TransferCharacteristics.
470*103e46e4SHarish Mahendrakar   };
471*103e46e4SHarish Mahendrakar   enum TransferCharacteristics {
472*103e46e4SHarish Mahendrakar     kIturBt709Tc = 1,
473*103e46e4SHarish Mahendrakar     kUnspecifiedTc = 2,
474*103e46e4SHarish Mahendrakar     kReservedTc = 3,
475*103e46e4SHarish Mahendrakar     kGamma22Curve = 4,
476*103e46e4SHarish Mahendrakar     kGamma28Curve = 5,
477*103e46e4SHarish Mahendrakar     kSmpte170MTc = 6,
478*103e46e4SHarish Mahendrakar     kSmpte240MTc = 7,
479*103e46e4SHarish Mahendrakar     kLinear = 8,
480*103e46e4SHarish Mahendrakar     kLog = 9,
481*103e46e4SHarish Mahendrakar     kLogSqrt = 10,
482*103e46e4SHarish Mahendrakar     kIec6196624 = 11,
483*103e46e4SHarish Mahendrakar     kIturBt1361ExtendedColourGamut = 12,
484*103e46e4SHarish Mahendrakar     kIec6196621 = 13,
485*103e46e4SHarish Mahendrakar     kIturBt202010bit = 14,
486*103e46e4SHarish Mahendrakar     kIturBt202012bit = 15,
487*103e46e4SHarish Mahendrakar     kSmpteSt2084 = 16,
488*103e46e4SHarish Mahendrakar     kSmpteSt4281Tc = 17,
489*103e46e4SHarish Mahendrakar     kAribStdB67Hlg = 18,
490*103e46e4SHarish Mahendrakar   };
491*103e46e4SHarish Mahendrakar   enum Primaries {
492*103e46e4SHarish Mahendrakar     kReservedP0 = 0,
493*103e46e4SHarish Mahendrakar     kIturBt709P = 1,
494*103e46e4SHarish Mahendrakar     kUnspecifiedP = 2,
495*103e46e4SHarish Mahendrakar     kReservedP3 = 3,
496*103e46e4SHarish Mahendrakar     kIturBt470M = 4,
497*103e46e4SHarish Mahendrakar     kIturBt470Bg = 5,
498*103e46e4SHarish Mahendrakar     kSmpte170MP = 6,
499*103e46e4SHarish Mahendrakar     kSmpte240MP = 7,
500*103e46e4SHarish Mahendrakar     kFilm = 8,
501*103e46e4SHarish Mahendrakar     kIturBt2020 = 9,
502*103e46e4SHarish Mahendrakar     kSmpteSt4281P = 10,
503*103e46e4SHarish Mahendrakar     kJedecP22Phosphors = 22,
504*103e46e4SHarish Mahendrakar   };
505*103e46e4SHarish Mahendrakar   static const uint64_t kValueNotPresent;
Colour()506*103e46e4SHarish Mahendrakar   Colour()
507*103e46e4SHarish Mahendrakar       : matrix_coefficients_(kValueNotPresent),
508*103e46e4SHarish Mahendrakar         bits_per_channel_(kValueNotPresent),
509*103e46e4SHarish Mahendrakar         chroma_subsampling_horz_(kValueNotPresent),
510*103e46e4SHarish Mahendrakar         chroma_subsampling_vert_(kValueNotPresent),
511*103e46e4SHarish Mahendrakar         cb_subsampling_horz_(kValueNotPresent),
512*103e46e4SHarish Mahendrakar         cb_subsampling_vert_(kValueNotPresent),
513*103e46e4SHarish Mahendrakar         chroma_siting_horz_(kValueNotPresent),
514*103e46e4SHarish Mahendrakar         chroma_siting_vert_(kValueNotPresent),
515*103e46e4SHarish Mahendrakar         range_(kValueNotPresent),
516*103e46e4SHarish Mahendrakar         transfer_characteristics_(kValueNotPresent),
517*103e46e4SHarish Mahendrakar         primaries_(kValueNotPresent),
518*103e46e4SHarish Mahendrakar         max_cll_(kValueNotPresent),
519*103e46e4SHarish Mahendrakar         max_fall_(kValueNotPresent),
520*103e46e4SHarish Mahendrakar         mastering_metadata_(NULL) {}
~Colour()521*103e46e4SHarish Mahendrakar   ~Colour() { delete mastering_metadata_; }
522*103e46e4SHarish Mahendrakar 
523*103e46e4SHarish Mahendrakar   // Returns total size of the Colour element.
524*103e46e4SHarish Mahendrakar   uint64_t ColourSize() const;
525*103e46e4SHarish Mahendrakar   bool Valid() const;
526*103e46e4SHarish Mahendrakar   bool Write(IMkvWriter* writer) const;
527*103e46e4SHarish Mahendrakar 
528*103e46e4SHarish Mahendrakar   // Deep copies |mastering_metadata|.
529*103e46e4SHarish Mahendrakar   bool SetMasteringMetadata(const MasteringMetadata& mastering_metadata);
530*103e46e4SHarish Mahendrakar 
mastering_metadata()531*103e46e4SHarish Mahendrakar   const MasteringMetadata* mastering_metadata() const {
532*103e46e4SHarish Mahendrakar     return mastering_metadata_;
533*103e46e4SHarish Mahendrakar   }
534*103e46e4SHarish Mahendrakar 
matrix_coefficients()535*103e46e4SHarish Mahendrakar   uint64_t matrix_coefficients() const { return matrix_coefficients_; }
set_matrix_coefficients(uint64_t matrix_coefficients)536*103e46e4SHarish Mahendrakar   void set_matrix_coefficients(uint64_t matrix_coefficients) {
537*103e46e4SHarish Mahendrakar     matrix_coefficients_ = matrix_coefficients;
538*103e46e4SHarish Mahendrakar   }
bits_per_channel()539*103e46e4SHarish Mahendrakar   uint64_t bits_per_channel() const { return bits_per_channel_; }
set_bits_per_channel(uint64_t bits_per_channel)540*103e46e4SHarish Mahendrakar   void set_bits_per_channel(uint64_t bits_per_channel) {
541*103e46e4SHarish Mahendrakar     bits_per_channel_ = bits_per_channel;
542*103e46e4SHarish Mahendrakar   }
chroma_subsampling_horz()543*103e46e4SHarish Mahendrakar   uint64_t chroma_subsampling_horz() const { return chroma_subsampling_horz_; }
set_chroma_subsampling_horz(uint64_t chroma_subsampling_horz)544*103e46e4SHarish Mahendrakar   void set_chroma_subsampling_horz(uint64_t chroma_subsampling_horz) {
545*103e46e4SHarish Mahendrakar     chroma_subsampling_horz_ = chroma_subsampling_horz;
546*103e46e4SHarish Mahendrakar   }
chroma_subsampling_vert()547*103e46e4SHarish Mahendrakar   uint64_t chroma_subsampling_vert() const { return chroma_subsampling_vert_; }
set_chroma_subsampling_vert(uint64_t chroma_subsampling_vert)548*103e46e4SHarish Mahendrakar   void set_chroma_subsampling_vert(uint64_t chroma_subsampling_vert) {
549*103e46e4SHarish Mahendrakar     chroma_subsampling_vert_ = chroma_subsampling_vert;
550*103e46e4SHarish Mahendrakar   }
cb_subsampling_horz()551*103e46e4SHarish Mahendrakar   uint64_t cb_subsampling_horz() const { return cb_subsampling_horz_; }
set_cb_subsampling_horz(uint64_t cb_subsampling_horz)552*103e46e4SHarish Mahendrakar   void set_cb_subsampling_horz(uint64_t cb_subsampling_horz) {
553*103e46e4SHarish Mahendrakar     cb_subsampling_horz_ = cb_subsampling_horz;
554*103e46e4SHarish Mahendrakar   }
cb_subsampling_vert()555*103e46e4SHarish Mahendrakar   uint64_t cb_subsampling_vert() const { return cb_subsampling_vert_; }
set_cb_subsampling_vert(uint64_t cb_subsampling_vert)556*103e46e4SHarish Mahendrakar   void set_cb_subsampling_vert(uint64_t cb_subsampling_vert) {
557*103e46e4SHarish Mahendrakar     cb_subsampling_vert_ = cb_subsampling_vert;
558*103e46e4SHarish Mahendrakar   }
chroma_siting_horz()559*103e46e4SHarish Mahendrakar   uint64_t chroma_siting_horz() const { return chroma_siting_horz_; }
set_chroma_siting_horz(uint64_t chroma_siting_horz)560*103e46e4SHarish Mahendrakar   void set_chroma_siting_horz(uint64_t chroma_siting_horz) {
561*103e46e4SHarish Mahendrakar     chroma_siting_horz_ = chroma_siting_horz;
562*103e46e4SHarish Mahendrakar   }
chroma_siting_vert()563*103e46e4SHarish Mahendrakar   uint64_t chroma_siting_vert() const { return chroma_siting_vert_; }
set_chroma_siting_vert(uint64_t chroma_siting_vert)564*103e46e4SHarish Mahendrakar   void set_chroma_siting_vert(uint64_t chroma_siting_vert) {
565*103e46e4SHarish Mahendrakar     chroma_siting_vert_ = chroma_siting_vert;
566*103e46e4SHarish Mahendrakar   }
range()567*103e46e4SHarish Mahendrakar   uint64_t range() const { return range_; }
set_range(uint64_t range)568*103e46e4SHarish Mahendrakar   void set_range(uint64_t range) { range_ = range; }
transfer_characteristics()569*103e46e4SHarish Mahendrakar   uint64_t transfer_characteristics() const {
570*103e46e4SHarish Mahendrakar     return transfer_characteristics_;
571*103e46e4SHarish Mahendrakar   }
set_transfer_characteristics(uint64_t transfer_characteristics)572*103e46e4SHarish Mahendrakar   void set_transfer_characteristics(uint64_t transfer_characteristics) {
573*103e46e4SHarish Mahendrakar     transfer_characteristics_ = transfer_characteristics;
574*103e46e4SHarish Mahendrakar   }
primaries()575*103e46e4SHarish Mahendrakar   uint64_t primaries() const { return primaries_; }
set_primaries(uint64_t primaries)576*103e46e4SHarish Mahendrakar   void set_primaries(uint64_t primaries) { primaries_ = primaries; }
max_cll()577*103e46e4SHarish Mahendrakar   uint64_t max_cll() const { return max_cll_; }
set_max_cll(uint64_t max_cll)578*103e46e4SHarish Mahendrakar   void set_max_cll(uint64_t max_cll) { max_cll_ = max_cll; }
max_fall()579*103e46e4SHarish Mahendrakar   uint64_t max_fall() const { return max_fall_; }
set_max_fall(uint64_t max_fall)580*103e46e4SHarish Mahendrakar   void set_max_fall(uint64_t max_fall) { max_fall_ = max_fall; }
581*103e46e4SHarish Mahendrakar 
582*103e46e4SHarish Mahendrakar  private:
583*103e46e4SHarish Mahendrakar   // Returns size of Colour child elements.
584*103e46e4SHarish Mahendrakar   uint64_t PayloadSize() const;
585*103e46e4SHarish Mahendrakar 
586*103e46e4SHarish Mahendrakar   uint64_t matrix_coefficients_;
587*103e46e4SHarish Mahendrakar   uint64_t bits_per_channel_;
588*103e46e4SHarish Mahendrakar   uint64_t chroma_subsampling_horz_;
589*103e46e4SHarish Mahendrakar   uint64_t chroma_subsampling_vert_;
590*103e46e4SHarish Mahendrakar   uint64_t cb_subsampling_horz_;
591*103e46e4SHarish Mahendrakar   uint64_t cb_subsampling_vert_;
592*103e46e4SHarish Mahendrakar   uint64_t chroma_siting_horz_;
593*103e46e4SHarish Mahendrakar   uint64_t chroma_siting_vert_;
594*103e46e4SHarish Mahendrakar   uint64_t range_;
595*103e46e4SHarish Mahendrakar   uint64_t transfer_characteristics_;
596*103e46e4SHarish Mahendrakar   uint64_t primaries_;
597*103e46e4SHarish Mahendrakar   uint64_t max_cll_;
598*103e46e4SHarish Mahendrakar   uint64_t max_fall_;
599*103e46e4SHarish Mahendrakar 
600*103e46e4SHarish Mahendrakar   MasteringMetadata* mastering_metadata_;
601*103e46e4SHarish Mahendrakar };
602*103e46e4SHarish Mahendrakar 
603*103e46e4SHarish Mahendrakar ///////////////////////////////////////////////////////////////
604*103e46e4SHarish Mahendrakar // Projection element.
605*103e46e4SHarish Mahendrakar class Projection {
606*103e46e4SHarish Mahendrakar  public:
607*103e46e4SHarish Mahendrakar   enum ProjectionType {
608*103e46e4SHarish Mahendrakar     kTypeNotPresent = -1,
609*103e46e4SHarish Mahendrakar     kRectangular = 0,
610*103e46e4SHarish Mahendrakar     kEquirectangular = 1,
611*103e46e4SHarish Mahendrakar     kCubeMap = 2,
612*103e46e4SHarish Mahendrakar     kMesh = 3,
613*103e46e4SHarish Mahendrakar   };
614*103e46e4SHarish Mahendrakar   static const uint64_t kValueNotPresent;
Projection()615*103e46e4SHarish Mahendrakar   Projection()
616*103e46e4SHarish Mahendrakar       : type_(kRectangular),
617*103e46e4SHarish Mahendrakar         pose_yaw_(0.0),
618*103e46e4SHarish Mahendrakar         pose_pitch_(0.0),
619*103e46e4SHarish Mahendrakar         pose_roll_(0.0),
620*103e46e4SHarish Mahendrakar         private_data_(NULL),
621*103e46e4SHarish Mahendrakar         private_data_length_(0) {}
~Projection()622*103e46e4SHarish Mahendrakar   ~Projection() { delete[] private_data_; }
623*103e46e4SHarish Mahendrakar 
624*103e46e4SHarish Mahendrakar   uint64_t ProjectionSize() const;
625*103e46e4SHarish Mahendrakar   bool Write(IMkvWriter* writer) const;
626*103e46e4SHarish Mahendrakar 
627*103e46e4SHarish Mahendrakar   bool SetProjectionPrivate(const uint8_t* private_data,
628*103e46e4SHarish Mahendrakar                             uint64_t private_data_length);
629*103e46e4SHarish Mahendrakar 
type()630*103e46e4SHarish Mahendrakar   ProjectionType type() const { return type_; }
set_type(ProjectionType type)631*103e46e4SHarish Mahendrakar   void set_type(ProjectionType type) { type_ = type; }
pose_yaw()632*103e46e4SHarish Mahendrakar   float pose_yaw() const { return pose_yaw_; }
set_pose_yaw(float pose_yaw)633*103e46e4SHarish Mahendrakar   void set_pose_yaw(float pose_yaw) { pose_yaw_ = pose_yaw; }
pose_pitch()634*103e46e4SHarish Mahendrakar   float pose_pitch() const { return pose_pitch_; }
set_pose_pitch(float pose_pitch)635*103e46e4SHarish Mahendrakar   void set_pose_pitch(float pose_pitch) { pose_pitch_ = pose_pitch; }
pose_roll()636*103e46e4SHarish Mahendrakar   float pose_roll() const { return pose_roll_; }
set_pose_roll(float pose_roll)637*103e46e4SHarish Mahendrakar   void set_pose_roll(float pose_roll) { pose_roll_ = pose_roll; }
private_data()638*103e46e4SHarish Mahendrakar   uint8_t* private_data() const { return private_data_; }
private_data_length()639*103e46e4SHarish Mahendrakar   uint64_t private_data_length() const { return private_data_length_; }
640*103e46e4SHarish Mahendrakar 
641*103e46e4SHarish Mahendrakar  private:
642*103e46e4SHarish Mahendrakar   // Returns size of VideoProjection child elements.
643*103e46e4SHarish Mahendrakar   uint64_t PayloadSize() const;
644*103e46e4SHarish Mahendrakar 
645*103e46e4SHarish Mahendrakar   ProjectionType type_;
646*103e46e4SHarish Mahendrakar   float pose_yaw_;
647*103e46e4SHarish Mahendrakar   float pose_pitch_;
648*103e46e4SHarish Mahendrakar   float pose_roll_;
649*103e46e4SHarish Mahendrakar   uint8_t* private_data_;
650*103e46e4SHarish Mahendrakar   uint64_t private_data_length_;
651*103e46e4SHarish Mahendrakar };
652*103e46e4SHarish Mahendrakar 
653*103e46e4SHarish Mahendrakar ///////////////////////////////////////////////////////////////
654*103e46e4SHarish Mahendrakar // Track element.
655*103e46e4SHarish Mahendrakar class Track {
656*103e46e4SHarish Mahendrakar  public:
657*103e46e4SHarish Mahendrakar   // The |seed| parameter is used to synthesize a UID for the track.
658*103e46e4SHarish Mahendrakar   explicit Track(unsigned int* seed);
659*103e46e4SHarish Mahendrakar   virtual ~Track();
660*103e46e4SHarish Mahendrakar 
661*103e46e4SHarish Mahendrakar   // Adds a ContentEncoding element to the Track. Returns true on success.
662*103e46e4SHarish Mahendrakar   virtual bool AddContentEncoding();
663*103e46e4SHarish Mahendrakar 
664*103e46e4SHarish Mahendrakar   // Returns the ContentEncoding by index. Returns NULL if there is no
665*103e46e4SHarish Mahendrakar   // ContentEncoding match.
666*103e46e4SHarish Mahendrakar   ContentEncoding* GetContentEncodingByIndex(uint32_t index) const;
667*103e46e4SHarish Mahendrakar 
668*103e46e4SHarish Mahendrakar   // Returns the size in bytes for the payload of the Track element.
669*103e46e4SHarish Mahendrakar   virtual uint64_t PayloadSize() const;
670*103e46e4SHarish Mahendrakar 
671*103e46e4SHarish Mahendrakar   // Returns the size in bytes of the Track element.
672*103e46e4SHarish Mahendrakar   virtual uint64_t Size() const;
673*103e46e4SHarish Mahendrakar 
674*103e46e4SHarish Mahendrakar   // Output the Track element to the writer. Returns true on success.
675*103e46e4SHarish Mahendrakar   virtual bool Write(IMkvWriter* writer) const;
676*103e46e4SHarish Mahendrakar 
677*103e46e4SHarish Mahendrakar   // Sets the CodecPrivate element of the Track element. Copies |length|
678*103e46e4SHarish Mahendrakar   // bytes from |codec_private| to |codec_private_|. Returns true on success.
679*103e46e4SHarish Mahendrakar   bool SetCodecPrivate(const uint8_t* codec_private, uint64_t length);
680*103e46e4SHarish Mahendrakar 
681*103e46e4SHarish Mahendrakar   void set_codec_id(const char* codec_id);
codec_id()682*103e46e4SHarish Mahendrakar   const char* codec_id() const { return codec_id_; }
codec_private()683*103e46e4SHarish Mahendrakar   const uint8_t* codec_private() const { return codec_private_; }
684*103e46e4SHarish Mahendrakar   void set_language(const char* language);
language()685*103e46e4SHarish Mahendrakar   const char* language() const { return language_; }
set_max_block_additional_id(uint64_t max_block_additional_id)686*103e46e4SHarish Mahendrakar   void set_max_block_additional_id(uint64_t max_block_additional_id) {
687*103e46e4SHarish Mahendrakar     max_block_additional_id_ = max_block_additional_id;
688*103e46e4SHarish Mahendrakar   }
max_block_additional_id()689*103e46e4SHarish Mahendrakar   uint64_t max_block_additional_id() const { return max_block_additional_id_; }
690*103e46e4SHarish Mahendrakar   void set_name(const char* name);
name()691*103e46e4SHarish Mahendrakar   const char* name() const { return name_; }
set_number(uint64_t number)692*103e46e4SHarish Mahendrakar   void set_number(uint64_t number) { number_ = number; }
number()693*103e46e4SHarish Mahendrakar   uint64_t number() const { return number_; }
set_type(uint64_t type)694*103e46e4SHarish Mahendrakar   void set_type(uint64_t type) { type_ = type; }
type()695*103e46e4SHarish Mahendrakar   uint64_t type() const { return type_; }
set_uid(uint64_t uid)696*103e46e4SHarish Mahendrakar   void set_uid(uint64_t uid) { uid_ = uid; }
uid()697*103e46e4SHarish Mahendrakar   uint64_t uid() const { return uid_; }
set_codec_delay(uint64_t codec_delay)698*103e46e4SHarish Mahendrakar   void set_codec_delay(uint64_t codec_delay) { codec_delay_ = codec_delay; }
codec_delay()699*103e46e4SHarish Mahendrakar   uint64_t codec_delay() const { return codec_delay_; }
set_seek_pre_roll(uint64_t seek_pre_roll)700*103e46e4SHarish Mahendrakar   void set_seek_pre_roll(uint64_t seek_pre_roll) {
701*103e46e4SHarish Mahendrakar     seek_pre_roll_ = seek_pre_roll;
702*103e46e4SHarish Mahendrakar   }
seek_pre_roll()703*103e46e4SHarish Mahendrakar   uint64_t seek_pre_roll() const { return seek_pre_roll_; }
set_default_duration(uint64_t default_duration)704*103e46e4SHarish Mahendrakar   void set_default_duration(uint64_t default_duration) {
705*103e46e4SHarish Mahendrakar     default_duration_ = default_duration;
706*103e46e4SHarish Mahendrakar   }
default_duration()707*103e46e4SHarish Mahendrakar   uint64_t default_duration() const { return default_duration_; }
708*103e46e4SHarish Mahendrakar 
codec_private_length()709*103e46e4SHarish Mahendrakar   uint64_t codec_private_length() const { return codec_private_length_; }
content_encoding_entries_size()710*103e46e4SHarish Mahendrakar   uint32_t content_encoding_entries_size() const {
711*103e46e4SHarish Mahendrakar     return content_encoding_entries_size_;
712*103e46e4SHarish Mahendrakar   }
713*103e46e4SHarish Mahendrakar 
714*103e46e4SHarish Mahendrakar  private:
715*103e46e4SHarish Mahendrakar   // Track element names.
716*103e46e4SHarish Mahendrakar   char* codec_id_;
717*103e46e4SHarish Mahendrakar   uint8_t* codec_private_;
718*103e46e4SHarish Mahendrakar   char* language_;
719*103e46e4SHarish Mahendrakar   uint64_t max_block_additional_id_;
720*103e46e4SHarish Mahendrakar   char* name_;
721*103e46e4SHarish Mahendrakar   uint64_t number_;
722*103e46e4SHarish Mahendrakar   uint64_t type_;
723*103e46e4SHarish Mahendrakar   uint64_t uid_;
724*103e46e4SHarish Mahendrakar   uint64_t codec_delay_;
725*103e46e4SHarish Mahendrakar   uint64_t seek_pre_roll_;
726*103e46e4SHarish Mahendrakar   uint64_t default_duration_;
727*103e46e4SHarish Mahendrakar 
728*103e46e4SHarish Mahendrakar   // Size of the CodecPrivate data in bytes.
729*103e46e4SHarish Mahendrakar   uint64_t codec_private_length_;
730*103e46e4SHarish Mahendrakar 
731*103e46e4SHarish Mahendrakar   // ContentEncoding element list.
732*103e46e4SHarish Mahendrakar   ContentEncoding** content_encoding_entries_;
733*103e46e4SHarish Mahendrakar 
734*103e46e4SHarish Mahendrakar   // Number of ContentEncoding elements added.
735*103e46e4SHarish Mahendrakar   uint32_t content_encoding_entries_size_;
736*103e46e4SHarish Mahendrakar 
737*103e46e4SHarish Mahendrakar   LIBWEBM_DISALLOW_COPY_AND_ASSIGN(Track);
738*103e46e4SHarish Mahendrakar };
739*103e46e4SHarish Mahendrakar 
740*103e46e4SHarish Mahendrakar ///////////////////////////////////////////////////////////////
741*103e46e4SHarish Mahendrakar // Track that has video specific elements.
742*103e46e4SHarish Mahendrakar class VideoTrack : public Track {
743*103e46e4SHarish Mahendrakar  public:
744*103e46e4SHarish Mahendrakar   // Supported modes for stereo 3D.
745*103e46e4SHarish Mahendrakar   enum StereoMode {
746*103e46e4SHarish Mahendrakar     kMono = 0,
747*103e46e4SHarish Mahendrakar     kSideBySideLeftIsFirst = 1,
748*103e46e4SHarish Mahendrakar     kTopBottomRightIsFirst = 2,
749*103e46e4SHarish Mahendrakar     kTopBottomLeftIsFirst = 3,
750*103e46e4SHarish Mahendrakar     kSideBySideRightIsFirst = 11
751*103e46e4SHarish Mahendrakar   };
752*103e46e4SHarish Mahendrakar 
753*103e46e4SHarish Mahendrakar   enum AlphaMode { kNoAlpha = 0, kAlpha = 1 };
754*103e46e4SHarish Mahendrakar 
755*103e46e4SHarish Mahendrakar   // The |seed| parameter is used to synthesize a UID for the track.
756*103e46e4SHarish Mahendrakar   explicit VideoTrack(unsigned int* seed);
757*103e46e4SHarish Mahendrakar   virtual ~VideoTrack();
758*103e46e4SHarish Mahendrakar 
759*103e46e4SHarish Mahendrakar   // Returns the size in bytes for the payload of the Track element plus the
760*103e46e4SHarish Mahendrakar   // video specific elements.
761*103e46e4SHarish Mahendrakar   virtual uint64_t PayloadSize() const;
762*103e46e4SHarish Mahendrakar 
763*103e46e4SHarish Mahendrakar   // Output the VideoTrack element to the writer. Returns true on success.
764*103e46e4SHarish Mahendrakar   virtual bool Write(IMkvWriter* writer) const;
765*103e46e4SHarish Mahendrakar 
766*103e46e4SHarish Mahendrakar   // Sets the video's stereo mode. Returns true on success.
767*103e46e4SHarish Mahendrakar   bool SetStereoMode(uint64_t stereo_mode);
768*103e46e4SHarish Mahendrakar 
769*103e46e4SHarish Mahendrakar   // Sets the video's alpha mode. Returns true on success.
770*103e46e4SHarish Mahendrakar   bool SetAlphaMode(uint64_t alpha_mode);
771*103e46e4SHarish Mahendrakar 
set_display_height(uint64_t height)772*103e46e4SHarish Mahendrakar   void set_display_height(uint64_t height) { display_height_ = height; }
display_height()773*103e46e4SHarish Mahendrakar   uint64_t display_height() const { return display_height_; }
set_display_width(uint64_t width)774*103e46e4SHarish Mahendrakar   void set_display_width(uint64_t width) { display_width_ = width; }
display_width()775*103e46e4SHarish Mahendrakar   uint64_t display_width() const { return display_width_; }
set_pixel_height(uint64_t height)776*103e46e4SHarish Mahendrakar   void set_pixel_height(uint64_t height) { pixel_height_ = height; }
pixel_height()777*103e46e4SHarish Mahendrakar   uint64_t pixel_height() const { return pixel_height_; }
set_pixel_width(uint64_t width)778*103e46e4SHarish Mahendrakar   void set_pixel_width(uint64_t width) { pixel_width_ = width; }
pixel_width()779*103e46e4SHarish Mahendrakar   uint64_t pixel_width() const { return pixel_width_; }
780*103e46e4SHarish Mahendrakar 
set_crop_left(uint64_t crop_left)781*103e46e4SHarish Mahendrakar   void set_crop_left(uint64_t crop_left) { crop_left_ = crop_left; }
crop_left()782*103e46e4SHarish Mahendrakar   uint64_t crop_left() const { return crop_left_; }
set_crop_right(uint64_t crop_right)783*103e46e4SHarish Mahendrakar   void set_crop_right(uint64_t crop_right) { crop_right_ = crop_right; }
crop_right()784*103e46e4SHarish Mahendrakar   uint64_t crop_right() const { return crop_right_; }
set_crop_top(uint64_t crop_top)785*103e46e4SHarish Mahendrakar   void set_crop_top(uint64_t crop_top) { crop_top_ = crop_top; }
crop_top()786*103e46e4SHarish Mahendrakar   uint64_t crop_top() const { return crop_top_; }
set_crop_bottom(uint64_t crop_bottom)787*103e46e4SHarish Mahendrakar   void set_crop_bottom(uint64_t crop_bottom) { crop_bottom_ = crop_bottom; }
crop_bottom()788*103e46e4SHarish Mahendrakar   uint64_t crop_bottom() const { return crop_bottom_; }
789*103e46e4SHarish Mahendrakar 
set_frame_rate(double frame_rate)790*103e46e4SHarish Mahendrakar   void set_frame_rate(double frame_rate) { frame_rate_ = frame_rate; }
frame_rate()791*103e46e4SHarish Mahendrakar   double frame_rate() const { return frame_rate_; }
set_height(uint64_t height)792*103e46e4SHarish Mahendrakar   void set_height(uint64_t height) { height_ = height; }
height()793*103e46e4SHarish Mahendrakar   uint64_t height() const { return height_; }
stereo_mode()794*103e46e4SHarish Mahendrakar   uint64_t stereo_mode() { return stereo_mode_; }
alpha_mode()795*103e46e4SHarish Mahendrakar   uint64_t alpha_mode() { return alpha_mode_; }
set_width(uint64_t width)796*103e46e4SHarish Mahendrakar   void set_width(uint64_t width) { width_ = width; }
width()797*103e46e4SHarish Mahendrakar   uint64_t width() const { return width_; }
798*103e46e4SHarish Mahendrakar   void set_colour_space(const char* colour_space);
colour_space()799*103e46e4SHarish Mahendrakar   const char* colour_space() const { return colour_space_; }
800*103e46e4SHarish Mahendrakar 
colour()801*103e46e4SHarish Mahendrakar   Colour* colour() { return colour_; }
802*103e46e4SHarish Mahendrakar 
803*103e46e4SHarish Mahendrakar   // Deep copies |colour|.
804*103e46e4SHarish Mahendrakar   bool SetColour(const Colour& colour);
805*103e46e4SHarish Mahendrakar 
projection()806*103e46e4SHarish Mahendrakar   Projection* projection() { return projection_; }
807*103e46e4SHarish Mahendrakar 
808*103e46e4SHarish Mahendrakar   // Deep copies |projection|.
809*103e46e4SHarish Mahendrakar   bool SetProjection(const Projection& projection);
810*103e46e4SHarish Mahendrakar 
811*103e46e4SHarish Mahendrakar  private:
812*103e46e4SHarish Mahendrakar   // Returns the size in bytes of the Video element.
813*103e46e4SHarish Mahendrakar   uint64_t VideoPayloadSize() const;
814*103e46e4SHarish Mahendrakar 
815*103e46e4SHarish Mahendrakar   // Video track element names.
816*103e46e4SHarish Mahendrakar   uint64_t display_height_;
817*103e46e4SHarish Mahendrakar   uint64_t display_width_;
818*103e46e4SHarish Mahendrakar   uint64_t pixel_height_;
819*103e46e4SHarish Mahendrakar   uint64_t pixel_width_;
820*103e46e4SHarish Mahendrakar   uint64_t crop_left_;
821*103e46e4SHarish Mahendrakar   uint64_t crop_right_;
822*103e46e4SHarish Mahendrakar   uint64_t crop_top_;
823*103e46e4SHarish Mahendrakar   uint64_t crop_bottom_;
824*103e46e4SHarish Mahendrakar   double frame_rate_;
825*103e46e4SHarish Mahendrakar   uint64_t height_;
826*103e46e4SHarish Mahendrakar   uint64_t stereo_mode_;
827*103e46e4SHarish Mahendrakar   uint64_t alpha_mode_;
828*103e46e4SHarish Mahendrakar   uint64_t width_;
829*103e46e4SHarish Mahendrakar   char* colour_space_;
830*103e46e4SHarish Mahendrakar 
831*103e46e4SHarish Mahendrakar   Colour* colour_;
832*103e46e4SHarish Mahendrakar   Projection* projection_;
833*103e46e4SHarish Mahendrakar 
834*103e46e4SHarish Mahendrakar   LIBWEBM_DISALLOW_COPY_AND_ASSIGN(VideoTrack);
835*103e46e4SHarish Mahendrakar };
836*103e46e4SHarish Mahendrakar 
837*103e46e4SHarish Mahendrakar ///////////////////////////////////////////////////////////////
838*103e46e4SHarish Mahendrakar // Track that has audio specific elements.
839*103e46e4SHarish Mahendrakar class AudioTrack : public Track {
840*103e46e4SHarish Mahendrakar  public:
841*103e46e4SHarish Mahendrakar   // The |seed| parameter is used to synthesize a UID for the track.
842*103e46e4SHarish Mahendrakar   explicit AudioTrack(unsigned int* seed);
843*103e46e4SHarish Mahendrakar   virtual ~AudioTrack();
844*103e46e4SHarish Mahendrakar 
845*103e46e4SHarish Mahendrakar   // Returns the size in bytes for the payload of the Track element plus the
846*103e46e4SHarish Mahendrakar   // audio specific elements.
847*103e46e4SHarish Mahendrakar   virtual uint64_t PayloadSize() const;
848*103e46e4SHarish Mahendrakar 
849*103e46e4SHarish Mahendrakar   // Output the AudioTrack element to the writer. Returns true on success.
850*103e46e4SHarish Mahendrakar   virtual bool Write(IMkvWriter* writer) const;
851*103e46e4SHarish Mahendrakar 
set_bit_depth(uint64_t bit_depth)852*103e46e4SHarish Mahendrakar   void set_bit_depth(uint64_t bit_depth) { bit_depth_ = bit_depth; }
bit_depth()853*103e46e4SHarish Mahendrakar   uint64_t bit_depth() const { return bit_depth_; }
set_channels(uint64_t channels)854*103e46e4SHarish Mahendrakar   void set_channels(uint64_t channels) { channels_ = channels; }
channels()855*103e46e4SHarish Mahendrakar   uint64_t channels() const { return channels_; }
set_sample_rate(double sample_rate)856*103e46e4SHarish Mahendrakar   void set_sample_rate(double sample_rate) { sample_rate_ = sample_rate; }
sample_rate()857*103e46e4SHarish Mahendrakar   double sample_rate() const { return sample_rate_; }
858*103e46e4SHarish Mahendrakar 
859*103e46e4SHarish Mahendrakar  private:
860*103e46e4SHarish Mahendrakar   // Audio track element names.
861*103e46e4SHarish Mahendrakar   uint64_t bit_depth_;
862*103e46e4SHarish Mahendrakar   uint64_t channels_;
863*103e46e4SHarish Mahendrakar   double sample_rate_;
864*103e46e4SHarish Mahendrakar 
865*103e46e4SHarish Mahendrakar   LIBWEBM_DISALLOW_COPY_AND_ASSIGN(AudioTrack);
866*103e46e4SHarish Mahendrakar };
867*103e46e4SHarish Mahendrakar 
868*103e46e4SHarish Mahendrakar ///////////////////////////////////////////////////////////////
869*103e46e4SHarish Mahendrakar // Tracks element
870*103e46e4SHarish Mahendrakar class Tracks {
871*103e46e4SHarish Mahendrakar  public:
872*103e46e4SHarish Mahendrakar   // Audio and video type defined by the Matroska specs.
873*103e46e4SHarish Mahendrakar   enum { kVideo = 0x1, kAudio = 0x2 };
874*103e46e4SHarish Mahendrakar 
875*103e46e4SHarish Mahendrakar   static const char kOpusCodecId[];
876*103e46e4SHarish Mahendrakar   static const char kVorbisCodecId[];
877*103e46e4SHarish Mahendrakar   static const char kAv1CodecId[];
878*103e46e4SHarish Mahendrakar   static const char kVp8CodecId[];
879*103e46e4SHarish Mahendrakar   static const char kVp9CodecId[];
880*103e46e4SHarish Mahendrakar   static const char kWebVttCaptionsId[];
881*103e46e4SHarish Mahendrakar   static const char kWebVttDescriptionsId[];
882*103e46e4SHarish Mahendrakar   static const char kWebVttMetadataId[];
883*103e46e4SHarish Mahendrakar   static const char kWebVttSubtitlesId[];
884*103e46e4SHarish Mahendrakar 
885*103e46e4SHarish Mahendrakar   Tracks();
886*103e46e4SHarish Mahendrakar   ~Tracks();
887*103e46e4SHarish Mahendrakar 
888*103e46e4SHarish Mahendrakar   // Adds a Track element to the Tracks object. |track| will be owned and
889*103e46e4SHarish Mahendrakar   // deleted by the Tracks object. Returns true on success. |number| is the
890*103e46e4SHarish Mahendrakar   // number to use for the track. |number| must be >= 0. If |number| == 0
891*103e46e4SHarish Mahendrakar   // then the muxer will decide on the track number.
892*103e46e4SHarish Mahendrakar   bool AddTrack(Track* track, int32_t number);
893*103e46e4SHarish Mahendrakar 
894*103e46e4SHarish Mahendrakar   // Returns the track by index. Returns NULL if there is no track match.
895*103e46e4SHarish Mahendrakar   const Track* GetTrackByIndex(uint32_t idx) const;
896*103e46e4SHarish Mahendrakar 
897*103e46e4SHarish Mahendrakar   // Search the Tracks and return the track that matches |tn|. Returns NULL
898*103e46e4SHarish Mahendrakar   // if there is no track match.
899*103e46e4SHarish Mahendrakar   Track* GetTrackByNumber(uint64_t track_number) const;
900*103e46e4SHarish Mahendrakar 
901*103e46e4SHarish Mahendrakar   // Returns true if the track number is an audio track.
902*103e46e4SHarish Mahendrakar   bool TrackIsAudio(uint64_t track_number) const;
903*103e46e4SHarish Mahendrakar 
904*103e46e4SHarish Mahendrakar   // Returns true if the track number is a video track.
905*103e46e4SHarish Mahendrakar   bool TrackIsVideo(uint64_t track_number) const;
906*103e46e4SHarish Mahendrakar 
907*103e46e4SHarish Mahendrakar   // Output the Tracks element to the writer. Returns true on success.
908*103e46e4SHarish Mahendrakar   bool Write(IMkvWriter* writer) const;
909*103e46e4SHarish Mahendrakar 
track_entries_size()910*103e46e4SHarish Mahendrakar   uint32_t track_entries_size() const { return track_entries_size_; }
911*103e46e4SHarish Mahendrakar 
912*103e46e4SHarish Mahendrakar  private:
913*103e46e4SHarish Mahendrakar   // Track element list.
914*103e46e4SHarish Mahendrakar   Track** track_entries_;
915*103e46e4SHarish Mahendrakar 
916*103e46e4SHarish Mahendrakar   // Number of Track elements added.
917*103e46e4SHarish Mahendrakar   uint32_t track_entries_size_;
918*103e46e4SHarish Mahendrakar 
919*103e46e4SHarish Mahendrakar   // Whether or not Tracks element has already been written via IMkvWriter.
920*103e46e4SHarish Mahendrakar   mutable bool wrote_tracks_;
921*103e46e4SHarish Mahendrakar 
922*103e46e4SHarish Mahendrakar   LIBWEBM_DISALLOW_COPY_AND_ASSIGN(Tracks);
923*103e46e4SHarish Mahendrakar };
924*103e46e4SHarish Mahendrakar 
925*103e46e4SHarish Mahendrakar ///////////////////////////////////////////////////////////////
926*103e46e4SHarish Mahendrakar // Chapter element
927*103e46e4SHarish Mahendrakar //
928*103e46e4SHarish Mahendrakar class Chapter {
929*103e46e4SHarish Mahendrakar  public:
930*103e46e4SHarish Mahendrakar   // Set the identifier for this chapter.  (This corresponds to the
931*103e46e4SHarish Mahendrakar   // Cue Identifier line in WebVTT.)
932*103e46e4SHarish Mahendrakar   // TODO(matthewjheaney): the actual serialization of this item in
933*103e46e4SHarish Mahendrakar   // MKV is pending.
934*103e46e4SHarish Mahendrakar   bool set_id(const char* id);
935*103e46e4SHarish Mahendrakar 
936*103e46e4SHarish Mahendrakar   // Converts the nanosecond start and stop times of this chapter to
937*103e46e4SHarish Mahendrakar   // their corresponding timecode values, and stores them that way.
938*103e46e4SHarish Mahendrakar   void set_time(const Segment& segment, uint64_t start_time_ns,
939*103e46e4SHarish Mahendrakar                 uint64_t end_time_ns);
940*103e46e4SHarish Mahendrakar 
941*103e46e4SHarish Mahendrakar   // Sets the uid for this chapter. Primarily used to enable
942*103e46e4SHarish Mahendrakar   // deterministic output from the muxer.
set_uid(const uint64_t uid)943*103e46e4SHarish Mahendrakar   void set_uid(const uint64_t uid) { uid_ = uid; }
944*103e46e4SHarish Mahendrakar 
945*103e46e4SHarish Mahendrakar   // Add a title string to this chapter, per the semantics described
946*103e46e4SHarish Mahendrakar   // here:
947*103e46e4SHarish Mahendrakar   //  http://www.matroska.org/technical/specs/index.html
948*103e46e4SHarish Mahendrakar   //
949*103e46e4SHarish Mahendrakar   // The title ("chapter string") is a UTF-8 string.
950*103e46e4SHarish Mahendrakar   //
951*103e46e4SHarish Mahendrakar   // The language has ISO 639-2 representation, described here:
952*103e46e4SHarish Mahendrakar   //  http://www.loc.gov/standards/iso639-2/englangn.html
953*103e46e4SHarish Mahendrakar   //  http://www.loc.gov/standards/iso639-2/php/English_list.php
954*103e46e4SHarish Mahendrakar   // If you specify NULL as the language value, this implies
955*103e46e4SHarish Mahendrakar   // English ("eng").
956*103e46e4SHarish Mahendrakar   //
957*103e46e4SHarish Mahendrakar   // The country value corresponds to the codes listed here:
958*103e46e4SHarish Mahendrakar   //  http://www.iana.org/domains/root/db/
959*103e46e4SHarish Mahendrakar   //
960*103e46e4SHarish Mahendrakar   // The function returns false if the string could not be allocated.
961*103e46e4SHarish Mahendrakar   bool add_string(const char* title, const char* language, const char* country);
962*103e46e4SHarish Mahendrakar 
963*103e46e4SHarish Mahendrakar  private:
964*103e46e4SHarish Mahendrakar   friend class Chapters;
965*103e46e4SHarish Mahendrakar 
966*103e46e4SHarish Mahendrakar   // For storage of chapter titles that differ by language.
967*103e46e4SHarish Mahendrakar   class Display {
968*103e46e4SHarish Mahendrakar    public:
969*103e46e4SHarish Mahendrakar     // Establish representation invariant for new Display object.
970*103e46e4SHarish Mahendrakar     void Init();
971*103e46e4SHarish Mahendrakar 
972*103e46e4SHarish Mahendrakar     // Reclaim resources, in anticipation of destruction.
973*103e46e4SHarish Mahendrakar     void Clear();
974*103e46e4SHarish Mahendrakar 
975*103e46e4SHarish Mahendrakar     // Copies the title to the |title_| member.  Returns false on
976*103e46e4SHarish Mahendrakar     // error.
977*103e46e4SHarish Mahendrakar     bool set_title(const char* title);
978*103e46e4SHarish Mahendrakar 
979*103e46e4SHarish Mahendrakar     // Copies the language to the |language_| member.  Returns false
980*103e46e4SHarish Mahendrakar     // on error.
981*103e46e4SHarish Mahendrakar     bool set_language(const char* language);
982*103e46e4SHarish Mahendrakar 
983*103e46e4SHarish Mahendrakar     // Copies the country to the |country_| member.  Returns false on
984*103e46e4SHarish Mahendrakar     // error.
985*103e46e4SHarish Mahendrakar     bool set_country(const char* country);
986*103e46e4SHarish Mahendrakar 
987*103e46e4SHarish Mahendrakar     // If |writer| is non-NULL, serialize the Display sub-element of
988*103e46e4SHarish Mahendrakar     // the Atom into the stream.  Returns the Display element size on
989*103e46e4SHarish Mahendrakar     // success, 0 if error.
990*103e46e4SHarish Mahendrakar     uint64_t WriteDisplay(IMkvWriter* writer) const;
991*103e46e4SHarish Mahendrakar 
992*103e46e4SHarish Mahendrakar    private:
993*103e46e4SHarish Mahendrakar     char* title_;
994*103e46e4SHarish Mahendrakar     char* language_;
995*103e46e4SHarish Mahendrakar     char* country_;
996*103e46e4SHarish Mahendrakar   };
997*103e46e4SHarish Mahendrakar 
998*103e46e4SHarish Mahendrakar   Chapter();
999*103e46e4SHarish Mahendrakar   ~Chapter();
1000*103e46e4SHarish Mahendrakar 
1001*103e46e4SHarish Mahendrakar   // Establish the representation invariant for a newly-created
1002*103e46e4SHarish Mahendrakar   // Chapter object.  The |seed| parameter is used to create the UID
1003*103e46e4SHarish Mahendrakar   // for this chapter atom.
1004*103e46e4SHarish Mahendrakar   void Init(unsigned int* seed);
1005*103e46e4SHarish Mahendrakar 
1006*103e46e4SHarish Mahendrakar   // Copies this Chapter object to a different one.  This is used when
1007*103e46e4SHarish Mahendrakar   // expanding a plain array of Chapter objects (see Chapters).
1008*103e46e4SHarish Mahendrakar   void ShallowCopy(Chapter* dst) const;
1009*103e46e4SHarish Mahendrakar 
1010*103e46e4SHarish Mahendrakar   // Reclaim resources used by this Chapter object, pending its
1011*103e46e4SHarish Mahendrakar   // destruction.
1012*103e46e4SHarish Mahendrakar   void Clear();
1013*103e46e4SHarish Mahendrakar 
1014*103e46e4SHarish Mahendrakar   // If there is no storage remaining on the |displays_| array for a
1015*103e46e4SHarish Mahendrakar   // new display object, creates a new, longer array and copies the
1016*103e46e4SHarish Mahendrakar   // existing Display objects to the new array.  Returns false if the
1017*103e46e4SHarish Mahendrakar   // array cannot be expanded.
1018*103e46e4SHarish Mahendrakar   bool ExpandDisplaysArray();
1019*103e46e4SHarish Mahendrakar 
1020*103e46e4SHarish Mahendrakar   // If |writer| is non-NULL, serialize the Atom sub-element into the
1021*103e46e4SHarish Mahendrakar   // stream.  Returns the total size of the element on success, 0 if
1022*103e46e4SHarish Mahendrakar   // error.
1023*103e46e4SHarish Mahendrakar   uint64_t WriteAtom(IMkvWriter* writer) const;
1024*103e46e4SHarish Mahendrakar 
1025*103e46e4SHarish Mahendrakar   // The string identifier for this chapter (corresponds to WebVTT cue
1026*103e46e4SHarish Mahendrakar   // identifier).
1027*103e46e4SHarish Mahendrakar   char* id_;
1028*103e46e4SHarish Mahendrakar 
1029*103e46e4SHarish Mahendrakar   // Start timecode of the chapter.
1030*103e46e4SHarish Mahendrakar   uint64_t start_timecode_;
1031*103e46e4SHarish Mahendrakar 
1032*103e46e4SHarish Mahendrakar   // Stop timecode of the chapter.
1033*103e46e4SHarish Mahendrakar   uint64_t end_timecode_;
1034*103e46e4SHarish Mahendrakar 
1035*103e46e4SHarish Mahendrakar   // The binary identifier for this chapter.
1036*103e46e4SHarish Mahendrakar   uint64_t uid_;
1037*103e46e4SHarish Mahendrakar 
1038*103e46e4SHarish Mahendrakar   // The Atom element can contain multiple Display sub-elements, as
1039*103e46e4SHarish Mahendrakar   // the same logical title can be rendered in different languages.
1040*103e46e4SHarish Mahendrakar   Display* displays_;
1041*103e46e4SHarish Mahendrakar 
1042*103e46e4SHarish Mahendrakar   // The physical length (total size) of the |displays_| array.
1043*103e46e4SHarish Mahendrakar   int displays_size_;
1044*103e46e4SHarish Mahendrakar 
1045*103e46e4SHarish Mahendrakar   // The logical length (number of active elements) on the |displays_|
1046*103e46e4SHarish Mahendrakar   // array.
1047*103e46e4SHarish Mahendrakar   int displays_count_;
1048*103e46e4SHarish Mahendrakar 
1049*103e46e4SHarish Mahendrakar   LIBWEBM_DISALLOW_COPY_AND_ASSIGN(Chapter);
1050*103e46e4SHarish Mahendrakar };
1051*103e46e4SHarish Mahendrakar 
1052*103e46e4SHarish Mahendrakar ///////////////////////////////////////////////////////////////
1053*103e46e4SHarish Mahendrakar // Chapters element
1054*103e46e4SHarish Mahendrakar //
1055*103e46e4SHarish Mahendrakar class Chapters {
1056*103e46e4SHarish Mahendrakar  public:
1057*103e46e4SHarish Mahendrakar   Chapters();
1058*103e46e4SHarish Mahendrakar   ~Chapters();
1059*103e46e4SHarish Mahendrakar 
1060*103e46e4SHarish Mahendrakar   Chapter* AddChapter(unsigned int* seed);
1061*103e46e4SHarish Mahendrakar 
1062*103e46e4SHarish Mahendrakar   // Returns the number of chapters that have been added.
1063*103e46e4SHarish Mahendrakar   int Count() const;
1064*103e46e4SHarish Mahendrakar 
1065*103e46e4SHarish Mahendrakar   // Output the Chapters element to the writer. Returns true on success.
1066*103e46e4SHarish Mahendrakar   bool Write(IMkvWriter* writer) const;
1067*103e46e4SHarish Mahendrakar 
1068*103e46e4SHarish Mahendrakar  private:
1069*103e46e4SHarish Mahendrakar   // Expands the chapters_ array if there is not enough space to contain
1070*103e46e4SHarish Mahendrakar   // another chapter object.  Returns true on success.
1071*103e46e4SHarish Mahendrakar   bool ExpandChaptersArray();
1072*103e46e4SHarish Mahendrakar 
1073*103e46e4SHarish Mahendrakar   // If |writer| is non-NULL, serialize the Edition sub-element of the
1074*103e46e4SHarish Mahendrakar   // Chapters element into the stream.  Returns the Edition element
1075*103e46e4SHarish Mahendrakar   // size on success, 0 if error.
1076*103e46e4SHarish Mahendrakar   uint64_t WriteEdition(IMkvWriter* writer) const;
1077*103e46e4SHarish Mahendrakar 
1078*103e46e4SHarish Mahendrakar   // Total length of the chapters_ array.
1079*103e46e4SHarish Mahendrakar   int chapters_size_;
1080*103e46e4SHarish Mahendrakar 
1081*103e46e4SHarish Mahendrakar   // Number of active chapters on the chapters_ array.
1082*103e46e4SHarish Mahendrakar   int chapters_count_;
1083*103e46e4SHarish Mahendrakar 
1084*103e46e4SHarish Mahendrakar   // Array for storage of chapter objects.
1085*103e46e4SHarish Mahendrakar   Chapter* chapters_;
1086*103e46e4SHarish Mahendrakar 
1087*103e46e4SHarish Mahendrakar   LIBWEBM_DISALLOW_COPY_AND_ASSIGN(Chapters);
1088*103e46e4SHarish Mahendrakar };
1089*103e46e4SHarish Mahendrakar 
1090*103e46e4SHarish Mahendrakar ///////////////////////////////////////////////////////////////
1091*103e46e4SHarish Mahendrakar // Tag element
1092*103e46e4SHarish Mahendrakar //
1093*103e46e4SHarish Mahendrakar class Tag {
1094*103e46e4SHarish Mahendrakar  public:
1095*103e46e4SHarish Mahendrakar   bool add_simple_tag(const char* tag_name, const char* tag_string);
1096*103e46e4SHarish Mahendrakar 
1097*103e46e4SHarish Mahendrakar  private:
1098*103e46e4SHarish Mahendrakar   // Tags calls Clear and the destructor of Tag
1099*103e46e4SHarish Mahendrakar   friend class Tags;
1100*103e46e4SHarish Mahendrakar 
1101*103e46e4SHarish Mahendrakar   // For storage of simple tags
1102*103e46e4SHarish Mahendrakar   class SimpleTag {
1103*103e46e4SHarish Mahendrakar    public:
1104*103e46e4SHarish Mahendrakar     // Establish representation invariant for new SimpleTag object.
1105*103e46e4SHarish Mahendrakar     void Init();
1106*103e46e4SHarish Mahendrakar 
1107*103e46e4SHarish Mahendrakar     // Reclaim resources, in anticipation of destruction.
1108*103e46e4SHarish Mahendrakar     void Clear();
1109*103e46e4SHarish Mahendrakar 
1110*103e46e4SHarish Mahendrakar     // Copies the title to the |tag_name_| member.  Returns false on
1111*103e46e4SHarish Mahendrakar     // error.
1112*103e46e4SHarish Mahendrakar     bool set_tag_name(const char* tag_name);
1113*103e46e4SHarish Mahendrakar 
1114*103e46e4SHarish Mahendrakar     // Copies the language to the |tag_string_| member.  Returns false
1115*103e46e4SHarish Mahendrakar     // on error.
1116*103e46e4SHarish Mahendrakar     bool set_tag_string(const char* tag_string);
1117*103e46e4SHarish Mahendrakar 
1118*103e46e4SHarish Mahendrakar     // If |writer| is non-NULL, serialize the SimpleTag sub-element of
1119*103e46e4SHarish Mahendrakar     // the Atom into the stream.  Returns the SimpleTag element size on
1120*103e46e4SHarish Mahendrakar     // success, 0 if error.
1121*103e46e4SHarish Mahendrakar     uint64_t Write(IMkvWriter* writer) const;
1122*103e46e4SHarish Mahendrakar 
1123*103e46e4SHarish Mahendrakar    private:
1124*103e46e4SHarish Mahendrakar     char* tag_name_;
1125*103e46e4SHarish Mahendrakar     char* tag_string_;
1126*103e46e4SHarish Mahendrakar   };
1127*103e46e4SHarish Mahendrakar 
1128*103e46e4SHarish Mahendrakar   Tag();
1129*103e46e4SHarish Mahendrakar   ~Tag();
1130*103e46e4SHarish Mahendrakar 
1131*103e46e4SHarish Mahendrakar   // Copies this Tag object to a different one.  This is used when
1132*103e46e4SHarish Mahendrakar   // expanding a plain array of Tag objects (see Tags).
1133*103e46e4SHarish Mahendrakar   void ShallowCopy(Tag* dst) const;
1134*103e46e4SHarish Mahendrakar 
1135*103e46e4SHarish Mahendrakar   // Reclaim resources used by this Tag object, pending its
1136*103e46e4SHarish Mahendrakar   // destruction.
1137*103e46e4SHarish Mahendrakar   void Clear();
1138*103e46e4SHarish Mahendrakar 
1139*103e46e4SHarish Mahendrakar   // If there is no storage remaining on the |simple_tags_| array for a
1140*103e46e4SHarish Mahendrakar   // new display object, creates a new, longer array and copies the
1141*103e46e4SHarish Mahendrakar   // existing SimpleTag objects to the new array.  Returns false if the
1142*103e46e4SHarish Mahendrakar   // array cannot be expanded.
1143*103e46e4SHarish Mahendrakar   bool ExpandSimpleTagsArray();
1144*103e46e4SHarish Mahendrakar 
1145*103e46e4SHarish Mahendrakar   // If |writer| is non-NULL, serialize the Tag sub-element into the
1146*103e46e4SHarish Mahendrakar   // stream.  Returns the total size of the element on success, 0 if
1147*103e46e4SHarish Mahendrakar   // error.
1148*103e46e4SHarish Mahendrakar   uint64_t Write(IMkvWriter* writer) const;
1149*103e46e4SHarish Mahendrakar 
1150*103e46e4SHarish Mahendrakar   // The Atom element can contain multiple SimpleTag sub-elements
1151*103e46e4SHarish Mahendrakar   SimpleTag* simple_tags_;
1152*103e46e4SHarish Mahendrakar 
1153*103e46e4SHarish Mahendrakar   // The physical length (total size) of the |simple_tags_| array.
1154*103e46e4SHarish Mahendrakar   int simple_tags_size_;
1155*103e46e4SHarish Mahendrakar 
1156*103e46e4SHarish Mahendrakar   // The logical length (number of active elements) on the |simple_tags_|
1157*103e46e4SHarish Mahendrakar   // array.
1158*103e46e4SHarish Mahendrakar   int simple_tags_count_;
1159*103e46e4SHarish Mahendrakar 
1160*103e46e4SHarish Mahendrakar   LIBWEBM_DISALLOW_COPY_AND_ASSIGN(Tag);
1161*103e46e4SHarish Mahendrakar };
1162*103e46e4SHarish Mahendrakar 
1163*103e46e4SHarish Mahendrakar ///////////////////////////////////////////////////////////////
1164*103e46e4SHarish Mahendrakar // Tags element
1165*103e46e4SHarish Mahendrakar //
1166*103e46e4SHarish Mahendrakar class Tags {
1167*103e46e4SHarish Mahendrakar  public:
1168*103e46e4SHarish Mahendrakar   Tags();
1169*103e46e4SHarish Mahendrakar   ~Tags();
1170*103e46e4SHarish Mahendrakar 
1171*103e46e4SHarish Mahendrakar   Tag* AddTag();
1172*103e46e4SHarish Mahendrakar 
1173*103e46e4SHarish Mahendrakar   // Returns the number of tags that have been added.
1174*103e46e4SHarish Mahendrakar   int Count() const;
1175*103e46e4SHarish Mahendrakar 
1176*103e46e4SHarish Mahendrakar   // Output the Tags element to the writer. Returns true on success.
1177*103e46e4SHarish Mahendrakar   bool Write(IMkvWriter* writer) const;
1178*103e46e4SHarish Mahendrakar 
1179*103e46e4SHarish Mahendrakar  private:
1180*103e46e4SHarish Mahendrakar   // Expands the tags_ array if there is not enough space to contain
1181*103e46e4SHarish Mahendrakar   // another tag object.  Returns true on success.
1182*103e46e4SHarish Mahendrakar   bool ExpandTagsArray();
1183*103e46e4SHarish Mahendrakar 
1184*103e46e4SHarish Mahendrakar   // Total length of the tags_ array.
1185*103e46e4SHarish Mahendrakar   int tags_size_;
1186*103e46e4SHarish Mahendrakar 
1187*103e46e4SHarish Mahendrakar   // Number of active tags on the tags_ array.
1188*103e46e4SHarish Mahendrakar   int tags_count_;
1189*103e46e4SHarish Mahendrakar 
1190*103e46e4SHarish Mahendrakar   // Array for storage of tag objects.
1191*103e46e4SHarish Mahendrakar   Tag* tags_;
1192*103e46e4SHarish Mahendrakar 
1193*103e46e4SHarish Mahendrakar   LIBWEBM_DISALLOW_COPY_AND_ASSIGN(Tags);
1194*103e46e4SHarish Mahendrakar };
1195*103e46e4SHarish Mahendrakar 
1196*103e46e4SHarish Mahendrakar ///////////////////////////////////////////////////////////////
1197*103e46e4SHarish Mahendrakar // Cluster element
1198*103e46e4SHarish Mahendrakar //
1199*103e46e4SHarish Mahendrakar // Notes:
1200*103e46e4SHarish Mahendrakar //  |Init| must be called before any other method in this class.
1201*103e46e4SHarish Mahendrakar class Cluster {
1202*103e46e4SHarish Mahendrakar  public:
1203*103e46e4SHarish Mahendrakar   // |timecode| is the absolute timecode of the cluster. |cues_pos| is the
1204*103e46e4SHarish Mahendrakar   // position for the cluster within the segment that should be written in
1205*103e46e4SHarish Mahendrakar   // the cues element. |timecode_scale| is the timecode scale of the segment.
1206*103e46e4SHarish Mahendrakar   Cluster(uint64_t timecode, int64_t cues_pos, uint64_t timecode_scale,
1207*103e46e4SHarish Mahendrakar           bool write_last_frame_with_duration = false,
1208*103e46e4SHarish Mahendrakar           bool fixed_size_timecode = false);
1209*103e46e4SHarish Mahendrakar   ~Cluster();
1210*103e46e4SHarish Mahendrakar 
1211*103e46e4SHarish Mahendrakar   bool Init(IMkvWriter* ptr_writer);
1212*103e46e4SHarish Mahendrakar 
1213*103e46e4SHarish Mahendrakar   // Adds a frame to be output in the file. The frame is written out through
1214*103e46e4SHarish Mahendrakar   // |writer_| if successful. Returns true on success.
1215*103e46e4SHarish Mahendrakar   bool AddFrame(const Frame* frame);
1216*103e46e4SHarish Mahendrakar 
1217*103e46e4SHarish Mahendrakar   // Adds a frame to be output in the file. The frame is written out through
1218*103e46e4SHarish Mahendrakar   // |writer_| if successful. Returns true on success.
1219*103e46e4SHarish Mahendrakar   // Inputs:
1220*103e46e4SHarish Mahendrakar   //   data: Pointer to the data
1221*103e46e4SHarish Mahendrakar   //   length: Length of the data
1222*103e46e4SHarish Mahendrakar   //   track_number: Track to add the data to. Value returned by Add track
1223*103e46e4SHarish Mahendrakar   //                 functions.  The range of allowed values is [1, 126].
1224*103e46e4SHarish Mahendrakar   //   timecode:     Absolute (not relative to cluster) timestamp of the
1225*103e46e4SHarish Mahendrakar   //                 frame, expressed in timecode units.
1226*103e46e4SHarish Mahendrakar   //   is_key:       Flag telling whether or not this frame is a key frame.
1227*103e46e4SHarish Mahendrakar   bool AddFrame(const uint8_t* data, uint64_t length, uint64_t track_number,
1228*103e46e4SHarish Mahendrakar                 uint64_t timecode,  // timecode units (absolute)
1229*103e46e4SHarish Mahendrakar                 bool is_key);
1230*103e46e4SHarish Mahendrakar 
1231*103e46e4SHarish Mahendrakar   // Adds a frame to be output in the file. The frame is written out through
1232*103e46e4SHarish Mahendrakar   // |writer_| if successful. Returns true on success.
1233*103e46e4SHarish Mahendrakar   // Inputs:
1234*103e46e4SHarish Mahendrakar   //   data: Pointer to the data
1235*103e46e4SHarish Mahendrakar   //   length: Length of the data
1236*103e46e4SHarish Mahendrakar   //   additional: Pointer to the additional data
1237*103e46e4SHarish Mahendrakar   //   additional_length: Length of the additional data
1238*103e46e4SHarish Mahendrakar   //   add_id: Value of BlockAddID element
1239*103e46e4SHarish Mahendrakar   //   track_number: Track to add the data to. Value returned by Add track
1240*103e46e4SHarish Mahendrakar   //                 functions.  The range of allowed values is [1, 126].
1241*103e46e4SHarish Mahendrakar   //   abs_timecode: Absolute (not relative to cluster) timestamp of the
1242*103e46e4SHarish Mahendrakar   //                 frame, expressed in timecode units.
1243*103e46e4SHarish Mahendrakar   //   is_key:       Flag telling whether or not this frame is a key frame.
1244*103e46e4SHarish Mahendrakar   bool AddFrameWithAdditional(const uint8_t* data, uint64_t length,
1245*103e46e4SHarish Mahendrakar                               const uint8_t* additional,
1246*103e46e4SHarish Mahendrakar                               uint64_t additional_length, uint64_t add_id,
1247*103e46e4SHarish Mahendrakar                               uint64_t track_number, uint64_t abs_timecode,
1248*103e46e4SHarish Mahendrakar                               bool is_key);
1249*103e46e4SHarish Mahendrakar 
1250*103e46e4SHarish Mahendrakar   // Adds a frame to be output in the file. The frame is written out through
1251*103e46e4SHarish Mahendrakar   // |writer_| if successful. Returns true on success.
1252*103e46e4SHarish Mahendrakar   // Inputs:
1253*103e46e4SHarish Mahendrakar   //   data: Pointer to the data.
1254*103e46e4SHarish Mahendrakar   //   length: Length of the data.
1255*103e46e4SHarish Mahendrakar   //   discard_padding: DiscardPadding element value.
1256*103e46e4SHarish Mahendrakar   //   track_number: Track to add the data to. Value returned by Add track
1257*103e46e4SHarish Mahendrakar   //                 functions.  The range of allowed values is [1, 126].
1258*103e46e4SHarish Mahendrakar   //   abs_timecode: Absolute (not relative to cluster) timestamp of the
1259*103e46e4SHarish Mahendrakar   //                 frame, expressed in timecode units.
1260*103e46e4SHarish Mahendrakar   //   is_key:       Flag telling whether or not this frame is a key frame.
1261*103e46e4SHarish Mahendrakar   bool AddFrameWithDiscardPadding(const uint8_t* data, uint64_t length,
1262*103e46e4SHarish Mahendrakar                                   int64_t discard_padding,
1263*103e46e4SHarish Mahendrakar                                   uint64_t track_number, uint64_t abs_timecode,
1264*103e46e4SHarish Mahendrakar                                   bool is_key);
1265*103e46e4SHarish Mahendrakar 
1266*103e46e4SHarish Mahendrakar   // Writes a frame of metadata to the output medium; returns true on
1267*103e46e4SHarish Mahendrakar   // success.
1268*103e46e4SHarish Mahendrakar   // Inputs:
1269*103e46e4SHarish Mahendrakar   //   data: Pointer to the data
1270*103e46e4SHarish Mahendrakar   //   length: Length of the data
1271*103e46e4SHarish Mahendrakar   //   track_number: Track to add the data to. Value returned by Add track
1272*103e46e4SHarish Mahendrakar   //                 functions.  The range of allowed values is [1, 126].
1273*103e46e4SHarish Mahendrakar   //   timecode:     Absolute (not relative to cluster) timestamp of the
1274*103e46e4SHarish Mahendrakar   //                 metadata frame, expressed in timecode units.
1275*103e46e4SHarish Mahendrakar   //   duration:     Duration of metadata frame, in timecode units.
1276*103e46e4SHarish Mahendrakar   //
1277*103e46e4SHarish Mahendrakar   // The metadata frame is written as a block group, with a duration
1278*103e46e4SHarish Mahendrakar   // sub-element but no reference time sub-elements (indicating that
1279*103e46e4SHarish Mahendrakar   // it is considered a keyframe, per Matroska semantics).
1280*103e46e4SHarish Mahendrakar   bool AddMetadata(const uint8_t* data, uint64_t length, uint64_t track_number,
1281*103e46e4SHarish Mahendrakar                    uint64_t timecode, uint64_t duration);
1282*103e46e4SHarish Mahendrakar 
1283*103e46e4SHarish Mahendrakar   // Increments the size of the cluster's data in bytes.
1284*103e46e4SHarish Mahendrakar   void AddPayloadSize(uint64_t size);
1285*103e46e4SHarish Mahendrakar 
1286*103e46e4SHarish Mahendrakar   // Closes the cluster so no more data can be written to it. Will update the
1287*103e46e4SHarish Mahendrakar   // cluster's size if |writer_| is seekable. Returns true on success. This
1288*103e46e4SHarish Mahendrakar   // variant of Finalize() fails when |write_last_frame_with_duration_| is set
1289*103e46e4SHarish Mahendrakar   // to true.
1290*103e46e4SHarish Mahendrakar   bool Finalize();
1291*103e46e4SHarish Mahendrakar 
1292*103e46e4SHarish Mahendrakar   // Closes the cluster so no more data can be written to it. Will update the
1293*103e46e4SHarish Mahendrakar   // cluster's size if |writer_| is seekable. Returns true on success.
1294*103e46e4SHarish Mahendrakar   // Inputs:
1295*103e46e4SHarish Mahendrakar   //   set_last_frame_duration: Boolean indicating whether or not the duration
1296*103e46e4SHarish Mahendrakar   //                            of the last frame should be set. If set to
1297*103e46e4SHarish Mahendrakar   //                            false, the |duration| value is ignored and
1298*103e46e4SHarish Mahendrakar   //                            |write_last_frame_with_duration_| will not be
1299*103e46e4SHarish Mahendrakar   //                            honored.
1300*103e46e4SHarish Mahendrakar   //   duration: Duration of the Cluster in timecode scale.
1301*103e46e4SHarish Mahendrakar   bool Finalize(bool set_last_frame_duration, uint64_t duration);
1302*103e46e4SHarish Mahendrakar 
1303*103e46e4SHarish Mahendrakar   // Returns the size in bytes for the entire Cluster element.
1304*103e46e4SHarish Mahendrakar   uint64_t Size() const;
1305*103e46e4SHarish Mahendrakar 
1306*103e46e4SHarish Mahendrakar   // Given |abs_timecode|, calculates timecode relative to most recent timecode.
1307*103e46e4SHarish Mahendrakar   // Returns -1 on failure, or a relative timecode.
1308*103e46e4SHarish Mahendrakar   int64_t GetRelativeTimecode(int64_t abs_timecode) const;
1309*103e46e4SHarish Mahendrakar 
size_position()1310*103e46e4SHarish Mahendrakar   int64_t size_position() const { return size_position_; }
blocks_added()1311*103e46e4SHarish Mahendrakar   int32_t blocks_added() const { return blocks_added_; }
payload_size()1312*103e46e4SHarish Mahendrakar   uint64_t payload_size() const { return payload_size_; }
position_for_cues()1313*103e46e4SHarish Mahendrakar   int64_t position_for_cues() const { return position_for_cues_; }
timecode()1314*103e46e4SHarish Mahendrakar   uint64_t timecode() const { return timecode_; }
timecode_scale()1315*103e46e4SHarish Mahendrakar   uint64_t timecode_scale() const { return timecode_scale_; }
set_write_last_frame_with_duration(bool write_last_frame_with_duration)1316*103e46e4SHarish Mahendrakar   void set_write_last_frame_with_duration(bool write_last_frame_with_duration) {
1317*103e46e4SHarish Mahendrakar     write_last_frame_with_duration_ = write_last_frame_with_duration;
1318*103e46e4SHarish Mahendrakar   }
write_last_frame_with_duration()1319*103e46e4SHarish Mahendrakar   bool write_last_frame_with_duration() const {
1320*103e46e4SHarish Mahendrakar     return write_last_frame_with_duration_;
1321*103e46e4SHarish Mahendrakar   }
1322*103e46e4SHarish Mahendrakar 
1323*103e46e4SHarish Mahendrakar  private:
1324*103e46e4SHarish Mahendrakar   // Iterator type for the |stored_frames_| map.
1325*103e46e4SHarish Mahendrakar   typedef std::map<uint64_t, std::list<Frame*> >::iterator FrameMapIterator;
1326*103e46e4SHarish Mahendrakar 
1327*103e46e4SHarish Mahendrakar   // Utility method that confirms that blocks can still be added, and that the
1328*103e46e4SHarish Mahendrakar   // cluster header has been written. Used by |DoWriteFrame*|. Returns true
1329*103e46e4SHarish Mahendrakar   // when successful.
1330*103e46e4SHarish Mahendrakar   bool PreWriteBlock();
1331*103e46e4SHarish Mahendrakar 
1332*103e46e4SHarish Mahendrakar   // Utility method used by the |DoWriteFrame*| methods that handles the book
1333*103e46e4SHarish Mahendrakar   // keeping required after each block is written.
1334*103e46e4SHarish Mahendrakar   void PostWriteBlock(uint64_t element_size);
1335*103e46e4SHarish Mahendrakar 
1336*103e46e4SHarish Mahendrakar   // Does some verification and calls WriteFrame.
1337*103e46e4SHarish Mahendrakar   bool DoWriteFrame(const Frame* const frame);
1338*103e46e4SHarish Mahendrakar 
1339*103e46e4SHarish Mahendrakar   // Either holds back the given frame, or writes it out depending on whether or
1340*103e46e4SHarish Mahendrakar   // not |write_last_frame_with_duration_| is set.
1341*103e46e4SHarish Mahendrakar   bool QueueOrWriteFrame(const Frame* const frame);
1342*103e46e4SHarish Mahendrakar 
1343*103e46e4SHarish Mahendrakar   // Outputs the Cluster header to |writer_|. Returns true on success.
1344*103e46e4SHarish Mahendrakar   bool WriteClusterHeader();
1345*103e46e4SHarish Mahendrakar 
1346*103e46e4SHarish Mahendrakar   // Number of blocks added to the cluster.
1347*103e46e4SHarish Mahendrakar   int32_t blocks_added_;
1348*103e46e4SHarish Mahendrakar 
1349*103e46e4SHarish Mahendrakar   // Flag telling if the cluster has been closed.
1350*103e46e4SHarish Mahendrakar   bool finalized_;
1351*103e46e4SHarish Mahendrakar 
1352*103e46e4SHarish Mahendrakar   // Flag indicating whether the cluster's timecode will always be written out
1353*103e46e4SHarish Mahendrakar   // using 8 bytes.
1354*103e46e4SHarish Mahendrakar   bool fixed_size_timecode_;
1355*103e46e4SHarish Mahendrakar 
1356*103e46e4SHarish Mahendrakar   // Flag telling if the cluster's header has been written.
1357*103e46e4SHarish Mahendrakar   bool header_written_;
1358*103e46e4SHarish Mahendrakar 
1359*103e46e4SHarish Mahendrakar   // The size of the cluster elements in bytes.
1360*103e46e4SHarish Mahendrakar   uint64_t payload_size_;
1361*103e46e4SHarish Mahendrakar 
1362*103e46e4SHarish Mahendrakar   // The file position used for cue points.
1363*103e46e4SHarish Mahendrakar   const int64_t position_for_cues_;
1364*103e46e4SHarish Mahendrakar 
1365*103e46e4SHarish Mahendrakar   // The file position of the cluster's size element.
1366*103e46e4SHarish Mahendrakar   int64_t size_position_;
1367*103e46e4SHarish Mahendrakar 
1368*103e46e4SHarish Mahendrakar   // The absolute timecode of the cluster.
1369*103e46e4SHarish Mahendrakar   const uint64_t timecode_;
1370*103e46e4SHarish Mahendrakar 
1371*103e46e4SHarish Mahendrakar   // The timecode scale of the Segment containing the cluster.
1372*103e46e4SHarish Mahendrakar   const uint64_t timecode_scale_;
1373*103e46e4SHarish Mahendrakar 
1374*103e46e4SHarish Mahendrakar   // Flag indicating whether the last frame of the cluster should be written as
1375*103e46e4SHarish Mahendrakar   // a Block with Duration. If set to true, then it will result in holding back
1376*103e46e4SHarish Mahendrakar   // of frames and the parameterized version of Finalize() must be called to
1377*103e46e4SHarish Mahendrakar   // finish writing the Cluster.
1378*103e46e4SHarish Mahendrakar   bool write_last_frame_with_duration_;
1379*103e46e4SHarish Mahendrakar 
1380*103e46e4SHarish Mahendrakar   // Map used to hold back frames, if required. Track number is the key.
1381*103e46e4SHarish Mahendrakar   std::map<uint64_t, std::list<Frame*> > stored_frames_;
1382*103e46e4SHarish Mahendrakar 
1383*103e46e4SHarish Mahendrakar   // Map from track number to the timestamp of the last block written for that
1384*103e46e4SHarish Mahendrakar   // track.
1385*103e46e4SHarish Mahendrakar   std::map<uint64_t, uint64_t> last_block_timestamp_;
1386*103e46e4SHarish Mahendrakar 
1387*103e46e4SHarish Mahendrakar   // Pointer to the writer object. Not owned by this class.
1388*103e46e4SHarish Mahendrakar   IMkvWriter* writer_;
1389*103e46e4SHarish Mahendrakar 
1390*103e46e4SHarish Mahendrakar   LIBWEBM_DISALLOW_COPY_AND_ASSIGN(Cluster);
1391*103e46e4SHarish Mahendrakar };
1392*103e46e4SHarish Mahendrakar 
1393*103e46e4SHarish Mahendrakar ///////////////////////////////////////////////////////////////
1394*103e46e4SHarish Mahendrakar // SeekHead element
1395*103e46e4SHarish Mahendrakar class SeekHead {
1396*103e46e4SHarish Mahendrakar  public:
1397*103e46e4SHarish Mahendrakar   SeekHead();
1398*103e46e4SHarish Mahendrakar   ~SeekHead();
1399*103e46e4SHarish Mahendrakar 
1400*103e46e4SHarish Mahendrakar   // TODO(fgalligan): Change this to reserve a certain size. Then check how
1401*103e46e4SHarish Mahendrakar   // big the seek entry to be added is as not every seek entry will be the
1402*103e46e4SHarish Mahendrakar   // maximum size it could be.
1403*103e46e4SHarish Mahendrakar   // Adds a seek entry to be written out when the element is finalized. |id|
1404*103e46e4SHarish Mahendrakar   // must be the coded mkv element id. |pos| is the file position of the
1405*103e46e4SHarish Mahendrakar   // element. Returns true on success.
1406*103e46e4SHarish Mahendrakar   bool AddSeekEntry(uint32_t id, uint64_t pos);
1407*103e46e4SHarish Mahendrakar 
1408*103e46e4SHarish Mahendrakar   // Writes out SeekHead and SeekEntry elements. Returns true on success.
1409*103e46e4SHarish Mahendrakar   bool Finalize(IMkvWriter* writer) const;
1410*103e46e4SHarish Mahendrakar 
1411*103e46e4SHarish Mahendrakar   // Returns the id of the Seek Entry at the given index. Returns -1 if index is
1412*103e46e4SHarish Mahendrakar   // out of range.
1413*103e46e4SHarish Mahendrakar   uint32_t GetId(int index) const;
1414*103e46e4SHarish Mahendrakar 
1415*103e46e4SHarish Mahendrakar   // Returns the position of the Seek Entry at the given index. Returns -1 if
1416*103e46e4SHarish Mahendrakar   // index is out of range.
1417*103e46e4SHarish Mahendrakar   uint64_t GetPosition(int index) const;
1418*103e46e4SHarish Mahendrakar 
1419*103e46e4SHarish Mahendrakar   // Sets the Seek Entry id and position at given index.
1420*103e46e4SHarish Mahendrakar   // Returns true on success.
1421*103e46e4SHarish Mahendrakar   bool SetSeekEntry(int index, uint32_t id, uint64_t position);
1422*103e46e4SHarish Mahendrakar 
1423*103e46e4SHarish Mahendrakar   // Reserves space by writing out a Void element which will be updated with
1424*103e46e4SHarish Mahendrakar   // a SeekHead element later. Returns true on success.
1425*103e46e4SHarish Mahendrakar   bool Write(IMkvWriter* writer);
1426*103e46e4SHarish Mahendrakar 
1427*103e46e4SHarish Mahendrakar   // We are going to put a cap on the number of Seek Entries.
1428*103e46e4SHarish Mahendrakar   constexpr static int32_t kSeekEntryCount = 5;
1429*103e46e4SHarish Mahendrakar 
1430*103e46e4SHarish Mahendrakar  private:
1431*103e46e4SHarish Mahendrakar   // Returns the maximum size in bytes of one seek entry.
1432*103e46e4SHarish Mahendrakar   uint64_t MaxEntrySize() const;
1433*103e46e4SHarish Mahendrakar 
1434*103e46e4SHarish Mahendrakar   // Seek entry id element list.
1435*103e46e4SHarish Mahendrakar   uint32_t seek_entry_id_[kSeekEntryCount];
1436*103e46e4SHarish Mahendrakar 
1437*103e46e4SHarish Mahendrakar   // Seek entry pos element list.
1438*103e46e4SHarish Mahendrakar   uint64_t seek_entry_pos_[kSeekEntryCount];
1439*103e46e4SHarish Mahendrakar 
1440*103e46e4SHarish Mahendrakar   // The file position of SeekHead element.
1441*103e46e4SHarish Mahendrakar   int64_t start_pos_;
1442*103e46e4SHarish Mahendrakar 
1443*103e46e4SHarish Mahendrakar   LIBWEBM_DISALLOW_COPY_AND_ASSIGN(SeekHead);
1444*103e46e4SHarish Mahendrakar };
1445*103e46e4SHarish Mahendrakar 
1446*103e46e4SHarish Mahendrakar ///////////////////////////////////////////////////////////////
1447*103e46e4SHarish Mahendrakar // Segment Information element
1448*103e46e4SHarish Mahendrakar class SegmentInfo {
1449*103e46e4SHarish Mahendrakar  public:
1450*103e46e4SHarish Mahendrakar   SegmentInfo();
1451*103e46e4SHarish Mahendrakar   ~SegmentInfo();
1452*103e46e4SHarish Mahendrakar 
1453*103e46e4SHarish Mahendrakar   // Will update the duration if |duration_| is > 0.0. Returns true on success.
1454*103e46e4SHarish Mahendrakar   bool Finalize(IMkvWriter* writer) const;
1455*103e46e4SHarish Mahendrakar 
1456*103e46e4SHarish Mahendrakar   // Sets |muxing_app_| and |writing_app_|.
1457*103e46e4SHarish Mahendrakar   bool Init();
1458*103e46e4SHarish Mahendrakar 
1459*103e46e4SHarish Mahendrakar   // Output the Segment Information element to the writer. Returns true on
1460*103e46e4SHarish Mahendrakar   // success.
1461*103e46e4SHarish Mahendrakar   bool Write(IMkvWriter* writer);
1462*103e46e4SHarish Mahendrakar 
set_duration(double duration)1463*103e46e4SHarish Mahendrakar   void set_duration(double duration) { duration_ = duration; }
duration()1464*103e46e4SHarish Mahendrakar   double duration() const { return duration_; }
1465*103e46e4SHarish Mahendrakar   void set_muxing_app(const char* app);
muxing_app()1466*103e46e4SHarish Mahendrakar   const char* muxing_app() const { return muxing_app_; }
set_timecode_scale(uint64_t scale)1467*103e46e4SHarish Mahendrakar   void set_timecode_scale(uint64_t scale) { timecode_scale_ = scale; }
timecode_scale()1468*103e46e4SHarish Mahendrakar   uint64_t timecode_scale() const { return timecode_scale_; }
1469*103e46e4SHarish Mahendrakar   void set_writing_app(const char* app);
writing_app()1470*103e46e4SHarish Mahendrakar   const char* writing_app() const { return writing_app_; }
set_date_utc(int64_t date_utc)1471*103e46e4SHarish Mahendrakar   void set_date_utc(int64_t date_utc) { date_utc_ = date_utc; }
date_utc()1472*103e46e4SHarish Mahendrakar   int64_t date_utc() const { return date_utc_; }
1473*103e46e4SHarish Mahendrakar 
1474*103e46e4SHarish Mahendrakar  private:
1475*103e46e4SHarish Mahendrakar   // Segment Information element names.
1476*103e46e4SHarish Mahendrakar   // Initially set to -1 to signify that a duration has not been set and should
1477*103e46e4SHarish Mahendrakar   // not be written out.
1478*103e46e4SHarish Mahendrakar   double duration_;
1479*103e46e4SHarish Mahendrakar   // Set to libwebm-%d.%d.%d.%d, major, minor, build, revision.
1480*103e46e4SHarish Mahendrakar   char* muxing_app_;
1481*103e46e4SHarish Mahendrakar   uint64_t timecode_scale_;
1482*103e46e4SHarish Mahendrakar   // Initially set to libwebm-%d.%d.%d.%d, major, minor, build, revision.
1483*103e46e4SHarish Mahendrakar   char* writing_app_;
1484*103e46e4SHarish Mahendrakar   // INT64_MIN when DateUTC is not set.
1485*103e46e4SHarish Mahendrakar   int64_t date_utc_;
1486*103e46e4SHarish Mahendrakar 
1487*103e46e4SHarish Mahendrakar   // The file position of the duration element.
1488*103e46e4SHarish Mahendrakar   int64_t duration_pos_;
1489*103e46e4SHarish Mahendrakar 
1490*103e46e4SHarish Mahendrakar   LIBWEBM_DISALLOW_COPY_AND_ASSIGN(SegmentInfo);
1491*103e46e4SHarish Mahendrakar };
1492*103e46e4SHarish Mahendrakar 
1493*103e46e4SHarish Mahendrakar ///////////////////////////////////////////////////////////////
1494*103e46e4SHarish Mahendrakar // This class represents the main segment in a WebM file. Currently only
1495*103e46e4SHarish Mahendrakar // supports one Segment element.
1496*103e46e4SHarish Mahendrakar //
1497*103e46e4SHarish Mahendrakar // Notes:
1498*103e46e4SHarish Mahendrakar //  |Init| must be called before any other method in this class.
1499*103e46e4SHarish Mahendrakar class Segment {
1500*103e46e4SHarish Mahendrakar  public:
1501*103e46e4SHarish Mahendrakar   enum Mode { kLive = 0x1, kFile = 0x2 };
1502*103e46e4SHarish Mahendrakar 
1503*103e46e4SHarish Mahendrakar   enum CuesPosition {
1504*103e46e4SHarish Mahendrakar     kAfterClusters = 0x0,  // Position Cues after Clusters - Default
1505*103e46e4SHarish Mahendrakar     kBeforeClusters = 0x1  // Position Cues before Clusters
1506*103e46e4SHarish Mahendrakar   };
1507*103e46e4SHarish Mahendrakar 
1508*103e46e4SHarish Mahendrakar   static constexpr uint32_t kDefaultDocTypeVersion = 4;
1509*103e46e4SHarish Mahendrakar   static constexpr uint64_t kDefaultMaxClusterDuration = 30000000000ULL;
1510*103e46e4SHarish Mahendrakar 
1511*103e46e4SHarish Mahendrakar   Segment();
1512*103e46e4SHarish Mahendrakar   ~Segment();
1513*103e46e4SHarish Mahendrakar 
1514*103e46e4SHarish Mahendrakar   // Initializes |SegmentInfo| and returns result. Always returns false when
1515*103e46e4SHarish Mahendrakar   // |ptr_writer| is NULL.
1516*103e46e4SHarish Mahendrakar   bool Init(IMkvWriter* ptr_writer);
1517*103e46e4SHarish Mahendrakar 
1518*103e46e4SHarish Mahendrakar   // Adds a generic track to the segment.  Returns the newly-allocated
1519*103e46e4SHarish Mahendrakar   // track object (which is owned by the segment) on success, NULL on
1520*103e46e4SHarish Mahendrakar   // error. |number| is the number to use for the track.  |number|
1521*103e46e4SHarish Mahendrakar   // must be >= 0. If |number| == 0 then the muxer will decide on the
1522*103e46e4SHarish Mahendrakar   // track number.
1523*103e46e4SHarish Mahendrakar   Track* AddTrack(int32_t number);
1524*103e46e4SHarish Mahendrakar 
1525*103e46e4SHarish Mahendrakar   // Adds a Vorbis audio track to the segment. Returns the number of the track
1526*103e46e4SHarish Mahendrakar   // on success, 0 on error. |number| is the number to use for the audio track.
1527*103e46e4SHarish Mahendrakar   // |number| must be >= 0. If |number| == 0 then the muxer will decide on
1528*103e46e4SHarish Mahendrakar   // the track number.
1529*103e46e4SHarish Mahendrakar   uint64_t AddAudioTrack(int32_t sample_rate, int32_t channels, int32_t number);
1530*103e46e4SHarish Mahendrakar 
1531*103e46e4SHarish Mahendrakar   // Adds an empty chapter to the chapters of this segment.  Returns
1532*103e46e4SHarish Mahendrakar   // non-NULL on success.  After adding the chapter, the caller should
1533*103e46e4SHarish Mahendrakar   // populate its fields via the Chapter member functions.
1534*103e46e4SHarish Mahendrakar   Chapter* AddChapter();
1535*103e46e4SHarish Mahendrakar 
1536*103e46e4SHarish Mahendrakar   // Adds an empty tag to the tags of this segment.  Returns
1537*103e46e4SHarish Mahendrakar   // non-NULL on success.  After adding the tag, the caller should
1538*103e46e4SHarish Mahendrakar   // populate its fields via the Tag member functions.
1539*103e46e4SHarish Mahendrakar   Tag* AddTag();
1540*103e46e4SHarish Mahendrakar 
1541*103e46e4SHarish Mahendrakar   // Adds a cue point to the Cues element. |timestamp| is the time in
1542*103e46e4SHarish Mahendrakar   // nanoseconds of the cue's time. |track| is the Track of the Cue. This
1543*103e46e4SHarish Mahendrakar   // function must be called after AddFrame to calculate the correct
1544*103e46e4SHarish Mahendrakar   // BlockNumber for the CuePoint. Returns true on success.
1545*103e46e4SHarish Mahendrakar   bool AddCuePoint(uint64_t timestamp, uint64_t track);
1546*103e46e4SHarish Mahendrakar 
1547*103e46e4SHarish Mahendrakar   // Adds a frame to be output in the file. Returns true on success.
1548*103e46e4SHarish Mahendrakar   // Inputs:
1549*103e46e4SHarish Mahendrakar   //   data: Pointer to the data
1550*103e46e4SHarish Mahendrakar   //   length: Length of the data
1551*103e46e4SHarish Mahendrakar   //   track_number: Track to add the data to. Value returned by Add track
1552*103e46e4SHarish Mahendrakar   //                 functions.
1553*103e46e4SHarish Mahendrakar   //   timestamp:    Timestamp of the frame in nanoseconds from 0.
1554*103e46e4SHarish Mahendrakar   //   is_key:       Flag telling whether or not this frame is a key frame.
1555*103e46e4SHarish Mahendrakar   bool AddFrame(const uint8_t* data, uint64_t length, uint64_t track_number,
1556*103e46e4SHarish Mahendrakar                 uint64_t timestamp_ns, bool is_key);
1557*103e46e4SHarish Mahendrakar 
1558*103e46e4SHarish Mahendrakar   // Writes a frame of metadata to the output medium; returns true on
1559*103e46e4SHarish Mahendrakar   // success.
1560*103e46e4SHarish Mahendrakar   // Inputs:
1561*103e46e4SHarish Mahendrakar   //   data: Pointer to the data
1562*103e46e4SHarish Mahendrakar   //   length: Length of the data
1563*103e46e4SHarish Mahendrakar   //   track_number: Track to add the data to. Value returned by Add track
1564*103e46e4SHarish Mahendrakar   //                 functions.
1565*103e46e4SHarish Mahendrakar   //   timecode:     Absolute timestamp of the metadata frame, expressed
1566*103e46e4SHarish Mahendrakar   //                 in nanosecond units.
1567*103e46e4SHarish Mahendrakar   //   duration:     Duration of metadata frame, in nanosecond units.
1568*103e46e4SHarish Mahendrakar   //
1569*103e46e4SHarish Mahendrakar   // The metadata frame is written as a block group, with a duration
1570*103e46e4SHarish Mahendrakar   // sub-element but no reference time sub-elements (indicating that
1571*103e46e4SHarish Mahendrakar   // it is considered a keyframe, per Matroska semantics).
1572*103e46e4SHarish Mahendrakar   bool AddMetadata(const uint8_t* data, uint64_t length, uint64_t track_number,
1573*103e46e4SHarish Mahendrakar                    uint64_t timestamp_ns, uint64_t duration_ns);
1574*103e46e4SHarish Mahendrakar 
1575*103e46e4SHarish Mahendrakar   // Writes a frame with additional data to the output medium; returns true on
1576*103e46e4SHarish Mahendrakar   // success.
1577*103e46e4SHarish Mahendrakar   // Inputs:
1578*103e46e4SHarish Mahendrakar   //   data: Pointer to the data.
1579*103e46e4SHarish Mahendrakar   //   length: Length of the data.
1580*103e46e4SHarish Mahendrakar   //   additional: Pointer to additional data.
1581*103e46e4SHarish Mahendrakar   //   additional_length: Length of additional data.
1582*103e46e4SHarish Mahendrakar   //   add_id: Additional ID which identifies the type of additional data.
1583*103e46e4SHarish Mahendrakar   //   track_number: Track to add the data to. Value returned by Add track
1584*103e46e4SHarish Mahendrakar   //                 functions.
1585*103e46e4SHarish Mahendrakar   //   timestamp:    Absolute timestamp of the frame, expressed in nanosecond
1586*103e46e4SHarish Mahendrakar   //                 units.
1587*103e46e4SHarish Mahendrakar   //   is_key:       Flag telling whether or not this frame is a key frame.
1588*103e46e4SHarish Mahendrakar   bool AddFrameWithAdditional(const uint8_t* data, uint64_t length,
1589*103e46e4SHarish Mahendrakar                               const uint8_t* additional,
1590*103e46e4SHarish Mahendrakar                               uint64_t additional_length, uint64_t add_id,
1591*103e46e4SHarish Mahendrakar                               uint64_t track_number, uint64_t timestamp,
1592*103e46e4SHarish Mahendrakar                               bool is_key);
1593*103e46e4SHarish Mahendrakar 
1594*103e46e4SHarish Mahendrakar   // Writes a frame with DiscardPadding to the output medium; returns true on
1595*103e46e4SHarish Mahendrakar   // success.
1596*103e46e4SHarish Mahendrakar   // Inputs:
1597*103e46e4SHarish Mahendrakar   //   data: Pointer to the data.
1598*103e46e4SHarish Mahendrakar   //   length: Length of the data.
1599*103e46e4SHarish Mahendrakar   //   discard_padding: DiscardPadding element value.
1600*103e46e4SHarish Mahendrakar   //   track_number: Track to add the data to. Value returned by Add track
1601*103e46e4SHarish Mahendrakar   //                 functions.
1602*103e46e4SHarish Mahendrakar   //   timestamp:    Absolute timestamp of the frame, expressed in nanosecond
1603*103e46e4SHarish Mahendrakar   //                 units.
1604*103e46e4SHarish Mahendrakar   //   is_key:       Flag telling whether or not this frame is a key frame.
1605*103e46e4SHarish Mahendrakar   bool AddFrameWithDiscardPadding(const uint8_t* data, uint64_t length,
1606*103e46e4SHarish Mahendrakar                                   int64_t discard_padding,
1607*103e46e4SHarish Mahendrakar                                   uint64_t track_number, uint64_t timestamp,
1608*103e46e4SHarish Mahendrakar                                   bool is_key);
1609*103e46e4SHarish Mahendrakar 
1610*103e46e4SHarish Mahendrakar   // Writes a Frame to the output medium. Chooses the correct way of writing
1611*103e46e4SHarish Mahendrakar   // the frame (Block vs SimpleBlock) based on the parameters passed.
1612*103e46e4SHarish Mahendrakar   // Inputs:
1613*103e46e4SHarish Mahendrakar   //   frame: frame object
1614*103e46e4SHarish Mahendrakar   bool AddGenericFrame(const Frame* frame);
1615*103e46e4SHarish Mahendrakar 
1616*103e46e4SHarish Mahendrakar   // Adds a VP8 video track to the segment. Returns the number of the track on
1617*103e46e4SHarish Mahendrakar   // success, 0 on error. |number| is the number to use for the video track.
1618*103e46e4SHarish Mahendrakar   // |number| must be >= 0. If |number| == 0 then the muxer will decide on
1619*103e46e4SHarish Mahendrakar   // the track number.
1620*103e46e4SHarish Mahendrakar   uint64_t AddVideoTrack(int32_t width, int32_t height, int32_t number);
1621*103e46e4SHarish Mahendrakar 
1622*103e46e4SHarish Mahendrakar   // This function must be called after Finalize() if you need a copy of the
1623*103e46e4SHarish Mahendrakar   // output with Cues written before the Clusters. It will return false if the
1624*103e46e4SHarish Mahendrakar   // writer is not seekable of if chunking is set to true.
1625*103e46e4SHarish Mahendrakar   // Input parameters:
1626*103e46e4SHarish Mahendrakar   // reader - an IMkvReader object created with the same underlying file of the
1627*103e46e4SHarish Mahendrakar   //          current writer object. Make sure to close the existing writer
1628*103e46e4SHarish Mahendrakar   //          object before creating this so that all the data is properly
1629*103e46e4SHarish Mahendrakar   //          flushed and available for reading.
1630*103e46e4SHarish Mahendrakar   // writer - an IMkvWriter object pointing to a *different* file than the one
1631*103e46e4SHarish Mahendrakar   //          pointed by the current writer object. This file will contain the
1632*103e46e4SHarish Mahendrakar   //          Cues element before the Clusters.
1633*103e46e4SHarish Mahendrakar   bool CopyAndMoveCuesBeforeClusters(mkvparser::IMkvReader* reader,
1634*103e46e4SHarish Mahendrakar                                      IMkvWriter* writer);
1635*103e46e4SHarish Mahendrakar 
1636*103e46e4SHarish Mahendrakar   // Sets which track to use for the Cues element. Must have added the track
1637*103e46e4SHarish Mahendrakar   // before calling this function. Returns true on success. |track_number| is
1638*103e46e4SHarish Mahendrakar   // returned by the Add track functions.
1639*103e46e4SHarish Mahendrakar   bool CuesTrack(uint64_t track_number);
1640*103e46e4SHarish Mahendrakar 
1641*103e46e4SHarish Mahendrakar   // This will force the muxer to create a new Cluster when the next frame is
1642*103e46e4SHarish Mahendrakar   // added.
1643*103e46e4SHarish Mahendrakar   void ForceNewClusterOnNextFrame();
1644*103e46e4SHarish Mahendrakar 
1645*103e46e4SHarish Mahendrakar   // Writes out any frames that have not been written out. Finalizes the last
1646*103e46e4SHarish Mahendrakar   // cluster. May update the size and duration of the segment. May output the
1647*103e46e4SHarish Mahendrakar   // Cues element. May finalize the SeekHead element. Returns true on success.
1648*103e46e4SHarish Mahendrakar   bool Finalize();
1649*103e46e4SHarish Mahendrakar 
1650*103e46e4SHarish Mahendrakar   // Returns the Cues object.
GetCues()1651*103e46e4SHarish Mahendrakar   Cues* GetCues() { return &cues_; }
1652*103e46e4SHarish Mahendrakar 
1653*103e46e4SHarish Mahendrakar   // Returns the Segment Information object.
GetSegmentInfo()1654*103e46e4SHarish Mahendrakar   const SegmentInfo* GetSegmentInfo() const { return &segment_info_; }
GetSegmentInfo()1655*103e46e4SHarish Mahendrakar   SegmentInfo* GetSegmentInfo() { return &segment_info_; }
1656*103e46e4SHarish Mahendrakar 
1657*103e46e4SHarish Mahendrakar   // Search the Tracks and return the track that matches |track_number|.
1658*103e46e4SHarish Mahendrakar   // Returns NULL if there is no track match.
1659*103e46e4SHarish Mahendrakar   Track* GetTrackByNumber(uint64_t track_number) const;
1660*103e46e4SHarish Mahendrakar 
1661*103e46e4SHarish Mahendrakar   // Toggles whether to output a cues element.
1662*103e46e4SHarish Mahendrakar   void OutputCues(bool output_cues);
1663*103e46e4SHarish Mahendrakar 
1664*103e46e4SHarish Mahendrakar   // Toggles whether to write the last frame in each Cluster with Duration.
1665*103e46e4SHarish Mahendrakar   void AccurateClusterDuration(bool accurate_cluster_duration);
1666*103e46e4SHarish Mahendrakar 
1667*103e46e4SHarish Mahendrakar   // Toggles whether to write the Cluster Timecode using exactly 8 bytes.
1668*103e46e4SHarish Mahendrakar   void UseFixedSizeClusterTimecode(bool fixed_size_cluster_timecode);
1669*103e46e4SHarish Mahendrakar 
1670*103e46e4SHarish Mahendrakar   // Sets if the muxer will output files in chunks or not. |chunking| is a
1671*103e46e4SHarish Mahendrakar   // flag telling whether or not to turn on chunking. |filename| is the base
1672*103e46e4SHarish Mahendrakar   // filename for the chunk files. The header chunk file will be named
1673*103e46e4SHarish Mahendrakar   // |filename|.hdr and the data chunks will be named
1674*103e46e4SHarish Mahendrakar   // |filename|_XXXXXX.chk. Chunking implies that the muxer will be writing
1675*103e46e4SHarish Mahendrakar   // to files so the muxer will use the default MkvWriter class to control
1676*103e46e4SHarish Mahendrakar   // what data is written to what files. Returns true on success.
1677*103e46e4SHarish Mahendrakar   // TODO: Should we change the IMkvWriter Interface to add Open and Close?
1678*103e46e4SHarish Mahendrakar   // That will force the interface to be dependent on files.
1679*103e46e4SHarish Mahendrakar   bool SetChunking(bool chunking, const char* filename);
1680*103e46e4SHarish Mahendrakar 
chunking()1681*103e46e4SHarish Mahendrakar   bool chunking() const { return chunking_; }
cues_track()1682*103e46e4SHarish Mahendrakar   uint64_t cues_track() const { return cues_track_; }
set_max_cluster_duration(uint64_t max_cluster_duration)1683*103e46e4SHarish Mahendrakar   void set_max_cluster_duration(uint64_t max_cluster_duration) {
1684*103e46e4SHarish Mahendrakar     max_cluster_duration_ = max_cluster_duration;
1685*103e46e4SHarish Mahendrakar   }
max_cluster_duration()1686*103e46e4SHarish Mahendrakar   uint64_t max_cluster_duration() const { return max_cluster_duration_; }
set_max_cluster_size(uint64_t max_cluster_size)1687*103e46e4SHarish Mahendrakar   void set_max_cluster_size(uint64_t max_cluster_size) {
1688*103e46e4SHarish Mahendrakar     max_cluster_size_ = max_cluster_size;
1689*103e46e4SHarish Mahendrakar   }
max_cluster_size()1690*103e46e4SHarish Mahendrakar   uint64_t max_cluster_size() const { return max_cluster_size_; }
set_mode(Mode mode)1691*103e46e4SHarish Mahendrakar   void set_mode(Mode mode) { mode_ = mode; }
mode()1692*103e46e4SHarish Mahendrakar   Mode mode() const { return mode_; }
cues_position()1693*103e46e4SHarish Mahendrakar   CuesPosition cues_position() const { return cues_position_; }
output_cues()1694*103e46e4SHarish Mahendrakar   bool output_cues() const { return output_cues_; }
set_estimate_file_duration(bool estimate_duration)1695*103e46e4SHarish Mahendrakar   void set_estimate_file_duration(bool estimate_duration) {
1696*103e46e4SHarish Mahendrakar     estimate_file_duration_ = estimate_duration;
1697*103e46e4SHarish Mahendrakar   }
estimate_file_duration()1698*103e46e4SHarish Mahendrakar   bool estimate_file_duration() const { return estimate_file_duration_; }
segment_info()1699*103e46e4SHarish Mahendrakar   const SegmentInfo* segment_info() const { return &segment_info_; }
set_duration(double duration)1700*103e46e4SHarish Mahendrakar   void set_duration(double duration) { duration_ = duration; }
duration()1701*103e46e4SHarish Mahendrakar   double duration() const { return duration_; }
1702*103e46e4SHarish Mahendrakar 
1703*103e46e4SHarish Mahendrakar   // Returns true when codec IDs are valid for WebM.
1704*103e46e4SHarish Mahendrakar   bool DocTypeIsWebm() const;
1705*103e46e4SHarish Mahendrakar 
1706*103e46e4SHarish Mahendrakar  private:
1707*103e46e4SHarish Mahendrakar   // Checks if header information has been output and initialized. If not it
1708*103e46e4SHarish Mahendrakar   // will output the Segment element and initialize the SeekHead elment and
1709*103e46e4SHarish Mahendrakar   // Cues elements.
1710*103e46e4SHarish Mahendrakar   bool CheckHeaderInfo();
1711*103e46e4SHarish Mahendrakar 
1712*103e46e4SHarish Mahendrakar   // Sets |doc_type_version_| based on the current element requirements.
1713*103e46e4SHarish Mahendrakar   void UpdateDocTypeVersion();
1714*103e46e4SHarish Mahendrakar 
1715*103e46e4SHarish Mahendrakar   // Sets |name| according to how many chunks have been written. |ext| is the
1716*103e46e4SHarish Mahendrakar   // file extension. |name| must be deleted by the calling app. Returns true
1717*103e46e4SHarish Mahendrakar   // on success.
1718*103e46e4SHarish Mahendrakar   bool UpdateChunkName(const char* ext, char** name) const;
1719*103e46e4SHarish Mahendrakar 
1720*103e46e4SHarish Mahendrakar   // Returns the maximum offset within the segment's payload. When chunking
1721*103e46e4SHarish Mahendrakar   // this function is needed to determine offsets of elements within the
1722*103e46e4SHarish Mahendrakar   // chunked files. Returns -1 on error.
1723*103e46e4SHarish Mahendrakar   int64_t MaxOffset();
1724*103e46e4SHarish Mahendrakar 
1725*103e46e4SHarish Mahendrakar   // Adds the frame to our frame array.
1726*103e46e4SHarish Mahendrakar   bool QueueFrame(Frame* frame);
1727*103e46e4SHarish Mahendrakar 
1728*103e46e4SHarish Mahendrakar   // Output all frames that are queued. Returns -1 on error, otherwise
1729*103e46e4SHarish Mahendrakar   // it returns the number of frames written.
1730*103e46e4SHarish Mahendrakar   int WriteFramesAll();
1731*103e46e4SHarish Mahendrakar 
1732*103e46e4SHarish Mahendrakar   // Output all frames that are queued that have an end time that is less
1733*103e46e4SHarish Mahendrakar   // then |timestamp|. Returns true on success and if there are no frames
1734*103e46e4SHarish Mahendrakar   // queued.
1735*103e46e4SHarish Mahendrakar   bool WriteFramesLessThan(uint64_t timestamp);
1736*103e46e4SHarish Mahendrakar 
1737*103e46e4SHarish Mahendrakar   // Outputs the segment header, Segment Information element, SeekHead element,
1738*103e46e4SHarish Mahendrakar   // and Tracks element to |writer_|.
1739*103e46e4SHarish Mahendrakar   bool WriteSegmentHeader();
1740*103e46e4SHarish Mahendrakar 
1741*103e46e4SHarish Mahendrakar   // Given a frame with the specified timestamp (nanosecond units) and
1742*103e46e4SHarish Mahendrakar   // keyframe status, determine whether a new cluster should be
1743*103e46e4SHarish Mahendrakar   // created, before writing enqueued frames and the frame itself. The
1744*103e46e4SHarish Mahendrakar   // function returns one of the following values:
1745*103e46e4SHarish Mahendrakar   //  -1 = error: an out-of-order frame was detected
1746*103e46e4SHarish Mahendrakar   //  0 = do not create a new cluster, and write frame to the existing cluster
1747*103e46e4SHarish Mahendrakar   //  1 = create a new cluster, and write frame to that new cluster
1748*103e46e4SHarish Mahendrakar   //  2 = create a new cluster, and re-run test
1749*103e46e4SHarish Mahendrakar   int TestFrame(uint64_t track_num, uint64_t timestamp_ns, bool key) const;
1750*103e46e4SHarish Mahendrakar 
1751*103e46e4SHarish Mahendrakar   // Create a new cluster, using the earlier of the first enqueued
1752*103e46e4SHarish Mahendrakar   // frame, or the indicated time. Returns true on success.
1753*103e46e4SHarish Mahendrakar   bool MakeNewCluster(uint64_t timestamp_ns);
1754*103e46e4SHarish Mahendrakar 
1755*103e46e4SHarish Mahendrakar   // Checks whether a new cluster needs to be created, and if so
1756*103e46e4SHarish Mahendrakar   // creates a new cluster. Returns false if creation of a new cluster
1757*103e46e4SHarish Mahendrakar   // was necessary but creation was not successful.
1758*103e46e4SHarish Mahendrakar   bool DoNewClusterProcessing(uint64_t track_num, uint64_t timestamp_ns,
1759*103e46e4SHarish Mahendrakar                               bool key);
1760*103e46e4SHarish Mahendrakar 
1761*103e46e4SHarish Mahendrakar   // Adjusts Cue Point values (to place Cues before Clusters) so that they
1762*103e46e4SHarish Mahendrakar   // reflect the correct offsets.
1763*103e46e4SHarish Mahendrakar   void MoveCuesBeforeClusters();
1764*103e46e4SHarish Mahendrakar 
1765*103e46e4SHarish Mahendrakar   // This function recursively computes the correct cluster offsets (this is
1766*103e46e4SHarish Mahendrakar   // done to move the Cues before Clusters). It recursively updates the change
1767*103e46e4SHarish Mahendrakar   // in size (which indicates a change in cluster offset) until no sizes change.
1768*103e46e4SHarish Mahendrakar   // Parameters:
1769*103e46e4SHarish Mahendrakar   // diff - indicates the difference in size of the Cues element that needs to
1770*103e46e4SHarish Mahendrakar   //        accounted for.
1771*103e46e4SHarish Mahendrakar   // index - index in the list of Cues which is currently being adjusted.
1772*103e46e4SHarish Mahendrakar   // cue_size - sum of size of all the CuePoint elements.
1773*103e46e4SHarish Mahendrakar   void MoveCuesBeforeClustersHelper(uint64_t diff, int index,
1774*103e46e4SHarish Mahendrakar                                     uint64_t* cue_size);
1775*103e46e4SHarish Mahendrakar 
1776*103e46e4SHarish Mahendrakar   // Seeds the random number generator used to make UIDs.
1777*103e46e4SHarish Mahendrakar   unsigned int seed_;
1778*103e46e4SHarish Mahendrakar 
1779*103e46e4SHarish Mahendrakar   // WebM elements
1780*103e46e4SHarish Mahendrakar   Cues cues_;
1781*103e46e4SHarish Mahendrakar   SeekHead seek_head_;
1782*103e46e4SHarish Mahendrakar   SegmentInfo segment_info_;
1783*103e46e4SHarish Mahendrakar   Tracks tracks_;
1784*103e46e4SHarish Mahendrakar   Chapters chapters_;
1785*103e46e4SHarish Mahendrakar   Tags tags_;
1786*103e46e4SHarish Mahendrakar 
1787*103e46e4SHarish Mahendrakar   // Number of chunks written.
1788*103e46e4SHarish Mahendrakar   int chunk_count_;
1789*103e46e4SHarish Mahendrakar 
1790*103e46e4SHarish Mahendrakar   // Current chunk filename.
1791*103e46e4SHarish Mahendrakar   char* chunk_name_;
1792*103e46e4SHarish Mahendrakar 
1793*103e46e4SHarish Mahendrakar   // Default MkvWriter object created by this class used for writing clusters
1794*103e46e4SHarish Mahendrakar   // out in separate files.
1795*103e46e4SHarish Mahendrakar   MkvWriter* chunk_writer_cluster_;
1796*103e46e4SHarish Mahendrakar 
1797*103e46e4SHarish Mahendrakar   // Default MkvWriter object created by this class used for writing Cues
1798*103e46e4SHarish Mahendrakar   // element out to a file.
1799*103e46e4SHarish Mahendrakar   MkvWriter* chunk_writer_cues_;
1800*103e46e4SHarish Mahendrakar 
1801*103e46e4SHarish Mahendrakar   // Default MkvWriter object created by this class used for writing the
1802*103e46e4SHarish Mahendrakar   // Matroska header out to a file.
1803*103e46e4SHarish Mahendrakar   MkvWriter* chunk_writer_header_;
1804*103e46e4SHarish Mahendrakar 
1805*103e46e4SHarish Mahendrakar   // Flag telling whether or not the muxer is chunking output to multiple
1806*103e46e4SHarish Mahendrakar   // files.
1807*103e46e4SHarish Mahendrakar   bool chunking_;
1808*103e46e4SHarish Mahendrakar 
1809*103e46e4SHarish Mahendrakar   // Base filename for the chunked files.
1810*103e46e4SHarish Mahendrakar   char* chunking_base_name_;
1811*103e46e4SHarish Mahendrakar 
1812*103e46e4SHarish Mahendrakar   // File position offset where the Clusters end.
1813*103e46e4SHarish Mahendrakar   int64_t cluster_end_offset_;
1814*103e46e4SHarish Mahendrakar 
1815*103e46e4SHarish Mahendrakar   // List of clusters.
1816*103e46e4SHarish Mahendrakar   Cluster** cluster_list_;
1817*103e46e4SHarish Mahendrakar 
1818*103e46e4SHarish Mahendrakar   // Number of cluster pointers allocated in the cluster list.
1819*103e46e4SHarish Mahendrakar   int32_t cluster_list_capacity_;
1820*103e46e4SHarish Mahendrakar 
1821*103e46e4SHarish Mahendrakar   // Number of clusters in the cluster list.
1822*103e46e4SHarish Mahendrakar   int32_t cluster_list_size_;
1823*103e46e4SHarish Mahendrakar 
1824*103e46e4SHarish Mahendrakar   // Indicates whether Cues should be written before or after Clusters
1825*103e46e4SHarish Mahendrakar   CuesPosition cues_position_;
1826*103e46e4SHarish Mahendrakar 
1827*103e46e4SHarish Mahendrakar   // Track number that is associated with the cues element for this segment.
1828*103e46e4SHarish Mahendrakar   uint64_t cues_track_;
1829*103e46e4SHarish Mahendrakar 
1830*103e46e4SHarish Mahendrakar   // Tells the muxer to force a new cluster on the next Block.
1831*103e46e4SHarish Mahendrakar   bool force_new_cluster_;
1832*103e46e4SHarish Mahendrakar 
1833*103e46e4SHarish Mahendrakar   // List of stored audio frames. These variables are used to store frames so
1834*103e46e4SHarish Mahendrakar   // the muxer can follow the guideline "Audio blocks that contain the video
1835*103e46e4SHarish Mahendrakar   // key frame's timecode should be in the same cluster as the video key frame
1836*103e46e4SHarish Mahendrakar   // block."
1837*103e46e4SHarish Mahendrakar   Frame** frames_;
1838*103e46e4SHarish Mahendrakar 
1839*103e46e4SHarish Mahendrakar   // Number of frame pointers allocated in the frame list.
1840*103e46e4SHarish Mahendrakar   int32_t frames_capacity_;
1841*103e46e4SHarish Mahendrakar 
1842*103e46e4SHarish Mahendrakar   // Number of frames in the frame list.
1843*103e46e4SHarish Mahendrakar   int32_t frames_size_;
1844*103e46e4SHarish Mahendrakar 
1845*103e46e4SHarish Mahendrakar   // Flag telling if a video track has been added to the segment.
1846*103e46e4SHarish Mahendrakar   bool has_video_;
1847*103e46e4SHarish Mahendrakar 
1848*103e46e4SHarish Mahendrakar   // Flag telling if the segment's header has been written.
1849*103e46e4SHarish Mahendrakar   bool header_written_;
1850*103e46e4SHarish Mahendrakar 
1851*103e46e4SHarish Mahendrakar   // Duration of the last block in nanoseconds.
1852*103e46e4SHarish Mahendrakar   uint64_t last_block_duration_;
1853*103e46e4SHarish Mahendrakar 
1854*103e46e4SHarish Mahendrakar   // Last timestamp in nanoseconds added to a cluster.
1855*103e46e4SHarish Mahendrakar   uint64_t last_timestamp_;
1856*103e46e4SHarish Mahendrakar 
1857*103e46e4SHarish Mahendrakar   // Last timestamp in nanoseconds by track number added to a cluster.
1858*103e46e4SHarish Mahendrakar   uint64_t last_track_timestamp_[kMaxTrackNumber];
1859*103e46e4SHarish Mahendrakar 
1860*103e46e4SHarish Mahendrakar   // Number of frames written per track.
1861*103e46e4SHarish Mahendrakar   uint64_t track_frames_written_[kMaxTrackNumber];
1862*103e46e4SHarish Mahendrakar 
1863*103e46e4SHarish Mahendrakar   // Maximum time in nanoseconds for a cluster duration. This variable is a
1864*103e46e4SHarish Mahendrakar   // guideline and some clusters may have a longer duration. Default is 30
1865*103e46e4SHarish Mahendrakar   // seconds.
1866*103e46e4SHarish Mahendrakar   uint64_t max_cluster_duration_;
1867*103e46e4SHarish Mahendrakar 
1868*103e46e4SHarish Mahendrakar   // Maximum size in bytes for a cluster. This variable is a guideline and
1869*103e46e4SHarish Mahendrakar   // some clusters may have a larger size. Default is 0 which signifies that
1870*103e46e4SHarish Mahendrakar   // the muxer will decide the size.
1871*103e46e4SHarish Mahendrakar   uint64_t max_cluster_size_;
1872*103e46e4SHarish Mahendrakar 
1873*103e46e4SHarish Mahendrakar   // The mode that segment is in. If set to |kLive| the writer must not
1874*103e46e4SHarish Mahendrakar   // seek backwards.
1875*103e46e4SHarish Mahendrakar   Mode mode_;
1876*103e46e4SHarish Mahendrakar 
1877*103e46e4SHarish Mahendrakar   // Flag telling the muxer that a new cue point should be added.
1878*103e46e4SHarish Mahendrakar   bool new_cuepoint_;
1879*103e46e4SHarish Mahendrakar 
1880*103e46e4SHarish Mahendrakar   // TODO(fgalligan): Should we add support for more than one Cues element?
1881*103e46e4SHarish Mahendrakar   // Flag whether or not the muxer should output a Cues element.
1882*103e46e4SHarish Mahendrakar   bool output_cues_;
1883*103e46e4SHarish Mahendrakar 
1884*103e46e4SHarish Mahendrakar   // Flag whether or not the last frame in each Cluster will have a Duration
1885*103e46e4SHarish Mahendrakar   // element in it.
1886*103e46e4SHarish Mahendrakar   bool accurate_cluster_duration_;
1887*103e46e4SHarish Mahendrakar 
1888*103e46e4SHarish Mahendrakar   // Flag whether or not to write the Cluster Timecode using exactly 8 bytes.
1889*103e46e4SHarish Mahendrakar   bool fixed_size_cluster_timecode_;
1890*103e46e4SHarish Mahendrakar 
1891*103e46e4SHarish Mahendrakar   // Flag whether or not to estimate the file duration.
1892*103e46e4SHarish Mahendrakar   bool estimate_file_duration_;
1893*103e46e4SHarish Mahendrakar 
1894*103e46e4SHarish Mahendrakar   // The size of the EBML header, used to validate the header if
1895*103e46e4SHarish Mahendrakar   // WriteEbmlHeader() is called more than once.
1896*103e46e4SHarish Mahendrakar   int32_t ebml_header_size_;
1897*103e46e4SHarish Mahendrakar 
1898*103e46e4SHarish Mahendrakar   // The file position of the segment's payload.
1899*103e46e4SHarish Mahendrakar   int64_t payload_pos_;
1900*103e46e4SHarish Mahendrakar 
1901*103e46e4SHarish Mahendrakar   // The file position of the element's size.
1902*103e46e4SHarish Mahendrakar   int64_t size_position_;
1903*103e46e4SHarish Mahendrakar 
1904*103e46e4SHarish Mahendrakar   // Current DocTypeVersion (|doc_type_version_|) and that written in
1905*103e46e4SHarish Mahendrakar   // WriteSegmentHeader().
1906*103e46e4SHarish Mahendrakar   // WriteEbmlHeader() will be called from Finalize() if |doc_type_version_|
1907*103e46e4SHarish Mahendrakar   // differs from |doc_type_version_written_|.
1908*103e46e4SHarish Mahendrakar   uint32_t doc_type_version_;
1909*103e46e4SHarish Mahendrakar   uint32_t doc_type_version_written_;
1910*103e46e4SHarish Mahendrakar 
1911*103e46e4SHarish Mahendrakar   // If |duration_| is > 0, then explicitly set the duration of the segment.
1912*103e46e4SHarish Mahendrakar   double duration_;
1913*103e46e4SHarish Mahendrakar 
1914*103e46e4SHarish Mahendrakar   // Pointer to the writer objects. Not owned by this class.
1915*103e46e4SHarish Mahendrakar   IMkvWriter* writer_cluster_;
1916*103e46e4SHarish Mahendrakar   IMkvWriter* writer_cues_;
1917*103e46e4SHarish Mahendrakar   IMkvWriter* writer_header_;
1918*103e46e4SHarish Mahendrakar 
1919*103e46e4SHarish Mahendrakar   LIBWEBM_DISALLOW_COPY_AND_ASSIGN(Segment);
1920*103e46e4SHarish Mahendrakar };
1921*103e46e4SHarish Mahendrakar 
1922*103e46e4SHarish Mahendrakar }  // namespace mkvmuxer
1923*103e46e4SHarish Mahendrakar 
1924*103e46e4SHarish Mahendrakar #endif  // MKVMUXER_MKVMUXER_H_
1925