1// Copyright (c) 2012 The WebM project authors. All Rights Reserved.
2//
3// Use of this source code is governed by a BSD-style license
4// that can be found in the LICENSE file in the root of the source
5// tree. An additional intellectual property rights grant can be found
6// in the file PATENTS.  All contributing project authors may
7// be found in the AUTHORS file in the root of the source tree.
8
9#ifndef MKVMUXERUTIL_HPP
10#define MKVMUXERUTIL_HPP
11
12#include "mkvmuxertypes.hpp"
13
14namespace mkvmuxer {
15
16class IMkvWriter;
17
18const uint64 kEbmlUnknownValue = 0x01FFFFFFFFFFFFFFULL;
19const int64 kMaxBlockTimecode = 0x07FFFLL;
20
21// Writes out |value| in Big Endian order. Returns 0 on success.
22int32 SerializeInt(IMkvWriter* writer, int64 value, int32 size);
23
24// Returns the size in bytes of the element.
25int32 GetUIntSize(uint64 value);
26int32 GetCodedUIntSize(uint64 value);
27uint64 EbmlMasterElementSize(uint64 type, uint64 value);
28uint64 EbmlElementSize(uint64 type, int64 value);
29uint64 EbmlElementSize(uint64 type, uint64 value);
30uint64 EbmlElementSize(uint64 type, float value);
31uint64 EbmlElementSize(uint64 type, const char* value);
32uint64 EbmlElementSize(uint64 type, const uint8* value, uint64 size);
33uint64 EbmlDateElementSize(uint64 type, int64 value);
34
35// Creates an EBML coded number from |value| and writes it out. The size of
36// the coded number is determined by the value of |value|. |value| must not
37// be in a coded form. Returns 0 on success.
38int32 WriteUInt(IMkvWriter* writer, uint64 value);
39
40// Creates an EBML coded number from |value| and writes it out. The size of
41// the coded number is determined by the value of |size|. |value| must not
42// be in a coded form. Returns 0 on success.
43int32 WriteUIntSize(IMkvWriter* writer, uint64 value, int32 size);
44
45// Output an Mkv master element. Returns true if the element was written.
46bool WriteEbmlMasterElement(IMkvWriter* writer, uint64 value, uint64 size);
47
48// Outputs an Mkv ID, calls |IMkvWriter::ElementStartNotify|, and passes the
49// ID to |SerializeInt|. Returns 0 on success.
50int32 WriteID(IMkvWriter* writer, uint64 type);
51
52// Output an Mkv non-master element. Returns true if the element was written.
53bool WriteEbmlElement(IMkvWriter* writer, uint64 type, uint64 value);
54bool WriteEbmlElement(IMkvWriter* writer, uint64 type, float value);
55bool WriteEbmlElement(IMkvWriter* writer, uint64 type, const char* value);
56bool WriteEbmlElement(IMkvWriter* writer, uint64 type, const uint8* value,
57                      uint64 size);
58bool WriteEbmlDateElement(IMkvWriter* writer, uint64 type, int64 value);
59
60// Output an Mkv Simple Block.
61// Inputs:
62//   data:         Pointer to the data.
63//   length:       Length of the data.
64//   track_number: Track to add the data to. Value returned by Add track
65//                  functions.  Only values in the range [1, 126] are
66//                  permitted.
67//   timecode:     Relative timecode of the Block.  Only values in the
68//                  range [0, 2^15) are permitted.
69//   is_key:       Non-zero value specifies that frame is a key frame.
70uint64 WriteSimpleBlock(IMkvWriter* writer, const uint8* data, uint64 length,
71                        uint64 track_number, int64 timecode, uint64 is_key);
72
73// Output a metadata keyframe, using a Block Group element.
74// Inputs:
75//   data:         Pointer to the (meta)data.
76//   length:       Length of the (meta)data.
77//   track_number: Track to add the data to. Value returned by Add track
78//                  functions.  Only values in the range [1, 126] are
79//                  permitted.
80//   timecode      Timecode of frame, relative to cluster timecode.  Only
81//                  values in the range [0, 2^15) are permitted.
82//   duration_timecode  Duration of frame, using timecode units.
83uint64 WriteMetadataBlock(IMkvWriter* writer, const uint8* data, uint64 length,
84                          uint64 track_number, int64 timecode,
85                          uint64 duration_timecode);
86
87// Output an Mkv Block with BlockAdditional data.
88// Inputs:
89//   data:         Pointer to the data.
90//   length:       Length of the data.
91//   additional:   Pointer to the additional data
92//   additional_length: Length of the additional data.
93//   add_id: Value of BlockAddID element.
94//   track_number: Track to add the data to. Value returned by Add track
95//                  functions.  Only values in the range [1, 126] are
96//                  permitted.
97//   timecode:     Relative timecode of the Block.  Only values in the
98//                  range [0, 2^15) are permitted.
99//   is_key:       Non-zero value specifies that frame is a key frame.
100uint64 WriteBlockWithAdditional(IMkvWriter* writer, const uint8* data,
101                                uint64 length, const uint8* additional,
102                                uint64 additional_length, uint64 add_id,
103                                uint64 track_number, int64 timecode,
104                                uint64 is_key);
105
106// Output an Mkv Block with a DiscardPadding element.
107// Inputs:
108//   data:            Pointer to the data.
109//   length:          Length of the data.
110//   discard_padding: DiscardPadding value.
111//   track_number:    Track to add the data to. Value returned by Add track
112//                    functions. Only values in the range [1, 126] are
113//                    permitted.
114//   timecode:        Relative timecode of the Block.  Only values in the
115//                    range [0, 2^15) are permitted.
116//   is_key:          Non-zero value specifies that frame is a key frame.
117uint64 WriteBlockWithDiscardPadding(IMkvWriter* writer, const uint8* data,
118                                    uint64 length, int64 discard_padding,
119                                    uint64 track_number, int64 timecode,
120                                    uint64 is_key);
121
122// Output a void element. |size| must be the entire size in bytes that will be
123// void. The function will calculate the size of the void header and subtract
124// it from |size|.
125uint64 WriteVoidElement(IMkvWriter* writer, uint64 size);
126
127// Returns the version number of the muxer in |major|, |minor|, |build|,
128// and |revision|.
129void GetVersion(int32* major, int32* minor, int32* build, int32* revision);
130
131// Returns a random number to be used for UID, using |seed| to seed
132// the random-number generator (see POSIX rand_r() for semantics).
133uint64 MakeUID(unsigned int* seed);
134
135}  // end namespace mkvmuxer
136
137#endif  // MKVMUXERUTIL_HPP
138