Title: [290557] trunk/Source/WebCore
Revision
290557
Author
[email protected]
Date
2022-02-27 03:44:58 -0800 (Sun, 27 Feb 2022)

Log Message

Simplify CVPixelBuffer data copies in SharedVideoFrameInfo
https://bugs.webkit.org/show_bug.cgi?id=237194

Reviewed by Eric Carlson.

Remove use of vImageUnpremultiplyData_BGRA8888 which might not be correct for canvas data.
Make use of CVPixelBufferGetPlaneCount to share more code between monoplanar and biplanar formats.

Covered by existing tests.

* platform/cocoa/CoreVideoSoftLink.cpp:
* platform/cocoa/CoreVideoSoftLink.h:
* platform/cocoa/SharedVideoFrameInfo.mm:
(WebCore::SharedVideoFrameInfo::isReadWriteSupported const):
(WebCore::copyToCVPixelBufferPlane):
(WebCore::SharedVideoFrameInfo::createPixelBufferFromMemory):
(WebCore::SharedVideoFrameInfo::writePixelBuffer):

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (290556 => 290557)


--- trunk/Source/WebCore/ChangeLog	2022-02-27 11:21:26 UTC (rev 290556)
+++ trunk/Source/WebCore/ChangeLog	2022-02-27 11:44:58 UTC (rev 290557)
@@ -1,3 +1,23 @@
+2022-02-27  Youenn Fablet  <[email protected]>
+
+        Simplify CVPixelBuffer data copies in SharedVideoFrameInfo
+        https://bugs.webkit.org/show_bug.cgi?id=237194
+
+        Reviewed by Eric Carlson.
+
+        Remove use of vImageUnpremultiplyData_BGRA8888 which might not be correct for canvas data.
+        Make use of CVPixelBufferGetPlaneCount to share more code between monoplanar and biplanar formats.
+
+        Covered by existing tests.
+
+        * platform/cocoa/CoreVideoSoftLink.cpp:
+        * platform/cocoa/CoreVideoSoftLink.h:
+        * platform/cocoa/SharedVideoFrameInfo.mm:
+        (WebCore::SharedVideoFrameInfo::isReadWriteSupported const):
+        (WebCore::copyToCVPixelBufferPlane):
+        (WebCore::SharedVideoFrameInfo::createPixelBufferFromMemory):
+        (WebCore::SharedVideoFrameInfo::writePixelBuffer):
+
 2022-02-26  Tim Nguyen  <[email protected]>
 
         Remove Node::deprecatedIsInert

Modified: trunk/Source/WebCore/platform/cocoa/CoreVideoSoftLink.cpp (290556 => 290557)


--- trunk/Source/WebCore/platform/cocoa/CoreVideoSoftLink.cpp	2022-02-27 11:21:26 UTC (rev 290556)
+++ trunk/Source/WebCore/platform/cocoa/CoreVideoSoftLink.cpp	2022-02-27 11:44:58 UTC (rev 290557)
@@ -46,6 +46,7 @@
 SOFT_LINK_FUNCTION_FOR_SOURCE(WebCore, CoreVideo, CVPixelBufferGetBytesPerRow, size_t, (CVPixelBufferRef pixelBuffer), (pixelBuffer))
 SOFT_LINK_FUNCTION_FOR_SOURCE(WebCore, CoreVideo, CVPixelBufferGetBytesPerRowOfPlane, size_t, (CVPixelBufferRef pixelBuffer, size_t planeIndex), (pixelBuffer, planeIndex))
 SOFT_LINK_FUNCTION_FOR_SOURCE(WebCore, CoreVideo, CVPixelBufferGetPixelFormatType, OSType, (CVPixelBufferRef pixelBuffer), (pixelBuffer))
+SOFT_LINK_FUNCTION_FOR_SOURCE(WebCore, CoreVideo, CVPixelBufferGetPlaneCount, size_t, (CVPixelBufferRef pixelBuffer), (pixelBuffer))
 SOFT_LINK_FUNCTION_FOR_SOURCE(WebCore, CoreVideo, CVPixelBufferGetBaseAddressOfPlane, void *, (CVPixelBufferRef pixelBuffer, size_t planeIndex), (pixelBuffer, planeIndex));
 SOFT_LINK_FUNCTION_FOR_SOURCE(WebCore, CoreVideo, CVPixelBufferLockBaseAddress, CVReturn, (CVPixelBufferRef pixelBuffer, CVOptionFlags lockFlags), (pixelBuffer, lockFlags))
 SOFT_LINK_FUNCTION_FOR_SOURCE(WebCore, CoreVideo, CVPixelBufferUnlockBaseAddress, CVReturn, (CVPixelBufferRef pixelBuffer, CVOptionFlags lockFlags), (pixelBuffer, lockFlags))

Modified: trunk/Source/WebCore/platform/cocoa/CoreVideoSoftLink.h (290556 => 290557)


--- trunk/Source/WebCore/platform/cocoa/CoreVideoSoftLink.h	2022-02-27 11:21:26 UTC (rev 290556)
+++ trunk/Source/WebCore/platform/cocoa/CoreVideoSoftLink.h	2022-02-27 11:44:58 UTC (rev 290557)
@@ -58,6 +58,8 @@
 #define CVPixelBufferGetBytesPerRowOfPlane softLink_CoreVideo_CVPixelBufferGetBytesPerRowOfPlane
 SOFT_LINK_FUNCTION_FOR_HEADER(WebCore, CoreVideo, CVPixelBufferGetPixelFormatType, OSType, (CVPixelBufferRef pixelBuffer), (pixelBuffer))
 #define CVPixelBufferGetPixelFormatType softLink_CoreVideo_CVPixelBufferGetPixelFormatType
+SOFT_LINK_FUNCTION_FOR_HEADER(WebCore, CoreVideo, CVPixelBufferGetPlaneCount, size_t, (CVPixelBufferRef pixelBuffer), (pixelBuffer))
+#define CVPixelBufferGetPlaneCount softLink_CoreVideo_CVPixelBufferGetPlaneCount
 SOFT_LINK_FUNCTION_FOR_HEADER(WebCore, CoreVideo, CVPixelBufferGetBaseAddressOfPlane, void *, (CVPixelBufferRef pixelBuffer, size_t planeIndex), (pixelBuffer, planeIndex));
 #define CVPixelBufferGetBaseAddressOfPlane softLink_CoreVideo_CVPixelBufferGetBaseAddressOfPlane
 SOFT_LINK_FUNCTION_FOR_HEADER(WebCore, CoreVideo, CVPixelBufferLockBaseAddress, CVReturn, (CVPixelBufferRef pixelBuffer, CVOptionFlags lockFlags), (pixelBuffer, lockFlags))

Modified: trunk/Source/WebCore/platform/cocoa/SharedVideoFrameInfo.mm (290556 => 290557)


--- trunk/Source/WebCore/platform/cocoa/SharedVideoFrameInfo.mm	2022-02-27 11:21:26 UTC (rev 290556)
+++ trunk/Source/WebCore/platform/cocoa/SharedVideoFrameInfo.mm	2022-02-27 11:44:58 UTC (rev 290557)
@@ -34,10 +34,6 @@
 #include <wtf/Scope.h>
 #include <wtf/persistence/PersistentCoders.h>
 
-#if USE(ACCELERATE)
-#include <Accelerate/Accelerate.h>
-#endif
-
 #if USE(LIBWEBRTC)
 
 ALLOW_UNUSED_PARAMETERS_BEGIN
@@ -63,11 +59,9 @@
 bool SharedVideoFrameInfo::isReadWriteSupported() const
 {
     return m_bufferType == kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange
-#if USE(ACCELERATE)
-            || m_bufferType == kCVPixelFormatType_32BGRA
-#endif
-            || m_bufferType == kCVPixelFormatType_420YpCbCr8BiPlanarFullRange
-            || m_bufferType == kCVPixelFormatType_420YpCbCr10BiPlanarFullRange;
+        || m_bufferType == kCVPixelFormatType_32BGRA
+        || m_bufferType == kCVPixelFormatType_420YpCbCr8BiPlanarFullRange
+        || m_bufferType == kCVPixelFormatType_420YpCbCr10BiPlanarFullRange;
 }
 
 size_t SharedVideoFrameInfo::storageSize() const
@@ -131,39 +125,21 @@
     return SharedVideoFrameInfo { *bufferType, *width, *height, *bytesPerRow , *widthPlaneB, *heightPlaneB, *bytesPerRowPlaneB };
 }
 
+static const uint8_t* copyToCVPixelBufferPlane(CVPixelBufferRef pixelBuffer, size_t planeIndex, const uint8_t* source, size_t height, uint32_t bytesPerRowSource)
+{
+    auto* destination = static_cast<uint8_t*>(CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, planeIndex));
+    uint32_t bytesPerRowDestination = CVPixelBufferGetBytesPerRowOfPlane(pixelBuffer, planeIndex);
+    for (unsigned i = 0; i < height; ++i) {
+        std::memcpy(destination, source, std::min(bytesPerRowSource, bytesPerRowDestination));
+        source += bytesPerRowSource;
+        destination += bytesPerRowDestination;
+    }
+    return source;
+}
+
 RetainPtr<CVPixelBufferRef> SharedVideoFrameInfo::createPixelBufferFromMemory(const uint8_t* data, CVPixelBufferPoolRef bufferPool)
 {
     ASSERT(isReadWriteSupported());
-    if (m_bufferType == kCVPixelFormatType_32BGRA) {
-#if USE(ACCELERATE)
-        IntSize size { static_cast<int>(m_width), static_cast<int>(m_height) };
-        auto ioSurface = IOSurface::create(size, DestinationColorSpace::SRGB(), IOSurface::Format::BGRA);
-
-        IOSurface::Locker lock(*ioSurface);
-        vImage_Buffer src;
-        src.width = m_width;
-        src.height = m_height;
-        src.rowBytes = m_bytesPerRow;
-        src.data = ""
-
-        vImage_Buffer dest;
-        dest.width = m_width;
-        dest.height = m_height;
-        dest.rowBytes = ioSurface->bytesPerRow();
-        dest.data = ""
-
-        vImageUnpremultiplyData_BGRA8888(&src, &dest, kvImageNoFlags);
-
-        auto pixelBuffer = WebCore::createCVPixelBuffer(ioSurface->surface());
-        if (!pixelBuffer)
-            return nullptr;
-        return WTFMove(*pixelBuffer);
-#else
-        RELEASE_LOG_ERROR(Media, "createIOSurfaceFromSharedMemory cannot convert to IOSurface");
-        return nullptr;
-#endif
-    }
-
     CVPixelBufferRef rawPixelBuffer = nullptr;
     if (bufferPool) {
         auto status = CVPixelBufferPoolCreatePixelBuffer(kCFAllocatorDefault, bufferPool, &rawPixelBuffer);
@@ -188,26 +164,13 @@
         CVPixelBufferUnlockBaseAddress(rawPixelBuffer, 0);
     });
 
-    if (CVPixelBufferGetWidthOfPlane(rawPixelBuffer, 1) != m_widthPlaneB || CVPixelBufferGetHeightOfPlane(rawPixelBuffer, 1) != m_heightPlaneB)
-        return nullptr;
-
-    auto* planeA = static_cast<uint8_t*>(CVPixelBufferGetBaseAddressOfPlane(rawPixelBuffer, 0));
-    uint32_t bytesPerRowPlaneA = CVPixelBufferGetBytesPerRowOfPlane(rawPixelBuffer, 0);
-    for (unsigned i = 0; i < m_height; ++i) {
-        std::memcpy(planeA, data, std::min(bytesPerRowPlaneA, m_bytesPerRow));
-        planeA += bytesPerRowPlaneA;
-        data += m_bytesPerRow;
+    data = "" 0, data, m_height, m_bytesPerRow);
+    if (CVPixelBufferGetPlaneCount(rawPixelBuffer) == 2) {
+        if (CVPixelBufferGetWidthOfPlane(rawPixelBuffer, 1) != m_widthPlaneB || CVPixelBufferGetHeightOfPlane(rawPixelBuffer, 1) != m_heightPlaneB)
+            return nullptr;
+        copyToCVPixelBufferPlane(rawPixelBuffer, 1, data, m_heightPlaneB, m_bytesPerRowPlaneB);
     }
 
-    auto* planeB = static_cast<uint8_t*>(CVPixelBufferGetBaseAddressOfPlane(rawPixelBuffer, 1));
-    uint32_t bytesPerRowPlaneB = CVPixelBufferGetBytesPerRowOfPlane(rawPixelBuffer, 1);
-    for (unsigned i = 0; i < m_heightPlaneB; ++i) {
-        std::memcpy(planeB, data, std::min(bytesPerRowPlaneB, m_bytesPerRowPlaneB));
-        planeB += bytesPerRowPlaneB;
-        data += m_bytesPerRowPlaneB;
-    }
-
-    CVPixelBufferUnlockBaseAddress(rawPixelBuffer, 0);
     return pixelBuffer;
 }
 
@@ -224,36 +187,16 @@
     encode(data);
     data += sizeof(SharedVideoFrameInfo);
 
-    if (m_bufferType == kCVPixelFormatType_32BGRA) {
-#if USE(ACCELERATE)
-        vImage_Buffer src;
-        src.width = m_width;
-        src.height = m_height;
-        src.rowBytes = m_bytesPerRow;
-        src.data = ""
+    auto* planeA = static_cast<const uint8_t*>(CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 0));
+    size_t planeASize = m_height * m_bytesPerRow;
+    std::memcpy(data, planeA, planeASize);
 
-        vImage_Buffer dest;
-        dest.width = m_width;
-        dest.height = m_height;
-        dest.rowBytes = m_bytesPerRow;
-        dest.data = ""
-
-        vImageUnpremultiplyData_BGRA8888(&src, &dest, kvImageNoFlags);
-
-        return true;
-#else
-        return false;
-#endif
+    if (CVPixelBufferGetPlaneCount(pixelBuffer) == 2) {
+        auto* planeB = static_cast<const uint8_t*>(CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 1));
+        size_t planeBSize = m_heightPlaneB * m_bytesPerRowPlaneB;
+        std::memcpy(data + planeASize, planeB, planeBSize);
     }
 
-    const uint8_t *planeA = static_cast<const uint8_t *>(CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 0));
-    const uint8_t *planeB = static_cast<const uint8_t *>(CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 1));
-
-    size_t planeASize = m_height * m_bytesPerRow;
-    std::memcpy(data, planeA, planeASize);
-    size_t planeBSize = m_heightPlaneB * m_bytesPerRowPlaneB;
-    std::memcpy(data + planeASize, planeB, planeBSize);
-
     return true;
 }
 
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to