Skip to content

sensor/hi3516cv200: OV2710 analogue gain is 16x linear, not one step less - #303

Closed
zx97 wants to merge 1 commit into
OpenIPC:mainfrom
zx97:fix-ov2710-gain-encoding
Closed

zx97 wants to merge 1 commit into
OpenIPC:mainfrom
zx97:fix-ov2710-gain-encoding

Conversation

@zx97

@zx97 zx97 commented Nov 16, 2026

Copy link
Copy Markdown

cmos_gains_update() encodes the analogue gain one step low. The OV2710 codes gain as 16 * linear in the 9-bit 0x350a/0x350b pair, so 1x is 0x10, but the code computes ((1 << index) - 1) << 4:

gain before after
1x 0x000 0x010
2x 0x010 0x020
4x 0x030 0x040
8x 0x070 0x080
16x 0x0f0 0x100
32x 0x1f0 0x1ff (clamped)

The sensor's own init table in the same driver writes 0x350b = 0x10, so 0x000 at 1x is below its documented minimum.

The 32x entry needed a second half. 0x200 shifted right by 8 gives 2, and cmos_get_sns_regs_info() declares the high byte of the pair; the value is stored with & 0x1, so 0x200 truncated straight back to 0x000 - the exact value this change removes. The clamp keeps the pair inside the 9 bits the sensor implements.

-    u16RegValue = (((1 << u32Again) - 1) << 4) | ((u32Dgain - 16) & 0xf);
+    u16RegValue = ((1u << u32Again) << 4) | ((u32Dgain - 16) & 0xf);
+    if (u16RegValue > 0x1ff)
+    {
+        u16RegValue = 0x1ff;
+    }

Measured on hardware

A VStarcam C23S-class Hi3518EV200 board, OV2710 wired to MIPI, running the shipped libsns_ov2710.so from the hi3516cv200 package:

ISP says      : Again = 1024 (1x Q10)
sensor before : 0x350a = 0x00   0x350b = 0x00
sensor after  : 0x350a = 0x00   0x350b = 0x10

With the sensor handed its own AE (0x3503 = 0x00) it asks for 0x350b = 0x12 and 1098 of 1104 lines, which is a sane request - so the sensor side is fine and this is purely the encoding in the driver.

Built

arm-linux-musleabi-cross (gcc 11.2.1), -march=armv5te, against the headers the Makefile expects. ABI checked against the shipped library before deploying:

check shipped rebuilt
nm -D --defined-only symbols — identical
readelf -d NEEDED libc.so libc.so
Tag_CPU_arch v5TEJ v5TE
Tag_ABI_VFP_args absent absent

What this does not fix

The picture on that board stays noisy, and it is not this bug: the AE pins the integration time at 2 lines (0x3501 = 0x00, 0x3502 = 0x20) while the ISP reports Exp = 2049, and the noise is invariant to both exposure (550 vs 1098 lines) and gain. The figure is added downstream - [DRC] runs with DRCStrengthTarget = 128 / Stretch = 54, and the 3DNR temporal strength is capped at TFStrMax = 14, which cannot smooth temporal noise. Sent separately because the gain encoding is a defect against the sensor's own encoding.

Also worth knowing for anyone bringing this sensor up: in OpenIPC/firmware, ov2710_mipi_1080p.ini selects the parallel interface (input_mode = 4, [vi_dev] Input_mod = 2), so the VI stays in DC and IntCnt stays 0. Reported separately on OpenIPC/firmware#2243.

…less

cmos_gains_update() encodes the analogue gain one increment low. The OV2710
codes gain as 16 * linear in the 9-bit 0x350a/0x350b pair, so 1x is 0x10, but
the code computed ((1 << index) - 1) << 4:

    gain   before    after
     1x    0x000     0x010
     2x    0x010     0x020
     4x    0x030     0x040
     8x    0x070     0x080
    16x    0x0f0     0x100
    32x    0x1f0     0x1ff (clamped)

Measured on a VStarcam C23S-class Hi3518EV200 board, OV2710 over MIPI: the
ISP reported Again = 1024 (1x Q10) while the sensor held 0x350b = 0x00, below
its own documented minimum, and the register's own init table writes 0x10.

The 32x entry needs the clamp as well: 0x200 shifted right by 8 gives 2, and
the high byte is masked with & 0x1, so 0x200 truncated back to 0x000 - the very
value this change removes. Clamping at 0x1ff keeps the pair inside the 9 bits
the sensor implements.

Verified: /usr/lib/sensors/libsns_ov2710.so rebuilt with
arm-linux-musleabi-cross (gcc 11.2.1, -march=armv5te), exported symbols
identical to the shipped library, NEEDED libc.so (musl), no VFP ABI tag, and
0x350b now reads 0x10 with the same Again = 1x reported by the ISP. The stream
is unaffected.

This does not fix the remaining noise on that board: the AE still pins the
integration time at 2 lines while reporting Exp = 2049, and the noise is
invariant to exposure and gain, so the figure is added downstream (DRC and a
capped 3DNR are the suspects). Sent on its own because the gain encoding is a
defect against the sensor's own encoding and is independent of that.
@zx97
zx97 force-pushed the fix-ov2710-gain-encoding branch from 1cd04a9 to f9f2867 Compare November 16, 2026 10:14
@qodo-free-for-open-source-projects

Copy link
Copy Markdown

PR Summary by Qodo

Fix OV2710 analogue gain encoding and clamp to 9 bits

🐞 Bug fix 🕐 10-20 Minutes

Grey Divider

AI Description

• Correct OV2710 gain encoding so 1× starts at the sensor’s expected register value.
• Clamp the 32× entry to prevent overflow from wrapping the register pair to zero.
Diagram

graph TD
    AE["AE gain request"] --> Encode["Encode linear gain"] --> Limit{"Exceeds 9 bits?"} -->|yes| Clamp["Clamp to 0x1ff"] --> Buffer["I2C register data"] --> Sensor["OV2710 gain registers"]
    Limit -->|no| Buffer
Loading
High-Level Assessment

The localized encoding fix is appropriate: it preserves the existing AE gain table and register-update path while preventing the maximum entry from wrapping to zero. Changing the advertised gain range would affect AE behavior without correcting the lower entries.

Files changed (1) +9 / -1

Bug fix (1) +9 / -1
ov2710_cmos.cCorrect and bound OV2710 gain register encoding +9/-1

Correct and bound OV2710 gain register encoding

• Removes the one-step subtraction so the encoded analogue gain starts at 0x010 for 1×. Caps the combined gain code at 0x1ff before it is split into the sensor’s 9-bit register pair, preventing the 32× entry from wrapping to zero.

libraries/sensor/hi3516cv200/omnivision_ov2710/ov2710_cmos.c

@qodo-free-for-open-source-projects

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (1) 📘 Rule violations (0) 📎 Requirement gaps (0) 📜 Skill insights (0)

Grey Divider


Informational

1. 32x gain is programmed as 31.9x without a note 🐞 Bug ⚙ Maintainability
Description
The clamp in cmos_gains_update replaces the 32x value 0x200 with 0x1ff, so the sensor receives
511/16 ≈ 31.94x while the ISP was told 32x (Again index 5, Q10 32768). The roughly 2% shortfall is
not documented next to the clamp, and the comment above it only explains the register encoding. A
later reader could take the clamp for a gain-table or scaling bug.
Code

libraries/sensor/hi3516cv200/omnivision_ov2710/ov2710_cmos.c[R216-218]

+    if (u16RegValue > 0x1ff)
+    {
+        u16RegValue = 0x1ff;
Evidence
The table analog_gain_table[5] = 32768 (Q10, i.e. 32x) maps to index 5 via
cmos_again_calc_table. The new formula yields (1u << 5) << 4 = 0x200, which the clamp reduces to
0x1ff. The 9-bit field 0x1ff encodes 511/16 ≈ 31.94x, so the programmed gain is slightly below the
value the ISP believes it applied.

libraries/sensor/hi3516cv200/omnivision_ov2710/ov2710_cmos.c[182-199]
libraries/sensor/hi3516cv200/omnivision_ov2710/ov2710_cmos.c[207-217]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
The 32x analogue gain entry is clamped from 0x200 to 0x1ff in `cmos_gains_update`, so the sensor is programmed at about 31.94x while the ISP assumes 32x. The reason is not recorded.

## Fix Focus Areas
- libraries/sensor/hi3516cv200/omnivision_ov2710/ov2710_cmos.c[207-220]

## Recommended Fix
Extend the existing comment to state that 0x200 (32x) does not fit the 9-bit field, so the clamp to 0x1ff yields about 31.94x. Either accept that the ISP's 32x is approximate, or revisit the gain table if exact 32x is required. No code change is needed for the comment fix.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Grey Divider

Context sources
Review mode: Auto: 🚀 Fast: Localized sensor gain encoding fix with one straightforward runtime path.

Grey Divider

Tip of the day
💡 Did you know, you can choose which labels appear on a finding, and whether they show icons or text

More tips ↗ | Customize Qodo ↗ | Qodo docs ↗

Grey Divider

Qodo Logo

@zx97

zx97 commented Nov 16, 2026

Copy link
Copy Markdown
Author

I was wrong, and this PR should not be merged. The formula it replaces is correct.

Two independent drivers settle the encoding, and both agree with the code in
main:

  • allwinner-zh/linux-3.4-sunxi, ov2710_mipi.c, sensor_s_gain(): the top
    four bits of 0x350b are range bits (each doubles the gain) and the low
    nibble is the mantissa, (gain_q4 - 16) & 0x0f.
  • espressif/esp-video-components, ov2710_mipi_1lane_24Minput_1920x1080_raw10_25fps.h:
    same table shape, same operating point as this board (1 lane, 24 MHz, RAW10).

Carrying ((1 << index) - 1) << 4 through that encoding:

gain openIPC main this PR Allwinner sensor_s_gain()
1x 0x000 0x010 0x000
2x 0x010 0x020 0x010
4x 0x030 0x040 0x030
8x 0x070 0x080 0x070
16x 0x0f0 0x100 0x0f0
32x 0x1f0 0x1ff 0x1f0

So main is right at every step, my patch shifts every gain one step up -
at 1x it asks for 2x - and the second defect I claimed does not exist: the
original never reaches 0x200, because (31 << 4) is exactly 0x1f0 and the
& 0x1 high-byte mask carries it correctly.

Measured on the board, with everything else unchanged:

main             0x350b = 0x00   frame mean 109, local 8x8 noise 71.8
this PR          0x350b = 0x10   frame mean  93, local 8x8 noise 78.6

Darker with more noise, which is what a doubled analogue gain looks like. I
have reverted my board to the shipped library.

Closing. The part of the investigation that still stands is on
OpenIPC/firmware#2243: the shipped ov2710_mipi_1080p.ini selected the
parallel interface (fixed in OpenIPC/firmware#2869), and with the sensor's own
colour-bar test pattern the frame is perfectly clean, which puts the remaining
noise in the analogue readout rather than anywhere in the digital path.

Thanks to whoever looks at this - the cross-check against a second driver is
what caught it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant