controller: adapt to controller api changes
[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/audio/mixer.h>
49 #include <gst/audio/gstaudiofilter.h>
50
51 #ifdef HAVE_ORC
52 #include <orc/orcfunctions.h>
53 #else
54 #define orc_memset memset
55 #endif
56
57 #include "gstvolumeorc.h"
58 #include "gstvolume.h"
59
60 /* some defines for audio processing */
61 /* the volume factor is a range from 0.0 to (arbitrary) VOLUME_MAX_DOUBLE = 10.0
62  * we map 1.0 to VOLUME_UNITY_INT*
63  */
64 #define VOLUME_UNITY_INT8            8  /* internal int for unity 2^(8-5) */
65 #define VOLUME_UNITY_INT8_BIT_SHIFT  3  /* number of bits to shift for unity */
66 #define VOLUME_UNITY_INT16           2048       /* internal int for unity 2^(16-5) */
67 #define VOLUME_UNITY_INT16_BIT_SHIFT 11 /* number of bits to shift for unity */
68 #define VOLUME_UNITY_INT24           524288     /* internal int for unity 2^(24-5) */
69 #define VOLUME_UNITY_INT24_BIT_SHIFT 19 /* number of bits to shift for unity */
70 #define VOLUME_UNITY_INT32           134217728  /* internal int for unity 2^(32-5) */
71 #define VOLUME_UNITY_INT32_BIT_SHIFT 27
72 #define VOLUME_MAX_DOUBLE            10.0
73 #define VOLUME_MAX_INT8              G_MAXINT8
74 #define VOLUME_MIN_INT8              G_MININT8
75 #define VOLUME_MAX_INT16             G_MAXINT16
76 #define VOLUME_MIN_INT16             G_MININT16
77 #define VOLUME_MAX_INT24             8388607
78 #define VOLUME_MIN_INT24             -8388608
79 #define VOLUME_MAX_INT32             G_MAXINT32
80 #define VOLUME_MIN_INT32             G_MININT32
81
82 /* number of steps we use for the mixer interface to go from 0.0 to 1.0 */
83 # define VOLUME_STEPS           100
84
85 #define GST_CAT_DEFAULT gst_volume_debug
86 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
87
88 /* Filter signals and args */
89 enum
90 {
91   /* FILL ME */
92   LAST_SIGNAL
93 };
94
95 #define DEFAULT_PROP_MUTE       FALSE
96 #define DEFAULT_PROP_VOLUME     1.0
97
98 enum
99 {
100   PROP_0,
101   PROP_MUTE,
102   PROP_VOLUME
103 };
104
105 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
106 #define ALLOWED_CAPS \
107     GST_AUDIO_CAPS_MAKE ("{ F32LE, F64LE, S8, S16LE, S24LE, S32LE }") \
108     ", layout = (string) interleaved"
109 #else
110 #define ALLOWED_CAPS \
111     GST_AUDIO_CAPS_MAKE ("{ F32BE, F64BE, S8, S16BE, S24BE, S32BE }") \
112     ", layout = (string) { interleaved, non-interleaved }"
113 #endif
114
115 static void gst_volume_mixer_init (GstMixerInterface * 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:
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
244   GST_DEBUG_OBJECT (self, "configure mute %d, volume %f", mute, volume);
245
246   if (mute) {
247     self->current_mute = TRUE;
248     self->current_volume = 0.0;
249
250     self->current_vol_i8 = 0;
251     self->current_vol_i16 = 0;
252     self->current_vol_i24 = 0;
253     self->current_vol_i32 = 0;
254
255     passthrough = FALSE;
256   } else {
257     self->current_mute = FALSE;
258     self->current_volume = volume;
259
260     self->current_vol_i8 = volume * VOLUME_UNITY_INT8;
261     self->current_vol_i16 = volume * VOLUME_UNITY_INT16;
262     self->current_vol_i24 = volume * VOLUME_UNITY_INT24;
263     self->current_vol_i32 = volume * VOLUME_UNITY_INT32;
264
265     passthrough = (self->current_vol_i16 == VOLUME_UNITY_INT16);
266   }
267
268   /* If a controller is used, never use passthrough mode
269    * because the property can change from 1.0 to something
270    * else in the middle of a buffer.
271    */
272   passthrough &= !gst_object_has_active_control_bindings (GST_OBJECT (self));
273
274   GST_DEBUG_OBJECT (self, "set passthrough %d", passthrough);
275
276   gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (self), passthrough);
277
278   res = self->negotiated = volume_choose_func (self);
279
280   return res;
281 }
282
283 /* Mixer interface */
284
285 static const GList *
286 gst_volume_list_tracks (GstMixer * mixer)
287 {
288   GstVolume *self = GST_VOLUME (mixer);
289
290   g_return_val_if_fail (self != NULL, NULL);
291   g_return_val_if_fail (GST_IS_VOLUME (self), NULL);
292
293   return self->tracklist;
294 }
295
296 static void
297 gst_volume_set_volume (GstMixer * mixer, GstMixerTrack * track, gint * volumes)
298 {
299   GstVolume *self = GST_VOLUME (mixer);
300
301   g_return_if_fail (self != NULL);
302   g_return_if_fail (GST_IS_VOLUME (self));
303
304   GST_OBJECT_LOCK (self);
305   self->volume = (gfloat) volumes[0] / VOLUME_STEPS;
306   GST_OBJECT_UNLOCK (self);
307 }
308
309 static void
310 gst_volume_get_volume (GstMixer * mixer, GstMixerTrack * track, gint * volumes)
311 {
312   GstVolume *self = GST_VOLUME (mixer);
313
314   g_return_if_fail (self != NULL);
315   g_return_if_fail (GST_IS_VOLUME (self));
316
317   GST_OBJECT_LOCK (self);
318   volumes[0] = (gint) self->volume * VOLUME_STEPS;
319   GST_OBJECT_UNLOCK (self);
320 }
321
322 static void
323 gst_volume_set_mute (GstMixer * mixer, GstMixerTrack * track, gboolean mute)
324 {
325   GstVolume *self = GST_VOLUME (mixer);
326
327   g_return_if_fail (self != NULL);
328   g_return_if_fail (GST_IS_VOLUME (self));
329
330   GST_OBJECT_LOCK (self);
331   self->mute = mute;
332   GST_OBJECT_UNLOCK (self);
333 }
334
335 static void
336 gst_volume_mixer_init (GstMixerInterface * iface)
337 {
338   GST_MIXER_TYPE (iface) = GST_MIXER_SOFTWARE;
339
340   /* default virtual functions */
341   iface->list_tracks = gst_volume_list_tracks;
342   iface->set_volume = gst_volume_set_volume;
343   iface->get_volume = gst_volume_get_volume;
344   iface->set_mute = gst_volume_set_mute;
345 }
346
347 /* Element class */
348
349 static void
350 gst_volume_dispose (GObject * object)
351 {
352   GstVolume *volume = GST_VOLUME (object);
353
354   if (volume->tracklist) {
355     if (volume->tracklist->data)
356       g_object_unref (volume->tracklist->data);
357     g_list_free (volume->tracklist);
358     volume->tracklist = NULL;
359   }
360
361   G_OBJECT_CLASS (parent_class)->dispose (object);
362 }
363
364 static void
365 gst_volume_class_init (GstVolumeClass * klass)
366 {
367   GObjectClass *gobject_class;
368   GstElementClass *element_class;
369   GstBaseTransformClass *trans_class;
370   GstAudioFilterClass *filter_class;
371   GstCaps *caps;
372
373   gobject_class = (GObjectClass *) klass;
374   element_class = (GstElementClass *) klass;
375   trans_class = (GstBaseTransformClass *) klass;
376   filter_class = (GstAudioFilterClass *) (klass);
377
378   gobject_class->set_property = volume_set_property;
379   gobject_class->get_property = volume_get_property;
380   gobject_class->dispose = gst_volume_dispose;
381
382   g_object_class_install_property (gobject_class, PROP_MUTE,
383       g_param_spec_boolean ("mute", "Mute", "mute channel",
384           DEFAULT_PROP_MUTE,
385           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
386
387   g_object_class_install_property (gobject_class, PROP_VOLUME,
388       g_param_spec_double ("volume", "Volume", "volume factor, 1.0=100%",
389           0.0, VOLUME_MAX_DOUBLE, DEFAULT_PROP_VOLUME,
390           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
391
392   gst_element_class_set_details_simple (element_class, "Volume",
393       "Filter/Effect/Audio",
394       "Set volume on audio/raw streams", "Andy Wingo <wingo@pobox.com>");
395
396   caps = gst_caps_from_string (ALLOWED_CAPS);
397   gst_audio_filter_class_add_pad_templates (filter_class, caps);
398   gst_caps_unref (caps);
399
400   trans_class->before_transform = GST_DEBUG_FUNCPTR (volume_before_transform);
401   trans_class->transform_ip = GST_DEBUG_FUNCPTR (volume_transform_ip);
402   trans_class->stop = GST_DEBUG_FUNCPTR (volume_stop);
403   filter_class->setup = GST_DEBUG_FUNCPTR (volume_setup);
404 }
405
406 static void
407 gst_volume_init (GstVolume * self)
408 {
409   GstMixerTrack *track = NULL;
410
411   self->mute = DEFAULT_PROP_MUTE;;
412   self->volume = DEFAULT_PROP_VOLUME;
413
414   self->tracklist = NULL;
415   self->negotiated = FALSE;
416
417   track = g_object_new (GST_TYPE_MIXER_TRACK, NULL);
418
419   if (GST_IS_MIXER_TRACK (track)) {
420     track->label = g_strdup ("volume");
421     track->num_channels = 1;
422     track->min_volume = 0;
423     track->max_volume = VOLUME_STEPS;
424     track->flags = GST_MIXER_TRACK_SOFTWARE;
425     self->tracklist = g_list_append (self->tracklist, track);
426   }
427
428   gst_base_transform_set_gap_aware (GST_BASE_TRANSFORM (self), TRUE);
429 }
430
431 static void
432 volume_process_double (GstVolume * self, gpointer bytes, guint n_bytes)
433 {
434   gdouble *data = (gdouble *) bytes;
435   guint num_samples = n_bytes / sizeof (gdouble);
436
437   orc_scalarmultiply_f64_ns (data, self->current_volume, num_samples);
438 }
439
440 static void
441 volume_process_controlled_double (GstVolume * self, gpointer bytes,
442     gdouble * volume, guint channels, guint n_bytes)
443 {
444   gdouble *data = (gdouble *) bytes;
445   guint num_samples = n_bytes / (sizeof (gdouble) * channels);
446   guint i, j;
447   gdouble vol;
448
449   if (channels == 1) {
450     orc_process_controlled_f64_1ch (data, volume, num_samples);
451   } else {
452     for (i = 0; i < num_samples; i++) {
453       vol = *volume++;
454       for (j = 0; j < channels; j++) {
455         *data++ *= vol;
456       }
457     }
458   }
459 }
460
461 static void
462 volume_process_float (GstVolume * self, gpointer bytes, guint n_bytes)
463 {
464   gfloat *data = (gfloat *) bytes;
465   guint num_samples = n_bytes / sizeof (gfloat);
466
467   orc_scalarmultiply_f32_ns (data, self->current_volume, num_samples);
468 }
469
470 static void
471 volume_process_controlled_float (GstVolume * self, gpointer bytes,
472     gdouble * volume, guint channels, guint n_bytes)
473 {
474   gfloat *data = (gfloat *) bytes;
475   guint num_samples = n_bytes / (sizeof (gfloat) * channels);
476   guint i, j;
477   gdouble vol;
478
479   if (channels == 1) {
480     orc_process_controlled_f32_1ch (data, volume, num_samples);
481   } else if (channels == 2) {
482     orc_process_controlled_f32_2ch (data, volume, num_samples);
483   } else {
484     for (i = 0; i < num_samples; i++) {
485       vol = *volume++;
486       for (j = 0; j < channels; j++) {
487         *data++ *= vol;
488       }
489     }
490   }
491 }
492
493 static void
494 volume_process_int32 (GstVolume * self, gpointer bytes, guint n_bytes)
495 {
496   gint32 *data = (gint32 *) bytes;
497   guint num_samples = n_bytes / sizeof (gint);
498
499   /* hard coded in volume.orc */
500   g_assert (VOLUME_UNITY_INT32_BIT_SHIFT == 27);
501   orc_process_int32 (data, self->current_vol_i32, num_samples);
502 }
503
504 static void
505 volume_process_int32_clamp (GstVolume * self, gpointer bytes, guint n_bytes)
506 {
507   gint32 *data = (gint32 *) bytes;
508   guint num_samples = n_bytes / sizeof (gint);
509
510   /* hard coded in volume.orc */
511   g_assert (VOLUME_UNITY_INT32_BIT_SHIFT == 27);
512
513   orc_process_int32_clamp (data, self->current_vol_i32, num_samples);
514 }
515
516 static void
517 volume_process_controlled_int32_clamp (GstVolume * self, gpointer bytes,
518     gdouble * volume, guint channels, guint n_bytes)
519 {
520   gint32 *data = (gint32 *) bytes;
521   guint i, j;
522   guint num_samples = n_bytes / (sizeof (gint32) * channels);
523   gdouble vol, val;
524
525   if (channels == 1) {
526     orc_process_controlled_int32_1ch (data, volume, num_samples);
527   } else {
528     for (i = 0; i < num_samples; i++) {
529       vol = *volume++;
530       for (j = 0; j < channels; j++) {
531         val = *data * vol;
532         *data++ = (gint32) CLAMP (val, VOLUME_MIN_INT32, VOLUME_MAX_INT32);
533       }
534     }
535   }
536 }
537
538 #if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
539 #define get_unaligned_i24(_x) ( (((guint8*)_x)[0]) | ((((guint8*)_x)[1]) << 8) | ((((gint8*)_x)[2]) << 16) )
540
541 #define write_unaligned_u24(_x,samp) \
542 G_STMT_START { \
543   *(_x)++ = samp & 0xFF; \
544   *(_x)++ = (samp >> 8) & 0xFF; \
545   *(_x)++ = (samp >> 16) & 0xFF; \
546 } G_STMT_END
547
548 #else /* BIG ENDIAN */
549 #define get_unaligned_i24(_x) ( (((guint8*)_x)[2]) | ((((guint8*)_x)[1]) << 8) | ((((gint8*)_x)[0]) << 16) )
550 #define write_unaligned_u24(_x,samp) \
551 G_STMT_START { \
552   *(_x)++ = (samp >> 16) & 0xFF; \
553   *(_x)++ = (samp >> 8) & 0xFF; \
554   *(_x)++ = samp & 0xFF; \
555 } G_STMT_END
556 #endif
557
558 static void
559 volume_process_int24 (GstVolume * self, gpointer bytes, guint n_bytes)
560 {
561   gint8 *data = (gint8 *) bytes;        /* treat the data as a byte stream */
562   guint i, num_samples;
563   guint32 samp;
564   gint64 val;
565
566   num_samples = n_bytes / (sizeof (gint8) * 3);
567   for (i = 0; i < num_samples; i++) {
568     samp = get_unaligned_i24 (data);
569
570     val = (gint32) samp;
571     val =
572         (((gint64) self->current_vol_i24 *
573             val) >> VOLUME_UNITY_INT24_BIT_SHIFT);
574     samp = (guint32) val;
575
576     /* write the value back into the stream */
577     write_unaligned_u24 (data, samp);
578   }
579 }
580
581 static void
582 volume_process_int24_clamp (GstVolume * self, gpointer bytes, guint n_bytes)
583 {
584   gint8 *data = (gint8 *) bytes;        /* treat the data as a byte stream */
585   guint i, num_samples;
586   guint32 samp;
587   gint64 val;
588
589   num_samples = n_bytes / (sizeof (gint8) * 3);
590   for (i = 0; i < num_samples; i++) {
591     samp = get_unaligned_i24 (data);
592
593     val = (gint32) samp;
594     val =
595         (((gint64) self->current_vol_i24 *
596             val) >> VOLUME_UNITY_INT24_BIT_SHIFT);
597     samp = (guint32) CLAMP (val, VOLUME_MIN_INT24, VOLUME_MAX_INT24);
598
599     /* write the value back into the stream */
600     write_unaligned_u24 (data, samp);
601   }
602 }
603
604 static void
605 volume_process_controlled_int24_clamp (GstVolume * self, gpointer bytes,
606     gdouble * volume, guint channels, guint n_bytes)
607 {
608   gint8 *data = (gint8 *) bytes;        /* treat the data as a byte stream */
609   guint i, j;
610   guint num_samples = n_bytes / (sizeof (gint8) * 3 * channels);
611   gdouble vol, val;
612
613   for (i = 0; i < num_samples; i++) {
614     vol = *volume++;
615     for (j = 0; j < channels; j++) {
616       val = get_unaligned_i24 (data) * vol;
617       val = CLAMP (val, VOLUME_MIN_INT24, VOLUME_MAX_INT24);
618       write_unaligned_u24 (data, (gint32) val);
619     }
620   }
621 }
622
623 static void
624 volume_process_int16 (GstVolume * self, gpointer bytes, guint n_bytes)
625 {
626   gint16 *data = (gint16 *) bytes;
627   guint num_samples = n_bytes / sizeof (gint16);
628
629   /* hard coded in volume.orc */
630   g_assert (VOLUME_UNITY_INT16_BIT_SHIFT == 11);
631
632   orc_process_int16 (data, self->current_vol_i16, num_samples);
633 }
634
635 static void
636 volume_process_int16_clamp (GstVolume * self, gpointer bytes, guint n_bytes)
637 {
638   gint16 *data = (gint16 *) bytes;
639   guint num_samples = n_bytes / sizeof (gint16);
640
641   /* hard coded in volume.orc */
642   g_assert (VOLUME_UNITY_INT16_BIT_SHIFT == 11);
643
644   orc_process_int16_clamp (data, self->current_vol_i16, num_samples);
645 }
646
647 static void
648 volume_process_controlled_int16_clamp (GstVolume * self, gpointer bytes,
649     gdouble * volume, guint channels, guint n_bytes)
650 {
651   gint16 *data = (gint16 *) bytes;
652   guint i, j;
653   guint num_samples = n_bytes / (sizeof (gint16) * channels);
654   gdouble vol, val;
655
656   if (channels == 1) {
657     orc_process_controlled_int16_1ch (data, volume, num_samples);
658   } else if (channels == 2) {
659     orc_process_controlled_int16_2ch (data, volume, num_samples);
660   } else {
661     for (i = 0; i < num_samples; i++) {
662       vol = *volume++;
663       for (j = 0; j < channels; j++) {
664         val = *data * vol;
665         *data++ = (gint16) CLAMP (val, VOLUME_MIN_INT16, VOLUME_MAX_INT16);
666       }
667     }
668   }
669 }
670
671 static void
672 volume_process_int8 (GstVolume * self, gpointer bytes, guint n_bytes)
673 {
674   gint8 *data = (gint8 *) bytes;
675   guint num_samples = n_bytes / sizeof (gint8);
676
677   /* hard coded in volume.orc */
678   g_assert (VOLUME_UNITY_INT8_BIT_SHIFT == 3);
679
680   orc_process_int8 (data, self->current_vol_i8, num_samples);
681 }
682
683 static void
684 volume_process_int8_clamp (GstVolume * self, gpointer bytes, guint n_bytes)
685 {
686   gint8 *data = (gint8 *) bytes;
687   guint num_samples = n_bytes / sizeof (gint8);
688
689   /* hard coded in volume.orc */
690   g_assert (VOLUME_UNITY_INT8_BIT_SHIFT == 3);
691
692   orc_process_int8_clamp (data, self->current_vol_i8, num_samples);
693 }
694
695 static void
696 volume_process_controlled_int8_clamp (GstVolume * self, gpointer bytes,
697     gdouble * volume, guint channels, guint n_bytes)
698 {
699   gint8 *data = (gint8 *) bytes;
700   guint i, j;
701   guint num_samples = n_bytes / (sizeof (gint8) * channels);
702   gdouble val, vol;
703
704   if (channels == 1) {
705     orc_process_controlled_int8_1ch (data, volume, num_samples);
706   } else if (channels == 2) {
707     orc_process_controlled_int8_2ch (data, volume, num_samples);
708   } else {
709     for (i = 0; i < num_samples; i++) {
710       vol = *volume++;
711       for (j = 0; j < channels; j++) {
712         val = *data * vol;
713         *data++ = (gint8) CLAMP (val, VOLUME_MIN_INT8, VOLUME_MAX_INT8);
714       }
715     }
716   }
717 }
718
719 /* GstBaseTransform vmethod implementations */
720
721 /* get notified of caps and plug in the correct process function */
722 static gboolean
723 volume_setup (GstAudioFilter * filter, const GstAudioInfo * info)
724 {
725   gboolean res;
726   GstVolume *self = GST_VOLUME (filter);
727   gfloat volume;
728   gboolean mute;
729
730   GST_OBJECT_LOCK (self);
731   volume = self->volume;
732   mute = self->mute;
733   GST_OBJECT_UNLOCK (self);
734
735   res = volume_update_volume (self, volume, mute);
736   if (!res) {
737     GST_ELEMENT_ERROR (self, CORE, NEGOTIATION,
738         ("Invalid incoming format"), (NULL));
739   }
740   self->negotiated = res;
741
742   return res;
743 }
744
745 static gboolean
746 volume_stop (GstBaseTransform * base)
747 {
748   GstVolume *self = GST_VOLUME (base);
749
750   g_free (self->volumes);
751   self->volumes = NULL;
752   self->volumes_count = 0;
753
754   g_free (self->mutes);
755   self->mutes = NULL;
756   self->mutes_count = 0;
757
758   return GST_CALL_PARENT_WITH_DEFAULT (GST_BASE_TRANSFORM_CLASS, stop, (base),
759       TRUE);
760 }
761
762 static void
763 volume_before_transform (GstBaseTransform * base, GstBuffer * buffer)
764 {
765   GstClockTime timestamp;
766   GstVolume *self = GST_VOLUME (base);
767   gfloat volume;
768   gboolean mute;
769
770   timestamp = GST_BUFFER_TIMESTAMP (buffer);
771   timestamp =
772       gst_segment_to_stream_time (&base->segment, GST_FORMAT_TIME, timestamp);
773
774   GST_DEBUG_OBJECT (base, "sync to %" GST_TIME_FORMAT,
775       GST_TIME_ARGS (timestamp));
776
777   if (GST_CLOCK_TIME_IS_VALID (timestamp))
778     gst_object_sync_values (GST_OBJECT (self), timestamp);
779
780   /* get latest values */
781   GST_OBJECT_LOCK (self);
782   volume = self->volume;
783   mute = self->mute;
784   GST_OBJECT_UNLOCK (self);
785
786   if ((volume != self->current_volume) || (mute != self->current_mute)) {
787     /* the volume or mute was updated, update our internal state before
788      * we continue processing. */
789     volume_update_volume (self, volume, mute);
790   }
791 }
792
793 /* call the plugged-in process function for this instance
794  * needs to be done with this indirection since volume_transform is
795  * a class-global method
796  */
797 static GstFlowReturn
798 volume_transform_ip (GstBaseTransform * base, GstBuffer * outbuf)
799 {
800   GstAudioFilter *filter = GST_AUDIO_FILTER_CAST (base);
801   GstVolume *self = GST_VOLUME (base);
802   guint8 *data;
803   gsize size;
804   GstControlBinding *mute_cb, *volume_cb;
805
806   if (G_UNLIKELY (!self->negotiated))
807     goto not_negotiated;
808
809   /* don't process data in passthrough-mode */
810   if (gst_base_transform_is_passthrough (base) ||
811       GST_BUFFER_FLAG_IS_SET (outbuf, GST_BUFFER_FLAG_GAP))
812     return GST_FLOW_OK;
813
814   data = gst_buffer_map (outbuf, &size, NULL, GST_MAP_READWRITE);
815
816   mute_cb = gst_object_get_control_binding (GST_OBJECT (self), "mute");
817   volume_cb = gst_object_get_control_binding (GST_OBJECT (self), "volume");
818
819   if (mute_cb || (volume_cb && !self->current_mute)) {
820     gint rate = GST_AUDIO_INFO_RATE (&filter->info);
821     gint width = GST_AUDIO_FORMAT_INFO_WIDTH (filter->info.finfo) / 8;
822     gint channels = GST_AUDIO_INFO_CHANNELS (&filter->info);
823     guint nsamples = size / (width * channels);
824     GstClockTime interval = gst_util_uint64_scale_int (1, GST_SECOND, rate);
825     GstClockTime ts = GST_BUFFER_TIMESTAMP (outbuf);
826     gboolean use_mutes = FALSE;
827
828     ts = gst_segment_to_stream_time (&base->segment, GST_FORMAT_TIME, ts);
829
830     if (self->mutes_count < nsamples && mute_cb) {
831       self->mutes = g_realloc (self->mutes, sizeof (gboolean) * nsamples);
832       self->mutes_count = nsamples;
833     }
834
835     if (self->volumes_count < nsamples) {
836       self->volumes = g_realloc (self->volumes, sizeof (gdouble) * nsamples);
837       self->volumes_count = nsamples;
838     }
839
840     if (mute_cb) {
841       if (!gst_control_binding_get_value_array (mute_cb, ts, interval,
842               nsamples, (gpointer) self->mutes))
843         goto controller_failure;
844
845       gst_object_replace ((GstObject **) & mute_cb, NULL);
846       use_mutes = TRUE;
847     } else {
848       g_free (self->mutes);
849       self->mutes = NULL;
850       self->mutes_count = 0;
851     }
852
853     if (volume_cb) {
854       if (!gst_control_binding_get_value_array (volume_cb, ts, interval,
855               nsamples, (gpointer) self->volumes))
856         goto controller_failure;
857
858       gst_object_replace ((GstObject **) & volume_cb, NULL);
859     } else {
860       orc_memset_f64 (self->volumes, self->current_volume, nsamples);
861     }
862
863     if (use_mutes) {
864       orc_prepare_volumes (self->volumes, self->mutes, nsamples);
865     }
866
867     self->process_controlled (self, data, self->volumes, channels, size);
868
869     return GST_FLOW_OK;
870   } else if (volume_cb) {
871     gst_object_unref (volume_cb);
872   }
873
874   if (self->current_volume == 0.0 || self->current_mute) {
875     orc_memset (data, 0, size);
876     GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_GAP);
877   } else if (self->current_volume != 1.0) {
878     self->process (self, data, size);
879   }
880   gst_buffer_unmap (outbuf, data, size);
881
882   return GST_FLOW_OK;
883
884   /* ERRORS */
885 not_negotiated:
886   {
887     GST_ELEMENT_ERROR (self, CORE, NEGOTIATION,
888         ("No format was negotiated"), (NULL));
889     return GST_FLOW_NOT_NEGOTIATED;
890   }
891 controller_failure:
892   {
893     if (mute_cb)
894       gst_object_unref (mute_cb);
895     if (volume_cb)
896       gst_object_unref (volume_cb);
897
898     GST_ELEMENT_ERROR (self, CORE, FAILED,
899         ("Failed to get values from controller"), (NULL));
900     gst_buffer_unmap (outbuf, data, size);
901     return GST_FLOW_ERROR;
902   }
903 }
904
905 static void
906 volume_set_property (GObject * object, guint prop_id, const GValue * value,
907     GParamSpec * pspec)
908 {
909   GstVolume *self = GST_VOLUME (object);
910
911   switch (prop_id) {
912     case PROP_MUTE:
913       GST_OBJECT_LOCK (self);
914       self->mute = g_value_get_boolean (value);
915       GST_OBJECT_UNLOCK (self);
916       break;
917     case PROP_VOLUME:
918       GST_OBJECT_LOCK (self);
919       self->volume = g_value_get_double (value);
920       GST_OBJECT_UNLOCK (self);
921       break;
922     default:
923       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
924       break;
925   }
926 }
927
928 static void
929 volume_get_property (GObject * object, guint prop_id, GValue * value,
930     GParamSpec * pspec)
931 {
932   GstVolume *self = GST_VOLUME (object);
933
934   switch (prop_id) {
935     case PROP_MUTE:
936       GST_OBJECT_LOCK (self);
937       g_value_set_boolean (value, self->mute);
938       GST_OBJECT_UNLOCK (self);
939       break;
940     case PROP_VOLUME:
941       GST_OBJECT_LOCK (self);
942       g_value_set_double (value, self->volume);
943       GST_OBJECT_UNLOCK (self);
944       break;
945     default:
946       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
947       break;
948   }
949 }
950
951 static gboolean
952 plugin_init (GstPlugin * plugin)
953 {
954   gst_volume_orc_init ();
955
956   GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "volume", 0, "Volume gain");
957
958   /* ref class from a thread-safe context to work around missing bit of
959    * thread-safety in GObject */
960   g_type_class_ref (GST_TYPE_MIXER_TRACK);
961
962   return gst_element_register (plugin, "volume", GST_RANK_NONE,
963       GST_TYPE_VOLUME);
964 }
965
966 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
967     GST_VERSION_MINOR,
968     "volume",
969     "plugin for controlling audio volume",
970     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);