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