1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
media / base / decoder_buffer.cc [blame]
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "media/base/decoder_buffer.h"
#include <sstream>
#include "base/containers/heap_array.h"
#include "base/debug/alias.h"
#include "base/strings/stringprintf.h"
#include "media/base/subsample_entry.h"
namespace media {
namespace {
template <class T>
class ExternalSharedMemoryAdapter : public DecoderBuffer::ExternalMemory {
public:
explicit ExternalSharedMemoryAdapter(T mapping)
: mapping_(std::move(mapping)) {}
const base::span<const uint8_t> Span() const override {
return mapping_.template GetMemoryAsSpan<const uint8_t>();
}
private:
T mapping_;
};
} // namespace
DecoderBuffer::DecoderBuffer(size_t size)
: data_(base::HeapArray<uint8_t>::Uninit(size)) {}
DecoderBuffer::DecoderBuffer(base::span<const uint8_t> data)
: data_(base::HeapArray<uint8_t>::CopiedFrom(data)) {}
DecoderBuffer::DecoderBuffer(base::HeapArray<uint8_t> data)
: data_(std::move(data)) {}
DecoderBuffer::DecoderBuffer(std::unique_ptr<ExternalMemory> external_memory)
: external_memory_(std::move(external_memory)) {}
DecoderBuffer::DecoderBuffer(DecoderBufferType decoder_buffer_type,
std::optional<ConfigVariant> next_config)
: is_end_of_stream_(decoder_buffer_type ==
DecoderBufferType::kEndOfStream) {
if (next_config) {
DCHECK(end_of_stream());
side_data_ = std::make_unique<DecoderBufferSideData>();
side_data_->next_config = std::move(next_config);
}
}
DecoderBuffer::~DecoderBuffer() = default;
// static
scoped_refptr<DecoderBuffer> DecoderBuffer::CopyFrom(
base::span<const uint8_t> data) {
return base::WrapRefCounted(new DecoderBuffer(data));
}
// static
scoped_refptr<DecoderBuffer> DecoderBuffer::FromArray(
base::HeapArray<uint8_t> data) {
return base::WrapRefCounted(new DecoderBuffer(std::move(data)));
}
// static
scoped_refptr<DecoderBuffer> DecoderBuffer::FromSharedMemoryRegion(
base::UnsafeSharedMemoryRegion region,
uint64_t offset,
size_t size) {
if (size == 0) {
return nullptr;
}
auto mapping = region.MapAt(offset, size);
if (!mapping.IsValid()) {
return nullptr;
}
return FromExternalMemory(
std::make_unique<
ExternalSharedMemoryAdapter<base::WritableSharedMemoryMapping>>(
std::move(mapping)));
}
// static
scoped_refptr<DecoderBuffer> DecoderBuffer::FromSharedMemoryRegion(
base::ReadOnlySharedMemoryRegion region,
uint64_t offset,
size_t size) {
if (size == 0) {
return nullptr;
}
auto mapping = region.MapAt(offset, size);
if (!mapping.IsValid()) {
return nullptr;
}
return FromExternalMemory(
std::make_unique<
ExternalSharedMemoryAdapter<base::ReadOnlySharedMemoryMapping>>(
std::move(mapping)));
}
// static
scoped_refptr<DecoderBuffer> DecoderBuffer::FromExternalMemory(
std::unique_ptr<ExternalMemory> external_memory) {
DCHECK(external_memory);
if (external_memory->Span().empty()) {
return nullptr;
}
return base::WrapRefCounted(new DecoderBuffer(std::move(external_memory)));
}
// static
scoped_refptr<DecoderBuffer> DecoderBuffer::CreateEOSBuffer(
std::optional<ConfigVariant> next_config) {
return base::WrapRefCounted(new DecoderBuffer(DecoderBufferType::kEndOfStream,
std::move(next_config)));
}
// static
bool DecoderBuffer::DoSubsamplesMatch(const DecoderBuffer& buffer) {
// If buffer is at end of stream, no subsamples to verify
if (buffer.end_of_stream()) {
return true;
}
// If stream is unencrypted, we do not have to verify subsamples size.
if (!buffer.is_encrypted()) {
return true;
}
const auto& subsamples = buffer.decrypt_config()->subsamples();
if (subsamples.empty()) {
return true;
}
return VerifySubsamplesMatchSize(subsamples, buffer.size());
}
base::span<const uint8_t> DecoderBuffer::AsSpan() const {
DCHECK(!end_of_stream());
return external_memory_ ? external_memory_->Span() : data_;
}
void DecoderBuffer::set_discard_padding(const DiscardPadding& discard_padding) {
DCHECK(!end_of_stream());
if (!side_data_ && discard_padding == DiscardPadding()) {
return;
}
WritableSideData().discard_padding = discard_padding;
}
DecoderBufferSideData& DecoderBuffer::WritableSideData() {
DCHECK(!end_of_stream());
if (!side_data()) {
side_data_ = std::make_unique<DecoderBufferSideData>();
}
return *side_data_;
}
void DecoderBuffer::set_side_data(
std::unique_ptr<DecoderBufferSideData> side_data) {
DCHECK(!end_of_stream());
side_data_ = std::move(side_data);
}
bool DecoderBuffer::MatchesMetadataForTesting(
const DecoderBuffer& buffer) const {
if (end_of_stream() != buffer.end_of_stream()) {
return false;
}
// Note: We use `side_data_` directly to avoid DCHECKs for EOS buffers.
if (side_data_ && !side_data_->Matches(*buffer.side_data_)) {
return false;
}
// None of the following methods may be called on an EOS buffer.
if (end_of_stream()) {
return true;
}
if (timestamp() != buffer.timestamp() || duration() != buffer.duration() ||
is_key_frame() != buffer.is_key_frame()) {
return false;
}
if ((decrypt_config() == nullptr) != (buffer.decrypt_config() == nullptr))
return false;
return decrypt_config() ? decrypt_config()->Matches(*buffer.decrypt_config())
: true;
}
bool DecoderBuffer::MatchesForTesting(const DecoderBuffer& buffer) const {
if (!MatchesMetadataForTesting(buffer)) // IN-TEST
return false;
// It is illegal to call any member function if eos is true.
if (end_of_stream())
return true;
DCHECK(!buffer.end_of_stream());
return base::span(*this) == base::span(buffer);
}
std::string DecoderBuffer::AsHumanReadableString(bool verbose) const {
if (end_of_stream()) {
if (!next_config()) {
return "EOS";
}
std::string config;
const auto nc = next_config().value();
if (const auto* ac = absl::get_if<media::AudioDecoderConfig>(&nc)) {
config = ac->AsHumanReadableString();
} else {
config = absl::get<media::VideoDecoderConfig>(nc).AsHumanReadableString();
}
return base::StringPrintf("EOS config=(%s)", config.c_str());
}
std::ostringstream s;
s << "{timestamp=" << timestamp_.InMicroseconds()
<< " duration=" << duration_.InMicroseconds() << " size=" << size()
<< " is_key_frame=" << is_key_frame_
<< " encrypted=" << (decrypt_config_ != nullptr);
if (verbose) {
s << " side_data=" << !!side_data();
if (side_data()) {
s << " discard_padding (us)=("
<< side_data_->discard_padding.first.InMicroseconds() << ", "
<< side_data_->discard_padding.second.InMicroseconds() << ")";
}
if (decrypt_config_) {
s << " decrypt_config=" << (*decrypt_config_);
}
}
s << "}";
return s.str();
}
void DecoderBuffer::set_timestamp(base::TimeDelta timestamp) {
DCHECK(!end_of_stream());
timestamp_ = timestamp;
}
size_t DecoderBuffer::GetMemoryUsage() const {
size_t memory_usage = sizeof(DecoderBuffer);
if (end_of_stream()) {
return memory_usage;
}
memory_usage += size();
// Side data and decrypt config would not change after construction.
if (side_data()) {
memory_usage += sizeof(decltype(side_data_->spatial_layers)::value_type) *
side_data_->spatial_layers.capacity();
memory_usage += side_data_->alpha_data.size();
}
if (decrypt_config_) {
memory_usage += sizeof(DecryptConfig);
memory_usage += decrypt_config_->key_id().capacity();
memory_usage += decrypt_config_->iv().capacity();
memory_usage +=
sizeof(SubsampleEntry) * decrypt_config_->subsamples().capacity();
}
return memory_usage;
}
} // namespace media