vivid-vid-common.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. /*
  2. * vivid-vid-common.c - common video 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/errno.h>
  20. #include <linux/kernel.h>
  21. #include <linux/sched.h>
  22. #include <linux/videodev2.h>
  23. #include <linux/v4l2-dv-timings.h>
  24. #include <media/v4l2-common.h>
  25. #include <media/v4l2-event.h>
  26. #include <media/v4l2-dv-timings.h>
  27. #include "vivid-core.h"
  28. #include "vivid-vid-common.h"
  29. const struct v4l2_dv_timings_cap vivid_dv_timings_cap = {
  30. .type = V4L2_DV_BT_656_1120,
  31. /* keep this initialization for compatibility with GCC < 4.4.6 */
  32. .reserved = { 0 },
  33. V4L2_INIT_BT_TIMINGS(0, MAX_WIDTH, 0, MAX_HEIGHT, 25000000, 600000000,
  34. V4L2_DV_BT_STD_CEA861 | V4L2_DV_BT_STD_DMT,
  35. V4L2_DV_BT_CAP_PROGRESSIVE | V4L2_DV_BT_CAP_INTERLACED)
  36. };
  37. /* ------------------------------------------------------------------
  38. Basic structures
  39. ------------------------------------------------------------------*/
  40. struct vivid_fmt vivid_formats[] = {
  41. {
  42. .name = "4:2:2, packed, YUYV",
  43. .fourcc = V4L2_PIX_FMT_YUYV,
  44. .depth = 16,
  45. .is_yuv = true,
  46. .planes = 1,
  47. .data_offset = { PLANE0_DATA_OFFSET, 0 },
  48. },
  49. {
  50. .name = "4:2:2, packed, UYVY",
  51. .fourcc = V4L2_PIX_FMT_UYVY,
  52. .depth = 16,
  53. .is_yuv = true,
  54. .planes = 1,
  55. },
  56. {
  57. .name = "4:2:2, packed, YVYU",
  58. .fourcc = V4L2_PIX_FMT_YVYU,
  59. .depth = 16,
  60. .is_yuv = true,
  61. .planes = 1,
  62. },
  63. {
  64. .name = "4:2:2, packed, VYUY",
  65. .fourcc = V4L2_PIX_FMT_VYUY,
  66. .depth = 16,
  67. .is_yuv = true,
  68. .planes = 1,
  69. },
  70. {
  71. .name = "RGB565 (LE)",
  72. .fourcc = V4L2_PIX_FMT_RGB565, /* gggbbbbb rrrrrggg */
  73. .depth = 16,
  74. .planes = 1,
  75. .can_do_overlay = true,
  76. },
  77. {
  78. .name = "RGB565 (BE)",
  79. .fourcc = V4L2_PIX_FMT_RGB565X, /* rrrrrggg gggbbbbb */
  80. .depth = 16,
  81. .planes = 1,
  82. .can_do_overlay = true,
  83. },
  84. {
  85. .name = "RGB555 (LE)",
  86. .fourcc = V4L2_PIX_FMT_RGB555, /* gggbbbbb arrrrrgg */
  87. .depth = 16,
  88. .planes = 1,
  89. .can_do_overlay = true,
  90. },
  91. {
  92. .name = "XRGB555 (LE)",
  93. .fourcc = V4L2_PIX_FMT_XRGB555, /* gggbbbbb arrrrrgg */
  94. .depth = 16,
  95. .planes = 1,
  96. .can_do_overlay = true,
  97. },
  98. {
  99. .name = "ARGB555 (LE)",
  100. .fourcc = V4L2_PIX_FMT_ARGB555, /* gggbbbbb arrrrrgg */
  101. .depth = 16,
  102. .planes = 1,
  103. .can_do_overlay = true,
  104. .alpha_mask = 0x8000,
  105. },
  106. {
  107. .name = "RGB555 (BE)",
  108. .fourcc = V4L2_PIX_FMT_RGB555X, /* arrrrrgg gggbbbbb */
  109. .depth = 16,
  110. .planes = 1,
  111. .can_do_overlay = true,
  112. },
  113. {
  114. .name = "RGB24 (LE)",
  115. .fourcc = V4L2_PIX_FMT_RGB24, /* rgb */
  116. .depth = 24,
  117. .planes = 1,
  118. },
  119. {
  120. .name = "RGB24 (BE)",
  121. .fourcc = V4L2_PIX_FMT_BGR24, /* bgr */
  122. .depth = 24,
  123. .planes = 1,
  124. },
  125. {
  126. .name = "RGB32 (LE)",
  127. .fourcc = V4L2_PIX_FMT_RGB32, /* argb */
  128. .depth = 32,
  129. .planes = 1,
  130. },
  131. {
  132. .name = "RGB32 (BE)",
  133. .fourcc = V4L2_PIX_FMT_BGR32, /* bgra */
  134. .depth = 32,
  135. .planes = 1,
  136. },
  137. {
  138. .name = "XRGB32 (LE)",
  139. .fourcc = V4L2_PIX_FMT_XRGB32, /* argb */
  140. .depth = 32,
  141. .planes = 1,
  142. },
  143. {
  144. .name = "XRGB32 (BE)",
  145. .fourcc = V4L2_PIX_FMT_XBGR32, /* bgra */
  146. .depth = 32,
  147. .planes = 1,
  148. },
  149. {
  150. .name = "ARGB32 (LE)",
  151. .fourcc = V4L2_PIX_FMT_ARGB32, /* argb */
  152. .depth = 32,
  153. .planes = 1,
  154. .alpha_mask = 0x000000ff,
  155. },
  156. {
  157. .name = "ARGB32 (BE)",
  158. .fourcc = V4L2_PIX_FMT_ABGR32, /* bgra */
  159. .depth = 32,
  160. .planes = 1,
  161. .alpha_mask = 0xff000000,
  162. },
  163. {
  164. .name = "4:2:2, planar, YUV",
  165. .fourcc = V4L2_PIX_FMT_NV16M,
  166. .depth = 8,
  167. .is_yuv = true,
  168. .planes = 2,
  169. .data_offset = { PLANE0_DATA_OFFSET, 0 },
  170. },
  171. {
  172. .name = "4:2:2, planar, YVU",
  173. .fourcc = V4L2_PIX_FMT_NV61M,
  174. .depth = 8,
  175. .is_yuv = true,
  176. .planes = 2,
  177. .data_offset = { 0, PLANE0_DATA_OFFSET },
  178. },
  179. };
  180. /* There are 2 multiplanar formats in the list */
  181. #define VIVID_MPLANAR_FORMATS 2
  182. const struct vivid_fmt *vivid_get_format(struct vivid_dev *dev, u32 pixelformat)
  183. {
  184. const struct vivid_fmt *fmt;
  185. unsigned k;
  186. for (k = 0; k < ARRAY_SIZE(vivid_formats); k++) {
  187. fmt = &vivid_formats[k];
  188. if (fmt->fourcc == pixelformat)
  189. if (fmt->planes == 1 || dev->multiplanar)
  190. return fmt;
  191. }
  192. return NULL;
  193. }
  194. bool vivid_vid_can_loop(struct vivid_dev *dev)
  195. {
  196. if (dev->src_rect.width != dev->sink_rect.width ||
  197. dev->src_rect.height != dev->sink_rect.height)
  198. return false;
  199. if (dev->fmt_cap->fourcc != dev->fmt_out->fourcc)
  200. return false;
  201. if (dev->field_cap != dev->field_out)
  202. return false;
  203. if (vivid_is_svid_cap(dev) && vivid_is_svid_out(dev)) {
  204. if (!(dev->std_cap & V4L2_STD_525_60) !=
  205. !(dev->std_out & V4L2_STD_525_60))
  206. return false;
  207. return true;
  208. }
  209. if (vivid_is_hdmi_cap(dev) && vivid_is_hdmi_out(dev))
  210. return true;
  211. return false;
  212. }
  213. void vivid_send_source_change(struct vivid_dev *dev, unsigned type)
  214. {
  215. struct v4l2_event ev = {
  216. .type = V4L2_EVENT_SOURCE_CHANGE,
  217. .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION,
  218. };
  219. unsigned i;
  220. for (i = 0; i < dev->num_inputs; i++) {
  221. ev.id = i;
  222. if (dev->input_type[i] == type) {
  223. if (video_is_registered(&dev->vid_cap_dev) && dev->has_vid_cap)
  224. v4l2_event_queue(&dev->vid_cap_dev, &ev);
  225. if (video_is_registered(&dev->vbi_cap_dev) && dev->has_vbi_cap)
  226. v4l2_event_queue(&dev->vbi_cap_dev, &ev);
  227. }
  228. }
  229. }
  230. /*
  231. * Conversion function that converts a single-planar format to a
  232. * single-plane multiplanar format.
  233. */
  234. void fmt_sp2mp(const struct v4l2_format *sp_fmt, struct v4l2_format *mp_fmt)
  235. {
  236. struct v4l2_pix_format_mplane *mp = &mp_fmt->fmt.pix_mp;
  237. struct v4l2_plane_pix_format *ppix = &mp->plane_fmt[0];
  238. const struct v4l2_pix_format *pix = &sp_fmt->fmt.pix;
  239. bool is_out = sp_fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT;
  240. memset(mp->reserved, 0, sizeof(mp->reserved));
  241. mp_fmt->type = is_out ? V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE :
  242. V4L2_CAP_VIDEO_CAPTURE_MPLANE;
  243. mp->width = pix->width;
  244. mp->height = pix->height;
  245. mp->pixelformat = pix->pixelformat;
  246. mp->field = pix->field;
  247. mp->colorspace = pix->colorspace;
  248. mp->num_planes = 1;
  249. mp->flags = pix->flags;
  250. ppix->sizeimage = pix->sizeimage;
  251. ppix->bytesperline = pix->bytesperline;
  252. memset(ppix->reserved, 0, sizeof(ppix->reserved));
  253. }
  254. int fmt_sp2mp_func(struct file *file, void *priv,
  255. struct v4l2_format *f, fmtfunc func)
  256. {
  257. struct v4l2_format fmt;
  258. struct v4l2_pix_format_mplane *mp = &fmt.fmt.pix_mp;
  259. struct v4l2_plane_pix_format *ppix = &mp->plane_fmt[0];
  260. struct v4l2_pix_format *pix = &f->fmt.pix;
  261. int ret;
  262. /* Converts to a mplane format */
  263. fmt_sp2mp(f, &fmt);
  264. /* Passes it to the generic mplane format function */
  265. ret = func(file, priv, &fmt);
  266. /* Copies back the mplane data to the single plane format */
  267. pix->width = mp->width;
  268. pix->height = mp->height;
  269. pix->pixelformat = mp->pixelformat;
  270. pix->field = mp->field;
  271. pix->colorspace = mp->colorspace;
  272. pix->sizeimage = ppix->sizeimage;
  273. pix->bytesperline = ppix->bytesperline;
  274. pix->flags = mp->flags;
  275. return ret;
  276. }
  277. /* v4l2_rect helper function: copy the width/height values */
  278. void rect_set_size_to(struct v4l2_rect *r, const struct v4l2_rect *size)
  279. {
  280. r->width = size->width;
  281. r->height = size->height;
  282. }
  283. /* v4l2_rect helper function: width and height of r should be >= min_size */
  284. void rect_set_min_size(struct v4l2_rect *r, const struct v4l2_rect *min_size)
  285. {
  286. if (r->width < min_size->width)
  287. r->width = min_size->width;
  288. if (r->height < min_size->height)
  289. r->height = min_size->height;
  290. }
  291. /* v4l2_rect helper function: width and height of r should be <= max_size */
  292. void rect_set_max_size(struct v4l2_rect *r, const struct v4l2_rect *max_size)
  293. {
  294. if (r->width > max_size->width)
  295. r->width = max_size->width;
  296. if (r->height > max_size->height)
  297. r->height = max_size->height;
  298. }
  299. /* v4l2_rect helper function: r should be inside boundary */
  300. void rect_map_inside(struct v4l2_rect *r, const struct v4l2_rect *boundary)
  301. {
  302. rect_set_max_size(r, boundary);
  303. if (r->left < boundary->left)
  304. r->left = boundary->left;
  305. if (r->top < boundary->top)
  306. r->top = boundary->top;
  307. if (r->left + r->width > boundary->width)
  308. r->left = boundary->width - r->width;
  309. if (r->top + r->height > boundary->height)
  310. r->top = boundary->height - r->height;
  311. }
  312. /* v4l2_rect helper function: return true if r1 has the same size as r2 */
  313. bool rect_same_size(const struct v4l2_rect *r1, const struct v4l2_rect *r2)
  314. {
  315. return r1->width == r2->width && r1->height == r2->height;
  316. }
  317. /* v4l2_rect helper function: calculate the intersection of two rects */
  318. struct v4l2_rect rect_intersect(const struct v4l2_rect *a, const struct v4l2_rect *b)
  319. {
  320. struct v4l2_rect r;
  321. int right, bottom;
  322. r.top = max(a->top, b->top);
  323. r.left = max(a->left, b->left);
  324. bottom = min(a->top + a->height, b->top + b->height);
  325. right = min(a->left + a->width, b->left + b->width);
  326. r.height = max(0, bottom - r.top);
  327. r.width = max(0, right - r.left);
  328. return r;
  329. }
  330. /*
  331. * v4l2_rect helper function: scale rect r by to->width / from->width and
  332. * to->height / from->height.
  333. */
  334. void rect_scale(struct v4l2_rect *r, const struct v4l2_rect *from,
  335. const struct v4l2_rect *to)
  336. {
  337. if (from->width == 0 || from->height == 0) {
  338. r->left = r->top = r->width = r->height = 0;
  339. return;
  340. }
  341. r->left = (((r->left - from->left) * to->width) / from->width) & ~1;
  342. r->width = ((r->width * to->width) / from->width) & ~1;
  343. r->top = ((r->top - from->top) * to->height) / from->height;
  344. r->height = (r->height * to->height) / from->height;
  345. }
  346. bool rect_overlap(const struct v4l2_rect *r1, const struct v4l2_rect *r2)
  347. {
  348. /*
  349. * IF the left side of r1 is to the right of the right side of r2 OR
  350. * the left side of r2 is to the right of the right side of r1 THEN
  351. * they do not overlap.
  352. */
  353. if (r1->left >= r2->left + r2->width ||
  354. r2->left >= r1->left + r1->width)
  355. return false;
  356. /*
  357. * IF the top side of r1 is below the bottom of r2 OR
  358. * the top side of r2 is below the bottom of r1 THEN
  359. * they do not overlap.
  360. */
  361. if (r1->top >= r2->top + r2->height ||
  362. r2->top >= r1->top + r1->height)
  363. return false;
  364. return true;
  365. }
  366. int vivid_vid_adjust_sel(unsigned flags, struct v4l2_rect *r)
  367. {
  368. unsigned w = r->width;
  369. unsigned h = r->height;
  370. if (!(flags & V4L2_SEL_FLAG_LE)) {
  371. w++;
  372. h++;
  373. if (w < 2)
  374. w = 2;
  375. if (h < 2)
  376. h = 2;
  377. }
  378. if (!(flags & V4L2_SEL_FLAG_GE)) {
  379. if (w > MAX_WIDTH)
  380. w = MAX_WIDTH;
  381. if (h > MAX_HEIGHT)
  382. h = MAX_HEIGHT;
  383. }
  384. w = w & ~1;
  385. h = h & ~1;
  386. if (w < 2 || h < 2)
  387. return -ERANGE;
  388. if (w > MAX_WIDTH || h > MAX_HEIGHT)
  389. return -ERANGE;
  390. if (r->top < 0)
  391. r->top = 0;
  392. if (r->left < 0)
  393. r->left = 0;
  394. r->left &= ~1;
  395. r->top &= ~1;
  396. if (r->left + w > MAX_WIDTH)
  397. r->left = MAX_WIDTH - w;
  398. if (r->top + h > MAX_HEIGHT)
  399. r->top = MAX_HEIGHT - h;
  400. if ((flags & (V4L2_SEL_FLAG_GE | V4L2_SEL_FLAG_LE)) ==
  401. (V4L2_SEL_FLAG_GE | V4L2_SEL_FLAG_LE) &&
  402. (r->width != w || r->height != h))
  403. return -ERANGE;
  404. r->width = w;
  405. r->height = h;
  406. return 0;
  407. }
  408. int vivid_enum_fmt_vid(struct file *file, void *priv,
  409. struct v4l2_fmtdesc *f)
  410. {
  411. struct vivid_dev *dev = video_drvdata(file);
  412. const struct vivid_fmt *fmt;
  413. if (f->index >= ARRAY_SIZE(vivid_formats) -
  414. (dev->multiplanar ? 0 : VIVID_MPLANAR_FORMATS))
  415. return -EINVAL;
  416. fmt = &vivid_formats[f->index];
  417. strlcpy(f->description, fmt->name, sizeof(f->description));
  418. f->pixelformat = fmt->fourcc;
  419. return 0;
  420. }
  421. int vidioc_enum_fmt_vid_mplane(struct file *file, void *priv,
  422. struct v4l2_fmtdesc *f)
  423. {
  424. struct vivid_dev *dev = video_drvdata(file);
  425. if (!dev->multiplanar)
  426. return -ENOTTY;
  427. return vivid_enum_fmt_vid(file, priv, f);
  428. }
  429. int vidioc_enum_fmt_vid(struct file *file, void *priv,
  430. struct v4l2_fmtdesc *f)
  431. {
  432. struct vivid_dev *dev = video_drvdata(file);
  433. if (dev->multiplanar)
  434. return -ENOTTY;
  435. return vivid_enum_fmt_vid(file, priv, f);
  436. }
  437. int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
  438. {
  439. struct vivid_dev *dev = video_drvdata(file);
  440. struct video_device *vdev = video_devdata(file);
  441. if (vdev->vfl_dir == VFL_DIR_RX) {
  442. if (!vivid_is_sdtv_cap(dev))
  443. return -ENODATA;
  444. *id = dev->std_cap;
  445. } else {
  446. if (!vivid_is_svid_out(dev))
  447. return -ENODATA;
  448. *id = dev->std_out;
  449. }
  450. return 0;
  451. }
  452. int vidioc_g_dv_timings(struct file *file, void *_fh,
  453. struct v4l2_dv_timings *timings)
  454. {
  455. struct vivid_dev *dev = video_drvdata(file);
  456. struct video_device *vdev = video_devdata(file);
  457. if (vdev->vfl_dir == VFL_DIR_RX) {
  458. if (!vivid_is_hdmi_cap(dev))
  459. return -ENODATA;
  460. *timings = dev->dv_timings_cap;
  461. } else {
  462. if (!vivid_is_hdmi_out(dev))
  463. return -ENODATA;
  464. *timings = dev->dv_timings_out;
  465. }
  466. return 0;
  467. }
  468. int vidioc_enum_dv_timings(struct file *file, void *_fh,
  469. struct v4l2_enum_dv_timings *timings)
  470. {
  471. struct vivid_dev *dev = video_drvdata(file);
  472. struct video_device *vdev = video_devdata(file);
  473. if (vdev->vfl_dir == VFL_DIR_RX) {
  474. if (!vivid_is_hdmi_cap(dev))
  475. return -ENODATA;
  476. } else {
  477. if (!vivid_is_hdmi_out(dev))
  478. return -ENODATA;
  479. }
  480. return v4l2_enum_dv_timings_cap(timings, &vivid_dv_timings_cap,
  481. NULL, NULL);
  482. }
  483. int vidioc_dv_timings_cap(struct file *file, void *_fh,
  484. struct v4l2_dv_timings_cap *cap)
  485. {
  486. struct vivid_dev *dev = video_drvdata(file);
  487. struct video_device *vdev = video_devdata(file);
  488. if (vdev->vfl_dir == VFL_DIR_RX) {
  489. if (!vivid_is_hdmi_cap(dev))
  490. return -ENODATA;
  491. } else {
  492. if (!vivid_is_hdmi_out(dev))
  493. return -ENODATA;
  494. }
  495. *cap = vivid_dv_timings_cap;
  496. return 0;
  497. }
  498. int vidioc_g_edid(struct file *file, void *_fh,
  499. struct v4l2_edid *edid)
  500. {
  501. struct vivid_dev *dev = video_drvdata(file);
  502. struct video_device *vdev = video_devdata(file);
  503. memset(edid->reserved, 0, sizeof(edid->reserved));
  504. if (vdev->vfl_dir == VFL_DIR_RX) {
  505. if (edid->pad >= dev->num_inputs)
  506. return -EINVAL;
  507. if (dev->input_type[edid->pad] != HDMI)
  508. return -EINVAL;
  509. } else {
  510. if (edid->pad >= dev->num_outputs)
  511. return -EINVAL;
  512. if (dev->output_type[edid->pad] != HDMI)
  513. return -EINVAL;
  514. }
  515. if (edid->start_block == 0 && edid->blocks == 0) {
  516. edid->blocks = dev->edid_blocks;
  517. return 0;
  518. }
  519. if (dev->edid_blocks == 0)
  520. return -ENODATA;
  521. if (edid->start_block >= dev->edid_blocks)
  522. return -EINVAL;
  523. if (edid->start_block + edid->blocks > dev->edid_blocks)
  524. edid->blocks = dev->edid_blocks - edid->start_block;
  525. memcpy(edid->edid, dev->edid, edid->blocks * 128);
  526. return 0;
  527. }