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.

143 lines
4.4KB

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