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.

178 lines
5.8KB

  1. /*
  2. * Copyright (c) 2006 Luca Barbato <lu_zero@gentoo.org>
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "config.h"
  21. #include "libavutil/attributes.h"
  22. #include "libavutil/cpu.h"
  23. #include "libavutil/mem.h"
  24. #include "libavutil/ppc/util_altivec.h"
  25. #include "libavcodec/fmtconvert.h"
  26. #include "dsputil_altivec.h"
  27. #if HAVE_ALTIVEC
  28. static void int32_to_float_fmul_scalar_altivec(float *dst, const int32_t *src,
  29. float mul, int len)
  30. {
  31. union {
  32. vector float v;
  33. float s[4];
  34. } mul_u;
  35. int i;
  36. vector float src1, src2, dst1, dst2, mul_v, zero;
  37. zero = (vector float)vec_splat_u32(0);
  38. mul_u.s[0] = mul;
  39. mul_v = vec_splat(mul_u.v, 0);
  40. for (i = 0; i < len; i += 8) {
  41. src1 = vec_ctf(vec_ld(0, src+i), 0);
  42. src2 = vec_ctf(vec_ld(16, src+i), 0);
  43. dst1 = vec_madd(src1, mul_v, zero);
  44. dst2 = vec_madd(src2, mul_v, zero);
  45. vec_st(dst1, 0, dst+i);
  46. vec_st(dst2, 16, dst+i);
  47. }
  48. }
  49. static vector signed short float_to_int16_one_altivec(const float *src)
  50. {
  51. vector float s0 = vec_ld(0, src);
  52. vector float s1 = vec_ld(16, src);
  53. vector signed int t0 = vec_cts(s0, 0);
  54. vector signed int t1 = vec_cts(s1, 0);
  55. return vec_packs(t0,t1);
  56. }
  57. static void float_to_int16_altivec(int16_t *dst, const float *src, long len)
  58. {
  59. int i;
  60. vector signed short d0, d1, d;
  61. vector unsigned char align;
  62. if (((long)dst) & 15) { //FIXME
  63. for (i = 0; i < len - 7; i += 8) {
  64. d0 = vec_ld(0, dst+i);
  65. d = float_to_int16_one_altivec(src + i);
  66. d1 = vec_ld(15, dst+i);
  67. d1 = vec_perm(d1, d0, vec_lvsl(0, dst + i));
  68. align = vec_lvsr(0, dst + i);
  69. d0 = vec_perm(d1, d, align);
  70. d1 = vec_perm(d, d1, align);
  71. vec_st(d0, 0, dst + i);
  72. vec_st(d1, 15, dst + i);
  73. }
  74. } else {
  75. for (i = 0; i < len - 7; i += 8) {
  76. d = float_to_int16_one_altivec(src + i);
  77. vec_st(d, 0, dst + i);
  78. }
  79. }
  80. }
  81. #define VSTE_INC(dst, v, elem, inc) do { \
  82. vector signed short s = vec_splat(v, elem); \
  83. vec_ste(s, 0, dst); \
  84. dst += inc; \
  85. } while (0)
  86. static void float_to_int16_stride_altivec(int16_t *dst, const float *src,
  87. long len, int stride)
  88. {
  89. int i;
  90. vector signed short d;
  91. for (i = 0; i < len - 7; i += 8) {
  92. d = float_to_int16_one_altivec(src + i);
  93. VSTE_INC(dst, d, 0, stride);
  94. VSTE_INC(dst, d, 1, stride);
  95. VSTE_INC(dst, d, 2, stride);
  96. VSTE_INC(dst, d, 3, stride);
  97. VSTE_INC(dst, d, 4, stride);
  98. VSTE_INC(dst, d, 5, stride);
  99. VSTE_INC(dst, d, 6, stride);
  100. VSTE_INC(dst, d, 7, stride);
  101. }
  102. }
  103. static void float_to_int16_interleave_altivec(int16_t *dst, const float **src,
  104. long len, int channels)
  105. {
  106. int i;
  107. vector signed short d0, d1, d2, c0, c1, t0, t1;
  108. vector unsigned char align;
  109. if (channels == 1)
  110. float_to_int16_altivec(dst, src[0], len);
  111. else {
  112. if (channels == 2) {
  113. if (((long)dst) & 15) {
  114. for (i = 0; i < len - 7; i += 8) {
  115. d0 = vec_ld(0, dst + i);
  116. t0 = float_to_int16_one_altivec(src[0] + i);
  117. d1 = vec_ld(31, dst + i);
  118. t1 = float_to_int16_one_altivec(src[1] + i);
  119. c0 = vec_mergeh(t0, t1);
  120. c1 = vec_mergel(t0, t1);
  121. d2 = vec_perm(d1, d0, vec_lvsl(0, dst + i));
  122. align = vec_lvsr(0, dst + i);
  123. d0 = vec_perm(d2, c0, align);
  124. d1 = vec_perm(c0, c1, align);
  125. vec_st(d0, 0, dst + i);
  126. d0 = vec_perm(c1, d2, align);
  127. vec_st(d1, 15, dst + i);
  128. vec_st(d0, 31, dst + i);
  129. dst += 8;
  130. }
  131. } else {
  132. for (i = 0; i < len - 7; i += 8) {
  133. t0 = float_to_int16_one_altivec(src[0] + i);
  134. t1 = float_to_int16_one_altivec(src[1] + i);
  135. d0 = vec_mergeh(t0, t1);
  136. d1 = vec_mergel(t0, t1);
  137. vec_st(d0, 0, dst + i);
  138. vec_st(d1, 16, dst + i);
  139. dst += 8;
  140. }
  141. }
  142. } else {
  143. for (i = 0; i < channels; i++)
  144. float_to_int16_stride_altivec(dst + i, src[i], len, channels);
  145. }
  146. }
  147. }
  148. #endif /* HAVE_ALTIVEC */
  149. av_cold void ff_fmt_convert_init_ppc(FmtConvertContext *c,
  150. AVCodecContext *avctx)
  151. {
  152. #if HAVE_ALTIVEC
  153. if (!(av_get_cpu_flags() & AV_CPU_FLAG_ALTIVEC))
  154. return;
  155. c->int32_to_float_fmul_scalar = int32_to_float_fmul_scalar_altivec;
  156. if (!(avctx->flags & CODEC_FLAG_BITEXACT)) {
  157. c->float_to_int16 = float_to_int16_altivec;
  158. c->float_to_int16_interleave = float_to_int16_interleave_altivec;
  159. }
  160. #endif /* HAVE_ALTIVEC */
  161. }