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