CI / test (push) Canceled after 0s
- Add chandra-camera-isp.c (C/OpenMP) for Quad-Bayer unpack, CCM, and tone mapping, with a NumPy fallback when it is not built - Add non-blocking contrast-detection VCM autofocus, with lens subdev discovery on the sensor's I2C bus - Tune Quad-Bayer AE: a video-mode vblank cap, damped steps, and full analog gain before extending blanking - Record variable-frame-rate video through a streaming Matroska muxer, and count dropped frames - Ship libcamera IPA tuning files for imx376, imx519, and uncalibrated sensors - Map camera buffers by fd and sync them with dma-buf ioctls
220 lines
7.6 KiB
C
220 lines
7.6 KiB
C
#include <math.h>
|
|
#include <stdint.h>
|
|
#include <stddef.h>
|
|
|
|
#ifdef _OPENMP
|
|
#include <omp.h>
|
|
#endif
|
|
|
|
/* Unpack 10-bit CSI-2 packed 4x4 Quad-Bayer SRGGB into 4x4 supercell BGR16 (0..4092). */
|
|
void quad_csi2p_to_bgr16_4x4(
|
|
const uint8_t *raw,
|
|
int width,
|
|
int height,
|
|
int stride,
|
|
uint16_t *out_bgr16
|
|
) {
|
|
if (!raw || !out_bgr16 || width < 4 || height < 4 || stride <= 0) {
|
|
return;
|
|
}
|
|
const int out_w = width / 4;
|
|
const int out_h = height / 4;
|
|
|
|
#ifdef _OPENMP
|
|
#pragma omp parallel for schedule(static) num_threads(4) if (out_h >= 64)
|
|
#endif
|
|
for (int cy = 0; cy < out_h; cy++) {
|
|
const uint8_t *r0 = raw + (size_t)(cy * 4 + 0) * (size_t)stride;
|
|
const uint8_t *r1 = raw + (size_t)(cy * 4 + 1) * (size_t)stride;
|
|
const uint8_t *r2 = raw + (size_t)(cy * 4 + 2) * (size_t)stride;
|
|
const uint8_t *r3 = raw + (size_t)(cy * 4 + 3) * (size_t)stride;
|
|
uint16_t *dst = out_bgr16 + (size_t)cy * (size_t)out_w * 3;
|
|
|
|
for (int cx = 0; cx < out_w; cx++) {
|
|
const size_t off = (size_t)cx * 5;
|
|
const uint8_t lo0 = r0[off + 4];
|
|
const uint8_t lo1 = r1[off + 4];
|
|
const uint8_t lo2 = r2[off + 4];
|
|
const uint8_t lo3 = r3[off + 4];
|
|
|
|
/* Rows 4y, 4y+1: (p0, p1) = R, (p2, p3) = Gr */
|
|
uint32_t r_sum = ((uint32_t)r0[off + 0] + r0[off + 1] + r1[off + 0] + r1[off + 1]) << 2;
|
|
r_sum += (uint32_t)(lo0 & 3U) + ((lo0 >> 2) & 3U) + (lo1 & 3U) + ((lo1 >> 2) & 3U);
|
|
|
|
uint32_t gr_sum = ((uint32_t)r0[off + 2] + r0[off + 3] + r1[off + 2] + r1[off + 3]) << 2;
|
|
gr_sum += (uint32_t)((lo0 >> 4) & 3U) + ((lo0 >> 6) & 3U) + ((lo1 >> 4) & 3U) + ((lo1 >> 6) & 3U);
|
|
|
|
/* Rows 4y+2, 4y+3: (p0, p1) = Gb, (p2, p3) = B */
|
|
uint32_t gb_sum = ((uint32_t)r2[off + 0] + r2[off + 1] + r3[off + 0] + r3[off + 1]) << 2;
|
|
gb_sum += (uint32_t)(lo2 & 3U) + ((lo2 >> 2) & 3U) + (lo3 & 3U) + ((lo3 >> 2) & 3U);
|
|
|
|
uint32_t b_sum = ((uint32_t)r2[off + 2] + r2[off + 3] + r3[off + 2] + r3[off + 3]) << 2;
|
|
b_sum += (uint32_t)((lo2 >> 4) & 3U) + ((lo2 >> 6) & 3U) + ((lo3 >> 4) & 3U) + ((lo3 >> 6) & 3U);
|
|
|
|
uint32_t g_avg = (gr_sum + gb_sum + 1U) >> 1;
|
|
|
|
dst[0] = (uint16_t)b_sum;
|
|
dst[1] = (uint16_t)g_avg;
|
|
dst[2] = (uint16_t)r_sum;
|
|
dst += 3;
|
|
}
|
|
}
|
|
}
|
|
|
|
/* Unpack 10-bit CSI-2 packed 4x4 Quad-Bayer SRGGB via 2x2 binning into 16-bit Bayer (0..4092). */
|
|
void quad_csi2p_to_bayer16_2x2(
|
|
const uint8_t *raw,
|
|
int width,
|
|
int height,
|
|
int stride,
|
|
uint16_t *out_bayer16
|
|
) {
|
|
if (!raw || !out_bayer16 || width < 4 || height < 2 || stride <= 0) {
|
|
return;
|
|
}
|
|
const int nblk = width / 4;
|
|
const int out_w = nblk * 2;
|
|
const int out_h = height / 2;
|
|
|
|
#ifdef _OPENMP
|
|
#pragma omp parallel for schedule(static) num_threads(4) if (out_h >= 64)
|
|
#endif
|
|
for (int by = 0; by < out_h; by++) {
|
|
const uint8_t *r0 = raw + (size_t)(by * 2 + 0) * (size_t)stride;
|
|
const uint8_t *r1 = raw + (size_t)(by * 2 + 1) * (size_t)stride;
|
|
uint16_t *dst = out_bayer16 + (size_t)by * (size_t)out_w;
|
|
|
|
for (int bx = 0; bx < nblk; bx++) {
|
|
const size_t off = (size_t)bx * 5;
|
|
const uint8_t lo0 = r0[off + 4];
|
|
const uint8_t lo1 = r1[off + 4];
|
|
|
|
uint32_t c0 = ((uint32_t)r0[off + 0] + r0[off + 1] + r1[off + 0] + r1[off + 1]) << 2;
|
|
c0 += (uint32_t)(lo0 & 3U) + ((lo0 >> 2) & 3U) + (lo1 & 3U) + ((lo1 >> 2) & 3U);
|
|
|
|
uint32_t c1 = ((uint32_t)r0[off + 2] + r0[off + 3] + r1[off + 2] + r1[off + 3]) << 2;
|
|
c1 += (uint32_t)((lo0 >> 4) & 3U) + ((lo0 >> 6) & 3U) + ((lo1 >> 4) & 3U) + ((lo1 >> 6) & 3U);
|
|
|
|
dst[0] = (uint16_t)c0;
|
|
dst[1] = (uint16_t)c1;
|
|
dst += 2;
|
|
}
|
|
}
|
|
}
|
|
|
|
/* Fused black-level subtraction, AWB + exposure scaling, 3x3 linear RGB CCM, and LUT tone-map. */
|
|
void isp_ccm_tone_bgr16_to_bgr8(
|
|
const uint16_t *in_bgr16,
|
|
uint8_t *out_bgr8,
|
|
int pixels,
|
|
float black,
|
|
float scale_norm,
|
|
float gain_b,
|
|
float gain_g,
|
|
float gain_r,
|
|
const float ccm_rgb[9],
|
|
const uint8_t lut[4096],
|
|
float lut_max
|
|
) {
|
|
if (!in_bgr16 || !out_bgr8 || !ccm_rgb || !lut || pixels <= 0) {
|
|
return;
|
|
}
|
|
const float m00 = ccm_rgb[0], m01 = ccm_rgb[1], m02 = ccm_rgb[2];
|
|
const float m10 = ccm_rgb[3], m11 = ccm_rgb[4], m12 = ccm_rgb[5];
|
|
const float m20 = ccm_rgb[6], m21 = ccm_rgb[7], m22 = ccm_rgb[8];
|
|
const float sb = scale_norm * gain_b;
|
|
const float sg = scale_norm * gain_g;
|
|
const float sr = scale_norm * gain_r;
|
|
const float inv_lut = 4095.0f / (lut_max > 1e-6f ? lut_max : 1.0f);
|
|
|
|
#ifdef _OPENMP
|
|
#pragma omp parallel for schedule(static) num_threads(4) if (pixels >= 16384)
|
|
#endif
|
|
for (int i = 0; i < pixels; i++) {
|
|
const size_t idx = (size_t)i * 3;
|
|
float b = (float)in_bgr16[idx + 0] - black;
|
|
float g = (float)in_bgr16[idx + 1] - black;
|
|
float r = (float)in_bgr16[idx + 2] - black;
|
|
if (b < 0.0f) b = 0.0f;
|
|
if (g < 0.0f) g = 0.0f;
|
|
if (r < 0.0f) r = 0.0f;
|
|
|
|
b *= sb;
|
|
g *= sg;
|
|
r *= sr;
|
|
|
|
float rc = m00 * r + m01 * g + m02 * b;
|
|
float gc = m10 * r + m11 * g + m12 * b;
|
|
float bc = m20 * r + m21 * g + m22 * b;
|
|
|
|
int ir = (int)(rc * inv_lut + 0.5f);
|
|
int ig = (int)(gc * inv_lut + 0.5f);
|
|
int ib = (int)(bc * inv_lut + 0.5f);
|
|
if (ir < 0) ir = 0; else if (ir > 4095) ir = 4095;
|
|
if (ig < 0) ig = 0; else if (ig > 4095) ig = 4095;
|
|
if (ib < 0) ib = 0; else if (ib > 4095) ib = 4095;
|
|
|
|
out_bgr8[idx + 0] = lut[ib];
|
|
out_bgr8[idx + 1] = lut[ig];
|
|
out_bgr8[idx + 2] = lut[ir];
|
|
}
|
|
}
|
|
|
|
/* Motion-guarded multi-frame temporal merge in linear 16-bit BGR space. */
|
|
void burst_merge_bgr16(
|
|
const uint16_t *base,
|
|
const uint16_t *const *others,
|
|
int num_others,
|
|
uint16_t *out,
|
|
int pixels,
|
|
float noise_thresh
|
|
) {
|
|
if (!base || !out || pixels <= 0) {
|
|
return;
|
|
}
|
|
const float inv_nt = 1.0f / (noise_thresh > 1.0f ? noise_thresh : 1.0f);
|
|
|
|
#ifdef _OPENMP
|
|
#pragma omp parallel for schedule(static) num_threads(4) if (pixels >= 16384)
|
|
#endif
|
|
for (int i = 0; i < pixels; i++) {
|
|
const size_t idx = (size_t)i * 3;
|
|
const float b0 = (float)base[idx + 0];
|
|
const float g0 = (float)base[idx + 1];
|
|
const float r0 = (float)base[idx + 2];
|
|
const float l0 = 0.114f * b0 + 0.587f * g0 + 0.299f * r0;
|
|
|
|
float acc_b = b0;
|
|
float acc_g = g0;
|
|
float acc_r = r0;
|
|
float w_sum = 1.0f;
|
|
|
|
for (int k = 0; k < num_others; k++) {
|
|
const uint16_t *oth = others[k];
|
|
if (!oth) continue;
|
|
const float bk = (float)oth[idx + 0];
|
|
const float gk = (float)oth[idx + 1];
|
|
const float rk = (float)oth[idx + 2];
|
|
const float lk = 0.114f * bk + 0.587f * gk + 0.299f * rk;
|
|
const float d = fabsf(lk - l0) * inv_nt;
|
|
const float d2 = d * d;
|
|
const float w = 1.0f / (1.0f + d2 * d2);
|
|
acc_b += w * bk;
|
|
acc_g += w * gk;
|
|
acc_r += w * rk;
|
|
w_sum += w;
|
|
}
|
|
|
|
const float inv_w = 1.0f / w_sum;
|
|
int ob = (int)(acc_b * inv_w + 0.5f);
|
|
int og = (int)(acc_g * inv_w + 0.5f);
|
|
int or_v = (int)(acc_r * inv_w + 0.5f);
|
|
if (ob < 0) ob = 0; else if (ob > 65535) ob = 65535;
|
|
if (og < 0) og = 0; else if (og > 65535) og = 65535;
|
|
if (or_v < 0) or_v = 0; else if (or_v > 65535) or_v = 65535;
|
|
out[idx + 0] = (uint16_t)ob;
|
|
out[idx + 1] = (uint16_t)og;
|
|
out[idx + 2] = (uint16_t)or_v;
|
|
}
|
|
}
|