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.

111 lines
3.5KB

  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. #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_nearest)(void *dst0, int dst_index, const void *src0, int index)
  54. {
  55. FELEM *dst = dst0;
  56. const FELEM *src = src0;
  57. dst[dst_index] = src[index];
  58. }
  59. static void SET_TYPE(resample_one)(ResampleContext *c,
  60. void *dst0, int dst_index, const void *src0,
  61. int src_size, int index, int frac)
  62. {
  63. FELEM *dst = dst0;
  64. const FELEM *src = src0;
  65. int i;
  66. int sample_index = index >> c->phase_shift;
  67. FELEM2 val = 0;
  68. FELEM *filter = ((FELEM *)c->filter_bank) +
  69. c->filter_length * (index & c->phase_mask);
  70. if (sample_index < 0) {
  71. for (i = 0; i < c->filter_length; i++)
  72. val += src[FFABS(sample_index + i) % src_size] *
  73. (FELEM2)filter[i];
  74. } else if (c->linear) {
  75. FELEM2 v2 = 0;
  76. for (i = 0; i < c->filter_length; i++) {
  77. val += src[sample_index + i] * (FELEM2)filter[i];
  78. v2 += src[sample_index + i] * (FELEM2)filter[i + c->filter_length];
  79. }
  80. val += (v2 - val) * (FELEML)frac / c->src_incr;
  81. } else {
  82. for (i = 0; i < c->filter_length; i++)
  83. val += src[sample_index + i] * (FELEM2)filter[i];
  84. }
  85. OUT(dst[dst_index], val);
  86. }
  87. static void SET_TYPE(set_filter)(void *filter0, double *tab, int phase,
  88. int tap_count)
  89. {
  90. int i;
  91. FELEM *filter = ((FELEM *)filter0) + phase * tap_count;
  92. for (i = 0; i < tap_count; i++) {
  93. DBL_TO_FELEM(filter[i], tab[i]);
  94. }
  95. }
  96. #undef SET_TYPE
  97. #undef FELEM
  98. #undef FELEM2
  99. #undef FELEML
  100. #undef OUT
  101. #undef DBL_TO_FELEM