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>
33 #include <gst/controller/gstcontroller.h>
35 /* FIXME: Remove this once we depend on gst-plugins-base 0.10.26 */
36 #ifndef GST_AUDIO_FILTER_CAST
37 #define GST_AUDIO_FILTER_CAST(obj) ((GstAudioFilter *) (obj))
40 #include "audiofxbasefirfilter.h"
42 #define GST_CAT_DEFAULT gst_audio_fx_base_fir_filter_debug
43 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
45 #define ALLOWED_CAPS \
46 "audio/x-raw-float, " \
47 " width = (int) { 32, 64 }, " \
48 " endianness = (int) BYTE_ORDER, " \
49 " rate = (int) [ 1, MAX ], " \
50 " channels = (int) [ 1, MAX ]"
52 /* Switch from time-domain to FFT convolution for kernels >= this */
53 #define FFT_THRESHOLD 32
62 #define DEFAULT_LOW_LATENCY FALSE
63 #define DEFAULT_DRAIN_ON_CHANGES TRUE
65 #define gst_audio_fx_base_fir_filter_parent_class parent_class
66 G_DEFINE_TYPE (GstAudioFXBaseFIRFilter, gst_audio_fx_base_fir_filter,
67 GST_TYPE_AUDIO_FILTER);
69 static GstFlowReturn gst_audio_fx_base_fir_filter_transform (GstBaseTransform *
70 base, GstBuffer * inbuf, GstBuffer * outbuf);
71 static gboolean gst_audio_fx_base_fir_filter_start (GstBaseTransform * base);
72 static gboolean gst_audio_fx_base_fir_filter_stop (GstBaseTransform * base);
73 static gboolean gst_audio_fx_base_fir_filter_event (GstBaseTransform * base,
75 static gboolean gst_audio_fx_base_fir_filter_transform_size (GstBaseTransform *
76 base, GstPadDirection direction, GstCaps * caps, gsize size,
77 GstCaps * othercaps, gsize * othersize);
78 static gboolean gst_audio_fx_base_fir_filter_setup (GstAudioFilter * base,
79 GstRingBufferSpec * format);
81 static gboolean gst_audio_fx_base_fir_filter_query (GstPad * pad,
83 static const GstQueryType *gst_audio_fx_base_fir_filter_query_type (GstPad *
87 * The code below calculates the linear convolution:
89 * y[t] = \sum_{u=0}^{M-1} x[t - u] * h[u]
91 * where y is the output, x is the input, M is the length
92 * of the filter kernel and h is the filter kernel. For x
93 * holds: x[t] == 0 \forall t < 0.
95 * The runtime complexity of this is O (M) per sample.
98 #define DEFINE_PROCESS_FUNC(width,ctype) \
100 process_##width (GstAudioFXBaseFIRFilter * self, const g##ctype * src, g##ctype * dst, guint input_samples) \
102 gint channels = GST_AUDIO_FILTER_CAST (self)->format.channels; \
103 TIME_DOMAIN_CONVOLUTION_BODY (channels); \
106 #define DEFINE_PROCESS_FUNC_FIXED_CHANNELS(width,channels,ctype) \
108 process_##channels##_##width (GstAudioFXBaseFIRFilter * self, const g##ctype * src, g##ctype * dst, guint input_samples) \
110 TIME_DOMAIN_CONVOLUTION_BODY (channels); \
113 #define TIME_DOMAIN_CONVOLUTION_BODY(channels) G_STMT_START { \
114 gint kernel_length = self->kernel_length; \
119 gdouble *buffer = self->buffer; \
120 gdouble *kernel = self->kernel; \
121 guint buffer_length = self->buffer_length; \
124 self->buffer_length = buffer_length = kernel_length * channels; \
125 self->buffer = buffer = g_new0 (gdouble, self->buffer_length); \
129 for (i = 0; i < input_samples; i++) { \
133 from_input = MIN (l, kernel_length-1); \
134 off = l * channels + k; \
135 for (j = 0; j <= from_input; j++) { \
136 dst[i] += src[off] * kernel[j]; \
139 /* j == from_input && off == (l - j) * channels + k */ \
140 off += kernel_length * channels; \
141 for (; j < kernel_length; j++) { \
142 dst[i] += buffer[off] * kernel[j]; \
147 /* copy the tail of the current input buffer to the residue, while \
148 * keeping parts of the residue if the input buffer is smaller than \
149 * the kernel length */ \
150 /* from now on take kernel length as length over all channels */ \
151 kernel_length *= channels; \
152 if (input_samples < kernel_length) \
153 res_start = kernel_length - input_samples; \
157 for (i = 0; i < res_start; i++) \
158 buffer[i] = buffer[i + input_samples]; \
159 /* i == res_start */ \
160 for (; i < kernel_length; i++) \
161 buffer[i] = src[input_samples - kernel_length + i]; \
163 self->buffer_fill += kernel_length - res_start; \
164 if (self->buffer_fill > kernel_length) \
165 self->buffer_fill = kernel_length; \
167 return input_samples / channels; \
170 DEFINE_PROCESS_FUNC (32, float);
171 DEFINE_PROCESS_FUNC (64, double);
173 DEFINE_PROCESS_FUNC_FIXED_CHANNELS (32, 1, float);
174 DEFINE_PROCESS_FUNC_FIXED_CHANNELS (64, 1, double);
176 DEFINE_PROCESS_FUNC_FIXED_CHANNELS (32, 2, float);
177 DEFINE_PROCESS_FUNC_FIXED_CHANNELS (64, 2, double);
179 #undef TIME_DOMAIN_CONVOLUTION_BODY
180 #undef DEFINE_PROCESS_FUNC
181 #undef DEFINE_PROCESS_FUNC_FIXED_CHANNELS
183 /* This implements FFT convolution and uses the overlap-save algorithm.
184 * See http://cnx.org/content/m12022/latest/ or your favorite
185 * digital signal processing book for details.
187 * In every pass the following is calculated:
189 * y = IFFT (FFT(x) * FFT(h))
191 * where y is the output in the time domain, x the
192 * input and h the filter kernel. * is the multiplication
193 * of complex numbers.
195 * Due to the circular convolution theorem this
196 * gives in the time domain:
198 * y[t] = \sum_{u=0}^{M-1} x[t - u] * h[u]
200 * where y is the output, M is the kernel length,
201 * x the periodically extended[0] input and h the
204 * ([0] Periodically extended means: )
205 * ( x[t] = x[t+kN] \forall k \in Z )
206 * ( where N is the length of x )
209 * - Obviously x and h need to be of the same size for the FFT
210 * - The first M-1 output values are useless because they're
211 * built from 1 up to M-1 values from the end of the input
212 * (circular convolusion!).
213 * - The last M-1 input values are only used for 1 up to M-1
214 * output values, i.e. they need to be used again in the
215 * next pass for the first M-1 input values.
217 * => The first pass needs M-1 zeroes at the beginning of the
218 * input and the last M-1 input values of every pass need to
219 * be used as the first M-1 input values of the next pass.
221 * => x must be larger than h to give a useful number of output
222 * samples and h needs to be padded by zeroes at the end to give
223 * it virtually the same size as x (by M we denote the number of
224 * non-padding samples of h). If len(x)==len(h)==M only 1 output
225 * sample would be calculated per pass, len(x)==2*len(h) would
226 * give M+1 output samples, etc. Usually a factor between 4 and 8
227 * gives a low number of operations per output samples (see website
230 * Overall this gives a runtime complexity per sample of
233 * O ( --------- ) compared to O (M) for the direct calculation.
236 #define DEFINE_FFT_PROCESS_FUNC(width,ctype) \
238 process_fft_##width (GstAudioFXBaseFIRFilter * self, const g##ctype * src, \
239 g##ctype * dst, guint input_samples) \
241 gint channels = GST_AUDIO_FILTER_CAST (self)->format.channels; \
242 FFT_CONVOLUTION_BODY (channels); \
245 #define DEFINE_FFT_PROCESS_FUNC_FIXED_CHANNELS(width,channels,ctype) \
247 process_fft_##channels##_##width (GstAudioFXBaseFIRFilter * self, const g##ctype * src, \
248 g##ctype * dst, guint input_samples) \
250 FFT_CONVOLUTION_BODY (channels); \
253 #define FFT_CONVOLUTION_BODY(channels) G_STMT_START { \
256 guint kernel_length = self->kernel_length; \
257 guint block_length = self->block_length; \
258 guint buffer_length = self->buffer_length; \
259 guint real_buffer_length = buffer_length + kernel_length - 1; \
260 guint buffer_fill = self->buffer_fill; \
261 GstFFTF64 *fft = self->fft; \
262 GstFFTF64 *ifft = self->ifft; \
263 GstFFTF64Complex *frequency_response = self->frequency_response; \
264 GstFFTF64Complex *fft_buffer = self->fft_buffer; \
265 guint frequency_response_length = self->frequency_response_length; \
266 gdouble *buffer = self->buffer; \
267 guint generated = 0; \
271 self->fft_buffer = fft_buffer = \
272 g_new (GstFFTF64Complex, frequency_response_length); \
274 /* Buffer contains the time domain samples of input data for one chunk \
275 * plus some more space for the inverse FFT below. \
277 * The samples are put at offset kernel_length, the inverse FFT \
278 * overwrites everthing from offset 0 to length-kernel_length+1, keeping \
279 * the last kernel_length-1 samples for copying to the next processing \
283 self->buffer_length = buffer_length = block_length; \
284 real_buffer_length = buffer_length + kernel_length - 1; \
286 self->buffer = buffer = g_new0 (gdouble, real_buffer_length * channels); \
288 /* Beginning has kernel_length-1 zeroes at the beginning */ \
289 self->buffer_fill = buffer_fill = kernel_length - 1; \
292 g_assert (self->buffer_length == block_length); \
294 while (input_samples) { \
295 pass = MIN (buffer_length - buffer_fill, input_samples); \
297 /* Deinterleave channels */ \
298 for (i = 0; i < pass; i++) { \
299 for (j = 0; j < channels; j++) { \
300 buffer[real_buffer_length * j + buffer_fill + kernel_length - 1 + i] = \
301 src[i * channels + j]; \
304 buffer_fill += pass; \
305 src += channels * pass; \
306 input_samples -= pass; \
308 /* If we don't have a complete buffer go out */ \
309 if (buffer_fill < buffer_length) \
312 for (j = 0; j < channels; j++) { \
313 /* Calculate FFT of input block */ \
314 gst_fft_f64_fft (fft, \
315 buffer + real_buffer_length * j + kernel_length - 1, fft_buffer); \
317 /* Complex multiplication of input and filter spectrum */ \
318 for (i = 0; i < frequency_response_length; i++) { \
319 re = fft_buffer[i].r; \
320 im = fft_buffer[i].i; \
323 re * frequency_response[i].r - \
324 im * frequency_response[i].i; \
326 re * frequency_response[i].i + \
327 im * frequency_response[i].r; \
330 /* Calculate inverse FFT of the result */ \
331 gst_fft_f64_inverse_fft (ifft, fft_buffer, \
332 buffer + real_buffer_length * j); \
334 /* Copy all except the first kernel_length-1 samples to the output */ \
335 for (i = 0; i < buffer_length - kernel_length + 1; i++) { \
336 dst[i * channels + j] = \
337 buffer[real_buffer_length * j + kernel_length - 1 + i]; \
340 /* Copy the last kernel_length-1 samples to the beginning for the next block */ \
341 for (i = 0; i < kernel_length - 1; i++) { \
342 buffer[real_buffer_length * j + kernel_length - 1 + i] = \
343 buffer[real_buffer_length * j + buffer_length + i]; \
347 generated += buffer_length - kernel_length + 1; \
348 dst += channels * (buffer_length - kernel_length + 1); \
350 /* The the first kernel_length-1 samples are there already */ \
351 buffer_fill = kernel_length - 1; \
354 /* Write back cached buffer_fill value */ \
355 self->buffer_fill = buffer_fill; \
360 DEFINE_FFT_PROCESS_FUNC (32, float);
361 DEFINE_FFT_PROCESS_FUNC (64, double);
363 DEFINE_FFT_PROCESS_FUNC_FIXED_CHANNELS (32, 1, float);
364 DEFINE_FFT_PROCESS_FUNC_FIXED_CHANNELS (64, 1, double);
366 DEFINE_FFT_PROCESS_FUNC_FIXED_CHANNELS (32, 2, float);
367 DEFINE_FFT_PROCESS_FUNC_FIXED_CHANNELS (64, 2, double);
369 #undef FFT_CONVOLUTION_BODY
370 #undef DEFINE_FFT_PROCESS_FUNC
371 #undef DEFINE_FFT_PROCESS_FUNC_FIXED_CHANNELS
375 gst_audio_fx_base_fir_filter_calculate_frequency_response
376 (GstAudioFXBaseFIRFilter * self)
378 gst_fft_f64_free (self->fft);
380 gst_fft_f64_free (self->ifft);
382 g_free (self->frequency_response);
383 self->frequency_response_length = 0;
384 g_free (self->fft_buffer);
385 self->fft_buffer = NULL;
387 if (self->kernel && self->kernel_length >= FFT_THRESHOLD
388 && !self->low_latency) {
389 guint block_length, i;
390 gdouble *kernel_tmp, *kernel = self->kernel;
392 /* We process 4 * kernel_length samples per pass in FFT mode */
393 block_length = 4 * self->kernel_length;
394 block_length = gst_fft_next_fast_length (block_length);
395 self->block_length = block_length;
397 kernel_tmp = g_new0 (gdouble, block_length);
398 memcpy (kernel_tmp, kernel, self->kernel_length * sizeof (gdouble));
400 self->fft = gst_fft_f64_new (block_length, FALSE);
401 self->ifft = gst_fft_f64_new (block_length, TRUE);
402 self->frequency_response_length = block_length / 2 + 1;
403 self->frequency_response =
404 g_new (GstFFTF64Complex, self->frequency_response_length);
405 gst_fft_f64_fft (self->fft, kernel_tmp, self->frequency_response);
408 /* Normalize to make sure IFFT(FFT(x)) == x */
409 for (i = 0; i < self->frequency_response_length; i++) {
410 self->frequency_response[i].r /= block_length;
411 self->frequency_response[i].i /= block_length;
416 /* Must be called with base transform lock! */
418 gst_audio_fx_base_fir_filter_select_process_function (GstAudioFXBaseFIRFilter *
419 self, gint width, gint channels)
421 if (width == 32 && self->fft && !self->low_latency) {
423 self->process = (GstAudioFXBaseFIRFilterProcessFunc) process_fft_1_32;
424 else if (channels == 2)
425 self->process = (GstAudioFXBaseFIRFilterProcessFunc) process_fft_2_32;
427 self->process = (GstAudioFXBaseFIRFilterProcessFunc) process_fft_32;
428 } else if (width == 64 && self->fft && !self->low_latency) {
430 self->process = (GstAudioFXBaseFIRFilterProcessFunc) process_fft_1_64;
431 else if (channels == 2)
432 self->process = (GstAudioFXBaseFIRFilterProcessFunc) process_fft_2_64;
434 self->process = (GstAudioFXBaseFIRFilterProcessFunc) process_fft_64;
435 } else if (width == 32) {
437 self->process = (GstAudioFXBaseFIRFilterProcessFunc) process_1_32;
438 else if (channels == 2)
439 self->process = (GstAudioFXBaseFIRFilterProcessFunc) process_2_32;
441 self->process = (GstAudioFXBaseFIRFilterProcessFunc) process_32;
442 } else if (width == 64) {
444 self->process = (GstAudioFXBaseFIRFilterProcessFunc) process_1_64;
445 else if (channels == 2)
446 self->process = (GstAudioFXBaseFIRFilterProcessFunc) process_2_64;
448 self->process = (GstAudioFXBaseFIRFilterProcessFunc) process_64;
450 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_CAST (self)->format.width,
504 GST_AUDIO_FILTER_CAST (self)->format.channels);
506 GST_BASE_TRANSFORM_UNLOCK (self);
509 case PROP_DRAIN_ON_CHANGES:{
510 GST_BASE_TRANSFORM_LOCK (self);
511 self->drain_on_changes = g_value_get_boolean (value);
512 GST_BASE_TRANSFORM_UNLOCK (self);
516 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
522 gst_audio_fx_base_fir_filter_get_property (GObject * object, guint prop_id,
523 GValue * value, GParamSpec * pspec)
525 GstAudioFXBaseFIRFilter *self = GST_AUDIO_FX_BASE_FIR_FILTER (object);
528 case PROP_LOW_LATENCY:
529 g_value_set_boolean (value, self->low_latency);
531 case PROP_DRAIN_ON_CHANGES:
532 g_value_set_boolean (value, self->drain_on_changes);
535 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
541 gst_audio_fx_base_fir_filter_class_init (GstAudioFXBaseFIRFilterClass * klass)
543 GObjectClass *gobject_class = (GObjectClass *) klass;
544 GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
545 GstAudioFilterClass *filter_class = (GstAudioFilterClass *) klass;
548 GST_DEBUG_CATEGORY_INIT (gst_audio_fx_base_fir_filter_debug,
549 "audiofxbasefirfilter", 0, "FIR filter base class");
551 gobject_class->dispose = gst_audio_fx_base_fir_filter_dispose;
552 gobject_class->set_property = gst_audio_fx_base_fir_filter_set_property;
553 gobject_class->get_property = gst_audio_fx_base_fir_filter_get_property;
556 * GstAudioFXBaseFIRFilter::low-latency:
558 * Work in low-latency mode. This mode is much slower for large filter sizes
559 * but the latency is always only the pre-latency of the filter.
563 g_object_class_install_property (gobject_class, PROP_LOW_LATENCY,
564 g_param_spec_boolean ("low-latency", "Low latency",
565 "Operate in low latency mode. This mode is slower but the "
566 "latency will only be the filter pre-latency. "
567 "Can only be changed in states < PAUSED!", DEFAULT_LOW_LATENCY,
568 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
571 * GstAudioFXBaseFIRFilter::drain-on-changes:
573 * Whether the filter should be drained when its coeficients change
575 * Note: Currently this only works if the kernel size is not changed!
576 * Support for drainless kernel size changes will be added in the future.
580 g_object_class_install_property (gobject_class, PROP_DRAIN_ON_CHANGES,
581 g_param_spec_boolean ("drain-on-changes", "Drain on changes",
582 "Drains the filter when its coeficients change",
583 DEFAULT_DRAIN_ON_CHANGES,
584 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
586 caps = gst_caps_from_string (ALLOWED_CAPS);
587 gst_audio_filter_class_add_pad_templates (GST_AUDIO_FILTER_CLASS (klass),
589 gst_caps_unref (caps);
591 trans_class->transform =
592 GST_DEBUG_FUNCPTR (gst_audio_fx_base_fir_filter_transform);
593 trans_class->start = GST_DEBUG_FUNCPTR (gst_audio_fx_base_fir_filter_start);
594 trans_class->stop = GST_DEBUG_FUNCPTR (gst_audio_fx_base_fir_filter_stop);
595 trans_class->event = GST_DEBUG_FUNCPTR (gst_audio_fx_base_fir_filter_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);
618 gst_pad_set_query_type_function (GST_BASE_TRANSFORM (self)->srcpad,
619 gst_audio_fx_base_fir_filter_query_type);
623 gst_audio_fx_base_fir_filter_push_residue (GstAudioFXBaseFIRFilter * self)
627 gint rate = GST_AUDIO_FILTER_CAST (self)->format.rate;
628 gint channels = GST_AUDIO_FILTER_CAST (self)->format.channels;
629 gint width = GST_AUDIO_FILTER_CAST (self)->format.width / 8;
630 gint outsize, outsamples;
631 guint8 *in, *out, *data;
634 if (channels == 0 || rate == 0 || self->nsamples_in == 0) {
635 self->buffer_fill = 0;
636 g_free (self->buffer);
641 /* Calculate the number of samples and their memory size that
642 * should be pushed from the residue */
643 outsamples = self->nsamples_in - (self->nsamples_out - self->latency);
644 if (outsamples <= 0) {
645 self->buffer_fill = 0;
646 g_free (self->buffer);
650 outsize = outsamples * channels * width;
652 if (!self->fft || self->low_latency) {
653 gint64 diffsize, diffsamples;
655 /* Process the difference between latency and residue length samples
656 * to start at the actual data instead of starting at the zeros before
657 * when we only got one buffer smaller than latency */
659 ((gint64) self->latency) - ((gint64) self->buffer_fill) / channels;
660 if (diffsamples > 0) {
661 diffsize = diffsamples * channels * width;
662 in = g_new0 (guint8, diffsize);
663 out = g_new0 (guint8, diffsize);
664 self->nsamples_out += self->process (self, in, out, diffsamples);
669 outbuf = gst_buffer_new_and_alloc (outsize);
671 /* Convolve the residue with zeros to get the actual remaining data */
672 in = g_new0 (guint8, outsize);
673 data = gst_buffer_map (outbuf, &size, NULL, GST_MAP_READWRITE);
674 self->nsamples_out += self->process (self, in, data, outsamples);
675 gst_buffer_unmap (outbuf, data, size);
679 guint gensamples = 0;
681 outbuf = gst_buffer_new_and_alloc (outsize);
682 data = gst_buffer_map (outbuf, &size, NULL, GST_MAP_READWRITE);
684 while (gensamples < outsamples) {
685 guint step_insamples = self->block_length - self->buffer_fill;
686 guint8 *zeroes = g_new0 (guint8, step_insamples * channels * width);
687 guint8 *out = g_new (guint8, self->block_length * channels * width);
688 guint step_gensamples;
690 step_gensamples = self->process (self, zeroes, out, step_insamples);
693 memcpy (data + gensamples * width, out, MIN (step_gensamples,
694 outsamples - gensamples) * width);
695 gensamples += MIN (step_gensamples, outsamples - gensamples);
699 self->nsamples_out += gensamples;
701 gst_buffer_unmap (outbuf, data, size);
704 /* Set timestamp, offset, etc from the values we
705 * saved when processing the regular buffers */
706 if (GST_CLOCK_TIME_IS_VALID (self->start_ts))
707 GST_BUFFER_TIMESTAMP (outbuf) = self->start_ts;
709 GST_BUFFER_TIMESTAMP (outbuf) = 0;
710 GST_BUFFER_TIMESTAMP (outbuf) +=
711 gst_util_uint64_scale_int (self->nsamples_out - outsamples -
712 self->latency, GST_SECOND, rate);
714 GST_BUFFER_DURATION (outbuf) =
715 gst_util_uint64_scale_int (outsamples, GST_SECOND, rate);
717 if (self->start_off != GST_BUFFER_OFFSET_NONE) {
718 GST_BUFFER_OFFSET (outbuf) =
719 self->start_off + self->nsamples_out - outsamples - self->latency;
720 GST_BUFFER_OFFSET_END (outbuf) = GST_BUFFER_OFFSET (outbuf) + outsamples;
723 GST_DEBUG_OBJECT (self, "Pushing residue buffer of size %d with timestamp: %"
724 GST_TIME_FORMAT ", duration: %" GST_TIME_FORMAT ", offset: %"
725 G_GUINT64_FORMAT ", offset_end: %" G_GUINT64_FORMAT ", nsamples_out: %d",
726 gst_buffer_get_size (outbuf),
727 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)),
728 GST_TIME_ARGS (GST_BUFFER_DURATION (outbuf)), GST_BUFFER_OFFSET (outbuf),
729 GST_BUFFER_OFFSET_END (outbuf), outsamples);
731 res = gst_pad_push (GST_BASE_TRANSFORM_CAST (self)->srcpad, outbuf);
733 if (G_UNLIKELY (res != GST_FLOW_OK)) {
734 GST_WARNING_OBJECT (self, "failed to push residue");
737 self->buffer_fill = 0;
740 /* GstAudioFilter vmethod implementations */
742 /* get notified of caps and plug in the correct process function */
744 gst_audio_fx_base_fir_filter_setup (GstAudioFilter * base,
745 GstRingBufferSpec * format)
747 GstAudioFXBaseFIRFilter *self = GST_AUDIO_FX_BASE_FIR_FILTER (base);
750 gst_audio_fx_base_fir_filter_push_residue (self);
751 g_free (self->buffer);
753 self->buffer_fill = 0;
754 self->buffer_length = 0;
755 self->start_ts = GST_CLOCK_TIME_NONE;
756 self->start_off = GST_BUFFER_OFFSET_NONE;
757 self->nsamples_out = 0;
758 self->nsamples_in = 0;
761 gst_audio_fx_base_fir_filter_select_process_function (self, format->width,
764 return (self->process != NULL);
767 /* GstBaseTransform vmethod implementations */
770 gst_audio_fx_base_fir_filter_transform_size (GstBaseTransform * base,
771 GstPadDirection direction, GstCaps * caps, gsize size, GstCaps * othercaps,
774 GstAudioFXBaseFIRFilter *self = GST_AUDIO_FX_BASE_FIR_FILTER (base);
777 gint width, channels;
779 if (!self->fft || self->low_latency || direction == GST_PAD_SRC) {
784 s = gst_caps_get_structure (caps, 0);
785 if (!gst_structure_get_int (s, "width", &width) ||
786 !gst_structure_get_int (s, "channels", &channels))
791 size /= width * channels;
793 blocklen = self->block_length - self->kernel_length + 1;
794 *othersize = ((size + blocklen - 1) / blocklen) * blocklen;
796 *othersize *= width * channels;
802 gst_audio_fx_base_fir_filter_transform (GstBaseTransform * base,
803 GstBuffer * inbuf, GstBuffer * outbuf)
805 GstAudioFXBaseFIRFilter *self = GST_AUDIO_FX_BASE_FIR_FILTER (base);
806 GstClockTime timestamp, expected_timestamp;
807 gint channels = GST_AUDIO_FILTER_CAST (self)->format.channels;
808 gint rate = GST_AUDIO_FILTER_CAST (self)->format.rate;
809 gint width = GST_AUDIO_FILTER_CAST (self)->format.width / 8;
810 guint8 *indata, *outdata;
811 gsize insize, outsize;
813 guint output_samples;
814 guint generated_samples;
815 guint64 output_offset;
817 GstClockTime stream_time;
819 timestamp = GST_BUFFER_TIMESTAMP (outbuf);
821 if (!GST_CLOCK_TIME_IS_VALID (timestamp)
822 && !GST_CLOCK_TIME_IS_VALID (self->start_ts)) {
823 GST_ERROR_OBJECT (self, "Invalid timestamp");
824 return GST_FLOW_ERROR;
828 gst_segment_to_stream_time (&base->segment, GST_FORMAT_TIME, timestamp);
830 GST_DEBUG_OBJECT (self, "sync to %" GST_TIME_FORMAT,
831 GST_TIME_ARGS (timestamp));
833 if (GST_CLOCK_TIME_IS_VALID (stream_time))
834 gst_object_sync_values (G_OBJECT (self), stream_time);
836 g_return_val_if_fail (self->kernel != NULL, GST_FLOW_ERROR);
837 g_return_val_if_fail (channels != 0, GST_FLOW_ERROR);
839 if (GST_CLOCK_TIME_IS_VALID (self->start_ts))
841 self->start_ts + gst_util_uint64_scale_int (self->nsamples_in,
844 expected_timestamp = GST_CLOCK_TIME_NONE;
846 /* Reset the residue if already existing on discont buffers */
847 if (GST_BUFFER_IS_DISCONT (inbuf)
848 || (GST_CLOCK_TIME_IS_VALID (expected_timestamp)
849 && (ABS (GST_CLOCK_DIFF (timestamp,
850 expected_timestamp) > 5 * GST_MSECOND)))) {
851 GST_DEBUG_OBJECT (self, "Discontinuity detected - flushing");
852 if (GST_CLOCK_TIME_IS_VALID (expected_timestamp))
853 gst_audio_fx_base_fir_filter_push_residue (self);
854 self->buffer_fill = 0;
855 g_free (self->buffer);
857 self->start_ts = timestamp;
858 self->start_off = GST_BUFFER_OFFSET (inbuf);
859 self->nsamples_out = 0;
860 self->nsamples_in = 0;
861 } else if (!GST_CLOCK_TIME_IS_VALID (self->start_ts)) {
862 self->start_ts = timestamp;
863 self->start_off = GST_BUFFER_OFFSET (inbuf);
866 indata = gst_buffer_map (inbuf, &insize, NULL, GST_MAP_READ);
867 outdata = gst_buffer_map (outbuf, &outsize, NULL, GST_MAP_WRITE);
869 input_samples = (insize / width) / channels;
870 output_samples = (outsize / width) / channels;
872 self->nsamples_in += input_samples;
874 generated_samples = self->process (self, indata, outdata, input_samples);
876 gst_buffer_unmap (inbuf, indata, insize);
877 gst_buffer_unmap (outbuf, outdata, outsize);
879 g_assert (generated_samples <= output_samples);
880 self->nsamples_out += generated_samples;
881 if (generated_samples == 0)
882 return GST_BASE_TRANSFORM_FLOW_DROPPED;
884 /* Calculate the number of samples we can push out now without outputting
885 * latency zeros in the beginning */
886 diff = ((gint64) self->nsamples_out) - ((gint64) self->latency);
888 return GST_BASE_TRANSFORM_FLOW_DROPPED;
889 } else if (diff < generated_samples) {
891 diff = generated_samples - diff;
892 generated_samples = tmp;
894 gst_buffer_resize (outbuf, diff * width * channels,
895 generated_samples * width * channels);
897 output_offset = self->nsamples_out - self->latency - generated_samples;
898 GST_BUFFER_TIMESTAMP (outbuf) =
899 self->start_ts + gst_util_uint64_scale_int (output_offset, GST_SECOND,
901 GST_BUFFER_DURATION (outbuf) =
902 gst_util_uint64_scale_int (output_samples, GST_SECOND, rate);
903 if (self->start_off != GST_BUFFER_OFFSET_NONE) {
904 GST_BUFFER_OFFSET (outbuf) = self->start_off + output_offset;
905 GST_BUFFER_OFFSET_END (outbuf) =
906 GST_BUFFER_OFFSET (outbuf) + generated_samples;
908 GST_BUFFER_OFFSET (outbuf) = GST_BUFFER_OFFSET_NONE;
909 GST_BUFFER_OFFSET_END (outbuf) = GST_BUFFER_OFFSET_NONE;
912 GST_DEBUG_OBJECT (self, "Pushing buffer of size %d with timestamp: %"
913 GST_TIME_FORMAT ", duration: %" GST_TIME_FORMAT ", offset: %"
914 G_GUINT64_FORMAT ", offset_end: %" G_GUINT64_FORMAT ", nsamples_out: %d",
915 gst_buffer_get_size (outbuf),
916 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)),
917 GST_TIME_ARGS (GST_BUFFER_DURATION (outbuf)), GST_BUFFER_OFFSET (outbuf),
918 GST_BUFFER_OFFSET_END (outbuf), generated_samples);
924 gst_audio_fx_base_fir_filter_start (GstBaseTransform * base)
926 GstAudioFXBaseFIRFilter *self = GST_AUDIO_FX_BASE_FIR_FILTER (base);
928 self->buffer_fill = 0;
929 g_free (self->buffer);
931 self->start_ts = GST_CLOCK_TIME_NONE;
932 self->start_off = GST_BUFFER_OFFSET_NONE;
933 self->nsamples_out = 0;
934 self->nsamples_in = 0;
940 gst_audio_fx_base_fir_filter_stop (GstBaseTransform * base)
942 GstAudioFXBaseFIRFilter *self = GST_AUDIO_FX_BASE_FIR_FILTER (base);
944 g_free (self->buffer);
946 self->buffer_length = 0;
952 gst_audio_fx_base_fir_filter_query (GstPad * pad, GstQuery * query)
954 GstAudioFXBaseFIRFilter *self =
955 GST_AUDIO_FX_BASE_FIR_FILTER (gst_pad_get_parent (pad));
958 switch (GST_QUERY_TYPE (query)) {
959 case GST_QUERY_LATENCY:
961 GstClockTime min, max;
965 gint rate = GST_AUDIO_FILTER (self)->format.rate;
969 } else if ((peer = gst_pad_get_peer (GST_BASE_TRANSFORM (self)->sinkpad))) {
970 if ((res = gst_pad_query (peer, query))) {
971 gst_query_parse_latency (query, &live, &min, &max);
973 GST_DEBUG_OBJECT (self, "Peer latency: min %"
974 GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
975 GST_TIME_ARGS (min), GST_TIME_ARGS (max));
977 if (self->fft && !self->low_latency)
978 latency = self->block_length - self->kernel_length + 1;
980 latency = self->latency;
982 /* add our own latency */
983 latency = gst_util_uint64_scale_round (latency, GST_SECOND, rate);
985 GST_DEBUG_OBJECT (self, "Our latency: %"
986 GST_TIME_FORMAT, GST_TIME_ARGS (latency));
989 if (max != GST_CLOCK_TIME_NONE)
992 GST_DEBUG_OBJECT (self, "Calculated total latency : min %"
993 GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
994 GST_TIME_ARGS (min), GST_TIME_ARGS (max));
996 gst_query_set_latency (query, live, min, max);
998 gst_object_unref (peer);
1003 res = gst_pad_query_default (pad, query);
1006 gst_object_unref (self);
1010 static const GstQueryType *
1011 gst_audio_fx_base_fir_filter_query_type (GstPad * pad)
1013 static const GstQueryType types[] = {
1022 gst_audio_fx_base_fir_filter_event (GstBaseTransform * base, GstEvent * event)
1024 GstAudioFXBaseFIRFilter *self = GST_AUDIO_FX_BASE_FIR_FILTER (base);
1026 switch (GST_EVENT_TYPE (event)) {
1028 gst_audio_fx_base_fir_filter_push_residue (self);
1029 self->start_ts = GST_CLOCK_TIME_NONE;
1030 self->start_off = GST_BUFFER_OFFSET_NONE;
1031 self->nsamples_out = 0;
1032 self->nsamples_in = 0;
1038 return GST_BASE_TRANSFORM_CLASS (parent_class)->event (base, event);
1042 gst_audio_fx_base_fir_filter_set_kernel (GstAudioFXBaseFIRFilter * self,
1043 gdouble * kernel, guint kernel_length, guint64 latency)
1045 gboolean latency_changed;
1047 g_return_if_fail (kernel != NULL);
1048 g_return_if_fail (self != NULL);
1050 GST_BASE_TRANSFORM_LOCK (self);
1052 latency_changed = (self->latency != latency
1053 || (!self->low_latency && self->kernel_length < FFT_THRESHOLD
1054 && kernel_length >= FFT_THRESHOLD)
1055 || (!self->low_latency && self->kernel_length >= FFT_THRESHOLD
1056 && kernel_length < FFT_THRESHOLD));
1058 /* FIXME: If the latency changes, the buffer size changes too and we
1059 * have to drain in any case until this is fixed in the future */
1060 if (self->buffer && (!self->drain_on_changes || latency_changed)) {
1061 gst_audio_fx_base_fir_filter_push_residue (self);
1062 self->start_ts = GST_CLOCK_TIME_NONE;
1063 self->start_off = GST_BUFFER_OFFSET_NONE;
1064 self->nsamples_out = 0;
1065 self->nsamples_in = 0;
1066 self->buffer_fill = 0;
1069 g_free (self->kernel);
1070 if (!self->drain_on_changes || latency_changed) {
1071 g_free (self->buffer);
1072 self->buffer = NULL;
1073 self->buffer_fill = 0;
1074 self->buffer_length = 0;
1077 self->kernel = kernel;
1078 self->kernel_length = kernel_length;
1080 gst_audio_fx_base_fir_filter_calculate_frequency_response (self);
1081 gst_audio_fx_base_fir_filter_select_process_function (self,
1082 GST_AUDIO_FILTER_CAST (self)->format.width,
1083 GST_AUDIO_FILTER_CAST (self)->format.channels);
1085 if (latency_changed) {
1086 self->latency = latency;
1087 gst_element_post_message (GST_ELEMENT (self),
1088 gst_message_new_latency (GST_OBJECT (self)));
1091 GST_BASE_TRANSFORM_UNLOCK (self);