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.

750 lines
32KB

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