rgb2rgb_template.c 29 KB

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