yuv2rgb_altivec.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  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/attributes.h"
  96. #include "libavutil/cpu.h"
  97. #include "libavutil/pixdesc.h"
  98. #include "yuv2rgb_altivec.h"
  99. #undef PROFILE_THE_BEAST
  100. #undef INC_SCALING
  101. typedef unsigned char ubyte;
  102. typedef signed char sbyte;
  103. /* RGB interleaver, 16 planar pels 8-bit samples per channel in
  104. * homogeneous vector registers x0,x1,x2 are interleaved with the
  105. * following technique:
  106. *
  107. * o0 = vec_mergeh(x0, x1);
  108. * o1 = vec_perm(o0, x2, perm_rgb_0);
  109. * o2 = vec_perm(o0, x2, perm_rgb_1);
  110. * o3 = vec_mergel(x0, x1);
  111. * o4 = vec_perm(o3, o2, perm_rgb_2);
  112. * o5 = vec_perm(o3, o2, perm_rgb_3);
  113. *
  114. * perm_rgb_0: o0(RG).h v1(B) --> o1*
  115. * 0 1 2 3 4
  116. * rgbr|gbrg|brgb|rgbr
  117. * 0010 0100 1001 0010
  118. * 0102 3145 2673 894A
  119. *
  120. * perm_rgb_1: o0(RG).h v1(B) --> o2
  121. * 0 1 2 3 4
  122. * gbrg|brgb|bbbb|bbbb
  123. * 0100 1001 1111 1111
  124. * B5CD 6EF7 89AB CDEF
  125. *
  126. * perm_rgb_2: o3(RG).l o2(rgbB.l) --> o4*
  127. * 0 1 2 3 4
  128. * gbrg|brgb|rgbr|gbrg
  129. * 1111 1111 0010 0100
  130. * 89AB CDEF 0182 3945
  131. *
  132. * perm_rgb_2: o3(RG).l o2(rgbB.l) ---> o5*
  133. * 0 1 2 3 4
  134. * brgb|rgbr|gbrg|brgb
  135. * 1001 0010 0100 1001
  136. * a67b 89cA BdCD eEFf
  137. *
  138. */
  139. static const vector unsigned char
  140. perm_rgb_0 = { 0x00, 0x01, 0x10, 0x02, 0x03, 0x11, 0x04, 0x05,
  141. 0x12, 0x06, 0x07, 0x13, 0x08, 0x09, 0x14, 0x0a },
  142. perm_rgb_1 = { 0x0b, 0x15, 0x0c, 0x0d, 0x16, 0x0e, 0x0f, 0x17,
  143. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f },
  144. perm_rgb_2 = { 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  145. 0x00, 0x01, 0x18, 0x02, 0x03, 0x19, 0x04, 0x05 },
  146. perm_rgb_3 = { 0x1a, 0x06, 0x07, 0x1b, 0x08, 0x09, 0x1c, 0x0a,
  147. 0x0b, 0x1d, 0x0c, 0x0d, 0x1e, 0x0e, 0x0f, 0x1f };
  148. #define vec_merge3(x2, x1, x0, y0, y1, y2) \
  149. do { \
  150. __typeof__(x0) o0, o2, o3; \
  151. o0 = vec_mergeh(x0, x1); \
  152. y0 = vec_perm(o0, x2, perm_rgb_0); \
  153. o2 = vec_perm(o0, x2, perm_rgb_1); \
  154. o3 = vec_mergel(x0, x1); \
  155. y1 = vec_perm(o3, o2, perm_rgb_2); \
  156. y2 = vec_perm(o3, o2, perm_rgb_3); \
  157. } while (0)
  158. #define vec_mstbgr24(x0, x1, x2, ptr) \
  159. do { \
  160. __typeof__(x0) _0, _1, _2; \
  161. vec_merge3(x0, x1, x2, _0, _1, _2); \
  162. vec_st(_0, 0, ptr++); \
  163. vec_st(_1, 0, ptr++); \
  164. vec_st(_2, 0, ptr++); \
  165. } while (0)
  166. #define vec_mstrgb24(x0, x1, x2, ptr) \
  167. do { \
  168. __typeof__(x0) _0, _1, _2; \
  169. vec_merge3(x2, x1, x0, _0, _1, _2); \
  170. vec_st(_0, 0, ptr++); \
  171. vec_st(_1, 0, ptr++); \
  172. vec_st(_2, 0, ptr++); \
  173. } while (0)
  174. /* pack the pixels in rgb0 format
  175. * msb R
  176. * lsb 0
  177. */
  178. #define vec_mstrgb32(T, x0, x1, x2, x3, ptr) \
  179. do { \
  180. T _0, _1, _2, _3; \
  181. _0 = vec_mergeh(x0, x1); \
  182. _1 = vec_mergeh(x2, x3); \
  183. _2 = (T) vec_mergeh((vector unsigned short) _0, \
  184. (vector unsigned short) _1); \
  185. _3 = (T) vec_mergel((vector unsigned short) _0, \
  186. (vector unsigned short) _1); \
  187. vec_st(_2, 0 * 16, (T *) ptr); \
  188. vec_st(_3, 1 * 16, (T *) ptr); \
  189. _0 = vec_mergel(x0, x1); \
  190. _1 = vec_mergel(x2, x3); \
  191. _2 = (T) vec_mergeh((vector unsigned short) _0, \
  192. (vector unsigned short) _1); \
  193. _3 = (T) vec_mergel((vector unsigned short) _0, \
  194. (vector unsigned short) _1); \
  195. vec_st(_2, 2 * 16, (T *) ptr); \
  196. vec_st(_3, 3 * 16, (T *) ptr); \
  197. ptr += 4; \
  198. } while (0)
  199. /*
  200. * 1 0 1.4021 | | Y |
  201. * 1 -0.3441 -0.7142 |x| Cb|
  202. * 1 1.7718 0 | | Cr|
  203. *
  204. *
  205. * Y: [-128 127]
  206. * Cb/Cr : [-128 127]
  207. *
  208. * typical YUV conversion works on Y: 0-255 this version has been
  209. * optimized for JPEG decoding.
  210. */
  211. #define vec_unh(x) \
  212. (vector signed short) \
  213. vec_perm(x, (__typeof__(x)) { 0 }, \
  214. ((vector unsigned char) { \
  215. 0x10, 0x00, 0x10, 0x01, 0x10, 0x02, 0x10, 0x03, \
  216. 0x10, 0x04, 0x10, 0x05, 0x10, 0x06, 0x10, 0x07 }))
  217. #define vec_unl(x) \
  218. (vector signed short) \
  219. vec_perm(x, (__typeof__(x)) { 0 }, \
  220. ((vector unsigned char) { \
  221. 0x10, 0x08, 0x10, 0x09, 0x10, 0x0A, 0x10, 0x0B, \
  222. 0x10, 0x0C, 0x10, 0x0D, 0x10, 0x0E, 0x10, 0x0F }))
  223. #define vec_clip_s16(x) \
  224. vec_max(vec_min(x, ((vector signed short) { \
  225. 235, 235, 235, 235, 235, 235, 235, 235 })), \
  226. ((vector signed short) { 16, 16, 16, 16, 16, 16, 16, 16 }))
  227. #define vec_packclp(x, y) \
  228. (vector unsigned char) \
  229. vec_packs((vector unsigned short) \
  230. vec_max(x, ((vector signed short) { 0 })), \
  231. (vector unsigned short) \
  232. vec_max(y, ((vector signed short) { 0 })))
  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, *outo; \
  298. \
  299. /* loop moves y{1, 2}i by w */ \
  300. instrides_scl[0] = instrides[0] * 2 - w; \
  301. /* loop moves ui by w / 2 */ \
  302. instrides_scl[1] = instrides[1] - w / 2; \
  303. /* loop moves vi by w / 2 */ \
  304. instrides_scl[2] = instrides[2] - w / 2; \
  305. \
  306. for (i = 0; i < h / 2; i++) { \
  307. oute = (vector unsigned char *)(oplanes[0] + outstrides[0] * \
  308. (srcSliceY + i * 2)); \
  309. outo = oute + (outstrides[0] >> 4); \
  310. vec_dstst(outo, (0x02000002 | (((w * 3 + 32) / 32) << 16)), 0); \
  311. vec_dstst(oute, (0x02000002 | (((w * 3 + 32) / 32) << 16)), 1); \
  312. \
  313. for (j = 0; j < w / 16; j++) { \
  314. y1ivP = (const vector unsigned char *) y1i; \
  315. y2ivP = (const vector unsigned char *) y2i; \
  316. uivP = (const vector unsigned char *) ui; \
  317. vivP = (const vector unsigned char *) vi; \
  318. \
  319. align_perm = vec_lvsl(0, y1i); \
  320. y0 = (vector unsigned char) \
  321. vec_perm(y1ivP[0], y1ivP[1], align_perm); \
  322. \
  323. align_perm = vec_lvsl(0, y2i); \
  324. y1 = (vector unsigned char) \
  325. vec_perm(y2ivP[0], y2ivP[1], align_perm); \
  326. \
  327. align_perm = vec_lvsl(0, ui); \
  328. u = (vector signed char) \
  329. vec_perm(uivP[0], uivP[1], align_perm); \
  330. \
  331. align_perm = vec_lvsl(0, vi); \
  332. v = (vector signed char) \
  333. vec_perm(vivP[0], vivP[1], align_perm); \
  334. \
  335. u = (vector signed char) \
  336. vec_sub(u, \
  337. (vector signed char) \
  338. vec_splat((vector signed char) { 128 }, 0)); \
  339. v = (vector signed char) \
  340. vec_sub(v, \
  341. (vector signed char) \
  342. vec_splat((vector signed char) { 128 }, 0)); \
  343. \
  344. U = vec_unpackh(u); \
  345. V = vec_unpackh(v); \
  346. \
  347. Y0 = vec_unh(y0); \
  348. Y1 = vec_unl(y0); \
  349. Y2 = vec_unh(y1); \
  350. Y3 = vec_unl(y1); \
  351. \
  352. Y0 = vec_mradds(Y0, lCY, lOY); \
  353. Y1 = vec_mradds(Y1, lCY, lOY); \
  354. Y2 = vec_mradds(Y2, lCY, lOY); \
  355. Y3 = vec_mradds(Y3, lCY, lOY); \
  356. \
  357. /* ux = (CBU * (u << CSHIFT) + 0x4000) >> 15 */ \
  358. ux = vec_sl(U, lCSHIFT); \
  359. ux = vec_mradds(ux, lCBU, (vector signed short) { 0 }); \
  360. ux0 = vec_mergeh(ux, ux); \
  361. ux1 = vec_mergel(ux, ux); \
  362. \
  363. /* vx = (CRV * (v << CSHIFT) + 0x4000) >> 15; */ \
  364. vx = vec_sl(V, lCSHIFT); \
  365. vx = vec_mradds(vx, lCRV, (vector signed short) { 0 }); \
  366. vx0 = vec_mergeh(vx, vx); \
  367. vx1 = vec_mergel(vx, vx); \
  368. \
  369. /* uvx = ((CGU * u) + (CGV * v)) >> 15 */ \
  370. uvx = vec_mradds(U, lCGU, (vector signed short) { 0 }); \
  371. uvx = vec_mradds(V, lCGV, uvx); \
  372. uvx0 = vec_mergeh(uvx, uvx); \
  373. uvx1 = vec_mergel(uvx, uvx); \
  374. \
  375. R0 = vec_add(Y0, vx0); \
  376. G0 = vec_add(Y0, uvx0); \
  377. B0 = vec_add(Y0, ux0); \
  378. R1 = vec_add(Y1, vx1); \
  379. G1 = vec_add(Y1, uvx1); \
  380. B1 = vec_add(Y1, ux1); \
  381. \
  382. R = vec_packclp(R0, R1); \
  383. G = vec_packclp(G0, G1); \
  384. B = vec_packclp(B0, B1); \
  385. \
  386. out_pixels(R, G, B, oute); \
  387. \
  388. R0 = vec_add(Y2, vx0); \
  389. G0 = vec_add(Y2, uvx0); \
  390. B0 = vec_add(Y2, ux0); \
  391. R1 = vec_add(Y3, vx1); \
  392. G1 = vec_add(Y3, uvx1); \
  393. B1 = vec_add(Y3, ux1); \
  394. R = vec_packclp(R0, R1); \
  395. G = vec_packclp(G0, G1); \
  396. B = vec_packclp(B0, B1); \
  397. \
  398. \
  399. out_pixels(R, G, B, outo); \
  400. \
  401. y1i += 16; \
  402. y2i += 16; \
  403. ui += 8; \
  404. vi += 8; \
  405. } \
  406. \
  407. ui += instrides_scl[1]; \
  408. vi += instrides_scl[2]; \
  409. y1i += instrides_scl[0]; \
  410. y2i += instrides_scl[0]; \
  411. } \
  412. return srcSliceH; \
  413. }
  414. #define out_abgr(a, b, c, ptr) \
  415. vec_mstrgb32(__typeof__(a), ((__typeof__(a)) { 255 }), c, b, a, ptr)
  416. #define out_bgra(a, b, c, ptr) \
  417. vec_mstrgb32(__typeof__(a), c, b, a, ((__typeof__(a)) { 255 }), ptr)
  418. #define out_rgba(a, b, c, ptr) \
  419. vec_mstrgb32(__typeof__(a), a, b, c, ((__typeof__(a)) { 255 }), ptr)
  420. #define out_argb(a, b, c, ptr) \
  421. vec_mstrgb32(__typeof__(a), ((__typeof__(a)) { 255 }), a, b, c, ptr)
  422. #define out_rgb24(a, b, c, ptr) vec_mstrgb24(a, b, c, ptr)
  423. #define out_bgr24(a, b, c, ptr) vec_mstbgr24(a, b, c, ptr)
  424. DEFCSP420_CVT(yuv2_abgr, out_abgr)
  425. DEFCSP420_CVT(yuv2_bgra, out_bgra)
  426. DEFCSP420_CVT(yuv2_rgba, out_rgba)
  427. DEFCSP420_CVT(yuv2_argb, out_argb)
  428. DEFCSP420_CVT(yuv2_rgb24, out_rgb24)
  429. DEFCSP420_CVT(yuv2_bgr24, out_bgr24)
  430. // uyvy|uyvy|uyvy|uyvy
  431. // 0123 4567 89ab cdef
  432. static const vector unsigned char
  433. demux_u = { 0x10, 0x00, 0x10, 0x00,
  434. 0x10, 0x04, 0x10, 0x04,
  435. 0x10, 0x08, 0x10, 0x08,
  436. 0x10, 0x0c, 0x10, 0x0c },
  437. demux_v = { 0x10, 0x02, 0x10, 0x02,
  438. 0x10, 0x06, 0x10, 0x06,
  439. 0x10, 0x0A, 0x10, 0x0A,
  440. 0x10, 0x0E, 0x10, 0x0E },
  441. demux_y = { 0x10, 0x01, 0x10, 0x03,
  442. 0x10, 0x05, 0x10, 0x07,
  443. 0x10, 0x09, 0x10, 0x0B,
  444. 0x10, 0x0D, 0x10, 0x0F };
  445. /*
  446. * this is so I can play live CCIR raw video
  447. */
  448. static int altivec_uyvy_rgb32(SwsContext *c, const unsigned char **in,
  449. int *instrides, int srcSliceY, int srcSliceH,
  450. unsigned char **oplanes, int *outstrides)
  451. {
  452. int w = c->srcW;
  453. int h = srcSliceH;
  454. int i, j;
  455. vector unsigned char uyvy;
  456. vector signed short Y, U, V;
  457. vector signed short R0, G0, B0, R1, G1, B1;
  458. vector unsigned char R, G, B;
  459. vector unsigned char *out;
  460. const ubyte *img;
  461. img = in[0];
  462. out = (vector unsigned char *) (oplanes[0] + srcSliceY * outstrides[0]);
  463. for (i = 0; i < h; i++)
  464. for (j = 0; j < w / 16; j++) {
  465. uyvy = vec_ld(0, img);
  466. U = (vector signed short)
  467. vec_perm(uyvy, (vector unsigned char) { 0 }, demux_u);
  468. V = (vector signed short)
  469. vec_perm(uyvy, (vector unsigned char) { 0 }, demux_v);
  470. Y = (vector signed short)
  471. vec_perm(uyvy, (vector unsigned char) { 0 }, demux_y);
  472. cvtyuvtoRGB(c, Y, U, V, &R0, &G0, &B0);
  473. uyvy = vec_ld(16, img);
  474. U = (vector signed short)
  475. vec_perm(uyvy, (vector unsigned char) { 0 }, demux_u);
  476. V = (vector signed short)
  477. vec_perm(uyvy, (vector unsigned char) { 0 }, demux_v);
  478. Y = (vector signed short)
  479. vec_perm(uyvy, (vector unsigned char) { 0 }, demux_y);
  480. cvtyuvtoRGB(c, Y, U, V, &R1, &G1, &B1);
  481. R = vec_packclp(R0, R1);
  482. G = vec_packclp(G0, G1);
  483. B = vec_packclp(B0, B1);
  484. // vec_mstbgr24 (R,G,B, out);
  485. out_rgba(R, G, B, out);
  486. img += 32;
  487. }
  488. return srcSliceH;
  489. }
  490. /* Ok currently the acceleration routine only supports
  491. * inputs of widths a multiple of 16
  492. * and heights a multiple 2
  493. *
  494. * So we just fall back to the C codes for this.
  495. */
  496. av_cold SwsFunc ff_yuv2rgb_init_altivec(SwsContext *c)
  497. {
  498. if (!(av_get_cpu_flags() & AV_CPU_FLAG_ALTIVEC))
  499. return NULL;
  500. /*
  501. * and this seems not to matter too much I tried a bunch of
  502. * videos with abnormal widths and MPlayer crashes elsewhere.
  503. * mplayer -vo x11 -rawvideo on:w=350:h=240 raw-350x240.eyuv
  504. * boom with X11 bad match.
  505. *
  506. */
  507. if ((c->srcW & 0xf) != 0)
  508. return NULL;
  509. switch (c->srcFormat) {
  510. case AV_PIX_FMT_YUV410P:
  511. case AV_PIX_FMT_YUV420P:
  512. /*case IMGFMT_CLPL: ??? */
  513. case AV_PIX_FMT_GRAY8:
  514. case AV_PIX_FMT_NV12:
  515. case AV_PIX_FMT_NV21:
  516. if ((c->srcH & 0x1) != 0)
  517. return NULL;
  518. switch (c->dstFormat) {
  519. case AV_PIX_FMT_RGB24:
  520. av_log(c, AV_LOG_WARNING, "ALTIVEC: Color Space RGB24\n");
  521. return altivec_yuv2_rgb24;
  522. case AV_PIX_FMT_BGR24:
  523. av_log(c, AV_LOG_WARNING, "ALTIVEC: Color Space BGR24\n");
  524. return altivec_yuv2_bgr24;
  525. case AV_PIX_FMT_ARGB:
  526. av_log(c, AV_LOG_WARNING, "ALTIVEC: Color Space ARGB\n");
  527. return altivec_yuv2_argb;
  528. case AV_PIX_FMT_ABGR:
  529. av_log(c, AV_LOG_WARNING, "ALTIVEC: Color Space ABGR\n");
  530. return altivec_yuv2_abgr;
  531. case AV_PIX_FMT_RGBA:
  532. av_log(c, AV_LOG_WARNING, "ALTIVEC: Color Space RGBA\n");
  533. return altivec_yuv2_rgba;
  534. case AV_PIX_FMT_BGRA:
  535. av_log(c, AV_LOG_WARNING, "ALTIVEC: Color Space BGRA\n");
  536. return altivec_yuv2_bgra;
  537. default: return NULL;
  538. }
  539. break;
  540. case AV_PIX_FMT_UYVY422:
  541. switch (c->dstFormat) {
  542. case AV_PIX_FMT_BGR32:
  543. av_log(c, AV_LOG_WARNING, "ALTIVEC: Color Space UYVY -> RGB32\n");
  544. return altivec_uyvy_rgb32;
  545. default: return NULL;
  546. }
  547. break;
  548. }
  549. return NULL;
  550. }
  551. av_cold void ff_yuv2rgb_init_tables_altivec(SwsContext *c,
  552. const int inv_table[4],
  553. int brightness,
  554. int contrast,
  555. int saturation)
  556. {
  557. union {
  558. DECLARE_ALIGNED(16, signed short, tmp)[8];
  559. vector signed short vec;
  560. } buf;
  561. buf.tmp[0] = ((0xffffLL) * contrast >> 8) >> 9; // cy
  562. buf.tmp[1] = -256 * brightness; // oy
  563. buf.tmp[2] = (inv_table[0] >> 3) * (contrast >> 16) * (saturation >> 16); // crv
  564. buf.tmp[3] = (inv_table[1] >> 3) * (contrast >> 16) * (saturation >> 16); // cbu
  565. buf.tmp[4] = -((inv_table[2] >> 1) * (contrast >> 16) * (saturation >> 16)); // cgu
  566. buf.tmp[5] = -((inv_table[3] >> 1) * (contrast >> 16) * (saturation >> 16)); // cgv
  567. c->CSHIFT = (vector unsigned short) vec_splat_u16(2);
  568. c->CY = vec_splat((vector signed short) buf.vec, 0);
  569. c->OY = vec_splat((vector signed short) buf.vec, 1);
  570. c->CRV = vec_splat((vector signed short) buf.vec, 2);
  571. c->CBU = vec_splat((vector signed short) buf.vec, 3);
  572. c->CGU = vec_splat((vector signed short) buf.vec, 4);
  573. c->CGV = vec_splat((vector signed short) buf.vec, 5);
  574. return;
  575. }
  576. static av_always_inline void yuv2packedX_altivec(SwsContext *c,
  577. const int16_t *lumFilter,
  578. const int16_t **lumSrc,
  579. int lumFilterSize,
  580. const int16_t *chrFilter,
  581. const int16_t **chrUSrc,
  582. const int16_t **chrVSrc,
  583. int chrFilterSize,
  584. const int16_t **alpSrc,
  585. uint8_t *dest,
  586. int dstW, int dstY,
  587. enum AVPixelFormat target)
  588. {
  589. int i, j;
  590. vector signed short X, X0, X1, Y0, U0, V0, Y1, U1, V1, U, V;
  591. vector signed short R0, G0, B0, R1, G1, B1;
  592. vector unsigned char R, G, B;
  593. vector unsigned char *out, *nout;
  594. vector signed short RND = vec_splat_s16(1 << 3);
  595. vector unsigned short SCL = vec_splat_u16(4);
  596. DECLARE_ALIGNED(16, unsigned int, scratch)[16];
  597. vector signed short *YCoeffs, *CCoeffs;
  598. YCoeffs = c->vYCoeffsBank + dstY * lumFilterSize;
  599. CCoeffs = c->vCCoeffsBank + dstY * chrFilterSize;
  600. out = (vector unsigned char *) dest;
  601. for (i = 0; i < dstW; i += 16) {
  602. Y0 = RND;
  603. Y1 = RND;
  604. /* extract 16 coeffs from lumSrc */
  605. for (j = 0; j < lumFilterSize; j++) {
  606. X0 = vec_ld(0, &lumSrc[j][i]);
  607. X1 = vec_ld(16, &lumSrc[j][i]);
  608. Y0 = vec_mradds(X0, YCoeffs[j], Y0);
  609. Y1 = vec_mradds(X1, YCoeffs[j], Y1);
  610. }
  611. U = RND;
  612. V = RND;
  613. /* extract 8 coeffs from U,V */
  614. for (j = 0; j < chrFilterSize; j++) {
  615. X = vec_ld(0, &chrUSrc[j][i / 2]);
  616. U = vec_mradds(X, CCoeffs[j], U);
  617. X = vec_ld(0, &chrVSrc[j][i / 2]);
  618. V = vec_mradds(X, CCoeffs[j], V);
  619. }
  620. /* scale and clip signals */
  621. Y0 = vec_sra(Y0, SCL);
  622. Y1 = vec_sra(Y1, SCL);
  623. U = vec_sra(U, SCL);
  624. V = vec_sra(V, SCL);
  625. Y0 = vec_clip_s16(Y0);
  626. Y1 = vec_clip_s16(Y1);
  627. U = vec_clip_s16(U);
  628. V = vec_clip_s16(V);
  629. /* now we have
  630. * Y0 = y0 y1 y2 y3 y4 y5 y6 y7 Y1 = y8 y9 y10 y11 y12 y13 y14 y15
  631. * U = u0 u1 u2 u3 u4 u5 u6 u7 V = v0 v1 v2 v3 v4 v5 v6 v7
  632. *
  633. * Y0 = y0 y1 y2 y3 y4 y5 y6 y7 Y1 = y8 y9 y10 y11 y12 y13 y14 y15
  634. * U0 = u0 u0 u1 u1 u2 u2 u3 u3 U1 = u4 u4 u5 u5 u6 u6 u7 u7
  635. * V0 = v0 v0 v1 v1 v2 v2 v3 v3 V1 = v4 v4 v5 v5 v6 v6 v7 v7
  636. */
  637. U0 = vec_mergeh(U, U);
  638. V0 = vec_mergeh(V, V);
  639. U1 = vec_mergel(U, U);
  640. V1 = vec_mergel(V, V);
  641. cvtyuvtoRGB(c, Y0, U0, V0, &R0, &G0, &B0);
  642. cvtyuvtoRGB(c, Y1, U1, V1, &R1, &G1, &B1);
  643. R = vec_packclp(R0, R1);
  644. G = vec_packclp(G0, G1);
  645. B = vec_packclp(B0, B1);
  646. switch (target) {
  647. case AV_PIX_FMT_ABGR:
  648. out_abgr(R, G, B, out);
  649. break;
  650. case AV_PIX_FMT_BGRA:
  651. out_bgra(R, G, B, out);
  652. break;
  653. case AV_PIX_FMT_RGBA:
  654. out_rgba(R, G, B, out);
  655. break;
  656. case AV_PIX_FMT_ARGB:
  657. out_argb(R, G, B, out);
  658. break;
  659. case AV_PIX_FMT_RGB24:
  660. out_rgb24(R, G, B, out);
  661. break;
  662. case AV_PIX_FMT_BGR24:
  663. out_bgr24(R, G, B, out);
  664. break;
  665. default:
  666. {
  667. /* If this is reached, the caller should have called yuv2packedXinC
  668. * instead. */
  669. static int printed_error_message;
  670. if (!printed_error_message) {
  671. av_log(c, AV_LOG_ERROR,
  672. "altivec_yuv2packedX doesn't support %s output\n",
  673. av_get_pix_fmt_name(c->dstFormat));
  674. printed_error_message = 1;
  675. }
  676. return;
  677. }
  678. }
  679. }
  680. if (i < dstW) {
  681. i -= 16;
  682. Y0 = RND;
  683. Y1 = RND;
  684. /* extract 16 coeffs from lumSrc */
  685. for (j = 0; j < lumFilterSize; j++) {
  686. X0 = vec_ld(0, &lumSrc[j][i]);
  687. X1 = vec_ld(16, &lumSrc[j][i]);
  688. Y0 = vec_mradds(X0, YCoeffs[j], Y0);
  689. Y1 = vec_mradds(X1, YCoeffs[j], Y1);
  690. }
  691. U = RND;
  692. V = RND;
  693. /* extract 8 coeffs from U,V */
  694. for (j = 0; j < chrFilterSize; j++) {
  695. X = vec_ld(0, &chrUSrc[j][i / 2]);
  696. U = vec_mradds(X, CCoeffs[j], U);
  697. X = vec_ld(0, &chrVSrc[j][i / 2]);
  698. V = vec_mradds(X, CCoeffs[j], V);
  699. }
  700. /* scale and clip signals */
  701. Y0 = vec_sra(Y0, SCL);
  702. Y1 = vec_sra(Y1, SCL);
  703. U = vec_sra(U, SCL);
  704. V = vec_sra(V, SCL);
  705. Y0 = vec_clip_s16(Y0);
  706. Y1 = vec_clip_s16(Y1);
  707. U = vec_clip_s16(U);
  708. V = vec_clip_s16(V);
  709. /* now we have
  710. * Y0 = y0 y1 y2 y3 y4 y5 y6 y7 Y1 = y8 y9 y10 y11 y12 y13 y14 y15
  711. * U = u0 u1 u2 u3 u4 u5 u6 u7 V = v0 v1 v2 v3 v4 v5 v6 v7
  712. *
  713. * Y0 = y0 y1 y2 y3 y4 y5 y6 y7 Y1 = y8 y9 y10 y11 y12 y13 y14 y15
  714. * U0 = u0 u0 u1 u1 u2 u2 u3 u3 U1 = u4 u4 u5 u5 u6 u6 u7 u7
  715. * V0 = v0 v0 v1 v1 v2 v2 v3 v3 V1 = v4 v4 v5 v5 v6 v6 v7 v7
  716. */
  717. U0 = vec_mergeh(U, U);
  718. V0 = vec_mergeh(V, V);
  719. U1 = vec_mergel(U, U);
  720. V1 = vec_mergel(V, V);
  721. cvtyuvtoRGB(c, Y0, U0, V0, &R0, &G0, &B0);
  722. cvtyuvtoRGB(c, Y1, U1, V1, &R1, &G1, &B1);
  723. R = vec_packclp(R0, R1);
  724. G = vec_packclp(G0, G1);
  725. B = vec_packclp(B0, B1);
  726. nout = (vector unsigned char *) scratch;
  727. switch (target) {
  728. case AV_PIX_FMT_ABGR:
  729. out_abgr(R, G, B, nout);
  730. break;
  731. case AV_PIX_FMT_BGRA:
  732. out_bgra(R, G, B, nout);
  733. break;
  734. case AV_PIX_FMT_RGBA:
  735. out_rgba(R, G, B, nout);
  736. break;
  737. case AV_PIX_FMT_ARGB:
  738. out_argb(R, G, B, nout);
  739. break;
  740. case AV_PIX_FMT_RGB24:
  741. out_rgb24(R, G, B, nout);
  742. break;
  743. case AV_PIX_FMT_BGR24:
  744. out_bgr24(R, G, B, nout);
  745. break;
  746. default:
  747. /* Unreachable, I think. */
  748. av_log(c, AV_LOG_ERROR,
  749. "altivec_yuv2packedX doesn't support %s output\n",
  750. av_get_pix_fmt_name(c->dstFormat));
  751. return;
  752. }
  753. memcpy(&((uint32_t *) dest)[i], scratch, (dstW - i) / 4);
  754. }
  755. }
  756. #define YUV2PACKEDX_WRAPPER(suffix, pixfmt) \
  757. void ff_yuv2 ## suffix ## _X_altivec(SwsContext *c, \
  758. const int16_t *lumFilter, \
  759. const int16_t **lumSrc, \
  760. int lumFilterSize, \
  761. const int16_t *chrFilter, \
  762. const int16_t **chrUSrc, \
  763. const int16_t **chrVSrc, \
  764. int chrFilterSize, \
  765. const int16_t **alpSrc, \
  766. uint8_t *dest, int dstW, int dstY) \
  767. { \
  768. yuv2packedX_altivec(c, lumFilter, lumSrc, lumFilterSize, \
  769. chrFilter, chrUSrc, chrVSrc, \
  770. chrFilterSize, alpSrc, \
  771. dest, dstW, dstY, pixfmt); \
  772. }
  773. YUV2PACKEDX_WRAPPER(abgr, AV_PIX_FMT_ABGR);
  774. YUV2PACKEDX_WRAPPER(bgra, AV_PIX_FMT_BGRA);
  775. YUV2PACKEDX_WRAPPER(argb, AV_PIX_FMT_ARGB);
  776. YUV2PACKEDX_WRAPPER(rgba, AV_PIX_FMT_RGBA);
  777. YUV2PACKEDX_WRAPPER(rgb24, AV_PIX_FMT_RGB24);
  778. YUV2PACKEDX_WRAPPER(bgr24, AV_PIX_FMT_BGR24);