hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include "camera_device.h"
 
#ifdef LOG_TAG
    #undef LOG_TAG
#endif
#define LOG_TAG "aiqtool"
 
void device_close(int dev_fd)
{
    if (-1 == close(dev_fd)) {
        errno_debug("close");
    }
    dev_fd = -1;
}
 
int device_open(const char* dev_name)
{
    int dev_fd;
    struct stat st;
    if (-1 == stat(dev_name, &st)) {
        LOG_ERROR("Cannot identify '%s': %d, %s\n", dev_name, errno, strerror(errno));
        return -1;
    }
    if (!S_ISCHR(st.st_mode)) {
        LOG_ERROR("%s is no devicen", dev_name);
        return -1;
    }
    dev_fd = open(dev_name, O_RDWR /*| O_NONBLOCK*/, 0);
    if (-1 == dev_fd) {
        LOG_ERROR("Cannot open '%s': %d, %s\n", dev_name, errno, strerror(errno));
        return -1;
    }
    return dev_fd;
}
 
int device_querycap(int dev_fd, struct v4l2_capability* cap)
{
    int ret = xioctl(dev_fd, VIDIOC_QUERYCAP, cap);
    if (-1 == ret) {
        if (EINVAL == errno) {
            errno_debug("VIDIOC_QUERYCAP EINVAL");
        } else {
            errno_debug("VIDIOC_QUERYCAP");
        }
    }
    return ret;
}
 
int device_cropcap(int dev_fd, struct v4l2_cropcap* cropcap, struct v4l2_crop* crop)
{
    int ret = xioctl(dev_fd, VIDIOC_CROPCAP, cropcap);
    // if (0 == ret) {
    ret = xioctl(dev_fd, VIDIOC_S_CROP, crop);
    if (-1 == ret) {
        switch (errno) {
            case EINVAL:
                LOG_ERROR("Cropping not supported.\n");
                break;
            default:
                /* Errors ignored. */
                break;
        }
    }
    //}
    return ret;
}
 
int device_setformat(int dev_fd, struct v4l2_format* fmt)
{
    /* Note VIDIOC_S_FMT may change width and height. */
    int ret = xioctl(dev_fd, VIDIOC_S_FMT, fmt);
    if (-1 == ret) {
        errno_debug("VIDIOC_S_FMT");
    }
    return ret;
}
 
int device_getformat(int dev_fd, struct v4l2_format* fmt)
{
    /* Preserve original settings as set by v4l2-ctl for example */
    int ret = xioctl(dev_fd, VIDIOC_G_FMT, fmt);
    if (-1 == ret) {
        errno_debug("VIDIOC_G_FMT");
    }
    return ret;
}
 
int device_getsubdevformat(int dev_fd, struct v4l2_subdev_format* fmt)
{
    int ret = xioctl(dev_fd, VIDIOC_SUBDEV_G_FMT, fmt);
    if (-1 == ret) {
        errno_debug("VIDIOC_SUBDEV_G_FMT");
    }
    return ret;
}
 
int device_setsubdevformat(int dev_fd, struct v4l2_subdev_format* fmt)
{
    int ret = xioctl(dev_fd, VIDIOC_SUBDEV_S_FMT, fmt);
    if (-1 == ret) {
        errno_debug("VIDIOC_SUBDEV_G_FMT");
    }
    return ret;
}
 
int device_setsubdevcrop(int dev_fd, struct v4l2_subdev_selection* sel)
{
    int ret = xioctl(dev_fd, VIDIOC_SUBDEV_S_SELECTION, sel);
    if (-1 == ret) {
        switch (errno) {
            case EINVAL:
                LOG_ERROR("Cropping not supported.\n");
                break;
            default:
                /* Errors ignored. */
                break;
        }
    }
    return ret;
}
 
int device_getblank(int dev_fd, struct v4l2_queryctrl* ctrl)
{
    int ret = xioctl(dev_fd, VIDIOC_QUERYCTRL, ctrl);
    if (-1 == ret) {
        errno_debug("VIDIOC_QUERYCTRL");
    }
    return ret;
}
 
int device_getsensorfps(int dev_fd, struct v4l2_subdev_frame_interval* finterval)
{
    int ret = xioctl(dev_fd, VIDIOC_SUBDEV_G_FRAME_INTERVAL, finterval);
    if (-1 == ret) {
        errno_debug("VIDIOC_SUBDEV_G_FRAME_INTERVAL");
    }
    return ret;
}
 
int device_set3aexposure(int dev_fd, struct v4l2_ext_controls* ctrls)
{
    int ret = xioctl(dev_fd, VIDIOC_S_EXT_CTRLS, ctrls);
    if (-1 == ret) {
        errno_debug("VIDIOC_S_EXT_CTRLS");
    }
    return ret;
}
 
int device_queryctrl(int dev_fd, struct v4l2_queryctrl* query)
{
    int ret = xioctl(dev_fd, VIDIOC_QUERYCTRL, query);
    if (-1 == ret) {
        errno_debug("VIDIOC_S_CTRL");
    }
    return ret;
}
 
int device_setctrl(int dev_fd, struct v4l2_control* ctrl)
{
    int ret = xioctl(dev_fd, VIDIOC_S_CTRL, ctrl);
    if (-1 == ret) {
        errno_debug("VIDIOC_S_CTRL");
    }
    return ret;
}
 
int device_streamon(int dev_fd, enum v4l2_buf_type* type)
{
    int ret = xioctl(dev_fd, VIDIOC_STREAMON, type);
    if (-1 == ret) {
        errno_debug("VIDIOC_STREAMON");
    }
    return ret;
}
 
int device_streamoff(int dev_fd, enum v4l2_buf_type* type)
{
    int ret = xioctl(dev_fd, VIDIOC_STREAMOFF, type);
    if (-1 == ret) {
        errno_debug("VIDIOC_STREAMOFF");
    }
    return ret;
}
 
int device_qbuf(int dev_fd, struct v4l2_buffer* buf)
{
    int ret = xioctl(dev_fd, VIDIOC_QBUF, buf);
    if (-1 == ret) {
        errno_debug("VIDIOC_QBUF");
    }
    return ret;
}
 
int device_dqbuf(int dev_fd, struct v4l2_buffer* buf)
{
    assert(buf);
    int ret = xioctl(dev_fd, VIDIOC_DQBUF, buf);
    if (-1 == ret) {
        switch (errno) {
            case EAGAIN:
                return 0;
            case EIO:
            /* Could ignore EIO, see spec. */
            /* fall through */
            default:
                errno_debug("VIDIOC_DQBUF");
        }
    }
    return ret;
}