2 * vivid-vid-cap.c - video capture 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/vmalloc.h>
24 #include <linux/videodev2.h>
25 #include <linux/v4l2-dv-timings.h>
26 #include <media/v4l2-common.h>
27 #include <media/v4l2-event.h>
28 #include <media/v4l2-dv-timings.h>
29 #include <media/v4l2-rect.h>
31 #include "vivid-core.h"
32 #include "vivid-vid-common.h"
33 #include "vivid-kthread-cap.h"
34 #include "vivid-vid-cap.h"
36 /* timeperframe: min/max and default */
37 static const struct v4l2_fract
38 tpf_min = {.numerator = 1, .denominator = FPS_MAX},
39 tpf_max = {.numerator = FPS_MAX, .denominator = 1},
40 tpf_default = {.numerator = 1, .denominator = 30};
42 static const struct vivid_fmt formats_ovl[] = {
44 .fourcc = V4L2_PIX_FMT_RGB565, /* gggbbbbb rrrrrggg */
45 .vdownsampling = { 1 },
51 .fourcc = V4L2_PIX_FMT_XRGB555, /* gggbbbbb arrrrrgg */
52 .vdownsampling = { 1 },
58 .fourcc = V4L2_PIX_FMT_ARGB555, /* gggbbbbb arrrrrgg */
59 .vdownsampling = { 1 },
66 /* The number of discrete webcam framesizes */
67 #define VIVID_WEBCAM_SIZES 4
68 /* The number of discrete webcam frameintervals */
69 #define VIVID_WEBCAM_IVALS (VIVID_WEBCAM_SIZES * 2)
71 /* Sizes must be in increasing order */
72 static const struct v4l2_frmsize_discrete webcam_sizes[VIVID_WEBCAM_SIZES] = {
80 * Intervals must be in increasing order and there must be twice as many
81 * elements in this array as there are in webcam_sizes.
83 static const struct v4l2_fract webcam_intervals[VIVID_WEBCAM_IVALS] = {
94 static const struct v4l2_discrete_probe webcam_probe = {
99 static int vid_cap_queue_setup(struct vb2_queue *vq,
100 unsigned *nbuffers, unsigned *nplanes,
101 unsigned sizes[], void *alloc_ctxs[])
103 struct vivid_dev *dev = vb2_get_drv_priv(vq);
104 unsigned buffers = tpg_g_buffers(&dev->tpg);
105 unsigned h = dev->fmt_cap_rect.height;
108 if (dev->field_cap == V4L2_FIELD_ALTERNATE) {
110 * You cannot use read() with FIELD_ALTERNATE since the field
111 * information (TOP/BOTTOM) cannot be passed back to the user.
113 if (vb2_fileio_is_active(vq))
117 if (dev->queue_setup_error) {
119 * Error injection: test what happens if queue_setup() returns
122 dev->queue_setup_error = false;
127 * Check if the number of requested planes match
128 * the number of buffers in the current format. You can't mix that.
130 if (*nplanes != buffers)
132 for (p = 0; p < buffers; p++) {
133 if (sizes[p] < tpg_g_line_width(&dev->tpg, p) * h +
134 dev->fmt_cap->data_offset[p])
138 for (p = 0; p < buffers; p++)
139 sizes[p] = tpg_g_line_width(&dev->tpg, p) * h +
140 dev->fmt_cap->data_offset[p];
143 if (vq->num_buffers + *nbuffers < 2)
144 *nbuffers = 2 - vq->num_buffers;
149 * videobuf2-vmalloc allocator is context-less so no need to set
153 dprintk(dev, 1, "%s: count=%d\n", __func__, *nbuffers);
154 for (p = 0; p < buffers; p++)
155 dprintk(dev, 1, "%s: size[%u]=%u\n", __func__, p, sizes[p]);
160 static int vid_cap_buf_prepare(struct vb2_buffer *vb)
162 struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
164 unsigned buffers = tpg_g_buffers(&dev->tpg);
167 dprintk(dev, 1, "%s\n", __func__);
169 if (WARN_ON(NULL == dev->fmt_cap))
172 if (dev->buf_prepare_error) {
174 * Error injection: test what happens if buf_prepare() returns
177 dev->buf_prepare_error = false;
180 for (p = 0; p < buffers; p++) {
181 size = tpg_g_line_width(&dev->tpg, p) * dev->fmt_cap_rect.height +
182 dev->fmt_cap->data_offset[p];
184 if (vb2_plane_size(vb, p) < size) {
185 dprintk(dev, 1, "%s data will not fit into plane %u (%lu < %lu)\n",
186 __func__, p, vb2_plane_size(vb, p), size);
190 vb2_set_plane_payload(vb, p, size);
191 vb->planes[p].data_offset = dev->fmt_cap->data_offset[p];
197 static void vid_cap_buf_finish(struct vb2_buffer *vb)
199 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
200 struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
201 struct v4l2_timecode *tc = &vbuf->timecode;
203 unsigned seq = vbuf->sequence;
205 if (!vivid_is_sdtv_cap(dev))
209 * Set the timecode. Rarely used, so it is interesting to
212 vbuf->flags |= V4L2_BUF_FLAG_TIMECODE;
213 if (dev->std_cap & V4L2_STD_525_60)
215 tc->type = (fps == 30) ? V4L2_TC_TYPE_30FPS : V4L2_TC_TYPE_25FPS;
217 tc->frames = seq % fps;
218 tc->seconds = (seq / fps) % 60;
219 tc->minutes = (seq / (60 * fps)) % 60;
220 tc->hours = (seq / (60 * 60 * fps)) % 24;
223 static void vid_cap_buf_queue(struct vb2_buffer *vb)
225 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
226 struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
227 struct vivid_buffer *buf = container_of(vbuf, struct vivid_buffer, vb);
229 dprintk(dev, 1, "%s\n", __func__);
231 spin_lock(&dev->slock);
232 list_add_tail(&buf->list, &dev->vid_cap_active);
233 spin_unlock(&dev->slock);
236 static int vid_cap_start_streaming(struct vb2_queue *vq, unsigned count)
238 struct vivid_dev *dev = vb2_get_drv_priv(vq);
242 if (vb2_is_streaming(&dev->vb_vid_out_q))
243 dev->can_loop_video = vivid_vid_can_loop(dev);
245 if (dev->kthread_vid_cap)
248 dev->vid_cap_seq_count = 0;
249 dprintk(dev, 1, "%s\n", __func__);
250 for (i = 0; i < VIDEO_MAX_FRAME; i++)
251 dev->must_blank[i] = tpg_g_perc_fill(&dev->tpg) < 100;
252 if (dev->start_streaming_error) {
253 dev->start_streaming_error = false;
256 err = vivid_start_generating_vid_cap(dev, &dev->vid_cap_streaming);
259 struct vivid_buffer *buf, *tmp;
261 list_for_each_entry_safe(buf, tmp, &dev->vid_cap_active, list) {
262 list_del(&buf->list);
263 vb2_buffer_done(&buf->vb.vb2_buf,
264 VB2_BUF_STATE_QUEUED);
270 /* abort streaming and wait for last buffer */
271 static void vid_cap_stop_streaming(struct vb2_queue *vq)
273 struct vivid_dev *dev = vb2_get_drv_priv(vq);
275 dprintk(dev, 1, "%s\n", __func__);
276 vivid_stop_generating_vid_cap(dev, &dev->vid_cap_streaming);
277 dev->can_loop_video = false;
280 const struct vb2_ops vivid_vid_cap_qops = {
281 .queue_setup = vid_cap_queue_setup,
282 .buf_prepare = vid_cap_buf_prepare,
283 .buf_finish = vid_cap_buf_finish,
284 .buf_queue = vid_cap_buf_queue,
285 .start_streaming = vid_cap_start_streaming,
286 .stop_streaming = vid_cap_stop_streaming,
287 .wait_prepare = vb2_ops_wait_prepare,
288 .wait_finish = vb2_ops_wait_finish,
292 * Determine the 'picture' quality based on the current TV frequency: either
293 * COLOR for a good 'signal', GRAY (grayscale picture) for a slightly off
294 * signal or NOISE for no signal.
296 void vivid_update_quality(struct vivid_dev *dev)
298 unsigned freq_modulus;
300 if (dev->loop_video && (vivid_is_svid_cap(dev) || vivid_is_hdmi_cap(dev))) {
302 * The 'noise' will only be replaced by the actual video
303 * if the output video matches the input video settings.
305 tpg_s_quality(&dev->tpg, TPG_QUAL_NOISE, 0);
308 if (vivid_is_hdmi_cap(dev) && VIVID_INVALID_SIGNAL(dev->dv_timings_signal_mode)) {
309 tpg_s_quality(&dev->tpg, TPG_QUAL_NOISE, 0);
312 if (vivid_is_sdtv_cap(dev) && VIVID_INVALID_SIGNAL(dev->std_signal_mode)) {
313 tpg_s_quality(&dev->tpg, TPG_QUAL_NOISE, 0);
316 if (!vivid_is_tv_cap(dev)) {
317 tpg_s_quality(&dev->tpg, TPG_QUAL_COLOR, 0);
322 * There is a fake channel every 6 MHz at 49.25, 55.25, etc.
323 * From +/- 0.25 MHz around the channel there is color, and from
324 * +/- 1 MHz there is grayscale (chroma is lost).
325 * Everywhere else it is just noise.
327 freq_modulus = (dev->tv_freq - 676 /* (43.25-1) * 16 */) % (6 * 16);
328 if (freq_modulus > 2 * 16) {
329 tpg_s_quality(&dev->tpg, TPG_QUAL_NOISE,
330 next_pseudo_random32(dev->tv_freq ^ 0x55) & 0x3f);
333 if (freq_modulus < 12 /*0.75 * 16*/ || freq_modulus > 20 /*1.25 * 16*/)
334 tpg_s_quality(&dev->tpg, TPG_QUAL_GRAY, 0);
336 tpg_s_quality(&dev->tpg, TPG_QUAL_COLOR, 0);
340 * Get the current picture quality and the associated afc value.
342 static enum tpg_quality vivid_get_quality(struct vivid_dev *dev, s32 *afc)
344 unsigned freq_modulus;
348 if (tpg_g_quality(&dev->tpg) == TPG_QUAL_COLOR ||
349 tpg_g_quality(&dev->tpg) == TPG_QUAL_NOISE)
350 return tpg_g_quality(&dev->tpg);
353 * There is a fake channel every 6 MHz at 49.25, 55.25, etc.
354 * From +/- 0.25 MHz around the channel there is color, and from
355 * +/- 1 MHz there is grayscale (chroma is lost).
356 * Everywhere else it is just gray.
358 freq_modulus = (dev->tv_freq - 676 /* (43.25-1) * 16 */) % (6 * 16);
360 *afc = freq_modulus - 1 * 16;
361 return TPG_QUAL_GRAY;
364 enum tpg_video_aspect vivid_get_video_aspect(const struct vivid_dev *dev)
366 if (vivid_is_sdtv_cap(dev))
367 return dev->std_aspect_ratio;
369 if (vivid_is_hdmi_cap(dev))
370 return dev->dv_timings_aspect_ratio;
372 return TPG_VIDEO_ASPECT_IMAGE;
375 static enum tpg_pixel_aspect vivid_get_pixel_aspect(const struct vivid_dev *dev)
377 if (vivid_is_sdtv_cap(dev))
378 return (dev->std_cap & V4L2_STD_525_60) ?
379 TPG_PIXEL_ASPECT_NTSC : TPG_PIXEL_ASPECT_PAL;
381 if (vivid_is_hdmi_cap(dev) &&
382 dev->src_rect.width == 720 && dev->src_rect.height <= 576)
383 return dev->src_rect.height == 480 ?
384 TPG_PIXEL_ASPECT_NTSC : TPG_PIXEL_ASPECT_PAL;
386 return TPG_PIXEL_ASPECT_SQUARE;
390 * Called whenever the format has to be reset which can occur when
391 * changing inputs, standard, timings, etc.
393 void vivid_update_format_cap(struct vivid_dev *dev, bool keep_controls)
395 struct v4l2_bt_timings *bt = &dev->dv_timings_cap.bt;
399 switch (dev->input_type[dev->input]) {
402 dev->src_rect.width = webcam_sizes[dev->webcam_size_idx].width;
403 dev->src_rect.height = webcam_sizes[dev->webcam_size_idx].height;
404 dev->timeperframe_vid_cap = webcam_intervals[dev->webcam_ival_idx];
405 dev->field_cap = V4L2_FIELD_NONE;
406 tpg_s_rgb_range(&dev->tpg, V4L2_DV_RGB_RANGE_AUTO);
410 dev->field_cap = dev->tv_field_cap;
411 dev->src_rect.width = 720;
412 if (dev->std_cap & V4L2_STD_525_60) {
413 dev->src_rect.height = 480;
414 dev->timeperframe_vid_cap = (struct v4l2_fract) { 1001, 30000 };
415 dev->service_set_cap = V4L2_SLICED_CAPTION_525;
417 dev->src_rect.height = 576;
418 dev->timeperframe_vid_cap = (struct v4l2_fract) { 1000, 25000 };
419 dev->service_set_cap = V4L2_SLICED_WSS_625 | V4L2_SLICED_TELETEXT_B;
421 tpg_s_rgb_range(&dev->tpg, V4L2_DV_RGB_RANGE_AUTO);
424 dev->src_rect.width = bt->width;
425 dev->src_rect.height = bt->height;
426 size = V4L2_DV_BT_FRAME_WIDTH(bt) * V4L2_DV_BT_FRAME_HEIGHT(bt);
427 if (dev->reduced_fps && can_reduce_fps(bt)) {
428 pixelclock = div_u64(bt->pixelclock * 1000, 1001);
429 bt->flags |= V4L2_DV_FL_REDUCED_FPS;
431 pixelclock = bt->pixelclock;
432 bt->flags &= ~V4L2_DV_FL_REDUCED_FPS;
434 dev->timeperframe_vid_cap = (struct v4l2_fract) {
435 size / 100, (u32)pixelclock / 100
438 dev->field_cap = V4L2_FIELD_ALTERNATE;
440 dev->field_cap = V4L2_FIELD_NONE;
443 * We can be called from within s_ctrl, in that case we can't
444 * set/get controls. Luckily we don't need to in that case.
446 if (keep_controls || !dev->colorspace)
448 if (bt->flags & V4L2_DV_FL_IS_CE_VIDEO) {
449 if (bt->width == 720 && bt->height <= 576)
450 v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_170M);
452 v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_709);
453 v4l2_ctrl_s_ctrl(dev->real_rgb_range_cap, 1);
455 v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_SRGB);
456 v4l2_ctrl_s_ctrl(dev->real_rgb_range_cap, 0);
458 tpg_s_rgb_range(&dev->tpg, v4l2_ctrl_g_ctrl(dev->rgb_range_cap));
461 vivid_update_quality(dev);
462 tpg_reset_source(&dev->tpg, dev->src_rect.width, dev->src_rect.height, dev->field_cap);
463 dev->crop_cap = dev->src_rect;
464 dev->crop_bounds_cap = dev->src_rect;
465 dev->compose_cap = dev->crop_cap;
466 if (V4L2_FIELD_HAS_T_OR_B(dev->field_cap))
467 dev->compose_cap.height /= 2;
468 dev->fmt_cap_rect = dev->compose_cap;
469 tpg_s_video_aspect(&dev->tpg, vivid_get_video_aspect(dev));
470 tpg_s_pixel_aspect(&dev->tpg, vivid_get_pixel_aspect(dev));
471 tpg_update_mv_step(&dev->tpg);
474 /* Map the field to something that is valid for the current input */
475 static enum v4l2_field vivid_field_cap(struct vivid_dev *dev, enum v4l2_field field)
477 if (vivid_is_sdtv_cap(dev)) {
479 case V4L2_FIELD_INTERLACED_TB:
480 case V4L2_FIELD_INTERLACED_BT:
481 case V4L2_FIELD_SEQ_TB:
482 case V4L2_FIELD_SEQ_BT:
484 case V4L2_FIELD_BOTTOM:
485 case V4L2_FIELD_ALTERNATE:
487 case V4L2_FIELD_INTERLACED:
489 return V4L2_FIELD_INTERLACED;
492 if (vivid_is_hdmi_cap(dev))
493 return dev->dv_timings_cap.bt.interlaced ? V4L2_FIELD_ALTERNATE :
495 return V4L2_FIELD_NONE;
498 static unsigned vivid_colorspace_cap(struct vivid_dev *dev)
500 if (!dev->loop_video || vivid_is_webcam(dev) || vivid_is_tv_cap(dev))
501 return tpg_g_colorspace(&dev->tpg);
502 return dev->colorspace_out;
505 static unsigned vivid_xfer_func_cap(struct vivid_dev *dev)
507 if (!dev->loop_video || vivid_is_webcam(dev) || vivid_is_tv_cap(dev))
508 return tpg_g_xfer_func(&dev->tpg);
509 return dev->xfer_func_out;
512 static unsigned vivid_ycbcr_enc_cap(struct vivid_dev *dev)
514 if (!dev->loop_video || vivid_is_webcam(dev) || vivid_is_tv_cap(dev))
515 return tpg_g_ycbcr_enc(&dev->tpg);
516 return dev->ycbcr_enc_out;
519 static unsigned vivid_quantization_cap(struct vivid_dev *dev)
521 if (!dev->loop_video || vivid_is_webcam(dev) || vivid_is_tv_cap(dev))
522 return tpg_g_quantization(&dev->tpg);
523 return dev->quantization_out;
526 int vivid_g_fmt_vid_cap(struct file *file, void *priv,
527 struct v4l2_format *f)
529 struct vivid_dev *dev = video_drvdata(file);
530 struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
533 mp->width = dev->fmt_cap_rect.width;
534 mp->height = dev->fmt_cap_rect.height;
535 mp->field = dev->field_cap;
536 mp->pixelformat = dev->fmt_cap->fourcc;
537 mp->colorspace = vivid_colorspace_cap(dev);
538 mp->xfer_func = vivid_xfer_func_cap(dev);
539 mp->ycbcr_enc = vivid_ycbcr_enc_cap(dev);
540 mp->quantization = vivid_quantization_cap(dev);
541 mp->num_planes = dev->fmt_cap->buffers;
542 for (p = 0; p < mp->num_planes; p++) {
543 mp->plane_fmt[p].bytesperline = tpg_g_bytesperline(&dev->tpg, p);
544 mp->plane_fmt[p].sizeimage =
545 tpg_g_line_width(&dev->tpg, p) * mp->height +
546 dev->fmt_cap->data_offset[p];
551 int vivid_try_fmt_vid_cap(struct file *file, void *priv,
552 struct v4l2_format *f)
554 struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
555 struct v4l2_plane_pix_format *pfmt = mp->plane_fmt;
556 struct vivid_dev *dev = video_drvdata(file);
557 const struct vivid_fmt *fmt;
558 unsigned bytesperline, max_bpl;
563 fmt = vivid_get_format(dev, mp->pixelformat);
565 dprintk(dev, 1, "Fourcc format (0x%08x) unknown.\n",
567 mp->pixelformat = V4L2_PIX_FMT_YUYV;
568 fmt = vivid_get_format(dev, mp->pixelformat);
571 mp->field = vivid_field_cap(dev, mp->field);
572 if (vivid_is_webcam(dev)) {
573 const struct v4l2_frmsize_discrete *sz =
574 v4l2_find_nearest_format(&webcam_probe, mp->width, mp->height);
578 } else if (vivid_is_sdtv_cap(dev)) {
580 h = (dev->std_cap & V4L2_STD_525_60) ? 480 : 576;
582 w = dev->src_rect.width;
583 h = dev->src_rect.height;
585 if (V4L2_FIELD_HAS_T_OR_B(mp->field))
587 if (vivid_is_webcam(dev) ||
588 (!dev->has_scaler_cap && !dev->has_crop_cap && !dev->has_compose_cap)) {
590 mp->height = h / factor;
592 struct v4l2_rect r = { 0, 0, mp->width, mp->height * factor };
594 v4l2_rect_set_min_size(&r, &vivid_min_rect);
595 v4l2_rect_set_max_size(&r, &vivid_max_rect);
596 if (dev->has_scaler_cap && !dev->has_compose_cap) {
597 struct v4l2_rect max_r = { 0, 0, MAX_ZOOM * w, MAX_ZOOM * h };
599 v4l2_rect_set_max_size(&r, &max_r);
600 } else if (!dev->has_scaler_cap && dev->has_crop_cap && !dev->has_compose_cap) {
601 v4l2_rect_set_max_size(&r, &dev->src_rect);
602 } else if (!dev->has_scaler_cap && !dev->has_crop_cap) {
603 v4l2_rect_set_min_size(&r, &dev->src_rect);
606 mp->height = r.height / factor;
609 /* This driver supports custom bytesperline values */
611 mp->num_planes = fmt->buffers;
612 for (p = 0; p < mp->num_planes; p++) {
613 /* Calculate the minimum supported bytesperline value */
614 bytesperline = (mp->width * fmt->bit_depth[p]) >> 3;
615 /* Calculate the maximum supported bytesperline value */
616 max_bpl = (MAX_ZOOM * MAX_WIDTH * fmt->bit_depth[p]) >> 3;
618 if (pfmt[p].bytesperline > max_bpl)
619 pfmt[p].bytesperline = max_bpl;
620 if (pfmt[p].bytesperline < bytesperline)
621 pfmt[p].bytesperline = bytesperline;
622 pfmt[p].sizeimage = tpg_calc_line_width(&dev->tpg, p, pfmt[p].bytesperline) *
623 mp->height + fmt->data_offset[p];
624 memset(pfmt[p].reserved, 0, sizeof(pfmt[p].reserved));
626 mp->colorspace = vivid_colorspace_cap(dev);
627 mp->ycbcr_enc = vivid_ycbcr_enc_cap(dev);
628 mp->xfer_func = vivid_xfer_func_cap(dev);
629 mp->quantization = vivid_quantization_cap(dev);
630 memset(mp->reserved, 0, sizeof(mp->reserved));
634 int vivid_s_fmt_vid_cap(struct file *file, void *priv,
635 struct v4l2_format *f)
637 struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
638 struct vivid_dev *dev = video_drvdata(file);
639 struct v4l2_rect *crop = &dev->crop_cap;
640 struct v4l2_rect *compose = &dev->compose_cap;
641 struct vb2_queue *q = &dev->vb_vid_cap_q;
642 int ret = vivid_try_fmt_vid_cap(file, priv, f);
650 if (vb2_is_busy(q)) {
651 dprintk(dev, 1, "%s device busy\n", __func__);
655 if (dev->overlay_cap_owner && dev->fb_cap.fmt.pixelformat != mp->pixelformat) {
656 dprintk(dev, 1, "overlay is active, can't change pixelformat\n");
660 dev->fmt_cap = vivid_get_format(dev, mp->pixelformat);
661 if (V4L2_FIELD_HAS_T_OR_B(mp->field))
664 /* Note: the webcam input doesn't support scaling, cropping or composing */
666 if (!vivid_is_webcam(dev) &&
667 (dev->has_scaler_cap || dev->has_crop_cap || dev->has_compose_cap)) {
668 struct v4l2_rect r = { 0, 0, mp->width, mp->height };
670 if (dev->has_scaler_cap) {
671 if (dev->has_compose_cap)
672 v4l2_rect_map_inside(compose, &r);
675 if (dev->has_crop_cap && !dev->has_compose_cap) {
676 struct v4l2_rect min_r = {
679 factor * r.height / MAX_ZOOM
681 struct v4l2_rect max_r = {
684 factor * r.height * MAX_ZOOM
687 v4l2_rect_set_min_size(crop, &min_r);
688 v4l2_rect_set_max_size(crop, &max_r);
689 v4l2_rect_map_inside(crop, &dev->crop_bounds_cap);
690 } else if (dev->has_crop_cap) {
691 struct v4l2_rect min_r = {
693 compose->width / MAX_ZOOM,
694 factor * compose->height / MAX_ZOOM
696 struct v4l2_rect max_r = {
698 compose->width * MAX_ZOOM,
699 factor * compose->height * MAX_ZOOM
702 v4l2_rect_set_min_size(crop, &min_r);
703 v4l2_rect_set_max_size(crop, &max_r);
704 v4l2_rect_map_inside(crop, &dev->crop_bounds_cap);
706 } else if (dev->has_crop_cap && !dev->has_compose_cap) {
708 v4l2_rect_set_size_to(crop, &r);
709 v4l2_rect_map_inside(crop, &dev->crop_bounds_cap);
712 v4l2_rect_set_size_to(compose, &r);
713 } else if (!dev->has_crop_cap) {
714 v4l2_rect_map_inside(compose, &r);
717 v4l2_rect_set_max_size(crop, &r);
718 v4l2_rect_map_inside(crop, &dev->crop_bounds_cap);
719 compose->top *= factor;
720 compose->height *= factor;
721 v4l2_rect_set_size_to(compose, crop);
722 v4l2_rect_map_inside(compose, &r);
723 compose->top /= factor;
724 compose->height /= factor;
726 } else if (vivid_is_webcam(dev)) {
727 /* Guaranteed to be a match */
728 for (i = 0; i < ARRAY_SIZE(webcam_sizes); i++)
729 if (webcam_sizes[i].width == mp->width &&
730 webcam_sizes[i].height == mp->height)
732 dev->webcam_size_idx = i;
733 if (dev->webcam_ival_idx >= 2 * (VIVID_WEBCAM_SIZES - i))
734 dev->webcam_ival_idx = 2 * (VIVID_WEBCAM_SIZES - i) - 1;
735 vivid_update_format_cap(dev, false);
737 struct v4l2_rect r = { 0, 0, mp->width, mp->height };
739 v4l2_rect_set_size_to(compose, &r);
741 v4l2_rect_set_size_to(crop, &r);
744 dev->fmt_cap_rect.width = mp->width;
745 dev->fmt_cap_rect.height = mp->height;
746 tpg_s_buf_height(&dev->tpg, mp->height);
747 tpg_s_fourcc(&dev->tpg, dev->fmt_cap->fourcc);
748 for (p = 0; p < tpg_g_buffers(&dev->tpg); p++)
749 tpg_s_bytesperline(&dev->tpg, p, mp->plane_fmt[p].bytesperline);
750 dev->field_cap = mp->field;
751 if (dev->field_cap == V4L2_FIELD_ALTERNATE)
752 tpg_s_field(&dev->tpg, V4L2_FIELD_TOP, true);
754 tpg_s_field(&dev->tpg, dev->field_cap, false);
755 tpg_s_crop_compose(&dev->tpg, &dev->crop_cap, &dev->compose_cap);
756 if (vivid_is_sdtv_cap(dev))
757 dev->tv_field_cap = mp->field;
758 tpg_update_mv_step(&dev->tpg);
762 int vidioc_g_fmt_vid_cap_mplane(struct file *file, void *priv,
763 struct v4l2_format *f)
765 struct vivid_dev *dev = video_drvdata(file);
767 if (!dev->multiplanar)
769 return vivid_g_fmt_vid_cap(file, priv, f);
772 int vidioc_try_fmt_vid_cap_mplane(struct file *file, void *priv,
773 struct v4l2_format *f)
775 struct vivid_dev *dev = video_drvdata(file);
777 if (!dev->multiplanar)
779 return vivid_try_fmt_vid_cap(file, priv, f);
782 int vidioc_s_fmt_vid_cap_mplane(struct file *file, void *priv,
783 struct v4l2_format *f)
785 struct vivid_dev *dev = video_drvdata(file);
787 if (!dev->multiplanar)
789 return vivid_s_fmt_vid_cap(file, priv, f);
792 int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
793 struct v4l2_format *f)
795 struct vivid_dev *dev = video_drvdata(file);
797 if (dev->multiplanar)
799 return fmt_sp2mp_func(file, priv, f, vivid_g_fmt_vid_cap);
802 int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
803 struct v4l2_format *f)
805 struct vivid_dev *dev = video_drvdata(file);
807 if (dev->multiplanar)
809 return fmt_sp2mp_func(file, priv, f, vivid_try_fmt_vid_cap);
812 int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
813 struct v4l2_format *f)
815 struct vivid_dev *dev = video_drvdata(file);
817 if (dev->multiplanar)
819 return fmt_sp2mp_func(file, priv, f, vivid_s_fmt_vid_cap);
822 int vivid_vid_cap_g_selection(struct file *file, void *priv,
823 struct v4l2_selection *sel)
825 struct vivid_dev *dev = video_drvdata(file);
827 if (!dev->has_crop_cap && !dev->has_compose_cap)
829 if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
831 if (vivid_is_webcam(dev))
834 sel->r.left = sel->r.top = 0;
835 switch (sel->target) {
836 case V4L2_SEL_TGT_CROP:
837 if (!dev->has_crop_cap)
839 sel->r = dev->crop_cap;
841 case V4L2_SEL_TGT_CROP_DEFAULT:
842 case V4L2_SEL_TGT_CROP_BOUNDS:
843 if (!dev->has_crop_cap)
845 sel->r = dev->src_rect;
847 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
848 if (!dev->has_compose_cap)
850 sel->r = vivid_max_rect;
852 case V4L2_SEL_TGT_COMPOSE:
853 if (!dev->has_compose_cap)
855 sel->r = dev->compose_cap;
857 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
858 if (!dev->has_compose_cap)
860 sel->r = dev->fmt_cap_rect;
868 int vivid_vid_cap_s_selection(struct file *file, void *fh, struct v4l2_selection *s)
870 struct vivid_dev *dev = video_drvdata(file);
871 struct v4l2_rect *crop = &dev->crop_cap;
872 struct v4l2_rect *compose = &dev->compose_cap;
873 unsigned factor = V4L2_FIELD_HAS_T_OR_B(dev->field_cap) ? 2 : 1;
876 if (!dev->has_crop_cap && !dev->has_compose_cap)
878 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
880 if (vivid_is_webcam(dev))
884 case V4L2_SEL_TGT_CROP:
885 if (!dev->has_crop_cap)
887 ret = vivid_vid_adjust_sel(s->flags, &s->r);
890 v4l2_rect_set_min_size(&s->r, &vivid_min_rect);
891 v4l2_rect_set_max_size(&s->r, &dev->src_rect);
892 v4l2_rect_map_inside(&s->r, &dev->crop_bounds_cap);
894 s->r.height /= factor;
895 if (dev->has_scaler_cap) {
896 struct v4l2_rect fmt = dev->fmt_cap_rect;
897 struct v4l2_rect max_rect = {
899 s->r.width * MAX_ZOOM,
900 s->r.height * MAX_ZOOM
902 struct v4l2_rect min_rect = {
904 s->r.width / MAX_ZOOM,
905 s->r.height / MAX_ZOOM
908 v4l2_rect_set_min_size(&fmt, &min_rect);
909 if (!dev->has_compose_cap)
910 v4l2_rect_set_max_size(&fmt, &max_rect);
911 if (!v4l2_rect_same_size(&dev->fmt_cap_rect, &fmt) &&
912 vb2_is_busy(&dev->vb_vid_cap_q))
914 if (dev->has_compose_cap) {
915 v4l2_rect_set_min_size(compose, &min_rect);
916 v4l2_rect_set_max_size(compose, &max_rect);
918 dev->fmt_cap_rect = fmt;
919 tpg_s_buf_height(&dev->tpg, fmt.height);
920 } else if (dev->has_compose_cap) {
921 struct v4l2_rect fmt = dev->fmt_cap_rect;
923 v4l2_rect_set_min_size(&fmt, &s->r);
924 if (!v4l2_rect_same_size(&dev->fmt_cap_rect, &fmt) &&
925 vb2_is_busy(&dev->vb_vid_cap_q))
927 dev->fmt_cap_rect = fmt;
928 tpg_s_buf_height(&dev->tpg, fmt.height);
929 v4l2_rect_set_size_to(compose, &s->r);
930 v4l2_rect_map_inside(compose, &dev->fmt_cap_rect);
932 if (!v4l2_rect_same_size(&s->r, &dev->fmt_cap_rect) &&
933 vb2_is_busy(&dev->vb_vid_cap_q))
935 v4l2_rect_set_size_to(&dev->fmt_cap_rect, &s->r);
936 v4l2_rect_set_size_to(compose, &s->r);
937 v4l2_rect_map_inside(compose, &dev->fmt_cap_rect);
938 tpg_s_buf_height(&dev->tpg, dev->fmt_cap_rect.height);
941 s->r.height *= factor;
944 case V4L2_SEL_TGT_COMPOSE:
945 if (!dev->has_compose_cap)
947 ret = vivid_vid_adjust_sel(s->flags, &s->r);
950 v4l2_rect_set_min_size(&s->r, &vivid_min_rect);
951 v4l2_rect_set_max_size(&s->r, &dev->fmt_cap_rect);
952 if (dev->has_scaler_cap) {
953 struct v4l2_rect max_rect = {
955 dev->src_rect.width * MAX_ZOOM,
956 (dev->src_rect.height / factor) * MAX_ZOOM
959 v4l2_rect_set_max_size(&s->r, &max_rect);
960 if (dev->has_crop_cap) {
961 struct v4l2_rect min_rect = {
963 s->r.width / MAX_ZOOM,
964 (s->r.height * factor) / MAX_ZOOM
966 struct v4l2_rect max_rect = {
968 s->r.width * MAX_ZOOM,
969 (s->r.height * factor) * MAX_ZOOM
972 v4l2_rect_set_min_size(crop, &min_rect);
973 v4l2_rect_set_max_size(crop, &max_rect);
974 v4l2_rect_map_inside(crop, &dev->crop_bounds_cap);
976 } else if (dev->has_crop_cap) {
978 s->r.height *= factor;
979 v4l2_rect_set_max_size(&s->r, &dev->src_rect);
980 v4l2_rect_set_size_to(crop, &s->r);
981 v4l2_rect_map_inside(crop, &dev->crop_bounds_cap);
983 s->r.height /= factor;
985 v4l2_rect_set_size_to(&s->r, &dev->src_rect);
986 s->r.height /= factor;
988 v4l2_rect_map_inside(&s->r, &dev->fmt_cap_rect);
989 if (dev->bitmap_cap && (compose->width != s->r.width ||
990 compose->height != s->r.height)) {
991 kfree(dev->bitmap_cap);
992 dev->bitmap_cap = NULL;
1000 tpg_s_crop_compose(&dev->tpg, crop, compose);
1004 int vivid_vid_cap_cropcap(struct file *file, void *priv,
1005 struct v4l2_cropcap *cap)
1007 struct vivid_dev *dev = video_drvdata(file);
1009 if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1012 switch (vivid_get_pixel_aspect(dev)) {
1013 case TPG_PIXEL_ASPECT_NTSC:
1014 cap->pixelaspect.numerator = 11;
1015 cap->pixelaspect.denominator = 10;
1017 case TPG_PIXEL_ASPECT_PAL:
1018 cap->pixelaspect.numerator = 54;
1019 cap->pixelaspect.denominator = 59;
1021 case TPG_PIXEL_ASPECT_SQUARE:
1022 cap->pixelaspect.numerator = 1;
1023 cap->pixelaspect.denominator = 1;
1029 int vidioc_enum_fmt_vid_overlay(struct file *file, void *priv,
1030 struct v4l2_fmtdesc *f)
1032 struct vivid_dev *dev = video_drvdata(file);
1033 const struct vivid_fmt *fmt;
1035 if (dev->multiplanar)
1038 if (f->index >= ARRAY_SIZE(formats_ovl))
1041 fmt = &formats_ovl[f->index];
1043 f->pixelformat = fmt->fourcc;
1047 int vidioc_g_fmt_vid_overlay(struct file *file, void *priv,
1048 struct v4l2_format *f)
1050 struct vivid_dev *dev = video_drvdata(file);
1051 const struct v4l2_rect *compose = &dev->compose_cap;
1052 struct v4l2_window *win = &f->fmt.win;
1053 unsigned clipcount = win->clipcount;
1055 if (dev->multiplanar)
1058 win->w.top = dev->overlay_cap_top;
1059 win->w.left = dev->overlay_cap_left;
1060 win->w.width = compose->width;
1061 win->w.height = compose->height;
1062 win->field = dev->overlay_cap_field;
1063 win->clipcount = dev->clipcount_cap;
1064 if (clipcount > dev->clipcount_cap)
1065 clipcount = dev->clipcount_cap;
1066 if (dev->bitmap_cap == NULL)
1068 else if (win->bitmap) {
1069 if (copy_to_user(win->bitmap, dev->bitmap_cap,
1070 ((compose->width + 7) / 8) * compose->height))
1073 if (clipcount && win->clips) {
1074 if (copy_to_user(win->clips, dev->clips_cap,
1075 clipcount * sizeof(dev->clips_cap[0])))
1081 int vidioc_try_fmt_vid_overlay(struct file *file, void *priv,
1082 struct v4l2_format *f)
1084 struct vivid_dev *dev = video_drvdata(file);
1085 const struct v4l2_rect *compose = &dev->compose_cap;
1086 struct v4l2_window *win = &f->fmt.win;
1089 if (dev->multiplanar)
1092 win->w.left = clamp_t(int, win->w.left,
1093 -dev->fb_cap.fmt.width, dev->fb_cap.fmt.width);
1094 win->w.top = clamp_t(int, win->w.top,
1095 -dev->fb_cap.fmt.height, dev->fb_cap.fmt.height);
1096 win->w.width = compose->width;
1097 win->w.height = compose->height;
1098 if (win->field != V4L2_FIELD_BOTTOM && win->field != V4L2_FIELD_TOP)
1099 win->field = V4L2_FIELD_ANY;
1101 win->global_alpha = 0;
1102 if (win->clipcount && !win->clips)
1104 if (win->clipcount > MAX_CLIPS)
1105 win->clipcount = MAX_CLIPS;
1106 if (win->clipcount) {
1107 if (copy_from_user(dev->try_clips_cap, win->clips,
1108 win->clipcount * sizeof(dev->clips_cap[0])))
1110 for (i = 0; i < win->clipcount; i++) {
1111 struct v4l2_rect *r = &dev->try_clips_cap[i].c;
1113 r->top = clamp_t(s32, r->top, 0, dev->fb_cap.fmt.height - 1);
1114 r->height = clamp_t(s32, r->height, 1, dev->fb_cap.fmt.height - r->top);
1115 r->left = clamp_t(u32, r->left, 0, dev->fb_cap.fmt.width - 1);
1116 r->width = clamp_t(u32, r->width, 1, dev->fb_cap.fmt.width - r->left);
1119 * Yeah, so sue me, it's an O(n^2) algorithm. But n is a small
1120 * number and it's typically a one-time deal.
1122 for (i = 0; i < win->clipcount - 1; i++) {
1123 struct v4l2_rect *r1 = &dev->try_clips_cap[i].c;
1125 for (j = i + 1; j < win->clipcount; j++) {
1126 struct v4l2_rect *r2 = &dev->try_clips_cap[j].c;
1128 if (v4l2_rect_overlap(r1, r2))
1132 if (copy_to_user(win->clips, dev->try_clips_cap,
1133 win->clipcount * sizeof(dev->clips_cap[0])))
1139 int vidioc_s_fmt_vid_overlay(struct file *file, void *priv,
1140 struct v4l2_format *f)
1142 struct vivid_dev *dev = video_drvdata(file);
1143 const struct v4l2_rect *compose = &dev->compose_cap;
1144 struct v4l2_window *win = &f->fmt.win;
1145 int ret = vidioc_try_fmt_vid_overlay(file, priv, f);
1146 unsigned bitmap_size = ((compose->width + 7) / 8) * compose->height;
1147 unsigned clips_size = win->clipcount * sizeof(dev->clips_cap[0]);
1148 void *new_bitmap = NULL;
1154 new_bitmap = vzalloc(bitmap_size);
1156 if (new_bitmap == NULL)
1158 if (copy_from_user(new_bitmap, win->bitmap, bitmap_size)) {
1164 dev->overlay_cap_top = win->w.top;
1165 dev->overlay_cap_left = win->w.left;
1166 dev->overlay_cap_field = win->field;
1167 vfree(dev->bitmap_cap);
1168 dev->bitmap_cap = new_bitmap;
1169 dev->clipcount_cap = win->clipcount;
1170 if (dev->clipcount_cap)
1171 memcpy(dev->clips_cap, dev->try_clips_cap, clips_size);
1175 int vivid_vid_cap_overlay(struct file *file, void *fh, unsigned i)
1177 struct vivid_dev *dev = video_drvdata(file);
1179 if (dev->multiplanar)
1182 if (i && dev->fb_vbase_cap == NULL)
1185 if (i && dev->fb_cap.fmt.pixelformat != dev->fmt_cap->fourcc) {
1186 dprintk(dev, 1, "mismatch between overlay and video capture pixelformats\n");
1190 if (dev->overlay_cap_owner && dev->overlay_cap_owner != fh)
1192 dev->overlay_cap_owner = i ? fh : NULL;
1196 int vivid_vid_cap_g_fbuf(struct file *file, void *fh,
1197 struct v4l2_framebuffer *a)
1199 struct vivid_dev *dev = video_drvdata(file);
1201 if (dev->multiplanar)
1205 a->capability = V4L2_FBUF_CAP_BITMAP_CLIPPING |
1206 V4L2_FBUF_CAP_LIST_CLIPPING;
1207 a->flags = V4L2_FBUF_FLAG_PRIMARY;
1208 a->fmt.field = V4L2_FIELD_NONE;
1209 a->fmt.colorspace = V4L2_COLORSPACE_SRGB;
1214 int vivid_vid_cap_s_fbuf(struct file *file, void *fh,
1215 const struct v4l2_framebuffer *a)
1217 struct vivid_dev *dev = video_drvdata(file);
1218 const struct vivid_fmt *fmt;
1220 if (dev->multiplanar)
1223 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
1226 if (dev->overlay_cap_owner)
1229 if (a->base == NULL) {
1230 dev->fb_cap.base = NULL;
1231 dev->fb_vbase_cap = NULL;
1235 if (a->fmt.width < 48 || a->fmt.height < 32)
1237 fmt = vivid_get_format(dev, a->fmt.pixelformat);
1238 if (!fmt || !fmt->can_do_overlay)
1240 if (a->fmt.bytesperline < (a->fmt.width * fmt->bit_depth[0]) / 8)
1242 if (a->fmt.height * a->fmt.bytesperline < a->fmt.sizeimage)
1245 dev->fb_vbase_cap = phys_to_virt((unsigned long)a->base);
1247 dev->overlay_cap_left = clamp_t(int, dev->overlay_cap_left,
1248 -dev->fb_cap.fmt.width, dev->fb_cap.fmt.width);
1249 dev->overlay_cap_top = clamp_t(int, dev->overlay_cap_top,
1250 -dev->fb_cap.fmt.height, dev->fb_cap.fmt.height);
1254 static const struct v4l2_audio vivid_audio_inputs[] = {
1255 { 0, "TV", V4L2_AUDCAP_STEREO },
1256 { 1, "Line-In", V4L2_AUDCAP_STEREO },
1259 int vidioc_enum_input(struct file *file, void *priv,
1260 struct v4l2_input *inp)
1262 struct vivid_dev *dev = video_drvdata(file);
1264 if (inp->index >= dev->num_inputs)
1267 inp->type = V4L2_INPUT_TYPE_CAMERA;
1268 switch (dev->input_type[inp->index]) {
1270 snprintf(inp->name, sizeof(inp->name), "Webcam %u",
1271 dev->input_name_counter[inp->index]);
1272 inp->capabilities = 0;
1275 snprintf(inp->name, sizeof(inp->name), "TV %u",
1276 dev->input_name_counter[inp->index]);
1277 inp->type = V4L2_INPUT_TYPE_TUNER;
1278 inp->std = V4L2_STD_ALL;
1279 if (dev->has_audio_inputs)
1280 inp->audioset = (1 << ARRAY_SIZE(vivid_audio_inputs)) - 1;
1281 inp->capabilities = V4L2_IN_CAP_STD;
1284 snprintf(inp->name, sizeof(inp->name), "S-Video %u",
1285 dev->input_name_counter[inp->index]);
1286 inp->std = V4L2_STD_ALL;
1287 if (dev->has_audio_inputs)
1288 inp->audioset = (1 << ARRAY_SIZE(vivid_audio_inputs)) - 1;
1289 inp->capabilities = V4L2_IN_CAP_STD;
1292 snprintf(inp->name, sizeof(inp->name), "HDMI %u",
1293 dev->input_name_counter[inp->index]);
1294 inp->capabilities = V4L2_IN_CAP_DV_TIMINGS;
1295 if (dev->edid_blocks == 0 ||
1296 dev->dv_timings_signal_mode == NO_SIGNAL)
1297 inp->status |= V4L2_IN_ST_NO_SIGNAL;
1298 else if (dev->dv_timings_signal_mode == NO_LOCK ||
1299 dev->dv_timings_signal_mode == OUT_OF_RANGE)
1300 inp->status |= V4L2_IN_ST_NO_H_LOCK;
1303 if (dev->sensor_hflip)
1304 inp->status |= V4L2_IN_ST_HFLIP;
1305 if (dev->sensor_vflip)
1306 inp->status |= V4L2_IN_ST_VFLIP;
1307 if (dev->input == inp->index && vivid_is_sdtv_cap(dev)) {
1308 if (dev->std_signal_mode == NO_SIGNAL) {
1309 inp->status |= V4L2_IN_ST_NO_SIGNAL;
1310 } else if (dev->std_signal_mode == NO_LOCK) {
1311 inp->status |= V4L2_IN_ST_NO_H_LOCK;
1312 } else if (vivid_is_tv_cap(dev)) {
1313 switch (tpg_g_quality(&dev->tpg)) {
1315 inp->status |= V4L2_IN_ST_COLOR_KILL;
1317 case TPG_QUAL_NOISE:
1318 inp->status |= V4L2_IN_ST_NO_H_LOCK;
1328 int vidioc_g_input(struct file *file, void *priv, unsigned *i)
1330 struct vivid_dev *dev = video_drvdata(file);
1336 int vidioc_s_input(struct file *file, void *priv, unsigned i)
1338 struct vivid_dev *dev = video_drvdata(file);
1339 struct v4l2_bt_timings *bt = &dev->dv_timings_cap.bt;
1340 unsigned brightness;
1342 if (i >= dev->num_inputs)
1345 if (i == dev->input)
1348 if (vb2_is_busy(&dev->vb_vid_cap_q) || vb2_is_busy(&dev->vb_vbi_cap_q))
1352 dev->vid_cap_dev.tvnorms = 0;
1353 if (dev->input_type[i] == TV || dev->input_type[i] == SVID) {
1354 dev->tv_audio_input = (dev->input_type[i] == TV) ? 0 : 1;
1355 dev->vid_cap_dev.tvnorms = V4L2_STD_ALL;
1357 dev->vbi_cap_dev.tvnorms = dev->vid_cap_dev.tvnorms;
1358 vivid_update_format_cap(dev, false);
1360 if (dev->colorspace) {
1361 switch (dev->input_type[i]) {
1363 v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_SRGB);
1367 v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_170M);
1370 if (bt->flags & V4L2_DV_FL_IS_CE_VIDEO) {
1371 if (dev->src_rect.width == 720 && dev->src_rect.height <= 576)
1372 v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_170M);
1374 v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_709);
1376 v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_SRGB);
1383 * Modify the brightness range depending on the input.
1384 * This makes it easy to use vivid to test if applications can
1385 * handle control range modifications and is also how this is
1386 * typically used in practice as different inputs may be hooked
1387 * up to different receivers with different control ranges.
1389 brightness = 128 * i + dev->input_brightness[i];
1390 v4l2_ctrl_modify_range(dev->brightness,
1391 128 * i, 255 + 128 * i, 1, 128 + 128 * i);
1392 v4l2_ctrl_s_ctrl(dev->brightness, brightness);
1396 int vidioc_enumaudio(struct file *file, void *fh, struct v4l2_audio *vin)
1398 if (vin->index >= ARRAY_SIZE(vivid_audio_inputs))
1400 *vin = vivid_audio_inputs[vin->index];
1404 int vidioc_g_audio(struct file *file, void *fh, struct v4l2_audio *vin)
1406 struct vivid_dev *dev = video_drvdata(file);
1408 if (!vivid_is_sdtv_cap(dev))
1410 *vin = vivid_audio_inputs[dev->tv_audio_input];
1414 int vidioc_s_audio(struct file *file, void *fh, const struct v4l2_audio *vin)
1416 struct vivid_dev *dev = video_drvdata(file);
1418 if (!vivid_is_sdtv_cap(dev))
1420 if (vin->index >= ARRAY_SIZE(vivid_audio_inputs))
1422 dev->tv_audio_input = vin->index;
1426 int vivid_video_g_frequency(struct file *file, void *fh, struct v4l2_frequency *vf)
1428 struct vivid_dev *dev = video_drvdata(file);
1432 vf->frequency = dev->tv_freq;
1436 int vivid_video_s_frequency(struct file *file, void *fh, const struct v4l2_frequency *vf)
1438 struct vivid_dev *dev = video_drvdata(file);
1442 dev->tv_freq = clamp_t(unsigned, vf->frequency, MIN_TV_FREQ, MAX_TV_FREQ);
1443 if (vivid_is_tv_cap(dev))
1444 vivid_update_quality(dev);
1448 int vivid_video_s_tuner(struct file *file, void *fh, const struct v4l2_tuner *vt)
1450 struct vivid_dev *dev = video_drvdata(file);
1454 if (vt->audmode > V4L2_TUNER_MODE_LANG1_LANG2)
1456 dev->tv_audmode = vt->audmode;
1460 int vivid_video_g_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
1462 struct vivid_dev *dev = video_drvdata(file);
1463 enum tpg_quality qual;
1468 vt->capability = V4L2_TUNER_CAP_NORM | V4L2_TUNER_CAP_STEREO |
1469 V4L2_TUNER_CAP_LANG1 | V4L2_TUNER_CAP_LANG2;
1470 vt->audmode = dev->tv_audmode;
1471 vt->rangelow = MIN_TV_FREQ;
1472 vt->rangehigh = MAX_TV_FREQ;
1473 qual = vivid_get_quality(dev, &vt->afc);
1474 if (qual == TPG_QUAL_COLOR)
1475 vt->signal = 0xffff;
1476 else if (qual == TPG_QUAL_GRAY)
1477 vt->signal = 0x8000;
1480 if (qual == TPG_QUAL_NOISE) {
1482 } else if (qual == TPG_QUAL_GRAY) {
1483 vt->rxsubchans = V4L2_TUNER_SUB_MONO;
1485 unsigned channel_nr = dev->tv_freq / (6 * 16);
1486 unsigned options = (dev->std_cap & V4L2_STD_NTSC_M) ? 4 : 3;
1488 switch (channel_nr % options) {
1490 vt->rxsubchans = V4L2_TUNER_SUB_MONO;
1493 vt->rxsubchans = V4L2_TUNER_SUB_STEREO;
1496 if (dev->std_cap & V4L2_STD_NTSC_M)
1497 vt->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_SAP;
1499 vt->rxsubchans = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
1502 vt->rxsubchans = V4L2_TUNER_SUB_STEREO | V4L2_TUNER_SUB_SAP;
1506 strlcpy(vt->name, "TV Tuner", sizeof(vt->name));
1510 /* Must remain in sync with the vivid_ctrl_standard_strings array */
1511 const v4l2_std_id vivid_standard[] = {
1516 V4L2_STD_PAL_BG | V4L2_STD_PAL_H,
1523 V4L2_STD_SECAM_B | V4L2_STD_SECAM_G | V4L2_STD_SECAM_H,
1530 /* Must remain in sync with the vivid_standard array */
1531 const char * const vivid_ctrl_standard_strings[] = {
1550 int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *id)
1552 struct vivid_dev *dev = video_drvdata(file);
1554 if (!vivid_is_sdtv_cap(dev))
1556 if (dev->std_signal_mode == NO_SIGNAL ||
1557 dev->std_signal_mode == NO_LOCK) {
1558 *id = V4L2_STD_UNKNOWN;
1561 if (vivid_is_tv_cap(dev) && tpg_g_quality(&dev->tpg) == TPG_QUAL_NOISE) {
1562 *id = V4L2_STD_UNKNOWN;
1563 } else if (dev->std_signal_mode == CURRENT_STD) {
1565 } else if (dev->std_signal_mode == SELECTED_STD) {
1566 *id = dev->query_std;
1568 *id = vivid_standard[dev->query_std_last];
1569 dev->query_std_last = (dev->query_std_last + 1) % ARRAY_SIZE(vivid_standard);
1575 int vivid_vid_cap_s_std(struct file *file, void *priv, v4l2_std_id id)
1577 struct vivid_dev *dev = video_drvdata(file);
1579 if (!vivid_is_sdtv_cap(dev))
1581 if (dev->std_cap == id)
1583 if (vb2_is_busy(&dev->vb_vid_cap_q) || vb2_is_busy(&dev->vb_vbi_cap_q))
1586 vivid_update_format_cap(dev, false);
1590 static void find_aspect_ratio(u32 width, u32 height,
1591 u32 *num, u32 *denom)
1593 if (!(height % 3) && ((height * 4 / 3) == width)) {
1596 } else if (!(height % 9) && ((height * 16 / 9) == width)) {
1599 } else if (!(height % 10) && ((height * 16 / 10) == width)) {
1602 } else if (!(height % 4) && ((height * 5 / 4) == width)) {
1605 } else if (!(height % 9) && ((height * 15 / 9) == width)) {
1608 } else { /* default to 16:9 */
1614 static bool valid_cvt_gtf_timings(struct v4l2_dv_timings *timings)
1616 struct v4l2_bt_timings *bt = &timings->bt;
1621 if (!v4l2_valid_dv_timings(timings, &vivid_dv_timings_cap,
1625 total_h_pixel = V4L2_DV_BT_FRAME_WIDTH(bt);
1626 total_v_lines = V4L2_DV_BT_FRAME_HEIGHT(bt);
1628 h_freq = (u32)bt->pixelclock / total_h_pixel;
1630 if (bt->standards == 0 || (bt->standards & V4L2_DV_BT_STD_CVT)) {
1631 if (v4l2_detect_cvt(total_v_lines, h_freq, bt->vsync, bt->width,
1632 bt->polarities, bt->interlaced, timings))
1636 if (bt->standards == 0 || (bt->standards & V4L2_DV_BT_STD_GTF)) {
1637 struct v4l2_fract aspect_ratio;
1639 find_aspect_ratio(bt->width, bt->height,
1640 &aspect_ratio.numerator,
1641 &aspect_ratio.denominator);
1642 if (v4l2_detect_gtf(total_v_lines, h_freq, bt->vsync,
1643 bt->polarities, bt->interlaced,
1644 aspect_ratio, timings))
1650 int vivid_vid_cap_s_dv_timings(struct file *file, void *_fh,
1651 struct v4l2_dv_timings *timings)
1653 struct vivid_dev *dev = video_drvdata(file);
1655 if (!vivid_is_hdmi_cap(dev))
1657 if (!v4l2_find_dv_timings_cap(timings, &vivid_dv_timings_cap,
1659 !valid_cvt_gtf_timings(timings))
1662 if (v4l2_match_dv_timings(timings, &dev->dv_timings_cap, 0, false))
1664 if (vb2_is_busy(&dev->vb_vid_cap_q))
1667 dev->dv_timings_cap = *timings;
1668 vivid_update_format_cap(dev, false);
1672 int vidioc_query_dv_timings(struct file *file, void *_fh,
1673 struct v4l2_dv_timings *timings)
1675 struct vivid_dev *dev = video_drvdata(file);
1677 if (!vivid_is_hdmi_cap(dev))
1679 if (dev->dv_timings_signal_mode == NO_SIGNAL ||
1680 dev->edid_blocks == 0)
1682 if (dev->dv_timings_signal_mode == NO_LOCK)
1684 if (dev->dv_timings_signal_mode == OUT_OF_RANGE) {
1685 timings->bt.pixelclock = vivid_dv_timings_cap.bt.max_pixelclock * 2;
1688 if (dev->dv_timings_signal_mode == CURRENT_DV_TIMINGS) {
1689 *timings = dev->dv_timings_cap;
1690 } else if (dev->dv_timings_signal_mode == SELECTED_DV_TIMINGS) {
1691 *timings = v4l2_dv_timings_presets[dev->query_dv_timings];
1693 *timings = v4l2_dv_timings_presets[dev->query_dv_timings_last];
1694 dev->query_dv_timings_last = (dev->query_dv_timings_last + 1) %
1695 dev->query_dv_timings_size;
1700 int vidioc_s_edid(struct file *file, void *_fh,
1701 struct v4l2_edid *edid)
1703 struct vivid_dev *dev = video_drvdata(file);
1705 memset(edid->reserved, 0, sizeof(edid->reserved));
1706 if (edid->pad >= dev->num_inputs)
1708 if (dev->input_type[edid->pad] != HDMI || edid->start_block)
1710 if (edid->blocks == 0) {
1711 dev->edid_blocks = 0;
1714 if (edid->blocks > dev->edid_max_blocks) {
1715 edid->blocks = dev->edid_max_blocks;
1718 dev->edid_blocks = edid->blocks;
1719 memcpy(dev->edid, edid->edid, edid->blocks * 128);
1723 int vidioc_enum_framesizes(struct file *file, void *fh,
1724 struct v4l2_frmsizeenum *fsize)
1726 struct vivid_dev *dev = video_drvdata(file);
1728 if (!vivid_is_webcam(dev) && !dev->has_scaler_cap)
1730 if (vivid_get_format(dev, fsize->pixel_format) == NULL)
1732 if (vivid_is_webcam(dev)) {
1733 if (fsize->index >= ARRAY_SIZE(webcam_sizes))
1735 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1736 fsize->discrete = webcam_sizes[fsize->index];
1741 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
1742 fsize->stepwise.min_width = MIN_WIDTH;
1743 fsize->stepwise.max_width = MAX_WIDTH * MAX_ZOOM;
1744 fsize->stepwise.step_width = 2;
1745 fsize->stepwise.min_height = MIN_HEIGHT;
1746 fsize->stepwise.max_height = MAX_HEIGHT * MAX_ZOOM;
1747 fsize->stepwise.step_height = 2;
1751 /* timeperframe is arbitrary and continuous */
1752 int vidioc_enum_frameintervals(struct file *file, void *priv,
1753 struct v4l2_frmivalenum *fival)
1755 struct vivid_dev *dev = video_drvdata(file);
1756 const struct vivid_fmt *fmt;
1759 fmt = vivid_get_format(dev, fival->pixel_format);
1763 if (!vivid_is_webcam(dev)) {
1766 if (fival->width < MIN_WIDTH || fival->width > MAX_WIDTH * MAX_ZOOM)
1768 if (fival->height < MIN_HEIGHT || fival->height > MAX_HEIGHT * MAX_ZOOM)
1770 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1771 fival->discrete = dev->timeperframe_vid_cap;
1775 for (i = 0; i < ARRAY_SIZE(webcam_sizes); i++)
1776 if (fival->width == webcam_sizes[i].width &&
1777 fival->height == webcam_sizes[i].height)
1779 if (i == ARRAY_SIZE(webcam_sizes))
1781 if (fival->index >= 2 * (VIVID_WEBCAM_SIZES - i))
1783 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1784 fival->discrete = webcam_intervals[fival->index];
1788 int vivid_vid_cap_g_parm(struct file *file, void *priv,
1789 struct v4l2_streamparm *parm)
1791 struct vivid_dev *dev = video_drvdata(file);
1793 if (parm->type != (dev->multiplanar ?
1794 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE :
1795 V4L2_BUF_TYPE_VIDEO_CAPTURE))
1798 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
1799 parm->parm.capture.timeperframe = dev->timeperframe_vid_cap;
1800 parm->parm.capture.readbuffers = 1;
1804 #define FRACT_CMP(a, OP, b) \
1805 ((u64)(a).numerator * (b).denominator OP (u64)(b).numerator * (a).denominator)
1807 int vivid_vid_cap_s_parm(struct file *file, void *priv,
1808 struct v4l2_streamparm *parm)
1810 struct vivid_dev *dev = video_drvdata(file);
1811 unsigned ival_sz = 2 * (VIVID_WEBCAM_SIZES - dev->webcam_size_idx);
1812 struct v4l2_fract tpf;
1815 if (parm->type != (dev->multiplanar ?
1816 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE :
1817 V4L2_BUF_TYPE_VIDEO_CAPTURE))
1819 if (!vivid_is_webcam(dev))
1820 return vivid_vid_cap_g_parm(file, priv, parm);
1822 tpf = parm->parm.capture.timeperframe;
1824 if (tpf.denominator == 0)
1825 tpf = webcam_intervals[ival_sz - 1];
1826 for (i = 0; i < ival_sz; i++)
1827 if (FRACT_CMP(tpf, >=, webcam_intervals[i]))
1831 dev->webcam_ival_idx = i;
1832 tpf = webcam_intervals[dev->webcam_ival_idx];
1833 tpf = FRACT_CMP(tpf, <, tpf_min) ? tpf_min : tpf;
1834 tpf = FRACT_CMP(tpf, >, tpf_max) ? tpf_max : tpf;
1836 /* resync the thread's timings */
1837 dev->cap_seq_resync = true;
1838 dev->timeperframe_vid_cap = tpf;
1839 parm->parm.capture.timeperframe = tpf;
1840 parm->parm.capture.readbuffers = 1;