Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(6946)

Unified Diff: content/common/gpu/media/vt_video_encode_accelerator_mac.cc

Issue 1636083003: H264 HW encode using VideoToolbox (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: miu@ comments. Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: content/common/gpu/media/vt_video_encode_accelerator_mac.cc
diff --git a/content/common/gpu/media/vt_video_encode_accelerator_mac.cc b/content/common/gpu/media/vt_video_encode_accelerator_mac.cc
new file mode 100644
index 0000000000000000000000000000000000000000..a32ec275867666dbead70ba6f289bd942023ae5b
--- /dev/null
+++ b/content/common/gpu/media/vt_video_encode_accelerator_mac.cc
@@ -0,0 +1,418 @@
+// 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 "content/common/gpu/media/vt_video_encode_accelerator_mac.h"
+
+#include "base/thread_task_runner_handle.h"
+#include "media/base/mac/coremedia_glue.h"
+#include "media/base/mac/corevideo_glue.h"
+#include "media/base/mac/video_frame_mac.h"
+
+namespace content {
+
+namespace {
+
+// Subjectively chosen.
+// TODO(emircan): Check if we can find the actual system capabilities via
+// creating VTCompressionSessions with varying requirements.
+// See crbug.com/584784.
+const size_t kNumInputBuffers = 1;
jfroy 2016/02/10 18:56:27 This is somewhat configurable using kVTCompression
emircan 2016/02/11 09:22:09 For me, setting kVTCompressionPropertyKey_MaxFrame
+const size_t kMaxFrameRateNumerator = 30;
+const size_t kMaxFrameRateDenominator = 1;
+const size_t kMaxResolutionWidth = 4096;
+const size_t kMaxResolutionHeight = 2160;
+// The ratio of |input_visible_size| area to the max expected output
+// BitstreamBuffer size in bytes. VideoToolbox returns variable sized encoded
+// data whereas media::VideoEncodeAccelerator provides a uniform BitstreamBuffer
+// size to fill this data into. This ratio is used to determine a size that
+// would ideally be big enough to fit all frames.
+const size_t kOutputBufferSizeRatio = 10;
+const size_t kBitsPerByte = 8;
+
+} // namespace
+
+struct VTVideoEncodeAccelerator::InProgressFrameEncode {
+ const base::TimeDelta timestamp;
+ const base::TimeTicks reference_time;
+
+ InProgressFrameEncode(base::TimeDelta rtp_timestamp, base::TimeTicks ref_time)
+ : timestamp(rtp_timestamp), reference_time(ref_time) {}
+
+ private:
+ DISALLOW_IMPLICIT_CONSTRUCTORS(InProgressFrameEncode);
+};
+
+struct VTVideoEncodeAccelerator::BitstreamBufferRef {
+ BitstreamBufferRef(int32_t id,
+ scoped_ptr<base::SharedMemory> shm,
+ size_t size)
+ : id(id), shm(std::move(shm)), size(size) {}
+ const int32_t id;
+ const scoped_ptr<base::SharedMemory> shm;
+ const size_t size;
+
+ private:
+ DISALLOW_IMPLICIT_CONSTRUCTORS(BitstreamBufferRef);
+};
+
+VTVideoEncodeAccelerator::VTVideoEncodeAccelerator()
+ : client_task_runner_(base::ThreadTaskRunnerHandle::Get()) {
+}
+
+VTVideoEncodeAccelerator::~VTVideoEncodeAccelerator() {
+ DCHECK(thread_checker_.CalledOnValidThread());
+}
+
+media::VideoEncodeAccelerator::SupportedProfiles
+VTVideoEncodeAccelerator::GetSupportedProfiles() {
+ DVLOG(3) << __FUNCTION__;
+ DCHECK(thread_checker_.CalledOnValidThread());
+
+ SupportedProfiles profiles;
+ SupportedProfile profile;
+ profile.profile = media::H264PROFILE_BASELINE;
+ profile.max_framerate_numerator = kMaxFrameRateNumerator;
+ profile.max_framerate_denominator = kMaxFrameRateDenominator;
+ profile.max_resolution = gfx::Size(kMaxResolutionWidth, kMaxResolutionHeight);
+ profiles.push_back(profile);
+ return profiles;
+}
+
+bool VTVideoEncodeAccelerator::Initialize(
+ media::VideoPixelFormat format,
+ const gfx::Size& input_visible_size,
+ media::VideoCodecProfile output_profile,
+ uint32_t initial_bitrate,
+ Client* client) {
+ DVLOG(3) << __FUNCTION__
+ << ": input_format=" << media::VideoPixelFormatToString(format)
+ << ", input_visible_size=" << input_visible_size.ToString()
+ << ", output_profile=" << output_profile
+ << ", initial_bitrate=" << initial_bitrate;
+ DCHECK(thread_checker_.CalledOnValidThread());
+ DCHECK(client);
+
+ if (media::PIXEL_FORMAT_I420 != format) {
jfroy 2016/02/10 18:56:27 You can also support NV12 pretty easily. I don't k
emircan 2016/02/11 09:22:09 I420 is the common format in Chrome HW encode.
+ DLOG(ERROR) << "Input format not supported= "
+ << media::VideoPixelFormatToString(format);
+ return false;
+ }
+ if (media::H264PROFILE_BASELINE != output_profile) {
+ DLOG(ERROR) << "Output profile not supported= "
+ << output_profile;
+ return false;
+ }
+
+ videotoolbox_glue_ = VideoToolboxGlue::Get();
+ if (!videotoolbox_glue_) {
+ DLOG(ERROR) << "Failed creating VideoToolbox glue";
+ return false;
+ }
+
+ client_ptr_factory_.reset(new base::WeakPtrFactory<Client>(client));
+ client_ = client_ptr_factory_->GetWeakPtr();
+ bitrate_ = initial_bitrate;
+ input_visible_size_ = input_visible_size;
+
+ if (!ResetCompressionSession()) {
+ DLOG(ERROR) << "Failed creating compression session";
+ return false;
+ }
+
+ client_->RequireBitstreamBuffers(
+ kNumInputBuffers, input_visible_size_,
+ std::max(input_visible_size_.GetArea() / kOutputBufferSizeRatio,
+ bitrate_ / kBitsPerByte));
miu 2016/02/10 21:04:56 Sanity-check: Is the bitrate_ setting a "max bitra
jfroy 2016/02/10 23:15:34 Myself and lite@ did extensive tests of the iOS en
emircan 2016/02/11 09:22:09 I will go with jfroy@ suggestions. I will remove t
jfroy 2016/02/11 18:27:40 I would really run a few experiments to see how yo
miu 2016/02/11 21:09:40 I can't remember whether the VEA interface treats
emircan 2016/02/12 03:40:55 I did some further investigation as jfroy@ suggest
Pawel Osciak 2016/02/18 11:16:14 It's not clearly specified in the docs, but it's e
+ return true;
+}
+
+void VTVideoEncodeAccelerator::Encode(
+ const scoped_refptr<media::VideoFrame>& frame,
+ bool force_keyframe) {
+ DVLOG(3) << __FUNCTION__;
+ DCHECK(thread_checker_.CalledOnValidThread());
+ DCHECK(compression_session_);
+ DCHECK(frame);
+
+ base::TimeTicks ref_time;
+ if (!frame->metadata()->GetTimeTicks(
+ media::VideoFrameMetadata::REFERENCE_TIME, &ref_time)) {
+ ref_time = base::TimeTicks::Now();
+ }
+ auto timestamp_cm = CoreMediaGlue::CMTimeMake(
+ frame->timestamp().InMicroseconds(), USEC_PER_SEC);
+ // Wrap information we'll need after the frame is encoded in a heap object.
+ // We'll get the pointer back from the VideoToolbox completion callback.
+ scoped_ptr<InProgressFrameEncode> request(new InProgressFrameEncode(
+ frame->timestamp(), ref_time));
+
+ // TODO(emircan): See if we can eliminate a copy here by using
+ // CVPixelBufferPool for the allocation of incoming VideoFrames.
+ base::ScopedCFTypeRef<CVPixelBufferRef> pixel_buffer =
+ media::WrapVideoFrameInCVPixelBuffer(*frame);
+ base::ScopedCFTypeRef<CFDictionaryRef> frame_props =
+ media::video_toolbox::DictionaryWithKeyValue(
+ videotoolbox_glue_->kVTEncodeFrameOptionKey_ForceKeyFrame(),
+ force_keyframe ? kCFBooleanTrue : kCFBooleanFalse);
+
+ OSStatus status = videotoolbox_glue_->VTCompressionSessionEncodeFrame(
+ compression_session_, pixel_buffer, timestamp_cm,
+ CoreMediaGlue::CMTime{0, 0, 0, 0}, frame_props,
+ reinterpret_cast<void*>(request.release()), nullptr);
+ if (status != noErr) {
+ DLOG(ERROR) << " VTCompressionSessionEncodeFrame failed: " << status;
+ client_->NotifyError(kPlatformFailureError);
+ }
+}
+
+void VTVideoEncodeAccelerator::UseOutputBitstreamBuffer(
+ const media::BitstreamBuffer& buffer) {
+ DVLOG(3) << __FUNCTION__;
+ DCHECK(thread_checker_.CalledOnValidThread());
+ if (buffer.size() < static_cast<size_t>(input_visible_size_.GetArea() /
+ kOutputBufferSizeRatio)) {
+ DLOG(ERROR) << "Output BitstreamBuffer isn't big enough: "
+ << buffer.size()
+ << " vs. "
+ << static_cast<size_t>(input_visible_size_.GetArea() /
+ kOutputBufferSizeRatio);
+ client_->NotifyError(kInvalidArgumentError);
+ return;
+ }
+
+ scoped_ptr<base::SharedMemory> shm(
+ new base::SharedMemory(buffer.handle(), false));
+ if (!shm->Map(buffer.size())) {
+ DLOG(ERROR) << "Failed mapping shared memory.";
+ client_->NotifyError(kPlatformFailureError);
+ return;
+ }
+
+ // If there are already CMSampleBufferRef waiting, copy their output first.
+ if (!encoder_output_sample_buffer_queue_.empty()) {
+ CMSampleBufferRef sbuf = encoder_output_sample_buffer_queue_.front();
+ encoder_output_sample_buffer_queue_.pop_front();
+
+ auto sample_attachments =
+ static_cast<CFDictionaryRef>(CFArrayGetValueAtIndex(
+ CoreMediaGlue::CMSampleBufferGetSampleAttachmentsArray(sbuf, true),
+ 0));
+ const bool keyframe = !CFDictionaryContainsKey(
+ sample_attachments,
+ CoreMediaGlue::kCMSampleAttachmentKey_NotSync());
+ size_t used_buffer_size = 0;
+ const bool copy_rv = media::video_toolbox::CopySampleBufferToAnnexBBuffer(
+ sbuf, keyframe, buffer.size(),
+ reinterpret_cast<uint8_t*>(shm->memory()), &used_buffer_size);
+ CFRelease(sbuf);
+ if (!copy_rv)
+ used_buffer_size = 0;
+ client_->BitstreamBufferReady(buffer.id(), used_buffer_size, keyframe);
+ return;
+ }
+
+ scoped_ptr<BitstreamBufferRef> buffer_ref(
+ new BitstreamBufferRef(buffer.id(), std::move(shm), buffer.size()));
+ encoder_output_queue_.push_back(std::move(buffer_ref));
+}
+
+void VTVideoEncodeAccelerator::RequestEncodingParametersChange(
+ uint32_t bitrate,
+ uint32_t framerate) {
+ DVLOG(3) << __FUNCTION__;
+ DCHECK(thread_checker_.CalledOnValidThread());
+
+ bitrate_ = bitrate > 1 ? bitrate : 1;
+
+ if (!compression_session_) {
+ client_->NotifyError(kPlatformFailureError);
+ return;
+ }
+ // TODO(emircan): VideoToolbox does not seem to support bitrate
jfroy 2016/02/10 18:56:27 See my update to that bug. You can actually contro
emircan 2016/02/11 09:22:09 Done.
+ // reconfiguration, see crbug.com/425352.
+ const bool rv = session_property_setter_->SetSessionProperty(
miu 2016/02/10 21:04:56 Suggestion (to eliminate extra heap-allocated data
emircan 2016/02/11 09:22:09 Done. Keeping it as a class member on stack.
+ videotoolbox_glue_->kVTCompressionPropertyKey_AverageBitRate(),
+ static_cast<int32_t>(bitrate_));
+ if (!rv) {
+ DLOG(ERROR) << "Couldn't change session bitrate.";
+ }
+}
+
+void VTVideoEncodeAccelerator::Destroy() {
+ DVLOG(3) << __FUNCTION__;
+ DCHECK(thread_checker_.CalledOnValidThread());
+
+ DestroyCompressionSession();
+ delete this;
+}
+
+// static
+void VTVideoEncodeAccelerator::CompressionCallback(void* encoder_opaque,
+ void* request_opaque,
+ OSStatus status,
+ VTEncodeInfoFlags info,
+ CMSampleBufferRef sbuf) {
+ // This function may be called asynchronously, on a different thread from the
+ // one that calls VTCompressionSessionEncodeFrame.
+ DVLOG(3) << __FUNCTION__;
+
+ auto encoder = reinterpret_cast<VTVideoEncodeAccelerator*>(encoder_opaque);
+ DCHECK(encoder);
+
+ if (status != noErr) {
+ DLOG(ERROR) << " encode failed: " << status;
+ encoder->client_task_runner_->PostTask(
+ FROM_HERE, base::Bind(&Client::NotifyError, encoder->client_,
+ kPlatformFailureError));
+ return;
+ }
+
+ // Release InProgressFrameEncode, since we don't have support to return
+ // timestamps at this point.
+ scoped_ptr<InProgressFrameEncode> request(
miu 2016/02/10 21:04:56 This is happening after a possible return statemen
emircan 2016/02/11 09:22:09 Done.
+ reinterpret_cast<InProgressFrameEncode*>(request_opaque));
+ request.reset();
+
+ // CFRetain is required to hold onto CMSampleBufferRef when posting task
+ // between threads. The object should be released later using CFRelease.
+ CFRetain(sbuf);
+ // This method is NOT called on |client_task_runner_|, so we still need to
+ // post a task back to it to reach |client_|.
+ encoder->client_task_runner_->PostTask(
+ FROM_HERE,
+ base::Bind(&VTVideoEncodeAccelerator::CompressionCallbackTask,
+ base::Unretained(encoder), info, sbuf));
+}
+
+void VTVideoEncodeAccelerator::CompressionCallbackTask(VTEncodeInfoFlags info,
+ CMSampleBufferRef sbuf) {
+ DVLOG(3) << __FUNCTION__;
+ DCHECK(thread_checker_.CalledOnValidThread());
+
+ // If there isn't any BitstreamBuffer to copy into, add it to a queue for
+ // later use.
+ if (encoder_output_queue_.empty()) {
+ encoder_output_sample_buffer_queue_.push_back(sbuf);
+ return;
miu 2016/02/10 21:04:56 Same problem w.r.t. early return. The client shou
emircan 2016/02/11 09:22:10 I do not have a BitstreamBuffer to return at this
miu 2016/02/11 21:09:40 Acknowledged. My misunderstanding.
+ }
+
+ bool frame_dropped = false;
+ if (info & VideoToolboxGlue::kVTEncodeInfo_FrameDropped) {
+ DVLOG(2) << " frame dropped";
+ frame_dropped = true;
miu 2016/02/10 21:04:56 Looks good here. Just need to do this above too.
emircan 2016/02/11 09:22:09 Done.
+ }
+
+ auto sample_attachments = static_cast<CFDictionaryRef>(CFArrayGetValueAtIndex(
jfroy 2016/02/10 18:56:27 This code seems duplicated from UseOutputBitstream
emircan 2016/02/11 09:22:09 Done.
+ CoreMediaGlue::CMSampleBufferGetSampleAttachmentsArray(sbuf, true), 0));
+ const bool keyframe =
+ !CFDictionaryContainsKey(sample_attachments,
+ CoreMediaGlue::kCMSampleAttachmentKey_NotSync());
+
+ scoped_ptr<VTVideoEncodeAccelerator::BitstreamBufferRef> buffer_ref =
+ std::move(encoder_output_queue_.front());
+ encoder_output_queue_.pop_front();
+
+ size_t used_buffer_size = 0;
+ if (!frame_dropped) {
+ const bool copy_rv = media::video_toolbox::CopySampleBufferToAnnexBBuffer(
+ sbuf, keyframe, buffer_ref->size,
+ reinterpret_cast<uint8_t*>(buffer_ref->shm->memory()),
+ &used_buffer_size);
+ CFRelease(sbuf);
+ if (!copy_rv) {
+ DLOG(ERROR) << "Cannot copy output from SampleBuffer to AnnexBBuffer.";
+ used_buffer_size = 0;
+ }
+ }
+
+ client_->BitstreamBufferReady(buffer_ref->id, used_buffer_size, keyframe);
+}
+
+bool VTVideoEncodeAccelerator::ResetCompressionSession() {
+ DCHECK(thread_checker_.CalledOnValidThread());
+
+ DestroyCompressionSession();
+
+ base::ScopedCFTypeRef<CFDictionaryRef> encoder_spec =
+ media::video_toolbox::DictionaryWithKeyValue(videotoolbox_glue_
+ ->kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder(),
jfroy 2016/02/10 18:56:27 Unless you want to fallback to Apple's *TERRIBLE*
emircan 2016/02/11 09:22:09 Thanks for the notice, I added RequireHardwareAcce
+ kCFBooleanTrue);
+
+ // Keep these in-sync with those in ConfigureCompressionSession().
+ CFTypeRef attributes_keys[] = {
+#if defined(OS_IOS)
+ kCVPixelBufferOpenGLESCompatibilityKey,
+#else
+ kCVPixelBufferOpenGLCompatibilityKey,
+#endif
+ kCVPixelBufferIOSurfacePropertiesKey,
+ kCVPixelBufferPixelFormatTypeKey
+ };
+ const int format[] = {
+ CoreVideoGlue::kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange};
+ CFTypeRef attributes_values[] = {
+ kCFBooleanTrue,
+ media::video_toolbox::DictionaryWithKeysAndValues(nullptr, nullptr, 0)
+ .release(),
+ media::video_toolbox::ArrayWithIntegers(format, arraysize(format))
+ .release()};
+ const base::ScopedCFTypeRef<CFDictionaryRef> attributes =
+ media::video_toolbox::DictionaryWithKeysAndValues(
+ attributes_keys, attributes_values, arraysize(attributes_keys));
+ for (auto& v : attributes_values)
+ CFRelease(v);
+
+ // Create the compression session.
+ OSStatus status = videotoolbox_glue_->VTCompressionSessionCreate(
+ kCFAllocatorDefault,
+ input_visible_size_.width(),
+ input_visible_size_.height(),
+ CoreMediaGlue::kCMVideoCodecType_H264,
+ encoder_spec,
+ attributes,
+ nullptr /* compressedDataAllocator */,
+ &VTVideoEncodeAccelerator::CompressionCallback,
+ reinterpret_cast<void*>(this),
+ compression_session_.InitializeInto());
+ if (status != noErr) {
+ DLOG(ERROR) << " VTCompressionSessionCreate failed: " << status;
+ return false;
+ }
+
+ return ConfigureCompressionSession();
+}
+
+bool VTVideoEncodeAccelerator::ConfigureCompressionSession() {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ DCHECK(compression_session_);
+
+ session_property_setter_.reset(
+ new media::video_toolbox::SessionPropertySetter(compression_session_,
+ videotoolbox_glue_));
+ bool rv = true;
+ rv &= session_property_setter_->SetSessionProperty(
+ videotoolbox_glue_->kVTCompressionPropertyKey_ProfileLevel(),
+ videotoolbox_glue_->kVTProfileLevel_H264_Baseline_AutoLevel());
+ rv &= session_property_setter_->SetSessionProperty(
+ videotoolbox_glue_->kVTCompressionPropertyKey_RealTime(), true);
+ rv &= session_property_setter_->SetSessionProperty(
+ videotoolbox_glue_->kVTCompressionPropertyKey_AverageBitRate(),
+ static_cast<int32_t>(bitrate_));
+ rv &= session_property_setter_->SetSessionProperty(
+ videotoolbox_glue_->kVTCompressionPropertyKey_AllowFrameReordering(),
+ false);
+ DLOG_IF(ERROR, !rv) << " SetSessionProperty failed.";
+ return rv;
+}
+
+void VTVideoEncodeAccelerator::DestroyCompressionSession() {
+ DCHECK(thread_checker_.CalledOnValidThread());
+
+ if (compression_session_) {
+ videotoolbox_glue_->VTCompressionSessionInvalidate(compression_session_);
+ compression_session_.reset();
+ }
+}
+
+} // namespace content

Powered by Google App Engine
This is Rietveld 408576698