2 * vivid-kthread-cap.h - video/vbi capture thread 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/module.h>
21 #include <linux/errno.h>
22 #include <linux/kernel.h>
23 #include <linux/init.h>
24 #include <linux/sched.h>
25 #include <linux/slab.h>
26 #include <linux/font.h>
27 #include <linux/mutex.h>
28 #include <linux/videodev2.h>
29 #include <linux/kthread.h>
30 #include <linux/freezer.h>
31 #include <linux/random.h>
32 #include <linux/v4l2-dv-timings.h>
33 #include <asm/div64.h>
34 #include <media/videobuf2-vmalloc.h>
35 #include <media/v4l2-dv-timings.h>
36 #include <media/v4l2-ioctl.h>
37 #include <media/v4l2-fh.h>
38 #include <media/v4l2-event.h>
40 #include "vivid-core.h"
41 #include "vivid-vid-common.h"
42 #include "vivid-vid-cap.h"
43 #include "vivid-vid-out.h"
44 #include "vivid-radio-common.h"
45 #include "vivid-radio-rx.h"
46 #include "vivid-radio-tx.h"
47 #include "vivid-sdr-cap.h"
48 #include "vivid-vbi-cap.h"
49 #include "vivid-vbi-out.h"
50 #include "vivid-osd.h"
51 #include "vivid-ctrls.h"
52 #include "vivid-kthread-cap.h"
54 static inline v4l2_std_id vivid_get_std_cap(const struct vivid_dev *dev)
56 if (vivid_is_sdtv_cap(dev))
61 static void copy_pix(struct vivid_dev *dev, int win_y, int win_x,
62 u16 *cap, const u16 *osd)
65 int left = dev->overlay_out_left;
66 int top = dev->overlay_out_top;
67 int fb_x = win_x + left;
68 int fb_y = win_y + top;
73 if (dev->bitmap_out) {
74 const u8 *p = dev->bitmap_out;
75 unsigned stride = (dev->compose_out.width + 7) / 8;
77 win_x -= dev->compose_out.left;
78 win_y -= dev->compose_out.top;
79 if (!(p[stride * win_y + win_x / 8] & (1 << (win_x & 7))))
83 for (i = 0; i < dev->clipcount_out; i++) {
84 struct v4l2_rect *r = &dev->clips_out[i].c;
86 if (fb_y >= r->top && fb_y < r->top + r->height &&
87 fb_x >= r->left && fb_x < r->left + r->width)
90 if ((dev->fbuf_out_flags & V4L2_FBUF_FLAG_CHROMAKEY) &&
91 *osd != dev->chromakey_out)
93 if ((dev->fbuf_out_flags & V4L2_FBUF_FLAG_SRC_CHROMAKEY) &&
94 out == dev->chromakey_out)
96 if (dev->fmt_cap->alpha_mask) {
97 if ((dev->fbuf_out_flags & V4L2_FBUF_FLAG_GLOBAL_ALPHA) &&
98 dev->global_alpha_out)
100 if ((dev->fbuf_out_flags & V4L2_FBUF_FLAG_LOCAL_ALPHA) &&
101 *cap & dev->fmt_cap->alpha_mask)
103 if ((dev->fbuf_out_flags & V4L2_FBUF_FLAG_LOCAL_INV_ALPHA) &&
104 !(*cap & dev->fmt_cap->alpha_mask))
110 static void blend_line(struct vivid_dev *dev, unsigned y_offset, unsigned x_offset,
111 u8 *vcapbuf, const u8 *vosdbuf,
112 unsigned width, unsigned pixsize)
116 for (x = 0; x < width; x++, vcapbuf += pixsize, vosdbuf += pixsize) {
117 copy_pix(dev, y_offset, x_offset + x,
118 (u16 *)vcapbuf, (const u16 *)vosdbuf);
122 static void scale_line(const u8 *src, u8 *dst, unsigned srcw, unsigned dstw, unsigned twopixsize)
124 /* Coarse scaling with Bresenham */
132 * We always combine two pixels to prevent color bleed in the packed
137 int_part = srcw / dstw;
138 fract_part = srcw % dstw;
139 for (x = 0; x < dstw; x++, dst += twopixsize) {
140 memcpy(dst, src + src_x * twopixsize, twopixsize);
151 * Precalculate the rectangles needed to perform video looping:
153 * The nominal pipeline is that the video output buffer is cropped by
154 * crop_out, scaled to compose_out, overlaid with the output overlay,
155 * cropped on the capture side by crop_cap and scaled again to the video
156 * capture buffer using compose_cap.
158 * To keep things efficient we calculate the intersection of compose_out
159 * and crop_cap (since that's the only part of the video that will
160 * actually end up in the capture buffer), determine which part of the
161 * video output buffer that is and which part of the video capture buffer
162 * so we can scale the video straight from the output buffer to the capture
163 * buffer without any intermediate steps.
165 * If we need to deal with an output overlay, then there is no choice and
166 * that intermediate step still has to be taken. For the output overlay
167 * support we calculate the intersection of the framebuffer and the overlay
168 * window (which may be partially or wholly outside of the framebuffer
169 * itself) and the intersection of that with loop_vid_copy (i.e. the part of
170 * the actual looped video that will be overlaid). The result is calculated
171 * both in framebuffer coordinates (loop_fb_copy) and compose_out coordinates
172 * (loop_vid_overlay). Finally calculate the part of the capture buffer that
173 * will receive that overlaid video.
175 static void vivid_precalc_copy_rects(struct vivid_dev *dev)
177 /* Framebuffer rectangle */
178 struct v4l2_rect r_fb = {
179 0, 0, dev->display_width, dev->display_height
181 /* Overlay window rectangle in framebuffer coordinates */
182 struct v4l2_rect r_overlay = {
183 dev->overlay_out_left, dev->overlay_out_top,
184 dev->compose_out.width, dev->compose_out.height
187 dev->loop_vid_copy = rect_intersect(&dev->crop_cap, &dev->compose_out);
189 dev->loop_vid_out = dev->loop_vid_copy;
190 rect_scale(&dev->loop_vid_out, &dev->compose_out, &dev->crop_out);
191 dev->loop_vid_out.left += dev->crop_out.left;
192 dev->loop_vid_out.top += dev->crop_out.top;
194 dev->loop_vid_cap = dev->loop_vid_copy;
195 rect_scale(&dev->loop_vid_cap, &dev->crop_cap, &dev->compose_cap);
198 "loop_vid_copy: %dx%d@%dx%d loop_vid_out: %dx%d@%dx%d loop_vid_cap: %dx%d@%dx%d\n",
199 dev->loop_vid_copy.width, dev->loop_vid_copy.height,
200 dev->loop_vid_copy.left, dev->loop_vid_copy.top,
201 dev->loop_vid_out.width, dev->loop_vid_out.height,
202 dev->loop_vid_out.left, dev->loop_vid_out.top,
203 dev->loop_vid_cap.width, dev->loop_vid_cap.height,
204 dev->loop_vid_cap.left, dev->loop_vid_cap.top);
206 r_overlay = rect_intersect(&r_fb, &r_overlay);
208 /* shift r_overlay to the same origin as compose_out */
209 r_overlay.left += dev->compose_out.left - dev->overlay_out_left;
210 r_overlay.top += dev->compose_out.top - dev->overlay_out_top;
212 dev->loop_vid_overlay = rect_intersect(&r_overlay, &dev->loop_vid_copy);
213 dev->loop_fb_copy = dev->loop_vid_overlay;
215 /* shift dev->loop_fb_copy back again to the fb origin */
216 dev->loop_fb_copy.left -= dev->compose_out.left - dev->overlay_out_left;
217 dev->loop_fb_copy.top -= dev->compose_out.top - dev->overlay_out_top;
219 dev->loop_vid_overlay_cap = dev->loop_vid_overlay;
220 rect_scale(&dev->loop_vid_overlay_cap, &dev->crop_cap, &dev->compose_cap);
223 "loop_fb_copy: %dx%d@%dx%d loop_vid_overlay: %dx%d@%dx%d loop_vid_overlay_cap: %dx%d@%dx%d\n",
224 dev->loop_fb_copy.width, dev->loop_fb_copy.height,
225 dev->loop_fb_copy.left, dev->loop_fb_copy.top,
226 dev->loop_vid_overlay.width, dev->loop_vid_overlay.height,
227 dev->loop_vid_overlay.left, dev->loop_vid_overlay.top,
228 dev->loop_vid_overlay_cap.width, dev->loop_vid_overlay_cap.height,
229 dev->loop_vid_overlay_cap.left, dev->loop_vid_overlay_cap.top);
232 static int vivid_copy_buffer(struct vivid_dev *dev, unsigned p, u8 *vcapbuf,
233 struct vivid_buffer *vid_cap_buf)
235 bool blank = dev->must_blank[vid_cap_buf->vb.v4l2_buf.index];
236 struct tpg_data *tpg = &dev->tpg;
237 struct vivid_buffer *vid_out_buf = NULL;
238 unsigned pixsize = tpg_g_twopixelsize(tpg, p) / 2;
239 unsigned img_width = dev->compose_cap.width;
240 unsigned img_height = dev->compose_cap.height;
241 unsigned stride_cap = tpg->bytesperline[p];
242 unsigned stride_out = dev->bytesperline_out[p];
243 unsigned stride_osd = dev->display_byte_stride;
244 unsigned hmax = (img_height * tpg->perc_fill) / 100;
248 bool blend = dev->bitmap_out || dev->clipcount_out || dev->fbuf_out_flags;
249 /* Coarse scaling with Bresenham */
250 unsigned vid_out_int_part;
251 unsigned vid_out_fract_part;
252 unsigned vid_out_y = 0;
253 unsigned vid_out_error = 0;
254 unsigned vid_overlay_int_part = 0;
255 unsigned vid_overlay_fract_part = 0;
256 unsigned vid_overlay_y = 0;
257 unsigned vid_overlay_error = 0;
258 unsigned vid_cap_right;
261 vid_out_int_part = dev->loop_vid_out.height / dev->loop_vid_cap.height;
262 vid_out_fract_part = dev->loop_vid_out.height % dev->loop_vid_cap.height;
264 if (!list_empty(&dev->vid_out_active))
265 vid_out_buf = list_entry(dev->vid_out_active.next,
266 struct vivid_buffer, list);
267 if (vid_out_buf == NULL)
270 vid_cap_buf->vb.v4l2_buf.field = vid_out_buf->vb.v4l2_buf.field;
272 voutbuf = vb2_plane_vaddr(&vid_out_buf->vb, p) +
273 vid_out_buf->vb.v4l2_planes[p].data_offset;
274 voutbuf += dev->loop_vid_out.left * pixsize + dev->loop_vid_out.top * stride_out;
275 vcapbuf += dev->compose_cap.left * pixsize + dev->compose_cap.top * stride_cap;
277 if (dev->loop_vid_copy.width == 0 || dev->loop_vid_copy.height == 0) {
279 * If there is nothing to copy, then just fill the capture window
282 for (y = 0; y < hmax; y++, vcapbuf += stride_cap)
283 memcpy(vcapbuf, tpg->black_line[p], img_width * pixsize);
287 if (dev->overlay_out_enabled &&
288 dev->loop_vid_overlay.width && dev->loop_vid_overlay.height) {
289 vosdbuf = dev->video_vbase;
290 vosdbuf += dev->loop_fb_copy.left * pixsize +
291 dev->loop_fb_copy.top * stride_osd;
292 vid_overlay_int_part = dev->loop_vid_overlay.height /
293 dev->loop_vid_overlay_cap.height;
294 vid_overlay_fract_part = dev->loop_vid_overlay.height %
295 dev->loop_vid_overlay_cap.height;
298 vid_cap_right = dev->loop_vid_cap.left + dev->loop_vid_cap.width;
299 /* quick is true if no video scaling is needed */
300 quick = dev->loop_vid_out.width == dev->loop_vid_cap.width;
302 dev->cur_scaled_line = dev->loop_vid_out.height;
303 for (y = 0; y < hmax; y++, vcapbuf += stride_cap) {
304 /* osdline is true if this line requires overlay blending */
305 bool osdline = vosdbuf && y >= dev->loop_vid_overlay_cap.top &&
306 y < dev->loop_vid_overlay_cap.top + dev->loop_vid_overlay_cap.height;
309 * If this line of the capture buffer doesn't get any video, then
310 * just fill with black.
312 if (y < dev->loop_vid_cap.top ||
313 y >= dev->loop_vid_cap.top + dev->loop_vid_cap.height) {
314 memcpy(vcapbuf, tpg->black_line[p], img_width * pixsize);
318 /* fill the left border with black */
319 if (dev->loop_vid_cap.left)
320 memcpy(vcapbuf, tpg->black_line[p], dev->loop_vid_cap.left * pixsize);
322 /* fill the right border with black */
323 if (vid_cap_right < img_width)
324 memcpy(vcapbuf + vid_cap_right * pixsize,
325 tpg->black_line[p], (img_width - vid_cap_right) * pixsize);
327 if (quick && !osdline) {
328 memcpy(vcapbuf + dev->loop_vid_cap.left * pixsize,
329 voutbuf + vid_out_y * stride_out,
330 dev->loop_vid_cap.width * pixsize);
331 goto update_vid_out_y;
333 if (dev->cur_scaled_line == vid_out_y) {
334 memcpy(vcapbuf + dev->loop_vid_cap.left * pixsize,
336 dev->loop_vid_cap.width * pixsize);
337 goto update_vid_out_y;
340 scale_line(voutbuf + vid_out_y * stride_out, dev->scaled_line,
341 dev->loop_vid_out.width, dev->loop_vid_cap.width,
342 tpg_g_twopixelsize(tpg, p));
345 * Offset in bytes within loop_vid_copy to the start of the
346 * loop_vid_overlay rectangle.
349 (dev->loop_vid_overlay.left - dev->loop_vid_copy.left) * pixsize;
350 u8 *osd = vosdbuf + vid_overlay_y * stride_osd;
352 scale_line(voutbuf + vid_out_y * stride_out, dev->blended_line,
353 dev->loop_vid_out.width, dev->loop_vid_copy.width,
354 tpg_g_twopixelsize(tpg, p));
356 blend_line(dev, vid_overlay_y + dev->loop_vid_overlay.top,
357 dev->loop_vid_overlay.left,
358 dev->blended_line + offset, osd,
359 dev->loop_vid_overlay.width, pixsize);
361 memcpy(dev->blended_line + offset,
362 osd, dev->loop_vid_overlay.width * pixsize);
363 scale_line(dev->blended_line, dev->scaled_line,
364 dev->loop_vid_copy.width, dev->loop_vid_cap.width,
365 tpg_g_twopixelsize(tpg, p));
367 dev->cur_scaled_line = vid_out_y;
368 memcpy(vcapbuf + dev->loop_vid_cap.left * pixsize,
370 dev->loop_vid_cap.width * pixsize);
374 vid_overlay_y += vid_overlay_int_part;
375 vid_overlay_error += vid_overlay_fract_part;
376 if (vid_overlay_error >= dev->loop_vid_overlay_cap.height) {
377 vid_overlay_error -= dev->loop_vid_overlay_cap.height;
381 vid_out_y += vid_out_int_part;
382 vid_out_error += vid_out_fract_part;
383 if (vid_out_error >= dev->loop_vid_cap.height) {
384 vid_out_error -= dev->loop_vid_cap.height;
391 for (; y < img_height; y++, vcapbuf += stride_cap)
392 memcpy(vcapbuf, tpg->contrast_line[p], img_width * pixsize);
396 static void vivid_fillbuff(struct vivid_dev *dev, struct vivid_buffer *buf)
398 unsigned factor = V4L2_FIELD_HAS_T_OR_B(dev->field_cap) ? 2 : 1;
399 unsigned line_height = 16 / factor;
400 bool is_tv = vivid_is_sdtv_cap(dev);
401 bool is_60hz = is_tv && (dev->std_cap & V4L2_STD_525_60);
404 u8 *basep[TPG_MAX_PLANES][2];
408 bool is_loop = false;
410 if (dev->loop_video && dev->can_loop_video &&
411 ((vivid_is_svid_cap(dev) && !VIVID_INVALID_SIGNAL(dev->std_signal_mode)) ||
412 (vivid_is_hdmi_cap(dev) && !VIVID_INVALID_SIGNAL(dev->dv_timings_signal_mode))))
415 buf->vb.v4l2_buf.sequence = dev->vid_cap_seq_count;
417 * Take the timestamp now if the timestamp source is set to
418 * "Start of Exposure".
420 if (dev->tstamp_src_is_soe)
421 v4l2_get_timestamp(&buf->vb.v4l2_buf.timestamp);
422 if (dev->field_cap == V4L2_FIELD_ALTERNATE) {
424 * 60 Hz standards start with the bottom field, 50 Hz standards
425 * with the top field. So if the 0-based seq_count is even,
426 * then the field is TOP for 50 Hz and BOTTOM for 60 Hz
429 buf->vb.v4l2_buf.field = ((dev->vid_cap_seq_count & 1) ^ is_60hz) ?
430 V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM;
432 * The sequence counter counts frames, not fields. So divide
435 buf->vb.v4l2_buf.sequence /= 2;
437 buf->vb.v4l2_buf.field = dev->field_cap;
439 tpg_s_field(&dev->tpg, buf->vb.v4l2_buf.field);
440 tpg_s_perc_fill_blank(&dev->tpg, dev->must_blank[buf->vb.v4l2_buf.index]);
442 vivid_precalc_copy_rects(dev);
444 for (p = 0; p < tpg_g_planes(&dev->tpg); p++) {
445 void *vbuf = vb2_plane_vaddr(&buf->vb, p);
448 * The first plane of a multiplanar format has a non-zero
449 * data_offset. This helps testing whether the application
450 * correctly supports non-zero data offsets.
452 if (dev->fmt_cap->data_offset[p]) {
453 memset(vbuf, dev->fmt_cap->data_offset[p] & 0xff,
454 dev->fmt_cap->data_offset[p]);
455 vbuf += dev->fmt_cap->data_offset[p];
457 tpg_calc_text_basep(&dev->tpg, basep, p, vbuf);
458 if (!is_loop || vivid_copy_buffer(dev, p, vbuf, buf))
459 tpg_fillbuffer(&dev->tpg, vivid_get_std_cap(dev), p, vbuf);
461 dev->must_blank[buf->vb.v4l2_buf.index] = false;
463 /* Updates stream time, only update at the start of a new frame. */
464 if (dev->field_cap != V4L2_FIELD_ALTERNATE || (buf->vb.v4l2_buf.sequence & 1) == 0)
465 dev->ms_vid_cap = jiffies_to_msecs(jiffies - dev->jiffies_vid_cap);
467 ms = dev->ms_vid_cap;
468 if (dev->osd_mode <= 1) {
469 snprintf(str, sizeof(str), " %02d:%02d:%02d:%03d %u%s",
470 (ms / (60 * 60 * 1000)) % 24,
471 (ms / (60 * 1000)) % 60,
474 buf->vb.v4l2_buf.sequence,
475 (dev->field_cap == V4L2_FIELD_ALTERNATE) ?
476 (buf->vb.v4l2_buf.field == V4L2_FIELD_TOP ?
477 " top" : " bottom") : "");
478 tpg_gen_text(&dev->tpg, basep, line++ * line_height, 16, str);
480 if (dev->osd_mode == 0) {
481 snprintf(str, sizeof(str), " %dx%d, input %d ",
482 dev->src_rect.width, dev->src_rect.height, dev->input);
483 tpg_gen_text(&dev->tpg, basep, line++ * line_height, 16, str);
485 gain = v4l2_ctrl_g_ctrl(dev->gain);
486 mutex_lock(dev->ctrl_hdl_user_vid.lock);
487 snprintf(str, sizeof(str),
488 " brightness %3d, contrast %3d, saturation %3d, hue %d ",
489 dev->brightness->cur.val,
490 dev->contrast->cur.val,
491 dev->saturation->cur.val,
493 tpg_gen_text(&dev->tpg, basep, line++ * line_height, 16, str);
494 snprintf(str, sizeof(str),
495 " autogain %d, gain %3d, alpha 0x%02x ",
496 dev->autogain->cur.val, gain, dev->alpha->cur.val);
497 mutex_unlock(dev->ctrl_hdl_user_vid.lock);
498 tpg_gen_text(&dev->tpg, basep, line++ * line_height, 16, str);
499 mutex_lock(dev->ctrl_hdl_user_aud.lock);
500 snprintf(str, sizeof(str),
501 " volume %3d, mute %d ",
502 dev->volume->cur.val, dev->mute->cur.val);
503 mutex_unlock(dev->ctrl_hdl_user_aud.lock);
504 tpg_gen_text(&dev->tpg, basep, line++ * line_height, 16, str);
505 mutex_lock(dev->ctrl_hdl_user_gen.lock);
506 snprintf(str, sizeof(str), " int32 %d, int64 %lld, bitmask %08x ",
508 *dev->int64->p_cur.p_s64,
509 dev->bitmask->cur.val);
510 tpg_gen_text(&dev->tpg, basep, line++ * line_height, 16, str);
511 snprintf(str, sizeof(str), " boolean %d, menu %s, string \"%s\" ",
512 dev->boolean->cur.val,
513 dev->menu->qmenu[dev->menu->cur.val],
514 dev->string->p_cur.p_char);
515 tpg_gen_text(&dev->tpg, basep, line++ * line_height, 16, str);
516 snprintf(str, sizeof(str), " integer_menu %lld, value %d ",
517 dev->int_menu->qmenu_int[dev->int_menu->cur.val],
518 dev->int_menu->cur.val);
519 mutex_unlock(dev->ctrl_hdl_user_gen.lock);
520 tpg_gen_text(&dev->tpg, basep, line++ * line_height, 16, str);
521 if (dev->button_pressed) {
522 dev->button_pressed--;
523 snprintf(str, sizeof(str), " button pressed!");
524 tpg_gen_text(&dev->tpg, basep, line++ * line_height, 16, str);
529 * If "End of Frame" is specified at the timestamp source, then take
532 if (!dev->tstamp_src_is_soe)
533 v4l2_get_timestamp(&buf->vb.v4l2_buf.timestamp);
534 buf->vb.v4l2_buf.timestamp.tv_sec += dev->time_wrap_offset;
538 * Return true if this pixel coordinate is a valid video pixel.
540 static bool valid_pix(struct vivid_dev *dev, int win_y, int win_x, int fb_y, int fb_x)
544 if (dev->bitmap_cap) {
546 * Only if the corresponding bit in the bitmap is set can
547 * the video pixel be shown. Coordinates are relative to
548 * the overlay window set by VIDIOC_S_FMT.
550 const u8 *p = dev->bitmap_cap;
551 unsigned stride = (dev->compose_cap.width + 7) / 8;
553 if (!(p[stride * win_y + win_x / 8] & (1 << (win_x & 7))))
557 for (i = 0; i < dev->clipcount_cap; i++) {
559 * Only if the framebuffer coordinate is not in any of the
560 * clip rectangles will be video pixel be shown.
562 struct v4l2_rect *r = &dev->clips_cap[i].c;
564 if (fb_y >= r->top && fb_y < r->top + r->height &&
565 fb_x >= r->left && fb_x < r->left + r->width)
572 * Draw the image into the overlay buffer.
573 * Note that the combination of overlay and multiplanar is not supported.
575 static void vivid_overlay(struct vivid_dev *dev, struct vivid_buffer *buf)
577 struct tpg_data *tpg = &dev->tpg;
578 unsigned pixsize = tpg_g_twopixelsize(tpg, 0) / 2;
579 void *vbase = dev->fb_vbase_cap;
580 void *vbuf = vb2_plane_vaddr(&buf->vb, 0);
581 unsigned img_width = dev->compose_cap.width;
582 unsigned img_height = dev->compose_cap.height;
583 unsigned stride = tpg->bytesperline[0];
584 /* if quick is true, then valid_pix() doesn't have to be called */
585 bool quick = dev->bitmap_cap == NULL && dev->clipcount_cap == 0;
586 int x, y, w, out_x = 0;
588 if ((dev->overlay_cap_field == V4L2_FIELD_TOP ||
589 dev->overlay_cap_field == V4L2_FIELD_BOTTOM) &&
590 dev->overlay_cap_field != buf->vb.v4l2_buf.field)
593 vbuf += dev->compose_cap.left * pixsize + dev->compose_cap.top * stride;
594 x = dev->overlay_cap_left;
601 w = dev->fb_cap.fmt.width - x;
607 if (dev->overlay_cap_top >= 0)
608 vbase += dev->overlay_cap_top * dev->fb_cap.fmt.bytesperline;
609 for (y = dev->overlay_cap_top;
610 y < dev->overlay_cap_top + (int)img_height;
611 y++, vbuf += stride) {
614 if (y < 0 || y > dev->fb_cap.fmt.height)
617 memcpy(vbase + x * pixsize,
618 vbuf + out_x * pixsize, w * pixsize);
619 vbase += dev->fb_cap.fmt.bytesperline;
622 for (px = 0; px < w; px++) {
623 if (!valid_pix(dev, y - dev->overlay_cap_top,
624 px + out_x, y, px + x))
626 memcpy(vbase + (px + x) * pixsize,
627 vbuf + (px + out_x) * pixsize,
630 vbase += dev->fb_cap.fmt.bytesperline;
634 static void vivid_thread_vid_cap_tick(struct vivid_dev *dev, int dropped_bufs)
636 struct vivid_buffer *vid_cap_buf = NULL;
637 struct vivid_buffer *vbi_cap_buf = NULL;
639 dprintk(dev, 1, "Video Capture Thread Tick\n");
641 while (dropped_bufs-- > 1)
642 tpg_update_mv_count(&dev->tpg,
643 dev->field_cap == V4L2_FIELD_NONE ||
644 dev->field_cap == V4L2_FIELD_ALTERNATE);
646 /* Drop a certain percentage of buffers. */
647 if (dev->perc_dropped_buffers &&
648 prandom_u32_max(100) < dev->perc_dropped_buffers)
651 spin_lock(&dev->slock);
652 if (!list_empty(&dev->vid_cap_active)) {
653 vid_cap_buf = list_entry(dev->vid_cap_active.next, struct vivid_buffer, list);
654 list_del(&vid_cap_buf->list);
656 if (!list_empty(&dev->vbi_cap_active)) {
657 if (dev->field_cap != V4L2_FIELD_ALTERNATE ||
658 (dev->vbi_cap_seq_count & 1)) {
659 vbi_cap_buf = list_entry(dev->vbi_cap_active.next,
660 struct vivid_buffer, list);
661 list_del(&vbi_cap_buf->list);
664 spin_unlock(&dev->slock);
666 if (!vid_cap_buf && !vbi_cap_buf)
671 vivid_fillbuff(dev, vid_cap_buf);
672 dprintk(dev, 1, "filled buffer %d\n",
673 vid_cap_buf->vb.v4l2_buf.index);
676 if (dev->overlay_cap_owner && dev->fb_cap.base &&
677 dev->fb_cap.fmt.pixelformat == dev->fmt_cap->fourcc)
678 vivid_overlay(dev, vid_cap_buf);
680 vb2_buffer_done(&vid_cap_buf->vb, dev->dqbuf_error ?
681 VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
682 dprintk(dev, 2, "vid_cap buffer %d done\n",
683 vid_cap_buf->vb.v4l2_buf.index);
687 if (dev->stream_sliced_vbi_cap)
688 vivid_sliced_vbi_cap_process(dev, vbi_cap_buf);
690 vivid_raw_vbi_cap_process(dev, vbi_cap_buf);
691 vb2_buffer_done(&vbi_cap_buf->vb, dev->dqbuf_error ?
692 VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
693 dprintk(dev, 2, "vbi_cap %d done\n",
694 vbi_cap_buf->vb.v4l2_buf.index);
696 dev->dqbuf_error = false;
699 /* Update the test pattern movement counters */
700 tpg_update_mv_count(&dev->tpg, dev->field_cap == V4L2_FIELD_NONE ||
701 dev->field_cap == V4L2_FIELD_ALTERNATE);
704 static int vivid_thread_vid_cap(void *data)
706 struct vivid_dev *dev = data;
707 u64 numerators_since_start;
708 u64 buffers_since_start;
709 u64 next_jiffies_since_start;
710 unsigned long jiffies_since_start;
711 unsigned long cur_jiffies;
712 unsigned wait_jiffies;
714 unsigned denominator;
717 dprintk(dev, 1, "Video Capture Thread Start\n");
721 /* Resets frame counters */
722 dev->cap_seq_offset = 0;
723 dev->cap_seq_count = 0;
724 dev->cap_seq_resync = false;
725 dev->jiffies_vid_cap = jiffies;
729 if (kthread_should_stop())
732 mutex_lock(&dev->mutex);
733 cur_jiffies = jiffies;
734 if (dev->cap_seq_resync) {
735 dev->jiffies_vid_cap = cur_jiffies;
736 dev->cap_seq_offset = dev->cap_seq_count + 1;
737 dev->cap_seq_count = 0;
738 dev->cap_seq_resync = false;
740 numerator = dev->timeperframe_vid_cap.numerator;
741 denominator = dev->timeperframe_vid_cap.denominator;
743 if (dev->field_cap == V4L2_FIELD_ALTERNATE)
746 /* Calculate the number of jiffies since we started streaming */
747 jiffies_since_start = cur_jiffies - dev->jiffies_vid_cap;
748 /* Get the number of buffers streamed since the start */
749 buffers_since_start = (u64)jiffies_since_start * denominator +
750 (HZ * numerator) / 2;
751 do_div(buffers_since_start, HZ * numerator);
754 * After more than 0xf0000000 (rounded down to a multiple of
755 * 'jiffies-per-day' to ease jiffies_to_msecs calculation)
756 * jiffies have passed since we started streaming reset the
757 * counters and keep track of the sequence offset.
759 if (jiffies_since_start > JIFFIES_RESYNC) {
760 dev->jiffies_vid_cap = cur_jiffies;
761 dev->cap_seq_offset = buffers_since_start;
762 buffers_since_start = 0;
764 dropped_bufs = buffers_since_start + dev->cap_seq_offset - dev->cap_seq_count;
765 dev->cap_seq_count = buffers_since_start + dev->cap_seq_offset;
766 dev->vid_cap_seq_count = dev->cap_seq_count - dev->vid_cap_seq_start;
767 dev->vbi_cap_seq_count = dev->cap_seq_count - dev->vbi_cap_seq_start;
769 vivid_thread_vid_cap_tick(dev, dropped_bufs);
772 * Calculate the number of 'numerators' streamed since we started,
773 * including the current buffer.
775 numerators_since_start = ++buffers_since_start * numerator;
777 /* And the number of jiffies since we started */
778 jiffies_since_start = jiffies - dev->jiffies_vid_cap;
780 mutex_unlock(&dev->mutex);
783 * Calculate when that next buffer is supposed to start
784 * in jiffies since we started streaming.
786 next_jiffies_since_start = numerators_since_start * HZ +
788 do_div(next_jiffies_since_start, denominator);
789 /* If it is in the past, then just schedule asap */
790 if (next_jiffies_since_start < jiffies_since_start)
791 next_jiffies_since_start = jiffies_since_start;
793 wait_jiffies = next_jiffies_since_start - jiffies_since_start;
794 schedule_timeout_interruptible(wait_jiffies ? wait_jiffies : 1);
796 dprintk(dev, 1, "Video Capture Thread End\n");
800 static void vivid_grab_controls(struct vivid_dev *dev, bool grab)
802 v4l2_ctrl_grab(dev->ctrl_has_crop_cap, grab);
803 v4l2_ctrl_grab(dev->ctrl_has_compose_cap, grab);
804 v4l2_ctrl_grab(dev->ctrl_has_scaler_cap, grab);
807 int vivid_start_generating_vid_cap(struct vivid_dev *dev, bool *pstreaming)
809 dprintk(dev, 1, "%s\n", __func__);
811 if (dev->kthread_vid_cap) {
812 u32 seq_count = dev->cap_seq_count + dev->seq_wrap * 128;
814 if (pstreaming == &dev->vid_cap_streaming)
815 dev->vid_cap_seq_start = seq_count;
817 dev->vbi_cap_seq_start = seq_count;
822 /* Resets frame counters */
823 tpg_init_mv_count(&dev->tpg);
825 dev->vid_cap_seq_start = dev->seq_wrap * 128;
826 dev->vbi_cap_seq_start = dev->seq_wrap * 128;
828 dev->kthread_vid_cap = kthread_run(vivid_thread_vid_cap, dev,
829 "%s-vid-cap", dev->v4l2_dev.name);
831 if (IS_ERR(dev->kthread_vid_cap)) {
832 v4l2_err(&dev->v4l2_dev, "kernel_thread() failed\n");
833 return PTR_ERR(dev->kthread_vid_cap);
836 vivid_grab_controls(dev, true);
838 dprintk(dev, 1, "returning from %s\n", __func__);
842 void vivid_stop_generating_vid_cap(struct vivid_dev *dev, bool *pstreaming)
844 dprintk(dev, 1, "%s\n", __func__);
846 if (dev->kthread_vid_cap == NULL)
850 if (pstreaming == &dev->vid_cap_streaming) {
851 /* Release all active buffers */
852 while (!list_empty(&dev->vid_cap_active)) {
853 struct vivid_buffer *buf;
855 buf = list_entry(dev->vid_cap_active.next,
856 struct vivid_buffer, list);
857 list_del(&buf->list);
858 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
859 dprintk(dev, 2, "vid_cap buffer %d done\n",
860 buf->vb.v4l2_buf.index);
864 if (pstreaming == &dev->vbi_cap_streaming) {
865 while (!list_empty(&dev->vbi_cap_active)) {
866 struct vivid_buffer *buf;
868 buf = list_entry(dev->vbi_cap_active.next,
869 struct vivid_buffer, list);
870 list_del(&buf->list);
871 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
872 dprintk(dev, 2, "vbi_cap buffer %d done\n",
873 buf->vb.v4l2_buf.index);
877 if (dev->vid_cap_streaming || dev->vbi_cap_streaming)
880 /* shutdown control thread */
881 vivid_grab_controls(dev, false);
882 mutex_unlock(&dev->mutex);
883 kthread_stop(dev->kthread_vid_cap);
884 dev->kthread_vid_cap = NULL;
885 mutex_lock(&dev->mutex);