yuv2rgb_altivec.c 37 KB

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