1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 #include <CoreVideo/CoreVideo.h>
8 #include <OpenGL/CGLIOSurface.h>
11 #include "base/bind.h"
12 #include "base/command_line.h"
13 #include "base/logging.h"
14 #include "base/mac/mac_logging.h"
15 #include "base/metrics/histogram_macros.h"
16 #include "base/sys_byteorder.h"
17 #include "base/sys_info.h"
18 #include "base/thread_task_runner_handle.h"
19 #include "base/version.h"
20 #include "content/common/gpu/media/vt_video_decode_accelerator.h"
21 #include "content/public/common/content_switches.h"
22 #include "media/base/limits.h"
23 #include "ui/gl/gl_context.h"
24 #include "ui/gl/scoped_binders.h"
26 using content_common_gpu_media::kModuleVt
;
27 using content_common_gpu_media::InitializeStubs
;
28 using content_common_gpu_media::IsVtInitialized
;
29 using content_common_gpu_media::StubPathMap
;
31 #define NOTIFY_STATUS(name, status, session_failure) \
33 OSSTATUS_DLOG(ERROR, status) << name; \
34 NotifyError(PLATFORM_FAILURE, session_failure); \
39 // Only H.264 with 4:2:0 chroma sampling is supported.
40 static const media::VideoCodecProfile kSupportedProfiles
[] = {
41 media::H264PROFILE_BASELINE
,
42 media::H264PROFILE_MAIN
,
43 media::H264PROFILE_EXTENDED
,
44 media::H264PROFILE_HIGH
,
45 media::H264PROFILE_HIGH10PROFILE
,
46 media::H264PROFILE_SCALABLEBASELINE
,
47 media::H264PROFILE_SCALABLEHIGH
,
48 media::H264PROFILE_STEREOHIGH
,
49 media::H264PROFILE_MULTIVIEWHIGH
,
52 // Size to use for NALU length headers in AVC format (can be 1, 2, or 4).
53 static const int kNALUHeaderLength
= 4;
55 // We request 5 picture buffers from the client, each of which has a texture ID
56 // that we can bind decoded frames to. We need enough to satisfy preroll, and
57 // enough to avoid unnecessary stalling, but no more than that. The resource
58 // requirements are low, as we don't need the textures to be backed by storage.
59 static const int kNumPictureBuffers
= media::limits::kMaxVideoFrames
+ 1;
61 // Maximum number of frames to queue for reordering before we stop asking for
62 // more. (NotifyEndOfBitstreamBuffer() is called when frames are moved into the
64 static const int kMaxReorderQueueSize
= 16;
66 // When set to false, always create a new decoder instead of reusing the
67 // existing configuration when the configuration changes. This works around a
68 // bug in VideoToolbox that results in corruption before Mac OS X 10.10.3. The
69 // value is set in InitializeVideoToolbox().
70 static bool g_enable_compatible_configuration_reuse
= true;
72 // Build an |image_config| dictionary for VideoToolbox initialization.
73 static base::ScopedCFTypeRef
<CFMutableDictionaryRef
>
74 BuildImageConfig(CMVideoDimensions coded_dimensions
) {
75 base::ScopedCFTypeRef
<CFMutableDictionaryRef
> image_config
;
77 // 4:2:2 is used over the native 4:2:0 because only 4:2:2 can be directly
78 // bound to a texture by CGLTexImageIOSurface2D().
79 int32_t pixel_format
= kCVPixelFormatType_422YpCbCr8
;
80 #define CFINT(i) CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &i)
81 base::ScopedCFTypeRef
<CFNumberRef
> cf_pixel_format(CFINT(pixel_format
));
82 base::ScopedCFTypeRef
<CFNumberRef
> cf_width(CFINT(coded_dimensions
.width
));
83 base::ScopedCFTypeRef
<CFNumberRef
> cf_height(CFINT(coded_dimensions
.height
));
85 if (!cf_pixel_format
.get() || !cf_width
.get() || !cf_height
.get())
89 CFDictionaryCreateMutable(
92 &kCFTypeDictionaryKeyCallBacks
,
93 &kCFTypeDictionaryValueCallBacks
));
94 if (!image_config
.get())
97 CFDictionarySetValue(image_config
, kCVPixelBufferPixelFormatTypeKey
,
99 CFDictionarySetValue(image_config
, kCVPixelBufferWidthKey
, cf_width
);
100 CFDictionarySetValue(image_config
, kCVPixelBufferHeightKey
, cf_height
);
101 CFDictionarySetValue(image_config
, kCVPixelBufferOpenGLCompatibilityKey
,
107 // Create a VTDecompressionSession using the provided |pps| and |sps|. If
108 // |require_hardware| is true, the session must uses real hardware decoding
109 // (as opposed to software decoding inside of VideoToolbox) to be considered
112 // TODO(sandersd): Merge with ConfigureDecoder(), as the code is very similar.
113 static bool CreateVideoToolboxSession(const uint8_t* sps
, size_t sps_size
,
114 const uint8_t* pps
, size_t pps_size
,
115 bool require_hardware
) {
116 const uint8_t* data_ptrs
[] = {sps
, pps
};
117 const size_t data_sizes
[] = {sps_size
, pps_size
};
119 base::ScopedCFTypeRef
<CMFormatDescriptionRef
> format
;
120 OSStatus status
= CMVideoFormatDescriptionCreateFromH264ParameterSets(
122 2, // parameter_set_count
123 data_ptrs
, // ¶meter_set_pointers
124 data_sizes
, // ¶meter_set_sizes
125 kNALUHeaderLength
, // nal_unit_header_length
126 format
.InitializeInto());
128 OSSTATUS_DLOG(WARNING
, status
)
129 << "Failed to create CMVideoFormatDescription.";
133 base::ScopedCFTypeRef
<CFMutableDictionaryRef
> decoder_config(
134 CFDictionaryCreateMutable(
137 &kCFTypeDictionaryKeyCallBacks
,
138 &kCFTypeDictionaryValueCallBacks
));
139 if (!decoder_config
.get())
142 if (require_hardware
) {
143 CFDictionarySetValue(
145 // kVTVideoDecoderSpecification_RequireHardwareAcceleratedVideoDecoder
146 CFSTR("RequireHardwareAcceleratedVideoDecoder"),
150 base::ScopedCFTypeRef
<CFMutableDictionaryRef
> image_config(
151 BuildImageConfig(CMVideoFormatDescriptionGetDimensions(format
)));
152 if (!image_config
.get())
155 VTDecompressionOutputCallbackRecord callback
= {0};
157 base::ScopedCFTypeRef
<VTDecompressionSessionRef
> session
;
158 status
= VTDecompressionSessionCreate(
160 format
, // video_format_description
161 decoder_config
, // video_decoder_specification
162 image_config
, // destination_image_buffer_attributes
163 &callback
, // output_callback
164 session
.InitializeInto());
166 OSSTATUS_DLOG(WARNING
, status
) << "Failed to create VTDecompressionSession";
173 // The purpose of this function is to preload the generic and hardware-specific
174 // libraries required by VideoToolbox before the GPU sandbox is enabled.
175 // VideoToolbox normally loads the hardware-specific libraries lazily, so we
176 // must actually create a decompression session. If creating a decompression
177 // session fails, hardware decoding will be disabled (Initialize() will always
179 static bool InitializeVideoToolboxInternal() {
180 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
181 switches::kDisableAcceleratedVideoDecode
)) {
185 if (!IsVtInitialized()) {
186 // CoreVideo is also required, but the loader stops after the first path is
187 // loaded. Instead we rely on the transitive dependency from VideoToolbox to
189 // TODO(sandersd): Fallback to PrivateFrameworks to support OS X < 10.8.
191 paths
[kModuleVt
].push_back(FILE_PATH_LITERAL(
192 "/System/Library/Frameworks/VideoToolbox.framework/VideoToolbox"));
193 if (!InitializeStubs(paths
)) {
194 LOG(WARNING
) << "Failed to initialize VideoToolbox framework. "
195 << "Hardware accelerated video decoding will be disabled.";
200 // Create a hardware decoding session.
201 // SPS and PPS data are taken from a 480p sample (buck2.mp4).
202 const uint8_t sps_normal
[] = {0x67, 0x64, 0x00, 0x1e, 0xac, 0xd9, 0x80, 0xd4,
203 0x3d, 0xa1, 0x00, 0x00, 0x03, 0x00, 0x01, 0x00,
204 0x00, 0x03, 0x00, 0x30, 0x8f, 0x16, 0x2d, 0x9a};
205 const uint8_t pps_normal
[] = {0x68, 0xe9, 0x7b, 0xcb};
206 if (!CreateVideoToolboxSession(sps_normal
, arraysize(sps_normal
), pps_normal
,
207 arraysize(pps_normal
), true)) {
208 LOG(WARNING
) << "Failed to create hardware VideoToolbox session. "
209 << "Hardware accelerated video decoding will be disabled.";
213 // Create a software decoding session.
214 // SPS and PPS data are taken from a 18p sample (small2.mp4).
215 const uint8_t sps_small
[] = {0x67, 0x64, 0x00, 0x0a, 0xac, 0xd9, 0x89, 0x7e,
216 0x22, 0x10, 0x00, 0x00, 0x3e, 0x90, 0x00, 0x0e,
217 0xa6, 0x08, 0xf1, 0x22, 0x59, 0xa0};
218 const uint8_t pps_small
[] = {0x68, 0xe9, 0x79, 0x72, 0xc0};
219 if (!CreateVideoToolboxSession(sps_small
, arraysize(sps_small
), pps_small
,
220 arraysize(pps_small
), false)) {
221 LOG(WARNING
) << "Failed to create software VideoToolbox session. "
222 << "Hardware accelerated video decoding will be disabled.";
226 // Set |g_enable_compatible_configuration_reuse| to false on
227 // Mac OS X < 10.10.3.
228 base::Version
os_x_version(base::SysInfo::OperatingSystemVersion());
229 if (os_x_version
.IsOlderThan("10.10.3"))
230 g_enable_compatible_configuration_reuse
= false;
235 bool InitializeVideoToolbox() {
236 // InitializeVideoToolbox() is called only from the GPU process main thread;
237 // once for sandbox warmup, and then once each time a VTVideoDecodeAccelerator
239 static bool attempted
= false;
240 static bool succeeded
= false;
244 succeeded
= InitializeVideoToolboxInternal();
250 // Route decoded frame callbacks back into the VTVideoDecodeAccelerator.
251 static void OutputThunk(
252 void* decompression_output_refcon
,
253 void* source_frame_refcon
,
255 VTDecodeInfoFlags info_flags
,
256 CVImageBufferRef image_buffer
,
257 CMTime presentation_time_stamp
,
258 CMTime presentation_duration
) {
259 VTVideoDecodeAccelerator
* vda
=
260 reinterpret_cast<VTVideoDecodeAccelerator
*>(decompression_output_refcon
);
261 vda
->Output(source_frame_refcon
, status
, image_buffer
);
264 VTVideoDecodeAccelerator::Task::Task(TaskType type
) : type(type
) {
267 VTVideoDecodeAccelerator::Task::~Task() {
270 VTVideoDecodeAccelerator::Frame::Frame(int32_t bitstream_id
)
271 : bitstream_id(bitstream_id
), pic_order_cnt(0), reorder_window(0) {
274 VTVideoDecodeAccelerator::Frame::~Frame() {
277 bool VTVideoDecodeAccelerator::FrameOrder::operator()(
278 const linked_ptr
<Frame
>& lhs
,
279 const linked_ptr
<Frame
>& rhs
) const {
280 if (lhs
->pic_order_cnt
!= rhs
->pic_order_cnt
)
281 return lhs
->pic_order_cnt
> rhs
->pic_order_cnt
;
282 // If |pic_order_cnt| is the same, fall back on using the bitstream order.
283 // TODO(sandersd): Assign a sequence number in Decode() and use that instead.
284 // TODO(sandersd): Using the sequence number, ensure that frames older than
285 // |kMaxReorderQueueSize| are ordered first, regardless of |pic_order_cnt|.
286 return lhs
->bitstream_id
> rhs
->bitstream_id
;
289 VTVideoDecodeAccelerator::VTVideoDecodeAccelerator(
290 const base::Callback
<bool(void)>& make_context_current
)
291 : make_context_current_(make_context_current
),
293 state_(STATE_DECODING
),
298 gpu_task_runner_(base::ThreadTaskRunnerHandle::Get()),
299 decoder_thread_("VTDecoderThread"),
300 weak_this_factory_(this) {
301 DCHECK(!make_context_current_
.is_null());
302 callback_
.decompressionOutputCallback
= OutputThunk
;
303 callback_
.decompressionOutputRefCon
= this;
304 weak_this_
= weak_this_factory_
.GetWeakPtr();
307 VTVideoDecodeAccelerator::~VTVideoDecodeAccelerator() {
310 bool VTVideoDecodeAccelerator::Initialize(
311 media::VideoCodecProfile profile
,
313 DCHECK(gpu_thread_checker_
.CalledOnValidThread());
316 if (!InitializeVideoToolbox())
319 bool profile_supported
= false;
320 for (const auto& supported_profile
: kSupportedProfiles
) {
321 if (profile
== supported_profile
) {
322 profile_supported
= true;
326 if (!profile_supported
)
329 // Spawn a thread to handle parsing and calling VideoToolbox.
330 if (!decoder_thread_
.Start())
333 // Count the session as successfully initialized.
334 UMA_HISTOGRAM_ENUMERATION("Media.VTVDA.SessionFailureReason",
335 SFT_SUCCESSFULLY_INITIALIZED
,
340 bool VTVideoDecodeAccelerator::FinishDelayedFrames() {
341 DCHECK(decoder_thread_
.task_runner()->BelongsToCurrentThread());
343 OSStatus status
= VTDecompressionSessionWaitForAsynchronousFrames(session_
);
345 NOTIFY_STATUS("VTDecompressionSessionWaitForAsynchronousFrames()",
346 status
, SFT_PLATFORM_ERROR
);
353 bool VTVideoDecodeAccelerator::ConfigureDecoder() {
354 DCHECK(decoder_thread_
.task_runner()->BelongsToCurrentThread());
355 DCHECK(!last_sps_
.empty());
356 DCHECK(!last_pps_
.empty());
358 // Build the configuration records.
359 std::vector
<const uint8_t*> nalu_data_ptrs
;
360 std::vector
<size_t> nalu_data_sizes
;
361 nalu_data_ptrs
.reserve(3);
362 nalu_data_sizes
.reserve(3);
363 nalu_data_ptrs
.push_back(&last_sps_
.front());
364 nalu_data_sizes
.push_back(last_sps_
.size());
365 if (!last_spsext_
.empty()) {
366 nalu_data_ptrs
.push_back(&last_spsext_
.front());
367 nalu_data_sizes
.push_back(last_spsext_
.size());
369 nalu_data_ptrs
.push_back(&last_pps_
.front());
370 nalu_data_sizes
.push_back(last_pps_
.size());
372 // Construct a new format description from the parameter sets.
373 // TODO(sandersd): Replace this with custom code to support OS X < 10.9.
375 OSStatus status
= CMVideoFormatDescriptionCreateFromH264ParameterSets(
377 nalu_data_ptrs
.size(), // parameter_set_count
378 &nalu_data_ptrs
.front(), // ¶meter_set_pointers
379 &nalu_data_sizes
.front(), // ¶meter_set_sizes
380 kNALUHeaderLength
, // nal_unit_header_length
381 format_
.InitializeInto());
383 NOTIFY_STATUS("CMVideoFormatDescriptionCreateFromH264ParameterSets()",
384 status
, SFT_PLATFORM_ERROR
);
388 // Store the new configuration data.
389 CMVideoDimensions coded_dimensions
=
390 CMVideoFormatDescriptionGetDimensions(format_
);
391 coded_size_
.SetSize(coded_dimensions
.width
, coded_dimensions
.height
);
393 // If the session is compatible, there's nothing else to do.
394 if (g_enable_compatible_configuration_reuse
&& session_
&&
395 VTDecompressionSessionCanAcceptFormatDescription(session_
, format_
)) {
399 // Prepare VideoToolbox configuration dictionaries.
400 base::ScopedCFTypeRef
<CFMutableDictionaryRef
> decoder_config(
401 CFDictionaryCreateMutable(
404 &kCFTypeDictionaryKeyCallBacks
,
405 &kCFTypeDictionaryValueCallBacks
));
406 if (!decoder_config
.get()) {
407 DLOG(ERROR
) << "Failed to create CFMutableDictionary.";
408 NotifyError(PLATFORM_FAILURE
, SFT_PLATFORM_ERROR
);
412 CFDictionarySetValue(
414 // kVTVideoDecoderSpecification_EnableHardwareAcceleratedVideoDecoder
415 CFSTR("EnableHardwareAcceleratedVideoDecoder"),
418 base::ScopedCFTypeRef
<CFMutableDictionaryRef
> image_config(
419 BuildImageConfig(coded_dimensions
));
420 if (!image_config
.get()) {
421 DLOG(ERROR
) << "Failed to create decoder image configuration.";
422 NotifyError(PLATFORM_FAILURE
, SFT_PLATFORM_ERROR
);
426 // Ensure that the old decoder emits all frames before the new decoder can
428 if (!FinishDelayedFrames())
432 status
= VTDecompressionSessionCreate(
434 format_
, // video_format_description
435 decoder_config
, // video_decoder_specification
436 image_config
, // destination_image_buffer_attributes
437 &callback_
, // output_callback
438 session_
.InitializeInto());
440 NOTIFY_STATUS("VTDecompressionSessionCreate()", status
,
441 SFT_UNSUPPORTED_STREAM_PARAMETERS
);
445 // Report whether hardware decode is being used.
446 bool using_hardware
= false;
447 base::ScopedCFTypeRef
<CFBooleanRef
> cf_using_hardware
;
448 if (VTSessionCopyProperty(
450 // kVTDecompressionPropertyKey_UsingHardwareAcceleratedVideoDecoder
451 CFSTR("UsingHardwareAcceleratedVideoDecoder"),
453 cf_using_hardware
.InitializeInto()) == 0) {
454 using_hardware
= CFBooleanGetValue(cf_using_hardware
);
456 UMA_HISTOGRAM_BOOLEAN("Media.VTVDA.HardwareAccelerated", using_hardware
);
461 void VTVideoDecodeAccelerator::DecodeTask(
462 const media::BitstreamBuffer
& bitstream
,
464 DCHECK(decoder_thread_
.task_runner()->BelongsToCurrentThread());
466 // Map the bitstream buffer.
467 base::SharedMemory
memory(bitstream
.handle(), true);
468 size_t size
= bitstream
.size();
469 if (!memory
.Map(size
)) {
470 DLOG(ERROR
) << "Failed to map bitstream buffer";
471 NotifyError(PLATFORM_FAILURE
, SFT_PLATFORM_ERROR
);
474 const uint8_t* buf
= static_cast<uint8_t*>(memory
.memory());
476 // NALUs are stored with Annex B format in the bitstream buffer (start codes),
477 // but VideoToolbox expects AVC format (length headers), so we must rewrite
480 // Locate relevant NALUs and compute the size of the rewritten data. Also
481 // record any parameter sets for VideoToolbox initialization.
482 std::vector
<uint8_t> sps
;
483 std::vector
<uint8_t> spsext
;
484 std::vector
<uint8_t> pps
;
485 bool has_slice
= false;
486 size_t data_size
= 0;
487 std::vector
<media::H264NALU
> nalus
;
488 parser_
.SetStream(buf
, size
);
489 media::H264NALU nalu
;
491 media::H264Parser::Result result
= parser_
.AdvanceToNextNALU(&nalu
);
492 if (result
== media::H264Parser::kEOStream
)
494 if (result
== media::H264Parser::kUnsupportedStream
) {
495 DLOG(ERROR
) << "Unsupported H.264 stream";
496 NotifyError(PLATFORM_FAILURE
, SFT_UNSUPPORTED_STREAM
);
499 if (result
!= media::H264Parser::kOk
) {
500 DLOG(ERROR
) << "Failed to parse H.264 stream";
501 NotifyError(UNREADABLE_INPUT
, SFT_INVALID_STREAM
);
504 switch (nalu
.nal_unit_type
) {
505 case media::H264NALU::kSPS
:
506 result
= parser_
.ParseSPS(&last_sps_id_
);
507 if (result
== media::H264Parser::kUnsupportedStream
) {
508 DLOG(ERROR
) << "Unsupported SPS";
509 NotifyError(PLATFORM_FAILURE
, SFT_UNSUPPORTED_STREAM
);
512 if (result
!= media::H264Parser::kOk
) {
513 DLOG(ERROR
) << "Could not parse SPS";
514 NotifyError(UNREADABLE_INPUT
, SFT_INVALID_STREAM
);
517 sps
.assign(nalu
.data
, nalu
.data
+ nalu
.size
);
521 case media::H264NALU::kSPSExt
:
522 // TODO(sandersd): Check that the previous NALU was an SPS.
523 spsext
.assign(nalu
.data
, nalu
.data
+ nalu
.size
);
526 case media::H264NALU::kPPS
:
527 result
= parser_
.ParsePPS(&last_pps_id_
);
528 if (result
== media::H264Parser::kUnsupportedStream
) {
529 DLOG(ERROR
) << "Unsupported PPS";
530 NotifyError(PLATFORM_FAILURE
, SFT_UNSUPPORTED_STREAM
);
533 if (result
!= media::H264Parser::kOk
) {
534 DLOG(ERROR
) << "Could not parse PPS";
535 NotifyError(UNREADABLE_INPUT
, SFT_INVALID_STREAM
);
538 pps
.assign(nalu
.data
, nalu
.data
+ nalu
.size
);
541 case media::H264NALU::kSliceDataA
:
542 case media::H264NALU::kSliceDataB
:
543 case media::H264NALU::kSliceDataC
:
544 case media::H264NALU::kNonIDRSlice
:
545 // TODO(sandersd): Check that there has been an IDR slice since the
547 case media::H264NALU::kIDRSlice
:
548 // Compute the |pic_order_cnt| for the picture from the first slice.
549 // TODO(sandersd): Make sure that any further slices are part of the
550 // same picture or a redundant coded picture.
552 media::H264SliceHeader slice_hdr
;
553 result
= parser_
.ParseSliceHeader(nalu
, &slice_hdr
);
554 if (result
== media::H264Parser::kUnsupportedStream
) {
555 DLOG(ERROR
) << "Unsupported slice header";
556 NotifyError(PLATFORM_FAILURE
, SFT_UNSUPPORTED_STREAM
);
559 if (result
!= media::H264Parser::kOk
) {
560 DLOG(ERROR
) << "Could not parse slice header";
561 NotifyError(UNREADABLE_INPUT
, SFT_INVALID_STREAM
);
565 // TODO(sandersd): Maintain a cache of configurations and reconfigure
566 // when a slice references a new config.
567 DCHECK_EQ(slice_hdr
.pic_parameter_set_id
, last_pps_id_
);
568 const media::H264PPS
* pps
=
569 parser_
.GetPPS(slice_hdr
.pic_parameter_set_id
);
571 DLOG(ERROR
) << "Mising PPS referenced by slice";
572 NotifyError(UNREADABLE_INPUT
, SFT_INVALID_STREAM
);
576 DCHECK_EQ(pps
->seq_parameter_set_id
, last_sps_id_
);
577 const media::H264SPS
* sps
= parser_
.GetSPS(pps
->seq_parameter_set_id
);
579 DLOG(ERROR
) << "Mising SPS referenced by PPS";
580 NotifyError(UNREADABLE_INPUT
, SFT_INVALID_STREAM
);
584 if (!poc_
.ComputePicOrderCnt(sps
, slice_hdr
, &frame
->pic_order_cnt
)) {
585 DLOG(ERROR
) << "Unable to compute POC";
586 NotifyError(UNREADABLE_INPUT
, SFT_INVALID_STREAM
);
590 if (sps
->vui_parameters_present_flag
&&
591 sps
->bitstream_restriction_flag
) {
592 frame
->reorder_window
= std::min(sps
->max_num_reorder_frames
,
593 kMaxReorderQueueSize
- 1);
598 nalus
.push_back(nalu
);
599 data_size
+= kNALUHeaderLength
+ nalu
.size
;
604 // Initialize VideoToolbox.
605 bool config_changed
= false;
606 if (!sps
.empty() && sps
!= last_sps_
) {
608 last_spsext_
.swap(spsext
);
609 config_changed
= true;
611 if (!pps
.empty() && pps
!= last_pps_
) {
613 config_changed
= true;
615 if (config_changed
) {
616 if (last_sps_
.empty()) {
617 DLOG(ERROR
) << "Invalid configuration; no SPS";
618 NotifyError(INVALID_ARGUMENT
, SFT_INVALID_STREAM
);
621 if (last_pps_
.empty()) {
622 DLOG(ERROR
) << "Invalid configuration; no PPS";
623 NotifyError(INVALID_ARGUMENT
, SFT_INVALID_STREAM
);
626 if (!ConfigureDecoder())
630 // If there are no image slices, drop the bitstream buffer by returning an
633 if (!FinishDelayedFrames())
635 gpu_task_runner_
->PostTask(FROM_HERE
, base::Bind(
636 &VTVideoDecodeAccelerator::DecodeDone
, weak_this_
, frame
));
640 // If the session is not configured by this point, fail.
642 DLOG(ERROR
) << "Cannot decode without configuration";
643 NotifyError(INVALID_ARGUMENT
, SFT_INVALID_STREAM
);
647 // Update the frame metadata with configuration data.
648 frame
->coded_size
= coded_size_
;
650 // Create a memory-backed CMBlockBuffer for the translated data.
651 // TODO(sandersd): Pool of memory blocks.
652 base::ScopedCFTypeRef
<CMBlockBufferRef
> data
;
653 OSStatus status
= CMBlockBufferCreateWithMemoryBlock(
655 nullptr, // &memory_block
656 data_size
, // block_length
657 kCFAllocatorDefault
, // block_allocator
658 nullptr, // &custom_block_source
660 data_size
, // data_length
662 data
.InitializeInto());
664 NOTIFY_STATUS("CMBlockBufferCreateWithMemoryBlock()", status
,
669 // Make sure that the memory is actually allocated.
670 // CMBlockBufferReplaceDataBytes() is documented to do this, but prints a
671 // message each time starting in Mac OS X 10.10.
672 status
= CMBlockBufferAssureBlockMemory(data
);
674 NOTIFY_STATUS("CMBlockBufferAssureBlockMemory()", status
,
679 // Copy NALU data into the CMBlockBuffer, inserting length headers.
681 for (size_t i
= 0; i
< nalus
.size(); i
++) {
682 media::H264NALU
& nalu
= nalus
[i
];
683 uint32_t header
= base::HostToNet32(static_cast<uint32_t>(nalu
.size
));
684 status
= CMBlockBufferReplaceDataBytes(
685 &header
, data
, offset
, kNALUHeaderLength
);
687 NOTIFY_STATUS("CMBlockBufferReplaceDataBytes()", status
,
691 offset
+= kNALUHeaderLength
;
692 status
= CMBlockBufferReplaceDataBytes(nalu
.data
, data
, offset
, nalu
.size
);
694 NOTIFY_STATUS("CMBlockBufferReplaceDataBytes()", status
,
701 // Package the data in a CMSampleBuffer.
702 base::ScopedCFTypeRef
<CMSampleBufferRef
> sample
;
703 status
= CMSampleBufferCreate(
707 nullptr, // make_data_ready_callback
708 nullptr, // make_data_ready_refcon
709 format_
, // format_description
711 0, // num_sample_timing_entries
712 nullptr, // &sample_timing_array
713 1, // num_sample_size_entries
714 &data_size
, // &sample_size_array
715 sample
.InitializeInto());
717 NOTIFY_STATUS("CMSampleBufferCreate()", status
, SFT_PLATFORM_ERROR
);
721 // Send the frame for decoding.
722 // Asynchronous Decompression allows for parallel submission of frames
723 // (without it, DecodeFrame() does not return until the frame has been
724 // decoded). We don't enable Temporal Processing so that frames are always
725 // returned in decode order; this makes it easier to avoid deadlock.
726 VTDecodeFrameFlags decode_flags
=
727 kVTDecodeFrame_EnableAsynchronousDecompression
;
728 status
= VTDecompressionSessionDecodeFrame(
730 sample
, // sample_buffer
731 decode_flags
, // decode_flags
732 reinterpret_cast<void*>(frame
), // source_frame_refcon
733 nullptr); // &info_flags_out
735 NOTIFY_STATUS("VTDecompressionSessionDecodeFrame()", status
,
741 // This method may be called on any VideoToolbox thread.
742 void VTVideoDecodeAccelerator::Output(
743 void* source_frame_refcon
,
745 CVImageBufferRef image_buffer
) {
747 NOTIFY_STATUS("Decoding", status
, SFT_DECODE_ERROR
);
751 // The type of |image_buffer| is CVImageBuffer, but we only handle
752 // CVPixelBuffers. This should be guaranteed as we set
753 // kCVPixelBufferOpenGLCompatibilityKey in |image_config|.
755 // Sometimes, for unknown reasons (http://crbug.com/453050), |image_buffer| is
756 // NULL, which causes CFGetTypeID() to crash. While the rest of the code would
757 // smoothly handle NULL as a dropped frame, we choose to fail permanantly here
758 // until the issue is better understood.
759 if (!image_buffer
|| CFGetTypeID(image_buffer
) != CVPixelBufferGetTypeID()) {
760 DLOG(ERROR
) << "Decoded frame is not a CVPixelBuffer";
761 NotifyError(PLATFORM_FAILURE
, SFT_DECODE_ERROR
);
765 Frame
* frame
= reinterpret_cast<Frame
*>(source_frame_refcon
);
766 frame
->image
.reset(image_buffer
, base::scoped_policy::RETAIN
);
767 gpu_task_runner_
->PostTask(FROM_HERE
, base::Bind(
768 &VTVideoDecodeAccelerator::DecodeDone
, weak_this_
, frame
));
771 void VTVideoDecodeAccelerator::DecodeDone(Frame
* frame
) {
772 DCHECK(gpu_thread_checker_
.CalledOnValidThread());
773 DCHECK_EQ(1u, pending_frames_
.count(frame
->bitstream_id
));
774 Task
task(TASK_FRAME
);
775 task
.frame
= pending_frames_
[frame
->bitstream_id
];
776 pending_frames_
.erase(frame
->bitstream_id
);
777 task_queue_
.push(task
);
781 void VTVideoDecodeAccelerator::FlushTask(TaskType type
) {
782 DCHECK(decoder_thread_
.task_runner()->BelongsToCurrentThread());
783 FinishDelayedFrames();
785 // Always queue a task, even if FinishDelayedFrames() fails, so that
786 // destruction always completes.
787 gpu_task_runner_
->PostTask(FROM_HERE
, base::Bind(
788 &VTVideoDecodeAccelerator::FlushDone
, weak_this_
, type
));
791 void VTVideoDecodeAccelerator::FlushDone(TaskType type
) {
792 DCHECK(gpu_thread_checker_
.CalledOnValidThread());
793 task_queue_
.push(Task(type
));
797 void VTVideoDecodeAccelerator::Decode(const media::BitstreamBuffer
& bitstream
) {
798 DCHECK(gpu_thread_checker_
.CalledOnValidThread());
799 DCHECK_EQ(0u, assigned_bitstream_ids_
.count(bitstream
.id()));
800 assigned_bitstream_ids_
.insert(bitstream
.id());
801 Frame
* frame
= new Frame(bitstream
.id());
802 pending_frames_
[frame
->bitstream_id
] = make_linked_ptr(frame
);
803 decoder_thread_
.task_runner()->PostTask(
804 FROM_HERE
, base::Bind(&VTVideoDecodeAccelerator::DecodeTask
,
805 base::Unretained(this), bitstream
, frame
));
808 void VTVideoDecodeAccelerator::AssignPictureBuffers(
809 const std::vector
<media::PictureBuffer
>& pictures
) {
810 DCHECK(gpu_thread_checker_
.CalledOnValidThread());
812 for (const media::PictureBuffer
& picture
: pictures
) {
813 DCHECK(!texture_ids_
.count(picture
.id()));
814 assigned_picture_ids_
.insert(picture
.id());
815 available_picture_ids_
.push_back(picture
.id());
816 texture_ids_
[picture
.id()] = picture
.texture_id();
819 // Pictures are not marked as uncleared until after this method returns, and
820 // they will be broken if they are used before that happens. So, schedule
821 // future work after that happens.
822 gpu_task_runner_
->PostTask(FROM_HERE
, base::Bind(
823 &VTVideoDecodeAccelerator::ProcessWorkQueues
, weak_this_
));
826 void VTVideoDecodeAccelerator::ReusePictureBuffer(int32_t picture_id
) {
827 DCHECK(gpu_thread_checker_
.CalledOnValidThread());
828 DCHECK_EQ(CFGetRetainCount(picture_bindings_
[picture_id
]), 1);
829 picture_bindings_
.erase(picture_id
);
830 if (assigned_picture_ids_
.count(picture_id
) != 0) {
831 available_picture_ids_
.push_back(picture_id
);
834 client_
->DismissPictureBuffer(picture_id
);
838 void VTVideoDecodeAccelerator::ProcessWorkQueues() {
839 DCHECK(gpu_thread_checker_
.CalledOnValidThread());
842 // TODO(sandersd): Batch where possible.
843 while (state_
== STATE_DECODING
) {
844 if (!ProcessReorderQueue() && !ProcessTaskQueue())
850 // Do nothing until Destroy() is called.
853 case STATE_DESTROYING
:
854 // Drop tasks until we are ready to destruct.
855 while (!task_queue_
.empty()) {
856 if (task_queue_
.front().type
== TASK_DESTROY
) {
866 bool VTVideoDecodeAccelerator::ProcessTaskQueue() {
867 DCHECK(gpu_thread_checker_
.CalledOnValidThread());
868 DCHECK_EQ(state_
, STATE_DECODING
);
870 if (task_queue_
.empty())
873 const Task
& task
= task_queue_
.front();
876 // TODO(sandersd): Signal IDR explicitly (not using pic_order_cnt == 0).
877 if (reorder_queue_
.size() < kMaxReorderQueueSize
&&
878 (task
.frame
->pic_order_cnt
!= 0 || reorder_queue_
.empty())) {
879 assigned_bitstream_ids_
.erase(task
.frame
->bitstream_id
);
880 client_
->NotifyEndOfBitstreamBuffer(task
.frame
->bitstream_id
);
881 reorder_queue_
.push(task
.frame
);
888 DCHECK_EQ(task
.type
, pending_flush_tasks_
.front());
889 if (reorder_queue_
.size() == 0) {
890 pending_flush_tasks_
.pop();
891 client_
->NotifyFlushDone();
898 DCHECK_EQ(task
.type
, pending_flush_tasks_
.front());
899 if (reorder_queue_
.size() == 0) {
903 last_spsext_
.clear();
906 pending_flush_tasks_
.pop();
907 client_
->NotifyResetDone();
914 NOTREACHED() << "Can't destroy while in STATE_DECODING.";
915 NotifyError(ILLEGAL_STATE
, SFT_PLATFORM_ERROR
);
920 bool VTVideoDecodeAccelerator::ProcessReorderQueue() {
921 DCHECK(gpu_thread_checker_
.CalledOnValidThread());
922 DCHECK_EQ(state_
, STATE_DECODING
);
924 if (reorder_queue_
.empty())
927 // If the next task is a flush (because there is a pending flush or becuase
928 // the next frame is an IDR), then we don't need a full reorder buffer to send
930 bool flushing
= !task_queue_
.empty() &&
931 (task_queue_
.front().type
!= TASK_FRAME
||
932 task_queue_
.front().frame
->pic_order_cnt
== 0);
934 size_t reorder_window
= std::max(0, reorder_queue_
.top()->reorder_window
);
935 if (flushing
|| reorder_queue_
.size() > reorder_window
) {
936 if (ProcessFrame(*reorder_queue_
.top())) {
937 reorder_queue_
.pop();
945 bool VTVideoDecodeAccelerator::ProcessFrame(const Frame
& frame
) {
946 DCHECK(gpu_thread_checker_
.CalledOnValidThread());
947 DCHECK_EQ(state_
, STATE_DECODING
);
949 // If the next pending flush is for a reset, then the frame will be dropped.
950 bool resetting
= !pending_flush_tasks_
.empty() &&
951 pending_flush_tasks_
.front() == TASK_RESET
;
953 if (!resetting
&& frame
.image
.get()) {
954 // If the |coded_size| has changed, request new picture buffers and then
956 // TODO(sandersd): If GpuVideoDecoder didn't specifically check the size of
957 // textures, this would be unnecessary, as the size is actually a property
958 // of the texture binding, not the texture. We rebind every frame, so the
959 // size passed to ProvidePictureBuffers() is meaningless.
960 if (picture_size_
!= frame
.coded_size
) {
961 // Dismiss current pictures.
962 for (int32_t picture_id
: assigned_picture_ids_
)
963 client_
->DismissPictureBuffer(picture_id
);
964 assigned_picture_ids_
.clear();
965 available_picture_ids_
.clear();
967 // Request new pictures.
968 picture_size_
= frame
.coded_size
;
969 client_
->ProvidePictureBuffers(
970 kNumPictureBuffers
, coded_size_
, GL_TEXTURE_RECTANGLE_ARB
);
973 if (!SendFrame(frame
))
980 bool VTVideoDecodeAccelerator::SendFrame(const Frame
& frame
) {
981 DCHECK(gpu_thread_checker_
.CalledOnValidThread());
982 DCHECK_EQ(state_
, STATE_DECODING
);
984 if (available_picture_ids_
.empty())
987 int32_t picture_id
= available_picture_ids_
.back();
988 IOSurfaceRef surface
= CVPixelBufferGetIOSurface(frame
.image
.get());
990 if (!make_context_current_
.Run()) {
991 DLOG(ERROR
) << "Failed to make GL context current";
992 NotifyError(PLATFORM_FAILURE
, SFT_PLATFORM_ERROR
);
996 glEnable(GL_TEXTURE_RECTANGLE_ARB
);
997 gfx::ScopedTextureBinder
998 texture_binder(GL_TEXTURE_RECTANGLE_ARB
, texture_ids_
[picture_id
]);
999 CGLContextObj cgl_context
=
1000 static_cast<CGLContextObj
>(gfx::GLContext::GetCurrent()->GetHandle());
1001 CGLError status
= CGLTexImageIOSurface2D(
1003 GL_TEXTURE_RECTANGLE_ARB
, // target
1004 GL_RGB
, // internal_format
1005 frame
.coded_size
.width(), // width
1006 frame
.coded_size
.height(), // height
1007 GL_YCBCR_422_APPLE
, // format
1008 GL_UNSIGNED_SHORT_8_8_APPLE
, // type
1009 surface
, // io_surface
1011 glDisable(GL_TEXTURE_RECTANGLE_ARB
);
1012 if (status
!= kCGLNoError
) {
1013 NOTIFY_STATUS("CGLTexImageIOSurface2D()", status
, SFT_PLATFORM_ERROR
);
1017 available_picture_ids_
.pop_back();
1018 picture_bindings_
[picture_id
] = frame
.image
;
1019 client_
->PictureReady(media::Picture(picture_id
, frame
.bitstream_id
,
1020 gfx::Rect(frame
.coded_size
), false));
1024 void VTVideoDecodeAccelerator::NotifyError(
1025 Error vda_error_type
,
1026 VTVDASessionFailureType session_failure_type
) {
1027 DCHECK_LT(session_failure_type
, SFT_MAX
+ 1);
1028 if (!gpu_thread_checker_
.CalledOnValidThread()) {
1029 gpu_task_runner_
->PostTask(FROM_HERE
, base::Bind(
1030 &VTVideoDecodeAccelerator::NotifyError
, weak_this_
, vda_error_type
,
1031 session_failure_type
));
1032 } else if (state_
== STATE_DECODING
) {
1033 state_
= STATE_ERROR
;
1034 UMA_HISTOGRAM_ENUMERATION("Media.VTVDA.SessionFailureReason",
1035 session_failure_type
,
1037 client_
->NotifyError(vda_error_type
);
1041 void VTVideoDecodeAccelerator::QueueFlush(TaskType type
) {
1042 DCHECK(gpu_thread_checker_
.CalledOnValidThread());
1043 pending_flush_tasks_
.push(type
);
1044 decoder_thread_
.task_runner()->PostTask(
1045 FROM_HERE
, base::Bind(&VTVideoDecodeAccelerator::FlushTask
,
1046 base::Unretained(this), type
));
1048 // If this is a new flush request, see if we can make progress.
1049 if (pending_flush_tasks_
.size() == 1)
1050 ProcessWorkQueues();
1053 void VTVideoDecodeAccelerator::Flush() {
1054 DCHECK(gpu_thread_checker_
.CalledOnValidThread());
1055 QueueFlush(TASK_FLUSH
);
1058 void VTVideoDecodeAccelerator::Reset() {
1059 DCHECK(gpu_thread_checker_
.CalledOnValidThread());
1060 QueueFlush(TASK_RESET
);
1063 void VTVideoDecodeAccelerator::Destroy() {
1064 DCHECK(gpu_thread_checker_
.CalledOnValidThread());
1066 // In a forceful shutdown, the decoder thread may be dead already.
1067 if (!decoder_thread_
.IsRunning()) {
1072 // For a graceful shutdown, return assigned buffers and flush before
1073 // destructing |this|.
1074 // TODO(sandersd): Make sure the decoder won't try to read the buffers again
1075 // before discarding them.
1076 for (int32_t bitstream_id
: assigned_bitstream_ids_
)
1077 client_
->NotifyEndOfBitstreamBuffer(bitstream_id
);
1078 assigned_bitstream_ids_
.clear();
1079 state_
= STATE_DESTROYING
;
1080 QueueFlush(TASK_DESTROY
);
1083 bool VTVideoDecodeAccelerator::CanDecodeOnIOThread() {
1088 media::VideoDecodeAccelerator::SupportedProfiles
1089 VTVideoDecodeAccelerator::GetSupportedProfiles() {
1090 SupportedProfiles profiles
;
1091 for (const auto& supported_profile
: kSupportedProfiles
) {
1092 SupportedProfile profile
;
1093 profile
.profile
= supported_profile
;
1094 profile
.min_resolution
.SetSize(16, 16);
1095 profile
.max_resolution
.SetSize(4096, 2160);
1096 profiles
.push_back(profile
);
1101 } // namespace content