resample_template.c 3.7 KB

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