1 /* -*- c-basic-offset: 2 -*-
4 * Copyright (C) 1999-2001 Erik Walthinsen <omega@cse.ogi.edu>
5 * 2006 Dreamlab Technologies Ltd. <mathis.hofer@dreamlab.net>
6 * 2007-2009 Sebastian Dröge <sebastian.droege@collabora.co.uk>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
32 #include <gst/audio/gstaudiofilter.h>
34 #include "audiofxbasefirfilter.h"
36 #define GST_CAT_DEFAULT gst_audio_fx_base_fir_filter_debug
37 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
39 #define ALLOWED_CAPS \
41 " format=(string){"GST_AUDIO_NE(F32)","GST_AUDIO_NE(F64)"}, " \
42 " rate = (int) [ 1, MAX ], " \
43 " channels = (int) [ 1, MAX ], " \
44 " layout=(string) interleaved"
46 /* Switch from time-domain to FFT convolution for kernels >= this */
47 #define FFT_THRESHOLD 32
56 #define DEFAULT_LOW_LATENCY FALSE
57 #define DEFAULT_DRAIN_ON_CHANGES TRUE
59 #define gst_audio_fx_base_fir_filter_parent_class parent_class
60 G_DEFINE_TYPE (GstAudioFXBaseFIRFilter, gst_audio_fx_base_fir_filter,
61 GST_TYPE_AUDIO_FILTER);
63 static GstFlowReturn gst_audio_fx_base_fir_filter_transform (GstBaseTransform *
64 base, GstBuffer * inbuf, GstBuffer * outbuf);
65 static gboolean gst_audio_fx_base_fir_filter_start (GstBaseTransform * base);
66 static gboolean gst_audio_fx_base_fir_filter_stop (GstBaseTransform * base);
67 static gboolean gst_audio_fx_base_fir_filter_sink_event (GstBaseTransform *
68 base, GstEvent * event);
69 static gboolean gst_audio_fx_base_fir_filter_transform_size (GstBaseTransform *
70 base, GstPadDirection direction, GstCaps * caps, gsize size,
71 GstCaps * othercaps, gsize * othersize);
72 static gboolean gst_audio_fx_base_fir_filter_setup (GstAudioFilter * base,
73 const GstAudioInfo * info);
75 static gboolean gst_audio_fx_base_fir_filter_query (GstPad * pad,
76 GstObject * parent, GstQuery * query);
79 * The code below calculates the linear convolution:
81 * y[t] = \sum_{u=0}^{M-1} x[t - u] * h[u]
83 * where y is the output, x is the input, M is the length
84 * of the filter kernel and h is the filter kernel. For x
85 * holds: x[t] == 0 \forall t < 0.
87 * The runtime complexity of this is O (M) per sample.
90 #define DEFINE_PROCESS_FUNC(width,ctype) \
92 process_##width (GstAudioFXBaseFIRFilter * self, const g##ctype * src, g##ctype * dst, guint input_samples) \
94 gint channels = GST_AUDIO_FILTER_CHANNELS (self); \
95 TIME_DOMAIN_CONVOLUTION_BODY (channels); \
98 #define DEFINE_PROCESS_FUNC_FIXED_CHANNELS(width,channels,ctype) \
100 process_##channels##_##width (GstAudioFXBaseFIRFilter * self, const g##ctype * src, g##ctype * dst, guint input_samples) \
102 TIME_DOMAIN_CONVOLUTION_BODY (channels); \
105 #define TIME_DOMAIN_CONVOLUTION_BODY(channels) G_STMT_START { \
106 gint kernel_length = self->kernel_length; \
111 gdouble *buffer = self->buffer; \
112 gdouble *kernel = self->kernel; \
113 guint buffer_length = self->buffer_length; \
116 self->buffer_length = buffer_length = kernel_length * channels; \
117 self->buffer = buffer = g_new0 (gdouble, self->buffer_length); \
121 for (i = 0; i < input_samples; i++) { \
125 from_input = MIN (l, kernel_length-1); \
126 off = l * channels + k; \
127 for (j = 0; j <= from_input; j++) { \
128 dst[i] += src[off] * kernel[j]; \
131 /* j == from_input && off == (l - j) * channels + k */ \
132 off += kernel_length * channels; \
133 for (; j < kernel_length; j++) { \
134 dst[i] += buffer[off] * kernel[j]; \
139 /* copy the tail of the current input buffer to the residue, while \
140 * keeping parts of the residue if the input buffer is smaller than \
141 * the kernel length */ \
142 /* from now on take kernel length as length over all channels */ \
143 kernel_length *= channels; \
144 if (input_samples < kernel_length) \
145 res_start = kernel_length - input_samples; \
149 for (i = 0; i < res_start; i++) \
150 buffer[i] = buffer[i + input_samples]; \
151 /* i == res_start */ \
152 for (; i < kernel_length; i++) \
153 buffer[i] = src[input_samples - kernel_length + i]; \
155 self->buffer_fill += kernel_length - res_start; \
156 if (self->buffer_fill > kernel_length) \
157 self->buffer_fill = kernel_length; \
159 return input_samples / channels; \
162 DEFINE_PROCESS_FUNC (32, float);
163 DEFINE_PROCESS_FUNC (64, double);
165 DEFINE_PROCESS_FUNC_FIXED_CHANNELS (32, 1, float);
166 DEFINE_PROCESS_FUNC_FIXED_CHANNELS (64, 1, double);
168 DEFINE_PROCESS_FUNC_FIXED_CHANNELS (32, 2, float);
169 DEFINE_PROCESS_FUNC_FIXED_CHANNELS (64, 2, double);
171 #undef TIME_DOMAIN_CONVOLUTION_BODY
172 #undef DEFINE_PROCESS_FUNC
173 #undef DEFINE_PROCESS_FUNC_FIXED_CHANNELS
175 /* This implements FFT convolution and uses the overlap-save algorithm.
176 * See http://cnx.org/content/m12022/latest/ or your favorite
177 * digital signal processing book for details.
179 * In every pass the following is calculated:
181 * y = IFFT (FFT(x) * FFT(h))
183 * where y is the output in the time domain, x the
184 * input and h the filter kernel. * is the multiplication
185 * of complex numbers.
187 * Due to the circular convolution theorem this
188 * gives in the time domain:
190 * y[t] = \sum_{u=0}^{M-1} x[t - u] * h[u]
192 * where y is the output, M is the kernel length,
193 * x the periodically extended[0] input and h the
196 * ([0] Periodically extended means: )
197 * ( x[t] = x[t+kN] \forall k \in Z )
198 * ( where N is the length of x )
201 * - Obviously x and h need to be of the same size for the FFT
202 * - The first M-1 output values are useless because they're
203 * built from 1 up to M-1 values from the end of the input
204 * (circular convolusion!).
205 * - The last M-1 input values are only used for 1 up to M-1
206 * output values, i.e. they need to be used again in the
207 * next pass for the first M-1 input values.
209 * => The first pass needs M-1 zeroes at the beginning of the
210 * input and the last M-1 input values of every pass need to
211 * be used as the first M-1 input values of the next pass.
213 * => x must be larger than h to give a useful number of output
214 * samples and h needs to be padded by zeroes at the end to give
215 * it virtually the same size as x (by M we denote the number of
216 * non-padding samples of h). If len(x)==len(h)==M only 1 output
217 * sample would be calculated per pass, len(x)==2*len(h) would
218 * give M+1 output samples, etc. Usually a factor between 4 and 8
219 * gives a low number of operations per output samples (see website
222 * Overall this gives a runtime complexity per sample of
225 * O ( --------- ) compared to O (M) for the direct calculation.
228 #define DEFINE_FFT_PROCESS_FUNC(width,ctype) \
230 process_fft_##width (GstAudioFXBaseFIRFilter * self, const g##ctype * src, \
231 g##ctype * dst, guint input_samples) \
233 gint channels = GST_AUDIO_FILTER_CHANNELS (self); \
234 FFT_CONVOLUTION_BODY (channels); \
237 #define DEFINE_FFT_PROCESS_FUNC_FIXED_CHANNELS(width,channels,ctype) \
239 process_fft_##channels##_##width (GstAudioFXBaseFIRFilter * self, const g##ctype * src, \
240 g##ctype * dst, guint input_samples) \
242 FFT_CONVOLUTION_BODY (channels); \
245 #define FFT_CONVOLUTION_BODY(channels) G_STMT_START { \
248 guint kernel_length = self->kernel_length; \
249 guint block_length = self->block_length; \
250 guint buffer_length = self->buffer_length; \
251 guint real_buffer_length = buffer_length + kernel_length - 1; \
252 guint buffer_fill = self->buffer_fill; \
253 GstFFTF64 *fft = self->fft; \
254 GstFFTF64 *ifft = self->ifft; \
255 GstFFTF64Complex *frequency_response = self->frequency_response; \
256 GstFFTF64Complex *fft_buffer = self->fft_buffer; \
257 guint frequency_response_length = self->frequency_response_length; \
258 gdouble *buffer = self->buffer; \
259 guint generated = 0; \
263 self->fft_buffer = fft_buffer = \
264 g_new (GstFFTF64Complex, frequency_response_length); \
266 /* Buffer contains the time domain samples of input data for one chunk \
267 * plus some more space for the inverse FFT below. \
269 * The samples are put at offset kernel_length, the inverse FFT \
270 * overwrites everthing from offset 0 to length-kernel_length+1, keeping \
271 * the last kernel_length-1 samples for copying to the next processing \
275 self->buffer_length = buffer_length = block_length; \
276 real_buffer_length = buffer_length + kernel_length - 1; \
278 self->buffer = buffer = g_new0 (gdouble, real_buffer_length * channels); \
280 /* Beginning has kernel_length-1 zeroes at the beginning */ \
281 self->buffer_fill = buffer_fill = kernel_length - 1; \
284 g_assert (self->buffer_length == block_length); \
286 while (input_samples) { \
287 pass = MIN (buffer_length - buffer_fill, input_samples); \
289 /* Deinterleave channels */ \
290 for (i = 0; i < pass; i++) { \
291 for (j = 0; j < channels; j++) { \
292 buffer[real_buffer_length * j + buffer_fill + kernel_length - 1 + i] = \
293 src[i * channels + j]; \
296 buffer_fill += pass; \
297 src += channels * pass; \
298 input_samples -= pass; \
300 /* If we don't have a complete buffer go out */ \
301 if (buffer_fill < buffer_length) \
304 for (j = 0; j < channels; j++) { \
305 /* Calculate FFT of input block */ \
306 gst_fft_f64_fft (fft, \
307 buffer + real_buffer_length * j + kernel_length - 1, fft_buffer); \
309 /* Complex multiplication of input and filter spectrum */ \
310 for (i = 0; i < frequency_response_length; i++) { \
311 re = fft_buffer[i].r; \
312 im = fft_buffer[i].i; \
315 re * frequency_response[i].r - \
316 im * frequency_response[i].i; \
318 re * frequency_response[i].i + \
319 im * frequency_response[i].r; \
322 /* Calculate inverse FFT of the result */ \
323 gst_fft_f64_inverse_fft (ifft, fft_buffer, \
324 buffer + real_buffer_length * j); \
326 /* Copy all except the first kernel_length-1 samples to the output */ \
327 for (i = 0; i < buffer_length - kernel_length + 1; i++) { \
328 dst[i * channels + j] = \
329 buffer[real_buffer_length * j + kernel_length - 1 + i]; \
332 /* Copy the last kernel_length-1 samples to the beginning for the next block */ \
333 for (i = 0; i < kernel_length - 1; i++) { \
334 buffer[real_buffer_length * j + kernel_length - 1 + i] = \
335 buffer[real_buffer_length * j + buffer_length + i]; \
339 generated += buffer_length - kernel_length + 1; \
340 dst += channels * (buffer_length - kernel_length + 1); \
342 /* The the first kernel_length-1 samples are there already */ \
343 buffer_fill = kernel_length - 1; \
346 /* Write back cached buffer_fill value */ \
347 self->buffer_fill = buffer_fill; \
352 DEFINE_FFT_PROCESS_FUNC (32, float);
353 DEFINE_FFT_PROCESS_FUNC (64, double);
355 DEFINE_FFT_PROCESS_FUNC_FIXED_CHANNELS (32, 1, float);
356 DEFINE_FFT_PROCESS_FUNC_FIXED_CHANNELS (64, 1, double);
358 DEFINE_FFT_PROCESS_FUNC_FIXED_CHANNELS (32, 2, float);
359 DEFINE_FFT_PROCESS_FUNC_FIXED_CHANNELS (64, 2, double);
361 #undef FFT_CONVOLUTION_BODY
362 #undef DEFINE_FFT_PROCESS_FUNC
363 #undef DEFINE_FFT_PROCESS_FUNC_FIXED_CHANNELS
367 gst_audio_fx_base_fir_filter_calculate_frequency_response
368 (GstAudioFXBaseFIRFilter * self)
370 gst_fft_f64_free (self->fft);
372 gst_fft_f64_free (self->ifft);
374 g_free (self->frequency_response);
375 self->frequency_response_length = 0;
376 g_free (self->fft_buffer);
377 self->fft_buffer = NULL;
379 if (self->kernel && self->kernel_length >= FFT_THRESHOLD
380 && !self->low_latency) {
381 guint block_length, i;
382 gdouble *kernel_tmp, *kernel = self->kernel;
384 /* We process 4 * kernel_length samples per pass in FFT mode */
385 block_length = 4 * self->kernel_length;
386 block_length = gst_fft_next_fast_length (block_length);
387 self->block_length = block_length;
389 kernel_tmp = g_new0 (gdouble, block_length);
390 memcpy (kernel_tmp, kernel, self->kernel_length * sizeof (gdouble));
392 self->fft = gst_fft_f64_new (block_length, FALSE);
393 self->ifft = gst_fft_f64_new (block_length, TRUE);
394 self->frequency_response_length = block_length / 2 + 1;
395 self->frequency_response =
396 g_new (GstFFTF64Complex, self->frequency_response_length);
397 gst_fft_f64_fft (self->fft, kernel_tmp, self->frequency_response);
400 /* Normalize to make sure IFFT(FFT(x)) == x */
401 for (i = 0; i < self->frequency_response_length; i++) {
402 self->frequency_response[i].r /= block_length;
403 self->frequency_response[i].i /= block_length;
408 /* Must be called with base transform lock! */
410 gst_audio_fx_base_fir_filter_select_process_function (GstAudioFXBaseFIRFilter *
411 self, GstAudioFormat format, gint channels)
414 case GST_AUDIO_FORMAT_F32:
415 if (self->fft && !self->low_latency) {
417 self->process = (GstAudioFXBaseFIRFilterProcessFunc) process_fft_1_32;
418 else if (channels == 2)
419 self->process = (GstAudioFXBaseFIRFilterProcessFunc) process_fft_2_32;
421 self->process = (GstAudioFXBaseFIRFilterProcessFunc) process_fft_32;
424 self->process = (GstAudioFXBaseFIRFilterProcessFunc) process_1_32;
425 else if (channels == 2)
426 self->process = (GstAudioFXBaseFIRFilterProcessFunc) process_2_32;
428 self->process = (GstAudioFXBaseFIRFilterProcessFunc) process_32;
431 case GST_AUDIO_FORMAT_F64:
432 if (self->fft && !self->low_latency) {
434 self->process = (GstAudioFXBaseFIRFilterProcessFunc) process_fft_1_64;
435 else if (channels == 2)
436 self->process = (GstAudioFXBaseFIRFilterProcessFunc) process_fft_2_64;
438 self->process = (GstAudioFXBaseFIRFilterProcessFunc) process_fft_64;
441 self->process = (GstAudioFXBaseFIRFilterProcessFunc) process_1_64;
442 else if (channels == 2)
443 self->process = (GstAudioFXBaseFIRFilterProcessFunc) process_2_64;
445 self->process = (GstAudioFXBaseFIRFilterProcessFunc) process_64;
449 self->process = NULL;
455 gst_audio_fx_base_fir_filter_dispose (GObject * object)
457 GstAudioFXBaseFIRFilter *self = GST_AUDIO_FX_BASE_FIR_FILTER (object);
459 g_free (self->buffer);
461 self->buffer_length = 0;
463 g_free (self->kernel);
466 gst_fft_f64_free (self->fft);
468 gst_fft_f64_free (self->ifft);
471 g_free (self->frequency_response);
472 self->frequency_response = NULL;
474 g_free (self->fft_buffer);
475 self->fft_buffer = NULL;
477 G_OBJECT_CLASS (parent_class)->dispose (object);
481 gst_audio_fx_base_fir_filter_set_property (GObject * object, guint prop_id,
482 const GValue * value, GParamSpec * pspec)
484 GstAudioFXBaseFIRFilter *self = GST_AUDIO_FX_BASE_FIR_FILTER (object);
487 case PROP_LOW_LATENCY:{
488 gboolean low_latency;
490 if (GST_STATE (self) >= GST_STATE_PAUSED) {
491 g_warning ("Changing the \"low-latency\" property "
492 "is only allowed in states < PAUSED");
496 GST_BASE_TRANSFORM_LOCK (self);
497 low_latency = g_value_get_boolean (value);
499 if (self->low_latency != low_latency) {
500 self->low_latency = low_latency;
501 gst_audio_fx_base_fir_filter_calculate_frequency_response (self);
502 gst_audio_fx_base_fir_filter_select_process_function (self,
503 GST_AUDIO_FILTER_FORMAT (self), GST_AUDIO_FILTER_CHANNELS (self));
505 GST_BASE_TRANSFORM_UNLOCK (self);
508 case PROP_DRAIN_ON_CHANGES:{
509 GST_BASE_TRANSFORM_LOCK (self);
510 self->drain_on_changes = g_value_get_boolean (value);
511 GST_BASE_TRANSFORM_UNLOCK (self);
515 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
521 gst_audio_fx_base_fir_filter_get_property (GObject * object, guint prop_id,
522 GValue * value, GParamSpec * pspec)
524 GstAudioFXBaseFIRFilter *self = GST_AUDIO_FX_BASE_FIR_FILTER (object);
527 case PROP_LOW_LATENCY:
528 g_value_set_boolean (value, self->low_latency);
530 case PROP_DRAIN_ON_CHANGES:
531 g_value_set_boolean (value, self->drain_on_changes);
534 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
540 gst_audio_fx_base_fir_filter_class_init (GstAudioFXBaseFIRFilterClass * klass)
542 GObjectClass *gobject_class = (GObjectClass *) klass;
543 GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
544 GstAudioFilterClass *filter_class = (GstAudioFilterClass *) klass;
547 GST_DEBUG_CATEGORY_INIT (gst_audio_fx_base_fir_filter_debug,
548 "audiofxbasefirfilter", 0, "FIR filter base class");
550 gobject_class->dispose = gst_audio_fx_base_fir_filter_dispose;
551 gobject_class->set_property = gst_audio_fx_base_fir_filter_set_property;
552 gobject_class->get_property = gst_audio_fx_base_fir_filter_get_property;
555 * GstAudioFXBaseFIRFilter::low-latency:
557 * Work in low-latency mode. This mode is much slower for large filter sizes
558 * but the latency is always only the pre-latency of the filter.
562 g_object_class_install_property (gobject_class, PROP_LOW_LATENCY,
563 g_param_spec_boolean ("low-latency", "Low latency",
564 "Operate in low latency mode. This mode is slower but the "
565 "latency will only be the filter pre-latency. "
566 "Can only be changed in states < PAUSED!", DEFAULT_LOW_LATENCY,
567 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
570 * GstAudioFXBaseFIRFilter::drain-on-changes:
572 * Whether the filter should be drained when its coeficients change
574 * Note: Currently this only works if the kernel size is not changed!
575 * Support for drainless kernel size changes will be added in the future.
579 g_object_class_install_property (gobject_class, PROP_DRAIN_ON_CHANGES,
580 g_param_spec_boolean ("drain-on-changes", "Drain on changes",
581 "Drains the filter when its coeficients change",
582 DEFAULT_DRAIN_ON_CHANGES,
583 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
585 caps = gst_caps_from_string (ALLOWED_CAPS);
586 gst_audio_filter_class_add_pad_templates (GST_AUDIO_FILTER_CLASS (klass),
588 gst_caps_unref (caps);
590 trans_class->transform =
591 GST_DEBUG_FUNCPTR (gst_audio_fx_base_fir_filter_transform);
592 trans_class->start = GST_DEBUG_FUNCPTR (gst_audio_fx_base_fir_filter_start);
593 trans_class->stop = GST_DEBUG_FUNCPTR (gst_audio_fx_base_fir_filter_stop);
594 trans_class->sink_event =
595 GST_DEBUG_FUNCPTR (gst_audio_fx_base_fir_filter_sink_event);
596 trans_class->transform_size =
597 GST_DEBUG_FUNCPTR (gst_audio_fx_base_fir_filter_transform_size);
598 filter_class->setup = GST_DEBUG_FUNCPTR (gst_audio_fx_base_fir_filter_setup);
602 gst_audio_fx_base_fir_filter_init (GstAudioFXBaseFIRFilter * self)
606 self->buffer_length = 0;
608 self->start_ts = GST_CLOCK_TIME_NONE;
609 self->start_off = GST_BUFFER_OFFSET_NONE;
610 self->nsamples_out = 0;
611 self->nsamples_in = 0;
613 self->low_latency = DEFAULT_LOW_LATENCY;
614 self->drain_on_changes = DEFAULT_DRAIN_ON_CHANGES;
616 gst_pad_set_query_function (GST_BASE_TRANSFORM (self)->srcpad,
617 gst_audio_fx_base_fir_filter_query);
621 gst_audio_fx_base_fir_filter_push_residue (GstAudioFXBaseFIRFilter * self)
625 gint rate = GST_AUDIO_FILTER_RATE (self);
626 gint channels = GST_AUDIO_FILTER_CHANNELS (self);
627 gint bps = GST_AUDIO_FILTER_BPS (self);
628 gint outsize, outsamples;
632 if (channels == 0 || rate == 0 || self->nsamples_in == 0) {
633 self->buffer_fill = 0;
634 g_free (self->buffer);
639 /* Calculate the number of samples and their memory size that
640 * should be pushed from the residue */
641 outsamples = self->nsamples_in - (self->nsamples_out - self->latency);
642 if (outsamples <= 0) {
643 self->buffer_fill = 0;
644 g_free (self->buffer);
648 outsize = outsamples * channels * bps;
650 if (!self->fft || self->low_latency) {
651 gint64 diffsize, diffsamples;
653 /* Process the difference between latency and residue length samples
654 * to start at the actual data instead of starting at the zeros before
655 * when we only got one buffer smaller than latency */
657 ((gint64) self->latency) - ((gint64) self->buffer_fill) / channels;
658 if (diffsamples > 0) {
659 diffsize = diffsamples * channels * bps;
660 in = g_new0 (guint8, diffsize);
661 out = g_new0 (guint8, diffsize);
662 self->nsamples_out += self->process (self, in, out, diffsamples);
667 outbuf = gst_buffer_new_and_alloc (outsize);
669 /* Convolve the residue with zeros to get the actual remaining data */
670 in = g_new0 (guint8, outsize);
671 gst_buffer_map (outbuf, &map, GST_MAP_READWRITE);
672 self->nsamples_out += self->process (self, in, map.data, outsamples);
673 gst_buffer_unmap (outbuf, &map);
677 guint gensamples = 0;
679 outbuf = gst_buffer_new_and_alloc (outsize);
680 gst_buffer_map (outbuf, &map, GST_MAP_READWRITE);
682 while (gensamples < outsamples) {
683 guint step_insamples = self->block_length - self->buffer_fill;
684 guint8 *zeroes = g_new0 (guint8, step_insamples * channels * bps);
685 guint8 *out = g_new (guint8, self->block_length * channels * bps);
686 guint step_gensamples;
688 step_gensamples = self->process (self, zeroes, out, step_insamples);
691 memcpy (map.data + gensamples * bps, out, MIN (step_gensamples,
692 outsamples - gensamples) * bps);
693 gensamples += MIN (step_gensamples, outsamples - gensamples);
697 self->nsamples_out += gensamples;
699 gst_buffer_unmap (outbuf, &map);
702 /* Set timestamp, offset, etc from the values we
703 * saved when processing the regular buffers */
704 if (GST_CLOCK_TIME_IS_VALID (self->start_ts))
705 GST_BUFFER_TIMESTAMP (outbuf) = self->start_ts;
707 GST_BUFFER_TIMESTAMP (outbuf) = 0;
708 GST_BUFFER_TIMESTAMP (outbuf) +=
709 gst_util_uint64_scale_int (self->nsamples_out - outsamples -
710 self->latency, GST_SECOND, rate);
712 GST_BUFFER_DURATION (outbuf) =
713 gst_util_uint64_scale_int (outsamples, GST_SECOND, rate);
715 if (self->start_off != GST_BUFFER_OFFSET_NONE) {
716 GST_BUFFER_OFFSET (outbuf) =
717 self->start_off + self->nsamples_out - outsamples - self->latency;
718 GST_BUFFER_OFFSET_END (outbuf) = GST_BUFFER_OFFSET (outbuf) + outsamples;
721 GST_DEBUG_OBJECT (self,
722 "Pushing residue buffer of size %" G_GSIZE_FORMAT " with timestamp: %"
723 GST_TIME_FORMAT ", duration: %" GST_TIME_FORMAT ", offset: %"
724 G_GUINT64_FORMAT ", offset_end: %" G_GUINT64_FORMAT ", nsamples_out: %d",
725 gst_buffer_get_size (outbuf),
726 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)),
727 GST_TIME_ARGS (GST_BUFFER_DURATION (outbuf)), GST_BUFFER_OFFSET (outbuf),
728 GST_BUFFER_OFFSET_END (outbuf), outsamples);
730 res = gst_pad_push (GST_BASE_TRANSFORM_CAST (self)->srcpad, outbuf);
732 if (G_UNLIKELY (res != GST_FLOW_OK)) {
733 GST_WARNING_OBJECT (self, "failed to push residue");
736 self->buffer_fill = 0;
739 /* GstAudioFilter vmethod implementations */
741 /* get notified of caps and plug in the correct process function */
743 gst_audio_fx_base_fir_filter_setup (GstAudioFilter * base,
744 const GstAudioInfo * info)
746 GstAudioFXBaseFIRFilter *self = GST_AUDIO_FX_BASE_FIR_FILTER (base);
749 gst_audio_fx_base_fir_filter_push_residue (self);
750 g_free (self->buffer);
752 self->buffer_fill = 0;
753 self->buffer_length = 0;
754 self->start_ts = GST_CLOCK_TIME_NONE;
755 self->start_off = GST_BUFFER_OFFSET_NONE;
756 self->nsamples_out = 0;
757 self->nsamples_in = 0;
760 gst_audio_fx_base_fir_filter_select_process_function (self,
761 GST_AUDIO_INFO_FORMAT (info), GST_AUDIO_INFO_CHANNELS (info));
763 return (self->process != NULL);
766 /* GstBaseTransform vmethod implementations */
769 gst_audio_fx_base_fir_filter_transform_size (GstBaseTransform * base,
770 GstPadDirection direction, GstCaps * caps, gsize size, GstCaps * othercaps,
773 GstAudioFXBaseFIRFilter *self = GST_AUDIO_FX_BASE_FIR_FILTER (base);
778 if (!self->fft || self->low_latency || direction == GST_PAD_SRC) {
783 if (!gst_audio_info_from_caps (&info, caps))
786 bpf = GST_AUDIO_INFO_BPF (&info);
789 blocklen = self->block_length - self->kernel_length + 1;
790 *othersize = ((size + blocklen - 1) / blocklen) * blocklen;
797 gst_audio_fx_base_fir_filter_transform (GstBaseTransform * base,
798 GstBuffer * inbuf, GstBuffer * outbuf)
800 GstAudioFXBaseFIRFilter *self = GST_AUDIO_FX_BASE_FIR_FILTER (base);
801 GstClockTime timestamp, expected_timestamp;
802 gint channels = GST_AUDIO_FILTER_CHANNELS (self);
803 gint rate = GST_AUDIO_FILTER_RATE (self);
804 gint bps = GST_AUDIO_FILTER_BPS (self);
805 GstMapInfo inmap, outmap;
807 guint output_samples;
808 guint generated_samples;
809 guint64 output_offset;
811 GstClockTime stream_time;
813 timestamp = GST_BUFFER_TIMESTAMP (outbuf);
815 if (!GST_CLOCK_TIME_IS_VALID (timestamp)
816 && !GST_CLOCK_TIME_IS_VALID (self->start_ts)) {
817 GST_ERROR_OBJECT (self, "Invalid timestamp");
818 return GST_FLOW_ERROR;
822 gst_segment_to_stream_time (&base->segment, GST_FORMAT_TIME, timestamp);
824 GST_DEBUG_OBJECT (self, "sync to %" GST_TIME_FORMAT,
825 GST_TIME_ARGS (timestamp));
827 if (GST_CLOCK_TIME_IS_VALID (stream_time))
828 gst_object_sync_values (GST_OBJECT (self), stream_time);
830 g_return_val_if_fail (self->kernel != NULL, GST_FLOW_ERROR);
831 g_return_val_if_fail (channels != 0, GST_FLOW_ERROR);
833 if (GST_CLOCK_TIME_IS_VALID (self->start_ts))
835 self->start_ts + gst_util_uint64_scale_int (self->nsamples_in,
838 expected_timestamp = GST_CLOCK_TIME_NONE;
840 /* Reset the residue if already existing on discont buffers */
841 if (GST_BUFFER_IS_DISCONT (inbuf)
842 || (GST_CLOCK_TIME_IS_VALID (expected_timestamp)
843 && (ABS (GST_CLOCK_DIFF (timestamp,
844 expected_timestamp) > 5 * GST_MSECOND)))) {
845 GST_DEBUG_OBJECT (self, "Discontinuity detected - flushing");
846 if (GST_CLOCK_TIME_IS_VALID (expected_timestamp))
847 gst_audio_fx_base_fir_filter_push_residue (self);
848 self->buffer_fill = 0;
849 g_free (self->buffer);
851 self->start_ts = timestamp;
852 self->start_off = GST_BUFFER_OFFSET (inbuf);
853 self->nsamples_out = 0;
854 self->nsamples_in = 0;
855 } else if (!GST_CLOCK_TIME_IS_VALID (self->start_ts)) {
856 self->start_ts = timestamp;
857 self->start_off = GST_BUFFER_OFFSET (inbuf);
860 gst_buffer_map (inbuf, &inmap, GST_MAP_READ);
861 gst_buffer_map (outbuf, &outmap, GST_MAP_WRITE);
863 input_samples = (inmap.size / bps) / channels;
864 output_samples = (outmap.size / bps) / channels;
866 self->nsamples_in += input_samples;
869 self->process (self, inmap.data, outmap.data, input_samples);
871 gst_buffer_unmap (inbuf, &inmap);
872 gst_buffer_unmap (outbuf, &outmap);
874 g_assert (generated_samples <= output_samples);
875 self->nsamples_out += generated_samples;
876 if (generated_samples == 0)
877 return GST_BASE_TRANSFORM_FLOW_DROPPED;
879 /* Calculate the number of samples we can push out now without outputting
880 * latency zeros in the beginning */
881 diff = ((gint64) self->nsamples_out) - ((gint64) self->latency);
883 return GST_BASE_TRANSFORM_FLOW_DROPPED;
884 } else if (diff < generated_samples) {
886 diff = generated_samples - diff;
887 generated_samples = tmp;
889 gst_buffer_resize (outbuf, diff * bps * channels,
890 generated_samples * bps * channels);
892 output_offset = self->nsamples_out - self->latency - generated_samples;
893 GST_BUFFER_TIMESTAMP (outbuf) =
894 self->start_ts + gst_util_uint64_scale_int (output_offset, GST_SECOND,
896 GST_BUFFER_DURATION (outbuf) =
897 gst_util_uint64_scale_int (output_samples, GST_SECOND, rate);
898 if (self->start_off != GST_BUFFER_OFFSET_NONE) {
899 GST_BUFFER_OFFSET (outbuf) = self->start_off + output_offset;
900 GST_BUFFER_OFFSET_END (outbuf) =
901 GST_BUFFER_OFFSET (outbuf) + generated_samples;
903 GST_BUFFER_OFFSET (outbuf) = GST_BUFFER_OFFSET_NONE;
904 GST_BUFFER_OFFSET_END (outbuf) = GST_BUFFER_OFFSET_NONE;
907 GST_DEBUG_OBJECT (self,
908 "Pushing buffer of size %" G_GSIZE_FORMAT " with timestamp: %"
909 GST_TIME_FORMAT ", duration: %" GST_TIME_FORMAT ", offset: %"
910 G_GUINT64_FORMAT ", offset_end: %" G_GUINT64_FORMAT ", nsamples_out: %d",
911 gst_buffer_get_size (outbuf),
912 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)),
913 GST_TIME_ARGS (GST_BUFFER_DURATION (outbuf)), GST_BUFFER_OFFSET (outbuf),
914 GST_BUFFER_OFFSET_END (outbuf), generated_samples);
920 gst_audio_fx_base_fir_filter_start (GstBaseTransform * base)
922 GstAudioFXBaseFIRFilter *self = GST_AUDIO_FX_BASE_FIR_FILTER (base);
924 self->buffer_fill = 0;
925 g_free (self->buffer);
927 self->start_ts = GST_CLOCK_TIME_NONE;
928 self->start_off = GST_BUFFER_OFFSET_NONE;
929 self->nsamples_out = 0;
930 self->nsamples_in = 0;
936 gst_audio_fx_base_fir_filter_stop (GstBaseTransform * base)
938 GstAudioFXBaseFIRFilter *self = GST_AUDIO_FX_BASE_FIR_FILTER (base);
940 g_free (self->buffer);
942 self->buffer_length = 0;
948 gst_audio_fx_base_fir_filter_query (GstPad * pad, GstObject * parent,
951 GstAudioFXBaseFIRFilter *self = GST_AUDIO_FX_BASE_FIR_FILTER (parent);
954 switch (GST_QUERY_TYPE (query)) {
955 case GST_QUERY_LATENCY:
957 GstClockTime min, max;
960 gint rate = GST_AUDIO_FILTER_RATE (self);
965 gst_pad_peer_query (GST_BASE_TRANSFORM (self)->sinkpad, query))) {
966 gst_query_parse_latency (query, &live, &min, &max);
968 GST_DEBUG_OBJECT (self, "Peer latency: min %"
969 GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
970 GST_TIME_ARGS (min), GST_TIME_ARGS (max));
972 if (self->fft && !self->low_latency)
973 latency = self->block_length - self->kernel_length + 1;
975 latency = self->latency;
977 /* add our own latency */
978 latency = gst_util_uint64_scale_round (latency, GST_SECOND, rate);
980 GST_DEBUG_OBJECT (self, "Our latency: %"
981 GST_TIME_FORMAT, GST_TIME_ARGS (latency));
984 if (max != GST_CLOCK_TIME_NONE)
987 GST_DEBUG_OBJECT (self, "Calculated total latency : min %"
988 GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
989 GST_TIME_ARGS (min), GST_TIME_ARGS (max));
991 gst_query_set_latency (query, live, min, max);
996 res = gst_pad_query_default (pad, parent, query);
1003 gst_audio_fx_base_fir_filter_sink_event (GstBaseTransform * base,
1006 GstAudioFXBaseFIRFilter *self = GST_AUDIO_FX_BASE_FIR_FILTER (base);
1008 switch (GST_EVENT_TYPE (event)) {
1010 gst_audio_fx_base_fir_filter_push_residue (self);
1011 self->start_ts = GST_CLOCK_TIME_NONE;
1012 self->start_off = GST_BUFFER_OFFSET_NONE;
1013 self->nsamples_out = 0;
1014 self->nsamples_in = 0;
1020 return GST_BASE_TRANSFORM_CLASS (parent_class)->sink_event (base, event);
1024 gst_audio_fx_base_fir_filter_set_kernel (GstAudioFXBaseFIRFilter * self,
1025 gdouble * kernel, guint kernel_length, guint64 latency)
1027 gboolean latency_changed;
1029 g_return_if_fail (kernel != NULL);
1030 g_return_if_fail (self != NULL);
1032 GST_BASE_TRANSFORM_LOCK (self);
1034 latency_changed = (self->latency != latency
1035 || (!self->low_latency && self->kernel_length < FFT_THRESHOLD
1036 && kernel_length >= FFT_THRESHOLD)
1037 || (!self->low_latency && self->kernel_length >= FFT_THRESHOLD
1038 && kernel_length < FFT_THRESHOLD));
1040 /* FIXME: If the latency changes, the buffer size changes too and we
1041 * have to drain in any case until this is fixed in the future */
1042 if (self->buffer && (!self->drain_on_changes || latency_changed)) {
1043 gst_audio_fx_base_fir_filter_push_residue (self);
1044 self->start_ts = GST_CLOCK_TIME_NONE;
1045 self->start_off = GST_BUFFER_OFFSET_NONE;
1046 self->nsamples_out = 0;
1047 self->nsamples_in = 0;
1048 self->buffer_fill = 0;
1051 g_free (self->kernel);
1052 if (!self->drain_on_changes || latency_changed) {
1053 g_free (self->buffer);
1054 self->buffer = NULL;
1055 self->buffer_fill = 0;
1056 self->buffer_length = 0;
1059 self->kernel = kernel;
1060 self->kernel_length = kernel_length;
1062 gst_audio_fx_base_fir_filter_calculate_frequency_response (self);
1063 gst_audio_fx_base_fir_filter_select_process_function (self,
1064 GST_AUDIO_FILTER_FORMAT (self), GST_AUDIO_FILTER_CHANNELS (self));
1066 if (latency_changed) {
1067 self->latency = latency;
1068 gst_element_post_message (GST_ELEMENT (self),
1069 gst_message_new_latency (GST_OBJECT (self)));
1072 GST_BASE_TRANSFORM_UNLOCK (self);