vivid-kthread-cap.c 29 KB

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