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_sink_event (GstBaseTransform *
74 base, GstEvent * event);
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->sink_event =
596 GST_DEBUG_FUNCPTR (gst_audio_fx_base_fir_filter_sink_event);
597 trans_class->transform_size =
598 GST_DEBUG_FUNCPTR (gst_audio_fx_base_fir_filter_transform_size);
599 filter_class->setup = GST_DEBUG_FUNCPTR (gst_audio_fx_base_fir_filter_setup);
603 gst_audio_fx_base_fir_filter_init (GstAudioFXBaseFIRFilter * self)
607 self->buffer_length = 0;
609 self->start_ts = GST_CLOCK_TIME_NONE;
610 self->start_off = GST_BUFFER_OFFSET_NONE;
611 self->nsamples_out = 0;
612 self->nsamples_in = 0;
614 self->low_latency = DEFAULT_LOW_LATENCY;
615 self->drain_on_changes = DEFAULT_DRAIN_ON_CHANGES;
617 gst_pad_set_query_function (GST_BASE_TRANSFORM (self)->srcpad,
618 gst_audio_fx_base_fir_filter_query);
619 gst_pad_set_query_type_function (GST_BASE_TRANSFORM (self)->srcpad,
620 gst_audio_fx_base_fir_filter_query_type);
624 gst_audio_fx_base_fir_filter_push_residue (GstAudioFXBaseFIRFilter * self)
628 gint rate = GST_AUDIO_FILTER_CAST (self)->format.rate;
629 gint channels = GST_AUDIO_FILTER_CAST (self)->format.channels;
630 gint width = GST_AUDIO_FILTER_CAST (self)->format.width / 8;
631 gint outsize, outsamples;
632 guint8 *in, *out, *data;
635 if (channels == 0 || rate == 0 || self->nsamples_in == 0) {
636 self->buffer_fill = 0;
637 g_free (self->buffer);
642 /* Calculate the number of samples and their memory size that
643 * should be pushed from the residue */
644 outsamples = self->nsamples_in - (self->nsamples_out - self->latency);
645 if (outsamples <= 0) {
646 self->buffer_fill = 0;
647 g_free (self->buffer);
651 outsize = outsamples * channels * width;
653 if (!self->fft || self->low_latency) {
654 gint64 diffsize, diffsamples;
656 /* Process the difference between latency and residue length samples
657 * to start at the actual data instead of starting at the zeros before
658 * when we only got one buffer smaller than latency */
660 ((gint64) self->latency) - ((gint64) self->buffer_fill) / channels;
661 if (diffsamples > 0) {
662 diffsize = diffsamples * channels * width;
663 in = g_new0 (guint8, diffsize);
664 out = g_new0 (guint8, diffsize);
665 self->nsamples_out += self->process (self, in, out, diffsamples);
670 outbuf = gst_buffer_new_and_alloc (outsize);
672 /* Convolve the residue with zeros to get the actual remaining data */
673 in = g_new0 (guint8, outsize);
674 data = gst_buffer_map (outbuf, &size, NULL, GST_MAP_READWRITE);
675 self->nsamples_out += self->process (self, in, data, outsamples);
676 gst_buffer_unmap (outbuf, data, size);
680 guint gensamples = 0;
682 outbuf = gst_buffer_new_and_alloc (outsize);
683 data = gst_buffer_map (outbuf, &size, NULL, GST_MAP_READWRITE);
685 while (gensamples < outsamples) {
686 guint step_insamples = self->block_length - self->buffer_fill;
687 guint8 *zeroes = g_new0 (guint8, step_insamples * channels * width);
688 guint8 *out = g_new (guint8, self->block_length * channels * width);
689 guint step_gensamples;
691 step_gensamples = self->process (self, zeroes, out, step_insamples);
694 memcpy (data + gensamples * width, out, MIN (step_gensamples,
695 outsamples - gensamples) * width);
696 gensamples += MIN (step_gensamples, outsamples - gensamples);
700 self->nsamples_out += gensamples;
702 gst_buffer_unmap (outbuf, data, size);
705 /* Set timestamp, offset, etc from the values we
706 * saved when processing the regular buffers */
707 if (GST_CLOCK_TIME_IS_VALID (self->start_ts))
708 GST_BUFFER_TIMESTAMP (outbuf) = self->start_ts;
710 GST_BUFFER_TIMESTAMP (outbuf) = 0;
711 GST_BUFFER_TIMESTAMP (outbuf) +=
712 gst_util_uint64_scale_int (self->nsamples_out - outsamples -
713 self->latency, GST_SECOND, rate);
715 GST_BUFFER_DURATION (outbuf) =
716 gst_util_uint64_scale_int (outsamples, GST_SECOND, rate);
718 if (self->start_off != GST_BUFFER_OFFSET_NONE) {
719 GST_BUFFER_OFFSET (outbuf) =
720 self->start_off + self->nsamples_out - outsamples - self->latency;
721 GST_BUFFER_OFFSET_END (outbuf) = GST_BUFFER_OFFSET (outbuf) + outsamples;
724 GST_DEBUG_OBJECT (self, "Pushing residue buffer of size %d with timestamp: %"
725 GST_TIME_FORMAT ", duration: %" GST_TIME_FORMAT ", offset: %"
726 G_GUINT64_FORMAT ", offset_end: %" G_GUINT64_FORMAT ", nsamples_out: %d",
727 gst_buffer_get_size (outbuf),
728 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)),
729 GST_TIME_ARGS (GST_BUFFER_DURATION (outbuf)), GST_BUFFER_OFFSET (outbuf),
730 GST_BUFFER_OFFSET_END (outbuf), outsamples);
732 res = gst_pad_push (GST_BASE_TRANSFORM_CAST (self)->srcpad, outbuf);
734 if (G_UNLIKELY (res != GST_FLOW_OK)) {
735 GST_WARNING_OBJECT (self, "failed to push residue");
738 self->buffer_fill = 0;
741 /* GstAudioFilter vmethod implementations */
743 /* get notified of caps and plug in the correct process function */
745 gst_audio_fx_base_fir_filter_setup (GstAudioFilter * base,
746 GstRingBufferSpec * format)
748 GstAudioFXBaseFIRFilter *self = GST_AUDIO_FX_BASE_FIR_FILTER (base);
751 gst_audio_fx_base_fir_filter_push_residue (self);
752 g_free (self->buffer);
754 self->buffer_fill = 0;
755 self->buffer_length = 0;
756 self->start_ts = GST_CLOCK_TIME_NONE;
757 self->start_off = GST_BUFFER_OFFSET_NONE;
758 self->nsamples_out = 0;
759 self->nsamples_in = 0;
762 gst_audio_fx_base_fir_filter_select_process_function (self, format->width,
765 return (self->process != NULL);
768 /* GstBaseTransform vmethod implementations */
771 gst_audio_fx_base_fir_filter_transform_size (GstBaseTransform * base,
772 GstPadDirection direction, GstCaps * caps, gsize size, GstCaps * othercaps,
775 GstAudioFXBaseFIRFilter *self = GST_AUDIO_FX_BASE_FIR_FILTER (base);
778 gint width, channels;
780 if (!self->fft || self->low_latency || direction == GST_PAD_SRC) {
785 s = gst_caps_get_structure (caps, 0);
786 if (!gst_structure_get_int (s, "width", &width) ||
787 !gst_structure_get_int (s, "channels", &channels))
792 size /= width * channels;
794 blocklen = self->block_length - self->kernel_length + 1;
795 *othersize = ((size + blocklen - 1) / blocklen) * blocklen;
797 *othersize *= width * channels;
803 gst_audio_fx_base_fir_filter_transform (GstBaseTransform * base,
804 GstBuffer * inbuf, GstBuffer * outbuf)
806 GstAudioFXBaseFIRFilter *self = GST_AUDIO_FX_BASE_FIR_FILTER (base);
807 GstClockTime timestamp, expected_timestamp;
808 gint channels = GST_AUDIO_FILTER_CAST (self)->format.channels;
809 gint rate = GST_AUDIO_FILTER_CAST (self)->format.rate;
810 gint width = GST_AUDIO_FILTER_CAST (self)->format.width / 8;
811 guint8 *indata, *outdata;
812 gsize insize, outsize;
814 guint output_samples;
815 guint generated_samples;
816 guint64 output_offset;
818 GstClockTime stream_time;
820 timestamp = GST_BUFFER_TIMESTAMP (outbuf);
822 if (!GST_CLOCK_TIME_IS_VALID (timestamp)
823 && !GST_CLOCK_TIME_IS_VALID (self->start_ts)) {
824 GST_ERROR_OBJECT (self, "Invalid timestamp");
825 return GST_FLOW_ERROR;
829 gst_segment_to_stream_time (&base->segment, GST_FORMAT_TIME, timestamp);
831 GST_DEBUG_OBJECT (self, "sync to %" GST_TIME_FORMAT,
832 GST_TIME_ARGS (timestamp));
834 if (GST_CLOCK_TIME_IS_VALID (stream_time))
835 gst_object_sync_values (G_OBJECT (self), stream_time);
837 g_return_val_if_fail (self->kernel != NULL, GST_FLOW_ERROR);
838 g_return_val_if_fail (channels != 0, GST_FLOW_ERROR);
840 if (GST_CLOCK_TIME_IS_VALID (self->start_ts))
842 self->start_ts + gst_util_uint64_scale_int (self->nsamples_in,
845 expected_timestamp = GST_CLOCK_TIME_NONE;
847 /* Reset the residue if already existing on discont buffers */
848 if (GST_BUFFER_IS_DISCONT (inbuf)
849 || (GST_CLOCK_TIME_IS_VALID (expected_timestamp)
850 && (ABS (GST_CLOCK_DIFF (timestamp,
851 expected_timestamp) > 5 * GST_MSECOND)))) {
852 GST_DEBUG_OBJECT (self, "Discontinuity detected - flushing");
853 if (GST_CLOCK_TIME_IS_VALID (expected_timestamp))
854 gst_audio_fx_base_fir_filter_push_residue (self);
855 self->buffer_fill = 0;
856 g_free (self->buffer);
858 self->start_ts = timestamp;
859 self->start_off = GST_BUFFER_OFFSET (inbuf);
860 self->nsamples_out = 0;
861 self->nsamples_in = 0;
862 } else if (!GST_CLOCK_TIME_IS_VALID (self->start_ts)) {
863 self->start_ts = timestamp;
864 self->start_off = GST_BUFFER_OFFSET (inbuf);
867 indata = gst_buffer_map (inbuf, &insize, NULL, GST_MAP_READ);
868 outdata = gst_buffer_map (outbuf, &outsize, NULL, GST_MAP_WRITE);
870 input_samples = (insize / width) / channels;
871 output_samples = (outsize / width) / channels;
873 self->nsamples_in += input_samples;
875 generated_samples = self->process (self, indata, outdata, input_samples);
877 gst_buffer_unmap (inbuf, indata, insize);
878 gst_buffer_unmap (outbuf, outdata, outsize);
880 g_assert (generated_samples <= output_samples);
881 self->nsamples_out += generated_samples;
882 if (generated_samples == 0)
883 return GST_BASE_TRANSFORM_FLOW_DROPPED;
885 /* Calculate the number of samples we can push out now without outputting
886 * latency zeros in the beginning */
887 diff = ((gint64) self->nsamples_out) - ((gint64) self->latency);
889 return GST_BASE_TRANSFORM_FLOW_DROPPED;
890 } else if (diff < generated_samples) {
892 diff = generated_samples - diff;
893 generated_samples = tmp;
895 gst_buffer_resize (outbuf, diff * width * channels,
896 generated_samples * width * channels);
898 output_offset = self->nsamples_out - self->latency - generated_samples;
899 GST_BUFFER_TIMESTAMP (outbuf) =
900 self->start_ts + gst_util_uint64_scale_int (output_offset, GST_SECOND,
902 GST_BUFFER_DURATION (outbuf) =
903 gst_util_uint64_scale_int (output_samples, GST_SECOND, rate);
904 if (self->start_off != GST_BUFFER_OFFSET_NONE) {
905 GST_BUFFER_OFFSET (outbuf) = self->start_off + output_offset;
906 GST_BUFFER_OFFSET_END (outbuf) =
907 GST_BUFFER_OFFSET (outbuf) + generated_samples;
909 GST_BUFFER_OFFSET (outbuf) = GST_BUFFER_OFFSET_NONE;
910 GST_BUFFER_OFFSET_END (outbuf) = GST_BUFFER_OFFSET_NONE;
913 GST_DEBUG_OBJECT (self, "Pushing buffer of size %d with timestamp: %"
914 GST_TIME_FORMAT ", duration: %" GST_TIME_FORMAT ", offset: %"
915 G_GUINT64_FORMAT ", offset_end: %" G_GUINT64_FORMAT ", nsamples_out: %d",
916 gst_buffer_get_size (outbuf),
917 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)),
918 GST_TIME_ARGS (GST_BUFFER_DURATION (outbuf)), GST_BUFFER_OFFSET (outbuf),
919 GST_BUFFER_OFFSET_END (outbuf), generated_samples);
925 gst_audio_fx_base_fir_filter_start (GstBaseTransform * base)
927 GstAudioFXBaseFIRFilter *self = GST_AUDIO_FX_BASE_FIR_FILTER (base);
929 self->buffer_fill = 0;
930 g_free (self->buffer);
932 self->start_ts = GST_CLOCK_TIME_NONE;
933 self->start_off = GST_BUFFER_OFFSET_NONE;
934 self->nsamples_out = 0;
935 self->nsamples_in = 0;
941 gst_audio_fx_base_fir_filter_stop (GstBaseTransform * base)
943 GstAudioFXBaseFIRFilter *self = GST_AUDIO_FX_BASE_FIR_FILTER (base);
945 g_free (self->buffer);
947 self->buffer_length = 0;
953 gst_audio_fx_base_fir_filter_query (GstPad * pad, GstQuery * query)
955 GstAudioFXBaseFIRFilter *self =
956 GST_AUDIO_FX_BASE_FIR_FILTER (gst_pad_get_parent (pad));
959 switch (GST_QUERY_TYPE (query)) {
960 case GST_QUERY_LATENCY:
962 GstClockTime min, max;
966 gint rate = GST_AUDIO_FILTER (self)->format.rate;
970 } else if ((peer = gst_pad_get_peer (GST_BASE_TRANSFORM (self)->sinkpad))) {
971 if ((res = gst_pad_query (peer, query))) {
972 gst_query_parse_latency (query, &live, &min, &max);
974 GST_DEBUG_OBJECT (self, "Peer latency: min %"
975 GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
976 GST_TIME_ARGS (min), GST_TIME_ARGS (max));
978 if (self->fft && !self->low_latency)
979 latency = self->block_length - self->kernel_length + 1;
981 latency = self->latency;
983 /* add our own latency */
984 latency = gst_util_uint64_scale_round (latency, GST_SECOND, rate);
986 GST_DEBUG_OBJECT (self, "Our latency: %"
987 GST_TIME_FORMAT, GST_TIME_ARGS (latency));
990 if (max != GST_CLOCK_TIME_NONE)
993 GST_DEBUG_OBJECT (self, "Calculated total latency : min %"
994 GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
995 GST_TIME_ARGS (min), GST_TIME_ARGS (max));
997 gst_query_set_latency (query, live, min, max);
999 gst_object_unref (peer);
1004 res = gst_pad_query_default (pad, query);
1007 gst_object_unref (self);
1011 static const GstQueryType *
1012 gst_audio_fx_base_fir_filter_query_type (GstPad * pad)
1014 static const GstQueryType types[] = {
1023 gst_audio_fx_base_fir_filter_sink_event (GstBaseTransform * base,
1026 GstAudioFXBaseFIRFilter *self = GST_AUDIO_FX_BASE_FIR_FILTER (base);
1028 switch (GST_EVENT_TYPE (event)) {
1030 gst_audio_fx_base_fir_filter_push_residue (self);
1031 self->start_ts = GST_CLOCK_TIME_NONE;
1032 self->start_off = GST_BUFFER_OFFSET_NONE;
1033 self->nsamples_out = 0;
1034 self->nsamples_in = 0;
1040 return GST_BASE_TRANSFORM_CLASS (parent_class)->sink_event (base, event);
1044 gst_audio_fx_base_fir_filter_set_kernel (GstAudioFXBaseFIRFilter * self,
1045 gdouble * kernel, guint kernel_length, guint64 latency)
1047 gboolean latency_changed;
1049 g_return_if_fail (kernel != NULL);
1050 g_return_if_fail (self != NULL);
1052 GST_BASE_TRANSFORM_LOCK (self);
1054 latency_changed = (self->latency != latency
1055 || (!self->low_latency && self->kernel_length < FFT_THRESHOLD
1056 && kernel_length >= FFT_THRESHOLD)
1057 || (!self->low_latency && self->kernel_length >= FFT_THRESHOLD
1058 && kernel_length < FFT_THRESHOLD));
1060 /* FIXME: If the latency changes, the buffer size changes too and we
1061 * have to drain in any case until this is fixed in the future */
1062 if (self->buffer && (!self->drain_on_changes || latency_changed)) {
1063 gst_audio_fx_base_fir_filter_push_residue (self);
1064 self->start_ts = GST_CLOCK_TIME_NONE;
1065 self->start_off = GST_BUFFER_OFFSET_NONE;
1066 self->nsamples_out = 0;
1067 self->nsamples_in = 0;
1068 self->buffer_fill = 0;
1071 g_free (self->kernel);
1072 if (!self->drain_on_changes || latency_changed) {
1073 g_free (self->buffer);
1074 self->buffer = NULL;
1075 self->buffer_fill = 0;
1076 self->buffer_length = 0;
1079 self->kernel = kernel;
1080 self->kernel_length = kernel_length;
1082 gst_audio_fx_base_fir_filter_calculate_frequency_response (self);
1083 gst_audio_fx_base_fir_filter_select_process_function (self,
1084 GST_AUDIO_FILTER_CAST (self)->format.width,
1085 GST_AUDIO_FILTER_CAST (self)->format.channels);
1087 if (latency_changed) {
1088 self->latency = latency;
1089 gst_element_post_message (GST_ELEMENT (self),
1090 gst_message_new_latency (GST_OBJECT (self)));
1093 GST_BASE_TRANSFORM_UNLOCK (self);