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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
media / base / mac / videotoolbox_helpers.cc [blame]
// Copyright 2016 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/mac/videotoolbox_helpers.h"
#include <array>
#include <vector>
#include "base/containers/span.h"
#include "base/logging.h"
#include "base/memory/raw_ptr.h"
#include "base/notreached.h"
#include "base/numerics/byte_conversions.h"
#include "media/base/video_codecs.h"
#include "media/media_buildflags.h"
namespace media::video_toolbox {
namespace {
static const char kAnnexBHeaderBytes[4] = {0, 0, 0, 1};
} // anonymous namespace
// Wrapper class for writing AnnexBBuffer output into.
class AnnexBBuffer {
public:
virtual bool Reserve(size_t size) = 0;
virtual void Append(const char* s, size_t n) = 0;
virtual size_t GetReservedSize() const = 0;
};
class RawAnnexBBuffer : public AnnexBBuffer {
public:
RawAnnexBBuffer(char* annexb_buffer, size_t annexb_buffer_size)
: annexb_buffer_(annexb_buffer),
annexb_buffer_size_(annexb_buffer_size),
annexb_buffer_offset_(0) {}
RawAnnexBBuffer() = delete;
RawAnnexBBuffer(const RawAnnexBBuffer&) = delete;
RawAnnexBBuffer& operator=(const RawAnnexBBuffer&) = delete;
bool Reserve(size_t size) override {
reserved_size_ = size;
return size <= annexb_buffer_size_;
}
void Append(const char* s, size_t n) override {
memcpy(annexb_buffer_ + annexb_buffer_offset_, s, n);
annexb_buffer_offset_ += n;
DCHECK_GE(reserved_size_, annexb_buffer_offset_);
}
size_t GetReservedSize() const override { return reserved_size_; }
private:
raw_ptr<char, AllowPtrArithmetic> annexb_buffer_;
size_t annexb_buffer_size_;
size_t annexb_buffer_offset_;
size_t reserved_size_;
};
class StringAnnexBBuffer : public AnnexBBuffer {
public:
explicit StringAnnexBBuffer(std::string* str_annexb_buffer)
: str_annexb_buffer_(str_annexb_buffer) {}
StringAnnexBBuffer() = delete;
StringAnnexBBuffer(const StringAnnexBBuffer&) = delete;
StringAnnexBBuffer& operator=(const StringAnnexBBuffer&) = delete;
bool Reserve(size_t size) override {
str_annexb_buffer_->reserve(size);
return true;
}
void Append(const char* s, size_t n) override {
str_annexb_buffer_->append(s, n);
}
size_t GetReservedSize() const override { return str_annexb_buffer_->size(); }
private:
raw_ptr<std::string> str_annexb_buffer_;
};
template <typename NalSizeType>
requires(std::is_integral_v<NalSizeType> && std::is_unsigned_v<NalSizeType> &&
sizeof(NalSizeType) <= 4)
void CopyNalsToAnnexB(base::span<const char> buffer,
AnnexBBuffer* annexb_buffer) {
while (!buffer.empty()) {
const auto nal_size_be =
base::as_bytes(buffer.take_first<sizeof(NalSizeType)>());
NalSizeType nal_size;
if constexpr (sizeof(NalSizeType) == 1u) {
nal_size = base::U8FromBigEndian(nal_size_be);
} else if constexpr (sizeof(NalSizeType) == 2u) {
nal_size = base::U16FromBigEndian(nal_size_be);
} else {
nal_size = base::U32FromBigEndian(nal_size_be);
}
auto nals_buf = buffer.take_first(nal_size);
annexb_buffer->Append(kAnnexBHeaderBytes, sizeof(kAnnexBHeaderBytes));
annexb_buffer->Append(nals_buf.data(), nals_buf.size());
}
}
OSStatus GetParameterSetAtIndex(VideoCodec codec,
CMFormatDescriptionRef videoDesc,
size_t parameterSetIndex,
const uint8_t** parameterSetPointerOut,
size_t* parameterSetSizeOut,
size_t* parameterSetCountOut,
int* NALUnitHeaderLengthOut) {
switch (codec) {
case VideoCodec::kH264:
return CMVideoFormatDescriptionGetH264ParameterSetAtIndex(
videoDesc, parameterSetIndex, parameterSetPointerOut,
parameterSetSizeOut, parameterSetCountOut, NALUnitHeaderLengthOut);
#if BUILDFLAG(ENABLE_HEVC_PARSER_AND_HW_DECODER)
case VideoCodec::kHEVC:
return CMVideoFormatDescriptionGetHEVCParameterSetAtIndex(
videoDesc, parameterSetIndex, parameterSetPointerOut,
parameterSetSizeOut, parameterSetCountOut, NALUnitHeaderLengthOut);
#endif // BUILDFLAG(ENABLE_HEVC_PARSER_AND_HW_DECODER)
default:
NOTREACHED();
}
}
bool CopySampleBufferToAnnexBBuffer(VideoCodec codec,
CMSampleBufferRef sbuf,
AnnexBBuffer* annexb_buffer,
bool keyframe) {
// Perform two pass, one to figure out the total output size, and another to
// copy the data after having performed a single output allocation. Note that
// we'll allocate a bit more because we'll count 4 bytes instead of 3 for
// video NALs.
OSStatus status;
// Get the sample buffer's block buffer and format description.
auto* const bb = CMSampleBufferGetDataBuffer(sbuf);
DCHECK(bb);
auto* fdesc = CMSampleBufferGetFormatDescription(sbuf);
DCHECK(fdesc);
const size_t bb_size = CMBlockBufferGetDataLength(bb);
size_t total_bytes = bb_size;
size_t pset_count;
int nal_size_field_bytes;
status = GetParameterSetAtIndex(codec, fdesc, 0, nullptr, nullptr,
&pset_count, &nal_size_field_bytes);
if (status == kCMFormatDescriptionBridgeError_InvalidParameter) {
DLOG(WARNING) << " assuming " << int(codec == VideoCodec::kHEVC ? 3 : 2)
<< " parameter sets and 4 bytes NAL length header";
pset_count = codec == VideoCodec::kHEVC ? 3 : 2;
nal_size_field_bytes = 4;
} else if (status != noErr) {
DLOG(ERROR) << " GetParameterSetAtIndex failed: " << status;
return false;
}
if (keyframe) {
const uint8_t* pset;
size_t pset_size;
for (size_t pset_i = 0; pset_i < pset_count; ++pset_i) {
status = GetParameterSetAtIndex(codec, fdesc, pset_i, &pset, &pset_size,
nullptr, nullptr);
if (status != noErr) {
DLOG(ERROR) << " GetParameterSetAtIndex failed: " << status;
return false;
}
total_bytes += pset_size + nal_size_field_bytes;
}
}
if (!annexb_buffer->Reserve(total_bytes)) {
DLOG(ERROR) << "Cannot fit encode output into bitstream buffer. Requested:"
<< total_bytes;
return false;
}
// Copy all parameter sets before keyframes.
if (keyframe) {
const uint8_t* pset;
size_t pset_size;
for (size_t pset_i = 0; pset_i < pset_count; ++pset_i) {
status = GetParameterSetAtIndex(codec, fdesc, pset_i, &pset, &pset_size,
nullptr, nullptr);
if (status != noErr) {
DLOG(ERROR) << " GetParameterSetAtIndex failed: " << status;
return false;
}
annexb_buffer->Append(kAnnexBHeaderBytes, sizeof(kAnnexBHeaderBytes));
annexb_buffer->Append(reinterpret_cast<const char*>(pset), pset_size);
}
}
// Block buffers can be composed of non-contiguous chunks. For the sake of
// keeping this code simple, flatten non-contiguous block buffers.
base::apple::ScopedCFTypeRef<CMBlockBufferRef> contiguous_bb(
bb, base::scoped_policy::RETAIN);
if (!CMBlockBufferIsRangeContiguous(bb, 0, 0)) {
contiguous_bb.reset();
status = CMBlockBufferCreateContiguous(kCFAllocatorDefault, bb,
kCFAllocatorDefault, nullptr, 0, 0,
0, contiguous_bb.InitializeInto());
if (status != noErr) {
DLOG(ERROR) << " CMBlockBufferCreateContiguous failed: " << status;
return false;
}
}
// Copy all the NAL units. In the process convert them from AVCC/HVCC format
// (length header) to AnnexB format (start code).
char* contiguous_bb_data;
status = CMBlockBufferGetDataPointer(contiguous_bb.get(), 0, nullptr, nullptr,
&contiguous_bb_data);
if (status != noErr) {
DLOG(ERROR) << " CMBlockBufferGetDataPointer failed: " << status;
return false;
}
auto contiguous_bb_span =
// SAFETY: `bb` is a block buffer of size `bb_size`, queried above through
// CMBlockBufferGetDataLength(). The `contiguous_bb` is a contiguous
// buffer created from `bb`, so it has the same size. Thus the
// `contiguous_bb_data` pointer, which points to the `contiguous_bb`
// buffer, will point to an array of size `bb_size`.
UNSAFE_BUFFERS(base::span<const char>(contiguous_bb_data, bb_size));
if (nal_size_field_bytes == 1) {
CopyNalsToAnnexB<uint8_t>(contiguous_bb_span, annexb_buffer);
} else if (nal_size_field_bytes == 2) {
CopyNalsToAnnexB<uint16_t>(contiguous_bb_span, annexb_buffer);
} else if (nal_size_field_bytes == 4) {
CopyNalsToAnnexB<uint32_t>(contiguous_bb_span, annexb_buffer);
} else {
NOTREACHED();
}
return true;
}
bool CopySampleBufferToAnnexBBuffer(VideoCodec codec,
CMSampleBufferRef sbuf,
bool keyframe,
std::string* annexb_buffer) {
StringAnnexBBuffer buffer(annexb_buffer);
return CopySampleBufferToAnnexBBuffer(codec, sbuf, &buffer, keyframe);
}
bool CopySampleBufferToAnnexBBuffer(VideoCodec codec,
CMSampleBufferRef sbuf,
bool keyframe,
size_t annexb_buffer_size,
char* annexb_buffer,
size_t* used_buffer_size) {
RawAnnexBBuffer buffer(annexb_buffer, annexb_buffer_size);
const bool copy_rv =
CopySampleBufferToAnnexBBuffer(codec, sbuf, &buffer, keyframe);
*used_buffer_size = buffer.GetReservedSize();
return copy_rv;
}
SessionPropertySetter::SessionPropertySetter(
base::apple::ScopedCFTypeRef<VTCompressionSessionRef> session)
: session_(session) {}
SessionPropertySetter::~SessionPropertySetter() {}
bool SessionPropertySetter::IsSupported(CFStringRef key) {
DCHECK(session_);
if (!supported_keys_) {
CFDictionaryRef dict_ref;
if (VTSessionCopySupportedPropertyDictionary(session_.get(), &dict_ref) ==
noErr) {
supported_keys_.reset(dict_ref);
}
}
return supported_keys_ && CFDictionaryContainsKey(supported_keys_.get(), key);
}
bool SessionPropertySetter::Set(CFStringRef key, int32_t value) {
DCHECK(session_);
base::apple::ScopedCFTypeRef<CFNumberRef> cfvalue(
CFNumberCreate(nullptr, kCFNumberSInt32Type, &value));
return VTSessionSetProperty(session_.get(), key, cfvalue.get()) == noErr;
}
bool SessionPropertySetter::Set(CFStringRef key, bool value) {
DCHECK(session_);
CFBooleanRef cfvalue = (value) ? kCFBooleanTrue : kCFBooleanFalse;
return VTSessionSetProperty(session_.get(), key, cfvalue) == noErr;
}
bool SessionPropertySetter::Set(CFStringRef key, double value) {
DCHECK(session_);
base::apple::ScopedCFTypeRef<CFNumberRef> cfvalue(
CFNumberCreate(nullptr, kCFNumberDoubleType, &value));
return VTSessionSetProperty(session_.get(), key, cfvalue.get()) == noErr;
}
bool SessionPropertySetter::Set(CFStringRef key, CFStringRef value) {
DCHECK(session_);
return VTSessionSetProperty(session_.get(), key, value) == noErr;
}
bool SessionPropertySetter::Set(CFStringRef key, CFArrayRef value) {
DCHECK(session_);
return VTSessionSetProperty(session_.get(), key, value) == noErr;
}
} // namespace media::video_toolbox