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, const struct v4l2_format *fmt,
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;
67 const struct v4l2_pix_format_mplane *mp;
68 struct v4l2_format mp_fmt;
70 if (!V4L2_TYPE_IS_MULTIPLANAR(fmt->type)) {
71 fmt_sp2mp(fmt, &mp_fmt);
74 mp = &fmt->fmt.pix_mp;
76 * Check if the number of planes in the specified format match
77 * the number of planes in the current format. You can't mix that.
79 if (mp->num_planes != planes)
81 sizes[0] = mp->plane_fmt[0].sizeimage;
84 for (p = 1; p < planes; p++) {
85 sizes[p] = mp->plane_fmt[p].sizeimage;
86 if (sizes[p] < dev->bytesperline_out[p] * h)
90 for (p = 0; p < planes; p++)
91 sizes[p] = p ? dev->bytesperline_out[p] * h : size;
94 if (vq->num_buffers + *nbuffers < 2)
95 *nbuffers = 2 - vq->num_buffers;
100 * videobuf2-vmalloc allocator is context-less so no need to set
104 dprintk(dev, 1, "%s: count=%d\n", __func__, *nbuffers);
105 for (p = 0; p < planes; p++)
106 dprintk(dev, 1, "%s: size[%u]=%u\n", __func__, p, sizes[p]);
110 static int vid_out_buf_prepare(struct vb2_buffer *vb)
112 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
113 struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
118 dprintk(dev, 1, "%s\n", __func__);
120 if (WARN_ON(NULL == dev->fmt_out))
123 planes = dev->fmt_out->planes;
125 if (dev->buf_prepare_error) {
127 * Error injection: test what happens if buf_prepare() returns
130 dev->buf_prepare_error = false;
134 if (dev->field_out != V4L2_FIELD_ALTERNATE)
135 vbuf->field = dev->field_out;
136 else if (vbuf->field != V4L2_FIELD_TOP &&
137 vbuf->field != V4L2_FIELD_BOTTOM)
140 for (p = 0; p < planes; p++) {
141 size = dev->bytesperline_out[p] * dev->fmt_out_rect.height +
142 vb->planes[p].data_offset;
144 if (vb2_get_plane_payload(vb, p) < size) {
145 dprintk(dev, 1, "%s the payload is too small for plane %u (%lu < %lu)\n",
146 __func__, p, vb2_get_plane_payload(vb, p), size);
154 static void vid_out_buf_queue(struct vb2_buffer *vb)
156 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
157 struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
158 struct vivid_buffer *buf = container_of(vbuf, struct vivid_buffer, vb);
160 dprintk(dev, 1, "%s\n", __func__);
162 spin_lock(&dev->slock);
163 list_add_tail(&buf->list, &dev->vid_out_active);
164 spin_unlock(&dev->slock);
167 static int vid_out_start_streaming(struct vb2_queue *vq, unsigned count)
169 struct vivid_dev *dev = vb2_get_drv_priv(vq);
172 if (vb2_is_streaming(&dev->vb_vid_cap_q))
173 dev->can_loop_video = vivid_vid_can_loop(dev);
175 if (dev->kthread_vid_out)
178 dev->vid_out_seq_count = 0;
179 dprintk(dev, 1, "%s\n", __func__);
180 if (dev->start_streaming_error) {
181 dev->start_streaming_error = false;
184 err = vivid_start_generating_vid_out(dev, &dev->vid_out_streaming);
187 struct vivid_buffer *buf, *tmp;
189 list_for_each_entry_safe(buf, tmp, &dev->vid_out_active, list) {
190 list_del(&buf->list);
191 vb2_buffer_done(&buf->vb.vb2_buf,
192 VB2_BUF_STATE_QUEUED);
198 /* abort streaming and wait for last buffer */
199 static void vid_out_stop_streaming(struct vb2_queue *vq)
201 struct vivid_dev *dev = vb2_get_drv_priv(vq);
203 dprintk(dev, 1, "%s\n", __func__);
204 vivid_stop_generating_vid_out(dev, &dev->vid_out_streaming);
205 dev->can_loop_video = false;
208 const struct vb2_ops vivid_vid_out_qops = {
209 .queue_setup = vid_out_queue_setup,
210 .buf_prepare = vid_out_buf_prepare,
211 .buf_queue = vid_out_buf_queue,
212 .start_streaming = vid_out_start_streaming,
213 .stop_streaming = vid_out_stop_streaming,
214 .wait_prepare = vb2_ops_wait_prepare,
215 .wait_finish = vb2_ops_wait_finish,
219 * Called whenever the format has to be reset which can occur when
220 * changing outputs, standard, timings, etc.
222 void vivid_update_format_out(struct vivid_dev *dev)
224 struct v4l2_bt_timings *bt = &dev->dv_timings_out.bt;
227 switch (dev->output_type[dev->output]) {
230 dev->field_out = dev->tv_field_out;
231 dev->sink_rect.width = 720;
232 if (dev->std_out & V4L2_STD_525_60) {
233 dev->sink_rect.height = 480;
234 dev->timeperframe_vid_out = (struct v4l2_fract) { 1001, 30000 };
235 dev->service_set_out = V4L2_SLICED_CAPTION_525;
237 dev->sink_rect.height = 576;
238 dev->timeperframe_vid_out = (struct v4l2_fract) { 1000, 25000 };
239 dev->service_set_out = V4L2_SLICED_WSS_625 | V4L2_SLICED_TELETEXT_B;
241 dev->colorspace_out = V4L2_COLORSPACE_SMPTE170M;
244 dev->sink_rect.width = bt->width;
245 dev->sink_rect.height = bt->height;
246 size = V4L2_DV_BT_FRAME_WIDTH(bt) * V4L2_DV_BT_FRAME_HEIGHT(bt);
247 dev->timeperframe_vid_out = (struct v4l2_fract) {
248 size / 100, (u32)bt->pixelclock / 100
251 dev->field_out = V4L2_FIELD_ALTERNATE;
253 dev->field_out = V4L2_FIELD_NONE;
254 if (!dev->dvi_d_out && (bt->flags & V4L2_DV_FL_IS_CE_VIDEO)) {
255 if (bt->width == 720 && bt->height <= 576)
256 dev->colorspace_out = V4L2_COLORSPACE_SMPTE170M;
258 dev->colorspace_out = V4L2_COLORSPACE_REC709;
260 dev->colorspace_out = V4L2_COLORSPACE_SRGB;
264 dev->xfer_func_out = V4L2_XFER_FUNC_DEFAULT;
265 dev->ycbcr_enc_out = V4L2_YCBCR_ENC_DEFAULT;
266 dev->quantization_out = V4L2_QUANTIZATION_DEFAULT;
267 dev->compose_out = dev->sink_rect;
268 dev->compose_bounds_out = dev->sink_rect;
269 dev->crop_out = dev->compose_out;
270 if (V4L2_FIELD_HAS_T_OR_B(dev->field_out))
271 dev->crop_out.height /= 2;
272 dev->fmt_out_rect = dev->crop_out;
273 for (p = 0; p < dev->fmt_out->planes; p++)
274 dev->bytesperline_out[p] =
275 (dev->sink_rect.width * dev->fmt_out->bit_depth[p]) / 8;
278 /* Map the field to something that is valid for the current output */
279 static enum v4l2_field vivid_field_out(struct vivid_dev *dev, enum v4l2_field field)
281 if (vivid_is_svid_out(dev)) {
283 case V4L2_FIELD_INTERLACED_TB:
284 case V4L2_FIELD_INTERLACED_BT:
285 case V4L2_FIELD_SEQ_TB:
286 case V4L2_FIELD_SEQ_BT:
287 case V4L2_FIELD_ALTERNATE:
289 case V4L2_FIELD_INTERLACED:
291 return V4L2_FIELD_INTERLACED;
294 if (vivid_is_hdmi_out(dev))
295 return dev->dv_timings_out.bt.interlaced ? V4L2_FIELD_ALTERNATE :
297 return V4L2_FIELD_NONE;
300 static enum tpg_pixel_aspect vivid_get_pixel_aspect(const struct vivid_dev *dev)
302 if (vivid_is_svid_out(dev))
303 return (dev->std_out & V4L2_STD_525_60) ?
304 TPG_PIXEL_ASPECT_NTSC : TPG_PIXEL_ASPECT_PAL;
306 if (vivid_is_hdmi_out(dev) &&
307 dev->sink_rect.width == 720 && dev->sink_rect.height <= 576)
308 return dev->sink_rect.height == 480 ?
309 TPG_PIXEL_ASPECT_NTSC : TPG_PIXEL_ASPECT_PAL;
311 return TPG_PIXEL_ASPECT_SQUARE;
314 int vivid_g_fmt_vid_out(struct file *file, void *priv,
315 struct v4l2_format *f)
317 struct vivid_dev *dev = video_drvdata(file);
318 struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
319 const struct vivid_fmt *fmt = dev->fmt_out;
322 mp->width = dev->fmt_out_rect.width;
323 mp->height = dev->fmt_out_rect.height;
324 mp->field = dev->field_out;
325 mp->pixelformat = fmt->fourcc;
326 mp->colorspace = dev->colorspace_out;
327 mp->xfer_func = dev->xfer_func_out;
328 mp->ycbcr_enc = dev->ycbcr_enc_out;
329 mp->quantization = dev->quantization_out;
330 mp->num_planes = fmt->buffers;
331 for (p = 0; p < mp->num_planes; p++) {
332 mp->plane_fmt[p].bytesperline = dev->bytesperline_out[p];
333 mp->plane_fmt[p].sizeimage =
334 mp->plane_fmt[p].bytesperline * mp->height;
336 for (p = fmt->buffers; p < fmt->planes; p++) {
337 unsigned stride = dev->bytesperline_out[p];
339 mp->plane_fmt[0].sizeimage +=
340 (stride * mp->height) / fmt->vdownsampling[p];
345 int vivid_try_fmt_vid_out(struct file *file, void *priv,
346 struct v4l2_format *f)
348 struct vivid_dev *dev = video_drvdata(file);
349 struct v4l2_bt_timings *bt = &dev->dv_timings_out.bt;
350 struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
351 struct v4l2_plane_pix_format *pfmt = mp->plane_fmt;
352 const struct vivid_fmt *fmt;
353 unsigned bytesperline, max_bpl;
358 fmt = vivid_get_format(dev, mp->pixelformat);
360 dprintk(dev, 1, "Fourcc format (0x%08x) unknown.\n",
362 mp->pixelformat = V4L2_PIX_FMT_YUYV;
363 fmt = vivid_get_format(dev, mp->pixelformat);
366 mp->field = vivid_field_out(dev, mp->field);
367 if (vivid_is_svid_out(dev)) {
369 h = (dev->std_out & V4L2_STD_525_60) ? 480 : 576;
371 w = dev->sink_rect.width;
372 h = dev->sink_rect.height;
374 if (V4L2_FIELD_HAS_T_OR_B(mp->field))
376 if (!dev->has_scaler_out && !dev->has_crop_out && !dev->has_compose_out) {
378 mp->height = h / factor;
380 struct v4l2_rect r = { 0, 0, mp->width, mp->height * factor };
382 rect_set_min_size(&r, &vivid_min_rect);
383 rect_set_max_size(&r, &vivid_max_rect);
384 if (dev->has_scaler_out && !dev->has_crop_out) {
385 struct v4l2_rect max_r = { 0, 0, MAX_ZOOM * w, MAX_ZOOM * h };
387 rect_set_max_size(&r, &max_r);
388 } else if (!dev->has_scaler_out && dev->has_compose_out && !dev->has_crop_out) {
389 rect_set_max_size(&r, &dev->sink_rect);
390 } else if (!dev->has_scaler_out && !dev->has_compose_out) {
391 rect_set_min_size(&r, &dev->sink_rect);
394 mp->height = r.height / factor;
397 /* This driver supports custom bytesperline values */
399 /* Calculate the minimum supported bytesperline value */
400 bytesperline = (mp->width * fmt->bit_depth[0]) >> 3;
401 /* Calculate the maximum supported bytesperline value */
402 max_bpl = (MAX_ZOOM * MAX_WIDTH * fmt->bit_depth[0]) >> 3;
403 mp->num_planes = fmt->buffers;
404 for (p = 0; p < mp->num_planes; p++) {
405 if (pfmt[p].bytesperline > max_bpl)
406 pfmt[p].bytesperline = max_bpl;
407 if (pfmt[p].bytesperline < bytesperline)
408 pfmt[p].bytesperline = bytesperline;
409 pfmt[p].sizeimage = pfmt[p].bytesperline * mp->height;
410 memset(pfmt[p].reserved, 0, sizeof(pfmt[p].reserved));
412 for (p = fmt->buffers; p < fmt->planes; p++)
413 pfmt[0].sizeimage += (pfmt[0].bytesperline * fmt->bit_depth[p]) /
414 (fmt->bit_depth[0] * fmt->vdownsampling[p]);
415 mp->xfer_func = V4L2_XFER_FUNC_DEFAULT;
416 mp->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
417 mp->quantization = V4L2_QUANTIZATION_DEFAULT;
418 if (vivid_is_svid_out(dev)) {
419 mp->colorspace = V4L2_COLORSPACE_SMPTE170M;
420 } else if (dev->dvi_d_out || !(bt->flags & V4L2_DV_FL_IS_CE_VIDEO)) {
421 mp->colorspace = V4L2_COLORSPACE_SRGB;
423 mp->quantization = V4L2_QUANTIZATION_LIM_RANGE;
424 } else if (bt->width == 720 && bt->height <= 576) {
425 mp->colorspace = V4L2_COLORSPACE_SMPTE170M;
426 } else if (mp->colorspace != V4L2_COLORSPACE_SMPTE170M &&
427 mp->colorspace != V4L2_COLORSPACE_REC709 &&
428 mp->colorspace != V4L2_COLORSPACE_ADOBERGB &&
429 mp->colorspace != V4L2_COLORSPACE_BT2020 &&
430 mp->colorspace != V4L2_COLORSPACE_SRGB) {
431 mp->colorspace = V4L2_COLORSPACE_REC709;
433 memset(mp->reserved, 0, sizeof(mp->reserved));
437 int vivid_s_fmt_vid_out(struct file *file, void *priv,
438 struct v4l2_format *f)
440 struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
441 struct vivid_dev *dev = video_drvdata(file);
442 struct v4l2_rect *crop = &dev->crop_out;
443 struct v4l2_rect *compose = &dev->compose_out;
444 struct vb2_queue *q = &dev->vb_vid_out_q;
445 int ret = vivid_try_fmt_vid_out(file, priv, f);
452 if (vb2_is_busy(q) &&
453 (vivid_is_svid_out(dev) ||
454 mp->width != dev->fmt_out_rect.width ||
455 mp->height != dev->fmt_out_rect.height ||
456 mp->pixelformat != dev->fmt_out->fourcc ||
457 mp->field != dev->field_out)) {
458 dprintk(dev, 1, "%s device busy\n", __func__);
463 * Allow for changing the colorspace on the fly. Useful for testing
464 * purposes, and it is something that HDMI transmitters are able
470 dev->fmt_out = vivid_get_format(dev, mp->pixelformat);
471 if (V4L2_FIELD_HAS_T_OR_B(mp->field))
474 if (dev->has_scaler_out || dev->has_crop_out || dev->has_compose_out) {
475 struct v4l2_rect r = { 0, 0, mp->width, mp->height };
477 if (dev->has_scaler_out) {
478 if (dev->has_crop_out)
479 rect_map_inside(crop, &r);
482 if (dev->has_compose_out && !dev->has_crop_out) {
483 struct v4l2_rect min_r = {
486 factor * r.height / MAX_ZOOM
488 struct v4l2_rect max_r = {
491 factor * r.height * MAX_ZOOM
494 rect_set_min_size(compose, &min_r);
495 rect_set_max_size(compose, &max_r);
496 rect_map_inside(compose, &dev->compose_bounds_out);
497 } else if (dev->has_compose_out) {
498 struct v4l2_rect min_r = {
500 crop->width / MAX_ZOOM,
501 factor * crop->height / MAX_ZOOM
503 struct v4l2_rect max_r = {
505 crop->width * MAX_ZOOM,
506 factor * crop->height * MAX_ZOOM
509 rect_set_min_size(compose, &min_r);
510 rect_set_max_size(compose, &max_r);
511 rect_map_inside(compose, &dev->compose_bounds_out);
513 } else if (dev->has_compose_out && !dev->has_crop_out) {
514 rect_set_size_to(crop, &r);
516 rect_set_size_to(compose, &r);
517 rect_map_inside(compose, &dev->compose_bounds_out);
518 } else if (!dev->has_compose_out) {
519 rect_map_inside(crop, &r);
521 rect_set_size_to(compose, &r);
524 rect_set_max_size(compose, &r);
525 rect_map_inside(compose, &dev->compose_bounds_out);
527 crop->height *= factor;
528 rect_set_size_to(crop, compose);
529 rect_map_inside(crop, &r);
531 crop->height /= factor;
534 struct v4l2_rect r = { 0, 0, mp->width, mp->height };
536 rect_set_size_to(crop, &r);
538 rect_set_size_to(compose, &r);
541 dev->fmt_out_rect.width = mp->width;
542 dev->fmt_out_rect.height = mp->height;
543 for (p = 0; p < mp->num_planes; p++)
544 dev->bytesperline_out[p] = mp->plane_fmt[p].bytesperline;
545 for (p = dev->fmt_out->buffers; p < dev->fmt_out->planes; p++)
546 dev->bytesperline_out[p] =
547 (dev->bytesperline_out[0] * dev->fmt_out->bit_depth[p]) /
548 dev->fmt_out->bit_depth[0];
549 dev->field_out = mp->field;
550 if (vivid_is_svid_out(dev))
551 dev->tv_field_out = mp->field;
554 dev->colorspace_out = mp->colorspace;
555 dev->xfer_func_out = mp->xfer_func;
556 dev->ycbcr_enc_out = mp->ycbcr_enc;
557 dev->quantization_out = mp->quantization;
558 if (dev->loop_video) {
559 vivid_send_source_change(dev, SVID);
560 vivid_send_source_change(dev, HDMI);
565 int vidioc_g_fmt_vid_out_mplane(struct file *file, void *priv,
566 struct v4l2_format *f)
568 struct vivid_dev *dev = video_drvdata(file);
570 if (!dev->multiplanar)
572 return vivid_g_fmt_vid_out(file, priv, f);
575 int vidioc_try_fmt_vid_out_mplane(struct file *file, void *priv,
576 struct v4l2_format *f)
578 struct vivid_dev *dev = video_drvdata(file);
580 if (!dev->multiplanar)
582 return vivid_try_fmt_vid_out(file, priv, f);
585 int vidioc_s_fmt_vid_out_mplane(struct file *file, void *priv,
586 struct v4l2_format *f)
588 struct vivid_dev *dev = video_drvdata(file);
590 if (!dev->multiplanar)
592 return vivid_s_fmt_vid_out(file, priv, f);
595 int vidioc_g_fmt_vid_out(struct file *file, void *priv,
596 struct v4l2_format *f)
598 struct vivid_dev *dev = video_drvdata(file);
600 if (dev->multiplanar)
602 return fmt_sp2mp_func(file, priv, f, vivid_g_fmt_vid_out);
605 int vidioc_try_fmt_vid_out(struct file *file, void *priv,
606 struct v4l2_format *f)
608 struct vivid_dev *dev = video_drvdata(file);
610 if (dev->multiplanar)
612 return fmt_sp2mp_func(file, priv, f, vivid_try_fmt_vid_out);
615 int vidioc_s_fmt_vid_out(struct file *file, void *priv,
616 struct v4l2_format *f)
618 struct vivid_dev *dev = video_drvdata(file);
620 if (dev->multiplanar)
622 return fmt_sp2mp_func(file, priv, f, vivid_s_fmt_vid_out);
625 int vivid_vid_out_g_selection(struct file *file, void *priv,
626 struct v4l2_selection *sel)
628 struct vivid_dev *dev = video_drvdata(file);
630 if (!dev->has_crop_out && !dev->has_compose_out)
632 if (sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
635 sel->r.left = sel->r.top = 0;
636 switch (sel->target) {
637 case V4L2_SEL_TGT_CROP:
638 if (!dev->has_crop_out)
640 sel->r = dev->crop_out;
642 case V4L2_SEL_TGT_CROP_DEFAULT:
643 if (!dev->has_crop_out)
645 sel->r = dev->fmt_out_rect;
647 case V4L2_SEL_TGT_CROP_BOUNDS:
648 if (!dev->has_crop_out)
650 sel->r = vivid_max_rect;
652 case V4L2_SEL_TGT_COMPOSE:
653 if (!dev->has_compose_out)
655 sel->r = dev->compose_out;
657 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
658 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
659 if (!dev->has_compose_out)
661 sel->r = dev->sink_rect;
669 int vivid_vid_out_s_selection(struct file *file, void *fh, struct v4l2_selection *s)
671 struct vivid_dev *dev = video_drvdata(file);
672 struct v4l2_rect *crop = &dev->crop_out;
673 struct v4l2_rect *compose = &dev->compose_out;
674 unsigned factor = V4L2_FIELD_HAS_T_OR_B(dev->field_out) ? 2 : 1;
677 if (!dev->has_crop_out && !dev->has_compose_out)
679 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
683 case V4L2_SEL_TGT_CROP:
684 if (!dev->has_crop_out)
686 ret = vivid_vid_adjust_sel(s->flags, &s->r);
689 rect_set_min_size(&s->r, &vivid_min_rect);
690 rect_set_max_size(&s->r, &dev->fmt_out_rect);
691 if (dev->has_scaler_out) {
692 struct v4l2_rect max_rect = {
694 dev->sink_rect.width * MAX_ZOOM,
695 (dev->sink_rect.height / factor) * MAX_ZOOM
698 rect_set_max_size(&s->r, &max_rect);
699 if (dev->has_compose_out) {
700 struct v4l2_rect min_rect = {
702 s->r.width / MAX_ZOOM,
703 (s->r.height * factor) / MAX_ZOOM
705 struct v4l2_rect max_rect = {
707 s->r.width * MAX_ZOOM,
708 (s->r.height * factor) * MAX_ZOOM
711 rect_set_min_size(compose, &min_rect);
712 rect_set_max_size(compose, &max_rect);
713 rect_map_inside(compose, &dev->compose_bounds_out);
715 } else if (dev->has_compose_out) {
717 s->r.height *= factor;
718 rect_set_max_size(&s->r, &dev->sink_rect);
719 rect_set_size_to(compose, &s->r);
720 rect_map_inside(compose, &dev->compose_bounds_out);
722 s->r.height /= factor;
724 rect_set_size_to(&s->r, &dev->sink_rect);
725 s->r.height /= factor;
727 rect_map_inside(&s->r, &dev->fmt_out_rect);
730 case V4L2_SEL_TGT_COMPOSE:
731 if (!dev->has_compose_out)
733 ret = vivid_vid_adjust_sel(s->flags, &s->r);
736 rect_set_min_size(&s->r, &vivid_min_rect);
737 rect_set_max_size(&s->r, &dev->sink_rect);
738 rect_map_inside(&s->r, &dev->compose_bounds_out);
740 s->r.height /= factor;
741 if (dev->has_scaler_out) {
742 struct v4l2_rect fmt = dev->fmt_out_rect;
743 struct v4l2_rect max_rect = {
745 s->r.width * MAX_ZOOM,
746 s->r.height * MAX_ZOOM
748 struct v4l2_rect min_rect = {
750 s->r.width / MAX_ZOOM,
751 s->r.height / MAX_ZOOM
754 rect_set_min_size(&fmt, &min_rect);
755 if (!dev->has_crop_out)
756 rect_set_max_size(&fmt, &max_rect);
757 if (!rect_same_size(&dev->fmt_out_rect, &fmt) &&
758 vb2_is_busy(&dev->vb_vid_out_q))
760 if (dev->has_crop_out) {
761 rect_set_min_size(crop, &min_rect);
762 rect_set_max_size(crop, &max_rect);
764 dev->fmt_out_rect = fmt;
765 } else if (dev->has_crop_out) {
766 struct v4l2_rect fmt = dev->fmt_out_rect;
768 rect_set_min_size(&fmt, &s->r);
769 if (!rect_same_size(&dev->fmt_out_rect, &fmt) &&
770 vb2_is_busy(&dev->vb_vid_out_q))
772 dev->fmt_out_rect = fmt;
773 rect_set_size_to(crop, &s->r);
774 rect_map_inside(crop, &dev->fmt_out_rect);
776 if (!rect_same_size(&s->r, &dev->fmt_out_rect) &&
777 vb2_is_busy(&dev->vb_vid_out_q))
779 rect_set_size_to(&dev->fmt_out_rect, &s->r);
780 rect_set_size_to(crop, &s->r);
781 crop->height /= factor;
782 rect_map_inside(crop, &dev->fmt_out_rect);
785 s->r.height *= factor;
786 if (dev->bitmap_out && (compose->width != s->r.width ||
787 compose->height != s->r.height)) {
788 kfree(dev->bitmap_out);
789 dev->bitmap_out = NULL;
800 int vivid_vid_out_cropcap(struct file *file, void *priv,
801 struct v4l2_cropcap *cap)
803 struct vivid_dev *dev = video_drvdata(file);
805 if (cap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
808 switch (vivid_get_pixel_aspect(dev)) {
809 case TPG_PIXEL_ASPECT_NTSC:
810 cap->pixelaspect.numerator = 11;
811 cap->pixelaspect.denominator = 10;
813 case TPG_PIXEL_ASPECT_PAL:
814 cap->pixelaspect.numerator = 54;
815 cap->pixelaspect.denominator = 59;
817 case TPG_PIXEL_ASPECT_SQUARE:
818 cap->pixelaspect.numerator = 1;
819 cap->pixelaspect.denominator = 1;
825 int vidioc_g_fmt_vid_out_overlay(struct file *file, void *priv,
826 struct v4l2_format *f)
828 struct vivid_dev *dev = video_drvdata(file);
829 const struct v4l2_rect *compose = &dev->compose_out;
830 struct v4l2_window *win = &f->fmt.win;
831 unsigned clipcount = win->clipcount;
835 win->w.top = dev->overlay_out_top;
836 win->w.left = dev->overlay_out_left;
837 win->w.width = compose->width;
838 win->w.height = compose->height;
839 win->clipcount = dev->clipcount_out;
840 win->field = V4L2_FIELD_ANY;
841 win->chromakey = dev->chromakey_out;
842 win->global_alpha = dev->global_alpha_out;
843 if (clipcount > dev->clipcount_out)
844 clipcount = dev->clipcount_out;
845 if (dev->bitmap_out == NULL)
847 else if (win->bitmap) {
848 if (copy_to_user(win->bitmap, dev->bitmap_out,
849 ((dev->compose_out.width + 7) / 8) * dev->compose_out.height))
852 if (clipcount && win->clips) {
853 if (copy_to_user(win->clips, dev->clips_out,
854 clipcount * sizeof(dev->clips_out[0])))
860 int vidioc_try_fmt_vid_out_overlay(struct file *file, void *priv,
861 struct v4l2_format *f)
863 struct vivid_dev *dev = video_drvdata(file);
864 const struct v4l2_rect *compose = &dev->compose_out;
865 struct v4l2_window *win = &f->fmt.win;
870 win->w.left = clamp_t(int, win->w.left,
871 -dev->display_width, dev->display_width);
872 win->w.top = clamp_t(int, win->w.top,
873 -dev->display_height, dev->display_height);
874 win->w.width = compose->width;
875 win->w.height = compose->height;
877 * It makes no sense for an OSD to overlay only top or bottom fields,
878 * so always set this to ANY.
880 win->field = V4L2_FIELD_ANY;
881 if (win->clipcount && !win->clips)
883 if (win->clipcount > MAX_CLIPS)
884 win->clipcount = MAX_CLIPS;
885 if (win->clipcount) {
886 if (copy_from_user(dev->try_clips_out, win->clips,
887 win->clipcount * sizeof(dev->clips_out[0])))
889 for (i = 0; i < win->clipcount; i++) {
890 struct v4l2_rect *r = &dev->try_clips_out[i].c;
892 r->top = clamp_t(s32, r->top, 0, dev->display_height - 1);
893 r->height = clamp_t(s32, r->height, 1, dev->display_height - r->top);
894 r->left = clamp_t(u32, r->left, 0, dev->display_width - 1);
895 r->width = clamp_t(u32, r->width, 1, dev->display_width - r->left);
898 * Yeah, so sue me, it's an O(n^2) algorithm. But n is a small
899 * number and it's typically a one-time deal.
901 for (i = 0; i < win->clipcount - 1; i++) {
902 struct v4l2_rect *r1 = &dev->try_clips_out[i].c;
904 for (j = i + 1; j < win->clipcount; j++) {
905 struct v4l2_rect *r2 = &dev->try_clips_out[j].c;
907 if (rect_overlap(r1, r2))
911 if (copy_to_user(win->clips, dev->try_clips_out,
912 win->clipcount * sizeof(dev->clips_out[0])))
918 int vidioc_s_fmt_vid_out_overlay(struct file *file, void *priv,
919 struct v4l2_format *f)
921 struct vivid_dev *dev = video_drvdata(file);
922 const struct v4l2_rect *compose = &dev->compose_out;
923 struct v4l2_window *win = &f->fmt.win;
924 int ret = vidioc_try_fmt_vid_out_overlay(file, priv, f);
925 unsigned bitmap_size = ((compose->width + 7) / 8) * compose->height;
926 unsigned clips_size = win->clipcount * sizeof(dev->clips_out[0]);
927 void *new_bitmap = NULL;
933 new_bitmap = memdup_user(win->bitmap, bitmap_size);
935 if (IS_ERR(new_bitmap))
936 return PTR_ERR(new_bitmap);
939 dev->overlay_out_top = win->w.top;
940 dev->overlay_out_left = win->w.left;
941 kfree(dev->bitmap_out);
942 dev->bitmap_out = new_bitmap;
943 dev->clipcount_out = win->clipcount;
944 if (dev->clipcount_out)
945 memcpy(dev->clips_out, dev->try_clips_out, clips_size);
946 dev->chromakey_out = win->chromakey;
947 dev->global_alpha_out = win->global_alpha;
951 int vivid_vid_out_overlay(struct file *file, void *fh, unsigned i)
953 struct vivid_dev *dev = video_drvdata(file);
955 if (i && !dev->fmt_out->can_do_overlay) {
956 dprintk(dev, 1, "unsupported output format for output overlay\n");
960 dev->overlay_out_enabled = i;
964 int vivid_vid_out_g_fbuf(struct file *file, void *fh,
965 struct v4l2_framebuffer *a)
967 struct vivid_dev *dev = video_drvdata(file);
969 a->capability = V4L2_FBUF_CAP_EXTERNOVERLAY |
970 V4L2_FBUF_CAP_BITMAP_CLIPPING |
971 V4L2_FBUF_CAP_LIST_CLIPPING |
972 V4L2_FBUF_CAP_CHROMAKEY |
973 V4L2_FBUF_CAP_SRC_CHROMAKEY |
974 V4L2_FBUF_CAP_GLOBAL_ALPHA |
975 V4L2_FBUF_CAP_LOCAL_ALPHA |
976 V4L2_FBUF_CAP_LOCAL_INV_ALPHA;
977 a->flags = V4L2_FBUF_FLAG_OVERLAY | dev->fbuf_out_flags;
978 a->base = (void *)dev->video_pbase;
979 a->fmt.width = dev->display_width;
980 a->fmt.height = dev->display_height;
981 if (dev->fb_defined.green.length == 5)
982 a->fmt.pixelformat = V4L2_PIX_FMT_ARGB555;
984 a->fmt.pixelformat = V4L2_PIX_FMT_RGB565;
985 a->fmt.bytesperline = dev->display_byte_stride;
986 a->fmt.sizeimage = a->fmt.height * a->fmt.bytesperline;
987 a->fmt.field = V4L2_FIELD_NONE;
988 a->fmt.colorspace = V4L2_COLORSPACE_SRGB;
993 int vivid_vid_out_s_fbuf(struct file *file, void *fh,
994 const struct v4l2_framebuffer *a)
996 struct vivid_dev *dev = video_drvdata(file);
997 const unsigned chroma_flags = V4L2_FBUF_FLAG_CHROMAKEY |
998 V4L2_FBUF_FLAG_SRC_CHROMAKEY;
999 const unsigned alpha_flags = V4L2_FBUF_FLAG_GLOBAL_ALPHA |
1000 V4L2_FBUF_FLAG_LOCAL_ALPHA |
1001 V4L2_FBUF_FLAG_LOCAL_INV_ALPHA;
1004 if ((a->flags & chroma_flags) == chroma_flags)
1006 switch (a->flags & alpha_flags) {
1008 case V4L2_FBUF_FLAG_GLOBAL_ALPHA:
1009 case V4L2_FBUF_FLAG_LOCAL_ALPHA:
1010 case V4L2_FBUF_FLAG_LOCAL_INV_ALPHA:
1015 dev->fbuf_out_flags &= ~(chroma_flags | alpha_flags);
1016 dev->fbuf_out_flags = a->flags & (chroma_flags | alpha_flags);
1020 static const struct v4l2_audioout vivid_audio_outputs[] = {
1021 { 0, "Line-Out 1" },
1022 { 1, "Line-Out 2" },
1025 int vidioc_enum_output(struct file *file, void *priv,
1026 struct v4l2_output *out)
1028 struct vivid_dev *dev = video_drvdata(file);
1030 if (out->index >= dev->num_outputs)
1033 out->type = V4L2_OUTPUT_TYPE_ANALOG;
1034 switch (dev->output_type[out->index]) {
1036 snprintf(out->name, sizeof(out->name), "S-Video %u",
1037 dev->output_name_counter[out->index]);
1038 out->std = V4L2_STD_ALL;
1039 if (dev->has_audio_outputs)
1040 out->audioset = (1 << ARRAY_SIZE(vivid_audio_outputs)) - 1;
1041 out->capabilities = V4L2_OUT_CAP_STD;
1044 snprintf(out->name, sizeof(out->name), "HDMI %u",
1045 dev->output_name_counter[out->index]);
1046 out->capabilities = V4L2_OUT_CAP_DV_TIMINGS;
1052 int vidioc_g_output(struct file *file, void *priv, unsigned *o)
1054 struct vivid_dev *dev = video_drvdata(file);
1060 int vidioc_s_output(struct file *file, void *priv, unsigned o)
1062 struct vivid_dev *dev = video_drvdata(file);
1064 if (o >= dev->num_outputs)
1067 if (o == dev->output)
1070 if (vb2_is_busy(&dev->vb_vid_out_q) || vb2_is_busy(&dev->vb_vbi_out_q))
1074 dev->tv_audio_output = 0;
1075 if (dev->output_type[o] == SVID)
1076 dev->vid_out_dev.tvnorms = V4L2_STD_ALL;
1078 dev->vid_out_dev.tvnorms = 0;
1080 dev->vbi_out_dev.tvnorms = dev->vid_out_dev.tvnorms;
1081 vivid_update_format_out(dev);
1085 int vidioc_enumaudout(struct file *file, void *fh, struct v4l2_audioout *vout)
1087 if (vout->index >= ARRAY_SIZE(vivid_audio_outputs))
1089 *vout = vivid_audio_outputs[vout->index];
1093 int vidioc_g_audout(struct file *file, void *fh, struct v4l2_audioout *vout)
1095 struct vivid_dev *dev = video_drvdata(file);
1097 if (!vivid_is_svid_out(dev))
1099 *vout = vivid_audio_outputs[dev->tv_audio_output];
1103 int vidioc_s_audout(struct file *file, void *fh, const struct v4l2_audioout *vout)
1105 struct vivid_dev *dev = video_drvdata(file);
1107 if (!vivid_is_svid_out(dev))
1109 if (vout->index >= ARRAY_SIZE(vivid_audio_outputs))
1111 dev->tv_audio_output = vout->index;
1115 int vivid_vid_out_s_std(struct file *file, void *priv, v4l2_std_id id)
1117 struct vivid_dev *dev = video_drvdata(file);
1119 if (!vivid_is_svid_out(dev))
1121 if (dev->std_out == id)
1123 if (vb2_is_busy(&dev->vb_vid_out_q) || vb2_is_busy(&dev->vb_vbi_out_q))
1126 vivid_update_format_out(dev);
1130 static bool valid_cvt_gtf_timings(struct v4l2_dv_timings *timings)
1132 struct v4l2_bt_timings *bt = &timings->bt;
1134 if ((bt->standards & (V4L2_DV_BT_STD_CVT | V4L2_DV_BT_STD_GTF)) &&
1135 v4l2_valid_dv_timings(timings, &vivid_dv_timings_cap, NULL, NULL))
1141 int vivid_vid_out_s_dv_timings(struct file *file, void *_fh,
1142 struct v4l2_dv_timings *timings)
1144 struct vivid_dev *dev = video_drvdata(file);
1145 if (!vivid_is_hdmi_out(dev))
1147 if (!v4l2_find_dv_timings_cap(timings, &vivid_dv_timings_cap,
1149 !valid_cvt_gtf_timings(timings))
1151 if (v4l2_match_dv_timings(timings, &dev->dv_timings_out, 0))
1153 if (vb2_is_busy(&dev->vb_vid_out_q))
1155 dev->dv_timings_out = *timings;
1156 vivid_update_format_out(dev);
1160 int vivid_vid_out_g_parm(struct file *file, void *priv,
1161 struct v4l2_streamparm *parm)
1163 struct vivid_dev *dev = video_drvdata(file);
1165 if (parm->type != (dev->multiplanar ?
1166 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE :
1167 V4L2_BUF_TYPE_VIDEO_OUTPUT))
1170 parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
1171 parm->parm.output.timeperframe = dev->timeperframe_vid_out;
1172 parm->parm.output.writebuffers = 1;
1177 int vidioc_subscribe_event(struct v4l2_fh *fh,
1178 const struct v4l2_event_subscription *sub)
1180 switch (sub->type) {
1181 case V4L2_EVENT_CTRL:
1182 return v4l2_ctrl_subscribe_event(fh, sub);
1183 case V4L2_EVENT_SOURCE_CHANGE:
1184 if (fh->vdev->vfl_dir == VFL_DIR_RX)
1185 return v4l2_src_change_event_subscribe(fh, sub);