Camera: C ISP pipeline, contrast autofocus, and VFR video
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
This commit is contained in:
nandi committed 2026-11-28 19:25:43 -08:00
1 parent 851c39d827
commit 52b42890d6
34 files changed
+2851 -364

No files matched your search

+147 -22
View File
@@ -63,19 +63,36 @@ typedef struct {
/* Shared latest frame from worker (CAIRO_FORMAT_RGB24 BGRA32) */
GMutex frame_mutex;
uint8_t *frame_buf;
size_t frame_cap;
int frame_w;
int frame_h;
size_t frame_len;
uint64_t frame_seq;
gboolean draw_pending;
/* Paint copy owned exclusively by GTK main thread */
/* Paint & reader buffers swapped under frame_mutex (zero per-frame alloc/memcpy) */
uint8_t *paint_buf;
size_t paint_cap;
int paint_w;
int paint_h;
size_t paint_len;
uint64_t paint_seq;
uint8_t *read_buf;
size_t read_cap;
/* Viewfinder draw rect & tap-to-focus reticle */
double view_rx;
double view_ry;
double view_rw;
double view_rh;
gboolean has_af;
gboolean af_active;
gboolean af_locked;
double af_nx;
double af_ny;
int64_t af_until_us;
/* Camera & UI state */
int cam_count;
gboolean has_flash;
@@ -366,18 +383,19 @@ static gboolean draw_viewfinder(GtkWidget *widget, cairo_t *cr, gpointer data) {
cairo_set_source_rgb(cr, 0.043, 0.051, 0.078);
cairo_paint(cr);
/* Copy latest frame buffer without holding frame_mutex during Cairo paint. */
/* Swap latest frame pointer without holding frame_mutex during Cairo paint. */
g_mutex_lock(&app->frame_mutex);
if (app->frame_buf && app->frame_seq != app->paint_seq) {
uint8_t *nb = (uint8_t *)realloc(app->paint_buf, app->frame_len);
if (nb) {
app->paint_buf = nb;
memcpy(app->paint_buf, app->frame_buf, app->frame_len);
app->paint_w = app->frame_w;
app->paint_h = app->frame_h;
app->paint_len = app->frame_len;
app->paint_seq = app->frame_seq;
}
uint8_t *tmp_buf = app->paint_buf;
size_t tmp_cap = app->paint_cap;
app->paint_buf = app->frame_buf;
app->paint_cap = app->frame_cap;
app->paint_w = app->frame_w;
app->paint_h = app->frame_h;
app->paint_len = app->frame_len;
app->paint_seq = app->frame_seq;
app->frame_buf = tmp_buf;
app->frame_cap = tmp_cap;
}
g_mutex_unlock(&app->frame_mutex);
@@ -399,6 +417,10 @@ static gboolean draw_viewfinder(GtkWidget *widget, cairo_t *cr, gpointer data) {
ry = y;
rw = dw;
rh = dh;
app->view_rx = rx;
app->view_ry = ry;
app->view_rw = rw;
app->view_rh = rh;
cairo_surface_t *surf = cairo_image_surface_create_for_data(
app->paint_buf, CAIRO_FORMAT_RGB24, iw, ih, iw * 4);
@@ -441,6 +463,20 @@ static gboolean draw_viewfinder(GtkWidget *widget, cairo_t *cr, gpointer data) {
cairo_stroke(cr);
}
if (app->af_active && g_get_monotonic_time() <= app->af_until_us) {
double fx = rx + app->af_nx * rw;
double fy = ry + app->af_ny * rh;
double sz = app->af_locked ? 46.0 : 54.0;
rounded_rect_path(cr, fx - sz / 2.0, fy - sz / 2.0, sz, sz, 10.0);
if (app->af_locked) {
cairo_set_source_rgba(cr, 0.20, 0.83, 0.60, 0.92);
} else {
cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 0.88);
}
cairo_set_line_width(cr, 2.0);
cairo_stroke(cr);
}
if (app->countdown_left > 0) {
char buf[16];
snprintf(buf, sizeof(buf), "%d", app->countdown_left);
@@ -685,6 +721,27 @@ static gboolean handle_worker_line_idle(gpointer data) {
gtk_label_set_text(GTK_LABEL(app->rec_label), "● 00:00");
}
sync_ui_labels(app);
} else if (strncmp(line, "af_caps ", 8) == 0) {
app->has_af = (atoi(line + 8) != 0);
if (!app->has_af) {
app->af_active = FALSE;
gtk_widget_queue_draw(app->view_area);
}
} else if (strncmp(line, "af_state ", 9) == 0) {
char st[32] = "";
double nx = 0.5, ny = 0.5;
if (sscanf(line + 9, "%31s %lf %lf", st, &nx, &ny) >= 1) {
if (strcmp(st, "idle") == 0) {
app->af_active = FALSE;
} else {
app->af_nx = fmax(0.05, fmin(0.95, nx));
app->af_ny = fmax(0.05, fmin(0.95, ny));
app->af_locked = (strcmp(st, "locked") == 0);
app->af_active = TRUE;
app->af_until_us = g_get_monotonic_time() + (app->af_locked ? 1200000LL : 4000000LL);
}
gtk_widget_queue_draw(app->view_area);
}
} else if (strncmp(line, "saved_photo ", 12) == 0) {
gchar *base = g_path_get_basename(line + 12);
gchar *msg = g_strdup_printf("Saved %s", base);
@@ -694,15 +751,41 @@ static gboolean handle_worker_line_idle(gpointer data) {
refresh_thumbnail(app);
} else if (strncmp(line, "saved_video ", 12) == 0) {
int frames = 0;
int dur_ms = 0;
const char *p = line + 12;
const char *sp = strchr(p, ' ');
if (sp) {
frames = atoi(p);
gchar *base = g_path_get_basename(sp + 1);
gchar *msg = g_strdup_printf("Saved %s (%d frames)", base, frames);
show_toast(app, msg, 2.8);
g_free(base);
g_free(msg);
const char *rest = sp + 1;
const char *sp2 = strchr(rest, ' ');
gboolean has_dur = FALSE;
if (sp2 && isdigit((unsigned char)rest[0])) {
has_dur = TRUE;
for (const char *c = rest; c < sp2; c++) {
if (!isdigit((unsigned char)*c)) {
has_dur = FALSE;
break;
}
}
}
if (has_dur) {
dur_ms = atoi(rest);
gchar *base = g_path_get_basename(sp2 + 1);
double secs = dur_ms > 0 ? (dur_ms / 1000.0) : 0.0;
double fps = (secs > 0.05 && frames > 0) ? (frames / secs) : 0.0;
gchar *msg = (fps > 0.5)
? g_strdup_printf("Saved %s (%.1fs · %.0f fps)", base, secs, fps)
: g_strdup_printf("Saved %s (%d frames)", base, frames);
show_toast(app, msg, 2.8);
g_free(base);
g_free(msg);
} else {
gchar *base = g_path_get_basename(rest);
gchar *msg = g_strdup_printf("Saved %s (%d frames)", base, frames);
show_toast(app, msg, 2.8);
g_free(base);
g_free(msg);
}
}
} else if (strncmp(line, "toast ", 6) == 0) {
show_toast(app, line + 6, 3.0);
@@ -723,25 +806,32 @@ static gpointer worker_reader_func(gpointer data) {
if (sscanf(line + 6, "%d %d %lu", &w, &h, &len) == 3 &&
w > 0 && h > 0 && len == (unsigned long)w * (unsigned long)h * 4UL &&
len <= 16UL * 1024UL * 1024UL) {
uint8_t *buf = (uint8_t *)malloc((size_t)len);
if (!buf) break;
if ((size_t)len > app->read_cap) {
uint8_t *nb = (uint8_t *)realloc(app->read_buf, (size_t)len);
if (!nb) break;
app->read_buf = nb;
app->read_cap = (size_t)len;
}
size_t got = 0;
while (got < (size_t)len) {
size_t n = fread(buf + got, 1, (size_t)len - got, app->from_worker);
size_t n = fread(app->read_buf + got, 1, (size_t)len - got, app->from_worker);
if (n == 0) break;
got += n;
}
if (got != (size_t)len) {
free(buf);
break;
}
g_mutex_lock(&app->frame_mutex);
free(app->frame_buf);
app->frame_buf = buf;
uint8_t *tmp_buf = app->frame_buf;
size_t tmp_cap = app->frame_cap;
app->frame_buf = app->read_buf;
app->frame_cap = app->read_cap;
app->frame_w = w;
app->frame_h = h;
app->frame_len = (size_t)len;
app->frame_seq++;
app->read_buf = tmp_buf;
app->read_cap = tmp_cap;
gboolean need_schedule = !app->draw_pending;
if (need_schedule) app->draw_pending = TRUE;
g_mutex_unlock(&app->frame_mutex);
@@ -774,6 +864,10 @@ static gboolean tick_ui(gpointer data) {
app->toast_text = NULL;
dirty = TRUE;
}
if (app->af_active && now > app->af_until_us) {
app->af_active = FALSE;
dirty = TRUE;
}
if (app->is_recording) {
int secs = (int)((now - app->rec_start_us) / 1000000LL);
if (secs < 0) secs = 0;
@@ -1100,10 +1194,38 @@ static gboolean start_worker_process(CameraApp *app) {
if (!app->to_worker || !app->from_worker) {
return FALSE;
}
setvbuf(app->from_worker, NULL, _IOFBF, 256 * 1024);
app->reader_thread = g_thread_new("cam-reader", worker_reader_func, app);
return TRUE;
}
static gboolean on_view_button_press(GtkWidget *w, GdkEventButton *ev, gpointer data) {
(void)w;
CameraApp *app = (CameraApp *)data;
if (ev->type != GDK_BUTTON_PRESS || ev->button != 1) return FALSE;
if (!app->has_af || app->view_rw <= 20.0 || app->view_rh <= 20.0) return FALSE;
if (ev->x < app->view_rx || ev->x > app->view_rx + app->view_rw ||
ev->y < app->view_ry || ev->y > app->view_ry + app->view_rh) {
return FALSE;
}
double nx = (ev->x - app->view_rx) / app->view_rw;
double ny = (ev->y - app->view_ry) / app->view_rh;
if (app->active_is_front) nx = 1.0 - nx;
nx = fmax(0.1, fmin(0.9, nx));
ny = fmax(0.1, fmin(0.9, ny));
char cmd[64];
snprintf(cmd, sizeof(cmd), "focus %.3f %.3f", nx, ny);
send_worker(app, cmd);
app->af_nx = app->active_is_front ? (1.0 - nx) : nx;
app->af_ny = ny;
app->af_locked = FALSE;
app->af_active = TRUE;
app->af_until_us = g_get_monotonic_time() + 4000000LL;
gtk_widget_queue_draw(app->view_area);
return TRUE;
}
static void build_ui(CameraApp *app) {
GtkSettings *settings = gtk_settings_get_default();
if (settings) {
@@ -1165,7 +1287,9 @@ static void build_ui(CameraApp *app) {
app->view_area = gtk_drawing_area_new();
gtk_widget_set_hexpand(app->view_area, TRUE);
gtk_widget_set_vexpand(app->view_area, TRUE);
gtk_widget_add_events(app->view_area, GDK_BUTTON_PRESS_MASK);
g_signal_connect(app->view_area, "draw", G_CALLBACK(draw_viewfinder), app);
g_signal_connect(app->view_area, "button-press-event", G_CALLBACK(on_view_button_press), app);
gtk_container_add(GTK_CONTAINER(overlay), app->view_area);
GtkWidget *top_bar = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 8);
@@ -1409,6 +1533,7 @@ int main(int argc, char **argv) {
free(app.frame_buf);
free(app.paint_buf);
free(app.read_buf);
g_free(app.worker_exe);
g_free(app.initial_camera);
g_free(app.active_label);