vivid-vid-common.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  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(16, MAX_WIDTH, 16, 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_SBGGR10, /* Bayer BG/GR */
  389. .vdownsampling = { 1 },
  390. .bit_depth = { 16 },
  391. .planes = 1,
  392. .buffers = 1,
  393. },
  394. {
  395. .fourcc = V4L2_PIX_FMT_SGBRG10, /* Bayer GB/RG */
  396. .vdownsampling = { 1 },
  397. .bit_depth = { 16 },
  398. .planes = 1,
  399. .buffers = 1,
  400. },
  401. {
  402. .fourcc = V4L2_PIX_FMT_SGRBG10, /* Bayer GR/BG */
  403. .vdownsampling = { 1 },
  404. .bit_depth = { 16 },
  405. .planes = 1,
  406. .buffers = 1,
  407. },
  408. {
  409. .fourcc = V4L2_PIX_FMT_SRGGB10, /* Bayer RG/GB */
  410. .vdownsampling = { 1 },
  411. .bit_depth = { 16 },
  412. .planes = 1,
  413. .buffers = 1,
  414. },
  415. {
  416. .fourcc = V4L2_PIX_FMT_SBGGR12, /* Bayer BG/GR */
  417. .vdownsampling = { 1 },
  418. .bit_depth = { 16 },
  419. .planes = 1,
  420. .buffers = 1,
  421. },
  422. {
  423. .fourcc = V4L2_PIX_FMT_SGBRG12, /* Bayer GB/RG */
  424. .vdownsampling = { 1 },
  425. .bit_depth = { 16 },
  426. .planes = 1,
  427. .buffers = 1,
  428. },
  429. {
  430. .fourcc = V4L2_PIX_FMT_SGRBG12, /* Bayer GR/BG */
  431. .vdownsampling = { 1 },
  432. .bit_depth = { 16 },
  433. .planes = 1,
  434. .buffers = 1,
  435. },
  436. {
  437. .fourcc = V4L2_PIX_FMT_SRGGB12, /* Bayer RG/GB */
  438. .vdownsampling = { 1 },
  439. .bit_depth = { 16 },
  440. .planes = 1,
  441. .buffers = 1,
  442. },
  443. {
  444. .fourcc = V4L2_PIX_FMT_NV16M,
  445. .vdownsampling = { 1, 1 },
  446. .bit_depth = { 8, 8 },
  447. .is_yuv = true,
  448. .planes = 2,
  449. .buffers = 2,
  450. .data_offset = { PLANE0_DATA_OFFSET, 0 },
  451. },
  452. {
  453. .fourcc = V4L2_PIX_FMT_NV61M,
  454. .vdownsampling = { 1, 1 },
  455. .bit_depth = { 8, 8 },
  456. .is_yuv = true,
  457. .planes = 2,
  458. .buffers = 2,
  459. .data_offset = { 0, PLANE0_DATA_OFFSET },
  460. },
  461. {
  462. .fourcc = V4L2_PIX_FMT_YUV420M,
  463. .vdownsampling = { 1, 2, 2 },
  464. .bit_depth = { 8, 4, 4 },
  465. .is_yuv = true,
  466. .planes = 3,
  467. .buffers = 3,
  468. },
  469. {
  470. .fourcc = V4L2_PIX_FMT_YVU420M,
  471. .vdownsampling = { 1, 2, 2 },
  472. .bit_depth = { 8, 4, 4 },
  473. .is_yuv = true,
  474. .planes = 3,
  475. .buffers = 3,
  476. },
  477. {
  478. .fourcc = V4L2_PIX_FMT_NV12M,
  479. .vdownsampling = { 1, 2 },
  480. .bit_depth = { 8, 8 },
  481. .is_yuv = true,
  482. .planes = 2,
  483. .buffers = 2,
  484. },
  485. {
  486. .fourcc = V4L2_PIX_FMT_NV21M,
  487. .vdownsampling = { 1, 2 },
  488. .bit_depth = { 8, 8 },
  489. .is_yuv = true,
  490. .planes = 2,
  491. .buffers = 2,
  492. },
  493. };
  494. /* There are 6 multiplanar formats in the list */
  495. #define VIVID_MPLANAR_FORMATS 6
  496. const struct vivid_fmt *vivid_get_format(struct vivid_dev *dev, u32 pixelformat)
  497. {
  498. const struct vivid_fmt *fmt;
  499. unsigned k;
  500. for (k = 0; k < ARRAY_SIZE(vivid_formats); k++) {
  501. fmt = &vivid_formats[k];
  502. if (fmt->fourcc == pixelformat)
  503. if (fmt->buffers == 1 || dev->multiplanar)
  504. return fmt;
  505. }
  506. return NULL;
  507. }
  508. bool vivid_vid_can_loop(struct vivid_dev *dev)
  509. {
  510. if (dev->src_rect.width != dev->sink_rect.width ||
  511. dev->src_rect.height != dev->sink_rect.height)
  512. return false;
  513. if (dev->fmt_cap->fourcc != dev->fmt_out->fourcc)
  514. return false;
  515. if (dev->field_cap != dev->field_out)
  516. return false;
  517. /*
  518. * While this can be supported, it is just too much work
  519. * to actually implement.
  520. */
  521. if (dev->field_cap == V4L2_FIELD_SEQ_TB ||
  522. dev->field_cap == V4L2_FIELD_SEQ_BT)
  523. return false;
  524. if (vivid_is_svid_cap(dev) && vivid_is_svid_out(dev)) {
  525. if (!(dev->std_cap & V4L2_STD_525_60) !=
  526. !(dev->std_out & V4L2_STD_525_60))
  527. return false;
  528. return true;
  529. }
  530. if (vivid_is_hdmi_cap(dev) && vivid_is_hdmi_out(dev))
  531. return true;
  532. return false;
  533. }
  534. void vivid_send_source_change(struct vivid_dev *dev, unsigned type)
  535. {
  536. struct v4l2_event ev = {
  537. .type = V4L2_EVENT_SOURCE_CHANGE,
  538. .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION,
  539. };
  540. unsigned i;
  541. for (i = 0; i < dev->num_inputs; i++) {
  542. ev.id = i;
  543. if (dev->input_type[i] == type) {
  544. if (video_is_registered(&dev->vid_cap_dev) && dev->has_vid_cap)
  545. v4l2_event_queue(&dev->vid_cap_dev, &ev);
  546. if (video_is_registered(&dev->vbi_cap_dev) && dev->has_vbi_cap)
  547. v4l2_event_queue(&dev->vbi_cap_dev, &ev);
  548. }
  549. }
  550. }
  551. /*
  552. * Conversion function that converts a single-planar format to a
  553. * single-plane multiplanar format.
  554. */
  555. void fmt_sp2mp(const struct v4l2_format *sp_fmt, struct v4l2_format *mp_fmt)
  556. {
  557. struct v4l2_pix_format_mplane *mp = &mp_fmt->fmt.pix_mp;
  558. struct v4l2_plane_pix_format *ppix = &mp->plane_fmt[0];
  559. const struct v4l2_pix_format *pix = &sp_fmt->fmt.pix;
  560. bool is_out = sp_fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT;
  561. memset(mp->reserved, 0, sizeof(mp->reserved));
  562. mp_fmt->type = is_out ? V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE :
  563. V4L2_CAP_VIDEO_CAPTURE_MPLANE;
  564. mp->width = pix->width;
  565. mp->height = pix->height;
  566. mp->pixelformat = pix->pixelformat;
  567. mp->field = pix->field;
  568. mp->colorspace = pix->colorspace;
  569. mp->xfer_func = pix->xfer_func;
  570. mp->ycbcr_enc = pix->ycbcr_enc;
  571. mp->quantization = pix->quantization;
  572. mp->num_planes = 1;
  573. mp->flags = pix->flags;
  574. ppix->sizeimage = pix->sizeimage;
  575. ppix->bytesperline = pix->bytesperline;
  576. memset(ppix->reserved, 0, sizeof(ppix->reserved));
  577. }
  578. int fmt_sp2mp_func(struct file *file, void *priv,
  579. struct v4l2_format *f, fmtfunc func)
  580. {
  581. struct v4l2_format fmt;
  582. struct v4l2_pix_format_mplane *mp = &fmt.fmt.pix_mp;
  583. struct v4l2_plane_pix_format *ppix = &mp->plane_fmt[0];
  584. struct v4l2_pix_format *pix = &f->fmt.pix;
  585. int ret;
  586. /* Converts to a mplane format */
  587. fmt_sp2mp(f, &fmt);
  588. /* Passes it to the generic mplane format function */
  589. ret = func(file, priv, &fmt);
  590. /* Copies back the mplane data to the single plane format */
  591. pix->width = mp->width;
  592. pix->height = mp->height;
  593. pix->pixelformat = mp->pixelformat;
  594. pix->field = mp->field;
  595. pix->colorspace = mp->colorspace;
  596. pix->xfer_func = mp->xfer_func;
  597. pix->ycbcr_enc = mp->ycbcr_enc;
  598. pix->quantization = mp->quantization;
  599. pix->sizeimage = ppix->sizeimage;
  600. pix->bytesperline = ppix->bytesperline;
  601. pix->flags = mp->flags;
  602. return ret;
  603. }
  604. /* v4l2_rect helper function: copy the width/height values */
  605. void rect_set_size_to(struct v4l2_rect *r, const struct v4l2_rect *size)
  606. {
  607. r->width = size->width;
  608. r->height = size->height;
  609. }
  610. /* v4l2_rect helper function: width and height of r should be >= min_size */
  611. void rect_set_min_size(struct v4l2_rect *r, const struct v4l2_rect *min_size)
  612. {
  613. if (r->width < min_size->width)
  614. r->width = min_size->width;
  615. if (r->height < min_size->height)
  616. r->height = min_size->height;
  617. }
  618. /* v4l2_rect helper function: width and height of r should be <= max_size */
  619. void rect_set_max_size(struct v4l2_rect *r, const struct v4l2_rect *max_size)
  620. {
  621. if (r->width > max_size->width)
  622. r->width = max_size->width;
  623. if (r->height > max_size->height)
  624. r->height = max_size->height;
  625. }
  626. /* v4l2_rect helper function: r should be inside boundary */
  627. void rect_map_inside(struct v4l2_rect *r, const struct v4l2_rect *boundary)
  628. {
  629. rect_set_max_size(r, boundary);
  630. if (r->left < boundary->left)
  631. r->left = boundary->left;
  632. if (r->top < boundary->top)
  633. r->top = boundary->top;
  634. if (r->left + r->width > boundary->width)
  635. r->left = boundary->width - r->width;
  636. if (r->top + r->height > boundary->height)
  637. r->top = boundary->height - r->height;
  638. }
  639. /* v4l2_rect helper function: return true if r1 has the same size as r2 */
  640. bool rect_same_size(const struct v4l2_rect *r1, const struct v4l2_rect *r2)
  641. {
  642. return r1->width == r2->width && r1->height == r2->height;
  643. }
  644. /* v4l2_rect helper function: calculate the intersection of two rects */
  645. struct v4l2_rect rect_intersect(const struct v4l2_rect *a, const struct v4l2_rect *b)
  646. {
  647. struct v4l2_rect r;
  648. int right, bottom;
  649. r.top = max(a->top, b->top);
  650. r.left = max(a->left, b->left);
  651. bottom = min(a->top + a->height, b->top + b->height);
  652. right = min(a->left + a->width, b->left + b->width);
  653. r.height = max(0, bottom - r.top);
  654. r.width = max(0, right - r.left);
  655. return r;
  656. }
  657. /*
  658. * v4l2_rect helper function: scale rect r by to->width / from->width and
  659. * to->height / from->height.
  660. */
  661. void rect_scale(struct v4l2_rect *r, const struct v4l2_rect *from,
  662. const struct v4l2_rect *to)
  663. {
  664. if (from->width == 0 || from->height == 0) {
  665. r->left = r->top = r->width = r->height = 0;
  666. return;
  667. }
  668. r->left = (((r->left - from->left) * to->width) / from->width) & ~1;
  669. r->width = ((r->width * to->width) / from->width) & ~1;
  670. r->top = ((r->top - from->top) * to->height) / from->height;
  671. r->height = (r->height * to->height) / from->height;
  672. }
  673. bool rect_overlap(const struct v4l2_rect *r1, const struct v4l2_rect *r2)
  674. {
  675. /*
  676. * IF the left side of r1 is to the right of the right side of r2 OR
  677. * the left side of r2 is to the right of the right side of r1 THEN
  678. * they do not overlap.
  679. */
  680. if (r1->left >= r2->left + r2->width ||
  681. r2->left >= r1->left + r1->width)
  682. return false;
  683. /*
  684. * IF the top side of r1 is below the bottom of r2 OR
  685. * the top side of r2 is below the bottom of r1 THEN
  686. * they do not overlap.
  687. */
  688. if (r1->top >= r2->top + r2->height ||
  689. r2->top >= r1->top + r1->height)
  690. return false;
  691. return true;
  692. }
  693. int vivid_vid_adjust_sel(unsigned flags, struct v4l2_rect *r)
  694. {
  695. unsigned w = r->width;
  696. unsigned h = r->height;
  697. /* sanitize w and h in case someone passes ~0 as the value */
  698. w &= 0xffff;
  699. h &= 0xffff;
  700. if (!(flags & V4L2_SEL_FLAG_LE)) {
  701. w++;
  702. h++;
  703. if (w < 2)
  704. w = 2;
  705. if (h < 2)
  706. h = 2;
  707. }
  708. if (!(flags & V4L2_SEL_FLAG_GE)) {
  709. if (w > MAX_WIDTH)
  710. w = MAX_WIDTH;
  711. if (h > MAX_HEIGHT)
  712. h = MAX_HEIGHT;
  713. }
  714. w = w & ~1;
  715. h = h & ~1;
  716. if (w < 2 || h < 2)
  717. return -ERANGE;
  718. if (w > MAX_WIDTH || h > MAX_HEIGHT)
  719. return -ERANGE;
  720. if (r->top < 0)
  721. r->top = 0;
  722. if (r->left < 0)
  723. r->left = 0;
  724. /* sanitize left and top in case someone passes ~0 as the value */
  725. r->left &= 0xfffe;
  726. r->top &= 0xfffe;
  727. if (r->left + w > MAX_WIDTH)
  728. r->left = MAX_WIDTH - w;
  729. if (r->top + h > MAX_HEIGHT)
  730. r->top = MAX_HEIGHT - h;
  731. if ((flags & (V4L2_SEL_FLAG_GE | V4L2_SEL_FLAG_LE)) ==
  732. (V4L2_SEL_FLAG_GE | V4L2_SEL_FLAG_LE) &&
  733. (r->width != w || r->height != h))
  734. return -ERANGE;
  735. r->width = w;
  736. r->height = h;
  737. return 0;
  738. }
  739. int vivid_enum_fmt_vid(struct file *file, void *priv,
  740. struct v4l2_fmtdesc *f)
  741. {
  742. struct vivid_dev *dev = video_drvdata(file);
  743. const struct vivid_fmt *fmt;
  744. if (f->index >= ARRAY_SIZE(vivid_formats) -
  745. (dev->multiplanar ? 0 : VIVID_MPLANAR_FORMATS))
  746. return -EINVAL;
  747. fmt = &vivid_formats[f->index];
  748. f->pixelformat = fmt->fourcc;
  749. return 0;
  750. }
  751. int vidioc_enum_fmt_vid_mplane(struct file *file, void *priv,
  752. struct v4l2_fmtdesc *f)
  753. {
  754. struct vivid_dev *dev = video_drvdata(file);
  755. if (!dev->multiplanar)
  756. return -ENOTTY;
  757. return vivid_enum_fmt_vid(file, priv, f);
  758. }
  759. int vidioc_enum_fmt_vid(struct file *file, void *priv,
  760. struct v4l2_fmtdesc *f)
  761. {
  762. struct vivid_dev *dev = video_drvdata(file);
  763. if (dev->multiplanar)
  764. return -ENOTTY;
  765. return vivid_enum_fmt_vid(file, priv, f);
  766. }
  767. int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
  768. {
  769. struct vivid_dev *dev = video_drvdata(file);
  770. struct video_device *vdev = video_devdata(file);
  771. if (vdev->vfl_dir == VFL_DIR_RX) {
  772. if (!vivid_is_sdtv_cap(dev))
  773. return -ENODATA;
  774. *id = dev->std_cap;
  775. } else {
  776. if (!vivid_is_svid_out(dev))
  777. return -ENODATA;
  778. *id = dev->std_out;
  779. }
  780. return 0;
  781. }
  782. int vidioc_g_dv_timings(struct file *file, void *_fh,
  783. struct v4l2_dv_timings *timings)
  784. {
  785. struct vivid_dev *dev = video_drvdata(file);
  786. struct video_device *vdev = video_devdata(file);
  787. if (vdev->vfl_dir == VFL_DIR_RX) {
  788. if (!vivid_is_hdmi_cap(dev))
  789. return -ENODATA;
  790. *timings = dev->dv_timings_cap;
  791. } else {
  792. if (!vivid_is_hdmi_out(dev))
  793. return -ENODATA;
  794. *timings = dev->dv_timings_out;
  795. }
  796. return 0;
  797. }
  798. int vidioc_enum_dv_timings(struct file *file, void *_fh,
  799. struct v4l2_enum_dv_timings *timings)
  800. {
  801. struct vivid_dev *dev = video_drvdata(file);
  802. struct video_device *vdev = video_devdata(file);
  803. if (vdev->vfl_dir == VFL_DIR_RX) {
  804. if (!vivid_is_hdmi_cap(dev))
  805. return -ENODATA;
  806. } else {
  807. if (!vivid_is_hdmi_out(dev))
  808. return -ENODATA;
  809. }
  810. return v4l2_enum_dv_timings_cap(timings, &vivid_dv_timings_cap,
  811. NULL, NULL);
  812. }
  813. int vidioc_dv_timings_cap(struct file *file, void *_fh,
  814. struct v4l2_dv_timings_cap *cap)
  815. {
  816. struct vivid_dev *dev = video_drvdata(file);
  817. struct video_device *vdev = video_devdata(file);
  818. if (vdev->vfl_dir == VFL_DIR_RX) {
  819. if (!vivid_is_hdmi_cap(dev))
  820. return -ENODATA;
  821. } else {
  822. if (!vivid_is_hdmi_out(dev))
  823. return -ENODATA;
  824. }
  825. *cap = vivid_dv_timings_cap;
  826. return 0;
  827. }
  828. int vidioc_g_edid(struct file *file, void *_fh,
  829. struct v4l2_edid *edid)
  830. {
  831. struct vivid_dev *dev = video_drvdata(file);
  832. struct video_device *vdev = video_devdata(file);
  833. memset(edid->reserved, 0, sizeof(edid->reserved));
  834. if (vdev->vfl_dir == VFL_DIR_RX) {
  835. if (edid->pad >= dev->num_inputs)
  836. return -EINVAL;
  837. if (dev->input_type[edid->pad] != HDMI)
  838. return -EINVAL;
  839. } else {
  840. if (edid->pad >= dev->num_outputs)
  841. return -EINVAL;
  842. if (dev->output_type[edid->pad] != HDMI)
  843. return -EINVAL;
  844. }
  845. if (edid->start_block == 0 && edid->blocks == 0) {
  846. edid->blocks = dev->edid_blocks;
  847. return 0;
  848. }
  849. if (dev->edid_blocks == 0)
  850. return -ENODATA;
  851. if (edid->start_block >= dev->edid_blocks)
  852. return -EINVAL;
  853. if (edid->start_block + edid->blocks > dev->edid_blocks)
  854. edid->blocks = dev->edid_blocks - edid->start_block;
  855. memcpy(edid->edid, dev->edid, edid->blocks * 128);
  856. return 0;
  857. }