vivid-kthread-cap.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916
  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, p);
  215. vbuf = vb2_plane_vaddr(&buf->vb, 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.v4l2_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.v4l2_buf.field = vid_out_buf->vb.v4l2_buf.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.v4l2_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) && !VIVID_INVALID_SIGNAL(dev->std_signal_mode)) ||
  390. (vivid_is_hdmi_cap(dev) && !VIVID_INVALID_SIGNAL(dev->dv_timings_signal_mode))))
  391. is_loop = true;
  392. buf->vb.v4l2_buf.sequence = dev->vid_cap_seq_count;
  393. /*
  394. * Take the timestamp now if the timestamp source is set to
  395. * "Start of Exposure".
  396. */
  397. if (dev->tstamp_src_is_soe)
  398. v4l2_get_timestamp(&buf->vb.v4l2_buf.timestamp);
  399. if (dev->field_cap == V4L2_FIELD_ALTERNATE) {
  400. /*
  401. * 60 Hz standards start with the bottom field, 50 Hz standards
  402. * with the top field. So if the 0-based seq_count is even,
  403. * then the field is TOP for 50 Hz and BOTTOM for 60 Hz
  404. * standards.
  405. */
  406. buf->vb.v4l2_buf.field = ((dev->vid_cap_seq_count & 1) ^ is_60hz) ?
  407. V4L2_FIELD_BOTTOM : V4L2_FIELD_TOP;
  408. /*
  409. * The sequence counter counts frames, not fields. So divide
  410. * by two.
  411. */
  412. buf->vb.v4l2_buf.sequence /= 2;
  413. } else {
  414. buf->vb.v4l2_buf.field = dev->field_cap;
  415. }
  416. tpg_s_field(tpg, buf->vb.v4l2_buf.field,
  417. dev->field_cap == V4L2_FIELD_ALTERNATE);
  418. tpg_s_perc_fill_blank(tpg, dev->must_blank[buf->vb.v4l2_buf.index]);
  419. vivid_precalc_copy_rects(dev);
  420. for (p = 0; p < tpg_g_planes(tpg); p++) {
  421. void *vbuf = plane_vaddr(tpg, buf, p,
  422. tpg->bytesperline, tpg->buf_height);
  423. /*
  424. * The first plane of a multiplanar format has a non-zero
  425. * data_offset. This helps testing whether the application
  426. * correctly supports non-zero data offsets.
  427. */
  428. if (p < tpg_g_buffers(tpg) && dev->fmt_cap->data_offset[p]) {
  429. memset(vbuf, dev->fmt_cap->data_offset[p] & 0xff,
  430. dev->fmt_cap->data_offset[p]);
  431. vbuf += dev->fmt_cap->data_offset[p];
  432. }
  433. tpg_calc_text_basep(tpg, basep, p, vbuf);
  434. if (!is_loop || vivid_copy_buffer(dev, p, vbuf, buf))
  435. tpg_fill_plane_buffer(tpg, vivid_get_std_cap(dev), p, vbuf);
  436. }
  437. dev->must_blank[buf->vb.v4l2_buf.index] = false;
  438. /* Updates stream time, only update at the start of a new frame. */
  439. if (dev->field_cap != V4L2_FIELD_ALTERNATE || (buf->vb.v4l2_buf.sequence & 1) == 0)
  440. dev->ms_vid_cap = jiffies_to_msecs(jiffies - dev->jiffies_vid_cap);
  441. ms = dev->ms_vid_cap;
  442. if (dev->osd_mode <= 1) {
  443. snprintf(str, sizeof(str), " %02d:%02d:%02d:%03d %u%s",
  444. (ms / (60 * 60 * 1000)) % 24,
  445. (ms / (60 * 1000)) % 60,
  446. (ms / 1000) % 60,
  447. ms % 1000,
  448. buf->vb.v4l2_buf.sequence,
  449. (dev->field_cap == V4L2_FIELD_ALTERNATE) ?
  450. (buf->vb.v4l2_buf.field == V4L2_FIELD_TOP ?
  451. " top" : " bottom") : "");
  452. tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
  453. }
  454. if (dev->osd_mode == 0) {
  455. snprintf(str, sizeof(str), " %dx%d, input %d ",
  456. dev->src_rect.width, dev->src_rect.height, dev->input);
  457. tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
  458. gain = v4l2_ctrl_g_ctrl(dev->gain);
  459. mutex_lock(dev->ctrl_hdl_user_vid.lock);
  460. snprintf(str, sizeof(str),
  461. " brightness %3d, contrast %3d, saturation %3d, hue %d ",
  462. dev->brightness->cur.val,
  463. dev->contrast->cur.val,
  464. dev->saturation->cur.val,
  465. dev->hue->cur.val);
  466. tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
  467. snprintf(str, sizeof(str),
  468. " autogain %d, gain %3d, alpha 0x%02x ",
  469. dev->autogain->cur.val, gain, dev->alpha->cur.val);
  470. mutex_unlock(dev->ctrl_hdl_user_vid.lock);
  471. tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
  472. mutex_lock(dev->ctrl_hdl_user_aud.lock);
  473. snprintf(str, sizeof(str),
  474. " volume %3d, mute %d ",
  475. dev->volume->cur.val, dev->mute->cur.val);
  476. mutex_unlock(dev->ctrl_hdl_user_aud.lock);
  477. tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
  478. mutex_lock(dev->ctrl_hdl_user_gen.lock);
  479. snprintf(str, sizeof(str), " int32 %d, int64 %lld, bitmask %08x ",
  480. dev->int32->cur.val,
  481. *dev->int64->p_cur.p_s64,
  482. dev->bitmask->cur.val);
  483. tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
  484. snprintf(str, sizeof(str), " boolean %d, menu %s, string \"%s\" ",
  485. dev->boolean->cur.val,
  486. dev->menu->qmenu[dev->menu->cur.val],
  487. dev->string->p_cur.p_char);
  488. tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
  489. snprintf(str, sizeof(str), " integer_menu %lld, value %d ",
  490. dev->int_menu->qmenu_int[dev->int_menu->cur.val],
  491. dev->int_menu->cur.val);
  492. mutex_unlock(dev->ctrl_hdl_user_gen.lock);
  493. tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
  494. if (dev->button_pressed) {
  495. dev->button_pressed--;
  496. snprintf(str, sizeof(str), " button pressed!");
  497. tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
  498. }
  499. }
  500. /*
  501. * If "End of Frame" is specified at the timestamp source, then take
  502. * the timestamp now.
  503. */
  504. if (!dev->tstamp_src_is_soe)
  505. v4l2_get_timestamp(&buf->vb.v4l2_buf.timestamp);
  506. buf->vb.v4l2_buf.timestamp.tv_sec += dev->time_wrap_offset;
  507. }
  508. /*
  509. * Return true if this pixel coordinate is a valid video pixel.
  510. */
  511. static bool valid_pix(struct vivid_dev *dev, int win_y, int win_x, int fb_y, int fb_x)
  512. {
  513. int i;
  514. if (dev->bitmap_cap) {
  515. /*
  516. * Only if the corresponding bit in the bitmap is set can
  517. * the video pixel be shown. Coordinates are relative to
  518. * the overlay window set by VIDIOC_S_FMT.
  519. */
  520. const u8 *p = dev->bitmap_cap;
  521. unsigned stride = (dev->compose_cap.width + 7) / 8;
  522. if (!(p[stride * win_y + win_x / 8] & (1 << (win_x & 7))))
  523. return false;
  524. }
  525. for (i = 0; i < dev->clipcount_cap; i++) {
  526. /*
  527. * Only if the framebuffer coordinate is not in any of the
  528. * clip rectangles will be video pixel be shown.
  529. */
  530. struct v4l2_rect *r = &dev->clips_cap[i].c;
  531. if (fb_y >= r->top && fb_y < r->top + r->height &&
  532. fb_x >= r->left && fb_x < r->left + r->width)
  533. return false;
  534. }
  535. return true;
  536. }
  537. /*
  538. * Draw the image into the overlay buffer.
  539. * Note that the combination of overlay and multiplanar is not supported.
  540. */
  541. static void vivid_overlay(struct vivid_dev *dev, struct vivid_buffer *buf)
  542. {
  543. struct tpg_data *tpg = &dev->tpg;
  544. unsigned pixsize = tpg_g_twopixelsize(tpg, 0) / 2;
  545. void *vbase = dev->fb_vbase_cap;
  546. void *vbuf = vb2_plane_vaddr(&buf->vb, 0);
  547. unsigned img_width = dev->compose_cap.width;
  548. unsigned img_height = dev->compose_cap.height;
  549. unsigned stride = tpg->bytesperline[0];
  550. /* if quick is true, then valid_pix() doesn't have to be called */
  551. bool quick = dev->bitmap_cap == NULL && dev->clipcount_cap == 0;
  552. int x, y, w, out_x = 0;
  553. /*
  554. * Overlay support is only supported for formats that have a twopixelsize
  555. * that's >= 2. Warn and bail out if that's not the case.
  556. */
  557. if (WARN_ON(pixsize == 0))
  558. return;
  559. if ((dev->overlay_cap_field == V4L2_FIELD_TOP ||
  560. dev->overlay_cap_field == V4L2_FIELD_BOTTOM) &&
  561. dev->overlay_cap_field != buf->vb.v4l2_buf.field)
  562. return;
  563. vbuf += dev->compose_cap.left * pixsize + dev->compose_cap.top * stride;
  564. x = dev->overlay_cap_left;
  565. w = img_width;
  566. if (x < 0) {
  567. out_x = -x;
  568. w = w - out_x;
  569. x = 0;
  570. } else {
  571. w = dev->fb_cap.fmt.width - x;
  572. if (w > img_width)
  573. w = img_width;
  574. }
  575. if (w <= 0)
  576. return;
  577. if (dev->overlay_cap_top >= 0)
  578. vbase += dev->overlay_cap_top * dev->fb_cap.fmt.bytesperline;
  579. for (y = dev->overlay_cap_top;
  580. y < dev->overlay_cap_top + (int)img_height;
  581. y++, vbuf += stride) {
  582. int px;
  583. if (y < 0 || y > dev->fb_cap.fmt.height)
  584. continue;
  585. if (quick) {
  586. memcpy(vbase + x * pixsize,
  587. vbuf + out_x * pixsize, w * pixsize);
  588. vbase += dev->fb_cap.fmt.bytesperline;
  589. continue;
  590. }
  591. for (px = 0; px < w; px++) {
  592. if (!valid_pix(dev, y - dev->overlay_cap_top,
  593. px + out_x, y, px + x))
  594. continue;
  595. memcpy(vbase + (px + x) * pixsize,
  596. vbuf + (px + out_x) * pixsize,
  597. pixsize);
  598. }
  599. vbase += dev->fb_cap.fmt.bytesperline;
  600. }
  601. }
  602. static void vivid_thread_vid_cap_tick(struct vivid_dev *dev, int dropped_bufs)
  603. {
  604. struct vivid_buffer *vid_cap_buf = NULL;
  605. struct vivid_buffer *vbi_cap_buf = NULL;
  606. dprintk(dev, 1, "Video Capture Thread Tick\n");
  607. while (dropped_bufs-- > 1)
  608. tpg_update_mv_count(&dev->tpg,
  609. dev->field_cap == V4L2_FIELD_NONE ||
  610. dev->field_cap == V4L2_FIELD_ALTERNATE);
  611. /* Drop a certain percentage of buffers. */
  612. if (dev->perc_dropped_buffers &&
  613. prandom_u32_max(100) < dev->perc_dropped_buffers)
  614. goto update_mv;
  615. spin_lock(&dev->slock);
  616. if (!list_empty(&dev->vid_cap_active)) {
  617. vid_cap_buf = list_entry(dev->vid_cap_active.next, struct vivid_buffer, list);
  618. list_del(&vid_cap_buf->list);
  619. }
  620. if (!list_empty(&dev->vbi_cap_active)) {
  621. if (dev->field_cap != V4L2_FIELD_ALTERNATE ||
  622. (dev->vbi_cap_seq_count & 1)) {
  623. vbi_cap_buf = list_entry(dev->vbi_cap_active.next,
  624. struct vivid_buffer, list);
  625. list_del(&vbi_cap_buf->list);
  626. }
  627. }
  628. spin_unlock(&dev->slock);
  629. if (!vid_cap_buf && !vbi_cap_buf)
  630. goto update_mv;
  631. if (vid_cap_buf) {
  632. /* Fill buffer */
  633. vivid_fillbuff(dev, vid_cap_buf);
  634. dprintk(dev, 1, "filled buffer %d\n",
  635. vid_cap_buf->vb.v4l2_buf.index);
  636. /* Handle overlay */
  637. if (dev->overlay_cap_owner && dev->fb_cap.base &&
  638. dev->fb_cap.fmt.pixelformat == dev->fmt_cap->fourcc)
  639. vivid_overlay(dev, vid_cap_buf);
  640. vb2_buffer_done(&vid_cap_buf->vb, dev->dqbuf_error ?
  641. VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
  642. dprintk(dev, 2, "vid_cap buffer %d done\n",
  643. vid_cap_buf->vb.v4l2_buf.index);
  644. }
  645. if (vbi_cap_buf) {
  646. if (dev->stream_sliced_vbi_cap)
  647. vivid_sliced_vbi_cap_process(dev, vbi_cap_buf);
  648. else
  649. vivid_raw_vbi_cap_process(dev, vbi_cap_buf);
  650. vb2_buffer_done(&vbi_cap_buf->vb, dev->dqbuf_error ?
  651. VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
  652. dprintk(dev, 2, "vbi_cap %d done\n",
  653. vbi_cap_buf->vb.v4l2_buf.index);
  654. }
  655. dev->dqbuf_error = false;
  656. update_mv:
  657. /* Update the test pattern movement counters */
  658. tpg_update_mv_count(&dev->tpg, dev->field_cap == V4L2_FIELD_NONE ||
  659. dev->field_cap == V4L2_FIELD_ALTERNATE);
  660. }
  661. static int vivid_thread_vid_cap(void *data)
  662. {
  663. struct vivid_dev *dev = data;
  664. u64 numerators_since_start;
  665. u64 buffers_since_start;
  666. u64 next_jiffies_since_start;
  667. unsigned long jiffies_since_start;
  668. unsigned long cur_jiffies;
  669. unsigned wait_jiffies;
  670. unsigned numerator;
  671. unsigned denominator;
  672. int dropped_bufs;
  673. dprintk(dev, 1, "Video Capture Thread Start\n");
  674. set_freezable();
  675. /* Resets frame counters */
  676. dev->cap_seq_offset = 0;
  677. dev->cap_seq_count = 0;
  678. dev->cap_seq_resync = false;
  679. dev->jiffies_vid_cap = jiffies;
  680. for (;;) {
  681. try_to_freeze();
  682. if (kthread_should_stop())
  683. break;
  684. mutex_lock(&dev->mutex);
  685. cur_jiffies = jiffies;
  686. if (dev->cap_seq_resync) {
  687. dev->jiffies_vid_cap = cur_jiffies;
  688. dev->cap_seq_offset = dev->cap_seq_count + 1;
  689. dev->cap_seq_count = 0;
  690. dev->cap_seq_resync = false;
  691. }
  692. numerator = dev->timeperframe_vid_cap.numerator;
  693. denominator = dev->timeperframe_vid_cap.denominator;
  694. if (dev->field_cap == V4L2_FIELD_ALTERNATE)
  695. denominator *= 2;
  696. /* Calculate the number of jiffies since we started streaming */
  697. jiffies_since_start = cur_jiffies - dev->jiffies_vid_cap;
  698. /* Get the number of buffers streamed since the start */
  699. buffers_since_start = (u64)jiffies_since_start * denominator +
  700. (HZ * numerator) / 2;
  701. do_div(buffers_since_start, HZ * numerator);
  702. /*
  703. * After more than 0xf0000000 (rounded down to a multiple of
  704. * 'jiffies-per-day' to ease jiffies_to_msecs calculation)
  705. * jiffies have passed since we started streaming reset the
  706. * counters and keep track of the sequence offset.
  707. */
  708. if (jiffies_since_start > JIFFIES_RESYNC) {
  709. dev->jiffies_vid_cap = cur_jiffies;
  710. dev->cap_seq_offset = buffers_since_start;
  711. buffers_since_start = 0;
  712. }
  713. dropped_bufs = buffers_since_start + dev->cap_seq_offset - dev->cap_seq_count;
  714. dev->cap_seq_count = buffers_since_start + dev->cap_seq_offset;
  715. dev->vid_cap_seq_count = dev->cap_seq_count - dev->vid_cap_seq_start;
  716. dev->vbi_cap_seq_count = dev->cap_seq_count - dev->vbi_cap_seq_start;
  717. vivid_thread_vid_cap_tick(dev, dropped_bufs);
  718. /*
  719. * Calculate the number of 'numerators' streamed since we started,
  720. * including the current buffer.
  721. */
  722. numerators_since_start = ++buffers_since_start * numerator;
  723. /* And the number of jiffies since we started */
  724. jiffies_since_start = jiffies - dev->jiffies_vid_cap;
  725. mutex_unlock(&dev->mutex);
  726. /*
  727. * Calculate when that next buffer is supposed to start
  728. * in jiffies since we started streaming.
  729. */
  730. next_jiffies_since_start = numerators_since_start * HZ +
  731. denominator / 2;
  732. do_div(next_jiffies_since_start, denominator);
  733. /* If it is in the past, then just schedule asap */
  734. if (next_jiffies_since_start < jiffies_since_start)
  735. next_jiffies_since_start = jiffies_since_start;
  736. wait_jiffies = next_jiffies_since_start - jiffies_since_start;
  737. schedule_timeout_interruptible(wait_jiffies ? wait_jiffies : 1);
  738. }
  739. dprintk(dev, 1, "Video Capture Thread End\n");
  740. return 0;
  741. }
  742. static void vivid_grab_controls(struct vivid_dev *dev, bool grab)
  743. {
  744. v4l2_ctrl_grab(dev->ctrl_has_crop_cap, grab);
  745. v4l2_ctrl_grab(dev->ctrl_has_compose_cap, grab);
  746. v4l2_ctrl_grab(dev->ctrl_has_scaler_cap, grab);
  747. }
  748. int vivid_start_generating_vid_cap(struct vivid_dev *dev, bool *pstreaming)
  749. {
  750. dprintk(dev, 1, "%s\n", __func__);
  751. if (dev->kthread_vid_cap) {
  752. u32 seq_count = dev->cap_seq_count + dev->seq_wrap * 128;
  753. if (pstreaming == &dev->vid_cap_streaming)
  754. dev->vid_cap_seq_start = seq_count;
  755. else
  756. dev->vbi_cap_seq_start = seq_count;
  757. *pstreaming = true;
  758. return 0;
  759. }
  760. /* Resets frame counters */
  761. tpg_init_mv_count(&dev->tpg);
  762. dev->vid_cap_seq_start = dev->seq_wrap * 128;
  763. dev->vbi_cap_seq_start = dev->seq_wrap * 128;
  764. dev->kthread_vid_cap = kthread_run(vivid_thread_vid_cap, dev,
  765. "%s-vid-cap", dev->v4l2_dev.name);
  766. if (IS_ERR(dev->kthread_vid_cap)) {
  767. v4l2_err(&dev->v4l2_dev, "kernel_thread() failed\n");
  768. return PTR_ERR(dev->kthread_vid_cap);
  769. }
  770. *pstreaming = true;
  771. vivid_grab_controls(dev, true);
  772. dprintk(dev, 1, "returning from %s\n", __func__);
  773. return 0;
  774. }
  775. void vivid_stop_generating_vid_cap(struct vivid_dev *dev, bool *pstreaming)
  776. {
  777. dprintk(dev, 1, "%s\n", __func__);
  778. if (dev->kthread_vid_cap == NULL)
  779. return;
  780. *pstreaming = false;
  781. if (pstreaming == &dev->vid_cap_streaming) {
  782. /* Release all active buffers */
  783. while (!list_empty(&dev->vid_cap_active)) {
  784. struct vivid_buffer *buf;
  785. buf = list_entry(dev->vid_cap_active.next,
  786. struct vivid_buffer, list);
  787. list_del(&buf->list);
  788. vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
  789. dprintk(dev, 2, "vid_cap buffer %d done\n",
  790. buf->vb.v4l2_buf.index);
  791. }
  792. }
  793. if (pstreaming == &dev->vbi_cap_streaming) {
  794. while (!list_empty(&dev->vbi_cap_active)) {
  795. struct vivid_buffer *buf;
  796. buf = list_entry(dev->vbi_cap_active.next,
  797. struct vivid_buffer, list);
  798. list_del(&buf->list);
  799. vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
  800. dprintk(dev, 2, "vbi_cap buffer %d done\n",
  801. buf->vb.v4l2_buf.index);
  802. }
  803. }
  804. if (dev->vid_cap_streaming || dev->vbi_cap_streaming)
  805. return;
  806. /* shutdown control thread */
  807. vivid_grab_controls(dev, false);
  808. mutex_unlock(&dev->mutex);
  809. kthread_stop(dev->kthread_vid_cap);
  810. dev->kthread_vid_cap = NULL;
  811. mutex_lock(&dev->mutex);
  812. }