vivid-kthread-cap.c 29 KB

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