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.

157 lines
5.1KB

  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 "libavcodec/fmtconvert.h"
  21. #include "libavutil/ppc/util_altivec.h"
  22. #include "libavutil/mem.h"
  23. #include "dsputil_altivec.h"
  24. static void int32_to_float_fmul_scalar_altivec(float *dst, const int *src,
  25. float mul, int len)
  26. {
  27. union {
  28. vector float v;
  29. float s[4];
  30. } mul_u;
  31. int i;
  32. vector float src1, src2, dst1, dst2, mul_v, zero;
  33. zero = (vector float)vec_splat_u32(0);
  34. mul_u.s[0] = mul;
  35. mul_v = vec_splat(mul_u.v, 0);
  36. for (i = 0; i < len; i += 8) {
  37. src1 = vec_ctf(vec_ld(0, src+i), 0);
  38. src2 = vec_ctf(vec_ld(16, src+i), 0);
  39. dst1 = vec_madd(src1, mul_v, zero);
  40. dst2 = vec_madd(src2, mul_v, zero);
  41. vec_st(dst1, 0, dst+i);
  42. vec_st(dst2, 16, dst+i);
  43. }
  44. }
  45. static vector signed short float_to_int16_one_altivec(const float *src)
  46. {
  47. vector float s0 = vec_ld(0, src);
  48. vector float s1 = vec_ld(16, src);
  49. vector signed int t0 = vec_cts(s0, 0);
  50. vector signed int t1 = vec_cts(s1, 0);
  51. return vec_packs(t0,t1);
  52. }
  53. static void float_to_int16_altivec(int16_t *dst, const float *src, long len)
  54. {
  55. int i;
  56. vector signed short d0, d1, d;
  57. vector unsigned char align;
  58. if (((long)dst) & 15) { //FIXME
  59. for (i = 0; i < len - 7; i += 8) {
  60. d0 = vec_ld(0, dst+i);
  61. d = float_to_int16_one_altivec(src + i);
  62. d1 = vec_ld(15, dst+i);
  63. d1 = vec_perm(d1, d0, vec_lvsl(0, dst + i));
  64. align = vec_lvsr(0, dst + i);
  65. d0 = vec_perm(d1, d, align);
  66. d1 = vec_perm(d, d1, align);
  67. vec_st(d0, 0, dst + i);
  68. vec_st(d1, 15, dst + i);
  69. }
  70. } else {
  71. for (i = 0; i < len - 7; i += 8) {
  72. d = float_to_int16_one_altivec(src + i);
  73. vec_st(d, 0, dst + i);
  74. }
  75. }
  76. }
  77. static void float_to_int16_stride_altivec(int16_t *dst, const float *src,
  78. long len, int stride)
  79. {
  80. int i, j;
  81. vector signed short d, s;
  82. for (i = 0; i < len - 7; i += 8) {
  83. d = float_to_int16_one_altivec(src + i);
  84. for (j = 0; j < 8; j++) {
  85. s = vec_splat(d, j);
  86. vec_ste(s, 0, dst);
  87. dst += stride;
  88. }
  89. }
  90. }
  91. static void float_to_int16_interleave_altivec(int16_t *dst, const float **src,
  92. long len, int channels)
  93. {
  94. int i;
  95. vector signed short d0, d1, d2, c0, c1, t0, t1;
  96. vector unsigned char align;
  97. if (channels == 1)
  98. float_to_int16_altivec(dst, src[0], len);
  99. else {
  100. if (channels == 2) {
  101. if (((long)dst) & 15) {
  102. for (i = 0; i < len - 7; i += 8) {
  103. d0 = vec_ld(0, dst + i);
  104. t0 = float_to_int16_one_altivec(src[0] + i);
  105. d1 = vec_ld(31, dst + i);
  106. t1 = float_to_int16_one_altivec(src[1] + i);
  107. c0 = vec_mergeh(t0, t1);
  108. c1 = vec_mergel(t0, t1);
  109. d2 = vec_perm(d1, d0, vec_lvsl(0, dst + i));
  110. align = vec_lvsr(0, dst + i);
  111. d0 = vec_perm(d2, c0, align);
  112. d1 = vec_perm(c0, c1, align);
  113. vec_st(d0, 0, dst + i);
  114. d0 = vec_perm(c1, d2, align);
  115. vec_st(d1, 15, dst + i);
  116. vec_st(d0, 31, dst + i);
  117. dst += 8;
  118. }
  119. } else {
  120. for (i = 0; i < len - 7; i += 8) {
  121. t0 = float_to_int16_one_altivec(src[0] + i);
  122. t1 = float_to_int16_one_altivec(src[1] + i);
  123. d0 = vec_mergeh(t0, t1);
  124. d1 = vec_mergel(t0, t1);
  125. vec_st(d0, 0, dst + i);
  126. vec_st(d1, 16, dst + i);
  127. dst += 8;
  128. }
  129. }
  130. } else {
  131. for (i = 0; i < channels; i++)
  132. float_to_int16_stride_altivec(dst + i, src[i], len, channels);
  133. }
  134. }
  135. }
  136. void ff_fmt_convert_init_altivec(FmtConvertContext *c, AVCodecContext *avctx)
  137. {
  138. c->int32_to_float_fmul_scalar = int32_to_float_fmul_scalar_altivec;
  139. if (!(avctx->flags & CODEC_FLAG_BITEXACT)) {
  140. c->float_to_int16 = float_to_int16_altivec;
  141. c->float_to_int16_interleave = float_to_int16_interleave_altivec;
  142. }
  143. }