| Index: media/base/mac/videotoolbox_helpers.cc
|
| diff --git a/media/base/mac/videotoolbox_helpers.cc b/media/base/mac/videotoolbox_helpers.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..2447deb1e32a9ace29169fe6b17e9decdb180af3
|
| --- /dev/null
|
| +++ b/media/base/mac/videotoolbox_helpers.cc
|
| @@ -0,0 +1,203 @@
|
| +// Copyright 2016 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.
|
| +
|
| +#include "media/base/mac/videotoolbox_helpers.h"
|
| +
|
| +#include <vector>
|
| +
|
| +#include "base/big_endian.h"
|
| +
|
| +namespace media {
|
| +
|
| +// Copy a H.264 frame stored in a CM sample buffer to an Annex B buffer. Copies
|
| +// parameter sets for keyframes before the frame data as well.
|
| +void CopySampleBufferToAnnexBBuffer(CoreMediaGlue::CMSampleBufferRef sbuf,
|
| + std::string* 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 bb = CoreMediaGlue::CMSampleBufferGetDataBuffer(sbuf);
|
| + DCHECK(bb);
|
| + auto fdesc = CoreMediaGlue::CMSampleBufferGetFormatDescription(sbuf);
|
| + DCHECK(fdesc);
|
| +
|
| + size_t bb_size = CoreMediaGlue::CMBlockBufferGetDataLength(bb);
|
| + size_t total_bytes = bb_size;
|
| +
|
| + size_t pset_count;
|
| + int nal_size_field_bytes;
|
| + status = CoreMediaGlue::CMVideoFormatDescriptionGetH264ParameterSetAtIndex(
|
| + fdesc, 0, nullptr, nullptr, &pset_count, &nal_size_field_bytes);
|
| + if (status ==
|
| + CoreMediaGlue::kCMFormatDescriptionBridgeError_InvalidParameter) {
|
| + DLOG(WARNING) << " assuming 2 parameter sets and 4 bytes NAL length header";
|
| + pset_count = 2;
|
| + nal_size_field_bytes = 4;
|
| + } else if (status != noErr) {
|
| + DLOG(ERROR)
|
| + << " CMVideoFormatDescriptionGetH264ParameterSetAtIndex failed: "
|
| + << status;
|
| + return;
|
| + }
|
| +
|
| + if (keyframe) {
|
| + const uint8_t* pset;
|
| + size_t pset_size;
|
| + for (size_t pset_i = 0; pset_i < pset_count; ++pset_i) {
|
| + status =
|
| + CoreMediaGlue::CMVideoFormatDescriptionGetH264ParameterSetAtIndex(
|
| + fdesc, pset_i, &pset, &pset_size, nullptr, nullptr);
|
| + if (status != noErr) {
|
| + DLOG(ERROR)
|
| + << " CMVideoFormatDescriptionGetH264ParameterSetAtIndex failed: "
|
| + << status;
|
| + return;
|
| + }
|
| + total_bytes += pset_size + nal_size_field_bytes;
|
| + }
|
| + }
|
| +
|
| + annexb_buffer->reserve(total_bytes);
|
| +
|
| + // 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 =
|
| + CoreMediaGlue::CMVideoFormatDescriptionGetH264ParameterSetAtIndex(
|
| + fdesc, pset_i, &pset, &pset_size, nullptr, nullptr);
|
| + if (status != noErr) {
|
| + DLOG(ERROR)
|
| + << " CMVideoFormatDescriptionGetH264ParameterSetAtIndex failed: "
|
| + << status;
|
| + return;
|
| + }
|
| + static const char startcode_4[4] = {0, 0, 0, 1};
|
| + annexb_buffer->append(startcode_4, sizeof(startcode_4));
|
| + 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::ScopedCFTypeRef<CoreMediaGlue::CMBlockBufferRef> contiguous_bb(
|
| + bb, base::scoped_policy::RETAIN);
|
| + if (!CoreMediaGlue::CMBlockBufferIsRangeContiguous(bb, 0, 0)) {
|
| + contiguous_bb.reset();
|
| + status = CoreMediaGlue::CMBlockBufferCreateContiguous(
|
| + kCFAllocatorDefault, bb, kCFAllocatorDefault, nullptr, 0, 0, 0,
|
| + contiguous_bb.InitializeInto());
|
| + if (status != noErr) {
|
| + DLOG(ERROR) << " CMBlockBufferCreateContiguous failed: " << status;
|
| + return;
|
| + }
|
| + }
|
| +
|
| + // Copy all the NAL units. In the process convert them from AVCC format
|
| + // (length header) to AnnexB format (start code).
|
| + char* bb_data;
|
| + status = CoreMediaGlue::CMBlockBufferGetDataPointer(contiguous_bb, 0, nullptr,
|
| + nullptr, &bb_data);
|
| + if (status != noErr) {
|
| + DLOG(ERROR) << " CMBlockBufferGetDataPointer failed: " << status;
|
| + return;
|
| + }
|
| +
|
| + if (nal_size_field_bytes == 1) {
|
| + CopyNalsToAnnexB<uint8_t>(bb_data, bb_size, annexb_buffer);
|
| + } else if (nal_size_field_bytes == 2) {
|
| + CopyNalsToAnnexB<uint16_t>(bb_data, bb_size, annexb_buffer);
|
| + } else if (nal_size_field_bytes == 4) {
|
| + CopyNalsToAnnexB<uint32_t>(bb_data, bb_size, annexb_buffer);
|
| + } else {
|
| + NOTREACHED();
|
| + }
|
| +}
|
| +
|
| +template <typename NalSizeType>
|
| +void CopyNalsToAnnexB(char* avcc_buffer,
|
| + const size_t avcc_size,
|
| + std::string* annexb_buffer) {
|
| + static_assert(sizeof(NalSizeType) == 1 || sizeof(NalSizeType) == 2 ||
|
| + sizeof(NalSizeType) == 4,
|
| + "NAL size type has unsupported size");
|
| + static const char startcode_3[3] = {0, 0, 1};
|
| + DCHECK(avcc_buffer);
|
| + DCHECK(annexb_buffer);
|
| + size_t bytes_left = avcc_size;
|
| + while (bytes_left > 0) {
|
| + DCHECK_GT(bytes_left, sizeof(NalSizeType));
|
| + NalSizeType nal_size;
|
| + base::ReadBigEndian(avcc_buffer, &nal_size);
|
| + bytes_left -= sizeof(NalSizeType);
|
| + avcc_buffer += sizeof(NalSizeType);
|
| +
|
| + DCHECK_GE(bytes_left, nal_size);
|
| + annexb_buffer->append(startcode_3, sizeof(startcode_3));
|
| + annexb_buffer->append(avcc_buffer, nal_size);
|
| + bytes_left -= nal_size;
|
| + avcc_buffer += nal_size;
|
| + }
|
| +}
|
| +
|
| +base::ScopedCFTypeRef<CFDictionaryRef>
|
| +DictionaryWithKeysAndValues(CFTypeRef* keys, CFTypeRef* values, size_t size) {
|
| + return base::ScopedCFTypeRef<CFDictionaryRef>(CFDictionaryCreate(
|
| + kCFAllocatorDefault, keys, values, size, &kCFTypeDictionaryKeyCallBacks,
|
| + &kCFTypeDictionaryValueCallBacks));
|
| +}
|
| +
|
| +base::ScopedCFTypeRef<CFDictionaryRef> DictionaryWithKeyValue(CFTypeRef key,
|
| + CFTypeRef value) {
|
| + CFTypeRef keys[1] = {key};
|
| + CFTypeRef values[1] = {value};
|
| + return DictionaryWithKeysAndValues(keys, values, 1);
|
| +}
|
| +
|
| +base::ScopedCFTypeRef<CFArrayRef> ArrayWithIntegers(const int* v, size_t size) {
|
| + std::vector<CFNumberRef> numbers;
|
| + numbers.reserve(size);
|
| + for (const int* end = v + size; v < end; ++v)
|
| + numbers.push_back(CFNumberCreate(nullptr, kCFNumberSInt32Type, v));
|
| + base::ScopedCFTypeRef<CFArrayRef> array(CFArrayCreate(
|
| + kCFAllocatorDefault, reinterpret_cast<const void**>(&numbers[0]),
|
| + numbers.size(), &kCFTypeArrayCallBacks));
|
| + for (auto& number : numbers) {
|
| + CFRelease(number);
|
| + }
|
| + return array;
|
| +}
|
| +
|
| +bool SetSessionProperty(base::ScopedCFTypeRef<VTCompressionSessionRef> session,
|
| + const VideoToolboxGlue* const glue,
|
| + CFStringRef key,
|
| + int32_t value) {
|
| + base::ScopedCFTypeRef<CFNumberRef> cfvalue(
|
| + CFNumberCreate(nullptr, kCFNumberSInt32Type, &value));
|
| + return glue->VTSessionSetProperty(session, key, cfvalue) == noErr;
|
| +}
|
| +
|
| +bool SetSessionProperty(base::ScopedCFTypeRef<VTCompressionSessionRef> session,
|
| + const VideoToolboxGlue* const glue,
|
| + CFStringRef key,
|
| + bool value) {
|
| + CFBooleanRef cfvalue = (value) ? kCFBooleanTrue : kCFBooleanFalse;
|
| + return glue->VTSessionSetProperty(session, key, cfvalue) == noErr;
|
| +}
|
| +
|
| +bool SetSessionProperty(base::ScopedCFTypeRef<VTCompressionSessionRef> session,
|
| + const VideoToolboxGlue* const glue,
|
| + CFStringRef key,
|
| + CFStringRef value) {
|
| + return glue->VTSessionSetProperty(session, key, value) == noErr;
|
| +}
|
| +
|
| +} // namespace media
|
|
|