rgb2rgb.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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_0321)(const uint8_t *src, uint8_t *dst, int src_size);
  50. void (*shuffle_bytes_2103)(const uint8_t *src, uint8_t *dst, int src_size);
  51. void (*shuffle_bytes_1230)(const uint8_t *src, uint8_t *dst, int src_size);
  52. void (*shuffle_bytes_3012)(const uint8_t *src, uint8_t *dst, int src_size);
  53. void (*shuffle_bytes_3210)(const uint8_t *src, uint8_t *dst, int src_size);
  54. void (*yv12toyuy2)(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 (*yv12touyvy)(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 (*yuv422ptoyuy2)(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 (*yuv422ptouyvy)(const uint8_t *ysrc, const uint8_t *usrc,
  67. const uint8_t *vsrc, uint8_t *dst,
  68. int width, int height,
  69. int lumStride, int chromStride, int dstStride);
  70. void (*yuy2toyv12)(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 (*ff_rgb24toyv12)(const uint8_t *src, uint8_t *ydst,
  75. uint8_t *udst, uint8_t *vdst,
  76. int width, int height,
  77. int lumStride, int chromStride, int srcStride,
  78. int32_t *rgb2yuv);
  79. void (*planar2x)(const uint8_t *src, uint8_t *dst, int width, int height,
  80. int srcStride, int dstStride);
  81. void (*interleaveBytes)(const uint8_t *src1, const uint8_t *src2, uint8_t *dst,
  82. int width, int height, int src1Stride,
  83. int src2Stride, int dstStride);
  84. void (*deinterleaveBytes)(const uint8_t *src, uint8_t *dst1, uint8_t *dst2,
  85. int width, int height, int srcStride,
  86. int dst1Stride, int dst2Stride);
  87. void (*vu9_to_vu12)(const uint8_t *src1, const uint8_t *src2,
  88. uint8_t *dst1, uint8_t *dst2,
  89. int width, int height,
  90. int srcStride1, int srcStride2,
  91. int dstStride1, int dstStride2);
  92. void (*yvu9_to_yuy2)(const uint8_t *src1, const uint8_t *src2,
  93. const uint8_t *src3, uint8_t *dst,
  94. int width, int height,
  95. int srcStride1, int srcStride2,
  96. int srcStride3, int dstStride);
  97. void (*uyvytoyuv420)(uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
  98. const uint8_t *src, int width, int height,
  99. int lumStride, int chromStride, int srcStride);
  100. void (*uyvytoyuv422)(uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
  101. const uint8_t *src, int width, int height,
  102. int lumStride, int chromStride, int srcStride);
  103. void (*yuyvtoyuv420)(uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
  104. const uint8_t *src, int width, int height,
  105. int lumStride, int chromStride, int srcStride);
  106. void (*yuyvtoyuv422)(uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
  107. const uint8_t *src, int width, int height,
  108. int lumStride, int chromStride, int srcStride);
  109. #define BY ((int)( 0.098 * (1 << RGB2YUV_SHIFT) + 0.5))
  110. #define BV ((int)(-0.071 * (1 << RGB2YUV_SHIFT) + 0.5))
  111. #define BU ((int)( 0.439 * (1 << RGB2YUV_SHIFT) + 0.5))
  112. #define GY ((int)( 0.504 * (1 << RGB2YUV_SHIFT) + 0.5))
  113. #define GV ((int)(-0.368 * (1 << RGB2YUV_SHIFT) + 0.5))
  114. #define GU ((int)(-0.291 * (1 << RGB2YUV_SHIFT) + 0.5))
  115. #define RY ((int)( 0.257 * (1 << RGB2YUV_SHIFT) + 0.5))
  116. #define RV ((int)( 0.439 * (1 << RGB2YUV_SHIFT) + 0.5))
  117. #define RU ((int)(-0.148 * (1 << RGB2YUV_SHIFT) + 0.5))
  118. //plain C versions
  119. #include "rgb2rgb_template.c"
  120. /*
  121. * RGB15->RGB16 original by Strepto/Astral
  122. * ported to gcc & bugfixed : A'rpi
  123. * MMXEXT, 3DNOW optimization by Nick Kurshev
  124. * 32-bit C version, and and&add trick by Michael Niedermayer
  125. */
  126. av_cold void ff_sws_rgb2rgb_init(void)
  127. {
  128. rgb2rgb_init_c();
  129. #if ARCH_AARCH64
  130. rgb2rgb_init_aarch64();
  131. #elif ARCH_RISCV
  132. rgb2rgb_init_riscv();
  133. #elif ARCH_X86
  134. rgb2rgb_init_x86();
  135. #elif ARCH_LOONGARCH64
  136. rgb2rgb_init_loongarch();
  137. #endif
  138. }
  139. void rgb32to24(const uint8_t *src, uint8_t *dst, int src_size)
  140. {
  141. int i, num_pixels = src_size >> 2;
  142. for (i = 0; i < num_pixels; i++) {
  143. #if HAVE_BIGENDIAN
  144. /* RGB32 (= A,B,G,R) -> BGR24 (= B,G,R) */
  145. dst[3 * i + 0] = src[4 * i + 1];
  146. dst[3 * i + 1] = src[4 * i + 2];
  147. dst[3 * i + 2] = src[4 * i + 3];
  148. #else
  149. dst[3 * i + 0] = src[4 * i + 2];
  150. dst[3 * i + 1] = src[4 * i + 1];
  151. dst[3 * i + 2] = src[4 * i + 0];
  152. #endif
  153. }
  154. }
  155. void rgb24to32(const uint8_t *src, uint8_t *dst, int src_size)
  156. {
  157. int i;
  158. for (i = 0; 3 * i < src_size; i++) {
  159. #if HAVE_BIGENDIAN
  160. /* RGB24 (= R, G, B) -> BGR32 (= A, R, G, B) */
  161. dst[4 * i + 0] = 255;
  162. dst[4 * i + 1] = src[3 * i + 0];
  163. dst[4 * i + 2] = src[3 * i + 1];
  164. dst[4 * i + 3] = src[3 * i + 2];
  165. #else
  166. dst[4 * i + 0] = src[3 * i + 2];
  167. dst[4 * i + 1] = src[3 * i + 1];
  168. dst[4 * i + 2] = src[3 * i + 0];
  169. dst[4 * i + 3] = 255;
  170. #endif
  171. }
  172. }
  173. void rgb16tobgr32(const uint8_t *src, uint8_t *dst, int src_size)
  174. {
  175. uint8_t *d = dst;
  176. const uint16_t *s = (const uint16_t *)src;
  177. const uint16_t *end = s + src_size / 2;
  178. while (s < end) {
  179. register uint16_t bgr = *s++;
  180. #if HAVE_BIGENDIAN
  181. *d++ = 255;
  182. *d++ = ((bgr&0x001F)<<3) | ((bgr&0x001F)>> 2);
  183. *d++ = ((bgr&0x07E0)>>3) | ((bgr&0x07E0)>> 9);
  184. *d++ = ((bgr&0xF800)>>8) | ((bgr&0xF800)>>13);
  185. #else
  186. *d++ = ((bgr&0xF800)>>8) | ((bgr&0xF800)>>13);
  187. *d++ = ((bgr&0x07E0)>>3) | ((bgr&0x07E0)>> 9);
  188. *d++ = ((bgr&0x001F)<<3) | ((bgr&0x001F)>> 2);
  189. *d++ = 255;
  190. #endif
  191. }
  192. }
  193. void rgb12to15(const uint8_t *src, uint8_t *dst, int src_size)
  194. {
  195. uint16_t rgb, r, g, b;
  196. uint16_t *d = (uint16_t *)dst;
  197. const uint16_t *s = (const uint16_t *)src;
  198. const uint16_t *end = s + src_size / 2;
  199. while (s < end) {
  200. rgb = *s++;
  201. r = rgb & 0xF00;
  202. g = rgb & 0x0F0;
  203. b = rgb & 0x00F;
  204. r = (r << 3) | ((r & 0x800) >> 1);
  205. g = (g << 2) | ((g & 0x080) >> 2);
  206. b = (b << 1) | ( b >> 3);
  207. *d++ = r | g | b;
  208. }
  209. }
  210. void rgb16to24(const uint8_t *src, uint8_t *dst, int src_size)
  211. {
  212. uint8_t *d = dst;
  213. const uint16_t *s = (const uint16_t *)src;
  214. const uint16_t *end = s + src_size / 2;
  215. while (s < end) {
  216. register uint16_t bgr = *s++;
  217. *d++ = ((bgr&0xF800)>>8) | ((bgr&0xF800)>>13);
  218. *d++ = ((bgr&0x07E0)>>3) | ((bgr&0x07E0)>> 9);
  219. *d++ = ((bgr&0x001F)<<3) | ((bgr&0x001F)>> 2);
  220. }
  221. }
  222. void rgb16tobgr16(const uint8_t *src, uint8_t *dst, int src_size)
  223. {
  224. int i, num_pixels = src_size >> 1;
  225. for (i = 0; i < num_pixels; i++) {
  226. unsigned rgb = ((const uint16_t *)src)[i];
  227. ((uint16_t *)dst)[i] = (rgb >> 11) | (rgb & 0x7E0) | (rgb << 11);
  228. }
  229. }
  230. void rgb16tobgr15(const uint8_t *src, uint8_t *dst, int src_size)
  231. {
  232. int i, num_pixels = src_size >> 1;
  233. for (i = 0; i < num_pixels; i++) {
  234. unsigned rgb = ((const uint16_t *)src)[i];
  235. ((uint16_t *)dst)[i] = (rgb >> 11) | ((rgb & 0x7C0) >> 1) | ((rgb & 0x1F) << 10);
  236. }
  237. }
  238. void rgb15tobgr32(const uint8_t *src, uint8_t *dst, int src_size)
  239. {
  240. uint8_t *d = dst;
  241. const uint16_t *s = (const uint16_t *)src;
  242. const uint16_t *end = s + src_size / 2;
  243. while (s < end) {
  244. register uint16_t bgr = *s++;
  245. #if HAVE_BIGENDIAN
  246. *d++ = 255;
  247. *d++ = ((bgr&0x001F)<<3) | ((bgr&0x001F)>> 2);
  248. *d++ = ((bgr&0x03E0)>>2) | ((bgr&0x03E0)>> 7);
  249. *d++ = ((bgr&0x7C00)>>7) | ((bgr&0x7C00)>>12);
  250. #else
  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. *d++ = 255;
  255. #endif
  256. }
  257. }
  258. void rgb15to24(const uint8_t *src, uint8_t *dst, int src_size)
  259. {
  260. uint8_t *d = dst;
  261. const uint16_t *s = (const uint16_t *)src;
  262. const uint16_t *end = s + src_size / 2;
  263. while (s < end) {
  264. register uint16_t bgr = *s++;
  265. *d++ = ((bgr&0x7C00)>>7) | ((bgr&0x7C00)>>12);
  266. *d++ = ((bgr&0x03E0)>>2) | ((bgr&0x03E0)>> 7);
  267. *d++ = ((bgr&0x001F)<<3) | ((bgr&0x001F)>> 2);
  268. }
  269. }
  270. void rgb15tobgr16(const uint8_t *src, uint8_t *dst, int src_size)
  271. {
  272. int i, num_pixels = src_size >> 1;
  273. for (i = 0; i < num_pixels; i++) {
  274. unsigned rgb = ((const uint16_t *)src)[i];
  275. ((uint16_t *)dst)[i] = ((rgb & 0x7C00) >> 10) | ((rgb & 0x3E0) << 1) | (rgb << 11);
  276. }
  277. }
  278. void rgb15tobgr15(const uint8_t *src, uint8_t *dst, int src_size)
  279. {
  280. int i, num_pixels = src_size >> 1;
  281. for (i = 0; i < num_pixels; i++) {
  282. unsigned rgb = ((const uint16_t *)src)[i];
  283. unsigned br = rgb & 0x7C1F;
  284. ((uint16_t *)dst)[i] = (br >> 10) | (rgb & 0x3E0) | (br << 10);
  285. }
  286. }
  287. void rgb12tobgr12(const uint8_t *src, uint8_t *dst, int src_size)
  288. {
  289. uint16_t *d = (uint16_t *)dst;
  290. const uint16_t *s = (const uint16_t *)src;
  291. int i, num_pixels = src_size >> 1;
  292. for (i = 0; i < num_pixels; i++) {
  293. unsigned rgb = s[i];
  294. d[i] = (rgb << 8 | rgb & 0xF0 | rgb >> 8) & 0xFFF;
  295. }
  296. }
  297. #define DEFINE_RGB48TOBGR48(need_bswap, swap) \
  298. void rgb48tobgr48_ ## need_bswap(const uint8_t *src, \
  299. uint8_t *dst, int src_size) \
  300. { \
  301. uint16_t *d = (uint16_t *)dst; \
  302. const uint16_t *s = (const uint16_t *)src; \
  303. int i, num_pixels = src_size >> 1; \
  304. \
  305. for (i = 0; i < num_pixels; i += 3) { \
  306. d[i ] = swap ? av_bswap16(s[i + 2]) : s[i + 2]; \
  307. d[i + 1] = swap ? av_bswap16(s[i + 1]) : s[i + 1]; \
  308. d[i + 2] = swap ? av_bswap16(s[i ]) : s[i ]; \
  309. } \
  310. }
  311. DEFINE_RGB48TOBGR48(nobswap, 0)
  312. DEFINE_RGB48TOBGR48(bswap, 1)
  313. #define DEFINE_RGB64TOBGR48(need_bswap, swap) \
  314. void rgb64tobgr48_ ## need_bswap(const uint8_t *src, \
  315. uint8_t *dst, int src_size) \
  316. { \
  317. uint16_t *d = (uint16_t *)dst; \
  318. const uint16_t *s = (const uint16_t *)src; \
  319. int i, num_pixels = src_size >> 3; \
  320. \
  321. for (i = 0; i < num_pixels; i++) { \
  322. d[3 * i ] = swap ? av_bswap16(s[4 * i + 2]) : s[4 * i + 2]; \
  323. d[3 * i + 1] = swap ? av_bswap16(s[4 * i + 1]) : s[4 * i + 1]; \
  324. d[3 * i + 2] = swap ? av_bswap16(s[4 * i ]) : s[4 * i ]; \
  325. } \
  326. }
  327. DEFINE_RGB64TOBGR48(nobswap, 0)
  328. DEFINE_RGB64TOBGR48(bswap, 1)
  329. #define DEFINE_RGB64TO48(need_bswap, swap) \
  330. void rgb64to48_ ## need_bswap(const uint8_t *src, \
  331. uint8_t *dst, int src_size) \
  332. { \
  333. uint16_t *d = (uint16_t *)dst; \
  334. const uint16_t *s = (const uint16_t *)src; \
  335. int i, num_pixels = src_size >> 3; \
  336. \
  337. for (i = 0; i < num_pixels; i++) { \
  338. d[3 * i ] = swap ? av_bswap16(s[4 * i ]) : s[4 * i ]; \
  339. d[3 * i + 1] = swap ? av_bswap16(s[4 * i + 1]) : s[4 * i + 1]; \
  340. d[3 * i + 2] = swap ? av_bswap16(s[4 * i + 2]) : s[4 * i + 2]; \
  341. } \
  342. }
  343. DEFINE_RGB64TO48(nobswap, 0)
  344. DEFINE_RGB64TO48(bswap, 1)
  345. #define DEFINE_RGB48TOBGR64(need_bswap, swap) \
  346. void rgb48tobgr64_ ## need_bswap(const uint8_t *src, \
  347. uint8_t *dst, int src_size) \
  348. { \
  349. uint16_t *d = (uint16_t *)dst; \
  350. const uint16_t *s = (const uint16_t *)src; \
  351. int i, num_pixels = src_size / 6; \
  352. \
  353. for (i = 0; i < num_pixels; i++) { \
  354. d[4 * i ] = swap ? av_bswap16(s[3 * i + 2]) : s[3 * i + 2]; \
  355. d[4 * i + 1] = swap ? av_bswap16(s[3 * i + 1]) : s[3 * i + 1]; \
  356. d[4 * i + 2] = swap ? av_bswap16(s[3 * i ]) : s[3 * i ]; \
  357. d[4 * i + 3] = 0xFFFF; \
  358. } \
  359. }
  360. DEFINE_RGB48TOBGR64(nobswap, 0)
  361. DEFINE_RGB48TOBGR64(bswap, 1)
  362. #define DEFINE_RGB48TO64(need_bswap, swap) \
  363. void rgb48to64_ ## need_bswap(const uint8_t *src, \
  364. uint8_t *dst, int src_size) \
  365. { \
  366. uint16_t *d = (uint16_t *)dst; \
  367. const uint16_t *s = (const uint16_t *)src; \
  368. int i, num_pixels = src_size / 6; \
  369. \
  370. for (i = 0; i < num_pixels; i++) { \
  371. d[4 * i ] = swap ? av_bswap16(s[3 * i ]) : s[3 * i ]; \
  372. d[4 * i + 1] = swap ? av_bswap16(s[3 * i + 1]) : s[3 * i + 1]; \
  373. d[4 * i + 2] = swap ? av_bswap16(s[3 * i + 2]) : s[3 * i + 2]; \
  374. d[4 * i + 3] = 0xFFFF; \
  375. } \
  376. }
  377. DEFINE_RGB48TO64(nobswap, 0)
  378. DEFINE_RGB48TO64(bswap, 1)