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.

964 lines
38KB

  1. /*
  2. * Copyright (c) 2002 Brian Foley
  3. * Copyright (c) 2002 Dieter Shirley
  4. * Copyright (c) 2003-2004 Romain Dolbeau <romain@dolbeau.org>
  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. #include "config.h"
  23. #if HAVE_ALTIVEC_H
  24. #include <altivec.h>
  25. #endif
  26. #include "libavutil/attributes.h"
  27. #include "libavutil/ppc/types_altivec.h"
  28. #include "libavutil/ppc/util_altivec.h"
  29. #include "libavcodec/dsputil.h"
  30. #include "dsputil_altivec.h"
  31. static int sad16_x2_altivec(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
  32. {
  33. int i;
  34. int s;
  35. const vector unsigned char zero = (const vector unsigned char)vec_splat_u8(0);
  36. vector unsigned char perm1 = vec_lvsl(0, pix2);
  37. vector unsigned char perm2 = vec_add(perm1, vec_splat_u8(1));
  38. vector unsigned char pix2l, pix2r;
  39. vector unsigned char pix1v, pix2v, pix2iv, avgv, t5;
  40. vector unsigned int sad;
  41. vector signed int sumdiffs;
  42. s = 0;
  43. sad = (vector unsigned int)vec_splat_u32(0);
  44. for (i = 0; i < h; i++) {
  45. /* Read unaligned pixels into our vectors. The vectors are as follows:
  46. pix1v: pix1[0]-pix1[15]
  47. pix2v: pix2[0]-pix2[15] pix2iv: pix2[1]-pix2[16] */
  48. pix1v = vec_ld( 0, pix1);
  49. pix2l = vec_ld( 0, pix2);
  50. pix2r = vec_ld(16, pix2);
  51. pix2v = vec_perm(pix2l, pix2r, perm1);
  52. pix2iv = vec_perm(pix2l, pix2r, perm2);
  53. /* Calculate the average vector */
  54. avgv = vec_avg(pix2v, pix2iv);
  55. /* Calculate a sum of abs differences vector */
  56. t5 = vec_sub(vec_max(pix1v, avgv), vec_min(pix1v, avgv));
  57. /* Add each 4 pixel group together and put 4 results into sad */
  58. sad = vec_sum4s(t5, sad);
  59. pix1 += line_size;
  60. pix2 += line_size;
  61. }
  62. /* Sum up the four partial sums, and put the result into s */
  63. sumdiffs = vec_sums((vector signed int) sad, (vector signed int) zero);
  64. sumdiffs = vec_splat(sumdiffs, 3);
  65. vec_ste(sumdiffs, 0, &s);
  66. return s;
  67. }
  68. static int sad16_y2_altivec(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
  69. {
  70. int i;
  71. int s;
  72. const vector unsigned char zero = (const vector unsigned char)vec_splat_u8(0);
  73. vector unsigned char perm = vec_lvsl(0, pix2);
  74. vector unsigned char pix2l, pix2r;
  75. vector unsigned char pix1v, pix2v, pix3v, avgv, t5;
  76. vector unsigned int sad;
  77. vector signed int sumdiffs;
  78. uint8_t *pix3 = pix2 + line_size;
  79. s = 0;
  80. sad = (vector unsigned int)vec_splat_u32(0);
  81. /* Due to the fact that pix3 = pix2 + line_size, the pix3 of one
  82. iteration becomes pix2 in the next iteration. We can use this
  83. fact to avoid a potentially expensive unaligned read, each
  84. time around the loop.
  85. Read unaligned pixels into our vectors. The vectors are as follows:
  86. pix2v: pix2[0]-pix2[15]
  87. Split the pixel vectors into shorts */
  88. pix2l = vec_ld( 0, pix2);
  89. pix2r = vec_ld(15, pix2);
  90. pix2v = vec_perm(pix2l, pix2r, perm);
  91. for (i = 0; i < h; i++) {
  92. /* Read unaligned pixels into our vectors. The vectors are as follows:
  93. pix1v: pix1[0]-pix1[15]
  94. pix3v: pix3[0]-pix3[15] */
  95. pix1v = vec_ld(0, pix1);
  96. pix2l = vec_ld( 0, pix3);
  97. pix2r = vec_ld(15, pix3);
  98. pix3v = vec_perm(pix2l, pix2r, perm);
  99. /* Calculate the average vector */
  100. avgv = vec_avg(pix2v, pix3v);
  101. /* Calculate a sum of abs differences vector */
  102. t5 = vec_sub(vec_max(pix1v, avgv), vec_min(pix1v, avgv));
  103. /* Add each 4 pixel group together and put 4 results into sad */
  104. sad = vec_sum4s(t5, sad);
  105. pix1 += line_size;
  106. pix2v = pix3v;
  107. pix3 += line_size;
  108. }
  109. /* Sum up the four partial sums, and put the result into s */
  110. sumdiffs = vec_sums((vector signed int) sad, (vector signed int) zero);
  111. sumdiffs = vec_splat(sumdiffs, 3);
  112. vec_ste(sumdiffs, 0, &s);
  113. return s;
  114. }
  115. static int sad16_xy2_altivec(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
  116. {
  117. int i;
  118. int s;
  119. uint8_t *pix3 = pix2 + line_size;
  120. const vector unsigned char zero = (const vector unsigned char)vec_splat_u8(0);
  121. const vector unsigned short two = (const vector unsigned short)vec_splat_u16(2);
  122. vector unsigned char avgv, t5;
  123. vector unsigned char perm1 = vec_lvsl(0, pix2);
  124. vector unsigned char perm2 = vec_add(perm1, vec_splat_u8(1));
  125. vector unsigned char pix2l, pix2r;
  126. vector unsigned char pix1v, pix2v, pix3v, pix2iv, pix3iv;
  127. vector unsigned short pix2lv, pix2hv, pix2ilv, pix2ihv;
  128. vector unsigned short pix3lv, pix3hv, pix3ilv, pix3ihv;
  129. vector unsigned short avghv, avglv;
  130. vector unsigned short t1, t2, t3, t4;
  131. vector unsigned int sad;
  132. vector signed int sumdiffs;
  133. sad = (vector unsigned int)vec_splat_u32(0);
  134. s = 0;
  135. /* Due to the fact that pix3 = pix2 + line_size, the pix3 of one
  136. iteration becomes pix2 in the next iteration. We can use this
  137. fact to avoid a potentially expensive unaligned read, as well
  138. as some splitting, and vector addition each time around the loop.
  139. Read unaligned pixels into our vectors. The vectors are as follows:
  140. pix2v: pix2[0]-pix2[15] pix2iv: pix2[1]-pix2[16]
  141. Split the pixel vectors into shorts */
  142. pix2l = vec_ld( 0, pix2);
  143. pix2r = vec_ld(16, pix2);
  144. pix2v = vec_perm(pix2l, pix2r, perm1);
  145. pix2iv = vec_perm(pix2l, pix2r, perm2);
  146. pix2hv = (vector unsigned short) vec_mergeh(zero, pix2v);
  147. pix2lv = (vector unsigned short) vec_mergel(zero, pix2v);
  148. pix2ihv = (vector unsigned short) vec_mergeh(zero, pix2iv);
  149. pix2ilv = (vector unsigned short) vec_mergel(zero, pix2iv);
  150. t1 = vec_add(pix2hv, pix2ihv);
  151. t2 = vec_add(pix2lv, pix2ilv);
  152. for (i = 0; i < h; i++) {
  153. /* Read unaligned pixels into our vectors. The vectors are as follows:
  154. pix1v: pix1[0]-pix1[15]
  155. pix3v: pix3[0]-pix3[15] pix3iv: pix3[1]-pix3[16] */
  156. pix1v = vec_ld(0, pix1);
  157. pix2l = vec_ld( 0, pix3);
  158. pix2r = vec_ld(16, pix3);
  159. pix3v = vec_perm(pix2l, pix2r, perm1);
  160. pix3iv = vec_perm(pix2l, pix2r, perm2);
  161. /* Note that AltiVec does have vec_avg, but this works on vector pairs
  162. and rounds up. We could do avg(avg(a,b),avg(c,d)), but the rounding
  163. would mean that, for example, avg(3,0,0,1) = 2, when it should be 1.
  164. Instead, we have to split the pixel vectors into vectors of shorts,
  165. and do the averaging by hand. */
  166. /* Split the pixel vectors into shorts */
  167. pix3hv = (vector unsigned short) vec_mergeh(zero, pix3v);
  168. pix3lv = (vector unsigned short) vec_mergel(zero, pix3v);
  169. pix3ihv = (vector unsigned short) vec_mergeh(zero, pix3iv);
  170. pix3ilv = (vector unsigned short) vec_mergel(zero, pix3iv);
  171. /* Do the averaging on them */
  172. t3 = vec_add(pix3hv, pix3ihv);
  173. t4 = vec_add(pix3lv, pix3ilv);
  174. avghv = vec_sr(vec_add(vec_add(t1, t3), two), two);
  175. avglv = vec_sr(vec_add(vec_add(t2, t4), two), two);
  176. /* Pack the shorts back into a result */
  177. avgv = vec_pack(avghv, avglv);
  178. /* Calculate a sum of abs differences vector */
  179. t5 = vec_sub(vec_max(pix1v, avgv), vec_min(pix1v, avgv));
  180. /* Add each 4 pixel group together and put 4 results into sad */
  181. sad = vec_sum4s(t5, sad);
  182. pix1 += line_size;
  183. pix3 += line_size;
  184. /* Transfer the calculated values for pix3 into pix2 */
  185. t1 = t3;
  186. t2 = t4;
  187. }
  188. /* Sum up the four partial sums, and put the result into s */
  189. sumdiffs = vec_sums((vector signed int) sad, (vector signed int) zero);
  190. sumdiffs = vec_splat(sumdiffs, 3);
  191. vec_ste(sumdiffs, 0, &s);
  192. return s;
  193. }
  194. static int sad16_altivec(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
  195. {
  196. int i;
  197. int s;
  198. const vector unsigned int zero = (const vector unsigned int)vec_splat_u32(0);
  199. vector unsigned char perm = vec_lvsl(0, pix2);
  200. vector unsigned char t1, t2, t3,t4, t5;
  201. vector unsigned int sad;
  202. vector signed int sumdiffs;
  203. sad = (vector unsigned int)vec_splat_u32(0);
  204. for (i = 0; i < h; i++) {
  205. /* Read potentially unaligned pixels into t1 and t2 */
  206. vector unsigned char pix2l = vec_ld( 0, pix2);
  207. vector unsigned char pix2r = vec_ld(15, pix2);
  208. t1 = vec_ld(0, pix1);
  209. t2 = vec_perm(pix2l, pix2r, perm);
  210. /* Calculate a sum of abs differences vector */
  211. t3 = vec_max(t1, t2);
  212. t4 = vec_min(t1, t2);
  213. t5 = vec_sub(t3, t4);
  214. /* Add each 4 pixel group together and put 4 results into sad */
  215. sad = vec_sum4s(t5, sad);
  216. pix1 += line_size;
  217. pix2 += line_size;
  218. }
  219. /* Sum up the four partial sums, and put the result into s */
  220. sumdiffs = vec_sums((vector signed int) sad, (vector signed int) zero);
  221. sumdiffs = vec_splat(sumdiffs, 3);
  222. vec_ste(sumdiffs, 0, &s);
  223. return s;
  224. }
  225. static int sad8_altivec(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
  226. {
  227. int i;
  228. int s;
  229. const vector unsigned int zero = (const vector unsigned int)vec_splat_u32(0);
  230. const vector unsigned char permclear = (vector unsigned char){255,255,255,255,255,255,255,255,0,0,0,0,0,0,0,0};
  231. vector unsigned char perm1 = vec_lvsl(0, pix1);
  232. vector unsigned char perm2 = vec_lvsl(0, pix2);
  233. vector unsigned char t1, t2, t3,t4, t5;
  234. vector unsigned int sad;
  235. vector signed int sumdiffs;
  236. sad = (vector unsigned int)vec_splat_u32(0);
  237. for (i = 0; i < h; i++) {
  238. /* Read potentially unaligned pixels into t1 and t2
  239. Since we're reading 16 pixels, and actually only want 8,
  240. mask out the last 8 pixels. The 0s don't change the sum. */
  241. vector unsigned char pix1l = vec_ld( 0, pix1);
  242. vector unsigned char pix1r = vec_ld(15, pix1);
  243. vector unsigned char pix2l = vec_ld( 0, pix2);
  244. vector unsigned char pix2r = vec_ld(15, pix2);
  245. t1 = vec_and(vec_perm(pix1l, pix1r, perm1), permclear);
  246. t2 = vec_and(vec_perm(pix2l, pix2r, perm2), permclear);
  247. /* Calculate a sum of abs differences vector */
  248. t3 = vec_max(t1, t2);
  249. t4 = vec_min(t1, t2);
  250. t5 = vec_sub(t3, t4);
  251. /* Add each 4 pixel group together and put 4 results into sad */
  252. sad = vec_sum4s(t5, sad);
  253. pix1 += line_size;
  254. pix2 += line_size;
  255. }
  256. /* Sum up the four partial sums, and put the result into s */
  257. sumdiffs = vec_sums((vector signed int) sad, (vector signed int) zero);
  258. sumdiffs = vec_splat(sumdiffs, 3);
  259. vec_ste(sumdiffs, 0, &s);
  260. return s;
  261. }
  262. static int pix_norm1_altivec(uint8_t *pix, int line_size)
  263. {
  264. int i;
  265. int s;
  266. const vector unsigned int zero = (const vector unsigned int)vec_splat_u32(0);
  267. vector unsigned char perm = vec_lvsl(0, pix);
  268. vector unsigned char pixv;
  269. vector unsigned int sv;
  270. vector signed int sum;
  271. sv = (vector unsigned int)vec_splat_u32(0);
  272. s = 0;
  273. for (i = 0; i < 16; i++) {
  274. /* Read in the potentially unaligned pixels */
  275. vector unsigned char pixl = vec_ld( 0, pix);
  276. vector unsigned char pixr = vec_ld(15, pix);
  277. pixv = vec_perm(pixl, pixr, perm);
  278. /* Square the values, and add them to our sum */
  279. sv = vec_msum(pixv, pixv, sv);
  280. pix += line_size;
  281. }
  282. /* Sum up the four partial sums, and put the result into s */
  283. sum = vec_sums((vector signed int) sv, (vector signed int) zero);
  284. sum = vec_splat(sum, 3);
  285. vec_ste(sum, 0, &s);
  286. return s;
  287. }
  288. /**
  289. * Sum of Squared Errors for a 8x8 block.
  290. * AltiVec-enhanced.
  291. * It's the sad8_altivec code above w/ squaring added.
  292. */
  293. static int sse8_altivec(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
  294. {
  295. int i;
  296. int s;
  297. const vector unsigned int zero = (const vector unsigned int)vec_splat_u32(0);
  298. const vector unsigned char permclear = (vector unsigned char){255,255,255,255,255,255,255,255,0,0,0,0,0,0,0,0};
  299. vector unsigned char perm1 = vec_lvsl(0, pix1);
  300. vector unsigned char perm2 = vec_lvsl(0, pix2);
  301. vector unsigned char t1, t2, t3,t4, t5;
  302. vector unsigned int sum;
  303. vector signed int sumsqr;
  304. sum = (vector unsigned int)vec_splat_u32(0);
  305. for (i = 0; i < h; i++) {
  306. /* Read potentially unaligned pixels into t1 and t2
  307. Since we're reading 16 pixels, and actually only want 8,
  308. mask out the last 8 pixels. The 0s don't change the sum. */
  309. vector unsigned char pix1l = vec_ld( 0, pix1);
  310. vector unsigned char pix1r = vec_ld(15, pix1);
  311. vector unsigned char pix2l = vec_ld( 0, pix2);
  312. vector unsigned char pix2r = vec_ld(15, pix2);
  313. t1 = vec_and(vec_perm(pix1l, pix1r, perm1), permclear);
  314. t2 = vec_and(vec_perm(pix2l, pix2r, perm2), permclear);
  315. /* Since we want to use unsigned chars, we can take advantage
  316. of the fact that abs(a-b)^2 = (a-b)^2. */
  317. /* Calculate abs differences vector */
  318. t3 = vec_max(t1, t2);
  319. t4 = vec_min(t1, t2);
  320. t5 = vec_sub(t3, t4);
  321. /* Square the values and add them to our sum */
  322. sum = vec_msum(t5, t5, sum);
  323. pix1 += line_size;
  324. pix2 += line_size;
  325. }
  326. /* Sum up the four partial sums, and put the result into s */
  327. sumsqr = vec_sums((vector signed int) sum, (vector signed int) zero);
  328. sumsqr = vec_splat(sumsqr, 3);
  329. vec_ste(sumsqr, 0, &s);
  330. return s;
  331. }
  332. /**
  333. * Sum of Squared Errors for a 16x16 block.
  334. * AltiVec-enhanced.
  335. * It's the sad16_altivec code above w/ squaring added.
  336. */
  337. static int sse16_altivec(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
  338. {
  339. int i;
  340. int s;
  341. const vector unsigned int zero = (const vector unsigned int)vec_splat_u32(0);
  342. vector unsigned char perm = vec_lvsl(0, pix2);
  343. vector unsigned char t1, t2, t3,t4, t5;
  344. vector unsigned int sum;
  345. vector signed int sumsqr;
  346. sum = (vector unsigned int)vec_splat_u32(0);
  347. for (i = 0; i < h; i++) {
  348. /* Read potentially unaligned pixels into t1 and t2 */
  349. vector unsigned char pix2l = vec_ld( 0, pix2);
  350. vector unsigned char pix2r = vec_ld(15, pix2);
  351. t1 = vec_ld(0, pix1);
  352. t2 = vec_perm(pix2l, pix2r, perm);
  353. /* Since we want to use unsigned chars, we can take advantage
  354. of the fact that abs(a-b)^2 = (a-b)^2. */
  355. /* Calculate abs differences vector */
  356. t3 = vec_max(t1, t2);
  357. t4 = vec_min(t1, t2);
  358. t5 = vec_sub(t3, t4);
  359. /* Square the values and add them to our sum */
  360. sum = vec_msum(t5, t5, sum);
  361. pix1 += line_size;
  362. pix2 += line_size;
  363. }
  364. /* Sum up the four partial sums, and put the result into s */
  365. sumsqr = vec_sums((vector signed int) sum, (vector signed int) zero);
  366. sumsqr = vec_splat(sumsqr, 3);
  367. vec_ste(sumsqr, 0, &s);
  368. return s;
  369. }
  370. static int pix_sum_altivec(uint8_t * pix, int line_size)
  371. {
  372. const vector unsigned int zero = (const vector unsigned int)vec_splat_u32(0);
  373. vector unsigned char perm = vec_lvsl(0, pix);
  374. vector unsigned char t1;
  375. vector unsigned int sad;
  376. vector signed int sumdiffs;
  377. int i;
  378. int s;
  379. sad = (vector unsigned int)vec_splat_u32(0);
  380. for (i = 0; i < 16; i++) {
  381. /* Read the potentially unaligned 16 pixels into t1 */
  382. vector unsigned char pixl = vec_ld( 0, pix);
  383. vector unsigned char pixr = vec_ld(15, pix);
  384. t1 = vec_perm(pixl, pixr, perm);
  385. /* Add each 4 pixel group together and put 4 results into sad */
  386. sad = vec_sum4s(t1, sad);
  387. pix += line_size;
  388. }
  389. /* Sum up the four partial sums, and put the result into s */
  390. sumdiffs = vec_sums((vector signed int) sad, (vector signed int) zero);
  391. sumdiffs = vec_splat(sumdiffs, 3);
  392. vec_ste(sumdiffs, 0, &s);
  393. return s;
  394. }
  395. static void get_pixels_altivec(int16_t *restrict block, const uint8_t *pixels, int line_size)
  396. {
  397. int i;
  398. vector unsigned char perm = vec_lvsl(0, pixels);
  399. vector unsigned char bytes;
  400. const vector unsigned char zero = (const vector unsigned char)vec_splat_u8(0);
  401. vector signed short shorts;
  402. for (i = 0; i < 8; i++) {
  403. // Read potentially unaligned pixels.
  404. // We're reading 16 pixels, and actually only want 8,
  405. // but we simply ignore the extras.
  406. vector unsigned char pixl = vec_ld( 0, pixels);
  407. vector unsigned char pixr = vec_ld(15, pixels);
  408. bytes = vec_perm(pixl, pixr, perm);
  409. // convert the bytes into shorts
  410. shorts = (vector signed short)vec_mergeh(zero, bytes);
  411. // save the data to the block, we assume the block is 16-byte aligned
  412. vec_st(shorts, i*16, (vector signed short*)block);
  413. pixels += line_size;
  414. }
  415. }
  416. static void diff_pixels_altivec(int16_t *restrict block, const uint8_t *s1,
  417. const uint8_t *s2, int stride)
  418. {
  419. int i;
  420. vector unsigned char perm1 = vec_lvsl(0, s1);
  421. vector unsigned char perm2 = vec_lvsl(0, s2);
  422. vector unsigned char bytes, pixl, pixr;
  423. const vector unsigned char zero = (const vector unsigned char)vec_splat_u8(0);
  424. vector signed short shorts1, shorts2;
  425. for (i = 0; i < 4; i++) {
  426. // Read potentially unaligned pixels
  427. // We're reading 16 pixels, and actually only want 8,
  428. // but we simply ignore the extras.
  429. pixl = vec_ld( 0, s1);
  430. pixr = vec_ld(15, s1);
  431. bytes = vec_perm(pixl, pixr, perm1);
  432. // convert the bytes into shorts
  433. shorts1 = (vector signed short)vec_mergeh(zero, bytes);
  434. // Do the same for the second block of pixels
  435. pixl = vec_ld( 0, s2);
  436. pixr = vec_ld(15, s2);
  437. bytes = vec_perm(pixl, pixr, perm2);
  438. // convert the bytes into shorts
  439. shorts2 = (vector signed short)vec_mergeh(zero, bytes);
  440. // Do the subtraction
  441. shorts1 = vec_sub(shorts1, shorts2);
  442. // save the data to the block, we assume the block is 16-byte aligned
  443. vec_st(shorts1, 0, (vector signed short*)block);
  444. s1 += stride;
  445. s2 += stride;
  446. block += 8;
  447. // The code below is a copy of the code above... This is a manual
  448. // unroll.
  449. // Read potentially unaligned pixels
  450. // We're reading 16 pixels, and actually only want 8,
  451. // but we simply ignore the extras.
  452. pixl = vec_ld( 0, s1);
  453. pixr = vec_ld(15, s1);
  454. bytes = vec_perm(pixl, pixr, perm1);
  455. // convert the bytes into shorts
  456. shorts1 = (vector signed short)vec_mergeh(zero, bytes);
  457. // Do the same for the second block of pixels
  458. pixl = vec_ld( 0, s2);
  459. pixr = vec_ld(15, s2);
  460. bytes = vec_perm(pixl, pixr, perm2);
  461. // convert the bytes into shorts
  462. shorts2 = (vector signed short)vec_mergeh(zero, bytes);
  463. // Do the subtraction
  464. shorts1 = vec_sub(shorts1, shorts2);
  465. // save the data to the block, we assume the block is 16-byte aligned
  466. vec_st(shorts1, 0, (vector signed short*)block);
  467. s1 += stride;
  468. s2 += stride;
  469. block += 8;
  470. }
  471. }
  472. static void clear_block_altivec(int16_t *block) {
  473. LOAD_ZERO;
  474. vec_st(zero_s16v, 0, block);
  475. vec_st(zero_s16v, 16, block);
  476. vec_st(zero_s16v, 32, block);
  477. vec_st(zero_s16v, 48, block);
  478. vec_st(zero_s16v, 64, block);
  479. vec_st(zero_s16v, 80, block);
  480. vec_st(zero_s16v, 96, block);
  481. vec_st(zero_s16v, 112, block);
  482. }
  483. static void add_bytes_altivec(uint8_t *dst, uint8_t *src, int w) {
  484. register int i;
  485. register vector unsigned char vdst, vsrc;
  486. /* dst and src are 16 bytes-aligned (guaranteed) */
  487. for (i = 0 ; (i + 15) < w ; i+=16) {
  488. vdst = vec_ld(i, (unsigned char*)dst);
  489. vsrc = vec_ld(i, (unsigned char*)src);
  490. vdst = vec_add(vsrc, vdst);
  491. vec_st(vdst, i, (unsigned char*)dst);
  492. }
  493. /* if w is not a multiple of 16 */
  494. for (; (i < w) ; i++) {
  495. dst[i] = src[i];
  496. }
  497. }
  498. static int hadamard8_diff8x8_altivec(/*MpegEncContext*/ void *s, uint8_t *dst, uint8_t *src, int stride, int h){
  499. int sum;
  500. register const vector unsigned char vzero =
  501. (const vector unsigned char)vec_splat_u8(0);
  502. register vector signed short temp0, temp1, temp2, temp3, temp4,
  503. temp5, temp6, temp7;
  504. {
  505. register const vector signed short vprod1 =(const vector signed short)
  506. { 1,-1, 1,-1, 1,-1, 1,-1 };
  507. register const vector signed short vprod2 =(const vector signed short)
  508. { 1, 1,-1,-1, 1, 1,-1,-1 };
  509. register const vector signed short vprod3 =(const vector signed short)
  510. { 1, 1, 1, 1,-1,-1,-1,-1 };
  511. register const vector unsigned char perm1 = (const vector unsigned char)
  512. {0x02, 0x03, 0x00, 0x01, 0x06, 0x07, 0x04, 0x05,
  513. 0x0A, 0x0B, 0x08, 0x09, 0x0E, 0x0F, 0x0C, 0x0D};
  514. register const vector unsigned char perm2 = (const vector unsigned char)
  515. {0x04, 0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03,
  516. 0x0C, 0x0D, 0x0E, 0x0F, 0x08, 0x09, 0x0A, 0x0B};
  517. register const vector unsigned char perm3 = (const vector unsigned char)
  518. {0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  519. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
  520. #define ONEITERBUTTERFLY(i, res) \
  521. { \
  522. register vector unsigned char src1, src2, srcO; \
  523. register vector unsigned char dst1, dst2, dstO; \
  524. register vector signed short srcV, dstV; \
  525. register vector signed short but0, but1, but2, op1, op2, op3; \
  526. src1 = vec_ld(stride * i, src); \
  527. src2 = vec_ld((stride * i) + 15, src); \
  528. srcO = vec_perm(src1, src2, vec_lvsl(stride * i, src)); \
  529. dst1 = vec_ld(stride * i, dst); \
  530. dst2 = vec_ld((stride * i) + 15, dst); \
  531. dstO = vec_perm(dst1, dst2, vec_lvsl(stride * i, dst)); \
  532. /* promote the unsigned chars to signed shorts */ \
  533. /* we're in the 8x8 function, we only care for the first 8 */ \
  534. srcV = (vector signed short)vec_mergeh((vector signed char)vzero, \
  535. (vector signed char)srcO); \
  536. dstV = (vector signed short)vec_mergeh((vector signed char)vzero, \
  537. (vector signed char)dstO); \
  538. /* subtractions inside the first butterfly */ \
  539. but0 = vec_sub(srcV, dstV); \
  540. op1 = vec_perm(but0, but0, perm1); \
  541. but1 = vec_mladd(but0, vprod1, op1); \
  542. op2 = vec_perm(but1, but1, perm2); \
  543. but2 = vec_mladd(but1, vprod2, op2); \
  544. op3 = vec_perm(but2, but2, perm3); \
  545. res = vec_mladd(but2, vprod3, op3); \
  546. }
  547. ONEITERBUTTERFLY(0, temp0);
  548. ONEITERBUTTERFLY(1, temp1);
  549. ONEITERBUTTERFLY(2, temp2);
  550. ONEITERBUTTERFLY(3, temp3);
  551. ONEITERBUTTERFLY(4, temp4);
  552. ONEITERBUTTERFLY(5, temp5);
  553. ONEITERBUTTERFLY(6, temp6);
  554. ONEITERBUTTERFLY(7, temp7);
  555. }
  556. #undef ONEITERBUTTERFLY
  557. {
  558. register vector signed int vsum;
  559. register vector signed short line0 = vec_add(temp0, temp1);
  560. register vector signed short line1 = vec_sub(temp0, temp1);
  561. register vector signed short line2 = vec_add(temp2, temp3);
  562. register vector signed short line3 = vec_sub(temp2, temp3);
  563. register vector signed short line4 = vec_add(temp4, temp5);
  564. register vector signed short line5 = vec_sub(temp4, temp5);
  565. register vector signed short line6 = vec_add(temp6, temp7);
  566. register vector signed short line7 = vec_sub(temp6, temp7);
  567. register vector signed short line0B = vec_add(line0, line2);
  568. register vector signed short line2B = vec_sub(line0, line2);
  569. register vector signed short line1B = vec_add(line1, line3);
  570. register vector signed short line3B = vec_sub(line1, line3);
  571. register vector signed short line4B = vec_add(line4, line6);
  572. register vector signed short line6B = vec_sub(line4, line6);
  573. register vector signed short line5B = vec_add(line5, line7);
  574. register vector signed short line7B = vec_sub(line5, line7);
  575. register vector signed short line0C = vec_add(line0B, line4B);
  576. register vector signed short line4C = vec_sub(line0B, line4B);
  577. register vector signed short line1C = vec_add(line1B, line5B);
  578. register vector signed short line5C = vec_sub(line1B, line5B);
  579. register vector signed short line2C = vec_add(line2B, line6B);
  580. register vector signed short line6C = vec_sub(line2B, line6B);
  581. register vector signed short line3C = vec_add(line3B, line7B);
  582. register vector signed short line7C = vec_sub(line3B, line7B);
  583. vsum = vec_sum4s(vec_abs(line0C), vec_splat_s32(0));
  584. vsum = vec_sum4s(vec_abs(line1C), vsum);
  585. vsum = vec_sum4s(vec_abs(line2C), vsum);
  586. vsum = vec_sum4s(vec_abs(line3C), vsum);
  587. vsum = vec_sum4s(vec_abs(line4C), vsum);
  588. vsum = vec_sum4s(vec_abs(line5C), vsum);
  589. vsum = vec_sum4s(vec_abs(line6C), vsum);
  590. vsum = vec_sum4s(vec_abs(line7C), vsum);
  591. vsum = vec_sums(vsum, (vector signed int)vzero);
  592. vsum = vec_splat(vsum, 3);
  593. vec_ste(vsum, 0, &sum);
  594. }
  595. return sum;
  596. }
  597. /*
  598. 16x8 works with 16 elements; it allows to avoid replicating loads, and
  599. give the compiler more rooms for scheduling. It's only used from
  600. inside hadamard8_diff16_altivec.
  601. Unfortunately, it seems gcc-3.3 is a bit dumb, and the compiled code has a LOT
  602. of spill code, it seems gcc (unlike xlc) cannot keep everything in registers
  603. by itself. The following code include hand-made registers allocation. It's not
  604. clean, but on a 7450 the resulting code is much faster (best case fall from
  605. 700+ cycles to 550).
  606. xlc doesn't add spill code, but it doesn't know how to schedule for the 7450,
  607. and its code isn't much faster than gcc-3.3 on the 7450 (but uses 25% less
  608. instructions...)
  609. On the 970, the hand-made RA is still a win (around 690 vs. around 780), but
  610. xlc goes to around 660 on the regular C code...
  611. */
  612. static int hadamard8_diff16x8_altivec(/*MpegEncContext*/ void *s, uint8_t *dst, uint8_t *src, int stride, int h) {
  613. int sum;
  614. register vector signed short
  615. temp0 __asm__ ("v0"),
  616. temp1 __asm__ ("v1"),
  617. temp2 __asm__ ("v2"),
  618. temp3 __asm__ ("v3"),
  619. temp4 __asm__ ("v4"),
  620. temp5 __asm__ ("v5"),
  621. temp6 __asm__ ("v6"),
  622. temp7 __asm__ ("v7");
  623. register vector signed short
  624. temp0S __asm__ ("v8"),
  625. temp1S __asm__ ("v9"),
  626. temp2S __asm__ ("v10"),
  627. temp3S __asm__ ("v11"),
  628. temp4S __asm__ ("v12"),
  629. temp5S __asm__ ("v13"),
  630. temp6S __asm__ ("v14"),
  631. temp7S __asm__ ("v15");
  632. register const vector unsigned char vzero __asm__ ("v31") =
  633. (const vector unsigned char)vec_splat_u8(0);
  634. {
  635. register const vector signed short vprod1 __asm__ ("v16") =
  636. (const vector signed short){ 1,-1, 1,-1, 1,-1, 1,-1 };
  637. register const vector signed short vprod2 __asm__ ("v17") =
  638. (const vector signed short){ 1, 1,-1,-1, 1, 1,-1,-1 };
  639. register const vector signed short vprod3 __asm__ ("v18") =
  640. (const vector signed short){ 1, 1, 1, 1,-1,-1,-1,-1 };
  641. register const vector unsigned char perm1 __asm__ ("v19") =
  642. (const vector unsigned char)
  643. {0x02, 0x03, 0x00, 0x01, 0x06, 0x07, 0x04, 0x05,
  644. 0x0A, 0x0B, 0x08, 0x09, 0x0E, 0x0F, 0x0C, 0x0D};
  645. register const vector unsigned char perm2 __asm__ ("v20") =
  646. (const vector unsigned char)
  647. {0x04, 0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03,
  648. 0x0C, 0x0D, 0x0E, 0x0F, 0x08, 0x09, 0x0A, 0x0B};
  649. register const vector unsigned char perm3 __asm__ ("v21") =
  650. (const vector unsigned char)
  651. {0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  652. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
  653. #define ONEITERBUTTERFLY(i, res1, res2) \
  654. { \
  655. register vector unsigned char src1 __asm__ ("v22"), \
  656. src2 __asm__ ("v23"), \
  657. dst1 __asm__ ("v24"), \
  658. dst2 __asm__ ("v25"), \
  659. srcO __asm__ ("v22"), \
  660. dstO __asm__ ("v23"); \
  661. \
  662. register vector signed short srcV __asm__ ("v24"), \
  663. dstV __asm__ ("v25"), \
  664. srcW __asm__ ("v26"), \
  665. dstW __asm__ ("v27"), \
  666. but0 __asm__ ("v28"), \
  667. but0S __asm__ ("v29"), \
  668. op1 __asm__ ("v30"), \
  669. but1 __asm__ ("v22"), \
  670. op1S __asm__ ("v23"), \
  671. but1S __asm__ ("v24"), \
  672. op2 __asm__ ("v25"), \
  673. but2 __asm__ ("v26"), \
  674. op2S __asm__ ("v27"), \
  675. but2S __asm__ ("v28"), \
  676. op3 __asm__ ("v29"), \
  677. op3S __asm__ ("v30"); \
  678. \
  679. src1 = vec_ld(stride * i, src); \
  680. src2 = vec_ld((stride * i) + 16, src); \
  681. srcO = vec_perm(src1, src2, vec_lvsl(stride * i, src)); \
  682. dst1 = vec_ld(stride * i, dst); \
  683. dst2 = vec_ld((stride * i) + 16, dst); \
  684. dstO = vec_perm(dst1, dst2, vec_lvsl(stride * i, dst)); \
  685. /* promote the unsigned chars to signed shorts */ \
  686. srcV = (vector signed short)vec_mergeh((vector signed char)vzero, \
  687. (vector signed char)srcO); \
  688. dstV = (vector signed short)vec_mergeh((vector signed char)vzero, \
  689. (vector signed char)dstO); \
  690. srcW = (vector signed short)vec_mergel((vector signed char)vzero, \
  691. (vector signed char)srcO); \
  692. dstW = (vector signed short)vec_mergel((vector signed char)vzero, \
  693. (vector signed char)dstO); \
  694. /* subtractions inside the first butterfly */ \
  695. but0 = vec_sub(srcV, dstV); \
  696. but0S = vec_sub(srcW, dstW); \
  697. op1 = vec_perm(but0, but0, perm1); \
  698. but1 = vec_mladd(but0, vprod1, op1); \
  699. op1S = vec_perm(but0S, but0S, perm1); \
  700. but1S = vec_mladd(but0S, vprod1, op1S); \
  701. op2 = vec_perm(but1, but1, perm2); \
  702. but2 = vec_mladd(but1, vprod2, op2); \
  703. op2S = vec_perm(but1S, but1S, perm2); \
  704. but2S = vec_mladd(but1S, vprod2, op2S); \
  705. op3 = vec_perm(but2, but2, perm3); \
  706. res1 = vec_mladd(but2, vprod3, op3); \
  707. op3S = vec_perm(but2S, but2S, perm3); \
  708. res2 = vec_mladd(but2S, vprod3, op3S); \
  709. }
  710. ONEITERBUTTERFLY(0, temp0, temp0S);
  711. ONEITERBUTTERFLY(1, temp1, temp1S);
  712. ONEITERBUTTERFLY(2, temp2, temp2S);
  713. ONEITERBUTTERFLY(3, temp3, temp3S);
  714. ONEITERBUTTERFLY(4, temp4, temp4S);
  715. ONEITERBUTTERFLY(5, temp5, temp5S);
  716. ONEITERBUTTERFLY(6, temp6, temp6S);
  717. ONEITERBUTTERFLY(7, temp7, temp7S);
  718. }
  719. #undef ONEITERBUTTERFLY
  720. {
  721. register vector signed int vsum;
  722. register vector signed short line0S, line1S, line2S, line3S, line4S,
  723. line5S, line6S, line7S, line0BS,line2BS,
  724. line1BS,line3BS,line4BS,line6BS,line5BS,
  725. line7BS,line0CS,line4CS,line1CS,line5CS,
  726. line2CS,line6CS,line3CS,line7CS;
  727. register vector signed short line0 = vec_add(temp0, temp1);
  728. register vector signed short line1 = vec_sub(temp0, temp1);
  729. register vector signed short line2 = vec_add(temp2, temp3);
  730. register vector signed short line3 = vec_sub(temp2, temp3);
  731. register vector signed short line4 = vec_add(temp4, temp5);
  732. register vector signed short line5 = vec_sub(temp4, temp5);
  733. register vector signed short line6 = vec_add(temp6, temp7);
  734. register vector signed short line7 = vec_sub(temp6, temp7);
  735. register vector signed short line0B = vec_add(line0, line2);
  736. register vector signed short line2B = vec_sub(line0, line2);
  737. register vector signed short line1B = vec_add(line1, line3);
  738. register vector signed short line3B = vec_sub(line1, line3);
  739. register vector signed short line4B = vec_add(line4, line6);
  740. register vector signed short line6B = vec_sub(line4, line6);
  741. register vector signed short line5B = vec_add(line5, line7);
  742. register vector signed short line7B = vec_sub(line5, line7);
  743. register vector signed short line0C = vec_add(line0B, line4B);
  744. register vector signed short line4C = vec_sub(line0B, line4B);
  745. register vector signed short line1C = vec_add(line1B, line5B);
  746. register vector signed short line5C = vec_sub(line1B, line5B);
  747. register vector signed short line2C = vec_add(line2B, line6B);
  748. register vector signed short line6C = vec_sub(line2B, line6B);
  749. register vector signed short line3C = vec_add(line3B, line7B);
  750. register vector signed short line7C = vec_sub(line3B, line7B);
  751. vsum = vec_sum4s(vec_abs(line0C), vec_splat_s32(0));
  752. vsum = vec_sum4s(vec_abs(line1C), vsum);
  753. vsum = vec_sum4s(vec_abs(line2C), vsum);
  754. vsum = vec_sum4s(vec_abs(line3C), vsum);
  755. vsum = vec_sum4s(vec_abs(line4C), vsum);
  756. vsum = vec_sum4s(vec_abs(line5C), vsum);
  757. vsum = vec_sum4s(vec_abs(line6C), vsum);
  758. vsum = vec_sum4s(vec_abs(line7C), vsum);
  759. line0S = vec_add(temp0S, temp1S);
  760. line1S = vec_sub(temp0S, temp1S);
  761. line2S = vec_add(temp2S, temp3S);
  762. line3S = vec_sub(temp2S, temp3S);
  763. line4S = vec_add(temp4S, temp5S);
  764. line5S = vec_sub(temp4S, temp5S);
  765. line6S = vec_add(temp6S, temp7S);
  766. line7S = vec_sub(temp6S, temp7S);
  767. line0BS = vec_add(line0S, line2S);
  768. line2BS = vec_sub(line0S, line2S);
  769. line1BS = vec_add(line1S, line3S);
  770. line3BS = vec_sub(line1S, line3S);
  771. line4BS = vec_add(line4S, line6S);
  772. line6BS = vec_sub(line4S, line6S);
  773. line5BS = vec_add(line5S, line7S);
  774. line7BS = vec_sub(line5S, line7S);
  775. line0CS = vec_add(line0BS, line4BS);
  776. line4CS = vec_sub(line0BS, line4BS);
  777. line1CS = vec_add(line1BS, line5BS);
  778. line5CS = vec_sub(line1BS, line5BS);
  779. line2CS = vec_add(line2BS, line6BS);
  780. line6CS = vec_sub(line2BS, line6BS);
  781. line3CS = vec_add(line3BS, line7BS);
  782. line7CS = vec_sub(line3BS, line7BS);
  783. vsum = vec_sum4s(vec_abs(line0CS), vsum);
  784. vsum = vec_sum4s(vec_abs(line1CS), vsum);
  785. vsum = vec_sum4s(vec_abs(line2CS), vsum);
  786. vsum = vec_sum4s(vec_abs(line3CS), vsum);
  787. vsum = vec_sum4s(vec_abs(line4CS), vsum);
  788. vsum = vec_sum4s(vec_abs(line5CS), vsum);
  789. vsum = vec_sum4s(vec_abs(line6CS), vsum);
  790. vsum = vec_sum4s(vec_abs(line7CS), vsum);
  791. vsum = vec_sums(vsum, (vector signed int)vzero);
  792. vsum = vec_splat(vsum, 3);
  793. vec_ste(vsum, 0, &sum);
  794. }
  795. return sum;
  796. }
  797. static int hadamard8_diff16_altivec(/*MpegEncContext*/ void *s, uint8_t *dst, uint8_t *src, int stride, int h){
  798. int score;
  799. score = hadamard8_diff16x8_altivec(s, dst, src, stride, 8);
  800. if (h==16) {
  801. dst += 8*stride;
  802. src += 8*stride;
  803. score += hadamard8_diff16x8_altivec(s, dst, src, stride, 8);
  804. }
  805. return score;
  806. }
  807. av_cold void ff_dsputil_init_altivec(DSPContext *c, AVCodecContext *avctx)
  808. {
  809. const int high_bit_depth = avctx->bits_per_raw_sample > 8;
  810. c->pix_abs[0][1] = sad16_x2_altivec;
  811. c->pix_abs[0][2] = sad16_y2_altivec;
  812. c->pix_abs[0][3] = sad16_xy2_altivec;
  813. c->pix_abs[0][0] = sad16_altivec;
  814. c->pix_abs[1][0] = sad8_altivec;
  815. c->sad[0]= sad16_altivec;
  816. c->sad[1]= sad8_altivec;
  817. c->pix_norm1 = pix_norm1_altivec;
  818. c->sse[1]= sse8_altivec;
  819. c->sse[0]= sse16_altivec;
  820. c->pix_sum = pix_sum_altivec;
  821. c->diff_pixels = diff_pixels_altivec;
  822. c->add_bytes= add_bytes_altivec;
  823. if (!high_bit_depth) {
  824. c->get_pixels = get_pixels_altivec;
  825. c->clear_block = clear_block_altivec;
  826. }
  827. c->hadamard8_diff[0] = hadamard8_diff16_altivec;
  828. c->hadamard8_diff[1] = hadamard8_diff8x8_altivec;
  829. }