rgb2rgb_template.c 31 KB

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