yuv2rgb_altivec.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  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. #if HAVE_ALTIVEC
  100. #undef PROFILE_THE_BEAST
  101. #undef INC_SCALING
  102. typedef unsigned char ubyte;
  103. typedef signed char sbyte;
  104. /* RGB interleaver, 16 planar pels 8-bit samples per channel in
  105. * homogeneous vector registers x0,x1,x2 are interleaved with the
  106. * following technique:
  107. *
  108. * o0 = vec_mergeh(x0, x1);
  109. * o1 = vec_perm(o0, x2, perm_rgb_0);
  110. * o2 = vec_perm(o0, x2, perm_rgb_1);
  111. * o3 = vec_mergel(x0, x1);
  112. * o4 = vec_perm(o3, o2, perm_rgb_2);
  113. * o5 = vec_perm(o3, o2, perm_rgb_3);
  114. *
  115. * perm_rgb_0: o0(RG).h v1(B) --> o1*
  116. * 0 1 2 3 4
  117. * rgbr|gbrg|brgb|rgbr
  118. * 0010 0100 1001 0010
  119. * 0102 3145 2673 894A
  120. *
  121. * perm_rgb_1: o0(RG).h v1(B) --> o2
  122. * 0 1 2 3 4
  123. * gbrg|brgb|bbbb|bbbb
  124. * 0100 1001 1111 1111
  125. * B5CD 6EF7 89AB CDEF
  126. *
  127. * perm_rgb_2: o3(RG).l o2(rgbB.l) --> o4*
  128. * 0 1 2 3 4
  129. * gbrg|brgb|rgbr|gbrg
  130. * 1111 1111 0010 0100
  131. * 89AB CDEF 0182 3945
  132. *
  133. * perm_rgb_2: o3(RG).l o2(rgbB.l) ---> o5*
  134. * 0 1 2 3 4
  135. * brgb|rgbr|gbrg|brgb
  136. * 1001 0010 0100 1001
  137. * a67b 89cA BdCD eEFf
  138. *
  139. */
  140. static const vector unsigned char
  141. perm_rgb_0 = { 0x00, 0x01, 0x10, 0x02, 0x03, 0x11, 0x04, 0x05,
  142. 0x12, 0x06, 0x07, 0x13, 0x08, 0x09, 0x14, 0x0a },
  143. perm_rgb_1 = { 0x0b, 0x15, 0x0c, 0x0d, 0x16, 0x0e, 0x0f, 0x17,
  144. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f },
  145. perm_rgb_2 = { 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  146. 0x00, 0x01, 0x18, 0x02, 0x03, 0x19, 0x04, 0x05 },
  147. perm_rgb_3 = { 0x1a, 0x06, 0x07, 0x1b, 0x08, 0x09, 0x1c, 0x0a,
  148. 0x0b, 0x1d, 0x0c, 0x0d, 0x1e, 0x0e, 0x0f, 0x1f };
  149. #define vec_merge3(x2, x1, x0, y0, y1, y2) \
  150. do { \
  151. __typeof__(x0) o0, o2, o3; \
  152. o0 = vec_mergeh(x0, x1); \
  153. y0 = vec_perm(o0, x2, perm_rgb_0); \
  154. o2 = vec_perm(o0, x2, perm_rgb_1); \
  155. o3 = vec_mergel(x0, x1); \
  156. y1 = vec_perm(o3, o2, perm_rgb_2); \
  157. y2 = vec_perm(o3, o2, perm_rgb_3); \
  158. } while (0)
  159. #define vec_mstbgr24(x0, x1, x2, ptr) \
  160. do { \
  161. __typeof__(x0) _0, _1, _2; \
  162. vec_merge3(x0, x1, x2, _0, _1, _2); \
  163. vec_st(_0, 0, ptr++); \
  164. vec_st(_1, 0, ptr++); \
  165. vec_st(_2, 0, ptr++); \
  166. } while (0)
  167. #define vec_mstrgb24(x0, x1, x2, ptr) \
  168. do { \
  169. __typeof__(x0) _0, _1, _2; \
  170. vec_merge3(x2, x1, x0, _0, _1, _2); \
  171. vec_st(_0, 0, ptr++); \
  172. vec_st(_1, 0, ptr++); \
  173. vec_st(_2, 0, ptr++); \
  174. } while (0)
  175. /* pack the pixels in rgb0 format
  176. * msb R
  177. * lsb 0
  178. */
  179. #define vec_mstrgb32(T, x0, x1, x2, x3, ptr) \
  180. do { \
  181. T _0, _1, _2, _3; \
  182. _0 = vec_mergeh(x0, x1); \
  183. _1 = vec_mergeh(x2, x3); \
  184. _2 = (T) vec_mergeh((vector unsigned short) _0, \
  185. (vector unsigned short) _1); \
  186. _3 = (T) vec_mergel((vector unsigned short) _0, \
  187. (vector unsigned short) _1); \
  188. vec_st(_2, 0 * 16, (T *) ptr); \
  189. vec_st(_3, 1 * 16, (T *) ptr); \
  190. _0 = vec_mergel(x0, x1); \
  191. _1 = vec_mergel(x2, x3); \
  192. _2 = (T) vec_mergeh((vector unsigned short) _0, \
  193. (vector unsigned short) _1); \
  194. _3 = (T) vec_mergel((vector unsigned short) _0, \
  195. (vector unsigned short) _1); \
  196. vec_st(_2, 2 * 16, (T *) ptr); \
  197. vec_st(_3, 3 * 16, (T *) ptr); \
  198. ptr += 4; \
  199. } while (0)
  200. /*
  201. * 1 0 1.4021 | | Y |
  202. * 1 -0.3441 -0.7142 |x| Cb|
  203. * 1 1.7718 0 | | Cr|
  204. *
  205. *
  206. * Y: [-128 127]
  207. * Cb/Cr : [-128 127]
  208. *
  209. * typical YUV conversion works on Y: 0-255 this version has been
  210. * optimized for JPEG decoding.
  211. */
  212. #define vec_unh(x) \
  213. (vector signed short) \
  214. vec_perm(x, (__typeof__(x)) { 0 }, \
  215. ((vector unsigned char) { \
  216. 0x10, 0x00, 0x10, 0x01, 0x10, 0x02, 0x10, 0x03, \
  217. 0x10, 0x04, 0x10, 0x05, 0x10, 0x06, 0x10, 0x07 }))
  218. #define vec_unl(x) \
  219. (vector signed short) \
  220. vec_perm(x, (__typeof__(x)) { 0 }, \
  221. ((vector unsigned char) { \
  222. 0x10, 0x08, 0x10, 0x09, 0x10, 0x0A, 0x10, 0x0B, \
  223. 0x10, 0x0C, 0x10, 0x0D, 0x10, 0x0E, 0x10, 0x0F }))
  224. #define vec_clip_s16(x) \
  225. vec_max(vec_min(x, ((vector signed short) { \
  226. 235, 235, 235, 235, 235, 235, 235, 235 })), \
  227. ((vector signed short) { 16, 16, 16, 16, 16, 16, 16, 16 }))
  228. #define vec_packclp(x, y) \
  229. (vector unsigned char) \
  230. vec_packs((vector unsigned short) \
  231. vec_max(x, ((vector signed short) { 0 })), \
  232. (vector unsigned short) \
  233. vec_max(y, ((vector signed short) { 0 })))
  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. #endif /* HAVE_ALTIVEC */
  492. /* Ok currently the acceleration routine only supports
  493. * inputs of widths a multiple of 16
  494. * and heights a multiple 2
  495. *
  496. * So we just fall back to the C codes for this.
  497. */
  498. av_cold SwsFunc ff_yuv2rgb_init_ppc(SwsContext *c)
  499. {
  500. #if HAVE_ALTIVEC
  501. if (!(av_get_cpu_flags() & AV_CPU_FLAG_ALTIVEC))
  502. return NULL;
  503. /*
  504. * and this seems not to matter too much I tried a bunch of
  505. * videos with abnormal widths and MPlayer crashes elsewhere.
  506. * mplayer -vo x11 -rawvideo on:w=350:h=240 raw-350x240.eyuv
  507. * boom with X11 bad match.
  508. *
  509. */
  510. if ((c->srcW & 0xf) != 0)
  511. return NULL;
  512. switch (c->srcFormat) {
  513. case AV_PIX_FMT_YUV410P:
  514. case AV_PIX_FMT_YUV420P:
  515. /*case IMGFMT_CLPL: ??? */
  516. case AV_PIX_FMT_GRAY8:
  517. case AV_PIX_FMT_NV12:
  518. case AV_PIX_FMT_NV21:
  519. if ((c->srcH & 0x1) != 0)
  520. return NULL;
  521. switch (c->dstFormat) {
  522. case AV_PIX_FMT_RGB24:
  523. av_log(c, AV_LOG_WARNING, "ALTIVEC: Color Space RGB24\n");
  524. return altivec_yuv2_rgb24;
  525. case AV_PIX_FMT_BGR24:
  526. av_log(c, AV_LOG_WARNING, "ALTIVEC: Color Space BGR24\n");
  527. return altivec_yuv2_bgr24;
  528. case AV_PIX_FMT_ARGB:
  529. av_log(c, AV_LOG_WARNING, "ALTIVEC: Color Space ARGB\n");
  530. return altivec_yuv2_argb;
  531. case AV_PIX_FMT_ABGR:
  532. av_log(c, AV_LOG_WARNING, "ALTIVEC: Color Space ABGR\n");
  533. return altivec_yuv2_abgr;
  534. case AV_PIX_FMT_RGBA:
  535. av_log(c, AV_LOG_WARNING, "ALTIVEC: Color Space RGBA\n");
  536. return altivec_yuv2_rgba;
  537. case AV_PIX_FMT_BGRA:
  538. av_log(c, AV_LOG_WARNING, "ALTIVEC: Color Space BGRA\n");
  539. return altivec_yuv2_bgra;
  540. default: return NULL;
  541. }
  542. break;
  543. case AV_PIX_FMT_UYVY422:
  544. switch (c->dstFormat) {
  545. case AV_PIX_FMT_BGR32:
  546. av_log(c, AV_LOG_WARNING, "ALTIVEC: Color Space UYVY -> RGB32\n");
  547. return altivec_uyvy_rgb32;
  548. default: return NULL;
  549. }
  550. break;
  551. }
  552. #endif /* HAVE_ALTIVEC */
  553. return NULL;
  554. }
  555. av_cold void ff_yuv2rgb_init_tables_ppc(SwsContext *c,
  556. const int inv_table[4],
  557. int brightness,
  558. int contrast,
  559. int saturation)
  560. {
  561. #if HAVE_ALTIVEC
  562. union {
  563. DECLARE_ALIGNED(16, signed short, tmp)[8];
  564. vector signed short vec;
  565. } buf;
  566. if (!(av_get_cpu_flags() & AV_CPU_FLAG_ALTIVEC))
  567. return;
  568. buf.tmp[0] = ((0xffffLL) * contrast >> 8) >> 9; // cy
  569. buf.tmp[1] = -256 * brightness; // oy
  570. buf.tmp[2] = (inv_table[0] >> 3) * (contrast >> 16) * (saturation >> 16); // crv
  571. buf.tmp[3] = (inv_table[1] >> 3) * (contrast >> 16) * (saturation >> 16); // cbu
  572. buf.tmp[4] = -((inv_table[2] >> 1) * (contrast >> 16) * (saturation >> 16)); // cgu
  573. buf.tmp[5] = -((inv_table[3] >> 1) * (contrast >> 16) * (saturation >> 16)); // cgv
  574. c->CSHIFT = (vector unsigned short) vec_splat_u16(2);
  575. c->CY = vec_splat((vector signed short) buf.vec, 0);
  576. c->OY = vec_splat((vector signed short) buf.vec, 1);
  577. c->CRV = vec_splat((vector signed short) buf.vec, 2);
  578. c->CBU = vec_splat((vector signed short) buf.vec, 3);
  579. c->CGU = vec_splat((vector signed short) buf.vec, 4);
  580. c->CGV = vec_splat((vector signed short) buf.vec, 5);
  581. return;
  582. #endif /* HAVE_ALTIVEC */
  583. }
  584. #if HAVE_ALTIVEC
  585. static av_always_inline void yuv2packedX_altivec(SwsContext *c,
  586. const int16_t *lumFilter,
  587. const int16_t **lumSrc,
  588. int lumFilterSize,
  589. const int16_t *chrFilter,
  590. const int16_t **chrUSrc,
  591. const int16_t **chrVSrc,
  592. int chrFilterSize,
  593. const int16_t **alpSrc,
  594. uint8_t *dest,
  595. int dstW, int dstY,
  596. enum AVPixelFormat target)
  597. {
  598. int i, j;
  599. vector signed short X, X0, X1, Y0, U0, V0, Y1, U1, V1, U, V;
  600. vector signed short R0, G0, B0, R1, G1, B1;
  601. vector unsigned char R, G, B;
  602. vector unsigned char *out, *nout;
  603. vector signed short RND = vec_splat_s16(1 << 3);
  604. vector unsigned short SCL = vec_splat_u16(4);
  605. DECLARE_ALIGNED(16, unsigned int, scratch)[16];
  606. vector signed short *YCoeffs, *CCoeffs;
  607. YCoeffs = c->vYCoeffsBank + dstY * lumFilterSize;
  608. CCoeffs = c->vCCoeffsBank + dstY * chrFilterSize;
  609. out = (vector unsigned char *) dest;
  610. for (i = 0; i < dstW; i += 16) {
  611. Y0 = RND;
  612. Y1 = RND;
  613. /* extract 16 coeffs from lumSrc */
  614. for (j = 0; j < lumFilterSize; j++) {
  615. X0 = vec_ld(0, &lumSrc[j][i]);
  616. X1 = vec_ld(16, &lumSrc[j][i]);
  617. Y0 = vec_mradds(X0, YCoeffs[j], Y0);
  618. Y1 = vec_mradds(X1, YCoeffs[j], Y1);
  619. }
  620. U = RND;
  621. V = RND;
  622. /* extract 8 coeffs from U,V */
  623. for (j = 0; j < chrFilterSize; j++) {
  624. X = vec_ld(0, &chrUSrc[j][i / 2]);
  625. U = vec_mradds(X, CCoeffs[j], U);
  626. X = vec_ld(0, &chrVSrc[j][i / 2]);
  627. V = vec_mradds(X, CCoeffs[j], V);
  628. }
  629. /* scale and clip signals */
  630. Y0 = vec_sra(Y0, SCL);
  631. Y1 = vec_sra(Y1, SCL);
  632. U = vec_sra(U, SCL);
  633. V = vec_sra(V, SCL);
  634. Y0 = vec_clip_s16(Y0);
  635. Y1 = vec_clip_s16(Y1);
  636. U = vec_clip_s16(U);
  637. V = vec_clip_s16(V);
  638. /* now we have
  639. * Y0 = y0 y1 y2 y3 y4 y5 y6 y7 Y1 = y8 y9 y10 y11 y12 y13 y14 y15
  640. * U = u0 u1 u2 u3 u4 u5 u6 u7 V = v0 v1 v2 v3 v4 v5 v6 v7
  641. *
  642. * Y0 = y0 y1 y2 y3 y4 y5 y6 y7 Y1 = y8 y9 y10 y11 y12 y13 y14 y15
  643. * U0 = u0 u0 u1 u1 u2 u2 u3 u3 U1 = u4 u4 u5 u5 u6 u6 u7 u7
  644. * V0 = v0 v0 v1 v1 v2 v2 v3 v3 V1 = v4 v4 v5 v5 v6 v6 v7 v7
  645. */
  646. U0 = vec_mergeh(U, U);
  647. V0 = vec_mergeh(V, V);
  648. U1 = vec_mergel(U, U);
  649. V1 = vec_mergel(V, V);
  650. cvtyuvtoRGB(c, Y0, U0, V0, &R0, &G0, &B0);
  651. cvtyuvtoRGB(c, Y1, U1, V1, &R1, &G1, &B1);
  652. R = vec_packclp(R0, R1);
  653. G = vec_packclp(G0, G1);
  654. B = vec_packclp(B0, B1);
  655. switch (target) {
  656. case AV_PIX_FMT_ABGR:
  657. out_abgr(R, G, B, out);
  658. break;
  659. case AV_PIX_FMT_BGRA:
  660. out_bgra(R, G, B, out);
  661. break;
  662. case AV_PIX_FMT_RGBA:
  663. out_rgba(R, G, B, out);
  664. break;
  665. case AV_PIX_FMT_ARGB:
  666. out_argb(R, G, B, out);
  667. break;
  668. case AV_PIX_FMT_RGB24:
  669. out_rgb24(R, G, B, out);
  670. break;
  671. case AV_PIX_FMT_BGR24:
  672. out_bgr24(R, G, B, out);
  673. break;
  674. default:
  675. {
  676. /* If this is reached, the caller should have called yuv2packedXinC
  677. * instead. */
  678. static int printed_error_message;
  679. if (!printed_error_message) {
  680. av_log(c, AV_LOG_ERROR,
  681. "altivec_yuv2packedX doesn't support %s output\n",
  682. av_get_pix_fmt_name(c->dstFormat));
  683. printed_error_message = 1;
  684. }
  685. return;
  686. }
  687. }
  688. }
  689. if (i < dstW) {
  690. i -= 16;
  691. Y0 = RND;
  692. Y1 = RND;
  693. /* extract 16 coeffs from lumSrc */
  694. for (j = 0; j < lumFilterSize; j++) {
  695. X0 = vec_ld(0, &lumSrc[j][i]);
  696. X1 = vec_ld(16, &lumSrc[j][i]);
  697. Y0 = vec_mradds(X0, YCoeffs[j], Y0);
  698. Y1 = vec_mradds(X1, YCoeffs[j], Y1);
  699. }
  700. U = RND;
  701. V = RND;
  702. /* extract 8 coeffs from U,V */
  703. for (j = 0; j < chrFilterSize; j++) {
  704. X = vec_ld(0, &chrUSrc[j][i / 2]);
  705. U = vec_mradds(X, CCoeffs[j], U);
  706. X = vec_ld(0, &chrVSrc[j][i / 2]);
  707. V = vec_mradds(X, CCoeffs[j], V);
  708. }
  709. /* scale and clip signals */
  710. Y0 = vec_sra(Y0, SCL);
  711. Y1 = vec_sra(Y1, SCL);
  712. U = vec_sra(U, SCL);
  713. V = vec_sra(V, SCL);
  714. Y0 = vec_clip_s16(Y0);
  715. Y1 = vec_clip_s16(Y1);
  716. U = vec_clip_s16(U);
  717. V = vec_clip_s16(V);
  718. /* now we have
  719. * Y0 = y0 y1 y2 y3 y4 y5 y6 y7 Y1 = y8 y9 y10 y11 y12 y13 y14 y15
  720. * U = u0 u1 u2 u3 u4 u5 u6 u7 V = v0 v1 v2 v3 v4 v5 v6 v7
  721. *
  722. * Y0 = y0 y1 y2 y3 y4 y5 y6 y7 Y1 = y8 y9 y10 y11 y12 y13 y14 y15
  723. * U0 = u0 u0 u1 u1 u2 u2 u3 u3 U1 = u4 u4 u5 u5 u6 u6 u7 u7
  724. * V0 = v0 v0 v1 v1 v2 v2 v3 v3 V1 = v4 v4 v5 v5 v6 v6 v7 v7
  725. */
  726. U0 = vec_mergeh(U, U);
  727. V0 = vec_mergeh(V, V);
  728. U1 = vec_mergel(U, U);
  729. V1 = vec_mergel(V, V);
  730. cvtyuvtoRGB(c, Y0, U0, V0, &R0, &G0, &B0);
  731. cvtyuvtoRGB(c, Y1, U1, V1, &R1, &G1, &B1);
  732. R = vec_packclp(R0, R1);
  733. G = vec_packclp(G0, G1);
  734. B = vec_packclp(B0, B1);
  735. nout = (vector unsigned char *) scratch;
  736. switch (target) {
  737. case AV_PIX_FMT_ABGR:
  738. out_abgr(R, G, B, nout);
  739. break;
  740. case AV_PIX_FMT_BGRA:
  741. out_bgra(R, G, B, nout);
  742. break;
  743. case AV_PIX_FMT_RGBA:
  744. out_rgba(R, G, B, nout);
  745. break;
  746. case AV_PIX_FMT_ARGB:
  747. out_argb(R, G, B, nout);
  748. break;
  749. case AV_PIX_FMT_RGB24:
  750. out_rgb24(R, G, B, nout);
  751. break;
  752. case AV_PIX_FMT_BGR24:
  753. out_bgr24(R, G, B, nout);
  754. break;
  755. default:
  756. /* Unreachable, I think. */
  757. av_log(c, AV_LOG_ERROR,
  758. "altivec_yuv2packedX doesn't support %s output\n",
  759. av_get_pix_fmt_name(c->dstFormat));
  760. return;
  761. }
  762. memcpy(&((uint32_t *) dest)[i], scratch, (dstW - i) / 4);
  763. }
  764. }
  765. #define YUV2PACKEDX_WRAPPER(suffix, pixfmt) \
  766. void ff_yuv2 ## suffix ## _X_altivec(SwsContext *c, \
  767. const int16_t *lumFilter, \
  768. const int16_t **lumSrc, \
  769. int lumFilterSize, \
  770. const int16_t *chrFilter, \
  771. const int16_t **chrUSrc, \
  772. const int16_t **chrVSrc, \
  773. int chrFilterSize, \
  774. const int16_t **alpSrc, \
  775. uint8_t *dest, int dstW, int dstY) \
  776. { \
  777. yuv2packedX_altivec(c, lumFilter, lumSrc, lumFilterSize, \
  778. chrFilter, chrUSrc, chrVSrc, \
  779. chrFilterSize, alpSrc, \
  780. dest, dstW, dstY, pixfmt); \
  781. }
  782. YUV2PACKEDX_WRAPPER(abgr, AV_PIX_FMT_ABGR);
  783. YUV2PACKEDX_WRAPPER(bgra, AV_PIX_FMT_BGRA);
  784. YUV2PACKEDX_WRAPPER(argb, AV_PIX_FMT_ARGB);
  785. YUV2PACKEDX_WRAPPER(rgba, AV_PIX_FMT_RGBA);
  786. YUV2PACKEDX_WRAPPER(rgb24, AV_PIX_FMT_RGB24);
  787. YUV2PACKEDX_WRAPPER(bgr24, AV_PIX_FMT_BGR24);
  788. #endif /* HAVE_ALTIVEC */