blob: 39a67cfae120079985598e8fc6500dac85d95265 [file] [log] [blame]
Hans Verkuil3f682ff2014-08-25 08:00:20 -03001/*
2 * vivid-kthread-cap.h - video/vbi capture thread support functions.
3 *
4 * Copyright 2014 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
5 *
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.
9 *
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
17 * SOFTWARE.
18 */
19
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>
39
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"
Mauro Carvalho Chehab96ec7d22014-09-02 17:57:34 -030052#include "vivid-kthread-cap.h"
Hans Verkuil3f682ff2014-08-25 08:00:20 -030053
54static inline v4l2_std_id vivid_get_std_cap(const struct vivid_dev *dev)
55{
56 if (vivid_is_sdtv_cap(dev))
57 return dev->std_cap;
58 return 0;
59}
60
61static void copy_pix(struct vivid_dev *dev, int win_y, int win_x,
62 u16 *cap, const u16 *osd)
63{
64 u16 out;
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;
69 int i;
70
71 out = *cap;
72 *cap = *osd;
73 if (dev->bitmap_out) {
74 const u8 *p = dev->bitmap_out;
75 unsigned stride = (dev->compose_out.width + 7) / 8;
76
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))))
80 return;
81 }
82
83 for (i = 0; i < dev->clipcount_out; i++) {
84 struct v4l2_rect *r = &dev->clips_out[i].c;
85
86 if (fb_y >= r->top && fb_y < r->top + r->height &&
87 fb_x >= r->left && fb_x < r->left + r->width)
88 return;
89 }
90 if ((dev->fbuf_out_flags & V4L2_FBUF_FLAG_CHROMAKEY) &&
91 *osd != dev->chromakey_out)
92 return;
93 if ((dev->fbuf_out_flags & V4L2_FBUF_FLAG_SRC_CHROMAKEY) &&
94 out == dev->chromakey_out)
95 return;
96 if (dev->fmt_cap->alpha_mask) {
97 if ((dev->fbuf_out_flags & V4L2_FBUF_FLAG_GLOBAL_ALPHA) &&
98 dev->global_alpha_out)
99 return;
100 if ((dev->fbuf_out_flags & V4L2_FBUF_FLAG_LOCAL_ALPHA) &&
101 *cap & dev->fmt_cap->alpha_mask)
102 return;
103 if ((dev->fbuf_out_flags & V4L2_FBUF_FLAG_LOCAL_INV_ALPHA) &&
104 !(*cap & dev->fmt_cap->alpha_mask))
105 return;
106 }
107 *cap = out;
108}
109
110static void blend_line(struct vivid_dev *dev, unsigned y_offset, unsigned x_offset,
111 u8 *vcapbuf, const u8 *vosdbuf,
112 unsigned width, unsigned pixsize)
113{
114 unsigned x;
115
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);
119 }
120}
121
122static void scale_line(const u8 *src, u8 *dst, unsigned srcw, unsigned dstw, unsigned twopixsize)
123{
124 /* Coarse scaling with Bresenham */
125 unsigned int_part;
126 unsigned fract_part;
127 unsigned src_x = 0;
128 unsigned error = 0;
129 unsigned x;
130
131 /*
132 * We always combine two pixels to prevent color bleed in the packed
133 * yuv case.
134 */
135 srcw /= 2;
136 dstw /= 2;
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);
141 src_x += int_part;
142 error += fract_part;
143 if (error >= dstw) {
144 error -= dstw;
145 src_x++;
146 }
147 }
148}
149
150/*
151 * Precalculate the rectangles needed to perform video looping:
152 *
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.
157 *
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.
164 *
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.
174 */
175static void vivid_precalc_copy_rects(struct vivid_dev *dev)
176{
177 /* Framebuffer rectangle */
178 struct v4l2_rect r_fb = {
179 0, 0, dev->display_width, dev->display_height
180 };
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
185 };
186
187 dev->loop_vid_copy = rect_intersect(&dev->crop_cap, &dev->compose_out);
188
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;
193
194 dev->loop_vid_cap = dev->loop_vid_copy;
195 rect_scale(&dev->loop_vid_cap, &dev->crop_cap, &dev->compose_cap);
196
197 dprintk(dev, 1,
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);
205
206 r_overlay = rect_intersect(&r_fb, &r_overlay);
207
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;
211
212 dev->loop_vid_overlay = rect_intersect(&r_overlay, &dev->loop_vid_copy);
213 dev->loop_fb_copy = dev->loop_vid_overlay;
214
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;
218
219 dev->loop_vid_overlay_cap = dev->loop_vid_overlay;
220 rect_scale(&dev->loop_vid_overlay_cap, &dev->crop_cap, &dev->compose_cap);
221
222 dprintk(dev, 1,
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);
230}
231
232static int vivid_copy_buffer(struct vivid_dev *dev, unsigned p, u8 *vcapbuf,
233 struct vivid_buffer *vid_cap_buf)
234{
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;
245 u8 *voutbuf;
246 u8 *vosdbuf = NULL;
247 unsigned y;
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;
259 bool quick;
260
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;
263
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)
268 return -ENODATA;
269
270 vid_cap_buf->vb.v4l2_buf.field = vid_out_buf->vb.v4l2_buf.field;
271
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;
276
277 if (dev->loop_vid_copy.width == 0 || dev->loop_vid_copy.height == 0) {
278 /*
279 * If there is nothing to copy, then just fill the capture window
280 * with black.
281 */
282 for (y = 0; y < hmax; y++, vcapbuf += stride_cap)
283 memcpy(vcapbuf, tpg->black_line[p], img_width * pixsize);
284 return 0;
285 }
286
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;
296 }
297
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;
301
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;
307
308 /*
309 * If this line of the capture buffer doesn't get any video, then
310 * just fill with black.
311 */
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);
315 continue;
316 }
317
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);
321
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);
326
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;
332 }
333 if (dev->cur_scaled_line == vid_out_y) {
334 memcpy(vcapbuf + dev->loop_vid_cap.left * pixsize,
335 dev->scaled_line,
336 dev->loop_vid_cap.width * pixsize);
337 goto update_vid_out_y;
338 }
339 if (!osdline) {
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));
343 } else {
344 /*
345 * Offset in bytes within loop_vid_copy to the start of the
346 * loop_vid_overlay rectangle.
347 */
348 unsigned offset =
349 (dev->loop_vid_overlay.left - dev->loop_vid_copy.left) * pixsize;
350 u8 *osd = vosdbuf + vid_overlay_y * stride_osd;
351
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));
355 if (blend)
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);
360 else
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));
366 }
367 dev->cur_scaled_line = vid_out_y;
368 memcpy(vcapbuf + dev->loop_vid_cap.left * pixsize,
369 dev->scaled_line,
370 dev->loop_vid_cap.width * pixsize);
371
372update_vid_out_y:
373 if (osdline) {
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;
378 vid_overlay_y++;
379 }
380 }
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;
385 vid_out_y++;
386 }
387 }
388
389 if (!blank)
390 return 0;
391 for (; y < img_height; y++, vcapbuf += stride_cap)
392 memcpy(vcapbuf, tpg->contrast_line[p], img_width * pixsize);
393 return 0;
394}
395
396static void vivid_fillbuff(struct vivid_dev *dev, struct vivid_buffer *buf)
397{
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);
402 unsigned p;
403 int line = 1;
404 u8 *basep[TPG_MAX_PLANES][2];
405 unsigned ms;
406 char str[100];
407 s32 gain;
408 bool is_loop = false;
409
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))))
413 is_loop = true;
414
415 buf->vb.v4l2_buf.sequence = dev->vid_cap_seq_count;
416 /*
417 * Take the timestamp now if the timestamp source is set to
418 * "Start of Exposure".
419 */
420 if (dev->tstamp_src_is_soe)
421 v4l2_get_timestamp(&buf->vb.v4l2_buf.timestamp);
422 if (dev->field_cap == V4L2_FIELD_ALTERNATE) {
423 /*
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
427 * standards.
428 */
429 buf->vb.v4l2_buf.field = ((dev->vid_cap_seq_count & 1) ^ is_60hz) ?
430 V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM;
431 /*
432 * The sequence counter counts frames, not fields. So divide
433 * by two.
434 */
435 buf->vb.v4l2_buf.sequence /= 2;
436 } else {
437 buf->vb.v4l2_buf.field = dev->field_cap;
438 }
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]);
441
442 vivid_precalc_copy_rects(dev);
443
444 for (p = 0; p < tpg_g_planes(&dev->tpg); p++) {
445 void *vbuf = vb2_plane_vaddr(&buf->vb, p);
446
447 /*
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.
451 */
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];
456 }
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);
460 }
461 dev->must_blank[buf->vb.v4l2_buf.index] = false;
462
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);
466
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,
472 (ms / 1000) % 60,
473 ms % 1000,
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);
479 }
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);
484
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,
492 dev->hue->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 ",
507 dev->int32->cur.val,
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);
525 }
526 }
527
528 /*
529 * If "End of Frame" is specified at the timestamp source, then take
530 * the timestamp now.
531 */
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;
535}
536
537/*
538 * Return true if this pixel coordinate is a valid video pixel.
539 */
540static bool valid_pix(struct vivid_dev *dev, int win_y, int win_x, int fb_y, int fb_x)
541{
542 int i;
543
544 if (dev->bitmap_cap) {
545 /*
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.
549 */
550 const u8 *p = dev->bitmap_cap;
551 unsigned stride = (dev->compose_cap.width + 7) / 8;
552
553 if (!(p[stride * win_y + win_x / 8] & (1 << (win_x & 7))))
554 return false;
555 }
556
557 for (i = 0; i < dev->clipcount_cap; i++) {
558 /*
559 * Only if the framebuffer coordinate is not in any of the
560 * clip rectangles will be video pixel be shown.
561 */
562 struct v4l2_rect *r = &dev->clips_cap[i].c;
563
564 if (fb_y >= r->top && fb_y < r->top + r->height &&
565 fb_x >= r->left && fb_x < r->left + r->width)
566 return false;
567 }
568 return true;
569}
570
571/*
572 * Draw the image into the overlay buffer.
573 * Note that the combination of overlay and multiplanar is not supported.
574 */
575static void vivid_overlay(struct vivid_dev *dev, struct vivid_buffer *buf)
576{
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;
587
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)
591 return;
592
593 vbuf += dev->compose_cap.left * pixsize + dev->compose_cap.top * stride;
594 x = dev->overlay_cap_left;
595 w = img_width;
596 if (x < 0) {
597 out_x = -x;
598 w = w - out_x;
599 x = 0;
600 } else {
601 w = dev->fb_cap.fmt.width - x;
602 if (w > img_width)
603 w = img_width;
604 }
605 if (w <= 0)
606 return;
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) {
612 int px;
613
614 if (y < 0 || y > dev->fb_cap.fmt.height)
615 continue;
616 if (quick) {
617 memcpy(vbase + x * pixsize,
618 vbuf + out_x * pixsize, w * pixsize);
619 vbase += dev->fb_cap.fmt.bytesperline;
620 continue;
621 }
622 for (px = 0; px < w; px++) {
623 if (!valid_pix(dev, y - dev->overlay_cap_top,
624 px + out_x, y, px + x))
625 continue;
626 memcpy(vbase + (px + x) * pixsize,
627 vbuf + (px + out_x) * pixsize,
628 pixsize);
629 }
630 vbase += dev->fb_cap.fmt.bytesperline;
631 }
632}
633
634static void vivid_thread_vid_cap_tick(struct vivid_dev *dev, int dropped_bufs)
635{
636 struct vivid_buffer *vid_cap_buf = NULL;
637 struct vivid_buffer *vbi_cap_buf = NULL;
638
639 dprintk(dev, 1, "Video Capture Thread Tick\n");
640
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);
645
646 /* Drop a certain percentage of buffers. */
647 if (dev->perc_dropped_buffers &&
648 prandom_u32_max(100) < dev->perc_dropped_buffers)
649 goto update_mv;
650
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);
655 }
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);
662 }
663 }
664 spin_unlock(&dev->slock);
665
666 if (!vid_cap_buf && !vbi_cap_buf)
667 goto update_mv;
668
669 if (vid_cap_buf) {
670 /* Fill buffer */
671 vivid_fillbuff(dev, vid_cap_buf);
672 dprintk(dev, 1, "filled buffer %d\n",
673 vid_cap_buf->vb.v4l2_buf.index);
674
675 /* Handle overlay */
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);
679
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);
684 }
685
686 if (vbi_cap_buf) {
687 if (dev->stream_sliced_vbi_cap)
688 vivid_sliced_vbi_cap_process(dev, vbi_cap_buf);
689 else
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);
695 }
696 dev->dqbuf_error = false;
697
698update_mv:
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);
702}
703
704static int vivid_thread_vid_cap(void *data)
705{
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;
713 unsigned numerator;
714 unsigned denominator;
715 int dropped_bufs;
716
717 dprintk(dev, 1, "Video Capture Thread Start\n");
718
719 set_freezable();
720
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;
726
727 for (;;) {
728 try_to_freeze();
729 if (kthread_should_stop())
730 break;
731
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;
739 }
740 numerator = dev->timeperframe_vid_cap.numerator;
741 denominator = dev->timeperframe_vid_cap.denominator;
742
743 if (dev->field_cap == V4L2_FIELD_ALTERNATE)
744 denominator *= 2;
745
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);
752
753 /*
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.
758 */
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;
763 }
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;
768
769 vivid_thread_vid_cap_tick(dev, dropped_bufs);
770
771 /*
772 * Calculate the number of 'numerators' streamed since we started,
773 * including the current buffer.
774 */
775 numerators_since_start = ++buffers_since_start * numerator;
776
777 /* And the number of jiffies since we started */
778 jiffies_since_start = jiffies - dev->jiffies_vid_cap;
779
780 mutex_unlock(&dev->mutex);
781
782 /*
783 * Calculate when that next buffer is supposed to start
784 * in jiffies since we started streaming.
785 */
786 next_jiffies_since_start = numerators_since_start * HZ +
787 denominator / 2;
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;
792
793 wait_jiffies = next_jiffies_since_start - jiffies_since_start;
794 schedule_timeout_interruptible(wait_jiffies ? wait_jiffies : 1);
795 }
796 dprintk(dev, 1, "Video Capture Thread End\n");
797 return 0;
798}
799
800static void vivid_grab_controls(struct vivid_dev *dev, bool grab)
801{
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);
805}
806
807int vivid_start_generating_vid_cap(struct vivid_dev *dev, bool *pstreaming)
808{
809 dprintk(dev, 1, "%s\n", __func__);
810
811 if (dev->kthread_vid_cap) {
812 u32 seq_count = dev->cap_seq_count + dev->seq_wrap * 128;
813
814 if (pstreaming == &dev->vid_cap_streaming)
815 dev->vid_cap_seq_start = seq_count;
816 else
817 dev->vbi_cap_seq_start = seq_count;
818 *pstreaming = true;
819 return 0;
820 }
821
822 /* Resets frame counters */
823 tpg_init_mv_count(&dev->tpg);
824
825 dev->vid_cap_seq_start = dev->seq_wrap * 128;
826 dev->vbi_cap_seq_start = dev->seq_wrap * 128;
827
828 dev->kthread_vid_cap = kthread_run(vivid_thread_vid_cap, dev,
829 "%s-vid-cap", dev->v4l2_dev.name);
830
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);
834 }
835 *pstreaming = true;
836 vivid_grab_controls(dev, true);
837
838 dprintk(dev, 1, "returning from %s\n", __func__);
839 return 0;
840}
841
842void vivid_stop_generating_vid_cap(struct vivid_dev *dev, bool *pstreaming)
843{
844 dprintk(dev, 1, "%s\n", __func__);
845
846 if (dev->kthread_vid_cap == NULL)
847 return;
848
849 *pstreaming = false;
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;
854
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);
861 }
862 }
863
864 if (pstreaming == &dev->vbi_cap_streaming) {
865 while (!list_empty(&dev->vbi_cap_active)) {
866 struct vivid_buffer *buf;
867
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);
874 }
875 }
876
877 if (dev->vid_cap_streaming || dev->vbi_cap_streaming)
878 return;
879
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);
886}