Debian Codegraph @

utils/qvidcap/qvidcap.cpp

raw
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * qvidcap: a control panel controlling v4l2 devices.
4 *
5 * Copyright 2018 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
6 */
7
8 #include <sys/types.h>
9 #include <sys/socket.h>
10 #include <netinet/in.h>
11
12 #include <QApplication>
13 #include <QScrollArea>
14 #include <QtMath>
15
16 #include "qvidcap.h"
17 #include "capture.h"
18
19 #include <libv4lconvert.h>
20 #include "v4l-stream.h"
21 #include "v4l2-info.h"
22
23 static void usage()
24 {
25 printf("Usage: qvidcap <options>\n\n"
26 "Options:\n\n"
27 " -d, --device=<dev> use device <dev> as the video device\n"
28 " if <dev> is a number, then /dev/video<dev> is used\n"
29 " -f, --file=<file> read from the file <file> for the raw frame data\n"
30 " -p, --port[=<port>] listen for a network connection on the given port\n"
31 " The default port is %d\n"
32 " -T, --tpg use the test pattern generator\n"
33 "\n"
34 " If neither -d, -f, -p nor -T is specified then use /dev/video0.\n"
35 "\n"
36 " -c, --count=<cnt> stop after <cnt> captured frames\n"
37 " -b, --buffers=<bufs> request <bufs> buffers (default 4) when streaming\n"
38 " from a video device\n"
39 " -s, --single-step[=<frm>] starting with frame <frm> (default 0), pause after\n"
40 " displaying each frame until Space is pressed.\n"
41 " -C, --colorspace=<c> override colorspace\n"
42 " <c> can be one of the following colorspaces:\n"
43 " smpte170m, smpte240m, rec709, 470m, 470bg, jpeg, srgb,\n"
44 " oprgb, bt2020, dcip3\n"
45 " -X, --xfer-func=<x> override transfer function\n"
46 " <x> can be one of the following transfer functions:\n"
47 " default, 709, srgb, oprgb, smpte240m, smpte2084, dcip3, none\n"
48 " -Y, --ycbcr-enc=<y> override Y'CbCr encoding\n"
49 " <y> can be one of the following Y'CbCr encodings:\n"
50 " default, 601, 709, xv601, xv709, bt2020, bt2020c, smpte240m\n"
51 " -H, --hsv-enc=<hsv> override HSV encoding\n"
52 " <hsv> can be one of the following HSV encodings:\n"
53 " default, 180, 256\n"
54 " -Q, --quant=<q> override quantization\n"
55 " <q> can be one of the following quantization methods:\n"
56 " default, full-range, lim-range\n"
57 " -P, --pixelformat=<p> For video devices: set the format to this pixel format.\n"
58 " For reading from a file: interpret the data using this\n"
59 " pixel format setting.\n"
60 " Use -l to see the list of supported pixel formats.\n"
61 "\n"
62 " -l, --list-formats display all supported formats\n"
63 " -h, --help display this help message\n"
64 " -t, --timings report frame render timings\n"
65 " -v, --verbose be more verbose\n"
66 " -R, --raw open device in raw mode\n"
67 "\n"
68 " --opengl force openGL to display the video\n"
69 " --opengles force openGL ES to display the video\n"
70 "\n"
71 " The following options are ignored when capturing from a video device:\n"
72 "\n"
73 " -W, --width=<width> set width\n"
74 " -H, --height=<height> set frame (not field!) height\n"
75 " -A, --padding=<bytes> set additional horizontal padding (after width)\n"
76 " --fps=<fps> set frames-per-second (default is 30)\n"
77 "\n"
78 " The following options are only valid when reading from a file:\n"
79 "\n"
80 " -F, --field=<f> override field setting\n"
81 " <f> can be one of the following field layouts:\n"
82 " any, none, top, bottom, interlaced, seq_tb, seq_bt,\n"
83 " alternate, interlaced_tb, interlaced_bt\n"
84 " --no-loop stop at the end of the file, don't loop back to the beginning\n"
85 " --from-frame=<frame> start playing back the file at the given frame number (starts at 0)\n"
86 "\n"
87 " The following options are specific to the test pattern generator:\n"
88 "\n"
89 " --list-patterns list available patterns for use with --pattern\n"
90 " --pattern=<pat> choose output test pattern, the default is 0\n"
91 " --square show a square in the middle of the output test pattern\n"
92 " --border show a border around the pillar/letterboxed video\n"
93 " --sav insert an SAV code in every line\n"
94 " --eav insert an EAV code in every line\n"
95 " --pixel-aspect=<aspect> select a pixel aspect ratio, the default is to autodetect\n"
96 " <aspect> can be one of: square, ntsc, pal\n"
97 " --video-aspect=<aspect> select a video aspect ratio, the default is to use the frame ratio\n"
98 " <aspect> can be one of: 4x3, 14x9, 16x9, anamorphic\n"
99 " --alpha=<alpha-value> value to use for the alpha component, range 0-255, the default is 0\n"
100 " --alpha-red-only only use the --alpha value for the red colors, for all others use 0\n"
101 " --rgb-lim-range encode RGB values as limited [16-235] instead of full range\n"
102 " --hor-speed=<speed> choose speed for horizontal movement, the default is 0\n"
103 " and the range is [-3...3]\n"
104 " --vert-speed=<speed> choose speed for vertical movement, the default is 0\n"
105 " and the range is [-3...3]\n"
106 " --perc-fill=<percentage> percentage of the frame to actually fill. the default is 100%%\n"
107 "\n"
108 " These options use the test pattern generator to test the OpenGL backend:\n"
109 "\n"
110 " --test=<count> test all formats, each test generates <count> frames.\n"
111 " --test-mask=<mask> mask which tests are performed.\n"
112 " <mask> is a bit mask with these values:\n"
113 " 0x01: mask iterating over pixel formats\n"
114 " 0x02: mask iterating over fields\n"
115 " 0x04: mask iterating over colorspaces\n"
116 " 0x08: mask iterating over transfer functions\n"
117 " 0x10: mask iterating over Y'CbCr/HSV encodings\n"
118 " 0x20: mask iterating over quantization ranges\n",
119 V4L_STREAM_PORT);
120 }
121
122 static void usageError(const char *msg)
123 {
124 printf("Missing parameter for %s\n", msg);
125 usage();
126 }
127
128 static void usageInvParm(const char *msg)
129 {
130 printf("Invalid parameter for %s\n", msg);
131 usage();
132 }
133
134 static QString getDeviceName(QString dev, QString &name)
135 {
136 bool ok;
137 name.toInt(&ok);
138 return ok ? QString("%1%2").arg(dev).arg(name) : name;
139 }
140
141 static __u32 parse_field(const QString &s)
142 {
143 if (s == "any") return V4L2_FIELD_ANY;
144 if (s == "none") return V4L2_FIELD_NONE;
145 if (s == "top") return V4L2_FIELD_TOP;
146 if (s == "bottom") return V4L2_FIELD_BOTTOM;
147 if (s == "interlaced") return V4L2_FIELD_INTERLACED;
148 if (s == "seq_tb") return V4L2_FIELD_SEQ_TB;
149 if (s == "seq_bt") return V4L2_FIELD_SEQ_BT;
150 if (s == "alternate") return V4L2_FIELD_ALTERNATE;
151 if (s == "interlaced_tb") return V4L2_FIELD_INTERLACED_TB;
152 if (s == "interlaced_bt") return V4L2_FIELD_INTERLACED_BT;
153 return V4L2_FIELD_ANY;
154 }
155
156 static __u32 parse_colorspace(const QString &s)
157 {
158 if (s == "smpte170m") return V4L2_COLORSPACE_SMPTE170M;
159 if (s == "smpte240m") return V4L2_COLORSPACE_SMPTE240M;
160 if (s == "rec709") return V4L2_COLORSPACE_REC709;
161 if (s == "470m") return V4L2_COLORSPACE_470_SYSTEM_M;
162 if (s == "470bg") return V4L2_COLORSPACE_470_SYSTEM_BG;
163 if (s == "jpeg") return V4L2_COLORSPACE_JPEG;
164 if (s == "srgb") return V4L2_COLORSPACE_SRGB;
165 if (s == "oprgb") return V4L2_COLORSPACE_OPRGB;
166 if (s == "bt2020") return V4L2_COLORSPACE_BT2020;
167 if (s == "dcip3") return V4L2_COLORSPACE_DCI_P3;
168 return 0;
169 }
170
171 static __u32 parse_xfer_func(const QString &s)
172 {
173 if (s == "default") return V4L2_XFER_FUNC_DEFAULT;
174 if (s == "smpte240m") return V4L2_XFER_FUNC_SMPTE240M;
175 if (s == "rec709") return V4L2_XFER_FUNC_709;
176 if (s == "srgb") return V4L2_XFER_FUNC_SRGB;
177 if (s == "oprgb") return V4L2_XFER_FUNC_OPRGB;
178 if (s == "dcip3") return V4L2_XFER_FUNC_DCI_P3;
179 if (s == "smpte2084") return V4L2_XFER_FUNC_SMPTE2084;
180 if (s == "none") return V4L2_XFER_FUNC_NONE;
181 return 0;
182 }
183
184 static __u32 parse_ycbcr(const QString &s)
185 {
186 if (s == "default") return V4L2_YCBCR_ENC_DEFAULT;
187 if (s == "601") return V4L2_YCBCR_ENC_601;
188 if (s == "709") return V4L2_YCBCR_ENC_709;
189 if (s == "xv601") return V4L2_YCBCR_ENC_XV601;
190 if (s == "xv709") return V4L2_YCBCR_ENC_XV709;
191 if (s == "bt2020") return V4L2_YCBCR_ENC_BT2020;
192 if (s == "bt2020c") return V4L2_YCBCR_ENC_BT2020_CONST_LUM;
193 if (s == "smpte240m") return V4L2_YCBCR_ENC_SMPTE240M;
194 return V4L2_YCBCR_ENC_DEFAULT;
195 }
196
197 static __u32 parse_hsv(const QString &s)
198 {
199 if (s == "default") return V4L2_YCBCR_ENC_DEFAULT;
200 if (s == "180") return V4L2_HSV_ENC_180;
201 if (s == "256") return V4L2_HSV_ENC_256;
202 return V4L2_YCBCR_ENC_DEFAULT;
203 }
204
205 static __u32 parse_quantization(const QString &s)
206 {
207 if (s == "default") return V4L2_QUANTIZATION_DEFAULT;
208 if (s == "full-range") return V4L2_QUANTIZATION_FULL_RANGE;
209 if (s == "lim-range") return V4L2_QUANTIZATION_LIM_RANGE;
210 return V4L2_QUANTIZATION_DEFAULT;
211 }
212
213 static __u32 parse_pixel_format(const QString &s)
214 {
215 for (unsigned i = 0; formats[i]; i++)
216 if (s == fcc2s(formats[i]).c_str())
217 return formats[i];
218 return 0;
219 }
220
221 static unsigned findVal(const __u32 *values, __u32 &val)
222 {
223 unsigned idx = 0;
224 __u32 first = *values;
225
226 while (*values) {
227 if (*values == val)
228 return idx;
229 values++;
230 idx++;
231 }
232 val = first;
233 return 0;
234 }
235
236 static void list_formats()
237 {
238 for (unsigned i = 0; formats[i]; i++)
239 printf("'%s':\r\t\t%s\n", fcc2s(formats[i]).c_str(),
240 pixfmt2s(formats[i]).c_str());
241 }
242
243 static bool processOption(const QStringList &args, int &i, QString &s)
244 {
245 int index = -1;
246
247 if (args[i][1] == '-')
248 index = args[i].indexOf('=');
249 else if (args[i].length() > 2)
250 index = 1;
251
252 if (index >= 0) {
253 s = args[i].mid(index + 1);
254 if (s.length() == 0) {
255 usageError(args[i].toUtf8());
256 return false;
257 }
258 return true;
259 }
260 if (i + 1 >= args.size()) {
261 usageError(args[i].toUtf8());
262 return false;
263 }
264 s = args[++i];
265 return true;
266 }
267
268 static bool processOption(const QStringList &args, int &i, unsigned &u)
269 {
270 QString s;
271 bool ok = processOption(args, i, s);
272
273 if (!ok)
274 return ok;
275 u = s.toUInt(&ok, 0);
276 if (!ok)
277 usageInvParm(s.toUtf8());
278 return ok;
279 }
280
281 static bool processOption(const QStringList &args, int &i, int &v)
282 {
283 QString s;
284 bool ok = processOption(args, i, s);
285
286 if (!ok)
287 return ok;
288 v = s.toInt(&ok, 0);
289 if (!ok)
290 usageInvParm(s.toUtf8());
291 return ok;
292 }
293
294 static bool processOption(const QStringList &args, int &i, double &v)
295 {
296 QString s;
297 bool ok = processOption(args, i, s);
298
299 if (!ok)
300 return ok;
301 v = s.toDouble(&ok);
302 if (!ok)
303 usageInvParm(s.toUtf8());
304 return ok;
305 }
306
307 static bool isOptArg(const QString &opt, const char *longOpt, const char *shortOpt = NULL)
308 {
309 return opt.startsWith(longOpt) || (shortOpt && opt.startsWith(shortOpt));
310 }
311
312 static bool isOption(const QString &opt, const char *longOpt, const char *shortOpt = NULL)
313 {
314 return opt == longOpt || opt == shortOpt;
315 }
316
317 __u32 read_u32(int fd)
318 {
319 __u32 v;
320 int n;
321
322 n = read(fd, &v, sizeof(v));
323 if (n != sizeof(v)) {
324 fprintf(stderr, "could not read __u32\n");
325 std::exit(EXIT_FAILURE);
326 }
327 return ntohl(v);
328 }
329
330 int initSocket(int port, cv4l_fmt &fmt, v4l2_fract &pixelaspect)
331 {
332 static int listen_fd = -1;
333 int sock_fd;
334 socklen_t clilen;
335 struct sockaddr_in serv_addr = {}, cli_addr;
336 int val = 1;
337
338 if (listen_fd < 0) {
339 listen_fd = socket(AF_INET, SOCK_STREAM, 0);
340 if (listen_fd < 0) {
341 fprintf(stderr, "could not opening socket\n");
342 std::exit(EXIT_FAILURE);
343 }
344 setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(int));
345
346 serv_addr.sin_family = AF_INET;
347 serv_addr.sin_addr.s_addr = INADDR_ANY;
348 serv_addr.sin_port = htons(port);
349 if (bind(listen_fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
350 fprintf(stderr, "could not bind: %s\n", strerror(errno));
351 std::exit(EXIT_FAILURE);
352 }
353 }
354 listen(listen_fd, 1);
355 clilen = sizeof(cli_addr);
356 sock_fd = accept(listen_fd, (struct sockaddr *)&cli_addr, &clilen);
357 if (sock_fd < 0) {
358 fprintf(stderr, "could not accept\n");
359 std::exit(EXIT_FAILURE);
360 }
361 if (read_u32(sock_fd) != V4L_STREAM_ID) {
362 fprintf(stderr, "unknown protocol ID\n");
363 std::exit(EXIT_FAILURE);
364 }
365 __u32 version = read_u32(sock_fd);
366
367 if (!version || version > V4L_STREAM_VERSION) {
368 fprintf(stderr, "unknown protocol version %u\n", version);
369 std::exit(EXIT_FAILURE);
370 }
371 for (;;) {
372 __u32 packet = read_u32(sock_fd);
373 char buf[1024];
374
375 if (packet == V4L_STREAM_PACKET_END) {
376 fprintf(stderr, "END packet read\n");
377 std::exit(EXIT_FAILURE);
378 }
379
380 if (packet == V4L_STREAM_PACKET_FMT_VIDEO)
381 break;
382
383 unsigned sz = read_u32(sock_fd);
384 while (sz) {
385 unsigned rdsize = sz > sizeof(buf) ? sizeof(buf) : sz;
386 int n;
387
388 n = read(sock_fd, buf, rdsize);
389 if (n < 0) {
390 fprintf(stderr, "error reading %d bytes\n", sz);
391 std::exit(EXIT_FAILURE);
392 }
393 sz -= n;
394 }
395 }
396 read_u32(sock_fd);
397 fmt.s_type(V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
398 unsigned sz = read_u32(sock_fd);
399
400 if (sz != V4L_STREAM_PACKET_FMT_VIDEO_SIZE_FMT) {
401 fprintf(stderr, "unsupported FMT_VIDEO size\n");
402 std::exit(EXIT_FAILURE);
403 }
404 fmt.s_num_planes(read_u32(sock_fd));
405 fmt.s_pixelformat(read_u32(sock_fd));
406 fmt.s_width(read_u32(sock_fd));
407 fmt.s_height(read_u32(sock_fd));
408 fmt.s_field(read_u32(sock_fd));
409 fmt.s_colorspace(read_u32(sock_fd));
410 fmt.s_ycbcr_enc(read_u32(sock_fd));
411 fmt.s_quantization(read_u32(sock_fd));
412 fmt.s_xfer_func(read_u32(sock_fd));
413 fmt.s_flags(read_u32(sock_fd));
414 pixelaspect.numerator = read_u32(sock_fd);
415 pixelaspect.denominator = read_u32(sock_fd);
416
417 for (unsigned i = 0; i < fmt.g_num_planes(); i++) {
418 unsigned sz = read_u32(sock_fd);
419
420 if (sz != V4L_STREAM_PACKET_FMT_VIDEO_SIZE_FMT_PLANE) {
421 fprintf(stderr, "unsupported FMT_VIDEO plane size\n");
422 std::exit(EXIT_FAILURE);
423 }
424 fmt.s_sizeimage(read_u32(sock_fd), i);
425 fmt.s_bytesperline(read_u32(sock_fd), i);
426 }
427 return sock_fd;
428 }
429
430 int main(int argc, char **argv)
431 {
432 QApplication disp(argc, argv);
433 QScrollArea *sa = new QScrollArea; // Automatically freed on window close
434 QSurfaceFormat format;
435 QString video_device = "0";
436 QString filename;
437 cv4l_fd fd;
438 enum AppMode mode = AppModeV4L2;
439 int sock_fd = -1;
440 cv4l_queue q;
441 cv4l_fmt fmt;
442 v4l2_fract pixelaspect = { 1, 1 };
443 unsigned cnt = 0;
444 unsigned v4l2_bufs = 4;
445 bool single_step = false;
446 unsigned single_step_start = 0;
447 int port = 0;
448 bool info_option = false;
449 bool report_timings = false;
450 bool verbose = false;
451 bool no_loop = false;
452 unsigned from_frame = 0;
453 __u32 overridePixelFormat = 0;
454 __u32 overrideWidth = 0;
455 __u32 overrideHeight = 0;
456 __u32 overrideHorPadding = 0;
457 __u32 overrideField = 0xffffffff;
458 __u32 overrideColorspace = 0xffffffff;
459 __u32 overrideYCbCrEnc = 0xffffffff;
460 __u32 overrideHSVEnc = 0xffffffff;
461 __u32 overrideXferFunc = 0xffffffff;
462 __u32 overrideQuantization = 0xffffffff;
463 double fps = 30;
464 unsigned pattern = 0;
465 unsigned test = 0;
466 unsigned test_mask = 0;
467 bool square = false;
468 bool border = false;
469 bool sav = false;
470 bool eav = false;
471 int tpg_pixelaspect = -1;
472 tpg_video_aspect video_aspect = TPG_VIDEO_ASPECT_IMAGE;
473 unsigned alpha = 0;
474 bool alpha_red_only = false;
475 bool rgb_lim_range = false;
476 unsigned perc_fill = 100;
477 tpg_move_mode hor_mode = TPG_MOVE_NONE;
478 tpg_move_mode vert_mode = TPG_MOVE_NONE;
479 bool force_opengl = false;
480 bool force_opengles = false;
481
482 disp.setWindowIcon(QIcon(":/qvidcap.png"));
483 disp.setApplicationDisplayName("V4L2 Viewer");
484 QStringList args = disp.arguments();
485 for (int i = 1; i < args.size(); i++) {
486 QString s;
487
488 if (isOptArg(args[i], "--device", "-d")) {
489 if (!processOption(args, i, video_device))
490 return 0;
491 mode = AppModeV4L2;
492 } else if (isOptArg(args[i], "--file", "-f")) {
493 if (!processOption(args, i, filename))
494 return 0;
495 mode = AppModeFile;
496 } else if (isOption(args[i], "--port", "-p")) {
497 mode = AppModeSocket;
498 port = V4L_STREAM_PORT;
499 } else if (isOptArg(args[i], "--port", "-p")) {
500 if (!processOption(args, i, port))
501 return 0;
502 mode = AppModeSocket;
503 } else if (isOption(args[i], "--tpg", "-T")) {
504 mode = AppModeTPG;
505 } else if (isOptArg(args[i], "--test-mask")) {
506 if (!processOption(args, i, test_mask))
507 return 0;
508 } else if (isOptArg(args[i], "--test")) {
509 if (!processOption(args, i, test))
510 return 0;
511 mode = AppModeTest;
512 test &= ~1;
513 if (test == 0)
514 test = 2;
515 if (!pattern)
516 pattern = TPG_PAT_CSC_COLORBAR;
517 } else if (isOptArg(args[i], "--pixelformat", "-P")) {
518 if (!processOption(args, i, s))
519 return 0;
520 overridePixelFormat = parse_pixel_format(s);
521 } else if (isOptArg(args[i], "--width", "-W")) {
522 if (!processOption(args, i, overrideWidth))
523 return 0;
524 } else if (isOptArg(args[i], "--height", "-H")) {
525 if (!processOption(args, i, overrideHeight))
526 return 0;
527 } else if (isOptArg(args[i], "--padding", "-A")) {
528 if (!processOption(args, i, overrideHorPadding))
529 return 0;
530 } else if (isOptArg(args[i], "--field", "-F")) {
531 if (!processOption(args, i, s))
532 return 0;
533 overrideField = parse_field(s);
534 } else if (isOptArg(args[i], "--colorspace", "-C")) {
535 if (!processOption(args, i, s))
536 return 0;
537 overrideColorspace = parse_colorspace(s);
538 } else if (isOptArg(args[i], "--ycbcr-enc", "-Y")) {
539 if (!processOption(args, i, s))
540 return 0;
541 overrideYCbCrEnc = parse_ycbcr(s);
542 } else if (isOptArg(args[i], "--hsv-enc", "-H")) {
543 if (!processOption(args, i, s))
544 return 0;
545 overrideHSVEnc = parse_hsv(s);
546 } else if (isOptArg(args[i], "--xfer-func", "-X")) {
547 if (!processOption(args, i, s))
548 return 0;
549 overrideXferFunc = parse_xfer_func(s);
550 } else if (isOptArg(args[i], "--quant", "-Q")) {
551 if (!processOption(args, i, s))
552 return 0;
553 overrideQuantization = parse_quantization(s);
554 } else if (isOption(args[i], "--list-patterns")) {
555 printf("List of available patterns:\n");
556 for (unsigned i = 0; tpg_pattern_strings[i]; i++)
557 printf("\t%2d: %s\n", i, tpg_pattern_strings[i]);
558 info_option = true;
559 } else if (isOptArg(args[i], "--fps")) {
560 if (!processOption(args, i, fps))
561 return 0;
562 if (fps <= 0)
563 fps = 30;
564 } else if (isOptArg(args[i], "--pattern")) {
565 if (!processOption(args, i, pattern))
566 return 0;
567 } else if (isOption(args[i], "--square")) {
568 } else if (args[i] == "--square") {
569 square = true;
570 } else if (isOption(args[i], "--border")) {
571 border = true;
572 } else if (isOption(args[i], "--sav")) {
573 sav = true;
574 } else if (isOption(args[i], "--eav")) {
575 eav = true;
576 } else if (isOptArg(args[i], "--pixel-aspect")) {
577 if (!processOption(args, i, s))
578 return 0;
579 if (s == "square") {
580 tpg_pixelaspect = TPG_PIXEL_ASPECT_SQUARE;
581 } else if (s == "ntsc") {
582 static const v4l2_fract hz50 = { 11, 12 };
583 pixelaspect = hz50;
584 tpg_pixelaspect = TPG_PIXEL_ASPECT_NTSC;
585 } else if (s == "pal") {
586 static const v4l2_fract hz60 = { 11, 10 };
587 pixelaspect = hz60;
588 tpg_pixelaspect = TPG_PIXEL_ASPECT_PAL;
589 } else {
590 usage();
591 return 0;
592 }
593 } else if (isOptArg(args[i], "--video-aspect")) {
594 if (!processOption(args, i, s))
595 return 0;
596 if (s == "4x3")
597 video_aspect = TPG_VIDEO_ASPECT_4X3;
598 else if (s == "14x9")
599 video_aspect = TPG_VIDEO_ASPECT_14X9_CENTRE;
600 else if (s == "16x9")
601 video_aspect = TPG_VIDEO_ASPECT_16X9_CENTRE;
602 else if (s == "anamorphic")
603 video_aspect = TPG_VIDEO_ASPECT_16X9_ANAMORPHIC;
604 else {
605 usage();
606 return 0;
607 }
608 } else if (isOption(args[i], "--alpha-red-only")) {
609 alpha_red_only = true;
610 } else if (isOptArg(args[i], "--alpha")) {
611 if (!processOption(args, i, alpha))
612 return 0;
613 if (alpha > 255)
614 alpha = 255;
615 } else if (isOption(args[i], "--rgb-lim-range")) {
616 rgb_lim_range = true;
617 } else if (isOptArg(args[i], "--hor-speed")) {
618 int speed;
619
620 if (!processOption(args, i, speed))
621 return 0;
622 if (speed < -3)
623 speed = -3;
624 if (speed > 3)
625 speed = 3;
626 hor_mode = (tpg_move_mode)(speed + 3);
627 } else if (isOptArg(args[i], "--vert-speed")) {
628 int speed;
629
630 if (!processOption(args, i, speed))
631 return 0;
632 if (speed < -3)
633 speed = -3;
634 if (speed > 3)
635 speed = 3;
636 vert_mode = (tpg_move_mode)(speed + 3);
637 } else if (isOptArg(args[i], "--perc-fill")) {
638 if (!processOption(args, i, perc_fill))
639 return 0;
640 if (perc_fill > 100)
641 perc_fill = 100;
642 } else if (isOption(args[i], "--help", "-h")) {
643 usage();
644 info_option = true;
645 } else if (isOption(args[i], "--list-formats", "-l")) {
646 list_formats();
647 info_option = true;
648 } else if (isOption(args[i], "--timings", "-t")) {
649 report_timings = true;
650 } else if (isOptArg(args[i], "--opengles")) {
651 force_opengles = true;
652 } else if (isOptArg(args[i], "--opengl")) {
653 force_opengl = true;
654 } else if (isOption(args[i], "--verbose", "-v")) {
655 verbose = true;
656 } else if (isOption(args[i], "--no-loop")) {
657 no_loop = true;
658 } else if (isOptArg(args[i], "--from-frame")) {
659 if (!processOption(args, i, from_frame))
660 return 0;
661 } else if (isOption(args[i], "--raw", "-R")) {
662 fd.s_direct(true);
663 } else if (isOptArg(args[i], "--count", "-c")) {
664 if (!processOption(args, i, cnt))
665 return 0;
666 } else if (isOptArg(args[i], "--buffers", "-b")) {
667 if (!processOption(args, i, v4l2_bufs))
668 return 0;
669 } else if (isOption(args[i], "--single-step", "-s")) {
670 single_step = true;
671 single_step_start = 0;
672 } else if (isOptArg(args[i], "--single-step", "-s")) {
673 if (!processOption(args, i, single_step_start))
674 return 0;
675 single_step = true;
676 } else {
677 printf("Invalid argument %s\n", args[i].toUtf8().data());
678 return 0;
679 }
680 }
681 if (info_option)
682 return 0;
683
684 if (mode == AppModeV4L2) {
685 fps = 0;
686 video_device = getDeviceName("/dev/video", video_device);
687 if (fd.open(video_device.toUtf8().data(), true) < 0) {
688 perror((QString("could not open ") + video_device).toUtf8().data());
689 std::exit(EXIT_FAILURE);
690 }
691 if (!fd.has_vid_cap()) {
692 fprintf(stderr, "%s is not a video capture device\n", video_device.toUtf8().data());
693 std::exit(EXIT_FAILURE);
694 }
695 fd.g_fmt(fmt);
696
697 if (!overridePixelFormat) {
698 bool found = false;
699
700 for (unsigned i = 0; formats[i]; i++) {
701 if (fmt.g_pixelformat() == formats[i]) {
702 found = true;
703 break;
704 }
705 }
706 if (!found)
707 overridePixelFormat = V4L2_PIX_FMT_RGB24;
708 }
709
710 if (overridePixelFormat) {
711 fmt.s_pixelformat(overridePixelFormat);
712 fd.s_fmt(fmt);
713 fd.g_fmt(fmt);
714 if (fmt.g_pixelformat() != overridePixelFormat) {
715 fprintf(stderr, "Could not set format: '%s' %s\n",
716 fcc2s(overridePixelFormat).c_str(),
717 pixfmt2s(overridePixelFormat).c_str());
718 fprintf(stderr, "Fall back to format: '%s' %s\n",
719 fcc2s(fmt.g_pixelformat()).c_str(),
720 pixfmt2s(fmt.g_pixelformat()).c_str());
721 }
722 }
723
724 unsigned tmp_w, tmp_h;
725
726 pixelaspect = fd.g_pixel_aspect(tmp_w, tmp_h);
727 } else if (mode == AppModeSocket) {
728 fps = 0;
729 sock_fd = initSocket(port, fmt, pixelaspect);
730 } else {
731 fmt.s_type(V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
732 fmt.s_num_planes(1);
733 fmt.s_pixelformat(V4L2_PIX_FMT_RGB24);
734 fmt.s_width(640);
735 fmt.s_height(360);
736 fmt.s_field(V4L2_FIELD_NONE);
737 fmt.s_colorspace(V4L2_COLORSPACE_SRGB);
738 fmt.s_xfer_func(V4L2_XFER_FUNC_DEFAULT);
739 fmt.s_ycbcr_enc(V4L2_YCBCR_ENC_DEFAULT);
740 fmt.s_quantization(V4L2_QUANTIZATION_DEFAULT);
741 fmt.s_bytesperline(3 * fmt.g_width());
742 fmt.s_sizeimage(fmt.g_bytesperline() * fmt.g_height());
743 }
744
745 format.setDepthBufferSize(24);
746
747 if (force_opengles)
748 format.setRenderableType(QSurfaceFormat::OpenGLES);
749 else if (force_opengl)
750 format.setRenderableType(QSurfaceFormat::OpenGL);
751 format.setProfile(QSurfaceFormat::CoreProfile);
752 format.setVersion(3, 3);
753
754 QSurfaceFormat::setDefaultFormat(format);
755 CaptureWin win(sa);
756 win.setVerbose(verbose);
757 win.setNoLoop(no_loop);
758 win.setFromFrame(from_frame);
759 if (mode == AppModeFile) {
760 win.setModeFile(filename);
761 } else if (mode == AppModeV4L2) {
762 win.setModeV4L2(&fd);
763 } else if (mode == AppModeTPG) {
764 win.setModeTPG();
765 }
766 win.setOverrideWidth(overrideWidth);
767 win.setOverrideHeight(overrideHeight);
768 win.setOverrideHorPadding(overrideHorPadding);
769 win.setFps(fps);
770 win.setFormat(format);
771 win.setReportTimings(report_timings);
772 win.setCount(test ? test : cnt);
773 if (mode == AppModeTest) {
774 win.setModeTest(test);
775
776 TestState state = { };
777
778 state.fmt_idx = findVal(formats, overridePixelFormat);
779 state.field_idx = findVal(fields, overrideField);
780 state.colorspace_idx = findVal(colorspaces, overrideColorspace);
781 state.xfer_func_idx = findVal(xfer_funcs, overrideXferFunc);
782 state.ycbcr_enc_idx = findVal(ycbcr_encs, overrideYCbCrEnc);
783 state.hsv_enc_idx = findVal(hsv_encs, overrideHSVEnc);
784 state.quant_idx = findVal(quantizations, overrideQuantization);
785 state.mask = test_mask;
786 win.setTestState(state);
787 }
788
789 win.setOverridePixelFormat(overridePixelFormat);
790 win.setOverrideField(overrideField);
791 win.setOverrideColorspace(overrideColorspace);
792 win.setOverrideYCbCrEnc(overrideYCbCrEnc);
793 win.setOverrideHSVEnc(overrideHSVEnc);
794 win.setOverrideXferFunc(overrideXferFunc);
795 win.setOverrideQuantization(overrideQuantization);
796 while (!win.setV4LFormat(fmt)) {
797 fprintf(stderr, "Unsupported format: '%s' %s\n",
798 fcc2s(fmt.g_pixelformat()).c_str(),
799 pixfmt2s(fmt.g_pixelformat()).c_str());
800 if (mode != AppModeSocket)
801 std::exit(EXIT_FAILURE);
802 sock_fd = initSocket(port, fmt, pixelaspect);
803 }
804 win.setPixelAspect(pixelaspect);
805 win.setMinimumSize(16, 16);
806 win.setSizeIncrement(2, 2);
807 win.resize(fmt.g_width(), fmt.g_frame_height());
808 win.setFocusPolicy(Qt::StrongFocus);
809 if (single_step && mode != AppModeTest)
810 win.setSingleStepStart(single_step_start);
811
812 sa->setAlignment(Qt::AlignVCenter | Qt::AlignHCenter);
813 sa->setWidget(win.window());
814 sa->setFrameShape(QFrame::NoFrame);
815 sa->resize(win.correctAspect(QSize(fmt.g_width(), fmt.g_frame_height())));
816 sa->setWidgetResizable(true);
817
818 if (mode == AppModeSocket)
819 win.setModeSocket(sock_fd, port);
820 else if (mode == AppModeV4L2) {
821 q.init(fd.g_type(), V4L2_MEMORY_MMAP);
822 q.reqbufs(&fd, v4l2_bufs);
823 q.obtain_bufs(&fd);
824 q.queue_all(&fd);
825 win.setQueue(&q);
826 if (fd.streamon())
827 std::exit(EXIT_FAILURE);
828 } else {
829 struct tpg_data *tpg = win.getTPG();
830
831 tpg_init(tpg, fmt.g_width(), fmt.g_height());
832 tpg_alloc(tpg, fmt.g_width());
833 tpg_s_pattern(tpg, (tpg_pattern)pattern);
834 tpg_s_mv_hor_mode(tpg, hor_mode);
835 tpg_s_mv_vert_mode(tpg, vert_mode);
836 tpg_s_show_square(tpg, square);
837 tpg_s_show_border(tpg, border);
838 tpg_s_insert_sav(tpg, sav);
839 tpg_s_insert_eav(tpg, eav);
840 tpg_s_perc_fill(tpg, perc_fill);
841 if (rgb_lim_range)
842 tpg_s_real_rgb_range(tpg, V4L2_DV_RGB_RANGE_LIMITED);
843 tpg_s_alpha_component(tpg, alpha);
844 tpg_s_alpha_mode(tpg, alpha_red_only);
845 tpg_s_video_aspect(tpg, video_aspect);
846 switch (tpg_pixelaspect) {
847 case -1:
848 break;
849 default:
850 tpg_s_pixel_aspect(tpg, (tpg_pixel_aspect)tpg_pixelaspect);
851 break;
852 }
853 win.startTimer();
854 }
855 sa->show();
856 return disp.exec();
857 }