1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * vivid-kthread-cap.h - video/vbi capture thread support functions.
4  *
5  * Copyright 2014 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
6  */
7 
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>
28 
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 
vivid_get_std_cap(const struct vivid_dev * dev)43 static inline v4l2_std_id vivid_get_std_cap(const struct vivid_dev *dev)
44 {
45 	if (vivid_is_sdtv_cap(dev))
46 		return dev->std_cap;
47 	return 0;
48 }
49 
copy_pix(struct vivid_dev * dev,int win_y,int win_x,u16 * cap,const u16 * osd)50 static void copy_pix(struct vivid_dev *dev, int win_y, int win_x,
51 			u16 *cap, const u16 *osd)
52 {
53 	u16 out;
54 	int left = dev->overlay_out_left;
55 	int top = dev->overlay_out_top;
56 	int fb_x = win_x + left;
57 	int fb_y = win_y + top;
58 	int i;
59 
60 	out = *cap;
61 	*cap = *osd;
62 	if (dev->bitmap_out) {
63 		const u8 *p = dev->bitmap_out;
64 		unsigned stride = (dev->compose_out.width + 7) / 8;
65 
66 		win_x -= dev->compose_out.left;
67 		win_y -= dev->compose_out.top;
68 		if (!(p[stride * win_y + win_x / 8] & (1 << (win_x & 7))))
69 			return;
70 	}
71 
72 	for (i = 0; i < dev->clipcount_out; i++) {
73 		struct v4l2_rect *r = &dev->clips_out[i].c;
74 
75 		if (fb_y >= r->top && fb_y < r->top + r->height &&
76 		    fb_x >= r->left && fb_x < r->left + r->width)
77 			return;
78 	}
79 	if ((dev->fbuf_out_flags & V4L2_FBUF_FLAG_CHROMAKEY) &&
80 	    *osd != dev->chromakey_out)
81 		return;
82 	if ((dev->fbuf_out_flags & V4L2_FBUF_FLAG_SRC_CHROMAKEY) &&
83 	    out == dev->chromakey_out)
84 		return;
85 	if (dev->fmt_cap->alpha_mask) {
86 		if ((dev->fbuf_out_flags & V4L2_FBUF_FLAG_GLOBAL_ALPHA) &&
87 		    dev->global_alpha_out)
88 			return;
89 		if ((dev->fbuf_out_flags & V4L2_FBUF_FLAG_LOCAL_ALPHA) &&
90 		    *cap & dev->fmt_cap->alpha_mask)
91 			return;
92 		if ((dev->fbuf_out_flags & V4L2_FBUF_FLAG_LOCAL_INV_ALPHA) &&
93 		    !(*cap & dev->fmt_cap->alpha_mask))
94 			return;
95 	}
96 	*cap = out;
97 }
98 
blend_line(struct vivid_dev * dev,unsigned y_offset,unsigned x_offset,u8 * vcapbuf,const u8 * vosdbuf,unsigned width,unsigned pixsize)99 static void blend_line(struct vivid_dev *dev, unsigned y_offset, unsigned x_offset,
100 		u8 *vcapbuf, const u8 *vosdbuf,
101 		unsigned width, unsigned pixsize)
102 {
103 	unsigned x;
104 
105 	for (x = 0; x < width; x++, vcapbuf += pixsize, vosdbuf += pixsize) {
106 		copy_pix(dev, y_offset, x_offset + x,
107 			 (u16 *)vcapbuf, (const u16 *)vosdbuf);
108 	}
109 }
110 
scale_line(const u8 * src,u8 * dst,unsigned srcw,unsigned dstw,unsigned twopixsize)111 static void scale_line(const u8 *src, u8 *dst, unsigned srcw, unsigned dstw, unsigned twopixsize)
112 {
113 	/* Coarse scaling with Bresenham */
114 	unsigned int_part;
115 	unsigned fract_part;
116 	unsigned src_x = 0;
117 	unsigned error = 0;
118 	unsigned x;
119 
120 	/*
121 	 * We always combine two pixels to prevent color bleed in the packed
122 	 * yuv case.
123 	 */
124 	srcw /= 2;
125 	dstw /= 2;
126 	int_part = srcw / dstw;
127 	fract_part = srcw % dstw;
128 	for (x = 0; x < dstw; x++, dst += twopixsize) {
129 		memcpy(dst, src + src_x * twopixsize, twopixsize);
130 		src_x += int_part;
131 		error += fract_part;
132 		if (error >= dstw) {
133 			error -= dstw;
134 			src_x++;
135 		}
136 	}
137 }
138 
139 /*
140  * Precalculate the rectangles needed to perform video looping:
141  *
142  * The nominal pipeline is that the video output buffer is cropped by
143  * crop_out, scaled to compose_out, overlaid with the output overlay,
144  * cropped on the capture side by crop_cap and scaled again to the video
145  * capture buffer using compose_cap.
146  *
147  * To keep things efficient we calculate the intersection of compose_out
148  * and crop_cap (since that's the only part of the video that will
149  * actually end up in the capture buffer), determine which part of the
150  * video output buffer that is and which part of the video capture buffer
151  * so we can scale the video straight from the output buffer to the capture
152  * buffer without any intermediate steps.
153  *
154  * If we need to deal with an output overlay, then there is no choice and
155  * that intermediate step still has to be taken. For the output overlay
156  * support we calculate the intersection of the framebuffer and the overlay
157  * window (which may be partially or wholly outside of the framebuffer
158  * itself) and the intersection of that with loop_vid_copy (i.e. the part of
159  * the actual looped video that will be overlaid). The result is calculated
160  * both in framebuffer coordinates (loop_fb_copy) and compose_out coordinates
161  * (loop_vid_overlay). Finally calculate the part of the capture buffer that
162  * will receive that overlaid video.
163  */
vivid_precalc_copy_rects(struct vivid_dev * dev)164 static void vivid_precalc_copy_rects(struct vivid_dev *dev)
165 {
166 	/* Framebuffer rectangle */
167 	struct v4l2_rect r_fb = {
168 		0, 0, dev->display_width, dev->display_height
169 	};
170 	/* Overlay window rectangle in framebuffer coordinates */
171 	struct v4l2_rect r_overlay = {
172 		dev->overlay_out_left, dev->overlay_out_top,
173 		dev->compose_out.width, dev->compose_out.height
174 	};
175 
176 	v4l2_rect_intersect(&dev->loop_vid_copy, &dev->crop_cap, &dev->compose_out);
177 
178 	dev->loop_vid_out = dev->loop_vid_copy;
179 	v4l2_rect_scale(&dev->loop_vid_out, &dev->compose_out, &dev->crop_out);
180 	dev->loop_vid_out.left += dev->crop_out.left;
181 	dev->loop_vid_out.top += dev->crop_out.top;
182 
183 	dev->loop_vid_cap = dev->loop_vid_copy;
184 	v4l2_rect_scale(&dev->loop_vid_cap, &dev->crop_cap, &dev->compose_cap);
185 
186 	dprintk(dev, 1,
187 		"loop_vid_copy: %dx%d@%dx%d loop_vid_out: %dx%d@%dx%d loop_vid_cap: %dx%d@%dx%d\n",
188 		dev->loop_vid_copy.width, dev->loop_vid_copy.height,
189 		dev->loop_vid_copy.left, dev->loop_vid_copy.top,
190 		dev->loop_vid_out.width, dev->loop_vid_out.height,
191 		dev->loop_vid_out.left, dev->loop_vid_out.top,
192 		dev->loop_vid_cap.width, dev->loop_vid_cap.height,
193 		dev->loop_vid_cap.left, dev->loop_vid_cap.top);
194 
195 	v4l2_rect_intersect(&r_overlay, &r_fb, &r_overlay);
196 
197 	/* shift r_overlay to the same origin as compose_out */
198 	r_overlay.left += dev->compose_out.left - dev->overlay_out_left;
199 	r_overlay.top += dev->compose_out.top - dev->overlay_out_top;
200 
201 	v4l2_rect_intersect(&dev->loop_vid_overlay, &r_overlay, &dev->loop_vid_copy);
202 	dev->loop_fb_copy = dev->loop_vid_overlay;
203 
204 	/* shift dev->loop_fb_copy back again to the fb origin */
205 	dev->loop_fb_copy.left -= dev->compose_out.left - dev->overlay_out_left;
206 	dev->loop_fb_copy.top -= dev->compose_out.top - dev->overlay_out_top;
207 
208 	dev->loop_vid_overlay_cap = dev->loop_vid_overlay;
209 	v4l2_rect_scale(&dev->loop_vid_overlay_cap, &dev->crop_cap, &dev->compose_cap);
210 
211 	dprintk(dev, 1,
212 		"loop_fb_copy: %dx%d@%dx%d loop_vid_overlay: %dx%d@%dx%d loop_vid_overlay_cap: %dx%d@%dx%d\n",
213 		dev->loop_fb_copy.width, dev->loop_fb_copy.height,
214 		dev->loop_fb_copy.left, dev->loop_fb_copy.top,
215 		dev->loop_vid_overlay.width, dev->loop_vid_overlay.height,
216 		dev->loop_vid_overlay.left, dev->loop_vid_overlay.top,
217 		dev->loop_vid_overlay_cap.width, dev->loop_vid_overlay_cap.height,
218 		dev->loop_vid_overlay_cap.left, dev->loop_vid_overlay_cap.top);
219 }
220 
plane_vaddr(struct tpg_data * tpg,struct vivid_buffer * buf,unsigned p,unsigned bpl[TPG_MAX_PLANES],unsigned h)221 static void *plane_vaddr(struct tpg_data *tpg, struct vivid_buffer *buf,
222 			 unsigned p, unsigned bpl[TPG_MAX_PLANES], unsigned h)
223 {
224 	unsigned i;
225 	void *vbuf;
226 
227 	if (p == 0 || tpg_g_buffers(tpg) > 1)
228 		return vb2_plane_vaddr(&buf->vb.vb2_buf, p);
229 	vbuf = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
230 	for (i = 0; i < p; i++)
231 		vbuf += bpl[i] * h / tpg->vdownsampling[i];
232 	return vbuf;
233 }
234 
vivid_copy_buffer(struct vivid_dev * dev,unsigned p,u8 * vcapbuf,struct vivid_buffer * vid_cap_buf)235 static int vivid_copy_buffer(struct vivid_dev *dev, unsigned p, u8 *vcapbuf,
236 		struct vivid_buffer *vid_cap_buf)
237 {
238 	bool blank = dev->must_blank[vid_cap_buf->vb.vb2_buf.index];
239 	struct tpg_data *tpg = &dev->tpg;
240 	struct vivid_buffer *vid_out_buf = NULL;
241 	unsigned vdiv = dev->fmt_out->vdownsampling[p];
242 	unsigned twopixsize = tpg_g_twopixelsize(tpg, p);
243 	unsigned img_width = tpg_hdiv(tpg, p, dev->compose_cap.width);
244 	unsigned img_height = dev->compose_cap.height;
245 	unsigned stride_cap = tpg->bytesperline[p];
246 	unsigned stride_out = dev->bytesperline_out[p];
247 	unsigned stride_osd = dev->display_byte_stride;
248 	unsigned hmax = (img_height * tpg->perc_fill) / 100;
249 	u8 *voutbuf;
250 	u8 *vosdbuf = NULL;
251 	unsigned y;
252 	bool blend = dev->bitmap_out || dev->clipcount_out || dev->fbuf_out_flags;
253 	/* Coarse scaling with Bresenham */
254 	unsigned vid_out_int_part;
255 	unsigned vid_out_fract_part;
256 	unsigned vid_out_y = 0;
257 	unsigned vid_out_error = 0;
258 	unsigned vid_overlay_int_part = 0;
259 	unsigned vid_overlay_fract_part = 0;
260 	unsigned vid_overlay_y = 0;
261 	unsigned vid_overlay_error = 0;
262 	unsigned vid_cap_left = tpg_hdiv(tpg, p, dev->loop_vid_cap.left);
263 	unsigned vid_cap_right;
264 	bool quick;
265 
266 	vid_out_int_part = dev->loop_vid_out.height / dev->loop_vid_cap.height;
267 	vid_out_fract_part = dev->loop_vid_out.height % dev->loop_vid_cap.height;
268 
269 	if (!list_empty(&dev->vid_out_active))
270 		vid_out_buf = list_entry(dev->vid_out_active.next,
271 					 struct vivid_buffer, list);
272 	if (vid_out_buf == NULL)
273 		return -ENODATA;
274 
275 	vid_cap_buf->vb.field = vid_out_buf->vb.field;
276 
277 	voutbuf = plane_vaddr(tpg, vid_out_buf, p,
278 			      dev->bytesperline_out, dev->fmt_out_rect.height);
279 	if (p < dev->fmt_out->buffers)
280 		voutbuf += vid_out_buf->vb.vb2_buf.planes[p].data_offset;
281 	voutbuf += tpg_hdiv(tpg, p, dev->loop_vid_out.left) +
282 		(dev->loop_vid_out.top / vdiv) * stride_out;
283 	vcapbuf += tpg_hdiv(tpg, p, dev->compose_cap.left) +
284 		(dev->compose_cap.top / vdiv) * stride_cap;
285 
286 	if (dev->loop_vid_copy.width == 0 || dev->loop_vid_copy.height == 0) {
287 		/*
288 		 * If there is nothing to copy, then just fill the capture window
289 		 * with black.
290 		 */
291 		for (y = 0; y < hmax / vdiv; y++, vcapbuf += stride_cap)
292 			memcpy(vcapbuf, tpg->black_line[p], img_width);
293 		return 0;
294 	}
295 
296 	if (dev->overlay_out_enabled &&
297 	    dev->loop_vid_overlay.width && dev->loop_vid_overlay.height) {
298 		vosdbuf = dev->video_vbase;
299 		vosdbuf += (dev->loop_fb_copy.left * twopixsize) / 2 +
300 			   dev->loop_fb_copy.top * stride_osd;
301 		vid_overlay_int_part = dev->loop_vid_overlay.height /
302 				       dev->loop_vid_overlay_cap.height;
303 		vid_overlay_fract_part = dev->loop_vid_overlay.height %
304 					 dev->loop_vid_overlay_cap.height;
305 	}
306 
307 	vid_cap_right = tpg_hdiv(tpg, p, dev->loop_vid_cap.left + dev->loop_vid_cap.width);
308 	/* quick is true if no video scaling is needed */
309 	quick = dev->loop_vid_out.width == dev->loop_vid_cap.width;
310 
311 	dev->cur_scaled_line = dev->loop_vid_out.height;
312 	for (y = 0; y < hmax; y += vdiv, vcapbuf += stride_cap) {
313 		/* osdline is true if this line requires overlay blending */
314 		bool osdline = vosdbuf && y >= dev->loop_vid_overlay_cap.top &&
315 			  y < dev->loop_vid_overlay_cap.top + dev->loop_vid_overlay_cap.height;
316 
317 		/*
318 		 * If this line of the capture buffer doesn't get any video, then
319 		 * just fill with black.
320 		 */
321 		if (y < dev->loop_vid_cap.top ||
322 		    y >= dev->loop_vid_cap.top + dev->loop_vid_cap.height) {
323 			memcpy(vcapbuf, tpg->black_line[p], img_width);
324 			continue;
325 		}
326 
327 		/* fill the left border with black */
328 		if (dev->loop_vid_cap.left)
329 			memcpy(vcapbuf, tpg->black_line[p], vid_cap_left);
330 
331 		/* fill the right border with black */
332 		if (vid_cap_right < img_width)
333 			memcpy(vcapbuf + vid_cap_right, tpg->black_line[p],
334 				img_width - vid_cap_right);
335 
336 		if (quick && !osdline) {
337 			memcpy(vcapbuf + vid_cap_left,
338 			       voutbuf + vid_out_y * stride_out,
339 			       tpg_hdiv(tpg, p, dev->loop_vid_cap.width));
340 			goto update_vid_out_y;
341 		}
342 		if (dev->cur_scaled_line == vid_out_y) {
343 			memcpy(vcapbuf + vid_cap_left, dev->scaled_line,
344 			       tpg_hdiv(tpg, p, dev->loop_vid_cap.width));
345 			goto update_vid_out_y;
346 		}
347 		if (!osdline) {
348 			scale_line(voutbuf + vid_out_y * stride_out, dev->scaled_line,
349 				tpg_hdiv(tpg, p, dev->loop_vid_out.width),
350 				tpg_hdiv(tpg, p, dev->loop_vid_cap.width),
351 				tpg_g_twopixelsize(tpg, p));
352 		} else {
353 			/*
354 			 * Offset in bytes within loop_vid_copy to the start of the
355 			 * loop_vid_overlay rectangle.
356 			 */
357 			unsigned offset =
358 				((dev->loop_vid_overlay.left - dev->loop_vid_copy.left) *
359 				 twopixsize) / 2;
360 			u8 *osd = vosdbuf + vid_overlay_y * stride_osd;
361 
362 			scale_line(voutbuf + vid_out_y * stride_out, dev->blended_line,
363 				dev->loop_vid_out.width, dev->loop_vid_copy.width,
364 				tpg_g_twopixelsize(tpg, p));
365 			if (blend)
366 				blend_line(dev, vid_overlay_y + dev->loop_vid_overlay.top,
367 					   dev->loop_vid_overlay.left,
368 					   dev->blended_line + offset, osd,
369 					   dev->loop_vid_overlay.width, twopixsize / 2);
370 			else
371 				memcpy(dev->blended_line + offset,
372 				       osd, (dev->loop_vid_overlay.width * twopixsize) / 2);
373 			scale_line(dev->blended_line, dev->scaled_line,
374 					dev->loop_vid_copy.width, dev->loop_vid_cap.width,
375 					tpg_g_twopixelsize(tpg, p));
376 		}
377 		dev->cur_scaled_line = vid_out_y;
378 		memcpy(vcapbuf + vid_cap_left, dev->scaled_line,
379 		       tpg_hdiv(tpg, p, dev->loop_vid_cap.width));
380 
381 update_vid_out_y:
382 		if (osdline) {
383 			vid_overlay_y += vid_overlay_int_part;
384 			vid_overlay_error += vid_overlay_fract_part;
385 			if (vid_overlay_error >= dev->loop_vid_overlay_cap.height) {
386 				vid_overlay_error -= dev->loop_vid_overlay_cap.height;
387 				vid_overlay_y++;
388 			}
389 		}
390 		vid_out_y += vid_out_int_part;
391 		vid_out_error += vid_out_fract_part;
392 		if (vid_out_error >= dev->loop_vid_cap.height / vdiv) {
393 			vid_out_error -= dev->loop_vid_cap.height / vdiv;
394 			vid_out_y++;
395 		}
396 	}
397 
398 	if (!blank)
399 		return 0;
400 	for (; y < img_height; y += vdiv, vcapbuf += stride_cap)
401 		memcpy(vcapbuf, tpg->contrast_line[p], img_width);
402 	return 0;
403 }
404 
vivid_fillbuff(struct vivid_dev * dev,struct vivid_buffer * buf)405 static void vivid_fillbuff(struct vivid_dev *dev, struct vivid_buffer *buf)
406 {
407 	struct tpg_data *tpg = &dev->tpg;
408 	unsigned factor = V4L2_FIELD_HAS_T_OR_B(dev->field_cap) ? 2 : 1;
409 	unsigned line_height = 16 / factor;
410 	bool is_tv = vivid_is_sdtv_cap(dev);
411 	bool is_60hz = is_tv && (dev->std_cap & V4L2_STD_525_60);
412 	unsigned p;
413 	int line = 1;
414 	u8 *basep[TPG_MAX_PLANES][2];
415 	unsigned ms;
416 	char str[100];
417 	s32 gain;
418 	bool is_loop = false;
419 
420 	if (dev->loop_video && dev->can_loop_video &&
421 		((vivid_is_svid_cap(dev) &&
422 		!VIVID_INVALID_SIGNAL(dev->std_signal_mode)) ||
423 		(vivid_is_hdmi_cap(dev) &&
424 		!VIVID_INVALID_SIGNAL(dev->dv_timings_signal_mode))))
425 		is_loop = true;
426 
427 	buf->vb.sequence = dev->vid_cap_seq_count;
428 	/*
429 	 * Take the timestamp now if the timestamp source is set to
430 	 * "Start of Exposure".
431 	 */
432 	if (dev->tstamp_src_is_soe)
433 		buf->vb.vb2_buf.timestamp = ktime_get_ns();
434 	if (dev->field_cap == V4L2_FIELD_ALTERNATE) {
435 		/*
436 		 * 60 Hz standards start with the bottom field, 50 Hz standards
437 		 * with the top field. So if the 0-based seq_count is even,
438 		 * then the field is TOP for 50 Hz and BOTTOM for 60 Hz
439 		 * standards.
440 		 */
441 		buf->vb.field = ((dev->vid_cap_seq_count & 1) ^ is_60hz) ?
442 			V4L2_FIELD_BOTTOM : V4L2_FIELD_TOP;
443 		/*
444 		 * The sequence counter counts frames, not fields. So divide
445 		 * by two.
446 		 */
447 		buf->vb.sequence /= 2;
448 	} else {
449 		buf->vb.field = dev->field_cap;
450 	}
451 	tpg_s_field(tpg, buf->vb.field,
452 		    dev->field_cap == V4L2_FIELD_ALTERNATE);
453 	tpg_s_perc_fill_blank(tpg, dev->must_blank[buf->vb.vb2_buf.index]);
454 
455 	vivid_precalc_copy_rects(dev);
456 
457 	for (p = 0; p < tpg_g_planes(tpg); p++) {
458 		void *vbuf = plane_vaddr(tpg, buf, p,
459 					 tpg->bytesperline, tpg->buf_height);
460 
461 		/*
462 		 * The first plane of a multiplanar format has a non-zero
463 		 * data_offset. This helps testing whether the application
464 		 * correctly supports non-zero data offsets.
465 		 */
466 		if (p < tpg_g_buffers(tpg) && dev->fmt_cap->data_offset[p]) {
467 			memset(vbuf, dev->fmt_cap->data_offset[p] & 0xff,
468 			       dev->fmt_cap->data_offset[p]);
469 			vbuf += dev->fmt_cap->data_offset[p];
470 		}
471 		tpg_calc_text_basep(tpg, basep, p, vbuf);
472 		if (!is_loop || vivid_copy_buffer(dev, p, vbuf, buf))
473 			tpg_fill_plane_buffer(tpg, vivid_get_std_cap(dev),
474 					p, vbuf);
475 	}
476 	dev->must_blank[buf->vb.vb2_buf.index] = false;
477 
478 	/* Updates stream time, only update at the start of a new frame. */
479 	if (dev->field_cap != V4L2_FIELD_ALTERNATE ||
480 			(dev->vid_cap_seq_count & 1) == 0)
481 		dev->ms_vid_cap =
482 			jiffies_to_msecs(jiffies - dev->jiffies_vid_cap);
483 
484 	ms = dev->ms_vid_cap;
485 	if (dev->osd_mode <= 1) {
486 		snprintf(str, sizeof(str), " %02d:%02d:%02d:%03d %u%s",
487 				(ms / (60 * 60 * 1000)) % 24,
488 				(ms / (60 * 1000)) % 60,
489 				(ms / 1000) % 60,
490 				ms % 1000,
491 				buf->vb.sequence,
492 				(dev->field_cap == V4L2_FIELD_ALTERNATE) ?
493 					(buf->vb.field == V4L2_FIELD_TOP ?
494 					 " top" : " bottom") : "");
495 		tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
496 	}
497 	if (dev->osd_mode == 0) {
498 		snprintf(str, sizeof(str), " %dx%d, input %d ",
499 				dev->src_rect.width, dev->src_rect.height, dev->input);
500 		tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
501 
502 		gain = v4l2_ctrl_g_ctrl(dev->gain);
503 		mutex_lock(dev->ctrl_hdl_user_vid.lock);
504 		snprintf(str, sizeof(str),
505 			" brightness %3d, contrast %3d, saturation %3d, hue %d ",
506 			dev->brightness->cur.val,
507 			dev->contrast->cur.val,
508 			dev->saturation->cur.val,
509 			dev->hue->cur.val);
510 		tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
511 		snprintf(str, sizeof(str),
512 			" autogain %d, gain %3d, alpha 0x%02x ",
513 			dev->autogain->cur.val, gain, dev->alpha->cur.val);
514 		mutex_unlock(dev->ctrl_hdl_user_vid.lock);
515 		tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
516 		mutex_lock(dev->ctrl_hdl_user_aud.lock);
517 		snprintf(str, sizeof(str),
518 			" volume %3d, mute %d ",
519 			dev->volume->cur.val, dev->mute->cur.val);
520 		mutex_unlock(dev->ctrl_hdl_user_aud.lock);
521 		tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
522 		mutex_lock(dev->ctrl_hdl_user_gen.lock);
523 		snprintf(str, sizeof(str), " int32 %d, int64 %lld, bitmask %08x ",
524 			dev->int32->cur.val,
525 			*dev->int64->p_cur.p_s64,
526 			dev->bitmask->cur.val);
527 		tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
528 		snprintf(str, sizeof(str), " boolean %d, menu %s, string \"%s\" ",
529 			dev->boolean->cur.val,
530 			dev->menu->qmenu[dev->menu->cur.val],
531 			dev->string->p_cur.p_char);
532 		tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
533 		snprintf(str, sizeof(str), " integer_menu %lld, value %d ",
534 			dev->int_menu->qmenu_int[dev->int_menu->cur.val],
535 			dev->int_menu->cur.val);
536 		mutex_unlock(dev->ctrl_hdl_user_gen.lock);
537 		tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
538 		if (dev->button_pressed) {
539 			dev->button_pressed--;
540 			snprintf(str, sizeof(str), " button pressed!");
541 			tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
542 		}
543 		if (dev->osd[0]) {
544 			if (vivid_is_hdmi_cap(dev)) {
545 				snprintf(str, sizeof(str),
546 					 " OSD \"%s\"", dev->osd);
547 				tpg_gen_text(tpg, basep, line++ * line_height,
548 					     16, str);
549 			}
550 			if (dev->osd_jiffies &&
551 			    time_is_before_jiffies(dev->osd_jiffies + 5 * HZ)) {
552 				dev->osd[0] = 0;
553 				dev->osd_jiffies = 0;
554 			}
555 		}
556 	}
557 
558 	/*
559 	 * If "End of Frame" is specified at the timestamp source, then take
560 	 * the timestamp now.
561 	 */
562 	if (!dev->tstamp_src_is_soe)
563 		buf->vb.vb2_buf.timestamp = ktime_get_ns();
564 	buf->vb.vb2_buf.timestamp += dev->time_wrap_offset;
565 }
566 
567 /*
568  * Return true if this pixel coordinate is a valid video pixel.
569  */
valid_pix(struct vivid_dev * dev,int win_y,int win_x,int fb_y,int fb_x)570 static bool valid_pix(struct vivid_dev *dev, int win_y, int win_x, int fb_y, int fb_x)
571 {
572 	int i;
573 
574 	if (dev->bitmap_cap) {
575 		/*
576 		 * Only if the corresponding bit in the bitmap is set can
577 		 * the video pixel be shown. Coordinates are relative to
578 		 * the overlay window set by VIDIOC_S_FMT.
579 		 */
580 		const u8 *p = dev->bitmap_cap;
581 		unsigned stride = (dev->compose_cap.width + 7) / 8;
582 
583 		if (!(p[stride * win_y + win_x / 8] & (1 << (win_x & 7))))
584 			return false;
585 	}
586 
587 	for (i = 0; i < dev->clipcount_cap; i++) {
588 		/*
589 		 * Only if the framebuffer coordinate is not in any of the
590 		 * clip rectangles will be video pixel be shown.
591 		 */
592 		struct v4l2_rect *r = &dev->clips_cap[i].c;
593 
594 		if (fb_y >= r->top && fb_y < r->top + r->height &&
595 		    fb_x >= r->left && fb_x < r->left + r->width)
596 			return false;
597 	}
598 	return true;
599 }
600 
601 /*
602  * Draw the image into the overlay buffer.
603  * Note that the combination of overlay and multiplanar is not supported.
604  */
vivid_overlay(struct vivid_dev * dev,struct vivid_buffer * buf)605 static void vivid_overlay(struct vivid_dev *dev, struct vivid_buffer *buf)
606 {
607 	struct tpg_data *tpg = &dev->tpg;
608 	unsigned pixsize = tpg_g_twopixelsize(tpg, 0) / 2;
609 	void *vbase = dev->fb_vbase_cap;
610 	void *vbuf = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
611 	unsigned img_width = dev->compose_cap.width;
612 	unsigned img_height = dev->compose_cap.height;
613 	unsigned stride = tpg->bytesperline[0];
614 	/* if quick is true, then valid_pix() doesn't have to be called */
615 	bool quick = dev->bitmap_cap == NULL && dev->clipcount_cap == 0;
616 	int x, y, w, out_x = 0;
617 
618 	/*
619 	 * Overlay support is only supported for formats that have a twopixelsize
620 	 * that's >= 2. Warn and bail out if that's not the case.
621 	 */
622 	if (WARN_ON(pixsize == 0))
623 		return;
624 	if ((dev->overlay_cap_field == V4L2_FIELD_TOP ||
625 	     dev->overlay_cap_field == V4L2_FIELD_BOTTOM) &&
626 	    dev->overlay_cap_field != buf->vb.field)
627 		return;
628 
629 	vbuf += dev->compose_cap.left * pixsize + dev->compose_cap.top * stride;
630 	x = dev->overlay_cap_left;
631 	w = img_width;
632 	if (x < 0) {
633 		out_x = -x;
634 		w = w - out_x;
635 		x = 0;
636 	} else {
637 		w = dev->fb_cap.fmt.width - x;
638 		if (w > img_width)
639 			w = img_width;
640 	}
641 	if (w <= 0)
642 		return;
643 	if (dev->overlay_cap_top >= 0)
644 		vbase += dev->overlay_cap_top * dev->fb_cap.fmt.bytesperline;
645 	for (y = dev->overlay_cap_top;
646 	     y < dev->overlay_cap_top + (int)img_height;
647 	     y++, vbuf += stride) {
648 		int px;
649 
650 		if (y < 0 || y > dev->fb_cap.fmt.height)
651 			continue;
652 		if (quick) {
653 			memcpy(vbase + x * pixsize,
654 			       vbuf + out_x * pixsize, w * pixsize);
655 			vbase += dev->fb_cap.fmt.bytesperline;
656 			continue;
657 		}
658 		for (px = 0; px < w; px++) {
659 			if (!valid_pix(dev, y - dev->overlay_cap_top,
660 				       px + out_x, y, px + x))
661 				continue;
662 			memcpy(vbase + (px + x) * pixsize,
663 			       vbuf + (px + out_x) * pixsize,
664 			       pixsize);
665 		}
666 		vbase += dev->fb_cap.fmt.bytesperline;
667 	}
668 }
669 
vivid_thread_vid_cap_tick(struct vivid_dev * dev,int dropped_bufs)670 static void vivid_thread_vid_cap_tick(struct vivid_dev *dev, int dropped_bufs)
671 {
672 	struct vivid_buffer *vid_cap_buf = NULL;
673 	struct vivid_buffer *vbi_cap_buf = NULL;
674 
675 	dprintk(dev, 1, "Video Capture Thread Tick\n");
676 
677 	while (dropped_bufs-- > 1)
678 		tpg_update_mv_count(&dev->tpg,
679 				dev->field_cap == V4L2_FIELD_NONE ||
680 				dev->field_cap == V4L2_FIELD_ALTERNATE);
681 
682 	/* Drop a certain percentage of buffers. */
683 	if (dev->perc_dropped_buffers &&
684 	    prandom_u32_max(100) < dev->perc_dropped_buffers)
685 		goto update_mv;
686 
687 	spin_lock(&dev->slock);
688 	if (!list_empty(&dev->vid_cap_active)) {
689 		vid_cap_buf = list_entry(dev->vid_cap_active.next, struct vivid_buffer, list);
690 		list_del(&vid_cap_buf->list);
691 	}
692 	if (!list_empty(&dev->vbi_cap_active)) {
693 		if (dev->field_cap != V4L2_FIELD_ALTERNATE ||
694 		    (dev->vbi_cap_seq_count & 1)) {
695 			vbi_cap_buf = list_entry(dev->vbi_cap_active.next,
696 						 struct vivid_buffer, list);
697 			list_del(&vbi_cap_buf->list);
698 		}
699 	}
700 	spin_unlock(&dev->slock);
701 
702 	if (!vid_cap_buf && !vbi_cap_buf)
703 		goto update_mv;
704 
705 	if (vid_cap_buf) {
706 		/* Fill buffer */
707 		vivid_fillbuff(dev, vid_cap_buf);
708 		dprintk(dev, 1, "filled buffer %d\n",
709 			vid_cap_buf->vb.vb2_buf.index);
710 
711 		/* Handle overlay */
712 		if (dev->overlay_cap_owner && dev->fb_cap.base &&
713 			dev->fb_cap.fmt.pixelformat == dev->fmt_cap->fourcc)
714 			vivid_overlay(dev, vid_cap_buf);
715 
716 		vb2_buffer_done(&vid_cap_buf->vb.vb2_buf, dev->dqbuf_error ?
717 				VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
718 		dprintk(dev, 2, "vid_cap buffer %d done\n",
719 				vid_cap_buf->vb.vb2_buf.index);
720 	}
721 
722 	if (vbi_cap_buf) {
723 		if (dev->stream_sliced_vbi_cap)
724 			vivid_sliced_vbi_cap_process(dev, vbi_cap_buf);
725 		else
726 			vivid_raw_vbi_cap_process(dev, vbi_cap_buf);
727 		vb2_buffer_done(&vbi_cap_buf->vb.vb2_buf, dev->dqbuf_error ?
728 				VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
729 		dprintk(dev, 2, "vbi_cap %d done\n",
730 				vbi_cap_buf->vb.vb2_buf.index);
731 	}
732 	dev->dqbuf_error = false;
733 
734 update_mv:
735 	/* Update the test pattern movement counters */
736 	tpg_update_mv_count(&dev->tpg, dev->field_cap == V4L2_FIELD_NONE ||
737 				       dev->field_cap == V4L2_FIELD_ALTERNATE);
738 }
739 
vivid_thread_vid_cap(void * data)740 static int vivid_thread_vid_cap(void *data)
741 {
742 	struct vivid_dev *dev = data;
743 	u64 numerators_since_start;
744 	u64 buffers_since_start;
745 	u64 next_jiffies_since_start;
746 	unsigned long jiffies_since_start;
747 	unsigned long cur_jiffies;
748 	unsigned wait_jiffies;
749 	unsigned numerator;
750 	unsigned denominator;
751 	int dropped_bufs;
752 
753 	dprintk(dev, 1, "Video Capture Thread Start\n");
754 
755 	set_freezable();
756 
757 	/* Resets frame counters */
758 	dev->cap_seq_offset = 0;
759 	dev->cap_seq_count = 0;
760 	dev->cap_seq_resync = false;
761 	dev->jiffies_vid_cap = jiffies;
762 
763 	for (;;) {
764 		try_to_freeze();
765 		if (kthread_should_stop())
766 			break;
767 
768 		mutex_lock(&dev->mutex);
769 		cur_jiffies = jiffies;
770 		if (dev->cap_seq_resync) {
771 			dev->jiffies_vid_cap = cur_jiffies;
772 			dev->cap_seq_offset = dev->cap_seq_count + 1;
773 			dev->cap_seq_count = 0;
774 			dev->cap_seq_resync = false;
775 		}
776 		numerator = dev->timeperframe_vid_cap.numerator;
777 		denominator = dev->timeperframe_vid_cap.denominator;
778 
779 		if (dev->field_cap == V4L2_FIELD_ALTERNATE)
780 			denominator *= 2;
781 
782 		/* Calculate the number of jiffies since we started streaming */
783 		jiffies_since_start = cur_jiffies - dev->jiffies_vid_cap;
784 		/* Get the number of buffers streamed since the start */
785 		buffers_since_start = (u64)jiffies_since_start * denominator +
786 				      (HZ * numerator) / 2;
787 		do_div(buffers_since_start, HZ * numerator);
788 
789 		/*
790 		 * After more than 0xf0000000 (rounded down to a multiple of
791 		 * 'jiffies-per-day' to ease jiffies_to_msecs calculation)
792 		 * jiffies have passed since we started streaming reset the
793 		 * counters and keep track of the sequence offset.
794 		 */
795 		if (jiffies_since_start > JIFFIES_RESYNC) {
796 			dev->jiffies_vid_cap = cur_jiffies;
797 			dev->cap_seq_offset = buffers_since_start;
798 			buffers_since_start = 0;
799 		}
800 		dropped_bufs = buffers_since_start + dev->cap_seq_offset - dev->cap_seq_count;
801 		dev->cap_seq_count = buffers_since_start + dev->cap_seq_offset;
802 		dev->vid_cap_seq_count = dev->cap_seq_count - dev->vid_cap_seq_start;
803 		dev->vbi_cap_seq_count = dev->cap_seq_count - dev->vbi_cap_seq_start;
804 
805 		vivid_thread_vid_cap_tick(dev, dropped_bufs);
806 
807 		/*
808 		 * Calculate the number of 'numerators' streamed since we started,
809 		 * including the current buffer.
810 		 */
811 		numerators_since_start = ++buffers_since_start * numerator;
812 
813 		/* And the number of jiffies since we started */
814 		jiffies_since_start = jiffies - dev->jiffies_vid_cap;
815 
816 		mutex_unlock(&dev->mutex);
817 
818 		/*
819 		 * Calculate when that next buffer is supposed to start
820 		 * in jiffies since we started streaming.
821 		 */
822 		next_jiffies_since_start = numerators_since_start * HZ +
823 					   denominator / 2;
824 		do_div(next_jiffies_since_start, denominator);
825 		/* If it is in the past, then just schedule asap */
826 		if (next_jiffies_since_start < jiffies_since_start)
827 			next_jiffies_since_start = jiffies_since_start;
828 
829 		wait_jiffies = next_jiffies_since_start - jiffies_since_start;
830 		schedule_timeout_interruptible(wait_jiffies ? wait_jiffies : 1);
831 	}
832 	dprintk(dev, 1, "Video Capture Thread End\n");
833 	return 0;
834 }
835 
vivid_grab_controls(struct vivid_dev * dev,bool grab)836 static void vivid_grab_controls(struct vivid_dev *dev, bool grab)
837 {
838 	v4l2_ctrl_grab(dev->ctrl_has_crop_cap, grab);
839 	v4l2_ctrl_grab(dev->ctrl_has_compose_cap, grab);
840 	v4l2_ctrl_grab(dev->ctrl_has_scaler_cap, grab);
841 }
842 
vivid_start_generating_vid_cap(struct vivid_dev * dev,bool * pstreaming)843 int vivid_start_generating_vid_cap(struct vivid_dev *dev, bool *pstreaming)
844 {
845 	dprintk(dev, 1, "%s\n", __func__);
846 
847 	if (dev->kthread_vid_cap) {
848 		u32 seq_count = dev->cap_seq_count + dev->seq_wrap * 128;
849 
850 		if (pstreaming == &dev->vid_cap_streaming)
851 			dev->vid_cap_seq_start = seq_count;
852 		else
853 			dev->vbi_cap_seq_start = seq_count;
854 		*pstreaming = true;
855 		return 0;
856 	}
857 
858 	/* Resets frame counters */
859 	tpg_init_mv_count(&dev->tpg);
860 
861 	dev->vid_cap_seq_start = dev->seq_wrap * 128;
862 	dev->vbi_cap_seq_start = dev->seq_wrap * 128;
863 
864 	dev->kthread_vid_cap = kthread_run(vivid_thread_vid_cap, dev,
865 			"%s-vid-cap", dev->v4l2_dev.name);
866 
867 	if (IS_ERR(dev->kthread_vid_cap)) {
868 		v4l2_err(&dev->v4l2_dev, "kernel_thread() failed\n");
869 		return PTR_ERR(dev->kthread_vid_cap);
870 	}
871 	*pstreaming = true;
872 	vivid_grab_controls(dev, true);
873 
874 	dprintk(dev, 1, "returning from %s\n", __func__);
875 	return 0;
876 }
877 
vivid_stop_generating_vid_cap(struct vivid_dev * dev,bool * pstreaming)878 void vivid_stop_generating_vid_cap(struct vivid_dev *dev, bool *pstreaming)
879 {
880 	dprintk(dev, 1, "%s\n", __func__);
881 
882 	if (dev->kthread_vid_cap == NULL)
883 		return;
884 
885 	*pstreaming = false;
886 	if (pstreaming == &dev->vid_cap_streaming) {
887 		/* Release all active buffers */
888 		while (!list_empty(&dev->vid_cap_active)) {
889 			struct vivid_buffer *buf;
890 
891 			buf = list_entry(dev->vid_cap_active.next,
892 					 struct vivid_buffer, list);
893 			list_del(&buf->list);
894 			vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
895 			dprintk(dev, 2, "vid_cap buffer %d done\n",
896 				buf->vb.vb2_buf.index);
897 		}
898 	}
899 
900 	if (pstreaming == &dev->vbi_cap_streaming) {
901 		while (!list_empty(&dev->vbi_cap_active)) {
902 			struct vivid_buffer *buf;
903 
904 			buf = list_entry(dev->vbi_cap_active.next,
905 					 struct vivid_buffer, list);
906 			list_del(&buf->list);
907 			vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
908 			dprintk(dev, 2, "vbi_cap buffer %d done\n",
909 				buf->vb.vb2_buf.index);
910 		}
911 	}
912 
913 	if (dev->vid_cap_streaming || dev->vbi_cap_streaming)
914 		return;
915 
916 	/* shutdown control thread */
917 	vivid_grab_controls(dev, false);
918 	mutex_unlock(&dev->mutex);
919 	kthread_stop(dev->kthread_vid_cap);
920 	dev->kthread_vid_cap = NULL;
921 	mutex_lock(&dev->mutex);
922 }
923