Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions Backends/Orbbec/OrbbecBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions Backends/Synthetic/SyntheticBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
4 changes: 4 additions & 0 deletions Backends/Synthetic/SyntheticBackend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}
3 changes: 3 additions & 0 deletions DepthCamera/Backend/depthcam_abi.h
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
1 change: 1 addition & 0 deletions DepthCamera/DepthCameraDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
68 changes: 51 additions & 17 deletions DepthCamera/DepthCameraStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

#include <DepthCamera/DepthCameraSettings.hpp>

#include <Video/GStreamerCompatibility.hpp>

extern "C" {
#include <libavutil/imgutils.h>
}

#include <QDebug>

Expand Down Expand Up @@ -49,6 +52,7 @@ AVPixelFormat to_av_format(int fmt)
case DEPTHCAM_FMT_NV12:
return AV_PIX_FMT_NV12;
case DEPTHCAM_FMT_YUV420P:
case DEPTHCAM_FMT_YV12:
return AV_PIX_FMT_YUV420P;
default:
// Compressed and point-cloud formats are handled separately.
Expand Down Expand Up @@ -102,6 +106,42 @@ bool wrap_borrowed(AVFrame& frame, const depthcam_frame& f)
frame.data[0] = frame.buf[0]->data;
return true;
}

/// Lays the planes of a borrowed buffer out from the backend's row stride.
/// Chroma rows are assumed to scale with the luma row, as in I420 and NV12.
bool layout_planes(AVFrame& frame, const depthcam_frame& f)
{
const auto fmt = AVPixelFormat(frame.format);
int lines[4]{};
if(av_image_fill_linesizes(lines, fmt, f.width) < 0 || lines[0] <= 0)
return false;
if(f.stride > 0 && f.stride != lines[0])
{
if(f.stride < lines[0])
return false;
for(int i = 1; i < 4; i++)
lines[i] = int(int64_t(lines[i]) * f.stride / lines[0]);
lines[0] = f.stride;
}

ptrdiff_t lines64[4];
for(int i = 0; i < 4; i++)
lines64[i] = lines[i];
size_t sizes[4]{};
if(av_image_fill_plane_sizes(sizes, fmt, f.height, lines64) < 0)
return false;
if(sizes[0] + sizes[1] + sizes[2] + sizes[3] > f.bytes)
return false;

if(av_image_fill_pointers(frame.data, fmt, f.height, frame.data[0], lines) < 0)
return false;
for(int i = 0; i < 4; i++)
frame.linesize[i] = lines[i];

if(f.format == DEPTHCAM_FMT_YV12)
std::swap(frame.data[1], frame.data[2]);
return true;
}
}

InputStream::InputStream(
Expand Down Expand Up @@ -413,16 +453,16 @@ void InputStream::handleImage(StreamOutput& out, const depthcam_frame& f)

if(const auto codec = to_codec_id(f.format); codec != AV_CODEC_ID_NONE)
{
AVFrame* decoded = decodeCompressed(f);
::Video::AVFramePointer decoded = decodeCompressed(out, f);
if(f.release)
f.release(f.owner); // the decoder copied what it needed
if(decoded)
{
out.width.store(decoded->width, std::memory_order_relaxed);
out.height.store(decoded->height, std::memory_order_relaxed);
out.format.store(decoded->format, std::memory_order_relaxed);
stamp(out, f, decoded);
out.queue.enqueue(decoded);
stamp(out, f, decoded.get());
out.queue.enqueue(decoded.release());
}
return;
}
Expand All @@ -447,15 +487,11 @@ void InputStream::handleImage(StreamOutput& out, const depthcam_frame& f)
return;
}

// Fill in the remaining planes/strides for the borrowed buffer. The frame
// already owns its reference, so a failure here still releases correctly when
// the AVFramePointer goes out of scope.
if(!::Video::initFrameFromRawData(frame.get(), frame->data[0], f.bytes))
// The frame already owns its reference, so a failure here still releases
// correctly when the AVFramePointer goes out of scope.
if(!layout_planes(*frame, f))
return;

if(f.stride > 0)
frame->linesize[0] = f.stride;

out.width.store(f.width, std::memory_order_relaxed);
out.height.store(f.height, std::memory_order_relaxed);
out.format.store(int(pixfmt), std::memory_order_relaxed);
Expand Down Expand Up @@ -561,7 +597,8 @@ void InputStream::releaseCodecs()
av_packet_free(&m_packet);
}

AVFrame* InputStream::decodeCompressed(const depthcam_frame& f)
::Video::AVFramePointer
InputStream::decodeCompressed(StreamOutput& out, const depthcam_frame& f)
{
AVCodecContext* ctx = codecContext(to_codec_id(f.format));
if(!ctx)
Expand All @@ -582,14 +619,11 @@ AVFrame* InputStream::decodeCompressed(const depthcam_frame& f)
if(avcodec_send_packet(ctx, m_packet) < 0)
return nullptr;

AVFrame* frame = av_frame_alloc();
::Video::AVFramePointer frame = out.queue.newFrame();
if(!frame)
return nullptr;
if(avcodec_receive_frame(ctx, frame) < 0)
{
av_frame_free(&frame);
if(avcodec_receive_frame(ctx, frame.get()) < 0)
return nullptr;
}
return frame;
}

Expand Down
2 changes: 1 addition & 1 deletion DepthCamera/DepthCameraStream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ class InputStream final
void handlePointCloud(const depthcam_frame& f);
void handleImu(const depthcam_frame& f);

AVFrame* decodeCompressed(const depthcam_frame& f);
::Video::AVFramePointer decodeCompressed(StreamOutput& out, const depthcam_frame& f);
AVCodecContext* codecContext(AVCodecID id);
void releaseCodecs();

Expand Down
147 changes: 147 additions & 0 deletions Tests/unit/DepthCameraUnit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,15 @@
#include <catch2/catch_test_macros.hpp>

extern "C" {
#include <libavcodec/avcodec.h>
#include <libavutil/frame.h>
}

#include <chrono>
#include <cmath>
#include <set>
#include <thread>
#include <vector>

using namespace Gfx::DepthCamera;
namespace syn = depthcam_synthetic;
Expand Down Expand Up @@ -237,6 +240,150 @@ TEST_CASE("An unconsumed stream is neither queued nor produced", "[depthcam][str
r.stream->addConsumer(r.stream->m_pcl, -1);
}

namespace
{
struct Injected
{
std::vector<uint8_t> bytes;
static void release(void* p) { delete static_cast<Injected*>(p); }
};

void injectColor(Rig& r, int fmt, int w, int h, int stride, std::vector<uint8_t> bytes)
{
auto* owner = new Injected{std::move(bytes)};
depthcam_frame f{};
f.stream = DEPTHCAM_STREAM_COLOR;
f.format = fmt;
f.width = w;
f.height = h;
f.stride = stride;
f.data = owner->bytes.data();
f.bytes = owner->bytes.size();
f.owner = owner;
f.release = &Injected::release;
syn::inject(r.stream->device(), f);
}

std::vector<uint8_t> encodeJpeg(int w, int h)
{
std::vector<uint8_t> out;
const AVCodec* codec = avcodec_find_encoder(AV_CODEC_ID_MJPEG);
if(!codec)
return out;
AVCodecContext* ctx = avcodec_alloc_context3(codec);
ctx->width = w;
ctx->height = h;
ctx->pix_fmt = AV_PIX_FMT_YUVJ420P;
ctx->time_base = AVRational{1, 30};
if(avcodec_open2(ctx, codec, nullptr) < 0)
{
avcodec_free_context(&ctx);
return out;
}
AVFrame* frame = av_frame_alloc();
frame->format = ctx->pix_fmt;
frame->width = w;
frame->height = h;
av_frame_get_buffer(frame, 0);
for(int p = 0; p < 3; p++)
for(int y = 0; y < (p ? h / 2 : h); y++)
std::memset(frame->data[p] + y * frame->linesize[p], p ? 128 : 0x40, p ? w / 2 : w);
AVPacket* pkt = av_packet_alloc();
if(avcodec_send_frame(ctx, frame) >= 0 && avcodec_receive_packet(ctx, pkt) >= 0)
out.assign(pkt->data, pkt->data + pkt->size);
av_packet_free(&pkt);
av_frame_free(&frame);
avcodec_free_context(&ctx);
return out;
}

DepthCameraSettings colorSettings()
{
auto s = settingsFor("synthetic:clock=manual");
s.rgb = true;
s.depth = false;
return s;
}

struct ColorRig : Rig
{
ColorRig()
: Rig{"synthetic:clock=manual", colorSettings(), false}
{
stream->addConsumer(stream->m_rgb, 1);
}
};
}

TEST_CASE("Decoded MJPEG frames are recycled", "[depthcam][stream]")
{
const auto jpeg = encodeJpeg(64, 48);
REQUIRE(!jpeg.empty());
ColorRig r;
REQUIRE(r.stream->start());

std::set<AVFrame*> seen;
for(int i = 0; i < 10; i++)
{
injectColor(r, DEPTHCAM_FMT_MJPEG, 64, 48, 0, jpeg);
AVFrame* f = r.stream->m_rgb.queue.dequeue();
REQUIRE(f);
CHECK(f->width == 64);
CHECK(f->color_range == AVCOL_RANGE_JPEG);
seen.insert(f);
r.stream->m_rgb.queue.release(f);
}
CHECK(seen.size() <= 2);
r.stream->addConsumer(r.stream->m_rgb, -1);
}

TEST_CASE("YV12 frames have their chroma planes in U, V order", "[depthcam][stream]")
{
ColorRig r;
REQUIRE(r.stream->start());

std::vector<uint8_t> yv12(4 * 4 + 2 * 2 * 2);
std::memset(yv12.data(), 16, 16);
std::memset(yv12.data() + 16, 200, 4); // V
std::memset(yv12.data() + 20, 50, 4); // U
injectColor(r, DEPTHCAM_FMT_YV12, 4, 4, 0, yv12);

AVFrame* f = r.stream->m_rgb.queue.dequeue();
REQUIRE(f);
CHECK(f->format == AV_PIX_FMT_YUV420P);
CHECK(f->data[1][0] == 50);
CHECK(f->data[2][0] == 200);
r.stream->m_rgb.queue.release(f);
r.stream->addConsumer(r.stream->m_rgb, -1);
}

TEST_CASE("Planar frames with a padded stride lay out every plane", "[depthcam][stream]")
{
ColorRig r;
REQUIRE(r.stream->start());

// 4x4 I420 with 8-byte luma rows: Y 8x4, U 4x2, V 4x2.
std::vector<uint8_t> i420(32 + 8 + 8);
std::memset(i420.data(), 16, 32);
std::memset(i420.data() + 32, 50, 8);
std::memset(i420.data() + 40, 200, 8);
injectColor(r, DEPTHCAM_FMT_YUV420P, 4, 4, 8, i420);

AVFrame* f = r.stream->m_rgb.queue.dequeue();
REQUIRE(f);
CHECK(f->linesize[0] == 8);
CHECK(f->linesize[1] == 4);
CHECK(f->linesize[2] == 4);
CHECK(f->data[1] - f->data[0] == 32);
CHECK(f->data[2] - f->data[0] == 40);
r.stream->m_rgb.queue.release(f);

// A stride the buffer cannot hold is refused rather than read past.
injectColor(r, DEPTHCAM_FMT_YUV420P, 4, 4, 16, i420);
CHECK(r.stream->m_rgb.queue.dequeue() == nullptr);
r.stream->addConsumer(r.stream->m_rgb, -1);
}

// --- calibration -----------------------------------------------------------------

TEST_CASE("Calibration is read from the camera", "[depthcam][calibration]")
Expand Down
Loading