yuv2rgb.c 32 KB

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