yuv2rgb_altivec.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  1. /*
  2. * AltiVec acceleration for colorspace conversion
  3. *
  4. * copyright (C) 2004 Marc Hoffman <marc.hoffman@analog.com>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /*
  23. * Convert I420 YV12 to RGB in various formats,
  24. * it rejects images that are not in 420 formats,
  25. * it rejects images that don't have widths of multiples of 16,
  26. * it rejects images that don't have heights of multiples of 2.
  27. * Reject defers to C simulation code.
  28. *
  29. * Lots of optimizations to be done here.
  30. *
  31. * 1. Need to fix saturation code. I just couldn't get it to fly with packs
  32. * and adds, so we currently use max/min to clip.
  33. *
  34. * 2. The inefficient use of chroma loading needs a bit of brushing up.
  35. *
  36. * 3. Analysis of pipeline stalls needs to be done. Use shark to identify
  37. * pipeline stalls.
  38. *
  39. *
  40. * MODIFIED to calculate coeffs from currently selected color space.
  41. * MODIFIED core to be a macro where you specify the output format.
  42. * ADDED UYVY conversion which is never called due to some thing in swscale.
  43. * CORRECTED algorithim selection to be strict on input formats.
  44. * ADDED runtime detection of AltiVec.
  45. *
  46. * ADDED altivec_yuv2packedX vertical scl + RGB converter
  47. *
  48. * March 27,2004
  49. * PERFORMANCE ANALYSIS
  50. *
  51. * The C version uses 25% of the processor or ~250Mips for D1 video rawvideo
  52. * used as test.
  53. * The AltiVec version uses 10% of the processor or ~100Mips for D1 video
  54. * same sequence.
  55. *
  56. * 720 * 480 * 30 ~10MPS
  57. *
  58. * so we have roughly 10 clocks per pixel. This is too high, something has
  59. * to be wrong.
  60. *
  61. * OPTIMIZED clip codes to utilize vec_max and vec_packs removing the
  62. * need for vec_min.
  63. *
  64. * OPTIMIZED DST OUTPUT cache/DMA controls. We are pretty much guaranteed to
  65. * have the input video frame, it was just decompressed so it probably resides
  66. * in L1 caches. However, we are creating the output video stream. This needs
  67. * to use the DSTST instruction to optimize for the cache. We couple this with
  68. * the fact that we are not going to be visiting the input buffer again so we
  69. * mark it Least Recently Used. This shaves 25% of the processor cycles off.
  70. *
  71. * Now memcpy is the largest mips consumer in the system, probably due
  72. * to the inefficient X11 stuff.
  73. *
  74. * GL libraries seem to be very slow on this machine 1.33Ghz PB running
  75. * Jaguar, this is not the case for my 1Ghz PB. I thought it might be
  76. * a versioning issue, however I have libGL.1.2.dylib for both
  77. * machines. (We need to figure this out now.)
  78. *
  79. * GL2 libraries work now with patch for RGB32.
  80. *
  81. * NOTE: quartz vo driver ARGB32_to_RGB24 consumes 30% of the processor.
  82. *
  83. * Integrated luma prescaling adjustment for saturation/contrast/brightness
  84. * adjustment.
  85. */
  86. #include <stdio.h>
  87. #include <stdlib.h>
  88. #include <string.h>
  89. #include <inttypes.h>
  90. #include <assert.h>
  91. #include "config.h"
  92. #include "libswscale/rgb2rgb.h"
  93. #include "libswscale/swscale.h"
  94. #include "libswscale/swscale_internal.h"
  95. #include "libavutil/cpu.h"
  96. #include "libavutil/pixdesc.h"
  97. #include "yuv2rgb_altivec.h"
  98. #undef PROFILE_THE_BEAST
  99. #undef INC_SCALING
  100. typedef unsigned char ubyte;
  101. typedef signed char sbyte;
  102. /* RGB interleaver, 16 planar pels 8-bit samples per channel in
  103. * homogeneous vector registers x0,x1,x2 are interleaved with the
  104. * following technique:
  105. *
  106. * o0 = vec_mergeh(x0, x1);
  107. * o1 = vec_perm(o0, x2, perm_rgb_0);
  108. * o2 = vec_perm(o0, x2, perm_rgb_1);
  109. * o3 = vec_mergel(x0, x1);
  110. * o4 = vec_perm(o3, o2, perm_rgb_2);
  111. * o5 = vec_perm(o3, o2, perm_rgb_3);
  112. *
  113. * perm_rgb_0: o0(RG).h v1(B) --> o1*
  114. * 0 1 2 3 4
  115. * rgbr|gbrg|brgb|rgbr
  116. * 0010 0100 1001 0010
  117. * 0102 3145 2673 894A
  118. *
  119. * perm_rgb_1: o0(RG).h v1(B) --> o2
  120. * 0 1 2 3 4
  121. * gbrg|brgb|bbbb|bbbb
  122. * 0100 1001 1111 1111
  123. * B5CD 6EF7 89AB CDEF
  124. *
  125. * perm_rgb_2: o3(RG).l o2(rgbB.l) --> o4*
  126. * 0 1 2 3 4
  127. * gbrg|brgb|rgbr|gbrg
  128. * 1111 1111 0010 0100
  129. * 89AB CDEF 0182 3945
  130. *
  131. * perm_rgb_2: o3(RG).l o2(rgbB.l) ---> o5*
  132. * 0 1 2 3 4
  133. * brgb|rgbr|gbrg|brgb
  134. * 1001 0010 0100 1001
  135. * a67b 89cA BdCD eEFf
  136. *
  137. */
  138. static const vector unsigned char
  139. perm_rgb_0 = { 0x00, 0x01, 0x10, 0x02, 0x03, 0x11, 0x04, 0x05,
  140. 0x12, 0x06, 0x07, 0x13, 0x08, 0x09, 0x14, 0x0a },
  141. perm_rgb_1 = { 0x0b, 0x15, 0x0c, 0x0d, 0x16, 0x0e, 0x0f, 0x17,
  142. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f },
  143. perm_rgb_2 = { 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  144. 0x00, 0x01, 0x18, 0x02, 0x03, 0x19, 0x04, 0x05 },
  145. perm_rgb_3 = { 0x1a, 0x06, 0x07, 0x1b, 0x08, 0x09, 0x1c, 0x0a,
  146. 0x0b, 0x1d, 0x0c, 0x0d, 0x1e, 0x0e, 0x0f, 0x1f };
  147. #define vec_merge3(x2, x1, x0, y0, y1, y2) \
  148. do { \
  149. __typeof__(x0) o0, o2, o3; \
  150. o0 = vec_mergeh(x0, x1); \
  151. y0 = vec_perm(o0, x2, perm_rgb_0); \
  152. o2 = vec_perm(o0, x2, perm_rgb_1); \
  153. o3 = vec_mergel(x0, x1); \
  154. y1 = vec_perm(o3, o2, perm_rgb_2); \
  155. y2 = vec_perm(o3, o2, perm_rgb_3); \
  156. } while (0)
  157. #define vec_mstbgr24(x0, x1, x2, ptr) \
  158. do { \
  159. __typeof__(x0) _0, _1, _2; \
  160. vec_merge3(x0, x1, x2, _0, _1, _2); \
  161. vec_st(_0, 0, ptr++); \
  162. vec_st(_1, 0, ptr++); \
  163. vec_st(_2, 0, ptr++); \
  164. } while (0)
  165. #define vec_mstrgb24(x0, x1, x2, ptr) \
  166. do { \
  167. __typeof__(x0) _0, _1, _2; \
  168. vec_merge3(x2, x1, x0, _0, _1, _2); \
  169. vec_st(_0, 0, ptr++); \
  170. vec_st(_1, 0, ptr++); \
  171. vec_st(_2, 0, ptr++); \
  172. } while (0)
  173. /* pack the pixels in rgb0 format
  174. * msb R
  175. * lsb 0
  176. */
  177. #define vec_mstrgb32(T, x0, x1, x2, x3, ptr) \
  178. do { \
  179. T _0, _1, _2, _3; \
  180. _0 = vec_mergeh(x0, x1); \
  181. _1 = vec_mergeh(x2, x3); \
  182. _2 = (T) vec_mergeh((vector unsigned short) _0, \
  183. (vector unsigned short) _1); \
  184. _3 = (T) vec_mergel((vector unsigned short) _0, \
  185. (vector unsigned short) _1); \
  186. vec_st(_2, 0 * 16, (T *) ptr); \
  187. vec_st(_3, 1 * 16, (T *) ptr); \
  188. _0 = vec_mergel(x0, x1); \
  189. _1 = vec_mergel(x2, x3); \
  190. _2 = (T) vec_mergeh((vector unsigned short) _0, \
  191. (vector unsigned short) _1); \
  192. _3 = (T) vec_mergel((vector unsigned short) _0, \
  193. (vector unsigned short) _1); \
  194. vec_st(_2, 2 * 16, (T *) ptr); \
  195. vec_st(_3, 3 * 16, (T *) ptr); \
  196. ptr += 4; \
  197. } while (0)
  198. /*
  199. * 1 0 1.4021 | | Y |
  200. * 1 -0.3441 -0.7142 |x| Cb|
  201. * 1 1.7718 0 | | Cr|
  202. *
  203. *
  204. * Y: [-128 127]
  205. * Cb/Cr : [-128 127]
  206. *
  207. * typical YUV conversion works on Y: 0-255 this version has been
  208. * optimized for JPEG decoding.
  209. */
  210. #define vec_unh(x) \
  211. (vector signed short) \
  212. vec_perm(x, (__typeof__(x)) { 0 }, \
  213. ((vector unsigned char) { \
  214. 0x10, 0x00, 0x10, 0x01, 0x10, 0x02, 0x10, 0x03, \
  215. 0x10, 0x04, 0x10, 0x05, 0x10, 0x06, 0x10, 0x07 }))
  216. #define vec_unl(x) \
  217. (vector signed short) \
  218. vec_perm(x, (__typeof__(x)) { 0 }, \
  219. ((vector unsigned char) { \
  220. 0x10, 0x08, 0x10, 0x09, 0x10, 0x0A, 0x10, 0x0B, \
  221. 0x10, 0x0C, 0x10, 0x0D, 0x10, 0x0E, 0x10, 0x0F }))
  222. #define vec_clip_s16(x) \
  223. vec_max(vec_min(x, ((vector signed short) { \
  224. 235, 235, 235, 235, 235, 235, 235, 235 })), \
  225. ((vector signed short) { 16, 16, 16, 16, 16, 16, 16, 16 }))
  226. #define vec_packclp(x, y) \
  227. (vector unsigned char) \
  228. vec_packs((vector unsigned short) \
  229. vec_max(x, ((vector signed short) { 0 })), \
  230. (vector unsigned short) \
  231. vec_max(y, ((vector signed short) { 0 })))
  232. //#define out_pixels(a, b, c, ptr) vec_mstrgb32(__typeof__(a), ((__typeof__(a)) { 255 }), a, a, a, ptr)
  233. static inline void cvtyuvtoRGB(SwsContext *c, vector signed short Y,
  234. vector signed short U, vector signed short V,
  235. vector signed short *R, vector signed short *G,
  236. vector signed short *B)
  237. {
  238. vector signed short vx, ux, uvx;
  239. Y = vec_mradds(Y, c->CY, c->OY);
  240. U = vec_sub(U, (vector signed short)
  241. vec_splat((vector signed short) { 128 }, 0));
  242. V = vec_sub(V, (vector signed short)
  243. vec_splat((vector signed short) { 128 }, 0));
  244. // ux = (CBU * (u << c->CSHIFT) + 0x4000) >> 15;
  245. ux = vec_sl(U, c->CSHIFT);
  246. *B = vec_mradds(ux, c->CBU, Y);
  247. // vx = (CRV * (v << c->CSHIFT) + 0x4000) >> 15;
  248. vx = vec_sl(V, c->CSHIFT);
  249. *R = vec_mradds(vx, c->CRV, Y);
  250. // uvx = ((CGU * u) + (CGV * v)) >> 15;
  251. uvx = vec_mradds(U, c->CGU, Y);
  252. *G = vec_mradds(V, c->CGV, uvx);
  253. }
  254. /*
  255. * ------------------------------------------------------------------------------
  256. * CS converters
  257. * ------------------------------------------------------------------------------
  258. */
  259. #define DEFCSP420_CVT(name, out_pixels) \
  260. static int altivec_ ## name(SwsContext *c, const unsigned char **in, \
  261. int *instrides, int srcSliceY, int srcSliceH, \
  262. unsigned char **oplanes, int *outstrides) \
  263. { \
  264. int w = c->srcW; \
  265. int h = srcSliceH; \
  266. int i, j; \
  267. int instrides_scl[3]; \
  268. vector unsigned char y0, y1; \
  269. \
  270. vector signed char u, v; \
  271. \
  272. vector signed short Y0, Y1, Y2, Y3; \
  273. vector signed short U, V; \
  274. vector signed short vx, ux, uvx; \
  275. vector signed short vx0, ux0, uvx0; \
  276. vector signed short vx1, ux1, uvx1; \
  277. vector signed short R0, G0, B0; \
  278. vector signed short R1, G1, B1; \
  279. vector unsigned char R, G, B; \
  280. \
  281. const vector unsigned char *y1ivP, *y2ivP, *uivP, *vivP; \
  282. vector unsigned char align_perm; \
  283. \
  284. vector signed short lCY = c->CY; \
  285. vector signed short lOY = c->OY; \
  286. vector signed short lCRV = c->CRV; \
  287. vector signed short lCBU = c->CBU; \
  288. vector signed short lCGU = c->CGU; \
  289. vector signed short lCGV = c->CGV; \
  290. vector unsigned short lCSHIFT = c->CSHIFT; \
  291. \
  292. const ubyte *y1i = in[0]; \
  293. const ubyte *y2i = in[0] + instrides[0]; \
  294. const ubyte *ui = in[1]; \
  295. const ubyte *vi = in[2]; \
  296. \
  297. vector unsigned char *oute = \
  298. (vector unsigned char *) \
  299. (oplanes[0] + srcSliceY * outstrides[0]); \
  300. vector unsigned char *outo = \
  301. (vector unsigned char *) \
  302. (oplanes[0] + srcSliceY * outstrides[0] + outstrides[0]); \
  303. \
  304. /* loop moves y{1, 2}i by w */ \
  305. instrides_scl[0] = instrides[0] * 2 - w; \
  306. /* loop moves ui by w / 2 */ \
  307. instrides_scl[1] = instrides[1] - w / 2; \
  308. /* loop moves vi by w / 2 */ \
  309. instrides_scl[2] = instrides[2] - w / 2; \
  310. \
  311. for (i = 0; i < h / 2; i++) { \
  312. vec_dstst(outo, (0x02000002 | (((w * 3 + 32) / 32) << 16)), 0); \
  313. vec_dstst(oute, (0x02000002 | (((w * 3 + 32) / 32) << 16)), 1); \
  314. \
  315. for (j = 0; j < w / 16; j++) { \
  316. y1ivP = (const vector unsigned char *) y1i; \
  317. y2ivP = (const vector unsigned char *) y2i; \
  318. uivP = (const vector unsigned char *) ui; \
  319. vivP = (const vector unsigned char *) vi; \
  320. \
  321. align_perm = vec_lvsl(0, y1i); \
  322. y0 = (vector unsigned char) \
  323. vec_perm(y1ivP[0], y1ivP[1], align_perm); \
  324. \
  325. align_perm = vec_lvsl(0, y2i); \
  326. y1 = (vector unsigned char) \
  327. vec_perm(y2ivP[0], y2ivP[1], align_perm); \
  328. \
  329. align_perm = vec_lvsl(0, ui); \
  330. u = (vector signed char) \
  331. vec_perm(uivP[0], uivP[1], align_perm); \
  332. \
  333. align_perm = vec_lvsl(0, vi); \
  334. v = (vector signed char) \
  335. vec_perm(vivP[0], vivP[1], align_perm); \
  336. \
  337. u = (vector signed char) \
  338. vec_sub(u, \
  339. (vector signed char) \
  340. vec_splat((vector signed char) { 128 }, 0)); \
  341. v = (vector signed char) \
  342. vec_sub(v, \
  343. (vector signed char) \
  344. vec_splat((vector signed char) { 128 }, 0)); \
  345. \
  346. U = vec_unpackh(u); \
  347. V = vec_unpackh(v); \
  348. \
  349. Y0 = vec_unh(y0); \
  350. Y1 = vec_unl(y0); \
  351. Y2 = vec_unh(y1); \
  352. Y3 = vec_unl(y1); \
  353. \
  354. Y0 = vec_mradds(Y0, lCY, lOY); \
  355. Y1 = vec_mradds(Y1, lCY, lOY); \
  356. Y2 = vec_mradds(Y2, lCY, lOY); \
  357. Y3 = vec_mradds(Y3, lCY, lOY); \
  358. \
  359. /* ux = (CBU * (u << CSHIFT) + 0x4000) >> 15 */ \
  360. ux = vec_sl(U, lCSHIFT); \
  361. ux = vec_mradds(ux, lCBU, (vector signed short) { 0 }); \
  362. ux0 = vec_mergeh(ux, ux); \
  363. ux1 = vec_mergel(ux, ux); \
  364. \
  365. /* vx = (CRV * (v << CSHIFT) + 0x4000) >> 15; */ \
  366. vx = vec_sl(V, lCSHIFT); \
  367. vx = vec_mradds(vx, lCRV, (vector signed short) { 0 }); \
  368. vx0 = vec_mergeh(vx, vx); \
  369. vx1 = vec_mergel(vx, vx); \
  370. \
  371. /* uvx = ((CGU * u) + (CGV * v)) >> 15 */ \
  372. uvx = vec_mradds(U, lCGU, (vector signed short) { 0 }); \
  373. uvx = vec_mradds(V, lCGV, uvx); \
  374. uvx0 = vec_mergeh(uvx, uvx); \
  375. uvx1 = vec_mergel(uvx, uvx); \
  376. \
  377. R0 = vec_add(Y0, vx0); \
  378. G0 = vec_add(Y0, uvx0); \
  379. B0 = vec_add(Y0, ux0); \
  380. R1 = vec_add(Y1, vx1); \
  381. G1 = vec_add(Y1, uvx1); \
  382. B1 = vec_add(Y1, ux1); \
  383. \
  384. R = vec_packclp(R0, R1); \
  385. G = vec_packclp(G0, G1); \
  386. B = vec_packclp(B0, B1); \
  387. \
  388. out_pixels(R, G, B, oute); \
  389. \
  390. R0 = vec_add(Y2, vx0); \
  391. G0 = vec_add(Y2, uvx0); \
  392. B0 = vec_add(Y2, ux0); \
  393. R1 = vec_add(Y3, vx1); \
  394. G1 = vec_add(Y3, uvx1); \
  395. B1 = vec_add(Y3, ux1); \
  396. R = vec_packclp(R0, R1); \
  397. G = vec_packclp(G0, G1); \
  398. B = vec_packclp(B0, B1); \
  399. \
  400. \
  401. out_pixels(R, G, B, outo); \
  402. \
  403. y1i += 16; \
  404. y2i += 16; \
  405. ui += 8; \
  406. vi += 8; \
  407. } \
  408. \
  409. outo += (outstrides[0]) >> 4; \
  410. oute += (outstrides[0]) >> 4; \
  411. \
  412. ui += instrides_scl[1]; \
  413. vi += instrides_scl[2]; \
  414. y1i += instrides_scl[0]; \
  415. y2i += instrides_scl[0]; \
  416. } \
  417. return srcSliceH; \
  418. }
  419. #define out_abgr(a, b, c, ptr) \
  420. vec_mstrgb32(__typeof__(a), ((__typeof__(a)) { 255 }), c, b, a, ptr)
  421. #define out_bgra(a, b, c, ptr) \
  422. vec_mstrgb32(__typeof__(a), c, b, a, ((__typeof__(a)) { 255 }), ptr)
  423. #define out_rgba(a, b, c, ptr) \
  424. vec_mstrgb32(__typeof__(a), a, b, c, ((__typeof__(a)) { 255 }), ptr)
  425. #define out_argb(a, b, c, ptr) \
  426. vec_mstrgb32(__typeof__(a), ((__typeof__(a)) { 255 }), a, b, c, ptr)
  427. #define out_rgb24(a, b, c, ptr) vec_mstrgb24(a, b, c, ptr)
  428. #define out_bgr24(a, b, c, ptr) vec_mstbgr24(a, b, c, ptr)
  429. DEFCSP420_CVT(yuv2_abgr, out_abgr)
  430. DEFCSP420_CVT(yuv2_bgra, out_bgra)
  431. DEFCSP420_CVT(yuv2_rgba, out_rgba)
  432. DEFCSP420_CVT(yuv2_argb, out_argb)
  433. DEFCSP420_CVT(yuv2_rgb24, out_rgb24)
  434. DEFCSP420_CVT(yuv2_bgr24, out_bgr24)
  435. // uyvy|uyvy|uyvy|uyvy
  436. // 0123 4567 89ab cdef
  437. static const vector unsigned char
  438. demux_u = { 0x10, 0x00, 0x10, 0x00,
  439. 0x10, 0x04, 0x10, 0x04,
  440. 0x10, 0x08, 0x10, 0x08,
  441. 0x10, 0x0c, 0x10, 0x0c },
  442. demux_v = { 0x10, 0x02, 0x10, 0x02,
  443. 0x10, 0x06, 0x10, 0x06,
  444. 0x10, 0x0A, 0x10, 0x0A,
  445. 0x10, 0x0E, 0x10, 0x0E },
  446. demux_y = { 0x10, 0x01, 0x10, 0x03,
  447. 0x10, 0x05, 0x10, 0x07,
  448. 0x10, 0x09, 0x10, 0x0B,
  449. 0x10, 0x0D, 0x10, 0x0F };
  450. /*
  451. * this is so I can play live CCIR raw video
  452. */
  453. static int altivec_uyvy_rgb32(SwsContext *c, const unsigned char **in,
  454. int *instrides, int srcSliceY, int srcSliceH,
  455. unsigned char **oplanes, int *outstrides)
  456. {
  457. int w = c->srcW;
  458. int h = srcSliceH;
  459. int i, j;
  460. vector unsigned char uyvy;
  461. vector signed short Y, U, V;
  462. vector signed short R0, G0, B0, R1, G1, B1;
  463. vector unsigned char R, G, B;
  464. vector unsigned char *out;
  465. const ubyte *img;
  466. img = in[0];
  467. out = (vector unsigned char *) (oplanes[0] + srcSliceY * outstrides[0]);
  468. for (i = 0; i < h; i++)
  469. for (j = 0; j < w / 16; j++) {
  470. uyvy = vec_ld(0, img);
  471. U = (vector signed short)
  472. vec_perm(uyvy, (vector unsigned char) { 0 }, demux_u);
  473. V = (vector signed short)
  474. vec_perm(uyvy, (vector unsigned char) { 0 }, demux_v);
  475. Y = (vector signed short)
  476. vec_perm(uyvy, (vector unsigned char) { 0 }, demux_y);
  477. cvtyuvtoRGB(c, Y, U, V, &R0, &G0, &B0);
  478. uyvy = vec_ld(16, img);
  479. U = (vector signed short)
  480. vec_perm(uyvy, (vector unsigned char) { 0 }, demux_u);
  481. V = (vector signed short)
  482. vec_perm(uyvy, (vector unsigned char) { 0 }, demux_v);
  483. Y = (vector signed short)
  484. vec_perm(uyvy, (vector unsigned char) { 0 }, demux_y);
  485. cvtyuvtoRGB(c, Y, U, V, &R1, &G1, &B1);
  486. R = vec_packclp(R0, R1);
  487. G = vec_packclp(G0, G1);
  488. B = vec_packclp(B0, B1);
  489. // vec_mstbgr24 (R,G,B, out);
  490. out_rgba(R, G, B, out);
  491. img += 32;
  492. }
  493. return srcSliceH;
  494. }
  495. /* Ok currently the acceleration routine only supports
  496. * inputs of widths a multiple of 16
  497. * and heights a multiple 2
  498. *
  499. * So we just fall back to the C codes for this.
  500. */
  501. SwsFunc ff_yuv2rgb_init_altivec(SwsContext *c)
  502. {
  503. if (!(av_get_cpu_flags() & AV_CPU_FLAG_ALTIVEC))
  504. return NULL;
  505. /*
  506. * and this seems not to matter too much I tried a bunch of
  507. * videos with abnormal widths and MPlayer crashes elsewhere.
  508. * mplayer -vo x11 -rawvideo on:w=350:h=240 raw-350x240.eyuv
  509. * boom with X11 bad match.
  510. *
  511. */
  512. if ((c->srcW & 0xf) != 0)
  513. return NULL;
  514. switch (c->srcFormat) {
  515. case PIX_FMT_YUV410P:
  516. case PIX_FMT_YUV420P:
  517. /*case IMGFMT_CLPL: ??? */
  518. case PIX_FMT_GRAY8:
  519. case PIX_FMT_NV12:
  520. case PIX_FMT_NV21:
  521. if ((c->srcH & 0x1) != 0)
  522. return NULL;
  523. switch (c->dstFormat) {
  524. case PIX_FMT_RGB24:
  525. av_log(c, AV_LOG_WARNING, "ALTIVEC: Color Space RGB24\n");
  526. return altivec_yuv2_rgb24;
  527. case PIX_FMT_BGR24:
  528. av_log(c, AV_LOG_WARNING, "ALTIVEC: Color Space BGR24\n");
  529. return altivec_yuv2_bgr24;
  530. case PIX_FMT_ARGB:
  531. av_log(c, AV_LOG_WARNING, "ALTIVEC: Color Space ARGB\n");
  532. return altivec_yuv2_argb;
  533. case PIX_FMT_ABGR:
  534. av_log(c, AV_LOG_WARNING, "ALTIVEC: Color Space ABGR\n");
  535. return altivec_yuv2_abgr;
  536. case PIX_FMT_RGBA:
  537. av_log(c, AV_LOG_WARNING, "ALTIVEC: Color Space RGBA\n");
  538. return altivec_yuv2_rgba;
  539. case PIX_FMT_BGRA:
  540. av_log(c, AV_LOG_WARNING, "ALTIVEC: Color Space BGRA\n");
  541. return altivec_yuv2_bgra;
  542. default: return NULL;
  543. }
  544. break;
  545. case PIX_FMT_UYVY422:
  546. switch (c->dstFormat) {
  547. case PIX_FMT_BGR32:
  548. av_log(c, AV_LOG_WARNING, "ALTIVEC: Color Space UYVY -> RGB32\n");
  549. return altivec_uyvy_rgb32;
  550. default: return NULL;
  551. }
  552. break;
  553. }
  554. return NULL;
  555. }
  556. void ff_yuv2rgb_init_tables_altivec(SwsContext *c, const int inv_table[4],
  557. int brightness, int contrast,
  558. int saturation)
  559. {
  560. union {
  561. DECLARE_ALIGNED(16, signed short, tmp)[8];
  562. vector signed short vec;
  563. } buf;
  564. buf.tmp[0] = ((0xffffLL) * contrast >> 8) >> 9; // cy
  565. buf.tmp[1] = -256 * brightness; // oy
  566. buf.tmp[2] = (inv_table[0] >> 3) * (contrast >> 16) * (saturation >> 16); // crv
  567. buf.tmp[3] = (inv_table[1] >> 3) * (contrast >> 16) * (saturation >> 16); // cbu
  568. buf.tmp[4] = -((inv_table[2] >> 1) * (contrast >> 16) * (saturation >> 16)); // cgu
  569. buf.tmp[5] = -((inv_table[3] >> 1) * (contrast >> 16) * (saturation >> 16)); // cgv
  570. c->CSHIFT = (vector unsigned short) vec_splat_u16(2);
  571. c->CY = vec_splat((vector signed short) buf.vec, 0);
  572. c->OY = vec_splat((vector signed short) buf.vec, 1);
  573. c->CRV = vec_splat((vector signed short) buf.vec, 2);
  574. c->CBU = vec_splat((vector signed short) buf.vec, 3);
  575. c->CGU = vec_splat((vector signed short) buf.vec, 4);
  576. c->CGV = vec_splat((vector signed short) buf.vec, 5);
  577. return;
  578. }
  579. static av_always_inline void ff_yuv2packedX_altivec(SwsContext *c,
  580. const int16_t *lumFilter,
  581. const int16_t **lumSrc,
  582. int lumFilterSize,
  583. const int16_t *chrFilter,
  584. const int16_t **chrUSrc,
  585. const int16_t **chrVSrc,
  586. int chrFilterSize,
  587. const int16_t **alpSrc,
  588. uint8_t *dest,
  589. int dstW, int dstY,
  590. enum PixelFormat target)
  591. {
  592. int i, j;
  593. vector signed short X, X0, X1, Y0, U0, V0, Y1, U1, V1, U, V;
  594. vector signed short R0, G0, B0, R1, G1, B1;
  595. vector unsigned char R, G, B;
  596. vector unsigned char *out, *nout;
  597. vector signed short RND = vec_splat_s16(1 << 3);
  598. vector unsigned short SCL = vec_splat_u16(4);
  599. DECLARE_ALIGNED(16, unsigned int, scratch)[16];
  600. vector signed short *YCoeffs, *CCoeffs;
  601. YCoeffs = c->vYCoeffsBank + dstY * lumFilterSize;
  602. CCoeffs = c->vCCoeffsBank + dstY * chrFilterSize;
  603. out = (vector unsigned char *) dest;
  604. for (i = 0; i < dstW; i += 16) {
  605. Y0 = RND;
  606. Y1 = RND;
  607. /* extract 16 coeffs from lumSrc */
  608. for (j = 0; j < lumFilterSize; j++) {
  609. X0 = vec_ld(0, &lumSrc[j][i]);
  610. X1 = vec_ld(16, &lumSrc[j][i]);
  611. Y0 = vec_mradds(X0, YCoeffs[j], Y0);
  612. Y1 = vec_mradds(X1, YCoeffs[j], Y1);
  613. }
  614. U = RND;
  615. V = RND;
  616. /* extract 8 coeffs from U,V */
  617. for (j = 0; j < chrFilterSize; j++) {
  618. X = vec_ld(0, &chrUSrc[j][i / 2]);
  619. U = vec_mradds(X, CCoeffs[j], U);
  620. X = vec_ld(0, &chrVSrc[j][i / 2]);
  621. V = vec_mradds(X, CCoeffs[j], V);
  622. }
  623. /* scale and clip signals */
  624. Y0 = vec_sra(Y0, SCL);
  625. Y1 = vec_sra(Y1, SCL);
  626. U = vec_sra(U, SCL);
  627. V = vec_sra(V, SCL);
  628. Y0 = vec_clip_s16(Y0);
  629. Y1 = vec_clip_s16(Y1);
  630. U = vec_clip_s16(U);
  631. V = vec_clip_s16(V);
  632. /* now we have
  633. * Y0 = y0 y1 y2 y3 y4 y5 y6 y7 Y1 = y8 y9 y10 y11 y12 y13 y14 y15
  634. * U = u0 u1 u2 u3 u4 u5 u6 u7 V = v0 v1 v2 v3 v4 v5 v6 v7
  635. *
  636. * Y0 = y0 y1 y2 y3 y4 y5 y6 y7 Y1 = y8 y9 y10 y11 y12 y13 y14 y15
  637. * U0 = u0 u0 u1 u1 u2 u2 u3 u3 U1 = u4 u4 u5 u5 u6 u6 u7 u7
  638. * V0 = v0 v0 v1 v1 v2 v2 v3 v3 V1 = v4 v4 v5 v5 v6 v6 v7 v7
  639. */
  640. U0 = vec_mergeh(U, U);
  641. V0 = vec_mergeh(V, V);
  642. U1 = vec_mergel(U, U);
  643. V1 = vec_mergel(V, V);
  644. cvtyuvtoRGB(c, Y0, U0, V0, &R0, &G0, &B0);
  645. cvtyuvtoRGB(c, Y1, U1, V1, &R1, &G1, &B1);
  646. R = vec_packclp(R0, R1);
  647. G = vec_packclp(G0, G1);
  648. B = vec_packclp(B0, B1);
  649. switch (target) {
  650. case PIX_FMT_ABGR:
  651. out_abgr(R, G, B, out);
  652. break;
  653. case PIX_FMT_BGRA:
  654. out_bgra(R, G, B, out);
  655. break;
  656. case PIX_FMT_RGBA:
  657. out_rgba(R, G, B, out);
  658. break;
  659. case PIX_FMT_ARGB:
  660. out_argb(R, G, B, out);
  661. break;
  662. case PIX_FMT_RGB24:
  663. out_rgb24(R, G, B, out);
  664. break;
  665. case PIX_FMT_BGR24:
  666. out_bgr24(R, G, B, out);
  667. break;
  668. default:
  669. {
  670. /* If this is reached, the caller should have called yuv2packedXinC
  671. * instead. */
  672. static int printed_error_message;
  673. if (!printed_error_message) {
  674. av_log(c, AV_LOG_ERROR,
  675. "altivec_yuv2packedX doesn't support %s output\n",
  676. av_get_pix_fmt_name(c->dstFormat));
  677. printed_error_message = 1;
  678. }
  679. return;
  680. }
  681. }
  682. }
  683. if (i < dstW) {
  684. i -= 16;
  685. Y0 = RND;
  686. Y1 = RND;
  687. /* extract 16 coeffs from lumSrc */
  688. for (j = 0; j < lumFilterSize; j++) {
  689. X0 = vec_ld(0, &lumSrc[j][i]);
  690. X1 = vec_ld(16, &lumSrc[j][i]);
  691. Y0 = vec_mradds(X0, YCoeffs[j], Y0);
  692. Y1 = vec_mradds(X1, YCoeffs[j], Y1);
  693. }
  694. U = RND;
  695. V = RND;
  696. /* extract 8 coeffs from U,V */
  697. for (j = 0; j < chrFilterSize; j++) {
  698. X = vec_ld(0, &chrUSrc[j][i / 2]);
  699. U = vec_mradds(X, CCoeffs[j], U);
  700. X = vec_ld(0, &chrVSrc[j][i / 2]);
  701. V = vec_mradds(X, CCoeffs[j], V);
  702. }
  703. /* scale and clip signals */
  704. Y0 = vec_sra(Y0, SCL);
  705. Y1 = vec_sra(Y1, SCL);
  706. U = vec_sra(U, SCL);
  707. V = vec_sra(V, SCL);
  708. Y0 = vec_clip_s16(Y0);
  709. Y1 = vec_clip_s16(Y1);
  710. U = vec_clip_s16(U);
  711. V = vec_clip_s16(V);
  712. /* now we have
  713. * Y0 = y0 y1 y2 y3 y4 y5 y6 y7 Y1 = y8 y9 y10 y11 y12 y13 y14 y15
  714. * U = u0 u1 u2 u3 u4 u5 u6 u7 V = v0 v1 v2 v3 v4 v5 v6 v7
  715. *
  716. * Y0 = y0 y1 y2 y3 y4 y5 y6 y7 Y1 = y8 y9 y10 y11 y12 y13 y14 y15
  717. * U0 = u0 u0 u1 u1 u2 u2 u3 u3 U1 = u4 u4 u5 u5 u6 u6 u7 u7
  718. * V0 = v0 v0 v1 v1 v2 v2 v3 v3 V1 = v4 v4 v5 v5 v6 v6 v7 v7
  719. */
  720. U0 = vec_mergeh(U, U);
  721. V0 = vec_mergeh(V, V);
  722. U1 = vec_mergel(U, U);
  723. V1 = vec_mergel(V, V);
  724. cvtyuvtoRGB(c, Y0, U0, V0, &R0, &G0, &B0);
  725. cvtyuvtoRGB(c, Y1, U1, V1, &R1, &G1, &B1);
  726. R = vec_packclp(R0, R1);
  727. G = vec_packclp(G0, G1);
  728. B = vec_packclp(B0, B1);
  729. nout = (vector unsigned char *) scratch;
  730. switch (target) {
  731. case PIX_FMT_ABGR:
  732. out_abgr(R, G, B, nout);
  733. break;
  734. case PIX_FMT_BGRA:
  735. out_bgra(R, G, B, nout);
  736. break;
  737. case PIX_FMT_RGBA:
  738. out_rgba(R, G, B, nout);
  739. break;
  740. case PIX_FMT_ARGB:
  741. out_argb(R, G, B, nout);
  742. break;
  743. case PIX_FMT_RGB24:
  744. out_rgb24(R, G, B, nout);
  745. break;
  746. case PIX_FMT_BGR24:
  747. out_bgr24(R, G, B, nout);
  748. break;
  749. default:
  750. /* Unreachable, I think. */
  751. av_log(c, AV_LOG_ERROR,
  752. "altivec_yuv2packedX doesn't support %s output\n",
  753. av_get_pix_fmt_name(c->dstFormat));
  754. return;
  755. }
  756. memcpy(&((uint32_t *) dest)[i], scratch, (dstW - i) / 4);
  757. }
  758. }
  759. #define YUV2PACKEDX_WRAPPER(suffix, pixfmt) \
  760. void ff_yuv2 ## suffix ## _X_altivec(SwsContext *c, \
  761. const int16_t *lumFilter, \
  762. const int16_t **lumSrc, \
  763. int lumFilterSize, \
  764. const int16_t *chrFilter, \
  765. const int16_t **chrUSrc, \
  766. const int16_t **chrVSrc, \
  767. int chrFilterSize, \
  768. const int16_t **alpSrc, \
  769. uint8_t *dest, int dstW, int dstY) \
  770. { \
  771. ff_yuv2packedX_altivec(c, lumFilter, lumSrc, lumFilterSize, \
  772. chrFilter, chrUSrc, chrVSrc, \
  773. chrFilterSize, alpSrc, \
  774. dest, dstW, dstY, pixfmt); \
  775. }
  776. YUV2PACKEDX_WRAPPER(abgr, PIX_FMT_ABGR);
  777. YUV2PACKEDX_WRAPPER(bgra, PIX_FMT_BGRA);
  778. YUV2PACKEDX_WRAPPER(argb, PIX_FMT_ARGB);
  779. YUV2PACKEDX_WRAPPER(rgba, PIX_FMT_RGBA);
  780. YUV2PACKEDX_WRAPPER(rgb24, PIX_FMT_RGB24);
  781. YUV2PACKEDX_WRAPPER(bgr24, PIX_FMT_BGR24);