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,
52 .data_offset = { PLANE0_DATA_OFFSET, 0 },
55 .name = "4:2:2, packed, UYVY",
56 .fourcc = V4L2_PIX_FMT_UYVY,
62 .name = "4:2:2, packed, YVYU",
63 .fourcc = V4L2_PIX_FMT_YVYU,
69 .name = "4:2:2, packed, VYUY",
70 .fourcc = V4L2_PIX_FMT_VYUY,
76 .name = "RGB565 (LE)",
77 .fourcc = V4L2_PIX_FMT_RGB565, /* gggbbbbb rrrrrggg */
80 .can_do_overlay = true,
83 .name = "RGB565 (BE)",
84 .fourcc = V4L2_PIX_FMT_RGB565X, /* rrrrrggg gggbbbbb */
87 .can_do_overlay = true,
90 .name = "RGB555 (LE)",
91 .fourcc = V4L2_PIX_FMT_RGB555, /* gggbbbbb arrrrrgg */
94 .can_do_overlay = true,
97 .name = "XRGB555 (LE)",
98 .fourcc = V4L2_PIX_FMT_XRGB555, /* gggbbbbb arrrrrgg */
101 .can_do_overlay = true,
104 .name = "ARGB555 (LE)",
105 .fourcc = V4L2_PIX_FMT_ARGB555, /* gggbbbbb arrrrrgg */
108 .can_do_overlay = true,
109 .alpha_mask = 0x8000,
112 .name = "RGB555 (BE)",
113 .fourcc = V4L2_PIX_FMT_RGB555X, /* arrrrrgg gggbbbbb */
116 .can_do_overlay = true,
119 .name = "RGB24 (LE)",
120 .fourcc = V4L2_PIX_FMT_RGB24, /* rgb */
125 .name = "RGB24 (BE)",
126 .fourcc = V4L2_PIX_FMT_BGR24, /* bgr */
131 .name = "RGB32 (LE)",
132 .fourcc = V4L2_PIX_FMT_RGB32, /* argb */
137 .name = "RGB32 (BE)",
138 .fourcc = V4L2_PIX_FMT_BGR32, /* bgra */
143 .name = "XRGB32 (LE)",
144 .fourcc = V4L2_PIX_FMT_XRGB32, /* argb */
149 .name = "XRGB32 (BE)",
150 .fourcc = V4L2_PIX_FMT_XBGR32, /* bgra */
155 .name = "ARGB32 (LE)",
156 .fourcc = V4L2_PIX_FMT_ARGB32, /* argb */
159 .alpha_mask = 0x000000ff,
162 .name = "ARGB32 (BE)",
163 .fourcc = V4L2_PIX_FMT_ABGR32, /* bgra */
166 .alpha_mask = 0xff000000,
169 .name = "4:2:2, planar, YUV",
170 .fourcc = V4L2_PIX_FMT_NV16M,
174 .data_offset = { PLANE0_DATA_OFFSET, 0 },
177 .name = "4:2:2, planar, YVU",
178 .fourcc = V4L2_PIX_FMT_NV61M,
182 .data_offset = { 0, PLANE0_DATA_OFFSET },
186 /* There are 2 multiplanar formats in the list */
187 #define VIVID_MPLANAR_FORMATS 2
189 const struct vivid_fmt *vivid_get_format(struct vivid_dev *dev, u32 pixelformat)
191 const struct vivid_fmt *fmt;
194 for (k = 0; k < ARRAY_SIZE(vivid_formats); k++) {
195 fmt = &vivid_formats[k];
196 if (fmt->fourcc == pixelformat)
197 if (fmt->planes == 1 || dev->multiplanar)
204 bool vivid_vid_can_loop(struct vivid_dev *dev)
206 if (dev->src_rect.width != dev->sink_rect.width ||
207 dev->src_rect.height != dev->sink_rect.height)
209 if (dev->fmt_cap->fourcc != dev->fmt_out->fourcc)
211 if (dev->field_cap != dev->field_out)
213 if (vivid_is_svid_cap(dev) && vivid_is_svid_out(dev)) {
214 if (!(dev->std_cap & V4L2_STD_525_60) !=
215 !(dev->std_out & V4L2_STD_525_60))
219 if (vivid_is_hdmi_cap(dev) && vivid_is_hdmi_out(dev))
224 void vivid_send_source_change(struct vivid_dev *dev, unsigned type)
226 struct v4l2_event ev = {
227 .type = V4L2_EVENT_SOURCE_CHANGE,
228 .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION,
232 for (i = 0; i < dev->num_inputs; i++) {
234 if (dev->input_type[i] == type) {
235 if (video_is_registered(&dev->vid_cap_dev) && dev->has_vid_cap)
236 v4l2_event_queue(&dev->vid_cap_dev, &ev);
237 if (video_is_registered(&dev->vbi_cap_dev) && dev->has_vbi_cap)
238 v4l2_event_queue(&dev->vbi_cap_dev, &ev);
244 * Conversion function that converts a single-planar format to a
245 * single-plane multiplanar format.
247 void fmt_sp2mp(const struct v4l2_format *sp_fmt, struct v4l2_format *mp_fmt)
249 struct v4l2_pix_format_mplane *mp = &mp_fmt->fmt.pix_mp;
250 struct v4l2_plane_pix_format *ppix = &mp->plane_fmt[0];
251 const struct v4l2_pix_format *pix = &sp_fmt->fmt.pix;
252 bool is_out = sp_fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT;
254 memset(mp->reserved, 0, sizeof(mp->reserved));
255 mp_fmt->type = is_out ? V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE :
256 V4L2_CAP_VIDEO_CAPTURE_MPLANE;
257 mp->width = pix->width;
258 mp->height = pix->height;
259 mp->pixelformat = pix->pixelformat;
260 mp->field = pix->field;
261 mp->colorspace = pix->colorspace;
262 mp->ycbcr_enc = pix->ycbcr_enc;
263 mp->quantization = pix->quantization;
265 mp->flags = pix->flags;
266 ppix->sizeimage = pix->sizeimage;
267 ppix->bytesperline = pix->bytesperline;
268 memset(ppix->reserved, 0, sizeof(ppix->reserved));
271 int fmt_sp2mp_func(struct file *file, void *priv,
272 struct v4l2_format *f, fmtfunc func)
274 struct v4l2_format fmt;
275 struct v4l2_pix_format_mplane *mp = &fmt.fmt.pix_mp;
276 struct v4l2_plane_pix_format *ppix = &mp->plane_fmt[0];
277 struct v4l2_pix_format *pix = &f->fmt.pix;
280 /* Converts to a mplane format */
282 /* Passes it to the generic mplane format function */
283 ret = func(file, priv, &fmt);
284 /* Copies back the mplane data to the single plane format */
285 pix->width = mp->width;
286 pix->height = mp->height;
287 pix->pixelformat = mp->pixelformat;
288 pix->field = mp->field;
289 pix->colorspace = mp->colorspace;
290 pix->ycbcr_enc = mp->ycbcr_enc;
291 pix->quantization = mp->quantization;
292 pix->sizeimage = ppix->sizeimage;
293 pix->bytesperline = ppix->bytesperline;
294 pix->flags = mp->flags;
298 /* v4l2_rect helper function: copy the width/height values */
299 void rect_set_size_to(struct v4l2_rect *r, const struct v4l2_rect *size)
301 r->width = size->width;
302 r->height = size->height;
305 /* v4l2_rect helper function: width and height of r should be >= min_size */
306 void rect_set_min_size(struct v4l2_rect *r, const struct v4l2_rect *min_size)
308 if (r->width < min_size->width)
309 r->width = min_size->width;
310 if (r->height < min_size->height)
311 r->height = min_size->height;
314 /* v4l2_rect helper function: width and height of r should be <= max_size */
315 void rect_set_max_size(struct v4l2_rect *r, const struct v4l2_rect *max_size)
317 if (r->width > max_size->width)
318 r->width = max_size->width;
319 if (r->height > max_size->height)
320 r->height = max_size->height;
323 /* v4l2_rect helper function: r should be inside boundary */
324 void rect_map_inside(struct v4l2_rect *r, const struct v4l2_rect *boundary)
326 rect_set_max_size(r, boundary);
327 if (r->left < boundary->left)
328 r->left = boundary->left;
329 if (r->top < boundary->top)
330 r->top = boundary->top;
331 if (r->left + r->width > boundary->width)
332 r->left = boundary->width - r->width;
333 if (r->top + r->height > boundary->height)
334 r->top = boundary->height - r->height;
337 /* v4l2_rect helper function: return true if r1 has the same size as r2 */
338 bool rect_same_size(const struct v4l2_rect *r1, const struct v4l2_rect *r2)
340 return r1->width == r2->width && r1->height == r2->height;
343 /* v4l2_rect helper function: calculate the intersection of two rects */
344 struct v4l2_rect rect_intersect(const struct v4l2_rect *a, const struct v4l2_rect *b)
349 r.top = max(a->top, b->top);
350 r.left = max(a->left, b->left);
351 bottom = min(a->top + a->height, b->top + b->height);
352 right = min(a->left + a->width, b->left + b->width);
353 r.height = max(0, bottom - r.top);
354 r.width = max(0, right - r.left);
359 * v4l2_rect helper function: scale rect r by to->width / from->width and
360 * to->height / from->height.
362 void rect_scale(struct v4l2_rect *r, const struct v4l2_rect *from,
363 const struct v4l2_rect *to)
365 if (from->width == 0 || from->height == 0) {
366 r->left = r->top = r->width = r->height = 0;
369 r->left = (((r->left - from->left) * to->width) / from->width) & ~1;
370 r->width = ((r->width * to->width) / from->width) & ~1;
371 r->top = ((r->top - from->top) * to->height) / from->height;
372 r->height = (r->height * to->height) / from->height;
375 bool rect_overlap(const struct v4l2_rect *r1, const struct v4l2_rect *r2)
378 * IF the left side of r1 is to the right of the right side of r2 OR
379 * the left side of r2 is to the right of the right side of r1 THEN
380 * they do not overlap.
382 if (r1->left >= r2->left + r2->width ||
383 r2->left >= r1->left + r1->width)
386 * IF the top side of r1 is below the bottom of r2 OR
387 * the top side of r2 is below the bottom of r1 THEN
388 * they do not overlap.
390 if (r1->top >= r2->top + r2->height ||
391 r2->top >= r1->top + r1->height)
395 int vivid_vid_adjust_sel(unsigned flags, struct v4l2_rect *r)
397 unsigned w = r->width;
398 unsigned h = r->height;
400 if (!(flags & V4L2_SEL_FLAG_LE)) {
408 if (!(flags & V4L2_SEL_FLAG_GE)) {
418 if (w > MAX_WIDTH || h > MAX_HEIGHT)
426 if (r->left + w > MAX_WIDTH)
427 r->left = MAX_WIDTH - w;
428 if (r->top + h > MAX_HEIGHT)
429 r->top = MAX_HEIGHT - h;
430 if ((flags & (V4L2_SEL_FLAG_GE | V4L2_SEL_FLAG_LE)) ==
431 (V4L2_SEL_FLAG_GE | V4L2_SEL_FLAG_LE) &&
432 (r->width != w || r->height != h))
439 int vivid_enum_fmt_vid(struct file *file, void *priv,
440 struct v4l2_fmtdesc *f)
442 struct vivid_dev *dev = video_drvdata(file);
443 const struct vivid_fmt *fmt;
445 if (f->index >= ARRAY_SIZE(vivid_formats) -
446 (dev->multiplanar ? 0 : VIVID_MPLANAR_FORMATS))
449 fmt = &vivid_formats[f->index];
451 strlcpy(f->description, fmt->name, sizeof(f->description));
452 f->pixelformat = fmt->fourcc;
456 int vidioc_enum_fmt_vid_mplane(struct file *file, void *priv,
457 struct v4l2_fmtdesc *f)
459 struct vivid_dev *dev = video_drvdata(file);
461 if (!dev->multiplanar)
463 return vivid_enum_fmt_vid(file, priv, f);
466 int vidioc_enum_fmt_vid(struct file *file, void *priv,
467 struct v4l2_fmtdesc *f)
469 struct vivid_dev *dev = video_drvdata(file);
471 if (dev->multiplanar)
473 return vivid_enum_fmt_vid(file, priv, f);
476 int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
478 struct vivid_dev *dev = video_drvdata(file);
479 struct video_device *vdev = video_devdata(file);
481 if (vdev->vfl_dir == VFL_DIR_RX) {
482 if (!vivid_is_sdtv_cap(dev))
486 if (!vivid_is_svid_out(dev))
493 int vidioc_g_dv_timings(struct file *file, void *_fh,
494 struct v4l2_dv_timings *timings)
496 struct vivid_dev *dev = video_drvdata(file);
497 struct video_device *vdev = video_devdata(file);
499 if (vdev->vfl_dir == VFL_DIR_RX) {
500 if (!vivid_is_hdmi_cap(dev))
502 *timings = dev->dv_timings_cap;
504 if (!vivid_is_hdmi_out(dev))
506 *timings = dev->dv_timings_out;
511 int vidioc_enum_dv_timings(struct file *file, void *_fh,
512 struct v4l2_enum_dv_timings *timings)
514 struct vivid_dev *dev = video_drvdata(file);
515 struct video_device *vdev = video_devdata(file);
517 if (vdev->vfl_dir == VFL_DIR_RX) {
518 if (!vivid_is_hdmi_cap(dev))
521 if (!vivid_is_hdmi_out(dev))
524 return v4l2_enum_dv_timings_cap(timings, &vivid_dv_timings_cap,
528 int vidioc_dv_timings_cap(struct file *file, void *_fh,
529 struct v4l2_dv_timings_cap *cap)
531 struct vivid_dev *dev = video_drvdata(file);
532 struct video_device *vdev = video_devdata(file);
534 if (vdev->vfl_dir == VFL_DIR_RX) {
535 if (!vivid_is_hdmi_cap(dev))
538 if (!vivid_is_hdmi_out(dev))
541 *cap = vivid_dv_timings_cap;
545 int vidioc_g_edid(struct file *file, void *_fh,
546 struct v4l2_edid *edid)
548 struct vivid_dev *dev = video_drvdata(file);
549 struct video_device *vdev = video_devdata(file);
551 memset(edid->reserved, 0, sizeof(edid->reserved));
552 if (vdev->vfl_dir == VFL_DIR_RX) {
553 if (edid->pad >= dev->num_inputs)
555 if (dev->input_type[edid->pad] != HDMI)
558 if (edid->pad >= dev->num_outputs)
560 if (dev->output_type[edid->pad] != HDMI)
563 if (edid->start_block == 0 && edid->blocks == 0) {
564 edid->blocks = dev->edid_blocks;
567 if (dev->edid_blocks == 0)
569 if (edid->start_block >= dev->edid_blocks)
571 if (edid->start_block + edid->blocks > dev->edid_blocks)
572 edid->blocks = dev->edid_blocks - edid->start_block;
573 memcpy(edid->edid, dev->edid, edid->blocks * 128);