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.

958 lines
40KB

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