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