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.

166 lines
5.6KB

  1. /*
  2. * Copyright (c) 2002 Brian Foley
  3. * Copyright (c) 2002 Dieter Shirley
  4. * Copyright (c) 2003-2004 Romain Dolbeau <romain@dolbeau.org>
  5. *
  6. * This file is part of Libav.
  7. *
  8. * Libav is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * Libav is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with Libav; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include <string.h>
  23. #include "libavutil/attributes.h"
  24. #include "libavutil/cpu.h"
  25. #include "libavutil/mem.h"
  26. #include "libavutil/ppc/cpu.h"
  27. #include "libavcodec/avcodec.h"
  28. #include "libavcodec/dsputil.h"
  29. #include "dsputil_altivec.h"
  30. /* ***** WARNING ***** WARNING ***** WARNING ***** */
  31. /*
  32. * clear_blocks_dcbz32_ppc will not work properly on PowerPC processors with
  33. * a cache line size not equal to 32 bytes. Fortunately all processors used
  34. * by Apple up to at least the 7450 (AKA second generation G4) use 32-byte
  35. * cache lines. This is due to the use of the 'dcbz' instruction. It simply
  36. * clears a single cache line to zero, so you need to know the cache line
  37. * size to use it! It's absurd, but it's fast...
  38. *
  39. * update 24/06/2003: Apple released the G5 yesterday, with a PPC970.
  40. * cache line size: 128 bytes. Oups.
  41. * The semantics of dcbz was changed, it always clears 32 bytes. So the function
  42. * below will work, but will be slow. So I fixed check_dcbz_effect to use dcbzl,
  43. * which is defined to clear a cache line (as dcbz before). So we can still
  44. * distinguish, and use dcbz (32 bytes) or dcbzl (one cache line) as required.
  45. *
  46. * see <http://developer.apple.com/technotes/tn/tn2087.html>
  47. * and <http://developer.apple.com/technotes/tn/tn2086.html>
  48. */
  49. static void clear_blocks_dcbz32_ppc(int16_t *blocks)
  50. {
  51. register int misal = (unsigned long) blocks & 0x00000010, i = 0;
  52. if (misal) {
  53. ((unsigned long *) blocks)[0] = 0L;
  54. ((unsigned long *) blocks)[1] = 0L;
  55. ((unsigned long *) blocks)[2] = 0L;
  56. ((unsigned long *) blocks)[3] = 0L;
  57. i += 16;
  58. }
  59. for (; i < sizeof(int16_t) * 6 * 64 - 31; i += 32)
  60. __asm__ volatile ("dcbz %0,%1" :: "b" (blocks), "r" (i) : "memory");
  61. if (misal) {
  62. ((unsigned long *) blocks)[188] = 0L;
  63. ((unsigned long *) blocks)[189] = 0L;
  64. ((unsigned long *) blocks)[190] = 0L;
  65. ((unsigned long *) blocks)[191] = 0L;
  66. i += 16;
  67. }
  68. }
  69. /* Same as above, when dcbzl clears a whole 128 bytes cache line
  70. * i.e. the PPC970 AKA G5. */
  71. static void clear_blocks_dcbz128_ppc(int16_t *blocks)
  72. {
  73. #if HAVE_DCBZL
  74. register int misal = (unsigned long) blocks & 0x0000007f, i = 0;
  75. if (misal) {
  76. /* We could probably also optimize this case,
  77. * but there's not much point as the machines
  78. * aren't available yet (2003-06-26). */
  79. memset(blocks, 0, sizeof(int16_t) * 6 * 64);
  80. } else {
  81. for (; i < sizeof(int16_t) * 6 * 64; i += 128)
  82. __asm__ volatile ("dcbzl %0,%1" :: "b" (blocks), "r" (i) : "memory");
  83. }
  84. #else
  85. memset(blocks, 0, sizeof(int16_t) * 6 * 64);
  86. #endif
  87. }
  88. /* Check dcbz report how many bytes are set to 0 by dcbz. */
  89. /* update 24/06/2003: Replace dcbz by dcbzl to get the intended effect
  90. * (Apple "fixed" dcbz). Unfortunately this cannot be used unless the
  91. * assembler knows about dcbzl ... */
  92. static long check_dcbzl_effect(void)
  93. {
  94. long count = 0;
  95. #if HAVE_DCBZL
  96. register char *fakedata = av_malloc(1024);
  97. register char *fakedata_middle;
  98. register long zero = 0, i = 0;
  99. if (!fakedata)
  100. return 0L;
  101. fakedata_middle = fakedata + 512;
  102. memset(fakedata, 0xFF, 1024);
  103. /* Below the constraint "b" seems to mean "address base register"
  104. * in gcc-3.3 / RS/6000 speaks. Seems to avoid using r0, so.... */
  105. __asm__ volatile ("dcbzl %0, %1" :: "b" (fakedata_middle), "r" (zero));
  106. for (i = 0; i < 1024; i++)
  107. if (fakedata[i] == (char) 0)
  108. count++;
  109. av_free(fakedata);
  110. #endif
  111. return count;
  112. }
  113. av_cold void ff_dsputil_init_ppc(DSPContext *c, AVCodecContext *avctx,
  114. unsigned high_bit_depth)
  115. {
  116. // common optimizations whether AltiVec is available or not
  117. if (!high_bit_depth) {
  118. switch (check_dcbzl_effect()) {
  119. case 32:
  120. c->clear_blocks = clear_blocks_dcbz32_ppc;
  121. break;
  122. case 128:
  123. c->clear_blocks = clear_blocks_dcbz128_ppc;
  124. break;
  125. default:
  126. break;
  127. }
  128. }
  129. if (PPC_ALTIVEC(av_get_cpu_flags())) {
  130. ff_dsputil_init_altivec(c, avctx, high_bit_depth);
  131. ff_int_init_altivec(c, avctx);
  132. c->gmc1 = ff_gmc1_altivec;
  133. if (!high_bit_depth) {
  134. #if CONFIG_ENCODERS
  135. if (avctx->dct_algo == FF_DCT_AUTO ||
  136. avctx->dct_algo == FF_DCT_ALTIVEC) {
  137. c->fdct = ff_fdct_altivec;
  138. }
  139. #endif //CONFIG_ENCODERS
  140. if ((avctx->idct_algo == FF_IDCT_AUTO) ||
  141. (avctx->idct_algo == FF_IDCT_ALTIVEC)) {
  142. c->idct_put = ff_idct_put_altivec;
  143. c->idct_add = ff_idct_add_altivec;
  144. c->idct_permutation_type = FF_TRANSPOSE_IDCT_PERM;
  145. }
  146. }
  147. }
  148. }