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 53e8b6952424edadf8ed13f136486ed029884ce9..bb8d9abc3336d125d0f464de8972cea8a4965eab 100644 |
| --- a/content/common/gpu/media/vaapi_wrapper.cc |
| +++ b/content/common/gpu/media/vaapi_wrapper.cc |
| @@ -76,9 +76,13 @@ typedef VAStatus (*VaapiCreateSurfaces8)(VADisplay dpy, |
| unsigned int num_surfaces, |
| VASurfaceAttrib *attrib_list, |
| unsigned int num_attribs); |
| +typedef VAStatus (*VaapiDeriveImage)(VADisplay dpy, |
| + VASurfaceID surface, |
| + VAImage* image); |
| typedef VAStatus (*VaapiDestroyBuffer)(VADisplay dpy, VABufferID buffer_id); |
| typedef VAStatus (*VaapiDestroyConfig)(VADisplay dpy, VAConfigID config_id); |
| typedef VAStatus (*VaapiDestroyContext)(VADisplay dpy, VAContextID context); |
| +typedef VAStatus (*VaapiDestroyImage)(VADisplay dpy, VAImageID image); |
| typedef VAStatus (*VaapiDestroySurfaces)(VADisplay dpy, |
| VASurfaceID *surfaces, |
| int num_surfaces); |
| @@ -94,6 +98,9 @@ typedef VADisplay (*VaapiGetDisplay)(Display *dpy); |
| typedef VAStatus (*VaapiInitialize)(VADisplay dpy, |
| int *major_version, |
| int *minor_version); |
| +typedef VAStatus (*VaapiMapBuffer)(VADisplay dpy, |
| + VABufferID buf_id, |
| + void** pbuf); |
| typedef VAStatus (*VaapiPutSurface)(VADisplay dpy, |
| VASurfaceID surface, |
| Drawable draw, |
| @@ -114,6 +121,7 @@ typedef VAStatus (*VaapiRenderPicture)(VADisplay dpy, |
| int num_buffers); |
| typedef VAStatus (*VaapiSyncSurface)(VADisplay dpy, VASurfaceID render_target); |
| typedef VAStatus (*VaapiTerminate)(VADisplay dpy); |
| +typedef VAStatus (*VaapiUnmapBuffer)(VADisplay dpy, VABufferID buf_id); |
| #define VAAPI_SYM(name, handle) Vaapi##name VAAPI_##name = NULL |
| @@ -122,9 +130,11 @@ VAAPI_SYM(CreateBuffer, vaapi_handle); |
| VAAPI_SYM(CreateConfig, vaapi_handle); |
| VAAPI_SYM(CreateContext, vaapi_handle); |
| VAAPI_SYM(CreateSurfaces, vaapi_handle); |
| +VAAPI_SYM(DeriveImage, vaapi_handle); |
| VAAPI_SYM(DestroyBuffer, vaapi_handle); |
| VAAPI_SYM(DestroyConfig, vaapi_handle); |
| VAAPI_SYM(DestroyContext, vaapi_handle); |
| +VAAPI_SYM(DestroyImage, vaapi_handle); |
| VAAPI_SYM(DestroySurfaces, vaapi_handle); |
| VAAPI_SYM(DisplayIsValid, vaapi_handle); |
| VAAPI_SYM(EndPicture, vaapi_handle); |
| @@ -132,10 +142,12 @@ VAAPI_SYM(ErrorStr, vaapi_handle); |
| VAAPI_SYM(GetConfigAttributes, vaapi_handle); |
| VAAPI_SYM(GetDisplay, vaapi_x11_handle); |
| VAAPI_SYM(Initialize, vaapi_handle); |
| +VAAPI_SYM(MapBuffer, vaapi_handle); |
| VAAPI_SYM(PutSurface, vaapi_x11_handle); |
| VAAPI_SYM(RenderPicture, vaapi_handle); |
| VAAPI_SYM(SyncSurface, vaapi_x11_handle); |
| VAAPI_SYM(Terminate, vaapi_handle); |
| +VAAPI_SYM(UnmapBuffer, vaapi_handle); |
| #undef VAAPI_SYM |
| @@ -433,6 +445,88 @@ bool VaapiWrapper::PutSurfaceIntoPixmap(VASurfaceID va_surface_id, |
| return true; |
| } |
| +static scoped_refptr<media::VideoFrame> CopyNV12ToI420(VAImage* image, |
| + void* mem) { |
| + DVLOG(1) << "CopyNV12ToI420 width=" << image->width |
| + << ", height=" << image->height; |
| + |
| + const gfx::Size coded_size(image->width, image->height); |
| + const gfx::Rect visible_rect(image->width, image->height); |
| + const gfx::Size natural_size(image->width, image->height); |
| + |
| + scoped_refptr<media::VideoFrame> frame = |
| + media::VideoFrame::CreateFrame(media::VideoFrame::I420, |
| + coded_size, |
| + visible_rect, |
| + natural_size, |
| + base::TimeDelta()); |
| + |
| + uint8_t* mem_byte_ptr = static_cast<uint8_t*>(mem); |
| + uint8_t* srcY = mem_byte_ptr + image->offsets[0]; |
| + uint8_t* srcU = mem_byte_ptr + image->offsets[1]; |
| + uint8_t* srcV = srcU + 1; |
| + |
| + uint8_t* dstY = frame->data(media::VideoFrame::kYPlane); |
| + uint8_t* dstU = frame->data(media::VideoFrame::kUPlane); |
| + uint8_t* dstV = frame->data(media::VideoFrame::kVPlane); |
| + |
| + for (int i = 0; i < image->height; i++) { |
| + memcpy(dstY, srcY, image->width); |
| + srcY += image->pitches[0]; |
| + dstY += frame->stride(media::VideoFrame::kYPlane); |
| + |
| + // U/V components are subsampled, so there is only one U/V row for every two |
| + // Y rows. |
| + if ((i & 1) == 1) |
| + continue; |
| + for (int j = 0, k = 0; j < image->width; j += 2, k++) { |
| + dstU[k] = srcU[j]; |
| + dstV[k] = srcV[j]; |
| + } |
| + srcU += image->pitches[1]; |
| + srcV += image->pitches[1]; |
| + dstU += frame->stride(media::VideoFrame::kUPlane); |
| + dstV += frame->stride(media::VideoFrame::kVPlane); |
| + } |
| + |
| + return frame; |
| +} |
| + |
| +scoped_refptr<media::VideoFrame> VaapiWrapper::VideoFrameFromVASurface( |
| + VASurfaceID va_surface_id) { |
| + base::AutoLock auto_lock(va_lock_); |
| + |
| + VAStatus va_res = VAAPI_SyncSurface(va_display_, va_surface_id); |
| + VA_SUCCESS_OR_RETURN(va_res, "Failed syncing surface", NULL); |
| + |
| + VAImage image; |
| + void* mem = NULL; |
| + scoped_refptr<media::VideoFrame> frame; |
| + |
| + // Derive a VAImage from the VASurface |
| + va_res = VAAPI_DeriveImage(va_display_, va_surface_id, &image); |
| + VA_LOG_ON_ERROR(va_res, "vaDeriveImage failed"); |
| + if (va_res != VA_STATUS_SUCCESS) |
| + return frame; |
| + |
| + // Check if the format is NV12 |
| + if (image.format.fourcc == VA_FOURCC_NV12) { |
| + // Map the buffer |
| + va_res = VAAPI_MapBuffer(va_display_, image.buf, &mem); |
| + VA_LOG_ON_ERROR(va_res, "vaMapBuffer failed"); |
| + if (va_res == VA_STATUS_SUCCESS) { |
| + frame = CopyNV12ToI420(&image, mem); |
| + // Unmap the buffer |
| + VAAPI_UnmapBuffer(va_display_, image.buf); |
| + } |
| + } else { |
| + DVLOG(ERROR) << "unexpected image format: " << image.format.fourcc; |
|
Pawel Osciak
2013/12/03 08:52:06
The convention is to use numeric constants for *VL
chihchung
2013/12/03 10:57:40
Moved to test and changed to LOG.
|
| + } |
| + VAAPI_DestroyImage(va_display_, image.image_id); |
| + |
| + return frame; |
| +} |
| + |
| // static |
| bool VaapiWrapper::PostSandboxInitialization() { |
| vaapi_handle = dlopen("libva.so.1", RTLD_NOW); |
| @@ -454,9 +548,11 @@ bool VaapiWrapper::PostSandboxInitialization() { |
| VAAPI_DLSYM_OR_RETURN_ON_ERROR(CreateConfig, vaapi_handle); |
| VAAPI_DLSYM_OR_RETURN_ON_ERROR(CreateContext, vaapi_handle); |
| VAAPI_DLSYM_OR_RETURN_ON_ERROR(CreateSurfaces, vaapi_handle); |
| + VAAPI_DLSYM_OR_RETURN_ON_ERROR(DeriveImage, vaapi_handle); |
| VAAPI_DLSYM_OR_RETURN_ON_ERROR(DestroyBuffer, vaapi_handle); |
| VAAPI_DLSYM_OR_RETURN_ON_ERROR(DestroyConfig, vaapi_handle); |
| VAAPI_DLSYM_OR_RETURN_ON_ERROR(DestroyContext, vaapi_handle); |
| + VAAPI_DLSYM_OR_RETURN_ON_ERROR(DestroyImage, vaapi_handle); |
| VAAPI_DLSYM_OR_RETURN_ON_ERROR(DestroySurfaces, vaapi_handle); |
| VAAPI_DLSYM_OR_RETURN_ON_ERROR(DisplayIsValid, vaapi_handle); |
| VAAPI_DLSYM_OR_RETURN_ON_ERROR(EndPicture, vaapi_handle); |
| @@ -464,10 +560,12 @@ bool VaapiWrapper::PostSandboxInitialization() { |
| VAAPI_DLSYM_OR_RETURN_ON_ERROR(GetConfigAttributes, vaapi_handle); |
| VAAPI_DLSYM_OR_RETURN_ON_ERROR(GetDisplay, vaapi_x11_handle); |
| VAAPI_DLSYM_OR_RETURN_ON_ERROR(Initialize, vaapi_handle); |
| + VAAPI_DLSYM_OR_RETURN_ON_ERROR(MapBuffer, vaapi_handle); |
| VAAPI_DLSYM_OR_RETURN_ON_ERROR(PutSurface, vaapi_x11_handle); |
| VAAPI_DLSYM_OR_RETURN_ON_ERROR(RenderPicture, vaapi_handle); |
| VAAPI_DLSYM_OR_RETURN_ON_ERROR(SyncSurface, vaapi_handle); |
| VAAPI_DLSYM_OR_RETURN_ON_ERROR(Terminate, vaapi_handle); |
| + VAAPI_DLSYM_OR_RETURN_ON_ERROR(UnmapBuffer, vaapi_handle); |
| #undef VAAPI_DLSYM |
| return true; |