|
| 1 | +#include "FrameProcessor.h" |
| 2 | +#include "FrameExtractor.h" |
| 3 | +#include <rnexecutorch/Log.h> |
| 4 | +#include <stdexcept> |
| 5 | + |
| 6 | +namespace rnexecutorch { |
| 7 | +namespace utils { |
| 8 | + |
| 9 | +cv::Mat FrameProcessor::extractFrame(jsi::Runtime &runtime, |
| 10 | + const jsi::Object &frameData) { |
| 11 | + // Get frame dimensions |
| 12 | + int width = |
| 13 | + static_cast<int>(frameData.getProperty(runtime, "width").asNumber()); |
| 14 | + int height = |
| 15 | + static_cast<int>(frameData.getProperty(runtime, "height").asNumber()); |
| 16 | + |
| 17 | + // Try zero-copy path first (nativeBuffer) |
| 18 | + if (hasNativeBuffer(runtime, frameData)) { |
| 19 | + static bool loggedPath = false; |
| 20 | + if (!loggedPath) { |
| 21 | + log(LOG_LEVEL::Debug, "FrameProcessor: Using zero-copy nativeBuffer"); |
| 22 | + loggedPath = true; |
| 23 | + } |
| 24 | + |
| 25 | + try { |
| 26 | + return extractFromNativeBuffer(runtime, frameData, width, height); |
| 27 | + } catch (const std::exception &e) { |
| 28 | + log(LOG_LEVEL::Debug, |
| 29 | + "FrameProcessor: nativeBuffer extraction failed: ", e.what()); |
| 30 | + log(LOG_LEVEL::Debug, "FrameProcessor: Falling back to ArrayBuffer"); |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + // Fallback to ArrayBuffer path (with copy) |
| 35 | + if (frameData.hasProperty(runtime, "data")) { |
| 36 | + static bool loggedPath = false; |
| 37 | + if (!loggedPath) { |
| 38 | + log(LOG_LEVEL::Debug, "FrameProcessor: Using ArrayBuffer (with copy)"); |
| 39 | + loggedPath = true; |
| 40 | + } |
| 41 | + |
| 42 | + return extractFromArrayBuffer(runtime, frameData, width, height); |
| 43 | + } |
| 44 | + |
| 45 | + // No valid frame data source |
| 46 | + throw std::runtime_error( |
| 47 | + "FrameProcessor: No valid frame data (neither nativeBuffer nor data " |
| 48 | + "property found)"); |
| 49 | +} |
| 50 | + |
| 51 | +cv::Size FrameProcessor::getFrameSize(jsi::Runtime &runtime, |
| 52 | + const jsi::Object &frameData) { |
| 53 | + if (!frameData.hasProperty(runtime, "width") || |
| 54 | + !frameData.hasProperty(runtime, "height")) { |
| 55 | + throw std::runtime_error("FrameProcessor: Frame data missing width or " |
| 56 | + "height property"); |
| 57 | + } |
| 58 | + |
| 59 | + int width = |
| 60 | + static_cast<int>(frameData.getProperty(runtime, "width").asNumber()); |
| 61 | + int height = |
| 62 | + static_cast<int>(frameData.getProperty(runtime, "height").asNumber()); |
| 63 | + |
| 64 | + return cv::Size(width, height); |
| 65 | +} |
| 66 | + |
| 67 | +bool FrameProcessor::hasNativeBuffer(jsi::Runtime &runtime, |
| 68 | + const jsi::Object &frameData) { |
| 69 | + return frameData.hasProperty(runtime, "nativeBuffer"); |
| 70 | +} |
| 71 | + |
| 72 | +cv::Mat FrameProcessor::extractFromNativeBuffer(jsi::Runtime &runtime, |
| 73 | + const jsi::Object &frameData, |
| 74 | + int width, int height) { |
| 75 | + auto nativeBufferValue = frameData.getProperty(runtime, "nativeBuffer"); |
| 76 | + |
| 77 | + // Handle bigint pointer value from JavaScript |
| 78 | + uint64_t bufferPtr = static_cast<uint64_t>( |
| 79 | + nativeBufferValue.asBigInt(runtime).asUint64(runtime)); |
| 80 | + |
| 81 | + // Use FrameExtractor to get cv::Mat from platform-specific buffer |
| 82 | + cv::Mat frame = FrameExtractor::extractFromNativeBuffer(bufferPtr); |
| 83 | + |
| 84 | + // Validate extracted frame dimensions match expected |
| 85 | + if (frame.cols != width || frame.rows != height) { |
| 86 | + log(LOG_LEVEL::Debug, "FrameProcessor: Dimension mismatch - expected ", |
| 87 | + width, "x", height, " but got ", frame.cols, "x", frame.rows); |
| 88 | + } |
| 89 | + |
| 90 | + return frame; |
| 91 | +} |
| 92 | + |
| 93 | +cv::Mat FrameProcessor::extractFromArrayBuffer(jsi::Runtime &runtime, |
| 94 | + const jsi::Object &frameData, |
| 95 | + int width, int height) { |
| 96 | + auto pixelData = frameData.getProperty(runtime, "data"); |
| 97 | + auto arrayBuffer = pixelData.asObject(runtime).getArrayBuffer(runtime); |
| 98 | + uint8_t *data = arrayBuffer.data(runtime); |
| 99 | + size_t bufferSize = arrayBuffer.size(runtime); |
| 100 | + |
| 101 | + // Determine format based on buffer size |
| 102 | + size_t stride = bufferSize / height; |
| 103 | + size_t expectedRGBAStride = width * 4; |
| 104 | + size_t expectedRGBStride = width * 3; |
| 105 | + |
| 106 | + cv::Mat frame; |
| 107 | + |
| 108 | + if (stride == expectedRGBAStride || bufferSize >= width * height * 4) { |
| 109 | + // RGBA format with potential padding |
| 110 | + frame = cv::Mat(height, width, CV_8UC4, data, stride); |
| 111 | + |
| 112 | + static bool loggedFormat = false; |
| 113 | + if (!loggedFormat) { |
| 114 | + log(LOG_LEVEL::Debug, |
| 115 | + "FrameProcessor: ArrayBuffer format is RGBA, " |
| 116 | + "stride: ", |
| 117 | + stride); |
| 118 | + loggedFormat = true; |
| 119 | + } |
| 120 | + } else if (stride >= expectedRGBStride) { |
| 121 | + // RGB format |
| 122 | + frame = cv::Mat(height, width, CV_8UC3, data, stride); |
| 123 | + |
| 124 | + static bool loggedFormat = false; |
| 125 | + if (!loggedFormat) { |
| 126 | + log(LOG_LEVEL::Debug, |
| 127 | + "FrameProcessor: ArrayBuffer format is RGB, stride: ", stride); |
| 128 | + loggedFormat = true; |
| 129 | + } |
| 130 | + } else { |
| 131 | + throw std::runtime_error( |
| 132 | + "FrameProcessor: Unexpected buffer size - expected " + |
| 133 | + std::to_string(expectedRGBStride) + " or " + |
| 134 | + std::to_string(expectedRGBAStride) + " bytes per row, got " + |
| 135 | + std::to_string(stride)); |
| 136 | + } |
| 137 | + |
| 138 | + return frame; |
| 139 | +} |
| 140 | + |
| 141 | +} // namespace utils |
| 142 | +} // namespace rnexecutorch |
0 commit comments