rgb2rgb.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  2. * software RGB to RGB converter
  3. * pluralize by software PAL8 to RGB converter
  4. * software YUV to YUV converter
  5. * software YUV to RGB converter
  6. * Written by Nick Kurshev.
  7. * palette & YUV & runtime CPU stuff by Michael (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 <inttypes.h>
  26. #include "libavutil/attributes.h"
  27. #include "libavutil/bswap.h"
  28. #include "config.h"
  29. #include "rgb2rgb.h"
  30. #include "swscale.h"
  31. #include "swscale_internal.h"
  32. void (*rgb32tobgr24)(const uint8_t *src, uint8_t *dst, int src_size);
  33. void (*rgb32tobgr16)(const uint8_t *src, uint8_t *dst, int src_size);
  34. void (*rgb32tobgr15)(const uint8_t *src, uint8_t *dst, int src_size);
  35. void (*rgb24tobgr32)(const uint8_t *src, uint8_t *dst, int src_size);
  36. void (*rgb24tobgr24)(const uint8_t *src, uint8_t *dst, int src_size);
  37. void (*rgb24tobgr16)(const uint8_t *src, uint8_t *dst, int src_size);
  38. void (*rgb24tobgr15)(const uint8_t *src, uint8_t *dst, int src_size);
  39. void (*rgb16tobgr24)(const uint8_t *src, uint8_t *dst, int src_size);
  40. void (*rgb15tobgr24)(const uint8_t *src, uint8_t *dst, int src_size);
  41. void (*rgb32to16)(const uint8_t *src, uint8_t *dst, int src_size);
  42. void (*rgb32to15)(const uint8_t *src, uint8_t *dst, int src_size);
  43. void (*rgb24to16)(const uint8_t *src, uint8_t *dst, int src_size);
  44. void (*rgb24to15)(const uint8_t *src, uint8_t *dst, int src_size);
  45. void (*rgb16to32)(const uint8_t *src, uint8_t *dst, int src_size);
  46. void (*rgb16to15)(const uint8_t *src, uint8_t *dst, int src_size);
  47. void (*rgb15to16)(const uint8_t *src, uint8_t *dst, int src_size);
  48. void (*rgb15to32)(const uint8_t *src, uint8_t *dst, int src_size);
  49. void (*shuffle_bytes_2103)(const uint8_t *src, uint8_t *dst, int src_size);
  50. void (*yv12toyuy2)(const uint8_t *ysrc, const uint8_t *usrc,
  51. const uint8_t *vsrc, uint8_t *dst,
  52. int width, int height,
  53. int lumStride, int chromStride, int dstStride);
  54. void (*yv12touyvy)(const uint8_t *ysrc, const uint8_t *usrc,
  55. const uint8_t *vsrc, uint8_t *dst,
  56. int width, int height,
  57. int lumStride, int chromStride, int dstStride);
  58. void (*yuv422ptoyuy2)(const uint8_t *ysrc, const uint8_t *usrc,
  59. const uint8_t *vsrc, uint8_t *dst,
  60. int width, int height,
  61. int lumStride, int chromStride, int dstStride);
  62. void (*yuv422ptouyvy)(const uint8_t *ysrc, const uint8_t *usrc,
  63. const uint8_t *vsrc, uint8_t *dst,
  64. int width, int height,
  65. int lumStride, int chromStride, int dstStride);
  66. void (*yuy2toyv12)(const uint8_t *src, uint8_t *ydst,
  67. uint8_t *udst, uint8_t *vdst,
  68. int width, int height,
  69. int lumStride, int chromStride, int srcStride);
  70. void (*rgb24toyv12)(const uint8_t *src, uint8_t *ydst,
  71. uint8_t *udst, uint8_t *vdst,
  72. int width, int height,
  73. int lumStride, int chromStride, int srcStride);
  74. void (*planar2x)(const uint8_t *src, uint8_t *dst, int width, int height,
  75. int srcStride, int dstStride);
  76. void (*interleaveBytes)(const uint8_t *src1, const uint8_t *src2, uint8_t *dst,
  77. int width, int height, int src1Stride,
  78. int src2Stride, int dstStride);
  79. void (*vu9_to_vu12)(const uint8_t *src1, const uint8_t *src2,
  80. uint8_t *dst1, uint8_t *dst2,
  81. int width, int height,
  82. int srcStride1, int srcStride2,
  83. int dstStride1, int dstStride2);
  84. void (*yvu9_to_yuy2)(const uint8_t *src1, const uint8_t *src2,
  85. const uint8_t *src3, uint8_t *dst,
  86. int width, int height,
  87. int srcStride1, int srcStride2,
  88. int srcStride3, int dstStride);
  89. void (*uyvytoyuv420)(uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
  90. const uint8_t *src, int width, int height,
  91. int lumStride, int chromStride, int srcStride);
  92. void (*uyvytoyuv422)(uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
  93. const uint8_t *src, int width, int height,
  94. int lumStride, int chromStride, int srcStride);
  95. void (*yuyvtoyuv420)(uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
  96. const uint8_t *src, int width, int height,
  97. int lumStride, int chromStride, int srcStride);
  98. void (*yuyvtoyuv422)(uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
  99. const uint8_t *src, int width, int height,
  100. int lumStride, int chromStride, int srcStride);
  101. #define RGB2YUV_SHIFT 8
  102. #define BY ((int)( 0.098 * (1 << RGB2YUV_SHIFT) + 0.5))
  103. #define BV ((int)(-0.071 * (1 << RGB2YUV_SHIFT) + 0.5))
  104. #define BU ((int)( 0.439 * (1 << RGB2YUV_SHIFT) + 0.5))
  105. #define GY ((int)( 0.504 * (1 << RGB2YUV_SHIFT) + 0.5))
  106. #define GV ((int)(-0.368 * (1 << RGB2YUV_SHIFT) + 0.5))
  107. #define GU ((int)(-0.291 * (1 << RGB2YUV_SHIFT) + 0.5))
  108. #define RY ((int)( 0.257 * (1 << RGB2YUV_SHIFT) + 0.5))
  109. #define RV ((int)( 0.439 * (1 << RGB2YUV_SHIFT) + 0.5))
  110. #define RU ((int)(-0.148 * (1 << RGB2YUV_SHIFT) + 0.5))
  111. //plain C versions
  112. #include "rgb2rgb_template.c"
  113. /*
  114. * RGB15->RGB16 original by Strepto/Astral
  115. * ported to gcc & bugfixed : A'rpi
  116. * MMX2, 3DNOW optimization by Nick Kurshev
  117. * 32-bit C version, and and&add trick by Michael Niedermayer
  118. */
  119. av_cold void sws_rgb2rgb_init(void)
  120. {
  121. rgb2rgb_init_c();
  122. if (HAVE_MMX)
  123. rgb2rgb_init_x86();
  124. }
  125. void rgb32to24(const uint8_t *src, uint8_t *dst, int src_size)
  126. {
  127. int i, num_pixels = src_size >> 2;
  128. for (i = 0; i < num_pixels; i++) {
  129. #if HAVE_BIGENDIAN
  130. /* RGB32 (= A,B,G,R) -> BGR24 (= B,G,R) */
  131. dst[3 * i + 0] = src[4 * i + 1];
  132. dst[3 * i + 1] = src[4 * i + 2];
  133. dst[3 * i + 2] = src[4 * i + 3];
  134. #else
  135. dst[3 * i + 0] = src[4 * i + 2];
  136. dst[3 * i + 1] = src[4 * i + 1];
  137. dst[3 * i + 2] = src[4 * i + 0];
  138. #endif
  139. }
  140. }
  141. void rgb24to32(const uint8_t *src, uint8_t *dst, int src_size)
  142. {
  143. int i;
  144. for (i = 0; 3 * i < src_size; i++) {
  145. #if HAVE_BIGENDIAN
  146. /* RGB24 (= R, G, B) -> BGR32 (= A, R, G, B) */
  147. dst[4 * i + 0] = 255;
  148. dst[4 * i + 1] = src[3 * i + 0];
  149. dst[4 * i + 2] = src[3 * i + 1];
  150. dst[4 * i + 3] = src[3 * i + 2];
  151. #else
  152. dst[4 * i + 0] = src[3 * i + 2];
  153. dst[4 * i + 1] = src[3 * i + 1];
  154. dst[4 * i + 2] = src[3 * i + 0];
  155. dst[4 * i + 3] = 255;
  156. #endif
  157. }
  158. }
  159. void rgb16tobgr32(const uint8_t *src, uint8_t *dst, int src_size)
  160. {
  161. uint8_t *d = dst;
  162. const uint16_t *s = (const uint16_t *)src;
  163. const uint16_t *end = s + src_size / 2;
  164. while (s < end) {
  165. register uint16_t bgr = *s++;
  166. #if HAVE_BIGENDIAN
  167. *d++ = 255;
  168. *d++ = ((bgr&0x001F)<<3) | ((bgr&0x001F)>> 2);
  169. *d++ = ((bgr&0x07E0)>>3) | ((bgr&0x07E0)>> 9);
  170. *d++ = ((bgr&0xF800)>>8) | ((bgr&0xF800)>>13);
  171. #else
  172. *d++ = ((bgr&0xF800)>>8) | ((bgr&0xF800)>>13);
  173. *d++ = ((bgr&0x07E0)>>3) | ((bgr&0x07E0)>> 9);
  174. *d++ = ((bgr&0x001F)<<3) | ((bgr&0x001F)>> 2);
  175. *d++ = 255;
  176. #endif
  177. }
  178. }
  179. void rgb12to15(const uint8_t *src, uint8_t *dst, int src_size)
  180. {
  181. uint16_t rgb, r, g, b;
  182. uint16_t *d = (uint16_t *)dst;
  183. const uint16_t *s = (const uint16_t *)src;
  184. const uint16_t *end = s + src_size / 2;
  185. while (s < end) {
  186. rgb = *s++;
  187. r = rgb & 0xF00;
  188. g = rgb & 0x0F0;
  189. b = rgb & 0x00F;
  190. r = (r << 3) | ((r & 0x800) >> 1);
  191. g = (g << 2) | ((g & 0x080) >> 2);
  192. b = (b << 1) | ( b >> 3);
  193. *d++ = r | g | b;
  194. }
  195. }
  196. void rgb16to24(const uint8_t *src, uint8_t *dst, int src_size)
  197. {
  198. uint8_t *d = dst;
  199. const uint16_t *s = (const uint16_t *)src;
  200. const uint16_t *end = s + src_size / 2;
  201. while (s < end) {
  202. register uint16_t bgr = *s++;
  203. *d++ = ((bgr&0xF800)>>8) | ((bgr&0xF800)>>13);
  204. *d++ = ((bgr&0x07E0)>>3) | ((bgr&0x07E0)>> 9);
  205. *d++ = ((bgr&0x001F)<<3) | ((bgr&0x001F)>> 2);
  206. }
  207. }
  208. void rgb16tobgr16(const uint8_t *src, uint8_t *dst, int src_size)
  209. {
  210. int i, num_pixels = src_size >> 1;
  211. for (i = 0; i < num_pixels; i++) {
  212. unsigned rgb = ((const uint16_t *)src)[i];
  213. ((uint16_t *)dst)[i] = (rgb >> 11) | (rgb & 0x7E0) | (rgb << 11);
  214. }
  215. }
  216. void rgb16tobgr15(const uint8_t *src, uint8_t *dst, int src_size)
  217. {
  218. int i, num_pixels = src_size >> 1;
  219. for (i = 0; i < num_pixels; i++) {
  220. unsigned rgb = ((const uint16_t *)src)[i];
  221. ((uint16_t *)dst)[i] = (rgb >> 11) | ((rgb & 0x7C0) >> 1) | ((rgb & 0x1F) << 10);
  222. }
  223. }
  224. void rgb15tobgr32(const uint8_t *src, uint8_t *dst, int src_size)
  225. {
  226. uint8_t *d = dst;
  227. const uint16_t *s = (const uint16_t *)src;
  228. const uint16_t *end = s + src_size / 2;
  229. while (s < end) {
  230. register uint16_t bgr = *s++;
  231. #if HAVE_BIGENDIAN
  232. *d++ = 255;
  233. *d++ = ((bgr&0x001F)<<3) | ((bgr&0x001F)>> 2);
  234. *d++ = ((bgr&0x03E0)>>2) | ((bgr&0x03E0)>> 7);
  235. *d++ = ((bgr&0x7C00)>>7) | ((bgr&0x7C00)>>12);
  236. #else
  237. *d++ = ((bgr&0x7C00)>>7) | ((bgr&0x7C00)>>12);
  238. *d++ = ((bgr&0x03E0)>>2) | ((bgr&0x03E0)>> 7);
  239. *d++ = ((bgr&0x001F)<<3) | ((bgr&0x001F)>> 2);
  240. *d++ = 255;
  241. #endif
  242. }
  243. }
  244. void rgb15to24(const uint8_t *src, uint8_t *dst, int src_size)
  245. {
  246. uint8_t *d = dst;
  247. const uint16_t *s = (const uint16_t *)src;
  248. const uint16_t *end = s + src_size / 2;
  249. while (s < end) {
  250. register uint16_t bgr = *s++;
  251. *d++ = ((bgr&0x7C00)>>7) | ((bgr&0x7C00)>>12);
  252. *d++ = ((bgr&0x03E0)>>2) | ((bgr&0x03E0)>> 7);
  253. *d++ = ((bgr&0x001F)<<3) | ((bgr&0x001F)>> 2);
  254. }
  255. }
  256. void rgb15tobgr16(const uint8_t *src, uint8_t *dst, int src_size)
  257. {
  258. int i, num_pixels = src_size >> 1;
  259. for (i = 0; i < num_pixels; i++) {
  260. unsigned rgb = ((const uint16_t *)src)[i];
  261. ((uint16_t *)dst)[i] = ((rgb & 0x7C00) >> 10) | ((rgb & 0x3E0) << 1) | (rgb << 11);
  262. }
  263. }
  264. void rgb15tobgr15(const uint8_t *src, uint8_t *dst, int src_size)
  265. {
  266. int i, num_pixels = src_size >> 1;
  267. for (i = 0; i < num_pixels; i++) {
  268. unsigned rgb = ((const uint16_t *)src)[i];
  269. unsigned br = rgb & 0x7C1F;
  270. ((uint16_t *)dst)[i] = (br >> 10) | (rgb & 0x3E0) | (br << 10);
  271. }
  272. }
  273. void rgb12tobgr12(const uint8_t *src, uint8_t *dst, int src_size)
  274. {
  275. uint16_t *d = (uint16_t *)dst;
  276. uint16_t *s = (uint16_t *)src;
  277. int i, num_pixels = src_size >> 1;
  278. for (i = 0; i < num_pixels; i++) {
  279. unsigned rgb = s[i];
  280. d[i] = (rgb << 8 | rgb & 0xF0 | rgb >> 8) & 0xFFF;
  281. }
  282. }
  283. #define DEFINE_SHUFFLE_BYTES(a, b, c, d) \
  284. void shuffle_bytes_ ## a ## b ## c ## d(const uint8_t *src, \
  285. uint8_t *dst, int src_size) \
  286. { \
  287. int i; \
  288. \
  289. for (i = 0; i < src_size; i += 4) { \
  290. dst[i + 0] = src[i + a]; \
  291. dst[i + 1] = src[i + b]; \
  292. dst[i + 2] = src[i + c]; \
  293. dst[i + 3] = src[i + d]; \
  294. } \
  295. }
  296. DEFINE_SHUFFLE_BYTES(0, 3, 2, 1)
  297. DEFINE_SHUFFLE_BYTES(1, 2, 3, 0)
  298. DEFINE_SHUFFLE_BYTES(3, 0, 1, 2)
  299. DEFINE_SHUFFLE_BYTES(3, 2, 1, 0)
  300. #define DEFINE_RGB48TOBGR48(need_bswap, swap) \
  301. void rgb48tobgr48_ ## need_bswap(const uint8_t *src, \
  302. uint8_t *dst, int src_size) \
  303. { \
  304. uint16_t *d = (uint16_t *)dst; \
  305. uint16_t *s = (uint16_t *)src; \
  306. int i, num_pixels = src_size >> 1; \
  307. \
  308. for (i = 0; i < num_pixels; i += 3) { \
  309. d[i ] = swap ? av_bswap16(s[i + 2]) : s[i + 2]; \
  310. d[i + 1] = swap ? av_bswap16(s[i + 1]) : s[i + 1]; \
  311. d[i + 2] = swap ? av_bswap16(s[i ]) : s[i ]; \
  312. } \
  313. }
  314. DEFINE_RGB48TOBGR48(nobswap, 0)
  315. DEFINE_RGB48TOBGR48(bswap, 1)
  316. #define DEFINE_RGB64TOBGR48(need_bswap, swap) \
  317. void rgb64tobgr48_ ## need_bswap(const uint8_t *src, \
  318. uint8_t *dst, int src_size) \
  319. { \
  320. uint16_t *d = (uint16_t *)dst; \
  321. uint16_t *s = (uint16_t *)src; \
  322. int i, num_pixels = src_size >> 3; \
  323. \
  324. for (i = 0; i < num_pixels; i++) { \
  325. d[3 * i ] = swap ? av_bswap16(s[4 * i + 2]) : s[4 * i + 2]; \
  326. d[3 * i + 1] = swap ? av_bswap16(s[4 * i + 1]) : s[4 * i + 1]; \
  327. d[3 * i + 2] = swap ? av_bswap16(s[4 * i ]) : s[4 * i ]; \
  328. } \
  329. }
  330. DEFINE_RGB64TOBGR48(nobswap, 0)
  331. DEFINE_RGB64TOBGR48(bswap, 1)
  332. #define DEFINE_RGB64TO48(need_bswap, swap) \
  333. void rgb64to48_ ## need_bswap(const uint8_t *src, \
  334. uint8_t *dst, int src_size) \
  335. { \
  336. uint16_t *d = (uint16_t *)dst; \
  337. uint16_t *s = (uint16_t *)src; \
  338. int i, num_pixels = src_size >> 3; \
  339. \
  340. for (i = 0; i < num_pixels; i++) { \
  341. d[3 * i ] = swap ? av_bswap16(s[4 * i ]) : s[4 * i ]; \
  342. d[3 * i + 1] = swap ? av_bswap16(s[4 * i + 1]) : s[4 * i + 1]; \
  343. d[3 * i + 2] = swap ? av_bswap16(s[4 * i + 2]) : s[4 * i + 2]; \
  344. } \
  345. }
  346. DEFINE_RGB64TO48(nobswap, 0)
  347. DEFINE_RGB64TO48(bswap, 1)