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
11 changes: 10 additions & 1 deletion apps/camera/src/CameraSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ namespace {
// frame after a restart can be torn.
constexpr int kStillFrames = 2;
constexpr size_t kPoolSize = 6;
// 30 fps: the exposure stays under a frame, and dim scenes take gain.
constexpr int64_t kFrameDuration = 33333;

void syncForCpu(int fd, bool start) {
void syncForCpu(int fd, bool start) {
dma_buf_sync sync{
__u64((start ? DMA_BUF_SYNC_START : DMA_BUF_SYNC_END) | DMA_BUF_SYNC_READ)};
ioctl(fd, DMA_BUF_IOCTL_SYNC, &sync);
Expand All @@ -35,6 +37,11 @@ namespace {
return last.offset + last.length;
}

void limitFrameDuration(const Camera &camera, ControlList &list) {
if (camera.controls().count(&controls::FrameDurationLimits))
list.set(controls::FrameDurationLimits, {kFrameDuration, kFrameDuration});
}

// A photo's buffer, mapped, and the allocator that owns it.
struct StillHold {
std::shared_ptr<FrameBufferAllocator> allocator;
Expand Down Expand Up @@ -282,6 +289,7 @@ bool CameraSession::startViewfinder() {
ControlList controls = m_sticky;
controls.merge(m_controls, ControlList::MergePolicy::OverwriteExisting);
m_controls = std::move(controls);
limitFrameDuration(*m_camera, m_controls);
}
for (const std::unique_ptr<Request> &request : m_requests) {
request->reuse(Request::ReuseBuffers);
Expand Down Expand Up @@ -488,6 +496,7 @@ void CameraSession::takeStill(const QSize &size) {
}
// Reconfigured, the autofocus starts a new scan: the photo keeps the lens where it was.
ControlList &first = m_stillRequests.front()->controls();
limitFrameDuration(*m_camera, first);
{
QMutexLocker locked(&m_controlLock);
if (m_hasAutofocus && m_lensPosition >= 0) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Patrick Quinn <patrick.jm.quinn@gmail.com>
Date: Sat, 28 Nov 2026 19:05:51 +0000
Subject: [PATCH 37/39] libcamera: software_isp: Sample fewer lines of tall
frames for statistics

The statistics read every second line pair of the window, every fourth
frame. On a 4096x3072 frame from uncached capture memory that took
127 ms on the Jolla Phone JP2601, long enough to drop frames every time
the statistics ran.

Halve the sampled line pairs until there are at most 128 of them. The
histogram, the colour sums and the focus contrast still come from
about 100000 Bayer quads of a 12 Mpixel frame. On the Jolla Phone the
statistics then take 17 ms, and the viewfinder runs at 30 fps.

Signed-off-by: Patrick Quinn <patrick.jm.quinn@gmail.com>
---
include/libcamera/internal/software_isp/swstats_cpu.h | 3 +++
src/libcamera/software_isp/swstats_cpu.cpp | 9 +++++++++
2 files changed, 12 insertions(+)

diff --git a/include/libcamera/internal/software_isp/swstats_cpu.h b/include/libcamera/internal/software_isp/swstats_cpu.h
index 68b354ed..74b97474 100644
--- a/include/libcamera/internal/software_isp/swstats_cpu.h
+++ b/include/libcamera/internal/software_isp/swstats_cpu.h
@@ -90,6 +90,9 @@ private:
*/
static constexpr uint32_t kStatPerNumFrames = 4;

+ /* Line pairs sampled at most per frame, see setWindow() */
+ static constexpr unsigned int kMaxLinePairs = 128;
+
int setupStandardBayerOrder(BayerFormat::Order order);
/* Bayer 8 bpp unpacked */
void statsBGGR8Line0(const uint8_t *src[], unsigned int y, SwIspStats &stats);
diff --git a/src/libcamera/software_isp/swstats_cpu.cpp b/src/libcamera/software_isp/swstats_cpu.cpp
index a38c0290..46ea0500 100644
--- a/src/libcamera/software_isp/swstats_cpu.cpp
+++ b/src/libcamera/software_isp/swstats_cpu.cpp
@@ -648,6 +648,15 @@ void SwStatsCpu::setWindow(const Rectangle &window)
window_.width = (window_.width > xShift_ ? window_.width - xShift_ : 0);
window_.width &= ~(patternSize_.width - 1);
window_.height &= ~(patternSize_.height - 1);
+
+ /*
+ * Reading the frame is most of the cost, and many capture devices give
+ * uncached buffers, so sample at most kMaxLinePairs line pairs of tall
+ * windows: every 2nd pair, or every 4th, 8th, and so on.
+ */
+ ySkipMask_ = 0x02;
+ while (window_.height / (ySkipMask_ + 2) > kMaxLinePairs)
+ ySkipMask_ = ySkipMask_ * 2 + 2;
}

void SwStatsCpu::processBayerFrame2(MappedFrameBuffer &in)
--
2.55.0

Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Patrick Quinn <patrick.jm.quinn@gmail.com>
Date: Sat, 28 Nov 2026 19:05:51 +0000
Subject: [PATCH 38/39] libcamera: v4l2_videodevice: Ask for cacheable MMAP
buffers

Drivers allocate MMAP buffers from coherent memory by default, which
the CPU maps uncached on Arm SoCs without I/O coherence. The software
ISP reads every frame it processes, so its statistics run several
times slower there.

Set V4L2_MEMORY_FLAG_NON_COHERENT when requesting MMAP buffers. vb2
then allocates cacheable memory and syncs the caches on QBUF and
DQBUF, so the frame is coherent when libcamera dequeues it. Drivers
that do not allow cache hints clear the flag and keep coherent memory.

Signed-off-by: Patrick Quinn <patrick.jm.quinn@gmail.com>
---
src/libcamera/v4l2_videodevice.cpp | 9 +++++++++
1 file changed, 9 insertions(+)

diff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp
index 41dc5d0f..beef3eb8 100644
--- a/src/libcamera/v4l2_videodevice.cpp
+++ b/src/libcamera/v4l2_videodevice.cpp
@@ -1317,6 +1317,15 @@ int V4L2VideoDevice::requestBuffers(unsigned int count,
rb.type = bufferType_;
rb.memory = memoryType;

+ /*
+ * Ask for cacheable memory: CPU reads of the default coherent memory
+ * are uncached on many Arm SoCs, and the software ISP reads every
+ * frame. The driver syncs the caches on QBUF and DQBUF, and ignores
+ * the flag when it does not support it.
+ */
+ if (memoryType == V4L2_MEMORY_MMAP)
+ rb.flags = V4L2_MEMORY_FLAG_NON_COHERENT;
+
ret = ioctl(VIDIOC_REQBUFS, &rb);
if (ret < 0) {
LOG(V4L2, Error)
--
2.55.0

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Patrick Quinn <patrick.jm.quinn@gmail.com>
Date: Sat, 28 Nov 2026 19:37:33 +0000
Subject: [PATCH 39/39] libipa: camera_sensor_helper: Fix the IMX766 gain model

The IMX766 codes its analogue gain as 16384 / (16384 - code), as
MediaTek's imgsensor driver for it computes it (gain2reg()), not as
1024 / (1024 - code). Measured on the Jolla Phone JP2601, the raw
frames scale with the 16384-based gain and barely move with codes
the 1024-based model picks.

Signed-off-by: Patrick Quinn <patrick.jm.quinn@gmail.com>
---
src/ipa/libipa/camera_sensor_helper.cpp | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/src/ipa/libipa/camera_sensor_helper.cpp b/src/ipa/libipa/camera_sensor_helper.cpp
index d97a7442..e05b7bb5 100644
--- a/src/ipa/libipa/camera_sensor_helper.cpp
+++ b/src/ipa/libipa/camera_sensor_helper.cpp
@@ -765,7 +765,8 @@ public:
{
/* 64 at 10 bits, as in the stock MediaTek driver. */
blackLevel_ = 4096;
- gain_ = AnalogueGainLinear{ 0, 1024, -1, 1024 };
+ /* 16384 / (16384 - code), as the stock driver's gain2reg(). */
+ gain_ = AnalogueGainLinear{ 0, 16384, -1, 16384 };
exposureMargin_ = 48;
}
};
--
2.55.0

23 changes: 19 additions & 4 deletions packaging/packages/libcamera/APKBUILD
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
# frame duration and vblank work), without qcam, the Python bindings and
# the docs, and with the patches the Zinwa Q25's MediaTek camera needs
# (software ISP for its 14-bit unpacked RAW, the simple pipeline on
# mtk-seninf, autofocus) and the Jolla JP2601 sensor helpers (0035, 0036);
# mtk-seninf, autofocus), the Jolla JP2601 sensor helpers (0035, 0036,
# 0039) and statistics that keep up with its 12 Mpixel frames (0037, 0038);
# each is written for upstream. 0013-0020 are
# Milan Zamazal's software ISP autofocus series v2, still in review.
# 99999 so it outranks postmarketOS's own fork (99990.<version>), which
Expand All @@ -15,7 +16,7 @@ _pkgver=0.8.0
_commit=5efdc682e5b54f7b9f793b1fbf99884d522fd9f9
_gitdate=20261118
pkgver=99999${_pkgver}_git$_gitdate
pkgrel=5
pkgrel=6
pkgdesc="Linux camera framework"
url="https://libcamera.org/"
arch="aarch64 x86_64"
Expand Down Expand Up @@ -81,8 +82,14 @@ source="libcamera-$_commit.tar.gz::https://gitlab.freedesktop.org/camera/libcame
0034-pipeline-simple-Pass-the-requested-stride-to-multi-p.patch
0035-libipa-camera_sensor_helper-Add-IMX616-and-IMX766-he.patch
0036-libcamera-camera_sensor_properties-Add-IMX616-and-IM.patch
0037-libcamera-software_isp-Sample-fewer-lines-of-tall-fr.patch
0038-libcamera-v4l2_videodevice-Ask-for-cacheable-MMAP-bu.patch
0039-libipa-camera_sensor_helper-Fix-the-IMX766-gain-mode.patch
s5kjn1.yaml
imx111.yaml
imx766.yaml
imx616.yaml
imx214.yaml
"
builddir="$srcdir/$pkgname-$_commit"
# gstreamer tests fail
Expand Down Expand Up @@ -121,9 +128,11 @@ build() {

package() {
DESTDIR="$pkgdir" meson install --no-rebuild -C output
# The Q25's cameras: tuned colour and contrast for the software ISP.
# The Q25's and the Jolla's cameras: colour and contrast for the software
# ISP.
install -Dm644 -t "$pkgdir"/usr/share/libcamera/ipa/softisp \
"$srcdir"/s5kjn1.yaml "$srcdir"/imx111.yaml
"$srcdir"/s5kjn1.yaml "$srcdir"/imx111.yaml \
"$srcdir"/imx766.yaml "$srcdir"/imx616.yaml "$srcdir"/imx214.yaml

# manual strip first..
scanelf --recursive \
Expand Down Expand Up @@ -209,6 +218,12 @@ d6e06b232e8d5eb08d11bcf6c99bc95f1c7b7c9df3aaafbfd608cfa68d0ea96c3145b218331213c1
f80f7d21df18601c89dde6dde2a6ac2f422fff7fa320760c0e2bcbc7ea7d4aa032c8331052254e53f13a2078a98e8109ea2454b21352e60883683f7b6110d5a2 0034-pipeline-simple-Pass-the-requested-stride-to-multi-p.patch
b7419b294245f9cbe58f21aede4fcc050d20849207b6977a9a6e0e66040bc3fdaff712f42b878fd04571ceb8feed4247ca7aeefd681ce8592197bc3a444455dc 0035-libipa-camera_sensor_helper-Add-IMX616-and-IMX766-he.patch
7d804f07a26d23f52c12f771c78bc4d71a5f9f02448d2d28784d894e86b06608dab718f1023598b7d4d776c88083eacc52912166fa460aca3fa0844db3ee8cb0 0036-libcamera-camera_sensor_properties-Add-IMX616-and-IM.patch
b347db2ac7d997259e5e395ba28d6406cd7fa0b74aff5fab91bb533e2187f5ad44a24411c68b9ee40d42fd841b27ac93478dfcca3616b6ea9b7de6029637fb61 0037-libcamera-software_isp-Sample-fewer-lines-of-tall-fr.patch
1eaa7261380d7b1155893cb7acd8798791e381a38710ba4cfcb3453b562ad3f195895210ae37401aa8e3d00e7b245bdbb5e2aa6abb31df3c88a6b4585deafec8 0038-libcamera-v4l2_videodevice-Ask-for-cacheable-MMAP-bu.patch
318e7d6518e92c04a6e2127d5521bc0778ae0b02afd03332c9d8619269f7dfa9c3e0496408f107fc183b99e0c460943e3b54c08ee7723227623f451be0293ec3 0039-libipa-camera_sensor_helper-Fix-the-IMX766-gain-mode.patch
1c9345a42c50624cc1f9b4f1a270375749ebcf20cce0cd777dbb2479aa9a105ce49d333d889d7405aca3d3a2f9fed8c3194d85ef27eecf69b7dabbaffad1ab01 s5kjn1.yaml
a9e2cfad7a92c8f63fd097fcd02dd64bd7782a64b543431536ec1480494091c2e3872206bfb7546981fd08593a42a2337270e4f3ff40787b938843503b0b3065 imx111.yaml
f668c85243e578a8f7c55837d6e02709fe7383206cb237ac655239e7da43b2e5cfa97dd2d3fb13aa9d7e315f2519327886a50cb41a2c426ba54c41f5a114523e imx766.yaml
10c237cf1b99f698498a909afc02bd06b6734ca1a68bfcff73270e0a8bd1743990c65b82c128f6b00ecbf603769737475a36e1cb25432454ef5df69bbc083002 imx616.yaml
95264a0c2ad5cead7937defc0c364baf127c1b96ba9d98282312e15ad9dc168b0771ca760742e0430c8e9ed8b53f3926bf24ae29224bc9b495a8ba9e9e518851 imx214.yaml
"
21 changes: 21 additions & 0 deletions packaging/packages/libcamera/imx214.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# SPDX-License-Identifier: CC0-1.0
%YAML 1.1
---
# The Jolla Phone JP2601's ultrawide rear camera. Interim: the colour matrix is
# the IMX708's 4640 K one from libcamera's Raspberry Pi tuning, standing
# in for a Sony sensor until a ColorChecker fit for this one. The black
# level and the gain model come from the sensor helper.
version: 1
algorithms:
- BlackLevel:
- Awb:
- Ccm:
ccms:
- ct: 4640
ccm: [ 1.530, -0.352, -0.178,
-0.283, 1.671, -0.388,
0.017, -0.572, 1.555 ]
- Adjust:
saturation: 1.2
- Agc:
...
21 changes: 21 additions & 0 deletions packaging/packages/libcamera/imx616.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# SPDX-License-Identifier: CC0-1.0
%YAML 1.1
---
# The Jolla Phone JP2601's front camera. Interim: the colour matrix is
# the IMX708's 4640 K one from libcamera's Raspberry Pi tuning, standing
# in for a Sony sensor until a ColorChecker fit for this one. The black
# level and the gain model come from the sensor helper.
version: 1
algorithms:
- BlackLevel:
- Awb:
- Ccm:
ccms:
- ct: 4640
ccm: [ 1.530, -0.352, -0.178,
-0.283, 1.671, -0.388,
0.017, -0.572, 1.555 ]
- Adjust:
saturation: 1.2
- Agc:
...
21 changes: 21 additions & 0 deletions packaging/packages/libcamera/imx766.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# SPDX-License-Identifier: CC0-1.0
%YAML 1.1
---
# The Jolla Phone JP2601's main rear camera. Interim: the colour matrix is
# the IMX708's 4640 K one from libcamera's Raspberry Pi tuning, standing
# in for a Sony sensor until a ColorChecker fit for this one. The black
# level and the gain model come from the sensor helper.
version: 1
algorithms:
- BlackLevel:
- Awb:
- Ccm:
ccms:
- ct: 4640
ccm: [ 1.530, -0.352, -0.178,
-0.283, 1.671, -0.388,
0.017, -0.572, 1.555 ]
- Adjust:
saturation: 1.2
- Agc:
...