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