#include "camera.h" #include "device/device.h" #include "device/buffer_list.h" #include "device/links.h" #include "opts/log.h" #include "opts/fourcc.h" void camera_init(camera_t *camera) { memset(camera, 0, sizeof(*camera)); camera->name = "CAMERA"; } void camera_close(camera_t *camera) { if (!camera) return; for (int i = MAX_DEVICES; i-- > 0; ) { if (camera->devices[i]) { device_close(camera->devices[i]); camera->devices[i] = NULL; } } for (int i = MAX_DEVICES; i-- > 0; ) { if (camera->links[i].callbacks.on_buffer) { camera->links[i].callbacks.on_buffer = NULL; } } memset(camera->links, 0, sizeof(camera->links)); free(camera); } camera_t *camera_open(camera_options_t *options) { camera_t *camera = calloc(1, sizeof(camera_t)); camera->name = "CAMERA"; camera->options = *options; switch (camera->options.type) { case CAMERA_V4L2: camera->camera = device_v4l2_open(camera->name, camera->options.path); break; case CAMERA_LIBCAMERA: camera->camera = device_libcamera_open(camera->name, camera->options.path); break; default: LOG_ERROR(camera, "Unsupported camera type"); } if (!camera->camera) { goto error; } camera->camera->opts.allow_dma = camera->options.allow_dma; camera->camera->opts.width = camera->options.width; camera->camera->opts.height = camera->options.height; if (strstr(camera->camera->bus_info, "usb")) { LOG_INFO(camera, "Disabling DMA since device uses USB (which is likely not working properly)."); camera->camera->opts.allow_dma = false; } if (device_open_buffer_list(camera->camera, true, camera->options.width, camera->options.height, camera->options.format, 0, camera->options.nbufs, true) < 0) { goto error; } camera->camera->capture_list->do_timestamps = true; if (camera->options.fps > 0) { camera->camera->capture_list->fmt_interval_us = 1000 * 1000 / camera->options.fps; } switch (camera->camera->capture_list->fmt_format) { case V4L2_PIX_FMT_YUYV: case V4L2_PIX_FMT_YVYU: case V4L2_PIX_FMT_VYUY: case V4L2_PIX_FMT_UYVY: case V4L2_PIX_FMT_YUV420: case V4L2_PIX_FMT_RGB565: case V4L2_PIX_FMT_RGB24: if (camera_configure_direct(camera) < 0) { goto error; } break; case V4L2_PIX_FMT_MJPEG: case V4L2_PIX_FMT_H264: if (camera_configure_decoder(camera) < 0) { goto error; } break; case V4L2_PIX_FMT_SRGGB10P: #if 1 if (camera_configure_isp(camera, camera->options.high_res_factor, camera->options.low_res_factor) < 0) { goto error; } #else if (camera_configure_legacy_isp(camera, camera->options.high_res_factor) < 0) { goto error; } #endif break; default: LOG_ERROR(camera, "Unsupported camera format=%s", fourcc_to_string(camera->camera->capture_list->fmt_format).buf); break; } if (camera_set_params(camera) < 0) { goto error; } return camera; error: camera_close(camera); return NULL; } int camera_set_params(camera_t *camera) { // Set some defaults device_set_option_string(camera->codec_jpeg, "compression_quality", "80"); device_set_option_string(camera->codec_jpeg_lowres, "compression_quality", "80"); device_set_option_string(camera->codec_h264, "video_bitrate_mode", "0"); device_set_option_string(camera->codec_h264, "video_bitrate", "5000000"); device_set_option_string(camera->codec_h264, "repeat_sequence_header", "1"); device_set_option_string(camera->codec_h264, "h264_i_frame_period", "30"); device_set_option_string(camera->codec_h264, "h264_level", "11"); device_set_option_string(camera->codec_h264, "h264_profile", "4"); device_set_option_string(camera->codec_h264, "h264_minimum_qp_value", "16"); device_set_option_string(camera->codec_h264, "h264_maximum_qp_value", "32"); device_set_option_string(camera->codec_h264_lowres, "video_bitrate_mode", "0"); device_set_option_string(camera->codec_h264_lowres, "video_bitrate", "5000000"); device_set_option_string(camera->codec_h264_lowres, "repeat_sequence_header", "1"); device_set_option_string(camera->codec_h264_lowres, "h264_i_frame_period", "30"); device_set_option_string(camera->codec_h264_lowres, "h264_level", "11"); device_set_option_string(camera->codec_h264_lowres, "h264_profile", "4"); device_set_option_string(camera->codec_h264_lowres, "h264_minimum_qp_value", "16"); device_set_option_string(camera->codec_h264_lowres, "h264_maximum_qp_value", "32"); device_set_fps(camera->camera, camera->options.fps); device_set_option_list(camera->camera, camera->options.options); device_set_option_list(camera->isp_srgb, camera->options.isp.options); device_set_option_list(camera->codec_h264, camera->options.h264.options); device_set_option_list(camera->codec_h264_lowres, camera->options.h264.options); device_set_option_list(camera->codec_jpeg, camera->options.jpeg.options); device_set_option_list(camera->codec_jpeg_lowres, camera->options.jpeg.options); // DEVICE_SET_OPTION(camera->camera, EXPOSURE, 2684); // DEVICE_SET_OPTION(camera->camera, ANALOGUE_GAIN, 938); // DEVICE_SET_OPTION(camera->camera, DIGITAL_GAIN, 512); // DEVICE_SET_OPTION(camera->camera, VBLANK, 1636); // DEVICE_SET_OPTION(camera->camera, HBLANK, 6906); // DEVICE_SET_OPTION(camera->isp_srgb, RED_BALANCE, 2120); // DEVICE_SET_OPTION(camera->isp_srgb, BLUE_BALANCE, 1472); // DEVICE_SET_OPTION(camera->isp_srgb, DIGITAL_GAIN, 1007); return 0; } int camera_run(camera_t *camera) { bool running = false; return links_loop(camera->links, &running); }