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.

936 lines
28KB

  1. /*
  2. marc.hoffman@analog.com March 8, 2004
  3. Altivec Acceleration for Color Space Conversion revision 0.2
  4. convert I420 YV12 to RGB in various formats,
  5. it rejects images that are not in 420 formats
  6. it rejects images that don't have widths of multiples of 16
  7. it rejects images that don't have heights of multiples of 2
  8. reject defers to C simulation codes.
  9. lots of optimizations to be done here
  10. 1. need to fix saturation code, I just couldn't get it to fly with packs and adds.
  11. so we currently use max min to clip
  12. 2. the inefficient use of chroma loading needs a bit of brushing up
  13. 3. analysis of pipeline stalls needs to be done, use shark to identify pipeline stalls
  14. MODIFIED to calculate coeffs from currently selected color space.
  15. MODIFIED core to be a macro which you spec the output format.
  16. ADDED UYVY conversion which is never called due to some thing in SWSCALE.
  17. CORRECTED algorithim selection to be strict on input formats.
  18. ADDED runtime detection of altivec.
  19. ADDED altivec_yuv2packedX vertical scl + RGB converter
  20. March 27,2004
  21. PERFORMANCE ANALYSIS
  22. The C version use 25% of the processor or ~250Mips for D1 video rawvideo used as test
  23. The ALTIVEC version uses 10% of the processor or ~100Mips for D1 video same sequence
  24. 720*480*30 ~10MPS
  25. so we have roughly 10clocks per pixel this is too high something has to be wrong.
  26. OPTIMIZED clip codes to utilize vec_max and vec_packs removing the need for vec_min.
  27. OPTIMIZED DST OUTPUT cache/dma controls. we are pretty much
  28. guaranteed to have the input video frame it was just decompressed so
  29. it probably resides in L1 caches. However we are creating the
  30. output video stream this needs to use the DSTST instruction to
  31. optimize for the cache. We couple this with the fact that we are
  32. not going to be visiting the input buffer again so we mark it Least
  33. Recently Used. This shaves 25% of the processor cycles off.
  34. Now MEMCPY is the largest mips consumer in the system, probably due
  35. to the inefficient X11 stuff.
  36. GL libraries seem to be very slow on this machine 1.33Ghz PB running
  37. Jaguar, this is not the case for my 1Ghz PB. I thought it might be
  38. a versioning issues, however i have libGL.1.2.dylib for both
  39. machines. ((We need to figure this out now))
  40. GL2 libraries work now with patch for RGB32
  41. NOTE quartz vo driver ARGB32_to_RGB24 consumes 30% of the processor
  42. Integrated luma prescaling adjustment for saturation/contrast/brightness adjustment.
  43. */
  44. #include <stdio.h>
  45. #include <stdlib.h>
  46. #include <string.h>
  47. #include <inttypes.h>
  48. #include <assert.h>
  49. #include "config.h"
  50. #include "rgb2rgb.h"
  51. #include "swscale.h"
  52. #include "swscale_internal.h"
  53. #include "../mangle.h"
  54. #include "../libvo/img_format.h" //FIXME try to reduce dependency of such stuff
  55. #undef PROFILE_THE_BEAST
  56. #undef INC_SCALING
  57. typedef unsigned char ubyte;
  58. typedef signed char sbyte;
  59. /* RGB interleaver, 16 planar pels 8-bit samples per channel in
  60. homogeneous vector registers x0,x1,x2 are interleaved with the
  61. following technique:
  62. o0 = vec_mergeh (x0,x1);
  63. o1 = vec_perm (o0, x2, perm_rgb_0);
  64. o2 = vec_perm (o0, x2, perm_rgb_1);
  65. o3 = vec_mergel (x0,x1);
  66. o4 = vec_perm (o3,o2,perm_rgb_2);
  67. o5 = vec_perm (o3,o2,perm_rgb_3);
  68. perm_rgb_0: o0(RG).h v1(B) --> o1*
  69. 0 1 2 3 4
  70. rgbr|gbrg|brgb|rgbr
  71. 0010 0100 1001 0010
  72. 0102 3145 2673 894A
  73. perm_rgb_1: o0(RG).h v1(B) --> o2
  74. 0 1 2 3 4
  75. gbrg|brgb|bbbb|bbbb
  76. 0100 1001 1111 1111
  77. B5CD 6EF7 89AB CDEF
  78. perm_rgb_2: o3(RG).l o2(rgbB.l) --> o4*
  79. 0 1 2 3 4
  80. gbrg|brgb|rgbr|gbrg
  81. 1111 1111 0010 0100
  82. 89AB CDEF 0182 3945
  83. perm_rgb_2: o3(RG).l o2(rgbB.l) ---> o5*
  84. 0 1 2 3 4
  85. brgb|rgbr|gbrg|brgb
  86. 1001 0010 0100 1001
  87. a67b 89cA BdCD eEFf
  88. */
  89. static
  90. const vector unsigned char
  91. perm_rgb_0 = (const vector unsigned char)AVV(0x00,0x01,0x10,0x02,0x03,0x11,0x04,0x05,
  92. 0x12,0x06,0x07,0x13,0x08,0x09,0x14,0x0a),
  93. perm_rgb_1 = (const vector unsigned char)AVV(0x0b,0x15,0x0c,0x0d,0x16,0x0e,0x0f,0x17,
  94. 0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f),
  95. perm_rgb_2 = (const vector unsigned char)AVV(0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,
  96. 0x00,0x01,0x18,0x02,0x03,0x19,0x04,0x05),
  97. perm_rgb_3 = (const vector unsigned char)AVV(0x1a,0x06,0x07,0x1b,0x08,0x09,0x1c,0x0a,
  98. 0x0b,0x1d,0x0c,0x0d,0x1e,0x0e,0x0f,0x1f);
  99. #define vec_merge3(x2,x1,x0,y0,y1,y2) \
  100. do { \
  101. typeof(x0) o0,o2,o3; \
  102. o0 = vec_mergeh (x0,x1); \
  103. y0 = vec_perm (o0, x2, perm_rgb_0);\
  104. o2 = vec_perm (o0, x2, perm_rgb_1);\
  105. o3 = vec_mergel (x0,x1); \
  106. y1 = vec_perm (o3,o2,perm_rgb_2); \
  107. y2 = vec_perm (o3,o2,perm_rgb_3); \
  108. } while(0)
  109. #define vec_mstrgb24(x0,x1,x2,ptr) \
  110. do { \
  111. typeof(x0) _0,_1,_2; \
  112. vec_merge3 (x0,x1,x2,_0,_1,_2); \
  113. vec_st (_0, 0, ptr++); \
  114. vec_st (_1, 0, ptr++); \
  115. vec_st (_2, 0, ptr++); \
  116. } while (0);
  117. #define vec_mstbgr24(x0,x1,x2,ptr) \
  118. do { \
  119. typeof(x0) _0,_1,_2; \
  120. vec_merge3 (x2,x1,x0,_0,_1,_2); \
  121. vec_st (_0, 0, ptr++); \
  122. vec_st (_1, 0, ptr++); \
  123. vec_st (_2, 0, ptr++); \
  124. } while (0);
  125. /* pack the pixels in rgb0 format
  126. msb R
  127. lsb 0
  128. */
  129. #define vec_mstrgb32(T,x0,x1,x2,x3,ptr) \
  130. do { \
  131. T _0,_1,_2,_3; \
  132. _0 = vec_mergeh (x0,x1); \
  133. _1 = vec_mergeh (x2,x3); \
  134. _2 = (T)vec_mergeh ((vector unsigned short)_0,(vector unsigned short)_1); \
  135. _3 = (T)vec_mergel ((vector unsigned short)_0,(vector unsigned short)_1); \
  136. vec_st (_2, 0*16, (T *)ptr); \
  137. vec_st (_3, 1*16, (T *)ptr); \
  138. _0 = vec_mergel (x0,x1); \
  139. _1 = vec_mergel (x2,x3); \
  140. _2 = (T)vec_mergeh ((vector unsigned short)_0,(vector unsigned short)_1); \
  141. _3 = (T)vec_mergel ((vector unsigned short)_0,(vector unsigned short)_1); \
  142. vec_st (_2, 2*16, (T *)ptr); \
  143. vec_st (_3, 3*16, (T *)ptr); \
  144. ptr += 4; \
  145. } while (0);
  146. /*
  147. | 1 0 1.4021 | | Y |
  148. | 1 -0.3441 -0.7142 |x| Cb|
  149. | 1 1.7718 0 | | Cr|
  150. Y: [-128 127]
  151. Cb/Cr : [-128 127]
  152. typical yuv conversion work on Y: 0-255 this version has been optimized for jpeg decode.
  153. */
  154. #define vec_unh(x) \
  155. (vector signed short) \
  156. vec_perm(x,(typeof(x))AVV(0),\
  157. (vector unsigned char)AVV(0x10,0x00,0x10,0x01,0x10,0x02,0x10,0x03,\
  158. 0x10,0x04,0x10,0x05,0x10,0x06,0x10,0x07))
  159. #define vec_unl(x) \
  160. (vector signed short) \
  161. vec_perm(x,(typeof(x))AVV(0),\
  162. (vector unsigned char)AVV(0x10,0x08,0x10,0x09,0x10,0x0A,0x10,0x0B,\
  163. 0x10,0x0C,0x10,0x0D,0x10,0x0E,0x10,0x0F))
  164. #define vec_clip(x) \
  165. vec_max (vec_min (x, (typeof(x))AVV(235)), (typeof(x))AVV(16))
  166. #define vec_packclp_a(x,y) \
  167. (vector unsigned char)vec_pack (vec_clip (x), vec_clip (y))
  168. #define vec_packclp(x,y) \
  169. (vector unsigned char)vec_packs \
  170. ((vector unsigned short)vec_max (x,(vector signed short) AVV(0)), \
  171. (vector unsigned short)vec_max (y,(vector signed short) AVV(0)))
  172. //#define out_pixels(a,b,c,ptr) vec_mstrgb32(typeof(a),((typeof (a))AVV(0)),a,a,a,ptr)
  173. static inline void cvtyuvtoRGB (SwsContext *c,
  174. vector signed short Y, vector signed short U, vector signed short V,
  175. vector signed short *R, vector signed short *G, vector signed short *B)
  176. {
  177. vector signed short vx,ux,uvx;
  178. Y = vec_mradds (Y, c->CY, c->OY);
  179. U = vec_sub (U,(vector signed short)
  180. vec_splat((vector signed short)AVV(128),0));
  181. V = vec_sub (V,(vector signed short)
  182. vec_splat((vector signed short)AVV(128),0));
  183. // ux = (CBU*(u<<c->CSHIFT)+0x4000)>>15;
  184. ux = vec_sl (U, c->CSHIFT);
  185. *B = vec_mradds (ux, c->CBU, Y);
  186. // vx = (CRV*(v<<c->CSHIFT)+0x4000)>>15;
  187. vx = vec_sl (V, c->CSHIFT);
  188. *R = vec_mradds (vx, c->CRV, Y);
  189. // uvx = ((CGU*u) + (CGV*v))>>15;
  190. uvx = vec_mradds (U, c->CGU, Y);
  191. *G = vec_mradds (V, c->CGV, uvx);
  192. }
  193. /*
  194. ------------------------------------------------------------------------------
  195. CS converters
  196. ------------------------------------------------------------------------------
  197. */
  198. #define DEFCSP420_CVT(name,out_pixels) \
  199. static int altivec_##name (SwsContext *c, \
  200. unsigned char **in, int *instrides, \
  201. int srcSliceY, int srcSliceH, \
  202. unsigned char **oplanes, int *outstrides) \
  203. { \
  204. int w = c->srcW; \
  205. int h = srcSliceH; \
  206. int i,j; \
  207. int instrides_scl[3]; \
  208. vector unsigned char y0,y1; \
  209. \
  210. vector signed char u,v; \
  211. \
  212. vector signed short Y0,Y1,Y2,Y3; \
  213. vector signed short U,V; \
  214. vector signed short vx,ux,uvx; \
  215. vector signed short vx0,ux0,uvx0; \
  216. vector signed short vx1,ux1,uvx1; \
  217. vector signed short R0,G0,B0; \
  218. vector signed short R1,G1,B1; \
  219. vector unsigned char R,G,B; \
  220. \
  221. vector unsigned char *uivP, *vivP; \
  222. vector unsigned char align_perm; \
  223. \
  224. vector signed short \
  225. lCY = c->CY, \
  226. lOY = c->OY, \
  227. lCRV = c->CRV, \
  228. lCBU = c->CBU, \
  229. lCGU = c->CGU, \
  230. lCGV = c->CGV; \
  231. \
  232. vector unsigned short lCSHIFT = c->CSHIFT; \
  233. \
  234. ubyte *y1i = in[0]; \
  235. ubyte *y2i = in[0]+w; \
  236. ubyte *ui = in[1]; \
  237. ubyte *vi = in[2]; \
  238. \
  239. vector unsigned char *oute \
  240. = (vector unsigned char *) \
  241. (oplanes[0]+srcSliceY*outstrides[0]); \
  242. vector unsigned char *outo \
  243. = (vector unsigned char *) \
  244. (oplanes[0]+srcSliceY*outstrides[0]+outstrides[0]); \
  245. \
  246. \
  247. instrides_scl[0] = instrides[0]; \
  248. instrides_scl[1] = instrides[1]-w/2; /* the loop moves ui by w/2 */ \
  249. instrides_scl[2] = instrides[2]-w/2; /* the loop moves vi by w/2 */ \
  250. \
  251. \
  252. for (i=0;i<h/2;i++) { \
  253. vec_dstst (outo, (0x02000002|(((w*3+32)/32)<<16)), 0); \
  254. vec_dstst (oute, (0x02000002|(((w*3+32)/32)<<16)), 1); \
  255. \
  256. for (j=0;j<w/16;j++) { \
  257. \
  258. y0 = vec_ldl (0,y1i); \
  259. y1 = vec_ldl (0,y2i); \
  260. uivP = (vector unsigned char *)ui; \
  261. vivP = (vector unsigned char *)vi; \
  262. \
  263. align_perm = vec_lvsl (0, ui); \
  264. u = (vector signed char)vec_perm (uivP[0], uivP[1], align_perm); \
  265. \
  266. align_perm = vec_lvsl (0, vi); \
  267. v = (vector signed char)vec_perm (vivP[0], vivP[1], align_perm); \
  268. \
  269. u = (vector signed char) \
  270. vec_sub (u,(vector signed char) \
  271. vec_splat((vector signed char)AVV(128),0));\
  272. v = (vector signed char) \
  273. vec_sub (v,(vector signed char) \
  274. vec_splat((vector signed char)AVV(128),0));\
  275. \
  276. U = vec_unpackh (u); \
  277. V = vec_unpackh (v); \
  278. \
  279. \
  280. Y0 = vec_unh (y0); \
  281. Y1 = vec_unl (y0); \
  282. Y2 = vec_unh (y1); \
  283. Y3 = vec_unl (y1); \
  284. \
  285. Y0 = vec_mradds (Y0, lCY, lOY); \
  286. Y1 = vec_mradds (Y1, lCY, lOY); \
  287. Y2 = vec_mradds (Y2, lCY, lOY); \
  288. Y3 = vec_mradds (Y3, lCY, lOY); \
  289. \
  290. /* ux = (CBU*(u<<CSHIFT)+0x4000)>>15 */ \
  291. ux = vec_sl (U, lCSHIFT); \
  292. ux = vec_mradds (ux, lCBU, (vector signed short)AVV(0)); \
  293. ux0 = vec_mergeh (ux,ux); \
  294. ux1 = vec_mergel (ux,ux); \
  295. \
  296. /* vx = (CRV*(v<<CSHIFT)+0x4000)>>15; */ \
  297. vx = vec_sl (V, lCSHIFT); \
  298. vx = vec_mradds (vx, lCRV, (vector signed short)AVV(0)); \
  299. vx0 = vec_mergeh (vx,vx); \
  300. vx1 = vec_mergel (vx,vx); \
  301. \
  302. /* uvx = ((CGU*u) + (CGV*v))>>15 */ \
  303. uvx = vec_mradds (U, lCGU, (vector signed short)AVV(0)); \
  304. uvx = vec_mradds (V, lCGV, uvx); \
  305. uvx0 = vec_mergeh (uvx,uvx); \
  306. uvx1 = vec_mergel (uvx,uvx); \
  307. \
  308. R0 = vec_add (Y0,vx0); \
  309. G0 = vec_add (Y0,uvx0); \
  310. B0 = vec_add (Y0,ux0); \
  311. R1 = vec_add (Y1,vx1); \
  312. G1 = vec_add (Y1,uvx1); \
  313. B1 = vec_add (Y1,ux1); \
  314. \
  315. R = vec_packclp (R0,R1); \
  316. G = vec_packclp (G0,G1); \
  317. B = vec_packclp (B0,B1); \
  318. \
  319. out_pixels(R,G,B,oute); \
  320. \
  321. R0 = vec_add (Y2,vx0); \
  322. G0 = vec_add (Y2,uvx0); \
  323. B0 = vec_add (Y2,ux0); \
  324. R1 = vec_add (Y3,vx1); \
  325. G1 = vec_add (Y3,uvx1); \
  326. B1 = vec_add (Y3,ux1); \
  327. R = vec_packclp (R0,R1); \
  328. G = vec_packclp (G0,G1); \
  329. B = vec_packclp (B0,B1); \
  330. \
  331. \
  332. out_pixels(R,G,B,outo); \
  333. \
  334. y1i += 16; \
  335. y2i += 16; \
  336. ui += 8; \
  337. vi += 8; \
  338. \
  339. } \
  340. \
  341. outo += (outstrides[0])>>4; \
  342. oute += (outstrides[0])>>4; \
  343. \
  344. ui += instrides_scl[1]; \
  345. vi += instrides_scl[2]; \
  346. y1i += instrides_scl[0]; \
  347. y2i += instrides_scl[0]; \
  348. } \
  349. return srcSliceH; \
  350. }
  351. #define out_abgr(a,b,c,ptr) vec_mstrgb32(typeof(a),((typeof (a))AVV(0)),c,b,a,ptr)
  352. #define out_bgra(a,b,c,ptr) vec_mstrgb32(typeof(a),c,b,a,((typeof (a))AVV(0)),ptr)
  353. #define out_rgba(a,b,c,ptr) vec_mstrgb32(typeof(a),a,b,c,((typeof (a))AVV(0)),ptr)
  354. #define out_argb(a,b,c,ptr) vec_mstrgb32(typeof(a),((typeof (a))AVV(0)),a,b,c,ptr)
  355. #define out_rgb24(a,b,c,ptr) vec_mstrgb24(a,b,c,ptr)
  356. #define out_bgr24(a,b,c,ptr) vec_mstbgr24(c,b,a,ptr)
  357. DEFCSP420_CVT (yuv2_abgr32, out_abgr)
  358. #if 1
  359. DEFCSP420_CVT (yuv2_bgra32, out_argb)
  360. #else
  361. static int altivec_yuv2_bgra32 (SwsContext *c,
  362. unsigned char **in, int *instrides,
  363. int srcSliceY, int srcSliceH,
  364. unsigned char **oplanes, int *outstrides)
  365. {
  366. int w = c->srcW;
  367. int h = srcSliceH;
  368. int i,j;
  369. int instrides_scl[3];
  370. vector unsigned char y0,y1;
  371. vector signed char u,v;
  372. vector signed short Y0,Y1,Y2,Y3;
  373. vector signed short U,V;
  374. vector signed short vx,ux,uvx;
  375. vector signed short vx0,ux0,uvx0;
  376. vector signed short vx1,ux1,uvx1;
  377. vector signed short R0,G0,B0;
  378. vector signed short R1,G1,B1;
  379. vector unsigned char R,G,B;
  380. vector unsigned char *uivP, *vivP;
  381. vector unsigned char align_perm;
  382. vector signed short
  383. lCY = c->CY,
  384. lOY = c->OY,
  385. lCRV = c->CRV,
  386. lCBU = c->CBU,
  387. lCGU = c->CGU,
  388. lCGV = c->CGV;
  389. vector unsigned short lCSHIFT = c->CSHIFT;
  390. ubyte *y1i = in[0];
  391. ubyte *y2i = in[0]+w;
  392. ubyte *ui = in[1];
  393. ubyte *vi = in[2];
  394. vector unsigned char *oute
  395. = (vector unsigned char *)
  396. (oplanes[0]+srcSliceY*outstrides[0]);
  397. vector unsigned char *outo
  398. = (vector unsigned char *)
  399. (oplanes[0]+srcSliceY*outstrides[0]+outstrides[0]);
  400. instrides_scl[0] = instrides[0];
  401. instrides_scl[1] = instrides[1]-w/2; /* the loop moves ui by w/2 */
  402. instrides_scl[2] = instrides[2]-w/2; /* the loop moves vi by w/2 */
  403. for (i=0;i<h/2;i++) {
  404. vec_dstst (outo, (0x02000002|(((w*3+32)/32)<<16)), 0);
  405. vec_dstst (oute, (0x02000002|(((w*3+32)/32)<<16)), 1);
  406. for (j=0;j<w/16;j++) {
  407. y0 = vec_ldl (0,y1i);
  408. y1 = vec_ldl (0,y2i);
  409. uivP = (vector unsigned char *)ui;
  410. vivP = (vector unsigned char *)vi;
  411. align_perm = vec_lvsl (0, ui);
  412. u = (vector signed char)vec_perm (uivP[0], uivP[1], align_perm);
  413. align_perm = vec_lvsl (0, vi);
  414. v = (vector signed char)vec_perm (vivP[0], vivP[1], align_perm);
  415. u = (vector signed char)
  416. vec_sub (u,(vector signed char)
  417. vec_splat((vector signed char)AVV(128),0));
  418. v = (vector signed char)
  419. vec_sub (v, (vector signed char)
  420. vec_splat((vector signed char)AVV(128),0));
  421. U = vec_unpackh (u);
  422. V = vec_unpackh (v);
  423. Y0 = vec_unh (y0);
  424. Y1 = vec_unl (y0);
  425. Y2 = vec_unh (y1);
  426. Y3 = vec_unl (y1);
  427. Y0 = vec_mradds (Y0, lCY, lOY);
  428. Y1 = vec_mradds (Y1, lCY, lOY);
  429. Y2 = vec_mradds (Y2, lCY, lOY);
  430. Y3 = vec_mradds (Y3, lCY, lOY);
  431. /* ux = (CBU*(u<<CSHIFT)+0x4000)>>15 */
  432. ux = vec_sl (U, lCSHIFT);
  433. ux = vec_mradds (ux, lCBU, (vector signed short)AVV(0));
  434. ux0 = vec_mergeh (ux,ux);
  435. ux1 = vec_mergel (ux,ux);
  436. /* vx = (CRV*(v<<CSHIFT)+0x4000)>>15; */
  437. vx = vec_sl (V, lCSHIFT);
  438. vx = vec_mradds (vx, lCRV, (vector signed short)AVV(0));
  439. vx0 = vec_mergeh (vx,vx);
  440. vx1 = vec_mergel (vx,vx);
  441. /* uvx = ((CGU*u) + (CGV*v))>>15 */
  442. uvx = vec_mradds (U, lCGU, (vector signed short)AVV(0));
  443. uvx = vec_mradds (V, lCGV, uvx);
  444. uvx0 = vec_mergeh (uvx,uvx);
  445. uvx1 = vec_mergel (uvx,uvx);
  446. R0 = vec_add (Y0,vx0);
  447. G0 = vec_add (Y0,uvx0);
  448. B0 = vec_add (Y0,ux0);
  449. R1 = vec_add (Y1,vx1);
  450. G1 = vec_add (Y1,uvx1);
  451. B1 = vec_add (Y1,ux1);
  452. R = vec_packclp (R0,R1);
  453. G = vec_packclp (G0,G1);
  454. B = vec_packclp (B0,B1);
  455. out_argb(R,G,B,oute);
  456. R0 = vec_add (Y2,vx0);
  457. G0 = vec_add (Y2,uvx0);
  458. B0 = vec_add (Y2,ux0);
  459. R1 = vec_add (Y3,vx1);
  460. G1 = vec_add (Y3,uvx1);
  461. B1 = vec_add (Y3,ux1);
  462. R = vec_packclp (R0,R1);
  463. G = vec_packclp (G0,G1);
  464. B = vec_packclp (B0,B1);
  465. out_argb(R,G,B,outo);
  466. y1i += 16;
  467. y2i += 16;
  468. ui += 8;
  469. vi += 8;
  470. }
  471. outo += (outstrides[0])>>4;
  472. oute += (outstrides[0])>>4;
  473. ui += instrides_scl[1];
  474. vi += instrides_scl[2];
  475. y1i += instrides_scl[0];
  476. y2i += instrides_scl[0];
  477. }
  478. return srcSliceH;
  479. }
  480. #endif
  481. DEFCSP420_CVT (yuv2_rgba32, out_rgba)
  482. DEFCSP420_CVT (yuv2_argb32, out_argb)
  483. DEFCSP420_CVT (yuv2_rgb24, out_rgb24)
  484. DEFCSP420_CVT (yuv2_bgr24, out_bgr24)
  485. // uyvy|uyvy|uyvy|uyvy
  486. // 0123 4567 89ab cdef
  487. static
  488. const vector unsigned char
  489. demux_u = (const vector unsigned char)AVV(0x10,0x00,0x10,0x00,
  490. 0x10,0x04,0x10,0x04,
  491. 0x10,0x08,0x10,0x08,
  492. 0x10,0x0c,0x10,0x0c),
  493. demux_v = (const vector unsigned char)AVV(0x10,0x02,0x10,0x02,
  494. 0x10,0x06,0x10,0x06,
  495. 0x10,0x0A,0x10,0x0A,
  496. 0x10,0x0E,0x10,0x0E),
  497. demux_y = (const vector unsigned char)AVV(0x10,0x01,0x10,0x03,
  498. 0x10,0x05,0x10,0x07,
  499. 0x10,0x09,0x10,0x0B,
  500. 0x10,0x0D,0x10,0x0F);
  501. /*
  502. this is so I can play live CCIR raw video
  503. */
  504. static int altivec_uyvy_rgb32 (SwsContext *c,
  505. unsigned char **in, int *instrides,
  506. int srcSliceY, int srcSliceH,
  507. unsigned char **oplanes, int *outstrides)
  508. {
  509. int w = c->srcW;
  510. int h = srcSliceH;
  511. int i,j;
  512. vector unsigned char uyvy;
  513. vector signed short Y,U,V;
  514. vector signed short vx,ux,uvx;
  515. vector signed short R0,G0,B0,R1,G1,B1;
  516. vector unsigned char R,G,B;
  517. vector unsigned char *out;
  518. ubyte *img;
  519. img = in[0];
  520. out = (vector unsigned char *)(oplanes[0]+srcSliceY*outstrides[0]);
  521. for (i=0;i<h;i++) {
  522. for (j=0;j<w/16;j++) {
  523. uyvy = vec_ld (0, img);
  524. U = (vector signed short)
  525. vec_perm (uyvy, (vector unsigned char)AVV(0), demux_u);
  526. V = (vector signed short)
  527. vec_perm (uyvy, (vector unsigned char)AVV(0), demux_v);
  528. Y = (vector signed short)
  529. vec_perm (uyvy, (vector unsigned char)AVV(0), demux_y);
  530. cvtyuvtoRGB (c, Y,U,V,&R0,&G0,&B0);
  531. uyvy = vec_ld (16, img);
  532. U = (vector signed short)
  533. vec_perm (uyvy, (vector unsigned char)AVV(0), demux_u);
  534. V = (vector signed short)
  535. vec_perm (uyvy, (vector unsigned char)AVV(0), demux_v);
  536. Y = (vector signed short)
  537. vec_perm (uyvy, (vector unsigned char)AVV(0), demux_y);
  538. cvtyuvtoRGB (c, Y,U,V,&R1,&G1,&B1);
  539. R = vec_packclp (R0,R1);
  540. G = vec_packclp (G0,G1);
  541. B = vec_packclp (B0,B1);
  542. // vec_mstbgr24 (R,G,B, out);
  543. out_rgba (R,G,B,out);
  544. img += 32;
  545. }
  546. }
  547. return srcSliceH;
  548. }
  549. /* Ok currently the acceleration routine only supports
  550. inputs of widths a multiple of 16
  551. and heights a multiple 2
  552. So we just fall back to the C codes for this.
  553. */
  554. SwsFunc yuv2rgb_init_altivec (SwsContext *c)
  555. {
  556. if (!(c->flags & SWS_CPU_CAPS_ALTIVEC))
  557. return NULL;
  558. /*
  559. and this seems not to matter too much I tried a bunch of
  560. videos with abnormal widths and mplayer crashes else where.
  561. mplayer -vo x11 -rawvideo on:w=350:h=240 raw-350x240.eyuv
  562. boom with X11 bad match.
  563. */
  564. if ((c->srcW & 0xf) != 0) return NULL;
  565. switch (c->srcFormat) {
  566. case IMGFMT_YVU9:
  567. case IMGFMT_IF09:
  568. case IMGFMT_YV12:
  569. case IMGFMT_I420:
  570. case IMGFMT_IYUV:
  571. case IMGFMT_CLPL:
  572. case IMGFMT_Y800:
  573. case IMGFMT_Y8:
  574. case IMGFMT_NV12:
  575. case IMGFMT_NV21:
  576. if ((c->srcH & 0x1) != 0)
  577. return NULL;
  578. switch(c->dstFormat){
  579. case IMGFMT_RGB24:
  580. MSG_WARN("ALTIVEC: Color Space RGB24\n");
  581. return altivec_yuv2_rgb24;
  582. case IMGFMT_BGR24:
  583. MSG_WARN("ALTIVEC: Color Space BGR24\n");
  584. return altivec_yuv2_bgr24;
  585. case IMGFMT_RGB32:
  586. MSG_WARN("ALTIVEC: Color Space ARGB32\n");
  587. return altivec_yuv2_argb32;
  588. case IMGFMT_BGR32:
  589. MSG_WARN("ALTIVEC: Color Space BGRA32\n");
  590. // return profile_altivec_bgra32;
  591. return altivec_yuv2_bgra32;
  592. default: return NULL;
  593. }
  594. break;
  595. case IMGFMT_UYVY:
  596. switch(c->dstFormat){
  597. case IMGFMT_RGB32:
  598. MSG_WARN("ALTIVEC: Color Space UYVY -> RGB32\n");
  599. return altivec_uyvy_rgb32;
  600. default: return NULL;
  601. }
  602. break;
  603. }
  604. return NULL;
  605. }
  606. static uint16_t roundToInt16(int64_t f){
  607. int r= (f + (1<<15))>>16;
  608. if(r<-0x7FFF) return 0x8000;
  609. else if(r> 0x7FFF) return 0x7FFF;
  610. else return r;
  611. }
  612. void yuv2rgb_altivec_init_tables (SwsContext *c, const int inv_table[4],int brightness,int contrast, int saturation)
  613. {
  614. union {
  615. signed short tmp[8] __attribute__ ((aligned(16)));
  616. vector signed short vec;
  617. } buf;
  618. buf.tmp[0] = ( (0xffffLL) * contrast>>8 )>>9; //cy
  619. buf.tmp[1] = -256*brightness; //oy
  620. buf.tmp[2] = (inv_table[0]>>3) *(contrast>>16)*(saturation>>16); //crv
  621. buf.tmp[3] = (inv_table[1]>>3) *(contrast>>16)*(saturation>>16); //cbu
  622. buf.tmp[4] = -((inv_table[2]>>1)*(contrast>>16)*(saturation>>16)); //cgu
  623. buf.tmp[5] = -((inv_table[3]>>1)*(contrast>>16)*(saturation>>16)); //cgv
  624. c->CSHIFT = (vector unsigned short)vec_splat((vector unsigned short)AVV(2),0);
  625. c->CY = vec_splat ((vector signed short)buf.vec, 0);
  626. c->OY = vec_splat ((vector signed short)buf.vec, 1);
  627. c->CRV = vec_splat ((vector signed short)buf.vec, 2);
  628. c->CBU = vec_splat ((vector signed short)buf.vec, 3);
  629. c->CGU = vec_splat ((vector signed short)buf.vec, 4);
  630. c->CGV = vec_splat ((vector signed short)buf.vec, 5);
  631. #if 0
  632. {
  633. int i;
  634. char *v[6]={"cy","oy","crv","cbu","cgu","cgv"};
  635. for (i=0; i<6;i++)
  636. printf("%s %d ", v[i],buf.tmp[i] );
  637. printf("\n");
  638. }
  639. #endif
  640. return;
  641. }
  642. void
  643. altivec_yuv2packedX (SwsContext *c,
  644. int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
  645. int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
  646. uint8_t *dest, int dstW, int dstY)
  647. {
  648. int i,j;
  649. short tmp __attribute__((aligned (16)));
  650. int16_t *p;
  651. short *f;
  652. vector signed short X,X0,X1,Y0,U0,V0,Y1,U1,V1,U,V;
  653. vector signed short R0,G0,B0,R1,G1,B1;
  654. vector unsigned char R,G,B,pels[3];
  655. vector unsigned char *out,*nout;
  656. vector signed short RND = vec_splat((vector signed short)AVV(1<<3),0);
  657. vector unsigned short SCL = vec_splat((vector unsigned short)AVV(4),0);
  658. unsigned long scratch[16] __attribute__ ((aligned (16)));
  659. vector signed short *vYCoeffsBank, *vCCoeffsBank;
  660. vector signed short *YCoeffs, *CCoeffs;
  661. vYCoeffsBank = malloc (sizeof (vector signed short)*lumFilterSize*dstW);
  662. vCCoeffsBank = malloc (sizeof (vector signed short)*chrFilterSize*dstW);
  663. for (i=0;i<lumFilterSize*dstW;i++) {
  664. tmp = c->vLumFilter[i];
  665. p = &vYCoeffsBank[i];
  666. for (j=0;j<8;j++)
  667. p[j] = tmp;
  668. }
  669. for (i=0;i<chrFilterSize*dstW;i++) {
  670. tmp = c->vChrFilter[i];
  671. p = &vCCoeffsBank[i];
  672. for (j=0;j<8;j++)
  673. p[j] = tmp;
  674. }
  675. YCoeffs = vYCoeffsBank+dstY*lumFilterSize;
  676. CCoeffs = vCCoeffsBank+dstY*chrFilterSize;
  677. out = (vector unsigned char *)dest;
  678. for(i=0; i<dstW; i+=16){
  679. Y0 = RND;
  680. Y1 = RND;
  681. /* extract 16 coeffs from lumSrc */
  682. for(j=0; j<lumFilterSize; j++) {
  683. X0 = vec_ld (0, &lumSrc[j][i]);
  684. X1 = vec_ld (16, &lumSrc[j][i]);
  685. Y0 = vec_mradds (X0, YCoeffs[j], Y0);
  686. Y1 = vec_mradds (X1, YCoeffs[j], Y1);
  687. }
  688. U = RND;
  689. V = RND;
  690. /* extract 8 coeffs from U,V */
  691. for(j=0; j<chrFilterSize; j++) {
  692. X = vec_ld (0, &chrSrc[j][i/2]);
  693. U = vec_mradds (X, CCoeffs[j], U);
  694. X = vec_ld (0, &chrSrc[j][i/2+2048]);
  695. V = vec_mradds (X, CCoeffs[j], V);
  696. }
  697. /* scale and clip signals */
  698. Y0 = vec_sra (Y0, SCL);
  699. Y1 = vec_sra (Y1, SCL);
  700. U = vec_sra (U, SCL);
  701. V = vec_sra (V, SCL);
  702. Y0 = vec_clip (Y0);
  703. Y1 = vec_clip (Y1);
  704. U = vec_clip (U);
  705. V = vec_clip (V);
  706. /* now we have
  707. Y0= y0 y1 y2 y3 y4 y5 y6 y7 Y1= y8 y9 y10 y11 y12 y13 y14 y15
  708. U= u0 u1 u2 u3 u4 u5 u6 u7 V= v0 v1 v2 v3 v4 v5 v6 v7
  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. out_rgba (R,G,B,out);
  723. }
  724. if (i < dstW) {
  725. i -= 16;
  726. Y0 = RND;
  727. Y1 = RND;
  728. /* extract 16 coeffs from lumSrc */
  729. for(j=0; j<lumFilterSize; j++) {
  730. X0 = vec_ld (0, &lumSrc[j][i]);
  731. X1 = vec_ld (16, &lumSrc[j][i]);
  732. Y0 = vec_mradds (X0, YCoeffs[j], Y0);
  733. Y1 = vec_mradds (X1, YCoeffs[j], Y1);
  734. }
  735. U = RND;
  736. V = RND;
  737. /* extract 8 coeffs from U,V */
  738. for(j=0; j<chrFilterSize; j++) {
  739. X = vec_ld (0, &chrSrc[j][i/2]);
  740. U = vec_mradds (X, CCoeffs[j], U);
  741. X = vec_ld (0, &chrSrc[j][i/2+2048]);
  742. V = vec_mradds (X, CCoeffs[j], V);
  743. }
  744. /* scale and clip signals */
  745. Y0 = vec_sra (Y0, SCL);
  746. Y1 = vec_sra (Y1, SCL);
  747. U = vec_sra (U, SCL);
  748. V = vec_sra (V, SCL);
  749. Y0 = vec_clip (Y0);
  750. Y1 = vec_clip (Y1);
  751. U = vec_clip (U);
  752. V = vec_clip (V);
  753. /* now we have
  754. Y0= y0 y1 y2 y3 y4 y5 y6 y7 Y1= y8 y9 y10 y11 y12 y13 y14 y15
  755. U= u0 u1 u2 u3 u4 u5 u6 u7 V= v0 v1 v2 v3 v4 v5 v6 v7
  756. Y0= y0 y1 y2 y3 y4 y5 y6 y7 Y1= y8 y9 y10 y11 y12 y13 y14 y15
  757. U0= u0 u0 u1 u1 u2 u2 u3 u3 U1= u4 u4 u5 u5 u6 u6 u7 u7
  758. V0= v0 v0 v1 v1 v2 v2 v3 v3 V1= v4 v4 v5 v5 v6 v6 v7 v7
  759. */
  760. U0 = vec_mergeh (U,U);
  761. V0 = vec_mergeh (V,V);
  762. U1 = vec_mergel (U,U);
  763. V1 = vec_mergel (V,V);
  764. cvtyuvtoRGB (c, Y0,U0,V0,&R0,&G0,&B0);
  765. cvtyuvtoRGB (c, Y1,U1,V1,&R1,&G1,&B1);
  766. R = vec_packclp (R0,R1);
  767. G = vec_packclp (G0,G1);
  768. B = vec_packclp (B0,B1);
  769. nout = (vector unsigned char *)scratch;
  770. out_rgba (R,G,B,nout);
  771. memcpy (&((uint32_t*)dest)[i], scratch, (dstW-i)/4);
  772. }
  773. if (vYCoeffsBank) free (vYCoeffsBank);
  774. if (vCCoeffsBank) free (vCCoeffsBank);
  775. }