1
2
3
4
5
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
43static 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[dev->input];
47 return 0;
48}
49
50static 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
99static 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
111static void scale_line(const u8 *src, u8 *dst, unsigned srcw, unsigned dstw, unsigned twopixsize)
112{
113
114 unsigned int_part;
115 unsigned fract_part;
116 unsigned src_x = 0;
117 unsigned error = 0;
118 unsigned x;
119
120
121
122
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164static void vivid_precalc_copy_rects(struct vivid_dev *dev)
165{
166
167 struct v4l2_rect r_fb = {
168 0, 0, dev->display_width, dev->display_height
169 };
170
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
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
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
221static 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
235static noinline_for_stack int vivid_copy_buffer(struct vivid_dev *dev, unsigned p,
236 u8 *vcapbuf, 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
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
289
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
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
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
319
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
328 if (dev->loop_vid_cap.left)
329 memcpy(vcapbuf, tpg->black_line[p], vid_cap_left);
330
331
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
355
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
381update_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
405static 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[dev->input] & 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[dev->input])) ||
423 (vivid_is_hdmi_cap(dev) &&
424 !VIVID_INVALID_SIGNAL(dev->dv_timings_signal_mode[dev->input]))))
425 is_loop = true;
426
427 buf->vb.sequence = dev->vid_cap_seq_count;
428 if (dev->field_cap == V4L2_FIELD_ALTERNATE) {
429
430
431
432
433
434
435 buf->vb.field = ((dev->vid_cap_seq_count & 1) ^ is_60hz) ?
436 V4L2_FIELD_BOTTOM : V4L2_FIELD_TOP;
437
438
439
440
441 buf->vb.sequence /= 2;
442 } else {
443 buf->vb.field = dev->field_cap;
444 }
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]);
448
449 vivid_precalc_copy_rects(dev);
450
451 for (p = 0; p < tpg_g_planes(tpg); p++) {
452 void *vbuf = plane_vaddr(tpg, buf, p,
453 tpg->bytesperline, tpg->buf_height);
454
455
456
457
458
459
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];
464 }
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),
468 p, vbuf);
469 }
470 dev->must_blank[buf->vb.vb2_buf.index] = false;
471
472
473 if (dev->field_cap != V4L2_FIELD_ALTERNATE ||
474 (dev->vid_cap_seq_count & 1) == 0)
475 dev->ms_vid_cap =
476 jiffies_to_msecs(jiffies - dev->jiffies_vid_cap);
477
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,
483 (ms / 1000) % 60,
484 ms % 1000,
485 buf->vb.sequence,
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);
490 }
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);
495
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,
503 dev->hue->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 ",
518 dev->int32->cur.val,
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);
536 }
537 if (dev->osd[0]) {
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,
542 16, str);
543 }
544 if (dev->osd_jiffies &&
545 time_is_before_jiffies(dev->osd_jiffies + 5 * HZ)) {
546 dev->osd[0] = 0;
547 dev->osd_jiffies = 0;
548 }
549 }
550 }
551}
552
553
554
555
556static bool valid_pix(struct vivid_dev *dev, int win_y, int win_x, int fb_y, int fb_x)
557{
558 int i;
559
560 if (dev->bitmap_cap) {
561
562
563
564
565
566 const u8 *p = dev->bitmap_cap;
567 unsigned stride = (dev->compose_cap.width + 7) / 8;
568
569 if (!(p[stride * win_y + win_x / 8] & (1 << (win_x & 7))))
570 return false;
571 }
572
573 for (i = 0; i < dev->clipcount_cap; i++) {
574
575
576
577
578 struct v4l2_rect *r = &dev->clips_cap[i].c;
579
580 if (fb_y >= r->top && fb_y < r->top + r->height &&
581 fb_x >= r->left && fb_x < r->left + r->width)
582 return false;
583 }
584 return true;
585}
586
587
588
589
590
591static void vivid_overlay(struct vivid_dev *dev, struct vivid_buffer *buf)
592{
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
601 bool quick = dev->bitmap_cap == NULL && dev->clipcount_cap == 0;
602 int x, y, w, out_x = 0;
603
604
605
606
607
608 if (WARN_ON(pixsize == 0))
609 return;
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)
613 return;
614
615 vbuf += dev->compose_cap.left * pixsize + dev->compose_cap.top * stride;
616 x = dev->overlay_cap_left;
617 w = img_width;
618 if (x < 0) {
619 out_x = -x;
620 w = w - out_x;
621 x = 0;
622 } else {
623 w = dev->fb_cap.fmt.width - x;
624 if (w > img_width)
625 w = img_width;
626 }
627 if (w <= 0)
628 return;
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) {
634 int px;
635
636 if (y < 0 || y > dev->fb_cap.fmt.height)
637 continue;
638 if (quick) {
639 memcpy(vbase + x * pixsize,
640 vbuf + out_x * pixsize, w * pixsize);
641 vbase += dev->fb_cap.fmt.bytesperline;
642 continue;
643 }
644 for (px = 0; px < w; px++) {
645 if (!valid_pix(dev, y - dev->overlay_cap_top,
646 px + out_x, y, px + x))
647 continue;
648 memcpy(vbase + (px + x) * pixsize,
649 vbuf + (px + out_x) * pixsize,
650 pixsize);
651 }
652 vbase += dev->fb_cap.fmt.bytesperline;
653 }
654}
655
656static void vivid_cap_update_frame_period(struct vivid_dev *dev)
657{
658 u64 f_period;
659
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)
665 f_period >>= 1;
666
667
668
669
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;
673}
674
675static noinline_for_stack void vivid_thread_vid_cap_tick(struct vivid_dev *dev,
676 int dropped_bufs)
677{
678 struct vivid_buffer *vid_cap_buf = NULL;
679 struct vivid_buffer *vbi_cap_buf = NULL;
680 u64 f_time = 0;
681
682 dprintk(dev, 1, "Video Capture Thread Tick\n");
683
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);
688
689
690 if (dev->perc_dropped_buffers &&
691 prandom_u32_max(100) < dev->perc_dropped_buffers)
692 goto update_mv;
693
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);
698 }
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);
705 }
706 }
707 spin_unlock(&dev->slock);
708
709 if (!vid_cap_buf && !vbi_cap_buf)
710 goto update_mv;
711
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;
716
717 if (vid_cap_buf) {
718 v4l2_ctrl_request_setup(vid_cap_buf->vb.vb2_buf.req_obj.req,
719 &dev->ctrl_hdl_vid_cap);
720
721 vivid_fillbuff(dev, vid_cap_buf);
722 dprintk(dev, 1, "filled buffer %d\n",
723 vid_cap_buf->vb.vb2_buf.index);
724
725
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);
729
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);
736
737 vid_cap_buf->vb.vb2_buf.timestamp = f_time;
738 }
739
740 if (vbi_cap_buf) {
741 u64 vbi_period;
742
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);
747 else
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);
755
756
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;
760 }
761 dev->dqbuf_error = false;
762
763update_mv:
764
765 tpg_update_mv_count(&dev->tpg, dev->field_cap == V4L2_FIELD_NONE ||
766 dev->field_cap == V4L2_FIELD_ALTERNATE);
767}
768
769static int vivid_thread_vid_cap(void *data)
770{
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;
778 unsigned numerator;
779 unsigned denominator;
780 int dropped_bufs;
781
782 dprintk(dev, 1, "Video Capture Thread Start\n");
783
784 set_freezable();
785
786
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);
793
794 for (;;) {
795 try_to_freeze();
796 if (kthread_should_stop())
797 break;
798
799 mutex_lock(&dev->mutex);
800 cur_jiffies = jiffies;
801 if (dev->cap_seq_resync) {
802 dev->jiffies_vid_cap = cur_jiffies;
803 dev->cap_seq_offset = dev->cap_seq_count + 1;
804 dev->cap_seq_count = 0;
805 dev->cap_stream_start += dev->cap_frame_period *
806 dev->cap_seq_offset;
807 vivid_cap_update_frame_period(dev);
808 dev->cap_seq_resync = false;
809 }
810 numerator = dev->timeperframe_vid_cap.numerator;
811 denominator = dev->timeperframe_vid_cap.denominator;
812
813 if (dev->field_cap == V4L2_FIELD_ALTERNATE)
814 denominator *= 2;
815
816
817 jiffies_since_start = cur_jiffies - dev->jiffies_vid_cap;
818
819 buffers_since_start = (u64)jiffies_since_start * denominator +
820 (HZ * numerator) / 2;
821 do_div(buffers_since_start, HZ * numerator);
822
823
824
825
826
827
828
829 if (jiffies_since_start > JIFFIES_RESYNC) {
830 dev->jiffies_vid_cap = cur_jiffies;
831 dev->cap_seq_offset = buffers_since_start;
832 buffers_since_start = 0;
833 }
834 dropped_bufs = buffers_since_start + dev->cap_seq_offset - dev->cap_seq_count;
835 dev->cap_seq_count = buffers_since_start + dev->cap_seq_offset;
836 dev->vid_cap_seq_count = dev->cap_seq_count - dev->vid_cap_seq_start;
837 dev->vbi_cap_seq_count = dev->cap_seq_count - dev->vbi_cap_seq_start;
838
839 vivid_thread_vid_cap_tick(dev, dropped_bufs);
840
841
842
843
844
845 numerators_since_start = ++buffers_since_start * numerator;
846
847
848 jiffies_since_start = jiffies - dev->jiffies_vid_cap;
849
850 mutex_unlock(&dev->mutex);
851
852
853
854
855
856 next_jiffies_since_start = numerators_since_start * HZ +
857 denominator / 2;
858 do_div(next_jiffies_since_start, denominator);
859
860 if (next_jiffies_since_start < jiffies_since_start)
861 next_jiffies_since_start = jiffies_since_start;
862
863 wait_jiffies = next_jiffies_since_start - jiffies_since_start;
864 schedule_timeout_interruptible(wait_jiffies ? wait_jiffies : 1);
865 }
866 dprintk(dev, 1, "Video Capture Thread End\n");
867 return 0;
868}
869
870static void vivid_grab_controls(struct vivid_dev *dev, bool grab)
871{
872 v4l2_ctrl_grab(dev->ctrl_has_crop_cap, grab);
873 v4l2_ctrl_grab(dev->ctrl_has_compose_cap, grab);
874 v4l2_ctrl_grab(dev->ctrl_has_scaler_cap, grab);
875}
876
877int vivid_start_generating_vid_cap(struct vivid_dev *dev, bool *pstreaming)
878{
879 dprintk(dev, 1, "%s\n", __func__);
880
881 if (dev->kthread_vid_cap) {
882 u32 seq_count = dev->cap_seq_count + dev->seq_wrap * 128;
883
884 if (pstreaming == &dev->vid_cap_streaming)
885 dev->vid_cap_seq_start = seq_count;
886 else
887 dev->vbi_cap_seq_start = seq_count;
888 *pstreaming = true;
889 return 0;
890 }
891
892
893 tpg_init_mv_count(&dev->tpg);
894
895 dev->vid_cap_seq_start = dev->seq_wrap * 128;
896 dev->vbi_cap_seq_start = dev->seq_wrap * 128;
897
898 dev->kthread_vid_cap = kthread_run(vivid_thread_vid_cap, dev,
899 "%s-vid-cap", dev->v4l2_dev.name);
900
901 if (IS_ERR(dev->kthread_vid_cap)) {
902 int err = PTR_ERR(dev->kthread_vid_cap);
903
904 dev->kthread_vid_cap = NULL;
905 v4l2_err(&dev->v4l2_dev, "kernel_thread() failed\n");
906 return err;
907 }
908 *pstreaming = true;
909 vivid_grab_controls(dev, true);
910
911 dprintk(dev, 1, "returning from %s\n", __func__);
912 return 0;
913}
914
915void vivid_stop_generating_vid_cap(struct vivid_dev *dev, bool *pstreaming)
916{
917 dprintk(dev, 1, "%s\n", __func__);
918
919 if (dev->kthread_vid_cap == NULL)
920 return;
921
922 *pstreaming = false;
923 if (pstreaming == &dev->vid_cap_streaming) {
924
925 while (!list_empty(&dev->vid_cap_active)) {
926 struct vivid_buffer *buf;
927
928 buf = list_entry(dev->vid_cap_active.next,
929 struct vivid_buffer, list);
930 list_del(&buf->list);
931 v4l2_ctrl_request_complete(buf->vb.vb2_buf.req_obj.req,
932 &dev->ctrl_hdl_vid_cap);
933 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
934 dprintk(dev, 2, "vid_cap buffer %d done\n",
935 buf->vb.vb2_buf.index);
936 }
937 }
938
939 if (pstreaming == &dev->vbi_cap_streaming) {
940 while (!list_empty(&dev->vbi_cap_active)) {
941 struct vivid_buffer *buf;
942
943 buf = list_entry(dev->vbi_cap_active.next,
944 struct vivid_buffer, list);
945 list_del(&buf->list);
946 v4l2_ctrl_request_complete(buf->vb.vb2_buf.req_obj.req,
947 &dev->ctrl_hdl_vbi_cap);
948 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
949 dprintk(dev, 2, "vbi_cap buffer %d done\n",
950 buf->vb.vb2_buf.index);
951 }
952 }
953
954 if (dev->vid_cap_streaming || dev->vbi_cap_streaming)
955 return;
956
957
958 vivid_grab_controls(dev, false);
959 mutex_unlock(&dev->mutex);
960 kthread_stop(dev->kthread_vid_cap);
961 dev->kthread_vid_cap = NULL;
962 mutex_lock(&dev->mutex);
963}
964