yuv2rgb.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. /*
  2. * software YUV to RGB converter
  3. *
  4. * Copyright (C) 2009 Konstantin Shishkov
  5. *
  6. * MMX/MMX2 template stuff (needed for fast movntq support),
  7. * 1,4,8bpp support and context / deglobalize stuff
  8. * by Michael Niedermayer (michaelni@gmx.at)
  9. *
  10. * This file is part of FFmpeg.
  11. *
  12. * FFmpeg is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU Lesser General Public
  14. * License as published by the Free Software Foundation; either
  15. * version 2.1 of the License, or (at your option) any later version.
  16. *
  17. * FFmpeg is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. * Lesser General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Lesser General Public
  23. * License along with FFmpeg; if not, write to the Free Software
  24. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  25. */
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <inttypes.h>
  29. #include <assert.h>
  30. #include "config.h"
  31. #include "rgb2rgb.h"
  32. #include "swscale.h"
  33. #include "swscale_internal.h"
  34. #define DITHER1XBPP // only for MMX
  35. extern const uint8_t dither_8x8_32[8][8];
  36. extern const uint8_t dither_8x8_73[8][8];
  37. extern const uint8_t dither_8x8_220[8][8];
  38. #if HAVE_MMX && CONFIG_GPL
  39. /* hope these constant values are cache line aligned */
  40. DECLARE_ASM_CONST(8, uint64_t, mmx_00ffw) = 0x00ff00ff00ff00ffULL;
  41. DECLARE_ASM_CONST(8, uint64_t, mmx_redmask) = 0xf8f8f8f8f8f8f8f8ULL;
  42. DECLARE_ASM_CONST(8, uint64_t, mmx_grnmask) = 0xfcfcfcfcfcfcfcfcULL;
  43. //MMX versions
  44. #undef RENAME
  45. #undef HAVE_MMX2
  46. #undef HAVE_AMD3DNOW
  47. #define HAVE_MMX2 0
  48. #define HAVE_AMD3DNOW 0
  49. #define RENAME(a) a ## _MMX
  50. #include "yuv2rgb_template.c"
  51. //MMX2 versions
  52. #undef RENAME
  53. #undef HAVE_MMX2
  54. #define HAVE_MMX2 1
  55. #define RENAME(a) a ## _MMX2
  56. #include "yuv2rgb_template.c"
  57. #endif /* HAVE_MMX && CONFIG_GPL */
  58. const int32_t ff_yuv2rgb_coeffs[8][4] = {
  59. {117504, 138453, 13954, 34903}, /* no sequence_display_extension */
  60. {117504, 138453, 13954, 34903}, /* ITU-R Rec. 709 (1990) */
  61. {104597, 132201, 25675, 53279}, /* unspecified */
  62. {104597, 132201, 25675, 53279}, /* reserved */
  63. {104448, 132798, 24759, 53109}, /* FCC */
  64. {104597, 132201, 25675, 53279}, /* ITU-R Rec. 624-4 System B, G */
  65. {104597, 132201, 25675, 53279}, /* SMPTE 170M */
  66. {117579, 136230, 16907, 35559} /* SMPTE 240M (1987) */
  67. };
  68. #define LOADCHROMA(i) \
  69. U = pu[i]; \
  70. V = pv[i]; \
  71. r = (void *)c->table_rV[V]; \
  72. g = (void *)(c->table_gU[U] + c->table_gV[V]); \
  73. b = (void *)c->table_bU[U];
  74. #define PUTRGB(dst,src,i,o) \
  75. Y = src[2*i+o]; \
  76. dst[2*i ] = r[Y] + g[Y] + b[Y]; \
  77. Y = src[2*i+1-o]; \
  78. dst[2*i+1] = r[Y] + g[Y] + b[Y];
  79. #define PUTRGB24(dst,src,i) \
  80. Y = src[2*i]; \
  81. dst[6*i+0] = r[Y]; dst[6*i+1] = g[Y]; dst[6*i+2] = b[Y]; \
  82. Y = src[2*i+1]; \
  83. dst[6*i+3] = r[Y]; dst[6*i+4] = g[Y]; dst[6*i+5] = b[Y];
  84. #define PUTBGR24(dst,src,i) \
  85. Y = src[2*i]; \
  86. dst[6*i+0] = b[Y]; dst[6*i+1] = g[Y]; dst[6*i+2] = r[Y]; \
  87. Y = src[2*i+1]; \
  88. dst[6*i+3] = b[Y]; dst[6*i+4] = g[Y]; dst[6*i+5] = r[Y];
  89. #define YUV2RGBFUNC(func_name, dst_type) \
  90. static int func_name(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY, \
  91. int srcSliceH, uint8_t* dst[], int dstStride[]){\
  92. int y;\
  93. \
  94. if (c->srcFormat == PIX_FMT_YUV422P) {\
  95. srcStride[1] *= 2;\
  96. srcStride[2] *= 2;\
  97. }\
  98. for (y=0; y<srcSliceH; y+=2) {\
  99. dst_type *dst_1 = (dst_type*)(dst[0] + (y+srcSliceY )*dstStride[0]);\
  100. dst_type *dst_2 = (dst_type*)(dst[0] + (y+srcSliceY+1)*dstStride[0]);\
  101. dst_type av_unused *r, *b;\
  102. dst_type *g;\
  103. uint8_t *py_1 = src[0] + y*srcStride[0];\
  104. uint8_t *py_2 = py_1 + srcStride[0];\
  105. uint8_t *pu = src[1] + (y>>1)*srcStride[1];\
  106. uint8_t *pv = src[2] + (y>>1)*srcStride[2];\
  107. unsigned int h_size = c->dstW>>3;\
  108. while (h_size--) {\
  109. int av_unused U, V;\
  110. int Y;\
  111. #define ENDYUV2RGBLINE(dst_delta)\
  112. pu += 4;\
  113. pv += 4;\
  114. py_1 += 8;\
  115. py_2 += 8;\
  116. dst_1 += dst_delta;\
  117. dst_2 += dst_delta;\
  118. }\
  119. if (c->dstW & 4) {\
  120. int av_unused Y, U, V;\
  121. #define ENDYUV2RGBFUNC()\
  122. }\
  123. }\
  124. return srcSliceH;\
  125. }
  126. #define CLOSEYUV2RGBFUNC(dst_delta)\
  127. ENDYUV2RGBLINE(dst_delta)\
  128. ENDYUV2RGBFUNC()
  129. YUV2RGBFUNC(yuv2rgb_c_32, uint32_t)
  130. LOADCHROMA(0);
  131. PUTRGB(dst_1,py_1,0,0);
  132. PUTRGB(dst_2,py_2,0,1);
  133. LOADCHROMA(1);
  134. PUTRGB(dst_2,py_2,1,1);
  135. PUTRGB(dst_1,py_1,1,0);
  136. LOADCHROMA(1);
  137. PUTRGB(dst_2,py_2,1,1);
  138. PUTRGB(dst_1,py_1,1,0);
  139. LOADCHROMA(2);
  140. PUTRGB(dst_1,py_1,2,0);
  141. PUTRGB(dst_2,py_2,2,1);
  142. LOADCHROMA(3);
  143. PUTRGB(dst_2,py_2,3,1);
  144. PUTRGB(dst_1,py_1,3,0);
  145. ENDYUV2RGBLINE(8)
  146. LOADCHROMA(0);
  147. PUTRGB(dst_1,py_1,0,0);
  148. PUTRGB(dst_2,py_2,0,1);
  149. LOADCHROMA(1);
  150. PUTRGB(dst_2,py_2,1,1);
  151. PUTRGB(dst_1,py_1,1,0);
  152. ENDYUV2RGBFUNC()
  153. YUV2RGBFUNC(yuv2rgb_c_24_rgb, uint8_t)
  154. LOADCHROMA(0);
  155. PUTRGB24(dst_1,py_1,0);
  156. PUTRGB24(dst_2,py_2,0);
  157. LOADCHROMA(1);
  158. PUTRGB24(dst_2,py_2,1);
  159. PUTRGB24(dst_1,py_1,1);
  160. LOADCHROMA(2);
  161. PUTRGB24(dst_1,py_1,2);
  162. PUTRGB24(dst_2,py_2,2);
  163. LOADCHROMA(3);
  164. PUTRGB24(dst_2,py_2,3);
  165. PUTRGB24(dst_1,py_1,3);
  166. ENDYUV2RGBLINE(24)
  167. LOADCHROMA(0);
  168. PUTRGB24(dst_1,py_1,0);
  169. PUTRGB24(dst_2,py_2,0);
  170. LOADCHROMA(1);
  171. PUTRGB24(dst_2,py_2,1);
  172. PUTRGB24(dst_1,py_1,1);
  173. ENDYUV2RGBFUNC()
  174. // only trivial mods from yuv2rgb_c_24_rgb
  175. YUV2RGBFUNC(yuv2rgb_c_24_bgr, uint8_t)
  176. LOADCHROMA(0);
  177. PUTBGR24(dst_1,py_1,0);
  178. PUTBGR24(dst_2,py_2,0);
  179. LOADCHROMA(1);
  180. PUTBGR24(dst_2,py_2,1);
  181. PUTBGR24(dst_1,py_1,1);
  182. LOADCHROMA(2);
  183. PUTBGR24(dst_1,py_1,2);
  184. PUTBGR24(dst_2,py_2,2);
  185. LOADCHROMA(3);
  186. PUTBGR24(dst_2,py_2,3);
  187. PUTBGR24(dst_1,py_1,3);
  188. ENDYUV2RGBLINE(24)
  189. LOADCHROMA(0);
  190. PUTBGR24(dst_1,py_1,0);
  191. PUTBGR24(dst_2,py_2,0);
  192. LOADCHROMA(1);
  193. PUTBGR24(dst_2,py_2,1);
  194. PUTBGR24(dst_1,py_1,1);
  195. ENDYUV2RGBFUNC()
  196. // This is exactly the same code as yuv2rgb_c_32 except for the types of
  197. // r, g, b, dst_1, dst_2
  198. YUV2RGBFUNC(yuv2rgb_c_16, uint16_t)
  199. LOADCHROMA(0);
  200. PUTRGB(dst_1,py_1,0,0);
  201. PUTRGB(dst_2,py_2,0,1);
  202. LOADCHROMA(1);
  203. PUTRGB(dst_2,py_2,1,1);
  204. PUTRGB(dst_1,py_1,1,0);
  205. LOADCHROMA(2);
  206. PUTRGB(dst_1,py_1,2,0);
  207. PUTRGB(dst_2,py_2,2,1);
  208. LOADCHROMA(3);
  209. PUTRGB(dst_2,py_2,3,1);
  210. PUTRGB(dst_1,py_1,3,0);
  211. CLOSEYUV2RGBFUNC(8)
  212. // This is exactly the same code as yuv2rgb_c_32 except for the types of
  213. // r, g, b, dst_1, dst_2
  214. YUV2RGBFUNC(yuv2rgb_c_8, uint8_t)
  215. LOADCHROMA(0);
  216. PUTRGB(dst_1,py_1,0,0);
  217. PUTRGB(dst_2,py_2,0,1);
  218. LOADCHROMA(1);
  219. PUTRGB(dst_2,py_2,1,1);
  220. PUTRGB(dst_1,py_1,1,0);
  221. LOADCHROMA(2);
  222. PUTRGB(dst_1,py_1,2,0);
  223. PUTRGB(dst_2,py_2,2,1);
  224. LOADCHROMA(3);
  225. PUTRGB(dst_2,py_2,3,1);
  226. PUTRGB(dst_1,py_1,3,0);
  227. CLOSEYUV2RGBFUNC(8)
  228. // r, g, b, dst_1, dst_2
  229. YUV2RGBFUNC(yuv2rgb_c_8_ordered_dither, uint8_t)
  230. const uint8_t *d32 = dither_8x8_32[y&7];
  231. const uint8_t *d64 = dither_8x8_73[y&7];
  232. #define PUTRGB8(dst,src,i,o) \
  233. Y = src[2*i]; \
  234. dst[2*i] = r[Y+d32[0+o]] + g[Y+d32[0+o]] + b[Y+d64[0+o]]; \
  235. Y = src[2*i+1]; \
  236. dst[2*i+1] = r[Y+d32[1+o]] + g[Y+d32[1+o]] + b[Y+d64[1+o]];
  237. LOADCHROMA(0);
  238. PUTRGB8(dst_1,py_1,0,0);
  239. PUTRGB8(dst_2,py_2,0,0+8);
  240. LOADCHROMA(1);
  241. PUTRGB8(dst_2,py_2,1,2+8);
  242. PUTRGB8(dst_1,py_1,1,2);
  243. LOADCHROMA(2);
  244. PUTRGB8(dst_1,py_1,2,4);
  245. PUTRGB8(dst_2,py_2,2,4+8);
  246. LOADCHROMA(3);
  247. PUTRGB8(dst_2,py_2,3,6+8);
  248. PUTRGB8(dst_1,py_1,3,6);
  249. CLOSEYUV2RGBFUNC(8)
  250. // This is exactly the same code as yuv2rgb_c_32 except for the types of
  251. // r, g, b, dst_1, dst_2
  252. YUV2RGBFUNC(yuv2rgb_c_4, uint8_t)
  253. int acc;
  254. #define PUTRGB4(dst,src,i) \
  255. Y = src[2*i]; \
  256. acc = r[Y] + g[Y] + b[Y]; \
  257. Y = src[2*i+1]; \
  258. acc |= (r[Y] + g[Y] + b[Y])<<4; \
  259. dst[i] = acc;
  260. LOADCHROMA(0);
  261. PUTRGB4(dst_1,py_1,0);
  262. PUTRGB4(dst_2,py_2,0);
  263. LOADCHROMA(1);
  264. PUTRGB4(dst_2,py_2,1);
  265. PUTRGB4(dst_1,py_1,1);
  266. LOADCHROMA(2);
  267. PUTRGB4(dst_1,py_1,2);
  268. PUTRGB4(dst_2,py_2,2);
  269. LOADCHROMA(3);
  270. PUTRGB4(dst_2,py_2,3);
  271. PUTRGB4(dst_1,py_1,3);
  272. CLOSEYUV2RGBFUNC(4)
  273. YUV2RGBFUNC(yuv2rgb_c_4_ordered_dither, uint8_t)
  274. const uint8_t *d64 = dither_8x8_73[y&7];
  275. const uint8_t *d128 = dither_8x8_220[y&7];
  276. int acc;
  277. #define PUTRGB4D(dst,src,i,o) \
  278. Y = src[2*i]; \
  279. acc = r[Y+d128[0+o]] + g[Y+d64[0+o]] + b[Y+d128[0+o]]; \
  280. Y = src[2*i+1]; \
  281. acc |= (r[Y+d128[1+o]] + g[Y+d64[1+o]] + b[Y+d128[1+o]])<<4; \
  282. dst[i]= acc;
  283. LOADCHROMA(0);
  284. PUTRGB4D(dst_1,py_1,0,0);
  285. PUTRGB4D(dst_2,py_2,0,0+8);
  286. LOADCHROMA(1);
  287. PUTRGB4D(dst_2,py_2,1,2+8);
  288. PUTRGB4D(dst_1,py_1,1,2);
  289. LOADCHROMA(2);
  290. PUTRGB4D(dst_1,py_1,2,4);
  291. PUTRGB4D(dst_2,py_2,2,4+8);
  292. LOADCHROMA(3);
  293. PUTRGB4D(dst_2,py_2,3,6+8);
  294. PUTRGB4D(dst_1,py_1,3,6);
  295. CLOSEYUV2RGBFUNC(4)
  296. // This is exactly the same code as yuv2rgb_c_32 except for the types of
  297. // r, g, b, dst_1, dst_2
  298. YUV2RGBFUNC(yuv2rgb_c_4b, uint8_t)
  299. LOADCHROMA(0);
  300. PUTRGB(dst_1,py_1,0,0);
  301. PUTRGB(dst_2,py_2,0,1);
  302. LOADCHROMA(1);
  303. PUTRGB(dst_2,py_2,1,1);
  304. PUTRGB(dst_1,py_1,1,0);
  305. LOADCHROMA(2);
  306. PUTRGB(dst_1,py_1,2,0);
  307. PUTRGB(dst_2,py_2,2,1);
  308. LOADCHROMA(3);
  309. PUTRGB(dst_2,py_2,3,1);
  310. PUTRGB(dst_1,py_1,3,0);
  311. CLOSEYUV2RGBFUNC(8)
  312. YUV2RGBFUNC(yuv2rgb_c_4b_ordered_dither, uint8_t)
  313. const uint8_t *d64 = dither_8x8_73[y&7];
  314. const uint8_t *d128 = dither_8x8_220[y&7];
  315. #define PUTRGB4DB(dst,src,i,o) \
  316. Y = src[2*i]; \
  317. dst[2*i] = r[Y+d128[0+o]] + g[Y+d64[0+o]] + b[Y+d128[0+o]]; \
  318. Y = src[2*i+1]; \
  319. dst[2*i+1] = r[Y+d128[1+o]] + g[Y+d64[1+o]] + b[Y+d128[1+o]];
  320. LOADCHROMA(0);
  321. PUTRGB4DB(dst_1,py_1,0,0);
  322. PUTRGB4DB(dst_2,py_2,0,0+8);
  323. LOADCHROMA(1);
  324. PUTRGB4DB(dst_2,py_2,1,2+8);
  325. PUTRGB4DB(dst_1,py_1,1,2);
  326. LOADCHROMA(2);
  327. PUTRGB4DB(dst_1,py_1,2,4);
  328. PUTRGB4DB(dst_2,py_2,2,4+8);
  329. LOADCHROMA(3);
  330. PUTRGB4DB(dst_2,py_2,3,6+8);
  331. PUTRGB4DB(dst_1,py_1,3,6);
  332. CLOSEYUV2RGBFUNC(8)
  333. YUV2RGBFUNC(yuv2rgb_c_1_ordered_dither, uint8_t)
  334. const uint8_t *d128 = dither_8x8_220[y&7];
  335. char out_1 = 0, out_2 = 0;
  336. g= c->table_gU[128] + c->table_gV[128];
  337. #define PUTRGB1(out,src,i,o) \
  338. Y = src[2*i]; \
  339. out+= out + g[Y+d128[0+o]]; \
  340. Y = src[2*i+1]; \
  341. out+= out + g[Y+d128[1+o]];
  342. PUTRGB1(out_1,py_1,0,0);
  343. PUTRGB1(out_2,py_2,0,0+8);
  344. PUTRGB1(out_2,py_2,1,2+8);
  345. PUTRGB1(out_1,py_1,1,2);
  346. PUTRGB1(out_1,py_1,2,4);
  347. PUTRGB1(out_2,py_2,2,4+8);
  348. PUTRGB1(out_2,py_2,3,6+8);
  349. PUTRGB1(out_1,py_1,3,6);
  350. dst_1[0]= out_1;
  351. dst_2[0]= out_2;
  352. CLOSEYUV2RGBFUNC(1)
  353. SwsFunc sws_yuv2rgb_get_func_ptr(SwsContext *c)
  354. {
  355. SwsFunc t = NULL;
  356. #if (HAVE_MMX2 || HAVE_MMX) && CONFIG_GPL
  357. if (c->flags & SWS_CPU_CAPS_MMX2) {
  358. switch (c->dstFormat) {
  359. case PIX_FMT_RGB32: return yuv420_rgb32_MMX2;
  360. case PIX_FMT_BGR24: return yuv420_rgb24_MMX2;
  361. case PIX_FMT_RGB565: return yuv420_rgb16_MMX2;
  362. case PIX_FMT_RGB555: return yuv420_rgb15_MMX2;
  363. }
  364. }
  365. if (c->flags & SWS_CPU_CAPS_MMX) {
  366. switch (c->dstFormat) {
  367. case PIX_FMT_RGB32: return yuv420_rgb32_MMX;
  368. case PIX_FMT_BGR24: return yuv420_rgb24_MMX;
  369. case PIX_FMT_RGB565: return yuv420_rgb16_MMX;
  370. case PIX_FMT_RGB555: return yuv420_rgb15_MMX;
  371. }
  372. }
  373. #endif
  374. #if HAVE_VIS
  375. t = sws_yuv2rgb_init_vis(c);
  376. #endif
  377. #if CONFIG_MLIB
  378. t = sws_yuv2rgb_init_mlib(c);
  379. #endif
  380. #if HAVE_ALTIVEC
  381. if (c->flags & SWS_CPU_CAPS_ALTIVEC)
  382. t = sws_yuv2rgb_init_altivec(c);
  383. #endif
  384. #if ARCH_BFIN
  385. if (c->flags & SWS_CPU_CAPS_BFIN)
  386. t = sws_ff_bfin_yuv2rgb_get_func_ptr(c);
  387. #endif
  388. if (t)
  389. return t;
  390. av_log(c, AV_LOG_WARNING, "No accelerated colorspace conversion found.\n");
  391. switch (c->dstFormat) {
  392. case PIX_FMT_BGR32_1:
  393. case PIX_FMT_RGB32_1:
  394. case PIX_FMT_BGR32:
  395. case PIX_FMT_RGB32: return yuv2rgb_c_32;
  396. case PIX_FMT_RGB24: return yuv2rgb_c_24_rgb;
  397. case PIX_FMT_BGR24: return yuv2rgb_c_24_bgr;
  398. case PIX_FMT_RGB565:
  399. case PIX_FMT_BGR565:
  400. case PIX_FMT_RGB555:
  401. case PIX_FMT_BGR555: return yuv2rgb_c_16;
  402. case PIX_FMT_RGB8:
  403. case PIX_FMT_BGR8: return yuv2rgb_c_8_ordered_dither;
  404. case PIX_FMT_RGB4:
  405. case PIX_FMT_BGR4: return yuv2rgb_c_4_ordered_dither;
  406. case PIX_FMT_RGB4_BYTE:
  407. case PIX_FMT_BGR4_BYTE: return yuv2rgb_c_4b_ordered_dither;
  408. case PIX_FMT_MONOBLACK: return yuv2rgb_c_1_ordered_dither;
  409. default:
  410. assert(0);
  411. }
  412. return NULL;
  413. }
  414. static void fill_table(uint8_t* table[256], const int elemsize, const int inc, uint8_t *y_table)
  415. {
  416. int i;
  417. int64_t cb = 0;
  418. y_table -= elemsize * (inc >> 9);
  419. for (i = 0; i < 256; i++) {
  420. table[i] = y_table + elemsize * (cb >> 16);
  421. cb += inc;
  422. }
  423. }
  424. static void fill_gv_table(int table[256], const int elemsize, const int inc)
  425. {
  426. int i;
  427. int64_t cb = 0;
  428. int off = -(inc >> 9);
  429. for (i = 0; i < 256; i++) {
  430. table[i] = elemsize * (off + (cb >> 16));
  431. cb += inc;
  432. }
  433. }
  434. av_cold int sws_yuv2rgb_c_init_tables(SwsContext *c, const int inv_table[4], int fullRange,
  435. int brightness, int contrast, int saturation)
  436. {
  437. const int isRgb = c->dstFormat==PIX_FMT_RGB32
  438. || c->dstFormat==PIX_FMT_RGB32_1
  439. || c->dstFormat==PIX_FMT_BGR24
  440. || c->dstFormat==PIX_FMT_RGB565
  441. || c->dstFormat==PIX_FMT_RGB555
  442. || c->dstFormat==PIX_FMT_RGB8
  443. || c->dstFormat==PIX_FMT_RGB4
  444. || c->dstFormat==PIX_FMT_RGB4_BYTE
  445. || c->dstFormat==PIX_FMT_MONOBLACK;
  446. const int bpp = fmt_depth(c->dstFormat);
  447. uint8_t *y_table;
  448. uint16_t *y_table16;
  449. uint32_t *y_table32;
  450. int i, base, rbase, gbase, bbase, abase;
  451. const int yoffs = fullRange ? 384 : 326;
  452. int64_t crv = inv_table[0];
  453. int64_t cbu = inv_table[1];
  454. int64_t cgu = -inv_table[2];
  455. int64_t cgv = -inv_table[3];
  456. int64_t cy = 1<<16;
  457. int64_t oy = 0;
  458. int64_t yb = 0;
  459. if (!fullRange) {
  460. cy = (cy*255) / 219;
  461. oy = 16<<16;
  462. } else {
  463. crv = (crv*224) / 255;
  464. cbu = (cbu*224) / 255;
  465. cgu = (cgu*224) / 255;
  466. cgv = (cgv*224) / 255;
  467. }
  468. cy = (cy *contrast ) >> 16;
  469. crv = (crv*contrast * saturation) >> 32;
  470. cbu = (cbu*contrast * saturation) >> 32;
  471. cgu = (cgu*contrast * saturation) >> 32;
  472. cgv = (cgv*contrast * saturation) >> 32;
  473. oy -= 256*brightness;
  474. //scale coefficients by cy
  475. crv = ((crv << 16) + 0x8000) / cy;
  476. cbu = ((cbu << 16) + 0x8000) / cy;
  477. cgu = ((cgu << 16) + 0x8000) / cy;
  478. cgv = ((cgv << 16) + 0x8000) / cy;
  479. av_free(c->yuvTable);
  480. switch (bpp) {
  481. case 1:
  482. c->yuvTable = av_malloc(1024);
  483. y_table = c->yuvTable;
  484. yb = -(384<<16) - oy;
  485. for (i = 0; i < 1024-110; i++) {
  486. y_table[i+110] = av_clip_uint8((yb + 0x8000) >> 16) >> 7;
  487. yb += cy;
  488. }
  489. fill_table(c->table_gU, 1, cgu, y_table + yoffs);
  490. fill_gv_table(c->table_gV, 1, cgv);
  491. break;
  492. case 4:
  493. case 4|128:
  494. rbase = isRgb ? 3 : 0;
  495. gbase = 1;
  496. bbase = isRgb ? 0 : 3;
  497. c->yuvTable = av_malloc(1024*3);
  498. y_table = c->yuvTable;
  499. yb = -(384<<16) - oy;
  500. for (i = 0; i < 1024-110; i++) {
  501. int yval = av_clip_uint8((yb + 0x8000) >> 16);
  502. y_table[i+110 ] = (yval >> 7) << rbase;
  503. y_table[i+ 37+1024] = ((yval + 43) / 85) << gbase;
  504. y_table[i+110+2048] = (yval >> 7) << bbase;
  505. yb += cy;
  506. }
  507. fill_table(c->table_rV, 1, crv, y_table + yoffs);
  508. fill_table(c->table_gU, 1, cgu, y_table + yoffs + 1024);
  509. fill_table(c->table_bU, 1, cbu, y_table + yoffs + 2048);
  510. fill_gv_table(c->table_gV, 1, cgv);
  511. break;
  512. case 8:
  513. rbase = isRgb ? 5 : 0;
  514. gbase = isRgb ? 2 : 3;
  515. bbase = isRgb ? 0 : 6;
  516. c->yuvTable = av_malloc(1024*3);
  517. y_table = c->yuvTable;
  518. yb = -(384<<16) - oy;
  519. for (i = 0; i < 1024-38; i++) {
  520. int yval = av_clip_uint8((yb + 0x8000) >> 16);
  521. y_table[i+16 ] = ((yval + 18) / 36) << rbase;
  522. y_table[i+16+1024] = ((yval + 18) / 36) << gbase;
  523. y_table[i+37+2048] = ((yval + 43) / 85) << bbase;
  524. yb += cy;
  525. }
  526. fill_table(c->table_rV, 1, crv, y_table + yoffs);
  527. fill_table(c->table_gU, 1, cgu, y_table + yoffs + 1024);
  528. fill_table(c->table_bU, 1, cbu, y_table + yoffs + 2048);
  529. fill_gv_table(c->table_gV, 1, cgv);
  530. break;
  531. case 15:
  532. case 16:
  533. rbase = isRgb ? bpp - 5 : 0;
  534. gbase = 5;
  535. bbase = isRgb ? 0 : (bpp - 5);
  536. c->yuvTable = av_malloc(1024*3*2);
  537. y_table16 = c->yuvTable;
  538. yb = -(384<<16) - oy;
  539. for (i = 0; i < 1024; i++) {
  540. uint8_t yval = av_clip_uint8((yb + 0x8000) >> 16);
  541. y_table16[i ] = (yval >> 3) << rbase;
  542. y_table16[i+1024] = (yval >> (18 - bpp)) << gbase;
  543. y_table16[i+2048] = (yval >> 3) << bbase;
  544. yb += cy;
  545. }
  546. fill_table(c->table_rV, 2, crv, y_table16 + yoffs);
  547. fill_table(c->table_gU, 2, cgu, y_table16 + yoffs + 1024);
  548. fill_table(c->table_bU, 2, cbu, y_table16 + yoffs + 2048);
  549. fill_gv_table(c->table_gV, 2, cgv);
  550. break;
  551. case 24:
  552. c->yuvTable = av_malloc(1024);
  553. y_table = c->yuvTable;
  554. yb = -(384<<16) - oy;
  555. for (i = 0; i < 1024; i++) {
  556. y_table[i] = av_clip_uint8((yb + 0x8000) >> 16);
  557. yb += cy;
  558. }
  559. fill_table(c->table_rV, 1, crv, y_table + yoffs);
  560. fill_table(c->table_gU, 1, cgu, y_table + yoffs);
  561. fill_table(c->table_bU, 1, cbu, y_table + yoffs);
  562. fill_gv_table(c->table_gV, 1, cgv);
  563. break;
  564. case 32:
  565. base = (c->dstFormat == PIX_FMT_RGB32_1 || c->dstFormat == PIX_FMT_BGR32_1) ? 8 : 0;
  566. rbase = base + (isRgb ? 16 : 0);
  567. gbase = base + 8;
  568. bbase = base + (isRgb ? 0 : 16);
  569. abase = (base + 24) & 31;
  570. c->yuvTable = av_malloc(1024*3*4);
  571. y_table32 = c->yuvTable;
  572. yb = -(384<<16) - oy;
  573. for (i = 0; i < 1024; i++) {
  574. uint8_t yval = av_clip_uint8((yb + 0x8000) >> 16);
  575. y_table32[i ] = (yval << rbase) + (255 << abase);
  576. y_table32[i+1024] = yval << gbase;
  577. y_table32[i+2048] = yval << bbase;
  578. yb += cy;
  579. }
  580. fill_table(c->table_rV, 4, crv, y_table32 + yoffs);
  581. fill_table(c->table_gU, 4, cgu, y_table32 + yoffs + 1024);
  582. fill_table(c->table_bU, 4, cbu, y_table32 + yoffs + 2048);
  583. fill_gv_table(c->table_gV, 4, cgv);
  584. break;
  585. default:
  586. c->yuvTable = NULL;
  587. av_log(c, AV_LOG_ERROR, "%ibpp not supported by yuv2rgb\n", bpp);
  588. return -1;
  589. }
  590. return 0;
  591. }