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 unsigned planes = dev->fmt_out->planes;
40 unsigned h = dev->fmt_out_rect.height;
41 unsigned size = dev->bytesperline_out[0] * h;
43 if (dev->field_out == V4L2_FIELD_ALTERNATE) {
45 * You cannot use write() with FIELD_ALTERNATE since the field
46 * information (TOP/BOTTOM) cannot be passed to the kernel.
48 if (vb2_fileio_is_active(vq))
52 if (dev->queue_setup_error) {
54 * Error injection: test what happens if queue_setup() returns
57 dev->queue_setup_error = false;
62 const struct v4l2_pix_format_mplane *mp;
63 struct v4l2_format mp_fmt;
65 if (!V4L2_TYPE_IS_MULTIPLANAR(fmt->type)) {
66 fmt_sp2mp(fmt, &mp_fmt);
69 mp = &fmt->fmt.pix_mp;
71 * Check if the number of planes in the specified format match
72 * the number of planes in the current format. You can't mix that.
74 if (mp->num_planes != planes)
76 sizes[0] = mp->plane_fmt[0].sizeimage;
78 sizes[1] = mp->plane_fmt[1].sizeimage;
79 if (sizes[0] < dev->bytesperline_out[0] * h ||
80 sizes[1] < dev->bytesperline_out[1] * h)
82 } else if (sizes[0] < size) {
87 sizes[0] = dev->bytesperline_out[0] * h;
88 sizes[1] = dev->bytesperline_out[1] * h;
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
105 dprintk(dev, 1, "%s, count=%d, sizes=%u, %u\n", __func__,
106 *nbuffers, sizes[0], sizes[1]);
108 dprintk(dev, 1, "%s, count=%d, size=%u\n", __func__,
109 *nbuffers, sizes[0]);
113 static int vid_out_buf_prepare(struct vb2_buffer *vb)
115 struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
117 unsigned planes = dev->fmt_out->planes;
120 dprintk(dev, 1, "%s\n", __func__);
122 if (WARN_ON(NULL == dev->fmt_out))
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 vb->v4l2_buf.field = dev->field_out;
136 else if (vb->v4l2_buf.field != V4L2_FIELD_TOP &&
137 vb->v4l2_buf.field != V4L2_FIELD_BOTTOM)
140 for (p = 0; p < planes; p++) {
141 size = dev->bytesperline_out[p] * dev->fmt_out_rect.height +
142 vb->v4l2_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 vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
157 struct vivid_buffer *buf = container_of(vb, struct vivid_buffer, vb);
159 dprintk(dev, 1, "%s\n", __func__);
161 spin_lock(&dev->slock);
162 list_add_tail(&buf->list, &dev->vid_out_active);
163 spin_unlock(&dev->slock);
166 static int vid_out_start_streaming(struct vb2_queue *vq, unsigned count)
168 struct vivid_dev *dev = vb2_get_drv_priv(vq);
171 if (vb2_is_streaming(&dev->vb_vid_cap_q))
172 dev->can_loop_video = vivid_vid_can_loop(dev);
174 if (dev->kthread_vid_out)
177 dev->vid_out_seq_count = 0;
178 dprintk(dev, 1, "%s\n", __func__);
179 if (dev->start_streaming_error) {
180 dev->start_streaming_error = false;
183 err = vivid_start_generating_vid_out(dev, &dev->vid_out_streaming);
186 struct vivid_buffer *buf, *tmp;
188 list_for_each_entry_safe(buf, tmp, &dev->vid_out_active, list) {
189 list_del(&buf->list);
190 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_QUEUED);
196 /* abort streaming and wait for last buffer */
197 static void vid_out_stop_streaming(struct vb2_queue *vq)
199 struct vivid_dev *dev = vb2_get_drv_priv(vq);
201 dprintk(dev, 1, "%s\n", __func__);
202 vivid_stop_generating_vid_out(dev, &dev->vid_out_streaming);
203 dev->can_loop_video = false;
206 const struct vb2_ops vivid_vid_out_qops = {
207 .queue_setup = vid_out_queue_setup,
208 .buf_prepare = vid_out_buf_prepare,
209 .buf_queue = vid_out_buf_queue,
210 .start_streaming = vid_out_start_streaming,
211 .stop_streaming = vid_out_stop_streaming,
212 .wait_prepare = vivid_unlock,
213 .wait_finish = vivid_lock,
217 * Called whenever the format has to be reset which can occur when
218 * changing outputs, standard, timings, etc.
220 void vivid_update_format_out(struct vivid_dev *dev)
222 struct v4l2_bt_timings *bt = &dev->dv_timings_out.bt;
225 switch (dev->output_type[dev->output]) {
228 dev->field_out = dev->tv_field_out;
229 dev->sink_rect.width = 720;
230 if (dev->std_out & V4L2_STD_525_60) {
231 dev->sink_rect.height = 480;
232 dev->timeperframe_vid_out = (struct v4l2_fract) { 1001, 30000 };
233 dev->service_set_out = V4L2_SLICED_CAPTION_525;
235 dev->sink_rect.height = 576;
236 dev->timeperframe_vid_out = (struct v4l2_fract) { 1000, 25000 };
237 dev->service_set_out = V4L2_SLICED_WSS_625 | V4L2_SLICED_TELETEXT_B;
239 dev->colorspace_out = V4L2_COLORSPACE_SMPTE170M;
242 dev->sink_rect.width = bt->width;
243 dev->sink_rect.height = bt->height;
244 size = V4L2_DV_BT_FRAME_WIDTH(bt) * V4L2_DV_BT_FRAME_HEIGHT(bt);
245 dev->timeperframe_vid_out = (struct v4l2_fract) {
246 size / 100, (u32)bt->pixelclock / 100
249 dev->field_out = V4L2_FIELD_ALTERNATE;
251 dev->field_out = V4L2_FIELD_NONE;
252 if (!dev->dvi_d_out && (bt->standards & V4L2_DV_BT_STD_CEA861)) {
253 if (bt->width == 720 && bt->height <= 576)
254 dev->colorspace_out = V4L2_COLORSPACE_SMPTE170M;
256 dev->colorspace_out = V4L2_COLORSPACE_REC709;
258 dev->colorspace_out = V4L2_COLORSPACE_SRGB;
262 dev->compose_out = dev->sink_rect;
263 dev->compose_bounds_out = dev->sink_rect;
264 dev->crop_out = dev->compose_out;
265 if (V4L2_FIELD_HAS_T_OR_B(dev->field_out))
266 dev->crop_out.height /= 2;
267 dev->fmt_out_rect = dev->crop_out;
268 dev->bytesperline_out[0] = (dev->sink_rect.width * dev->fmt_out->depth) / 8;
269 if (dev->fmt_out->planes == 2)
270 dev->bytesperline_out[1] = (dev->sink_rect.width * dev->fmt_out->depth) / 8;
273 /* Map the field to something that is valid for the current output */
274 static enum v4l2_field vivid_field_out(struct vivid_dev *dev, enum v4l2_field field)
276 if (vivid_is_svid_out(dev)) {
278 case V4L2_FIELD_INTERLACED_TB:
279 case V4L2_FIELD_INTERLACED_BT:
280 case V4L2_FIELD_SEQ_TB:
281 case V4L2_FIELD_SEQ_BT:
282 case V4L2_FIELD_ALTERNATE:
284 case V4L2_FIELD_INTERLACED:
286 return V4L2_FIELD_INTERLACED;
289 if (vivid_is_hdmi_out(dev))
290 return dev->dv_timings_out.bt.interlaced ? V4L2_FIELD_ALTERNATE :
292 return V4L2_FIELD_NONE;
295 static enum tpg_pixel_aspect vivid_get_pixel_aspect(const struct vivid_dev *dev)
297 if (vivid_is_svid_out(dev))
298 return (dev->std_out & V4L2_STD_525_60) ?
299 TPG_PIXEL_ASPECT_NTSC : TPG_PIXEL_ASPECT_PAL;
301 if (vivid_is_hdmi_out(dev) &&
302 dev->sink_rect.width == 720 && dev->sink_rect.height <= 576)
303 return dev->sink_rect.height == 480 ?
304 TPG_PIXEL_ASPECT_NTSC : TPG_PIXEL_ASPECT_PAL;
306 return TPG_PIXEL_ASPECT_SQUARE;
309 int vivid_g_fmt_vid_out(struct file *file, void *priv,
310 struct v4l2_format *f)
312 struct vivid_dev *dev = video_drvdata(file);
313 struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
316 mp->width = dev->fmt_out_rect.width;
317 mp->height = dev->fmt_out_rect.height;
318 mp->field = dev->field_out;
319 mp->pixelformat = dev->fmt_out->fourcc;
320 mp->colorspace = dev->colorspace_out;
321 mp->num_planes = dev->fmt_out->planes;
322 for (p = 0; p < mp->num_planes; p++) {
323 mp->plane_fmt[p].bytesperline = dev->bytesperline_out[p];
324 mp->plane_fmt[p].sizeimage =
325 mp->plane_fmt[p].bytesperline * mp->height;
330 int vivid_try_fmt_vid_out(struct file *file, void *priv,
331 struct v4l2_format *f)
333 struct vivid_dev *dev = video_drvdata(file);
334 struct v4l2_bt_timings *bt = &dev->dv_timings_out.bt;
335 struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
336 struct v4l2_plane_pix_format *pfmt = mp->plane_fmt;
337 const struct vivid_fmt *fmt;
338 unsigned bytesperline, max_bpl;
343 fmt = vivid_get_format(dev, mp->pixelformat);
345 dprintk(dev, 1, "Fourcc format (0x%08x) unknown.\n",
347 mp->pixelformat = V4L2_PIX_FMT_YUYV;
348 fmt = vivid_get_format(dev, mp->pixelformat);
351 mp->field = vivid_field_out(dev, mp->field);
352 if (vivid_is_svid_out(dev)) {
354 h = (dev->std_out & V4L2_STD_525_60) ? 480 : 576;
356 w = dev->sink_rect.width;
357 h = dev->sink_rect.height;
359 if (V4L2_FIELD_HAS_T_OR_B(mp->field))
361 if (!dev->has_scaler_out && !dev->has_crop_out && !dev->has_compose_out) {
363 mp->height = h / factor;
365 struct v4l2_rect r = { 0, 0, mp->width, mp->height * factor };
367 rect_set_min_size(&r, &vivid_min_rect);
368 rect_set_max_size(&r, &vivid_max_rect);
369 if (dev->has_scaler_out && !dev->has_crop_out) {
370 struct v4l2_rect max_r = { 0, 0, MAX_ZOOM * w, MAX_ZOOM * h };
372 rect_set_max_size(&r, &max_r);
373 } else if (!dev->has_scaler_out && dev->has_compose_out && !dev->has_crop_out) {
374 rect_set_max_size(&r, &dev->sink_rect);
375 } else if (!dev->has_scaler_out && !dev->has_compose_out) {
376 rect_set_min_size(&r, &dev->sink_rect);
379 mp->height = r.height / factor;
382 /* This driver supports custom bytesperline values */
384 /* Calculate the minimum supported bytesperline value */
385 bytesperline = (mp->width * fmt->depth) >> 3;
386 /* Calculate the maximum supported bytesperline value */
387 max_bpl = (MAX_ZOOM * MAX_WIDTH * fmt->depth) >> 3;
388 mp->num_planes = fmt->planes;
389 for (p = 0; p < mp->num_planes; p++) {
390 if (pfmt[p].bytesperline > max_bpl)
391 pfmt[p].bytesperline = max_bpl;
392 if (pfmt[p].bytesperline < bytesperline)
393 pfmt[p].bytesperline = bytesperline;
394 pfmt[p].sizeimage = pfmt[p].bytesperline * mp->height;
395 memset(pfmt[p].reserved, 0, sizeof(pfmt[p].reserved));
397 if (vivid_is_svid_out(dev))
398 mp->colorspace = V4L2_COLORSPACE_SMPTE170M;
399 else if (dev->dvi_d_out || !(bt->standards & V4L2_DV_BT_STD_CEA861))
400 mp->colorspace = V4L2_COLORSPACE_SRGB;
401 else if (bt->width == 720 && bt->height <= 576)
402 mp->colorspace = V4L2_COLORSPACE_SMPTE170M;
403 else if (mp->colorspace != V4L2_COLORSPACE_SMPTE170M &&
404 mp->colorspace != V4L2_COLORSPACE_REC709 &&
405 mp->colorspace != V4L2_COLORSPACE_SRGB)
406 mp->colorspace = V4L2_COLORSPACE_REC709;
407 memset(mp->reserved, 0, sizeof(mp->reserved));
411 int vivid_s_fmt_vid_out(struct file *file, void *priv,
412 struct v4l2_format *f)
414 struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
415 struct vivid_dev *dev = video_drvdata(file);
416 struct v4l2_rect *crop = &dev->crop_out;
417 struct v4l2_rect *compose = &dev->compose_out;
418 struct vb2_queue *q = &dev->vb_vid_out_q;
419 int ret = vivid_try_fmt_vid_out(file, priv, f);
425 if (vb2_is_busy(q) &&
426 (vivid_is_svid_out(dev) ||
427 mp->width != dev->fmt_out_rect.width ||
428 mp->height != dev->fmt_out_rect.height ||
429 mp->pixelformat != dev->fmt_out->fourcc ||
430 mp->field != dev->field_out)) {
431 dprintk(dev, 1, "%s device busy\n", __func__);
436 * Allow for changing the colorspace on the fly. Useful for testing
437 * purposes, and it is something that HDMI transmitters are able
443 dev->fmt_out = vivid_get_format(dev, mp->pixelformat);
444 if (V4L2_FIELD_HAS_T_OR_B(mp->field))
447 if (dev->has_scaler_out || dev->has_crop_out || dev->has_compose_out) {
448 struct v4l2_rect r = { 0, 0, mp->width, mp->height };
450 if (dev->has_scaler_out) {
451 if (dev->has_crop_out)
452 rect_map_inside(crop, &r);
455 if (dev->has_compose_out && !dev->has_crop_out) {
456 struct v4l2_rect min_r = {
459 factor * r.height / MAX_ZOOM
461 struct v4l2_rect max_r = {
464 factor * r.height * MAX_ZOOM
467 rect_set_min_size(compose, &min_r);
468 rect_set_max_size(compose, &max_r);
469 rect_map_inside(compose, &dev->compose_bounds_out);
470 } else if (dev->has_compose_out) {
471 struct v4l2_rect min_r = {
473 crop->width / MAX_ZOOM,
474 factor * crop->height / MAX_ZOOM
476 struct v4l2_rect max_r = {
478 crop->width * MAX_ZOOM,
479 factor * crop->height * MAX_ZOOM
482 rect_set_min_size(compose, &min_r);
483 rect_set_max_size(compose, &max_r);
484 rect_map_inside(compose, &dev->compose_bounds_out);
486 } else if (dev->has_compose_out && !dev->has_crop_out) {
487 rect_set_size_to(crop, &r);
489 rect_set_size_to(compose, &r);
490 rect_map_inside(compose, &dev->compose_bounds_out);
491 } else if (!dev->has_compose_out) {
492 rect_map_inside(crop, &r);
494 rect_set_size_to(compose, &r);
497 rect_set_max_size(compose, &r);
498 rect_map_inside(compose, &dev->compose_bounds_out);
500 crop->height *= factor;
501 rect_set_size_to(crop, compose);
502 rect_map_inside(crop, &r);
504 crop->height /= factor;
507 struct v4l2_rect r = { 0, 0, mp->width, mp->height };
509 rect_set_size_to(crop, &r);
511 rect_set_size_to(compose, &r);
514 dev->fmt_out_rect.width = mp->width;
515 dev->fmt_out_rect.height = mp->height;
516 dev->bytesperline_out[0] = mp->plane_fmt[0].bytesperline;
517 if (mp->num_planes > 1)
518 dev->bytesperline_out[1] = mp->plane_fmt[1].bytesperline;
519 dev->field_out = mp->field;
520 if (vivid_is_svid_out(dev))
521 dev->tv_field_out = mp->field;
524 dev->colorspace_out = mp->colorspace;
525 if (dev->loop_video) {
526 vivid_send_source_change(dev, SVID);
527 vivid_send_source_change(dev, HDMI);
532 int vidioc_g_fmt_vid_out_mplane(struct file *file, void *priv,
533 struct v4l2_format *f)
535 struct vivid_dev *dev = video_drvdata(file);
537 if (!dev->multiplanar)
539 return vivid_g_fmt_vid_out(file, priv, f);
542 int vidioc_try_fmt_vid_out_mplane(struct file *file, void *priv,
543 struct v4l2_format *f)
545 struct vivid_dev *dev = video_drvdata(file);
547 if (!dev->multiplanar)
549 return vivid_try_fmt_vid_out(file, priv, f);
552 int vidioc_s_fmt_vid_out_mplane(struct file *file, void *priv,
553 struct v4l2_format *f)
555 struct vivid_dev *dev = video_drvdata(file);
557 if (!dev->multiplanar)
559 return vivid_s_fmt_vid_out(file, priv, f);
562 int vidioc_g_fmt_vid_out(struct file *file, void *priv,
563 struct v4l2_format *f)
565 struct vivid_dev *dev = video_drvdata(file);
567 if (dev->multiplanar)
569 return fmt_sp2mp_func(file, priv, f, vivid_g_fmt_vid_out);
572 int vidioc_try_fmt_vid_out(struct file *file, void *priv,
573 struct v4l2_format *f)
575 struct vivid_dev *dev = video_drvdata(file);
577 if (dev->multiplanar)
579 return fmt_sp2mp_func(file, priv, f, vivid_try_fmt_vid_out);
582 int vidioc_s_fmt_vid_out(struct file *file, void *priv,
583 struct v4l2_format *f)
585 struct vivid_dev *dev = video_drvdata(file);
587 if (dev->multiplanar)
589 return fmt_sp2mp_func(file, priv, f, vivid_s_fmt_vid_out);
592 int vivid_vid_out_g_selection(struct file *file, void *priv,
593 struct v4l2_selection *sel)
595 struct vivid_dev *dev = video_drvdata(file);
597 if (!dev->has_crop_out && !dev->has_compose_out)
599 if (sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
602 sel->r.left = sel->r.top = 0;
603 switch (sel->target) {
604 case V4L2_SEL_TGT_CROP:
605 if (!dev->has_crop_out)
607 sel->r = dev->crop_out;
609 case V4L2_SEL_TGT_CROP_DEFAULT:
610 if (!dev->has_crop_out)
612 sel->r = dev->fmt_out_rect;
614 case V4L2_SEL_TGT_CROP_BOUNDS:
615 if (!dev->has_compose_out)
617 sel->r = vivid_max_rect;
619 case V4L2_SEL_TGT_COMPOSE:
620 if (!dev->has_compose_out)
622 sel->r = dev->compose_out;
624 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
625 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
626 if (!dev->has_compose_out)
628 sel->r = dev->sink_rect;
636 int vivid_vid_out_s_selection(struct file *file, void *fh, struct v4l2_selection *s)
638 struct vivid_dev *dev = video_drvdata(file);
639 struct v4l2_rect *crop = &dev->crop_out;
640 struct v4l2_rect *compose = &dev->compose_out;
641 unsigned factor = V4L2_FIELD_HAS_T_OR_B(dev->field_out) ? 2 : 1;
644 if (!dev->has_crop_out && !dev->has_compose_out)
646 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
650 case V4L2_SEL_TGT_CROP:
651 if (!dev->has_crop_out)
653 ret = vivid_vid_adjust_sel(s->flags, &s->r);
656 rect_set_min_size(&s->r, &vivid_min_rect);
657 rect_set_max_size(&s->r, &dev->fmt_out_rect);
658 if (dev->has_scaler_out) {
659 struct v4l2_rect max_rect = {
661 dev->sink_rect.width * MAX_ZOOM,
662 (dev->sink_rect.height / factor) * MAX_ZOOM
665 rect_set_max_size(&s->r, &max_rect);
666 if (dev->has_compose_out) {
667 struct v4l2_rect min_rect = {
669 s->r.width / MAX_ZOOM,
670 (s->r.height * factor) / MAX_ZOOM
672 struct v4l2_rect max_rect = {
674 s->r.width * MAX_ZOOM,
675 (s->r.height * factor) * MAX_ZOOM
678 rect_set_min_size(compose, &min_rect);
679 rect_set_max_size(compose, &max_rect);
680 rect_map_inside(compose, &dev->compose_bounds_out);
682 } else if (dev->has_compose_out) {
684 s->r.height *= factor;
685 rect_set_max_size(&s->r, &dev->sink_rect);
686 rect_set_size_to(compose, &s->r);
687 rect_map_inside(compose, &dev->compose_bounds_out);
689 s->r.height /= factor;
691 rect_set_size_to(&s->r, &dev->sink_rect);
692 s->r.height /= factor;
694 rect_map_inside(&s->r, &dev->fmt_out_rect);
697 case V4L2_SEL_TGT_COMPOSE:
698 if (!dev->has_compose_out)
700 ret = vivid_vid_adjust_sel(s->flags, &s->r);
703 rect_set_min_size(&s->r, &vivid_min_rect);
704 rect_set_max_size(&s->r, &dev->sink_rect);
705 rect_map_inside(&s->r, &dev->compose_bounds_out);
707 s->r.height /= factor;
708 if (dev->has_scaler_out) {
709 struct v4l2_rect fmt = dev->fmt_out_rect;
710 struct v4l2_rect max_rect = {
712 s->r.width * MAX_ZOOM,
713 s->r.height * MAX_ZOOM
715 struct v4l2_rect min_rect = {
717 s->r.width / MAX_ZOOM,
718 s->r.height / MAX_ZOOM
721 rect_set_min_size(&fmt, &min_rect);
722 if (!dev->has_crop_out)
723 rect_set_max_size(&fmt, &max_rect);
724 if (!rect_same_size(&dev->fmt_out_rect, &fmt) &&
725 vb2_is_busy(&dev->vb_vid_out_q))
727 if (dev->has_crop_out) {
728 rect_set_min_size(crop, &min_rect);
729 rect_set_max_size(crop, &max_rect);
731 dev->fmt_out_rect = fmt;
732 } else if (dev->has_crop_out) {
733 struct v4l2_rect fmt = dev->fmt_out_rect;
735 rect_set_min_size(&fmt, &s->r);
736 if (!rect_same_size(&dev->fmt_out_rect, &fmt) &&
737 vb2_is_busy(&dev->vb_vid_out_q))
739 dev->fmt_out_rect = fmt;
740 rect_set_size_to(crop, &s->r);
741 rect_map_inside(crop, &dev->fmt_out_rect);
743 if (!rect_same_size(&s->r, &dev->fmt_out_rect) &&
744 vb2_is_busy(&dev->vb_vid_out_q))
746 rect_set_size_to(&dev->fmt_out_rect, &s->r);
747 rect_set_size_to(crop, &s->r);
748 crop->height /= factor;
749 rect_map_inside(crop, &dev->fmt_out_rect);
752 s->r.height *= factor;
753 if (dev->bitmap_out && (compose->width != s->r.width ||
754 compose->height != s->r.height)) {
755 kfree(dev->bitmap_out);
756 dev->bitmap_out = NULL;
767 int vivid_vid_out_cropcap(struct file *file, void *priv,
768 struct v4l2_cropcap *cap)
770 struct vivid_dev *dev = video_drvdata(file);
772 if (cap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
775 switch (vivid_get_pixel_aspect(dev)) {
776 case TPG_PIXEL_ASPECT_NTSC:
777 cap->pixelaspect.numerator = 11;
778 cap->pixelaspect.denominator = 10;
780 case TPG_PIXEL_ASPECT_PAL:
781 cap->pixelaspect.numerator = 54;
782 cap->pixelaspect.denominator = 59;
784 case TPG_PIXEL_ASPECT_SQUARE:
785 cap->pixelaspect.numerator = 1;
786 cap->pixelaspect.denominator = 1;
792 int vidioc_g_fmt_vid_out_overlay(struct file *file, void *priv,
793 struct v4l2_format *f)
795 struct vivid_dev *dev = video_drvdata(file);
796 const struct v4l2_rect *compose = &dev->compose_out;
797 struct v4l2_window *win = &f->fmt.win;
798 unsigned clipcount = win->clipcount;
802 win->w.top = dev->overlay_out_top;
803 win->w.left = dev->overlay_out_left;
804 win->w.width = compose->width;
805 win->w.height = compose->height;
806 win->clipcount = dev->clipcount_out;
807 win->field = V4L2_FIELD_ANY;
808 win->chromakey = dev->chromakey_out;
809 win->global_alpha = dev->global_alpha_out;
810 if (clipcount > dev->clipcount_out)
811 clipcount = dev->clipcount_out;
812 if (dev->bitmap_out == NULL)
814 else if (win->bitmap) {
815 if (copy_to_user(win->bitmap, dev->bitmap_out,
816 ((dev->compose_out.width + 7) / 8) * dev->compose_out.height))
819 if (clipcount && win->clips) {
820 if (copy_to_user(win->clips, dev->clips_out,
821 clipcount * sizeof(dev->clips_out[0])))
827 int vidioc_try_fmt_vid_out_overlay(struct file *file, void *priv,
828 struct v4l2_format *f)
830 struct vivid_dev *dev = video_drvdata(file);
831 const struct v4l2_rect *compose = &dev->compose_out;
832 struct v4l2_window *win = &f->fmt.win;
837 win->w.left = clamp_t(int, win->w.left,
838 -dev->display_width, dev->display_width);
839 win->w.top = clamp_t(int, win->w.top,
840 -dev->display_height, dev->display_height);
841 win->w.width = compose->width;
842 win->w.height = compose->height;
844 * It makes no sense for an OSD to overlay only top or bottom fields,
845 * so always set this to ANY.
847 win->field = V4L2_FIELD_ANY;
848 if (win->clipcount && !win->clips)
850 if (win->clipcount > MAX_CLIPS)
851 win->clipcount = MAX_CLIPS;
852 if (win->clipcount) {
853 if (copy_from_user(dev->try_clips_out, win->clips,
854 win->clipcount * sizeof(dev->clips_out[0])))
856 for (i = 0; i < win->clipcount; i++) {
857 struct v4l2_rect *r = &dev->try_clips_out[i].c;
859 r->top = clamp_t(s32, r->top, 0, dev->display_height - 1);
860 r->height = clamp_t(s32, r->height, 1, dev->display_height - r->top);
861 r->left = clamp_t(u32, r->left, 0, dev->display_width - 1);
862 r->width = clamp_t(u32, r->width, 1, dev->display_width - r->left);
865 * Yeah, so sue me, it's an O(n^2) algorithm. But n is a small
866 * number and it's typically a one-time deal.
868 for (i = 0; i < win->clipcount - 1; i++) {
869 struct v4l2_rect *r1 = &dev->try_clips_out[i].c;
871 for (j = i + 1; j < win->clipcount; j++) {
872 struct v4l2_rect *r2 = &dev->try_clips_out[j].c;
874 if (rect_overlap(r1, r2))
878 if (copy_to_user(win->clips, dev->try_clips_out,
879 win->clipcount * sizeof(dev->clips_out[0])))
885 int vidioc_s_fmt_vid_out_overlay(struct file *file, void *priv,
886 struct v4l2_format *f)
888 struct vivid_dev *dev = video_drvdata(file);
889 const struct v4l2_rect *compose = &dev->compose_out;
890 struct v4l2_window *win = &f->fmt.win;
891 int ret = vidioc_try_fmt_vid_out_overlay(file, priv, f);
892 unsigned bitmap_size = ((compose->width + 7) / 8) * compose->height;
893 unsigned clips_size = win->clipcount * sizeof(dev->clips_out[0]);
894 void *new_bitmap = NULL;
900 new_bitmap = memdup_user(win->bitmap, bitmap_size);
902 if (IS_ERR(new_bitmap))
903 return PTR_ERR(new_bitmap);
906 dev->overlay_out_top = win->w.top;
907 dev->overlay_out_left = win->w.left;
908 kfree(dev->bitmap_out);
909 dev->bitmap_out = new_bitmap;
910 dev->clipcount_out = win->clipcount;
911 if (dev->clipcount_out)
912 memcpy(dev->clips_out, dev->try_clips_out, clips_size);
913 dev->chromakey_out = win->chromakey;
914 dev->global_alpha_out = win->global_alpha;
918 int vivid_vid_out_overlay(struct file *file, void *fh, unsigned i)
920 struct vivid_dev *dev = video_drvdata(file);
922 if (i && !dev->fmt_out->can_do_overlay) {
923 dprintk(dev, 1, "unsupported output format for output overlay\n");
927 dev->overlay_out_enabled = i;
931 int vivid_vid_out_g_fbuf(struct file *file, void *fh,
932 struct v4l2_framebuffer *a)
934 struct vivid_dev *dev = video_drvdata(file);
936 a->capability = V4L2_FBUF_CAP_EXTERNOVERLAY |
937 V4L2_FBUF_CAP_BITMAP_CLIPPING |
938 V4L2_FBUF_CAP_LIST_CLIPPING |
939 V4L2_FBUF_CAP_CHROMAKEY |
940 V4L2_FBUF_CAP_SRC_CHROMAKEY |
941 V4L2_FBUF_CAP_GLOBAL_ALPHA |
942 V4L2_FBUF_CAP_LOCAL_ALPHA |
943 V4L2_FBUF_CAP_LOCAL_INV_ALPHA;
944 a->flags = V4L2_FBUF_FLAG_OVERLAY | dev->fbuf_out_flags;
945 a->base = (void *)dev->video_pbase;
946 a->fmt.width = dev->display_width;
947 a->fmt.height = dev->display_height;
948 if (dev->fb_defined.green.length == 5)
949 a->fmt.pixelformat = V4L2_PIX_FMT_ARGB555;
951 a->fmt.pixelformat = V4L2_PIX_FMT_RGB565;
952 a->fmt.bytesperline = dev->display_byte_stride;
953 a->fmt.sizeimage = a->fmt.height * a->fmt.bytesperline;
954 a->fmt.field = V4L2_FIELD_NONE;
955 a->fmt.colorspace = V4L2_COLORSPACE_SRGB;
960 int vivid_vid_out_s_fbuf(struct file *file, void *fh,
961 const struct v4l2_framebuffer *a)
963 struct vivid_dev *dev = video_drvdata(file);
964 const unsigned chroma_flags = V4L2_FBUF_FLAG_CHROMAKEY |
965 V4L2_FBUF_FLAG_SRC_CHROMAKEY;
966 const unsigned alpha_flags = V4L2_FBUF_FLAG_GLOBAL_ALPHA |
967 V4L2_FBUF_FLAG_LOCAL_ALPHA |
968 V4L2_FBUF_FLAG_LOCAL_INV_ALPHA;
971 if ((a->flags & chroma_flags) == chroma_flags)
973 switch (a->flags & alpha_flags) {
975 case V4L2_FBUF_FLAG_GLOBAL_ALPHA:
976 case V4L2_FBUF_FLAG_LOCAL_ALPHA:
977 case V4L2_FBUF_FLAG_LOCAL_INV_ALPHA:
982 dev->fbuf_out_flags &= ~(chroma_flags | alpha_flags);
983 dev->fbuf_out_flags = a->flags & (chroma_flags | alpha_flags);
987 static const struct v4l2_audioout vivid_audio_outputs[] = {
992 int vidioc_enum_output(struct file *file, void *priv,
993 struct v4l2_output *out)
995 struct vivid_dev *dev = video_drvdata(file);
997 if (out->index >= dev->num_outputs)
1000 out->type = V4L2_OUTPUT_TYPE_ANALOG;
1001 switch (dev->output_type[out->index]) {
1003 snprintf(out->name, sizeof(out->name), "S-Video %u",
1004 dev->output_name_counter[out->index]);
1005 out->std = V4L2_STD_ALL;
1006 if (dev->has_audio_outputs)
1007 out->audioset = (1 << ARRAY_SIZE(vivid_audio_outputs)) - 1;
1008 out->capabilities = V4L2_OUT_CAP_STD;
1011 snprintf(out->name, sizeof(out->name), "HDMI %u",
1012 dev->output_name_counter[out->index]);
1013 out->capabilities = V4L2_OUT_CAP_DV_TIMINGS;
1019 int vidioc_g_output(struct file *file, void *priv, unsigned *o)
1021 struct vivid_dev *dev = video_drvdata(file);
1027 int vidioc_s_output(struct file *file, void *priv, unsigned o)
1029 struct vivid_dev *dev = video_drvdata(file);
1031 if (o >= dev->num_outputs)
1034 if (o == dev->output)
1037 if (vb2_is_busy(&dev->vb_vid_out_q) || vb2_is_busy(&dev->vb_vbi_out_q))
1041 dev->tv_audio_output = 0;
1042 if (dev->output_type[o] == SVID)
1043 dev->vid_out_dev.tvnorms = V4L2_STD_ALL;
1045 dev->vid_out_dev.tvnorms = 0;
1047 dev->vbi_out_dev.tvnorms = dev->vid_out_dev.tvnorms;
1048 vivid_update_format_out(dev);
1052 int vidioc_enumaudout(struct file *file, void *fh, struct v4l2_audioout *vout)
1054 if (vout->index >= ARRAY_SIZE(vivid_audio_outputs))
1056 *vout = vivid_audio_outputs[vout->index];
1060 int vidioc_g_audout(struct file *file, void *fh, struct v4l2_audioout *vout)
1062 struct vivid_dev *dev = video_drvdata(file);
1064 if (!vivid_is_svid_out(dev))
1066 *vout = vivid_audio_outputs[dev->tv_audio_output];
1070 int vidioc_s_audout(struct file *file, void *fh, const struct v4l2_audioout *vout)
1072 struct vivid_dev *dev = video_drvdata(file);
1074 if (!vivid_is_svid_out(dev))
1076 if (vout->index >= ARRAY_SIZE(vivid_audio_outputs))
1078 dev->tv_audio_output = vout->index;
1082 int vivid_vid_out_s_std(struct file *file, void *priv, v4l2_std_id id)
1084 struct vivid_dev *dev = video_drvdata(file);
1086 if (!vivid_is_svid_out(dev))
1088 if (dev->std_out == id)
1090 if (vb2_is_busy(&dev->vb_vid_out_q) || vb2_is_busy(&dev->vb_vbi_out_q))
1093 vivid_update_format_out(dev);
1097 int vivid_vid_out_s_dv_timings(struct file *file, void *_fh,
1098 struct v4l2_dv_timings *timings)
1100 struct vivid_dev *dev = video_drvdata(file);
1102 if (!vivid_is_hdmi_out(dev))
1104 if (vb2_is_busy(&dev->vb_vid_out_q))
1106 if (!v4l2_find_dv_timings_cap(timings, &vivid_dv_timings_cap,
1109 if (v4l2_match_dv_timings(timings, &dev->dv_timings_out, 0))
1111 dev->dv_timings_out = *timings;
1112 vivid_update_format_out(dev);
1116 int vivid_vid_out_g_parm(struct file *file, void *priv,
1117 struct v4l2_streamparm *parm)
1119 struct vivid_dev *dev = video_drvdata(file);
1121 if (parm->type != (dev->multiplanar ?
1122 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE :
1123 V4L2_BUF_TYPE_VIDEO_OUTPUT))
1126 parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
1127 parm->parm.output.timeperframe = dev->timeperframe_vid_out;
1128 parm->parm.output.writebuffers = 1;
1132 int vidioc_subscribe_event(struct v4l2_fh *fh,
1133 const struct v4l2_event_subscription *sub)
1135 switch (sub->type) {
1136 case V4L2_EVENT_CTRL:
1137 return v4l2_ctrl_subscribe_event(fh, sub);
1138 case V4L2_EVENT_SOURCE_CHANGE:
1139 if (fh->vdev->vfl_dir == VFL_DIR_RX)
1140 return v4l2_src_change_event_subscribe(fh, sub);