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 | }, | |
146 | { | |
147 | .name = "Monochrome", | |
148 | .fourcc = V4L2_PIX_FMT_GREY, | |
149 | .vdownsampling = { 1 }, | |
150 | .bit_depth = { 8 }, | |
151 | .is_yuv = true, | |
152 | .planes = 1, | |
153 | .buffers = 1, | |
154 | }, | |
ef834f78 HV |
155 | { |
156 | .name = "RGB565 (LE)", | |
157 | .fourcc = V4L2_PIX_FMT_RGB565, /* gggbbbbb rrrrrggg */ | |
96c76efa HV |
158 | .vdownsampling = { 1 }, |
159 | .bit_depth = { 16 }, | |
ef834f78 | 160 | .planes = 1, |
96c76efa | 161 | .buffers = 1, |
ef834f78 HV |
162 | .can_do_overlay = true, |
163 | }, | |
164 | { | |
165 | .name = "RGB565 (BE)", | |
166 | .fourcc = V4L2_PIX_FMT_RGB565X, /* rrrrrggg gggbbbbb */ | |
96c76efa HV |
167 | .vdownsampling = { 1 }, |
168 | .bit_depth = { 16 }, | |
ef834f78 | 169 | .planes = 1, |
96c76efa | 170 | .buffers = 1, |
ef834f78 HV |
171 | .can_do_overlay = true, |
172 | }, | |
8aca230b HV |
173 | { |
174 | .name = "RGB444", | |
175 | .fourcc = V4L2_PIX_FMT_RGB444, /* xxxxrrrr ggggbbbb */ | |
176 | .vdownsampling = { 1 }, | |
177 | .bit_depth = { 16 }, | |
178 | .planes = 1, | |
179 | .buffers = 1, | |
180 | }, | |
181 | { | |
182 | .name = "XRGB444", | |
183 | .fourcc = V4L2_PIX_FMT_XRGB444, /* xxxxrrrr ggggbbbb */ | |
184 | .vdownsampling = { 1 }, | |
185 | .bit_depth = { 16 }, | |
186 | .planes = 1, | |
187 | .buffers = 1, | |
188 | }, | |
189 | { | |
190 | .name = "ARGB444", | |
191 | .fourcc = V4L2_PIX_FMT_ARGB444, /* aaaarrrr ggggbbbb */ | |
192 | .vdownsampling = { 1 }, | |
193 | .bit_depth = { 16 }, | |
194 | .planes = 1, | |
195 | .buffers = 1, | |
196 | .alpha_mask = 0x00f0, | |
197 | }, | |
ef834f78 HV |
198 | { |
199 | .name = "RGB555 (LE)", | |
9e1b73cf | 200 | .fourcc = V4L2_PIX_FMT_RGB555, /* gggbbbbb xrrrrrgg */ |
96c76efa HV |
201 | .vdownsampling = { 1 }, |
202 | .bit_depth = { 16 }, | |
ef834f78 | 203 | .planes = 1, |
96c76efa | 204 | .buffers = 1, |
ef834f78 HV |
205 | .can_do_overlay = true, |
206 | }, | |
207 | { | |
208 | .name = "XRGB555 (LE)", | |
9e1b73cf | 209 | .fourcc = V4L2_PIX_FMT_XRGB555, /* gggbbbbb xrrrrrgg */ |
96c76efa HV |
210 | .vdownsampling = { 1 }, |
211 | .bit_depth = { 16 }, | |
ef834f78 | 212 | .planes = 1, |
96c76efa | 213 | .buffers = 1, |
ef834f78 HV |
214 | .can_do_overlay = true, |
215 | }, | |
216 | { | |
217 | .name = "ARGB555 (LE)", | |
218 | .fourcc = V4L2_PIX_FMT_ARGB555, /* gggbbbbb arrrrrgg */ | |
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 | .alpha_mask = 0x8000, | |
225 | }, | |
226 | { | |
227 | .name = "RGB555 (BE)", | |
9e1b73cf | 228 | .fourcc = V4L2_PIX_FMT_RGB555X, /* xrrrrrgg gggbbbbb */ |
96c76efa HV |
229 | .vdownsampling = { 1 }, |
230 | .bit_depth = { 16 }, | |
ef834f78 | 231 | .planes = 1, |
96c76efa | 232 | .buffers = 1, |
ef834f78 HV |
233 | .can_do_overlay = true, |
234 | }, | |
235 | { | |
236 | .name = "RGB24 (LE)", | |
237 | .fourcc = V4L2_PIX_FMT_RGB24, /* rgb */ | |
96c76efa HV |
238 | .vdownsampling = { 1 }, |
239 | .bit_depth = { 24 }, | |
ef834f78 | 240 | .planes = 1, |
96c76efa | 241 | .buffers = 1, |
ef834f78 HV |
242 | }, |
243 | { | |
244 | .name = "RGB24 (BE)", | |
245 | .fourcc = V4L2_PIX_FMT_BGR24, /* bgr */ | |
96c76efa HV |
246 | .vdownsampling = { 1 }, |
247 | .bit_depth = { 24 }, | |
ef834f78 | 248 | .planes = 1, |
96c76efa | 249 | .buffers = 1, |
ef834f78 HV |
250 | }, |
251 | { | |
252 | .name = "RGB32 (LE)", | |
9e1b73cf | 253 | .fourcc = V4L2_PIX_FMT_RGB32, /* xrgb */ |
96c76efa HV |
254 | .vdownsampling = { 1 }, |
255 | .bit_depth = { 32 }, | |
ef834f78 | 256 | .planes = 1, |
96c76efa | 257 | .buffers = 1, |
ef834f78 HV |
258 | }, |
259 | { | |
260 | .name = "RGB32 (BE)", | |
9e1b73cf | 261 | .fourcc = V4L2_PIX_FMT_BGR32, /* bgrx */ |
96c76efa HV |
262 | .vdownsampling = { 1 }, |
263 | .bit_depth = { 32 }, | |
ef834f78 | 264 | .planes = 1, |
96c76efa | 265 | .buffers = 1, |
ef834f78 HV |
266 | }, |
267 | { | |
268 | .name = "XRGB32 (LE)", | |
9e1b73cf | 269 | .fourcc = V4L2_PIX_FMT_XRGB32, /* xrgb */ |
96c76efa HV |
270 | .vdownsampling = { 1 }, |
271 | .bit_depth = { 32 }, | |
ef834f78 | 272 | .planes = 1, |
96c76efa | 273 | .buffers = 1, |
ef834f78 HV |
274 | }, |
275 | { | |
276 | .name = "XRGB32 (BE)", | |
9e1b73cf | 277 | .fourcc = V4L2_PIX_FMT_XBGR32, /* bgrx */ |
96c76efa HV |
278 | .vdownsampling = { 1 }, |
279 | .bit_depth = { 32 }, | |
ef834f78 | 280 | .planes = 1, |
96c76efa | 281 | .buffers = 1, |
ef834f78 HV |
282 | }, |
283 | { | |
284 | .name = "ARGB32 (LE)", | |
285 | .fourcc = V4L2_PIX_FMT_ARGB32, /* argb */ | |
96c76efa HV |
286 | .vdownsampling = { 1 }, |
287 | .bit_depth = { 32 }, | |
ef834f78 | 288 | .planes = 1, |
96c76efa | 289 | .buffers = 1, |
ef834f78 HV |
290 | .alpha_mask = 0x000000ff, |
291 | }, | |
292 | { | |
293 | .name = "ARGB32 (BE)", | |
294 | .fourcc = V4L2_PIX_FMT_ABGR32, /* bgra */ | |
96c76efa HV |
295 | .vdownsampling = { 1 }, |
296 | .bit_depth = { 32 }, | |
ef834f78 | 297 | .planes = 1, |
96c76efa | 298 | .buffers = 1, |
ef834f78 HV |
299 | .alpha_mask = 0xff000000, |
300 | }, | |
301 | { | |
96c76efa | 302 | .name = "4:2:2, biplanar, YUV", |
ef834f78 | 303 | .fourcc = V4L2_PIX_FMT_NV16M, |
96c76efa HV |
304 | .vdownsampling = { 1, 1 }, |
305 | .bit_depth = { 8, 8 }, | |
ef834f78 HV |
306 | .is_yuv = true, |
307 | .planes = 2, | |
96c76efa | 308 | .buffers = 2, |
ef834f78 HV |
309 | .data_offset = { PLANE0_DATA_OFFSET, 0 }, |
310 | }, | |
311 | { | |
96c76efa | 312 | .name = "4:2:2, biplanar, YVU", |
ef834f78 | 313 | .fourcc = V4L2_PIX_FMT_NV61M, |
96c76efa HV |
314 | .vdownsampling = { 1, 1 }, |
315 | .bit_depth = { 8, 8 }, | |
ef834f78 HV |
316 | .is_yuv = true, |
317 | .planes = 2, | |
96c76efa | 318 | .buffers = 2, |
ef834f78 HV |
319 | .data_offset = { 0, PLANE0_DATA_OFFSET }, |
320 | }, | |
b521c375 HV |
321 | { |
322 | .name = "4:2:0, triplanar, YUV", | |
323 | .fourcc = V4L2_PIX_FMT_YUV420M, | |
324 | .vdownsampling = { 1, 2, 2 }, | |
325 | .bit_depth = { 8, 4, 4 }, | |
326 | .is_yuv = true, | |
327 | .planes = 3, | |
328 | .buffers = 3, | |
329 | }, | |
330 | { | |
331 | .name = "4:2:0, triplanar, YVU", | |
332 | .fourcc = V4L2_PIX_FMT_YVU420M, | |
333 | .vdownsampling = { 1, 2, 2 }, | |
334 | .bit_depth = { 8, 4, 4 }, | |
335 | .is_yuv = true, | |
336 | .planes = 3, | |
337 | .buffers = 3, | |
338 | }, | |
339 | { | |
340 | .name = "4:2:0, biplanar, YUV", | |
341 | .fourcc = V4L2_PIX_FMT_NV12M, | |
342 | .vdownsampling = { 1, 2 }, | |
343 | .bit_depth = { 8, 8 }, | |
344 | .is_yuv = true, | |
345 | .planes = 2, | |
346 | .buffers = 2, | |
347 | }, | |
348 | { | |
349 | .name = "4:2:0, biplanar, YVU", | |
350 | .fourcc = V4L2_PIX_FMT_NV21M, | |
351 | .vdownsampling = { 1, 2 }, | |
352 | .bit_depth = { 8, 8 }, | |
353 | .is_yuv = true, | |
354 | .planes = 2, | |
355 | .buffers = 2, | |
356 | }, | |
ef834f78 HV |
357 | }; |
358 | ||
b521c375 HV |
359 | /* There are 6 multiplanar formats in the list */ |
360 | #define VIVID_MPLANAR_FORMATS 6 | |
ef834f78 | 361 | |
1fc78bc9 | 362 | const struct vivid_fmt *vivid_get_format(struct vivid_dev *dev, u32 pixelformat) |
ef834f78 HV |
363 | { |
364 | const struct vivid_fmt *fmt; | |
365 | unsigned k; | |
366 | ||
367 | for (k = 0; k < ARRAY_SIZE(vivid_formats); k++) { | |
368 | fmt = &vivid_formats[k]; | |
369 | if (fmt->fourcc == pixelformat) | |
ddcaee9d | 370 | if (fmt->buffers == 1 || dev->multiplanar) |
ef834f78 HV |
371 | return fmt; |
372 | } | |
373 | ||
374 | return NULL; | |
375 | } | |
376 | ||
377 | bool vivid_vid_can_loop(struct vivid_dev *dev) | |
378 | { | |
379 | if (dev->src_rect.width != dev->sink_rect.width || | |
380 | dev->src_rect.height != dev->sink_rect.height) | |
381 | return false; | |
382 | if (dev->fmt_cap->fourcc != dev->fmt_out->fourcc) | |
383 | return false; | |
384 | if (dev->field_cap != dev->field_out) | |
385 | return false; | |
ba24b442 HV |
386 | /* |
387 | * While this can be supported, it is just too much work | |
388 | * to actually implement. | |
389 | */ | |
390 | if (dev->field_cap == V4L2_FIELD_SEQ_TB || | |
391 | dev->field_cap == V4L2_FIELD_SEQ_BT) | |
392 | return false; | |
ef834f78 HV |
393 | if (vivid_is_svid_cap(dev) && vivid_is_svid_out(dev)) { |
394 | if (!(dev->std_cap & V4L2_STD_525_60) != | |
395 | !(dev->std_out & V4L2_STD_525_60)) | |
396 | return false; | |
397 | return true; | |
398 | } | |
399 | if (vivid_is_hdmi_cap(dev) && vivid_is_hdmi_out(dev)) | |
400 | return true; | |
401 | return false; | |
402 | } | |
403 | ||
404 | void vivid_send_source_change(struct vivid_dev *dev, unsigned type) | |
405 | { | |
406 | struct v4l2_event ev = { | |
407 | .type = V4L2_EVENT_SOURCE_CHANGE, | |
408 | .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION, | |
409 | }; | |
410 | unsigned i; | |
411 | ||
412 | for (i = 0; i < dev->num_inputs; i++) { | |
413 | ev.id = i; | |
414 | if (dev->input_type[i] == type) { | |
415 | if (video_is_registered(&dev->vid_cap_dev) && dev->has_vid_cap) | |
416 | v4l2_event_queue(&dev->vid_cap_dev, &ev); | |
417 | if (video_is_registered(&dev->vbi_cap_dev) && dev->has_vbi_cap) | |
418 | v4l2_event_queue(&dev->vbi_cap_dev, &ev); | |
419 | } | |
420 | } | |
421 | } | |
422 | ||
423 | /* | |
424 | * Conversion function that converts a single-planar format to a | |
425 | * single-plane multiplanar format. | |
426 | */ | |
427 | void fmt_sp2mp(const struct v4l2_format *sp_fmt, struct v4l2_format *mp_fmt) | |
428 | { | |
429 | struct v4l2_pix_format_mplane *mp = &mp_fmt->fmt.pix_mp; | |
430 | struct v4l2_plane_pix_format *ppix = &mp->plane_fmt[0]; | |
431 | const struct v4l2_pix_format *pix = &sp_fmt->fmt.pix; | |
432 | bool is_out = sp_fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT; | |
433 | ||
434 | memset(mp->reserved, 0, sizeof(mp->reserved)); | |
435 | mp_fmt->type = is_out ? V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE : | |
436 | V4L2_CAP_VIDEO_CAPTURE_MPLANE; | |
437 | mp->width = pix->width; | |
438 | mp->height = pix->height; | |
439 | mp->pixelformat = pix->pixelformat; | |
440 | mp->field = pix->field; | |
441 | mp->colorspace = pix->colorspace; | |
3e8a78d1 HV |
442 | mp->ycbcr_enc = pix->ycbcr_enc; |
443 | mp->quantization = pix->quantization; | |
ef834f78 HV |
444 | mp->num_planes = 1; |
445 | mp->flags = pix->flags; | |
446 | ppix->sizeimage = pix->sizeimage; | |
447 | ppix->bytesperline = pix->bytesperline; | |
448 | memset(ppix->reserved, 0, sizeof(ppix->reserved)); | |
449 | } | |
450 | ||
451 | int fmt_sp2mp_func(struct file *file, void *priv, | |
452 | struct v4l2_format *f, fmtfunc func) | |
453 | { | |
454 | struct v4l2_format fmt; | |
455 | struct v4l2_pix_format_mplane *mp = &fmt.fmt.pix_mp; | |
456 | struct v4l2_plane_pix_format *ppix = &mp->plane_fmt[0]; | |
457 | struct v4l2_pix_format *pix = &f->fmt.pix; | |
458 | int ret; | |
459 | ||
460 | /* Converts to a mplane format */ | |
461 | fmt_sp2mp(f, &fmt); | |
462 | /* Passes it to the generic mplane format function */ | |
463 | ret = func(file, priv, &fmt); | |
464 | /* Copies back the mplane data to the single plane format */ | |
465 | pix->width = mp->width; | |
466 | pix->height = mp->height; | |
467 | pix->pixelformat = mp->pixelformat; | |
468 | pix->field = mp->field; | |
469 | pix->colorspace = mp->colorspace; | |
3e8a78d1 HV |
470 | pix->ycbcr_enc = mp->ycbcr_enc; |
471 | pix->quantization = mp->quantization; | |
ef834f78 HV |
472 | pix->sizeimage = ppix->sizeimage; |
473 | pix->bytesperline = ppix->bytesperline; | |
474 | pix->flags = mp->flags; | |
475 | return ret; | |
476 | } | |
477 | ||
478 | /* v4l2_rect helper function: copy the width/height values */ | |
479 | void rect_set_size_to(struct v4l2_rect *r, const struct v4l2_rect *size) | |
480 | { | |
481 | r->width = size->width; | |
482 | r->height = size->height; | |
483 | } | |
484 | ||
485 | /* v4l2_rect helper function: width and height of r should be >= min_size */ | |
486 | void rect_set_min_size(struct v4l2_rect *r, const struct v4l2_rect *min_size) | |
487 | { | |
488 | if (r->width < min_size->width) | |
489 | r->width = min_size->width; | |
490 | if (r->height < min_size->height) | |
491 | r->height = min_size->height; | |
492 | } | |
493 | ||
494 | /* v4l2_rect helper function: width and height of r should be <= max_size */ | |
495 | void rect_set_max_size(struct v4l2_rect *r, const struct v4l2_rect *max_size) | |
496 | { | |
497 | if (r->width > max_size->width) | |
498 | r->width = max_size->width; | |
499 | if (r->height > max_size->height) | |
500 | r->height = max_size->height; | |
501 | } | |
502 | ||
503 | /* v4l2_rect helper function: r should be inside boundary */ | |
504 | void rect_map_inside(struct v4l2_rect *r, const struct v4l2_rect *boundary) | |
505 | { | |
506 | rect_set_max_size(r, boundary); | |
507 | if (r->left < boundary->left) | |
508 | r->left = boundary->left; | |
509 | if (r->top < boundary->top) | |
510 | r->top = boundary->top; | |
511 | if (r->left + r->width > boundary->width) | |
512 | r->left = boundary->width - r->width; | |
513 | if (r->top + r->height > boundary->height) | |
514 | r->top = boundary->height - r->height; | |
515 | } | |
516 | ||
517 | /* v4l2_rect helper function: return true if r1 has the same size as r2 */ | |
518 | bool rect_same_size(const struct v4l2_rect *r1, const struct v4l2_rect *r2) | |
519 | { | |
520 | return r1->width == r2->width && r1->height == r2->height; | |
521 | } | |
522 | ||
523 | /* v4l2_rect helper function: calculate the intersection of two rects */ | |
524 | struct v4l2_rect rect_intersect(const struct v4l2_rect *a, const struct v4l2_rect *b) | |
525 | { | |
526 | struct v4l2_rect r; | |
527 | int right, bottom; | |
528 | ||
529 | r.top = max(a->top, b->top); | |
530 | r.left = max(a->left, b->left); | |
531 | bottom = min(a->top + a->height, b->top + b->height); | |
532 | right = min(a->left + a->width, b->left + b->width); | |
533 | r.height = max(0, bottom - r.top); | |
534 | r.width = max(0, right - r.left); | |
535 | return r; | |
536 | } | |
537 | ||
538 | /* | |
539 | * v4l2_rect helper function: scale rect r by to->width / from->width and | |
540 | * to->height / from->height. | |
541 | */ | |
542 | void rect_scale(struct v4l2_rect *r, const struct v4l2_rect *from, | |
543 | const struct v4l2_rect *to) | |
544 | { | |
545 | if (from->width == 0 || from->height == 0) { | |
546 | r->left = r->top = r->width = r->height = 0; | |
547 | return; | |
548 | } | |
549 | r->left = (((r->left - from->left) * to->width) / from->width) & ~1; | |
550 | r->width = ((r->width * to->width) / from->width) & ~1; | |
551 | r->top = ((r->top - from->top) * to->height) / from->height; | |
552 | r->height = (r->height * to->height) / from->height; | |
553 | } | |
554 | ||
555 | bool rect_overlap(const struct v4l2_rect *r1, const struct v4l2_rect *r2) | |
556 | { | |
557 | /* | |
558 | * IF the left side of r1 is to the right of the right side of r2 OR | |
559 | * the left side of r2 is to the right of the right side of r1 THEN | |
560 | * they do not overlap. | |
561 | */ | |
562 | if (r1->left >= r2->left + r2->width || | |
563 | r2->left >= r1->left + r1->width) | |
564 | return false; | |
565 | /* | |
566 | * IF the top side of r1 is below the bottom of r2 OR | |
567 | * the top side of r2 is below the bottom of r1 THEN | |
568 | * they do not overlap. | |
569 | */ | |
570 | if (r1->top >= r2->top + r2->height || | |
571 | r2->top >= r1->top + r1->height) | |
572 | return false; | |
573 | return true; | |
574 | } | |
575 | int vivid_vid_adjust_sel(unsigned flags, struct v4l2_rect *r) | |
576 | { | |
577 | unsigned w = r->width; | |
578 | unsigned h = r->height; | |
579 | ||
580 | if (!(flags & V4L2_SEL_FLAG_LE)) { | |
581 | w++; | |
582 | h++; | |
583 | if (w < 2) | |
584 | w = 2; | |
585 | if (h < 2) | |
586 | h = 2; | |
587 | } | |
588 | if (!(flags & V4L2_SEL_FLAG_GE)) { | |
589 | if (w > MAX_WIDTH) | |
590 | w = MAX_WIDTH; | |
591 | if (h > MAX_HEIGHT) | |
592 | h = MAX_HEIGHT; | |
593 | } | |
594 | w = w & ~1; | |
595 | h = h & ~1; | |
596 | if (w < 2 || h < 2) | |
597 | return -ERANGE; | |
598 | if (w > MAX_WIDTH || h > MAX_HEIGHT) | |
599 | return -ERANGE; | |
600 | if (r->top < 0) | |
601 | r->top = 0; | |
602 | if (r->left < 0) | |
603 | r->left = 0; | |
604 | r->left &= ~1; | |
605 | r->top &= ~1; | |
606 | if (r->left + w > MAX_WIDTH) | |
607 | r->left = MAX_WIDTH - w; | |
608 | if (r->top + h > MAX_HEIGHT) | |
609 | r->top = MAX_HEIGHT - h; | |
610 | if ((flags & (V4L2_SEL_FLAG_GE | V4L2_SEL_FLAG_LE)) == | |
611 | (V4L2_SEL_FLAG_GE | V4L2_SEL_FLAG_LE) && | |
612 | (r->width != w || r->height != h)) | |
613 | return -ERANGE; | |
614 | r->width = w; | |
615 | r->height = h; | |
616 | return 0; | |
617 | } | |
618 | ||
619 | int vivid_enum_fmt_vid(struct file *file, void *priv, | |
620 | struct v4l2_fmtdesc *f) | |
621 | { | |
622 | struct vivid_dev *dev = video_drvdata(file); | |
623 | const struct vivid_fmt *fmt; | |
624 | ||
625 | if (f->index >= ARRAY_SIZE(vivid_formats) - | |
626 | (dev->multiplanar ? 0 : VIVID_MPLANAR_FORMATS)) | |
627 | return -EINVAL; | |
628 | ||
629 | fmt = &vivid_formats[f->index]; | |
630 | ||
631 | strlcpy(f->description, fmt->name, sizeof(f->description)); | |
632 | f->pixelformat = fmt->fourcc; | |
633 | return 0; | |
634 | } | |
635 | ||
636 | int vidioc_enum_fmt_vid_mplane(struct file *file, void *priv, | |
637 | struct v4l2_fmtdesc *f) | |
638 | { | |
639 | struct vivid_dev *dev = video_drvdata(file); | |
640 | ||
641 | if (!dev->multiplanar) | |
642 | return -ENOTTY; | |
643 | return vivid_enum_fmt_vid(file, priv, f); | |
644 | } | |
645 | ||
646 | int vidioc_enum_fmt_vid(struct file *file, void *priv, | |
647 | struct v4l2_fmtdesc *f) | |
648 | { | |
649 | struct vivid_dev *dev = video_drvdata(file); | |
650 | ||
651 | if (dev->multiplanar) | |
652 | return -ENOTTY; | |
653 | return vivid_enum_fmt_vid(file, priv, f); | |
654 | } | |
655 | ||
656 | int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id) | |
657 | { | |
658 | struct vivid_dev *dev = video_drvdata(file); | |
659 | struct video_device *vdev = video_devdata(file); | |
660 | ||
661 | if (vdev->vfl_dir == VFL_DIR_RX) { | |
662 | if (!vivid_is_sdtv_cap(dev)) | |
663 | return -ENODATA; | |
664 | *id = dev->std_cap; | |
665 | } else { | |
666 | if (!vivid_is_svid_out(dev)) | |
667 | return -ENODATA; | |
668 | *id = dev->std_out; | |
669 | } | |
670 | return 0; | |
671 | } | |
672 | ||
673 | int vidioc_g_dv_timings(struct file *file, void *_fh, | |
674 | struct v4l2_dv_timings *timings) | |
675 | { | |
676 | struct vivid_dev *dev = video_drvdata(file); | |
677 | struct video_device *vdev = video_devdata(file); | |
678 | ||
679 | if (vdev->vfl_dir == VFL_DIR_RX) { | |
680 | if (!vivid_is_hdmi_cap(dev)) | |
681 | return -ENODATA; | |
682 | *timings = dev->dv_timings_cap; | |
683 | } else { | |
684 | if (!vivid_is_hdmi_out(dev)) | |
685 | return -ENODATA; | |
686 | *timings = dev->dv_timings_out; | |
687 | } | |
688 | return 0; | |
689 | } | |
690 | ||
691 | int vidioc_enum_dv_timings(struct file *file, void *_fh, | |
692 | struct v4l2_enum_dv_timings *timings) | |
693 | { | |
694 | struct vivid_dev *dev = video_drvdata(file); | |
695 | struct video_device *vdev = video_devdata(file); | |
696 | ||
697 | if (vdev->vfl_dir == VFL_DIR_RX) { | |
698 | if (!vivid_is_hdmi_cap(dev)) | |
699 | return -ENODATA; | |
700 | } else { | |
701 | if (!vivid_is_hdmi_out(dev)) | |
702 | return -ENODATA; | |
703 | } | |
704 | return v4l2_enum_dv_timings_cap(timings, &vivid_dv_timings_cap, | |
705 | NULL, NULL); | |
706 | } | |
707 | ||
708 | int vidioc_dv_timings_cap(struct file *file, void *_fh, | |
709 | struct v4l2_dv_timings_cap *cap) | |
710 | { | |
711 | struct vivid_dev *dev = video_drvdata(file); | |
712 | struct video_device *vdev = video_devdata(file); | |
713 | ||
714 | if (vdev->vfl_dir == VFL_DIR_RX) { | |
715 | if (!vivid_is_hdmi_cap(dev)) | |
716 | return -ENODATA; | |
717 | } else { | |
718 | if (!vivid_is_hdmi_out(dev)) | |
719 | return -ENODATA; | |
720 | } | |
721 | *cap = vivid_dv_timings_cap; | |
722 | return 0; | |
723 | } | |
724 | ||
725 | int vidioc_g_edid(struct file *file, void *_fh, | |
726 | struct v4l2_edid *edid) | |
727 | { | |
728 | struct vivid_dev *dev = video_drvdata(file); | |
729 | struct video_device *vdev = video_devdata(file); | |
730 | ||
731 | memset(edid->reserved, 0, sizeof(edid->reserved)); | |
732 | if (vdev->vfl_dir == VFL_DIR_RX) { | |
733 | if (edid->pad >= dev->num_inputs) | |
734 | return -EINVAL; | |
735 | if (dev->input_type[edid->pad] != HDMI) | |
736 | return -EINVAL; | |
737 | } else { | |
738 | if (edid->pad >= dev->num_outputs) | |
739 | return -EINVAL; | |
740 | if (dev->output_type[edid->pad] != HDMI) | |
741 | return -EINVAL; | |
742 | } | |
743 | if (edid->start_block == 0 && edid->blocks == 0) { | |
744 | edid->blocks = dev->edid_blocks; | |
745 | return 0; | |
746 | } | |
747 | if (dev->edid_blocks == 0) | |
748 | return -ENODATA; | |
749 | if (edid->start_block >= dev->edid_blocks) | |
750 | return -EINVAL; | |
751 | if (edid->start_block + edid->blocks > dev->edid_blocks) | |
752 | edid->blocks = dev->edid_blocks - edid->start_block; | |
753 | memcpy(edid->edid, dev->edid, edid->blocks * 128); | |
754 | return 0; | |
755 | } |