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