Debian Codegraph @

utils/v4l2-ctl/v4l2-ctl-vbi.cpp

raw
1 #include <cstring>
2
3 #include "compiler.h"
4 #include "v4l2-ctl.h"
5
6 static struct v4l2_format sliced_fmt; /* set_format/get_format for sliced VBI */
7 static struct v4l2_format sliced_fmt_out; /* set_format/get_format for sliced VBI output */
8 static struct v4l2_format raw_fmt; /* set_format/get_format for VBI */
9 static struct v4l2_format raw_fmt_out; /* set_format/get_format for VBI output */
10
11 void vbi_usage()
12 {
13 printf("\nVBI Formats options:\n"
14 " --get-sliced-vbi-cap\n"
15 " query the sliced VBI capture capabilities\n"
16 " [VIDIOC_G_SLICED_VBI_CAP]\n"
17 " --get-sliced-vbi-out-cap\n"
18 " query the sliced VBI output capabilities\n"
19 " [VIDIOC_G_SLICED_VBI_CAP]\n"
20 " -B, --get-fmt-sliced-vbi\n"
21 " query the sliced VBI capture format [VIDIOC_G_FMT]\n"
22 " --get-fmt-sliced-vbi-out\n"
23 " query the sliced VBI output format [VIDIOC_G_FMT]\n"
24 " -b, --set-fmt-sliced-vbi\n"
25 " --try-fmt-sliced-vbi\n"
26 " --set-fmt-sliced-vbi-out\n"
27 " --try-fmt-sliced-vbi-out <mode>\n"
28 " set/try the sliced VBI capture/output format to <mode>\n"
29 " [VIDIOC_S/TRY_FMT], <mode> is a comma separated list of:\n"
30 " off: turn off sliced VBI (cannot be combined with\n"
31 " other modes)\n"
32 " teletext: teletext (PAL/SECAM)\n"
33 " cc: closed caption (NTSC)\n"
34 " wss: widescreen signal (PAL/SECAM)\n"
35 " vps: VPS (PAL/SECAM)\n"
36 " --get-fmt-vbi query the VBI capture format [VIDIOC_G_FMT]\n"
37 " --get-fmt-vbi-out query the VBI output format [VIDIOC_G_FMT]\n"
38 " --set-fmt-vbi\n"
39 " --try-fmt-vbi\n"
40 " --set-fmt-vbi-out\n"
41 " --try-fmt-vbi-out samplingrate=<r>,offset=<o>,samplesperline=<spl>,\n"
42 " start0=<s0>,count0=<c0>,start1=<s1>,count1=<c1>\n"
43 " set/try the raw VBI capture/output format [VIDIOC_S/TRY_FMT]\n"
44 " samplingrate: samples per second\n"
45 " offset: horizontal offset in samples\n"
46 " samplesperline: samples per line\n"
47 " start0: start line number of the first field\n"
48 " count0: number of lines in the first field\n"
49 " start1: start line number of the second field\n"
50 " count1: number of lines in the second field\n"
51 );
52 }
53
54 static void print_sliced_vbi_cap(struct v4l2_sliced_vbi_cap &cap)
55 {
56 printf("\tType : %s\n", buftype2s(cap.type).c_str());
57 printf("\tService Set : %s\n",
58 service2s(cap.service_set).c_str());
59 for (int i = 0; i < 24; i++) {
60 printf("\tService Line %2d: %8s / %-8s\n", i,
61 service2s(cap.service_lines[0][i]).c_str(),
62 service2s(cap.service_lines[1][i]).c_str());
63 }
64 }
65
66 void vbi_cmd(int ch, char *optarg)
67 {
68 char *value, *subs;
69 bool found_off = false;
70 v4l2_format *sliced = &sliced_fmt;
71 v4l2_format *raw = &raw_fmt;
72
73 switch (ch) {
74 case OptSetSlicedVbiOutFormat:
75 case OptTrySlicedVbiOutFormat:
76 sliced = &sliced_fmt_out;
77 fallthrough;
78 case OptSetSlicedVbiFormat:
79 case OptTrySlicedVbiFormat:
80 sliced->fmt.sliced.service_set = 0;
81 if (optarg[0] == 0) {
82 fprintf(stderr, "empty string\n");
83 vbi_usage();
84 std::exit(EXIT_FAILURE);
85 }
86 while (*optarg) {
87 subs = std::strchr(optarg, ',');
88 if (subs)
89 *subs = 0;
90
91 if (!strcmp(optarg, "off"))
92 found_off = true;
93 else if (!strcmp(optarg, "teletext"))
94 sliced->fmt.sliced.service_set |=
95 V4L2_SLICED_TELETEXT_B;
96 else if (!strcmp(optarg, "cc"))
97 sliced->fmt.sliced.service_set |=
98 V4L2_SLICED_CAPTION_525;
99 else if (!strcmp(optarg, "wss"))
100 sliced->fmt.sliced.service_set |=
101 V4L2_SLICED_WSS_625;
102 else if (!strcmp(optarg, "vps"))
103 sliced->fmt.sliced.service_set |=
104 V4L2_SLICED_VPS;
105 else
106 vbi_usage();
107 if (subs == nullptr)
108 break;
109 optarg = subs + 1;
110 }
111 if (found_off && sliced->fmt.sliced.service_set) {
112 fprintf(stderr, "Sliced VBI mode 'off' cannot be combined with other modes\n");
113 vbi_usage();
114 std::exit(EXIT_FAILURE);
115 }
116 break;
117 case OptSetVbiOutFormat:
118 case OptTryVbiOutFormat:
119 raw = &raw_fmt_out;
120 fallthrough;
121 case OptSetVbiFormat:
122 case OptTryVbiFormat:
123 subs = optarg;
124 memset(&raw->fmt.vbi, 0, sizeof(raw->fmt.vbi));
125 while (*subs != '\0') {
126 static constexpr const char *subopts[] = {
127 "samplingrate",
128 "offset",
129 "samplesperline",
130 "start0",
131 "start1",
132 "count0",
133 "count1",
134 nullptr
135 };
136
137 switch (parse_subopt(&subs, subopts, &value)) {
138 case 0:
139 raw->fmt.vbi.sampling_rate = strtoul(value, nullptr, 0);
140 break;
141 case 1:
142 raw->fmt.vbi.offset = strtoul(value, nullptr, 0);
143 break;
144 case 2:
145 raw->fmt.vbi.samples_per_line = strtoul(value, nullptr, 0);
146 break;
147 case 3:
148 raw->fmt.vbi.start[0] = strtoul(value, nullptr, 0);
149 break;
150 case 4:
151 raw->fmt.vbi.start[1] = strtoul(value, nullptr, 0);
152 break;
153 case 5:
154 raw->fmt.vbi.count[0] = strtoul(value, nullptr, 0);
155 break;
156 case 6:
157 raw->fmt.vbi.count[1] = strtoul(value, nullptr, 0);
158 break;
159 default:
160 vbi_usage();
161 break;
162 }
163 }
164 break;
165 }
166 }
167
168 static void fill_raw_vbi(v4l2_vbi_format &dst, const v4l2_vbi_format &src)
169 {
170 if (src.sampling_rate)
171 dst.sampling_rate = src.sampling_rate;
172 if (src.offset)
173 dst.offset = src.offset;
174 if (src.samples_per_line)
175 dst.samples_per_line = src.samples_per_line;
176 if (src.start[0])
177 dst.start[0] = src.start[0];
178 if (src.start[1])
179 dst.start[1] = src.start[1];
180 if (src.count[0])
181 dst.count[0] = src.count[0];
182 if (src.count[1])
183 dst.count[1] = src.count[1];
184 }
185
186 void vbi_set(cv4l_fd &_fd)
187 {
188 int fd = _fd.g_fd();
189 int ret;
190
191 if (options[OptSetSlicedVbiFormat] || options[OptTrySlicedVbiFormat]) {
192 sliced_fmt.type = V4L2_BUF_TYPE_SLICED_VBI_CAPTURE;
193 if (options[OptSetSlicedVbiFormat])
194 ret = doioctl(fd, VIDIOC_S_FMT, &sliced_fmt);
195 else
196 ret = doioctl(fd, VIDIOC_TRY_FMT, &sliced_fmt);
197 if (ret == 0 && (verbose || options[OptTrySlicedVbiFormat]))
198 printfmt(fd, sliced_fmt);
199 }
200
201 if (options[OptSetSlicedVbiOutFormat] || options[OptTrySlicedVbiOutFormat]) {
202 sliced_fmt_out.type = V4L2_BUF_TYPE_SLICED_VBI_OUTPUT;
203 if (options[OptSetSlicedVbiOutFormat])
204 ret = doioctl(fd, VIDIOC_S_FMT, &sliced_fmt_out);
205 else
206 ret = doioctl(fd, VIDIOC_TRY_FMT, &sliced_fmt_out);
207 if (ret == 0 && (verbose || options[OptTrySlicedVbiOutFormat]))
208 printfmt(fd, sliced_fmt_out);
209 }
210
211 if (options[OptSetVbiFormat] || options[OptTryVbiFormat]) {
212 v4l2_format fmt;
213
214 fmt.type = V4L2_BUF_TYPE_VBI_CAPTURE;
215 doioctl(fd, VIDIOC_G_FMT, &fmt);
216 fill_raw_vbi(fmt.fmt.vbi, raw_fmt.fmt.vbi);
217 if (options[OptSetVbiFormat])
218 ret = doioctl(fd, VIDIOC_S_FMT, &fmt);
219 else
220 ret = doioctl(fd, VIDIOC_TRY_FMT, &fmt);
221 if (ret == 0 && (verbose || options[OptTryVbiFormat]))
222 printfmt(fd, fmt);
223 }
224
225 if (options[OptSetVbiOutFormat] || options[OptTryVbiOutFormat]) {
226 v4l2_format fmt;
227
228 fmt.type = V4L2_BUF_TYPE_VBI_OUTPUT;
229 doioctl(fd, VIDIOC_G_FMT, &fmt);
230 fill_raw_vbi(fmt.fmt.vbi, raw_fmt.fmt.vbi);
231 if (options[OptSetVbiOutFormat])
232 ret = doioctl(fd, VIDIOC_S_FMT, &fmt);
233 else
234 ret = doioctl(fd, VIDIOC_TRY_FMT, &fmt);
235 if (ret == 0 && (verbose || options[OptTryVbiOutFormat]))
236 printfmt(fd, fmt);
237 }
238 }
239
240 void vbi_get(cv4l_fd &_fd)
241 {
242 int fd = _fd.g_fd();
243
244 if (options[OptGetSlicedVbiFormat]) {
245 sliced_fmt.type = V4L2_BUF_TYPE_SLICED_VBI_CAPTURE;
246 if (doioctl(fd, VIDIOC_G_FMT, &sliced_fmt) == 0)
247 printfmt(fd, sliced_fmt);
248 }
249
250 if (options[OptGetSlicedVbiOutFormat]) {
251 sliced_fmt_out.type = V4L2_BUF_TYPE_SLICED_VBI_OUTPUT;
252 if (doioctl(fd, VIDIOC_G_FMT, &sliced_fmt_out) == 0)
253 printfmt(fd, sliced_fmt_out);
254 }
255
256 if (options[OptGetVbiFormat]) {
257 raw_fmt.type = V4L2_BUF_TYPE_VBI_CAPTURE;
258 if (doioctl(fd, VIDIOC_G_FMT, &raw_fmt) == 0)
259 printfmt(fd, raw_fmt);
260 }
261
262 if (options[OptGetVbiOutFormat]) {
263 raw_fmt_out.type = V4L2_BUF_TYPE_VBI_OUTPUT;
264 if (doioctl(fd, VIDIOC_G_FMT, &raw_fmt_out) == 0)
265 printfmt(fd, raw_fmt_out);
266 }
267 }
268
269 void vbi_list(cv4l_fd &fd)
270 {
271 if (options[OptGetSlicedVbiCap]) {
272 struct v4l2_sliced_vbi_cap cap;
273
274 cap.type = V4L2_BUF_TYPE_SLICED_VBI_CAPTURE;
275 if (doioctl(fd.g_fd(), VIDIOC_G_SLICED_VBI_CAP, &cap) == 0) {
276 print_sliced_vbi_cap(cap);
277 }
278 }
279
280 if (options[OptGetSlicedVbiOutCap]) {
281 struct v4l2_sliced_vbi_cap cap;
282
283 cap.type = V4L2_BUF_TYPE_SLICED_VBI_OUTPUT;
284 if (doioctl(fd.g_fd(), VIDIOC_G_SLICED_VBI_CAP, &cap) == 0) {
285 print_sliced_vbi_cap(cap);
286 }
287 }
288 }