Skip to content

Commit

Permalink
v4l2: ignore devices without suitable pixel format
Browse files Browse the repository at this point in the history
  • Loading branch information
EmmanuelP committed Jan 19, 2024
1 parent a9ca7b2 commit dc16732
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 23 deletions.
20 changes: 15 additions & 5 deletions src/arvv4l2device.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,19 @@ G_DEFINE_TYPE_WITH_CODE (ArvV4l2Device, arv_v4l2_device, ARV_TYPE_DEVICE, G_ADD_

/* ArvV4l2Device implemenation */

ArvPixelFormat
arv_pixel_format_from_v4l2 (guint32 v4l2_pixel_format)
{
unsigned int i;

for (i = 0; i < G_N_ELEMENTS(pixel_format_map); i++) {
if (v4l2_pixel_format == pixel_format_map[i].v4l2)
return pixel_format_map[i].genicam;
}

return 0;
}

/* ArvDevice implemenation */

static ArvStream *
Expand Down Expand Up @@ -547,7 +560,7 @@ arv_v4l2_device_constructed (GObject *self)
" <DisplayName>Pixel format</DisplayName>\n");

for (i = 0; TRUE; i++) {
int j, k;
int j;
struct v4l2_fmtdesc format = {0};
guint32 genicam_pixel_format = 0;

Expand All @@ -558,10 +571,7 @@ arv_v4l2_device_constructed (GObject *self)

arv_info_device ("Found format %s", format.description);

for (k = 0; k < G_N_ELEMENTS(pixel_format_map); k++) {
if (format.pixelformat == pixel_format_map[k].v4l2)
genicam_pixel_format = pixel_format_map[k].genicam;
}
genicam_pixel_format = arv_pixel_format_from_v4l2(format.pixelformat);

g_array_insert_val (priv->pixel_formats, i, genicam_pixel_format);

Expand Down
4 changes: 3 additions & 1 deletion src/arvv4l2deviceprivate.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@

G_BEGIN_DECLS

ArvPixelFormat arv_pixel_format_from_v4l2 (guint32 v4l2_pixel_format);

gboolean arv_v4l2_device_set_image_format (ArvV4l2Device *device);
gboolean arv_v4l2_device_get_image_format (ArvV4l2Device *device,
guint32 *payload_size, guint32 *pixel_format,
guint32 *payload_size, ArvPixelFormat *pixel_format,
guint32 *width, guint32 *height, guint32 *bytes_per_line);
int arv_v4l2_device_get_fd (ArvV4l2Device *v4l2_device);

Expand Down
58 changes: 41 additions & 17 deletions src/arvv4l2interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#include <arvv4l2deviceprivate.h>
#include <arvinterfaceprivate.h>
#include <arvv4l2device.h>
#include <arvdebug.h>
#include <arvdebugprivate.h>
#include <gudev/gudev.h>
#include <libv4l2.h>
#include <linux/videodev2.h>
Expand Down Expand Up @@ -75,22 +75,46 @@ arv_v4l2_interface_device_infos_new (const char *device_file, const char *name)
if (v4l2_ioctl (fd, VIDIOC_QUERYCAP, &cap) != -1 &&
((cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) != 0) &&
((cap.capabilities & V4L2_CAP_STREAMING) != 0)) {
infos = g_new0 (ArvV4l2InterfaceDeviceInfos, 1);

infos->ref_count = 1;
infos->id = g_strdup_printf ("%s-%s", (char *) cap.card, name);
infos->bus = g_strdup ((char *) cap.bus_info);
infos->device_file = g_strdup (device_file);
infos->version = g_strdup_printf ("%d.%d.%d",
(cap.version >> 16) & 0xff,
(cap.version >> 8) & 0xff,
(cap.version >> 0) & 0xff);

return infos;
}
v4l2_close (fd);
}
}
unsigned int i;
gboolean found = FALSE;

for (i = 0; TRUE; i++) {
struct v4l2_fmtdesc format = {0};

format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
format.index = i;
if (v4l2_ioctl(fd, VIDIOC_ENUM_FMT, &format) == -1)
break;

if (arv_pixel_format_from_v4l2(format.pixelformat) != 0) {
found = TRUE;
break;
}
}

if (found) {
infos = g_new0 (ArvV4l2InterfaceDeviceInfos, 1);

infos->ref_count = 1;
infos->id = g_strdup_printf ("%s-%s", (char *) cap.card, name);
infos->bus = g_strdup ((char *) cap.bus_info);
infos->device_file = g_strdup (device_file);
infos->version = g_strdup_printf ("%d.%d.%d",
(cap.version >> 16) & 0xff,
(cap.version >> 8) & 0xff,
(cap.version >> 0) & 0xff);

v4l2_close (fd);

return infos;
}

arv_warning_interface ("No suitable pixel format found for v4l2 device '%s'",
device_file);
}
v4l2_close (fd);
}
}

return NULL;
}
Expand Down

0 comments on commit dc16732

Please sign in to comment.