Skip to content

Commit 53cf84d

Browse files
committed
pipeline: rpi: Add SW 14-bit unpack option
Signed-off-by: Naushir Patuck <[email protected]>
1 parent 73dfcfa commit 53cf84d

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

src/libcamera/pipeline/rpi/common/rpi_stream.h

+5
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ class Stream : public libcamera::Stream
8787
* conversion to be applied before ISP processing.
8888
*/
8989
Needs16bitEndianSwap = (1 << 5),
90+
/*
91+
* Indicates that the input stream needs a software 14-bit to
92+
* 16-bit unpacking.
93+
*/
94+
Needs14bitUnpack = (1 << 6),
9095
};
9196

9297
using StreamFlags = Flags<StreamFlag>;

src/libcamera/pipeline/rpi/pisp/pisp.cpp

+47-3
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,31 @@ void do16BitEndianSwap([[maybe_unused]] void *mem, [[maybe_unused]] unsigned int
392392
#endif
393393
}
394394

395+
void do14bitUnpack(void *mem, unsigned int width, unsigned int height,
396+
unsigned int stride)
397+
{
398+
std::vector<uint8_t> cache(stride);
399+
400+
for (unsigned int j = 0; j < height; j++) {
401+
const uint8_t *in = ((uint8_t *)mem) + j * stride;
402+
uint8_t *out = ((uint8_t *)mem) + j * stride;
403+
uint8_t *p = cache.data();
404+
405+
std::memcpy(p, in, stride);
406+
for (unsigned int i = 0; i < width; i += 4, p += 7) {
407+
uint16_t p0 = (p[0] << 8) | ((p[4] & 0x3f) << 2);
408+
uint16_t p1 = (p[1] << 8) | ((p[4] & 0xc0) >> 4) | ((p[5] & 0x0f) << 4);
409+
uint16_t p2 = (p[2] << 8) | ((p[5] & 0xf0) >> 2) | ((p[6] & 0x03) << 6);
410+
uint16_t p3 = (p[3] << 8) | (p[6] & 0xfc);
411+
412+
*(uint16_t *)(out + i * 2 + 0) = p0;
413+
*(uint16_t *)(out + i * 2 + 2) = p1;
414+
*(uint16_t *)(out + i * 2 + 4) = p2;
415+
*(uint16_t *)(out + i * 2 + 6) = p3;
416+
}
417+
}
418+
}
419+
395420
void downscaleInterleaved3(void *mem, unsigned int height, unsigned int src_width,
396421
unsigned int stride)
397422
{
@@ -1433,6 +1458,15 @@ int PiSPCameraData::platformConfigure(const RPi::RPiCameraConfiguration *rpiConf
14331458
<< " will not be correct. You must use manual camera settings.";
14341459
}
14351460

1461+
/* Ditto for the 14-bit unpacking. */
1462+
cfe_[Cfe::Output0].clearFlags(StreamFlag::Needs14bitUnpack);
1463+
if (MediaBusFormatInfo::info(rpiConfig->sensorFormat_.code).bitsPerPixel == 14) {
1464+
cfe_[Cfe::Output0].setFlags(StreamFlag::Needs14bitUnpack);
1465+
LOG(RPI, Warning)
1466+
<< "The sensor is configured for a 14-bit output, statistics"
1467+
<< " will not be correct. You must use manual camera settings.";
1468+
}
1469+
14361470
ret = cfe->setFormat(&cfeFormat);
14371471
if (ret)
14381472
return ret;
@@ -1682,8 +1716,9 @@ void PiSPCameraData::cfeBufferDequeue(FrameBuffer *buffer)
16821716
job.buffers[stream] = buffer;
16831717

16841718
if (stream == &cfe_[Cfe::Output0]) {
1685-
/* Do an endian swap if needed. */
1686-
if (stream->getFlags() & StreamFlag::Needs16bitEndianSwap) {
1719+
/* Do an endian swap or 14-bit unpacking if needed. */
1720+
if (stream->getFlags() & StreamFlag::Needs16bitEndianSwap ||
1721+
stream->getFlags() & StreamFlag::Needs14bitUnpack) {
16871722
const unsigned int stride = stream->configuration().stride;
16881723
const unsigned int width = stream->configuration().size.width;
16891724
const unsigned int height = stream->configuration().size.height;
@@ -1693,7 +1728,12 @@ void PiSPCameraData::cfeBufferDequeue(FrameBuffer *buffer)
16931728
void *mem = b.mapped->planes()[0].data();
16941729

16951730
dmabufSyncStart(buffer->planes()[0].fd);
1696-
do16BitEndianSwap(mem, width, height, stride);
1731+
1732+
if (stream->getFlags() & StreamFlag::Needs16bitEndianSwap)
1733+
do16BitEndianSwap(mem, width, height, stride);
1734+
else
1735+
do14bitUnpack(mem, width, height, stride);
1736+
16971737
dmabufSyncEnd(buffer->planes()[0].fd);
16981738
}
16991739

@@ -1861,6 +1901,10 @@ int PiSPCameraData::configureCfe()
18611901
pisp_image_format_config image = toPiSPImageFormat(cfeFormat);
18621902
pisp_fe_input_config input = {};
18631903

1904+
/* 14-bit bodge */
1905+
if (cfe_[Cfe::Output0].getFlags() & StreamFlag::Needs14bitUnpack)
1906+
image.width = image.width * 14 / 16;
1907+
18641908
input.streaming = 1;
18651909
input.format = image;
18661910
input.format.format = PISP_IMAGE_FORMAT_BPS_16;

0 commit comments

Comments
 (0)