postprocess_altivec_template.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210
  1. /*
  2. * AltiVec optimizations (C) 2004 Romain Dolbeau <romain@dolbeau.org>
  3. *
  4. * based on code by Copyright (C) 2001-2003 Michael Niedermayer (michaelni@gmx.at)
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (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
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * 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 "libavutil/avutil.h"
  23. #define ALTIVEC_TRANSPOSE_8x8_SHORT(src_a,src_b,src_c,src_d,src_e,src_f,src_g,src_h) \
  24. do { \
  25. __typeof__(src_a) tempA1, tempB1, tempC1, tempD1; \
  26. __typeof__(src_a) tempE1, tempF1, tempG1, tempH1; \
  27. __typeof__(src_a) tempA2, tempB2, tempC2, tempD2; \
  28. __typeof__(src_a) tempE2, tempF2, tempG2, tempH2; \
  29. tempA1 = vec_mergeh (src_a, src_e); \
  30. tempB1 = vec_mergel (src_a, src_e); \
  31. tempC1 = vec_mergeh (src_b, src_f); \
  32. tempD1 = vec_mergel (src_b, src_f); \
  33. tempE1 = vec_mergeh (src_c, src_g); \
  34. tempF1 = vec_mergel (src_c, src_g); \
  35. tempG1 = vec_mergeh (src_d, src_h); \
  36. tempH1 = vec_mergel (src_d, src_h); \
  37. tempA2 = vec_mergeh (tempA1, tempE1); \
  38. tempB2 = vec_mergel (tempA1, tempE1); \
  39. tempC2 = vec_mergeh (tempB1, tempF1); \
  40. tempD2 = vec_mergel (tempB1, tempF1); \
  41. tempE2 = vec_mergeh (tempC1, tempG1); \
  42. tempF2 = vec_mergel (tempC1, tempG1); \
  43. tempG2 = vec_mergeh (tempD1, tempH1); \
  44. tempH2 = vec_mergel (tempD1, tempH1); \
  45. src_a = vec_mergeh (tempA2, tempE2); \
  46. src_b = vec_mergel (tempA2, tempE2); \
  47. src_c = vec_mergeh (tempB2, tempF2); \
  48. src_d = vec_mergel (tempB2, tempF2); \
  49. src_e = vec_mergeh (tempC2, tempG2); \
  50. src_f = vec_mergel (tempC2, tempG2); \
  51. src_g = vec_mergeh (tempD2, tempH2); \
  52. src_h = vec_mergel (tempD2, tempH2); \
  53. } while (0)
  54. static inline int vertClassify_altivec(uint8_t src[], int stride, PPContext *c) {
  55. /*
  56. this code makes no assumption on src or stride.
  57. One could remove the recomputation of the perm
  58. vector by assuming (stride % 16) == 0, unfortunately
  59. this is not always true.
  60. */
  61. short data_0 = ((c->nonBQP*c->ppMode.baseDcDiff)>>8) + 1;
  62. DECLARE_ALIGNED(16, short, data)[8] =
  63. {
  64. data_0,
  65. data_0 * 2 + 1,
  66. c->QP * 2,
  67. c->QP * 4
  68. };
  69. int numEq;
  70. uint8_t *src2 = src;
  71. vector signed short v_dcOffset;
  72. vector signed short v2QP;
  73. vector unsigned short v4QP;
  74. vector unsigned short v_dcThreshold;
  75. const int properStride = (stride % 16);
  76. const int srcAlign = ((unsigned long)src2 % 16);
  77. const int two_vectors = ((srcAlign > 8) || properStride) ? 1 : 0;
  78. const vector signed int zero = vec_splat_s32(0);
  79. const vector signed short mask = vec_splat_s16(1);
  80. vector signed int v_numEq = vec_splat_s32(0);
  81. vector signed short v_data = vec_ld(0, data);
  82. vector signed short v_srcAss0, v_srcAss1, v_srcAss2, v_srcAss3,
  83. v_srcAss4, v_srcAss5, v_srcAss6, v_srcAss7;
  84. //FIXME avoid this mess if possible
  85. register int j0 = 0,
  86. j1 = stride,
  87. j2 = 2 * stride,
  88. j3 = 3 * stride,
  89. j4 = 4 * stride,
  90. j5 = 5 * stride,
  91. j6 = 6 * stride,
  92. j7 = 7 * stride;
  93. vector unsigned char v_srcA0, v_srcA1, v_srcA2, v_srcA3,
  94. v_srcA4, v_srcA5, v_srcA6, v_srcA7;
  95. v_dcOffset = vec_splat(v_data, 0);
  96. v_dcThreshold = (vector unsigned short)vec_splat(v_data, 1);
  97. v2QP = vec_splat(v_data, 2);
  98. v4QP = (vector unsigned short)vec_splat(v_data, 3);
  99. src2 += stride * 4;
  100. #define LOAD_LINE(i) \
  101. { \
  102. vector unsigned char perm##i = vec_lvsl(j##i, src2); \
  103. vector unsigned char v_srcA2##i; \
  104. vector unsigned char v_srcA1##i = vec_ld(j##i, src2); \
  105. if (two_vectors) \
  106. v_srcA2##i = vec_ld(j##i + 16, src2); \
  107. v_srcA##i = \
  108. vec_perm(v_srcA1##i, v_srcA2##i, perm##i); \
  109. v_srcAss##i = \
  110. (vector signed short)vec_mergeh((vector signed char)zero, \
  111. (vector signed char)v_srcA##i); }
  112. #define LOAD_LINE_ALIGNED(i) \
  113. v_srcA##i = vec_ld(j##i, src2); \
  114. v_srcAss##i = \
  115. (vector signed short)vec_mergeh((vector signed char)zero, \
  116. (vector signed char)v_srcA##i)
  117. /* Special-casing the aligned case is worthwhile, as all calls from
  118. * the (transposed) horizontable deblocks will be aligned, in addition
  119. * to the naturally aligned vertical deblocks. */
  120. if (properStride && srcAlign) {
  121. LOAD_LINE_ALIGNED(0);
  122. LOAD_LINE_ALIGNED(1);
  123. LOAD_LINE_ALIGNED(2);
  124. LOAD_LINE_ALIGNED(3);
  125. LOAD_LINE_ALIGNED(4);
  126. LOAD_LINE_ALIGNED(5);
  127. LOAD_LINE_ALIGNED(6);
  128. LOAD_LINE_ALIGNED(7);
  129. } else {
  130. LOAD_LINE(0);
  131. LOAD_LINE(1);
  132. LOAD_LINE(2);
  133. LOAD_LINE(3);
  134. LOAD_LINE(4);
  135. LOAD_LINE(5);
  136. LOAD_LINE(6);
  137. LOAD_LINE(7);
  138. }
  139. #undef LOAD_LINE
  140. #undef LOAD_LINE_ALIGNED
  141. #define ITER(i, j) \
  142. const vector signed short v_diff##i = \
  143. vec_sub(v_srcAss##i, v_srcAss##j); \
  144. const vector signed short v_sum##i = \
  145. vec_add(v_diff##i, v_dcOffset); \
  146. const vector signed short v_comp##i = \
  147. (vector signed short)vec_cmplt((vector unsigned short)v_sum##i, \
  148. v_dcThreshold); \
  149. const vector signed short v_part##i = vec_and(mask, v_comp##i);
  150. {
  151. ITER(0, 1)
  152. ITER(1, 2)
  153. ITER(2, 3)
  154. ITER(3, 4)
  155. ITER(4, 5)
  156. ITER(5, 6)
  157. ITER(6, 7)
  158. v_numEq = vec_sum4s(v_part0, v_numEq);
  159. v_numEq = vec_sum4s(v_part1, v_numEq);
  160. v_numEq = vec_sum4s(v_part2, v_numEq);
  161. v_numEq = vec_sum4s(v_part3, v_numEq);
  162. v_numEq = vec_sum4s(v_part4, v_numEq);
  163. v_numEq = vec_sum4s(v_part5, v_numEq);
  164. v_numEq = vec_sum4s(v_part6, v_numEq);
  165. }
  166. #undef ITER
  167. v_numEq = vec_sums(v_numEq, zero);
  168. v_numEq = vec_splat(v_numEq, 3);
  169. vec_ste(v_numEq, 0, &numEq);
  170. if (numEq > c->ppMode.flatnessThreshold){
  171. const vector unsigned char mmoP1 = (const vector unsigned char)
  172. {0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f,
  173. 0x00, 0x01, 0x12, 0x13, 0x08, 0x09, 0x1A, 0x1B};
  174. const vector unsigned char mmoP2 = (const vector unsigned char)
  175. {0x04, 0x05, 0x16, 0x17, 0x0C, 0x0D, 0x1E, 0x1F,
  176. 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f};
  177. const vector unsigned char mmoP = (const vector unsigned char)
  178. vec_lvsl(8, (unsigned char*)0);
  179. vector signed short mmoL1 = vec_perm(v_srcAss0, v_srcAss2, mmoP1);
  180. vector signed short mmoL2 = vec_perm(v_srcAss4, v_srcAss6, mmoP2);
  181. vector signed short mmoL = vec_perm(mmoL1, mmoL2, mmoP);
  182. vector signed short mmoR1 = vec_perm(v_srcAss5, v_srcAss7, mmoP1);
  183. vector signed short mmoR2 = vec_perm(v_srcAss1, v_srcAss3, mmoP2);
  184. vector signed short mmoR = vec_perm(mmoR1, mmoR2, mmoP);
  185. vector signed short mmoDiff = vec_sub(mmoL, mmoR);
  186. vector unsigned short mmoSum = (vector unsigned short)vec_add(mmoDiff, v2QP);
  187. if (vec_any_gt(mmoSum, v4QP))
  188. return 0;
  189. else
  190. return 1;
  191. }
  192. else return 2;
  193. }
  194. static inline void doVertLowPass_altivec(uint8_t *src, int stride, PPContext *c) {
  195. /*
  196. this code makes no assumption on src or stride.
  197. One could remove the recomputation of the perm
  198. vector by assuming (stride % 16) == 0, unfortunately
  199. this is not always true. Quite a lot of load/stores
  200. can be removed by assuming proper alignment of
  201. src & stride :-(
  202. */
  203. uint8_t *src2 = src;
  204. const vector signed int zero = vec_splat_s32(0);
  205. const int properStride = (stride % 16);
  206. const int srcAlign = ((unsigned long)src2 % 16);
  207. DECLARE_ALIGNED(16, short, qp)[8] = {c->QP};
  208. vector signed short vqp = vec_ld(0, qp);
  209. vector signed short vb0, vb1, vb2, vb3, vb4, vb5, vb6, vb7, vb8, vb9;
  210. vector unsigned char vbA0, av_uninit(vbA1), av_uninit(vbA2), av_uninit(vbA3), av_uninit(vbA4), av_uninit(vbA5), av_uninit(vbA6), av_uninit(vbA7), av_uninit(vbA8), vbA9;
  211. vector unsigned char vbB0, av_uninit(vbB1), av_uninit(vbB2), av_uninit(vbB3), av_uninit(vbB4), av_uninit(vbB5), av_uninit(vbB6), av_uninit(vbB7), av_uninit(vbB8), vbB9;
  212. vector unsigned char vbT0, vbT1, vbT2, vbT3, vbT4, vbT5, vbT6, vbT7, vbT8, vbT9;
  213. vector unsigned char perml0, perml1, perml2, perml3, perml4,
  214. perml5, perml6, perml7, perml8, perml9;
  215. register int j0 = 0,
  216. j1 = stride,
  217. j2 = 2 * stride,
  218. j3 = 3 * stride,
  219. j4 = 4 * stride,
  220. j5 = 5 * stride,
  221. j6 = 6 * stride,
  222. j7 = 7 * stride,
  223. j8 = 8 * stride,
  224. j9 = 9 * stride;
  225. vqp = vec_splat(vqp, 0);
  226. src2 += stride*3;
  227. #define LOAD_LINE(i) \
  228. perml##i = vec_lvsl(i * stride, src2); \
  229. vbA##i = vec_ld(i * stride, src2); \
  230. vbB##i = vec_ld(i * stride + 16, src2); \
  231. vbT##i = vec_perm(vbA##i, vbB##i, perml##i); \
  232. vb##i = \
  233. (vector signed short)vec_mergeh((vector unsigned char)zero, \
  234. (vector unsigned char)vbT##i)
  235. #define LOAD_LINE_ALIGNED(i) \
  236. vbT##i = vec_ld(j##i, src2); \
  237. vb##i = \
  238. (vector signed short)vec_mergeh((vector signed char)zero, \
  239. (vector signed char)vbT##i)
  240. /* Special-casing the aligned case is worthwhile, as all calls from
  241. * the (transposed) horizontable deblocks will be aligned, in addition
  242. * to the naturally aligned vertical deblocks. */
  243. if (properStride && srcAlign) {
  244. LOAD_LINE_ALIGNED(0);
  245. LOAD_LINE_ALIGNED(1);
  246. LOAD_LINE_ALIGNED(2);
  247. LOAD_LINE_ALIGNED(3);
  248. LOAD_LINE_ALIGNED(4);
  249. LOAD_LINE_ALIGNED(5);
  250. LOAD_LINE_ALIGNED(6);
  251. LOAD_LINE_ALIGNED(7);
  252. LOAD_LINE_ALIGNED(8);
  253. LOAD_LINE_ALIGNED(9);
  254. } else {
  255. LOAD_LINE(0);
  256. LOAD_LINE(1);
  257. LOAD_LINE(2);
  258. LOAD_LINE(3);
  259. LOAD_LINE(4);
  260. LOAD_LINE(5);
  261. LOAD_LINE(6);
  262. LOAD_LINE(7);
  263. LOAD_LINE(8);
  264. LOAD_LINE(9);
  265. }
  266. #undef LOAD_LINE
  267. #undef LOAD_LINE_ALIGNED
  268. {
  269. const vector unsigned short v_2 = vec_splat_u16(2);
  270. const vector unsigned short v_4 = vec_splat_u16(4);
  271. const vector signed short v_diff01 = vec_sub(vb0, vb1);
  272. const vector unsigned short v_cmp01 =
  273. (const vector unsigned short) vec_cmplt(vec_abs(v_diff01), vqp);
  274. const vector signed short v_first = vec_sel(vb1, vb0, v_cmp01);
  275. const vector signed short v_diff89 = vec_sub(vb8, vb9);
  276. const vector unsigned short v_cmp89 =
  277. (const vector unsigned short) vec_cmplt(vec_abs(v_diff89), vqp);
  278. const vector signed short v_last = vec_sel(vb8, vb9, v_cmp89);
  279. const vector signed short temp01 = vec_mladd(v_first, (vector signed short)v_4, vb1);
  280. const vector signed short temp02 = vec_add(vb2, vb3);
  281. const vector signed short temp03 = vec_add(temp01, (vector signed short)v_4);
  282. const vector signed short v_sumsB0 = vec_add(temp02, temp03);
  283. const vector signed short temp11 = vec_sub(v_sumsB0, v_first);
  284. const vector signed short v_sumsB1 = vec_add(temp11, vb4);
  285. const vector signed short temp21 = vec_sub(v_sumsB1, v_first);
  286. const vector signed short v_sumsB2 = vec_add(temp21, vb5);
  287. const vector signed short temp31 = vec_sub(v_sumsB2, v_first);
  288. const vector signed short v_sumsB3 = vec_add(temp31, vb6);
  289. const vector signed short temp41 = vec_sub(v_sumsB3, v_first);
  290. const vector signed short v_sumsB4 = vec_add(temp41, vb7);
  291. const vector signed short temp51 = vec_sub(v_sumsB4, vb1);
  292. const vector signed short v_sumsB5 = vec_add(temp51, vb8);
  293. const vector signed short temp61 = vec_sub(v_sumsB5, vb2);
  294. const vector signed short v_sumsB6 = vec_add(temp61, v_last);
  295. const vector signed short temp71 = vec_sub(v_sumsB6, vb3);
  296. const vector signed short v_sumsB7 = vec_add(temp71, v_last);
  297. const vector signed short temp81 = vec_sub(v_sumsB7, vb4);
  298. const vector signed short v_sumsB8 = vec_add(temp81, v_last);
  299. const vector signed short temp91 = vec_sub(v_sumsB8, vb5);
  300. const vector signed short v_sumsB9 = vec_add(temp91, v_last);
  301. #define COMPUTE_VR(i, j, k) \
  302. const vector signed short temps1##i = \
  303. vec_add(v_sumsB##i, v_sumsB##k); \
  304. const vector signed short temps2##i = \
  305. vec_mladd(vb##j, (vector signed short)v_2, temps1##i); \
  306. const vector signed short vr##j = vec_sra(temps2##i, v_4)
  307. COMPUTE_VR(0, 1, 2);
  308. COMPUTE_VR(1, 2, 3);
  309. COMPUTE_VR(2, 3, 4);
  310. COMPUTE_VR(3, 4, 5);
  311. COMPUTE_VR(4, 5, 6);
  312. COMPUTE_VR(5, 6, 7);
  313. COMPUTE_VR(6, 7, 8);
  314. COMPUTE_VR(7, 8, 9);
  315. const vector signed char neg1 = vec_splat_s8(-1);
  316. const vector unsigned char permHH = (const vector unsigned char){0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  317. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  318. #define PACK_AND_STORE(i) \
  319. { const vector unsigned char perms##i = \
  320. vec_lvsr(i * stride, src2); \
  321. const vector unsigned char vf##i = \
  322. vec_packsu(vr##i, (vector signed short)zero); \
  323. const vector unsigned char vg##i = \
  324. vec_perm(vf##i, vbT##i, permHH); \
  325. const vector unsigned char mask##i = \
  326. vec_perm((vector unsigned char)zero, (vector unsigned char)neg1, perms##i); \
  327. const vector unsigned char vg2##i = \
  328. vec_perm(vg##i, vg##i, perms##i); \
  329. const vector unsigned char svA##i = \
  330. vec_sel(vbA##i, vg2##i, mask##i); \
  331. const vector unsigned char svB##i = \
  332. vec_sel(vg2##i, vbB##i, mask##i); \
  333. vec_st(svA##i, i * stride, src2); \
  334. vec_st(svB##i, i * stride + 16, src2);}
  335. #define PACK_AND_STORE_ALIGNED(i) \
  336. { const vector unsigned char vf##i = \
  337. vec_packsu(vr##i, (vector signed short)zero); \
  338. const vector unsigned char vg##i = \
  339. vec_perm(vf##i, vbT##i, permHH); \
  340. vec_st(vg##i, i * stride, src2);}
  341. /* Special-casing the aligned case is worthwhile, as all calls from
  342. * the (transposed) horizontable deblocks will be aligned, in addition
  343. * to the naturally aligned vertical deblocks. */
  344. if (properStride && srcAlign) {
  345. PACK_AND_STORE_ALIGNED(1)
  346. PACK_AND_STORE_ALIGNED(2)
  347. PACK_AND_STORE_ALIGNED(3)
  348. PACK_AND_STORE_ALIGNED(4)
  349. PACK_AND_STORE_ALIGNED(5)
  350. PACK_AND_STORE_ALIGNED(6)
  351. PACK_AND_STORE_ALIGNED(7)
  352. PACK_AND_STORE_ALIGNED(8)
  353. } else {
  354. PACK_AND_STORE(1)
  355. PACK_AND_STORE(2)
  356. PACK_AND_STORE(3)
  357. PACK_AND_STORE(4)
  358. PACK_AND_STORE(5)
  359. PACK_AND_STORE(6)
  360. PACK_AND_STORE(7)
  361. PACK_AND_STORE(8)
  362. }
  363. #undef PACK_AND_STORE
  364. #undef PACK_AND_STORE_ALIGNED
  365. }
  366. }
  367. static inline void doVertDefFilter_altivec(uint8_t src[], int stride, PPContext *c) {
  368. /*
  369. this code makes no assumption on src or stride.
  370. One could remove the recomputation of the perm
  371. vector by assuming (stride % 16) == 0, unfortunately
  372. this is not always true. Quite a lot of load/stores
  373. can be removed by assuming proper alignment of
  374. src & stride :-(
  375. */
  376. uint8_t *src2 = src + stride*3;
  377. const vector signed int zero = vec_splat_s32(0);
  378. DECLARE_ALIGNED(16, short, qp)[8] = {8*c->QP};
  379. vector signed short vqp = vec_splat(
  380. (vector signed short)vec_ld(0, qp), 0);
  381. #define LOAD_LINE(i) \
  382. const vector unsigned char perm##i = \
  383. vec_lvsl(i * stride, src2); \
  384. const vector unsigned char vbA##i = \
  385. vec_ld(i * stride, src2); \
  386. const vector unsigned char vbB##i = \
  387. vec_ld(i * stride + 16, src2); \
  388. const vector unsigned char vbT##i = \
  389. vec_perm(vbA##i, vbB##i, perm##i); \
  390. const vector signed short vb##i = \
  391. (vector signed short)vec_mergeh((vector unsigned char)zero, \
  392. (vector unsigned char)vbT##i)
  393. LOAD_LINE(1);
  394. LOAD_LINE(2);
  395. LOAD_LINE(3);
  396. LOAD_LINE(4);
  397. LOAD_LINE(5);
  398. LOAD_LINE(6);
  399. LOAD_LINE(7);
  400. LOAD_LINE(8);
  401. #undef LOAD_LINE
  402. const vector signed short v_1 = vec_splat_s16(1);
  403. const vector signed short v_2 = vec_splat_s16(2);
  404. const vector signed short v_5 = vec_splat_s16(5);
  405. const vector signed short v_32 = vec_sl(v_1,
  406. (vector unsigned short)v_5);
  407. /* middle energy */
  408. const vector signed short l3minusl6 = vec_sub(vb3, vb6);
  409. const vector signed short l5minusl4 = vec_sub(vb5, vb4);
  410. const vector signed short twotimes_l3minusl6 = vec_mladd(v_2, l3minusl6, (vector signed short)zero);
  411. const vector signed short mE = vec_mladd(v_5, l5minusl4, twotimes_l3minusl6);
  412. const vector signed short absmE = vec_abs(mE);
  413. /* left & right energy */
  414. const vector signed short l1minusl4 = vec_sub(vb1, vb4);
  415. const vector signed short l3minusl2 = vec_sub(vb3, vb2);
  416. const vector signed short l5minusl8 = vec_sub(vb5, vb8);
  417. const vector signed short l7minusl6 = vec_sub(vb7, vb6);
  418. const vector signed short twotimes_l1minusl4 = vec_mladd(v_2, l1minusl4, (vector signed short)zero);
  419. const vector signed short twotimes_l5minusl8 = vec_mladd(v_2, l5minusl8, (vector signed short)zero);
  420. const vector signed short lE = vec_mladd(v_5, l3minusl2, twotimes_l1minusl4);
  421. const vector signed short rE = vec_mladd(v_5, l7minusl6, twotimes_l5minusl8);
  422. /* d */
  423. const vector signed short ddiff = vec_sub(absmE,
  424. vec_min(vec_abs(lE),
  425. vec_abs(rE)));
  426. const vector signed short ddiffclamp = vec_max(ddiff, (vector signed short)zero);
  427. const vector signed short dtimes64 = vec_mladd(v_5, ddiffclamp, v_32);
  428. const vector signed short d = vec_sra(dtimes64, vec_splat_u16(6));
  429. const vector signed short minusd = vec_sub((vector signed short)zero, d);
  430. const vector signed short finald = vec_sel(minusd,
  431. d,
  432. vec_cmpgt(vec_sub((vector signed short)zero, mE),
  433. (vector signed short)zero));
  434. /* q */
  435. const vector signed short qtimes2 = vec_sub(vb4, vb5);
  436. /* for a shift right to behave like /2, we need to add one
  437. to all negative integer */
  438. const vector signed short rounddown = vec_sel((vector signed short)zero,
  439. v_1,
  440. vec_cmplt(qtimes2, (vector signed short)zero));
  441. const vector signed short q = vec_sra(vec_add(qtimes2, rounddown), vec_splat_u16(1));
  442. /* clamp */
  443. const vector signed short dclamp_P1 = vec_max((vector signed short)zero, finald);
  444. const vector signed short dclamp_P = vec_min(dclamp_P1, q);
  445. const vector signed short dclamp_N1 = vec_min((vector signed short)zero, finald);
  446. const vector signed short dclamp_N = vec_max(dclamp_N1, q);
  447. const vector signed short dclampedfinal = vec_sel(dclamp_N,
  448. dclamp_P,
  449. vec_cmpgt(q, (vector signed short)zero));
  450. const vector signed short dornotd = vec_sel((vector signed short)zero,
  451. dclampedfinal,
  452. vec_cmplt(absmE, vqp));
  453. /* add/subtract to l4 and l5 */
  454. const vector signed short vb4minusd = vec_sub(vb4, dornotd);
  455. const vector signed short vb5plusd = vec_add(vb5, dornotd);
  456. /* finally, stores */
  457. const vector unsigned char st4 = vec_packsu(vb4minusd, (vector signed short)zero);
  458. const vector unsigned char st5 = vec_packsu(vb5plusd, (vector signed short)zero);
  459. const vector signed char neg1 = vec_splat_s8(-1);
  460. const vector unsigned char permHH = (const vector unsigned char){0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  461. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  462. #define STORE(i) \
  463. { const vector unsigned char perms##i = \
  464. vec_lvsr(i * stride, src2); \
  465. const vector unsigned char vg##i = \
  466. vec_perm(st##i, vbT##i, permHH); \
  467. const vector unsigned char mask##i = \
  468. vec_perm((vector unsigned char)zero, (vector unsigned char)neg1, perms##i); \
  469. const vector unsigned char vg2##i = \
  470. vec_perm(vg##i, vg##i, perms##i); \
  471. const vector unsigned char svA##i = \
  472. vec_sel(vbA##i, vg2##i, mask##i); \
  473. const vector unsigned char svB##i = \
  474. vec_sel(vg2##i, vbB##i, mask##i); \
  475. vec_st(svA##i, i * stride, src2); \
  476. vec_st(svB##i, i * stride + 16, src2);}
  477. STORE(4)
  478. STORE(5)
  479. }
  480. static inline void dering_altivec(uint8_t src[], int stride, PPContext *c) {
  481. const vector signed int vsint32_8 = vec_splat_s32(8);
  482. const vector unsigned int vuint32_4 = vec_splat_u32(4);
  483. const vector signed char neg1 = vec_splat_s8(-1);
  484. const vector unsigned char permA1 = (vector unsigned char)
  485. {0x00, 0x01, 0x02, 0x10, 0x11, 0x12, 0x1F, 0x1F,
  486. 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F};
  487. const vector unsigned char permA2 = (vector unsigned char)
  488. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x10, 0x11,
  489. 0x12, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F};
  490. const vector unsigned char permA1inc = (vector unsigned char)
  491. {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00,
  492. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  493. const vector unsigned char permA2inc = (vector unsigned char)
  494. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01,
  495. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  496. const vector unsigned char magic = (vector unsigned char)
  497. {0x01, 0x02, 0x01, 0x02, 0x04, 0x02, 0x01, 0x02,
  498. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  499. const vector unsigned char extractPerm = (vector unsigned char)
  500. {0x10, 0x10, 0x10, 0x01, 0x10, 0x10, 0x10, 0x01,
  501. 0x10, 0x10, 0x10, 0x01, 0x10, 0x10, 0x10, 0x01};
  502. const vector unsigned char extractPermInc = (vector unsigned char)
  503. {0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
  504. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01};
  505. const vector unsigned char identity = vec_lvsl(0,(unsigned char *)0);
  506. const vector unsigned char tenRight = (vector unsigned char)
  507. {0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  508. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  509. const vector unsigned char eightLeft = (vector unsigned char)
  510. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  511. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08};
  512. /*
  513. this code makes no assumption on src or stride.
  514. One could remove the recomputation of the perm
  515. vector by assuming (stride % 16) == 0, unfortunately
  516. this is not always true. Quite a lot of load/stores
  517. can be removed by assuming proper alignment of
  518. src & stride :-(
  519. */
  520. uint8_t *srcCopy = src;
  521. DECLARE_ALIGNED(16, uint8_t, dt)[16] = { deringThreshold };
  522. const vector signed int zero = vec_splat_s32(0);
  523. vector unsigned char v_dt = vec_splat(vec_ld(0, dt), 0);
  524. #define LOAD_LINE(i) \
  525. const vector unsigned char perm##i = \
  526. vec_lvsl(i * stride, srcCopy); \
  527. vector unsigned char sA##i = vec_ld(i * stride, srcCopy); \
  528. vector unsigned char sB##i = vec_ld(i * stride + 16, srcCopy); \
  529. vector unsigned char src##i = vec_perm(sA##i, sB##i, perm##i)
  530. LOAD_LINE(0);
  531. LOAD_LINE(1);
  532. LOAD_LINE(2);
  533. LOAD_LINE(3);
  534. LOAD_LINE(4);
  535. LOAD_LINE(5);
  536. LOAD_LINE(6);
  537. LOAD_LINE(7);
  538. LOAD_LINE(8);
  539. LOAD_LINE(9);
  540. #undef LOAD_LINE
  541. vector unsigned char v_avg;
  542. DECLARE_ALIGNED(16, signed int, S)[8];
  543. DECLARE_ALIGNED(16, int, tQP2)[4] = { c->QP/2 + 1 };
  544. vector signed int vQP2 = vec_ld(0, tQP2);
  545. vQP2 = vec_splat(vQP2, 0);
  546. {
  547. const vector unsigned char trunc_perm = (vector unsigned char)
  548. {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  549. 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18};
  550. const vector unsigned char trunc_src12 = vec_perm(src1, src2, trunc_perm);
  551. const vector unsigned char trunc_src34 = vec_perm(src3, src4, trunc_perm);
  552. const vector unsigned char trunc_src56 = vec_perm(src5, src6, trunc_perm);
  553. const vector unsigned char trunc_src78 = vec_perm(src7, src8, trunc_perm);
  554. #define EXTRACT(op) do { \
  555. const vector unsigned char s_1 = vec_##op(trunc_src12, trunc_src34); \
  556. const vector unsigned char s_2 = vec_##op(trunc_src56, trunc_src78); \
  557. const vector unsigned char s_6 = vec_##op(s_1, s_2); \
  558. const vector unsigned char s_8h = vec_mergeh(s_6, s_6); \
  559. const vector unsigned char s_8l = vec_mergel(s_6, s_6); \
  560. const vector unsigned char s_9 = vec_##op(s_8h, s_8l); \
  561. const vector unsigned char s_9h = vec_mergeh(s_9, s_9); \
  562. const vector unsigned char s_9l = vec_mergel(s_9, s_9); \
  563. const vector unsigned char s_10 = vec_##op(s_9h, s_9l); \
  564. const vector unsigned char s_10h = vec_mergeh(s_10, s_10); \
  565. const vector unsigned char s_10l = vec_mergel(s_10, s_10); \
  566. const vector unsigned char s_11 = vec_##op(s_10h, s_10l); \
  567. const vector unsigned char s_11h = vec_mergeh(s_11, s_11); \
  568. const vector unsigned char s_11l = vec_mergel(s_11, s_11); \
  569. v_##op = vec_##op(s_11h, s_11l); \
  570. } while (0)
  571. vector unsigned char v_min;
  572. vector unsigned char v_max;
  573. EXTRACT(min);
  574. EXTRACT(max);
  575. #undef EXTRACT
  576. if (vec_all_lt(vec_sub(v_max, v_min), v_dt))
  577. return;
  578. v_avg = vec_avg(v_min, v_max);
  579. }
  580. {
  581. const vector unsigned short mask1 = (vector unsigned short)
  582. {0x0001, 0x0002, 0x0004, 0x0008,
  583. 0x0010, 0x0020, 0x0040, 0x0080};
  584. const vector unsigned short mask2 = (vector unsigned short)
  585. {0x0100, 0x0200, 0x0000, 0x0000,
  586. 0x0000, 0x0000, 0x0000, 0x0000};
  587. const vector unsigned int vuint32_16 = vec_sl(vec_splat_u32(1), vec_splat_u32(4));
  588. const vector unsigned int vuint32_1 = vec_splat_u32(1);
  589. vector signed int sumA2;
  590. vector signed int sumB2;
  591. vector signed int sum0, sum1, sum2, sum3, sum4;
  592. vector signed int sum5, sum6, sum7, sum8, sum9;
  593. #define COMPARE(i) \
  594. do { \
  595. const vector unsigned char cmp = \
  596. (vector unsigned char)vec_cmpgt(src##i, v_avg); \
  597. const vector unsigned short cmpHi = \
  598. (vector unsigned short)vec_mergeh(cmp, cmp); \
  599. const vector unsigned short cmpLi = \
  600. (vector unsigned short)vec_mergel(cmp, cmp); \
  601. const vector signed short cmpHf = \
  602. (vector signed short)vec_and(cmpHi, mask1); \
  603. const vector signed short cmpLf = \
  604. (vector signed short)vec_and(cmpLi, mask2); \
  605. const vector signed int sump = vec_sum4s(cmpHf, zero); \
  606. const vector signed int sumq = vec_sum4s(cmpLf, sump); \
  607. sum##i = vec_sums(sumq, zero); \
  608. } while (0)
  609. COMPARE(0);
  610. COMPARE(1);
  611. COMPARE(2);
  612. COMPARE(3);
  613. COMPARE(4);
  614. COMPARE(5);
  615. COMPARE(6);
  616. COMPARE(7);
  617. COMPARE(8);
  618. COMPARE(9);
  619. #undef COMPARE
  620. {
  621. const vector signed int sump02 = vec_mergel(sum0, sum2);
  622. const vector signed int sump13 = vec_mergel(sum1, sum3);
  623. const vector signed int sumA = vec_mergel(sump02, sump13);
  624. const vector signed int sump46 = vec_mergel(sum4, sum6);
  625. const vector signed int sump57 = vec_mergel(sum5, sum7);
  626. const vector signed int sumB = vec_mergel(sump46, sump57);
  627. const vector signed int sump8A = vec_mergel(sum8, zero);
  628. const vector signed int sump9B = vec_mergel(sum9, zero);
  629. const vector signed int sumC = vec_mergel(sump8A, sump9B);
  630. const vector signed int tA = vec_sl(vec_nor(zero, sumA), vuint32_16);
  631. const vector signed int tB = vec_sl(vec_nor(zero, sumB), vuint32_16);
  632. const vector signed int tC = vec_sl(vec_nor(zero, sumC), vuint32_16);
  633. const vector signed int t2A = vec_or(sumA, tA);
  634. const vector signed int t2B = vec_or(sumB, tB);
  635. const vector signed int t2C = vec_or(sumC, tC);
  636. const vector signed int t3A = vec_and(vec_sra(t2A, vuint32_1),
  637. vec_sl(t2A, vuint32_1));
  638. const vector signed int t3B = vec_and(vec_sra(t2B, vuint32_1),
  639. vec_sl(t2B, vuint32_1));
  640. const vector signed int t3C = vec_and(vec_sra(t2C, vuint32_1),
  641. vec_sl(t2C, vuint32_1));
  642. const vector signed int yA = vec_and(t2A, t3A);
  643. const vector signed int yB = vec_and(t2B, t3B);
  644. const vector signed int yC = vec_and(t2C, t3C);
  645. const vector unsigned char strangeperm1 = vec_lvsl(4, (unsigned char*)0);
  646. const vector unsigned char strangeperm2 = vec_lvsl(8, (unsigned char*)0);
  647. const vector signed int sumAd4 = vec_perm(yA, yB, strangeperm1);
  648. const vector signed int sumAd8 = vec_perm(yA, yB, strangeperm2);
  649. const vector signed int sumBd4 = vec_perm(yB, yC, strangeperm1);
  650. const vector signed int sumBd8 = vec_perm(yB, yC, strangeperm2);
  651. const vector signed int sumAp = vec_and(yA,
  652. vec_and(sumAd4,sumAd8));
  653. const vector signed int sumBp = vec_and(yB,
  654. vec_and(sumBd4,sumBd8));
  655. sumA2 = vec_or(sumAp,
  656. vec_sra(sumAp,
  657. vuint32_16));
  658. sumB2 = vec_or(sumBp,
  659. vec_sra(sumBp,
  660. vuint32_16));
  661. }
  662. vec_st(sumA2, 0, S);
  663. vec_st(sumB2, 16, S);
  664. }
  665. /* I'm not sure the following is actually faster
  666. than straight, unvectorized C code :-( */
  667. #define F_INIT() \
  668. vector unsigned char tenRightM = tenRight; \
  669. vector unsigned char permA1M = permA1; \
  670. vector unsigned char permA2M = permA2; \
  671. vector unsigned char extractPermM = extractPerm
  672. #define F2(i, j, k, l) \
  673. if (S[i] & (1 << (l+1))) { \
  674. const vector unsigned char a_A = vec_perm(src##i, src##j, permA1M); \
  675. const vector unsigned char a_B = vec_perm(a_A, src##k, permA2M); \
  676. const vector signed int a_sump = \
  677. (vector signed int)vec_msum(a_B, magic, (vector unsigned int)zero);\
  678. vector signed int F = vec_sr(vec_sums(a_sump, vsint32_8), vuint32_4); \
  679. const vector signed int p = \
  680. (vector signed int)vec_perm(src##j, (vector unsigned char)zero, \
  681. extractPermM); \
  682. const vector signed int sum = vec_add(p, vQP2); \
  683. const vector signed int diff = vec_sub(p, vQP2); \
  684. vector signed int newpm; \
  685. vector unsigned char newpm2, mask; \
  686. F = vec_splat(F, 3); \
  687. if (vec_all_lt(sum, F)) \
  688. newpm = sum; \
  689. else if (vec_all_gt(diff, F)) \
  690. newpm = diff; \
  691. else newpm = F; \
  692. newpm2 = vec_splat((vector unsigned char)newpm, 15); \
  693. mask = vec_add(identity, tenRightM); \
  694. src##j = vec_perm(src##j, newpm2, mask); \
  695. } \
  696. permA1M = vec_add(permA1M, permA1inc); \
  697. permA2M = vec_add(permA2M, permA2inc); \
  698. tenRightM = vec_sro(tenRightM, eightLeft); \
  699. extractPermM = vec_add(extractPermM, extractPermInc)
  700. #define ITER(i, j, k) do { \
  701. F_INIT(); \
  702. F2(i, j, k, 0); \
  703. F2(i, j, k, 1); \
  704. F2(i, j, k, 2); \
  705. F2(i, j, k, 3); \
  706. F2(i, j, k, 4); \
  707. F2(i, j, k, 5); \
  708. F2(i, j, k, 6); \
  709. F2(i, j, k, 7); \
  710. } while (0)
  711. ITER(0, 1, 2);
  712. ITER(1, 2, 3);
  713. ITER(2, 3, 4);
  714. ITER(3, 4, 5);
  715. ITER(4, 5, 6);
  716. ITER(5, 6, 7);
  717. ITER(6, 7, 8);
  718. ITER(7, 8, 9);
  719. #define STORE_LINE(i) do { \
  720. const vector unsigned char permST = \
  721. vec_lvsr(i * stride, srcCopy); \
  722. const vector unsigned char maskST = \
  723. vec_perm((vector unsigned char)zero, \
  724. (vector unsigned char)neg1, permST); \
  725. src##i = vec_perm(src##i ,src##i, permST); \
  726. sA##i= vec_sel(sA##i, src##i, maskST); \
  727. sB##i= vec_sel(src##i, sB##i, maskST); \
  728. vec_st(sA##i, i * stride, srcCopy); \
  729. vec_st(sB##i, i * stride + 16, srcCopy); \
  730. } while (0)
  731. STORE_LINE(1);
  732. STORE_LINE(2);
  733. STORE_LINE(3);
  734. STORE_LINE(4);
  735. STORE_LINE(5);
  736. STORE_LINE(6);
  737. STORE_LINE(7);
  738. STORE_LINE(8);
  739. #undef STORE_LINE
  740. #undef ITER
  741. #undef F2
  742. }
  743. #define doHorizLowPass_altivec(a...) doHorizLowPass_C(a)
  744. #define doHorizDefFilter_altivec(a...) doHorizDefFilter_C(a)
  745. #define do_a_deblock_altivec(a...) do_a_deblock_C(a)
  746. static inline void tempNoiseReducer_altivec(uint8_t *src, int stride,
  747. uint8_t *tempBlurred, uint32_t *tempBlurredPast, int *maxNoise)
  748. {
  749. const vector signed char neg1 = vec_splat_s8(-1);
  750. const vector unsigned char permHH = (const vector unsigned char){0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  751. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  752. const vector signed int zero = vec_splat_s32(0);
  753. const vector signed short vsint16_1 = vec_splat_s16(1);
  754. vector signed int v_dp = zero;
  755. vector signed int v_sysdp = zero;
  756. int d, sysd, i;
  757. #define LOAD_LINE(src, i) \
  758. register int j##src##i = i * stride; \
  759. vector unsigned char perm##src##i = vec_lvsl(j##src##i, src); \
  760. const vector unsigned char v_##src##A1##i = vec_ld(j##src##i, src); \
  761. const vector unsigned char v_##src##A2##i = vec_ld(j##src##i + 16, src); \
  762. const vector unsigned char v_##src##A##i = \
  763. vec_perm(v_##src##A1##i, v_##src##A2##i, perm##src##i); \
  764. vector signed short v_##src##Ass##i = \
  765. (vector signed short)vec_mergeh((vector signed char)zero, \
  766. (vector signed char)v_##src##A##i)
  767. LOAD_LINE(src, 0);
  768. LOAD_LINE(src, 1);
  769. LOAD_LINE(src, 2);
  770. LOAD_LINE(src, 3);
  771. LOAD_LINE(src, 4);
  772. LOAD_LINE(src, 5);
  773. LOAD_LINE(src, 6);
  774. LOAD_LINE(src, 7);
  775. LOAD_LINE(tempBlurred, 0);
  776. LOAD_LINE(tempBlurred, 1);
  777. LOAD_LINE(tempBlurred, 2);
  778. LOAD_LINE(tempBlurred, 3);
  779. LOAD_LINE(tempBlurred, 4);
  780. LOAD_LINE(tempBlurred, 5);
  781. LOAD_LINE(tempBlurred, 6);
  782. LOAD_LINE(tempBlurred, 7);
  783. #undef LOAD_LINE
  784. #define ACCUMULATE_DIFFS(i) do { \
  785. vector signed short v_d = vec_sub(v_tempBlurredAss##i, \
  786. v_srcAss##i); \
  787. v_dp = vec_msums(v_d, v_d, v_dp); \
  788. v_sysdp = vec_msums(v_d, vsint16_1, v_sysdp); \
  789. } while (0)
  790. ACCUMULATE_DIFFS(0);
  791. ACCUMULATE_DIFFS(1);
  792. ACCUMULATE_DIFFS(2);
  793. ACCUMULATE_DIFFS(3);
  794. ACCUMULATE_DIFFS(4);
  795. ACCUMULATE_DIFFS(5);
  796. ACCUMULATE_DIFFS(6);
  797. ACCUMULATE_DIFFS(7);
  798. #undef ACCUMULATE_DIFFS
  799. tempBlurredPast[127]= maxNoise[0];
  800. tempBlurredPast[128]= maxNoise[1];
  801. tempBlurredPast[129]= maxNoise[2];
  802. v_dp = vec_sums(v_dp, zero);
  803. v_sysdp = vec_sums(v_sysdp, zero);
  804. v_dp = vec_splat(v_dp, 3);
  805. v_sysdp = vec_splat(v_sysdp, 3);
  806. vec_ste(v_dp, 0, &d);
  807. vec_ste(v_sysdp, 0, &sysd);
  808. i = d;
  809. d = (4*d
  810. +(*(tempBlurredPast-256))
  811. +(*(tempBlurredPast-1))+ (*(tempBlurredPast+1))
  812. +(*(tempBlurredPast+256))
  813. +4)>>3;
  814. *tempBlurredPast=i;
  815. if (d > maxNoise[1]) {
  816. if (d < maxNoise[2]) {
  817. #define OP(i) v_tempBlurredAss##i = vec_avg(v_tempBlurredAss##i, v_srcAss##i);
  818. OP(0);
  819. OP(1);
  820. OP(2);
  821. OP(3);
  822. OP(4);
  823. OP(5);
  824. OP(6);
  825. OP(7);
  826. #undef OP
  827. } else {
  828. #define OP(i) v_tempBlurredAss##i = v_srcAss##i;
  829. OP(0);
  830. OP(1);
  831. OP(2);
  832. OP(3);
  833. OP(4);
  834. OP(5);
  835. OP(6);
  836. OP(7);
  837. #undef OP
  838. }
  839. } else {
  840. if (d < maxNoise[0]) {
  841. const vector signed short vsint16_7 = vec_splat_s16(7);
  842. const vector signed short vsint16_4 = vec_splat_s16(4);
  843. const vector unsigned short vuint16_3 = vec_splat_u16(3);
  844. #define OP(i) do { \
  845. const vector signed short v_temp = \
  846. vec_mladd(v_tempBlurredAss##i, vsint16_7, v_srcAss##i); \
  847. const vector signed short v_temp2 = vec_add(v_temp, vsint16_4); \
  848. v_tempBlurredAss##i = vec_sr(v_temp2, vuint16_3); \
  849. } while (0)
  850. OP(0);
  851. OP(1);
  852. OP(2);
  853. OP(3);
  854. OP(4);
  855. OP(5);
  856. OP(6);
  857. OP(7);
  858. #undef OP
  859. } else {
  860. const vector signed short vsint16_3 = vec_splat_s16(3);
  861. const vector signed short vsint16_2 = vec_splat_s16(2);
  862. #define OP(i) do { \
  863. const vector signed short v_temp = \
  864. vec_mladd(v_tempBlurredAss##i, vsint16_3, v_srcAss##i); \
  865. const vector signed short v_temp2 = vec_add(v_temp, vsint16_2); \
  866. v_tempBlurredAss##i = \
  867. vec_sr(v_temp2, (vector unsigned short)vsint16_2); \
  868. } while (0)
  869. OP(0);
  870. OP(1);
  871. OP(2);
  872. OP(3);
  873. OP(4);
  874. OP(5);
  875. OP(6);
  876. OP(7);
  877. #undef OP
  878. }
  879. }
  880. #define PACK_AND_STORE(src, i) do { \
  881. const vector unsigned char perms = vec_lvsr(i * stride, src); \
  882. const vector unsigned char vf = \
  883. vec_packsu(v_tempBlurredAss##1, (vector signed short)zero); \
  884. const vector unsigned char vg = vec_perm(vf, v_##src##A##i, permHH); \
  885. const vector unsigned char mask = \
  886. vec_perm((vector unsigned char)zero, (vector unsigned char)neg1, perms); \
  887. const vector unsigned char vg2 = vec_perm(vg, vg, perms); \
  888. const vector unsigned char svA = vec_sel(v_##src##A1##i, vg2, mask); \
  889. const vector unsigned char svB = vec_sel(vg2, v_##src##A2##i, mask); \
  890. vec_st(svA, i * stride, src); \
  891. vec_st(svB, i * stride + 16, src); \
  892. } while (0)
  893. PACK_AND_STORE(src, 0);
  894. PACK_AND_STORE(src, 1);
  895. PACK_AND_STORE(src, 2);
  896. PACK_AND_STORE(src, 3);
  897. PACK_AND_STORE(src, 4);
  898. PACK_AND_STORE(src, 5);
  899. PACK_AND_STORE(src, 6);
  900. PACK_AND_STORE(src, 7);
  901. PACK_AND_STORE(tempBlurred, 0);
  902. PACK_AND_STORE(tempBlurred, 1);
  903. PACK_AND_STORE(tempBlurred, 2);
  904. PACK_AND_STORE(tempBlurred, 3);
  905. PACK_AND_STORE(tempBlurred, 4);
  906. PACK_AND_STORE(tempBlurred, 5);
  907. PACK_AND_STORE(tempBlurred, 6);
  908. PACK_AND_STORE(tempBlurred, 7);
  909. #undef PACK_AND_STORE
  910. }
  911. static inline void transpose_16x8_char_toPackedAlign_altivec(unsigned char* dst, unsigned char* src, int stride) {
  912. const vector unsigned char zero = vec_splat_u8(0);
  913. #define LOAD_DOUBLE_LINE(i, j) \
  914. vector unsigned char perm1##i = vec_lvsl(i * stride, src); \
  915. vector unsigned char perm2##i = vec_lvsl(j * stride, src); \
  916. vector unsigned char srcA##i = vec_ld(i * stride, src); \
  917. vector unsigned char srcB##i = vec_ld(i * stride + 16, src); \
  918. vector unsigned char srcC##i = vec_ld(j * stride, src); \
  919. vector unsigned char srcD##i = vec_ld(j * stride+ 16, src); \
  920. vector unsigned char src##i = vec_perm(srcA##i, srcB##i, perm1##i); \
  921. vector unsigned char src##j = vec_perm(srcC##i, srcD##i, perm2##i)
  922. LOAD_DOUBLE_LINE(0, 1);
  923. LOAD_DOUBLE_LINE(2, 3);
  924. LOAD_DOUBLE_LINE(4, 5);
  925. LOAD_DOUBLE_LINE(6, 7);
  926. #undef LOAD_DOUBLE_LINE
  927. vector unsigned char tempA = vec_mergeh(src0, zero);
  928. vector unsigned char tempB = vec_mergel(src0, zero);
  929. vector unsigned char tempC = vec_mergeh(src1, zero);
  930. vector unsigned char tempD = vec_mergel(src1, zero);
  931. vector unsigned char tempE = vec_mergeh(src2, zero);
  932. vector unsigned char tempF = vec_mergel(src2, zero);
  933. vector unsigned char tempG = vec_mergeh(src3, zero);
  934. vector unsigned char tempH = vec_mergel(src3, zero);
  935. vector unsigned char tempI = vec_mergeh(src4, zero);
  936. vector unsigned char tempJ = vec_mergel(src4, zero);
  937. vector unsigned char tempK = vec_mergeh(src5, zero);
  938. vector unsigned char tempL = vec_mergel(src5, zero);
  939. vector unsigned char tempM = vec_mergeh(src6, zero);
  940. vector unsigned char tempN = vec_mergel(src6, zero);
  941. vector unsigned char tempO = vec_mergeh(src7, zero);
  942. vector unsigned char tempP = vec_mergel(src7, zero);
  943. vector unsigned char temp0 = vec_mergeh(tempA, tempI);
  944. vector unsigned char temp1 = vec_mergel(tempA, tempI);
  945. vector unsigned char temp2 = vec_mergeh(tempB, tempJ);
  946. vector unsigned char temp3 = vec_mergel(tempB, tempJ);
  947. vector unsigned char temp4 = vec_mergeh(tempC, tempK);
  948. vector unsigned char temp5 = vec_mergel(tempC, tempK);
  949. vector unsigned char temp6 = vec_mergeh(tempD, tempL);
  950. vector unsigned char temp7 = vec_mergel(tempD, tempL);
  951. vector unsigned char temp8 = vec_mergeh(tempE, tempM);
  952. vector unsigned char temp9 = vec_mergel(tempE, tempM);
  953. vector unsigned char temp10 = vec_mergeh(tempF, tempN);
  954. vector unsigned char temp11 = vec_mergel(tempF, tempN);
  955. vector unsigned char temp12 = vec_mergeh(tempG, tempO);
  956. vector unsigned char temp13 = vec_mergel(tempG, tempO);
  957. vector unsigned char temp14 = vec_mergeh(tempH, tempP);
  958. vector unsigned char temp15 = vec_mergel(tempH, tempP);
  959. tempA = vec_mergeh(temp0, temp8);
  960. tempB = vec_mergel(temp0, temp8);
  961. tempC = vec_mergeh(temp1, temp9);
  962. tempD = vec_mergel(temp1, temp9);
  963. tempE = vec_mergeh(temp2, temp10);
  964. tempF = vec_mergel(temp2, temp10);
  965. tempG = vec_mergeh(temp3, temp11);
  966. tempH = vec_mergel(temp3, temp11);
  967. tempI = vec_mergeh(temp4, temp12);
  968. tempJ = vec_mergel(temp4, temp12);
  969. tempK = vec_mergeh(temp5, temp13);
  970. tempL = vec_mergel(temp5, temp13);
  971. tempM = vec_mergeh(temp6, temp14);
  972. tempN = vec_mergel(temp6, temp14);
  973. tempO = vec_mergeh(temp7, temp15);
  974. tempP = vec_mergel(temp7, temp15);
  975. temp0 = vec_mergeh(tempA, tempI);
  976. temp1 = vec_mergel(tempA, tempI);
  977. temp2 = vec_mergeh(tempB, tempJ);
  978. temp3 = vec_mergel(tempB, tempJ);
  979. temp4 = vec_mergeh(tempC, tempK);
  980. temp5 = vec_mergel(tempC, tempK);
  981. temp6 = vec_mergeh(tempD, tempL);
  982. temp7 = vec_mergel(tempD, tempL);
  983. temp8 = vec_mergeh(tempE, tempM);
  984. temp9 = vec_mergel(tempE, tempM);
  985. temp10 = vec_mergeh(tempF, tempN);
  986. temp11 = vec_mergel(tempF, tempN);
  987. temp12 = vec_mergeh(tempG, tempO);
  988. temp13 = vec_mergel(tempG, tempO);
  989. temp14 = vec_mergeh(tempH, tempP);
  990. temp15 = vec_mergel(tempH, tempP);
  991. vec_st(temp0, 0, dst);
  992. vec_st(temp1, 16, dst);
  993. vec_st(temp2, 32, dst);
  994. vec_st(temp3, 48, dst);
  995. vec_st(temp4, 64, dst);
  996. vec_st(temp5, 80, dst);
  997. vec_st(temp6, 96, dst);
  998. vec_st(temp7, 112, dst);
  999. vec_st(temp8, 128, dst);
  1000. vec_st(temp9, 144, dst);
  1001. vec_st(temp10, 160, dst);
  1002. vec_st(temp11, 176, dst);
  1003. vec_st(temp12, 192, dst);
  1004. vec_st(temp13, 208, dst);
  1005. vec_st(temp14, 224, dst);
  1006. vec_st(temp15, 240, dst);
  1007. }
  1008. static inline void transpose_8x16_char_fromPackedAlign_altivec(unsigned char* dst, unsigned char* src, int stride) {
  1009. const vector unsigned char zero = vec_splat_u8(0);
  1010. const vector signed char neg1 = vec_splat_s8(-1);
  1011. #define LOAD_DOUBLE_LINE(i, j) \
  1012. vector unsigned char src##i = vec_ld(i * 16, src); \
  1013. vector unsigned char src##j = vec_ld(j * 16, src)
  1014. LOAD_DOUBLE_LINE(0, 1);
  1015. LOAD_DOUBLE_LINE(2, 3);
  1016. LOAD_DOUBLE_LINE(4, 5);
  1017. LOAD_DOUBLE_LINE(6, 7);
  1018. LOAD_DOUBLE_LINE(8, 9);
  1019. LOAD_DOUBLE_LINE(10, 11);
  1020. LOAD_DOUBLE_LINE(12, 13);
  1021. LOAD_DOUBLE_LINE(14, 15);
  1022. #undef LOAD_DOUBLE_LINE
  1023. vector unsigned char tempA = vec_mergeh(src0, src8);
  1024. vector unsigned char tempB;
  1025. vector unsigned char tempC = vec_mergeh(src1, src9);
  1026. vector unsigned char tempD;
  1027. vector unsigned char tempE = vec_mergeh(src2, src10);
  1028. vector unsigned char tempG = vec_mergeh(src3, src11);
  1029. vector unsigned char tempI = vec_mergeh(src4, src12);
  1030. vector unsigned char tempJ;
  1031. vector unsigned char tempK = vec_mergeh(src5, src13);
  1032. vector unsigned char tempL;
  1033. vector unsigned char tempM = vec_mergeh(src6, src14);
  1034. vector unsigned char tempO = vec_mergeh(src7, src15);
  1035. vector unsigned char temp0 = vec_mergeh(tempA, tempI);
  1036. vector unsigned char temp1 = vec_mergel(tempA, tempI);
  1037. vector unsigned char temp2;
  1038. vector unsigned char temp3;
  1039. vector unsigned char temp4 = vec_mergeh(tempC, tempK);
  1040. vector unsigned char temp5 = vec_mergel(tempC, tempK);
  1041. vector unsigned char temp6;
  1042. vector unsigned char temp7;
  1043. vector unsigned char temp8 = vec_mergeh(tempE, tempM);
  1044. vector unsigned char temp9 = vec_mergel(tempE, tempM);
  1045. vector unsigned char temp12 = vec_mergeh(tempG, tempO);
  1046. vector unsigned char temp13 = vec_mergel(tempG, tempO);
  1047. tempA = vec_mergeh(temp0, temp8);
  1048. tempB = vec_mergel(temp0, temp8);
  1049. tempC = vec_mergeh(temp1, temp9);
  1050. tempD = vec_mergel(temp1, temp9);
  1051. tempI = vec_mergeh(temp4, temp12);
  1052. tempJ = vec_mergel(temp4, temp12);
  1053. tempK = vec_mergeh(temp5, temp13);
  1054. tempL = vec_mergel(temp5, temp13);
  1055. temp0 = vec_mergeh(tempA, tempI);
  1056. temp1 = vec_mergel(tempA, tempI);
  1057. temp2 = vec_mergeh(tempB, tempJ);
  1058. temp3 = vec_mergel(tempB, tempJ);
  1059. temp4 = vec_mergeh(tempC, tempK);
  1060. temp5 = vec_mergel(tempC, tempK);
  1061. temp6 = vec_mergeh(tempD, tempL);
  1062. temp7 = vec_mergel(tempD, tempL);
  1063. #define STORE_DOUBLE_LINE(i, j) do { \
  1064. vector unsigned char dstAi = vec_ld(i * stride, dst); \
  1065. vector unsigned char dstBi = vec_ld(i * stride + 16, dst); \
  1066. vector unsigned char dstAj = vec_ld(j * stride, dst); \
  1067. vector unsigned char dstBj = vec_ld(j * stride+ 16, dst); \
  1068. vector unsigned char aligni = vec_lvsr(i * stride, dst); \
  1069. vector unsigned char alignj = vec_lvsr(j * stride, dst); \
  1070. vector unsigned char maski = \
  1071. vec_perm(zero, (vector unsigned char)neg1, aligni); \
  1072. vector unsigned char maskj = \
  1073. vec_perm(zero, (vector unsigned char)neg1, alignj); \
  1074. vector unsigned char dstRi = vec_perm(temp##i, temp##i, aligni); \
  1075. vector unsigned char dstRj = vec_perm(temp##j, temp##j, alignj); \
  1076. vector unsigned char dstAFi = vec_sel(dstAi, dstRi, maski); \
  1077. vector unsigned char dstBFi = vec_sel(dstRi, dstBi, maski); \
  1078. vector unsigned char dstAFj = vec_sel(dstAj, dstRj, maskj); \
  1079. vector unsigned char dstBFj = vec_sel(dstRj, dstBj, maskj); \
  1080. vec_st(dstAFi, i * stride, dst); \
  1081. vec_st(dstBFi, i * stride + 16, dst); \
  1082. vec_st(dstAFj, j * stride, dst); \
  1083. vec_st(dstBFj, j * stride + 16, dst); \
  1084. } while (0)
  1085. STORE_DOUBLE_LINE(0,1);
  1086. STORE_DOUBLE_LINE(2,3);
  1087. STORE_DOUBLE_LINE(4,5);
  1088. STORE_DOUBLE_LINE(6,7);
  1089. }