2015-10-08 21:48:07 +00:00
|
|
|
// Copyright 2014 The Chromium Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2023-10-10 23:51:11 +00:00
|
|
|
#include <packager/media/formats/webm/cluster_builder.h>
|
2015-10-08 21:48:07 +00:00
|
|
|
|
2023-10-13 19:42:47 +00:00
|
|
|
#include <absl/log/check.h>
|
|
|
|
#include <absl/log/log.h>
|
2023-10-11 08:49:50 +00:00
|
|
|
|
2023-10-10 23:51:11 +00:00
|
|
|
#include <packager/media/formats/webm/webm_constants.h>
|
2015-10-08 21:48:07 +00:00
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2015-10-08 21:48:07 +00:00
|
|
|
namespace media {
|
|
|
|
|
2015-10-14 22:46:23 +00:00
|
|
|
static const uint8_t kClusterHeader[] = {
|
|
|
|
0x1F, 0x43, 0xB6, 0x75, // CLUSTER ID
|
|
|
|
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // cluster(size = 0)
|
|
|
|
0xE7, // Timecode ID
|
|
|
|
0x88, // timecode(size=8)
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // timecode value
|
2015-10-08 21:48:07 +00:00
|
|
|
};
|
|
|
|
|
2015-10-14 22:46:23 +00:00
|
|
|
static const uint8_t kSimpleBlockHeader[] = {
|
|
|
|
0xA3, // SimpleBlock ID
|
|
|
|
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // SimpleBlock(size = 0)
|
2015-10-08 21:48:07 +00:00
|
|
|
};
|
|
|
|
|
2015-10-14 22:46:23 +00:00
|
|
|
static const uint8_t kBlockGroupHeader[] = {
|
|
|
|
0xA0, // BlockGroup ID
|
|
|
|
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // BlockGroup(size = 0)
|
|
|
|
0x9B, // BlockDuration ID
|
|
|
|
0x88, // BlockDuration(size = 8)
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // duration
|
|
|
|
0xA1, // Block ID
|
|
|
|
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Block(size = 0)
|
2015-10-08 21:48:07 +00:00
|
|
|
};
|
|
|
|
|
2015-10-14 22:46:23 +00:00
|
|
|
static const uint8_t kBlockGroupHeaderWithoutBlockDuration[] = {
|
|
|
|
0xA0, // BlockGroup ID
|
|
|
|
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // BlockGroup(size = 0)
|
|
|
|
0xA1, // Block ID
|
|
|
|
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Block(size = 0)
|
2015-10-08 21:48:07 +00:00
|
|
|
};
|
|
|
|
|
2016-07-29 23:06:20 +00:00
|
|
|
static const uint8_t kBlockGroupReferenceBlock[] = {
|
|
|
|
0xFB, // ReferenceBlock ID
|
|
|
|
0x81, 0x00, // ReferenceBlock (size=1, value=0)
|
|
|
|
};
|
|
|
|
|
2015-10-08 21:48:07 +00:00
|
|
|
enum {
|
|
|
|
kClusterSizeOffset = 4,
|
|
|
|
kClusterTimecodeOffset = 14,
|
|
|
|
|
|
|
|
kSimpleBlockSizeOffset = 1,
|
|
|
|
|
|
|
|
kBlockGroupSizeOffset = 1,
|
|
|
|
kBlockGroupWithoutBlockDurationBlockSizeOffset = 10,
|
|
|
|
kBlockGroupDurationOffset = 11,
|
|
|
|
kBlockGroupBlockSizeOffset = 20,
|
|
|
|
|
|
|
|
kInitialBufferSize = 32768,
|
|
|
|
};
|
|
|
|
|
2016-08-17 17:41:40 +00:00
|
|
|
Cluster::Cluster(std::unique_ptr<uint8_t[]> data, int size)
|
|
|
|
: data_(std::move(data)), size_(size) {}
|
2015-10-08 21:48:07 +00:00
|
|
|
Cluster::~Cluster() {}
|
|
|
|
|
|
|
|
ClusterBuilder::ClusterBuilder() { Reset(); }
|
|
|
|
ClusterBuilder::~ClusterBuilder() {}
|
|
|
|
|
2015-10-14 22:46:23 +00:00
|
|
|
void ClusterBuilder::SetClusterTimecode(int64_t cluster_timecode) {
|
2015-10-08 21:48:07 +00:00
|
|
|
DCHECK_EQ(cluster_timecode_, -1);
|
|
|
|
|
|
|
|
cluster_timecode_ = cluster_timecode;
|
|
|
|
|
|
|
|
// Write the timecode into the header.
|
2015-10-14 22:46:23 +00:00
|
|
|
uint8_t* buf = buffer_.get() + kClusterTimecodeOffset;
|
2015-10-08 21:48:07 +00:00
|
|
|
for (int i = 7; i >= 0; --i) {
|
|
|
|
buf[i] = cluster_timecode & 0xff;
|
|
|
|
cluster_timecode >>= 8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-14 22:46:23 +00:00
|
|
|
void ClusterBuilder::AddSimpleBlock(int track_num,
|
|
|
|
int64_t timecode,
|
|
|
|
int flags,
|
|
|
|
const uint8_t* data,
|
|
|
|
int size) {
|
2015-10-08 21:48:07 +00:00
|
|
|
int block_size = size + 4;
|
|
|
|
int bytes_needed = sizeof(kSimpleBlockHeader) + block_size;
|
|
|
|
if (bytes_needed > (buffer_size_ - bytes_used_))
|
|
|
|
ExtendBuffer(bytes_needed);
|
|
|
|
|
2015-10-14 22:46:23 +00:00
|
|
|
uint8_t* buf = buffer_.get() + bytes_used_;
|
2015-10-08 21:48:07 +00:00
|
|
|
int block_offset = bytes_used_;
|
|
|
|
memcpy(buf, kSimpleBlockHeader, sizeof(kSimpleBlockHeader));
|
|
|
|
UpdateUInt64(block_offset + kSimpleBlockSizeOffset, block_size);
|
|
|
|
buf += sizeof(kSimpleBlockHeader);
|
|
|
|
|
|
|
|
WriteBlock(buf, track_num, timecode, flags, data, size);
|
|
|
|
|
|
|
|
bytes_used_ += bytes_needed;
|
|
|
|
}
|
|
|
|
|
2015-10-14 22:46:23 +00:00
|
|
|
void ClusterBuilder::AddBlockGroup(int track_num,
|
|
|
|
int64_t timecode,
|
|
|
|
int duration,
|
|
|
|
int flags,
|
2016-07-29 23:06:20 +00:00
|
|
|
bool is_key_frame,
|
2015-10-14 22:46:23 +00:00
|
|
|
const uint8_t* data,
|
|
|
|
int size) {
|
2016-07-29 23:06:20 +00:00
|
|
|
AddBlockGroupInternal(track_num, timecode, true, duration, flags,
|
|
|
|
is_key_frame, data, size);
|
2015-10-08 21:48:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ClusterBuilder::AddBlockGroupWithoutBlockDuration(int track_num,
|
2015-10-14 22:46:23 +00:00
|
|
|
int64_t timecode,
|
2015-10-08 21:48:07 +00:00
|
|
|
int flags,
|
2016-07-29 23:06:20 +00:00
|
|
|
bool is_key_frame,
|
2015-10-14 22:46:23 +00:00
|
|
|
const uint8_t* data,
|
2015-10-08 21:48:07 +00:00
|
|
|
int size) {
|
2016-07-29 23:06:20 +00:00
|
|
|
AddBlockGroupInternal(track_num, timecode, false, 0, flags, is_key_frame,
|
|
|
|
data, size);
|
2015-10-08 21:48:07 +00:00
|
|
|
}
|
|
|
|
|
2015-10-14 22:46:23 +00:00
|
|
|
void ClusterBuilder::AddBlockGroupInternal(int track_num,
|
|
|
|
int64_t timecode,
|
2015-10-08 21:48:07 +00:00
|
|
|
bool include_block_duration,
|
2015-10-14 22:46:23 +00:00
|
|
|
int duration,
|
|
|
|
int flags,
|
2016-07-29 23:06:20 +00:00
|
|
|
bool is_key_frame,
|
2015-10-14 22:46:23 +00:00
|
|
|
const uint8_t* data,
|
|
|
|
int size) {
|
2015-10-08 21:48:07 +00:00
|
|
|
int block_size = size + 4;
|
|
|
|
int bytes_needed = block_size;
|
|
|
|
if (include_block_duration) {
|
|
|
|
bytes_needed += sizeof(kBlockGroupHeader);
|
|
|
|
} else {
|
|
|
|
bytes_needed += sizeof(kBlockGroupHeaderWithoutBlockDuration);
|
|
|
|
}
|
2016-07-29 23:06:20 +00:00
|
|
|
if (!is_key_frame) {
|
|
|
|
bytes_needed += sizeof(kBlockGroupReferenceBlock);
|
|
|
|
}
|
2015-10-08 21:48:07 +00:00
|
|
|
|
|
|
|
int block_group_size = bytes_needed - 9;
|
|
|
|
|
|
|
|
if (bytes_needed > (buffer_size_ - bytes_used_))
|
|
|
|
ExtendBuffer(bytes_needed);
|
|
|
|
|
2015-10-14 22:46:23 +00:00
|
|
|
uint8_t* buf = buffer_.get() + bytes_used_;
|
2015-10-08 21:48:07 +00:00
|
|
|
int block_group_offset = bytes_used_;
|
|
|
|
if (include_block_duration) {
|
|
|
|
memcpy(buf, kBlockGroupHeader, sizeof(kBlockGroupHeader));
|
|
|
|
UpdateUInt64(block_group_offset + kBlockGroupDurationOffset, duration);
|
|
|
|
UpdateUInt64(block_group_offset + kBlockGroupBlockSizeOffset, block_size);
|
|
|
|
buf += sizeof(kBlockGroupHeader);
|
|
|
|
} else {
|
|
|
|
memcpy(buf, kBlockGroupHeaderWithoutBlockDuration,
|
|
|
|
sizeof(kBlockGroupHeaderWithoutBlockDuration));
|
|
|
|
UpdateUInt64(
|
|
|
|
block_group_offset + kBlockGroupWithoutBlockDurationBlockSizeOffset,
|
|
|
|
block_size);
|
|
|
|
buf += sizeof(kBlockGroupHeaderWithoutBlockDuration);
|
|
|
|
}
|
|
|
|
|
|
|
|
UpdateUInt64(block_group_offset + kBlockGroupSizeOffset, block_group_size);
|
|
|
|
|
|
|
|
// Make sure the 4 most-significant bits are 0.
|
|
|
|
// http://www.matroska.org/technical/specs/index.html#block_structure
|
|
|
|
flags &= 0x0f;
|
|
|
|
|
|
|
|
WriteBlock(buf, track_num, timecode, flags, data, size);
|
2016-07-29 23:06:20 +00:00
|
|
|
buf += size + 4;
|
2015-10-08 21:48:07 +00:00
|
|
|
|
2016-07-29 23:06:20 +00:00
|
|
|
if (!is_key_frame)
|
|
|
|
memcpy(buf, kBlockGroupReferenceBlock, sizeof(kBlockGroupReferenceBlock));
|
2015-10-08 21:48:07 +00:00
|
|
|
bytes_used_ += bytes_needed;
|
|
|
|
}
|
|
|
|
|
2015-10-14 22:46:23 +00:00
|
|
|
void ClusterBuilder::WriteBlock(uint8_t* buf,
|
|
|
|
int track_num,
|
|
|
|
int64_t timecode,
|
|
|
|
int flags,
|
|
|
|
const uint8_t* data,
|
|
|
|
int size) {
|
2015-10-08 21:48:07 +00:00
|
|
|
DCHECK_GE(track_num, 0);
|
|
|
|
DCHECK_LE(track_num, 126);
|
|
|
|
DCHECK_GE(flags, 0);
|
|
|
|
DCHECK_LE(flags, 0xff);
|
|
|
|
DCHECK(data);
|
|
|
|
DCHECK_GT(size, 0);
|
|
|
|
DCHECK_NE(cluster_timecode_, -1);
|
|
|
|
|
2015-10-14 22:46:23 +00:00
|
|
|
int64_t timecode_delta = timecode - cluster_timecode_;
|
2015-10-08 21:48:07 +00:00
|
|
|
DCHECK_GE(timecode_delta, -32768);
|
|
|
|
DCHECK_LE(timecode_delta, 32767);
|
|
|
|
|
|
|
|
buf[0] = 0x80 | (track_num & 0x7F);
|
|
|
|
buf[1] = (timecode_delta >> 8) & 0xff;
|
|
|
|
buf[2] = timecode_delta & 0xff;
|
|
|
|
buf[3] = flags & 0xff;
|
|
|
|
memcpy(buf + 4, data, size);
|
|
|
|
}
|
|
|
|
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<Cluster> ClusterBuilder::Finish() {
|
2015-10-08 21:48:07 +00:00
|
|
|
DCHECK_NE(cluster_timecode_, -1);
|
|
|
|
|
|
|
|
UpdateUInt64(kClusterSizeOffset, bytes_used_ - (kClusterSizeOffset + 8));
|
|
|
|
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<Cluster> ret(new Cluster(std::move(buffer_), bytes_used_));
|
2015-10-08 21:48:07 +00:00
|
|
|
Reset();
|
2016-08-17 17:41:40 +00:00
|
|
|
return ret;
|
2015-10-08 21:48:07 +00:00
|
|
|
}
|
|
|
|
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<Cluster> ClusterBuilder::FinishWithUnknownSize() {
|
2015-10-08 21:48:07 +00:00
|
|
|
DCHECK_NE(cluster_timecode_, -1);
|
|
|
|
|
|
|
|
UpdateUInt64(kClusterSizeOffset, kWebMUnknownSize);
|
|
|
|
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<Cluster> ret(new Cluster(std::move(buffer_), bytes_used_));
|
2015-10-08 21:48:07 +00:00
|
|
|
Reset();
|
2016-08-17 17:41:40 +00:00
|
|
|
return ret;
|
2015-10-08 21:48:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ClusterBuilder::Reset() {
|
|
|
|
buffer_size_ = kInitialBufferSize;
|
2015-10-14 22:46:23 +00:00
|
|
|
buffer_.reset(new uint8_t[buffer_size_]);
|
2015-10-08 21:48:07 +00:00
|
|
|
memcpy(buffer_.get(), kClusterHeader, sizeof(kClusterHeader));
|
|
|
|
bytes_used_ = sizeof(kClusterHeader);
|
|
|
|
cluster_timecode_ = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClusterBuilder::ExtendBuffer(int bytes_needed) {
|
|
|
|
int new_buffer_size = 2 * buffer_size_;
|
|
|
|
|
|
|
|
while ((new_buffer_size - bytes_used_) < bytes_needed)
|
|
|
|
new_buffer_size *= 2;
|
|
|
|
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<uint8_t[]> new_buffer(new uint8_t[new_buffer_size]);
|
2015-10-08 21:48:07 +00:00
|
|
|
|
|
|
|
memcpy(new_buffer.get(), buffer_.get(), bytes_used_);
|
|
|
|
buffer_.reset(new_buffer.release());
|
|
|
|
buffer_size_ = new_buffer_size;
|
|
|
|
}
|
|
|
|
|
2015-10-14 22:46:23 +00:00
|
|
|
void ClusterBuilder::UpdateUInt64(int offset, int64_t value) {
|
2015-10-08 21:48:07 +00:00
|
|
|
DCHECK_LE(offset + 7, buffer_size_);
|
2015-10-14 22:46:23 +00:00
|
|
|
uint8_t* buf = buffer_.get() + offset;
|
2015-10-08 21:48:07 +00:00
|
|
|
|
|
|
|
// Fill the last 7 bytes of size field in big-endian order.
|
|
|
|
for (int i = 7; i > 0; i--) {
|
|
|
|
buf[i] = value & 0xff;
|
|
|
|
value >>= 8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|