Commit | Line | Data |
---|---|---|
ef834f78 HV |
1 | /* |
2 | * vivid-vid-common.c - common video support functions. | |
3 | * | |
4 | * Copyright 2014 Cisco Systems, Inc. and/or its affiliates. All rights reserved. | |
5 | * | |
6 | * This program is free software; you may redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; version 2 of the License. | |
9 | * | |
10 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
11 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
12 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
13 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS | |
14 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN | |
15 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | |
16 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
17 | * SOFTWARE. | |
18 | */ | |
19 | ||
20 | #include <linux/errno.h> | |
21 | #include <linux/kernel.h> | |
22 | #include <linux/sched.h> | |
23 | #include <linux/videodev2.h> | |
24 | #include <linux/v4l2-dv-timings.h> | |
25 | #include <media/v4l2-common.h> | |
26 | #include <media/v4l2-event.h> | |
27 | #include <media/v4l2-dv-timings.h> | |
28 | ||
29 | #include "vivid-core.h" | |
30 | #include "vivid-vid-common.h" | |
31 | ||
32 | const struct v4l2_dv_timings_cap vivid_dv_timings_cap = { | |
33 | .type = V4L2_DV_BT_656_1120, | |
34 | /* keep this initialization for compatibility with GCC < 4.4.6 */ | |
35 | .reserved = { 0 }, | |
36 | V4L2_INIT_BT_TIMINGS(0, MAX_WIDTH, 0, MAX_HEIGHT, 25000000, 600000000, | |
37 | V4L2_DV_BT_STD_CEA861 | V4L2_DV_BT_STD_DMT, | |
38 | V4L2_DV_BT_CAP_PROGRESSIVE | V4L2_DV_BT_CAP_INTERLACED) | |
39 | }; | |
40 | ||
41 | /* ------------------------------------------------------------------ | |
42 | Basic structures | |
43 | ------------------------------------------------------------------*/ | |
44 | ||
45 | struct vivid_fmt vivid_formats[] = { | |
46 | { | |
47 | .name = "4:2:2, packed, YUYV", | |
48 | .fourcc = V4L2_PIX_FMT_YUYV, | |
96c76efa HV |
49 | .vdownsampling = { 1 }, |
50 | .bit_depth = { 16 }, | |
ef834f78 HV |
51 | .is_yuv = true, |
52 | .planes = 1, | |
96c76efa | 53 | .buffers = 1, |
b521c375 | 54 | .data_offset = { PLANE0_DATA_OFFSET }, |
ef834f78 HV |
55 | }, |
56 | { | |
57 | .name = "4:2:2, packed, UYVY", | |
58 | .fourcc = V4L2_PIX_FMT_UYVY, | |
96c76efa HV |
59 | .vdownsampling = { 1 }, |
60 | .bit_depth = { 16 }, | |
ef834f78 HV |
61 | .is_yuv = true, |
62 | .planes = 1, | |
96c76efa | 63 | .buffers = 1, |
ef834f78 HV |
64 | }, |
65 | { | |
66 | .name = "4:2:2, packed, YVYU", | |
67 | .fourcc = V4L2_PIX_FMT_YVYU, | |
96c76efa HV |
68 | .vdownsampling = { 1 }, |
69 | .bit_depth = { 16 }, | |
ef834f78 HV |
70 | .is_yuv = true, |
71 | .planes = 1, | |
96c76efa | 72 | .buffers = 1, |
ef834f78 HV |
73 | }, |
74 | { | |
75 | .name = "4:2:2, packed, VYUY", | |
76 | .fourcc = V4L2_PIX_FMT_VYUY, | |
96c76efa HV |
77 | .vdownsampling = { 1 }, |
78 | .bit_depth = { 16 }, | |
ef834f78 HV |
79 | .is_yuv = true, |
80 | .planes = 1, | |
96c76efa | 81 | .buffers = 1, |
ef834f78 | 82 | }, |
b521c375 HV |
83 | { |
84 | .name = "YUV 4:2:2 triplanar", | |
85 | .fourcc = V4L2_PIX_FMT_YUV422P, | |
86 | .vdownsampling = { 1, 1, 1 }, | |
87 | .bit_depth = { 8, 4, 4 }, | |
88 | .is_yuv = true, | |
89 | .planes = 3, | |
90 | .buffers = 1, | |
91 | }, | |
92 | { | |
93 | .name = "YUV 4:2:0 triplanar", | |
94 | .fourcc = V4L2_PIX_FMT_YUV420, | |
95 | .vdownsampling = { 1, 2, 2 }, | |
96 | .bit_depth = { 8, 4, 4 }, | |
97 | .is_yuv = true, | |
98 | .planes = 3, | |
99 | .buffers = 1, | |
100 | }, | |
101 | { | |
102 | .name = "YVU 4:2:0 triplanar", | |
103 | .fourcc = V4L2_PIX_FMT_YVU420, | |
104 | .vdownsampling = { 1, 2, 2 }, | |
105 | .bit_depth = { 8, 4, 4 }, | |
106 | .is_yuv = true, | |
107 | .planes = 3, | |
108 | .buffers = 1, | |
109 | }, | |
110 | { | |
111 | .name = "YUV 4:2:0 biplanar", | |
112 | .fourcc = V4L2_PIX_FMT_NV12, | |
113 | .vdownsampling = { 1, 2 }, | |
114 | .bit_depth = { 8, 8 }, | |
115 | .is_yuv = true, | |
116 | .planes = 2, | |
117 | .buffers = 1, | |
118 | }, | |
119 | { | |
120 | .name = "YVU 4:2:0 biplanar", | |
121 | .fourcc = V4L2_PIX_FMT_NV21, | |
122 | .vdownsampling = { 1, 2 }, | |
123 | .bit_depth = { 8, 8 }, | |
124 | .is_yuv = true, | |
125 | .planes = 2, | |
126 | .buffers = 1, | |
127 | }, | |
128 | { | |
129 | .name = "YUV 4:2:2 biplanar", | |
130 | .fourcc = V4L2_PIX_FMT_NV16, | |
131 | .vdownsampling = { 1, 1 }, | |
132 | .bit_depth = { 8, 8 }, | |
133 | .is_yuv = true, | |
134 | .planes = 2, | |
135 | .buffers = 1, | |
136 | }, | |
137 | { | |
138 | .name = "YVU 4:2:2 biplanar", | |
139 | .fourcc = V4L2_PIX_FMT_NV61, | |
140 | .vdownsampling = { 1, 1 }, | |
141 | .bit_depth = { 8, 8 }, | |
142 | .is_yuv = true, | |
143 | .planes = 2, | |
144 | .buffers = 1, | |
145 | }, | |
dde72bd7 HV |
146 | { |
147 | .name = "YUV 4:4:4 biplanar", | |
148 | .fourcc = V4L2_PIX_FMT_NV24, | |
149 | .vdownsampling = { 1, 1 }, | |
150 | .bit_depth = { 8, 16 }, | |
151 | .is_yuv = true, | |
152 | .planes = 2, | |
153 | .buffers = 1, | |
154 | }, | |
155 | { | |
156 | .name = "YVU 4:4:4 biplanar", | |
157 | .fourcc = V4L2_PIX_FMT_NV42, | |
158 | .vdownsampling = { 1, 1 }, | |
159 | .bit_depth = { 8, 16 }, | |
160 | .is_yuv = true, | |
161 | .planes = 2, | |
162 | .buffers = 1, | |
163 | }, | |
b521c375 HV |
164 | { |
165 | .name = "Monochrome", | |
166 | .fourcc = V4L2_PIX_FMT_GREY, | |
167 | .vdownsampling = { 1 }, | |
168 | .bit_depth = { 8 }, | |
169 | .is_yuv = true, | |
170 | .planes = 1, | |
171 | .buffers = 1, | |
172 | }, | |
ef834f78 HV |
173 | { |
174 | .name = "RGB565 (LE)", | |
175 | .fourcc = V4L2_PIX_FMT_RGB565, /* gggbbbbb rrrrrggg */ | |
96c76efa HV |
176 | .vdownsampling = { 1 }, |
177 | .bit_depth = { 16 }, | |
ef834f78 | 178 | .planes = 1, |
96c76efa | 179 | .buffers = 1, |
ef834f78 HV |
180 | .can_do_overlay = true, |
181 | }, | |
182 | { | |
183 | .name = "RGB565 (BE)", | |
184 | .fourcc = V4L2_PIX_FMT_RGB565X, /* rrrrrggg gggbbbbb */ | |
96c76efa HV |
185 | .vdownsampling = { 1 }, |
186 | .bit_depth = { 16 }, | |
ef834f78 | 187 | .planes = 1, |
96c76efa | 188 | .buffers = 1, |
ef834f78 HV |
189 | .can_do_overlay = true, |
190 | }, | |
8aca230b HV |
191 | { |
192 | .name = "RGB444", | |
193 | .fourcc = V4L2_PIX_FMT_RGB444, /* xxxxrrrr ggggbbbb */ | |
194 | .vdownsampling = { 1 }, | |
195 | .bit_depth = { 16 }, | |
196 | .planes = 1, | |
197 | .buffers = 1, | |
198 | }, | |
199 | { | |
200 | .name = "XRGB444", | |
201 | .fourcc = V4L2_PIX_FMT_XRGB444, /* xxxxrrrr ggggbbbb */ | |
202 | .vdownsampling = { 1 }, | |
203 | .bit_depth = { 16 }, | |
204 | .planes = 1, | |
205 | .buffers = 1, | |
206 | }, | |
207 | { | |
208 | .name = "ARGB444", | |
209 | .fourcc = V4L2_PIX_FMT_ARGB444, /* aaaarrrr ggggbbbb */ | |
210 | .vdownsampling = { 1 }, | |
211 | .bit_depth = { 16 }, | |
212 | .planes = 1, | |
213 | .buffers = 1, | |
214 | .alpha_mask = 0x00f0, | |
215 | }, | |
ef834f78 HV |
216 | { |
217 | .name = "RGB555 (LE)", | |
9e1b73cf | 218 | .fourcc = V4L2_PIX_FMT_RGB555, /* gggbbbbb xrrrrrgg */ |
96c76efa HV |
219 | .vdownsampling = { 1 }, |
220 | .bit_depth = { 16 }, | |
ef834f78 | 221 | .planes = 1, |
96c76efa | 222 | .buffers = 1, |
ef834f78 HV |
223 | .can_do_overlay = true, |
224 | }, | |
225 | { | |
226 | .name = "XRGB555 (LE)", | |
9e1b73cf | 227 | .fourcc = V4L2_PIX_FMT_XRGB555, /* gggbbbbb xrrrrrgg */ |
96c76efa HV |
228 | .vdownsampling = { 1 }, |
229 | .bit_depth = { 16 }, | |
ef834f78 | 230 | .planes = 1, |
96c76efa | 231 | .buffers = 1, |
ef834f78 HV |
232 | .can_do_overlay = true, |
233 | }, | |
234 | { | |
235 | .name = "ARGB555 (LE)", | |
236 | .fourcc = V4L2_PIX_FMT_ARGB555, /* gggbbbbb arrrrrgg */ | |
96c76efa HV |
237 | .vdownsampling = { 1 }, |
238 | .bit_depth = { 16 }, | |
ef834f78 | 239 | .planes = 1, |
96c76efa | 240 | .buffers = 1, |
ef834f78 HV |
241 | .can_do_overlay = true, |
242 | .alpha_mask = 0x8000, | |
243 | }, | |
244 | { | |
245 | .name = "RGB555 (BE)", | |
9e1b73cf | 246 | .fourcc = V4L2_PIX_FMT_RGB555X, /* xrrrrrgg gggbbbbb */ |
96c76efa HV |
247 | .vdownsampling = { 1 }, |
248 | .bit_depth = { 16 }, | |
ef834f78 | 249 | .planes = 1, |
96c76efa | 250 | .buffers = 1, |
8f1ff543 HV |
251 | }, |
252 | { | |
253 | .name = "XRGB555 (BE)", | |
254 | .fourcc = V4L2_PIX_FMT_XRGB555X, /* xrrrrrgg gggbbbbb */ | |
255 | .vdownsampling = { 1 }, | |
256 | .bit_depth = { 16 }, | |
257 | .planes = 1, | |
258 | .buffers = 1, | |
259 | }, | |
260 | { | |
261 | .name = "ARGB555 (BE)", | |
262 | .fourcc = V4L2_PIX_FMT_ARGB555X, /* arrrrrgg gggbbbbb */ | |
263 | .vdownsampling = { 1 }, | |
264 | .bit_depth = { 16 }, | |
265 | .planes = 1, | |
266 | .buffers = 1, | |
267 | .alpha_mask = 0x0080, | |
ef834f78 HV |
268 | }, |
269 | { | |
270 | .name = "RGB24 (LE)", | |
271 | .fourcc = V4L2_PIX_FMT_RGB24, /* rgb */ | |
96c76efa HV |
272 | .vdownsampling = { 1 }, |
273 | .bit_depth = { 24 }, | |
ef834f78 | 274 | .planes = 1, |
96c76efa | 275 | .buffers = 1, |
ef834f78 HV |
276 | }, |
277 | { | |
278 | .name = "RGB24 (BE)", | |
279 | .fourcc = V4L2_PIX_FMT_BGR24, /* bgr */ | |
96c76efa HV |
280 | .vdownsampling = { 1 }, |
281 | .bit_depth = { 24 }, | |
ef834f78 | 282 | .planes = 1, |
96c76efa | 283 | .buffers = 1, |
ef834f78 HV |
284 | }, |
285 | { | |
286 | .name = "RGB32 (LE)", | |
9e1b73cf | 287 | .fourcc = V4L2_PIX_FMT_RGB32, /* xrgb */ |
96c76efa HV |
288 | .vdownsampling = { 1 }, |
289 | .bit_depth = { 32 }, | |
ef834f78 | 290 | .planes = 1, |
96c76efa | 291 | .buffers = 1, |
ef834f78 HV |
292 | }, |
293 | { | |
294 | .name = "RGB32 (BE)", | |
9e1b73cf | 295 | .fourcc = V4L2_PIX_FMT_BGR32, /* bgrx */ |
96c76efa HV |
296 | .vdownsampling = { 1 }, |
297 | .bit_depth = { 32 }, | |
ef834f78 | 298 | .planes = 1, |
96c76efa | 299 | .buffers = 1, |
ef834f78 HV |
300 | }, |
301 | { | |
302 | .name = "XRGB32 (LE)", | |
9e1b73cf | 303 | .fourcc = V4L2_PIX_FMT_XRGB32, /* xrgb */ |
96c76efa HV |
304 | .vdownsampling = { 1 }, |
305 | .bit_depth = { 32 }, | |
ef834f78 | 306 | .planes = 1, |
96c76efa | 307 | .buffers = 1, |
ef834f78 HV |
308 | }, |
309 | { | |
310 | .name = "XRGB32 (BE)", | |
9e1b73cf | 311 | .fourcc = V4L2_PIX_FMT_XBGR32, /* bgrx */ |
96c76efa HV |
312 | .vdownsampling = { 1 }, |
313 | .bit_depth = { 32 }, | |
ef834f78 | 314 | .planes = 1, |
96c76efa | 315 | .buffers = 1, |
ef834f78 HV |
316 | }, |
317 | { | |
318 | .name = "ARGB32 (LE)", | |
319 | .fourcc = V4L2_PIX_FMT_ARGB32, /* argb */ | |
96c76efa HV |
320 | .vdownsampling = { 1 }, |
321 | .bit_depth = { 32 }, | |
ef834f78 | 322 | .planes = 1, |
96c76efa | 323 | .buffers = 1, |
ef834f78 HV |
324 | .alpha_mask = 0x000000ff, |
325 | }, | |
326 | { | |
327 | .name = "ARGB32 (BE)", | |
328 | .fourcc = V4L2_PIX_FMT_ABGR32, /* bgra */ | |
96c76efa HV |
329 | .vdownsampling = { 1 }, |
330 | .bit_depth = { 32 }, | |
ef834f78 | 331 | .planes = 1, |
96c76efa | 332 | .buffers = 1, |
ef834f78 HV |
333 | .alpha_mask = 0xff000000, |
334 | }, | |
335 | { | |
96c76efa | 336 | .name = "4:2:2, biplanar, YUV", |
ef834f78 | 337 | .fourcc = V4L2_PIX_FMT_NV16M, |
96c76efa HV |
338 | .vdownsampling = { 1, 1 }, |
339 | .bit_depth = { 8, 8 }, | |
ef834f78 HV |
340 | .is_yuv = true, |
341 | .planes = 2, | |
96c76efa | 342 | .buffers = 2, |
ef834f78 HV |
343 | .data_offset = { PLANE0_DATA_OFFSET, 0 }, |
344 | }, | |
345 | { | |
96c76efa | 346 | .name = "4:2:2, biplanar, YVU", |
ef834f78 | 347 | .fourcc = V4L2_PIX_FMT_NV61M, |
96c76efa HV |
348 | .vdownsampling = { 1, 1 }, |
349 | .bit_depth = { 8, 8 }, | |
ef834f78 HV |
350 | .is_yuv = true, |
351 | .planes = 2, | |
96c76efa | 352 | .buffers = 2, |
ef834f78 HV |
353 | .data_offset = { 0, PLANE0_DATA_OFFSET }, |
354 | }, | |
b521c375 HV |
355 | { |
356 | .name = "4:2:0, triplanar, YUV", | |
357 | .fourcc = V4L2_PIX_FMT_YUV420M, | |
358 | .vdownsampling = { 1, 2, 2 }, | |
359 | .bit_depth = { 8, 4, 4 }, | |
360 | .is_yuv = true, | |
361 | .planes = 3, | |
362 | .buffers = 3, | |
363 | }, | |
364 | { | |
365 | .name = "4:2:0, triplanar, YVU", | |
366 | .fourcc = V4L2_PIX_FMT_YVU420M, | |
367 | .vdownsampling = { 1, 2, 2 }, | |
368 | .bit_depth = { 8, 4, 4 }, | |
369 | .is_yuv = true, | |
370 | .planes = 3, | |
371 | .buffers = 3, | |
372 | }, | |
373 | { | |
374 | .name = "4:2:0, biplanar, YUV", | |
375 | .fourcc = V4L2_PIX_FMT_NV12M, | |
376 | .vdownsampling = { 1, 2 }, | |
377 | .bit_depth = { 8, 8 }, | |
378 | .is_yuv = true, | |
379 | .planes = 2, | |
380 | .buffers = 2, | |
381 | }, | |
382 | { | |
383 | .name = "4:2:0, biplanar, YVU", | |
384 | .fourcc = V4L2_PIX_FMT_NV21M, | |
385 | .vdownsampling = { 1, 2 }, | |
386 | .bit_depth = { 8, 8 }, | |
387 | .is_yuv = true, | |
388 | .planes = 2, | |
389 | .buffers = 2, | |
390 | }, | |
ef834f78 HV |
391 | }; |
392 | ||
b521c375 HV |
393 | /* There are 6 multiplanar formats in the list */ |
394 | #define VIVID_MPLANAR_FORMATS 6 | |
ef834f78 | 395 | |
1fc78bc9 | 396 | const struct vivid_fmt *vivid_get_format(struct vivid_dev *dev, u32 pixelformat) |
ef834f78 HV |
397 | { |
398 | const struct vivid_fmt *fmt; | |
399 | unsigned k; | |
400 | ||
401 | for (k = 0; k < ARRAY_SIZE(vivid_formats); k++) { | |
402 | fmt = &vivid_formats[k]; | |
403 | if (fmt->fourcc == pixelformat) | |
ddcaee9d | 404 | if (fmt->buffers == 1 || dev->multiplanar) |
ef834f78 HV |
405 | return fmt; |
406 | } | |
407 | ||
408 | return NULL; | |
409 | } | |
410 | ||
411 | bool vivid_vid_can_loop(struct vivid_dev *dev) | |
412 | { | |
413 | if (dev->src_rect.width != dev->sink_rect.width || | |
414 | dev->src_rect.height != dev->sink_rect.height) | |
415 | return false; | |
416 | if (dev->fmt_cap->fourcc != dev->fmt_out->fourcc) | |
417 | return false; | |
418 | if (dev->field_cap != dev->field_out) | |
419 | return false; | |
ba24b442 HV |
420 | /* |
421 | * While this can be supported, it is just too much work | |
422 | * to actually implement. | |
423 | */ | |
424 | if (dev->field_cap == V4L2_FIELD_SEQ_TB || | |
425 | dev->field_cap == V4L2_FIELD_SEQ_BT) | |
426 | return false; | |
ef834f78 HV |
427 | if (vivid_is_svid_cap(dev) && vivid_is_svid_out(dev)) { |
428 | if (!(dev->std_cap & V4L2_STD_525_60) != | |
429 | !(dev->std_out & V4L2_STD_525_60)) | |
430 | return false; | |
431 | return true; | |
432 | } | |
433 | if (vivid_is_hdmi_cap(dev) && vivid_is_hdmi_out(dev)) | |
434 | return true; | |
435 | return false; | |
436 | } | |
437 | ||
438 | void vivid_send_source_change(struct vivid_dev *dev, unsigned type) | |
439 | { | |
440 | struct v4l2_event ev = { | |
441 | .type = V4L2_EVENT_SOURCE_CHANGE, | |
442 | .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION, | |
443 | }; | |
444 | unsigned i; | |
445 | ||
446 | for (i = 0; i < dev->num_inputs; i++) { | |
447 | ev.id = i; | |
448 | if (dev->input_type[i] == type) { | |
449 | if (video_is_registered(&dev->vid_cap_dev) && dev->has_vid_cap) | |
450 | v4l2_event_queue(&dev->vid_cap_dev, &ev); | |
451 | if (video_is_registered(&dev->vbi_cap_dev) && dev->has_vbi_cap) | |
452 | v4l2_event_queue(&dev->vbi_cap_dev, &ev); | |
453 | } | |
454 | } | |
455 | } | |
456 | ||
457 | /* | |
458 | * Conversion function that converts a single-planar format to a | |
459 | * single-plane multiplanar format. | |
460 | */ | |
461 | void fmt_sp2mp(const struct v4l2_format *sp_fmt, struct v4l2_format *mp_fmt) | |
462 | { | |
463 | struct v4l2_pix_format_mplane *mp = &mp_fmt->fmt.pix_mp; | |
464 | struct v4l2_plane_pix_format *ppix = &mp->plane_fmt[0]; | |
465 | const struct v4l2_pix_format *pix = &sp_fmt->fmt.pix; | |
466 | bool is_out = sp_fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT; | |
467 | ||
468 | memset(mp->reserved, 0, sizeof(mp->reserved)); | |
469 | mp_fmt->type = is_out ? V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE : | |
470 | V4L2_CAP_VIDEO_CAPTURE_MPLANE; | |
471 | mp->width = pix->width; | |
472 | mp->height = pix->height; | |
473 | mp->pixelformat = pix->pixelformat; | |
474 | mp->field = pix->field; | |
475 | mp->colorspace = pix->colorspace; | |
3e8a78d1 HV |
476 | mp->ycbcr_enc = pix->ycbcr_enc; |
477 | mp->quantization = pix->quantization; | |
ef834f78 HV |
478 | mp->num_planes = 1; |
479 | mp->flags = pix->flags; | |
480 | ppix->sizeimage = pix->sizeimage; | |
481 | ppix->bytesperline = pix->bytesperline; | |
482 | memset(ppix->reserved, 0, sizeof(ppix->reserved)); | |
483 | } | |
484 | ||
485 | int fmt_sp2mp_func(struct file *file, void *priv, | |
486 | struct v4l2_format *f, fmtfunc func) | |
487 | { | |
488 | struct v4l2_format fmt; | |
489 | struct v4l2_pix_format_mplane *mp = &fmt.fmt.pix_mp; | |
490 | struct v4l2_plane_pix_format *ppix = &mp->plane_fmt[0]; | |
491 | struct v4l2_pix_format *pix = &f->fmt.pix; | |
492 | int ret; | |
493 | ||
494 | /* Converts to a mplane format */ | |
495 | fmt_sp2mp(f, &fmt); | |
496 | /* Passes it to the generic mplane format function */ | |
497 | ret = func(file, priv, &fmt); | |
498 | /* Copies back the mplane data to the single plane format */ | |
499 | pix->width = mp->width; | |
500 | pix->height = mp->height; | |
501 | pix->pixelformat = mp->pixelformat; | |
502 | pix->field = mp->field; | |
503 | pix->colorspace = mp->colorspace; | |
3e8a78d1 HV |
504 | pix->ycbcr_enc = mp->ycbcr_enc; |
505 | pix->quantization = mp->quantization; | |
ef834f78 HV |
506 | pix->sizeimage = ppix->sizeimage; |
507 | pix->bytesperline = ppix->bytesperline; | |
508 | pix->flags = mp->flags; | |
509 | return ret; | |
510 | } | |
511 | ||
512 | /* v4l2_rect helper function: copy the width/height values */ | |
513 | void rect_set_size_to(struct v4l2_rect *r, const struct v4l2_rect *size) | |
514 | { | |
515 | r->width = size->width; | |
516 | r->height = size->height; | |
517 | } | |
518 | ||
519 | /* v4l2_rect helper function: width and height of r should be >= min_size */ | |
520 | void rect_set_min_size(struct v4l2_rect *r, const struct v4l2_rect *min_size) | |
521 | { | |
522 | if (r->width < min_size->width) | |
523 | r->width = min_size->width; | |
524 | if (r->height < min_size->height) | |
525 | r->height = min_size->height; | |
526 | } | |
527 | ||
528 | /* v4l2_rect helper function: width and height of r should be <= max_size */ | |
529 | void rect_set_max_size(struct v4l2_rect *r, const struct v4l2_rect *max_size) | |
530 | { | |
531 | if (r->width > max_size->width) | |
532 | r->width = max_size->width; | |
533 | if (r->height > max_size->height) | |
534 | r->height = max_size->height; | |
535 | } | |
536 | ||
537 | /* v4l2_rect helper function: r should be inside boundary */ | |
538 | void rect_map_inside(struct v4l2_rect *r, const struct v4l2_rect *boundary) | |
539 | { | |
540 | rect_set_max_size(r, boundary); | |
541 | if (r->left < boundary->left) | |
542 | r->left = boundary->left; | |
543 | if (r->top < boundary->top) | |
544 | r->top = boundary->top; | |
545 | if (r->left + r->width > boundary->width) | |
546 | r->left = boundary->width - r->width; | |
547 | if (r->top + r->height > boundary->height) | |
548 | r->top = boundary->height - r->height; | |
549 | } | |
550 | ||
551 | /* v4l2_rect helper function: return true if r1 has the same size as r2 */ | |
552 | bool rect_same_size(const struct v4l2_rect *r1, const struct v4l2_rect *r2) | |
553 | { | |
554 | return r1->width == r2->width && r1->height == r2->height; | |
555 | } | |
556 | ||
557 | /* v4l2_rect helper function: calculate the intersection of two rects */ | |
558 | struct v4l2_rect rect_intersect(const struct v4l2_rect *a, const struct v4l2_rect *b) | |
559 | { | |
560 | struct v4l2_rect r; | |
561 | int right, bottom; | |
562 | ||
563 | r.top = max(a->top, b->top); | |
564 | r.left = max(a->left, b->left); | |
565 | bottom = min(a->top + a->height, b->top + b->height); | |
566 | right = min(a->left + a->width, b->left + b->width); | |
567 | r.height = max(0, bottom - r.top); | |
568 | r.width = max(0, right - r.left); | |
569 | return r; | |
570 | } | |
571 | ||
572 | /* | |
573 | * v4l2_rect helper function: scale rect r by to->width / from->width and | |
574 | * to->height / from->height. | |
575 | */ | |
576 | void rect_scale(struct v4l2_rect *r, const struct v4l2_rect *from, | |
577 | const struct v4l2_rect *to) | |
578 | { | |
579 | if (from->width == 0 || from->height == 0) { | |
580 | r->left = r->top = r->width = r->height = 0; | |
581 | return; | |
582 | } | |
583 | r->left = (((r->left - from->left) * to->width) / from->width) & ~1; | |
584 | r->width = ((r->width * to->width) / from->width) & ~1; | |
585 | r->top = ((r->top - from->top) * to->height) / from->height; | |
586 | r->height = (r->height * to->height) / from->height; | |
587 | } | |
588 | ||
589 | bool rect_overlap(const struct v4l2_rect *r1, const struct v4l2_rect *r2) | |
590 | { | |
591 | /* | |
592 | * IF the left side of r1 is to the right of the right side of r2 OR | |
593 | * the left side of r2 is to the right of the right side of r1 THEN | |
594 | * they do not overlap. | |
595 | */ | |
596 | if (r1->left >= r2->left + r2->width || | |
597 | r2->left >= r1->left + r1->width) | |
598 | return false; | |
599 | /* | |
600 | * IF the top side of r1 is below the bottom of r2 OR | |
601 | * the top side of r2 is below the bottom of r1 THEN | |
602 | * they do not overlap. | |
603 | */ | |
604 | if (r1->top >= r2->top + r2->height || | |
605 | r2->top >= r1->top + r1->height) | |
606 | return false; | |
607 | return true; | |
608 | } | |
609 | int vivid_vid_adjust_sel(unsigned flags, struct v4l2_rect *r) | |
610 | { | |
611 | unsigned w = r->width; | |
612 | unsigned h = r->height; | |
613 | ||
614 | if (!(flags & V4L2_SEL_FLAG_LE)) { | |
615 | w++; | |
616 | h++; | |
617 | if (w < 2) | |
618 | w = 2; | |
619 | if (h < 2) | |
620 | h = 2; | |
621 | } | |
622 | if (!(flags & V4L2_SEL_FLAG_GE)) { | |
623 | if (w > MAX_WIDTH) | |
624 | w = MAX_WIDTH; | |
625 | if (h > MAX_HEIGHT) | |
626 | h = MAX_HEIGHT; | |
627 | } | |
628 | w = w & ~1; | |
629 | h = h & ~1; | |
630 | if (w < 2 || h < 2) | |
631 | return -ERANGE; | |
632 | if (w > MAX_WIDTH || h > MAX_HEIGHT) | |
633 | return -ERANGE; | |
634 | if (r->top < 0) | |
635 | r->top = 0; | |
636 | if (r->left < 0) | |
637 | r->left = 0; | |
638 | r->left &= ~1; | |
639 | r->top &= ~1; | |
640 | if (r->left + w > MAX_WIDTH) | |
641 | r->left = MAX_WIDTH - w; | |
642 | if (r->top + h > MAX_HEIGHT) | |
643 | r->top = MAX_HEIGHT - h; | |
644 | if ((flags & (V4L2_SEL_FLAG_GE | V4L2_SEL_FLAG_LE)) == | |
645 | (V4L2_SEL_FLAG_GE | V4L2_SEL_FLAG_LE) && | |
646 | (r->width != w || r->height != h)) | |
647 | return -ERANGE; | |
648 | r->width = w; | |
649 | r->height = h; | |
650 | return 0; | |
651 | } | |
652 | ||
653 | int vivid_enum_fmt_vid(struct file *file, void *priv, | |
654 | struct v4l2_fmtdesc *f) | |
655 | { | |
656 | struct vivid_dev *dev = video_drvdata(file); | |
657 | const struct vivid_fmt *fmt; | |
658 | ||
659 | if (f->index >= ARRAY_SIZE(vivid_formats) - | |
660 | (dev->multiplanar ? 0 : VIVID_MPLANAR_FORMATS)) | |
661 | return -EINVAL; | |
662 | ||
663 | fmt = &vivid_formats[f->index]; | |
664 | ||
665 | strlcpy(f->description, fmt->name, sizeof(f->description)); | |
666 | f->pixelformat = fmt->fourcc; | |
667 | return 0; | |
668 | } | |
669 | ||
670 | int vidioc_enum_fmt_vid_mplane(struct file *file, void *priv, | |
671 | struct v4l2_fmtdesc *f) | |
672 | { | |
673 | struct vivid_dev *dev = video_drvdata(file); | |
674 | ||
675 | if (!dev->multiplanar) | |
676 | return -ENOTTY; | |
677 | return vivid_enum_fmt_vid(file, priv, f); | |
678 | } | |
679 | ||
680 | int vidioc_enum_fmt_vid(struct file *file, void *priv, | |
681 | struct v4l2_fmtdesc *f) | |
682 | { | |
683 | struct vivid_dev *dev = video_drvdata(file); | |
684 | ||
685 | if (dev->multiplanar) | |
686 | return -ENOTTY; | |
687 | return vivid_enum_fmt_vid(file, priv, f); | |
688 | } | |
689 | ||
690 | int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id) | |
691 | { | |
692 | struct vivid_dev *dev = video_drvdata(file); | |
693 | struct video_device *vdev = video_devdata(file); | |
694 | ||
695 | if (vdev->vfl_dir == VFL_DIR_RX) { | |
696 | if (!vivid_is_sdtv_cap(dev)) | |
697 | return -ENODATA; | |
698 | *id = dev->std_cap; | |
699 | } else { | |
700 | if (!vivid_is_svid_out(dev)) | |
701 | return -ENODATA; | |
702 | *id = dev->std_out; | |
703 | } | |
704 | return 0; | |
705 | } | |
706 | ||
707 | int vidioc_g_dv_timings(struct file *file, void *_fh, | |
708 | struct v4l2_dv_timings *timings) | |
709 | { | |
710 | struct vivid_dev *dev = video_drvdata(file); | |
711 | struct video_device *vdev = video_devdata(file); | |
712 | ||
713 | if (vdev->vfl_dir == VFL_DIR_RX) { | |
714 | if (!vivid_is_hdmi_cap(dev)) | |
715 | return -ENODATA; | |
716 | *timings = dev->dv_timings_cap; | |
717 | } else { | |
718 | if (!vivid_is_hdmi_out(dev)) | |
719 | return -ENODATA; | |
720 | *timings = dev->dv_timings_out; | |
721 | } | |
722 | return 0; | |
723 | } | |
724 | ||
725 | int vidioc_enum_dv_timings(struct file *file, void *_fh, | |
726 | struct v4l2_enum_dv_timings *timings) | |
727 | { | |
728 | struct vivid_dev *dev = video_drvdata(file); | |
729 | struct video_device *vdev = video_devdata(file); | |
730 | ||
731 | if (vdev->vfl_dir == VFL_DIR_RX) { | |
732 | if (!vivid_is_hdmi_cap(dev)) | |
733 | return -ENODATA; | |
734 | } else { | |
735 | if (!vivid_is_hdmi_out(dev)) | |
736 | return -ENODATA; | |
737 | } | |
738 | return v4l2_enum_dv_timings_cap(timings, &vivid_dv_timings_cap, | |
739 | NULL, NULL); | |
740 | } | |
741 | ||
742 | int vidioc_dv_timings_cap(struct file *file, void *_fh, | |
743 | struct v4l2_dv_timings_cap *cap) | |
744 | { | |
745 | struct vivid_dev *dev = video_drvdata(file); | |
746 | struct video_device *vdev = video_devdata(file); | |
747 | ||
748 | if (vdev->vfl_dir == VFL_DIR_RX) { | |
749 | if (!vivid_is_hdmi_cap(dev)) | |
750 | return -ENODATA; | |
751 | } else { | |
752 | if (!vivid_is_hdmi_out(dev)) | |
753 | return -ENODATA; | |
754 | } | |
755 | *cap = vivid_dv_timings_cap; | |
756 | return 0; | |
757 | } | |
758 | ||
759 | int vidioc_g_edid(struct file *file, void *_fh, | |
760 | struct v4l2_edid *edid) | |
761 | { | |
762 | struct vivid_dev *dev = video_drvdata(file); | |
763 | struct video_device *vdev = video_devdata(file); | |
764 | ||
765 | memset(edid->reserved, 0, sizeof(edid->reserved)); | |
766 | if (vdev->vfl_dir == VFL_DIR_RX) { | |
767 | if (edid->pad >= dev->num_inputs) | |
768 | return -EINVAL; | |
769 | if (dev->input_type[edid->pad] != HDMI) | |
770 | return -EINVAL; | |
771 | } else { | |
772 | if (edid->pad >= dev->num_outputs) | |
773 | return -EINVAL; | |
774 | if (dev->output_type[edid->pad] != HDMI) | |
775 | return -EINVAL; | |
776 | } | |
777 | if (edid->start_block == 0 && edid->blocks == 0) { | |
778 | edid->blocks = dev->edid_blocks; | |
779 | return 0; | |
780 | } | |
781 | if (dev->edid_blocks == 0) | |
782 | return -ENODATA; | |
783 | if (edid->start_block >= dev->edid_blocks) | |
784 | return -EINVAL; | |
785 | if (edid->start_block + edid->blocks > dev->edid_blocks) | |
786 | edid->blocks = dev->edid_blocks - edid->start_block; | |
787 | memcpy(edid->edid, dev->edid, edid->blocks * 128); | |
788 | return 0; | |
789 | } |