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, uint64 type, const uint8* value,
327 if (!writer || !value || size < 1)
330 if (WriteID(writer, type))
333 if (WriteUInt(writer, size))
336 if (writer->Write(value, static_cast<uint32>(size)))
342 bool WriteEbmlDateElement(IMkvWriter* writer, uint64 type, int64 value) {
346 if (WriteID(writer, type))
349 if (WriteUInt(writer, kDateElementSize))
352 if (SerializeInt(writer, value, kDateElementSize))
358 uint64 WriteSimpleBlock(IMkvWriter* writer, const uint8* data, uint64 length,
359 uint64 track_number, int64 timecode, uint64 is_key) {
363 if (!data || length < 1)
366 // Here we only permit track number values to be no greater than
367 // 126, which the largest value we can store having a Matroska
368 // integer representation of only 1 byte.
370 if (track_number < 1 || track_number > 126)
373 // Technically the timestamp for a block can be less than the
374 // timestamp for the cluster itself (remember that block timestamp
375 // is a signed, 16-bit integer). However, as a simplification we
376 // only permit non-negative cluster-relative timestamps for blocks.
378 if (timecode < 0 || timecode > kMaxBlockTimecode)
381 if (WriteID(writer, kMkvSimpleBlock))
384 const int32 size = static_cast<int32>(length) + 4;
385 if (WriteUInt(writer, size))
388 if (WriteUInt(writer, static_cast<uint64>(track_number)))
391 if (SerializeInt(writer, timecode, 2))
398 if (SerializeInt(writer, flags, 1))
401 if (writer->Write(data, static_cast<uint32>(length)))
404 const uint64 element_size =
405 GetUIntSize(kMkvSimpleBlock) + GetCodedUIntSize(size) + 4 + length;
410 // We must write the metadata (key)frame as a BlockGroup element,
411 // because we need to specify a duration for the frame. The
412 // BlockGroup element comprises the frame itself and its duration,
413 // and is laid out as follows:
419 // (the frame is the block payload)
422 // (duration payload)
424 uint64 WriteMetadataBlock(IMkvWriter* writer, const uint8* data, uint64 length,
425 uint64 track_number, int64 timecode,
427 // We don't backtrack when writing to the stream, so we must
428 // pre-compute the BlockGroup size, by summing the sizes of each
429 // sub-element (the block and the duration).
431 // We use a single byte for the track number of the block, which
432 // means the block header is exactly 4 bytes.
434 // TODO(matthewjheaney): use EbmlMasterElementSize and WriteEbmlMasterElement
436 const uint64 block_payload_size = 4 + length;
437 const int32 block_size = GetCodedUIntSize(block_payload_size);
438 const uint64 block_elem_size = 1 + block_size + block_payload_size;
440 const int32 duration_payload_size = GetUIntSize(duration);
441 const int32 duration_size = GetCodedUIntSize(duration_payload_size);
442 const uint64 duration_elem_size = 1 + duration_size + duration_payload_size;
444 const uint64 blockg_payload_size = block_elem_size + duration_elem_size;
445 const int32 blockg_size = GetCodedUIntSize(blockg_payload_size);
446 const uint64 blockg_elem_size = 1 + blockg_size + blockg_payload_size;
448 if (WriteID(writer, kMkvBlockGroup)) // 1-byte ID size
451 if (WriteUInt(writer, blockg_payload_size))
454 // Write Block element
456 if (WriteID(writer, kMkvBlock)) // 1-byte ID size
459 if (WriteUInt(writer, block_payload_size))
464 if (WriteUInt(writer, track_number))
469 if (SerializeInt(writer, timecode, 2))
474 const uint64 flags = 0;
476 if (SerializeInt(writer, flags, 1))
479 // Now write the actual frame (of metadata)
481 if (writer->Write(data, static_cast<uint32>(length)))
484 // Write Duration element
486 if (WriteID(writer, kMkvBlockDuration)) // 1-byte ID size
489 if (WriteUInt(writer, duration_payload_size))
492 if (SerializeInt(writer, duration, duration_payload_size))
495 // Note that we don't write a reference time as part of the block
496 // group; no reference time(s) indicates that this block is a
497 // keyframe. (Unlike the case for a SimpleBlock element, the header
498 // bits of the Block sub-element of a BlockGroup element do not
499 // indicate keyframe status. The keyframe status is inferred from
500 // the absence of reference time sub-elements.)
502 return blockg_elem_size;
505 // Writes a WebM BlockGroup with BlockAdditional data. The structure is as
507 // Indentation shows sub-levels
517 uint64 WriteBlockWithAdditional(IMkvWriter* writer, const uint8* data,
518 uint64 length, const uint8* additional,
519 uint64 additional_length, uint64 add_id,
520 uint64 track_number, int64 timecode,
522 if (!data || !additional || length < 1 || additional_length < 1)
525 const uint64 block_payload_size = 4 + length;
526 const uint64 block_elem_size =
527 EbmlMasterElementSize(kMkvBlock, block_payload_size) + block_payload_size;
528 const uint64 block_additional_elem_size =
529 EbmlElementSize(kMkvBlockAdditional, additional, additional_length);
530 const uint64 block_addid_elem_size = EbmlElementSize(kMkvBlockAddID, add_id);
532 const uint64 block_more_payload_size =
533 block_addid_elem_size + block_additional_elem_size;
534 const uint64 block_more_elem_size =
535 EbmlMasterElementSize(kMkvBlockMore, block_more_payload_size) +
536 block_more_payload_size;
537 const uint64 block_additions_payload_size = block_more_elem_size;
538 const uint64 block_additions_elem_size =
539 EbmlMasterElementSize(kMkvBlockAdditions, block_additions_payload_size) +
540 block_additions_payload_size;
541 const uint64 block_group_payload_size =
542 block_elem_size + block_additions_elem_size;
543 const uint64 block_group_elem_size =
544 EbmlMasterElementSize(kMkvBlockGroup, block_group_payload_size) +
545 block_group_payload_size;
547 if (!WriteEbmlMasterElement(writer, kMkvBlockGroup, block_group_payload_size))
550 if (!WriteEbmlMasterElement(writer, kMkvBlock, block_payload_size))
553 if (WriteUInt(writer, track_number))
556 if (SerializeInt(writer, timecode, 2))
562 if (SerializeInt(writer, flags, 1))
565 if (writer->Write(data, static_cast<uint32>(length)))
568 if (!WriteEbmlMasterElement(writer, kMkvBlockAdditions,
569 block_additions_payload_size))
572 if (!WriteEbmlMasterElement(writer, kMkvBlockMore, block_more_payload_size))
575 if (!WriteEbmlElement(writer, kMkvBlockAddID, add_id))
578 if (!WriteEbmlElement(writer, kMkvBlockAdditional, additional,
582 return block_group_elem_size;
585 // Writes a WebM BlockGroup with DiscardPadding. The structure is as follows:
586 // Indentation shows sub-levels
591 uint64 WriteBlockWithDiscardPadding(IMkvWriter* writer, const uint8* data,
592 uint64 length, int64 discard_padding,
593 uint64 track_number, int64 timecode,
595 if (!data || length < 1 || discard_padding <= 0)
598 const uint64 block_payload_size = 4 + length;
599 const uint64 block_elem_size =
600 EbmlMasterElementSize(kMkvBlock, block_payload_size) + block_payload_size;
601 const uint64 discard_padding_elem_size =
602 EbmlElementSize(kMkvDiscardPadding, discard_padding);
603 const uint64 block_group_payload_size =
604 block_elem_size + discard_padding_elem_size;
605 const uint64 block_group_elem_size =
606 EbmlMasterElementSize(kMkvBlockGroup, block_group_payload_size) +
607 block_group_payload_size;
609 if (!WriteEbmlMasterElement(writer, kMkvBlockGroup, block_group_payload_size))
612 if (!WriteEbmlMasterElement(writer, kMkvBlock, block_payload_size))
615 if (WriteUInt(writer, track_number))
618 if (SerializeInt(writer, timecode, 2))
624 if (SerializeInt(writer, flags, 1))
627 if (writer->Write(data, static_cast<uint32>(length)))
630 if (WriteID(writer, kMkvDiscardPadding))
633 const uint64 size = GetUIntSize(discard_padding);
634 if (WriteUInt(writer, size))
637 if (SerializeInt(writer, discard_padding, static_cast<int32>(size)))
640 return block_group_elem_size;
643 uint64 WriteVoidElement(IMkvWriter* writer, uint64 size) {
647 // Subtract one for the void ID and the coded size.
648 uint64 void_entry_size = size - 1 - GetCodedUIntSize(size - 1);
650 EbmlMasterElementSize(kMkvVoid, void_entry_size) + void_entry_size;
652 if (void_size != size)
655 const int64 payload_position = writer->Position();
656 if (payload_position < 0)
659 if (WriteID(writer, kMkvVoid))
662 if (WriteUInt(writer, void_entry_size))
665 const uint8 value = 0;
666 for (int32 i = 0; i < static_cast<int32>(void_entry_size); ++i) {
667 if (writer->Write(&value, 1))
671 const int64 stop_position = writer->Position();
672 if (stop_position < 0 ||
673 stop_position - payload_position != static_cast<int64>(void_size))
679 void GetVersion(int32* major, int32* minor, int32* build, int32* revision) {
686 } // namespace mkvmuxer
688 mkvmuxer::uint64 mkvmuxer::MakeUID(unsigned int* seed) {
695 for (int i = 0; i < 7; ++i) { // avoid problems with 8-byte values
698 // TODO(fgalligan): Move random number generation to platform specific code.
701 unsigned int random_value;
702 const errno_t e = rand_s(&random_value);
704 const int32 nn = random_value;
707 int fd = open("/dev/urandom", O_RDONLY);
709 read(fd, &temp_num, sizeof(int32));
712 const int32 nn = temp_num;
713 #elif defined __MINGW32__
714 const int32 nn = rand();
716 const int32 nn = rand_r(seed);
718 const int32 n = 0xFF & (nn >> 4); // throw away low-order bits