OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <CoreVideo/CoreVideo.h> | |
6 #include <OpenGL/CGLIOSurface.h> | |
7 | |
8 #include "base/bind.h" | |
9 #include "base/thread_task_runner_handle.h" | |
5 #include "content/common/gpu/media/vt_video_decode_accelerator.h" | 10 #include "content/common/gpu/media/vt_video_decode_accelerator.h" |
11 #include "media/filters/h264_parser.h" | |
12 | |
13 using content_common_gpu_media::kModuleVt; | |
14 using content_common_gpu_media::InitializeStubs; | |
15 using content_common_gpu_media::IsVtInitialized; | |
16 using content_common_gpu_media::StubPathMap; | |
6 | 17 |
7 namespace content { | 18 namespace content { |
8 | 19 |
20 // Route decoded frame callbacks back into the VTVideoDecodeAccelerator. | |
21 static void OutputThunk( | |
22 void* decompression_output_refcon, | |
23 void* source_frame_refcon, | |
24 OSStatus status, | |
25 VTDecodeInfoFlags info_flags, | |
26 CVImageBufferRef image_buffer, | |
27 CMTime presentation_time_stamp, | |
28 CMTime presentation_duration) { | |
29 VTVideoDecodeAccelerator* vda = | |
30 reinterpret_cast<VTVideoDecodeAccelerator*>(decompression_output_refcon); | |
31 int32_t* bitstream_id_ptr = reinterpret_cast<int32_t*>(source_frame_refcon); | |
32 int32_t bitstream_id = *bitstream_id_ptr; | |
33 delete bitstream_id_ptr; | |
34 CFRetain(image_buffer); | |
35 vda->Output(bitstream_id, status, info_flags, image_buffer); | |
Pawel Osciak
2014/07/10 00:10:55
What guarantees that vda is still valid (haven't b
sandersd (OOO until July 31)
2014/07/10 00:35:38
The VTDecompressionSession will be flushed in Dest
| |
36 } | |
37 | |
9 VTVideoDecodeAccelerator::VTVideoDecodeAccelerator(CGLContextObj cgl_context) | 38 VTVideoDecodeAccelerator::VTVideoDecodeAccelerator(CGLContextObj cgl_context) |
10 : loop_proxy_(base::MessageLoopProxy::current()), | 39 : gpu_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
11 cgl_context_(cgl_context), | 40 cgl_context_(cgl_context), |
12 client_(NULL), | 41 client_(NULL), |
42 decoder_thread_("VTDecoderThread"), | |
43 format_(NULL), | |
44 session_(NULL), | |
45 coded_width_(0), | |
46 coded_height_(0), | |
13 weak_this_factory_(this) { | 47 weak_this_factory_(this) { |
48 callback_.decompressionOutputCallback = OutputThunk; | |
49 callback_.decompressionOutputRefCon = this; | |
14 } | 50 } |
15 | 51 |
16 VTVideoDecodeAccelerator::~VTVideoDecodeAccelerator() { | 52 VTVideoDecodeAccelerator::~VTVideoDecodeAccelerator() { |
17 } | 53 } |
18 | 54 |
19 bool VTVideoDecodeAccelerator::Initialize( | 55 bool VTVideoDecodeAccelerator::Initialize( |
20 media::VideoCodecProfile profile, | 56 media::VideoCodecProfile profile, |
21 Client* client) { | 57 Client* client) { |
22 DCHECK(CalledOnValidThread()); | 58 DCHECK(CalledOnValidThread()); |
23 DVLOG(2) << __FUNCTION__; | |
24 client_ = client; | 59 client_ = client; |
25 | 60 |
26 // Only H.264 is supported. | 61 // Only H.264 is supported. |
27 if (profile < media::H264PROFILE_MIN || profile > media::H264PROFILE_MAX) | 62 if (profile < media::H264PROFILE_MIN || profile > media::H264PROFILE_MAX) |
28 return false; | 63 return false; |
29 | 64 |
30 // Prevent anyone from using VTVideoDecoder for now. http://crbug.com/133828 | 65 // TODO(sandersd): Move VideoToolbox library loading to sandbox startup; |
31 return false; | 66 // until then, --no-sandbox is required. |
67 if (!IsVtInitialized()) { | |
68 StubPathMap paths; | |
69 // CoreVideo is also required, but the loader stops after the first | |
70 // path is loaded. Instead we rely on the transitive dependency from | |
71 // VideoToolbox to CoreVideo. | |
72 // TODO(sandersd): Fallback to PrivateFrameworks for VideoToolbox. | |
73 paths[kModuleVt].push_back(FILE_PATH_LITERAL( | |
74 "/System/Library/Frameworks/VideoToolbox.framework/VideoToolbox")); | |
75 if (!InitializeStubs(paths)) | |
76 return false; | |
77 } | |
78 | |
79 // Spawn a thread to handle parsing and calling VideoToolbox. | |
80 if (!decoder_thread_.Start()) | |
81 return false; | |
82 | |
83 // Note that --ignore-gpu-blacklist is still required to get here. | |
84 return true; | |
85 } | |
86 | |
87 // TODO(sandersd): Proper error reporting instead of CHECKs. | |
88 void VTVideoDecodeAccelerator::ConfigureDecoder( | |
89 const std::vector<const uint8_t*>& nalu_data_ptrs, | |
90 const std::vector<size_t>& nalu_data_sizes) { | |
91 format_.reset(); | |
92 CHECK(!CMVideoFormatDescriptionCreateFromH264ParameterSets( | |
93 kCFAllocatorDefault, | |
94 nalu_data_ptrs.size(), // parameter_set_count | |
95 &nalu_data_ptrs.front(), // ¶meter_set_pointers | |
96 &nalu_data_sizes.front(), // ¶meter_set_sizes | |
97 4, // nal_unit_header_length | |
98 format_.InitializeInto() | |
99 )); | |
100 | |
101 // TODO(sandersd): Check if the size has changed and handle picture requests. | |
102 CMVideoDimensions coded_size = CMVideoFormatDescriptionGetDimensions(format_); | |
103 coded_width_ = coded_size.width; | |
104 coded_height_ = coded_size.height; | |
105 | |
106 base::ScopedCFTypeRef<CFMutableDictionaryRef> decoder_config( | |
107 CFDictionaryCreateMutable( | |
108 kCFAllocatorDefault, | |
109 1, // capacity | |
110 &kCFTypeDictionaryKeyCallBacks, | |
111 &kCFTypeDictionaryValueCallBacks)); | |
112 | |
113 CFDictionarySetValue( | |
114 decoder_config, | |
115 // kVTVideoDecoderSpecification_EnableHardwareAcceleratedVideoDecoder | |
116 CFSTR("EnableHardwareAcceleratedVideoDecoder"), | |
117 kCFBooleanTrue); | |
118 | |
119 base::ScopedCFTypeRef<CFMutableDictionaryRef> image_config( | |
120 CFDictionaryCreateMutable( | |
121 kCFAllocatorDefault, | |
122 4, // capacity | |
123 &kCFTypeDictionaryKeyCallBacks, | |
124 &kCFTypeDictionaryValueCallBacks)); | |
125 | |
126 // TODO(sandersd): ARGB for video that is not 4:2:0. | |
127 int32_t pixel_format = '2vuy'; | |
128 #define CFINT(i) CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &i) | |
scherkus (not reviewing)
2014/07/09 22:39:25
nit: it's good practice to #undef when you're done
sandersd (OOO until July 31)
2014/07/10 00:35:38
Done.
| |
129 base::ScopedCFTypeRef<CFNumberRef> cf_pixel_format(CFINT(pixel_format)); | |
130 base::ScopedCFTypeRef<CFNumberRef> cf_width(CFINT(coded_width_)); | |
131 base::ScopedCFTypeRef<CFNumberRef> cf_height(CFINT(coded_height_)); | |
132 CFDictionarySetValue( | |
133 image_config, kCVPixelBufferPixelFormatTypeKey, cf_pixel_format); | |
134 CFDictionarySetValue(image_config, kCVPixelBufferWidthKey, cf_width); | |
135 CFDictionarySetValue(image_config, kCVPixelBufferHeightKey, cf_height); | |
136 CFDictionarySetValue( | |
137 image_config, kCVPixelBufferOpenGLCompatibilityKey, kCFBooleanTrue); | |
138 | |
139 // TODO(sandersd): Skip if the session is compatable. | |
140 // TODO(sandersd): Flush frames when resetting. | |
141 session_.reset(); | |
142 CHECK(!VTDecompressionSessionCreate( | |
143 kCFAllocatorDefault, | |
144 format_, // video_format_description | |
145 decoder_config, // video_decoder_specification | |
146 image_config, // destination_image_buffer_attributes | |
147 &callback_, // output_callback | |
148 session_.InitializeInto() | |
149 )); | |
150 DVLOG(2) << "Created VTDecompressionSession"; | |
32 } | 151 } |
33 | 152 |
34 void VTVideoDecodeAccelerator::Decode(const media::BitstreamBuffer& bitstream) { | 153 void VTVideoDecodeAccelerator::Decode(const media::BitstreamBuffer& bitstream) { |
35 DCHECK(CalledOnValidThread()); | 154 DCHECK(CalledOnValidThread()); |
155 decoder_thread_.message_loop_proxy()->PostTask(FROM_HERE, base::Bind( | |
156 &VTVideoDecodeAccelerator::DecodeTask, base::Unretained(this), | |
157 bitstream)); | |
158 } | |
159 | |
160 void VTVideoDecodeAccelerator::DecodeTask( | |
161 const media::BitstreamBuffer bitstream) { | |
162 DCHECK(decoder_thread_.message_loop_proxy()->BelongsToCurrentThread()); | |
163 | |
164 // Map the bitstream buffer. | |
165 base::SharedMemory memory(bitstream.handle(), true); | |
166 size_t size = bitstream.size(); | |
167 CHECK(memory.Map(size)); | |
168 const uint8_t* buf = static_cast<uint8_t*>(memory.memory()); | |
169 | |
170 // Locate relevant NALUs in the buffer. | |
171 size_t data_size = 0; | |
172 std::vector<media::H264NALU> nalus; | |
Pawel Osciak
2014/07/10 00:10:55
Unused?
sandersd (OOO until July 31)
2014/07/10 00:35:38
Currently unused, will be used as the source of po
| |
173 std::vector<const uint8_t*> config_nalu_data_ptrs; | |
174 std::vector<size_t> config_nalu_data_sizes; | |
175 parser_.SetStream(buf, size); | |
176 media::H264NALU nalu; | |
177 while (true) { | |
178 media::H264Parser::Result result = parser_.AdvanceToNextNALU(&nalu); | |
179 if (result == media::H264Parser::kEOStream) | |
180 break; | |
181 CHECK_EQ(result, media::H264Parser::kOk); | |
182 if (nalu.nal_unit_type == media::H264NALU::kSPS || | |
183 nalu.nal_unit_type == media::H264NALU::kPPS || | |
184 nalu.nal_unit_type == media::H264NALU::kSPSExt) { | |
185 config_nalu_data_ptrs.push_back(nalu.data); | |
186 config_nalu_data_sizes.push_back(nalu.size); | |
187 } | |
188 nalus.push_back(nalu); | |
189 data_size += 4 + nalu.size; | |
Pawel Osciak
2014/07/10 00:10:55
What is "4" for? Also, data_size seems unused in g
sandersd (OOO until July 31)
2014/07/10 00:35:38
Added comment to explain.
| |
190 } | |
191 | |
192 if (!config_nalu_data_ptrs.empty()) | |
193 ConfigureDecoder(config_nalu_data_ptrs, config_nalu_data_sizes); | |
194 | |
195 // TODO(sandersd): Rewrite slice NALU headers and send for decoding. | |
196 } | |
197 | |
198 // This method may be called on any VideoToolbox thread. | |
199 void VTVideoDecodeAccelerator::Output( | |
200 int32_t bitstream_id, | |
201 OSStatus status, | |
202 VTDecodeInfoFlags info_flags, | |
203 CVImageBufferRef image_buffer) { | |
204 // TODO(sandersd): Store the frame in a queue. | |
205 CFRelease(image_buffer); | |
36 } | 206 } |
37 | 207 |
38 void VTVideoDecodeAccelerator::AssignPictureBuffers( | 208 void VTVideoDecodeAccelerator::AssignPictureBuffers( |
39 const std::vector<media::PictureBuffer>& pictures) { | 209 const std::vector<media::PictureBuffer>& pictures) { |
40 DCHECK(CalledOnValidThread()); | 210 DCHECK(CalledOnValidThread()); |
41 } | 211 } |
42 | 212 |
43 void VTVideoDecodeAccelerator::ReusePictureBuffer(int32_t picture_id) { | 213 void VTVideoDecodeAccelerator::ReusePictureBuffer(int32_t picture_id) { |
44 DCHECK(CalledOnValidThread()); | 214 DCHECK(CalledOnValidThread()); |
45 } | 215 } |
46 | 216 |
47 void VTVideoDecodeAccelerator::Flush() { | 217 void VTVideoDecodeAccelerator::Flush() { |
48 DCHECK(CalledOnValidThread()); | 218 DCHECK(CalledOnValidThread()); |
219 // TODO(sandersd): Trigger flush, sending frames. | |
49 } | 220 } |
50 | 221 |
51 void VTVideoDecodeAccelerator::Reset() { | 222 void VTVideoDecodeAccelerator::Reset() { |
52 DCHECK(CalledOnValidThread()); | 223 DCHECK(CalledOnValidThread()); |
224 // TODO(sandersd): Trigger flush, discarding frames. | |
53 } | 225 } |
54 | 226 |
55 void VTVideoDecodeAccelerator::Destroy() { | 227 void VTVideoDecodeAccelerator::Destroy() { |
56 DCHECK(CalledOnValidThread()); | 228 DCHECK(CalledOnValidThread()); |
229 // TODO(sandersd): Trigger flush, discarding frames, and wait for them. | |
230 delete this; | |
57 } | 231 } |
58 | 232 |
59 bool VTVideoDecodeAccelerator::CanDecodeOnIOThread() { | 233 bool VTVideoDecodeAccelerator::CanDecodeOnIOThread() { |
60 return false; | 234 return false; |
61 } | 235 } |
62 | 236 |
63 } // namespace content | 237 } // namespace content |
OLD | NEW |