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
37KB

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