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