vivid-kthread-cap.c 29 KB

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