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.

946 lines
29KB

  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. * lot of big-endian byte order fixes by Alex Beregszaszi
  9. *
  10. * This file is part of Libav.
  11. *
  12. * Libav is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU Lesser General Public
  14. * License as published by the Free Software Foundation; either
  15. * version 2.1 of the License, or (at your option) any later version.
  16. *
  17. * Libav is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. * Lesser General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Lesser General Public
  23. * License along with Libav; if not, write to the Free Software
  24. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  25. */
  26. #include <stddef.h>
  27. static inline void rgb24tobgr32_c(const uint8_t *src, uint8_t *dst,
  28. int src_size)
  29. {
  30. uint8_t *dest = dst;
  31. const uint8_t *s = src;
  32. const uint8_t *end = s + src_size;
  33. while (s < end) {
  34. #if HAVE_BIGENDIAN
  35. /* RGB24 (= R, G, B) -> RGB32 (= A, B, G, R) */
  36. *dest++ = 255;
  37. *dest++ = s[2];
  38. *dest++ = s[1];
  39. *dest++ = s[0];
  40. s += 3;
  41. #else
  42. *dest++ = *s++;
  43. *dest++ = *s++;
  44. *dest++ = *s++;
  45. *dest++ = 255;
  46. #endif
  47. }
  48. }
  49. static inline void rgb32tobgr24_c(const uint8_t *src, uint8_t *dst,
  50. int src_size)
  51. {
  52. uint8_t *dest = dst;
  53. const uint8_t *s = src;
  54. const uint8_t *end = s + src_size;
  55. while (s < end) {
  56. #if HAVE_BIGENDIAN
  57. /* RGB32 (= A, B, G, R) -> RGB24 (= R, G, B) */
  58. s++;
  59. dest[2] = *s++;
  60. dest[1] = *s++;
  61. dest[0] = *s++;
  62. dest += 3;
  63. #else
  64. *dest++ = *s++;
  65. *dest++ = *s++;
  66. *dest++ = *s++;
  67. s++;
  68. #endif
  69. }
  70. }
  71. /*
  72. * original by Strepto/Astral
  73. * ported to gcc & bugfixed: A'rpi
  74. * MMX2, 3DNOW optimization by Nick Kurshev
  75. * 32-bit C version, and and&add trick by Michael Niedermayer
  76. */
  77. static inline void rgb15to16_c(const uint8_t *src, uint8_t *dst, int src_size)
  78. {
  79. register uint8_t *d = dst;
  80. register const uint8_t *s = src;
  81. register const uint8_t *end = s + src_size;
  82. const uint8_t *mm_end = end - 3;
  83. while (s < mm_end) {
  84. register unsigned x = *((const uint32_t *)s);
  85. *((uint32_t *)d) = (x & 0x7FFF7FFF) + (x & 0x7FE07FE0);
  86. d += 4;
  87. s += 4;
  88. }
  89. if (s < end) {
  90. register unsigned short x = *((const uint16_t *)s);
  91. *((uint16_t *)d) = (x & 0x7FFF) + (x & 0x7FE0);
  92. }
  93. }
  94. static inline void rgb16to15_c(const uint8_t *src, uint8_t *dst, int src_size)
  95. {
  96. register uint8_t *d = dst;
  97. register const uint8_t *s = src;
  98. register const uint8_t *end = s + src_size;
  99. const uint8_t *mm_end = end - 3;
  100. while (s < mm_end) {
  101. register uint32_t x = *((const uint32_t *)s);
  102. *((uint32_t *)d) = ((x >> 1) & 0x7FE07FE0) | (x & 0x001F001F);
  103. s += 4;
  104. d += 4;
  105. }
  106. if (s < end) {
  107. register uint16_t x = *((const uint16_t *)s);
  108. *((uint16_t *)d) = ((x >> 1) & 0x7FE0) | (x & 0x001F);
  109. }
  110. }
  111. static inline void rgb32to16_c(const uint8_t *src, uint8_t *dst, int src_size)
  112. {
  113. uint16_t *d = (uint16_t *)dst;
  114. const uint8_t *s = src;
  115. const uint8_t *end = s + src_size;
  116. while (s < end) {
  117. register int rgb = *(const uint32_t *)s;
  118. s += 4;
  119. *d++ = ((rgb & 0xFF) >> 3) +
  120. ((rgb & 0xFC00) >> 5) +
  121. ((rgb & 0xF80000) >> 8);
  122. }
  123. }
  124. static inline void rgb32tobgr16_c(const uint8_t *src, uint8_t *dst,
  125. int src_size)
  126. {
  127. uint16_t *d = (uint16_t *)dst;
  128. const uint8_t *s = src;
  129. const uint8_t *end = s + src_size;
  130. while (s < end) {
  131. register int rgb = *(const uint32_t *)s;
  132. s += 4;
  133. *d++ = ((rgb & 0xF8) << 8) +
  134. ((rgb & 0xFC00) >> 5) +
  135. ((rgb & 0xF80000) >> 19);
  136. }
  137. }
  138. static inline void rgb32to15_c(const uint8_t *src, uint8_t *dst, int src_size)
  139. {
  140. uint16_t *d = (uint16_t *)dst;
  141. const uint8_t *s = src;
  142. const uint8_t *end = s + src_size;
  143. while (s < end) {
  144. register int rgb = *(const uint32_t *)s;
  145. s += 4;
  146. *d++ = ((rgb & 0xFF) >> 3) +
  147. ((rgb & 0xF800) >> 6) +
  148. ((rgb & 0xF80000) >> 9);
  149. }
  150. }
  151. static inline void rgb32tobgr15_c(const uint8_t *src, uint8_t *dst,
  152. int src_size)
  153. {
  154. uint16_t *d = (uint16_t *)dst;
  155. const uint8_t *s = src;
  156. const uint8_t *end = s + src_size;
  157. while (s < end) {
  158. register int rgb = *(const uint32_t *)s;
  159. s += 4;
  160. *d++ = ((rgb & 0xF8) << 7) +
  161. ((rgb & 0xF800) >> 6) +
  162. ((rgb & 0xF80000) >> 19);
  163. }
  164. }
  165. static inline void rgb24tobgr16_c(const uint8_t *src, uint8_t *dst,
  166. int src_size)
  167. {
  168. uint16_t *d = (uint16_t *)dst;
  169. const uint8_t *s = src;
  170. const uint8_t *end = s + src_size;
  171. while (s < end) {
  172. const int b = *s++;
  173. const int g = *s++;
  174. const int r = *s++;
  175. *d++ = (b >> 3) | ((g & 0xFC) << 3) | ((r & 0xF8) << 8);
  176. }
  177. }
  178. static inline void rgb24to16_c(const uint8_t *src, uint8_t *dst, int src_size)
  179. {
  180. uint16_t *d = (uint16_t *)dst;
  181. const uint8_t *s = src;
  182. const uint8_t *end = s + src_size;
  183. while (s < end) {
  184. const int r = *s++;
  185. const int g = *s++;
  186. const int b = *s++;
  187. *d++ = (b >> 3) | ((g & 0xFC) << 3) | ((r & 0xF8) << 8);
  188. }
  189. }
  190. static inline void rgb24tobgr15_c(const uint8_t *src, uint8_t *dst,
  191. int src_size)
  192. {
  193. uint16_t *d = (uint16_t *)dst;
  194. const uint8_t *s = src;
  195. const uint8_t *end = s + src_size;
  196. while (s < end) {
  197. const int b = *s++;
  198. const int g = *s++;
  199. const int r = *s++;
  200. *d++ = (b >> 3) | ((g & 0xF8) << 2) | ((r & 0xF8) << 7);
  201. }
  202. }
  203. static inline void rgb24to15_c(const uint8_t *src, uint8_t *dst, int src_size)
  204. {
  205. uint16_t *d = (uint16_t *)dst;
  206. const uint8_t *s = src;
  207. const uint8_t *end = s + src_size;
  208. while (s < end) {
  209. const int r = *s++;
  210. const int g = *s++;
  211. const int b = *s++;
  212. *d++ = (b >> 3) | ((g & 0xF8) << 2) | ((r & 0xF8) << 7);
  213. }
  214. }
  215. /*
  216. * I use less accurate approximation here by simply left-shifting the input
  217. * value and filling the low order bits with zeroes. This method improves PNG
  218. * compression but this scheme cannot reproduce white exactly, since it does
  219. * not generate an all-ones maximum value; the net effect is to darken the
  220. * image slightly.
  221. *
  222. * The better method should be "left bit replication":
  223. *
  224. * 4 3 2 1 0
  225. * ---------
  226. * 1 1 0 1 1
  227. *
  228. * 7 6 5 4 3 2 1 0
  229. * ----------------
  230. * 1 1 0 1 1 1 1 0
  231. * |=======| |===|
  232. * | leftmost bits repeated to fill open bits
  233. * |
  234. * original bits
  235. */
  236. static inline void rgb15tobgr24_c(const uint8_t *src, uint8_t *dst,
  237. int src_size)
  238. {
  239. uint8_t *d = dst;
  240. const uint16_t *s = (const uint16_t *)src;
  241. const uint16_t *end = s + src_size / 2;
  242. while (s < end) {
  243. register uint16_t bgr = *s++;
  244. *d++ = (bgr & 0x1F) << 3;
  245. *d++ = (bgr & 0x3E0) >> 2;
  246. *d++ = (bgr & 0x7C00) >> 7;
  247. }
  248. }
  249. static inline void rgb16tobgr24_c(const uint8_t *src, uint8_t *dst,
  250. int src_size)
  251. {
  252. uint8_t *d = (uint8_t *)dst;
  253. const uint16_t *s = (const uint16_t *)src;
  254. const uint16_t *end = s + src_size / 2;
  255. while (s < end) {
  256. register uint16_t bgr = *s++;
  257. *d++ = (bgr & 0x1F) << 3;
  258. *d++ = (bgr & 0x7E0) >> 3;
  259. *d++ = (bgr & 0xF800) >> 8;
  260. }
  261. }
  262. static inline void rgb15to32_c(const uint8_t *src, uint8_t *dst, int src_size)
  263. {
  264. uint8_t *d = dst;
  265. const uint16_t *s = (const uint16_t *)src;
  266. const uint16_t *end = s + src_size / 2;
  267. while (s < end) {
  268. register uint16_t bgr = *s++;
  269. #if HAVE_BIGENDIAN
  270. *d++ = 255;
  271. *d++ = (bgr & 0x7C00) >> 7;
  272. *d++ = (bgr & 0x3E0) >> 2;
  273. *d++ = (bgr & 0x1F) << 3;
  274. #else
  275. *d++ = (bgr & 0x1F) << 3;
  276. *d++ = (bgr & 0x3E0) >> 2;
  277. *d++ = (bgr & 0x7C00) >> 7;
  278. *d++ = 255;
  279. #endif
  280. }
  281. }
  282. static inline void rgb16to32_c(const uint8_t *src, uint8_t *dst, int src_size)
  283. {
  284. uint8_t *d = dst;
  285. const uint16_t *s = (const uint16_t *)src;
  286. const uint16_t *end = s + src_size / 2;
  287. while (s < end) {
  288. register uint16_t bgr = *s++;
  289. #if HAVE_BIGENDIAN
  290. *d++ = 255;
  291. *d++ = (bgr & 0xF800) >> 8;
  292. *d++ = (bgr & 0x7E0) >> 3;
  293. *d++ = (bgr & 0x1F) << 3;
  294. #else
  295. *d++ = (bgr & 0x1F) << 3;
  296. *d++ = (bgr & 0x7E0) >> 3;
  297. *d++ = (bgr & 0xF800) >> 8;
  298. *d++ = 255;
  299. #endif
  300. }
  301. }
  302. static inline void shuffle_bytes_2103_c(const uint8_t *src, uint8_t *dst,
  303. int src_size)
  304. {
  305. int idx = 15 - src_size;
  306. const uint8_t *s = src - idx;
  307. uint8_t *d = dst - idx;
  308. for (; idx < 15; idx += 4) {
  309. register int v = *(const uint32_t *)&s[idx], g = v & 0xff00ff00;
  310. v &= 0xff00ff;
  311. *(uint32_t *)&d[idx] = (v >> 16) + g + (v << 16);
  312. }
  313. }
  314. static inline void rgb24tobgr24_c(const uint8_t *src, uint8_t *dst, int src_size)
  315. {
  316. unsigned i;
  317. for (i = 0; i < src_size; i += 3) {
  318. register uint8_t x = src[i + 2];
  319. dst[i + 1] = src[i + 1];
  320. dst[i + 2] = src[i + 0];
  321. dst[i + 0] = x;
  322. }
  323. }
  324. static inline void yuvPlanartoyuy2_c(const uint8_t *ysrc, const uint8_t *usrc,
  325. const uint8_t *vsrc, uint8_t *dst,
  326. int width, int height,
  327. int lumStride, int chromStride,
  328. int dstStride, int vertLumPerChroma)
  329. {
  330. int y, i;
  331. const int chromWidth = width >> 1;
  332. for (y = 0; y < height; y++) {
  333. #if HAVE_FAST_64BIT
  334. uint64_t *ldst = (uint64_t *)dst;
  335. const uint8_t *yc = ysrc, *uc = usrc, *vc = vsrc;
  336. for (i = 0; i < chromWidth; i += 2) {
  337. uint64_t k = yc[0] + (uc[0] << 8) +
  338. (yc[1] << 16) + (vc[0] << 24);
  339. uint64_t l = yc[2] + (uc[1] << 8) +
  340. (yc[3] << 16) + (vc[1] << 24);
  341. *ldst++ = k + (l << 32);
  342. yc += 4;
  343. uc += 2;
  344. vc += 2;
  345. }
  346. #else
  347. int *idst = (int32_t *)dst;
  348. const uint8_t *yc = ysrc, *uc = usrc, *vc = vsrc;
  349. for (i = 0; i < chromWidth; i++) {
  350. #if HAVE_BIGENDIAN
  351. *idst++ = (yc[0] << 24) + (uc[0] << 16) +
  352. (yc[1] << 8) + (vc[0] << 0);
  353. #else
  354. *idst++ = yc[0] + (uc[0] << 8) +
  355. (yc[1] << 16) + (vc[0] << 24);
  356. #endif
  357. yc += 2;
  358. uc++;
  359. vc++;
  360. }
  361. #endif
  362. if ((y & (vertLumPerChroma - 1)) == vertLumPerChroma - 1) {
  363. usrc += chromStride;
  364. vsrc += chromStride;
  365. }
  366. ysrc += lumStride;
  367. dst += dstStride;
  368. }
  369. }
  370. /**
  371. * Height should be a multiple of 2 and width should be a multiple of 16.
  372. * (If this is a problem for anyone then tell me, and I will fix it.)
  373. */
  374. static inline void yv12toyuy2_c(const uint8_t *ysrc, const uint8_t *usrc,
  375. const uint8_t *vsrc, uint8_t *dst,
  376. int width, int height, int lumStride,
  377. int chromStride, int dstStride)
  378. {
  379. //FIXME interpolate chroma
  380. yuvPlanartoyuy2_c(ysrc, usrc, vsrc, dst, width, height, lumStride,
  381. chromStride, dstStride, 2);
  382. }
  383. static inline void yuvPlanartouyvy_c(const uint8_t *ysrc, const uint8_t *usrc,
  384. const uint8_t *vsrc, uint8_t *dst,
  385. int width, int height,
  386. int lumStride, int chromStride,
  387. int dstStride, int vertLumPerChroma)
  388. {
  389. int y, i;
  390. const int chromWidth = width >> 1;
  391. for (y = 0; y < height; y++) {
  392. #if HAVE_FAST_64BIT
  393. uint64_t *ldst = (uint64_t *)dst;
  394. const uint8_t *yc = ysrc, *uc = usrc, *vc = vsrc;
  395. for (i = 0; i < chromWidth; i += 2) {
  396. uint64_t k = uc[0] + (yc[0] << 8) +
  397. (vc[0] << 16) + (yc[1] << 24);
  398. uint64_t l = uc[1] + (yc[2] << 8) +
  399. (vc[1] << 16) + (yc[3] << 24);
  400. *ldst++ = k + (l << 32);
  401. yc += 4;
  402. uc += 2;
  403. vc += 2;
  404. }
  405. #else
  406. int *idst = (int32_t *)dst;
  407. const uint8_t *yc = ysrc, *uc = usrc, *vc = vsrc;
  408. for (i = 0; i < chromWidth; i++) {
  409. #if HAVE_BIGENDIAN
  410. *idst++ = (uc[0] << 24) + (yc[0] << 16) +
  411. (vc[0] << 8) + (yc[1] << 0);
  412. #else
  413. *idst++ = uc[0] + (yc[0] << 8) +
  414. (vc[0] << 16) + (yc[1] << 24);
  415. #endif
  416. yc += 2;
  417. uc++;
  418. vc++;
  419. }
  420. #endif
  421. if ((y & (vertLumPerChroma - 1)) == vertLumPerChroma - 1) {
  422. usrc += chromStride;
  423. vsrc += chromStride;
  424. }
  425. ysrc += lumStride;
  426. dst += dstStride;
  427. }
  428. }
  429. /**
  430. * Height should be a multiple of 2 and width should be a multiple of 16
  431. * (If this is a problem for anyone then tell me, and I will fix it.)
  432. */
  433. static inline void yv12touyvy_c(const uint8_t *ysrc, const uint8_t *usrc,
  434. const uint8_t *vsrc, uint8_t *dst,
  435. int width, int height, int lumStride,
  436. int chromStride, int dstStride)
  437. {
  438. //FIXME interpolate chroma
  439. yuvPlanartouyvy_c(ysrc, usrc, vsrc, dst, width, height, lumStride,
  440. chromStride, dstStride, 2);
  441. }
  442. /**
  443. * Width should be a multiple of 16.
  444. */
  445. static inline void yuv422ptouyvy_c(const uint8_t *ysrc, const uint8_t *usrc,
  446. const uint8_t *vsrc, uint8_t *dst,
  447. int width, int height, int lumStride,
  448. int chromStride, int dstStride)
  449. {
  450. yuvPlanartouyvy_c(ysrc, usrc, vsrc, dst, width, height, lumStride,
  451. chromStride, dstStride, 1);
  452. }
  453. /**
  454. * Width should be a multiple of 16.
  455. */
  456. static inline void yuv422ptoyuy2_c(const uint8_t *ysrc, const uint8_t *usrc,
  457. const uint8_t *vsrc, uint8_t *dst,
  458. int width, int height, int lumStride,
  459. int chromStride, int dstStride)
  460. {
  461. yuvPlanartoyuy2_c(ysrc, usrc, vsrc, dst, width, height, lumStride,
  462. chromStride, dstStride, 1);
  463. }
  464. /**
  465. * Height should be a multiple of 2 and width should be a multiple of 16.
  466. * (If this is a problem for anyone then tell me, and I will fix it.)
  467. */
  468. static inline void yuy2toyv12_c(const uint8_t *src, uint8_t *ydst,
  469. uint8_t *udst, uint8_t *vdst,
  470. int width, int height, int lumStride,
  471. int chromStride, int srcStride)
  472. {
  473. int y;
  474. const int chromWidth = width >> 1;
  475. for (y = 0; y < height; y += 2) {
  476. int i;
  477. for (i = 0; i < chromWidth; i++) {
  478. ydst[2 * i + 0] = src[4 * i + 0];
  479. udst[i] = src[4 * i + 1];
  480. ydst[2 * i + 1] = src[4 * i + 2];
  481. vdst[i] = src[4 * i + 3];
  482. }
  483. ydst += lumStride;
  484. src += srcStride;
  485. for (i = 0; i < chromWidth; i++) {
  486. ydst[2 * i + 0] = src[4 * i + 0];
  487. ydst[2 * i + 1] = src[4 * i + 2];
  488. }
  489. udst += chromStride;
  490. vdst += chromStride;
  491. ydst += lumStride;
  492. src += srcStride;
  493. }
  494. }
  495. static inline void planar2x_c(const uint8_t *src, uint8_t *dst, int srcWidth,
  496. int srcHeight, int srcStride, int dstStride)
  497. {
  498. int x, y;
  499. dst[0] = src[0];
  500. // first line
  501. for (x = 0; x < srcWidth - 1; x++) {
  502. dst[2 * x + 1] = (3 * src[x] + src[x + 1]) >> 2;
  503. dst[2 * x + 2] = (src[x] + 3 * src[x + 1]) >> 2;
  504. }
  505. dst[2 * srcWidth - 1] = src[srcWidth - 1];
  506. dst += dstStride;
  507. for (y = 1; y < srcHeight; y++) {
  508. const int mmxSize = 1;
  509. dst[0] = (src[0] * 3 + src[srcStride]) >> 2;
  510. dst[dstStride] = (src[0] + 3 * src[srcStride]) >> 2;
  511. for (x = mmxSize - 1; x < srcWidth - 1; x++) {
  512. dst[2 * x + 1] = (src[x + 0] * 3 + src[x + srcStride + 1]) >> 2;
  513. dst[2 * x + dstStride + 2] = (src[x + 0] + 3 * src[x + srcStride + 1]) >> 2;
  514. dst[2 * x + dstStride + 1] = (src[x + 1] + 3 * src[x + srcStride]) >> 2;
  515. dst[2 * x + 2] = (src[x + 1] * 3 + src[x + srcStride]) >> 2;
  516. }
  517. dst[srcWidth * 2 - 1] = (src[srcWidth - 1] * 3 + src[srcWidth - 1 + srcStride]) >> 2;
  518. dst[srcWidth * 2 - 1 + dstStride] = (src[srcWidth - 1] + 3 * src[srcWidth - 1 + srcStride]) >> 2;
  519. dst += dstStride * 2;
  520. src += srcStride;
  521. }
  522. // last line
  523. dst[0] = src[0];
  524. for (x = 0; x < srcWidth - 1; x++) {
  525. dst[2 * x + 1] = (src[x] * 3 + src[x + 1]) >> 2;
  526. dst[2 * x + 2] = (src[x] + 3 * src[x + 1]) >> 2;
  527. }
  528. dst[2 * srcWidth - 1] = src[srcWidth - 1];
  529. }
  530. /**
  531. * Height should be a multiple of 2 and width should be a multiple of 16.
  532. * (If this is a problem for anyone then tell me, and I will fix it.)
  533. * Chrominance data is only taken from every second line, others are ignored.
  534. * FIXME: Write HQ version.
  535. */
  536. static inline void uyvytoyv12_c(const uint8_t *src, uint8_t *ydst,
  537. uint8_t *udst, uint8_t *vdst,
  538. int width, int height, int lumStride,
  539. int chromStride, int srcStride)
  540. {
  541. int y;
  542. const int chromWidth = width >> 1;
  543. for (y = 0; y < height; y += 2) {
  544. int i;
  545. for (i = 0; i < chromWidth; i++) {
  546. udst[i] = src[4 * i + 0];
  547. ydst[2 * i + 0] = src[4 * i + 1];
  548. vdst[i] = src[4 * i + 2];
  549. ydst[2 * i + 1] = src[4 * i + 3];
  550. }
  551. ydst += lumStride;
  552. src += srcStride;
  553. for (i = 0; i < chromWidth; i++) {
  554. ydst[2 * i + 0] = src[4 * i + 1];
  555. ydst[2 * i + 1] = src[4 * i + 3];
  556. }
  557. udst += chromStride;
  558. vdst += chromStride;
  559. ydst += lumStride;
  560. src += srcStride;
  561. }
  562. }
  563. /**
  564. * Height should be a multiple of 2 and width should be a multiple of 2.
  565. * (If this is a problem for anyone then tell me, and I will fix it.)
  566. * Chrominance data is only taken from every second line,
  567. * others are ignored in the C version.
  568. * FIXME: Write HQ version.
  569. */
  570. void rgb24toyv12_c(const uint8_t *src, uint8_t *ydst, uint8_t *udst,
  571. uint8_t *vdst, int width, int height, int lumStride,
  572. int chromStride, int srcStride)
  573. {
  574. int y;
  575. const int chromWidth = width >> 1;
  576. for (y = 0; y < height; y += 2) {
  577. int i;
  578. for (i = 0; i < chromWidth; i++) {
  579. unsigned int b = src[6 * i + 0];
  580. unsigned int g = src[6 * i + 1];
  581. unsigned int r = src[6 * i + 2];
  582. unsigned int Y = ((RY * r + GY * g + BY * b) >> RGB2YUV_SHIFT) + 16;
  583. unsigned int V = ((RV * r + GV * g + BV * b) >> RGB2YUV_SHIFT) + 128;
  584. unsigned int U = ((RU * r + GU * g + BU * b) >> RGB2YUV_SHIFT) + 128;
  585. udst[i] = U;
  586. vdst[i] = V;
  587. ydst[2 * i] = Y;
  588. b = src[6 * i + 3];
  589. g = src[6 * i + 4];
  590. r = src[6 * i + 5];
  591. Y = ((RY * r + GY * g + BY * b) >> RGB2YUV_SHIFT) + 16;
  592. ydst[2 * i + 1] = Y;
  593. }
  594. ydst += lumStride;
  595. src += srcStride;
  596. for (i = 0; i < chromWidth; i++) {
  597. unsigned int b = src[6 * i + 0];
  598. unsigned int g = src[6 * i + 1];
  599. unsigned int r = src[6 * i + 2];
  600. unsigned int Y = ((RY * r + GY * g + BY * b) >> RGB2YUV_SHIFT) + 16;
  601. ydst[2 * i] = Y;
  602. b = src[6 * i + 3];
  603. g = src[6 * i + 4];
  604. r = src[6 * i + 5];
  605. Y = ((RY * r + GY * g + BY * b) >> RGB2YUV_SHIFT) + 16;
  606. ydst[2 * i + 1] = Y;
  607. }
  608. udst += chromStride;
  609. vdst += chromStride;
  610. ydst += lumStride;
  611. src += srcStride;
  612. }
  613. }
  614. static void interleaveBytes_c(const uint8_t *src1, const uint8_t *src2,
  615. uint8_t *dest, int width, int height,
  616. int src1Stride, int src2Stride, int dstStride)
  617. {
  618. int h;
  619. for (h = 0; h < height; h++) {
  620. int w;
  621. for (w = 0; w < width; w++) {
  622. dest[2 * w + 0] = src1[w];
  623. dest[2 * w + 1] = src2[w];
  624. }
  625. dest += dstStride;
  626. src1 += src1Stride;
  627. src2 += src2Stride;
  628. }
  629. }
  630. static inline void vu9_to_vu12_c(const uint8_t *src1, const uint8_t *src2,
  631. uint8_t *dst1, uint8_t *dst2,
  632. int width, int height,
  633. int srcStride1, int srcStride2,
  634. int dstStride1, int dstStride2)
  635. {
  636. int x, y;
  637. int w = width / 2;
  638. int h = height / 2;
  639. for (y = 0; y < h; y++) {
  640. const uint8_t *s1 = src1 + srcStride1 * (y >> 1);
  641. uint8_t *d = dst1 + dstStride1 * y;
  642. for (x = 0; x < w; x++)
  643. d[2 * x] = d[2 * x + 1] = s1[x];
  644. }
  645. for (y = 0; y < h; y++) {
  646. const uint8_t *s2 = src2 + srcStride2 * (y >> 1);
  647. uint8_t *d = dst2 + dstStride2 * y;
  648. for (x = 0; x < w; x++)
  649. d[2 * x] = d[2 * x + 1] = s2[x];
  650. }
  651. }
  652. static inline void yvu9_to_yuy2_c(const uint8_t *src1, const uint8_t *src2,
  653. const uint8_t *src3, uint8_t *dst,
  654. int width, int height,
  655. int srcStride1, int srcStride2,
  656. int srcStride3, int dstStride)
  657. {
  658. int x, y;
  659. int w = width / 2;
  660. int h = height;
  661. for (y = 0; y < h; y++) {
  662. const uint8_t *yp = src1 + srcStride1 * y;
  663. const uint8_t *up = src2 + srcStride2 * (y >> 2);
  664. const uint8_t *vp = src3 + srcStride3 * (y >> 2);
  665. uint8_t *d = dst + dstStride * y;
  666. for (x = 0; x < w; x++) {
  667. const int x2 = x << 2;
  668. d[8 * x + 0] = yp[x2];
  669. d[8 * x + 1] = up[x];
  670. d[8 * x + 2] = yp[x2 + 1];
  671. d[8 * x + 3] = vp[x];
  672. d[8 * x + 4] = yp[x2 + 2];
  673. d[8 * x + 5] = up[x];
  674. d[8 * x + 6] = yp[x2 + 3];
  675. d[8 * x + 7] = vp[x];
  676. }
  677. }
  678. }
  679. static void extract_even_c(const uint8_t *src, uint8_t *dst, int count)
  680. {
  681. dst += count;
  682. src += count * 2;
  683. count = -count;
  684. while (count < 0) {
  685. dst[count] = src[2 * count];
  686. count++;
  687. }
  688. }
  689. static void extract_even2_c(const uint8_t *src, uint8_t *dst0, uint8_t *dst1,
  690. int count)
  691. {
  692. dst0 += count;
  693. dst1 += count;
  694. src += count * 4;
  695. count = -count;
  696. while (count < 0) {
  697. dst0[count] = src[4 * count + 0];
  698. dst1[count] = src[4 * count + 2];
  699. count++;
  700. }
  701. }
  702. static void extract_even2avg_c(const uint8_t *src0, const uint8_t *src1,
  703. uint8_t *dst0, uint8_t *dst1, int count)
  704. {
  705. dst0 += count;
  706. dst1 += count;
  707. src0 += count * 4;
  708. src1 += count * 4;
  709. count = -count;
  710. while (count < 0) {
  711. dst0[count] = (src0[4 * count + 0] + src1[4 * count + 0]) >> 1;
  712. dst1[count] = (src0[4 * count + 2] + src1[4 * count + 2]) >> 1;
  713. count++;
  714. }
  715. }
  716. static void extract_odd2_c(const uint8_t *src, uint8_t *dst0, uint8_t *dst1,
  717. int count)
  718. {
  719. dst0 += count;
  720. dst1 += count;
  721. src += count * 4;
  722. count = -count;
  723. src++;
  724. while (count < 0) {
  725. dst0[count] = src[4 * count + 0];
  726. dst1[count] = src[4 * count + 2];
  727. count++;
  728. }
  729. }
  730. static void extract_odd2avg_c(const uint8_t *src0, const uint8_t *src1,
  731. uint8_t *dst0, uint8_t *dst1, int count)
  732. {
  733. dst0 += count;
  734. dst1 += count;
  735. src0 += count * 4;
  736. src1 += count * 4;
  737. count = -count;
  738. src0++;
  739. src1++;
  740. while (count < 0) {
  741. dst0[count] = (src0[4 * count + 0] + src1[4 * count + 0]) >> 1;
  742. dst1[count] = (src0[4 * count + 2] + src1[4 * count + 2]) >> 1;
  743. count++;
  744. }
  745. }
  746. static void yuyvtoyuv420_c(uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
  747. const uint8_t *src, int width, int height,
  748. int lumStride, int chromStride, int srcStride)
  749. {
  750. int y;
  751. const int chromWidth = -((-width) >> 1);
  752. for (y = 0; y < height; y++) {
  753. extract_even_c(src, ydst, width);
  754. if (y & 1) {
  755. extract_odd2avg_c(src - srcStride, src, udst, vdst, chromWidth);
  756. udst += chromStride;
  757. vdst += chromStride;
  758. }
  759. src += srcStride;
  760. ydst += lumStride;
  761. }
  762. }
  763. static void yuyvtoyuv422_c(uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
  764. const uint8_t *src, int width, int height,
  765. int lumStride, int chromStride, int srcStride)
  766. {
  767. int y;
  768. const int chromWidth = -((-width) >> 1);
  769. for (y = 0; y < height; y++) {
  770. extract_even_c(src, ydst, width);
  771. extract_odd2_c(src, udst, vdst, chromWidth);
  772. src += srcStride;
  773. ydst += lumStride;
  774. udst += chromStride;
  775. vdst += chromStride;
  776. }
  777. }
  778. static void uyvytoyuv420_c(uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
  779. const uint8_t *src, int width, int height,
  780. int lumStride, int chromStride, int srcStride)
  781. {
  782. int y;
  783. const int chromWidth = -((-width) >> 1);
  784. for (y = 0; y < height; y++) {
  785. extract_even_c(src + 1, ydst, width);
  786. if (y & 1) {
  787. extract_even2avg_c(src - srcStride, src, udst, vdst, chromWidth);
  788. udst += chromStride;
  789. vdst += chromStride;
  790. }
  791. src += srcStride;
  792. ydst += lumStride;
  793. }
  794. }
  795. static void uyvytoyuv422_c(uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
  796. const uint8_t *src, int width, int height,
  797. int lumStride, int chromStride, int srcStride)
  798. {
  799. int y;
  800. const int chromWidth = -((-width) >> 1);
  801. for (y = 0; y < height; y++) {
  802. extract_even_c(src + 1, ydst, width);
  803. extract_even2_c(src, udst, vdst, chromWidth);
  804. src += srcStride;
  805. ydst += lumStride;
  806. udst += chromStride;
  807. vdst += chromStride;
  808. }
  809. }
  810. static inline void rgb2rgb_init_c(void)
  811. {
  812. rgb15to16 = rgb15to16_c;
  813. rgb15tobgr24 = rgb15tobgr24_c;
  814. rgb15to32 = rgb15to32_c;
  815. rgb16tobgr24 = rgb16tobgr24_c;
  816. rgb16to32 = rgb16to32_c;
  817. rgb16to15 = rgb16to15_c;
  818. rgb24tobgr16 = rgb24tobgr16_c;
  819. rgb24tobgr15 = rgb24tobgr15_c;
  820. rgb24tobgr32 = rgb24tobgr32_c;
  821. rgb32to16 = rgb32to16_c;
  822. rgb32to15 = rgb32to15_c;
  823. rgb32tobgr24 = rgb32tobgr24_c;
  824. rgb24to15 = rgb24to15_c;
  825. rgb24to16 = rgb24to16_c;
  826. rgb24tobgr24 = rgb24tobgr24_c;
  827. shuffle_bytes_2103 = shuffle_bytes_2103_c;
  828. rgb32tobgr16 = rgb32tobgr16_c;
  829. rgb32tobgr15 = rgb32tobgr15_c;
  830. yv12toyuy2 = yv12toyuy2_c;
  831. yv12touyvy = yv12touyvy_c;
  832. yuv422ptoyuy2 = yuv422ptoyuy2_c;
  833. yuv422ptouyvy = yuv422ptouyvy_c;
  834. yuy2toyv12 = yuy2toyv12_c;
  835. planar2x = planar2x_c;
  836. rgb24toyv12 = rgb24toyv12_c;
  837. interleaveBytes = interleaveBytes_c;
  838. vu9_to_vu12 = vu9_to_vu12_c;
  839. yvu9_to_yuy2 = yvu9_to_yuy2_c;
  840. uyvytoyuv420 = uyvytoyuv420_c;
  841. uyvytoyuv422 = uyvytoyuv422_c;
  842. yuyvtoyuv420 = yuyvtoyuv420_c;
  843. yuyvtoyuv422 = yuyvtoyuv422_c;
  844. }