From b13b9371865f5edaee1560a0e032e6dc5942eecf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Micha=C3=ABl=20Celerier?= Date: Thu, 26 Nov 2026 08:46:21 -0500 Subject: [PATCH] stream: recycle decoded MJPEG frames, lay out padded and YV12 planes Decoded frames were allocated with av_frame_alloc for every compressed colour frame. The consumer hands frames back to the queue's released list, which only newFrame() pops, and the decode path never called it, so every MJPEG frame (the default colour format on Orbbec and Azure Kinect) stayed allocated until the stream closed. Decoding now takes its frame from the queue. A backend row stride was applied to plane 0 only, after the planes had been laid out tightly, so a planar or semi-planar frame with padded rows had its chroma planes read from the wrong offsets. The planes are now laid out from the stride, chroma rows scaling with the luma row as in I420 and NV12, and a stride the buffer cannot hold drops the frame. Orbbec YV12 was reported as YUV420P, with U and V swapped. It now has its own format, DEPTHCAM_FMT_YV12, appended to the ABI enum, which the host lays out with the chroma planes exchanged. A host that predates it drops these frames. The synthetic backend gains inject(), so tests can hand the host frame layouts the scene does not produce. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01KdPguixxwrpgc8UWhoHDeQ --- Backends/Orbbec/OrbbecBackend.cpp | 5 +- Backends/Synthetic/SyntheticBackend.cpp | 8 ++ Backends/Synthetic/SyntheticBackend.hpp | 4 + DepthCamera/Backend/depthcam_abi.h | 3 + DepthCamera/DepthCameraDevice.cpp | 1 + DepthCamera/DepthCameraStream.cpp | 68 ++++++++--- DepthCamera/DepthCameraStream.hpp | 2 +- Tests/unit/DepthCameraUnit.cpp | 147 ++++++++++++++++++++++++ 8 files changed, 217 insertions(+), 21 deletions(-) diff --git a/Backends/Orbbec/OrbbecBackend.cpp b/Backends/Orbbec/OrbbecBackend.cpp index 7b5cc31..ced1f2f 100644 --- a/Backends/Orbbec/OrbbecBackend.cpp +++ b/Backends/Orbbec/OrbbecBackend.cpp @@ -69,10 +69,9 @@ depthcam_format to_depthcam_format(OBFormat fmt) case OB_FORMAT_NV12: return DEPTHCAM_FMT_NV12; case OB_FORMAT_I420: - // YV12 is planar YUV 4:2:0 with the chroma planes swapped, not a grey - // format; the plane order is a layout detail the host handles. - case OB_FORMAT_YV12: return DEPTHCAM_FMT_YUV420P; + case OB_FORMAT_YV12: + return DEPTHCAM_FMT_YV12; case OB_FORMAT_RGB: return DEPTHCAM_FMT_RGB24; diff --git a/Backends/Synthetic/SyntheticBackend.cpp b/Backends/Synthetic/SyntheticBackend.cpp index 5ab9f40..fe1ba61 100644 --- a/Backends/Synthetic/SyntheticBackend.cpp +++ b/Backends/Synthetic/SyntheticBackend.cpp @@ -958,4 +958,12 @@ int64_t nextFrame(depthcam_device* d) return d ? d->frame.load() : 0; } +void inject(depthcam_device* d, const depthcam_frame& f) +{ + if(d && d->cb) + d->cb(&f, d->user); + else if(f.release) + f.release(f.owner); +} + } diff --git a/Backends/Synthetic/SyntheticBackend.hpp b/Backends/Synthetic/SyntheticBackend.hpp index 7faf7a5..6f45435 100644 --- a/Backends/Synthetic/SyntheticBackend.hpp +++ b/Backends/Synthetic/SyntheticBackend.hpp @@ -68,4 +68,8 @@ void step(depthcam_device* dev, int n = 1); /// The frame number of the next frame. int64_t nextFrame(depthcam_device* dev); +/// Hands @p f to the host's frame callback from the calling thread, for +/// layouts the scene does not produce. The host releases it as usual. +void inject(depthcam_device* dev, const depthcam_frame& f); + } diff --git a/DepthCamera/Backend/depthcam_abi.h b/DepthCamera/Backend/depthcam_abi.h index 78e5857..4d88648 100644 --- a/DepthCamera/Backend/depthcam_abi.h +++ b/DepthCamera/Backend/depthcam_abi.h @@ -121,6 +121,9 @@ enum depthcam_format /* One depthcam_imu_sample. */ DEPTHCAM_FMT_IMU, + + /* Planar 4:2:0 like DEPTHCAM_FMT_YUV420P, V plane before U. */ + DEPTHCAM_FMT_YV12, }; /** Which fields of a depthcam_imu_sample carry a reading. */ diff --git a/DepthCamera/DepthCameraDevice.cpp b/DepthCamera/DepthCameraDevice.cpp index 8d68716..9646fac 100644 --- a/DepthCamera/DepthCameraDevice.cpp +++ b/DepthCamera/DepthCameraDevice.cpp @@ -559,6 +559,7 @@ const char* formatName(int fmt) return "YUV 4:2:2"; case DEPTHCAM_FMT_NV12: case DEPTHCAM_FMT_YUV420P: + case DEPTHCAM_FMT_YV12: return "YUV 4:2:0"; case DEPTHCAM_FMT_MJPEG: return "MJPEG"; diff --git a/DepthCamera/DepthCameraStream.cpp b/DepthCamera/DepthCameraStream.cpp index 1415074..db10fa9 100644 --- a/DepthCamera/DepthCameraStream.cpp +++ b/DepthCamera/DepthCameraStream.cpp @@ -4,7 +4,10 @@ #include -#include