Chromium Code Reviews| 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 | |
| 10 // TODO use Pawel's factory instead | |
| 11 #include "content/public/common/create_vda.h" | |
| 12 | |
| 13 #undef DVLOG | |
| 14 #define DVLOG VLOG | |
| 15 | |
| 16 namespace chromeos { | |
| 17 namespace arc { | |
| 18 | |
| 19 ArcGpuVideoDecodeAccelerator::InputRecord::InputRecord( | |
| 20 int32_t bitstream_buffer_id, | |
| 21 uint32_t buffer_index, | |
| 22 int64_t timestamp) | |
| 23 : bitstream_buffer_id(bitstream_buffer_id), | |
| 24 buffer_index(buffer_index), | |
| 25 timestamp(timestamp) {} | |
| 26 | |
| 27 ArcGpuVideoDecodeAccelerator::InputBufferInfo::InputBufferInfo() | |
| 28 : offset(0), length(0) {} | |
| 29 | |
| 30 ArcGpuVideoDecodeAccelerator::InputBufferInfo::InputBufferInfo( | |
| 31 InputBufferInfo&& a) { | |
| 32 handle = std::move(a.handle); | |
| 33 offset = a.offset; | |
| 34 length = a.length; | |
| 35 } | |
| 36 | |
| 37 ArcGpuVideoDecodeAccelerator::InputBufferInfo::~InputBufferInfo() {} | |
| 38 | |
| 39 ArcGpuVideoDecodeAccelerator::PortInfo::PortInfo() | |
| 40 : memory_type(MEMORY_DMABUF), num_buffers(0) {} | |
| 41 | |
| 42 ArcGpuVideoDecodeAccelerator::ArcGpuVideoDecodeAccelerator( | |
| 43 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner) | |
| 44 : pending_eos_output_buffer_(false), | |
| 45 arc_client_(nullptr), | |
| 46 next_bitstream_buffer_id_(0), | |
| 47 io_task_runner_(io_task_runner), | |
| 48 output_buffer_size_(0) {} | |
| 49 | |
| 50 ArcGpuVideoDecodeAccelerator::~ArcGpuVideoDecodeAccelerator() {} | |
| 51 | |
| 52 bool ArcGpuVideoDecodeAccelerator::Initialize( | |
| 53 DeviceType device_type, | |
| 54 ArcVideoAccelerator::Client* client) { | |
| 55 DVLOG(5) << "Initialize()"; | |
| 56 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 57 if (device_type != DEVICE_DECODER) | |
| 58 return false; | |
| 59 DCHECK(client); | |
| 60 DCHECK(!arc_client_); | |
| 61 arc_client_ = client; | |
| 62 | |
| 63 vda_.reset(content::CreateVDA(AsWeakPtr(), io_task_runner_)); | |
| 64 if (!vda_) { | |
| 65 DVLOG(1) << "Failed to create VDA."; | |
| 66 return false; | |
| 67 } | |
| 68 return true; | |
| 69 } | |
| 70 | |
| 71 bool ArcGpuVideoDecodeAccelerator::SetBufferCount(PortType port, | |
| 72 size_t* count) { | |
|
Pawel Osciak
2016/02/29 08:25:20
We are not assigning to count, do we need it to be
Owen Lin
2016/03/03 06:30:42
That's because VDA doesn't returns false even on f
| |
| 73 DVLOG(5) << "SetBufferCount(port=" << port << ", count=" << *count << ")"; | |
| 74 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 75 if (!ValidatePort(port)) | |
| 76 return false; | |
| 77 if (vda_ == nullptr) { | |
| 78 DVLOG(1) << "Must call setBufferFormat before setBufferCount"; | |
|
Pawel Osciak
2016/02/29 08:25:19
VDA is now created in Initialize()... But ideally
Owen Lin
2016/03/03 06:30:42
Done.
| |
| 79 return false; | |
| 80 } | |
| 81 if (port == PORT_OUTPUT) { | |
|
Pawel Osciak
2016/02/29 08:25:19
We should probably check we don't have any existin
Owen Lin
2016/03/03 06:30:43
I think VDA should handle this case?
| |
| 82 std::vector<media::PictureBuffer> buffers; | |
| 83 for (int32_t id = 0, n = *count; id < n; ++id) { | |
| 84 // TODO: Make sure the size is what we want. | |
| 85 buffers.push_back(media::PictureBuffer(id, coded_size_, 0)); | |
| 86 } | |
| 87 vda_->AssignPictureBuffers(buffers); | |
| 88 } | |
| 89 PortInfo* port_info = &port_info_[port]; | |
| 90 port_info->num_buffers = *count; | |
| 91 if (port == PORT_INPUT) { | |
|
Pawel Osciak
2016/02/29 08:25:19
Is it ok to allow SetBufferCount at any time for i
Owen Lin
2016/03/03 06:30:43
Will move this to be part of initialization.
| |
| 92 input_buffer_info_.clear(); | |
| 93 input_buffer_info_.resize(port_info->num_buffers); | |
| 94 } | |
| 95 return true; | |
| 96 } | |
| 97 | |
| 98 bool ArcGpuVideoDecodeAccelerator::SetBufferFormat(PortType port, | |
|
Pawel Osciak
2016/02/29 08:25:20
IIRC, we could not change format and memory type a
Owen Lin
2016/03/03 06:30:43
I agree. But this will involve changes of differen
| |
| 99 const BufferFormat& format) { | |
| 100 DVLOG(5) << "SetBufferFormat(port=" << port | |
| 101 << ", format=(memory_type=" << format.memory_type | |
| 102 << ", pixel_format=" << format.pixel_format << "))"; | |
| 103 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 104 if (!ValidatePort(port)) | |
| 105 return false; | |
| 106 switch (port) { | |
| 107 case PORT_INPUT: { | |
| 108 PortInfo* port_info = &port_info_[port]; | |
| 109 if (format.memory_type != MEMORY_SHARED_MEMORY) { | |
| 110 DVLOG(1) << "Only SharedMemory is supported for input buffers"; | |
| 111 return false; | |
| 112 } | |
| 113 port_info->memory_type = format.memory_type; | |
| 114 media::VideoDecodeAccelerator::Config config; | |
| 115 switch (format.pixel_format) { | |
| 116 case HAL_PIXEL_FORMAT_H264: | |
| 117 config.profile = media::H264PROFILE_MAIN; | |
|
Pawel Osciak
2016/02/29 08:25:20
On second thought, could we pass the exact profile
Owen Lin
2016/03/03 06:30:42
I don't think we know the exact profile. MediaCode
| |
| 118 break; | |
| 119 case HAL_PIXEL_FORMAT_VP8: | |
| 120 config.profile = media::VP8PROFILE_ANY; | |
| 121 break; | |
| 122 default: | |
| 123 DVLOG(1) << "Unsupported input format: " << format.pixel_format; | |
| 124 return false; | |
| 125 } | |
| 126 config.output_mode = | |
| 127 media::VideoDecodeAccelerator::Config::OutputMode::IMPORT; | |
| 128 if (!vda_->Initialize(config, this)) { | |
|
Pawel Osciak
2016/02/29 08:25:20
We should probably make sure vda_ is not null, oth
Owen Lin
2016/03/03 06:30:42
Done.
| |
| 129 DVLOG(1) << "VDA::Initialize() failed."; | |
| 130 return false; | |
| 131 } | |
| 132 break; | |
| 133 } | |
| 134 case PORT_OUTPUT: { | |
| 135 PortInfo* port_info = &port_info_[port]; | |
| 136 if (format.memory_type != MEMORY_DMABUF) { | |
| 137 DVLOG(1) << "Only DMA buffer is supported for output buffers"; | |
|
Pawel Osciak
2016/02/29 08:25:19
s/DMA buffer/dmabuf/
Owen Lin
2016/03/03 06:30:42
Done.
| |
| 138 return false; | |
| 139 } | |
| 140 port_info->memory_type = format.memory_type; | |
| 141 break; | |
| 142 } | |
| 143 default: | |
| 144 NOTREACHED() << "Invalid port: " << port; | |
| 145 return false; | |
| 146 } | |
| 147 return true; | |
| 148 } | |
| 149 | |
| 150 bool ArcGpuVideoDecodeAccelerator::ValidatePort(PortType port) { | |
| 151 if (port != PORT_INPUT && port != PORT_OUTPUT) { | |
| 152 DVLOG(1) << "Invalid port: " << port; | |
| 153 return false; | |
| 154 } | |
| 155 return true; | |
| 156 } | |
| 157 | |
| 158 bool ArcGpuVideoDecodeAccelerator::ValidatePortAndIndex(PortType port, | |
| 159 uint32_t index) { | |
| 160 if (!ValidatePort(port)) | |
| 161 return false; | |
| 162 if (index >= port_info_[port].num_buffers) { | |
| 163 DVLOG(1) << "Invalid buffer - port: " << port << ", index: " << index; | |
| 164 return false; | |
| 165 } | |
| 166 return true; | |
| 167 } | |
| 168 | |
| 169 bool ArcGpuVideoDecodeAccelerator::BindSharedMemory(PortType port, | |
| 170 uint32_t index, | |
| 171 int ashmem_fd, | |
| 172 size_t offset, | |
|
Pawel Osciak
2016/02/29 08:25:20
off_t ?
Owen Lin
2016/03/03 06:30:42
Done.
| |
| 173 size_t length) { | |
| 174 DVLOG(5) << "ArcGVDA::BindSharedMemory, offset: " << offset | |
| 175 << ", length: " << length; | |
| 176 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 177 | |
| 178 // Make sure we will close the file descriptor. | |
| 179 base::ScopedFD handle(ashmem_fd); | |
| 180 if (!ValidatePortAndIndex(port, index)) | |
| 181 return false; | |
| 182 if (port != PORT_INPUT) { | |
|
Pawel Osciak
2016/02/29 08:25:20
Should we instead check against port_info->memory_
Owen Lin
2016/03/03 06:30:43
port_info is just removed.
| |
| 183 DVLOG(1) << "SharedBuffer is only supported for input"; | |
| 184 return false; | |
| 185 } | |
| 186 InputBufferInfo* input_info = &input_buffer_info_[index]; | |
| 187 input_info->handle = std::move(handle); | |
| 188 input_info->offset = offset; | |
| 189 input_info->length = length; | |
| 190 return true; | |
| 191 } | |
| 192 | |
| 193 bool ArcGpuVideoDecodeAccelerator::BindDmabuf(PortType port, | |
| 194 uint32_t index, | |
| 195 int dmabuf_fd) { | |
| 196 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 197 base::ScopedFD handle(dmabuf_fd); | |
| 198 | |
| 199 // Make sure we will close the file descriptor. | |
| 200 if (!ValidatePortAndIndex(port, index)) | |
| 201 return false; | |
| 202 if (port != PORT_OUTPUT) { | |
|
Pawel Osciak
2016/02/29 08:25:20
Here as well, should we instead check against port
Owen Lin
2016/03/03 06:30:42
same as above.
| |
| 203 DVLOG(1) << "GraphicBuffer is only supported for input"; | |
| 204 return false; | |
| 205 } | |
| 206 std::vector<base::ScopedFD> dmabuf_fds; | |
| 207 dmabuf_fds.push_back(std::move(handle)); | |
| 208 vda_->ImportBufferForPicture(index, std::move(dmabuf_fds)); | |
| 209 return true; | |
| 210 } | |
| 211 | |
| 212 void ArcGpuVideoDecodeAccelerator::UseBuffer(PortType port, | |
| 213 uint32_t index, | |
| 214 const BufferMetadata& metadata) { | |
| 215 DVLOG(5) << "UseBuffer(port=" << port << ", index=" << index | |
| 216 << ", metadata=(bytes_used=" << metadata.bytes_used | |
| 217 << ", timestamp=" << metadata.timestamp << "))"; | |
| 218 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 219 if (!ValidatePortAndIndex(port, index)) { | |
| 220 arc_client_->OnError(INVALID_ARGUMENT); | |
| 221 return; | |
| 222 } | |
| 223 switch (port) { | |
| 224 case PORT_INPUT: { | |
| 225 InputBufferInfo* input_info = &input_buffer_info_[index]; | |
| 226 if (metadata.flags & BUFFER_FLAG_EOS) { | |
| 227 // TODO(owenlin): Ask VDA to return all output pictures so that we | |
| 228 // can output an EOS picture when Flush() is done. | |
| 229 vda_->Flush(); | |
| 230 } | |
| 231 if (metadata.bytes_used == 0) { | |
|
Pawel Osciak
2016/02/29 08:25:19
Could we handle this case in VDAs? We already do I
Owen Lin
2016/03/03 06:30:42
Done.
| |
| 232 arc_client_->OnBufferDone(PORT_INPUT, index, BufferMetadata()); | |
| 233 return; | |
| 234 } | |
| 235 if (metadata.bytes_used > input_info->length) { | |
|
Pawel Osciak
2016/02/29 08:25:19
I think this also should be validated in the VDA,
Owen Lin
2016/03/03 06:30:42
Yes, you're right.
| |
| 236 DVLOG(1) << "Invalid bytes_used: " << metadata.bytes_used << " > " | |
| 237 << input_info->length; | |
| 238 arc_client_->OnError(INVALID_ARGUMENT); | |
| 239 return; | |
| 240 } | |
| 241 int32_t bitstream_buffer_id = next_bitstream_buffer_id_; | |
| 242 // Mask against 30 bits, to avoid (undefined wraparound on signed integer) | |
| 243 next_bitstream_buffer_id_ = (next_bitstream_buffer_id_ + 1) & 0x3FFFFFFF; | |
| 244 SetInputRecord(bitstream_buffer_id, index, metadata.timestamp); | |
| 245 int dup_fd = HANDLE_EINTR(dup(input_info->handle.get())); | |
| 246 if (dup_fd < 0) { | |
| 247 DVLOG(1) << "dup() failed."; | |
| 248 arc_client_->OnError(PLATFORM_FAILURE); | |
| 249 return; | |
| 250 } | |
| 251 vda_->Decode(media::BitstreamBuffer( | |
| 252 bitstream_buffer_id, base::SharedMemoryHandle(dup_fd, true), | |
| 253 metadata.bytes_used, input_info->offset)); | |
| 254 break; | |
| 255 } | |
| 256 case PORT_OUTPUT: { | |
| 257 SendEosIfNeededOrReusePicture(index); | |
| 258 break; | |
| 259 } | |
| 260 default: | |
| 261 NOTREACHED(); | |
| 262 } | |
| 263 } | |
| 264 | |
| 265 void ArcGpuVideoDecodeAccelerator::Reset() { | |
| 266 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 267 base::RunLoop loop; | |
| 268 reset_done_callback_ = loop.QuitClosure(); | |
| 269 vda_->Reset(); | |
| 270 base::MessageLoop::ScopedNestableTaskAllower allow( | |
| 271 base::MessageLoop::current()); | |
| 272 // Wait for the ResetDone callback. | |
| 273 loop.Run(); | |
| 274 } | |
| 275 | |
| 276 void ArcGpuVideoDecodeAccelerator::ProvidePictureBuffers( | |
| 277 uint32_t requested_num_of_buffers, | |
| 278 const gfx::Size& dimensions, | |
| 279 uint32_t texture_target) { | |
| 280 DVLOG(5) << "ProvidePictureBuffers(" | |
| 281 << "requested_num_of_buffers=" << requested_num_of_buffers | |
| 282 << ", dimensions=" << dimensions.ToString() << ")"; | |
| 283 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 284 coded_size_ = dimensions; | |
| 285 // TODO(owenlin): use VDA::GetOutputFormat() here and calculate correct | |
| 286 // |image_size|. | |
| 287 VideoFormat video_format; | |
| 288 video_format.image_size = dimensions.GetArea() * 3 / 2; | |
| 289 output_buffer_size_ = video_format.image_size; | |
| 290 video_format.min_num_buffers = requested_num_of_buffers; | |
| 291 video_format.coded_width = dimensions.width(); | |
| 292 video_format.coded_height = dimensions.height(); | |
| 293 // TODO(owenlin): How to get visible size? | |
|
Pawel Osciak
2016/02/29 08:25:20
Do we need it at this time for the client?
Owen Lin
2016/03/03 06:30:42
Maybe not, I think client needs the visible size b
| |
| 294 video_format.crop_top = 0; | |
| 295 video_format.crop_left = 0; | |
| 296 video_format.crop_width = dimensions.width(); | |
| 297 video_format.crop_height = dimensions.height(); | |
| 298 arc_client_->OnOutputFormatChanged(video_format); | |
| 299 } | |
| 300 | |
| 301 void ArcGpuVideoDecodeAccelerator::DismissPictureBuffer( | |
| 302 int32_t picture_buffer) { | |
| 303 // no-op | |
|
Pawel Osciak
2016/02/29 08:25:20
I'm wondering if we should still clean up here, i.
Owen Lin
2016/03/03 06:30:42
There should not be any fd owned by us and need to
| |
| 304 } | |
| 305 | |
| 306 void ArcGpuVideoDecodeAccelerator::PictureReady(const media::Picture& picture) { | |
| 307 DVLOG(5) << "PictureReady(picture_buffer_id=" << picture.picture_buffer_id() | |
| 308 << ", bitstream_buffer_id=" << picture.bitstream_buffer_id(); | |
| 309 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 310 if (!ValidatePortAndIndex(PORT_OUTPUT, picture.picture_buffer_id())) { | |
|
Pawel Osciak
2016/02/29 08:25:20
Do we need to validate PR(), since it's coming fro
Owen Lin
2016/03/03 06:30:43
Done.
| |
| 311 DVLOG(1) << "Invalid index: " << picture.picture_buffer_id(); | |
| 312 arc_client_->OnError(PLATFORM_FAILURE); | |
| 313 return; | |
| 314 } | |
| 315 | |
| 316 // Empty buffer, returned in Flushing. | |
| 317 if (picture.bitstream_buffer_id() == -1) { | |
| 318 buffers_pending_eos_.push(picture.picture_buffer_id()); | |
| 319 return; | |
| 320 } | |
| 321 InputRecord* input_record = FindInputRecord(picture.bitstream_buffer_id()); | |
| 322 if (input_record == nullptr) { | |
| 323 DVLOG(1) << "Cannot find for bitstream buffer id: " | |
| 324 << picture.bitstream_buffer_id(); | |
| 325 arc_client_->OnError(PLATFORM_FAILURE); | |
| 326 return; | |
| 327 } | |
| 328 | |
| 329 BufferMetadata metadata; | |
| 330 metadata.timestamp = input_record->timestamp; | |
| 331 metadata.bytes_used = output_buffer_size_; | |
| 332 arc_client_->OnBufferDone(PORT_OUTPUT, picture.picture_buffer_id(), metadata); | |
| 333 } | |
| 334 | |
| 335 void ArcGpuVideoDecodeAccelerator::NotifyEndOfBitstreamBuffer( | |
| 336 int32_t bitstream_buffer_id) { | |
| 337 DVLOG(5) << "NotifyEndOfBitstreamBuffer(" << bitstream_buffer_id << ")"; | |
| 338 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 339 InputRecord* input_record = FindInputRecord(bitstream_buffer_id); | |
| 340 if (input_record == nullptr) { | |
| 341 arc_client_->OnError(PLATFORM_FAILURE); | |
| 342 return; | |
| 343 } | |
| 344 arc_client_->OnBufferDone(PORT_INPUT, input_record->buffer_index, | |
| 345 BufferMetadata()); | |
| 346 } | |
| 347 | |
| 348 void ArcGpuVideoDecodeAccelerator::NotifyFlushDone() { | |
| 349 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 350 pending_eos_output_buffer_ = true; | |
| 351 while (!buffers_pending_eos_.empty()) { | |
| 352 SendEosIfNeededOrReusePicture(buffers_pending_eos_.front()); | |
| 353 buffers_pending_eos_.pop(); | |
| 354 } | |
| 355 } | |
| 356 | |
| 357 void ArcGpuVideoDecodeAccelerator::NotifyResetDone() { | |
| 358 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 359 base::ResetAndReturn(&reset_done_callback_).Run(); | |
| 360 } | |
| 361 | |
| 362 static ArcVideoAccelerator::Error ConvertErrorCode( | |
| 363 media::VideoDecodeAccelerator::Error error) { | |
| 364 switch (error) { | |
| 365 case media::VideoDecodeAccelerator::ILLEGAL_STATE: | |
| 366 return ArcVideoAccelerator::ILLEGAL_STATE; | |
| 367 case media::VideoDecodeAccelerator::INVALID_ARGUMENT: | |
| 368 return ArcVideoAccelerator::INVALID_ARGUMENT; | |
| 369 case media::VideoDecodeAccelerator::UNREADABLE_INPUT: | |
| 370 return ArcVideoAccelerator::UNREADABLE_INPUT; | |
| 371 case media::VideoDecodeAccelerator::PLATFORM_FAILURE: | |
| 372 return ArcVideoAccelerator::PLATFORM_FAILURE; | |
| 373 default: | |
| 374 DVLOG(1) << "Unknown error: " << error; | |
| 375 return ArcVideoAccelerator::PLATFORM_FAILURE; | |
| 376 } | |
| 377 } | |
| 378 | |
| 379 void ArcGpuVideoDecodeAccelerator::NotifyError( | |
| 380 media::VideoDecodeAccelerator::Error error) { | |
| 381 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 382 arc_client_->OnError(ConvertErrorCode(error)); | |
| 383 } | |
| 384 | |
| 385 void ArcGpuVideoDecodeAccelerator::SendEosIfNeededOrReusePicture( | |
| 386 uint32_t index) { | |
| 387 if (pending_eos_output_buffer_) { | |
| 388 BufferMetadata metadata; | |
| 389 metadata.flags = BUFFER_FLAG_EOS; | |
| 390 arc_client_->OnBufferDone(PORT_OUTPUT, index, metadata); | |
| 391 pending_eos_output_buffer_ = false; | |
| 392 } else { | |
| 393 vda_->ReusePictureBuffer(index); | |
| 394 } | |
| 395 } | |
| 396 | |
| 397 void ArcGpuVideoDecodeAccelerator::SetInputRecord(int32_t bitstream_buffer_id, | |
|
Pawel Osciak
2016/02/29 08:25:20
Since we can't have more inputs than buffer indice
Owen Lin
2016/03/03 06:30:42
InputRecord could be more than InputBuffers. Assum
| |
| 398 uint32_t buffer_index, | |
| 399 int64_t timestamp) { | |
| 400 input_records_.push_front( | |
| 401 InputRecord(bitstream_buffer_id, buffer_index, timestamp)); | |
| 402 // The same value copied from media::GpuVideoDecoder. | |
| 403 const size_t kMaxNumberOfInputRecords = 128; | |
| 404 if (input_records_.size() > kMaxNumberOfInputRecords) | |
| 405 input_records_.pop_back(); | |
| 406 } | |
| 407 | |
| 408 ArcGpuVideoDecodeAccelerator::InputRecord* | |
| 409 ArcGpuVideoDecodeAccelerator::FindInputRecord(int32_t bitstream_buffer_id) { | |
| 410 for (auto& record : input_records_) { | |
| 411 if (record.bitstream_buffer_id == bitstream_buffer_id) | |
| 412 return &record; | |
| 413 } | |
| 414 return nullptr; | |
| 415 } | |
| 416 | |
| 417 } // namespace arc | |
| 418 } // namespace chromeos | |
| OLD | NEW |