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 void *parg,
35 unsigned *nbuffers, unsigned *nplanes,
36 unsigned sizes[], void *alloc_ctxs[])
38 const struct v4l2_format *fmt = parg;
39 struct vivid_dev *dev = vb2_get_drv_priv(vq);
40 const struct vivid_fmt *vfmt = dev->fmt_out;
41 unsigned planes = dev->fmt_out_metadata_height ? vfmt->buffers : (vfmt->buffers - 1);
42 unsigned h = dev->fmt_out_rect.height;
43 unsigned size = dev->bytesperline_out[0] * (h + dev->fmt_out_metadata_height);
46 for (p = vfmt->buffers; p < planes; p++)
47 size += dev->bytesperline_out[p] * h / vfmt->vdownsampling[p];
49 if (dev->field_out == V4L2_FIELD_ALTERNATE) {
51 * You cannot use write() with FIELD_ALTERNATE since the field
52 * information (TOP/BOTTOM) cannot be passed to the kernel.
54 if (vb2_fileio_is_active(vq))
58 if (dev->queue_setup_error) {
60 * Error injection: test what happens if queue_setup() returns
63 dev->queue_setup_error = false;
68 const struct v4l2_pix_format_mplane *mp;
69 struct v4l2_format mp_fmt;
71 if (!V4L2_TYPE_IS_MULTIPLANAR(fmt->type)) {
72 fmt_sp2mp(fmt, &mp_fmt);
75 mp = &fmt->fmt.pix_mp;
77 * Check if the number of planes in the specified format match
78 * the number of planes in the current format. You can't mix that.
80 if (mp->num_planes != planes)
82 sizes[0] = mp->plane_fmt[0].sizeimage;
85 for (p = 1; p < planes; p++) {
86 sizes[p] = mp->plane_fmt[p].sizeimage;
87 if (sizes[p] < dev->bytesperline_out[p] * h)
91 for (p = 0; p < planes; p++)
92 sizes[p] = p ? dev->bytesperline_out[p] * h : size;
95 if (vq->num_buffers + *nbuffers < 2)
96 *nbuffers = 2 - vq->num_buffers;
101 * videobuf2-vmalloc allocator is context-less so no need to set
105 dprintk(dev, 1, "%s: count=%d\n", __func__, *nbuffers);
106 for (p = 0; p < planes; p++)
107 dprintk(dev, 1, "%s: size[%u]=%u\n", __func__, p, sizes[p]);
111 static int vid_out_buf_prepare(struct vb2_buffer *vb)
113 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
114 struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
119 dprintk(dev, 1, "%s\n", __func__);
121 if (WARN_ON(NULL == dev->fmt_out))
124 planes = dev->fmt_out->planes;
125 planes -= dev->fmt_out_metadata_height ? 0 : 1;
127 if (dev->buf_prepare_error) {
129 * Error injection: test what happens if buf_prepare() returns
132 dev->buf_prepare_error = false;
136 if (dev->field_out != V4L2_FIELD_ALTERNATE)
137 vbuf->field = dev->field_out;
138 else if (vbuf->field != V4L2_FIELD_TOP &&
139 vbuf->field != V4L2_FIELD_BOTTOM)
142 for (p = 0; p < planes; p++) {
143 size = dev->bytesperline_out[p] * dev->fmt_out_rect.height +
144 vb->planes[p].data_offset;
146 if (vb2_get_plane_payload(vb, p) < size) {
147 dprintk(dev, 1, "%s the payload is too small for plane %u (%lu < %lu)\n",
148 __func__, p, vb2_get_plane_payload(vb, p), size);
156 static void vid_out_buf_queue(struct vb2_buffer *vb)
158 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
159 struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
160 struct vivid_buffer *buf = container_of(vbuf, struct vivid_buffer, vb);
162 dprintk(dev, 1, "%s\n", __func__);
164 spin_lock(&dev->slock);
165 list_add_tail(&buf->list, &dev->vid_out_active);
166 spin_unlock(&dev->slock);
169 static int vid_out_start_streaming(struct vb2_queue *vq, unsigned count)
171 struct vivid_dev *dev = vb2_get_drv_priv(vq);
174 if (vb2_is_streaming(&dev->vb_vid_cap_q))
175 dev->can_loop_video = vivid_vid_can_loop(dev);
177 if (dev->kthread_vid_out)
180 dev->vid_out_seq_count = 0;
181 dprintk(dev, 1, "%s\n", __func__);
182 if (dev->start_streaming_error) {
183 dev->start_streaming_error = false;
186 err = vivid_start_generating_vid_out(dev, &dev->vid_out_streaming);
189 struct vivid_buffer *buf, *tmp;
191 list_for_each_entry_safe(buf, tmp, &dev->vid_out_active, list) {
192 list_del(&buf->list);
193 vb2_buffer_done(&buf->vb.vb2_buf,
194 VB2_BUF_STATE_QUEUED);
200 /* abort streaming and wait for last buffer */
201 static void vid_out_stop_streaming(struct vb2_queue *vq)
203 struct vivid_dev *dev = vb2_get_drv_priv(vq);
205 dprintk(dev, 1, "%s\n", __func__);
206 vivid_stop_generating_vid_out(dev, &dev->vid_out_streaming);
207 dev->can_loop_video = false;
210 const struct vb2_ops vivid_vid_out_qops = {
211 .queue_setup = vid_out_queue_setup,
212 .buf_prepare = vid_out_buf_prepare,
213 .buf_queue = vid_out_buf_queue,
214 .start_streaming = vid_out_start_streaming,
215 .stop_streaming = vid_out_stop_streaming,
216 .wait_prepare = vb2_ops_wait_prepare,
217 .wait_finish = vb2_ops_wait_finish,
221 * Called whenever the format has to be reset which can occur when
222 * changing outputs, standard, timings, etc.
224 void vivid_update_format_out(struct vivid_dev *dev)
226 struct v4l2_bt_timings *bt = &dev->dv_timings_out.bt;
228 unsigned packedpixels = dev->fmt_out->packedpixels;
230 switch (dev->output_type[dev->output]) {
233 dev->field_out = dev->tv_field_out;
234 dev->sink_rect.width = 720;
235 if (dev->std_out & V4L2_STD_525_60) {
236 dev->sink_rect.height = 480;
237 dev->timeperframe_vid_out = (struct v4l2_fract) { 1001, 30000 };
238 dev->service_set_out = V4L2_SLICED_CAPTION_525;
240 dev->sink_rect.height = 576;
241 dev->timeperframe_vid_out = (struct v4l2_fract) { 1000, 25000 };
242 dev->service_set_out = V4L2_SLICED_WSS_625 | V4L2_SLICED_TELETEXT_B;
244 dev->colorspace_out = V4L2_COLORSPACE_SMPTE170M;
247 dev->sink_rect.width = bt->width;
248 dev->sink_rect.height = bt->height;
249 size = V4L2_DV_BT_FRAME_WIDTH(bt) * V4L2_DV_BT_FRAME_HEIGHT(bt);
250 dev->timeperframe_vid_out = (struct v4l2_fract) {
251 size / 100, (u32)bt->pixelclock / 100
254 dev->field_out = V4L2_FIELD_ALTERNATE;
256 dev->field_out = V4L2_FIELD_NONE;
257 if (!dev->dvi_d_out && (bt->flags & V4L2_DV_FL_IS_CE_VIDEO)) {
258 if (bt->width == 720 && bt->height <= 576)
259 dev->colorspace_out = V4L2_COLORSPACE_SMPTE170M;
261 dev->colorspace_out = V4L2_COLORSPACE_REC709;
263 dev->colorspace_out = V4L2_COLORSPACE_SRGB;
267 dev->xfer_func_out = V4L2_XFER_FUNC_DEFAULT;
268 dev->ycbcr_enc_out = V4L2_YCBCR_ENC_DEFAULT;
269 dev->quantization_out = V4L2_QUANTIZATION_DEFAULT;
270 dev->compose_out = dev->sink_rect;
271 dev->compose_bounds_out = dev->sink_rect;
272 dev->crop_out = dev->compose_out;
273 if (V4L2_FIELD_HAS_T_OR_B(dev->field_out))
274 dev->crop_out.height /= 2;
275 dev->fmt_out_rect = dev->crop_out;
278 for (p = 0; p < dev->fmt_out->planes; p++)
279 dev->bytesperline_out[p] =
280 (dev->sink_rect.width * dev->fmt_out->bit_depth[p]) /
284 /* Map the field to something that is valid for the current output */
285 static enum v4l2_field vivid_field_out(struct vivid_dev *dev, enum v4l2_field field)
287 if (vivid_is_svid_out(dev)) {
289 case V4L2_FIELD_INTERLACED_TB:
290 case V4L2_FIELD_INTERLACED_BT:
291 case V4L2_FIELD_SEQ_TB:
292 case V4L2_FIELD_SEQ_BT:
293 case V4L2_FIELD_ALTERNATE:
295 case V4L2_FIELD_INTERLACED:
297 return V4L2_FIELD_INTERLACED;
300 if (vivid_is_hdmi_out(dev))
301 return dev->dv_timings_out.bt.interlaced ? V4L2_FIELD_ALTERNATE :
303 return V4L2_FIELD_NONE;
306 static enum tpg_pixel_aspect vivid_get_pixel_aspect(const struct vivid_dev *dev)
308 if (vivid_is_svid_out(dev))
309 return (dev->std_out & V4L2_STD_525_60) ?
310 TPG_PIXEL_ASPECT_NTSC : TPG_PIXEL_ASPECT_PAL;
312 if (vivid_is_hdmi_out(dev) &&
313 dev->sink_rect.width == 720 && dev->sink_rect.height <= 576)
314 return dev->sink_rect.height == 480 ?
315 TPG_PIXEL_ASPECT_NTSC : TPG_PIXEL_ASPECT_PAL;
317 return TPG_PIXEL_ASPECT_SQUARE;
320 int vivid_g_fmt_vid_out(struct file *file, void *priv,
321 struct v4l2_format *f)
323 struct vivid_dev *dev = video_drvdata(file);
324 struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
325 const struct vivid_fmt *fmt = dev->fmt_out;
328 mp->width = dev->fmt_out_rect.width;
329 mp->height = dev->fmt_out_rect.height;
330 mp->field = dev->field_out;
331 mp->pixelformat = fmt->fourcc;
332 mp->colorspace = dev->colorspace_out;
333 mp->xfer_func = dev->xfer_func_out;
334 mp->ycbcr_enc = dev->ycbcr_enc_out;
335 mp->quantization = dev->quantization_out;
336 mp->num_planes = dev->fmt_out_metadata_height ? fmt->buffers : (fmt->buffers - 1);
337 mp->metadata_height = dev->fmt_out_metadata_height;
338 for (p = 0; p < mp->num_planes; p++) {
339 mp->plane_fmt[p].bytesperline = dev->bytesperline_out[p];
340 mp->plane_fmt[p].sizeimage =
341 mp->plane_fmt[p].bytesperline * dev->height_out[p];
343 for (p = fmt->buffers; p < fmt->planes; p++) {
344 unsigned stride = dev->bytesperline_out[p];
346 mp->plane_fmt[0].sizeimage +=
347 (stride * mp->height) / fmt->vdownsampling[p];
352 int vivid_try_fmt_vid_out(struct file *file, void *priv,
353 struct v4l2_format *f)
355 struct vivid_dev *dev = video_drvdata(file);
356 struct v4l2_bt_timings *bt = &dev->dv_timings_out.bt;
357 struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
358 struct v4l2_plane_pix_format *pfmt = mp->plane_fmt;
359 const struct vivid_fmt *fmt;
360 unsigned bytesperline, max_bpl;
364 unsigned packedpixels;
366 fmt = vivid_get_format(dev, mp->pixelformat);
368 dprintk(dev, 1, "Fourcc format (0x%08x) unknown.\n",
370 mp->pixelformat = V4L2_PIX_FMT_YUYV;
371 fmt = vivid_get_format(dev, mp->pixelformat);
373 packedpixels = fmt->packedpixels;
377 mp->field = vivid_field_out(dev, mp->field);
378 if (vivid_is_svid_out(dev)) {
380 h = (dev->std_out & V4L2_STD_525_60) ? 480 : 576;
382 w = dev->sink_rect.width;
383 h = dev->sink_rect.height;
385 if (V4L2_FIELD_HAS_T_OR_B(mp->field))
387 if (!dev->has_scaler_out && !dev->has_crop_out && !dev->has_compose_out) {
389 mp->height = h / factor;
391 struct v4l2_rect r = { 0, 0, mp->width, mp->height * factor };
393 rect_set_min_size(&r, &vivid_min_rect);
394 rect_set_max_size(&r, &vivid_max_rect);
395 if (dev->has_scaler_out && !dev->has_crop_out) {
396 struct v4l2_rect max_r = { 0, 0, MAX_ZOOM * w, MAX_ZOOM * h };
398 rect_set_max_size(&r, &max_r);
399 } else if (!dev->has_scaler_out && dev->has_compose_out && !dev->has_crop_out) {
400 rect_set_max_size(&r, &dev->sink_rect);
401 } else if (!dev->has_scaler_out && !dev->has_compose_out) {
402 rect_set_min_size(&r, &dev->sink_rect);
405 mp->height = r.height / factor;
408 /* clip metadata height maximum value */
409 if (mp->metadata_height > MAX_METADATA_HEIGHT)
410 mp->metadata_height = MAX_METADATA_HEIGHT;
412 /* This driver supports custom bytesperline values */
414 /* Calculate the minimum supported bytesperline value */
415 bytesperline = ((mp->width * fmt->bit_depth[0]) >> 3) / packedpixels;
416 /* Calculate the maximum supported bytesperline value */
417 max_bpl = (MAX_ZOOM * MAX_WIDTH * fmt->bit_depth[0]) >> 3;
418 mp->num_planes = dev->fmt_out_metadata_height ? fmt->buffers : (fmt->buffers - 1);
419 for (p = 0; p < mp->num_planes; p++) {
420 if (pfmt[p].bytesperline > max_bpl)
421 pfmt[p].bytesperline = max_bpl;
422 if (pfmt[p].bytesperline < bytesperline)
423 pfmt[p].bytesperline = bytesperline;
424 pfmt[p].sizeimage = pfmt[p].bytesperline *
425 (fmt->is_metadata[p] ? mp->metadata_height : mp->height);
426 memset(pfmt[p].reserved, 0, sizeof(pfmt[p].reserved));
428 for (p = fmt->buffers; p < fmt->planes; p++)
429 pfmt[0].sizeimage += (pfmt[0].bytesperline * fmt->bit_depth[p]) /
430 (fmt->bit_depth[0] * fmt->vdownsampling[p]);
431 mp->xfer_func = V4L2_XFER_FUNC_DEFAULT;
432 mp->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
433 mp->quantization = V4L2_QUANTIZATION_DEFAULT;
434 if (vivid_is_svid_out(dev)) {
435 mp->colorspace = V4L2_COLORSPACE_SMPTE170M;
436 } else if (dev->dvi_d_out || !(bt->flags & V4L2_DV_FL_IS_CE_VIDEO)) {
437 mp->colorspace = V4L2_COLORSPACE_SRGB;
439 mp->quantization = V4L2_QUANTIZATION_LIM_RANGE;
440 } else if (bt->width == 720 && bt->height <= 576) {
441 mp->colorspace = V4L2_COLORSPACE_SMPTE170M;
442 } else if (mp->colorspace != V4L2_COLORSPACE_SMPTE170M &&
443 mp->colorspace != V4L2_COLORSPACE_REC709 &&
444 mp->colorspace != V4L2_COLORSPACE_ADOBERGB &&
445 mp->colorspace != V4L2_COLORSPACE_BT2020 &&
446 mp->colorspace != V4L2_COLORSPACE_SRGB) {
447 mp->colorspace = V4L2_COLORSPACE_REC709;
449 memset(mp->reserved, 0, sizeof(mp->reserved));
453 int vivid_s_fmt_vid_out(struct file *file, void *priv,
454 struct v4l2_format *f)
456 struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
457 struct vivid_dev *dev = video_drvdata(file);
458 struct v4l2_rect *crop = &dev->crop_out;
459 struct v4l2_rect *compose = &dev->compose_out;
460 struct vb2_queue *q = &dev->vb_vid_out_q;
461 int ret = vivid_try_fmt_vid_out(file, priv, f);
468 if (vb2_is_busy(q) &&
469 (vivid_is_svid_out(dev) ||
470 mp->width != dev->fmt_out_rect.width ||
471 mp->height != dev->fmt_out_rect.height ||
472 mp->pixelformat != dev->fmt_out->fourcc ||
473 mp->field != dev->field_out)) {
474 dprintk(dev, 1, "%s device busy\n", __func__);
479 * Allow for changing the colorspace on the fly. Useful for testing
480 * purposes, and it is something that HDMI transmitters are able
486 dev->fmt_out = vivid_get_format(dev, mp->pixelformat);
487 if (V4L2_FIELD_HAS_T_OR_B(mp->field))
490 if (dev->has_scaler_out || dev->has_crop_out || dev->has_compose_out) {
491 struct v4l2_rect r = { 0, 0, mp->width, mp->height };
493 if (dev->has_scaler_out) {
494 if (dev->has_crop_out)
495 rect_map_inside(crop, &r);
498 if (dev->has_compose_out && !dev->has_crop_out) {
499 struct v4l2_rect min_r = {
502 factor * r.height / MAX_ZOOM
504 struct v4l2_rect max_r = {
507 factor * r.height * MAX_ZOOM
510 rect_set_min_size(compose, &min_r);
511 rect_set_max_size(compose, &max_r);
512 rect_map_inside(compose, &dev->compose_bounds_out);
513 } else if (dev->has_compose_out) {
514 struct v4l2_rect min_r = {
516 crop->width / MAX_ZOOM,
517 factor * crop->height / MAX_ZOOM
519 struct v4l2_rect max_r = {
521 crop->width * MAX_ZOOM,
522 factor * crop->height * MAX_ZOOM
525 rect_set_min_size(compose, &min_r);
526 rect_set_max_size(compose, &max_r);
527 rect_map_inside(compose, &dev->compose_bounds_out);
529 } else if (dev->has_compose_out && !dev->has_crop_out) {
530 rect_set_size_to(crop, &r);
532 rect_set_size_to(compose, &r);
533 rect_map_inside(compose, &dev->compose_bounds_out);
534 } else if (!dev->has_compose_out) {
535 rect_map_inside(crop, &r);
537 rect_set_size_to(compose, &r);
540 rect_set_max_size(compose, &r);
541 rect_map_inside(compose, &dev->compose_bounds_out);
543 crop->height *= factor;
544 rect_set_size_to(crop, compose);
545 rect_map_inside(crop, &r);
547 crop->height /= factor;
550 struct v4l2_rect r = { 0, 0, mp->width, mp->height };
552 rect_set_size_to(crop, &r);
554 rect_set_size_to(compose, &r);
557 dev->fmt_out_rect.width = mp->width;
558 dev->fmt_out_rect.height = mp->height;
559 for (p = 0; p < mp->num_planes; p++) {
560 dev->bytesperline_out[p] = mp->plane_fmt[p].bytesperline;
561 dev->height_out[p] = dev->fmt_out->is_metadata[p] ?
562 mp->metadata_height : mp->height;
564 dev->fmt_out_metadata_height = mp->metadata_height;
565 for (p = dev->fmt_out->buffers; p < dev->fmt_out->planes; p++)
566 dev->bytesperline_out[p] =
567 (dev->bytesperline_out[0] * dev->fmt_out->bit_depth[p]) /
568 dev->fmt_out->bit_depth[0];
569 dev->field_out = mp->field;
570 if (vivid_is_svid_out(dev))
571 dev->tv_field_out = mp->field;
574 dev->colorspace_out = mp->colorspace;
575 dev->xfer_func_out = mp->xfer_func;
576 dev->ycbcr_enc_out = mp->ycbcr_enc;
577 dev->quantization_out = mp->quantization;
578 if (dev->loop_video) {
579 vivid_send_source_change(dev, SVID);
580 vivid_send_source_change(dev, HDMI);
585 int vidioc_g_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_g_fmt_vid_out(file, priv, f);
595 int vidioc_try_fmt_vid_out_mplane(struct file *file, void *priv,
596 struct v4l2_format *f)
598 struct vivid_dev *dev = video_drvdata(file);
600 if (!dev->multiplanar)
602 return vivid_try_fmt_vid_out(file, priv, f);
605 int vidioc_s_fmt_vid_out_mplane(struct file *file, void *priv,
606 struct v4l2_format *f)
608 struct vivid_dev *dev = video_drvdata(file);
610 if (!dev->multiplanar)
612 return vivid_s_fmt_vid_out(file, priv, f);
615 int vidioc_g_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_g_fmt_vid_out);
625 int vidioc_try_fmt_vid_out(struct file *file, void *priv,
626 struct v4l2_format *f)
628 struct vivid_dev *dev = video_drvdata(file);
630 if (dev->multiplanar)
632 return fmt_sp2mp_func(file, priv, f, vivid_try_fmt_vid_out);
635 int vidioc_s_fmt_vid_out(struct file *file, void *priv,
636 struct v4l2_format *f)
638 struct vivid_dev *dev = video_drvdata(file);
640 if (dev->multiplanar)
642 return fmt_sp2mp_func(file, priv, f, vivid_s_fmt_vid_out);
645 int vivid_vid_out_g_selection(struct file *file, void *priv,
646 struct v4l2_selection *sel)
648 struct vivid_dev *dev = video_drvdata(file);
650 if (!dev->has_crop_out && !dev->has_compose_out)
652 if (sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
655 sel->r.left = sel->r.top = 0;
656 switch (sel->target) {
657 case V4L2_SEL_TGT_CROP:
658 if (!dev->has_crop_out)
660 sel->r = dev->crop_out;
662 case V4L2_SEL_TGT_CROP_DEFAULT:
663 if (!dev->has_crop_out)
665 sel->r = dev->fmt_out_rect;
667 case V4L2_SEL_TGT_CROP_BOUNDS:
668 if (!dev->has_crop_out)
670 sel->r = vivid_max_rect;
672 case V4L2_SEL_TGT_COMPOSE:
673 if (!dev->has_compose_out)
675 sel->r = dev->compose_out;
677 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
678 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
679 if (!dev->has_compose_out)
681 sel->r = dev->sink_rect;
689 int vivid_vid_out_s_selection(struct file *file, void *fh, struct v4l2_selection *s)
691 struct vivid_dev *dev = video_drvdata(file);
692 struct v4l2_rect *crop = &dev->crop_out;
693 struct v4l2_rect *compose = &dev->compose_out;
694 unsigned factor = V4L2_FIELD_HAS_T_OR_B(dev->field_out) ? 2 : 1;
697 if (!dev->has_crop_out && !dev->has_compose_out)
699 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
703 case V4L2_SEL_TGT_CROP:
704 if (!dev->has_crop_out)
706 ret = vivid_vid_adjust_sel(s->flags, &s->r);
709 rect_set_min_size(&s->r, &vivid_min_rect);
710 rect_set_max_size(&s->r, &dev->fmt_out_rect);
711 if (dev->has_scaler_out) {
712 struct v4l2_rect max_rect = {
714 dev->sink_rect.width * MAX_ZOOM,
715 (dev->sink_rect.height / factor) * MAX_ZOOM
718 rect_set_max_size(&s->r, &max_rect);
719 if (dev->has_compose_out) {
720 struct v4l2_rect min_rect = {
722 s->r.width / MAX_ZOOM,
723 (s->r.height * factor) / MAX_ZOOM
725 struct v4l2_rect max_rect = {
727 s->r.width * MAX_ZOOM,
728 (s->r.height * factor) * MAX_ZOOM
731 rect_set_min_size(compose, &min_rect);
732 rect_set_max_size(compose, &max_rect);
733 rect_map_inside(compose, &dev->compose_bounds_out);
735 } else if (dev->has_compose_out) {
737 s->r.height *= factor;
738 rect_set_max_size(&s->r, &dev->sink_rect);
739 rect_set_size_to(compose, &s->r);
740 rect_map_inside(compose, &dev->compose_bounds_out);
742 s->r.height /= factor;
744 rect_set_size_to(&s->r, &dev->sink_rect);
745 s->r.height /= factor;
747 rect_map_inside(&s->r, &dev->fmt_out_rect);
750 case V4L2_SEL_TGT_COMPOSE:
751 if (!dev->has_compose_out)
753 ret = vivid_vid_adjust_sel(s->flags, &s->r);
756 rect_set_min_size(&s->r, &vivid_min_rect);
757 rect_set_max_size(&s->r, &dev->sink_rect);
758 rect_map_inside(&s->r, &dev->compose_bounds_out);
760 s->r.height /= factor;
761 if (dev->has_scaler_out) {
762 struct v4l2_rect fmt = dev->fmt_out_rect;
763 struct v4l2_rect max_rect = {
765 s->r.width * MAX_ZOOM,
766 s->r.height * MAX_ZOOM
768 struct v4l2_rect min_rect = {
770 s->r.width / MAX_ZOOM,
771 s->r.height / MAX_ZOOM
774 rect_set_min_size(&fmt, &min_rect);
775 if (!dev->has_crop_out)
776 rect_set_max_size(&fmt, &max_rect);
777 if (!rect_same_size(&dev->fmt_out_rect, &fmt) &&
778 vb2_is_busy(&dev->vb_vid_out_q))
780 if (dev->has_crop_out) {
781 rect_set_min_size(crop, &min_rect);
782 rect_set_max_size(crop, &max_rect);
784 dev->fmt_out_rect = fmt;
785 } else if (dev->has_crop_out) {
786 struct v4l2_rect fmt = dev->fmt_out_rect;
788 rect_set_min_size(&fmt, &s->r);
789 if (!rect_same_size(&dev->fmt_out_rect, &fmt) &&
790 vb2_is_busy(&dev->vb_vid_out_q))
792 dev->fmt_out_rect = fmt;
793 rect_set_size_to(crop, &s->r);
794 rect_map_inside(crop, &dev->fmt_out_rect);
796 if (!rect_same_size(&s->r, &dev->fmt_out_rect) &&
797 vb2_is_busy(&dev->vb_vid_out_q))
799 rect_set_size_to(&dev->fmt_out_rect, &s->r);
800 rect_set_size_to(crop, &s->r);
801 crop->height /= factor;
802 rect_map_inside(crop, &dev->fmt_out_rect);
805 s->r.height *= factor;
806 if (dev->bitmap_out && (compose->width != s->r.width ||
807 compose->height != s->r.height)) {
808 kfree(dev->bitmap_out);
809 dev->bitmap_out = NULL;
820 int vivid_vid_out_cropcap(struct file *file, void *priv,
821 struct v4l2_cropcap *cap)
823 struct vivid_dev *dev = video_drvdata(file);
825 if (cap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
828 switch (vivid_get_pixel_aspect(dev)) {
829 case TPG_PIXEL_ASPECT_NTSC:
830 cap->pixelaspect.numerator = 11;
831 cap->pixelaspect.denominator = 10;
833 case TPG_PIXEL_ASPECT_PAL:
834 cap->pixelaspect.numerator = 54;
835 cap->pixelaspect.denominator = 59;
837 case TPG_PIXEL_ASPECT_SQUARE:
838 cap->pixelaspect.numerator = 1;
839 cap->pixelaspect.denominator = 1;
845 int vidioc_g_fmt_vid_out_overlay(struct file *file, void *priv,
846 struct v4l2_format *f)
848 struct vivid_dev *dev = video_drvdata(file);
849 const struct v4l2_rect *compose = &dev->compose_out;
850 struct v4l2_window *win = &f->fmt.win;
851 unsigned clipcount = win->clipcount;
855 win->w.top = dev->overlay_out_top;
856 win->w.left = dev->overlay_out_left;
857 win->w.width = compose->width;
858 win->w.height = compose->height;
859 win->clipcount = dev->clipcount_out;
860 win->field = V4L2_FIELD_ANY;
861 win->chromakey = dev->chromakey_out;
862 win->global_alpha = dev->global_alpha_out;
863 if (clipcount > dev->clipcount_out)
864 clipcount = dev->clipcount_out;
865 if (dev->bitmap_out == NULL)
867 else if (win->bitmap) {
868 if (copy_to_user(win->bitmap, dev->bitmap_out,
869 ((dev->compose_out.width + 7) / 8) * dev->compose_out.height))
872 if (clipcount && win->clips) {
873 if (copy_to_user(win->clips, dev->clips_out,
874 clipcount * sizeof(dev->clips_out[0])))
880 int vidioc_try_fmt_vid_out_overlay(struct file *file, void *priv,
881 struct v4l2_format *f)
883 struct vivid_dev *dev = video_drvdata(file);
884 const struct v4l2_rect *compose = &dev->compose_out;
885 struct v4l2_window *win = &f->fmt.win;
890 win->w.left = clamp_t(int, win->w.left,
891 -dev->display_width, dev->display_width);
892 win->w.top = clamp_t(int, win->w.top,
893 -dev->display_height, dev->display_height);
894 win->w.width = compose->width;
895 win->w.height = compose->height;
897 * It makes no sense for an OSD to overlay only top or bottom fields,
898 * so always set this to ANY.
900 win->field = V4L2_FIELD_ANY;
901 if (win->clipcount && !win->clips)
903 if (win->clipcount > MAX_CLIPS)
904 win->clipcount = MAX_CLIPS;
905 if (win->clipcount) {
906 if (copy_from_user(dev->try_clips_out, win->clips,
907 win->clipcount * sizeof(dev->clips_out[0])))
909 for (i = 0; i < win->clipcount; i++) {
910 struct v4l2_rect *r = &dev->try_clips_out[i].c;
912 r->top = clamp_t(s32, r->top, 0, dev->display_height - 1);
913 r->height = clamp_t(s32, r->height, 1, dev->display_height - r->top);
914 r->left = clamp_t(u32, r->left, 0, dev->display_width - 1);
915 r->width = clamp_t(u32, r->width, 1, dev->display_width - r->left);
918 * Yeah, so sue me, it's an O(n^2) algorithm. But n is a small
919 * number and it's typically a one-time deal.
921 for (i = 0; i < win->clipcount - 1; i++) {
922 struct v4l2_rect *r1 = &dev->try_clips_out[i].c;
924 for (j = i + 1; j < win->clipcount; j++) {
925 struct v4l2_rect *r2 = &dev->try_clips_out[j].c;
927 if (rect_overlap(r1, r2))
931 if (copy_to_user(win->clips, dev->try_clips_out,
932 win->clipcount * sizeof(dev->clips_out[0])))
938 int vidioc_s_fmt_vid_out_overlay(struct file *file, void *priv,
939 struct v4l2_format *f)
941 struct vivid_dev *dev = video_drvdata(file);
942 const struct v4l2_rect *compose = &dev->compose_out;
943 struct v4l2_window *win = &f->fmt.win;
944 int ret = vidioc_try_fmt_vid_out_overlay(file, priv, f);
945 unsigned bitmap_size = ((compose->width + 7) / 8) * compose->height;
946 unsigned clips_size = win->clipcount * sizeof(dev->clips_out[0]);
947 void *new_bitmap = NULL;
953 new_bitmap = memdup_user(win->bitmap, bitmap_size);
955 if (IS_ERR(new_bitmap))
956 return PTR_ERR(new_bitmap);
959 dev->overlay_out_top = win->w.top;
960 dev->overlay_out_left = win->w.left;
961 kfree(dev->bitmap_out);
962 dev->bitmap_out = new_bitmap;
963 dev->clipcount_out = win->clipcount;
964 if (dev->clipcount_out)
965 memcpy(dev->clips_out, dev->try_clips_out, clips_size);
966 dev->chromakey_out = win->chromakey;
967 dev->global_alpha_out = win->global_alpha;
971 int vivid_vid_out_overlay(struct file *file, void *fh, unsigned i)
973 struct vivid_dev *dev = video_drvdata(file);
975 if (i && !dev->fmt_out->can_do_overlay) {
976 dprintk(dev, 1, "unsupported output format for output overlay\n");
980 dev->overlay_out_enabled = i;
984 int vivid_vid_out_g_fbuf(struct file *file, void *fh,
985 struct v4l2_framebuffer *a)
987 struct vivid_dev *dev = video_drvdata(file);
989 a->capability = V4L2_FBUF_CAP_EXTERNOVERLAY |
990 V4L2_FBUF_CAP_BITMAP_CLIPPING |
991 V4L2_FBUF_CAP_LIST_CLIPPING |
992 V4L2_FBUF_CAP_CHROMAKEY |
993 V4L2_FBUF_CAP_SRC_CHROMAKEY |
994 V4L2_FBUF_CAP_GLOBAL_ALPHA |
995 V4L2_FBUF_CAP_LOCAL_ALPHA |
996 V4L2_FBUF_CAP_LOCAL_INV_ALPHA;
997 a->flags = V4L2_FBUF_FLAG_OVERLAY | dev->fbuf_out_flags;
998 a->base = (void *)dev->video_pbase;
999 a->fmt.width = dev->display_width;
1000 a->fmt.height = dev->display_height;
1001 if (dev->fb_defined.green.length == 5)
1002 a->fmt.pixelformat = V4L2_PIX_FMT_ARGB555;
1004 a->fmt.pixelformat = V4L2_PIX_FMT_RGB565;
1005 a->fmt.bytesperline = dev->display_byte_stride;
1006 a->fmt.sizeimage = a->fmt.height * a->fmt.bytesperline;
1007 a->fmt.field = V4L2_FIELD_NONE;
1008 a->fmt.colorspace = V4L2_COLORSPACE_SRGB;
1013 int vivid_vid_out_s_fbuf(struct file *file, void *fh,
1014 const struct v4l2_framebuffer *a)
1016 struct vivid_dev *dev = video_drvdata(file);
1017 const unsigned chroma_flags = V4L2_FBUF_FLAG_CHROMAKEY |
1018 V4L2_FBUF_FLAG_SRC_CHROMAKEY;
1019 const unsigned alpha_flags = V4L2_FBUF_FLAG_GLOBAL_ALPHA |
1020 V4L2_FBUF_FLAG_LOCAL_ALPHA |
1021 V4L2_FBUF_FLAG_LOCAL_INV_ALPHA;
1024 if ((a->flags & chroma_flags) == chroma_flags)
1026 switch (a->flags & alpha_flags) {
1028 case V4L2_FBUF_FLAG_GLOBAL_ALPHA:
1029 case V4L2_FBUF_FLAG_LOCAL_ALPHA:
1030 case V4L2_FBUF_FLAG_LOCAL_INV_ALPHA:
1035 dev->fbuf_out_flags &= ~(chroma_flags | alpha_flags);
1036 dev->fbuf_out_flags = a->flags & (chroma_flags | alpha_flags);
1040 static const struct v4l2_audioout vivid_audio_outputs[] = {
1041 { 0, "Line-Out 1" },
1042 { 1, "Line-Out 2" },
1045 int vidioc_enum_output(struct file *file, void *priv,
1046 struct v4l2_output *out)
1048 struct vivid_dev *dev = video_drvdata(file);
1050 if (out->index >= dev->num_outputs)
1053 out->type = V4L2_OUTPUT_TYPE_ANALOG;
1054 switch (dev->output_type[out->index]) {
1056 snprintf(out->name, sizeof(out->name), "S-Video %u",
1057 dev->output_name_counter[out->index]);
1058 out->std = V4L2_STD_ALL;
1059 if (dev->has_audio_outputs)
1060 out->audioset = (1 << ARRAY_SIZE(vivid_audio_outputs)) - 1;
1061 out->capabilities = V4L2_OUT_CAP_STD;
1064 snprintf(out->name, sizeof(out->name), "HDMI %u",
1065 dev->output_name_counter[out->index]);
1066 out->capabilities = V4L2_OUT_CAP_DV_TIMINGS;
1072 int vidioc_g_output(struct file *file, void *priv, unsigned *o)
1074 struct vivid_dev *dev = video_drvdata(file);
1080 int vidioc_s_output(struct file *file, void *priv, unsigned o)
1082 struct vivid_dev *dev = video_drvdata(file);
1084 if (o >= dev->num_outputs)
1087 if (o == dev->output)
1090 if (vb2_is_busy(&dev->vb_vid_out_q) || vb2_is_busy(&dev->vb_vbi_out_q))
1094 dev->tv_audio_output = 0;
1095 if (dev->output_type[o] == SVID)
1096 dev->vid_out_dev.tvnorms = V4L2_STD_ALL;
1098 dev->vid_out_dev.tvnorms = 0;
1100 dev->vbi_out_dev.tvnorms = dev->vid_out_dev.tvnorms;
1101 vivid_update_format_out(dev);
1105 int vidioc_enumaudout(struct file *file, void *fh, struct v4l2_audioout *vout)
1107 if (vout->index >= ARRAY_SIZE(vivid_audio_outputs))
1109 *vout = vivid_audio_outputs[vout->index];
1113 int vidioc_g_audout(struct file *file, void *fh, struct v4l2_audioout *vout)
1115 struct vivid_dev *dev = video_drvdata(file);
1117 if (!vivid_is_svid_out(dev))
1119 *vout = vivid_audio_outputs[dev->tv_audio_output];
1123 int vidioc_s_audout(struct file *file, void *fh, const struct v4l2_audioout *vout)
1125 struct vivid_dev *dev = video_drvdata(file);
1127 if (!vivid_is_svid_out(dev))
1129 if (vout->index >= ARRAY_SIZE(vivid_audio_outputs))
1131 dev->tv_audio_output = vout->index;
1135 int vivid_vid_out_s_std(struct file *file, void *priv, v4l2_std_id id)
1137 struct vivid_dev *dev = video_drvdata(file);
1139 if (!vivid_is_svid_out(dev))
1141 if (dev->std_out == id)
1143 if (vb2_is_busy(&dev->vb_vid_out_q) || vb2_is_busy(&dev->vb_vbi_out_q))
1146 vivid_update_format_out(dev);
1150 static bool valid_cvt_gtf_timings(struct v4l2_dv_timings *timings)
1152 struct v4l2_bt_timings *bt = &timings->bt;
1154 if ((bt->standards & (V4L2_DV_BT_STD_CVT | V4L2_DV_BT_STD_GTF)) &&
1155 v4l2_valid_dv_timings(timings, &vivid_dv_timings_cap, NULL, NULL))
1161 int vivid_vid_out_s_dv_timings(struct file *file, void *_fh,
1162 struct v4l2_dv_timings *timings)
1164 struct vivid_dev *dev = video_drvdata(file);
1165 if (!vivid_is_hdmi_out(dev))
1167 if (!v4l2_find_dv_timings_cap(timings, &vivid_dv_timings_cap,
1169 !valid_cvt_gtf_timings(timings))
1171 if (v4l2_match_dv_timings(timings, &dev->dv_timings_out, 0))
1173 if (vb2_is_busy(&dev->vb_vid_out_q))
1175 dev->dv_timings_out = *timings;
1176 vivid_update_format_out(dev);
1180 int vivid_vid_out_g_parm(struct file *file, void *priv,
1181 struct v4l2_streamparm *parm)
1183 struct vivid_dev *dev = video_drvdata(file);
1185 if (parm->type != (dev->multiplanar ?
1186 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE :
1187 V4L2_BUF_TYPE_VIDEO_OUTPUT))
1190 parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
1191 parm->parm.output.timeperframe = dev->timeperframe_vid_out;
1192 parm->parm.output.writebuffers = 1;
1197 int vidioc_subscribe_event(struct v4l2_fh *fh,
1198 const struct v4l2_event_subscription *sub)
1200 switch (sub->type) {
1201 case V4L2_EVENT_CTRL:
1202 return v4l2_ctrl_subscribe_event(fh, sub);
1203 case V4L2_EVENT_SOURCE_CHANGE:
1204 if (fh->vdev->vfl_dir == VFL_DIR_RX)
1205 return v4l2_src_change_event_subscribe(fh, sub);