vivid-vid-common.c 21 KB

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