Merge branch 'master' into 0.11
[platform/upstream/gstreamer.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            32 /* internal int for unity 2^(8-3) */
67 #define VOLUME_UNITY_INT8_BIT_SHIFT  5  /* number of bits to shift for unity */
68 #define VOLUME_UNITY_INT16           8192       /* internal int for unity 2^(16-3) */
69 #define VOLUME_UNITY_INT16_BIT_SHIFT 13 /* number of bits to shift for unity */
70 #define VOLUME_UNITY_INT24           2097152    /* internal int for unity 2^(24-3) */
71 #define VOLUME_UNITY_INT24_BIT_SHIFT 21 /* 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
142 static void gst_volume_interface_init (GstImplementsInterfaceClass * klass);
143 static void gst_volume_mixer_init (GstMixerClass * iface);
144
145 #define gst_volume_parent_class parent_class
146 G_DEFINE_TYPE_WITH_CODE (GstVolume, gst_volume,
147     GST_TYPE_AUDIO_FILTER, G_IMPLEMENT_INTERFACE (GST_TYPE_IMPLEMENTS_INTERFACE,
148         gst_volume_interface_init);
149     G_IMPLEMENT_INTERFACE (GST_TYPE_MIXER, gst_volume_mixer_init);
150     G_IMPLEMENT_INTERFACE (GST_TYPE_STREAM_VOLUME, NULL));
151
152 static void volume_set_property (GObject * object, guint prop_id,
153     const GValue * value, GParamSpec * pspec);
154 static void volume_get_property (GObject * object, guint prop_id,
155     GValue * value, GParamSpec * pspec);
156
157 static void volume_before_transform (GstBaseTransform * base,
158     GstBuffer * buffer);
159 static GstFlowReturn volume_transform_ip (GstBaseTransform * base,
160     GstBuffer * outbuf);
161 static gboolean volume_stop (GstBaseTransform * base);
162 static gboolean volume_setup (GstAudioFilter * filter,
163     GstRingBufferSpec * format);
164
165 static void volume_process_double (GstVolume * self, gpointer bytes,
166     guint n_bytes);
167 static void volume_process_controlled_double (GstVolume * self, gpointer bytes,
168     gdouble * volume, guint channels, guint n_bytes);
169 static void volume_process_float (GstVolume * self, gpointer bytes,
170     guint n_bytes);
171 static void volume_process_controlled_float (GstVolume * self, gpointer bytes,
172     gdouble * volume, guint channels, guint n_bytes);
173 static void volume_process_int32 (GstVolume * self, gpointer bytes,
174     guint n_bytes);
175 static void volume_process_int32_clamp (GstVolume * self, gpointer bytes,
176     guint n_bytes);
177 static void volume_process_controlled_int32_clamp (GstVolume * self,
178     gpointer bytes, gdouble * volume, guint channels, guint n_bytes);
179 static void volume_process_int24 (GstVolume * self, gpointer bytes,
180     guint n_bytes);
181 static void volume_process_int24_clamp (GstVolume * self, gpointer bytes,
182     guint n_bytes);
183 static void volume_process_controlled_int24_clamp (GstVolume * self,
184     gpointer bytes, gdouble * volume, guint channels, guint n_bytes);
185 static void volume_process_int16 (GstVolume * self, gpointer bytes,
186     guint n_bytes);
187 static void volume_process_int16_clamp (GstVolume * self, gpointer bytes,
188     guint n_bytes);
189 static void volume_process_controlled_int16_clamp (GstVolume * self,
190     gpointer bytes, gdouble * volume, guint channels, guint n_bytes);
191 static void volume_process_int8 (GstVolume * self, gpointer bytes,
192     guint n_bytes);
193 static void volume_process_int8_clamp (GstVolume * self, gpointer bytes,
194     guint n_bytes);
195 static void volume_process_controlled_int8_clamp (GstVolume * self,
196     gpointer bytes, gdouble * volume, guint channels, guint n_bytes);
197
198
199 /* helper functions */
200
201 static gboolean
202 volume_choose_func (GstVolume * self)
203 {
204   self->process = NULL;
205   self->process_controlled = NULL;
206
207   if (GST_AUDIO_FILTER (self)->format.caps == NULL)
208     return FALSE;
209
210   switch (GST_AUDIO_FILTER (self)->format.type) {
211     case GST_BUFTYPE_LINEAR:
212       switch (GST_AUDIO_FILTER (self)->format.width) {
213         case 32:
214           /* only clamp if the gain is greater than 1.0
215            */
216           if (self->current_vol_i32 > VOLUME_UNITY_INT32) {
217             self->process = volume_process_int32_clamp;
218           } else {
219             self->process = volume_process_int32;
220           }
221           self->process_controlled = volume_process_controlled_int32_clamp;
222           break;
223         case 24:
224           /* only clamp if the gain is greater than 1.0
225            */
226           if (self->current_vol_i24 > VOLUME_UNITY_INT24) {
227             self->process = volume_process_int24_clamp;
228           } else {
229             self->process = volume_process_int24;
230           }
231           self->process_controlled = volume_process_controlled_int24_clamp;
232           break;
233         case 16:
234           /* only clamp if the gain is greater than 1.0
235            */
236           if (self->current_vol_i16 > VOLUME_UNITY_INT16) {
237             self->process = volume_process_int16_clamp;
238           } else {
239             self->process = volume_process_int16;
240           }
241           self->process_controlled = volume_process_controlled_int16_clamp;
242           break;
243         case 8:
244           /* only clamp if the gain is greater than 1.0
245            */
246           if (self->current_vol_i16 > VOLUME_UNITY_INT8) {
247             self->process = volume_process_int8_clamp;
248           } else {
249             self->process = volume_process_int8;
250           }
251           self->process_controlled = volume_process_controlled_int8_clamp;
252           break;
253       }
254       break;
255     case GST_BUFTYPE_FLOAT:
256       switch (GST_AUDIO_FILTER (self)->format.width) {
257         case 32:
258           self->process = volume_process_float;
259           self->process_controlled = volume_process_controlled_float;
260           break;
261         case 64:
262           self->process = volume_process_double;
263           self->process_controlled = volume_process_controlled_double;
264           break;
265       }
266       break;
267     default:
268       break;
269   }
270
271   return (self->process != NULL);
272 }
273
274 static gboolean
275 volume_update_volume (GstVolume * self, gfloat volume, gboolean mute)
276 {
277   gboolean passthrough;
278   gboolean res;
279   GstController *controller;
280
281   GST_DEBUG_OBJECT (self, "configure mute %d, volume %f", mute, volume);
282
283   if (mute) {
284     self->current_mute = TRUE;
285     self->current_volume = 0.0;
286
287     self->current_vol_i8 = 0;
288     self->current_vol_i16 = 0;
289     self->current_vol_i24 = 0;
290     self->current_vol_i32 = 0;
291
292     passthrough = FALSE;
293   } else {
294     self->current_mute = FALSE;
295     self->current_volume = volume;
296
297     self->current_vol_i8 = volume * VOLUME_UNITY_INT8;
298     self->current_vol_i16 = volume * VOLUME_UNITY_INT16;
299     self->current_vol_i24 = volume * VOLUME_UNITY_INT24;
300     self->current_vol_i32 = volume * VOLUME_UNITY_INT32;
301
302     passthrough = (self->current_vol_i16 == VOLUME_UNITY_INT16);
303   }
304
305   /* If a controller is used, never use passthrough mode
306    * because the property can change from 1.0 to something
307    * else in the middle of a buffer.
308    */
309   controller = gst_object_get_controller (G_OBJECT (self));
310   passthrough = passthrough && (controller == NULL);
311
312   GST_DEBUG_OBJECT (self, "set passthrough %d", passthrough);
313
314   gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (self), passthrough);
315
316   res = self->negotiated = volume_choose_func (self);
317
318   return res;
319 }
320
321 /* Mixer interface */
322
323 static gboolean
324 gst_volume_interface_supported (GstImplementsInterface * iface, GType type)
325 {
326   return (type == GST_TYPE_MIXER || type == GST_TYPE_STREAM_VOLUME);
327 }
328
329 static void
330 gst_volume_interface_init (GstImplementsInterfaceClass * klass)
331 {
332   klass->supported = gst_volume_interface_supported;
333 }
334
335 static const GList *
336 gst_volume_list_tracks (GstMixer * mixer)
337 {
338   GstVolume *self = GST_VOLUME (mixer);
339
340   g_return_val_if_fail (self != NULL, NULL);
341   g_return_val_if_fail (GST_IS_VOLUME (self), NULL);
342
343   return self->tracklist;
344 }
345
346 static void
347 gst_volume_set_volume (GstMixer * mixer, GstMixerTrack * track, gint * volumes)
348 {
349   GstVolume *self = GST_VOLUME (mixer);
350
351   g_return_if_fail (self != NULL);
352   g_return_if_fail (GST_IS_VOLUME (self));
353
354   GST_OBJECT_LOCK (self);
355   self->volume = (gfloat) volumes[0] / VOLUME_STEPS;
356   GST_OBJECT_UNLOCK (self);
357 }
358
359 static void
360 gst_volume_get_volume (GstMixer * mixer, GstMixerTrack * track, gint * volumes)
361 {
362   GstVolume *self = GST_VOLUME (mixer);
363
364   g_return_if_fail (self != NULL);
365   g_return_if_fail (GST_IS_VOLUME (self));
366
367   GST_OBJECT_LOCK (self);
368   volumes[0] = (gint) self->volume * VOLUME_STEPS;
369   GST_OBJECT_UNLOCK (self);
370 }
371
372 static void
373 gst_volume_set_mute (GstMixer * mixer, GstMixerTrack * track, gboolean mute)
374 {
375   GstVolume *self = GST_VOLUME (mixer);
376
377   g_return_if_fail (self != NULL);
378   g_return_if_fail (GST_IS_VOLUME (self));
379
380   GST_OBJECT_LOCK (self);
381   self->mute = mute;
382   GST_OBJECT_UNLOCK (self);
383 }
384
385 static void
386 gst_volume_mixer_init (GstMixerClass * klass)
387 {
388   GST_MIXER_TYPE (klass) = GST_MIXER_SOFTWARE;
389
390   /* default virtual functions */
391   klass->list_tracks = gst_volume_list_tracks;
392   klass->set_volume = gst_volume_set_volume;
393   klass->get_volume = gst_volume_get_volume;
394   klass->set_mute = gst_volume_set_mute;
395 }
396
397 /* Element class */
398
399 static void
400 gst_volume_dispose (GObject * object)
401 {
402   GstVolume *volume = GST_VOLUME (object);
403
404   if (volume->tracklist) {
405     if (volume->tracklist->data)
406       g_object_unref (volume->tracklist->data);
407     g_list_free (volume->tracklist);
408     volume->tracklist = NULL;
409   }
410
411   G_OBJECT_CLASS (parent_class)->dispose (object);
412 }
413
414 static void
415 gst_volume_class_init (GstVolumeClass * klass)
416 {
417   GObjectClass *gobject_class;
418   GstElementClass *element_class;
419   GstBaseTransformClass *trans_class;
420   GstAudioFilterClass *filter_class;
421   GstCaps *caps;
422
423   gobject_class = (GObjectClass *) klass;
424   element_class = (GstElementClass *) klass;
425   trans_class = (GstBaseTransformClass *) klass;
426   filter_class = (GstAudioFilterClass *) (klass);
427
428   gobject_class->set_property = volume_set_property;
429   gobject_class->get_property = volume_get_property;
430   gobject_class->dispose = gst_volume_dispose;
431
432   g_object_class_install_property (gobject_class, PROP_MUTE,
433       g_param_spec_boolean ("mute", "Mute", "mute channel",
434           DEFAULT_PROP_MUTE,
435           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
436
437   g_object_class_install_property (gobject_class, PROP_VOLUME,
438       g_param_spec_double ("volume", "Volume", "volume factor, 1.0=100%",
439           0.0, VOLUME_MAX_DOUBLE, DEFAULT_PROP_VOLUME,
440           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
441
442   gst_element_class_set_details_simple (element_class, "Volume",
443       "Filter/Effect/Audio",
444       "Set volume on audio/raw streams", "Andy Wingo <wingo@pobox.com>");
445
446   caps = gst_caps_from_string (ALLOWED_CAPS);
447   gst_audio_filter_class_add_pad_templates (filter_class, caps);
448   gst_caps_unref (caps);
449
450   trans_class->before_transform = GST_DEBUG_FUNCPTR (volume_before_transform);
451   trans_class->transform_ip = GST_DEBUG_FUNCPTR (volume_transform_ip);
452   trans_class->stop = GST_DEBUG_FUNCPTR (volume_stop);
453   filter_class->setup = GST_DEBUG_FUNCPTR (volume_setup);
454 }
455
456 static void
457 gst_volume_init (GstVolume * self)
458 {
459   GstMixerTrack *track = NULL;
460
461   self->mute = DEFAULT_PROP_MUTE;;
462   self->volume = DEFAULT_PROP_VOLUME;
463
464   self->tracklist = NULL;
465   self->negotiated = FALSE;
466
467   track = g_object_new (GST_TYPE_MIXER_TRACK, NULL);
468
469   if (GST_IS_MIXER_TRACK (track)) {
470     track->label = g_strdup ("volume");
471     track->num_channels = 1;
472     track->min_volume = 0;
473     track->max_volume = VOLUME_STEPS;
474     track->flags = GST_MIXER_TRACK_SOFTWARE;
475     self->tracklist = g_list_append (self->tracklist, track);
476   }
477
478   gst_base_transform_set_gap_aware (GST_BASE_TRANSFORM (self), TRUE);
479 }
480
481 static void
482 volume_process_double (GstVolume * self, gpointer bytes, guint n_bytes)
483 {
484   gdouble *data = (gdouble *) bytes;
485   guint num_samples = n_bytes / sizeof (gdouble);
486
487   orc_scalarmultiply_f64_ns (data, self->current_volume, num_samples);
488 }
489
490 static void
491 volume_process_controlled_double (GstVolume * self, gpointer bytes,
492     gdouble * volume, guint channels, guint n_bytes)
493 {
494   gdouble *data = (gdouble *) bytes;
495   guint num_samples = n_bytes / (sizeof (gdouble) * channels);
496   guint i, j;
497   gdouble vol;
498
499   if (channels == 1) {
500     orc_process_controlled_f64_1ch (data, volume, num_samples);
501   } else {
502     for (i = 0; i < num_samples; i++) {
503       vol = *volume++;
504       for (j = 0; j < channels; j++) {
505         *data++ *= vol;
506       }
507     }
508   }
509 }
510
511 static void
512 volume_process_float (GstVolume * self, gpointer bytes, guint n_bytes)
513 {
514   gfloat *data = (gfloat *) bytes;
515   guint num_samples = n_bytes / sizeof (gfloat);
516
517   orc_scalarmultiply_f32_ns (data, self->current_volume, num_samples);
518 }
519
520 static void
521 volume_process_controlled_float (GstVolume * self, gpointer bytes,
522     gdouble * volume, guint channels, guint n_bytes)
523 {
524   gfloat *data = (gfloat *) bytes;
525   guint num_samples = n_bytes / (sizeof (gfloat) * channels);
526   guint i, j;
527   gdouble vol;
528
529   if (channels == 1) {
530     orc_process_controlled_f32_1ch (data, volume, num_samples);
531   } else if (channels == 2) {
532     orc_process_controlled_f32_2ch (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_int32 (GstVolume * self, gpointer bytes, guint n_bytes)
545 {
546   gint32 *data = (gint32 *) bytes;
547   guint num_samples = n_bytes / sizeof (gint);
548
549   /* hard coded in volume.orc */
550   g_assert (VOLUME_UNITY_INT32_BIT_SHIFT == 27);
551   orc_process_int32 (data, self->current_vol_i32, num_samples);
552 }
553
554 static void
555 volume_process_int32_clamp (GstVolume * self, gpointer bytes, guint n_bytes)
556 {
557   gint32 *data = (gint32 *) bytes;
558   guint num_samples = n_bytes / sizeof (gint);
559
560   /* hard coded in volume.orc */
561   g_assert (VOLUME_UNITY_INT32_BIT_SHIFT == 27);
562
563   orc_process_int32_clamp (data, self->current_vol_i32, num_samples);
564 }
565
566 static void
567 volume_process_controlled_int32_clamp (GstVolume * self, gpointer bytes,
568     gdouble * volume, guint channels, guint n_bytes)
569 {
570   gint32 *data = (gint32 *) bytes;
571   guint i, j;
572   guint num_samples = n_bytes / (sizeof (gint32) * channels);
573   gdouble vol, val;
574
575   if (channels == 1) {
576     orc_process_controlled_int32_1ch (data, volume, num_samples);
577   } else {
578     for (i = 0; i < num_samples; i++) {
579       vol = *volume++;
580       for (j = 0; j < channels; j++) {
581         val = *data * vol;
582         *data++ = (gint32) CLAMP (val, VOLUME_MIN_INT32, VOLUME_MAX_INT32);
583       }
584     }
585   }
586 }
587
588 #if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
589 #define get_unaligned_i24(_x) ( (((guint8*)_x)[0]) | ((((guint8*)_x)[1]) << 8) | ((((gint8*)_x)[2]) << 16) )
590
591 #define write_unaligned_u24(_x,samp) \
592 G_STMT_START { \
593   *(_x)++ = samp & 0xFF; \
594   *(_x)++ = (samp >> 8) & 0xFF; \
595   *(_x)++ = (samp >> 16) & 0xFF; \
596 } G_STMT_END
597
598 #else /* BIG ENDIAN */
599 #define get_unaligned_i24(_x) ( (((guint8*)_x)[2]) | ((((guint8*)_x)[1]) << 8) | ((((gint8*)_x)[0]) << 16) )
600 #define write_unaligned_u24(_x,samp) \
601 G_STMT_START { \
602   *(_x)++ = (samp >> 16) & 0xFF; \
603   *(_x)++ = (samp >> 8) & 0xFF; \
604   *(_x)++ = samp & 0xFF; \
605 } G_STMT_END
606 #endif
607
608 static void
609 volume_process_int24 (GstVolume * self, gpointer bytes, guint n_bytes)
610 {
611   gint8 *data = (gint8 *) bytes;        /* treat the data as a byte stream */
612   guint i, num_samples;
613   guint32 samp;
614   gint64 val;
615
616   num_samples = n_bytes / (sizeof (gint8) * 3);
617   for (i = 0; i < num_samples; i++) {
618     samp = get_unaligned_i24 (data);
619
620     val = (gint32) samp;
621     val =
622         (((gint64) self->current_vol_i24 *
623             val) >> VOLUME_UNITY_INT24_BIT_SHIFT);
624     samp = (guint32) val;
625
626     /* write the value back into the stream */
627     write_unaligned_u24 (data, samp);
628   }
629 }
630
631 static void
632 volume_process_int24_clamp (GstVolume * self, gpointer bytes, guint n_bytes)
633 {
634   gint8 *data = (gint8 *) bytes;        /* treat the data as a byte stream */
635   guint i, num_samples;
636   guint32 samp;
637   gint64 val;
638
639   num_samples = n_bytes / (sizeof (gint8) * 3);
640   for (i = 0; i < num_samples; i++) {
641     samp = get_unaligned_i24 (data);
642
643     val = (gint32) samp;
644     val =
645         (((gint64) self->current_vol_i24 *
646             val) >> VOLUME_UNITY_INT24_BIT_SHIFT);
647     samp = (guint32) CLAMP (val, VOLUME_MIN_INT24, VOLUME_MAX_INT24);
648
649     /* write the value back into the stream */
650     write_unaligned_u24 (data, samp);
651   }
652 }
653
654 static void
655 volume_process_controlled_int24_clamp (GstVolume * self, gpointer bytes,
656     gdouble * volume, guint channels, guint n_bytes)
657 {
658   gint8 *data = (gint8 *) bytes;        /* treat the data as a byte stream */
659   guint i, j;
660   guint num_samples = n_bytes / (sizeof (gint8) * 3 * channels);
661   gdouble vol, val;
662
663   for (i = 0; i < num_samples; i++) {
664     vol = *volume++;
665     for (j = 0; j < channels; j++) {
666       val = get_unaligned_i24 (data) * vol;
667       val = CLAMP (val, VOLUME_MIN_INT24, VOLUME_MAX_INT24);
668       write_unaligned_u24 (data, (gint32) val);
669     }
670   }
671 }
672
673 static void
674 volume_process_int16 (GstVolume * self, gpointer bytes, guint n_bytes)
675 {
676   gint16 *data = (gint16 *) bytes;
677   guint num_samples = n_bytes / sizeof (gint16);
678
679   /* hard coded in volume.orc */
680   g_assert (VOLUME_UNITY_INT16_BIT_SHIFT == 13);
681
682   orc_process_int16 (data, self->current_vol_i16, num_samples);
683 }
684
685 static void
686 volume_process_int16_clamp (GstVolume * self, gpointer bytes, guint n_bytes)
687 {
688   gint16 *data = (gint16 *) bytes;
689   guint num_samples = n_bytes / sizeof (gint16);
690
691   /* hard coded in volume.orc */
692   g_assert (VOLUME_UNITY_INT16_BIT_SHIFT == 13);
693
694   orc_process_int16_clamp (data, self->current_vol_i16, num_samples);
695 }
696
697 static void
698 volume_process_controlled_int16_clamp (GstVolume * self, gpointer bytes,
699     gdouble * volume, guint channels, guint n_bytes)
700 {
701   gint16 *data = (gint16 *) bytes;
702   guint i, j;
703   guint num_samples = n_bytes / (sizeof (gint16) * channels);
704   gdouble vol, val;
705
706   if (channels == 1) {
707     orc_process_controlled_int16_1ch (data, volume, num_samples);
708   } else if (channels == 2) {
709     orc_process_controlled_int16_2ch (data, volume, num_samples);
710   } else {
711     for (i = 0; i < num_samples; i++) {
712       vol = *volume++;
713       for (j = 0; j < channels; j++) {
714         val = *data * vol;
715         *data++ = (gint16) CLAMP (val, VOLUME_MIN_INT16, VOLUME_MAX_INT16);
716       }
717     }
718   }
719 }
720
721 static void
722 volume_process_int8 (GstVolume * self, gpointer bytes, guint n_bytes)
723 {
724   gint8 *data = (gint8 *) bytes;
725   guint num_samples = n_bytes / sizeof (gint8);
726
727   /* hard coded in volume.orc */
728   g_assert (VOLUME_UNITY_INT8_BIT_SHIFT == 5);
729
730   orc_process_int8 (data, self->current_vol_i8, num_samples);
731 }
732
733 static void
734 volume_process_int8_clamp (GstVolume * self, gpointer bytes, guint n_bytes)
735 {
736   gint8 *data = (gint8 *) bytes;
737   guint num_samples = n_bytes / sizeof (gint8);
738
739   /* hard coded in volume.orc */
740   g_assert (VOLUME_UNITY_INT8_BIT_SHIFT == 5);
741
742   orc_process_int8_clamp (data, self->current_vol_i8, num_samples);
743 }
744
745 static void
746 volume_process_controlled_int8_clamp (GstVolume * self, gpointer bytes,
747     gdouble * volume, guint channels, guint n_bytes)
748 {
749   gint8 *data = (gint8 *) bytes;
750   guint i, j;
751   guint num_samples = n_bytes / (sizeof (gint8) * channels);
752   gdouble val, vol;
753
754   if (channels == 1) {
755     orc_process_controlled_int8_1ch (data, volume, num_samples);
756   } else if (channels == 2) {
757     orc_process_controlled_int8_2ch (data, volume, num_samples);
758   } else {
759     for (i = 0; i < num_samples; i++) {
760       vol = *volume++;
761       for (j = 0; j < channels; j++) {
762         val = *data * vol;
763         *data++ = (gint8) CLAMP (val, VOLUME_MIN_INT8, VOLUME_MAX_INT8);
764       }
765     }
766   }
767 }
768
769 /* GstBaseTransform vmethod implementations */
770
771 /* get notified of caps and plug in the correct process function */
772 static gboolean
773 volume_setup (GstAudioFilter * filter, GstRingBufferSpec * format)
774 {
775   gboolean res;
776   GstVolume *self = GST_VOLUME (filter);
777   gfloat volume;
778   gboolean mute;
779
780   GST_OBJECT_LOCK (self);
781   volume = self->volume;
782   mute = self->mute;
783   GST_OBJECT_UNLOCK (self);
784
785   res = volume_update_volume (self, volume, mute);
786   if (!res) {
787     GST_ELEMENT_ERROR (self, CORE, NEGOTIATION,
788         ("Invalid incoming format"), (NULL));
789   }
790   self->negotiated = res;
791
792   return res;
793 }
794
795 static gboolean
796 volume_stop (GstBaseTransform * base)
797 {
798   GstVolume *self = GST_VOLUME (base);
799
800   g_free (self->volumes);
801   self->volumes = NULL;
802   self->volumes_count = 0;
803
804   g_free (self->mutes);
805   self->mutes = NULL;
806   self->mutes_count = 0;
807
808   return GST_CALL_PARENT_WITH_DEFAULT (GST_BASE_TRANSFORM_CLASS, stop, (base),
809       TRUE);
810 }
811
812 static void
813 volume_before_transform (GstBaseTransform * base, GstBuffer * buffer)
814 {
815   GstClockTime timestamp;
816   GstVolume *self = GST_VOLUME (base);
817   gfloat volume;
818   gboolean mute;
819
820   timestamp = GST_BUFFER_TIMESTAMP (buffer);
821   timestamp =
822       gst_segment_to_stream_time (&base->segment, GST_FORMAT_TIME, timestamp);
823
824   GST_DEBUG_OBJECT (base, "sync to %" GST_TIME_FORMAT,
825       GST_TIME_ARGS (timestamp));
826
827   if (GST_CLOCK_TIME_IS_VALID (timestamp))
828     gst_object_sync_values (G_OBJECT (self), timestamp);
829
830   /* get latest values */
831   GST_OBJECT_LOCK (self);
832   volume = self->volume;
833   mute = self->mute;
834   GST_OBJECT_UNLOCK (self);
835
836   if ((volume != self->current_volume) || (mute != self->current_mute)) {
837     /* the volume or mute was updated, update our internal state before
838      * we continue processing. */
839     volume_update_volume (self, volume, mute);
840   }
841 }
842
843 /* call the plugged-in process function for this instance
844  * needs to be done with this indirection since volume_transform is
845  * a class-global method
846  */
847 static GstFlowReturn
848 volume_transform_ip (GstBaseTransform * base, GstBuffer * outbuf)
849 {
850   GstVolume *self = GST_VOLUME (base);
851   guint8 *data;
852   gsize size;
853   GstControlSource *mute_csource, *volume_csource;
854
855   if (G_UNLIKELY (!self->negotiated))
856     goto not_negotiated;
857
858   /* don't process data in passthrough-mode */
859   if (gst_base_transform_is_passthrough (base) ||
860       GST_BUFFER_FLAG_IS_SET (outbuf, GST_BUFFER_FLAG_GAP))
861     return GST_FLOW_OK;
862
863   data = gst_buffer_map (outbuf, &size, NULL, GST_MAP_READWRITE);
864
865   mute_csource = gst_object_get_control_source (G_OBJECT (self), "mute");
866   volume_csource = gst_object_get_control_source (G_OBJECT (self), "volume");
867   if (mute_csource || (volume_csource && !self->current_mute)) {
868     gint rate = GST_AUDIO_FILTER_CAST (self)->format.rate;
869     gint width = GST_AUDIO_FILTER_CAST (self)->format.width / 8;
870     gint channels = GST_AUDIO_FILTER_CAST (self)->format.channels;
871     guint nsamples = size / (width * channels);
872     GstClockTime interval = gst_util_uint64_scale_int (1, GST_SECOND, rate);
873     GstClockTime ts = GST_BUFFER_TIMESTAMP (outbuf);
874     gboolean use_mutes = FALSE;
875
876     ts = gst_segment_to_stream_time (&base->segment, GST_FORMAT_TIME, ts);
877
878     if (self->mutes_count < nsamples && mute_csource) {
879       self->mutes = g_realloc (self->mutes, sizeof (gboolean) * nsamples);
880       self->mutes_count = nsamples;
881     }
882
883     if (self->volumes_count < nsamples) {
884       self->volumes = g_realloc (self->volumes, sizeof (gdouble) * nsamples);
885       self->volumes_count = nsamples;
886     }
887
888     if (mute_csource) {
889       GstValueArray va = { "mute", nsamples, interval, (gpointer) self->mutes };
890
891       if (!gst_control_source_get_value_array (mute_csource, ts, &va))
892         goto controller_failure;
893
894       gst_object_unref (mute_csource);
895       mute_csource = NULL;
896       use_mutes = TRUE;
897     } else {
898       g_free (self->mutes);
899       self->mutes = NULL;
900       self->mutes_count = 0;
901     }
902
903     if (volume_csource) {
904       GstValueArray va =
905           { "volume", nsamples, interval, (gpointer) self->volumes };
906
907       if (!gst_control_source_get_value_array (volume_csource, ts, &va))
908         goto controller_failure;
909
910       gst_object_unref (volume_csource);
911       volume_csource = NULL;
912     } else {
913       orc_memset_f64 (self->volumes, self->current_volume, nsamples);
914     }
915
916     if (use_mutes) {
917       orc_prepare_volumes (self->volumes, self->mutes, nsamples);
918     }
919
920     self->process_controlled (self, data, self->volumes, channels, size);
921
922     return GST_FLOW_OK;
923   } else if (volume_csource) {
924     gst_object_unref (volume_csource);
925   }
926
927   if (self->current_volume == 0.0 || self->current_mute) {
928     orc_memset (data, 0, size);
929     GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_GAP);
930   } else if (self->current_volume != 1.0) {
931     self->process (self, data, size);
932   }
933   gst_buffer_unmap (outbuf, data, size);
934
935   return GST_FLOW_OK;
936
937   /* ERRORS */
938 not_negotiated:
939   {
940     GST_ELEMENT_ERROR (self, CORE, NEGOTIATION,
941         ("No format was negotiated"), (NULL));
942     return GST_FLOW_NOT_NEGOTIATED;
943   }
944 controller_failure:
945   {
946     if (mute_csource)
947       gst_object_unref (mute_csource);
948     if (volume_csource)
949       gst_object_unref (volume_csource);
950
951     GST_ELEMENT_ERROR (self, CORE, FAILED,
952         ("Failed to get values from controller"), (NULL));
953     gst_buffer_unmap (outbuf, data, size);
954     return GST_FLOW_ERROR;
955   }
956 }
957
958 static void
959 volume_set_property (GObject * object, guint prop_id, const GValue * value,
960     GParamSpec * pspec)
961 {
962   GstVolume *self = GST_VOLUME (object);
963
964   switch (prop_id) {
965     case PROP_MUTE:
966       GST_OBJECT_LOCK (self);
967       self->mute = g_value_get_boolean (value);
968       GST_OBJECT_UNLOCK (self);
969       break;
970     case PROP_VOLUME:
971       GST_OBJECT_LOCK (self);
972       self->volume = g_value_get_double (value);
973       GST_OBJECT_UNLOCK (self);
974       break;
975     default:
976       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
977       break;
978   }
979 }
980
981 static void
982 volume_get_property (GObject * object, guint prop_id, GValue * value,
983     GParamSpec * pspec)
984 {
985   GstVolume *self = GST_VOLUME (object);
986
987   switch (prop_id) {
988     case PROP_MUTE:
989       GST_OBJECT_LOCK (self);
990       g_value_set_boolean (value, self->mute);
991       GST_OBJECT_UNLOCK (self);
992       break;
993     case PROP_VOLUME:
994       GST_OBJECT_LOCK (self);
995       g_value_set_double (value, self->volume);
996       GST_OBJECT_UNLOCK (self);
997       break;
998     default:
999       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1000       break;
1001   }
1002 }
1003
1004 static gboolean
1005 plugin_init (GstPlugin * plugin)
1006 {
1007   gst_volume_orc_init ();
1008
1009   /* initialize gst controller library */
1010   gst_controller_init (NULL, NULL);
1011
1012   GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "volume", 0, "Volume gain");
1013
1014   /* ref class from a thread-safe context to work around missing bit of
1015    * thread-safety in GObject */
1016   g_type_class_ref (GST_TYPE_MIXER_TRACK);
1017
1018   return gst_element_register (plugin, "volume", GST_RANK_NONE,
1019       GST_TYPE_VOLUME);
1020 }
1021
1022 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1023     GST_VERSION_MINOR,
1024     "volume",
1025     "plugin for controlling audio volume",
1026     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);