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.

765 lines
34KB

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