From 54c1303858e9105f393bae1a67184254048e5548 Mon Sep 17 00:00:00 2001 From: patrickjquinn Date: Sat, 28 Nov 2026 19:27:58 +0000 Subject: [PATCH 1/3] perf(camera): hold the viewfinder and photos at 30 fps The software ISP's exposure control lengthens the frames to 66 ms in a dim room, so the viewfinder fell to 15 fps. Ask for a 33.3 ms frame duration for the viewfinder and the photo's frames: the exposure stays within one frame and a dim scene takes more gain instead. On the Jolla Phone's IMX766 with the libcamera from this branch, the viewfinder (1400x1050) ran at 30.0 fps with no frame lost over 360 frames, measured in the app. --- apps/camera/src/CameraSession.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/apps/camera/src/CameraSession.cpp b/apps/camera/src/CameraSession.cpp index 21a6a9a7a..897fd4a68 100644 --- a/apps/camera/src/CameraSession.cpp +++ b/apps/camera/src/CameraSession.cpp @@ -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); @@ -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 allocator; @@ -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 : m_requests) { request->reuse(Request::ReuseBuffers); @@ -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) { From 8460d3380317463d9c5284ca1349b0b1b0dc7cb1 Mon Sep 17 00:00:00 2001 From: patrickjquinn Date: Sat, 28 Nov 2026 20:01:31 +0000 Subject: [PATCH 2/3] perf(libcamera): keep the software ISP at 30 fps on the Jolla's 12 Mpixel sensor (r6) - 0037: sample at most 128 line pairs for the statistics. On the Jolla Phone the statistics read each 4096x3072 frame from uncached CAMSV memory in 127 ms, every fourth frame; now 17 ms. The Camera app's viewfinder went from 23.6 fps to 30.0 fps, with no frame lost, and the GPU debayer plus statistics from 37.5 to 12.2 ms a frame. - 0038: ask for cacheable MMAP buffers (V4L2_MEMORY_FLAG_NON_COHERENT); drivers that don't allow cache hints ignore it. CAMSV allows them from linux-mt6858 r27. - 0039: the IMX766's analogue gain is 16384 / (16384 - code), as in MediaTek's driver; with the 1024-based model the AGC believed it ran 16x while the sensor stayed near 1x (raw levels measured on the phone at 1x, 4x and 16x were the same), so frames came out grey and dark. --- ...re_isp-Sample-fewer-lines-of-tall-fr.patch | 59 +++++++++++++++++++ ...ideodevice-Ask-for-cacheable-MMAP-bu.patch | 44 ++++++++++++++ ...nsor_helper-Fix-the-IMX766-gain-mode.patch | 33 +++++++++++ packaging/packages/libcamera/APKBUILD | 11 +++- 4 files changed, 145 insertions(+), 2 deletions(-) create mode 100644 packaging/packages/libcamera/0037-libcamera-software_isp-Sample-fewer-lines-of-tall-fr.patch create mode 100644 packaging/packages/libcamera/0038-libcamera-v4l2_videodevice-Ask-for-cacheable-MMAP-bu.patch create mode 100644 packaging/packages/libcamera/0039-libipa-camera_sensor_helper-Fix-the-IMX766-gain-mode.patch diff --git a/packaging/packages/libcamera/0037-libcamera-software_isp-Sample-fewer-lines-of-tall-fr.patch b/packaging/packages/libcamera/0037-libcamera-software_isp-Sample-fewer-lines-of-tall-fr.patch new file mode 100644 index 000000000..0d4686e81 --- /dev/null +++ b/packaging/packages/libcamera/0037-libcamera-software_isp-Sample-fewer-lines-of-tall-fr.patch @@ -0,0 +1,59 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Patrick Quinn +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 +--- + 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 + diff --git a/packaging/packages/libcamera/0038-libcamera-v4l2_videodevice-Ask-for-cacheable-MMAP-bu.patch b/packaging/packages/libcamera/0038-libcamera-v4l2_videodevice-Ask-for-cacheable-MMAP-bu.patch new file mode 100644 index 000000000..4fb5b1a8c --- /dev/null +++ b/packaging/packages/libcamera/0038-libcamera-v4l2_videodevice-Ask-for-cacheable-MMAP-bu.patch @@ -0,0 +1,44 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Patrick Quinn +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 +--- + 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 + diff --git a/packaging/packages/libcamera/0039-libipa-camera_sensor_helper-Fix-the-IMX766-gain-mode.patch b/packaging/packages/libcamera/0039-libipa-camera_sensor_helper-Fix-the-IMX766-gain-mode.patch new file mode 100644 index 000000000..66ef930f6 --- /dev/null +++ b/packaging/packages/libcamera/0039-libipa-camera_sensor_helper-Fix-the-IMX766-gain-mode.patch @@ -0,0 +1,33 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Patrick Quinn +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 +--- + 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 + diff --git a/packaging/packages/libcamera/APKBUILD b/packaging/packages/libcamera/APKBUILD index 5ffa50374..dff3eed19 100644 --- a/packaging/packages/libcamera/APKBUILD +++ b/packaging/packages/libcamera/APKBUILD @@ -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.), which @@ -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" @@ -81,6 +82,9 @@ 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 " @@ -209,6 +213,9 @@ 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 " From 4f2752b510aecfc273535e5b418c2a8863f8378b Mon Sep 17 00:00:00 2001 From: patrickjquinn Date: Sat, 28 Nov 2026 20:46:24 +0000 Subject: [PATCH 3/3] feat(libcamera): interim tuning for the Jolla's three cameras Without a tuning file the software ISP falls back to uncalibrated.yaml: no colour matrix, so the sensors' overlapping colour channels came out nearly grey. Give the IMX766, IMX616 and IMX214 a colour matrix and a little more saturation (1.2). The matrix is the IMX708's 4640 K one from libcamera's Raspberry Pi tuning, a stand-in for a Sony sensor until a ColorChecker fit per sensor. On the IMX214 under a desk lamp, the mean saturation of the viewfinder went from 11.7 to about 30 (HSV, pixels above value 40). --- packaging/packages/libcamera/APKBUILD | 12 ++++++++++-- packaging/packages/libcamera/imx214.yaml | 21 +++++++++++++++++++++ packaging/packages/libcamera/imx616.yaml | 21 +++++++++++++++++++++ packaging/packages/libcamera/imx766.yaml | 21 +++++++++++++++++++++ 4 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 packaging/packages/libcamera/imx214.yaml create mode 100644 packaging/packages/libcamera/imx616.yaml create mode 100644 packaging/packages/libcamera/imx766.yaml diff --git a/packaging/packages/libcamera/APKBUILD b/packaging/packages/libcamera/APKBUILD index dff3eed19..bf1bcb487 100644 --- a/packaging/packages/libcamera/APKBUILD +++ b/packaging/packages/libcamera/APKBUILD @@ -87,6 +87,9 @@ source="libcamera-$_commit.tar.gz::https://gitlab.freedesktop.org/camera/libcame 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 @@ -125,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 \ @@ -218,4 +223,7 @@ b347db2ac7d997259e5e395ba28d6406cd7fa0b74aff5fab91bb533e2187f5ad44a24411c68b9ee4 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 " diff --git a/packaging/packages/libcamera/imx214.yaml b/packaging/packages/libcamera/imx214.yaml new file mode 100644 index 000000000..0788cc0a5 --- /dev/null +++ b/packaging/packages/libcamera/imx214.yaml @@ -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: +... diff --git a/packaging/packages/libcamera/imx616.yaml b/packaging/packages/libcamera/imx616.yaml new file mode 100644 index 000000000..ed14638a4 --- /dev/null +++ b/packaging/packages/libcamera/imx616.yaml @@ -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: +... diff --git a/packaging/packages/libcamera/imx766.yaml b/packaging/packages/libcamera/imx766.yaml new file mode 100644 index 000000000..3384e9fb9 --- /dev/null +++ b/packaging/packages/libcamera/imx766.yaml @@ -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: +...