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..c7a5fa0a8a369e04c3e8afc99118d45aa2d53259 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,85 @@ bool VaapiWrapper::PutSurfaceIntoPixmap(VASurfaceID va_surface_id, |
return true; |
} |
+static scoped_refptr<media::VideoFrame> CopyNV12ToI420(VAImage* image, |
+ void* mem) { |
Pawel Osciak
2013/11/21 06:31:41
What do you think of using VideoFrame::WrapExterna
chihchung
2013/11/21 12:17:32
I don't understand this, but you said it's not a b
|
+ 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); |
+ |
+ 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::GetI420FromSurface( |
+ 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> result; |
+ |
+ // 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) { |
+ // 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) { |
+ result = CopyNV12ToI420(&image, mem); |
+ // Unmap the buffer |
+ VAAPI_UnmapBuffer(va_display_, image.buf); |
+ } |
+ } else { |
+ LOG(ERROR) << "unexpected image format: " << image.format.fourcc; |
+ } |
+ VAAPI_DestroyImage(va_display_, image.image_id); |
+ } |
+ |
+ return result; |
+} |
+ |
// static |
bool VaapiWrapper::pre_sandbox_init_done_ = false; |
@@ -447,6 +538,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; |