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.

940 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 int hadamard8_diff8x8_altivec(MpegEncContext *s, uint8_t *dst,
  479. uint8_t *src, int stride, int h)
  480. {
  481. int sum;
  482. register const vector unsigned char vzero =
  483. (const vector unsigned char) vec_splat_u8(0);
  484. register vector signed short temp0, temp1, temp2, temp3, temp4,
  485. temp5, temp6, temp7;
  486. {
  487. register const vector signed short vprod1 =
  488. (const vector signed short) { 1, -1, 1, -1, 1, -1, 1, -1 };
  489. register const vector signed short vprod2 =
  490. (const vector signed short) { 1, 1, -1, -1, 1, 1, -1, -1 };
  491. register const vector signed short vprod3 =
  492. (const vector signed short) { 1, 1, 1, 1, -1, -1, -1, -1 };
  493. register const vector unsigned char perm1 =
  494. (const vector unsigned char)
  495. { 0x02, 0x03, 0x00, 0x01, 0x06, 0x07, 0x04, 0x05,
  496. 0x0A, 0x0B, 0x08, 0x09, 0x0E, 0x0F, 0x0C, 0x0D };
  497. register const vector unsigned char perm2 =
  498. (const vector unsigned char)
  499. { 0x04, 0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03,
  500. 0x0C, 0x0D, 0x0E, 0x0F, 0x08, 0x09, 0x0A, 0x0B };
  501. register const vector unsigned char perm3 =
  502. (const vector unsigned char)
  503. { 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  504. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
  505. #define ONEITERBUTTERFLY(i, res) \
  506. { \
  507. register vector unsigned char src1 = vec_ld(stride * i, src); \
  508. register vector unsigned char src2 = vec_ld(stride * i + 15, src); \
  509. register vector unsigned char srcO = \
  510. vec_perm(src1, src2, vec_lvsl(stride * i, src)); \
  511. register vector unsigned char dst1 = vec_ld(stride * i, dst); \
  512. register vector unsigned char dst2 = vec_ld(stride * i + 15, dst); \
  513. register vector unsigned char dstO = \
  514. vec_perm(dst1, dst2, vec_lvsl(stride * i, dst)); \
  515. \
  516. /* Promote the unsigned chars to signed shorts. */ \
  517. /* We're in the 8x8 function, we only care for the first 8. */ \
  518. register vector signed short srcV = \
  519. (vector signed short) vec_mergeh((vector signed char) vzero, \
  520. (vector signed char) srcO); \
  521. register vector signed short dstV = \
  522. (vector signed short) vec_mergeh((vector signed char) vzero, \
  523. (vector signed char) dstO); \
  524. \
  525. /* subtractions inside the first butterfly */ \
  526. register vector signed short but0 = vec_sub(srcV, dstV); \
  527. register vector signed short op1 = vec_perm(but0, but0, perm1); \
  528. register vector signed short but1 = vec_mladd(but0, vprod1, op1); \
  529. register vector signed short op2 = vec_perm(but1, but1, perm2); \
  530. register vector signed short but2 = vec_mladd(but1, vprod2, op2); \
  531. register vector signed short op3 = vec_perm(but2, but2, perm3); \
  532. res = vec_mladd(but2, vprod3, op3); \
  533. }
  534. ONEITERBUTTERFLY(0, temp0);
  535. ONEITERBUTTERFLY(1, temp1);
  536. ONEITERBUTTERFLY(2, temp2);
  537. ONEITERBUTTERFLY(3, temp3);
  538. ONEITERBUTTERFLY(4, temp4);
  539. ONEITERBUTTERFLY(5, temp5);
  540. ONEITERBUTTERFLY(6, temp6);
  541. ONEITERBUTTERFLY(7, temp7);
  542. }
  543. #undef ONEITERBUTTERFLY
  544. {
  545. register vector signed int vsum;
  546. register vector signed short line0 = vec_add(temp0, temp1);
  547. register vector signed short line1 = vec_sub(temp0, temp1);
  548. register vector signed short line2 = vec_add(temp2, temp3);
  549. register vector signed short line3 = vec_sub(temp2, temp3);
  550. register vector signed short line4 = vec_add(temp4, temp5);
  551. register vector signed short line5 = vec_sub(temp4, temp5);
  552. register vector signed short line6 = vec_add(temp6, temp7);
  553. register vector signed short line7 = vec_sub(temp6, temp7);
  554. register vector signed short line0B = vec_add(line0, line2);
  555. register vector signed short line2B = vec_sub(line0, line2);
  556. register vector signed short line1B = vec_add(line1, line3);
  557. register vector signed short line3B = vec_sub(line1, line3);
  558. register vector signed short line4B = vec_add(line4, line6);
  559. register vector signed short line6B = vec_sub(line4, line6);
  560. register vector signed short line5B = vec_add(line5, line7);
  561. register vector signed short line7B = vec_sub(line5, line7);
  562. register vector signed short line0C = vec_add(line0B, line4B);
  563. register vector signed short line4C = vec_sub(line0B, line4B);
  564. register vector signed short line1C = vec_add(line1B, line5B);
  565. register vector signed short line5C = vec_sub(line1B, line5B);
  566. register vector signed short line2C = vec_add(line2B, line6B);
  567. register vector signed short line6C = vec_sub(line2B, line6B);
  568. register vector signed short line3C = vec_add(line3B, line7B);
  569. register vector signed short line7C = vec_sub(line3B, line7B);
  570. vsum = vec_sum4s(vec_abs(line0C), vec_splat_s32(0));
  571. vsum = vec_sum4s(vec_abs(line1C), vsum);
  572. vsum = vec_sum4s(vec_abs(line2C), vsum);
  573. vsum = vec_sum4s(vec_abs(line3C), vsum);
  574. vsum = vec_sum4s(vec_abs(line4C), vsum);
  575. vsum = vec_sum4s(vec_abs(line5C), vsum);
  576. vsum = vec_sum4s(vec_abs(line6C), vsum);
  577. vsum = vec_sum4s(vec_abs(line7C), vsum);
  578. vsum = vec_sums(vsum, (vector signed int) vzero);
  579. vsum = vec_splat(vsum, 3);
  580. vec_ste(vsum, 0, &sum);
  581. }
  582. return sum;
  583. }
  584. /*
  585. * 16x8 works with 16 elements; it allows to avoid replicating loads, and
  586. * gives the compiler more room for scheduling. It's only used from
  587. * inside hadamard8_diff16_altivec.
  588. *
  589. * Unfortunately, it seems gcc-3.3 is a bit dumb, and the compiled code has
  590. * a LOT of spill code, it seems gcc (unlike xlc) cannot keep everything in
  591. * registers by itself. The following code includes hand-made register
  592. * allocation. It's not clean, but on a 7450 the resulting code is much faster
  593. * (best case falls from 700+ cycles to 550).
  594. *
  595. * xlc doesn't add spill code, but it doesn't know how to schedule for the
  596. * 7450, and its code isn't much faster than gcc-3.3 on the 7450 (but uses
  597. * 25% fewer instructions...)
  598. *
  599. * On the 970, the hand-made RA is still a win (around 690 vs. around 780),
  600. * but xlc goes to around 660 on the regular C code...
  601. */
  602. static int hadamard8_diff16x8_altivec(MpegEncContext *s, uint8_t *dst,
  603. uint8_t *src, int stride, int h)
  604. {
  605. int sum;
  606. register vector signed short
  607. temp0 __asm__ ("v0"),
  608. temp1 __asm__ ("v1"),
  609. temp2 __asm__ ("v2"),
  610. temp3 __asm__ ("v3"),
  611. temp4 __asm__ ("v4"),
  612. temp5 __asm__ ("v5"),
  613. temp6 __asm__ ("v6"),
  614. temp7 __asm__ ("v7");
  615. register vector signed short
  616. temp0S __asm__ ("v8"),
  617. temp1S __asm__ ("v9"),
  618. temp2S __asm__ ("v10"),
  619. temp3S __asm__ ("v11"),
  620. temp4S __asm__ ("v12"),
  621. temp5S __asm__ ("v13"),
  622. temp6S __asm__ ("v14"),
  623. temp7S __asm__ ("v15");
  624. register const vector unsigned char vzero __asm__ ("v31") =
  625. (const vector unsigned char) vec_splat_u8(0);
  626. {
  627. register const vector signed short vprod1 __asm__ ("v16") =
  628. (const vector signed short) { 1, -1, 1, -1, 1, -1, 1, -1 };
  629. register const vector signed short vprod2 __asm__ ("v17") =
  630. (const vector signed short) { 1, 1, -1, -1, 1, 1, -1, -1 };
  631. register const vector signed short vprod3 __asm__ ("v18") =
  632. (const vector signed short) { 1, 1, 1, 1, -1, -1, -1, -1 };
  633. register const vector unsigned char perm1 __asm__ ("v19") =
  634. (const vector unsigned char)
  635. { 0x02, 0x03, 0x00, 0x01, 0x06, 0x07, 0x04, 0x05,
  636. 0x0A, 0x0B, 0x08, 0x09, 0x0E, 0x0F, 0x0C, 0x0D };
  637. register const vector unsigned char perm2 __asm__ ("v20") =
  638. (const vector unsigned char)
  639. { 0x04, 0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03,
  640. 0x0C, 0x0D, 0x0E, 0x0F, 0x08, 0x09, 0x0A, 0x0B };
  641. register const vector unsigned char perm3 __asm__ ("v21") =
  642. (const vector unsigned char)
  643. { 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  644. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
  645. #define ONEITERBUTTERFLY(i, res1, res2) \
  646. { \
  647. register vector unsigned char src1 __asm__ ("v22") = \
  648. vec_ld(stride * i, src); \
  649. register vector unsigned char src2 __asm__ ("v23") = \
  650. vec_ld(stride * i + 16, src); \
  651. register vector unsigned char srcO __asm__ ("v22") = \
  652. vec_perm(src1, src2, vec_lvsl(stride * i, src)); \
  653. register vector unsigned char dst1 __asm__ ("v24") = \
  654. vec_ld(stride * i, dst); \
  655. register vector unsigned char dst2 __asm__ ("v25") = \
  656. vec_ld(stride * i + 16, dst); \
  657. register vector unsigned char dstO __asm__ ("v23") = \
  658. vec_perm(dst1, dst2, vec_lvsl(stride * i, dst)); \
  659. \
  660. /* Promote the unsigned chars to signed shorts. */ \
  661. register vector signed short srcV __asm__ ("v24") = \
  662. (vector signed short) vec_mergeh((vector signed char) vzero, \
  663. (vector signed char) srcO); \
  664. register vector signed short dstV __asm__ ("v25") = \
  665. (vector signed short) vec_mergeh((vector signed char) vzero, \
  666. (vector signed char) dstO); \
  667. register vector signed short srcW __asm__ ("v26") = \
  668. (vector signed short) vec_mergel((vector signed char) vzero, \
  669. (vector signed char) srcO); \
  670. register vector signed short dstW __asm__ ("v27") = \
  671. (vector signed short) vec_mergel((vector signed char) vzero, \
  672. (vector signed char) dstO); \
  673. \
  674. /* subtractions inside the first butterfly */ \
  675. register vector signed short but0 __asm__ ("v28") = \
  676. vec_sub(srcV, dstV); \
  677. register vector signed short but0S __asm__ ("v29") = \
  678. vec_sub(srcW, dstW); \
  679. register vector signed short op1 __asm__ ("v30") = \
  680. vec_perm(but0, but0, perm1); \
  681. register vector signed short but1 __asm__ ("v22") = \
  682. vec_mladd(but0, vprod1, op1); \
  683. register vector signed short op1S __asm__ ("v23") = \
  684. vec_perm(but0S, but0S, perm1); \
  685. register vector signed short but1S __asm__ ("v24") = \
  686. vec_mladd(but0S, vprod1, op1S); \
  687. register vector signed short op2 __asm__ ("v25") = \
  688. vec_perm(but1, but1, perm2); \
  689. register vector signed short but2 __asm__ ("v26") = \
  690. vec_mladd(but1, vprod2, op2); \
  691. register vector signed short op2S __asm__ ("v27") = \
  692. vec_perm(but1S, but1S, perm2); \
  693. register vector signed short but2S __asm__ ("v28") = \
  694. vec_mladd(but1S, vprod2, op2S); \
  695. register vector signed short op3 __asm__ ("v29") = \
  696. vec_perm(but2, but2, perm3); \
  697. register vector signed short op3S __asm__ ("v30") = \
  698. vec_perm(but2S, but2S, perm3); \
  699. res1 = vec_mladd(but2, vprod3, op3); \
  700. res2 = vec_mladd(but2S, vprod3, op3S); \
  701. }
  702. ONEITERBUTTERFLY(0, temp0, temp0S);
  703. ONEITERBUTTERFLY(1, temp1, temp1S);
  704. ONEITERBUTTERFLY(2, temp2, temp2S);
  705. ONEITERBUTTERFLY(3, temp3, temp3S);
  706. ONEITERBUTTERFLY(4, temp4, temp4S);
  707. ONEITERBUTTERFLY(5, temp5, temp5S);
  708. ONEITERBUTTERFLY(6, temp6, temp6S);
  709. ONEITERBUTTERFLY(7, temp7, temp7S);
  710. }
  711. #undef ONEITERBUTTERFLY
  712. {
  713. register vector signed int vsum;
  714. register vector signed short line0 = vec_add(temp0, temp1);
  715. register vector signed short line1 = vec_sub(temp0, temp1);
  716. register vector signed short line2 = vec_add(temp2, temp3);
  717. register vector signed short line3 = vec_sub(temp2, temp3);
  718. register vector signed short line4 = vec_add(temp4, temp5);
  719. register vector signed short line5 = vec_sub(temp4, temp5);
  720. register vector signed short line6 = vec_add(temp6, temp7);
  721. register vector signed short line7 = vec_sub(temp6, temp7);
  722. register vector signed short line0B = vec_add(line0, line2);
  723. register vector signed short line2B = vec_sub(line0, line2);
  724. register vector signed short line1B = vec_add(line1, line3);
  725. register vector signed short line3B = vec_sub(line1, line3);
  726. register vector signed short line4B = vec_add(line4, line6);
  727. register vector signed short line6B = vec_sub(line4, line6);
  728. register vector signed short line5B = vec_add(line5, line7);
  729. register vector signed short line7B = vec_sub(line5, line7);
  730. register vector signed short line0C = vec_add(line0B, line4B);
  731. register vector signed short line4C = vec_sub(line0B, line4B);
  732. register vector signed short line1C = vec_add(line1B, line5B);
  733. register vector signed short line5C = vec_sub(line1B, line5B);
  734. register vector signed short line2C = vec_add(line2B, line6B);
  735. register vector signed short line6C = vec_sub(line2B, line6B);
  736. register vector signed short line3C = vec_add(line3B, line7B);
  737. register vector signed short line7C = vec_sub(line3B, line7B);
  738. register vector signed short line0S = vec_add(temp0S, temp1S);
  739. register vector signed short line1S = vec_sub(temp0S, temp1S);
  740. register vector signed short line2S = vec_add(temp2S, temp3S);
  741. register vector signed short line3S = vec_sub(temp2S, temp3S);
  742. register vector signed short line4S = vec_add(temp4S, temp5S);
  743. register vector signed short line5S = vec_sub(temp4S, temp5S);
  744. register vector signed short line6S = vec_add(temp6S, temp7S);
  745. register vector signed short line7S = vec_sub(temp6S, temp7S);
  746. register vector signed short line0BS = vec_add(line0S, line2S);
  747. register vector signed short line2BS = vec_sub(line0S, line2S);
  748. register vector signed short line1BS = vec_add(line1S, line3S);
  749. register vector signed short line3BS = vec_sub(line1S, line3S);
  750. register vector signed short line4BS = vec_add(line4S, line6S);
  751. register vector signed short line6BS = vec_sub(line4S, line6S);
  752. register vector signed short line5BS = vec_add(line5S, line7S);
  753. register vector signed short line7BS = vec_sub(line5S, line7S);
  754. register vector signed short line0CS = vec_add(line0BS, line4BS);
  755. register vector signed short line4CS = vec_sub(line0BS, line4BS);
  756. register vector signed short line1CS = vec_add(line1BS, line5BS);
  757. register vector signed short line5CS = vec_sub(line1BS, line5BS);
  758. register vector signed short line2CS = vec_add(line2BS, line6BS);
  759. register vector signed short line6CS = vec_sub(line2BS, line6BS);
  760. register vector signed short line3CS = vec_add(line3BS, line7BS);
  761. register vector signed short line7CS = vec_sub(line3BS, line7BS);
  762. vsum = vec_sum4s(vec_abs(line0C), vec_splat_s32(0));
  763. vsum = vec_sum4s(vec_abs(line1C), vsum);
  764. vsum = vec_sum4s(vec_abs(line2C), vsum);
  765. vsum = vec_sum4s(vec_abs(line3C), vsum);
  766. vsum = vec_sum4s(vec_abs(line4C), vsum);
  767. vsum = vec_sum4s(vec_abs(line5C), vsum);
  768. vsum = vec_sum4s(vec_abs(line6C), vsum);
  769. vsum = vec_sum4s(vec_abs(line7C), vsum);
  770. vsum = vec_sum4s(vec_abs(line0CS), vsum);
  771. vsum = vec_sum4s(vec_abs(line1CS), vsum);
  772. vsum = vec_sum4s(vec_abs(line2CS), vsum);
  773. vsum = vec_sum4s(vec_abs(line3CS), vsum);
  774. vsum = vec_sum4s(vec_abs(line4CS), vsum);
  775. vsum = vec_sum4s(vec_abs(line5CS), vsum);
  776. vsum = vec_sum4s(vec_abs(line6CS), vsum);
  777. vsum = vec_sum4s(vec_abs(line7CS), vsum);
  778. vsum = vec_sums(vsum, (vector signed int) vzero);
  779. vsum = vec_splat(vsum, 3);
  780. vec_ste(vsum, 0, &sum);
  781. }
  782. return sum;
  783. }
  784. static int hadamard8_diff16_altivec(MpegEncContext *s, uint8_t *dst,
  785. uint8_t *src, int stride, int h)
  786. {
  787. int score = hadamard8_diff16x8_altivec(s, dst, src, stride, 8);
  788. if (h == 16) {
  789. dst += 8 * stride;
  790. src += 8 * stride;
  791. score += hadamard8_diff16x8_altivec(s, dst, src, stride, 8);
  792. }
  793. return score;
  794. }
  795. av_cold void ff_dsputil_init_altivec(DSPContext *c, AVCodecContext *avctx,
  796. unsigned high_bit_depth)
  797. {
  798. c->pix_abs[0][1] = sad16_x2_altivec;
  799. c->pix_abs[0][2] = sad16_y2_altivec;
  800. c->pix_abs[0][3] = sad16_xy2_altivec;
  801. c->pix_abs[0][0] = sad16_altivec;
  802. c->pix_abs[1][0] = sad8_altivec;
  803. c->sad[0] = sad16_altivec;
  804. c->sad[1] = sad8_altivec;
  805. c->sse[0] = sse16_altivec;
  806. c->sse[1] = sse8_altivec;
  807. c->pix_norm1 = pix_norm1_altivec;
  808. c->pix_sum = pix_sum_altivec;
  809. c->diff_pixels = diff_pixels_altivec;
  810. if (!high_bit_depth) {
  811. c->get_pixels = get_pixels_altivec;
  812. c->clear_block = clear_block_altivec;
  813. }
  814. c->hadamard8_diff[0] = hadamard8_diff16_altivec;
  815. c->hadamard8_diff[1] = hadamard8_diff8x8_altivec;
  816. }