vivid-vid-common.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  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, 14000000, 775000000,
  34. V4L2_DV_BT_STD_CEA861 | V4L2_DV_BT_STD_DMT |
  35. V4L2_DV_BT_STD_CVT | V4L2_DV_BT_STD_GTF,
  36. V4L2_DV_BT_CAP_PROGRESSIVE | V4L2_DV_BT_CAP_INTERLACED)
  37. };
  38. /* ------------------------------------------------------------------
  39. Basic structures
  40. ------------------------------------------------------------------*/
  41. struct vivid_fmt vivid_formats[] = {
  42. {
  43. .fourcc = V4L2_PIX_FMT_YUYV,
  44. .vdownsampling = { 1 },
  45. .bit_depth = { 16 },
  46. .is_yuv = true,
  47. .planes = 1,
  48. .buffers = 1,
  49. .data_offset = { PLANE0_DATA_OFFSET },
  50. },
  51. {
  52. .fourcc = V4L2_PIX_FMT_UYVY,
  53. .vdownsampling = { 1 },
  54. .bit_depth = { 16 },
  55. .is_yuv = true,
  56. .planes = 1,
  57. .buffers = 1,
  58. },
  59. {
  60. .fourcc = V4L2_PIX_FMT_YVYU,
  61. .vdownsampling = { 1 },
  62. .bit_depth = { 16 },
  63. .is_yuv = true,
  64. .planes = 1,
  65. .buffers = 1,
  66. },
  67. {
  68. .fourcc = V4L2_PIX_FMT_VYUY,
  69. .vdownsampling = { 1 },
  70. .bit_depth = { 16 },
  71. .is_yuv = true,
  72. .planes = 1,
  73. .buffers = 1,
  74. },
  75. {
  76. .fourcc = V4L2_PIX_FMT_YUV422P,
  77. .vdownsampling = { 1, 1, 1 },
  78. .bit_depth = { 8, 4, 4 },
  79. .is_yuv = true,
  80. .planes = 3,
  81. .buffers = 1,
  82. },
  83. {
  84. .fourcc = V4L2_PIX_FMT_YUV420,
  85. .vdownsampling = { 1, 2, 2 },
  86. .bit_depth = { 8, 4, 4 },
  87. .is_yuv = true,
  88. .planes = 3,
  89. .buffers = 1,
  90. },
  91. {
  92. .fourcc = V4L2_PIX_FMT_YVU420,
  93. .vdownsampling = { 1, 2, 2 },
  94. .bit_depth = { 8, 4, 4 },
  95. .is_yuv = true,
  96. .planes = 3,
  97. .buffers = 1,
  98. },
  99. {
  100. .fourcc = V4L2_PIX_FMT_NV12,
  101. .vdownsampling = { 1, 2 },
  102. .bit_depth = { 8, 8 },
  103. .is_yuv = true,
  104. .planes = 2,
  105. .buffers = 1,
  106. },
  107. {
  108. .fourcc = V4L2_PIX_FMT_NV21,
  109. .vdownsampling = { 1, 2 },
  110. .bit_depth = { 8, 8 },
  111. .is_yuv = true,
  112. .planes = 2,
  113. .buffers = 1,
  114. },
  115. {
  116. .fourcc = V4L2_PIX_FMT_NV16,
  117. .vdownsampling = { 1, 1 },
  118. .bit_depth = { 8, 8 },
  119. .is_yuv = true,
  120. .planes = 2,
  121. .buffers = 1,
  122. },
  123. {
  124. .fourcc = V4L2_PIX_FMT_NV61,
  125. .vdownsampling = { 1, 1 },
  126. .bit_depth = { 8, 8 },
  127. .is_yuv = true,
  128. .planes = 2,
  129. .buffers = 1,
  130. },
  131. {
  132. .fourcc = V4L2_PIX_FMT_NV24,
  133. .vdownsampling = { 1, 1 },
  134. .bit_depth = { 8, 16 },
  135. .is_yuv = true,
  136. .planes = 2,
  137. .buffers = 1,
  138. },
  139. {
  140. .fourcc = V4L2_PIX_FMT_NV42,
  141. .vdownsampling = { 1, 1 },
  142. .bit_depth = { 8, 16 },
  143. .is_yuv = true,
  144. .planes = 2,
  145. .buffers = 1,
  146. },
  147. {
  148. .fourcc = V4L2_PIX_FMT_YUV555, /* uuuvvvvv ayyyyyuu */
  149. .vdownsampling = { 1 },
  150. .bit_depth = { 16 },
  151. .planes = 1,
  152. .buffers = 1,
  153. .alpha_mask = 0x8000,
  154. },
  155. {
  156. .fourcc = V4L2_PIX_FMT_YUV565, /* uuuvvvvv yyyyyuuu */
  157. .vdownsampling = { 1 },
  158. .bit_depth = { 16 },
  159. .planes = 1,
  160. .buffers = 1,
  161. },
  162. {
  163. .fourcc = V4L2_PIX_FMT_YUV444, /* uuuuvvvv aaaayyyy */
  164. .vdownsampling = { 1 },
  165. .bit_depth = { 16 },
  166. .planes = 1,
  167. .buffers = 1,
  168. .alpha_mask = 0xf000,
  169. },
  170. {
  171. .fourcc = V4L2_PIX_FMT_YUV32, /* ayuv */
  172. .vdownsampling = { 1 },
  173. .bit_depth = { 32 },
  174. .planes = 1,
  175. .buffers = 1,
  176. .alpha_mask = 0x000000ff,
  177. },
  178. {
  179. .fourcc = V4L2_PIX_FMT_GREY,
  180. .vdownsampling = { 1 },
  181. .bit_depth = { 8 },
  182. .is_yuv = true,
  183. .planes = 1,
  184. .buffers = 1,
  185. },
  186. {
  187. .fourcc = V4L2_PIX_FMT_Y16,
  188. .vdownsampling = { 1 },
  189. .bit_depth = { 16 },
  190. .is_yuv = true,
  191. .planes = 1,
  192. .buffers = 1,
  193. },
  194. {
  195. .fourcc = V4L2_PIX_FMT_Y16_BE,
  196. .vdownsampling = { 1 },
  197. .bit_depth = { 16 },
  198. .is_yuv = true,
  199. .planes = 1,
  200. .buffers = 1,
  201. },
  202. {
  203. .fourcc = V4L2_PIX_FMT_RGB332, /* rrrgggbb */
  204. .vdownsampling = { 1 },
  205. .bit_depth = { 8 },
  206. .planes = 1,
  207. .buffers = 1,
  208. },
  209. {
  210. .fourcc = V4L2_PIX_FMT_RGB565, /* gggbbbbb rrrrrggg */
  211. .vdownsampling = { 1 },
  212. .bit_depth = { 16 },
  213. .planes = 1,
  214. .buffers = 1,
  215. .can_do_overlay = true,
  216. },
  217. {
  218. .fourcc = V4L2_PIX_FMT_RGB565X, /* rrrrrggg gggbbbbb */
  219. .vdownsampling = { 1 },
  220. .bit_depth = { 16 },
  221. .planes = 1,
  222. .buffers = 1,
  223. .can_do_overlay = true,
  224. },
  225. {
  226. .fourcc = V4L2_PIX_FMT_RGB444, /* xxxxrrrr ggggbbbb */
  227. .vdownsampling = { 1 },
  228. .bit_depth = { 16 },
  229. .planes = 1,
  230. .buffers = 1,
  231. },
  232. {
  233. .fourcc = V4L2_PIX_FMT_XRGB444, /* xxxxrrrr ggggbbbb */
  234. .vdownsampling = { 1 },
  235. .bit_depth = { 16 },
  236. .planes = 1,
  237. .buffers = 1,
  238. },
  239. {
  240. .fourcc = V4L2_PIX_FMT_ARGB444, /* aaaarrrr ggggbbbb */
  241. .vdownsampling = { 1 },
  242. .bit_depth = { 16 },
  243. .planes = 1,
  244. .buffers = 1,
  245. .alpha_mask = 0x00f0,
  246. },
  247. {
  248. .fourcc = V4L2_PIX_FMT_RGB555, /* gggbbbbb xrrrrrgg */
  249. .vdownsampling = { 1 },
  250. .bit_depth = { 16 },
  251. .planes = 1,
  252. .buffers = 1,
  253. .can_do_overlay = true,
  254. },
  255. {
  256. .fourcc = V4L2_PIX_FMT_XRGB555, /* gggbbbbb xrrrrrgg */
  257. .vdownsampling = { 1 },
  258. .bit_depth = { 16 },
  259. .planes = 1,
  260. .buffers = 1,
  261. .can_do_overlay = true,
  262. },
  263. {
  264. .fourcc = V4L2_PIX_FMT_ARGB555, /* gggbbbbb arrrrrgg */
  265. .vdownsampling = { 1 },
  266. .bit_depth = { 16 },
  267. .planes = 1,
  268. .buffers = 1,
  269. .can_do_overlay = true,
  270. .alpha_mask = 0x8000,
  271. },
  272. {
  273. .fourcc = V4L2_PIX_FMT_RGB555X, /* xrrrrrgg gggbbbbb */
  274. .vdownsampling = { 1 },
  275. .bit_depth = { 16 },
  276. .planes = 1,
  277. .buffers = 1,
  278. },
  279. {
  280. .fourcc = V4L2_PIX_FMT_XRGB555X, /* xrrrrrgg gggbbbbb */
  281. .vdownsampling = { 1 },
  282. .bit_depth = { 16 },
  283. .planes = 1,
  284. .buffers = 1,
  285. },
  286. {
  287. .fourcc = V4L2_PIX_FMT_ARGB555X, /* arrrrrgg gggbbbbb */
  288. .vdownsampling = { 1 },
  289. .bit_depth = { 16 },
  290. .planes = 1,
  291. .buffers = 1,
  292. .alpha_mask = 0x0080,
  293. },
  294. {
  295. .fourcc = V4L2_PIX_FMT_RGB24, /* rgb */
  296. .vdownsampling = { 1 },
  297. .bit_depth = { 24 },
  298. .planes = 1,
  299. .buffers = 1,
  300. },
  301. {
  302. .fourcc = V4L2_PIX_FMT_BGR24, /* bgr */
  303. .vdownsampling = { 1 },
  304. .bit_depth = { 24 },
  305. .planes = 1,
  306. .buffers = 1,
  307. },
  308. {
  309. .fourcc = V4L2_PIX_FMT_BGR666, /* bbbbbbgg ggggrrrr rrxxxxxx */
  310. .vdownsampling = { 1 },
  311. .bit_depth = { 32 },
  312. .planes = 1,
  313. .buffers = 1,
  314. },
  315. {
  316. .fourcc = V4L2_PIX_FMT_RGB32, /* xrgb */
  317. .vdownsampling = { 1 },
  318. .bit_depth = { 32 },
  319. .planes = 1,
  320. .buffers = 1,
  321. },
  322. {
  323. .fourcc = V4L2_PIX_FMT_BGR32, /* bgrx */
  324. .vdownsampling = { 1 },
  325. .bit_depth = { 32 },
  326. .planes = 1,
  327. .buffers = 1,
  328. },
  329. {
  330. .fourcc = V4L2_PIX_FMT_XRGB32, /* xrgb */
  331. .vdownsampling = { 1 },
  332. .bit_depth = { 32 },
  333. .planes = 1,
  334. .buffers = 1,
  335. },
  336. {
  337. .fourcc = V4L2_PIX_FMT_XBGR32, /* bgrx */
  338. .vdownsampling = { 1 },
  339. .bit_depth = { 32 },
  340. .planes = 1,
  341. .buffers = 1,
  342. },
  343. {
  344. .fourcc = V4L2_PIX_FMT_ARGB32, /* argb */
  345. .vdownsampling = { 1 },
  346. .bit_depth = { 32 },
  347. .planes = 1,
  348. .buffers = 1,
  349. .alpha_mask = 0x000000ff,
  350. },
  351. {
  352. .fourcc = V4L2_PIX_FMT_ABGR32, /* bgra */
  353. .vdownsampling = { 1 },
  354. .bit_depth = { 32 },
  355. .planes = 1,
  356. .buffers = 1,
  357. .alpha_mask = 0xff000000,
  358. },
  359. {
  360. .fourcc = V4L2_PIX_FMT_SBGGR8, /* Bayer BG/GR */
  361. .vdownsampling = { 1 },
  362. .bit_depth = { 8 },
  363. .planes = 1,
  364. .buffers = 1,
  365. },
  366. {
  367. .fourcc = V4L2_PIX_FMT_SGBRG8, /* Bayer GB/RG */
  368. .vdownsampling = { 1 },
  369. .bit_depth = { 8 },
  370. .planes = 1,
  371. .buffers = 1,
  372. },
  373. {
  374. .fourcc = V4L2_PIX_FMT_SGRBG8, /* Bayer GR/BG */
  375. .vdownsampling = { 1 },
  376. .bit_depth = { 8 },
  377. .planes = 1,
  378. .buffers = 1,
  379. },
  380. {
  381. .fourcc = V4L2_PIX_FMT_SRGGB8, /* Bayer RG/GB */
  382. .vdownsampling = { 1 },
  383. .bit_depth = { 8 },
  384. .planes = 1,
  385. .buffers = 1,
  386. },
  387. {
  388. .fourcc = V4L2_PIX_FMT_NV16M,
  389. .vdownsampling = { 1, 1 },
  390. .bit_depth = { 8, 8 },
  391. .is_yuv = true,
  392. .planes = 2,
  393. .buffers = 2,
  394. .data_offset = { PLANE0_DATA_OFFSET, 0 },
  395. },
  396. {
  397. .fourcc = V4L2_PIX_FMT_NV61M,
  398. .vdownsampling = { 1, 1 },
  399. .bit_depth = { 8, 8 },
  400. .is_yuv = true,
  401. .planes = 2,
  402. .buffers = 2,
  403. .data_offset = { 0, PLANE0_DATA_OFFSET },
  404. },
  405. {
  406. .fourcc = V4L2_PIX_FMT_YUV420M,
  407. .vdownsampling = { 1, 2, 2 },
  408. .bit_depth = { 8, 4, 4 },
  409. .is_yuv = true,
  410. .planes = 3,
  411. .buffers = 3,
  412. },
  413. {
  414. .fourcc = V4L2_PIX_FMT_YVU420M,
  415. .vdownsampling = { 1, 2, 2 },
  416. .bit_depth = { 8, 4, 4 },
  417. .is_yuv = true,
  418. .planes = 3,
  419. .buffers = 3,
  420. },
  421. {
  422. .fourcc = V4L2_PIX_FMT_NV12M,
  423. .vdownsampling = { 1, 2 },
  424. .bit_depth = { 8, 8 },
  425. .is_yuv = true,
  426. .planes = 2,
  427. .buffers = 2,
  428. },
  429. {
  430. .fourcc = V4L2_PIX_FMT_NV21M,
  431. .vdownsampling = { 1, 2 },
  432. .bit_depth = { 8, 8 },
  433. .is_yuv = true,
  434. .planes = 2,
  435. .buffers = 2,
  436. },
  437. };
  438. /* There are 6 multiplanar formats in the list */
  439. #define VIVID_MPLANAR_FORMATS 6
  440. const struct vivid_fmt *vivid_get_format(struct vivid_dev *dev, u32 pixelformat)
  441. {
  442. const struct vivid_fmt *fmt;
  443. unsigned k;
  444. for (k = 0; k < ARRAY_SIZE(vivid_formats); k++) {
  445. fmt = &vivid_formats[k];
  446. if (fmt->fourcc == pixelformat)
  447. if (fmt->buffers == 1 || dev->multiplanar)
  448. return fmt;
  449. }
  450. return NULL;
  451. }
  452. bool vivid_vid_can_loop(struct vivid_dev *dev)
  453. {
  454. if (dev->src_rect.width != dev->sink_rect.width ||
  455. dev->src_rect.height != dev->sink_rect.height)
  456. return false;
  457. if (dev->fmt_cap->fourcc != dev->fmt_out->fourcc)
  458. return false;
  459. if (dev->field_cap != dev->field_out)
  460. return false;
  461. /*
  462. * While this can be supported, it is just too much work
  463. * to actually implement.
  464. */
  465. if (dev->field_cap == V4L2_FIELD_SEQ_TB ||
  466. dev->field_cap == V4L2_FIELD_SEQ_BT)
  467. return false;
  468. if (vivid_is_svid_cap(dev) && vivid_is_svid_out(dev)) {
  469. if (!(dev->std_cap & V4L2_STD_525_60) !=
  470. !(dev->std_out & V4L2_STD_525_60))
  471. return false;
  472. return true;
  473. }
  474. if (vivid_is_hdmi_cap(dev) && vivid_is_hdmi_out(dev))
  475. return true;
  476. return false;
  477. }
  478. void vivid_send_source_change(struct vivid_dev *dev, unsigned type)
  479. {
  480. struct v4l2_event ev = {
  481. .type = V4L2_EVENT_SOURCE_CHANGE,
  482. .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION,
  483. };
  484. unsigned i;
  485. for (i = 0; i < dev->num_inputs; i++) {
  486. ev.id = i;
  487. if (dev->input_type[i] == type) {
  488. if (video_is_registered(&dev->vid_cap_dev) && dev->has_vid_cap)
  489. v4l2_event_queue(&dev->vid_cap_dev, &ev);
  490. if (video_is_registered(&dev->vbi_cap_dev) && dev->has_vbi_cap)
  491. v4l2_event_queue(&dev->vbi_cap_dev, &ev);
  492. }
  493. }
  494. }
  495. /*
  496. * Conversion function that converts a single-planar format to a
  497. * single-plane multiplanar format.
  498. */
  499. void fmt_sp2mp(const struct v4l2_format *sp_fmt, struct v4l2_format *mp_fmt)
  500. {
  501. struct v4l2_pix_format_mplane *mp = &mp_fmt->fmt.pix_mp;
  502. struct v4l2_plane_pix_format *ppix = &mp->plane_fmt[0];
  503. const struct v4l2_pix_format *pix = &sp_fmt->fmt.pix;
  504. bool is_out = sp_fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT;
  505. memset(mp->reserved, 0, sizeof(mp->reserved));
  506. mp_fmt->type = is_out ? V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE :
  507. V4L2_CAP_VIDEO_CAPTURE_MPLANE;
  508. mp->width = pix->width;
  509. mp->height = pix->height;
  510. mp->pixelformat = pix->pixelformat;
  511. mp->field = pix->field;
  512. mp->colorspace = pix->colorspace;
  513. mp->xfer_func = pix->xfer_func;
  514. mp->ycbcr_enc = pix->ycbcr_enc;
  515. mp->quantization = pix->quantization;
  516. mp->num_planes = 1;
  517. mp->flags = pix->flags;
  518. ppix->sizeimage = pix->sizeimage;
  519. ppix->bytesperline = pix->bytesperline;
  520. memset(ppix->reserved, 0, sizeof(ppix->reserved));
  521. }
  522. int fmt_sp2mp_func(struct file *file, void *priv,
  523. struct v4l2_format *f, fmtfunc func)
  524. {
  525. struct v4l2_format fmt;
  526. struct v4l2_pix_format_mplane *mp = &fmt.fmt.pix_mp;
  527. struct v4l2_plane_pix_format *ppix = &mp->plane_fmt[0];
  528. struct v4l2_pix_format *pix = &f->fmt.pix;
  529. int ret;
  530. /* Converts to a mplane format */
  531. fmt_sp2mp(f, &fmt);
  532. /* Passes it to the generic mplane format function */
  533. ret = func(file, priv, &fmt);
  534. /* Copies back the mplane data to the single plane format */
  535. pix->width = mp->width;
  536. pix->height = mp->height;
  537. pix->pixelformat = mp->pixelformat;
  538. pix->field = mp->field;
  539. pix->colorspace = mp->colorspace;
  540. pix->xfer_func = mp->xfer_func;
  541. pix->ycbcr_enc = mp->ycbcr_enc;
  542. pix->quantization = mp->quantization;
  543. pix->sizeimage = ppix->sizeimage;
  544. pix->bytesperline = ppix->bytesperline;
  545. pix->flags = mp->flags;
  546. return ret;
  547. }
  548. /* v4l2_rect helper function: copy the width/height values */
  549. void rect_set_size_to(struct v4l2_rect *r, const struct v4l2_rect *size)
  550. {
  551. r->width = size->width;
  552. r->height = size->height;
  553. }
  554. /* v4l2_rect helper function: width and height of r should be >= min_size */
  555. void rect_set_min_size(struct v4l2_rect *r, const struct v4l2_rect *min_size)
  556. {
  557. if (r->width < min_size->width)
  558. r->width = min_size->width;
  559. if (r->height < min_size->height)
  560. r->height = min_size->height;
  561. }
  562. /* v4l2_rect helper function: width and height of r should be <= max_size */
  563. void rect_set_max_size(struct v4l2_rect *r, const struct v4l2_rect *max_size)
  564. {
  565. if (r->width > max_size->width)
  566. r->width = max_size->width;
  567. if (r->height > max_size->height)
  568. r->height = max_size->height;
  569. }
  570. /* v4l2_rect helper function: r should be inside boundary */
  571. void rect_map_inside(struct v4l2_rect *r, const struct v4l2_rect *boundary)
  572. {
  573. rect_set_max_size(r, boundary);
  574. if (r->left < boundary->left)
  575. r->left = boundary->left;
  576. if (r->top < boundary->top)
  577. r->top = boundary->top;
  578. if (r->left + r->width > boundary->width)
  579. r->left = boundary->width - r->width;
  580. if (r->top + r->height > boundary->height)
  581. r->top = boundary->height - r->height;
  582. }
  583. /* v4l2_rect helper function: return true if r1 has the same size as r2 */
  584. bool rect_same_size(const struct v4l2_rect *r1, const struct v4l2_rect *r2)
  585. {
  586. return r1->width == r2->width && r1->height == r2->height;
  587. }
  588. /* v4l2_rect helper function: calculate the intersection of two rects */
  589. struct v4l2_rect rect_intersect(const struct v4l2_rect *a, const struct v4l2_rect *b)
  590. {
  591. struct v4l2_rect r;
  592. int right, bottom;
  593. r.top = max(a->top, b->top);
  594. r.left = max(a->left, b->left);
  595. bottom = min(a->top + a->height, b->top + b->height);
  596. right = min(a->left + a->width, b->left + b->width);
  597. r.height = max(0, bottom - r.top);
  598. r.width = max(0, right - r.left);
  599. return r;
  600. }
  601. /*
  602. * v4l2_rect helper function: scale rect r by to->width / from->width and
  603. * to->height / from->height.
  604. */
  605. void rect_scale(struct v4l2_rect *r, const struct v4l2_rect *from,
  606. const struct v4l2_rect *to)
  607. {
  608. if (from->width == 0 || from->height == 0) {
  609. r->left = r->top = r->width = r->height = 0;
  610. return;
  611. }
  612. r->left = (((r->left - from->left) * to->width) / from->width) & ~1;
  613. r->width = ((r->width * to->width) / from->width) & ~1;
  614. r->top = ((r->top - from->top) * to->height) / from->height;
  615. r->height = (r->height * to->height) / from->height;
  616. }
  617. bool rect_overlap(const struct v4l2_rect *r1, const struct v4l2_rect *r2)
  618. {
  619. /*
  620. * IF the left side of r1 is to the right of the right side of r2 OR
  621. * the left side of r2 is to the right of the right side of r1 THEN
  622. * they do not overlap.
  623. */
  624. if (r1->left >= r2->left + r2->width ||
  625. r2->left >= r1->left + r1->width)
  626. return false;
  627. /*
  628. * IF the top side of r1 is below the bottom of r2 OR
  629. * the top side of r2 is below the bottom of r1 THEN
  630. * they do not overlap.
  631. */
  632. if (r1->top >= r2->top + r2->height ||
  633. r2->top >= r1->top + r1->height)
  634. return false;
  635. return true;
  636. }
  637. int vivid_vid_adjust_sel(unsigned flags, struct v4l2_rect *r)
  638. {
  639. unsigned w = r->width;
  640. unsigned h = r->height;
  641. /* sanitize w and h in case someone passes ~0 as the value */
  642. w &= 0xffff;
  643. h &= 0xffff;
  644. if (!(flags & V4L2_SEL_FLAG_LE)) {
  645. w++;
  646. h++;
  647. if (w < 2)
  648. w = 2;
  649. if (h < 2)
  650. h = 2;
  651. }
  652. if (!(flags & V4L2_SEL_FLAG_GE)) {
  653. if (w > MAX_WIDTH)
  654. w = MAX_WIDTH;
  655. if (h > MAX_HEIGHT)
  656. h = MAX_HEIGHT;
  657. }
  658. w = w & ~1;
  659. h = h & ~1;
  660. if (w < 2 || h < 2)
  661. return -ERANGE;
  662. if (w > MAX_WIDTH || h > MAX_HEIGHT)
  663. return -ERANGE;
  664. if (r->top < 0)
  665. r->top = 0;
  666. if (r->left < 0)
  667. r->left = 0;
  668. /* sanitize left and top in case someone passes ~0 as the value */
  669. r->left &= 0xfffe;
  670. r->top &= 0xfffe;
  671. if (r->left + w > MAX_WIDTH)
  672. r->left = MAX_WIDTH - w;
  673. if (r->top + h > MAX_HEIGHT)
  674. r->top = MAX_HEIGHT - h;
  675. if ((flags & (V4L2_SEL_FLAG_GE | V4L2_SEL_FLAG_LE)) ==
  676. (V4L2_SEL_FLAG_GE | V4L2_SEL_FLAG_LE) &&
  677. (r->width != w || r->height != h))
  678. return -ERANGE;
  679. r->width = w;
  680. r->height = h;
  681. return 0;
  682. }
  683. int vivid_enum_fmt_vid(struct file *file, void *priv,
  684. struct v4l2_fmtdesc *f)
  685. {
  686. struct vivid_dev *dev = video_drvdata(file);
  687. const struct vivid_fmt *fmt;
  688. if (f->index >= ARRAY_SIZE(vivid_formats) -
  689. (dev->multiplanar ? 0 : VIVID_MPLANAR_FORMATS))
  690. return -EINVAL;
  691. fmt = &vivid_formats[f->index];
  692. f->pixelformat = fmt->fourcc;
  693. return 0;
  694. }
  695. int vidioc_enum_fmt_vid_mplane(struct file *file, void *priv,
  696. struct v4l2_fmtdesc *f)
  697. {
  698. struct vivid_dev *dev = video_drvdata(file);
  699. if (!dev->multiplanar)
  700. return -ENOTTY;
  701. return vivid_enum_fmt_vid(file, priv, f);
  702. }
  703. int vidioc_enum_fmt_vid(struct file *file, void *priv,
  704. struct v4l2_fmtdesc *f)
  705. {
  706. struct vivid_dev *dev = video_drvdata(file);
  707. if (dev->multiplanar)
  708. return -ENOTTY;
  709. return vivid_enum_fmt_vid(file, priv, f);
  710. }
  711. int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
  712. {
  713. struct vivid_dev *dev = video_drvdata(file);
  714. struct video_device *vdev = video_devdata(file);
  715. if (vdev->vfl_dir == VFL_DIR_RX) {
  716. if (!vivid_is_sdtv_cap(dev))
  717. return -ENODATA;
  718. *id = dev->std_cap;
  719. } else {
  720. if (!vivid_is_svid_out(dev))
  721. return -ENODATA;
  722. *id = dev->std_out;
  723. }
  724. return 0;
  725. }
  726. int vidioc_g_dv_timings(struct file *file, void *_fh,
  727. struct v4l2_dv_timings *timings)
  728. {
  729. struct vivid_dev *dev = video_drvdata(file);
  730. struct video_device *vdev = video_devdata(file);
  731. if (vdev->vfl_dir == VFL_DIR_RX) {
  732. if (!vivid_is_hdmi_cap(dev))
  733. return -ENODATA;
  734. *timings = dev->dv_timings_cap;
  735. } else {
  736. if (!vivid_is_hdmi_out(dev))
  737. return -ENODATA;
  738. *timings = dev->dv_timings_out;
  739. }
  740. return 0;
  741. }
  742. int vidioc_enum_dv_timings(struct file *file, void *_fh,
  743. struct v4l2_enum_dv_timings *timings)
  744. {
  745. struct vivid_dev *dev = video_drvdata(file);
  746. struct video_device *vdev = video_devdata(file);
  747. if (vdev->vfl_dir == VFL_DIR_RX) {
  748. if (!vivid_is_hdmi_cap(dev))
  749. return -ENODATA;
  750. } else {
  751. if (!vivid_is_hdmi_out(dev))
  752. return -ENODATA;
  753. }
  754. return v4l2_enum_dv_timings_cap(timings, &vivid_dv_timings_cap,
  755. NULL, NULL);
  756. }
  757. int vidioc_dv_timings_cap(struct file *file, void *_fh,
  758. struct v4l2_dv_timings_cap *cap)
  759. {
  760. struct vivid_dev *dev = video_drvdata(file);
  761. struct video_device *vdev = video_devdata(file);
  762. if (vdev->vfl_dir == VFL_DIR_RX) {
  763. if (!vivid_is_hdmi_cap(dev))
  764. return -ENODATA;
  765. } else {
  766. if (!vivid_is_hdmi_out(dev))
  767. return -ENODATA;
  768. }
  769. *cap = vivid_dv_timings_cap;
  770. return 0;
  771. }
  772. int vidioc_g_edid(struct file *file, void *_fh,
  773. struct v4l2_edid *edid)
  774. {
  775. struct vivid_dev *dev = video_drvdata(file);
  776. struct video_device *vdev = video_devdata(file);
  777. memset(edid->reserved, 0, sizeof(edid->reserved));
  778. if (vdev->vfl_dir == VFL_DIR_RX) {
  779. if (edid->pad >= dev->num_inputs)
  780. return -EINVAL;
  781. if (dev->input_type[edid->pad] != HDMI)
  782. return -EINVAL;
  783. } else {
  784. if (edid->pad >= dev->num_outputs)
  785. return -EINVAL;
  786. if (dev->output_type[edid->pad] != HDMI)
  787. return -EINVAL;
  788. }
  789. if (edid->start_block == 0 && edid->blocks == 0) {
  790. edid->blocks = dev->edid_blocks;
  791. return 0;
  792. }
  793. if (dev->edid_blocks == 0)
  794. return -ENODATA;
  795. if (edid->start_block >= dev->edid_blocks)
  796. return -EINVAL;
  797. if (edid->start_block + edid->blocks > dev->edid_blocks)
  798. edid->blocks = dev->edid_blocks - edid->start_block;
  799. memcpy(edid->edid, dev->edid, edid->blocks * 128);
  800. return 0;
  801. }