vivid-vid-common.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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->ycbcr_enc = pix->ycbcr_enc;
  249. mp->quantization = pix->quantization;
  250. mp->num_planes = 1;
  251. mp->flags = pix->flags;
  252. ppix->sizeimage = pix->sizeimage;
  253. ppix->bytesperline = pix->bytesperline;
  254. memset(ppix->reserved, 0, sizeof(ppix->reserved));
  255. }
  256. int fmt_sp2mp_func(struct file *file, void *priv,
  257. struct v4l2_format *f, fmtfunc func)
  258. {
  259. struct v4l2_format fmt;
  260. struct v4l2_pix_format_mplane *mp = &fmt.fmt.pix_mp;
  261. struct v4l2_plane_pix_format *ppix = &mp->plane_fmt[0];
  262. struct v4l2_pix_format *pix = &f->fmt.pix;
  263. int ret;
  264. /* Converts to a mplane format */
  265. fmt_sp2mp(f, &fmt);
  266. /* Passes it to the generic mplane format function */
  267. ret = func(file, priv, &fmt);
  268. /* Copies back the mplane data to the single plane format */
  269. pix->width = mp->width;
  270. pix->height = mp->height;
  271. pix->pixelformat = mp->pixelformat;
  272. pix->field = mp->field;
  273. pix->colorspace = mp->colorspace;
  274. pix->ycbcr_enc = mp->ycbcr_enc;
  275. pix->quantization = mp->quantization;
  276. pix->sizeimage = ppix->sizeimage;
  277. pix->bytesperline = ppix->bytesperline;
  278. pix->flags = mp->flags;
  279. return ret;
  280. }
  281. /* v4l2_rect helper function: copy the width/height values */
  282. void rect_set_size_to(struct v4l2_rect *r, const struct v4l2_rect *size)
  283. {
  284. r->width = size->width;
  285. r->height = size->height;
  286. }
  287. /* v4l2_rect helper function: width and height of r should be >= min_size */
  288. void rect_set_min_size(struct v4l2_rect *r, const struct v4l2_rect *min_size)
  289. {
  290. if (r->width < min_size->width)
  291. r->width = min_size->width;
  292. if (r->height < min_size->height)
  293. r->height = min_size->height;
  294. }
  295. /* v4l2_rect helper function: width and height of r should be <= max_size */
  296. void rect_set_max_size(struct v4l2_rect *r, const struct v4l2_rect *max_size)
  297. {
  298. if (r->width > max_size->width)
  299. r->width = max_size->width;
  300. if (r->height > max_size->height)
  301. r->height = max_size->height;
  302. }
  303. /* v4l2_rect helper function: r should be inside boundary */
  304. void rect_map_inside(struct v4l2_rect *r, const struct v4l2_rect *boundary)
  305. {
  306. rect_set_max_size(r, boundary);
  307. if (r->left < boundary->left)
  308. r->left = boundary->left;
  309. if (r->top < boundary->top)
  310. r->top = boundary->top;
  311. if (r->left + r->width > boundary->width)
  312. r->left = boundary->width - r->width;
  313. if (r->top + r->height > boundary->height)
  314. r->top = boundary->height - r->height;
  315. }
  316. /* v4l2_rect helper function: return true if r1 has the same size as r2 */
  317. bool rect_same_size(const struct v4l2_rect *r1, const struct v4l2_rect *r2)
  318. {
  319. return r1->width == r2->width && r1->height == r2->height;
  320. }
  321. /* v4l2_rect helper function: calculate the intersection of two rects */
  322. struct v4l2_rect rect_intersect(const struct v4l2_rect *a, const struct v4l2_rect *b)
  323. {
  324. struct v4l2_rect r;
  325. int right, bottom;
  326. r.top = max(a->top, b->top);
  327. r.left = max(a->left, b->left);
  328. bottom = min(a->top + a->height, b->top + b->height);
  329. right = min(a->left + a->width, b->left + b->width);
  330. r.height = max(0, bottom - r.top);
  331. r.width = max(0, right - r.left);
  332. return r;
  333. }
  334. /*
  335. * v4l2_rect helper function: scale rect r by to->width / from->width and
  336. * to->height / from->height.
  337. */
  338. void rect_scale(struct v4l2_rect *r, const struct v4l2_rect *from,
  339. const struct v4l2_rect *to)
  340. {
  341. if (from->width == 0 || from->height == 0) {
  342. r->left = r->top = r->width = r->height = 0;
  343. return;
  344. }
  345. r->left = (((r->left - from->left) * to->width) / from->width) & ~1;
  346. r->width = ((r->width * to->width) / from->width) & ~1;
  347. r->top = ((r->top - from->top) * to->height) / from->height;
  348. r->height = (r->height * to->height) / from->height;
  349. }
  350. bool rect_overlap(const struct v4l2_rect *r1, const struct v4l2_rect *r2)
  351. {
  352. /*
  353. * IF the left side of r1 is to the right of the right side of r2 OR
  354. * the left side of r2 is to the right of the right side of r1 THEN
  355. * they do not overlap.
  356. */
  357. if (r1->left >= r2->left + r2->width ||
  358. r2->left >= r1->left + r1->width)
  359. return false;
  360. /*
  361. * IF the top side of r1 is below the bottom of r2 OR
  362. * the top side of r2 is below the bottom of r1 THEN
  363. * they do not overlap.
  364. */
  365. if (r1->top >= r2->top + r2->height ||
  366. r2->top >= r1->top + r1->height)
  367. return false;
  368. return true;
  369. }
  370. int vivid_vid_adjust_sel(unsigned flags, struct v4l2_rect *r)
  371. {
  372. unsigned w = r->width;
  373. unsigned h = r->height;
  374. if (!(flags & V4L2_SEL_FLAG_LE)) {
  375. w++;
  376. h++;
  377. if (w < 2)
  378. w = 2;
  379. if (h < 2)
  380. h = 2;
  381. }
  382. if (!(flags & V4L2_SEL_FLAG_GE)) {
  383. if (w > MAX_WIDTH)
  384. w = MAX_WIDTH;
  385. if (h > MAX_HEIGHT)
  386. h = MAX_HEIGHT;
  387. }
  388. w = w & ~1;
  389. h = h & ~1;
  390. if (w < 2 || h < 2)
  391. return -ERANGE;
  392. if (w > MAX_WIDTH || h > MAX_HEIGHT)
  393. return -ERANGE;
  394. if (r->top < 0)
  395. r->top = 0;
  396. if (r->left < 0)
  397. r->left = 0;
  398. r->left &= ~1;
  399. r->top &= ~1;
  400. if (r->left + w > MAX_WIDTH)
  401. r->left = MAX_WIDTH - w;
  402. if (r->top + h > MAX_HEIGHT)
  403. r->top = MAX_HEIGHT - h;
  404. if ((flags & (V4L2_SEL_FLAG_GE | V4L2_SEL_FLAG_LE)) ==
  405. (V4L2_SEL_FLAG_GE | V4L2_SEL_FLAG_LE) &&
  406. (r->width != w || r->height != h))
  407. return -ERANGE;
  408. r->width = w;
  409. r->height = h;
  410. return 0;
  411. }
  412. int vivid_enum_fmt_vid(struct file *file, void *priv,
  413. struct v4l2_fmtdesc *f)
  414. {
  415. struct vivid_dev *dev = video_drvdata(file);
  416. const struct vivid_fmt *fmt;
  417. if (f->index >= ARRAY_SIZE(vivid_formats) -
  418. (dev->multiplanar ? 0 : VIVID_MPLANAR_FORMATS))
  419. return -EINVAL;
  420. fmt = &vivid_formats[f->index];
  421. strlcpy(f->description, fmt->name, sizeof(f->description));
  422. f->pixelformat = fmt->fourcc;
  423. return 0;
  424. }
  425. int vidioc_enum_fmt_vid_mplane(struct file *file, void *priv,
  426. struct v4l2_fmtdesc *f)
  427. {
  428. struct vivid_dev *dev = video_drvdata(file);
  429. if (!dev->multiplanar)
  430. return -ENOTTY;
  431. return vivid_enum_fmt_vid(file, priv, f);
  432. }
  433. int vidioc_enum_fmt_vid(struct file *file, void *priv,
  434. struct v4l2_fmtdesc *f)
  435. {
  436. struct vivid_dev *dev = video_drvdata(file);
  437. if (dev->multiplanar)
  438. return -ENOTTY;
  439. return vivid_enum_fmt_vid(file, priv, f);
  440. }
  441. int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
  442. {
  443. struct vivid_dev *dev = video_drvdata(file);
  444. struct video_device *vdev = video_devdata(file);
  445. if (vdev->vfl_dir == VFL_DIR_RX) {
  446. if (!vivid_is_sdtv_cap(dev))
  447. return -ENODATA;
  448. *id = dev->std_cap;
  449. } else {
  450. if (!vivid_is_svid_out(dev))
  451. return -ENODATA;
  452. *id = dev->std_out;
  453. }
  454. return 0;
  455. }
  456. int vidioc_g_dv_timings(struct file *file, void *_fh,
  457. struct v4l2_dv_timings *timings)
  458. {
  459. struct vivid_dev *dev = video_drvdata(file);
  460. struct video_device *vdev = video_devdata(file);
  461. if (vdev->vfl_dir == VFL_DIR_RX) {
  462. if (!vivid_is_hdmi_cap(dev))
  463. return -ENODATA;
  464. *timings = dev->dv_timings_cap;
  465. } else {
  466. if (!vivid_is_hdmi_out(dev))
  467. return -ENODATA;
  468. *timings = dev->dv_timings_out;
  469. }
  470. return 0;
  471. }
  472. int vidioc_enum_dv_timings(struct file *file, void *_fh,
  473. struct v4l2_enum_dv_timings *timings)
  474. {
  475. struct vivid_dev *dev = video_drvdata(file);
  476. struct video_device *vdev = video_devdata(file);
  477. if (vdev->vfl_dir == VFL_DIR_RX) {
  478. if (!vivid_is_hdmi_cap(dev))
  479. return -ENODATA;
  480. } else {
  481. if (!vivid_is_hdmi_out(dev))
  482. return -ENODATA;
  483. }
  484. return v4l2_enum_dv_timings_cap(timings, &vivid_dv_timings_cap,
  485. NULL, NULL);
  486. }
  487. int vidioc_dv_timings_cap(struct file *file, void *_fh,
  488. struct v4l2_dv_timings_cap *cap)
  489. {
  490. struct vivid_dev *dev = video_drvdata(file);
  491. struct video_device *vdev = video_devdata(file);
  492. if (vdev->vfl_dir == VFL_DIR_RX) {
  493. if (!vivid_is_hdmi_cap(dev))
  494. return -ENODATA;
  495. } else {
  496. if (!vivid_is_hdmi_out(dev))
  497. return -ENODATA;
  498. }
  499. *cap = vivid_dv_timings_cap;
  500. return 0;
  501. }
  502. int vidioc_g_edid(struct file *file, void *_fh,
  503. struct v4l2_edid *edid)
  504. {
  505. struct vivid_dev *dev = video_drvdata(file);
  506. struct video_device *vdev = video_devdata(file);
  507. memset(edid->reserved, 0, sizeof(edid->reserved));
  508. if (vdev->vfl_dir == VFL_DIR_RX) {
  509. if (edid->pad >= dev->num_inputs)
  510. return -EINVAL;
  511. if (dev->input_type[edid->pad] != HDMI)
  512. return -EINVAL;
  513. } else {
  514. if (edid->pad >= dev->num_outputs)
  515. return -EINVAL;
  516. if (dev->output_type[edid->pad] != HDMI)
  517. return -EINVAL;
  518. }
  519. if (edid->start_block == 0 && edid->blocks == 0) {
  520. edid->blocks = dev->edid_blocks;
  521. return 0;
  522. }
  523. if (dev->edid_blocks == 0)
  524. return -ENODATA;
  525. if (edid->start_block >= dev->edid_blocks)
  526. return -EINVAL;
  527. if (edid->start_block + edid->blocks > dev->edid_blocks)
  528. edid->blocks = dev->edid_blocks - edid->start_block;
  529. memcpy(edid->edid, dev->edid, edid->blocks * 128);
  530. return 0;
  531. }