dsputil_ppc.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * Copyright (c) 2002 Brian Foley
  3. * Copyright (c) 2002 Dieter Shirley
  4. * Copyright (c) 2003-2004 Romain Dolbeau <romain@dolbeau.org>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "libavcodec/dsputil.h"
  23. #include "dsputil_ppc.h"
  24. #include "dsputil_altivec.h"
  25. void fdct_altivec(int16_t *block);
  26. void gmc1_altivec(uint8_t *dst, uint8_t *src, int stride, int h,
  27. int x16, int y16, int rounder);
  28. void idct_put_altivec(uint8_t *dest, int line_size, int16_t *block);
  29. void idct_add_altivec(uint8_t *dest, int line_size, int16_t *block);
  30. void dsputil_h264_init_ppc(DSPContext* c, AVCodecContext *avctx);
  31. void dsputil_init_altivec(DSPContext* c, AVCodecContext *avctx);
  32. void vc1dsp_init_altivec(DSPContext* c, AVCodecContext *avctx);
  33. void snow_init_altivec(DSPContext* c, AVCodecContext *avctx);
  34. void float_init_altivec(DSPContext* c, AVCodecContext *avctx);
  35. void int_init_altivec(DSPContext* c, AVCodecContext *avctx);
  36. int mm_flags = 0;
  37. int mm_support(void)
  38. {
  39. int result = 0;
  40. #if HAVE_ALTIVEC
  41. if (has_altivec()) {
  42. result |= FF_MM_ALTIVEC;
  43. }
  44. #endif /* result */
  45. return result;
  46. }
  47. #if CONFIG_POWERPC_PERF
  48. unsigned long long perfdata[POWERPC_NUM_PMC_ENABLED][powerpc_perf_total][powerpc_data_total];
  49. /* list below must match enum in dsputil_ppc.h */
  50. static unsigned char* perfname[] = {
  51. "ff_fft_calc_altivec",
  52. "gmc1_altivec",
  53. "dct_unquantize_h263_altivec",
  54. "fdct_altivec",
  55. "idct_add_altivec",
  56. "idct_put_altivec",
  57. "put_pixels16_altivec",
  58. "avg_pixels16_altivec",
  59. "avg_pixels8_altivec",
  60. "put_pixels8_xy2_altivec",
  61. "put_no_rnd_pixels8_xy2_altivec",
  62. "put_pixels16_xy2_altivec",
  63. "put_no_rnd_pixels16_xy2_altivec",
  64. "hadamard8_diff8x8_altivec",
  65. "hadamard8_diff16_altivec",
  66. "avg_pixels8_xy2_altivec",
  67. "clear_blocks_dcbz32_ppc",
  68. "clear_blocks_dcbz128_ppc",
  69. "put_h264_chroma_mc8_altivec",
  70. "avg_h264_chroma_mc8_altivec",
  71. "put_h264_qpel16_h_lowpass_altivec",
  72. "avg_h264_qpel16_h_lowpass_altivec",
  73. "put_h264_qpel16_v_lowpass_altivec",
  74. "avg_h264_qpel16_v_lowpass_altivec",
  75. "put_h264_qpel16_hv_lowpass_altivec",
  76. "avg_h264_qpel16_hv_lowpass_altivec",
  77. ""
  78. };
  79. #include <stdio.h>
  80. #endif
  81. #if CONFIG_POWERPC_PERF
  82. void powerpc_display_perf_report(void)
  83. {
  84. int i, j;
  85. av_log(NULL, AV_LOG_INFO, "PowerPC performance report\n Values are from the PMC registers, and represent whatever the registers are set to record.\n");
  86. for(i = 0 ; i < powerpc_perf_total ; i++) {
  87. for (j = 0; j < POWERPC_NUM_PMC_ENABLED ; j++) {
  88. if (perfdata[j][i][powerpc_data_num] != (unsigned long long)0)
  89. av_log(NULL, AV_LOG_INFO,
  90. " Function \"%s\" (pmc%d):\n\tmin: %"PRIu64"\n\tmax: %"PRIu64"\n\tavg: %1.2lf (%"PRIu64")\n",
  91. perfname[i],
  92. j+1,
  93. perfdata[j][i][powerpc_data_min],
  94. perfdata[j][i][powerpc_data_max],
  95. (double)perfdata[j][i][powerpc_data_sum] /
  96. (double)perfdata[j][i][powerpc_data_num],
  97. perfdata[j][i][powerpc_data_num]);
  98. }
  99. }
  100. }
  101. #endif /* CONFIG_POWERPC_PERF */
  102. /* ***** WARNING ***** WARNING ***** WARNING ***** */
  103. /*
  104. clear_blocks_dcbz32_ppc will not work properly on PowerPC processors with a
  105. cache line size not equal to 32 bytes.
  106. Fortunately all processor used by Apple up to at least the 7450 (aka second
  107. generation G4) use 32 bytes cache line.
  108. This is due to the use of the 'dcbz' instruction. It simply clear to zero a
  109. single cache line, so you need to know the cache line size to use it !
  110. It's absurd, but it's fast...
  111. update 24/06/2003 : Apple released yesterday the G5, with a PPC970. cache line
  112. size: 128 bytes. Oups.
  113. The semantic of dcbz was changed, it always clear 32 bytes. so the function
  114. below will work, but will be slow. So I fixed check_dcbz_effect to use dcbzl,
  115. which is defined to clear a cache line (as dcbz before). So we still can
  116. distinguish, and use dcbz (32 bytes) or dcbzl (one cache line) as required.
  117. see <http://developer.apple.com/technotes/tn/tn2087.html>
  118. and <http://developer.apple.com/technotes/tn/tn2086.html>
  119. */
  120. void clear_blocks_dcbz32_ppc(DCTELEM *blocks)
  121. {
  122. POWERPC_PERF_DECLARE(powerpc_clear_blocks_dcbz32, 1);
  123. register int misal = ((unsigned long)blocks & 0x00000010);
  124. register int i = 0;
  125. POWERPC_PERF_START_COUNT(powerpc_clear_blocks_dcbz32, 1);
  126. #if 1
  127. if (misal) {
  128. ((unsigned long*)blocks)[0] = 0L;
  129. ((unsigned long*)blocks)[1] = 0L;
  130. ((unsigned long*)blocks)[2] = 0L;
  131. ((unsigned long*)blocks)[3] = 0L;
  132. i += 16;
  133. }
  134. for ( ; i < sizeof(DCTELEM)*6*64-31 ; i += 32) {
  135. __asm__ volatile("dcbz %0,%1" : : "b" (blocks), "r" (i) : "memory");
  136. }
  137. if (misal) {
  138. ((unsigned long*)blocks)[188] = 0L;
  139. ((unsigned long*)blocks)[189] = 0L;
  140. ((unsigned long*)blocks)[190] = 0L;
  141. ((unsigned long*)blocks)[191] = 0L;
  142. i += 16;
  143. }
  144. #else
  145. memset(blocks, 0, sizeof(DCTELEM)*6*64);
  146. #endif
  147. POWERPC_PERF_STOP_COUNT(powerpc_clear_blocks_dcbz32, 1);
  148. }
  149. /* same as above, when dcbzl clear a whole 128B cache line
  150. i.e. the PPC970 aka G5 */
  151. #if HAVE_DCBZL
  152. void clear_blocks_dcbz128_ppc(DCTELEM *blocks)
  153. {
  154. POWERPC_PERF_DECLARE(powerpc_clear_blocks_dcbz128, 1);
  155. register int misal = ((unsigned long)blocks & 0x0000007f);
  156. register int i = 0;
  157. POWERPC_PERF_START_COUNT(powerpc_clear_blocks_dcbz128, 1);
  158. #if 1
  159. if (misal) {
  160. // we could probably also optimize this case,
  161. // but there's not much point as the machines
  162. // aren't available yet (2003-06-26)
  163. memset(blocks, 0, sizeof(DCTELEM)*6*64);
  164. }
  165. else
  166. for ( ; i < sizeof(DCTELEM)*6*64 ; i += 128) {
  167. __asm__ volatile("dcbzl %0,%1" : : "b" (blocks), "r" (i) : "memory");
  168. }
  169. #else
  170. memset(blocks, 0, sizeof(DCTELEM)*6*64);
  171. #endif
  172. POWERPC_PERF_STOP_COUNT(powerpc_clear_blocks_dcbz128, 1);
  173. }
  174. #else
  175. void clear_blocks_dcbz128_ppc(DCTELEM *blocks)
  176. {
  177. memset(blocks, 0, sizeof(DCTELEM)*6*64);
  178. }
  179. #endif
  180. #if HAVE_DCBZL
  181. /* check dcbz report how many bytes are set to 0 by dcbz */
  182. /* update 24/06/2003 : replace dcbz by dcbzl to get
  183. the intended effect (Apple "fixed" dcbz)
  184. unfortunately this cannot be used unless the assembler
  185. knows about dcbzl ... */
  186. long check_dcbzl_effect(void)
  187. {
  188. register char *fakedata = av_malloc(1024);
  189. register char *fakedata_middle;
  190. register long zero = 0;
  191. register long i = 0;
  192. long count = 0;
  193. if (!fakedata) {
  194. return 0L;
  195. }
  196. fakedata_middle = (fakedata + 512);
  197. memset(fakedata, 0xFF, 1024);
  198. /* below the constraint "b" seems to mean "Address base register"
  199. in gcc-3.3 / RS/6000 speaks. seems to avoid using r0, so.... */
  200. __asm__ volatile("dcbzl %0, %1" : : "b" (fakedata_middle), "r" (zero));
  201. for (i = 0; i < 1024 ; i ++) {
  202. if (fakedata[i] == (char)0)
  203. count++;
  204. }
  205. av_free(fakedata);
  206. return count;
  207. }
  208. #else
  209. long check_dcbzl_effect(void)
  210. {
  211. return 0;
  212. }
  213. #endif
  214. static void prefetch_ppc(void *mem, int stride, int h)
  215. {
  216. register const uint8_t *p = mem;
  217. do {
  218. __asm__ volatile ("dcbt 0,%0" : : "r" (p));
  219. p+= stride;
  220. } while(--h);
  221. }
  222. void dsputil_init_ppc(DSPContext* c, AVCodecContext *avctx)
  223. {
  224. // Common optimizations whether AltiVec is available or not
  225. c->prefetch = prefetch_ppc;
  226. switch (check_dcbzl_effect()) {
  227. case 32:
  228. c->clear_blocks = clear_blocks_dcbz32_ppc;
  229. break;
  230. case 128:
  231. c->clear_blocks = clear_blocks_dcbz128_ppc;
  232. break;
  233. default:
  234. break;
  235. }
  236. #if HAVE_ALTIVEC
  237. if(CONFIG_H264_DECODER) dsputil_h264_init_ppc(c, avctx);
  238. if (has_altivec()) {
  239. mm_flags |= FF_MM_ALTIVEC;
  240. dsputil_init_altivec(c, avctx);
  241. if(CONFIG_SNOW_DECODER) snow_init_altivec(c, avctx);
  242. if(CONFIG_VC1_DECODER || CONFIG_WMV3_DECODER)
  243. vc1dsp_init_altivec(c, avctx);
  244. float_init_altivec(c, avctx);
  245. int_init_altivec(c, avctx);
  246. c->gmc1 = gmc1_altivec;
  247. #if CONFIG_ENCODERS
  248. if (avctx->dct_algo == FF_DCT_AUTO ||
  249. avctx->dct_algo == FF_DCT_ALTIVEC) {
  250. c->fdct = fdct_altivec;
  251. }
  252. #endif //CONFIG_ENCODERS
  253. if (avctx->lowres==0) {
  254. if ((avctx->idct_algo == FF_IDCT_AUTO) ||
  255. (avctx->idct_algo == FF_IDCT_ALTIVEC)) {
  256. c->idct_put = idct_put_altivec;
  257. c->idct_add = idct_add_altivec;
  258. c->idct_permutation_type = FF_TRANSPOSE_IDCT_PERM;
  259. }
  260. }
  261. #if CONFIG_POWERPC_PERF
  262. {
  263. int i, j;
  264. for (i = 0 ; i < powerpc_perf_total ; i++) {
  265. for (j = 0; j < POWERPC_NUM_PMC_ENABLED ; j++) {
  266. perfdata[j][i][powerpc_data_min] = 0xFFFFFFFFFFFFFFFFULL;
  267. perfdata[j][i][powerpc_data_max] = 0x0000000000000000ULL;
  268. perfdata[j][i][powerpc_data_sum] = 0x0000000000000000ULL;
  269. perfdata[j][i][powerpc_data_num] = 0x0000000000000000ULL;
  270. }
  271. }
  272. }
  273. #endif /* CONFIG_POWERPC_PERF */
  274. }
  275. #endif /* HAVE_ALTIVEC */
  276. }