You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

208 lines
6.5KB

  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 "libavutil/cpu.h"
  23. #include "libavcodec/dsputil.h"
  24. #include "dsputil_altivec.h"
  25. /* ***** WARNING ***** WARNING ***** WARNING ***** */
  26. /*
  27. clear_blocks_dcbz32_ppc will not work properly on PowerPC processors with a
  28. cache line size not equal to 32 bytes.
  29. Fortunately all processor used by Apple up to at least the 7450 (aka second
  30. generation G4) use 32 bytes cache line.
  31. This is due to the use of the 'dcbz' instruction. It simply clear to zero a
  32. single cache line, so you need to know the cache line size to use it !
  33. It's absurd, but it's fast...
  34. update 24/06/2003 : Apple released yesterday the G5, with a PPC970. cache line
  35. size: 128 bytes. Oups.
  36. The semantic of dcbz was changed, it always clear 32 bytes. so the function
  37. below will work, but will be slow. So I fixed check_dcbz_effect to use dcbzl,
  38. which is defined to clear a cache line (as dcbz before). So we still can
  39. distinguish, and use dcbz (32 bytes) or dcbzl (one cache line) as required.
  40. see <http://developer.apple.com/technotes/tn/tn2087.html>
  41. and <http://developer.apple.com/technotes/tn/tn2086.html>
  42. */
  43. static void clear_blocks_dcbz32_ppc(DCTELEM *blocks)
  44. {
  45. register int misal = ((unsigned long)blocks & 0x00000010);
  46. register int i = 0;
  47. if (misal) {
  48. ((unsigned long*)blocks)[0] = 0L;
  49. ((unsigned long*)blocks)[1] = 0L;
  50. ((unsigned long*)blocks)[2] = 0L;
  51. ((unsigned long*)blocks)[3] = 0L;
  52. i += 16;
  53. }
  54. for ( ; i < sizeof(DCTELEM)*6*64-31 ; i += 32) {
  55. __asm__ volatile("dcbz %0,%1" : : "b" (blocks), "r" (i) : "memory");
  56. }
  57. if (misal) {
  58. ((unsigned long*)blocks)[188] = 0L;
  59. ((unsigned long*)blocks)[189] = 0L;
  60. ((unsigned long*)blocks)[190] = 0L;
  61. ((unsigned long*)blocks)[191] = 0L;
  62. i += 16;
  63. }
  64. }
  65. /* same as above, when dcbzl clear a whole 128B cache line
  66. i.e. the PPC970 aka G5 */
  67. #if HAVE_DCBZL
  68. static void clear_blocks_dcbz128_ppc(DCTELEM *blocks)
  69. {
  70. register int misal = ((unsigned long)blocks & 0x0000007f);
  71. register int i = 0;
  72. if (misal) {
  73. // we could probably also optimize this case,
  74. // but there's not much point as the machines
  75. // aren't available yet (2003-06-26)
  76. memset(blocks, 0, sizeof(DCTELEM)*6*64);
  77. }
  78. else
  79. for ( ; i < sizeof(DCTELEM)*6*64 ; i += 128) {
  80. __asm__ volatile("dcbzl %0,%1" : : "b" (blocks), "r" (i) : "memory");
  81. }
  82. }
  83. #else
  84. static void clear_blocks_dcbz128_ppc(DCTELEM *blocks)
  85. {
  86. memset(blocks, 0, sizeof(DCTELEM)*6*64);
  87. }
  88. #endif
  89. #if HAVE_DCBZL
  90. /* check dcbz report how many bytes are set to 0 by dcbz */
  91. /* update 24/06/2003 : replace dcbz by dcbzl to get
  92. the intended effect (Apple "fixed" dcbz)
  93. unfortunately this cannot be used unless the assembler
  94. knows about dcbzl ... */
  95. static long check_dcbzl_effect(void)
  96. {
  97. register char *fakedata = av_malloc(1024);
  98. register char *fakedata_middle;
  99. register long zero = 0;
  100. register long i = 0;
  101. long count = 0;
  102. if (!fakedata) {
  103. return 0L;
  104. }
  105. fakedata_middle = (fakedata + 512);
  106. memset(fakedata, 0xFF, 1024);
  107. /* below the constraint "b" seems to mean "Address base register"
  108. in gcc-3.3 / RS/6000 speaks. seems to avoid using r0, so.... */
  109. __asm__ volatile("dcbzl %0, %1" : : "b" (fakedata_middle), "r" (zero));
  110. for (i = 0; i < 1024 ; i ++) {
  111. if (fakedata[i] == (char)0)
  112. count++;
  113. }
  114. av_free(fakedata);
  115. return count;
  116. }
  117. #else
  118. static long check_dcbzl_effect(void)
  119. {
  120. return 0;
  121. }
  122. #endif
  123. static void prefetch_ppc(void *mem, int stride, int h)
  124. {
  125. register const uint8_t *p = mem;
  126. do {
  127. __asm__ volatile ("dcbt 0,%0" : : "r" (p));
  128. p+= stride;
  129. } while(--h);
  130. }
  131. void ff_dsputil_init_ppc(DSPContext* c, AVCodecContext *avctx)
  132. {
  133. const int high_bit_depth = avctx->bits_per_raw_sample > 8;
  134. int mm_flags = av_get_cpu_flags();
  135. if (avctx->dsp_mask) {
  136. if (avctx->dsp_mask & AV_CPU_FLAG_FORCE)
  137. mm_flags |= (avctx->dsp_mask & 0xffff);
  138. else
  139. mm_flags &= ~(avctx->dsp_mask & 0xffff);
  140. }
  141. // Common optimizations whether AltiVec is available or not
  142. c->prefetch = prefetch_ppc;
  143. if (!high_bit_depth) {
  144. switch (check_dcbzl_effect()) {
  145. case 32:
  146. c->clear_blocks = clear_blocks_dcbz32_ppc;
  147. break;
  148. case 128:
  149. c->clear_blocks = clear_blocks_dcbz128_ppc;
  150. break;
  151. default:
  152. break;
  153. }
  154. }
  155. #if HAVE_ALTIVEC
  156. if(CONFIG_H264_DECODER) ff_dsputil_h264_init_ppc(c, avctx);
  157. if (mm_flags & AV_CPU_FLAG_ALTIVEC) {
  158. ff_dsputil_init_altivec(c, avctx);
  159. ff_float_init_altivec(c, avctx);
  160. ff_int_init_altivec(c, avctx);
  161. c->gmc1 = ff_gmc1_altivec;
  162. #if CONFIG_ENCODERS
  163. if (avctx->bits_per_raw_sample <= 8 &&
  164. (avctx->dct_algo == FF_DCT_AUTO ||
  165. avctx->dct_algo == FF_DCT_ALTIVEC)) {
  166. c->fdct = ff_fdct_altivec;
  167. }
  168. #endif //CONFIG_ENCODERS
  169. if (avctx->bits_per_raw_sample <= 8) {
  170. if ((avctx->idct_algo == FF_IDCT_AUTO) ||
  171. (avctx->idct_algo == FF_IDCT_ALTIVEC)) {
  172. c->idct_put = ff_idct_put_altivec;
  173. c->idct_add = ff_idct_add_altivec;
  174. c->idct_permutation_type = FF_TRANSPOSE_IDCT_PERM;
  175. }else if((CONFIG_VP3_DECODER || CONFIG_VP5_DECODER || CONFIG_VP6_DECODER) &&
  176. avctx->idct_algo==FF_IDCT_VP3){
  177. c->idct_put = ff_vp3_idct_put_altivec;
  178. c->idct_add = ff_vp3_idct_add_altivec;
  179. c->idct = ff_vp3_idct_altivec;
  180. c->idct_permutation_type = FF_TRANSPOSE_IDCT_PERM;
  181. }
  182. }
  183. }
  184. #endif /* HAVE_ALTIVEC */
  185. }