yuv2rgb.c 34 KB

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