You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

856 lines
38KB

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