rgb2rgb_template.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  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 FFmpeg.
  11. *
  12. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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. #include "libavutil/attributes.h"
  28. static inline void rgb24tobgr32_c(const uint8_t *src, uint8_t *dst,
  29. int src_size)
  30. {
  31. uint8_t *dest = dst;
  32. const uint8_t *s = src;
  33. const uint8_t *end = s + src_size;
  34. while (s < end) {
  35. #if HAVE_BIGENDIAN
  36. /* RGB24 (= R, G, B) -> RGB32 (= A, B, G, R) */
  37. *dest++ = 255;
  38. *dest++ = s[2];
  39. *dest++ = s[1];
  40. *dest++ = s[0];
  41. s += 3;
  42. #else
  43. *dest++ = *s++;
  44. *dest++ = *s++;
  45. *dest++ = *s++;
  46. *dest++ = 255;
  47. #endif
  48. }
  49. }
  50. static inline void rgb32tobgr24_c(const uint8_t *src, uint8_t *dst,
  51. int src_size)
  52. {
  53. uint8_t *dest = dst;
  54. const uint8_t *s = src;
  55. const uint8_t *end = s + src_size;
  56. while (s < end) {
  57. #if HAVE_BIGENDIAN
  58. /* RGB32 (= A, B, G, R) -> RGB24 (= R, G, B) */
  59. s++;
  60. dest[2] = *s++;
  61. dest[1] = *s++;
  62. dest[0] = *s++;
  63. dest += 3;
  64. #else
  65. *dest++ = *s++;
  66. *dest++ = *s++;
  67. *dest++ = *s++;
  68. s++;
  69. #endif
  70. }
  71. }
  72. /*
  73. * original by Strepto/Astral
  74. * ported to gcc & bugfixed: A'rpi
  75. * MMXEXT, 3DNOW optimization by Nick Kurshev
  76. * 32-bit C version, and and&add trick by Michael Niedermayer
  77. */
  78. static inline void rgb15to16_c(const uint8_t *src, uint8_t *dst, int src_size)
  79. {
  80. register uint8_t *d = dst;
  81. register const uint8_t *s = src;
  82. register const uint8_t *end = s + src_size;
  83. const uint8_t *mm_end = end - 3;
  84. while (s < mm_end) {
  85. register unsigned x = *((const uint32_t *)s);
  86. *((uint32_t *)d) = (x & 0x7FFF7FFF) + (x & 0x7FE07FE0);
  87. d += 4;
  88. s += 4;
  89. }
  90. if (s < end) {
  91. register unsigned short x = *((const uint16_t *)s);
  92. *((uint16_t *)d) = (x & 0x7FFF) + (x & 0x7FE0);
  93. }
  94. }
  95. static inline void rgb16to15_c(const uint8_t *src, uint8_t *dst, int src_size)
  96. {
  97. register uint8_t *d = dst;
  98. register const uint8_t *s = src;
  99. register const uint8_t *end = s + src_size;
  100. const uint8_t *mm_end = end - 3;
  101. while (s < mm_end) {
  102. register uint32_t x = *((const uint32_t *)s);
  103. *((uint32_t *)d) = ((x >> 1) & 0x7FE07FE0) | (x & 0x001F001F);
  104. s += 4;
  105. d += 4;
  106. }
  107. if (s < end) {
  108. register uint16_t x = *((const uint16_t *)s);
  109. *((uint16_t *)d) = ((x >> 1) & 0x7FE0) | (x & 0x001F);
  110. }
  111. }
  112. static inline void rgb32to16_c(const uint8_t *src, uint8_t *dst, int src_size)
  113. {
  114. uint16_t *d = (uint16_t *)dst;
  115. const uint8_t *s = src;
  116. const uint8_t *end = s + src_size;
  117. while (s < end) {
  118. register int rgb = *(const uint32_t *)s;
  119. s += 4;
  120. *d++ = ((rgb & 0xFF) >> 3) +
  121. ((rgb & 0xFC00) >> 5) +
  122. ((rgb & 0xF80000) >> 8);
  123. }
  124. }
  125. static inline void rgb32tobgr16_c(const uint8_t *src, uint8_t *dst,
  126. int src_size)
  127. {
  128. uint16_t *d = (uint16_t *)dst;
  129. const uint8_t *s = src;
  130. const uint8_t *end = s + src_size;
  131. while (s < end) {
  132. register int rgb = *(const uint32_t *)s;
  133. s += 4;
  134. *d++ = ((rgb & 0xF8) << 8) +
  135. ((rgb & 0xFC00) >> 5) +
  136. ((rgb & 0xF80000) >> 19);
  137. }
  138. }
  139. static inline void rgb32to15_c(const uint8_t *src, uint8_t *dst, int src_size)
  140. {
  141. uint16_t *d = (uint16_t *)dst;
  142. const uint8_t *s = src;
  143. const uint8_t *end = s + src_size;
  144. while (s < end) {
  145. register int rgb = *(const uint32_t *)s;
  146. s += 4;
  147. *d++ = ((rgb & 0xFF) >> 3) +
  148. ((rgb & 0xF800) >> 6) +
  149. ((rgb & 0xF80000) >> 9);
  150. }
  151. }
  152. static inline void rgb32tobgr15_c(const uint8_t *src, uint8_t *dst,
  153. int src_size)
  154. {
  155. uint16_t *d = (uint16_t *)dst;
  156. const uint8_t *s = src;
  157. const uint8_t *end = s + src_size;
  158. while (s < end) {
  159. register int rgb = *(const uint32_t *)s;
  160. s += 4;
  161. *d++ = ((rgb & 0xF8) << 7) +
  162. ((rgb & 0xF800) >> 6) +
  163. ((rgb & 0xF80000) >> 19);
  164. }
  165. }
  166. static inline void rgb24tobgr16_c(const uint8_t *src, uint8_t *dst,
  167. int src_size)
  168. {
  169. uint16_t *d = (uint16_t *)dst;
  170. const uint8_t *s = src;
  171. const uint8_t *end = s + src_size;
  172. while (s < end) {
  173. const int b = *s++;
  174. const int g = *s++;
  175. const int r = *s++;
  176. *d++ = (b >> 3) | ((g & 0xFC) << 3) | ((r & 0xF8) << 8);
  177. }
  178. }
  179. static inline void rgb24to16_c(const uint8_t *src, uint8_t *dst, int src_size)
  180. {
  181. uint16_t *d = (uint16_t *)dst;
  182. const uint8_t *s = src;
  183. const uint8_t *end = s + src_size;
  184. while (s < end) {
  185. const int r = *s++;
  186. const int g = *s++;
  187. const int b = *s++;
  188. *d++ = (b >> 3) | ((g & 0xFC) << 3) | ((r & 0xF8) << 8);
  189. }
  190. }
  191. static inline void rgb24tobgr15_c(const uint8_t *src, uint8_t *dst,
  192. int src_size)
  193. {
  194. uint16_t *d = (uint16_t *)dst;
  195. const uint8_t *s = src;
  196. const uint8_t *end = s + src_size;
  197. while (s < end) {
  198. const int b = *s++;
  199. const int g = *s++;
  200. const int r = *s++;
  201. *d++ = (b >> 3) | ((g & 0xF8) << 2) | ((r & 0xF8) << 7);
  202. }
  203. }
  204. static inline void rgb24to15_c(const uint8_t *src, uint8_t *dst, int src_size)
  205. {
  206. uint16_t *d = (uint16_t *)dst;
  207. const uint8_t *s = src;
  208. const uint8_t *end = s + src_size;
  209. while (s < end) {
  210. const int r = *s++;
  211. const int g = *s++;
  212. const int b = *s++;
  213. *d++ = (b >> 3) | ((g & 0xF8) << 2) | ((r & 0xF8) << 7);
  214. }
  215. }
  216. static inline void rgb15tobgr24_c(const uint8_t *src, uint8_t *dst,
  217. int src_size)
  218. {
  219. uint8_t *d = dst;
  220. const uint16_t *s = (const uint16_t *)src;
  221. const uint16_t *end = s + src_size / 2;
  222. while (s < end) {
  223. register uint16_t bgr = *s++;
  224. *d++ = ((bgr&0x001F)<<3) | ((bgr&0x001F)>> 2);
  225. *d++ = ((bgr&0x03E0)>>2) | ((bgr&0x03E0)>> 7);
  226. *d++ = ((bgr&0x7C00)>>7) | ((bgr&0x7C00)>>12);
  227. }
  228. }
  229. static inline void rgb16tobgr24_c(const uint8_t *src, uint8_t *dst,
  230. int src_size)
  231. {
  232. uint8_t *d = (uint8_t *)dst;
  233. const uint16_t *s = (const uint16_t *)src;
  234. const uint16_t *end = s + src_size / 2;
  235. while (s < end) {
  236. register uint16_t bgr = *s++;
  237. *d++ = ((bgr&0x001F)<<3) | ((bgr&0x001F)>> 2);
  238. *d++ = ((bgr&0x07E0)>>3) | ((bgr&0x07E0)>> 9);
  239. *d++ = ((bgr&0xF800)>>8) | ((bgr&0xF800)>>13);
  240. }
  241. }
  242. static inline void rgb15to32_c(const uint8_t *src, uint8_t *dst, int src_size)
  243. {
  244. uint8_t *d = dst;
  245. const uint16_t *s = (const uint16_t *)src;
  246. const uint16_t *end = s + src_size / 2;
  247. while (s < end) {
  248. register uint16_t bgr = *s++;
  249. #if HAVE_BIGENDIAN
  250. *d++ = 255;
  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. #else
  255. *d++ = ((bgr&0x001F)<<3) | ((bgr&0x001F)>> 2);
  256. *d++ = ((bgr&0x03E0)>>2) | ((bgr&0x03E0)>> 7);
  257. *d++ = ((bgr&0x7C00)>>7) | ((bgr&0x7C00)>>12);
  258. *d++ = 255;
  259. #endif
  260. }
  261. }
  262. static inline void rgb16to32_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&0xF800)>>8) | ((bgr&0xF800)>>13);
  272. *d++ = ((bgr&0x07E0)>>3) | ((bgr&0x07E0)>> 9);
  273. *d++ = ((bgr&0x001F)<<3) | ((bgr&0x001F)>> 2);
  274. #else
  275. *d++ = ((bgr&0x001F)<<3) | ((bgr&0x001F)>> 2);
  276. *d++ = ((bgr&0x07E0)>>3) | ((bgr&0x07E0)>> 9);
  277. *d++ = ((bgr&0xF800)>>8) | ((bgr&0xF800)>>13);
  278. *d++ = 255;
  279. #endif
  280. }
  281. }
  282. static inline void shuffle_bytes_2103_c(const uint8_t *src, uint8_t *dst,
  283. int src_size)
  284. {
  285. int idx = 15 - src_size;
  286. const uint8_t *s = src - idx;
  287. uint8_t *d = dst - idx;
  288. for (; idx < 15; idx += 4) {
  289. register unsigned v = *(const uint32_t *)&s[idx], g = v & 0xff00ff00;
  290. v &= 0xff00ff;
  291. *(uint32_t *)&d[idx] = (v >> 16) + g + (v << 16);
  292. }
  293. }
  294. static inline void shuffle_bytes_0321_c(const uint8_t *src, uint8_t *dst,
  295. int src_size)
  296. {
  297. int idx = 15 - src_size;
  298. const uint8_t *s = src - idx;
  299. uint8_t *d = dst - idx;
  300. for (; idx < 15; idx += 4) {
  301. register unsigned v = *(const uint32_t *)&s[idx], g = v & 0x00ff00ff;
  302. v &= 0xff00ff00;
  303. *(uint32_t *)&d[idx] = (v >> 16) + g + (v << 16);
  304. }
  305. }
  306. #define DEFINE_SHUFFLE_BYTES(name, a, b, c, d) \
  307. static void shuffle_bytes_##name (const uint8_t *src, \
  308. uint8_t *dst, int src_size) \
  309. { \
  310. int i; \
  311. \
  312. for (i = 0; i < src_size; i += 4) { \
  313. dst[i + 0] = src[i + a]; \
  314. dst[i + 1] = src[i + b]; \
  315. dst[i + 2] = src[i + c]; \
  316. dst[i + 3] = src[i + d]; \
  317. } \
  318. }
  319. DEFINE_SHUFFLE_BYTES(1230_c, 1, 2, 3, 0)
  320. DEFINE_SHUFFLE_BYTES(3012_c, 3, 0, 1, 2)
  321. DEFINE_SHUFFLE_BYTES(3210_c, 3, 2, 1, 0)
  322. DEFINE_SHUFFLE_BYTES(3102_c, 3, 1, 0, 2)
  323. DEFINE_SHUFFLE_BYTES(2013_c, 2, 0, 1, 3)
  324. DEFINE_SHUFFLE_BYTES(2130_c, 2, 1, 3, 0)
  325. DEFINE_SHUFFLE_BYTES(1203_c, 1, 2, 0, 3)
  326. static inline void rgb24tobgr24_c(const uint8_t *src, uint8_t *dst, int src_size)
  327. {
  328. unsigned i;
  329. for (i = 0; i < src_size; i += 3) {
  330. register uint8_t x = src[i + 2];
  331. dst[i + 1] = src[i + 1];
  332. dst[i + 2] = src[i + 0];
  333. dst[i + 0] = x;
  334. }
  335. }
  336. static inline void yuvPlanartoyuy2_c(const uint8_t *ysrc, const uint8_t *usrc,
  337. const uint8_t *vsrc, uint8_t *dst,
  338. int width, int height,
  339. int lumStride, int chromStride,
  340. int dstStride, int vertLumPerChroma)
  341. {
  342. int y, i;
  343. const int chromWidth = width >> 1;
  344. for (y = 0; y < height; y++) {
  345. #if HAVE_FAST_64BIT
  346. uint64_t *ldst = (uint64_t *)dst;
  347. const uint8_t *yc = ysrc, *uc = usrc, *vc = vsrc;
  348. for (i = 0; i < chromWidth; i += 2) {
  349. uint64_t k = yc[0] + (uc[0] << 8) +
  350. (yc[1] << 16) + ((unsigned) vc[0] << 24);
  351. uint64_t l = yc[2] + (uc[1] << 8) +
  352. (yc[3] << 16) + ((unsigned) vc[1] << 24);
  353. *ldst++ = k + (l << 32);
  354. yc += 4;
  355. uc += 2;
  356. vc += 2;
  357. }
  358. #else
  359. int *idst = (int32_t *)dst;
  360. const uint8_t *yc = ysrc, *uc = usrc, *vc = vsrc;
  361. for (i = 0; i < chromWidth; i++) {
  362. #if HAVE_BIGENDIAN
  363. *idst++ = (yc[0] << 24) + (uc[0] << 16) +
  364. (yc[1] << 8) + (vc[0] << 0);
  365. #else
  366. *idst++ = yc[0] + (uc[0] << 8) +
  367. (yc[1] << 16) + (vc[0] << 24);
  368. #endif
  369. yc += 2;
  370. uc++;
  371. vc++;
  372. }
  373. #endif
  374. if ((y & (vertLumPerChroma - 1)) == vertLumPerChroma - 1) {
  375. usrc += chromStride;
  376. vsrc += chromStride;
  377. }
  378. ysrc += lumStride;
  379. dst += dstStride;
  380. }
  381. }
  382. /**
  383. * Height should be a multiple of 2 and width should be a multiple of 16.
  384. * (If this is a problem for anyone then tell me, and I will fix it.)
  385. */
  386. static inline void yv12toyuy2_c(const uint8_t *ysrc, const uint8_t *usrc,
  387. const uint8_t *vsrc, uint8_t *dst,
  388. int width, int height, int lumStride,
  389. int chromStride, int dstStride)
  390. {
  391. //FIXME interpolate chroma
  392. yuvPlanartoyuy2_c(ysrc, usrc, vsrc, dst, width, height, lumStride,
  393. chromStride, dstStride, 2);
  394. }
  395. static inline void yuvPlanartouyvy_c(const uint8_t *ysrc, const uint8_t *usrc,
  396. const uint8_t *vsrc, uint8_t *dst,
  397. int width, int height,
  398. int lumStride, int chromStride,
  399. int dstStride, int vertLumPerChroma)
  400. {
  401. int y, i;
  402. const int chromWidth = width >> 1;
  403. for (y = 0; y < height; y++) {
  404. #if HAVE_FAST_64BIT
  405. uint64_t *ldst = (uint64_t *)dst;
  406. const uint8_t *yc = ysrc, *uc = usrc, *vc = vsrc;
  407. for (i = 0; i < chromWidth; i += 2) {
  408. uint64_t k = uc[0] + (yc[0] << 8) +
  409. (vc[0] << 16) + ((unsigned) yc[1] << 24);
  410. uint64_t l = uc[1] + (yc[2] << 8) +
  411. (vc[1] << 16) + ((unsigned) yc[3] << 24);
  412. *ldst++ = k + (l << 32);
  413. yc += 4;
  414. uc += 2;
  415. vc += 2;
  416. }
  417. #else
  418. int *idst = (int32_t *)dst;
  419. const uint8_t *yc = ysrc, *uc = usrc, *vc = vsrc;
  420. for (i = 0; i < chromWidth; i++) {
  421. #if HAVE_BIGENDIAN
  422. *idst++ = (uc[0] << 24) + (yc[0] << 16) +
  423. (vc[0] << 8) + (yc[1] << 0);
  424. #else
  425. *idst++ = uc[0] + (yc[0] << 8) +
  426. (vc[0] << 16) + (yc[1] << 24);
  427. #endif
  428. yc += 2;
  429. uc++;
  430. vc++;
  431. }
  432. #endif
  433. if ((y & (vertLumPerChroma - 1)) == vertLumPerChroma - 1) {
  434. usrc += chromStride;
  435. vsrc += chromStride;
  436. }
  437. ysrc += lumStride;
  438. dst += dstStride;
  439. }
  440. }
  441. /**
  442. * Height should be a multiple of 2 and width should be a multiple of 16
  443. * (If this is a problem for anyone then tell me, and I will fix it.)
  444. */
  445. static inline void yv12touyvy_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. //FIXME interpolate chroma
  451. yuvPlanartouyvy_c(ysrc, usrc, vsrc, dst, width, height, lumStride,
  452. chromStride, dstStride, 2);
  453. }
  454. /**
  455. * Width should be a multiple of 16.
  456. */
  457. static inline void yuv422ptouyvy_c(const uint8_t *ysrc, const uint8_t *usrc,
  458. const uint8_t *vsrc, uint8_t *dst,
  459. int width, int height, int lumStride,
  460. int chromStride, int dstStride)
  461. {
  462. yuvPlanartouyvy_c(ysrc, usrc, vsrc, dst, width, height, lumStride,
  463. chromStride, dstStride, 1);
  464. }
  465. /**
  466. * Width should be a multiple of 16.
  467. */
  468. static inline void yuv422ptoyuy2_c(const uint8_t *ysrc, const uint8_t *usrc,
  469. const uint8_t *vsrc, uint8_t *dst,
  470. int width, int height, int lumStride,
  471. int chromStride, int dstStride)
  472. {
  473. yuvPlanartoyuy2_c(ysrc, usrc, vsrc, dst, width, height, lumStride,
  474. chromStride, dstStride, 1);
  475. }
  476. /**
  477. * Height should be a multiple of 2 and width should be a multiple of 16.
  478. * (If this is a problem for anyone then tell me, and I will fix it.)
  479. */
  480. static inline void yuy2toyv12_c(const uint8_t *src, uint8_t *ydst,
  481. uint8_t *udst, uint8_t *vdst,
  482. int width, int height, int lumStride,
  483. int chromStride, int srcStride)
  484. {
  485. int y;
  486. const int chromWidth = width >> 1;
  487. for (y = 0; y < height; y += 2) {
  488. int i;
  489. for (i = 0; i < chromWidth; i++) {
  490. ydst[2 * i + 0] = src[4 * i + 0];
  491. udst[i] = src[4 * i + 1];
  492. ydst[2 * i + 1] = src[4 * i + 2];
  493. vdst[i] = src[4 * i + 3];
  494. }
  495. ydst += lumStride;
  496. src += srcStride;
  497. for (i = 0; i < chromWidth; i++) {
  498. ydst[2 * i + 0] = src[4 * i + 0];
  499. ydst[2 * i + 1] = src[4 * i + 2];
  500. }
  501. udst += chromStride;
  502. vdst += chromStride;
  503. ydst += lumStride;
  504. src += srcStride;
  505. }
  506. }
  507. static inline void planar2x_c(const uint8_t *src, uint8_t *dst, int srcWidth,
  508. int srcHeight, int srcStride, int dstStride)
  509. {
  510. int x, y;
  511. dst[0] = src[0];
  512. // first line
  513. for (x = 0; x < srcWidth - 1; x++) {
  514. dst[2 * x + 1] = (3 * src[x] + src[x + 1]) >> 2;
  515. dst[2 * x + 2] = (src[x] + 3 * src[x + 1]) >> 2;
  516. }
  517. dst[2 * srcWidth - 1] = src[srcWidth - 1];
  518. dst += dstStride;
  519. for (y = 1; y < srcHeight; y++) {
  520. const int mmxSize = 1;
  521. dst[0] = (src[0] * 3 + src[srcStride]) >> 2;
  522. dst[dstStride] = (src[0] + 3 * src[srcStride]) >> 2;
  523. for (x = mmxSize - 1; x < srcWidth - 1; x++) {
  524. dst[2 * x + 1] = (src[x + 0] * 3 + src[x + srcStride + 1]) >> 2;
  525. dst[2 * x + dstStride + 2] = (src[x + 0] + 3 * src[x + srcStride + 1]) >> 2;
  526. dst[2 * x + dstStride + 1] = (src[x + 1] + 3 * src[x + srcStride]) >> 2;
  527. dst[2 * x + 2] = (src[x + 1] * 3 + src[x + srcStride]) >> 2;
  528. }
  529. dst[srcWidth * 2 - 1] = (src[srcWidth - 1] * 3 + src[srcWidth - 1 + srcStride]) >> 2;
  530. dst[srcWidth * 2 - 1 + dstStride] = (src[srcWidth - 1] + 3 * src[srcWidth - 1 + srcStride]) >> 2;
  531. dst += dstStride * 2;
  532. src += srcStride;
  533. }
  534. // last line
  535. dst[0] = src[0];
  536. for (x = 0; x < srcWidth - 1; x++) {
  537. dst[2 * x + 1] = (src[x] * 3 + src[x + 1]) >> 2;
  538. dst[2 * x + 2] = (src[x] + 3 * src[x + 1]) >> 2;
  539. }
  540. dst[2 * srcWidth - 1] = src[srcWidth - 1];
  541. }
  542. /**
  543. * Height should be a multiple of 2 and width should be a multiple of 16.
  544. * (If this is a problem for anyone then tell me, and I will fix it.)
  545. * Chrominance data is only taken from every second line, others are ignored.
  546. * FIXME: Write HQ version.
  547. */
  548. static inline void uyvytoyv12_c(const uint8_t *src, uint8_t *ydst,
  549. uint8_t *udst, uint8_t *vdst,
  550. int width, int height, int lumStride,
  551. int chromStride, int srcStride)
  552. {
  553. int y;
  554. const int chromWidth = width >> 1;
  555. for (y = 0; y < height; y += 2) {
  556. int i;
  557. for (i = 0; i < chromWidth; i++) {
  558. udst[i] = src[4 * i + 0];
  559. ydst[2 * i + 0] = src[4 * i + 1];
  560. vdst[i] = src[4 * i + 2];
  561. ydst[2 * i + 1] = src[4 * i + 3];
  562. }
  563. ydst += lumStride;
  564. src += srcStride;
  565. for (i = 0; i < chromWidth; i++) {
  566. ydst[2 * i + 0] = src[4 * i + 1];
  567. ydst[2 * i + 1] = src[4 * i + 3];
  568. }
  569. udst += chromStride;
  570. vdst += chromStride;
  571. ydst += lumStride;
  572. src += srcStride;
  573. }
  574. }
  575. /**
  576. * width should be a multiple of 2.
  577. * (If this is a problem for anyone then tell me, and I will fix it.)
  578. */
  579. void ff_rgb24toyv12_c(const uint8_t *src, uint8_t *ydst, uint8_t *udst,
  580. uint8_t *vdst, int width, int height, int lumStride,
  581. int chromStride, int srcStride, const int32_t *rgb2yuv)
  582. {
  583. int32_t ry = rgb2yuv[RY_IDX], gy = rgb2yuv[GY_IDX], by = rgb2yuv[BY_IDX];
  584. int32_t ru = rgb2yuv[RU_IDX], gu = rgb2yuv[GU_IDX], bu = rgb2yuv[BU_IDX];
  585. int32_t rv = rgb2yuv[RV_IDX], gv = rgb2yuv[GV_IDX], bv = rgb2yuv[BV_IDX];
  586. int y;
  587. const int chromWidth = width >> 1;
  588. const uint8_t *src1 = src;
  589. const uint8_t *src2 = src1 + srcStride;
  590. uint8_t *ydst1 = ydst;
  591. uint8_t *ydst2 = ydst + lumStride;
  592. for (y = 0; y < height; y += 2) {
  593. int i;
  594. if (y + 1 == height) {
  595. ydst2 = ydst1;
  596. src2 = src1;
  597. }
  598. for (i = 0; i < chromWidth; i++) {
  599. unsigned int b11 = src1[6 * i + 0];
  600. unsigned int g11 = src1[6 * i + 1];
  601. unsigned int r11 = src1[6 * i + 2];
  602. unsigned int b12 = src1[6 * i + 3];
  603. unsigned int g12 = src1[6 * i + 4];
  604. unsigned int r12 = src1[6 * i + 5];
  605. unsigned int b21 = src2[6 * i + 0];
  606. unsigned int g21 = src2[6 * i + 1];
  607. unsigned int r21 = src2[6 * i + 2];
  608. unsigned int b22 = src2[6 * i + 3];
  609. unsigned int g22 = src2[6 * i + 4];
  610. unsigned int r22 = src2[6 * i + 5];
  611. unsigned int Y11 = ((ry * r11 + gy * g11 + by * b11) >> RGB2YUV_SHIFT) + 16;
  612. unsigned int Y12 = ((ry * r12 + gy * g12 + by * b12) >> RGB2YUV_SHIFT) + 16;
  613. unsigned int Y21 = ((ry * r21 + gy * g21 + by * b21) >> RGB2YUV_SHIFT) + 16;
  614. unsigned int Y22 = ((ry * r22 + gy * g22 + by * b22) >> RGB2YUV_SHIFT) + 16;
  615. unsigned int bx = (b11 + b12 + b21 + b22) >> 2;
  616. unsigned int gx = (g11 + g12 + g21 + g22) >> 2;
  617. unsigned int rx = (r11 + r12 + r21 + r22) >> 2;
  618. unsigned int U = ((ru * rx + gu * gx + bu * bx) >> RGB2YUV_SHIFT) + 128;
  619. unsigned int V = ((rv * rx + gv * gx + bv * bx) >> RGB2YUV_SHIFT) + 128;
  620. ydst1[2 * i + 0] = Y11;
  621. ydst1[2 * i + 1] = Y12;
  622. ydst2[2 * i + 0] = Y21;
  623. ydst2[2 * i + 1] = Y22;
  624. udst[i] = U;
  625. vdst[i] = V;
  626. }
  627. src1 += srcStride * 2;
  628. src2 += srcStride * 2;
  629. ydst1 += lumStride * 2;
  630. ydst2 += lumStride * 2;
  631. udst += chromStride;
  632. vdst += chromStride;
  633. }
  634. }
  635. static void interleaveBytes_c(const uint8_t *src1, const uint8_t *src2,
  636. uint8_t *dest, int width, int height,
  637. int src1Stride, int src2Stride, int dstStride)
  638. {
  639. int h;
  640. for (h = 0; h < height; h++) {
  641. int w;
  642. for (w = 0; w < width; w++) {
  643. dest[2 * w + 0] = src1[w];
  644. dest[2 * w + 1] = src2[w];
  645. }
  646. dest += dstStride;
  647. src1 += src1Stride;
  648. src2 += src2Stride;
  649. }
  650. }
  651. static void deinterleaveBytes_c(const uint8_t *src, uint8_t *dst1, uint8_t *dst2,
  652. int width, int height, int srcStride,
  653. int dst1Stride, int dst2Stride)
  654. {
  655. int h;
  656. for (h = 0; h < height; h++) {
  657. int w;
  658. for (w = 0; w < width; w++) {
  659. dst1[w] = src[2 * w + 0];
  660. dst2[w] = src[2 * w + 1];
  661. }
  662. src += srcStride;
  663. dst1 += dst1Stride;
  664. dst2 += dst2Stride;
  665. }
  666. }
  667. static inline void vu9_to_vu12_c(const uint8_t *src1, const uint8_t *src2,
  668. uint8_t *dst1, uint8_t *dst2,
  669. int width, int height,
  670. int srcStride1, int srcStride2,
  671. int dstStride1, int dstStride2)
  672. {
  673. int x, y;
  674. int w = width / 2;
  675. int h = height / 2;
  676. for (y = 0; y < h; y++) {
  677. const uint8_t *s1 = src1 + srcStride1 * (y >> 1);
  678. uint8_t *d = dst1 + dstStride1 * y;
  679. for (x = 0; x < w; x++)
  680. d[2 * x] = d[2 * x + 1] = s1[x];
  681. }
  682. for (y = 0; y < h; y++) {
  683. const uint8_t *s2 = src2 + srcStride2 * (y >> 1);
  684. uint8_t *d = dst2 + dstStride2 * y;
  685. for (x = 0; x < w; x++)
  686. d[2 * x] = d[2 * x + 1] = s2[x];
  687. }
  688. }
  689. static inline void yvu9_to_yuy2_c(const uint8_t *src1, const uint8_t *src2,
  690. const uint8_t *src3, uint8_t *dst,
  691. int width, int height,
  692. int srcStride1, int srcStride2,
  693. int srcStride3, int dstStride)
  694. {
  695. int x, y;
  696. int w = width / 2;
  697. int h = height;
  698. for (y = 0; y < h; y++) {
  699. const uint8_t *yp = src1 + srcStride1 * y;
  700. const uint8_t *up = src2 + srcStride2 * (y >> 2);
  701. const uint8_t *vp = src3 + srcStride3 * (y >> 2);
  702. uint8_t *d = dst + dstStride * y;
  703. for (x = 0; x < w; x++) {
  704. const int x2 = x << 2;
  705. d[8 * x + 0] = yp[x2];
  706. d[8 * x + 1] = up[x];
  707. d[8 * x + 2] = yp[x2 + 1];
  708. d[8 * x + 3] = vp[x];
  709. d[8 * x + 4] = yp[x2 + 2];
  710. d[8 * x + 5] = up[x];
  711. d[8 * x + 6] = yp[x2 + 3];
  712. d[8 * x + 7] = vp[x];
  713. }
  714. }
  715. }
  716. static void extract_even_c(const uint8_t *src, uint8_t *dst, int count)
  717. {
  718. dst += count;
  719. src += count * 2;
  720. count = -count;
  721. while (count < 0) {
  722. dst[count] = src[2 * count];
  723. count++;
  724. }
  725. }
  726. static void extract_even2_c(const uint8_t *src, uint8_t *dst0, uint8_t *dst1,
  727. int count)
  728. {
  729. dst0 += count;
  730. dst1 += count;
  731. src += count * 4;
  732. count = -count;
  733. while (count < 0) {
  734. dst0[count] = src[4 * count + 0];
  735. dst1[count] = src[4 * count + 2];
  736. count++;
  737. }
  738. }
  739. static void extract_even2avg_c(const uint8_t *src0, const uint8_t *src1,
  740. uint8_t *dst0, uint8_t *dst1, int count)
  741. {
  742. dst0 += count;
  743. dst1 += count;
  744. src0 += count * 4;
  745. src1 += count * 4;
  746. count = -count;
  747. while (count < 0) {
  748. dst0[count] = (src0[4 * count + 0] + src1[4 * count + 0]) >> 1;
  749. dst1[count] = (src0[4 * count + 2] + src1[4 * count + 2]) >> 1;
  750. count++;
  751. }
  752. }
  753. static void extract_odd2_c(const uint8_t *src, uint8_t *dst0, uint8_t *dst1,
  754. int count)
  755. {
  756. dst0 += count;
  757. dst1 += count;
  758. src += count * 4;
  759. count = -count;
  760. src++;
  761. while (count < 0) {
  762. dst0[count] = src[4 * count + 0];
  763. dst1[count] = src[4 * count + 2];
  764. count++;
  765. }
  766. }
  767. static void extract_odd2avg_c(const uint8_t *src0, const uint8_t *src1,
  768. uint8_t *dst0, uint8_t *dst1, int count)
  769. {
  770. dst0 += count;
  771. dst1 += count;
  772. src0 += count * 4;
  773. src1 += count * 4;
  774. count = -count;
  775. src0++;
  776. src1++;
  777. while (count < 0) {
  778. dst0[count] = (src0[4 * count + 0] + src1[4 * count + 0]) >> 1;
  779. dst1[count] = (src0[4 * count + 2] + src1[4 * count + 2]) >> 1;
  780. count++;
  781. }
  782. }
  783. static void yuyvtoyuv420_c(uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
  784. const uint8_t *src, int width, int height,
  785. int lumStride, int chromStride, int srcStride)
  786. {
  787. int y;
  788. const int chromWidth = AV_CEIL_RSHIFT(width, 1);
  789. for (y = 0; y < height; y++) {
  790. extract_even_c(src, ydst, width);
  791. if (y & 1) {
  792. extract_odd2avg_c(src - srcStride, src, udst, vdst, chromWidth);
  793. udst += chromStride;
  794. vdst += chromStride;
  795. }
  796. src += srcStride;
  797. ydst += lumStride;
  798. }
  799. }
  800. static void yuyvtoyuv422_c(uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
  801. const uint8_t *src, int width, int height,
  802. int lumStride, int chromStride, int srcStride)
  803. {
  804. int y;
  805. const int chromWidth = AV_CEIL_RSHIFT(width, 1);
  806. for (y = 0; y < height; y++) {
  807. extract_even_c(src, ydst, width);
  808. extract_odd2_c(src, udst, vdst, chromWidth);
  809. src += srcStride;
  810. ydst += lumStride;
  811. udst += chromStride;
  812. vdst += chromStride;
  813. }
  814. }
  815. static void uyvytoyuv420_c(uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
  816. const uint8_t *src, int width, int height,
  817. int lumStride, int chromStride, int srcStride)
  818. {
  819. int y;
  820. const int chromWidth = AV_CEIL_RSHIFT(width, 1);
  821. for (y = 0; y < height; y++) {
  822. extract_even_c(src + 1, ydst, width);
  823. if (y & 1) {
  824. extract_even2avg_c(src - srcStride, src, udst, vdst, chromWidth);
  825. udst += chromStride;
  826. vdst += chromStride;
  827. }
  828. src += srcStride;
  829. ydst += lumStride;
  830. }
  831. }
  832. static void uyvytoyuv422_c(uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
  833. const uint8_t *src, int width, int height,
  834. int lumStride, int chromStride, int srcStride)
  835. {
  836. int y;
  837. const int chromWidth = AV_CEIL_RSHIFT(width, 1);
  838. for (y = 0; y < height; y++) {
  839. extract_even_c(src + 1, ydst, width);
  840. extract_even2_c(src, udst, vdst, chromWidth);
  841. src += srcStride;
  842. ydst += lumStride;
  843. udst += chromStride;
  844. vdst += chromStride;
  845. }
  846. }
  847. static av_cold void rgb2rgb_init_c(void)
  848. {
  849. rgb15to16 = rgb15to16_c;
  850. rgb15tobgr24 = rgb15tobgr24_c;
  851. rgb15to32 = rgb15to32_c;
  852. rgb16tobgr24 = rgb16tobgr24_c;
  853. rgb16to32 = rgb16to32_c;
  854. rgb16to15 = rgb16to15_c;
  855. rgb24tobgr16 = rgb24tobgr16_c;
  856. rgb24tobgr15 = rgb24tobgr15_c;
  857. rgb24tobgr32 = rgb24tobgr32_c;
  858. rgb32to16 = rgb32to16_c;
  859. rgb32to15 = rgb32to15_c;
  860. rgb32tobgr24 = rgb32tobgr24_c;
  861. rgb24to15 = rgb24to15_c;
  862. rgb24to16 = rgb24to16_c;
  863. rgb24tobgr24 = rgb24tobgr24_c;
  864. #if HAVE_BIGENDIAN
  865. shuffle_bytes_0321 = shuffle_bytes_2103_c;
  866. shuffle_bytes_2103 = shuffle_bytes_0321_c;
  867. #else
  868. shuffle_bytes_0321 = shuffle_bytes_0321_c;
  869. shuffle_bytes_2103 = shuffle_bytes_2103_c;
  870. #endif
  871. shuffle_bytes_1230 = shuffle_bytes_1230_c;
  872. shuffle_bytes_3012 = shuffle_bytes_3012_c;
  873. shuffle_bytes_3210 = shuffle_bytes_3210_c;
  874. shuffle_bytes_3102 = shuffle_bytes_3102_c;
  875. shuffle_bytes_2013 = shuffle_bytes_2013_c;
  876. shuffle_bytes_2130 = shuffle_bytes_2130_c;
  877. shuffle_bytes_1203 = shuffle_bytes_1203_c;
  878. rgb32tobgr16 = rgb32tobgr16_c;
  879. rgb32tobgr15 = rgb32tobgr15_c;
  880. yv12toyuy2 = yv12toyuy2_c;
  881. yv12touyvy = yv12touyvy_c;
  882. yuv422ptoyuy2 = yuv422ptoyuy2_c;
  883. yuv422ptouyvy = yuv422ptouyvy_c;
  884. yuy2toyv12 = yuy2toyv12_c;
  885. planar2x = planar2x_c;
  886. ff_rgb24toyv12 = ff_rgb24toyv12_c;
  887. interleaveBytes = interleaveBytes_c;
  888. deinterleaveBytes = deinterleaveBytes_c;
  889. vu9_to_vu12 = vu9_to_vu12_c;
  890. yvu9_to_yuy2 = yvu9_to_yuy2_c;
  891. uyvytoyuv420 = uyvytoyuv420_c;
  892. uyvytoyuv422 = uyvytoyuv422_c;
  893. yuyvtoyuv420 = yuyvtoyuv420_c;
  894. yuyvtoyuv422 = yuyvtoyuv422_c;
  895. }