vivid-kthread-cap.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  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 int vivid_copy_buffer(struct vivid_dev *dev, unsigned p, u8 *vcapbuf,
  209. struct vivid_buffer *vid_cap_buf)
  210. {
  211. bool blank = dev->must_blank[vid_cap_buf->vb.v4l2_buf.index];
  212. struct tpg_data *tpg = &dev->tpg;
  213. struct vivid_buffer *vid_out_buf = NULL;
  214. unsigned pixsize = tpg_g_twopixelsize(tpg, p) / 2;
  215. unsigned img_width = dev->compose_cap.width;
  216. unsigned img_height = dev->compose_cap.height;
  217. unsigned stride_cap = tpg->bytesperline[p];
  218. unsigned stride_out = dev->bytesperline_out[p];
  219. unsigned stride_osd = dev->display_byte_stride;
  220. unsigned hmax = (img_height * tpg->perc_fill) / 100;
  221. u8 *voutbuf;
  222. u8 *vosdbuf = NULL;
  223. unsigned y;
  224. bool blend = dev->bitmap_out || dev->clipcount_out || dev->fbuf_out_flags;
  225. /* Coarse scaling with Bresenham */
  226. unsigned vid_out_int_part;
  227. unsigned vid_out_fract_part;
  228. unsigned vid_out_y = 0;
  229. unsigned vid_out_error = 0;
  230. unsigned vid_overlay_int_part = 0;
  231. unsigned vid_overlay_fract_part = 0;
  232. unsigned vid_overlay_y = 0;
  233. unsigned vid_overlay_error = 0;
  234. unsigned vid_cap_right;
  235. bool quick;
  236. vid_out_int_part = dev->loop_vid_out.height / dev->loop_vid_cap.height;
  237. vid_out_fract_part = dev->loop_vid_out.height % dev->loop_vid_cap.height;
  238. if (!list_empty(&dev->vid_out_active))
  239. vid_out_buf = list_entry(dev->vid_out_active.next,
  240. struct vivid_buffer, list);
  241. if (vid_out_buf == NULL)
  242. return -ENODATA;
  243. vid_cap_buf->vb.v4l2_buf.field = vid_out_buf->vb.v4l2_buf.field;
  244. voutbuf = vb2_plane_vaddr(&vid_out_buf->vb, p) +
  245. vid_out_buf->vb.v4l2_planes[p].data_offset;
  246. voutbuf += dev->loop_vid_out.left * pixsize + dev->loop_vid_out.top * stride_out;
  247. vcapbuf += dev->compose_cap.left * pixsize + dev->compose_cap.top * stride_cap;
  248. if (dev->loop_vid_copy.width == 0 || dev->loop_vid_copy.height == 0) {
  249. /*
  250. * If there is nothing to copy, then just fill the capture window
  251. * with black.
  252. */
  253. for (y = 0; y < hmax; y++, vcapbuf += stride_cap)
  254. memcpy(vcapbuf, tpg->black_line[p], img_width * pixsize);
  255. return 0;
  256. }
  257. if (dev->overlay_out_enabled &&
  258. dev->loop_vid_overlay.width && dev->loop_vid_overlay.height) {
  259. vosdbuf = dev->video_vbase;
  260. vosdbuf += dev->loop_fb_copy.left * pixsize +
  261. dev->loop_fb_copy.top * stride_osd;
  262. vid_overlay_int_part = dev->loop_vid_overlay.height /
  263. dev->loop_vid_overlay_cap.height;
  264. vid_overlay_fract_part = dev->loop_vid_overlay.height %
  265. dev->loop_vid_overlay_cap.height;
  266. }
  267. vid_cap_right = dev->loop_vid_cap.left + dev->loop_vid_cap.width;
  268. /* quick is true if no video scaling is needed */
  269. quick = dev->loop_vid_out.width == dev->loop_vid_cap.width;
  270. dev->cur_scaled_line = dev->loop_vid_out.height;
  271. for (y = 0; y < hmax; y++, vcapbuf += stride_cap) {
  272. /* osdline is true if this line requires overlay blending */
  273. bool osdline = vosdbuf && y >= dev->loop_vid_overlay_cap.top &&
  274. y < dev->loop_vid_overlay_cap.top + dev->loop_vid_overlay_cap.height;
  275. /*
  276. * If this line of the capture buffer doesn't get any video, then
  277. * just fill with black.
  278. */
  279. if (y < dev->loop_vid_cap.top ||
  280. y >= dev->loop_vid_cap.top + dev->loop_vid_cap.height) {
  281. memcpy(vcapbuf, tpg->black_line[p], img_width * pixsize);
  282. continue;
  283. }
  284. /* fill the left border with black */
  285. if (dev->loop_vid_cap.left)
  286. memcpy(vcapbuf, tpg->black_line[p], dev->loop_vid_cap.left * pixsize);
  287. /* fill the right border with black */
  288. if (vid_cap_right < img_width)
  289. memcpy(vcapbuf + vid_cap_right * pixsize,
  290. tpg->black_line[p], (img_width - vid_cap_right) * pixsize);
  291. if (quick && !osdline) {
  292. memcpy(vcapbuf + dev->loop_vid_cap.left * pixsize,
  293. voutbuf + vid_out_y * stride_out,
  294. dev->loop_vid_cap.width * pixsize);
  295. goto update_vid_out_y;
  296. }
  297. if (dev->cur_scaled_line == vid_out_y) {
  298. memcpy(vcapbuf + dev->loop_vid_cap.left * pixsize,
  299. dev->scaled_line,
  300. dev->loop_vid_cap.width * pixsize);
  301. goto update_vid_out_y;
  302. }
  303. if (!osdline) {
  304. scale_line(voutbuf + vid_out_y * stride_out, dev->scaled_line,
  305. dev->loop_vid_out.width, dev->loop_vid_cap.width,
  306. tpg_g_twopixelsize(tpg, p));
  307. } else {
  308. /*
  309. * Offset in bytes within loop_vid_copy to the start of the
  310. * loop_vid_overlay rectangle.
  311. */
  312. unsigned offset =
  313. (dev->loop_vid_overlay.left - dev->loop_vid_copy.left) * pixsize;
  314. u8 *osd = vosdbuf + vid_overlay_y * stride_osd;
  315. scale_line(voutbuf + vid_out_y * stride_out, dev->blended_line,
  316. dev->loop_vid_out.width, dev->loop_vid_copy.width,
  317. tpg_g_twopixelsize(tpg, p));
  318. if (blend)
  319. blend_line(dev, vid_overlay_y + dev->loop_vid_overlay.top,
  320. dev->loop_vid_overlay.left,
  321. dev->blended_line + offset, osd,
  322. dev->loop_vid_overlay.width, pixsize);
  323. else
  324. memcpy(dev->blended_line + offset,
  325. osd, dev->loop_vid_overlay.width * pixsize);
  326. scale_line(dev->blended_line, dev->scaled_line,
  327. dev->loop_vid_copy.width, dev->loop_vid_cap.width,
  328. tpg_g_twopixelsize(tpg, p));
  329. }
  330. dev->cur_scaled_line = vid_out_y;
  331. memcpy(vcapbuf + dev->loop_vid_cap.left * pixsize,
  332. dev->scaled_line,
  333. dev->loop_vid_cap.width * pixsize);
  334. update_vid_out_y:
  335. if (osdline) {
  336. vid_overlay_y += vid_overlay_int_part;
  337. vid_overlay_error += vid_overlay_fract_part;
  338. if (vid_overlay_error >= dev->loop_vid_overlay_cap.height) {
  339. vid_overlay_error -= dev->loop_vid_overlay_cap.height;
  340. vid_overlay_y++;
  341. }
  342. }
  343. vid_out_y += vid_out_int_part;
  344. vid_out_error += vid_out_fract_part;
  345. if (vid_out_error >= dev->loop_vid_cap.height) {
  346. vid_out_error -= dev->loop_vid_cap.height;
  347. vid_out_y++;
  348. }
  349. }
  350. if (!blank)
  351. return 0;
  352. for (; y < img_height; y++, vcapbuf += stride_cap)
  353. memcpy(vcapbuf, tpg->contrast_line[p], img_width * pixsize);
  354. return 0;
  355. }
  356. static void vivid_fillbuff(struct vivid_dev *dev, struct vivid_buffer *buf)
  357. {
  358. unsigned factor = V4L2_FIELD_HAS_T_OR_B(dev->field_cap) ? 2 : 1;
  359. unsigned line_height = 16 / factor;
  360. bool is_tv = vivid_is_sdtv_cap(dev);
  361. bool is_60hz = is_tv && (dev->std_cap & V4L2_STD_525_60);
  362. unsigned p;
  363. int line = 1;
  364. u8 *basep[TPG_MAX_PLANES][2];
  365. unsigned ms;
  366. char str[100];
  367. s32 gain;
  368. bool is_loop = false;
  369. if (dev->loop_video && dev->can_loop_video &&
  370. ((vivid_is_svid_cap(dev) && !VIVID_INVALID_SIGNAL(dev->std_signal_mode)) ||
  371. (vivid_is_hdmi_cap(dev) && !VIVID_INVALID_SIGNAL(dev->dv_timings_signal_mode))))
  372. is_loop = true;
  373. buf->vb.v4l2_buf.sequence = dev->vid_cap_seq_count;
  374. /*
  375. * Take the timestamp now if the timestamp source is set to
  376. * "Start of Exposure".
  377. */
  378. if (dev->tstamp_src_is_soe)
  379. v4l2_get_timestamp(&buf->vb.v4l2_buf.timestamp);
  380. if (dev->field_cap == V4L2_FIELD_ALTERNATE) {
  381. /*
  382. * 60 Hz standards start with the bottom field, 50 Hz standards
  383. * with the top field. So if the 0-based seq_count is even,
  384. * then the field is TOP for 50 Hz and BOTTOM for 60 Hz
  385. * standards.
  386. */
  387. buf->vb.v4l2_buf.field = ((dev->vid_cap_seq_count & 1) ^ is_60hz) ?
  388. V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM;
  389. /*
  390. * The sequence counter counts frames, not fields. So divide
  391. * by two.
  392. */
  393. buf->vb.v4l2_buf.sequence /= 2;
  394. } else {
  395. buf->vb.v4l2_buf.field = dev->field_cap;
  396. }
  397. tpg_s_field(&dev->tpg, buf->vb.v4l2_buf.field);
  398. tpg_s_perc_fill_blank(&dev->tpg, dev->must_blank[buf->vb.v4l2_buf.index]);
  399. vivid_precalc_copy_rects(dev);
  400. for (p = 0; p < tpg_g_planes(&dev->tpg); p++) {
  401. void *vbuf = vb2_plane_vaddr(&buf->vb, p);
  402. /*
  403. * The first plane of a multiplanar format has a non-zero
  404. * data_offset. This helps testing whether the application
  405. * correctly supports non-zero data offsets.
  406. */
  407. if (dev->fmt_cap->data_offset[p]) {
  408. memset(vbuf, dev->fmt_cap->data_offset[p] & 0xff,
  409. dev->fmt_cap->data_offset[p]);
  410. vbuf += dev->fmt_cap->data_offset[p];
  411. }
  412. tpg_calc_text_basep(&dev->tpg, basep, p, vbuf);
  413. if (!is_loop || vivid_copy_buffer(dev, p, vbuf, buf))
  414. tpg_fillbuffer(&dev->tpg, vivid_get_std_cap(dev), p, vbuf);
  415. }
  416. dev->must_blank[buf->vb.v4l2_buf.index] = false;
  417. /* Updates stream time, only update at the start of a new frame. */
  418. if (dev->field_cap != V4L2_FIELD_ALTERNATE || (buf->vb.v4l2_buf.sequence & 1) == 0)
  419. dev->ms_vid_cap = jiffies_to_msecs(jiffies - dev->jiffies_vid_cap);
  420. ms = dev->ms_vid_cap;
  421. if (dev->osd_mode <= 1) {
  422. snprintf(str, sizeof(str), " %02d:%02d:%02d:%03d %u%s",
  423. (ms / (60 * 60 * 1000)) % 24,
  424. (ms / (60 * 1000)) % 60,
  425. (ms / 1000) % 60,
  426. ms % 1000,
  427. buf->vb.v4l2_buf.sequence,
  428. (dev->field_cap == V4L2_FIELD_ALTERNATE) ?
  429. (buf->vb.v4l2_buf.field == V4L2_FIELD_TOP ?
  430. " top" : " bottom") : "");
  431. tpg_gen_text(&dev->tpg, basep, line++ * line_height, 16, str);
  432. }
  433. if (dev->osd_mode == 0) {
  434. snprintf(str, sizeof(str), " %dx%d, input %d ",
  435. dev->src_rect.width, dev->src_rect.height, dev->input);
  436. tpg_gen_text(&dev->tpg, basep, line++ * line_height, 16, str);
  437. gain = v4l2_ctrl_g_ctrl(dev->gain);
  438. mutex_lock(dev->ctrl_hdl_user_vid.lock);
  439. snprintf(str, sizeof(str),
  440. " brightness %3d, contrast %3d, saturation %3d, hue %d ",
  441. dev->brightness->cur.val,
  442. dev->contrast->cur.val,
  443. dev->saturation->cur.val,
  444. dev->hue->cur.val);
  445. tpg_gen_text(&dev->tpg, basep, line++ * line_height, 16, str);
  446. snprintf(str, sizeof(str),
  447. " autogain %d, gain %3d, alpha 0x%02x ",
  448. dev->autogain->cur.val, gain, dev->alpha->cur.val);
  449. mutex_unlock(dev->ctrl_hdl_user_vid.lock);
  450. tpg_gen_text(&dev->tpg, basep, line++ * line_height, 16, str);
  451. mutex_lock(dev->ctrl_hdl_user_aud.lock);
  452. snprintf(str, sizeof(str),
  453. " volume %3d, mute %d ",
  454. dev->volume->cur.val, dev->mute->cur.val);
  455. mutex_unlock(dev->ctrl_hdl_user_aud.lock);
  456. tpg_gen_text(&dev->tpg, basep, line++ * line_height, 16, str);
  457. mutex_lock(dev->ctrl_hdl_user_gen.lock);
  458. snprintf(str, sizeof(str), " int32 %d, int64 %lld, bitmask %08x ",
  459. dev->int32->cur.val,
  460. *dev->int64->p_cur.p_s64,
  461. dev->bitmask->cur.val);
  462. tpg_gen_text(&dev->tpg, basep, line++ * line_height, 16, str);
  463. snprintf(str, sizeof(str), " boolean %d, menu %s, string \"%s\" ",
  464. dev->boolean->cur.val,
  465. dev->menu->qmenu[dev->menu->cur.val],
  466. dev->string->p_cur.p_char);
  467. tpg_gen_text(&dev->tpg, basep, line++ * line_height, 16, str);
  468. snprintf(str, sizeof(str), " integer_menu %lld, value %d ",
  469. dev->int_menu->qmenu_int[dev->int_menu->cur.val],
  470. dev->int_menu->cur.val);
  471. mutex_unlock(dev->ctrl_hdl_user_gen.lock);
  472. tpg_gen_text(&dev->tpg, basep, line++ * line_height, 16, str);
  473. if (dev->button_pressed) {
  474. dev->button_pressed--;
  475. snprintf(str, sizeof(str), " button pressed!");
  476. tpg_gen_text(&dev->tpg, basep, line++ * line_height, 16, str);
  477. }
  478. }
  479. /*
  480. * If "End of Frame" is specified at the timestamp source, then take
  481. * the timestamp now.
  482. */
  483. if (!dev->tstamp_src_is_soe)
  484. v4l2_get_timestamp(&buf->vb.v4l2_buf.timestamp);
  485. buf->vb.v4l2_buf.timestamp.tv_sec += dev->time_wrap_offset;
  486. }
  487. /*
  488. * Return true if this pixel coordinate is a valid video pixel.
  489. */
  490. static bool valid_pix(struct vivid_dev *dev, int win_y, int win_x, int fb_y, int fb_x)
  491. {
  492. int i;
  493. if (dev->bitmap_cap) {
  494. /*
  495. * Only if the corresponding bit in the bitmap is set can
  496. * the video pixel be shown. Coordinates are relative to
  497. * the overlay window set by VIDIOC_S_FMT.
  498. */
  499. const u8 *p = dev->bitmap_cap;
  500. unsigned stride = (dev->compose_cap.width + 7) / 8;
  501. if (!(p[stride * win_y + win_x / 8] & (1 << (win_x & 7))))
  502. return false;
  503. }
  504. for (i = 0; i < dev->clipcount_cap; i++) {
  505. /*
  506. * Only if the framebuffer coordinate is not in any of the
  507. * clip rectangles will be video pixel be shown.
  508. */
  509. struct v4l2_rect *r = &dev->clips_cap[i].c;
  510. if (fb_y >= r->top && fb_y < r->top + r->height &&
  511. fb_x >= r->left && fb_x < r->left + r->width)
  512. return false;
  513. }
  514. return true;
  515. }
  516. /*
  517. * Draw the image into the overlay buffer.
  518. * Note that the combination of overlay and multiplanar is not supported.
  519. */
  520. static void vivid_overlay(struct vivid_dev *dev, struct vivid_buffer *buf)
  521. {
  522. struct tpg_data *tpg = &dev->tpg;
  523. unsigned pixsize = tpg_g_twopixelsize(tpg, 0) / 2;
  524. void *vbase = dev->fb_vbase_cap;
  525. void *vbuf = vb2_plane_vaddr(&buf->vb, 0);
  526. unsigned img_width = dev->compose_cap.width;
  527. unsigned img_height = dev->compose_cap.height;
  528. unsigned stride = tpg->bytesperline[0];
  529. /* if quick is true, then valid_pix() doesn't have to be called */
  530. bool quick = dev->bitmap_cap == NULL && dev->clipcount_cap == 0;
  531. int x, y, w, out_x = 0;
  532. if ((dev->overlay_cap_field == V4L2_FIELD_TOP ||
  533. dev->overlay_cap_field == V4L2_FIELD_BOTTOM) &&
  534. dev->overlay_cap_field != buf->vb.v4l2_buf.field)
  535. return;
  536. vbuf += dev->compose_cap.left * pixsize + dev->compose_cap.top * stride;
  537. x = dev->overlay_cap_left;
  538. w = img_width;
  539. if (x < 0) {
  540. out_x = -x;
  541. w = w - out_x;
  542. x = 0;
  543. } else {
  544. w = dev->fb_cap.fmt.width - x;
  545. if (w > img_width)
  546. w = img_width;
  547. }
  548. if (w <= 0)
  549. return;
  550. if (dev->overlay_cap_top >= 0)
  551. vbase += dev->overlay_cap_top * dev->fb_cap.fmt.bytesperline;
  552. for (y = dev->overlay_cap_top;
  553. y < dev->overlay_cap_top + (int)img_height;
  554. y++, vbuf += stride) {
  555. int px;
  556. if (y < 0 || y > dev->fb_cap.fmt.height)
  557. continue;
  558. if (quick) {
  559. memcpy(vbase + x * pixsize,
  560. vbuf + out_x * pixsize, w * pixsize);
  561. vbase += dev->fb_cap.fmt.bytesperline;
  562. continue;
  563. }
  564. for (px = 0; px < w; px++) {
  565. if (!valid_pix(dev, y - dev->overlay_cap_top,
  566. px + out_x, y, px + x))
  567. continue;
  568. memcpy(vbase + (px + x) * pixsize,
  569. vbuf + (px + out_x) * pixsize,
  570. pixsize);
  571. }
  572. vbase += dev->fb_cap.fmt.bytesperline;
  573. }
  574. }
  575. static void vivid_thread_vid_cap_tick(struct vivid_dev *dev, int dropped_bufs)
  576. {
  577. struct vivid_buffer *vid_cap_buf = NULL;
  578. struct vivid_buffer *vbi_cap_buf = NULL;
  579. dprintk(dev, 1, "Video Capture Thread Tick\n");
  580. while (dropped_bufs-- > 1)
  581. tpg_update_mv_count(&dev->tpg,
  582. dev->field_cap == V4L2_FIELD_NONE ||
  583. dev->field_cap == V4L2_FIELD_ALTERNATE);
  584. /* Drop a certain percentage of buffers. */
  585. if (dev->perc_dropped_buffers &&
  586. prandom_u32_max(100) < dev->perc_dropped_buffers)
  587. goto update_mv;
  588. spin_lock(&dev->slock);
  589. if (!list_empty(&dev->vid_cap_active)) {
  590. vid_cap_buf = list_entry(dev->vid_cap_active.next, struct vivid_buffer, list);
  591. list_del(&vid_cap_buf->list);
  592. }
  593. if (!list_empty(&dev->vbi_cap_active)) {
  594. if (dev->field_cap != V4L2_FIELD_ALTERNATE ||
  595. (dev->vbi_cap_seq_count & 1)) {
  596. vbi_cap_buf = list_entry(dev->vbi_cap_active.next,
  597. struct vivid_buffer, list);
  598. list_del(&vbi_cap_buf->list);
  599. }
  600. }
  601. spin_unlock(&dev->slock);
  602. if (!vid_cap_buf && !vbi_cap_buf)
  603. goto update_mv;
  604. if (vid_cap_buf) {
  605. /* Fill buffer */
  606. vivid_fillbuff(dev, vid_cap_buf);
  607. dprintk(dev, 1, "filled buffer %d\n",
  608. vid_cap_buf->vb.v4l2_buf.index);
  609. /* Handle overlay */
  610. if (dev->overlay_cap_owner && dev->fb_cap.base &&
  611. dev->fb_cap.fmt.pixelformat == dev->fmt_cap->fourcc)
  612. vivid_overlay(dev, vid_cap_buf);
  613. vb2_buffer_done(&vid_cap_buf->vb, dev->dqbuf_error ?
  614. VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
  615. dprintk(dev, 2, "vid_cap buffer %d done\n",
  616. vid_cap_buf->vb.v4l2_buf.index);
  617. }
  618. if (vbi_cap_buf) {
  619. if (dev->stream_sliced_vbi_cap)
  620. vivid_sliced_vbi_cap_process(dev, vbi_cap_buf);
  621. else
  622. vivid_raw_vbi_cap_process(dev, vbi_cap_buf);
  623. vb2_buffer_done(&vbi_cap_buf->vb, dev->dqbuf_error ?
  624. VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
  625. dprintk(dev, 2, "vbi_cap %d done\n",
  626. vbi_cap_buf->vb.v4l2_buf.index);
  627. }
  628. dev->dqbuf_error = false;
  629. update_mv:
  630. /* Update the test pattern movement counters */
  631. tpg_update_mv_count(&dev->tpg, dev->field_cap == V4L2_FIELD_NONE ||
  632. dev->field_cap == V4L2_FIELD_ALTERNATE);
  633. }
  634. static int vivid_thread_vid_cap(void *data)
  635. {
  636. struct vivid_dev *dev = data;
  637. u64 numerators_since_start;
  638. u64 buffers_since_start;
  639. u64 next_jiffies_since_start;
  640. unsigned long jiffies_since_start;
  641. unsigned long cur_jiffies;
  642. unsigned wait_jiffies;
  643. unsigned numerator;
  644. unsigned denominator;
  645. int dropped_bufs;
  646. dprintk(dev, 1, "Video Capture Thread Start\n");
  647. set_freezable();
  648. /* Resets frame counters */
  649. dev->cap_seq_offset = 0;
  650. dev->cap_seq_count = 0;
  651. dev->cap_seq_resync = false;
  652. dev->jiffies_vid_cap = jiffies;
  653. for (;;) {
  654. try_to_freeze();
  655. if (kthread_should_stop())
  656. break;
  657. mutex_lock(&dev->mutex);
  658. cur_jiffies = jiffies;
  659. if (dev->cap_seq_resync) {
  660. dev->jiffies_vid_cap = cur_jiffies;
  661. dev->cap_seq_offset = dev->cap_seq_count + 1;
  662. dev->cap_seq_count = 0;
  663. dev->cap_seq_resync = false;
  664. }
  665. numerator = dev->timeperframe_vid_cap.numerator;
  666. denominator = dev->timeperframe_vid_cap.denominator;
  667. if (dev->field_cap == V4L2_FIELD_ALTERNATE)
  668. denominator *= 2;
  669. /* Calculate the number of jiffies since we started streaming */
  670. jiffies_since_start = cur_jiffies - dev->jiffies_vid_cap;
  671. /* Get the number of buffers streamed since the start */
  672. buffers_since_start = (u64)jiffies_since_start * denominator +
  673. (HZ * numerator) / 2;
  674. do_div(buffers_since_start, HZ * numerator);
  675. /*
  676. * After more than 0xf0000000 (rounded down to a multiple of
  677. * 'jiffies-per-day' to ease jiffies_to_msecs calculation)
  678. * jiffies have passed since we started streaming reset the
  679. * counters and keep track of the sequence offset.
  680. */
  681. if (jiffies_since_start > JIFFIES_RESYNC) {
  682. dev->jiffies_vid_cap = cur_jiffies;
  683. dev->cap_seq_offset = buffers_since_start;
  684. buffers_since_start = 0;
  685. }
  686. dropped_bufs = buffers_since_start + dev->cap_seq_offset - dev->cap_seq_count;
  687. dev->cap_seq_count = buffers_since_start + dev->cap_seq_offset;
  688. dev->vid_cap_seq_count = dev->cap_seq_count - dev->vid_cap_seq_start;
  689. dev->vbi_cap_seq_count = dev->cap_seq_count - dev->vbi_cap_seq_start;
  690. vivid_thread_vid_cap_tick(dev, dropped_bufs);
  691. /*
  692. * Calculate the number of 'numerators' streamed since we started,
  693. * including the current buffer.
  694. */
  695. numerators_since_start = ++buffers_since_start * numerator;
  696. /* And the number of jiffies since we started */
  697. jiffies_since_start = jiffies - dev->jiffies_vid_cap;
  698. mutex_unlock(&dev->mutex);
  699. /*
  700. * Calculate when that next buffer is supposed to start
  701. * in jiffies since we started streaming.
  702. */
  703. next_jiffies_since_start = numerators_since_start * HZ +
  704. denominator / 2;
  705. do_div(next_jiffies_since_start, denominator);
  706. /* If it is in the past, then just schedule asap */
  707. if (next_jiffies_since_start < jiffies_since_start)
  708. next_jiffies_since_start = jiffies_since_start;
  709. wait_jiffies = next_jiffies_since_start - jiffies_since_start;
  710. schedule_timeout_interruptible(wait_jiffies ? wait_jiffies : 1);
  711. }
  712. dprintk(dev, 1, "Video Capture Thread End\n");
  713. return 0;
  714. }
  715. static void vivid_grab_controls(struct vivid_dev *dev, bool grab)
  716. {
  717. v4l2_ctrl_grab(dev->ctrl_has_crop_cap, grab);
  718. v4l2_ctrl_grab(dev->ctrl_has_compose_cap, grab);
  719. v4l2_ctrl_grab(dev->ctrl_has_scaler_cap, grab);
  720. }
  721. int vivid_start_generating_vid_cap(struct vivid_dev *dev, bool *pstreaming)
  722. {
  723. dprintk(dev, 1, "%s\n", __func__);
  724. if (dev->kthread_vid_cap) {
  725. u32 seq_count = dev->cap_seq_count + dev->seq_wrap * 128;
  726. if (pstreaming == &dev->vid_cap_streaming)
  727. dev->vid_cap_seq_start = seq_count;
  728. else
  729. dev->vbi_cap_seq_start = seq_count;
  730. *pstreaming = true;
  731. return 0;
  732. }
  733. /* Resets frame counters */
  734. tpg_init_mv_count(&dev->tpg);
  735. dev->vid_cap_seq_start = dev->seq_wrap * 128;
  736. dev->vbi_cap_seq_start = dev->seq_wrap * 128;
  737. dev->kthread_vid_cap = kthread_run(vivid_thread_vid_cap, dev,
  738. "%s-vid-cap", dev->v4l2_dev.name);
  739. if (IS_ERR(dev->kthread_vid_cap)) {
  740. v4l2_err(&dev->v4l2_dev, "kernel_thread() failed\n");
  741. return PTR_ERR(dev->kthread_vid_cap);
  742. }
  743. *pstreaming = true;
  744. vivid_grab_controls(dev, true);
  745. dprintk(dev, 1, "returning from %s\n", __func__);
  746. return 0;
  747. }
  748. void vivid_stop_generating_vid_cap(struct vivid_dev *dev, bool *pstreaming)
  749. {
  750. dprintk(dev, 1, "%s\n", __func__);
  751. if (dev->kthread_vid_cap == NULL)
  752. return;
  753. *pstreaming = false;
  754. if (pstreaming == &dev->vid_cap_streaming) {
  755. /* Release all active buffers */
  756. while (!list_empty(&dev->vid_cap_active)) {
  757. struct vivid_buffer *buf;
  758. buf = list_entry(dev->vid_cap_active.next,
  759. struct vivid_buffer, list);
  760. list_del(&buf->list);
  761. vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
  762. dprintk(dev, 2, "vid_cap buffer %d done\n",
  763. buf->vb.v4l2_buf.index);
  764. }
  765. }
  766. if (pstreaming == &dev->vbi_cap_streaming) {
  767. while (!list_empty(&dev->vbi_cap_active)) {
  768. struct vivid_buffer *buf;
  769. buf = list_entry(dev->vbi_cap_active.next,
  770. struct vivid_buffer, list);
  771. list_del(&buf->list);
  772. vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
  773. dprintk(dev, 2, "vbi_cap buffer %d done\n",
  774. buf->vb.v4l2_buf.index);
  775. }
  776. }
  777. if (dev->vid_cap_streaming || dev->vbi_cap_streaming)
  778. return;
  779. /* shutdown control thread */
  780. vivid_grab_controls(dev, false);
  781. mutex_unlock(&dev->mutex);
  782. kthread_stop(dev->kthread_vid_cap);
  783. dev->kthread_vid_cap = NULL;
  784. mutex_lock(&dev->mutex);
  785. }