yuv2rgb.c 36 KB

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