• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer
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>
5  *
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.
10  *
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.
15  *
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.
20  */
21 
22 /**
23  * SECTION:element-audioresample
24  * @title: audioresample
25  *
26  * audioresample resamples raw audio buffers to different sample rates using
27  * a configurable windowing function to enhance quality.
28  *
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.
36  *
37  * ## Example launch line
38  * |[
39  * gst-launch-1.0 -v uridecodebin uri=file:///path/to/audio.ogg ! audioconvert ! audioresample ! audio/x-raw, rate=8000 ! autoaudiosink
40  * ]|
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.
44  *
45  */
46 
47 /* TODO:
48  *  - Enable SSE/ARM optimizations and select at runtime
49  */
50 
51 #ifdef HAVE_CONFIG_H
52 #include "config.h"
53 #endif
54 
55 #include <string.h>
56 #include <math.h>
57 
58 #include "gstaudioresample.h"
59 #include <gst/gstutils.h>
60 #include <gst/audio/audio.h>
61 #include <gst/base/gstbasetransform.h>
62 
63 GST_DEBUG_CATEGORY (audio_resample_debug);
64 #define GST_CAT_DEFAULT audio_resample_debug
65 
66 #undef USE_SPEEX
67 
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
73 
74 enum
75 {
76   PROP_0,
77   PROP_QUALITY,
78   PROP_RESAMPLE_METHOD,
79   PROP_SINC_FILTER_MODE,
80   PROP_SINC_FILTER_AUTO_THRESHOLD,
81   PROP_SINC_FILTER_INTERPOLATION
82 };
83 
84 #define SUPPORTED_CAPS \
85   GST_AUDIO_CAPS_MAKE (GST_AUDIO_FORMATS_ALL) \
86   ", layout = (string) { interleaved, non-interleaved }"
87 
88 static GstStaticPadTemplate gst_audio_resample_sink_template =
89 GST_STATIC_PAD_TEMPLATE ("sink",
90     GST_PAD_SINK,
91     GST_PAD_ALWAYS,
92     GST_STATIC_CAPS (SUPPORTED_CAPS));
93 
94 static GstStaticPadTemplate gst_audio_resample_src_template =
95 GST_STATIC_PAD_TEMPLATE ("src",
96     GST_PAD_SRC,
97     GST_PAD_ALWAYS,
98     GST_STATIC_CAPS (SUPPORTED_CAPS));
99 
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;
103 
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);
108 
109 /* vmethods */
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,
128     GstEvent * event);
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,
132     GstQuery * query);
133 
134 static void gst_audio_resample_push_drain (GstAudioResample * resample,
135     guint history_len);
136 
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"));
143 static void
gst_audio_resample_class_init(GstAudioResampleClass * klass)144 gst_audio_resample_class_init (GstAudioResampleClass * klass)
145 {
146   GObjectClass *gobject_class = (GObjectClass *) klass;
147   GstElementClass *gstelement_class = (GstElementClass *) klass;
148 
149   gobject_class->set_property = gst_audio_resample_set_property;
150   gobject_class->get_property = gst_audio_resample_get_property;
151 
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,
156           DEFAULT_QUALITY,
157           G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
158 
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));
170 
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));
186 
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);
191 
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>");
195 
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);
218 
219   GST_BASE_TRANSFORM_CLASS (klass)->passthrough_on_same_caps = TRUE;
220 
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,
223       0);
224   gst_type_mark_as_plugin_api (GST_TYPE_AUDIO_RESAMPLER_FILTER_MODE, 0);
225 
226   meta_tag_audio_quark = g_quark_from_static_string (GST_META_TAG_AUDIO_STR);
227 }
228 
229 static void
gst_audio_resample_init(GstAudioResample * resample)230 gst_audio_resample_init (GstAudioResample * resample)
231 {
232   GstBaseTransform *trans = GST_BASE_TRANSFORM (resample);
233 
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;
239 
240   gst_base_transform_set_gap_aware (trans, TRUE);
241   gst_pad_set_query_function (trans->srcpad, gst_audio_resample_query);
242 }
243 
244 /* vmethods */
245 static gboolean
gst_audio_resample_start(GstBaseTransform * base)246 gst_audio_resample_start (GstBaseTransform * base)
247 {
248   GstAudioResample *resample = GST_AUDIO_RESAMPLE (base);
249 
250   resample->need_discont = TRUE;
251 
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;
259 
260   return TRUE;
261 }
262 
263 static gboolean
gst_audio_resample_stop(GstBaseTransform * base)264 gst_audio_resample_stop (GstBaseTransform * base)
265 {
266   GstAudioResample *resample = GST_AUDIO_RESAMPLE (base);
267 
268   if (resample->converter) {
269     gst_audio_converter_free (resample->converter);
270     resample->converter = NULL;
271   }
272   return TRUE;
273 }
274 
275 static gboolean
gst_audio_resample_get_unit_size(GstBaseTransform * base,GstCaps * caps,gsize * size)276 gst_audio_resample_get_unit_size (GstBaseTransform * base, GstCaps * caps,
277     gsize * size)
278 {
279   GstAudioInfo info;
280 
281   if (!gst_audio_info_from_caps (&info, caps))
282     goto invalid_caps;
283 
284   *size = GST_AUDIO_INFO_BPF (&info);
285 
286   return TRUE;
287 
288   /* ERRORS */
289 invalid_caps:
290   {
291     GST_ERROR_OBJECT (base, "invalid caps");
292     return FALSE;
293   }
294 }
295 
296 static GstCaps *
gst_audio_resample_transform_caps(GstBaseTransform * base,GstPadDirection direction,GstCaps * caps,GstCaps * filter)297 gst_audio_resample_transform_caps (GstBaseTransform * base,
298     GstPadDirection direction, GstCaps * caps, GstCaps * filter)
299 {
300   const GValue *val;
301   GstStructure *s;
302   GstCaps *res;
303   gint i, n;
304 
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);
312 
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))
316       continue;
317 
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);
331     } else {
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);
335     }
336     gst_caps_append_structure (res, s);
337   }
338 
339   if (filter) {
340     GstCaps *intersection;
341 
342     intersection =
343         gst_caps_intersect_full (filter, res, GST_CAPS_INTERSECT_FIRST);
344     gst_caps_unref (res);
345     res = intersection;
346   }
347 
348   return res;
349 }
350 
351 /* Fixate rate to the allowed rate that has the smallest difference */
352 static GstCaps *
gst_audio_resample_fixate_caps(GstBaseTransform * base,GstPadDirection direction,GstCaps * caps,GstCaps * othercaps)353 gst_audio_resample_fixate_caps (GstBaseTransform * base,
354     GstPadDirection direction, GstCaps * caps, GstCaps * othercaps)
355 {
356   GstStructure *s;
357   gint rate;
358 
359   s = gst_caps_get_structure (caps, 0);
360   if (G_UNLIKELY (!gst_structure_get_int (s, "rate", &rate)))
361     return othercaps;
362 
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);
367 
368   return gst_caps_fixate (othercaps);
369 }
370 
371 static GstStructure *
make_options(GstAudioResample * resample,GstAudioInfo * in,GstAudioInfo * out)372 make_options (GstAudioResample * resample, GstAudioInfo * in,
373     GstAudioInfo * out)
374 {
375   GstStructure *options;
376 
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);
381 
382   gst_structure_set (options,
383       GST_AUDIO_CONVERTER_OPT_RESAMPLER_METHOD, GST_TYPE_AUDIO_RESAMPLER_METHOD,
384       resample->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);
391 
392   return options;
393 }
394 
395 static gboolean
gst_audio_resample_update_state(GstAudioResample * resample,GstAudioInfo * in,GstAudioInfo * out)396 gst_audio_resample_update_state (GstAudioResample * resample, GstAudioInfo * in,
397     GstAudioInfo * out)
398 {
399   gboolean updated_latency = FALSE;
400   gsize old_latency = -1;
401   GstStructure *options;
402 
403   if (resample->converter == NULL && in == NULL && out == NULL)
404     return TRUE;
405 
406   options = make_options (resample, in, out);
407 
408   if (resample->converter)
409     old_latency = gst_audio_converter_get_max_latency (resample->converter);
410 
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;
417   }
418   if (resample->converter == NULL) {
419     resample->converter =
420         gst_audio_converter_new (GST_AUDIO_CONVERTER_FLAG_VARIABLE_RATE, in,
421         out, options);
422     if (resample->converter == NULL)
423       goto resampler_failed;
424   } else if (in && out) {
425     gboolean ret;
426 
427     ret =
428         gst_audio_converter_update_config (resample->converter, in->rate,
429         out->rate, options);
430     if (!ret)
431       goto update_failed;
432   } else {
433     gst_structure_free (options);
434   }
435   if (old_latency != -1)
436     updated_latency =
437         old_latency !=
438         gst_audio_converter_get_max_latency (resample->converter);
439 
440   if (updated_latency)
441     gst_element_post_message (GST_ELEMENT (resample),
442         gst_message_new_latency (GST_OBJECT (resample)));
443 
444   return TRUE;
445 
446   /* ERRORS */
447 resampler_failed:
448   {
449     GST_ERROR_OBJECT (resample, "failed to create resampler");
450     return FALSE;
451   }
452 update_failed:
453   {
454     GST_ERROR_OBJECT (resample, "failed to update resampler");
455     return FALSE;
456   }
457 }
458 
459 static void
gst_audio_resample_reset_state(GstAudioResample * resample)460 gst_audio_resample_reset_state (GstAudioResample * resample)
461 {
462   if (resample->converter)
463     gst_audio_converter_reset (resample->converter);
464 }
465 
466 static gboolean
gst_audio_resample_transform_size(GstBaseTransform * base,GstPadDirection direction,GstCaps * caps,gsize size,GstCaps * othercaps,gsize * othersize)467 gst_audio_resample_transform_size (GstBaseTransform * base,
468     GstPadDirection direction, GstCaps * caps, gsize size, GstCaps * othercaps,
469     gsize * othersize)
470 {
471   GstAudioResample *resample = GST_AUDIO_RESAMPLE (base);
472   gboolean ret = TRUE;
473   gint bpf;
474 
475   GST_LOG_OBJECT (base, "asked to transform size %" G_GSIZE_FORMAT
476       " in direction %s", size, direction == GST_PAD_SINK ? "SINK" : "SRC");
477 
478   /* Number of samples in either buffer is size / (width*channels) ->
479    * calculate the factor */
480   bpf = GST_AUDIO_INFO_BPF (&resample->in);
481 
482   /* Convert source buffer size to samples */
483   size /= bpf;
484 
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);
488     *othersize *= bpf;
489   } else {
490     /* asked to convert size of an outgoing buffer */
491     *othersize = gst_audio_converter_get_in_frames (resample->converter, size);
492     *othersize *= bpf;
493   }
494 
495   GST_LOG_OBJECT (base,
496       "transformed size %" G_GSIZE_FORMAT " to %" G_GSIZE_FORMAT,
497       size * bpf, *othersize);
498 
499   return ret;
500 }
501 
502 static gboolean
gst_audio_resample_set_caps(GstBaseTransform * base,GstCaps * incaps,GstCaps * outcaps)503 gst_audio_resample_set_caps (GstBaseTransform * base, GstCaps * incaps,
504     GstCaps * outcaps)
505 {
506   GstAudioResample *resample = GST_AUDIO_RESAMPLE (base);
507   GstAudioInfo in, out;
508 
509   GST_LOG ("incaps %" GST_PTR_FORMAT ", outcaps %"
510       GST_PTR_FORMAT, incaps, outcaps);
511 
512   if (!gst_audio_info_from_caps (&in, incaps))
513     goto invalid_incaps;
514   if (!gst_audio_info_from_caps (&out, outcaps))
515     goto invalid_outcaps;
516 
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);
526     }
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;
536   }
537 
538   gst_audio_resample_update_state (resample, &in, &out);
539 
540   resample->in = in;
541   resample->out = out;
542 
543   return TRUE;
544 
545   /* ERROR */
546 invalid_incaps:
547   {
548     GST_ERROR_OBJECT (base, "invalid incaps");
549     return FALSE;
550   }
551 invalid_outcaps:
552   {
553     GST_ERROR_OBJECT (base, "invalid outcaps");
554     return FALSE;
555   }
556 }
557 
558 /* Push history_len zeros into the filter, but discard the output. */
559 static void
gst_audio_resample_dump_drain(GstAudioResample * resample,guint history_len)560 gst_audio_resample_dump_drain (GstAudioResample * resample, guint history_len)
561 {
562   gsize out_len, outsize;
563   GstBuffer *outbuf;
564   GstAudioBuffer abuf;
565 
566   out_len =
567       gst_audio_converter_get_out_frames (resample->converter, history_len);
568   if (out_len == 0)
569     return;
570 
571   outsize = out_len * resample->out.bpf;
572   outbuf = gst_buffer_new_and_alloc (outsize);
573 
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);
577   }
578 
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);
583 
584   gst_buffer_unref (outbuf);
585 }
586 
587 static void
gst_audio_resample_push_drain(GstAudioResample * resample,guint history_len)588 gst_audio_resample_push_drain (GstAudioResample * resample, guint history_len)
589 {
590   GstBuffer *outbuf;
591   GstFlowReturn res;
592   gint outsize;
593   gsize out_len;
594   GstAudioBuffer abuf;
595 
596   g_assert (resample->converter != NULL);
597 
598   /* Don't drain samples if we were reset. */
599   if (!GST_CLOCK_TIME_IS_VALID (resample->t0))
600     return;
601 
602   out_len =
603       gst_audio_converter_get_out_frames (resample->converter, history_len);
604   if (out_len == 0)
605     return;
606 
607   outsize = out_len * resample->in.bpf;
608   outbuf = gst_buffer_new_and_alloc (outsize);
609 
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);
613   }
614 
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);
619 
620   /* time */
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,
624         resample->out.rate);
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);
628   } else {
629     GST_BUFFER_TIMESTAMP (outbuf) = GST_CLOCK_TIME_NONE;
630     GST_BUFFER_DURATION (outbuf) = GST_CLOCK_TIME_NONE;
631   }
632   /* offset */
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;
636   } else {
637     GST_BUFFER_OFFSET (outbuf) = GST_BUFFER_OFFSET_NONE;
638     GST_BUFFER_OFFSET_END (outbuf) = GST_BUFFER_OFFSET_NONE;
639   }
640   /* move along */
641   resample->samples_out += out_len;
642   resample->samples_in += history_len;
643 
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));
651 
652   res = gst_pad_push (GST_BASE_TRANSFORM_SRC_PAD (resample), outbuf);
653 
654   if (G_UNLIKELY (res != GST_FLOW_OK))
655     GST_WARNING_OBJECT (resample, "Failed to push drain: %s",
656         gst_flow_get_name (res));
657 
658   return;
659 }
660 
661 static gboolean
gst_audio_resample_sink_event(GstBaseTransform * base,GstEvent * event)662 gst_audio_resample_sink_event (GstBaseTransform * base, GstEvent * event)
663 {
664   GstAudioResample *resample = GST_AUDIO_RESAMPLE (base);
665 
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;
677       break;
678     case GST_EVENT_STREAM_START:
679     case GST_EVENT_SEGMENT:
680     case GST_EVENT_EOS:
681       if (resample->converter) {
682         gsize latency =
683             gst_audio_converter_get_max_latency (resample->converter);
684         gst_audio_resample_push_drain (resample, latency);
685       }
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;
695       break;
696     default:
697       break;
698   }
699 
700   return GST_BASE_TRANSFORM_CLASS (parent_class)->sink_event (base, event);
701 }
702 
703 static gboolean
gst_audio_resample_check_discont(GstAudioResample * resample,GstBuffer * buf)704 gst_audio_resample_check_discont (GstAudioResample * resample, GstBuffer * buf)
705 {
706   guint64 offset;
707   guint64 delta;
708 
709   /* is the incoming buffer a discontinuity? */
710   if (G_UNLIKELY (GST_BUFFER_IS_DISCONT (buf)))
711     return TRUE;
712 
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))))
716     return FALSE;
717 
718   /* convert the inbound timestamp to an offset. */
719   offset =
720       gst_util_uint64_scale_int_round (GST_BUFFER_TIMESTAMP (buf) -
721       resample->t0, resample->in.rate, GST_SECOND);
722 
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))
730     return FALSE;
731 
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)));
737   return TRUE;
738 }
739 
740 static GstFlowReturn
gst_audio_resample_process(GstAudioResample * resample,GstBuffer * inbuf,GstBuffer * outbuf)741 gst_audio_resample_process (GstAudioResample * resample, GstBuffer * inbuf,
742     GstBuffer * outbuf)
743 {
744   GstAudioBuffer srcabuf, dstabuf;
745   gsize outsize;
746   gsize in_len;
747   gsize out_len;
748   guint filt_len =
749       gst_audio_converter_get_max_latency (resample->converter) * 2;
750   gboolean inbuf_writable;
751 
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));
755 
756   gst_audio_buffer_map (&srcabuf, &resample->in, inbuf,
757       inbuf_writable ? GST_MAP_READWRITE : GST_MAP_READ);
758 
759   in_len = srcabuf.n_samples;
760   out_len = gst_audio_converter_get_out_frames (resample->converter, in_len);
761 
762   GST_DEBUG_OBJECT (resample, "in %" G_GSIZE_FORMAT " frames, out %"
763       G_GSIZE_FORMAT " frames", in_len, out_len);
764 
765   /* ensure that the output buffer is not bigger than what we need */
766   gst_buffer_set_size (outbuf, out_len * resample->in.bpf);
767 
768   if (GST_AUDIO_INFO_LAYOUT (&resample->out) ==
769       GST_AUDIO_LAYOUT_NON_INTERLEAVED) {
770     gst_buffer_add_audio_meta (outbuf, &resample->out, out_len, NULL);
771   }
772 
773   gst_audio_buffer_map (&dstabuf, &resample->out, outbuf, GST_MAP_WRITE);
774 
775   if (GST_BUFFER_FLAG_IS_SET (inbuf, GST_BUFFER_FLAG_GAP)) {
776     resample->num_nongap_samples = 0;
777     if (resample->num_gap_samples < filt_len) {
778       guint zeros_to_push;
779       if (in_len >= filt_len - resample->num_gap_samples)
780         zeros_to_push = filt_len - resample->num_gap_samples;
781       else
782         zeros_to_push = in_len;
783 
784       gst_audio_resample_push_drain (resample, zeros_to_push);
785       in_len -= zeros_to_push;
786       resample->num_gap_samples += zeros_to_push;
787     }
788 
789     {
790       guint num, den;
791       gint i;
792 
793       num = resample->in.rate;
794       den = resample->out.rate;
795 
796       if (resample->samples_in + in_len >= filt_len / 2)
797         out_len =
798             gst_util_uint64_scale_int_ceil (resample->samples_in + in_len -
799             filt_len / 2, den, num) - resample->samples_out;
800       else
801         out_len = 0;
802 
803       for (i = 0; i < dstabuf.n_planes; i++)
804         memset (dstabuf.planes[i], 0, GST_AUDIO_BUFFER_PLANE_SIZE (&dstabuf));
805 
806       GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_GAP);
807       resample->num_gap_samples += in_len;
808     }
809   } else {                      /* not a gap */
810     if (resample->num_gap_samples > filt_len) {
811       /* push in enough zeros to restore the filter to the right offset */
812       guint num;
813 
814       num = resample->in.rate;
815 
816       gst_audio_resample_dump_drain (resample,
817           (resample->num_gap_samples - filt_len) % num);
818     }
819     resample->num_gap_samples = 0;
820     if (resample->num_nongap_samples < filt_len) {
821       resample->num_nongap_samples += in_len;
822       if (resample->num_nongap_samples > filt_len)
823         resample->num_nongap_samples = filt_len;
824     }
825     {
826       /* process */
827       GstAudioConverterFlags flags;
828 
829       flags = 0;
830       if (inbuf_writable)
831         flags |= GST_AUDIO_CONVERTER_FLAG_IN_WRITABLE;
832 
833       gst_audio_converter_samples (resample->converter, flags, srcabuf.planes,
834           in_len, dstabuf.planes, out_len);
835     }
836   }
837 
838   /* time */
839   if (GST_CLOCK_TIME_IS_VALID (resample->t0)) {
840     GST_BUFFER_TIMESTAMP (outbuf) = resample->t0 +
841         gst_util_uint64_scale_int_round (resample->samples_out, GST_SECOND,
842         resample->out.rate);
843     GST_BUFFER_DURATION (outbuf) = resample->t0 +
844         gst_util_uint64_scale_int_round (resample->samples_out + out_len,
845         GST_SECOND, resample->out.rate) - GST_BUFFER_TIMESTAMP (outbuf);
846   } else {
847     GST_BUFFER_TIMESTAMP (outbuf) = GST_CLOCK_TIME_NONE;
848     GST_BUFFER_DURATION (outbuf) = GST_CLOCK_TIME_NONE;
849   }
850   /* offset */
851   if (resample->out_offset0 != GST_BUFFER_OFFSET_NONE) {
852     GST_BUFFER_OFFSET (outbuf) = resample->out_offset0 + resample->samples_out;
853     GST_BUFFER_OFFSET_END (outbuf) = GST_BUFFER_OFFSET (outbuf) + out_len;
854   } else {
855     GST_BUFFER_OFFSET (outbuf) = GST_BUFFER_OFFSET_NONE;
856     GST_BUFFER_OFFSET_END (outbuf) = GST_BUFFER_OFFSET_NONE;
857   }
858   /* move along */
859   resample->samples_out += out_len;
860   resample->samples_in += in_len;
861 
862   gst_audio_buffer_unmap (&srcabuf);
863   gst_audio_buffer_unmap (&dstabuf);
864 
865   outsize = out_len * resample->in.bpf;
866 
867   GST_LOG_OBJECT (resample,
868       "Converted to buffer of %" G_GSIZE_FORMAT
869       " samples (%" G_GSIZE_FORMAT " bytes) with timestamp %" GST_TIME_FORMAT
870       ", duration %" GST_TIME_FORMAT ", offset %" G_GUINT64_FORMAT
871       ", offset_end %" G_GUINT64_FORMAT, out_len, outsize,
872       GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)),
873       GST_TIME_ARGS (GST_BUFFER_DURATION (outbuf)),
874       GST_BUFFER_OFFSET (outbuf), GST_BUFFER_OFFSET_END (outbuf));
875 
876   if (outsize == 0)
877     return GST_BASE_TRANSFORM_FLOW_DROPPED;
878   else
879     return GST_FLOW_OK;
880 }
881 
882 static GstFlowReturn
gst_audio_resample_transform(GstBaseTransform * base,GstBuffer * inbuf,GstBuffer * outbuf)883 gst_audio_resample_transform (GstBaseTransform * base, GstBuffer * inbuf,
884     GstBuffer * outbuf)
885 {
886   GstAudioResample *resample = GST_AUDIO_RESAMPLE (base);
887   GstFlowReturn ret;
888 
889   GST_LOG_OBJECT (resample, "transforming buffer of %" G_GSIZE_FORMAT " bytes,"
890       " ts %" GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT ", offset %"
891       G_GINT64_FORMAT ", offset_end %" G_GINT64_FORMAT,
892       gst_buffer_get_size (inbuf), GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (inbuf)),
893       GST_TIME_ARGS (GST_BUFFER_DURATION (inbuf)),
894       GST_BUFFER_OFFSET (inbuf), GST_BUFFER_OFFSET_END (inbuf));
895 
896   /* check for timestamp discontinuities;  flush/reset if needed, and set
897    * flag to resync timestamp and offset counters and send event
898    * downstream */
899   if (G_UNLIKELY (gst_audio_resample_check_discont (resample, inbuf))) {
900     if (resample->converter) {
901       gsize latency = gst_audio_converter_get_max_latency (resample->converter);
902       gst_audio_resample_push_drain (resample, latency);
903     }
904 
905     gst_audio_resample_reset_state (resample);
906     resample->need_discont = TRUE;
907   }
908 
909   /* handle discontinuity */
910   if (G_UNLIKELY (resample->need_discont)) {
911     resample->num_gap_samples = 0;
912     resample->num_nongap_samples = 0;
913     /* reset */
914     resample->samples_in = 0;
915     resample->samples_out = 0;
916     GST_DEBUG_OBJECT (resample, "found discontinuity; resyncing");
917     /* resync the timestamp and offset counters if possible */
918     if (GST_BUFFER_TIMESTAMP_IS_VALID (inbuf)) {
919       resample->t0 = GST_BUFFER_TIMESTAMP (inbuf);
920     } else {
921       GST_DEBUG_OBJECT (resample, "... but new timestamp is invalid");
922       resample->t0 = GST_CLOCK_TIME_NONE;
923     }
924     if (GST_BUFFER_OFFSET_IS_VALID (inbuf)) {
925       resample->in_offset0 = GST_BUFFER_OFFSET (inbuf);
926       resample->out_offset0 =
927           gst_util_uint64_scale_int_round (resample->in_offset0,
928           resample->out.rate, resample->in.rate);
929     } else {
930       GST_DEBUG_OBJECT (resample, "... but new offset is invalid");
931       resample->in_offset0 = GST_BUFFER_OFFSET_NONE;
932       resample->out_offset0 = GST_BUFFER_OFFSET_NONE;
933     }
934     /* set DISCONT flag on output buffer */
935     GST_DEBUG_OBJECT (resample, "marking this buffer with the DISCONT flag");
936     GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
937     resample->need_discont = FALSE;
938   }
939 
940   ret = gst_audio_resample_process (resample, inbuf, outbuf);
941   if (G_UNLIKELY (ret != GST_FLOW_OK))
942     return ret;
943 
944   GST_DEBUG_OBJECT (resample, "input = samples [%" G_GUINT64_FORMAT ", %"
945       G_GUINT64_FORMAT ") = [%" G_GUINT64_FORMAT ", %" G_GUINT64_FORMAT
946       ") ns;  output = samples [%" G_GUINT64_FORMAT ", %" G_GUINT64_FORMAT
947       ") = [%" G_GUINT64_FORMAT ", %" G_GUINT64_FORMAT ") ns",
948       GST_BUFFER_OFFSET (inbuf), GST_BUFFER_OFFSET_END (inbuf),
949       GST_BUFFER_TIMESTAMP (inbuf), GST_BUFFER_TIMESTAMP (inbuf) +
950       GST_BUFFER_DURATION (inbuf), GST_BUFFER_OFFSET (outbuf),
951       GST_BUFFER_OFFSET_END (outbuf), GST_BUFFER_TIMESTAMP (outbuf),
952       GST_BUFFER_TIMESTAMP (outbuf) + GST_BUFFER_DURATION (outbuf));
953 
954   return GST_FLOW_OK;
955 }
956 
957 static gboolean
gst_audio_resample_transform_meta(GstBaseTransform * trans,GstBuffer * outbuf,GstMeta * meta,GstBuffer * inbuf)958 gst_audio_resample_transform_meta (GstBaseTransform * trans, GstBuffer * outbuf,
959     GstMeta * meta, GstBuffer * inbuf)
960 {
961   const GstMetaInfo *info = meta->info;
962   const gchar *const *tags;
963 
964   tags = gst_meta_api_type_get_tags (info->api);
965 
966   if (!tags || (g_strv_length ((gchar **) tags) == 1
967           && gst_meta_api_type_has_tag (info->api, META_TAG_AUDIO)))
968     return TRUE;
969 
970   return FALSE;
971 }
972 
973 static GstFlowReturn
gst_audio_resample_submit_input_buffer(GstBaseTransform * base,gboolean is_discont,GstBuffer * input)974 gst_audio_resample_submit_input_buffer (GstBaseTransform * base,
975     gboolean is_discont, GstBuffer * input)
976 {
977   GstAudioResample *resample = GST_AUDIO_RESAMPLE (base);
978 
979   if (base->segment.format == GST_FORMAT_TIME) {
980     input =
981         gst_audio_buffer_clip (input, &base->segment, resample->in.rate,
982         resample->in.bpf);
983 
984     if (!input)
985       return GST_FLOW_OK;
986   }
987 
988   return GST_BASE_TRANSFORM_CLASS (parent_class)->submit_input_buffer (base,
989       is_discont, input);
990 }
991 
992 static gboolean
gst_audio_resample_query(GstPad * pad,GstObject * parent,GstQuery * query)993 gst_audio_resample_query (GstPad * pad, GstObject * parent, GstQuery * query)
994 {
995   GstAudioResample *resample = GST_AUDIO_RESAMPLE (parent);
996   GstBaseTransform *trans;
997   gboolean res = TRUE;
998 
999   trans = GST_BASE_TRANSFORM (resample);
1000 
1001   switch (GST_QUERY_TYPE (query)) {
1002     case GST_QUERY_LATENCY:
1003     {
1004       GstClockTime min, max;
1005       gboolean live;
1006       guint64 latency;
1007       gint rate = resample->in.rate;
1008       gint resampler_latency;
1009 
1010       if (resample->converter)
1011         resampler_latency =
1012             gst_audio_converter_get_max_latency (resample->converter);
1013       else
1014         resampler_latency = 0;
1015 
1016       if (gst_base_transform_is_passthrough (trans))
1017         resampler_latency = 0;
1018 
1019       if ((res =
1020               gst_pad_peer_query (GST_BASE_TRANSFORM_SINK_PAD (trans),
1021                   query))) {
1022         gst_query_parse_latency (query, &live, &min, &max);
1023 
1024         GST_DEBUG_OBJECT (resample, "Peer latency: min %"
1025             GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
1026             GST_TIME_ARGS (min), GST_TIME_ARGS (max));
1027 
1028         /* add our own latency */
1029         if (rate != 0 && resampler_latency != 0)
1030           latency = gst_util_uint64_scale_round (resampler_latency,
1031               GST_SECOND, rate);
1032         else
1033           latency = 0;
1034 
1035         GST_DEBUG_OBJECT (resample, "Our latency: %" GST_TIME_FORMAT,
1036             GST_TIME_ARGS (latency));
1037 
1038         min += latency;
1039         if (GST_CLOCK_TIME_IS_VALID (max))
1040           max += latency;
1041 
1042         GST_DEBUG_OBJECT (resample, "Calculated total latency : min %"
1043             GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
1044             GST_TIME_ARGS (min), GST_TIME_ARGS (max));
1045 
1046         gst_query_set_latency (query, live, min, max);
1047       }
1048       break;
1049     }
1050     default:
1051       res = gst_pad_query_default (pad, parent, query);
1052       break;
1053   }
1054   return res;
1055 }
1056 
1057 static void
gst_audio_resample_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)1058 gst_audio_resample_set_property (GObject * object, guint prop_id,
1059     const GValue * value, GParamSpec * pspec)
1060 {
1061   GstAudioResample *resample;
1062 
1063   resample = GST_AUDIO_RESAMPLE (object);
1064 
1065   switch (prop_id) {
1066     case PROP_QUALITY:
1067       /* FIXME locking! */
1068       resample->quality = g_value_get_int (value);
1069       GST_DEBUG_OBJECT (resample, "new quality %d", resample->quality);
1070       gst_audio_resample_update_state (resample, NULL, NULL);
1071       break;
1072     case PROP_RESAMPLE_METHOD:
1073       resample->method = g_value_get_enum (value);
1074       gst_audio_resample_update_state (resample, NULL, NULL);
1075       break;
1076     case PROP_SINC_FILTER_MODE:
1077       /* FIXME locking! */
1078       resample->sinc_filter_mode = g_value_get_enum (value);
1079       gst_audio_resample_update_state (resample, NULL, NULL);
1080       break;
1081     case PROP_SINC_FILTER_AUTO_THRESHOLD:
1082       /* FIXME locking! */
1083       resample->sinc_filter_auto_threshold = g_value_get_uint (value);
1084       gst_audio_resample_update_state (resample, NULL, NULL);
1085       break;
1086     case PROP_SINC_FILTER_INTERPOLATION:
1087       /* FIXME locking! */
1088       resample->sinc_filter_interpolation = g_value_get_enum (value);
1089       gst_audio_resample_update_state (resample, NULL, NULL);
1090       break;
1091     default:
1092       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1093       break;
1094   }
1095 }
1096 
1097 static void
gst_audio_resample_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)1098 gst_audio_resample_get_property (GObject * object, guint prop_id,
1099     GValue * value, GParamSpec * pspec)
1100 {
1101   GstAudioResample *resample;
1102 
1103   resample = GST_AUDIO_RESAMPLE (object);
1104 
1105   switch (prop_id) {
1106     case PROP_QUALITY:
1107       g_value_set_int (value, resample->quality);
1108       break;
1109     case PROP_RESAMPLE_METHOD:
1110       g_value_set_enum (value, resample->method);
1111       break;
1112     case PROP_SINC_FILTER_MODE:
1113       g_value_set_enum (value, resample->sinc_filter_mode);
1114       break;
1115     case PROP_SINC_FILTER_AUTO_THRESHOLD:
1116       g_value_set_uint (value, resample->sinc_filter_auto_threshold);
1117       break;
1118     case PROP_SINC_FILTER_INTERPOLATION:
1119       g_value_set_enum (value, resample->sinc_filter_interpolation);
1120       break;
1121     default:
1122       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1123       break;
1124   }
1125 }
1126 
1127 static gboolean
plugin_init(GstPlugin * plugin)1128 plugin_init (GstPlugin * plugin)
1129 {
1130   return GST_ELEMENT_REGISTER (audioresample, plugin);
1131 }
1132 
1133 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1134     GST_VERSION_MINOR,
1135     audioresample,
1136     "Resamples audio", plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME,
1137     GST_PACKAGE_ORIGIN);
1138