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.
29 * <title>Example launch line</title>
31 * gst-launch -v filesrc location=sine.ogg ! oggdemux ! vorbisdec ! audioconvert ! audioresample ! audio/x-raw-int, rate=8000 ! alsasink
32 * ]| Decode an Ogg/Vorbis downsample to 8Khz and play sound through alsa.
33 * To create the Ogg/Vorbis file refer to the documentation of vorbisenc.
38 * - Enable SSE/ARM optimizations and select at runtime
48 #include "gstaudioresample.h"
49 #include <gst/audio/audio.h>
50 #include <gst/base/gstbasetransform.h>
52 #if defined AUDIORESAMPLE_FORMAT_AUTO
53 #define OIL_ENABLE_UNSTABLE_API
54 #include <liboil/liboilprofile.h>
55 #include <liboil/liboil.h>
58 GST_DEBUG_CATEGORY (audio_resample_debug);
59 #define GST_CAT_DEFAULT audio_resample_debug
68 #define SUPPORTED_CAPS \
70 "audio/x-raw-float, " \
71 "rate = (int) [ 1, MAX ], " \
72 "channels = (int) [ 1, MAX ], " \
73 "endianness = (int) BYTE_ORDER, " \
74 "width = (int) { 32, 64 }; " \
76 "rate = (int) [ 1, MAX ], " \
77 "channels = (int) [ 1, MAX ], " \
78 "endianness = (int) BYTE_ORDER, " \
79 "width = (int) 32, " \
80 "depth = (int) 32, " \
81 "signed = (boolean) true; " \
83 "rate = (int) [ 1, MAX ], " \
84 "channels = (int) [ 1, MAX ], " \
85 "endianness = (int) BYTE_ORDER, " \
86 "width = (int) 24, " \
87 "depth = (int) 24, " \
88 "signed = (boolean) true; " \
90 "rate = (int) [ 1, MAX ], " \
91 "channels = (int) [ 1, MAX ], " \
92 "endianness = (int) BYTE_ORDER, " \
93 "width = (int) 16, " \
94 "depth = (int) 16, " \
95 "signed = (boolean) true; " \
97 "rate = (int) [ 1, MAX ], " \
98 "channels = (int) [ 1, MAX ], " \
99 "endianness = (int) BYTE_ORDER, " \
100 "width = (int) 8, " \
101 "depth = (int) 8, " \
102 "signed = (boolean) true" \
105 /* If TRUE integer arithmetic resampling is faster and will be used if appropiate */
106 #if defined AUDIORESAMPLE_FORMAT_INT
107 static gboolean gst_audio_resample_use_int = TRUE;
108 #elif defined AUDIORESAMPLE_FORMAT_FLOAT
109 static gboolean gst_audio_resample_use_int = FALSE;
111 static gboolean gst_audio_resample_use_int = FALSE;
114 static GstStaticPadTemplate gst_audio_resample_sink_template =
115 GST_STATIC_PAD_TEMPLATE ("sink",
116 GST_PAD_SINK, GST_PAD_ALWAYS, SUPPORTED_CAPS);
118 static GstStaticPadTemplate gst_audio_resample_src_template =
119 GST_STATIC_PAD_TEMPLATE ("src",
120 GST_PAD_SRC, GST_PAD_ALWAYS, SUPPORTED_CAPS);
122 static void gst_audio_resample_set_property (GObject * object,
123 guint prop_id, const GValue * value, GParamSpec * pspec);
124 static void gst_audio_resample_get_property (GObject * object,
125 guint prop_id, GValue * value, GParamSpec * pspec);
128 static gboolean gst_audio_resample_get_unit_size (GstBaseTransform * base,
129 GstCaps * caps, guint * size);
130 static GstCaps *gst_audio_resample_transform_caps (GstBaseTransform * base,
131 GstPadDirection direction, GstCaps * caps);
132 static void gst_audio_resample_fixate_caps (GstBaseTransform * base,
133 GstPadDirection direction, GstCaps * caps, GstCaps * othercaps);
134 static gboolean gst_audio_resample_transform_size (GstBaseTransform * trans,
135 GstPadDirection direction, GstCaps * incaps, guint insize,
136 GstCaps * outcaps, guint * outsize);
137 static gboolean gst_audio_resample_set_caps (GstBaseTransform * base,
138 GstCaps * incaps, GstCaps * outcaps);
139 static GstFlowReturn gst_audio_resample_transform (GstBaseTransform * base,
140 GstBuffer * inbuf, GstBuffer * outbuf);
141 static gboolean gst_audio_resample_event (GstBaseTransform * base,
143 static gboolean gst_audio_resample_start (GstBaseTransform * base);
144 static gboolean gst_audio_resample_stop (GstBaseTransform * base);
145 static gboolean gst_audio_resample_query (GstPad * pad, GstQuery * query);
146 static const GstQueryType *gst_audio_resample_query_type (GstPad * pad);
148 GST_BOILERPLATE (GstAudioResample, gst_audio_resample, GstBaseTransform,
149 GST_TYPE_BASE_TRANSFORM);
152 gst_audio_resample_base_init (gpointer g_class)
154 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
156 gst_element_class_add_pad_template (gstelement_class,
157 gst_static_pad_template_get (&gst_audio_resample_src_template));
158 gst_element_class_add_pad_template (gstelement_class,
159 gst_static_pad_template_get (&gst_audio_resample_sink_template));
161 gst_element_class_set_details_simple (gstelement_class, "Audio resampler",
162 "Filter/Converter/Audio", "Resamples audio",
163 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
167 gst_audio_resample_class_init (GstAudioResampleClass * klass)
169 GObjectClass *gobject_class = (GObjectClass *) klass;
171 gobject_class->set_property = gst_audio_resample_set_property;
172 gobject_class->get_property = gst_audio_resample_get_property;
174 g_object_class_install_property (gobject_class, PROP_QUALITY,
175 g_param_spec_int ("quality", "Quality", "Resample quality with 0 being "
176 "the lowest and 10 being the best",
177 SPEEX_RESAMPLER_QUALITY_MIN, SPEEX_RESAMPLER_QUALITY_MAX,
178 SPEEX_RESAMPLER_QUALITY_DEFAULT,
179 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
181 /* FIXME 0.11: Remove this property, it's just for compatibility
182 * with old audioresample
185 * GstAudioResample:filter-length:
187 * Length of the resample filter
189 * Deprectated: Use #GstAudioResample:quality property instead
191 g_object_class_install_property (gobject_class, PROP_FILTER_LENGTH,
192 g_param_spec_int ("filter-length", "Filter length",
193 "Length of the resample filter", 0, G_MAXINT, 64, G_PARAM_READWRITE));
195 GST_BASE_TRANSFORM_CLASS (klass)->start =
196 GST_DEBUG_FUNCPTR (gst_audio_resample_start);
197 GST_BASE_TRANSFORM_CLASS (klass)->stop =
198 GST_DEBUG_FUNCPTR (gst_audio_resample_stop);
199 GST_BASE_TRANSFORM_CLASS (klass)->transform_size =
200 GST_DEBUG_FUNCPTR (gst_audio_resample_transform_size);
201 GST_BASE_TRANSFORM_CLASS (klass)->get_unit_size =
202 GST_DEBUG_FUNCPTR (gst_audio_resample_get_unit_size);
203 GST_BASE_TRANSFORM_CLASS (klass)->transform_caps =
204 GST_DEBUG_FUNCPTR (gst_audio_resample_transform_caps);
205 GST_BASE_TRANSFORM_CLASS (klass)->fixate_caps =
206 GST_DEBUG_FUNCPTR (gst_audio_resample_fixate_caps);
207 GST_BASE_TRANSFORM_CLASS (klass)->set_caps =
208 GST_DEBUG_FUNCPTR (gst_audio_resample_set_caps);
209 GST_BASE_TRANSFORM_CLASS (klass)->transform =
210 GST_DEBUG_FUNCPTR (gst_audio_resample_transform);
211 GST_BASE_TRANSFORM_CLASS (klass)->event =
212 GST_DEBUG_FUNCPTR (gst_audio_resample_event);
214 GST_BASE_TRANSFORM_CLASS (klass)->passthrough_on_same_caps = TRUE;
218 gst_audio_resample_init (GstAudioResample * resample,
219 GstAudioResampleClass * klass)
221 GstBaseTransform *trans = GST_BASE_TRANSFORM (resample);
223 resample->quality = SPEEX_RESAMPLER_QUALITY_DEFAULT;
225 resample->need_discont = FALSE;
227 gst_pad_set_query_function (trans->srcpad, gst_audio_resample_query);
228 gst_pad_set_query_type_function (trans->srcpad,
229 gst_audio_resample_query_type);
234 gst_audio_resample_start (GstBaseTransform * base)
236 GstAudioResample *resample = GST_AUDIO_RESAMPLE (base);
238 resample->next_offset = -1;
239 resample->next_ts = -1;
240 resample->next_upstream_ts = -1;
246 gst_audio_resample_stop (GstBaseTransform * base)
248 GstAudioResample *resample = GST_AUDIO_RESAMPLE (base);
250 if (resample->state) {
251 resample->funcs->destroy (resample->state);
252 resample->state = NULL;
255 resample->funcs = NULL;
257 g_free (resample->tmp_in);
258 resample->tmp_in = NULL;
259 resample->tmp_in_size = 0;
261 g_free (resample->tmp_out);
262 resample->tmp_out = NULL;
263 resample->tmp_out_size = 0;
265 gst_caps_replace (&resample->sinkcaps, NULL);
266 gst_caps_replace (&resample->srccaps, NULL);
272 gst_audio_resample_get_unit_size (GstBaseTransform * base, GstCaps * caps,
275 gint width, channels;
276 GstStructure *structure;
279 g_return_val_if_fail (size != NULL, FALSE);
281 /* this works for both float and int */
282 structure = gst_caps_get_structure (caps, 0);
283 ret = gst_structure_get_int (structure, "width", &width);
284 ret &= gst_structure_get_int (structure, "channels", &channels);
286 if (G_UNLIKELY (!ret))
289 *size = (width / 8) * channels;
295 gst_audio_resample_transform_caps (GstBaseTransform * base,
296 GstPadDirection direction, GstCaps * caps)
302 /* transform single caps into input_caps + input_caps with the rate
303 * field set to our supported range. This ensures that upstream knows
304 * about downstream's prefered rate(s) and can negotiate accordingly. */
305 res = gst_caps_copy (caps);
307 /* first, however, check if the caps contain a range for the rate field, in
308 * which case that side isn't going to care much about the exact sample rate
309 * chosen and we should just assume things will get fixated to something sane
310 * and we may just as well offer our full range instead of the range in the
311 * caps. If the rate is not an int range value, it's likely to express a
312 * real preference or limitation and we should maintain that structure as
313 * preference by putting it first into the transformed caps, and only add
314 * our full rate range as second option */
315 s = gst_caps_get_structure (res, 0);
316 val = gst_structure_get_value (s, "rate");
317 if (val == NULL || GST_VALUE_HOLDS_INT_RANGE (val)) {
318 /* overwrite existing range, or add field if it doesn't exist yet */
319 gst_structure_set (s, "rate", GST_TYPE_INT_RANGE, 1, G_MAXINT, NULL);
321 /* append caps with full range to existing caps with non-range rate field */
322 s = gst_structure_copy (s);
323 gst_structure_set (s, "rate", GST_TYPE_INT_RANGE, 1, G_MAXINT, NULL);
324 gst_caps_append_structure (res, s);
330 /* Fixate rate to the allowed rate that has the smallest difference */
332 gst_audio_resample_fixate_caps (GstBaseTransform * base,
333 GstPadDirection direction, GstCaps * caps, GstCaps * othercaps)
338 s = gst_caps_get_structure (caps, 0);
339 if (G_UNLIKELY (!gst_structure_get_int (s, "rate", &rate)))
342 s = gst_caps_get_structure (othercaps, 0);
343 gst_structure_fixate_field_nearest_int (s, "rate", rate);
346 static const SpeexResampleFuncs *
347 gst_audio_resample_get_funcs (gint width, gboolean fp)
349 const SpeexResampleFuncs *funcs = NULL;
351 if (gst_audio_resample_use_int && (width == 8 || width == 16) && !fp)
353 else if ((!gst_audio_resample_use_int && (width == 8 || width == 16) && !fp)
354 || (width == 32 && fp))
355 funcs = &float_funcs;
356 else if ((width == 64 && fp) || ((width == 32 || width == 24) && !fp))
357 funcs = &double_funcs;
359 g_assert_not_reached ();
364 static SpeexResamplerState *
365 gst_audio_resample_init_state (GstAudioResample * resample, gint width,
366 gint channels, gint inrate, gint outrate, gint quality, gboolean fp)
368 SpeexResamplerState *ret = NULL;
369 gint err = RESAMPLER_ERR_SUCCESS;
370 const SpeexResampleFuncs *funcs = gst_audio_resample_get_funcs (width, fp);
372 ret = funcs->init (channels, inrate, outrate, quality, &err);
374 if (G_UNLIKELY (err != RESAMPLER_ERR_SUCCESS)) {
375 GST_ERROR_OBJECT (resample, "Failed to create resampler state: %s",
376 funcs->strerror (err));
380 funcs->skip_zeros (ret);
386 gst_audio_resample_update_state (GstAudioResample * resample, gint width,
387 gint channels, gint inrate, gint outrate, gint quality, gboolean fp)
390 gboolean updated_latency = FALSE;
392 updated_latency = (resample->inrate != inrate
393 || quality != resample->quality) && resample->state != NULL;
395 if (resample->state == NULL) {
397 } else if (resample->channels != channels || fp != resample->fp
398 || width != resample->width) {
399 resample->funcs->destroy (resample->state);
401 gst_audio_resample_init_state (resample, width, channels, inrate,
402 outrate, quality, fp);
404 resample->funcs = gst_audio_resample_get_funcs (width, fp);
405 ret = (resample->state != NULL);
406 } else if (resample->inrate != inrate || resample->outrate != outrate) {
407 gint err = RESAMPLER_ERR_SUCCESS;
409 err = resample->funcs->set_rate (resample->state, inrate, outrate);
411 if (G_UNLIKELY (err != RESAMPLER_ERR_SUCCESS))
412 GST_ERROR_OBJECT (resample, "Failed to update rate: %s",
413 resample->funcs->strerror (err));
415 ret = (err == RESAMPLER_ERR_SUCCESS);
416 } else if (quality != resample->quality) {
417 gint err = RESAMPLER_ERR_SUCCESS;
419 err = resample->funcs->set_quality (resample->state, quality);
421 if (G_UNLIKELY (err != RESAMPLER_ERR_SUCCESS))
422 GST_ERROR_OBJECT (resample, "Failed to update quality: %s",
423 resample->funcs->strerror (err));
425 ret = (err == RESAMPLER_ERR_SUCCESS);
428 resample->width = width;
429 resample->channels = channels;
431 resample->quality = quality;
432 resample->inrate = inrate;
433 resample->outrate = outrate;
436 gst_element_post_message (GST_ELEMENT (resample),
437 gst_message_new_latency (GST_OBJECT (resample)));
443 gst_audio_resample_reset_state (GstAudioResample * resample)
446 resample->funcs->reset_mem (resample->state);
450 gst_audio_resample_parse_caps (GstCaps * incaps,
451 GstCaps * outcaps, gint * width, gint * channels, gint * inrate,
452 gint * outrate, gboolean * fp)
454 GstStructure *structure;
456 gint mywidth, myinrate, myoutrate, mychannels;
459 GST_DEBUG ("incaps %" GST_PTR_FORMAT ", outcaps %"
460 GST_PTR_FORMAT, incaps, outcaps);
462 structure = gst_caps_get_structure (incaps, 0);
464 if (g_str_equal (gst_structure_get_name (structure), "audio/x-raw-float"))
469 ret = gst_structure_get_int (structure, "rate", &myinrate);
470 ret &= gst_structure_get_int (structure, "channels", &mychannels);
471 ret &= gst_structure_get_int (structure, "width", &mywidth);
472 if (G_UNLIKELY (!ret))
473 goto no_in_rate_channels;
475 structure = gst_caps_get_structure (outcaps, 0);
476 ret = gst_structure_get_int (structure, "rate", &myoutrate);
477 if (G_UNLIKELY (!ret))
481 *channels = mychannels;
485 *outrate = myoutrate;
496 GST_DEBUG ("could not get input rate and channels");
501 GST_DEBUG ("could not get output rate");
507 _gcd (gint a, gint b)
520 gst_audio_resample_transform_size (GstBaseTransform * base,
521 GstPadDirection direction, GstCaps * caps, guint size, GstCaps * othercaps,
524 GstCaps *srccaps, *sinkcaps;
526 guint32 ratio_den, ratio_num;
527 gint inrate, outrate, gcd;
530 GST_LOG_OBJECT (base, "asked to transform size %d in direction %s",
531 size, direction == GST_PAD_SINK ? "SINK" : "SRC");
532 if (direction == GST_PAD_SINK) {
536 sinkcaps = othercaps;
541 gst_audio_resample_parse_caps (caps, othercaps, &width, NULL, &inrate,
543 if (G_UNLIKELY (!ret)) {
544 GST_ERROR_OBJECT (base, "Wrong caps");
548 gcd = _gcd (inrate, outrate);
549 ratio_num = inrate / gcd;
550 ratio_den = outrate / gcd;
552 if (direction == GST_PAD_SINK) {
553 gint fac = width / 8;
555 /* asked to convert size of an incoming buffer */
557 *othersize = (size * ratio_den + ratio_num - 1) / ratio_num;
561 gint fac = width / 8;
563 /* asked to convert size of an outgoing buffer */
565 *othersize = (size * ratio_num + ratio_den - 1) / ratio_den;
570 GST_LOG_OBJECT (base, "transformed size %d to %d", size, *othersize);
576 gst_audio_resample_set_caps (GstBaseTransform * base, GstCaps * incaps,
580 gint width = 0, inrate = 0, outrate = 0, channels = 0;
582 GstAudioResample *resample = GST_AUDIO_RESAMPLE (base);
584 GST_LOG ("incaps %" GST_PTR_FORMAT ", outcaps %"
585 GST_PTR_FORMAT, incaps, outcaps);
587 ret = gst_audio_resample_parse_caps (incaps, outcaps,
588 &width, &channels, &inrate, &outrate, &fp);
590 if (G_UNLIKELY (!ret))
594 gst_audio_resample_update_state (resample, width, channels, inrate,
595 outrate, resample->quality, fp);
597 if (G_UNLIKELY (!ret))
600 /* save caps so we can short-circuit in the size_transform if the caps
602 gst_caps_replace (&resample->sinkcaps, incaps);
603 gst_caps_replace (&resample->srccaps, outcaps);
608 #define GST_MAXINT24 (8388607)
609 #define GST_MININT24 (-8388608)
611 #if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
612 #define GST_READ_UINT24 GST_READ_UINT24_LE
613 #define GST_WRITE_UINT24 GST_WRITE_UINT24_LE
615 #define GST_READ_UINT24 GST_READ_UINT24_BE
616 #define GST_WRITE_UINT24 GST_WRITE_UINT24_BE
620 gst_audio_resample_convert_buffer (GstAudioResample * resample,
621 const guint8 * in, guint8 * out, guint len, gboolean inverse)
623 len *= resample->channels;
626 if (gst_audio_resample_use_int && resample->width == 8 && !resample->fp) {
627 gint8 *o = (gint8 *) out;
628 gint16 *i = (gint16 *) in;
632 tmp = *i + (G_MAXINT8 >> 1);
633 *o = CLAMP (tmp >> 8, G_MININT8, G_MAXINT8);
638 } else if (!gst_audio_resample_use_int && resample->width == 8
640 gint8 *o = (gint8 *) out;
641 gfloat *i = (gfloat *) in;
646 *o = (gint8) CLAMP (tmp * G_MAXINT8 + 0.5, G_MININT8, G_MAXINT8);
651 } else if (!gst_audio_resample_use_int && resample->width == 16
653 gint16 *o = (gint16 *) out;
654 gfloat *i = (gfloat *) in;
659 *o = (gint16) CLAMP (tmp * G_MAXINT16 + 0.5, G_MININT16, G_MAXINT16);
664 } else if (resample->width == 24 && !resample->fp) {
665 guint8 *o = (guint8 *) out;
666 gdouble *i = (gdouble *) in;
671 GST_WRITE_UINT24 (o, (gint32) CLAMP (tmp * GST_MAXINT24 + 0.5,
672 GST_MININT24, GST_MAXINT24));
677 } else if (resample->width == 32 && !resample->fp) {
678 gint32 *o = (gint32 *) out;
679 gdouble *i = (gdouble *) in;
684 *o = (gint32) CLAMP (tmp * G_MAXINT32 + 0.5, G_MININT32, G_MAXINT32);
690 g_assert_not_reached ();
693 if (gst_audio_resample_use_int && resample->width == 8 && !resample->fp) {
694 gint8 *i = (gint8 *) in;
695 gint16 *o = (gint16 *) out;
705 } else if (!gst_audio_resample_use_int && resample->width == 8
707 gint8 *i = (gint8 *) in;
708 gfloat *o = (gfloat *) out;
713 *o = tmp / G_MAXINT8;
718 } else if (!gst_audio_resample_use_int && resample->width == 16
720 gint16 *i = (gint16 *) in;
721 gfloat *o = (gfloat *) out;
726 *o = tmp / G_MAXINT16;
731 } else if (resample->width == 24 && !resample->fp) {
732 guint8 *i = (guint8 *) in;
733 gdouble *o = (gdouble *) out;
738 tmp2 = GST_READ_UINT24 (i);
739 if (tmp2 & 0x00800000)
742 *o = tmp / GST_MAXINT24;
747 } else if (resample->width == 32 && !resample->fp) {
748 gint32 *i = (gint32 *) in;
749 gdouble *o = (gdouble *) out;
754 *o = tmp / G_MAXINT32;
760 g_assert_not_reached ();
766 gst_audio_resample_push_drain (GstAudioResample * resample)
769 GstBaseTransform *trans = GST_BASE_TRANSFORM (resample);
772 guint out_len, out_processed;
775 guint8 *outtmp = NULL;
776 gboolean need_convert = FALSE;
778 if (!resample->state)
781 need_convert = (resample->funcs->width != resample->width);
783 resample->funcs->get_ratio (resample->state, &num, &den);
785 out_len = resample->funcs->get_input_latency (resample->state);
786 out_len = out_processed = (out_len * den + num - 1) / num;
787 outsize = (resample->width / 8) * out_len * resample->channels;
791 (resample->funcs->width / 8) * out_len * resample->channels;
792 if (outsize_tmp <= resample->tmp_out_size) {
793 outtmp = resample->tmp_out;
795 resample->tmp_out_size = outsize_tmp;
796 resample->tmp_out = outtmp = g_realloc (resample->tmp_out, outsize_tmp);
801 gst_pad_alloc_buffer_and_set_caps (trans->srcpad, GST_BUFFER_OFFSET_NONE,
802 outsize, GST_PAD_CAPS (trans->srcpad), &buf);
804 if (G_UNLIKELY (res != GST_FLOW_OK)) {
805 GST_WARNING_OBJECT (resample, "failed allocating buffer of %d bytes",
810 len = resample->funcs->get_input_latency (resample->state);
813 resample->funcs->process (resample->state,
814 NULL, &len, (need_convert) ? outtmp : GST_BUFFER_DATA (buf),
817 if (G_UNLIKELY (err != RESAMPLER_ERR_SUCCESS)) {
818 GST_WARNING_OBJECT (resample, "Failed to process drain: %s",
819 resample->funcs->strerror (err));
820 gst_buffer_unref (buf);
824 if (G_UNLIKELY (out_processed == 0)) {
825 GST_WARNING_OBJECT (resample, "Failed to get drain, dropping buffer");
826 gst_buffer_unref (buf);
830 /* If we wrote more than allocated something is really wrong now
831 * and we should better abort immediately */
832 g_assert (out_len >= out_processed);
835 gst_audio_resample_convert_buffer (resample, outtmp, GST_BUFFER_DATA (buf),
836 out_processed, TRUE);
838 GST_BUFFER_DURATION (buf) =
839 GST_FRAMES_TO_CLOCK_TIME (out_processed, resample->outrate);
840 GST_BUFFER_SIZE (buf) =
841 out_processed * resample->channels * (resample->width / 8);
843 if (GST_CLOCK_TIME_IS_VALID (resample->next_ts)) {
844 GST_BUFFER_OFFSET (buf) = resample->next_offset;
845 GST_BUFFER_OFFSET_END (buf) = resample->next_offset + out_processed;
846 GST_BUFFER_TIMESTAMP (buf) = resample->next_ts;
848 resample->next_ts += GST_BUFFER_DURATION (buf);
849 resample->next_offset += out_processed;
852 GST_LOG_OBJECT (resample,
853 "Pushing drain buffer of %u bytes with timestamp %" GST_TIME_FORMAT
854 " duration %" GST_TIME_FORMAT " offset %" G_GUINT64_FORMAT " offset_end %"
855 G_GUINT64_FORMAT, GST_BUFFER_SIZE (buf),
856 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
857 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)), GST_BUFFER_OFFSET (buf),
858 GST_BUFFER_OFFSET_END (buf));
860 res = gst_pad_push (trans->srcpad, buf);
862 if (G_UNLIKELY (res != GST_FLOW_OK))
863 GST_WARNING_OBJECT (resample, "Failed to push drain: %s",
864 gst_flow_get_name (res));
870 gst_audio_resample_event (GstBaseTransform * base, GstEvent * event)
872 GstAudioResample *resample = GST_AUDIO_RESAMPLE (base);
874 switch (GST_EVENT_TYPE (event)) {
875 case GST_EVENT_FLUSH_STOP:
876 gst_audio_resample_reset_state (resample);
877 resample->next_offset = -1;
878 resample->next_ts = -1;
879 resample->next_upstream_ts = -1;
881 case GST_EVENT_NEWSEGMENT:
882 gst_audio_resample_push_drain (resample);
883 gst_audio_resample_reset_state (resample);
884 resample->next_offset = -1;
885 resample->next_ts = -1;
886 resample->next_upstream_ts = -1;
889 gst_audio_resample_push_drain (resample);
890 gst_audio_resample_reset_state (resample);
896 return parent_class->event (base, event);
900 gst_audio_resample_check_discont (GstAudioResample * resample,
901 GstClockTime timestamp)
903 if (timestamp != GST_CLOCK_TIME_NONE &&
904 resample->next_upstream_ts != GST_CLOCK_TIME_NONE &&
905 timestamp != resample->next_upstream_ts) {
906 /* Potentially a discontinuous buffer. However, it turns out that many
907 * elements generate imperfect streams due to rounding errors, so we permit
908 * a small error (up to one sample) without triggering a filter
909 * flush/restart (if triggered incorrectly, this will be audible) */
910 GstClockTimeDiff diff = timestamp - resample->next_upstream_ts;
912 if (ABS (diff) > (GST_SECOND + resample->inrate - 1) / resample->inrate) {
913 GST_WARNING_OBJECT (resample,
914 "encountered timestamp discontinuity of %s%" GST_TIME_FORMAT,
915 (diff < 0) ? "-" : "", GST_TIME_ARGS ((GstClockTime) ABS (diff)));
924 gst_audio_resample_process (GstAudioResample * resample, GstBuffer * inbuf,
927 guint32 in_len, in_processed;
928 guint32 out_len, out_processed;
929 gint err = RESAMPLER_ERR_SUCCESS;
930 guint8 *in_tmp = NULL, *out_tmp = NULL;
931 gboolean need_convert = (resample->funcs->width != resample->width);
933 in_len = GST_BUFFER_SIZE (inbuf) / resample->channels;
934 out_len = GST_BUFFER_SIZE (outbuf) / resample->channels;
936 in_len /= (resample->width / 8);
937 out_len /= (resample->width / 8);
939 in_processed = in_len;
940 out_processed = out_len;
944 in_len * resample->channels * (resample->funcs->width / 8);
946 out_len * resample->channels * (resample->funcs->width / 8);
948 if (in_size_tmp <= resample->tmp_in_size) {
949 in_tmp = resample->tmp_in;
951 resample->tmp_in = in_tmp = g_realloc (resample->tmp_in, in_size_tmp);
952 resample->tmp_in_size = in_size_tmp;
955 gst_audio_resample_convert_buffer (resample, GST_BUFFER_DATA (inbuf),
956 in_tmp, in_len, FALSE);
958 if (out_size_tmp <= resample->tmp_out_size) {
959 out_tmp = resample->tmp_out;
961 resample->tmp_out = out_tmp = g_realloc (resample->tmp_out, out_size_tmp);
962 resample->tmp_out_size = out_size_tmp;
967 err = resample->funcs->process (resample->state,
968 in_tmp, &in_processed, out_tmp, &out_processed);
970 err = resample->funcs->process (resample->state,
971 (const guint8 *) GST_BUFFER_DATA (inbuf), &in_processed,
972 (guint8 *) GST_BUFFER_DATA (outbuf), &out_processed);
975 if (G_UNLIKELY (in_len != in_processed))
976 GST_WARNING_OBJECT (resample, "Converted %d of %d input samples",
977 in_processed, in_len);
979 if (out_len != out_processed) {
980 if (out_processed == 0) {
981 GST_DEBUG_OBJECT (resample, "Converted to 0 samples, buffer dropped");
983 return GST_BASE_TRANSFORM_FLOW_DROPPED;
986 /* If we wrote more than allocated something is really wrong now
987 * and we should better abort immediately */
988 g_assert (out_len >= out_processed);
991 if (G_UNLIKELY (err != RESAMPLER_ERR_SUCCESS)) {
992 GST_ERROR_OBJECT (resample, "Failed to convert data: %s",
993 resample->funcs->strerror (err));
994 return GST_FLOW_ERROR;
998 gst_audio_resample_convert_buffer (resample, out_tmp,
999 GST_BUFFER_DATA (outbuf), out_processed, TRUE);
1001 GST_BUFFER_DURATION (outbuf) =
1002 GST_FRAMES_TO_CLOCK_TIME (out_processed, resample->outrate);
1003 GST_BUFFER_SIZE (outbuf) =
1004 out_processed * resample->channels * (resample->width / 8);
1006 if (GST_CLOCK_TIME_IS_VALID (resample->next_ts)) {
1007 GST_BUFFER_TIMESTAMP (outbuf) = resample->next_ts;
1008 GST_BUFFER_OFFSET (outbuf) = resample->next_offset;
1009 GST_BUFFER_OFFSET_END (outbuf) = resample->next_offset + out_processed;
1011 resample->next_ts += GST_BUFFER_DURATION (outbuf);
1012 resample->next_offset += out_processed;
1015 GST_LOG_OBJECT (resample,
1016 "Converted to buffer of %u bytes with timestamp %" GST_TIME_FORMAT
1017 ", duration %" GST_TIME_FORMAT ", offset %" G_GUINT64_FORMAT
1018 ", offset_end %" G_GUINT64_FORMAT, GST_BUFFER_SIZE (outbuf),
1019 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)),
1020 GST_TIME_ARGS (GST_BUFFER_DURATION (outbuf)),
1021 GST_BUFFER_OFFSET (outbuf), GST_BUFFER_OFFSET_END (outbuf));
1027 static GstFlowReturn
1028 gst_audio_resample_transform (GstBaseTransform * base, GstBuffer * inbuf,
1031 GstAudioResample *resample = GST_AUDIO_RESAMPLE (base);
1034 GstClockTime timestamp;
1035 guint outsamples, insamples;
1038 if (resample->state == NULL) {
1039 if (G_UNLIKELY (!(resample->state =
1040 gst_audio_resample_init_state (resample, resample->width,
1041 resample->channels, resample->inrate, resample->outrate,
1042 resample->quality, resample->fp))))
1043 return GST_FLOW_ERROR;
1046 gst_audio_resample_get_funcs (resample->width, resample->fp);
1049 data = GST_BUFFER_DATA (inbuf);
1050 size = GST_BUFFER_SIZE (inbuf);
1051 timestamp = GST_BUFFER_TIMESTAMP (inbuf);
1053 GST_LOG_OBJECT (resample, "transforming buffer of %ld bytes, ts %"
1054 GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT ", offset %"
1055 G_GINT64_FORMAT ", offset_end %" G_GINT64_FORMAT,
1056 size, GST_TIME_ARGS (timestamp),
1057 GST_TIME_ARGS (GST_BUFFER_DURATION (inbuf)),
1058 GST_BUFFER_OFFSET (inbuf), GST_BUFFER_OFFSET_END (inbuf));
1060 /* check for timestamp discontinuities and flush/reset if needed */
1061 if (G_UNLIKELY (gst_audio_resample_check_discont (resample, timestamp)
1062 || GST_BUFFER_IS_DISCONT (inbuf))) {
1063 /* Flush internal samples */
1064 gst_audio_resample_reset_state (resample);
1065 /* Inform downstream element about discontinuity */
1066 resample->need_discont = TRUE;
1067 /* We want to recalculate the timestamps */
1068 resample->next_ts = -1;
1069 resample->next_upstream_ts = -1;
1070 resample->next_offset = -1;
1073 insamples = GST_BUFFER_SIZE (inbuf) / resample->channels;
1074 insamples /= (resample->width / 8);
1076 outsamples = GST_BUFFER_SIZE (outbuf) / resample->channels;
1077 outsamples /= (resample->width / 8);
1079 if (GST_CLOCK_TIME_IS_VALID (timestamp)
1080 && !GST_CLOCK_TIME_IS_VALID (resample->next_ts)) {
1081 resample->next_ts = timestamp;
1082 resample->next_offset =
1083 GST_CLOCK_TIME_TO_FRAMES (timestamp, resample->outrate);
1086 if (G_UNLIKELY (resample->need_discont)) {
1087 GST_DEBUG_OBJECT (resample, "marking this buffer with the DISCONT flag");
1088 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
1089 resample->need_discont = FALSE;
1092 ret = gst_audio_resample_process (resample, inbuf, outbuf);
1093 if (G_UNLIKELY (ret != GST_FLOW_OK))
1096 if (GST_CLOCK_TIME_IS_VALID (timestamp)
1097 && !GST_CLOCK_TIME_IS_VALID (resample->next_upstream_ts))
1098 resample->next_upstream_ts = timestamp;
1100 if (GST_CLOCK_TIME_IS_VALID (resample->next_upstream_ts))
1101 resample->next_upstream_ts +=
1102 GST_FRAMES_TO_CLOCK_TIME (insamples, resample->inrate);
1108 gst_audio_resample_query (GstPad * pad, GstQuery * query)
1110 GstAudioResample *resample = GST_AUDIO_RESAMPLE (gst_pad_get_parent (pad));
1111 GstBaseTransform *trans = GST_BASE_TRANSFORM (resample);
1112 gboolean res = TRUE;
1114 switch (GST_QUERY_TYPE (query)) {
1115 case GST_QUERY_LATENCY:
1117 GstClockTime min, max;
1121 gint rate = resample->inrate;
1122 gint resampler_latency;
1124 if (resample->state)
1126 resample->funcs->get_input_latency (resample->state);
1128 resampler_latency = 0;
1130 if (gst_base_transform_is_passthrough (trans))
1131 resampler_latency = 0;
1133 if ((peer = gst_pad_get_peer (trans->sinkpad))) {
1134 if ((res = gst_pad_query (peer, query))) {
1135 gst_query_parse_latency (query, &live, &min, &max);
1137 GST_DEBUG_OBJECT (resample, "Peer latency: min %"
1138 GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
1139 GST_TIME_ARGS (min), GST_TIME_ARGS (max));
1141 /* add our own latency */
1142 if (rate != 0 && resampler_latency != 0)
1144 gst_util_uint64_scale (resampler_latency, GST_SECOND, rate);
1148 GST_DEBUG_OBJECT (resample, "Our latency: %" GST_TIME_FORMAT,
1149 GST_TIME_ARGS (latency));
1152 if (max != GST_CLOCK_TIME_NONE)
1155 GST_DEBUG_OBJECT (resample, "Calculated total latency : min %"
1156 GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
1157 GST_TIME_ARGS (min), GST_TIME_ARGS (max));
1159 gst_query_set_latency (query, live, min, max);
1161 gst_object_unref (peer);
1166 res = gst_pad_query_default (pad, query);
1169 gst_object_unref (resample);
1173 static const GstQueryType *
1174 gst_audio_resample_query_type (GstPad * pad)
1176 static const GstQueryType types[] = {
1185 gst_audio_resample_set_property (GObject * object, guint prop_id,
1186 const GValue * value, GParamSpec * pspec)
1188 GstAudioResample *resample;
1190 resample = GST_AUDIO_RESAMPLE (object);
1194 GST_BASE_TRANSFORM_LOCK (resample);
1195 resample->quality = g_value_get_int (value);
1196 GST_DEBUG_OBJECT (resample, "new quality %d", resample->quality);
1198 gst_audio_resample_update_state (resample, resample->width,
1199 resample->channels, resample->inrate, resample->outrate,
1200 resample->quality, resample->fp);
1201 GST_BASE_TRANSFORM_UNLOCK (resample);
1203 case PROP_FILTER_LENGTH:{
1204 gint filter_length = g_value_get_int (value);
1206 GST_BASE_TRANSFORM_LOCK (resample);
1207 if (filter_length <= 8)
1208 resample->quality = 0;
1209 else if (filter_length <= 16)
1210 resample->quality = 1;
1211 else if (filter_length <= 32)
1212 resample->quality = 2;
1213 else if (filter_length <= 48)
1214 resample->quality = 3;
1215 else if (filter_length <= 64)
1216 resample->quality = 4;
1217 else if (filter_length <= 80)
1218 resample->quality = 5;
1219 else if (filter_length <= 96)
1220 resample->quality = 6;
1221 else if (filter_length <= 128)
1222 resample->quality = 7;
1223 else if (filter_length <= 160)
1224 resample->quality = 8;
1225 else if (filter_length <= 192)
1226 resample->quality = 9;
1228 resample->quality = 10;
1230 GST_DEBUG_OBJECT (resample, "new quality %d", resample->quality);
1232 gst_audio_resample_update_state (resample, resample->width,
1233 resample->channels, resample->inrate, resample->outrate,
1234 resample->quality, resample->fp);
1235 GST_BASE_TRANSFORM_UNLOCK (resample);
1239 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1245 gst_audio_resample_get_property (GObject * object, guint prop_id,
1246 GValue * value, GParamSpec * pspec)
1248 GstAudioResample *resample;
1250 resample = GST_AUDIO_RESAMPLE (object);
1254 g_value_set_int (value, resample->quality);
1256 case PROP_FILTER_LENGTH:
1257 switch (resample->quality) {
1259 g_value_set_int (value, 8);
1262 g_value_set_int (value, 16);
1265 g_value_set_int (value, 32);
1268 g_value_set_int (value, 48);
1271 g_value_set_int (value, 64);
1274 g_value_set_int (value, 80);
1277 g_value_set_int (value, 96);
1280 g_value_set_int (value, 128);
1283 g_value_set_int (value, 160);
1286 g_value_set_int (value, 192);
1289 g_value_set_int (value, 256);
1294 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1299 #if defined AUDIORESAMPLE_FORMAT_AUTO
1300 #define BENCHMARK_SIZE 512
1303 _benchmark_int_float (SpeexResamplerState * st)
1305 gint16 in[BENCHMARK_SIZE] = { 0, }, out[BENCHMARK_SIZE / 2];
1306 gfloat in_tmp[BENCHMARK_SIZE], out_tmp[BENCHMARK_SIZE / 2];
1308 guint32 inlen = BENCHMARK_SIZE, outlen = BENCHMARK_SIZE / 2;
1310 for (i = 0; i < BENCHMARK_SIZE; i++) {
1312 in_tmp[i] = tmp / G_MAXINT16;
1315 resample_float_resampler_process_interleaved_float (st,
1316 (const guint8 *) in_tmp, &inlen, (guint8 *) out_tmp, &outlen);
1319 GST_ERROR ("Failed to use float resampler");
1323 for (i = 0; i < outlen; i++) {
1324 gfloat tmp = out_tmp[i];
1325 out[i] = CLAMP (tmp * G_MAXINT16 + 0.5, G_MININT16, G_MAXINT16);
1332 _benchmark_int_int (SpeexResamplerState * st)
1334 gint16 in[BENCHMARK_SIZE] = { 0, }, out[BENCHMARK_SIZE / 2];
1335 guint32 inlen = BENCHMARK_SIZE, outlen = BENCHMARK_SIZE / 2;
1337 resample_int_resampler_process_interleaved_int (st, (const guint8 *) in,
1338 &inlen, (guint8 *) out, &outlen);
1341 GST_ERROR ("Failed to use int resampler");
1349 _benchmark_integer_resampling (void)
1353 SpeexResamplerState *sta, *stb;
1355 oil_profile_init (&a);
1356 oil_profile_init (&b);
1358 sta = resample_float_resampler_init (1, 48000, 24000, 4, NULL);
1360 GST_ERROR ("Failed to create float resampler state");
1364 stb = resample_int_resampler_init (1, 48000, 24000, 4, NULL);
1366 resample_float_resampler_destroy (sta);
1367 GST_ERROR ("Failed to create int resampler state");
1372 if (!_benchmark_int_float (sta))
1374 if (!_benchmark_int_float (sta))
1378 oil_profile_start (&a);
1379 if (!_benchmark_int_float (sta))
1381 oil_profile_stop (&a);
1384 if (!_benchmark_int_int (stb))
1386 if (!_benchmark_int_int (stb))
1390 oil_profile_start (&b);
1391 if (!_benchmark_int_int (stb))
1393 oil_profile_stop (&b);
1395 /* Handle results */
1396 oil_profile_get_ave_std (&a, &av, NULL);
1397 oil_profile_get_ave_std (&b, &bv, NULL);
1399 /* Remember benchmark result in global variable */
1400 gst_audio_resample_use_int = (av > bv);
1401 resample_float_resampler_destroy (sta);
1402 resample_int_resampler_destroy (stb);
1405 GST_INFO ("Using integer resampler if appropiate: %lf < %lf", bv, av);
1407 GST_INFO ("Using float resampler for everything: %lf <= %lf", av, bv);
1412 resample_float_resampler_destroy (sta);
1413 resample_int_resampler_destroy (stb);
1420 plugin_init (GstPlugin * plugin)
1422 GST_DEBUG_CATEGORY_INIT (audio_resample_debug, "audioresample", 0,
1423 "audio resampling element");
1424 #if defined AUDIORESAMPLE_FORMAT_AUTO
1427 if (!_benchmark_integer_resampling ())
1431 if (!gst_element_register (plugin, "audioresample", GST_RANK_PRIMARY,
1432 GST_TYPE_AUDIO_RESAMPLE)) {
1439 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1442 "Resamples audio", plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME,
1443 GST_PACKAGE_ORIGIN);