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