rgb2rgb.c 12 KB

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