1 // SPDX-License-Identifier: GPL-2.0-only
3 * vivid-kthread-cap.h - video/vbi capture thread support functions.
5 * Copyright 2014 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
8 #include <linux/module.h>
9 #include <linux/errno.h>
10 #include <linux/kernel.h>
11 #include <linux/init.h>
12 #include <linux/sched.h>
13 #include <linux/slab.h>
14 #include <linux/font.h>
15 #include <linux/mutex.h>
16 #include <linux/videodev2.h>
17 #include <linux/kthread.h>
18 #include <linux/freezer.h>
19 #include <linux/random.h>
20 #include <linux/v4l2-dv-timings.h>
21 #include <asm/div64.h>
22 #include <media/videobuf2-vmalloc.h>
23 #include <media/v4l2-dv-timings.h>
24 #include <media/v4l2-ioctl.h>
25 #include <media/v4l2-fh.h>
26 #include <media/v4l2-event.h>
27 #include <media/v4l2-rect.h>
29 #include "vivid-core.h"
30 #include "vivid-vid-common.h"
31 #include "vivid-vid-cap.h"
32 #include "vivid-vid-out.h"
33 #include "vivid-radio-common.h"
34 #include "vivid-radio-rx.h"
35 #include "vivid-radio-tx.h"
36 #include "vivid-sdr-cap.h"
37 #include "vivid-vbi-cap.h"
38 #include "vivid-vbi-out.h"
39 #include "vivid-osd.h"
40 #include "vivid-ctrls.h"
41 #include "vivid-kthread-cap.h"
43 static inline v4l2_std_id
vivid_get_std_cap(const struct vivid_dev
*dev
)
45 if (vivid_is_sdtv_cap(dev
))
46 return dev
->std_cap
[dev
->input
];
50 static void copy_pix(struct vivid_dev
*dev
, int win_y
, int win_x
,
51 u16
*cap
, const u16
*osd
)
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
;
62 if (dev
->bitmap_out
) {
63 const u8
*p
= dev
->bitmap_out
;
64 unsigned stride
= (dev
->compose_out
.width
+ 7) / 8;
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))))
72 for (i
= 0; i
< dev
->clipcount_out
; i
++) {
73 struct v4l2_rect
*r
= &dev
->clips_out
[i
].c
;
75 if (fb_y
>= r
->top
&& fb_y
< r
->top
+ r
->height
&&
76 fb_x
>= r
->left
&& fb_x
< r
->left
+ r
->width
)
79 if ((dev
->fbuf_out_flags
& V4L2_FBUF_FLAG_CHROMAKEY
) &&
80 *osd
!= dev
->chromakey_out
)
82 if ((dev
->fbuf_out_flags
& V4L2_FBUF_FLAG_SRC_CHROMAKEY
) &&
83 out
== dev
->chromakey_out
)
85 if (dev
->fmt_cap
->alpha_mask
) {
86 if ((dev
->fbuf_out_flags
& V4L2_FBUF_FLAG_GLOBAL_ALPHA
) &&
87 dev
->global_alpha_out
)
89 if ((dev
->fbuf_out_flags
& V4L2_FBUF_FLAG_LOCAL_ALPHA
) &&
90 *cap
& dev
->fmt_cap
->alpha_mask
)
92 if ((dev
->fbuf_out_flags
& V4L2_FBUF_FLAG_LOCAL_INV_ALPHA
) &&
93 !(*cap
& dev
->fmt_cap
->alpha_mask
))
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
)
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
);
111 static void scale_line(const u8
*src
, u8
*dst
, unsigned srcw
, unsigned dstw
, unsigned twopixsize
)
113 /* Coarse scaling with Bresenham */
121 * We always combine two pixels to prevent color bleed in the packed
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
);
140 * Precalculate the rectangles needed to perform video looping:
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.
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.
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.
164 static void vivid_precalc_copy_rects(struct vivid_dev
*dev
)
166 /* Framebuffer rectangle */
167 struct v4l2_rect r_fb
= {
168 0, 0, dev
->display_width
, dev
->display_height
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
176 v4l2_rect_intersect(&dev
->loop_vid_copy
, &dev
->crop_cap
, &dev
->compose_out
);
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
;
183 dev
->loop_vid_cap
= dev
->loop_vid_copy
;
184 v4l2_rect_scale(&dev
->loop_vid_cap
, &dev
->crop_cap
, &dev
->compose_cap
);
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
);
195 v4l2_rect_intersect(&r_overlay
, &r_fb
, &r_overlay
);
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
;
201 v4l2_rect_intersect(&dev
->loop_vid_overlay
, &r_overlay
, &dev
->loop_vid_copy
);
202 dev
->loop_fb_copy
= dev
->loop_vid_overlay
;
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
;
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
);
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
);
221 static void *plane_vaddr(struct tpg_data
*tpg
, struct vivid_buffer
*buf
,
222 unsigned p
, unsigned bpl
[TPG_MAX_PLANES
], unsigned h
)
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
];
235 static noinline_for_stack
int vivid_copy_buffer(struct vivid_dev
*dev
, unsigned p
,
236 u8
*vcapbuf
, struct vivid_buffer
*vid_cap_buf
)
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;
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
;
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
;
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
)
275 vid_cap_buf
->vb
.field
= vid_out_buf
->vb
.field
;
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
;
286 if (dev
->loop_vid_copy
.width
== 0 || dev
->loop_vid_copy
.height
== 0) {
288 * If there is nothing to copy, then just fill the capture window
291 for (y
= 0; y
< hmax
/ vdiv
; y
++, vcapbuf
+= stride_cap
)
292 memcpy(vcapbuf
, tpg
->black_line
[p
], img_width
);
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
;
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
;
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
;
318 * If this line of the capture buffer doesn't get any video, then
319 * just fill with black.
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
);
327 /* fill the left border with black */
328 if (dev
->loop_vid_cap
.left
)
329 memcpy(vcapbuf
, tpg
->black_line
[p
], vid_cap_left
);
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
);
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
;
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
;
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
));
354 * Offset in bytes within loop_vid_copy to the start of the
355 * loop_vid_overlay rectangle.
358 ((dev
->loop_vid_overlay
.left
- dev
->loop_vid_copy
.left
) *
360 u8
*osd
= vosdbuf
+ vid_overlay_y
* stride_osd
;
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
));
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);
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
));
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
));
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
;
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
;
400 for (; y
< img_height
; y
+= vdiv
, vcapbuf
+= stride_cap
)
401 memcpy(vcapbuf
, tpg
->contrast_line
[p
], img_width
);
405 static void vivid_fillbuff(struct vivid_dev
*dev
, struct vivid_buffer
*buf
)
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
[dev
->input
] & V4L2_STD_525_60
);
414 u8
*basep
[TPG_MAX_PLANES
][2];
418 bool is_loop
= false;
420 if (dev
->loop_video
&& dev
->can_loop_video
&&
421 ((vivid_is_svid_cap(dev
) &&
422 !VIVID_INVALID_SIGNAL(dev
->std_signal_mode
[dev
->input
])) ||
423 (vivid_is_hdmi_cap(dev
) &&
424 !VIVID_INVALID_SIGNAL(dev
->dv_timings_signal_mode
[dev
->input
]))))
427 buf
->vb
.sequence
= dev
->vid_cap_seq_count
;
428 if (dev
->field_cap
== V4L2_FIELD_ALTERNATE
) {
430 * 60 Hz standards start with the bottom field, 50 Hz standards
431 * with the top field. So if the 0-based seq_count is even,
432 * then the field is TOP for 50 Hz and BOTTOM for 60 Hz
435 buf
->vb
.field
= ((dev
->vid_cap_seq_count
& 1) ^ is_60hz
) ?
436 V4L2_FIELD_BOTTOM
: V4L2_FIELD_TOP
;
438 * The sequence counter counts frames, not fields. So divide
441 buf
->vb
.sequence
/= 2;
443 buf
->vb
.field
= dev
->field_cap
;
445 tpg_s_field(tpg
, buf
->vb
.field
,
446 dev
->field_cap
== V4L2_FIELD_ALTERNATE
);
447 tpg_s_perc_fill_blank(tpg
, dev
->must_blank
[buf
->vb
.vb2_buf
.index
]);
449 vivid_precalc_copy_rects(dev
);
451 for (p
= 0; p
< tpg_g_planes(tpg
); p
++) {
452 void *vbuf
= plane_vaddr(tpg
, buf
, p
,
453 tpg
->bytesperline
, tpg
->buf_height
);
456 * The first plane of a multiplanar format has a non-zero
457 * data_offset. This helps testing whether the application
458 * correctly supports non-zero data offsets.
460 if (p
< tpg_g_buffers(tpg
) && dev
->fmt_cap
->data_offset
[p
]) {
461 memset(vbuf
, dev
->fmt_cap
->data_offset
[p
] & 0xff,
462 dev
->fmt_cap
->data_offset
[p
]);
463 vbuf
+= dev
->fmt_cap
->data_offset
[p
];
465 tpg_calc_text_basep(tpg
, basep
, p
, vbuf
);
466 if (!is_loop
|| vivid_copy_buffer(dev
, p
, vbuf
, buf
))
467 tpg_fill_plane_buffer(tpg
, vivid_get_std_cap(dev
),
470 dev
->must_blank
[buf
->vb
.vb2_buf
.index
] = false;
472 /* Updates stream time, only update at the start of a new frame. */
473 if (dev
->field_cap
!= V4L2_FIELD_ALTERNATE
||
474 (dev
->vid_cap_seq_count
& 1) == 0)
476 jiffies_to_msecs(jiffies
- dev
->jiffies_vid_cap
);
478 ms
= dev
->ms_vid_cap
;
479 if (dev
->osd_mode
<= 1) {
480 snprintf(str
, sizeof(str
), " %02d:%02d:%02d:%03d %u%s",
481 (ms
/ (60 * 60 * 1000)) % 24,
482 (ms
/ (60 * 1000)) % 60,
486 (dev
->field_cap
== V4L2_FIELD_ALTERNATE
) ?
487 (buf
->vb
.field
== V4L2_FIELD_TOP
?
488 " top" : " bottom") : "");
489 tpg_gen_text(tpg
, basep
, line
++ * line_height
, 16, str
);
491 if (dev
->osd_mode
== 0) {
492 snprintf(str
, sizeof(str
), " %dx%d, input %d ",
493 dev
->src_rect
.width
, dev
->src_rect
.height
, dev
->input
);
494 tpg_gen_text(tpg
, basep
, line
++ * line_height
, 16, str
);
496 gain
= v4l2_ctrl_g_ctrl(dev
->gain
);
497 mutex_lock(dev
->ctrl_hdl_user_vid
.lock
);
498 snprintf(str
, sizeof(str
),
499 " brightness %3d, contrast %3d, saturation %3d, hue %d ",
500 dev
->brightness
->cur
.val
,
501 dev
->contrast
->cur
.val
,
502 dev
->saturation
->cur
.val
,
504 tpg_gen_text(tpg
, basep
, line
++ * line_height
, 16, str
);
505 snprintf(str
, sizeof(str
),
506 " autogain %d, gain %3d, alpha 0x%02x ",
507 dev
->autogain
->cur
.val
, gain
, dev
->alpha
->cur
.val
);
508 mutex_unlock(dev
->ctrl_hdl_user_vid
.lock
);
509 tpg_gen_text(tpg
, basep
, line
++ * line_height
, 16, str
);
510 mutex_lock(dev
->ctrl_hdl_user_aud
.lock
);
511 snprintf(str
, sizeof(str
),
512 " volume %3d, mute %d ",
513 dev
->volume
->cur
.val
, dev
->mute
->cur
.val
);
514 mutex_unlock(dev
->ctrl_hdl_user_aud
.lock
);
515 tpg_gen_text(tpg
, basep
, line
++ * line_height
, 16, str
);
516 mutex_lock(dev
->ctrl_hdl_user_gen
.lock
);
517 snprintf(str
, sizeof(str
), " int32 %d, int64 %lld, bitmask %08x ",
519 *dev
->int64
->p_cur
.p_s64
,
520 dev
->bitmask
->cur
.val
);
521 tpg_gen_text(tpg
, basep
, line
++ * line_height
, 16, str
);
522 snprintf(str
, sizeof(str
), " boolean %d, menu %s, string \"%s\" ",
523 dev
->boolean
->cur
.val
,
524 dev
->menu
->qmenu
[dev
->menu
->cur
.val
],
525 dev
->string
->p_cur
.p_char
);
526 tpg_gen_text(tpg
, basep
, line
++ * line_height
, 16, str
);
527 snprintf(str
, sizeof(str
), " integer_menu %lld, value %d ",
528 dev
->int_menu
->qmenu_int
[dev
->int_menu
->cur
.val
],
529 dev
->int_menu
->cur
.val
);
530 mutex_unlock(dev
->ctrl_hdl_user_gen
.lock
);
531 tpg_gen_text(tpg
, basep
, line
++ * line_height
, 16, str
);
532 if (dev
->button_pressed
) {
533 dev
->button_pressed
--;
534 snprintf(str
, sizeof(str
), " button pressed!");
535 tpg_gen_text(tpg
, basep
, line
++ * line_height
, 16, str
);
538 if (vivid_is_hdmi_cap(dev
)) {
539 snprintf(str
, sizeof(str
),
540 " OSD \"%s\"", dev
->osd
);
541 tpg_gen_text(tpg
, basep
, line
++ * line_height
,
544 if (dev
->osd_jiffies
&&
545 time_is_before_jiffies(dev
->osd_jiffies
+ 5 * HZ
)) {
547 dev
->osd_jiffies
= 0;
554 * Return true if this pixel coordinate is a valid video pixel.
556 static bool valid_pix(struct vivid_dev
*dev
, int win_y
, int win_x
, int fb_y
, int fb_x
)
560 if (dev
->bitmap_cap
) {
562 * Only if the corresponding bit in the bitmap is set can
563 * the video pixel be shown. Coordinates are relative to
564 * the overlay window set by VIDIOC_S_FMT.
566 const u8
*p
= dev
->bitmap_cap
;
567 unsigned stride
= (dev
->compose_cap
.width
+ 7) / 8;
569 if (!(p
[stride
* win_y
+ win_x
/ 8] & (1 << (win_x
& 7))))
573 for (i
= 0; i
< dev
->clipcount_cap
; i
++) {
575 * Only if the framebuffer coordinate is not in any of the
576 * clip rectangles will be video pixel be shown.
578 struct v4l2_rect
*r
= &dev
->clips_cap
[i
].c
;
580 if (fb_y
>= r
->top
&& fb_y
< r
->top
+ r
->height
&&
581 fb_x
>= r
->left
&& fb_x
< r
->left
+ r
->width
)
588 * Draw the image into the overlay buffer.
589 * Note that the combination of overlay and multiplanar is not supported.
591 static void vivid_overlay(struct vivid_dev
*dev
, struct vivid_buffer
*buf
)
593 struct tpg_data
*tpg
= &dev
->tpg
;
594 unsigned pixsize
= tpg_g_twopixelsize(tpg
, 0) / 2;
595 void *vbase
= dev
->fb_vbase_cap
;
596 void *vbuf
= vb2_plane_vaddr(&buf
->vb
.vb2_buf
, 0);
597 unsigned img_width
= dev
->compose_cap
.width
;
598 unsigned img_height
= dev
->compose_cap
.height
;
599 unsigned stride
= tpg
->bytesperline
[0];
600 /* if quick is true, then valid_pix() doesn't have to be called */
601 bool quick
= dev
->bitmap_cap
== NULL
&& dev
->clipcount_cap
== 0;
602 int x
, y
, w
, out_x
= 0;
605 * Overlay support is only supported for formats that have a twopixelsize
606 * that's >= 2. Warn and bail out if that's not the case.
608 if (WARN_ON(pixsize
== 0))
610 if ((dev
->overlay_cap_field
== V4L2_FIELD_TOP
||
611 dev
->overlay_cap_field
== V4L2_FIELD_BOTTOM
) &&
612 dev
->overlay_cap_field
!= buf
->vb
.field
)
615 vbuf
+= dev
->compose_cap
.left
* pixsize
+ dev
->compose_cap
.top
* stride
;
616 x
= dev
->overlay_cap_left
;
623 w
= dev
->fb_cap
.fmt
.width
- x
;
629 if (dev
->overlay_cap_top
>= 0)
630 vbase
+= dev
->overlay_cap_top
* dev
->fb_cap
.fmt
.bytesperline
;
631 for (y
= dev
->overlay_cap_top
;
632 y
< dev
->overlay_cap_top
+ (int)img_height
;
633 y
++, vbuf
+= stride
) {
636 if (y
< 0 || y
> dev
->fb_cap
.fmt
.height
)
639 memcpy(vbase
+ x
* pixsize
,
640 vbuf
+ out_x
* pixsize
, w
* pixsize
);
641 vbase
+= dev
->fb_cap
.fmt
.bytesperline
;
644 for (px
= 0; px
< w
; px
++) {
645 if (!valid_pix(dev
, y
- dev
->overlay_cap_top
,
646 px
+ out_x
, y
, px
+ x
))
648 memcpy(vbase
+ (px
+ x
) * pixsize
,
649 vbuf
+ (px
+ out_x
) * pixsize
,
652 vbase
+= dev
->fb_cap
.fmt
.bytesperline
;
656 static void vivid_cap_update_frame_period(struct vivid_dev
*dev
)
660 f_period
= (u64
)dev
->timeperframe_vid_cap
.numerator
* 1000000000;
661 if (WARN_ON(dev
->timeperframe_vid_cap
.denominator
== 0))
662 dev
->timeperframe_vid_cap
.denominator
= 1;
663 do_div(f_period
, dev
->timeperframe_vid_cap
.denominator
);
664 if (dev
->field_cap
== V4L2_FIELD_ALTERNATE
)
667 * If "End of Frame", then offset the exposure time by 0.9
668 * of the frame period.
670 dev
->cap_frame_eof_offset
= f_period
* 9;
671 do_div(dev
->cap_frame_eof_offset
, 10);
672 dev
->cap_frame_period
= f_period
;
675 static noinline_for_stack
void vivid_thread_vid_cap_tick(struct vivid_dev
*dev
,
678 struct vivid_buffer
*vid_cap_buf
= NULL
;
679 struct vivid_buffer
*vbi_cap_buf
= NULL
;
682 dprintk(dev
, 1, "Video Capture Thread Tick\n");
684 while (dropped_bufs
-- > 1)
685 tpg_update_mv_count(&dev
->tpg
,
686 dev
->field_cap
== V4L2_FIELD_NONE
||
687 dev
->field_cap
== V4L2_FIELD_ALTERNATE
);
689 /* Drop a certain percentage of buffers. */
690 if (dev
->perc_dropped_buffers
&&
691 prandom_u32_max(100) < dev
->perc_dropped_buffers
)
694 spin_lock(&dev
->slock
);
695 if (!list_empty(&dev
->vid_cap_active
)) {
696 vid_cap_buf
= list_entry(dev
->vid_cap_active
.next
, struct vivid_buffer
, list
);
697 list_del(&vid_cap_buf
->list
);
699 if (!list_empty(&dev
->vbi_cap_active
)) {
700 if (dev
->field_cap
!= V4L2_FIELD_ALTERNATE
||
701 (dev
->vbi_cap_seq_count
& 1)) {
702 vbi_cap_buf
= list_entry(dev
->vbi_cap_active
.next
,
703 struct vivid_buffer
, list
);
704 list_del(&vbi_cap_buf
->list
);
707 spin_unlock(&dev
->slock
);
709 if (!vid_cap_buf
&& !vbi_cap_buf
)
712 f_time
= dev
->cap_frame_period
* dev
->vid_cap_seq_count
+
713 dev
->cap_stream_start
+ dev
->time_wrap_offset
;
714 if (!dev
->tstamp_src_is_soe
)
715 f_time
+= dev
->cap_frame_eof_offset
;
718 v4l2_ctrl_request_setup(vid_cap_buf
->vb
.vb2_buf
.req_obj
.req
,
719 &dev
->ctrl_hdl_vid_cap
);
721 vivid_fillbuff(dev
, vid_cap_buf
);
722 dprintk(dev
, 1, "filled buffer %d\n",
723 vid_cap_buf
->vb
.vb2_buf
.index
);
726 if (dev
->overlay_cap_owner
&& dev
->fb_cap
.base
&&
727 dev
->fb_cap
.fmt
.pixelformat
== dev
->fmt_cap
->fourcc
)
728 vivid_overlay(dev
, vid_cap_buf
);
730 v4l2_ctrl_request_complete(vid_cap_buf
->vb
.vb2_buf
.req_obj
.req
,
731 &dev
->ctrl_hdl_vid_cap
);
732 vb2_buffer_done(&vid_cap_buf
->vb
.vb2_buf
, dev
->dqbuf_error
?
733 VB2_BUF_STATE_ERROR
: VB2_BUF_STATE_DONE
);
734 dprintk(dev
, 2, "vid_cap buffer %d done\n",
735 vid_cap_buf
->vb
.vb2_buf
.index
);
737 vid_cap_buf
->vb
.vb2_buf
.timestamp
= f_time
;
743 v4l2_ctrl_request_setup(vbi_cap_buf
->vb
.vb2_buf
.req_obj
.req
,
744 &dev
->ctrl_hdl_vbi_cap
);
745 if (dev
->stream_sliced_vbi_cap
)
746 vivid_sliced_vbi_cap_process(dev
, vbi_cap_buf
);
748 vivid_raw_vbi_cap_process(dev
, vbi_cap_buf
);
749 v4l2_ctrl_request_complete(vbi_cap_buf
->vb
.vb2_buf
.req_obj
.req
,
750 &dev
->ctrl_hdl_vbi_cap
);
751 vb2_buffer_done(&vbi_cap_buf
->vb
.vb2_buf
, dev
->dqbuf_error
?
752 VB2_BUF_STATE_ERROR
: VB2_BUF_STATE_DONE
);
753 dprintk(dev
, 2, "vbi_cap %d done\n",
754 vbi_cap_buf
->vb
.vb2_buf
.index
);
756 /* If capturing a VBI, offset by 0.05 */
757 vbi_period
= dev
->cap_frame_period
* 5;
758 do_div(vbi_period
, 100);
759 vbi_cap_buf
->vb
.vb2_buf
.timestamp
= f_time
+ vbi_period
;
761 dev
->dqbuf_error
= false;
764 /* Update the test pattern movement counters */
765 tpg_update_mv_count(&dev
->tpg
, dev
->field_cap
== V4L2_FIELD_NONE
||
766 dev
->field_cap
== V4L2_FIELD_ALTERNATE
);
769 static int vivid_thread_vid_cap(void *data
)
771 struct vivid_dev
*dev
= data
;
772 u64 numerators_since_start
;
773 u64 buffers_since_start
;
774 u64 next_jiffies_since_start
;
775 unsigned long jiffies_since_start
;
776 unsigned long cur_jiffies
;
777 unsigned wait_jiffies
;
779 unsigned denominator
;
782 dprintk(dev
, 1, "Video Capture Thread Start\n");
786 /* Resets frame counters */
787 dev
->cap_seq_offset
= 0;
788 dev
->cap_seq_count
= 0;
789 dev
->cap_seq_resync
= false;
790 dev
->jiffies_vid_cap
= jiffies
;
791 dev
->cap_stream_start
= ktime_get_ns();
792 vivid_cap_update_frame_period(dev
);
796 if (kthread_should_stop())
799 if (!mutex_trylock(&dev
->mutex
)) {
800 schedule_timeout_uninterruptible(1);
804 cur_jiffies
= jiffies
;
805 if (dev
->cap_seq_resync
) {
806 dev
->jiffies_vid_cap
= cur_jiffies
;
807 dev
->cap_seq_offset
= dev
->cap_seq_count
+ 1;
808 dev
->cap_seq_count
= 0;
809 dev
->cap_stream_start
+= dev
->cap_frame_period
*
811 vivid_cap_update_frame_period(dev
);
812 dev
->cap_seq_resync
= false;
814 numerator
= dev
->timeperframe_vid_cap
.numerator
;
815 denominator
= dev
->timeperframe_vid_cap
.denominator
;
817 if (dev
->field_cap
== V4L2_FIELD_ALTERNATE
)
820 /* Calculate the number of jiffies since we started streaming */
821 jiffies_since_start
= cur_jiffies
- dev
->jiffies_vid_cap
;
822 /* Get the number of buffers streamed since the start */
823 buffers_since_start
= (u64
)jiffies_since_start
* denominator
+
824 (HZ
* numerator
) / 2;
825 do_div(buffers_since_start
, HZ
* numerator
);
828 * After more than 0xf0000000 (rounded down to a multiple of
829 * 'jiffies-per-day' to ease jiffies_to_msecs calculation)
830 * jiffies have passed since we started streaming reset the
831 * counters and keep track of the sequence offset.
833 if (jiffies_since_start
> JIFFIES_RESYNC
) {
834 dev
->jiffies_vid_cap
= cur_jiffies
;
835 dev
->cap_seq_offset
= buffers_since_start
;
836 buffers_since_start
= 0;
838 dropped_bufs
= buffers_since_start
+ dev
->cap_seq_offset
- dev
->cap_seq_count
;
839 dev
->cap_seq_count
= buffers_since_start
+ dev
->cap_seq_offset
;
840 dev
->vid_cap_seq_count
= dev
->cap_seq_count
- dev
->vid_cap_seq_start
;
841 dev
->vbi_cap_seq_count
= dev
->cap_seq_count
- dev
->vbi_cap_seq_start
;
843 vivid_thread_vid_cap_tick(dev
, dropped_bufs
);
846 * Calculate the number of 'numerators' streamed since we started,
847 * including the current buffer.
849 numerators_since_start
= ++buffers_since_start
* numerator
;
851 /* And the number of jiffies since we started */
852 jiffies_since_start
= jiffies
- dev
->jiffies_vid_cap
;
854 mutex_unlock(&dev
->mutex
);
857 * Calculate when that next buffer is supposed to start
858 * in jiffies since we started streaming.
860 next_jiffies_since_start
= numerators_since_start
* HZ
+
862 do_div(next_jiffies_since_start
, denominator
);
863 /* If it is in the past, then just schedule asap */
864 if (next_jiffies_since_start
< jiffies_since_start
)
865 next_jiffies_since_start
= jiffies_since_start
;
867 wait_jiffies
= next_jiffies_since_start
- jiffies_since_start
;
868 schedule_timeout_interruptible(wait_jiffies
? wait_jiffies
: 1);
870 dprintk(dev
, 1, "Video Capture Thread End\n");
874 static void vivid_grab_controls(struct vivid_dev
*dev
, bool grab
)
876 v4l2_ctrl_grab(dev
->ctrl_has_crop_cap
, grab
);
877 v4l2_ctrl_grab(dev
->ctrl_has_compose_cap
, grab
);
878 v4l2_ctrl_grab(dev
->ctrl_has_scaler_cap
, grab
);
881 int vivid_start_generating_vid_cap(struct vivid_dev
*dev
, bool *pstreaming
)
883 dprintk(dev
, 1, "%s\n", __func__
);
885 if (dev
->kthread_vid_cap
) {
886 u32 seq_count
= dev
->cap_seq_count
+ dev
->seq_wrap
* 128;
888 if (pstreaming
== &dev
->vid_cap_streaming
)
889 dev
->vid_cap_seq_start
= seq_count
;
891 dev
->vbi_cap_seq_start
= seq_count
;
896 /* Resets frame counters */
897 tpg_init_mv_count(&dev
->tpg
);
899 dev
->vid_cap_seq_start
= dev
->seq_wrap
* 128;
900 dev
->vbi_cap_seq_start
= dev
->seq_wrap
* 128;
902 dev
->kthread_vid_cap
= kthread_run(vivid_thread_vid_cap
, dev
,
903 "%s-vid-cap", dev
->v4l2_dev
.name
);
905 if (IS_ERR(dev
->kthread_vid_cap
)) {
906 int err
= PTR_ERR(dev
->kthread_vid_cap
);
908 dev
->kthread_vid_cap
= NULL
;
909 v4l2_err(&dev
->v4l2_dev
, "kernel_thread() failed\n");
913 vivid_grab_controls(dev
, true);
915 dprintk(dev
, 1, "returning from %s\n", __func__
);
919 void vivid_stop_generating_vid_cap(struct vivid_dev
*dev
, bool *pstreaming
)
921 dprintk(dev
, 1, "%s\n", __func__
);
923 if (dev
->kthread_vid_cap
== NULL
)
927 if (pstreaming
== &dev
->vid_cap_streaming
) {
928 /* Release all active buffers */
929 while (!list_empty(&dev
->vid_cap_active
)) {
930 struct vivid_buffer
*buf
;
932 buf
= list_entry(dev
->vid_cap_active
.next
,
933 struct vivid_buffer
, list
);
934 list_del(&buf
->list
);
935 v4l2_ctrl_request_complete(buf
->vb
.vb2_buf
.req_obj
.req
,
936 &dev
->ctrl_hdl_vid_cap
);
937 vb2_buffer_done(&buf
->vb
.vb2_buf
, VB2_BUF_STATE_ERROR
);
938 dprintk(dev
, 2, "vid_cap buffer %d done\n",
939 buf
->vb
.vb2_buf
.index
);
943 if (pstreaming
== &dev
->vbi_cap_streaming
) {
944 while (!list_empty(&dev
->vbi_cap_active
)) {
945 struct vivid_buffer
*buf
;
947 buf
= list_entry(dev
->vbi_cap_active
.next
,
948 struct vivid_buffer
, list
);
949 list_del(&buf
->list
);
950 v4l2_ctrl_request_complete(buf
->vb
.vb2_buf
.req_obj
.req
,
951 &dev
->ctrl_hdl_vbi_cap
);
952 vb2_buffer_done(&buf
->vb
.vb2_buf
, VB2_BUF_STATE_ERROR
);
953 dprintk(dev
, 2, "vbi_cap buffer %d done\n",
954 buf
->vb
.vb2_buf
.index
);
958 if (dev
->vid_cap_streaming
|| dev
->vbi_cap_streaming
)
961 /* shutdown control thread */
962 vivid_grab_controls(dev
, false);
963 kthread_stop(dev
->kthread_vid_cap
);
964 dev
->kthread_vid_cap
= NULL
;