Chromium Code Reviews| Index: content/common/gpu/media/vaapi_wrapper.cc |
| diff --git a/content/common/gpu/media/vaapi_wrapper.cc b/content/common/gpu/media/vaapi_wrapper.cc |
| index 5250e590eacce33206bcade8a8e8ce385271d971..67515212e39d645126f8e13577e3e41a5b00cefc 100644 |
| --- a/content/common/gpu/media/vaapi_wrapper.cc |
| +++ b/content/common/gpu/media/vaapi_wrapper.cc |
| @@ -7,10 +7,12 @@ |
| #include <dlfcn.h> |
| #include "base/bind.h" |
| +#include "base/callback_helpers.h" |
| #include "base/logging.h" |
| #include "base/numerics/safe_conversions.h" |
| // Auto-generated for dlopen libva libraries |
| #include "content/common/gpu/media/va_stubs.h" |
| +#include "third_party/libyuv/include/libyuv.h" |
| using content_common_gpu_media::kModuleVa; |
| using content_common_gpu_media::InitializeStubs; |
| @@ -43,6 +45,18 @@ static const base::FilePath::CharType kVaLib[] = |
| namespace content { |
| +// Config attributes common for both encode and decode. |
| +static const VAConfigAttrib kCommonVAConfigAttribs[] = { |
| + {VAConfigAttribRTFormat, VA_RT_FORMAT_YUV420}, |
|
wuchengli
2014/06/17 14:33:12
I'm curious. What does RT stand for?
Pawel Osciak
2014/06/18 07:05:08
My guess is raw texture, but I may be wrong.
|
| +}; |
| + |
| +// Attributes required for encode. |
| +static const VAConfigAttrib kEncodeVAConfigAttribs[] = { |
| + {VAConfigAttribRateControl, VA_RC_CBR}, |
| + {VAConfigAttribEncPackedHeaders, |
| + VA_ENC_PACKED_HEADER_SEQUENCE | VA_ENC_PACKED_HEADER_PICTURE}, |
| +}; |
| + |
| // Maps Profile enum values to VaProfile values. |
| static VAProfile ProfileToVAProfile( |
| media::VideoCodecProfile profile, |
| @@ -106,17 +120,20 @@ VaapiWrapper::VaapiWrapper() |
| VaapiWrapper::~VaapiWrapper() { |
| DestroyPendingBuffers(); |
| + DestroyCodedBuffers(); |
| DestroySurfaces(); |
| Deinitialize(); |
| } |
| scoped_ptr<VaapiWrapper> VaapiWrapper::Create( |
| + CodecMode mode, |
| media::VideoCodecProfile profile, |
| Display* x_display, |
| const base::Closure& report_error_to_uma_cb) { |
| scoped_ptr<VaapiWrapper> vaapi_wrapper(new VaapiWrapper()); |
| - if (!vaapi_wrapper->Initialize(profile, x_display, report_error_to_uma_cb)) |
| + if (!vaapi_wrapper->Initialize( |
| + mode, profile, x_display, report_error_to_uma_cb)) |
| vaapi_wrapper.reset(); |
| return vaapi_wrapper.Pass(); |
| @@ -134,7 +151,8 @@ void VaapiWrapper::TryToSetVADisplayAttributeToLocalGPU() { |
| DVLOG(2) << "vaSetDisplayAttributes unsupported, ignoring by default."; |
| } |
| -bool VaapiWrapper::Initialize(media::VideoCodecProfile profile, |
| +bool VaapiWrapper::Initialize(CodecMode mode, |
| + media::VideoCodecProfile profile, |
| Display* x_display, |
| const base::Closure& report_error_to_uma_cb) { |
| static bool vaapi_functions_initialized = PostSandboxInitialization(); |
| @@ -184,21 +202,72 @@ bool VaapiWrapper::Initialize(media::VideoCodecProfile profile, |
| return false; |
| } |
| - VAConfigAttrib attrib = {VAConfigAttribRTFormat, 0}; |
| - const VAEntrypoint kEntrypoint = VAEntrypointVLD; |
| - va_res = vaGetConfigAttributes(va_display_, va_profile, kEntrypoint, |
| - &attrib, 1); |
| - VA_SUCCESS_OR_RETURN(va_res, "vaGetConfigAttributes failed", false); |
| + // Query the driver for supported entrypoints. |
| + int max_entrypoints = vaMaxNumEntrypoints(va_display_); |
| + std::vector<VAEntrypoint> supported_entrypoints( |
| + base::checked_cast<size_t>(max_entrypoints)); |
| + |
| + int num_supported_entrypoints; |
| + va_res = vaQueryConfigEntrypoints(va_display_, |
| + va_profile, |
| + &supported_entrypoints[0], |
| + &num_supported_entrypoints); |
| + VA_SUCCESS_OR_RETURN(va_res, "vaQueryConfigEntrypoints failed", false); |
| + if (num_supported_entrypoints < 0 || |
| + num_supported_entrypoints > max_entrypoints) { |
| + DVLOG(1) << "vaQueryConfigEntrypoints returned: " |
|
wuchengli
2014/06/17 14:33:12
Doesn't this mean something is wrong? LOG(WARNING)
Pawel Osciak
2014/06/18 07:05:08
LOG() would also show up in release. This goes bac
|
| + << num_supported_entrypoints; |
| + return false; |
| + } |
| - if (!(attrib.value & VA_RT_FORMAT_YUV420)) { |
| - DVLOG(1) << "YUV420 not supported by this VAAPI implementation"; |
| + VAEntrypoint entrypoint = |
| + (mode == kEncode ? VAEntrypointEncSlice : VAEntrypointVLD); |
| + |
| + if (std::find(supported_entrypoints.begin(), |
| + supported_entrypoints.end(), |
| + entrypoint) == supported_entrypoints.end()) { |
| + DVLOG(1) << "Unsupported entrypoint"; |
|
wuchengli
2014/06/17 14:33:12
print |entrypoint|?
Pawel Osciak
2014/06/18 07:05:08
Done.
|
| return false; |
| } |
| + // Query the driver for required attributes. |
| + std::vector<VAConfigAttrib> required_attribs; |
| + required_attribs.insert( |
| + required_attribs.end(), |
| + kCommonVAConfigAttribs, |
|
wuchengli
2014/06/17 14:33:12
This can be inserted by constructor.
Pawel Osciak
2014/06/18 07:05:09
It'd have to be a member, but we don't really need
|
| + kCommonVAConfigAttribs + arraysize(kCommonVAConfigAttribs)); |
| + if (mode == kEncode) { |
| + required_attribs.insert( |
| + required_attribs.end(), |
| + kEncodeVAConfigAttribs, |
| + kEncodeVAConfigAttribs + arraysize(kEncodeVAConfigAttribs)); |
| + } |
| + |
| + std::vector<VAConfigAttrib> attribs = required_attribs; |
|
wuchengli
2014/06/17 14:33:12
nit: the constructor can take a vector. Not sure w
Pawel Osciak
2014/06/18 07:05:09
In C++ assignment on definition calls the copy con
|
| + for (size_t i = 0; i < required_attribs.size(); ++i) |
| + attribs[i].value = 0; |
| + |
| + va_res = vaGetConfigAttributes( |
| + va_display_, va_profile, entrypoint, &attribs[0], attribs.size()); |
| + VA_SUCCESS_OR_RETURN(va_res, "vaGetConfigAttributes failed", false); |
| + |
| + for (size_t i = 0; i < required_attribs.size(); ++i) { |
| + if (attribs[i].type != required_attribs[i].type || |
|
wuchengli
2014/06/17 14:33:12
No need to compare the type according to the funct
Pawel Osciak
2014/06/18 07:05:09
Yes, but I'd still prefer to keep this.
|
| + !(attribs[i].value & required_attribs[i].value)) { |
|
wuchengli
2014/06/17 14:33:12
Should be (attribs[i].value & required_attribs[i].
Pawel Osciak
2014/06/18 07:05:09
Good catch.
|
| + DVLOG(1) << "Unsupported value " << required_attribs[i].value |
| + << " for attribute type " << required_attribs[i].type; |
| + return false; |
| + } |
| + } |
| + |
| TryToSetVADisplayAttributeToLocalGPU(); |
| - va_res = vaCreateConfig(va_display_, va_profile, kEntrypoint, |
| - &attrib, 1, &va_config_id_); |
| + va_res = vaCreateConfig(va_display_, |
| + va_profile, |
| + entrypoint, |
| + &required_attribs[0], |
| + required_attribs.size(), |
| + &va_config_id_); |
| VA_SUCCESS_OR_RETURN(va_res, "vaCreateConfig failed", false); |
| return true; |
| @@ -299,6 +368,7 @@ bool VaapiWrapper::SubmitBuffer(VABufferType va_buffer_type, |
| switch (va_buffer_type) { |
| case VASliceParameterBufferType: |
| case VASliceDataBufferType: |
| + case VAEncSliceParameterBufferType: |
| pending_slice_bufs_.push_back(buffer_id); |
| break; |
| @@ -310,6 +380,43 @@ bool VaapiWrapper::SubmitBuffer(VABufferType va_buffer_type, |
| return true; |
| } |
| +bool VaapiWrapper::SubmitVAEncMiscParamBuffer( |
| + VAEncMiscParameterType misc_param_type, |
| + size_t size, |
| + void* buffer) { |
| + base::AutoLock auto_lock(va_lock_); |
| + |
| + VABufferID buffer_id; |
| + VAStatus va_res = vaCreateBuffer(va_display_, |
| + va_context_id_, |
| + VAEncMiscParameterBufferType, |
| + sizeof(VAEncMiscParameterBuffer) + size, |
| + 1, |
| + NULL, |
| + &buffer_id); |
| + VA_SUCCESS_OR_RETURN(va_res, "Failed to create a VA buffer", false); |
| + |
| + void* data_ptr = NULL; |
| + va_res = vaMapBuffer(va_display_, buffer_id, &data_ptr); |
| + VA_LOG_ON_ERROR(va_res, "vaMapBuffer failed"); |
|
wuchengli
2014/06/17 14:33:12
Use LOG_VA_ERROR_AND_REPORT and move it to if clau
Pawel Osciak
2014/06/18 07:05:08
While you are right, honestly we have a lot of thi
|
| + if (va_res != VA_STATUS_SUCCESS) { |
| + vaDestroyBuffer(va_display_, buffer_id); |
| + return false; |
| + } |
| + |
| + DCHECK(data_ptr); |
|
wuchengli
2014/06/17 14:33:13
Do we need this? The code will crash in misc_param
Pawel Osciak
2014/06/18 07:05:09
But the crash may not be as obvious.
|
| + |
| + VAEncMiscParameterBuffer* misc_param = |
| + reinterpret_cast<VAEncMiscParameterBuffer*>(data_ptr); |
| + misc_param->type = misc_param_type; |
| + memcpy(misc_param->data, buffer, size); |
| + va_res = vaUnmapBuffer(va_display_, buffer_id); |
| + VA_LOG_ON_ERROR(va_res, "vaUnmapBuffer failed"); |
|
wuchengli
2014/06/17 14:33:12
If unmap fails, do things still work later?
Pawel Osciak
2014/06/18 07:05:08
We'll destroy the buffer anyway.
|
| + |
| + pending_va_bufs_.push_back(buffer_id); |
| + return true; |
| +} |
| + |
| void VaapiWrapper::DestroyPendingBuffers() { |
| base::AutoLock auto_lock(va_lock_); |
| @@ -327,38 +434,71 @@ void VaapiWrapper::DestroyPendingBuffers() { |
| pending_slice_bufs_.clear(); |
| } |
| -bool VaapiWrapper::SubmitDecode(VASurfaceID va_surface_id) { |
| +bool VaapiWrapper::CreateCodedBuffer(size_t size, VABufferID* buffer_id) { |
| + base::AutoLock auto_lock(va_lock_); |
| + VAStatus va_res = vaCreateBuffer(va_display_, |
| + va_context_id_, |
| + VAEncCodedBufferType, |
| + size, |
| + 1, |
| + NULL, |
| + buffer_id); |
| + VA_SUCCESS_OR_RETURN(va_res, "Failed to create a coded buffer", false); |
| + |
| + DCHECK(coded_buffers_.insert(*buffer_id).second); |
| + return true; |
| +} |
| + |
| +void VaapiWrapper::DestroyCodedBuffers() { |
|
wuchengli
2014/06/17 14:33:12
Need auto_lock(va_lock_) because this method is pu
Pawel Osciak
2014/06/18 07:05:09
Oh good catch. I missed it when refactoring.
|
| + for (std::set<VABufferID>::const_iterator iter = coded_buffers_.begin(); |
| + iter != coded_buffers_.end(); |
| + ++iter) { |
| + VAStatus va_res = vaDestroyBuffer(va_display_, *iter); |
| + VA_LOG_ON_ERROR(va_res, "vaDestroyBuffer failed"); |
| + } |
| + |
| + coded_buffers_.clear(); |
| +} |
| + |
| +bool VaapiWrapper::Execute(VASurfaceID va_surface_id) { |
| base::AutoLock auto_lock(va_lock_); |
| DVLOG(4) << "Pending VA bufs to commit: " << pending_va_bufs_.size(); |
| DVLOG(4) << "Pending slice bufs to commit: " << pending_slice_bufs_.size(); |
| - DVLOG(4) << "Decoding into VA surface " << va_surface_id; |
| + DVLOG(4) << "Target VA surface " << va_surface_id; |
| - // Get ready to decode into surface. |
| + // Get ready to execute for given surface. |
| VAStatus va_res = vaBeginPicture(va_display_, va_context_id_, |
| va_surface_id); |
| VA_SUCCESS_OR_RETURN(va_res, "vaBeginPicture failed", false); |
| - // Commit parameter and slice buffers. |
| - va_res = vaRenderPicture(va_display_, va_context_id_, |
| - &pending_va_bufs_[0], pending_va_bufs_.size()); |
| - VA_SUCCESS_OR_RETURN(va_res, "vaRenderPicture for va_bufs failed", false); |
| + if (pending_va_bufs_.size() > 0) { |
| + // Commit parameter and slice buffers. |
| + va_res = vaRenderPicture(va_display_, |
| + va_context_id_, |
| + &pending_va_bufs_[0], |
| + pending_va_bufs_.size()); |
| + VA_SUCCESS_OR_RETURN(va_res, "vaRenderPicture for va_bufs failed", false); |
| + } |
| - va_res = vaRenderPicture(va_display_, va_context_id_, |
| - &pending_slice_bufs_[0], |
| - pending_slice_bufs_.size()); |
| - VA_SUCCESS_OR_RETURN(va_res, "vaRenderPicture for slices failed", false); |
| + if (pending_slice_bufs_.size() > 0) { |
| + va_res = vaRenderPicture(va_display_, |
| + va_context_id_, |
| + &pending_slice_bufs_[0], |
| + pending_slice_bufs_.size()); |
| + VA_SUCCESS_OR_RETURN(va_res, "vaRenderPicture for slices failed", false); |
| + } |
| - // Instruct HW decoder to start processing committed buffers (decode this |
| - // picture). This does not block until the end of decode. |
| + // Instruct HW codec to start processing committed buffers. |
| + // Does not block and the job is not finished after it returns. |
|
wuchengli
2014/06/17 14:33:12
"after this returns" seems clearer. I think you me
Pawel Osciak
2014/06/18 07:05:09
Done.
|
| va_res = vaEndPicture(va_display_, va_context_id_); |
| VA_SUCCESS_OR_RETURN(va_res, "vaEndPicture failed", false); |
| return true; |
| } |
| -bool VaapiWrapper::DecodeAndDestroyPendingBuffers(VASurfaceID va_surface_id) { |
| - bool result = SubmitDecode(va_surface_id); |
| +bool VaapiWrapper::ExecuteAndDestroyPendingBuffers(VASurfaceID va_surface_id) { |
|
wuchengli
2014/06/17 14:33:12
Do we need to make sure Execute and DestroyPending
Pawel Osciak
2014/06/18 07:05:09
No.
|
| + bool result = Execute(va_surface_id); |
| DestroyPendingBuffers(); |
| return result; |
| } |
| @@ -378,8 +518,7 @@ bool VaapiWrapper::PutSurfaceIntoPixmap(VASurfaceID va_surface_id, |
| 0, 0, dest_size.width(), dest_size.height(), |
| 0, 0, dest_size.width(), dest_size.height(), |
| NULL, 0, 0); |
| - VA_SUCCESS_OR_RETURN(va_res, "Failed putting decode surface to pixmap", |
| - false); |
| + VA_SUCCESS_OR_RETURN(va_res, "Failed putting surface to pixmap", false); |
| return true; |
| } |
| @@ -403,15 +542,124 @@ bool VaapiWrapper::GetVaImageForTesting(VASurfaceID va_surface_id, |
| if (va_res == VA_STATUS_SUCCESS) |
| return true; |
| - vaDestroyImage(va_display_, image->image_id); |
| + va_res = vaDestroyImage(va_display_, image->image_id); |
| + VA_LOG_ON_ERROR(va_res, "vaDestroyImage failed"); |
| + |
| return false; |
| } |
| void VaapiWrapper::ReturnVaImageForTesting(VAImage* image) { |
| base::AutoLock auto_lock(va_lock_); |
| - vaUnmapBuffer(va_display_, image->buf); |
| - vaDestroyImage(va_display_, image->image_id); |
| + VAStatus va_res = vaUnmapBuffer(va_display_, image->buf); |
| + VA_LOG_ON_ERROR(va_res, "vaUnmapBuffer failed"); |
| + |
| + va_res = vaDestroyImage(va_display_, image->image_id); |
| + VA_LOG_ON_ERROR(va_res, "vaDestroyImage failed"); |
| +} |
| + |
| +static void DestroyVAImage(VADisplay va_display, VAImage image) { |
| + if (image.image_id != VA_INVALID_ID) |
|
wuchengli
2014/06/17 14:33:12
Should this a DCHECK or this can happen?
Pawel Osciak
2014/06/18 07:05:08
This is defensive.
|
| + vaDestroyImage(va_display, image.image_id); |
| +} |
| + |
| +bool VaapiWrapper::UploadVideoFrameToSurface( |
| + const scoped_refptr<media::VideoFrame>& frame, |
| + VASurfaceID va_surface_id) { |
| + base::AutoLock auto_lock(va_lock_); |
| + |
| + VAImage image; |
| + VAStatus va_res = vaDeriveImage(va_display_, va_surface_id, &image); |
| + VA_SUCCESS_OR_RETURN(va_res, "vaDeriveImage failed", false); |
| + base::ScopedClosureRunner vaimage_deleter( |
| + base::Bind(&DestroyVAImage, va_display_, image)); |
| + |
| + if (image.format.fourcc != VA_FOURCC_NV12) { |
| + DVLOG(1) << "Unsupported image format: " << image.format.fourcc; |
| + return false; |
| + } |
| + |
| + if (gfx::Rect(image.width, image.height) < gfx::Rect(frame->coded_size())) { |
| + DVLOG(1) << "Buffer too small to fit the frame."; |
| + return false; |
| + } |
| + |
| + void* image_ptr = NULL; |
| + va_res = vaMapBuffer(va_display_, image.buf, &image_ptr); |
| + VA_SUCCESS_OR_RETURN(va_res, "vaMapBuffer failed", false); |
| + DCHECK(image_ptr); |
| + |
| + int ret = 0; |
| + { |
| + base::AutoUnlock auto_unlock(va_lock_); |
| + ret = libyuv::I420ToNV12(frame->data(media::VideoFrame::kYPlane), |
| + frame->stride(media::VideoFrame::kYPlane), |
| + frame->data(media::VideoFrame::kUPlane), |
| + frame->stride(media::VideoFrame::kUPlane), |
| + frame->data(media::VideoFrame::kVPlane), |
| + frame->stride(media::VideoFrame::kVPlane), |
| + static_cast<uint8*>(image_ptr) + image.offsets[0], |
| + image.pitches[0], |
| + static_cast<uint8*>(image_ptr) + image.offsets[1], |
| + image.pitches[1], |
| + image.width, |
| + image.height); |
| + } |
| + |
| + va_res = vaUnmapBuffer(va_display_, image.buf); |
| + VA_LOG_ON_ERROR(va_res, "vaUnmapBuffer failed"); |
| + |
| + return ret == 0; |
| +} |
| + |
| +bool VaapiWrapper::DownloadAndDestroyCodedBuffer(VABufferID buffer, |
|
wuchengli
2014/06/17 14:33:12
buffer_id for consistency
Pawel Osciak
2014/06/18 07:05:08
Done.
|
| + VASurfaceID sync_surface_id, |
| + uint8* target_ptr, |
| + size_t target_size, |
| + size_t* coded_data_size) { |
| + base::AutoLock auto_lock(va_lock_); |
| + |
| + VAStatus va_res = vaSyncSurface(va_display_, sync_surface_id); |
| + VA_SUCCESS_OR_RETURN(va_res, "Failed syncing surface", false); |
| + |
| + VACodedBufferSegment* buffer_segment = NULL; |
| + va_res = vaMapBuffer( |
| + va_display_, buffer, reinterpret_cast<void**>(&buffer_segment)); |
| + VA_SUCCESS_OR_RETURN(va_res, "vaMapBuffer failed", false); |
| + DCHECK(target_ptr); |
| + |
| + { |
| + base::AutoUnlock auto_unlock(va_lock_); |
| + *coded_data_size = 0; |
| + |
| + while (buffer_segment) { |
| + DCHECK(buffer_segment->buf); |
| + |
| + if (buffer_segment->size > target_size) { |
| + DVLOG(1) << "Insufficient output buffer size"; |
| + break; |
| + } |
| + |
| + memcpy(target_ptr, buffer_segment->buf, buffer_segment->size); |
| + |
| + target_ptr += buffer_segment->size; |
| + *coded_data_size += buffer_segment->size; |
| + target_size -= buffer_segment->size; |
| + |
| + buffer_segment = |
| + reinterpret_cast<VACodedBufferSegment*>(buffer_segment->next); |
| + } |
| + } |
| + |
| + va_res = vaUnmapBuffer(va_display_, buffer); |
| + VA_LOG_ON_ERROR(va_res, "vaUnmapBuffer failed"); |
| + |
| + va_res = vaDestroyBuffer(va_display_, buffer); |
| + VA_LOG_ON_ERROR(va_res, "vaDestroyBuffer failed"); |
| + |
| + DCHECK(coded_buffers_.erase(buffer)); |
| + |
| + return buffer_segment == NULL; |
| } |
| // static |