rgb2rgb_template.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  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. #if !HAVE_BIGENDIAN
  307. #define DEFINE_SHUFFLE_BYTES(name, a, b, c, d) \
  308. static void shuffle_bytes_##name (const uint8_t *src, \
  309. uint8_t *dst, int src_size) \
  310. { \
  311. int i; \
  312. \
  313. for (i = 0; i < src_size; i += 4) { \
  314. dst[i + 0] = src[i + a]; \
  315. dst[i + 1] = src[i + b]; \
  316. dst[i + 2] = src[i + c]; \
  317. dst[i + 3] = src[i + d]; \
  318. } \
  319. }
  320. DEFINE_SHUFFLE_BYTES(1230_c, 1, 2, 3, 0)
  321. DEFINE_SHUFFLE_BYTES(3012_c, 3, 0, 1, 2)
  322. DEFINE_SHUFFLE_BYTES(3210_c, 3, 2, 1, 0)
  323. #endif
  324. static inline void rgb24tobgr24_c(const uint8_t *src, uint8_t *dst, int src_size)
  325. {
  326. unsigned i;
  327. for (i = 0; i < src_size; i += 3) {
  328. register uint8_t x = src[i + 2];
  329. dst[i + 1] = src[i + 1];
  330. dst[i + 2] = src[i + 0];
  331. dst[i + 0] = x;
  332. }
  333. }
  334. static inline void yuvPlanartoyuy2_c(const uint8_t *ysrc, const uint8_t *usrc,
  335. const uint8_t *vsrc, uint8_t *dst,
  336. int width, int height,
  337. int lumStride, int chromStride,
  338. int dstStride, int vertLumPerChroma)
  339. {
  340. int y, i;
  341. const int chromWidth = width >> 1;
  342. for (y = 0; y < height; y++) {
  343. #if HAVE_FAST_64BIT
  344. uint64_t *ldst = (uint64_t *)dst;
  345. const uint8_t *yc = ysrc, *uc = usrc, *vc = vsrc;
  346. for (i = 0; i < chromWidth; i += 2) {
  347. uint64_t k = yc[0] + (uc[0] << 8) +
  348. (yc[1] << 16) + ((unsigned) vc[0] << 24);
  349. uint64_t l = yc[2] + (uc[1] << 8) +
  350. (yc[3] << 16) + ((unsigned) vc[1] << 24);
  351. *ldst++ = k + (l << 32);
  352. yc += 4;
  353. uc += 2;
  354. vc += 2;
  355. }
  356. #else
  357. int *idst = (int32_t *)dst;
  358. const uint8_t *yc = ysrc, *uc = usrc, *vc = vsrc;
  359. for (i = 0; i < chromWidth; i++) {
  360. #if HAVE_BIGENDIAN
  361. *idst++ = (yc[0] << 24) + (uc[0] << 16) +
  362. (yc[1] << 8) + (vc[0] << 0);
  363. #else
  364. *idst++ = yc[0] + (uc[0] << 8) +
  365. (yc[1] << 16) + (vc[0] << 24);
  366. #endif
  367. yc += 2;
  368. uc++;
  369. vc++;
  370. }
  371. #endif
  372. if ((y & (vertLumPerChroma - 1)) == vertLumPerChroma - 1) {
  373. usrc += chromStride;
  374. vsrc += chromStride;
  375. }
  376. ysrc += lumStride;
  377. dst += dstStride;
  378. }
  379. }
  380. /**
  381. * Height should be a multiple of 2 and width should be a multiple of 16.
  382. * (If this is a problem for anyone then tell me, and I will fix it.)
  383. */
  384. static inline void yv12toyuy2_c(const uint8_t *ysrc, const uint8_t *usrc,
  385. const uint8_t *vsrc, uint8_t *dst,
  386. int width, int height, int lumStride,
  387. int chromStride, int dstStride)
  388. {
  389. //FIXME interpolate chroma
  390. yuvPlanartoyuy2_c(ysrc, usrc, vsrc, dst, width, height, lumStride,
  391. chromStride, dstStride, 2);
  392. }
  393. static inline void yuvPlanartouyvy_c(const uint8_t *ysrc, const uint8_t *usrc,
  394. const uint8_t *vsrc, uint8_t *dst,
  395. int width, int height,
  396. int lumStride, int chromStride,
  397. int dstStride, int vertLumPerChroma)
  398. {
  399. int y, i;
  400. const int chromWidth = width >> 1;
  401. for (y = 0; y < height; y++) {
  402. #if HAVE_FAST_64BIT
  403. uint64_t *ldst = (uint64_t *)dst;
  404. const uint8_t *yc = ysrc, *uc = usrc, *vc = vsrc;
  405. for (i = 0; i < chromWidth; i += 2) {
  406. uint64_t k = uc[0] + (yc[0] << 8) +
  407. (vc[0] << 16) + ((unsigned) yc[1] << 24);
  408. uint64_t l = uc[1] + (yc[2] << 8) +
  409. (vc[1] << 16) + ((unsigned) yc[3] << 24);
  410. *ldst++ = k + (l << 32);
  411. yc += 4;
  412. uc += 2;
  413. vc += 2;
  414. }
  415. #else
  416. int *idst = (int32_t *)dst;
  417. const uint8_t *yc = ysrc, *uc = usrc, *vc = vsrc;
  418. for (i = 0; i < chromWidth; i++) {
  419. #if HAVE_BIGENDIAN
  420. *idst++ = (uc[0] << 24) + (yc[0] << 16) +
  421. (vc[0] << 8) + (yc[1] << 0);
  422. #else
  423. *idst++ = uc[0] + (yc[0] << 8) +
  424. (vc[0] << 16) + (yc[1] << 24);
  425. #endif
  426. yc += 2;
  427. uc++;
  428. vc++;
  429. }
  430. #endif
  431. if ((y & (vertLumPerChroma - 1)) == vertLumPerChroma - 1) {
  432. usrc += chromStride;
  433. vsrc += chromStride;
  434. }
  435. ysrc += lumStride;
  436. dst += dstStride;
  437. }
  438. }
  439. /**
  440. * Height should be a multiple of 2 and width should be a multiple of 16
  441. * (If this is a problem for anyone then tell me, and I will fix it.)
  442. */
  443. static inline void yv12touyvy_c(const uint8_t *ysrc, const uint8_t *usrc,
  444. const uint8_t *vsrc, uint8_t *dst,
  445. int width, int height, int lumStride,
  446. int chromStride, int dstStride)
  447. {
  448. //FIXME interpolate chroma
  449. yuvPlanartouyvy_c(ysrc, usrc, vsrc, dst, width, height, lumStride,
  450. chromStride, dstStride, 2);
  451. }
  452. /**
  453. * Width should be a multiple of 16.
  454. */
  455. static inline void yuv422ptouyvy_c(const uint8_t *ysrc, const uint8_t *usrc,
  456. const uint8_t *vsrc, uint8_t *dst,
  457. int width, int height, int lumStride,
  458. int chromStride, int dstStride)
  459. {
  460. yuvPlanartouyvy_c(ysrc, usrc, vsrc, dst, width, height, lumStride,
  461. chromStride, dstStride, 1);
  462. }
  463. /**
  464. * Width should be a multiple of 16.
  465. */
  466. static inline void yuv422ptoyuy2_c(const uint8_t *ysrc, const uint8_t *usrc,
  467. const uint8_t *vsrc, uint8_t *dst,
  468. int width, int height, int lumStride,
  469. int chromStride, int dstStride)
  470. {
  471. yuvPlanartoyuy2_c(ysrc, usrc, vsrc, dst, width, height, lumStride,
  472. chromStride, dstStride, 1);
  473. }
  474. /**
  475. * Height should be a multiple of 2 and width should be a multiple of 16.
  476. * (If this is a problem for anyone then tell me, and I will fix it.)
  477. */
  478. static inline void yuy2toyv12_c(const uint8_t *src, uint8_t *ydst,
  479. uint8_t *udst, uint8_t *vdst,
  480. int width, int height, int lumStride,
  481. int chromStride, int srcStride)
  482. {
  483. int y;
  484. const int chromWidth = width >> 1;
  485. for (y = 0; y < height; y += 2) {
  486. int i;
  487. for (i = 0; i < chromWidth; i++) {
  488. ydst[2 * i + 0] = src[4 * i + 0];
  489. udst[i] = src[4 * i + 1];
  490. ydst[2 * i + 1] = src[4 * i + 2];
  491. vdst[i] = src[4 * i + 3];
  492. }
  493. ydst += lumStride;
  494. src += srcStride;
  495. for (i = 0; i < chromWidth; i++) {
  496. ydst[2 * i + 0] = src[4 * i + 0];
  497. ydst[2 * i + 1] = src[4 * i + 2];
  498. }
  499. udst += chromStride;
  500. vdst += chromStride;
  501. ydst += lumStride;
  502. src += srcStride;
  503. }
  504. }
  505. static inline void planar2x_c(const uint8_t *src, uint8_t *dst, int srcWidth,
  506. int srcHeight, int srcStride, int dstStride)
  507. {
  508. int x, y;
  509. dst[0] = src[0];
  510. // first line
  511. for (x = 0; x < srcWidth - 1; x++) {
  512. dst[2 * x + 1] = (3 * src[x] + src[x + 1]) >> 2;
  513. dst[2 * x + 2] = (src[x] + 3 * src[x + 1]) >> 2;
  514. }
  515. dst[2 * srcWidth - 1] = src[srcWidth - 1];
  516. dst += dstStride;
  517. for (y = 1; y < srcHeight; y++) {
  518. const int mmxSize = 1;
  519. dst[0] = (src[0] * 3 + src[srcStride]) >> 2;
  520. dst[dstStride] = (src[0] + 3 * src[srcStride]) >> 2;
  521. for (x = mmxSize - 1; x < srcWidth - 1; x++) {
  522. dst[2 * x + 1] = (src[x + 0] * 3 + src[x + srcStride + 1]) >> 2;
  523. dst[2 * x + dstStride + 2] = (src[x + 0] + 3 * src[x + srcStride + 1]) >> 2;
  524. dst[2 * x + dstStride + 1] = (src[x + 1] + 3 * src[x + srcStride]) >> 2;
  525. dst[2 * x + 2] = (src[x + 1] * 3 + src[x + srcStride]) >> 2;
  526. }
  527. dst[srcWidth * 2 - 1] = (src[srcWidth - 1] * 3 + src[srcWidth - 1 + srcStride]) >> 2;
  528. dst[srcWidth * 2 - 1 + dstStride] = (src[srcWidth - 1] + 3 * src[srcWidth - 1 + srcStride]) >> 2;
  529. dst += dstStride * 2;
  530. src += srcStride;
  531. }
  532. // last line
  533. dst[0] = src[0];
  534. for (x = 0; x < srcWidth - 1; x++) {
  535. dst[2 * x + 1] = (src[x] * 3 + src[x + 1]) >> 2;
  536. dst[2 * x + 2] = (src[x] + 3 * src[x + 1]) >> 2;
  537. }
  538. dst[2 * srcWidth - 1] = src[srcWidth - 1];
  539. }
  540. /**
  541. * Height should be a multiple of 2 and width should be a multiple of 16.
  542. * (If this is a problem for anyone then tell me, and I will fix it.)
  543. * Chrominance data is only taken from every second line, others are ignored.
  544. * FIXME: Write HQ version.
  545. */
  546. static inline void uyvytoyv12_c(const uint8_t *src, uint8_t *ydst,
  547. uint8_t *udst, uint8_t *vdst,
  548. int width, int height, int lumStride,
  549. int chromStride, int srcStride)
  550. {
  551. int y;
  552. const int chromWidth = width >> 1;
  553. for (y = 0; y < height; y += 2) {
  554. int i;
  555. for (i = 0; i < chromWidth; i++) {
  556. udst[i] = src[4 * i + 0];
  557. ydst[2 * i + 0] = src[4 * i + 1];
  558. vdst[i] = src[4 * i + 2];
  559. ydst[2 * i + 1] = src[4 * i + 3];
  560. }
  561. ydst += lumStride;
  562. src += srcStride;
  563. for (i = 0; i < chromWidth; i++) {
  564. ydst[2 * i + 0] = src[4 * i + 1];
  565. ydst[2 * i + 1] = src[4 * i + 3];
  566. }
  567. udst += chromStride;
  568. vdst += chromStride;
  569. ydst += lumStride;
  570. src += srcStride;
  571. }
  572. }
  573. /**
  574. * Height should be a multiple of 2 and width should be a multiple of 2.
  575. * (If this is a problem for anyone then tell me, and I will fix it.)
  576. * Chrominance data is only taken from every second line,
  577. * others are ignored in the C version.
  578. * FIXME: Write HQ version.
  579. */
  580. void ff_rgb24toyv12_c(const uint8_t *src, uint8_t *ydst, uint8_t *udst,
  581. uint8_t *vdst, int width, int height, int lumStride,
  582. int chromStride, int srcStride, int32_t *rgb2yuv)
  583. {
  584. int32_t ry = rgb2yuv[RY_IDX], gy = rgb2yuv[GY_IDX], by = rgb2yuv[BY_IDX];
  585. int32_t ru = rgb2yuv[RU_IDX], gu = rgb2yuv[GU_IDX], bu = rgb2yuv[BU_IDX];
  586. int32_t rv = rgb2yuv[RV_IDX], gv = rgb2yuv[GV_IDX], bv = rgb2yuv[BV_IDX];
  587. int y;
  588. const int chromWidth = width >> 1;
  589. for (y = 0; y < height; y += 2) {
  590. int i;
  591. for (i = 0; i < chromWidth; i++) {
  592. unsigned int b = src[6 * i + 0];
  593. unsigned int g = src[6 * i + 1];
  594. unsigned int r = src[6 * i + 2];
  595. unsigned int Y = ((ry * r + gy * g + by * b) >> RGB2YUV_SHIFT) + 16;
  596. unsigned int V = ((rv * r + gv * g + bv * b) >> RGB2YUV_SHIFT) + 128;
  597. unsigned int U = ((ru * r + gu * g + bu * b) >> RGB2YUV_SHIFT) + 128;
  598. udst[i] = U;
  599. vdst[i] = V;
  600. ydst[2 * i] = Y;
  601. b = src[6 * i + 3];
  602. g = src[6 * i + 4];
  603. r = src[6 * i + 5];
  604. Y = ((ry * r + gy * g + by * b) >> RGB2YUV_SHIFT) + 16;
  605. ydst[2 * i + 1] = Y;
  606. }
  607. ydst += lumStride;
  608. src += srcStride;
  609. if (y+1 == height)
  610. break;
  611. for (i = 0; i < chromWidth; i++) {
  612. unsigned int b = src[6 * i + 0];
  613. unsigned int g = src[6 * i + 1];
  614. unsigned int r = src[6 * i + 2];
  615. unsigned int Y = ((ry * r + gy * g + by * b) >> RGB2YUV_SHIFT) + 16;
  616. ydst[2 * i] = Y;
  617. b = src[6 * i + 3];
  618. g = src[6 * i + 4];
  619. r = src[6 * i + 5];
  620. Y = ((ry * r + gy * g + by * b) >> RGB2YUV_SHIFT) + 16;
  621. ydst[2 * i + 1] = Y;
  622. }
  623. udst += chromStride;
  624. vdst += chromStride;
  625. ydst += lumStride;
  626. src += srcStride;
  627. }
  628. }
  629. static void interleaveBytes_c(const uint8_t *src1, const uint8_t *src2,
  630. uint8_t *dest, int width, int height,
  631. int src1Stride, int src2Stride, int dstStride)
  632. {
  633. int h;
  634. for (h = 0; h < height; h++) {
  635. int w;
  636. for (w = 0; w < width; w++) {
  637. dest[2 * w + 0] = src1[w];
  638. dest[2 * w + 1] = src2[w];
  639. }
  640. dest += dstStride;
  641. src1 += src1Stride;
  642. src2 += src2Stride;
  643. }
  644. }
  645. static void deinterleaveBytes_c(const uint8_t *src, uint8_t *dst1, uint8_t *dst2,
  646. int width, int height, int srcStride,
  647. int dst1Stride, int dst2Stride)
  648. {
  649. int h;
  650. for (h = 0; h < height; h++) {
  651. int w;
  652. for (w = 0; w < width; w++) {
  653. dst1[w] = src[2 * w + 0];
  654. dst2[w] = src[2 * w + 1];
  655. }
  656. src += srcStride;
  657. dst1 += dst1Stride;
  658. dst2 += dst2Stride;
  659. }
  660. }
  661. static inline void vu9_to_vu12_c(const uint8_t *src1, const uint8_t *src2,
  662. uint8_t *dst1, uint8_t *dst2,
  663. int width, int height,
  664. int srcStride1, int srcStride2,
  665. int dstStride1, int dstStride2)
  666. {
  667. int x, y;
  668. int w = width / 2;
  669. int h = height / 2;
  670. for (y = 0; y < h; y++) {
  671. const uint8_t *s1 = src1 + srcStride1 * (y >> 1);
  672. uint8_t *d = dst1 + dstStride1 * y;
  673. for (x = 0; x < w; x++)
  674. d[2 * x] = d[2 * x + 1] = s1[x];
  675. }
  676. for (y = 0; y < h; y++) {
  677. const uint8_t *s2 = src2 + srcStride2 * (y >> 1);
  678. uint8_t *d = dst2 + dstStride2 * y;
  679. for (x = 0; x < w; x++)
  680. d[2 * x] = d[2 * x + 1] = s2[x];
  681. }
  682. }
  683. static inline void yvu9_to_yuy2_c(const uint8_t *src1, const uint8_t *src2,
  684. const uint8_t *src3, uint8_t *dst,
  685. int width, int height,
  686. int srcStride1, int srcStride2,
  687. int srcStride3, int dstStride)
  688. {
  689. int x, y;
  690. int w = width / 2;
  691. int h = height;
  692. for (y = 0; y < h; y++) {
  693. const uint8_t *yp = src1 + srcStride1 * y;
  694. const uint8_t *up = src2 + srcStride2 * (y >> 2);
  695. const uint8_t *vp = src3 + srcStride3 * (y >> 2);
  696. uint8_t *d = dst + dstStride * y;
  697. for (x = 0; x < w; x++) {
  698. const int x2 = x << 2;
  699. d[8 * x + 0] = yp[x2];
  700. d[8 * x + 1] = up[x];
  701. d[8 * x + 2] = yp[x2 + 1];
  702. d[8 * x + 3] = vp[x];
  703. d[8 * x + 4] = yp[x2 + 2];
  704. d[8 * x + 5] = up[x];
  705. d[8 * x + 6] = yp[x2 + 3];
  706. d[8 * x + 7] = vp[x];
  707. }
  708. }
  709. }
  710. static void extract_even_c(const uint8_t *src, uint8_t *dst, int count)
  711. {
  712. dst += count;
  713. src += count * 2;
  714. count = -count;
  715. while (count < 0) {
  716. dst[count] = src[2 * count];
  717. count++;
  718. }
  719. }
  720. static void extract_even2_c(const uint8_t *src, uint8_t *dst0, uint8_t *dst1,
  721. int count)
  722. {
  723. dst0 += count;
  724. dst1 += count;
  725. src += count * 4;
  726. count = -count;
  727. while (count < 0) {
  728. dst0[count] = src[4 * count + 0];
  729. dst1[count] = src[4 * count + 2];
  730. count++;
  731. }
  732. }
  733. static void extract_even2avg_c(const uint8_t *src0, const uint8_t *src1,
  734. uint8_t *dst0, uint8_t *dst1, int count)
  735. {
  736. dst0 += count;
  737. dst1 += count;
  738. src0 += count * 4;
  739. src1 += count * 4;
  740. count = -count;
  741. while (count < 0) {
  742. dst0[count] = (src0[4 * count + 0] + src1[4 * count + 0]) >> 1;
  743. dst1[count] = (src0[4 * count + 2] + src1[4 * count + 2]) >> 1;
  744. count++;
  745. }
  746. }
  747. static void extract_odd2_c(const uint8_t *src, uint8_t *dst0, uint8_t *dst1,
  748. int count)
  749. {
  750. dst0 += count;
  751. dst1 += count;
  752. src += count * 4;
  753. count = -count;
  754. src++;
  755. while (count < 0) {
  756. dst0[count] = src[4 * count + 0];
  757. dst1[count] = src[4 * count + 2];
  758. count++;
  759. }
  760. }
  761. static void extract_odd2avg_c(const uint8_t *src0, const uint8_t *src1,
  762. uint8_t *dst0, uint8_t *dst1, int count)
  763. {
  764. dst0 += count;
  765. dst1 += count;
  766. src0 += count * 4;
  767. src1 += count * 4;
  768. count = -count;
  769. src0++;
  770. src1++;
  771. while (count < 0) {
  772. dst0[count] = (src0[4 * count + 0] + src1[4 * count + 0]) >> 1;
  773. dst1[count] = (src0[4 * count + 2] + src1[4 * count + 2]) >> 1;
  774. count++;
  775. }
  776. }
  777. static void yuyvtoyuv420_c(uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
  778. const uint8_t *src, int width, int height,
  779. int lumStride, int chromStride, int srcStride)
  780. {
  781. int y;
  782. const int chromWidth = AV_CEIL_RSHIFT(width, 1);
  783. for (y = 0; y < height; y++) {
  784. extract_even_c(src, ydst, width);
  785. if (y & 1) {
  786. extract_odd2avg_c(src - srcStride, src, udst, vdst, chromWidth);
  787. udst += chromStride;
  788. vdst += chromStride;
  789. }
  790. src += srcStride;
  791. ydst += lumStride;
  792. }
  793. }
  794. static void yuyvtoyuv422_c(uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
  795. const uint8_t *src, int width, int height,
  796. int lumStride, int chromStride, int srcStride)
  797. {
  798. int y;
  799. const int chromWidth = AV_CEIL_RSHIFT(width, 1);
  800. for (y = 0; y < height; y++) {
  801. extract_even_c(src, ydst, width);
  802. extract_odd2_c(src, udst, vdst, chromWidth);
  803. src += srcStride;
  804. ydst += lumStride;
  805. udst += chromStride;
  806. vdst += chromStride;
  807. }
  808. }
  809. static void uyvytoyuv420_c(uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
  810. const uint8_t *src, int width, int height,
  811. int lumStride, int chromStride, int srcStride)
  812. {
  813. int y;
  814. const int chromWidth = AV_CEIL_RSHIFT(width, 1);
  815. for (y = 0; y < height; y++) {
  816. extract_even_c(src + 1, ydst, width);
  817. if (y & 1) {
  818. extract_even2avg_c(src - srcStride, src, udst, vdst, chromWidth);
  819. udst += chromStride;
  820. vdst += chromStride;
  821. }
  822. src += srcStride;
  823. ydst += lumStride;
  824. }
  825. }
  826. static void uyvytoyuv422_c(uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
  827. const uint8_t *src, int width, int height,
  828. int lumStride, int chromStride, int srcStride)
  829. {
  830. int y;
  831. const int chromWidth = AV_CEIL_RSHIFT(width, 1);
  832. for (y = 0; y < height; y++) {
  833. extract_even_c(src + 1, ydst, width);
  834. extract_even2_c(src, udst, vdst, chromWidth);
  835. src += srcStride;
  836. ydst += lumStride;
  837. udst += chromStride;
  838. vdst += chromStride;
  839. }
  840. }
  841. static av_cold void rgb2rgb_init_c(void)
  842. {
  843. rgb15to16 = rgb15to16_c;
  844. rgb15tobgr24 = rgb15tobgr24_c;
  845. rgb15to32 = rgb15to32_c;
  846. rgb16tobgr24 = rgb16tobgr24_c;
  847. rgb16to32 = rgb16to32_c;
  848. rgb16to15 = rgb16to15_c;
  849. rgb24tobgr16 = rgb24tobgr16_c;
  850. rgb24tobgr15 = rgb24tobgr15_c;
  851. rgb24tobgr32 = rgb24tobgr32_c;
  852. rgb32to16 = rgb32to16_c;
  853. rgb32to15 = rgb32to15_c;
  854. rgb32tobgr24 = rgb32tobgr24_c;
  855. rgb24to15 = rgb24to15_c;
  856. rgb24to16 = rgb24to16_c;
  857. rgb24tobgr24 = rgb24tobgr24_c;
  858. #if HAVE_BIGENDIAN
  859. shuffle_bytes_0321 = shuffle_bytes_2103_c;
  860. shuffle_bytes_2103 = shuffle_bytes_0321_c;
  861. #else
  862. shuffle_bytes_0321 = shuffle_bytes_0321_c;
  863. shuffle_bytes_2103 = shuffle_bytes_2103_c;
  864. shuffle_bytes_1230 = shuffle_bytes_1230_c;
  865. shuffle_bytes_3012 = shuffle_bytes_3012_c;
  866. shuffle_bytes_3210 = shuffle_bytes_3210_c;
  867. #endif
  868. rgb32tobgr16 = rgb32tobgr16_c;
  869. rgb32tobgr15 = rgb32tobgr15_c;
  870. yv12toyuy2 = yv12toyuy2_c;
  871. yv12touyvy = yv12touyvy_c;
  872. yuv422ptoyuy2 = yuv422ptoyuy2_c;
  873. yuv422ptouyvy = yuv422ptouyvy_c;
  874. yuy2toyv12 = yuy2toyv12_c;
  875. planar2x = planar2x_c;
  876. ff_rgb24toyv12 = ff_rgb24toyv12_c;
  877. interleaveBytes = interleaveBytes_c;
  878. deinterleaveBytes = deinterleaveBytes_c;
  879. vu9_to_vu12 = vu9_to_vu12_c;
  880. yvu9_to_yuy2 = yvu9_to_yuy2_c;
  881. uyvytoyuv420 = uyvytoyuv420_c;
  882. uyvytoyuv422 = uyvytoyuv422_c;
  883. yuyvtoyuv420 = yuyvtoyuv420_c;
  884. yuyvtoyuv422 = yuyvtoyuv422_c;
  885. }