2026-11-22 10:49:59 +02:00
|
|
|
#define _POSIX_C_SOURCE 200809L
|
|
|
|
|
#include "gemui.h"
|
2026-11-23 06:38:06 +02:00
|
|
|
#include "geminux-cam-bayer.h"
|
2026-11-22 10:49:59 +02:00
|
|
|
|
|
|
|
|
#include <dirent.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <fcntl.h>
|
2026-11-23 06:38:06 +02:00
|
|
|
#include <linux/v4l2-subdev.h>
|
2026-11-22 10:49:59 +02:00
|
|
|
#include <linux/videodev2.h>
|
2026-11-23 06:38:06 +02:00
|
|
|
#include <math.h>
|
|
|
|
|
#include <poll.h>
|
2026-11-22 10:49:59 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <jpeglib.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
#include <sys/mman.h>
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
#include <time.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
|
|
#define HIT_SHUTTER 1
|
|
|
|
|
#define HIT_RECORD 2
|
|
|
|
|
#define HIT_SWITCH 3
|
2026-11-23 06:38:06 +02:00
|
|
|
#define HIT_ROTATE 4
|
2026-11-22 10:49:59 +02:00
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
void *start;
|
|
|
|
|
size_t length;
|
|
|
|
|
} v4l2_buf_t;
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
uint32_t offset;
|
|
|
|
|
uint32_t size;
|
|
|
|
|
} avi_idx_t;
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
gemui_app_t *app;
|
|
|
|
|
int vfd;
|
2026-11-23 06:38:06 +02:00
|
|
|
int subdev_fd;
|
2026-11-22 10:49:59 +02:00
|
|
|
char dev_path[64];
|
|
|
|
|
char dev_list[8][64];
|
|
|
|
|
int num_devs;
|
|
|
|
|
int cur_dev;
|
|
|
|
|
uint32_t pixfmt;
|
2026-11-23 06:38:06 +02:00
|
|
|
int raw_w;
|
|
|
|
|
int raw_h;
|
|
|
|
|
int raw_stride;
|
2026-11-22 10:49:59 +02:00
|
|
|
int cam_w;
|
|
|
|
|
int cam_h;
|
2026-11-23 06:38:06 +02:00
|
|
|
int rot_deg;
|
|
|
|
|
bool mirror;
|
|
|
|
|
uint16_t *raw16;
|
|
|
|
|
geminux_bayer10_isp_t isp;
|
|
|
|
|
uint32_t frame_count;
|
|
|
|
|
int ae_exp;
|
|
|
|
|
int ae_again;
|
|
|
|
|
int ae_dgain;
|
2026-11-22 10:49:59 +02:00
|
|
|
v4l2_buf_t bufs[4];
|
|
|
|
|
int num_bufs;
|
|
|
|
|
uint8_t *rgb24;
|
|
|
|
|
uint32_t *argb32;
|
|
|
|
|
cairo_surface_t *frame_surf;
|
|
|
|
|
bool recording;
|
|
|
|
|
FILE *avi_fp;
|
|
|
|
|
uint32_t avi_frames;
|
|
|
|
|
long avi_movi_pos;
|
|
|
|
|
avi_idx_t *avi_idx;
|
|
|
|
|
int avi_idx_cap;
|
|
|
|
|
char last_file[128];
|
|
|
|
|
} cam_state_t;
|
|
|
|
|
|
|
|
|
|
static uint8_t clamp_u8(int v) {
|
|
|
|
|
return (uint8_t)(v < 0 ? 0 : (v > 255 ? 255 : v));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void yuyv_to_buffers(const uint8_t *src, int w, int h, uint8_t *rgb, uint32_t *argb) {
|
|
|
|
|
int pairs = (w * h) / 2;
|
|
|
|
|
for (int i = 0; i < pairs; i++) {
|
|
|
|
|
int y0 = src[4 * i + 0] - 16;
|
|
|
|
|
int u = src[4 * i + 1] - 128;
|
|
|
|
|
int y1 = src[4 * i + 2] - 16;
|
|
|
|
|
int v = src[4 * i + 3] - 128;
|
|
|
|
|
|
|
|
|
|
int c0 = 298 * y0;
|
|
|
|
|
int c1 = 298 * y1;
|
|
|
|
|
int r0 = clamp_u8((c0 + 409 * v + 128) >> 8);
|
|
|
|
|
int g0 = clamp_u8((c0 - 100 * u - 208 * v + 128) >> 8);
|
|
|
|
|
int b0 = clamp_u8((c0 + 516 * u + 128) >> 8);
|
|
|
|
|
int r1 = clamp_u8((c1 + 409 * v + 128) >> 8);
|
|
|
|
|
int g1 = clamp_u8((c1 - 100 * u - 208 * v + 128) >> 8);
|
|
|
|
|
int b1 = clamp_u8((c1 + 516 * u + 128) >> 8);
|
|
|
|
|
|
|
|
|
|
rgb[6 * i + 0] = (uint8_t)r0;
|
|
|
|
|
rgb[6 * i + 1] = (uint8_t)g0;
|
|
|
|
|
rgb[6 * i + 2] = (uint8_t)b0;
|
|
|
|
|
rgb[6 * i + 3] = (uint8_t)r1;
|
|
|
|
|
rgb[6 * i + 4] = (uint8_t)g1;
|
|
|
|
|
rgb[6 * i + 5] = (uint8_t)b1;
|
|
|
|
|
|
|
|
|
|
argb[2 * i + 0] = 0xff000000U | ((uint32_t)r0 << 16) | ((uint32_t)g0 << 8) | (uint32_t)b0;
|
|
|
|
|
argb[2 * i + 1] = 0xff000000U | ((uint32_t)r1 << 16) | ((uint32_t)g1 << 8) | (uint32_t)b1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool encode_rgb24_jpeg_mem(const uint8_t *rgb, int w, int h, int quality,
|
|
|
|
|
unsigned char **out_buf, unsigned long *out_sz) {
|
|
|
|
|
struct jpeg_compress_struct cinfo;
|
|
|
|
|
struct jpeg_error_mgr jerr;
|
|
|
|
|
cinfo.err = jpeg_std_error(&jerr);
|
|
|
|
|
jpeg_create_compress(&cinfo);
|
|
|
|
|
*out_buf = NULL;
|
|
|
|
|
*out_sz = 0;
|
|
|
|
|
jpeg_mem_dest(&cinfo, out_buf, out_sz);
|
|
|
|
|
cinfo.image_width = (JDIMENSION)w;
|
|
|
|
|
cinfo.image_height = (JDIMENSION)h;
|
|
|
|
|
cinfo.input_components = 3;
|
|
|
|
|
cinfo.in_color_space = JCS_RGB;
|
|
|
|
|
jpeg_set_defaults(&cinfo);
|
|
|
|
|
jpeg_set_quality(&cinfo, quality, TRUE);
|
|
|
|
|
jpeg_start_compress(&cinfo, TRUE);
|
|
|
|
|
while (cinfo.next_scanline < cinfo.image_height) {
|
|
|
|
|
JSAMPROW row[1] = {(JSAMPROW)(rgb + (size_t)cinfo.next_scanline * (size_t)w * 3U)};
|
|
|
|
|
jpeg_write_scanlines(&cinfo, row, 1);
|
|
|
|
|
}
|
|
|
|
|
jpeg_finish_compress(&cinfo);
|
|
|
|
|
jpeg_destroy_compress(&cinfo);
|
|
|
|
|
return *out_buf != NULL && *out_sz > 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void put_le32(FILE *f, uint32_t v) {
|
|
|
|
|
uint8_t b[4] = {(uint8_t)v, (uint8_t)(v >> 8), (uint8_t)(v >> 16), (uint8_t)(v >> 24)};
|
|
|
|
|
fwrite(b, 1, 4, f);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void put_le16(FILE *f, uint16_t v) {
|
|
|
|
|
uint8_t b[2] = {(uint8_t)v, (uint8_t)(v >> 8)};
|
|
|
|
|
fwrite(b, 1, 2, f);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void avi_stop(cam_state_t *st) {
|
|
|
|
|
if (!st->recording || !st->avi_fp) return;
|
|
|
|
|
FILE *f = st->avi_fp;
|
|
|
|
|
long end_movi = ftell(f);
|
|
|
|
|
uint32_t movi_sz = (uint32_t)(end_movi - st->avi_movi_pos - 4);
|
|
|
|
|
|
|
|
|
|
fwrite("idx1", 1, 4, f);
|
|
|
|
|
put_le32(f, st->avi_frames * 16U);
|
|
|
|
|
for (uint32_t i = 0; i < st->avi_frames; i++) {
|
|
|
|
|
fwrite("00dc", 1, 4, f);
|
|
|
|
|
put_le32(f, 0x10U);
|
|
|
|
|
put_le32(f, st->avi_idx[i].offset);
|
|
|
|
|
put_le32(f, st->avi_idx[i].size);
|
|
|
|
|
}
|
|
|
|
|
long file_end = ftell(f);
|
|
|
|
|
|
|
|
|
|
fseek(f, 4, SEEK_SET);
|
|
|
|
|
put_le32(f, (uint32_t)(file_end - 8));
|
|
|
|
|
fseek(f, 48, SEEK_SET);
|
|
|
|
|
put_le32(f, st->avi_frames);
|
|
|
|
|
fseek(f, 140, SEEK_SET);
|
|
|
|
|
put_le32(f, st->avi_frames);
|
|
|
|
|
fseek(f, st->avi_movi_pos, SEEK_SET);
|
|
|
|
|
put_le32(f, movi_sz);
|
|
|
|
|
|
|
|
|
|
fclose(f);
|
|
|
|
|
st->avi_fp = NULL;
|
|
|
|
|
st->recording = false;
|
|
|
|
|
free(st->avi_idx);
|
|
|
|
|
st->avi_idx = NULL;
|
|
|
|
|
st->avi_idx_cap = 0;
|
2026-11-23 06:38:06 +02:00
|
|
|
if (st->app) gemui_show_toast(st->app, "Saved MJPEG AVI video", 2000);
|
2026-11-22 10:49:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void avi_start(cam_state_t *st) {
|
|
|
|
|
if (!st->rgb24 || st->cam_w <= 0 || st->cam_h <= 0) {
|
2026-11-23 06:38:06 +02:00
|
|
|
if (st->app) gemui_show_toast(st->app, "No active camera stream", 1800);
|
2026-11-22 10:49:59 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const char *home = getenv("HOME");
|
|
|
|
|
char dir[512], path[576], ts[32];
|
|
|
|
|
snprintf(dir, sizeof(dir), "%s/Videos", (home && home[0]) ? home : "/home/user");
|
|
|
|
|
const char *mk[] = {"mkdir", "-p", dir, NULL};
|
|
|
|
|
gemui_run_cmd_capture(mk, NULL, 0);
|
|
|
|
|
time_t now = time(NULL);
|
|
|
|
|
struct tm tm;
|
|
|
|
|
localtime_r(&now, &tm);
|
|
|
|
|
strftime(ts, sizeof(ts), "%Y%m%d_%H%M%S", &tm);
|
|
|
|
|
snprintf(path, sizeof(path), "%s/VID_%s.avi", dir, ts);
|
|
|
|
|
|
|
|
|
|
FILE *f = fopen(path, "wb+");
|
|
|
|
|
if (!f) return;
|
|
|
|
|
|
|
|
|
|
fwrite("RIFF", 1, 4, f); put_le32(f, 0); fwrite("AVI ", 1, 4, f);
|
|
|
|
|
fwrite("LIST", 1, 4, f); put_le32(f, 192); fwrite("hdrl", 1, 4, f);
|
|
|
|
|
fwrite("avih", 1, 4, f); put_le32(f, 56);
|
|
|
|
|
put_le32(f, 66667); put_le32(f, 0); put_le32(f, 0); put_le32(f, 0x10U);
|
|
|
|
|
put_le32(f, 0); put_le32(f, 0); put_le32(f, 1); put_le32(f, 0);
|
|
|
|
|
put_le32(f, (uint32_t)st->cam_w); put_le32(f, (uint32_t)st->cam_h);
|
|
|
|
|
for (int i = 0; i < 4; i++) put_le32(f, 0);
|
|
|
|
|
|
|
|
|
|
fwrite("LIST", 1, 4, f); put_le32(f, 116); fwrite("strl", 1, 4, f);
|
|
|
|
|
fwrite("strh", 1, 4, f); put_le32(f, 56);
|
|
|
|
|
fwrite("vids", 1, 4, f); fwrite("MJPG", 1, 4, f);
|
|
|
|
|
put_le32(f, 0); put_le32(f, 0); put_le32(f, 0);
|
|
|
|
|
put_le32(f, 1); put_le32(f, 15); put_le32(f, 0);
|
|
|
|
|
put_le32(f, 0); put_le32(f, 0); put_le32(f, 0);
|
|
|
|
|
put_le16(f, 0); put_le16(f, 0);
|
|
|
|
|
put_le16(f, (uint16_t)st->cam_w); put_le16(f, (uint16_t)st->cam_h);
|
|
|
|
|
|
|
|
|
|
fwrite("strf", 1, 4, f); put_le32(f, 40);
|
|
|
|
|
put_le32(f, 40); put_le32(f, (uint32_t)st->cam_w); put_le32(f, (uint32_t)st->cam_h);
|
|
|
|
|
put_le16(f, 1); put_le16(f, 24); fwrite("MJPG", 1, 4, f);
|
|
|
|
|
put_le32(f, (uint32_t)(st->cam_w * st->cam_h * 3));
|
|
|
|
|
for (int i = 0; i < 4; i++) put_le32(f, 0);
|
|
|
|
|
|
|
|
|
|
fwrite("LIST", 1, 4, f);
|
|
|
|
|
st->avi_movi_pos = ftell(f);
|
|
|
|
|
put_le32(f, 0);
|
|
|
|
|
fwrite("movi", 1, 4, f);
|
|
|
|
|
|
|
|
|
|
st->avi_fp = f;
|
|
|
|
|
st->avi_frames = 0;
|
|
|
|
|
st->recording = true;
|
|
|
|
|
snprintf(st->last_file, sizeof(st->last_file), "VID_%s.avi", ts);
|
2026-11-23 06:38:06 +02:00
|
|
|
if (st->app) gemui_show_toast(st->app, "Recording MJPEG AVI video...", 1500);
|
2026-11-22 10:49:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void avi_write_frame(cam_state_t *st, const uint8_t *jpg, size_t jpg_sz) {
|
|
|
|
|
if (!st->recording || !st->avi_fp || !jpg || !jpg_sz) return;
|
|
|
|
|
if ((int)st->avi_frames >= st->avi_idx_cap) {
|
|
|
|
|
int ncap = st->avi_idx_cap ? st->avi_idx_cap * 2 : 512;
|
|
|
|
|
avi_idx_t *nptr = realloc(st->avi_idx, (size_t)ncap * sizeof(avi_idx_t));
|
|
|
|
|
if (!nptr) return;
|
|
|
|
|
st->avi_idx = nptr;
|
|
|
|
|
st->avi_idx_cap = ncap;
|
|
|
|
|
}
|
|
|
|
|
long chunk_off = ftell(st->avi_fp) - (st->avi_movi_pos + 4);
|
|
|
|
|
fwrite("00dc", 1, 4, st->avi_fp);
|
|
|
|
|
put_le32(st->avi_fp, (uint32_t)jpg_sz);
|
|
|
|
|
fwrite(jpg, 1, jpg_sz, st->avi_fp);
|
|
|
|
|
if (jpg_sz & 1U) fputc(0, st->avi_fp);
|
|
|
|
|
|
|
|
|
|
st->avi_idx[st->avi_frames].offset = (uint32_t)chunk_off;
|
|
|
|
|
st->avi_idx[st->avi_frames].size = (uint32_t)jpg_sz;
|
|
|
|
|
st->avi_frames++;
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-23 06:38:06 +02:00
|
|
|
static void load_cam_config(cam_state_t *st) {
|
|
|
|
|
const char *home = getenv("HOME");
|
|
|
|
|
char path[512];
|
|
|
|
|
snprintf(path, sizeof(path), "%s/.config/geminux-camera.conf",
|
|
|
|
|
(home && home[0]) ? home : "/home/user");
|
|
|
|
|
FILE *f = fopen(path, "r");
|
|
|
|
|
if (!f) return;
|
|
|
|
|
char line[128];
|
|
|
|
|
while (fgets(line, sizeof(line), f)) {
|
|
|
|
|
int rot = 0;
|
|
|
|
|
if (sscanf(line, "rot_deg=%d", &rot) == 1 &&
|
|
|
|
|
(rot == 0 || rot == 90 || rot == 180 || rot == 270)) {
|
|
|
|
|
st->rot_deg = rot;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
fclose(f);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void save_cam_config(const cam_state_t *st) {
|
|
|
|
|
const char *home = getenv("HOME");
|
|
|
|
|
char dir[512], path[560];
|
|
|
|
|
snprintf(dir, sizeof(dir), "%s/.config", (home && home[0]) ? home : "/home/user");
|
|
|
|
|
mkdir(dir, 0755);
|
|
|
|
|
snprintf(path, sizeof(path), "%s/geminux-camera.conf", dir);
|
|
|
|
|
FILE *f = fopen(path, "w");
|
|
|
|
|
if (!f) return;
|
|
|
|
|
fprintf(f, "rot_deg=%d\n", st->rot_deg);
|
|
|
|
|
fclose(f);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool alloc_output_buffers(cam_state_t *st, bool with_surface) {
|
|
|
|
|
bool swap_dims = (st->pixfmt == V4L2_PIX_FMT_MTISP_SRGGB10 ||
|
|
|
|
|
st->pixfmt == V4L2_PIX_FMT_MTISP_SGRBG10) &&
|
|
|
|
|
(st->rot_deg == 90 || st->rot_deg == 270);
|
|
|
|
|
int ow = swap_dims ? st->raw_h : st->raw_w;
|
|
|
|
|
int oh = swap_dims ? st->raw_w : st->raw_h;
|
|
|
|
|
|
|
|
|
|
if (st->frame_surf) {
|
|
|
|
|
cairo_surface_destroy(st->frame_surf);
|
|
|
|
|
st->frame_surf = NULL;
|
|
|
|
|
}
|
|
|
|
|
free(st->rgb24); st->rgb24 = NULL;
|
|
|
|
|
free(st->argb32); st->argb32 = NULL;
|
|
|
|
|
|
|
|
|
|
if (ow <= 0 || oh <= 0) return false;
|
|
|
|
|
st->cam_w = ow;
|
|
|
|
|
st->cam_h = oh;
|
|
|
|
|
st->rgb24 = calloc((size_t)ow * (size_t)oh, 3);
|
|
|
|
|
if (with_surface)
|
|
|
|
|
st->argb32 = calloc((size_t)ow * (size_t)oh, sizeof(uint32_t));
|
|
|
|
|
if (!st->rgb24 || (with_surface && !st->argb32)) {
|
|
|
|
|
free(st->rgb24); st->rgb24 = NULL;
|
|
|
|
|
free(st->argb32); st->argb32 = NULL;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (with_surface) {
|
|
|
|
|
st->frame_surf = cairo_image_surface_create_for_data(
|
|
|
|
|
(unsigned char *)st->argb32, CAIRO_FORMAT_RGB24, ow, oh, ow * 4);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-22 10:49:59 +02:00
|
|
|
static void close_camera(cam_state_t *st) {
|
|
|
|
|
if (st->recording) avi_stop(st);
|
|
|
|
|
if (st->vfd >= 0) {
|
|
|
|
|
enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
|
|
|
|
ioctl(st->vfd, VIDIOC_STREAMOFF, &type);
|
2026-11-23 06:38:06 +02:00
|
|
|
if (st->app) gemui_del_fd(st->app, st->vfd);
|
2026-11-22 10:49:59 +02:00
|
|
|
for (int i = 0; i < st->num_bufs; i++) {
|
|
|
|
|
if (st->bufs[i].start && st->bufs[i].start != MAP_FAILED) {
|
|
|
|
|
munmap(st->bufs[i].start, st->bufs[i].length);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
close(st->vfd);
|
|
|
|
|
st->vfd = -1;
|
|
|
|
|
}
|
2026-11-23 06:38:06 +02:00
|
|
|
if (st->subdev_fd >= 0) {
|
|
|
|
|
close(st->subdev_fd);
|
|
|
|
|
st->subdev_fd = -1;
|
|
|
|
|
}
|
2026-11-22 10:49:59 +02:00
|
|
|
st->num_bufs = 0;
|
|
|
|
|
if (st->frame_surf) {
|
|
|
|
|
cairo_surface_destroy(st->frame_surf);
|
|
|
|
|
st->frame_surf = NULL;
|
|
|
|
|
}
|
2026-11-23 06:38:06 +02:00
|
|
|
free(st->raw16); st->raw16 = NULL;
|
2026-11-22 10:49:59 +02:00
|
|
|
free(st->rgb24); st->rgb24 = NULL;
|
|
|
|
|
free(st->argb32); st->argb32 = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-23 06:38:06 +02:00
|
|
|
static int set_subdev_ctrl(int sfd, uint32_t id, int32_t val) {
|
|
|
|
|
struct v4l2_control ctrl;
|
|
|
|
|
memset(&ctrl, 0, sizeof(ctrl));
|
|
|
|
|
ctrl.id = id;
|
|
|
|
|
ctrl.value = val;
|
|
|
|
|
return ioctl(sfd, VIDIOC_S_CTRL, &ctrl);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void update_ae_agc(cam_state_t *st, int lin_avg) {
|
|
|
|
|
if (st->subdev_fd < 0) return;
|
|
|
|
|
int cur = lin_avg < 6 ? 6 : lin_avg;
|
|
|
|
|
if (cur >= 125 && cur <= 180) return;
|
|
|
|
|
|
|
|
|
|
double ratio = 150.0 / (double)cur;
|
|
|
|
|
ratio = 1.0 + (ratio - 1.0) * 0.5;
|
|
|
|
|
if (ratio < 0.75) ratio = 0.75;
|
|
|
|
|
if (ratio > 1.35) ratio = 1.35;
|
|
|
|
|
|
|
|
|
|
double again_mult = (double)(16 + st->ae_again) / 16.0;
|
|
|
|
|
double dgain_mult = (double)st->ae_dgain / 256.0;
|
|
|
|
|
double ev = (double)st->ae_exp * again_mult * dgain_mult * ratio;
|
|
|
|
|
double min_ev = 16.0;
|
|
|
|
|
double max_ev = 2064.0 * 16.0 * (640.0 / 256.0);
|
|
|
|
|
if (ev < min_ev) ev = min_ev;
|
|
|
|
|
if (ev > max_ev) ev = max_ev;
|
|
|
|
|
|
|
|
|
|
int n_exp, n_again, n_dgain;
|
|
|
|
|
if (ev <= 2064.0) {
|
|
|
|
|
n_exp = (int)lrint(ev);
|
|
|
|
|
n_again = 0;
|
|
|
|
|
n_dgain = 256;
|
|
|
|
|
} else if (ev <= 2064.0 * 16.0) {
|
|
|
|
|
n_exp = 2064;
|
|
|
|
|
n_again = (int)lrint((ev / 2064.0) * 16.0) - 16;
|
|
|
|
|
n_dgain = 256;
|
|
|
|
|
} else {
|
|
|
|
|
n_exp = 2064;
|
|
|
|
|
n_again = 240;
|
|
|
|
|
n_dgain = (int)lrint((ev / (2064.0 * 16.0)) * 256.0);
|
|
|
|
|
}
|
|
|
|
|
if (n_exp < 16) n_exp = 16;
|
|
|
|
|
if (n_exp > 2064) n_exp = 2064;
|
|
|
|
|
if (n_again < 0) n_again = 0;
|
|
|
|
|
if (n_again > 240) n_again = 240;
|
|
|
|
|
if (n_dgain < 256) n_dgain = 256;
|
|
|
|
|
if (n_dgain > 640) n_dgain = 640;
|
|
|
|
|
|
|
|
|
|
if (n_exp != st->ae_exp) {
|
|
|
|
|
set_subdev_ctrl(st->subdev_fd, V4L2_CID_EXPOSURE, n_exp);
|
|
|
|
|
st->ae_exp = n_exp;
|
|
|
|
|
}
|
|
|
|
|
if (n_again != st->ae_again) {
|
|
|
|
|
set_subdev_ctrl(st->subdev_fd, V4L2_CID_ANALOGUE_GAIN, n_again);
|
|
|
|
|
st->ae_again = n_again;
|
|
|
|
|
}
|
|
|
|
|
if (n_dgain != st->ae_dgain) {
|
|
|
|
|
set_subdev_ctrl(st->subdev_fd, V4L2_CID_DIGITAL_GAIN, n_dgain);
|
|
|
|
|
st->ae_dgain = n_dgain;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool find_mc_subdevs(char *sensor_dev, size_t sensor_sz,
|
|
|
|
|
char *bridge_dev, size_t bridge_sz) {
|
|
|
|
|
sensor_dev[0] = '\0';
|
|
|
|
|
bridge_dev[0] = '\0';
|
|
|
|
|
DIR *d = opendir("/sys/class/video4linux");
|
|
|
|
|
if (!d) return false;
|
|
|
|
|
|
|
|
|
|
struct dirent *ent;
|
|
|
|
|
while ((ent = readdir(d)) != NULL) {
|
|
|
|
|
if (strncmp(ent->d_name, "v4l-subdev", 10) != 0) continue;
|
|
|
|
|
char sys_p[128], name[128] = "";
|
|
|
|
|
snprintf(sys_p, sizeof(sys_p), "/sys/class/video4linux/%s/name", ent->d_name);
|
|
|
|
|
FILE *f = fopen(sys_p, "r");
|
|
|
|
|
if (!f) continue;
|
|
|
|
|
if (fgets(name, sizeof(name), f)) {
|
|
|
|
|
size_t nl = strlen(name);
|
|
|
|
|
while (nl > 0 && (name[nl - 1] == '\n' || name[nl - 1] == '\r'))
|
|
|
|
|
name[--nl] = '\0';
|
|
|
|
|
if (strncmp(name, "hi556", 5) == 0 && !sensor_dev[0]) {
|
|
|
|
|
snprintf(sensor_dev, sensor_sz, "/dev/%s", ent->d_name);
|
|
|
|
|
} else if (strstr(name, "seninf") != NULL && !bridge_dev[0]) {
|
|
|
|
|
snprintf(bridge_dev, bridge_sz, "/dev/%s", ent->d_name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
fclose(f);
|
|
|
|
|
}
|
|
|
|
|
closedir(d);
|
|
|
|
|
return sensor_dev[0] != '\0' && bridge_dev[0] != '\0';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Configure sensor and CSI bridge subdevs for V4L2_CAP_IO_MC capture nodes.
|
|
|
|
|
* Returns 1 when MC configured (*out_sensor_fd open), 0 for non-MC devices, -1 on error.
|
|
|
|
|
*/
|
|
|
|
|
static int configure_mc_pipeline(int vfd, struct v4l2_subdev_format *out_fmt,
|
|
|
|
|
int *out_sensor_fd, int *exp_out,
|
|
|
|
|
int *again_out, int *dgain_out) {
|
|
|
|
|
*out_sensor_fd = -1;
|
|
|
|
|
struct v4l2_capability cap;
|
|
|
|
|
memset(&cap, 0, sizeof(cap));
|
|
|
|
|
if (ioctl(vfd, VIDIOC_QUERYCAP, &cap) < 0) return -1;
|
|
|
|
|
|
|
|
|
|
uint32_t eff_caps = cap.device_caps ? cap.device_caps : cap.capabilities;
|
|
|
|
|
bool is_mc = (eff_caps & V4L2_CAP_IO_MC) ||
|
|
|
|
|
(strstr((const char *)cap.driver, "seninf") != NULL);
|
|
|
|
|
if (!is_mc) return 0;
|
|
|
|
|
|
|
|
|
|
char sensor_dev[64], bridge_dev[64];
|
|
|
|
|
if (!find_mc_subdevs(sensor_dev, sizeof(sensor_dev), bridge_dev, sizeof(bridge_dev)))
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
int sfd = open(sensor_dev, O_RDWR | O_CLOEXEC);
|
|
|
|
|
if (sfd < 0) return -1;
|
|
|
|
|
|
|
|
|
|
struct v4l2_subdev_format sfmt;
|
|
|
|
|
memset(&sfmt, 0, sizeof(sfmt));
|
|
|
|
|
sfmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
|
|
|
|
|
sfmt.pad = 0;
|
|
|
|
|
sfmt.format.width = 1296;
|
|
|
|
|
sfmt.format.height = 972;
|
|
|
|
|
sfmt.format.code = MEDIA_BUS_FMT_SRGGB10_1X10;
|
|
|
|
|
sfmt.format.field = V4L2_FIELD_NONE;
|
|
|
|
|
if (ioctl(sfd, VIDIOC_SUBDEV_S_FMT, &sfmt) < 0) {
|
|
|
|
|
close(sfd);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int bfd = open(bridge_dev, O_RDWR | O_CLOEXEC);
|
|
|
|
|
if (bfd < 0) {
|
|
|
|
|
close(sfd);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
struct v4l2_subdev_format bfmt = sfmt;
|
|
|
|
|
bfmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
|
|
|
|
|
bfmt.pad = 0;
|
|
|
|
|
if (ioctl(bfd, VIDIOC_SUBDEV_S_FMT, &bfmt) < 0) {
|
|
|
|
|
close(bfd);
|
|
|
|
|
close(sfd);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
close(bfd);
|
|
|
|
|
|
|
|
|
|
*exp_out = 1000;
|
|
|
|
|
*again_out = 64;
|
|
|
|
|
*dgain_out = 256;
|
|
|
|
|
set_subdev_ctrl(sfd, V4L2_CID_EXPOSURE, *exp_out);
|
|
|
|
|
set_subdev_ctrl(sfd, V4L2_CID_ANALOGUE_GAIN, *again_out);
|
|
|
|
|
set_subdev_ctrl(sfd, V4L2_CID_DIGITAL_GAIN, *dgain_out);
|
|
|
|
|
|
|
|
|
|
*out_fmt = sfmt;
|
|
|
|
|
*out_sensor_fd = sfd;
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool process_captured_frame(cam_state_t *st, const void *data, uint32_t bytesused) {
|
|
|
|
|
const uint8_t *raw = data;
|
|
|
|
|
if (!raw || bytesused == 0) return false;
|
|
|
|
|
|
|
|
|
|
if (st->pixfmt == V4L2_PIX_FMT_MTISP_SRGGB10 ||
|
|
|
|
|
st->pixfmt == V4L2_PIX_FMT_MTISP_SGRBG10) {
|
|
|
|
|
if (bytesused < (uint32_t)((size_t)st->raw_stride * (size_t)st->raw_h))
|
|
|
|
|
return false;
|
|
|
|
|
int lin_avg = 0;
|
|
|
|
|
bool is_rggb = (st->pixfmt == V4L2_PIX_FMT_MTISP_SRGGB10);
|
|
|
|
|
geminux_bayer10_to_buffers(&st->isp, raw, st->raw_stride,
|
|
|
|
|
st->raw_w, st->raw_h, is_rggb,
|
|
|
|
|
st->rot_deg, st->mirror,
|
|
|
|
|
st->raw16, st->rgb24, st->argb32, &lin_avg);
|
|
|
|
|
st->frame_count++;
|
|
|
|
|
if ((st->frame_count % 4U) == 0U && st->subdev_fd >= 0) {
|
|
|
|
|
update_ae_agc(st, lin_avg);
|
|
|
|
|
}
|
|
|
|
|
if (st->recording) {
|
|
|
|
|
unsigned char *jb = NULL;
|
|
|
|
|
unsigned long jsz = 0;
|
|
|
|
|
if (encode_rgb24_jpeg_mem(st->rgb24, st->cam_w, st->cam_h, 80, &jb, &jsz)) {
|
|
|
|
|
avi_write_frame(st, jb, jsz);
|
|
|
|
|
free(jb);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else if (st->pixfmt == V4L2_PIX_FMT_YUYV &&
|
|
|
|
|
bytesused >= (uint32_t)(st->raw_w * st->raw_h * 2)) {
|
|
|
|
|
yuyv_to_buffers(raw, st->raw_w, st->raw_h, st->rgb24, st->argb32);
|
|
|
|
|
if (st->recording) {
|
|
|
|
|
unsigned char *jb = NULL;
|
|
|
|
|
unsigned long jsz = 0;
|
|
|
|
|
if (encode_rgb24_jpeg_mem(st->rgb24, st->cam_w, st->cam_h, 80, &jb, &jsz)) {
|
|
|
|
|
avi_write_frame(st, jb, jsz);
|
|
|
|
|
free(jb);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else if (st->pixfmt == V4L2_PIX_FMT_MJPEG && bytesused > 4) {
|
|
|
|
|
if (st->recording) avi_write_frame(st, raw, bytesused);
|
|
|
|
|
struct jpeg_decompress_struct cinfo;
|
|
|
|
|
struct jpeg_error_mgr jerr;
|
|
|
|
|
cinfo.err = jpeg_std_error(&jerr);
|
|
|
|
|
jpeg_create_decompress(&cinfo);
|
|
|
|
|
jpeg_mem_src(&cinfo, raw, bytesused);
|
|
|
|
|
if (jpeg_read_header(&cinfo, TRUE) == JPEG_HEADER_OK) {
|
|
|
|
|
cinfo.out_color_space = JCS_RGB;
|
|
|
|
|
jpeg_start_decompress(&cinfo);
|
|
|
|
|
while ((int)cinfo.output_scanline < (int)cinfo.output_height &&
|
|
|
|
|
(int)cinfo.output_scanline < st->cam_h) {
|
|
|
|
|
int y = (int)cinfo.output_scanline;
|
|
|
|
|
uint8_t *dst = st->rgb24 + (size_t)y * (size_t)st->cam_w * 3U;
|
|
|
|
|
JSAMPROW row[1] = {dst};
|
|
|
|
|
jpeg_read_scanlines(&cinfo, row, 1);
|
|
|
|
|
if (st->argb32) {
|
|
|
|
|
int cw = (int)cinfo.output_width < st->cam_w ? (int)cinfo.output_width : st->cam_w;
|
|
|
|
|
for (int x = 0; x < cw; x++) {
|
|
|
|
|
uint8_t r = dst[3 * x], g = dst[3 * x + 1], b = dst[3 * x + 2];
|
|
|
|
|
st->argb32[y * st->cam_w + x] = 0xff000000U | ((uint32_t)r << 16) | ((uint32_t)g << 8) | b;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
jpeg_finish_decompress(&cinfo);
|
|
|
|
|
}
|
|
|
|
|
jpeg_destroy_decompress(&cinfo);
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (st->frame_surf) cairo_surface_mark_dirty(st->frame_surf);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-22 10:49:59 +02:00
|
|
|
static void on_v4l2_ready(gemui_app_t *app, int fd, void *ud) {
|
|
|
|
|
cam_state_t *st = ud;
|
|
|
|
|
struct v4l2_buffer buf;
|
|
|
|
|
memset(&buf, 0, sizeof(buf));
|
|
|
|
|
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
|
|
|
|
buf.memory = V4L2_MEMORY_MMAP;
|
|
|
|
|
if (ioctl(fd, VIDIOC_DQBUF, &buf) < 0) return;
|
|
|
|
|
|
2026-11-23 06:38:06 +02:00
|
|
|
if (buf.index < (uint32_t)st->num_bufs) {
|
|
|
|
|
process_captured_frame(st, st->bufs[buf.index].start, buf.bytesused);
|
2026-11-22 10:49:59 +02:00
|
|
|
}
|
|
|
|
|
ioctl(fd, VIDIOC_QBUF, &buf);
|
|
|
|
|
gemui_request_redraw(app);
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-23 06:38:06 +02:00
|
|
|
static bool open_camera_idx(cam_state_t *st, int idx, bool with_ui) {
|
2026-11-22 10:49:59 +02:00
|
|
|
close_camera(st);
|
2026-11-23 06:38:06 +02:00
|
|
|
if (idx < 0 || idx >= st->num_devs) return false;
|
2026-11-22 10:49:59 +02:00
|
|
|
|
|
|
|
|
int fd = open(st->dev_list[idx], O_RDWR | O_NONBLOCK | O_CLOEXEC);
|
2026-11-23 06:38:06 +02:00
|
|
|
if (fd < 0) return false;
|
|
|
|
|
|
|
|
|
|
struct v4l2_subdev_format mc_fmt;
|
|
|
|
|
memset(&mc_fmt, 0, sizeof(mc_fmt));
|
|
|
|
|
int sensor_fd = -1;
|
|
|
|
|
int mc_rc = configure_mc_pipeline(fd, &mc_fmt, &sensor_fd,
|
|
|
|
|
&st->ae_exp, &st->ae_again, &st->ae_dgain);
|
|
|
|
|
if (mc_rc < 0) {
|
|
|
|
|
close(fd);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2026-11-22 10:49:59 +02:00
|
|
|
|
|
|
|
|
struct v4l2_format fmt;
|
|
|
|
|
memset(&fmt, 0, sizeof(fmt));
|
|
|
|
|
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
2026-11-23 06:38:06 +02:00
|
|
|
if (mc_rc > 0) {
|
|
|
|
|
fmt.fmt.pix.width = mc_fmt.format.width;
|
|
|
|
|
fmt.fmt.pix.height = mc_fmt.format.height;
|
|
|
|
|
fmt.fmt.pix.pixelformat = (mc_fmt.format.code == MEDIA_BUS_FMT_SRGGB10_1X10)
|
|
|
|
|
? V4L2_PIX_FMT_MTISP_SRGGB10
|
|
|
|
|
: V4L2_PIX_FMT_MTISP_SGRBG10;
|
|
|
|
|
fmt.fmt.pix.field = V4L2_FIELD_NONE;
|
|
|
|
|
if (ioctl(fd, VIDIOC_S_FMT, &fmt) < 0) {
|
|
|
|
|
if (sensor_fd >= 0) close(sensor_fd);
|
|
|
|
|
close(fd);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
fmt.fmt.pix.width = 1280;
|
|
|
|
|
fmt.fmt.pix.height = 720;
|
|
|
|
|
fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
|
|
|
|
|
fmt.fmt.pix.field = V4L2_FIELD_NONE;
|
|
|
|
|
if (ioctl(fd, VIDIOC_S_FMT, &fmt) < 0 ||
|
|
|
|
|
(fmt.fmt.pix.pixelformat != V4L2_PIX_FMT_YUYV &&
|
|
|
|
|
fmt.fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG)) {
|
|
|
|
|
memset(&fmt, 0, sizeof(fmt));
|
|
|
|
|
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
|
|
|
|
fmt.fmt.pix.width = 1280;
|
|
|
|
|
fmt.fmt.pix.height = 720;
|
|
|
|
|
fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_MJPEG;
|
|
|
|
|
fmt.fmt.pix.field = V4L2_FIELD_NONE;
|
|
|
|
|
if (ioctl(fd, VIDIOC_S_FMT, &fmt) < 0) {
|
|
|
|
|
close(fd);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-11-22 10:49:59 +02:00
|
|
|
}
|
|
|
|
|
|
2026-11-23 06:38:06 +02:00
|
|
|
st->vfd = fd;
|
|
|
|
|
st->subdev_fd = sensor_fd;
|
2026-11-22 10:49:59 +02:00
|
|
|
st->pixfmt = fmt.fmt.pix.pixelformat;
|
2026-11-23 06:38:06 +02:00
|
|
|
st->raw_w = (int)fmt.fmt.pix.width;
|
|
|
|
|
st->raw_h = (int)fmt.fmt.pix.height;
|
|
|
|
|
st->raw_stride = (int)fmt.fmt.pix.bytesperline;
|
|
|
|
|
if (st->raw_stride <= 0) {
|
|
|
|
|
st->raw_stride = (st->pixfmt == V4L2_PIX_FMT_MTISP_SRGGB10 ||
|
|
|
|
|
st->pixfmt == V4L2_PIX_FMT_MTISP_SGRBG10)
|
|
|
|
|
? (st->raw_w * 10 / 8)
|
|
|
|
|
: (st->raw_w * 2);
|
|
|
|
|
}
|
|
|
|
|
st->frame_count = 0;
|
|
|
|
|
st->mirror = false;
|
|
|
|
|
geminux_bayer10_isp_init(&st->isp);
|
|
|
|
|
|
|
|
|
|
if (st->pixfmt == V4L2_PIX_FMT_MTISP_SRGGB10 ||
|
|
|
|
|
st->pixfmt == V4L2_PIX_FMT_MTISP_SGRBG10) {
|
|
|
|
|
st->raw16 = malloc((size_t)st->raw_w * (size_t)st->raw_h * sizeof(uint16_t));
|
|
|
|
|
if (!st->raw16) {
|
|
|
|
|
close_camera(st);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!alloc_output_buffers(st, with_ui)) {
|
|
|
|
|
close_camera(st);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2026-11-22 10:49:59 +02:00
|
|
|
|
|
|
|
|
struct v4l2_requestbuffers req;
|
|
|
|
|
memset(&req, 0, sizeof(req));
|
|
|
|
|
req.count = 4;
|
|
|
|
|
req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
|
|
|
|
req.memory = V4L2_MEMORY_MMAP;
|
|
|
|
|
if (ioctl(fd, VIDIOC_REQBUFS, &req) < 0 || req.count == 0) {
|
2026-11-23 06:38:06 +02:00
|
|
|
close_camera(st);
|
|
|
|
|
return false;
|
2026-11-22 10:49:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < (int)req.count && i < 4; i++) {
|
|
|
|
|
struct v4l2_buffer b;
|
|
|
|
|
memset(&b, 0, sizeof(b));
|
|
|
|
|
b.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
|
|
|
|
b.memory = V4L2_MEMORY_MMAP;
|
|
|
|
|
b.index = (uint32_t)i;
|
|
|
|
|
if (ioctl(fd, VIDIOC_QUERYBUF, &b) < 0) break;
|
|
|
|
|
st->bufs[i].length = b.length;
|
|
|
|
|
st->bufs[i].start = mmap(NULL, b.length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, b.m.offset);
|
|
|
|
|
if (st->bufs[i].start == MAP_FAILED) break;
|
|
|
|
|
st->num_bufs = i + 1;
|
|
|
|
|
ioctl(fd, VIDIOC_QBUF, &b);
|
|
|
|
|
}
|
2026-11-23 06:38:06 +02:00
|
|
|
if (st->num_bufs == 0) {
|
|
|
|
|
close_camera(st);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2026-11-22 10:49:59 +02:00
|
|
|
|
|
|
|
|
enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
|
|
|
|
if (ioctl(fd, VIDIOC_STREAMON, &type) < 0) {
|
|
|
|
|
close_camera(st);
|
2026-11-23 06:38:06 +02:00
|
|
|
return false;
|
2026-11-22 10:49:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
st->cur_dev = idx;
|
|
|
|
|
snprintf(st->dev_path, sizeof(st->dev_path), "%s", st->dev_list[idx]);
|
2026-11-23 06:38:06 +02:00
|
|
|
if (with_ui && st->app) gemui_add_fd(st->app, fd, on_v4l2_ready, st);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void rotate_camera(cam_state_t *st) {
|
|
|
|
|
if (st->recording) avi_stop(st);
|
|
|
|
|
st->rot_deg = (st->rot_deg + 90) % 360;
|
|
|
|
|
save_cam_config(st);
|
|
|
|
|
if (st->vfd >= 0) alloc_output_buffers(st, st->app != NULL);
|
|
|
|
|
if (st->app) {
|
|
|
|
|
char msg[48];
|
|
|
|
|
snprintf(msg, sizeof(msg), "Camera rotation: %d deg", st->rot_deg);
|
|
|
|
|
gemui_show_toast(st->app, msg, 1200);
|
|
|
|
|
gemui_request_redraw(st->app);
|
|
|
|
|
}
|
2026-11-22 10:49:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void capture_still(cam_state_t *st) {
|
|
|
|
|
if (!st->rgb24 || st->cam_w <= 0 || st->cam_h <= 0) {
|
|
|
|
|
gemui_show_toast(st->app, "No active V4L2 frame to capture", 1800);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const char *home = getenv("HOME");
|
|
|
|
|
char dir[512], path[576], ts[32];
|
|
|
|
|
snprintf(dir, sizeof(dir), "%s/Pictures", (home && home[0]) ? home : "/home/user");
|
|
|
|
|
const char *mk[] = {"mkdir", "-p", dir, NULL};
|
|
|
|
|
gemui_run_cmd_capture(mk, NULL, 0);
|
|
|
|
|
|
|
|
|
|
time_t now = time(NULL);
|
|
|
|
|
struct tm tm;
|
|
|
|
|
localtime_r(&now, &tm);
|
|
|
|
|
strftime(ts, sizeof(ts), "%Y%m%d_%H%M%S", &tm);
|
|
|
|
|
snprintf(path, sizeof(path), "%s/IMG_%s.jpg", dir, ts);
|
|
|
|
|
|
|
|
|
|
unsigned char *jb = NULL;
|
|
|
|
|
unsigned long jsz = 0;
|
|
|
|
|
if (encode_rgb24_jpeg_mem(st->rgb24, st->cam_w, st->cam_h, 92, &jb, &jsz)) {
|
|
|
|
|
FILE *f = fopen(path, "wb");
|
|
|
|
|
if (f) {
|
|
|
|
|
fwrite(jb, 1, jsz, f);
|
|
|
|
|
fclose(f);
|
|
|
|
|
snprintf(st->last_file, sizeof(st->last_file), "IMG_%s.jpg", ts);
|
|
|
|
|
gemui_show_toast(st->app, "Captured still JPEG to ~/Pictures", 2000);
|
|
|
|
|
}
|
|
|
|
|
free(jb);
|
|
|
|
|
}
|
|
|
|
|
gemui_request_redraw(st->app);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void discover_cameras(cam_state_t *st) {
|
|
|
|
|
st->num_devs = 0;
|
|
|
|
|
for (int i = 0; i < 16 && st->num_devs < 8; i++) {
|
|
|
|
|
char p[64];
|
|
|
|
|
snprintf(p, sizeof(p), "/dev/video%d", i);
|
|
|
|
|
int fd = open(p, O_RDWR | O_NONBLOCK | O_CLOEXEC);
|
|
|
|
|
if (fd < 0) continue;
|
|
|
|
|
struct v4l2_capability cap;
|
|
|
|
|
if (ioctl(fd, VIDIOC_QUERYCAP, &cap) == 0 &&
|
|
|
|
|
((cap.device_caps & V4L2_CAP_VIDEO_CAPTURE) ||
|
|
|
|
|
(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE))) {
|
|
|
|
|
snprintf(st->dev_list[st->num_devs++], 64, "%s", p);
|
|
|
|
|
}
|
|
|
|
|
close(fd);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-23 06:38:06 +02:00
|
|
|
static int run_oneshot(const char *out_path) {
|
|
|
|
|
cam_state_t st;
|
|
|
|
|
memset(&st, 0, sizeof(st));
|
|
|
|
|
st.vfd = -1;
|
|
|
|
|
st.subdev_fd = -1;
|
|
|
|
|
load_cam_config(&st);
|
|
|
|
|
discover_cameras(&st);
|
|
|
|
|
if (st.num_devs == 0 || !open_camera_idx(&st, 0, false)) {
|
|
|
|
|
fprintf(stderr, "geminux-camera: no usable V4L2 camera device\n");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool got_frame = false;
|
|
|
|
|
for (int n = 0; n < 8; n++) {
|
|
|
|
|
struct pollfd pfd = { .fd = st.vfd, .events = POLLIN, .revents = 0 };
|
|
|
|
|
int pr = poll(&pfd, 1, 2000);
|
|
|
|
|
if (pr <= 0 || !(pfd.revents & POLLIN)) {
|
|
|
|
|
fprintf(stderr, "geminux-camera: poll timeout waiting for frame %d\n", n);
|
|
|
|
|
close_camera(&st);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
struct v4l2_buffer buf;
|
|
|
|
|
memset(&buf, 0, sizeof(buf));
|
|
|
|
|
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
|
|
|
|
buf.memory = V4L2_MEMORY_MMAP;
|
|
|
|
|
if (ioctl(st.vfd, VIDIOC_DQBUF, &buf) < 0) {
|
|
|
|
|
fprintf(stderr, "geminux-camera: VIDIOC_DQBUF failed: %s\n", strerror(errno));
|
|
|
|
|
close_camera(&st);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
if (buf.index < (uint32_t)st.num_bufs &&
|
|
|
|
|
process_captured_frame(&st, st.bufs[buf.index].start, buf.bytesused)) {
|
|
|
|
|
got_frame = true;
|
|
|
|
|
}
|
|
|
|
|
ioctl(st.vfd, VIDIOC_QBUF, &buf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!got_frame) {
|
|
|
|
|
fprintf(stderr, "geminux-camera: failed to decode captured frame\n");
|
|
|
|
|
close_camera(&st);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsigned char *jb = NULL;
|
|
|
|
|
unsigned long jsz = 0;
|
|
|
|
|
int rc = 1;
|
|
|
|
|
if (encode_rgb24_jpeg_mem(st.rgb24, st.cam_w, st.cam_h, 92, &jb, &jsz)) {
|
|
|
|
|
FILE *f = fopen(out_path, "wb");
|
|
|
|
|
if (f) {
|
|
|
|
|
if (fwrite(jb, 1, jsz, f) == jsz) rc = 0;
|
|
|
|
|
fclose(f);
|
|
|
|
|
}
|
|
|
|
|
free(jb);
|
|
|
|
|
}
|
|
|
|
|
close_camera(&st);
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-22 10:49:59 +02:00
|
|
|
static void on_draw(gemui_app_t *app, cairo_t *cr, int w, int h, void *ud) {
|
|
|
|
|
cam_state_t *st = ud;
|
|
|
|
|
const gemui_theme_t *t = gemui_theme(app);
|
|
|
|
|
|
|
|
|
|
char badge[96];
|
2026-11-23 06:38:06 +02:00
|
|
|
if (st->vfd >= 0) {
|
|
|
|
|
snprintf(badge, sizeof(badge), "%s %dx%d%s", st->dev_path, st->cam_w, st->cam_h,
|
|
|
|
|
st->recording ? " [REC]" : "");
|
|
|
|
|
} else {
|
|
|
|
|
snprintf(badge, sizeof(badge), "No V4L2 Camera");
|
|
|
|
|
}
|
2026-11-22 10:49:59 +02:00
|
|
|
gemui_draw_header(app, cr, "\xef\x80\xb0", "Geminux Camera", badge);
|
|
|
|
|
|
|
|
|
|
int pad = 20;
|
2026-11-22 15:49:38 +02:00
|
|
|
int rw = 320;
|
|
|
|
|
gemui_rect_t vf = {pad, 92, w - rw - pad * 3, h - 112};
|
2026-11-22 10:49:59 +02:00
|
|
|
gemui_fill_round(cr, vf, 14, t->bg_overlay);
|
|
|
|
|
gemui_stroke_round(cr, vf, 14, 2.0, st->recording ? t->danger : t->border);
|
|
|
|
|
|
|
|
|
|
if (st->vfd >= 0 && st->frame_surf && st->cam_w > 0 && st->cam_h > 0) {
|
|
|
|
|
double sx = (double)(vf.w - 8) / st->cam_w;
|
|
|
|
|
double sy = (double)(vf.h - 8) / st->cam_h;
|
|
|
|
|
double sc = sx < sy ? sx : sy;
|
|
|
|
|
int dw = (int)(st->cam_w * sc);
|
|
|
|
|
int dh = (int)(st->cam_h * sc);
|
|
|
|
|
int dx = vf.x + (vf.w - dw) / 2;
|
|
|
|
|
int dy = vf.y + (vf.h - dh) / 2;
|
|
|
|
|
cairo_save(cr);
|
|
|
|
|
cairo_translate(cr, dx, dy);
|
|
|
|
|
cairo_scale(cr, sc, sc);
|
|
|
|
|
cairo_set_source_surface(cr, st->frame_surf, 0, 0);
|
|
|
|
|
cairo_paint(cr);
|
|
|
|
|
cairo_restore(cr);
|
|
|
|
|
} else {
|
2026-11-22 15:49:38 +02:00
|
|
|
gemui_draw_text(cr, t, (gemui_rect_t){vf.x + 24, vf.y + vf.h / 2 - 48, vf.w - 48, 48},
|
2026-11-22 10:49:59 +02:00
|
|
|
"Camera Stream Unavailable", 4, true, GEMUI_ALIGN_CENTER, t->fg_bright);
|
2026-11-22 15:49:38 +02:00
|
|
|
gemui_draw_text(cr, t, (gemui_rect_t){vf.x + 24, vf.y + vf.h / 2 + 10, vf.w - 48, 42},
|
2026-11-22 10:49:59 +02:00
|
|
|
"Waiting for hi556 / mt6797-seninf V4L2 /dev/video* device",
|
|
|
|
|
-2, false, GEMUI_ALIGN_CENTER, t->fg_muted);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int rx = vf.x + vf.w + pad;
|
2026-11-23 06:38:06 +02:00
|
|
|
gemui_draw_button(app, cr, (gemui_rect_t){rx, 96, rw, 90},
|
2026-11-22 10:49:59 +02:00
|
|
|
"Photo (JPEG)", HIT_SHUTTER, 0, true, false, false);
|
2026-11-23 06:38:06 +02:00
|
|
|
gemui_draw_button(app, cr, (gemui_rect_t){rx, 202, rw, 90},
|
2026-11-22 10:49:59 +02:00
|
|
|
st->recording ? "Stop AVI" : "Record AVI", HIT_RECORD, 0, false, st->recording, false);
|
2026-11-23 06:38:06 +02:00
|
|
|
gemui_draw_button(app, cr, (gemui_rect_t){rx, 308, rw, 78},
|
2026-11-22 10:49:59 +02:00
|
|
|
"Switch Sensor", HIT_SWITCH, 0, false, false, false);
|
2026-11-23 06:38:06 +02:00
|
|
|
char rot_lbl[32];
|
|
|
|
|
snprintf(rot_lbl, sizeof(rot_lbl), "Rotate 90\xc2\xb0 (%d\xc2\xb0)", st->rot_deg);
|
|
|
|
|
gemui_draw_button(app, cr, (gemui_rect_t){rx, 402, rw, 78},
|
|
|
|
|
rot_lbl, HIT_ROTATE, 0, false, false, false);
|
2026-11-22 10:49:59 +02:00
|
|
|
|
2026-11-23 06:38:06 +02:00
|
|
|
gemui_draw_text(cr, t, (gemui_rect_t){rx, 504, rw, 40},
|
2026-11-22 10:49:59 +02:00
|
|
|
st->last_file[0] ? "Last Saved:" : "", -3, true, GEMUI_ALIGN_LEFT, t->fg_muted);
|
2026-11-23 06:38:06 +02:00
|
|
|
gemui_draw_text(cr, t, (gemui_rect_t){rx, 546, rw, 40},
|
|
|
|
|
st->last_file[0] ? st->last_file : "Space: Shutter | O: Rotate",
|
2026-11-22 10:49:59 +02:00
|
|
|
-3, false, GEMUI_ALIGN_LEFT, t->accent);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void on_key(gemui_app_t *app, KeySym ks, unsigned int state, const char *utf8, void *ud) {
|
|
|
|
|
(void)state;
|
|
|
|
|
(void)utf8;
|
|
|
|
|
cam_state_t *st = ud;
|
|
|
|
|
if (ks == XK_Escape) {
|
|
|
|
|
gemui_quit(app, 0);
|
|
|
|
|
} else if (ks == XK_space || ks == XK_Return) {
|
|
|
|
|
capture_still(st);
|
|
|
|
|
} else if (ks == XK_r) {
|
|
|
|
|
if (st->recording) avi_stop(st); else avi_start(st);
|
|
|
|
|
gemui_request_redraw(app);
|
2026-11-23 06:38:06 +02:00
|
|
|
} else if (ks == XK_o) {
|
|
|
|
|
rotate_camera(st);
|
2026-11-22 10:49:59 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void on_ptr(gemui_app_t *app, int type, int x, int y, unsigned int btn, void *ud) {
|
|
|
|
|
(void)btn;
|
|
|
|
|
cam_state_t *st = ud;
|
|
|
|
|
if (type != GEMUI_PTR_DOWN) return;
|
|
|
|
|
const gemui_hit_t *hit = gemui_hit_test(app, x, y);
|
|
|
|
|
if (!hit) return;
|
|
|
|
|
|
|
|
|
|
if (hit->id == HIT_SHUTTER) {
|
|
|
|
|
capture_still(st);
|
|
|
|
|
} else if (hit->id == HIT_RECORD) {
|
|
|
|
|
if (st->recording) avi_stop(st); else avi_start(st);
|
|
|
|
|
gemui_request_redraw(app);
|
|
|
|
|
} else if (hit->id == HIT_SWITCH) {
|
|
|
|
|
discover_cameras(st);
|
|
|
|
|
if (st->num_devs > 0) {
|
2026-11-23 06:38:06 +02:00
|
|
|
open_camera_idx(st, (st->cur_dev + 1) % st->num_devs, true);
|
2026-11-22 10:49:59 +02:00
|
|
|
} else {
|
|
|
|
|
gemui_show_toast(app, "No /dev/video* sensor found", 1800);
|
|
|
|
|
}
|
|
|
|
|
gemui_request_redraw(app);
|
2026-11-23 06:38:06 +02:00
|
|
|
} else if (hit->id == HIT_ROTATE) {
|
|
|
|
|
rotate_camera(st);
|
2026-11-22 10:49:59 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-23 06:38:06 +02:00
|
|
|
int main(int argc, char **argv) {
|
|
|
|
|
if (argc >= 3 && strcmp(argv[1], "--oneshot") == 0) {
|
|
|
|
|
return run_oneshot(argv[2]);
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-22 10:49:59 +02:00
|
|
|
cam_state_t st;
|
|
|
|
|
memset(&st, 0, sizeof(st));
|
|
|
|
|
st.vfd = -1;
|
2026-11-23 06:38:06 +02:00
|
|
|
st.subdev_fd = -1;
|
|
|
|
|
load_cam_config(&st);
|
2026-11-22 10:49:59 +02:00
|
|
|
discover_cameras(&st);
|
|
|
|
|
|
2026-11-22 15:49:38 +02:00
|
|
|
st.app = gemui_create("geminux-camera", "Geminux Camera", 1920, 940, false);
|
2026-11-22 10:49:59 +02:00
|
|
|
if (!st.app) return 1;
|
2026-11-23 06:38:06 +02:00
|
|
|
if (st.num_devs > 0) open_camera_idx(&st, 0, true);
|
2026-11-22 10:49:59 +02:00
|
|
|
|
|
|
|
|
gemui_handlers_t h = { .on_draw = on_draw, .on_key = on_key, .on_ptr = on_ptr };
|
|
|
|
|
gemui_set_handlers(st.app, &h, &st);
|
|
|
|
|
int rc = gemui_run(st.app);
|
|
|
|
|
close_camera(&st);
|
|
|
|
|
gemui_destroy(st.app);
|
|
|
|
|
return rc;
|
|
|
|
|
}
|