yuv2rgb_altivec.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  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. //#define out_pixels(a, b, c, ptr) vec_mstrgb32(__typeof__(a), ((__typeof__(a)) { 255 }), a, a, a, ptr)
  234. static inline void cvtyuvtoRGB(SwsContext *c, vector signed short Y,
  235. vector signed short U, vector signed short V,
  236. vector signed short *R, vector signed short *G,
  237. vector signed short *B)
  238. {
  239. vector signed short vx, ux, uvx;
  240. Y = vec_mradds(Y, c->CY, c->OY);
  241. U = vec_sub(U, (vector signed short)
  242. vec_splat((vector signed short) { 128 }, 0));
  243. V = vec_sub(V, (vector signed short)
  244. vec_splat((vector signed short) { 128 }, 0));
  245. // ux = (CBU * (u << c->CSHIFT) + 0x4000) >> 15;
  246. ux = vec_sl(U, c->CSHIFT);
  247. *B = vec_mradds(ux, c->CBU, Y);
  248. // vx = (CRV * (v << c->CSHIFT) + 0x4000) >> 15;
  249. vx = vec_sl(V, c->CSHIFT);
  250. *R = vec_mradds(vx, c->CRV, Y);
  251. // uvx = ((CGU * u) + (CGV * v)) >> 15;
  252. uvx = vec_mradds(U, c->CGU, Y);
  253. *G = vec_mradds(V, c->CGV, uvx);
  254. }
  255. /*
  256. * ------------------------------------------------------------------------------
  257. * CS converters
  258. * ------------------------------------------------------------------------------
  259. */
  260. #define DEFCSP420_CVT(name, out_pixels) \
  261. static int altivec_ ## name(SwsContext *c, const unsigned char **in, \
  262. int *instrides, int srcSliceY, int srcSliceH, \
  263. unsigned char **oplanes, int *outstrides) \
  264. { \
  265. int w = c->srcW; \
  266. int h = srcSliceH; \
  267. int i, j; \
  268. int instrides_scl[3]; \
  269. vector unsigned char y0, y1; \
  270. \
  271. vector signed char u, v; \
  272. \
  273. vector signed short Y0, Y1, Y2, Y3; \
  274. vector signed short U, V; \
  275. vector signed short vx, ux, uvx; \
  276. vector signed short vx0, ux0, uvx0; \
  277. vector signed short vx1, ux1, uvx1; \
  278. vector signed short R0, G0, B0; \
  279. vector signed short R1, G1, B1; \
  280. vector unsigned char R, G, B; \
  281. \
  282. const vector unsigned char *y1ivP, *y2ivP, *uivP, *vivP; \
  283. vector unsigned char align_perm; \
  284. \
  285. vector signed short lCY = c->CY; \
  286. vector signed short lOY = c->OY; \
  287. vector signed short lCRV = c->CRV; \
  288. vector signed short lCBU = c->CBU; \
  289. vector signed short lCGU = c->CGU; \
  290. vector signed short lCGV = c->CGV; \
  291. vector unsigned short lCSHIFT = c->CSHIFT; \
  292. \
  293. const ubyte *y1i = in[0]; \
  294. const ubyte *y2i = in[0] + instrides[0]; \
  295. const ubyte *ui = in[1]; \
  296. const ubyte *vi = in[2]; \
  297. \
  298. vector unsigned char *oute, *outo; \
  299. \
  300. /* loop moves y{1, 2}i by w */ \
  301. instrides_scl[0] = instrides[0] * 2 - w; \
  302. /* loop moves ui by w / 2 */ \
  303. instrides_scl[1] = instrides[1] - w / 2; \
  304. /* loop moves vi by w / 2 */ \
  305. instrides_scl[2] = instrides[2] - w / 2; \
  306. \
  307. for (i = 0; i < h / 2; i++) { \
  308. oute = (vector unsigned char *)(oplanes[0] + outstrides[0] * \
  309. (srcSliceY + i * 2)); \
  310. outo = oute + (outstrides[0] >> 4); \
  311. vec_dstst(outo, (0x02000002 | (((w * 3 + 32) / 32) << 16)), 0); \
  312. vec_dstst(oute, (0x02000002 | (((w * 3 + 32) / 32) << 16)), 1); \
  313. \
  314. for (j = 0; j < w / 16; j++) { \
  315. y1ivP = (const vector unsigned char *) y1i; \
  316. y2ivP = (const vector unsigned char *) y2i; \
  317. uivP = (const vector unsigned char *) ui; \
  318. vivP = (const vector unsigned char *) vi; \
  319. \
  320. align_perm = vec_lvsl(0, y1i); \
  321. y0 = (vector unsigned char) \
  322. vec_perm(y1ivP[0], y1ivP[1], align_perm); \
  323. \
  324. align_perm = vec_lvsl(0, y2i); \
  325. y1 = (vector unsigned char) \
  326. vec_perm(y2ivP[0], y2ivP[1], align_perm); \
  327. \
  328. align_perm = vec_lvsl(0, ui); \
  329. u = (vector signed char) \
  330. vec_perm(uivP[0], uivP[1], align_perm); \
  331. \
  332. align_perm = vec_lvsl(0, vi); \
  333. v = (vector signed char) \
  334. vec_perm(vivP[0], vivP[1], align_perm); \
  335. \
  336. u = (vector signed char) \
  337. vec_sub(u, \
  338. (vector signed char) \
  339. vec_splat((vector signed char) { 128 }, 0)); \
  340. v = (vector signed char) \
  341. vec_sub(v, \
  342. (vector signed char) \
  343. vec_splat((vector signed char) { 128 }, 0)); \
  344. \
  345. U = vec_unpackh(u); \
  346. V = vec_unpackh(v); \
  347. \
  348. Y0 = vec_unh(y0); \
  349. Y1 = vec_unl(y0); \
  350. Y2 = vec_unh(y1); \
  351. Y3 = vec_unl(y1); \
  352. \
  353. Y0 = vec_mradds(Y0, lCY, lOY); \
  354. Y1 = vec_mradds(Y1, lCY, lOY); \
  355. Y2 = vec_mradds(Y2, lCY, lOY); \
  356. Y3 = vec_mradds(Y3, lCY, lOY); \
  357. \
  358. /* ux = (CBU * (u << CSHIFT) + 0x4000) >> 15 */ \
  359. ux = vec_sl(U, lCSHIFT); \
  360. ux = vec_mradds(ux, lCBU, (vector signed short) { 0 }); \
  361. ux0 = vec_mergeh(ux, ux); \
  362. ux1 = vec_mergel(ux, ux); \
  363. \
  364. /* vx = (CRV * (v << CSHIFT) + 0x4000) >> 15; */ \
  365. vx = vec_sl(V, lCSHIFT); \
  366. vx = vec_mradds(vx, lCRV, (vector signed short) { 0 }); \
  367. vx0 = vec_mergeh(vx, vx); \
  368. vx1 = vec_mergel(vx, vx); \
  369. \
  370. /* uvx = ((CGU * u) + (CGV * v)) >> 15 */ \
  371. uvx = vec_mradds(U, lCGU, (vector signed short) { 0 }); \
  372. uvx = vec_mradds(V, lCGV, uvx); \
  373. uvx0 = vec_mergeh(uvx, uvx); \
  374. uvx1 = vec_mergel(uvx, uvx); \
  375. \
  376. R0 = vec_add(Y0, vx0); \
  377. G0 = vec_add(Y0, uvx0); \
  378. B0 = vec_add(Y0, ux0); \
  379. R1 = vec_add(Y1, vx1); \
  380. G1 = vec_add(Y1, uvx1); \
  381. B1 = vec_add(Y1, ux1); \
  382. \
  383. R = vec_packclp(R0, R1); \
  384. G = vec_packclp(G0, G1); \
  385. B = vec_packclp(B0, B1); \
  386. \
  387. out_pixels(R, G, B, oute); \
  388. \
  389. R0 = vec_add(Y2, vx0); \
  390. G0 = vec_add(Y2, uvx0); \
  391. B0 = vec_add(Y2, ux0); \
  392. R1 = vec_add(Y3, vx1); \
  393. G1 = vec_add(Y3, uvx1); \
  394. B1 = vec_add(Y3, ux1); \
  395. R = vec_packclp(R0, R1); \
  396. G = vec_packclp(G0, G1); \
  397. B = vec_packclp(B0, B1); \
  398. \
  399. \
  400. out_pixels(R, G, B, outo); \
  401. \
  402. y1i += 16; \
  403. y2i += 16; \
  404. ui += 8; \
  405. vi += 8; \
  406. } \
  407. \
  408. ui += instrides_scl[1]; \
  409. vi += instrides_scl[2]; \
  410. y1i += instrides_scl[0]; \
  411. y2i += instrides_scl[0]; \
  412. } \
  413. return srcSliceH; \
  414. }
  415. #define out_abgr(a, b, c, ptr) \
  416. vec_mstrgb32(__typeof__(a), ((__typeof__(a)) { 255 }), c, b, a, ptr)
  417. #define out_bgra(a, b, c, ptr) \
  418. vec_mstrgb32(__typeof__(a), c, b, a, ((__typeof__(a)) { 255 }), ptr)
  419. #define out_rgba(a, b, c, ptr) \
  420. vec_mstrgb32(__typeof__(a), a, b, c, ((__typeof__(a)) { 255 }), ptr)
  421. #define out_argb(a, b, c, ptr) \
  422. vec_mstrgb32(__typeof__(a), ((__typeof__(a)) { 255 }), a, b, c, ptr)
  423. #define out_rgb24(a, b, c, ptr) vec_mstrgb24(a, b, c, ptr)
  424. #define out_bgr24(a, b, c, ptr) vec_mstbgr24(a, b, c, ptr)
  425. DEFCSP420_CVT(yuv2_abgr, out_abgr)
  426. DEFCSP420_CVT(yuv2_bgra, out_bgra)
  427. DEFCSP420_CVT(yuv2_rgba, out_rgba)
  428. DEFCSP420_CVT(yuv2_argb, out_argb)
  429. DEFCSP420_CVT(yuv2_rgb24, out_rgb24)
  430. DEFCSP420_CVT(yuv2_bgr24, out_bgr24)
  431. // uyvy|uyvy|uyvy|uyvy
  432. // 0123 4567 89ab cdef
  433. static const vector unsigned char
  434. demux_u = { 0x10, 0x00, 0x10, 0x00,
  435. 0x10, 0x04, 0x10, 0x04,
  436. 0x10, 0x08, 0x10, 0x08,
  437. 0x10, 0x0c, 0x10, 0x0c },
  438. demux_v = { 0x10, 0x02, 0x10, 0x02,
  439. 0x10, 0x06, 0x10, 0x06,
  440. 0x10, 0x0A, 0x10, 0x0A,
  441. 0x10, 0x0E, 0x10, 0x0E },
  442. demux_y = { 0x10, 0x01, 0x10, 0x03,
  443. 0x10, 0x05, 0x10, 0x07,
  444. 0x10, 0x09, 0x10, 0x0B,
  445. 0x10, 0x0D, 0x10, 0x0F };
  446. /*
  447. * this is so I can play live CCIR raw video
  448. */
  449. static int altivec_uyvy_rgb32(SwsContext *c, const unsigned char **in,
  450. int *instrides, int srcSliceY, int srcSliceH,
  451. unsigned char **oplanes, int *outstrides)
  452. {
  453. int w = c->srcW;
  454. int h = srcSliceH;
  455. int i, j;
  456. vector unsigned char uyvy;
  457. vector signed short Y, U, V;
  458. vector signed short R0, G0, B0, R1, G1, B1;
  459. vector unsigned char R, G, B;
  460. vector unsigned char *out;
  461. const ubyte *img;
  462. img = in[0];
  463. out = (vector unsigned char *) (oplanes[0] + srcSliceY * outstrides[0]);
  464. for (i = 0; i < h; i++)
  465. for (j = 0; j < w / 16; j++) {
  466. uyvy = vec_ld(0, img);
  467. U = (vector signed short)
  468. vec_perm(uyvy, (vector unsigned char) { 0 }, demux_u);
  469. V = (vector signed short)
  470. vec_perm(uyvy, (vector unsigned char) { 0 }, demux_v);
  471. Y = (vector signed short)
  472. vec_perm(uyvy, (vector unsigned char) { 0 }, demux_y);
  473. cvtyuvtoRGB(c, Y, U, V, &R0, &G0, &B0);
  474. uyvy = vec_ld(16, img);
  475. U = (vector signed short)
  476. vec_perm(uyvy, (vector unsigned char) { 0 }, demux_u);
  477. V = (vector signed short)
  478. vec_perm(uyvy, (vector unsigned char) { 0 }, demux_v);
  479. Y = (vector signed short)
  480. vec_perm(uyvy, (vector unsigned char) { 0 }, demux_y);
  481. cvtyuvtoRGB(c, Y, U, V, &R1, &G1, &B1);
  482. R = vec_packclp(R0, R1);
  483. G = vec_packclp(G0, G1);
  484. B = vec_packclp(B0, B1);
  485. // vec_mstbgr24 (R,G,B, out);
  486. out_rgba(R, G, B, out);
  487. img += 32;
  488. }
  489. return srcSliceH;
  490. }
  491. /* Ok currently the acceleration routine only supports
  492. * inputs of widths a multiple of 16
  493. * and heights a multiple 2
  494. *
  495. * So we just fall back to the C codes for this.
  496. */
  497. av_cold SwsFunc ff_yuv2rgb_init_altivec(SwsContext *c)
  498. {
  499. if (!(av_get_cpu_flags() & AV_CPU_FLAG_ALTIVEC))
  500. return NULL;
  501. /*
  502. * and this seems not to matter too much I tried a bunch of
  503. * videos with abnormal widths and MPlayer crashes elsewhere.
  504. * mplayer -vo x11 -rawvideo on:w=350:h=240 raw-350x240.eyuv
  505. * boom with X11 bad match.
  506. *
  507. */
  508. if ((c->srcW & 0xf) != 0)
  509. return NULL;
  510. switch (c->srcFormat) {
  511. case PIX_FMT_YUV410P:
  512. case PIX_FMT_YUV420P:
  513. /*case IMGFMT_CLPL: ??? */
  514. case PIX_FMT_GRAY8:
  515. case PIX_FMT_NV12:
  516. case PIX_FMT_NV21:
  517. if ((c->srcH & 0x1) != 0)
  518. return NULL;
  519. switch (c->dstFormat) {
  520. case PIX_FMT_RGB24:
  521. av_log(c, AV_LOG_WARNING, "ALTIVEC: Color Space RGB24\n");
  522. return altivec_yuv2_rgb24;
  523. case PIX_FMT_BGR24:
  524. av_log(c, AV_LOG_WARNING, "ALTIVEC: Color Space BGR24\n");
  525. return altivec_yuv2_bgr24;
  526. case PIX_FMT_ARGB:
  527. av_log(c, AV_LOG_WARNING, "ALTIVEC: Color Space ARGB\n");
  528. return altivec_yuv2_argb;
  529. case PIX_FMT_ABGR:
  530. av_log(c, AV_LOG_WARNING, "ALTIVEC: Color Space ABGR\n");
  531. return altivec_yuv2_abgr;
  532. case PIX_FMT_RGBA:
  533. av_log(c, AV_LOG_WARNING, "ALTIVEC: Color Space RGBA\n");
  534. return altivec_yuv2_rgba;
  535. case PIX_FMT_BGRA:
  536. av_log(c, AV_LOG_WARNING, "ALTIVEC: Color Space BGRA\n");
  537. return altivec_yuv2_bgra;
  538. default: return NULL;
  539. }
  540. break;
  541. case PIX_FMT_UYVY422:
  542. switch (c->dstFormat) {
  543. case PIX_FMT_BGR32:
  544. av_log(c, AV_LOG_WARNING, "ALTIVEC: Color Space UYVY -> RGB32\n");
  545. return altivec_uyvy_rgb32;
  546. default: return NULL;
  547. }
  548. break;
  549. }
  550. return NULL;
  551. }
  552. av_cold void ff_yuv2rgb_init_tables_altivec(SwsContext *c,
  553. const int inv_table[4],
  554. int brightness,
  555. int contrast,
  556. int saturation)
  557. {
  558. union {
  559. DECLARE_ALIGNED(16, signed short, tmp)[8];
  560. vector signed short vec;
  561. } buf;
  562. buf.tmp[0] = ((0xffffLL) * contrast >> 8) >> 9; // cy
  563. buf.tmp[1] = -256 * brightness; // oy
  564. buf.tmp[2] = (inv_table[0] >> 3) * (contrast >> 16) * (saturation >> 16); // crv
  565. buf.tmp[3] = (inv_table[1] >> 3) * (contrast >> 16) * (saturation >> 16); // cbu
  566. buf.tmp[4] = -((inv_table[2] >> 1) * (contrast >> 16) * (saturation >> 16)); // cgu
  567. buf.tmp[5] = -((inv_table[3] >> 1) * (contrast >> 16) * (saturation >> 16)); // cgv
  568. c->CSHIFT = (vector unsigned short) vec_splat_u16(2);
  569. c->CY = vec_splat((vector signed short) buf.vec, 0);
  570. c->OY = vec_splat((vector signed short) buf.vec, 1);
  571. c->CRV = vec_splat((vector signed short) buf.vec, 2);
  572. c->CBU = vec_splat((vector signed short) buf.vec, 3);
  573. c->CGU = vec_splat((vector signed short) buf.vec, 4);
  574. c->CGV = vec_splat((vector signed short) buf.vec, 5);
  575. return;
  576. }
  577. static av_always_inline void ff_yuv2packedX_altivec(SwsContext *c,
  578. const int16_t *lumFilter,
  579. const int16_t **lumSrc,
  580. int lumFilterSize,
  581. const int16_t *chrFilter,
  582. const int16_t **chrUSrc,
  583. const int16_t **chrVSrc,
  584. int chrFilterSize,
  585. const int16_t **alpSrc,
  586. uint8_t *dest,
  587. int dstW, int dstY,
  588. enum PixelFormat target)
  589. {
  590. int i, j;
  591. vector signed short X, X0, X1, Y0, U0, V0, Y1, U1, V1, U, V;
  592. vector signed short R0, G0, B0, R1, G1, B1;
  593. vector unsigned char R, G, B;
  594. vector unsigned char *out, *nout;
  595. vector signed short RND = vec_splat_s16(1 << 3);
  596. vector unsigned short SCL = vec_splat_u16(4);
  597. DECLARE_ALIGNED(16, unsigned int, scratch)[16];
  598. vector signed short *YCoeffs, *CCoeffs;
  599. YCoeffs = c->vYCoeffsBank + dstY * lumFilterSize;
  600. CCoeffs = c->vCCoeffsBank + dstY * chrFilterSize;
  601. out = (vector unsigned char *) dest;
  602. for (i = 0; i < dstW; i += 16) {
  603. Y0 = RND;
  604. Y1 = RND;
  605. /* extract 16 coeffs from lumSrc */
  606. for (j = 0; j < lumFilterSize; j++) {
  607. X0 = vec_ld(0, &lumSrc[j][i]);
  608. X1 = vec_ld(16, &lumSrc[j][i]);
  609. Y0 = vec_mradds(X0, YCoeffs[j], Y0);
  610. Y1 = vec_mradds(X1, YCoeffs[j], Y1);
  611. }
  612. U = RND;
  613. V = RND;
  614. /* extract 8 coeffs from U,V */
  615. for (j = 0; j < chrFilterSize; j++) {
  616. X = vec_ld(0, &chrUSrc[j][i / 2]);
  617. U = vec_mradds(X, CCoeffs[j], U);
  618. X = vec_ld(0, &chrVSrc[j][i / 2]);
  619. V = vec_mradds(X, CCoeffs[j], V);
  620. }
  621. /* scale and clip signals */
  622. Y0 = vec_sra(Y0, SCL);
  623. Y1 = vec_sra(Y1, SCL);
  624. U = vec_sra(U, SCL);
  625. V = vec_sra(V, SCL);
  626. Y0 = vec_clip_s16(Y0);
  627. Y1 = vec_clip_s16(Y1);
  628. U = vec_clip_s16(U);
  629. V = vec_clip_s16(V);
  630. /* now we have
  631. * Y0 = y0 y1 y2 y3 y4 y5 y6 y7 Y1 = y8 y9 y10 y11 y12 y13 y14 y15
  632. * U = u0 u1 u2 u3 u4 u5 u6 u7 V = v0 v1 v2 v3 v4 v5 v6 v7
  633. *
  634. * Y0 = y0 y1 y2 y3 y4 y5 y6 y7 Y1 = y8 y9 y10 y11 y12 y13 y14 y15
  635. * U0 = u0 u0 u1 u1 u2 u2 u3 u3 U1 = u4 u4 u5 u5 u6 u6 u7 u7
  636. * V0 = v0 v0 v1 v1 v2 v2 v3 v3 V1 = v4 v4 v5 v5 v6 v6 v7 v7
  637. */
  638. U0 = vec_mergeh(U, U);
  639. V0 = vec_mergeh(V, V);
  640. U1 = vec_mergel(U, U);
  641. V1 = vec_mergel(V, V);
  642. cvtyuvtoRGB(c, Y0, U0, V0, &R0, &G0, &B0);
  643. cvtyuvtoRGB(c, Y1, U1, V1, &R1, &G1, &B1);
  644. R = vec_packclp(R0, R1);
  645. G = vec_packclp(G0, G1);
  646. B = vec_packclp(B0, B1);
  647. switch (target) {
  648. case PIX_FMT_ABGR:
  649. out_abgr(R, G, B, out);
  650. break;
  651. case PIX_FMT_BGRA:
  652. out_bgra(R, G, B, out);
  653. break;
  654. case PIX_FMT_RGBA:
  655. out_rgba(R, G, B, out);
  656. break;
  657. case PIX_FMT_ARGB:
  658. out_argb(R, G, B, out);
  659. break;
  660. case PIX_FMT_RGB24:
  661. out_rgb24(R, G, B, out);
  662. break;
  663. case PIX_FMT_BGR24:
  664. out_bgr24(R, G, B, out);
  665. break;
  666. default:
  667. {
  668. /* If this is reached, the caller should have called yuv2packedXinC
  669. * instead. */
  670. static int printed_error_message;
  671. if (!printed_error_message) {
  672. av_log(c, AV_LOG_ERROR,
  673. "altivec_yuv2packedX doesn't support %s output\n",
  674. av_get_pix_fmt_name(c->dstFormat));
  675. printed_error_message = 1;
  676. }
  677. return;
  678. }
  679. }
  680. }
  681. if (i < dstW) {
  682. i -= 16;
  683. Y0 = RND;
  684. Y1 = RND;
  685. /* extract 16 coeffs from lumSrc */
  686. for (j = 0; j < lumFilterSize; j++) {
  687. X0 = vec_ld(0, &lumSrc[j][i]);
  688. X1 = vec_ld(16, &lumSrc[j][i]);
  689. Y0 = vec_mradds(X0, YCoeffs[j], Y0);
  690. Y1 = vec_mradds(X1, YCoeffs[j], Y1);
  691. }
  692. U = RND;
  693. V = RND;
  694. /* extract 8 coeffs from U,V */
  695. for (j = 0; j < chrFilterSize; j++) {
  696. X = vec_ld(0, &chrUSrc[j][i / 2]);
  697. U = vec_mradds(X, CCoeffs[j], U);
  698. X = vec_ld(0, &chrVSrc[j][i / 2]);
  699. V = vec_mradds(X, CCoeffs[j], V);
  700. }
  701. /* scale and clip signals */
  702. Y0 = vec_sra(Y0, SCL);
  703. Y1 = vec_sra(Y1, SCL);
  704. U = vec_sra(U, SCL);
  705. V = vec_sra(V, SCL);
  706. Y0 = vec_clip_s16(Y0);
  707. Y1 = vec_clip_s16(Y1);
  708. U = vec_clip_s16(U);
  709. V = vec_clip_s16(V);
  710. /* now we have
  711. * Y0 = y0 y1 y2 y3 y4 y5 y6 y7 Y1 = y8 y9 y10 y11 y12 y13 y14 y15
  712. * U = u0 u1 u2 u3 u4 u5 u6 u7 V = v0 v1 v2 v3 v4 v5 v6 v7
  713. *
  714. * Y0 = y0 y1 y2 y3 y4 y5 y6 y7 Y1 = y8 y9 y10 y11 y12 y13 y14 y15
  715. * U0 = u0 u0 u1 u1 u2 u2 u3 u3 U1 = u4 u4 u5 u5 u6 u6 u7 u7
  716. * V0 = v0 v0 v1 v1 v2 v2 v3 v3 V1 = v4 v4 v5 v5 v6 v6 v7 v7
  717. */
  718. U0 = vec_mergeh(U, U);
  719. V0 = vec_mergeh(V, V);
  720. U1 = vec_mergel(U, U);
  721. V1 = vec_mergel(V, V);
  722. cvtyuvtoRGB(c, Y0, U0, V0, &R0, &G0, &B0);
  723. cvtyuvtoRGB(c, Y1, U1, V1, &R1, &G1, &B1);
  724. R = vec_packclp(R0, R1);
  725. G = vec_packclp(G0, G1);
  726. B = vec_packclp(B0, B1);
  727. nout = (vector unsigned char *) scratch;
  728. switch (target) {
  729. case PIX_FMT_ABGR:
  730. out_abgr(R, G, B, nout);
  731. break;
  732. case PIX_FMT_BGRA:
  733. out_bgra(R, G, B, nout);
  734. break;
  735. case PIX_FMT_RGBA:
  736. out_rgba(R, G, B, nout);
  737. break;
  738. case PIX_FMT_ARGB:
  739. out_argb(R, G, B, nout);
  740. break;
  741. case PIX_FMT_RGB24:
  742. out_rgb24(R, G, B, nout);
  743. break;
  744. case PIX_FMT_BGR24:
  745. out_bgr24(R, G, B, nout);
  746. break;
  747. default:
  748. /* Unreachable, I think. */
  749. av_log(c, AV_LOG_ERROR,
  750. "altivec_yuv2packedX doesn't support %s output\n",
  751. av_get_pix_fmt_name(c->dstFormat));
  752. return;
  753. }
  754. memcpy(&((uint32_t *) dest)[i], scratch, (dstW - i) / 4);
  755. }
  756. }
  757. #define YUV2PACKEDX_WRAPPER(suffix, pixfmt) \
  758. void ff_yuv2 ## suffix ## _X_altivec(SwsContext *c, \
  759. const int16_t *lumFilter, \
  760. const int16_t **lumSrc, \
  761. int lumFilterSize, \
  762. const int16_t *chrFilter, \
  763. const int16_t **chrUSrc, \
  764. const int16_t **chrVSrc, \
  765. int chrFilterSize, \
  766. const int16_t **alpSrc, \
  767. uint8_t *dest, int dstW, int dstY) \
  768. { \
  769. ff_yuv2packedX_altivec(c, lumFilter, lumSrc, lumFilterSize, \
  770. chrFilter, chrUSrc, chrVSrc, \
  771. chrFilterSize, alpSrc, \
  772. dest, dstW, dstY, pixfmt); \
  773. }
  774. YUV2PACKEDX_WRAPPER(abgr, PIX_FMT_ABGR);
  775. YUV2PACKEDX_WRAPPER(bgra, PIX_FMT_BGRA);
  776. YUV2PACKEDX_WRAPPER(argb, PIX_FMT_ARGB);
  777. YUV2PACKEDX_WRAPPER(rgba, PIX_FMT_RGBA);
  778. YUV2PACKEDX_WRAPPER(rgb24, PIX_FMT_RGB24);
  779. YUV2PACKEDX_WRAPPER(bgr24, PIX_FMT_BGR24);