Chromium Code Reviews| Index: content/common/gpu/media/arc/arc_gpu_video_decode_accelerator.cc |
| diff --git a/content/common/gpu/media/arc/arc_gpu_video_decode_accelerator.cc b/content/common/gpu/media/arc/arc_gpu_video_decode_accelerator.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..cf0accfb50a028ff7cfae385ed9eb93c941e2716 |
| --- /dev/null |
| +++ b/content/common/gpu/media/arc/arc_gpu_video_decode_accelerator.cc |
| @@ -0,0 +1,341 @@ |
| +// 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 "base/callback_helpers.h" |
| +#include "base/logging.h" |
| +#include "base/run_loop.h" |
| +#include "content/common/gpu/media/arc/arc_gpu_video_decode_accelerator.h" |
| + |
| +namespace content { |
| +namespace arc { |
| + |
| +ArcGpuVideoDecodeAccelerator::InputRecord::InputRecord() |
| + : offset(0), length(0), bitstream_buffer_id(-1), timestamp(0) {} |
| + |
| +ArcGpuVideoDecodeAccelerator::PortInfo::PortInfo() |
| + : memory_type(MEMORY_DMABUF), num_buffers(0) {} |
| + |
| +ArcGpuVideoDecodeAccelerator::ArcGpuVideoDecodeAccelerator() |
| + : arc_client_(nullptr), next_bitstream_buffer_id_(0) {} |
| + |
| +bool ArcGpuVideoDecodeAccelerator::Initialize( |
| + DeviceType device, |
| + ArcVideoAccelerator::Client* client) { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + if (device != DEVICE_DECODER) |
| + return false; |
| + DCHECK(client); |
| + DCHECK(!arc_client_); |
| + arc_client_ = client; |
| + vda_ = nullptr; // TODO: connect to a real vda. |
| + return true; |
| +} |
| + |
| +bool ArcGpuVideoDecodeAccelerator::SetBufferCount(PortType port, |
| + size_t* count) { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + if (!ValidatePort(port)) |
| + return false; |
| + if (vda_ == nullptr) { |
| + DVLOG(1) << "Must call setBufferFormat before setBufferCount"; |
| + return false; |
| + } |
| + if (port == PORT_OUTPUT) { |
| + std::vector<media::PictureBuffer> buffers; |
| + for (int32_t id = 0, n = *count; id < n; ++id) { |
|
kcwu
2016/01/20 09:11:05
how about just
for (int32_t id = 0; id < *count; +
Owen Lin
2016/02/02 09:08:06
Also prevent comparing signed and unsigned numbers
kcwu
2016/03/09 07:58:21
If so, how about just cast explicitly?
|
| + // TODO: Make sure the size is what we want. |
| + buffers.push_back(media::PictureBuffer(id, coded_size_, 0)); |
|
kcwu
2016/01/20 09:11:05
need to make sure the value of *count is not too b
Owen Lin
2016/02/02 09:08:06
Done.
|
| + } |
| + vda_->AssignPictureBuffers(buffers); |
| + } |
| + PortInfo* port_info = &port_info_[port]; |
| + port_info->num_buffers = *count; |
| + if (port == PORT_INPUT) { |
| + input_records_.clear(); |
| + input_records_.resize(port_info->num_buffers); |
| + } |
| + return true; |
| +} |
| + |
| +bool ArcGpuVideoDecodeAccelerator::SetBufferFormat(PortType port, |
| + const BufferFormat& format) { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + if (!ValidatePort(port)) |
| + return false; |
| + switch (port) { |
| + case PORT_INPUT: { |
| + PortInfo* port_info = &port_info_[port]; |
| + if (format.memory_type != MEMORY_SHARED_MEMORY) { |
| + DVLOG(1) << "Only SharedMemory is supported for input buffers"; |
|
kcwu
2016/01/20 09:11:05
I feel most of DVLOG(1) in this file should be DLO
Owen Lin
2016/02/02 09:08:06
Done.
|
| + return false; |
| + } |
| + port_info->memory_type = format.memory_type; |
| + media::VideoDecodeAccelerator::Config config; |
| + switch (format.pixel_format) { |
| + case HAL_PIXEL_FORMAT_H264: |
| + config.profile = media::H264PROFILE_MAIN; |
| + break; |
| + case HAL_PIXEL_FORMAT_VP8: |
| + config.profile = media::VP8PROFILE_ANY; |
| + break; |
| + default: |
| + DVLOG(1) << "Unsupported input format: " << format.pixel_format; |
| + return false; |
| + } |
| + config.output_mode = |
| + media::VideoDecodeAccelerator::Config::OutputMode::IMPORT; |
| + if (!vda_->Initialize(config, this)) { |
| + DVLOG(1) << "VDA::Initialize() failed."; |
| + return false; |
| + } |
| + break; |
| + } |
| + case PORT_OUTPUT: { |
| + PortInfo* port_info = &port_info_[port]; |
| + if (format.memory_type != MEMORY_DMABUF) { |
| + DVLOG(1) << "Only DMA buffer is supported for output buffers"; |
| + return false; |
| + } |
| + port_info->memory_type = format.memory_type; |
| + break; |
| + } |
| + default: |
| + NOTREACHED() << "Invalid port: " << port; |
| + return false; |
| + } |
| + return true; |
| +} |
| + |
| +bool ArcGpuVideoDecodeAccelerator::ValidatePort(PortType port) { |
|
kcwu
2016/01/20 09:11:04
Move these two helpers below or above, don't mix w
Owen Lin
2016/02/02 09:08:06
Done.
|
| + if (port != PORT_INPUT && port != PORT_OUTPUT) { |
| + DVLOG(1) << "Invalid port: " << port; |
| + return false; |
| + } |
| + return true; |
| +} |
| + |
| +bool ArcGpuVideoDecodeAccelerator::ValidatePortAndIndex(PortType port, |
| + uint32_t index) { |
| + if (!ValidatePort(port)) |
| + return false; |
| + if (index >= port_info_[port].num_buffers) { |
| + DVLOG(1) << "Invalid buffer - port: " << port << ", index: " << index; |
| + return false; |
| + } |
| + return true; |
| +} |
| + |
| +bool ArcGpuVideoDecodeAccelerator::BindSharedMemory(PortType port, |
| + uint32_t index, |
| + int ashmem_fd, |
| + size_t offset, |
| + size_t length) { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + |
| + // Make sure we will close the file descriptor. |
| + base::ScopedFD handle(ashmem_fd); |
| + if (!ValidatePortAndIndex(port, index)) |
| + return false; |
| + if (port != PORT_INPUT) { |
| + DVLOG(1) << "SharedBuffer is only supported for input"; |
| + return false; |
| + } |
| + InputRecord* input_record = &input_records_[index]; |
| + input_record->handle = std::move(handle); |
| + input_record->offset = offset; |
| + input_record->length = length; |
| + return true; |
| +} |
| + |
| +bool ArcGpuVideoDecodeAccelerator::BindDmabuf(PortType port, |
| + uint32_t index, |
| + int dmabuf_fd) { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + base::ScopedFD handle(dmabuf_fd); |
| + |
| + // Make sure we will close the file descriptor. |
|
kcwu
2016/01/20 09:11:05
this comment should be in front of line 155
Owen Lin
2016/02/02 09:08:06
Done.
|
| + if (!ValidatePortAndIndex(port, index)) |
| + return false; |
| + if (port != PORT_OUTPUT) { |
| + DVLOG(1) << "GraphicBuffer is only supported for input"; |
| + return false; |
| + } |
| + std::vector<base::ScopedFD> dmabuf_fds; |
| + dmabuf_fds.push_back(std::move(handle)); |
| + vda_->ImportBufferForPicture(index, std::move(dmabuf_fds)); |
| + return true; |
| +} |
| + |
| +void ArcGpuVideoDecodeAccelerator::UseBuffer(PortType port, |
| + uint32_t index, |
| + const BufferMetadata& metadata) { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + if (!ValidatePortAndIndex(port, index)) { |
| + arc_client_->OnError(INVALID_ARGUMENT); |
| + return; |
| + } |
| + switch (port) { |
| + case PORT_INPUT: { |
| + InputRecord* input_record = &input_records_[index]; |
| + if (metadata.flags & BUFFER_FLAG_EOS) { |
| + // Ask VDA to return all output pictures so that we can output an EOS |
| + // picture when Flush() is done. |
| + // vda_->Flush(true); |
| + vda_->Flush(); |
| + } |
| + if (metadata.bytes_used == 0) { |
| + arc_client_->OnBufferDone(PORT_INPUT, index, BufferMetadata()); |
| + return; |
| + } |
| + input_record->bitstream_buffer_id = next_bitstream_buffer_id_; |
| + // Mask against 30 bits, to avoid (undefined wraparound on signed integer) |
| + next_bitstream_buffer_id_ = (next_bitstream_buffer_id_ + 1) & 0x3FFFFFFF; |
| + input_record->timestamp = metadata.timestamp; |
| + int dup_fd = HANDLE_EINTR(dup(input_record->handle.get())); |
| + if (dup_fd < 0) { |
| + DVLOG(1) << "dup() failed."; |
| + arc_client_->OnError(PLATFORM_FAILURE); |
| + return; |
| + } |
| + base::SharedMemoryHandle shared_memory_handle(dup_fd, true); |
| + // TODO(owenlin): Make BitstreamBuffer surpport offset and use here. |
| + media::BitstreamBuffer bitstream_buffer(input_record->bitstream_buffer_id, |
| + shared_memory_handle, |
| + input_record->length); |
| + vda_->Decode(bitstream_buffer); |
| + } |
|
kcwu
2016/01/20 09:11:05
break;
Owen Lin
2016/02/02 09:08:06
Thanks. I spend few minutes on this. :p
|
| + case PORT_OUTPUT: { |
| + SendEosIfNeededOrReusePicture(index); |
| + break; |
| + } |
| + default: |
| + NOTREACHED(); |
| + } |
| +} |
| + |
| +void ArcGpuVideoDecodeAccelerator::Reset() { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + base::RunLoop loop; |
| + reset_done_callback_ = loop.QuitClosure(); |
| + vda_->Reset(); |
|
kcwu
2016/01/20 09:11:05
check vda_ is not null.
Owen Lin
2016/02/02 09:08:06
Done.
|
| + base::MessageLoop::ScopedNestableTaskAllower allow( |
| + base::MessageLoop::current()); |
| + // Wait for the ResetDone callback. |
| + loop.Run(); |
| +} |
| + |
| +void ArcGpuVideoDecodeAccelerator::ProvidePictureBuffers( |
| + size_t requested_num_of_buffers, |
| + const gfx::Size& dimensions, |
| + uint32_t texture_target) { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + coded_size_ = dimensions; |
| + // TODO(owenlin): use VDA::GetOutputFormat() here. |
| + VideoFormat video_format; |
| + video_format.min_num_buffers = requested_num_of_buffers; |
| + video_format.coded_width = dimensions.width(); |
| + video_format.coded_height = dimensions.height(); |
| + arc_client_->OnOutputFormatChanged(video_format); |
| +} |
| + |
| +void ArcGpuVideoDecodeAccelerator::PictureReady(const media::Picture& picture) { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + if (!ValidatePortAndIndex(PORT_OUTPUT, picture.picture_buffer_id())) { |
| + DVLOG(1) << "Invalid index: " << picture.picture_buffer_id(); |
| + arc_client_->OnError(PLATFORM_FAILURE); |
| + return; |
| + } |
| + |
| + // Empty buffer, returned in Flushing. |
| + if (picture.bitstream_buffer_id() == -1) { |
| + buffers_pending_eos_.push(picture.picture_buffer_id()); |
| + return; |
| + } |
| + uint32_t index = |
| + FindInputRecordByBitstreamBufferId(picture.bitstream_buffer_id()); |
| + PortInfo* input_port = &port_info_[PORT_INPUT]; |
| + if (index >= input_port->num_buffers) { |
| + DVLOG(1) << "Cannot find for bitstream buffer id: " |
| + << picture.bitstream_buffer_id(); |
| + arc_client_->OnError(PLATFORM_FAILURE); |
| + return; |
| + } |
| + |
| + BufferMetadata metadata; |
| + metadata.timestamp = input_records_[index].timestamp; |
| + arc_client_->OnBufferDone(PORT_OUTPUT, picture.picture_buffer_id(), metadata); |
| +} |
| + |
| +void ArcGpuVideoDecodeAccelerator::NotifyEndOfBitstreamBuffer( |
| + int32_t bitstream_buffer_id) { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + uint32_t index = FindInputRecordByBitstreamBufferId(bitstream_buffer_id); |
| + if (!ValidatePortAndIndex(PORT_INPUT, index)) { |
| + arc_client_->OnError(PLATFORM_FAILURE); |
| + return; |
| + } |
| + arc_client_->OnBufferDone(PORT_INPUT, index, BufferMetadata()); |
| +} |
| + |
| +void ArcGpuVideoDecodeAccelerator::NotifyFlushDone() { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + pending_eos_output_buffer_ = true; |
| + while (!buffers_pending_eos_.empty()) { |
| + SendEosIfNeededOrReusePicture(buffers_pending_eos_.front()); |
| + buffers_pending_eos_.pop(); |
| + } |
| +} |
| + |
| +void ArcGpuVideoDecodeAccelerator::NotifyResetDone() { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + base::ResetAndReturn(&reset_done_callback_).Run(); |
| +} |
| + |
| +static ArcVideoAccelerator::Error ConvertErrorCode( |
| + media::VideoDecodeAccelerator::Error error) { |
| + switch (error) { |
| + case media::VideoDecodeAccelerator::ILLEGAL_STATE: |
| + return ArcVideoAccelerator::ILLEGAL_STATE; |
| + case media::VideoDecodeAccelerator::INVALID_ARGUMENT: |
| + return ArcVideoAccelerator::INVALID_ARGUMENT; |
| + case media::VideoDecodeAccelerator::UNREADABLE_INPUT: |
| + return ArcVideoAccelerator::UNREADABLE_INPUT; |
| + case media::VideoDecodeAccelerator::PLATFORM_FAILURE: |
| + return ArcVideoAccelerator::PLATFORM_FAILURE; |
| + default: |
| + DVLOG(1) << "Unknown error: " << error; |
| + return ArcVideoAccelerator::PLATFORM_FAILURE; |
| + } |
| +} |
| + |
| +void ArcGpuVideoDecodeAccelerator::NotifyError( |
| + media::VideoDecodeAccelerator::Error error) { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + arc_client_->OnError(ConvertErrorCode(error)); |
| +} |
| + |
| +void ArcGpuVideoDecodeAccelerator::SendEosIfNeededOrReusePicture( |
| + uint32_t index) { |
| + if (pending_eos_output_buffer_) { |
| + BufferMetadata metadata; |
| + metadata.flags = BUFFER_FLAG_EOS; |
| + arc_client_->OnBufferDone(PORT_OUTPUT, index, metadata); |
| + pending_eos_output_buffer_ = false; |
| + } else { |
| + vda_->ReusePictureBuffer(index); |
| + } |
| +} |
| + |
| +uint32_t ArcGpuVideoDecodeAccelerator::FindInputRecordByBitstreamBufferId( |
| + int32_t bitstream_buffer_id) { |
| + uint32_t index = 0; |
| + for (; index < input_records_.size(); ++index) { |
| + if (input_records_[index].bitstream_buffer_id == bitstream_buffer_id) |
| + break; |
| + } |
| + return index; |
| +} |
| + |
| +} // arc |
| +} // content |