resample_init.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Audio resampling
  3. *
  4. * Copyright (c) 2004-2012 Michael Niedermayer <michaelni@gmx.at>
  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 "config.h"
  23. #include "libavutil/cpu.h"
  24. #include "libavutil/avassert.h"
  25. #include "libavutil/aarch64/cpu.h"
  26. #include "libswresample/resample.h"
  27. #define DECLARE_RESAMPLE_COMMON_TEMPLATE(TYPE, DELEM, FELEM, FELEM2, OUT) \
  28. \
  29. void ff_resample_common_apply_filter_x4_##TYPE##_neon(FELEM2 *acc, const DELEM *src, \
  30. const FELEM *filter, int length); \
  31. \
  32. void ff_resample_common_apply_filter_x8_##TYPE##_neon(FELEM2 *acc, const DELEM *src, \
  33. const FELEM *filter, int length); \
  34. \
  35. static int ff_resample_common_##TYPE##_neon(ResampleContext *c, void *dest, const void *source, \
  36. int n, int update_ctx) \
  37. { \
  38. DELEM *dst = dest; \
  39. const DELEM *src = source; \
  40. int dst_index; \
  41. int index = c->index; \
  42. int frac = c->frac; \
  43. int sample_index = 0; \
  44. int x4_aligned_filter_length = c->filter_length & ~3; \
  45. int x8_aligned_filter_length = c->filter_length & ~7; \
  46. \
  47. while (index >= c->phase_count) { \
  48. sample_index++; \
  49. index -= c->phase_count; \
  50. } \
  51. \
  52. for (dst_index = 0; dst_index < n; dst_index++) { \
  53. FELEM *filter = ((FELEM *) c->filter_bank) + c->filter_alloc * index; \
  54. \
  55. FELEM2 val = 0; \
  56. int i = 0; \
  57. if (x8_aligned_filter_length >= 8) { \
  58. ff_resample_common_apply_filter_x8_##TYPE##_neon(&val, &src[sample_index], \
  59. filter, x8_aligned_filter_length); \
  60. i += x8_aligned_filter_length; \
  61. \
  62. } else if (x4_aligned_filter_length >= 4) { \
  63. ff_resample_common_apply_filter_x4_##TYPE##_neon(&val, &src[sample_index], \
  64. filter, x4_aligned_filter_length); \
  65. i += x4_aligned_filter_length; \
  66. } \
  67. for (; i < c->filter_length; i++) { \
  68. val += src[sample_index + i] * (FELEM2)filter[i]; \
  69. } \
  70. OUT(dst[dst_index], val); \
  71. \
  72. frac += c->dst_incr_mod; \
  73. index += c->dst_incr_div; \
  74. if (frac >= c->src_incr) { \
  75. frac -= c->src_incr; \
  76. index++; \
  77. } \
  78. \
  79. while (index >= c->phase_count) { \
  80. sample_index++; \
  81. index -= c->phase_count; \
  82. } \
  83. } \
  84. \
  85. if (update_ctx) { \
  86. c->frac = frac; \
  87. c->index = index; \
  88. } \
  89. \
  90. return sample_index; \
  91. } \
  92. #define OUT(d, v) d = v
  93. DECLARE_RESAMPLE_COMMON_TEMPLATE(float, float, float, float, OUT)
  94. #undef OUT
  95. #define OUT(d, v) (v) = ((v) + (1<<(14)))>>15; (d) = av_clip_int16(v)
  96. DECLARE_RESAMPLE_COMMON_TEMPLATE(s16, int16_t, int16_t, int32_t, OUT)
  97. #undef OUT
  98. av_cold void swri_resample_dsp_aarch64_init(ResampleContext *c)
  99. {
  100. int cpu_flags = av_get_cpu_flags();
  101. if (!have_neon(cpu_flags))
  102. return;
  103. switch(c->format) {
  104. case AV_SAMPLE_FMT_FLTP:
  105. c->dsp.resample_common = ff_resample_common_float_neon;
  106. break;
  107. case AV_SAMPLE_FMT_S16P:
  108. c->dsp.resample_common = ff_resample_common_s16_neon;
  109. break;
  110. }
  111. }