rgb2rgb_template.c 30 KB

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