Debian Codegraph @

utils/v4l2-ctl/v4l2-ctl-vidout.cpp

raw
1 #include "v4l2-ctl.h"
2
3 static unsigned set_fmts_out;
4 static __u32 width, height, pixfmt, field, colorspace, xfer_func, ycbcr, quantization, flags;
5 static __u32 bytesperline[VIDEO_MAX_PLANES];
6 static __u32 sizeimage[VIDEO_MAX_PLANES];
7 static unsigned mbus_code_out;
8 static bool enum_all;
9
10 void vidout_usage()
11 {
12 printf("\nVideo Output Formats options:\n"
13 " --list-formats-out [<mbus_code>|all] display supported video output formats.\n"
14 " <mbus_code> is an optional media bus code, if the device has\n"
15 " capability V4L2_CAP_IO_MC then only formats that support this\n"
16 " media bus code are listed.\n"
17 " When 'all' is specified it enumerates all pixel formats if\n"
18 " V4L2_FMTDESC_FLAG_ENUM_ALL flag is supported by the driver.\n"
19 " [VIDIOC_ENUM_FMT]\n"
20 " --list-formats-out-ext [<mbus_code>|all] display supported video output formats including\n"
21 " frame sizes and intervals. <mbus_code> is an optional media bus code,\n"
22 " if the device has capability V4L2_CAP_IO_MC then only formats that\n"
23 " support this media bus code are listed\n"
24 " When 'all' is specified it enumerates all pixel formats if\n"
25 " V4L2_FMTDESC_FLAG_ENUM_ALL flag is supported by the driver.\n"
26 " [VIDIOC_ENUM_FMT]\n"
27 " --list-fields-out list supported fields for the current output format\n"
28 " -X, --get-fmt-video-out\n"
29 " query the video output format [VIDIOC_G_FMT]\n"
30 " -x, --set-fmt-video-out\n"
31 " --try-fmt-video-out width=<w>,height=<h>,pixelformat=<pf>,field=<f>,colorspace=<c>,\n"
32 " xfer=<xf>,ycbcr=<y>,hsv=<hsv>,quantization=<q>,\n"
33 " premul-alpha=<0/1>,bytesperline=<bpl>,sizeimage=<sz>\n"
34 " set/try the video output format [VIDIOC_S/TRY_FMT]\n"
35 " pixelformat is either the format index as reported by\n"
36 " --list-formats-out, or the fourcc value as a string.\n"
37 " premul-alpha sets (1) or clears (0) V4L2_PIX_FMT_FLAG_PREMUL_ALPHA.\n"
38 " The bytesperline and sizeimage options can be used multiple times,\n"
39 " once for each plane.\n"
40 " <f> can be one of the following field layouts:\n"
41 " any, none, top, bottom, interlaced, seq_tb, seq_bt,\n"
42 " alternate, interlaced_tb, interlaced_bt\n"
43 " <c> can be one of the following colorspaces:\n"
44 " smpte170m, smpte240m, rec709, 470m, 470bg, jpeg, srgb,\n"
45 " oprgb, bt2020, dcip3\n"
46 " <xf> can be one of the following transfer functions:\n"
47 " default, 709, srgb, oprgb, smpte240m, smpte2084, dcip3, none\n"
48 " <y> can be one of the following Y'CbCr encodings:\n"
49 " default, 601, 709, xv601, xv709, bt2020, bt2020c, smpte240m\n"
50 " <hsv> can be one of the following HSV encodings:\n"
51 " default, 180, 256\n"
52 " <q> can be one of the following quantization methods:\n"
53 " default, full-range, lim-range\n"
54 );
55 }
56
57 static void print_video_out_fields(int fd)
58 {
59 struct v4l2_format fmt;
60 struct v4l2_format tmp;
61
62 memset(&fmt, 0, sizeof(fmt));
63 fmt.fmt.pix.priv = priv_magic;
64 fmt.type = vidout_buftype;
65 if (test_ioctl(fd, VIDIOC_G_FMT, &fmt) < 0)
66 return;
67
68 printf("Supported Video Output Fields:\n");
69 for (__u32 f = V4L2_FIELD_NONE; f <= V4L2_FIELD_INTERLACED_BT; f++) {
70 bool ok;
71
72 tmp = fmt;
73 if (is_multiplanar)
74 tmp.fmt.pix_mp.field = f;
75 else
76 tmp.fmt.pix.field = f;
77 if (test_ioctl(fd, VIDIOC_TRY_FMT, &tmp) < 0)
78 continue;
79 if (is_multiplanar)
80 ok = tmp.fmt.pix_mp.field == f;
81 else
82 ok = tmp.fmt.pix.field == f;
83 if (ok)
84 printf("\t%s\n", field2s(f).c_str());
85 }
86 }
87
88 void vidout_cmd(int ch, char *optarg)
89 {
90 switch (ch) {
91 case OptSetVideoOutFormat:
92 case OptTryVideoOutFormat:
93 set_fmts_out = parse_fmt(optarg, width, height, pixfmt, field,
94 colorspace, xfer_func, ycbcr, quantization, flags, bytesperline,
95 sizeimage);
96 if (!set_fmts_out) {
97 vidcap_usage();
98 std::exit(EXIT_FAILURE);
99 }
100 break;
101 case OptListOutFormats:
102 case OptListOutFormatsExt:
103 if (optarg) {
104 if (strstr(optarg , "all"))
105 enum_all = true;
106 else
107 mbus_code_out = strtoul(optarg, nullptr, 0);
108 }
109 break;
110 }
111 }
112
113 void vidout_set(cv4l_fd &_fd)
114 {
115 int fd = _fd.g_fd();
116 int ret;
117
118 if (options[OptSetVideoOutFormat] || options[OptTryVideoOutFormat]) {
119 struct v4l2_format vfmt;
120
121 memset(&vfmt, 0, sizeof(vfmt));
122 vfmt.fmt.pix.priv = priv_magic;
123 vfmt.type = vidout_buftype;
124 if (doioctl(fd, VIDIOC_G_FMT, &vfmt) == 0) {
125 if (is_multiplanar) {
126 if (set_fmts_out & FmtWidth)
127 vfmt.fmt.pix_mp.width = width;
128 if (set_fmts_out & FmtHeight)
129 vfmt.fmt.pix_mp.height = height;
130 if (set_fmts_out & FmtPixelFormat) {
131 vfmt.fmt.pix_mp.pixelformat = pixfmt;
132 if (vfmt.fmt.pix_mp.pixelformat < 256) {
133 vfmt.fmt.pix_mp.pixelformat = pixfmt =
134 find_pixel_format(fd, vfmt.fmt.pix_mp.pixelformat,
135 true, true);
136 }
137 }
138 if (set_fmts_out & FmtField)
139 vfmt.fmt.pix_mp.field = field;
140 if (set_fmts_out & FmtColorspace)
141 vfmt.fmt.pix_mp.colorspace = colorspace;
142 if (set_fmts_out & FmtXferFunc)
143 vfmt.fmt.pix_mp.xfer_func = xfer_func;
144 if (set_fmts_out & FmtYCbCr)
145 vfmt.fmt.pix_mp.ycbcr_enc = ycbcr;
146 if (set_fmts_out & FmtQuantization)
147 vfmt.fmt.pix_mp.quantization = quantization;
148 if (set_fmts_out & FmtFlags)
149 vfmt.fmt.pix_mp.flags = flags;
150 if (set_fmts_out & FmtBytesPerLine) {
151 for (unsigned i = 0; i < VIDEO_MAX_PLANES; i++)
152 vfmt.fmt.pix_mp.plane_fmt[i].bytesperline =
153 bytesperline[i];
154 } else {
155 /* G_FMT might return bytesperline values > width,
156 * reset them to 0 to force the driver to update them
157 * to the closest value for the new width. */
158 for (unsigned i = 0; i < vfmt.fmt.pix_mp.num_planes; i++)
159 vfmt.fmt.pix_mp.plane_fmt[i].bytesperline = 0;
160 }
161 if (set_fmts_out & FmtSizeImage) {
162 for (unsigned i = 0; i < VIDEO_MAX_PLANES; i++)
163 vfmt.fmt.pix_mp.plane_fmt[i].sizeimage =
164 sizeimage[i];
165 }
166 } else {
167 if (set_fmts_out & FmtWidth)
168 vfmt.fmt.pix.width = width;
169 if (set_fmts_out & FmtHeight)
170 vfmt.fmt.pix.height = height;
171 if (set_fmts_out & FmtPixelFormat) {
172 vfmt.fmt.pix.pixelformat = pixfmt;
173 if (vfmt.fmt.pix.pixelformat < 256) {
174 vfmt.fmt.pix.pixelformat = pixfmt =
175 find_pixel_format(fd, vfmt.fmt.pix.pixelformat,
176 true, false);
177 }
178 }
179 if (set_fmts_out & FmtField)
180 vfmt.fmt.pix.field = field;
181 if (set_fmts_out & FmtColorspace)
182 vfmt.fmt.pix.colorspace = colorspace;
183 if (set_fmts_out & FmtXferFunc)
184 vfmt.fmt.pix.xfer_func = xfer_func;
185 if (set_fmts_out & FmtYCbCr)
186 vfmt.fmt.pix.ycbcr_enc = ycbcr;
187 if (set_fmts_out & FmtQuantization)
188 vfmt.fmt.pix.quantization = quantization;
189 if (set_fmts_out & FmtFlags)
190 vfmt.fmt.pix.flags = flags;
191 if (set_fmts_out & FmtBytesPerLine) {
192 vfmt.fmt.pix.bytesperline = bytesperline[0];
193 } else {
194 /* G_FMT might return a bytesperline value > width,
195 * reset this to 0 to force the driver to update it
196 * to the closest value for the new width. */
197 vfmt.fmt.pix.bytesperline = 0;
198 }
199 if (set_fmts_out & FmtSizeImage)
200 vfmt.fmt.pix.sizeimage = sizeimage[0];
201 }
202
203 if ((set_fmts_out & FmtPixelFormat) &&
204 !valid_pixel_format(fd, pixfmt, true, is_multiplanar)) {
205 if (pixfmt)
206 fprintf(stderr, "The pixelformat '%s' is invalid\n",
207 fcc2s(pixfmt).c_str());
208 else
209 fprintf(stderr, "The pixelformat index was invalid\n");
210 std::exit(EXIT_FAILURE);
211 }
212
213 if (options[OptSetVideoOutFormat])
214 ret = doioctl(fd, VIDIOC_S_FMT, &vfmt);
215 else
216 ret = doioctl(fd, VIDIOC_TRY_FMT, &vfmt);
217 if (ret == 0 && (verbose || options[OptTryVideoOutFormat]))
218 printfmt(fd, vfmt);
219 }
220 }
221 }
222
223 void vidout_get(cv4l_fd &fd)
224 {
225 if (options[OptGetVideoOutFormat]) {
226 struct v4l2_format vfmt;
227
228 memset(&vfmt, 0, sizeof(vfmt));
229 vfmt.fmt.pix.priv = priv_magic;
230 vfmt.type = vidout_buftype;
231 if (doioctl(fd.g_fd(), VIDIOC_G_FMT, &vfmt) == 0)
232 printfmt(fd.g_fd(), vfmt);
233 }
234 }
235
236 void vidout_list(cv4l_fd &fd)
237 {
238 if (options[OptListOutFormats]) {
239 printf("ioctl: VIDIOC_ENUM_FMT\n");
240 print_video_formats(fd, vidout_buftype, mbus_code_out, enum_all);
241 }
242
243 if (options[OptListOutFormatsExt]) {
244 printf("ioctl: VIDIOC_ENUM_FMT\n");
245 print_video_formats_ext(fd, vidout_buftype, mbus_code_out, enum_all);
246 }
247
248 if (options[OptListOutFields]) {
249 print_video_out_fields(fd.g_fd());
250 }
251 }