Tizen 2.0 Release
[framework/multimedia/gst-plugins-good0.10.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 #include <gst/controller/gstcontroller.h>
47
48 #include "audioamplify.h"
49
50 #define GST_CAT_DEFAULT gst_audio_amplify_debug
51 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
52
53 /* Filter signals and args */
54 enum
55 {
56   /* FILL ME */
57   LAST_SIGNAL
58 };
59
60 enum
61 {
62   PROP_0,
63   PROP_AMPLIFICATION,
64   PROP_CLIPPING_METHOD
65 };
66
67 enum
68 {
69   METHOD_CLIP = 0,
70   METHOD_WRAP_NEGATIVE,
71   METHOD_WRAP_POSITIVE,
72   METHOD_NOCLIP,
73   NUM_METHODS
74 };
75
76 #define GST_TYPE_AUDIO_AMPLIFY_CLIPPING_METHOD (gst_audio_amplify_clipping_method_get_type ())
77 static GType
78 gst_audio_amplify_clipping_method_get_type (void)
79 {
80   static GType gtype = 0;
81
82   if (gtype == 0) {
83     static const GEnumValue values[] = {
84       {METHOD_CLIP, "Normal clipping (default)", "clip"},
85       {METHOD_WRAP_NEGATIVE,
86             "Push overdriven values back from the opposite side",
87           "wrap-negative"},
88       {METHOD_WRAP_POSITIVE, "Push overdriven values back from the same side",
89           "wrap-positive"},
90       {METHOD_NOCLIP, "No clipping", "none"},
91       {0, NULL, NULL}
92     };
93
94     /* FIXME 0.11: rename to GstAudioAmplifyClippingMethod */
95     gtype = g_enum_register_static ("GstAudioPanoramaClippingMethod", values);
96   }
97   return gtype;
98 }
99
100 #define ALLOWED_CAPS                                                  \
101     "audio/x-raw-int,"                                                \
102     " depth=(int)8,"                                                  \
103     " width=(int)8,"                                                  \
104     " endianness=(int)BYTE_ORDER,"                                    \
105     " signed=(bool)TRUE,"                                             \
106     " rate=(int)[1,MAX],"                                             \
107     " channels=(int)[1,MAX]; "                                        \
108     "audio/x-raw-int,"                                                \
109     " depth=(int)16,"                                                 \
110     " width=(int)16,"                                                 \
111     " endianness=(int)BYTE_ORDER,"                                    \
112     " signed=(bool)TRUE,"                                             \
113     " rate=(int)[1,MAX],"                                             \
114     " channels=(int)[1,MAX]; "                                        \
115     "audio/x-raw-int,"                                                \
116     " depth=(int)32,"                                                 \
117     " width=(int)32,"                                                 \
118     " endianness=(int)BYTE_ORDER,"                                    \
119     " signed=(bool)TRUE,"                                             \
120     " rate=(int)[1,MAX],"                                             \
121     " channels=(int)[1,MAX]; "                                        \
122     "audio/x-raw-float,"                                              \
123     " width=(int){32,64},"                                            \
124     " endianness=(int)BYTE_ORDER,"                                    \
125     " rate=(int)[1,MAX],"                                             \
126     " channels=(int)[1,MAX]"
127
128 #define DEBUG_INIT(bla) \
129   GST_DEBUG_CATEGORY_INIT (gst_audio_amplify_debug, "audioamplify", 0, "audioamplify element");
130
131 GST_BOILERPLATE_FULL (GstAudioAmplify, gst_audio_amplify, GstAudioFilter,
132     GST_TYPE_AUDIO_FILTER, DEBUG_INIT);
133
134 static gboolean gst_audio_amplify_set_process_function (GstAudioAmplify *
135     filter, gint clipping, gint format, gint width);
136 static void gst_audio_amplify_set_property (GObject * object, guint prop_id,
137     const GValue * value, GParamSpec * pspec);
138 static void gst_audio_amplify_get_property (GObject * object, guint prop_id,
139     GValue * value, GParamSpec * pspec);
140
141 static gboolean gst_audio_amplify_setup (GstAudioFilter * filter,
142     GstRingBufferSpec * format);
143 static GstFlowReturn gst_audio_amplify_transform_ip (GstBaseTransform * base,
144     GstBuffer * buf);
145
146 #define MIN_gint8 G_MININT8
147 #define MAX_gint8 G_MAXINT8
148 #define MIN_gint16 G_MININT16
149 #define MAX_gint16 G_MAXINT16
150 #define MIN_gint32 G_MININT32
151 #define MAX_gint32 G_MAXINT32
152
153 #define MAKE_INT_FUNCS(type,largetype)                                        \
154 static void                                                                   \
155 gst_audio_amplify_transform_##type##_clip (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     *d++ =  CLAMP (val, MIN_##type, MAX_##type);                              \
163   }                                                                           \
164 }                                                                             \
165 static void                                                                   \
166 gst_audio_amplify_transform_##type##_wrap_negative (GstAudioAmplify * filter, \
167     void * data, guint num_samples)                                           \
168 {                                                                             \
169   type *d = data;                                                             \
170                                                                               \
171   while (num_samples--) {                                                     \
172     largetype val = *d * filter->amplification;                               \
173     if (val > MAX_##type)                                                     \
174       val = MIN_##type + (val - MIN_##type) % ((largetype) MAX_##type + 1 -   \
175           MIN_##type);                                                        \
176     else if (val < MIN_##type)                                                \
177       val = MAX_##type - (MAX_##type - val) % ((largetype) MAX_##type + 1 -   \
178           MIN_##type);                                                        \
179     *d++ = val;                                                               \
180   }                                                                           \
181 }                                                                             \
182 static void                                                                   \
183 gst_audio_amplify_transform_##type##_wrap_positive (GstAudioAmplify * filter, \
184     void * data, guint num_samples)                                           \
185 {                                                                             \
186   type *d = data;                                                             \
187                                                                               \
188   while (num_samples--) {                                                     \
189     largetype val = *d * filter->amplification;                               \
190     do {                                                                      \
191       if (val > MAX_##type)                                                   \
192         val = MAX_##type - (val - MAX_##type);                                \
193       else if (val < MIN_##type)                                              \
194         val = MIN_##type + (MIN_##type - val);                                \
195       else                                                                    \
196         break;                                                                \
197     } while (1);                                                              \
198     *d++ = val;                                                               \
199   }                                                                           \
200 }                                                                             \
201 static void                                                                   \
202 gst_audio_amplify_transform_##type##_noclip (GstAudioAmplify * filter,        \
203     void * data, guint num_samples)                                           \
204 {                                                                             \
205   type *d = data;                                                             \
206                                                                               \
207   while (num_samples--)                                                       \
208     *d++ *= filter->amplification;                                            \
209 }
210
211 #define MAKE_FLOAT_FUNCS(type)                                                \
212 static void                                                                   \
213 gst_audio_amplify_transform_##type##_clip (GstAudioAmplify * filter,          \
214     void * data, guint num_samples)                                           \
215 {                                                                             \
216   type *d = data;                                                             \
217                                                                               \
218   while (num_samples--) {                                                     \
219     type val = *d* filter->amplification;                                     \
220     *d++ = CLAMP (val, -1.0, +1.0);                                           \
221   }                                                                           \
222 }                                                                             \
223 static void                                                                   \
224 gst_audio_amplify_transform_##type##_wrap_negative (GstAudioAmplify *         \
225     filter, void * data, guint num_samples)                                   \
226 {                                                                             \
227   type *d = data;                                                             \
228                                                                               \
229   while (num_samples--) {                                                     \
230     type val = *d * filter->amplification;                                    \
231     do {                                                                      \
232       if (val > 1.0)                                                          \
233         val = -1.0 + (val - 1.0);                                             \
234       else if (val < -1.0)                                                    \
235         val = 1.0 - (1.0 - val);                                              \
236       else                                                                    \
237         break;                                                                \
238     } while (1);                                                              \
239     *d++ = val;                                                               \
240   }                                                                           \
241 }                                                                             \
242 static void                                                                   \
243 gst_audio_amplify_transform_##type##_wrap_positive (GstAudioAmplify * filter, \
244     void * data, guint num_samples)                                           \
245 {                                                                             \
246   type *d = data;                                                             \
247                                                                               \
248   while (num_samples--) {                                                     \
249     type val = *d* filter->amplification;                                     \
250     do {                                                                      \
251       if (val > 1.0)                                                          \
252         val = 1.0 - (val - 1.0);                                              \
253       else if (val < -1.0)                                                    \
254         val = -1.0 + (-1.0 - val);                                            \
255       else                                                                    \
256         break;                                                                \
257     } while (1);                                                              \
258     *d++ = val;                                                               \
259   }                                                                           \
260 }                                                                             \
261 static void                                                                   \
262 gst_audio_amplify_transform_##type##_noclip (GstAudioAmplify * filter,        \
263     void * data, guint num_samples)                                           \
264 {                                                                             \
265   type *d = data;                                                             \
266                                                                               \
267   while (num_samples--)                                                       \
268     *d++ *= filter->amplification;                                            \
269 }
270
271 /* *INDENT-OFF* */
272 MAKE_INT_FUNCS (gint8,gint)
273 MAKE_INT_FUNCS (gint16,gint)
274 MAKE_INT_FUNCS (gint32,gint64)
275 MAKE_FLOAT_FUNCS (gfloat)
276 MAKE_FLOAT_FUNCS (gdouble)
277 /* *INDENT-ON* */
278
279 /* GObject vmethod implementations */
280
281 static void
282 gst_audio_amplify_base_init (gpointer klass)
283 {
284   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
285   GstCaps *caps;
286
287   gst_element_class_set_details_simple (element_class, "Audio amplifier",
288       "Filter/Effect/Audio",
289       "Amplifies an audio stream by a given factor",
290       "Sebastian Dröge <slomo@circular-chaos.org>");
291
292   caps = gst_caps_from_string (ALLOWED_CAPS);
293   gst_audio_filter_class_add_pad_templates (GST_AUDIO_FILTER_CLASS (klass),
294       caps);
295   gst_caps_unref (caps);
296 }
297
298 static void
299 gst_audio_amplify_class_init (GstAudioAmplifyClass * klass)
300 {
301   GObjectClass *gobject_class;
302
303   gobject_class = (GObjectClass *) klass;
304   gobject_class->set_property = gst_audio_amplify_set_property;
305   gobject_class->get_property = gst_audio_amplify_get_property;
306
307   g_object_class_install_property (gobject_class, PROP_AMPLIFICATION,
308       g_param_spec_float ("amplification", "Amplification",
309           "Factor of amplification", -G_MAXFLOAT, G_MAXFLOAT,
310           1.0,
311           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
312
313   /**
314    * GstAudioAmplify:clipping-method
315    *
316    * Clipping method: clip mode set values higher than the maximum to the
317    * maximum. The wrap-negative mode pushes those values back from the
318    * opposite side, wrap-positive pushes them back from the same side.
319    *
320    **/
321   g_object_class_install_property (gobject_class, PROP_CLIPPING_METHOD,
322       g_param_spec_enum ("clipping-method", "Clipping method",
323           "Selects how to handle values higher than the maximum",
324           GST_TYPE_AUDIO_AMPLIFY_CLIPPING_METHOD, METHOD_CLIP,
325           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
326
327   GST_AUDIO_FILTER_CLASS (klass)->setup =
328       GST_DEBUG_FUNCPTR (gst_audio_amplify_setup);
329   GST_BASE_TRANSFORM_CLASS (klass)->transform_ip =
330       GST_DEBUG_FUNCPTR (gst_audio_amplify_transform_ip);
331 }
332
333 static void
334 gst_audio_amplify_init (GstAudioAmplify * filter, GstAudioAmplifyClass * klass)
335 {
336   filter->amplification = 1.0;
337   gst_audio_amplify_set_process_function (filter, METHOD_CLIP,
338       GST_BUFTYPE_LINEAR, 16);
339   gst_base_transform_set_in_place (GST_BASE_TRANSFORM (filter), TRUE);
340   gst_base_transform_set_gap_aware (GST_BASE_TRANSFORM (filter), TRUE);
341 }
342
343 static GstAudioAmplifyProcessFunc
344 gst_audio_amplify_process_function (gint clipping, gint format, gint width)
345 {
346   static const struct process
347   {
348     gint format;
349     gint width;
350     gint clipping;
351     GstAudioAmplifyProcessFunc func;
352   } process[] = {
353     {
354     GST_BUFTYPE_FLOAT, 32, METHOD_CLIP,
355           gst_audio_amplify_transform_gfloat_clip}, {
356     GST_BUFTYPE_FLOAT, 32, METHOD_WRAP_NEGATIVE,
357           gst_audio_amplify_transform_gfloat_wrap_negative}, {
358     GST_BUFTYPE_FLOAT, 32, METHOD_WRAP_POSITIVE,
359           gst_audio_amplify_transform_gfloat_wrap_positive}, {
360     GST_BUFTYPE_FLOAT, 32, METHOD_NOCLIP,
361           gst_audio_amplify_transform_gfloat_noclip}, {
362     GST_BUFTYPE_FLOAT, 64, METHOD_CLIP,
363           gst_audio_amplify_transform_gdouble_clip}, {
364     GST_BUFTYPE_FLOAT, 64, METHOD_WRAP_NEGATIVE,
365           gst_audio_amplify_transform_gdouble_wrap_negative}, {
366     GST_BUFTYPE_FLOAT, 64, METHOD_WRAP_POSITIVE,
367           gst_audio_amplify_transform_gdouble_wrap_positive}, {
368     GST_BUFTYPE_FLOAT, 64, METHOD_NOCLIP,
369           gst_audio_amplify_transform_gdouble_noclip}, {
370     GST_BUFTYPE_LINEAR, 8, METHOD_CLIP, gst_audio_amplify_transform_gint8_clip}, {
371     GST_BUFTYPE_LINEAR, 8, METHOD_WRAP_NEGATIVE,
372           gst_audio_amplify_transform_gint8_wrap_negative}, {
373     GST_BUFTYPE_LINEAR, 8, METHOD_WRAP_POSITIVE,
374           gst_audio_amplify_transform_gint8_wrap_positive}, {
375     GST_BUFTYPE_LINEAR, 8, METHOD_NOCLIP,
376           gst_audio_amplify_transform_gint8_noclip}, {
377     GST_BUFTYPE_LINEAR, 16, METHOD_CLIP,
378           gst_audio_amplify_transform_gint16_clip}, {
379     GST_BUFTYPE_LINEAR, 16, METHOD_WRAP_NEGATIVE,
380           gst_audio_amplify_transform_gint16_wrap_negative}, {
381     GST_BUFTYPE_LINEAR, 16, METHOD_WRAP_POSITIVE,
382           gst_audio_amplify_transform_gint16_wrap_positive}, {
383     GST_BUFTYPE_LINEAR, 16, METHOD_NOCLIP,
384           gst_audio_amplify_transform_gint16_noclip}, {
385     GST_BUFTYPE_LINEAR, 32, METHOD_CLIP,
386           gst_audio_amplify_transform_gint32_clip}, {
387     GST_BUFTYPE_LINEAR, 32, METHOD_WRAP_NEGATIVE,
388           gst_audio_amplify_transform_gint32_wrap_negative}, {
389     GST_BUFTYPE_LINEAR, 32, METHOD_WRAP_POSITIVE,
390           gst_audio_amplify_transform_gint32_wrap_positive}, {
391     GST_BUFTYPE_LINEAR, 32, METHOD_NOCLIP,
392           gst_audio_amplify_transform_gint32_noclip}, {
393     0, 0, 0, NULL}
394   };
395   const struct process *p;
396
397   for (p = process; p->func; p++)
398     if (p->format == format && p->width == width && p->clipping == clipping)
399       return p->func;
400   return NULL;
401 }
402
403 static gboolean
404 gst_audio_amplify_set_process_function (GstAudioAmplify * filter, gint
405     clipping_method, gint format, gint width)
406 {
407   GstAudioAmplifyProcessFunc process;
408
409   /* set processing function */
410
411   process = gst_audio_amplify_process_function (clipping_method, format, width);
412   if (!process) {
413     GST_DEBUG ("wrong format");
414     return FALSE;
415   }
416
417   filter->process = process;
418   filter->clipping_method = clipping_method;
419   filter->format = format;
420   filter->width = width;
421
422   return TRUE;
423 }
424
425 static void
426 gst_audio_amplify_set_property (GObject * object, guint prop_id,
427     const GValue * value, GParamSpec * pspec)
428 {
429   GstAudioAmplify *filter = GST_AUDIO_AMPLIFY (object);
430
431   switch (prop_id) {
432     case PROP_AMPLIFICATION:
433       filter->amplification = g_value_get_float (value);
434       gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (filter),
435           filter->amplification == 1.0);
436       break;
437     case PROP_CLIPPING_METHOD:
438       gst_audio_amplify_set_process_function (filter, g_value_get_enum (value),
439           filter->format, filter->width);
440       break;
441     default:
442       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
443       break;
444   }
445 }
446
447 static void
448 gst_audio_amplify_get_property (GObject * object, guint prop_id,
449     GValue * value, GParamSpec * pspec)
450 {
451   GstAudioAmplify *filter = GST_AUDIO_AMPLIFY (object);
452
453   switch (prop_id) {
454     case PROP_AMPLIFICATION:
455       g_value_set_float (value, filter->amplification);
456       break;
457     case PROP_CLIPPING_METHOD:
458       g_value_set_enum (value, filter->clipping_method);
459       break;
460     default:
461       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
462       break;
463   }
464 }
465
466 /* GstAudioFilter vmethod implementations */
467 static gboolean
468 gst_audio_amplify_setup (GstAudioFilter * base, GstRingBufferSpec * format)
469 {
470   GstAudioAmplify *filter = GST_AUDIO_AMPLIFY (base);
471
472   return gst_audio_amplify_set_process_function (filter,
473       filter->clipping_method, format->type, format->width);
474 }
475
476 /* GstBaseTransform vmethod implementations */
477 static GstFlowReturn
478 gst_audio_amplify_transform_ip (GstBaseTransform * base, GstBuffer * buf)
479 {
480   GstAudioAmplify *filter = GST_AUDIO_AMPLIFY (base);
481   guint num_samples;
482   GstClockTime timestamp, stream_time;
483
484   timestamp = GST_BUFFER_TIMESTAMP (buf);
485   stream_time =
486       gst_segment_to_stream_time (&base->segment, GST_FORMAT_TIME, timestamp);
487
488   GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT,
489       GST_TIME_ARGS (timestamp));
490
491   if (GST_CLOCK_TIME_IS_VALID (stream_time))
492     gst_object_sync_values (G_OBJECT (filter), stream_time);
493
494   num_samples =
495       GST_BUFFER_SIZE (buf) / (GST_AUDIO_FILTER (filter)->format.width / 8);
496
497   if (gst_base_transform_is_passthrough (base) ||
498       G_UNLIKELY (GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_GAP)))
499     return GST_FLOW_OK;
500
501   filter->process (filter, GST_BUFFER_DATA (buf), num_samples);
502
503   return GST_FLOW_OK;
504 }