1 // Copyright (c) 2012 The WebM project authors. All Rights Reserved.
2 //
3 // Use of this source code is governed by a BSD-style license
4 // that can be found in the LICENSE file in the root of the source
5 // tree. An additional intellectual property rights grant can be found
6 // in the file PATENTS.  All contributing project authors may
7 // be found in the AUTHORS file in the root of the source tree.
8 #ifndef MKVMUXER_MKVMUXERUTIL_H_
9 #define MKVMUXER_MKVMUXERUTIL_H_
10 
11 #include "mkvmuxertypes.h"
12 
13 #include "stdint.h"
14 
15 namespace mkvmuxer {
16 class Cluster;
17 class Frame;
18 class IMkvWriter;
19 
20 // TODO(tomfinegan): mkvmuxer:: integer types continue to be used here because
21 // changing them causes pain for downstream projects. It would be nice if a
22 // solution that allows removal of the mkvmuxer:: integer types while avoiding
23 // pain for downstream users of libwebm. Considering that mkvmuxerutil.{cc,h}
24 // are really, for the great majority of cases, EBML size calculation and writer
25 // functions, perhaps a more EBML focused utility would be the way to go as a
26 // first step.
27 
28 const uint64 kEbmlUnknownValue = 0x01FFFFFFFFFFFFFFULL;
29 const int64 kMaxBlockTimecode = 0x07FFFLL;
30 
31 // Writes out |value| in Big Endian order. Returns 0 on success.
32 int32 SerializeInt(IMkvWriter* writer, int64 value, int32 size);
33 
34 // Writes out |f| in Big Endian order. Returns 0 on success.
35 int32 SerializeFloat(IMkvWriter* writer, float f);
36 
37 // Returns the size in bytes of the element.
38 int32 GetUIntSize(uint64 value);
39 int32 GetIntSize(int64 value);
40 int32 GetCodedUIntSize(uint64 value);
41 uint64 EbmlMasterElementSize(uint64 type, uint64 value);
42 uint64 EbmlElementSize(uint64 type, int64 value);
43 uint64 EbmlElementSize(uint64 type, uint64 value);
44 uint64 EbmlElementSize(uint64 type, float value);
45 uint64 EbmlElementSize(uint64 type, const char* value);
46 uint64 EbmlElementSize(uint64 type, const uint8* value, uint64 size);
47 uint64 EbmlDateElementSize(uint64 type);
48 
49 // Returns the size in bytes of the element assuming that the element was
50 // written using |fixed_size| bytes. If |fixed_size| is set to zero, then it
51 // computes the necessary number of bytes based on |value|.
52 uint64 EbmlElementSize(uint64 type, uint64 value, uint64 fixed_size);
53 
54 // Creates an EBML coded number from |value| and writes it out. The size of
55 // the coded number is determined by the value of |value|. |value| must not
56 // be in a coded form. Returns 0 on success.
57 int32 WriteUInt(IMkvWriter* writer, uint64 value);
58 
59 // Creates an EBML coded number from |value| and writes it out. The size of
60 // the coded number is determined by the value of |size|. |value| must not
61 // be in a coded form. Returns 0 on success.
62 int32 WriteUIntSize(IMkvWriter* writer, uint64 value, int32 size);
63 
64 // Output an Mkv master element. Returns true if the element was written.
65 bool WriteEbmlMasterElement(IMkvWriter* writer, uint64 value, uint64 size);
66 
67 // Outputs an Mkv ID, calls |IMkvWriter::ElementStartNotify|, and passes the
68 // ID to |SerializeInt|. Returns 0 on success.
69 int32 WriteID(IMkvWriter* writer, uint64 type);
70 
71 // Output an Mkv non-master element. Returns true if the element was written.
72 bool WriteEbmlElement(IMkvWriter* writer, uint64 type, uint64 value);
73 bool WriteEbmlElement(IMkvWriter* writer, uint64 type, int64 value);
74 bool WriteEbmlElement(IMkvWriter* writer, uint64 type, float value);
75 bool WriteEbmlElement(IMkvWriter* writer, uint64 type, const char* value);
76 bool WriteEbmlElement(IMkvWriter* writer, uint64 type, const uint8* value,
77                       uint64 size);
78 bool WriteEbmlDateElement(IMkvWriter* writer, uint64 type, int64 value);
79 
80 // Output an Mkv non-master element using fixed size. The element will be
81 // written out using exactly |fixed_size| bytes. If |fixed_size| is set to zero
82 // then it computes the necessary number of bytes based on |value|. Returns true
83 // if the element was written.
84 bool WriteEbmlElement(IMkvWriter* writer, uint64 type, uint64 value,
85                       uint64 fixed_size);
86 
87 // Output a Mkv Frame. It decides the correct element to write (Block vs
88 // SimpleBlock) based on the parameters of the Frame.
89 uint64 WriteFrame(IMkvWriter* writer, const Frame* const frame,
90                   Cluster* cluster);
91 
92 // Output a void element. |size| must be the entire size in bytes that will be
93 // void. The function will calculate the size of the void header and subtract
94 // it from |size|.
95 uint64 WriteVoidElement(IMkvWriter* writer, uint64 size);
96 
97 // Returns the version number of the muxer in |major|, |minor|, |build|,
98 // and |revision|.
99 void GetVersion(int32* major, int32* minor, int32* build, int32* revision);
100 
101 // Returns a random number to be used for UID, using |seed| to seed
102 // the random-number generator (see POSIX rand_r() for semantics).
103 uint64 MakeUID(unsigned int* seed);
104 
105 // Colour field validation helpers. All return true when |value| is valid.
106 bool IsMatrixCoefficientsValueValid(uint64_t value);
107 bool IsChromaSitingHorzValueValid(uint64_t value);
108 bool IsChromaSitingVertValueValid(uint64_t value);
109 bool IsColourRangeValueValid(uint64_t value);
110 bool IsTransferCharacteristicsValueValid(uint64_t value);
111 bool IsPrimariesValueValid(uint64_t value);
112 
113 }  // namespace mkvmuxer
114 
115 #endif  // MKVMUXER_MKVMUXERUTIL_H_
116