2 * Copyright (C) 1999 Erik Walthinsen <omega@cse.ogi.edu>
3 * Copyright (C) 2003,2004 David A. Schleef <ds@schleef.org>
4 * Copyright (C) 2007-2008 Sebastian Dröge <sebastian.droege@collabora.co.uk>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
23 * SECTION:element-audioresample
25 * audioresample resamples raw audio buffers to different sample rates using
26 * a configurable windowing function to enhance quality.
28 * By default, the resampler uses a reduced sinc table, with cubic interpolation filling in
29 * the gaps. This ensures that the table does not become too big. However, the interpolation
30 * increases the CPU usage considerably. As an alternative, a full sinc table can be used.
31 * Doing so can drastically reduce CPU usage (4x faster with 44.1 -> 48 kHz conversions for
32 * example), at the cost of increased memory consumption, plus the sinc table takes longer
33 * to initialize when the element is created. A third mode exists, which uses the full table
34 * unless said table would become too large, in which case the interpolated one is used instead.
37 * <title>Example launch line</title>
39 * gst-launch -v filesrc location=sine.ogg ! oggdemux ! vorbisdec ! audioconvert ! audioresample ! audio/x-raw, rate=8000 ! alsasink
40 * ]| Decode an Ogg/Vorbis downsample to 8Khz and play sound through alsa.
41 * To create the Ogg/Vorbis file refer to the documentation of vorbisenc.
46 * - Enable SSE/ARM optimizations and select at runtime
56 #include "gstaudioresample.h"
57 #include <gst/gstutils.h>
58 #include <gst/audio/audio.h>
59 #include <gst/base/gstbasetransform.h>
63 #include <orc-test/orctest.h>
64 #include <orc-test/orcprofile.h>
67 GST_DEBUG_CATEGORY (audio_resample_debug);
68 #define GST_CAT_DEFAULT audio_resample_debug
69 #if !defined(AUDIORESAMPLE_FORMAT_AUTO) || defined(DISABLE_ORC)
70 GST_DEBUG_CATEGORY_STATIC (GST_CAT_PERFORMANCE);
73 #define GST_TYPE_SPEEX_RESAMPLER_SINC_FILTER_MODE (speex_resampler_sinc_filter_mode_get_type ())
79 PROP_SINC_FILTER_MODE,
80 PROP_SINC_FILTER_AUTO_THRESHOLD
83 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
84 #define SUPPORTED_CAPS \
85 GST_AUDIO_CAPS_MAKE ("{ F32LE, F64LE, S32LE, S24LE, S16LE, S8 }") \
86 ", layout = (string) { interleaved, non-interleaved }"
88 #define SUPPORTED_CAPS \
89 GST_AUDIO_CAPS_MAKE ("{ F32BE, F64BE, S32BE, S24BE, S16BE, S8 }") \
90 ", layout = (string) { interleaved, non-interleaved }"
93 /* If TRUE integer arithmetic resampling is faster and will be used if appropriate */
94 #if defined AUDIORESAMPLE_FORMAT_INT
95 static gboolean gst_audio_resample_use_int = TRUE;
96 #elif defined AUDIORESAMPLE_FORMAT_FLOAT
97 static gboolean gst_audio_resample_use_int = FALSE;
99 static gboolean gst_audio_resample_use_int = FALSE;
102 static GstStaticPadTemplate gst_audio_resample_sink_template =
103 GST_STATIC_PAD_TEMPLATE ("sink",
106 GST_STATIC_CAPS (SUPPORTED_CAPS));
108 static GstStaticPadTemplate gst_audio_resample_src_template =
109 GST_STATIC_PAD_TEMPLATE ("src",
112 GST_STATIC_CAPS (SUPPORTED_CAPS));
114 static void gst_audio_resample_set_property (GObject * object,
115 guint prop_id, const GValue * value, GParamSpec * pspec);
116 static void gst_audio_resample_get_property (GObject * object,
117 guint prop_id, GValue * value, GParamSpec * pspec);
120 speex_resampler_sinc_filter_mode_get_type (void);
123 static gboolean gst_audio_resample_get_unit_size (GstBaseTransform * base,
124 GstCaps * caps, gsize * size);
125 static GstCaps *gst_audio_resample_transform_caps (GstBaseTransform * base,
126 GstPadDirection direction, GstCaps * caps, GstCaps * filter);
127 static GstCaps *gst_audio_resample_fixate_caps (GstBaseTransform * base,
128 GstPadDirection direction, GstCaps * caps, GstCaps * othercaps);
129 static gboolean gst_audio_resample_transform_size (GstBaseTransform * trans,
130 GstPadDirection direction, GstCaps * incaps, gsize insize,
131 GstCaps * outcaps, gsize * outsize);
132 static gboolean gst_audio_resample_set_caps (GstBaseTransform * base,
133 GstCaps * incaps, GstCaps * outcaps);
134 static GstFlowReturn gst_audio_resample_transform (GstBaseTransform * base,
135 GstBuffer * inbuf, GstBuffer * outbuf);
136 static gboolean gst_audio_resample_sink_event (GstBaseTransform * base,
138 static gboolean gst_audio_resample_start (GstBaseTransform * base);
139 static gboolean gst_audio_resample_stop (GstBaseTransform * base);
140 static gboolean gst_audio_resample_query (GstPad * pad, GstObject * parent,
143 #define gst_audio_resample_parent_class parent_class
144 G_DEFINE_TYPE (GstAudioResample, gst_audio_resample, GST_TYPE_BASE_TRANSFORM);
147 gst_audio_resample_class_init (GstAudioResampleClass * klass)
149 GObjectClass *gobject_class = (GObjectClass *) klass;
150 GstElementClass *gstelement_class = (GstElementClass *) klass;
152 gobject_class->set_property = gst_audio_resample_set_property;
153 gobject_class->get_property = gst_audio_resample_get_property;
155 g_object_class_install_property (gobject_class, PROP_QUALITY,
156 g_param_spec_int ("quality", "Quality", "Resample quality with 0 being "
157 "the lowest and 10 being the best",
158 SPEEX_RESAMPLER_QUALITY_MIN, SPEEX_RESAMPLER_QUALITY_MAX,
159 SPEEX_RESAMPLER_QUALITY_DEFAULT,
160 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
162 g_object_class_install_property (gobject_class, PROP_SINC_FILTER_MODE,
163 g_param_spec_enum ("sinc-filter-mode", "Sinc filter table mode",
164 "What sinc filter table mode to use",
165 GST_TYPE_SPEEX_RESAMPLER_SINC_FILTER_MODE,
166 SPEEX_RESAMPLER_SINC_FILTER_DEFAULT,
167 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
169 g_object_class_install_property (gobject_class, PROP_SINC_FILTER_AUTO_THRESHOLD,
170 g_param_spec_uint ("sinc-filter-auto-threshold", "Sinc filter auto mode threshold",
171 "Memory usage threshold to use if sinc filter mode is AUTO, given in bytes",
173 SPEEX_RESAMPLER_SINC_FILTER_AUTO_THRESHOLD_DEFAULT,
174 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
176 gst_element_class_add_pad_template (gstelement_class,
177 gst_static_pad_template_get (&gst_audio_resample_src_template));
178 gst_element_class_add_pad_template (gstelement_class,
179 gst_static_pad_template_get (&gst_audio_resample_sink_template));
181 gst_element_class_set_static_metadata (gstelement_class, "Audio resampler",
182 "Filter/Converter/Audio", "Resamples audio",
183 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
185 GST_BASE_TRANSFORM_CLASS (klass)->start =
186 GST_DEBUG_FUNCPTR (gst_audio_resample_start);
187 GST_BASE_TRANSFORM_CLASS (klass)->stop =
188 GST_DEBUG_FUNCPTR (gst_audio_resample_stop);
189 GST_BASE_TRANSFORM_CLASS (klass)->transform_size =
190 GST_DEBUG_FUNCPTR (gst_audio_resample_transform_size);
191 GST_BASE_TRANSFORM_CLASS (klass)->get_unit_size =
192 GST_DEBUG_FUNCPTR (gst_audio_resample_get_unit_size);
193 GST_BASE_TRANSFORM_CLASS (klass)->transform_caps =
194 GST_DEBUG_FUNCPTR (gst_audio_resample_transform_caps);
195 GST_BASE_TRANSFORM_CLASS (klass)->fixate_caps =
196 GST_DEBUG_FUNCPTR (gst_audio_resample_fixate_caps);
197 GST_BASE_TRANSFORM_CLASS (klass)->set_caps =
198 GST_DEBUG_FUNCPTR (gst_audio_resample_set_caps);
199 GST_BASE_TRANSFORM_CLASS (klass)->transform =
200 GST_DEBUG_FUNCPTR (gst_audio_resample_transform);
201 GST_BASE_TRANSFORM_CLASS (klass)->sink_event =
202 GST_DEBUG_FUNCPTR (gst_audio_resample_sink_event);
204 GST_BASE_TRANSFORM_CLASS (klass)->passthrough_on_same_caps = TRUE;
208 gst_audio_resample_init (GstAudioResample * resample)
210 GstBaseTransform *trans = GST_BASE_TRANSFORM (resample);
212 resample->quality = SPEEX_RESAMPLER_QUALITY_DEFAULT;
213 resample->sinc_filter_mode = SPEEX_RESAMPLER_SINC_FILTER_DEFAULT;
214 resample->sinc_filter_auto_threshold = SPEEX_RESAMPLER_SINC_FILTER_AUTO_THRESHOLD_DEFAULT;
216 gst_base_transform_set_gap_aware (trans, TRUE);
217 gst_pad_set_query_function (trans->srcpad, gst_audio_resample_query);
222 gst_audio_resample_start (GstBaseTransform * base)
224 GstAudioResample *resample = GST_AUDIO_RESAMPLE (base);
226 resample->need_discont = TRUE;
228 resample->num_gap_samples = 0;
229 resample->num_nongap_samples = 0;
230 resample->t0 = GST_CLOCK_TIME_NONE;
231 resample->in_offset0 = GST_BUFFER_OFFSET_NONE;
232 resample->out_offset0 = GST_BUFFER_OFFSET_NONE;
233 resample->samples_in = 0;
234 resample->samples_out = 0;
236 resample->tmp_in = NULL;
237 resample->tmp_in_size = 0;
238 resample->tmp_out = NULL;
239 resample->tmp_out_size = 0;
245 gst_audio_resample_stop (GstBaseTransform * base)
247 GstAudioResample *resample = GST_AUDIO_RESAMPLE (base);
249 if (resample->state) {
250 resample->funcs->destroy (resample->state);
251 resample->state = NULL;
254 resample->funcs = NULL;
256 g_free (resample->tmp_in);
257 resample->tmp_in = NULL;
258 resample->tmp_in_size = 0;
260 g_free (resample->tmp_out);
261 resample->tmp_out = NULL;
262 resample->tmp_out_size = 0;
268 gst_audio_resample_get_unit_size (GstBaseTransform * base, GstCaps * caps,
273 if (!gst_audio_info_from_caps (&info, caps))
276 *size = GST_AUDIO_INFO_BPF (&info);
283 GST_ERROR_OBJECT (base, "invalid caps");
289 gst_audio_resample_transform_caps (GstBaseTransform * base,
290 GstPadDirection direction, GstCaps * caps, GstCaps * filter)
297 /* transform single caps into input_caps + input_caps with the rate
298 * field set to our supported range. This ensures that upstream knows
299 * about downstream's prefered rate(s) and can negotiate accordingly. */
300 res = gst_caps_new_empty ();
301 n = gst_caps_get_size (caps);
302 for (i = 0; i < n; i++) {
303 s = gst_caps_get_structure (caps, i);
305 /* If this is already expressed by the existing caps
306 * skip this structure */
307 if (i > 0 && gst_caps_is_subset_structure (res, s))
310 /* first, however, check if the caps contain a range for the rate field, in
311 * which case that side isn't going to care much about the exact sample rate
312 * chosen and we should just assume things will get fixated to something sane
313 * and we may just as well offer our full range instead of the range in the
314 * caps. If the rate is not an int range value, it's likely to express a
315 * real preference or limitation and we should maintain that structure as
316 * preference by putting it first into the transformed caps, and only add
317 * our full rate range as second option */
318 s = gst_structure_copy (s);
319 val = gst_structure_get_value (s, "rate");
320 if (val == NULL || GST_VALUE_HOLDS_INT_RANGE (val)) {
321 /* overwrite existing range, or add field if it doesn't exist yet */
322 gst_structure_set (s, "rate", GST_TYPE_INT_RANGE, 1, G_MAXINT, NULL);
324 /* append caps with full range to existing caps with non-range rate field */
325 gst_caps_append_structure (res, gst_structure_copy (s));
326 gst_structure_set (s, "rate", GST_TYPE_INT_RANGE, 1, G_MAXINT, NULL);
328 gst_caps_append_structure (res, s);
332 GstCaps *intersection;
335 gst_caps_intersect_full (filter, res, GST_CAPS_INTERSECT_FIRST);
336 gst_caps_unref (res);
343 /* Fixate rate to the allowed rate that has the smallest difference */
345 gst_audio_resample_fixate_caps (GstBaseTransform * base,
346 GstPadDirection direction, GstCaps * caps, GstCaps * othercaps)
351 s = gst_caps_get_structure (caps, 0);
352 if (G_UNLIKELY (!gst_structure_get_int (s, "rate", &rate)))
355 othercaps = gst_caps_truncate (othercaps);
356 othercaps = gst_caps_make_writable (othercaps);
357 s = gst_caps_get_structure (othercaps, 0);
358 gst_structure_fixate_field_nearest_int (s, "rate", rate);
363 static const SpeexResampleFuncs *
364 gst_audio_resample_get_funcs (gint width, gboolean fp)
366 const SpeexResampleFuncs *funcs = NULL;
368 if (gst_audio_resample_use_int && (width == 8 || width == 16) && !fp)
370 else if ((!gst_audio_resample_use_int && (width == 8 || width == 16) && !fp)
371 || (width == 32 && fp))
372 funcs = &float_funcs;
373 else if ((width == 64 && fp) || ((width == 32 || width == 24) && !fp))
374 funcs = &double_funcs;
376 g_assert_not_reached ();
381 static SpeexResamplerState *
382 gst_audio_resample_init_state (GstAudioResample * resample, gint width,
383 gint channels, gint inrate, gint outrate, gint quality, gboolean fp,
384 SpeexResamplerSincFilterMode sinc_filter_mode,
385 guint32 sinc_filter_auto_threshold)
387 SpeexResamplerState *ret = NULL;
388 gint err = RESAMPLER_ERR_SUCCESS;
389 const SpeexResampleFuncs *funcs = gst_audio_resample_get_funcs (width, fp);
391 ret = funcs->init (channels, inrate, outrate, quality,
392 sinc_filter_mode, sinc_filter_auto_threshold, &err);
394 if (G_UNLIKELY (err != RESAMPLER_ERR_SUCCESS)) {
395 GST_ERROR_OBJECT (resample, "Failed to create resampler state: %s",
396 funcs->strerror (err));
400 if (sinc_filter_mode == SPEEX_RESAMPLER_SINC_FILTER_AUTO) {
401 GST_INFO_OBJECT (resample, "Using the %s sinc filter table",
402 funcs->get_sinc_filter_mode(ret) ? "full" : "interpolated");
405 funcs->skip_zeros (ret);
411 gst_audio_resample_update_state (GstAudioResample * resample, gint width,
412 gint channels, gint inrate, gint outrate, gint quality, gboolean fp,
413 SpeexResamplerSincFilterMode sinc_filter_mode,
414 guint32 sinc_filter_auto_threshold)
417 gboolean updated_latency = FALSE;
419 updated_latency = (resample->inrate != inrate
420 || quality != resample->quality) && resample->state != NULL;
422 if (resample->state == NULL) {
424 } else if (resample->channels != channels || fp != resample->fp
425 || width != resample->width || sinc_filter_mode != resample->sinc_filter_mode
426 || sinc_filter_auto_threshold != resample->sinc_filter_auto_threshold) {
427 resample->funcs->destroy (resample->state);
429 gst_audio_resample_init_state (resample, width, channels, inrate,
430 outrate, quality, fp, sinc_filter_mode, sinc_filter_auto_threshold);
432 resample->funcs = gst_audio_resample_get_funcs (width, fp);
433 ret = (resample->state != NULL);
434 } else if (resample->inrate != inrate || resample->outrate != outrate) {
435 gint err = RESAMPLER_ERR_SUCCESS;
437 err = resample->funcs->set_rate (resample->state, inrate, outrate);
439 if (G_UNLIKELY (err != RESAMPLER_ERR_SUCCESS))
440 GST_ERROR_OBJECT (resample, "Failed to update rate: %s",
441 resample->funcs->strerror (err));
443 ret = (err == RESAMPLER_ERR_SUCCESS);
444 } else if (quality != resample->quality) {
445 gint err = RESAMPLER_ERR_SUCCESS;
447 err = resample->funcs->set_quality (resample->state, quality);
449 if (G_UNLIKELY (err != RESAMPLER_ERR_SUCCESS))
450 GST_ERROR_OBJECT (resample, "Failed to update quality: %s",
451 resample->funcs->strerror (err));
453 ret = (err == RESAMPLER_ERR_SUCCESS);
456 resample->width = width;
457 resample->channels = channels;
459 resample->quality = quality;
460 resample->inrate = inrate;
461 resample->outrate = outrate;
462 resample->sinc_filter_mode = sinc_filter_mode;
463 resample->sinc_filter_auto_threshold = sinc_filter_auto_threshold;
466 gst_element_post_message (GST_ELEMENT (resample),
467 gst_message_new_latency (GST_OBJECT (resample)));
473 gst_audio_resample_reset_state (GstAudioResample * resample)
476 resample->funcs->reset_mem (resample->state);
480 _gcd (gint a, gint b)
493 gst_audio_resample_transform_size (GstBaseTransform * base,
494 GstPadDirection direction, GstCaps * caps, gsize size, GstCaps * othercaps,
498 GstAudioInfo in, out;
499 guint32 ratio_den, ratio_num;
500 gint inrate, outrate, gcd;
503 GST_LOG_OBJECT (base, "asked to transform size %" G_GSIZE_FORMAT
504 " in direction %s", size, direction == GST_PAD_SINK ? "SINK" : "SRC");
506 /* Get sample width -> bytes_per_samp, channels, inrate, outrate */
507 ret = gst_audio_info_from_caps (&in, caps);
508 ret &= gst_audio_info_from_caps (&out, othercaps);
509 if (G_UNLIKELY (!ret)) {
510 GST_ERROR_OBJECT (base, "Wrong caps");
513 /* Number of samples in either buffer is size / (width*channels) ->
514 * calculate the factor */
515 bpf = GST_AUDIO_INFO_BPF (&in);
516 inrate = GST_AUDIO_INFO_RATE (&in);
517 outrate = GST_AUDIO_INFO_RATE (&out);
519 /* Convert source buffer size to samples */
522 /* Simplify the conversion ratio factors */
523 gcd = _gcd (inrate, outrate);
524 ratio_num = inrate / gcd;
525 ratio_den = outrate / gcd;
527 if (direction == GST_PAD_SINK) {
528 /* asked to convert size of an incoming buffer. Round up the output size */
529 *othersize = gst_util_uint64_scale_int_ceil (size, ratio_den, ratio_num);
532 /* asked to convert size of an outgoing buffer. Round down the input size */
533 *othersize = gst_util_uint64_scale_int (size, ratio_num, ratio_den);
537 GST_LOG_OBJECT (base,
538 "transformed size %" G_GSIZE_FORMAT " to %" G_GSIZE_FORMAT,
539 size * bpf, *othersize);
545 gst_audio_resample_set_caps (GstBaseTransform * base, GstCaps * incaps,
549 gint width, inrate, outrate, channels;
551 GstAudioResample *resample = GST_AUDIO_RESAMPLE (base);
552 GstAudioInfo in, out;
554 GST_LOG ("incaps %" GST_PTR_FORMAT ", outcaps %"
555 GST_PTR_FORMAT, incaps, outcaps);
557 if (!gst_audio_info_from_caps (&in, incaps))
559 if (!gst_audio_info_from_caps (&out, outcaps))
560 goto invalid_outcaps;
562 /* FIXME do some checks */
564 /* take new values */
565 width = GST_AUDIO_FORMAT_INFO_WIDTH (in.finfo);
566 channels = GST_AUDIO_INFO_CHANNELS (&in);
567 inrate = GST_AUDIO_INFO_RATE (&in);
568 outrate = GST_AUDIO_INFO_RATE (&out);
569 fp = GST_AUDIO_FORMAT_INFO_IS_FLOAT (in.finfo);
572 gst_audio_resample_update_state (resample, width, channels, inrate,
573 outrate, resample->quality, fp, resample->sinc_filter_mode,
574 resample->sinc_filter_auto_threshold);
576 if (G_UNLIKELY (!ret))
584 GST_ERROR_OBJECT (base, "invalid incaps");
589 GST_ERROR_OBJECT (base, "invalid outcaps");
594 #define GST_MAXINT24 (8388607)
595 #define GST_MININT24 (-8388608)
597 #if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
598 #define GST_READ_UINT24 GST_READ_UINT24_LE
599 #define GST_WRITE_UINT24 GST_WRITE_UINT24_LE
601 #define GST_READ_UINT24 GST_READ_UINT24_BE
602 #define GST_WRITE_UINT24 GST_WRITE_UINT24_BE
606 gst_audio_resample_convert_buffer (GstAudioResample * resample,
607 const guint8 * in, guint8 * out, guint len, gboolean inverse)
609 len *= resample->channels;
612 if (gst_audio_resample_use_int && resample->width == 8 && !resample->fp) {
613 gint8 *o = (gint8 *) out;
614 gint16 *i = (gint16 *) in;
618 tmp = *i + (G_MAXINT8 >> 1);
619 *o = CLAMP (tmp >> 8, G_MININT8, G_MAXINT8);
624 } else if (!gst_audio_resample_use_int && resample->width == 8
626 gint8 *o = (gint8 *) out;
627 gfloat *i = (gfloat *) in;
632 *o = (gint8) CLAMP (tmp * G_MAXINT8 + 0.5, G_MININT8, G_MAXINT8);
637 } else if (!gst_audio_resample_use_int && resample->width == 16
639 gint16 *o = (gint16 *) out;
640 gfloat *i = (gfloat *) in;
645 *o = (gint16) CLAMP (tmp * G_MAXINT16 + 0.5, G_MININT16, G_MAXINT16);
650 } else if (resample->width == 24 && !resample->fp) {
651 guint8 *o = (guint8 *) out;
652 gdouble *i = (gdouble *) in;
657 GST_WRITE_UINT24 (o, (gint32) CLAMP (tmp * GST_MAXINT24 + 0.5,
658 GST_MININT24, GST_MAXINT24));
663 } else if (resample->width == 32 && !resample->fp) {
664 gint32 *o = (gint32 *) out;
665 gdouble *i = (gdouble *) in;
670 *o = (gint32) CLAMP (tmp * G_MAXINT32 + 0.5, G_MININT32, G_MAXINT32);
676 g_assert_not_reached ();
679 if (gst_audio_resample_use_int && resample->width == 8 && !resample->fp) {
680 gint8 *i = (gint8 *) in;
681 gint16 *o = (gint16 *) out;
691 } else if (!gst_audio_resample_use_int && resample->width == 8
693 gint8 *i = (gint8 *) in;
694 gfloat *o = (gfloat *) out;
699 *o = tmp / G_MAXINT8;
704 } else if (!gst_audio_resample_use_int && resample->width == 16
706 gint16 *i = (gint16 *) in;
707 gfloat *o = (gfloat *) out;
712 *o = tmp / G_MAXINT16;
717 } else if (resample->width == 24 && !resample->fp) {
718 guint8 *i = (guint8 *) in;
719 gdouble *o = (gdouble *) out;
724 tmp2 = GST_READ_UINT24 (i);
725 if (tmp2 & 0x00800000)
728 *o = tmp / GST_MAXINT24;
733 } else if (resample->width == 32 && !resample->fp) {
734 gint32 *i = (gint32 *) in;
735 gdouble *o = (gdouble *) out;
740 *o = tmp / G_MAXINT32;
746 g_assert_not_reached ();
752 gst_audio_resample_workspace_realloc (guint8 ** workspace, guint * size,
756 if (new_size <= *size)
757 /* no need to resize */
759 new = g_realloc (*workspace, new_size);
761 /* failure (re)allocating memeory */
769 /* Push history_len zeros into the filter, but discard the output. */
771 gst_audio_resample_dump_drain (GstAudioResample * resample, guint history_len)
774 guint in_len G_GNUC_UNUSED, in_processed;
775 guint out_len, out_processed;
779 g_assert (resample->state != NULL);
781 resample->funcs->get_ratio (resample->state, &num, &den);
783 in_len = in_processed = history_len;
784 out_processed = out_len =
785 gst_util_uint64_scale_int_ceil (history_len, den, num);
786 outsize = out_len * resample->channels * (resample->funcs->width / 8);
791 buf = g_malloc (outsize);
792 resample->funcs->process (resample->state, NULL, &in_processed, buf,
796 g_assert (in_len == in_processed);
800 gst_audio_resample_push_drain (GstAudioResample * resample, guint history_len)
805 guint in_len, in_processed;
806 guint out_len, out_processed;
811 g_assert (resample->state != NULL);
813 /* Don't drain samples if we were reset. */
814 if (!GST_CLOCK_TIME_IS_VALID (resample->t0))
817 resample->funcs->get_ratio (resample->state, &num, &den);
819 in_len = in_processed = history_len;
820 out_len = out_processed =
821 gst_util_uint64_scale_int_ceil (history_len, den, num);
822 outsize = out_len * resample->channels * (resample->width / 8);
827 outbuf = gst_buffer_new_and_alloc (outsize);
829 gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
831 if (resample->funcs->width != resample->width) {
832 /* need to convert data format; allocate workspace */
833 if (!gst_audio_resample_workspace_realloc (&resample->tmp_out,
834 &resample->tmp_out_size, (resample->funcs->width / 8) * out_len *
835 resample->channels)) {
836 GST_ERROR_OBJECT (resample, "failed to allocate workspace");
841 err = resample->funcs->process (resample->state, NULL, &in_processed,
842 resample->tmp_out, &out_processed);
844 /* convert output format */
845 gst_audio_resample_convert_buffer (resample, resample->tmp_out,
846 map.data, out_processed, TRUE);
848 /* don't need to convert data format; process */
849 err = resample->funcs->process (resample->state, NULL, &in_processed,
850 map.data, &out_processed);
853 /* If we wrote more than allocated something is really wrong now
854 * and we should better abort immediately */
855 g_assert (out_len >= out_processed);
857 outsize = out_processed * resample->channels * (resample->width / 8);
858 gst_buffer_unmap (outbuf, &map);
859 gst_buffer_resize (outbuf, 0, outsize);
861 if (G_UNLIKELY (err != RESAMPLER_ERR_SUCCESS)) {
862 GST_WARNING_OBJECT (resample, "Failed to process drain: %s",
863 resample->funcs->strerror (err));
864 gst_buffer_unref (outbuf);
869 if (GST_CLOCK_TIME_IS_VALID (resample->t0)) {
870 GST_BUFFER_TIMESTAMP (outbuf) = resample->t0 +
871 gst_util_uint64_scale_int_round (resample->samples_out, GST_SECOND,
873 GST_BUFFER_DURATION (outbuf) = resample->t0 +
874 gst_util_uint64_scale_int_round (resample->samples_out + out_processed,
875 GST_SECOND, resample->outrate) - GST_BUFFER_TIMESTAMP (outbuf);
877 GST_BUFFER_TIMESTAMP (outbuf) = GST_CLOCK_TIME_NONE;
878 GST_BUFFER_DURATION (outbuf) = GST_CLOCK_TIME_NONE;
881 if (resample->out_offset0 != GST_BUFFER_OFFSET_NONE) {
882 GST_BUFFER_OFFSET (outbuf) = resample->out_offset0 + resample->samples_out;
883 GST_BUFFER_OFFSET_END (outbuf) = GST_BUFFER_OFFSET (outbuf) + out_processed;
885 GST_BUFFER_OFFSET (outbuf) = GST_BUFFER_OFFSET_NONE;
886 GST_BUFFER_OFFSET_END (outbuf) = GST_BUFFER_OFFSET_NONE;
889 resample->samples_out += out_processed;
890 resample->samples_in += history_len;
892 if (G_UNLIKELY (out_processed == 0 && in_len * den > num)) {
893 GST_WARNING_OBJECT (resample, "Failed to get drain, dropping buffer");
894 gst_buffer_unref (outbuf);
898 GST_LOG_OBJECT (resample,
899 "Pushing drain buffer of %u bytes with timestamp %" GST_TIME_FORMAT
900 " duration %" GST_TIME_FORMAT " offset %" G_GUINT64_FORMAT " offset_end %"
901 G_GUINT64_FORMAT, outsize,
902 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)),
903 GST_TIME_ARGS (GST_BUFFER_DURATION (outbuf)), GST_BUFFER_OFFSET (outbuf),
904 GST_BUFFER_OFFSET_END (outbuf));
906 res = gst_pad_push (GST_BASE_TRANSFORM_SRC_PAD (resample), outbuf);
908 if (G_UNLIKELY (res != GST_FLOW_OK))
909 GST_WARNING_OBJECT (resample, "Failed to push drain: %s",
910 gst_flow_get_name (res));
916 gst_audio_resample_sink_event (GstBaseTransform * base, GstEvent * event)
918 GstAudioResample *resample = GST_AUDIO_RESAMPLE (base);
920 switch (GST_EVENT_TYPE (event)) {
921 case GST_EVENT_FLUSH_STOP:
922 gst_audio_resample_reset_state (resample);
924 resample->funcs->skip_zeros (resample->state);
925 resample->num_gap_samples = 0;
926 resample->num_nongap_samples = 0;
927 resample->t0 = GST_CLOCK_TIME_NONE;
928 resample->in_offset0 = GST_BUFFER_OFFSET_NONE;
929 resample->out_offset0 = GST_BUFFER_OFFSET_NONE;
930 resample->samples_in = 0;
931 resample->samples_out = 0;
932 resample->need_discont = TRUE;
934 case GST_EVENT_SEGMENT:
935 if (resample->state) {
936 guint latency = resample->funcs->get_input_latency (resample->state);
937 gst_audio_resample_push_drain (resample, latency);
939 gst_audio_resample_reset_state (resample);
941 resample->funcs->skip_zeros (resample->state);
942 resample->num_gap_samples = 0;
943 resample->num_nongap_samples = 0;
944 resample->t0 = GST_CLOCK_TIME_NONE;
945 resample->in_offset0 = GST_BUFFER_OFFSET_NONE;
946 resample->out_offset0 = GST_BUFFER_OFFSET_NONE;
947 resample->samples_in = 0;
948 resample->samples_out = 0;
949 resample->need_discont = TRUE;
952 if (resample->state) {
953 guint latency = resample->funcs->get_input_latency (resample->state);
954 gst_audio_resample_push_drain (resample, latency);
956 gst_audio_resample_reset_state (resample);
962 return GST_BASE_TRANSFORM_CLASS (parent_class)->sink_event (base, event);
966 gst_audio_resample_check_discont (GstAudioResample * resample, GstBuffer * buf)
971 /* is the incoming buffer a discontinuity? */
972 if (G_UNLIKELY (GST_BUFFER_IS_DISCONT (buf)))
975 /* no valid timestamps or offsets to compare --> no discontinuity */
976 if (G_UNLIKELY (!(GST_BUFFER_TIMESTAMP_IS_VALID (buf) &&
977 GST_CLOCK_TIME_IS_VALID (resample->t0))))
980 /* convert the inbound timestamp to an offset. */
982 gst_util_uint64_scale_int_round (GST_BUFFER_TIMESTAMP (buf) -
983 resample->t0, resample->inrate, GST_SECOND);
985 /* many elements generate imperfect streams due to rounding errors, so we
986 * permit a small error (up to one sample) without triggering a filter
987 * flush/restart (if triggered incorrectly, this will be audible) */
988 /* allow even up to more samples, since sink is not so strict anyway,
989 * so give that one a chance to handle this as configured */
990 delta = ABS ((gint64) (offset - resample->samples_in));
991 if (delta <= (resample->inrate >> 5))
994 GST_WARNING_OBJECT (resample,
995 "encountered timestamp discontinuity of %" G_GUINT64_FORMAT " samples = %"
996 GST_TIME_FORMAT, delta,
997 GST_TIME_ARGS (gst_util_uint64_scale_int_round (delta, GST_SECOND,
1002 static GstFlowReturn
1003 gst_audio_resample_process (GstAudioResample * resample, GstBuffer * inbuf,
1006 GstMapInfo in_map, out_map;
1008 guint32 in_len, in_processed;
1009 guint32 out_len, out_processed;
1010 guint filt_len = resample->funcs->get_filt_len (resample->state);
1012 gst_buffer_map (inbuf, &in_map, GST_MAP_READ);
1013 gst_buffer_map (outbuf, &out_map, GST_MAP_WRITE);
1015 in_len = in_map.size / resample->channels;
1016 out_len = out_map.size / resample->channels;
1018 in_len /= (resample->width / 8);
1019 out_len /= (resample->width / 8);
1021 in_processed = in_len;
1022 out_processed = out_len;
1024 if (GST_BUFFER_FLAG_IS_SET (inbuf, GST_BUFFER_FLAG_GAP)) {
1025 resample->num_nongap_samples = 0;
1026 if (resample->num_gap_samples < filt_len) {
1027 guint zeros_to_push;
1028 if (in_len >= filt_len - resample->num_gap_samples)
1029 zeros_to_push = filt_len - resample->num_gap_samples;
1031 zeros_to_push = in_len;
1033 gst_audio_resample_push_drain (resample, zeros_to_push);
1034 in_len -= zeros_to_push;
1035 resample->num_gap_samples += zeros_to_push;
1040 resample->funcs->get_ratio (resample->state, &num, &den);
1041 if (resample->samples_in + in_len >= filt_len / 2)
1043 gst_util_uint64_scale_int_ceil (resample->samples_in + in_len -
1044 filt_len / 2, den, num) - resample->samples_out;
1048 memset (out_map.data, 0, out_map.size);
1049 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_GAP);
1050 resample->num_gap_samples += in_len;
1051 in_processed = in_len;
1053 } else { /* not a gap */
1057 if (resample->num_gap_samples > filt_len) {
1058 /* push in enough zeros to restore the filter to the right offset */
1060 resample->funcs->get_ratio (resample->state, &num, &den);
1061 gst_audio_resample_dump_drain (resample,
1062 (resample->num_gap_samples - filt_len) % num);
1064 resample->num_gap_samples = 0;
1065 if (resample->num_nongap_samples < filt_len) {
1066 resample->num_nongap_samples += in_len;
1067 if (resample->num_nongap_samples > filt_len)
1068 resample->num_nongap_samples = filt_len;
1071 if (resample->funcs->width != resample->width) {
1072 /* need to convert data format for processing; ensure we have enough
1073 * workspace available */
1074 if (!gst_audio_resample_workspace_realloc (&resample->tmp_in,
1075 &resample->tmp_in_size, in_len * resample->channels *
1076 (resample->funcs->width / 8)) ||
1077 !gst_audio_resample_workspace_realloc (&resample->tmp_out,
1078 &resample->tmp_out_size, out_len * resample->channels *
1079 (resample->funcs->width / 8))) {
1080 GST_ERROR_OBJECT (resample, "failed to allocate workspace");
1081 gst_buffer_unmap (inbuf, &in_map);
1082 gst_buffer_unmap (outbuf, &out_map);
1083 return GST_FLOW_ERROR;
1087 gst_audio_resample_convert_buffer (resample, in_map.data,
1088 resample->tmp_in, in_len, FALSE);
1091 err = resample->funcs->process (resample->state,
1092 resample->tmp_in, &in_processed, resample->tmp_out, &out_processed);
1094 /* convert output */
1095 gst_audio_resample_convert_buffer (resample, resample->tmp_out,
1096 out_map.data, out_processed, TRUE);
1098 /* no format conversion required; process */
1099 err = resample->funcs->process (resample->state,
1100 in_map.data, &in_processed, out_map.data, &out_processed);
1103 if (G_UNLIKELY (err != RESAMPLER_ERR_SUCCESS)) {
1104 GST_ERROR_OBJECT (resample, "Failed to convert data: %s",
1105 resample->funcs->strerror (err));
1106 gst_buffer_unmap (inbuf, &in_map);
1107 gst_buffer_unmap (outbuf, &out_map);
1108 return GST_FLOW_ERROR;
1112 /* If we wrote more than allocated something is really wrong now and we
1113 * should better abort immediately */
1114 g_assert (out_len >= out_processed);
1116 if (G_UNLIKELY (in_len != in_processed)) {
1117 GST_WARNING_OBJECT (resample, "converted %d of %d input samples",
1118 in_processed, in_len);
1122 if (GST_CLOCK_TIME_IS_VALID (resample->t0)) {
1123 GST_BUFFER_TIMESTAMP (outbuf) = resample->t0 +
1124 gst_util_uint64_scale_int_round (resample->samples_out, GST_SECOND,
1126 GST_BUFFER_DURATION (outbuf) = resample->t0 +
1127 gst_util_uint64_scale_int_round (resample->samples_out + out_processed,
1128 GST_SECOND, resample->outrate) - GST_BUFFER_TIMESTAMP (outbuf);
1130 GST_BUFFER_TIMESTAMP (outbuf) = GST_CLOCK_TIME_NONE;
1131 GST_BUFFER_DURATION (outbuf) = GST_CLOCK_TIME_NONE;
1134 if (resample->out_offset0 != GST_BUFFER_OFFSET_NONE) {
1135 GST_BUFFER_OFFSET (outbuf) = resample->out_offset0 + resample->samples_out;
1136 GST_BUFFER_OFFSET_END (outbuf) = GST_BUFFER_OFFSET (outbuf) + out_processed;
1138 GST_BUFFER_OFFSET (outbuf) = GST_BUFFER_OFFSET_NONE;
1139 GST_BUFFER_OFFSET_END (outbuf) = GST_BUFFER_OFFSET_NONE;
1142 resample->samples_out += out_processed;
1143 resample->samples_in += in_len;
1145 gst_buffer_unmap (inbuf, &in_map);
1146 gst_buffer_unmap (outbuf, &out_map);
1148 outsize = out_processed * resample->channels * (resample->width / 8);
1149 gst_buffer_resize (outbuf, 0, outsize);
1151 GST_LOG_OBJECT (resample,
1152 "Converted to buffer of %" G_GUINT32_FORMAT
1153 " samples (%" G_GSIZE_FORMAT " bytes) with timestamp %" GST_TIME_FORMAT
1154 ", duration %" GST_TIME_FORMAT ", offset %" G_GUINT64_FORMAT
1155 ", offset_end %" G_GUINT64_FORMAT, out_processed, outsize,
1156 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)),
1157 GST_TIME_ARGS (GST_BUFFER_DURATION (outbuf)),
1158 GST_BUFFER_OFFSET (outbuf), GST_BUFFER_OFFSET_END (outbuf));
1163 static GstFlowReturn
1164 gst_audio_resample_transform (GstBaseTransform * base, GstBuffer * inbuf,
1167 GstAudioResample *resample = GST_AUDIO_RESAMPLE (base);
1170 if (resample->state == NULL) {
1171 if (G_UNLIKELY (!(resample->state =
1172 gst_audio_resample_init_state (resample, resample->width,
1173 resample->channels, resample->inrate, resample->outrate,
1174 resample->quality, resample->fp, resample->sinc_filter_mode,
1175 resample->sinc_filter_auto_threshold))))
1176 return GST_FLOW_ERROR;
1179 gst_audio_resample_get_funcs (resample->width, resample->fp);
1182 GST_LOG_OBJECT (resample, "transforming buffer of %" G_GSIZE_FORMAT " bytes,"
1183 " ts %" GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT ", offset %"
1184 G_GINT64_FORMAT ", offset_end %" G_GINT64_FORMAT,
1185 gst_buffer_get_size (inbuf), GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (inbuf)),
1186 GST_TIME_ARGS (GST_BUFFER_DURATION (inbuf)),
1187 GST_BUFFER_OFFSET (inbuf), GST_BUFFER_OFFSET_END (inbuf));
1189 /* check for timestamp discontinuities; flush/reset if needed, and set
1190 * flag to resync timestamp and offset counters and send event
1192 if (G_UNLIKELY (gst_audio_resample_check_discont (resample, inbuf))) {
1193 gst_audio_resample_reset_state (resample);
1194 resample->need_discont = TRUE;
1197 /* handle discontinuity */
1198 if (G_UNLIKELY (resample->need_discont)) {
1199 resample->funcs->skip_zeros (resample->state);
1200 resample->num_gap_samples = 0;
1201 resample->num_nongap_samples = 0;
1203 resample->samples_in = 0;
1204 resample->samples_out = 0;
1205 GST_DEBUG_OBJECT (resample, "found discontinuity; resyncing");
1206 /* resync the timestamp and offset counters if possible */
1207 if (GST_BUFFER_TIMESTAMP_IS_VALID (inbuf)) {
1208 resample->t0 = GST_BUFFER_TIMESTAMP (inbuf);
1210 GST_DEBUG_OBJECT (resample, "... but new timestamp is invalid");
1211 resample->t0 = GST_CLOCK_TIME_NONE;
1213 if (GST_BUFFER_OFFSET_IS_VALID (inbuf)) {
1214 resample->in_offset0 = GST_BUFFER_OFFSET (inbuf);
1215 resample->out_offset0 =
1216 gst_util_uint64_scale_int_round (resample->in_offset0,
1217 resample->outrate, resample->inrate);
1219 GST_DEBUG_OBJECT (resample, "... but new offset is invalid");
1220 resample->in_offset0 = GST_BUFFER_OFFSET_NONE;
1221 resample->out_offset0 = GST_BUFFER_OFFSET_NONE;
1223 /* set DISCONT flag on output buffer */
1224 GST_DEBUG_OBJECT (resample, "marking this buffer with the DISCONT flag");
1225 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
1226 resample->need_discont = FALSE;
1229 ret = gst_audio_resample_process (resample, inbuf, outbuf);
1230 if (G_UNLIKELY (ret != GST_FLOW_OK))
1233 GST_DEBUG_OBJECT (resample, "input = samples [%" G_GUINT64_FORMAT ", %"
1234 G_GUINT64_FORMAT ") = [%" G_GUINT64_FORMAT ", %" G_GUINT64_FORMAT
1235 ") ns; output = samples [%" G_GUINT64_FORMAT ", %" G_GUINT64_FORMAT
1236 ") = [%" G_GUINT64_FORMAT ", %" G_GUINT64_FORMAT ") ns",
1237 GST_BUFFER_OFFSET (inbuf), GST_BUFFER_OFFSET_END (inbuf),
1238 GST_BUFFER_TIMESTAMP (inbuf), GST_BUFFER_TIMESTAMP (inbuf) +
1239 GST_BUFFER_DURATION (inbuf), GST_BUFFER_OFFSET (outbuf),
1240 GST_BUFFER_OFFSET_END (outbuf), GST_BUFFER_TIMESTAMP (outbuf),
1241 GST_BUFFER_TIMESTAMP (outbuf) + GST_BUFFER_DURATION (outbuf));
1247 gst_audio_resample_query (GstPad * pad, GstObject * parent, GstQuery * query)
1249 GstAudioResample *resample = GST_AUDIO_RESAMPLE (parent);
1250 GstBaseTransform *trans;
1251 gboolean res = TRUE;
1253 trans = GST_BASE_TRANSFORM (resample);
1255 switch (GST_QUERY_TYPE (query)) {
1256 case GST_QUERY_LATENCY:
1258 GstClockTime min, max;
1261 gint rate = resample->inrate;
1262 gint resampler_latency;
1264 if (resample->state)
1266 resample->funcs->get_input_latency (resample->state);
1268 resampler_latency = 0;
1270 if (gst_base_transform_is_passthrough (trans))
1271 resampler_latency = 0;
1274 gst_pad_peer_query (GST_BASE_TRANSFORM_SINK_PAD (trans),
1276 gst_query_parse_latency (query, &live, &min, &max);
1278 GST_DEBUG_OBJECT (resample, "Peer latency: min %"
1279 GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
1280 GST_TIME_ARGS (min), GST_TIME_ARGS (max));
1282 /* add our own latency */
1283 if (rate != 0 && resampler_latency != 0)
1284 latency = gst_util_uint64_scale_round (resampler_latency,
1289 GST_DEBUG_OBJECT (resample, "Our latency: %" GST_TIME_FORMAT,
1290 GST_TIME_ARGS (latency));
1293 if (GST_CLOCK_TIME_IS_VALID (max))
1296 GST_DEBUG_OBJECT (resample, "Calculated total latency : min %"
1297 GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
1298 GST_TIME_ARGS (min), GST_TIME_ARGS (max));
1300 gst_query_set_latency (query, live, min, max);
1305 res = gst_pad_query_default (pad, parent, query);
1312 gst_audio_resample_set_property (GObject * object, guint prop_id,
1313 const GValue * value, GParamSpec * pspec)
1315 GstAudioResample *resample;
1318 resample = GST_AUDIO_RESAMPLE (object);
1322 /* FIXME locking! */
1323 quality = g_value_get_int (value);
1324 GST_DEBUG_OBJECT (resample, "new quality %d", quality);
1326 gst_audio_resample_update_state (resample, resample->width,
1327 resample->channels, resample->inrate, resample->outrate,
1328 quality, resample->fp, resample->sinc_filter_mode,
1329 resample->sinc_filter_auto_threshold);
1331 case PROP_SINC_FILTER_MODE: {
1332 /* FIXME locking! */
1333 SpeexResamplerSincFilterMode sinc_filter_mode = g_value_get_enum (value);
1335 gst_audio_resample_update_state (resample, resample->width,
1336 resample->channels, resample->inrate, resample->outrate,
1337 resample->quality, resample->fp, sinc_filter_mode,
1338 resample->sinc_filter_auto_threshold);
1342 case PROP_SINC_FILTER_AUTO_THRESHOLD: {
1343 /* FIXME locking! */
1344 guint32 sinc_filter_auto_threshold = g_value_get_uint (value);
1346 gst_audio_resample_update_state (resample, resample->width,
1347 resample->channels, resample->inrate, resample->outrate,
1348 resample->quality, resample->fp, resample->sinc_filter_mode,
1349 sinc_filter_auto_threshold);
1354 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1360 gst_audio_resample_get_property (GObject * object, guint prop_id,
1361 GValue * value, GParamSpec * pspec)
1363 GstAudioResample *resample;
1365 resample = GST_AUDIO_RESAMPLE (object);
1369 g_value_set_int (value, resample->quality);
1371 case PROP_SINC_FILTER_MODE:
1372 g_value_set_enum(value, resample->sinc_filter_mode);
1374 case PROP_SINC_FILTER_AUTO_THRESHOLD:
1375 g_value_set_uint(value, resample->sinc_filter_auto_threshold);
1378 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1384 speex_resampler_sinc_filter_mode_get_type (void)
1386 static GType speex_resampler_sinc_filter_mode_type = 0;
1388 if (!speex_resampler_sinc_filter_mode_type) {
1389 static GEnumValue sinc_filter_modes[] = {
1390 { SPEEX_RESAMPLER_SINC_FILTER_INTERPOLATED, "Use interpolated sinc table", "interpolated" },
1391 { SPEEX_RESAMPLER_SINC_FILTER_FULL, "Use full sinc table", "full" },
1392 { SPEEX_RESAMPLER_SINC_FILTER_AUTO, "Use full table if table size below threshold", "auto" },
1396 speex_resampler_sinc_filter_mode_type = g_enum_register_static (
1397 "SpeexResamplerSincFilterMode",
1401 return speex_resampler_sinc_filter_mode_type;
1404 /* FIXME: should have a benchmark fallback for the case where orc is disabled */
1405 #if defined(AUDIORESAMPLE_FORMAT_AUTO) && !defined(DISABLE_ORC)
1407 #define BENCHMARK_SIZE 512
1410 _benchmark_int_float (SpeexResamplerState * st)
1412 gint16 in[BENCHMARK_SIZE] = { 0, }, G_GNUC_UNUSED out[BENCHMARK_SIZE / 2];
1413 gfloat in_tmp[BENCHMARK_SIZE], out_tmp[BENCHMARK_SIZE / 2];
1415 guint32 inlen = BENCHMARK_SIZE, outlen = BENCHMARK_SIZE / 2;
1417 for (i = 0; i < BENCHMARK_SIZE; i++) {
1419 in_tmp[i] = tmp / G_MAXINT16;
1422 resample_float_resampler_process_interleaved_float (st,
1423 (const guint8 *) in_tmp, &inlen, (guint8 *) out_tmp, &outlen);
1426 GST_ERROR ("Failed to use float resampler");
1430 for (i = 0; i < outlen; i++) {
1431 gfloat tmp = out_tmp[i];
1432 out[i] = CLAMP (tmp * G_MAXINT16 + 0.5, G_MININT16, G_MAXINT16);
1439 _benchmark_int_int (SpeexResamplerState * st)
1441 gint16 in[BENCHMARK_SIZE] = { 0, }, out[BENCHMARK_SIZE / 2];
1442 guint32 inlen = BENCHMARK_SIZE, outlen = BENCHMARK_SIZE / 2;
1444 resample_int_resampler_process_interleaved_int (st, (const guint8 *) in,
1445 &inlen, (guint8 *) out, &outlen);
1448 GST_ERROR ("Failed to use int resampler");
1456 _benchmark_integer_resampling (void)
1460 SpeexResamplerState *sta, *stb;
1463 orc_profile_init (&a);
1464 orc_profile_init (&b);
1466 sta = resample_float_resampler_init (1, 48000, 24000, 4,
1467 SPEEX_RESAMPLER_SINC_FILTER_INTERPOLATED,
1468 SPEEX_RESAMPLER_SINC_FILTER_AUTO_THRESHOLD_DEFAULT,
1471 GST_ERROR ("Failed to create float resampler state");
1475 stb = resample_int_resampler_init (1, 48000, 24000, 4,
1476 SPEEX_RESAMPLER_SINC_FILTER_INTERPOLATED,
1477 SPEEX_RESAMPLER_SINC_FILTER_AUTO_THRESHOLD_DEFAULT,
1480 resample_float_resampler_destroy (sta);
1481 GST_ERROR ("Failed to create int resampler state");
1486 for (i = 0; i < 10; i++) {
1487 orc_profile_start (&a);
1488 if (!_benchmark_int_float (sta))
1490 orc_profile_stop (&a);
1494 for (i = 0; i < 10; i++) {
1495 orc_profile_start (&b);
1496 if (!_benchmark_int_int (stb))
1498 orc_profile_stop (&b);
1501 /* Handle results */
1502 orc_profile_get_ave_std (&a, &av, NULL);
1503 orc_profile_get_ave_std (&b, &bv, NULL);
1505 /* Remember benchmark result in global variable */
1506 gst_audio_resample_use_int = (av > bv);
1507 resample_float_resampler_destroy (sta);
1508 resample_int_resampler_destroy (stb);
1511 GST_INFO ("Using integer resampler if appropriate: %lf < %lf", bv, av);
1513 GST_INFO ("Using float resampler for everything: %lf <= %lf", av, bv);
1518 resample_float_resampler_destroy (sta);
1519 resample_int_resampler_destroy (stb);
1523 #endif /* defined(AUDIORESAMPLE_FORMAT_AUTO) && !defined(DISABLE_ORC) */
1526 plugin_init (GstPlugin * plugin)
1528 GST_DEBUG_CATEGORY_INIT (audio_resample_debug, "audioresample", 0,
1529 "audio resampling element");
1531 #if defined(AUDIORESAMPLE_FORMAT_AUTO) && !defined(DISABLE_ORC)
1532 if (!_benchmark_integer_resampling ())
1535 GST_WARNING ("Orc disabled, can't benchmark int vs. float resampler");
1537 GST_DEBUG_CATEGORY_GET (GST_CAT_PERFORMANCE, "GST_PERFORMANCE");
1538 GST_CAT_WARNING (GST_CAT_PERFORMANCE, "orc disabled, no benchmarking done");
1542 if (!gst_element_register (plugin, "audioresample", GST_RANK_PRIMARY,
1543 GST_TYPE_AUDIO_RESAMPLE)) {
1550 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1553 "Resamples audio", plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME,
1554 GST_PACKAGE_ORIGIN);