You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

436 lines
16KB

  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 "config.h"
  27. #include "libavutil/x86_cpu.h"
  28. #include "libavutil/bswap.h"
  29. #include "rgb2rgb.h"
  30. #include "swscale.h"
  31. #include "swscale_internal.h"
  32. void (*rgb24tobgr32)(const uint8_t *src, uint8_t *dst, long src_size);
  33. void (*rgb24tobgr16)(const uint8_t *src, uint8_t *dst, long src_size);
  34. void (*rgb24tobgr15)(const uint8_t *src, uint8_t *dst, long src_size);
  35. void (*rgb32tobgr24)(const uint8_t *src, uint8_t *dst, long src_size);
  36. void (*rgb32to16)(const uint8_t *src, uint8_t *dst, long src_size);
  37. void (*rgb32to15)(const uint8_t *src, uint8_t *dst, long src_size);
  38. void (*rgb15to16)(const uint8_t *src, uint8_t *dst, long src_size);
  39. void (*rgb15tobgr24)(const uint8_t *src, uint8_t *dst, long src_size);
  40. void (*rgb15to32)(const uint8_t *src, uint8_t *dst, long src_size);
  41. void (*rgb16to15)(const uint8_t *src, uint8_t *dst, long src_size);
  42. void (*rgb16tobgr24)(const uint8_t *src, uint8_t *dst, long src_size);
  43. void (*rgb16to32)(const uint8_t *src, uint8_t *dst, long src_size);
  44. void (*rgb24tobgr24)(const uint8_t *src, uint8_t *dst, long src_size);
  45. void (*rgb24to16)(const uint8_t *src, uint8_t *dst, long src_size);
  46. void (*rgb24to15)(const uint8_t *src, uint8_t *dst, long src_size);
  47. void (*shuffle_bytes_2103)(const uint8_t *src, uint8_t *dst, long src_size);
  48. void (*rgb32tobgr16)(const uint8_t *src, uint8_t *dst, long src_size);
  49. void (*rgb32tobgr15)(const uint8_t *src, uint8_t *dst, long src_size);
  50. void (*yv12toyuy2)(const uint8_t *ysrc, const uint8_t *usrc, const uint8_t *vsrc, uint8_t *dst,
  51. long width, long height,
  52. long lumStride, long chromStride, long dstStride);
  53. void (*yv12touyvy)(const uint8_t *ysrc, const uint8_t *usrc, const uint8_t *vsrc, uint8_t *dst,
  54. long width, long height,
  55. long lumStride, long chromStride, long dstStride);
  56. void (*yuv422ptoyuy2)(const uint8_t *ysrc, const uint8_t *usrc, const uint8_t *vsrc, uint8_t *dst,
  57. long width, long height,
  58. long lumStride, long chromStride, long dstStride);
  59. void (*yuv422ptouyvy)(const uint8_t *ysrc, const uint8_t *usrc, const uint8_t *vsrc, uint8_t *dst,
  60. long width, long height,
  61. long lumStride, long chromStride, long dstStride);
  62. void (*yuy2toyv12)(const uint8_t *src, uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
  63. long width, long height,
  64. long lumStride, long chromStride, long srcStride);
  65. void (*rgb24toyv12)(const uint8_t *src, uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
  66. long width, long height,
  67. long lumStride, long chromStride, long srcStride);
  68. void (*planar2x)(const uint8_t *src, uint8_t *dst, long width, long height,
  69. long srcStride, long dstStride);
  70. void (*interleaveBytes)(const uint8_t *src1, const uint8_t *src2, uint8_t *dst,
  71. long width, long height, long src1Stride,
  72. long src2Stride, long dstStride);
  73. void (*vu9_to_vu12)(const uint8_t *src1, const uint8_t *src2,
  74. uint8_t *dst1, uint8_t *dst2,
  75. long width, long height,
  76. long srcStride1, long srcStride2,
  77. long dstStride1, long dstStride2);
  78. void (*yvu9_to_yuy2)(const uint8_t *src1, const uint8_t *src2, const uint8_t *src3,
  79. uint8_t *dst,
  80. long width, long height,
  81. long srcStride1, long srcStride2,
  82. long srcStride3, long dstStride);
  83. void (*uyvytoyuv420)(uint8_t *ydst, uint8_t *udst, uint8_t *vdst, const uint8_t *src,
  84. long width, long height,
  85. long lumStride, long chromStride, long srcStride);
  86. void (*uyvytoyuv422)(uint8_t *ydst, uint8_t *udst, uint8_t *vdst, const uint8_t *src,
  87. long width, long height,
  88. long lumStride, long chromStride, long srcStride);
  89. void (*yuyvtoyuv420)(uint8_t *ydst, uint8_t *udst, uint8_t *vdst, const uint8_t *src,
  90. long width, long height,
  91. long lumStride, long chromStride, long srcStride);
  92. void (*yuyvtoyuv422)(uint8_t *ydst, uint8_t *udst, uint8_t *vdst, const uint8_t *src,
  93. long width, long height,
  94. long lumStride, long chromStride, long srcStride);
  95. #if ARCH_X86
  96. DECLARE_ASM_CONST(8, uint64_t, mmx_ff) = 0x00000000000000FFULL;
  97. DECLARE_ASM_CONST(8, uint64_t, mmx_null) = 0x0000000000000000ULL;
  98. DECLARE_ASM_CONST(8, uint64_t, mmx_one) = 0xFFFFFFFFFFFFFFFFULL;
  99. DECLARE_ASM_CONST(8, uint64_t, mask32b) = 0x000000FF000000FFULL;
  100. DECLARE_ASM_CONST(8, uint64_t, mask32g) = 0x0000FF000000FF00ULL;
  101. DECLARE_ASM_CONST(8, uint64_t, mask32r) = 0x00FF000000FF0000ULL;
  102. DECLARE_ASM_CONST(8, uint64_t, mask32a) = 0xFF000000FF000000ULL;
  103. DECLARE_ASM_CONST(8, uint64_t, mask32) = 0x00FFFFFF00FFFFFFULL;
  104. DECLARE_ASM_CONST(8, uint64_t, mask3216br) = 0x00F800F800F800F8ULL;
  105. DECLARE_ASM_CONST(8, uint64_t, mask3216g) = 0x0000FC000000FC00ULL;
  106. DECLARE_ASM_CONST(8, uint64_t, mask3215g) = 0x0000F8000000F800ULL;
  107. DECLARE_ASM_CONST(8, uint64_t, mul3216) = 0x2000000420000004ULL;
  108. DECLARE_ASM_CONST(8, uint64_t, mul3215) = 0x2000000820000008ULL;
  109. DECLARE_ASM_CONST(8, uint64_t, mask24b) = 0x00FF0000FF0000FFULL;
  110. DECLARE_ASM_CONST(8, uint64_t, mask24g) = 0xFF0000FF0000FF00ULL;
  111. DECLARE_ASM_CONST(8, uint64_t, mask24r) = 0x0000FF0000FF0000ULL;
  112. DECLARE_ASM_CONST(8, uint64_t, mask24l) = 0x0000000000FFFFFFULL;
  113. DECLARE_ASM_CONST(8, uint64_t, mask24h) = 0x0000FFFFFF000000ULL;
  114. DECLARE_ASM_CONST(8, uint64_t, mask24hh) = 0xffff000000000000ULL;
  115. DECLARE_ASM_CONST(8, uint64_t, mask24hhh) = 0xffffffff00000000ULL;
  116. DECLARE_ASM_CONST(8, uint64_t, mask24hhhh) = 0xffffffffffff0000ULL;
  117. DECLARE_ASM_CONST(8, uint64_t, mask15b) = 0x001F001F001F001FULL; /* 00000000 00011111 xxB */
  118. DECLARE_ASM_CONST(8, uint64_t, mask15rg) = 0x7FE07FE07FE07FE0ULL; /* 01111111 11100000 RGx */
  119. DECLARE_ASM_CONST(8, uint64_t, mask15s) = 0xFFE0FFE0FFE0FFE0ULL;
  120. DECLARE_ASM_CONST(8, uint64_t, mask15g) = 0x03E003E003E003E0ULL;
  121. DECLARE_ASM_CONST(8, uint64_t, mask15r) = 0x7C007C007C007C00ULL;
  122. #define mask16b mask15b
  123. DECLARE_ASM_CONST(8, uint64_t, mask16g) = 0x07E007E007E007E0ULL;
  124. DECLARE_ASM_CONST(8, uint64_t, mask16r) = 0xF800F800F800F800ULL;
  125. DECLARE_ASM_CONST(8, uint64_t, red_16mask) = 0x0000f8000000f800ULL;
  126. DECLARE_ASM_CONST(8, uint64_t, green_16mask) = 0x000007e0000007e0ULL;
  127. DECLARE_ASM_CONST(8, uint64_t, blue_16mask) = 0x0000001f0000001fULL;
  128. DECLARE_ASM_CONST(8, uint64_t, red_15mask) = 0x00007c0000007c00ULL;
  129. DECLARE_ASM_CONST(8, uint64_t, green_15mask) = 0x000003e0000003e0ULL;
  130. DECLARE_ASM_CONST(8, uint64_t, blue_15mask) = 0x0000001f0000001fULL;
  131. #endif /* ARCH_X86 */
  132. #define RGB2YUV_SHIFT 8
  133. #define BY ((int)( 0.098*(1<<RGB2YUV_SHIFT)+0.5))
  134. #define BV ((int)(-0.071*(1<<RGB2YUV_SHIFT)+0.5))
  135. #define BU ((int)( 0.439*(1<<RGB2YUV_SHIFT)+0.5))
  136. #define GY ((int)( 0.504*(1<<RGB2YUV_SHIFT)+0.5))
  137. #define GV ((int)(-0.368*(1<<RGB2YUV_SHIFT)+0.5))
  138. #define GU ((int)(-0.291*(1<<RGB2YUV_SHIFT)+0.5))
  139. #define RY ((int)( 0.257*(1<<RGB2YUV_SHIFT)+0.5))
  140. #define RV ((int)( 0.439*(1<<RGB2YUV_SHIFT)+0.5))
  141. #define RU ((int)(-0.148*(1<<RGB2YUV_SHIFT)+0.5))
  142. //Note: We have C, MMX, MMX2, 3DNOW versions, there is no 3DNOW + MMX2 one.
  143. //plain C versions
  144. #define COMPILE_TEMPLATE_MMX 0
  145. #define COMPILE_TEMPLATE_MMX2 0
  146. #define COMPILE_TEMPLATE_AMD3DNOW 0
  147. #define COMPILE_TEMPLATE_SSE2 0
  148. #define RENAME(a) a ## _C
  149. #include "rgb2rgb_template.c"
  150. #if ARCH_X86
  151. //MMX versions
  152. #undef RENAME
  153. #undef COMPILE_TEMPLATE_MMX
  154. #define COMPILE_TEMPLATE_MMX 1
  155. #define RENAME(a) a ## _MMX
  156. #include "rgb2rgb_template.c"
  157. //MMX2 versions
  158. #undef RENAME
  159. #undef COMPILE_TEMPLATE_MMX2
  160. #define COMPILE_TEMPLATE_MMX2 1
  161. #define RENAME(a) a ## _MMX2
  162. #include "rgb2rgb_template.c"
  163. //SSE2 versions
  164. #undef RENAME
  165. #undef COMPILE_TEMPLATE_SSE2
  166. #define COMPILE_TEMPLATE_SSE2 1
  167. #define RENAME(a) a ## _SSE2
  168. #include "rgb2rgb_template.c"
  169. //3DNOW versions
  170. #undef RENAME
  171. #undef COMPILE_TEMPLATE_MMX2
  172. #undef COMPILE_TEMPLATE_SSE2
  173. #undef COMPILE_TEMPLATE_AMD3DNOW
  174. #define COMPILE_TEMPLATE_MMX2 0
  175. #define COMPILE_TEMPLATE_SSE2 1
  176. #define COMPILE_TEMPLATE_AMD3DNOW 1
  177. #define RENAME(a) a ## _3DNOW
  178. #include "rgb2rgb_template.c"
  179. #endif //ARCH_X86 || ARCH_X86_64
  180. /*
  181. RGB15->RGB16 original by Strepto/Astral
  182. ported to gcc & bugfixed : A'rpi
  183. MMX2, 3DNOW optimization by Nick Kurshev
  184. 32-bit C version, and and&add trick by Michael Niedermayer
  185. */
  186. void sws_rgb2rgb_init(int flags)
  187. {
  188. #if HAVE_MMX2 || HAVE_AMD3DNOW || HAVE_MMX
  189. if (flags & SWS_CPU_CAPS_SSE2)
  190. rgb2rgb_init_SSE2();
  191. else if (flags & SWS_CPU_CAPS_MMX2)
  192. rgb2rgb_init_MMX2();
  193. else if (flags & SWS_CPU_CAPS_3DNOW)
  194. rgb2rgb_init_3DNOW();
  195. else if (flags & SWS_CPU_CAPS_MMX)
  196. rgb2rgb_init_MMX();
  197. else
  198. #endif /* HAVE_MMX2 || HAVE_AMD3DNOW || HAVE_MMX */
  199. rgb2rgb_init_C();
  200. }
  201. #if LIBSWSCALE_VERSION_MAJOR < 1
  202. void palette8topacked32(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
  203. {
  204. sws_convertPalette8ToPacked32(src, dst, num_pixels, palette);
  205. }
  206. void palette8topacked24(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
  207. {
  208. sws_convertPalette8ToPacked24(src, dst, num_pixels, palette);
  209. }
  210. /**
  211. * Palette is assumed to contain BGR16, see rgb32to16 to convert the palette.
  212. */
  213. void palette8torgb16(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
  214. {
  215. long i;
  216. for (i=0; i<num_pixels; i++)
  217. ((uint16_t *)dst)[i] = ((const uint16_t *)palette)[src[i]];
  218. }
  219. void palette8tobgr16(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
  220. {
  221. long i;
  222. for (i=0; i<num_pixels; i++)
  223. ((uint16_t *)dst)[i] = av_bswap16(((const uint16_t *)palette)[src[i]]);
  224. }
  225. #endif
  226. void rgb32to24(const uint8_t *src, uint8_t *dst, long src_size)
  227. {
  228. long i;
  229. long num_pixels = src_size >> 2;
  230. for (i=0; i<num_pixels; i++) {
  231. #if HAVE_BIGENDIAN
  232. /* RGB32 (= A,B,G,R) -> BGR24 (= B,G,R) */
  233. dst[3*i + 0] = src[4*i + 1];
  234. dst[3*i + 1] = src[4*i + 2];
  235. dst[3*i + 2] = src[4*i + 3];
  236. #else
  237. dst[3*i + 0] = src[4*i + 2];
  238. dst[3*i + 1] = src[4*i + 1];
  239. dst[3*i + 2] = src[4*i + 0];
  240. #endif
  241. }
  242. }
  243. void rgb24to32(const uint8_t *src, uint8_t *dst, long src_size)
  244. {
  245. long i;
  246. for (i=0; 3*i<src_size; i++) {
  247. #if HAVE_BIGENDIAN
  248. /* RGB24 (= R,G,B) -> BGR32 (= A,R,G,B) */
  249. dst[4*i + 0] = 255;
  250. dst[4*i + 1] = src[3*i + 0];
  251. dst[4*i + 2] = src[3*i + 1];
  252. dst[4*i + 3] = src[3*i + 2];
  253. #else
  254. dst[4*i + 0] = src[3*i + 2];
  255. dst[4*i + 1] = src[3*i + 1];
  256. dst[4*i + 2] = src[3*i + 0];
  257. dst[4*i + 3] = 255;
  258. #endif
  259. }
  260. }
  261. void rgb16tobgr32(const uint8_t *src, uint8_t *dst, long src_size)
  262. {
  263. const uint16_t *end;
  264. uint8_t *d = dst;
  265. const uint16_t *s = (const uint16_t *)src;
  266. end = s + src_size/2;
  267. while (s < end) {
  268. register uint16_t bgr;
  269. bgr = *s++;
  270. #if HAVE_BIGENDIAN
  271. *d++ = 255;
  272. *d++ = (bgr&0x1F)<<3;
  273. *d++ = (bgr&0x7E0)>>3;
  274. *d++ = (bgr&0xF800)>>8;
  275. #else
  276. *d++ = (bgr&0xF800)>>8;
  277. *d++ = (bgr&0x7E0)>>3;
  278. *d++ = (bgr&0x1F)<<3;
  279. *d++ = 255;
  280. #endif
  281. }
  282. }
  283. void rgb16to24(const uint8_t *src, uint8_t *dst, long src_size)
  284. {
  285. const uint16_t *end;
  286. uint8_t *d = dst;
  287. const uint16_t *s = (const uint16_t *)src;
  288. end = s + src_size/2;
  289. while (s < end) {
  290. register uint16_t bgr;
  291. bgr = *s++;
  292. *d++ = (bgr&0xF800)>>8;
  293. *d++ = (bgr&0x7E0)>>3;
  294. *d++ = (bgr&0x1F)<<3;
  295. }
  296. }
  297. void rgb16tobgr16(const uint8_t *src, uint8_t *dst, long src_size)
  298. {
  299. long i;
  300. long num_pixels = src_size >> 1;
  301. for (i=0; i<num_pixels; i++) {
  302. unsigned rgb = ((const uint16_t*)src)[i];
  303. ((uint16_t*)dst)[i] = (rgb>>11) | (rgb&0x7E0) | (rgb<<11);
  304. }
  305. }
  306. void rgb16tobgr15(const uint8_t *src, uint8_t *dst, long src_size)
  307. {
  308. long i;
  309. long num_pixels = src_size >> 1;
  310. for (i=0; i<num_pixels; i++) {
  311. unsigned rgb = ((const uint16_t*)src)[i];
  312. ((uint16_t*)dst)[i] = (rgb>>11) | ((rgb&0x7C0)>>1) | ((rgb&0x1F)<<10);
  313. }
  314. }
  315. void rgb15tobgr32(const uint8_t *src, uint8_t *dst, long src_size)
  316. {
  317. const uint16_t *end;
  318. uint8_t *d = dst;
  319. const uint16_t *s = (const uint16_t *)src;
  320. end = s + src_size/2;
  321. while (s < end) {
  322. register uint16_t bgr;
  323. bgr = *s++;
  324. #if HAVE_BIGENDIAN
  325. *d++ = 255;
  326. *d++ = (bgr&0x1F)<<3;
  327. *d++ = (bgr&0x3E0)>>2;
  328. *d++ = (bgr&0x7C00)>>7;
  329. #else
  330. *d++ = (bgr&0x7C00)>>7;
  331. *d++ = (bgr&0x3E0)>>2;
  332. *d++ = (bgr&0x1F)<<3;
  333. *d++ = 255;
  334. #endif
  335. }
  336. }
  337. void rgb15to24(const uint8_t *src, uint8_t *dst, long src_size)
  338. {
  339. const uint16_t *end;
  340. uint8_t *d = dst;
  341. const uint16_t *s = (const uint16_t *)src;
  342. end = s + src_size/2;
  343. while (s < end) {
  344. register uint16_t bgr;
  345. bgr = *s++;
  346. *d++ = (bgr&0x7C00)>>7;
  347. *d++ = (bgr&0x3E0)>>2;
  348. *d++ = (bgr&0x1F)<<3;
  349. }
  350. }
  351. void rgb15tobgr16(const uint8_t *src, uint8_t *dst, long src_size)
  352. {
  353. long i;
  354. long num_pixels = src_size >> 1;
  355. for (i=0; i<num_pixels; i++) {
  356. unsigned rgb = ((const uint16_t*)src)[i];
  357. ((uint16_t*)dst)[i] = ((rgb&0x7C00)>>10) | ((rgb&0x3E0)<<1) | (rgb<<11);
  358. }
  359. }
  360. void rgb15tobgr15(const uint8_t *src, uint8_t *dst, long src_size)
  361. {
  362. long i;
  363. long num_pixels = src_size >> 1;
  364. for (i=0; i<num_pixels; i++) {
  365. unsigned br;
  366. unsigned rgb = ((const uint16_t*)src)[i];
  367. br = rgb&0x7c1F;
  368. ((uint16_t*)dst)[i] = (br>>10) | (rgb&0x3E0) | (br<<10);
  369. }
  370. }
  371. void bgr8torgb8(const uint8_t *src, uint8_t *dst, long src_size)
  372. {
  373. long i;
  374. long num_pixels = src_size;
  375. for (i=0; i<num_pixels; i++) {
  376. unsigned b,g,r;
  377. register uint8_t rgb;
  378. rgb = src[i];
  379. r = (rgb&0x07);
  380. g = (rgb&0x38)>>3;
  381. b = (rgb&0xC0)>>6;
  382. dst[i] = ((b<<1)&0x07) | ((g&0x07)<<3) | ((r&0x03)<<6);
  383. }
  384. }
  385. #define DEFINE_SHUFFLE_BYTES(a, b, c, d) \
  386. void shuffle_bytes_##a##b##c##d(const uint8_t *src, uint8_t *dst, long src_size) \
  387. { \
  388. long i; \
  389. \
  390. for (i = 0; i < src_size; i+=4) { \
  391. dst[i + 0] = src[i + a]; \
  392. dst[i + 1] = src[i + b]; \
  393. dst[i + 2] = src[i + c]; \
  394. dst[i + 3] = src[i + d]; \
  395. } \
  396. }
  397. DEFINE_SHUFFLE_BYTES(0, 3, 2, 1);
  398. DEFINE_SHUFFLE_BYTES(1, 2, 3, 0);
  399. DEFINE_SHUFFLE_BYTES(3, 0, 1, 2);
  400. DEFINE_SHUFFLE_BYTES(3, 2, 1, 0);