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.

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