ipu-ic-csc.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2019 Mentor Graphics Inc.
  4. */
  5. #include <linux/types.h>
  6. #include <linux/init.h>
  7. #include <linux/errno.h>
  8. #include <linux/err.h>
  9. #include <linux/sizes.h>
  10. #include "ipu-prv.h"
  11. #define QUANT_MAP(q) \
  12. ((q) == V4L2_QUANTIZATION_FULL_RANGE || \
  13. (q) == V4L2_QUANTIZATION_DEFAULT ? 0 : 1)
  14. /* identity matrix */
  15. static const struct ipu_ic_csc_params identity = {
  16. .coeff = {
  17. { 128, 0, 0, },
  18. { 0, 128, 0, },
  19. { 0, 0, 128, },
  20. },
  21. .offset = { 0, 0, 0, },
  22. .scale = 2,
  23. };
  24. /*
  25. * RGB full-range to RGB limited-range
  26. *
  27. * R_lim = 0.8588 * R_full + 16
  28. * G_lim = 0.8588 * G_full + 16
  29. * B_lim = 0.8588 * B_full + 16
  30. */
  31. static const struct ipu_ic_csc_params rgbf2rgbl = {
  32. .coeff = {
  33. { 220, 0, 0, },
  34. { 0, 220, 0, },
  35. { 0, 0, 220, },
  36. },
  37. .offset = { 64, 64, 64, },
  38. .scale = 1,
  39. };
  40. /*
  41. * RGB limited-range to RGB full-range
  42. *
  43. * R_full = 1.1644 * (R_lim - 16)
  44. * G_full = 1.1644 * (G_lim - 16)
  45. * B_full = 1.1644 * (B_lim - 16)
  46. */
  47. static const struct ipu_ic_csc_params rgbl2rgbf = {
  48. .coeff = {
  49. { 149, 0, 0, },
  50. { 0, 149, 0, },
  51. { 0, 0, 149, },
  52. },
  53. .offset = { -37, -37, -37, },
  54. .scale = 2,
  55. };
  56. /*
  57. * YUV full-range to YUV limited-range
  58. *
  59. * Y_lim = 0.8588 * Y_full + 16
  60. * Cb_lim = 0.8784 * (Cb_full - 128) + 128
  61. * Cr_lim = 0.8784 * (Cr_full - 128) + 128
  62. */
  63. static const struct ipu_ic_csc_params yuvf2yuvl = {
  64. .coeff = {
  65. { 220, 0, 0, },
  66. { 0, 225, 0, },
  67. { 0, 0, 225, },
  68. },
  69. .offset = { 64, 62, 62, },
  70. .scale = 1,
  71. .sat = true,
  72. };
  73. /*
  74. * YUV limited-range to YUV full-range
  75. *
  76. * Y_full = 1.1644 * (Y_lim - 16)
  77. * Cb_full = 1.1384 * (Cb_lim - 128) + 128
  78. * Cr_full = 1.1384 * (Cr_lim - 128) + 128
  79. */
  80. static const struct ipu_ic_csc_params yuvl2yuvf = {
  81. .coeff = {
  82. { 149, 0, 0, },
  83. { 0, 146, 0, },
  84. { 0, 0, 146, },
  85. },
  86. .offset = { -37, -35, -35, },
  87. .scale = 2,
  88. };
  89. static const struct ipu_ic_csc_params *rgb2rgb[] = {
  90. &identity,
  91. &rgbf2rgbl,
  92. &rgbl2rgbf,
  93. &identity,
  94. };
  95. static const struct ipu_ic_csc_params *yuv2yuv[] = {
  96. &identity,
  97. &yuvf2yuvl,
  98. &yuvl2yuvf,
  99. &identity,
  100. };
  101. /*
  102. * BT.601 RGB full-range to YUV full-range
  103. *
  104. * Y = .2990 * R + .5870 * G + .1140 * B
  105. * U = -.1687 * R - .3313 * G + .5000 * B + 128
  106. * V = .5000 * R - .4187 * G - .0813 * B + 128
  107. */
  108. static const struct ipu_ic_csc_params rgbf2yuvf_601 = {
  109. .coeff = {
  110. { 77, 150, 29, },
  111. { -43, -85, 128, },
  112. { 128, -107, -21, },
  113. },
  114. .offset = { 0, 512, 512, },
  115. .scale = 1,
  116. };
  117. /* BT.601 RGB full-range to YUV limited-range */
  118. static const struct ipu_ic_csc_params rgbf2yuvl_601 = {
  119. .coeff = {
  120. { 66, 129, 25, },
  121. { -38, -74, 112, },
  122. { 112, -94, -18, },
  123. },
  124. .offset = { 64, 512, 512, },
  125. .scale = 1,
  126. .sat = true,
  127. };
  128. /* BT.601 RGB limited-range to YUV full-range */
  129. static const struct ipu_ic_csc_params rgbl2yuvf_601 = {
  130. .coeff = {
  131. { 89, 175, 34, },
  132. { -50, -99, 149, },
  133. { 149, -125, -24, },
  134. },
  135. .offset = { -75, 512, 512, },
  136. .scale = 1,
  137. };
  138. /* BT.601 RGB limited-range to YUV limited-range */
  139. static const struct ipu_ic_csc_params rgbl2yuvl_601 = {
  140. .coeff = {
  141. { 77, 150, 29, },
  142. { -44, -87, 131, },
  143. { 131, -110, -21, },
  144. },
  145. .offset = { 0, 512, 512, },
  146. .scale = 1,
  147. .sat = true,
  148. };
  149. /*
  150. * BT.601 YUV full-range to RGB full-range
  151. *
  152. * R = 1. * Y + 0 * (Cb - 128) + 1.4020 * (Cr - 128)
  153. * G = 1. * Y - .3441 * (Cb - 128) - .7141 * (Cr - 128)
  154. * B = 1. * Y + 1.7720 * (Cb - 128) + 0 * (Cr - 128)
  155. *
  156. * equivalently (factoring out the offsets):
  157. *
  158. * R = 1. * Y + 0 * Cb + 1.4020 * Cr - 179.456
  159. * G = 1. * Y - .3441 * Cb - .7141 * Cr + 135.450
  160. * B = 1. * Y + 1.7720 * Cb + 0 * Cr - 226.816
  161. */
  162. static const struct ipu_ic_csc_params yuvf2rgbf_601 = {
  163. .coeff = {
  164. { 128, 0, 179, },
  165. { 128, -44, -91, },
  166. { 128, 227, 0, },
  167. },
  168. .offset = { -359, 271, -454, },
  169. .scale = 2,
  170. };
  171. /* BT.601 YUV full-range to RGB limited-range */
  172. static const struct ipu_ic_csc_params yuvf2rgbl_601 = {
  173. .coeff = {
  174. { 110, 0, 154, },
  175. { 110, -38, -78, },
  176. { 110, 195, 0, },
  177. },
  178. .offset = { -276, 265, -358, },
  179. .scale = 2,
  180. };
  181. /* BT.601 YUV limited-range to RGB full-range */
  182. static const struct ipu_ic_csc_params yuvl2rgbf_601 = {
  183. .coeff = {
  184. { 75, 0, 102, },
  185. { 75, -25, -52, },
  186. { 75, 129, 0, },
  187. },
  188. .offset = { -223, 136, -277, },
  189. .scale = 3,
  190. };
  191. /* BT.601 YUV limited-range to RGB limited-range */
  192. static const struct ipu_ic_csc_params yuvl2rgbl_601 = {
  193. .coeff = {
  194. { 128, 0, 175, },
  195. { 128, -43, -89, },
  196. { 128, 222, 0, },
  197. },
  198. .offset = { -351, 265, -443, },
  199. .scale = 2,
  200. };
  201. static const struct ipu_ic_csc_params *rgb2yuv_601[] = {
  202. &rgbf2yuvf_601,
  203. &rgbf2yuvl_601,
  204. &rgbl2yuvf_601,
  205. &rgbl2yuvl_601,
  206. };
  207. static const struct ipu_ic_csc_params *yuv2rgb_601[] = {
  208. &yuvf2rgbf_601,
  209. &yuvf2rgbl_601,
  210. &yuvl2rgbf_601,
  211. &yuvl2rgbl_601,
  212. };
  213. /*
  214. * REC.709 encoding from RGB full range to YUV full range:
  215. *
  216. * Y = .2126 * R + .7152 * G + .0722 * B
  217. * U = -.1146 * R - .3854 * G + .5000 * B + 128
  218. * V = .5000 * R - .4542 * G - .0458 * B + 128
  219. */
  220. static const struct ipu_ic_csc_params rgbf2yuvf_709 = {
  221. .coeff = {
  222. { 54, 183, 19 },
  223. { -29, -99, 128 },
  224. { 128, -116, -12 },
  225. },
  226. .offset = { 0, 512, 512 },
  227. .scale = 1,
  228. };
  229. /* Rec.709 RGB full-range to YUV limited-range */
  230. static const struct ipu_ic_csc_params rgbf2yuvl_709 = {
  231. .coeff = {
  232. { 47, 157, 16, },
  233. { -26, -87, 112, },
  234. { 112, -102, -10, },
  235. },
  236. .offset = { 64, 512, 512, },
  237. .scale = 1,
  238. .sat = true,
  239. };
  240. /* Rec.709 RGB limited-range to YUV full-range */
  241. static const struct ipu_ic_csc_params rgbl2yuvf_709 = {
  242. .coeff = {
  243. { 63, 213, 22, },
  244. { -34, -115, 149, },
  245. { 149, -135, -14, },
  246. },
  247. .offset = { -75, 512, 512, },
  248. .scale = 1,
  249. };
  250. /* Rec.709 RGB limited-range to YUV limited-range */
  251. static const struct ipu_ic_csc_params rgbl2yuvl_709 = {
  252. .coeff = {
  253. { 54, 183, 18, },
  254. { -30, -101, 131, },
  255. { 131, -119, -12, },
  256. },
  257. .offset = { 0, 512, 512, },
  258. .scale = 1,
  259. .sat = true,
  260. };
  261. /*
  262. * Inverse REC.709 encoding from YUV full range to RGB full range:
  263. *
  264. * R = 1. * Y + 0 * (Cb - 128) + 1.5748 * (Cr - 128)
  265. * G = 1. * Y - .1873 * (Cb - 128) - .4681 * (Cr - 128)
  266. * B = 1. * Y + 1.8556 * (Cb - 128) + 0 * (Cr - 128)
  267. *
  268. * equivalently (factoring out the offsets):
  269. *
  270. * R = 1. * Y + 0 * Cb + 1.5748 * Cr - 201.574
  271. * G = 1. * Y - .1873 * Cb - .4681 * Cr + 83.891
  272. * B = 1. * Y + 1.8556 * Cb + 0 * Cr - 237.517
  273. */
  274. static const struct ipu_ic_csc_params yuvf2rgbf_709 = {
  275. .coeff = {
  276. { 128, 0, 202 },
  277. { 128, -24, -60 },
  278. { 128, 238, 0 },
  279. },
  280. .offset = { -403, 168, -475 },
  281. .scale = 2,
  282. };
  283. /* Rec.709 YUV full-range to RGB limited-range */
  284. static const struct ipu_ic_csc_params yuvf2rgbl_709 = {
  285. .coeff = {
  286. { 110, 0, 173, },
  287. { 110, -21, -51, },
  288. { 110, 204, 0, },
  289. },
  290. .offset = { -314, 176, -376, },
  291. .scale = 2,
  292. };
  293. /* Rec.709 YUV limited-range to RGB full-range */
  294. static const struct ipu_ic_csc_params yuvl2rgbf_709 = {
  295. .coeff = {
  296. { 75, 0, 115, },
  297. { 75, -14, -34, },
  298. { 75, 135, 0, },
  299. },
  300. .offset = { -248, 77, -289, },
  301. .scale = 3,
  302. };
  303. /* Rec.709 YUV limited-range to RGB limited-range */
  304. static const struct ipu_ic_csc_params yuvl2rgbl_709 = {
  305. .coeff = {
  306. { 128, 0, 197, },
  307. { 128, -23, -59, },
  308. { 128, 232, 0, },
  309. },
  310. .offset = { -394, 164, -464, },
  311. .scale = 2,
  312. };
  313. static const struct ipu_ic_csc_params *rgb2yuv_709[] = {
  314. &rgbf2yuvf_709,
  315. &rgbf2yuvl_709,
  316. &rgbl2yuvf_709,
  317. &rgbl2yuvl_709,
  318. };
  319. static const struct ipu_ic_csc_params *yuv2rgb_709[] = {
  320. &yuvf2rgbf_709,
  321. &yuvf2rgbl_709,
  322. &yuvl2rgbf_709,
  323. &yuvl2rgbl_709,
  324. };
  325. static int calc_csc_coeffs(struct ipu_ic_csc *csc)
  326. {
  327. const struct ipu_ic_csc_params **params_tbl;
  328. int tbl_idx;
  329. tbl_idx = (QUANT_MAP(csc->in_cs.quant) << 1) |
  330. QUANT_MAP(csc->out_cs.quant);
  331. if (csc->in_cs.cs == csc->out_cs.cs) {
  332. csc->params = (csc->in_cs.cs == IPUV3_COLORSPACE_YUV) ?
  333. *yuv2yuv[tbl_idx] : *rgb2rgb[tbl_idx];
  334. return 0;
  335. }
  336. /* YUV <-> RGB encoding is required */
  337. switch (csc->out_cs.enc) {
  338. case V4L2_YCBCR_ENC_601:
  339. params_tbl = (csc->in_cs.cs == IPUV3_COLORSPACE_YUV) ?
  340. yuv2rgb_601 : rgb2yuv_601;
  341. break;
  342. case V4L2_YCBCR_ENC_709:
  343. params_tbl = (csc->in_cs.cs == IPUV3_COLORSPACE_YUV) ?
  344. yuv2rgb_709 : rgb2yuv_709;
  345. break;
  346. default:
  347. return -ENOTSUPP;
  348. }
  349. csc->params = *params_tbl[tbl_idx];
  350. return 0;
  351. }
  352. int __ipu_ic_calc_csc(struct ipu_ic_csc *csc)
  353. {
  354. return calc_csc_coeffs(csc);
  355. }
  356. EXPORT_SYMBOL_GPL(__ipu_ic_calc_csc);
  357. int ipu_ic_calc_csc(struct ipu_ic_csc *csc,
  358. enum v4l2_ycbcr_encoding in_enc,
  359. enum v4l2_quantization in_quant,
  360. enum ipu_color_space in_cs,
  361. enum v4l2_ycbcr_encoding out_enc,
  362. enum v4l2_quantization out_quant,
  363. enum ipu_color_space out_cs)
  364. {
  365. ipu_ic_fill_colorspace(&csc->in_cs, in_enc, in_quant, in_cs);
  366. ipu_ic_fill_colorspace(&csc->out_cs, out_enc, out_quant, out_cs);
  367. return __ipu_ic_calc_csc(csc);
  368. }
  369. EXPORT_SYMBOL_GPL(ipu_ic_calc_csc);