Allow to set -camera-vflip and -camera-hflip

This commit is contained in:
Kamil Trzcinski 2022-08-24 17:48:55 +02:00
parent 8b1d066dfc
commit a9c65fae0d
9 changed files with 61 additions and 3 deletions

View File

@ -138,7 +138,9 @@ option_value_t camera_formats[] = {
{ "MJPEG", V4L2_PIX_FMT_MJPEG },
{ "JPEG", V4L2_PIX_FMT_MJPEG },
{ "H264", V4L2_PIX_FMT_H264 },
{ "RG10", V4L2_PIX_FMT_SRGGB10P },
{ "RG10", V4L2_PIX_FMT_SRGGB10 },
{ "GB10P", V4L2_PIX_FMT_SGRBG10P },
{ "RG10P", V4L2_PIX_FMT_SRGGB10P },
{ "RGB565", V4L2_PIX_FMT_RGB565 },
{ "RGBP", V4L2_PIX_FMT_RGB565 },
{ "RGB24", V4L2_PIX_FMT_RGB24 },
@ -167,6 +169,8 @@ option_t all_options[] = {
DEFINE_OPTION_PTR(camera, options, list, "Set the camera options. List all available options with `-camera-list_options`."),
DEFINE_OPTION(camera, auto_reconnect, uint, "Set the camera auto-reconnect delay in seconds."),
DEFINE_OPTION_DEFAULT(camera, auto_focus, bool, "1", "Do auto-focus on start-up."),
DEFINE_OPTION_DEFAULT(camera, vflip, bool, "1", "Do vertical image flip."),
DEFINE_OPTION_DEFAULT(camera, hflip, bool, "1", "Do horizontal image flip."),
DEFINE_OPTION_PTR(camera, isp.options, list, "Set the ISP processing options. List all available options with `-camera-list_options`."),
DEFINE_OPTION_PTR(camera, jpeg.options, list, "Set the JPEG compression options. List all available options with `-camera-list_options`."),

View File

@ -24,6 +24,14 @@ typedef struct camera_options_s {
float low_res_factor;
bool auto_focus;
unsigned auto_reconnect;
union {
bool vflip;
unsigned vflip_align;
};
union {
bool hflip;
unsigned hflip_align;
};
char options[CAMERA_OPTIONS_LENGTH];
bool list_options;

View File

@ -22,6 +22,8 @@ static int camera_configure_input_v4l2(camera_t *camera)
return -1;
}
device_set_rotation(camera->camera, camera->options.vflip, camera->options.hflip);
camera->camera->opts.allow_dma = camera->options.allow_dma;
if (strstr(camera->camera->bus_info, "usb")) {
@ -79,10 +81,20 @@ static int camera_configure_input_libcamera(camera_t *camera)
return -1;
}
device_set_rotation(camera->camera, camera->options.vflip, camera->options.hflip);
camera->camera->opts.allow_dma = camera->options.allow_dma;
buffer_list_t *camera_capture = device_open_buffer_list(
camera->camera, true, camera->options.width / camera->options.high_res_factor, camera->options.height / camera->options.high_res_factor, camera->options.format, 0, camera->options.nbufs, true);
camera->camera,
true,
camera->options.width / camera->options.high_res_factor,
camera->options.height / camera->options.high_res_factor,
camera->options.format,
0,
camera->options.nbufs,
true
);
if (!camera_capture) {
return -1;
}

View File

@ -226,6 +226,20 @@ int device_set_fps(device_t *dev, int desired_fps)
return 0;
}
int device_set_rotation(device_t *dev, bool vflip, bool hflip)
{
if (!dev)
return -1;
if (dev->hw->device_set_rotation) {
return dev->hw->device_set_rotation(dev, vflip, hflip);
}
int hret = device_set_option_string(dev, "horizontal_flip", hflip ? "1" : "0");
int vret = device_set_option_string(dev, "vertical_flip", vflip ? "1" : "0");
return hret ? hret : vret;
}
int device_set_option_string(device_t *dev, const char *key, const char *value)
{
if (!dev) {

View File

@ -16,6 +16,7 @@ typedef struct device_hw_s {
int (*device_video_force_key)(device_t *dev);
void (*device_dump_options)(device_t *dev, FILE *stream);
int (*device_set_fps)(device_t *dev, int desired_fps);
int (*device_set_rotation)(device_t *dev, bool vflip, bool hflip);
int (*device_set_option)(device_t *dev, const char *key, const char *value);
int (*buffer_open)(buffer_t *buf);
@ -69,6 +70,7 @@ int device_video_force_key(device_t *dev);
void device_dump_options(device_t *dev, FILE *stream);
int device_set_fps(device_t *dev, int desired_fps);
int device_set_rotation(device_t *dev, bool vflip, bool hflip);
int device_set_option_string(device_t *dev, const char *option, const char *value);
void device_set_option_list(device_t *dev, const char *option_list);

View File

@ -76,6 +76,15 @@ int libcamera_buffer_list_open(buffer_list_t *buf_list)
if (buf_list->libcamera->configuration->validate() == libcamera::CameraConfiguration::Invalid) {
LOG_ERROR(buf_list, "Camera configuration invalid");
}
if (buf_list->dev->libcamera->vflip) {
buf_list->libcamera->configuration->transform |= libcamera::Transform::VFlip;
}
if (buf_list->dev->libcamera->hflip) {
buf_list->libcamera->configuration->transform |= libcamera::Transform::HFlip;
}
if (!!(buf_list->libcamera->configuration->transform & libcamera::Transform::Transpose)) {
LOG_ERROR(buf_list, "Transformation requiring transpose not supported");
}
if (buf_list->dev->libcamera->camera->configure(buf_list->libcamera->configuration.get()) < 0) {
LOG_ERROR(buf_list, "Failed to configure camera");
@ -90,7 +99,6 @@ int libcamera_buffer_list_open(buffer_list_t *buf_list)
buf_list->libcamera->allocator = std::make_shared<libcamera::FrameBufferAllocator>(
buf_list->dev->libcamera->camera);
for (libcamera::StreamConfiguration &stream_config : *buf_list->libcamera->configuration) {
if (buf_list->libcamera->allocator->allocate(stream_config.stream()) < 0) {
LOG_ERROR(buf_list, "Can't allocate buffers");

View File

@ -127,6 +127,13 @@ int libcamera_device_set_fps(device_t *dev, int desired_fps)
return 0;
}
int libcamera_device_set_rotation(device_t *dev, bool vflip, bool hflip)
{
dev->libcamera->vflip = vflip;
dev->libcamera->hflip = hflip;
return 0;
}
int libcamera_device_set_option(device_t *dev, const char *keyp, const char *value)
{
auto key = libcamera_device_option_normalize(keyp);

View File

@ -6,6 +6,7 @@ device_hw_t libcamera_device_hw = {
.device_close = libcamera_device_close,
.device_dump_options = libcamera_device_dump_options,
.device_set_fps = libcamera_device_set_fps,
.device_set_rotation = libcamera_device_set_rotation,
.device_set_option = libcamera_device_set_option,
.buffer_open = libcamera_buffer_open,

View File

@ -40,6 +40,7 @@ typedef struct device_libcamera_s {
std::shared_ptr<libcamera::CameraManager> camera_manager;
std::shared_ptr<libcamera::Camera> camera;
libcamera::ControlList controls;
bool vflip, hflip;
} device_libcamera_t;
typedef struct buffer_list_libcamera_s {
@ -59,6 +60,7 @@ int libcamera_device_open(device_t *dev);
void libcamera_device_close(device_t *dev);
void libcamera_device_dump_options(device_t *dev, FILE *stream);
int libcamera_device_set_fps(device_t *dev, int desired_fps);
int libcamera_device_set_rotation(device_t *dev, bool vflip, bool hflip);
int libcamera_device_set_option(device_t *dev, const char *key, const char *value);
int libcamera_buffer_open(buffer_t *buf);