2 * vivid-vid-out.c - video output support functions.
4 * Copyright 2014 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
6 * This program is free software; you may redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
10 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
11 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
12 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
13 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
14 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
15 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
16 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 #include <linux/errno.h>
21 #include <linux/kernel.h>
22 #include <linux/sched.h>
23 #include <linux/videodev2.h>
24 #include <linux/v4l2-dv-timings.h>
25 #include <media/v4l2-common.h>
26 #include <media/v4l2-event.h>
27 #include <media/v4l2-dv-timings.h>
29 #include "vivid-core.h"
30 #include "vivid-vid-common.h"
31 #include "vivid-kthread-out.h"
32 #include "vivid-vid-out.h"
34 static int vid_out_queue_setup(struct vb2_queue *vq,
35 unsigned *nbuffers, unsigned *nplanes,
36 unsigned sizes[], void *alloc_ctxs[])
38 struct vivid_dev *dev = vb2_get_drv_priv(vq);
39 const struct vivid_fmt *vfmt = dev->fmt_out;
40 unsigned planes = vfmt->buffers;
41 unsigned h = dev->fmt_out_rect.height;
42 unsigned size = dev->bytesperline_out[0] * h;
45 for (p = vfmt->buffers; p < vfmt->planes; p++)
46 size += dev->bytesperline_out[p] * h / vfmt->vdownsampling[p];
48 if (dev->field_out == V4L2_FIELD_ALTERNATE) {
50 * You cannot use write() with FIELD_ALTERNATE since the field
51 * information (TOP/BOTTOM) cannot be passed to the kernel.
53 if (vb2_fileio_is_active(vq))
57 if (dev->queue_setup_error) {
59 * Error injection: test what happens if queue_setup() returns
62 dev->queue_setup_error = false;
68 * Check if the number of requested planes match
69 * the number of planes in the current format. You can't mix that.
71 if (*nplanes != planes)
75 for (p = 1; p < planes; p++) {
76 if (sizes[p] < dev->bytesperline_out[p] * h)
80 for (p = 0; p < planes; p++)
81 sizes[p] = p ? dev->bytesperline_out[p] * h : size;
84 if (vq->num_buffers + *nbuffers < 2)
85 *nbuffers = 2 - vq->num_buffers;
90 * videobuf2-vmalloc allocator is context-less so no need to set
94 dprintk(dev, 1, "%s: count=%d\n", __func__, *nbuffers);
95 for (p = 0; p < planes; p++)
96 dprintk(dev, 1, "%s: size[%u]=%u\n", __func__, p, sizes[p]);
100 static int vid_out_buf_prepare(struct vb2_buffer *vb)
102 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
103 struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
108 dprintk(dev, 1, "%s\n", __func__);
110 if (WARN_ON(NULL == dev->fmt_out))
113 planes = dev->fmt_out->planes;
115 if (dev->buf_prepare_error) {
117 * Error injection: test what happens if buf_prepare() returns
120 dev->buf_prepare_error = false;
124 if (dev->field_out != V4L2_FIELD_ALTERNATE)
125 vbuf->field = dev->field_out;
126 else if (vbuf->field != V4L2_FIELD_TOP &&
127 vbuf->field != V4L2_FIELD_BOTTOM)
130 for (p = 0; p < planes; p++) {
131 size = dev->bytesperline_out[p] * dev->fmt_out_rect.height +
132 vb->planes[p].data_offset;
134 if (vb2_get_plane_payload(vb, p) < size) {
135 dprintk(dev, 1, "%s the payload is too small for plane %u (%lu < %lu)\n",
136 __func__, p, vb2_get_plane_payload(vb, p), size);
144 static void vid_out_buf_queue(struct vb2_buffer *vb)
146 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
147 struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
148 struct vivid_buffer *buf = container_of(vbuf, struct vivid_buffer, vb);
150 dprintk(dev, 1, "%s\n", __func__);
152 spin_lock(&dev->slock);
153 list_add_tail(&buf->list, &dev->vid_out_active);
154 spin_unlock(&dev->slock);
157 static int vid_out_start_streaming(struct vb2_queue *vq, unsigned count)
159 struct vivid_dev *dev = vb2_get_drv_priv(vq);
162 if (vb2_is_streaming(&dev->vb_vid_cap_q))
163 dev->can_loop_video = vivid_vid_can_loop(dev);
165 if (dev->kthread_vid_out)
168 dev->vid_out_seq_count = 0;
169 dprintk(dev, 1, "%s\n", __func__);
170 if (dev->start_streaming_error) {
171 dev->start_streaming_error = false;
174 err = vivid_start_generating_vid_out(dev, &dev->vid_out_streaming);
177 struct vivid_buffer *buf, *tmp;
179 list_for_each_entry_safe(buf, tmp, &dev->vid_out_active, list) {
180 list_del(&buf->list);
181 vb2_buffer_done(&buf->vb.vb2_buf,
182 VB2_BUF_STATE_QUEUED);
188 /* abort streaming and wait for last buffer */
189 static void vid_out_stop_streaming(struct vb2_queue *vq)
191 struct vivid_dev *dev = vb2_get_drv_priv(vq);
193 dprintk(dev, 1, "%s\n", __func__);
194 vivid_stop_generating_vid_out(dev, &dev->vid_out_streaming);
195 dev->can_loop_video = false;
198 const struct vb2_ops vivid_vid_out_qops = {
199 .queue_setup = vid_out_queue_setup,
200 .buf_prepare = vid_out_buf_prepare,
201 .buf_queue = vid_out_buf_queue,
202 .start_streaming = vid_out_start_streaming,
203 .stop_streaming = vid_out_stop_streaming,
204 .wait_prepare = vb2_ops_wait_prepare,
205 .wait_finish = vb2_ops_wait_finish,
209 * Called whenever the format has to be reset which can occur when
210 * changing outputs, standard, timings, etc.
212 void vivid_update_format_out(struct vivid_dev *dev)
214 struct v4l2_bt_timings *bt = &dev->dv_timings_out.bt;
218 switch (dev->output_type[dev->output]) {
221 dev->field_out = dev->tv_field_out;
222 dev->sink_rect.width = 720;
223 if (dev->std_out & V4L2_STD_525_60) {
224 dev->sink_rect.height = 480;
225 dev->timeperframe_vid_out = (struct v4l2_fract) { 1001, 30000 };
226 dev->service_set_out = V4L2_SLICED_CAPTION_525;
228 dev->sink_rect.height = 576;
229 dev->timeperframe_vid_out = (struct v4l2_fract) { 1000, 25000 };
230 dev->service_set_out = V4L2_SLICED_WSS_625 | V4L2_SLICED_TELETEXT_B;
232 dev->colorspace_out = V4L2_COLORSPACE_SMPTE170M;
235 dev->sink_rect.width = bt->width;
236 dev->sink_rect.height = bt->height;
237 size = V4L2_DV_BT_FRAME_WIDTH(bt) * V4L2_DV_BT_FRAME_HEIGHT(bt);
239 if (can_reduce_fps(bt) && (bt->flags & V4L2_DV_FL_REDUCED_FPS))
240 pixelclock = div_u64(bt->pixelclock * 1000, 1001);
242 pixelclock = bt->pixelclock;
244 dev->timeperframe_vid_out = (struct v4l2_fract) {
245 size / 100, (u32)pixelclock / 100
248 dev->field_out = V4L2_FIELD_ALTERNATE;
250 dev->field_out = V4L2_FIELD_NONE;
251 if (!dev->dvi_d_out && (bt->flags & V4L2_DV_FL_IS_CE_VIDEO)) {
252 if (bt->width == 720 && bt->height <= 576)
253 dev->colorspace_out = V4L2_COLORSPACE_SMPTE170M;
255 dev->colorspace_out = V4L2_COLORSPACE_REC709;
257 dev->colorspace_out = V4L2_COLORSPACE_SRGB;
261 dev->xfer_func_out = V4L2_XFER_FUNC_DEFAULT;
262 dev->ycbcr_enc_out = V4L2_YCBCR_ENC_DEFAULT;
263 dev->quantization_out = V4L2_QUANTIZATION_DEFAULT;
264 dev->compose_out = dev->sink_rect;
265 dev->compose_bounds_out = dev->sink_rect;
266 dev->crop_out = dev->compose_out;
267 if (V4L2_FIELD_HAS_T_OR_B(dev->field_out))
268 dev->crop_out.height /= 2;
269 dev->fmt_out_rect = dev->crop_out;
270 for (p = 0; p < dev->fmt_out->planes; p++)
271 dev->bytesperline_out[p] =
272 (dev->sink_rect.width * dev->fmt_out->bit_depth[p]) / 8;
275 /* Map the field to something that is valid for the current output */
276 static enum v4l2_field vivid_field_out(struct vivid_dev *dev, enum v4l2_field field)
278 if (vivid_is_svid_out(dev)) {
280 case V4L2_FIELD_INTERLACED_TB:
281 case V4L2_FIELD_INTERLACED_BT:
282 case V4L2_FIELD_SEQ_TB:
283 case V4L2_FIELD_SEQ_BT:
284 case V4L2_FIELD_ALTERNATE:
286 case V4L2_FIELD_INTERLACED:
288 return V4L2_FIELD_INTERLACED;
291 if (vivid_is_hdmi_out(dev))
292 return dev->dv_timings_out.bt.interlaced ? V4L2_FIELD_ALTERNATE :
294 return V4L2_FIELD_NONE;
297 static enum tpg_pixel_aspect vivid_get_pixel_aspect(const struct vivid_dev *dev)
299 if (vivid_is_svid_out(dev))
300 return (dev->std_out & V4L2_STD_525_60) ?
301 TPG_PIXEL_ASPECT_NTSC : TPG_PIXEL_ASPECT_PAL;
303 if (vivid_is_hdmi_out(dev) &&
304 dev->sink_rect.width == 720 && dev->sink_rect.height <= 576)
305 return dev->sink_rect.height == 480 ?
306 TPG_PIXEL_ASPECT_NTSC : TPG_PIXEL_ASPECT_PAL;
308 return TPG_PIXEL_ASPECT_SQUARE;
311 int vivid_g_fmt_vid_out(struct file *file, void *priv,
312 struct v4l2_format *f)
314 struct vivid_dev *dev = video_drvdata(file);
315 struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
316 const struct vivid_fmt *fmt = dev->fmt_out;
319 mp->width = dev->fmt_out_rect.width;
320 mp->height = dev->fmt_out_rect.height;
321 mp->field = dev->field_out;
322 mp->pixelformat = fmt->fourcc;
323 mp->colorspace = dev->colorspace_out;
324 mp->xfer_func = dev->xfer_func_out;
325 mp->ycbcr_enc = dev->ycbcr_enc_out;
326 mp->quantization = dev->quantization_out;
327 mp->num_planes = fmt->buffers;
328 for (p = 0; p < mp->num_planes; p++) {
329 mp->plane_fmt[p].bytesperline = dev->bytesperline_out[p];
330 mp->plane_fmt[p].sizeimage =
331 mp->plane_fmt[p].bytesperline * mp->height;
333 for (p = fmt->buffers; p < fmt->planes; p++) {
334 unsigned stride = dev->bytesperline_out[p];
336 mp->plane_fmt[0].sizeimage +=
337 (stride * mp->height) / fmt->vdownsampling[p];
342 int vivid_try_fmt_vid_out(struct file *file, void *priv,
343 struct v4l2_format *f)
345 struct vivid_dev *dev = video_drvdata(file);
346 struct v4l2_bt_timings *bt = &dev->dv_timings_out.bt;
347 struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
348 struct v4l2_plane_pix_format *pfmt = mp->plane_fmt;
349 const struct vivid_fmt *fmt;
350 unsigned bytesperline, max_bpl;
355 fmt = vivid_get_format(dev, mp->pixelformat);
357 dprintk(dev, 1, "Fourcc format (0x%08x) unknown.\n",
359 mp->pixelformat = V4L2_PIX_FMT_YUYV;
360 fmt = vivid_get_format(dev, mp->pixelformat);
363 mp->field = vivid_field_out(dev, mp->field);
364 if (vivid_is_svid_out(dev)) {
366 h = (dev->std_out & V4L2_STD_525_60) ? 480 : 576;
368 w = dev->sink_rect.width;
369 h = dev->sink_rect.height;
371 if (V4L2_FIELD_HAS_T_OR_B(mp->field))
373 if (!dev->has_scaler_out && !dev->has_crop_out && !dev->has_compose_out) {
375 mp->height = h / factor;
377 struct v4l2_rect r = { 0, 0, mp->width, mp->height * factor };
379 rect_set_min_size(&r, &vivid_min_rect);
380 rect_set_max_size(&r, &vivid_max_rect);
381 if (dev->has_scaler_out && !dev->has_crop_out) {
382 struct v4l2_rect max_r = { 0, 0, MAX_ZOOM * w, MAX_ZOOM * h };
384 rect_set_max_size(&r, &max_r);
385 } else if (!dev->has_scaler_out && dev->has_compose_out && !dev->has_crop_out) {
386 rect_set_max_size(&r, &dev->sink_rect);
387 } else if (!dev->has_scaler_out && !dev->has_compose_out) {
388 rect_set_min_size(&r, &dev->sink_rect);
391 mp->height = r.height / factor;
394 /* This driver supports custom bytesperline values */
396 /* Calculate the minimum supported bytesperline value */
397 bytesperline = (mp->width * fmt->bit_depth[0]) >> 3;
398 /* Calculate the maximum supported bytesperline value */
399 max_bpl = (MAX_ZOOM * MAX_WIDTH * fmt->bit_depth[0]) >> 3;
400 mp->num_planes = fmt->buffers;
401 for (p = 0; p < mp->num_planes; p++) {
402 if (pfmt[p].bytesperline > max_bpl)
403 pfmt[p].bytesperline = max_bpl;
404 if (pfmt[p].bytesperline < bytesperline)
405 pfmt[p].bytesperline = bytesperline;
406 pfmt[p].sizeimage = pfmt[p].bytesperline * mp->height;
407 memset(pfmt[p].reserved, 0, sizeof(pfmt[p].reserved));
409 for (p = fmt->buffers; p < fmt->planes; p++)
410 pfmt[0].sizeimage += (pfmt[0].bytesperline * fmt->bit_depth[p]) /
411 (fmt->bit_depth[0] * fmt->vdownsampling[p]);
412 mp->xfer_func = V4L2_XFER_FUNC_DEFAULT;
413 mp->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
414 mp->quantization = V4L2_QUANTIZATION_DEFAULT;
415 if (vivid_is_svid_out(dev)) {
416 mp->colorspace = V4L2_COLORSPACE_SMPTE170M;
417 } else if (dev->dvi_d_out || !(bt->flags & V4L2_DV_FL_IS_CE_VIDEO)) {
418 mp->colorspace = V4L2_COLORSPACE_SRGB;
420 mp->quantization = V4L2_QUANTIZATION_LIM_RANGE;
421 } else if (bt->width == 720 && bt->height <= 576) {
422 mp->colorspace = V4L2_COLORSPACE_SMPTE170M;
423 } else if (mp->colorspace != V4L2_COLORSPACE_SMPTE170M &&
424 mp->colorspace != V4L2_COLORSPACE_REC709 &&
425 mp->colorspace != V4L2_COLORSPACE_ADOBERGB &&
426 mp->colorspace != V4L2_COLORSPACE_BT2020 &&
427 mp->colorspace != V4L2_COLORSPACE_SRGB) {
428 mp->colorspace = V4L2_COLORSPACE_REC709;
430 memset(mp->reserved, 0, sizeof(mp->reserved));
434 int vivid_s_fmt_vid_out(struct file *file, void *priv,
435 struct v4l2_format *f)
437 struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
438 struct vivid_dev *dev = video_drvdata(file);
439 struct v4l2_rect *crop = &dev->crop_out;
440 struct v4l2_rect *compose = &dev->compose_out;
441 struct vb2_queue *q = &dev->vb_vid_out_q;
442 int ret = vivid_try_fmt_vid_out(file, priv, f);
449 if (vb2_is_busy(q) &&
450 (vivid_is_svid_out(dev) ||
451 mp->width != dev->fmt_out_rect.width ||
452 mp->height != dev->fmt_out_rect.height ||
453 mp->pixelformat != dev->fmt_out->fourcc ||
454 mp->field != dev->field_out)) {
455 dprintk(dev, 1, "%s device busy\n", __func__);
460 * Allow for changing the colorspace on the fly. Useful for testing
461 * purposes, and it is something that HDMI transmitters are able
467 dev->fmt_out = vivid_get_format(dev, mp->pixelformat);
468 if (V4L2_FIELD_HAS_T_OR_B(mp->field))
471 if (dev->has_scaler_out || dev->has_crop_out || dev->has_compose_out) {
472 struct v4l2_rect r = { 0, 0, mp->width, mp->height };
474 if (dev->has_scaler_out) {
475 if (dev->has_crop_out)
476 rect_map_inside(crop, &r);
479 if (dev->has_compose_out && !dev->has_crop_out) {
480 struct v4l2_rect min_r = {
483 factor * r.height / MAX_ZOOM
485 struct v4l2_rect max_r = {
488 factor * r.height * MAX_ZOOM
491 rect_set_min_size(compose, &min_r);
492 rect_set_max_size(compose, &max_r);
493 rect_map_inside(compose, &dev->compose_bounds_out);
494 } else if (dev->has_compose_out) {
495 struct v4l2_rect min_r = {
497 crop->width / MAX_ZOOM,
498 factor * crop->height / MAX_ZOOM
500 struct v4l2_rect max_r = {
502 crop->width * MAX_ZOOM,
503 factor * crop->height * MAX_ZOOM
506 rect_set_min_size(compose, &min_r);
507 rect_set_max_size(compose, &max_r);
508 rect_map_inside(compose, &dev->compose_bounds_out);
510 } else if (dev->has_compose_out && !dev->has_crop_out) {
511 rect_set_size_to(crop, &r);
513 rect_set_size_to(compose, &r);
514 rect_map_inside(compose, &dev->compose_bounds_out);
515 } else if (!dev->has_compose_out) {
516 rect_map_inside(crop, &r);
518 rect_set_size_to(compose, &r);
521 rect_set_max_size(compose, &r);
522 rect_map_inside(compose, &dev->compose_bounds_out);
524 crop->height *= factor;
525 rect_set_size_to(crop, compose);
526 rect_map_inside(crop, &r);
528 crop->height /= factor;
531 struct v4l2_rect r = { 0, 0, mp->width, mp->height };
533 rect_set_size_to(crop, &r);
535 rect_set_size_to(compose, &r);
538 dev->fmt_out_rect.width = mp->width;
539 dev->fmt_out_rect.height = mp->height;
540 for (p = 0; p < mp->num_planes; p++)
541 dev->bytesperline_out[p] = mp->plane_fmt[p].bytesperline;
542 for (p = dev->fmt_out->buffers; p < dev->fmt_out->planes; p++)
543 dev->bytesperline_out[p] =
544 (dev->bytesperline_out[0] * dev->fmt_out->bit_depth[p]) /
545 dev->fmt_out->bit_depth[0];
546 dev->field_out = mp->field;
547 if (vivid_is_svid_out(dev))
548 dev->tv_field_out = mp->field;
551 dev->colorspace_out = mp->colorspace;
552 dev->xfer_func_out = mp->xfer_func;
553 dev->ycbcr_enc_out = mp->ycbcr_enc;
554 dev->quantization_out = mp->quantization;
555 if (dev->loop_video) {
556 vivid_send_source_change(dev, SVID);
557 vivid_send_source_change(dev, HDMI);
562 int vidioc_g_fmt_vid_out_mplane(struct file *file, void *priv,
563 struct v4l2_format *f)
565 struct vivid_dev *dev = video_drvdata(file);
567 if (!dev->multiplanar)
569 return vivid_g_fmt_vid_out(file, priv, f);
572 int vidioc_try_fmt_vid_out_mplane(struct file *file, void *priv,
573 struct v4l2_format *f)
575 struct vivid_dev *dev = video_drvdata(file);
577 if (!dev->multiplanar)
579 return vivid_try_fmt_vid_out(file, priv, f);
582 int vidioc_s_fmt_vid_out_mplane(struct file *file, void *priv,
583 struct v4l2_format *f)
585 struct vivid_dev *dev = video_drvdata(file);
587 if (!dev->multiplanar)
589 return vivid_s_fmt_vid_out(file, priv, f);
592 int vidioc_g_fmt_vid_out(struct file *file, void *priv,
593 struct v4l2_format *f)
595 struct vivid_dev *dev = video_drvdata(file);
597 if (dev->multiplanar)
599 return fmt_sp2mp_func(file, priv, f, vivid_g_fmt_vid_out);
602 int vidioc_try_fmt_vid_out(struct file *file, void *priv,
603 struct v4l2_format *f)
605 struct vivid_dev *dev = video_drvdata(file);
607 if (dev->multiplanar)
609 return fmt_sp2mp_func(file, priv, f, vivid_try_fmt_vid_out);
612 int vidioc_s_fmt_vid_out(struct file *file, void *priv,
613 struct v4l2_format *f)
615 struct vivid_dev *dev = video_drvdata(file);
617 if (dev->multiplanar)
619 return fmt_sp2mp_func(file, priv, f, vivid_s_fmt_vid_out);
622 int vivid_vid_out_g_selection(struct file *file, void *priv,
623 struct v4l2_selection *sel)
625 struct vivid_dev *dev = video_drvdata(file);
627 if (!dev->has_crop_out && !dev->has_compose_out)
629 if (sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
632 sel->r.left = sel->r.top = 0;
633 switch (sel->target) {
634 case V4L2_SEL_TGT_CROP:
635 if (!dev->has_crop_out)
637 sel->r = dev->crop_out;
639 case V4L2_SEL_TGT_CROP_DEFAULT:
640 if (!dev->has_crop_out)
642 sel->r = dev->fmt_out_rect;
644 case V4L2_SEL_TGT_CROP_BOUNDS:
645 if (!dev->has_crop_out)
647 sel->r = vivid_max_rect;
649 case V4L2_SEL_TGT_COMPOSE:
650 if (!dev->has_compose_out)
652 sel->r = dev->compose_out;
654 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
655 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
656 if (!dev->has_compose_out)
658 sel->r = dev->sink_rect;
666 int vivid_vid_out_s_selection(struct file *file, void *fh, struct v4l2_selection *s)
668 struct vivid_dev *dev = video_drvdata(file);
669 struct v4l2_rect *crop = &dev->crop_out;
670 struct v4l2_rect *compose = &dev->compose_out;
671 unsigned factor = V4L2_FIELD_HAS_T_OR_B(dev->field_out) ? 2 : 1;
674 if (!dev->has_crop_out && !dev->has_compose_out)
676 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
680 case V4L2_SEL_TGT_CROP:
681 if (!dev->has_crop_out)
683 ret = vivid_vid_adjust_sel(s->flags, &s->r);
686 rect_set_min_size(&s->r, &vivid_min_rect);
687 rect_set_max_size(&s->r, &dev->fmt_out_rect);
688 if (dev->has_scaler_out) {
689 struct v4l2_rect max_rect = {
691 dev->sink_rect.width * MAX_ZOOM,
692 (dev->sink_rect.height / factor) * MAX_ZOOM
695 rect_set_max_size(&s->r, &max_rect);
696 if (dev->has_compose_out) {
697 struct v4l2_rect min_rect = {
699 s->r.width / MAX_ZOOM,
700 (s->r.height * factor) / MAX_ZOOM
702 struct v4l2_rect max_rect = {
704 s->r.width * MAX_ZOOM,
705 (s->r.height * factor) * MAX_ZOOM
708 rect_set_min_size(compose, &min_rect);
709 rect_set_max_size(compose, &max_rect);
710 rect_map_inside(compose, &dev->compose_bounds_out);
712 } else if (dev->has_compose_out) {
714 s->r.height *= factor;
715 rect_set_max_size(&s->r, &dev->sink_rect);
716 rect_set_size_to(compose, &s->r);
717 rect_map_inside(compose, &dev->compose_bounds_out);
719 s->r.height /= factor;
721 rect_set_size_to(&s->r, &dev->sink_rect);
722 s->r.height /= factor;
724 rect_map_inside(&s->r, &dev->fmt_out_rect);
727 case V4L2_SEL_TGT_COMPOSE:
728 if (!dev->has_compose_out)
730 ret = vivid_vid_adjust_sel(s->flags, &s->r);
733 rect_set_min_size(&s->r, &vivid_min_rect);
734 rect_set_max_size(&s->r, &dev->sink_rect);
735 rect_map_inside(&s->r, &dev->compose_bounds_out);
737 s->r.height /= factor;
738 if (dev->has_scaler_out) {
739 struct v4l2_rect fmt = dev->fmt_out_rect;
740 struct v4l2_rect max_rect = {
742 s->r.width * MAX_ZOOM,
743 s->r.height * MAX_ZOOM
745 struct v4l2_rect min_rect = {
747 s->r.width / MAX_ZOOM,
748 s->r.height / MAX_ZOOM
751 rect_set_min_size(&fmt, &min_rect);
752 if (!dev->has_crop_out)
753 rect_set_max_size(&fmt, &max_rect);
754 if (!rect_same_size(&dev->fmt_out_rect, &fmt) &&
755 vb2_is_busy(&dev->vb_vid_out_q))
757 if (dev->has_crop_out) {
758 rect_set_min_size(crop, &min_rect);
759 rect_set_max_size(crop, &max_rect);
761 dev->fmt_out_rect = fmt;
762 } else if (dev->has_crop_out) {
763 struct v4l2_rect fmt = dev->fmt_out_rect;
765 rect_set_min_size(&fmt, &s->r);
766 if (!rect_same_size(&dev->fmt_out_rect, &fmt) &&
767 vb2_is_busy(&dev->vb_vid_out_q))
769 dev->fmt_out_rect = fmt;
770 rect_set_size_to(crop, &s->r);
771 rect_map_inside(crop, &dev->fmt_out_rect);
773 if (!rect_same_size(&s->r, &dev->fmt_out_rect) &&
774 vb2_is_busy(&dev->vb_vid_out_q))
776 rect_set_size_to(&dev->fmt_out_rect, &s->r);
777 rect_set_size_to(crop, &s->r);
778 crop->height /= factor;
779 rect_map_inside(crop, &dev->fmt_out_rect);
782 s->r.height *= factor;
783 if (dev->bitmap_out && (compose->width != s->r.width ||
784 compose->height != s->r.height)) {
785 kfree(dev->bitmap_out);
786 dev->bitmap_out = NULL;
797 int vivid_vid_out_cropcap(struct file *file, void *priv,
798 struct v4l2_cropcap *cap)
800 struct vivid_dev *dev = video_drvdata(file);
802 if (cap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
805 switch (vivid_get_pixel_aspect(dev)) {
806 case TPG_PIXEL_ASPECT_NTSC:
807 cap->pixelaspect.numerator = 11;
808 cap->pixelaspect.denominator = 10;
810 case TPG_PIXEL_ASPECT_PAL:
811 cap->pixelaspect.numerator = 54;
812 cap->pixelaspect.denominator = 59;
814 case TPG_PIXEL_ASPECT_SQUARE:
815 cap->pixelaspect.numerator = 1;
816 cap->pixelaspect.denominator = 1;
822 int vidioc_g_fmt_vid_out_overlay(struct file *file, void *priv,
823 struct v4l2_format *f)
825 struct vivid_dev *dev = video_drvdata(file);
826 const struct v4l2_rect *compose = &dev->compose_out;
827 struct v4l2_window *win = &f->fmt.win;
828 unsigned clipcount = win->clipcount;
832 win->w.top = dev->overlay_out_top;
833 win->w.left = dev->overlay_out_left;
834 win->w.width = compose->width;
835 win->w.height = compose->height;
836 win->clipcount = dev->clipcount_out;
837 win->field = V4L2_FIELD_ANY;
838 win->chromakey = dev->chromakey_out;
839 win->global_alpha = dev->global_alpha_out;
840 if (clipcount > dev->clipcount_out)
841 clipcount = dev->clipcount_out;
842 if (dev->bitmap_out == NULL)
844 else if (win->bitmap) {
845 if (copy_to_user(win->bitmap, dev->bitmap_out,
846 ((dev->compose_out.width + 7) / 8) * dev->compose_out.height))
849 if (clipcount && win->clips) {
850 if (copy_to_user(win->clips, dev->clips_out,
851 clipcount * sizeof(dev->clips_out[0])))
857 int vidioc_try_fmt_vid_out_overlay(struct file *file, void *priv,
858 struct v4l2_format *f)
860 struct vivid_dev *dev = video_drvdata(file);
861 const struct v4l2_rect *compose = &dev->compose_out;
862 struct v4l2_window *win = &f->fmt.win;
867 win->w.left = clamp_t(int, win->w.left,
868 -dev->display_width, dev->display_width);
869 win->w.top = clamp_t(int, win->w.top,
870 -dev->display_height, dev->display_height);
871 win->w.width = compose->width;
872 win->w.height = compose->height;
874 * It makes no sense for an OSD to overlay only top or bottom fields,
875 * so always set this to ANY.
877 win->field = V4L2_FIELD_ANY;
878 if (win->clipcount && !win->clips)
880 if (win->clipcount > MAX_CLIPS)
881 win->clipcount = MAX_CLIPS;
882 if (win->clipcount) {
883 if (copy_from_user(dev->try_clips_out, win->clips,
884 win->clipcount * sizeof(dev->clips_out[0])))
886 for (i = 0; i < win->clipcount; i++) {
887 struct v4l2_rect *r = &dev->try_clips_out[i].c;
889 r->top = clamp_t(s32, r->top, 0, dev->display_height - 1);
890 r->height = clamp_t(s32, r->height, 1, dev->display_height - r->top);
891 r->left = clamp_t(u32, r->left, 0, dev->display_width - 1);
892 r->width = clamp_t(u32, r->width, 1, dev->display_width - r->left);
895 * Yeah, so sue me, it's an O(n^2) algorithm. But n is a small
896 * number and it's typically a one-time deal.
898 for (i = 0; i < win->clipcount - 1; i++) {
899 struct v4l2_rect *r1 = &dev->try_clips_out[i].c;
901 for (j = i + 1; j < win->clipcount; j++) {
902 struct v4l2_rect *r2 = &dev->try_clips_out[j].c;
904 if (rect_overlap(r1, r2))
908 if (copy_to_user(win->clips, dev->try_clips_out,
909 win->clipcount * sizeof(dev->clips_out[0])))
915 int vidioc_s_fmt_vid_out_overlay(struct file *file, void *priv,
916 struct v4l2_format *f)
918 struct vivid_dev *dev = video_drvdata(file);
919 const struct v4l2_rect *compose = &dev->compose_out;
920 struct v4l2_window *win = &f->fmt.win;
921 int ret = vidioc_try_fmt_vid_out_overlay(file, priv, f);
922 unsigned bitmap_size = ((compose->width + 7) / 8) * compose->height;
923 unsigned clips_size = win->clipcount * sizeof(dev->clips_out[0]);
924 void *new_bitmap = NULL;
930 new_bitmap = memdup_user(win->bitmap, bitmap_size);
932 if (IS_ERR(new_bitmap))
933 return PTR_ERR(new_bitmap);
936 dev->overlay_out_top = win->w.top;
937 dev->overlay_out_left = win->w.left;
938 kfree(dev->bitmap_out);
939 dev->bitmap_out = new_bitmap;
940 dev->clipcount_out = win->clipcount;
941 if (dev->clipcount_out)
942 memcpy(dev->clips_out, dev->try_clips_out, clips_size);
943 dev->chromakey_out = win->chromakey;
944 dev->global_alpha_out = win->global_alpha;
948 int vivid_vid_out_overlay(struct file *file, void *fh, unsigned i)
950 struct vivid_dev *dev = video_drvdata(file);
952 if (i && !dev->fmt_out->can_do_overlay) {
953 dprintk(dev, 1, "unsupported output format for output overlay\n");
957 dev->overlay_out_enabled = i;
961 int vivid_vid_out_g_fbuf(struct file *file, void *fh,
962 struct v4l2_framebuffer *a)
964 struct vivid_dev *dev = video_drvdata(file);
966 a->capability = V4L2_FBUF_CAP_EXTERNOVERLAY |
967 V4L2_FBUF_CAP_BITMAP_CLIPPING |
968 V4L2_FBUF_CAP_LIST_CLIPPING |
969 V4L2_FBUF_CAP_CHROMAKEY |
970 V4L2_FBUF_CAP_SRC_CHROMAKEY |
971 V4L2_FBUF_CAP_GLOBAL_ALPHA |
972 V4L2_FBUF_CAP_LOCAL_ALPHA |
973 V4L2_FBUF_CAP_LOCAL_INV_ALPHA;
974 a->flags = V4L2_FBUF_FLAG_OVERLAY | dev->fbuf_out_flags;
975 a->base = (void *)dev->video_pbase;
976 a->fmt.width = dev->display_width;
977 a->fmt.height = dev->display_height;
978 if (dev->fb_defined.green.length == 5)
979 a->fmt.pixelformat = V4L2_PIX_FMT_ARGB555;
981 a->fmt.pixelformat = V4L2_PIX_FMT_RGB565;
982 a->fmt.bytesperline = dev->display_byte_stride;
983 a->fmt.sizeimage = a->fmt.height * a->fmt.bytesperline;
984 a->fmt.field = V4L2_FIELD_NONE;
985 a->fmt.colorspace = V4L2_COLORSPACE_SRGB;
990 int vivid_vid_out_s_fbuf(struct file *file, void *fh,
991 const struct v4l2_framebuffer *a)
993 struct vivid_dev *dev = video_drvdata(file);
994 const unsigned chroma_flags = V4L2_FBUF_FLAG_CHROMAKEY |
995 V4L2_FBUF_FLAG_SRC_CHROMAKEY;
996 const unsigned alpha_flags = V4L2_FBUF_FLAG_GLOBAL_ALPHA |
997 V4L2_FBUF_FLAG_LOCAL_ALPHA |
998 V4L2_FBUF_FLAG_LOCAL_INV_ALPHA;
1001 if ((a->flags & chroma_flags) == chroma_flags)
1003 switch (a->flags & alpha_flags) {
1005 case V4L2_FBUF_FLAG_GLOBAL_ALPHA:
1006 case V4L2_FBUF_FLAG_LOCAL_ALPHA:
1007 case V4L2_FBUF_FLAG_LOCAL_INV_ALPHA:
1012 dev->fbuf_out_flags &= ~(chroma_flags | alpha_flags);
1013 dev->fbuf_out_flags = a->flags & (chroma_flags | alpha_flags);
1017 static const struct v4l2_audioout vivid_audio_outputs[] = {
1018 { 0, "Line-Out 1" },
1019 { 1, "Line-Out 2" },
1022 int vidioc_enum_output(struct file *file, void *priv,
1023 struct v4l2_output *out)
1025 struct vivid_dev *dev = video_drvdata(file);
1027 if (out->index >= dev->num_outputs)
1030 out->type = V4L2_OUTPUT_TYPE_ANALOG;
1031 switch (dev->output_type[out->index]) {
1033 snprintf(out->name, sizeof(out->name), "S-Video %u",
1034 dev->output_name_counter[out->index]);
1035 out->std = V4L2_STD_ALL;
1036 if (dev->has_audio_outputs)
1037 out->audioset = (1 << ARRAY_SIZE(vivid_audio_outputs)) - 1;
1038 out->capabilities = V4L2_OUT_CAP_STD;
1041 snprintf(out->name, sizeof(out->name), "HDMI %u",
1042 dev->output_name_counter[out->index]);
1043 out->capabilities = V4L2_OUT_CAP_DV_TIMINGS;
1049 int vidioc_g_output(struct file *file, void *priv, unsigned *o)
1051 struct vivid_dev *dev = video_drvdata(file);
1057 int vidioc_s_output(struct file *file, void *priv, unsigned o)
1059 struct vivid_dev *dev = video_drvdata(file);
1061 if (o >= dev->num_outputs)
1064 if (o == dev->output)
1067 if (vb2_is_busy(&dev->vb_vid_out_q) || vb2_is_busy(&dev->vb_vbi_out_q))
1071 dev->tv_audio_output = 0;
1072 if (dev->output_type[o] == SVID)
1073 dev->vid_out_dev.tvnorms = V4L2_STD_ALL;
1075 dev->vid_out_dev.tvnorms = 0;
1077 dev->vbi_out_dev.tvnorms = dev->vid_out_dev.tvnorms;
1078 vivid_update_format_out(dev);
1082 int vidioc_enumaudout(struct file *file, void *fh, struct v4l2_audioout *vout)
1084 if (vout->index >= ARRAY_SIZE(vivid_audio_outputs))
1086 *vout = vivid_audio_outputs[vout->index];
1090 int vidioc_g_audout(struct file *file, void *fh, struct v4l2_audioout *vout)
1092 struct vivid_dev *dev = video_drvdata(file);
1094 if (!vivid_is_svid_out(dev))
1096 *vout = vivid_audio_outputs[dev->tv_audio_output];
1100 int vidioc_s_audout(struct file *file, void *fh, const struct v4l2_audioout *vout)
1102 struct vivid_dev *dev = video_drvdata(file);
1104 if (!vivid_is_svid_out(dev))
1106 if (vout->index >= ARRAY_SIZE(vivid_audio_outputs))
1108 dev->tv_audio_output = vout->index;
1112 int vivid_vid_out_s_std(struct file *file, void *priv, v4l2_std_id id)
1114 struct vivid_dev *dev = video_drvdata(file);
1116 if (!vivid_is_svid_out(dev))
1118 if (dev->std_out == id)
1120 if (vb2_is_busy(&dev->vb_vid_out_q) || vb2_is_busy(&dev->vb_vbi_out_q))
1123 vivid_update_format_out(dev);
1127 static bool valid_cvt_gtf_timings(struct v4l2_dv_timings *timings)
1129 struct v4l2_bt_timings *bt = &timings->bt;
1131 if ((bt->standards & (V4L2_DV_BT_STD_CVT | V4L2_DV_BT_STD_GTF)) &&
1132 v4l2_valid_dv_timings(timings, &vivid_dv_timings_cap, NULL, NULL))
1138 int vivid_vid_out_s_dv_timings(struct file *file, void *_fh,
1139 struct v4l2_dv_timings *timings)
1141 struct vivid_dev *dev = video_drvdata(file);
1142 if (!vivid_is_hdmi_out(dev))
1144 if (!v4l2_find_dv_timings_cap(timings, &vivid_dv_timings_cap,
1146 !valid_cvt_gtf_timings(timings))
1148 if (v4l2_match_dv_timings(timings, &dev->dv_timings_out, 0, true))
1150 if (vb2_is_busy(&dev->vb_vid_out_q))
1152 dev->dv_timings_out = *timings;
1153 vivid_update_format_out(dev);
1157 int vivid_vid_out_g_parm(struct file *file, void *priv,
1158 struct v4l2_streamparm *parm)
1160 struct vivid_dev *dev = video_drvdata(file);
1162 if (parm->type != (dev->multiplanar ?
1163 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE :
1164 V4L2_BUF_TYPE_VIDEO_OUTPUT))
1167 parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
1168 parm->parm.output.timeperframe = dev->timeperframe_vid_out;
1169 parm->parm.output.writebuffers = 1;
1174 int vidioc_subscribe_event(struct v4l2_fh *fh,
1175 const struct v4l2_event_subscription *sub)
1177 switch (sub->type) {
1178 case V4L2_EVENT_CTRL:
1179 return v4l2_ctrl_subscribe_event(fh, sub);
1180 case V4L2_EVENT_SOURCE_CHANGE:
1181 if (fh->vdev->vfl_dir == VFL_DIR_RX)
1182 return v4l2_src_change_event_subscribe(fh, sub);