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 // Returns the size in bytes of the element. 35 int32 GetUIntSize(uint64 value); 36 int32 GetIntSize(int64 value); 37 int32 GetCodedUIntSize(uint64 value); 38 uint64 EbmlMasterElementSize(uint64 type, uint64 value); 39 uint64 EbmlElementSize(uint64 type, int64 value); 40 uint64 EbmlElementSize(uint64 type, uint64 value); 41 uint64 EbmlElementSize(uint64 type, float value); 42 uint64 EbmlElementSize(uint64 type, const char* value); 43 uint64 EbmlElementSize(uint64 type, const uint8* value, uint64 size); 44 uint64 EbmlDateElementSize(uint64 type); 45 46 // Returns the size in bytes of the element assuming that the element was 47 // written using |fixed_size| bytes. If |fixed_size| is set to zero, then it 48 // computes the necessary number of bytes based on |value|. 49 uint64 EbmlElementSize(uint64 type, uint64 value, uint64 fixed_size); 50 51 // Creates an EBML coded number from |value| and writes it out. The size of 52 // the coded number is determined by the value of |value|. |value| must not 53 // be in a coded form. Returns 0 on success. 54 int32 WriteUInt(IMkvWriter* writer, uint64 value); 55 56 // Creates an EBML coded number from |value| and writes it out. The size of 57 // the coded number is determined by the value of |size|. |value| must not 58 // be in a coded form. Returns 0 on success. 59 int32 WriteUIntSize(IMkvWriter* writer, uint64 value, int32 size); 60 61 // Output an Mkv master element. Returns true if the element was written. 62 bool WriteEbmlMasterElement(IMkvWriter* writer, uint64 value, uint64 size); 63 64 // Outputs an Mkv ID, calls |IMkvWriter::ElementStartNotify|, and passes the 65 // ID to |SerializeInt|. Returns 0 on success. 66 int32 WriteID(IMkvWriter* writer, uint64 type); 67 68 // Output an Mkv non-master element. Returns true if the element was written. 69 bool WriteEbmlElement(IMkvWriter* writer, uint64 type, uint64 value); 70 bool WriteEbmlElement(IMkvWriter* writer, uint64 type, int64 value); 71 bool WriteEbmlElement(IMkvWriter* writer, uint64 type, float value); 72 bool WriteEbmlElement(IMkvWriter* writer, uint64 type, const char* value); 73 bool WriteEbmlElement(IMkvWriter* writer, uint64 type, const uint8* value, 74 uint64 size); 75 bool WriteEbmlDateElement(IMkvWriter* writer, uint64 type, int64 value); 76 77 // Output an Mkv non-master element using fixed size. The element will be 78 // written out using exactly |fixed_size| bytes. If |fixed_size| is set to zero 79 // then it computes the necessary number of bytes based on |value|. Returns true 80 // if the element was written. 81 bool WriteEbmlElement(IMkvWriter* writer, uint64 type, uint64 value, 82 uint64 fixed_size); 83 84 // Output a Mkv Frame. It decides the correct element to write (Block vs 85 // SimpleBlock) based on the parameters of the Frame. 86 uint64 WriteFrame(IMkvWriter* writer, const Frame* const frame, 87 Cluster* cluster); 88 89 // Output a void element. |size| must be the entire size in bytes that will be 90 // void. The function will calculate the size of the void header and subtract 91 // it from |size|. 92 uint64 WriteVoidElement(IMkvWriter* writer, uint64 size); 93 94 // Returns the version number of the muxer in |major|, |minor|, |build|, 95 // and |revision|. 96 void GetVersion(int32* major, int32* minor, int32* build, int32* revision); 97 98 // Returns a random number to be used for UID, using |seed| to seed 99 // the random-number generator (see POSIX rand_r() for semantics). 100 uint64 MakeUID(unsigned int* seed); 101 102 // Colour field validation helpers. All return true when |value| is valid. 103 bool IsMatrixCoefficientsValueValid(uint64_t value); 104 bool IsChromaSitingHorzValueValid(uint64_t value); 105 bool IsChromaSitingVertValueValid(uint64_t value); 106 bool IsColourRangeValueValid(uint64_t value); 107 bool IsTransferCharacteristicsValueValid(uint64_t value); 108 bool IsPrimariesValueValid(uint64_t value); 109 110 } // namespace mkvmuxer 111 112 #endif // MKVMUXER_MKVMUXERUTIL_H_ 113