Skip to content

Commit 17f8a68

Browse files
committed
v4l2: ignore devices without suitable pixel format
1 parent 8c978cf commit 17f8a68

File tree

3 files changed

+59
-23
lines changed

3 files changed

+59
-23
lines changed

src/arvv4l2device.c

+15-5
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,19 @@ G_DEFINE_TYPE_WITH_CODE (ArvV4l2Device, arv_v4l2_device, ARV_TYPE_DEVICE, G_ADD_
9999

100100
/* ArvV4l2Device implemenation */
101101

102+
ArvPixelFormat
103+
arv_pixel_format_from_v4l2 (guint32 v4l2_pixel_format)
104+
{
105+
unsigned int i;
106+
107+
for (i = 0; i < G_N_ELEMENTS(pixel_format_map); i++) {
108+
if (v4l2_pixel_format == pixel_format_map[i].v4l2)
109+
return pixel_format_map[i].genicam;
110+
}
111+
112+
return 0;
113+
}
114+
102115
/* ArvDevice implemenation */
103116

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

549562
for (i = 0; TRUE; i++) {
550-
int j, k;
563+
int j;
551564
struct v4l2_fmtdesc format = {0};
552565
guint32 genicam_pixel_format = 0;
553566

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

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

561-
for (k = 0; k < G_N_ELEMENTS(pixel_format_map); k++) {
562-
if (format.pixelformat == pixel_format_map[k].v4l2)
563-
genicam_pixel_format = pixel_format_map[k].genicam;
564-
}
574+
genicam_pixel_format = arv_pixel_format_from_v4l2(format.pixelformat);
565575

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

src/arvv4l2deviceprivate.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@
3131

3232
G_BEGIN_DECLS
3333

34+
ArvPixelFormat arv_pixel_format_from_v4l2 (guint32 v4l2_pixel_format);
35+
3436
gboolean arv_v4l2_device_set_image_format (ArvV4l2Device *device);
3537
gboolean arv_v4l2_device_get_image_format (ArvV4l2Device *device,
36-
guint32 *payload_size, guint32 *pixel_format,
38+
guint32 *payload_size, ArvPixelFormat *pixel_format,
3739
guint32 *width, guint32 *height, guint32 *bytes_per_line);
3840
int arv_v4l2_device_get_fd (ArvV4l2Device *v4l2_device);
3941

src/arvv4l2interface.c

+41-17
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
#include <arvv4l2deviceprivate.h>
3030
#include <arvinterfaceprivate.h>
3131
#include <arvv4l2device.h>
32-
#include <arvdebug.h>
32+
#include <arvdebugprivate.h>
3333
#include <gudev/gudev.h>
3434
#include <libv4l2.h>
3535
#include <linux/videodev2.h>
@@ -75,22 +75,46 @@ arv_v4l2_interface_device_infos_new (const char *device_file, const char *name)
7575
if (v4l2_ioctl (fd, VIDIOC_QUERYCAP, &cap) != -1 &&
7676
((cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) != 0) &&
7777
((cap.capabilities & V4L2_CAP_STREAMING) != 0)) {
78-
infos = g_new0 (ArvV4l2InterfaceDeviceInfos, 1);
79-
80-
infos->ref_count = 1;
81-
infos->id = g_strdup_printf ("%s-%s", (char *) cap.card, name);
82-
infos->bus = g_strdup ((char *) cap.bus_info);
83-
infos->device_file = g_strdup (device_file);
84-
infos->version = g_strdup_printf ("%d.%d.%d",
85-
(cap.version >> 16) & 0xff,
86-
(cap.version >> 8) & 0xff,
87-
(cap.version >> 0) & 0xff);
88-
89-
return infos;
90-
}
91-
v4l2_close (fd);
92-
}
93-
}
78+
unsigned int i;
79+
gboolean found = FALSE;
80+
81+
for (i = 0; TRUE; i++) {
82+
struct v4l2_fmtdesc format = {0};
83+
84+
format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
85+
format.index = i;
86+
if (v4l2_ioctl(fd, VIDIOC_ENUM_FMT, &format) == -1)
87+
break;
88+
89+
if (arv_pixel_format_from_v4l2(format.pixelformat) != 0) {
90+
found = TRUE;
91+
break;
92+
}
93+
}
94+
95+
if (found) {
96+
infos = g_new0 (ArvV4l2InterfaceDeviceInfos, 1);
97+
98+
infos->ref_count = 1;
99+
infos->id = g_strdup_printf ("%s-%s", (char *) cap.card, name);
100+
infos->bus = g_strdup ((char *) cap.bus_info);
101+
infos->device_file = g_strdup (device_file);
102+
infos->version = g_strdup_printf ("%d.%d.%d",
103+
(cap.version >> 16) & 0xff,
104+
(cap.version >> 8) & 0xff,
105+
(cap.version >> 0) & 0xff);
106+
107+
v4l2_close (fd);
108+
109+
return infos;
110+
}
111+
112+
arv_warning_interface ("No suitable pixel format found for v4l2 device '%s'",
113+
device_file);
114+
}
115+
v4l2_close (fd);
116+
}
117+
}
94118

95119
return NULL;
96120
}

0 commit comments

Comments
 (0)