-
Notifications
You must be signed in to change notification settings - Fork 0
/
v4l2-bayer-client.c
422 lines (338 loc) · 8.41 KB
/
v4l2-bayer-client.c
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <sys/un.h>
#include <linux/videodev2.h>
#include <cairo.h>
#include <v4l2-bayer-protocol.h>
#define ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0]))
struct v4l2_bayer_client {
int fd;
void *raw_buffer;
unsigned char *raw_pointer;
unsigned int raw_length;
void *rgb_buffer;
unsigned int rgb_length;
int dump_fd;
};
struct v4l2_bayer_format {
char *name;
unsigned int format;
};
int v4l2_bayer_client_open(struct v4l2_bayer_client *client, char *host_name)
{
struct sockaddr_in server_addr = { 0 };
struct hostent *host;
int fd = -1;
int ret;
if (!client || !host_name)
return -EINVAL;
fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0) {
ret = -errno;
goto error;
}
host = gethostbyname(host_name);
if (!host) {
ret = -errno;
goto error;
}
server_addr.sin_addr = *(struct in_addr *)host->h_addr;
server_addr.sin_port = htons(V4L2_BAYER_SERVER_PORT);
server_addr.sin_family = AF_INET;
ret = connect(fd, (struct sockaddr *)&server_addr, sizeof(server_addr));
if (ret) {
ret = -errno;
goto error;
}
client->fd = fd;
return 0;
error:
if (fd >= 0)
close(fd);
return ret;
}
int v4l2_bayer_client_close(struct v4l2_bayer_client *client)
{
if (!client || client->fd < 0)
return -EINVAL;
close(client->fd);
client->fd = -1;
return 0;
}
static int frame_fragment_process(struct v4l2_bayer_client *client,
void *buffer, unsigned int length)
{
if (client->dump_fd >= 0)
write(client->dump_fd, buffer, length);
if (client->raw_pointer) {
memcpy(client->raw_pointer, buffer, length);
client->raw_pointer += length;
}
return 0;
}
static int frame_fragments_read(struct v4l2_bayer_client *client)
{
struct v4l2_bayer_message message;
struct v4l2_bayer_frame_fragment fragment;
struct timeval timeout = { 0 };
unsigned int length = V4L2_BAYER_FRAME_FRAGMENT_SIZE;
void *buffer;
int ret = -1;
buffer = malloc(length);
do {
timeout.tv_sec = 2;
timeout.tv_usec = 0;
ret = v4l2_bayer_data_read_poll(client->fd, &timeout);
if (ret <= 0)
break;
ret = v4l2_bayer_data_read(client->fd, &message,
sizeof(message));
if (ret <= 0)
goto complete;
if (message.id != V4L2_BAYER_FRAME_FRAGMENT ||
message.length < sizeof(struct v4l2_bayer_frame_fragment))
return -EINVAL;
ret = v4l2_bayer_data_read(client->fd, &fragment,
sizeof(fragment));
if (ret <= 0)
goto complete;
if (length < fragment.length) {
length = fragment.length;
buffer = realloc(buffer, length);
}
ret = v4l2_bayer_data_read_poll(client->fd, &timeout);
if (ret <= 0)
break;
ret = v4l2_bayer_data_read(client->fd, buffer, fragment.length);
if (ret <= 0)
goto complete;
/*
printf("Rx fragment %u length %u\n", fragment.serial,
fragment.length);
*/
ret = frame_fragment_process(client, buffer, fragment.length);
if (ret < 0)
goto complete;
} while (1);
ret = 0;
complete:
if (buffer)
free(buffer);
return ret;
}
static int capture_request(struct v4l2_bayer_client *client, unsigned int width,
unsigned int height, unsigned int format)
{
struct v4l2_bayer_capture_request request = {
.width = width,
.height = height,
.format = format,
};
int ret;
ret = v4l2_bayer_message_write(client->fd, V4L2_BAYER_CAPTURE_REQUEST,
sizeof(request));
if (ret < 0)
return ret;
ret = v4l2_bayer_data_write(client->fd, &request, sizeof(request));
if (ret < 0)
return ret;
printf("Tx capture request size %ux%u, format %#x\n", request.width,
request.height, request.format);
return 0;
}
static int stream_start(struct v4l2_bayer_client *client, unsigned int width,
unsigned int height, unsigned int format)
{
struct v4l2_bayer_stream_start stream = {
.width = width,
.height = height,
.format = format,
};
int ret;
ret = v4l2_bayer_message_write(client->fd, V4L2_BAYER_STREAM_START,
sizeof(stream));
if (ret < 0)
return ret;
ret = v4l2_bayer_data_write(client->fd, &stream, sizeof(stream));
if (ret < 0)
return ret;
return 0;
}
static int stream_stop(struct v4l2_bayer_client *client)
{
int ret;
ret = v4l2_bayer_message_write(client->fd, V4L2_BAYER_STREAM_STOP, 0);
if (ret < 0)
return ret;
return 0;
}
#include "image-convert.c"
void image_write(char *path, void *rgb_data, unsigned int width, unsigned int height)
{
cairo_surface_t *surface = NULL;
surface = cairo_image_surface_create_for_data(rgb_data, CAIRO_FORMAT_RGB24, width, height, width * 4);
if (!surface)
return;
cairo_surface_write_to_png(surface, path);
cairo_surface_destroy(surface);
}
struct v4l2_bayer_format formats[] = {
/* Bayer */
{ "bggr8", V4L2_PIX_FMT_SBGGR8 },
{ "rggb8", V4L2_PIX_FMT_SRGGB8 },
{ "bggr10", V4L2_PIX_FMT_SBGGR10 },
{ "rggb10", V4L2_PIX_FMT_SRGGB10 },
/* YUV420 */
{ "nv12", V4L2_PIX_FMT_NV12 },
{ "nv21", V4L2_PIX_FMT_NV21 },
{ "yuv420", V4L2_PIX_FMT_YUV420 },
{ "yvu420", V4L2_PIX_FMT_YVU420 },
/* YUV422 */
{ "yuyv", V4L2_PIX_FMT_YUYV },
{ "uyvy", V4L2_PIX_FMT_UYVY },
{ "nv16", V4L2_PIX_FMT_NV16 },
{ "nv61", V4L2_PIX_FMT_NV61 },
{ "yuv422p", V4L2_PIX_FMT_YUV422P },
};
int main(int argc, char *argv[])
{
struct v4l2_bayer_client client = {
.dump_fd = -1,
.fd = -1,
};
char *host_name = strdup("localhost");
unsigned int width, height, format;
unsigned int command;
unsigned int i;
int option = 0;
bool dump = false;
int ret;
width = 2592;
height = 1944;
format = V4L2_PIX_FMT_SBGGR8;
command = V4L2_BAYER_CAPTURE_REQUEST;
while (option != -1) {
option = getopt(argc, argv, "w:h:f:r:");
if (option < 0)
break;
switch (option) {
case 'w':
width = atoi(optarg);
break;
case 'h':
height = atoi(optarg);
break;
case 'f':
for (i = 0; i < ARRAY_SIZE(formats); i++) {
if (!strcmp(optarg, formats[i].name)) {
format = formats[i].format;
break;
}
}
if (i == ARRAY_SIZE(formats))
goto error;
break;
case 'r':
host_name = strdup(optarg);
break;
}
}
if (optind < argc) {
if (!strcmp(argv[optind], "request"))
command = V4L2_BAYER_CAPTURE_REQUEST;
else if (!strcmp(argv[optind], "stream-start"))
command = V4L2_BAYER_STREAM_START;
else if (!strcmp(argv[optind], "stream-stop"))
command = V4L2_BAYER_STREAM_STOP;
else
printf("Invalid command, using default.\n");
}
ret = v4l2_bayer_client_open(&client, host_name);
if (ret)
goto error;
free(host_name);
switch (command) {
case V4L2_BAYER_CAPTURE_REQUEST:
switch (format) {
/* Bayer */
case V4L2_PIX_FMT_SBGGR8:
case V4L2_PIX_FMT_SRGGB8:
client.raw_length = width * height;
break;
case V4L2_PIX_FMT_SBGGR10:
case V4L2_PIX_FMT_SRGGB10:
client.raw_length = width * height * 2;
break;
/* YUV420 */
case V4L2_PIX_FMT_NV12:
case V4L2_PIX_FMT_NV21:
case V4L2_PIX_FMT_YUV420:
case V4L2_PIX_FMT_YVU420:
client.raw_length = 3 * width * height / 2;
break;
/* YUV422 */
case V4L2_PIX_FMT_YUYV:
case V4L2_PIX_FMT_UYVY:
case V4L2_PIX_FMT_NV16:
case V4L2_PIX_FMT_NV61:
case V4L2_PIX_FMT_YUV422P:
client.raw_length = width * height * 2;
break;
default:
goto error;
}
client.raw_buffer = malloc(client.raw_length);
client.raw_pointer = client.raw_buffer;
client.rgb_length = width * height * 4;
client.rgb_buffer = malloc(client.rgb_length);
if (dump) {
client.dump_fd = open("frame.raw", O_RDWR | O_CREAT | O_TRUNC,
0644);
if (client.dump_fd < 0)
goto error;
}
ret = capture_request(&client, width, height, format);
if (ret)
goto error;
printf("Capture requested!\n");
ret = frame_fragments_read(&client);
if (ret)
goto error;
printf("Frame fragments read done!\n");
image_convert(client.rgb_buffer, client.raw_buffer, client.raw_length,
width, height, format);
printf("Image convert done!\n");
image_write("frame.png", client.rgb_buffer, width, height);
printf("Image write done!\n");
if (dump)
close(client.dump_fd);
break;
case V4L2_BAYER_STREAM_START:
ret = stream_start(&client, width, height, format);
if (ret)
goto error;
printf("Stream on requested!\n");
break;
case V4L2_BAYER_STREAM_STOP:
ret = stream_stop(&client);
if (ret)
goto error;
printf("Stream off requested!\n");
break;
}
ret = v4l2_bayer_client_close(&client);
if (ret)
goto error;
return 0;
error:
return 1;
}