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.

109 lines
3.5KB

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