yuv2rgb.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  1. /*
  2. * software YUV to RGB converter
  3. *
  4. * Copyright (C) 2009 Konstantin Shishkov
  5. *
  6. * 1,4,8bpp support and context / deglobalize stuff
  7. * by Michael Niedermayer (michaelni@gmx.at)
  8. *
  9. * This file is part of FFmpeg.
  10. *
  11. * FFmpeg is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 2.1 of the License, or (at your option) any later version.
  15. *
  16. * FFmpeg is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with FFmpeg; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. */
  25. #include <stddef.h>
  26. #include <stdint.h>
  27. #include "libavutil/bswap.h"
  28. #include "libavutil/mem.h"
  29. #include "config.h"
  30. #include "swscale.h"
  31. #include "swscale_internal.h"
  32. #include "libavutil/pixdesc.h"
  33. /* Color space conversion coefficients for YCbCr -> RGB mapping.
  34. *
  35. * Entries are {crv, cbu, cgu, cgv}
  36. *
  37. * crv = (255 / 224) * 65536 * (1 - cr) / 0.5
  38. * cbu = (255 / 224) * 65536 * (1 - cb) / 0.5
  39. * cgu = (255 / 224) * 65536 * (cb / cg) * (1 - cb) / 0.5
  40. * cgv = (255 / 224) * 65536 * (cr / cg) * (1 - cr) / 0.5
  41. *
  42. * where Y = cr * R + cg * G + cb * B and cr + cg + cb = 1.
  43. */
  44. const int32_t ff_yuv2rgb_coeffs[11][4] = {
  45. { 104597, 132201, 25675, 53279 }, /* no sequence_display_extension */
  46. { 117489, 138438, 13975, 34925 }, /* ITU-R Rec. 709 (1990) */
  47. { 104597, 132201, 25675, 53279 }, /* unspecified */
  48. { 104597, 132201, 25675, 53279 }, /* reserved */
  49. { 104448, 132798, 24759, 53109 }, /* FCC */
  50. { 104597, 132201, 25675, 53279 }, /* ITU-R Rec. 624-4 System B, G */
  51. { 104597, 132201, 25675, 53279 }, /* SMPTE 170M */
  52. { 117579, 136230, 16907, 35559 }, /* SMPTE 240M (1987) */
  53. { 0 }, /* YCgCo */
  54. { 110013, 140363, 12277, 42626 }, /* Bt-2020-NCL */
  55. { 110013, 140363, 12277, 42626 }, /* Bt-2020-CL */
  56. };
  57. const int *sws_getCoefficients(int colorspace)
  58. {
  59. if (colorspace > 10 || colorspace < 0 || colorspace == 8)
  60. colorspace = SWS_CS_DEFAULT;
  61. return ff_yuv2rgb_coeffs[colorspace];
  62. }
  63. #define LOADCHROMA(l, i) \
  64. U = pu_##l[i]; \
  65. V = pv_##l[i]; \
  66. r = (void *)c->table_rV[V+YUVRGB_TABLE_HEADROOM]; \
  67. g = (void *)(c->table_gU[U+YUVRGB_TABLE_HEADROOM] + c->table_gV[V+YUVRGB_TABLE_HEADROOM]); \
  68. b = (void *)c->table_bU[U+YUVRGB_TABLE_HEADROOM];
  69. #define PUTRGB(l, i, abase) \
  70. Y = py_##l[2 * i]; \
  71. dst_##l[2 * i] = r[Y] + g[Y] + b[Y]; \
  72. Y = py_##l[2 * i + 1]; \
  73. dst_##l[2 * i + 1] = r[Y] + g[Y] + b[Y];
  74. #define PUTRGB24(l, i, abase) \
  75. Y = py_##l[2 * i]; \
  76. dst_##l[6 * i + 0] = r[Y]; \
  77. dst_##l[6 * i + 1] = g[Y]; \
  78. dst_##l[6 * i + 2] = b[Y]; \
  79. Y = py_##l[2 * i + 1]; \
  80. dst_##l[6 * i + 3] = r[Y]; \
  81. dst_##l[6 * i + 4] = g[Y]; \
  82. dst_##l[6 * i + 5] = b[Y];
  83. #define PUTBGR24(l, i, abase) \
  84. Y = py_##l[2 * i]; \
  85. dst_##l[6 * i + 0] = b[Y]; \
  86. dst_##l[6 * i + 1] = g[Y]; \
  87. dst_##l[6 * i + 2] = r[Y]; \
  88. Y = py_##l[2 * i + 1]; \
  89. dst_##l[6 * i + 3] = b[Y]; \
  90. dst_##l[6 * i + 4] = g[Y]; \
  91. dst_##l[6 * i + 5] = r[Y];
  92. #define PUTRGBA(l, i, abase) \
  93. Y = py_##l[2 * i]; \
  94. dst_##l[2 * i] = r[Y] + g[Y] + b[Y] + ((uint32_t)(pa_##l[2 * i]) << abase); \
  95. Y = py_##l[2 * i + 1]; \
  96. dst_##l[2 * i + 1] = r[Y] + g[Y] + b[Y] + ((uint32_t)(pa_##l[2 * i + 1]) << abase);
  97. #define PUTRGB48(l, i, abase) \
  98. Y = py_##l[ 2 * i]; \
  99. dst_##l[12 * i + 0] = dst_##l[12 * i + 1] = r[Y]; \
  100. dst_##l[12 * i + 2] = dst_##l[12 * i + 3] = g[Y]; \
  101. dst_##l[12 * i + 4] = dst_##l[12 * i + 5] = b[Y]; \
  102. Y = py_##l[ 2 * i + 1]; \
  103. dst_##l[12 * i + 6] = dst_##l[12 * i + 7] = r[Y]; \
  104. dst_##l[12 * i + 8] = dst_##l[12 * i + 9] = g[Y]; \
  105. dst_##l[12 * i + 10] = dst_##l[12 * i + 11] = b[Y];
  106. #define PUTBGR48(l, i, abase) \
  107. Y = py_##l[2 * i]; \
  108. dst_##l[12 * i + 0] = dst_##l[12 * i + 1] = b[Y]; \
  109. dst_##l[12 * i + 2] = dst_##l[12 * i + 3] = g[Y]; \
  110. dst_##l[12 * i + 4] = dst_##l[12 * i + 5] = r[Y]; \
  111. Y = py_##l[2 * i + 1]; \
  112. dst_##l[12 * i + 6] = dst_##l[12 * i + 7] = b[Y]; \
  113. dst_##l[12 * i + 8] = dst_##l[12 * i + 9] = g[Y]; \
  114. dst_##l[12 * i + 10] = dst_##l[12 * i + 11] = r[Y];
  115. #define PUTGBRP(l, i, abase) \
  116. Y = py_##l[2 * i]; \
  117. dst_##l [2 * i + 0] = g[Y]; \
  118. dst1_##l[2 * i + 0] = b[Y]; \
  119. dst2_##l[2 * i + 0] = r[Y]; \
  120. Y = py_##l[2 * i + 1]; \
  121. dst_##l [2 * i + 1] = g[Y]; \
  122. dst1_##l[2 * i + 1] = b[Y]; \
  123. dst2_##l[2 * i + 1] = r[Y];
  124. #define YUV2RGBFUNC(func_name, dst_type, alpha, yuv422, nb_dst_planes) \
  125. static int func_name(SwsInternal *c, const uint8_t *const src[], \
  126. const int srcStride[], int srcSliceY, int srcSliceH, \
  127. uint8_t *const dst[], const int dstStride[]) \
  128. { \
  129. int y; \
  130. \
  131. for (y = 0; y < srcSliceH; y += 2) { \
  132. int yd = y + srcSliceY; \
  133. dst_type *dst_1 = \
  134. (dst_type *)(dst[0] + (yd) * dstStride[0]); \
  135. dst_type *dst_2 = \
  136. (dst_type *)(dst[0] + (yd + 1) * dstStride[0]); \
  137. dst_type av_unused *dst1_1, *dst1_2, *dst2_1, *dst2_2; \
  138. dst_type av_unused *r, *g, *b; \
  139. const uint8_t *py_1 = src[0] + y * srcStride[0]; \
  140. const uint8_t *py_2 = py_1 + srcStride[0]; \
  141. const uint8_t av_unused *pu_1 = src[1] + (y >> !yuv422) * srcStride[1]; \
  142. const uint8_t av_unused *pv_1 = src[2] + (y >> !yuv422) * srcStride[2]; \
  143. const uint8_t av_unused *pu_2, *pv_2; \
  144. const uint8_t av_unused *pa_1, *pa_2; \
  145. unsigned int h_size = c->opts.dst_w >> 3; \
  146. if (nb_dst_planes > 1) { \
  147. dst1_1 = (dst_type *)(dst[1] + (yd) * dstStride[1]); \
  148. dst1_2 = (dst_type *)(dst[1] + (yd + 1) * dstStride[1]); \
  149. dst2_1 = (dst_type *)(dst[2] + (yd) * dstStride[2]); \
  150. dst2_2 = (dst_type *)(dst[2] + (yd + 1) * dstStride[2]); \
  151. } \
  152. if (yuv422) { \
  153. pu_2 = pu_1 + srcStride[1]; \
  154. pv_2 = pv_1 + srcStride[2]; \
  155. } \
  156. if (alpha) { \
  157. pa_1 = src[3] + y * srcStride[3]; \
  158. pa_2 = pa_1 + srcStride[3]; \
  159. } \
  160. while (h_size--) { \
  161. int av_unused U, V, Y; \
  162. #define ENDYUV2RGBLINE(dst_delta, ss, alpha, yuv422, nb_dst_planes) \
  163. pu_1 += 4 >> ss; \
  164. pv_1 += 4 >> ss; \
  165. if (yuv422) { \
  166. pu_2 += 4 >> ss; \
  167. pv_2 += 4 >> ss; \
  168. } \
  169. py_1 += 8 >> ss; \
  170. py_2 += 8 >> ss; \
  171. if (alpha) { \
  172. pa_1 += 8 >> ss; \
  173. pa_2 += 8 >> ss; \
  174. } \
  175. dst_1 += dst_delta >> ss; \
  176. dst_2 += dst_delta >> ss; \
  177. if (nb_dst_planes > 1) { \
  178. dst1_1 += dst_delta >> ss; \
  179. dst1_2 += dst_delta >> ss; \
  180. dst2_1 += dst_delta >> ss; \
  181. dst2_2 += dst_delta >> ss; \
  182. } \
  183. } \
  184. if (c->opts.dst_w & (4 >> ss)) { \
  185. int av_unused Y, U, V; \
  186. #define ENDYUV2RGBFUNC() \
  187. } \
  188. } \
  189. return srcSliceH; \
  190. }
  191. #define YUV420FUNC(func_name, dst_type, alpha, abase, PUTFUNC, dst_delta, nb_dst_planes) \
  192. YUV2RGBFUNC(func_name, dst_type, alpha, 0, nb_dst_planes) \
  193. LOADCHROMA(1, 0); \
  194. PUTFUNC(1, 0, abase); \
  195. PUTFUNC(2, 0, abase); \
  196. \
  197. LOADCHROMA(1, 1); \
  198. PUTFUNC(2, 1, abase); \
  199. PUTFUNC(1, 1, abase); \
  200. \
  201. LOADCHROMA(1, 2); \
  202. PUTFUNC(1, 2, abase); \
  203. PUTFUNC(2, 2, abase); \
  204. \
  205. LOADCHROMA(1, 3); \
  206. PUTFUNC(2, 3, abase); \
  207. PUTFUNC(1, 3, abase); \
  208. ENDYUV2RGBLINE(dst_delta, 0, alpha, 0, nb_dst_planes) \
  209. LOADCHROMA(1, 0); \
  210. PUTFUNC(1, 0, abase); \
  211. PUTFUNC(2, 0, abase); \
  212. \
  213. LOADCHROMA(1, 1); \
  214. PUTFUNC(2, 1, abase); \
  215. PUTFUNC(1, 1, abase); \
  216. ENDYUV2RGBLINE(dst_delta, 1, alpha, 0, nb_dst_planes) \
  217. LOADCHROMA(1, 0); \
  218. PUTFUNC(1, 0, abase); \
  219. PUTFUNC(2, 0, abase); \
  220. ENDYUV2RGBFUNC()
  221. #define YUV422FUNC(func_name, dst_type, alpha, abase, PUTFUNC, dst_delta, nb_dst_planes) \
  222. YUV2RGBFUNC(func_name, dst_type, alpha, 1, nb_dst_planes) \
  223. LOADCHROMA(1, 0); \
  224. PUTFUNC(1, 0, abase); \
  225. \
  226. LOADCHROMA(2, 0); \
  227. PUTFUNC(2, 0, abase); \
  228. \
  229. LOADCHROMA(2, 1); \
  230. PUTFUNC(2, 1, abase); \
  231. \
  232. LOADCHROMA(1, 1); \
  233. PUTFUNC(1, 1, abase); \
  234. \
  235. LOADCHROMA(1, 2); \
  236. PUTFUNC(1, 2, abase); \
  237. \
  238. LOADCHROMA(2, 2); \
  239. PUTFUNC(2, 2, abase); \
  240. \
  241. LOADCHROMA(2, 3); \
  242. PUTFUNC(2, 3, abase); \
  243. \
  244. LOADCHROMA(1, 3); \
  245. PUTFUNC(1, 3, abase); \
  246. ENDYUV2RGBLINE(dst_delta, 0, alpha, 1, nb_dst_planes) \
  247. LOADCHROMA(1, 0); \
  248. PUTFUNC(1, 0, abase); \
  249. \
  250. LOADCHROMA(2, 0); \
  251. PUTFUNC(2, 0, abase); \
  252. \
  253. LOADCHROMA(2, 1); \
  254. PUTFUNC(2, 1, abase); \
  255. \
  256. LOADCHROMA(1, 1); \
  257. PUTFUNC(1, 1, abase); \
  258. ENDYUV2RGBLINE(dst_delta, 1, alpha, 1, nb_dst_planes) \
  259. LOADCHROMA(1, 0); \
  260. PUTFUNC(1, 0, abase); \
  261. \
  262. LOADCHROMA(2, 0); \
  263. PUTFUNC(2, 0, abase); \
  264. ENDYUV2RGBFUNC()
  265. #define YUV420FUNC_DITHER(func_name, dst_type, LOADDITHER, PUTFUNC, dst_delta) \
  266. YUV2RGBFUNC(func_name, dst_type, 0, 0, 1) \
  267. LOADDITHER \
  268. \
  269. LOADCHROMA(1, 0); \
  270. PUTFUNC(1, 0, 0); \
  271. PUTFUNC(2, 0, 0 + 8); \
  272. \
  273. LOADCHROMA(1, 1); \
  274. PUTFUNC(2, 1, 2 + 8); \
  275. PUTFUNC(1, 1, 2); \
  276. \
  277. LOADCHROMA(1, 2); \
  278. PUTFUNC(1, 2, 4); \
  279. PUTFUNC(2, 2, 4 + 8); \
  280. \
  281. LOADCHROMA(1, 3); \
  282. PUTFUNC(2, 3, 6 + 8); \
  283. PUTFUNC(1, 3, 6); \
  284. ENDYUV2RGBLINE(dst_delta, 0, 0, 0, 1) \
  285. LOADDITHER \
  286. \
  287. LOADCHROMA(1, 0); \
  288. PUTFUNC(1, 0, 0); \
  289. PUTFUNC(2, 0, 0 + 8); \
  290. \
  291. LOADCHROMA(1, 1); \
  292. PUTFUNC(2, 1, 2 + 8); \
  293. PUTFUNC(1, 1, 2); \
  294. ENDYUV2RGBLINE(dst_delta, 1, 0, 0, 1) \
  295. LOADDITHER \
  296. \
  297. LOADCHROMA(1, 0); \
  298. PUTFUNC(1, 0, 0); \
  299. PUTFUNC(2, 0, 0 + 8); \
  300. ENDYUV2RGBFUNC()
  301. #define YUV422FUNC_DITHER(func_name, dst_type, LOADDITHER, PUTFUNC, dst_delta) \
  302. YUV2RGBFUNC(func_name, dst_type, 0, 1, 1) \
  303. LOADDITHER \
  304. \
  305. LOADCHROMA(1, 0); \
  306. PUTFUNC(1, 0, 0); \
  307. \
  308. LOADCHROMA(2, 0); \
  309. PUTFUNC(2, 0, 0 + 8); \
  310. \
  311. LOADCHROMA(2, 1); \
  312. PUTFUNC(2, 1, 2 + 8); \
  313. \
  314. LOADCHROMA(1, 1); \
  315. PUTFUNC(1, 1, 2); \
  316. \
  317. LOADCHROMA(1, 2); \
  318. PUTFUNC(1, 2, 4); \
  319. \
  320. LOADCHROMA(2, 2); \
  321. PUTFUNC(2, 2, 4 + 8); \
  322. \
  323. LOADCHROMA(2, 3); \
  324. PUTFUNC(2, 3, 6 + 8); \
  325. \
  326. LOADCHROMA(1, 3); \
  327. PUTFUNC(1, 3, 6); \
  328. ENDYUV2RGBLINE(dst_delta, 0, 0, 1, 1) \
  329. LOADDITHER \
  330. \
  331. LOADCHROMA(1, 0); \
  332. PUTFUNC(1, 0, 0); \
  333. \
  334. LOADCHROMA(2, 0); \
  335. PUTFUNC(2, 0, 0 + 8); \
  336. \
  337. LOADCHROMA(2, 1); \
  338. PUTFUNC(2, 1, 2 + 8); \
  339. \
  340. LOADCHROMA(1, 1); \
  341. PUTFUNC(1, 1, 2); \
  342. ENDYUV2RGBLINE(dst_delta, 1, 0, 1, 1) \
  343. LOADDITHER \
  344. \
  345. LOADCHROMA(1, 0); \
  346. PUTFUNC(1, 0, 0); \
  347. \
  348. LOADCHROMA(2, 0); \
  349. PUTFUNC(2, 0, 0 + 8); \
  350. ENDYUV2RGBFUNC()
  351. #define LOADDITHER16 \
  352. const uint8_t *d16 = ff_dither_2x2_8[y & 1]; \
  353. const uint8_t *e16 = ff_dither_2x2_4[y & 1]; \
  354. const uint8_t *f16 = ff_dither_2x2_8[(y & 1)^1];
  355. #define PUTRGB16(l, i, o) \
  356. Y = py_##l[2 * i]; \
  357. dst_##l[2 * i] = r[Y + d16[0 + o]] + \
  358. g[Y + e16[0 + o]] + \
  359. b[Y + f16[0 + o]]; \
  360. Y = py_##l[2 * i + 1]; \
  361. dst_##l[2 * i + 1] = r[Y + d16[1 + o]] + \
  362. g[Y + e16[1 + o]] + \
  363. b[Y + f16[1 + o]];
  364. #define LOADDITHER15 \
  365. const uint8_t *d16 = ff_dither_2x2_8[y & 1]; \
  366. const uint8_t *e16 = ff_dither_2x2_8[(y & 1)^1];
  367. #define PUTRGB15(l, i, o) \
  368. Y = py_##l[2 * i]; \
  369. dst_##l[2 * i] = r[Y + d16[0 + o]] + \
  370. g[Y + d16[1 + o]] + \
  371. b[Y + e16[0 + o]]; \
  372. Y = py_##l[2 * i + 1]; \
  373. dst_##l[2 * i + 1] = r[Y + d16[1 + o]] + \
  374. g[Y + d16[0 + o]] + \
  375. b[Y + e16[1 + o]];
  376. #define LOADDITHER12 \
  377. const uint8_t *d16 = ff_dither_4x4_16[y & 3];
  378. #define PUTRGB12(l, i, o) \
  379. Y = py_##l[2 * i]; \
  380. dst_##l[2 * i] = r[Y + d16[0 + o]] + \
  381. g[Y + d16[0 + o]] + \
  382. b[Y + d16[0 + o]]; \
  383. Y = py_##l[2 * i + 1]; \
  384. dst_##l[2 * i + 1] = r[Y + d16[1 + o]] + \
  385. g[Y + d16[1 + o]] + \
  386. b[Y + d16[1 + o]];
  387. #define LOADDITHER8 \
  388. const uint8_t *d32 = ff_dither_8x8_32[yd & 7]; \
  389. const uint8_t *d64 = ff_dither_8x8_73[yd & 7];
  390. #define PUTRGB8(l, i, o) \
  391. Y = py_##l[2 * i]; \
  392. dst_##l[2 * i] = r[Y + d32[0 + o]] + \
  393. g[Y + d32[0 + o]] + \
  394. b[Y + d64[0 + o]]; \
  395. Y = py_##l[2 * i + 1]; \
  396. dst_##l[2 * i + 1] = r[Y + d32[1 + o]] + \
  397. g[Y + d32[1 + o]] + \
  398. b[Y + d64[1 + o]];
  399. #define LOADDITHER4D \
  400. const uint8_t * d64 = ff_dither_8x8_73[yd & 7]; \
  401. const uint8_t *d128 = ff_dither_8x8_220[yd & 7]; \
  402. int acc;
  403. #define PUTRGB4D(l, i, o) \
  404. Y = py_##l[2 * i]; \
  405. acc = r[Y + d128[0 + o]] + \
  406. g[Y + d64[0 + o]] + \
  407. b[Y + d128[0 + o]]; \
  408. Y = py_##l[2 * i + 1]; \
  409. acc |= (r[Y + d128[1 + o]] + \
  410. g[Y + d64[1 + o]] + \
  411. b[Y + d128[1 + o]]) << 4; \
  412. dst_##l[i] = acc;
  413. #define LOADDITHER4DB \
  414. const uint8_t *d64 = ff_dither_8x8_73[yd & 7]; \
  415. const uint8_t *d128 = ff_dither_8x8_220[yd & 7];
  416. #define PUTRGB4DB(l, i, o) \
  417. Y = py_##l[2 * i]; \
  418. dst_##l[2 * i] = r[Y + d128[0 + o]] + \
  419. g[Y + d64[0 + o]] + \
  420. b[Y + d128[0 + o]]; \
  421. Y = py_##l[2 * i + 1]; \
  422. dst_##l[2 * i + 1] = r[Y + d128[1 + o]] + \
  423. g[Y + d64[1 + o]] + \
  424. b[Y + d128[1 + o]];
  425. YUV2RGBFUNC(yuv2rgb_c_1_ordered_dither, uint8_t, 0, 0, 1)
  426. const uint8_t *d128 = ff_dither_8x8_220[yd & 7];
  427. char out_1 = 0, out_2 = 0;
  428. g = c->table_gU[128 + YUVRGB_TABLE_HEADROOM] + c->table_gV[128 + YUVRGB_TABLE_HEADROOM];
  429. #define PUTRGB1(out, src, i, o) \
  430. Y = src[2 * i]; \
  431. out += out + g[Y + d128[0 + o]]; \
  432. Y = src[2 * i + 1]; \
  433. out += out + g[Y + d128[1 + o]];
  434. PUTRGB1(out_1, py_1, 0, 0);
  435. PUTRGB1(out_2, py_2, 0, 0 + 8);
  436. PUTRGB1(out_2, py_2, 1, 2 + 8);
  437. PUTRGB1(out_1, py_1, 1, 2);
  438. PUTRGB1(out_1, py_1, 2, 4);
  439. PUTRGB1(out_2, py_2, 2, 4 + 8);
  440. PUTRGB1(out_2, py_2, 3, 6 + 8);
  441. PUTRGB1(out_1, py_1, 3, 6);
  442. dst_1[0] = out_1;
  443. dst_2[0] = out_2;
  444. py_1 += 8;
  445. py_2 += 8;
  446. dst_1 += 1;
  447. dst_2 += 1;
  448. }
  449. if (c->opts.dst_w & 7) {
  450. int av_unused Y, U, V;
  451. int pixels_left = c->opts.dst_w & 7;
  452. const uint8_t *d128 = ff_dither_8x8_220[yd & 7];
  453. char out_1 = 0, out_2 = 0;
  454. g = c->table_gU[128 + YUVRGB_TABLE_HEADROOM] + c->table_gV[128 + YUVRGB_TABLE_HEADROOM];
  455. #define PUTRGB1_OR00(out, src, i, o) \
  456. if (pixels_left) { \
  457. PUTRGB1(out, src, i, o) \
  458. pixels_left--; \
  459. } else { \
  460. out <<= 2; \
  461. }
  462. PUTRGB1_OR00(out_1, py_1, 0, 0);
  463. PUTRGB1_OR00(out_2, py_2, 0, 0 + 8);
  464. PUTRGB1_OR00(out_2, py_2, 1, 2 + 8);
  465. PUTRGB1_OR00(out_1, py_1, 1, 2);
  466. PUTRGB1_OR00(out_1, py_1, 2, 4);
  467. PUTRGB1_OR00(out_2, py_2, 2, 4 + 8);
  468. PUTRGB1_OR00(out_2, py_2, 3, 6 + 8);
  469. PUTRGB1_OR00(out_1, py_1, 3, 6);
  470. dst_1[0] = out_1;
  471. dst_2[0] = out_2;
  472. ENDYUV2RGBFUNC()
  473. // YUV420
  474. YUV420FUNC(yuv2rgb_c_48, uint8_t, 0, 0, PUTRGB48, 48, 1)
  475. YUV420FUNC(yuv2rgb_c_bgr48, uint8_t, 0, 0, PUTBGR48, 48, 1)
  476. YUV420FUNC(yuv2rgb_c_32, uint32_t, 0, 0, PUTRGB, 8, 1)
  477. #if HAVE_BIGENDIAN
  478. YUV420FUNC(yuva2argb_c, uint32_t, 1, 24, PUTRGBA, 8, 1)
  479. YUV420FUNC(yuva2rgba_c, uint32_t, 1, 0, PUTRGBA, 8, 1)
  480. #else
  481. YUV420FUNC(yuva2rgba_c, uint32_t, 1, 24, PUTRGBA, 8, 1)
  482. YUV420FUNC(yuva2argb_c, uint32_t, 1, 0, PUTRGBA, 8, 1)
  483. #endif
  484. YUV420FUNC(yuv2rgb_c_24_rgb, uint8_t, 0, 0, PUTRGB24, 24, 1)
  485. YUV420FUNC(yuv2rgb_c_24_bgr, uint8_t, 0, 0, PUTBGR24, 24, 1)
  486. YUV420FUNC(yuv420p_gbrp_c, uint8_t, 0, 0, PUTGBRP, 8, 3)
  487. YUV420FUNC_DITHER(yuv2rgb_c_16_ordered_dither, uint16_t, LOADDITHER16, PUTRGB16, 8)
  488. YUV420FUNC_DITHER(yuv2rgb_c_15_ordered_dither, uint16_t, LOADDITHER15, PUTRGB15, 8)
  489. YUV420FUNC_DITHER(yuv2rgb_c_12_ordered_dither, uint16_t, LOADDITHER12, PUTRGB12, 8)
  490. YUV420FUNC_DITHER(yuv2rgb_c_8_ordered_dither, uint8_t, LOADDITHER8, PUTRGB8, 8)
  491. YUV420FUNC_DITHER(yuv2rgb_c_4_ordered_dither, uint8_t, LOADDITHER4D, PUTRGB4D, 4)
  492. YUV420FUNC_DITHER(yuv2rgb_c_4b_ordered_dither, uint8_t, LOADDITHER4DB, PUTRGB4DB, 8)
  493. // YUV422
  494. YUV422FUNC(yuv422p_rgb48_c, uint8_t, 0, 0, PUTRGB48, 48, 1)
  495. YUV422FUNC(yuv422p_bgr48_c, uint8_t, 0, 0, PUTBGR48, 48, 1)
  496. YUV422FUNC(yuv422p_rgb32_c, uint32_t, 0, 0, PUTRGB, 8, 1)
  497. #if HAVE_BIGENDIAN
  498. YUV422FUNC(yuva422p_argb_c, uint32_t, 1, 24, PUTRGBA, 8, 1)
  499. YUV422FUNC(yuva422p_rgba_c, uint32_t, 1, 0, PUTRGBA, 8, 1)
  500. #else
  501. YUV422FUNC(yuva422p_rgba_c, uint32_t, 1, 24, PUTRGBA, 8, 1)
  502. YUV422FUNC(yuva422p_argb_c, uint32_t, 1, 0, PUTRGBA, 8, 1)
  503. #endif
  504. YUV422FUNC(yuv422p_rgb24_c, uint8_t, 0, 0, PUTRGB24, 24, 1)
  505. YUV422FUNC(yuv422p_bgr24_c, uint8_t, 0, 0, PUTBGR24, 24, 1)
  506. YUV422FUNC(yuv422p_gbrp_c, uint8_t, 0, 0, PUTGBRP, 8, 3)
  507. YUV422FUNC_DITHER(yuv422p_bgr16, uint16_t, LOADDITHER16, PUTRGB16, 8)
  508. YUV422FUNC_DITHER(yuv422p_bgr15, uint16_t, LOADDITHER15, PUTRGB15, 8)
  509. YUV422FUNC_DITHER(yuv422p_bgr12, uint16_t, LOADDITHER12, PUTRGB12, 8)
  510. YUV422FUNC_DITHER(yuv422p_bgr8, uint8_t, LOADDITHER8, PUTRGB8, 8)
  511. YUV422FUNC_DITHER(yuv422p_bgr4, uint8_t, LOADDITHER4D, PUTRGB4D, 4)
  512. YUV422FUNC_DITHER(yuv422p_bgr4_byte, uint8_t, LOADDITHER4DB, PUTRGB4DB, 8)
  513. SwsFunc ff_yuv2rgb_get_func_ptr(SwsInternal *c)
  514. {
  515. SwsFunc t = NULL;
  516. #if ARCH_PPC
  517. t = ff_yuv2rgb_init_ppc(c);
  518. #elif ARCH_X86
  519. t = ff_yuv2rgb_init_x86(c);
  520. #elif ARCH_LOONGARCH64
  521. t = ff_yuv2rgb_init_loongarch(c);
  522. #endif
  523. if (t)
  524. return t;
  525. av_log(c, AV_LOG_WARNING,
  526. "No accelerated colorspace conversion found from %s to %s.\n",
  527. av_get_pix_fmt_name(c->opts.src_format), av_get_pix_fmt_name(c->opts.dst_format));
  528. if (c->opts.src_format == AV_PIX_FMT_YUV422P) {
  529. switch (c->opts.dst_format) {
  530. case AV_PIX_FMT_BGR48BE:
  531. case AV_PIX_FMT_BGR48LE:
  532. return yuv422p_bgr48_c;
  533. case AV_PIX_FMT_RGB48BE:
  534. case AV_PIX_FMT_RGB48LE:
  535. return yuv422p_rgb48_c;
  536. case AV_PIX_FMT_ARGB:
  537. case AV_PIX_FMT_ABGR:
  538. if (CONFIG_SWSCALE_ALPHA && isALPHA(c->opts.src_format))
  539. return yuva422p_argb_c;
  540. case AV_PIX_FMT_RGBA:
  541. case AV_PIX_FMT_BGRA:
  542. return (CONFIG_SWSCALE_ALPHA && isALPHA(c->opts.src_format)) ? yuva422p_rgba_c : yuv422p_rgb32_c;
  543. case AV_PIX_FMT_RGB24:
  544. return yuv422p_rgb24_c;
  545. case AV_PIX_FMT_BGR24:
  546. return yuv422p_bgr24_c;
  547. case AV_PIX_FMT_RGB565:
  548. case AV_PIX_FMT_BGR565:
  549. return yuv422p_bgr16;
  550. case AV_PIX_FMT_RGB555:
  551. case AV_PIX_FMT_BGR555:
  552. return yuv422p_bgr15;
  553. case AV_PIX_FMT_RGB444:
  554. case AV_PIX_FMT_BGR444:
  555. return yuv422p_bgr12;
  556. case AV_PIX_FMT_RGB8:
  557. case AV_PIX_FMT_BGR8:
  558. return yuv422p_bgr8;
  559. case AV_PIX_FMT_RGB4:
  560. case AV_PIX_FMT_BGR4:
  561. return yuv422p_bgr4;
  562. case AV_PIX_FMT_RGB4_BYTE:
  563. case AV_PIX_FMT_BGR4_BYTE:
  564. return yuv422p_bgr4_byte;
  565. case AV_PIX_FMT_MONOBLACK:
  566. return yuv2rgb_c_1_ordered_dither;
  567. case AV_PIX_FMT_GBRP:
  568. return yuv422p_gbrp_c;
  569. }
  570. } else {
  571. switch (c->opts.dst_format) {
  572. case AV_PIX_FMT_BGR48BE:
  573. case AV_PIX_FMT_BGR48LE:
  574. return yuv2rgb_c_bgr48;
  575. case AV_PIX_FMT_RGB48BE:
  576. case AV_PIX_FMT_RGB48LE:
  577. return yuv2rgb_c_48;
  578. case AV_PIX_FMT_ARGB:
  579. case AV_PIX_FMT_ABGR:
  580. if (CONFIG_SWSCALE_ALPHA && isALPHA(c->opts.src_format))
  581. return yuva2argb_c;
  582. case AV_PIX_FMT_RGBA:
  583. case AV_PIX_FMT_BGRA:
  584. return (CONFIG_SWSCALE_ALPHA && isALPHA(c->opts.src_format)) ? yuva2rgba_c : yuv2rgb_c_32;
  585. case AV_PIX_FMT_RGB24:
  586. return yuv2rgb_c_24_rgb;
  587. case AV_PIX_FMT_BGR24:
  588. return yuv2rgb_c_24_bgr;
  589. case AV_PIX_FMT_RGB565:
  590. case AV_PIX_FMT_BGR565:
  591. return yuv2rgb_c_16_ordered_dither;
  592. case AV_PIX_FMT_RGB555:
  593. case AV_PIX_FMT_BGR555:
  594. return yuv2rgb_c_15_ordered_dither;
  595. case AV_PIX_FMT_RGB444:
  596. case AV_PIX_FMT_BGR444:
  597. return yuv2rgb_c_12_ordered_dither;
  598. case AV_PIX_FMT_RGB8:
  599. case AV_PIX_FMT_BGR8:
  600. return yuv2rgb_c_8_ordered_dither;
  601. case AV_PIX_FMT_RGB4:
  602. case AV_PIX_FMT_BGR4:
  603. return yuv2rgb_c_4_ordered_dither;
  604. case AV_PIX_FMT_RGB4_BYTE:
  605. case AV_PIX_FMT_BGR4_BYTE:
  606. return yuv2rgb_c_4b_ordered_dither;
  607. case AV_PIX_FMT_MONOBLACK:
  608. return yuv2rgb_c_1_ordered_dither;
  609. case AV_PIX_FMT_GBRP:
  610. return yuv420p_gbrp_c;
  611. }
  612. }
  613. return NULL;
  614. }
  615. static void fill_table(uint8_t* table[256 + 2*YUVRGB_TABLE_HEADROOM], const int elemsize,
  616. const int64_t inc, void *y_tab)
  617. {
  618. int i;
  619. uint8_t *y_table = y_tab;
  620. y_table -= elemsize * (inc >> 9);
  621. for (i = 0; i < 256 + 2*YUVRGB_TABLE_HEADROOM; i++) {
  622. int64_t cb = av_clip_uint8(i-YUVRGB_TABLE_HEADROOM)*inc;
  623. table[i] = y_table + elemsize * (cb >> 16);
  624. }
  625. }
  626. static void fill_gv_table(int table[256 + 2*YUVRGB_TABLE_HEADROOM], const int elemsize, const int64_t inc)
  627. {
  628. int i;
  629. int off = -(inc >> 9);
  630. for (i = 0; i < 256 + 2*YUVRGB_TABLE_HEADROOM; i++) {
  631. int64_t cb = av_clip_uint8(i-YUVRGB_TABLE_HEADROOM)*inc;
  632. table[i] = elemsize * (off + (cb >> 16));
  633. }
  634. }
  635. static uint16_t roundToInt16(int64_t f)
  636. {
  637. int r = (f + (1 << 15)) >> 16;
  638. if (r < -0x7FFF)
  639. return 0x8000;
  640. else if (r > 0x7FFF)
  641. return 0x7FFF;
  642. else
  643. return r;
  644. }
  645. av_cold int ff_yuv2rgb_c_init_tables(SwsInternal *c, const int inv_table[4],
  646. int fullRange, int brightness,
  647. int contrast, int saturation)
  648. {
  649. const int isRgb = c->opts.dst_format == AV_PIX_FMT_RGB32 ||
  650. c->opts.dst_format == AV_PIX_FMT_RGB32_1 ||
  651. c->opts.dst_format == AV_PIX_FMT_BGR24 ||
  652. c->opts.dst_format == AV_PIX_FMT_RGB565BE ||
  653. c->opts.dst_format == AV_PIX_FMT_RGB565LE ||
  654. c->opts.dst_format == AV_PIX_FMT_RGB555BE ||
  655. c->opts.dst_format == AV_PIX_FMT_RGB555LE ||
  656. c->opts.dst_format == AV_PIX_FMT_RGB444BE ||
  657. c->opts.dst_format == AV_PIX_FMT_RGB444LE ||
  658. c->opts.dst_format == AV_PIX_FMT_X2RGB10BE ||
  659. c->opts.dst_format == AV_PIX_FMT_X2RGB10LE ||
  660. c->opts.dst_format == AV_PIX_FMT_RGB8 ||
  661. c->opts.dst_format == AV_PIX_FMT_RGB4 ||
  662. c->opts.dst_format == AV_PIX_FMT_RGB4_BYTE ||
  663. c->opts.dst_format == AV_PIX_FMT_MONOBLACK;
  664. const int isNotNe = c->opts.dst_format == AV_PIX_FMT_NE(RGB565LE, RGB565BE) ||
  665. c->opts.dst_format == AV_PIX_FMT_NE(RGB555LE, RGB555BE) ||
  666. c->opts.dst_format == AV_PIX_FMT_NE(RGB444LE, RGB444BE) ||
  667. c->opts.dst_format == AV_PIX_FMT_NE(BGR565LE, BGR565BE) ||
  668. c->opts.dst_format == AV_PIX_FMT_NE(BGR555LE, BGR555BE) ||
  669. c->opts.dst_format == AV_PIX_FMT_NE(BGR444LE, BGR444BE) ||
  670. c->opts.dst_format == AV_PIX_FMT_NE(X2RGB10LE, X2RGB10BE) ||
  671. c->opts.dst_format == AV_PIX_FMT_NE(X2BGR10LE, X2BGR10BE);
  672. const int bpp = c->dstFormatBpp;
  673. uint8_t *y_table;
  674. uint16_t *y_table16;
  675. uint32_t *y_table32;
  676. int i, base, rbase, gbase, bbase, av_uninit(abase), needAlpha;
  677. const int yoffs = (fullRange ? 384 : 326) + YUVRGB_TABLE_LUMA_HEADROOM;
  678. const int table_plane_size = 1024 + 2*YUVRGB_TABLE_LUMA_HEADROOM;
  679. int64_t crv = inv_table[0];
  680. int64_t cbu = inv_table[1];
  681. int64_t cgu = -inv_table[2];
  682. int64_t cgv = -inv_table[3];
  683. int64_t cy = 1 << 16;
  684. int64_t oy = 0;
  685. int64_t yb = 0;
  686. if (!fullRange) {
  687. cy = (cy * 255) / 219;
  688. oy = 16 << 16;
  689. } else {
  690. crv = (crv * 224) / 255;
  691. cbu = (cbu * 224) / 255;
  692. cgu = (cgu * 224) / 255;
  693. cgv = (cgv * 224) / 255;
  694. }
  695. cy = (cy * contrast) >> 16;
  696. crv = (crv * contrast * saturation) >> 32;
  697. cbu = (cbu * contrast * saturation) >> 32;
  698. cgu = (cgu * contrast * saturation) >> 32;
  699. cgv = (cgv * contrast * saturation) >> 32;
  700. oy -= 256LL * brightness;
  701. c->uOffset = 0x0400040004000400LL;
  702. c->vOffset = 0x0400040004000400LL;
  703. c->yCoeff = roundToInt16(cy * (1 << 13)) * 0x0001000100010001ULL;
  704. c->vrCoeff = roundToInt16(crv * (1 << 13)) * 0x0001000100010001ULL;
  705. c->ubCoeff = roundToInt16(cbu * (1 << 13)) * 0x0001000100010001ULL;
  706. c->vgCoeff = roundToInt16(cgv * (1 << 13)) * 0x0001000100010001ULL;
  707. c->ugCoeff = roundToInt16(cgu * (1 << 13)) * 0x0001000100010001ULL;
  708. c->yOffset = roundToInt16(oy * (1 << 3)) * 0x0001000100010001ULL;
  709. c->yuv2rgb_y_coeff = (int16_t)roundToInt16(cy * (1 << 13));
  710. c->yuv2rgb_y_offset = (int16_t)roundToInt16(oy * (1 << 9));
  711. c->yuv2rgb_v2r_coeff = (int16_t)roundToInt16(crv * (1 << 13));
  712. c->yuv2rgb_v2g_coeff = (int16_t)roundToInt16(cgv * (1 << 13));
  713. c->yuv2rgb_u2g_coeff = (int16_t)roundToInt16(cgu * (1 << 13));
  714. c->yuv2rgb_u2b_coeff = (int16_t)roundToInt16(cbu * (1 << 13));
  715. //scale coefficients by cy
  716. crv = ((crv * (1 << 16)) + 0x8000) / FFMAX(cy, 1);
  717. cbu = ((cbu * (1 << 16)) + 0x8000) / FFMAX(cy, 1);
  718. cgu = ((cgu * (1 << 16)) + 0x8000) / FFMAX(cy, 1);
  719. cgv = ((cgv * (1 << 16)) + 0x8000) / FFMAX(cy, 1);
  720. av_freep(&c->yuvTable);
  721. #define ALLOC_YUV_TABLE(x) \
  722. c->yuvTable = av_malloc(x); \
  723. if (!c->yuvTable) \
  724. return AVERROR(ENOMEM);
  725. switch (bpp) {
  726. case 1:
  727. ALLOC_YUV_TABLE(table_plane_size);
  728. y_table = c->yuvTable;
  729. yb = -(384 << 16) - YUVRGB_TABLE_LUMA_HEADROOM*cy - oy;
  730. for (i = 0; i < table_plane_size - 110; i++) {
  731. y_table[i + 110] = av_clip_uint8((yb + 0x8000) >> 16) >> 7;
  732. yb += cy;
  733. }
  734. fill_table(c->table_gU, 1, cgu, y_table + yoffs);
  735. fill_gv_table(c->table_gV, 1, cgv);
  736. break;
  737. case 4:
  738. case 4 | 128:
  739. rbase = isRgb ? 3 : 0;
  740. gbase = 1;
  741. bbase = isRgb ? 0 : 3;
  742. ALLOC_YUV_TABLE(table_plane_size * 3);
  743. y_table = c->yuvTable;
  744. yb = -(384 << 16) - YUVRGB_TABLE_LUMA_HEADROOM*cy - oy;
  745. for (i = 0; i < table_plane_size - 110; i++) {
  746. int yval = av_clip_uint8((yb + 0x8000) >> 16);
  747. y_table[i + 110] = (yval >> 7) << rbase;
  748. y_table[i + 37 + table_plane_size] = ((yval + 43) / 85) << gbase;
  749. y_table[i + 110 + 2*table_plane_size] = (yval >> 7) << bbase;
  750. yb += cy;
  751. }
  752. fill_table(c->table_rV, 1, crv, y_table + yoffs);
  753. fill_table(c->table_gU, 1, cgu, y_table + yoffs + table_plane_size);
  754. fill_table(c->table_bU, 1, cbu, y_table + yoffs + 2*table_plane_size);
  755. fill_gv_table(c->table_gV, 1, cgv);
  756. break;
  757. case 8:
  758. rbase = isRgb ? 5 : 0;
  759. gbase = isRgb ? 2 : 3;
  760. bbase = isRgb ? 0 : 6;
  761. ALLOC_YUV_TABLE(table_plane_size * 3);
  762. y_table = c->yuvTable;
  763. yb = -(384 << 16) - YUVRGB_TABLE_LUMA_HEADROOM*cy - oy;
  764. for (i = 0; i < table_plane_size - 38; i++) {
  765. int yval = av_clip_uint8((yb + 0x8000) >> 16);
  766. y_table[i + 16] = ((yval + 18) / 36) << rbase;
  767. y_table[i + 16 + table_plane_size] = ((yval + 18) / 36) << gbase;
  768. y_table[i + 37 + 2*table_plane_size] = ((yval + 43) / 85) << bbase;
  769. yb += cy;
  770. }
  771. fill_table(c->table_rV, 1, crv, y_table + yoffs);
  772. fill_table(c->table_gU, 1, cgu, y_table + yoffs + table_plane_size);
  773. fill_table(c->table_bU, 1, cbu, y_table + yoffs + 2*table_plane_size);
  774. fill_gv_table(c->table_gV, 1, cgv);
  775. break;
  776. case 12:
  777. rbase = isRgb ? 8 : 0;
  778. gbase = 4;
  779. bbase = isRgb ? 0 : 8;
  780. ALLOC_YUV_TABLE(table_plane_size * 3 * 2);
  781. y_table16 = c->yuvTable;
  782. yb = -(384 << 16) - YUVRGB_TABLE_LUMA_HEADROOM*cy - oy;
  783. for (i = 0; i < table_plane_size; i++) {
  784. uint8_t yval = av_clip_uint8((yb + 0x8000) >> 16);
  785. y_table16[i] = (yval >> 4) << rbase;
  786. y_table16[i + table_plane_size] = (yval >> 4) << gbase;
  787. y_table16[i + 2*table_plane_size] = (yval >> 4) << bbase;
  788. yb += cy;
  789. }
  790. if (isNotNe)
  791. for (i = 0; i < table_plane_size * 3; i++)
  792. y_table16[i] = av_bswap16(y_table16[i]);
  793. fill_table(c->table_rV, 2, crv, y_table16 + yoffs);
  794. fill_table(c->table_gU, 2, cgu, y_table16 + yoffs + table_plane_size);
  795. fill_table(c->table_bU, 2, cbu, y_table16 + yoffs + 2*table_plane_size);
  796. fill_gv_table(c->table_gV, 2, cgv);
  797. break;
  798. case 15:
  799. case 16:
  800. rbase = isRgb ? bpp - 5 : 0;
  801. gbase = 5;
  802. bbase = isRgb ? 0 : (bpp - 5);
  803. ALLOC_YUV_TABLE(table_plane_size * 3 * 2);
  804. y_table16 = c->yuvTable;
  805. yb = -(384 << 16) - YUVRGB_TABLE_LUMA_HEADROOM*cy - oy;
  806. for (i = 0; i < table_plane_size; i++) {
  807. uint8_t yval = av_clip_uint8((yb + 0x8000) >> 16);
  808. y_table16[i] = (yval >> 3) << rbase;
  809. y_table16[i + table_plane_size] = (yval >> (18 - bpp)) << gbase;
  810. y_table16[i + 2*table_plane_size] = (yval >> 3) << bbase;
  811. yb += cy;
  812. }
  813. if (isNotNe)
  814. for (i = 0; i < table_plane_size * 3; i++)
  815. y_table16[i] = av_bswap16(y_table16[i]);
  816. fill_table(c->table_rV, 2, crv, y_table16 + yoffs);
  817. fill_table(c->table_gU, 2, cgu, y_table16 + yoffs + table_plane_size);
  818. fill_table(c->table_bU, 2, cbu, y_table16 + yoffs + 2*table_plane_size);
  819. fill_gv_table(c->table_gV, 2, cgv);
  820. break;
  821. case 24:
  822. case 48:
  823. ALLOC_YUV_TABLE(table_plane_size);
  824. y_table = c->yuvTable;
  825. yb = -(384 << 16) - YUVRGB_TABLE_LUMA_HEADROOM*cy - oy;
  826. for (i = 0; i < table_plane_size; i++) {
  827. y_table[i] = av_clip_uint8((yb + 0x8000) >> 16);
  828. yb += cy;
  829. }
  830. fill_table(c->table_rV, 1, crv, y_table + yoffs);
  831. fill_table(c->table_gU, 1, cgu, y_table + yoffs);
  832. fill_table(c->table_bU, 1, cbu, y_table + yoffs);
  833. fill_gv_table(c->table_gV, 1, cgv);
  834. break;
  835. case 30:
  836. rbase = isRgb ? 20 : 0;
  837. gbase = 10;
  838. bbase = isRgb ? 0 : 20;
  839. needAlpha = CONFIG_SWSCALE_ALPHA && isALPHA(c->opts.src_format);
  840. if (!needAlpha)
  841. abase = 30;
  842. ALLOC_YUV_TABLE(table_plane_size * 3 * 4);
  843. y_table32 = c->yuvTable;
  844. yb = -(384 << 16) - YUVRGB_TABLE_LUMA_HEADROOM*cy - oy;
  845. for (i = 0; i < table_plane_size; i++) {
  846. unsigned yval = av_clip_uintp2((yb + 0x8000) >> 14, 10);
  847. y_table32[i]= (yval << rbase) + (needAlpha ? 0 : (255u << abase));
  848. y_table32[i + table_plane_size] = yval << gbase;
  849. y_table32[i + 2 * table_plane_size] = yval << bbase;
  850. yb += cy;
  851. }
  852. if (isNotNe) {
  853. for (i = 0; i < table_plane_size * 3; i++)
  854. y_table32[i] = av_bswap32(y_table32[i]);
  855. }
  856. fill_table(c->table_rV, 4, crv, y_table32 + yoffs);
  857. fill_table(c->table_gU, 4, cgu, y_table32 + yoffs + table_plane_size);
  858. fill_table(c->table_bU, 4, cbu, y_table32 + yoffs + 2 * table_plane_size);
  859. fill_gv_table(c->table_gV, 4, cgv);
  860. break;
  861. case 32:
  862. case 64:
  863. base = (c->opts.dst_format == AV_PIX_FMT_RGB32_1 ||
  864. c->opts.dst_format == AV_PIX_FMT_BGR32_1) ? 8 : 0;
  865. rbase = base + (isRgb ? 16 : 0);
  866. gbase = base + 8;
  867. bbase = base + (isRgb ? 0 : 16);
  868. needAlpha = CONFIG_SWSCALE_ALPHA && isALPHA(c->opts.src_format);
  869. if (!needAlpha)
  870. abase = (base + 24) & 31;
  871. ALLOC_YUV_TABLE(table_plane_size * 3 * 4);
  872. y_table32 = c->yuvTable;
  873. yb = -(384 << 16) - YUVRGB_TABLE_LUMA_HEADROOM*cy - oy;
  874. for (i = 0; i < table_plane_size; i++) {
  875. unsigned yval = av_clip_uint8((yb + 0x8000) >> 16);
  876. y_table32[i] = (yval << rbase) +
  877. (needAlpha ? 0 : (255u << abase));
  878. y_table32[i + table_plane_size] = yval << gbase;
  879. y_table32[i + 2*table_plane_size] = yval << bbase;
  880. yb += cy;
  881. }
  882. fill_table(c->table_rV, 4, crv, y_table32 + yoffs);
  883. fill_table(c->table_gU, 4, cgu, y_table32 + yoffs + table_plane_size);
  884. fill_table(c->table_bU, 4, cbu, y_table32 + yoffs + 2*table_plane_size);
  885. fill_gv_table(c->table_gV, 4, cgv);
  886. break;
  887. default:
  888. if(!isPlanar(c->opts.dst_format) || bpp <= 24)
  889. av_log(c, AV_LOG_ERROR, "%ibpp not supported by yuv2rgb\n", bpp);
  890. return AVERROR(EINVAL);
  891. }
  892. return 0;
  893. }