1 // Copyright (c) 2012 The WebM project authors. All Rights Reserved.
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.
9 #include "mkvmuxerutil.hpp"
27 #include "mkvwriter.hpp"
28 #include "webmids.hpp"
34 // Date elements are always 8 octets in size.
35 const int kDateElementSize = 8;
39 int32 GetCodedUIntSize(uint64 value) {
40 if (value < 0x000000000000007FULL)
42 else if (value < 0x0000000000003FFFULL)
44 else if (value < 0x00000000001FFFFFULL)
46 else if (value < 0x000000000FFFFFFFULL)
48 else if (value < 0x00000007FFFFFFFFULL)
50 else if (value < 0x000003FFFFFFFFFFULL)
52 else if (value < 0x0001FFFFFFFFFFFFULL)
57 int32 GetUIntSize(uint64 value) {
58 if (value < 0x0000000000000100ULL)
60 else if (value < 0x0000000000010000ULL)
62 else if (value < 0x0000000001000000ULL)
64 else if (value < 0x0000000100000000ULL)
66 else if (value < 0x0000010000000000ULL)
68 else if (value < 0x0001000000000000ULL)
70 else if (value < 0x0100000000000000ULL)
75 uint64 EbmlMasterElementSize(uint64 type, uint64 value) {
77 int32 ebml_size = GetUIntSize(type);
80 ebml_size += GetCodedUIntSize(value);
85 uint64 EbmlElementSize(uint64 type, int64 value) {
86 return EbmlElementSize(type, static_cast<uint64>(value));
89 uint64 EbmlElementSize(uint64 type, uint64 value) {
91 int32 ebml_size = GetUIntSize(type);
94 ebml_size += GetUIntSize(value);
102 uint64 EbmlElementSize(uint64 type, float /* value */ ) {
104 uint64 ebml_size = GetUIntSize(type);
107 ebml_size += sizeof(float);
115 uint64 EbmlElementSize(uint64 type, const char* value) {
120 uint64 ebml_size = GetUIntSize(type);
123 ebml_size += strlen(value);
131 uint64 EbmlElementSize(uint64 type, const uint8* value, uint64 size) {
136 uint64 ebml_size = GetUIntSize(type);
142 ebml_size += GetCodedUIntSize(size);
147 uint64 EbmlDateElementSize(uint64 type, int64 value) {
149 uint64 ebml_size = GetUIntSize(type);
152 ebml_size += kDateElementSize;
160 int32 SerializeInt(IMkvWriter* writer, int64 value, int32 size) {
161 if (!writer || size < 1 || size > 8)
164 for (int32 i = 1; i <= size; ++i) {
165 const int32 byte_count = size - i;
166 const int32 bit_count = byte_count * 8;
168 const int64 bb = value >> bit_count;
169 const uint8 b = static_cast<uint8>(bb);
171 const int32 status = writer->Write(&b, 1);
180 int32 SerializeFloat(IMkvWriter* writer, float f) {
184 assert(sizeof(uint32) == sizeof(float));
185 // This union is merely used to avoid a reinterpret_cast from float& to
186 // uint32& which will result in violation of strict aliasing.
193 for (int32 i = 1; i <= 4; ++i) {
194 const int32 byte_count = 4 - i;
195 const int32 bit_count = byte_count * 8;
197 const uint8 byte = static_cast<uint8>(value.u32 >> bit_count);
199 const int32 status = writer->Write(&byte, 1);
208 int32 WriteUInt(IMkvWriter* writer, uint64 value) {
212 int32 size = GetCodedUIntSize(value);
214 return WriteUIntSize(writer, value, size);
217 int32 WriteUIntSize(IMkvWriter* writer, uint64 value, int32 size) {
218 if (!writer || size < 0 || size > 8)
222 const uint64 bit = 1LL << (size * 7);
224 if (value > (bit - 2))
233 bit = 1LL << (size * 7);
234 const uint64 max = bit - 2;
248 return SerializeInt(writer, value, size);
251 int32 WriteID(IMkvWriter* writer, uint64 type) {
255 writer->ElementStartNotify(type, writer->Position());
257 const int32 size = GetUIntSize(type);
259 return SerializeInt(writer, type, size);
262 bool WriteEbmlMasterElement(IMkvWriter* writer, uint64 type, uint64 size) {
266 if (WriteID(writer, type))
269 if (WriteUInt(writer, size))
275 bool WriteEbmlElement(IMkvWriter* writer, uint64 type, uint64 value) {
279 if (WriteID(writer, type))
282 const uint64 size = GetUIntSize(value);
283 if (WriteUInt(writer, size))
286 if (SerializeInt(writer, value, static_cast<int32>(size)))
292 bool WriteEbmlElement(IMkvWriter* writer, uint64 type, float value) {
296 if (WriteID(writer, type))
299 if (WriteUInt(writer, 4))
302 if (SerializeFloat(writer, value))
308 bool WriteEbmlElement(IMkvWriter* writer, uint64 type, const char* value) {
309 if (!writer || !value)
312 if (WriteID(writer, type))
315 const uint64 length = strlen(value);
316 if (WriteUInt(writer, length))
319 if (writer->Write(value, static_cast<const uint32>(length)))
325 bool WriteEbmlElement(IMkvWriter* writer,
329 if (!writer || !value || size < 1)
332 if (WriteID(writer, type))
335 if (WriteUInt(writer, size))
338 if (writer->Write(value, static_cast<uint32>(size)))
344 bool WriteEbmlDateElement(IMkvWriter* writer, uint64 type, int64 value) {
348 if (WriteID(writer, type))
351 if (WriteUInt(writer, kDateElementSize))
354 if (SerializeInt(writer, value, kDateElementSize))
360 uint64 WriteSimpleBlock(IMkvWriter* writer,
369 if (!data || length < 1)
372 // Here we only permit track number values to be no greater than
373 // 126, which the largest value we can store having a Matroska
374 // integer representation of only 1 byte.
376 if (track_number < 1 || track_number > 126)
379 // Technically the timestamp for a block can be less than the
380 // timestamp for the cluster itself (remember that block timestamp
381 // is a signed, 16-bit integer). However, as a simplification we
382 // only permit non-negative cluster-relative timestamps for blocks.
384 if (timecode < 0 || timecode > kMaxBlockTimecode)
387 if (WriteID(writer, kMkvSimpleBlock))
390 const int32 size = static_cast<int32>(length) + 4;
391 if (WriteUInt(writer, size))
394 if (WriteUInt(writer, static_cast<uint64>(track_number)))
397 if (SerializeInt(writer, timecode, 2))
404 if (SerializeInt(writer, flags, 1))
407 if (writer->Write(data, static_cast<uint32>(length)))
410 const uint64 element_size =
411 GetUIntSize(kMkvSimpleBlock) + GetCodedUIntSize(size) + 4 + length;
416 // We must write the metadata (key)frame as a BlockGroup element,
417 // because we need to specify a duration for the frame. The
418 // BlockGroup element comprises the frame itself and its duration,
419 // and is laid out as follows:
425 // (the frame is the block payload)
428 // (duration payload)
430 uint64 WriteMetadataBlock(IMkvWriter* writer,
436 // We don't backtrack when writing to the stream, so we must
437 // pre-compute the BlockGroup size, by summing the sizes of each
438 // sub-element (the block and the duration).
440 // We use a single byte for the track number of the block, which
441 // means the block header is exactly 4 bytes.
443 // TODO(matthewjheaney): use EbmlMasterElementSize and WriteEbmlMasterElement
445 const uint64 block_payload_size = 4 + length;
446 const int32 block_size = GetCodedUIntSize(block_payload_size);
447 const uint64 block_elem_size = 1 + block_size + block_payload_size;
449 const int32 duration_payload_size = GetUIntSize(duration);
450 const int32 duration_size = GetCodedUIntSize(duration_payload_size);
451 const uint64 duration_elem_size = 1 + duration_size + duration_payload_size;
453 const uint64 blockg_payload_size = block_elem_size + duration_elem_size;
454 const int32 blockg_size = GetCodedUIntSize(blockg_payload_size);
455 const uint64 blockg_elem_size = 1 + blockg_size + blockg_payload_size;
457 if (WriteID(writer, kMkvBlockGroup)) // 1-byte ID size
460 if (WriteUInt(writer, blockg_payload_size))
463 // Write Block element
465 if (WriteID(writer, kMkvBlock)) // 1-byte ID size
468 if (WriteUInt(writer, block_payload_size))
473 if (WriteUInt(writer, track_number))
478 if (SerializeInt(writer, timecode, 2))
483 const uint64 flags = 0;
485 if (SerializeInt(writer, flags, 1))
488 // Now write the actual frame (of metadata)
490 if (writer->Write(data, static_cast<uint32>(length)))
493 // Write Duration element
495 if (WriteID(writer, kMkvBlockDuration)) // 1-byte ID size
498 if (WriteUInt(writer, duration_payload_size))
501 if (SerializeInt(writer, duration, duration_payload_size))
504 // Note that we don't write a reference time as part of the block
505 // group; no reference time(s) indicates that this block is a
506 // keyframe. (Unlike the case for a SimpleBlock element, the header
507 // bits of the Block sub-element of a BlockGroup element do not
508 // indicate keyframe status. The keyframe status is inferred from
509 // the absence of reference time sub-elements.)
511 return blockg_elem_size;
514 // Writes a WebM BlockGroup with BlockAdditional data. The structure is as
516 // Indentation shows sub-levels
526 uint64 WriteBlockWithAdditional(IMkvWriter* writer,
529 const uint8* additional,
530 uint64 additional_length,
535 if (!data || !additional || length < 1 || additional_length < 1)
538 const uint64 block_payload_size = 4 + length;
539 const uint64 block_elem_size = EbmlMasterElementSize(kMkvBlock,
540 block_payload_size) +
542 const uint64 block_additional_elem_size = EbmlElementSize(kMkvBlockAdditional,
545 const uint64 block_addid_elem_size = EbmlElementSize(kMkvBlockAddID, add_id);
547 const uint64 block_more_payload_size = block_addid_elem_size +
548 block_additional_elem_size;
549 const uint64 block_more_elem_size = EbmlMasterElementSize(
551 block_more_payload_size) +
552 block_more_payload_size;
553 const uint64 block_additions_payload_size = block_more_elem_size;
554 const uint64 block_additions_elem_size = EbmlMasterElementSize(
556 block_additions_payload_size) +
557 block_additions_payload_size;
558 const uint64 block_group_payload_size = block_elem_size +
559 block_additions_elem_size;
560 const uint64 block_group_elem_size = EbmlMasterElementSize(
562 block_group_payload_size) +
563 block_group_payload_size;
565 if (!WriteEbmlMasterElement(writer, kMkvBlockGroup,
566 block_group_payload_size))
569 if (!WriteEbmlMasterElement(writer, kMkvBlock, block_payload_size))
572 if (WriteUInt(writer, track_number))
575 if (SerializeInt(writer, timecode, 2))
581 if (SerializeInt(writer, flags, 1))
584 if (writer->Write(data, static_cast<uint32>(length)))
587 if (!WriteEbmlMasterElement(writer, kMkvBlockAdditions,
588 block_additions_payload_size))
591 if (!WriteEbmlMasterElement(writer, kMkvBlockMore, block_more_payload_size))
594 if (!WriteEbmlElement(writer, kMkvBlockAddID, add_id))
597 if (!WriteEbmlElement(writer, kMkvBlockAdditional,
598 additional, additional_length))
601 return block_group_elem_size;
604 // Writes a WebM BlockGroup with DiscardPadding. The structure is as follows:
605 // Indentation shows sub-levels
610 uint64 WriteBlockWithDiscardPadding(IMkvWriter* writer,
613 int64 discard_padding,
617 if (!data || length < 1 || discard_padding <= 0)
620 const uint64 block_payload_size = 4 + length;
621 const uint64 block_elem_size = EbmlMasterElementSize(kMkvBlock,
622 block_payload_size) +
624 const uint64 discard_padding_elem_size = EbmlElementSize(kMkvDiscardPadding,
626 const uint64 block_group_payload_size = block_elem_size +
627 discard_padding_elem_size;
628 const uint64 block_group_elem_size = EbmlMasterElementSize(
630 block_group_payload_size) +
631 block_group_payload_size;
633 if (!WriteEbmlMasterElement(writer, kMkvBlockGroup,
634 block_group_payload_size))
637 if (!WriteEbmlMasterElement(writer, kMkvBlock, block_payload_size))
640 if (WriteUInt(writer, track_number))
643 if (SerializeInt(writer, timecode, 2))
649 if (SerializeInt(writer, flags, 1))
652 if (writer->Write(data, static_cast<uint32>(length)))
655 if (WriteID(writer, kMkvDiscardPadding))
658 const uint64 size = GetUIntSize(discard_padding);
659 if (WriteUInt(writer, size))
662 if (SerializeInt(writer, discard_padding, static_cast<int32>(size)))
665 return block_group_elem_size;
668 uint64 WriteVoidElement(IMkvWriter* writer, uint64 size) {
672 // Subtract one for the void ID and the coded size.
673 uint64 void_entry_size = size - 1 - GetCodedUIntSize(size-1);
674 uint64 void_size = EbmlMasterElementSize(kMkvVoid, void_entry_size) +
677 if (void_size != size)
680 const int64 payload_position = writer->Position();
681 if (payload_position < 0)
684 if (WriteID(writer, kMkvVoid))
687 if (WriteUInt(writer, void_entry_size))
690 const uint8 value = 0;
691 for (int32 i = 0; i < static_cast<int32>(void_entry_size); ++i) {
692 if (writer->Write(&value, 1))
696 const int64 stop_position = writer->Position();
697 if (stop_position < 0 ||
698 stop_position - payload_position != static_cast<int64>(void_size))
704 void GetVersion(int32* major, int32* minor, int32* build, int32* revision) {
711 } // namespace mkvmuxer
713 mkvmuxer::uint64 mkvmuxer::MakeUID(unsigned int* seed) {
720 for (int i = 0; i < 7; ++i) { // avoid problems with 8-byte values
723 // TODO(fgalligan): Move random number generation to platform specific code.
726 unsigned int random_value;
727 const errno_t e = rand_s(&random_value);
729 const int32 nn = random_value;
732 int fd = open("/dev/urandom", O_RDONLY);
734 read(fd, &temp_num, sizeof(int32));
737 const int32 nn = temp_num;
738 #elif defined __MINGW32__
739 const int32 nn = rand();
741 const int32 nn = rand_r(seed);
743 const int32 n = 0xFF & (nn >> 4); // throw away low-order bits