1 // SPDX-License-Identifier: GPL-2.0-only
3 * vivid-kthread-cap.h - video/vbi capture thread support functions.
5 * Copyright 2014 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
8 #include <linux/module.h>
9 #include <linux/errno.h>
10 #include <linux/kernel.h>
11 #include <linux/init.h>
12 #include <linux/sched.h>
13 #include <linux/slab.h>
14 #include <linux/font.h>
15 #include <linux/mutex.h>
16 #include <linux/videodev2.h>
17 #include <linux/kthread.h>
18 #include <linux/freezer.h>
19 #include <linux/random.h>
20 #include <linux/v4l2-dv-timings.h>
21 #include <asm/div64.h>
22 #include <media/videobuf2-vmalloc.h>
23 #include <media/v4l2-dv-timings.h>
24 #include <media/v4l2-ioctl.h>
25 #include <media/v4l2-fh.h>
26 #include <media/v4l2-event.h>
27 #include <media/v4l2-rect.h>
29 #include "vivid-core.h"
30 #include "vivid-vid-common.h"
31 #include "vivid-vid-cap.h"
32 #include "vivid-vid-out.h"
33 #include "vivid-radio-common.h"
34 #include "vivid-radio-rx.h"
35 #include "vivid-radio-tx.h"
36 #include "vivid-sdr-cap.h"
37 #include "vivid-vbi-cap.h"
38 #include "vivid-vbi-out.h"
39 #include "vivid-osd.h"
40 #include "vivid-ctrls.h"
41 #include "vivid-kthread-cap.h"
42 #include "vivid-meta-cap.h"
44 static inline v4l2_std_id vivid_get_std_cap(const struct vivid_dev *dev)
46 if (vivid_is_sdtv_cap(dev))
47 return dev->std_cap[dev->input];
51 static void copy_pix(struct vivid_dev *dev, int win_y, int win_x,
52 u16 *cap, const u16 *osd)
55 int left = dev->overlay_out_left;
56 int top = dev->overlay_out_top;
57 int fb_x = win_x + left;
58 int fb_y = win_y + top;
63 if (dev->bitmap_out) {
64 const u8 *p = dev->bitmap_out;
65 unsigned stride = (dev->compose_out.width + 7) / 8;
67 win_x -= dev->compose_out.left;
68 win_y -= dev->compose_out.top;
69 if (!(p[stride * win_y + win_x / 8] & (1 << (win_x & 7))))
73 for (i = 0; i < dev->clipcount_out; i++) {
74 struct v4l2_rect *r = &dev->clips_out[i].c;
76 if (fb_y >= r->top && fb_y < r->top + r->height &&
77 fb_x >= r->left && fb_x < r->left + r->width)
80 if ((dev->fbuf_out_flags & V4L2_FBUF_FLAG_CHROMAKEY) &&
81 *osd != dev->chromakey_out)
83 if ((dev->fbuf_out_flags & V4L2_FBUF_FLAG_SRC_CHROMAKEY) &&
84 out == dev->chromakey_out)
86 if (dev->fmt_cap->alpha_mask) {
87 if ((dev->fbuf_out_flags & V4L2_FBUF_FLAG_GLOBAL_ALPHA) &&
88 dev->global_alpha_out)
90 if ((dev->fbuf_out_flags & V4L2_FBUF_FLAG_LOCAL_ALPHA) &&
91 *cap & dev->fmt_cap->alpha_mask)
93 if ((dev->fbuf_out_flags & V4L2_FBUF_FLAG_LOCAL_INV_ALPHA) &&
94 !(*cap & dev->fmt_cap->alpha_mask))
100 static void blend_line(struct vivid_dev *dev, unsigned y_offset, unsigned x_offset,
101 u8 *vcapbuf, const u8 *vosdbuf,
102 unsigned width, unsigned pixsize)
106 for (x = 0; x < width; x++, vcapbuf += pixsize, vosdbuf += pixsize) {
107 copy_pix(dev, y_offset, x_offset + x,
108 (u16 *)vcapbuf, (const u16 *)vosdbuf);
112 static void scale_line(const u8 *src, u8 *dst, unsigned srcw, unsigned dstw, unsigned twopixsize)
114 /* Coarse scaling with Bresenham */
122 * We always combine two pixels to prevent color bleed in the packed
127 int_part = srcw / dstw;
128 fract_part = srcw % dstw;
129 for (x = 0; x < dstw; x++, dst += twopixsize) {
130 memcpy(dst, src + src_x * twopixsize, twopixsize);
141 * Precalculate the rectangles needed to perform video looping:
143 * The nominal pipeline is that the video output buffer is cropped by
144 * crop_out, scaled to compose_out, overlaid with the output overlay,
145 * cropped on the capture side by crop_cap and scaled again to the video
146 * capture buffer using compose_cap.
148 * To keep things efficient we calculate the intersection of compose_out
149 * and crop_cap (since that's the only part of the video that will
150 * actually end up in the capture buffer), determine which part of the
151 * video output buffer that is and which part of the video capture buffer
152 * so we can scale the video straight from the output buffer to the capture
153 * buffer without any intermediate steps.
155 * If we need to deal with an output overlay, then there is no choice and
156 * that intermediate step still has to be taken. For the output overlay
157 * support we calculate the intersection of the framebuffer and the overlay
158 * window (which may be partially or wholly outside of the framebuffer
159 * itself) and the intersection of that with loop_vid_copy (i.e. the part of
160 * the actual looped video that will be overlaid). The result is calculated
161 * both in framebuffer coordinates (loop_fb_copy) and compose_out coordinates
162 * (loop_vid_overlay). Finally calculate the part of the capture buffer that
163 * will receive that overlaid video.
165 static void vivid_precalc_copy_rects(struct vivid_dev *dev)
167 /* Framebuffer rectangle */
168 struct v4l2_rect r_fb = {
169 0, 0, dev->display_width, dev->display_height
171 /* Overlay window rectangle in framebuffer coordinates */
172 struct v4l2_rect r_overlay = {
173 dev->overlay_out_left, dev->overlay_out_top,
174 dev->compose_out.width, dev->compose_out.height
177 v4l2_rect_intersect(&dev->loop_vid_copy, &dev->crop_cap, &dev->compose_out);
179 dev->loop_vid_out = dev->loop_vid_copy;
180 v4l2_rect_scale(&dev->loop_vid_out, &dev->compose_out, &dev->crop_out);
181 dev->loop_vid_out.left += dev->crop_out.left;
182 dev->loop_vid_out.top += dev->crop_out.top;
184 dev->loop_vid_cap = dev->loop_vid_copy;
185 v4l2_rect_scale(&dev->loop_vid_cap, &dev->crop_cap, &dev->compose_cap);
188 "loop_vid_copy: %dx%d@%dx%d loop_vid_out: %dx%d@%dx%d loop_vid_cap: %dx%d@%dx%d\n",
189 dev->loop_vid_copy.width, dev->loop_vid_copy.height,
190 dev->loop_vid_copy.left, dev->loop_vid_copy.top,
191 dev->loop_vid_out.width, dev->loop_vid_out.height,
192 dev->loop_vid_out.left, dev->loop_vid_out.top,
193 dev->loop_vid_cap.width, dev->loop_vid_cap.height,
194 dev->loop_vid_cap.left, dev->loop_vid_cap.top);
196 v4l2_rect_intersect(&r_overlay, &r_fb, &r_overlay);
198 /* shift r_overlay to the same origin as compose_out */
199 r_overlay.left += dev->compose_out.left - dev->overlay_out_left;
200 r_overlay.top += dev->compose_out.top - dev->overlay_out_top;
202 v4l2_rect_intersect(&dev->loop_vid_overlay, &r_overlay, &dev->loop_vid_copy);
203 dev->loop_fb_copy = dev->loop_vid_overlay;
205 /* shift dev->loop_fb_copy back again to the fb origin */
206 dev->loop_fb_copy.left -= dev->compose_out.left - dev->overlay_out_left;
207 dev->loop_fb_copy.top -= dev->compose_out.top - dev->overlay_out_top;
209 dev->loop_vid_overlay_cap = dev->loop_vid_overlay;
210 v4l2_rect_scale(&dev->loop_vid_overlay_cap, &dev->crop_cap, &dev->compose_cap);
213 "loop_fb_copy: %dx%d@%dx%d loop_vid_overlay: %dx%d@%dx%d loop_vid_overlay_cap: %dx%d@%dx%d\n",
214 dev->loop_fb_copy.width, dev->loop_fb_copy.height,
215 dev->loop_fb_copy.left, dev->loop_fb_copy.top,
216 dev->loop_vid_overlay.width, dev->loop_vid_overlay.height,
217 dev->loop_vid_overlay.left, dev->loop_vid_overlay.top,
218 dev->loop_vid_overlay_cap.width, dev->loop_vid_overlay_cap.height,
219 dev->loop_vid_overlay_cap.left, dev->loop_vid_overlay_cap.top);
222 static void *plane_vaddr(struct tpg_data *tpg, struct vivid_buffer *buf,
223 unsigned p, unsigned bpl[TPG_MAX_PLANES], unsigned h)
228 if (p == 0 || tpg_g_buffers(tpg) > 1)
229 return vb2_plane_vaddr(&buf->vb.vb2_buf, p);
230 vbuf = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
231 for (i = 0; i < p; i++)
232 vbuf += bpl[i] * h / tpg->vdownsampling[i];
236 static noinline_for_stack int vivid_copy_buffer(struct vivid_dev *dev, unsigned p,
237 u8 *vcapbuf, struct vivid_buffer *vid_cap_buf)
239 bool blank = dev->must_blank[vid_cap_buf->vb.vb2_buf.index];
240 struct tpg_data *tpg = &dev->tpg;
241 struct vivid_buffer *vid_out_buf = NULL;
242 unsigned vdiv = dev->fmt_out->vdownsampling[p];
243 unsigned twopixsize = tpg_g_twopixelsize(tpg, p);
244 unsigned img_width = tpg_hdiv(tpg, p, dev->compose_cap.width);
245 unsigned img_height = dev->compose_cap.height;
246 unsigned stride_cap = tpg->bytesperline[p];
247 unsigned stride_out = dev->bytesperline_out[p];
248 unsigned stride_osd = dev->display_byte_stride;
249 unsigned hmax = (img_height * tpg->perc_fill) / 100;
253 bool blend = dev->bitmap_out || dev->clipcount_out || dev->fbuf_out_flags;
254 /* Coarse scaling with Bresenham */
255 unsigned vid_out_int_part;
256 unsigned vid_out_fract_part;
257 unsigned vid_out_y = 0;
258 unsigned vid_out_error = 0;
259 unsigned vid_overlay_int_part = 0;
260 unsigned vid_overlay_fract_part = 0;
261 unsigned vid_overlay_y = 0;
262 unsigned vid_overlay_error = 0;
263 unsigned vid_cap_left = tpg_hdiv(tpg, p, dev->loop_vid_cap.left);
264 unsigned vid_cap_right;
267 vid_out_int_part = dev->loop_vid_out.height / dev->loop_vid_cap.height;
268 vid_out_fract_part = dev->loop_vid_out.height % dev->loop_vid_cap.height;
270 if (!list_empty(&dev->vid_out_active))
271 vid_out_buf = list_entry(dev->vid_out_active.next,
272 struct vivid_buffer, list);
273 if (vid_out_buf == NULL)
276 vid_cap_buf->vb.field = vid_out_buf->vb.field;
278 voutbuf = plane_vaddr(tpg, vid_out_buf, p,
279 dev->bytesperline_out, dev->fmt_out_rect.height);
280 if (p < dev->fmt_out->buffers)
281 voutbuf += vid_out_buf->vb.vb2_buf.planes[p].data_offset;
282 voutbuf += tpg_hdiv(tpg, p, dev->loop_vid_out.left) +
283 (dev->loop_vid_out.top / vdiv) * stride_out;
284 vcapbuf += tpg_hdiv(tpg, p, dev->compose_cap.left) +
285 (dev->compose_cap.top / vdiv) * stride_cap;
287 if (dev->loop_vid_copy.width == 0 || dev->loop_vid_copy.height == 0) {
289 * If there is nothing to copy, then just fill the capture window
292 for (y = 0; y < hmax / vdiv; y++, vcapbuf += stride_cap)
293 memcpy(vcapbuf, tpg->black_line[p], img_width);
297 if (dev->overlay_out_enabled &&
298 dev->loop_vid_overlay.width && dev->loop_vid_overlay.height) {
299 vosdbuf = dev->video_vbase;
300 vosdbuf += (dev->loop_fb_copy.left * twopixsize) / 2 +
301 dev->loop_fb_copy.top * stride_osd;
302 vid_overlay_int_part = dev->loop_vid_overlay.height /
303 dev->loop_vid_overlay_cap.height;
304 vid_overlay_fract_part = dev->loop_vid_overlay.height %
305 dev->loop_vid_overlay_cap.height;
308 vid_cap_right = tpg_hdiv(tpg, p, dev->loop_vid_cap.left + dev->loop_vid_cap.width);
309 /* quick is true if no video scaling is needed */
310 quick = dev->loop_vid_out.width == dev->loop_vid_cap.width;
312 dev->cur_scaled_line = dev->loop_vid_out.height;
313 for (y = 0; y < hmax; y += vdiv, vcapbuf += stride_cap) {
314 /* osdline is true if this line requires overlay blending */
315 bool osdline = vosdbuf && y >= dev->loop_vid_overlay_cap.top &&
316 y < dev->loop_vid_overlay_cap.top + dev->loop_vid_overlay_cap.height;
319 * If this line of the capture buffer doesn't get any video, then
320 * just fill with black.
322 if (y < dev->loop_vid_cap.top ||
323 y >= dev->loop_vid_cap.top + dev->loop_vid_cap.height) {
324 memcpy(vcapbuf, tpg->black_line[p], img_width);
328 /* fill the left border with black */
329 if (dev->loop_vid_cap.left)
330 memcpy(vcapbuf, tpg->black_line[p], vid_cap_left);
332 /* fill the right border with black */
333 if (vid_cap_right < img_width)
334 memcpy(vcapbuf + vid_cap_right, tpg->black_line[p],
335 img_width - vid_cap_right);
337 if (quick && !osdline) {
338 memcpy(vcapbuf + vid_cap_left,
339 voutbuf + vid_out_y * stride_out,
340 tpg_hdiv(tpg, p, dev->loop_vid_cap.width));
341 goto update_vid_out_y;
343 if (dev->cur_scaled_line == vid_out_y) {
344 memcpy(vcapbuf + vid_cap_left, dev->scaled_line,
345 tpg_hdiv(tpg, p, dev->loop_vid_cap.width));
346 goto update_vid_out_y;
349 scale_line(voutbuf + vid_out_y * stride_out, dev->scaled_line,
350 tpg_hdiv(tpg, p, dev->loop_vid_out.width),
351 tpg_hdiv(tpg, p, dev->loop_vid_cap.width),
352 tpg_g_twopixelsize(tpg, p));
355 * Offset in bytes within loop_vid_copy to the start of the
356 * loop_vid_overlay rectangle.
359 ((dev->loop_vid_overlay.left - dev->loop_vid_copy.left) *
361 u8 *osd = vosdbuf + vid_overlay_y * stride_osd;
363 scale_line(voutbuf + vid_out_y * stride_out, dev->blended_line,
364 dev->loop_vid_out.width, dev->loop_vid_copy.width,
365 tpg_g_twopixelsize(tpg, p));
367 blend_line(dev, vid_overlay_y + dev->loop_vid_overlay.top,
368 dev->loop_vid_overlay.left,
369 dev->blended_line + offset, osd,
370 dev->loop_vid_overlay.width, twopixsize / 2);
372 memcpy(dev->blended_line + offset,
373 osd, (dev->loop_vid_overlay.width * twopixsize) / 2);
374 scale_line(dev->blended_line, dev->scaled_line,
375 dev->loop_vid_copy.width, dev->loop_vid_cap.width,
376 tpg_g_twopixelsize(tpg, p));
378 dev->cur_scaled_line = vid_out_y;
379 memcpy(vcapbuf + vid_cap_left, dev->scaled_line,
380 tpg_hdiv(tpg, p, dev->loop_vid_cap.width));
384 vid_overlay_y += vid_overlay_int_part;
385 vid_overlay_error += vid_overlay_fract_part;
386 if (vid_overlay_error >= dev->loop_vid_overlay_cap.height) {
387 vid_overlay_error -= dev->loop_vid_overlay_cap.height;
391 vid_out_y += vid_out_int_part;
392 vid_out_error += vid_out_fract_part;
393 if (vid_out_error >= dev->loop_vid_cap.height / vdiv) {
394 vid_out_error -= dev->loop_vid_cap.height / vdiv;
401 for (; y < img_height; y += vdiv, vcapbuf += stride_cap)
402 memcpy(vcapbuf, tpg->contrast_line[p], img_width);
406 static void vivid_fillbuff(struct vivid_dev *dev, struct vivid_buffer *buf)
408 struct tpg_data *tpg = &dev->tpg;
409 unsigned factor = V4L2_FIELD_HAS_T_OR_B(dev->field_cap) ? 2 : 1;
410 unsigned line_height = 16 / factor;
411 bool is_tv = vivid_is_sdtv_cap(dev);
412 bool is_60hz = is_tv && (dev->std_cap[dev->input] & V4L2_STD_525_60);
415 u8 *basep[TPG_MAX_PLANES][2];
419 bool is_loop = false;
421 if (dev->loop_video && dev->can_loop_video &&
422 ((vivid_is_svid_cap(dev) &&
423 !VIVID_INVALID_SIGNAL(dev->std_signal_mode[dev->input])) ||
424 (vivid_is_hdmi_cap(dev) &&
425 !VIVID_INVALID_SIGNAL(dev->dv_timings_signal_mode[dev->input]))))
428 buf->vb.sequence = dev->vid_cap_seq_count;
429 if (dev->field_cap == V4L2_FIELD_ALTERNATE) {
431 * 60 Hz standards start with the bottom field, 50 Hz standards
432 * with the top field. So if the 0-based seq_count is even,
433 * then the field is TOP for 50 Hz and BOTTOM for 60 Hz
436 buf->vb.field = ((dev->vid_cap_seq_count & 1) ^ is_60hz) ?
437 V4L2_FIELD_BOTTOM : V4L2_FIELD_TOP;
439 * The sequence counter counts frames, not fields. So divide
442 buf->vb.sequence /= 2;
444 buf->vb.field = dev->field_cap;
446 tpg_s_field(tpg, buf->vb.field,
447 dev->field_cap == V4L2_FIELD_ALTERNATE);
448 tpg_s_perc_fill_blank(tpg, dev->must_blank[buf->vb.vb2_buf.index]);
450 vivid_precalc_copy_rects(dev);
452 for (p = 0; p < tpg_g_planes(tpg); p++) {
453 void *vbuf = plane_vaddr(tpg, buf, p,
454 tpg->bytesperline, tpg->buf_height);
457 * The first plane of a multiplanar format has a non-zero
458 * data_offset. This helps testing whether the application
459 * correctly supports non-zero data offsets.
461 if (p < tpg_g_buffers(tpg) && dev->fmt_cap->data_offset[p]) {
462 memset(vbuf, dev->fmt_cap->data_offset[p] & 0xff,
463 dev->fmt_cap->data_offset[p]);
464 vbuf += dev->fmt_cap->data_offset[p];
466 tpg_calc_text_basep(tpg, basep, p, vbuf);
467 if (!is_loop || vivid_copy_buffer(dev, p, vbuf, buf))
468 tpg_fill_plane_buffer(tpg, vivid_get_std_cap(dev),
471 dev->must_blank[buf->vb.vb2_buf.index] = false;
473 /* Updates stream time, only update at the start of a new frame. */
474 if (dev->field_cap != V4L2_FIELD_ALTERNATE ||
475 (dev->vid_cap_seq_count & 1) == 0)
477 jiffies_to_msecs(jiffies - dev->jiffies_vid_cap);
479 ms = dev->ms_vid_cap;
480 if (dev->osd_mode <= 1) {
481 snprintf(str, sizeof(str), " %02d:%02d:%02d:%03d %u%s",
482 (ms / (60 * 60 * 1000)) % 24,
483 (ms / (60 * 1000)) % 60,
487 (dev->field_cap == V4L2_FIELD_ALTERNATE) ?
488 (buf->vb.field == V4L2_FIELD_TOP ?
489 " top" : " bottom") : "");
490 tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
492 if (dev->osd_mode == 0) {
493 snprintf(str, sizeof(str), " %dx%d, input %d ",
494 dev->src_rect.width, dev->src_rect.height, dev->input);
495 tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
497 gain = v4l2_ctrl_g_ctrl(dev->gain);
498 mutex_lock(dev->ctrl_hdl_user_vid.lock);
499 snprintf(str, sizeof(str),
500 " brightness %3d, contrast %3d, saturation %3d, hue %d ",
501 dev->brightness->cur.val,
502 dev->contrast->cur.val,
503 dev->saturation->cur.val,
505 tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
506 snprintf(str, sizeof(str),
507 " autogain %d, gain %3d, alpha 0x%02x ",
508 dev->autogain->cur.val, gain, dev->alpha->cur.val);
509 mutex_unlock(dev->ctrl_hdl_user_vid.lock);
510 tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
511 mutex_lock(dev->ctrl_hdl_user_aud.lock);
512 snprintf(str, sizeof(str),
513 " volume %3d, mute %d ",
514 dev->volume->cur.val, dev->mute->cur.val);
515 mutex_unlock(dev->ctrl_hdl_user_aud.lock);
516 tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
517 mutex_lock(dev->ctrl_hdl_user_gen.lock);
518 snprintf(str, sizeof(str), " int32 %d, int64 %lld, bitmask %08x ",
520 *dev->int64->p_cur.p_s64,
521 dev->bitmask->cur.val);
522 tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
523 snprintf(str, sizeof(str), " boolean %d, menu %s, string \"%s\" ",
524 dev->boolean->cur.val,
525 dev->menu->qmenu[dev->menu->cur.val],
526 dev->string->p_cur.p_char);
527 tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
528 snprintf(str, sizeof(str), " integer_menu %lld, value %d ",
529 dev->int_menu->qmenu_int[dev->int_menu->cur.val],
530 dev->int_menu->cur.val);
531 mutex_unlock(dev->ctrl_hdl_user_gen.lock);
532 tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
533 if (dev->button_pressed) {
534 dev->button_pressed--;
535 snprintf(str, sizeof(str), " button pressed!");
536 tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
539 if (vivid_is_hdmi_cap(dev)) {
540 snprintf(str, sizeof(str),
541 " OSD \"%s\"", dev->osd);
542 tpg_gen_text(tpg, basep, line++ * line_height,
545 if (dev->osd_jiffies &&
546 time_is_before_jiffies(dev->osd_jiffies + 5 * HZ)) {
548 dev->osd_jiffies = 0;
555 * Return true if this pixel coordinate is a valid video pixel.
557 static bool valid_pix(struct vivid_dev *dev, int win_y, int win_x, int fb_y, int fb_x)
561 if (dev->bitmap_cap) {
563 * Only if the corresponding bit in the bitmap is set can
564 * the video pixel be shown. Coordinates are relative to
565 * the overlay window set by VIDIOC_S_FMT.
567 const u8 *p = dev->bitmap_cap;
568 unsigned stride = (dev->compose_cap.width + 7) / 8;
570 if (!(p[stride * win_y + win_x / 8] & (1 << (win_x & 7))))
574 for (i = 0; i < dev->clipcount_cap; i++) {
576 * Only if the framebuffer coordinate is not in any of the
577 * clip rectangles will be video pixel be shown.
579 struct v4l2_rect *r = &dev->clips_cap[i].c;
581 if (fb_y >= r->top && fb_y < r->top + r->height &&
582 fb_x >= r->left && fb_x < r->left + r->width)
589 * Draw the image into the overlay buffer.
590 * Note that the combination of overlay and multiplanar is not supported.
592 static void vivid_overlay(struct vivid_dev *dev, struct vivid_buffer *buf)
594 struct tpg_data *tpg = &dev->tpg;
595 unsigned pixsize = tpg_g_twopixelsize(tpg, 0) / 2;
596 void *vbase = dev->fb_vbase_cap;
597 void *vbuf = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
598 unsigned img_width = dev->compose_cap.width;
599 unsigned img_height = dev->compose_cap.height;
600 unsigned stride = tpg->bytesperline[0];
601 /* if quick is true, then valid_pix() doesn't have to be called */
602 bool quick = dev->bitmap_cap == NULL && dev->clipcount_cap == 0;
603 int x, y, w, out_x = 0;
606 * Overlay support is only supported for formats that have a twopixelsize
607 * that's >= 2. Warn and bail out if that's not the case.
609 if (WARN_ON(pixsize == 0))
611 if ((dev->overlay_cap_field == V4L2_FIELD_TOP ||
612 dev->overlay_cap_field == V4L2_FIELD_BOTTOM) &&
613 dev->overlay_cap_field != buf->vb.field)
616 vbuf += dev->compose_cap.left * pixsize + dev->compose_cap.top * stride;
617 x = dev->overlay_cap_left;
624 w = dev->fb_cap.fmt.width - x;
630 if (dev->overlay_cap_top >= 0)
631 vbase += dev->overlay_cap_top * dev->fb_cap.fmt.bytesperline;
632 for (y = dev->overlay_cap_top;
633 y < dev->overlay_cap_top + (int)img_height;
634 y++, vbuf += stride) {
637 if (y < 0 || y > dev->fb_cap.fmt.height)
640 memcpy(vbase + x * pixsize,
641 vbuf + out_x * pixsize, w * pixsize);
642 vbase += dev->fb_cap.fmt.bytesperline;
645 for (px = 0; px < w; px++) {
646 if (!valid_pix(dev, y - dev->overlay_cap_top,
647 px + out_x, y, px + x))
649 memcpy(vbase + (px + x) * pixsize,
650 vbuf + (px + out_x) * pixsize,
653 vbase += dev->fb_cap.fmt.bytesperline;
657 static void vivid_cap_update_frame_period(struct vivid_dev *dev)
661 f_period = (u64)dev->timeperframe_vid_cap.numerator * 1000000000;
662 if (WARN_ON(dev->timeperframe_vid_cap.denominator == 0))
663 dev->timeperframe_vid_cap.denominator = 1;
664 do_div(f_period, dev->timeperframe_vid_cap.denominator);
665 if (dev->field_cap == V4L2_FIELD_ALTERNATE)
668 * If "End of Frame", then offset the exposure time by 0.9
669 * of the frame period.
671 dev->cap_frame_eof_offset = f_period * 9;
672 do_div(dev->cap_frame_eof_offset, 10);
673 dev->cap_frame_period = f_period;
676 static noinline_for_stack void vivid_thread_vid_cap_tick(struct vivid_dev *dev,
679 struct vivid_buffer *vid_cap_buf = NULL;
680 struct vivid_buffer *vbi_cap_buf = NULL;
681 struct vivid_buffer *meta_cap_buf = NULL;
684 dprintk(dev, 1, "Video Capture Thread Tick\n");
686 while (dropped_bufs-- > 1)
687 tpg_update_mv_count(&dev->tpg,
688 dev->field_cap == V4L2_FIELD_NONE ||
689 dev->field_cap == V4L2_FIELD_ALTERNATE);
691 /* Drop a certain percentage of buffers. */
692 if (dev->perc_dropped_buffers &&
693 prandom_u32_max(100) < dev->perc_dropped_buffers)
696 spin_lock(&dev->slock);
697 if (!list_empty(&dev->vid_cap_active)) {
698 vid_cap_buf = list_entry(dev->vid_cap_active.next, struct vivid_buffer, list);
699 list_del(&vid_cap_buf->list);
701 if (!list_empty(&dev->vbi_cap_active)) {
702 if (dev->field_cap != V4L2_FIELD_ALTERNATE ||
703 (dev->vbi_cap_seq_count & 1)) {
704 vbi_cap_buf = list_entry(dev->vbi_cap_active.next,
705 struct vivid_buffer, list);
706 list_del(&vbi_cap_buf->list);
709 if (!list_empty(&dev->meta_cap_active)) {
710 meta_cap_buf = list_entry(dev->meta_cap_active.next,
711 struct vivid_buffer, list);
712 list_del(&meta_cap_buf->list);
715 spin_unlock(&dev->slock);
717 if (!vid_cap_buf && !vbi_cap_buf && !meta_cap_buf)
720 f_time = dev->cap_frame_period * dev->vid_cap_seq_count +
721 dev->cap_stream_start + dev->time_wrap_offset;
724 v4l2_ctrl_request_setup(vid_cap_buf->vb.vb2_buf.req_obj.req,
725 &dev->ctrl_hdl_vid_cap);
727 vivid_fillbuff(dev, vid_cap_buf);
728 dprintk(dev, 1, "filled buffer %d\n",
729 vid_cap_buf->vb.vb2_buf.index);
732 if (dev->overlay_cap_owner && dev->fb_cap.base &&
733 dev->fb_cap.fmt.pixelformat == dev->fmt_cap->fourcc)
734 vivid_overlay(dev, vid_cap_buf);
736 v4l2_ctrl_request_complete(vid_cap_buf->vb.vb2_buf.req_obj.req,
737 &dev->ctrl_hdl_vid_cap);
738 vb2_buffer_done(&vid_cap_buf->vb.vb2_buf, dev->dqbuf_error ?
739 VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
740 dprintk(dev, 2, "vid_cap buffer %d done\n",
741 vid_cap_buf->vb.vb2_buf.index);
743 vid_cap_buf->vb.vb2_buf.timestamp = f_time;
744 if (!dev->tstamp_src_is_soe)
745 vid_cap_buf->vb.vb2_buf.timestamp += dev->cap_frame_eof_offset;
751 v4l2_ctrl_request_setup(vbi_cap_buf->vb.vb2_buf.req_obj.req,
752 &dev->ctrl_hdl_vbi_cap);
753 if (dev->stream_sliced_vbi_cap)
754 vivid_sliced_vbi_cap_process(dev, vbi_cap_buf);
756 vivid_raw_vbi_cap_process(dev, vbi_cap_buf);
757 v4l2_ctrl_request_complete(vbi_cap_buf->vb.vb2_buf.req_obj.req,
758 &dev->ctrl_hdl_vbi_cap);
759 vb2_buffer_done(&vbi_cap_buf->vb.vb2_buf, dev->dqbuf_error ?
760 VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
761 dprintk(dev, 2, "vbi_cap %d done\n",
762 vbi_cap_buf->vb.vb2_buf.index);
764 /* If capturing a VBI, offset by 0.05 */
765 vbi_period = dev->cap_frame_period * 5;
766 do_div(vbi_period, 100);
767 vbi_cap_buf->vb.vb2_buf.timestamp = f_time + dev->cap_frame_eof_offset + vbi_period;
771 v4l2_ctrl_request_setup(meta_cap_buf->vb.vb2_buf.req_obj.req,
772 &dev->ctrl_hdl_meta_cap);
773 vivid_meta_cap_fillbuff(dev, meta_cap_buf, f_time);
774 v4l2_ctrl_request_complete(meta_cap_buf->vb.vb2_buf.req_obj.req,
775 &dev->ctrl_hdl_meta_cap);
776 vb2_buffer_done(&meta_cap_buf->vb.vb2_buf, dev->dqbuf_error ?
777 VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
778 dprintk(dev, 2, "meta_cap %d done\n",
779 meta_cap_buf->vb.vb2_buf.index);
780 meta_cap_buf->vb.vb2_buf.timestamp = f_time + dev->cap_frame_eof_offset;
783 dev->dqbuf_error = false;
786 /* Update the test pattern movement counters */
787 tpg_update_mv_count(&dev->tpg, dev->field_cap == V4L2_FIELD_NONE ||
788 dev->field_cap == V4L2_FIELD_ALTERNATE);
791 static int vivid_thread_vid_cap(void *data)
793 struct vivid_dev *dev = data;
794 u64 numerators_since_start;
795 u64 buffers_since_start;
796 u64 next_jiffies_since_start;
797 unsigned long jiffies_since_start;
798 unsigned long cur_jiffies;
799 unsigned wait_jiffies;
801 unsigned denominator;
804 dprintk(dev, 1, "Video Capture Thread Start\n");
808 /* Resets frame counters */
809 dev->cap_seq_offset = 0;
810 dev->cap_seq_count = 0;
811 dev->cap_seq_resync = false;
812 dev->jiffies_vid_cap = jiffies;
813 dev->cap_stream_start = ktime_get_ns();
814 vivid_cap_update_frame_period(dev);
818 if (kthread_should_stop())
821 if (!mutex_trylock(&dev->mutex)) {
822 schedule_timeout_uninterruptible(1);
826 cur_jiffies = jiffies;
827 if (dev->cap_seq_resync) {
828 dev->jiffies_vid_cap = cur_jiffies;
829 dev->cap_seq_offset = dev->cap_seq_count + 1;
830 dev->cap_seq_count = 0;
831 dev->cap_stream_start += dev->cap_frame_period *
833 vivid_cap_update_frame_period(dev);
834 dev->cap_seq_resync = false;
836 numerator = dev->timeperframe_vid_cap.numerator;
837 denominator = dev->timeperframe_vid_cap.denominator;
839 if (dev->field_cap == V4L2_FIELD_ALTERNATE)
842 /* Calculate the number of jiffies since we started streaming */
843 jiffies_since_start = cur_jiffies - dev->jiffies_vid_cap;
844 /* Get the number of buffers streamed since the start */
845 buffers_since_start = (u64)jiffies_since_start * denominator +
846 (HZ * numerator) / 2;
847 do_div(buffers_since_start, HZ * numerator);
850 * After more than 0xf0000000 (rounded down to a multiple of
851 * 'jiffies-per-day' to ease jiffies_to_msecs calculation)
852 * jiffies have passed since we started streaming reset the
853 * counters and keep track of the sequence offset.
855 if (jiffies_since_start > JIFFIES_RESYNC) {
856 dev->jiffies_vid_cap = cur_jiffies;
857 dev->cap_seq_offset = buffers_since_start;
858 buffers_since_start = 0;
860 dropped_bufs = buffers_since_start + dev->cap_seq_offset - dev->cap_seq_count;
861 dev->cap_seq_count = buffers_since_start + dev->cap_seq_offset;
862 dev->vid_cap_seq_count = dev->cap_seq_count - dev->vid_cap_seq_start;
863 dev->vbi_cap_seq_count = dev->cap_seq_count - dev->vbi_cap_seq_start;
864 dev->meta_cap_seq_count = dev->cap_seq_count - dev->meta_cap_seq_start;
866 vivid_thread_vid_cap_tick(dev, dropped_bufs);
869 * Calculate the number of 'numerators' streamed since we started,
870 * including the current buffer.
872 numerators_since_start = ++buffers_since_start * numerator;
874 /* And the number of jiffies since we started */
875 jiffies_since_start = jiffies - dev->jiffies_vid_cap;
877 mutex_unlock(&dev->mutex);
880 * Calculate when that next buffer is supposed to start
881 * in jiffies since we started streaming.
883 next_jiffies_since_start = numerators_since_start * HZ +
885 do_div(next_jiffies_since_start, denominator);
886 /* If it is in the past, then just schedule asap */
887 if (next_jiffies_since_start < jiffies_since_start)
888 next_jiffies_since_start = jiffies_since_start;
890 wait_jiffies = next_jiffies_since_start - jiffies_since_start;
891 schedule_timeout_interruptible(wait_jiffies ? wait_jiffies : 1);
893 dprintk(dev, 1, "Video Capture Thread End\n");
897 static void vivid_grab_controls(struct vivid_dev *dev, bool grab)
899 v4l2_ctrl_grab(dev->ctrl_has_crop_cap, grab);
900 v4l2_ctrl_grab(dev->ctrl_has_compose_cap, grab);
901 v4l2_ctrl_grab(dev->ctrl_has_scaler_cap, grab);
904 int vivid_start_generating_vid_cap(struct vivid_dev *dev, bool *pstreaming)
906 dprintk(dev, 1, "%s\n", __func__);
908 if (dev->kthread_vid_cap) {
909 u32 seq_count = dev->cap_seq_count + dev->seq_wrap * 128;
911 if (pstreaming == &dev->vid_cap_streaming)
912 dev->vid_cap_seq_start = seq_count;
913 else if (pstreaming == &dev->vbi_cap_streaming)
914 dev->vbi_cap_seq_start = seq_count;
916 dev->meta_cap_seq_start = seq_count;
921 /* Resets frame counters */
922 tpg_init_mv_count(&dev->tpg);
924 dev->vid_cap_seq_start = dev->seq_wrap * 128;
925 dev->vbi_cap_seq_start = dev->seq_wrap * 128;
926 dev->meta_cap_seq_start = dev->seq_wrap * 128;
928 dev->kthread_vid_cap = kthread_run(vivid_thread_vid_cap, dev,
929 "%s-vid-cap", dev->v4l2_dev.name);
931 if (IS_ERR(dev->kthread_vid_cap)) {
932 int err = PTR_ERR(dev->kthread_vid_cap);
934 dev->kthread_vid_cap = NULL;
935 v4l2_err(&dev->v4l2_dev, "kernel_thread() failed\n");
939 vivid_grab_controls(dev, true);
941 dprintk(dev, 1, "returning from %s\n", __func__);
945 void vivid_stop_generating_vid_cap(struct vivid_dev *dev, bool *pstreaming)
947 dprintk(dev, 1, "%s\n", __func__);
949 if (dev->kthread_vid_cap == NULL)
953 if (pstreaming == &dev->vid_cap_streaming) {
954 /* Release all active buffers */
955 while (!list_empty(&dev->vid_cap_active)) {
956 struct vivid_buffer *buf;
958 buf = list_entry(dev->vid_cap_active.next,
959 struct vivid_buffer, list);
960 list_del(&buf->list);
961 v4l2_ctrl_request_complete(buf->vb.vb2_buf.req_obj.req,
962 &dev->ctrl_hdl_vid_cap);
963 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
964 dprintk(dev, 2, "vid_cap buffer %d done\n",
965 buf->vb.vb2_buf.index);
969 if (pstreaming == &dev->vbi_cap_streaming) {
970 while (!list_empty(&dev->vbi_cap_active)) {
971 struct vivid_buffer *buf;
973 buf = list_entry(dev->vbi_cap_active.next,
974 struct vivid_buffer, list);
975 list_del(&buf->list);
976 v4l2_ctrl_request_complete(buf->vb.vb2_buf.req_obj.req,
977 &dev->ctrl_hdl_vbi_cap);
978 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
979 dprintk(dev, 2, "vbi_cap buffer %d done\n",
980 buf->vb.vb2_buf.index);
984 if (pstreaming == &dev->meta_cap_streaming) {
985 while (!list_empty(&dev->meta_cap_active)) {
986 struct vivid_buffer *buf;
988 buf = list_entry(dev->meta_cap_active.next,
989 struct vivid_buffer, list);
990 list_del(&buf->list);
991 v4l2_ctrl_request_complete(buf->vb.vb2_buf.req_obj.req,
992 &dev->ctrl_hdl_meta_cap);
993 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
994 dprintk(dev, 2, "meta_cap buffer %d done\n",
995 buf->vb.vb2_buf.index);
999 if (dev->vid_cap_streaming || dev->vbi_cap_streaming ||
1000 dev->meta_cap_streaming)
1003 /* shutdown control thread */
1004 vivid_grab_controls(dev, false);
1005 kthread_stop(dev->kthread_vid_cap);
1006 dev->kthread_vid_cap = NULL;