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., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
23 * SECTION:element-audioresample
24 * @title: audioresample
26 * audioresample resamples raw audio buffers to different sample rates using
27 * a configurable windowing function to enhance quality.
29 * By default, the resampler uses a reduced sinc table, with cubic interpolation filling in
30 * the gaps. This ensures that the table does not become too big. However, the interpolation
31 * increases the CPU usage considerably. As an alternative, a full sinc table can be used.
32 * Doing so can drastically reduce CPU usage (4x faster with 44.1 -> 48 kHz conversions for
33 * example), at the cost of increased memory consumption, plus the sinc table takes longer
34 * to initialize when the element is created. A third mode exists, which uses the full table
35 * unless said table would become too large, in which case the interpolated one is used instead.
37 * ## Example launch line
39 * gst-launch-1.0 -v uridecodebin uri=file:///path/to/audio.ogg ! audioconvert ! audioresample ! audio/x-raw, rate=8000 ! autoaudiosink
41 * Decode an audio file and downsample it to 8Khz and play sound.
42 * To create the Ogg/Vorbis file refer to the documentation of vorbisenc.
43 * This assumes there is an audio sink that will accept/handle 8kHz audio.
48 * - Enable SSE/ARM optimizations and select at runtime
58 #include "gstaudioresample.h"
59 #include <gst/gstutils.h>
60 #include <gst/audio/audio.h>
61 #include <gst/base/gstbasetransform.h>
63 GST_DEBUG_CATEGORY (audio_resample_debug);
64 #define GST_CAT_DEFAULT audio_resample_debug
68 #define DEFAULT_QUALITY GST_AUDIO_RESAMPLER_QUALITY_DEFAULT
69 #define DEFAULT_RESAMPLE_METHOD GST_AUDIO_RESAMPLER_METHOD_KAISER
70 #define DEFAULT_SINC_FILTER_MODE GST_AUDIO_RESAMPLER_FILTER_MODE_AUTO
71 #define DEFAULT_SINC_FILTER_AUTO_THRESHOLD (1*1048576)
72 #define DEFAULT_SINC_FILTER_INTERPOLATION GST_AUDIO_RESAMPLER_FILTER_INTERPOLATION_CUBIC
79 PROP_SINC_FILTER_MODE,
80 PROP_SINC_FILTER_AUTO_THRESHOLD,
81 PROP_SINC_FILTER_INTERPOLATION
84 #define SUPPORTED_CAPS \
85 GST_AUDIO_CAPS_MAKE (GST_AUDIO_FORMATS_ALL) \
86 ", layout = (string) { interleaved, non-interleaved }"
88 static GstStaticPadTemplate gst_audio_resample_sink_template =
89 GST_STATIC_PAD_TEMPLATE ("sink",
92 GST_STATIC_CAPS (SUPPORTED_CAPS));
94 static GstStaticPadTemplate gst_audio_resample_src_template =
95 GST_STATIC_PAD_TEMPLATE ("src",
98 GST_STATIC_CAPS (SUPPORTED_CAPS));
100 /* cached quark to avoid contention on the global quark table lock */
101 #define META_TAG_AUDIO meta_tag_audio_quark
102 static GQuark meta_tag_audio_quark;
104 static void gst_audio_resample_set_property (GObject * object,
105 guint prop_id, const GValue * value, GParamSpec * pspec);
106 static void gst_audio_resample_get_property (GObject * object,
107 guint prop_id, GValue * value, GParamSpec * pspec);
110 static gboolean gst_audio_resample_get_unit_size (GstBaseTransform * base,
111 GstCaps * caps, gsize * size);
112 static GstCaps *gst_audio_resample_transform_caps (GstBaseTransform * base,
113 GstPadDirection direction, GstCaps * caps, GstCaps * filter);
114 static GstCaps *gst_audio_resample_fixate_caps (GstBaseTransform * base,
115 GstPadDirection direction, GstCaps * caps, GstCaps * othercaps);
116 static gboolean gst_audio_resample_transform_size (GstBaseTransform * trans,
117 GstPadDirection direction, GstCaps * incaps, gsize insize,
118 GstCaps * outcaps, gsize * outsize);
119 static gboolean gst_audio_resample_set_caps (GstBaseTransform * base,
120 GstCaps * incaps, GstCaps * outcaps);
121 static GstFlowReturn gst_audio_resample_transform (GstBaseTransform * base,
122 GstBuffer * inbuf, GstBuffer * outbuf);
123 static gboolean gst_audio_resample_transform_meta (GstBaseTransform * trans,
124 GstBuffer * outbuf, GstMeta * meta, GstBuffer * inbuf);
125 static GstFlowReturn gst_audio_resample_submit_input_buffer (GstBaseTransform *
126 base, gboolean is_discont, GstBuffer * input);
127 static gboolean gst_audio_resample_sink_event (GstBaseTransform * base,
129 static gboolean gst_audio_resample_start (GstBaseTransform * base);
130 static gboolean gst_audio_resample_stop (GstBaseTransform * base);
131 static gboolean gst_audio_resample_query (GstPad * pad, GstObject * parent,
134 static void gst_audio_resample_push_drain (GstAudioResample * resample,
137 #define gst_audio_resample_parent_class parent_class
138 G_DEFINE_TYPE (GstAudioResample, gst_audio_resample, GST_TYPE_BASE_TRANSFORM);
139 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (audioresample, "audioresample",
140 GST_RANK_PRIMARY, GST_TYPE_AUDIO_RESAMPLE,
141 GST_DEBUG_CATEGORY_INIT (audio_resample_debug, "audioresample", 0,
142 "audio resampling element"));
144 gst_audio_resample_class_init (GstAudioResampleClass * klass)
146 GObjectClass *gobject_class = (GObjectClass *) klass;
147 GstElementClass *gstelement_class = (GstElementClass *) klass;
149 gobject_class->set_property = gst_audio_resample_set_property;
150 gobject_class->get_property = gst_audio_resample_get_property;
152 g_object_class_install_property (gobject_class, PROP_QUALITY,
153 g_param_spec_int ("quality", "Quality", "Resample quality with 0 being "
154 "the lowest and 10 being the best",
155 GST_AUDIO_RESAMPLER_QUALITY_MIN, GST_AUDIO_RESAMPLER_QUALITY_MAX,
157 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
159 g_object_class_install_property (gobject_class, PROP_RESAMPLE_METHOD,
160 g_param_spec_enum ("resample-method", "Resample method to use",
161 "What resample method to use",
162 GST_TYPE_AUDIO_RESAMPLER_METHOD,
163 DEFAULT_RESAMPLE_METHOD, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
164 g_object_class_install_property (gobject_class, PROP_SINC_FILTER_MODE,
165 g_param_spec_enum ("sinc-filter-mode", "Sinc filter table mode",
166 "What sinc filter table mode to use",
167 GST_TYPE_AUDIO_RESAMPLER_FILTER_MODE,
168 DEFAULT_SINC_FILTER_MODE,
169 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
171 g_object_class_install_property (gobject_class,
172 PROP_SINC_FILTER_AUTO_THRESHOLD,
173 g_param_spec_uint ("sinc-filter-auto-threshold",
174 "Sinc filter auto mode threshold",
175 "Memory usage threshold to use if sinc filter mode is AUTO, given in bytes",
176 0, G_MAXUINT, DEFAULT_SINC_FILTER_AUTO_THRESHOLD,
177 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
178 g_object_class_install_property (gobject_class,
179 PROP_SINC_FILTER_INTERPOLATION,
180 g_param_spec_enum ("sinc-filter-interpolation",
181 "Sinc filter interpolation",
182 "How to interpolate the sinc filter table",
183 GST_TYPE_AUDIO_RESAMPLER_FILTER_INTERPOLATION,
184 DEFAULT_SINC_FILTER_INTERPOLATION,
185 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
187 gst_element_class_add_static_pad_template (gstelement_class,
188 &gst_audio_resample_src_template);
189 gst_element_class_add_static_pad_template (gstelement_class,
190 &gst_audio_resample_sink_template);
192 gst_element_class_set_static_metadata (gstelement_class, "Audio resampler",
193 "Filter/Converter/Audio", "Resamples audio",
194 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
196 GST_BASE_TRANSFORM_CLASS (klass)->start =
197 GST_DEBUG_FUNCPTR (gst_audio_resample_start);
198 GST_BASE_TRANSFORM_CLASS (klass)->stop =
199 GST_DEBUG_FUNCPTR (gst_audio_resample_stop);
200 GST_BASE_TRANSFORM_CLASS (klass)->transform_size =
201 GST_DEBUG_FUNCPTR (gst_audio_resample_transform_size);
202 GST_BASE_TRANSFORM_CLASS (klass)->get_unit_size =
203 GST_DEBUG_FUNCPTR (gst_audio_resample_get_unit_size);
204 GST_BASE_TRANSFORM_CLASS (klass)->transform_caps =
205 GST_DEBUG_FUNCPTR (gst_audio_resample_transform_caps);
206 GST_BASE_TRANSFORM_CLASS (klass)->fixate_caps =
207 GST_DEBUG_FUNCPTR (gst_audio_resample_fixate_caps);
208 GST_BASE_TRANSFORM_CLASS (klass)->set_caps =
209 GST_DEBUG_FUNCPTR (gst_audio_resample_set_caps);
210 GST_BASE_TRANSFORM_CLASS (klass)->transform =
211 GST_DEBUG_FUNCPTR (gst_audio_resample_transform);
212 GST_BASE_TRANSFORM_CLASS (klass)->sink_event =
213 GST_DEBUG_FUNCPTR (gst_audio_resample_sink_event);
214 GST_BASE_TRANSFORM_CLASS (klass)->transform_meta =
215 GST_DEBUG_FUNCPTR (gst_audio_resample_transform_meta);
216 GST_BASE_TRANSFORM_CLASS (klass)->submit_input_buffer =
217 GST_DEBUG_FUNCPTR (gst_audio_resample_submit_input_buffer);
219 GST_BASE_TRANSFORM_CLASS (klass)->passthrough_on_same_caps = TRUE;
221 gst_type_mark_as_plugin_api (GST_TYPE_AUDIO_RESAMPLER_METHOD, 0);
222 gst_type_mark_as_plugin_api (GST_TYPE_AUDIO_RESAMPLER_FILTER_INTERPOLATION,
224 gst_type_mark_as_plugin_api (GST_TYPE_AUDIO_RESAMPLER_FILTER_MODE, 0);
226 meta_tag_audio_quark = g_quark_from_static_string (GST_META_TAG_AUDIO_STR);
230 gst_audio_resample_init (GstAudioResample * resample)
232 GstBaseTransform *trans = GST_BASE_TRANSFORM (resample);
234 resample->method = DEFAULT_RESAMPLE_METHOD;
235 resample->quality = DEFAULT_QUALITY;
236 resample->sinc_filter_mode = DEFAULT_SINC_FILTER_MODE;
237 resample->sinc_filter_auto_threshold = DEFAULT_SINC_FILTER_AUTO_THRESHOLD;
238 resample->sinc_filter_interpolation = DEFAULT_SINC_FILTER_INTERPOLATION;
240 gst_base_transform_set_gap_aware (trans, TRUE);
241 gst_pad_set_query_function (trans->srcpad, gst_audio_resample_query);
246 gst_audio_resample_start (GstBaseTransform * base)
248 GstAudioResample *resample = GST_AUDIO_RESAMPLE (base);
250 resample->need_discont = TRUE;
252 resample->num_gap_samples = 0;
253 resample->num_nongap_samples = 0;
254 resample->t0 = GST_CLOCK_TIME_NONE;
255 resample->in_offset0 = GST_BUFFER_OFFSET_NONE;
256 resample->out_offset0 = GST_BUFFER_OFFSET_NONE;
257 resample->samples_in = 0;
258 resample->samples_out = 0;
264 gst_audio_resample_stop (GstBaseTransform * base)
266 GstAudioResample *resample = GST_AUDIO_RESAMPLE (base);
268 if (resample->converter) {
269 gst_audio_converter_free (resample->converter);
270 resample->converter = NULL;
276 gst_audio_resample_get_unit_size (GstBaseTransform * base, GstCaps * caps,
281 if (!gst_audio_info_from_caps (&info, caps))
284 *size = GST_AUDIO_INFO_BPF (&info);
291 GST_ERROR_OBJECT (base, "invalid caps");
297 gst_audio_resample_transform_caps (GstBaseTransform * base,
298 GstPadDirection direction, GstCaps * caps, GstCaps * filter)
305 /* transform single caps into input_caps + input_caps with the rate
306 * field set to our supported range. This ensures that upstream knows
307 * about downstream's preferred rate(s) and can negotiate accordingly. */
308 res = gst_caps_new_empty ();
309 n = gst_caps_get_size (caps);
310 for (i = 0; i < n; i++) {
311 s = gst_caps_get_structure (caps, i);
313 /* If this is already expressed by the existing caps
314 * skip this structure */
315 if (i > 0 && gst_caps_is_subset_structure (res, s))
318 /* first, however, check if the caps contain a range for the rate field, in
319 * which case that side isn't going to care much about the exact sample rate
320 * chosen and we should just assume things will get fixated to something sane
321 * and we may just as well offer our full range instead of the range in the
322 * caps. If the rate is not an int range value, it's likely to express a
323 * real preference or limitation and we should maintain that structure as
324 * preference by putting it first into the transformed caps, and only add
325 * our full rate range as second option */
326 s = gst_structure_copy (s);
327 val = gst_structure_get_value (s, "rate");
328 if (val == NULL || GST_VALUE_HOLDS_INT_RANGE (val)) {
329 /* overwrite existing range, or add field if it doesn't exist yet */
330 gst_structure_set (s, "rate", GST_TYPE_INT_RANGE, 1, G_MAXINT, NULL);
332 /* append caps with full range to existing caps with non-range rate field */
333 gst_caps_append_structure (res, gst_structure_copy (s));
334 gst_structure_set (s, "rate", GST_TYPE_INT_RANGE, 1, G_MAXINT, NULL);
336 gst_caps_append_structure (res, s);
340 GstCaps *intersection;
343 gst_caps_intersect_full (filter, res, GST_CAPS_INTERSECT_FIRST);
344 gst_caps_unref (res);
351 /* Fixate rate to the allowed rate that has the smallest difference */
353 gst_audio_resample_fixate_caps (GstBaseTransform * base,
354 GstPadDirection direction, GstCaps * caps, GstCaps * othercaps)
359 s = gst_caps_get_structure (caps, 0);
360 if (G_UNLIKELY (!gst_structure_get_int (s, "rate", &rate)))
363 othercaps = gst_caps_truncate (othercaps);
364 othercaps = gst_caps_make_writable (othercaps);
365 s = gst_caps_get_structure (othercaps, 0);
366 gst_structure_fixate_field_nearest_int (s, "rate", rate);
368 return gst_caps_fixate (othercaps);
371 static GstStructure *
372 make_options (GstAudioResample * resample, GstAudioInfo * in,
375 GstStructure *options;
377 options = gst_structure_new_empty ("resampler-options");
378 if (in != NULL && out != NULL)
379 gst_audio_resampler_options_set_quality (resample->method,
380 resample->quality, in->rate, out->rate, options);
382 gst_structure_set (options,
383 GST_AUDIO_CONVERTER_OPT_RESAMPLER_METHOD, GST_TYPE_AUDIO_RESAMPLER_METHOD,
385 GST_AUDIO_RESAMPLER_OPT_FILTER_MODE, GST_TYPE_AUDIO_RESAMPLER_FILTER_MODE,
386 resample->sinc_filter_mode, GST_AUDIO_RESAMPLER_OPT_FILTER_MODE_THRESHOLD,
387 G_TYPE_UINT, resample->sinc_filter_auto_threshold,
388 GST_AUDIO_RESAMPLER_OPT_FILTER_INTERPOLATION,
389 GST_TYPE_AUDIO_RESAMPLER_FILTER_INTERPOLATION,
390 resample->sinc_filter_interpolation, NULL);
396 gst_audio_resample_update_state (GstAudioResample * resample, GstAudioInfo * in,
399 gboolean updated_latency = FALSE;
400 gsize old_latency = -1;
401 GstStructure *options;
403 if (resample->converter == NULL && in == NULL && out == NULL)
406 options = make_options (resample, in, out);
408 if (resample->converter)
409 old_latency = gst_audio_converter_get_max_latency (resample->converter);
411 /* if channels and layout changed, destroy existing resampler */
412 if (in != NULL && (in->finfo != resample->in.finfo ||
413 in->channels != resample->in.channels ||
414 in->layout != resample->in.layout) && resample->converter) {
415 gst_audio_converter_free (resample->converter);
416 resample->converter = NULL;
418 if (resample->converter == NULL) {
419 resample->converter =
420 gst_audio_converter_new (GST_AUDIO_CONVERTER_FLAG_VARIABLE_RATE, in,
422 if (resample->converter == NULL)
423 goto resampler_failed;
424 } else if (in && out) {
428 gst_audio_converter_update_config (resample->converter, in->rate,
433 gst_structure_free (options);
435 if (old_latency != -1)
438 gst_audio_converter_get_max_latency (resample->converter);
441 gst_element_post_message (GST_ELEMENT (resample),
442 gst_message_new_latency (GST_OBJECT (resample)));
449 GST_ERROR_OBJECT (resample, "failed to create resampler");
454 GST_ERROR_OBJECT (resample, "failed to update resampler");
460 gst_audio_resample_reset_state (GstAudioResample * resample)
462 if (resample->converter)
463 gst_audio_converter_reset (resample->converter);
467 gst_audio_resample_transform_size (GstBaseTransform * base,
468 GstPadDirection direction, GstCaps * caps, gsize size, GstCaps * othercaps,
471 GstAudioResample *resample = GST_AUDIO_RESAMPLE (base);
475 GST_LOG_OBJECT (base, "asked to transform size %" G_GSIZE_FORMAT
476 " in direction %s", size, direction == GST_PAD_SINK ? "SINK" : "SRC");
478 /* Number of samples in either buffer is size / (width*channels) ->
479 * calculate the factor */
480 bpf = GST_AUDIO_INFO_BPF (&resample->in);
482 /* Convert source buffer size to samples */
485 if (direction == GST_PAD_SINK) {
486 /* asked to convert size of an incoming buffer */
487 *othersize = gst_audio_converter_get_out_frames (resample->converter, size);
490 /* asked to convert size of an outgoing buffer */
491 *othersize = gst_audio_converter_get_in_frames (resample->converter, size);
495 GST_LOG_OBJECT (base,
496 "transformed size %" G_GSIZE_FORMAT " to %" G_GSIZE_FORMAT,
497 size * bpf, *othersize);
503 gst_audio_resample_set_caps (GstBaseTransform * base, GstCaps * incaps,
506 GstAudioResample *resample = GST_AUDIO_RESAMPLE (base);
507 GstAudioInfo in, out;
509 GST_LOG ("incaps %" GST_PTR_FORMAT ", outcaps %"
510 GST_PTR_FORMAT, incaps, outcaps);
512 if (!gst_audio_info_from_caps (&in, incaps))
514 if (!gst_audio_info_from_caps (&out, outcaps))
515 goto invalid_outcaps;
517 /* Reset timestamp tracking and drain the resampler if the audio format is
518 * changing. Especially when changing the sample rate our timestamp tracking
519 * will be completely off, but even otherwise we would usually lose the last
520 * few samples if we don't drain here */
521 if (!gst_audio_info_is_equal (&in, &resample->in) ||
522 !gst_audio_info_is_equal (&out, &resample->out)) {
523 if (resample->converter) {
524 gsize latency = gst_audio_converter_get_max_latency (resample->converter);
525 gst_audio_resample_push_drain (resample, latency);
527 gst_audio_resample_reset_state (resample);
528 resample->num_gap_samples = 0;
529 resample->num_nongap_samples = 0;
530 resample->t0 = GST_CLOCK_TIME_NONE;
531 resample->in_offset0 = GST_BUFFER_OFFSET_NONE;
532 resample->out_offset0 = GST_BUFFER_OFFSET_NONE;
533 resample->samples_in = 0;
534 resample->samples_out = 0;
535 resample->need_discont = TRUE;
538 gst_audio_resample_update_state (resample, &in, &out);
548 GST_ERROR_OBJECT (base, "invalid incaps");
553 GST_ERROR_OBJECT (base, "invalid outcaps");
558 /* Push history_len zeros into the filter, but discard the output. */
560 gst_audio_resample_dump_drain (GstAudioResample * resample, guint history_len)
562 gsize out_len, outsize;
567 gst_audio_converter_get_out_frames (resample->converter, history_len);
571 outsize = out_len * resample->out.bpf;
572 outbuf = gst_buffer_new_and_alloc (outsize);
574 if (GST_AUDIO_INFO_LAYOUT (&resample->out) ==
575 GST_AUDIO_LAYOUT_NON_INTERLEAVED) {
576 gst_buffer_add_audio_meta (outbuf, &resample->out, out_len, NULL);
579 gst_audio_buffer_map (&abuf, &resample->out, outbuf, GST_MAP_WRITE);
580 gst_audio_converter_samples (resample->converter, 0, NULL, history_len,
581 abuf.planes, out_len);
582 gst_audio_buffer_unmap (&abuf);
584 gst_buffer_unref (outbuf);
588 gst_audio_resample_push_drain (GstAudioResample * resample, guint history_len)
596 g_assert (resample->converter != NULL);
598 /* Don't drain samples if we were reset. */
599 if (!GST_CLOCK_TIME_IS_VALID (resample->t0))
603 gst_audio_converter_get_out_frames (resample->converter, history_len);
607 outsize = out_len * resample->in.bpf;
608 outbuf = gst_buffer_new_and_alloc (outsize);
610 if (GST_AUDIO_INFO_LAYOUT (&resample->out) ==
611 GST_AUDIO_LAYOUT_NON_INTERLEAVED) {
612 gst_buffer_add_audio_meta (outbuf, &resample->out, out_len, NULL);
615 gst_audio_buffer_map (&abuf, &resample->out, outbuf, GST_MAP_WRITE);
616 gst_audio_converter_samples (resample->converter, 0, NULL, history_len,
617 abuf.planes, out_len);
618 gst_audio_buffer_unmap (&abuf);
621 if (GST_CLOCK_TIME_IS_VALID (resample->t0)) {
622 GST_BUFFER_TIMESTAMP (outbuf) = resample->t0 +
623 gst_util_uint64_scale_int_round (resample->samples_out, GST_SECOND,
625 GST_BUFFER_DURATION (outbuf) = resample->t0 +
626 gst_util_uint64_scale_int_round (resample->samples_out + out_len,
627 GST_SECOND, resample->out.rate) - GST_BUFFER_TIMESTAMP (outbuf);
629 GST_BUFFER_TIMESTAMP (outbuf) = GST_CLOCK_TIME_NONE;
630 GST_BUFFER_DURATION (outbuf) = GST_CLOCK_TIME_NONE;
633 if (resample->out_offset0 != GST_BUFFER_OFFSET_NONE) {
634 GST_BUFFER_OFFSET (outbuf) = resample->out_offset0 + resample->samples_out;
635 GST_BUFFER_OFFSET_END (outbuf) = GST_BUFFER_OFFSET (outbuf) + out_len;
637 GST_BUFFER_OFFSET (outbuf) = GST_BUFFER_OFFSET_NONE;
638 GST_BUFFER_OFFSET_END (outbuf) = GST_BUFFER_OFFSET_NONE;
641 resample->samples_out += out_len;
642 resample->samples_in += history_len;
644 GST_LOG_OBJECT (resample,
645 "Pushing drain buffer of %u bytes with timestamp %" GST_TIME_FORMAT
646 " duration %" GST_TIME_FORMAT " offset %" G_GUINT64_FORMAT " offset_end %"
647 G_GUINT64_FORMAT, outsize,
648 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)),
649 GST_TIME_ARGS (GST_BUFFER_DURATION (outbuf)), GST_BUFFER_OFFSET (outbuf),
650 GST_BUFFER_OFFSET_END (outbuf));
652 res = gst_pad_push (GST_BASE_TRANSFORM_SRC_PAD (resample), outbuf);
654 if (G_UNLIKELY (res != GST_FLOW_OK))
655 GST_WARNING_OBJECT (resample, "Failed to push drain: %s",
656 gst_flow_get_name (res));
662 gst_audio_resample_sink_event (GstBaseTransform * base, GstEvent * event)
664 GstAudioResample *resample = GST_AUDIO_RESAMPLE (base);
666 switch (GST_EVENT_TYPE (event)) {
667 case GST_EVENT_FLUSH_STOP:
668 gst_audio_resample_reset_state (resample);
669 resample->num_gap_samples = 0;
670 resample->num_nongap_samples = 0;
671 resample->t0 = GST_CLOCK_TIME_NONE;
672 resample->in_offset0 = GST_BUFFER_OFFSET_NONE;
673 resample->out_offset0 = GST_BUFFER_OFFSET_NONE;
674 resample->samples_in = 0;
675 resample->samples_out = 0;
676 resample->need_discont = TRUE;
678 case GST_EVENT_STREAM_START:
679 case GST_EVENT_SEGMENT:
681 if (resample->converter) {
683 gst_audio_converter_get_max_latency (resample->converter);
684 gst_audio_resample_push_drain (resample, latency);
686 gst_audio_resample_reset_state (resample);
687 resample->num_gap_samples = 0;
688 resample->num_nongap_samples = 0;
689 resample->t0 = GST_CLOCK_TIME_NONE;
690 resample->in_offset0 = GST_BUFFER_OFFSET_NONE;
691 resample->out_offset0 = GST_BUFFER_OFFSET_NONE;
692 resample->samples_in = 0;
693 resample->samples_out = 0;
694 resample->need_discont = TRUE;
700 return GST_BASE_TRANSFORM_CLASS (parent_class)->sink_event (base, event);
704 gst_audio_resample_check_discont (GstAudioResample * resample, GstBuffer * buf)
709 /* is the incoming buffer a discontinuity? */
710 if (G_UNLIKELY (GST_BUFFER_IS_DISCONT (buf)))
713 /* no valid timestamps or offsets to compare --> no discontinuity */
714 if (G_UNLIKELY (!(GST_BUFFER_TIMESTAMP_IS_VALID (buf) &&
715 GST_CLOCK_TIME_IS_VALID (resample->t0))))
718 /* convert the inbound timestamp to an offset. */
720 gst_util_uint64_scale_int_round (GST_BUFFER_TIMESTAMP (buf) -
721 resample->t0, resample->in.rate, GST_SECOND);
723 /* many elements generate imperfect streams due to rounding errors, so we
724 * permit a small error (up to one sample) without triggering a filter
725 * flush/restart (if triggered incorrectly, this will be audible) */
726 /* allow even up to more samples, since sink is not so strict anyway,
727 * so give that one a chance to handle this as configured */
728 delta = ABS ((gint64) (offset - resample->samples_in));
729 if (delta <= (resample->in.rate >> 5))
732 GST_WARNING_OBJECT (resample,
733 "encountered timestamp discontinuity of %" G_GUINT64_FORMAT " samples = %"
734 GST_TIME_FORMAT, delta,
735 GST_TIME_ARGS (gst_util_uint64_scale_int_round (delta, GST_SECOND,
736 resample->in.rate)));
741 gst_audio_resample_process (GstAudioResample * resample, GstBuffer * inbuf,
744 GstAudioBuffer srcabuf, dstabuf;
749 gst_audio_converter_get_max_latency (resample->converter) * 2;
750 gboolean inbuf_writable;
752 inbuf_writable = gst_buffer_is_writable (inbuf)
753 && gst_buffer_n_memory (inbuf) == 1
754 && gst_memory_is_writable (gst_buffer_peek_memory (inbuf, 0));
756 gst_audio_buffer_map (&srcabuf, &resample->in, inbuf,
757 inbuf_writable ? GST_MAP_READWRITE : GST_MAP_READ);
759 in_len = srcabuf.n_samples;
760 out_len = gst_audio_converter_get_out_frames (resample->converter, in_len);
762 /* ensure that the output buffer is not bigger than what we need */
763 gst_buffer_set_size (outbuf, out_len * resample->in.bpf);
765 if (GST_AUDIO_INFO_LAYOUT (&resample->out) ==
766 GST_AUDIO_LAYOUT_NON_INTERLEAVED) {
767 gst_buffer_add_audio_meta (outbuf, &resample->out, out_len, NULL);
770 gst_audio_buffer_map (&dstabuf, &resample->out, outbuf, GST_MAP_WRITE);
772 if (GST_BUFFER_FLAG_IS_SET (inbuf, GST_BUFFER_FLAG_GAP)) {
773 resample->num_nongap_samples = 0;
774 if (resample->num_gap_samples < filt_len) {
776 if (in_len >= filt_len - resample->num_gap_samples)
777 zeros_to_push = filt_len - resample->num_gap_samples;
779 zeros_to_push = in_len;
781 gst_audio_resample_push_drain (resample, zeros_to_push);
782 in_len -= zeros_to_push;
783 resample->num_gap_samples += zeros_to_push;
790 num = resample->in.rate;
791 den = resample->out.rate;
793 if (resample->samples_in + in_len >= filt_len / 2)
795 gst_util_uint64_scale_int_ceil (resample->samples_in + in_len -
796 filt_len / 2, den, num) - resample->samples_out;
800 for (i = 0; i < dstabuf.n_planes; i++)
801 memset (dstabuf.planes[i], 0, GST_AUDIO_BUFFER_PLANE_SIZE (&dstabuf));
803 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_GAP);
804 resample->num_gap_samples += in_len;
806 } else { /* not a gap */
807 if (resample->num_gap_samples > filt_len) {
808 /* push in enough zeros to restore the filter to the right offset */
811 num = resample->in.rate;
813 gst_audio_resample_dump_drain (resample,
814 (resample->num_gap_samples - filt_len) % num);
816 resample->num_gap_samples = 0;
817 if (resample->num_nongap_samples < filt_len) {
818 resample->num_nongap_samples += in_len;
819 if (resample->num_nongap_samples > filt_len)
820 resample->num_nongap_samples = filt_len;
824 GstAudioConverterFlags flags;
828 flags |= GST_AUDIO_CONVERTER_FLAG_IN_WRITABLE;
830 gst_audio_converter_samples (resample->converter, flags, srcabuf.planes,
831 in_len, dstabuf.planes, out_len);
836 if (GST_CLOCK_TIME_IS_VALID (resample->t0)) {
837 GST_BUFFER_TIMESTAMP (outbuf) = resample->t0 +
838 gst_util_uint64_scale_int_round (resample->samples_out, GST_SECOND,
840 GST_BUFFER_DURATION (outbuf) = resample->t0 +
841 gst_util_uint64_scale_int_round (resample->samples_out + out_len,
842 GST_SECOND, resample->out.rate) - GST_BUFFER_TIMESTAMP (outbuf);
844 GST_BUFFER_TIMESTAMP (outbuf) = GST_CLOCK_TIME_NONE;
845 GST_BUFFER_DURATION (outbuf) = GST_CLOCK_TIME_NONE;
848 if (resample->out_offset0 != GST_BUFFER_OFFSET_NONE) {
849 GST_BUFFER_OFFSET (outbuf) = resample->out_offset0 + resample->samples_out;
850 GST_BUFFER_OFFSET_END (outbuf) = GST_BUFFER_OFFSET (outbuf) + out_len;
852 GST_BUFFER_OFFSET (outbuf) = GST_BUFFER_OFFSET_NONE;
853 GST_BUFFER_OFFSET_END (outbuf) = GST_BUFFER_OFFSET_NONE;
856 resample->samples_out += out_len;
857 resample->samples_in += in_len;
859 gst_audio_buffer_unmap (&srcabuf);
860 gst_audio_buffer_unmap (&dstabuf);
862 outsize = out_len * resample->in.bpf;
864 GST_LOG_OBJECT (resample,
865 "Converted to buffer of %" G_GSIZE_FORMAT
866 " samples (%" G_GSIZE_FORMAT " bytes) with timestamp %" GST_TIME_FORMAT
867 ", duration %" GST_TIME_FORMAT ", offset %" G_GUINT64_FORMAT
868 ", offset_end %" G_GUINT64_FORMAT, out_len, outsize,
869 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)),
870 GST_TIME_ARGS (GST_BUFFER_DURATION (outbuf)),
871 GST_BUFFER_OFFSET (outbuf), GST_BUFFER_OFFSET_END (outbuf));
874 return GST_BASE_TRANSFORM_FLOW_DROPPED;
880 gst_audio_resample_transform (GstBaseTransform * base, GstBuffer * inbuf,
883 GstAudioResample *resample = GST_AUDIO_RESAMPLE (base);
886 GST_LOG_OBJECT (resample, "transforming buffer of %" G_GSIZE_FORMAT " bytes,"
887 " ts %" GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT ", offset %"
888 G_GINT64_FORMAT ", offset_end %" G_GINT64_FORMAT,
889 gst_buffer_get_size (inbuf), GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (inbuf)),
890 GST_TIME_ARGS (GST_BUFFER_DURATION (inbuf)),
891 GST_BUFFER_OFFSET (inbuf), GST_BUFFER_OFFSET_END (inbuf));
893 /* check for timestamp discontinuities; flush/reset if needed, and set
894 * flag to resync timestamp and offset counters and send event
896 if (G_UNLIKELY (gst_audio_resample_check_discont (resample, inbuf))) {
897 if (resample->converter) {
898 gsize latency = gst_audio_converter_get_max_latency (resample->converter);
899 gst_audio_resample_push_drain (resample, latency);
902 gst_audio_resample_reset_state (resample);
903 resample->need_discont = TRUE;
906 /* handle discontinuity */
907 if (G_UNLIKELY (resample->need_discont)) {
908 resample->num_gap_samples = 0;
909 resample->num_nongap_samples = 0;
911 resample->samples_in = 0;
912 resample->samples_out = 0;
913 GST_DEBUG_OBJECT (resample, "found discontinuity; resyncing");
914 /* resync the timestamp and offset counters if possible */
915 if (GST_BUFFER_TIMESTAMP_IS_VALID (inbuf)) {
916 resample->t0 = GST_BUFFER_TIMESTAMP (inbuf);
918 GST_DEBUG_OBJECT (resample, "... but new timestamp is invalid");
919 resample->t0 = GST_CLOCK_TIME_NONE;
921 if (GST_BUFFER_OFFSET_IS_VALID (inbuf)) {
922 resample->in_offset0 = GST_BUFFER_OFFSET (inbuf);
923 resample->out_offset0 =
924 gst_util_uint64_scale_int_round (resample->in_offset0,
925 resample->out.rate, resample->in.rate);
927 GST_DEBUG_OBJECT (resample, "... but new offset is invalid");
928 resample->in_offset0 = GST_BUFFER_OFFSET_NONE;
929 resample->out_offset0 = GST_BUFFER_OFFSET_NONE;
931 /* set DISCONT flag on output buffer */
932 GST_DEBUG_OBJECT (resample, "marking this buffer with the DISCONT flag");
933 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
934 resample->need_discont = FALSE;
937 ret = gst_audio_resample_process (resample, inbuf, outbuf);
938 if (G_UNLIKELY (ret != GST_FLOW_OK))
941 GST_DEBUG_OBJECT (resample, "input = samples [%" G_GUINT64_FORMAT ", %"
942 G_GUINT64_FORMAT ") = [%" G_GUINT64_FORMAT ", %" G_GUINT64_FORMAT
943 ") ns; output = samples [%" G_GUINT64_FORMAT ", %" G_GUINT64_FORMAT
944 ") = [%" G_GUINT64_FORMAT ", %" G_GUINT64_FORMAT ") ns",
945 GST_BUFFER_OFFSET (inbuf), GST_BUFFER_OFFSET_END (inbuf),
946 GST_BUFFER_TIMESTAMP (inbuf), GST_BUFFER_TIMESTAMP (inbuf) +
947 GST_BUFFER_DURATION (inbuf), GST_BUFFER_OFFSET (outbuf),
948 GST_BUFFER_OFFSET_END (outbuf), GST_BUFFER_TIMESTAMP (outbuf),
949 GST_BUFFER_TIMESTAMP (outbuf) + GST_BUFFER_DURATION (outbuf));
955 gst_audio_resample_transform_meta (GstBaseTransform * trans, GstBuffer * outbuf,
956 GstMeta * meta, GstBuffer * inbuf)
958 const GstMetaInfo *info = meta->info;
959 const gchar *const *tags;
961 tags = gst_meta_api_type_get_tags (info->api);
963 if (!tags || (g_strv_length ((gchar **) tags) == 1
964 && gst_meta_api_type_has_tag (info->api, META_TAG_AUDIO)))
971 gst_audio_resample_submit_input_buffer (GstBaseTransform * base,
972 gboolean is_discont, GstBuffer * input)
974 GstAudioResample *resample = GST_AUDIO_RESAMPLE (base);
976 if (base->segment.format == GST_FORMAT_TIME) {
978 gst_audio_buffer_clip (input, &base->segment, resample->in.rate,
985 return GST_BASE_TRANSFORM_CLASS (parent_class)->submit_input_buffer (base,
990 gst_audio_resample_query (GstPad * pad, GstObject * parent, GstQuery * query)
992 GstAudioResample *resample = GST_AUDIO_RESAMPLE (parent);
993 GstBaseTransform *trans;
996 trans = GST_BASE_TRANSFORM (resample);
998 switch (GST_QUERY_TYPE (query)) {
999 case GST_QUERY_LATENCY:
1001 GstClockTime min, max;
1004 gint rate = resample->in.rate;
1005 gint resampler_latency;
1007 if (resample->converter)
1009 gst_audio_converter_get_max_latency (resample->converter);
1011 resampler_latency = 0;
1013 if (gst_base_transform_is_passthrough (trans))
1014 resampler_latency = 0;
1017 gst_pad_peer_query (GST_BASE_TRANSFORM_SINK_PAD (trans),
1019 gst_query_parse_latency (query, &live, &min, &max);
1021 GST_DEBUG_OBJECT (resample, "Peer latency: min %"
1022 GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
1023 GST_TIME_ARGS (min), GST_TIME_ARGS (max));
1025 /* add our own latency */
1026 if (rate != 0 && resampler_latency != 0)
1027 latency = gst_util_uint64_scale_round (resampler_latency,
1032 GST_DEBUG_OBJECT (resample, "Our latency: %" GST_TIME_FORMAT,
1033 GST_TIME_ARGS (latency));
1036 if (GST_CLOCK_TIME_IS_VALID (max))
1039 GST_DEBUG_OBJECT (resample, "Calculated total latency : min %"
1040 GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
1041 GST_TIME_ARGS (min), GST_TIME_ARGS (max));
1043 gst_query_set_latency (query, live, min, max);
1048 res = gst_pad_query_default (pad, parent, query);
1055 gst_audio_resample_set_property (GObject * object, guint prop_id,
1056 const GValue * value, GParamSpec * pspec)
1058 GstAudioResample *resample;
1060 resample = GST_AUDIO_RESAMPLE (object);
1064 /* FIXME locking! */
1065 resample->quality = g_value_get_int (value);
1066 GST_DEBUG_OBJECT (resample, "new quality %d", resample->quality);
1067 gst_audio_resample_update_state (resample, NULL, NULL);
1069 case PROP_RESAMPLE_METHOD:
1070 resample->method = g_value_get_enum (value);
1071 gst_audio_resample_update_state (resample, NULL, NULL);
1073 case PROP_SINC_FILTER_MODE:
1074 /* FIXME locking! */
1075 resample->sinc_filter_mode = g_value_get_enum (value);
1076 gst_audio_resample_update_state (resample, NULL, NULL);
1078 case PROP_SINC_FILTER_AUTO_THRESHOLD:
1079 /* FIXME locking! */
1080 resample->sinc_filter_auto_threshold = g_value_get_uint (value);
1081 gst_audio_resample_update_state (resample, NULL, NULL);
1083 case PROP_SINC_FILTER_INTERPOLATION:
1084 /* FIXME locking! */
1085 resample->sinc_filter_interpolation = g_value_get_enum (value);
1086 gst_audio_resample_update_state (resample, NULL, NULL);
1089 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1095 gst_audio_resample_get_property (GObject * object, guint prop_id,
1096 GValue * value, GParamSpec * pspec)
1098 GstAudioResample *resample;
1100 resample = GST_AUDIO_RESAMPLE (object);
1104 g_value_set_int (value, resample->quality);
1106 case PROP_RESAMPLE_METHOD:
1107 g_value_set_enum (value, resample->method);
1109 case PROP_SINC_FILTER_MODE:
1110 g_value_set_enum (value, resample->sinc_filter_mode);
1112 case PROP_SINC_FILTER_AUTO_THRESHOLD:
1113 g_value_set_uint (value, resample->sinc_filter_auto_threshold);
1115 case PROP_SINC_FILTER_INTERPOLATION:
1116 g_value_set_enum (value, resample->sinc_filter_interpolation);
1119 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1125 plugin_init (GstPlugin * plugin)
1127 return GST_ELEMENT_REGISTER (audioresample, plugin);
1130 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1133 "Resamples audio", plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME,
1134 GST_PACKAGE_ORIGIN);