2 * vivid-vid-common.c - common video support functions.
4 * Copyright 2014 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
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.
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
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>
29 #include "vivid-core.h"
30 #include "vivid-vid-common.h"
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 */
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
)
41 /* ------------------------------------------------------------------
43 ------------------------------------------------------------------*/
45 struct vivid_fmt vivid_formats
[] = {
47 .name
= "4:2:2, packed, YUYV",
48 .fourcc
= V4L2_PIX_FMT_YUYV
,
49 .vdownsampling
= { 1 },
54 .data_offset
= { PLANE0_DATA_OFFSET
},
57 .name
= "4:2:2, packed, UYVY",
58 .fourcc
= V4L2_PIX_FMT_UYVY
,
59 .vdownsampling
= { 1 },
66 .name
= "4:2:2, packed, YVYU",
67 .fourcc
= V4L2_PIX_FMT_YVYU
,
68 .vdownsampling
= { 1 },
75 .name
= "4:2:2, packed, VYUY",
76 .fourcc
= V4L2_PIX_FMT_VYUY
,
77 .vdownsampling
= { 1 },
84 .name
= "YUV 4:2:2 triplanar",
85 .fourcc
= V4L2_PIX_FMT_YUV422P
,
86 .vdownsampling
= { 1, 1, 1 },
87 .bit_depth
= { 8, 4, 4 },
93 .name
= "YUV 4:2:0 triplanar",
94 .fourcc
= V4L2_PIX_FMT_YUV420
,
95 .vdownsampling
= { 1, 2, 2 },
96 .bit_depth
= { 8, 4, 4 },
102 .name
= "YVU 4:2:0 triplanar",
103 .fourcc
= V4L2_PIX_FMT_YVU420
,
104 .vdownsampling
= { 1, 2, 2 },
105 .bit_depth
= { 8, 4, 4 },
111 .name
= "YUV 4:2:0 biplanar",
112 .fourcc
= V4L2_PIX_FMT_NV12
,
113 .vdownsampling
= { 1, 2 },
114 .bit_depth
= { 8, 8 },
120 .name
= "YVU 4:2:0 biplanar",
121 .fourcc
= V4L2_PIX_FMT_NV21
,
122 .vdownsampling
= { 1, 2 },
123 .bit_depth
= { 8, 8 },
129 .name
= "YUV 4:2:2 biplanar",
130 .fourcc
= V4L2_PIX_FMT_NV16
,
131 .vdownsampling
= { 1, 1 },
132 .bit_depth
= { 8, 8 },
138 .name
= "YVU 4:2:2 biplanar",
139 .fourcc
= V4L2_PIX_FMT_NV61
,
140 .vdownsampling
= { 1, 1 },
141 .bit_depth
= { 8, 8 },
147 .name
= "Monochrome",
148 .fourcc
= V4L2_PIX_FMT_GREY
,
149 .vdownsampling
= { 1 },
156 .name
= "RGB565 (LE)",
157 .fourcc
= V4L2_PIX_FMT_RGB565
, /* gggbbbbb rrrrrggg */
158 .vdownsampling
= { 1 },
162 .can_do_overlay
= true,
165 .name
= "RGB565 (BE)",
166 .fourcc
= V4L2_PIX_FMT_RGB565X
, /* rrrrrggg gggbbbbb */
167 .vdownsampling
= { 1 },
171 .can_do_overlay
= true,
175 .fourcc
= V4L2_PIX_FMT_RGB444
, /* xxxxrrrr ggggbbbb */
176 .vdownsampling
= { 1 },
183 .fourcc
= V4L2_PIX_FMT_XRGB444
, /* xxxxrrrr ggggbbbb */
184 .vdownsampling
= { 1 },
191 .fourcc
= V4L2_PIX_FMT_ARGB444
, /* aaaarrrr ggggbbbb */
192 .vdownsampling
= { 1 },
196 .alpha_mask
= 0x00f0,
199 .name
= "RGB555 (LE)",
200 .fourcc
= V4L2_PIX_FMT_RGB555
, /* gggbbbbb xrrrrrgg */
201 .vdownsampling
= { 1 },
205 .can_do_overlay
= true,
208 .name
= "XRGB555 (LE)",
209 .fourcc
= V4L2_PIX_FMT_XRGB555
, /* gggbbbbb xrrrrrgg */
210 .vdownsampling
= { 1 },
214 .can_do_overlay
= true,
217 .name
= "ARGB555 (LE)",
218 .fourcc
= V4L2_PIX_FMT_ARGB555
, /* gggbbbbb arrrrrgg */
219 .vdownsampling
= { 1 },
223 .can_do_overlay
= true,
224 .alpha_mask
= 0x8000,
227 .name
= "RGB555 (BE)",
228 .fourcc
= V4L2_PIX_FMT_RGB555X
, /* xrrrrrgg gggbbbbb */
229 .vdownsampling
= { 1 },
235 .name
= "XRGB555 (BE)",
236 .fourcc
= V4L2_PIX_FMT_XRGB555X
, /* xrrrrrgg gggbbbbb */
237 .vdownsampling
= { 1 },
243 .name
= "ARGB555 (BE)",
244 .fourcc
= V4L2_PIX_FMT_ARGB555X
, /* arrrrrgg gggbbbbb */
245 .vdownsampling
= { 1 },
249 .alpha_mask
= 0x0080,
252 .name
= "RGB24 (LE)",
253 .fourcc
= V4L2_PIX_FMT_RGB24
, /* rgb */
254 .vdownsampling
= { 1 },
260 .name
= "RGB24 (BE)",
261 .fourcc
= V4L2_PIX_FMT_BGR24
, /* bgr */
262 .vdownsampling
= { 1 },
268 .name
= "RGB32 (LE)",
269 .fourcc
= V4L2_PIX_FMT_RGB32
, /* xrgb */
270 .vdownsampling
= { 1 },
276 .name
= "RGB32 (BE)",
277 .fourcc
= V4L2_PIX_FMT_BGR32
, /* bgrx */
278 .vdownsampling
= { 1 },
284 .name
= "XRGB32 (LE)",
285 .fourcc
= V4L2_PIX_FMT_XRGB32
, /* xrgb */
286 .vdownsampling
= { 1 },
292 .name
= "XRGB32 (BE)",
293 .fourcc
= V4L2_PIX_FMT_XBGR32
, /* bgrx */
294 .vdownsampling
= { 1 },
300 .name
= "ARGB32 (LE)",
301 .fourcc
= V4L2_PIX_FMT_ARGB32
, /* argb */
302 .vdownsampling
= { 1 },
306 .alpha_mask
= 0x000000ff,
309 .name
= "ARGB32 (BE)",
310 .fourcc
= V4L2_PIX_FMT_ABGR32
, /* bgra */
311 .vdownsampling
= { 1 },
315 .alpha_mask
= 0xff000000,
318 .name
= "4:2:2, biplanar, YUV",
319 .fourcc
= V4L2_PIX_FMT_NV16M
,
320 .vdownsampling
= { 1, 1 },
321 .bit_depth
= { 8, 8 },
325 .data_offset
= { PLANE0_DATA_OFFSET
, 0 },
328 .name
= "4:2:2, biplanar, YVU",
329 .fourcc
= V4L2_PIX_FMT_NV61M
,
330 .vdownsampling
= { 1, 1 },
331 .bit_depth
= { 8, 8 },
335 .data_offset
= { 0, PLANE0_DATA_OFFSET
},
338 .name
= "4:2:0, triplanar, YUV",
339 .fourcc
= V4L2_PIX_FMT_YUV420M
,
340 .vdownsampling
= { 1, 2, 2 },
341 .bit_depth
= { 8, 4, 4 },
347 .name
= "4:2:0, triplanar, YVU",
348 .fourcc
= V4L2_PIX_FMT_YVU420M
,
349 .vdownsampling
= { 1, 2, 2 },
350 .bit_depth
= { 8, 4, 4 },
356 .name
= "4:2:0, biplanar, YUV",
357 .fourcc
= V4L2_PIX_FMT_NV12M
,
358 .vdownsampling
= { 1, 2 },
359 .bit_depth
= { 8, 8 },
365 .name
= "4:2:0, biplanar, YVU",
366 .fourcc
= V4L2_PIX_FMT_NV21M
,
367 .vdownsampling
= { 1, 2 },
368 .bit_depth
= { 8, 8 },
375 /* There are 6 multiplanar formats in the list */
376 #define VIVID_MPLANAR_FORMATS 6
378 const struct vivid_fmt
*vivid_get_format(struct vivid_dev
*dev
, u32 pixelformat
)
380 const struct vivid_fmt
*fmt
;
383 for (k
= 0; k
< ARRAY_SIZE(vivid_formats
); k
++) {
384 fmt
= &vivid_formats
[k
];
385 if (fmt
->fourcc
== pixelformat
)
386 if (fmt
->buffers
== 1 || dev
->multiplanar
)
393 bool vivid_vid_can_loop(struct vivid_dev
*dev
)
395 if (dev
->src_rect
.width
!= dev
->sink_rect
.width
||
396 dev
->src_rect
.height
!= dev
->sink_rect
.height
)
398 if (dev
->fmt_cap
->fourcc
!= dev
->fmt_out
->fourcc
)
400 if (dev
->field_cap
!= dev
->field_out
)
403 * While this can be supported, it is just too much work
404 * to actually implement.
406 if (dev
->field_cap
== V4L2_FIELD_SEQ_TB
||
407 dev
->field_cap
== V4L2_FIELD_SEQ_BT
)
409 if (vivid_is_svid_cap(dev
) && vivid_is_svid_out(dev
)) {
410 if (!(dev
->std_cap
& V4L2_STD_525_60
) !=
411 !(dev
->std_out
& V4L2_STD_525_60
))
415 if (vivid_is_hdmi_cap(dev
) && vivid_is_hdmi_out(dev
))
420 void vivid_send_source_change(struct vivid_dev
*dev
, unsigned type
)
422 struct v4l2_event ev
= {
423 .type
= V4L2_EVENT_SOURCE_CHANGE
,
424 .u
.src_change
.changes
= V4L2_EVENT_SRC_CH_RESOLUTION
,
428 for (i
= 0; i
< dev
->num_inputs
; i
++) {
430 if (dev
->input_type
[i
] == type
) {
431 if (video_is_registered(&dev
->vid_cap_dev
) && dev
->has_vid_cap
)
432 v4l2_event_queue(&dev
->vid_cap_dev
, &ev
);
433 if (video_is_registered(&dev
->vbi_cap_dev
) && dev
->has_vbi_cap
)
434 v4l2_event_queue(&dev
->vbi_cap_dev
, &ev
);
440 * Conversion function that converts a single-planar format to a
441 * single-plane multiplanar format.
443 void fmt_sp2mp(const struct v4l2_format
*sp_fmt
, struct v4l2_format
*mp_fmt
)
445 struct v4l2_pix_format_mplane
*mp
= &mp_fmt
->fmt
.pix_mp
;
446 struct v4l2_plane_pix_format
*ppix
= &mp
->plane_fmt
[0];
447 const struct v4l2_pix_format
*pix
= &sp_fmt
->fmt
.pix
;
448 bool is_out
= sp_fmt
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
;
450 memset(mp
->reserved
, 0, sizeof(mp
->reserved
));
451 mp_fmt
->type
= is_out
? V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
:
452 V4L2_CAP_VIDEO_CAPTURE_MPLANE
;
453 mp
->width
= pix
->width
;
454 mp
->height
= pix
->height
;
455 mp
->pixelformat
= pix
->pixelformat
;
456 mp
->field
= pix
->field
;
457 mp
->colorspace
= pix
->colorspace
;
458 mp
->ycbcr_enc
= pix
->ycbcr_enc
;
459 mp
->quantization
= pix
->quantization
;
461 mp
->flags
= pix
->flags
;
462 ppix
->sizeimage
= pix
->sizeimage
;
463 ppix
->bytesperline
= pix
->bytesperline
;
464 memset(ppix
->reserved
, 0, sizeof(ppix
->reserved
));
467 int fmt_sp2mp_func(struct file
*file
, void *priv
,
468 struct v4l2_format
*f
, fmtfunc func
)
470 struct v4l2_format fmt
;
471 struct v4l2_pix_format_mplane
*mp
= &fmt
.fmt
.pix_mp
;
472 struct v4l2_plane_pix_format
*ppix
= &mp
->plane_fmt
[0];
473 struct v4l2_pix_format
*pix
= &f
->fmt
.pix
;
476 /* Converts to a mplane format */
478 /* Passes it to the generic mplane format function */
479 ret
= func(file
, priv
, &fmt
);
480 /* Copies back the mplane data to the single plane format */
481 pix
->width
= mp
->width
;
482 pix
->height
= mp
->height
;
483 pix
->pixelformat
= mp
->pixelformat
;
484 pix
->field
= mp
->field
;
485 pix
->colorspace
= mp
->colorspace
;
486 pix
->ycbcr_enc
= mp
->ycbcr_enc
;
487 pix
->quantization
= mp
->quantization
;
488 pix
->sizeimage
= ppix
->sizeimage
;
489 pix
->bytesperline
= ppix
->bytesperline
;
490 pix
->flags
= mp
->flags
;
494 /* v4l2_rect helper function: copy the width/height values */
495 void rect_set_size_to(struct v4l2_rect
*r
, const struct v4l2_rect
*size
)
497 r
->width
= size
->width
;
498 r
->height
= size
->height
;
501 /* v4l2_rect helper function: width and height of r should be >= min_size */
502 void rect_set_min_size(struct v4l2_rect
*r
, const struct v4l2_rect
*min_size
)
504 if (r
->width
< min_size
->width
)
505 r
->width
= min_size
->width
;
506 if (r
->height
< min_size
->height
)
507 r
->height
= min_size
->height
;
510 /* v4l2_rect helper function: width and height of r should be <= max_size */
511 void rect_set_max_size(struct v4l2_rect
*r
, const struct v4l2_rect
*max_size
)
513 if (r
->width
> max_size
->width
)
514 r
->width
= max_size
->width
;
515 if (r
->height
> max_size
->height
)
516 r
->height
= max_size
->height
;
519 /* v4l2_rect helper function: r should be inside boundary */
520 void rect_map_inside(struct v4l2_rect
*r
, const struct v4l2_rect
*boundary
)
522 rect_set_max_size(r
, boundary
);
523 if (r
->left
< boundary
->left
)
524 r
->left
= boundary
->left
;
525 if (r
->top
< boundary
->top
)
526 r
->top
= boundary
->top
;
527 if (r
->left
+ r
->width
> boundary
->width
)
528 r
->left
= boundary
->width
- r
->width
;
529 if (r
->top
+ r
->height
> boundary
->height
)
530 r
->top
= boundary
->height
- r
->height
;
533 /* v4l2_rect helper function: return true if r1 has the same size as r2 */
534 bool rect_same_size(const struct v4l2_rect
*r1
, const struct v4l2_rect
*r2
)
536 return r1
->width
== r2
->width
&& r1
->height
== r2
->height
;
539 /* v4l2_rect helper function: calculate the intersection of two rects */
540 struct v4l2_rect
rect_intersect(const struct v4l2_rect
*a
, const struct v4l2_rect
*b
)
545 r
.top
= max(a
->top
, b
->top
);
546 r
.left
= max(a
->left
, b
->left
);
547 bottom
= min(a
->top
+ a
->height
, b
->top
+ b
->height
);
548 right
= min(a
->left
+ a
->width
, b
->left
+ b
->width
);
549 r
.height
= max(0, bottom
- r
.top
);
550 r
.width
= max(0, right
- r
.left
);
555 * v4l2_rect helper function: scale rect r by to->width / from->width and
556 * to->height / from->height.
558 void rect_scale(struct v4l2_rect
*r
, const struct v4l2_rect
*from
,
559 const struct v4l2_rect
*to
)
561 if (from
->width
== 0 || from
->height
== 0) {
562 r
->left
= r
->top
= r
->width
= r
->height
= 0;
565 r
->left
= (((r
->left
- from
->left
) * to
->width
) / from
->width
) & ~1;
566 r
->width
= ((r
->width
* to
->width
) / from
->width
) & ~1;
567 r
->top
= ((r
->top
- from
->top
) * to
->height
) / from
->height
;
568 r
->height
= (r
->height
* to
->height
) / from
->height
;
571 bool rect_overlap(const struct v4l2_rect
*r1
, const struct v4l2_rect
*r2
)
574 * IF the left side of r1 is to the right of the right side of r2 OR
575 * the left side of r2 is to the right of the right side of r1 THEN
576 * they do not overlap.
578 if (r1
->left
>= r2
->left
+ r2
->width
||
579 r2
->left
>= r1
->left
+ r1
->width
)
582 * IF the top side of r1 is below the bottom of r2 OR
583 * the top side of r2 is below the bottom of r1 THEN
584 * they do not overlap.
586 if (r1
->top
>= r2
->top
+ r2
->height
||
587 r2
->top
>= r1
->top
+ r1
->height
)
591 int vivid_vid_adjust_sel(unsigned flags
, struct v4l2_rect
*r
)
593 unsigned w
= r
->width
;
594 unsigned h
= r
->height
;
596 if (!(flags
& V4L2_SEL_FLAG_LE
)) {
604 if (!(flags
& V4L2_SEL_FLAG_GE
)) {
614 if (w
> MAX_WIDTH
|| h
> MAX_HEIGHT
)
622 if (r
->left
+ w
> MAX_WIDTH
)
623 r
->left
= MAX_WIDTH
- w
;
624 if (r
->top
+ h
> MAX_HEIGHT
)
625 r
->top
= MAX_HEIGHT
- h
;
626 if ((flags
& (V4L2_SEL_FLAG_GE
| V4L2_SEL_FLAG_LE
)) ==
627 (V4L2_SEL_FLAG_GE
| V4L2_SEL_FLAG_LE
) &&
628 (r
->width
!= w
|| r
->height
!= h
))
635 int vivid_enum_fmt_vid(struct file
*file
, void *priv
,
636 struct v4l2_fmtdesc
*f
)
638 struct vivid_dev
*dev
= video_drvdata(file
);
639 const struct vivid_fmt
*fmt
;
641 if (f
->index
>= ARRAY_SIZE(vivid_formats
) -
642 (dev
->multiplanar
? 0 : VIVID_MPLANAR_FORMATS
))
645 fmt
= &vivid_formats
[f
->index
];
647 strlcpy(f
->description
, fmt
->name
, sizeof(f
->description
));
648 f
->pixelformat
= fmt
->fourcc
;
652 int vidioc_enum_fmt_vid_mplane(struct file
*file
, void *priv
,
653 struct v4l2_fmtdesc
*f
)
655 struct vivid_dev
*dev
= video_drvdata(file
);
657 if (!dev
->multiplanar
)
659 return vivid_enum_fmt_vid(file
, priv
, f
);
662 int vidioc_enum_fmt_vid(struct file
*file
, void *priv
,
663 struct v4l2_fmtdesc
*f
)
665 struct vivid_dev
*dev
= video_drvdata(file
);
667 if (dev
->multiplanar
)
669 return vivid_enum_fmt_vid(file
, priv
, f
);
672 int vidioc_g_std(struct file
*file
, void *priv
, v4l2_std_id
*id
)
674 struct vivid_dev
*dev
= video_drvdata(file
);
675 struct video_device
*vdev
= video_devdata(file
);
677 if (vdev
->vfl_dir
== VFL_DIR_RX
) {
678 if (!vivid_is_sdtv_cap(dev
))
682 if (!vivid_is_svid_out(dev
))
689 int vidioc_g_dv_timings(struct file
*file
, void *_fh
,
690 struct v4l2_dv_timings
*timings
)
692 struct vivid_dev
*dev
= video_drvdata(file
);
693 struct video_device
*vdev
= video_devdata(file
);
695 if (vdev
->vfl_dir
== VFL_DIR_RX
) {
696 if (!vivid_is_hdmi_cap(dev
))
698 *timings
= dev
->dv_timings_cap
;
700 if (!vivid_is_hdmi_out(dev
))
702 *timings
= dev
->dv_timings_out
;
707 int vidioc_enum_dv_timings(struct file
*file
, void *_fh
,
708 struct v4l2_enum_dv_timings
*timings
)
710 struct vivid_dev
*dev
= video_drvdata(file
);
711 struct video_device
*vdev
= video_devdata(file
);
713 if (vdev
->vfl_dir
== VFL_DIR_RX
) {
714 if (!vivid_is_hdmi_cap(dev
))
717 if (!vivid_is_hdmi_out(dev
))
720 return v4l2_enum_dv_timings_cap(timings
, &vivid_dv_timings_cap
,
724 int vidioc_dv_timings_cap(struct file
*file
, void *_fh
,
725 struct v4l2_dv_timings_cap
*cap
)
727 struct vivid_dev
*dev
= video_drvdata(file
);
728 struct video_device
*vdev
= video_devdata(file
);
730 if (vdev
->vfl_dir
== VFL_DIR_RX
) {
731 if (!vivid_is_hdmi_cap(dev
))
734 if (!vivid_is_hdmi_out(dev
))
737 *cap
= vivid_dv_timings_cap
;
741 int vidioc_g_edid(struct file
*file
, void *_fh
,
742 struct v4l2_edid
*edid
)
744 struct vivid_dev
*dev
= video_drvdata(file
);
745 struct video_device
*vdev
= video_devdata(file
);
747 memset(edid
->reserved
, 0, sizeof(edid
->reserved
));
748 if (vdev
->vfl_dir
== VFL_DIR_RX
) {
749 if (edid
->pad
>= dev
->num_inputs
)
751 if (dev
->input_type
[edid
->pad
] != HDMI
)
754 if (edid
->pad
>= dev
->num_outputs
)
756 if (dev
->output_type
[edid
->pad
] != HDMI
)
759 if (edid
->start_block
== 0 && edid
->blocks
== 0) {
760 edid
->blocks
= dev
->edid_blocks
;
763 if (dev
->edid_blocks
== 0)
765 if (edid
->start_block
>= dev
->edid_blocks
)
767 if (edid
->start_block
+ edid
->blocks
> dev
->edid_blocks
)
768 edid
->blocks
= dev
->edid_blocks
- edid
->start_block
;
769 memcpy(edid
->edid
, dev
->edid
, edid
->blocks
* 128);