tizen 2.3.1 release
[framework/multimedia/gst-plugins-base0.10.git] / gst / volume / gstvolume.c
1 /* -*- c-basic-offset: 2 -*-
2  * vi:si:et:sw=2:sts=8:ts=8:expandtab
3  *
4  * GStreamer
5  * Copyright (C) 1999-2001 Erik Walthinsen <omega@cse.ogi.edu>
6  * Copyright (C) 2005 Andy Wingo <wingo@pobox.com>
7  * Copyright (C) 2010 Sebastian Dröge <sebastian.droege@collabora.co.uk>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Library General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Library General Public License for more details.
18  *
19  * You should have received a copy of the GNU Library General Public
20  * License along with this library; if not, write to the
21  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22  * Boston, MA 02111-1307, USA.
23  */
24
25 /**
26  * SECTION:element-volume
27  *
28  * The volume element changes the volume of the audio data.
29  *
30  * <refsect2>
31  * <title>Example launch line</title>
32  * |[
33  * gst-launch -v -m audiotestsrc ! volume volume=0.5 ! level ! fakesink silent=TRUE
34  * ]| This pipeline shows that the level of audiotestsrc has been halved
35  * (peak values are around -6 dB and RMS around -9 dB) compared to
36  * the same pipeline without the volume element.
37  * </refsect2>
38  */
39
40 #ifdef HAVE_CONFIG_H
41 #include "config.h"
42 #endif
43
44 #include <string.h>
45 #include <gst/gst.h>
46 #include <gst/base/gstbasetransform.h>
47 #include <gst/audio/audio.h>
48 #include <gst/interfaces/mixer.h>
49 #include <gst/controller/gstcontroller.h>
50 #include <gst/audio/audio.h>
51 #include <gst/audio/gstaudiofilter.h>
52
53 #ifdef HAVE_ORC
54 #include <orc/orcfunctions.h>
55 #else
56 #define orc_memset memset
57 #endif
58
59 #include "gstvolumeorc.h"
60 #include "gstvolume.h"
61
62 /* some defines for audio processing */
63 /* the volume factor is a range from 0.0 to (arbitrary) VOLUME_MAX_DOUBLE = 10.0
64  * we map 1.0 to VOLUME_UNITY_INT*
65  */
66 #define VOLUME_UNITY_INT8            8  /* internal int for unity 2^(8-5) */
67 #define VOLUME_UNITY_INT8_BIT_SHIFT  3  /* number of bits to shift for unity */
68 #define VOLUME_UNITY_INT16           2048       /* internal int for unity 2^(16-5) */
69 #define VOLUME_UNITY_INT16_BIT_SHIFT 11 /* number of bits to shift for unity */
70 #define VOLUME_UNITY_INT24           524288     /* internal int for unity 2^(24-5) */
71 #define VOLUME_UNITY_INT24_BIT_SHIFT 19 /* number of bits to shift for unity */
72 #define VOLUME_UNITY_INT32           134217728  /* internal int for unity 2^(32-5) */
73 #define VOLUME_UNITY_INT32_BIT_SHIFT 27
74 #define VOLUME_MAX_DOUBLE            10.0
75 #define VOLUME_MAX_INT8              G_MAXINT8
76 #define VOLUME_MIN_INT8              G_MININT8
77 #define VOLUME_MAX_INT16             G_MAXINT16
78 #define VOLUME_MIN_INT16             G_MININT16
79 #define VOLUME_MAX_INT24             8388607
80 #define VOLUME_MIN_INT24             -8388608
81 #define VOLUME_MAX_INT32             G_MAXINT32
82 #define VOLUME_MIN_INT32             G_MININT32
83
84 /* number of steps we use for the mixer interface to go from 0.0 to 1.0 */
85 # define VOLUME_STEPS           100
86
87 #define GST_CAT_DEFAULT gst_volume_debug
88 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
89
90 /* Filter signals and args */
91 enum
92 {
93   /* FILL ME */
94   LAST_SIGNAL
95 };
96
97 #define DEFAULT_PROP_MUTE       FALSE
98 #define DEFAULT_PROP_VOLUME     1.0
99
100 enum
101 {
102   PROP_0,
103   PROP_MUTE,
104   PROP_VOLUME
105 };
106
107 #define ALLOWED_CAPS \
108         "audio/x-raw-float, " \
109         "rate = (int) [ 1, MAX ], " \
110         "channels = (int) [ 1, MAX ], " \
111         "endianness = (int) BYTE_ORDER, " \
112         "width = (int) {32, 64}; " \
113         "audio/x-raw-int, " \
114         "channels = (int) [ 1, MAX ], " \
115         "rate = (int) [ 1,  MAX ], " \
116         "endianness = (int) BYTE_ORDER, " \
117         "width = (int) 8, " \
118         "depth = (int) 8, " \
119         "signed = (bool) TRUE; " \
120         "audio/x-raw-int, " \
121         "channels = (int) [ 1, MAX ], " \
122         "rate = (int) [ 1,  MAX ], " \
123         "endianness = (int) BYTE_ORDER, " \
124         "width = (int) 16, " \
125         "depth = (int) 16, " \
126         "signed = (bool) TRUE; " \
127         "audio/x-raw-int, " \
128         "channels = (int) [ 1, MAX ], " \
129         "rate = (int) [ 1,  MAX ], " \
130         "endianness = (int) BYTE_ORDER, " \
131         "width = (int) 24, " \
132         "depth = (int) 24, " \
133         "signed = (bool) TRUE; " \
134         "audio/x-raw-int, " \
135         "channels = (int) [ 1, MAX ], " \
136         "rate = (int) [ 1,  MAX ], " \
137         "endianness = (int) BYTE_ORDER, " \
138         "width = (int) 32, " \
139         "depth = (int) 32, " \
140         "signed = (bool) TRUE;" \
141         "audio/x-raw-int, " \
142         "channels = (int) [ 1, MAX ], " \
143         "rate = (int) [ 1,  MAX ], " \
144         "endianness = (int) BYTE_ORDER, " \
145         "width = (int) 32, " \
146         "depth = (int) 24, " \
147         "signed = (bool) TRUE"
148
149 static void gst_volume_interface_init (GstImplementsInterfaceClass * klass);
150 static void gst_volume_mixer_init (GstMixerClass * iface);
151
152 #define _init_interfaces(type)                                          \
153   {                                                                     \
154     static const GInterfaceInfo voliface_info = {                       \
155       (GInterfaceInitFunc) gst_volume_interface_init,                   \
156       NULL,                                                             \
157       NULL                                                              \
158     };                                                                  \
159     static const GInterfaceInfo volmixer_info = {                       \
160       (GInterfaceInitFunc) gst_volume_mixer_init,                       \
161       NULL,                                                             \
162       NULL                                                              \
163     };                                                                  \
164     static const GInterfaceInfo svol_info = {                           \
165       NULL,                                                             \
166       NULL,                                                             \
167       NULL                                                              \
168     };                                                                  \
169                                                                         \
170     g_type_add_interface_static (type, GST_TYPE_IMPLEMENTS_INTERFACE,   \
171         &voliface_info);                                                \
172     g_type_add_interface_static (type, GST_TYPE_MIXER, &volmixer_info); \
173     g_type_add_interface_static (type, GST_TYPE_STREAM_VOLUME, &svol_info); \
174   }
175
176 GST_BOILERPLATE_FULL (GstVolume, gst_volume, GstAudioFilter,
177     GST_TYPE_AUDIO_FILTER, _init_interfaces);
178
179 static void volume_set_property (GObject * object, guint prop_id,
180     const GValue * value, GParamSpec * pspec);
181 static void volume_get_property (GObject * object, guint prop_id,
182     GValue * value, GParamSpec * pspec);
183
184 static void volume_before_transform (GstBaseTransform * base,
185     GstBuffer * buffer);
186 static GstFlowReturn volume_transform_ip (GstBaseTransform * base,
187     GstBuffer * outbuf);
188 static gboolean volume_stop (GstBaseTransform * base);
189 static gboolean volume_setup (GstAudioFilter * filter,
190     GstRingBufferSpec * format);
191
192 static void volume_process_double (GstVolume * self, gpointer bytes,
193     guint n_bytes);
194 static void volume_process_controlled_double (GstVolume * self, gpointer bytes,
195     gdouble * volume, guint channels, guint n_bytes);
196 static void volume_process_float (GstVolume * self, gpointer bytes,
197     guint n_bytes);
198 static void volume_process_controlled_float (GstVolume * self, gpointer bytes,
199     gdouble * volume, guint channels, guint n_bytes);
200 static void volume_process_int32 (GstVolume * self, gpointer bytes,
201     guint n_bytes);
202 static void volume_process_int32_clamp (GstVolume * self, gpointer bytes,
203     guint n_bytes);
204 static void volume_process_controlled_int32_clamp (GstVolume * self,
205     gpointer bytes, gdouble * volume, guint channels, guint n_bytes);
206 static void volume_process_int24 (GstVolume * self, gpointer bytes,
207     guint n_bytes);
208 static void volume_process_int24_clamp (GstVolume * self, gpointer bytes,
209     guint n_bytes);
210 static void volume_process_controlled_int24_clamp (GstVolume * self,
211     gpointer bytes, gdouble * volume, guint channels, guint n_bytes);
212 static void volume_process_int16 (GstVolume * self, gpointer bytes,
213     guint n_bytes);
214 static void volume_process_int16_clamp (GstVolume * self, gpointer bytes,
215     guint n_bytes);
216 static void volume_process_controlled_int16_clamp (GstVolume * self,
217     gpointer bytes, gdouble * volume, guint channels, guint n_bytes);
218 static void volume_process_int8 (GstVolume * self, gpointer bytes,
219     guint n_bytes);
220 static void volume_process_int8_clamp (GstVolume * self, gpointer bytes,
221     guint n_bytes);
222 static void volume_process_controlled_int8_clamp (GstVolume * self,
223     gpointer bytes, gdouble * volume, guint channels, guint n_bytes);
224
225
226 /* helper functions */
227
228 static gboolean
229 volume_choose_func (GstVolume * self)
230 {
231   self->process = NULL;
232   self->process_controlled = NULL;
233
234   if (GST_AUDIO_FILTER (self)->format.caps == NULL)
235     return FALSE;
236
237   switch (GST_AUDIO_FILTER (self)->format.type) {
238     case GST_BUFTYPE_LINEAR:
239       switch (GST_AUDIO_FILTER (self)->format.width) {
240         case 32:
241           /* only clamp if the gain is greater than 1.0
242            */
243           if (self->current_vol_i32 > VOLUME_UNITY_INT32) {
244             self->process = volume_process_int32_clamp;
245           } else {
246             self->process = volume_process_int32;
247           }
248           self->process_controlled = volume_process_controlled_int32_clamp;
249           break;
250         case 24:
251           /* only clamp if the gain is greater than 1.0
252            */
253           if (self->current_vol_i24 > VOLUME_UNITY_INT24) {
254             self->process = volume_process_int24_clamp;
255           } else {
256             self->process = volume_process_int24;
257           }
258           self->process_controlled = volume_process_controlled_int24_clamp;
259           break;
260         case 16:
261           /* only clamp if the gain is greater than 1.0
262            */
263           if (self->current_vol_i16 > VOLUME_UNITY_INT16) {
264             self->process = volume_process_int16_clamp;
265           } else {
266             self->process = volume_process_int16;
267           }
268           self->process_controlled = volume_process_controlled_int16_clamp;
269           break;
270         case 8:
271           /* only clamp if the gain is greater than 1.0
272            */
273           if (self->current_vol_i8 > VOLUME_UNITY_INT8) {
274             self->process = volume_process_int8_clamp;
275           } else {
276             self->process = volume_process_int8;
277           }
278           self->process_controlled = volume_process_controlled_int8_clamp;
279           break;
280       }
281       break;
282     case GST_BUFTYPE_FLOAT:
283       switch (GST_AUDIO_FILTER (self)->format.width) {
284         case 32:
285           self->process = volume_process_float;
286           self->process_controlled = volume_process_controlled_float;
287           break;
288         case 64:
289           self->process = volume_process_double;
290           self->process_controlled = volume_process_controlled_double;
291           break;
292       }
293       break;
294     default:
295       break;
296   }
297
298   return (self->process != NULL);
299 }
300
301 static gboolean
302 volume_update_volume (GstVolume * self, gfloat volume, gboolean mute)
303 {
304   gboolean passthrough;
305   gboolean res;
306   GstController *controller;
307
308   GST_DEBUG_OBJECT (self, "configure mute %d, volume %f", mute, volume);
309
310   if (mute) {
311     self->current_mute = TRUE;
312     self->current_volume = 0.0;
313
314     self->current_vol_i8 = 0;
315     self->current_vol_i16 = 0;
316     self->current_vol_i24 = 0;
317     self->current_vol_i32 = 0;
318
319     passthrough = FALSE;
320   } else {
321     self->current_mute = FALSE;
322     self->current_volume = volume;
323
324     self->current_vol_i8 = volume * VOLUME_UNITY_INT8;
325     self->current_vol_i16 = volume * VOLUME_UNITY_INT16;
326     self->current_vol_i24 = volume * VOLUME_UNITY_INT24;
327     self->current_vol_i32 = volume * VOLUME_UNITY_INT32;
328
329     passthrough = (self->current_vol_i16 == VOLUME_UNITY_INT16);
330   }
331
332   /* If a controller is used, never use passthrough mode
333    * because the property can change from 1.0 to something
334    * else in the middle of a buffer.
335    */
336   controller = gst_object_get_controller (G_OBJECT (self));
337   passthrough = passthrough && (controller == NULL);
338
339   GST_DEBUG_OBJECT (self, "set passthrough %d", passthrough);
340
341   gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (self), passthrough);
342
343   res = self->negotiated = volume_choose_func (self);
344
345   return res;
346 }
347
348 /* Mixer interface */
349
350 static gboolean
351 gst_volume_interface_supported (GstImplementsInterface * iface, GType type)
352 {
353   return (type == GST_TYPE_MIXER || type == GST_TYPE_STREAM_VOLUME);
354 }
355
356 static void
357 gst_volume_interface_init (GstImplementsInterfaceClass * klass)
358 {
359   klass->supported = gst_volume_interface_supported;
360 }
361
362 static const GList *
363 gst_volume_list_tracks (GstMixer * mixer)
364 {
365   GstVolume *self = GST_VOLUME (mixer);
366
367   g_return_val_if_fail (self != NULL, NULL);
368   g_return_val_if_fail (GST_IS_VOLUME (self), NULL);
369
370   return self->tracklist;
371 }
372
373 static void
374 gst_volume_set_volume (GstMixer * mixer, GstMixerTrack * track, gint * volumes)
375 {
376   GstVolume *self = GST_VOLUME (mixer);
377
378   g_return_if_fail (self != NULL);
379   g_return_if_fail (GST_IS_VOLUME (self));
380
381   GST_OBJECT_LOCK (self);
382   self->volume = (gfloat) volumes[0] / VOLUME_STEPS;
383   GST_OBJECT_UNLOCK (self);
384 }
385
386 static void
387 gst_volume_get_volume (GstMixer * mixer, GstMixerTrack * track, gint * volumes)
388 {
389   GstVolume *self = GST_VOLUME (mixer);
390
391   g_return_if_fail (self != NULL);
392   g_return_if_fail (GST_IS_VOLUME (self));
393
394   GST_OBJECT_LOCK (self);
395   volumes[0] = (gint) self->volume * VOLUME_STEPS;
396   GST_OBJECT_UNLOCK (self);
397 }
398
399 static void
400 gst_volume_set_mute (GstMixer * mixer, GstMixerTrack * track, gboolean mute)
401 {
402   GstVolume *self = GST_VOLUME (mixer);
403
404   g_return_if_fail (self != NULL);
405   g_return_if_fail (GST_IS_VOLUME (self));
406
407   GST_OBJECT_LOCK (self);
408   self->mute = mute;
409   GST_OBJECT_UNLOCK (self);
410 }
411
412 static void
413 gst_volume_mixer_init (GstMixerClass * klass)
414 {
415   GST_MIXER_TYPE (klass) = GST_MIXER_SOFTWARE;
416
417   /* default virtual functions */
418   klass->list_tracks = gst_volume_list_tracks;
419   klass->set_volume = gst_volume_set_volume;
420   klass->get_volume = gst_volume_get_volume;
421   klass->set_mute = gst_volume_set_mute;
422 }
423
424 /* Element class */
425
426 static void
427 gst_volume_dispose (GObject * object)
428 {
429   GstVolume *volume = GST_VOLUME (object);
430
431   if (volume->tracklist) {
432     if (volume->tracklist->data)
433       g_object_unref (volume->tracklist->data);
434     g_list_free (volume->tracklist);
435     volume->tracklist = NULL;
436   }
437
438   G_OBJECT_CLASS (parent_class)->dispose (object);
439 }
440
441 static void
442 gst_volume_base_init (gpointer g_class)
443 {
444   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
445   GstAudioFilterClass *filter_class = GST_AUDIO_FILTER_CLASS (g_class);
446   GstCaps *caps;
447
448   gst_element_class_set_details_simple (element_class, "Volume",
449       "Filter/Effect/Audio",
450       "Set volume on audio/raw streams", "Andy Wingo <wingo@pobox.com>");
451
452   caps = gst_caps_from_string (ALLOWED_CAPS);
453   gst_audio_filter_class_add_pad_templates (filter_class, caps);
454   gst_caps_unref (caps);
455 }
456
457 static void
458 gst_volume_class_init (GstVolumeClass * klass)
459 {
460   GObjectClass *gobject_class;
461   GstBaseTransformClass *trans_class;
462   GstAudioFilterClass *filter_class;
463
464   gobject_class = (GObjectClass *) klass;
465   trans_class = (GstBaseTransformClass *) klass;
466   filter_class = (GstAudioFilterClass *) (klass);
467
468   gobject_class->set_property = volume_set_property;
469   gobject_class->get_property = volume_get_property;
470   gobject_class->dispose = gst_volume_dispose;
471
472   g_object_class_install_property (gobject_class, PROP_MUTE,
473       g_param_spec_boolean ("mute", "Mute", "mute channel",
474           DEFAULT_PROP_MUTE,
475           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
476
477   g_object_class_install_property (gobject_class, PROP_VOLUME,
478       g_param_spec_double ("volume", "Volume", "volume factor, 1.0=100%",
479           0.0, VOLUME_MAX_DOUBLE, DEFAULT_PROP_VOLUME,
480           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
481
482   trans_class->before_transform = GST_DEBUG_FUNCPTR (volume_before_transform);
483   trans_class->transform_ip = GST_DEBUG_FUNCPTR (volume_transform_ip);
484   trans_class->stop = GST_DEBUG_FUNCPTR (volume_stop);
485   filter_class->setup = GST_DEBUG_FUNCPTR (volume_setup);
486 }
487
488 static void
489 gst_volume_init (GstVolume * self, GstVolumeClass * g_class)
490 {
491   GstMixerTrack *track = NULL;
492
493   self->mute = DEFAULT_PROP_MUTE;;
494   self->volume = DEFAULT_PROP_VOLUME;
495
496   self->tracklist = NULL;
497   self->negotiated = FALSE;
498
499   track = g_object_new (GST_TYPE_MIXER_TRACK, NULL);
500
501   if (GST_IS_MIXER_TRACK (track)) {
502     track->label = g_strdup ("volume");
503     track->num_channels = 1;
504     track->min_volume = 0;
505     track->max_volume = VOLUME_STEPS;
506     track->flags = GST_MIXER_TRACK_SOFTWARE;
507     self->tracklist = g_list_append (self->tracklist, track);
508   }
509
510   gst_base_transform_set_gap_aware (GST_BASE_TRANSFORM (self), TRUE);
511 }
512
513 static void
514 volume_process_double (GstVolume * self, gpointer bytes, guint n_bytes)
515 {
516   gdouble *data = (gdouble *) bytes;
517   guint num_samples = n_bytes / sizeof (gdouble);
518
519   orc_scalarmultiply_f64_ns (data, self->current_volume, num_samples);
520 }
521
522 static void
523 volume_process_controlled_double (GstVolume * self, gpointer bytes,
524     gdouble * volume, guint channels, guint n_bytes)
525 {
526   gdouble *data = (gdouble *) bytes;
527   guint num_samples = n_bytes / (sizeof (gdouble) * channels);
528   guint i, j;
529   gdouble vol;
530
531   if (channels == 1) {
532     orc_process_controlled_f64_1ch (data, volume, num_samples);
533   } else {
534     for (i = 0; i < num_samples; i++) {
535       vol = *volume++;
536       for (j = 0; j < channels; j++) {
537         *data++ *= vol;
538       }
539     }
540   }
541 }
542
543 static void
544 volume_process_float (GstVolume * self, gpointer bytes, guint n_bytes)
545 {
546   gfloat *data = (gfloat *) bytes;
547   guint num_samples = n_bytes / sizeof (gfloat);
548
549   orc_scalarmultiply_f32_ns (data, self->current_volume, num_samples);
550 }
551
552 static void
553 volume_process_controlled_float (GstVolume * self, gpointer bytes,
554     gdouble * volume, guint channels, guint n_bytes)
555 {
556   gfloat *data = (gfloat *) bytes;
557   guint num_samples = n_bytes / (sizeof (gfloat) * channels);
558   guint i, j;
559   gdouble vol;
560
561   if (channels == 1) {
562     orc_process_controlled_f32_1ch (data, volume, num_samples);
563   } else if (channels == 2) {
564     orc_process_controlled_f32_2ch (data, volume, num_samples);
565   } else {
566     for (i = 0; i < num_samples; i++) {
567       vol = *volume++;
568       for (j = 0; j < channels; j++) {
569         *data++ *= vol;
570       }
571     }
572   }
573 }
574
575 static void
576 volume_process_int32 (GstVolume * self, gpointer bytes, guint n_bytes)
577 {
578   gint32 *data = (gint32 *) bytes;
579   guint num_samples = n_bytes / sizeof (gint);
580
581   /* hard coded in volume.orc */
582   g_assert (VOLUME_UNITY_INT32_BIT_SHIFT == 27);
583   orc_process_int32 (data, self->current_vol_i32, num_samples);
584 }
585
586 static void
587 volume_process_int32_clamp (GstVolume * self, gpointer bytes, guint n_bytes)
588 {
589   gint32 *data = (gint32 *) bytes;
590   guint num_samples = n_bytes / sizeof (gint);
591
592   /* hard coded in volume.orc */
593   g_assert (VOLUME_UNITY_INT32_BIT_SHIFT == 27);
594
595   orc_process_int32_clamp (data, self->current_vol_i32, num_samples);
596 }
597
598 static void
599 volume_process_controlled_int32_clamp (GstVolume * self, gpointer bytes,
600     gdouble * volume, guint channels, guint n_bytes)
601 {
602   gint32 *data = (gint32 *) bytes;
603   guint i, j;
604   guint num_samples = n_bytes / (sizeof (gint32) * channels);
605   gdouble vol, val;
606
607   if (channels == 1) {
608     orc_process_controlled_int32_1ch (data, volume, num_samples);
609   } else {
610     for (i = 0; i < num_samples; i++) {
611       vol = *volume++;
612       for (j = 0; j < channels; j++) {
613         val = *data * vol;
614         *data++ = (gint32) CLAMP (val, VOLUME_MIN_INT32, VOLUME_MAX_INT32);
615       }
616     }
617   }
618 }
619
620 #if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
621 #define get_unaligned_i24(_x) ( (((guint8*)_x)[0]) | ((((guint8*)_x)[1]) << 8) | ((((gint8*)_x)[2]) << 16) )
622
623 #define write_unaligned_u24(_x,samp) \
624 G_STMT_START { \
625   *(_x)++ = samp & 0xFF; \
626   *(_x)++ = (samp >> 8) & 0xFF; \
627   *(_x)++ = (samp >> 16) & 0xFF; \
628 } G_STMT_END
629
630 #else /* BIG ENDIAN */
631 #define get_unaligned_i24(_x) ( (((guint8*)_x)[2]) | ((((guint8*)_x)[1]) << 8) | ((((gint8*)_x)[0]) << 16) )
632 #define write_unaligned_u24(_x,samp) \
633 G_STMT_START { \
634   *(_x)++ = (samp >> 16) & 0xFF; \
635   *(_x)++ = (samp >> 8) & 0xFF; \
636   *(_x)++ = samp & 0xFF; \
637 } G_STMT_END
638 #endif
639
640 static void
641 volume_process_int24 (GstVolume * self, gpointer bytes, guint n_bytes)
642 {
643   gint8 *data = (gint8 *) bytes;        /* treat the data as a byte stream */
644   guint i, num_samples;
645   guint32 samp;
646   gint64 val;
647
648   num_samples = n_bytes / (sizeof (gint8) * 3);
649   for (i = 0; i < num_samples; i++) {
650     samp = get_unaligned_i24 (data);
651
652     val = (gint32) samp;
653     val =
654         (((gint64) self->current_vol_i24 *
655             val) >> VOLUME_UNITY_INT24_BIT_SHIFT);
656     samp = (guint32) val;
657
658     /* write the value back into the stream */
659     write_unaligned_u24 (data, samp);
660   }
661 }
662
663 static void
664 volume_process_int24_clamp (GstVolume * self, gpointer bytes, guint n_bytes)
665 {
666   gint8 *data = (gint8 *) bytes;        /* treat the data as a byte stream */
667   guint i, num_samples;
668   guint32 samp;
669   gint64 val;
670
671   num_samples = n_bytes / (sizeof (gint8) * 3);
672   for (i = 0; i < num_samples; i++) {
673     samp = get_unaligned_i24 (data);
674
675     val = (gint32) samp;
676     val =
677         (((gint64) self->current_vol_i24 *
678             val) >> VOLUME_UNITY_INT24_BIT_SHIFT);
679     samp = (guint32) CLAMP (val, VOLUME_MIN_INT24, VOLUME_MAX_INT24);
680
681     /* write the value back into the stream */
682     write_unaligned_u24 (data, samp);
683   }
684 }
685
686 static void
687 volume_process_controlled_int24_clamp (GstVolume * self, gpointer bytes,
688     gdouble * volume, guint channels, guint n_bytes)
689 {
690   gint8 *data = (gint8 *) bytes;        /* treat the data as a byte stream */
691   guint i, j;
692   guint num_samples = n_bytes / (sizeof (gint8) * 3 * channels);
693   gdouble vol, val;
694
695   for (i = 0; i < num_samples; i++) {
696     vol = *volume++;
697     for (j = 0; j < channels; j++) {
698       val = get_unaligned_i24 (data) * vol;
699       val = CLAMP (val, VOLUME_MIN_INT24, VOLUME_MAX_INT24);
700       write_unaligned_u24 (data, (gint32) val);
701     }
702   }
703 }
704
705 static void
706 volume_process_int16 (GstVolume * self, gpointer bytes, guint n_bytes)
707 {
708   gint16 *data = (gint16 *) bytes;
709   guint num_samples = n_bytes / sizeof (gint16);
710
711   /* hard coded in volume.orc */
712   g_assert (VOLUME_UNITY_INT16_BIT_SHIFT == 11);
713
714   orc_process_int16 (data, self->current_vol_i16, num_samples);
715 }
716
717 static void
718 volume_process_int16_clamp (GstVolume * self, gpointer bytes, guint n_bytes)
719 {
720   gint16 *data = (gint16 *) bytes;
721   guint num_samples = n_bytes / sizeof (gint16);
722
723   /* hard coded in volume.orc */
724   g_assert (VOLUME_UNITY_INT16_BIT_SHIFT == 11);
725
726   orc_process_int16_clamp (data, self->current_vol_i16, num_samples);
727 }
728
729 static void
730 volume_process_controlled_int16_clamp (GstVolume * self, gpointer bytes,
731     gdouble * volume, guint channels, guint n_bytes)
732 {
733   gint16 *data = (gint16 *) bytes;
734   guint i, j;
735   guint num_samples = n_bytes / (sizeof (gint16) * channels);
736   gdouble vol, val;
737
738   if (channels == 1) {
739     orc_process_controlled_int16_1ch (data, volume, num_samples);
740   } else if (channels == 2) {
741     orc_process_controlled_int16_2ch (data, volume, num_samples);
742   } else {
743     for (i = 0; i < num_samples; i++) {
744       vol = *volume++;
745       for (j = 0; j < channels; j++) {
746         val = *data * vol;
747         *data++ = (gint16) CLAMP (val, VOLUME_MIN_INT16, VOLUME_MAX_INT16);
748       }
749     }
750   }
751 }
752
753 static void
754 volume_process_int8 (GstVolume * self, gpointer bytes, guint n_bytes)
755 {
756   gint8 *data = (gint8 *) bytes;
757   guint num_samples = n_bytes / sizeof (gint8);
758
759   /* hard coded in volume.orc */
760   g_assert (VOLUME_UNITY_INT8_BIT_SHIFT == 3);
761
762   orc_process_int8 (data, self->current_vol_i8, num_samples);
763 }
764
765 static void
766 volume_process_int8_clamp (GstVolume * self, gpointer bytes, guint n_bytes)
767 {
768   gint8 *data = (gint8 *) bytes;
769   guint num_samples = n_bytes / sizeof (gint8);
770
771   /* hard coded in volume.orc */
772   g_assert (VOLUME_UNITY_INT8_BIT_SHIFT == 3);
773
774   orc_process_int8_clamp (data, self->current_vol_i8, num_samples);
775 }
776
777 static void
778 volume_process_controlled_int8_clamp (GstVolume * self, gpointer bytes,
779     gdouble * volume, guint channels, guint n_bytes)
780 {
781   gint8 *data = (gint8 *) bytes;
782   guint i, j;
783   guint num_samples = n_bytes / (sizeof (gint8) * channels);
784   gdouble val, vol;
785
786   if (channels == 1) {
787     orc_process_controlled_int8_1ch (data, volume, num_samples);
788   } else if (channels == 2) {
789     orc_process_controlled_int8_2ch (data, volume, num_samples);
790   } else {
791     for (i = 0; i < num_samples; i++) {
792       vol = *volume++;
793       for (j = 0; j < channels; j++) {
794         val = *data * vol;
795         *data++ = (gint8) CLAMP (val, VOLUME_MIN_INT8, VOLUME_MAX_INT8);
796       }
797     }
798   }
799 }
800
801 /* GstBaseTransform vmethod implementations */
802
803 /* get notified of caps and plug in the correct process function */
804 static gboolean
805 volume_setup (GstAudioFilter * filter, GstRingBufferSpec * format)
806 {
807   gboolean res;
808   GstVolume *self = GST_VOLUME (filter);
809   gfloat volume;
810   gboolean mute;
811
812   GST_OBJECT_LOCK (self);
813   volume = self->volume;
814   mute = self->mute;
815   GST_OBJECT_UNLOCK (self);
816
817   res = volume_update_volume (self, volume, mute);
818   if (!res) {
819     GST_ELEMENT_ERROR (self, CORE, NEGOTIATION,
820         ("Invalid incoming format"), (NULL));
821   }
822   self->negotiated = res;
823
824   return res;
825 }
826
827 static gboolean
828 volume_stop (GstBaseTransform * base)
829 {
830   GstVolume *self = GST_VOLUME (base);
831
832   g_free (self->volumes);
833   self->volumes = NULL;
834   self->volumes_count = 0;
835
836   g_free (self->mutes);
837   self->mutes = NULL;
838   self->mutes_count = 0;
839
840   return GST_CALL_PARENT_WITH_DEFAULT (GST_BASE_TRANSFORM_CLASS, stop, (base),
841       TRUE);
842 }
843
844 static void
845 volume_before_transform (GstBaseTransform * base, GstBuffer * buffer)
846 {
847   GstClockTime timestamp;
848   GstVolume *self = GST_VOLUME (base);
849   gfloat volume;
850   gboolean mute;
851
852   timestamp = GST_BUFFER_TIMESTAMP (buffer);
853   timestamp =
854       gst_segment_to_stream_time (&base->segment, GST_FORMAT_TIME, timestamp);
855
856   GST_DEBUG_OBJECT (base, "sync to %" GST_TIME_FORMAT,
857       GST_TIME_ARGS (timestamp));
858
859   if (GST_CLOCK_TIME_IS_VALID (timestamp))
860     gst_object_sync_values (G_OBJECT (self), timestamp);
861
862   /* get latest values */
863   GST_OBJECT_LOCK (self);
864   volume = self->volume;
865   mute = self->mute;
866   GST_OBJECT_UNLOCK (self);
867
868   if ((volume != self->current_volume) || (mute != self->current_mute)) {
869     /* the volume or mute was updated, update our internal state before
870      * we continue processing. */
871     volume_update_volume (self, volume, mute);
872   }
873 }
874
875 /* call the plugged-in process function for this instance
876  * needs to be done with this indirection since volume_transform is
877  * a class-global method
878  */
879 static GstFlowReturn
880 volume_transform_ip (GstBaseTransform * base, GstBuffer * outbuf)
881 {
882   GstVolume *self = GST_VOLUME (base);
883   guint8 *data;
884   guint size;
885   GstControlSource *mute_csource, *volume_csource;
886
887   if (G_UNLIKELY (!self->negotiated))
888     goto not_negotiated;
889
890   /* don't process data in passthrough-mode */
891   if (gst_base_transform_is_passthrough (base) ||
892       GST_BUFFER_FLAG_IS_SET (outbuf, GST_BUFFER_FLAG_GAP))
893     return GST_FLOW_OK;
894
895   data = GST_BUFFER_DATA (outbuf);
896   size = GST_BUFFER_SIZE (outbuf);
897
898   mute_csource = gst_object_get_control_source (G_OBJECT (self), "mute");
899   volume_csource = gst_object_get_control_source (G_OBJECT (self), "volume");
900   if (mute_csource || (volume_csource && !self->current_mute)) {
901     gint rate = GST_AUDIO_FILTER_CAST (self)->format.rate;
902     gint width = GST_AUDIO_FILTER_CAST (self)->format.width / 8;
903     gint channels = GST_AUDIO_FILTER_CAST (self)->format.channels;
904     guint nsamples = size / (width * channels);
905     GstClockTime interval = gst_util_uint64_scale_int (1, GST_SECOND, rate);
906     GstClockTime ts = GST_BUFFER_TIMESTAMP (outbuf);
907     gboolean use_mutes = FALSE;
908
909     ts = gst_segment_to_stream_time (&base->segment, GST_FORMAT_TIME, ts);
910
911     if (self->mutes_count < nsamples && mute_csource) {
912       self->mutes = g_realloc (self->mutes, sizeof (gboolean) * nsamples);
913       self->mutes_count = nsamples;
914     }
915
916     if (self->volumes_count < nsamples) {
917       self->volumes = g_realloc (self->volumes, sizeof (gdouble) * nsamples);
918       self->volumes_count = nsamples;
919     }
920
921     if (mute_csource) {
922       GstValueArray va = { "mute", nsamples, interval, (gpointer) self->mutes };
923
924       if (!gst_control_source_get_value_array (mute_csource, ts, &va))
925         goto controller_failure;
926
927       gst_object_unref (mute_csource);
928       mute_csource = NULL;
929       use_mutes = TRUE;
930     } else {
931       g_free (self->mutes);
932       self->mutes = NULL;
933       self->mutes_count = 0;
934     }
935
936     if (volume_csource) {
937       GstValueArray va =
938           { "volume", nsamples, interval, (gpointer) self->volumes };
939
940       if (!gst_control_source_get_value_array (volume_csource, ts, &va))
941         goto controller_failure;
942
943       gst_object_unref (volume_csource);
944       volume_csource = NULL;
945     } else {
946       orc_memset_f64 (self->volumes, self->current_volume, nsamples);
947     }
948
949     if (use_mutes) {
950       orc_prepare_volumes (self->volumes, self->mutes, nsamples);
951     }
952
953     self->process_controlled (self, data, self->volumes, channels, size);
954
955     return GST_FLOW_OK;
956   } else if (volume_csource) {
957     gst_object_unref (volume_csource);
958   }
959
960   if (self->current_volume == 0.0 || self->current_mute) {
961     orc_memset (data, 0, size);
962     GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_GAP);
963   } else if (self->current_volume != 1.0) {
964     self->process (self, data, size);
965   }
966
967   return GST_FLOW_OK;
968
969   /* ERRORS */
970 not_negotiated:
971   {
972     GST_ELEMENT_ERROR (self, CORE, NEGOTIATION,
973         ("No format was negotiated"), (NULL));
974     return GST_FLOW_NOT_NEGOTIATED;
975   }
976 controller_failure:
977   {
978     if (mute_csource)
979       gst_object_unref (mute_csource);
980     if (volume_csource)
981       gst_object_unref (volume_csource);
982
983     GST_ELEMENT_ERROR (self, CORE, FAILED,
984         ("Failed to get values from controller"), (NULL));
985     return GST_FLOW_ERROR;
986   }
987 }
988
989 static void
990 volume_set_property (GObject * object, guint prop_id, const GValue * value,
991     GParamSpec * pspec)
992 {
993   GstVolume *self = GST_VOLUME (object);
994
995   switch (prop_id) {
996     case PROP_MUTE:
997       GST_OBJECT_LOCK (self);
998       self->mute = g_value_get_boolean (value);
999       GST_OBJECT_UNLOCK (self);
1000       break;
1001     case PROP_VOLUME:
1002       GST_OBJECT_LOCK (self);
1003       self->volume = g_value_get_double (value);
1004       GST_OBJECT_UNLOCK (self);
1005       break;
1006     default:
1007       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1008       break;
1009   }
1010 }
1011
1012 static void
1013 volume_get_property (GObject * object, guint prop_id, GValue * value,
1014     GParamSpec * pspec)
1015 {
1016   GstVolume *self = GST_VOLUME (object);
1017
1018   switch (prop_id) {
1019     case PROP_MUTE:
1020       GST_OBJECT_LOCK (self);
1021       g_value_set_boolean (value, self->mute);
1022       GST_OBJECT_UNLOCK (self);
1023       break;
1024     case PROP_VOLUME:
1025       GST_OBJECT_LOCK (self);
1026       g_value_set_double (value, self->volume);
1027       GST_OBJECT_UNLOCK (self);
1028       break;
1029     default:
1030       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1031       break;
1032   }
1033 }
1034
1035 static gboolean
1036 plugin_init (GstPlugin * plugin)
1037 {
1038   gst_volume_orc_init ();
1039
1040   /* initialize gst controller library */
1041   gst_controller_init (NULL, NULL);
1042
1043   GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "volume", 0, "Volume gain");
1044
1045   /* ref class from a thread-safe context to work around missing bit of
1046    * thread-safety in GObject */
1047   g_type_class_ref (GST_TYPE_MIXER_TRACK);
1048
1049   return gst_element_register (plugin, "volume", GST_RANK_NONE,
1050       GST_TYPE_VOLUME);
1051 }
1052
1053 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1054     GST_VERSION_MINOR,
1055     "volume",
1056     "plugin for controlling audio volume",
1057     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);