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, 14000000, 775000000,
37 V4L2_DV_BT_STD_CEA861 | V4L2_DV_BT_STD_DMT |
38 V4L2_DV_BT_STD_CVT | V4L2_DV_BT_STD_GTF,
39 V4L2_DV_BT_CAP_PROGRESSIVE | V4L2_DV_BT_CAP_INTERLACED)
42 /* ------------------------------------------------------------------
44 ------------------------------------------------------------------*/
46 struct vivid_fmt vivid_formats[] = {
48 .name = "4:2:2, packed, YUYV",
49 .fourcc = V4L2_PIX_FMT_YUYV,
50 .vdownsampling = { 1 },
55 .data_offset = { PLANE0_DATA_OFFSET },
58 .name = "4:2:2, packed, UYVY",
59 .fourcc = V4L2_PIX_FMT_UYVY,
60 .vdownsampling = { 1 },
67 .name = "4:2:2, packed, YVYU",
68 .fourcc = V4L2_PIX_FMT_YVYU,
69 .vdownsampling = { 1 },
76 .name = "4:2:2, packed, VYUY",
77 .fourcc = V4L2_PIX_FMT_VYUY,
78 .vdownsampling = { 1 },
85 .name = "YUV 4:2:2 triplanar",
86 .fourcc = V4L2_PIX_FMT_YUV422P,
87 .vdownsampling = { 1, 1, 1 },
88 .bit_depth = { 8, 4, 4 },
94 .name = "YUV 4:2:0 triplanar",
95 .fourcc = V4L2_PIX_FMT_YUV420,
96 .vdownsampling = { 1, 2, 2 },
97 .bit_depth = { 8, 4, 4 },
103 .name = "YVU 4:2:0 triplanar",
104 .fourcc = V4L2_PIX_FMT_YVU420,
105 .vdownsampling = { 1, 2, 2 },
106 .bit_depth = { 8, 4, 4 },
112 .name = "YUV 4:2:0 biplanar",
113 .fourcc = V4L2_PIX_FMT_NV12,
114 .vdownsampling = { 1, 2 },
115 .bit_depth = { 8, 8 },
121 .name = "YVU 4:2:0 biplanar",
122 .fourcc = V4L2_PIX_FMT_NV21,
123 .vdownsampling = { 1, 2 },
124 .bit_depth = { 8, 8 },
130 .name = "YUV 4:2:2 biplanar",
131 .fourcc = V4L2_PIX_FMT_NV16,
132 .vdownsampling = { 1, 1 },
133 .bit_depth = { 8, 8 },
139 .name = "YVU 4:2:2 biplanar",
140 .fourcc = V4L2_PIX_FMT_NV61,
141 .vdownsampling = { 1, 1 },
142 .bit_depth = { 8, 8 },
148 .name = "YUV 4:4:4 biplanar",
149 .fourcc = V4L2_PIX_FMT_NV24,
150 .vdownsampling = { 1, 1 },
151 .bit_depth = { 8, 16 },
157 .name = "YVU 4:4:4 biplanar",
158 .fourcc = V4L2_PIX_FMT_NV42,
159 .vdownsampling = { 1, 1 },
160 .bit_depth = { 8, 16 },
166 .name = "YUV555 (LE)",
167 .fourcc = V4L2_PIX_FMT_YUV555, /* uuuvvvvv ayyyyyuu */
168 .vdownsampling = { 1 },
172 .alpha_mask = 0x8000,
175 .name = "YUV565 (LE)",
176 .fourcc = V4L2_PIX_FMT_YUV565, /* uuuvvvvv yyyyyuuu */
177 .vdownsampling = { 1 },
184 .fourcc = V4L2_PIX_FMT_YUV444, /* uuuuvvvv aaaayyyy */
185 .vdownsampling = { 1 },
189 .alpha_mask = 0xf000,
192 .name = "YUV32 (LE)",
193 .fourcc = V4L2_PIX_FMT_YUV32, /* ayuv */
194 .vdownsampling = { 1 },
198 .alpha_mask = 0x000000ff,
201 .name = "Monochrome",
202 .fourcc = V4L2_PIX_FMT_GREY,
203 .vdownsampling = { 1 },
211 .fourcc = V4L2_PIX_FMT_RGB332, /* rrrgggbb */
212 .vdownsampling = { 1 },
218 .name = "RGB565 (LE)",
219 .fourcc = V4L2_PIX_FMT_RGB565, /* gggbbbbb rrrrrggg */
220 .vdownsampling = { 1 },
224 .can_do_overlay = true,
227 .name = "RGB565 (BE)",
228 .fourcc = V4L2_PIX_FMT_RGB565X, /* rrrrrggg gggbbbbb */
229 .vdownsampling = { 1 },
233 .can_do_overlay = true,
237 .fourcc = V4L2_PIX_FMT_RGB444, /* xxxxrrrr ggggbbbb */
238 .vdownsampling = { 1 },
245 .fourcc = V4L2_PIX_FMT_XRGB444, /* xxxxrrrr ggggbbbb */
246 .vdownsampling = { 1 },
253 .fourcc = V4L2_PIX_FMT_ARGB444, /* aaaarrrr ggggbbbb */
254 .vdownsampling = { 1 },
258 .alpha_mask = 0x00f0,
261 .name = "RGB555 (LE)",
262 .fourcc = V4L2_PIX_FMT_RGB555, /* gggbbbbb xrrrrrgg */
263 .vdownsampling = { 1 },
267 .can_do_overlay = true,
270 .name = "XRGB555 (LE)",
271 .fourcc = V4L2_PIX_FMT_XRGB555, /* gggbbbbb xrrrrrgg */
272 .vdownsampling = { 1 },
276 .can_do_overlay = true,
279 .name = "ARGB555 (LE)",
280 .fourcc = V4L2_PIX_FMT_ARGB555, /* gggbbbbb arrrrrgg */
281 .vdownsampling = { 1 },
285 .can_do_overlay = true,
286 .alpha_mask = 0x8000,
289 .name = "RGB555 (BE)",
290 .fourcc = V4L2_PIX_FMT_RGB555X, /* xrrrrrgg gggbbbbb */
291 .vdownsampling = { 1 },
297 .name = "XRGB555 (BE)",
298 .fourcc = V4L2_PIX_FMT_XRGB555X, /* xrrrrrgg gggbbbbb */
299 .vdownsampling = { 1 },
305 .name = "ARGB555 (BE)",
306 .fourcc = V4L2_PIX_FMT_ARGB555X, /* arrrrrgg gggbbbbb */
307 .vdownsampling = { 1 },
311 .alpha_mask = 0x0080,
314 .name = "RGB24 (LE)",
315 .fourcc = V4L2_PIX_FMT_RGB24, /* rgb */
316 .vdownsampling = { 1 },
322 .name = "RGB24 (BE)",
323 .fourcc = V4L2_PIX_FMT_BGR24, /* bgr */
324 .vdownsampling = { 1 },
331 .fourcc = V4L2_PIX_FMT_BGR666, /* bbbbbbgg ggggrrrr rrxxxxxx */
332 .vdownsampling = { 1 },
338 .name = "RGB32 (LE)",
339 .fourcc = V4L2_PIX_FMT_RGB32, /* xrgb */
340 .vdownsampling = { 1 },
346 .name = "RGB32 (BE)",
347 .fourcc = V4L2_PIX_FMT_BGR32, /* bgrx */
348 .vdownsampling = { 1 },
354 .name = "XRGB32 (LE)",
355 .fourcc = V4L2_PIX_FMT_XRGB32, /* xrgb */
356 .vdownsampling = { 1 },
362 .name = "XRGB32 (BE)",
363 .fourcc = V4L2_PIX_FMT_XBGR32, /* bgrx */
364 .vdownsampling = { 1 },
370 .name = "ARGB32 (LE)",
371 .fourcc = V4L2_PIX_FMT_ARGB32, /* argb */
372 .vdownsampling = { 1 },
376 .alpha_mask = 0x000000ff,
379 .name = "ARGB32 (BE)",
380 .fourcc = V4L2_PIX_FMT_ABGR32, /* bgra */
381 .vdownsampling = { 1 },
385 .alpha_mask = 0xff000000,
388 .name = "Bayer BG/GR",
389 .fourcc = V4L2_PIX_FMT_SBGGR8, /* Bayer BG/GR */
390 .vdownsampling = { 1 },
396 .name = "Bayer GB/RG",
397 .fourcc = V4L2_PIX_FMT_SGBRG8, /* Bayer GB/RG */
398 .vdownsampling = { 1 },
404 .name = "Bayer GR/BG",
405 .fourcc = V4L2_PIX_FMT_SGRBG8, /* Bayer GR/BG */
406 .vdownsampling = { 1 },
412 .name = "Bayer RG/GB",
413 .fourcc = V4L2_PIX_FMT_SRGGB8, /* Bayer RG/GB */
414 .vdownsampling = { 1 },
420 .name = "4:2:2, biplanar, YUV",
421 .fourcc = V4L2_PIX_FMT_NV16M,
422 .vdownsampling = { 1, 1 },
423 .bit_depth = { 8, 8 },
427 .data_offset = { PLANE0_DATA_OFFSET, 0 },
430 .name = "4:2:2, biplanar, YVU",
431 .fourcc = V4L2_PIX_FMT_NV61M,
432 .vdownsampling = { 1, 1 },
433 .bit_depth = { 8, 8 },
437 .data_offset = { 0, PLANE0_DATA_OFFSET },
440 .name = "4:2:0, triplanar, YUV",
441 .fourcc = V4L2_PIX_FMT_YUV420M,
442 .vdownsampling = { 1, 2, 2 },
443 .bit_depth = { 8, 4, 4 },
449 .name = "4:2:0, triplanar, YVU",
450 .fourcc = V4L2_PIX_FMT_YVU420M,
451 .vdownsampling = { 1, 2, 2 },
452 .bit_depth = { 8, 4, 4 },
458 .name = "4:2:0, biplanar, YUV",
459 .fourcc = V4L2_PIX_FMT_NV12M,
460 .vdownsampling = { 1, 2 },
461 .bit_depth = { 8, 8 },
467 .name = "4:2:0, biplanar, YVU",
468 .fourcc = V4L2_PIX_FMT_NV21M,
469 .vdownsampling = { 1, 2 },
470 .bit_depth = { 8, 8 },
477 /* There are 6 multiplanar formats in the list */
478 #define VIVID_MPLANAR_FORMATS 6
480 const struct vivid_fmt *vivid_get_format(struct vivid_dev *dev, u32 pixelformat)
482 const struct vivid_fmt *fmt;
485 for (k = 0; k < ARRAY_SIZE(vivid_formats); k++) {
486 fmt = &vivid_formats[k];
487 if (fmt->fourcc == pixelformat)
488 if (fmt->buffers == 1 || dev->multiplanar)
495 bool vivid_vid_can_loop(struct vivid_dev *dev)
497 if (dev->src_rect.width != dev->sink_rect.width ||
498 dev->src_rect.height != dev->sink_rect.height)
500 if (dev->fmt_cap->fourcc != dev->fmt_out->fourcc)
502 if (dev->field_cap != dev->field_out)
505 * While this can be supported, it is just too much work
506 * to actually implement.
508 if (dev->field_cap == V4L2_FIELD_SEQ_TB ||
509 dev->field_cap == V4L2_FIELD_SEQ_BT)
511 if (vivid_is_svid_cap(dev) && vivid_is_svid_out(dev)) {
512 if (!(dev->std_cap & V4L2_STD_525_60) !=
513 !(dev->std_out & V4L2_STD_525_60))
517 if (vivid_is_hdmi_cap(dev) && vivid_is_hdmi_out(dev))
522 void vivid_send_source_change(struct vivid_dev *dev, unsigned type)
524 struct v4l2_event ev = {
525 .type = V4L2_EVENT_SOURCE_CHANGE,
526 .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION,
530 for (i = 0; i < dev->num_inputs; i++) {
532 if (dev->input_type[i] == type) {
533 if (video_is_registered(&dev->vid_cap_dev) && dev->has_vid_cap)
534 v4l2_event_queue(&dev->vid_cap_dev, &ev);
535 if (video_is_registered(&dev->vbi_cap_dev) && dev->has_vbi_cap)
536 v4l2_event_queue(&dev->vbi_cap_dev, &ev);
542 * Conversion function that converts a single-planar format to a
543 * single-plane multiplanar format.
545 void fmt_sp2mp(const struct v4l2_format *sp_fmt, struct v4l2_format *mp_fmt)
547 struct v4l2_pix_format_mplane *mp = &mp_fmt->fmt.pix_mp;
548 struct v4l2_plane_pix_format *ppix = &mp->plane_fmt[0];
549 const struct v4l2_pix_format *pix = &sp_fmt->fmt.pix;
550 bool is_out = sp_fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT;
552 memset(mp->reserved, 0, sizeof(mp->reserved));
553 mp_fmt->type = is_out ? V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE :
554 V4L2_CAP_VIDEO_CAPTURE_MPLANE;
555 mp->width = pix->width;
556 mp->height = pix->height;
557 mp->pixelformat = pix->pixelformat;
558 mp->field = pix->field;
559 mp->colorspace = pix->colorspace;
560 mp->ycbcr_enc = pix->ycbcr_enc;
561 mp->quantization = pix->quantization;
563 mp->flags = pix->flags;
564 ppix->sizeimage = pix->sizeimage;
565 ppix->bytesperline = pix->bytesperline;
566 memset(ppix->reserved, 0, sizeof(ppix->reserved));
569 int fmt_sp2mp_func(struct file *file, void *priv,
570 struct v4l2_format *f, fmtfunc func)
572 struct v4l2_format fmt;
573 struct v4l2_pix_format_mplane *mp = &fmt.fmt.pix_mp;
574 struct v4l2_plane_pix_format *ppix = &mp->plane_fmt[0];
575 struct v4l2_pix_format *pix = &f->fmt.pix;
578 /* Converts to a mplane format */
580 /* Passes it to the generic mplane format function */
581 ret = func(file, priv, &fmt);
582 /* Copies back the mplane data to the single plane format */
583 pix->width = mp->width;
584 pix->height = mp->height;
585 pix->pixelformat = mp->pixelformat;
586 pix->field = mp->field;
587 pix->colorspace = mp->colorspace;
588 pix->ycbcr_enc = mp->ycbcr_enc;
589 pix->quantization = mp->quantization;
590 pix->sizeimage = ppix->sizeimage;
591 pix->bytesperline = ppix->bytesperline;
592 pix->flags = mp->flags;
596 /* v4l2_rect helper function: copy the width/height values */
597 void rect_set_size_to(struct v4l2_rect *r, const struct v4l2_rect *size)
599 r->width = size->width;
600 r->height = size->height;
603 /* v4l2_rect helper function: width and height of r should be >= min_size */
604 void rect_set_min_size(struct v4l2_rect *r, const struct v4l2_rect *min_size)
606 if (r->width < min_size->width)
607 r->width = min_size->width;
608 if (r->height < min_size->height)
609 r->height = min_size->height;
612 /* v4l2_rect helper function: width and height of r should be <= max_size */
613 void rect_set_max_size(struct v4l2_rect *r, const struct v4l2_rect *max_size)
615 if (r->width > max_size->width)
616 r->width = max_size->width;
617 if (r->height > max_size->height)
618 r->height = max_size->height;
621 /* v4l2_rect helper function: r should be inside boundary */
622 void rect_map_inside(struct v4l2_rect *r, const struct v4l2_rect *boundary)
624 rect_set_max_size(r, boundary);
625 if (r->left < boundary->left)
626 r->left = boundary->left;
627 if (r->top < boundary->top)
628 r->top = boundary->top;
629 if (r->left + r->width > boundary->width)
630 r->left = boundary->width - r->width;
631 if (r->top + r->height > boundary->height)
632 r->top = boundary->height - r->height;
635 /* v4l2_rect helper function: return true if r1 has the same size as r2 */
636 bool rect_same_size(const struct v4l2_rect *r1, const struct v4l2_rect *r2)
638 return r1->width == r2->width && r1->height == r2->height;
641 /* v4l2_rect helper function: calculate the intersection of two rects */
642 struct v4l2_rect rect_intersect(const struct v4l2_rect *a, const struct v4l2_rect *b)
647 r.top = max(a->top, b->top);
648 r.left = max(a->left, b->left);
649 bottom = min(a->top + a->height, b->top + b->height);
650 right = min(a->left + a->width, b->left + b->width);
651 r.height = max(0, bottom - r.top);
652 r.width = max(0, right - r.left);
657 * v4l2_rect helper function: scale rect r by to->width / from->width and
658 * to->height / from->height.
660 void rect_scale(struct v4l2_rect *r, const struct v4l2_rect *from,
661 const struct v4l2_rect *to)
663 if (from->width == 0 || from->height == 0) {
664 r->left = r->top = r->width = r->height = 0;
667 r->left = (((r->left - from->left) * to->width) / from->width) & ~1;
668 r->width = ((r->width * to->width) / from->width) & ~1;
669 r->top = ((r->top - from->top) * to->height) / from->height;
670 r->height = (r->height * to->height) / from->height;
673 bool rect_overlap(const struct v4l2_rect *r1, const struct v4l2_rect *r2)
676 * IF the left side of r1 is to the right of the right side of r2 OR
677 * the left side of r2 is to the right of the right side of r1 THEN
678 * they do not overlap.
680 if (r1->left >= r2->left + r2->width ||
681 r2->left >= r1->left + r1->width)
684 * IF the top side of r1 is below the bottom of r2 OR
685 * the top side of r2 is below the bottom of r1 THEN
686 * they do not overlap.
688 if (r1->top >= r2->top + r2->height ||
689 r2->top >= r1->top + r1->height)
693 int vivid_vid_adjust_sel(unsigned flags, struct v4l2_rect *r)
695 unsigned w = r->width;
696 unsigned h = r->height;
698 /* sanitize w and h in case someone passes ~0 as the value */
701 if (!(flags & V4L2_SEL_FLAG_LE)) {
709 if (!(flags & V4L2_SEL_FLAG_GE)) {
719 if (w > MAX_WIDTH || h > MAX_HEIGHT)
725 /* sanitize left and top in case someone passes ~0 as the value */
728 if (r->left + w > MAX_WIDTH)
729 r->left = MAX_WIDTH - w;
730 if (r->top + h > MAX_HEIGHT)
731 r->top = MAX_HEIGHT - h;
732 if ((flags & (V4L2_SEL_FLAG_GE | V4L2_SEL_FLAG_LE)) ==
733 (V4L2_SEL_FLAG_GE | V4L2_SEL_FLAG_LE) &&
734 (r->width != w || r->height != h))
741 int vivid_enum_fmt_vid(struct file *file, void *priv,
742 struct v4l2_fmtdesc *f)
744 struct vivid_dev *dev = video_drvdata(file);
745 const struct vivid_fmt *fmt;
747 if (f->index >= ARRAY_SIZE(vivid_formats) -
748 (dev->multiplanar ? 0 : VIVID_MPLANAR_FORMATS))
751 fmt = &vivid_formats[f->index];
753 strlcpy(f->description, fmt->name, sizeof(f->description));
754 f->pixelformat = fmt->fourcc;
758 int vidioc_enum_fmt_vid_mplane(struct file *file, void *priv,
759 struct v4l2_fmtdesc *f)
761 struct vivid_dev *dev = video_drvdata(file);
763 if (!dev->multiplanar)
765 return vivid_enum_fmt_vid(file, priv, f);
768 int vidioc_enum_fmt_vid(struct file *file, void *priv,
769 struct v4l2_fmtdesc *f)
771 struct vivid_dev *dev = video_drvdata(file);
773 if (dev->multiplanar)
775 return vivid_enum_fmt_vid(file, priv, f);
778 int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
780 struct vivid_dev *dev = video_drvdata(file);
781 struct video_device *vdev = video_devdata(file);
783 if (vdev->vfl_dir == VFL_DIR_RX) {
784 if (!vivid_is_sdtv_cap(dev))
788 if (!vivid_is_svid_out(dev))
795 int vidioc_g_dv_timings(struct file *file, void *_fh,
796 struct v4l2_dv_timings *timings)
798 struct vivid_dev *dev = video_drvdata(file);
799 struct video_device *vdev = video_devdata(file);
801 if (vdev->vfl_dir == VFL_DIR_RX) {
802 if (!vivid_is_hdmi_cap(dev))
804 *timings = dev->dv_timings_cap;
806 if (!vivid_is_hdmi_out(dev))
808 *timings = dev->dv_timings_out;
813 int vidioc_enum_dv_timings(struct file *file, void *_fh,
814 struct v4l2_enum_dv_timings *timings)
816 struct vivid_dev *dev = video_drvdata(file);
817 struct video_device *vdev = video_devdata(file);
819 if (vdev->vfl_dir == VFL_DIR_RX) {
820 if (!vivid_is_hdmi_cap(dev))
823 if (!vivid_is_hdmi_out(dev))
826 return v4l2_enum_dv_timings_cap(timings, &vivid_dv_timings_cap,
830 int vidioc_dv_timings_cap(struct file *file, void *_fh,
831 struct v4l2_dv_timings_cap *cap)
833 struct vivid_dev *dev = video_drvdata(file);
834 struct video_device *vdev = video_devdata(file);
836 if (vdev->vfl_dir == VFL_DIR_RX) {
837 if (!vivid_is_hdmi_cap(dev))
840 if (!vivid_is_hdmi_out(dev))
843 *cap = vivid_dv_timings_cap;
847 int vidioc_g_edid(struct file *file, void *_fh,
848 struct v4l2_edid *edid)
850 struct vivid_dev *dev = video_drvdata(file);
851 struct video_device *vdev = video_devdata(file);
853 memset(edid->reserved, 0, sizeof(edid->reserved));
854 if (vdev->vfl_dir == VFL_DIR_RX) {
855 if (edid->pad >= dev->num_inputs)
857 if (dev->input_type[edid->pad] != HDMI)
860 if (edid->pad >= dev->num_outputs)
862 if (dev->output_type[edid->pad] != HDMI)
865 if (edid->start_block == 0 && edid->blocks == 0) {
866 edid->blocks = dev->edid_blocks;
869 if (dev->edid_blocks == 0)
871 if (edid->start_block >= dev->edid_blocks)
873 if (edid->start_block + edid->blocks > dev->edid_blocks)
874 edid->blocks = dev->edid_blocks - edid->start_block;
875 memcpy(edid->edid, dev->edid, edid->blocks * 128);