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 5d22e4bd7759be342b6ebe5b1675735be8a05604..fcca299ded939319322f025c0ddc57cc500fc860 100644 |
--- a/content/common/gpu/media/vaapi_wrapper.cc |
+++ b/content/common/gpu/media/vaapi_wrapper.cc |
@@ -102,6 +102,14 @@ typedef VAStatus (*VaapiRenderPicture)(VADisplay dpy, |
int num_buffers); |
typedef VAStatus (*VaapiSyncSurface)(VADisplay dpy, VASurfaceID render_target); |
typedef VAStatus (*VaapiTerminate)(VADisplay dpy); |
+typedef VAStatus (*VaapiMapBuffer)(VADisplay dpy, |
+ VABufferID buf_id, |
+ void **pbuf); |
+typedef VAStatus (*VaapiUnmapBuffer)(VADisplay dpy, VABufferID buf_id); |
+typedef VAStatus (*VaapiDeriveImage)(VADisplay dpy, |
+ VASurfaceID surface, |
+ VAImage *image); |
+typedef VAStatus (*VaapiDestroyImage)(VADisplay dpy, VAImageID image); |
#define VAAPI_SYM(name, handle) Vaapi##name VAAPI_##name = NULL |
@@ -124,6 +132,10 @@ VAAPI_SYM(PutSurface, vaapi_x11_handle); |
VAAPI_SYM(RenderPicture, vaapi_handle); |
VAAPI_SYM(SyncSurface, vaapi_x11_handle); |
VAAPI_SYM(Terminate, vaapi_handle); |
+VAAPI_SYM(MapBuffer, vaapi_handle); |
+VAAPI_SYM(UnmapBuffer, vaapi_handle); |
+VAAPI_SYM(DeriveImage, vaapi_handle); |
+VAAPI_SYM(DestroyImage, vaapi_handle); |
#undef VAAPI_SYM |
@@ -404,6 +416,80 @@ bool VaapiWrapper::PutSurfaceIntoPixmap(VASurfaceID va_surface_id, |
return true; |
} |
+static void CopyNV12ToI420(VAImage* image, |
+ void* mem, |
+ void** buffer, |
+ size_t* size) { |
+ DVLOG(1) << "CopyNV12ToI420 width=" << image->width |
+ << ", height=" << image->height; |
+ *size = image->width * image->height * 3 / 2; |
Pawel Osciak
2013/10/20 23:53:11
There are some useful functions in VideoFrame for
chihchung
2013/10/21 09:33:05
Done.
|
+ *buffer = malloc(*size); |
+ uint8_t* mem_byte_ptr = (uint8_t*)mem; |
+ uint8_t* buffer_byte_ptr = (uint8_t*)*buffer; |
+ |
+ 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 = buffer_byte_ptr; |
+ uint8_t* dstU = buffer_byte_ptr + image->width * image->height; |
+ uint8_t* dstV = dstU + image->width * image->height / 4; |
+ |
+ for (int i = 0; i < image->height; i++) { |
+ memcpy(dstY, srcY, image->width); |
+ dstY += image->width; |
+ srcY += image->pitches[0]; |
+ |
+ if ((i & 1) == 1) continue; |
+ for (int j = 0; j < image->width; j += 2) { |
+ *dstU++ = srcU[j]; |
+ *dstV++ = srcV[j]; |
+ } |
+ srcU += image->pitches[1]; |
+ srcV += image->pitches[1]; |
+ } |
+} |
+ |
+bool VaapiWrapper::GetI420FromSurface(VASurfaceID va_surface_id, |
+ void** buffer, |
+ size_t* size) { |
+ 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", false); |
+ |
+ VAImage image; |
+ void* mem = NULL; |
+ |
+ // 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) |
+ goto out; |
Pawel Osciak
2013/10/20 23:53:11
Don't use goto.
chihchung
2013/10/21 09:33:05
Done.
|
+ |
+ // Check if the format is NV12 |
+ if (image.format.fourcc != VA_FOURCC_NV12) { |
+ LOG(ERROR) << "unexpected image format: " << image.format.fourcc; |
+ goto destroy_image; |
+ } |
+ |
+ // 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) |
+ goto destroy_image; |
+ |
+ CopyNV12ToI420(&image, mem, buffer, size); |
+ |
+ // Unmap the buffer |
+ VAAPI_UnmapBuffer(va_display_, image.buf); |
+ |
+destroy_image: |
+ VAAPI_DestroyImage(va_display_, image.image_id); |
+ |
+out: |
+ return va_res == VA_STATUS_SUCCESS; |
+} |
+ |
// static |
bool VaapiWrapper::pre_sandbox_init_done_ = false; |
@@ -447,6 +533,10 @@ bool VaapiWrapper::PostSandboxInitialization() { |
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(MapBuffer, vaapi_handle); |
+ VAAPI_DLSYM_OR_RETURN_ON_ERROR(UnmapBuffer, vaapi_handle); |
+ VAAPI_DLSYM_OR_RETURN_ON_ERROR(DeriveImage, vaapi_handle); |
+ VAAPI_DLSYM_OR_RETURN_ON_ERROR(DestroyImage, vaapi_handle); |
#undef VAAPI_DLSYM |
return true; |