h264_altivec.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141
  1. /*
  2. * Copyright (c) 2004 Romain Dolbeau <romain@dolbeau.org>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "libavcodec/dsputil.h"
  21. #include "libavcodec/h264data.h"
  22. #include "gcc_fixes.h"
  23. #include "dsputil_ppc.h"
  24. #include "dsputil_altivec.h"
  25. #include "util_altivec.h"
  26. #include "types_altivec.h"
  27. #define PUT_OP_U8_ALTIVEC(d, s, dst) d = s
  28. #define AVG_OP_U8_ALTIVEC(d, s, dst) d = vec_avg(dst, s)
  29. #define OP_U8_ALTIVEC PUT_OP_U8_ALTIVEC
  30. #define PREFIX_h264_chroma_mc8_altivec put_h264_chroma_mc8_altivec
  31. #define PREFIX_h264_chroma_mc8_num altivec_put_h264_chroma_mc8_num
  32. #define PREFIX_h264_qpel16_h_lowpass_altivec put_h264_qpel16_h_lowpass_altivec
  33. #define PREFIX_h264_qpel16_h_lowpass_num altivec_put_h264_qpel16_h_lowpass_num
  34. #define PREFIX_h264_qpel16_v_lowpass_altivec put_h264_qpel16_v_lowpass_altivec
  35. #define PREFIX_h264_qpel16_v_lowpass_num altivec_put_h264_qpel16_v_lowpass_num
  36. #define PREFIX_h264_qpel16_hv_lowpass_altivec put_h264_qpel16_hv_lowpass_altivec
  37. #define PREFIX_h264_qpel16_hv_lowpass_num altivec_put_h264_qpel16_hv_lowpass_num
  38. #include "h264_template_altivec.c"
  39. #undef OP_U8_ALTIVEC
  40. #undef PREFIX_h264_chroma_mc8_altivec
  41. #undef PREFIX_h264_chroma_mc8_num
  42. #undef PREFIX_h264_qpel16_h_lowpass_altivec
  43. #undef PREFIX_h264_qpel16_h_lowpass_num
  44. #undef PREFIX_h264_qpel16_v_lowpass_altivec
  45. #undef PREFIX_h264_qpel16_v_lowpass_num
  46. #undef PREFIX_h264_qpel16_hv_lowpass_altivec
  47. #undef PREFIX_h264_qpel16_hv_lowpass_num
  48. #define OP_U8_ALTIVEC AVG_OP_U8_ALTIVEC
  49. #define PREFIX_h264_chroma_mc8_altivec avg_h264_chroma_mc8_altivec
  50. #define PREFIX_h264_chroma_mc8_num altivec_avg_h264_chroma_mc8_num
  51. #define PREFIX_h264_qpel16_h_lowpass_altivec avg_h264_qpel16_h_lowpass_altivec
  52. #define PREFIX_h264_qpel16_h_lowpass_num altivec_avg_h264_qpel16_h_lowpass_num
  53. #define PREFIX_h264_qpel16_v_lowpass_altivec avg_h264_qpel16_v_lowpass_altivec
  54. #define PREFIX_h264_qpel16_v_lowpass_num altivec_avg_h264_qpel16_v_lowpass_num
  55. #define PREFIX_h264_qpel16_hv_lowpass_altivec avg_h264_qpel16_hv_lowpass_altivec
  56. #define PREFIX_h264_qpel16_hv_lowpass_num altivec_avg_h264_qpel16_hv_lowpass_num
  57. #include "h264_template_altivec.c"
  58. #undef OP_U8_ALTIVEC
  59. #undef PREFIX_h264_chroma_mc8_altivec
  60. #undef PREFIX_h264_chroma_mc8_num
  61. #undef PREFIX_h264_qpel16_h_lowpass_altivec
  62. #undef PREFIX_h264_qpel16_h_lowpass_num
  63. #undef PREFIX_h264_qpel16_v_lowpass_altivec
  64. #undef PREFIX_h264_qpel16_v_lowpass_num
  65. #undef PREFIX_h264_qpel16_hv_lowpass_altivec
  66. #undef PREFIX_h264_qpel16_hv_lowpass_num
  67. #define H264_MC(OPNAME, SIZE, CODETYPE) \
  68. static void OPNAME ## h264_qpel ## SIZE ## _mc00_ ## CODETYPE (uint8_t *dst, uint8_t *src, int stride){\
  69. OPNAME ## pixels ## SIZE ## _ ## CODETYPE(dst, src, stride, SIZE);\
  70. }\
  71. \
  72. static void OPNAME ## h264_qpel ## SIZE ## _mc10_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){ \
  73. DECLARE_ALIGNED_16(uint8_t, half[SIZE*SIZE]);\
  74. put_h264_qpel ## SIZE ## _h_lowpass_ ## CODETYPE(half, src, SIZE, stride);\
  75. OPNAME ## pixels ## SIZE ## _l2_ ## CODETYPE(dst, src, half, stride, stride, SIZE);\
  76. }\
  77. \
  78. static void OPNAME ## h264_qpel ## SIZE ## _mc20_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){\
  79. OPNAME ## h264_qpel ## SIZE ## _h_lowpass_ ## CODETYPE(dst, src, stride, stride);\
  80. }\
  81. \
  82. static void OPNAME ## h264_qpel ## SIZE ## _mc30_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){\
  83. DECLARE_ALIGNED_16(uint8_t, half[SIZE*SIZE]);\
  84. put_h264_qpel ## SIZE ## _h_lowpass_ ## CODETYPE(half, src, SIZE, stride);\
  85. OPNAME ## pixels ## SIZE ## _l2_ ## CODETYPE(dst, src+1, half, stride, stride, SIZE);\
  86. }\
  87. \
  88. static void OPNAME ## h264_qpel ## SIZE ## _mc01_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){\
  89. DECLARE_ALIGNED_16(uint8_t, half[SIZE*SIZE]);\
  90. put_h264_qpel ## SIZE ## _v_lowpass_ ## CODETYPE(half, src, SIZE, stride);\
  91. OPNAME ## pixels ## SIZE ## _l2_ ## CODETYPE(dst, src, half, stride, stride, SIZE);\
  92. }\
  93. \
  94. static void OPNAME ## h264_qpel ## SIZE ## _mc02_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){\
  95. OPNAME ## h264_qpel ## SIZE ## _v_lowpass_ ## CODETYPE(dst, src, stride, stride);\
  96. }\
  97. \
  98. static void OPNAME ## h264_qpel ## SIZE ## _mc03_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){\
  99. DECLARE_ALIGNED_16(uint8_t, half[SIZE*SIZE]);\
  100. put_h264_qpel ## SIZE ## _v_lowpass_ ## CODETYPE(half, src, SIZE, stride);\
  101. OPNAME ## pixels ## SIZE ## _l2_ ## CODETYPE(dst, src+stride, half, stride, stride, SIZE);\
  102. }\
  103. \
  104. static void OPNAME ## h264_qpel ## SIZE ## _mc11_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){\
  105. DECLARE_ALIGNED_16(uint8_t, halfH[SIZE*SIZE]);\
  106. DECLARE_ALIGNED_16(uint8_t, halfV[SIZE*SIZE]);\
  107. put_h264_qpel ## SIZE ## _h_lowpass_ ## CODETYPE(halfH, src, SIZE, stride);\
  108. put_h264_qpel ## SIZE ## _v_lowpass_ ## CODETYPE(halfV, src, SIZE, stride);\
  109. OPNAME ## pixels ## SIZE ## _l2_ ## CODETYPE(dst, halfH, halfV, stride, SIZE, SIZE);\
  110. }\
  111. \
  112. static void OPNAME ## h264_qpel ## SIZE ## _mc31_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){\
  113. DECLARE_ALIGNED_16(uint8_t, halfH[SIZE*SIZE]);\
  114. DECLARE_ALIGNED_16(uint8_t, halfV[SIZE*SIZE]);\
  115. put_h264_qpel ## SIZE ## _h_lowpass_ ## CODETYPE(halfH, src, SIZE, stride);\
  116. put_h264_qpel ## SIZE ## _v_lowpass_ ## CODETYPE(halfV, src+1, SIZE, stride);\
  117. OPNAME ## pixels ## SIZE ## _l2_ ## CODETYPE(dst, halfH, halfV, stride, SIZE, SIZE);\
  118. }\
  119. \
  120. static void OPNAME ## h264_qpel ## SIZE ## _mc13_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){\
  121. DECLARE_ALIGNED_16(uint8_t, halfH[SIZE*SIZE]);\
  122. DECLARE_ALIGNED_16(uint8_t, halfV[SIZE*SIZE]);\
  123. put_h264_qpel ## SIZE ## _h_lowpass_ ## CODETYPE(halfH, src + stride, SIZE, stride);\
  124. put_h264_qpel ## SIZE ## _v_lowpass_ ## CODETYPE(halfV, src, SIZE, stride);\
  125. OPNAME ## pixels ## SIZE ## _l2_ ## CODETYPE(dst, halfH, halfV, stride, SIZE, SIZE);\
  126. }\
  127. \
  128. static void OPNAME ## h264_qpel ## SIZE ## _mc33_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){\
  129. DECLARE_ALIGNED_16(uint8_t, halfH[SIZE*SIZE]);\
  130. DECLARE_ALIGNED_16(uint8_t, halfV[SIZE*SIZE]);\
  131. put_h264_qpel ## SIZE ## _h_lowpass_ ## CODETYPE(halfH, src + stride, SIZE, stride);\
  132. put_h264_qpel ## SIZE ## _v_lowpass_ ## CODETYPE(halfV, src+1, SIZE, stride);\
  133. OPNAME ## pixels ## SIZE ## _l2_ ## CODETYPE(dst, halfH, halfV, stride, SIZE, SIZE);\
  134. }\
  135. \
  136. static void OPNAME ## h264_qpel ## SIZE ## _mc22_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){\
  137. DECLARE_ALIGNED_16(int16_t, tmp[SIZE*(SIZE+8)]);\
  138. OPNAME ## h264_qpel ## SIZE ## _hv_lowpass_ ## CODETYPE(dst, tmp, src, stride, SIZE, stride);\
  139. }\
  140. \
  141. static void OPNAME ## h264_qpel ## SIZE ## _mc21_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){\
  142. DECLARE_ALIGNED_16(uint8_t, halfH[SIZE*SIZE]);\
  143. DECLARE_ALIGNED_16(uint8_t, halfHV[SIZE*SIZE]);\
  144. DECLARE_ALIGNED_16(int16_t, tmp[SIZE*(SIZE+8)]);\
  145. put_h264_qpel ## SIZE ## _h_lowpass_ ## CODETYPE(halfH, src, SIZE, stride);\
  146. put_h264_qpel ## SIZE ## _hv_lowpass_ ## CODETYPE(halfHV, tmp, src, SIZE, SIZE, stride);\
  147. OPNAME ## pixels ## SIZE ## _l2_ ## CODETYPE(dst, halfH, halfHV, stride, SIZE, SIZE);\
  148. }\
  149. \
  150. static void OPNAME ## h264_qpel ## SIZE ## _mc23_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){\
  151. DECLARE_ALIGNED_16(uint8_t, halfH[SIZE*SIZE]);\
  152. DECLARE_ALIGNED_16(uint8_t, halfHV[SIZE*SIZE]);\
  153. DECLARE_ALIGNED_16(int16_t, tmp[SIZE*(SIZE+8)]);\
  154. put_h264_qpel ## SIZE ## _h_lowpass_ ## CODETYPE(halfH, src + stride, SIZE, stride);\
  155. put_h264_qpel ## SIZE ## _hv_lowpass_ ## CODETYPE(halfHV, tmp, src, SIZE, SIZE, stride);\
  156. OPNAME ## pixels ## SIZE ## _l2_ ## CODETYPE(dst, halfH, halfHV, stride, SIZE, SIZE);\
  157. }\
  158. \
  159. static void OPNAME ## h264_qpel ## SIZE ## _mc12_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){\
  160. DECLARE_ALIGNED_16(uint8_t, halfV[SIZE*SIZE]);\
  161. DECLARE_ALIGNED_16(uint8_t, halfHV[SIZE*SIZE]);\
  162. DECLARE_ALIGNED_16(int16_t, tmp[SIZE*(SIZE+8)]);\
  163. put_h264_qpel ## SIZE ## _v_lowpass_ ## CODETYPE(halfV, src, SIZE, stride);\
  164. put_h264_qpel ## SIZE ## _hv_lowpass_ ## CODETYPE(halfHV, tmp, src, SIZE, SIZE, stride);\
  165. OPNAME ## pixels ## SIZE ## _l2_ ## CODETYPE(dst, halfV, halfHV, stride, SIZE, SIZE);\
  166. }\
  167. \
  168. static void OPNAME ## h264_qpel ## SIZE ## _mc32_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){\
  169. DECLARE_ALIGNED_16(uint8_t, halfV[SIZE*SIZE]);\
  170. DECLARE_ALIGNED_16(uint8_t, halfHV[SIZE*SIZE]);\
  171. DECLARE_ALIGNED_16(int16_t, tmp[SIZE*(SIZE+8)]);\
  172. put_h264_qpel ## SIZE ## _v_lowpass_ ## CODETYPE(halfV, src+1, SIZE, stride);\
  173. put_h264_qpel ## SIZE ## _hv_lowpass_ ## CODETYPE(halfHV, tmp, src, SIZE, SIZE, stride);\
  174. OPNAME ## pixels ## SIZE ## _l2_ ## CODETYPE(dst, halfV, halfHV, stride, SIZE, SIZE);\
  175. }\
  176. /* this code assume that stride % 16 == 0 */
  177. void put_no_rnd_h264_chroma_mc8_altivec(uint8_t * dst, uint8_t * src, int stride, int h, int x, int y) {
  178. DECLARE_ALIGNED_16(signed int, ABCD[4]) =
  179. {((8 - x) * (8 - y)),
  180. ((x) * (8 - y)),
  181. ((8 - x) * (y)),
  182. ((x) * (y))};
  183. register int i;
  184. vec_u8 fperm;
  185. const vec_s32 vABCD = vec_ld(0, ABCD);
  186. const vec_s16 vA = vec_splat((vec_s16)vABCD, 1);
  187. const vec_s16 vB = vec_splat((vec_s16)vABCD, 3);
  188. const vec_s16 vC = vec_splat((vec_s16)vABCD, 5);
  189. const vec_s16 vD = vec_splat((vec_s16)vABCD, 7);
  190. LOAD_ZERO;
  191. const vec_s16 v28ss = vec_sub(vec_sl(vec_splat_s16(1),vec_splat_u16(5)),vec_splat_s16(4));
  192. const vec_u16 v6us = vec_splat_u16(6);
  193. register int loadSecond = (((unsigned long)src) % 16) <= 7 ? 0 : 1;
  194. register int reallyBadAlign = (((unsigned long)src) % 16) == 15 ? 1 : 0;
  195. vec_u8 vsrcAuc, vsrcBuc, vsrcperm0, vsrcperm1;
  196. vec_u8 vsrc0uc, vsrc1uc;
  197. vec_s16 vsrc0ssH, vsrc1ssH;
  198. vec_u8 vsrcCuc, vsrc2uc, vsrc3uc;
  199. vec_s16 vsrc2ssH, vsrc3ssH, psum;
  200. vec_u8 vdst, ppsum, fsum;
  201. if (((unsigned long)dst) % 16 == 0) {
  202. fperm = (vec_u8){0x10, 0x11, 0x12, 0x13,
  203. 0x14, 0x15, 0x16, 0x17,
  204. 0x08, 0x09, 0x0A, 0x0B,
  205. 0x0C, 0x0D, 0x0E, 0x0F};
  206. } else {
  207. fperm = (vec_u8){0x00, 0x01, 0x02, 0x03,
  208. 0x04, 0x05, 0x06, 0x07,
  209. 0x18, 0x19, 0x1A, 0x1B,
  210. 0x1C, 0x1D, 0x1E, 0x1F};
  211. }
  212. vsrcAuc = vec_ld(0, src);
  213. if (loadSecond)
  214. vsrcBuc = vec_ld(16, src);
  215. vsrcperm0 = vec_lvsl(0, src);
  216. vsrcperm1 = vec_lvsl(1, src);
  217. vsrc0uc = vec_perm(vsrcAuc, vsrcBuc, vsrcperm0);
  218. if (reallyBadAlign)
  219. vsrc1uc = vsrcBuc;
  220. else
  221. vsrc1uc = vec_perm(vsrcAuc, vsrcBuc, vsrcperm1);
  222. vsrc0ssH = (vec_s16)vec_mergeh(zero_u8v, (vec_u8)vsrc0uc);
  223. vsrc1ssH = (vec_s16)vec_mergeh(zero_u8v, (vec_u8)vsrc1uc);
  224. if (!loadSecond) {// -> !reallyBadAlign
  225. for (i = 0 ; i < h ; i++) {
  226. vsrcCuc = vec_ld(stride + 0, src);
  227. vsrc2uc = vec_perm(vsrcCuc, vsrcCuc, vsrcperm0);
  228. vsrc3uc = vec_perm(vsrcCuc, vsrcCuc, vsrcperm1);
  229. vsrc2ssH = (vec_s16)vec_mergeh(zero_u8v, (vec_u8)vsrc2uc);
  230. vsrc3ssH = (vec_s16)vec_mergeh(zero_u8v, (vec_u8)vsrc3uc);
  231. psum = vec_mladd(vA, vsrc0ssH, vec_splat_s16(0));
  232. psum = vec_mladd(vB, vsrc1ssH, psum);
  233. psum = vec_mladd(vC, vsrc2ssH, psum);
  234. psum = vec_mladd(vD, vsrc3ssH, psum);
  235. psum = vec_add(v28ss, psum);
  236. psum = vec_sra(psum, v6us);
  237. vdst = vec_ld(0, dst);
  238. ppsum = (vec_u8)vec_packsu(psum, psum);
  239. fsum = vec_perm(vdst, ppsum, fperm);
  240. vec_st(fsum, 0, dst);
  241. vsrc0ssH = vsrc2ssH;
  242. vsrc1ssH = vsrc3ssH;
  243. dst += stride;
  244. src += stride;
  245. }
  246. } else {
  247. vec_u8 vsrcDuc;
  248. for (i = 0 ; i < h ; i++) {
  249. vsrcCuc = vec_ld(stride + 0, src);
  250. vsrcDuc = vec_ld(stride + 16, src);
  251. vsrc2uc = vec_perm(vsrcCuc, vsrcDuc, vsrcperm0);
  252. if (reallyBadAlign)
  253. vsrc3uc = vsrcDuc;
  254. else
  255. vsrc3uc = vec_perm(vsrcCuc, vsrcDuc, vsrcperm1);
  256. vsrc2ssH = (vec_s16)vec_mergeh(zero_u8v, (vec_u8)vsrc2uc);
  257. vsrc3ssH = (vec_s16)vec_mergeh(zero_u8v, (vec_u8)vsrc3uc);
  258. psum = vec_mladd(vA, vsrc0ssH, vec_splat_s16(0));
  259. psum = vec_mladd(vB, vsrc1ssH, psum);
  260. psum = vec_mladd(vC, vsrc2ssH, psum);
  261. psum = vec_mladd(vD, vsrc3ssH, psum);
  262. psum = vec_add(v28ss, psum);
  263. psum = vec_sr(psum, v6us);
  264. vdst = vec_ld(0, dst);
  265. ppsum = (vec_u8)vec_pack(psum, psum);
  266. fsum = vec_perm(vdst, ppsum, fperm);
  267. vec_st(fsum, 0, dst);
  268. vsrc0ssH = vsrc2ssH;
  269. vsrc1ssH = vsrc3ssH;
  270. dst += stride;
  271. src += stride;
  272. }
  273. }
  274. }
  275. static inline void put_pixels16_l2_altivec( uint8_t * dst, const uint8_t * src1,
  276. const uint8_t * src2, int dst_stride,
  277. int src_stride1, int h)
  278. {
  279. int i;
  280. vec_u8 a, b, d, tmp1, tmp2, mask, mask_, edges, align;
  281. mask_ = vec_lvsl(0, src2);
  282. for (i = 0; i < h; i++) {
  283. tmp1 = vec_ld(i * src_stride1, src1);
  284. mask = vec_lvsl(i * src_stride1, src1);
  285. tmp2 = vec_ld(i * src_stride1 + 15, src1);
  286. a = vec_perm(tmp1, tmp2, mask);
  287. tmp1 = vec_ld(i * 16, src2);
  288. tmp2 = vec_ld(i * 16 + 15, src2);
  289. b = vec_perm(tmp1, tmp2, mask_);
  290. tmp1 = vec_ld(0, dst);
  291. mask = vec_lvsl(0, dst);
  292. tmp2 = vec_ld(15, dst);
  293. d = vec_avg(a, b);
  294. edges = vec_perm(tmp2, tmp1, mask);
  295. align = vec_lvsr(0, dst);
  296. tmp2 = vec_perm(d, edges, align);
  297. tmp1 = vec_perm(edges, d, align);
  298. vec_st(tmp2, 15, dst);
  299. vec_st(tmp1, 0 , dst);
  300. dst += dst_stride;
  301. }
  302. }
  303. static inline void avg_pixels16_l2_altivec( uint8_t * dst, const uint8_t * src1,
  304. const uint8_t * src2, int dst_stride,
  305. int src_stride1, int h)
  306. {
  307. int i;
  308. vec_u8 a, b, d, tmp1, tmp2, mask, mask_, edges, align;
  309. mask_ = vec_lvsl(0, src2);
  310. for (i = 0; i < h; i++) {
  311. tmp1 = vec_ld(i * src_stride1, src1);
  312. mask = vec_lvsl(i * src_stride1, src1);
  313. tmp2 = vec_ld(i * src_stride1 + 15, src1);
  314. a = vec_perm(tmp1, tmp2, mask);
  315. tmp1 = vec_ld(i * 16, src2);
  316. tmp2 = vec_ld(i * 16 + 15, src2);
  317. b = vec_perm(tmp1, tmp2, mask_);
  318. tmp1 = vec_ld(0, dst);
  319. mask = vec_lvsl(0, dst);
  320. tmp2 = vec_ld(15, dst);
  321. d = vec_avg(vec_perm(tmp1, tmp2, mask), vec_avg(a, b));
  322. edges = vec_perm(tmp2, tmp1, mask);
  323. align = vec_lvsr(0, dst);
  324. tmp2 = vec_perm(d, edges, align);
  325. tmp1 = vec_perm(edges, d, align);
  326. vec_st(tmp2, 15, dst);
  327. vec_st(tmp1, 0 , dst);
  328. dst += dst_stride;
  329. }
  330. }
  331. /* Implemented but could be faster
  332. #define put_pixels16_l2_altivec(d,s1,s2,ds,s1s,h) put_pixels16_l2(d,s1,s2,ds,s1s,16,h)
  333. #define avg_pixels16_l2_altivec(d,s1,s2,ds,s1s,h) avg_pixels16_l2(d,s1,s2,ds,s1s,16,h)
  334. */
  335. H264_MC(put_, 16, altivec)
  336. H264_MC(avg_, 16, altivec)
  337. /****************************************************************************
  338. * IDCT transform:
  339. ****************************************************************************/
  340. #define VEC_1D_DCT(vb0,vb1,vb2,vb3,va0,va1,va2,va3) \
  341. /* 1st stage */ \
  342. vz0 = vec_add(vb0,vb2); /* temp[0] = Y[0] + Y[2] */ \
  343. vz1 = vec_sub(vb0,vb2); /* temp[1] = Y[0] - Y[2] */ \
  344. vz2 = vec_sra(vb1,vec_splat_u16(1)); \
  345. vz2 = vec_sub(vz2,vb3); /* temp[2] = Y[1].1/2 - Y[3] */ \
  346. vz3 = vec_sra(vb3,vec_splat_u16(1)); \
  347. vz3 = vec_add(vb1,vz3); /* temp[3] = Y[1] + Y[3].1/2 */ \
  348. /* 2nd stage: output */ \
  349. va0 = vec_add(vz0,vz3); /* x[0] = temp[0] + temp[3] */ \
  350. va1 = vec_add(vz1,vz2); /* x[1] = temp[1] + temp[2] */ \
  351. va2 = vec_sub(vz1,vz2); /* x[2] = temp[1] - temp[2] */ \
  352. va3 = vec_sub(vz0,vz3) /* x[3] = temp[0] - temp[3] */
  353. #define VEC_TRANSPOSE_4(a0,a1,a2,a3,b0,b1,b2,b3) \
  354. b0 = vec_mergeh( a0, a0 ); \
  355. b1 = vec_mergeh( a1, a0 ); \
  356. b2 = vec_mergeh( a2, a0 ); \
  357. b3 = vec_mergeh( a3, a0 ); \
  358. a0 = vec_mergeh( b0, b2 ); \
  359. a1 = vec_mergel( b0, b2 ); \
  360. a2 = vec_mergeh( b1, b3 ); \
  361. a3 = vec_mergel( b1, b3 ); \
  362. b0 = vec_mergeh( a0, a2 ); \
  363. b1 = vec_mergel( a0, a2 ); \
  364. b2 = vec_mergeh( a1, a3 ); \
  365. b3 = vec_mergel( a1, a3 )
  366. #define VEC_LOAD_U8_ADD_S16_STORE_U8(va) \
  367. vdst_orig = vec_ld(0, dst); \
  368. vdst = vec_perm(vdst_orig, zero_u8v, vdst_mask); \
  369. vdst_ss = (vec_s16) vec_mergeh(zero_u8v, vdst); \
  370. va = vec_add(va, vdst_ss); \
  371. va_u8 = vec_packsu(va, zero_s16v); \
  372. va_u32 = vec_splat((vec_u32)va_u8, 0); \
  373. vec_ste(va_u32, element, (uint32_t*)dst);
  374. static void ff_h264_idct_add_altivec(uint8_t *dst, DCTELEM *block, int stride)
  375. {
  376. vec_s16 va0, va1, va2, va3;
  377. vec_s16 vz0, vz1, vz2, vz3;
  378. vec_s16 vtmp0, vtmp1, vtmp2, vtmp3;
  379. vec_u8 va_u8;
  380. vec_u32 va_u32;
  381. vec_s16 vdst_ss;
  382. const vec_u16 v6us = vec_splat_u16(6);
  383. vec_u8 vdst, vdst_orig;
  384. vec_u8 vdst_mask = vec_lvsl(0, dst);
  385. int element = ((unsigned long)dst & 0xf) >> 2;
  386. LOAD_ZERO;
  387. block[0] += 32; /* add 32 as a DC-level for rounding */
  388. vtmp0 = vec_ld(0,block);
  389. vtmp1 = vec_sld(vtmp0, vtmp0, 8);
  390. vtmp2 = vec_ld(16,block);
  391. vtmp3 = vec_sld(vtmp2, vtmp2, 8);
  392. VEC_1D_DCT(vtmp0,vtmp1,vtmp2,vtmp3,va0,va1,va2,va3);
  393. VEC_TRANSPOSE_4(va0,va1,va2,va3,vtmp0,vtmp1,vtmp2,vtmp3);
  394. VEC_1D_DCT(vtmp0,vtmp1,vtmp2,vtmp3,va0,va1,va2,va3);
  395. va0 = vec_sra(va0,v6us);
  396. va1 = vec_sra(va1,v6us);
  397. va2 = vec_sra(va2,v6us);
  398. va3 = vec_sra(va3,v6us);
  399. VEC_LOAD_U8_ADD_S16_STORE_U8(va0);
  400. dst += stride;
  401. VEC_LOAD_U8_ADD_S16_STORE_U8(va1);
  402. dst += stride;
  403. VEC_LOAD_U8_ADD_S16_STORE_U8(va2);
  404. dst += stride;
  405. VEC_LOAD_U8_ADD_S16_STORE_U8(va3);
  406. }
  407. #define IDCT8_1D_ALTIVEC(s0, s1, s2, s3, s4, s5, s6, s7, d0, d1, d2, d3, d4, d5, d6, d7) {\
  408. /* a0 = SRC(0) + SRC(4); */ \
  409. vec_s16 a0v = vec_add(s0, s4); \
  410. /* a2 = SRC(0) - SRC(4); */ \
  411. vec_s16 a2v = vec_sub(s0, s4); \
  412. /* a4 = (SRC(2)>>1) - SRC(6); */ \
  413. vec_s16 a4v = vec_sub(vec_sra(s2, onev), s6); \
  414. /* a6 = (SRC(6)>>1) + SRC(2); */ \
  415. vec_s16 a6v = vec_add(vec_sra(s6, onev), s2); \
  416. /* b0 = a0 + a6; */ \
  417. vec_s16 b0v = vec_add(a0v, a6v); \
  418. /* b2 = a2 + a4; */ \
  419. vec_s16 b2v = vec_add(a2v, a4v); \
  420. /* b4 = a2 - a4; */ \
  421. vec_s16 b4v = vec_sub(a2v, a4v); \
  422. /* b6 = a0 - a6; */ \
  423. vec_s16 b6v = vec_sub(a0v, a6v); \
  424. /* a1 = SRC(5) - SRC(3) - SRC(7) - (SRC(7)>>1); */ \
  425. /* a1 = (SRC(5)-SRC(3)) - (SRC(7) + (SRC(7)>>1)); */ \
  426. vec_s16 a1v = vec_sub( vec_sub(s5, s3), vec_add(s7, vec_sra(s7, onev)) ); \
  427. /* a3 = SRC(7) + SRC(1) - SRC(3) - (SRC(3)>>1); */ \
  428. /* a3 = (SRC(7)+SRC(1)) - (SRC(3) + (SRC(3)>>1)); */ \
  429. vec_s16 a3v = vec_sub( vec_add(s7, s1), vec_add(s3, vec_sra(s3, onev)) );\
  430. /* a5 = SRC(7) - SRC(1) + SRC(5) + (SRC(5)>>1); */ \
  431. /* a5 = (SRC(7)-SRC(1)) + SRC(5) + (SRC(5)>>1); */ \
  432. vec_s16 a5v = vec_add( vec_sub(s7, s1), vec_add(s5, vec_sra(s5, onev)) );\
  433. /* a7 = SRC(5)+SRC(3) + SRC(1) + (SRC(1)>>1); */ \
  434. vec_s16 a7v = vec_add( vec_add(s5, s3), vec_add(s1, vec_sra(s1, onev)) );\
  435. /* b1 = (a7>>2) + a1; */ \
  436. vec_s16 b1v = vec_add( vec_sra(a7v, twov), a1v); \
  437. /* b3 = a3 + (a5>>2); */ \
  438. vec_s16 b3v = vec_add(a3v, vec_sra(a5v, twov)); \
  439. /* b5 = (a3>>2) - a5; */ \
  440. vec_s16 b5v = vec_sub( vec_sra(a3v, twov), a5v); \
  441. /* b7 = a7 - (a1>>2); */ \
  442. vec_s16 b7v = vec_sub( a7v, vec_sra(a1v, twov)); \
  443. /* DST(0, b0 + b7); */ \
  444. d0 = vec_add(b0v, b7v); \
  445. /* DST(1, b2 + b5); */ \
  446. d1 = vec_add(b2v, b5v); \
  447. /* DST(2, b4 + b3); */ \
  448. d2 = vec_add(b4v, b3v); \
  449. /* DST(3, b6 + b1); */ \
  450. d3 = vec_add(b6v, b1v); \
  451. /* DST(4, b6 - b1); */ \
  452. d4 = vec_sub(b6v, b1v); \
  453. /* DST(5, b4 - b3); */ \
  454. d5 = vec_sub(b4v, b3v); \
  455. /* DST(6, b2 - b5); */ \
  456. d6 = vec_sub(b2v, b5v); \
  457. /* DST(7, b0 - b7); */ \
  458. d7 = vec_sub(b0v, b7v); \
  459. }
  460. #define ALTIVEC_STORE_SUM_CLIP(dest, idctv, perm_ldv, perm_stv, sel) { \
  461. /* unaligned load */ \
  462. vec_u8 hv = vec_ld( 0, dest ); \
  463. vec_u8 lv = vec_ld( 7, dest ); \
  464. vec_u8 dstv = vec_perm( hv, lv, (vec_u8)perm_ldv ); \
  465. vec_s16 idct_sh6 = vec_sra(idctv, sixv); \
  466. vec_u16 dst16 = (vec_u16)vec_mergeh(zero_u8v, dstv); \
  467. vec_s16 idstsum = vec_adds(idct_sh6, (vec_s16)dst16); \
  468. vec_u8 idstsum8 = vec_packsu(zero_s16v, idstsum); \
  469. vec_u8 edgehv; \
  470. /* unaligned store */ \
  471. vec_u8 bodyv = vec_perm( idstsum8, idstsum8, perm_stv );\
  472. vec_u8 edgelv = vec_perm( sel, zero_u8v, perm_stv ); \
  473. lv = vec_sel( lv, bodyv, edgelv ); \
  474. vec_st( lv, 7, dest ); \
  475. hv = vec_ld( 0, dest ); \
  476. edgehv = vec_perm( zero_u8v, sel, perm_stv ); \
  477. hv = vec_sel( hv, bodyv, edgehv ); \
  478. vec_st( hv, 0, dest ); \
  479. }
  480. void ff_h264_idct8_add_altivec( uint8_t *dst, DCTELEM *dct, int stride ) {
  481. vec_s16 s0, s1, s2, s3, s4, s5, s6, s7;
  482. vec_s16 d0, d1, d2, d3, d4, d5, d6, d7;
  483. vec_s16 idct0, idct1, idct2, idct3, idct4, idct5, idct6, idct7;
  484. vec_u8 perm_ldv = vec_lvsl(0, dst);
  485. vec_u8 perm_stv = vec_lvsr(8, dst);
  486. const vec_u16 onev = vec_splat_u16(1);
  487. const vec_u16 twov = vec_splat_u16(2);
  488. const vec_u16 sixv = vec_splat_u16(6);
  489. const vec_u8 sel = (vec_u8) {0,0,0,0,0,0,0,0,-1,-1,-1,-1,-1,-1,-1,-1};
  490. LOAD_ZERO;
  491. dct[0] += 32; // rounding for the >>6 at the end
  492. s0 = vec_ld(0x00, (int16_t*)dct);
  493. s1 = vec_ld(0x10, (int16_t*)dct);
  494. s2 = vec_ld(0x20, (int16_t*)dct);
  495. s3 = vec_ld(0x30, (int16_t*)dct);
  496. s4 = vec_ld(0x40, (int16_t*)dct);
  497. s5 = vec_ld(0x50, (int16_t*)dct);
  498. s6 = vec_ld(0x60, (int16_t*)dct);
  499. s7 = vec_ld(0x70, (int16_t*)dct);
  500. IDCT8_1D_ALTIVEC(s0, s1, s2, s3, s4, s5, s6, s7,
  501. d0, d1, d2, d3, d4, d5, d6, d7);
  502. TRANSPOSE8( d0, d1, d2, d3, d4, d5, d6, d7 );
  503. IDCT8_1D_ALTIVEC(d0, d1, d2, d3, d4, d5, d6, d7,
  504. idct0, idct1, idct2, idct3, idct4, idct5, idct6, idct7);
  505. ALTIVEC_STORE_SUM_CLIP(&dst[0*stride], idct0, perm_ldv, perm_stv, sel);
  506. ALTIVEC_STORE_SUM_CLIP(&dst[1*stride], idct1, perm_ldv, perm_stv, sel);
  507. ALTIVEC_STORE_SUM_CLIP(&dst[2*stride], idct2, perm_ldv, perm_stv, sel);
  508. ALTIVEC_STORE_SUM_CLIP(&dst[3*stride], idct3, perm_ldv, perm_stv, sel);
  509. ALTIVEC_STORE_SUM_CLIP(&dst[4*stride], idct4, perm_ldv, perm_stv, sel);
  510. ALTIVEC_STORE_SUM_CLIP(&dst[5*stride], idct5, perm_ldv, perm_stv, sel);
  511. ALTIVEC_STORE_SUM_CLIP(&dst[6*stride], idct6, perm_ldv, perm_stv, sel);
  512. ALTIVEC_STORE_SUM_CLIP(&dst[7*stride], idct7, perm_ldv, perm_stv, sel);
  513. }
  514. static av_always_inline void h264_idct_dc_add_internal(uint8_t *dst, DCTELEM *block, int stride, int size)
  515. {
  516. vec_s16 dc16;
  517. vec_u8 dcplus, dcminus, v0, v1, v2, v3, aligner;
  518. LOAD_ZERO;
  519. DECLARE_ALIGNED_16(int, dc);
  520. int i;
  521. dc = (block[0] + 32) >> 6;
  522. dc16 = vec_splat((vec_s16) vec_lde(0, &dc), 1);
  523. if (size == 4)
  524. dc16 = vec_sld(dc16, zero_s16v, 8);
  525. dcplus = vec_packsu(dc16, zero_s16v);
  526. dcminus = vec_packsu(vec_sub(zero_s16v, dc16), zero_s16v);
  527. aligner = vec_lvsr(0, dst);
  528. dcplus = vec_perm(dcplus, dcplus, aligner);
  529. dcminus = vec_perm(dcminus, dcminus, aligner);
  530. for (i = 0; i < size; i += 4) {
  531. v0 = vec_ld(0, dst+0*stride);
  532. v1 = vec_ld(0, dst+1*stride);
  533. v2 = vec_ld(0, dst+2*stride);
  534. v3 = vec_ld(0, dst+3*stride);
  535. v0 = vec_adds(v0, dcplus);
  536. v1 = vec_adds(v1, dcplus);
  537. v2 = vec_adds(v2, dcplus);
  538. v3 = vec_adds(v3, dcplus);
  539. v0 = vec_subs(v0, dcminus);
  540. v1 = vec_subs(v1, dcminus);
  541. v2 = vec_subs(v2, dcminus);
  542. v3 = vec_subs(v3, dcminus);
  543. vec_st(v0, 0, dst+0*stride);
  544. vec_st(v1, 0, dst+1*stride);
  545. vec_st(v2, 0, dst+2*stride);
  546. vec_st(v3, 0, dst+3*stride);
  547. dst += 4*stride;
  548. }
  549. }
  550. static void h264_idct_dc_add_altivec(uint8_t *dst, DCTELEM *block, int stride)
  551. {
  552. h264_idct_dc_add_internal(dst, block, stride, 4);
  553. }
  554. static void ff_h264_idct8_dc_add_altivec(uint8_t *dst, DCTELEM *block, int stride)
  555. {
  556. h264_idct_dc_add_internal(dst, block, stride, 8);
  557. }
  558. static void ff_h264_idct_add16_altivec(uint8_t *dst, const int *block_offset, DCTELEM *block, int stride, const uint8_t nnzc[6*8]){
  559. int i;
  560. for(i=0; i<16; i++){
  561. int nnz = nnzc[ scan8[i] ];
  562. if(nnz){
  563. if(nnz==1 && block[i*16]) h264_idct_dc_add_altivec(dst + block_offset[i], block + i*16, stride);
  564. else ff_h264_idct_add_altivec(dst + block_offset[i], block + i*16, stride);
  565. }
  566. }
  567. }
  568. static void ff_h264_idct_add16intra_altivec(uint8_t *dst, const int *block_offset, DCTELEM *block, int stride, const uint8_t nnzc[6*8]){
  569. int i;
  570. for(i=0; i<16; i++){
  571. if(nnzc[ scan8[i] ]) ff_h264_idct_add_altivec(dst + block_offset[i], block + i*16, stride);
  572. else if(block[i*16]) h264_idct_dc_add_altivec(dst + block_offset[i], block + i*16, stride);
  573. }
  574. }
  575. static void ff_h264_idct8_add4_altivec(uint8_t *dst, const int *block_offset, DCTELEM *block, int stride, const uint8_t nnzc[6*8]){
  576. int i;
  577. for(i=0; i<16; i+=4){
  578. int nnz = nnzc[ scan8[i] ];
  579. if(nnz){
  580. if(nnz==1 && block[i*16]) ff_h264_idct8_dc_add_altivec(dst + block_offset[i], block + i*16, stride);
  581. else ff_h264_idct8_add_altivec (dst + block_offset[i], block + i*16, stride);
  582. }
  583. }
  584. }
  585. static void ff_h264_idct_add8_altivec(uint8_t **dest, const int *block_offset, DCTELEM *block, int stride, const uint8_t nnzc[6*8]){
  586. int i;
  587. for(i=16; i<16+8; i++){
  588. if(nnzc[ scan8[i] ])
  589. ff_h264_idct_add_altivec(dest[(i&4)>>2] + block_offset[i], block + i*16, stride);
  590. else if(block[i*16])
  591. h264_idct_dc_add_altivec(dest[(i&4)>>2] + block_offset[i], block + i*16, stride);
  592. }
  593. }
  594. #define transpose4x16(r0, r1, r2, r3) { \
  595. register vec_u8 r4; \
  596. register vec_u8 r5; \
  597. register vec_u8 r6; \
  598. register vec_u8 r7; \
  599. \
  600. r4 = vec_mergeh(r0, r2); /*0, 2 set 0*/ \
  601. r5 = vec_mergel(r0, r2); /*0, 2 set 1*/ \
  602. r6 = vec_mergeh(r1, r3); /*1, 3 set 0*/ \
  603. r7 = vec_mergel(r1, r3); /*1, 3 set 1*/ \
  604. \
  605. r0 = vec_mergeh(r4, r6); /*all set 0*/ \
  606. r1 = vec_mergel(r4, r6); /*all set 1*/ \
  607. r2 = vec_mergeh(r5, r7); /*all set 2*/ \
  608. r3 = vec_mergel(r5, r7); /*all set 3*/ \
  609. }
  610. static inline void write16x4(uint8_t *dst, int dst_stride,
  611. register vec_u8 r0, register vec_u8 r1,
  612. register vec_u8 r2, register vec_u8 r3) {
  613. DECLARE_ALIGNED_16(unsigned char, result[64]);
  614. uint32_t *src_int = (uint32_t *)result, *dst_int = (uint32_t *)dst;
  615. int int_dst_stride = dst_stride/4;
  616. vec_st(r0, 0, result);
  617. vec_st(r1, 16, result);
  618. vec_st(r2, 32, result);
  619. vec_st(r3, 48, result);
  620. /* FIXME: there has to be a better way!!!! */
  621. *dst_int = *src_int;
  622. *(dst_int+ int_dst_stride) = *(src_int + 1);
  623. *(dst_int+ 2*int_dst_stride) = *(src_int + 2);
  624. *(dst_int+ 3*int_dst_stride) = *(src_int + 3);
  625. *(dst_int+ 4*int_dst_stride) = *(src_int + 4);
  626. *(dst_int+ 5*int_dst_stride) = *(src_int + 5);
  627. *(dst_int+ 6*int_dst_stride) = *(src_int + 6);
  628. *(dst_int+ 7*int_dst_stride) = *(src_int + 7);
  629. *(dst_int+ 8*int_dst_stride) = *(src_int + 8);
  630. *(dst_int+ 9*int_dst_stride) = *(src_int + 9);
  631. *(dst_int+10*int_dst_stride) = *(src_int + 10);
  632. *(dst_int+11*int_dst_stride) = *(src_int + 11);
  633. *(dst_int+12*int_dst_stride) = *(src_int + 12);
  634. *(dst_int+13*int_dst_stride) = *(src_int + 13);
  635. *(dst_int+14*int_dst_stride) = *(src_int + 14);
  636. *(dst_int+15*int_dst_stride) = *(src_int + 15);
  637. }
  638. /** \brief performs a 6x16 transpose of data in src, and stores it to dst
  639. \todo FIXME: see if we can't spare some vec_lvsl() by them factorizing
  640. out of unaligned_load() */
  641. #define readAndTranspose16x6(src, src_stride, r8, r9, r10, r11, r12, r13) {\
  642. register vec_u8 r0 = unaligned_load(0, src); \
  643. register vec_u8 r1 = unaligned_load( src_stride, src); \
  644. register vec_u8 r2 = unaligned_load(2* src_stride, src); \
  645. register vec_u8 r3 = unaligned_load(3* src_stride, src); \
  646. register vec_u8 r4 = unaligned_load(4* src_stride, src); \
  647. register vec_u8 r5 = unaligned_load(5* src_stride, src); \
  648. register vec_u8 r6 = unaligned_load(6* src_stride, src); \
  649. register vec_u8 r7 = unaligned_load(7* src_stride, src); \
  650. register vec_u8 r14 = unaligned_load(14*src_stride, src); \
  651. register vec_u8 r15 = unaligned_load(15*src_stride, src); \
  652. \
  653. r8 = unaligned_load( 8*src_stride, src); \
  654. r9 = unaligned_load( 9*src_stride, src); \
  655. r10 = unaligned_load(10*src_stride, src); \
  656. r11 = unaligned_load(11*src_stride, src); \
  657. r12 = unaligned_load(12*src_stride, src); \
  658. r13 = unaligned_load(13*src_stride, src); \
  659. \
  660. /*Merge first pairs*/ \
  661. r0 = vec_mergeh(r0, r8); /*0, 8*/ \
  662. r1 = vec_mergeh(r1, r9); /*1, 9*/ \
  663. r2 = vec_mergeh(r2, r10); /*2,10*/ \
  664. r3 = vec_mergeh(r3, r11); /*3,11*/ \
  665. r4 = vec_mergeh(r4, r12); /*4,12*/ \
  666. r5 = vec_mergeh(r5, r13); /*5,13*/ \
  667. r6 = vec_mergeh(r6, r14); /*6,14*/ \
  668. r7 = vec_mergeh(r7, r15); /*7,15*/ \
  669. \
  670. /*Merge second pairs*/ \
  671. r8 = vec_mergeh(r0, r4); /*0,4, 8,12 set 0*/ \
  672. r9 = vec_mergel(r0, r4); /*0,4, 8,12 set 1*/ \
  673. r10 = vec_mergeh(r1, r5); /*1,5, 9,13 set 0*/ \
  674. r11 = vec_mergel(r1, r5); /*1,5, 9,13 set 1*/ \
  675. r12 = vec_mergeh(r2, r6); /*2,6,10,14 set 0*/ \
  676. r13 = vec_mergel(r2, r6); /*2,6,10,14 set 1*/ \
  677. r14 = vec_mergeh(r3, r7); /*3,7,11,15 set 0*/ \
  678. r15 = vec_mergel(r3, r7); /*3,7,11,15 set 1*/ \
  679. \
  680. /*Third merge*/ \
  681. r0 = vec_mergeh(r8, r12); /*0,2,4,6,8,10,12,14 set 0*/ \
  682. r1 = vec_mergel(r8, r12); /*0,2,4,6,8,10,12,14 set 1*/ \
  683. r2 = vec_mergeh(r9, r13); /*0,2,4,6,8,10,12,14 set 2*/ \
  684. r4 = vec_mergeh(r10, r14); /*1,3,5,7,9,11,13,15 set 0*/ \
  685. r5 = vec_mergel(r10, r14); /*1,3,5,7,9,11,13,15 set 1*/ \
  686. r6 = vec_mergeh(r11, r15); /*1,3,5,7,9,11,13,15 set 2*/ \
  687. /* Don't need to compute 3 and 7*/ \
  688. \
  689. /*Final merge*/ \
  690. r8 = vec_mergeh(r0, r4); /*all set 0*/ \
  691. r9 = vec_mergel(r0, r4); /*all set 1*/ \
  692. r10 = vec_mergeh(r1, r5); /*all set 2*/ \
  693. r11 = vec_mergel(r1, r5); /*all set 3*/ \
  694. r12 = vec_mergeh(r2, r6); /*all set 4*/ \
  695. r13 = vec_mergel(r2, r6); /*all set 5*/ \
  696. /* Don't need to compute 14 and 15*/ \
  697. \
  698. }
  699. // out: o = |x-y| < a
  700. static inline vec_u8 diff_lt_altivec ( register vec_u8 x,
  701. register vec_u8 y,
  702. register vec_u8 a) {
  703. register vec_u8 diff = vec_subs(x, y);
  704. register vec_u8 diffneg = vec_subs(y, x);
  705. register vec_u8 o = vec_or(diff, diffneg); /* |x-y| */
  706. o = (vec_u8)vec_cmplt(o, a);
  707. return o;
  708. }
  709. static inline vec_u8 h264_deblock_mask ( register vec_u8 p0,
  710. register vec_u8 p1,
  711. register vec_u8 q0,
  712. register vec_u8 q1,
  713. register vec_u8 alpha,
  714. register vec_u8 beta) {
  715. register vec_u8 mask;
  716. register vec_u8 tempmask;
  717. mask = diff_lt_altivec(p0, q0, alpha);
  718. tempmask = diff_lt_altivec(p1, p0, beta);
  719. mask = vec_and(mask, tempmask);
  720. tempmask = diff_lt_altivec(q1, q0, beta);
  721. mask = vec_and(mask, tempmask);
  722. return mask;
  723. }
  724. // out: newp1 = clip((p2 + ((p0 + q0 + 1) >> 1)) >> 1, p1-tc0, p1+tc0)
  725. static inline vec_u8 h264_deblock_q1(register vec_u8 p0,
  726. register vec_u8 p1,
  727. register vec_u8 p2,
  728. register vec_u8 q0,
  729. register vec_u8 tc0) {
  730. register vec_u8 average = vec_avg(p0, q0);
  731. register vec_u8 temp;
  732. register vec_u8 uncliped;
  733. register vec_u8 ones;
  734. register vec_u8 max;
  735. register vec_u8 min;
  736. register vec_u8 newp1;
  737. temp = vec_xor(average, p2);
  738. average = vec_avg(average, p2); /*avg(p2, avg(p0, q0)) */
  739. ones = vec_splat_u8(1);
  740. temp = vec_and(temp, ones); /*(p2^avg(p0, q0)) & 1 */
  741. uncliped = vec_subs(average, temp); /*(p2+((p0+q0+1)>>1))>>1 */
  742. max = vec_adds(p1, tc0);
  743. min = vec_subs(p1, tc0);
  744. newp1 = vec_max(min, uncliped);
  745. newp1 = vec_min(max, newp1);
  746. return newp1;
  747. }
  748. #define h264_deblock_p0_q0(p0, p1, q0, q1, tc0masked) { \
  749. \
  750. const vec_u8 A0v = vec_sl(vec_splat_u8(10), vec_splat_u8(4)); \
  751. \
  752. register vec_u8 pq0bit = vec_xor(p0,q0); \
  753. register vec_u8 q1minus; \
  754. register vec_u8 p0minus; \
  755. register vec_u8 stage1; \
  756. register vec_u8 stage2; \
  757. register vec_u8 vec160; \
  758. register vec_u8 delta; \
  759. register vec_u8 deltaneg; \
  760. \
  761. q1minus = vec_nor(q1, q1); /* 255 - q1 */ \
  762. stage1 = vec_avg(p1, q1minus); /* (p1 - q1 + 256)>>1 */ \
  763. stage2 = vec_sr(stage1, vec_splat_u8(1)); /* (p1 - q1 + 256)>>2 = 64 + (p1 - q1) >> 2 */ \
  764. p0minus = vec_nor(p0, p0); /* 255 - p0 */ \
  765. stage1 = vec_avg(q0, p0minus); /* (q0 - p0 + 256)>>1 */ \
  766. pq0bit = vec_and(pq0bit, vec_splat_u8(1)); \
  767. stage2 = vec_avg(stage2, pq0bit); /* 32 + ((q0 - p0)&1 + (p1 - q1) >> 2 + 1) >> 1 */ \
  768. stage2 = vec_adds(stage2, stage1); /* 160 + ((p0 - q0) + (p1 - q1) >> 2 + 1) >> 1 */ \
  769. vec160 = vec_ld(0, &A0v); \
  770. deltaneg = vec_subs(vec160, stage2); /* -d */ \
  771. delta = vec_subs(stage2, vec160); /* d */ \
  772. deltaneg = vec_min(tc0masked, deltaneg); \
  773. delta = vec_min(tc0masked, delta); \
  774. p0 = vec_subs(p0, deltaneg); \
  775. q0 = vec_subs(q0, delta); \
  776. p0 = vec_adds(p0, delta); \
  777. q0 = vec_adds(q0, deltaneg); \
  778. }
  779. #define h264_loop_filter_luma_altivec(p2, p1, p0, q0, q1, q2, alpha, beta, tc0) { \
  780. DECLARE_ALIGNED_16(unsigned char, temp[16]); \
  781. register vec_u8 alphavec; \
  782. register vec_u8 betavec; \
  783. register vec_u8 mask; \
  784. register vec_u8 p1mask; \
  785. register vec_u8 q1mask; \
  786. register vector signed char tc0vec; \
  787. register vec_u8 finaltc0; \
  788. register vec_u8 tc0masked; \
  789. register vec_u8 newp1; \
  790. register vec_u8 newq1; \
  791. \
  792. temp[0] = alpha; \
  793. temp[1] = beta; \
  794. alphavec = vec_ld(0, temp); \
  795. betavec = vec_splat(alphavec, 0x1); \
  796. alphavec = vec_splat(alphavec, 0x0); \
  797. mask = h264_deblock_mask(p0, p1, q0, q1, alphavec, betavec); /*if in block */ \
  798. \
  799. *((int *)temp) = *((int *)tc0); \
  800. tc0vec = vec_ld(0, (signed char*)temp); \
  801. tc0vec = vec_mergeh(tc0vec, tc0vec); \
  802. tc0vec = vec_mergeh(tc0vec, tc0vec); \
  803. mask = vec_and(mask, vec_cmpgt(tc0vec, vec_splat_s8(-1))); /* if tc0[i] >= 0 */ \
  804. finaltc0 = vec_and((vec_u8)tc0vec, mask); /* tc = tc0 */ \
  805. \
  806. p1mask = diff_lt_altivec(p2, p0, betavec); \
  807. p1mask = vec_and(p1mask, mask); /* if ( |p2 - p0| < beta) */ \
  808. tc0masked = vec_and(p1mask, (vec_u8)tc0vec); \
  809. finaltc0 = vec_sub(finaltc0, p1mask); /* tc++ */ \
  810. newp1 = h264_deblock_q1(p0, p1, p2, q0, tc0masked); \
  811. /*end if*/ \
  812. \
  813. q1mask = diff_lt_altivec(q2, q0, betavec); \
  814. q1mask = vec_and(q1mask, mask); /* if ( |q2 - q0| < beta ) */\
  815. tc0masked = vec_and(q1mask, (vec_u8)tc0vec); \
  816. finaltc0 = vec_sub(finaltc0, q1mask); /* tc++ */ \
  817. newq1 = h264_deblock_q1(p0, q1, q2, q0, tc0masked); \
  818. /*end if*/ \
  819. \
  820. h264_deblock_p0_q0(p0, p1, q0, q1, finaltc0); \
  821. p1 = newp1; \
  822. q1 = newq1; \
  823. }
  824. static void h264_v_loop_filter_luma_altivec(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0) {
  825. if ((tc0[0] & tc0[1] & tc0[2] & tc0[3]) >= 0) {
  826. register vec_u8 p2 = vec_ld(-3*stride, pix);
  827. register vec_u8 p1 = vec_ld(-2*stride, pix);
  828. register vec_u8 p0 = vec_ld(-1*stride, pix);
  829. register vec_u8 q0 = vec_ld(0, pix);
  830. register vec_u8 q1 = vec_ld(stride, pix);
  831. register vec_u8 q2 = vec_ld(2*stride, pix);
  832. h264_loop_filter_luma_altivec(p2, p1, p0, q0, q1, q2, alpha, beta, tc0);
  833. vec_st(p1, -2*stride, pix);
  834. vec_st(p0, -1*stride, pix);
  835. vec_st(q0, 0, pix);
  836. vec_st(q1, stride, pix);
  837. }
  838. }
  839. static void h264_h_loop_filter_luma_altivec(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0) {
  840. register vec_u8 line0, line1, line2, line3, line4, line5;
  841. if ((tc0[0] & tc0[1] & tc0[2] & tc0[3]) < 0)
  842. return;
  843. readAndTranspose16x6(pix-3, stride, line0, line1, line2, line3, line4, line5);
  844. h264_loop_filter_luma_altivec(line0, line1, line2, line3, line4, line5, alpha, beta, tc0);
  845. transpose4x16(line1, line2, line3, line4);
  846. write16x4(pix-2, stride, line1, line2, line3, line4);
  847. }
  848. static av_always_inline
  849. void weight_h264_WxH_altivec(uint8_t *block, int stride, int log2_denom, int weight, int offset, int w, int h)
  850. {
  851. int y, aligned;
  852. vec_u8 vblock;
  853. vec_s16 vtemp, vweight, voffset, v0, v1;
  854. vec_u16 vlog2_denom;
  855. DECLARE_ALIGNED_16(int32_t, temp[4]);
  856. LOAD_ZERO;
  857. offset <<= log2_denom;
  858. if(log2_denom) offset += 1<<(log2_denom-1);
  859. temp[0] = log2_denom;
  860. temp[1] = weight;
  861. temp[2] = offset;
  862. vtemp = (vec_s16)vec_ld(0, temp);
  863. vlog2_denom = (vec_u16)vec_splat(vtemp, 1);
  864. vweight = vec_splat(vtemp, 3);
  865. voffset = vec_splat(vtemp, 5);
  866. aligned = !((unsigned long)block & 0xf);
  867. for (y=0; y<h; y++) {
  868. vblock = vec_ld(0, block);
  869. v0 = (vec_s16)vec_mergeh(zero_u8v, vblock);
  870. v1 = (vec_s16)vec_mergel(zero_u8v, vblock);
  871. if (w == 16 || aligned) {
  872. v0 = vec_mladd(v0, vweight, zero_s16v);
  873. v0 = vec_adds(v0, voffset);
  874. v0 = vec_sra(v0, vlog2_denom);
  875. }
  876. if (w == 16 || !aligned) {
  877. v1 = vec_mladd(v1, vweight, zero_s16v);
  878. v1 = vec_adds(v1, voffset);
  879. v1 = vec_sra(v1, vlog2_denom);
  880. }
  881. vblock = vec_packsu(v0, v1);
  882. vec_st(vblock, 0, block);
  883. block += stride;
  884. }
  885. }
  886. static av_always_inline
  887. void biweight_h264_WxH_altivec(uint8_t *dst, uint8_t *src, int stride, int log2_denom,
  888. int weightd, int weights, int offset, int w, int h)
  889. {
  890. int y, dst_aligned, src_aligned;
  891. vec_u8 vsrc, vdst;
  892. vec_s16 vtemp, vweights, vweightd, voffset, v0, v1, v2, v3;
  893. vec_u16 vlog2_denom;
  894. DECLARE_ALIGNED_16(int32_t, temp[4]);
  895. LOAD_ZERO;
  896. offset = ((offset + 1) | 1) << log2_denom;
  897. temp[0] = log2_denom+1;
  898. temp[1] = weights;
  899. temp[2] = weightd;
  900. temp[3] = offset;
  901. vtemp = (vec_s16)vec_ld(0, temp);
  902. vlog2_denom = (vec_u16)vec_splat(vtemp, 1);
  903. vweights = vec_splat(vtemp, 3);
  904. vweightd = vec_splat(vtemp, 5);
  905. voffset = vec_splat(vtemp, 7);
  906. dst_aligned = !((unsigned long)dst & 0xf);
  907. src_aligned = !((unsigned long)src & 0xf);
  908. for (y=0; y<h; y++) {
  909. vdst = vec_ld(0, dst);
  910. vsrc = vec_ld(0, src);
  911. v0 = (vec_s16)vec_mergeh(zero_u8v, vdst);
  912. v1 = (vec_s16)vec_mergel(zero_u8v, vdst);
  913. v2 = (vec_s16)vec_mergeh(zero_u8v, vsrc);
  914. v3 = (vec_s16)vec_mergel(zero_u8v, vsrc);
  915. if (w == 8) {
  916. if (src_aligned)
  917. v3 = v2;
  918. else
  919. v2 = v3;
  920. }
  921. if (w == 16 || dst_aligned) {
  922. v0 = vec_mladd(v0, vweightd, zero_s16v);
  923. v2 = vec_mladd(v2, vweights, zero_s16v);
  924. v0 = vec_adds(v0, voffset);
  925. v0 = vec_adds(v0, v2);
  926. v0 = vec_sra(v0, vlog2_denom);
  927. }
  928. if (w == 16 || !dst_aligned) {
  929. v1 = vec_mladd(v1, vweightd, zero_s16v);
  930. v3 = vec_mladd(v3, vweights, zero_s16v);
  931. v1 = vec_adds(v1, voffset);
  932. v1 = vec_adds(v1, v3);
  933. v1 = vec_sra(v1, vlog2_denom);
  934. }
  935. vdst = vec_packsu(v0, v1);
  936. vec_st(vdst, 0, dst);
  937. dst += stride;
  938. src += stride;
  939. }
  940. }
  941. #define H264_WEIGHT(W,H) \
  942. static void ff_weight_h264_pixels ## W ## x ## H ## _altivec(uint8_t *block, int stride, int log2_denom, int weight, int offset){ \
  943. weight_h264_WxH_altivec(block, stride, log2_denom, weight, offset, W, H); \
  944. }\
  945. static void ff_biweight_h264_pixels ## W ## x ## H ## _altivec(uint8_t *dst, uint8_t *src, int stride, int log2_denom, int weightd, int weights, int offset){ \
  946. biweight_h264_WxH_altivec(dst, src, stride, log2_denom, weightd, weights, offset, W, H); \
  947. }
  948. H264_WEIGHT(16,16)
  949. H264_WEIGHT(16, 8)
  950. H264_WEIGHT( 8,16)
  951. H264_WEIGHT( 8, 8)
  952. H264_WEIGHT( 8, 4)
  953. void dsputil_h264_init_ppc(DSPContext* c, AVCodecContext *avctx) {
  954. if (has_altivec()) {
  955. c->put_h264_chroma_pixels_tab[0] = put_h264_chroma_mc8_altivec;
  956. c->put_no_rnd_h264_chroma_pixels_tab[0] = put_no_rnd_h264_chroma_mc8_altivec;
  957. c->avg_h264_chroma_pixels_tab[0] = avg_h264_chroma_mc8_altivec;
  958. c->h264_idct_add = ff_h264_idct_add_altivec;
  959. c->h264_idct_add8 = ff_h264_idct_add8_altivec;
  960. c->h264_idct_add16 = ff_h264_idct_add16_altivec;
  961. c->h264_idct_add16intra = ff_h264_idct_add16intra_altivec;
  962. c->h264_idct_dc_add= h264_idct_dc_add_altivec;
  963. c->h264_idct8_dc_add = ff_h264_idct8_dc_add_altivec;
  964. c->h264_idct8_add = ff_h264_idct8_add_altivec;
  965. c->h264_idct8_add4 = ff_h264_idct8_add4_altivec;
  966. c->h264_v_loop_filter_luma= h264_v_loop_filter_luma_altivec;
  967. c->h264_h_loop_filter_luma= h264_h_loop_filter_luma_altivec;
  968. #define dspfunc(PFX, IDX, NUM) \
  969. c->PFX ## _pixels_tab[IDX][ 0] = PFX ## NUM ## _mc00_altivec; \
  970. c->PFX ## _pixels_tab[IDX][ 1] = PFX ## NUM ## _mc10_altivec; \
  971. c->PFX ## _pixels_tab[IDX][ 2] = PFX ## NUM ## _mc20_altivec; \
  972. c->PFX ## _pixels_tab[IDX][ 3] = PFX ## NUM ## _mc30_altivec; \
  973. c->PFX ## _pixels_tab[IDX][ 4] = PFX ## NUM ## _mc01_altivec; \
  974. c->PFX ## _pixels_tab[IDX][ 5] = PFX ## NUM ## _mc11_altivec; \
  975. c->PFX ## _pixels_tab[IDX][ 6] = PFX ## NUM ## _mc21_altivec; \
  976. c->PFX ## _pixels_tab[IDX][ 7] = PFX ## NUM ## _mc31_altivec; \
  977. c->PFX ## _pixels_tab[IDX][ 8] = PFX ## NUM ## _mc02_altivec; \
  978. c->PFX ## _pixels_tab[IDX][ 9] = PFX ## NUM ## _mc12_altivec; \
  979. c->PFX ## _pixels_tab[IDX][10] = PFX ## NUM ## _mc22_altivec; \
  980. c->PFX ## _pixels_tab[IDX][11] = PFX ## NUM ## _mc32_altivec; \
  981. c->PFX ## _pixels_tab[IDX][12] = PFX ## NUM ## _mc03_altivec; \
  982. c->PFX ## _pixels_tab[IDX][13] = PFX ## NUM ## _mc13_altivec; \
  983. c->PFX ## _pixels_tab[IDX][14] = PFX ## NUM ## _mc23_altivec; \
  984. c->PFX ## _pixels_tab[IDX][15] = PFX ## NUM ## _mc33_altivec
  985. dspfunc(put_h264_qpel, 0, 16);
  986. dspfunc(avg_h264_qpel, 0, 16);
  987. #undef dspfunc
  988. c->weight_h264_pixels_tab[0] = ff_weight_h264_pixels16x16_altivec;
  989. c->weight_h264_pixels_tab[1] = ff_weight_h264_pixels16x8_altivec;
  990. c->weight_h264_pixels_tab[2] = ff_weight_h264_pixels8x16_altivec;
  991. c->weight_h264_pixels_tab[3] = ff_weight_h264_pixels8x8_altivec;
  992. c->weight_h264_pixels_tab[4] = ff_weight_h264_pixels8x4_altivec;
  993. c->biweight_h264_pixels_tab[0] = ff_biweight_h264_pixels16x16_altivec;
  994. c->biweight_h264_pixels_tab[1] = ff_biweight_h264_pixels16x8_altivec;
  995. c->biweight_h264_pixels_tab[2] = ff_biweight_h264_pixels8x16_altivec;
  996. c->biweight_h264_pixels_tab[3] = ff_biweight_h264_pixels8x8_altivec;
  997. c->biweight_h264_pixels_tab[4] = ff_biweight_h264_pixels8x4_altivec;
  998. }
  999. }