yuv2rgb.c 43 KB

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