Merge remote-tracking branch 'origin/master' into 0.11
[platform/upstream/gst-plugins-good.git] / gst / audiofx / audioamplify.c
1 /* 
2  * GStreamer
3  * Copyright (C) 2007 Sebastian Dröge <slomo@circular-chaos.org>
4  * Copyright (C) 2006 Stefan Kost <ensonic@users.sf.net>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 /**
23  * SECTION:element-audioamplify
24  *
25  * Amplifies an audio stream by a given factor and allows the selection of different clipping modes.
26  * The difference between the clipping modes is best evaluated by testing.
27  *
28  * <refsect2>
29  * <title>Example launch line</title>
30  * |[
31  * gst-launch audiotestsrc wave=saw ! audioamplify amplification=1.5 ! alsasink
32  * gst-launch filesrc location="melo1.ogg" ! oggdemux ! vorbisdec ! audioconvert ! audioamplify amplification=1.5 method=wrap-negative ! alsasink
33  * gst-launch audiotestsrc wave=saw ! audioconvert ! audioamplify amplification=1.5 method=wrap-positive ! audioconvert ! alsasink
34  * ]|
35  * </refsect2>
36  */
37
38 #ifdef HAVE_CONFIG_H
39 #include "config.h"
40 #endif
41
42 #include <gst/gst.h>
43 #include <gst/base/gstbasetransform.h>
44 #include <gst/audio/audio.h>
45 #include <gst/audio/gstaudiofilter.h>
46
47 #include "audioamplify.h"
48
49 #define GST_CAT_DEFAULT gst_audio_amplify_debug
50 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
51
52 /* Filter signals and args */
53 enum
54 {
55   /* FILL ME */
56   LAST_SIGNAL
57 };
58
59 enum
60 {
61   PROP_0,
62   PROP_AMPLIFICATION,
63   PROP_CLIPPING_METHOD
64 };
65
66 enum
67 {
68   METHOD_CLIP = 0,
69   METHOD_WRAP_NEGATIVE,
70   METHOD_WRAP_POSITIVE,
71   METHOD_NOCLIP,
72   NUM_METHODS
73 };
74
75 #define GST_TYPE_AUDIO_AMPLIFY_CLIPPING_METHOD (gst_audio_amplify_clipping_method_get_type ())
76 static GType
77 gst_audio_amplify_clipping_method_get_type (void)
78 {
79   static GType gtype = 0;
80
81   if (gtype == 0) {
82     static const GEnumValue values[] = {
83       {METHOD_CLIP, "Normal clipping (default)", "clip"},
84       {METHOD_WRAP_NEGATIVE,
85             "Push overdriven values back from the opposite side",
86           "wrap-negative"},
87       {METHOD_WRAP_POSITIVE, "Push overdriven values back from the same side",
88           "wrap-positive"},
89       {METHOD_NOCLIP, "No clipping", "none"},
90       {0, NULL, NULL}
91     };
92     gtype = g_enum_register_static ("GstAudioAmplifyClippingMethod", values);
93   }
94   return gtype;
95 }
96
97 #define ALLOWED_CAPS                                                  \
98     "audio/x-raw,"                                                    \
99     " format=(string) {S8,"GST_AUDIO_NE(S16)","GST_AUDIO_NE(S32)","   \
100                            GST_AUDIO_NE(F32)","GST_AUDIO_NE(F64)"},"  \
101     " rate=(int)[1,MAX],"                                             \
102     " channels=(int)[1,MAX]"
103
104 G_DEFINE_TYPE (GstAudioAmplify, gst_audio_amplify, GST_TYPE_AUDIO_FILTER);
105
106 static gboolean gst_audio_amplify_set_process_function (GstAudioAmplify *
107     filter, gint clipping, GstAudioFormat format);
108 static void gst_audio_amplify_set_property (GObject * object, guint prop_id,
109     const GValue * value, GParamSpec * pspec);
110 static void gst_audio_amplify_get_property (GObject * object, guint prop_id,
111     GValue * value, GParamSpec * pspec);
112
113 static gboolean gst_audio_amplify_setup (GstAudioFilter * filter,
114     const GstAudioInfo * info);
115 static GstFlowReturn gst_audio_amplify_transform_ip (GstBaseTransform * base,
116     GstBuffer * buf);
117
118 #define MIN_gint8 G_MININT8
119 #define MAX_gint8 G_MAXINT8
120 #define MIN_gint16 G_MININT16
121 #define MAX_gint16 G_MAXINT16
122 #define MIN_gint32 G_MININT32
123 #define MAX_gint32 G_MAXINT32
124
125 #define MAKE_INT_FUNCS(type,largetype)                                        \
126 static void                                                                   \
127 gst_audio_amplify_transform_##type##_clip (GstAudioAmplify * filter,          \
128     void * data, guint num_samples)                                           \
129 {                                                                             \
130   type *d = data;                                                             \
131                                                                               \
132   while (num_samples--) {                                                     \
133     largetype val = *d * filter->amplification;                               \
134     *d++ =  CLAMP (val, MIN_##type, MAX_##type);                              \
135   }                                                                           \
136 }                                                                             \
137 static void                                                                   \
138 gst_audio_amplify_transform_##type##_wrap_negative (GstAudioAmplify * filter, \
139     void * data, guint num_samples)                                           \
140 {                                                                             \
141   type *d = data;                                                             \
142                                                                               \
143   while (num_samples--) {                                                     \
144     largetype val = *d * filter->amplification;                               \
145     if (val > MAX_##type)                                                     \
146       val = MIN_##type + (val - MIN_##type) % ((largetype) MAX_##type + 1 -   \
147           MIN_##type);                                                        \
148     else if (val < MIN_##type)                                                \
149       val = MAX_##type - (MAX_##type - val) % ((largetype) MAX_##type + 1 -   \
150           MIN_##type);                                                        \
151     *d++ = val;                                                               \
152   }                                                                           \
153 }                                                                             \
154 static void                                                                   \
155 gst_audio_amplify_transform_##type##_wrap_positive (GstAudioAmplify * filter, \
156     void * data, guint num_samples)                                           \
157 {                                                                             \
158   type *d = data;                                                             \
159                                                                               \
160   while (num_samples--) {                                                     \
161     largetype val = *d * filter->amplification;                               \
162     do {                                                                      \
163       if (val > MAX_##type)                                                   \
164         val = MAX_##type - (val - MAX_##type);                                \
165       else if (val < MIN_##type)                                              \
166         val = MIN_##type + (MIN_##type - val);                                \
167       else                                                                    \
168         break;                                                                \
169     } while (1);                                                              \
170     *d++ = val;                                                               \
171   }                                                                           \
172 }                                                                             \
173 static void                                                                   \
174 gst_audio_amplify_transform_##type##_noclip (GstAudioAmplify * filter,        \
175     void * data, guint num_samples)                                           \
176 {                                                                             \
177   type *d = data;                                                             \
178                                                                               \
179   while (num_samples--)                                                       \
180     *d++ *= filter->amplification;                                            \
181 }
182
183 #define MAKE_FLOAT_FUNCS(type)                                                \
184 static void                                                                   \
185 gst_audio_amplify_transform_##type##_clip (GstAudioAmplify * filter,          \
186     void * data, guint num_samples)                                           \
187 {                                                                             \
188   type *d = data;                                                             \
189                                                                               \
190   while (num_samples--) {                                                     \
191     type val = *d* filter->amplification;                                     \
192     *d++ = CLAMP (val, -1.0, +1.0);                                           \
193   }                                                                           \
194 }                                                                             \
195 static void                                                                   \
196 gst_audio_amplify_transform_##type##_wrap_negative (GstAudioAmplify *         \
197     filter, void * data, guint num_samples)                                   \
198 {                                                                             \
199   type *d = data;                                                             \
200                                                                               \
201   while (num_samples--) {                                                     \
202     type val = *d * filter->amplification;                                    \
203     do {                                                                      \
204       if (val > 1.0)                                                          \
205         val = -1.0 + (val - 1.0);                                             \
206       else if (val < -1.0)                                                    \
207         val = 1.0 - (1.0 - val);                                              \
208       else                                                                    \
209         break;                                                                \
210     } while (1);                                                              \
211     *d++ = val;                                                               \
212   }                                                                           \
213 }                                                                             \
214 static void                                                                   \
215 gst_audio_amplify_transform_##type##_wrap_positive (GstAudioAmplify * filter, \
216     void * data, guint num_samples)                                           \
217 {                                                                             \
218   type *d = data;                                                             \
219                                                                               \
220   while (num_samples--) {                                                     \
221     type val = *d* filter->amplification;                                     \
222     do {                                                                      \
223       if (val > 1.0)                                                          \
224         val = 1.0 - (val - 1.0);                                              \
225       else if (val < -1.0)                                                    \
226         val = -1.0 + (-1.0 - val);                                            \
227       else                                                                    \
228         break;                                                                \
229     } while (1);                                                              \
230     *d++ = val;                                                               \
231   }                                                                           \
232 }                                                                             \
233 static void                                                                   \
234 gst_audio_amplify_transform_##type##_noclip (GstAudioAmplify * filter,        \
235     void * data, guint num_samples)                                           \
236 {                                                                             \
237   type *d = data;                                                             \
238                                                                               \
239   while (num_samples--)                                                       \
240     *d++ *= filter->amplification;                                            \
241 }
242
243 /* *INDENT-OFF* */
244 MAKE_INT_FUNCS (gint8,gint)
245 MAKE_INT_FUNCS (gint16,gint)
246 MAKE_INT_FUNCS (gint32,gint64)
247 MAKE_FLOAT_FUNCS (gfloat)
248 MAKE_FLOAT_FUNCS (gdouble)
249 /* *INDENT-ON* */
250
251 /* GObject vmethod implementations */
252
253 static void
254 gst_audio_amplify_class_init (GstAudioAmplifyClass * klass)
255 {
256   GObjectClass *gobject_class;
257   GstElementClass *gstelement_class;
258   GstCaps *caps;
259
260   GST_DEBUG_CATEGORY_INIT (gst_audio_amplify_debug, "audioamplify", 0,
261       "audioamplify element");
262
263   gobject_class = (GObjectClass *) klass;
264   gstelement_class = (GstElementClass *) klass;
265
266   gobject_class->set_property = gst_audio_amplify_set_property;
267   gobject_class->get_property = gst_audio_amplify_get_property;
268
269   g_object_class_install_property (gobject_class, PROP_AMPLIFICATION,
270       g_param_spec_float ("amplification", "Amplification",
271           "Factor of amplification", -G_MAXFLOAT, G_MAXFLOAT,
272           1.0,
273           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
274
275   /**
276    * GstAudioAmplify:clipping-method
277    *
278    * Clipping method: clip mode set values higher than the maximum to the
279    * maximum. The wrap-negative mode pushes those values back from the
280    * opposite side, wrap-positive pushes them back from the same side.
281    *
282    **/
283   g_object_class_install_property (gobject_class, PROP_CLIPPING_METHOD,
284       g_param_spec_enum ("clipping-method", "Clipping method",
285           "Selects how to handle values higher than the maximum",
286           GST_TYPE_AUDIO_AMPLIFY_CLIPPING_METHOD, METHOD_CLIP,
287           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
288
289   gst_element_class_set_details_simple (gstelement_class, "Audio amplifier",
290       "Filter/Effect/Audio",
291       "Amplifies an audio stream by a given factor",
292       "Sebastian Dröge <slomo@circular-chaos.org>");
293
294   caps = gst_caps_from_string (ALLOWED_CAPS);
295   gst_audio_filter_class_add_pad_templates (GST_AUDIO_FILTER_CLASS (klass),
296       caps);
297   gst_caps_unref (caps);
298
299   GST_AUDIO_FILTER_CLASS (klass)->setup =
300       GST_DEBUG_FUNCPTR (gst_audio_amplify_setup);
301   GST_BASE_TRANSFORM_CLASS (klass)->transform_ip =
302       GST_DEBUG_FUNCPTR (gst_audio_amplify_transform_ip);
303 }
304
305 static void
306 gst_audio_amplify_init (GstAudioAmplify * filter)
307 {
308   filter->amplification = 1.0;
309   gst_audio_amplify_set_process_function (filter, METHOD_CLIP,
310       GST_AUDIO_FORMAT_S16);
311   gst_base_transform_set_in_place (GST_BASE_TRANSFORM (filter), TRUE);
312   gst_base_transform_set_gap_aware (GST_BASE_TRANSFORM (filter), TRUE);
313 }
314
315 static GstAudioAmplifyProcessFunc
316 gst_audio_amplify_process_function (gint clipping, GstAudioFormat format)
317 {
318   static const struct process
319   {
320     GstAudioFormat format;
321     gint clipping;
322     GstAudioAmplifyProcessFunc func;
323   } process[] = {
324     {
325     GST_AUDIO_FORMAT_F32, METHOD_CLIP, gst_audio_amplify_transform_gfloat_clip}, {
326     GST_AUDIO_FORMAT_F32, METHOD_WRAP_NEGATIVE,
327           gst_audio_amplify_transform_gfloat_wrap_negative}, {
328     GST_AUDIO_FORMAT_F32, METHOD_WRAP_POSITIVE,
329           gst_audio_amplify_transform_gfloat_wrap_positive}, {
330     GST_AUDIO_FORMAT_F32, METHOD_NOCLIP,
331           gst_audio_amplify_transform_gfloat_noclip}, {
332     GST_AUDIO_FORMAT_F64, METHOD_CLIP,
333           gst_audio_amplify_transform_gdouble_clip}, {
334     GST_AUDIO_FORMAT_F64, METHOD_WRAP_NEGATIVE,
335           gst_audio_amplify_transform_gdouble_wrap_negative}, {
336     GST_AUDIO_FORMAT_F64, METHOD_WRAP_POSITIVE,
337           gst_audio_amplify_transform_gdouble_wrap_positive}, {
338     GST_AUDIO_FORMAT_F64, METHOD_NOCLIP,
339           gst_audio_amplify_transform_gdouble_noclip}, {
340     GST_AUDIO_FORMAT_S8, METHOD_CLIP, gst_audio_amplify_transform_gint8_clip}, {
341     GST_AUDIO_FORMAT_S8, METHOD_WRAP_NEGATIVE,
342           gst_audio_amplify_transform_gint8_wrap_negative}, {
343     GST_AUDIO_FORMAT_S8, METHOD_WRAP_POSITIVE,
344           gst_audio_amplify_transform_gint8_wrap_positive}, {
345     GST_AUDIO_FORMAT_S8, METHOD_NOCLIP,
346           gst_audio_amplify_transform_gint8_noclip}, {
347     GST_AUDIO_FORMAT_S16, METHOD_CLIP, gst_audio_amplify_transform_gint16_clip}, {
348     GST_AUDIO_FORMAT_S16, METHOD_WRAP_NEGATIVE,
349           gst_audio_amplify_transform_gint16_wrap_negative}, {
350     GST_AUDIO_FORMAT_S16, METHOD_WRAP_POSITIVE,
351           gst_audio_amplify_transform_gint16_wrap_positive}, {
352     GST_AUDIO_FORMAT_S16, METHOD_NOCLIP,
353           gst_audio_amplify_transform_gint16_noclip}, {
354     GST_AUDIO_FORMAT_S32, METHOD_CLIP, gst_audio_amplify_transform_gint32_clip}, {
355     GST_AUDIO_FORMAT_S32, METHOD_WRAP_NEGATIVE,
356           gst_audio_amplify_transform_gint32_wrap_negative}, {
357     GST_AUDIO_FORMAT_S32, METHOD_WRAP_POSITIVE,
358           gst_audio_amplify_transform_gint32_wrap_positive}, {
359     GST_AUDIO_FORMAT_S32, METHOD_NOCLIP,
360           gst_audio_amplify_transform_gint32_noclip}, {
361     0, 0, NULL}
362   };
363   const struct process *p;
364
365   for (p = process; p->func; p++)
366     if (p->format == format && p->clipping == clipping)
367       return p->func;
368   return NULL;
369 }
370
371 static gboolean
372 gst_audio_amplify_set_process_function (GstAudioAmplify * filter, gint
373     clipping_method, GstAudioFormat format)
374 {
375   GstAudioAmplifyProcessFunc process;
376
377   /* set processing function */
378
379   process = gst_audio_amplify_process_function (clipping_method, format);
380   if (!process) {
381     GST_DEBUG ("wrong format");
382     return FALSE;
383   }
384
385   filter->process = process;
386   filter->clipping_method = clipping_method;
387   filter->format = format;
388
389   return TRUE;
390 }
391
392 static void
393 gst_audio_amplify_set_property (GObject * object, guint prop_id,
394     const GValue * value, GParamSpec * pspec)
395 {
396   GstAudioAmplify *filter = GST_AUDIO_AMPLIFY (object);
397
398   switch (prop_id) {
399     case PROP_AMPLIFICATION:
400       filter->amplification = g_value_get_float (value);
401       gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (filter),
402           filter->amplification == 1.0);
403       break;
404     case PROP_CLIPPING_METHOD:
405       gst_audio_amplify_set_process_function (filter, g_value_get_enum (value),
406           filter->format);
407       break;
408     default:
409       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
410       break;
411   }
412 }
413
414 static void
415 gst_audio_amplify_get_property (GObject * object, guint prop_id,
416     GValue * value, GParamSpec * pspec)
417 {
418   GstAudioAmplify *filter = GST_AUDIO_AMPLIFY (object);
419
420   switch (prop_id) {
421     case PROP_AMPLIFICATION:
422       g_value_set_float (value, filter->amplification);
423       break;
424     case PROP_CLIPPING_METHOD:
425       g_value_set_enum (value, filter->clipping_method);
426       break;
427     default:
428       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
429       break;
430   }
431 }
432
433 /* GstAudioFilter vmethod implementations */
434 static gboolean
435 gst_audio_amplify_setup (GstAudioFilter * base, const GstAudioInfo * info)
436 {
437   GstAudioAmplify *filter = GST_AUDIO_AMPLIFY (base);
438
439   return gst_audio_amplify_set_process_function (filter,
440       filter->clipping_method, GST_AUDIO_INFO_FORMAT (info));
441 }
442
443 /* GstBaseTransform vmethod implementations */
444 static GstFlowReturn
445 gst_audio_amplify_transform_ip (GstBaseTransform * base, GstBuffer * buf)
446 {
447   GstAudioAmplify *filter = GST_AUDIO_AMPLIFY (base);
448   guint num_samples;
449   GstClockTime timestamp, stream_time;
450   guint8 *data;
451   gsize size;
452
453   timestamp = GST_BUFFER_TIMESTAMP (buf);
454   stream_time =
455       gst_segment_to_stream_time (&base->segment, GST_FORMAT_TIME, timestamp);
456
457   GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT,
458       GST_TIME_ARGS (timestamp));
459
460   if (GST_CLOCK_TIME_IS_VALID (stream_time))
461     gst_object_sync_values (GST_OBJECT (filter), stream_time);
462
463   if (gst_base_transform_is_passthrough (base) ||
464       G_UNLIKELY (GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_GAP)))
465     return GST_FLOW_OK;
466
467   data = gst_buffer_map (buf, &size, NULL, GST_MAP_READWRITE);
468   num_samples = size / GST_AUDIO_FILTER_BPS (filter);
469
470   filter->process (filter, data, num_samples);
471
472   gst_buffer_unmap (buf, data, size);
473
474   return GST_FLOW_OK;
475 }