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 .fourcc = V4L2_PIX_FMT_YUYV,
49 .vdownsampling = { 1 },
54 .data_offset = { PLANE0_DATA_OFFSET },
57 .fourcc = V4L2_PIX_FMT_UYVY,
58 .vdownsampling = { 1 },
65 .fourcc = V4L2_PIX_FMT_YVYU,
66 .vdownsampling = { 1 },
73 .fourcc = V4L2_PIX_FMT_VYUY,
74 .vdownsampling = { 1 },
81 .fourcc = V4L2_PIX_FMT_YUV422P,
82 .vdownsampling = { 1, 1, 1 },
83 .bit_depth = { 8, 4, 4 },
89 .fourcc = V4L2_PIX_FMT_YUV420,
90 .vdownsampling = { 1, 2, 2 },
91 .bit_depth = { 8, 4, 4 },
97 .fourcc = V4L2_PIX_FMT_YVU420,
98 .vdownsampling = { 1, 2, 2 },
99 .bit_depth = { 8, 4, 4 },
105 .fourcc = V4L2_PIX_FMT_NV12,
106 .vdownsampling = { 1, 2 },
107 .bit_depth = { 8, 8 },
113 .fourcc = V4L2_PIX_FMT_NV21,
114 .vdownsampling = { 1, 2 },
115 .bit_depth = { 8, 8 },
121 .fourcc = V4L2_PIX_FMT_NV16,
122 .vdownsampling = { 1, 1 },
123 .bit_depth = { 8, 8 },
129 .fourcc = V4L2_PIX_FMT_NV61,
130 .vdownsampling = { 1, 1 },
131 .bit_depth = { 8, 8 },
137 .fourcc = V4L2_PIX_FMT_NV24,
138 .vdownsampling = { 1, 1 },
139 .bit_depth = { 8, 16 },
145 .fourcc = V4L2_PIX_FMT_NV42,
146 .vdownsampling = { 1, 1 },
147 .bit_depth = { 8, 16 },
153 .fourcc = V4L2_PIX_FMT_YUV555, /* uuuvvvvv ayyyyyuu */
154 .vdownsampling = { 1 },
158 .alpha_mask = 0x8000,
161 .fourcc = V4L2_PIX_FMT_YUV565, /* uuuvvvvv yyyyyuuu */
162 .vdownsampling = { 1 },
168 .fourcc = V4L2_PIX_FMT_YUV444, /* uuuuvvvv aaaayyyy */
169 .vdownsampling = { 1 },
173 .alpha_mask = 0xf000,
176 .fourcc = V4L2_PIX_FMT_YUV32, /* ayuv */
177 .vdownsampling = { 1 },
181 .alpha_mask = 0x000000ff,
184 .fourcc = V4L2_PIX_FMT_GREY,
185 .vdownsampling = { 1 },
192 .fourcc = V4L2_PIX_FMT_Y16,
193 .vdownsampling = { 1 },
200 .fourcc = V4L2_PIX_FMT_Y16_BE,
201 .vdownsampling = { 1 },
208 .fourcc = V4L2_PIX_FMT_RGB332, /* rrrgggbb */
209 .vdownsampling = { 1 },
215 .fourcc = V4L2_PIX_FMT_RGB565, /* gggbbbbb rrrrrggg */
216 .vdownsampling = { 1 },
220 .can_do_overlay = true,
223 .fourcc = V4L2_PIX_FMT_RGB565X, /* rrrrrggg gggbbbbb */
224 .vdownsampling = { 1 },
228 .can_do_overlay = true,
231 .fourcc = V4L2_PIX_FMT_RGB444, /* xxxxrrrr ggggbbbb */
232 .vdownsampling = { 1 },
238 .fourcc = V4L2_PIX_FMT_XRGB444, /* xxxxrrrr ggggbbbb */
239 .vdownsampling = { 1 },
245 .fourcc = V4L2_PIX_FMT_ARGB444, /* aaaarrrr ggggbbbb */
246 .vdownsampling = { 1 },
250 .alpha_mask = 0x00f0,
253 .fourcc = V4L2_PIX_FMT_RGB555, /* gggbbbbb xrrrrrgg */
254 .vdownsampling = { 1 },
258 .can_do_overlay = true,
261 .fourcc = V4L2_PIX_FMT_XRGB555, /* gggbbbbb xrrrrrgg */
262 .vdownsampling = { 1 },
266 .can_do_overlay = true,
269 .fourcc = V4L2_PIX_FMT_ARGB555, /* gggbbbbb arrrrrgg */
270 .vdownsampling = { 1 },
274 .can_do_overlay = true,
275 .alpha_mask = 0x8000,
278 .fourcc = V4L2_PIX_FMT_RGB555X, /* xrrrrrgg gggbbbbb */
279 .vdownsampling = { 1 },
285 .fourcc = V4L2_PIX_FMT_XRGB555X, /* xrrrrrgg gggbbbbb */
286 .vdownsampling = { 1 },
292 .fourcc = V4L2_PIX_FMT_ARGB555X, /* arrrrrgg gggbbbbb */
293 .vdownsampling = { 1 },
297 .alpha_mask = 0x0080,
300 .fourcc = V4L2_PIX_FMT_RGB24, /* rgb */
301 .vdownsampling = { 1 },
307 .fourcc = V4L2_PIX_FMT_BGR24, /* bgr */
308 .vdownsampling = { 1 },
314 .fourcc = V4L2_PIX_FMT_BGR666, /* bbbbbbgg ggggrrrr rrxxxxxx */
315 .vdownsampling = { 1 },
321 .fourcc = V4L2_PIX_FMT_RGB32, /* xrgb */
322 .vdownsampling = { 1 },
328 .fourcc = V4L2_PIX_FMT_BGR32, /* bgrx */
329 .vdownsampling = { 1 },
335 .fourcc = V4L2_PIX_FMT_XRGB32, /* xrgb */
336 .vdownsampling = { 1 },
342 .fourcc = V4L2_PIX_FMT_XBGR32, /* bgrx */
343 .vdownsampling = { 1 },
349 .fourcc = V4L2_PIX_FMT_ARGB32, /* argb */
350 .vdownsampling = { 1 },
354 .alpha_mask = 0x000000ff,
357 .fourcc = V4L2_PIX_FMT_ABGR32, /* bgra */
358 .vdownsampling = { 1 },
362 .alpha_mask = 0xff000000,
365 .fourcc = V4L2_PIX_FMT_SBGGR8, /* Bayer BG/GR */
366 .vdownsampling = { 1 },
372 .fourcc = V4L2_PIX_FMT_SGBRG8, /* Bayer GB/RG */
373 .vdownsampling = { 1 },
379 .fourcc = V4L2_PIX_FMT_SGRBG8, /* Bayer GR/BG */
380 .vdownsampling = { 1 },
386 .fourcc = V4L2_PIX_FMT_SRGGB8, /* Bayer RG/GB */
387 .vdownsampling = { 1 },
393 .fourcc = V4L2_PIX_FMT_SBGGR10, /* Bayer BG/GR */
394 .vdownsampling = { 1 },
400 .fourcc = V4L2_PIX_FMT_SGBRG10, /* Bayer GB/RG */
401 .vdownsampling = { 1 },
407 .fourcc = V4L2_PIX_FMT_SGRBG10, /* Bayer GR/BG */
408 .vdownsampling = { 1 },
414 .fourcc = V4L2_PIX_FMT_SRGGB10, /* Bayer RG/GB */
415 .vdownsampling = { 1 },
421 .fourcc = V4L2_PIX_FMT_SBGGR12, /* Bayer BG/GR */
422 .vdownsampling = { 1 },
428 .fourcc = V4L2_PIX_FMT_SGBRG12, /* Bayer GB/RG */
429 .vdownsampling = { 1 },
435 .fourcc = V4L2_PIX_FMT_SGRBG12, /* Bayer GR/BG */
436 .vdownsampling = { 1 },
442 .fourcc = V4L2_PIX_FMT_SRGGB12, /* Bayer RG/GB */
443 .vdownsampling = { 1 },
449 .fourcc = V4L2_PIX_FMT_NV16M,
450 .vdownsampling = { 1, 1 },
451 .bit_depth = { 8, 8 },
455 .data_offset = { PLANE0_DATA_OFFSET, 0 },
458 .fourcc = V4L2_PIX_FMT_NV61M,
459 .vdownsampling = { 1, 1 },
460 .bit_depth = { 8, 8 },
464 .data_offset = { 0, PLANE0_DATA_OFFSET },
467 .fourcc = V4L2_PIX_FMT_YUV420M,
468 .vdownsampling = { 1, 2, 2 },
469 .bit_depth = { 8, 4, 4 },
475 .fourcc = V4L2_PIX_FMT_YVU420M,
476 .vdownsampling = { 1, 2, 2 },
477 .bit_depth = { 8, 4, 4 },
483 .fourcc = V4L2_PIX_FMT_NV12M,
484 .vdownsampling = { 1, 2 },
485 .bit_depth = { 8, 8 },
491 .fourcc = V4L2_PIX_FMT_NV21M,
492 .vdownsampling = { 1, 2 },
493 .bit_depth = { 8, 8 },
500 /* There are 6 multiplanar formats in the list */
501 #define VIVID_MPLANAR_FORMATS 6
503 const struct vivid_fmt *vivid_get_format(struct vivid_dev *dev, u32 pixelformat)
505 const struct vivid_fmt *fmt;
508 for (k = 0; k < ARRAY_SIZE(vivid_formats); k++) {
509 fmt = &vivid_formats[k];
510 if (fmt->fourcc == pixelformat)
511 if (fmt->buffers == 1 || dev->multiplanar)
518 bool vivid_vid_can_loop(struct vivid_dev *dev)
520 if (dev->src_rect.width != dev->sink_rect.width ||
521 dev->src_rect.height != dev->sink_rect.height)
523 if (dev->fmt_cap->fourcc != dev->fmt_out->fourcc)
525 if (dev->field_cap != dev->field_out)
528 * While this can be supported, it is just too much work
529 * to actually implement.
531 if (dev->field_cap == V4L2_FIELD_SEQ_TB ||
532 dev->field_cap == V4L2_FIELD_SEQ_BT)
534 if (vivid_is_svid_cap(dev) && vivid_is_svid_out(dev)) {
535 if (!(dev->std_cap & V4L2_STD_525_60) !=
536 !(dev->std_out & V4L2_STD_525_60))
540 if (vivid_is_hdmi_cap(dev) && vivid_is_hdmi_out(dev))
545 void vivid_send_source_change(struct vivid_dev *dev, unsigned type)
547 struct v4l2_event ev = {
548 .type = V4L2_EVENT_SOURCE_CHANGE,
549 .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION,
553 for (i = 0; i < dev->num_inputs; i++) {
555 if (dev->input_type[i] == type) {
556 if (video_is_registered(&dev->vid_cap_dev) && dev->has_vid_cap)
557 v4l2_event_queue(&dev->vid_cap_dev, &ev);
558 if (video_is_registered(&dev->vbi_cap_dev) && dev->has_vbi_cap)
559 v4l2_event_queue(&dev->vbi_cap_dev, &ev);
565 * Conversion function that converts a single-planar format to a
566 * single-plane multiplanar format.
568 void fmt_sp2mp(const struct v4l2_format *sp_fmt, struct v4l2_format *mp_fmt)
570 struct v4l2_pix_format_mplane *mp = &mp_fmt->fmt.pix_mp;
571 struct v4l2_plane_pix_format *ppix = &mp->plane_fmt[0];
572 const struct v4l2_pix_format *pix = &sp_fmt->fmt.pix;
573 bool is_out = sp_fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT;
575 memset(mp->reserved, 0, sizeof(mp->reserved));
576 mp_fmt->type = is_out ? V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE :
577 V4L2_CAP_VIDEO_CAPTURE_MPLANE;
578 mp->width = pix->width;
579 mp->height = pix->height;
580 mp->pixelformat = pix->pixelformat;
581 mp->field = pix->field;
582 mp->colorspace = pix->colorspace;
583 mp->xfer_func = pix->xfer_func;
584 mp->ycbcr_enc = pix->ycbcr_enc;
585 mp->quantization = pix->quantization;
587 mp->flags = pix->flags;
588 ppix->sizeimage = pix->sizeimage;
589 ppix->bytesperline = pix->bytesperline;
590 memset(ppix->reserved, 0, sizeof(ppix->reserved));
593 int fmt_sp2mp_func(struct file *file, void *priv,
594 struct v4l2_format *f, fmtfunc func)
596 struct v4l2_format fmt;
597 struct v4l2_pix_format_mplane *mp = &fmt.fmt.pix_mp;
598 struct v4l2_plane_pix_format *ppix = &mp->plane_fmt[0];
599 struct v4l2_pix_format *pix = &f->fmt.pix;
602 /* Converts to a mplane format */
604 /* Passes it to the generic mplane format function */
605 ret = func(file, priv, &fmt);
606 /* Copies back the mplane data to the single plane format */
607 pix->width = mp->width;
608 pix->height = mp->height;
609 pix->pixelformat = mp->pixelformat;
610 pix->field = mp->field;
611 pix->colorspace = mp->colorspace;
612 pix->xfer_func = mp->xfer_func;
613 pix->ycbcr_enc = mp->ycbcr_enc;
614 pix->quantization = mp->quantization;
615 pix->sizeimage = ppix->sizeimage;
616 pix->bytesperline = ppix->bytesperline;
617 pix->flags = mp->flags;
621 /* v4l2_rect helper function: copy the width/height values */
622 void rect_set_size_to(struct v4l2_rect *r, const struct v4l2_rect *size)
624 r->width = size->width;
625 r->height = size->height;
628 /* v4l2_rect helper function: width and height of r should be >= min_size */
629 void rect_set_min_size(struct v4l2_rect *r, const struct v4l2_rect *min_size)
631 if (r->width < min_size->width)
632 r->width = min_size->width;
633 if (r->height < min_size->height)
634 r->height = min_size->height;
637 /* v4l2_rect helper function: width and height of r should be <= max_size */
638 void rect_set_max_size(struct v4l2_rect *r, const struct v4l2_rect *max_size)
640 if (r->width > max_size->width)
641 r->width = max_size->width;
642 if (r->height > max_size->height)
643 r->height = max_size->height;
646 /* v4l2_rect helper function: r should be inside boundary */
647 void rect_map_inside(struct v4l2_rect *r, const struct v4l2_rect *boundary)
649 rect_set_max_size(r, boundary);
650 if (r->left < boundary->left)
651 r->left = boundary->left;
652 if (r->top < boundary->top)
653 r->top = boundary->top;
654 if (r->left + r->width > boundary->width)
655 r->left = boundary->width - r->width;
656 if (r->top + r->height > boundary->height)
657 r->top = boundary->height - r->height;
660 /* v4l2_rect helper function: return true if r1 has the same size as r2 */
661 bool rect_same_size(const struct v4l2_rect *r1, const struct v4l2_rect *r2)
663 return r1->width == r2->width && r1->height == r2->height;
666 /* v4l2_rect helper function: calculate the intersection of two rects */
667 struct v4l2_rect rect_intersect(const struct v4l2_rect *a, const struct v4l2_rect *b)
672 r.top = max(a->top, b->top);
673 r.left = max(a->left, b->left);
674 bottom = min(a->top + a->height, b->top + b->height);
675 right = min(a->left + a->width, b->left + b->width);
676 r.height = max(0, bottom - r.top);
677 r.width = max(0, right - r.left);
682 * v4l2_rect helper function: scale rect r by to->width / from->width and
683 * to->height / from->height.
685 void rect_scale(struct v4l2_rect *r, const struct v4l2_rect *from,
686 const struct v4l2_rect *to)
688 if (from->width == 0 || from->height == 0) {
689 r->left = r->top = r->width = r->height = 0;
692 r->left = (((r->left - from->left) * to->width) / from->width) & ~1;
693 r->width = ((r->width * to->width) / from->width) & ~1;
694 r->top = ((r->top - from->top) * to->height) / from->height;
695 r->height = (r->height * to->height) / from->height;
698 bool rect_overlap(const struct v4l2_rect *r1, const struct v4l2_rect *r2)
701 * IF the left side of r1 is to the right of the right side of r2 OR
702 * the left side of r2 is to the right of the right side of r1 THEN
703 * they do not overlap.
705 if (r1->left >= r2->left + r2->width ||
706 r2->left >= r1->left + r1->width)
709 * IF the top side of r1 is below the bottom of r2 OR
710 * the top side of r2 is below the bottom of r1 THEN
711 * they do not overlap.
713 if (r1->top >= r2->top + r2->height ||
714 r2->top >= r1->top + r1->height)
718 int vivid_vid_adjust_sel(unsigned flags, struct v4l2_rect *r)
720 unsigned w = r->width;
721 unsigned h = r->height;
723 /* sanitize w and h in case someone passes ~0 as the value */
726 if (!(flags & V4L2_SEL_FLAG_LE)) {
734 if (!(flags & V4L2_SEL_FLAG_GE)) {
744 if (w > MAX_WIDTH || h > MAX_HEIGHT)
750 /* sanitize left and top in case someone passes ~0 as the value */
753 if (r->left + w > MAX_WIDTH)
754 r->left = MAX_WIDTH - w;
755 if (r->top + h > MAX_HEIGHT)
756 r->top = MAX_HEIGHT - h;
757 if ((flags & (V4L2_SEL_FLAG_GE | V4L2_SEL_FLAG_LE)) ==
758 (V4L2_SEL_FLAG_GE | V4L2_SEL_FLAG_LE) &&
759 (r->width != w || r->height != h))
766 int vivid_enum_fmt_vid(struct file *file, void *priv,
767 struct v4l2_fmtdesc *f)
769 struct vivid_dev *dev = video_drvdata(file);
770 const struct vivid_fmt *fmt;
772 if (f->index >= ARRAY_SIZE(vivid_formats) -
773 (dev->multiplanar ? 0 : VIVID_MPLANAR_FORMATS))
776 fmt = &vivid_formats[f->index];
778 f->pixelformat = fmt->fourcc;
782 int vidioc_enum_fmt_vid_mplane(struct file *file, void *priv,
783 struct v4l2_fmtdesc *f)
785 struct vivid_dev *dev = video_drvdata(file);
787 if (!dev->multiplanar)
789 return vivid_enum_fmt_vid(file, priv, f);
792 int vidioc_enum_fmt_vid(struct file *file, void *priv,
793 struct v4l2_fmtdesc *f)
795 struct vivid_dev *dev = video_drvdata(file);
797 if (dev->multiplanar)
799 return vivid_enum_fmt_vid(file, priv, f);
802 int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
804 struct vivid_dev *dev = video_drvdata(file);
805 struct video_device *vdev = video_devdata(file);
807 if (vdev->vfl_dir == VFL_DIR_RX) {
808 if (!vivid_is_sdtv_cap(dev))
812 if (!vivid_is_svid_out(dev))
819 int vidioc_g_dv_timings(struct file *file, void *_fh,
820 struct v4l2_dv_timings *timings)
822 struct vivid_dev *dev = video_drvdata(file);
823 struct video_device *vdev = video_devdata(file);
825 if (vdev->vfl_dir == VFL_DIR_RX) {
826 if (!vivid_is_hdmi_cap(dev))
828 *timings = dev->dv_timings_cap;
830 if (!vivid_is_hdmi_out(dev))
832 *timings = dev->dv_timings_out;
837 int vidioc_enum_dv_timings(struct file *file, void *_fh,
838 struct v4l2_enum_dv_timings *timings)
840 struct vivid_dev *dev = video_drvdata(file);
841 struct video_device *vdev = video_devdata(file);
843 if (vdev->vfl_dir == VFL_DIR_RX) {
844 if (!vivid_is_hdmi_cap(dev))
847 if (!vivid_is_hdmi_out(dev))
850 return v4l2_enum_dv_timings_cap(timings, &vivid_dv_timings_cap,
854 int vidioc_dv_timings_cap(struct file *file, void *_fh,
855 struct v4l2_dv_timings_cap *cap)
857 struct vivid_dev *dev = video_drvdata(file);
858 struct video_device *vdev = video_devdata(file);
860 if (vdev->vfl_dir == VFL_DIR_RX) {
861 if (!vivid_is_hdmi_cap(dev))
864 if (!vivid_is_hdmi_out(dev))
867 *cap = vivid_dv_timings_cap;
871 int vidioc_g_edid(struct file *file, void *_fh,
872 struct v4l2_edid *edid)
874 struct vivid_dev *dev = video_drvdata(file);
875 struct video_device *vdev = video_devdata(file);
877 memset(edid->reserved, 0, sizeof(edid->reserved));
878 if (vdev->vfl_dir == VFL_DIR_RX) {
879 if (edid->pad >= dev->num_inputs)
881 if (dev->input_type[edid->pad] != HDMI)
884 if (edid->pad >= dev->num_outputs)
886 if (dev->output_type[edid->pad] != HDMI)
889 if (edid->start_block == 0 && edid->blocks == 0) {
890 edid->blocks = dev->edid_blocks;
893 if (dev->edid_blocks == 0)
895 if (edid->start_block >= dev->edid_blocks)
897 if (edid->start_block + edid->blocks > dev->edid_blocks)
898 edid->blocks = dev->edid_blocks - edid->start_block;
899 memcpy(edid->edid, dev->edid, edid->blocks * 128);