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>
30 #include "vivid-core.h"
31 #include "vivid-vid-common.h"
32 #include "vivid-kthread-cap.h"
33 #include "vivid-vid-cap.h"
35 /* timeperframe: min/max and default */
36 static const struct v4l2_fract
37 tpf_min
= {.numerator
= 1, .denominator
= FPS_MAX
},
38 tpf_max
= {.numerator
= FPS_MAX
, .denominator
= 1},
39 tpf_default
= {.numerator
= 1, .denominator
= 30};
41 static const struct vivid_fmt formats_ovl
[] = {
43 .fourcc
= V4L2_PIX_FMT_RGB565
, /* gggbbbbb rrrrrggg */
44 .vdownsampling
= { 1 },
50 .fourcc
= V4L2_PIX_FMT_XRGB555
, /* gggbbbbb arrrrrgg */
51 .vdownsampling
= { 1 },
57 .fourcc
= V4L2_PIX_FMT_ARGB555
, /* gggbbbbb arrrrrgg */
58 .vdownsampling
= { 1 },
65 /* The number of discrete webcam framesizes */
66 #define VIVID_WEBCAM_SIZES 4
67 /* The number of discrete webcam frameintervals */
68 #define VIVID_WEBCAM_IVALS (VIVID_WEBCAM_SIZES * 2)
70 /* Sizes must be in increasing order */
71 static const struct v4l2_frmsize_discrete webcam_sizes
[VIVID_WEBCAM_SIZES
] = {
79 * Intervals must be in increasing order and there must be twice as many
80 * elements in this array as there are in webcam_sizes.
82 static const struct v4l2_fract webcam_intervals
[VIVID_WEBCAM_IVALS
] = {
93 static const struct v4l2_discrete_probe webcam_probe
= {
98 static int vid_cap_queue_setup(struct vb2_queue
*vq
, const void *parg
,
99 unsigned *nbuffers
, unsigned *nplanes
,
100 unsigned sizes
[], void *alloc_ctxs
[])
102 const struct v4l2_format
*fmt
= parg
;
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;
126 const struct v4l2_pix_format_mplane
*mp
;
127 struct v4l2_format mp_fmt
;
128 const struct vivid_fmt
*vfmt
;
130 if (!V4L2_TYPE_IS_MULTIPLANAR(fmt
->type
)) {
131 fmt_sp2mp(fmt
, &mp_fmt
);
134 mp
= &fmt
->fmt
.pix_mp
;
136 * Check if the number of planes in the specified format match
137 * the number of buffers in the current format. You can't mix that.
139 if (mp
->num_planes
!= buffers
)
141 vfmt
= vivid_get_format(dev
, mp
->pixelformat
);
142 for (p
= 0; p
< buffers
; p
++) {
143 sizes
[p
] = mp
->plane_fmt
[p
].sizeimage
;
144 if (sizes
[p
] < tpg_g_line_width(&dev
->tpg
, p
) * h
+
145 vfmt
->data_offset
[p
])
149 for (p
= 0; p
< buffers
; p
++)
150 sizes
[p
] = tpg_g_line_width(&dev
->tpg
, p
) * h
+
151 dev
->fmt_cap
->data_offset
[p
];
154 if (vq
->num_buffers
+ *nbuffers
< 2)
155 *nbuffers
= 2 - vq
->num_buffers
;
160 * videobuf2-vmalloc allocator is context-less so no need to set
164 dprintk(dev
, 1, "%s: count=%d\n", __func__
, *nbuffers
);
165 for (p
= 0; p
< buffers
; p
++)
166 dprintk(dev
, 1, "%s: size[%u]=%u\n", __func__
, p
, sizes
[p
]);
171 static int vid_cap_buf_prepare(struct vb2_buffer
*vb
)
173 struct vivid_dev
*dev
= vb2_get_drv_priv(vb
->vb2_queue
);
175 unsigned buffers
= tpg_g_buffers(&dev
->tpg
);
178 dprintk(dev
, 1, "%s\n", __func__
);
180 if (WARN_ON(NULL
== dev
->fmt_cap
))
183 if (dev
->buf_prepare_error
) {
185 * Error injection: test what happens if buf_prepare() returns
188 dev
->buf_prepare_error
= false;
191 for (p
= 0; p
< buffers
; p
++) {
192 size
= tpg_g_line_width(&dev
->tpg
, p
) * dev
->fmt_cap_rect
.height
+
193 dev
->fmt_cap
->data_offset
[p
];
195 if (vb2_plane_size(vb
, p
) < size
) {
196 dprintk(dev
, 1, "%s data will not fit into plane %u (%lu < %lu)\n",
197 __func__
, p
, vb2_plane_size(vb
, p
), size
);
201 vb2_set_plane_payload(vb
, p
, size
);
202 vb
->planes
[p
].data_offset
= dev
->fmt_cap
->data_offset
[p
];
208 static void vid_cap_buf_finish(struct vb2_buffer
*vb
)
210 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
211 struct vivid_dev
*dev
= vb2_get_drv_priv(vb
->vb2_queue
);
212 struct v4l2_timecode
*tc
= &vbuf
->timecode
;
214 unsigned seq
= vbuf
->sequence
;
216 if (!vivid_is_sdtv_cap(dev
))
220 * Set the timecode. Rarely used, so it is interesting to
223 vbuf
->flags
|= V4L2_BUF_FLAG_TIMECODE
;
224 if (dev
->std_cap
& V4L2_STD_525_60
)
226 tc
->type
= (fps
== 30) ? V4L2_TC_TYPE_30FPS
: V4L2_TC_TYPE_25FPS
;
228 tc
->frames
= seq
% fps
;
229 tc
->seconds
= (seq
/ fps
) % 60;
230 tc
->minutes
= (seq
/ (60 * fps
)) % 60;
231 tc
->hours
= (seq
/ (60 * 60 * fps
)) % 24;
234 static void vid_cap_buf_queue(struct vb2_buffer
*vb
)
236 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
237 struct vivid_dev
*dev
= vb2_get_drv_priv(vb
->vb2_queue
);
238 struct vivid_buffer
*buf
= container_of(vbuf
, struct vivid_buffer
, vb
);
240 dprintk(dev
, 1, "%s\n", __func__
);
242 spin_lock(&dev
->slock
);
243 list_add_tail(&buf
->list
, &dev
->vid_cap_active
);
244 spin_unlock(&dev
->slock
);
247 static int vid_cap_start_streaming(struct vb2_queue
*vq
, unsigned count
)
249 struct vivid_dev
*dev
= vb2_get_drv_priv(vq
);
253 if (vb2_is_streaming(&dev
->vb_vid_out_q
))
254 dev
->can_loop_video
= vivid_vid_can_loop(dev
);
256 if (dev
->kthread_vid_cap
)
259 dev
->vid_cap_seq_count
= 0;
260 dprintk(dev
, 1, "%s\n", __func__
);
261 for (i
= 0; i
< VIDEO_MAX_FRAME
; i
++)
262 dev
->must_blank
[i
] = tpg_g_perc_fill(&dev
->tpg
) < 100;
263 if (dev
->start_streaming_error
) {
264 dev
->start_streaming_error
= false;
267 err
= vivid_start_generating_vid_cap(dev
, &dev
->vid_cap_streaming
);
270 struct vivid_buffer
*buf
, *tmp
;
272 list_for_each_entry_safe(buf
, tmp
, &dev
->vid_cap_active
, list
) {
273 list_del(&buf
->list
);
274 vb2_buffer_done(&buf
->vb
.vb2_buf
,
275 VB2_BUF_STATE_QUEUED
);
281 /* abort streaming and wait for last buffer */
282 static void vid_cap_stop_streaming(struct vb2_queue
*vq
)
284 struct vivid_dev
*dev
= vb2_get_drv_priv(vq
);
286 dprintk(dev
, 1, "%s\n", __func__
);
287 vivid_stop_generating_vid_cap(dev
, &dev
->vid_cap_streaming
);
288 dev
->can_loop_video
= false;
291 const struct vb2_ops vivid_vid_cap_qops
= {
292 .queue_setup
= vid_cap_queue_setup
,
293 .buf_prepare
= vid_cap_buf_prepare
,
294 .buf_finish
= vid_cap_buf_finish
,
295 .buf_queue
= vid_cap_buf_queue
,
296 .start_streaming
= vid_cap_start_streaming
,
297 .stop_streaming
= vid_cap_stop_streaming
,
298 .wait_prepare
= vb2_ops_wait_prepare
,
299 .wait_finish
= vb2_ops_wait_finish
,
303 * Determine the 'picture' quality based on the current TV frequency: either
304 * COLOR for a good 'signal', GRAY (grayscale picture) for a slightly off
305 * signal or NOISE for no signal.
307 void vivid_update_quality(struct vivid_dev
*dev
)
309 unsigned freq_modulus
;
311 if (dev
->loop_video
&& (vivid_is_svid_cap(dev
) || vivid_is_hdmi_cap(dev
))) {
313 * The 'noise' will only be replaced by the actual video
314 * if the output video matches the input video settings.
316 tpg_s_quality(&dev
->tpg
, TPG_QUAL_NOISE
, 0);
319 if (vivid_is_hdmi_cap(dev
) && VIVID_INVALID_SIGNAL(dev
->dv_timings_signal_mode
)) {
320 tpg_s_quality(&dev
->tpg
, TPG_QUAL_NOISE
, 0);
323 if (vivid_is_sdtv_cap(dev
) && VIVID_INVALID_SIGNAL(dev
->std_signal_mode
)) {
324 tpg_s_quality(&dev
->tpg
, TPG_QUAL_NOISE
, 0);
327 if (!vivid_is_tv_cap(dev
)) {
328 tpg_s_quality(&dev
->tpg
, TPG_QUAL_COLOR
, 0);
333 * There is a fake channel every 6 MHz at 49.25, 55.25, etc.
334 * From +/- 0.25 MHz around the channel there is color, and from
335 * +/- 1 MHz there is grayscale (chroma is lost).
336 * Everywhere else it is just noise.
338 freq_modulus
= (dev
->tv_freq
- 676 /* (43.25-1) * 16 */) % (6 * 16);
339 if (freq_modulus
> 2 * 16) {
340 tpg_s_quality(&dev
->tpg
, TPG_QUAL_NOISE
,
341 next_pseudo_random32(dev
->tv_freq
^ 0x55) & 0x3f);
344 if (freq_modulus
< 12 /*0.75 * 16*/ || freq_modulus
> 20 /*1.25 * 16*/)
345 tpg_s_quality(&dev
->tpg
, TPG_QUAL_GRAY
, 0);
347 tpg_s_quality(&dev
->tpg
, TPG_QUAL_COLOR
, 0);
351 * Get the current picture quality and the associated afc value.
353 static enum tpg_quality
vivid_get_quality(struct vivid_dev
*dev
, s32
*afc
)
355 unsigned freq_modulus
;
359 if (tpg_g_quality(&dev
->tpg
) == TPG_QUAL_COLOR
||
360 tpg_g_quality(&dev
->tpg
) == TPG_QUAL_NOISE
)
361 return tpg_g_quality(&dev
->tpg
);
364 * There is a fake channel every 6 MHz at 49.25, 55.25, etc.
365 * From +/- 0.25 MHz around the channel there is color, and from
366 * +/- 1 MHz there is grayscale (chroma is lost).
367 * Everywhere else it is just gray.
369 freq_modulus
= (dev
->tv_freq
- 676 /* (43.25-1) * 16 */) % (6 * 16);
371 *afc
= freq_modulus
- 1 * 16;
372 return TPG_QUAL_GRAY
;
375 enum tpg_video_aspect
vivid_get_video_aspect(const struct vivid_dev
*dev
)
377 if (vivid_is_sdtv_cap(dev
))
378 return dev
->std_aspect_ratio
;
380 if (vivid_is_hdmi_cap(dev
))
381 return dev
->dv_timings_aspect_ratio
;
383 return TPG_VIDEO_ASPECT_IMAGE
;
386 static enum tpg_pixel_aspect
vivid_get_pixel_aspect(const struct vivid_dev
*dev
)
388 if (vivid_is_sdtv_cap(dev
))
389 return (dev
->std_cap
& V4L2_STD_525_60
) ?
390 TPG_PIXEL_ASPECT_NTSC
: TPG_PIXEL_ASPECT_PAL
;
392 if (vivid_is_hdmi_cap(dev
) &&
393 dev
->src_rect
.width
== 720 && dev
->src_rect
.height
<= 576)
394 return dev
->src_rect
.height
== 480 ?
395 TPG_PIXEL_ASPECT_NTSC
: TPG_PIXEL_ASPECT_PAL
;
397 return TPG_PIXEL_ASPECT_SQUARE
;
401 * Called whenever the format has to be reset which can occur when
402 * changing inputs, standard, timings, etc.
404 void vivid_update_format_cap(struct vivid_dev
*dev
, bool keep_controls
)
406 struct v4l2_bt_timings
*bt
= &dev
->dv_timings_cap
.bt
;
409 switch (dev
->input_type
[dev
->input
]) {
412 dev
->src_rect
.width
= webcam_sizes
[dev
->webcam_size_idx
].width
;
413 dev
->src_rect
.height
= webcam_sizes
[dev
->webcam_size_idx
].height
;
414 dev
->timeperframe_vid_cap
= webcam_intervals
[dev
->webcam_ival_idx
];
415 dev
->field_cap
= V4L2_FIELD_NONE
;
416 tpg_s_rgb_range(&dev
->tpg
, V4L2_DV_RGB_RANGE_AUTO
);
420 dev
->field_cap
= dev
->tv_field_cap
;
421 dev
->src_rect
.width
= 720;
422 if (dev
->std_cap
& V4L2_STD_525_60
) {
423 dev
->src_rect
.height
= 480;
424 dev
->timeperframe_vid_cap
= (struct v4l2_fract
) { 1001, 30000 };
425 dev
->service_set_cap
= V4L2_SLICED_CAPTION_525
;
427 dev
->src_rect
.height
= 576;
428 dev
->timeperframe_vid_cap
= (struct v4l2_fract
) { 1000, 25000 };
429 dev
->service_set_cap
= V4L2_SLICED_WSS_625
| V4L2_SLICED_TELETEXT_B
;
431 tpg_s_rgb_range(&dev
->tpg
, V4L2_DV_RGB_RANGE_AUTO
);
434 dev
->src_rect
.width
= bt
->width
;
435 dev
->src_rect
.height
= bt
->height
;
436 size
= V4L2_DV_BT_FRAME_WIDTH(bt
) * V4L2_DV_BT_FRAME_HEIGHT(bt
);
437 dev
->timeperframe_vid_cap
= (struct v4l2_fract
) {
438 size
/ 100, (u32
)bt
->pixelclock
/ 100
441 dev
->field_cap
= V4L2_FIELD_ALTERNATE
;
443 dev
->field_cap
= V4L2_FIELD_NONE
;
446 * We can be called from within s_ctrl, in that case we can't
447 * set/get controls. Luckily we don't need to in that case.
449 if (keep_controls
|| !dev
->colorspace
)
451 if (bt
->flags
& V4L2_DV_FL_IS_CE_VIDEO
) {
452 if (bt
->width
== 720 && bt
->height
<= 576)
453 v4l2_ctrl_s_ctrl(dev
->colorspace
, VIVID_CS_170M
);
455 v4l2_ctrl_s_ctrl(dev
->colorspace
, VIVID_CS_709
);
456 v4l2_ctrl_s_ctrl(dev
->real_rgb_range_cap
, 1);
458 v4l2_ctrl_s_ctrl(dev
->colorspace
, VIVID_CS_SRGB
);
459 v4l2_ctrl_s_ctrl(dev
->real_rgb_range_cap
, 0);
461 tpg_s_rgb_range(&dev
->tpg
, v4l2_ctrl_g_ctrl(dev
->rgb_range_cap
));
464 vivid_update_quality(dev
);
465 tpg_reset_source(&dev
->tpg
, dev
->src_rect
.width
, dev
->src_rect
.height
, dev
->field_cap
);
466 dev
->crop_cap
= dev
->src_rect
;
467 dev
->crop_bounds_cap
= dev
->src_rect
;
468 dev
->compose_cap
= dev
->crop_cap
;
469 if (V4L2_FIELD_HAS_T_OR_B(dev
->field_cap
))
470 dev
->compose_cap
.height
/= 2;
471 dev
->fmt_cap_rect
= dev
->compose_cap
;
472 tpg_s_video_aspect(&dev
->tpg
, vivid_get_video_aspect(dev
));
473 tpg_s_pixel_aspect(&dev
->tpg
, vivid_get_pixel_aspect(dev
));
474 tpg_update_mv_step(&dev
->tpg
);
477 /* Map the field to something that is valid for the current input */
478 static enum v4l2_field
vivid_field_cap(struct vivid_dev
*dev
, enum v4l2_field field
)
480 if (vivid_is_sdtv_cap(dev
)) {
482 case V4L2_FIELD_INTERLACED_TB
:
483 case V4L2_FIELD_INTERLACED_BT
:
484 case V4L2_FIELD_SEQ_TB
:
485 case V4L2_FIELD_SEQ_BT
:
487 case V4L2_FIELD_BOTTOM
:
488 case V4L2_FIELD_ALTERNATE
:
490 case V4L2_FIELD_INTERLACED
:
492 return V4L2_FIELD_INTERLACED
;
495 if (vivid_is_hdmi_cap(dev
))
496 return dev
->dv_timings_cap
.bt
.interlaced
? V4L2_FIELD_ALTERNATE
:
498 return V4L2_FIELD_NONE
;
501 static unsigned vivid_colorspace_cap(struct vivid_dev
*dev
)
503 if (!dev
->loop_video
|| vivid_is_webcam(dev
) || vivid_is_tv_cap(dev
))
504 return tpg_g_colorspace(&dev
->tpg
);
505 return dev
->colorspace_out
;
508 static unsigned vivid_xfer_func_cap(struct vivid_dev
*dev
)
510 if (!dev
->loop_video
|| vivid_is_webcam(dev
) || vivid_is_tv_cap(dev
))
511 return tpg_g_xfer_func(&dev
->tpg
);
512 return dev
->xfer_func_out
;
515 static unsigned vivid_ycbcr_enc_cap(struct vivid_dev
*dev
)
517 if (!dev
->loop_video
|| vivid_is_webcam(dev
) || vivid_is_tv_cap(dev
))
518 return tpg_g_ycbcr_enc(&dev
->tpg
);
519 return dev
->ycbcr_enc_out
;
522 static unsigned vivid_quantization_cap(struct vivid_dev
*dev
)
524 if (!dev
->loop_video
|| vivid_is_webcam(dev
) || vivid_is_tv_cap(dev
))
525 return tpg_g_quantization(&dev
->tpg
);
526 return dev
->quantization_out
;
529 int vivid_g_fmt_vid_cap(struct file
*file
, void *priv
,
530 struct v4l2_format
*f
)
532 struct vivid_dev
*dev
= video_drvdata(file
);
533 struct v4l2_pix_format_mplane
*mp
= &f
->fmt
.pix_mp
;
536 mp
->width
= dev
->fmt_cap_rect
.width
;
537 mp
->height
= dev
->fmt_cap_rect
.height
;
538 mp
->field
= dev
->field_cap
;
539 mp
->pixelformat
= dev
->fmt_cap
->fourcc
;
540 mp
->colorspace
= vivid_colorspace_cap(dev
);
541 mp
->xfer_func
= vivid_xfer_func_cap(dev
);
542 mp
->ycbcr_enc
= vivid_ycbcr_enc_cap(dev
);
543 mp
->quantization
= vivid_quantization_cap(dev
);
544 mp
->num_planes
= dev
->fmt_cap
->buffers
;
545 for (p
= 0; p
< mp
->num_planes
; p
++) {
546 mp
->plane_fmt
[p
].bytesperline
= tpg_g_bytesperline(&dev
->tpg
, p
);
547 mp
->plane_fmt
[p
].sizeimage
=
548 tpg_g_line_width(&dev
->tpg
, p
) * mp
->height
+
549 dev
->fmt_cap
->data_offset
[p
];
554 int vivid_try_fmt_vid_cap(struct file
*file
, void *priv
,
555 struct v4l2_format
*f
)
557 struct v4l2_pix_format_mplane
*mp
= &f
->fmt
.pix_mp
;
558 struct v4l2_plane_pix_format
*pfmt
= mp
->plane_fmt
;
559 struct vivid_dev
*dev
= video_drvdata(file
);
560 const struct vivid_fmt
*fmt
;
561 unsigned bytesperline
, max_bpl
;
566 fmt
= vivid_get_format(dev
, mp
->pixelformat
);
568 dprintk(dev
, 1, "Fourcc format (0x%08x) unknown.\n",
570 mp
->pixelformat
= V4L2_PIX_FMT_YUYV
;
571 fmt
= vivid_get_format(dev
, mp
->pixelformat
);
574 mp
->field
= vivid_field_cap(dev
, mp
->field
);
575 if (vivid_is_webcam(dev
)) {
576 const struct v4l2_frmsize_discrete
*sz
=
577 v4l2_find_nearest_format(&webcam_probe
, mp
->width
, mp
->height
);
581 } else if (vivid_is_sdtv_cap(dev
)) {
583 h
= (dev
->std_cap
& V4L2_STD_525_60
) ? 480 : 576;
585 w
= dev
->src_rect
.width
;
586 h
= dev
->src_rect
.height
;
588 if (V4L2_FIELD_HAS_T_OR_B(mp
->field
))
590 if (vivid_is_webcam(dev
) ||
591 (!dev
->has_scaler_cap
&& !dev
->has_crop_cap
&& !dev
->has_compose_cap
)) {
593 mp
->height
= h
/ factor
;
595 struct v4l2_rect r
= { 0, 0, mp
->width
, mp
->height
* factor
};
597 rect_set_min_size(&r
, &vivid_min_rect
);
598 rect_set_max_size(&r
, &vivid_max_rect
);
599 if (dev
->has_scaler_cap
&& !dev
->has_compose_cap
) {
600 struct v4l2_rect max_r
= { 0, 0, MAX_ZOOM
* w
, MAX_ZOOM
* h
};
602 rect_set_max_size(&r
, &max_r
);
603 } else if (!dev
->has_scaler_cap
&& dev
->has_crop_cap
&& !dev
->has_compose_cap
) {
604 rect_set_max_size(&r
, &dev
->src_rect
);
605 } else if (!dev
->has_scaler_cap
&& !dev
->has_crop_cap
) {
606 rect_set_min_size(&r
, &dev
->src_rect
);
609 mp
->height
= r
.height
/ factor
;
612 /* This driver supports custom bytesperline values */
614 mp
->num_planes
= fmt
->buffers
;
615 for (p
= 0; p
< mp
->num_planes
; p
++) {
616 /* Calculate the minimum supported bytesperline value */
617 bytesperline
= (mp
->width
* fmt
->bit_depth
[p
]) >> 3;
618 /* Calculate the maximum supported bytesperline value */
619 max_bpl
= (MAX_ZOOM
* MAX_WIDTH
* fmt
->bit_depth
[p
]) >> 3;
621 if (pfmt
[p
].bytesperline
> max_bpl
)
622 pfmt
[p
].bytesperline
= max_bpl
;
623 if (pfmt
[p
].bytesperline
< bytesperline
)
624 pfmt
[p
].bytesperline
= bytesperline
;
625 pfmt
[p
].sizeimage
= tpg_calc_line_width(&dev
->tpg
, p
, pfmt
[p
].bytesperline
) *
626 mp
->height
+ fmt
->data_offset
[p
];
627 memset(pfmt
[p
].reserved
, 0, sizeof(pfmt
[p
].reserved
));
629 mp
->colorspace
= vivid_colorspace_cap(dev
);
630 mp
->ycbcr_enc
= vivid_ycbcr_enc_cap(dev
);
631 mp
->xfer_func
= vivid_xfer_func_cap(dev
);
632 mp
->quantization
= vivid_quantization_cap(dev
);
633 memset(mp
->reserved
, 0, sizeof(mp
->reserved
));
637 int vivid_s_fmt_vid_cap(struct file
*file
, void *priv
,
638 struct v4l2_format
*f
)
640 struct v4l2_pix_format_mplane
*mp
= &f
->fmt
.pix_mp
;
641 struct vivid_dev
*dev
= video_drvdata(file
);
642 struct v4l2_rect
*crop
= &dev
->crop_cap
;
643 struct v4l2_rect
*compose
= &dev
->compose_cap
;
644 struct vb2_queue
*q
= &dev
->vb_vid_cap_q
;
645 int ret
= vivid_try_fmt_vid_cap(file
, priv
, f
);
653 if (vb2_is_busy(q
)) {
654 dprintk(dev
, 1, "%s device busy\n", __func__
);
658 if (dev
->overlay_cap_owner
&& dev
->fb_cap
.fmt
.pixelformat
!= mp
->pixelformat
) {
659 dprintk(dev
, 1, "overlay is active, can't change pixelformat\n");
663 dev
->fmt_cap
= vivid_get_format(dev
, mp
->pixelformat
);
664 if (V4L2_FIELD_HAS_T_OR_B(mp
->field
))
667 /* Note: the webcam input doesn't support scaling, cropping or composing */
669 if (!vivid_is_webcam(dev
) &&
670 (dev
->has_scaler_cap
|| dev
->has_crop_cap
|| dev
->has_compose_cap
)) {
671 struct v4l2_rect r
= { 0, 0, mp
->width
, mp
->height
};
673 if (dev
->has_scaler_cap
) {
674 if (dev
->has_compose_cap
)
675 rect_map_inside(compose
, &r
);
678 if (dev
->has_crop_cap
&& !dev
->has_compose_cap
) {
679 struct v4l2_rect min_r
= {
682 factor
* r
.height
/ MAX_ZOOM
684 struct v4l2_rect max_r
= {
687 factor
* r
.height
* MAX_ZOOM
690 rect_set_min_size(crop
, &min_r
);
691 rect_set_max_size(crop
, &max_r
);
692 rect_map_inside(crop
, &dev
->crop_bounds_cap
);
693 } else if (dev
->has_crop_cap
) {
694 struct v4l2_rect min_r
= {
696 compose
->width
/ MAX_ZOOM
,
697 factor
* compose
->height
/ MAX_ZOOM
699 struct v4l2_rect max_r
= {
701 compose
->width
* MAX_ZOOM
,
702 factor
* compose
->height
* MAX_ZOOM
705 rect_set_min_size(crop
, &min_r
);
706 rect_set_max_size(crop
, &max_r
);
707 rect_map_inside(crop
, &dev
->crop_bounds_cap
);
709 } else if (dev
->has_crop_cap
&& !dev
->has_compose_cap
) {
711 rect_set_size_to(crop
, &r
);
712 rect_map_inside(crop
, &dev
->crop_bounds_cap
);
715 rect_set_size_to(compose
, &r
);
716 } else if (!dev
->has_crop_cap
) {
717 rect_map_inside(compose
, &r
);
720 rect_set_max_size(crop
, &r
);
721 rect_map_inside(crop
, &dev
->crop_bounds_cap
);
722 compose
->top
*= factor
;
723 compose
->height
*= factor
;
724 rect_set_size_to(compose
, crop
);
725 rect_map_inside(compose
, &r
);
726 compose
->top
/= factor
;
727 compose
->height
/= factor
;
729 } else if (vivid_is_webcam(dev
)) {
730 /* Guaranteed to be a match */
731 for (i
= 0; i
< ARRAY_SIZE(webcam_sizes
); i
++)
732 if (webcam_sizes
[i
].width
== mp
->width
&&
733 webcam_sizes
[i
].height
== mp
->height
)
735 dev
->webcam_size_idx
= i
;
736 if (dev
->webcam_ival_idx
>= 2 * (VIVID_WEBCAM_SIZES
- i
))
737 dev
->webcam_ival_idx
= 2 * (VIVID_WEBCAM_SIZES
- i
) - 1;
738 vivid_update_format_cap(dev
, false);
740 struct v4l2_rect r
= { 0, 0, mp
->width
, mp
->height
};
742 rect_set_size_to(compose
, &r
);
744 rect_set_size_to(crop
, &r
);
747 dev
->fmt_cap_rect
.width
= mp
->width
;
748 dev
->fmt_cap_rect
.height
= mp
->height
;
749 tpg_s_buf_height(&dev
->tpg
, mp
->height
);
750 tpg_s_fourcc(&dev
->tpg
, dev
->fmt_cap
->fourcc
);
751 for (p
= 0; p
< tpg_g_buffers(&dev
->tpg
); p
++)
752 tpg_s_bytesperline(&dev
->tpg
, p
, mp
->plane_fmt
[p
].bytesperline
);
753 dev
->field_cap
= mp
->field
;
754 if (dev
->field_cap
== V4L2_FIELD_ALTERNATE
)
755 tpg_s_field(&dev
->tpg
, V4L2_FIELD_TOP
, true);
757 tpg_s_field(&dev
->tpg
, dev
->field_cap
, false);
758 tpg_s_crop_compose(&dev
->tpg
, &dev
->crop_cap
, &dev
->compose_cap
);
759 if (vivid_is_sdtv_cap(dev
))
760 dev
->tv_field_cap
= mp
->field
;
761 tpg_update_mv_step(&dev
->tpg
);
765 int vidioc_g_fmt_vid_cap_mplane(struct file
*file
, void *priv
,
766 struct v4l2_format
*f
)
768 struct vivid_dev
*dev
= video_drvdata(file
);
770 if (!dev
->multiplanar
)
772 return vivid_g_fmt_vid_cap(file
, priv
, f
);
775 int vidioc_try_fmt_vid_cap_mplane(struct file
*file
, void *priv
,
776 struct v4l2_format
*f
)
778 struct vivid_dev
*dev
= video_drvdata(file
);
780 if (!dev
->multiplanar
)
782 return vivid_try_fmt_vid_cap(file
, priv
, f
);
785 int vidioc_s_fmt_vid_cap_mplane(struct file
*file
, void *priv
,
786 struct v4l2_format
*f
)
788 struct vivid_dev
*dev
= video_drvdata(file
);
790 if (!dev
->multiplanar
)
792 return vivid_s_fmt_vid_cap(file
, priv
, f
);
795 int vidioc_g_fmt_vid_cap(struct file
*file
, void *priv
,
796 struct v4l2_format
*f
)
798 struct vivid_dev
*dev
= video_drvdata(file
);
800 if (dev
->multiplanar
)
802 return fmt_sp2mp_func(file
, priv
, f
, vivid_g_fmt_vid_cap
);
805 int vidioc_try_fmt_vid_cap(struct file
*file
, void *priv
,
806 struct v4l2_format
*f
)
808 struct vivid_dev
*dev
= video_drvdata(file
);
810 if (dev
->multiplanar
)
812 return fmt_sp2mp_func(file
, priv
, f
, vivid_try_fmt_vid_cap
);
815 int vidioc_s_fmt_vid_cap(struct file
*file
, void *priv
,
816 struct v4l2_format
*f
)
818 struct vivid_dev
*dev
= video_drvdata(file
);
820 if (dev
->multiplanar
)
822 return fmt_sp2mp_func(file
, priv
, f
, vivid_s_fmt_vid_cap
);
825 int vivid_vid_cap_g_selection(struct file
*file
, void *priv
,
826 struct v4l2_selection
*sel
)
828 struct vivid_dev
*dev
= video_drvdata(file
);
830 if (!dev
->has_crop_cap
&& !dev
->has_compose_cap
)
832 if (sel
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
834 if (vivid_is_webcam(dev
))
837 sel
->r
.left
= sel
->r
.top
= 0;
838 switch (sel
->target
) {
839 case V4L2_SEL_TGT_CROP
:
840 if (!dev
->has_crop_cap
)
842 sel
->r
= dev
->crop_cap
;
844 case V4L2_SEL_TGT_CROP_DEFAULT
:
845 case V4L2_SEL_TGT_CROP_BOUNDS
:
846 if (!dev
->has_crop_cap
)
848 sel
->r
= dev
->src_rect
;
850 case V4L2_SEL_TGT_COMPOSE_BOUNDS
:
851 if (!dev
->has_compose_cap
)
853 sel
->r
= vivid_max_rect
;
855 case V4L2_SEL_TGT_COMPOSE
:
856 if (!dev
->has_compose_cap
)
858 sel
->r
= dev
->compose_cap
;
860 case V4L2_SEL_TGT_COMPOSE_DEFAULT
:
861 if (!dev
->has_compose_cap
)
863 sel
->r
= dev
->fmt_cap_rect
;
871 int vivid_vid_cap_s_selection(struct file
*file
, void *fh
, struct v4l2_selection
*s
)
873 struct vivid_dev
*dev
= video_drvdata(file
);
874 struct v4l2_rect
*crop
= &dev
->crop_cap
;
875 struct v4l2_rect
*compose
= &dev
->compose_cap
;
876 unsigned factor
= V4L2_FIELD_HAS_T_OR_B(dev
->field_cap
) ? 2 : 1;
879 if (!dev
->has_crop_cap
&& !dev
->has_compose_cap
)
881 if (s
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
883 if (vivid_is_webcam(dev
))
887 case V4L2_SEL_TGT_CROP
:
888 if (!dev
->has_crop_cap
)
890 ret
= vivid_vid_adjust_sel(s
->flags
, &s
->r
);
893 rect_set_min_size(&s
->r
, &vivid_min_rect
);
894 rect_set_max_size(&s
->r
, &dev
->src_rect
);
895 rect_map_inside(&s
->r
, &dev
->crop_bounds_cap
);
897 s
->r
.height
/= factor
;
898 if (dev
->has_scaler_cap
) {
899 struct v4l2_rect fmt
= dev
->fmt_cap_rect
;
900 struct v4l2_rect max_rect
= {
902 s
->r
.width
* MAX_ZOOM
,
903 s
->r
.height
* MAX_ZOOM
905 struct v4l2_rect min_rect
= {
907 s
->r
.width
/ MAX_ZOOM
,
908 s
->r
.height
/ MAX_ZOOM
911 rect_set_min_size(&fmt
, &min_rect
);
912 if (!dev
->has_compose_cap
)
913 rect_set_max_size(&fmt
, &max_rect
);
914 if (!rect_same_size(&dev
->fmt_cap_rect
, &fmt
) &&
915 vb2_is_busy(&dev
->vb_vid_cap_q
))
917 if (dev
->has_compose_cap
) {
918 rect_set_min_size(compose
, &min_rect
);
919 rect_set_max_size(compose
, &max_rect
);
921 dev
->fmt_cap_rect
= fmt
;
922 tpg_s_buf_height(&dev
->tpg
, fmt
.height
);
923 } else if (dev
->has_compose_cap
) {
924 struct v4l2_rect fmt
= dev
->fmt_cap_rect
;
926 rect_set_min_size(&fmt
, &s
->r
);
927 if (!rect_same_size(&dev
->fmt_cap_rect
, &fmt
) &&
928 vb2_is_busy(&dev
->vb_vid_cap_q
))
930 dev
->fmt_cap_rect
= fmt
;
931 tpg_s_buf_height(&dev
->tpg
, fmt
.height
);
932 rect_set_size_to(compose
, &s
->r
);
933 rect_map_inside(compose
, &dev
->fmt_cap_rect
);
935 if (!rect_same_size(&s
->r
, &dev
->fmt_cap_rect
) &&
936 vb2_is_busy(&dev
->vb_vid_cap_q
))
938 rect_set_size_to(&dev
->fmt_cap_rect
, &s
->r
);
939 rect_set_size_to(compose
, &s
->r
);
940 rect_map_inside(compose
, &dev
->fmt_cap_rect
);
941 tpg_s_buf_height(&dev
->tpg
, dev
->fmt_cap_rect
.height
);
944 s
->r
.height
*= factor
;
947 case V4L2_SEL_TGT_COMPOSE
:
948 if (!dev
->has_compose_cap
)
950 ret
= vivid_vid_adjust_sel(s
->flags
, &s
->r
);
953 rect_set_min_size(&s
->r
, &vivid_min_rect
);
954 rect_set_max_size(&s
->r
, &dev
->fmt_cap_rect
);
955 if (dev
->has_scaler_cap
) {
956 struct v4l2_rect max_rect
= {
958 dev
->src_rect
.width
* MAX_ZOOM
,
959 (dev
->src_rect
.height
/ factor
) * MAX_ZOOM
962 rect_set_max_size(&s
->r
, &max_rect
);
963 if (dev
->has_crop_cap
) {
964 struct v4l2_rect min_rect
= {
966 s
->r
.width
/ MAX_ZOOM
,
967 (s
->r
.height
* factor
) / MAX_ZOOM
969 struct v4l2_rect max_rect
= {
971 s
->r
.width
* MAX_ZOOM
,
972 (s
->r
.height
* factor
) * MAX_ZOOM
975 rect_set_min_size(crop
, &min_rect
);
976 rect_set_max_size(crop
, &max_rect
);
977 rect_map_inside(crop
, &dev
->crop_bounds_cap
);
979 } else if (dev
->has_crop_cap
) {
981 s
->r
.height
*= factor
;
982 rect_set_max_size(&s
->r
, &dev
->src_rect
);
983 rect_set_size_to(crop
, &s
->r
);
984 rect_map_inside(crop
, &dev
->crop_bounds_cap
);
986 s
->r
.height
/= factor
;
988 rect_set_size_to(&s
->r
, &dev
->src_rect
);
989 s
->r
.height
/= factor
;
991 rect_map_inside(&s
->r
, &dev
->fmt_cap_rect
);
992 if (dev
->bitmap_cap
&& (compose
->width
!= s
->r
.width
||
993 compose
->height
!= s
->r
.height
)) {
994 kfree(dev
->bitmap_cap
);
995 dev
->bitmap_cap
= NULL
;
1003 tpg_s_crop_compose(&dev
->tpg
, crop
, compose
);
1007 int vivid_vid_cap_cropcap(struct file
*file
, void *priv
,
1008 struct v4l2_cropcap
*cap
)
1010 struct vivid_dev
*dev
= video_drvdata(file
);
1012 if (cap
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1015 switch (vivid_get_pixel_aspect(dev
)) {
1016 case TPG_PIXEL_ASPECT_NTSC
:
1017 cap
->pixelaspect
.numerator
= 11;
1018 cap
->pixelaspect
.denominator
= 10;
1020 case TPG_PIXEL_ASPECT_PAL
:
1021 cap
->pixelaspect
.numerator
= 54;
1022 cap
->pixelaspect
.denominator
= 59;
1024 case TPG_PIXEL_ASPECT_SQUARE
:
1025 cap
->pixelaspect
.numerator
= 1;
1026 cap
->pixelaspect
.denominator
= 1;
1032 int vidioc_enum_fmt_vid_overlay(struct file
*file
, void *priv
,
1033 struct v4l2_fmtdesc
*f
)
1035 struct vivid_dev
*dev
= video_drvdata(file
);
1036 const struct vivid_fmt
*fmt
;
1038 if (dev
->multiplanar
)
1041 if (f
->index
>= ARRAY_SIZE(formats_ovl
))
1044 fmt
= &formats_ovl
[f
->index
];
1046 f
->pixelformat
= fmt
->fourcc
;
1050 int vidioc_g_fmt_vid_overlay(struct file
*file
, void *priv
,
1051 struct v4l2_format
*f
)
1053 struct vivid_dev
*dev
= video_drvdata(file
);
1054 const struct v4l2_rect
*compose
= &dev
->compose_cap
;
1055 struct v4l2_window
*win
= &f
->fmt
.win
;
1056 unsigned clipcount
= win
->clipcount
;
1058 if (dev
->multiplanar
)
1061 win
->w
.top
= dev
->overlay_cap_top
;
1062 win
->w
.left
= dev
->overlay_cap_left
;
1063 win
->w
.width
= compose
->width
;
1064 win
->w
.height
= compose
->height
;
1065 win
->field
= dev
->overlay_cap_field
;
1066 win
->clipcount
= dev
->clipcount_cap
;
1067 if (clipcount
> dev
->clipcount_cap
)
1068 clipcount
= dev
->clipcount_cap
;
1069 if (dev
->bitmap_cap
== NULL
)
1071 else if (win
->bitmap
) {
1072 if (copy_to_user(win
->bitmap
, dev
->bitmap_cap
,
1073 ((compose
->width
+ 7) / 8) * compose
->height
))
1076 if (clipcount
&& win
->clips
) {
1077 if (copy_to_user(win
->clips
, dev
->clips_cap
,
1078 clipcount
* sizeof(dev
->clips_cap
[0])))
1084 int vidioc_try_fmt_vid_overlay(struct file
*file
, void *priv
,
1085 struct v4l2_format
*f
)
1087 struct vivid_dev
*dev
= video_drvdata(file
);
1088 const struct v4l2_rect
*compose
= &dev
->compose_cap
;
1089 struct v4l2_window
*win
= &f
->fmt
.win
;
1092 if (dev
->multiplanar
)
1095 win
->w
.left
= clamp_t(int, win
->w
.left
,
1096 -dev
->fb_cap
.fmt
.width
, dev
->fb_cap
.fmt
.width
);
1097 win
->w
.top
= clamp_t(int, win
->w
.top
,
1098 -dev
->fb_cap
.fmt
.height
, dev
->fb_cap
.fmt
.height
);
1099 win
->w
.width
= compose
->width
;
1100 win
->w
.height
= compose
->height
;
1101 if (win
->field
!= V4L2_FIELD_BOTTOM
&& win
->field
!= V4L2_FIELD_TOP
)
1102 win
->field
= V4L2_FIELD_ANY
;
1104 win
->global_alpha
= 0;
1105 if (win
->clipcount
&& !win
->clips
)
1107 if (win
->clipcount
> MAX_CLIPS
)
1108 win
->clipcount
= MAX_CLIPS
;
1109 if (win
->clipcount
) {
1110 if (copy_from_user(dev
->try_clips_cap
, win
->clips
,
1111 win
->clipcount
* sizeof(dev
->clips_cap
[0])))
1113 for (i
= 0; i
< win
->clipcount
; i
++) {
1114 struct v4l2_rect
*r
= &dev
->try_clips_cap
[i
].c
;
1116 r
->top
= clamp_t(s32
, r
->top
, 0, dev
->fb_cap
.fmt
.height
- 1);
1117 r
->height
= clamp_t(s32
, r
->height
, 1, dev
->fb_cap
.fmt
.height
- r
->top
);
1118 r
->left
= clamp_t(u32
, r
->left
, 0, dev
->fb_cap
.fmt
.width
- 1);
1119 r
->width
= clamp_t(u32
, r
->width
, 1, dev
->fb_cap
.fmt
.width
- r
->left
);
1122 * Yeah, so sue me, it's an O(n^2) algorithm. But n is a small
1123 * number and it's typically a one-time deal.
1125 for (i
= 0; i
< win
->clipcount
- 1; i
++) {
1126 struct v4l2_rect
*r1
= &dev
->try_clips_cap
[i
].c
;
1128 for (j
= i
+ 1; j
< win
->clipcount
; j
++) {
1129 struct v4l2_rect
*r2
= &dev
->try_clips_cap
[j
].c
;
1131 if (rect_overlap(r1
, r2
))
1135 if (copy_to_user(win
->clips
, dev
->try_clips_cap
,
1136 win
->clipcount
* sizeof(dev
->clips_cap
[0])))
1142 int vidioc_s_fmt_vid_overlay(struct file
*file
, void *priv
,
1143 struct v4l2_format
*f
)
1145 struct vivid_dev
*dev
= video_drvdata(file
);
1146 const struct v4l2_rect
*compose
= &dev
->compose_cap
;
1147 struct v4l2_window
*win
= &f
->fmt
.win
;
1148 int ret
= vidioc_try_fmt_vid_overlay(file
, priv
, f
);
1149 unsigned bitmap_size
= ((compose
->width
+ 7) / 8) * compose
->height
;
1150 unsigned clips_size
= win
->clipcount
* sizeof(dev
->clips_cap
[0]);
1151 void *new_bitmap
= NULL
;
1157 new_bitmap
= vzalloc(bitmap_size
);
1159 if (new_bitmap
== NULL
)
1161 if (copy_from_user(new_bitmap
, win
->bitmap
, bitmap_size
)) {
1167 dev
->overlay_cap_top
= win
->w
.top
;
1168 dev
->overlay_cap_left
= win
->w
.left
;
1169 dev
->overlay_cap_field
= win
->field
;
1170 vfree(dev
->bitmap_cap
);
1171 dev
->bitmap_cap
= new_bitmap
;
1172 dev
->clipcount_cap
= win
->clipcount
;
1173 if (dev
->clipcount_cap
)
1174 memcpy(dev
->clips_cap
, dev
->try_clips_cap
, clips_size
);
1178 int vivid_vid_cap_overlay(struct file
*file
, void *fh
, unsigned i
)
1180 struct vivid_dev
*dev
= video_drvdata(file
);
1182 if (dev
->multiplanar
)
1185 if (i
&& dev
->fb_vbase_cap
== NULL
)
1188 if (i
&& dev
->fb_cap
.fmt
.pixelformat
!= dev
->fmt_cap
->fourcc
) {
1189 dprintk(dev
, 1, "mismatch between overlay and video capture pixelformats\n");
1193 if (dev
->overlay_cap_owner
&& dev
->overlay_cap_owner
!= fh
)
1195 dev
->overlay_cap_owner
= i
? fh
: NULL
;
1199 int vivid_vid_cap_g_fbuf(struct file
*file
, void *fh
,
1200 struct v4l2_framebuffer
*a
)
1202 struct vivid_dev
*dev
= video_drvdata(file
);
1204 if (dev
->multiplanar
)
1208 a
->capability
= V4L2_FBUF_CAP_BITMAP_CLIPPING
|
1209 V4L2_FBUF_CAP_LIST_CLIPPING
;
1210 a
->flags
= V4L2_FBUF_FLAG_PRIMARY
;
1211 a
->fmt
.field
= V4L2_FIELD_NONE
;
1212 a
->fmt
.colorspace
= V4L2_COLORSPACE_SRGB
;
1217 int vivid_vid_cap_s_fbuf(struct file
*file
, void *fh
,
1218 const struct v4l2_framebuffer
*a
)
1220 struct vivid_dev
*dev
= video_drvdata(file
);
1221 const struct vivid_fmt
*fmt
;
1223 if (dev
->multiplanar
)
1226 if (!capable(CAP_SYS_ADMIN
) && !capable(CAP_SYS_RAWIO
))
1229 if (dev
->overlay_cap_owner
)
1232 if (a
->base
== NULL
) {
1233 dev
->fb_cap
.base
= NULL
;
1234 dev
->fb_vbase_cap
= NULL
;
1238 if (a
->fmt
.width
< 48 || a
->fmt
.height
< 32)
1240 fmt
= vivid_get_format(dev
, a
->fmt
.pixelformat
);
1241 if (!fmt
|| !fmt
->can_do_overlay
)
1243 if (a
->fmt
.bytesperline
< (a
->fmt
.width
* fmt
->bit_depth
[0]) / 8)
1245 if (a
->fmt
.height
* a
->fmt
.bytesperline
< a
->fmt
.sizeimage
)
1248 dev
->fb_vbase_cap
= phys_to_virt((unsigned long)a
->base
);
1250 dev
->overlay_cap_left
= clamp_t(int, dev
->overlay_cap_left
,
1251 -dev
->fb_cap
.fmt
.width
, dev
->fb_cap
.fmt
.width
);
1252 dev
->overlay_cap_top
= clamp_t(int, dev
->overlay_cap_top
,
1253 -dev
->fb_cap
.fmt
.height
, dev
->fb_cap
.fmt
.height
);
1257 static const struct v4l2_audio vivid_audio_inputs
[] = {
1258 { 0, "TV", V4L2_AUDCAP_STEREO
},
1259 { 1, "Line-In", V4L2_AUDCAP_STEREO
},
1262 int vidioc_enum_input(struct file
*file
, void *priv
,
1263 struct v4l2_input
*inp
)
1265 struct vivid_dev
*dev
= video_drvdata(file
);
1267 if (inp
->index
>= dev
->num_inputs
)
1270 inp
->type
= V4L2_INPUT_TYPE_CAMERA
;
1271 switch (dev
->input_type
[inp
->index
]) {
1273 snprintf(inp
->name
, sizeof(inp
->name
), "Webcam %u",
1274 dev
->input_name_counter
[inp
->index
]);
1275 inp
->capabilities
= 0;
1278 snprintf(inp
->name
, sizeof(inp
->name
), "TV %u",
1279 dev
->input_name_counter
[inp
->index
]);
1280 inp
->type
= V4L2_INPUT_TYPE_TUNER
;
1281 inp
->std
= V4L2_STD_ALL
;
1282 if (dev
->has_audio_inputs
)
1283 inp
->audioset
= (1 << ARRAY_SIZE(vivid_audio_inputs
)) - 1;
1284 inp
->capabilities
= V4L2_IN_CAP_STD
;
1287 snprintf(inp
->name
, sizeof(inp
->name
), "S-Video %u",
1288 dev
->input_name_counter
[inp
->index
]);
1289 inp
->std
= V4L2_STD_ALL
;
1290 if (dev
->has_audio_inputs
)
1291 inp
->audioset
= (1 << ARRAY_SIZE(vivid_audio_inputs
)) - 1;
1292 inp
->capabilities
= V4L2_IN_CAP_STD
;
1295 snprintf(inp
->name
, sizeof(inp
->name
), "HDMI %u",
1296 dev
->input_name_counter
[inp
->index
]);
1297 inp
->capabilities
= V4L2_IN_CAP_DV_TIMINGS
;
1298 if (dev
->edid_blocks
== 0 ||
1299 dev
->dv_timings_signal_mode
== NO_SIGNAL
)
1300 inp
->status
|= V4L2_IN_ST_NO_SIGNAL
;
1301 else if (dev
->dv_timings_signal_mode
== NO_LOCK
||
1302 dev
->dv_timings_signal_mode
== OUT_OF_RANGE
)
1303 inp
->status
|= V4L2_IN_ST_NO_H_LOCK
;
1306 if (dev
->sensor_hflip
)
1307 inp
->status
|= V4L2_IN_ST_HFLIP
;
1308 if (dev
->sensor_vflip
)
1309 inp
->status
|= V4L2_IN_ST_VFLIP
;
1310 if (dev
->input
== inp
->index
&& vivid_is_sdtv_cap(dev
)) {
1311 if (dev
->std_signal_mode
== NO_SIGNAL
) {
1312 inp
->status
|= V4L2_IN_ST_NO_SIGNAL
;
1313 } else if (dev
->std_signal_mode
== NO_LOCK
) {
1314 inp
->status
|= V4L2_IN_ST_NO_H_LOCK
;
1315 } else if (vivid_is_tv_cap(dev
)) {
1316 switch (tpg_g_quality(&dev
->tpg
)) {
1318 inp
->status
|= V4L2_IN_ST_COLOR_KILL
;
1320 case TPG_QUAL_NOISE
:
1321 inp
->status
|= V4L2_IN_ST_NO_H_LOCK
;
1331 int vidioc_g_input(struct file
*file
, void *priv
, unsigned *i
)
1333 struct vivid_dev
*dev
= video_drvdata(file
);
1339 int vidioc_s_input(struct file
*file
, void *priv
, unsigned i
)
1341 struct vivid_dev
*dev
= video_drvdata(file
);
1342 struct v4l2_bt_timings
*bt
= &dev
->dv_timings_cap
.bt
;
1343 unsigned brightness
;
1345 if (i
>= dev
->num_inputs
)
1348 if (i
== dev
->input
)
1351 if (vb2_is_busy(&dev
->vb_vid_cap_q
) || vb2_is_busy(&dev
->vb_vbi_cap_q
))
1355 dev
->vid_cap_dev
.tvnorms
= 0;
1356 if (dev
->input_type
[i
] == TV
|| dev
->input_type
[i
] == SVID
) {
1357 dev
->tv_audio_input
= (dev
->input_type
[i
] == TV
) ? 0 : 1;
1358 dev
->vid_cap_dev
.tvnorms
= V4L2_STD_ALL
;
1360 dev
->vbi_cap_dev
.tvnorms
= dev
->vid_cap_dev
.tvnorms
;
1361 vivid_update_format_cap(dev
, false);
1363 if (dev
->colorspace
) {
1364 switch (dev
->input_type
[i
]) {
1366 v4l2_ctrl_s_ctrl(dev
->colorspace
, VIVID_CS_SRGB
);
1370 v4l2_ctrl_s_ctrl(dev
->colorspace
, VIVID_CS_170M
);
1373 if (bt
->flags
& V4L2_DV_FL_IS_CE_VIDEO
) {
1374 if (dev
->src_rect
.width
== 720 && dev
->src_rect
.height
<= 576)
1375 v4l2_ctrl_s_ctrl(dev
->colorspace
, VIVID_CS_170M
);
1377 v4l2_ctrl_s_ctrl(dev
->colorspace
, VIVID_CS_709
);
1379 v4l2_ctrl_s_ctrl(dev
->colorspace
, VIVID_CS_SRGB
);
1386 * Modify the brightness range depending on the input.
1387 * This makes it easy to use vivid to test if applications can
1388 * handle control range modifications and is also how this is
1389 * typically used in practice as different inputs may be hooked
1390 * up to different receivers with different control ranges.
1392 brightness
= 128 * i
+ dev
->input_brightness
[i
];
1393 v4l2_ctrl_modify_range(dev
->brightness
,
1394 128 * i
, 255 + 128 * i
, 1, 128 + 128 * i
);
1395 v4l2_ctrl_s_ctrl(dev
->brightness
, brightness
);
1399 int vidioc_enumaudio(struct file
*file
, void *fh
, struct v4l2_audio
*vin
)
1401 if (vin
->index
>= ARRAY_SIZE(vivid_audio_inputs
))
1403 *vin
= vivid_audio_inputs
[vin
->index
];
1407 int vidioc_g_audio(struct file
*file
, void *fh
, struct v4l2_audio
*vin
)
1409 struct vivid_dev
*dev
= video_drvdata(file
);
1411 if (!vivid_is_sdtv_cap(dev
))
1413 *vin
= vivid_audio_inputs
[dev
->tv_audio_input
];
1417 int vidioc_s_audio(struct file
*file
, void *fh
, const struct v4l2_audio
*vin
)
1419 struct vivid_dev
*dev
= video_drvdata(file
);
1421 if (!vivid_is_sdtv_cap(dev
))
1423 if (vin
->index
>= ARRAY_SIZE(vivid_audio_inputs
))
1425 dev
->tv_audio_input
= vin
->index
;
1429 int vivid_video_g_frequency(struct file
*file
, void *fh
, struct v4l2_frequency
*vf
)
1431 struct vivid_dev
*dev
= video_drvdata(file
);
1435 vf
->frequency
= dev
->tv_freq
;
1439 int vivid_video_s_frequency(struct file
*file
, void *fh
, const struct v4l2_frequency
*vf
)
1441 struct vivid_dev
*dev
= video_drvdata(file
);
1445 dev
->tv_freq
= clamp_t(unsigned, vf
->frequency
, MIN_TV_FREQ
, MAX_TV_FREQ
);
1446 if (vivid_is_tv_cap(dev
))
1447 vivid_update_quality(dev
);
1451 int vivid_video_s_tuner(struct file
*file
, void *fh
, const struct v4l2_tuner
*vt
)
1453 struct vivid_dev
*dev
= video_drvdata(file
);
1457 if (vt
->audmode
> V4L2_TUNER_MODE_LANG1_LANG2
)
1459 dev
->tv_audmode
= vt
->audmode
;
1463 int vivid_video_g_tuner(struct file
*file
, void *fh
, struct v4l2_tuner
*vt
)
1465 struct vivid_dev
*dev
= video_drvdata(file
);
1466 enum tpg_quality qual
;
1471 vt
->capability
= V4L2_TUNER_CAP_NORM
| V4L2_TUNER_CAP_STEREO
|
1472 V4L2_TUNER_CAP_LANG1
| V4L2_TUNER_CAP_LANG2
;
1473 vt
->audmode
= dev
->tv_audmode
;
1474 vt
->rangelow
= MIN_TV_FREQ
;
1475 vt
->rangehigh
= MAX_TV_FREQ
;
1476 qual
= vivid_get_quality(dev
, &vt
->afc
);
1477 if (qual
== TPG_QUAL_COLOR
)
1478 vt
->signal
= 0xffff;
1479 else if (qual
== TPG_QUAL_GRAY
)
1480 vt
->signal
= 0x8000;
1483 if (qual
== TPG_QUAL_NOISE
) {
1485 } else if (qual
== TPG_QUAL_GRAY
) {
1486 vt
->rxsubchans
= V4L2_TUNER_SUB_MONO
;
1488 unsigned channel_nr
= dev
->tv_freq
/ (6 * 16);
1489 unsigned options
= (dev
->std_cap
& V4L2_STD_NTSC_M
) ? 4 : 3;
1491 switch (channel_nr
% options
) {
1493 vt
->rxsubchans
= V4L2_TUNER_SUB_MONO
;
1496 vt
->rxsubchans
= V4L2_TUNER_SUB_STEREO
;
1499 if (dev
->std_cap
& V4L2_STD_NTSC_M
)
1500 vt
->rxsubchans
= V4L2_TUNER_SUB_MONO
| V4L2_TUNER_SUB_SAP
;
1502 vt
->rxsubchans
= V4L2_TUNER_SUB_LANG1
| V4L2_TUNER_SUB_LANG2
;
1505 vt
->rxsubchans
= V4L2_TUNER_SUB_STEREO
| V4L2_TUNER_SUB_SAP
;
1509 strlcpy(vt
->name
, "TV Tuner", sizeof(vt
->name
));
1513 /* Must remain in sync with the vivid_ctrl_standard_strings array */
1514 const v4l2_std_id vivid_standard
[] = {
1519 V4L2_STD_PAL_BG
| V4L2_STD_PAL_H
,
1526 V4L2_STD_SECAM_B
| V4L2_STD_SECAM_G
| V4L2_STD_SECAM_H
,
1533 /* Must remain in sync with the vivid_standard array */
1534 const char * const vivid_ctrl_standard_strings
[] = {
1553 int vidioc_querystd(struct file
*file
, void *priv
, v4l2_std_id
*id
)
1555 struct vivid_dev
*dev
= video_drvdata(file
);
1557 if (!vivid_is_sdtv_cap(dev
))
1559 if (dev
->std_signal_mode
== NO_SIGNAL
||
1560 dev
->std_signal_mode
== NO_LOCK
) {
1561 *id
= V4L2_STD_UNKNOWN
;
1564 if (vivid_is_tv_cap(dev
) && tpg_g_quality(&dev
->tpg
) == TPG_QUAL_NOISE
) {
1565 *id
= V4L2_STD_UNKNOWN
;
1566 } else if (dev
->std_signal_mode
== CURRENT_STD
) {
1568 } else if (dev
->std_signal_mode
== SELECTED_STD
) {
1569 *id
= dev
->query_std
;
1571 *id
= vivid_standard
[dev
->query_std_last
];
1572 dev
->query_std_last
= (dev
->query_std_last
+ 1) % ARRAY_SIZE(vivid_standard
);
1578 int vivid_vid_cap_s_std(struct file
*file
, void *priv
, v4l2_std_id id
)
1580 struct vivid_dev
*dev
= video_drvdata(file
);
1582 if (!vivid_is_sdtv_cap(dev
))
1584 if (dev
->std_cap
== id
)
1586 if (vb2_is_busy(&dev
->vb_vid_cap_q
) || vb2_is_busy(&dev
->vb_vbi_cap_q
))
1589 vivid_update_format_cap(dev
, false);
1593 static void find_aspect_ratio(u32 width
, u32 height
,
1594 u32
*num
, u32
*denom
)
1596 if (!(height
% 3) && ((height
* 4 / 3) == width
)) {
1599 } else if (!(height
% 9) && ((height
* 16 / 9) == width
)) {
1602 } else if (!(height
% 10) && ((height
* 16 / 10) == width
)) {
1605 } else if (!(height
% 4) && ((height
* 5 / 4) == width
)) {
1608 } else if (!(height
% 9) && ((height
* 15 / 9) == width
)) {
1611 } else { /* default to 16:9 */
1617 static bool valid_cvt_gtf_timings(struct v4l2_dv_timings
*timings
)
1619 struct v4l2_bt_timings
*bt
= &timings
->bt
;
1624 if (!v4l2_valid_dv_timings(timings
, &vivid_dv_timings_cap
,
1628 total_h_pixel
= V4L2_DV_BT_FRAME_WIDTH(bt
);
1629 total_v_lines
= V4L2_DV_BT_FRAME_HEIGHT(bt
);
1631 h_freq
= (u32
)bt
->pixelclock
/ total_h_pixel
;
1633 if (bt
->standards
== 0 || (bt
->standards
& V4L2_DV_BT_STD_CVT
)) {
1634 if (v4l2_detect_cvt(total_v_lines
, h_freq
, bt
->vsync
, bt
->width
,
1635 bt
->polarities
, bt
->interlaced
, timings
))
1639 if (bt
->standards
== 0 || (bt
->standards
& V4L2_DV_BT_STD_GTF
)) {
1640 struct v4l2_fract aspect_ratio
;
1642 find_aspect_ratio(bt
->width
, bt
->height
,
1643 &aspect_ratio
.numerator
,
1644 &aspect_ratio
.denominator
);
1645 if (v4l2_detect_gtf(total_v_lines
, h_freq
, bt
->vsync
,
1646 bt
->polarities
, bt
->interlaced
,
1647 aspect_ratio
, timings
))
1653 int vivid_vid_cap_s_dv_timings(struct file
*file
, void *_fh
,
1654 struct v4l2_dv_timings
*timings
)
1656 struct vivid_dev
*dev
= video_drvdata(file
);
1658 if (!vivid_is_hdmi_cap(dev
))
1660 if (!v4l2_find_dv_timings_cap(timings
, &vivid_dv_timings_cap
,
1662 !valid_cvt_gtf_timings(timings
))
1665 if (v4l2_match_dv_timings(timings
, &dev
->dv_timings_cap
, 0))
1667 if (vb2_is_busy(&dev
->vb_vid_cap_q
))
1670 dev
->dv_timings_cap
= *timings
;
1671 vivid_update_format_cap(dev
, false);
1675 int vidioc_query_dv_timings(struct file
*file
, void *_fh
,
1676 struct v4l2_dv_timings
*timings
)
1678 struct vivid_dev
*dev
= video_drvdata(file
);
1680 if (!vivid_is_hdmi_cap(dev
))
1682 if (dev
->dv_timings_signal_mode
== NO_SIGNAL
||
1683 dev
->edid_blocks
== 0)
1685 if (dev
->dv_timings_signal_mode
== NO_LOCK
)
1687 if (dev
->dv_timings_signal_mode
== OUT_OF_RANGE
) {
1688 timings
->bt
.pixelclock
= vivid_dv_timings_cap
.bt
.max_pixelclock
* 2;
1691 if (dev
->dv_timings_signal_mode
== CURRENT_DV_TIMINGS
) {
1692 *timings
= dev
->dv_timings_cap
;
1693 } else if (dev
->dv_timings_signal_mode
== SELECTED_DV_TIMINGS
) {
1694 *timings
= v4l2_dv_timings_presets
[dev
->query_dv_timings
];
1696 *timings
= v4l2_dv_timings_presets
[dev
->query_dv_timings_last
];
1697 dev
->query_dv_timings_last
= (dev
->query_dv_timings_last
+ 1) %
1698 dev
->query_dv_timings_size
;
1703 int vidioc_s_edid(struct file
*file
, void *_fh
,
1704 struct v4l2_edid
*edid
)
1706 struct vivid_dev
*dev
= video_drvdata(file
);
1708 memset(edid
->reserved
, 0, sizeof(edid
->reserved
));
1709 if (edid
->pad
>= dev
->num_inputs
)
1711 if (dev
->input_type
[edid
->pad
] != HDMI
|| edid
->start_block
)
1713 if (edid
->blocks
== 0) {
1714 dev
->edid_blocks
= 0;
1717 if (edid
->blocks
> dev
->edid_max_blocks
) {
1718 edid
->blocks
= dev
->edid_max_blocks
;
1721 dev
->edid_blocks
= edid
->blocks
;
1722 memcpy(dev
->edid
, edid
->edid
, edid
->blocks
* 128);
1726 int vidioc_enum_framesizes(struct file
*file
, void *fh
,
1727 struct v4l2_frmsizeenum
*fsize
)
1729 struct vivid_dev
*dev
= video_drvdata(file
);
1731 if (!vivid_is_webcam(dev
) && !dev
->has_scaler_cap
)
1733 if (vivid_get_format(dev
, fsize
->pixel_format
) == NULL
)
1735 if (vivid_is_webcam(dev
)) {
1736 if (fsize
->index
>= ARRAY_SIZE(webcam_sizes
))
1738 fsize
->type
= V4L2_FRMSIZE_TYPE_DISCRETE
;
1739 fsize
->discrete
= webcam_sizes
[fsize
->index
];
1744 fsize
->type
= V4L2_FRMSIZE_TYPE_STEPWISE
;
1745 fsize
->stepwise
.min_width
= MIN_WIDTH
;
1746 fsize
->stepwise
.max_width
= MAX_WIDTH
* MAX_ZOOM
;
1747 fsize
->stepwise
.step_width
= 2;
1748 fsize
->stepwise
.min_height
= MIN_HEIGHT
;
1749 fsize
->stepwise
.max_height
= MAX_HEIGHT
* MAX_ZOOM
;
1750 fsize
->stepwise
.step_height
= 2;
1754 /* timeperframe is arbitrary and continuous */
1755 int vidioc_enum_frameintervals(struct file
*file
, void *priv
,
1756 struct v4l2_frmivalenum
*fival
)
1758 struct vivid_dev
*dev
= video_drvdata(file
);
1759 const struct vivid_fmt
*fmt
;
1762 fmt
= vivid_get_format(dev
, fival
->pixel_format
);
1766 if (!vivid_is_webcam(dev
)) {
1769 if (fival
->width
< MIN_WIDTH
|| fival
->width
> MAX_WIDTH
* MAX_ZOOM
)
1771 if (fival
->height
< MIN_HEIGHT
|| fival
->height
> MAX_HEIGHT
* MAX_ZOOM
)
1773 fival
->type
= V4L2_FRMIVAL_TYPE_DISCRETE
;
1774 fival
->discrete
= dev
->timeperframe_vid_cap
;
1778 for (i
= 0; i
< ARRAY_SIZE(webcam_sizes
); i
++)
1779 if (fival
->width
== webcam_sizes
[i
].width
&&
1780 fival
->height
== webcam_sizes
[i
].height
)
1782 if (i
== ARRAY_SIZE(webcam_sizes
))
1784 if (fival
->index
>= 2 * (VIVID_WEBCAM_SIZES
- i
))
1786 fival
->type
= V4L2_FRMIVAL_TYPE_DISCRETE
;
1787 fival
->discrete
= webcam_intervals
[fival
->index
];
1791 int vivid_vid_cap_g_parm(struct file
*file
, void *priv
,
1792 struct v4l2_streamparm
*parm
)
1794 struct vivid_dev
*dev
= video_drvdata(file
);
1796 if (parm
->type
!= (dev
->multiplanar
?
1797 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
:
1798 V4L2_BUF_TYPE_VIDEO_CAPTURE
))
1801 parm
->parm
.capture
.capability
= V4L2_CAP_TIMEPERFRAME
;
1802 parm
->parm
.capture
.timeperframe
= dev
->timeperframe_vid_cap
;
1803 parm
->parm
.capture
.readbuffers
= 1;
1807 #define FRACT_CMP(a, OP, b) \
1808 ((u64)(a).numerator * (b).denominator OP (u64)(b).numerator * (a).denominator)
1810 int vivid_vid_cap_s_parm(struct file
*file
, void *priv
,
1811 struct v4l2_streamparm
*parm
)
1813 struct vivid_dev
*dev
= video_drvdata(file
);
1814 unsigned ival_sz
= 2 * (VIVID_WEBCAM_SIZES
- dev
->webcam_size_idx
);
1815 struct v4l2_fract tpf
;
1818 if (parm
->type
!= (dev
->multiplanar
?
1819 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
:
1820 V4L2_BUF_TYPE_VIDEO_CAPTURE
))
1822 if (!vivid_is_webcam(dev
))
1823 return vivid_vid_cap_g_parm(file
, priv
, parm
);
1825 tpf
= parm
->parm
.capture
.timeperframe
;
1827 if (tpf
.denominator
== 0)
1828 tpf
= webcam_intervals
[ival_sz
- 1];
1829 for (i
= 0; i
< ival_sz
; i
++)
1830 if (FRACT_CMP(tpf
, >=, webcam_intervals
[i
]))
1834 dev
->webcam_ival_idx
= i
;
1835 tpf
= webcam_intervals
[dev
->webcam_ival_idx
];
1836 tpf
= FRACT_CMP(tpf
, <, tpf_min
) ? tpf_min
: tpf
;
1837 tpf
= FRACT_CMP(tpf
, >, tpf_max
) ? tpf_max
: tpf
;
1839 /* resync the thread's timings */
1840 dev
->cap_seq_resync
= true;
1841 dev
->timeperframe_vid_cap
= tpf
;
1842 parm
->parm
.capture
.timeperframe
= tpf
;
1843 parm
->parm
.capture
.readbuffers
= 1;