resample_template.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
  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. #if defined(CONFIG_RESAMPLE_DBL)
  21. #define SET_TYPE(func) func ## _dbl
  22. #define FELEM double
  23. #define FELEM2 double
  24. #define FELEML double
  25. #define OUT(d, v) d = v
  26. #define DBL_TO_FELEM(d, v) d = v
  27. #elif defined(CONFIG_RESAMPLE_FLT)
  28. #define SET_TYPE(func) func ## _flt
  29. #define FELEM float
  30. #define FELEM2 float
  31. #define FELEML float
  32. #define OUT(d, v) d = v
  33. #define DBL_TO_FELEM(d, v) d = v
  34. #elif defined(CONFIG_RESAMPLE_S32)
  35. #define SET_TYPE(func) func ## _s32
  36. #define FELEM int32_t
  37. #define FELEM2 int64_t
  38. #define FELEML int64_t
  39. #define OUT(d, v) d = av_clipl_int32((v + (1 << 29)) >> 30)
  40. #define DBL_TO_FELEM(d, v) d = av_clipl_int32(llrint(v * (1 << 30)));
  41. #else
  42. #define SET_TYPE(func) func ## _s16
  43. #define FELEM int16_t
  44. #define FELEM2 int32_t
  45. #define FELEML int64_t
  46. #define OUT(d, v) d = av_clip_int16((v + (1 << 14)) >> 15)
  47. #define DBL_TO_FELEM(d, v) d = av_clip_int16(lrint(v * (1 << 15)))
  48. #endif
  49. static void SET_TYPE(resample_one)(ResampleContext *c, int no_filter,
  50. void *dst0, int dst_index, const void *src0,
  51. int src_size, int index, int frac)
  52. {
  53. FELEM *dst = dst0;
  54. const FELEM *src = src0;
  55. if (no_filter) {
  56. dst[dst_index] = src[index];
  57. } else {
  58. int i;
  59. int sample_index = index >> c->phase_shift;
  60. FELEM2 val = 0;
  61. FELEM *filter = ((FELEM *)c->filter_bank) +
  62. c->filter_length * (index & c->phase_mask);
  63. if (sample_index < 0) {
  64. for (i = 0; i < c->filter_length; i++)
  65. val += src[FFABS(sample_index + i) % src_size] *
  66. (FELEM2)filter[i];
  67. } else if (c->linear) {
  68. FELEM2 v2 = 0;
  69. for (i = 0; i < c->filter_length; i++) {
  70. val += src[abs(sample_index + i)] * (FELEM2)filter[i];
  71. v2 += src[abs(sample_index + i)] * (FELEM2)filter[i + c->filter_length];
  72. }
  73. val += (v2 - val) * (FELEML)frac / c->src_incr;
  74. } else {
  75. for (i = 0; i < c->filter_length; i++)
  76. val += src[sample_index + i] * (FELEM2)filter[i];
  77. }
  78. OUT(dst[dst_index], val);
  79. }
  80. }
  81. static void SET_TYPE(set_filter)(void *filter0, double *tab, int phase,
  82. int tap_count)
  83. {
  84. int i;
  85. FELEM *filter = ((FELEM *)filter0) + phase * tap_count;
  86. for (i = 0; i < tap_count; i++) {
  87. DBL_TO_FELEM(filter[i], tab[i]);
  88. }
  89. }
  90. #undef SET_TYPE
  91. #undef FELEM
  92. #undef FELEM2
  93. #undef FELEML
  94. #undef OUT
  95. #undef DBL_TO_FELEM