31b975713c
eb50da8 Option to write timecode using fixed # of bytes c1991fe mkvmuxer: Add missing Segment member initializer. 10aed96 Android.mk: Make libwebm easier to build downstream. 5c50e31 Add support for parsing VPx track codec private data. 4cbdbf1 Fix Android build. bb48a3f mkvmuxerutil: remove stray 'int32' a1cba34 Support cross compile for windows via mingw64. 596f5e0 Add webm_info. ccf75f6 msvc/muxer_tests: Silence integer conversion warnings. 2ff2954 msvc/webm2pes: Silence integer and floating point conversion warnings. 1f24323 msvc/hdr_util: Silence double to float conversion warnings. 0744563 msvc/vpxpes_parser: Silence integer conversion warning. 59614b8 msvc/libwebm_util: Fix floating point to int conversion warning. 6481c24 webvtt: Fix include in vttreader. e6ed0f4 msvc/vpxpes2ts: Fix MSVC integer conversion warning. da64396 cmake/msvc: Disable C4996 project wide. 6ef8264 Merge "mkvparser::BlockEntry: inline EOS()" 3fa6aec mkvparser::BlockEntry: inline EOS() 26306f9 mkvmuxer: Remove unused Cluster ctor overload. 0d76597 mkvmuxer: Fix build with GCC 5.3. 0ba80bc mkvparser/sample: Minor clean up. 2e0e906 iosbuild.sh: Fix build. 918440a Makefile.unix: allow CXXFLAGS to be easily overridden 4ff5785 cmake: Add C++11 move ctor and member initializer tests. 402ef4d cmake: remove argc and argv from C++11 test main fns. cbe5c40 Restore original namespaces for mkvmuxer and mkvparser. 504e0f2 Mass file extension update. 79cb980 Android.mk: Update source file locations. 01db4c2 webmids: Move to common/ sub dir. 235ce59 mkvparser: Explicitly reference internal sources in includes. f578419 mkvmuxer: Move sources to mkvmuxer/ sub dir. 5f1065e webvtt: Organize and clean up webvtt support. 7abe8ac cmake: Add missing dumpvtt target. f2f87e2 Makefile.unix: Tidy things up. 12f6dc3 Use <stdint.h> types instead of custom typedefs. 0407360 mkvmuxer: Write last block in each Cluster with Duration 008aa63 mkvparser: move to mkvparser sub dir. e64bf75 Namespace reorg: Make everything a child of libwebm. 5fdb386 cmake: move c++11 checks into build/cxx11_tests.cmake. 3672488 Copy reference block values in Frame::CopyFrom() 91ca780 reapply clang-format 8d34215 Merge "Clean up AddAudioTrack in muxer_tests" 90861d4 Clean up AddAudioTrack in muxer_tests a9dfb3d Un-ignore webm files in testdata c5b76d8 Extract PES parser from WebM2Pes tests. 16524e8 cmake: Add include-what-you-use integration. 7015af5 iwyu/vpxpes2ts: Update includes. c1d6a70 iwyu/webm2pes: Update includes. 110e797 iwyu/libwebm_util: Update includes. 44e31fb iwyu/webm2pes_tests: Update includes. d919f96 iwyu/mkvwriter: Update includes. 75790e1 iwyu/mkvparser: Update includes. 5f673ca iwyu/webm2pes_main: Update includes. 747244a iwyu/vpxpes2ts_main: Update includes. 94c985f iwyu/mkvmuxerutil: Update includes. c365630 iwyu/mkvmuxer: Update includes. b15b8ef iwyu/file_util: Update includes. 3dfba95 iwyu/hdr_util: Update includes. baba8b1 iwyu/vttdemux: Update includes. 3212ec1 iwyu/webvttparser: Update includes. b6d8d92 iwyu/sample_muxer_metadata: Update includes. a9a1a01 iwyu/sample_muxer: Update includes. e020ffd iwyu/sample: Update includes. 18834bc iwyu/parser_tests: Update includes. 9c00ae3 iwyu/muxer_tests: Update includes. 41a17eb iwyu/test_util: Update includes b6174be muxer_tests: Fix windows brokenness. e092515 file_util: Remove tmpnam() usage in MSVC. b9dc4ac test_util: Don't pass NULL to std::string() in GetTestDataDir(). 1f74651 webmts: Move PES/TS sources to m2ts sub directory. 1b895e9 Rename libwebm_utils to libwebm_util. 2fabcd3 sample_muxer: Replace std::tmpnam() with libwebm::GetTempFileName(). e6a0033 Add file_util. 87f9bea Move hdr_util to common. 1f64aaf cmake: Expand C++11 tests. 6dc81c1 muxer_tests: Die immediately when unable to prep for file writing. 521ce4d webm2pes: Fix type limit warning. 64c4163 vpxpes2ts: Fix sign-compare and type-limits warnings. 741ba68 muxer_tests: Replace std::tmpnam() with GetTempFileName(). 6159e83 Merge "test_util: add missing include for close()" ff81c74 parser_tests: Fix sign compare warnings. 163f57d test_util: add missing include for close() 7c89eb5 Merge "test_util: Remove tmpnam() usage on non-MSVC targets." c4b8686 Merge "webm2pes_tests: Fix sign compare warnings." 9c9f546 Merge "muxer_tests: Fix sign compare warnings." 0fbefef webm2pes: Silence sign compare warnings. 599e4e8 cmake: Silence clang/gcc deprecation warnings. 82f376f test_util: Remove tmpnam() usage on non-MSVC targets. 4d31d6b webm2pes_tests: Fix sign compare warnings. 07ed7e0 muxer_tests: Fix sign compare warnings. ae2fbfe parser_tests: Silence sign compare warning. f488528 libwebm_utils: Silence sign compare warning. 777247b Add C++11 detection to cmake file. 9b89187 Add missing include to libwebm_utils.h. 421874a Merge "mkvmuxer: Fix GCC build." dd6ab35 Set the mastering metadata on the muxers colour 8b61ef5 mkvmuxer: Fix GCC build. 353b050 Add hdr_util. c92e080 mkvmuxer: Use kValueNotPresent in Colour/MasteringMetadata. 2d09128 Colour element: TransferFunction renamed to TransferCharacteristics. f2fc28e Colour element: Matrix renamed to MatrixCoefficients. e0b1135 cmake: Minor CMakeLists.txt refactor. 1e1872b Revert change from auto_ptr to unique_ptr in sample code. d7fc382 Track updates to the proposed Matroska Colour spec. 99981ee sample(mkvparser): Output Colour element when present. 375e416 mkvmuxer: Fix Colour element support. eaeca34 mkvmuxer: Fix bits per channel in the colour element. 1dab7f3 mkvparser: Avoid crash when encountering a Colour element. a1517aa sample_muxer: copy the Colour element. ea9dd94 Merge "webm2pes: Fix tests." 8635c5b Merge "mkvparser: Make omitted values detectable in the Colour element." ae4ae7e mkvparser: Make omitted values detectable in the Colour element. 8c8cba6 webm2pes: Fix tests. a281a22 mkvmuxer: Add support for the Colour element and its children. 41a9147 sample_muxer: clang-format include order fix. 939a64d Signal E_BUFFER_NOT_FULL in EBMLHeader::Parse fb1406e mkvparser: Add support for the Colour element and its children. 22bfdf7 Merge "parser_tests: Add validation of cues." b873000 parser_tests: Add validation of cues. 799891e Update .gitignore to include some new binaries e051c60 Merge "Update muxer test gold files" b81d5f0 Update muxer test gold files 48b1e9a mkvparser: clang format run 93c4690 webm2pes: Add PES packet parsing tests. 65ca38f Merge "test_util: Fix gcc build." 520ca6c Merge "parser_tests: Fix gcc build." 37a38ca test_util: Fix gcc build. ee0ebba parser_tests: Fix gcc build. c32f970 Replace auto_ptr usage with unique_ptr. e569ab0 webm2pes/ts: Fix gcc build. 2e55d6c Merge "add bitcode embedding support for ios" 0cfb2dc add bitcode embedding support for ios bb8cefd webm2ts: Converts WebM VPx video to a MPEG TS. 453bf44 webm2pes: Begin addition of tests. 9299bbb libwebm: Googletest integration. 3bec1ba Merge changes I7bcb5b3e,I8ce733be,I98a928ff,I71910f24 5c83bbe Fix ParseElementHeader to support 0 payload elements be35869 libwebm_utils: Add FileDeleter. d6db1e1 webm2pes: Add a WebM parser init method. aa3593e webm2pes: Rename Convert to ConvertToFile(). e8fca12 webm2pes: Fix super frame splitting. 3cb96b6 webm2pes: Move main() and helper functions into their own files. 021432b webm2pes: Fix the linux build. 82ac5fc Remove RELEASE.TXT. 852e173 webm2pes: Split super frames and packetize large frames. faf85c2 webm2pes: Refactor header/optional header writing. 7c19266 Add Webm2Pes. 01fdee4 mkvmuxer: Disallow AddTrack() after Tracks element is output. 1ad314e mkvparser: EBMLHeader::Parse: remove dead init Change-Id: I13637ceacab953786064b7f8f6ecf1495b7475d0
650 lines
16 KiB
C++
650 lines
16 KiB
C++
// Copyright (c) 2012 The WebM project authors. All Rights Reserved.
|
|
//
|
|
// Use of this source code is governed by a BSD-style license
|
|
// that can be found in the LICENSE file in the root of the source
|
|
// tree. An additional intellectual property rights grant can be found
|
|
// in the file PATENTS. All contributing project authors may
|
|
// be found in the AUTHORS file in the root of the source tree.
|
|
|
|
#include "mkvmuxer/mkvmuxerutil.h"
|
|
|
|
#ifdef __ANDROID__
|
|
#include <fcntl.h>
|
|
#endif
|
|
|
|
#include <cassert>
|
|
#include <cmath>
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <cstring>
|
|
#include <ctime>
|
|
#include <new>
|
|
|
|
#include "common/webmids.h"
|
|
#include "mkvmuxer/mkvmuxer.h"
|
|
#include "mkvmuxer/mkvwriter.h"
|
|
|
|
namespace mkvmuxer {
|
|
|
|
namespace {
|
|
|
|
// Date elements are always 8 octets in size.
|
|
const int kDateElementSize = 8;
|
|
|
|
uint64_t WriteBlock(IMkvWriter* writer, const Frame* const frame,
|
|
int64_t timecode, uint64_t timecode_scale) {
|
|
uint64_t block_additional_elem_size = 0;
|
|
uint64_t block_addid_elem_size = 0;
|
|
uint64_t block_more_payload_size = 0;
|
|
uint64_t block_more_elem_size = 0;
|
|
uint64_t block_additions_payload_size = 0;
|
|
uint64_t block_additions_elem_size = 0;
|
|
if (frame->additional()) {
|
|
block_additional_elem_size =
|
|
EbmlElementSize(libwebm::kMkvBlockAdditional, frame->additional(),
|
|
frame->additional_length());
|
|
block_addid_elem_size =
|
|
EbmlElementSize(libwebm::kMkvBlockAddID, frame->add_id());
|
|
|
|
block_more_payload_size =
|
|
block_addid_elem_size + block_additional_elem_size;
|
|
block_more_elem_size =
|
|
EbmlMasterElementSize(libwebm::kMkvBlockMore, block_more_payload_size) +
|
|
block_more_payload_size;
|
|
block_additions_payload_size = block_more_elem_size;
|
|
block_additions_elem_size =
|
|
EbmlMasterElementSize(libwebm::kMkvBlockAdditions,
|
|
block_additions_payload_size) +
|
|
block_additions_payload_size;
|
|
}
|
|
|
|
uint64_t discard_padding_elem_size = 0;
|
|
if (frame->discard_padding() != 0) {
|
|
discard_padding_elem_size =
|
|
EbmlElementSize(libwebm::kMkvDiscardPadding, frame->discard_padding());
|
|
}
|
|
|
|
const uint64_t reference_block_timestamp =
|
|
frame->reference_block_timestamp() / timecode_scale;
|
|
uint64_t reference_block_elem_size = 0;
|
|
if (!frame->is_key()) {
|
|
reference_block_elem_size =
|
|
EbmlElementSize(libwebm::kMkvReferenceBlock, reference_block_timestamp);
|
|
}
|
|
|
|
const uint64_t duration = frame->duration() / timecode_scale;
|
|
uint64_t block_duration_elem_size = 0;
|
|
if (duration > 0)
|
|
block_duration_elem_size =
|
|
EbmlElementSize(libwebm::kMkvBlockDuration, duration);
|
|
|
|
const uint64_t block_payload_size = 4 + frame->length();
|
|
const uint64_t block_elem_size =
|
|
EbmlMasterElementSize(libwebm::kMkvBlock, block_payload_size) +
|
|
block_payload_size;
|
|
|
|
const uint64_t block_group_payload_size =
|
|
block_elem_size + block_additions_elem_size + block_duration_elem_size +
|
|
discard_padding_elem_size + reference_block_elem_size;
|
|
|
|
if (!WriteEbmlMasterElement(writer, libwebm::kMkvBlockGroup,
|
|
block_group_payload_size)) {
|
|
return 0;
|
|
}
|
|
|
|
if (!WriteEbmlMasterElement(writer, libwebm::kMkvBlock, block_payload_size))
|
|
return 0;
|
|
|
|
if (WriteUInt(writer, frame->track_number()))
|
|
return 0;
|
|
|
|
if (SerializeInt(writer, timecode, 2))
|
|
return 0;
|
|
|
|
// For a Block, flags is always 0.
|
|
if (SerializeInt(writer, 0, 1))
|
|
return 0;
|
|
|
|
if (writer->Write(frame->frame(), static_cast<uint32_t>(frame->length())))
|
|
return 0;
|
|
|
|
if (frame->additional()) {
|
|
if (!WriteEbmlMasterElement(writer, libwebm::kMkvBlockAdditions,
|
|
block_additions_payload_size)) {
|
|
return 0;
|
|
}
|
|
|
|
if (!WriteEbmlMasterElement(writer, libwebm::kMkvBlockMore,
|
|
block_more_payload_size))
|
|
return 0;
|
|
|
|
if (!WriteEbmlElement(writer, libwebm::kMkvBlockAddID, frame->add_id()))
|
|
return 0;
|
|
|
|
if (!WriteEbmlElement(writer, libwebm::kMkvBlockAdditional,
|
|
frame->additional(), frame->additional_length())) {
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
if (frame->discard_padding() != 0 &&
|
|
!WriteEbmlElement(writer, libwebm::kMkvDiscardPadding,
|
|
frame->discard_padding())) {
|
|
return false;
|
|
}
|
|
|
|
if (!frame->is_key() &&
|
|
!WriteEbmlElement(writer, libwebm::kMkvReferenceBlock,
|
|
reference_block_timestamp)) {
|
|
return false;
|
|
}
|
|
|
|
if (duration > 0 &&
|
|
!WriteEbmlElement(writer, libwebm::kMkvBlockDuration, duration)) {
|
|
return false;
|
|
}
|
|
return EbmlMasterElementSize(libwebm::kMkvBlockGroup,
|
|
block_group_payload_size) +
|
|
block_group_payload_size;
|
|
}
|
|
|
|
uint64_t WriteSimpleBlock(IMkvWriter* writer, const Frame* const frame,
|
|
int64_t timecode) {
|
|
if (WriteID(writer, libwebm::kMkvSimpleBlock))
|
|
return 0;
|
|
|
|
const int32_t size = static_cast<int32_t>(frame->length()) + 4;
|
|
if (WriteUInt(writer, size))
|
|
return 0;
|
|
|
|
if (WriteUInt(writer, static_cast<uint64_t>(frame->track_number())))
|
|
return 0;
|
|
|
|
if (SerializeInt(writer, timecode, 2))
|
|
return 0;
|
|
|
|
uint64_t flags = 0;
|
|
if (frame->is_key())
|
|
flags |= 0x80;
|
|
|
|
if (SerializeInt(writer, flags, 1))
|
|
return 0;
|
|
|
|
if (writer->Write(frame->frame(), static_cast<uint32_t>(frame->length())))
|
|
return 0;
|
|
|
|
return GetUIntSize(libwebm::kMkvSimpleBlock) + GetCodedUIntSize(size) + 4 +
|
|
frame->length();
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int32_t GetCodedUIntSize(uint64_t value) {
|
|
if (value < 0x000000000000007FULL)
|
|
return 1;
|
|
else if (value < 0x0000000000003FFFULL)
|
|
return 2;
|
|
else if (value < 0x00000000001FFFFFULL)
|
|
return 3;
|
|
else if (value < 0x000000000FFFFFFFULL)
|
|
return 4;
|
|
else if (value < 0x00000007FFFFFFFFULL)
|
|
return 5;
|
|
else if (value < 0x000003FFFFFFFFFFULL)
|
|
return 6;
|
|
else if (value < 0x0001FFFFFFFFFFFFULL)
|
|
return 7;
|
|
return 8;
|
|
}
|
|
|
|
int32_t GetUIntSize(uint64_t value) {
|
|
if (value < 0x0000000000000100ULL)
|
|
return 1;
|
|
else if (value < 0x0000000000010000ULL)
|
|
return 2;
|
|
else if (value < 0x0000000001000000ULL)
|
|
return 3;
|
|
else if (value < 0x0000000100000000ULL)
|
|
return 4;
|
|
else if (value < 0x0000010000000000ULL)
|
|
return 5;
|
|
else if (value < 0x0001000000000000ULL)
|
|
return 6;
|
|
else if (value < 0x0100000000000000ULL)
|
|
return 7;
|
|
return 8;
|
|
}
|
|
|
|
int32_t GetIntSize(int64_t value) {
|
|
// Doubling the requested value ensures positive values with their high bit
|
|
// set are written with 0-padding to avoid flipping the signedness.
|
|
const uint64_t v = (value < 0) ? value ^ -1LL : value;
|
|
return GetUIntSize(2 * v);
|
|
}
|
|
|
|
uint64_t EbmlMasterElementSize(uint64_t type, uint64_t value) {
|
|
// Size of EBML ID
|
|
int32_t ebml_size = GetUIntSize(type);
|
|
|
|
// Datasize
|
|
ebml_size += GetCodedUIntSize(value);
|
|
|
|
return ebml_size;
|
|
}
|
|
|
|
uint64_t EbmlElementSize(uint64_t type, int64_t value) {
|
|
// Size of EBML ID
|
|
int32_t ebml_size = GetUIntSize(type);
|
|
|
|
// Datasize
|
|
ebml_size += GetIntSize(value);
|
|
|
|
// Size of Datasize
|
|
ebml_size++;
|
|
|
|
return ebml_size;
|
|
}
|
|
|
|
uint64_t EbmlElementSize(uint64_t type, uint64_t value) {
|
|
return EbmlElementSize(type, value, 0);
|
|
}
|
|
|
|
uint64_t EbmlElementSize(uint64_t type, uint64_t value, uint64_t fixed_size) {
|
|
// Size of EBML ID
|
|
int32_t ebml_size = GetUIntSize(type);
|
|
|
|
// Datasize
|
|
ebml_size += (fixed_size > 0) ? fixed_size : GetUIntSize(value);
|
|
|
|
// Size of Datasize
|
|
ebml_size++;
|
|
|
|
return ebml_size;
|
|
}
|
|
|
|
uint64_t EbmlElementSize(uint64_t type, float /* value */) {
|
|
// Size of EBML ID
|
|
uint64_t ebml_size = GetUIntSize(type);
|
|
|
|
// Datasize
|
|
ebml_size += sizeof(float);
|
|
|
|
// Size of Datasize
|
|
ebml_size++;
|
|
|
|
return ebml_size;
|
|
}
|
|
|
|
uint64_t EbmlElementSize(uint64_t type, const char* value) {
|
|
if (!value)
|
|
return 0;
|
|
|
|
// Size of EBML ID
|
|
uint64_t ebml_size = GetUIntSize(type);
|
|
|
|
// Datasize
|
|
ebml_size += strlen(value);
|
|
|
|
// Size of Datasize
|
|
ebml_size++;
|
|
|
|
return ebml_size;
|
|
}
|
|
|
|
uint64_t EbmlElementSize(uint64_t type, const uint8_t* value, uint64_t size) {
|
|
if (!value)
|
|
return 0;
|
|
|
|
// Size of EBML ID
|
|
uint64_t ebml_size = GetUIntSize(type);
|
|
|
|
// Datasize
|
|
ebml_size += size;
|
|
|
|
// Size of Datasize
|
|
ebml_size += GetCodedUIntSize(size);
|
|
|
|
return ebml_size;
|
|
}
|
|
|
|
uint64_t EbmlDateElementSize(uint64_t type) {
|
|
// Size of EBML ID
|
|
uint64_t ebml_size = GetUIntSize(type);
|
|
|
|
// Datasize
|
|
ebml_size += kDateElementSize;
|
|
|
|
// Size of Datasize
|
|
ebml_size++;
|
|
|
|
return ebml_size;
|
|
}
|
|
|
|
int32_t SerializeInt(IMkvWriter* writer, int64_t value, int32_t size) {
|
|
if (!writer || size < 1 || size > 8)
|
|
return -1;
|
|
|
|
for (int32_t i = 1; i <= size; ++i) {
|
|
const int32_t byte_count = size - i;
|
|
const int32_t bit_count = byte_count * 8;
|
|
|
|
const int64_t bb = value >> bit_count;
|
|
const uint8_t b = static_cast<uint8_t>(bb);
|
|
|
|
const int32_t status = writer->Write(&b, 1);
|
|
|
|
if (status < 0)
|
|
return status;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
int32_t SerializeFloat(IMkvWriter* writer, float f) {
|
|
if (!writer)
|
|
return -1;
|
|
|
|
assert(sizeof(uint32_t) == sizeof(float));
|
|
// This union is merely used to avoid a reinterpret_cast from float& to
|
|
// uint32& which will result in violation of strict aliasing.
|
|
union U32 {
|
|
uint32_t u32;
|
|
float f;
|
|
} value;
|
|
value.f = f;
|
|
|
|
for (int32_t i = 1; i <= 4; ++i) {
|
|
const int32_t byte_count = 4 - i;
|
|
const int32_t bit_count = byte_count * 8;
|
|
|
|
const uint8_t byte = static_cast<uint8_t>(value.u32 >> bit_count);
|
|
|
|
const int32_t status = writer->Write(&byte, 1);
|
|
|
|
if (status < 0)
|
|
return status;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
int32_t WriteUInt(IMkvWriter* writer, uint64_t value) {
|
|
if (!writer)
|
|
return -1;
|
|
|
|
int32_t size = GetCodedUIntSize(value);
|
|
|
|
return WriteUIntSize(writer, value, size);
|
|
}
|
|
|
|
int32_t WriteUIntSize(IMkvWriter* writer, uint64_t value, int32_t size) {
|
|
if (!writer || size < 0 || size > 8)
|
|
return -1;
|
|
|
|
if (size > 0) {
|
|
const uint64_t bit = 1LL << (size * 7);
|
|
|
|
if (value > (bit - 2))
|
|
return -1;
|
|
|
|
value |= bit;
|
|
} else {
|
|
size = 1;
|
|
int64_t bit;
|
|
|
|
for (;;) {
|
|
bit = 1LL << (size * 7);
|
|
const uint64_t max = bit - 2;
|
|
|
|
if (value <= max)
|
|
break;
|
|
|
|
++size;
|
|
}
|
|
|
|
if (size > 8)
|
|
return false;
|
|
|
|
value |= bit;
|
|
}
|
|
|
|
return SerializeInt(writer, value, size);
|
|
}
|
|
|
|
int32_t WriteID(IMkvWriter* writer, uint64_t type) {
|
|
if (!writer)
|
|
return -1;
|
|
|
|
writer->ElementStartNotify(type, writer->Position());
|
|
|
|
const int32_t size = GetUIntSize(type);
|
|
|
|
return SerializeInt(writer, type, size);
|
|
}
|
|
|
|
bool WriteEbmlMasterElement(IMkvWriter* writer, uint64_t type, uint64_t size) {
|
|
if (!writer)
|
|
return false;
|
|
|
|
if (WriteID(writer, type))
|
|
return false;
|
|
|
|
if (WriteUInt(writer, size))
|
|
return false;
|
|
|
|
return true;
|
|
}
|
|
|
|
bool WriteEbmlElement(IMkvWriter* writer, uint64_t type, uint64_t value) {
|
|
return WriteEbmlElement(writer, type, value, 0);
|
|
}
|
|
|
|
bool WriteEbmlElement(IMkvWriter* writer, uint64_t type, uint64_t value,
|
|
uint64_t fixed_size) {
|
|
if (!writer)
|
|
return false;
|
|
|
|
if (WriteID(writer, type))
|
|
return false;
|
|
|
|
uint64_t size = GetUIntSize(value);
|
|
if (fixed_size > 0) {
|
|
if (size > fixed_size)
|
|
return false;
|
|
size = fixed_size;
|
|
}
|
|
if (WriteUInt(writer, size))
|
|
return false;
|
|
|
|
if (SerializeInt(writer, value, static_cast<int32_t>(size)))
|
|
return false;
|
|
|
|
return true;
|
|
}
|
|
|
|
bool WriteEbmlElement(IMkvWriter* writer, uint64_t type, int64_t value) {
|
|
if (!writer)
|
|
return false;
|
|
|
|
if (WriteID(writer, type))
|
|
return 0;
|
|
|
|
const uint64_t size = GetIntSize(value);
|
|
if (WriteUInt(writer, size))
|
|
return false;
|
|
|
|
if (SerializeInt(writer, value, static_cast<int32_t>(size)))
|
|
return false;
|
|
|
|
return true;
|
|
}
|
|
|
|
bool WriteEbmlElement(IMkvWriter* writer, uint64_t type, float value) {
|
|
if (!writer)
|
|
return false;
|
|
|
|
if (WriteID(writer, type))
|
|
return false;
|
|
|
|
if (WriteUInt(writer, 4))
|
|
return false;
|
|
|
|
if (SerializeFloat(writer, value))
|
|
return false;
|
|
|
|
return true;
|
|
}
|
|
|
|
bool WriteEbmlElement(IMkvWriter* writer, uint64_t type, const char* value) {
|
|
if (!writer || !value)
|
|
return false;
|
|
|
|
if (WriteID(writer, type))
|
|
return false;
|
|
|
|
const uint64_t length = strlen(value);
|
|
if (WriteUInt(writer, length))
|
|
return false;
|
|
|
|
if (writer->Write(value, static_cast<const uint32_t>(length)))
|
|
return false;
|
|
|
|
return true;
|
|
}
|
|
|
|
bool WriteEbmlElement(IMkvWriter* writer, uint64_t type, const uint8_t* value,
|
|
uint64_t size) {
|
|
if (!writer || !value || size < 1)
|
|
return false;
|
|
|
|
if (WriteID(writer, type))
|
|
return false;
|
|
|
|
if (WriteUInt(writer, size))
|
|
return false;
|
|
|
|
if (writer->Write(value, static_cast<uint32_t>(size)))
|
|
return false;
|
|
|
|
return true;
|
|
}
|
|
|
|
bool WriteEbmlDateElement(IMkvWriter* writer, uint64_t type, int64_t value) {
|
|
if (!writer)
|
|
return false;
|
|
|
|
if (WriteID(writer, type))
|
|
return false;
|
|
|
|
if (WriteUInt(writer, kDateElementSize))
|
|
return false;
|
|
|
|
if (SerializeInt(writer, value, kDateElementSize))
|
|
return false;
|
|
|
|
return true;
|
|
}
|
|
|
|
uint64_t WriteFrame(IMkvWriter* writer, const Frame* const frame,
|
|
Cluster* cluster) {
|
|
if (!writer || !frame || !frame->IsValid() || !cluster ||
|
|
!cluster->timecode_scale())
|
|
return 0;
|
|
|
|
// Technically the timecode for a block can be less than the
|
|
// timecode for the cluster itself (remember that block timecode
|
|
// is a signed, 16-bit integer). However, as a simplification we
|
|
// only permit non-negative cluster-relative timecodes for blocks.
|
|
const int64_t relative_timecode = cluster->GetRelativeTimecode(
|
|
frame->timestamp() / cluster->timecode_scale());
|
|
if (relative_timecode < 0 || relative_timecode > kMaxBlockTimecode)
|
|
return 0;
|
|
|
|
return frame->CanBeSimpleBlock() ?
|
|
WriteSimpleBlock(writer, frame, relative_timecode) :
|
|
WriteBlock(writer, frame, relative_timecode,
|
|
cluster->timecode_scale());
|
|
}
|
|
|
|
uint64_t WriteVoidElement(IMkvWriter* writer, uint64_t size) {
|
|
if (!writer)
|
|
return false;
|
|
|
|
// Subtract one for the void ID and the coded size.
|
|
uint64_t void_entry_size = size - 1 - GetCodedUIntSize(size - 1);
|
|
uint64_t void_size =
|
|
EbmlMasterElementSize(libwebm::kMkvVoid, void_entry_size) +
|
|
void_entry_size;
|
|
|
|
if (void_size != size)
|
|
return 0;
|
|
|
|
const int64_t payload_position = writer->Position();
|
|
if (payload_position < 0)
|
|
return 0;
|
|
|
|
if (WriteID(writer, libwebm::kMkvVoid))
|
|
return 0;
|
|
|
|
if (WriteUInt(writer, void_entry_size))
|
|
return 0;
|
|
|
|
const uint8_t value = 0;
|
|
for (int32_t i = 0; i < static_cast<int32_t>(void_entry_size); ++i) {
|
|
if (writer->Write(&value, 1))
|
|
return 0;
|
|
}
|
|
|
|
const int64_t stop_position = writer->Position();
|
|
if (stop_position < 0 ||
|
|
stop_position - payload_position != static_cast<int64_t>(void_size))
|
|
return 0;
|
|
|
|
return void_size;
|
|
}
|
|
|
|
void GetVersion(int32_t* major, int32_t* minor, int32_t* build,
|
|
int32_t* revision) {
|
|
*major = 0;
|
|
*minor = 2;
|
|
*build = 1;
|
|
*revision = 0;
|
|
}
|
|
|
|
uint64_t MakeUID(unsigned int* seed) {
|
|
uint64_t uid = 0;
|
|
|
|
#ifdef __MINGW32__
|
|
srand(*seed);
|
|
#endif
|
|
|
|
for (int i = 0; i < 7; ++i) { // avoid problems with 8-byte values
|
|
uid <<= 8;
|
|
|
|
// TODO(fgalligan): Move random number generation to platform specific code.
|
|
#ifdef _MSC_VER
|
|
(void)seed;
|
|
const int32_t nn = rand();
|
|
#elif __ANDROID__
|
|
int32_t temp_num = 1;
|
|
int fd = open("/dev/urandom", O_RDONLY);
|
|
if (fd != -1) {
|
|
read(fd, &temp_num, sizeof(temp_num));
|
|
close(fd);
|
|
}
|
|
const int32_t nn = temp_num;
|
|
#elif defined __MINGW32__
|
|
const int32_t nn = rand();
|
|
#else
|
|
const int32_t nn = rand_r(seed);
|
|
#endif
|
|
const int32_t n = 0xFF & (nn >> 4); // throw away low-order bits
|
|
|
|
uid |= n;
|
|
}
|
|
|
|
return uid;
|
|
}
|
|
|
|
} // namespace mkvmuxer
|