yuv2rgb.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  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 <assert.h>
  29. #include "config.h"
  30. #include "rgb2rgb.h"
  31. #include "swscale.h"
  32. #include "swscale_internal.h"
  33. #include "libavutil/cpu.h"
  34. #include "libavutil/bswap.h"
  35. #include "libavutil/pixdesc.h"
  36. extern const uint8_t dither_4x4_16[4][8];
  37. extern const uint8_t dither_8x8_32[8][8];
  38. extern const uint8_t dither_8x8_73[8][8];
  39. extern const uint8_t dither_8x8_220[8][8];
  40. const int32_t ff_yuv2rgb_coeffs[8][4] = {
  41. {117504, 138453, 13954, 34903}, /* no sequence_display_extension */
  42. {117504, 138453, 13954, 34903}, /* ITU-R Rec. 709 (1990) */
  43. {104597, 132201, 25675, 53279}, /* unspecified */
  44. {104597, 132201, 25675, 53279}, /* reserved */
  45. {104448, 132798, 24759, 53109}, /* FCC */
  46. {104597, 132201, 25675, 53279}, /* ITU-R Rec. 624-4 System B, G */
  47. {104597, 132201, 25675, 53279}, /* SMPTE 170M */
  48. {117579, 136230, 16907, 35559} /* SMPTE 240M (1987) */
  49. };
  50. const int *sws_getCoefficients(int colorspace)
  51. {
  52. if (colorspace > 7 || colorspace < 0)
  53. colorspace = SWS_CS_DEFAULT;
  54. return ff_yuv2rgb_coeffs[colorspace];
  55. }
  56. #define LOADCHROMA(i) \
  57. U = pu[i]; \
  58. V = pv[i]; \
  59. r = (void *)c->table_rV[V]; \
  60. g = (void *)(c->table_gU[U] + c->table_gV[V]); \
  61. b = (void *)c->table_bU[U];
  62. #define PUTRGB(dst,src,i) \
  63. Y = src[2*i]; \
  64. dst[2*i ] = r[Y] + g[Y] + b[Y]; \
  65. Y = src[2*i+1]; \
  66. dst[2*i+1] = r[Y] + g[Y] + b[Y];
  67. #define PUTRGB24(dst,src,i) \
  68. Y = src[2*i]; \
  69. dst[6*i+0] = r[Y]; dst[6*i+1] = g[Y]; dst[6*i+2] = b[Y]; \
  70. Y = src[2*i+1]; \
  71. dst[6*i+3] = r[Y]; dst[6*i+4] = g[Y]; dst[6*i+5] = b[Y];
  72. #define PUTBGR24(dst,src,i) \
  73. Y = src[2*i]; \
  74. dst[6*i+0] = b[Y]; dst[6*i+1] = g[Y]; dst[6*i+2] = r[Y]; \
  75. Y = src[2*i+1]; \
  76. dst[6*i+3] = b[Y]; dst[6*i+4] = g[Y]; dst[6*i+5] = r[Y];
  77. #define PUTRGBA(dst,ysrc,asrc,i,s) \
  78. Y = ysrc[2*i]; \
  79. dst[2*i ] = r[Y] + g[Y] + b[Y] + (asrc[2*i ]<<s); \
  80. Y = ysrc[2*i+1]; \
  81. dst[2*i+1] = r[Y] + g[Y] + b[Y] + (asrc[2*i+1]<<s);
  82. #define PUTRGB48(dst,src,i) \
  83. Y = src[2*i]; \
  84. dst[12*i+ 0] = dst[12*i+ 1] = r[Y]; \
  85. dst[12*i+ 2] = dst[12*i+ 3] = g[Y]; \
  86. dst[12*i+ 4] = dst[12*i+ 5] = b[Y]; \
  87. Y = src[2*i+1]; \
  88. dst[12*i+ 6] = dst[12*i+ 7] = r[Y]; \
  89. dst[12*i+ 8] = dst[12*i+ 9] = g[Y]; \
  90. dst[12*i+10] = dst[12*i+11] = b[Y];
  91. #define PUTBGR48(dst,src,i) \
  92. Y = src[2*i]; \
  93. dst[12*i+ 0] = dst[12*i+ 1] = b[Y]; \
  94. dst[12*i+ 2] = dst[12*i+ 3] = g[Y]; \
  95. dst[12*i+ 4] = dst[12*i+ 5] = r[Y]; \
  96. Y = src[2*i+1]; \
  97. dst[12*i+ 6] = dst[12*i+ 7] = b[Y]; \
  98. dst[12*i+ 8] = dst[12*i+ 9] = g[Y]; \
  99. dst[12*i+10] = dst[12*i+11] = r[Y];
  100. #define YUV2RGBFUNC(func_name, dst_type, alpha) \
  101. static int func_name(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY, \
  102. int srcSliceH, uint8_t* dst[], int dstStride[]) \
  103. {\
  104. int y;\
  105. \
  106. if (!alpha && c->srcFormat == PIX_FMT_YUV422P) {\
  107. srcStride[1] *= 2;\
  108. srcStride[2] *= 2;\
  109. }\
  110. for (y=0; y<srcSliceH; y+=2) {\
  111. dst_type *dst_1 = (dst_type*)(dst[0] + (y+srcSliceY )*dstStride[0]);\
  112. dst_type *dst_2 = (dst_type*)(dst[0] + (y+srcSliceY+1)*dstStride[0]);\
  113. dst_type av_unused *r, *b;\
  114. dst_type *g;\
  115. const uint8_t *py_1 = src[0] + y*srcStride[0];\
  116. const uint8_t *py_2 = py_1 + srcStride[0];\
  117. const uint8_t *pu = src[1] + (y>>1)*srcStride[1];\
  118. const uint8_t *pv = src[2] + (y>>1)*srcStride[2];\
  119. const uint8_t av_unused *pa_1, *pa_2;\
  120. unsigned int h_size = c->dstW>>3;\
  121. if (alpha) {\
  122. pa_1 = src[3] + y*srcStride[3];\
  123. pa_2 = pa_1 + srcStride[3];\
  124. }\
  125. while (h_size--) {\
  126. int av_unused U, V;\
  127. int Y;\
  128. #define ENDYUV2RGBLINE(dst_delta)\
  129. pu += 4;\
  130. pv += 4;\
  131. py_1 += 8;\
  132. py_2 += 8;\
  133. dst_1 += dst_delta;\
  134. dst_2 += dst_delta;\
  135. }\
  136. if (c->dstW & 4) {\
  137. int av_unused Y, U, V;\
  138. #define ENDYUV2RGBFUNC()\
  139. }\
  140. }\
  141. return srcSliceH;\
  142. }
  143. #define CLOSEYUV2RGBFUNC(dst_delta)\
  144. ENDYUV2RGBLINE(dst_delta)\
  145. ENDYUV2RGBFUNC()
  146. YUV2RGBFUNC(yuv2rgb_c_48, uint8_t, 0)
  147. LOADCHROMA(0);
  148. PUTRGB48(dst_1,py_1,0);
  149. PUTRGB48(dst_2,py_2,0);
  150. LOADCHROMA(1);
  151. PUTRGB48(dst_2,py_2,1);
  152. PUTRGB48(dst_1,py_1,1);
  153. LOADCHROMA(2);
  154. PUTRGB48(dst_1,py_1,2);
  155. PUTRGB48(dst_2,py_2,2);
  156. LOADCHROMA(3);
  157. PUTRGB48(dst_2,py_2,3);
  158. PUTRGB48(dst_1,py_1,3);
  159. ENDYUV2RGBLINE(48)
  160. LOADCHROMA(0);
  161. PUTRGB48(dst_1,py_1,0);
  162. PUTRGB48(dst_2,py_2,0);
  163. LOADCHROMA(1);
  164. PUTRGB48(dst_2,py_2,1);
  165. PUTRGB48(dst_1,py_1,1);
  166. ENDYUV2RGBFUNC()
  167. YUV2RGBFUNC(yuv2rgb_c_bgr48, uint8_t, 0)
  168. LOADCHROMA(0);
  169. PUTBGR48(dst_1,py_1,0);
  170. PUTBGR48(dst_2,py_2,0);
  171. LOADCHROMA(1);
  172. PUTBGR48(dst_2,py_2,1);
  173. PUTBGR48(dst_1,py_1,1);
  174. LOADCHROMA(2);
  175. PUTBGR48(dst_1,py_1,2);
  176. PUTBGR48(dst_2,py_2,2);
  177. LOADCHROMA(3);
  178. PUTBGR48(dst_2,py_2,3);
  179. PUTBGR48(dst_1,py_1,3);
  180. ENDYUV2RGBLINE(48)
  181. LOADCHROMA(0);
  182. PUTBGR48(dst_1,py_1,0);
  183. PUTBGR48(dst_2,py_2,0);
  184. LOADCHROMA(1);
  185. PUTBGR48(dst_2,py_2,1);
  186. PUTBGR48(dst_1,py_1,1);
  187. ENDYUV2RGBFUNC()
  188. YUV2RGBFUNC(yuv2rgb_c_32, uint32_t, 0)
  189. LOADCHROMA(0);
  190. PUTRGB(dst_1,py_1,0);
  191. PUTRGB(dst_2,py_2,0);
  192. LOADCHROMA(1);
  193. PUTRGB(dst_2,py_2,1);
  194. PUTRGB(dst_1,py_1,1);
  195. LOADCHROMA(2);
  196. PUTRGB(dst_1,py_1,2);
  197. PUTRGB(dst_2,py_2,2);
  198. LOADCHROMA(3);
  199. PUTRGB(dst_2,py_2,3);
  200. PUTRGB(dst_1,py_1,3);
  201. ENDYUV2RGBLINE(8)
  202. LOADCHROMA(0);
  203. PUTRGB(dst_1,py_1,0);
  204. PUTRGB(dst_2,py_2,0);
  205. LOADCHROMA(1);
  206. PUTRGB(dst_2,py_2,1);
  207. PUTRGB(dst_1,py_1,1);
  208. ENDYUV2RGBFUNC()
  209. YUV2RGBFUNC(yuva2rgba_c, uint32_t, 1)
  210. LOADCHROMA(0);
  211. PUTRGBA(dst_1,py_1,pa_1,0,24);
  212. PUTRGBA(dst_2,py_2,pa_2,0,24);
  213. LOADCHROMA(1);
  214. PUTRGBA(dst_2,py_2,pa_1,1,24);
  215. PUTRGBA(dst_1,py_1,pa_2,1,24);
  216. LOADCHROMA(2);
  217. PUTRGBA(dst_1,py_1,pa_1,2,24);
  218. PUTRGBA(dst_2,py_2,pa_2,2,24);
  219. LOADCHROMA(3);
  220. PUTRGBA(dst_2,py_2,pa_1,3,24);
  221. PUTRGBA(dst_1,py_1,pa_2,3,24);
  222. pa_1 += 8;\
  223. pa_2 += 8;\
  224. ENDYUV2RGBLINE(8)
  225. LOADCHROMA(0);
  226. PUTRGBA(dst_1,py_1,pa_1,0,24);
  227. PUTRGBA(dst_2,py_2,pa_2,0,24);
  228. LOADCHROMA(1);
  229. PUTRGBA(dst_2,py_2,pa_1,1,24);
  230. PUTRGBA(dst_1,py_1,pa_2,1,24);
  231. ENDYUV2RGBFUNC()
  232. YUV2RGBFUNC(yuva2argb_c, uint32_t, 1)
  233. LOADCHROMA(0);
  234. PUTRGBA(dst_1,py_1,pa_1,0,0);
  235. PUTRGBA(dst_2,py_2,pa_2,0,0);
  236. LOADCHROMA(1);
  237. PUTRGBA(dst_2,py_2,pa_2,1,0);
  238. PUTRGBA(dst_1,py_1,pa_1,1,0);
  239. LOADCHROMA(2);
  240. PUTRGBA(dst_1,py_1,pa_1,2,0);
  241. PUTRGBA(dst_2,py_2,pa_2,2,0);
  242. LOADCHROMA(3);
  243. PUTRGBA(dst_2,py_2,pa_2,3,0);
  244. PUTRGBA(dst_1,py_1,pa_1,3,0);
  245. pa_1 += 8;\
  246. pa_2 += 8;\
  247. ENDYUV2RGBLINE(8)
  248. LOADCHROMA(0);
  249. PUTRGBA(dst_1,py_1,pa_1,0,0);
  250. PUTRGBA(dst_2,py_2,pa_2,0,0);
  251. LOADCHROMA(1);
  252. PUTRGBA(dst_2,py_2,pa_2,1,0);
  253. PUTRGBA(dst_1,py_1,pa_1,1,0);
  254. ENDYUV2RGBFUNC()
  255. YUV2RGBFUNC(yuv2rgb_c_24_rgb, uint8_t, 0)
  256. LOADCHROMA(0);
  257. PUTRGB24(dst_1,py_1,0);
  258. PUTRGB24(dst_2,py_2,0);
  259. LOADCHROMA(1);
  260. PUTRGB24(dst_2,py_2,1);
  261. PUTRGB24(dst_1,py_1,1);
  262. LOADCHROMA(2);
  263. PUTRGB24(dst_1,py_1,2);
  264. PUTRGB24(dst_2,py_2,2);
  265. LOADCHROMA(3);
  266. PUTRGB24(dst_2,py_2,3);
  267. PUTRGB24(dst_1,py_1,3);
  268. ENDYUV2RGBLINE(24)
  269. LOADCHROMA(0);
  270. PUTRGB24(dst_1,py_1,0);
  271. PUTRGB24(dst_2,py_2,0);
  272. LOADCHROMA(1);
  273. PUTRGB24(dst_2,py_2,1);
  274. PUTRGB24(dst_1,py_1,1);
  275. ENDYUV2RGBFUNC()
  276. // only trivial mods from yuv2rgb_c_24_rgb
  277. YUV2RGBFUNC(yuv2rgb_c_24_bgr, uint8_t, 0)
  278. LOADCHROMA(0);
  279. PUTBGR24(dst_1,py_1,0);
  280. PUTBGR24(dst_2,py_2,0);
  281. LOADCHROMA(1);
  282. PUTBGR24(dst_2,py_2,1);
  283. PUTBGR24(dst_1,py_1,1);
  284. LOADCHROMA(2);
  285. PUTBGR24(dst_1,py_1,2);
  286. PUTBGR24(dst_2,py_2,2);
  287. LOADCHROMA(3);
  288. PUTBGR24(dst_2,py_2,3);
  289. PUTBGR24(dst_1,py_1,3);
  290. ENDYUV2RGBLINE(24)
  291. LOADCHROMA(0);
  292. PUTBGR24(dst_1,py_1,0);
  293. PUTBGR24(dst_2,py_2,0);
  294. LOADCHROMA(1);
  295. PUTBGR24(dst_2,py_2,1);
  296. PUTBGR24(dst_1,py_1,1);
  297. ENDYUV2RGBFUNC()
  298. // This is exactly the same code as yuv2rgb_c_32 except for the types of
  299. // r, g, b, dst_1, dst_2
  300. YUV2RGBFUNC(yuv2rgb_c_16, uint16_t, 0)
  301. LOADCHROMA(0);
  302. PUTRGB(dst_1,py_1,0);
  303. PUTRGB(dst_2,py_2,0);
  304. LOADCHROMA(1);
  305. PUTRGB(dst_2,py_2,1);
  306. PUTRGB(dst_1,py_1,1);
  307. LOADCHROMA(2);
  308. PUTRGB(dst_1,py_1,2);
  309. PUTRGB(dst_2,py_2,2);
  310. LOADCHROMA(3);
  311. PUTRGB(dst_2,py_2,3);
  312. PUTRGB(dst_1,py_1,3);
  313. CLOSEYUV2RGBFUNC(8)
  314. // r, g, b, dst_1, dst_2
  315. YUV2RGBFUNC(yuv2rgb_c_12_ordered_dither, uint16_t, 0)
  316. const uint8_t *d16 = dither_4x4_16[y&3];
  317. #define PUTRGB12(dst,src,i,o) \
  318. Y = src[2*i]; \
  319. dst[2*i] = r[Y+d16[0+o]] + g[Y+d16[0+o]] + b[Y+d16[0+o]]; \
  320. Y = src[2*i+1]; \
  321. dst[2*i+1] = r[Y+d16[1+o]] + g[Y+d16[1+o]] + b[Y+d16[1+o]];
  322. LOADCHROMA(0);
  323. PUTRGB12(dst_1,py_1,0,0);
  324. PUTRGB12(dst_2,py_2,0,0+8);
  325. LOADCHROMA(1);
  326. PUTRGB12(dst_2,py_2,1,2+8);
  327. PUTRGB12(dst_1,py_1,1,2);
  328. LOADCHROMA(2);
  329. PUTRGB12(dst_1,py_1,2,4);
  330. PUTRGB12(dst_2,py_2,2,4+8);
  331. LOADCHROMA(3);
  332. PUTRGB12(dst_2,py_2,3,6+8);
  333. PUTRGB12(dst_1,py_1,3,6);
  334. CLOSEYUV2RGBFUNC(8)
  335. // r, g, b, dst_1, dst_2
  336. YUV2RGBFUNC(yuv2rgb_c_8_ordered_dither, uint8_t, 0)
  337. const uint8_t *d32 = dither_8x8_32[y&7];
  338. const uint8_t *d64 = dither_8x8_73[y&7];
  339. #define PUTRGB8(dst,src,i,o) \
  340. Y = src[2*i]; \
  341. dst[2*i] = r[Y+d32[0+o]] + g[Y+d32[0+o]] + b[Y+d64[0+o]]; \
  342. Y = src[2*i+1]; \
  343. dst[2*i+1] = r[Y+d32[1+o]] + g[Y+d32[1+o]] + b[Y+d64[1+o]];
  344. LOADCHROMA(0);
  345. PUTRGB8(dst_1,py_1,0,0);
  346. PUTRGB8(dst_2,py_2,0,0+8);
  347. LOADCHROMA(1);
  348. PUTRGB8(dst_2,py_2,1,2+8);
  349. PUTRGB8(dst_1,py_1,1,2);
  350. LOADCHROMA(2);
  351. PUTRGB8(dst_1,py_1,2,4);
  352. PUTRGB8(dst_2,py_2,2,4+8);
  353. LOADCHROMA(3);
  354. PUTRGB8(dst_2,py_2,3,6+8);
  355. PUTRGB8(dst_1,py_1,3,6);
  356. CLOSEYUV2RGBFUNC(8)
  357. YUV2RGBFUNC(yuv2rgb_c_4_ordered_dither, uint8_t, 0)
  358. const uint8_t *d64 = dither_8x8_73[y&7];
  359. const uint8_t *d128 = dither_8x8_220[y&7];
  360. int acc;
  361. #define PUTRGB4D(dst,src,i,o) \
  362. Y = src[2*i]; \
  363. acc = r[Y+d128[0+o]] + g[Y+d64[0+o]] + b[Y+d128[0+o]]; \
  364. Y = src[2*i+1]; \
  365. acc |= (r[Y+d128[1+o]] + g[Y+d64[1+o]] + b[Y+d128[1+o]])<<4; \
  366. dst[i]= acc;
  367. LOADCHROMA(0);
  368. PUTRGB4D(dst_1,py_1,0,0);
  369. PUTRGB4D(dst_2,py_2,0,0+8);
  370. LOADCHROMA(1);
  371. PUTRGB4D(dst_2,py_2,1,2+8);
  372. PUTRGB4D(dst_1,py_1,1,2);
  373. LOADCHROMA(2);
  374. PUTRGB4D(dst_1,py_1,2,4);
  375. PUTRGB4D(dst_2,py_2,2,4+8);
  376. LOADCHROMA(3);
  377. PUTRGB4D(dst_2,py_2,3,6+8);
  378. PUTRGB4D(dst_1,py_1,3,6);
  379. CLOSEYUV2RGBFUNC(4)
  380. YUV2RGBFUNC(yuv2rgb_c_4b_ordered_dither, uint8_t, 0)
  381. const uint8_t *d64 = dither_8x8_73[y&7];
  382. const uint8_t *d128 = dither_8x8_220[y&7];
  383. #define PUTRGB4DB(dst,src,i,o) \
  384. Y = src[2*i]; \
  385. dst[2*i] = r[Y+d128[0+o]] + g[Y+d64[0+o]] + b[Y+d128[0+o]]; \
  386. Y = src[2*i+1]; \
  387. dst[2*i+1] = r[Y+d128[1+o]] + g[Y+d64[1+o]] + b[Y+d128[1+o]];
  388. LOADCHROMA(0);
  389. PUTRGB4DB(dst_1,py_1,0,0);
  390. PUTRGB4DB(dst_2,py_2,0,0+8);
  391. LOADCHROMA(1);
  392. PUTRGB4DB(dst_2,py_2,1,2+8);
  393. PUTRGB4DB(dst_1,py_1,1,2);
  394. LOADCHROMA(2);
  395. PUTRGB4DB(dst_1,py_1,2,4);
  396. PUTRGB4DB(dst_2,py_2,2,4+8);
  397. LOADCHROMA(3);
  398. PUTRGB4DB(dst_2,py_2,3,6+8);
  399. PUTRGB4DB(dst_1,py_1,3,6);
  400. CLOSEYUV2RGBFUNC(8)
  401. YUV2RGBFUNC(yuv2rgb_c_1_ordered_dither, uint8_t, 0)
  402. const uint8_t *d128 = dither_8x8_220[y&7];
  403. char out_1 = 0, out_2 = 0;
  404. g= c->table_gU[128] + c->table_gV[128];
  405. #define PUTRGB1(out,src,i,o) \
  406. Y = src[2*i]; \
  407. out+= out + g[Y+d128[0+o]]; \
  408. Y = src[2*i+1]; \
  409. out+= out + g[Y+d128[1+o]];
  410. PUTRGB1(out_1,py_1,0,0);
  411. PUTRGB1(out_2,py_2,0,0+8);
  412. PUTRGB1(out_2,py_2,1,2+8);
  413. PUTRGB1(out_1,py_1,1,2);
  414. PUTRGB1(out_1,py_1,2,4);
  415. PUTRGB1(out_2,py_2,2,4+8);
  416. PUTRGB1(out_2,py_2,3,6+8);
  417. PUTRGB1(out_1,py_1,3,6);
  418. dst_1[0]= out_1;
  419. dst_2[0]= out_2;
  420. CLOSEYUV2RGBFUNC(1)
  421. SwsFunc ff_yuv2rgb_get_func_ptr(SwsContext *c)
  422. {
  423. SwsFunc t = NULL;
  424. if (HAVE_MMX) {
  425. t = ff_yuv2rgb_init_mmx(c);
  426. } else if (HAVE_VIS) {
  427. t = ff_yuv2rgb_init_vis(c);
  428. } else if (CONFIG_MLIB) {
  429. t = ff_yuv2rgb_init_mlib(c);
  430. } else if (HAVE_ALTIVEC) {
  431. t = ff_yuv2rgb_init_altivec(c);
  432. } else if (ARCH_BFIN) {
  433. t = ff_yuv2rgb_get_func_ptr_bfin(c);
  434. }
  435. if (t)
  436. return t;
  437. av_log(c, AV_LOG_WARNING, "No accelerated colorspace conversion found from %s to %s.\n",
  438. av_get_pix_fmt_name(c->srcFormat), av_get_pix_fmt_name(c->dstFormat));
  439. switch (c->dstFormat) {
  440. case PIX_FMT_BGR48BE:
  441. case PIX_FMT_BGR48LE: return yuv2rgb_c_bgr48;
  442. case PIX_FMT_RGB48BE:
  443. case PIX_FMT_RGB48LE: return yuv2rgb_c_48;
  444. case PIX_FMT_ARGB:
  445. case PIX_FMT_ABGR: if (CONFIG_SWSCALE_ALPHA && c->srcFormat == PIX_FMT_YUVA420P) return yuva2argb_c;
  446. case PIX_FMT_RGBA:
  447. case PIX_FMT_BGRA: return (CONFIG_SWSCALE_ALPHA && c->srcFormat == PIX_FMT_YUVA420P) ? yuva2rgba_c : yuv2rgb_c_32;
  448. case PIX_FMT_RGB24: return yuv2rgb_c_24_rgb;
  449. case PIX_FMT_BGR24: return yuv2rgb_c_24_bgr;
  450. case PIX_FMT_RGB565:
  451. case PIX_FMT_BGR565:
  452. case PIX_FMT_RGB555:
  453. case PIX_FMT_BGR555: return yuv2rgb_c_16;
  454. case PIX_FMT_RGB444:
  455. case PIX_FMT_BGR444: return yuv2rgb_c_12_ordered_dither;
  456. case PIX_FMT_RGB8:
  457. case PIX_FMT_BGR8: return yuv2rgb_c_8_ordered_dither;
  458. case PIX_FMT_RGB4:
  459. case PIX_FMT_BGR4: return yuv2rgb_c_4_ordered_dither;
  460. case PIX_FMT_RGB4_BYTE:
  461. case PIX_FMT_BGR4_BYTE: return yuv2rgb_c_4b_ordered_dither;
  462. case PIX_FMT_MONOBLACK: return yuv2rgb_c_1_ordered_dither;
  463. default:
  464. assert(0);
  465. }
  466. return NULL;
  467. }
  468. static void fill_table(uint8_t* table[256], const int elemsize, const int inc, void *y_tab)
  469. {
  470. int i;
  471. int64_t cb = 0;
  472. uint8_t *y_table = y_tab;
  473. y_table -= elemsize * (inc >> 9);
  474. for (i = 0; i < 256; i++) {
  475. table[i] = y_table + elemsize * (cb >> 16);
  476. cb += inc;
  477. }
  478. }
  479. static void fill_gv_table(int table[256], const int elemsize, const int inc)
  480. {
  481. int i;
  482. int64_t cb = 0;
  483. int off = -(inc >> 9);
  484. for (i = 0; i < 256; i++) {
  485. table[i] = elemsize * (off + (cb >> 16));
  486. cb += inc;
  487. }
  488. }
  489. static uint16_t roundToInt16(int64_t f)
  490. {
  491. int r= (f + (1<<15))>>16;
  492. if (r<-0x7FFF) return 0x8000;
  493. else if (r> 0x7FFF) return 0x7FFF;
  494. else return r;
  495. }
  496. av_cold int ff_yuv2rgb_c_init_tables(SwsContext *c, const int inv_table[4], int fullRange,
  497. int brightness, int contrast, int saturation)
  498. {
  499. const int isRgb = c->dstFormat==PIX_FMT_RGB32
  500. || c->dstFormat==PIX_FMT_RGB32_1
  501. || c->dstFormat==PIX_FMT_BGR24
  502. || c->dstFormat==PIX_FMT_RGB565BE
  503. || c->dstFormat==PIX_FMT_RGB565LE
  504. || c->dstFormat==PIX_FMT_RGB555BE
  505. || c->dstFormat==PIX_FMT_RGB555LE
  506. || c->dstFormat==PIX_FMT_RGB444BE
  507. || c->dstFormat==PIX_FMT_RGB444LE
  508. || c->dstFormat==PIX_FMT_RGB8
  509. || c->dstFormat==PIX_FMT_RGB4
  510. || c->dstFormat==PIX_FMT_RGB4_BYTE
  511. || c->dstFormat==PIX_FMT_MONOBLACK;
  512. const int isNotNe = c->dstFormat==PIX_FMT_NE(RGB565LE,RGB565BE)
  513. || c->dstFormat==PIX_FMT_NE(RGB555LE,RGB555BE)
  514. || c->dstFormat==PIX_FMT_NE(RGB444LE,RGB444BE)
  515. || c->dstFormat==PIX_FMT_NE(BGR565LE,BGR565BE)
  516. || c->dstFormat==PIX_FMT_NE(BGR555LE,BGR555BE)
  517. || c->dstFormat==PIX_FMT_NE(BGR444LE,BGR444BE);
  518. const int bpp = c->dstFormatBpp;
  519. uint8_t *y_table;
  520. uint16_t *y_table16;
  521. uint32_t *y_table32;
  522. int i, base, rbase, gbase, bbase, abase, needAlpha;
  523. const int yoffs = fullRange ? 384 : 326;
  524. int64_t crv = inv_table[0];
  525. int64_t cbu = inv_table[1];
  526. int64_t cgu = -inv_table[2];
  527. int64_t cgv = -inv_table[3];
  528. int64_t cy = 1<<16;
  529. int64_t oy = 0;
  530. int64_t yb = 0;
  531. if (!fullRange) {
  532. cy = (cy*255) / 219;
  533. oy = 16<<16;
  534. } else {
  535. crv = (crv*224) / 255;
  536. cbu = (cbu*224) / 255;
  537. cgu = (cgu*224) / 255;
  538. cgv = (cgv*224) / 255;
  539. }
  540. cy = (cy *contrast ) >> 16;
  541. crv = (crv*contrast * saturation) >> 32;
  542. cbu = (cbu*contrast * saturation) >> 32;
  543. cgu = (cgu*contrast * saturation) >> 32;
  544. cgv = (cgv*contrast * saturation) >> 32;
  545. oy -= 256*brightness;
  546. c->uOffset= 0x0400040004000400LL;
  547. c->vOffset= 0x0400040004000400LL;
  548. c->yCoeff= roundToInt16(cy *8192) * 0x0001000100010001ULL;
  549. c->vrCoeff= roundToInt16(crv*8192) * 0x0001000100010001ULL;
  550. c->ubCoeff= roundToInt16(cbu*8192) * 0x0001000100010001ULL;
  551. c->vgCoeff= roundToInt16(cgv*8192) * 0x0001000100010001ULL;
  552. c->ugCoeff= roundToInt16(cgu*8192) * 0x0001000100010001ULL;
  553. c->yOffset= roundToInt16(oy * 8) * 0x0001000100010001ULL;
  554. c->yuv2rgb_y_coeff = (int16_t)roundToInt16(cy <<13);
  555. c->yuv2rgb_y_offset = (int16_t)roundToInt16(oy << 9);
  556. c->yuv2rgb_v2r_coeff= (int16_t)roundToInt16(crv<<13);
  557. c->yuv2rgb_v2g_coeff= (int16_t)roundToInt16(cgv<<13);
  558. c->yuv2rgb_u2g_coeff= (int16_t)roundToInt16(cgu<<13);
  559. c->yuv2rgb_u2b_coeff= (int16_t)roundToInt16(cbu<<13);
  560. //scale coefficients by cy
  561. crv = ((crv << 16) + 0x8000) / cy;
  562. cbu = ((cbu << 16) + 0x8000) / cy;
  563. cgu = ((cgu << 16) + 0x8000) / cy;
  564. cgv = ((cgv << 16) + 0x8000) / cy;
  565. av_free(c->yuvTable);
  566. switch (bpp) {
  567. case 1:
  568. c->yuvTable = av_malloc(1024);
  569. y_table = c->yuvTable;
  570. yb = -(384<<16) - oy;
  571. for (i = 0; i < 1024-110; i++) {
  572. y_table[i+110] = av_clip_uint8((yb + 0x8000) >> 16) >> 7;
  573. yb += cy;
  574. }
  575. fill_table(c->table_gU, 1, cgu, y_table + yoffs);
  576. fill_gv_table(c->table_gV, 1, cgv);
  577. break;
  578. case 4:
  579. case 4|128:
  580. rbase = isRgb ? 3 : 0;
  581. gbase = 1;
  582. bbase = isRgb ? 0 : 3;
  583. c->yuvTable = av_malloc(1024*3);
  584. y_table = c->yuvTable;
  585. yb = -(384<<16) - oy;
  586. for (i = 0; i < 1024-110; i++) {
  587. int yval = av_clip_uint8((yb + 0x8000) >> 16);
  588. y_table[i+110 ] = (yval >> 7) << rbase;
  589. y_table[i+ 37+1024] = ((yval + 43) / 85) << gbase;
  590. y_table[i+110+2048] = (yval >> 7) << bbase;
  591. yb += cy;
  592. }
  593. fill_table(c->table_rV, 1, crv, y_table + yoffs);
  594. fill_table(c->table_gU, 1, cgu, y_table + yoffs + 1024);
  595. fill_table(c->table_bU, 1, cbu, y_table + yoffs + 2048);
  596. fill_gv_table(c->table_gV, 1, cgv);
  597. break;
  598. case 8:
  599. rbase = isRgb ? 5 : 0;
  600. gbase = isRgb ? 2 : 3;
  601. bbase = isRgb ? 0 : 6;
  602. c->yuvTable = av_malloc(1024*3);
  603. y_table = c->yuvTable;
  604. yb = -(384<<16) - oy;
  605. for (i = 0; i < 1024-38; i++) {
  606. int yval = av_clip_uint8((yb + 0x8000) >> 16);
  607. y_table[i+16 ] = ((yval + 18) / 36) << rbase;
  608. y_table[i+16+1024] = ((yval + 18) / 36) << gbase;
  609. y_table[i+37+2048] = ((yval + 43) / 85) << bbase;
  610. yb += cy;
  611. }
  612. fill_table(c->table_rV, 1, crv, y_table + yoffs);
  613. fill_table(c->table_gU, 1, cgu, y_table + yoffs + 1024);
  614. fill_table(c->table_bU, 1, cbu, y_table + yoffs + 2048);
  615. fill_gv_table(c->table_gV, 1, cgv);
  616. break;
  617. case 12:
  618. rbase = isRgb ? 8 : 0;
  619. gbase = 4;
  620. bbase = isRgb ? 0 : 8;
  621. c->yuvTable = av_malloc(1024*3*2);
  622. y_table16 = c->yuvTable;
  623. yb = -(384<<16) - oy;
  624. for (i = 0; i < 1024; i++) {
  625. uint8_t yval = av_clip_uint8((yb + 0x8000) >> 16);
  626. y_table16[i ] = (yval >> 4) << rbase;
  627. y_table16[i+1024] = (yval >> 4) << gbase;
  628. y_table16[i+2048] = (yval >> 4) << bbase;
  629. yb += cy;
  630. }
  631. if (isNotNe)
  632. for (i = 0; i < 1024*3; i++)
  633. y_table16[i] = av_bswap16(y_table16[i]);
  634. fill_table(c->table_rV, 2, crv, y_table16 + yoffs);
  635. fill_table(c->table_gU, 2, cgu, y_table16 + yoffs + 1024);
  636. fill_table(c->table_bU, 2, cbu, y_table16 + yoffs + 2048);
  637. fill_gv_table(c->table_gV, 2, cgv);
  638. break;
  639. case 15:
  640. case 16:
  641. rbase = isRgb ? bpp - 5 : 0;
  642. gbase = 5;
  643. bbase = isRgb ? 0 : (bpp - 5);
  644. c->yuvTable = av_malloc(1024*3*2);
  645. y_table16 = c->yuvTable;
  646. yb = -(384<<16) - oy;
  647. for (i = 0; i < 1024; i++) {
  648. uint8_t yval = av_clip_uint8((yb + 0x8000) >> 16);
  649. y_table16[i ] = (yval >> 3) << rbase;
  650. y_table16[i+1024] = (yval >> (18 - bpp)) << gbase;
  651. y_table16[i+2048] = (yval >> 3) << bbase;
  652. yb += cy;
  653. }
  654. if(isNotNe)
  655. for (i = 0; i < 1024*3; i++)
  656. y_table16[i] = av_bswap16(y_table16[i]);
  657. fill_table(c->table_rV, 2, crv, y_table16 + yoffs);
  658. fill_table(c->table_gU, 2, cgu, y_table16 + yoffs + 1024);
  659. fill_table(c->table_bU, 2, cbu, y_table16 + yoffs + 2048);
  660. fill_gv_table(c->table_gV, 2, cgv);
  661. break;
  662. case 24:
  663. case 48:
  664. c->yuvTable = av_malloc(1024);
  665. y_table = c->yuvTable;
  666. yb = -(384<<16) - oy;
  667. for (i = 0; i < 1024; i++) {
  668. y_table[i] = av_clip_uint8((yb + 0x8000) >> 16);
  669. yb += cy;
  670. }
  671. fill_table(c->table_rV, 1, crv, y_table + yoffs);
  672. fill_table(c->table_gU, 1, cgu, y_table + yoffs);
  673. fill_table(c->table_bU, 1, cbu, y_table + yoffs);
  674. fill_gv_table(c->table_gV, 1, cgv);
  675. break;
  676. case 32:
  677. base = (c->dstFormat == PIX_FMT_RGB32_1 || c->dstFormat == PIX_FMT_BGR32_1) ? 8 : 0;
  678. rbase = base + (isRgb ? 16 : 0);
  679. gbase = base + 8;
  680. bbase = base + (isRgb ? 0 : 16);
  681. needAlpha = CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat);
  682. if (!needAlpha)
  683. abase = (base + 24) & 31;
  684. c->yuvTable = av_malloc(1024*3*4);
  685. y_table32 = c->yuvTable;
  686. yb = -(384<<16) - oy;
  687. for (i = 0; i < 1024; i++) {
  688. uint8_t yval = av_clip_uint8((yb + 0x8000) >> 16);
  689. y_table32[i ] = (yval << rbase) + (needAlpha ? 0 : (255 << abase));
  690. y_table32[i+1024] = yval << gbase;
  691. y_table32[i+2048] = yval << bbase;
  692. yb += cy;
  693. }
  694. fill_table(c->table_rV, 4, crv, y_table32 + yoffs);
  695. fill_table(c->table_gU, 4, cgu, y_table32 + yoffs + 1024);
  696. fill_table(c->table_bU, 4, cbu, y_table32 + yoffs + 2048);
  697. fill_gv_table(c->table_gV, 4, cgv);
  698. break;
  699. default:
  700. c->yuvTable = NULL;
  701. av_log(c, AV_LOG_ERROR, "%ibpp not supported by yuv2rgb\n", bpp);
  702. return -1;
  703. }
  704. return 0;
  705. }