resample_init.c 8.0 KB

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