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.

866 lines
38KB

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