Coverage Report

Created: 2026-10-07 06:29

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gstreamer/subprojects/gst-plugins-base/gst-libs/gst/audio/audio-resampler.c
Line
Count
Source
1
/* GStreamer
2
 * Copyright (C) <2015> Wim Taymans <wim.taymans@gmail.com>
3
 *
4
 * This library is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Library General Public
6
 * License as published by the Free Software Foundation; either
7
 * version 2 of the License, or (at your option) any later version.
8
 *
9
 * This library is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
 * Library General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Library General Public
15
 * License along with this library; if not, write to the
16
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17
 * Boston, MA 02110-1301, USA.
18
 */
19
20
#ifdef HAVE_CONFIG_H
21
#  include "config.h"
22
#endif
23
24
#include <string.h>
25
#include <stdio.h>
26
#include <math.h>
27
28
#include "audio-resampler.h"
29
#include "audio-resampler-private.h"
30
#include "audio-resampler-macros.h"
31
32
6.56k
#define MEM_ALIGN(m,a) ((gint8 *)((guintptr)((gint8 *)(m) + ((a)-1)) & ~((a)-1)))
33
6.56k
#define ALIGN 16
34
#define TAPS_OVERREAD 16
35
36
GST_DEBUG_CATEGORY_STATIC (audio_resampler_debug);
37
#define GST_CAT_DEFAULT audio_resampler_debug
38
39
/**
40
 * SECTION:gstaudioresampler
41
 * @title: GstAudioResampler
42
 * @short_description: Utility structure for resampler information
43
 *
44
 * #GstAudioResampler is a structure which holds the information
45
 * required to perform various kinds of resampling filtering.
46
 *
47
 */
48
49
static const gint oversample_qualities[] = {
50
  4, 4, 4, 8, 8, 16, 16, 16, 16, 32, 32
51
};
52
53
typedef struct
54
{
55
  gdouble cutoff;
56
  gdouble downsample_cutoff_factor;
57
  gdouble stopband_attenuation;
58
  gdouble transition_bandwidth;
59
} KaiserQualityMap;
60
61
static const KaiserQualityMap kaiser_qualities[] = {
62
  {0.860, 0.96511, 60, 0.7},    /* 8 taps */
63
  {0.880, 0.96591, 65, 0.29},   /* 16 taps */
64
  {0.910, 0.96923, 70, 0.145},  /* 32 taps */
65
  {0.920, 0.97600, 80, 0.105},  /* 48 taps */
66
  {0.940, 0.97979, 85, 0.087},  /* 64 taps default quality */
67
  {0.940, 0.98085, 95, 0.077},  /* 80 taps */
68
  {0.945, 0.99471, 100, 0.068}, /* 96 taps */
69
  {0.950, 1.0, 105, 0.055},     /* 128 taps */
70
  {0.960, 1.0, 110, 0.045},     /* 160 taps */
71
  {0.968, 1.0, 115, 0.039},     /* 192 taps */
72
  {0.975, 1.0, 120, 0.0305}     /* 256 taps */
73
};
74
75
typedef struct
76
{
77
  gint n_taps;
78
  gdouble cutoff;
79
} BlackmanQualityMap;
80
81
static const BlackmanQualityMap blackman_qualities[] = {
82
  {8, 0.5,},
83
  {16, 0.6,},
84
  {24, 0.72,},
85
  {32, 0.8,},
86
  {48, 0.85,},                  /* default */
87
  {64, 0.90,},
88
  {80, 0.92,},
89
  {96, 0.933,},
90
  {128, 0.950,},
91
  {148, 0.955,},
92
  {160, 0.960,}
93
};
94
95
2.50k
#define DEFAULT_RESAMPLER_METHOD GST_AUDIO_RESAMPLER_METHOD_KAISER
96
2.00k
#define DEFAULT_QUALITY GST_AUDIO_RESAMPLER_QUALITY_DEFAULT
97
397
#define DEFAULT_OPT_CUBIC_B 1.0
98
397
#define DEFAULT_OPT_CUBIC_C 0.0
99
2.00k
#define DEFAULT_OPT_FILTER_MODE GST_AUDIO_RESAMPLER_FILTER_MODE_AUTO
100
2.00k
#define DEFAULT_OPT_FILTER_MODE_THRESHOLD 1048576
101
2.00k
#define DEFAULT_OPT_FILTER_INTERPOLATION GST_AUDIO_RESAMPLER_FILTER_INTERPOLATION_CUBIC
102
2.00k
#define DEFAULT_OPT_FILTER_OVERSAMPLE 8
103
3.87k
#define DEFAULT_OPT_MAX_PHASE_ERROR 0.1
104
105
static gdouble
106
get_opt_double (GstStructure * options, const gchar * name, gdouble def)
107
7.18k
{
108
7.18k
  gdouble res;
109
7.18k
  if (!options || !gst_structure_get_double (options, name, &res))
110
6.02k
    res = def;
111
7.18k
  return res;
112
7.18k
}
113
114
static gint
115
get_opt_int (GstStructure * options, const gchar * name, gint def)
116
6.37k
{
117
6.37k
  gint res;
118
6.37k
  if (!options || !gst_structure_get_int (options, name, &res))
119
5.73k
    res = def;
120
6.37k
  return res;
121
6.37k
}
122
123
static gint
124
get_opt_enum (GstStructure * options, const gchar * name, GType type, gint def)
125
4.00k
{
126
4.00k
  gint res;
127
4.00k
  if (!options || !gst_structure_get_enum (options, name, type, &res))
128
4.00k
    res = def;
129
4.00k
  return res;
130
4.00k
}
131
132
133
2.00k
#define GET_OPT_CUTOFF(options,def) get_opt_double(options, \
134
2.00k
    GST_AUDIO_RESAMPLER_OPT_CUTOFF,def)
135
#define GET_OPT_DOWN_CUTOFF_FACTOR(options,def) get_opt_double(options, \
136
    GST_AUDIO_RESAMPLER_OPT_DOWN_CUTOFF_FACTOR, def)
137
259
#define GET_OPT_STOP_ATTENUATION(options,def) get_opt_double(options, \
138
259
    GST_AUDIO_RESAMPLER_OPT_STOP_ATTENUATION, def)
139
259
#define GET_OPT_TRANSITION_BANDWIDTH(options,def) get_opt_double(options, \
140
259
    GST_AUDIO_RESAMPLER_OPT_TRANSITION_BANDWIDTH, def)
141
397
#define GET_OPT_CUBIC_B(options) get_opt_double(options, \
142
397
    GST_AUDIO_RESAMPLER_OPT_CUBIC_B, DEFAULT_OPT_CUBIC_B)
143
397
#define GET_OPT_CUBIC_C(options) get_opt_double(options, \
144
397
    GST_AUDIO_RESAMPLER_OPT_CUBIC_C, DEFAULT_OPT_CUBIC_C)
145
2.37k
#define GET_OPT_N_TAPS(options,def) get_opt_int(options, \
146
2.37k
    GST_AUDIO_RESAMPLER_OPT_N_TAPS, def)
147
2.00k
#define GET_OPT_FILTER_MODE(options) get_opt_enum(options, \
148
2.00k
    GST_AUDIO_RESAMPLER_OPT_FILTER_MODE, GST_TYPE_AUDIO_RESAMPLER_FILTER_MODE, \
149
2.00k
    DEFAULT_OPT_FILTER_MODE)
150
2.00k
#define GET_OPT_FILTER_MODE_THRESHOLD(options) get_opt_int(options, \
151
2.00k
    GST_AUDIO_RESAMPLER_OPT_FILTER_MODE_THRESHOLD, DEFAULT_OPT_FILTER_MODE_THRESHOLD)
152
2.00k
#define GET_OPT_FILTER_INTERPOLATION(options) get_opt_enum(options, \
153
2.00k
    GST_AUDIO_RESAMPLER_OPT_FILTER_INTERPOLATION, GST_TYPE_AUDIO_RESAMPLER_FILTER_INTERPOLATION, \
154
2.00k
    DEFAULT_OPT_FILTER_INTERPOLATION)
155
2.00k
#define GET_OPT_FILTER_OVERSAMPLE(options) get_opt_int(options, \
156
2.00k
    GST_AUDIO_RESAMPLER_OPT_FILTER_OVERSAMPLE, DEFAULT_OPT_FILTER_OVERSAMPLE)
157
3.87k
#define GET_OPT_MAX_PHASE_ERROR(options) get_opt_double(options, \
158
3.87k
    GST_AUDIO_RESAMPLER_OPT_MAX_PHASE_ERROR, DEFAULT_OPT_MAX_PHASE_ERROR)
159
160
#include "dbesi0.c"
161
3.83M
#define bessel dbesi0
162
163
static inline gdouble
164
get_linear_tap (gdouble x, gint n_taps)
165
792k
{
166
792k
  gdouble res = GST_ROUND_UP_2 (n_taps) / 2 - fabs (x);
167
792k
  return res;
168
792k
}
169
170
static inline gdouble
171
get_cubic_tap (gdouble x, gint n_taps, gdouble b, gdouble c)
172
1.62M
{
173
1.62M
  gdouble res, a, a2, a3;
174
175
1.62M
  a = fabs (x * 4.0) / n_taps;
176
1.62M
  a2 = a * a;
177
1.62M
  a3 = a2 * a;
178
179
1.62M
  if (a <= 1.0)
180
810k
    res = ((12.0 - 9.0 * b - 6.0 * c) * a3 +
181
810k
        (-18.0 + 12.0 * b + 6.0 * c) * a2 + (6.0 - 2.0 * b)) / 6.0;
182
810k
  else if (a <= 2.0)
183
809k
    res = ((-b - 6.0 * c) * a3 +
184
809k
        (6.0 * b + 30.0 * c) * a2 +
185
809k
        (-12.0 * b - 48.0 * c) * a + (8.0 * b + 24.0 * c)) / 6.0;
186
573
  else
187
573
    res = 0.0;
188
189
1.62M
  return res;
190
1.62M
}
191
192
static inline gdouble
193
get_blackman_nuttall_tap (gdouble x, gint n_taps, gdouble Fc)
194
14.4M
{
195
14.4M
  gdouble s, y, w;
196
197
14.4M
  y = G_PI * x;
198
14.4M
  s = (y == 0.0 ? Fc : sin (y * Fc) / y);
199
200
14.4M
  w = 2.0 * y / n_taps + G_PI;
201
14.4M
  return s * (0.3635819 - 0.4891775 * cos (w) + 0.1365995 * cos (2 * w) -
202
14.4M
      0.0106411 * cos (3 * w));
203
14.4M
}
204
205
static inline gdouble
206
get_kaiser_tap (gdouble x, gint n_taps, gdouble Fc, gdouble beta)
207
3.83M
{
208
3.83M
  gdouble s, y, w;
209
210
3.83M
  y = G_PI * x;
211
3.83M
  s = (y == 0.0 ? Fc : sin (y * Fc) / y);
212
213
3.83M
  w = 2.0 * x / n_taps;
214
3.83M
  return s * bessel (beta * sqrt (MAX (1 - w * w, 0)));
215
3.83M
}
216
217
#define MAKE_CONVERT_TAPS_INT_FUNC(type, precision)                     \
218
static void                                                             \
219
convert_taps_##type##_c (gdouble *tmp_taps, gpointer taps,              \
220
500k
    gdouble weight, gint n_taps)                                        \
221
500k
{                                                                       \
222
500k
  gint64 one = (1LL << precision) - 1;                                  \
223
500k
  type *t = taps;                                                       \
224
500k
  gdouble multiplier = one;                                             \
225
500k
  gint i, j;                                                            \
226
500k
  gdouble offset, l_offset, h_offset;                                   \
227
500k
  gboolean exact = FALSE;                                               \
228
500k
  /* Round to integer, but with an adjustable bias that we use to */    \
229
500k
  /* eliminate the DC error. */                                         \
230
500k
  l_offset = 0.0;                                                       \
231
500k
  h_offset = 1.0;                                                       \
232
500k
  offset = 0.5;                                                         \
233
846k
  for (i = 0; i < 32; i++) {                                            \
234
845k
    gint64 sum = 0;                                                     \
235
271M
    for (j = 0; j < n_taps; j++)                                        \
236
271M
      sum += floor (offset + tmp_taps[j] * multiplier / weight);        \
237
845k
    if (sum == one) {                                                   \
238
499k
      exact = TRUE;                                                     \
239
499k
      break;                                                            \
240
499k
    }                                                                   \
241
845k
    if (l_offset == h_offset)                                           \
242
345k
      break;                                                            \
243
345k
    if (sum < one) {                                                    \
244
167k
      if (offset > l_offset)                                            \
245
167k
        l_offset = offset;                                              \
246
167k
      offset += (h_offset - l_offset) / 2;                              \
247
178k
    } else {                                                            \
248
178k
      if (offset < h_offset)                                            \
249
178k
        h_offset = offset;                                              \
250
178k
      offset -= (h_offset - l_offset) / 2;                              \
251
178k
    }                                                                   \
252
345k
  }                                                                     \
253
14.8M
  for (j = 0; j < n_taps; j++)                                          \
254
14.3M
    t[j] = floor (offset + tmp_taps[j] * multiplier / weight);          \
255
500k
  if (!exact)                                                           \
256
500k
    GST_DEBUG ("can't find exact taps");                                \
257
500k
}
audio-resampler.c:convert_taps_gint16_c
Line
Count
Source
220
332k
    gdouble weight, gint n_taps)                                        \
221
332k
{                                                                       \
222
332k
  gint64 one = (1LL << precision) - 1;                                  \
223
332k
  type *t = taps;                                                       \
224
332k
  gdouble multiplier = one;                                             \
225
332k
  gint i, j;                                                            \
226
332k
  gdouble offset, l_offset, h_offset;                                   \
227
332k
  gboolean exact = FALSE;                                               \
228
332k
  /* Round to integer, but with an adjustable bias that we use to */    \
229
332k
  /* eliminate the DC error. */                                         \
230
332k
  l_offset = 0.0;                                                       \
231
332k
  h_offset = 1.0;                                                       \
232
332k
  offset = 0.5;                                                         \
233
498k
  for (i = 0; i < 32; i++) {                                            \
234
497k
    gint64 sum = 0;                                                     \
235
68.9M
    for (j = 0; j < n_taps; j++)                                        \
236
68.4M
      sum += floor (offset + tmp_taps[j] * multiplier / weight);        \
237
497k
    if (sum == one) {                                                   \
238
332k
      exact = TRUE;                                                     \
239
332k
      break;                                                            \
240
332k
    }                                                                   \
241
497k
    if (l_offset == h_offset)                                           \
242
165k
      break;                                                            \
243
165k
    if (sum < one) {                                                    \
244
82.4k
      if (offset > l_offset)                                            \
245
82.4k
        l_offset = offset;                                              \
246
82.4k
      offset += (h_offset - l_offset) / 2;                              \
247
82.7k
    } else {                                                            \
248
82.7k
      if (offset < h_offset)                                            \
249
82.7k
        h_offset = offset;                                              \
250
82.7k
      offset -= (h_offset - l_offset) / 2;                              \
251
82.7k
    }                                                                   \
252
165k
  }                                                                     \
253
4.53M
  for (j = 0; j < n_taps; j++)                                          \
254
4.20M
    t[j] = floor (offset + tmp_taps[j] * multiplier / weight);          \
255
332k
  if (!exact)                                                           \
256
332k
    GST_DEBUG ("can't find exact taps");                                \
257
332k
}
audio-resampler.c:convert_taps_gint32_c
Line
Count
Source
220
167k
    gdouble weight, gint n_taps)                                        \
221
167k
{                                                                       \
222
167k
  gint64 one = (1LL << precision) - 1;                                  \
223
167k
  type *t = taps;                                                       \
224
167k
  gdouble multiplier = one;                                             \
225
167k
  gint i, j;                                                            \
226
167k
  gdouble offset, l_offset, h_offset;                                   \
227
167k
  gboolean exact = FALSE;                                               \
228
167k
  /* Round to integer, but with an adjustable bias that we use to */    \
229
167k
  /* eliminate the DC error. */                                         \
230
167k
  l_offset = 0.0;                                                       \
231
167k
  h_offset = 1.0;                                                       \
232
167k
  offset = 0.5;                                                         \
233
348k
  for (i = 0; i < 32; i++) {                                            \
234
348k
    gint64 sum = 0;                                                     \
235
202M
    for (j = 0; j < n_taps; j++)                                        \
236
202M
      sum += floor (offset + tmp_taps[j] * multiplier / weight);        \
237
348k
    if (sum == one) {                                                   \
238
167k
      exact = TRUE;                                                     \
239
167k
      break;                                                            \
240
167k
    }                                                                   \
241
348k
    if (l_offset == h_offset)                                           \
242
180k
      break;                                                            \
243
180k
    if (sum < one) {                                                    \
244
85.4k
      if (offset > l_offset)                                            \
245
85.4k
        l_offset = offset;                                              \
246
85.4k
      offset += (h_offset - l_offset) / 2;                              \
247
95.2k
    } else {                                                            \
248
95.2k
      if (offset < h_offset)                                            \
249
95.2k
        h_offset = offset;                                              \
250
95.2k
      offset -= (h_offset - l_offset) / 2;                              \
251
95.2k
    }                                                                   \
252
180k
  }                                                                     \
253
10.2M
  for (j = 0; j < n_taps; j++)                                          \
254
10.1M
    t[j] = floor (offset + tmp_taps[j] * multiplier / weight);          \
255
167k
  if (!exact)                                                           \
256
167k
    GST_DEBUG ("can't find exact taps");                                \
257
167k
}
258
259
#define MAKE_CONVERT_TAPS_FLOAT_FUNC(type)                              \
260
static void                                                             \
261
convert_taps_##type##_c (gdouble *tmp_taps, gpointer taps,              \
262
296k
    gdouble weight, gint n_taps)                                        \
263
296k
{                                                                       \
264
296k
  gint i;                                                               \
265
296k
  type *t = taps;                                                       \
266
6.67M
  for (i = 0; i < n_taps; i++)                                          \
267
6.38M
    t[i] = tmp_taps[i] / weight;                                        \
268
296k
}
audio-resampler.c:convert_taps_gfloat_c
Line
Count
Source
262
290k
    gdouble weight, gint n_taps)                                        \
263
290k
{                                                                       \
264
290k
  gint i;                                                               \
265
290k
  type *t = taps;                                                       \
266
3.22M
  for (i = 0; i < n_taps; i++)                                          \
267
2.93M
    t[i] = tmp_taps[i] / weight;                                        \
268
290k
}
audio-resampler.c:convert_taps_gdouble_c
Line
Count
Source
262
5.68k
    gdouble weight, gint n_taps)                                        \
263
5.68k
{                                                                       \
264
5.68k
  gint i;                                                               \
265
5.68k
  type *t = taps;                                                       \
266
3.44M
  for (i = 0; i < n_taps; i++)                                          \
267
3.44M
    t[i] = tmp_taps[i] / weight;                                        \
268
5.68k
}
269
270
MAKE_CONVERT_TAPS_INT_FUNC (gint16, PRECISION_S16);
271
MAKE_CONVERT_TAPS_INT_FUNC (gint32, PRECISION_S32);
272
MAKE_CONVERT_TAPS_FLOAT_FUNC (gfloat);
273
MAKE_CONVERT_TAPS_FLOAT_FUNC (gdouble);
274
275
static ConvertTapsFunc convert_taps_funcs[] = {
276
  convert_taps_gint16_c,
277
  convert_taps_gint32_c,
278
  convert_taps_gfloat_c,
279
  convert_taps_gdouble_c
280
};
281
282
#define convert_taps_gint16   convert_taps_funcs[0]
283
#define convert_taps_gint32   convert_taps_funcs[1]
284
#define convert_taps_gfloat   convert_taps_funcs[2]
285
#define convert_taps_gdouble  convert_taps_funcs[3]
286
287
static void
288
make_taps (GstAudioResampler * resampler, gdouble * res, gdouble x, gint n_taps)
289
797k
{
290
797k
  gdouble weight = 0.0, *tmp_taps = resampler->tmp_taps;
291
797k
  gint i;
292
293
797k
  switch (resampler->method) {
294
0
    case GST_AUDIO_RESAMPLER_METHOD_NEAREST:
295
0
      break;
296
297
388k
    case GST_AUDIO_RESAMPLER_METHOD_LINEAR:
298
1.18M
      for (i = 0; i < n_taps; i++)
299
792k
        weight += tmp_taps[i] = get_linear_tap (x + i, resampler->n_taps);
300
388k
      break;
301
302
388k
    case GST_AUDIO_RESAMPLER_METHOD_CUBIC:
303
2.00M
      for (i = 0; i < n_taps; i++)
304
1.62M
        weight += tmp_taps[i] = get_cubic_tap (x + i, resampler->n_taps,
305
1.62M
            resampler->b, resampler->c);
306
388k
      break;
307
308
18.0k
    case GST_AUDIO_RESAMPLER_METHOD_BLACKMAN_NUTTALL:
309
14.4M
      for (i = 0; i < n_taps; i++)
310
14.4M
        weight += tmp_taps[i] =
311
14.4M
            get_blackman_nuttall_tap (x + i,
312
14.4M
            resampler->n_taps, resampler->cutoff);
313
18.0k
      break;
314
315
2.48k
    case GST_AUDIO_RESAMPLER_METHOD_KAISER:
316
3.83M
      for (i = 0; i < n_taps; i++)
317
3.83M
        weight += tmp_taps[i] =
318
3.83M
            get_kaiser_tap (x + i, resampler->n_taps,
319
3.83M
            resampler->cutoff, resampler->kaiser_beta);
320
2.48k
      break;
321
797k
  }
322
797k
  resampler->convert_taps (tmp_taps, res, weight, n_taps);
323
797k
}
324
325
#define MAKE_COEFF_LINEAR_INT_FUNC(type,type2,prec)                     \
326
static inline void                                                      \
327
0
make_coeff_##type##_linear (gint num, gint denom, type *icoeff)         \
328
0
{                                                                       \
329
0
  type x = ((gint64)num << prec) / denom;                               \
330
0
  icoeff[0] = icoeff[2] = x;                                            \
331
0
  icoeff[1] = icoeff[3] = (type)(((type2)1 << prec)-1)  - x;            \
332
0
}
Unexecuted instantiation: audio-resampler.c:make_coeff_gint16_linear
Unexecuted instantiation: audio-resampler.c:make_coeff_gint32_linear
333
#define MAKE_COEFF_LINEAR_FLOAT_FUNC(type)                              \
334
static inline void                                                      \
335
0
make_coeff_##type##_linear (gint num, gint denom, type *icoeff)         \
336
0
{                                                                       \
337
0
  type x = (type)num / denom;                                           \
338
0
  icoeff[0] = icoeff[2] = x;                                            \
339
0
  icoeff[1] = icoeff[3] = (type)1.0 - x;                                \
340
0
}
Unexecuted instantiation: audio-resampler.c:make_coeff_gfloat_linear
Unexecuted instantiation: audio-resampler.c:make_coeff_gdouble_linear
341
MAKE_COEFF_LINEAR_INT_FUNC (gint16, gint32, PRECISION_S16);
342
MAKE_COEFF_LINEAR_INT_FUNC (gint32, gint64, PRECISION_S32);
343
MAKE_COEFF_LINEAR_FLOAT_FUNC (gfloat);
344
MAKE_COEFF_LINEAR_FLOAT_FUNC (gdouble);
345
346
#define MAKE_COEFF_CUBIC_INT_FUNC(type,type2,prec)                      \
347
static inline void                                                      \
348
245M
make_coeff_##type##_cubic (gint num, gint denom, type *icoeff)          \
349
245M
{                                                                       \
350
245M
  type2 one = ((type2)1 << prec) - 1;                                   \
351
245M
  type2 x = ((gint64) num << prec) / denom;                             \
352
245M
  type2 x2 = (x * x) >> prec;                                           \
353
245M
  type2 x3 = (x2 * x) >> prec;                                          \
354
245M
  icoeff[0] = (((x3 - x) << prec) / 6) >> prec;                         \
355
245M
  icoeff[1] = x + ((x2 - x3) >> 1);                                     \
356
245M
  icoeff[3] = -(((x << prec) / 3) >> prec) +                            \
357
245M
            (x2 >> 1) - (((x3 << prec) / 6) >> prec);                   \
358
245M
  icoeff[2] = one - icoeff[0] - icoeff[1] - icoeff[3];                  \
359
245M
}
audio-resampler.c:make_coeff_gint16_cubic
Line
Count
Source
348
4.69M
make_coeff_##type##_cubic (gint num, gint denom, type *icoeff)          \
349
4.69M
{                                                                       \
350
4.69M
  type2 one = ((type2)1 << prec) - 1;                                   \
351
4.69M
  type2 x = ((gint64) num << prec) / denom;                             \
352
4.69M
  type2 x2 = (x * x) >> prec;                                           \
353
4.69M
  type2 x3 = (x2 * x) >> prec;                                          \
354
4.69M
  icoeff[0] = (((x3 - x) << prec) / 6) >> prec;                         \
355
4.69M
  icoeff[1] = x + ((x2 - x3) >> 1);                                     \
356
4.69M
  icoeff[3] = -(((x << prec) / 3) >> prec) +                            \
357
4.69M
            (x2 >> 1) - (((x3 << prec) / 6) >> prec);                   \
358
4.69M
  icoeff[2] = one - icoeff[0] - icoeff[1] - icoeff[3];                  \
359
4.69M
}
audio-resampler.c:make_coeff_gint32_cubic
Line
Count
Source
348
240M
make_coeff_##type##_cubic (gint num, gint denom, type *icoeff)          \
349
240M
{                                                                       \
350
240M
  type2 one = ((type2)1 << prec) - 1;                                   \
351
240M
  type2 x = ((gint64) num << prec) / denom;                             \
352
240M
  type2 x2 = (x * x) >> prec;                                           \
353
240M
  type2 x3 = (x2 * x) >> prec;                                          \
354
240M
  icoeff[0] = (((x3 - x) << prec) / 6) >> prec;                         \
355
240M
  icoeff[1] = x + ((x2 - x3) >> 1);                                     \
356
240M
  icoeff[3] = -(((x << prec) / 3) >> prec) +                            \
357
240M
            (x2 >> 1) - (((x3 << prec) / 6) >> prec);                   \
358
240M
  icoeff[2] = one - icoeff[0] - icoeff[1] - icoeff[3];                  \
359
240M
}
360
#define MAKE_COEFF_CUBIC_FLOAT_FUNC(type)                               \
361
static inline void                                                      \
362
187M
make_coeff_##type##_cubic (gint num, gint denom, type *icoeff)          \
363
187M
{                                                                       \
364
187M
  type x = (type) num / denom, x2 = x * x, x3 = x2 * x;                 \
365
187M
  icoeff[0] = 0.16667f * (x3 - x);                                      \
366
187M
  icoeff[1] = x + 0.5f * (x2 - x3);                                     \
367
187M
  icoeff[3] = -0.33333f * x + 0.5f * x2 - 0.16667f * x3;                \
368
187M
  icoeff[2] = (type)1.0 - icoeff[0] - icoeff[1] - icoeff[3];            \
369
187M
}
audio-resampler.c:make_coeff_gfloat_cubic
Line
Count
Source
362
2.15M
make_coeff_##type##_cubic (gint num, gint denom, type *icoeff)          \
363
2.15M
{                                                                       \
364
2.15M
  type x = (type) num / denom, x2 = x * x, x3 = x2 * x;                 \
365
2.15M
  icoeff[0] = 0.16667f * (x3 - x);                                      \
366
2.15M
  icoeff[1] = x + 0.5f * (x2 - x3);                                     \
367
2.15M
  icoeff[3] = -0.33333f * x + 0.5f * x2 - 0.16667f * x3;                \
368
2.15M
  icoeff[2] = (type)1.0 - icoeff[0] - icoeff[1] - icoeff[3];            \
369
2.15M
}
audio-resampler.c:make_coeff_gdouble_cubic
Line
Count
Source
362
185M
make_coeff_##type##_cubic (gint num, gint denom, type *icoeff)          \
363
185M
{                                                                       \
364
185M
  type x = (type) num / denom, x2 = x * x, x3 = x2 * x;                 \
365
185M
  icoeff[0] = 0.16667f * (x3 - x);                                      \
366
185M
  icoeff[1] = x + 0.5f * (x2 - x3);                                     \
367
185M
  icoeff[3] = -0.33333f * x + 0.5f * x2 - 0.16667f * x3;                \
368
185M
  icoeff[2] = (type)1.0 - icoeff[0] - icoeff[1] - icoeff[3];            \
369
185M
}
370
MAKE_COEFF_CUBIC_INT_FUNC (gint16, gint32, PRECISION_S16);
371
MAKE_COEFF_CUBIC_INT_FUNC (gint32, gint64, PRECISION_S32);
372
MAKE_COEFF_CUBIC_FLOAT_FUNC (gfloat);
373
MAKE_COEFF_CUBIC_FLOAT_FUNC (gdouble);
374
375
#define INTERPOLATE_INT_LINEAR_FUNC(type,type2,prec,limit)      \
376
static inline void                                              \
377
interpolate_##type##_linear_c (gpointer op, const gpointer ap,  \
378
0
    gint len, const gpointer icp, gint astride)                 \
379
0
{                                                               \
380
0
  gint i;                                                       \
381
0
  type *o = op, *a = ap, *ic = icp;                             \
382
0
  type2 tmp, c0 = ic[0];                                        \
383
0
  const type *c[2] = {(type*)((gint8*)a + 0*astride),           \
384
0
                      (type*)((gint8*)a + 1*astride)};          \
385
0
                                                                \
386
0
  for (i = 0; i < len; i++) {                                   \
387
0
    tmp = ((type2)c[0][i] - (type2)c[1][i]) * c0 +              \
388
0
         (((type2)c[1][i]) << (prec));                          \
389
0
    o[i] = (tmp + ((type2)1 << ((prec) - 1))) >> (prec);        \
390
0
  }                                                             \
391
0
}
Unexecuted instantiation: audio-resampler.c:interpolate_gint16_linear_c
Unexecuted instantiation: audio-resampler.c:interpolate_gint32_linear_c
392
#define INTERPOLATE_FLOAT_LINEAR_FUNC(type)                     \
393
static inline void                                              \
394
interpolate_##type##_linear_c (gpointer op, const gpointer ap,  \
395
0
    gint len, const gpointer icp, gint astride)                 \
396
0
{                                                               \
397
0
  gint i;                                                       \
398
0
  type *o = op, *a = ap, *ic = icp;                             \
399
0
  type c0 = ic[0];                                              \
400
0
  const type *c[2] = {(type*)((gint8*)a + 0*astride),           \
401
0
                      (type*)((gint8*)a + 1*astride)};          \
402
0
                                                                \
403
0
  for (i = 0; i < len; i++) {                                   \
404
0
    o[i] = (c[0][i] - c[1][i]) * c0 + c[1][i];                  \
405
0
  }                                                             \
406
0
}
Unexecuted instantiation: audio-resampler.c:interpolate_gfloat_linear_c
Unexecuted instantiation: audio-resampler.c:interpolate_gdouble_linear_c
407
408
INTERPOLATE_INT_LINEAR_FUNC (gint16, gint32, PRECISION_S16, (gint32) 1 << 15);
409
INTERPOLATE_INT_LINEAR_FUNC (gint32, gint64, PRECISION_S32, (gint64) 1 << 31);
410
INTERPOLATE_FLOAT_LINEAR_FUNC (gfloat);
411
INTERPOLATE_FLOAT_LINEAR_FUNC (gdouble);
412
413
#define INTERPOLATE_INT_CUBIC_FUNC(type,type2,prec,limit)       \
414
static inline void                                              \
415
interpolate_##type##_cubic_c (gpointer op, const gpointer ap,   \
416
80.1k
    gint len, const gpointer icp, gint astride)                 \
417
80.1k
{                                                               \
418
80.1k
  gint i;                                                       \
419
80.1k
  type *o = op, *a = ap, *ic = icp;                             \
420
80.1k
  type2 tmp, c0 = ic[0], c1 = ic[1], c2 = ic[2], c3 = ic[3];    \
421
80.1k
  const type *c[4] = {(type*)((gint8*)a + 0*astride),           \
422
80.1k
                      (type*)((gint8*)a + 1*astride),           \
423
80.1k
                      (type*)((gint8*)a + 2*astride),           \
424
80.1k
                      (type*)((gint8*)a + 3*astride)};          \
425
80.1k
                                                                \
426
4.75M
  for (i = 0; i < len; i++) {                                   \
427
4.67M
    tmp = (type2)c[0][i] * c0 + (type2)c[1][i] * c1 +           \
428
4.67M
          (type2)c[2][i] * c2 + (type2)c[3][i] * c3;            \
429
4.67M
    tmp = (tmp + ((type2)1 << ((prec) - 1))) >> (prec);         \
430
4.67M
    o[i] = CLAMP (tmp, -(limit), (limit) - 1);                  \
431
4.67M
  }                                                             \
432
80.1k
}
Unexecuted instantiation: audio-resampler.c:interpolate_gint16_cubic_c
audio-resampler.c:interpolate_gint32_cubic_c
Line
Count
Source
416
80.1k
    gint len, const gpointer icp, gint astride)                 \
417
80.1k
{                                                               \
418
80.1k
  gint i;                                                       \
419
80.1k
  type *o = op, *a = ap, *ic = icp;                             \
420
80.1k
  type2 tmp, c0 = ic[0], c1 = ic[1], c2 = ic[2], c3 = ic[3];    \
421
80.1k
  const type *c[4] = {(type*)((gint8*)a + 0*astride),           \
422
80.1k
                      (type*)((gint8*)a + 1*astride),           \
423
80.1k
                      (type*)((gint8*)a + 2*astride),           \
424
80.1k
                      (type*)((gint8*)a + 3*astride)};          \
425
80.1k
                                                                \
426
4.75M
  for (i = 0; i < len; i++) {                                   \
427
4.67M
    tmp = (type2)c[0][i] * c0 + (type2)c[1][i] * c1 +           \
428
4.67M
          (type2)c[2][i] * c2 + (type2)c[3][i] * c3;            \
429
4.67M
    tmp = (tmp + ((type2)1 << ((prec) - 1))) >> (prec);         \
430
4.67M
    o[i] = CLAMP (tmp, -(limit), (limit) - 1);                  \
431
4.67M
  }                                                             \
432
80.1k
}
433
#define INTERPOLATE_FLOAT_CUBIC_FUNC(type)                      \
434
static inline void                                              \
435
interpolate_##type##_cubic_c (gpointer op, const gpointer ap,   \
436
0
    gint len, const gpointer icp, gint astride)                 \
437
0
{                                                               \
438
0
  gint i;                                                       \
439
0
  type *o = op, *a = ap, *ic = icp;                             \
440
0
  type c0 = ic[0], c1 = ic[1], c2 = ic[2], c3 = ic[3];          \
441
0
  const type *c[4] = {(type*)((gint8*)a + 0*astride),           \
442
0
                      (type*)((gint8*)a + 1*astride),           \
443
0
                      (type*)((gint8*)a + 2*astride),           \
444
0
                      (type*)((gint8*)a + 3*astride)};          \
445
0
                                                                \
446
0
  for (i = 0; i < len; i++) {                                   \
447
0
    o[i] = c[0][i] * c0 + c[1][i] * c1 +                        \
448
0
           c[2][i] * c2 + c[3][i] * c3;                         \
449
0
  }                                                             \
450
0
}
Unexecuted instantiation: audio-resampler.c:interpolate_gfloat_cubic_c
Unexecuted instantiation: audio-resampler.c:interpolate_gdouble_cubic_c
451
452
INTERPOLATE_INT_CUBIC_FUNC (gint16, gint32, PRECISION_S16, (gint32) 1 << 15);
453
INTERPOLATE_INT_CUBIC_FUNC (gint32, gint64, PRECISION_S32, (gint64) 1 << 31);
454
INTERPOLATE_FLOAT_CUBIC_FUNC (gfloat);
455
INTERPOLATE_FLOAT_CUBIC_FUNC (gdouble);
456
457
static InterpolateFunc interpolate_funcs[] = {
458
  interpolate_gint16_linear_c,
459
  interpolate_gint32_linear_c,
460
  interpolate_gfloat_linear_c,
461
  interpolate_gdouble_linear_c,
462
463
  interpolate_gint16_cubic_c,
464
  interpolate_gint32_cubic_c,
465
  interpolate_gfloat_cubic_c,
466
  interpolate_gdouble_cubic_c,
467
};
468
469
1
#define interpolate_gint16_linear  interpolate_funcs[0]
470
#define interpolate_gint32_linear  interpolate_funcs[1]
471
1
#define interpolate_gfloat_linear  interpolate_funcs[2]
472
1
#define interpolate_gdouble_linear interpolate_funcs[3]
473
474
1
#define interpolate_gint16_cubic   interpolate_funcs[4]
475
#define interpolate_gint32_cubic   interpolate_funcs[5]
476
1
#define interpolate_gfloat_cubic   interpolate_funcs[6]
477
1
#define interpolate_gdouble_cubic  interpolate_funcs[7]
478
479
#define GET_TAPS_NEAREST_FUNC(type)                                             \
480
static inline gpointer                                                          \
481
get_taps_##type##_nearest (GstAudioResampler * resampler,                       \
482
55.1M
    gint *samp_index, gint *samp_phase, type icoeff[4])                         \
483
55.1M
{                                                                               \
484
55.1M
  gint out_rate = resampler->out_rate;                                          \
485
55.1M
  *samp_index += resampler->samp_inc;                                           \
486
55.1M
  *samp_phase += resampler->samp_frac;                                          \
487
55.1M
  if (*samp_phase >= out_rate) {                                                \
488
475k
    *samp_phase -= out_rate;                                                    \
489
475k
    *samp_index += 1;                                                           \
490
475k
  }                                                                             \
491
55.1M
  return NULL;                                                                  \
492
55.1M
}
493
27.3M
GET_TAPS_NEAREST_FUNC (gint16);
494
5.74M
GET_TAPS_NEAREST_FUNC (gint32);
495
15.6M
GET_TAPS_NEAREST_FUNC (gfloat);
496
6.35M
GET_TAPS_NEAREST_FUNC (gdouble);
497
498
27.3M
#define get_taps_gint16_nearest get_taps_gint16_nearest
499
5.74M
#define get_taps_gint32_nearest get_taps_gint32_nearest
500
15.6M
#define get_taps_gfloat_nearest get_taps_gfloat_nearest
501
6.35M
#define get_taps_gdouble_nearest get_taps_gdouble_nearest
502
503
#define GET_TAPS_FULL_FUNC(type)                                                \
504
516M
DECL_GET_TAPS_FULL_FUNC(type)                                                   \
505
516M
{                                                                               \
506
516M
  gpointer res;                                                                 \
507
516M
  gint out_rate = resampler->out_rate;                                          \
508
516M
  gint n_phases = resampler->n_phases;                                          \
509
516M
  gint phase = (n_phases == out_rate ? *samp_phase :                            \
510
516M
      ((gint64)*samp_phase * n_phases) / out_rate);                             \
511
516M
                                                                                \
512
516M
  res = resampler->cached_phases[phase];                                        \
513
516M
  if (G_UNLIKELY (res == NULL)) {                                               \
514
925k
    res = (gint8 *) resampler->cached_taps +                                    \
515
925k
                        phase * resampler->cached_taps_stride;                  \
516
925k
    switch (resampler->filter_interpolation) {                                  \
517
776k
      case GST_AUDIO_RESAMPLER_FILTER_INTERPOLATION_NONE:                       \
518
776k
      {                                                                         \
519
776k
        gdouble x;                                                              \
520
776k
        gint n_taps = resampler->n_taps;                                        \
521
776k
                                                                                \
522
776k
        x = 1.0 - n_taps / 2 - (gdouble) phase / n_phases;                      \
523
776k
        make_taps (resampler, res, x, n_taps);                                  \
524
776k
        break;                                                                  \
525
0
      }                                                                         \
526
148k
      default:                                                                  \
527
148k
      {                                                                         \
528
148k
        gint offset, pos, frac;                                                 \
529
148k
        gint oversample = resampler->oversample;                                \
530
148k
        gint taps_stride = resampler->taps_stride;                              \
531
148k
        gint n_taps = resampler->n_taps;                                        \
532
148k
        type ic[4], *taps;                                                      \
533
148k
                                                                                \
534
148k
        pos = phase * oversample;                                               \
535
148k
        offset = (oversample - 1) - pos / n_phases;                             \
536
148k
        frac = pos % n_phases;                                                  \
537
148k
                                                                                \
538
148k
        taps = (type *) ((gint8 *) resampler->taps + offset * taps_stride);     \
539
148k
                                                                                \
540
148k
        switch (resampler->filter_interpolation) {                              \
541
0
          default:                                                              \
542
0
          case GST_AUDIO_RESAMPLER_FILTER_INTERPOLATION_LINEAR:                 \
543
0
            make_coeff_##type##_linear (frac, n_phases, ic);                    \
544
0
            break;                                                              \
545
148k
          case GST_AUDIO_RESAMPLER_FILTER_INTERPOLATION_CUBIC:                  \
546
148k
            make_coeff_##type##_cubic (frac, n_phases, ic);                     \
547
148k
            break;                                                              \
548
148k
        }                                                                       \
549
148k
        resampler->interpolate (res, taps, n_taps, ic, taps_stride);            \
550
148k
      }                                                                         \
551
925k
    }                                                                           \
552
925k
    resampler->cached_phases[phase] = res;                                      \
553
925k
  }                                                                             \
554
516M
  *samp_index += resampler->samp_inc;                                           \
555
516M
  *samp_phase += resampler->samp_frac;                                          \
556
516M
  if (*samp_phase >= out_rate) {                                                \
557
7.43M
    *samp_phase -= out_rate;                                                    \
558
7.43M
    *samp_index += 1;                                                           \
559
7.43M
  }                                                                             \
560
516M
  return res;                                                                   \
561
516M
}
get_taps_gint16_full
Line
Count
Source
504
16.6M
DECL_GET_TAPS_FULL_FUNC(type)                                                   \
505
16.6M
{                                                                               \
506
16.6M
  gpointer res;                                                                 \
507
16.6M
  gint out_rate = resampler->out_rate;                                          \
508
16.6M
  gint n_phases = resampler->n_phases;                                          \
509
16.6M
  gint phase = (n_phases == out_rate ? *samp_phase :                            \
510
16.6M
      ((gint64)*samp_phase * n_phases) / out_rate);                             \
511
16.6M
                                                                                \
512
16.6M
  res = resampler->cached_phases[phase];                                        \
513
16.6M
  if (G_UNLIKELY (res == NULL)) {                                               \
514
355k
    res = (gint8 *) resampler->cached_taps +                                    \
515
355k
                        phase * resampler->cached_taps_stride;                  \
516
355k
    switch (resampler->filter_interpolation) {                                  \
517
329k
      case GST_AUDIO_RESAMPLER_FILTER_INTERPOLATION_NONE:                       \
518
329k
      {                                                                         \
519
329k
        gdouble x;                                                              \
520
329k
        gint n_taps = resampler->n_taps;                                        \
521
329k
                                                                                \
522
329k
        x = 1.0 - n_taps / 2 - (gdouble) phase / n_phases;                      \
523
329k
        make_taps (resampler, res, x, n_taps);                                  \
524
329k
        break;                                                                  \
525
0
      }                                                                         \
526
26.1k
      default:                                                                  \
527
26.1k
      {                                                                         \
528
26.1k
        gint offset, pos, frac;                                                 \
529
26.1k
        gint oversample = resampler->oversample;                                \
530
26.1k
        gint taps_stride = resampler->taps_stride;                              \
531
26.1k
        gint n_taps = resampler->n_taps;                                        \
532
26.1k
        type ic[4], *taps;                                                      \
533
26.1k
                                                                                \
534
26.1k
        pos = phase * oversample;                                               \
535
26.1k
        offset = (oversample - 1) - pos / n_phases;                             \
536
26.1k
        frac = pos % n_phases;                                                  \
537
26.1k
                                                                                \
538
26.1k
        taps = (type *) ((gint8 *) resampler->taps + offset * taps_stride);     \
539
26.1k
                                                                                \
540
26.1k
        switch (resampler->filter_interpolation) {                              \
541
0
          default:                                                              \
542
0
          case GST_AUDIO_RESAMPLER_FILTER_INTERPOLATION_LINEAR:                 \
543
0
            make_coeff_##type##_linear (frac, n_phases, ic);                    \
544
0
            break;                                                              \
545
26.1k
          case GST_AUDIO_RESAMPLER_FILTER_INTERPOLATION_CUBIC:                  \
546
26.1k
            make_coeff_##type##_cubic (frac, n_phases, ic);                     \
547
26.1k
            break;                                                              \
548
26.1k
        }                                                                       \
549
26.1k
        resampler->interpolate (res, taps, n_taps, ic, taps_stride);            \
550
26.1k
      }                                                                         \
551
355k
    }                                                                           \
552
355k
    resampler->cached_phases[phase] = res;                                      \
553
355k
  }                                                                             \
554
16.6M
  *samp_index += resampler->samp_inc;                                           \
555
16.6M
  *samp_phase += resampler->samp_frac;                                          \
556
16.6M
  if (*samp_phase >= out_rate) {                                                \
557
486k
    *samp_phase -= out_rate;                                                    \
558
486k
    *samp_index += 1;                                                           \
559
486k
  }                                                                             \
560
16.6M
  return res;                                                                   \
561
16.6M
}
get_taps_gint32_full
Line
Count
Source
504
243M
DECL_GET_TAPS_FULL_FUNC(type)                                                   \
505
243M
{                                                                               \
506
243M
  gpointer res;                                                                 \
507
243M
  gint out_rate = resampler->out_rate;                                          \
508
243M
  gint n_phases = resampler->n_phases;                                          \
509
243M
  gint phase = (n_phases == out_rate ? *samp_phase :                            \
510
243M
      ((gint64)*samp_phase * n_phases) / out_rate);                             \
511
243M
                                                                                \
512
243M
  res = resampler->cached_phases[phase];                                        \
513
243M
  if (G_UNLIKELY (res == NULL)) {                                               \
514
238k
    res = (gint8 *) resampler->cached_taps +                                    \
515
238k
                        phase * resampler->cached_taps_stride;                  \
516
238k
    switch (resampler->filter_interpolation) {                                  \
517
158k
      case GST_AUDIO_RESAMPLER_FILTER_INTERPOLATION_NONE:                       \
518
158k
      {                                                                         \
519
158k
        gdouble x;                                                              \
520
158k
        gint n_taps = resampler->n_taps;                                        \
521
158k
                                                                                \
522
158k
        x = 1.0 - n_taps / 2 - (gdouble) phase / n_phases;                      \
523
158k
        make_taps (resampler, res, x, n_taps);                                  \
524
158k
        break;                                                                  \
525
0
      }                                                                         \
526
80.1k
      default:                                                                  \
527
80.1k
      {                                                                         \
528
80.1k
        gint offset, pos, frac;                                                 \
529
80.1k
        gint oversample = resampler->oversample;                                \
530
80.1k
        gint taps_stride = resampler->taps_stride;                              \
531
80.1k
        gint n_taps = resampler->n_taps;                                        \
532
80.1k
        type ic[4], *taps;                                                      \
533
80.1k
                                                                                \
534
80.1k
        pos = phase * oversample;                                               \
535
80.1k
        offset = (oversample - 1) - pos / n_phases;                             \
536
80.1k
        frac = pos % n_phases;                                                  \
537
80.1k
                                                                                \
538
80.1k
        taps = (type *) ((gint8 *) resampler->taps + offset * taps_stride);     \
539
80.1k
                                                                                \
540
80.1k
        switch (resampler->filter_interpolation) {                              \
541
0
          default:                                                              \
542
0
          case GST_AUDIO_RESAMPLER_FILTER_INTERPOLATION_LINEAR:                 \
543
0
            make_coeff_##type##_linear (frac, n_phases, ic);                    \
544
0
            break;                                                              \
545
80.1k
          case GST_AUDIO_RESAMPLER_FILTER_INTERPOLATION_CUBIC:                  \
546
80.1k
            make_coeff_##type##_cubic (frac, n_phases, ic);                     \
547
80.1k
            break;                                                              \
548
80.1k
        }                                                                       \
549
80.1k
        resampler->interpolate (res, taps, n_taps, ic, taps_stride);            \
550
80.1k
      }                                                                         \
551
238k
    }                                                                           \
552
238k
    resampler->cached_phases[phase] = res;                                      \
553
238k
  }                                                                             \
554
243M
  *samp_index += resampler->samp_inc;                                           \
555
243M
  *samp_phase += resampler->samp_frac;                                          \
556
243M
  if (*samp_phase >= out_rate) {                                                \
557
3.48M
    *samp_phase -= out_rate;                                                    \
558
3.48M
    *samp_index += 1;                                                           \
559
3.48M
  }                                                                             \
560
243M
  return res;                                                                   \
561
243M
}
get_taps_gfloat_full
Line
Count
Source
504
187M
DECL_GET_TAPS_FULL_FUNC(type)                                                   \
505
187M
{                                                                               \
506
187M
  gpointer res;                                                                 \
507
187M
  gint out_rate = resampler->out_rate;                                          \
508
187M
  gint n_phases = resampler->n_phases;                                          \
509
187M
  gint phase = (n_phases == out_rate ? *samp_phase :                            \
510
187M
      ((gint64)*samp_phase * n_phases) / out_rate);                             \
511
187M
                                                                                \
512
187M
  res = resampler->cached_phases[phase];                                        \
513
187M
  if (G_UNLIKELY (res == NULL)) {                                               \
514
297k
    res = (gint8 *) resampler->cached_taps +                                    \
515
297k
                        phase * resampler->cached_taps_stride;                  \
516
297k
    switch (resampler->filter_interpolation) {                                  \
517
288k
      case GST_AUDIO_RESAMPLER_FILTER_INTERPOLATION_NONE:                       \
518
288k
      {                                                                         \
519
288k
        gdouble x;                                                              \
520
288k
        gint n_taps = resampler->n_taps;                                        \
521
288k
                                                                                \
522
288k
        x = 1.0 - n_taps / 2 - (gdouble) phase / n_phases;                      \
523
288k
        make_taps (resampler, res, x, n_taps);                                  \
524
288k
        break;                                                                  \
525
0
      }                                                                         \
526
9.04k
      default:                                                                  \
527
9.04k
      {                                                                         \
528
9.04k
        gint offset, pos, frac;                                                 \
529
9.04k
        gint oversample = resampler->oversample;                                \
530
9.04k
        gint taps_stride = resampler->taps_stride;                              \
531
9.04k
        gint n_taps = resampler->n_taps;                                        \
532
9.04k
        type ic[4], *taps;                                                      \
533
9.04k
                                                                                \
534
9.04k
        pos = phase * oversample;                                               \
535
9.04k
        offset = (oversample - 1) - pos / n_phases;                             \
536
9.04k
        frac = pos % n_phases;                                                  \
537
9.04k
                                                                                \
538
9.04k
        taps = (type *) ((gint8 *) resampler->taps + offset * taps_stride);     \
539
9.04k
                                                                                \
540
9.04k
        switch (resampler->filter_interpolation) {                              \
541
0
          default:                                                              \
542
0
          case GST_AUDIO_RESAMPLER_FILTER_INTERPOLATION_LINEAR:                 \
543
0
            make_coeff_##type##_linear (frac, n_phases, ic);                    \
544
0
            break;                                                              \
545
9.04k
          case GST_AUDIO_RESAMPLER_FILTER_INTERPOLATION_CUBIC:                  \
546
9.04k
            make_coeff_##type##_cubic (frac, n_phases, ic);                     \
547
9.04k
            break;                                                              \
548
9.04k
        }                                                                       \
549
9.04k
        resampler->interpolate (res, taps, n_taps, ic, taps_stride);            \
550
9.04k
      }                                                                         \
551
297k
    }                                                                           \
552
297k
    resampler->cached_phases[phase] = res;                                      \
553
297k
  }                                                                             \
554
187M
  *samp_index += resampler->samp_inc;                                           \
555
187M
  *samp_phase += resampler->samp_frac;                                          \
556
187M
  if (*samp_phase >= out_rate) {                                                \
557
1.11M
    *samp_phase -= out_rate;                                                    \
558
1.11M
    *samp_index += 1;                                                           \
559
1.11M
  }                                                                             \
560
187M
  return res;                                                                   \
561
187M
}
get_taps_gdouble_full
Line
Count
Source
504
68.8M
DECL_GET_TAPS_FULL_FUNC(type)                                                   \
505
68.8M
{                                                                               \
506
68.8M
  gpointer res;                                                                 \
507
68.8M
  gint out_rate = resampler->out_rate;                                          \
508
68.8M
  gint n_phases = resampler->n_phases;                                          \
509
68.8M
  gint phase = (n_phases == out_rate ? *samp_phase :                            \
510
68.8M
      ((gint64)*samp_phase * n_phases) / out_rate);                             \
511
68.8M
                                                                                \
512
68.8M
  res = resampler->cached_phases[phase];                                        \
513
68.8M
  if (G_UNLIKELY (res == NULL)) {                                               \
514
34.4k
    res = (gint8 *) resampler->cached_taps +                                    \
515
34.4k
                        phase * resampler->cached_taps_stride;                  \
516
34.4k
    switch (resampler->filter_interpolation) {                                  \
517
1.02k
      case GST_AUDIO_RESAMPLER_FILTER_INTERPOLATION_NONE:                       \
518
1.02k
      {                                                                         \
519
1.02k
        gdouble x;                                                              \
520
1.02k
        gint n_taps = resampler->n_taps;                                        \
521
1.02k
                                                                                \
522
1.02k
        x = 1.0 - n_taps / 2 - (gdouble) phase / n_phases;                      \
523
1.02k
        make_taps (resampler, res, x, n_taps);                                  \
524
1.02k
        break;                                                                  \
525
0
      }                                                                         \
526
33.3k
      default:                                                                  \
527
33.3k
      {                                                                         \
528
33.3k
        gint offset, pos, frac;                                                 \
529
33.3k
        gint oversample = resampler->oversample;                                \
530
33.3k
        gint taps_stride = resampler->taps_stride;                              \
531
33.3k
        gint n_taps = resampler->n_taps;                                        \
532
33.3k
        type ic[4], *taps;                                                      \
533
33.3k
                                                                                \
534
33.3k
        pos = phase * oversample;                                               \
535
33.3k
        offset = (oversample - 1) - pos / n_phases;                             \
536
33.3k
        frac = pos % n_phases;                                                  \
537
33.3k
                                                                                \
538
33.3k
        taps = (type *) ((gint8 *) resampler->taps + offset * taps_stride);     \
539
33.3k
                                                                                \
540
33.3k
        switch (resampler->filter_interpolation) {                              \
541
0
          default:                                                              \
542
0
          case GST_AUDIO_RESAMPLER_FILTER_INTERPOLATION_LINEAR:                 \
543
0
            make_coeff_##type##_linear (frac, n_phases, ic);                    \
544
0
            break;                                                              \
545
33.3k
          case GST_AUDIO_RESAMPLER_FILTER_INTERPOLATION_CUBIC:                  \
546
33.3k
            make_coeff_##type##_cubic (frac, n_phases, ic);                     \
547
33.3k
            break;                                                              \
548
33.3k
        }                                                                       \
549
33.3k
        resampler->interpolate (res, taps, n_taps, ic, taps_stride);            \
550
33.3k
      }                                                                         \
551
34.4k
    }                                                                           \
552
34.4k
    resampler->cached_phases[phase] = res;                                      \
553
34.4k
  }                                                                             \
554
68.8M
  *samp_index += resampler->samp_inc;                                           \
555
68.8M
  *samp_phase += resampler->samp_frac;                                          \
556
68.8M
  if (*samp_phase >= out_rate) {                                                \
557
2.34M
    *samp_phase -= out_rate;                                                    \
558
2.34M
    *samp_index += 1;                                                           \
559
2.34M
  }                                                                             \
560
68.8M
  return res;                                                                   \
561
68.8M
}
562
GET_TAPS_FULL_FUNC (gint16);
563
GET_TAPS_FULL_FUNC (gint32);
564
GET_TAPS_FULL_FUNC (gfloat);
565
GET_TAPS_FULL_FUNC (gdouble);
566
567
#define GET_TAPS_INTERPOLATE_FUNC(type,inter)                   \
568
432M
DECL_GET_TAPS_INTERPOLATE_FUNC (type, inter)                    \
569
432M
{                                                               \
570
432M
  gpointer res;                                                 \
571
432M
  gint out_rate = resampler->out_rate;                          \
572
432M
  gint offset, frac, pos;                                       \
573
432M
  gint oversample = resampler->oversample;                      \
574
432M
  gint taps_stride = resampler->taps_stride;                    \
575
432M
                                                                \
576
432M
  pos = *samp_phase * oversample;                               \
577
432M
  offset = (oversample - 1) - pos / out_rate;                   \
578
432M
  frac = pos % out_rate;                                        \
579
432M
                                                                \
580
432M
  res = (gint8 *) resampler->taps + offset * taps_stride;       \
581
432M
  make_coeff_##type##_##inter (frac, out_rate, icoeff);         \
582
432M
                                                                \
583
432M
  *samp_index += resampler->samp_inc;                           \
584
432M
  *samp_phase += resampler->samp_frac;                          \
585
432M
  if (*samp_phase >= out_rate) {                                \
586
11.6M
    *samp_phase -= out_rate;                                    \
587
11.6M
    *samp_index += 1;                                           \
588
11.6M
  }                                                             \
589
432M
  return res;                                                   \
590
432M
}
Unexecuted instantiation: get_taps_gint16_linear
Unexecuted instantiation: get_taps_gint32_linear
Unexecuted instantiation: get_taps_gfloat_linear
Unexecuted instantiation: get_taps_gdouble_linear
get_taps_gint16_cubic
Line
Count
Source
568
4.67M
DECL_GET_TAPS_INTERPOLATE_FUNC (type, inter)                    \
569
4.67M
{                                                               \
570
4.67M
  gpointer res;                                                 \
571
4.67M
  gint out_rate = resampler->out_rate;                          \
572
4.67M
  gint offset, frac, pos;                                       \
573
4.67M
  gint oversample = resampler->oversample;                      \
574
4.67M
  gint taps_stride = resampler->taps_stride;                    \
575
4.67M
                                                                \
576
4.67M
  pos = *samp_phase * oversample;                               \
577
4.67M
  offset = (oversample - 1) - pos / out_rate;                   \
578
4.67M
  frac = pos % out_rate;                                        \
579
4.67M
                                                                \
580
4.67M
  res = (gint8 *) resampler->taps + offset * taps_stride;       \
581
4.67M
  make_coeff_##type##_##inter (frac, out_rate, icoeff);         \
582
4.67M
                                                                \
583
4.67M
  *samp_index += resampler->samp_inc;                           \
584
4.67M
  *samp_phase += resampler->samp_frac;                          \
585
4.67M
  if (*samp_phase >= out_rate) {                                \
586
69.9k
    *samp_phase -= out_rate;                                    \
587
69.9k
    *samp_index += 1;                                           \
588
69.9k
  }                                                             \
589
4.67M
  return res;                                                   \
590
4.67M
}
get_taps_gint32_cubic
Line
Count
Source
568
240M
DECL_GET_TAPS_INTERPOLATE_FUNC (type, inter)                    \
569
240M
{                                                               \
570
240M
  gpointer res;                                                 \
571
240M
  gint out_rate = resampler->out_rate;                          \
572
240M
  gint offset, frac, pos;                                       \
573
240M
  gint oversample = resampler->oversample;                      \
574
240M
  gint taps_stride = resampler->taps_stride;                    \
575
240M
                                                                \
576
240M
  pos = *samp_phase * oversample;                               \
577
240M
  offset = (oversample - 1) - pos / out_rate;                   \
578
240M
  frac = pos % out_rate;                                        \
579
240M
                                                                \
580
240M
  res = (gint8 *) resampler->taps + offset * taps_stride;       \
581
240M
  make_coeff_##type##_##inter (frac, out_rate, icoeff);         \
582
240M
                                                                \
583
240M
  *samp_index += resampler->samp_inc;                           \
584
240M
  *samp_phase += resampler->samp_frac;                          \
585
240M
  if (*samp_phase >= out_rate) {                                \
586
8.55M
    *samp_phase -= out_rate;                                    \
587
8.55M
    *samp_index += 1;                                           \
588
8.55M
  }                                                             \
589
240M
  return res;                                                   \
590
240M
}
get_taps_gfloat_cubic
Line
Count
Source
568
2.14M
DECL_GET_TAPS_INTERPOLATE_FUNC (type, inter)                    \
569
2.14M
{                                                               \
570
2.14M
  gpointer res;                                                 \
571
2.14M
  gint out_rate = resampler->out_rate;                          \
572
2.14M
  gint offset, frac, pos;                                       \
573
2.14M
  gint oversample = resampler->oversample;                      \
574
2.14M
  gint taps_stride = resampler->taps_stride;                    \
575
2.14M
                                                                \
576
2.14M
  pos = *samp_phase * oversample;                               \
577
2.14M
  offset = (oversample - 1) - pos / out_rate;                   \
578
2.14M
  frac = pos % out_rate;                                        \
579
2.14M
                                                                \
580
2.14M
  res = (gint8 *) resampler->taps + offset * taps_stride;       \
581
2.14M
  make_coeff_##type##_##inter (frac, out_rate, icoeff);         \
582
2.14M
                                                                \
583
2.14M
  *samp_index += resampler->samp_inc;                           \
584
2.14M
  *samp_phase += resampler->samp_frac;                          \
585
2.14M
  if (*samp_phase >= out_rate) {                                \
586
276k
    *samp_phase -= out_rate;                                    \
587
276k
    *samp_index += 1;                                           \
588
276k
  }                                                             \
589
2.14M
  return res;                                                   \
590
2.14M
}
get_taps_gdouble_cubic
Line
Count
Source
568
185M
DECL_GET_TAPS_INTERPOLATE_FUNC (type, inter)                    \
569
185M
{                                                               \
570
185M
  gpointer res;                                                 \
571
185M
  gint out_rate = resampler->out_rate;                          \
572
185M
  gint offset, frac, pos;                                       \
573
185M
  gint oversample = resampler->oversample;                      \
574
185M
  gint taps_stride = resampler->taps_stride;                    \
575
185M
                                                                \
576
185M
  pos = *samp_phase * oversample;                               \
577
185M
  offset = (oversample - 1) - pos / out_rate;                   \
578
185M
  frac = pos % out_rate;                                        \
579
185M
                                                                \
580
185M
  res = (gint8 *) resampler->taps + offset * taps_stride;       \
581
185M
  make_coeff_##type##_##inter (frac, out_rate, icoeff);         \
582
185M
                                                                \
583
185M
  *samp_index += resampler->samp_inc;                           \
584
185M
  *samp_phase += resampler->samp_frac;                          \
585
185M
  if (*samp_phase >= out_rate) {                                \
586
2.72M
    *samp_phase -= out_rate;                                    \
587
2.72M
    *samp_index += 1;                                           \
588
2.72M
  }                                                             \
589
185M
  return res;                                                   \
590
185M
}
591
592
GET_TAPS_INTERPOLATE_FUNC (gint16, linear);
593
GET_TAPS_INTERPOLATE_FUNC (gint32, linear);
594
GET_TAPS_INTERPOLATE_FUNC (gfloat, linear);
595
GET_TAPS_INTERPOLATE_FUNC (gdouble, linear);
596
597
GET_TAPS_INTERPOLATE_FUNC (gint16, cubic);
598
GET_TAPS_INTERPOLATE_FUNC (gint32, cubic);
599
GET_TAPS_INTERPOLATE_FUNC (gfloat, cubic);
600
GET_TAPS_INTERPOLATE_FUNC (gdouble, cubic);
601
602
#define INNER_PRODUCT_NEAREST_FUNC(type)                        \
603
static inline void                                              \
604
inner_product_##type##_nearest_1_c (type * o, const type * a,   \
605
55.1M
    const type * b, gint len, const type *ic, gint bstride)     \
606
55.1M
{                                                               \
607
55.1M
  *o = *a;                                                      \
608
55.1M
}
audio-resampler.c:inner_product_gint16_nearest_1_c
Line
Count
Source
605
27.3M
    const type * b, gint len, const type *ic, gint bstride)     \
606
27.3M
{                                                               \
607
27.3M
  *o = *a;                                                      \
608
27.3M
}
audio-resampler.c:inner_product_gint32_nearest_1_c
Line
Count
Source
605
5.74M
    const type * b, gint len, const type *ic, gint bstride)     \
606
5.74M
{                                                               \
607
5.74M
  *o = *a;                                                      \
608
5.74M
}
audio-resampler.c:inner_product_gfloat_nearest_1_c
Line
Count
Source
605
15.6M
    const type * b, gint len, const type *ic, gint bstride)     \
606
15.6M
{                                                               \
607
15.6M
  *o = *a;                                                      \
608
15.6M
}
audio-resampler.c:inner_product_gdouble_nearest_1_c
Line
Count
Source
605
6.35M
    const type * b, gint len, const type *ic, gint bstride)     \
606
6.35M
{                                                               \
607
6.35M
  *o = *a;                                                      \
608
6.35M
}
609
INNER_PRODUCT_NEAREST_FUNC (gint16);
610
INNER_PRODUCT_NEAREST_FUNC (gint32);
611
INNER_PRODUCT_NEAREST_FUNC (gfloat);
612
INNER_PRODUCT_NEAREST_FUNC (gdouble);
613
614
#define INNER_PRODUCT_INT_FULL_FUNC(type,type2,prec,limit)      \
615
static inline void                                              \
616
inner_product_##type##_full_1_c (type * o, const type * a,      \
617
0
    const type * b, gint len, const type *ic, gint bstride)     \
618
0
{                                                               \
619
0
  gint i;                                                       \
620
0
  type2 res[4] = { 0, 0, 0, 0 };                                \
621
0
                                                                \
622
0
  for (i = 0; i < len; i += 4) {                                \
623
0
    res[0] += (type2) a[i + 0] * (type2) b[i + 0];              \
624
0
    res[1] += (type2) a[i + 1] * (type2) b[i + 1];              \
625
0
    res[2] += (type2) a[i + 2] * (type2) b[i + 2];              \
626
0
    res[3] += (type2) a[i + 3] * (type2) b[i + 3];              \
627
0
  }                                                             \
628
0
  res[0] = res[0] + res[1] + res[2] + res[3];                   \
629
0
  res[0] = (res[0] + ((type2)1 << ((prec) - 1))) >> (prec);     \
630
0
  *o = CLAMP (res[0], -(limit), (limit) - 1);                   \
631
0
}
Unexecuted instantiation: audio-resampler.c:inner_product_gint16_full_1_c
Unexecuted instantiation: audio-resampler.c:inner_product_gint32_full_1_c
632
633
INNER_PRODUCT_INT_FULL_FUNC (gint16, gint32, PRECISION_S16, (gint32) 1 << 15);
634
INNER_PRODUCT_INT_FULL_FUNC (gint32, gint64, PRECISION_S32, (gint64) 1 << 31);
635
636
#define INNER_PRODUCT_INT_LINEAR_FUNC(type,type2,prec,limit)    \
637
static inline void                                              \
638
inner_product_##type##_linear_1_c (type * o, const type * a,    \
639
0
    const type * b, gint len, const type *ic, gint bstride)     \
640
0
{                                                               \
641
0
  gint i;                                                       \
642
0
  type2 res[4] = { 0, 0, 0, 0 }, c0 = ic[0];                    \
643
0
  const type *c[2] = {(type*)((gint8*)b + 0*bstride),           \
644
0
                      (type*)((gint8*)b + 1*bstride)};          \
645
0
                                                                \
646
0
  for (i = 0; i < len; i += 2) {                                \
647
0
    res[0] += (type2) a[i + 0] * (type2) c[0][i + 0];           \
648
0
    res[1] += (type2) a[i + 0] * (type2) c[1][i + 0];           \
649
0
    res[2] += (type2) a[i + 1] * (type2) c[0][i + 1];           \
650
0
    res[3] += (type2) a[i + 1] * (type2) c[1][i + 1];           \
651
0
  }                                                             \
652
0
  res[0] = (res[0] + res[2]) >> (prec);                         \
653
0
  res[1] = (res[1] + res[3]) >> (prec);                         \
654
0
  res[0] = ((type2)(type)res[0] - (type2)(type)res[1]) * c0 +   \
655
0
           ((type2)(type)res[1] << (prec));                     \
656
0
  res[0] = (res[0] + ((type2)1 << ((prec) - 1))) >> (prec);     \
657
0
  *o = CLAMP (res[0], -(limit), (limit) - 1);                   \
658
0
}
Unexecuted instantiation: audio-resampler.c:inner_product_gint16_linear_1_c
Unexecuted instantiation: audio-resampler.c:inner_product_gint32_linear_1_c
659
660
INNER_PRODUCT_INT_LINEAR_FUNC (gint16, gint32, PRECISION_S16, (gint32) 1 << 15);
661
INNER_PRODUCT_INT_LINEAR_FUNC (gint32, gint64, PRECISION_S32, (gint64) 1 << 31);
662
663
#define INNER_PRODUCT_INT_CUBIC_FUNC(type,type2,prec,limit)     \
664
static inline void                                              \
665
inner_product_##type##_cubic_1_c (type * o, const type * a,     \
666
0
    const type * b, gint len, const type *ic, gint bstride)     \
667
0
{                                                               \
668
0
  gint i;                                                       \
669
0
  type2 res[4] = { 0, 0, 0, 0 };                                \
670
0
  const type *c[4] = {(type*)((gint8*)b + 0*bstride),           \
671
0
                      (type*)((gint8*)b + 1*bstride),           \
672
0
                      (type*)((gint8*)b + 2*bstride),           \
673
0
                      (type*)((gint8*)b + 3*bstride)};          \
674
0
                                                                \
675
0
  for (i = 0; i < len; i++) {                                   \
676
0
    res[0] += (type2) a[i] * (type2) c[0][i];                   \
677
0
    res[1] += (type2) a[i] * (type2) c[1][i];                   \
678
0
    res[2] += (type2) a[i] * (type2) c[2][i];                   \
679
0
    res[3] += (type2) a[i] * (type2) c[3][i];                   \
680
0
  }                                                             \
681
0
  res[0] = (type2)(type)(res[0] >> (prec)) * (type2) ic[0] +    \
682
0
           (type2)(type)(res[1] >> (prec)) * (type2) ic[1] +    \
683
0
           (type2)(type)(res[2] >> (prec)) * (type2) ic[2] +    \
684
0
           (type2)(type)(res[3] >> (prec)) * (type2) ic[3];     \
685
0
  res[0] = (res[0] + ((type2)1 << ((prec) - 1))) >> (prec);     \
686
0
  *o = CLAMP (res[0], -(limit), (limit) - 1);                   \
687
0
}
Unexecuted instantiation: audio-resampler.c:inner_product_gint16_cubic_1_c
Unexecuted instantiation: audio-resampler.c:inner_product_gint32_cubic_1_c
688
689
INNER_PRODUCT_INT_CUBIC_FUNC (gint16, gint32, PRECISION_S16, (gint32) 1 << 15);
690
INNER_PRODUCT_INT_CUBIC_FUNC (gint32, gint64, PRECISION_S32, (gint64) 1 << 31);
691
692
#define INNER_PRODUCT_FLOAT_FULL_FUNC(type)                     \
693
static inline void                                              \
694
inner_product_##type##_full_1_c (type * o, const type * a,      \
695
0
    const type * b, gint len, const type *ic, gint bstride)     \
696
0
{                                                               \
697
0
  gint i;                                                       \
698
0
  type res[4] = { 0.0, 0.0, 0.0, 0.0 };                         \
699
0
                                                                \
700
0
  for (i = 0; i < len; i += 4) {                                \
701
0
    res[0] += a[i + 0] * b[i + 0];                              \
702
0
    res[1] += a[i + 1] * b[i + 1];                              \
703
0
    res[2] += a[i + 2] * b[i + 2];                              \
704
0
    res[3] += a[i + 3] * b[i + 3];                              \
705
0
  }                                                             \
706
0
  *o = res[0] + res[1] + res[2] + res[3];                       \
707
0
}
Unexecuted instantiation: audio-resampler.c:inner_product_gfloat_full_1_c
Unexecuted instantiation: audio-resampler.c:inner_product_gdouble_full_1_c
708
709
INNER_PRODUCT_FLOAT_FULL_FUNC (gfloat);
710
INNER_PRODUCT_FLOAT_FULL_FUNC (gdouble);
711
712
#define INNER_PRODUCT_FLOAT_LINEAR_FUNC(type)                   \
713
static inline void                                              \
714
inner_product_##type##_linear_1_c (type * o, const type * a,    \
715
0
    const type * b, gint len, const type *ic, gint bstride)     \
716
0
{                                                               \
717
0
  gint i;                                                       \
718
0
  type res[4] = { 0.0, 0.0, 0.0, 0.0 };                         \
719
0
  const type *c[2] = {(type*)((gint8*)b + 0*bstride),           \
720
0
                      (type*)((gint8*)b + 1*bstride)};          \
721
0
                                                                \
722
0
  for (i = 0; i < len; i += 2) {                                \
723
0
    res[0] += a[i + 0] * c[0][i + 0];                           \
724
0
    res[1] += a[i + 0] * c[1][i + 0];                           \
725
0
    res[2] += a[i + 1] * c[0][i + 1];                           \
726
0
    res[3] += a[i + 1] * c[1][i + 1];                           \
727
0
  }                                                             \
728
0
  res[0] += res[2];                                             \
729
0
  res[1] += res[3];                                             \
730
0
  *o = (res[0] - res[1]) * ic[0] + res[1];                      \
731
0
}
Unexecuted instantiation: audio-resampler.c:inner_product_gfloat_linear_1_c
Unexecuted instantiation: audio-resampler.c:inner_product_gdouble_linear_1_c
732
INNER_PRODUCT_FLOAT_LINEAR_FUNC (gfloat);
733
INNER_PRODUCT_FLOAT_LINEAR_FUNC (gdouble);
734
735
#define INNER_PRODUCT_FLOAT_CUBIC_FUNC(type)                    \
736
static inline void                                              \
737
inner_product_##type##_cubic_1_c (type * o, const type * a,     \
738
0
    const type * b, gint len, const type *ic, gint bstride)     \
739
0
{                                                               \
740
0
  gint i;                                                       \
741
0
  type res[4] = { 0.0, 0.0, 0.0, 0.0 };                         \
742
0
  const type *c[4] = {(type*)((gint8*)b + 0*bstride),           \
743
0
                      (type*)((gint8*)b + 1*bstride),           \
744
0
                      (type*)((gint8*)b + 2*bstride),           \
745
0
                      (type*)((gint8*)b + 3*bstride)};          \
746
0
                                                                \
747
0
  for (i = 0; i < len; i++) {                                   \
748
0
    res[0] += a[i] * c[0][i];                                   \
749
0
    res[1] += a[i] * c[1][i];                                   \
750
0
    res[2] += a[i] * c[2][i];                                   \
751
0
    res[3] += a[i] * c[3][i];                                   \
752
0
  }                                                             \
753
0
  *o = res[0] * ic[0] + res[1] * ic[1] +                        \
754
0
       res[2] * ic[2] + res[3] * ic[3];                         \
755
0
}
Unexecuted instantiation: audio-resampler.c:inner_product_gfloat_cubic_1_c
Unexecuted instantiation: audio-resampler.c:inner_product_gdouble_cubic_1_c
756
INNER_PRODUCT_FLOAT_CUBIC_FUNC (gfloat);
757
INNER_PRODUCT_FLOAT_CUBIC_FUNC (gdouble);
758
759
MAKE_RESAMPLE_FUNC_STATIC (gint16, nearest, 1, c);
760
MAKE_RESAMPLE_FUNC_STATIC (gint32, nearest, 1, c);
761
MAKE_RESAMPLE_FUNC_STATIC (gfloat, nearest, 1, c);
762
MAKE_RESAMPLE_FUNC_STATIC (gdouble, nearest, 1, c);
763
764
MAKE_RESAMPLE_FUNC_STATIC (gint16, full, 1, c);
765
MAKE_RESAMPLE_FUNC_STATIC (gint32, full, 1, c);
766
MAKE_RESAMPLE_FUNC_STATIC (gfloat, full, 1, c);
767
MAKE_RESAMPLE_FUNC_STATIC (gdouble, full, 1, c);
768
769
MAKE_RESAMPLE_FUNC_STATIC (gint16, linear, 1, c);
770
MAKE_RESAMPLE_FUNC_STATIC (gint32, linear, 1, c);
771
MAKE_RESAMPLE_FUNC_STATIC (gfloat, linear, 1, c);
772
MAKE_RESAMPLE_FUNC_STATIC (gdouble, linear, 1, c);
773
774
MAKE_RESAMPLE_FUNC_STATIC (gint16, cubic, 1, c);
775
MAKE_RESAMPLE_FUNC_STATIC (gint32, cubic, 1, c);
776
MAKE_RESAMPLE_FUNC_STATIC (gfloat, cubic, 1, c);
777
MAKE_RESAMPLE_FUNC_STATIC (gdouble, cubic, 1, c);
778
779
static ResampleFunc resample_funcs[] = {
780
  resample_gint16_nearest_1_c,
781
  resample_gint32_nearest_1_c,
782
  resample_gfloat_nearest_1_c,
783
  resample_gdouble_nearest_1_c,
784
785
  resample_gint16_full_1_c,
786
  resample_gint32_full_1_c,
787
  resample_gfloat_full_1_c,
788
  resample_gdouble_full_1_c,
789
790
  resample_gint16_linear_1_c,
791
  resample_gint32_linear_1_c,
792
  resample_gfloat_linear_1_c,
793
  resample_gdouble_linear_1_c,
794
795
  resample_gint16_cubic_1_c,
796
  resample_gint32_cubic_1_c,
797
  resample_gfloat_cubic_1_c,
798
  resample_gdouble_cubic_1_c,
799
};
800
801
#define resample_gint16_nearest_1 resample_funcs[0]
802
#define resample_gint32_nearest_1 resample_funcs[1]
803
#define resample_gfloat_nearest_1 resample_funcs[2]
804
#define resample_gdouble_nearest_1 resample_funcs[3]
805
806
1
#define resample_gint16_full_1 resample_funcs[4]
807
1
#define resample_gint32_full_1 resample_funcs[5]
808
1
#define resample_gfloat_full_1 resample_funcs[6]
809
1
#define resample_gdouble_full_1 resample_funcs[7]
810
811
1
#define resample_gint16_linear_1 resample_funcs[8]
812
1
#define resample_gint32_linear_1 resample_funcs[9]
813
1
#define resample_gfloat_linear_1 resample_funcs[10]
814
1
#define resample_gdouble_linear_1 resample_funcs[11]
815
816
1
#define resample_gint16_cubic_1 resample_funcs[12]
817
1
#define resample_gint32_cubic_1 resample_funcs[13]
818
1
#define resample_gfloat_cubic_1 resample_funcs[14]
819
1
#define resample_gdouble_cubic_1 resample_funcs[15]
820
821
#if defined (HAVE_ARM_NEON)
822
#  define CHECK_NEON
823
#  include "audio-resampler-neon.h"
824
#endif
825
#if defined (HAVE_SSE) || defined(HAVE_SSE2) || defined(HAVE_SSE41)
826
#  define CHECK_X86
827
#  include "audio-resampler-x86.h"
828
#endif
829
#if defined (HAVE_RISCV_RVV)
830
#  define CHECK_RVV
831
#  include "audio-resampler-rvv.h"
832
#endif
833
834
static void
835
audio_resampler_init (void)
836
3.87k
{
837
3.87k
  static gsize init_gonce = 0;
838
3.87k
  if (g_once_init_enter (&init_gonce)) {
839
840
1
    GST_DEBUG_CATEGORY_INIT (audio_resampler_debug, "audio-resampler", 0,
841
1
        "audio-resampler object");
842
843
1
#ifdef CHECK_X86
844
1
    audio_resampler_check_x86 ();
845
1
#endif
846
#ifdef CHECK_NEON
847
    audio_resampler_check_neon ();
848
#endif
849
#ifdef CHECK_RVV
850
    if (gst_cpuid_supports_riscv_v ()) {
851
      GST_DEBUG ("enable RVV optimisations");
852
853
      resample_gint16_full_1 = resample_gint16_full_1_rvv;
854
      resample_gint16_linear_1 = resample_gint16_linear_1_rvv;
855
      resample_gint16_cubic_1 = resample_gint16_cubic_1_rvv;
856
857
      interpolate_gint16_linear = interpolate_gint16_linear_rvv;
858
      interpolate_gint16_cubic = interpolate_gint16_cubic_rvv;
859
860
      resample_gint32_full_1 = resample_gint32_full_1_rvv;
861
      resample_gint32_linear_1 = resample_gint32_linear_1_rvv;
862
      resample_gint32_cubic_1 = resample_gint32_cubic_1_rvv;
863
864
      interpolate_gint32_linear = interpolate_gint32_linear_rvv;
865
      interpolate_gint32_cubic = interpolate_gint32_cubic_rvv;
866
867
      resample_gfloat_full_1 = resample_gfloat_full_1_rvv;
868
      resample_gfloat_linear_1 = resample_gfloat_linear_1_rvv;
869
      resample_gfloat_cubic_1 = resample_gfloat_cubic_1_rvv;
870
871
      interpolate_gfloat_linear = interpolate_gfloat_linear_rvv;
872
      interpolate_gfloat_cubic = interpolate_gfloat_cubic_rvv;
873
    }
874
#endif
875
1
    g_once_init_leave (&init_gonce, 1);
876
1
  }
877
3.87k
}
878
879
#define MAKE_DEINTERLEAVE_FUNC(type)                                    \
880
static void                                                             \
881
deinterleave_ ##type (GstAudioResampler * resampler, gpointer sbuf[],   \
882
2.96k
    gpointer in[], gsize in_frames)                                     \
883
2.96k
{                                                                       \
884
2.96k
  gint i, c, channels = resampler->channels;                            \
885
2.96k
  gsize samples_avail = resampler->samples_avail;                       \
886
8.14k
  for (c = 0; c < channels; c++) {                                      \
887
5.17k
    type *s = (type *) sbuf[c] + samples_avail;                         \
888
5.17k
    if (G_UNLIKELY (in == NULL)) {                                      \
889
0
      for (i = 0; i < in_frames; i++)                                   \
890
0
        s[i] = 0;                                                       \
891
5.17k
    } else {                                                            \
892
5.17k
      type *ip = (type *) in[0] + c;                                    \
893
37.6M
      for (i = 0; i < in_frames; i++, ip += channels)                   \
894
37.6M
        s[i] = *ip;                                                     \
895
5.17k
    }                                                                   \
896
5.17k
  }                                                                     \
897
2.96k
}
audio-resampler.c:deinterleave_gint16
Line
Count
Source
882
904
    gpointer in[], gsize in_frames)                                     \
883
904
{                                                                       \
884
904
  gint i, c, channels = resampler->channels;                            \
885
904
  gsize samples_avail = resampler->samples_avail;                       \
886
2.34k
  for (c = 0; c < channels; c++) {                                      \
887
1.44k
    type *s = (type *) sbuf[c] + samples_avail;                         \
888
1.44k
    if (G_UNLIKELY (in == NULL)) {                                      \
889
0
      for (i = 0; i < in_frames; i++)                                   \
890
0
        s[i] = 0;                                                       \
891
1.44k
    } else {                                                            \
892
1.44k
      type *ip = (type *) in[0] + c;                                    \
893
1.59M
      for (i = 0; i < in_frames; i++, ip += channels)                   \
894
1.59M
        s[i] = *ip;                                                     \
895
1.44k
    }                                                                   \
896
1.44k
  }                                                                     \
897
904
}
audio-resampler.c:deinterleave_gint32
Line
Count
Source
882
1.09k
    gpointer in[], gsize in_frames)                                     \
883
1.09k
{                                                                       \
884
1.09k
  gint i, c, channels = resampler->channels;                            \
885
1.09k
  gsize samples_avail = resampler->samples_avail;                       \
886
2.96k
  for (c = 0; c < channels; c++) {                                      \
887
1.87k
    type *s = (type *) sbuf[c] + samples_avail;                         \
888
1.87k
    if (G_UNLIKELY (in == NULL)) {                                      \
889
0
      for (i = 0; i < in_frames; i++)                                   \
890
0
        s[i] = 0;                                                       \
891
1.87k
    } else {                                                            \
892
1.87k
      type *ip = (type *) in[0] + c;                                    \
893
24.4M
      for (i = 0; i < in_frames; i++, ip += channels)                   \
894
24.4M
        s[i] = *ip;                                                     \
895
1.87k
    }                                                                   \
896
1.87k
  }                                                                     \
897
1.09k
}
audio-resampler.c:deinterleave_gfloat
Line
Count
Source
882
377
    gpointer in[], gsize in_frames)                                     \
883
377
{                                                                       \
884
377
  gint i, c, channels = resampler->channels;                            \
885
377
  gsize samples_avail = resampler->samples_avail;                       \
886
1.19k
  for (c = 0; c < channels; c++) {                                      \
887
819
    type *s = (type *) sbuf[c] + samples_avail;                         \
888
819
    if (G_UNLIKELY (in == NULL)) {                                      \
889
0
      for (i = 0; i < in_frames; i++)                                   \
890
0
        s[i] = 0;                                                       \
891
819
    } else {                                                            \
892
819
      type *ip = (type *) in[0] + c;                                    \
893
1.70M
      for (i = 0; i < in_frames; i++, ip += channels)                   \
894
1.70M
        s[i] = *ip;                                                     \
895
819
    }                                                                   \
896
819
  }                                                                     \
897
377
}
audio-resampler.c:deinterleave_gdouble
Line
Count
Source
882
595
    gpointer in[], gsize in_frames)                                     \
883
595
{                                                                       \
884
595
  gint i, c, channels = resampler->channels;                            \
885
595
  gsize samples_avail = resampler->samples_avail;                       \
886
1.63k
  for (c = 0; c < channels; c++) {                                      \
887
1.04k
    type *s = (type *) sbuf[c] + samples_avail;                         \
888
1.04k
    if (G_UNLIKELY (in == NULL)) {                                      \
889
0
      for (i = 0; i < in_frames; i++)                                   \
890
0
        s[i] = 0;                                                       \
891
1.04k
    } else {                                                            \
892
1.04k
      type *ip = (type *) in[0] + c;                                    \
893
9.88M
      for (i = 0; i < in_frames; i++, ip += channels)                   \
894
9.88M
        s[i] = *ip;                                                     \
895
1.04k
    }                                                                   \
896
1.04k
  }                                                                     \
897
595
}
898
899
MAKE_DEINTERLEAVE_FUNC (gint16);
900
MAKE_DEINTERLEAVE_FUNC (gint32);
901
MAKE_DEINTERLEAVE_FUNC (gfloat);
902
MAKE_DEINTERLEAVE_FUNC (gdouble);
903
904
static DeinterleaveFunc deinterleave_funcs[] = {
905
  deinterleave_gint16,
906
  deinterleave_gint32,
907
  deinterleave_gfloat,
908
  deinterleave_gdouble
909
};
910
911
static void
912
copy_func (GstAudioResampler * resampler, gpointer sbuf[],
913
    gpointer in[], gsize in_frames)
914
0
{
915
0
  gint c, channels = resampler->channels;
916
0
  gsize samples_avail = resampler->samples_avail;
917
0
  for (c = 0; c < channels; c++) {
918
0
    guint8 *s = ((guint8 *) sbuf[c]) + (samples_avail * resampler->bps);
919
0
    if (G_UNLIKELY (in == NULL)) {
920
0
      memset (s, 0, in_frames * resampler->bps);
921
0
    } else {
922
0
      memcpy (s, in[c], in_frames * resampler->bps);
923
0
    }
924
0
  }
925
0
}
926
927
static void
928
calculate_kaiser_params (GstAudioResampler * resampler)
929
259
{
930
259
  gdouble A, B, dw, tr_bw, Fc;
931
259
  gint n;
932
259
  const KaiserQualityMap *q = &kaiser_qualities[DEFAULT_QUALITY];
933
934
  /* default cutoff */
935
259
  Fc = q->cutoff;
936
259
  if (resampler->out_rate < resampler->in_rate)
937
116
    Fc *= q->downsample_cutoff_factor;
938
939
259
  Fc = GET_OPT_CUTOFF (resampler->options, Fc);
940
259
  A = GET_OPT_STOP_ATTENUATION (resampler->options, q->stopband_attenuation);
941
259
  tr_bw =
942
259
      GET_OPT_TRANSITION_BANDWIDTH (resampler->options,
943
259
      q->transition_bandwidth);
944
945
259
  GST_LOG ("Fc %f, A %f, tr_bw %f", Fc, A, tr_bw);
946
947
  /* calculate Beta */
948
259
  if (A > 50)
949
259
    B = 0.1102 * (A - 8.7);
950
0
  else if (A >= 21)
951
0
    B = 0.5842 * pow (A - 21, 0.4) + 0.07886 * (A - 21);
952
0
  else
953
0
    B = 0.0;
954
  /* calculate transition width in radians */
955
259
  dw = 2 * G_PI * (tr_bw);
956
  /* order of the filter */
957
259
  n = (A - 8.0) / (2.285 * dw);
958
959
259
  resampler->kaiser_beta = B;
960
259
  resampler->n_taps = n + 1;
961
259
  resampler->cutoff = Fc;
962
963
259
  GST_LOG ("using Beta %f n_taps %d cutoff %f", resampler->kaiser_beta,
964
259
      resampler->n_taps, resampler->cutoff);
965
259
}
966
967
static void
968
alloc_taps_mem (GstAudioResampler * resampler, gint bps, gint n_taps,
969
    gint n_phases)
970
2.00k
{
971
2.00k
  if (resampler->alloc_taps >= n_taps && resampler->alloc_phases >= n_phases)
972
0
    return;
973
974
2.00k
  GST_DEBUG ("allocate bps %d n_taps %d n_phases %d", bps, n_taps, n_phases);
975
976
2.00k
  resampler->tmp_taps =
977
2.00k
      g_realloc_n (resampler->tmp_taps, n_taps, sizeof (gdouble));
978
979
2.00k
  resampler->taps_stride = GST_ROUND_UP_32 (bps * (n_taps + TAPS_OVERREAD));
980
981
2.00k
  g_free (resampler->taps_mem);
982
2.00k
  resampler->taps_mem =
983
2.00k
      g_malloc0 (n_phases * resampler->taps_stride + ALIGN - 1);
984
2.00k
  resampler->taps = MEM_ALIGN ((gint8 *) resampler->taps_mem, ALIGN);
985
2.00k
  resampler->alloc_taps = n_taps;
986
2.00k
  resampler->alloc_phases = n_phases;
987
2.00k
}
988
989
static void
990
alloc_cache_mem (GstAudioResampler * resampler, gint bps, gint n_taps,
991
    gint n_phases)
992
1.59k
{
993
1.59k
  gsize phases_size;
994
995
1.59k
  resampler->tmp_taps =
996
1.59k
      g_realloc_n (resampler->tmp_taps, n_taps, sizeof (gdouble));
997
998
1.59k
  resampler->cached_taps_stride =
999
1.59k
      GST_ROUND_UP_32 (bps * (n_taps + TAPS_OVERREAD));
1000
1001
1.59k
  phases_size = sizeof (gpointer) * n_phases;
1002
1003
1.59k
  g_free (resampler->cached_taps_mem);
1004
1.59k
  resampler->cached_taps_mem =
1005
1.59k
      g_malloc0 (phases_size + n_phases * resampler->cached_taps_stride +
1006
1.59k
      ALIGN - 1);
1007
1.59k
  resampler->cached_taps =
1008
1.59k
      MEM_ALIGN ((gint8 *) resampler->cached_taps_mem + phases_size, ALIGN);
1009
1.59k
  resampler->cached_phases = resampler->cached_taps_mem;
1010
1.59k
}
1011
1012
static void
1013
setup_functions (GstAudioResampler * resampler)
1014
3.87k
{
1015
3.87k
  gint index, fidx;
1016
1017
3.87k
  index = resampler->format_index;
1018
1019
3.87k
  if (resampler->in_rate == resampler->out_rate)
1020
524
    resampler->resample = resample_funcs[index];
1021
3.34k
  else {
1022
3.34k
    switch (resampler->filter_interpolation) {
1023
0
      default:
1024
1.41k
      case GST_AUDIO_RESAMPLER_FILTER_INTERPOLATION_NONE:
1025
1.41k
        fidx = 0;
1026
1.41k
        break;
1027
0
      case GST_AUDIO_RESAMPLER_FILTER_INTERPOLATION_LINEAR:
1028
0
        GST_DEBUG ("using linear interpolation for filter coefficients");
1029
0
        fidx = 0;
1030
0
        break;
1031
1.93k
      case GST_AUDIO_RESAMPLER_FILTER_INTERPOLATION_CUBIC:
1032
1.93k
        GST_DEBUG ("using cubic interpolation for filter coefficients");
1033
1.93k
        fidx = 4;
1034
1.93k
        break;
1035
3.34k
    }
1036
3.34k
    GST_DEBUG ("using filter interpolate function %d", index + fidx);
1037
3.34k
    resampler->interpolate = interpolate_funcs[index + fidx];
1038
1039
3.34k
    switch (resampler->method) {
1040
936
      case GST_AUDIO_RESAMPLER_METHOD_NEAREST:
1041
936
        GST_DEBUG ("using nearest filter function");
1042
936
        break;
1043
2.41k
      default:
1044
2.41k
        index += 4;
1045
2.41k
        switch (resampler->filter_mode) {
1046
0
          default:
1047
1.38k
          case GST_AUDIO_RESAMPLER_FILTER_MODE_FULL:
1048
1.38k
            GST_DEBUG ("using full filter function");
1049
1.38k
            break;
1050
1.03k
          case GST_AUDIO_RESAMPLER_FILTER_MODE_INTERPOLATED:
1051
1.03k
            index += 4 + fidx;
1052
1.03k
            GST_DEBUG ("using interpolated filter function");
1053
1.03k
            break;
1054
2.41k
        }
1055
2.41k
        break;
1056
3.34k
    }
1057
3.34k
    GST_DEBUG ("using resample function %d", index);
1058
3.34k
    resampler->resample = resample_funcs[index];
1059
3.34k
  }
1060
3.87k
}
1061
1062
static void
1063
resampler_calculate_taps (GstAudioResampler * resampler)
1064
3.87k
{
1065
3.87k
  gint bps;
1066
3.87k
  gint n_taps, oversample;
1067
3.87k
  gint in_rate, out_rate;
1068
3.87k
  gboolean scale = TRUE, sinc_table = FALSE;
1069
3.87k
  GstAudioResamplerFilterInterpolation filter_interpolation;
1070
1071
3.87k
  switch (resampler->method) {
1072
1.24k
    case GST_AUDIO_RESAMPLER_METHOD_NEAREST:
1073
1.24k
      resampler->n_taps = 2;
1074
1.24k
      scale = FALSE;
1075
1.24k
      break;
1076
229
    case GST_AUDIO_RESAMPLER_METHOD_LINEAR:
1077
229
      resampler->n_taps = GET_OPT_N_TAPS (resampler->options, 2);
1078
229
      break;
1079
397
    case GST_AUDIO_RESAMPLER_METHOD_CUBIC:
1080
397
      resampler->n_taps = GET_OPT_N_TAPS (resampler->options, 4);
1081
397
      resampler->b = GET_OPT_CUBIC_B (resampler->options);
1082
397
      resampler->c = GET_OPT_CUBIC_C (resampler->options);;
1083
397
      break;
1084
1.74k
    case GST_AUDIO_RESAMPLER_METHOD_BLACKMAN_NUTTALL:
1085
1.74k
    {
1086
1.74k
      const BlackmanQualityMap *q = &blackman_qualities[DEFAULT_QUALITY];
1087
1.74k
      resampler->n_taps = GET_OPT_N_TAPS (resampler->options, q->n_taps);
1088
1.74k
      resampler->cutoff = GET_OPT_CUTOFF (resampler->options, q->cutoff);
1089
1.74k
      sinc_table = TRUE;
1090
1.74k
      break;
1091
0
    }
1092
259
    case GST_AUDIO_RESAMPLER_METHOD_KAISER:
1093
259
      calculate_kaiser_params (resampler);
1094
259
      sinc_table = TRUE;
1095
259
      break;
1096
3.87k
  }
1097
1098
3.87k
  in_rate = resampler->in_rate;
1099
3.87k
  out_rate = resampler->out_rate;
1100
1101
3.87k
  if (out_rate < in_rate && scale) {
1102
897
    resampler->cutoff = resampler->cutoff * out_rate / in_rate;
1103
897
    resampler->n_taps =
1104
897
        gst_util_uint64_scale_int (resampler->n_taps, in_rate, out_rate);
1105
897
  }
1106
1107
3.87k
  if (sinc_table) {
1108
2.00k
    resampler->n_taps = GST_ROUND_UP_8 (resampler->n_taps);
1109
2.00k
    resampler->filter_mode = GET_OPT_FILTER_MODE (resampler->options);
1110
2.00k
    resampler->filter_threshold =
1111
2.00k
        GET_OPT_FILTER_MODE_THRESHOLD (resampler->options);
1112
2.00k
    filter_interpolation = GET_OPT_FILTER_INTERPOLATION (resampler->options);
1113
1114
2.00k
  } else {
1115
1.86k
    resampler->filter_mode = GST_AUDIO_RESAMPLER_FILTER_MODE_FULL;
1116
1.86k
    filter_interpolation = GST_AUDIO_RESAMPLER_FILTER_INTERPOLATION_NONE;
1117
1.86k
  }
1118
1119
  /* calculate oversampling for interpolated filter */
1120
3.87k
  if (filter_interpolation != GST_AUDIO_RESAMPLER_FILTER_INTERPOLATION_NONE) {
1121
2.00k
    gint mult = 2;
1122
1123
2.00k
    oversample = GET_OPT_FILTER_OVERSAMPLE (resampler->options);
1124
3.34k
    while (oversample > 1) {
1125
3.00k
      if (mult * out_rate >= in_rate)
1126
1.66k
        break;
1127
1128
1.34k
      mult *= 2;
1129
1.34k
      oversample >>= 1;
1130
1.34k
    }
1131
1132
2.00k
    switch (filter_interpolation) {
1133
0
      case GST_AUDIO_RESAMPLER_FILTER_INTERPOLATION_LINEAR:
1134
0
        oversample *= 11;
1135
0
        break;
1136
2.00k
      default:
1137
2.00k
        break;
1138
2.00k
    }
1139
2.00k
  } else {
1140
1.86k
    oversample = 1;
1141
1.86k
  }
1142
3.87k
  resampler->oversample = oversample;
1143
1144
3.87k
  n_taps = resampler->n_taps;
1145
3.87k
  bps = resampler->bps;
1146
1147
3.87k
  GST_LOG ("using n_taps %d cutoff %f oversample %d", n_taps, resampler->cutoff,
1148
3.87k
      oversample);
1149
1150
3.87k
  if (resampler->filter_mode == GST_AUDIO_RESAMPLER_FILTER_MODE_AUTO) {
1151
2.00k
    if (out_rate <= oversample
1152
335
        && !(resampler->flags & GST_AUDIO_RESAMPLER_FLAG_VARIABLE_RATE)) {
1153
      /* don't interpolate if we need to calculate at least the same amount
1154
       * of filter coefficients than the full table case */
1155
335
      resampler->filter_mode = GST_AUDIO_RESAMPLER_FILTER_MODE_FULL;
1156
335
      GST_DEBUG ("automatically selected full filter, %d <= %d", out_rate,
1157
335
          oversample);
1158
1.66k
    } else if (bps * n_taps * out_rate < resampler->filter_threshold) {
1159
      /* switch to full filter when memory is below threshold */
1160
638
      resampler->filter_mode = GST_AUDIO_RESAMPLER_FILTER_MODE_FULL;
1161
638
      GST_DEBUG ("automatically selected full filter, memory %d <= %d",
1162
638
          bps * n_taps * out_rate, resampler->filter_threshold);
1163
1.03k
    } else {
1164
1.03k
      GST_DEBUG ("automatically selected interpolated filter");
1165
1.03k
      resampler->filter_mode = GST_AUDIO_RESAMPLER_FILTER_MODE_INTERPOLATED;
1166
1.03k
    }
1167
2.00k
  }
1168
  /* interpolated table but no interpolation given, assume default */
1169
3.87k
  if (resampler->filter_mode != GST_AUDIO_RESAMPLER_FILTER_MODE_FULL &&
1170
1.03k
      filter_interpolation == GST_AUDIO_RESAMPLER_FILTER_INTERPOLATION_NONE)
1171
0
    filter_interpolation = DEFAULT_OPT_FILTER_INTERPOLATION;
1172
1173
3.87k
  resampler->filter_interpolation = filter_interpolation;
1174
1175
3.87k
  if (resampler->filter_mode == GST_AUDIO_RESAMPLER_FILTER_MODE_FULL &&
1176
2.84k
      resampler->method != GST_AUDIO_RESAMPLER_METHOD_NEAREST) {
1177
1.59k
    GST_DEBUG ("setting up filter cache");
1178
1.59k
    resampler->n_phases = out_rate;
1179
1.59k
    alloc_cache_mem (resampler, bps, n_taps, out_rate);
1180
1.59k
  }
1181
1182
3.87k
  if (resampler->filter_interpolation !=
1183
3.87k
      GST_AUDIO_RESAMPLER_FILTER_INTERPOLATION_NONE) {
1184
2.00k
    gint i, isize;
1185
2.00k
    gdouble x;
1186
2.00k
    gpointer taps;
1187
1188
2.00k
    switch (resampler->filter_interpolation) {
1189
0
      default:
1190
0
      case GST_AUDIO_RESAMPLER_FILTER_INTERPOLATION_LINEAR:
1191
0
        GST_DEBUG ("using linear interpolation to build filter");
1192
0
        isize = 2;
1193
0
        break;
1194
2.00k
      case GST_AUDIO_RESAMPLER_FILTER_INTERPOLATION_CUBIC:
1195
2.00k
        GST_DEBUG ("using cubic interpolation to build filter");
1196
2.00k
        isize = 4;
1197
2.00k
        break;
1198
2.00k
    }
1199
1200
2.00k
    alloc_taps_mem (resampler, bps, n_taps, oversample + isize);
1201
1202
22.5k
    for (i = 0; i < oversample + isize; i++) {
1203
20.5k
      x = -(n_taps / 2) + i / (gdouble) oversample;
1204
20.5k
      taps = (gint8 *) resampler->taps + i * resampler->taps_stride;
1205
20.5k
      make_taps (resampler, taps, x, n_taps);
1206
20.5k
    }
1207
2.00k
  }
1208
3.87k
}
1209
1210
#define PRINT_TAPS(type,print)                          \
1211
G_STMT_START {                                          \
1212
  type sum = 0.0, *taps;                                \
1213
  type icoeff[4];                                       \
1214
  gint samp_index = 0, samp_phase = i;                  \
1215
                                                        \
1216
  taps = get_taps_##type##_full (resampler, &samp_index,\
1217
      &samp_phase, icoeff);                             \
1218
                                                        \
1219
  for (j = 0; j < n_taps; j++) {                        \
1220
    type tap = taps[j];                                 \
1221
    fprintf (stderr, "\t%" print " ", tap);             \
1222
    sum += tap;                                         \
1223
  }                                                     \
1224
  fprintf (stderr, "\t: sum %" print "\n", sum);        \
1225
} G_STMT_END
1226
1227
static void
1228
resampler_dump (GstAudioResampler * resampler)
1229
3.87k
{
1230
#if 0
1231
  gint i, n_taps, out_rate;
1232
  gint64 a;
1233
1234
  out_rate = resampler->out_rate;
1235
  n_taps = resampler->n_taps;
1236
1237
  fprintf (stderr, "out size %d, max taps %d\n", out_rate, n_taps);
1238
1239
  a = g_get_monotonic_time ();
1240
1241
  for (i = 0; i < out_rate; i++) {
1242
    gint j;
1243
1244
    //fprintf (stderr, "%u: %d %d\t ", i, t->sample_inc, t->next_phase);
1245
    switch (resampler->format) {
1246
      case GST_AUDIO_FORMAT_F64:
1247
        PRINT_TAPS (gdouble, "f");
1248
        break;
1249
      case GST_AUDIO_FORMAT_F32:
1250
        PRINT_TAPS (gfloat, "f");
1251
        break;
1252
      case GST_AUDIO_FORMAT_S32:
1253
        PRINT_TAPS (gint32, "d");
1254
        break;
1255
      case GST_AUDIO_FORMAT_S16:
1256
        PRINT_TAPS (gint16, "d");
1257
        break;
1258
      default:
1259
        break;
1260
    }
1261
  }
1262
  fprintf (stderr, "time %" G_GUINT64_FORMAT "\n", g_get_monotonic_time () - a);
1263
#endif
1264
3.87k
}
1265
1266
/**
1267
 * gst_audio_resampler_options_set_quality:
1268
 * @method: a #GstAudioResamplerMethod
1269
 * @quality: the quality
1270
 * @in_rate: the input rate
1271
 * @out_rate: the output rate
1272
 * @options: a #GstStructure
1273
 *
1274
 * Set the parameters for resampling from @in_rate to @out_rate using @method
1275
 * for @quality in @options.
1276
 */
1277
void
1278
gst_audio_resampler_options_set_quality (GstAudioResamplerMethod method,
1279
    guint quality, gint in_rate, gint out_rate, GstStructure * options)
1280
2.50k
{
1281
2.50k
  g_return_if_fail (options != NULL);
1282
2.50k
  g_return_if_fail (quality <= GST_AUDIO_RESAMPLER_QUALITY_MAX);
1283
2.50k
  g_return_if_fail (in_rate > 0 && out_rate > 0);
1284
1285
2.50k
  switch (method) {
1286
0
    case GST_AUDIO_RESAMPLER_METHOD_NEAREST:
1287
0
      break;
1288
0
    case GST_AUDIO_RESAMPLER_METHOD_LINEAR:
1289
0
      gst_structure_set (options,
1290
0
          GST_AUDIO_RESAMPLER_OPT_N_TAPS, G_TYPE_INT, 2, NULL);
1291
0
      break;
1292
0
    case GST_AUDIO_RESAMPLER_METHOD_CUBIC:
1293
0
      gst_structure_set (options,
1294
0
          GST_AUDIO_RESAMPLER_OPT_N_TAPS, G_TYPE_INT, 4,
1295
0
          GST_AUDIO_RESAMPLER_OPT_CUBIC_B, G_TYPE_DOUBLE, DEFAULT_OPT_CUBIC_B,
1296
0
          GST_AUDIO_RESAMPLER_OPT_CUBIC_C, G_TYPE_DOUBLE, DEFAULT_OPT_CUBIC_C,
1297
0
          NULL);
1298
0
      break;
1299
0
    case GST_AUDIO_RESAMPLER_METHOD_BLACKMAN_NUTTALL:
1300
0
    {
1301
0
      const BlackmanQualityMap *map = &blackman_qualities[quality];
1302
0
      gst_structure_set (options,
1303
0
          GST_AUDIO_RESAMPLER_OPT_N_TAPS, G_TYPE_INT, map->n_taps,
1304
0
          GST_AUDIO_RESAMPLER_OPT_CUTOFF, G_TYPE_DOUBLE, map->cutoff, NULL);
1305
0
      break;
1306
0
    }
1307
2.50k
    case GST_AUDIO_RESAMPLER_METHOD_KAISER:
1308
2.50k
    {
1309
2.50k
      const KaiserQualityMap *map = &kaiser_qualities[quality];
1310
2.50k
      gdouble cutoff;
1311
1312
2.50k
      cutoff = map->cutoff;
1313
2.50k
      if (out_rate < in_rate)
1314
731
        cutoff *= map->downsample_cutoff_factor;
1315
1316
2.50k
      gst_structure_set (options,
1317
2.50k
          GST_AUDIO_RESAMPLER_OPT_CUTOFF, G_TYPE_DOUBLE, cutoff,
1318
2.50k
          GST_AUDIO_RESAMPLER_OPT_STOP_ATTENUATION, G_TYPE_DOUBLE,
1319
2.50k
          map->stopband_attenuation,
1320
2.50k
          GST_AUDIO_RESAMPLER_OPT_TRANSITION_BANDWIDTH, G_TYPE_DOUBLE,
1321
2.50k
          map->transition_bandwidth, NULL);
1322
2.50k
      break;
1323
0
    }
1324
2.50k
  }
1325
2.50k
  gst_structure_set (options,
1326
2.50k
      GST_AUDIO_RESAMPLER_OPT_FILTER_OVERSAMPLE, G_TYPE_INT,
1327
2.50k
      oversample_qualities[quality], NULL);
1328
2.50k
}
1329
1330
/**
1331
 * gst_audio_resampler_new:
1332
 * @method: a #GstAudioResamplerMethod
1333
 * @flags: #GstAudioResamplerFlags
1334
 * @format: the #GstAudioFormat
1335
 * @channels: the number of channels
1336
 * @in_rate: input rate
1337
 * @out_rate: output rate
1338
 * @options: extra options
1339
 *
1340
 * Make a new resampler.
1341
 *
1342
 * Returns: (skip) (transfer full): The new #GstAudioResampler.
1343
 */
1344
GstAudioResampler *
1345
gst_audio_resampler_new (GstAudioResamplerMethod method,
1346
    GstAudioResamplerFlags flags,
1347
    GstAudioFormat format, gint channels,
1348
    gint in_rate, gint out_rate, GstStructure * options)
1349
3.87k
{
1350
3.87k
  gboolean non_interleaved_in, non_interleaved_out;
1351
3.87k
  GstAudioResampler *resampler;
1352
3.87k
  const GstAudioFormatInfo *info;
1353
3.87k
  GstStructure *def_options = NULL;
1354
1355
3.87k
  g_return_val_if_fail (method >= GST_AUDIO_RESAMPLER_METHOD_NEAREST
1356
3.87k
      && method <= GST_AUDIO_RESAMPLER_METHOD_KAISER, NULL);
1357
3.87k
  g_return_val_if_fail (format == GST_AUDIO_FORMAT_S16 ||
1358
3.87k
      format == GST_AUDIO_FORMAT_S32 || format == GST_AUDIO_FORMAT_F32 ||
1359
3.87k
      format == GST_AUDIO_FORMAT_F64, NULL);
1360
3.87k
  g_return_val_if_fail (channels > 0, NULL);
1361
3.87k
  g_return_val_if_fail (in_rate > 0, NULL);
1362
3.87k
  g_return_val_if_fail (out_rate > 0, NULL);
1363
1364
3.87k
  audio_resampler_init ();
1365
1366
3.87k
  resampler = g_new0 (GstAudioResampler, 1);
1367
3.87k
  resampler->method = method;
1368
3.87k
  resampler->flags = flags;
1369
3.87k
  resampler->format = format;
1370
3.87k
  resampler->channels = channels;
1371
1372
3.87k
  switch (format) {
1373
1.16k
    case GST_AUDIO_FORMAT_S16:
1374
1.16k
      resampler->format_index = 0;
1375
1.16k
      break;
1376
1.32k
    case GST_AUDIO_FORMAT_S32:
1377
1.32k
      resampler->format_index = 1;
1378
1.32k
      break;
1379
540
    case GST_AUDIO_FORMAT_F32:
1380
540
      resampler->format_index = 2;
1381
540
      break;
1382
836
    case GST_AUDIO_FORMAT_F64:
1383
836
      resampler->format_index = 3;
1384
836
      break;
1385
0
    default:
1386
0
      g_assert_not_reached ();
1387
0
      break;
1388
3.87k
  }
1389
1390
3.87k
  info = gst_audio_format_get_info (format);
1391
3.87k
  resampler->bps = GST_AUDIO_FORMAT_INFO_WIDTH (info) / 8;
1392
3.87k
  resampler->sbuf = g_malloc0 (sizeof (gpointer) * channels);
1393
1394
3.87k
  non_interleaved_in =
1395
3.87k
      (resampler->flags & GST_AUDIO_RESAMPLER_FLAG_NON_INTERLEAVED_IN);
1396
3.87k
  non_interleaved_out =
1397
3.87k
      (resampler->flags & GST_AUDIO_RESAMPLER_FLAG_NON_INTERLEAVED_OUT);
1398
1399
  /* we resample each channel separately */
1400
3.87k
  resampler->blocks = resampler->channels;
1401
3.87k
  resampler->inc = 1;
1402
3.87k
  resampler->ostride = non_interleaved_out ? 1 : resampler->channels;
1403
3.87k
  resampler->deinterleave = non_interleaved_in ?
1404
3.87k
      copy_func : deinterleave_funcs[resampler->format_index];
1405
3.87k
  resampler->convert_taps = convert_taps_funcs[resampler->format_index];
1406
1407
3.87k
  GST_DEBUG ("method %d, bps %d, channels %d", method, resampler->bps,
1408
3.87k
      resampler->channels);
1409
1410
3.87k
  if (options == NULL) {
1411
2.50k
    options = def_options =
1412
2.50k
        gst_structure_new_empty ("GstAudioResampler.options");
1413
2.50k
    gst_audio_resampler_options_set_quality (DEFAULT_RESAMPLER_METHOD,
1414
2.50k
        GST_AUDIO_RESAMPLER_QUALITY_DEFAULT, in_rate, out_rate, options);
1415
2.50k
  }
1416
1417
3.87k
  gst_audio_resampler_update (resampler, in_rate, out_rate, options);
1418
3.87k
  gst_audio_resampler_reset (resampler);
1419
1420
3.87k
  if (def_options)
1421
2.50k
    gst_structure_free (def_options);
1422
1423
3.87k
  return resampler;
1424
3.87k
}
1425
1426
/* make the buffers to hold the (deinterleaved) samples */
1427
static inline gpointer *
1428
get_sample_bufs (GstAudioResampler * resampler, gsize need)
1429
2.96k
{
1430
2.96k
  if (G_LIKELY (resampler->samples_len < need)) {
1431
2.96k
    gint c, blocks = resampler->blocks;
1432
2.96k
    gsize bytes, to_move = 0;
1433
2.96k
    gint8 *ptr, *samples;
1434
1435
2.96k
    GST_LOG ("realloc %d -> %d", (gint) resampler->samples_len, (gint) need);
1436
1437
2.96k
    bytes = GST_ROUND_UP_N (need * resampler->bps * resampler->inc, ALIGN);
1438
1439
2.96k
    samples = g_malloc0 (blocks * bytes + ALIGN - 1);
1440
2.96k
    ptr = MEM_ALIGN (samples, ALIGN);
1441
1442
    /* if we had some data, move history */
1443
2.96k
    if (resampler->samples_len > 0)
1444
0
      to_move = resampler->samples_avail * resampler->bps * resampler->inc;
1445
1446
    /* set up new pointers */
1447
8.14k
    for (c = 0; c < blocks; c++) {
1448
5.17k
      memcpy (ptr + (c * bytes), resampler->sbuf[c], to_move);
1449
5.17k
      resampler->sbuf[c] = ptr + (c * bytes);
1450
5.17k
    }
1451
2.96k
    g_free (resampler->samples);
1452
2.96k
    resampler->samples = samples;
1453
2.96k
    resampler->samples_len = need;
1454
2.96k
  }
1455
2.96k
  return resampler->sbuf;
1456
2.96k
}
1457
1458
/**
1459
 * gst_audio_resampler_reset:
1460
 * @resampler: a #GstAudioResampler
1461
 *
1462
 * Reset @resampler to the state it was when it was first created, discarding
1463
 * all sample history.
1464
 */
1465
void
1466
gst_audio_resampler_reset (GstAudioResampler * resampler)
1467
3.87k
{
1468
3.87k
  g_return_if_fail (resampler != NULL);
1469
1470
3.87k
  if (resampler->samples) {
1471
0
    gsize bytes;
1472
0
    gint c, blocks, bpf;
1473
1474
0
    bpf = resampler->bps * resampler->inc;
1475
0
    bytes = (resampler->n_taps / 2) * bpf;
1476
0
    blocks = resampler->blocks;
1477
1478
0
    for (c = 0; c < blocks; c++)
1479
0
      memset (resampler->sbuf[c], 0, bytes);
1480
0
  }
1481
  /* half of the filter is filled with 0 */
1482
3.87k
  resampler->samp_index = 0;
1483
3.87k
  resampler->samples_avail = resampler->n_taps / 2 - 1;
1484
3.87k
}
1485
1486
/**
1487
 * gst_audio_resampler_update:
1488
 * @resampler: a #GstAudioResampler
1489
 * @in_rate: new input rate
1490
 * @out_rate: new output rate
1491
 * @options: new options or %NULL
1492
 *
1493
 * Update the resampler parameters for @resampler. This function should
1494
 * not be called concurrently with any other function on @resampler.
1495
 *
1496
 * When @in_rate or @out_rate is 0, its value is unchanged.
1497
 *
1498
 * When @options is %NULL, the previously configured options are reused.
1499
 *
1500
 * Returns: %TRUE if the new parameters could be set
1501
 */
1502
gboolean
1503
gst_audio_resampler_update (GstAudioResampler * resampler,
1504
    gint in_rate, gint out_rate, GstStructure * options)
1505
3.87k
{
1506
3.87k
  gint gcd, samp_phase, old_n_taps;
1507
3.87k
  gdouble max_error;
1508
1509
3.87k
  g_return_val_if_fail (resampler != NULL, FALSE);
1510
1511
3.87k
  if (in_rate <= 0)
1512
0
    in_rate = resampler->in_rate;
1513
3.87k
  if (out_rate <= 0)
1514
0
    out_rate = resampler->out_rate;
1515
1516
3.87k
  if (resampler->out_rate > 0) {
1517
0
    GST_INFO ("old phase %d/%d", resampler->samp_phase, resampler->out_rate);
1518
0
    samp_phase =
1519
0
        gst_util_uint64_scale_int (resampler->samp_phase, out_rate,
1520
0
        resampler->out_rate);
1521
0
  } else
1522
3.87k
    samp_phase = 0;
1523
1524
3.87k
  gcd = gst_util_greatest_common_divisor (in_rate, out_rate);
1525
1526
3.87k
  max_error = GET_OPT_MAX_PHASE_ERROR (resampler->options);
1527
1528
3.87k
  if (max_error < 1.0e-8) {
1529
0
    GST_INFO ("using exact phase divider");
1530
0
    gcd = gst_util_greatest_common_divisor (gcd, samp_phase);
1531
3.87k
  } else {
1532
3.87k
    while (gcd > 1) {
1533
1.89k
      gdouble ph1 = (gdouble) samp_phase / out_rate;
1534
1.89k
      gint factor = 2;
1535
1536
      /* reduce the factor until we have a phase error of less than 10% */
1537
1.89k
      gdouble ph2 = (gdouble) (samp_phase / gcd) / (out_rate / gcd);
1538
1539
1.89k
      if (fabs (ph1 - ph2) < max_error)
1540
1.89k
        break;
1541
1542
0
      while (gcd % factor != 0)
1543
0
        factor++;
1544
0
      gcd /= factor;
1545
1546
0
      GST_INFO ("divide by factor %d, gcd %d", factor, gcd);
1547
0
    }
1548
3.87k
  }
1549
1550
3.87k
  GST_INFO ("phase %d out_rate %d, in_rate %d, gcd %d", samp_phase, out_rate,
1551
3.87k
      in_rate, gcd);
1552
1553
3.87k
  resampler->samp_phase = samp_phase /= gcd;
1554
3.87k
  resampler->in_rate = in_rate /= gcd;
1555
3.87k
  resampler->out_rate = out_rate /= gcd;
1556
1557
3.87k
  GST_INFO ("new phase %d/%d", resampler->samp_phase, resampler->out_rate);
1558
1559
3.87k
  resampler->samp_inc = in_rate / out_rate;
1560
3.87k
  resampler->samp_frac = in_rate % out_rate;
1561
1562
3.87k
  if (options) {
1563
3.87k
    GST_INFO ("have new options, reconfigure filter");
1564
1565
3.87k
    if (resampler->options)
1566
0
      gst_structure_free (resampler->options);
1567
3.87k
    resampler->options = gst_structure_copy (options);
1568
1569
3.87k
    old_n_taps = resampler->n_taps;
1570
1571
3.87k
    resampler_calculate_taps (resampler);
1572
3.87k
    resampler_dump (resampler);
1573
1574
3.87k
    if (old_n_taps > 0 && old_n_taps != resampler->n_taps) {
1575
0
      gpointer *sbuf;
1576
0
      gint i, bpf, bytes, soff, doff, diff;
1577
1578
0
      sbuf = get_sample_bufs (resampler, resampler->n_taps);
1579
1580
0
      bpf = resampler->bps * resampler->inc;
1581
0
      bytes = resampler->samples_avail * bpf;
1582
0
      soff = doff = resampler->samp_index * bpf;
1583
1584
0
      diff = ((gint) resampler->n_taps - old_n_taps) / 2;
1585
1586
0
      GST_DEBUG ("taps %d->%d, %d", old_n_taps, resampler->n_taps, diff);
1587
1588
0
      if (diff < 0) {
1589
        /* diff < 0, decrease taps, adjust source */
1590
0
        soff += -diff * bpf;
1591
0
        bytes -= -diff * bpf;
1592
0
      } else {
1593
        /* diff > 0, increase taps, adjust dest */
1594
0
        doff += diff * bpf;
1595
0
      }
1596
1597
      /* now shrink or enlarge the history buffer, when we enlarge we
1598
       * just leave the old samples in there. FIXME, probably do something better
1599
       * like mirror or fill with zeroes. */
1600
0
      for (i = 0; i < resampler->blocks; i++)
1601
0
        memmove ((gint8 *) sbuf[i] + doff, (gint8 *) sbuf[i] + soff, bytes);
1602
1603
0
      resampler->samples_avail += diff;
1604
0
    }
1605
3.87k
  } else if (resampler->filter_mode == GST_AUDIO_RESAMPLER_FILTER_MODE_FULL) {
1606
0
    GST_DEBUG ("setting up filter cache");
1607
0
    resampler->n_phases = resampler->out_rate;
1608
0
    alloc_cache_mem (resampler, resampler->bps, resampler->n_taps,
1609
0
        resampler->n_phases);
1610
0
  }
1611
3.87k
  setup_functions (resampler);
1612
1613
3.87k
  return TRUE;
1614
3.87k
}
1615
1616
/**
1617
 * gst_audio_resampler_free:
1618
 * @resampler: a #GstAudioResampler
1619
 *
1620
 * Free a previously allocated #GstAudioResampler @resampler.
1621
 */
1622
void
1623
gst_audio_resampler_free (GstAudioResampler * resampler)
1624
3.87k
{
1625
3.87k
  g_return_if_fail (resampler != NULL);
1626
1627
3.87k
  g_free (resampler->cached_taps_mem);
1628
3.87k
  g_free (resampler->taps_mem);
1629
3.87k
  g_free (resampler->tmp_taps);
1630
3.87k
  g_free (resampler->samples);
1631
3.87k
  g_free (resampler->sbuf);
1632
3.87k
  if (resampler->options)
1633
3.87k
    gst_structure_free (resampler->options);
1634
3.87k
  g_free (resampler);
1635
3.87k
}
1636
1637
/**
1638
 * gst_audio_resampler_get_out_frames:
1639
 * @resampler: a #GstAudioResampler
1640
 * @in_frames: number of input frames
1641
 *
1642
 * Get the number of output frames that would be currently available when
1643
 * @in_frames are given to @resampler.
1644
 *
1645
 * Returns: The number of frames that would be available after giving
1646
 * @in_frames as input to @resampler.
1647
 */
1648
gsize
1649
gst_audio_resampler_get_out_frames (GstAudioResampler * resampler,
1650
    gsize in_frames)
1651
3.46k
{
1652
3.46k
  gsize need, avail, out;
1653
1654
3.46k
  g_return_val_if_fail (resampler != NULL, 0);
1655
1656
3.46k
  need = resampler->n_taps + resampler->samp_index + resampler->skip;
1657
3.46k
  avail = resampler->samples_avail + in_frames;
1658
3.46k
  GST_LOG ("need %d = %d + %d + %d, avail %d = %d + %d", (gint) need,
1659
3.46k
      resampler->n_taps, resampler->samp_index, resampler->skip,
1660
3.46k
      (gint) avail, (gint) resampler->samples_avail, (gint) in_frames);
1661
3.46k
  if (avail < need) {
1662
989
    GST_LOG ("avail %d < need %d", (int) avail, (int) need);
1663
989
    return 0;
1664
989
  }
1665
1666
2.47k
  out = (avail - need) * resampler->out_rate;
1667
2.47k
  if (out < resampler->samp_phase) {
1668
0
    GST_LOG ("out %d < samp_phase %d", (int) out, (int) resampler->samp_phase);
1669
0
    return 0;
1670
0
  }
1671
1672
2.47k
  out = ((out - resampler->samp_phase) / resampler->in_rate) + 1;
1673
2.47k
  GST_LOG ("out %d = ((%d * %d - %d) / %d) + 1", (gint) out,
1674
2.47k
      (gint) (avail - need), resampler->out_rate, resampler->samp_phase,
1675
2.47k
      resampler->in_rate);
1676
1677
2.47k
  return out;
1678
2.47k
}
1679
1680
/**
1681
 * gst_audio_resampler_get_in_frames:
1682
 * @resampler: a #GstAudioResampler
1683
 * @out_frames: number of input frames
1684
 *
1685
 * Get the number of input frames that would currently be needed
1686
 * to produce @out_frames from @resampler.
1687
 *
1688
 * Returns: The number of input frames needed for producing
1689
 * @out_frames of data from @resampler.
1690
 */
1691
gsize
1692
gst_audio_resampler_get_in_frames (GstAudioResampler * resampler,
1693
    gsize out_frames)
1694
0
{
1695
0
  gsize in_frames;
1696
1697
0
  g_return_val_if_fail (resampler != NULL, 0);
1698
1699
0
  in_frames =
1700
0
      (resampler->samp_phase +
1701
0
      out_frames * resampler->samp_frac) / resampler->out_rate;
1702
0
  in_frames += out_frames * resampler->samp_inc;
1703
1704
0
  return in_frames;
1705
0
}
1706
1707
/**
1708
 * gst_audio_resampler_get_max_latency:
1709
 * @resampler: a #GstAudioResampler
1710
 *
1711
 * Get the maximum number of input samples that the resampler would
1712
 * need before producing output.
1713
 *
1714
 * Returns: the latency of @resampler as expressed in the number of
1715
 * frames.
1716
 */
1717
gsize
1718
gst_audio_resampler_get_max_latency (GstAudioResampler * resampler)
1719
0
{
1720
0
  g_return_val_if_fail (resampler != NULL, 0);
1721
1722
0
  return resampler->n_taps / 2;
1723
0
}
1724
1725
/**
1726
 * gst_audio_resampler_resample:
1727
 * @resampler: a #GstAudioResampler
1728
 * @in: input samples
1729
 * @in_frames: number of input frames
1730
 * @out: output samples
1731
 * @out_frames: number of output frames
1732
 *
1733
 * Perform resampling on @in_frames frames in @in and write @out_frames to @out.
1734
 *
1735
 * In case the samples are interleaved, @in and @out must point to an
1736
 * array with a single element pointing to a block of interleaved samples.
1737
 *
1738
 * If non-interleaved samples are used, @in and @out must point to an
1739
 * array with pointers to memory blocks, one for each channel.
1740
 *
1741
 * @in may be %NULL, in which case @in_frames of silence samples are pushed
1742
 * into the resampler.
1743
 *
1744
 * This function always produces @out_frames of output and consumes @in_frames of
1745
 * input. Use gst_audio_resampler_get_out_frames() and
1746
 * gst_audio_resampler_get_in_frames() to make sure @in_frames and @out_frames
1747
 * are matching and @in and @out point to enough memory.
1748
 */
1749
void
1750
gst_audio_resampler_resample (GstAudioResampler * resampler,
1751
    gpointer in[], gsize in_frames, gpointer out[], gsize out_frames)
1752
2.96k
{
1753
2.96k
  gsize samples_avail;
1754
2.96k
  gsize need, consumed;
1755
2.96k
  gpointer *sbuf;
1756
1757
  /* do sample skipping */
1758
2.96k
  if (G_UNLIKELY (resampler->skip >= in_frames)) {
1759
    /* we need tp skip all input */
1760
0
    resampler->skip -= in_frames;
1761
0
    return;
1762
0
  }
1763
  /* skip the last samples by advancing the sample index */
1764
2.96k
  resampler->samp_index += resampler->skip;
1765
1766
2.96k
  samples_avail = resampler->samples_avail;
1767
1768
  /* make sure we have enough space to copy our samples */
1769
2.96k
  sbuf = get_sample_bufs (resampler, in_frames + samples_avail);
1770
1771
  /* copy/deinterleave the samples */
1772
2.96k
  resampler->deinterleave (resampler, sbuf, in, in_frames);
1773
1774
  /* update new amount of samples in our buffer */
1775
2.96k
  resampler->samples_avail = samples_avail += in_frames;
1776
1777
2.96k
  need = resampler->n_taps + resampler->samp_index;
1778
2.96k
  if (G_UNLIKELY (samples_avail < need || out_frames == 0)) {
1779
496
    GST_LOG ("not enough samples to start: need %" G_GSIZE_FORMAT ", avail %"
1780
496
        G_GSIZE_FORMAT ", out %" G_GSIZE_FORMAT, need, samples_avail,
1781
496
        out_frames);
1782
    /* not enough samples to start */
1783
496
    return;
1784
496
  }
1785
1786
  /* resample all channels */
1787
2.47k
  resampler->resample (resampler, sbuf, samples_avail, out, out_frames,
1788
2.47k
      &consumed);
1789
1790
2.47k
  GST_LOG ("in %" G_GSIZE_FORMAT ", avail %" G_GSIZE_FORMAT ", consumed %"
1791
2.47k
      G_GSIZE_FORMAT, in_frames, samples_avail, consumed);
1792
1793
  /* update pointers */
1794
2.47k
  if (G_LIKELY (consumed > 0)) {
1795
2.35k
    gssize left = samples_avail - consumed;
1796
2.35k
    if (left > 0) {
1797
      /* we consumed part of our samples */
1798
2.20k
      resampler->samples_avail = left;
1799
2.20k
    } else {
1800
      /* we consumed all our samples, empty our buffers */
1801
150
      resampler->samples_avail = 0;
1802
150
      resampler->skip = -left;
1803
150
    }
1804
2.35k
  }
1805
2.47k
}