OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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. | |
4 | |
5 #include "base/callback_helpers.h" | |
6 #include "base/logging.h" | |
7 #include "base/run_loop.h" | |
8 #include "chrome/gpu/arc_gpu_video_decode_accelerator.h" | |
9 #include "content/public/common/gpu_video_decode_accelerator_factory.h" | |
10 | |
11 namespace chromeos { | |
12 namespace arc { | |
13 | |
14 ArcGpuVideoDecodeAccelerator::InputRecord::InputRecord( | |
15 int32_t bitstream_buffer_id, | |
16 uint32_t buffer_index, | |
17 int64_t timestamp) | |
18 : bitstream_buffer_id(bitstream_buffer_id), | |
19 buffer_index(buffer_index), | |
20 timestamp(timestamp) {} | |
21 | |
22 ArcGpuVideoDecodeAccelerator::ArcGpuVideoDecodeAccelerator() | |
23 : pending_eos_output_buffer_(false), | |
24 arc_client_(nullptr), | |
25 next_bitstream_buffer_id_(0), | |
26 output_buffer_size_(0) {} | |
27 | |
28 ArcGpuVideoDecodeAccelerator::~ArcGpuVideoDecodeAccelerator() {} | |
29 | |
30 namespace { | |
31 | |
32 // An arbitrary chosen limit of the number of buffers. The number of | |
33 // buffers used is requested from the untrusted client side. | |
34 const size_t kMaxBufferCount = 128; | |
35 | |
36 } // anonymous namespace | |
37 | |
38 bool ArcGpuVideoDecodeAccelerator::Initialize( | |
39 const Config& config, | |
40 ArcVideoAccelerator::Client* client) { | |
41 DVLOG(5) << "Initialize(device=" << config.device_type | |
42 << ", input_pixel_format=" << config.input_pixel_format | |
43 << ", num_input_buffers=" << config.num_input_buffers << ")"; | |
44 DCHECK(thread_checker_.CalledOnValidThread()); | |
45 if (config.device_type != Config::DEVICE_DECODER) | |
46 return false; | |
47 DCHECK(client); | |
48 | |
49 if (arc_client_) { | |
50 DLOG(ERROR) << "Re-Initialize() is not allowed"; | |
51 return false; | |
52 } | |
53 | |
54 arc_client_ = client; | |
55 | |
56 if (config.num_input_buffers > kMaxBufferCount) { | |
57 DLOG(ERROR) << "Request too many buffers: " << config.num_input_buffers; | |
58 return false; | |
59 } | |
60 input_buffer_info_.resize(config.num_input_buffers); | |
61 | |
62 media::VideoDecodeAccelerator::Config vda_config; | |
63 switch (config.input_pixel_format) { | |
64 case HAL_PIXEL_FORMAT_H264: | |
65 vda_config.profile = media::H264PROFILE_MAIN; | |
66 break; | |
67 case HAL_PIXEL_FORMAT_VP8: | |
68 vda_config.profile = media::VP8PROFILE_ANY; | |
69 break; | |
70 default: | |
71 DLOG(ERROR) << "Unsupported input format: " << config.input_pixel_format; | |
72 return false; | |
73 } | |
74 vda_config.output_mode = | |
75 media::VideoDecodeAccelerator::Config::OutputMode::IMPORT; | |
76 | |
77 scoped_ptr<content::GpuVideoDecodeAcceleratorFactory> vda_factory = | |
78 content::GpuVideoDecodeAcceleratorFactory::CreateWithNoGL(); | |
79 vda_ = std::move(vda_factory->CreateVDA(this, vda_config)); | |
80 if (!vda_) { | |
81 DLOG(ERROR) << "Failed to create VDA."; | |
82 return false; | |
83 } | |
84 return true; | |
85 } | |
86 | |
87 void ArcGpuVideoDecodeAccelerator::SetNumberOfOutputBuffers(size_t number) { | |
88 DVLOG(5) << "SetNumberOfOutputBuffers(" << number << ")"; | |
89 DCHECK(thread_checker_.CalledOnValidThread()); | |
90 if (!vda_) { | |
91 DLOG(ERROR) << "VDA not initialized"; | |
92 return; | |
93 } | |
94 | |
95 if (number > kMaxBufferCount) { | |
96 DLOG(ERROR) << "Too many buffers: " << number; | |
97 arc_client_->OnError(INVALID_ARGUMENT); | |
98 return; | |
99 } | |
100 | |
101 std::vector<media::PictureBuffer> buffers; | |
102 for (int32_t id = 0; static_cast<size_t>(id) < number; ++id) { | |
Pawel Osciak
2016/03/25 08:38:44
Perhaps we could instead try:
for (size_t i = 0;
Owen Lin
2016/03/29 07:10:41
Done.
| |
103 // TODO: Make sure the |coded_size| is what we want. | |
104 buffers.push_back(media::PictureBuffer(id, coded_size_, 0)); | |
105 } | |
106 vda_->AssignPictureBuffers(buffers); | |
107 | |
108 buffers_pending_import_.clear(); | |
109 buffers_pending_import_.resize(number); | |
110 } | |
111 | |
112 void ArcGpuVideoDecodeAccelerator::BindSharedMemory(PortType port, | |
113 uint32_t index, | |
114 int ashmem_fd, | |
115 off_t offset, | |
116 size_t length) { | |
117 DVLOG(5) << "ArcGVDA::BindSharedMemory, offset: " << offset | |
118 << ", length: " << length; | |
119 DCHECK(thread_checker_.CalledOnValidThread()); | |
120 if (!vda_) { | |
121 DLOG(ERROR) << "VDA not initialized"; | |
122 return; | |
123 } | |
124 | |
125 // Make sure we will close the file descriptor. | |
126 base::ScopedFD handle(ashmem_fd); | |
127 if (port != PORT_INPUT) { | |
128 DLOG(ERROR) << "SharedBuffer is only supported for input"; | |
129 arc_client_->OnError(INVALID_ARGUMENT); | |
130 return; | |
131 } | |
132 if (!ValidatePortAndIndex(port, index)) { | |
133 arc_client_->OnError(INVALID_ARGUMENT); | |
134 return; | |
135 } | |
136 InputBufferInfo* input_info = &input_buffer_info_[index]; | |
137 input_info->handle = std::move(handle); | |
138 input_info->offset = offset; | |
139 input_info->length = length; | |
140 } | |
141 | |
142 void ArcGpuVideoDecodeAccelerator::BindDmabuf(PortType port, | |
143 uint32_t index, | |
144 int dmabuf_fd) { | |
145 DCHECK(thread_checker_.CalledOnValidThread()); | |
146 | |
147 if (!vda_) { | |
148 DLOG(ERROR) << "VDA not initialized"; | |
149 return; | |
150 } | |
151 | |
152 // Make sure we will close the file descriptor. | |
153 base::ScopedFD handle(dmabuf_fd); | |
154 if (port != PORT_OUTPUT) { | |
155 DLOG(ERROR) << "DmaBuffer is only supported for input"; | |
Pawel Osciak
2016/03/25 08:38:45
s/DmaBuffer/Dmabuf/
Owen Lin
2016/03/29 07:10:41
Done.
| |
156 arc_client_->OnError(INVALID_ARGUMENT); | |
157 return; | |
158 } | |
159 if (!ValidatePortAndIndex(port, index)) { | |
160 arc_client_->OnError(INVALID_ARGUMENT); | |
161 return; | |
162 } | |
163 buffers_pending_import_[index] = std::move(handle); | |
164 } | |
165 | |
166 void ArcGpuVideoDecodeAccelerator::UseBuffer(PortType port, | |
167 uint32_t index, | |
168 const BufferMetadata& metadata) { | |
169 DVLOG(5) << "UseBuffer(port=" << port << ", index=" << index | |
170 << ", metadata=(bytes_used=" << metadata.bytes_used | |
171 << ", timestamp=" << metadata.timestamp << ")"; | |
172 DCHECK(thread_checker_.CalledOnValidThread()); | |
173 if (!vda_) { | |
174 DLOG(ERROR) << "VDA not initialized"; | |
175 return; | |
176 } | |
177 if (!ValidatePortAndIndex(port, index)) { | |
178 arc_client_->OnError(INVALID_ARGUMENT); | |
179 return; | |
180 } | |
181 switch (port) { | |
182 case PORT_INPUT: { | |
183 InputBufferInfo* input_info = &input_buffer_info_[index]; | |
184 int32_t bitstream_buffer_id = next_bitstream_buffer_id_; | |
185 // Mask against 30 bits, to avoid (undefined) wraparound on signed | |
186 // integer. | |
187 next_bitstream_buffer_id_ = (next_bitstream_buffer_id_ + 1) & 0x3FFFFFFF; | |
188 int dup_fd = HANDLE_EINTR(dup(input_info->handle.get())); | |
189 if (dup_fd < 0) { | |
190 DLOG(ERROR) << "dup() failed."; | |
191 arc_client_->OnError(PLATFORM_FAILURE); | |
192 return; | |
193 } | |
194 CreateInputRecord(bitstream_buffer_id, index, metadata.timestamp); | |
195 vda_->Decode(media::BitstreamBuffer( | |
196 bitstream_buffer_id, base::SharedMemoryHandle(dup_fd, true), | |
197 metadata.bytes_used, input_info->offset)); | |
198 if (metadata.flags & BUFFER_FLAG_EOS) { | |
199 vda_->Flush(true); | |
200 } | |
201 break; | |
202 } | |
203 case PORT_OUTPUT: { | |
204 SendEosIfNeededOrReusePicture(index); | |
205 break; | |
206 } | |
207 default: | |
208 NOTREACHED(); | |
209 } | |
210 } | |
211 | |
212 void ArcGpuVideoDecodeAccelerator::Reset() { | |
213 DCHECK(thread_checker_.CalledOnValidThread()); | |
214 if (!vda_) { | |
215 DLOG(ERROR) << "VDA not initialized"; | |
216 return; | |
217 } | |
218 vda_->Reset(); | |
219 } | |
220 | |
221 void ArcGpuVideoDecodeAccelerator::ProvidePictureBuffers( | |
222 size_t requested_num_of_buffers, | |
223 const gfx::Size& dimensions, | |
224 uint32_t texture_target) { | |
225 DVLOG(5) << "ProvidePictureBuffers(" | |
226 << "requested_num_of_buffers=" << requested_num_of_buffers | |
227 << ", dimensions=" << dimensions.ToString() << ")"; | |
228 DCHECK(thread_checker_.CalledOnValidThread()); | |
229 coded_size_ = dimensions; | |
230 // TODO(owenlin): use VDA::GetOutputFormat() here and calculate correct | |
231 // |buffer_size|. | |
232 VideoFormat video_format; | |
233 video_format.buffer_size = dimensions.GetArea() * 3 / 2; | |
234 output_buffer_size_ = video_format.buffer_size; | |
235 video_format.min_num_buffers = requested_num_of_buffers; | |
236 video_format.coded_width = dimensions.width(); | |
237 video_format.coded_height = dimensions.height(); | |
238 // TODO(owenlin): How to get visible size? | |
239 video_format.crop_top = 0; | |
240 video_format.crop_left = 0; | |
241 video_format.crop_width = dimensions.width(); | |
242 video_format.crop_height = dimensions.height(); | |
243 arc_client_->OnOutputFormatChanged(video_format); | |
244 } | |
245 | |
246 void ArcGpuVideoDecodeAccelerator::DismissPictureBuffer( | |
247 int32_t picture_buffer) { | |
248 // no-op | |
249 } | |
250 | |
251 void ArcGpuVideoDecodeAccelerator::PictureReady(const media::Picture& picture) { | |
252 DVLOG(5) << "PictureReady(picture_buffer_id=" << picture.picture_buffer_id() | |
253 << ", bitstream_buffer_id=" << picture.bitstream_buffer_id(); | |
254 DCHECK(thread_checker_.CalledOnValidThread()); | |
255 | |
256 // Empty buffer, returned in Flushing. | |
257 if (picture.bitstream_buffer_id() == -1) { | |
258 buffers_pending_eos_.push(picture.picture_buffer_id()); | |
259 return; | |
260 } | |
261 InputRecord* input_record = FindInputRecord(picture.bitstream_buffer_id()); | |
262 if (input_record == nullptr) { | |
263 DLOG(ERROR) << "Cannot find for bitstream buffer id: " | |
264 << picture.bitstream_buffer_id(); | |
265 arc_client_->OnError(PLATFORM_FAILURE); | |
266 return; | |
267 } | |
268 | |
269 BufferMetadata metadata; | |
270 metadata.timestamp = input_record->timestamp; | |
271 metadata.bytes_used = output_buffer_size_; | |
272 arc_client_->OnBufferDone(PORT_OUTPUT, picture.picture_buffer_id(), metadata); | |
273 } | |
274 | |
275 void ArcGpuVideoDecodeAccelerator::NotifyEndOfBitstreamBuffer( | |
276 int32_t bitstream_buffer_id) { | |
277 DVLOG(5) << "NotifyEndOfBitstreamBuffer(" << bitstream_buffer_id << ")"; | |
278 DCHECK(thread_checker_.CalledOnValidThread()); | |
279 InputRecord* input_record = FindInputRecord(bitstream_buffer_id); | |
280 if (input_record == nullptr) { | |
281 arc_client_->OnError(PLATFORM_FAILURE); | |
282 return; | |
283 } | |
284 arc_client_->OnBufferDone(PORT_INPUT, input_record->buffer_index, | |
285 BufferMetadata()); | |
286 } | |
287 | |
288 void ArcGpuVideoDecodeAccelerator::NotifyFlushDone() { | |
289 DCHECK(thread_checker_.CalledOnValidThread()); | |
290 pending_eos_output_buffer_ = true; | |
291 while (!buffers_pending_eos_.empty()) { | |
292 SendEosIfNeededOrReusePicture(buffers_pending_eos_.front()); | |
293 buffers_pending_eos_.pop(); | |
294 } | |
295 } | |
296 | |
297 void ArcGpuVideoDecodeAccelerator::NotifyResetDone() { | |
298 DCHECK(thread_checker_.CalledOnValidThread()); | |
299 arc_client_->OnResetDone(); | |
300 } | |
301 | |
302 static ArcVideoAccelerator::Error ConvertErrorCode( | |
303 media::VideoDecodeAccelerator::Error error) { | |
304 switch (error) { | |
305 case media::VideoDecodeAccelerator::ILLEGAL_STATE: | |
306 return ArcVideoAccelerator::ILLEGAL_STATE; | |
307 case media::VideoDecodeAccelerator::INVALID_ARGUMENT: | |
308 return ArcVideoAccelerator::INVALID_ARGUMENT; | |
309 case media::VideoDecodeAccelerator::UNREADABLE_INPUT: | |
310 return ArcVideoAccelerator::UNREADABLE_INPUT; | |
311 case media::VideoDecodeAccelerator::PLATFORM_FAILURE: | |
312 return ArcVideoAccelerator::PLATFORM_FAILURE; | |
313 default: | |
314 DLOG(ERROR) << "Unknown error: " << error; | |
315 return ArcVideoAccelerator::PLATFORM_FAILURE; | |
316 } | |
317 } | |
318 | |
319 void ArcGpuVideoDecodeAccelerator::NotifyError( | |
320 media::VideoDecodeAccelerator::Error error) { | |
321 DCHECK(thread_checker_.CalledOnValidThread()); | |
322 DLOG(ERROR) << "Error notified: " << error; | |
323 arc_client_->OnError(ConvertErrorCode(error)); | |
324 } | |
325 | |
326 void ArcGpuVideoDecodeAccelerator::SendEosIfNeededOrReusePicture( | |
327 uint32_t index) { | |
328 if (pending_eos_output_buffer_) { | |
329 BufferMetadata metadata; | |
330 metadata.flags = BUFFER_FLAG_EOS; | |
331 arc_client_->OnBufferDone(PORT_OUTPUT, index, metadata); | |
332 pending_eos_output_buffer_ = false; | |
333 } else { | |
334 if (buffers_pending_import_[index].is_valid()) { | |
335 std::vector<gfx::GpuMemoryBufferHandle> buffers; | |
336 buffers.push_back(gfx::GpuMemoryBufferHandle()); | |
337 buffers.back().native_pixmap_handle.fd = | |
338 base::FileDescriptor(buffers_pending_import_[index].release(), true); | |
339 vda_->ImportBufferForPicture(index, buffers); | |
340 } else { | |
341 vda_->ReusePictureBuffer(index); | |
342 } | |
343 } | |
344 } | |
345 | |
346 void ArcGpuVideoDecodeAccelerator::CreateInputRecord( | |
347 int32_t bitstream_buffer_id, | |
348 uint32_t buffer_index, | |
349 int64_t timestamp) { | |
350 input_records_.push_front( | |
351 InputRecord(bitstream_buffer_id, buffer_index, timestamp)); | |
352 // The same value copied from media::GpuVideoDecoder. The input record is | |
353 // needed when the corresponding output buffer is returned from VDA. However | |
354 // there is no guarantee how much buffers the VDA will be kept. We kept | |
355 // the last |kMaxNumberOfInputRecords| in input_records_ and drop the | |
356 // others. | |
Pawel Osciak
2016/03/25 08:38:44
Actually, it's needed in two situations, when inpu
Owen Lin
2016/03/29 07:10:41
Document is updated.
| |
357 const size_t kMaxNumberOfInputRecords = 128; | |
358 if (input_records_.size() > kMaxNumberOfInputRecords) | |
359 input_records_.pop_back(); | |
360 } | |
361 | |
362 ArcGpuVideoDecodeAccelerator::InputRecord* | |
363 ArcGpuVideoDecodeAccelerator::FindInputRecord(int32_t bitstream_buffer_id) { | |
364 for (auto& record : input_records_) { | |
365 if (record.bitstream_buffer_id == bitstream_buffer_id) | |
366 return &record; | |
367 } | |
368 return nullptr; | |
369 } | |
370 | |
371 bool ArcGpuVideoDecodeAccelerator::ValidatePortAndIndex(PortType port, | |
372 uint32_t index) { | |
373 switch (port) { | |
374 case PORT_INPUT: | |
375 if (index >= input_buffer_info_.size()) { | |
376 DLOG(ERROR) << "Invalid index: " << index; | |
377 return false; | |
378 } | |
379 return true; | |
380 case PORT_OUTPUT: | |
381 if (index >= buffers_pending_import_.size()) { | |
382 DLOG(ERROR) << "Invalid index: " << index; | |
383 return false; | |
384 } | |
385 return true; | |
386 default: | |
387 DLOG(ERROR) << "Invalid port: " << port; | |
388 return false; | |
389 } | |
390 } | |
391 | |
392 } // namespace arc | |
393 } // namespace chromeos | |
OLD | NEW |