Skip to content

Commit

Permalink
ImageManipV2 stride alignment (#1238)
Browse files Browse the repository at this point in the history
* Align stride to 1 instead of 8 in ImageManipV2

* RVC4 FW: Align stride to 1 instead of 8 in ImageManipV2

* RVC4 FW: Fix incorrect width and height in transformations

* Bump rvc4 fw
  • Loading branch information
asahtik authored Feb 14, 2025
1 parent 21cb51d commit 7c304dc
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 52 deletions.
2 changes: 1 addition & 1 deletion cmake/Depthai/DepthaiDeviceRVC4Config.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ set(DEPTHAI_DEVICE_RVC4_MATURITY "snapshot")

# "version if applicable"
# set(DEPTHAI_DEVICE_RVC4_VERSION "0.0.1+93f7b75a885aa32f44c5e9f53b74470c49d2b1af")
set(DEPTHAI_DEVICE_RVC4_VERSION "0.0.1+ee40f16b48648261a85c3e568cb1a005ee57b2d7")
set(DEPTHAI_DEVICE_RVC4_VERSION "0.0.1+5a2bbf4074c70b11a0b12cee0d849dd4ebf982d5")
65 changes: 33 additions & 32 deletions include/depthai/utility/ImageManipV2Impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
#include <opencv2/core/types.hpp>
#endif

#define PLANE_ALIGNMENT 128
#define DEPTHAI_PLANE_ALIGNMENT 128
#define DEPTHAI_STRIDE_ALIGNMENT 1

#if defined(WIN32) || defined(_WIN32)
#define _RESTRICT
Expand Down Expand Up @@ -310,7 +311,7 @@ bool ColorChange<ImageManipBuffer, ImageManipData>::colorConvertToRGB888p(

auto src = inputFrame.data();
auto inputSize = inputFrame.size();
uint32_t auxStride = ALIGN_UP(3 * srcSpecs.width, 8);
uint32_t auxStride = ALIGN_UP(3 * srcSpecs.width, DEPTHAI_STRIDE_ALIGNMENT);

bool done = false;
switch(from) {
Expand Down Expand Up @@ -625,7 +626,7 @@ bool ColorChange<ImageManipBuffer, ImageManipData>::colorConvertToBGR888p(

auto src = inputFrame.data();
auto inputSize = inputFrame.size();
uint32_t auxStride = ALIGN_UP(3 * srcSpecs.width, 8);
uint32_t auxStride = ALIGN_UP(3 * srcSpecs.width, DEPTHAI_STRIDE_ALIGNMENT);

bool done = false;
switch(from) {
Expand Down Expand Up @@ -940,7 +941,7 @@ bool ColorChange<ImageManipBuffer, ImageManipData>::colorConvertToRGB888i(

auto src = inputFrame.data();
auto inputSize = inputFrame.size();
uint32_t auxStride = ALIGN_UP(3 * srcSpecs.width, 8);
uint32_t auxStride = ALIGN_UP(3 * srcSpecs.width, DEPTHAI_STRIDE_ALIGNMENT);

bool done = false;
switch(from) {
Expand Down Expand Up @@ -1144,7 +1145,7 @@ bool ColorChange<ImageManipBuffer, ImageManipData>::colorConvertToBGR888i(
auto src = inputFrame.data();
auto inputSize = inputFrame.size();
#if defined(DEPTHAI_HAVE_FASTCV_SUPPORT)
uint32_t auxStride = ALIGN_UP(3 * srcSpecs.width, 8);
uint32_t auxStride = ALIGN_UP(3 * srcSpecs.width, DEPTHAI_STRIDE_ALIGNMENT);
#endif

bool done = false;
Expand Down Expand Up @@ -1349,7 +1350,7 @@ bool ColorChange<ImageManipBuffer, ImageManipData>::colorConvertToNV12(
auto src = inputFrame.data();
auto inputSize = inputFrame.size();
#if defined(DEPTHAI_HAVE_FASTCV_SUPPORT)
uint32_t auxStride = ALIGN_UP(3 * srcSpecs.width, 8);
uint32_t auxStride = ALIGN_UP(3 * srcSpecs.width, DEPTHAI_STRIDE_ALIGNMENT);
#endif

bool done = false;
Expand Down Expand Up @@ -1600,7 +1601,7 @@ bool ColorChange<ImageManipBuffer, ImageManipData>::colorConvertToYUV420p(
auto src = inputFrame.data();
auto inputSize = inputFrame.size();
#if defined(DEPTHAI_HAVE_FASTCV_SUPPORT)
uint32_t auxStride = ALIGN_UP(3 * srcSpecs.width, 8);
uint32_t auxStride = ALIGN_UP(3 * srcSpecs.width, DEPTHAI_STRIDE_ALIGNMENT);
#endif

bool done = false;
Expand Down Expand Up @@ -1870,7 +1871,7 @@ bool ColorChange<ImageManipBuffer, ImageManipData>::colorConvertToGRAY8(

auto src = inputFrame.data();
auto inputSize = inputFrame.size();
uint32_t auxStride = ALIGN_UP(3 * srcSpecs.width, 8);
uint32_t auxStride = ALIGN_UP(3 * srcSpecs.width, DEPTHAI_STRIDE_ALIGNMENT);

bool done = false;
switch(from) {
Expand Down Expand Up @@ -2070,7 +2071,7 @@ void ColorChange<ImageManipBuffer, ImageManipData>::build(const FrameSpecs srcFr
to = typeTo;
srcSpecs = srcFrameSpecs;
dstSpecs = dstFrameSpecs;
size_t newAuxFrameSize = srcSpecs.height * ALIGN_UP(3 * srcSpecs.width, 8);
size_t newAuxFrameSize = srcSpecs.height * ALIGN_UP(3 * srcSpecs.width, DEPTHAI_STRIDE_ALIGNMENT);
if(!ccAuxFrame || ccAuxFrame->size() < newAuxFrameSize) ccAuxFrame = std::make_shared<ImageManipData>(newAuxFrameSize);
}

Expand Down Expand Up @@ -2492,7 +2493,7 @@ bool ImageManipOperations<ImageManipBuffer, ImageManipData>::apply(const std::sh
base.outputHeight,
CV_8UC1,
mode & MODE_WARP ? colormapFrame->data() : (convertInput ? convertedFrame->data() : src->getData().data()),
ALIGN_UP(base.outputWidth, 8));
ALIGN_UP(base.outputWidth, DEPTHAI_STRIDE_ALIGNMENT));
cv::Mat color(base.outputWidth, base.outputHeight, CV_8UC3, colormapDst);
cv::ColormapTypes cvColormap = cv::COLORMAP_JET;
switch(base.colormap) { // TODO(asahtik): set correct stereo colormaps
Expand Down Expand Up @@ -2536,19 +2537,19 @@ size_t ImageManipOperations<ImageManipBuffer, ImageManipData>::getOutputStride(u
switch(outputFrameType) {
case ImgFrame::Type::RGB888p:
case ImgFrame::Type::BGR888p:
return plane < 3 ? ALIGN_UP(base.outputWidth, 8) : 0;
return plane < 3 ? ALIGN_UP(base.outputWidth, DEPTHAI_STRIDE_ALIGNMENT) : 0;
case ImgFrame::Type::RGB888i:
case ImgFrame::Type::BGR888i:
return plane == 0 ? ALIGN_UP(base.outputWidth * 3, 8) : 0;
return plane == 0 ? ALIGN_UP(base.outputWidth * 3, DEPTHAI_STRIDE_ALIGNMENT) : 0;
case ImgFrame::Type::NV12:
return plane < 2 ? ALIGN_UP(base.outputWidth, 8) : 0;
return plane < 2 ? ALIGN_UP(base.outputWidth, DEPTHAI_STRIDE_ALIGNMENT) : 0;
case ImgFrame::Type::YUV420p:
return plane == 0 ? ALIGN_UP(base.outputWidth, 8) : (plane < 3 ? base.outputWidth / 2 : 0);
return plane == 0 ? ALIGN_UP(base.outputWidth, DEPTHAI_STRIDE_ALIGNMENT) : (plane < 3 ? base.outputWidth / 2 : 0);
case ImgFrame::Type::GRAY8:
case ImgFrame::Type::RAW8:
return plane == 0 ? ALIGN_UP(base.outputWidth, 8) : 0;
return plane == 0 ? ALIGN_UP(base.outputWidth, DEPTHAI_STRIDE_ALIGNMENT) : 0;
case ImgFrame::Type::RAW16:
return plane == 0 ? ALIGN_UP(base.outputWidth * 2, 8) : 0;
return plane == 0 ? ALIGN_UP(base.outputWidth * 2, DEPTHAI_STRIDE_ALIGNMENT) : 0;
case ImgFrame::Type::YUV422i:
case ImgFrame::Type::YUV444p:
case ImgFrame::Type::YUV422p:
Expand Down Expand Up @@ -2586,7 +2587,7 @@ size_t ImageManipOperations<ImageManipBuffer, ImageManipData>::getOutputSize() c
switch(outputFrameType) {
case ImgFrame::Type::RGB888p:
case ImgFrame::Type::BGR888p:
size = ALIGN_UP(getOutputStride() * getOutputHeight(), PLANE_ALIGNMENT) * 3;
size = ALIGN_UP(getOutputStride() * getOutputHeight(), DEPTHAI_PLANE_ALIGNMENT) * 3;
break;
case ImgFrame::Type::RGB888i:
case ImgFrame::Type::BGR888i:
Expand All @@ -2595,11 +2596,11 @@ size_t ImageManipOperations<ImageManipBuffer, ImageManipData>::getOutputSize() c
size = getOutputStride() * getOutputHeight();
break;
case ImgFrame::Type::NV12:
size = ALIGN_UP(getOutputStride(0) * getOutputHeight(), PLANE_ALIGNMENT) + ALIGN_UP(getOutputStride(1) * getOutputHeight() / 2, PLANE_ALIGNMENT);
size = ALIGN_UP(getOutputStride(0) * getOutputHeight(), DEPTHAI_PLANE_ALIGNMENT) + ALIGN_UP(getOutputStride(1) * getOutputHeight() / 2, DEPTHAI_PLANE_ALIGNMENT);
break;
case ImgFrame::Type::YUV420p:
size =
ALIGN_UP(getOutputStride(0) * getOutputHeight(), PLANE_ALIGNMENT) + ALIGN_UP(getOutputStride(1) * getOutputHeight() / 2, PLANE_ALIGNMENT) * 2;
ALIGN_UP(getOutputStride(0) * getOutputHeight(), DEPTHAI_PLANE_ALIGNMENT) + ALIGN_UP(getOutputStride(1) * getOutputHeight() / 2, DEPTHAI_PLANE_ALIGNMENT) * 2;
break;
case ImgFrame::Type::RAW16:
size = getOutputStride() * getOutputHeight();
Expand Down Expand Up @@ -2645,40 +2646,40 @@ FrameSpecs ImageManipOperations<ImageManipBuffer, ImageManipData>::getOutputFram
switch(type) {
case dai::ImgFrame::Type::RGB888p:
case dai::ImgFrame::Type::BGR888p:
specs.p1Stride = ALIGN_UP(specs.width, 8);
specs.p1Stride = ALIGN_UP(specs.width, DEPTHAI_STRIDE_ALIGNMENT);
specs.p2Stride = specs.p1Stride;
specs.p3Stride = specs.p1Stride;
specs.p2Offset = specs.p1Offset + ALIGN_UP(specs.p1Stride * specs.height, PLANE_ALIGNMENT);
specs.p3Offset = specs.p2Offset + ALIGN_UP(specs.p1Stride * specs.height, PLANE_ALIGNMENT);
specs.p2Offset = specs.p1Offset + ALIGN_UP(specs.p1Stride * specs.height, DEPTHAI_PLANE_ALIGNMENT);
specs.p3Offset = specs.p2Offset + ALIGN_UP(specs.p1Stride * specs.height, DEPTHAI_PLANE_ALIGNMENT);
break;
case dai::ImgFrame::Type::RGB888i:
case dai::ImgFrame::Type::BGR888i:
specs.p1Stride = ALIGN_UP(specs.width * 3, 8);
specs.p1Stride = ALIGN_UP(specs.width * 3, DEPTHAI_STRIDE_ALIGNMENT);
specs.p2Stride = specs.p1Stride;
specs.p3Stride = specs.p1Stride;
specs.p2Offset = specs.p1Offset;
specs.p3Offset = specs.p1Offset;
break;
case dai::ImgFrame::Type::NV12:
specs.p1Stride = ALIGN_UP(specs.width, 8);
specs.p1Stride = ALIGN_UP(specs.width, DEPTHAI_STRIDE_ALIGNMENT);
specs.p2Stride = specs.p1Stride;
specs.p2Offset = specs.p1Offset + ALIGN_UP(specs.p1Stride * specs.height, PLANE_ALIGNMENT);
specs.p2Offset = specs.p1Offset + ALIGN_UP(specs.p1Stride * specs.height, DEPTHAI_PLANE_ALIGNMENT);
specs.p3Offset = specs.p2Offset;
specs.p3Stride = 0;
break;
case dai::ImgFrame::Type::YUV420p:
specs.p1Stride = ALIGN_UP(specs.width, 8);
specs.p2Stride = ALIGN_UP(specs.width / 2, 8);
specs.p3Stride = ALIGN_UP(specs.width / 2, 8);
specs.p2Offset = specs.p1Offset + ALIGN_UP(specs.p1Stride * specs.height, PLANE_ALIGNMENT);
specs.p3Offset = specs.p2Offset + ALIGN_UP(specs.p2Stride * (specs.height / 2), PLANE_ALIGNMENT);
specs.p1Stride = ALIGN_UP(specs.width, DEPTHAI_STRIDE_ALIGNMENT);
specs.p2Stride = ALIGN_UP(specs.width / 2, DEPTHAI_STRIDE_ALIGNMENT);
specs.p3Stride = ALIGN_UP(specs.width / 2, DEPTHAI_STRIDE_ALIGNMENT);
specs.p2Offset = specs.p1Offset + ALIGN_UP(specs.p1Stride * specs.height, DEPTHAI_PLANE_ALIGNMENT);
specs.p3Offset = specs.p2Offset + ALIGN_UP(specs.p2Stride * (specs.height / 2), DEPTHAI_PLANE_ALIGNMENT);
break;
case dai::ImgFrame::Type::RAW8:
case dai::ImgFrame::Type::GRAY8:
specs.p1Stride = ALIGN_UP(specs.width, 8);
specs.p1Stride = ALIGN_UP(specs.width, DEPTHAI_STRIDE_ALIGNMENT);
break;
case ImgFrame::Type::RAW16:
specs.p1Stride = ALIGN_UP(specs.width * 2, 8);
specs.p1Stride = ALIGN_UP(specs.width * 2, DEPTHAI_STRIDE_ALIGNMENT);
break;
case ImgFrame::Type::YUV422i:
case ImgFrame::Type::YUV444p:
Expand Down
38 changes: 19 additions & 19 deletions src/utility/ImageManipV2Impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -436,40 +436,40 @@ dai::impl::FrameSpecs dai::impl::getCcDstFrameSpecs(FrameSpecs srcSpecs, dai::Im
switch(to) {
case dai::ImgFrame::Type::RGB888p:
case dai::ImgFrame::Type::BGR888p:
specs.p1Stride = ALIGN_UP(specs.width, 8);
specs.p1Stride = ALIGN_UP(specs.width, DEPTHAI_STRIDE_ALIGNMENT);
specs.p2Stride = specs.p1Stride;
specs.p3Stride = specs.p1Stride;
specs.p2Offset = specs.p1Offset + ALIGN_UP(specs.p1Stride * specs.height, PLANE_ALIGNMENT);
specs.p3Offset = specs.p2Offset + ALIGN_UP(specs.p1Stride * specs.height, PLANE_ALIGNMENT);
specs.p2Offset = specs.p1Offset + ALIGN_UP(specs.p1Stride * specs.height, DEPTHAI_PLANE_ALIGNMENT);
specs.p3Offset = specs.p2Offset + ALIGN_UP(specs.p1Stride * specs.height, DEPTHAI_PLANE_ALIGNMENT);
break;
case dai::ImgFrame::Type::RGB888i:
case dai::ImgFrame::Type::BGR888i:
specs.p1Stride = ALIGN_UP(specs.width * 3, 8);
specs.p1Stride = ALIGN_UP(specs.width * 3, DEPTHAI_STRIDE_ALIGNMENT);
specs.p2Stride = specs.p1Stride;
specs.p3Stride = specs.p1Stride;
specs.p2Offset = specs.p1Offset;
specs.p3Offset = specs.p1Offset;
break;
case dai::ImgFrame::Type::NV12:
specs.p1Stride = ALIGN_UP(specs.width, 8);
specs.p1Stride = ALIGN_UP(specs.width, DEPTHAI_STRIDE_ALIGNMENT);
specs.p2Stride = specs.p1Stride;
specs.p2Offset = specs.p1Offset + ALIGN_UP(specs.p1Stride * specs.height, PLANE_ALIGNMENT);
specs.p2Offset = specs.p1Offset + ALIGN_UP(specs.p1Stride * specs.height, DEPTHAI_PLANE_ALIGNMENT);
specs.p3Offset = specs.p2Offset;
specs.p3Stride = 0;
break;
case dai::ImgFrame::Type::YUV420p:
specs.p1Stride = ALIGN_UP(specs.width, 8);
specs.p2Stride = ALIGN_UP(specs.width / 2, 8);
specs.p3Stride = ALIGN_UP(specs.width / 2, 8);
specs.p2Offset = specs.p1Offset + ALIGN_UP(specs.p1Stride * specs.height, PLANE_ALIGNMENT);
specs.p3Offset = specs.p2Offset + ALIGN_UP(specs.p2Stride * (specs.height / 2), PLANE_ALIGNMENT);
specs.p1Stride = ALIGN_UP(specs.width, DEPTHAI_STRIDE_ALIGNMENT);
specs.p2Stride = ALIGN_UP(specs.width / 2, DEPTHAI_STRIDE_ALIGNMENT);
specs.p3Stride = ALIGN_UP(specs.width / 2, DEPTHAI_STRIDE_ALIGNMENT);
specs.p2Offset = specs.p1Offset + ALIGN_UP(specs.p1Stride * specs.height, DEPTHAI_PLANE_ALIGNMENT);
specs.p3Offset = specs.p2Offset + ALIGN_UP(specs.p2Stride * (specs.height / 2), DEPTHAI_PLANE_ALIGNMENT);
break;
case dai::ImgFrame::Type::RAW8:
case dai::ImgFrame::Type::GRAY8:
specs.p1Stride = ALIGN_UP(specs.width, 8);
specs.p1Stride = ALIGN_UP(specs.width, DEPTHAI_STRIDE_ALIGNMENT);
break;
case ImgFrame::Type::RAW16:
specs.p1Stride = ALIGN_UP(specs.width * 2, 8);
specs.p1Stride = ALIGN_UP(specs.width * 2, DEPTHAI_STRIDE_ALIGNMENT);
break;
case ImgFrame::Type::YUV422i:
case ImgFrame::Type::YUV444p:
Expand Down Expand Up @@ -996,19 +996,19 @@ void dai::impl::printSpecs(spdlog::async_logger& logger, FrameSpecs specs) {
size_t dai::impl::getAlignedOutputFrameSize(ImgFrame::Type type, size_t width, size_t height) {
switch(type) {
case ImgFrame::Type::YUV420p:
return ALIGN_UP(ALIGN_UP(width, 8) * height, PLANE_ALIGNMENT) + ALIGN_UP(ALIGN_UP(width / 2, 8) * (height / 2), PLANE_ALIGNMENT)
+ ALIGN_UP(width / 2, 8) * (height / 2);
return ALIGN_UP(ALIGN_UP(width, DEPTHAI_STRIDE_ALIGNMENT) * height, DEPTHAI_PLANE_ALIGNMENT) + ALIGN_UP(ALIGN_UP(width / 2, DEPTHAI_STRIDE_ALIGNMENT) * (height / 2), DEPTHAI_PLANE_ALIGNMENT)
+ ALIGN_UP(width / 2, DEPTHAI_STRIDE_ALIGNMENT) * (height / 2);
case ImgFrame::Type::RGB888p:
case ImgFrame::Type::BGR888p:
return 2 * ALIGN_UP(ALIGN_UP(width, 8) * height, PLANE_ALIGNMENT) + ALIGN_UP(width, 8) * height;
return 2 * ALIGN_UP(ALIGN_UP(width, DEPTHAI_STRIDE_ALIGNMENT) * height, DEPTHAI_PLANE_ALIGNMENT) + ALIGN_UP(width, DEPTHAI_STRIDE_ALIGNMENT) * height;
case ImgFrame::Type::RGB888i:
case ImgFrame::Type::BGR888i:
return ALIGN_UP(3 * width, 8) * height;
return ALIGN_UP(3 * width, DEPTHAI_STRIDE_ALIGNMENT) * height;
case ImgFrame::Type::NV12:
return ALIGN_UP(ALIGN_UP(width, 8) * height, PLANE_ALIGNMENT) + ALIGN_UP(width, 8) * (height / 2);
return ALIGN_UP(ALIGN_UP(width, DEPTHAI_STRIDE_ALIGNMENT) * height, DEPTHAI_PLANE_ALIGNMENT) + ALIGN_UP(width, DEPTHAI_STRIDE_ALIGNMENT) * (height / 2);
case ImgFrame::Type::RAW8:
case ImgFrame::Type::GRAY8:
return ALIGN_UP(width, 8) * height;
return ALIGN_UP(width, DEPTHAI_STRIDE_ALIGNMENT) * height;
case ImgFrame::Type::YUV422i:
case ImgFrame::Type::YUV444p:
case ImgFrame::Type::YUV422p:
Expand Down

0 comments on commit 7c304dc

Please sign in to comment.